:root {
  --bg: #0f141b;
  --panel: rgba(18, 24, 33, .86);
  --card: #18202b;
  --card2: #222c39;
  --line: rgba(255, 255, 255, .1);
  --text: #e9eef4;
  --muted: #93a1b2;
  --accent: #ff7a00;
  --accent2: #2fa4ff;
  --ok: #3ccf6e;
  --warn: #ffc53d;
  --bad: #ff4d4d;
  --wheel: clamp(118px, 30vmin, 210px);
  --pedal: clamp(58px, 15vmin, 96px);
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  overscroll-behavior: none;
}

#world { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

button { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.hidden { display: none !important; }

/* ---------- screens & cards ---------- */
.screen {
  position: fixed; inset: 0; display: none; z-index: 10;
  overflow-y: auto; -webkit-overflow-scrolling: touch; touch-action: pan-y;
  background: radial-gradient(ellipse at center, rgba(10, 14, 20, .55), rgba(10, 14, 20, .88));
  padding: calc(16px + var(--sat)) calc(12px + var(--sar)) calc(16px + var(--sab)) calc(12px + var(--sal));
}
.screen.active { display: flex; }
.screen > .card { margin: auto; }

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: 18px;
  padding: 20px; width: min(460px, 100%); box-shadow: 0 20px 60px rgba(0, 0, 0, .5);
}
.card.wide { width: min(860px, 100%); }
.card h1 { margin: 6px 0 4px; font-size: clamp(22px, 5vw, 32px); text-align: center; line-height: 1.15; }
.card h2 { margin: 0 0 12px; font-size: 22px; }
.card-head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.card-head h2 { margin: 0; }
.sec-title { margin: 6px 0 8px; font-size: 14px; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; }

.menu-card { text-align: center; display: flex; flex-direction: column; gap: 10px; }
.logo {
  width: 72px; height: 72px; margin: 0 auto; border-radius: 18px; display: grid; place-items: center;
  font-size: 46px; font-weight: 800; color: #fff; background: linear-gradient(135deg, #2f7dff, #1b4fd1);
  box-shadow: 0 8px 24px rgba(47, 125, 255, .45); border: 3px solid #fff;
}
.subtitle { margin: 0 0 6px; color: var(--muted); }
.menu-total { display: flex; gap: 8px; justify-content: center; margin-bottom: 6px; }
.menu-total > div { background: var(--card2); border-radius: 12px; padding: 6px 12px; min-width: 80px; }
.menu-total small, .hud-stats small { display: block; font-size: 11px; color: var(--muted); }
.menu-total b { font-size: 18px; }

.btn {
  display: block; width: 100%; padding: 12px 16px; margin: 0; border-radius: 12px; cursor: pointer;
  background: var(--card2); border: 1px solid var(--line); font-weight: 600; font-size: 16px;
  transition: transform .08s, background .15s;
}
.btn:hover { background: #2a3646; }
.btn:active { transform: scale(.97); }
.btn.primary { background: linear-gradient(180deg, #ff8f26, #f06a00); border-color: #ff9a40; color: #fff; }
.btn.primary:hover { background: linear-gradient(180deg, #ffa04a, #ff7a00); }
.btn.danger { background: #4a1d22; border-color: #7a2c33; }
.btn.big { font-size: 20px; padding: 16px; }
.btn.small { width: auto; padding: 8px 14px; }
.btn-row { display: flex; gap: 8px; margin-top: 10px; }
.btn-row .btn { flex: 1; }
.overlay .card > .btn { margin-top: 8px; }

.lang-row { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; margin-top: 6px; }
.lang-row button {
  border: 1px solid var(--line); background: transparent; border-radius: 8px; padding: 6px 10px; cursor: pointer; font-size: 14px;
}
.lang-row button.active { background: var(--accent2); border-color: var(--accent2); color: #fff; }

/* ---------- language picker ---------- */
.menu-card { position: relative; }
.lang-picker { position: absolute; top: 12px; right: 12px; z-index: 30; direction: ltr; text-align: left; }
[dir="rtl"] .lang-picker { right: auto; left: 12px; }
[dir="rtl"] .lang-list { right: auto; left: 0; }
.lang-btn {
  display: flex; align-items: center; gap: 6px; padding: 7px 12px; border-radius: 10px; cursor: pointer;
  background: var(--panel); border: 1px solid var(--line); font-weight: 700; font-size: 14px;
}
.lang-btn:hover { border-color: var(--accent2); }
.lang-btn .caret { font-size: 11px; opacity: .7; transition: transform .15s; }
.lang-picker.open .caret { transform: rotate(180deg); }
.lang-list {
  display: none; position: absolute; right: 0; top: calc(100% + 6px); margin: 0; padding: 6px; list-style: none;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 12px 40px rgba(0,0,0,.5);
  min-width: 190px; max-height: min(70vh, 460px); overflow-y: auto;
}
.lang-picker.open .lang-list { display: block; }
.lang-list li { display: flex; gap: 10px; align-items: center; padding: 8px 10px; border-radius: 8px; cursor: pointer; font-size: 15px; }
.lang-list li b { width: 26px; font-size: 12px; color: var(--muted); }
.lang-list li:hover, .lang-list li:focus { background: var(--card2); outline: none; }
.lang-list li.active { background: var(--accent2); color: #fff; }
.lang-list li.active b { color: #fff; }

/* ---------- modes ---------- */
.mode-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.mode-btn { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 14px 10px; }
.mode-btn b { font-size: 19px; }
.mode-btn small { font-size: 12px; font-weight: 500; opacity: .85; line-height: 1.3; }
.mode-btn.training { background: linear-gradient(180deg, #2f8fe0, #1c62b0); border-color: #4aa6f5; color: #fff; }
.mode-btn.training:hover { background: linear-gradient(180deg, #45a0ee, #2f8fe0); }
.mode-badge {
  margin-left: auto; font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em;
  padding: 4px 10px; border-radius: 8px; background: var(--accent); color: #fff;
}
.mode-badge.training { background: var(--accent2); }
.mode-badge.small { margin-left: 0; font-size: 10px; padding: 1px 6px; vertical-align: middle; }
.hud-level b .mode-badge, .hud-level .mode-badge { display: inline-block; margin-top: 2px; margin-right: 6px; }
.filters { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.filters button {
  border: 1px solid var(--line); background: var(--card2); border-radius: 20px; padding: 7px 14px; cursor: pointer; font-size: 14px;
}
.filters button.active { background: var(--accent2); border-color: var(--accent2); color: #fff; }
.filters button small { opacity: .7; margin-left: 4px; }
@media (max-width: 420px) { .mode-row { grid-template-columns: 1fr; } }

/* ---------- level select ---------- */
.car-select { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 14px; }
.car-opt {
  background: var(--card2); border: 2px solid transparent; border-radius: 12px; padding: 8px; cursor: pointer; text-align: center;
}
.car-opt.active { border-color: var(--accent); }
.car-opt canvas { width: 100%; height: 44px; display: block; }
.car-opt b { display: block; font-size: 14px; }
.car-opt small { color: var(--muted); font-size: 11px; display: block; }

.level-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
.lvl {
  position: relative; background: var(--card2); border: 1px solid var(--line); border-radius: 14px;
  padding: 10px; text-align: left; cursor: pointer; min-height: 104px; display: flex; flex-direction: column; gap: 4px;
}
.lvl:hover { border-color: var(--accent2); }
.lvl .num { font-size: 26px; font-weight: 800; color: var(--accent2); line-height: 1; }
.lvl .name { font-weight: 600; font-size: 14px; }
.lvl .type { font-size: 11px; color: var(--muted); }
.lvl .st { margin-top: auto; color: #ffd24a; letter-spacing: 2px; }
.lvl .st .off { color: #3c4756; }
.lvl .best { position: absolute; right: 10px; top: 10px; font-size: 12px; color: var(--muted); }
.lvl.locked { opacity: .45; cursor: not-allowed; }
.lvl.locked .num::after { content: " 🔒"; font-size: 16px; }

/* ---------- settings / help ---------- */
.settings { display: flex; flex-direction: column; gap: 6px; }
.set-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: var(--card2); border-radius: 12px; padding: 10px 12px;
}
.switch { position: relative; width: 50px; height: 28px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; border-radius: 28px; background: #3a4556; transition: .2s; cursor: pointer; }
.switch span::before { content: ""; position: absolute; width: 22px; height: 22px; left: 3px; top: 3px; border-radius: 50%; background: #fff; transition: .2s; }
.switch input:checked + span { background: var(--ok); }
.switch input:checked + span::before { transform: translateX(22px); }
.seg { display: flex; flex-wrap: wrap; justify-content: flex-end; border-radius: 10px; overflow: hidden; border: 1px solid var(--line); }
.seg button { border: 0; background: transparent; padding: 6px 12px; cursor: pointer; }
.seg button.active { background: var(--accent2); color: #fff; }

.help { line-height: 1.5; font-size: 15px; }
.help h3 { margin: 14px 0 4px; color: var(--accent); }
.help ul { margin: 4px 0; padding-left: 20px; }
.help kbd { background: #2c3644; border-radius: 5px; padding: 1px 6px; border: 1px solid #3c4858; font-size: 13px; }

/* ---------- HUD ---------- */
/* height follows the VISIBLE viewport (mobile browser toolbars), set from JS as --app-h */
/* driving controls keep a fixed physical layout even in RTL languages */
#hud, #sensor-canvas, .wheel-wrap, .pedals { direction: ltr; }
#hud { position: fixed; top: 0; left: 0; right: 0; height: 100vh; height: 100dvh; height: var(--app-h, 100dvh); z-index: 5; pointer-events: none; }
#world { height: 100vh; height: 100dvh; height: var(--app-h, 100dvh); }
#hud button, #hud .wheel { pointer-events: auto; }

.hud-top {
  position: absolute; top: calc(8px + var(--sat)); left: calc(8px + var(--sal)); right: calc(8px + var(--sar));
  display: flex; gap: 8px; align-items: center;
}
.hud-btn {
  width: 42px; height: 42px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel);
  font-size: 18px; cursor: pointer; display: grid; place-items: center; flex: none; touch-action: none;
}
.hud-btn.on { background: var(--accent2); }
.hud-level { background: var(--panel); border-radius: 12px; padding: 4px 12px; min-width: 0; }
.hud-level b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 15px; }
.hud-level small { color: var(--muted); font-size: 11px; white-space: nowrap; }
.hud-stats { display: flex; gap: 6px; margin-left: auto; }
.hud-stats > div { background: var(--panel); padding: 4px 10px; border-radius: 12px; text-align: center; min-width: 56px; }
.hud-stats b { font-size: 16px; font-variant-numeric: tabular-nums; }
.hud-stats .bad { color: var(--bad); }

.hud-left {
  position: absolute; left: calc(8px + var(--sal)); top: calc(60px + var(--sat));
  display: flex; flex-direction: column; gap: 6px;
}
.hud-right { position: absolute; right: calc(8px + var(--sar)); top: calc(60px + var(--sat)); }
#sensor-canvas { width: 112px; height: 160px; background: var(--panel); border-radius: 14px; display: block; }

.toast {
  position: absolute; left: 50%; top: calc(92px + var(--sat)); transform: translate(-50%, -10px);
  background: var(--panel); padding: 8px 16px; border-radius: 12px; font-weight: 600; opacity: 0;
  transition: opacity .2s, transform .2s; white-space: normal; text-align: center; max-width: min(90vw, 460px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.bad { background: rgba(170, 30, 30, .92); }
.toast.ok { background: rgba(30, 140, 70, .92); }

.park-meter {
  position: absolute; left: 50%; top: calc(134px + var(--sat)); transform: translateX(-50%);
  display: none; align-items: center; gap: 8px; background: var(--panel); padding: 6px 12px; border-radius: 12px; font-weight: 600;
}
.park-meter.show { display: flex; }
.park-meter .bar { width: 120px; height: 8px; border-radius: 4px; background: #2b3542; overflow: hidden; }
.park-meter i { display: block; height: 100%; width: 0; background: var(--ok); }
.park-meter.align i { background: var(--warn); }

.controls {
  position: absolute; left: 0; right: 0; bottom: 0; display: flex; align-items: flex-end; justify-content: space-between;
  padding: 10px calc(12px + var(--sar)) calc(10px + var(--sab)) calc(12px + var(--sal)); gap: 8px;
}
.wheel-wrap { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.wheel {
  width: var(--wheel); height: var(--wheel); touch-action: none; cursor: grab; border-radius: 50%;
  filter: drop-shadow(0 6px 14px rgba(0, 0, 0, .55));
}
.wheel:active { cursor: grabbing; }
.wheel svg { width: 100%; height: 100%; display: block; will-change: transform; }
.wheel-info {
  display: flex; align-items: center; gap: 8px; background: var(--panel); border-radius: 10px; padding: 2px 8px;
  font-variant-numeric: tabular-nums; font-size: 13px; pointer-events: auto;
}
.wheel-info span { min-width: 44px; text-align: center; }
.mini-btn { border: 0; background: #2c3644; border-radius: 8px; width: 30px; height: 26px; cursor: pointer; touch-action: none; }

.center-info { display: flex; flex-direction: column; align-items: center; gap: 4px; margin-bottom: 6px; }
.gear {
  width: 52px; height: 52px; border-radius: 12px; background: var(--panel); border: 2px solid var(--line);
  display: grid; place-items: center; font-size: 26px; font-weight: 800;
}
.gear { pointer-events: auto; cursor: help; }
.gear.D { color: var(--ok); border-color: var(--ok); }
.gear.R { color: var(--bad); border-color: var(--bad); }
.speed { background: var(--panel); border-radius: 10px; padding: 3px 10px; font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }

.pedals {
  display: grid; grid-template-columns: auto auto; grid-template-rows: auto auto; gap: 8px;
}
.pedal {
  width: var(--pedal); height: var(--pedal); border-radius: 16px; cursor: pointer; touch-action: none;
  border: 2px solid rgba(255, 255, 255, .18); background: linear-gradient(180deg, #343e4c, #1f2630);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  box-shadow: 0 6px 14px rgba(0, 0, 0, .45);
}
.pedal b { font-size: calc(var(--pedal) * .32); line-height: 1; }
.pedal small { font-size: 11px; font-weight: 600; letter-spacing: .02em; }
.pedal.fwd { grid-column: 2; grid-row: 1; border-color: rgba(60, 207, 110, .6); }
.pedal.rev { grid-column: 2; grid-row: 2; border-color: rgba(255, 77, 77, .6); }
.pedal.brake { grid-column: 1; grid-row: 2; width: calc(var(--pedal) * .8); }
.pedal.active { transform: scale(.94); filter: brightness(1.4); }
.pedal.fwd.active { background: linear-gradient(180deg, #2e9a55, #1c5e35); }
.pedal.rev.active { background: linear-gradient(180deg, #b13a3a, #6d2020); }
.pedal.brake.active { background: linear-gradient(180deg, #a07d1c, #5e4910); }

/* ---------- overlays ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 20; display: none; align-items: center; justify-content: center;
  background: rgba(5, 8, 12, .6); padding: 16px; overflow-y: auto; touch-action: pan-y;
}
.overlay.show { display: flex; }
.overlay .card { margin: auto; }
.intro-head { display: flex; gap: 12px; align-items: center; margin-bottom: 8px; }
.intro-head h2 { margin: 0; }
.lvl-badge {
  width: 48px; height: 48px; border-radius: 12px; background: var(--accent2); display: grid; place-items: center;
  font-size: 24px; font-weight: 800; flex: none;
}
.intro-facts { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin: 10px 0; }
.intro-facts div { background: var(--card2); border-radius: 10px; padding: 6px 10px; font-size: 14px; }
.intro-facts small { display: block; color: var(--muted); font-size: 11px; }

.stars { text-align: center; font-size: 44px; color: #ffd24a; letter-spacing: 6px; margin: -4px 0 4px; }
.stars .off { color: #3c4756; }
.breakdown { width: 100%; border-collapse: collapse; margin: 8px 0; font-size: 14px; }
.breakdown td { padding: 4px 6px; border-bottom: 1px solid var(--line); }
.breakdown td:last-child { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.breakdown .neg { color: #ff8080; }
.breakdown .pos { color: #7ee09c; }
.breakdown tr.total td { font-size: 18px; border-bottom: 0; padding-top: 8px; }
.record { text-align: center; color: var(--warn); font-weight: 700; min-height: 1em; }

.orient-hint {
  display: none; position: fixed; left: 50%; bottom: calc(50% - 20px); transform: translateX(-50%); z-index: 4;
  background: var(--panel); padding: 6px 12px; border-radius: 10px; font-size: 13px; color: var(--muted); pointer-events: none;
}

/* ---------- responsive ---------- */
@media (max-height: 460px) {
  :root { --wheel: clamp(110px, 34vmin, 160px); --pedal: clamp(54px, 17vmin, 76px); }
  #sensor-canvas { width: 84px; height: 120px; }
  .hud-btn { width: 36px; height: 36px; font-size: 16px; }
  .hud-left, .hud-right { top: calc(50px + var(--sat)); }
  .hud-left { flex-direction: row; top: calc(64px + var(--sat)); }
  .hud-level { padding: 3px 10px; }
  .hud-level b { font-size: 14px; }
  .hud-stats > div { min-width: 48px; padding: 2px 8px; }
  .hud-stats b { font-size: 14px; }
  .gear { width: 42px; height: 42px; font-size: 22px; }
  .card { padding: 14px; }
}
@media (max-width: 560px) {
  .hud-level { display: none; }
  .hud-stats { margin-left: 0; flex: 1; justify-content: flex-end; }
  .center-info { position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(var(--wheel) + 46px + var(--sab)); }
  .car-select { gap: 6px; }
}
@media (orientation: portrait) and (max-width: 700px) {
  .orient-hint.ingame { display: block; animation: fadeHint 6s forwards; }
}
@keyframes fadeHint { 0%, 70% { opacity: 1; } 100% { opacity: 0; } }
