:root {
  --bg: #11151c;
  --panel: rgba(16, 20, 27, 0.92);
  --line: rgba(255, 255, 255, 0.12);
  --text: #eef2f6;
  --muted: #9aa5b1;
  --accent: #e63946;
  --warn: #ffb703;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text); }
body { -webkit-user-select: none; user-select: none; }
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { opacity: 0.45; cursor: default; }
.hidden { display: none !important; }
.row { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }

#scene { position: fixed; inset: 0; }
#scene canvas { display: block; touch-action: none; }
#fade { position: fixed; inset: 0; z-index: 30; background: #000; opacity: 0; pointer-events: none; transition: opacity 0.4s; }
#fade.on { opacity: 1; }

/* ── Screens ─────────────────────────────────────────────────────────────── */

.screen {
  position: fixed; inset: 0; z-index: 20; overflow-y: auto;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: max(16px, env(safe-area-inset-top)) 16px 24px;
  background: rgba(8, 10, 14, 0.5);
}
#menu { background: linear-gradient(180deg, rgba(8, 10, 14, 0.1), rgba(8, 10, 14, 0.75)); }
.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
  padding: 22px; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.menu-panel { width: min(500px, 100%); display: flex; flex-direction: column; gap: 10px; text-align: center; }
.menu-panel > button, .row > button {
  padding: 11px 12px; border-radius: 10px; font-weight: 600;
  background: rgba(255, 255, 255, 0.06); border: 1px solid var(--line);
}
button.primary { background: linear-gradient(180deg, #ef5560, #c1121f) !important; border: 0 !important; color: #fff; font-weight: 800; }
button.big { font-size: 20px; padding: 14px; letter-spacing: 0.08em; }
h2 { margin: 0; font-size: 24px; letter-spacing: 0.05em; }
.logo { margin: 0; font-size: clamp(40px, 11vw, 64px); font-weight: 900; letter-spacing: 0.08em; line-height: 1; color: #fff; text-shadow: 0 0 24px rgba(230, 57, 70, 0.6); }
.logo span { display: block; font-size: 0.32em; letter-spacing: 0.5em; color: var(--accent); margin-top: 8px; }
.tag { margin: 0; color: var(--muted); }
.career { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.career div { background: rgba(255, 255, 255, 0.05); border-radius: 8px; padding: 6px 4px; }
.career small, #stats small { display: block; font-size: 10px; color: var(--muted); letter-spacing: 0.08em; }
.career b { font-size: 15px; }
.help { text-align: left; color: var(--muted); font-size: 14px; }
.help summary { cursor: pointer; color: var(--text); text-align: center; }
.help ol { padding-left: 20px; }
.help b { color: var(--text); }

/* ── HUD ─────────────────────────────────────────────────────────────────── */

#hud { position: fixed; inset: 0; z-index: 10; pointer-events: none; }
#hud button { pointer-events: auto; }
#stats { position: absolute; top: max(10px, env(safe-area-inset-top)); right: 10px; display: flex; gap: 6px; }
#stats div { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 5px 10px; text-align: center; min-width: 74px; }
#stats b { font-size: 18px; white-space: nowrap; }
#stats b.danger { color: var(--accent); animation: pulse 0.6s infinite; }
@keyframes pulse { 50% { opacity: 0.55; } }
#truck {
  position: absolute; top: calc(max(10px, env(safe-area-inset-top)) + 56px); right: 10px; width: 260px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 7px 10px; font-size: 13px;
}
.truck-head { display: flex; justify-content: space-between; font-weight: 700; }
.truck-head span:last-child { color: var(--muted); font-weight: 400; font-size: 12px; }
.bar { height: 8px; border-radius: 4px; background: rgba(255, 255, 255, 0.1); margin-top: 5px; overflow: hidden; }
#health-fill { display: block; height: 100%; width: 100%; background: #52b788; transition: width 0.2s; }
#hud-footage { margin-top: 4px; font-size: 12px; color: var(--warn); }
#hud-footage:empty { display: none; }

#warning {
  position: absolute; left: 50%; top: max(10px, env(safe-area-inset-top)); transform: translateX(-50%);
  max-width: min(640px, calc(100% - 520px)); padding: 7px 14px; border-radius: 10px; font-size: 14px; text-align: center;
}
#warning.tor { background: rgba(193, 18, 31, 0.92); animation: flash 1s infinite; }
#warning.svr { background: rgba(214, 140, 0, 0.9); color: #111; }
@keyframes flash { 50% { background: rgba(120, 8, 18, 0.92); } }

#radar-box {
  position: absolute; right: 10px; bottom: max(10px, env(safe-area-inset-bottom)); width: 240px;
  background: #0b1016; border: 2px solid rgba(255, 255, 255, 0.22); border-radius: 12px; overflow: hidden;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.5); pointer-events: auto;
}
#radar { display: block; width: 100%; aspect-ratio: 1; }
#radar-bar { display: flex; align-items: center; gap: 4px; padding: 4px; font-size: 11px; color: var(--muted); }
#radar-bar span { flex: 1; text-align: center; white-space: nowrap; overflow: hidden; }
#radar-bar button { background: rgba(255, 255, 255, 0.08); border: 1px solid var(--line); border-radius: 6px; padding: 2px 8px; font-size: 11px; font-weight: 700; }
#radar-box.big { right: 50%; bottom: 50%; transform: translate(50%, 50%); width: min(82vh, calc(100vw - 32px)); z-index: 15; }

#where {
  position: absolute; right: 10px; bottom: calc(max(10px, env(safe-area-inset-bottom)) + 280px);
  background: var(--panel); border-radius: 8px; padding: 3px 9px; font-size: 13px; font-weight: 600; max-width: 300px;
}
#prompt {
  position: absolute; left: 50%; bottom: 40px; transform: translateX(-50%);
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 8px 16px; font-weight: 700;
  white-space: nowrap; max-width: calc(100% - 300px); overflow: hidden; text-overflow: ellipsis;
}
#warp { position: absolute; left: 50%; top: 64px; transform: translateX(-50%); color: var(--warn); font-weight: 900; letter-spacing: 0.1em; text-shadow: 0 2px 6px #000; }
#toast {
  position: absolute; left: 50%; top: 22%; transform: translateX(-50%); width: min(560px, calc(100% - 32px));
  display: flex; flex-direction: column; gap: 4px; align-items: center;
}
#toast div {
  text-align: center; font-size: 17px; font-weight: 800; text-shadow: 0 2px 6px #000;
  animation: toast-in 0.25s; transition: opacity 0.6s;
}
#toast div.out { opacity: 0; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* ── Garage ──────────────────────────────────────────────────────────────── */

.garage-panel { width: min(640px, 100%); display: flex; flex-direction: column; gap: 10px; text-align: center; }
#garage-list { display: flex; flex-direction: column; gap: 8px; text-align: left; }
.truck-card { display: flex; gap: 12px; align-items: center; background: rgba(255, 255, 255, 0.05); border: 1px solid var(--line); border-radius: 10px; padding: 9px 10px; }
.truck-card.using { border-color: #52b788; }
.truck-card > i { flex: none; width: 34px; height: 34px; border-radius: 8px; border: 2px solid rgba(255, 255, 255, 0.3); }
.truck-info { flex: 1; display: flex; flex-direction: column; gap: 2px; }
.truck-info small { color: var(--muted); }
.truck-info span { font-size: 12px; color: var(--warn); }
.truck-card button { flex: none; padding: 8px 12px; border-radius: 8px; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.08); font-weight: 700; }

/* ── House editor ────────────────────────────────────────────────────────── */

#house-ui { position: fixed; inset: 0; z-index: 11; pointer-events: none; }
#house-ui > * { pointer-events: auto; }
#house-top {
  position: absolute; top: max(10px, env(safe-area-inset-top)); left: 10px; right: 10px;
  display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 8px 12px;
}
#house-top small { color: var(--muted); }
#house-top .row button { padding: 7px 10px; }
#house-hint {
  position: absolute; left: 50%; bottom: 128px; transform: translateX(-50%); width: max-content; max-width: calc(100% - 24px);
  background: var(--panel); border-radius: 8px; padding: 5px 12px; font-size: 13px; pointer-events: none; text-align: center;
}
#catalog {
  position: absolute; left: 0; right: 0; bottom: max(8px, env(safe-area-inset-bottom)); display: flex; gap: 6px;
  overflow-x: auto; padding: 6px 10px; scrollbar-width: thin;
}
#catalog button {
  flex: none; width: 92px; display: flex; flex-direction: column; align-items: center; gap: 1px;
  background: var(--panel); border: 2px solid var(--line); border-radius: 10px; padding: 6px 4px; font-size: 24px;
}
#catalog button b { font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
#catalog button small { font-size: 10px; color: var(--muted); }
#catalog button.on { border-color: var(--warn); background: rgba(255, 183, 3, 0.15); }
#catalog button[data-poor] { opacity: 0.5; }

/* Inside a house the editor takes the screen; the driving HUD steps aside. */
body.in-house #stats, body.in-house #truck, body.in-house #where, body.in-house #warning, body.in-house #radar-box, body.in-house #prompt { display: none !important; }

/* ── Touch ───────────────────────────────────────────────────────────────── */

#touch-ui { position: fixed; inset: 0; z-index: 12; pointer-events: none; }
#touch-ui > * { pointer-events: auto; }
#stick {
  position: absolute; left: 22px; bottom: max(22px, env(safe-area-inset-bottom)); width: 140px; height: 140px;
  border-radius: 50%; background: rgba(255, 255, 255, 0.12); border: 2px solid rgba(255, 255, 255, 0.3); touch-action: none;
}
#stick-knob {
  position: absolute; left: 50%; top: 50%; width: 60px; height: 60px; margin: -30px 0 0 -30px; border-radius: 50%;
  background: rgba(255, 255, 255, 0.55);
}
#touch-right { position: absolute; right: 18px; bottom: max(22px, env(safe-area-inset-bottom)); display: flex; gap: 12px; align-items: flex-end; }
#touch-top { position: absolute; left: 10px; top: max(8px, env(safe-area-inset-top)); display: flex; gap: 8px; }
.tb {
  border-radius: 50%; border: 2px solid rgba(255, 255, 255, 0.4); background: rgba(20, 20, 24, 0.6); color: #fff;
  width: 60px; height: 60px; font-size: 24px; font-weight: 800; touch-action: none;
}
.tb.act { width: 80px; height: 80px; font-size: 16px; background: rgba(193, 18, 31, 0.85); }
.tb.act.dim { opacity: 0.4; }
.tb.on { background: rgba(255, 183, 3, 0.7); }
.tb.small { width: 44px; height: 44px; font-size: 18px; }

/* On touch, the radar moves to the top left and the buttons take the bottom right. */
body.touch #radar-box:not(.big) { right: auto; left: 10px; bottom: auto; top: calc(max(8px, env(safe-area-inset-top)) + 52px); width: 170px; }
body.touch #where { right: auto; left: 10px; bottom: auto; top: calc(max(8px, env(safe-area-inset-top)) + 260px); font-size: 11px; max-width: 170px; }
body.touch #stats div { min-width: 56px; padding: 4px 6px; }
body.touch #stats b { font-size: 14px; }
body.touch #truck { width: 210px; font-size: 12px; }
body.touch #warning { top: auto; bottom: calc(max(22px, env(safe-area-inset-bottom)) + 160px); max-width: calc(100% - 20px); font-size: 12px; }

@media (max-width: 760px) {
  #warning { max-width: calc(100% - 20px); top: calc(max(10px, env(safe-area-inset-top)) + 110px); font-size: 12px; }
  #truck { width: 200px; }
  #stats div { min-width: 0; padding: 4px 6px; }
  #stats b { font-size: 14px; }
  #radar-box { width: 180px; }
  #where { bottom: calc(max(10px, env(safe-area-inset-bottom)) + 216px); font-size: 11px; }
  #prompt { max-width: calc(100% - 210px); left: 10px; transform: none; font-size: 12px; white-space: normal; }
  .career { grid-template-columns: repeat(3, 1fr); }
  #toast { top: 40%; }
  #toast div { font-size: 14px; }
  #house-hint { bottom: 118px; font-size: 12px; }
}
