:root {
  --ink: #0b0820;
  --ink-2: #160f33;
  --panel: rgba(16, 10, 38, 0.78);
  --line: rgba(255, 255, 255, 0.12);
  --text: #f4f1ff;
  --muted: #a69cc9;
  --pink: #ff2d95;
  --cyan: #29e7ff;
  --sun: #ffb347;
  --good: #4dffb0;
  --bad: #ff5468;
  --font-display: 'Chakra Petch', 'Noto Sans TC', system-ui, sans-serif;
  --font-body: 'Noto Sans TC', system-ui, sans-serif;
  --ease: cubic-bezier(0.16, 1, 0.3, 1);
  --gutter: clamp(12px, 2.4vw, 28px);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--ink); color: var(--text); font-family: var(--font-body); }
body { -webkit-tap-highlight-color: transparent; user-select: none; }
#app { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- 啟動 ---------- */
.boot { position: fixed; inset: 0; display: grid; place-items: center; background: var(--ink); font-family: var(--font-display); letter-spacing: 0.3em; color: var(--cyan); z-index: 50; transition: opacity 0.6s var(--ease); }
.boot.is-done { opacity: 0; pointer-events: none; }

/* ---------- 共用面板 ---------- */
.overlay { position: fixed; inset: 0; z-index: 20; overflow-y: auto; padding: var(--gutter); background: linear-gradient(90deg, rgba(11, 8, 32, 0.92) 0%, rgba(11, 8, 32, 0.55) 55%, rgba(11, 8, 32, 0.1) 100%); }
.panel { position: relative; background: var(--panel); border: 1px solid var(--line); border-radius: 4px 18px 4px 18px; padding: clamp(18px, 3vw, 28px); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); box-shadow: 0 30px 80px -30px rgba(255, 45, 149, 0.45), inset 0 1px 0 rgba(255, 255, 255, 0.06); }
.panel::before { content: ''; position: absolute; left: -1px; top: 18px; bottom: 18px; width: 3px; background: linear-gradient(var(--pink), var(--cyan)); border-radius: 3px; }
.panel h2 { margin: 0 0 18px; font-family: var(--font-display); font-weight: 700; font-size: 1.35rem; letter-spacing: 0.04em; }
.kicker { margin: 0 0 6px; font-family: var(--font-display); font-size: 0.75rem; letter-spacing: 0.35em; color: var(--cyan); }
.hint { margin: 14px 0 0; font-size: 0.82rem; color: var(--muted); }
.error { min-height: 1.2em; margin: 10px 0 0; color: var(--bad); font-size: 0.9rem; }

.btn-primary { position: relative; width: 100%; margin-top: 8px; padding: 15px 20px; border: 0; border-radius: 3px 14px 3px 14px; background: linear-gradient(100deg, var(--pink), #ff6a3d 60%, var(--sun)); color: #1a0716; font-family: var(--font-display); font-weight: 700; font-size: 1.05rem; letter-spacing: 0.12em; box-shadow: 0 12px 32px -10px rgba(255, 45, 149, 0.8); transition: transform 0.25s var(--ease), box-shadow 0.25s var(--ease), filter 0.2s; }
.btn-primary:hover { transform: translateY(-2px); box-shadow: 0 18px 40px -12px rgba(255, 45, 149, 0.95); }
.btn-primary:active { transform: translateY(1px) scale(0.99); filter: brightness(0.95); }
.btn-primary:focus-visible, .btn-ghost:focus-visible, input:focus-visible, .swatch:focus-visible { outline: 2px solid var(--cyan); outline-offset: 3px; }
.btn-primary:disabled { filter: grayscale(0.6) brightness(0.7); cursor: wait; transform: none; }
.btn-primary.is-ready { background: linear-gradient(100deg, #17d98b, var(--good)); color: #04210f; box-shadow: 0 12px 32px -10px rgba(77, 255, 176, 0.7); }
.btn-ghost { padding: 8px 14px; border: 1px solid var(--line); border-radius: 999px; background: rgba(255, 255, 255, 0.04); font-size: 0.82rem; transition: background 0.2s, border-color 0.2s; }
.btn-ghost:hover { background: rgba(41, 231, 255, 0.12); border-color: var(--cyan); }

/* ---------- 大廳 ---------- */
.lobby-grid { min-height: 100%; display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(320px, 420px); gap: clamp(20px, 5vw, 72px); align-items: center; max-width: 1180px; }
.brand h1 { margin: 0; font-family: var(--font-display); font-style: italic; font-weight: 700; font-size: clamp(3.6rem, 11vw, 8.5rem); line-height: 0.85; letter-spacing: -0.02em; text-shadow: 0 0 40px rgba(255, 45, 149, 0.55); }
.brand h1 span { background: linear-gradient(180deg, var(--sun) 10%, var(--pink) 90%); -webkit-background-clip: text; background-clip: text; color: transparent; }
.tagline { margin: 18px 0 28px; max-width: 26em; font-size: clamp(1rem, 1.6vw, 1.2rem); color: #d9d1f5; line-height: 1.6; }
.keys { display: flex; flex-wrap: wrap; gap: 8px 18px; margin: 0; padding: 0; list-style: none; font-size: 0.85rem; color: var(--muted); }
kbd { display: inline-block; min-width: 1.9em; margin-right: 3px; padding: 2px 6px; border: 1px solid rgba(255, 255, 255, 0.22); border-bottom-width: 3px; border-radius: 5px; background: rgba(255, 255, 255, 0.05); color: var(--text); font-family: var(--font-display); font-size: 0.78rem; text-align: center; }

.field { display: block; margin: 0 0 16px; padding: 0; border: 0; }
.field > span, .field legend { display: block; margin-bottom: 7px; font-size: 0.82rem; color: var(--muted); padding: 0; }
.field small { color: #7f76a3; }
.field input { width: 100%; padding: 12px 14px; border: 1px solid var(--line); border-radius: 3px 10px 3px 10px; background: rgba(0, 0, 0, 0.35); color: var(--text); font: 600 1rem var(--font-body); transition: border-color 0.2s, box-shadow 0.2s; user-select: text; }
.field input:focus { border-color: var(--cyan); box-shadow: 0 0 0 4px rgba(41, 231, 255, 0.12); outline: none; }
.swatches { display: flex; flex-wrap: wrap; gap: 10px; }
.swatch { width: 36px; height: 36px; border: 2px solid transparent; border-radius: 50%; background: var(--c); box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.5) inset, 0 0 18px -4px var(--c); transition: transform 0.2s var(--ease), border-color 0.2s; }
.swatch:hover { transform: scale(1.12); }
.swatch[aria-checked='true'] { border-color: #fff; transform: scale(1.12); }
.room-list { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.room-list button { padding: 5px 11px; border: 1px solid var(--line); border-radius: 999px; background: transparent; font-size: 0.78rem; color: var(--muted); }
.room-list button:hover { color: var(--text); border-color: var(--pink); }

.room-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
#roomName { color: var(--cyan); font-family: var(--font-display); }
.player-list { margin: 0 0 14px; padding: 0; list-style: none; display: grid; gap: 6px; }
.player-list li { display: grid; grid-template-columns: 14px 1fr auto; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 3px 10px 3px 10px; background: rgba(255, 255, 255, 0.04); border: 1px solid transparent; animation: slide-in 0.4s var(--ease) both; }
.player-list li.is-me { border-color: rgba(41, 231, 255, 0.35); }
.player-list .dot { width: 14px; height: 14px; border-radius: 50%; background: var(--c); box-shadow: 0 0 12px var(--c); }
.player-list .state { font-family: var(--font-display); font-size: 0.75rem; letter-spacing: 0.15em; color: var(--muted); }
.player-list .state.ok { color: var(--good); }
.lobby-status { margin: 0 0 6px; min-height: 1.3em; font-family: var(--font-display); color: var(--sun); }

/* ---------- 成績 ---------- */
.overlay-results { display: grid; place-items: center; background: radial-gradient(ellipse at center, rgba(11, 8, 32, 0.85), rgba(11, 8, 32, 0.55)); }
.results-panel { width: min(520px, 100%); }
.result-list { margin: 0; padding: 0; list-style: none; display: grid; gap: 6px; }
.result-list li { display: grid; grid-template-columns: 44px 14px 1fr auto; gap: 12px; align-items: center; padding: 10px 14px; border-radius: 3px 10px 3px 10px; background: rgba(255, 255, 255, 0.04); animation: slide-in 0.5s var(--ease) both; }
.result-list li:nth-child(1) { background: linear-gradient(90deg, rgba(255, 179, 71, 0.28), transparent); }
.result-list li.is-me { outline: 1px solid var(--cyan); }
.result-list .rank { font-family: var(--font-display); font-style: italic; font-weight: 700; font-size: 1.5rem; color: var(--sun); }
.result-list .dot { width: 14px; height: 14px; border-radius: 50%; background: var(--c); }
.result-list .time { font-family: var(--font-display); font-variant-numeric: tabular-nums; color: #d9d1f5; }
@keyframes slide-in { from { opacity: 0; transform: translateX(-14px); } }

/* ---------- HUD ---------- */
.hud { position: fixed; inset: 0; z-index: 10; pointer-events: none; font-family: var(--font-display); text-shadow: 0 2px 10px rgba(0, 0, 0, 0.6); }
.hud-label { font-size: 0.72rem; letter-spacing: 0.3em; color: var(--cyan); }
.hud-pos { position: absolute; left: var(--gutter); top: var(--gutter); display: flex; align-items: baseline; gap: 4px; }
.hud-pos .hud-label { writing-mode: vertical-rl; transform: rotate(180deg); margin-right: 6px; }
.hud-pos strong { font-style: italic; font-size: clamp(3.4rem, 9vw, 5.6rem); line-height: 0.8; background: linear-gradient(180deg, #fff 30%, var(--sun)); -webkit-background-clip: text; background-clip: text; color: transparent; }
#hudPosTotal { font-size: 1.4rem; color: var(--muted); }
.hud-lap { position: absolute; left: 50%; top: var(--gutter); transform: translateX(-50%); text-align: center; padding: 8px 22px; background: linear-gradient(180deg, rgba(11, 8, 32, 0.6), transparent); }
.hud-lap strong { font-size: 1.6rem; }
.hud-time { font-size: 1.15rem; font-variant-numeric: tabular-nums; letter-spacing: 0.05em; }
.hud-best { font-family: var(--font-body); font-size: 0.75rem; color: var(--muted); }
#minimap { position: absolute; right: var(--gutter); top: var(--gutter); width: clamp(120px, 16vw, 190px); height: auto; aspect-ratio: 1; border-radius: 50%; background: radial-gradient(circle, rgba(22, 15, 51, 0.75), rgba(11, 8, 32, 0.4)); border: 1px solid var(--line); }
.standings { position: absolute; left: var(--gutter); top: calc(var(--gutter) + clamp(70px, 12vw, 110px)); margin: 0; padding: 0; list-style: none; display: grid; gap: 3px; font-size: 0.85rem; min-width: 150px; }
.standings li { display: grid; grid-template-columns: 22px 10px 1fr; gap: 8px; align-items: center; padding: 3px 10px 3px 6px; background: linear-gradient(90deg, rgba(11, 8, 32, 0.65), transparent); font-family: var(--font-body); transition: transform 0.4s var(--ease); }
.standings li.is-me { background: linear-gradient(90deg, rgba(41, 231, 255, 0.35), transparent); }
.standings b { font-family: var(--font-display); font-style: italic; color: var(--sun); text-align: right; }
.standings i { width: 10px; height: 10px; border-radius: 50%; background: var(--c); }
.standings .fin::after { content: ' 🏁'; }

.speedo { position: absolute; right: var(--gutter); bottom: calc(var(--gutter) + env(safe-area-inset-bottom)); width: clamp(170px, 20vw, 230px); text-align: center; }
.speedo-arc { width: 100%; display: block; }
.arc-bg, .arc-fg { fill: none; stroke-width: 12; stroke-linecap: round; }
.arc-bg { stroke: rgba(255, 255, 255, 0.1); }
.arc-fg { stroke: var(--cyan); stroke-dasharray: 0 100; filter: drop-shadow(0 0 6px var(--cyan)); transition: stroke 0.2s; }
.speedo.is-boost .arc-fg { stroke: var(--pink); filter: drop-shadow(0 0 10px var(--pink)); }
.speedo strong { display: block; margin-top: -58px; font-style: italic; font-size: clamp(2.6rem, 5vw, 3.6rem); line-height: 1; font-variant-numeric: tabular-nums; }
.nitro { position: relative; height: 10px; margin-top: 10px; border-radius: 10px; background: rgba(255, 255, 255, 0.08); overflow: hidden; }
.nitro i { position: absolute; inset: 0; transform-origin: left; background: linear-gradient(90deg, var(--cyan), var(--pink)); box-shadow: 0 0 12px var(--pink); }
.nitro span { position: absolute; right: 6px; top: -1px; font-size: 0.6rem; letter-spacing: 0.1em; color: #fff; }

.center-msg { position: absolute; left: 0; right: 0; top: 34%; text-align: center; font-style: italic; font-weight: 700; font-size: clamp(3rem, 14vw, 9rem); letter-spacing: 0.02em; color: #fff; text-shadow: 0 0 40px var(--pink), 0 4px 0 rgba(0, 0, 0, 0.4); }
.center-msg.pop { animation: pop 0.9s var(--ease) both; }
.center-msg.warn { font-size: clamp(1.6rem, 5vw, 3rem); color: var(--bad); text-shadow: 0 0 30px var(--bad); animation: blink 0.6s steps(2) infinite; }
@keyframes pop { 0% { transform: scale(1.8); opacity: 0; } 25% { transform: scale(1); opacity: 1; } 80% { opacity: 1; } 100% { opacity: 0; transform: scale(0.9); } }
@keyframes blink { 50% { opacity: 0.35; } }
.toast { position: absolute; left: 50%; bottom: 24%; transform: translateX(-50%); padding: 8px 18px; border-radius: 999px; background: rgba(11, 8, 32, 0.75); font-family: var(--font-body); font-size: 0.95rem; opacity: 0; transition: opacity 0.3s; white-space: nowrap; }
.toast.show { opacity: 1; }
.hud-actions { position: absolute; left: var(--gutter); bottom: calc(var(--gutter) + env(safe-area-inset-bottom)); display: flex; gap: 6px; pointer-events: auto; }
.hud-actions button { padding: 6px 12px; border: 1px solid var(--line); border-radius: 999px; background: rgba(11, 8, 32, 0.6); font-family: var(--font-body); font-size: 0.78rem; color: var(--muted); }
.hud-actions button:hover { color: var(--text); border-color: var(--cyan); }

/* 名牌 */
#labels { position: fixed; inset: 0; pointer-events: none; z-index: 5; }
.tag { position: absolute; left: 0; top: 0; padding: 2px 8px; border-radius: 4px; background: rgba(11, 8, 32, 0.65); border-bottom: 2px solid var(--c); font-size: 0.75rem; font-weight: 700; white-space: nowrap; will-change: transform; }

/* ---------- 觸控 ---------- */
.touch { position: fixed; inset: auto 0 0 0; z-index: 12; display: flex; justify-content: space-between; align-items: flex-end; padding: 0 var(--gutter) calc(var(--gutter) + env(safe-area-inset-bottom)); pointer-events: none; }
.touch button { pointer-events: auto; width: 72px; height: 72px; border: 1px solid rgba(255, 255, 255, 0.25); border-radius: 50%; background: rgba(11, 8, 32, 0.45); font-size: 1.3rem; font-weight: 700; touch-action: none; transition: transform 0.08s, background 0.08s; }
.touch button.is-down { transform: scale(0.92); background: rgba(41, 231, 255, 0.35); }
.touch-left, .touch-right { display: flex; gap: 12px; align-items: flex-end; }
.touch .t-gas { width: 88px; height: 88px; background: rgba(255, 45, 149, 0.35); }
.touch .t-small { width: 56px; height: 56px; font-size: 0.8rem; }
body.is-touch .speedo { bottom: 120px; }
body.is-touch .hud-actions { bottom: 120px; }

/* ---------- 響應式 ---------- */
@media (max-width: 820px) {
  .overlay { background: linear-gradient(180deg, rgba(11, 8, 32, 0.6), rgba(11, 8, 32, 0.92) 45%); }
  .lobby-grid { grid-template-columns: 1fr; align-content: start; gap: 18px; }
  .keys { display: none; }
  .tagline { margin: 10px 0 0; }
  .standings { display: none; }
  .hud-lap { top: calc(var(--gutter) + 4px); padding: 4px 10px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
