:root {
  --ink: #fdfbff;
  --muted: #b9b3d6;
  --panel: rgba(27, 20, 64, 0.92);
  --panel-edge: #4b3c9c;
  --pill: rgba(20, 14, 48, 0.72);
  --accent: #ffd23f;
  --accent-ink: #3a2600;
  --good: #7ed957;
  --bad: #ff5a5a;
  --font: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #1b1440; color: var(--ink); font-family: var(--font); }
body { touch-action: none; -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; }
canvas#view { position: fixed; inset: 0; display: block; }
.hidden { display: none !important; }
.muted { color: var(--muted); }

/* ---------- buttons ---------- */
.btn {
  font: inherit; font-weight: 700; color: var(--ink); cursor: pointer;
  background: #3a2d86; border: 2px solid #6a58d0; border-bottom-width: 4px; border-radius: 12px;
  padding: 8px 14px; font-size: 1rem; line-height: 1.1;
  transition: transform 0.06s ease, background 0.15s;
}
.btn:hover { background: #4a3aa6; }
.btn:active { transform: translateY(2px); border-bottom-width: 2px; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: #c79a00; }
.btn.primary:hover { background: #ffe06e; }
.btn.huge { font-size: 1.5rem; padding: 14px 28px; width: 100%; margin-top: 12px; }
.btn.small { font-size: 0.9rem; padding: 6px 10px; }
.btn:disabled { opacity: 0.6; }

/* ---------- panels & screens ---------- */
.panel {
  background: var(--panel); border: 3px solid var(--panel-edge); border-radius: 20px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.45); padding: 20px;
}
.screen, .modal {
  position: fixed; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: safe center;
  padding: 16px; z-index: 20; overflow-y: auto; touch-action: pan-y;
}
.screen { background: radial-gradient(ellipse at top, #7fd3ff 0%, #ff9f5a 45%, #1b1440 100%); }
.modal { background: rgba(10, 6, 30, 0.55); }
h1, h2 { margin: 0 0 12px; }
h1 { font-size: 2.2rem; text-shadow: 0 3px 0 rgba(0, 0, 0, 0.35); }

/* ---------- lobby ---------- */
.lobby-panel { width: min(460px, 100%); }
.lobby-panel label, .lobby-panel .label { display: block; font-weight: 700; margin: 14px 0 6px; }
.lobby-panel small { color: var(--muted); font-weight: 400; }
.tagline { color: var(--muted); margin: -4px 0 8px; }
input {
  font: inherit; font-size: 1.2rem; width: 100%; margin-top: 6px; padding: 10px 12px; color: var(--ink);
  background: #120d33; border: 2px solid #5b4cb0; border-radius: 12px; outline: none;
  -webkit-user-select: text; user-select: text;
}
input:focus { border-color: var(--accent); }
.colors { display: flex; gap: 10px; flex-wrap: wrap; }
.color-btn { width: 44px; height: 44px; border-radius: 50%; border: 4px solid transparent; cursor: pointer; box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.25); }
.color-btn.sel { border-color: #fff; transform: scale(1.12); }
.how { margin-top: 14px; font-size: 0.92rem; color: var(--muted); }
.how p { margin: 4px 0; }

/* ---------- garage ---------- */
.garage {
  position: fixed; inset: 0; z-index: 10; display: flex; flex-direction: column; gap: 8px;
  padding: max(10px, 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));
  background: linear-gradient(#3b2f8f, #1b1440);
}
.garage-top { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.track-card { display: flex; align-items: center; gap: 8px; background: var(--pill); border-radius: 16px; padding: 6px 12px; }
.track-emoji { font-size: 2rem; }
.track-name { font-weight: 800; }
.track-mini { width: 180px; height: 36px; display: block; }
.g-timer { font-size: 2rem; font-weight: 900; font-variant-numeric: tabular-nums; background: var(--pill); border-radius: 999px; padding: 4px 18px; }
.g-timer.urgent { color: var(--bad); animation: pulse 0.5s infinite alternate; }
.g-right { display: flex; align-items: center; gap: 10px; }
#btn-ready { font-size: 1.3rem; padding: 10px 18px; }
#btn-ready.is-ready { background: var(--good); border-color: #4a9a2a; }
.pill {
  background: var(--pill); border-radius: 999px; padding: 5px 12px; backdrop-filter: blur(4px);
  font-weight: 700; display: flex; align-items: center; gap: 6px; white-space: nowrap;
}
.garage-main { flex: 1; min-height: 0; display: flex; gap: 10px; }
.grid-wrap { flex: 1; min-width: 0; min-height: 0; position: relative; touch-action: none; }
.grid-wrap canvas { position: absolute; inset: 0; touch-action: none; }
.side { width: 230px; display: flex; flex-direction: column; gap: 8px; overflow-y: auto; touch-action: pan-y; }
.palette { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.part-btn {
  font: inherit; color: var(--ink); cursor: pointer; position: relative; touch-action: none;
  background: #241c5a; border: 3px solid #5b4cb0; border-radius: 14px; padding: 6px 4px;
  display: flex; flex-direction: column; align-items: center; gap: 2px; min-height: 92px; justify-content: center;
}
.part-btn.sel { border-color: var(--accent); background: #3a2d86; box-shadow: 0 0 12px rgba(255, 210, 63, 0.5); }
.part-btn.empty { opacity: 0.5; }
.part-name { font-size: 0.8rem; font-weight: 700; text-align: center; line-height: 1.1; }
.part-emoji { font-size: 2.2rem; line-height: 48px; }
.part-left { position: absolute; top: 4px; right: 6px; font-size: 0.8rem; font-weight: 900; color: var(--accent); }
.stats { background: var(--pill); border-radius: 14px; padding: 8px 10px; font-size: 0.9rem; }
.stats div { display: flex; justify-content: space-between; gap: 6px; }
.stats b { color: var(--accent); letter-spacing: 1px; }
.g-hint { margin: 0; min-height: 1.3em; text-align: center; font-weight: 700; color: var(--accent); }
.drag-ghost { position: fixed; z-index: 100; pointer-events: none; filter: drop-shadow(0 6px 8px rgba(0, 0, 0, 0.5)); }
.g-wait { position: absolute; left: 50%; bottom: 16px; transform: translateX(-50%); width: min(520px, 92vw); text-align: center; pointer-events: none; }
.g-wait .panel { padding: 12px 16px; }
.g-wait h2 { font-size: 1.2rem; }
.g-wait p { margin: 4px 0; }
.join-url { font-weight: 800; color: var(--accent); word-break: break-all; -webkit-user-select: text; user-select: text; pointer-events: auto; }

@media (orientation: portrait) {
  .garage-main { flex-direction: column; }
  .side { width: auto; flex-direction: row; flex-wrap: wrap; overflow: visible; }
  .palette { grid-template-columns: repeat(5, 1fr); flex: 1 1 100%; }
  .stats { flex: 1; }
  .garage-top { flex-wrap: wrap; }
}
@media (max-height: 500px) {
  .track-mini { width: 120px; height: 26px; }
  .g-timer { font-size: 1.4rem; }
  .part-btn { min-height: 72px; }
  .side { width: 200px; }
}

/* ---------- race HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 5; }
#hud button { pointer-events: auto; }
.hud-top {
  position: absolute; top: 0; left: 0; right: 0; display: flex; justify-content: space-between; align-items: center;
  gap: 12px; padding: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) 0 max(10px, env(safe-area-inset-left));
}
.hud-right { display: flex; gap: 8px; align-items: center; }
.time { font-variant-numeric: tabular-nums; font-size: 1.2rem; min-width: 84px; justify-content: center; }
.progress { flex: 1; position: relative; height: 28px; max-width: 520px; }
.progress-track { position: absolute; left: 0; right: 0; top: 11px; height: 6px; border-radius: 3px; background: rgba(20, 14, 48, 0.55); }
.progress-track .flag { position: absolute; right: -10px; top: -14px; font-size: 1.3rem; }
.dot { position: absolute; top: 2px; width: 24px; height: 24px; margin-left: -12px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4); transition: left 0.1s linear; }
.dot.ghost { opacity: 0.7; transform: scale(0.85); }

.banner {
  position: absolute; top: 30%; left: 50%; transform: translate(-50%, -50%); text-align: center;
  font-size: clamp(2rem, 7vw, 4.5rem); font-weight: 900; color: #fff; white-space: nowrap;
  text-shadow: 0 4px 0 rgba(0, 0, 0, 0.35), 0 0 24px rgba(0, 0, 0, 0.35);
  animation: pop 0.3s ease-out;
}
.banner small { display: block; font-size: 0.4em; font-weight: 700; }

.touch { position: absolute; inset: 0; pointer-events: none; }
.pedal {
  pointer-events: auto; touch-action: none; position: absolute; bottom: max(24px, env(safe-area-inset-bottom));
  width: 130px; height: 130px; border-radius: 28px; font-size: 3.6rem; font-weight: 900; color: #fff;
  background: rgba(20, 14, 48, 0.45); border: 4px solid rgba(255, 255, 255, 0.55); cursor: pointer;
}
.pedal.back { left: max(24px, env(safe-area-inset-left)); }
.pedal.gas { right: max(24px, env(safe-area-inset-right)); background: rgba(126, 217, 87, 0.45); }
.pedal.down { background: rgba(255, 210, 63, 0.7); transform: scale(0.95); }
@media (pointer: fine) { .pedal { width: 90px; height: 90px; font-size: 2.4rem; opacity: 0.7; } }
@media (max-height: 500px) { .pedal { width: 104px; height: 104px; font-size: 2.8rem; } }

/* ---------- rocket button ---------- */
.pedal.boost { left: auto; right: calc(max(24px, env(safe-area-inset-right)) + 150px); background: rgba(255, 123, 57, 0.5); font-size: 3rem; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; }
.pedal.boost.empty { opacity: 0.35; }
.fuel { width: 70%; height: 8px; border-radius: 4px; background: rgba(0, 0, 0, 0.4); overflow: hidden; }
#fuel-fill { height: 100%; width: 100%; background: var(--accent); }
@media (pointer: fine) { .pedal.boost { right: 130px; } }
@media (max-height: 500px) { .pedal.boost { right: calc(max(24px, env(safe-area-inset-right)) + 120px); font-size: 2.3rem; } }

/* ---------- shop ---------- */
.shop-btn { font-size: 1.2rem; padding: 9px 14px; }
.shop-panel { width: min(760px, 100%); max-height: 100%; display: flex; flex-direction: column; }
.shop-head { display: flex; align-items: center; gap: 12px; margin-bottom: 10px; }
.shop-head h2 { margin: 0; flex: 1; }
.shop-head .coins { font-size: 1.3rem; color: var(--accent); }
.shop-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(165px, 1fr)); gap: 10px; overflow-y: auto; touch-action: pan-y; padding: 2px; }
.shop-item { background: #241c5a; border: 3px solid #5b4cb0; border-radius: 16px; padding: 10px; display: flex; flex-direction: column; align-items: center; gap: 4px; text-align: center; }
.shop-item.locked { opacity: 0.55; }
.shop-item .si-name { font-weight: 800; }
.shop-item .si-info { font-size: 0.85rem; color: var(--muted); min-height: 2.2em; }
.shop-item .si-own { font-size: 0.85rem; }
.shop-item .btn { width: 100%; font-size: 1.05rem; }
.shop-item.new { border-color: var(--accent); box-shadow: 0 0 12px rgba(255, 210, 63, 0.4); }
.shop-item.bought { animation: bought 0.4s ease-out; }
@keyframes bought { 0% { transform: scale(1); } 40% { transform: scale(1.08); background: #3d6f2a; } 100% { transform: scale(1); } }

/* ---------- lucky wheel ---------- */
.wheel-modal { background: radial-gradient(ellipse at center, rgba(60, 40, 140, 0.95), rgba(10, 6, 30, 0.97)); z-index: 30; gap: 10px; }
.wheel-title { font-size: clamp(1.6rem, 5vw, 2.6rem); text-align: center; margin: 0; text-shadow: 0 3px 0 rgba(0, 0, 0, 0.35); }
.wheel-msg { min-height: 2.4em; font-size: clamp(1.4rem, 4.5vw, 2.4rem); font-weight: 900; text-align: center; color: var(--accent); }
.wheel-msg.pop { animation: prizepop 0.6s ease-out; }
@keyframes prizepop { 0% { transform: scale(0.3); } 60% { transform: scale(1.3); } 100% { transform: scale(1); } }
.wheel-modal .result-buttons { width: min(420px, 90vw); }
.spin-btn { animation: pulse 0.6s infinite alternate; }
.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 60; }
.result-coins { color: var(--accent); font-weight: 900; margin-left: 6px; }

/* ---------- result ---------- */
.result-panel { width: min(480px, 100%); text-align: center; }
.result-panel h2 { font-size: 2rem; }
.result-panel .big { font-size: 4rem; margin: 6px 0; }
.result-list { list-style: none; padding: 0; margin: 10px 0; text-align: left; }
.result-list li { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-radius: 12px; background: rgba(255, 255, 255, 0.07); margin: 6px 0; font-size: 1.15rem; font-weight: 700; }
.result-list .sw { width: 18px; height: 18px; border-radius: 50%; flex: none; }
.result-list .val { margin-left: auto; color: var(--accent); }
.result-buttons { display: flex; flex-direction: column; gap: 8px; justify-content: center; margin-top: 8px; }
.result-buttons .btn.huge { margin-top: 0; }

/* ---------- portrait hint while racing (tablets and phones only) ---------- */
.rotate { display: none; }
@media (orientation: portrait) and (pointer: coarse) {
  body.racing .rotate {
    display: flex; position: fixed; inset: 0; z-index: 50; flex-direction: column; align-items: center; justify-content: center;
    background: #1b1440; font-size: 1.4rem; font-weight: 700;
  }
  .rotate div { font-size: 4rem; animation: tilt 1.6s ease-in-out infinite; }
}

@keyframes pop { from { transform: translate(-50%, -50%) scale(0.6); opacity: 0; } to { transform: translate(-50%, -50%) scale(1); opacity: 1; } }
@keyframes pulse { from { opacity: 1; } to { opacity: 0.55; } }
@keyframes tilt { 0%, 100% { transform: rotate(0); } 50% { transform: rotate(-90deg); } }
