:root {
  --bg: #0b0d10; --panel: rgba(12, 14, 18, 0.84); --panel2: rgba(22, 25, 31, 0.92); --line: rgba(255, 255, 255, 0.1);
  --text: #f2f2f2; --muted: #9aa3ad; --red: #e10600; --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(8, 9, 12, 0.94) 0%, rgba(8, 9, 12, 0.75) 42%, rgba(8, 9, 12, 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: #ff1a10; 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; }
#tower { position: absolute; left: calc(12px + var(--safe-l)); top: calc(12px + var(--safe-t)); width: 190px; font-size: 13px; }
#tower .pos { background: var(--red); color: #fff; font-weight: 900; font-style: italic; font-size: 26px; padding: 2px 10px; display: flex; justify-content: space-between; align-items: baseline; }
#tower .pos small { font-size: 14px; opacity: 0.85; }
#tower .r { display: grid; grid-template-columns: 22px 6px 1fr auto; gap: 6px; align-items: center; background: var(--panel); padding: 2px 8px; height: 22px; }
#tower .r:nth-child(odd) { background: rgba(20, 23, 28, 0.84); }
#tower .r i { width: 4px; height: 14px; display: block; }
#tower .r b { font-style: italic; }
#tower .r em { font-style: normal; color: var(--muted); font-size: 12px; }
#tower .r.me { background: #fff; color: #000; } #tower .r.me em { color: #333; }
#hudTop { position: absolute; top: calc(10px + var(--safe-t)); left: 50%; transform: translateX(-50%); text-align: center; background: var(--panel); padding: 6px 18px 8px; min-width: 210px; }
#hLap { font-size: 12px; letter-spacing: 2px; color: var(--muted); font-weight: 700; }
#hTime { font-size: 30px; font-weight: 800; line-height: 1.05; }
#hTime.bad { color: #ff6b5e; text-decoration: line-through; text-decoration-thickness: 2px; }
#hSub { font-size: 13px; color: var(--muted); }
.coin { display: inline-block; width: 0.9em; height: 0.9em; border-radius: 50%; background: radial-gradient(circle at 35% 30%, #fff3b0, #ffc21a 45%, #b07800); box-shadow: inset 0 0 0 2px #e0a000; vertical-align: -0.12em; margin-right: 5px; }
#hCoins { font-size: 16px; font-weight: 800; color: #ffd21f; margin-top: 2px; }
#hCoins.pop b { color: #fff; }
.shop { display: grid; gap: 8px; }
.item { background: var(--panel2); border: 1px solid var(--line); padding: 10px 14px; display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; align-items: center; }
.item b { font-style: italic; text-transform: uppercase; font-size: 17px; }
.item p { margin: 0; color: var(--muted); font-size: 13px; grid-column: 1; }
.pips { display: flex; gap: 4px; grid-column: 1; } .pips i { width: 26px; height: 6px; background: #ffffff20; } .pips i.on { background: var(--yellow); }
.item .btn { grid-row: 1 / span 3; grid-column: 2; min-width: 120px; justify-content: center; }
.item .btn[disabled] { opacity: 0.45; cursor: default; transform: none; }
.wallet { font-size: 22px; font-weight: 900; color: var(--yellow); }
#hSub .g { color: var(--green); } #hSub .r { color: #ff6b5e; } #hSub .p { color: var(--purple); }
#map { position: absolute; right: calc(12px + var(--safe-r)); top: calc(10px + var(--safe-t)); width: 170px; height: 170px; }
#dash { position: absolute; right: calc(16px + var(--safe-r)); bottom: calc(16px + var(--safe-b)); width: 250px; background: var(--panel); padding: 10px 14px 10px; border-bottom: 3px solid var(--red); }
#revs { height: 12px; display: grid; grid-template-columns: repeat(18, 1fr); gap: 2px; margin-bottom: 4px; }
#revs i { background: #ffffff18; }
#revs i.on { background: var(--green); } #revs i.on.y { background: var(--yellow); } #revs i.on.r { background: var(--red); }
#revs.flash i.on { background: #4aa3ff; }
#dashRow { display: flex; align-items: flex-end; justify-content: space-between; }
#gear { font-size: 54px; font-weight: 900; font-style: italic; line-height: 0.9; width: 56px; }
#speed { text-align: right; } #speed b { font-size: 50px; font-weight: 900; font-style: italic; line-height: 0.9; } #speed span { display: block; font-size: 12px; color: var(--muted); font-weight: 700; text-transform: uppercase; }
#dashLow { display: flex; justify-content: space-between; align-items: center; font-size: 13px; margin-top: 4px; color: var(--muted); }
#leanA { font-weight: 800; color: var(--text); }
#aids i { font-style: normal; font-weight: 800; margin-left: 6px; padding: 0 4px; color: #ffffff40; border: 1px solid #ffffff25; font-size: 11px; }
#aids i.on { color: #000; background: var(--yellow); border-color: var(--yellow); }
#lights { position: absolute; top: 18%; left: 50%; transform: translateX(-50%); display: flex; gap: 10px; background: #000c; padding: 12px 16px; border-radius: 6px; }
#lights i { width: 38px; height: 38px; border-radius: 50%; background: #2a0505; box-shadow: inset 0 0 6px #000; }
#lights i.on { background: radial-gradient(circle at 40% 35%, #ff8a75, #ff1a0a 55%, #9a0800); box-shadow: 0 0 22px #ff1a0a; }
#msg { position: absolute; top: 30%; left: 50%; transform: translateX(-50%); text-align: center; font-weight: 900; font-style: italic; text-transform: uppercase; font-size: 34px; text-shadow: 0 2px 10px #000a; white-space: nowrap; }
#msg.warn { color: #ffd0cc; } #msg.warn span { background: var(--red); padding: 2px 14px; text-shadow: none; }
#msg.small { font-size: 20px; }
#corner { position: absolute; left: 50%; top: calc(86px + var(--safe-t)); transform: translateX(-50%); font-size: 13px; letter-spacing: 3px; text-transform: uppercase; color: #fff; opacity: 0; transition: opacity .4s; background: #0008; padding: 3px 12px; }
#corner.show { opacity: 0.9; }
#split { position: absolute; left: 50%; top: calc(112px + var(--safe-t)); transform: translateX(-50%); font-size: 18px; font-weight: 800; padding: 2px 12px; opacity: 0; transition: opacity .3s; }
#split.show { opacity: 1; } #split.g { background: var(--green); color: #000; } #split.r { background: var(--red); color: #fff; } #split.p { background: var(--purple); color: #fff; }
#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; }
#tSteer { position: absolute; left: 0; bottom: 0; width: 46%; height: 62%; }
#tTrack { position: absolute; left: calc(30px + var(--safe-l)); bottom: calc(46px + var(--safe-b)); width: 170px; height: 6px; background: #ffffff30; border-radius: 3px; }
#tKnob { position: absolute; left: calc(30px + 85px - 30px + var(--safe-l)); bottom: calc(46px - 27px + var(--safe-b)); width: 60px; height: 60px; border-radius: 50%; background: #ffffff35; border: 2px solid #ffffffa0; }
#tSteer span { position: absolute; left: calc(30px + var(--safe-l)); bottom: calc(96px + var(--safe-b)); font-size: 12px; color: #ffffff90; text-transform: uppercase; letter-spacing: 2px; }
.tb { position: absolute; bottom: calc(18px + var(--safe-b)); width: 112px; height: 112px; border-radius: 50%; border: 3px solid #ffffff70; background: #0006; font-weight: 900; font-style: italic; text-transform: uppercase; font-size: 15px; }
.tb.on { background: #fff4; }
#tThr { right: calc(18px + var(--safe-r)); border-color: #2ecc71c0; }
#tBrk { right: calc(142px + var(--safe-r)); border-color: #e10600c0; width: 96px; height: 96px; }
.ts { position: absolute; width: 48px; height: 48px; border-radius: 10px; border: 1px solid #ffffff50; background: #0007; font-weight: 800; font-size: 14px; }
#tPause { top: calc(196px + var(--safe-t)); right: calc(14px + var(--safe-r)); }
#tCam { top: calc(196px + var(--safe-t)); right: calc(70px + var(--safe-r)); }
#tDn { bottom: calc(140px + var(--safe-b)); right: calc(142px + var(--safe-r)); font-size: 22px; }
#tUp { bottom: calc(140px + var(--safe-b)); right: calc(30px + var(--safe-r)); font-size: 22px; }
body.touch #dash { bottom: auto; top: calc(186px + var(--safe-t)); right: calc(128px + var(--safe-r)); width: 200px; padding: 6px 10px; }
body.touch #gear { font-size: 40px; } body.touch #speed b { font-size: 36px; }

/* 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; }
  #tower { width: 160px; font-size: 12px; } #tower .r { height: 19px; } #tower .pos { font-size: 20px; }
  #map { width: 120px; height: 120px; }
  #hTime { font-size: 22px; } #hudTop { padding: 4px 12px; min-width: 160px; }
  body.touch #dash { 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 { width: 150px; } #tower .r:nth-child(n+6) { display: none; } #tower .r.me { display: grid; } }
