:root {
  --ink: #fff4e6;
  --ink-soft: rgba(255, 238, 214, 0.72);
  --glass: rgba(40, 22, 12, 0.46);
  --glass-strong: rgba(36, 20, 12, 0.72);
  --line: rgba(255, 226, 190, 0.18);
  --accent: #ff7a2e;
  --accent-dark: #e05a14;
  --gold: #ffcf4a;
  --radius: 18px;
  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: #d99260; }
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(--gold); outline-offset: 2px; }
canvas:focus { outline: none; }
kbd { display: inline-block; min-width: 1.6em; padding: 1px 5px; border-radius: 6px; background: rgba(255, 240, 220, 0.14); border-bottom: 2px solid rgba(255, 240, 220, 0.25); font: 700 11px/1.5 ui-monospace, SFMono-Regular, monospace; text-align: center; color: var(--ink); }

#app { position: fixed; inset: 0; background: linear-gradient(#5a7fb8, #f3c495 62%, #d9a676); }
#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; gap: 12px; text-align: center; background: radial-gradient(circle at 50% 60%, rgba(255, 210, 150, 0.35), transparent 60%); transition: opacity 0.6s; }
.loading.is-done { opacity: 0; pointer-events: none; }
.loading-bar { width: min(320px, 70vw); height: 6px; border-radius: 6px; background: rgba(60, 30, 14, 0.35); overflow: hidden; }
.loading-bar i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--gold), var(--accent)); transition: width 0.25s; }
.loading-text { margin: 0; font-weight: 700; color: rgba(60, 30, 14, 0.75); }
.loading .eyebrow { color: rgba(60, 30, 14, 0.8); }

/* HUD */
.hud { position: absolute; top: max(12px, env(safe-area-inset-top)); left: 12px; right: 12px; display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 14px; pointer-events: none; }
.hud[hidden] { display: none; }
.hud-card { display: grid; gap: 0; padding: 8px 14px; border-radius: var(--radius); background: var(--glass); backdrop-filter: blur(10px) saturate(1.2); -webkit-backdrop-filter: blur(10px) saturate(1.2); box-shadow: 0 6px 22px rgba(40, 16, 4, 0.18), inset 0 0 0 1px var(--line); min-width: 112px; }
.hud-level { font-size: 12px; font-weight: 700; color: var(--ink-soft); letter-spacing: 0.02em; }
.hud-time { font-size: 26px; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; line-height: 1.1; }
.hud-right { display: flex; gap: 8px; align-items: center; pointer-events: auto; }
.hud-stars { grid-auto-flow: column; align-items: center; gap: 8px; min-width: 0; padding: 10px 14px; }
.hud-stars strong { font-size: 22px; font-variant-numeric: tabular-nums; }
.star-icon { color: var(--gold); font-size: 22px; text-shadow: 0 0 12px rgba(255, 200, 60, 0.7); }
.hud-stars.is-pop { animation: pop 0.35s cubic-bezier(0.2, 0.9, 0.3, 1.4); }
@keyframes pop { 40% { transform: scale(1.14); } }
.icon-button { width: 44px; height: 44px; border: 0; border-radius: 50%; background: var(--glass); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); box-shadow: 0 6px 22px rgba(40, 16, 4, 0.18), inset 0 0 0 1px var(--line); font-size: 14px; font-weight: 800; letter-spacing: -2px; line-height: 1; }

.progress { position: relative; height: 8px; max-width: 520px; width: 100%; justify-self: center; border-radius: 8px; background: rgba(40, 22, 12, 0.32); box-shadow: inset 0 0 0 1px var(--line); }
.progress > i { position: absolute; left: 0; top: 0; bottom: 0; width: 0; border-radius: inherit; background: linear-gradient(90deg, var(--gold), var(--accent)); }
.progress > b { position: absolute; top: 50%; left: 0; width: 16px; height: 16px; border-radius: 50%; transform: translate(-50%, -50%); background: var(--ink); box-shadow: 0 0 0 3px var(--accent), 0 2px 8px rgba(0, 0, 0, 0.3); }
.progress-ticks span { position: absolute; top: -3px; width: 4px; height: 14px; margin-left: -2px; border-radius: 3px; background: rgba(255, 240, 220, 0.45); }
.progress-ticks span.is-passed { background: #8fe07a; }
.progress-ticks span.is-service { width: 7px; height: 18px; top: -5px; background: var(--gold); }
.progress-ticks span.is-finish { background: repeating-linear-gradient(#1b120c 0 3px, #fff 3px 6px); width: 6px; margin-left: -3px; }

.toast { position: absolute; left: 50%; top: 20%; transform: translate(-50%, -8px) scale(0.96); padding: 10px 22px; border-radius: 999px; background: var(--glass-strong); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); font-size: 22px; font-weight: 800; letter-spacing: -0.01em; box-shadow: 0 10px 30px rgba(40, 16, 4, 0.25); 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.is-bad { color: #ffb4a0; }
.toast.is-good { color: var(--gold); }

.start-hint { position: absolute; left: 50%; bottom: max(26px, env(safe-area-inset-bottom)); transform: translateX(-50%); margin: 0; padding: 10px 18px; border-radius: 999px; background: var(--glass-strong); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); font-weight: 700; box-shadow: 0 8px 24px rgba(40, 16, 4, 0.2); animation: bob 1.4s ease-in-out infinite; }
.start-hint[hidden] { display: none; }
@keyframes bob { 0%, 100% { transform: translate(-50%, 0); } 50% { transform: translate(-50%, -4px); } }

.touch { position: absolute; left: 0; right: 0; bottom: max(14px, env(safe-area-inset-bottom)); display: flex; justify-content: space-between; padding: 0 14px; pointer-events: none; }
.touch[hidden] { display: none; }
.touch-group { display: flex; gap: 12px; pointer-events: auto; }
.touch button { width: 76px; height: 76px; border: 0; border-radius: 50%; background: rgba(40, 22, 12, 0.42); backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px); font-size: 30px; font-weight: 800; box-shadow: 0 8px 22px rgba(40, 16, 4, 0.2), inset 0 0 0 1px var(--line); touch-action: none; }
.touch button.is-down { background: rgba(255, 240, 220, 0.35); transform: scale(0.94); }
.touch .touch-gas { background: rgba(255, 122, 46, 0.85); color: #fff; }
.touch .touch-gas.is-down { background: var(--accent-dark); }

/* 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: 26px 28px 22px; border-radius: 26px; background: linear-gradient(160deg, rgba(48, 26, 14, 0.78), rgba(30, 16, 10, 0.72)); backdrop-filter: blur(16px) saturate(1.3); -webkit-backdrop-filter: blur(16px) saturate(1.3); box-shadow: 0 30px 80px rgba(30, 10, 2, 0.4), inset 0 0 0 1px var(--line); }
.panel[hidden] { display: none; }
.panel--small { width: min(390px, calc(100% - 24px)); text-align: center; }
.panel--menu { left: auto; right: max(4vw, 16px); transform: translateY(-50%); width: min(480px, calc(100% - 24px)); }
@media (max-width: 760px) { .panel--menu { left: 50%; right: auto; transform: translate(-50%, -50%); } }
.eyebrow { margin: 0 0 4px; font-size: 12px; font-weight: 800; letter-spacing: 0.16em; text-transform: uppercase; color: var(--gold); }
h1 { margin: 0; font-size: clamp(38px, 6vw, 54px); line-height: 1; letter-spacing: -0.03em; }
h1 em { font-style: normal; background: linear-gradient(180deg, var(--gold), var(--accent)); -webkit-background-clip: text; background-clip: text; color: transparent; }
h2 { margin: 0 0 10px; font-size: 32px; letter-spacing: -0.02em; }
.panel-copy { margin: 10px 0 16px; color: var(--ink-soft); line-height: 1.45; }

.level-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
@media (max-width: 460px) { .level-grid { grid-template-columns: repeat(2, 1fr); } }
.level { position: relative; display: grid; gap: 2px; padding: 10px 10px 9px; border: 0; border-radius: 14px; background: rgba(255, 240, 220, 0.08); box-shadow: inset 0 0 0 1px var(--line); text-align: left; transition: transform 0.15s, background 0.15s; }
.level:hover:not(:disabled) { background: rgba(255, 240, 220, 0.16); transform: translateY(-1px); }
.level:disabled { opacity: 0.4; cursor: not-allowed; }
.level.is-current { box-shadow: inset 0 0 0 2px var(--accent); }
.level-num { font-size: 11px; font-weight: 800; color: var(--ink-soft); }
.level-name { font-size: 14px; font-weight: 800; line-height: 1.15; }
.level-stars { font-size: 13px; letter-spacing: 1px; color: rgba(255, 240, 220, 0.25); }
.level-stars b { color: var(--gold); font-weight: 400; }
.level-best { font-size: 11px; color: var(--ink-soft); font-variant-numeric: tabular-nums; }

.menu-row { display: flex; gap: 8px; margin-top: 10px; }
.primary, .secondary, .ghost { padding: 12px 18px; border: 0; border-radius: 14px; font-weight: 800; }
.primary { background: linear-gradient(180deg, #ff9445, var(--accent)); color: #fff; box-shadow: 0 8px 20px rgba(255, 110, 30, 0.35); }
.secondary { background: rgba(255, 240, 220, 0.12); box-shadow: inset 0 0 0 1px var(--line); }
.random-stage { position: relative; display: flex; align-items: center; gap: 14px; width: 100%; padding: 12px 16px 12px 12px; border: 0; border-radius: 16px; overflow: hidden; text-align: left; color: #fff; background: linear-gradient(180deg, #ff9445, var(--accent) 55%, var(--accent-dark)); box-shadow: 0 10px 24px rgba(255, 110, 30, 0.32), inset 0 1px 0 rgba(255, 255, 255, 0.35), inset 0 -2px 0 rgba(120, 40, 0, 0.25); transition: transform 0.16s ease, box-shadow 0.16s ease, filter 0.16s ease; }
.random-stage::after { content: ""; position: absolute; inset: 0; background: linear-gradient(105deg, transparent 35%, rgba(255, 255, 255, 0.28) 50%, transparent 65%); transform: translateX(-100%); pointer-events: none; }
.random-stage__dice { position: relative; flex: none; width: 40px; height: 40px; border-radius: 10px; background: #fff4e6; box-shadow: 0 3px 0 rgba(120, 40, 0, 0.35), inset 0 -2px 0 rgba(200, 120, 60, 0.35); transform: rotate(-8deg); transition: transform 0.3s cubic-bezier(0.3, 1.6, 0.5, 1); }
.random-stage__dice i { position: absolute; width: 7px; height: 7px; border-radius: 50%; background: var(--accent-dark); }
.random-stage__dice i:nth-child(1) { left: 7px; top: 7px; }
.random-stage__dice i:nth-child(2) { right: 7px; top: 7px; }
.random-stage__dice i:nth-child(3) { left: 50%; top: 50%; transform: translate(-50%, -50%); }
.random-stage__dice i:nth-child(4) { left: 7px; bottom: 7px; }
.random-stage__dice i:nth-child(5) { right: 7px; bottom: 7px; }
.random-stage__text { display: flex; flex: 1; flex-direction: column; gap: 2px; min-width: 0; }
.random-stage__text b { font-size: 17px; font-weight: 800; letter-spacing: -0.01em; }
.random-stage__text small { font-size: 12px; font-weight: 600; color: rgba(255, 246, 236, 0.82); }
.random-stage__go { flex: none; font-size: 20px; font-weight: 800; transition: transform 0.16s ease; }
@media (hover: hover) {
  .random-stage:hover { transform: translateY(-1px); filter: brightness(1.05); box-shadow: 0 14px 30px rgba(255, 110, 30, 0.42), inset 0 1px 0 rgba(255, 255, 255, 0.35), inset 0 -2px 0 rgba(120, 40, 0, 0.25); }
  .random-stage:hover::after { transform: translateX(100%); transition: transform 0.7s ease; }
  .random-stage:hover .random-stage__dice { transform: rotate(14deg) scale(1.05); }
  .random-stage:hover .random-stage__go { transform: translateX(3px); }
}
.random-stage:active { transform: translateY(1px) scale(0.99); filter: brightness(0.97); }
@media (prefers-reduced-motion: reduce) { .random-stage, .random-stage__dice, .random-stage__go { transition: none; } .random-stage:hover::after { transition: none; } }
.ghost { background: transparent; color: var(--ink-soft); }
.panel-actions { display: grid; gap: 8px; margin-top: 16px; }

.keys { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px 14px; margin: 16px 0 0; font-size: 13px; }
.keys div { display: flex; align-items: center; gap: 8px; }
.keys dt { display: flex; gap: 3px; min-width: 64px; }
.keys dd { margin: 0; color: var(--ink-soft); }
.save-note { margin: 10px 0 0; font-size: 12px; color: #ffb4a0; }
.save-note:empty { display: none; }
.credits { margin: 12px 0 0; font-size: 11px; color: rgba(255, 238, 214, 0.45); }

.stars { margin: 0; font-size: 44px; letter-spacing: 6px; color: rgba(255, 240, 220, 0.2); }
.stars b { color: var(--gold); font-weight: 400; text-shadow: 0 0 18px rgba(255, 200, 60, 0.6); }
.finish-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 14px; margin: 14px 0 0; text-align: left; }
.finish-stats div { padding: 8px 12px; border-radius: 12px; background: rgba(255, 240, 220, 0.08); }
.finish-stats dt { font-size: 11px; font-weight: 800; color: var(--ink-soft); text-transform: uppercase; letter-spacing: 0.08em; }
.finish-stats dd { margin: 0; font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; }
.finish-note { margin: 10px 0 0; color: var(--gold); font-weight: 700; }
.finish-note:empty { display: none; }

.fps { position: absolute; left: 12px; bottom: 12px; padding: 4px 8px; border-radius: 8px; background: rgba(0, 0, 0, 0.45); font: 600 11px/1.3 ui-monospace, monospace; white-space: pre; pointer-events: none; }

@media (prefers-reduced-motion: reduce) {
  .start-hint, .hud-stars.is-pop { animation: none; }
}

@media (pointer: coarse) {
  .keys { display: none; }
}

/* rally HUD */
.hud-left { min-width: 170px; }
.hud-stage { font-size: 12px; font-weight: 700; color: var(--ink-soft); letter-spacing: 0.02em; }
.hud-row { display: flex; align-items: baseline; gap: 12px; }
.hud-pos { font-size: 34px; line-height: 1.05; letter-spacing: -0.03em; }
.hud-pos small { font-size: 16px; color: var(--ink-soft); font-weight: 700; }
.hud-gap { font-size: 12px; font-weight: 700; color: var(--gold); font-variant-numeric: tabular-nums; min-height: 1em; }
.minimap { width: 150px; height: 150px; border-radius: 18px; background: rgba(40, 22, 12, 0.32); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); box-shadow: 0 6px 22px rgba(40, 16, 4, 0.18), inset 0 0 0 1px var(--line); }
.hud-right { align-items: flex-start; }
.hud-speed { position: absolute; right: 20px; bottom: max(20px, env(safe-area-inset-bottom)); font-size: 44px; font-weight: 800; font-variant-numeric: tabular-nums; letter-spacing: -0.03em; text-shadow: 0 2px 14px rgba(40, 16, 4, 0.45); pointer-events: none; }
.hud-speed small { font-size: 16px; font-weight: 700; color: var(--ink-soft); }
#hud[hidden] ~ .hud-speed { display: none; }
.countdown { position: absolute; left: 50%; top: 34%; transform: translate(-50%, -50%); font-size: clamp(80px, 16vw, 160px); font-weight: 900; letter-spacing: -0.05em; text-shadow: 0 8px 40px rgba(40, 16, 4, 0.5); pointer-events: none; }
.countdown[hidden] { display: none; }
.results { list-style: none; margin: 12px 0 0; padding: 0; display: grid; gap: 4px; text-align: left; }
.results li { display: grid; grid-template-columns: 22px 12px 1fr auto; align-items: center; gap: 10px; padding: 7px 12px; border-radius: 12px; background: rgba(255, 240, 220, 0.07); font-variant-numeric: tabular-nums; }
.results li.is-you { background: rgba(255, 122, 46, 0.22); box-shadow: inset 0 0 0 1px rgba(255, 122, 46, 0.55); }
.results span { font-weight: 800; color: var(--ink-soft); }
.results i { width: 12px; height: 12px; border-radius: 50%; box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.25); }
.results em { font-style: normal; font-weight: 700; color: var(--ink-soft); }
.level-grid { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 460px) { .level-grid { grid-template-columns: repeat(2, 1fr); } .minimap { width: 96px; height: 96px; } .hud-speed { font-size: 32px; bottom: 110px; } }

/* Fuel, condition and live service stops. */
.resources { position: absolute; left: 16px; bottom: max(20px, env(safe-area-inset-bottom)); width: 204px; padding: 12px 14px; border-radius: 16px; background: var(--glass-strong); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); pointer-events: none; }
.resources[hidden], .service-working[hidden], .service-choices[hidden] { display: none; }
.resource-row { display: flex; justify-content: space-between; gap: 14px; font-size: 12px; }
.resource-row strong, .service-status b { font-variant-numeric: tabular-nums; }
progress { display: block; width: 100%; height: 5px; margin: 5px 0 9px; appearance: none; -webkit-appearance: none; border: 0; border-radius: 4px; overflow: hidden; background: rgba(255,255,255,0.12); accent-color: var(--gold); }
progress::-webkit-progress-bar { background: rgba(255,255,255,0.12); }
progress::-webkit-progress-value { background: var(--gold); border-radius: 4px; }
progress::-moz-progress-bar { background: var(--gold); border-radius: 4px; }
.resources.is-low { box-shadow: inset 0 0 0 1px #d6855b; }
.resources p { margin: 0; font-size: 12px; line-height: 1.4; }
.resources .resource-hint { margin-top: 3px; font-size: 11px; color: var(--ink-soft); }
.panel--service { left: auto; right: 24px; transform: translateY(-50%); width: min(370px, calc(100% - 24px)); padding: 24px; }
.panel--service h2 { font-size: 30px; }
.service-status { display: flex; justify-content: space-between; gap: 12px; margin: 16px 0; font-size: 13px; }
.service-status span { color: var(--ink-soft); }
.service-status b { display: block; margin-top: 3px; color: var(--ink); font-size: 22px; }
.fuel-plan { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; margin: 0 0 10px; padding: 4px; border-radius: 14px; background: rgba(255, 240, 220, 0.08); box-shadow: inset 0 0 0 1px var(--line); }
.fuel-plan[hidden] { display: none; }
.fuel-plan button { padding: 7px 4px; border: 0; border-radius: 10px; background: transparent; font-size: 12px; font-weight: 700; line-height: 1.2; transition: background 0.15s ease, color 0.15s ease; }
.fuel-plan button small { display: block; margin-top: 2px; font-size: 11px; font-weight: 600; opacity: 0.7; font-variant-numeric: tabular-nums; }
.fuel-plan button[aria-checked="true"] { background: var(--accent); color: #fff; box-shadow: 0 4px 12px rgba(255, 110, 30, 0.3); }
.fuel-plan button[aria-checked="true"] small { opacity: 0.9; }
.fuel-plan button:disabled { opacity: 0.35; cursor: default; }
.service-choices { display: grid; gap: 9px; }
.service-choices button { display: flex; align-items: center; justify-content: space-between; gap: 12px; text-align: left; }
.service-choices button:disabled { opacity: 0.4; cursor: default; }
.service-choices small { display: block; margin-top: 3px; font-size: 11px; font-weight: 500; opacity: 0.75; }
.service-choices b { flex-shrink: 0; font-size: 15px; font-variant-numeric: tabular-nums; }
.service-choices .ghost { justify-content: center; font-size: 13px; }
.service-working { text-align: center; }
.service-working > p:first-child { margin: 10px 0 6px; color: var(--gold); font-weight: 700; }
.service-working > strong { display: block; margin: 12px 0 20px; font-size: 48px; font-variant-numeric: tabular-nums; }
.service-working small { display: block; font-size: 12px; color: var(--ink-soft); }
.results b small { display: block; margin-top: 2px; font-size: 10px; font-weight: 500; color: var(--ink-soft); }
@media (max-width: 600px) {
  .resources { left: 12px; bottom: 108px; width: 164px; padding: 9px 11px; }
  .resource-row { font-size: 11px; }
  .resources p { font-size: 11px; }
  .panel--service { left: 50%; right: auto; transform: translate(-50%, -50%); padding: 20px; max-height: calc(100% - 24px); background: rgba(36, 20, 12, 0.9); }
  .hud-left { min-width: 128px; padding: 8px 10px; }
  .hud-time { font-size: 20px; }
  .hud-gap { font-size: 10px; }
  .hud { gap: 8px; }
}
@media (max-height: 500px) {
  .resources { bottom: 92px; width: 160px; padding: 8px 10px; }
  .panel--service { padding: 14px 18px; }
  .panel--service .panel-copy { margin: 6px 0; font-size: 12px; }
  .service-status { margin: 8px 0; }
  .service-choices { gap: 5px; }
  .service-choices button { padding: 9px 12px; }
}
