/* ============================================================
   GLOBAL — kill browser gestures, scrolling, selection, double-tap zoom
   ============================================================ */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  overflow: hidden;             /* no scrollbars, ever                 */
  overscroll-behavior: none;    /* no pull-to-refresh / elastic bounce */
  background: #0b0b12;
  color: #fff;
  font-family: 'Segoe UI', system-ui, sans-serif;
}

/* neutralise selection + touch-callout on the whole game area */
#stage {
  width: 100vw;
  height: 100vh;
  position: relative;
  touch-action: none;           /* tells browser: hands off, we handle touches */
  user-select: none;            /* no text selection while hammering buttons  */
  -webkit-user-select: none;
  -webkit-touch-callout: none;  /* iOS: no long-press popup                    */
  -webkit-tap-highlight-color: transparent; /* no grey flash on tap           */
}

/* ============================================================
   CANVAS — the game surface
   CSS size is "whatever the screen is". The real pixel buffer
   is set in renderer.js using devicePixelRatio. Never hard-code px here.
   ============================================================ */
#game {
  width: 100%;
  height: 100%;
  display: block;
}

/* ============================================================
   HUD — F1 style: TIME/LAP/POS box top-left, driver names
   bottom-left, round gear+KPH speedo bottom-right.
   HTML so we skip canvas text rendering. pointer-events:none
   everywhere: the HUD never steals touches from the buttons.
   ============================================================ */
#hud {
  position: absolute;
  inset: 0;
  z-index: 10;
  pointer-events: none;
}
#f1box {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 8px);
  left: 8px;
  background: rgba(10, 10, 14, 0.72);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: clamp(12px, 2.4vw, 20px);
  font-weight: 700;
  letter-spacing: 1px;
  color: #fff;
}
.f1row { display: flex; gap: 8px; margin: 1px 0; }
.f1lab { font-weight: 600; min-width: 3.2em; color: #e8e8e8; }
.f1row:first-child .f1lab { color: #37c837; }   /* green TIME: label */
#drivers {
  position: absolute;
  left: 8px;
  bottom: calc(env(safe-area-inset-bottom) + 10px);
  font-size: clamp(11px, 2vw, 17px);
  font-weight: 600;
}
#drv1 { background: rgba(240, 240, 240, 0.92); color: #111; padding: 3px 10px; border-radius: 3px 3px 0 0; }
#drv2 { background: rgba(10, 10, 14, 0.78); color: #fff; padding: 3px 10px; border-radius: 0 0 3px 3px; }
#speedo {
  position: absolute;
  right: 12px;
  bottom: calc(env(safe-area-inset-bottom) + 10px);
  width: clamp(84px, 15vh, 128px);
  height: clamp(84px, 15vh, 128px);
  border-radius: 50%;
  background: rgba(8, 8, 10, 0.82);
  border: 3px solid rgba(255, 255, 255, 0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #fff;
}
#gear { font-size: clamp(20px, 4.5vh, 34px); font-weight: 800; line-height: 1; }

/* drift tag — amber "DRIFT 24°" badge that pops above the speedo
   while the rear is sliding out (gear also flips to D) */
#driftTag {
  position: absolute;
  right: 12px;
  bottom: calc(env(safe-area-inset-bottom) + 10px + clamp(84px, 15vh, 128px) + 10px);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 3px 10px;
  border-radius: 6px;
  background: rgba(214, 99, 20, 0.9);
  color: #fff;
  font-weight: 800;
  letter-spacing: 2px;
  font-size: clamp(11px, 2vw, 16px);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
}

/* drift competition box — top-centre on the airport arena: the
   settled head-bar also shows points + chain multiplier */
#driftBox {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 8px);
  left: 50%;
  transform: translateX(-50%);
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-width: 150px;
  padding: 8px 16px;
  border-radius: 10px;
  background: rgba(8, 8, 12, 0.66);
  border: 1px solid rgba(255, 176, 44, 0.35);
  color: #fff;
  text-align: center;
  pointer-events: none;
}
#driftBox .dLab {
  font-size: 10px;
  letter-spacing: 4px;
  color: #ffb02c;
  font-weight: 800;
}
#driftPts {
  font-size: clamp(20px, 3vw, 30px);
  font-weight: 900;
  line-height: 1.1;
  color: #ffd54b;
  font-variant-numeric: tabular-nums;
}
#driftChain { font-size: 12px; font-weight: 800; letter-spacing: 1px; color: #ffb02c; min-height: 14px; }

/* ============================================================
   GARAGE (start screen) — car selection, coins, Play / Create Room
   ============================================================ */
#startScreen {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at center, rgba(5,5,10,0.30) 0%, rgba(5,5,10,0.84) 100%);
  overflow: auto;
}
#garage {
  width: min(780px, 94vw);
  max-height: 96vh;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
  align-items: center;
  padding: 6px 0 20px;
}
#garageTop {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
#garageTitle {
  font-size: clamp(30px, 6.5vw, 52px);
  font-weight: 900;
  letter-spacing: 4px;
  color: #fff;
  text-shadow: 0 3px 0 #c8352b, 0 6px 18px rgba(0,0,0,0.6);
}
#garageCoinWrap {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  background: rgba(10, 10, 14, 0.72);
  border: 1px solid rgba(255, 213, 75, 0.35);
  border-radius: 8px;
  padding: 5px 12px;
}
#garageCoins { font-size: clamp(20px, 3.4vw, 30px); font-weight: 800; color: #ffd54b; line-height: 1.05; }
.coinLabel { font-size: 10px; letter-spacing: 2px; color: #bbb; }
#bestLine {
  margin: 0;
  font-size: clamp(13px, 2.2vw, 18px);
  font-weight: 700;
  letter-spacing: 2px;
  color: #9fc5ff;
  align-self: flex-start;
}
#mapRow {
  width: 100%;
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}
.mapChip {
  cursor: pointer;
  border-radius: 8px;
  border: 2px solid rgba(255, 255, 255, 0.12);
  background: rgba(10, 10, 14, 0.72);
  padding: 6px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  transition: transform 0.12s, border-color 0.12s, box-shadow 0.12s;
}
.mapChip:hover { transform: translateY(-2px); }
.mapChip.selected {
  border-color: #ffd54b;
  box-shadow: 0 0 0 2px rgba(255, 213, 75, 0.3);
}
.mapChipName { font-size: 13px; font-weight: 800; letter-spacing: 2px; color: #fff; }
.mapChipTag { font-size: 9px; letter-spacing: 1px; color: #9fc5ff; }
#carRow {
  width: 100%;
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}
.carCard {
  position: relative;
  width: 128px;
  border-radius: 10px;
  background: rgba(10, 10, 14, 0.72);
  border: 2px solid rgba(255, 255, 255, 0.12);
  padding: 8px 8px 9px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 6px;
  transition: transform 0.12s, border-color 0.12s, box-shadow 0.12s;
}
.carCard:hover { transform: translateY(-2px); }
.carCard.selected {
  border-color: #ffd54b;
  box-shadow: 0 0 0 2px rgba(255, 213, 75, 0.35), 0 10px 24px rgba(0,0,0,0.5);
}
.carCard.locked { filter: grayscale(1); opacity: 0.62; }
.carThumb {
  height: 46px;
  border-radius: 6px;
  background: var(--body);
  border-top: 8px solid var(--trim);
  position: relative;
  overflow: hidden;
}
.carThumb::after {                      /* a hint of a cockpit window */
  content: "";
  position: absolute;
  left: 28%; right: 28%; top: 26%; height: 30%;
  border-radius: 999px;
  background: rgba(10, 10, 14, 0.65);
}
.carName { font-size: 15px; font-weight: 800; letter-spacing: 2px; text-align: center; color: #fff; }
.carFoot { font-size: 11px; text-align: center; color: #9fc5ff; font-weight: 600; }
.carCard.locked .carFoot { color: #ffd54b; }
.lockBadge {
  position: absolute;
  top: 5px; right: 5px;
  background: #a0312b;
  color: #fff;
  font-size: 9px;
  letter-spacing: 1px;
  padding: 2px 5px;
  border-radius: 4px;
}
#carDetail {
  width: 100%;
  display: flex;
  gap: 14px;
  align-items: flex-start;
  flex-wrap: wrap;
  background: rgba(10, 10, 14, 0.72);
  border: 1px solid rgba(255, 255, 255, 0.10);
  border-radius: 12px;
  padding: 14px;
}
#carThumbBig {
  width: 92px;
  height: 64px;
  flex: 0 0 auto;
  border-radius: 10px;
  border-top: 12px solid var(--trim);
  background: var(--body);
  position: relative;
  overflow: hidden;
}
#carThumbBig::after {
  content: "";
  position: absolute;
  left: 26%; right: 26%; top: 24%; height: 34%;
  border-radius: 999px;
  background: rgba(10, 10, 14, 0.65);
}
#carDetailMain { flex: 1 1 200px; display: flex; flex-direction: column; gap: 3px; min-width: 0; }
#carDetailName { font-size: clamp(17px, 3vw, 25px); font-weight: 900; letter-spacing: 3px; color: #fff; }
#carDetailTag { font-size: 13px; color: #ffd54b; font-weight: 700; letter-spacing: 1px; }
#carDetailDesc { font-size: 12px; color: #ccc; line-height: 1.45; }
#carStats { min-width: 150px; display: flex; flex-direction: column; gap: 4px; }
.statRow { display: flex; align-items: center; gap: 8px; font-size: 11px; letter-spacing: 1px; color: #bbb; }
.statRow .sLabel { width: 46px; }
.statDots { display: flex; gap: 3px; }
.statDots i { width: 11px; height: 7px; border-radius: 2px; background: rgba(255,255,255,0.18); }
.statDots i.on { background: #37c837; }
#carAction { flex: 1 1 100%; display: flex; align-items: center; gap: 12px; }
#unlockBtn {
  background: #1a7a3c;
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 800;
  letter-spacing: 1px;
  cursor: pointer;
  box-shadow: 0 4px 0 #0f4d24;
}
#unlockBtn:disabled { background: #3f4a52; box-shadow: none; cursor: not-allowed; color: #9aa4ad; }
#unlockBtn:active:not(:disabled) { transform: translateY(3px); box-shadow: 0 1px 0 #0f4d24; }
#selectedTag { font-size: 12px; letter-spacing: 2px; color: #37c837; font-weight: 800; }
#garageActions { display: flex; gap: 12px; align-items: center; }
#playBtn {
  font-size: clamp(18px, 3.4vw, 26px);
  font-weight: 800;
  letter-spacing: 3px;
  color: #fff;
  background: #1a7a3c;
  border: none;
  border-radius: 10px;
  padding: 10px 38px;
  cursor: pointer;
  box-shadow: 0 5px 0 #0f4d24, 0 10px 22px rgba(0,0,0,0.5);
}
#playBtn:active { transform: translateY(3px); box-shadow: 0 2px 0 #0f4d24; }
#roomBtn {
  font-size: clamp(16px, 3vw, 22px);
  font-weight: 800;
  letter-spacing: 2px;
  color: #fff;
  background: #1c4f9c;
  border: none;
  border-radius: 10px;
  padding: 10px 26px;
  cursor: pointer;
  box-shadow: 0 5px 0 #0f2d54, 0 10px 22px rgba(0,0,0,0.5);
}
#roomBtn:active { transform: translateY(3px); box-shadow: 0 2px 0 #0f2d54; }
#adminBtn {
  background: none;
  border: 1px solid rgba(255,255,255,0.25);
  color: rgba(255,255,255,0.5);
  font-size: 11px;
  letter-spacing: 2px;
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
}
#adminBtn:hover { color: #fff; border-color: rgba(255,255,255,0.5); }
#startHint {
  margin-top: 2px;
  font-size: clamp(11px, 1.8vw, 15px);
  letter-spacing: 1px;
  color: rgba(255,255,255,0.75);
}
#toast {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 12px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 25;
  background: rgba(10, 10, 14, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #ffd54b;
  padding: 8px 18px;
  border-radius: 8px;
  font-weight: 700;
  letter-spacing: 1px;
  display: none;
  white-space: nowrap;
}
#toast.show { display: block; }

/* ============================================================
   PAUSE / STOP — small ⏸ button (top-right, shown while racing)
   and a full-screen PAUSED menu (Resume / Restart / Stop).
   ============================================================ */
#pauseBtn {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 8px);
  right: 8px;
  z-index: 12;
  pointer-events: auto;              /* HUD itself is none; re-enable here */
  display: none;                     /* shown only while a race is active */
  align-items: center;
  justify-content: center;
  width: clamp(34px, 6vw, 48px);
  height: clamp(34px, 6vw, 48px);
  border-radius: 8px;
  background: rgba(10, 10, 14, 0.72);
  border: 1px solid rgba(255, 255, 255, 0.35);
  color: #fff;
  font-size: clamp(15px, 2.4vw, 20px);
  cursor: pointer;
  line-height: 1;
}
#pauseBtn:active { background: rgba(255, 255, 255, 0.28); }

#pauseMenu {
  position: absolute;
  inset: 0;
  z-index: 21;
  display: none;
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at center, rgba(5,5,10,0.30) 0%, rgba(5,5,10,0.82) 100%);
}
#pauseCard { text-align: center; }
#pauseTitle {
  font-size: clamp(30px, 7vw, 58px);
  font-weight: 900;
  letter-spacing: 5px;
  color: #fff;
  text-shadow: 0 4px 0 #c8352b, 0 8px 24px rgba(0,0,0,0.6);
  margin-bottom: 26px;
}
#resumeBtn, #restartBtn, #stopBtn {
  display: block;
  margin: 0 auto 14px;
  width: clamp(190px, 34vw, 260px);
  font-size: clamp(17px, 3vw, 26px);
  font-weight: 800;
  letter-spacing: 3px;
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 11px 0;
  cursor: pointer;
  box-shadow: 0 5px 0 rgba(0,0,0,0.45), 0 10px 22px rgba(0,0,0,0.5);
}
#resumeBtn:active, #restartBtn:active, #stopBtn:active { transform: translateY(3px); box-shadow: 0 2px 0 rgba(0,0,0,0.45); }
#resumeBtn  { background: #1a7a3c; }
#restartBtn { background: #a3681a; }
#stopBtn    { background: #a0312b; }
#pauseHint {
  margin-top: 18px;
  font-size: clamp(11px, 1.8vw, 15px);
  letter-spacing: 1px;
  color: rgba(255,255,255,0.75);
}

/* ============================================================
   COUNTDOWN — giant 3·2·1·GO over the track
   ============================================================ */
#countdown {
  position: absolute;
  inset: 0;
  z-index: 15;
  display: none;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  white-space: pre-line;          /* finish overlay can show 2 lines */
  text-align: center;
  font-size: clamp(52px, 13vw, 150px);
  font-weight: 900;
  line-height: 1.02;
  color: #fff;
  text-shadow: 0 6px 0 rgba(0,0,0,0.45), 0 12px 40px rgba(0,0,0,0.6);
}
#speed { font-size: clamp(18px, 4vh, 30px); font-weight: 800; line-height: 1.05; }
.kph { font-size: clamp(9px, 1.6vh, 12px); letter-spacing: 2px; color: #bbb; }

/* ============================================================
   TOUCHPAD — thumb buttons.
   Grid: exactly like a racing game — accel bottom-right,
   brake left of it, handbrake above accel, steering left side.
   ============================================================ */
#touchpad {
  position: absolute;
  inset: 0;
  z-index: 5;
  pointer-events: none;      /* buttons only catch touches, never block the canvas */
  display: none;             /* keyboard players don't even get it in the DOM flow */
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  gap: 0;
}

/* activated by input.js when a touch device is detected */
#touchpad.active { display: grid; }

.tc-btn {
  pointer-events: auto;      /* re-enable touches ONLY on the buttons */
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 7vh;
  color: rgba(255, 255, 255, 0.85);
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.10);
  border-radius: 16px;
  margin: 2px;
  touch-action: none;
  user-select: none;
}

/* pressed-state feedback: input.js toggles .held on the button */
.tc-btn.held { background: rgba(255, 255, 255, 0.28); color: #fff; transform: scale(0.97); }

/* ============================================================
   Touch control LAYOUT (landscape is the racing view):
   left column  = steering  |  right column = accel/brake/hand
   ============================================================ */
#steerL { grid-column: 1; grid-row: 2 / 4; }
#steerR { grid-column: 2; grid-row: 2 / 4; }
#accel  { grid-column: 5 / 7; grid-row: 3; }
#brake  { grid-column: 3 / 5; grid-row: 3; }
#hand   { grid-column: 3 / 5; grid-row: 2; }

/* portrait: stack controls tighter (best-effort, landscape is the main view) */
@media (orientation: portrait) {
  .tc-btn { font-size: 11vw; border-radius: 12px; }
  #speedo { right: 8px; }
}