:root {
  --bg: #0a0a0a;
  --fg: #ededed;
  --muted: #a1a1a1;
  --subtle: #6b6b6b;
  --border: rgba(255, 255, 255, 0.12);
  --glass: rgba(10, 10, 10, 0.72);
  --accent: #f5b301;
  --err: #ff4d4d;
  --ok: #3ddc84;
  --sans: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; background: var(--bg); color: var(--fg); font-family: var(--sans); -webkit-font-smoothing: antialiased; }
body { overflow: hidden; }
button, input { font: inherit; color: inherit; }
a { color: inherit; }
[hidden] { display: none !important; }

#app { position: fixed; inset: 0; overflow: hidden; }
#scene { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.vignette { position: absolute; inset: 0; pointer-events: none; background: radial-gradient(ellipse at 50% 40%, transparent 55%, rgba(0, 0, 0, 0.55)); }

/* Top bar */
.topbar { position: absolute; top: 0; left: 0; right: 0; z-index: 5; display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; pointer-events: none; }
.topbar > * { pointer-events: auto; }
.brand { font-size: 14px; color: var(--fg); text-decoration: none; padding: 6px 12px; border-radius: 999px; background: var(--glass); border: 1px solid var(--border); backdrop-filter: blur(12px); }
.title { font-family: var(--mono); font-size: 13px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
.icon-btn { width: 36px; height: 36px; display: grid; place-items: center; border-radius: 999px; background: var(--glass); border: 1px solid var(--border); cursor: pointer; backdrop-filter: blur(12px); }
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn .x { display: none; }
.icon-btn.muted .wave { display: none; }
.icon-btn.muted .x { display: inline; }

/* Panels */
.panel { position: absolute; z-index: 4; left: 50%; top: 50%; transform: translate(-50%, -50%); width: min(380px, calc(100% - 28px)); max-height: calc(100% - 84px); overflow: auto; padding: 20px 22px; border-radius: 16px; background: var(--glass); border: 1px solid var(--border); backdrop-filter: blur(18px); box-shadow: 0 30px 80px rgba(0, 0, 0, 0.5); animation: pop 0.35s cubic-bezier(0.2, 0.9, 0.3, 1.2) both; }
.panel h1 { font-size: 24px; letter-spacing: -0.03em; font-weight: 700; }
.panel h2 { font-size: 20px; letter-spacing: -0.02em; margin: 2px 0 12px; }
.sub { color: var(--muted); font-size: 13px; line-height: 1.45; margin-top: 4px; }
.note { color: var(--subtle); font-size: 11.5px; line-height: 1.4; margin-top: 6px; }
.kicker { font-family: var(--mono); font-size: 12px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--subtle); }
.field { display: grid; gap: 6px; margin-top: 12px; }
.field > span { font-size: 12px; color: var(--subtle); }
.field input, .field select, .join input { height: 36px; padding: 0 12px; border-radius: 9px; font-size: 14px; border: 1px solid var(--border); background: rgba(255, 255, 255, 0.04); outline: none; }
.field input:focus, .join input:focus { border-color: rgba(255, 255, 255, 0.35); }

.cars { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.car-opt { position: relative; aspect-ratio: 1.15; border-radius: 10px; border: 1px solid var(--border); background: rgba(255, 255, 255, 0.04); cursor: pointer; display: grid; place-items: center; overflow: hidden; transition: border-color 0.15s, transform 0.15s; }
.car-opt:hover { transform: translateY(-2px); }
.car-opt[aria-checked="true"] { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
.car-opt img { width: 100%; height: 100%; object-fit: contain; }
.car-opt span { position: absolute; bottom: 4px; left: 0; right: 0; text-align: center; font-size: 10px; color: var(--muted); }

.modes { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 14px; }
.settings { display: grid; grid-template-columns: 92px 1fr; gap: 10px; }
.field select { appearance: none; border: 1px solid var(--border); background: rgba(255, 255, 255, 0.04) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%23a1a1a1' fill='none' stroke-width='1.5'/%3E%3C/svg%3E") no-repeat right 10px center; outline: none; cursor: pointer; }
.field select option { background: #161616; }
.field select:disabled, .seg button:disabled { opacity: 0.6; cursor: default; }
.seg { display: grid; grid-template-columns: repeat(5, 1fr); height: 36px; border: 1px solid var(--border); border-radius: 9px; overflow: hidden; }
.seg button { border: 0; background: rgba(255, 255, 255, 0.03); font-family: var(--mono); font-size: 13px; color: var(--muted); cursor: pointer; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button[aria-checked="true"] { background: var(--accent); color: #111; font-weight: 600; }
.btn { height: 38px; font-size: 14px; border-radius: 9px; border: 1px solid var(--border); background: rgba(255, 255, 255, 0.06); font-weight: 600; cursor: pointer; transition: background 0.15s, transform 0.1s, opacity 0.15s; }
.btn:hover { background: rgba(255, 255, 255, 0.12); }
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: #111; border-color: transparent; }
.btn.primary:hover { background: #ffc629; }
.join { display: grid; grid-template-columns: 1fr auto; gap: 8px; margin-top: 8px; }
.join input { font-family: var(--mono); letter-spacing: 0.25em; text-transform: uppercase; }
.join .btn { padding: 0 18px; }
.error { color: var(--err); font-size: 12.5px; min-height: 16px; margin-top: 8px; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-top: 6px; }
.stats div { padding: 7px 9px; border-radius: 8px; background: rgba(255, 255, 255, 0.04); }
.stats dt { font-size: 11px; color: var(--subtle); }
.stats dd { font-family: var(--mono); font-size: 15px; margin-top: 2px; }

/* Waiting room */
.code { display: block; margin: 4px 0 2px; font-family: var(--mono); font-size: 34px; font-weight: 600; letter-spacing: 0.2em; background: none; border: 0; cursor: pointer; color: var(--accent); }
.roster { list-style: none; display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 10px; }
.roster li { display: flex; align-items: center; gap: 7px; min-width: 0; padding: 7px 9px; border-radius: 8px; background: rgba(255, 255, 255, 0.04); font-size: 12.5px; white-space: nowrap; overflow: hidden; }
.roster .dot { flex-shrink: 0; width: 8px; height: 8px; border-radius: 50%; }
.roster .tag { margin-left: auto; font-size: 10px; color: var(--subtle); font-family: var(--mono); }

/* Countdown */
.countdown { position: absolute; z-index: 6; left: 50%; top: 34%; transform: translate(-50%, -50%); font-family: var(--mono); font-weight: 600; font-size: clamp(64px, 13vw, 140px); color: #fff; text-shadow: 0 10px 40px rgba(0, 0, 0, 0.6); pointer-events: none; }
.countdown.tick { animation: tick 0.9s ease-out both; }
.countdown.go { color: var(--ok); }

/* Standings */
.standings { position: absolute; z-index: 4; top: 60px; left: 16px; display: grid; gap: 4px; width: 180px; }
.standings:empty { display: none; }
.st { display: grid; grid-template-columns: 16px 1fr 34px; align-items: center; gap: 3px 6px; padding: 5px 9px; border-radius: 8px; background: var(--glass); border: 1px solid var(--border); backdrop-filter: blur(10px); font-size: 12px; transition: transform 0.3s; }
.st.you { border-color: var(--accent); }
.st b { font-family: var(--mono); color: var(--subtle); font-weight: 500; }
.st .name { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.st .bar { grid-column: 2 / 4; height: 3px; border-radius: 3px; background: rgba(255, 255, 255, 0.1); overflow: hidden; }
.st .bar i { display: block; height: 100%; border-radius: 3px; }
.st .w { font-family: var(--mono); font-size: 12px; color: var(--muted); text-align: right; }

/* Typer */
.typer { position: absolute; z-index: 4; left: 50%; bottom: 36px; transform: translateX(-50%); width: min(720px, calc(100% - 24px)); padding: 12px 18px 10px; border-radius: 14px; background: var(--glass); border: 1px solid var(--border); backdrop-filter: blur(18px); cursor: text; }
.hud { display: flex; gap: 16px; font-size: 12px; color: var(--subtle); margin-bottom: 6px; }
.hud b { font-family: var(--mono); color: var(--accent); font-size: 15px; font-weight: 600; }
.hud .timer { margin-left: auto; font-family: var(--mono); }
.words { position: relative; font-family: var(--mono); font-size: clamp(16px, 1.8vw, 20px); line-height: 1.6; height: calc(1.6em * 3); overflow: hidden; color: var(--subtle); user-select: none; }
.words .inner { position: relative; transition: transform 0.2s ease; }
.word { display: inline-block; white-space: pre; }
.ch { position: relative; }
.ch.ok { color: var(--fg); }
.ch.bad { color: var(--err); }
.ch.cur::before { content: ""; position: absolute; left: -1px; top: 0.18em; bottom: 0.18em; width: 2px; border-radius: 2px; background: var(--accent); animation: blink 1s steps(1) infinite; }
.typer.typing .ch.cur::before { animation: none; }
.ch.cur.bad { background: rgba(255, 77, 77, 0.18); border-radius: 3px; }
.typer.stalled { animation: shake 0.35s; }
.typer.blocked { border-color: rgba(255, 77, 77, 0.6); }
.hint { margin-top: 4px; font-size: 11px; color: var(--subtle); }
.type-input { position: absolute; opacity: 0; width: 1px; height: 1px; left: 0; bottom: 0; pointer-events: none; }

/* Results */
.results { width: min(520px, calc(100% - 28px)); }
.board-wrap { max-height: 46vh; overflow: auto; }
.board { width: 100%; border-collapse: collapse; font-size: 13px; }
.board th { text-align: left; font-size: 11px; font-weight: 500; color: var(--subtle); text-transform: uppercase; letter-spacing: 0.06em; padding: 6px 8px; }
.board td { padding: 7px 8px; border-top: 1px solid var(--border); font-family: var(--mono); }
.board td:nth-child(2) { font-family: var(--sans); }
.board tr.you td { color: var(--accent); }
.board .medal { font-weight: 700; }

.loading { position: absolute; inset: 0; z-index: 10; display: grid; place-items: center; background: var(--bg); color: var(--muted); font-family: var(--mono); font-size: 14px; transition: opacity 0.4s; }
.loading.done { opacity: 0; pointer-events: none; }

.credit { position: fixed; z-index: 3; bottom: 10px; left: 0; right: 0; text-align: center; font-size: 11px; color: rgba(255, 255, 255, 0.45); pointer-events: none; }
.credit a { pointer-events: auto; }

#app[data-screen="race"] ~ .credit { opacity: 0; }

@keyframes pop { from { opacity: 0; transform: translate(-50%, -46%) scale(0.97); } to { opacity: 1; transform: translate(-50%, -50%) scale(1); } }
@keyframes tick { 0% { opacity: 0; transform: translate(-50%, -50%) scale(1.6); } 25% { opacity: 1; transform: translate(-50%, -50%) scale(1); } 100% { opacity: 0; transform: translate(-50%, -50%) scale(0.9); } }
@keyframes blink { 50% { opacity: 0; } }
@keyframes shake { 0%, 100% { translate: 0; } 20% { translate: -8px; } 40% { translate: 7px; } 60% { translate: -5px; } 80% { translate: 3px; } }

@media (max-width: 640px) {
  .panel { padding: 22px; }
  .panel h1 { font-size: 28px; }
  .standings { top: 58px; left: 10px; min-width: 0; width: 132px; gap: 4px; }
  .st { grid-template-columns: 12px 1fr 26px; gap: 4px 6px; font-size: 11px; padding: 4px 7px; border-radius: 8px; }
  .typer { bottom: 10px; padding: 14px; }
  .hud { gap: 12px; font-size: 12px; }
  .credit { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
