:root {
  --ink: #f4f1ea;
  --ink-soft: #a9a59c;
  --card: rgba(22, 24, 30, 0.78);
  --card-solid: #1b1f27;
  --line: rgba(255, 255, 255, 0.1);
  --accent: #ffc21a;
  --accent-dark: #d99a00;
  --danger: #ff4d3d;
  --good: #4de07a;
  --radius: 16px;
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #1b1f27; }
body { -webkit-tap-highlight-color: transparent; user-select: none; -webkit-user-select: none; }
button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, canvas:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
canvas:focus { outline: none; }
kbd { display: inline-block; min-width: 1.6em; padding: 1px 5px; border-radius: 6px; background: rgba(255, 255, 255, 0.1); border-bottom: 2px solid rgba(255, 255, 255, 0.2); font: 700 11px/1.5 ui-monospace, SFMono-Regular, monospace; text-align: center; color: var(--ink); }

#app { position: fixed; inset: 0; background: linear-gradient(#5d9be0, #cfe3f2 70%); }
#scene { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

/* ---------- Loading ---------- */
.loading { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 14px; background: #1b1f27; }
.loading[hidden] { display: none; }
.loading strong { font-size: 44px; font-style: italic; letter-spacing: -0.03em; }
.loading strong em, .panel h1 em { color: var(--accent); font-style: italic; }
.loading-track { width: 220px; height: 8px; border-radius: 8px; background: rgba(255, 255, 255, 0.12); overflow: hidden; }
.loading-track i { display: block; width: 0; height: 100%; background: var(--accent); transition: width 0.2s; }
.loading p { margin: 0; color: var(--ink-soft); font-size: 14px; }

/* ---------- HUD ---------- */
.hud { position: absolute; top: max(10px, env(safe-area-inset-top)); left: 10px; right: 10px; display: grid; grid-template-columns: 1fr auto 1fr; align-items: start; gap: 10px; pointer-events: none; }
.hud[hidden] { display: none; }
.gauges { display: grid; gap: 6px; justify-self: start; }
.gauge { display: flex; align-items: center; gap: 10px; padding: 7px 12px 7px 8px; border-radius: var(--radius); background: var(--card); backdrop-filter: blur(8px); box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25); min-width: 190px; }
.gauge-body { flex: 1; display: grid; gap: 3px; }
.gauge small { font-size: 10px; font-weight: 800; letter-spacing: 0.1em; text-transform: uppercase; color: var(--ink-soft); }
.bar { height: 8px; border-radius: 8px; background: rgba(255, 255, 255, 0.12); overflow: hidden; }
.bar i { display: block; height: 100%; width: 100%; border-radius: inherit; background: linear-gradient(90deg, var(--danger), #ffb02e 45%, var(--good) 80%); background-size: 190px 100%; transition: width 0.2s; }
.bar--fuel i { background: linear-gradient(90deg, #ff7a2e, #ffc21a); }
#fuel.is-low { animation: pulse 0.8s ease-in-out infinite; }
#fuel.is-low .bar i { background: var(--danger); }
.light { width: 26px; height: 26px; border-radius: 50%; border: 3px solid #0d0f13; box-shadow: inset 0 -3px 6px rgba(0, 0, 0, 0.3), 0 0 14px currentColor; }
.light[data-level="ok"] { background: var(--good); color: rgba(77, 224, 122, 0.6); }
.light[data-level="warn"] { background: #ffb02e; color: rgba(255, 176, 46, 0.6); }
.light[data-level="bad"] { background: var(--danger); color: rgba(255, 77, 61, 0.7); animation: pulse 0.6s ease-in-out infinite; }
.fuel-icon { width: 26px; text-align: center; font-size: 18px; }
@keyframes pulse { 50% { opacity: 0.55; } }

.hud-center { display: grid; grid-auto-flow: column; gap: 8px; align-items: start; justify-items: center; position: relative; }
.stat { display: flex; align-items: center; gap: 7px; padding: 8px 14px; border-radius: var(--radius); background: var(--card); backdrop-filter: blur(8px); box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25); font-size: 22px; font-weight: 900; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.stat span { font-size: 18px; }
.star { color: var(--accent); text-shadow: 0 0 10px rgba(255, 194, 26, 0.8); }
.stat--time.is-low { color: var(--danger); animation: pulse 0.8s ease-in-out infinite; }
.arrow { position: absolute; top: calc(100% + 8px); left: 50%; width: 44px; height: 44px; margin-left: -22px; transition: transform 0.08s linear; filter: drop-shadow(0 3px 5px rgba(0, 0, 0, 0.5)); }
.arrow[hidden] { display: none; }
.arrow svg { width: 100%; height: 100%; }
.arrow path { fill: var(--accent); stroke: #1b1f27; stroke-width: 2.5; stroke-linejoin: round; }
.hud-buttons { display: flex; gap: 8px; pointer-events: auto; justify-self: end; }
.icon-button { width: 44px; height: 44px; border: 0; border-radius: 50%; background: var(--card); backdrop-filter: blur(8px); box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25); font-size: 18px; line-height: 1; }

.corner-left { position: absolute; left: 14px; bottom: max(14px, env(safe-area-inset-bottom)); pointer-events: none; }
.speedo { display: flex; align-items: baseline; gap: 5px; padding: 6px 14px; border-radius: var(--radius); background: var(--card); backdrop-filter: blur(8px); }
.speedo strong { font-size: 30px; font-weight: 900; font-variant-numeric: tabular-nums; font-style: italic; min-width: 2.2em; text-align: right; }
.speedo small { color: var(--ink-soft); font-weight: 800; font-size: 12px; }
.minimap { position: absolute; right: 14px; bottom: max(14px, env(safe-area-inset-bottom)); width: 170px; height: 170px; border-radius: 50%; border: 3px solid rgba(20, 22, 28, 0.9); box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35); pointer-events: none; }
body:not([data-state="driving"]):not([data-state="countdown"]):not([data-state="paused"]) .minimap,
body:not([data-state="driving"]):not([data-state="countdown"]):not([data-state="paused"]) .corner-left { display: none; }

.countdown { position: absolute; left: 50%; top: 38%; transform: translate(-50%, -50%); font-size: 110px; font-weight: 900; font-style: italic; color: var(--accent); text-shadow: 0 6px 0 #1b1f27, 0 10px 30px rgba(0, 0, 0, 0.5); pointer-events: none; animation: pop 1s ease-out infinite; }
.countdown[hidden] { display: none; }
@keyframes pop { 0% { transform: translate(-50%, -50%) scale(1.3); opacity: 0.4; } 25% { transform: translate(-50%, -50%) scale(1); opacity: 1; } }

.toast { position: absolute; left: 50%; top: 24%; transform: translate(-50%, -8px) scale(0.96); padding: 10px 20px; border-radius: 999px; background: var(--card); backdrop-filter: blur(8px); font-size: 20px; font-weight: 800; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3); opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.25s cubic-bezier(0.2, 0.9, 0.3, 1.4); white-space: nowrap; }
.toast.is-visible { opacity: 1; transform: translate(-50%, 0) scale(1); }
.toast.good { color: var(--accent); }
.toast.bad { color: var(--danger); }

/* ---------- Touch ---------- */
.touch { position: absolute; left: 0; right: 0; bottom: calc(max(14px, env(safe-area-inset-bottom)) + 64px); display: flex; justify-content: space-between; align-items: end; padding: 0 14px; pointer-events: none; }
.touch[hidden] { display: none; }
.touch-group { display: flex; gap: 12px; align-items: end; pointer-events: auto; }
.touch button { width: 74px; height: 74px; border: 0; border-radius: 50%; background: rgba(22, 24, 30, 0.6); backdrop-filter: blur(6px); font-size: 26px; font-weight: 800; box-shadow: 0 8px 22px rgba(0, 0, 0, 0.3); touch-action: none; }
.touch button.is-down { background: rgba(255, 255, 255, 0.35); transform: scale(0.94); }
.touch .touch-gas { background: rgba(255, 194, 26, 0.85); color: #1b1f27; }
.touch .touch-small { width: 56px; height: 56px; font-size: 22px; }
@media (max-width: 480px) {
  .touch { padding: 0 10px; }
  .touch-group { gap: 8px; }
  .touch button { width: 62px; height: 62px; font-size: 22px; }
  .touch .touch-small { width: 46px; height: 46px; font-size: 18px; }
}
@media (pointer: coarse) {
  .minimap { width: 120px; height: 120px; top: calc(max(10px, env(safe-area-inset-top)) + 104px); bottom: auto; }
}

/* ---------- Panels ---------- */
.panel { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); width: min(600px, calc(100% - 24px)); max-height: calc(100% - 24px); overflow-y: auto; padding: 24px 26px 22px; border-radius: 24px; background: rgba(22, 24, 30, 0.9); backdrop-filter: blur(12px); box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45); border: 1px solid var(--line); }
.panel[hidden] { display: none; }
.panel--menu { left: max(12px, calc(50% - 560px)); transform: translate(0, -50%); width: min(470px, calc(100% - 24px)); }
.panel--small { width: min(400px, calc(100% - 24px)); text-align: center; }
.eyebrow { margin: 0 0 4px; font-size: 12px; font-weight: 800; letter-spacing: 0.14em; text-transform: uppercase; color: var(--accent); }
.panel h1 { margin: 0 0 8px; font-size: clamp(40px, 8vw, 56px); line-height: 0.95; letter-spacing: -0.04em; font-style: italic; }
.panel h2 { margin: 0 0 10px; font-size: 30px; letter-spacing: -0.02em; font-style: italic; }
.panel-copy { margin: 0 0 16px; color: var(--ink-soft); line-height: 1.45; font-size: 14.5px; }
.label { margin: 4px 0 8px; font-size: 11px; font-weight: 800; letter-spacing: 0.12em; text-transform: uppercase; color: var(--ink-soft); }

.level-grid { display: grid; gap: 8px; margin-bottom: 14px; }
.level-card { position: relative; display: grid; grid-template-columns: 34px 1fr auto; grid-template-rows: auto auto; column-gap: 12px; align-items: center; padding: 11px 14px 11px 10px; border: 0; border-radius: 14px; background: rgba(255, 255, 255, 0.06); text-align: left; box-shadow: inset 0 0 0 1px var(--line); transition: transform 0.15s, box-shadow 0.15s, background-color 0.15s; }
.level-card:hover:not(:disabled) { transform: translateX(3px); background: rgba(255, 194, 26, 0.1); box-shadow: inset 0 0 0 2px var(--accent); }
.level-card:disabled { cursor: default; opacity: 0.45; }
.level-num { grid-row: span 2; display: grid; place-items: center; width: 34px; height: 34px; border-radius: 10px; background: var(--accent); color: #1b1f27; font-weight: 900; font-style: italic; font-size: 18px; }
.level-card strong { font-size: 16px; }
.level-card small { grid-column: 2; color: var(--ink-soft); font-size: 12.5px; line-height: 1.3; }
.level-card em { grid-column: 3; grid-row: 1 / span 2; font-style: normal; font-size: 12px; font-weight: 800; color: var(--accent); font-variant-numeric: tabular-nums; white-space: nowrap; }

.car-grid { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 16px; }
.car-chip { display: inline-flex; align-items: center; gap: 7px; padding: 7px 12px 7px 8px; border: 0; border-radius: 999px; background: rgba(255, 255, 255, 0.06); box-shadow: inset 0 0 0 1px var(--line); font-size: 13px; font-weight: 700; }
.car-chip i { width: 16px; height: 16px; border-radius: 50%; box-shadow: inset 0 0 0 2px rgba(0, 0, 0, 0.3); }
.car-chip:disabled { opacity: 0.45; cursor: default; }
.car-chip[aria-pressed="true"] { box-shadow: inset 0 0 0 2px var(--accent); background: rgba(255, 194, 26, 0.12); }

.keys { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px 10px; margin: 0; font-size: 12px; color: var(--ink-soft); }
.keys div { display: flex; align-items: center; gap: 6px; }
.keys dt { display: flex; gap: 3px; }
.keys dd { margin: 0; }
@media (pointer: coarse) { .keys { display: none; } }
.save-note { margin: 10px 0 0; font-size: 12px; color: var(--ink-soft); }
.save-note:empty { display: none; }

.panel-actions { display: grid; gap: 8px; margin-top: 14px; }
.primary, .secondary, .ghost { padding: 12px 18px; border: 0; border-radius: 14px; font-weight: 800; font-size: 15px; transition: transform 0.12s, background-color 0.15s; }
.primary { background: var(--accent); color: #1b1f27; box-shadow: 0 4px 0 var(--accent-dark); }
.primary:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--accent-dark); }
.secondary { background: rgba(255, 255, 255, 0.1); }
.ghost { background: transparent; color: var(--ink-soft); }
.primary[hidden], .secondary[hidden] { display: none; }
.primary kbd, .secondary kbd { margin-left: 6px; }

.result-stats { display: grid; gap: 4px; margin: 0; padding: 12px 14px; border-radius: 14px; background: rgba(255, 255, 255, 0.05); font-variant-numeric: tabular-nums; }
.result-stats div { display: flex; justify-content: space-between; font-size: 14px; color: var(--ink-soft); }
.result-stats div:last-child { margin-top: 4px; padding-top: 8px; border-top: 1px solid var(--line); color: var(--ink); font-size: 20px; font-weight: 900; }
.result-stats dd { margin: 0; font-weight: 800; }

@media (max-width: 640px) {
  .gauge { min-width: 0; padding: 5px 10px 5px 6px; }
  .gauge small { display: none; }
  .gauges { width: 128px; }
  .light, .fuel-icon { width: 18px; height: 18px; font-size: 14px; }
  .stat { font-size: 17px; padding: 6px 10px; }
  .hud-center { grid-auto-flow: row; gap: 4px; }
  .icon-button { width: 38px; height: 38px; font-size: 16px; }
  .panel--menu { left: 50%; transform: translate(-50%, -50%); }
}
