/* Turbo Rally — Cartoon Arcade UI (matches approved mockups; zero glassmorphism/AI-slop) */
:root {
  --ink: #101622;
  --panel: #131b2b;
  --surface: #1b263d;
  --yellow: #ffd026;
  --red: #ef3340;
  --blue: #1982fc;
  --green: #22c55e;
  --purple: #9b3de8;
  --white: #ffffff;
  --muted: #cbd5e1;
  --font-display: 'Lilita One', 'Bangers', 'Arial Black', Impact, sans-serif;
  --font-ui: 'Fredoka', 'Nunito', system-ui, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; user-select: none; -webkit-user-select: none; }
html, body { width: 100%; height: 100%; overflow: hidden; background: #14223b; font-family: var(--font-ui); color: var(--white); }

#game-root { position: relative; width: 100%; height: 100%; overflow-x: clip; }
#game-canvas { display: block; width: 100%; height: 100%; }
#ui-root { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
#ui-root.no-world { background: #1982fc; }

button { font-family: inherit; cursor: pointer; }
button:focus-visible { outline: 3px solid var(--yellow); outline-offset: 2px; }

.hidden { display: none !important; }
.screen { position: absolute; inset: 0; pointer-events: auto; }

/* ============================================================================
   TITLE SCREEN (matches cartoon-title-2.png)
   ============================================================================ */
.title-screen {
  display: flex; flex-direction: column; align-items: center; justify-content: space-between;
  padding: 5vh 24px 3.5vh;
  background: linear-gradient(180deg, rgba(16,22,34,0.35) 0%, rgba(16,22,34,0) 35%, rgba(16,22,34,0) 68%, rgba(16,22,34,0.55) 100%);
}

.tr-logo-wrap {
  display: flex; flex-direction: column; align-items: center;
  transform: skewX(-8deg) rotate(-2deg);
  filter: drop-shadow(0 8px 0 var(--ink));
}
.tr-logo-top {
  font-family: var(--font-display);
  font-size: clamp(64px, 11vw, 132px);
  line-height: 0.88; letter-spacing: 0.03em;
  color: var(--yellow);
  -webkit-text-stroke: 6px var(--ink);
  paint-order: stroke fill;
  text-shadow: 0 7px 0 #c97a00, 0 11px 0 var(--ink);
  display: flex; align-items: center; gap: 10px;
}
.tr-flame {
  display: inline-block; width: 0.52em; height: 0.36em;
  background: var(--red); border: 4px solid var(--ink);
  clip-path: polygon(0 20%, 60% 0, 100% 50%, 60% 100%, 0 80%, 22% 50%);
}
.tr-logo-bot {
  font-family: var(--font-display);
  font-size: clamp(54px, 9.2vw, 112px);
  line-height: 0.9; letter-spacing: 0.04em;
  color: var(--white);
  -webkit-text-stroke: 6px var(--ink);
  paint-order: stroke fill;
  text-shadow: 0 7px 0 var(--red), 0 11px 0 var(--ink);
  display: flex; align-items: center; gap: 12px;
  margin-top: -6px;
}
.tr-check {
  display: inline-block; width: 1.05em; height: 0.68em;
  border: 4px solid var(--ink);
  background-image:
    linear-gradient(45deg, var(--ink) 25%, transparent 25%),
    linear-gradient(-45deg, var(--ink) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--ink) 75%),
    linear-gradient(-45deg, transparent 75%, var(--ink) 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
  background-color: var(--white);
}

.tr-start-btn {
  font-family: var(--font-display);
  font-size: clamp(24px, 3.2vw, 38px);
  letter-spacing: 0.06em;
  color: var(--ink);
  background: var(--yellow);
  border: 4px solid var(--ink);
  box-shadow: 0 6px 0 var(--ink);
  padding: 14px 44px;
  transform: skewX(-8deg);
  transition: transform 0.1s ease, box-shadow 0.1s ease;
}
.tr-start-btn:hover { transform: skewX(-8deg) translateY(-2px); box-shadow: 0 8px 0 var(--ink); }
.tr-start-btn:active { transform: skewX(-8deg) translateY(3px); box-shadow: 0 3px 0 var(--ink); }
.tr-start-btn .star { color: var(--red); margin: 0 6px; }

.title-foot {
  width: 100%; max-width: 1240px;
  display: flex; align-items: center; justify-content: space-between;
  font-weight: 700; font-size: 14px; letter-spacing: 0.06em; text-transform: uppercase;
  text-shadow: 2px 2px 0 var(--ink);
}
.key-pill kbd {
  display: inline-block; padding: 2px 8px; margin-right: 4px;
  background: var(--white); color: var(--ink); border: 2px solid var(--ink);
  font-family: var(--font-display); font-size: 13px;
}

/* ============================================================================
   RACER & TRACK SELECT SCREEN (matches cartoon-select-2.png)
   ============================================================================ */
.select-screen {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  padding: 14px 20px 12px;
  gap: 12px;
  background: linear-gradient(90deg, rgba(16,22,34,0.82) 0%, rgba(16,22,34,0.18) 32%, rgba(16,22,34,0.18) 60%, rgba(16,22,34,0.88) 100%);
}

.sel-topbar {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  background: var(--panel);
  border: 3px solid var(--ink);
  box-shadow: 0 5px 0 var(--ink);
  padding: 8px 16px;
}
.sel-brand {
  font-family: var(--font-display); font-size: 24px; letter-spacing: 0.04em;
  transform: skewX(-8deg); padding-right: 12px; border-right: 3px solid var(--red);
}
.sel-brand .b-y { color: var(--yellow); }
.sel-brand .b-w { color: var(--white); }
.sel-heading {
  font-family: var(--font-display); font-size: clamp(20px, 2.5vw, 30px);
  letter-spacing: 0.04em; color: var(--white); margin-right: auto;
  transform: skewX(-5deg);
}

.sel-classes { display: flex; gap: 6px; }
.cls-tab, .laps-btn, .back-btn {
  font-family: var(--font-display); font-size: 16px; letter-spacing: 0.04em;
  padding: 7px 14px; border: 3px solid var(--ink);
  background: var(--surface); color: var(--white);
  transform: skewX(-8deg);
  transition: transform 0.08s ease;
}
.cls-tab.cls-easy.sel { background: var(--yellow); color: var(--ink); }
.cls-tab.cls-normal.sel { background: var(--blue); color: var(--white); }
.cls-tab.cls-hard.sel { background: var(--red); color: var(--white); }
.laps-btn { background: var(--surface); color: var(--yellow); }
.back-btn { background: var(--panel); color: var(--muted); }
.cls-tab:hover, .laps-btn:hover, .back-btn:hover { transform: skewX(-8deg) translateY(-1px); }

.sel-body {
  display: grid;
  grid-template-columns: minmax(250px, 310px) minmax(0, 1fr) minmax(320px, 410px);
  gap: 16px;
  min-height: 0;
}

.col-label {
  font-family: var(--font-display); font-size: 15px; letter-spacing: 0.08em;
  color: var(--yellow); background: var(--ink); padding: 4px 10px;
  display: inline-block; margin-bottom: 6px; transform: skewX(-8deg);
}

/* Left: 3 Track Cards */
.sel-tracks { display: flex; flex-direction: column; gap: 8px; min-height: 0; }
.track-card {
  display: flex; flex-direction: column; text-align: left;
  background: var(--panel); border: 3px solid var(--ink);
  box-shadow: 4px 4px 0 var(--ink);
  padding: 0; overflow: hidden;
  transition: transform 0.1s ease;
}
.track-card:hover { transform: translateX(4px); }
.track-card.sel { border-color: var(--yellow); box-shadow: 0 0 0 2px var(--ink), 5px 5px 0 var(--ink); }
.tc-head {
  display: flex; align-items: center; gap: 8px;
  background: var(--tc-accent, var(--green)); color: var(--ink);
  padding: 4px 10px; border-bottom: 3px solid var(--ink);
  font-family: var(--font-display); font-size: 18px; letter-spacing: 0.04em;
}
.tc-num { background: var(--ink); color: var(--white); padding: 1px 6px; font-size: 14px; }
.tc-art { width: 100%; height: 74px; overflow: hidden; background: #0d1322; }
.tc-art img { display: block; width: 100%; height: 100%; object-fit: cover; }
.tc-meta {
  display: flex; justify-content: space-between;
  padding: 4px 10px; font-size: 12px; font-weight: 700; color: var(--muted);
  background: var(--panel);
}

/* Center: Open live 3D view + selected racer nameplate */
.sel-stage { display: flex; align-items: flex-end; justify-content: center; padding-bottom: 8px; pointer-events: none; }
.stage-badge {
  display: flex; align-items: center; gap: 12px;
  background: var(--panel); border: 3px solid var(--ink);
  border-left: 8px solid var(--c, var(--red));
  box-shadow: 5px 5px 0 var(--ink);
  padding: 8px 18px; transform: skewX(-8deg);
}
.sb-num {
  font-family: var(--font-display); font-size: 32px; color: var(--yellow);
  -webkit-text-stroke: 2px var(--ink); paint-order: stroke fill;
}
.sb-name { font-family: var(--font-display); font-size: 24px; letter-spacing: 0.04em; color: var(--white); line-height: 1; }
.sb-sub { font-size: 12px; font-weight: 700; color: var(--yellow); letter-spacing: 0.06em; margin-top: 2px; }

/* Right: 8 Racer Portraits + Stats + RACE! */
.sel-right { display: flex; flex-direction: column; gap: 8px; min-height: 0; }
.char-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 7px; }
.char-card {
  position: relative; display: flex; flex-direction: column; align-items: center;
  background: var(--panel); border: 3px solid var(--ink);
  box-shadow: 3px 3px 0 var(--ink); padding: 0; overflow: hidden;
  transition: transform 0.08s ease;
}
.char-card:hover { transform: translateY(-2px); }
.char-card.sel { border-color: var(--yellow); box-shadow: 0 0 0 2px var(--ink), 4px 4px 0 var(--ink); }
.p1-tag {
  display: none; position: absolute; top: 3px; left: 3px; z-index: 2;
  background: var(--yellow); color: var(--ink); border: 2px solid var(--ink);
  font-family: var(--font-display); font-size: 11px; padding: 0 5px;
}
.char-card.sel .p1-tag { display: block; }
.cc-av { width: 100%; aspect-ratio: 1; background: var(--c, var(--red)); }
.cc-av img { display: block; width: 100%; height: 100%; object-fit: cover; }
.cc-name {
  width: 100%; text-align: center; padding: 3px 2px;
  font-family: var(--font-display); font-size: 13px; letter-spacing: 0.04em;
  color: var(--white); background: var(--ink);
}

.sel-stats {
  background: var(--panel); border: 3px solid var(--ink);
  box-shadow: 4px 4px 0 var(--ink); padding: 10px 12px;
  display: flex; flex-direction: column; gap: 6px;
}
.tr-stat { display: grid; grid-template-columns: 42px minmax(0, 1fr) 32px; align-items: center; gap: 8px; }
.ts-lbl { font-family: var(--font-display); font-size: 14px; color: var(--white); letter-spacing: 0.05em; }
.ts-num { font-family: var(--font-display); font-size: 13px; color: var(--muted); text-align: right; }
.tr-bar { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; height: 12px; }
.tr-bar i { display: block; background: #26324d; border: 2px solid var(--ink); transform: skewX(-12deg); }
.tr-bar.spd i.on { background: var(--red); }
.tr-bar.acc i.on { background: var(--blue); }
.tr-bar.hdl i.on { background: var(--green); }
.tr-bar.wgt i.on { background: var(--purple); }

.race-btn {
  display: flex; align-items: center; justify-content: center; gap: 14px;
  font-family: var(--font-display); font-size: 32px; letter-spacing: 0.06em;
  color: var(--ink); background: var(--yellow);
  border: 4px solid var(--ink); box-shadow: 0 6px 0 var(--ink);
  padding: 10px 18px; transform: skewX(-6deg);
  transition: transform 0.08s ease, box-shadow 0.08s ease;
}
.race-btn:hover { transform: skewX(-6deg) translateY(-2px); box-shadow: 0 8px 0 var(--ink); }
.race-btn:active { transform: skewX(-6deg) translateY(3px); box-shadow: 0 3px 0 var(--ink); }
.rb-check {
  width: 28px; height: 22px; border: 2px solid var(--ink);
  background-image:
    linear-gradient(45deg, var(--ink) 25%, transparent 25%),
    linear-gradient(-45deg, var(--ink) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--ink) 75%),
    linear-gradient(-45deg, transparent 75%, var(--ink) 75%);
  background-size: 10px 10px;
  background-position: 0 0, 0 5px, 5px -5px, -5px 0;
  background-color: var(--white);
}

.sel-controls {
  display: flex; justify-content: center; flex-wrap: wrap; gap: 16px;
  font-size: 13px; font-weight: 700; color: var(--white);
  background: var(--panel); border: 3px solid var(--ink); padding: 6px 14px;
}
.sel-controls kbd {
  display: inline-block; padding: 1px 6px; margin-right: 4px;
  background: var(--yellow); color: var(--ink); border: 2px solid var(--ink);
  font-family: var(--font-display); font-size: 12px;
}

/* ============================================================================
   PAUSE & LOADING
   ============================================================================ */
.pause-screen, .load-screen {
  display: flex; align-items: center; justify-content: center;
  background: rgba(16, 22, 34, 0.72);
}
.pause-panel, .load-box {
  background: var(--panel); border: 4px solid var(--ink);
  box-shadow: 8px 8px 0 var(--ink); padding: 26px 32px;
  min-width: 320px; display: flex; flex-direction: column; gap: 10px; text-align: center;
}
.pause-title, .load-title {
  font-family: var(--font-display); font-size: 40px; color: var(--yellow);
  -webkit-text-stroke: 3px var(--ink); paint-order: stroke fill;
  transform: skewX(-6deg); margin-bottom: 6px;
}
.load-sub { font-family: var(--font-display); font-size: 20px; color: var(--white); letter-spacing: 0.08em; }
.pause-btn {
  font-family: var(--font-display); font-size: 20px; letter-spacing: 0.04em;
  padding: 10px 18px; border: 3px solid var(--ink);
  background: var(--surface); color: var(--white); transform: skewX(-6deg);
}
.pause-btn.sel, .pause-btn:hover { background: var(--yellow); color: var(--ink); }

/* ============================================================================
   IN-RACE HUD (matches cartoon-hud-2.png)
   ============================================================================ */
.hud { position: absolute; inset: 0; pointer-events: none; }

.hud-tl { position: absolute; top: 16px; left: 20px; display: flex; flex-direction: column; gap: 4px; }
.hud-lap {
  font-family: var(--font-display); font-size: clamp(28px, 3.6vw, 44px); line-height: 1;
  color: var(--white); -webkit-text-stroke: 3px var(--ink); paint-order: stroke fill;
  text-shadow: 0 4px 0 var(--ink); transform: skewX(-8deg);
}
.hud-lap .val { color: var(--yellow); font-size: 1.2em; margin-left: 4px; }
.hud-lap .of { color: var(--white); }
.hud-timer {
  display: inline-block; align-self: flex-start;
  font-family: var(--font-display); font-size: 20px; letter-spacing: 0.06em;
  background: var(--panel); color: var(--white);
  border: 3px solid var(--ink); box-shadow: 3px 3px 0 var(--ink);
  padding: 2px 10px; transform: skewX(-8deg);
}
.hud-splits { font-size: 12px; font-weight: 700; color: var(--yellow); text-shadow: 2px 2px 0 var(--ink); }

/* Top-center angled item slot */
.hud-item { position: absolute; top: 14px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; align-items: center; gap: 4px; }
.item-slot {
  position: relative; width: 92px; height: 92px;
  background: var(--panel); border: 4px solid var(--yellow);
  box-shadow: 0 0 0 3px var(--ink), 5px 5px 0 var(--ink);
  transform: skewX(-8deg);
  display: flex; align-items: center; justify-content: center;
}
.item-img { width: 74px; height: 74px; object-fit: contain; transform: skewX(8deg); }
.item-count {
  position: absolute; bottom: -6px; right: -8px;
  background: var(--red); color: var(--white); border: 3px solid var(--ink);
  font-family: var(--font-display); font-size: 16px; padding: 0 6px;
}
.item-count:empty { display: none; }
.item-hint {
  font-family: var(--font-display); font-size: 11px; letter-spacing: 0.06em;
  background: var(--ink); color: var(--yellow); padding: 1px 8px; transform: skewX(-8deg);
}

/* Compact top-right standings */
.hud-standings { position: absolute; top: 16px; right: 18px; display: flex; flex-direction: column; gap: 3px; width: 156px; }
.st-row {
  display: grid; grid-template-columns: 20px 10px minmax(0, 1fr); align-items: center; gap: 6px;
  background: rgba(19, 27, 43, 0.88); border: 2px solid var(--ink);
  padding: 2px 8px; font-size: 12px; font-weight: 800; transform: skewX(-6deg);
}
.st-row.me { background: var(--yellow); color: var(--ink); border-width: 3px; }
.st-pos { font-family: var(--font-display); text-align: center; }
.st-chip { width: 10px; height: 10px; border: 2px solid var(--ink); }
.st-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Bottom-left minimap */
.hud-minimap {
  position: absolute; bottom: 16px; left: 18px;
  width: 170px; height: 170px;
  background: rgba(19, 27, 43, 0.72); border: 3px solid var(--ink);
  box-shadow: 4px 4px 0 var(--ink);
}
.hud-minimap canvas { width: 100%; height: 100%; display: block; }

/* Bottom-right: giant place + speedo + drift */
.hud-br { position: absolute; bottom: 16px; right: 20px; display: flex; align-items: flex-end; gap: 14px; }
.hud-speedo {
  position: relative; width: 146px; height: 146px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.hud-speedo canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.speed-val {
  position: relative; z-index: 1; margin-top: 10px;
  font-family: var(--font-display); font-size: 38px; line-height: 1; color: var(--white);
  -webkit-text-stroke: 2.5px var(--ink); paint-order: stroke fill;
}
.speed-unit {
  position: relative; z-index: 1; font-family: var(--font-display); font-size: 12px;
  letter-spacing: 0.08em; color: var(--yellow); text-transform: uppercase;
}
.drift-pill {
  position: absolute; bottom: 4px; z-index: 2;
  font-family: var(--font-display); font-size: 12px; letter-spacing: 0.08em;
  background: var(--panel); color: var(--muted); border: 2px solid var(--ink);
  padding: 2px 10px; transform: skewX(-10deg); opacity: 0.35;
}
.drift-pill.l1 { opacity: 1; background: var(--blue); color: var(--white); }
.drift-pill.l2 { opacity: 1; background: #f97316; color: var(--ink); }
.drift-pill.l3 { opacity: 1; background: var(--purple); color: var(--white); }

.hud-place {
  font-family: var(--font-display); line-height: 0.85;
  transform: skewX(-8deg);
  filter: drop-shadow(0 6px 0 var(--ink));
}
.hud-place .num { font-size: clamp(64px, 7.5vw, 96px); -webkit-text-stroke: 5px var(--ink); paint-order: stroke fill; }
.hud-place .suf { font-size: clamp(28px, 3vw, 40px); -webkit-text-stroke: 3.5px var(--ink); paint-order: stroke fill; margin-left: 2px; }

/* Countdown, banners, wrong-way, intro, results */
.hud-countdown {
  position: absolute; top: 34%; left: 50%; transform: translate(-50%, -50%) scale(0.7);
  font-family: var(--font-display); font-size: clamp(90px, 14vw, 150px);
  color: var(--yellow); -webkit-text-stroke: 7px var(--ink); paint-order: stroke fill;
  text-shadow: 0 10px 0 var(--ink); opacity: 0; pointer-events: none;
}
.hud-countdown.show { opacity: 1; transform: translate(-50%, -50%) scale(1); transition: transform 0.14s ease, opacity 0.14s ease; }
.hud-countdown.go { color: var(--green); }

.hud-banner, .hud-finish, .hud-lappop {
  position: absolute; top: 24%; left: 50%; transform: translateX(-50%) skewX(-8deg);
  font-family: var(--font-display); font-size: clamp(36px, 5vw, 60px);
  color: var(--yellow); -webkit-text-stroke: 4px var(--ink); paint-order: stroke fill;
  text-shadow: 0 6px 0 var(--ink); opacity: 0; pointer-events: none;
}
.hud-banner.show, .hud-finish.show, .hud-lappop.show { opacity: 1; }

.hud-wrongway {
  position: absolute; top: 28%; left: 50%; transform: translateX(-50%);
  background: var(--red); color: var(--white); border: 4px solid var(--ink);
  box-shadow: 5px 5px 0 var(--ink); padding: 8px 22px;
  font-family: var(--font-display); font-size: 32px; opacity: 0;
}
.hud-wrongway.show { opacity: 1; }

.hud-flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; }
.hud-flash.show { opacity: 0.75; transition: opacity 0.35s ease; }

.intro-card {
  position: absolute; bottom: 12%; left: 50%; transform: translateX(-50%) skewX(-8deg);
  background: var(--panel); border: 4px solid var(--ink); box-shadow: 6px 6px 0 var(--ink);
  padding: 12px 28px; text-align: center;
}
.ic-sub { font-family: var(--font-display); font-size: 16px; color: var(--yellow); letter-spacing: 0.08em; }
.ic-name { font-family: var(--font-display); font-size: 38px; color: var(--white); line-height: 1.05; }
.ic-skip { font-size: 12px; font-weight: 700; color: var(--muted); margin-top: 4px; }

.results {
  position: absolute; inset: 0; pointer-events: auto;
  display: flex; align-items: center; justify-content: center;
  background: rgba(16, 22, 34, 0.78);
}
.res-card {
  background: var(--panel); border: 4px solid var(--ink); box-shadow: 8px 8px 0 var(--ink);
  padding: 22px 28px; width: min(680px, 92vw); display: flex; flex-direction: column; gap: 12px;
}
.res-title {
  font-family: var(--font-display); font-size: 40px; color: var(--yellow);
  -webkit-text-stroke: 3px var(--ink); paint-order: stroke fill; transform: skewX(-6deg); text-align: center;
}
.res-table { display: flex; flex-direction: column; gap: 5px; }
.res-row {
  display: grid; grid-template-columns: 44px 36px minmax(0, 1fr) 90px; align-items: center; gap: 10px;
  background: var(--surface); border: 2px solid var(--ink); padding: 6px 10px; font-weight: 800;
}
.res-row.me { background: var(--yellow); color: var(--ink); border-width: 3px; }
.res-row img { width: 32px; height: 32px; border: 2px solid var(--ink); display: block; }
.res-btns { display: flex; gap: 12px; justify-content: center; margin-top: 6px; }
.res-btn {
  font-family: var(--font-display); font-size: 22px; padding: 10px 26px;
  border: 3px solid var(--ink); box-shadow: 4px 4px 0 var(--ink);
  background: var(--surface); color: var(--white); transform: skewX(-6deg);
}
.res-btn.primary { background: var(--yellow); color: var(--ink); }

.toast {
  position: absolute; bottom: 18px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: var(--yellow); border: 2px solid var(--yellow);
  font-family: var(--font-display); font-size: 14px; padding: 4px 12px; opacity: 0;
}
.toast.show { opacity: 1; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}
