:root {
  --bg: #0e1218;
  --card: rgba(14, 18, 26, 0.86);
  --text: #f2f4f7;
  --muted: #a6adbb;
  --accent: #ffcf3f;
  --accent-text: #1a1400;
  --line: rgba(255, 255, 255, 0.14);
  --hover: rgba(255, 255, 255, 0.14);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
}

canvas {
  position: fixed;
  inset: 0;
  /* The visible viewport (100vh on phones reaches under the browser bars). */
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

#hud { pointer-events: none; }

.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  padding: 16px 16px 16px max(16px, 6vw);
  background: linear-gradient(90deg, rgba(5, 8, 12, 0.55), rgba(5, 8, 12, 0) 60%);
}

/* Splash: the title sits top-left in the picture, so crop from the left. */
#loading {
  justify-content: center;
  align-items: flex-end;
  padding: 16px 16px 8vh;
  background: var(--bg) url("splash.webp") left center / cover no-repeat;
}
#loading .card { width: auto; padding: 10px 22px; border-radius: 999px; }
@media (max-aspect-ratio: 1/1) {
  #loading { background-size: contain; }
}

/* First visit: the language picker on the splash. */
#loading:has(#lang-picker:not([hidden])) { align-items: center; }
#loading #lang-picker { width: min(440px, 100%); padding: 24px; border-radius: 16px; }
#lang-picker h2 { text-align: center; }
.lang-list { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.lang-list button:first-child { grid-column: 1 / -1; }

/* Language switch at the bottom of the main screen. */
.lang-switch { flex-direction: row; align-items: center; gap: 8px; margin: 16px 0 0; }
.lang-switch .globe { font-size: 18px; }
.lang-switch select { flex: 1; min-width: 0; }

#menu[data-screen="pause"] { justify-content: center; padding: 16px; background: rgba(5, 8, 12, 0.5); }
#menu[data-screen="pause"] .card { width: min(320px, 100%); }

.overlay[hidden], .card[hidden] { display: none; }

.card.wide { width: min(460px, 100%); }

@media (max-width: 640px) {
  .overlay { justify-content: center; padding: 16px; background: rgba(5, 8, 12, 0.4); }
}

.card {
  width: min(400px, 100%);
  max-height: 100%;
  overflow-y: auto;
  padding: 24px;
  border-radius: 16px;
  background: var(--card);
  border: 1px solid var(--line);
  backdrop-filter: blur(6px);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
}

h1 {
  margin: 0 0 22px;
  font-size: 38px;
  font-weight: 900;
  letter-spacing: 0.5px;
  color: var(--accent);
}

h2 { margin: 0 0 16px; font-size: 22px; font-weight: 800; }

.header { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; }
.header h2 { margin: 0; }

.stack { display: flex; flex-direction: column; gap: 10px; }

label, .field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 14px;
  color: var(--muted);
  font-size: 13px;
}

.row { display: flex; gap: 10px; }
.row label { flex: 1; min-width: 0; }

select, input {
  width: 100%;
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.07);
  color: var(--text);
  font: inherit;
}

select option { background: #1b212b; }

button {
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.08);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

button:hover:not(:disabled) { background: var(--hover); }
button:disabled { cursor: not-allowed; color: var(--muted); opacity: 0.7; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

button.big { width: 100%; padding: 13px 16px; font-size: 16px; }

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 800;
}

button.primary:hover { filter: brightness(1.08); background: var(--accent); }

button.icon { width: 36px; height: 36px; padding: 0; font-size: 18px; flex: none; }

button.link {
  display: block;
  margin: 10px auto 0;
  padding: 4px 8px;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
  text-decoration: underline;
}

button.link:hover { color: var(--text); background: none; }

.badge {
  margin-left: 6px;
  padding: 2px 7px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
  font-size: 11px;
  font-weight: 600;
  vertical-align: middle;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  padding: 4px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--line);
}

.segmented.four { grid-template-columns: repeat(4, 1fr); }
.segmented.two { grid-template-columns: 1fr 2fr; }
.segmented.halves { grid-template-columns: 1fr 1fr; }

.segmented button {
  padding: 8px 4px;
  border: none;
  border-radius: 7px;
  background: none;
  color: var(--muted);
  font-size: 14px;
}

.segmented button[aria-checked="true"] {
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 800;
}

.help {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 12.5px;
}

.help b { color: var(--text); font-weight: 600; }

.error { color: #ff8080; margin: 12px 0 0; }

.hint { color: var(--muted); font-size: 12.5px; min-height: 1.4em; }

.names { margin: 0 0 14px; color: var(--muted); font-size: 13px; }
.names summary { cursor: pointer; margin-bottom: 10px; }
.names summary:hover { color: var(--text); }

.volume { margin: 18px 0 0; }
.graphics { margin: 12px 0 0; }
.volume-row { display: flex; align-items: center; gap: 10px; }
.volume-row input { flex: 1; min-width: 0; }
.volume.muted input { opacity: 0.4; }
input[type="range"] { padding: 0; accent-color: var(--accent); background: none; border: none; }

[hidden] { display: none !important; }

/* --- Track library --- */

.track-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.track-list li {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 4px 4px 10px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
}
.track-list .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.track-list .draft { color: var(--muted); font-size: 12px; }
.track-list button { padding: 5px 10px; font-size: 13px; }
.track-list button.icon { width: 30px; height: 30px; font-size: 14px; }
.track-list .empty { background: none; color: var(--muted); padding-left: 0; }

/* --- Editor panel --- */

.editor {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 330px;
  overflow-y: auto;
  padding: 12px 14px 14px;
  background: var(--card);
  border-right: 1px solid var(--line);
  backdrop-filter: blur(6px);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

@media (max-width: 640px) {
  .editor { width: 100%; bottom: auto; max-height: 55vh; border-right: none; border-bottom: 1px solid var(--line); }
}

.editor label, .editor .field { margin-bottom: 8px; }
.editor .hint { margin: 0 0 8px; min-height: 0; }
.editor button { padding: 8px 10px; font-size: 14px; }
.editor button.icon { width: 36px; height: 36px; padding: 0; font-size: 18px; }
.editor button.wide { width: 100%; margin-bottom: 8px; }
.editor .help { margin-top: 0; }

.ed-head { display: flex; align-items: center; gap: 8px; }
.ed-head input { flex: 1; min-width: 0; font-weight: 700; }
.unsaved { color: var(--accent); font-size: 14px; }

.ed-bar { display: flex; gap: 6px; }
.ed-bar #ed-save { flex: 1; }
.ed-bar #ed-drive { flex: 1.3; }

.ed-tools { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; }
.ed-tools button { padding: 7px 2px; font-size: 13px; color: var(--muted); }
.ed-tools button[aria-selected="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 800; }

.ed-panel { padding: 10px; border-radius: 10px; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.03); }
.ed-panel .segmented { margin-bottom: 10px; }
.ed-panel .segmented button { font-size: 13px; padding: 7px 2px; }
.ed-row { display: flex; gap: 6px; margin-bottom: 8px; align-items: end; }
.ed-row > * { flex: 1; }
.ed-row label.inline { flex: 0 0 80px; margin: 0; }
output { color: var(--text); font-weight: 600; }

.swatches { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; margin-bottom: 10px; }
.swatches button {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 5px 2px;
  font-size: 11px;
  font-weight: 500;
  color: var(--muted);
}
.swatches .chip { width: 28px; height: 18px; border-radius: 4px; border: 1px solid rgba(0, 0, 0, 0.4); }
.swatches button[aria-checked="true"] { border-color: var(--accent); color: var(--text); box-shadow: inset 0 0 0 1px var(--accent); }

.ed-check ul { list-style: none; margin: 6px 0 0; padding: 0; display: flex; flex-direction: column; gap: 3px; font-size: 13px; }
.ed-check li { padding: 5px 8px; border-radius: 6px; background: rgba(255, 200, 60, 0.1); color: #ffe39a; }
.ed-check li.error { background: rgba(255, 90, 90, 0.14); color: #ffb0b0; }
.ed-check li.ok { background: rgba(90, 220, 120, 0.12); color: #a8f0b8; }
.ed-check li.link { cursor: pointer; }
.ed-check li.link:hover { filter: brightness(1.25); }
.ed-msg { margin: 0; padding: 6px 8px; border-radius: 6px; background: rgba(255, 255, 255, 0.08); font-size: 13px; }
.ed-msg.error { background: rgba(255, 90, 90, 0.16); color: #ffb0b0; }

/* --- Network lobby --- */

.player-list { list-style: none; margin: 0 0 16px; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.player-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.05);
  font-size: 14px;
}
.player-list li.you { background: rgba(255, 255, 255, 0.1); }
.player-list .dot { width: 12px; height: 12px; border-radius: 50%; flex: none; border: 1px solid rgba(0, 0, 0, 0.5); }
.player-list .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player-list .car, .player-list .ping { color: var(--muted); font-size: 12.5px; }
.player-list .state { min-width: 66px; text-align: right; font-size: 12.5px; color: var(--muted); }
.player-list .state.racing { color: var(--accent); }

.value { color: var(--text); font-size: 15px; font-weight: 600; }

.chat ul {
  list-style: none;
  margin: 0;
  padding: 6px 8px;
  height: 96px;
  overflow-y: auto;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.25);
  color: var(--text);
  font-size: 13px;
}
.chat li b { font-weight: 700; }
.chat li.notice { color: #ffe39a; }
#lobby-watch { margin-top: 8px; }
.chat { margin-top: 16px; }

.row.buttons { margin-bottom: 18px; }
.row > button { flex: none; }
.row.buttons > button { flex: 1; }
#net-code { flex: 1; min-width: 0; letter-spacing: 2px; }
button.link.start { margin: 2px 0 0; padding: 0; text-align: left; }

.room-code { display: flex; align-items: baseline; gap: 10px; margin-bottom: 6px; color: var(--muted); font-size: 13px; }
.room-code .code { color: var(--accent); font-size: 26px; font-weight: 900; letter-spacing: 3px; font-variant-numeric: tabular-nums; user-select: all; }
.room-code .badge { margin-left: 0; }
.status { margin: 0 0 12px; font-size: 16px; font-weight: 700; }

.room-list li { padding: 6px 6px 6px 12px; }
.room-list .name { display: flex; flex-direction: column; }
.room-list .name b { overflow: hidden; text-overflow: ellipsis; }
.room-list .meta { color: var(--muted); font-size: 12.5px; }

/* Phones in landscape: denser menus. */
@media (max-height: 500px) {
  .overlay { padding-top: 8px; padding-bottom: 8px; }
  .card { padding: 14px 18px; border-radius: 12px; }
  h1 { margin-bottom: 12px; font-size: 30px; }
  .header { margin-bottom: 10px; }
  label, .field { margin-bottom: 10px; }
  button.big { padding: 10px 16px; }
  .stack { gap: 8px; }
}

/* --- Touch controls (phones, tablets) ---
   The HUD leaves room for them: TOUCH_TOP, TOUCH_PADS, TOUCH_RESULTS in hud.rs. */

body:not(.touch) #help-touch, body.touch #help-one, body.touch #help-two { display: none; }

#touch {
  position: fixed;
  inset: 0;
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}
#touch[hidden], #touch [hidden] { display: none; }
#touch .t-top, #touch .pad, #touch .t-bar { position: absolute; pointer-events: auto; touch-action: none; }

#touch svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }

.t-top { top: 12px; right: 12px; display: flex; gap: 8px; }
.t-btn {
  width: 44px;
  height: 44px;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: rgba(10, 14, 20, 0.55);
  border-color: rgba(255, 255, 255, 0.2);
}

.pad { bottom: 16px; display: flex; align-items: flex-end; gap: 10px; }
.pad.steer { left: 16px; }
.pad.pedals { right: 16px; }
.pad > div {
  display: grid;
  place-items: center;
  border-radius: 18px;
  border: 2px solid rgba(255, 255, 255, 0.28);
  background: rgba(10, 14, 20, 0.35);
  color: rgba(255, 255, 255, 0.85);
  font-size: 13px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.pad > div.on { background: rgba(255, 207, 63, 0.45); border-color: var(--accent); color: #fff; }
.pad svg { width: 36px; height: 36px; }
.steer > div { width: 96px; height: min(120px, 34vh); }
.pedals .gas { width: 96px; height: min(150px, 42vh); }
.pedals .brake { width: 84px; height: min(110px, 31vh); }
.pedals .hand { width: 68px; height: min(64px, 20vh); font-size: 11px; }

.t-bar { bottom: 16px; left: 50%; transform: translateX(-50%); display: flex; gap: 8px; }
.t-bar button { min-width: 48px; height: 48px; padding: 0 16px; background: rgba(10, 14, 20, 0.65); border-color: rgba(255, 255, 255, 0.22); }
#t-results button { min-width: 110px; }
#t-results button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.t-bar button.on { background: rgba(255, 207, 63, 0.45); }

/* Narrow (portrait) phones: buttons in a column (TOUCH_NARROW in hud.rs), smaller pads. */
@media (max-width: 520px) {
  .t-top { flex-direction: column; }
  .steer > div { width: 72px; }
  .pedals .gas { width: 76px; }
  .pedals .brake { width: 66px; }
  .pedals .hand { width: 54px; }
  .pad { gap: 6px; }
  .pad.steer { left: 10px; }
  .pad.pedals { right: 10px; }
}
