:root {
  color-scheme: dark;
  --bg: #070a18;
  --bg-2: #0d1230;
  --ink: #f4f6ff;
  --muted: #9aa3c7;
  --faint: rgba(190, 200, 255, .14);
  --gold: #ffb938;
  --cyan: #45d6ff;
  --pink: #ff4f8b;
  --green: #5cf2a5;
  --bar: 48px;
  --replay-bar: 84px;
  --radius: 14px;
  --display: "Russo One", "Arial Black", Impact, sans-serif;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overflow: hidden; background: var(--bg); color: var(--ink); }
body {
  background:
    radial-gradient(60vmax 50vmax at 50% 110%, rgba(255, 185, 56, .14), transparent 60%),
    radial-gradient(60vmax 50vmax at 50% -10%, rgba(69, 214, 255, .14), transparent 60%),
    var(--bg);
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
input { -webkit-user-select: text; user-select: text; }

.app {
  height: 100svh;
  display: grid;
  place-items: center;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.stage {
  --w: min(100vw, 480px, calc((100svh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 2 * var(--bar)) * .5625));
  width: var(--w);
  display: grid;
  grid-template-rows: var(--bar) auto var(--bar);
}

/* ---------- player bars ---------- */
.player-bar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  transition: opacity .3s;
}
.app[data-screen="title"] .player-bar, .app[data-screen="wait"] .player-bar { opacity: 0; pointer-events: none; }
.app[data-screen="game"] .mute-button, .app[data-screen="pause"] .mute-button, .app[data-screen="result"] .mute-button, .app[data-screen="replay"] .mute-button { display: none; }
.player-bar .who { display: flex; align-items: center; gap: 8px; min-width: 0; }
.player-bar strong { font-size: 15px; font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.player-bar .dot { flex: none; width: 10px; height: 10px; border-radius: 50%; background: var(--c, var(--ink)); box-shadow: 0 0 10px var(--c, transparent); }
.player-bar .tag { flex: none; color: var(--muted); font-size: 12px; font-weight: 700; }
.player-bar .score { display: flex; justify-content: flex-end; align-items: center; gap: 10px; }
.pips { display: flex; gap: 5px; }
.pips i { width: 12px; height: 12px; border-radius: 3px; border: 2px solid var(--c, var(--muted)); opacity: .45; }
.pips i.on { background: var(--c); opacity: 1; box-shadow: 0 0 8px var(--c); }
.rounds { font-family: var(--display); font-size: 14px; color: var(--c, var(--ink)); letter-spacing: .06em; }
.round-clock { font-family: var(--display); font-size: 18px; font-variant-numeric: tabular-nums; letter-spacing: .04em; }
.round-clock.hot { color: var(--pink); }
.net { color: var(--muted); font-size: 12px; font-weight: 700; font-variant-numeric: tabular-nums; }

/* ---------- board ---------- */
.board {
  position: relative;
  aspect-ratio: 9 / 16;
  width: 100%;
  overflow: hidden;
  border-radius: var(--radius);
  background: #060918;
  box-shadow: 0 0 0 1px var(--faint), 0 30px 80px rgba(0, 0, 0, .55);
  touch-action: none;
}
#game-canvas { display: block; width: 100%; height: 100%; touch-action: none; }

.icon-button {
  display: grid; place-items: center;
  width: 40px; height: 40px;
  border: 1px solid var(--faint); border-radius: 12px;
  background: rgba(7, 10, 24, .6);
  font-weight: 800; font-size: 14px; letter-spacing: -.05em;
  backdrop-filter: blur(6px);
}
.menu-button { width: 34px; height: 30px; border-radius: 9px; font-size: 12px; }
.app:not([data-screen="game"]) .menu-button { visibility: hidden; }
.mute-button { position: fixed; top: calc(10px + env(safe-area-inset-top)); right: calc(10px + env(safe-area-inset-right)); }
.mute-button[aria-pressed="true"] { color: var(--muted); text-decoration: line-through; }

/* ---------- screens ---------- */
.screen {
  position: absolute; inset: 0;
  display: none;
  flex-direction: column; align-items: center; justify-content: center;
  gap: 12px;
  padding: 24px 22px;
  text-align: center;
  background: rgba(6, 9, 24, .78);
  backdrop-filter: blur(3px);
  animation: fade-in .25s ease-out;
}
.app[data-screen="title"] [data-for="title"],
.app[data-screen="wait"] [data-for="wait"],
.app[data-screen="result"] [data-for="result"],
.app[data-screen="pause"] [data-for="pause"] { display: flex; }
.title-screen { background: linear-gradient(rgba(6, 9, 24, .55), rgba(6, 9, 24, .86) 45%); justify-content: flex-start; padding-top: 9%; overflow-y: auto; }
@keyframes fade-in { from { opacity: 0; } }

.logo { display: flex; flex-direction: column; align-items: center; line-height: .86; font-family: var(--display); margin-bottom: 2px; }
.logo-top { font-size: clamp(34px, 11.5vw, 54px); letter-spacing: .02em; color: var(--ink); text-shadow: 0 0 24px rgba(69, 214, 255, .55); }
.logo-bottom { font-size: clamp(46px, 16vw, 74px); letter-spacing: .04em; background: linear-gradient(180deg, #ffe08a, var(--gold) 55%, #ff7a2f); -webkit-background-clip: text; background-clip: text; color: transparent; filter: drop-shadow(0 0 18px rgba(255, 185, 56, .45)); }
.tagline { margin: 0 0 8px; color: var(--muted); font-size: 15px; font-weight: 700; }

.name-field { display: flex; align-items: center; gap: 10px; width: 100%; max-width: 300px; padding: 0 12px; border: 1px solid var(--faint); border-radius: 12px; background: rgba(255, 255, 255, .04); }
.name-field span { color: var(--muted); font-size: 13px; font-weight: 700; white-space: nowrap; }
.name-field input { flex: 1; min-width: 0; height: 42px; border: 0; outline: 0; background: transparent; font-weight: 800; font-size: 16px; text-align: right; }

button.primary, button.secondary, button.ghost {
  min-height: 46px; padding: 0 20px;
  border-radius: 12px;
  font-weight: 800; font-size: 16px; letter-spacing: .01em;
  transition: transform .08s, filter .15s, background .15s;
}
button:active { transform: scale(.97); }
button.primary { width: 100%; max-width: 300px; border: 0; color: #1b1203; background: linear-gradient(180deg, #ffd36b, var(--gold)); box-shadow: 0 8px 26px rgba(255, 185, 56, .35), inset 0 -3px 0 rgba(0, 0, 0, .18); }
button.primary:hover { filter: brightness(1.08); }
button.primary.big { min-height: 58px; font-family: var(--display); font-size: 22px; font-weight: 400; letter-spacing: .04em; }
button.secondary { border: 1px solid rgba(69, 214, 255, .55); color: var(--cyan); background: rgba(69, 214, 255, .08); }
button.ghost { border: 1px solid var(--faint); background: rgba(255, 255, 255, .04); color: var(--ink); }
button.ghost.danger { color: var(--pink); border-color: rgba(255, 79, 139, .4); }
button:disabled { opacity: .5; cursor: default; }

.bot-row { display: grid; grid-template-columns: 1fr; gap: 8px; width: 100%; max-width: 300px; }
.chips { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.chips button { min-height: 34px; border: 1px solid var(--faint); border-radius: 10px; background: transparent; color: var(--muted); font-size: 13px; font-weight: 800; }
.chips button[aria-checked="true"] { color: var(--ink); border-color: var(--cyan); background: rgba(69, 214, 255, .14); }
.room-row { display: grid; grid-template-columns: 1fr; gap: 8px; width: 100%; max-width: 300px; margin-top: 4px; }
.room-row .ghost { min-height: 42px; font-size: 14px; }
.join-form { display: grid; grid-template-columns: 1fr auto; gap: 6px; }
.join-form input { height: 42px; min-width: 0; padding: 0 12px; border: 1px solid var(--faint); border-radius: 12px; background: rgba(255, 255, 255, .04); font-family: var(--display); font-size: 16px; letter-spacing: .2em; text-transform: uppercase; outline: 0; }
.join-form input:focus, .name-field:focus-within { border-color: var(--cyan); }

.how { margin: 6px 0 0; padding: 0; list-style: none; max-width: 300px; color: var(--muted); font-size: 13px; line-height: 1.45; text-align: left; counter-reset: how; }
.how li { position: relative; padding-left: 24px; margin-top: 5px; counter-increment: how; }
.how li::before { content: counter(how); position: absolute; left: 0; top: 1px; width: 17px; height: 17px; border-radius: 50%; background: var(--faint); color: var(--ink); font-size: 11px; font-weight: 800; text-align: center; line-height: 17px; }
.how b { color: var(--ink); }
.how i { color: var(--gold); font-style: normal; font-weight: 800; }
.record { margin: 2px 0 0; color: var(--muted); font-size: 12px; font-weight: 700; min-height: 1em; }

h2 { margin: 0; font-family: var(--display); font-weight: 400; font-size: clamp(28px, 9vw, 40px); letter-spacing: .02em; }
.screen p { margin: 0; max-width: 300px; color: var(--muted); font-size: 15px; line-height: 1.45; }
.screen .primary:not(.big), .screen .ghost, .screen .secondary { width: 100%; max-width: 300px; }

.spinner { width: 56px; height: 56px; border-radius: 50%; border: 4px solid var(--faint); border-top-color: var(--gold); border-right-color: var(--cyan); animation: spin .9s linear infinite; margin-bottom: 6px; }
@keyframes spin { to { transform: rotate(360deg); } }
.room-code { font-family: var(--display); font-size: 44px; letter-spacing: .18em; padding: 8px 4px 8px 22px; border: 2px dashed var(--faint); border-radius: 14px; color: var(--gold); }

.kicker { color: var(--muted); font-size: 13px; font-weight: 800; letter-spacing: .14em; text-transform: uppercase; }
.result-screen h2 { font-size: clamp(44px, 15vw, 68px); line-height: 1; }
.result-screen h2.win { background: linear-gradient(180deg, #ffe08a, var(--gold) 55%, #ff7a2f); -webkit-background-clip: text; background-clip: text; color: transparent; filter: drop-shadow(0 0 18px rgba(255, 185, 56, .5)); }
.result-screen h2.lose { color: var(--pink); text-shadow: 0 0 22px rgba(255, 79, 139, .45); }
.result-score { font-family: var(--display); font-size: 26px !important; color: var(--ink) !important; }
.stats { width: 100%; max-width: 300px; border-collapse: collapse; margin: 4px 0 6px; font-size: 14px; }
.stats th { color: var(--muted); font-weight: 700; text-align: left; padding: 6px 0; }
.stats td { font-family: var(--display); text-align: center; padding: 6px 0; width: 64px; font-size: 16px; }
.stats thead td { font-family: inherit; font-size: 12px; color: var(--muted); font-weight: 800; }
.stats tr + tr { border-top: 1px solid var(--faint); }
.rematch-note { min-height: 1.2em; color: var(--green) !important; font-weight: 800; }

.toast { position: absolute; left: 50%; top: 9%; transform: translateX(-50%); max-width: 86%; padding: 10px 16px; border-radius: 12px; background: rgba(7, 10, 24, .9); border: 1px solid var(--faint); font-size: 14px; font-weight: 700; text-align: center; pointer-events: none; animation: fade-in .2s; }

@media (max-height: 640px) {
  .title-screen { gap: 8px; padding-top: 5%; }
  .how { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* ---------- power-up slots ---------- */
.player-bar .inv { flex: none; color: var(--c, var(--muted)); font-size: 12px; letter-spacing: .15em; opacity: .85; }
.slots {
  position: absolute; left: 50%; bottom: 5.9%; transform: translateX(-50%);
  display: none; gap: 10px; z-index: 2;
}
.app[data-screen="game"] .slots { display: flex; }
.app[data-screen="replay"] .slots { display: flex; pointer-events: none; }
.slot {
  --slot: clamp(32px, 10.5vw, 40px);
  width: var(--slot); height: var(--slot);
  border-radius: 12px;
  border: 2px solid var(--c, var(--faint));
  background: rgba(7, 10, 24, .55);
  color: var(--c, var(--muted));
  font-family: var(--display); font-size: calc(var(--slot) * .42);
  display: grid; place-items: center;
  touch-action: none;
  transition: transform .08s, background .15s, opacity .15s;
}
.slot[aria-disabled="true"] { border-style: dashed; opacity: .4; cursor: default; }
.slot:not([aria-disabled="true"]) { background: color-mix(in srgb, var(--c) 22%, rgba(7, 10, 24, .7)); box-shadow: 0 0 14px color-mix(in srgb, var(--c) 55%, transparent); }
.slot:not([aria-disabled="true"]):active { transform: scale(.92); }
.slot.waiting:not([aria-disabled="true"]) { background: rgba(7, 10, 24, .7); box-shadow: none; opacity: .6; }
.slot.fresh { animation: slot-pop .35s ease-out; }
.slot.denied { animation: slot-deny .3s ease-out; }
@keyframes slot-pop { from { transform: scale(1.35); } to { transform: scale(1); } }
@keyframes slot-deny { 20%, 60% { transform: translateX(-4px); } 40%, 80% { transform: translateX(4px); } }
@media (prefers-reduced-motion: reduce) { .slot.fresh, .slot.denied { animation: none; } }

.app[data-screen="game"], .app[data-screen="game"] * { touch-action: none; }

/* ---------- replay ---------- */
.button-pair { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; width: 100%; max-width: 300px; }
.button-pair .ghost { padding: 0 10px; }
.replay-bar { display: none; }
.app[data-screen="replay"] .stage {
  --w: min(100vw, 480px, calc((100svh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 2 * var(--bar) - var(--replay-bar)) * .5625));
  grid-template-rows: var(--bar) auto var(--bar) var(--replay-bar);
}
.app[data-screen="replay"] .replay-bar { display: grid; align-content: center; gap: 8px; padding: 0 12px; }
.replay-track { position: relative; height: 22px; }
.replay-marks { position: absolute; top: 0; bottom: 0; left: 9px; right: 9px; pointer-events: none; }
.replay-marks i { position: absolute; transform: translateX(-50%); }
.replay-marks .round { top: 4px; width: 2px; height: 14px; border-radius: 1px; background: var(--muted); opacity: .6; }
.replay-marks .goal { top: 0; width: 7px; height: 7px; border-radius: 50%; background: var(--c); box-shadow: 0 0 6px var(--c); }
#replay-seek { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; background: transparent; -webkit-appearance: none; appearance: none; cursor: pointer; }
#replay-seek:focus-visible { outline: 2px solid var(--cyan); outline-offset: 2px; border-radius: 6px; }
#replay-seek::-webkit-slider-runnable-track { height: 4px; border-radius: 2px; background: var(--faint); }
#replay-seek::-moz-range-track { height: 4px; border-radius: 2px; background: var(--faint); }
#replay-seek::-webkit-slider-thumb { -webkit-appearance: none; appearance: none; width: 18px; height: 18px; margin-top: -7px; border: 0; border-radius: 50%; background: var(--gold); box-shadow: 0 0 10px rgba(255, 185, 56, .6); }
#replay-seek::-moz-range-thumb { width: 18px; height: 18px; border: 0; border-radius: 50%; background: var(--gold); box-shadow: 0 0 10px rgba(255, 185, 56, .6); }
.replay-controls { display: grid; grid-template-columns: auto auto 1fr auto auto; align-items: center; gap: 8px; }
.replay-controls .icon-button { width: 38px; height: 36px; border-radius: 10px; font-size: 13px; letter-spacing: 0; }
.replay-time { text-align: center; font-family: var(--display); font-size: 14px; font-variant-numeric: tabular-nums; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
