:root {
  --bg: #120e0b; --panel: rgba(20, 15, 11, 0.86); --panel2: rgba(34, 26, 20, 0.94); --line: rgba(255, 255, 255, 0.1);
  --text: #f4efe9; --muted: #b3a597; --red: #ff6a00; --yellow: #ffd21f; --green: #2ecc71; --purple: #b14cff;
  --font: "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --safe-l: env(safe-area-inset-left, 0px); --safe-r: env(safe-area-inset-right, 0px); --safe-b: env(safe-area-inset-bottom, 0px); --safe-t: env(safe-area-inset-top, 0px);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-family: var(--font); overflow: hidden; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; }
body { height: 100dvh; }
canvas#c { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
button { font-family: inherit; color: inherit; cursor: pointer; }

/* ---------- menus ---------- */
#ui { position: fixed; inset: 0; pointer-events: none; z-index: 10; }
#ui > .screen { position: absolute; inset: 0; pointer-events: auto; display: flex; overflow: hidden; }
.shade { background: linear-gradient(90deg, rgba(14, 10, 8, 0.95) 0%, rgba(14, 10, 8, 0.75) 42%, rgba(14, 10, 8, 0) 75%); }
.dim { background: rgba(6, 7, 9, 0.72); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); }
.col { display: flex; flex-direction: column; gap: 10px; padding: calc(28px + var(--safe-t)) 28px calc(24px + var(--safe-b)) calc(36px + var(--safe-l)); width: min(440px, 100%); overflow-y: auto; }
.logo { font-weight: 900; font-style: italic; letter-spacing: -1px; font-size: 64px; line-height: 0.9; margin: 0; text-transform: uppercase; }
.logo b { color: var(--red); }
.tag { color: var(--muted); margin: 0 0 14px; font-size: 15px; letter-spacing: 0.5px; }
h2 { font-style: italic; font-weight: 900; text-transform: uppercase; font-size: 30px; margin: 0; letter-spacing: -0.5px; }
h3 { font-size: 12px; text-transform: uppercase; letter-spacing: 2px; color: var(--muted); margin: 14px 0 4px; font-weight: 700; }
.btn { display: flex; align-items: center; justify-content: space-between; gap: 12px; background: var(--panel2); border: 1px solid var(--line); border-left: 4px solid transparent; padding: 12px 16px; min-height: 48px; font-size: 17px; font-weight: 800; font-style: italic; text-transform: uppercase; letter-spacing: 0.5px; text-align: left; transition: background .12s, border-color .12s, transform .12s; }
.btn small { font-weight: 500; font-style: normal; text-transform: none; color: var(--muted); font-size: 13px; letter-spacing: 0; }
.btn:hover, .btn:focus-visible { background: #20242c; border-left-color: var(--red); outline: none; transform: translateX(3px); }
.btn.primary { background: var(--red); border-color: var(--red); color: #fff; justify-content: center; font-size: 20px; }
.btn.primary:hover, .btn.primary:focus-visible { background: #ff8a2e; border-left-color: #fff; }
.btn.ghost { background: transparent; }
.btn.small { min-height: 44px; padding: 8px 14px; font-size: 14px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row > .btn { flex: 1; justify-content: center; }
.note { color: var(--muted); font-size: 13px; line-height: 1.4; }
.chip { position: absolute; top: calc(12px + var(--safe-t)); right: calc(14px + var(--safe-r)); z-index: 3; pointer-events: auto; background: var(--panel2); border: 1px solid var(--line); border-radius: 22px; padding: 6px 14px 6px 6px; display: flex; align-items: center; gap: 8px; min-height: 44px; font-size: 14px; font-weight: 700; }
.chip img, .chip .av { width: 32px; height: 32px; border-radius: 50%; background: #333; object-fit: cover; display: grid; place-items: center; font-size: 14px; }

/* option pickers */
.opt { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 6px 0; border-bottom: 1px solid var(--line); min-height: 50px; }
.opt > span { font-size: 15px; font-weight: 600; }
.opt > span small { display: block; color: var(--muted); font-weight: 400; font-size: 12px; }
.seg { display: flex; background: #000a; border: 1px solid var(--line); flex-wrap: wrap; }
.seg button { background: none; border: 0; padding: 0 12px; min-height: 44px; min-width: 44px; font-weight: 700; font-size: 14px; color: var(--muted); }
.seg button.on { background: var(--red); color: #fff; }
.swatches { display: grid; grid-template-columns: repeat(9, 30px); gap: 6px; }
.swatches button { width: 30px; height: 30px; border-radius: 4px; border: 2px solid rgba(255,255,255,0.15); padding: 0; position: relative; }
.swatches button::after { content: ''; position: absolute; inset: -8px; }
.swatches button.on { border-color: #fff; box-shadow: 0 0 0 2px var(--red); }
input[type=text], input[type=number] { background: #000a; border: 1px solid var(--line); color: var(--text); font: inherit; font-weight: 700; padding: 10px 12px; min-height: 44px; width: 170px; }
input[type=range] { width: 170px; accent-color: var(--red); min-height: 44px; }

/* circuit select */
.circuits { flex: 1; display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px; padding: calc(24px + var(--safe-t)) 18px 24px; overflow-y: auto; align-content: start; }
.card { background: var(--panel2); border: 1px solid var(--line); border-top: 3px solid transparent; padding: 10px 12px 12px; text-align: left; display: flex; flex-direction: column; gap: 4px; min-height: 170px; transition: transform .12s, border-color .12s; }
.card:hover, .card:focus-visible { transform: translateY(-2px); outline: none; border-top-color: #fff3; }
.card.on { border-top-color: var(--red); background: #1d2128; }
.card svg { width: 100%; height: 92px; }
.card svg path { fill: none; stroke: #fff; stroke-width: 2.4; stroke-linejoin: round; }
.card.on svg path { stroke: var(--red); }
.card b { font-style: italic; font-size: 17px; text-transform: uppercase; }
.card span { color: var(--muted); font-size: 12px; }
.card em { color: var(--yellow); font-style: normal; font-size: 12px; font-weight: 700; }
.side { width: min(360px, 100%); background: var(--panel); border-left: 1px solid var(--line); display: flex; flex-direction: column; gap: 6px; padding: calc(22px + var(--safe-t)) calc(20px + var(--safe-r)) calc(20px + var(--safe-b)) 20px; overflow-y: auto; }
.side .grow { flex: 1; }

/* garage */
.tabs { display: flex; gap: 4px; margin: 6px 0 4px; }
.tabs button { flex: 1; min-height: 44px; background: #000a; border: 1px solid var(--line); font-weight: 800; font-style: italic; text-transform: uppercase; color: var(--muted); }
.tabs button.on { background: var(--red); color: #fff; border-color: var(--red); }

/* results */
.results { margin: auto; width: min(760px, 96%); max-height: 96%; background: var(--panel); border: 1px solid var(--line); display: flex; flex-direction: column; padding: 18px 20px; gap: 8px; }
.tbl { overflow-y: auto; flex: 1; min-height: 0; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
td, th { padding: 5px 8px; text-align: left; white-space: nowrap; }
th { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 1px; font-weight: 700; }
tr:nth-child(even) td { background: #ffffff08; }
tr.me td { background: rgba(225, 6, 0, 0.28); font-weight: 800; }
td.pos { font-weight: 900; font-style: italic; width: 34px; }
td.num { color: var(--muted); width: 34px; }
td.t { font-variant-numeric: tabular-nums; text-align: right; }
.big { font-size: 40px; font-weight: 900; font-style: italic; line-height: 1; }
.medal { color: var(--yellow); }
.lb { background: #000a; border: 1px solid var(--line); padding: 8px 12px; font-size: 14px; }
.lb ol { margin: 4px 0 0; padding-left: 22px; } .lb li { padding: 1px 0; } .lb li.me { color: var(--yellow); font-weight: 800; }

/* loading */
.loading { margin: auto; text-align: center; width: min(420px, 90%); }
.bar { height: 4px; background: #ffffff20; margin-top: 16px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--red); transition: width .25s; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 5; font-variant-numeric: tabular-nums; }
#hLeft { position: absolute; left: calc(14px + var(--safe-l)); top: calc(12px + var(--safe-t)); }
#hPos { font-size: 54px; font-weight: 900; font-style: italic; line-height: 0.9; text-shadow: 0 3px 12px #000a; }
#hPos small { font-size: 22px; opacity: 0.8; }
#hLap { font-size: 14px; font-weight: 800; letter-spacing: 2px; background: var(--red); color: #fff; display: inline-block; padding: 2px 10px; margin-top: 4px; }
#tower { margin-top: 8px; font-size: 13px; font-weight: 700; }
#tower div { display: flex; align-items: center; gap: 6px; background: var(--panel); padding: 2px 8px; width: 104px; }
#tower div.me { background: #fff; color: #000; }
#tower b { width: 14px; font-style: italic; } #tower i { width: 4px; height: 12px; }
#hTop { position: absolute; top: calc(12px + var(--safe-t)); left: 50%; transform: translateX(-50%); text-align: center; background: var(--panel); padding: 4px 16px 6px; min-width: 150px; }
#hTime { font-size: 28px; font-weight: 800; } #hBest { font-size: 12px; color: var(--yellow); font-weight: 700; }
#hCoins { position: absolute; top: calc(12px + var(--safe-t)); right: calc(196px + var(--safe-r)); font-size: 24px; font-weight: 900; color: var(--yellow); background: var(--panel); padding: 4px 12px; border-radius: 22px; }
#hCoins.bump { animation: bump .25s ease-out; }
@keyframes bump { 50% { transform: scale(1.18); } }
#map { position: absolute; right: calc(12px + var(--safe-r)); top: calc(8px + var(--safe-t)); width: 172px; height: 172px; }
#speedo { position: absolute; right: calc(18px + var(--safe-r)); bottom: calc(16px + var(--safe-b)); text-align: right; background: var(--panel); padding: 8px 16px 10px; border-bottom: 3px solid var(--red); min-width: 170px; }
#speedo b { font-size: 56px; font-weight: 900; font-style: italic; line-height: 0.9; display: block; }
#speedo span { font-size: 12px; color: var(--muted); font-weight: 700; text-transform: uppercase; }
#drift { margin-top: 6px; display: flex; align-items: center; gap: 6px; opacity: 0.35; }
#drift span { font-size: 11px; font-weight: 900; letter-spacing: 2px; color: #fff !important; }
#drift i { flex: 1; height: 8px; background: #ffffff25; display: block; overflow: hidden; }
#drift u { display: block; height: 100%; width: 0; background: var(--yellow); text-decoration: none; }
#drift.on { opacity: 1; } #drift.full u { background: #4ad6ff; box-shadow: 0 0 10px #4ad6ff; }
#pop { position: absolute; left: 50%; top: 24%; transform: translateX(-50%); text-align: center; opacity: 0; }
#pop.show { opacity: 1; } #pop.anim { animation: popin .35s cubic-bezier(.2, 1.6, .4, 1) both; }
#pop b { display: block; font-size: 48px; font-weight: 900; font-style: italic; color: #fff; text-shadow: 0 3px 0 #000, 0 0 24px #ff6a00; white-space: nowrap; }
#pop.flip b { color: var(--yellow); } #pop.drift b { color: #4ad6ff; text-shadow: 0 3px 0 #000, 0 0 24px #4ad6ff; }
#pop span { font-size: 18px; font-weight: 800; color: var(--yellow); text-shadow: 0 2px 6px #000; }
@keyframes popin { from { transform: translateX(-50%) scale(0.4); opacity: 0; } to { transform: translateX(-50%) scale(1); opacity: 1; } }
#msg { position: absolute; top: 44%; left: 50%; transform: translate(-50%, -50%); text-align: center; white-space: nowrap; }
#msg span { font-weight: 900; font-style: italic; text-transform: uppercase; font-size: 26px; text-shadow: 0 2px 10px #000; }
#msg.count span { font-size: 110px; color: #fff; text-shadow: 0 6px 0 #000, 0 0 40px #ff6a00; }
#msg.tip span { font-size: 20px; background: rgba(0,0,0,0.72); padding: 8px 18px; border-left: 4px solid var(--yellow); text-transform: none; font-style: normal; font-weight: 800; }
#msg.warn span { background: var(--red); padding: 4px 16px; }
#msg.small span { font-size: 18px; }
#fade { position: fixed; inset: 0; background: #000; opacity: 0; pointer-events: none; z-index: 6; }
#rotate { position: absolute; inset: 0; background: #000e; display: grid; place-items: center; font-size: 22px; font-weight: 800; text-align: center; padding: 30px; pointer-events: auto; }

/* ---------- touch ---------- */
#touch { position: fixed; inset: 0; z-index: 6; touch-action: none; pointer-events: none; }
#touch button { pointer-events: auto; }
.tb { position: absolute; border-radius: 50%; border: 3px solid #ffffff70; background: #0007; font-weight: 900; font-size: 22px; display: grid; place-items: center; line-height: 1; }
.tb small { display: block; font-size: 10px; font-weight: 700; margin-top: 4px; }
.tb.on { background: #fff5; }
.tb.steer { width: 92px; height: 92px; bottom: calc(22px + var(--safe-b)); font-size: 34px; }
#tL { left: calc(18px + var(--safe-l)); } #tR { left: calc(126px + var(--safe-l)); }
#tGas { width: 116px; height: 116px; right: calc(18px + var(--safe-r)); bottom: calc(20px + var(--safe-b)); border-color: #4ade80c0; font-style: italic; }
#tBrk { width: 92px; height: 92px; right: calc(148px + var(--safe-r)); bottom: calc(20px + var(--safe-b)); border-color: #ff6a00c0; }
#tDrift { width: 78px; height: 78px; right: calc(36px + var(--safe-r)); bottom: calc(150px + var(--safe-b)); font-size: 14px; border-color: #4ad6ffc0; }
.ts { position: absolute; width: 48px; height: 48px; border-radius: 10px; border: 1px solid #ffffff50; background: #0007; font-weight: 800; font-size: 14px; pointer-events: auto; }
#tPause { top: calc(190px + var(--safe-t)); right: calc(14px + var(--safe-r)); } #tCam { top: calc(190px + var(--safe-t)); right: calc(70px + var(--safe-r)); }
body.touch #speedo { bottom: auto; top: calc(186px + var(--safe-t)); right: calc(128px + var(--safe-r)); min-width: 120px; padding: 4px 10px; } body.touch #speedo b { font-size: 34px; }

/* small and short screens */
@media (max-height: 560px) {
  .logo { font-size: 46px; } .tag { margin-bottom: 6px; }
  .col { gap: 7px; padding-top: calc(16px + var(--safe-t)); }
  .btn { min-height: 44px; padding: 8px 14px; font-size: 15px; }
  #hPos { font-size: 38px; } #tower { display: none; }
  #map { width: 120px; height: 120px; }
  #hTime { font-size: 22px; } #hTop { padding: 3px 12px; min-width: 120px; }
  #hCoins { right: calc(140px + var(--safe-r)); font-size: 18px; } #pop b { font-size: 34px; } #msg.count span { font-size: 80px; }
  .tb.steer { width: 80px; height: 80px; } #tR { left: calc(110px + var(--safe-l)); } #tGas { width: 100px; height: 100px; } #tBrk { width: 80px; height: 80px; right: calc(130px + var(--safe-r)); } #tDrift { bottom: calc(130px + var(--safe-b)); }
  body.touch #speedo { top: calc(136px + var(--safe-t)); }
  #tPause, #tCam { top: calc(140px + var(--safe-t)); }
  .circuits { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
  .card { min-height: 140px; } .card svg { height: 64px; }
}
@media (max-width: 760px) {
  #ui > .screen.split { flex-direction: column; overflow-y: auto; }
  .side { width: 100%; border-left: 0; border-top: 1px solid var(--line); }
  .circuits { flex: none; overflow: visible; grid-template-columns: repeat(2, 1fr); }
  .shade { background: linear-gradient(180deg, rgba(8, 9, 12, 0.9) 0%, rgba(8, 9, 12, 0.7) 60%, rgba(8, 9, 12, 0.4) 100%); }
  .col { width: 100%; padding-left: calc(20px + var(--safe-l)); }
  .logo { font-size: 48px; }
  .swatches { grid-template-columns: repeat(6, 30px); }
}
@media (max-width: 520px) { #tower { display: none; } }

.paints { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; }
.paint { background: var(--panel2); border: 2px solid var(--line); padding: 8px; display: flex; flex-direction: column; gap: 6px; text-align: left; min-height: 44px; }
.paint.on { border-color: var(--yellow); }
.paint i { display: flex; height: 26px; } .paint i u { flex: 1; text-decoration: none; }
.paint b { font-size: 13px; } .paint small { color: var(--muted); font-size: 12px; }
/* coin icon (HUD, store, results) */
i.coin { display: inline-block; width: 0.82em; height: 0.82em; border-radius: 50%; vertical-align: -0.1em; margin-right: 0.28em; background: radial-gradient(circle at 35% 30%, #fff6b0 0 12%, #ffd21f 13% 52%, #e09b00 53% 62%, #ffcf2a 63% 100%); box-shadow: inset 0 -2px 0 #b97800, 0 1px 3px #0008; }
#hCoins { display: flex; align-items: center; }
/* store: upgrade cards */
.wallet { font-size: 22px; font-weight: 900; color: var(--yellow); }
.shop { display: grid; gap: 8px; }
.shop .item { display: grid; grid-template-columns: 1fr auto; grid-template-areas: "name btn" "desc btn" "pips btn"; column-gap: 14px; row-gap: 2px; align-items: center; background: #0009; border: 1px solid var(--line); border-left: 4px solid var(--red); padding: 10px 12px; }
.shop .item b { grid-area: name; font-size: 17px; font-style: italic; text-transform: uppercase; letter-spacing: .5px; }
.shop .item b small { font-size: 11px; font-style: normal; color: var(--muted); letter-spacing: 1px; margin-left: 6px; }
.shop .item p { grid-area: desc; margin: 0; font-size: 13px; color: var(--muted); }
.shop .item .pips { grid-area: pips; display: flex; gap: 4px; margin-top: 4px; }
.shop .item .pips i { width: 26px; height: 6px; background: #fff2; }
.shop .item .pips i.on { background: var(--red); box-shadow: 0 0 6px var(--red); }
.shop .item .btn { grid-area: btn; min-width: 96px; margin: 0; }
/* tips and small messages sit under the timer, clear of the rider; the countdown stays in the middle */
#msg.tip, #msg.small, #msg.warn { top: calc(84px + var(--safe-t)); transform: translate(-50%, 0); }

/* Saved progress line (R23): where coins and upgrades are kept, with a Sign in link when signed out. */
.saveline { margin: 10px 0 0; }
.saveline .linkish { background: none; border: 0; padding: 6px 2px; margin: -6px 0; font: inherit; font-weight: 700; color: #ff8a2b; text-decoration: underline; text-underline-offset: 3px; cursor: pointer; }
.touch .saveline .linkish { padding: 12px 4px; margin: -12px 0; }
