@font-face { font-family: 'SairaCond'; src: url('../fonts/saira-condensed-600.woff2') format('woff2'); font-weight: 600; font-display: swap; }
@font-face { font-family: 'SairaCond'; src: url('../fonts/saira-condensed-700.woff2') format('woff2'); font-weight: 700; font-display: swap; }
@font-face { font-family: 'SairaCond'; src: url('../fonts/saira-condensed-800.woff2') format('woff2'); font-weight: 800; font-display: swap; }

:root {
  --ink: #05070b;
  --panel: rgba(12, 16, 22, 0.58);
  --line: rgba(255, 255, 255, 0.10);
  --line-strong: rgba(255, 255, 255, 0.22);
  --text: #eef3f8;
  --muted: #96a3b3;
  --amber: #ff8a3d;
  --amber-soft: #ffb46b;
  --red: #ff3b30;
  --r: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  height: 100%; overflow: hidden; background: var(--ink); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Inter, Roboto, sans-serif;
  overscroll-behavior: none; touch-action: none; user-select: none;
}
canvas#game { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }
.mono { font-family: 'SairaCond', ui-monospace, monospace; font-variant-numeric: tabular-nums; letter-spacing: 0.02em; }
.hidden { opacity: 0 !important; pointer-events: none !important; }

/* ---- film grain + vignette ---- */
.grade {
  position: fixed; inset: 0; pointer-events: none; z-index: 2;
  background:
    radial-gradient(120% 90% at 50% 42%, transparent 40%, rgba(0,0,0,0.42) 100%),
    linear-gradient(to top, rgba(4,6,10,0.62) 0%, transparent 34%);
  mix-blend-mode: multiply;
}

/* ================================ HUD ================================ */
#hud {
  position: fixed; inset: 0; z-index: 5; pointer-events: none;
  padding: calc(14px + env(safe-area-inset-top, 0px)) 16px calc(16px + var(--safe-b));
  transition: opacity .45s ease;
}
.hud-top { display: flex; justify-content: space-between; align-items: flex-start; }
.panel {
  background: var(--panel); border: 1px solid var(--line);
  backdrop-filter: blur(16px) saturate(140%); -webkit-backdrop-filter: blur(16px) saturate(140%);
  border-radius: var(--r); box-shadow: 0 18px 40px rgba(0,0,0,.45);
}
.lapbox { padding: 10px 14px 11px; min-width: 172px; }
.lap-row { display: flex; align-items: baseline; gap: 8px; }
.lap-row .v { font-family: 'SairaCond'; font-weight: 800; font-size: 30px; line-height: 1; letter-spacing: .01em; }
.lap-row .v i { font-style: normal; font-size: 14px; color: var(--muted); font-weight: 600; }
.k { font-size: 9.5px; letter-spacing: .18em; color: var(--muted); font-weight: 700; }
.t-row { display: flex; justify-content: space-between; align-items: baseline; margin-top: 5px; gap: 14px; }
.t-row .t { font-size: 16px; font-weight: 700; }
.t-row.dim .t { color: var(--muted); font-size: 14px; }
.hud-right { display: flex; flex-direction: column; align-items: flex-end; gap: 10px; }
#minimap {
  width: 96px; height: 96px; border-radius: 50%;
  background: rgba(8,11,16,.5); border: 1px solid var(--line);
  backdrop-filter: blur(10px); box-shadow: 0 10px 26px rgba(0,0,0,.4);
}
.top-actions { display: flex; gap: 8px; pointer-events: auto; }
.iconbtn {
  width: 38px; height: 38px; border-radius: 12px; display: grid; place-items: center;
  background: var(--panel); border: 1px solid var(--line); backdrop-filter: blur(12px);
  cursor: pointer; transition: transform .18s ease, border-color .18s ease;
}
.iconbtn:hover { transform: translateY(-2px); border-color: var(--line-strong); }
.iconbtn svg { width: 18px; height: 18px; fill: none; stroke: var(--text); stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.iconbtn svg rect { fill: var(--text); stroke: none; }
.iconbtn.muted .w1, .iconbtn.muted .w2 { opacity: .15; }
.iconbtn.muted { color: var(--muted); }

/* ------------------------------ speedometer ------------------------------ */
.hud-bottom {
  position: absolute; left: 0; right: 0; bottom: calc(18px + var(--safe-b));
  display: flex; align-items: flex-end; justify-content: space-between;
  padding: 0 20px; gap: 18px;
}
.speedo { position: relative; width: 200px; height: 118px; }
.arc { position: absolute; inset: 0; width: 200px; height: 108px; overflow: visible; }
.arc path { fill: none; stroke-linecap: round; }
.arc-bg { stroke: rgba(255,255,255,.11); stroke-width: 7; stroke-dasharray: 1 400; }
.arc-fill {
  stroke: url(#g1); stroke: var(--amber); stroke-width: 7;
  stroke-dasharray: 270; stroke-dashoffset: 270;
  filter: drop-shadow(0 0 10px rgba(255,138,61,.55));
  transition: stroke-dashoffset .12s linear;
}
.speedval { position: absolute; left: 0; right: 0; bottom: 12px; text-align: center; }
.speedval #kmh {
  font-family: 'SairaCond'; font-weight: 800; font-size: 62px; line-height: .9;
  letter-spacing: -.02em; font-variant-numeric: tabular-nums;
  text-shadow: 0 6px 26px rgba(0,0,0,.6);
}
.speedval small { display: block; font-size: 9px; letter-spacing: .26em; color: var(--muted); margin-top: 4px; font-weight: 700; }
.gearbox {
  position: absolute; right: -4px; bottom: 6px; text-align: center;
  border-left: 1px solid var(--line); padding-left: 10px;
}
.gearbox b { display: block; font-family: 'SairaCond'; font-size: 26px; font-weight: 700; line-height: 1; margin-top: 2px; }

.nitro { flex: 1; max-width: 300px; margin-bottom: 16px; }
.nitro-label { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; }
.nitro-label span { font-size: 9.5px; letter-spacing: .2em; color: var(--muted); font-weight: 700; }
.nitro-label i { font-style: normal; font-family: 'SairaCond'; font-size: 14px; color: var(--amber-soft); }
.nitro .bar {
  position: relative; height: 9px; border-radius: 99px; overflow: hidden;
  background: rgba(255,255,255,.08); border: 1px solid var(--line);
}
#nitroFill {
  height: 100%; width: 100%; border-radius: 99px;
  background: linear-gradient(90deg, #ff6a00, #ffc46b);
  box-shadow: 0 0 16px rgba(255,138,61,.7);
  transition: width .1s linear;
}
.nitro.active #nitroFill { background: linear-gradient(90deg, #fff2d8, #ff9a3d); }
.ticks { position: absolute; inset: 0; background-image: repeating-linear-gradient(90deg, transparent 0 19px, rgba(5,7,11,.55) 19px 21px); }

/* -------------------------------- pedals -------------------------------- */
.pedals {
  position: absolute; right: 18px; bottom: calc(108px + var(--safe-b));
  display: flex; flex-direction: column; gap: 10px; pointer-events: auto;
}
.pedal {
  width: 70px; height: 70px; border-radius: 50%;
  border: 1px solid rgba(255,255,255,.16);
  background: radial-gradient(circle at 35% 28%, rgba(255,255,255,.16), rgba(12,16,22,.72) 62%);
  backdrop-filter: blur(14px);
  color: var(--text); font-size: 10px; font-weight: 800; letter-spacing: .12em;
  display: grid; place-items: center; cursor: pointer;
  box-shadow: 0 12px 26px rgba(0,0,0,.45), inset 0 1px 0 rgba(255,255,255,.14);
  transition: transform .12s ease, box-shadow .12s ease, background .18s ease;
}
.pedal:active, .pedal.on { transform: scale(.94); }
.pedal.nitro { border-color: rgba(255,138,61,.45); color: #ffd7b6; box-shadow: 0 12px 30px rgba(255,110,20,.3), inset 0 1px 0 rgba(255,255,255,.18); }
.pedal.nitro.on { background: radial-gradient(circle at 35% 28%, rgba(255,190,120,.55), rgba(255,110,20,.35) 70%); }
.pedal em { font-style: normal; font-size: 8px; vertical-align: super; opacity: .8; }

/* ------------------------------ countdown ------------------------------ */
.countdown {
  position: absolute; inset: 0; display: grid; place-items: center;
  transition: opacity .2s ease;
}
.countdown span {
  font-family: 'SairaCond'; font-weight: 800; font-size: min(46vw, 260px); line-height: 1;
  color: #fff; text-shadow: 0 10px 70px rgba(255,120,40,.55), 0 2px 0 rgba(0,0,0,.3);
  animation: pop .5s cubic-bezier(.2,.9,.3,1.4);
}
@keyframes pop { from { transform: scale(.55); opacity: 0; } to { transform: scale(1); opacity: 1; } }

.toast {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%) scale(.9);
  padding: 12px 26px; border-radius: 999px; font-family: 'SairaCond'; font-weight: 700;
  letter-spacing: .16em; font-size: 15px; background: rgba(8,11,16,.6);
  border: 1px solid rgba(255,255,255,.14); backdrop-filter: blur(14px);
  opacity: 0; transition: opacity .3s ease, transform .3s ease; white-space: nowrap;
}
.toast.show { opacity: 1; transform: translate(-50%,-50%) scale(1); }

/* ============================== SCREENS ============================== */
.screen {
  position: fixed; inset: 0; z-index: 10;
  display: flex; flex-direction: column; justify-content: space-between;
  padding: calc(24px + env(safe-area-inset-top, 0px)) 22px calc(24px + var(--safe-b));
  transition: opacity .4s ease;
  background:
    radial-gradient(90% 60% at 78% 10%, rgba(255,138,61,.13), transparent 60%),
    linear-gradient(to top, rgba(5,7,11,.94) 0%, rgba(5,7,11,.62) 34%, rgba(5,7,11,.15) 62%, transparent 100%);
}
.brand-apex {
  display: block; font-family: 'SairaCond'; font-weight: 800; font-size: clamp(46px, 13vw, 92px);
  line-height: .82; letter-spacing: -.02em;
  background: linear-gradient(180deg, #fff 10%, #cfd8e3 55%, #8b98a8 100%);
  -webkit-background-clip: text; background-clip: text; color: transparent;
  filter: drop-shadow(0 10px 30px rgba(0,0,0,.55));
}
.brand-drive {
  display: block; font-family: 'SairaCond'; font-weight: 700; font-size: clamp(20px, 5.4vw, 34px);
  letter-spacing: .42em; color: var(--amber); margin-top: 2px; text-indent: .1em;
  text-shadow: 0 0 30px rgba(255,138,61,.45);
}
.brand-sub { margin-top: 12px; font-size: 10px; letter-spacing: .28em; color: var(--muted); font-weight: 700; }
.brand-sub i { color: var(--amber); font-style: normal; }

.garage { margin-top: auto; margin-bottom: 18px; }
.garage-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 10px; }
.garage-head span { font-size: 9.5px; letter-spacing: .22em; color: var(--muted); font-weight: 700; }
.garage-head i { font-style: normal; font-family: 'SairaCond'; font-size: 13px; letter-spacing: .1em; color: var(--amber-soft); }
.swatches { display: flex; gap: 10px; flex-wrap: wrap; }
.sw {
  width: 38px; height: 38px; border-radius: 50%; position: relative; cursor: pointer;
  border: 1px solid rgba(255,255,255,.18);
  box-shadow: inset 0 1px 0 rgba(255,255,255,.35), 0 6px 16px rgba(0,0,0,.45);
  transition: transform .18s ease;
}
.sw:hover { transform: translateY(-2px); }
.sw.on::after {
  content: ''; position: absolute; inset: -5px; border-radius: 50%;
  border: 1.5px solid var(--amber); box-shadow: 0 0 18px rgba(255,138,61,.5);
}
.paints { display: flex; gap: 6px; margin-top: 12px; }
.paint {
  padding: 7px 13px; border-radius: 99px; font-size: 9.5px; letter-spacing: .16em; font-weight: 800;
  color: var(--muted); background: rgba(255,255,255,.05); border: 1px solid var(--line);
  cursor: pointer; transition: all .18s ease;
}
.paint.on { color: #10141a; background: #e9eef5; border-color: #e9eef5; }

.cta {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  width: 100%; padding: 18px 22px; border: none; border-radius: 16px; cursor: pointer;
  font-family: 'SairaCond'; font-weight: 800; font-size: 17px; letter-spacing: .16em;
  color: #170c04; background: linear-gradient(135deg, #ffc074, #ff7a26 60%, #f4560f);
  box-shadow: 0 16px 40px rgba(255,110,20,.34), inset 0 1px 0 rgba(255,255,255,.45);
  transition: transform .18s cubic-bezier(.2,.8,.3,1.2), box-shadow .18s ease;
}
.cta:hover { transform: translateY(-2px); box-shadow: 0 22px 50px rgba(255,110,20,.44), inset 0 1px 0 rgba(255,255,255,.45); }
.cta:active { transform: translateY(0) scale(.99); }
.cta svg { width: 20px; height: 20px; fill: #170c04; }
.cta.small { width: auto; padding: 16px 30px; font-size: 15px; }
.ghost {
  padding: 16px 22px; border-radius: 14px; background: transparent; color: var(--text);
  border: 1px solid var(--line-strong); font-weight: 700; font-size: 11px; letter-spacing: .16em;
  cursor: pointer; transition: all .18s ease;
}
.ghost:hover { background: rgba(255,255,255,.07); }
.hints { display: flex; gap: 18px; justify-content: center; margin-top: 16px; font-size: 9.5px; letter-spacing: .13em; color: var(--muted); }
.hints b { color: var(--text); font-weight: 800; }
.fineprint { text-align: center; margin-top: 10px; font-size: 9px; letter-spacing: .16em; color: rgba(150,163,179,.55); }

/* -------------------------------- results -------------------------------- */
#finish, #pause { justify-content: center; align-items: center; background: rgba(4,6,10,.72); backdrop-filter: blur(8px); }
.result-card {
  width: min(430px, 100%); padding: 26px 24px; border-radius: 22px;
  background: linear-gradient(180deg, rgba(20,26,34,.92), rgba(9,12,17,.96));
  border: 1px solid rgba(255,255,255,.12); box-shadow: 0 30px 80px rgba(0,0,0,.6);
  animation: rise .45s cubic-bezier(.2,.8,.3,1);
}
.result-card.narrow { width: min(340px, 100%); }
@keyframes rise { from { transform: translateY(16px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
.res-kicker { font-size: 9.5px; letter-spacing: .28em; color: var(--amber); font-weight: 800; }
.res-total { font-family: 'SairaCond'; font-weight: 800; font-size: 54px; line-height: 1; margin: 8px 0 18px; }
.res-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; padding: 14px 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.res-grid span { display: block; font-size: 8.5px; letter-spacing: .16em; color: var(--muted); font-weight: 700; margin-bottom: 5px; }
.res-grid b { font-family: 'SairaCond'; font-size: 19px; font-weight: 700; }
.res-laps { display: grid; gap: 6px; margin: 16px 0 20px; max-height: 120px; overflow: auto; }
.res-laps div { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); letter-spacing: .06em; }
.res-laps b { color: var(--text); font-family: 'SairaCond'; font-size: 14px; }
.res-actions { display: flex; gap: 10px; justify-content: flex-end; align-items: center; }
.res-actions.col { flex-direction: column; align-items: stretch; margin-top: 22px; }

/* -------------------------------- loading -------------------------------- */
#loading {
  position: fixed; inset: 0; z-index: 40; display: grid; place-items: center; gap: 14px;
  background: #05070b; transition: opacity .6s ease; font-size: 9.5px; letter-spacing: .3em;
  color: var(--muted); font-weight: 700; align-content: center;
}
#loading.done { opacity: 0; pointer-events: none; }
.spin {
  width: 26px; height: 26px; border-radius: 50%;
  border: 2px solid rgba(255,255,255,.12); border-top-color: var(--amber);
  animation: sp .8s linear infinite; margin: 0 auto 12px;
}
@keyframes sp { to { transform: rotate(360deg); } }

/* ------------------------------ responsive ------------------------------ */
@media (max-width: 560px) {
  .lapbox { min-width: 148px; padding: 9px 12px 10px; }
  .lap-row .v { font-size: 25px; }
  .hud-bottom { padding: 0 14px; }
  .speedo { width: 156px; height: 96px; }
  .arc { width: 156px; height: 86px; }
  .speedval #kmh { font-size: 48px; }
  .nitro { margin-bottom: 12px; }
  .pedals { right: 14px; bottom: calc(96px + var(--safe-b)); }
  .pedal { width: 60px; height: 60px; font-size: 9px; }
  #minimap { width: 78px; height: 78px; }
  .hints { display: none; }
}
@media (min-width: 900px) {
  .screen { padding: 40px 48px; }
  .garage { width: 420px; }
  .cta { width: 340px; align-self: flex-start; }
  .hints { justify-content: flex-start; }
  .fineprint { text-align: left; }
}
/* on-screen pedals are for touch only — a wide tablet still needs them */
@media (hover: hover) and (pointer: fine) {
  .pedals { display: none; }
}
@media (max-height: 700px) {
  .brand-apex { font-size: clamp(38px, 9vw, 62px); }
  .garage { margin-bottom: 12px; }
  .sw { width: 32px; height: 32px; }
}

/* input hints — touch vs keyboard */
.m-desk { display: none; }
@media (min-width: 900px) {
  .m-touch { display: none; }
  .m-desk { display: block; }
}
