:root {
  --bg: #0e1320; --panel: rgba(14, 20, 34, 0.82); --panel2: rgba(24, 33, 54, 0.9); --line: rgba(120, 170, 255, 0.18);
  --txt: #eaf2ff; --mut: #8ea3c4; --acc: #47c6ff; --acc2: #ff7a1a; --good: #3cff9a; --bad: #ff4f6d; --gold: #ffc23a;
  --font: 'Rajdhani', 'Segoe UI', system-ui, sans-serif; --font2: 'Inter', 'Segoe UI', system-ui, sans-serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--txt); font-family: var(--font2); user-select: none; -webkit-user-select: none; touch-action: none; }
#game { position: fixed; inset: 0; width: 100vw; height: 100vh; display: block; }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }

/* ---------- generic UI ---------- */
.ui { position: fixed; inset: 0; z-index: 10; display: flex; flex-direction: column; padding: max(16px, env(safe-area-inset-top)) max(18px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-left)); overflow: hidden; }
.ui.hidden { display: none; }
.ui.screen-main, .ui.screen-quick, .ui.screen-timetrial, .ui.screen-career, .ui.screen-garage, .ui.screen-dealer, .ui.screen-settings, .ui.screen-picker, .ui.screen-event, .ui.screen-starter {
  background: linear-gradient(90deg, rgba(8, 12, 22, 0.88) 0%, rgba(8, 12, 22, 0.55) 48%, rgba(8, 12, 22, 0.15) 100%);
}
.top { display: flex; align-items: center; gap: 18px; margin-bottom: 14px; min-height: 44px; }
.top h2 { margin: 0; font-family: var(--font); font-weight: 700; font-size: 30px; letter-spacing: 0.04em; text-transform: uppercase; flex: 1; transform: skewX(-8deg); }
.top .back { font-family: var(--font); font-size: 18px; font-weight: 700; padding: 8px 16px; background: var(--panel2); border: 1px solid var(--line); border-radius: 6px; transform: skewX(-12deg); }
.top .back:hover { border-color: var(--acc); }
.wallet { margin-left: auto; font-family: var(--font); font-weight: 700; font-size: 20px; color: var(--gold); background: rgba(0, 0, 0, 0.35); padding: 6px 14px; border-radius: 6px; border: 1px solid rgba(255, 194, 58, 0.3); }
.logo { display: inline-flex; flex-direction: column; line-height: 0.85; font-family: var(--font); font-weight: 700; transform: skewX(-10deg); }
.logo .l1 { font-size: 64px; color: var(--acc); letter-spacing: 0.1em; text-shadow: 3px 3px 0 #000; }
.logo .l2 { font-size: 92px; letter-spacing: 0.05em; color: #fff; text-shadow: 4px 4px 0 #000, 0 0 30px rgba(71, 198, 255, 0.4); }
.logo.small .l1 { font-size: 20px; } .logo.small .l2 { font-size: 28px; text-shadow: 2px 2px 0 #000; }
.split { display: grid; grid-template-columns: minmax(320px, 47%) minmax(300px, 1fr); gap: 16px; flex: 1; min-height: 0; }
.split > .carpanel { align-self: end; max-height: 64vh; width: min(100%, 470px); justify-self: end; }
.split.one { grid-template-columns: minmax(300px, 820px); }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; overflow: auto; min-height: 0; backdrop-filter: blur(6px); scrollbar-width: thin; scrollbar-color: #2b3c60 transparent; }
.panel h3 { font-family: var(--font); font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; color: var(--acc); margin: 6px 0 10px; font-size: 16px; }
.carpanel { display: flex; flex-direction: column; gap: 8px; background: var(--panel); }
.hint { color: var(--mut); font-size: 13px; line-height: 1.45; }
.hint.warn { color: var(--gold); }
.btn { font-family: var(--font); font-weight: 700; font-size: 18px; letter-spacing: 0.05em; padding: 10px 18px; border-radius: 8px; background: var(--panel2); border: 1px solid var(--line); transition: transform 0.08s, border-color 0.15s, background 0.15s; text-transform: uppercase; }
.btn:hover { border-color: var(--acc); transform: translateY(-1px); }
.btn:active { transform: translateY(1px); }
.btn.go { background: linear-gradient(90deg, var(--acc2), #ff4f1a); border-color: transparent; color: #fff; font-size: 22px; padding: 14px; box-shadow: 0 6px 20px rgba(255, 90, 30, 0.35); margin-top: 6px; flex-shrink: 0; }
.btn.go:hover { filter: brightness(1.1); }
.btn.danger { background: rgba(255, 79, 109, 0.15); border-color: rgba(255, 79, 109, 0.5); }
.btn.disabled { filter: grayscale(1) brightness(0.6); }
.card { display: flex; align-items: center; gap: 12px; text-align: left; background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; transition: border-color 0.15s, transform 0.08s; width: 100%; }
.card:hover { border-color: var(--acc); }
.card.sel { border-color: var(--acc2); box-shadow: inset 0 0 0 1px var(--acc2), 0 0 18px rgba(255, 122, 26, 0.25); }
.card small { display: block; color: var(--mut); font-size: 12px; }
.card b { display: block; font-family: var(--font); font-size: 19px; font-weight: 700; }
.card em { margin-left: auto; font-style: normal; color: var(--gold); font-family: var(--font); font-weight: 700; white-space: nowrap; }

/* title */
.screen-title { align-items: center; justify-content: center; background: radial-gradient(circle at 50% 120%, rgba(71, 198, 255, 0.18), transparent 60%), linear-gradient(0deg, rgba(8, 12, 22, 0.9), rgba(8, 12, 22, 0.2)); cursor: pointer; }
.title-wrap { text-align: center; margin-top: 34vh; }
.tagline { font-family: var(--font); font-weight: 600; letter-spacing: 0.4em; color: var(--mut); margin-top: 10px; text-transform: uppercase; font-size: 15px; }
.press { margin-top: 40px; font-family: var(--font); font-size: 20px; letter-spacing: 0.15em; animation: blink 1.4s infinite; }
@keyframes blink { 50% { opacity: 0.25; } }

/* main */
.main-grid { display: grid; grid-template-columns: repeat(2, minmax(220px, 300px)); gap: 14px; margin-top: 4vh; }
.tile { position: relative; display: flex; flex-direction: column; align-items: flex-start; gap: 4px; text-align: left; padding: 18px 20px; min-height: 110px; background: linear-gradient(135deg, rgba(24, 33, 54, 0.95), rgba(14, 20, 34, 0.85)); border: 1px solid var(--line); border-radius: 14px; transition: transform 0.1s, border-color 0.15s; overflow: hidden; }
.tile::after { content: ''; position: absolute; right: -30px; top: -30px; width: 120px; height: 120px; background: radial-gradient(circle, rgba(71, 198, 255, 0.18), transparent 70%); }
.tile:hover { transform: translateY(-2px) skewX(-2deg); border-color: var(--acc); }
.tile .ti { font-size: 28px; }
.tile b { font-family: var(--font); font-size: 26px; text-transform: uppercase; letter-spacing: 0.03em; }
.tile small { color: var(--mut); font-size: 13px; }
.foot { margin-top: auto; color: var(--mut); font-size: 12px; letter-spacing: 0.05em; }

/* tracks */
.tracks { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.card.track { flex-direction: column; align-items: stretch; padding: 8px; gap: 2px; }
.card.track canvas { width: 100%; height: auto; background: radial-gradient(circle at 50% 40%, #1d2a44, #121a2b); border-radius: 6px; }
.tc-name { font-family: var(--font); font-weight: 700; font-size: 17px; }
.tc-sub { font-size: 11px; color: var(--mut); }
.opts { display: flex; flex-direction: column; gap: 8px; }
.opts.wide { max-width: 820px; }
.opt { display: flex; align-items: center; gap: 12px; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid rgba(255, 255, 255, 0.04); flex-wrap: wrap; }
.opt > span { color: var(--mut); font-weight: 600; font-size: 14px; }
.opt input[type=range] { flex: 1; max-width: 260px; accent-color: var(--acc); }
.opt input[type=text] { background: #0a0f1a; color: var(--txt); border: 1px solid var(--line); border-radius: 6px; padding: 6px 10px; font: inherit; }
.rv { width: 44px; text-align: right; font-family: var(--font); }
.seg { display: inline-flex; background: rgba(0, 0, 0, 0.35); border-radius: 8px; padding: 3px; gap: 2px; flex-wrap: wrap; }
.seg button { padding: 6px 12px; border-radius: 6px; font-family: var(--font); font-weight: 700; font-size: 15px; color: var(--mut); }
.seg button.sel { background: var(--acc); color: #04121c; }
.stepper { display: inline-flex; align-items: center; gap: 8px; }
.stepper button { width: 34px; height: 34px; border-radius: 8px; background: var(--panel2); border: 1px solid var(--line); font-size: 20px; font-weight: 700; }
.stepper b { min-width: 30px; text-align: center; font-family: var(--font); font-size: 22px; }
.carname small { color: var(--mut); font-size: 13px; }
.carname b { display: block; font-family: var(--font); font-size: 30px; line-height: 1; text-transform: uppercase; transform: skewX(-6deg); }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 14px; }
.stats div { display: grid; grid-template-columns: 1fr auto; font-size: 13px; }
.stats span { color: var(--mut); }
.stats b { font-family: var(--font); font-size: 16px; }
.stats em { grid-column: 1 / 3; height: 4px; background: rgba(255, 255, 255, 0.08); border-radius: 2px; overflow: hidden; }
.stats em i { display: block; height: 100%; background: linear-gradient(90deg, var(--acc), #8fe3ff); }
.desc { color: var(--mut); font-size: 13px; margin: 4px 0; }
.pp-badge { align-self: flex-start; font-family: var(--font); background: #000; border: 1px solid var(--acc); color: var(--acc); padding: 2px 10px; border-radius: 4px; font-weight: 700; }
.pp-badge b { color: #fff; font-size: 20px; }
.paints { display: flex; flex-wrap: wrap; gap: 6px; }
.sw { width: 28px; height: 28px; border-radius: 50%; background: var(--c); border: 2px solid rgba(255, 255, 255, 0.25); box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.25); position: relative; overflow: hidden; }
.sw.sel { border-color: #fff; box-shadow: 0 0 0 2px var(--acc2); }
.sw.custom { background: conic-gradient(red, yellow, lime, cyan, blue, magenta, red); }
.sw.custom input { position: absolute; inset: -6px; opacity: 0; cursor: pointer; }
.tabs { display: flex; gap: 6px; margin-bottom: 12px; overflow-x: auto; scrollbar-width: none; flex-shrink: 0; }
.tabs button { padding: 8px 14px; border-radius: 6px; background: var(--panel); border: 1px solid var(--line); font-family: var(--font); font-weight: 700; white-space: nowrap; color: var(--mut); transform: skewX(-8deg); }
.tabs button.sel { background: var(--acc); color: #04121c; border-color: var(--acc); }
.list { display: flex; flex-direction: column; gap: 6px; }
.carrow .dot { width: 18px; height: 18px; border-radius: 50%; background: var(--c); border: 2px solid rgba(255, 255, 255, 0.4); flex-shrink: 0; }
.record { font-family: var(--font); font-size: 18px; margin: 10px 0; }
.record b { color: var(--gold); }
.price { font-family: var(--font); font-size: 30px; font-weight: 700; color: var(--gold); margin-top: auto; }
.keys { font-size: 13px; color: var(--mut); line-height: 1.7; margin-top: 10px; }
.keys b { color: var(--txt); }

/* career */
.career { display: flex; flex-direction: column; gap: 14px; overflow: auto; flex: 1; max-width: 1000px; scrollbar-width: thin; }
.league { background: var(--panel); border: 1px solid var(--line); border-left: 4px solid var(--lc); border-radius: 12px; padding: 12px 14px; }
.league h3 { margin: 0 0 10px; font-family: var(--font); text-transform: uppercase; letter-spacing: 0.06em; color: var(--lc); }
.league h3 small { color: var(--mut); font-size: 13px; letter-spacing: 0; margin-left: 8px; }
.league.locked { opacity: 0.55; }
.events { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 8px; }
.card.event canvas { width: 92px; height: 60px; background: #121a2b; border-radius: 6px; flex-shrink: 0; }
.evhead { display: flex; gap: 14px; align-items: center; margin-bottom: 8px; }
.evhead canvas { width: 200px; height: auto; background: #121a2b; border-radius: 8px; }
.evhead small { display: block; color: var(--mut); margin-top: 4px; }
.evhead b { font-family: var(--font); font-size: 22px; }

/* loading */
.screen-loading { background: radial-gradient(circle at 70% 40%, #1a2846, #070b14 70%); align-items: center; justify-content: center; }
.loading { text-align: center; max-width: 640px; }
.loading canvas { width: min(420px, 80vw); height: auto; }
.loading h1 { font-family: var(--font); font-size: 52px; margin: 6px 0 0; text-transform: uppercase; transform: skewX(-8deg); }
.loading h4 { margin: 4px 0 8px; color: var(--acc); font-weight: 600; }
.loading p { color: var(--mut); }
.bar { height: 6px; background: rgba(255, 255, 255, 0.08); border-radius: 3px; overflow: hidden; margin: 18px auto 6px; width: 70%; }
.bar i { display: block; height: 100%; background: linear-gradient(90deg, var(--acc), var(--acc2)); transition: width 0.3s; }
.lstatus { font-size: 13px; color: var(--mut); }
.tip { font-size: 14px; margin-top: 22px; }

/* pause / results / replay */
.screen-pause, .screen-results { background: rgba(5, 8, 15, 0.6); align-items: center; justify-content: center; backdrop-filter: blur(3px); }
.pause { display: flex; flex-direction: column; gap: 10px; width: min(460px, 92vw); background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 20px; max-height: 92vh; overflow: auto; }
.pause h2, .results h2 { font-family: var(--font); font-size: 40px; margin: 0; text-align: center; letter-spacing: 0.1em; transform: skewX(-8deg); }
.opts.compact .opt { padding: 3px 0; }
.results { width: min(900px, 96vw); background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 18px 20px; max-height: 94vh; overflow: auto; }
.results h4 { text-align: center; margin: 2px 0 12px; color: var(--acc); }
.results table { width: 100%; border-collapse: collapse; font-size: 14px; }
.results th { text-align: left; color: var(--mut); font-weight: 600; padding: 4px 6px; border-bottom: 1px solid var(--line); }
.results td { padding: 5px 6px; border-bottom: 1px solid rgba(255, 255, 255, 0.04); font-family: var(--font); font-size: 17px; }
.results tr.me td { color: var(--acc2); font-weight: 700; }
.prize { text-align: center; font-family: var(--font); font-size: 24px; margin: 12px 0; }
.prize b { color: var(--gold); }
.row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 12px; }
.row .btn.go { margin-top: 0; padding: 10px 26px; }
.screen-replay { pointer-events: none; justify-content: flex-end; }
.replay { pointer-events: auto; }
.rtag { position: fixed; top: 18px; left: 22px; font-family: var(--font); font-weight: 700; font-size: 26px; letter-spacing: 0.3em; color: #fff; background: var(--bad); padding: 2px 14px; transform: skewX(-10deg); }
.rbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px; }
.rbar .btn { font-size: 15px; padding: 7px 12px; }
.rseek { flex: 1; min-width: 120px; accent-color: var(--acc2); }
.rtime { font-family: var(--font); font-size: 15px; color: var(--mut); }

.toast { position: fixed; left: 50%; bottom: 40px; transform: translate(-50%, 30px); background: var(--panel2); border: 1px solid var(--acc); padding: 10px 18px; border-radius: 8px; z-index: 100; opacity: 0; transition: all 0.3s; font-weight: 600; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 5; font-family: var(--font); color: #fff; text-shadow: 0 2px 0 rgba(0, 0, 0, 0.6); }
#hud .lbl { font-size: 12px; letter-spacing: 0.15em; color: rgba(255, 255, 255, 0.7); font-weight: 600; }
.h-top-left { position: absolute; top: max(12px, env(safe-area-inset-top)); left: max(16px, env(safe-area-inset-left)); }
.h-pos { font-weight: 700; line-height: 1; transform: skewX(-8deg); }
.h-pos-n { font-size: 64px; }
.h-pos-t { font-size: 26px; color: rgba(255, 255, 255, 0.7); }
.h-lap { font-size: 22px; margin-top: 2px; }
.h-lap b { font-size: 26px; }
.h-top-right { position: absolute; top: max(12px, env(safe-area-inset-top)); right: max(16px, env(safe-area-inset-right)); text-align: right; background: rgba(5, 9, 18, 0.45); padding: 8px 12px; border-radius: 8px; min-width: 190px; }
.h-row { display: flex; justify-content: space-between; gap: 14px; align-items: baseline; }
.h-row b { font-size: 22px; font-variant-numeric: tabular-nums; }
.h-delta { font-size: 22px; font-weight: 700; display: none; }
.h-delta.show { display: block; }
.h-delta.fast { color: var(--good); } .h-delta.slow { color: var(--bad); }
.h-map { position: absolute; left: max(12px, env(safe-area-inset-left)); bottom: max(12px, env(safe-area-inset-bottom)); width: 170px; height: 170px; background: radial-gradient(circle, rgba(5, 9, 18, 0.55), rgba(5, 9, 18, 0.15) 70%); border-radius: 50%; }
.h-board { position: absolute; left: max(16px, env(safe-area-inset-left)); top: 130px; font-size: 15px; display: flex; flex-direction: column; gap: 2px; }
.h-board div { display: grid; grid-template-columns: 26px 110px 46px; background: rgba(5, 9, 18, 0.45); padding: 1px 8px; border-radius: 4px; transform: skewX(-8deg); }
.h-board div.me { background: rgba(255, 122, 26, 0.65); }
.h-board i { font-style: normal; font-weight: 700; }
.h-board em { font-style: normal; text-align: right; color: rgba(255, 255, 255, 0.8); font-size: 13px; }
.h-dash { position: absolute; right: max(10px, env(safe-area-inset-right)); bottom: max(8px, env(safe-area-inset-bottom)); width: 260px; height: 160px; }
.h-tach { position: absolute; inset: 0; width: 260px; height: 150px; }
.h-speed { position: absolute; left: 0; right: 0; bottom: 18px; text-align: center; }
.h-speed b { font-size: 58px; line-height: 1; font-variant-numeric: tabular-nums; }
.h-speed span { font-size: 14px; margin-left: 4px; color: rgba(255, 255, 255, 0.75); }
.h-gear { position: absolute; left: 50%; top: 40px; transform: translateX(-50%) skewX(-8deg); font-size: 34px; font-weight: 700; width: 46px; height: 46px; display: flex; align-items: center; justify-content: center; background: rgba(5, 9, 18, 0.6); border: 2px solid rgba(255, 255, 255, 0.4); border-radius: 8px; }
.h-gear.shift { background: var(--bad); border-color: #fff; }
.h-aids { position: absolute; bottom: 0; left: 0; right: 0; display: flex; justify-content: center; gap: 6px; font-size: 11px; }
.h-aids span { padding: 0 5px; border-radius: 3px; color: rgba(255, 255, 255, 0.3); border: 1px solid rgba(255, 255, 255, 0.15); }
.h-aids span.on { color: #000; background: var(--gold); text-shadow: none; }
.h-msg { position: absolute; top: 28%; left: 0; right: 0; text-align: center; font-size: 74px; font-weight: 700; letter-spacing: 0.06em; opacity: 0; transform: scale(0.85) skewX(-8deg); transition: opacity 0.2s, transform 0.2s; text-shadow: 4px 4px 0 #000; }
.h-msg.show { opacity: 1; transform: scale(1) skewX(-8deg); }
.h-msg.go { color: var(--good); } .h-msg.win { color: var(--gold); } .h-msg.final { color: var(--acc); } .h-msg.bad { color: var(--bad); }
.h-sub { position: absolute; top: calc(28% + 90px); left: 0; right: 0; text-align: center; font-size: 26px; opacity: 0; transition: opacity 0.2s; }
.h-sub.show { opacity: 1; }
.h-lights { position: absolute; top: 14%; left: 50%; transform: translateX(-50%); display: none; gap: 12px; background: rgba(10, 10, 12, 0.85); padding: 12px 16px; border-radius: 14px; border: 2px solid #333; }
.h-lights.show { display: flex; }
.h-lights i { width: 38px; height: 38px; border-radius: 50%; background: #2a0909; box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.5); }
.h-lights i.on { background: #ff2020; box-shadow: 0 0 18px #ff2020; }
.h-lights i.go { background: #2bff6a; box-shadow: 0 0 18px #2bff6a; }
.h-fps { position: absolute; top: 4px; left: 50%; transform: translateX(-50%); font-size: 13px; color: var(--good); }
#hud.min .h-board, #hud.min .h-map, #hud.min .h-top-right { display: none; }

/* ---------- touch controls ---------- */
#touch { position: fixed; inset: 0; z-index: 6; touch-action: none; }
.t-steer-zone { position: absolute; left: 0; top: 0; bottom: 0; width: 50%; }
.t-wheel { position: absolute; width: 150px; height: 150px; margin: -75px 0 0 -75px; color: rgba(255, 255, 255, 0.85); opacity: 0; transition: opacity 0.12s; pointer-events: none; filter: drop-shadow(0 3px 0 rgba(0, 0, 0, 0.6)); }
.t-wheel.on { opacity: 1; }
.t-wheel svg { width: 100%; height: 100%; transition: transform 0.03s linear; }
.t-pedal { position: absolute; right: max(14px, env(safe-area-inset-right)); width: calc(min(26vw, 190px) * var(--t-sc, 1)); opacity: var(--t-op, 1); display: flex; align-items: center; justify-content: center; border-radius: 22px; font-family: var(--font); font-weight: 700; letter-spacing: 0.1em; font-size: 16px; color: rgba(255, 255, 255, 0.75); border: 3px solid rgba(255, 255, 255, 0.35); pointer-events: none; transition: scale 0.06s, background 0.06s, opacity 0.1s; }
.t-gas { bottom: max(18px, env(safe-area-inset-bottom)); height: calc(44vh * min(var(--t-sc, 1), 1)); background: rgba(60, 255, 154, 0.14); }
.t-brake { top: 13vh; height: calc(27vh * min(var(--t-sc, 1), 1)); background: rgba(255, 79, 109, 0.14); }
.t-gas.on { background: rgba(60, 255, 154, 0.45); scale: 0.97; opacity: max(var(--t-op, 1), 0.85); }
.t-brake.on { background: rgba(255, 79, 109, 0.5); scale: 0.97; opacity: max(var(--t-op, 1), 0.85); }
.t-btn { position: absolute; width: calc(50px * var(--t-sc, 1)); height: calc(50px * var(--t-sc, 1)); opacity: var(--t-op, 1); border-radius: 50%; background: rgba(5, 9, 18, 0.55); border: 2px solid rgba(255, 255, 255, 0.35); display: flex; align-items: center; justify-content: center; font-family: var(--font); font-weight: 700; font-size: 15px; color: #fff; }
.t-btn.on { background: rgba(71, 198, 255, 0.6); opacity: max(var(--t-op, 1), 0.85); }
.t-pause { top: max(10px, env(safe-area-inset-top)); left: 50%; margin-left: -25px; }
.t-cam { top: max(10px, env(safe-area-inset-top)); left: calc(50% + 40px); }
.t-reset { top: max(10px, env(safe-area-inset-top)); left: calc(50% - 90px); }
.t-hand { right: calc(min(26vw, 190px) * var(--t-sc, 1) + 30px); bottom: 26vh; }
.t-up, .t-down { display: none; right: calc(min(26vw, 190px) * var(--t-sc, 1) + 30px); }
#touch.manual .t-up, #touch.manual .t-down { display: flex; }
.t-up { bottom: calc(26vh + 130px); } .t-down { bottom: calc(26vh + 68px); }
body.touch-on .h-dash { right: calc(min(26vw, 190px) * var(--t-sc, 1) + 24px); }
body.touch-on .h-top-right { right: calc(min(26vw, 190px) * var(--t-sc, 1) + 30px); }
body.touch-on .h-board { display: none; }

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  .split { grid-template-columns: 1fr; overflow: auto; }
  .main-grid { grid-template-columns: 1fr 1fr; gap: 10px; }
  .tile { min-height: 84px; padding: 12px 14px; }
  .tile b { font-size: 20px; }
  .logo .l1 { font-size: 40px; } .logo .l2 { font-size: 58px; }
  .h-pos-n { font-size: 44px; }
  .h-map { width: 120px; height: 120px; }
  .h-board { display: none; }
  .h-dash { transform: scale(0.78); transform-origin: right bottom; }
  .h-msg { font-size: 48px; }
}
@media (max-height: 500px) {
  .h-dash { transform: scale(0.7); transform-origin: right bottom; }
  .h-top-right { transform: scale(0.85); transform-origin: right top; }
  .h-map { width: 110px; height: 110px; }
  .h-board { display: none; }
  .title-wrap { margin-top: 18vh; }
  .main-grid { margin-top: 1vh; }
}

#rotate { display: none; position: fixed; inset: 0; z-index: 50; background: rgba(8, 12, 22, 0.94); color: var(--txt); font-family: var(--font); font-size: 22px; align-items: center; justify-content: center; flex-direction: column; text-align: center; padding: 30px; }
#rotate div { font-size: 64px; animation: spin 2s infinite; }
@keyframes spin { 50% { transform: rotate(90deg); } }
@media (orientation: portrait) and (pointer: coarse) { body.racing #rotate { display: flex; } }

.h-timer { display: none; position: absolute; top: max(14px, env(safe-area-inset-top)); left: 50%; transform: translateX(-50%) skewX(-8deg); font-size: 26px; font-weight: 700; background: rgba(5, 9, 18, 0.6); border: 1px solid rgba(71, 198, 255, 0.5); padding: 4px 16px; border-radius: 8px; color: var(--acc); }
.h-tags { position: absolute; inset: 0; overflow: hidden; }
.h-tag { position: absolute; left: 0; top: 0; display: none; font-size: 14px; font-weight: 700; padding: 1px 8px; background: rgba(5, 9, 18, 0.65); border: 1px solid rgba(255, 255, 255, 0.35); border-radius: 4px; white-space: nowrap; will-change: transform; }

/* ---------- multiplayer ---------- */
.mp-grid { display: grid; grid-template-columns: minmax(300px, 42%) minmax(320px, 1fr); gap: 14px; flex: 1; min-height: 0; }
.mp-left { display: flex; flex-direction: column; gap: 6px; }
.mp-player { display: grid; grid-template-columns: 20px 1fr auto auto auto; gap: 10px; align-items: center; padding: 6px 8px; background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; margin-bottom: 4px; }
.mp-player.me { border-color: var(--acc2); }
.mp-player .dot { width: 16px; height: 16px; border-radius: 50%; background: var(--c); border: 2px solid rgba(255,255,255,0.4); }
.mp-player b { font-family: var(--font); font-size: 17px; display: block; }
.mp-player small { color: var(--mut); font-size: 12px; }
.mp-player em { font-style: normal; font-size: 12px; color: var(--mut); font-weight: 700; letter-spacing: 0.05em; }
.mp-player em.ok { color: var(--good); }
.mp-player .ping { font-style: normal; font-size: 11px; color: var(--mut); }
.x { width: 28px; height: 28px; border-radius: 6px; background: rgba(255,255,255,0.06); border: 1px solid var(--line); font-weight: 700; }
.x:hover { border-color: var(--acc); }
.mp-me { display: flex; flex-direction: column; gap: 8px; padding: 8px 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); margin: 6px 0; }
.mp-mycar { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.mp-chatlog { flex: 1; min-height: 80px; max-height: 180px; overflow: auto; font-size: 13px; background: rgba(0,0,0,0.25); border-radius: 8px; padding: 6px 8px; }
.mp-chatlog .sys { color: var(--mut); font-style: italic; }
.mp-chatin, .opt input#mpIp, .opt input#mpPort, .opt input#mpName, .opt input#mpLobbyName { background: #0a0f1a; color: var(--txt); border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px; font: inherit; }
.mp-chatin { width: 100%; }
.mp-track { display: grid; grid-template-columns: 22px 1fr auto auto auto auto; gap: 6px; align-items: center; padding: 3px 0; }
.mp-track i { font-style: normal; color: var(--acc); font-weight: 700; }
.mp-track span { font-family: var(--font); font-size: 16px; font-weight: 600; }
.opt.col { flex-direction: column; align-items: stretch; }
.chips, .mp-addtrack { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 4px; }
.chip { padding: 4px 10px; border-radius: 14px; background: rgba(255,255,255,0.06); border: 1px solid var(--line); font-size: 12px; font-weight: 600; color: var(--mut); }
.chip.sel { background: var(--acc); color: #04121c; border-color: var(--acc); }
.chip:disabled, .seg button:disabled { cursor: default; }
.mp-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.mp-table th { text-align: left; color: var(--mut); font-weight: 600; border-bottom: 1px solid var(--line); }
.mp-table td { padding: 3px 4px; font-family: var(--font); font-size: 16px; }
.lobby-id { font-size: 14px; color: var(--mut); letter-spacing: 0.1em; }
.mp-help ol { padding-left: 18px; line-height: 1.6; font-size: 14px; }
.mp-help code { background: rgba(0,0,0,0.4); padding: 1px 6px; border-radius: 4px; color: var(--acc); }
.mp-lobbies { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.mp-results { width: min(1100px, 97vw); }
.mp-res-grid { display: grid; grid-template-columns: 1.6fr 1fr; gap: 16px; }
.mp-res-grid h4 { margin: 4px 0 6px; color: var(--acc); text-align: left; }
@media (max-width: 900px) { .mp-grid, .mp-res-grid { grid-template-columns: 1fr; overflow: auto; } }

/* ---------- mobile / small screens ---------- */
/* menus must scroll with the finger (the game canvas and the touch pad keep touch-action: none) */
.ui, .ui * { touch-action: pan-x pan-y; }
#touch, #touch * , #game { touch-action: none; }
@media (max-width: 900px), (max-height: 560px) {
  .ui:not(.screen-title):not(.screen-loading):not(.screen-replay) { overflow-y: auto; overflow-x: hidden; -webkit-overflow-scrolling: touch; padding: max(8px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left)); }
  .top { position: sticky; top: calc(-1 * max(8px, env(safe-area-inset-top))); z-index: 3; margin: 0 -10px 8px; padding: 6px 10px; background: rgba(8, 12, 22, 0.94); min-height: 0; gap: 10px; }
  .top h2 { font-size: 20px; }
  .top .back { font-size: 15px; padding: 6px 12px; }
  .wallet { font-size: 15px; padding: 4px 10px; }
  .split, .mp-grid, .mp-res-grid { display: flex; flex-direction: column; gap: 10px; flex: none; min-height: auto; overflow: visible; }
  .split > .carpanel, .split > .panel, .mp-grid > .panel { max-height: none; width: 100%; align-self: stretch; overflow: visible; justify-self: auto; }
  .panel { padding: 10px 12px; }
  .career { overflow: visible; flex: none; }
  .btn.go { position: sticky; bottom: 0; z-index: 2; font-size: 18px; padding: 12px; box-shadow: 0 -6px 18px rgba(8, 12, 22, 0.9); }
  .main-grid { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 8px; margin-top: 6px; }
  .tile { min-height: 0; padding: 10px 12px; gap: 2px; }
  .tile .ti { font-size: 20px; }
  .tile b { font-size: 18px; }
  .tile small { font-size: 11px; }
  .tracks { grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 6px; }
  .tc-name { font-size: 14px; }
  .opt { padding: 4px 0; gap: 6px; }
  .seg button { padding: 6px 9px; font-size: 14px; }
  .stats { gap: 4px 10px; }
  .carname b { font-size: 22px; }
  .events { grid-template-columns: 1fr; }
  .tabs { margin-bottom: 8px; }
  .pause, .results { max-height: calc(100vh - 16px); max-height: calc(100dvh - 16px); overflow-y: auto; padding: 14px; }
  .pause h2, .results h2 { font-size: 28px; }
  .results table { font-size: 12px; }
  .results td { font-size: 14px; padding: 3px 4px; }
  .results th:nth-child(3), .results td:nth-child(3) { display: none; } /* car name column */
  .row { position: sticky; bottom: 0; background: var(--panel); padding-top: 8px; }
  .screen-pause, .screen-results { align-items: stretch; justify-content: flex-start; overflow-y: auto; }
  .mp-chatlog { max-height: 120px; }
  .mp-player { grid-template-columns: 16px 1fr auto auto auto; gap: 6px; }
  .rbar { gap: 4px; padding: 6px; }
  .rbar .btn { font-size: 12px; padding: 6px 8px; }
  .rseek { min-width: 80px; }
  .title-wrap { margin-top: 18vh; }
  .logo .l1 { font-size: 38px; } .logo .l2 { font-size: 56px; }
  .toast { bottom: 12px; max-width: 90vw; text-align: center; }
}
@media (max-width: 900px) and (orientation: portrait) {
  .main-grid { grid-template-columns: 1fr 1fr; }
  .mp-res-grid table { font-size: 12px; }
}
@media (max-width: 900px), (max-height: 560px) {
  /* the main action of each screen floats bottom-right so it is always reachable */
  .split .btn.go, .mp-me .btn.go, .evhead ~ .btn.go { position: fixed; right: max(10px, env(safe-area-inset-right)); bottom: max(10px, env(safe-area-inset-bottom)); left: auto; width: auto; min-width: 180px; z-index: 6; margin: 0; }
  .ui.screen-quick, .ui.screen-timetrial, .ui.screen-picker, .ui.screen-starter, .ui.screen-event, .ui.screen-dealer, .ui.screen-garage, .ui.screen-mpLobby { padding-bottom: 76px; }
  .card.track canvas { height: 64px; width: 100%; object-fit: contain; }
  .card.track { padding: 6px; }
  .tc-sub { display: none; }
  .evhead canvas { width: 130px; }
  .carpanel .desc { display: none; }
}
@media (max-width: 900px), (max-height: 560px) {
  .panel, .league, .tile { backdrop-filter: none; }
  .top { background: #080c16; box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5); }
}
@media (max-width: 900px), (max-height: 560px) {
  .ui > * { flex-shrink: 0; } /* the screen scrolls instead of squeezing the header/panels */
}
@media (max-height: 560px) and (min-width: 600px) {
  /* landscape phones: two-column pause menu so everything fits with little scrolling */
  .pause { width: min(860px, 96vw); margin: 0 auto; display: grid; grid-template-columns: 1fr 1fr; gap: 8px 14px; align-content: start; }
  .pause h2, .pause > p { grid-column: 1 / -1; }
  .pause { grid-template-rows: auto repeat(5, min-content) 1fr; }
  .pause .opts { grid-column: 2; grid-row: 2 / -1; } /* the last 1fr row absorbs the taller options column */
  .results, .mp-results { margin: 0 auto; }
}
@media (max-width: 900px) { .pause { margin: 0 auto; } }
/* touch HUD: keep the round buttons clear of the lap-time panel and the pedals */
body.touch-on .t-reset { left: max(140px, calc(env(safe-area-inset-left) + 130px)); margin-left: 0; }
body.touch-on .t-pause { left: max(200px, calc(env(safe-area-inset-left) + 190px)); margin-left: 0; }
body.touch-on .t-cam { left: max(260px, calc(env(safe-area-inset-left) + 250px)); margin-left: 0; }
@media (max-height: 560px) {
  body.touch-on .h-top-right { min-width: 0; padding: 4px 8px; }
  body.touch-on .h-row b { font-size: 16px; }
  body.touch-on #hud .lbl { font-size: 10px; }
  .h-pos-n { font-size: 46px; } .h-pos-t { font-size: 20px; } .h-lap { font-size: 16px; } .h-lap b { font-size: 20px; }
  .h-dash { transform: scale(0.78); transform-origin: 100% 100%; }
  .h-map { width: 120px; height: 120px; }
  .t-btn { width: calc(44px * var(--t-sc, 1)); height: calc(44px * var(--t-sc, 1)); font-size: 13px; }
  .t-hand { bottom: calc(26vh + 44px); }
  .h-lights { top: 38%; }
  .t-down { bottom: calc(26vh + 96px); } .t-up { bottom: calc(26vh + 148px); }
  .h-msg { font-size: 52px; } .h-sub { font-size: 20px; top: calc(28% + 64px); }
  .h-lights i { width: 28px; height: 28px; }
}

/* ---------- fullscreen button ---------- */
.fsb { flex: 0 0 auto; width: 42px; height: 42px; margin-left: auto; display: inline-flex; align-items: center; justify-content: center; padding: 0; background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; color: #fff; cursor: pointer; }
.fsb:hover { border-color: var(--acc); }
.fsb svg { width: 22px; height: 22px; }
body.fs .fsb .fs-in, body:not(.fs) .fsb .fs-out { display: none; }
.fsb.float { position: fixed; top: max(12px, env(safe-area-inset-top)); right: max(14px, env(safe-area-inset-right)); z-index: 12; }
.pause .btn.fsrow { display: inline-flex; align-items: center; justify-content: center; gap: 10px; }
.pause .btn.fsrow svg { width: 20px; height: 20px; }
body.fs .btn.fsrow .fs-in, body:not(.fs) .btn.fsrow .fs-out { display: none; }
@media (max-width: 900px), (max-height: 560px) { .fsb { width: 36px; height: 36px; } .fsb svg { width: 19px; height: 19px; } }

/* ---------- touch layout: mirrored (pedals on the left) ---------- */
body.touch-on.touch-mirror .h-dash { right: max(10px, env(safe-area-inset-right)); }
body.touch-on.touch-mirror .h-top-right { right: max(16px, env(safe-area-inset-right)); }
body.touch-on.touch-mirror .h-top-left { left: calc(min(26vw, 190px) * var(--t-sc, 1) + 30px); }
body.touch-on.touch-mirror .h-map { left: calc(min(26vw, 190px) * var(--t-sc, 1) + 24px); }

/* ---------- touch layout editor ---------- */
body.touch-editing #ui, body.touch-editing #hud { display: none; }
.t-steer-zone span { display: none; }
#touch.editing { background: rgba(4, 7, 14, 0.35); }
#touch.editing .t-steer-zone { background: rgba(71, 198, 255, 0.07); border: 2px dashed rgba(71, 198, 255, 0.35); box-sizing: border-box; display: flex; align-items: flex-start; justify-content: center; padding-top: 22vh; }
#touch.editing .t-steer-zone span { display: block; text-align: center; font-family: var(--font); font-weight: 700; letter-spacing: 0.12em; color: rgba(150, 220, 255, 0.6); font-size: 15px; }
#touch.editing .t-steer-zone small { letter-spacing: 0.02em; font-weight: 500; font-size: 12px; }
#touch.editing .t-pedal { pointer-events: auto; }
#touch.editing .t-pedal, #touch.editing .t-btn { cursor: grab; outline: 2px dashed rgba(255, 255, 255, 0.55); outline-offset: 3px; }
#touch.editing .drag { outline-color: var(--acc); cursor: grabbing; z-index: 2; }
.t-edit-bar { position: fixed; z-index: 20; left: 25%; bottom: max(12px, env(safe-area-inset-bottom)); transform: translateX(-50%); width: min(300px, 44vw); max-height: calc(100dvh - 90px); overflow-y: auto; display: flex; flex-direction: column; gap: 8px; padding: 12px 14px; background: rgba(10, 15, 28, 0.94); border: 1px solid var(--line); border-radius: 12px; font-family: var(--font2, sans-serif); color: #fff; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5); }
.t-edit-bar.right { left: 75%; }
.t-edit-bar b { font-family: var(--font); font-size: 17px; letter-spacing: 0.04em; text-transform: uppercase; }
.t-edit-bar small { color: rgba(255, 255, 255, 0.65); font-size: 12px; }
.t-edit-bar label { display: grid; grid-template-columns: 76px 1fr 40px; align-items: center; gap: 8px; font-size: 13px; }
.t-edit-bar label i { font-style: normal; text-align: right; color: var(--acc); font-weight: 700; }
.t-edit-bar input[type=range] { width: 100%; }
.t-edit-row { display: flex; gap: 8px; }
.t-edit-row button { flex: 1; padding: 8px 6px; border-radius: 7px; background: var(--panel2); border: 1px solid var(--line); color: #fff; font-family: var(--font); font-weight: 700; font-size: 14px; cursor: pointer; }
.t-edit-row button.ok { background: linear-gradient(90deg, #ff7a1a, #ff4f1a); border-color: transparent; }
@media (max-height: 420px) { .t-edit-bar { gap: 5px; padding: 8px 10px; } .t-edit-bar small { display: none; } .t-edit-row button { padding: 6px; } }
.opt > span .sub { display: block; font-size: 12px; font-weight: 500; color: var(--mut); opacity: 0.8; }
@media (max-height: 560px) and (min-width: 600px) {
  .pause > .btn { padding: 8px 14px; font-size: 16px; min-height: 0; margin: 0; }
  .pause h2 { margin: 0 0 2px; font-size: 26px; }
}
