:root {
  --bg: #0d0e14;
  --panel: rgba(16, 17, 26, 0.78);
  --panel-border: rgba(255, 179, 71, 0.22);
  --amber: #ffb347;
  --orange: #ff6a3d;
  --teal: #37e0c8;
  --magenta: #e84a7f;
  --text: #f4eee2;
  --text-dim: #9a937f;
  --danger: #ff4a5a;
  --font-display: 'Chakra Petch', 'Segoe UI', sans-serif;
  --font-num: 'Rajdhani', 'Chakra Petch', sans-serif;
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-display);
  -webkit-user-select: none; user-select: none;
  touch-action: none;
}

#scene { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }

.hidden { display: none !important; }

/* ---------- shared panel ---------- */
.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  clip-path: polygon(12px 0, 100% 0, 100% calc(100% - 12px), calc(100% - 12px) 100%, 0 100%, 0 12px);
}

.stat-label {
  font-size: 10px; letter-spacing: 2.5px; color: var(--text-dim); font-weight: 600;
}

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 10; }

.hud-top {
  position: absolute; top: 14px; left: 14px; right: 14px;
  display: flex; align-items: flex-start; gap: 12px;
}
.stat-panel { display: flex; align-items: center; gap: 14px; padding: 10px 18px; }
.stat { display: flex; flex-direction: column; align-items: center; gap: 1px; }
.position-stat { flex-direction: row; align-items: baseline; gap: 2px; }
#pos-num { font-family: var(--font-num); font-size: 44px; font-weight: 700; line-height: 1; color: var(--amber); }
.pos-suffix { font-family: var(--font-num); font-size: 18px; font-weight: 700; color: var(--amber); }
.position-stat .stat-label { margin-left: 5px; font-size: 14px; }
.stat-divider { width: 1px; height: 36px; background: var(--panel-border); }
.lap-num { font-family: var(--font-num); font-size: 30px; font-weight: 700; line-height: 1; }
.lap-total { font-size: 16px; color: var(--text-dim); }

.time-panel { padding: 8px 20px; text-align: center; margin-left: auto; margin-right: auto; }
.race-time { font-family: var(--font-num); font-size: 30px; font-weight: 700; line-height: 1.05; letter-spacing: 1px; }
.best-lap { font-size: 10px; letter-spacing: 2px; color: var(--text-dim); }
.best-lap span { color: var(--teal); font-family: var(--font-num); font-size: 13px; font-weight: 600; }

.hud-top-right { margin-left: auto; }
.minimap-panel { padding: 6px; line-height: 0; }
#minimap { width: 132px; height: 132px; }

.hud-bottom {
  position: absolute; left: 14px; right: 14px; bottom: 14px;
  display: flex; align-items: flex-end; justify-content: space-between;
}
.cp-panel { padding: 10px 16px; display: flex; flex-direction: column; gap: 7px; }
.cp-dots { display: flex; gap: 6px; }
.cp-dot {
  width: 12px; height: 12px; transform: rotate(45deg);
  border: 1.5px solid rgba(55, 224, 200, 0.45);
  transition: background 0.25s, box-shadow 0.25s;
}
.cp-dot.done { background: var(--teal); box-shadow: 0 0 8px rgba(55, 224, 200, 0.7); }
.cp-dot.next { border-color: var(--amber); animation: cpPulse 1s ease-in-out infinite; }
@keyframes cpPulse { 50% { background: rgba(255, 179, 71, 0.55); } }

.speed-panel { padding: 10px 20px 12px; min-width: 190px; text-align: right; position: relative; }
.speed-row { display: flex; align-items: baseline; justify-content: flex-end; gap: 8px; }
#speed-num { font-family: var(--font-num); font-size: 56px; font-weight: 700; line-height: 0.95; color: var(--text); }
.speed-unit { font-size: 11px; letter-spacing: 2px; color: var(--text-dim); font-weight: 600; }
.speed-bar { height: 5px; background: rgba(255,255,255,0.09); margin-top: 7px; overflow: hidden; }
#speed-fill { height: 100%; width: 0%; background: linear-gradient(90deg, var(--teal), var(--amber) 60%, var(--orange)); transition: width 0.1s linear; }
.gear {
  position: absolute; left: 14px; top: 12px;
  font-family: var(--font-num); font-size: 24px; font-weight: 700; color: var(--amber);
  border: 1px solid var(--panel-border); padding: 1px 9px;
}

/* ---------- messages ---------- */
.center-msg {
  position: absolute; top: 24%; left: 50%; transform: translateX(-50%);
  font-size: 30px; font-weight: 700; letter-spacing: 4px; color: var(--teal);
  text-shadow: 0 0 22px rgba(55,224,200,0.6), 0 2px 4px rgba(0,0,0,0.6);
  opacity: 0; white-space: nowrap;
}
.center-msg.flash { animation: msgFlash 1.5s ease-out forwards; }
@keyframes msgFlash {
  0% { opacity: 0; transform: translateX(-50%) scale(1.5); }
  15% { opacity: 1; transform: translateX(-50%) scale(1); }
  75% { opacity: 1; }
  100% { opacity: 0; }
}
.wrong-way {
  position: absolute; top: 36%; left: 50%; transform: translateX(-50%);
  font-size: 26px; font-weight: 700; letter-spacing: 5px; color: var(--danger);
  text-shadow: 0 0 18px rgba(255,74,90,0.7);
  animation: wrongBlink 0.6s steps(2) infinite;
}
@keyframes wrongBlink { 50% { opacity: 0.25; } }

.countdown {
  position: absolute; top: 34%; left: 50%; transform: translate(-50%, -50%);
  font-family: var(--font-num); font-size: 130px; font-weight: 700; color: var(--amber);
  text-shadow: 0 0 40px rgba(255,179,71,0.55), 0 4px 8px rgba(0,0,0,0.5);
}
.countdown.pop { animation: cdPop 0.9s ease-out forwards; }
.countdown.go { color: var(--teal); text-shadow: 0 0 40px rgba(55,224,200,0.65); }
@keyframes cdPop {
  0% { transform: translate(-50%, -50%) scale(2.1); opacity: 0; }
  25% { transform: translate(-50%, -50%) scale(1); opacity: 1; }
  80% { opacity: 1; }
  100% { opacity: 0; }
}

/* ---------- mute + badge ---------- */
#mute-btn {
  position: fixed; top: 14px; right: 14px; z-index: 40;
  width: 42px; height: 42px; display: grid; place-items: center;
  background: var(--panel); color: var(--amber);
  border: 1px solid var(--panel-border); cursor: pointer;
  clip-path: polygon(8px 0, 100% 0, 100% calc(100% - 8px), calc(100% - 8px) 100%, 0 100%, 0 8px);
  transition: color 0.2s, border-color 0.2s;
  pointer-events: auto;
}
#mute-btn:hover { color: var(--text); border-color: var(--amber); }
#mute-btn.muted { color: var(--text-dim); }
body.racing #mute-btn { top: 160px; }

#backend-badge {
  position: fixed; left: 14px; bottom: 14px; z-index: 40;
  font-size: 10px; letter-spacing: 2px; font-weight: 600;
  color: var(--teal); background: rgba(13,14,20,0.6);
  border: 1px solid rgba(55,224,200,0.25); padding: 4px 10px;
}
body.racing #backend-badge { bottom: 84px; }
#backend-badge.fallback { color: var(--amber); border-color: rgba(255,179,71,0.3); }

/* ---------- overlays ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 30;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(160deg, rgba(10,10,18,0.82), rgba(24,14,10,0.6) 55%, rgba(10,10,18,0.85));
}
.overlay::before {
  content: ''; position: absolute; inset: 0; pointer-events: none;
  background:
    radial-gradient(ellipse 70% 55% at 70% 20%, rgba(255,106,61,0.13), transparent),
    radial-gradient(ellipse 55% 45% at 20% 80%, rgba(55,224,200,0.08), transparent);
}

.menu-inner { position: relative; text-align: center; padding: 20px; max-width: 640px; width: 100%; }
.title-block { margin-bottom: 26px; animation: riseIn 0.7s ease-out both; }
.title-kicker { font-size: 12px; letter-spacing: 6px; color: var(--teal); font-weight: 600; margin-bottom: 8px; }
.game-title {
  font-size: clamp(56px, 11vw, 104px); font-weight: 700; line-height: 0.95; letter-spacing: 2px;
  color: var(--text);
  text-shadow: 0 0 34px rgba(255,179,71,0.3), 0 4px 0 rgba(255,106,61,0.35);
}
.game-title span { color: var(--orange); }
.title-sub { margin-top: 10px; color: var(--text-dim); font-size: 14px; letter-spacing: 1.5px; }

.menu-card { padding: 26px 30px 24px; animation: riseIn 0.7s 0.15s ease-out both; }
@keyframes riseIn { from { opacity: 0; transform: translateY(26px); } to { opacity: 1; transform: none; } }

.menu-section-label { font-size: 11px; letter-spacing: 3px; color: var(--text-dim); font-weight: 600; margin-bottom: 10px; }
.difficulty-row { display: flex; gap: 8px; justify-content: center; margin-bottom: 20px; }
.diff-btn {
  font-family: var(--font-display); font-weight: 700; font-size: 14px; letter-spacing: 2px;
  padding: 9px 24px; cursor: pointer; color: var(--text-dim);
  background: rgba(255,255,255,0.04); border: 1px solid rgba(255,255,255,0.12);
  transition: all 0.18s;
}
.diff-btn:hover { color: var(--text); border-color: var(--amber); }
.diff-btn.active {
  color: #1a1206; background: var(--amber); border-color: var(--amber);
  box-shadow: 0 0 18px rgba(255,179,71,0.4);
}

.big-btn {
  font-family: var(--font-display); font-weight: 700; font-size: 21px; letter-spacing: 4px;
  width: 100%; padding: 15px 20px; cursor: pointer;
  color: #1a1206; background: linear-gradient(100deg, var(--amber), var(--orange));
  border: none; position: relative;
  clip-path: polygon(14px 0, 100% 0, 100% calc(100% - 14px), calc(100% - 14px) 100%, 0 100%, 0 14px);
  transition: filter 0.15s, transform 0.15s;
  box-shadow: 0 0 28px rgba(255,106,61,0.35);
}
.big-btn:hover { filter: brightness(1.12); transform: translateY(-2px); }
.big-btn:active { transform: translateY(1px); }

.ghost-btn {
  font-family: var(--font-display); font-weight: 600; font-size: 14px; letter-spacing: 3px;
  width: 100%; padding: 12px; margin-top: 10px; cursor: pointer;
  color: var(--text-dim); background: transparent; border: 1px solid rgba(255,255,255,0.15);
  transition: all 0.15s;
}
.ghost-btn:hover { color: var(--text); border-color: var(--amber); }

.controls-grid {
  margin-top: 20px; display: grid; grid-template-columns: 1fr 1fr; gap: 8px 18px;
  font-size: 12px; color: var(--text-dim); text-align: left; letter-spacing: 0.5px;
}
kbd {
  display: inline-block; font-family: var(--font-num); font-weight: 700; font-size: 11px;
  color: var(--amber); border: 1px solid rgba(255,179,71,0.35); border-bottom-width: 2px;
  padding: 1px 6px; margin-right: 3px; background: rgba(255,179,71,0.07);
}

/* ---------- dialogs ---------- */
.dialog { padding: 34px 40px; text-align: center; min-width: 340px; max-width: 92vw; animation: riseIn 0.4s ease-out both; }
.dialog h2 { font-size: 34px; letter-spacing: 6px; margin-bottom: 22px; color: var(--amber); }

.finish-dialog { min-width: 400px; }
.medal {
  display: inline-block; font-size: 13px; font-weight: 700; letter-spacing: 4px;
  padding: 6px 22px; margin-bottom: 14px;
  clip-path: polygon(10px 0, 100% 0, calc(100% - 10px) 100%, 0 100%);
}
.medal.gold { background: linear-gradient(100deg, #ffd97a, #ffb347); color: #3a2404; box-shadow: 0 0 26px rgba(255,179,71,0.5); }
.medal.silver { background: linear-gradient(100deg, #e8ecf2, #aab4c2); color: #2a2e36; }
.medal.bronze { background: linear-gradient(100deg, #e8a06a, #b8724a); color: #36180a; }
.medal.finisher { background: rgba(255,255,255,0.12); color: var(--text-dim); }
#finish-position { font-size: 44px; margin-bottom: 20px; }
.finish-stats { display: flex; flex-direction: column; gap: 10px; margin-bottom: 24px; }
.finish-stats > div {
  display: flex; justify-content: space-between; align-items: baseline; gap: 30px;
  border-bottom: 1px solid rgba(255,255,255,0.08); padding-bottom: 8px;
}
.finish-value { font-family: var(--font-num); font-size: 24px; font-weight: 700; color: var(--teal); }

/* ---------- loading ---------- */
#loading { z-index: 50; background: var(--bg); flex-direction: column; }
.load-inner { text-align: center; }
.load-title { font-size: 42px; font-weight: 700; letter-spacing: 4px; color: var(--orange); margin-bottom: 22px; }
.load-bar { width: 260px; height: 4px; background: rgba(255,255,255,0.08); margin: 0 auto; overflow: hidden; }
.load-fill { height: 100%; width: 40%; background: linear-gradient(90deg, var(--teal), var(--amber)); animation: loadSlide 1.1s ease-in-out infinite; }
@keyframes loadSlide { 0% { transform: translateX(-100%); } 100% { transform: translateX(350%); } }
.load-text { margin-top: 14px; font-size: 12px; letter-spacing: 3px; color: var(--text-dim); }

/* ---------- touch ---------- */
#touch-controls { position: fixed; inset: auto 0 0 0; z-index: 20; pointer-events: none; }
.touch-left { position: absolute; left: 16px; bottom: 100px; display: flex; gap: 14px; }
.touch-right { position: absolute; right: 16px; bottom: 100px; display: flex; gap: 14px; }
.touch-btn {
  pointer-events: auto; width: 74px; height: 74px; border-radius: 50%;
  font-size: 26px; color: var(--text); background: rgba(16,17,26,0.55);
  border: 2px solid rgba(255,179,71,0.4); backdrop-filter: blur(6px);
  touch-action: none;
}
.touch-btn:active { background: rgba(255,179,71,0.35); }
.touch-gas { border-color: rgba(55,224,200,0.55); }
.touch-brake { border-color: rgba(255,74,90,0.5); }

/* ---------- scrollbars ---------- */
::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-track { background: rgba(255,255,255,0.04); }
::-webkit-scrollbar-thumb { background: rgba(255,179,71,0.35); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: rgba(255,179,71,0.6); }

/* ---------- responsive ---------- */
@media (max-width: 760px) {
  .hud-top { gap: 8px; }
  #pos-num { font-size: 32px; }
  .race-time { font-size: 22px; }
  #minimap { width: 92px; height: 92px; }
  #speed-num { font-size: 40px; }
  .speed-panel { min-width: 150px; }
  .controls-grid { grid-template-columns: 1fr; }
  .dialog { padding: 26px 22px; min-width: 0; width: 92vw; }
  .finish-dialog { min-width: 0; }
  body.racing #mute-btn { top: 118px; }
  .cp-panel { display: none; }
  .hud-bottom { justify-content: flex-end; }
}