/* ====================== Theme ====================== */
:root {
  --bg: #0f1226;
  --bg2: #1a1f3d;
  --panel: #20264a;
  --ink: #f3f5ff;
  --muted: #9aa3cf;
  --accent: #ffd24d;
  --accent2: #4d8bff;
  --good: #37d67a;
  --bad: #ff5a5a;
  --radius: 16px;
  --road: #2b2f4f;
  font-synthesis: none;
}

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

html, body { height: 100%; margin: 0; }

body {
  background: radial-gradient(1200px 600px at 50% -10%, #2a2f5e 0%, var(--bg) 60%);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

.app {
  max-width: 920px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 18px 32px;
  min-height: 100%;
}

/* ====================== Screens ====================== */
.screen { display: none; animation: fade .25s ease; }
.screen.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

h1, h2 { text-align: center; }
.logo { font-size: clamp(2rem, 8vw, 3.4rem); margin: 28px 0 6px; letter-spacing: -1px; }
.tagline { text-align: center; color: var(--muted); margin: 0 0 28px; font-size: 1.05rem; }
.fineprint { text-align: center; color: var(--muted); font-size: .85rem; margin-top: 28px; }
.muted { color: var(--muted); }
.help { color: var(--muted); font-size: .85rem; }
.hint { text-align: center; color: var(--muted); margin-top: 14px; }

/* ====================== Buttons ====================== */
.btn {
  appearance: none; border: 0; cursor: pointer;
  background: linear-gradient(180deg, var(--accent), #ffb800);
  color: #1a1400; font-weight: 800; font-size: 1.05rem;
  padding: 14px 22px; border-radius: 999px;
  box-shadow: 0 6px 0 #b88200, 0 8px 18px rgba(0,0,0,.35);
  transition: transform .06s ease, box-shadow .06s ease, opacity .2s;
}
.btn:active { transform: translateY(4px); box-shadow: 0 2px 0 #b88200, 0 4px 10px rgba(0,0,0,.35); }
.btn.big { font-size: 1.25rem; padding: 18px 30px; width: 100%; }
.btn.small { font-size: .9rem; padding: 10px 16px; }
.btn.alt { background: linear-gradient(180deg, var(--accent2), #2f6bff); color: #fff; box-shadow: 0 6px 0 #1f47b8, 0 8px 18px rgba(0,0,0,.35); }
.btn.alt:active { box-shadow: 0 2px 0 #1f47b8, 0 4px 10px rgba(0,0,0,.35); }
.btn:disabled { opacity: .45; cursor: not-allowed; transform: none; }
.home-btns { display: grid; gap: 14px; max-width: 380px; margin: 0 auto; }
.row { display: flex; gap: 12px; justify-content: center; margin-top: 18px; }
.row .btn { flex: 1; }

.home-msg { text-align: center; color: var(--bad); margin-top: 18px; min-height: 1.2em; opacity: 0; transition: opacity .2s; }
.home-msg.show { opacity: 1; }

/* visible keyboard focus for accessibility */
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 8px; }

/* sound on/off toggle, fixed top-right */
.sound-toggle {
  position: fixed; top: 10px; right: 10px; z-index: 60;
  width: 44px; height: 44px; border: 0; border-radius: 50%; cursor: pointer;
  background: var(--panel); color: var(--ink); font-size: 1.2rem;
  box-shadow: 0 2px 10px rgba(0,0,0,.4);
}
.sound-toggle:active { transform: scale(.92); }

/* ====================== Forms ====================== */
label { display: block; margin: 16px 0 6px; font-weight: 700; color: var(--muted); }
.code-input, .text-input {
  width: 100%; padding: 14px 16px; font-size: 1.2rem;
  background: var(--panel); color: var(--ink);
  border: 2px solid #34406f; border-radius: 12px; outline: none;
}
.code-input { letter-spacing: 12px; text-align: center; font-weight: 800; font-size: 2rem; }
.code-input:focus, .text-input:focus { border-color: var(--accent); }
#join { max-width: 420px; margin: 30px auto 0; }

.age-options { display: flex; flex-wrap: wrap; gap: 10px; }
.age-btn {
  flex: 1 1 60px; padding: 12px 6px; border-radius: 12px; cursor: pointer;
  background: var(--panel); color: var(--ink); border: 2px solid #34406f;
  font-weight: 800; font-size: 1rem;
}
.age-btn.sel { border-color: var(--accent); background: #2c3566; color: var(--accent); }
.status { text-align: center; min-height: 1.3em; color: var(--accent); margin-top: 14px; }

/* ====================== Code box (host) ====================== */
.code-box {
  text-align: center; font-size: 1.2rem; color: var(--muted);
  margin: 20px auto; font-weight: 700;
}
.code-box span {
  display: inline-block; font-size: clamp(2.6rem, 12vw, 4.5rem); font-weight: 900;
  letter-spacing: 10px; color: var(--accent); margin-left: 8px;
  text-shadow: 0 0 24px rgba(255,210,77,.5);
}

/* ====================== Roster (lobby) ====================== */
.roster-head { font-weight: 800; margin: 22px 0 10px; color: var(--muted); text-align: center; }
.roster-head span { color: var(--accent); }
.roster { list-style: none; padding: 0; margin: 0 auto; max-width: 480px; display: grid; gap: 8px; }
.roster li {
  display: flex; align-items: center; gap: 10px;
  background: var(--panel); padding: 10px 14px; border-radius: 12px;
}
.roster li.muted { justify-content: center; color: var(--muted); }
.car { font-size: 1.5rem; }
.pname { font-weight: 800; }
.badge { margin-left: auto; font-size: .75rem; background: #34406f; color: var(--ink); padding: 3px 10px; border-radius: 999px; }
.you { display: flex; align-items: center; justify-content: center; gap: 10px; font-size: 1.3rem; font-weight: 800; margin: 14px 0; }

/* ====================== Track (host race) ====================== */
.race-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.race-head h2 { margin: 14px 0; }

.track { display: grid; gap: 10px; margin: 12px 0 18px; }
.lane { background: var(--panel); border-radius: 14px; padding: 8px 10px; }
.lane-label { font-weight: 800; font-size: .9rem; margin-bottom: 4px; }
.lane-road {
  position: relative; height: 44px; border-radius: 10px;
  background:
    repeating-linear-gradient(90deg, transparent 0 38px, rgba(255,255,255,.12) 38px 56px),
    var(--road);
  border-bottom: 3px dashed rgba(255,255,255,.12);
  overflow: hidden;
}
.car-el {
  position: absolute; left: 0; top: 50%;
  transform: translate3d(0,0,0);
  margin-top: -16px; font-size: 2rem; line-height: 1;
  will-change: transform;
}
.flag { position: absolute; right: 4px; top: 50%; margin-top: -14px; font-size: 1.6rem; opacity: .9; }

/* ====================== 3D race stage (host) ====================== */
.stage {
  position: relative; width: 100%;
  aspect-ratio: 16 / 9; max-height: 58vh;
  margin: 10px 0 16px; border-radius: 16px; overflow: hidden;
  background: linear-gradient(180deg, #8fd3ff 0%, #cdeeff 100%);
  box-shadow: 0 12px 34px rgba(0,0,0,.4); border: 1px solid rgba(255,255,255,.12);
}
.scene3d { display: block; width: 100%; height: 100%; }

/* manga-style "speed lines" overlay — shown only while a car is boosting (the
   3D layer toggles .boosting on .stage). Radial streaks, masked clear at center. */
.stage::after {
  content: ""; position: absolute; inset: 0; z-index: 2; pointer-events: none;
  opacity: 0; transition: opacity .18s ease;
  background: repeating-conic-gradient(from 0deg at 50% 50%,
    rgba(255,255,255,.22) 0deg .7deg, transparent .7deg 5deg);
  -webkit-mask-image: radial-gradient(circle at 50% 50%, transparent 42%, #000 78%);
          mask-image: radial-gradient(circle at 50% 50%, transparent 42%, #000 78%);
}
.stage.boosting::after { opacity: .85; animation: speedlines .22s steps(2) infinite; }
@keyframes speedlines { 50% { opacity: .45; } }

/* Hype-man MC speech bubble. The bar handles horizontal centering so anime.js
   is free to own the bubble's transform (translateY/scale) for the pop-in. */
.mc-bar {
  position: absolute; left: 0; right: 0; bottom: 16px; z-index: 3;
  display: flex; justify-content: center; padding: 0 14px; pointer-events: none;
}
.mc-bubble {
  display: none; align-items: center; gap: 10px;
  max-width: min(92%, 720px); padding: 12px 22px; border-radius: 999px;
  background: linear-gradient(180deg, #ffffff, #ffe39a);
  color: #221a00; font-weight: 900; font-size: clamp(1rem, 3.2vw, 1.55rem);
  border: 3px solid #ffb800; box-shadow: 0 8px 24px rgba(0,0,0,.45);
  text-shadow: 0 1px 0 rgba(255,255,255,.6);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.mc-bubble.show { display: inline-flex; }
.mc-mic { font-size: 1.35em; filter: drop-shadow(0 0 5px rgba(255,184,0,.85)); }

/* turbo flame trailing the player's own mini-car (driven by host 'boost' state) */
.mini-car.boosting { filter: drop-shadow(0 0 10px #ff9b2f) drop-shadow(0 0 18px #ffd24d); }
.mini-car.boosting::before {
  content: "🔥"; position: absolute; right: 100%; top: 50%;
  transform: translateY(-50%); font-size: .85em; margin-right: -6px;
  animation: flicker .18s steps(2) infinite;
}
@keyframes flicker { 50% { opacity: .5; transform: translateY(-50%) scaleX(.8); } }

/* ====================== Countdown overlay ====================== */
.countdown {
  position: fixed; inset: 0; display: none; place-items: center;
  font-size: clamp(5rem, 30vw, 14rem); font-weight: 900; color: var(--accent);
  background: rgba(8,10,24,.7); z-index: 50; text-shadow: 0 0 40px rgba(255,210,77,.6);
}
.countdown.show { display: grid; animation: pop .4s ease; }
@keyframes pop { from { transform: scale(.4); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* ====================== Leaderboard (host) ====================== */
.leaderboard { display: grid; gap: 6px; }
.lb-row {
  display: grid; grid-template-columns: 34px 30px 1fr 90px 50px 70px 64px;
  align-items: center; gap: 8px;
  background: var(--panel); padding: 8px 10px; border-radius: 10px; font-size: .9rem;
}
.lb-rank { font-size: 1.1rem; text-align: center; }
.lb-car { font-size: 1.3rem; }
.lb-name { font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-bar { background: var(--road); border-radius: 999px; height: 12px; overflow: hidden; }
.lb-bar i { display: block; height: 100%; border-radius: 999px; transition: width .12s linear; }
.lb-fuel { background: var(--road); border-radius: 999px; height: 8px; overflow: hidden; }
.lb-fuel i { display: block; height: 100%; background: linear-gradient(90deg,#ff8a3d,var(--accent)); transition: width .12s linear; }
.lb-wpm { color: var(--muted); text-align: right; font-variant-numeric: tabular-nums; }
.lb-status { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }

/* ====================== Results ====================== */
.results { display: grid; gap: 8px; max-width: 560px; margin: 18px auto; }
.res-row {
  display: grid; grid-template-columns: 40px 34px 1fr auto auto auto;
  gap: 10px; align-items: center;
  background: var(--panel); padding: 12px 14px; border-radius: 12px;
}
.res-row:first-child { background: linear-gradient(90deg, #3a3a16, var(--panel)); border: 1px solid var(--accent); }
.res-rank { font-size: 1.3rem; text-align: center; }
.res-car { font-size: 1.5rem; }
.res-name { font-weight: 800; }
.res-badge { font-size: .75rem; color: var(--muted); }
.res-stat { color: var(--muted); font-size: .85rem; font-variant-numeric: tabular-nums; }
#host-again, #host-results h2, #player-results { text-align: center; }
#host-again { max-width: 380px; margin: 8px auto 0; display: block; }

/* ====================== Player race HUD ====================== */
.phud { display: flex; justify-content: space-around; gap: 8px; margin: 8px 0 14px; }
.phud-item { background: var(--panel); border-radius: 12px; padding: 10px 14px; text-align: center; flex: 1; color: var(--muted); font-size: .8rem; }
.phud-item b { display: block; font-size: 1.5rem; color: var(--ink); font-variant-numeric: tabular-nums; }

.mini-road {
  position: relative; height: 52px; border-radius: 12px; overflow: hidden;
  background: repeating-linear-gradient(90deg, transparent 0 38px, rgba(255,255,255,.12) 38px 56px), var(--road);
  margin-bottom: 12px;
}
.mini-fill { position: absolute; left: 0; top: 0; bottom: 0; width: 0; background: linear-gradient(90deg, rgba(77,139,255,.35), rgba(55,214,122,.35)); transition: width .12s linear; will-change: width; }
/* the car rides the right edge of the fill, so only the fill width animates */
.mini-car { position: absolute; right: -8px; top: 50%; transform: translateY(-50%); font-size: 2.2rem; }
.mini-flag { position: absolute; right: 6px; top: 50%; margin-top: -16px; font-size: 1.8rem; }

.gauge-wrap { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; }
.gauge-icon { font-size: 1.4rem; }
.gauge { flex: 1; height: 18px; background: var(--road); border-radius: 999px; overflow: hidden; }
.gauge-fill { height: 100%; width: 0; background: linear-gradient(90deg, #ff8a3d, var(--accent)); transition: width .12s linear; }

/* ====================== Typing card ====================== */
.type-card {
  position: relative; background: var(--panel); border-radius: var(--radius);
  padding: 28px 22px; border: 2px solid #34406f; min-height: 120px;
}
.type-card.bad { animation: shake .18s ease; border-color: var(--bad); }
@keyframes shake { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

.type-line {
  font-size: clamp(1.6rem, 6vw, 2.4rem); font-weight: 700; line-height: 1.4;
  text-align: center; word-break: break-word; font-family: "SF Mono", ui-monospace, Menlo, Consolas, monospace;
  letter-spacing: 1px;
}
.type-line .done { color: var(--good); }
.type-line .wrong { color: var(--bad); background: rgba(255,90,90,.2); border-radius: 4px; }
.type-line .cur {
  color: #1a1400; background: var(--accent); border-radius: 4px;
  padding: 0 2px; animation: blink 1s steps(2) infinite;
}
.type-line .rest { color: var(--muted); }
@keyframes blink { 50% { background: #fff3c4; } }

/* invisible input layered over the card so taps open the keyboard on tablets */
.type-input {
  position: absolute; inset: 0; width: 100%; height: 100%;
  background: transparent; border: 0; outline: 0;
  color: transparent; caret-color: transparent; font-size: 16px; /* 16px avoids iOS zoom */
  cursor: text;
}

/* tap-to-type overlay — the reliable way to open the soft keyboard on iOS,
   shown whenever the typing input is not focused during a race */
.tap-to-type {
  position: absolute; inset: 0; display: none; place-items: center; z-index: 2;
  background: rgba(32,38,74,.88); border-radius: var(--radius);
  color: var(--accent); font-weight: 800; font-size: 1.25rem; cursor: pointer;
  animation: pulse 1.4s ease-in-out infinite;
}
.tap-to-type.show { display: grid; }
@keyframes pulse { 50% { opacity: .65; } }

.pr-place { text-align: center; font-size: clamp(2rem, 9vw, 3.2rem); font-weight: 900; margin: 24px 0 10px; color: var(--accent); }
.pr-place span { display: block; font-size: 1.2rem; color: var(--ink); }

/* ====================== Responsive ====================== */
@media (max-width: 560px) {
  .lb-row { grid-template-columns: 28px 26px 1fr 60px 60px; font-size: .8rem; }
  .lb-fuel, .lb-wpm { display: none; }
  .res-badge { display: none; }
  .res-row { grid-template-columns: 34px 30px 1fr auto auto; }
}

@media (prefers-reduced-motion: reduce) {
  *, .screen { animation: none !important; }
  .lb-bar i, .lb-fuel i, .mini-fill, .mini-car, .gauge-fill { transition: none; }
}
