*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0; height: 100%; width: 100%;
  overflow: hidden;
}

body {
  font-family: 'Cairo', system-ui, -apple-system, 'Segoe UI', sans-serif;
  background: #8FD3FF;
  color: #2A2118;
  -webkit-user-select: none; user-select: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  touch-action: manipulation;
}

#game {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  background: #8FD3FF;
}

/* ================= HUD ================= */
#hud {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 8px 10px 6px;
  z-index: 10;
}

.hud-card {
  flex: 1 1 0;
  min-width: 0;
  background: #FFF3DC;
  border: 3px solid #2A2118;
  border-radius: 14px;
  box-shadow: 3px 3px 0 #E8621A;
  padding: 2px 6px 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  line-height: 1.15;
  color: #2A2118;
}

.hud-label {
  font-size: 10px;
  font-weight: 800;
  color: #8A6B4E;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.hud-value {
  font-size: 19px;
  font-weight: 900;
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
  color: #2A2118;
}

.hud-row {
  display: flex;
  align-items: baseline;
  gap: 4px;
}

.hud-combo {
  font-size: 10px;
  font-weight: 900;
  color: #FFF3DC;
  background: #2B5CE6;
  border-radius: 6px;
  padding: 0 4px 1px;
  line-height: 1.35;
}

.hud-map {
  flex: 0 0 60px;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  border: 3px solid #2A2118;
  box-shadow: 3px 3px 0 #E8621A;
  background: #F6E3C5;
  overflow: hidden;
  position: relative;
}

.hud-map canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.hud-pause {
  flex: 0 0 38px;
  width: 38px;
  height: 38px;
  border-radius: 12px;
  border: 3px solid #2A2118;
  background: #FFC43D;
  box-shadow: 3px 3px 0 #E8621A;
  color: #2A2118;
  display: grid;
  place-items: center;
  padding: 0;
  cursor: pointer;
}
.hud-pause svg { width: 18px; height: 18px; }
.hud-pause:active { transform: translate(2px, 2px); box-shadow: 1px 1px 0 #E8621A; }

/* ================= STAGE ================= */
#stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  background: #8FD3FF;
  touch-action: none;
}

#stage canvas#scene {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

#flash {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: #FF3B30;
  opacity: 0;
  transition: opacity .16s ease-out;
  z-index: 6;
}

.pill {
  position: absolute;
  left: 50%;
  top: 22%;
  transform: translate(-50%, 0) scale(.94);
  background: rgba(42, 33, 24, .88);
  color: #FFF3DC;
  font-weight: 900;
  font-size: 14px;
  padding: 7px 16px;
  border-radius: 14px;
  border: 3px solid #FFC43D;
  white-space: nowrap;
  pointer-events: none;
  z-index: 8;
  opacity: 0;
  transition: opacity .18s ease-out, transform .18s ease-out;
}

.pill.show {
  opacity: 1;
  transform: translate(-50%, 0) scale(1);
}

.pill.hidden { display: none !important; }

#load {
  top: auto;
  bottom: 14px;
  border-color: #2B5CE6;
  background: rgba(43, 92, 230, .92);
}

/* ================= CONTROLS ================= */
#controls {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px 10px;
  z-index: 10;
}

.ctrl-group {
  display: flex;
  align-items: center;
  gap: 7px;
}

.ctrl {
  --b: clamp(56px, 15vw, 66px);
  width: var(--b);
  height: var(--b);
  border-radius: 20px;
  border: 3px solid #2A2118;
  background: #FFC43D;
  color: #2A2118;
  box-shadow: 0 5px 0 #E8621A;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  padding: 0;
  font-family: inherit;
  font-weight: 900;
  cursor: pointer;
  touch-action: none;
  transition: transform .07s, box-shadow .07s;
  will-change: transform;
}

.ctrl svg { width: 26px; height: 26px; display: block; }
.ctrl span { font-size: 10px; line-height: 1; opacity: .9; }

.ctrl.pressed {
  transform: translateY(4px);
  box-shadow: 0 1px 0 #E8621A;
}

.ctrl-gas {
  background: #2B5CE6;
  color: #FFF3DC;
  box-shadow: 0 5px 0 #1A3E9E;
}
.ctrl-gas.pressed { box-shadow: 0 1px 0 #1A3E9E; }

.ctrl-brake {
  background: #FFF3DC;
  color: #2A2118;
}

.ctrl-hand {
  width: 46px;
  height: 46px;
  border-radius: 14px;
  background: #FF7A2F;
  color: #FFF3DC;
  box-shadow: 0 5px 0 #C24E12;
  gap: 0;
}
.ctrl-hand svg { width: 20px; height: 20px; }
.ctrl-hand span { font-size: 9px; }
.ctrl-hand.pressed { box-shadow: 0 1px 0 #C24E12; }

/* ================= MENUS ================= */
.menu {
  position: absolute;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 96px 14px 16px;
  background: rgba(42, 33, 24, .52);
  -webkit-backdrop-filter: blur(5px);
  backdrop-filter: blur(5px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.menu.hidden { display: none !important; }

.card {
  width: 100%;
  max-width: 400px;
  margin: auto;
  background: #FFF3DC;
  border: 4px solid #2A2118;
  border-radius: 24px;
  box-shadow: 6px 6px 0 #E8621A;
  padding: 16px 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  color: #2A2118;
}

.title {
  margin: 0;
  font-size: 30px;
  font-weight: 900;
  line-height: 1.05;
  text-align: center;
  color: #2A2118;
  letter-spacing: 0;
}

.title small {
  display: block;
  font-size: 12px;
  font-weight: 700;
  color: #8A6B4E;
  margin-top: 6px;
  letter-spacing: 0;
}

.rules {
  display: flex;
  flex-direction: column;
  gap: 7px;
  font-size: 13px;
  font-weight: 600;
  color: #4A3A2A;
  line-height: 1.45;
}

.rules > div {
  display: flex;
  gap: 8px;
  align-items: flex-start;
}

.bullet {
  flex: 0 0 22px;
  width: 22px;
  height: 22px;
  border-radius: 8px;
  background: #FFC43D;
  border: 2px solid #2A2118;
  display: grid;
  place-items: center;
  font-size: 12px;
  font-weight: 900;
  color: #2A2118;
}

.btn-main {
  width: 100%;
  padding: 13px 12px;
  border-radius: 18px;
  border: 4px solid #2A2118;
  background: #FFC43D;
  color: #2A2118;
  font-family: inherit;
  font-weight: 900;
  font-size: 20px;
  line-height: 1.2;
  box-shadow: 0 6px 0 #E8621A;
  cursor: pointer;
  transition: transform .08s, box-shadow .08s;
}
.btn-main:active { transform: translateY(4px); box-shadow: 0 2px 0 #E8621A; }
.btn-main:disabled { opacity: .55; cursor: default; }

.btn-ghost {
  width: 100%;
  padding: 11px 12px;
  border-radius: 16px;
  border: 3px solid #2A2118;
  background: #FFF3DC;
  color: #2A2118;
  font-family: inherit;
  font-weight: 800;
  font-size: 15px;
  box-shadow: 0 4px 0 #E8621A;
  cursor: pointer;
}
.btn-ghost:active { transform: translateY(3px); box-shadow: 0 1px 0 #E8621A; }

.stats {
  display: flex;
  gap: 8px;
}

.stat {
  flex: 1 1 0;
  min-width: 0;
  background: #FFF9EC;
  border: 3px solid #2A2118;
  border-radius: 14px;
  padding: 6px 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}
.stat span { font-size: 10px; font-weight: 800; color: #8A6B4E; }
.stat b { font-size: 19px; font-weight: 900; font-variant-numeric: tabular-nums; }

.save-note {
  font-size: 11px;
  font-weight: 700;
  color: #8A6B4E;
  text-align: center;
  min-height: 14px;
}

/* ================= LEADERBOARD ================= */
.board {
  background: #FFF9EC;
  border: 3px solid #2A2118;
  border-radius: 16px;
  padding: 8px 10px;
}

.board-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-weight: 900;
  font-size: 13px;
  color: #2A2118;
  margin-bottom: 6px;
}

.board ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 132px;
  overflow-y: auto;
}

.board li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 13px;
  font-weight: 700;
  color: #4A3A2A;
}

.board li b { font-variant-numeric: tabular-nums; }
.board li.me { color: #2B5CE6; }
.board li.empty { font-weight: 600; color: #A08A73; font-size: 12px; justify-content: center; }

.best-line {
  margin-top: 6px;
  font-size: 12px;
  font-weight: 700;
  color: #8A6B4E;
  text-align: center;
}
.best-line b { color: #2B5CE6; font-size: 14px; }

.mini-btn {
  border: 2px solid #2A2118;
  background: #2B5CE6;
  color: #FFF3DC;
  border-radius: 10px;
  font-family: inherit;
  font-weight: 900;
  font-size: 11px;
  padding: 4px 9px;
  cursor: pointer;
  box-shadow: 0 3px 0 #1A3E9E;
}
.mini-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 #1A3E9E; }
.mini-btn.hidden { display: none !important; }

/* ================= SCORE POP ================= */
@keyframes popScore {
  0% { transform: translate(-50%, 0) scale(.7); opacity: 0; }
  25% { transform: translate(-50%, -10px) scale(1.14); opacity: 1; }
  100% { transform: translate(-50%, -52px) scale(1); opacity: 0; }
}

.pill.pop { animation: popScore 1s ease-out forwards; }

@media (max-height: 460px) {
  .menu { padding: 70px 12px 12px; }
  .card { padding: 12px; gap: 9px; }
  .title { font-size: 22px; }
  .rules { font-size: 12px; }
  .board ul { max-height: 88px; }
}

@media (prefers-reduced-motion: reduce) {
  .pill, .ctrl, .btn-main, .btn-ghost { transition: none !important; }
  .pill.pop { animation-duration: .01s; }
}