:root {
  --pink: #ff2e88;
  --cyan: #22d3ee;
  --gold: #ffd166;
  --red: #ff4d4d;
  --bg: #07020f;
  --ink: #f7f2ff;
  --muted: #a99ec4;
  --panel: rgba(18, 8, 32, 0.62);
  --border: rgba(255, 255, 255, 0.12);
  --font-display: 'Lalezar', 'Tajawal', system-ui, sans-serif;
  --font-body: 'Tajawal', system-ui, sans-serif;
  --font-num: 'Press Start 2P', 'Tajawal', monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

html, body {
  height: 100%;
  background: radial-gradient(circle at 50% 10%, #1b0730 0%, #07020f 62%, #02000a 100%);
  color: var(--ink);
  font-family: var(--font-body);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body {
  display: grid;
  place-items: center;
  padding: clamp(0px, 1.6vw, 18px);
}

.stage {
  position: relative;
  width: min(100%, 1500px);
  aspect-ratio: 1100 / 680;
  max-height: 100vh;
  border-radius: clamp(0px, 1.4vw, 20px);
  overflow: hidden;
  background: #0b0418;
  box-shadow:
    0 0 0 1px rgba(255, 46, 136, 0.22),
    0 30px 90px -20px rgba(255, 46, 136, 0.35),
    0 0 120px -30px rgba(34, 211, 238, 0.35);
  touch-action: none;
  user-select: none;
}

#game {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: auto;
}

/* ---------------- HUD ---------------- */
.hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  padding: clamp(8px, 1.4vw, 18px);
  display: flex;
  flex-direction: column;
  opacity: 0;
  transition: opacity 0.35s ease;
}
body.playing .hud { opacity: 1; }

.hud-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: clamp(6px, 1vw, 14px);
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: 14px;
  padding: clamp(6px, 0.7vw, 11px) clamp(8px, 1vw, 16px);
  box-shadow: 0 10px 30px -14px rgba(0, 0, 0, 0.9);
}

.stats {
  display: flex;
  gap: clamp(10px, 1.6vw, 26px);
}

.stat { display: flex; flex-direction: column; gap: 3px; }
.lbl {
  font-size: clamp(8px, 0.78vw, 11px);
  letter-spacing: 0.06em;
  color: var(--muted);
  font-weight: 700;
}
.stat .val {
  font-family: var(--font-num);
  font-size: clamp(8px, 0.85vw, 12px);
  color: #fff;
  text-shadow: 0 0 12px rgba(34, 211, 238, 0.7);
  line-height: 1.5;
}
.stat:nth-child(1) .val { color: var(--gold); text-shadow: 0 0 12px rgba(255, 209, 102, 0.6); }

.timebox { min-width: clamp(110px, 16vw, 240px); }
.trow { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.tval {
  font-family: var(--font-num);
  font-size: clamp(11px, 1.25vw, 18px);
  color: var(--cyan);
  text-shadow: 0 0 16px rgba(34, 211, 238, 0.8);
}
.timebox.danger .tval { color: var(--red); text-shadow: 0 0 18px rgba(255, 77, 77, 0.9); animation: pulse 0.8s ease-in-out infinite; }
.bar {
  margin-top: 6px;
  height: clamp(4px, 0.5vw, 7px);
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.12);
  overflow: hidden;
}
.bar i {
  display: block;
  height: 100%;
  width: 100%;
  border-radius: 99px;
  background: linear-gradient(90deg, var(--pink), var(--cyan));
  box-shadow: 0 0 12px rgba(255, 46, 136, 0.7);
  transition: width 0.15s linear;
}

.right { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; }
.speedo { display: flex; align-items: baseline; gap: 6px; }
.speedo .val {
  font-family: var(--font-num);
  font-size: clamp(14px, 1.7vw, 26px);
  color: #fff;
  text-shadow: 0 0 18px rgba(255, 46, 136, 0.8);
}
.speedo .unit { font-size: clamp(8px, 0.8vw, 11px); color: var(--muted); font-weight: 700; }
.gear { font-size: clamp(8px, 0.8vw, 11px); color: var(--muted); font-weight: 700; }
.gear b { color: var(--cyan); font-family: var(--font-num); font-size: clamp(8px, 0.85vw, 12px); }

/* ---------------- combo + actions ---------------- */
.combo {
  position: absolute;
  top: clamp(84px, 11.5vw, 138px);
  left: 50%;
  transform: translate(-50%, -6px) scale(0.9);
  font-family: var(--font-display);
  font-size: clamp(16px, 2.2vw, 30px);
  color: var(--gold);
  text-shadow: 0 0 18px rgba(255, 209, 102, 0.9), 0 0 44px rgba(255, 46, 136, 0.6);
  opacity: 0;
  transition: opacity 0.22s ease, transform 0.22s ease;
  pointer-events: none;
}
.combo.show { opacity: 1; transform: translate(-50%, 0) scale(1); }
.combo.hot { color: #fff; text-shadow: 0 0 20px rgba(255, 46, 136, 1), 0 0 60px rgba(255, 46, 136, 0.7); }

.hud-actions {
  position: absolute;
  bottom: clamp(8px, 1.2vw, 16px);
  right: clamp(8px, 1.4vw, 18px);
  display: flex;
  gap: 8px;
  pointer-events: auto;
}

.icon-btn {
  width: clamp(30px, 3vw, 40px);
  height: clamp(30px, 3vw, 40px);
  display: grid;
  place-items: center;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(18, 8, 32, 0.7);
  color: var(--ink);
  cursor: pointer;
  backdrop-filter: blur(10px);
  transition: transform 0.2s ease, border-color 0.2s ease, color 0.2s ease;
}
.icon-btn:hover { transform: translateY(-2px); border-color: var(--pink); color: var(--pink); }
.icon-btn.off { color: var(--red); border-color: rgba(255, 77, 77, 0.5); }
.ico-off { display: none; }
.icon-btn.off .ico-on { display: none; }
.icon-btn.off .ico-off { display: block; }
.icon-btn svg { width: 55%; height: 55%; }

/* ---------------- touch controls ---------------- */
.touch {
  position: absolute;
  inset: auto 0 clamp(8px, 1.4vw, 18px) 0;
  display: none;
  align-items: flex-end;
  justify-content: space-between;
  padding: 0 clamp(10px, 1.6vw, 20px);
  pointer-events: none;
}
.tgroup { display: flex; gap: clamp(8px, 1.4vw, 14px); pointer-events: auto; }

.tbtn {
  width: clamp(48px, 9vw, 82px);
  height: clamp(48px, 9vw, 82px);
  border-radius: 50%;
  display: grid;
  place-items: center;
  border: 1.5px solid rgba(255, 255, 255, 0.28);
  background: rgba(20, 9, 36, 0.55);
  color: #fff;
  backdrop-filter: blur(8px);
  cursor: pointer;
  transition: transform 0.12s ease, background 0.12s ease, border-color 0.12s ease;
  touch-action: none;
}
.tbtn svg { width: 46%; height: 46%; }
.tbtn.active { transform: scale(0.94); background: rgba(255, 46, 136, 0.36); border-color: var(--pink); }
.tbtn.gas { border-color: rgba(34, 211, 238, 0.6); color: var(--cyan); }
.tbtn.gas.active { background: rgba(34, 211, 238, 0.34); }
.tbtn.brake { border-color: rgba(255, 77, 77, 0.55); color: #ff8f8f; }
.tbtn.brake.active { background: rgba(255, 77, 77, 0.34); }

@media (pointer: coarse) {
  .touch { display: flex; }
  .hud-actions { display: none; }
}

/* ---------------- screens ---------------- */
.screen {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: clamp(10px, 3vw, 28px);
  background:
    radial-gradient(circle at 50% 45%, rgba(60, 10, 80, 0.55), rgba(5, 1, 12, 0.88) 70%),
    rgba(4, 1, 10, 0.55);
  backdrop-filter: blur(3px);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s;
  z-index: 5;
}
.screen.show { opacity: 1; visibility: visible; }

.card {
  position: relative;
  width: min(100%, 620px);
  padding: clamp(18px, 3vw, 38px);
  border-radius: clamp(16px, 2vw, 26px);
  background: linear-gradient(160deg, rgba(32, 12, 52, 0.92), rgba(11, 4, 24, 0.95));
  border: 1px solid rgba(255, 255, 255, 0.14);
  box-shadow:
    0 0 0 1px rgba(255, 46, 136, 0.16) inset,
    0 40px 90px -30px rgba(255, 46, 136, 0.55),
    0 0 70px -20px rgba(34, 211, 238, 0.35);
  text-align: center;
  animation: cardIn 0.45s cubic-bezier(0.22, 1, 0.36, 1) both;
}
.card.small { width: min(100%, 420px); }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 6px 13px;
  border-radius: 99px;
  font-size: clamp(10px, 1vw, 12px);
  font-weight: 700;
  color: var(--cyan);
  background: rgba(34, 211, 238, 0.12);
  border: 1px solid rgba(34, 211, 238, 0.4);
  margin-bottom: clamp(10px, 1.6vw, 18px);
}
.badge svg { width: 14px; height: 14px; }
.badge.danger { color: #ff9a9a; background: rgba(255, 77, 77, 0.12); border-color: rgba(255, 77, 77, 0.45); }

h1 {
  font-family: var(--font-display);
  font-weight: 400;
  font-size: clamp(30px, 5.4vw, 62px);
  line-height: 1.1;
  letter-spacing: 0.01em;
  background: linear-gradient(180deg, #fff 10%, #ffd166 55%, #ff2e88 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 0 22px rgba(255, 46, 136, 0.5));
  margin-bottom: clamp(8px, 1.4vw, 14px);
}

.card p {
  font-size: clamp(12px, 1.15vw, 15px);
  line-height: 1.85;
  color: #d8cfe8;
  margin-bottom: clamp(14px, 2vw, 22px);
}
.card p b { color: var(--gold); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  padding: clamp(11px, 1.3vw, 16px) clamp(22px, 2.6vw, 38px);
  border: 0;
  border-radius: 14px;
  font-family: var(--font-body);
  font-size: clamp(13px, 1.25vw, 17px);
  font-weight: 800;
  color: #fff;
  cursor: pointer;
  background: linear-gradient(135deg, #ff2e88, #a855f7 65%, #22d3ee);
  box-shadow: 0 14px 34px -12px rgba(255, 46, 136, 0.85), 0 0 0 1px rgba(255, 255, 255, 0.18) inset;
  transition: transform 0.2s ease, box-shadow 0.2s ease, filter 0.2s ease;
}
.btn svg { width: 1.05em; height: 1.05em; }
.btn:hover { transform: translateY(-3px); box-shadow: 0 22px 44px -14px rgba(255, 46, 136, 1); filter: brightness(1.06); }
.btn:active { transform: translateY(0) scale(0.985); }

.hint {
  display: flex;
  flex-wrap: wrap;
  gap: clamp(8px, 1.4vw, 16px);
  justify-content: center;
  margin-top: clamp(14px, 2vw, 22px);
  font-size: clamp(10px, 0.95vw, 12px);
  color: var(--muted);
  font-weight: 700;
}
.rotate-note {
  display: none;
  margin-top: 14px;
  font-size: 11px;
  font-weight: 700;
  color: var(--cyan);
  opacity: 0.85;
}
@keyframes turn { 0%,100% { transform: rotate(0deg); } 50% { transform: rotate(-90deg); } }
.rotate-note svg { width: 14px; height: 14px; vertical-align: -3px; animation: turn 3s ease-in-out infinite; }

.kbd {
  display: inline-grid;
  place-items: center;
  min-width: 22px;
  height: 22px;
  padding: 0 5px;
  margin-inline-end: 4px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.4);
  color: #fff;
  font-size: 10px;
}

/* ---------------- results ---------------- */
.results {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: clamp(8px, 1.2vw, 14px);
  margin: clamp(6px, 1vw, 12px) 0 clamp(18px, 2.4vw, 26px);
}
.results > div {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: clamp(9px, 1.2vw, 15px);
  border-radius: 13px;
  background: rgba(255, 255, 255, 0.045);
  border: 1px solid rgba(255, 255, 255, 0.1);
}
.results span { font-size: clamp(9px, 0.9vw, 11px); color: var(--muted); font-weight: 700; }
.results b {
  font-family: var(--font-num);
  font-size: clamp(11px, 1.15vw, 15px);
  color: var(--gold);
  text-shadow: 0 0 14px rgba(255, 209, 102, 0.55);
}
.results > div:nth-child(4) b { color: var(--cyan); text-shadow: 0 0 14px rgba(34, 211, 238, 0.55); }

@keyframes cardIn {
  from { opacity: 0; transform: translateY(18px) scale(0.97); }
  to { opacity: 1; transform: none; }
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.45; }
}

/* ---------------- responsive ---------------- */
@media (orientation: portrait) and (max-width: 900px) {
  .rotate-note { display: block; }
}

@media (max-width: 640px) {
  .stage { border-radius: 0; width: 100vw; }
  body { padding: 0; }
  .stats { gap: 10px; }
  .hint { display: none; }
  .combo { top: 96px; }
}

@media (max-aspect-ratio: 4/3) and (max-width: 900px) {
  .stat:nth-child(3) { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .card, .combo, .btn, .icon-btn, .tbtn { animation: none !important; transition: none !important; }
  .timebox.danger .tval { animation: none; }
}
