:root {
  --bg: #05070c;
  --text: #c9d1d9;
  --blue: #58a6ff;
  --cyan: #35d0ff;
  --orange: #ffa657;
  --purple: #c69bff;
  --link: #79c0ff;
  --mono: 'JetBrains Mono', ui-monospace, monospace;
  --sans: Inter, system-ui, -apple-system, sans-serif;
  --glass: rgba(13, 17, 23, 0.55);
  --line: rgba(201, 209, 217, 0.14);
  --pad: 10px;
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
}

#stage, #stage canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}
#stage canvas { opacity: 0; transition: opacity 0.6s ease; }
body.scene-ready #stage canvas { opacity: 1; }

.glass {
  background: var(--glass);
  border: 1px solid var(--line);
  border-radius: 14px;
  backdrop-filter: blur(12px) saturate(140%);
  -webkit-backdrop-filter: blur(12px) saturate(140%);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.35);
}

button { font-family: inherit; }
.hud { position: fixed; z-index: 2; transition: opacity 0.6s ease; }
body.loading .hud, body.loading .card { opacity: 0; pointer-events: none; }

/* ---------- Top bar: title, tiles, sound buttons ---------- */

.hud-top {
  top: calc(var(--pad) + env(safe-area-inset-top, 0px));
  left: calc(var(--pad) + var(--safe-l));
  right: calc(var(--pad) + var(--safe-r));
  display: flex;
  align-items: flex-start;
  gap: 10px;
  pointer-events: none;
}
.hud-top > * { pointer-events: auto; }
.brand { flex: 0 0 auto; }
#lab-links { margin: -4px 0 4px; gap: 10px; }
h1 { margin: 0; line-height: 0.95; text-transform: uppercase; }
h1 span { display: block; font: 800 10px/1 var(--sans); letter-spacing: 0.2em; color: var(--orange); }
h1 strong {
  display: block;
  font-weight: 900;
  font-size: 21px;
  letter-spacing: 0.02em;
  background: linear-gradient(90deg, var(--cyan), var(--blue) 45%, var(--purple));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.tiles { display: flex; gap: 5px; margin: 0 auto; }
.tile {
  min-width: 62px;
  padding: 6px 8px 5px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(5, 8, 13, 0.55);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.tile-label { display: block; font: 500 8px/1 var(--mono); letter-spacing: 0.12em; text-transform: uppercase; color: #8b949e; }
.tile-value { display: block; margin-top: 4px; font: 400 9.5px/1 var(--mono); color: #8b949e; white-space: nowrap; }
.tile-value b { font-size: 15px; font-weight: 700; color: var(--link); font-variant-numeric: tabular-nums; }
#tile-item.has b { color: var(--purple); }
#tile-item.flash { animation: flash 0.5s ease 2; }
@keyframes flash { 50% { border-color: var(--purple); box-shadow: 0 0 14px rgba(198, 155, 255, 0.6); } }
body.phase-grid .tiles { opacity: 0; pointer-events: none; }

.top-buttons { display: flex; gap: 6px; flex: 0 0 auto; }
.round {
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: rgba(5, 8, 13, 0.55);
  color: #e6edf3;
  font: 600 15px/1 var(--sans);
  cursor: pointer;
  display: grid;
  place-items: center;
}
.round svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
/* Grey = off. For the sound button "pressed" means muted, so it is the reverse of the others. */
.round[aria-pressed="false"]:not(#mute) { color: #6e7681; }
#mute .cross { display: none; }
#mute[aria-pressed="true"] { color: #6e7681; }
#mute[aria-pressed="true"] .cross { display: inline; }
#mute[aria-pressed="true"] .wave { display: none; }

.sentence {
  top: calc(var(--pad) + env(safe-area-inset-top, 0px) + 50px);
  left: 50%;
  transform: translateX(-50%);
  width: min(520px, 62vw);
  margin: 0;
  padding: 7px 12px 7px 26px;
  font-size: 12px;
  line-height: 1.4;
  color: #d7dde4;
  text-align: left;
}
.sentence::before {
  content: '';
  position: absolute;
  left: 11px;
  top: 12px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--cyan);
  box-shadow: 0 0 10px var(--cyan);
  animation: pulse 1.6s ease-in-out infinite;
}
@keyframes pulse { 50% { opacity: 0.35; } }

/* ---------- Thumb controls ---------- */

.controls {
  left: calc(var(--pad) + var(--safe-l));
  right: calc(var(--pad) + var(--safe-r));
  bottom: calc(var(--pad) + var(--safe-b));
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  pointer-events: none;
}
.controls > * { pointer-events: auto; }
body.phase-grid .controls, body.phase-done .controls { opacity: 0; pointer-events: none; }
body.phase-grid .controls > *, body.phase-done .controls > * { pointer-events: none; }
.left-hand { display: flex; flex-direction: column; gap: 8px; width: min(300px, 36vw); }
.brakes { display: flex; justify-content: space-between; }
.right-hand { display: flex; align-items: flex-end; gap: 10px; }

.pad, .strip {
  border: 1px solid var(--line);
  background: rgba(5, 8, 13, 0.45);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  color: #c9d1d9;
  touch-action: none;
  cursor: pointer;
}
.pad { width: 64px; height: 52px; border-radius: 14px; font: 700 13px/1 var(--mono); }
.pad.held, .pad.on { background: rgba(53, 208, 255, 0.28); border-color: var(--cyan); color: #fff; }
.fire { width: 72px; height: 72px; border-radius: 50%; display: grid; place-items: center; }
.fire svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linejoin: round; }
body.armed .fire { border-color: var(--purple); color: var(--purple); box-shadow: 0 0 16px rgba(198, 155, 255, 0.45); }
.fire.held { background: rgba(198, 155, 255, 0.35); }

.strip { position: relative; border-radius: 16px; overflow: hidden; }
.steer { height: 64px; --x: 0; }
.steer::before { content: ''; position: absolute; left: 50%; top: 14px; bottom: 14px; width: 1px; background: var(--line); }
.steer .knob {
  position: absolute;
  top: 8px;
  bottom: 8px;
  width: 44px;
  left: calc(50% - 22px + var(--x) * (50% - 26px));
  border-radius: 12px;
  background: rgba(230, 237, 243, 0.14);
  border: 1px solid rgba(230, 237, 243, 0.35);
}
.steer.held .knob { background: rgba(53, 208, 255, 0.35); border-color: var(--cyan); }
.throttle { width: 70px; height: min(220px, 58vh); --level: 0; }
.throttle .fill {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: calc(var(--level) * 100%);
  background: linear-gradient(0deg, rgba(53, 208, 255, 0.25), rgba(255, 166, 87, 0.55));
}
.throttle .mark {
  position: absolute;
  left: 0;
  right: 0;
  top: 10px;
  text-align: center;
  font: 500 8.5px/1 var(--mono);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: #8b949e;
}
.throttle.held { border-color: var(--orange); }

/* ---------- Cards ---------- */

.card {
  position: fixed;
  z-index: 3;
  left: 50%;
  top: 50%;
  width: min(420px, calc(100vw - 40px));
  max-height: calc(100vh - 90px);
  overflow-y: auto;
  padding: 14px 18px 16px;
  background: rgba(8, 11, 17, 0.8);
  transform: translate(-50%, -44%);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s ease, transform 0.4s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.card.open { opacity: 1; transform: translate(-50%, -40%); pointer-events: auto; }
.card-kicker { margin: 0 0 4px; font: 500 9.5px/1 var(--mono); letter-spacing: 0.16em; text-transform: uppercase; color: var(--orange); }
.card h2 { margin: 0 0 6px; font-size: 19px; font-weight: 800; color: #f0f6fc; }
.card-note { margin: 0 0 10px; font-size: 12px; line-height: 1.45; color: #b7c0ca; }
.segmented {
  display: flex;
  gap: 2px;
  padding: 3px;
  margin-bottom: 10px;
  border-radius: 12px;
  background: rgba(1, 4, 9, 0.45);
  border: 1px solid var(--line);
}
.segmented button {
  flex: 1;
  min-height: 40px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: #b7c0ca;
  font: 500 12.5px/1 var(--sans);
  cursor: pointer;
}
.segmented button.active { background: #e6edf3; color: #0d1117; font-weight: 600; }
.go {
  width: 100%;
  min-height: 46px;
  border: 0;
  border-radius: 12px;
  background: linear-gradient(90deg, var(--cyan), var(--blue));
  color: #03101a;
  font: 800 15px/1 var(--sans);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  cursor: pointer;
}
#finish-table { width: 100%; margin: 4px 0 10px; border-collapse: collapse; font: 400 12px/1.2 var(--mono); }
#finish-table td { padding: 5px 4px; border-top: 1px solid var(--line); color: #c9d1d9; font-variant-numeric: tabular-nums; }
#finish-table td:last-child { text-align: right; }
#finish-table tr.me td { color: var(--orange); font-weight: 700; }
#finish-table .swatch { display: inline-block; width: 8px; height: 8px; margin-right: 6px; border-radius: 2px; }

/* ---------- Portrait: turn the phone ---------- */

.turn {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: rgba(5, 7, 12, 0.82);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  color: #e6edf3;
  font-size: 15px;
}
.turn svg { width: 72px; height: 72px; fill: none; stroke: var(--cyan); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; animation: tilt 2.4s ease-in-out infinite; }
@keyframes tilt { 40%, 70% { transform: rotate(-90deg); } }
body.portrait .turn { display: flex; }

/* ---------- Essay ---------- */

.essay {
  position: fixed;
  z-index: 4;
  top: 10px;
  right: 10px;
  bottom: 10px;
  width: min(460px, calc(100vw - 20px));
  padding: 16px 20px;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: rgba(8, 11, 17, 0.92);
  transform: translateX(calc(100% + 40px));
  transition: transform 0.45s cubic-bezier(0.2, 0.8, 0.2, 1);
  -webkit-user-select: text;
  user-select: text;
}
.essay.open { transform: none; }
.essay .close { position: sticky; top: 0; float: right; }
.essay h2 { margin: 6px 0 6px; font-size: 21px; font-weight: 800; color: #f0f6fc; }
.essay h3 { margin: 18px 0 6px; font: 500 11px/1 var(--mono); letter-spacing: 0.14em; text-transform: uppercase; color: var(--orange); }
.essay p { margin: 0 0 8px; font-size: 13px; line-height: 1.6; color: #b7c0ca; }
.essay a { color: var(--link); }

.footer-left { display: none; left: 18px; bottom: 6px; font: 400 10.5px/1 var(--mono); color: #6e7681; pointer-events: none; }

/* Without a touch screen the pads stay, smaller and quiet in the corners: a visitor who does not
   know the keys can still drive with the mouse. They light up under the pointer. */
@media not all and (any-pointer: coarse) {
  .left-hand { transform: scale(0.7); transform-origin: bottom left; }
  .right-hand { transform: scale(0.7); transform-origin: bottom right; }
  .controls .left-hand, .controls .right-hand { opacity: 0.35; transition: opacity 0.2s ease; }
  .controls .left-hand:hover, .controls .right-hand:hover { opacity: 0.9; }
}

/* ---------- Short landscape phones: a compact HUD so the track stays visible ---------- */

@media (max-height: 480px) {
  :root { --pad: 8px; }
  h1 span { font-size: 8px; }
  h1 strong { font-size: 16px; }
  .tiles { gap: 4px; }
  .tile { min-width: 48px; padding: 4px 6px 3px; border-radius: 8px; }
  .tile-label { font-size: 7px; }
  .tile-value { margin-top: 2px; font-size: 8.5px; }
  .tile-value b { font-size: 12.5px; }
  .round { width: 32px; height: 32px; font-size: 13px; }
  .round svg { width: 15px; height: 15px; }
  .sentence { top: calc(var(--pad) + env(safe-area-inset-top, 0px) + 38px); width: min(460px, 54vw); padding: 4px 10px 4px 22px; font-size: 11px; }
  .sentence::before { left: 9px; top: 9px; width: 6px; height: 6px; }
  .left-hand { gap: 6px; width: min(250px, 32vw); }
  .pad { width: 54px; height: 42px; border-radius: 12px; }
  .fire { width: 58px; height: 58px; }
  .steer { height: 52px; }
  .steer .knob { width: 38px; left: calc(50% - 19px + var(--x) * (50% - 23px)); }
  .throttle { width: 60px; height: min(180px, 46vh); }
}

/* ---------- Desktop ---------- */

@media (min-width: 900px) and (min-height: 560px) {
  :root { --pad: 18px; }
  h1 strong { font-size: 30px; }
  .tile { min-width: 84px; padding: 8px 11px 7px; }
  .tile-label { font-size: 9px; }
  .tile-value b { font-size: 20px; }
  .sentence { top: calc(var(--pad) + 62px); font-size: 13px; }
  .footer-left { display: block; }
  .round:hover { background: #e6edf3; color: #0d1117; }
}
