:root {
  --bg: #0b1020;
  --line: rgba(255,255,255,.20);
  --text: #f4f8ff;
  --muted: rgba(244,248,255,.62);

  /* Saturated team colours — the resting state is already vivid, and the
     serving state lifts further rather than being the only bright moment. */
  --left: #1466b8;
  --left-serve: #35a0ff;
  --right: #d0472a;
  --right-serve: #ff7a45;

  --panel: #151c2e;
  --panel-line: rgba(255,255,255,.16);
  --accent: #35a0ff;
  --win: #ffd23f;

  /* Vertical space the control bar reserves at the bottom of each side. */
  --hud-clearance: 50px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
  touch-action: manipulation;
}

/* ---------- Board ---------- */

#board {
  position: fixed;
  inset: 0;
  display: flex;
}

.side {
  flex: 1 1 50%;
  position: relative;
  border: 0;
  margin: 0;
  padding: 0;
  font: inherit;
  color: inherit;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color .18s ease;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

#side-left  { background: var(--left);  padding-left: env(safe-area-inset-left); }
#side-right { background: var(--right); padding-right: env(safe-area-inset-right); }

#side-left.serving  { background: color-mix(in srgb, var(--left-serve) 55%, var(--left)); }
#side-right.serving { background: color-mix(in srgb, var(--right-serve) 55%, var(--right)); }

/* ---------- Trophy ---------- */

.trophy-wrap {
  height: 0;
  overflow: visible;
  display: flex;
  justify-content: center;
  opacity: 0;
  transform: scale(.4) translateY(10px);
  transition: opacity .3s ease, transform .3s ease;
  pointer-events: none;
}

.trophy-shine { stroke: rgba(255,255,255,.45); }

.trophy {
  width: clamp(36px, 10vh, 74px);
  height: auto;
  color: var(--win);
  filter: drop-shadow(0 4px 14px rgba(0,0,0,.45));
}

/* Only the winning side reveals its trophy. */
.side.won .trophy-wrap {
  height: auto;
  opacity: 1;
  transform: scale(1) translateY(0);
  margin-bottom: .35em;
  animation: trophyPop .55s cubic-bezier(.2,1.5,.4,1) both;
}

@keyframes trophyPop {
  0%   { transform: scale(.3) rotate(-18deg); opacity: 0; }
  60%  { transform: scale(1.12) rotate(4deg); }
  100% { transform: scale(1) rotate(0); opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .side.won .trophy-wrap { animation: none; }
}

/* ---------- Win state ---------- */

/* Both winners get the same bright green, independent of team colour. */
.side.won {
  background:
    radial-gradient(ellipse 120% 90% at 50% 42%,
                    #1eaa53 0%, #14913f 48%, #0d7534 100%);
}

.side.won { animation: winPulse 1.6s ease-in-out infinite; }
.side.won .player-name { color: rgba(255,255,255,.92); }
.side.won .score {
  color: #fff;
  text-shadow: 0 2px 16px rgba(0,0,0,.30);
}
.side.won .trophy-shine { stroke: rgba(255,255,255,.5); }

.side.won .trophy {
  color: var(--win);
  filter: drop-shadow(0 3px 10px rgba(4,40,18,.5));
}
.side.won .games { color: rgba(255,255,255,.78); }

@keyframes winPulse {
  0%, 100% { filter: brightness(1) saturate(1); }
  50%      { filter: brightness(1.06) saturate(1.12); }
}

/* The side that didn't win recedes. */
.side.lost { opacity: .45; }
.side.lost .score { transform: scale(.9); }

@media (prefers-reduced-motion: reduce) {
  .side.won { animation: none; }
}

.side.flash::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(255,255,255,.10);
  animation: flash .25s ease-out forwards;
  pointer-events: none;
}
@keyframes flash { to { opacity: 0; } }

.side-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .1em;
  pointer-events: none;
  /* Offset the HUD so the score stays optically centred above it. */
  margin-bottom: var(--hud-clearance);
}

.player-name {
  font-size: clamp(14px, 3.4vh, 30px);
  font-weight: 600;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--muted);
  max-width: 42vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.side.serving .player-name { color: rgba(255,255,255,.8); }

.score {
  font-size: min(40vh, 32vw);
  line-height: .92;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: -.04em;
  transition: transform .12s ease;
}

.side.bump .score { transform: scale(1.08); }

.serve-badge {
  font-size: clamp(9px, 1.6vh, 13px);
  font-weight: 800;
  letter-spacing: .22em;
  padding: .3em .7em;
  border-radius: 999px;
  background: rgba(255,255,255,.16);
  opacity: 0;
  transition: opacity .18s ease;
  margin-bottom: .5em;
}
.side.serving .serve-badge { opacity: 1; }

.games {
  font-size: clamp(11px, 1.8vh, 15px);
  letter-spacing: .12em;
  color: var(--muted);
  margin-top: .6em;
}

.divider {
  position: absolute;
  left: 50%;
  top: 0;
  bottom: 0;
  width: 2px;
  margin-left: -1px;
  background: var(--line);
  pointer-events: none;
}

/* ---------- Fullscreen button ---------- */

.fs-btn {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 9px);
  right: calc(env(safe-area-inset-right) + 10px);
  z-index: 7;
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  padding: 0;
  border-radius: 13px;
  border: 1px solid rgba(255,255,255,.12);
  background: rgba(0,0,0,.30);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  color: rgba(244,248,255,.85);
  cursor: pointer;
}
.fs-btn:active { background: rgba(255,255,255,.16); }

.fs-icon {
  width: 21px;
  height: 21px;
  display: block;
}

/* Swap the glyph based on the state the document is actually in. */
.fs-icon-exit { display: none; }
body.is-fullscreen .fs-icon-enter { display: none; }
body.is-fullscreen .fs-icon-exit  { display: block; }

/* ---------- App title ---------- */

.app-title {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 9px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 6;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 5px 14px 5px 11px;
  border-radius: 999px;
  background: rgba(0,0,0,.30);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(255,255,255,.10);
  pointer-events: none;
  user-select: none;
}

/* The paddle emoji as the title mark — drawn as text, so it carries no
   background of its own. */
.app-title-mark {
  font-size: 17px;
  line-height: 1;
  flex: none;
  display: block;
  animation: markBob 2.8s ease-in-out infinite;
}
@keyframes markBob {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-1.5px); }
}

.app-title-text {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .13em;
  text-transform: uppercase;
  color: rgba(244,248,255,.80);
  white-space: nowrap;
  line-height: 1;
}
.app-title-text span { color: var(--win); }

@media (prefers-reduced-motion: reduce) {
  .app-title-mark { animation: none; }
}

/* ---------- HUD ---------- */

.hud {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom) + 8px);
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 22px;
  background: rgba(0,0,0,.42);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--panel-line);
  z-index: 5;
  max-width: calc(100vw - 16px);
}

/* Last resort on very narrow screens: drop the status text, keep the buttons. */
@media (max-width: 420px) {
  .hud-status { display: none; }
}

.hud-btn {
  min-width: 68px;
  min-height: 58px;
  padding: 6px 12px;
  border-radius: 16px;
  border: 0;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  line-height: 1;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;
}
.hud-btn:active { background: rgba(255,255,255,.16); }
.hud-btn:disabled { opacity: .28; }

.hud-icon {
  width: 26px;
  height: 26px;
  display: block;
}

.hud-label {
  font-size: 12px;
  line-height: 1;
  font-weight: 600;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: rgba(233,238,245,.72);
}

.hud-status.is-win {
  color: var(--win);
  font-weight: 700;
}

.hud-status {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--muted);
  padding: 0 14px;
  white-space: nowrap;
}

/* ---------- Winner banner ---------- */

.banner {
  position: absolute;
  inset: 0;
  z-index: 8;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: .6em;
  gap: .3em;
  background: radial-gradient(ellipse at center,
              rgba(5,8,12,.72) 0%, rgba(5,8,12,.88) 100%);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  animation: fade .25s ease;
}
@keyframes fade { from { opacity: 0 } }

.banner-trophy {
  line-height: 0;
  animation: trophyIn .5s cubic-bezier(.2,1.5,.4,1) both;
}
.banner-trophy .trophy {
  width: clamp(44px, 11vh, 82px);
}
@keyframes trophyIn {
  from { transform: scale(0) rotate(-25deg); opacity: 0; }
}

.banner-text {
  font-size: clamp(26px, 9.5vh, 84px);
  font-weight: 800;
  letter-spacing: -.02em;
  text-align: center;
  padding: 0 6vw;
  color: var(--win);
  animation: bannerIn .38s cubic-bezier(.2,1.2,.3,1) both .06s;
}

.banner-score {
  font-size: clamp(18px, 4.5vh, 34px);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: .02em;
  animation: bannerIn .38s cubic-bezier(.2,1.2,.3,1) both .12s;
}

@keyframes bannerIn {
  from { transform: translateY(14px); opacity: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .banner-trophy, .banner-text, .banner-score { animation: none; }
}
.banner-sub {
  font-size: 12px;
  letter-spacing: .18em;
  text-transform: uppercase;
  color: var(--muted);
}

/* ---------- Overlays / panels ---------- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0,0,0,.6);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  overflow: auto;
}

.panel {
  width: min(560px, 100%);
  max-height: 100%;
  overflow: auto;
  background: var(--panel);
  border: 1px solid var(--panel-line);
  border-radius: 16px;
  padding: 24px;
  animation: pop .16s ease;
}
@keyframes pop { from { transform: scale(.97); opacity: 0 } }

.panel h2 {
  margin: 0 0 20px;
  font-size: 15px;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 700;
}

.field { margin-bottom: 22px; }

.field-row {
  display: flex;
  gap: 18px;
}
.field-row > .field { flex: 1; min-width: 0; }

.switch-row {
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
}

/* Two switches side by side reclaim another row. */
.field > .switch + .switch { margin-left: 0; }

.field > label {
  display: block;
  font-size: 12px;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 10px;
}

input[type="text"],
input[type="number"] {
  width: 100%;
  padding: 11px 12px;
  font-size: 16px;
  color: var(--text);
  background: rgba(255,255,255,.05);
  border: 1px solid var(--panel-line);
  border-radius: 10px;
  font-family: inherit;
}
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.segmented {
  display: flex;
  gap: 8px;
}
.segmented button {
  flex: 1;
  padding: 11px 8px;
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  color: var(--text);
  background: rgba(255,255,255,.05);
  border: 1px solid var(--panel-line);
  border-radius: 10px;
  cursor: pointer;
}
.segmented button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.switch {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 6px 0;
  font-size: 14px;
  cursor: pointer;
}
.switch input { width: 20px; height: 20px; accent-color: var(--accent); }

.adjust-grid {
  display: flex;
  gap: 18px;
  margin-bottom: 24px;
}
.adjust-col { flex: 1; text-align: center; }
.adjust-label {
  font-size: 12px;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.stepper {
  display: flex;
  align-items: center;
  gap: 12px;
}
.step-btn {
  width: 48px;
  height: 48px;
  flex: none;
  font-size: 24px;
  font-family: inherit;
  color: var(--text);
  background: rgba(255,255,255,.06);
  border: 1px solid var(--panel-line);
  border-radius: 12px;
  cursor: pointer;
}
.step-btn:active { background: rgba(255,255,255,.16); }
.step-value {
  flex: 1;
  font-size: 38px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.help-text {
  margin: 0 0 14px;
  font-size: 14px;
  line-height: 1.5;
  color: rgba(244,248,255,.78);
}

.help-steps {
  margin: 0 0 20px;
  padding-left: 20px;
  font-size: 14px;
  line-height: 1.65;
  color: rgba(244,248,255,.78);
}
.help-steps li { margin-bottom: 7px; }
.help-steps strong { color: var(--text); font-weight: 700; }

/* The button opens help rather than toggling, so it reads as informational. */
.fs-btn-hint { color: rgba(244,248,255,.7); }

.panel-actions {
  display: flex;
  gap: 12px;
  margin-top: 8px;
  padding-top: 20px;
  border-top: 1px solid var(--panel-line);
}
.btn {
  flex: 1;
  padding: 13px;
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  color: var(--text);
  background: rgba(255,255,255,.06);
  border: 1px solid var(--panel-line);
  border-radius: 11px;
  cursor: pointer;
}
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ---------- Compact settings for short (landscape phone) viewports ---------- */

@media (max-height: 480px) {
  .overlay { padding: 8px; align-items: flex-start; }

  .panel {
    padding: 18px 20px;
    border-radius: 14px;
  }
  .panel h2 { margin-bottom: 14px; font-size: 12px; }

  .field { margin-bottom: 15px; }
  .field > label { margin-bottom: 7px; font-size: 11px; }
  .field-row { gap: 14px; }
  .switch-row { gap: 18px; }

  /* 16px keeps iOS from zooming the page on focus, so it stays. */
  input[type="text"] { padding: 9px 11px; }

  .segmented button { padding: 9px 6px; font-size: 14px; }

  .switch { padding: 5px 0; font-size: 13px; }
  .switch input { width: 18px; height: 18px; }

  .panel-actions { padding-top: 14px; margin-top: 4px; }
  .btn { padding: 11px; font-size: 14px; }

  /* Score adjust panel shrinks too. */
  .adjust-grid { margin-bottom: 10px; }
  .step-btn { width: 40px; height: 40px; font-size: 20px; }
  .step-value { font-size: 28px; }

  /* Title shrinks so it stays clear of the score and the win trophy. */
  .app-title {
    top: calc(env(safe-area-inset-top) + 6px);
    padding: 4px 10px 4px 6px;
    gap: 6px;
  }
  .app-title-text { font-size: 11px; letter-spacing: .11em; }
  .app-title-mark { font-size: 15px; }

  .fs-btn {
    top: calc(env(safe-area-inset-top) + 6px);
    right: calc(env(safe-area-inset-right) + 8px);
    width: 36px;
    height: 36px;
    border-radius: 11px;
  }
  .fs-icon { width: 18px; height: 18px; }

  /* Keep the controls comfortably tappable, just a little tighter. */
  :root { --hud-clearance: 44px; }
  .hud {
    bottom: calc(env(safe-area-inset-bottom) + 5px);
    gap: 4px;
    padding: 5px 8px;
  }
  .hud-btn { min-width: 62px; min-height: 52px; padding: 5px 9px; gap: 4px; }
  .hud-icon { width: 23px; height: 23px; }
  .hud-label { font-size: 11px; }
  .hud-status { font-size: 12px; padding: 0 10px; }
}

/* Very short viewports (small phones in landscape) trade a little air to fit. */
@media (max-height: 360px) {
  .overlay { padding: 6px; }
  .panel { padding: 12px 16px; }
  .panel h2 { margin-bottom: 9px; }
  .field { margin-bottom: 10px; }
  .field > label { margin-bottom: 5px; }
  .switch { padding: 3px 0; }
  .panel-actions { padding-top: 10px; margin-top: 2px; }
  .btn { padding: 9px; }
}
