:root {
  --bg: #0b1430;
  --panel: #14213f;
  --panel-2: #1b2b52;
  --line: #2a3d6e;
  --text: #eef3ff;
  --dim: #9fb0d6;
  --accent: #ffcf3d;
  --accent-ink: #2a1d00;
  --good: #4ade80;
  --bad: #ff6b6b;
  --warn: #ffa53d;
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", "Segoe UI", system-ui, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; touch-action: manipulation; overscroll-behavior: none; }
button, input, select { font: inherit; color: inherit; }
h3 { margin: 0 0 6px; font-size: 11px; letter-spacing: 0.12em; text-transform: uppercase; color: var(--dim); }
kbd {
  display: inline-block; min-width: 1.6em; padding: 1px 6px; border-radius: 6px; text-align: center;
  background: #0a1022; border: 1px solid #3a4f86; border-bottom-width: 3px; font: 700 0.82em ui-monospace, monospace; color: #ffe58a;
}
.hidden { display: none !important; }
.spacer { flex: 1; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row.center { justify-content: center; }

.screen { position: fixed; inset: 0; }

/* ───────── garage layout ───────── */
#garage {
  display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr) auto; gap: 10px; padding: 10px;
  background:
    radial-gradient(1200px 500px at 50% -10%, #1d3168 0%, transparent 70%),
    repeating-linear-gradient(0deg, transparent 0 39px, rgba(120, 160, 255, 0.05) 39px 40px),
    repeating-linear-gradient(90deg, transparent 0 39px, rgba(120, 160, 255, 0.05) 39px 40px),
    var(--bg);
}
.topbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.logo { font-size: 22px; font-weight: 800; letter-spacing: 0.04em; margin-right: 10px; white-space: nowrap; }
.logo b { color: var(--accent); }
.logo .gear { display: inline-block; margin-right: 6px; color: var(--accent); animation: spin 9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
#ride-name {
  width: 170px; padding: 7px 10px; border-radius: 10px; border: 1px solid var(--line); background: #0d1730; user-select: text; -webkit-user-select: text;
}
#ride-name:focus { outline: 2px solid var(--accent); }

.btn {
  padding: 7px 12px; border-radius: 10px; border: 1px solid var(--line); border-bottom-width: 3px;
  background: var(--panel-2); cursor: pointer; font-weight: 700; white-space: nowrap;
}
.btn:hover { filter: brightness(1.18); }
.btn:active { transform: translateY(1px); border-bottom-width: 2px; }
.btn.accent { background: var(--accent); color: var(--accent-ink); border-color: #b98a00; }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.danger { border-color: #7a2c2c; color: #ffb3b3; }

.workbench { display: grid; grid-template-columns: 236px minmax(0, 1fr) 250px; gap: 10px; min-height: 0; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 10px; min-height: 0; overflow: auto; }

.tabs { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px; }
.tab { padding: 5px 8px; border-radius: 8px; border: 1px solid transparent; background: #0f1a36; color: var(--dim); cursor: pointer; font-weight: 700; font-size: 12px; }
.tab.on { background: var(--accent); color: var(--accent-ink); }
.part-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
.part {
  display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px 4px 7px; cursor: pointer;
  border-radius: 12px; border: 2px solid transparent; background: #0f1a36;
}
.part img { width: 56px; height: 56px; pointer-events: none; }
.part span { font-size: 12px; font-weight: 700; text-align: center; line-height: 1.1; overflow-wrap: anywhere; }
.part:hover { background: #18274d; }
.part.on { border-color: var(--accent); background: #22335f; }

.bench { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; gap: 8px; min-height: 0; min-width: 0; }
.toolbar { display: flex; gap: 6px; align-items: center; }
.tool { padding: 6px 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); cursor: pointer; font-weight: 700; font-size: 13px; }
.tool:hover { filter: brightness(1.2); }
.tool.on { background: var(--accent); color: var(--accent-ink); border-color: #b98a00; }
.tool.on kbd { color: #ffe58a; }
.canvas-wrap { display: grid; place-items: center; min-height: 0; min-width: 0; overflow: hidden; }
#build-canvas { border-radius: 14px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45), 0 0 0 1px var(--line); touch-action: none; }
.status { min-height: 22px; display: flex; flex-direction: column; gap: 2px; font-size: 13px; text-align: center; }
.warn { color: var(--warn); }
.ok { color: var(--good); }

.side p { margin: 6px 0; font-size: 13px; line-height: 1.35; color: #d5def5; }
.side .hint { color: var(--dim); }
.insp-head { display: flex; align-items: center; gap: 8px; }
.insp-head img { width: 52px; height: 52px; background: #0f1a36; border-radius: 10px; }
.insp-head b { display: block; font-size: 16px; }
.insp-head small { color: var(--dim); }
.field { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 6px 0; font-size: 13px; }
.field label { color: var(--dim); }
.field select, .hud select { padding: 4px 6px; border-radius: 8px; border: 1px solid var(--line); background: #0d1730; }
.bind { padding: 5px 9px; border-radius: 8px; border: 1px solid var(--line); background: #0d1730; cursor: pointer; font-size: 13px; }
.bind.wait { border-color: var(--accent); color: var(--accent); animation: pulse 0.8s infinite alternate; }
@keyframes pulse { to { opacity: 0.5; } }
.check { display: flex; gap: 6px; align-items: center; font-size: 13px; margin: 6px 0; cursor: pointer; }
#inspector { padding-bottom: 10px; margin-bottom: 10px; border-bottom: 1px solid var(--line); }
.legend { display: flex; flex-direction: column; gap: 4px; }
.key-row { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.key-row kbd { min-width: 3.2em; }
.stats { margin-top: 10px; padding-top: 8px; border-top: 1px solid var(--line); }
.stat-line { display: flex; justify-content: space-between; color: var(--dim); font-size: 13px; font-weight: 700; }

/* ───────── launch bar ───────── */
.launch {
  display: grid; grid-template-columns: minmax(0, 1fr) 330px auto; gap: 12px; align-items: stretch;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 10px;
}
.launch-col { min-width: 0; }
.map-list { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: thin; scrollbar-color: var(--line) transparent; }
.map-card {
  position: relative; flex: 0 0 132px; height: 96px; border-radius: 12px; overflow: hidden; cursor: pointer;
  border: 3px solid transparent; padding: 0; background: #000; text-align: left;
}
.map-card canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.map-card .mc-name { position: absolute; left: 0; right: 0; bottom: 0; padding: 12px 7px 5px; font-size: 12px; font-weight: 800; line-height: 1.1; background: linear-gradient(transparent, rgba(4, 8, 20, 0.88)); }
.map-card .mc-icon { position: absolute; top: 4px; left: 6px; font-size: 20px; filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.6)); }
.map-card .mc-best { position: absolute; top: 5px; right: 6px; font-size: 10px; font-weight: 800; padding: 2px 5px; border-radius: 6px; background: rgba(4, 8, 20, 0.7); color: var(--accent); }
.map-card.on { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(255, 207, 61, 0.25); }
#map-blurb { margin: 5px 0 0; font-size: 12.5px; color: var(--dim); min-height: 1.3em; }
.chips { display: flex; flex-wrap: wrap; gap: 5px; margin-bottom: 8px; }
.chip { padding: 4px 8px; border-radius: 999px; border: 1px solid var(--line); background: #0f1a36; cursor: pointer; font-size: 12.5px; font-weight: 700; }
.chip:hover { filter: brightness(1.25); }
.chip.on { background: var(--accent); color: var(--accent-ink); border-color: #b98a00; }
.drive {
  min-width: 150px; border-radius: 16px; border: 0; border-bottom: 6px solid #1a8f47; cursor: pointer;
  background: linear-gradient(#5ff08f, #26c463); color: #05310f; font-size: 30px; font-weight: 900; letter-spacing: 0.04em;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
}
.drive small { font-size: 11px; font-weight: 700; letter-spacing: 0; opacity: 0.7; }
.drive:hover { filter: brightness(1.08); }
.drive:active { transform: translateY(2px); border-bottom-width: 4px; }
.drive.off { filter: grayscale(0.8) brightness(0.7); }
.drive.shake { animation: shake 0.35s; }
@keyframes shake { 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

/* ───────── play ───────── */
#play { background: #000; }
#game { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
.hud { position: absolute; display: flex; gap: 8px; align-items: center; }
.hud.top-left { top: calc(10px + env(safe-area-inset-top)); left: calc(10px + env(safe-area-inset-left)); }
.hud.top-right { top: calc(10px + env(safe-area-inset-top)); right: calc(10px + env(safe-area-inset-right)); }
.hud.top-center {
  top: 10px; left: 50%; transform: translateX(-50%); padding: 7px 14px; border-radius: 999px; gap: 14px;
  background: rgba(8, 14, 32, 0.72); border: 1px solid rgba(255, 255, 255, 0.14); font-weight: 800; white-space: nowrap;
}
#hud-stars { color: var(--accent); letter-spacing: 0.1em; }
#hud-time { font-variant-numeric: tabular-nums; }
.hud .btn, .hud select { background: rgba(8, 14, 32, 0.72); border-color: rgba(255, 255, 255, 0.18); }
.pill { padding: 6px 10px; border-radius: 999px; background: rgba(8, 14, 32, 0.72); border: 1px solid rgba(255, 255, 255, 0.14); font-weight: 800; font-variant-numeric: tabular-nums; min-width: 86px; text-align: center; }
.hud.bottom-left { left: 10px; bottom: 10px; flex-direction: row; flex-wrap: wrap; max-width: 80vw; }
.hud.bottom-left .key-row { padding: 5px 9px; border-radius: 10px; background: rgba(8, 14, 32, 0.66); border: 1px solid rgba(255, 255, 255, 0.12); }
.hud.bottom-left .key-row kbd { min-width: 1.6em; }
.hud.bottom-left .key-row.on { background: var(--accent); color: var(--accent-ink); }
#toast {
  position: absolute; top: 20%; left: 50%; transform: translate(-50%, -10px); padding: 10px 20px; border-radius: 14px; pointer-events: none;
  background: rgba(8, 14, 32, 0.8); font-size: 22px; font-weight: 900; opacity: 0; transition: opacity 0.25s, transform 0.25s; white-space: nowrap;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ───────── overlays ───────── */
.overlay { position: fixed; inset: 0; display: grid; place-items: center; background: rgba(4, 8, 20, 0.6); z-index: 20; padding: 20px; }
#finish { position: absolute; background: rgba(4, 8, 20, 0.35); }
.modal-card {
  position: relative; width: min(880px, 100%); max-height: 100%; overflow: auto; padding: 20px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 18px; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
}
.modal-card h2 { margin: 0 0 12px; }
.close { position: absolute; top: 12px; right: 12px; width: 32px; height: 32px; border-radius: 50%; border: 1px solid var(--line); background: #0f1a36; cursor: pointer; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px; }
.card { display: flex; flex-direction: column; gap: 4px; padding: 8px; border-radius: 14px; border: 2px solid transparent; background: #0f1a36; cursor: pointer; text-align: left; }
.card:hover { border-color: var(--accent); }
.card canvas { width: 100%; height: 96px; background: #1a2f5e; border-radius: 10px; }
.card b { font-size: 15px; }
.card small { color: var(--dim); line-height: 1.25; }
.card .row { margin-top: 4px; }
.empty { color: var(--dim); padding: 20px 0; }
.help { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 14px; user-select: text; -webkit-user-select: text; }
.help h4 { margin: 0 0 4px; color: var(--accent); }
.help p, .help li { margin: 0 0 6px; font-size: 14px; line-height: 1.4; color: #d5def5; }
.help ul { margin: 0; padding-left: 18px; }
.finish-card { width: min(440px, 100%); text-align: center; }
.fin-stars { font-size: 44px; letter-spacing: 0.08em; color: var(--accent); }
.fin-time { font-size: 34px; font-weight: 900; font-variant-numeric: tabular-nums; }
.fin-best { color: var(--dim); margin: 4px 0 16px; }

@media (max-width: 1080px) {
  .workbench { grid-template-columns: 190px minmax(0, 1fr) 210px; }
  .launch { grid-template-columns: minmax(0, 1fr) 250px auto; }
  .drive { min-width: 112px; font-size: 24px; }
  #btn-help, .logo { font-size: 16px; }
  #ride-name { width: 120px; }
  .part img { width: 44px; height: 44px; }
}
@media (max-width: 1180px) {
  .hud.top-center { top: 54px; }
  #toast { top: 26%; }
}
@media (max-width: 900px) {
  .hud .lbl { display: none; }
  .hud.top-right { gap: 5px; }
  .hud select { max-width: 118px; }
  .pill { min-width: 0; padding: 6px 8px; }
}
@media (max-height: 720px) {
  .map-card { height: 78px; }
  .launch h3 { margin-bottom: 4px; }
  .chips { margin-bottom: 5px; }
}

/* narrow windows: stack the panels and let the garage scroll */
@media (max-width: 860px) {
  #garage { display: block; overflow-y: auto; }
  #garage > * { margin-bottom: 10px; }
  .topbar .spacer { display: none; }
  .logo { width: 100%; }
  #ride-name { flex: 1; min-width: 90px; }
  .workbench { grid-template-columns: 148px minmax(0, 1fr); grid-template-rows: clamp(360px, 58vh, 560px) auto; }
  .panel.side { grid-column: 1 / -1; }
  .part-grid { gap: 4px; }
  .palette { padding: 6px; }
  .part { padding: 4px 2px 5px; }
  .part img { width: 38px; height: 38px; }
  .part span { font-size: 10px; }
  .tab { padding: 4px 6px; font-size: 11px; }
  .toolbar { flex-wrap: wrap; }
  .tool { padding: 5px 8px; font-size: 12px; }
  .launch { grid-template-columns: minmax(0, 1fr); }
  .drive { min-height: 62px; flex-direction: row; gap: 10px; }
}

/* ───────── touch controls ───────── */
#touch { position: absolute; inset: 0; pointer-events: none; }
.t-pad { position: absolute; bottom: calc(14px + env(safe-area-inset-bottom)); display: flex; gap: 12px; align-items: flex-end; pointer-events: none; }
.t-left { left: calc(16px + env(safe-area-inset-left)); }
.t-right { right: calc(16px + env(safe-area-inset-right)); flex-wrap: wrap-reverse; justify-content: flex-end; max-width: min(46vw, 300px); }
.t-btn {
  pointer-events: auto; touch-action: none; width: 62px; height: 62px; border-radius: 50%; padding: 2px; cursor: pointer;
  border: 2px solid rgba(255, 255, 255, 0.4); background: rgba(8, 14, 32, 0.5); color: #fff;
  font-size: 11.5px; font-weight: 800; line-height: 1.05; overflow: hidden; -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px);
}
.t-btn.big { width: 78px; height: 78px; font-size: 30px; }
.t-btn.on { background: var(--accent); color: var(--accent-ink); border-color: #fff; transform: scale(0.94); }
.t-btn.lit { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(255, 207, 61, 0.35); }
#play.touch #hud-legend { display: none; }
#play.touch .hud kbd { display: none; }
#play.touch .hud .lbl { display: inline; }
@media (max-height: 460px) {
  .t-btn { width: 54px; height: 54px; font-size: 10.5px; }
  .t-btn.big { width: 68px; height: 68px; }
  .hud.top-center { top: calc(52px + env(safe-area-inset-top)); padding: 4px 12px; font-size: 13px; }
  #play.touch #hud-map { display: none; }
}
/* phones in portrait: palette becomes a strip above the grid */
@media (max-width: 560px) {
  .workbench { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto clamp(300px, 52vh, 460px) auto; }
  .palette { max-height: none; }
  .part-grid { grid-template-columns: none; grid-auto-flow: column; grid-auto-columns: 70px; overflow-x: auto; padding-bottom: 4px; }
  .launch-col.sky .chips { flex-wrap: nowrap; overflow-x: auto; }
  .chip { white-space: nowrap; }
}
#garage { padding-top: calc(10px + env(safe-area-inset-top)); padding-left: calc(10px + env(safe-area-inset-left)); padding-right: calc(10px + env(safe-area-inset-right)); }
.bind-row { display: flex; gap: 6px; align-items: center; }
@media (pointer: coarse) { #bind { display: none; } .tool, .tab, .chip, .btn { min-height: 38px; } }
