:root {
  --bg: #0c0d10;
  --panel: rgba(18, 20, 24, 0.82);
  --panel-solid: #16181d;
  --line: rgba(255, 255, 255, 0.12);
  --accent: #e3262f;
  --accent2: #f2b134;
  --ok: #3ccf7a;
  --text: #f2f3f5;
  --muted: #9aa1ab;
  --radius: 6px;
  font-family: "Noto Sans TC", "PingFang HK", "Microsoft JhengHei", system-ui, sans-serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden;
  overscroll-behavior: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
#app { position: fixed; inset: 0; }
#race-canvas, .overlay-canvas {
  position: absolute; inset: 0; width: 100%; height: 100%; display: block; z-index: 0; touch-action: none;
}
.overlay-canvas { pointer-events: none; z-index: 5; }
.screen {
  position: absolute; inset: 0; z-index: 2;
  display: none; flex-direction: column; align-items: center; justify-content: center;
  padding: max(20px, env(safe-area-inset-top)) 20px 20px; gap: 14px; overflow: auto;
  background:
    linear-gradient(180deg, rgba(10,11,14,.55), rgba(10,11,14,.92)),
    repeating-linear-gradient(115deg, #121419 0 22px, #0f1115 22px 44px);
}
.screen.active { display: flex; }
.screen.race { background: transparent; justify-content: flex-start; pointer-events: none; padding: 0; touch-action: none; }

h1, h2, h3 { font-style: italic; letter-spacing: .04em; }
h2 { margin: 0; font-size: 1.6rem; }
h3 { margin: 6px 0 0; color: var(--muted); font-size: .95rem; text-transform: uppercase; }

.logo { text-align: center; }
.logo h1 {
  margin: 0; font-size: clamp(2.6rem, 9vw, 4.4rem); font-weight: 900; letter-spacing: .1em;
  background: linear-gradient(180deg, #ffffff 0%, #c9ccd1 55%, #8d939b 100%);
  -webkit-background-clip: text; background-clip: text; color: transparent;
  filter: drop-shadow(0 4px 0 rgba(0,0,0,.6));
}
.logo .sub {
  margin: 4px 0 0; color: var(--accent); font-weight: 800; font-style: italic; letter-spacing: .5em;
  border-top: 2px solid var(--accent); padding-top: 6px;
}
.story { max-width: 34rem; text-align: center; color: #d5d8dd; line-height: 1.7; }
.tip { color: var(--accent2); font-size: .85rem; }
.osm { font-size: .78rem; color: var(--muted); }
.osm a { color: #cfd3d8; }
.hint { color: var(--muted); font-size: .9rem; text-align: center; }

.btn {
  appearance: none; border: 1px solid var(--line); cursor: pointer;
  background: var(--panel-solid); color: var(--text);
  padding: 12px 22px; border-radius: var(--radius); font-size: 1rem; font-weight: 800;
  font-style: italic; letter-spacing: .05em; text-decoration: none;
  transition: transform .08s, background .15s;
}
.btn:hover { background: #20232a; }
.btn:active { transform: translateY(1px) scale(.99); }
.btn.primary { background: var(--accent); border-color: #ff5059; color: #fff; }
.btn.primary:hover { background: #f0323b; }
.btn.big { font-size: 1.2rem; padding: 16px 48px; clip-path: polygon(6% 0, 100% 0, 94% 100%, 0 100%); }
.btn.small { padding: 8px 14px; font-size: .9rem; }
.btn.ghost { background: transparent; }
.btn.danger { background: #5d1418; }
.boot-links { display: flex; gap: 10px; }

.driver-grid { display: grid; grid-template-columns: repeat(2, minmax(150px, 1fr)); gap: 12px; width: min(560px, 100%); }
.driver-card {
  background: var(--panel); border: 1px solid var(--line); border-left: 4px solid var(--c, #666);
  border-radius: var(--radius); padding: 14px; color: var(--text); cursor: pointer; text-align: left;
  display: grid; grid-template-columns: 48px 1fr; column-gap: 12px; align-items: center;
}
.driver-card .mono {
  grid-row: span 2; width: 48px; height: 48px; border-radius: 50%; display: grid; place-items: center;
  background: #2a2d33; border: 2px solid var(--c, #666); font-weight: 900; font-size: 1.3rem;
}
.driver-card b { font-size: 1.05rem; }
.driver-card.selected { background: #22252c; outline: 2px solid var(--c, #fff); }
.driver-card p { margin: 2px 0 0; color: var(--muted); font-size: .82rem; }

.topbar { width: min(960px, 100%); display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.topbar h2 { flex: 1; }
.stat { background: var(--panel); border: 1px solid var(--line); padding: 6px 12px; border-radius: var(--radius); }
.coin-ic {
  display: inline-block; width: 14px; height: 14px; border-radius: 50%; vertical-align: -2px;
  background: radial-gradient(circle at 35% 35%, #ffe9a6, #d8a93a 55%, #8a6414);
}

.hk-map {
  position: relative; width: min(920px, 100%); height: min(58vh, 480px);
  border-radius: var(--radius); overflow: hidden; border: 1px solid var(--line); background: #0d1720;
}
.map-bg {
  position: absolute; inset: 0;
  background:
    radial-gradient(ellipse 30% 22% at 30% 62%, #26342a 0 60%, transparent 62%),
    radial-gradient(ellipse 18% 14% at 52% 30%, #2a332c 0 60%, transparent 62%),
    radial-gradient(ellipse 22% 18% at 10% 55%, #24301f 0 60%, transparent 62%),
    linear-gradient(0deg, rgba(255,255,255,.03) 1px, transparent 1px) 0 0/40px 40px,
    linear-gradient(90deg, rgba(255,255,255,.03) 1px, transparent 1px) 0 0/40px 40px,
    linear-gradient(180deg, #0f1d28, #0b141c);
}
.map-node {
  position: absolute; transform: translate(-50%, -50%);
  width: 118px; padding: 7px 8px; border-radius: var(--radius); text-align: left;
  background: rgba(16,18,22,.92); border: 1px solid var(--line); color: var(--text);
  font-size: .72rem; cursor: pointer; line-height: 1.25;
}
.map-node .num { float: right; font-weight: 900; font-style: italic; color: var(--accent); font-size: 1rem; }
.map-node .name { display: block; font-weight: 800; }
.map-node .meta { display: block; color: var(--muted); font-size: .64rem; }
.map-node .stars { color: var(--accent2); letter-spacing: .1em; }
.map-node.unlocked { border-color: rgba(227,38,47,.7); }
.map-node.unlocked:hover { background: #22252c; }
.map-node.locked { opacity: .4; cursor: not-allowed; }

.garage-row { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; width: min(760px, 100%); }
.garage-item {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px; color: var(--text); min-width: 130px; cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 6px;
}
.garage-item .car-ic { width: 84px; height: 32px; }
.garage-item i { color: var(--muted); font-size: .75rem; }
.garage-item.selected { border-color: var(--accent); box-shadow: inset 0 -3px 0 var(--accent); }
.garage-item.locked { opacity: .4; }
.garage-item .swatch { display: inline-block; width: 34px; height: 34px; border-radius: 50%; border: 1px solid var(--line); }

/* ---------------- race HUD ---------------- */
.hud-top {
  position: absolute; top: 0; left: 0; right: 0; display: flex; align-items: flex-start; gap: 10px;
  padding: max(10px, env(safe-area-inset-top)) 12px 0; pointer-events: none;
}
.hud-top > * { pointer-events: auto; }
.hud-btn {
  width: 42px; height: 42px; border-radius: var(--radius); border: 1px solid var(--line);
  background: var(--panel); color: var(--text); font-size: 1.1rem; cursor: pointer; touch-action: none;
}
.hud-block { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 6px 12px; backdrop-filter: blur(4px); }
.hud-title { min-width: 0; }
.race-title { font-weight: 900; font-style: italic; font-size: .95rem; }
.street-sign {
  display: inline-block; margin-top: 4px; background: #0f5b34; border: 2px solid #fff; border-radius: 3px;
  padding: 1px 8px; font-size: .78rem; font-weight: 700; color: #fff; white-space: nowrap; max-width: 46vw; overflow: hidden; text-overflow: ellipsis;
}
.hud-stats { margin-left: auto; display: flex; gap: 14px; pointer-events: none; }
.hud-stats label { display: block; font-size: .62rem; color: var(--muted); letter-spacing: .08em; }
.hud-stats b { font-size: 1.05rem; font-variant-numeric: tabular-nums; font-style: italic; }
.hud-stats small { color: var(--muted); font-weight: 600; }
.progress-bar {
  position: absolute; top: calc(max(10px, env(safe-area-inset-top)) + 58px); left: 12px; right: 12px;
  height: 4px; background: rgba(255,255,255,.15); border-radius: 2px; overflow: hidden; pointer-events: none;
}
.progress-bar i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--accent2), var(--accent)); }

.speedo {
  position: absolute; left: 50%; bottom: max(14px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  width: 220px; height: 132px; pointer-events: none; text-align: center;
}
.speedo svg { position: absolute; inset: 0; width: 100%; height: 100%; filter: drop-shadow(0 2px 4px rgba(0,0,0,.6)); }
.speedo .arc-bg { fill: none; stroke: rgba(0,0,0,.45); stroke-width: 14; }
.speedo .arc-fg { fill: none; stroke: url(#none); stroke: var(--accent); stroke-width: 10; stroke-dasharray: 100; stroke-dashoffset: 100; transition: stroke-dashoffset .08s linear; }
.speed { position: absolute; left: 0; right: 0; top: 50px; font-size: 2.5rem; font-weight: 900; font-style: italic; line-height: 1; text-shadow: 0 2px 6px #000; font-variant-numeric: tabular-nums; }
.speed small { display: block; font-size: .7rem; color: #d6d9de; font-weight: 700; letter-spacing: .1em; }
.gear { position: absolute; right: 18px; top: 86px; font-weight: 900; font-style: italic; color: var(--accent2); text-shadow: 0 1px 3px #000; }
.speedo .nitro { position: absolute; left: 0; right: 0; bottom: 0; display: flex; gap: 4px; justify-content: center; }
.nitro-pip { width: 22px; height: 6px; border-radius: 1px; background: rgba(255,255,255,.2); }
.nitro-pip.on { background: #3b8cff; box-shadow: 0 0 6px #3b8cff; }
.speedo .nitro.active .nitro-pip.on { animation: blink .2s infinite alternate; }
@keyframes blink { to { opacity: .4; } }

.countdown {
  position: absolute; inset: 0; display: none; align-items: center; justify-content: center;
  font-size: 7rem; font-weight: 900; font-style: italic; color: #fff; text-shadow: 0 6px 20px rgba(0,0,0,.8); pointer-events: none;
}
.countdown.show { display: flex; }
.hud-warn {
  position: absolute; left: 50%; top: 30%; transform: translateX(-50%); display: none;
  background: rgba(160,20,26,.88); border: 1px solid #ff5a62; padding: 8px 16px; border-radius: var(--radius);
  font-weight: 800; pointer-events: none; text-align: center;
}
.hud-warn.show { display: block; }
.street-toast {
  position: absolute; left: 50%; top: 18%; transform: translateX(-50%) translateY(-8px);
  background: #0f5b34; border: 2px solid #fff; padding: 8px 18px; border-radius: 4px; font-weight: 800;
  opacity: 0; transition: .25s; pointer-events: none; white-space: nowrap;
}
.street-toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.kbd-hint {
  position: absolute; left: 12px; bottom: 12px; font-size: .72rem; color: #c8ccd2;
  background: var(--panel); border: 1px solid var(--line); padding: 6px 10px; border-radius: var(--radius); pointer-events: none;
}
.respawn { position: absolute; right: 12px; top: calc(max(10px, env(safe-area-inset-top)) + 72px); pointer-events: auto; }

/* touch controls (shown on touch devices) */
.touch-ui { display: none; position: absolute; inset: 0; pointer-events: none; touch-action: none; }
.screen.race.touch .touch-ui { display: block; }
.screen.race.touch .kbd-hint { display: none; }
.steer-zone {
  position: absolute; left: 0; top: 120px; bottom: 0; width: 52%; pointer-events: auto; touch-action: none;
}
.zone-label {
  position: absolute; left: 16px; bottom: max(16px, env(safe-area-inset-bottom)); font-size: .72rem; color: rgba(255,255,255,.7);
  background: rgba(0,0,0,.35); padding: 4px 8px; border-radius: 4px;
}
.steer-wheel {
  position: absolute; width: 120px; height: 120px; margin: -60px 0 0 -60px; border-radius: 50%;
  border: 8px solid rgba(255,255,255,.55); display: none; transform: rotate(var(--rot, 0deg));
  box-shadow: 0 0 0 2px rgba(0,0,0,.4), inset 0 0 0 2px rgba(0,0,0,.4);
}
.steer-wheel::before { content: ''; position: absolute; left: 50%; top: -8px; width: 6px; height: 22px; margin-left: -3px; background: var(--accent); border-radius: 2px; }
.steer-wheel::after { content: ''; position: absolute; left: 6px; right: 6px; top: 50%; height: 8px; margin-top: -4px; background: rgba(255,255,255,.45); }
.steer-wheel.active { display: block; }
.pedals {
  position: absolute; right: max(12px, env(safe-area-inset-right)); bottom: max(14px, env(safe-area-inset-bottom));
  display: grid; grid-template-columns: auto auto; grid-template-rows: auto auto; gap: 12px; pointer-events: auto; align-items: end;
}
.pedal {
  touch-action: none; border: 1px solid rgba(255,255,255,.25); border-radius: 10px; color: #fff; font-weight: 900; font-style: italic;
  font-size: 1.05rem; background: rgba(20,22,26,.72); -webkit-user-select: none; user-select: none;
}
.pedal small { display: block; font-size: .6rem; font-weight: 600; opacity: .8; }
.pedal.gas { grid-column: 2; grid-row: 1 / span 2; width: 96px; height: 150px; background: rgba(40,140,80,.7); }
.pedal.brake { grid-column: 1; grid-row: 2; width: 96px; height: 96px; background: rgba(170,30,36,.72); }
.pedal.nitro { grid-column: 1; grid-row: 1; width: 96px; height: 52px; background: rgba(40,100,220,.72); }
.pedal.nitro.disabled { opacity: .4; }
.pedal.nitro.hidden { visibility: hidden; }
.pedal.down { filter: brightness(1.5); transform: scale(.97); }
.screen.race.touch .speedo { bottom: auto; top: 76px; transform: translateX(-50%) scale(.72); transform-origin: top center; }
.screen.race.touch .respawn { top: auto; bottom: 250px; }

.result-stars { font-size: 3rem; color: var(--accent2); letter-spacing: .15em; }
.row { display: flex; gap: 12px; }
.opt {
  width: min(460px, 100%); background: var(--panel); border: 1px solid var(--line); padding: 14px 16px; border-radius: var(--radius);
  display: flex; align-items: center; gap: 10px;
}
.opt select { margin-left: auto; background: #22252b; color: var(--text); border: 1px solid var(--line); padding: 6px; border-radius: 4px; }

@media (max-width: 700px) {
  .map-node { width: 96px; font-size: .62rem; }
  .map-node .meta { display: none; }
  .hud-stats { gap: 8px; }
  .hud-stats b { font-size: .9rem; }
  .race-title { font-size: .8rem; }
  .kbd-hint { display: none; }
}
@media (max-height: 500px) {
  .steer-zone { top: 80px; }
  .pedal.gas { height: 120px; }
  .pedal.brake { height: 76px; }
  .screen.race.touch .respawn { bottom: auto; top: 80px; }
}
