:root {
  --cyan: #27e6ff;
  --magenta: #ff3df0;
  --amber: #ffb84d;
  --ink: #eef3f6;
  --ink-dim: #9fb2bd;
  --panel: rgba(8, 12, 20, 0.72);
  --panel-line: rgba(39, 230, 255, 0.28);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  width: 100%; height: 100%;
  overflow: hidden;
  background: #05060c;
  font-family: "Rajdhani", system-ui, sans-serif;
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

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

#vig {
  position: fixed; inset: 0;
  pointer-events: none;
  background: radial-gradient(115% 90% at 50% 40%, transparent 55%, rgba(2, 3, 8, 0.62) 100%);
  z-index: 2;
}

button { font-family: inherit; border: none; cursor: pointer; user-select: none; -webkit-user-select: none; }

/* ---------- HUD ---------- */

#hud { position: fixed; inset: 0; z-index: 5; pointer-events: none; }
#hud[hidden] { display: none; }
#hud button { pointer-events: auto; }

#scoreWrap {
  position: absolute;
  top: calc(var(--safe-t) + 12px);
  left: 50%;
  transform: translateX(-50%);
  text-align: center;
}
#score {
  font-size: 44px; font-weight: 700; line-height: 1;
  letter-spacing: 0.04em;
  text-shadow: 0 0 18px rgba(39, 230, 255, 0.55), 0 2px 8px rgba(0,0,0,0.8);
  font-variant-numeric: tabular-nums;
}
#combo {
  margin-top: 2px;
  font-size: 20px; font-weight: 700;
  color: var(--magenta);
  text-shadow: 0 0 14px rgba(255, 61, 240, 0.7);
  animation: combo-pulse 0.55s ease-in-out infinite;
}
#combo[hidden] { display: none; }
@keyframes combo-pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.12); } }

#statsWrap {
  position: absolute;
  top: calc(var(--safe-t) + 12px);
  right: calc(var(--safe-r) + 14px);
  display: flex; flex-direction: column; gap: 6px;
  align-items: flex-end;
}
.stat {
  background: var(--panel);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 12px;
  padding: 4px 12px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex; align-items: baseline; gap: 6px;
}
.stat span { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat label { font-size: 11px; color: var(--ink-dim); letter-spacing: 0.12em; text-transform: uppercase; }
.stat.best span { font-size: 16px; color: var(--amber); }

#driftLabel {
  position: absolute;
  top: calc(var(--safe-t) + 118px);
  left: 50%;
  transform: translateX(-50%) rotate(-4deg);
  font-size: 26px; font-weight: 700;
  letter-spacing: 0.4em;
  color: var(--amber);
  text-shadow: 0 0 16px rgba(255, 184, 77, 0.8);
}
#driftLabel[hidden] { display: none; }

#bonus {
  position: absolute;
  top: calc(var(--safe-t) + 152px);
  left: 50%;
  transform: translateX(-50%);
  font-size: 18px; font-weight: 700;
  color: var(--cyan);
  text-shadow: 0 0 12px rgba(39, 230, 255, 0.8);
  animation: bonus-rise 0.9s ease forwards;
}
#bonus[hidden] { display: none; }
@keyframes bonus-rise { from { opacity: 1; transform: translate(-50%, 8px); } to { opacity: 0; transform: translate(-50%, -18px); } }

.steer-zone { position: absolute; bottom: 0; top: 34%; width: 38%; pointer-events: none; }
#steerL { left: 0; }
#steerR { right: 0; }

#pedals {
  position: absolute;
  left: calc(var(--safe-l) + 18px);
  bottom: calc(var(--safe-b) + 26px);
  display: flex; gap: 12px;
}
.pedal {
  width: 86px; height: 92px;
  border-radius: 22px;
  background: rgba(8, 12, 20, 0.55);
  border: 2px solid rgba(39, 230, 255, 0.4);
  color: var(--cyan);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 0 18px rgba(39, 230, 255, 0.12), inset 0 0 14px rgba(39, 230, 255, 0.06);
  transition: transform 0.08s ease, background 0.08s ease;
  touch-action: none;
}
.pedal:active, .pedal.on {
  transform: scale(0.93);
  background: rgba(39, 230, 255, 0.22);
  box-shadow: 0 0 26px rgba(39, 230, 255, 0.35);
}

#goalChip {
  position: absolute;
  top: calc(var(--safe-t) + 12px);
  left: calc(var(--safe-l) + 66px);
  background: var(--panel);
  border: 1px solid rgba(255, 184, 77, 0.35);
  border-radius: 12px;
  padding: 6px 12px 8px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  min-width: 118px;
}
#goalText { font-size: 13px; font-weight: 700; color: var(--amber); letter-spacing: 0.04em; }
#goalBar { margin-top: 4px; height: 4px; border-radius: 999px; background: rgba(255,255,255,0.12); overflow: hidden; }
#goalFill { height: 100%; width: 0%; background: linear-gradient(90deg, var(--amber), #ffe9b8); border-radius: 999px; transition: width 0.2s ease; }

#heatGlow {
  position: fixed; inset: 0;
  pointer-events: none;
  opacity: 0;
  box-shadow: inset 0 0 90px rgba(255, 122, 40, 0.55), inset 0 0 200px rgba(255, 61, 240, 0.18);
  transition: opacity 0.35s ease;
  z-index: 3;
}

#crashRank { margin-top: 8px; font-size: 15px; letter-spacing: 0.3em; color: var(--ink-dim); font-weight: 600; }
#crashRank b { font-size: 30px; color: var(--amber); letter-spacing: 0; text-shadow: 0 0 18px rgba(255,184,77,0.7); }
#newBest {
  margin-top: 6px;
  font-size: 17px; font-weight: 700; letter-spacing: 0.24em;
  color: var(--mint, #37f0a0); color: #37f0a0;
  text-shadow: 0 0 16px rgba(55, 240, 160, 0.8);
  animation: combo-pulse 0.7s ease-in-out infinite;
}
#newBest[hidden] { display: none; }

#driftBtn {
  position: absolute;
  right: calc(var(--safe-r) + 18px);
  bottom: calc(var(--safe-b) + 26px);
  width: 92px; height: 92px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, rgba(255, 184, 77, 0.25), rgba(8, 12, 20, 0.8));
  border: 2px solid rgba(255, 184, 77, 0.55);
  color: var(--amber);
  font-size: 16px; font-weight: 700;
  letter-spacing: 0.12em;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  box-shadow: 0 0 24px rgba(255, 184, 77, 0.18), inset 0 0 18px rgba(255, 184, 77, 0.1);
  transition: transform 0.1s ease;
  touch-action: none;
}
#driftBtn:active, #driftBtn.on { transform: scale(0.93); background: radial-gradient(circle at 35% 30%, rgba(255, 184, 77, 0.5), rgba(20, 14, 6, 0.85)); }

.icon-btn {
  position: absolute;
  left: calc(var(--safe-l) + 14px);
  top: calc(var(--safe-t) + 12px);
  width: 44px; height: 44px;
  border-radius: 50%;
  background: var(--panel);
  border: 1px solid rgba(255,255,255,0.09);
  color: var(--ink);
  font-size: 17px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

/* ---------- crash ---------- */

#crash {
  position: fixed; inset: 0;
  z-index: 9;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(80% 60% at 50% 45%, rgba(60, 8, 14, 0.35), rgba(3, 4, 9, 0.88));
  padding: 20px;
}
#crash[hidden] { display: none; }
.crash-inner { text-align: center; }
.crash-title {
  font-size: 56px; font-weight: 700;
  letter-spacing: 0.22em;
  color: #ff5468;
  text-shadow: 0 0 30px rgba(255, 60, 90, 0.65);
  animation: crash-in 0.4s cubic-bezier(0.2, 1.6, 0.4, 1);
}
@keyframes crash-in { from { transform: scale(1.6); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.crash-sub { margin-top: 6px; font-size: 18px; color: var(--ink-dim); font-weight: 600; }
.crash-sub span { color: var(--ink); }

/* ---------- menu ---------- */

#menu {
  position: fixed; inset: 0;
  z-index: 10;
  display: flex; align-items: center; justify-content: center;
  padding: 20px calc(var(--safe-r) + 20px) calc(var(--safe-b) + 20px) calc(var(--safe-l) + 20px);
  background: radial-gradient(90% 70% at 50% 30%, rgba(6, 8, 16, 0.15), rgba(3, 4, 9, 0.8));
}
#menu[hidden] { display: none; }

.menu-card {
  width: min(92vw, 400px);
  background: rgba(7, 10, 18, 0.8);
  border: 1px solid var(--panel-line);
  border-radius: 26px;
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  padding: 30px 26px 24px;
  display: flex; flex-direction: column; align-items: center;
  text-align: center;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.65), 0 0 40px rgba(39, 230, 255, 0.06);
}

.menu-logo { filter: drop-shadow(0 0 10px rgba(39, 230, 255, 0.5)); }

.menu-title {
  font-size: 52px; font-weight: 700;
  line-height: 0.92;
  letter-spacing: 0.14em;
  margin-top: 10px;
  background: linear-gradient(165deg, #b7f6ff 10%, var(--cyan) 45%, var(--magenta) 95%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: none;
}
.menu-sub { font-size: 16px; color: var(--ink-dim); margin-top: 10px; font-weight: 600; }
.menu-best {
  margin-top: 12px;
  font-size: 14px;
  color: var(--ink-dim);
  background: rgba(255, 184, 77, 0.08);
  border: 1px solid rgba(255, 184, 77, 0.25);
  border-radius: 999px;
  padding: 5px 14px;
}
.menu-best b { color: var(--amber); font-size: 16px; }
.menu-best[hidden] { display: none; }

.primary {
  margin-top: 20px;
  width: 100%;
  padding: 16px;
  border-radius: 18px;
  background: linear-gradient(160deg, #7deeff, var(--cyan) 45%, #0e9cbd);
  color: #041018;
  font-size: 21px; font-weight: 700;
  letter-spacing: 0.16em;
  box-shadow: 0 10px 30px rgba(39, 230, 255, 0.3);
  transition: transform 0.12s ease;
}
.primary:active { transform: scale(0.97); }

.menu-hints {
  margin-top: 18px;
  display: flex; flex-direction: column; gap: 4px;
  font-size: 13px; color: var(--ink-dim); font-weight: 600;
}

/* ---------- loader ---------- */

#loader {
  position: fixed; inset: 0;
  z-index: 20;
  display: flex; align-items: center; justify-content: center;
  background:
    radial-gradient(120% 90% at 50% 10%, #131a33 0%, #0a0d1e 50%, #04050c 100%);
  transition: opacity 0.45s ease;
}
#loader.done { opacity: 0; pointer-events: none; }
.loader-inner { display: flex; flex-direction: column; align-items: center; gap: 14px; }
.loader-title {
  font-size: 38px; font-weight: 700;
  letter-spacing: 0.22em;
  background: linear-gradient(165deg, #b7f6ff 10%, var(--cyan) 45%, var(--magenta) 95%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  animation: neon-flicker 2.4s linear infinite;
}
@keyframes neon-flicker {
  0%, 100% { opacity: 1; } 92% { opacity: 1; } 93% { opacity: 0.4; }
  94% { opacity: 1; } 96% { opacity: 0.6; } 97% { opacity: 1; }
}
.loader-bar {
  width: min(60vw, 260px); height: 5px;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 999px;
  overflow: hidden;
}
.loader-fill {
  height: 100%; width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--cyan), var(--magenta));
  transition: width 0.25s ease;
  box-shadow: 0 0 12px rgba(39, 230, 255, 0.7);
}
.loader-sub { font-size: 14px; color: var(--ink-dim); font-weight: 600; letter-spacing: 0.06em; }

@media (max-width: 700px) {
  #score { font-size: 38px; }
  .menu-title { font-size: 44px; }
  #driftBtn { width: 84px; height: 84px; }
}
