:root {
  --cream: #f3e9d2;
  --gold: #d9a441;
  --burgundy: #6d1f2a;
  --burgundy-hi: #8a2a37;
  --ink: #120d0a;
  --panel: rgba(22, 15, 11, 0.86);
  --line: rgba(217, 164, 65, 0.28);
  --heading: 'Barlow Condensed', 'Arial Narrow', sans-serif;
  --body: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --top: #ff8a5c;
  --back: #5fd0ff;
  --side: #c99bff;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none; /* no pull-to-refresh or rubber-banding while playing */
  background: var(--ink);
  color: var(--cream);
  font-family: var(--body);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}

/* Mouse-only and touch-only text (the `touch` class is set by src/device.js). */
html.touch .only-mouse,
html:not(.touch) .only-touch {
  display: none;
}

@media (orientation: landscape) {
  .only-portrait {
    display: none !important;
  }
}

#scene {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

body.playing #scene {
  cursor: none;
}

[hidden] {
  display: none !important;
}

/* --- Score bug (top left, broadcast style) --- */

#hud {
  position: fixed;
  inset: 0;
  pointer-events: none;
}

.bug {
  position: absolute;
  top: max(18px, env(safe-area-inset-top));
  left: max(18px, env(safe-area-inset-left));
  min-width: 190px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  font-family: var(--heading);
}

.bug-head {
  padding: 5px 12px;
  background: var(--burgundy);
  color: var(--gold);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.bug-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 12px;
  border-top: 1px solid rgba(255, 255, 255, 0.05);
}

.bug-row .name {
  flex: 1;
  font-size: 20px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.bug-row b {
  font-size: 30px;
  font-weight: 800;
  min-width: 1.4em;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.bug-foot {
  padding: 3px 12px 5px;
  font-size: 11px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  opacity: 0.5;
}

.serve-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--gold);
  box-shadow: 0 0 10px var(--gold);
  visibility: hidden;
}

.serve-dot.on {
  visibility: visible;
}

/* --- Shot callout --- */

#shot {
  position: absolute;
  top: max(22px, env(safe-area-inset-top));
  left: 50%;
  transform: translate(-50%, -6px);
  padding: 5px 14px;
  border-radius: 999px;
  font-family: var(--heading);
  font-size: 17px;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  background: var(--panel);
  border: 1px solid currentColor;
  color: var(--cream);
  opacity: 0;
  transition: opacity 0.18s, transform 0.18s;
}

#shot.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

#shot[data-spin='topspin'],
.k.top {
  color: var(--top);
}

#shot[data-spin='backspin'],
.k.back {
  color: var(--back);
}

#shot[data-spin='sidespin'],
.k.side {
  color: var(--side);
}

.k {
  font-weight: 600;
}

/* --- Point / serve messages (lower third) --- */

#message {
  position: fixed;
  left: 50%;
  bottom: max(9%, env(safe-area-inset-bottom));
  transform: translate(-50%, 8px);
  padding: 10px 22px 11px;
  border-radius: 8px;
  background: var(--panel);
  border-left: 4px solid var(--gold);
  font-family: var(--heading);
  font-size: 26px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  text-align: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  max-width: 90vw;
}

#message.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

#message small {
  display: block;
  margin-top: 2px;
  font-family: var(--body);
  font-size: 13px;
  font-weight: 400;
  letter-spacing: 0;
  text-transform: none;
  opacity: 0.7;
}

/* --- Overlays --- */

.overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: radial-gradient(ellipse at center, rgba(18, 13, 10, 0.2), rgba(18, 13, 10, 0.78));
  padding: 20px;
  padding-top: max(20px, env(safe-area-inset-top));
  padding-bottom: max(20px, env(safe-area-inset-bottom));
  padding-left: max(20px, env(safe-area-inset-left));
  padding-right: max(20px, env(safe-area-inset-right));
}

.panel {
  width: min(480px, 100%);
  max-height: calc(100vh - 40px);
  max-height: calc(100dvh - 40px);
  overflow: auto;
  overscroll-behavior: contain;
  touch-action: pan-y; /* the panel scrolls on short screens */
  padding: 30px 30px 24px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: 0 30px 70px rgba(0, 0, 0, 0.5);
  text-align: center;
  backdrop-filter: blur(4px);
}

.poster {
  border-top: 5px solid var(--burgundy);
}

.kicker {
  margin: 0 0 6px;
  font-family: var(--heading);
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--gold);
}

h1 {
  margin: 0 0 6px;
  font-family: var(--heading);
  font-size: 44px;
  font-weight: 800;
  letter-spacing: 0.01em;
  text-transform: uppercase;
  line-height: 1;
}

.poster h1 {
  font-size: 64px;
}

h1 span {
  color: var(--gold);
}

.tagline {
  margin: 0 0 22px;
  opacity: 0.72;
  font-size: 14px;
}

.buttons {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}

button {
  touch-action: manipulation;
  font: inherit;
  font-family: var(--heading);
  font-size: 19px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 10px 20px;
  border-radius: 10px;
  border: 1px solid rgba(243, 233, 210, 0.2);
  background: rgba(243, 233, 210, 0.06);
  color: inherit;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s, border-color 0.15s;
}

@media (hover: hover) {
  button:hover:not(:disabled) {
    background: rgba(243, 233, 210, 0.14);
    border-color: rgba(217, 164, 65, 0.6);
  }

  button.primary:hover:not(:disabled) {
    background: var(--burgundy-hi);
  }
}

button:active:not(:disabled) {
  transform: scale(0.97);
}

button:disabled {
  opacity: 0.4;
  cursor: progress;
}

button.primary {
  background: var(--burgundy);
  border-color: var(--gold);
}

.levels button {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 124px;
  padding: 10px 16px 9px;
}

.levels small {
  font-family: var(--body);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.02em;
  text-transform: none;
  opacity: 0.7;
}

.loading {
  margin: 12px 0 0;
  font-size: 13px;
  color: var(--gold);
}

.loading:empty,
.record:empty {
  display: none;
}

.record {
  margin: 14px 0 0;
  font-size: 13px;
  opacity: 0.65;
}

.howto-wrap {
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
  text-align: left;
}

.howto-wrap summary {
  cursor: pointer;
  font-family: var(--heading);
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--gold);
  text-align: center;
  list-style: none;
}

.howto-wrap summary::-webkit-details-marker {
  display: none;
}

.howto-wrap summary::after {
  content: ' +';
}

.howto-wrap[open] summary::after {
  content: ' –';
}

.howto {
  margin: 12px 0 0;
  padding: 0;
  list-style: none;
  font-size: 13.5px;
  line-height: 1.5;
  opacity: 0.85;
}

.howto li + li {
  margin-top: 7px;
}

.sensitivity {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-top: 16px;
  font-size: 13px;
  opacity: 0.8;
}

.sensitivity input {
  width: 140px;
  accent-color: var(--gold);
}

.sensitivity output {
  min-width: 2.6em;
  text-align: left;
  font-variant-numeric: tabular-nums;
}

/* --- Game over stats --- */

.stats {
  width: 100%;
  margin: 0 0 20px;
  border-collapse: collapse;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}

.stats th {
  font-family: var(--heading);
  font-size: 13px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--gold);
  padding-bottom: 6px;
}

.stats td {
  padding: 4px 0;
  border-top: 1px solid rgba(243, 233, 210, 0.07);
}

.stats td:first-child {
  text-align: left;
  opacity: 0.75;
}

.stats td:not(:first-child),
.stats th {
  width: 22%;
  text-align: center;
}

/* --- On-screen buttons (pause, sound, fullscreen) --- */

.hud-buttons {
  position: absolute;
  top: max(18px, env(safe-area-inset-top));
  right: max(18px, env(safe-area-inset-right));
  display: flex;
  gap: 8px;
  pointer-events: auto;
}

button.icon {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 12px;
  background: var(--panel);
  border-color: var(--line);
}

button.icon[hidden] {
  display: none;
}

button.icon svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: var(--cream);
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

button.icon svg .fill,
button.icon svg .spk {
  fill: var(--cream);
  stroke: none;
}

button.icon svg .slash {
  display: none;
  stroke: var(--gold);
}

button.icon.off svg .wave {
  display: none;
}

button.icon.off svg .slash {
  display: block;
}

.tip {
  margin: 14px 0 0;
  font-size: 13px;
  color: var(--gold);
}

/* --- Touch: bigger targets --- */

html.touch .sensitivity input {
  width: min(220px, 46vw);
  height: 32px;
}

html.touch .howto {
  font-size: 14.5px;
}

/* --- Phones held upright --- */

@media (max-width: 560px) {
  .poster h1 {
    font-size: 52px;
  }

  .panel {
    padding: 24px 18px 20px;
  }

  .levels {
    flex-direction: column;
  }

  .levels button {
    width: 100%;
    min-height: 58px;
  }

  /* The near end of the table (and your bat) sits low on a tall screen, so messages go up top. */
  #message {
    bottom: auto;
    top: calc(max(12px, env(safe-area-inset-top)) + 142px);
    font-size: 20px;
    padding: 7px 16px 8px;
    max-width: 92vw;
  }

  #message small {
    font-size: 12px;
  }

  #shot {
    font-size: 14px;
    top: calc(max(12px, env(safe-area-inset-top)) + 104px);
  }
}

/* Small screens of either shape: a compact score box and buttons so they never collide. */
@media (max-width: 560px), (max-height: 520px) {
  #message .hint {
    display: none; /* the full instructions are under "How to play" */
  }

  .bug {
    top: max(10px, env(safe-area-inset-top));
    left: max(10px, env(safe-area-inset-left));
    min-width: 132px;
    border-radius: 8px;
  }

  .bug-head {
    padding: 3px 9px;
    font-size: 10px;
  }

  .bug-row {
    padding: 1px 9px;
    gap: 6px;
  }

  .bug-row .name {
    font-size: 15px;
  }

  .bug-row b {
    font-size: 23px;
  }

  .bug-foot {
    display: none;
  }

  .hud-buttons {
    top: max(10px, env(safe-area-inset-top));
    right: max(10px, env(safe-area-inset-right));
    gap: 6px;
  }

  button.icon {
    width: 44px;
    height: 44px;
  }
}

@media (max-width: 380px) {
  .poster h1 {
    font-size: 44px;
  }
}

/* --- Short screens (phones held sideways) --- */

@media (max-height: 520px) {
  #shot {
    top: max(8px, env(safe-area-inset-top));
    font-size: 13px;
    padding: 3px 11px;
  }

  /* The near end of the table is low on a short screen, so messages go up top, under the pill. */
  #message {
    bottom: auto;
    top: calc(max(8px, env(safe-area-inset-top)) + 40px);
    padding: 6px 16px 7px;
    font-size: 20px;
    max-width: 60vw;
  }

  #message small {
    font-size: 11.5px;
  }

  .overlay {
    padding-top: max(10px, env(safe-area-inset-top));
    padding-bottom: max(10px, env(safe-area-inset-bottom));
  }

  .panel {
    padding: 14px 22px 12px;
    max-height: calc(100vh - 20px);
    max-height: calc(100dvh - 20px);
  }

  .kicker {
    font-size: 12px;
    margin-bottom: 2px;
  }

  h1,
  .poster h1 {
    font-size: 40px;
  }

  .tagline {
    margin-bottom: 12px;
    font-size: 13px;
  }

  .levels button {
    padding: 7px 14px 6px;
    min-width: 116px;
  }

  .howto-wrap {
    margin-top: 10px;
    padding-top: 8px;
  }

  .sensitivity {
    margin-top: 8px;
  }

  .record {
    margin-top: 8px;
  }

  .stats {
    margin-bottom: 10px;
    font-size: 13px;
  }

  .stats td {
    padding: 2px 0;
  }
}

/* Tablets: messages up top as on phones, so they never cover your bat and the ball. */
@media (min-width: 561px) and (min-height: 521px) {
  html.touch #message {
    bottom: auto;
    top: calc(max(12px, env(safe-area-inset-top)) + 84px);
  }

  html.touch #shot {
    top: max(22px, env(safe-area-inset-top));
  }
}
