/* Tilt n Turn — HUD overlay. The WebGL canvas is full-screen; this is the
   HTML/CSS layer on top. Palette lives in :root so the game reskins in one place
   (must stay in sync with materials.js PALETTE). */
:root {
  --sky-top: #bcd8ff;
  --sky-bottom: #ffe9c7;
  --road: #2f8f86;
  --lane: #f3e9d2;
  --car: #ff5a4d;
  --accent: #ffcf5c;

  /* FBD vector colours (shared by 3D arrows and the 2D inset). */
  --c-weight: #8a8f9a; /* grey   — weight mg */
  --c-normal: #5b8def; /* blue   — normal N */
  --c-ncos: #5b8def; /* blue   — N cosθ (dashed) */
  --c-nsin: #1fb5a6; /* teal   — N sinθ inward */
  --c-cf: #f5a623; /* amber  — centrifugal outward */
  --c-net: #ff5a4d; /* coral  — net imbalance */

  --panel-bg: rgba(20, 28, 38, 0.72);
  --panel-fg: #eef3f8;
  --panel-muted: #9fb0c0;
  --ok: #58d68d;
  --bad: #ff6b6b;

  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
  --mono: "SFMono-Regular", "JetBrains Mono", Consolas, monospace;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none; /* no rubber-band / pull-to-refresh on mobile */
  touch-action: none;
  background: var(--sky-top);
  font-family: var(--font);
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#scene {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  display: block;
  touch-action: none; /* we handle pointer gestures ourselves */
}

/* The overlay container does not eat pointer events; its children opt in. */
#hud {
  position: fixed;
  inset: 0;
  pointer-events: none;
  color: var(--panel-fg);
}
#hud button,
#hud input,
#hud .panel,
#hud .readout {
  pointer-events: auto;
}

.panel {
  background: var(--panel-bg);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  padding: 12px 14px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25);
}
.panel-title {
  font-size: 12px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--panel-muted);
  margin-bottom: 8px;
}
.muted {
  color: var(--panel-muted);
}

/* --- top-left: reset + level chip ---------------------------------------- */
.hud-topleft {
  position: absolute;
  top: calc(14px + env(safe-area-inset-top));
  left: calc(14px + env(safe-area-inset-left));
  display: flex;
  gap: 10px;
  align-items: center;
}
.btn {
  font-family: var(--font);
  font-size: 14px;
  color: var(--panel-fg);
  background: var(--panel-bg);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  padding: 8px 14px;
  cursor: pointer;
  transition: transform 0.08s ease, background 0.15s ease;
}
.btn:hover {
  background: rgba(40, 52, 66, 0.85);
}
.btn:active {
  transform: scale(0.96);
}
.chip {
  background: var(--accent);
  color: #2a2410;
  font-weight: 700;
  font-size: 13px;
  border-radius: 999px;
  padding: 7px 14px;
}

.hud-left-stack {
  position: absolute;
  left: calc(16px + env(safe-area-inset-left));
  top: calc(64px + env(safe-area-inset-top));
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 10px;
  max-width: calc(
    100vw - 32px - env(safe-area-inset-left) - env(safe-area-inset-right)
  );
}

/* --- equation block (the pedagogy) --------------------------------------- */
#equations {
  width: max-content;
  max-width: 100%;
  padding: 10px 12px;
  overflow: visible;
}
#equations:has(.eq-details.show) {
  width: min(
    420px,
    calc(100vw - 32px - env(safe-area-inset-left) - env(safe-area-inset-right))
  );
}
/* Compact strip (always visible): the solving value + the answer blank. */
.eq-compact {
  display: flex;
  align-items: center;
  gap: 10px;
  font-family: var(--mono);
  font-size: 14px;
  color: #d8e6f5;
  white-space: nowrap;
  cursor: pointer;
  border-radius: 8px;
}
.eq-compact:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}
.eq-compact-line {
  flex: 0 1 auto;
  min-width: 0;
}
.eq-compact-line .val {
  color: var(--accent);
  font-weight: 700;
}
.eq-compact-line .blank {
  color: var(--c-nsin);
  border-bottom: 2px solid var(--c-nsin);
  padding: 0 4px;
}
.eq-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-left: auto;
  width: 28px;
  height: 26px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.18);
  background: rgba(255, 255, 255, 0.06);
  color: var(--panel-fg);
  font-size: 14px;
  cursor: pointer;
  flex: none;
  line-height: 1;
}
.eq-toggle.open {
  background: var(--accent);
  color: #2a2410;
}
.eq-details {
  display: none;
  position: relative;
  margin-top: 10px;
  max-height: min(56vh, 460px);
  overflow-y: auto;
}
.eq-details.show {
  display: block;
}
.eq-balance-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  width: 100%;
}
.eq-balance-row:has(#fbd-inset:not(.collapsed)) {
  grid-template-columns: minmax(max-content, 1fr) minmax(128px, 0.72fr);
  align-items: start;
  column-gap: 16px;
  row-gap: 10px;
}
.eq-block {
  font-family: var(--mono);
  font-size: 16px;
  line-height: 1.9;
}
.eq-block .row {
  white-space: nowrap;
}
.eq-block .known {
  color: var(--accent);
}
.eq-block .blank {
  display: inline-block;
  min-width: 22px;
  text-align: center;
  color: var(--c-nsin);
  border-bottom: 2px solid var(--c-nsin);
}
.eq-solving {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px dashed rgba(255, 255, 255, 0.15);
  font-family: var(--mono);
  font-size: 15px;
  color: #d8e6f5;
}
.eq-solving .val {
  color: var(--accent);
  font-weight: 700;
}
.eq-block .given {
  margin-top: 8px;
  font-size: 12.5px;
  color: var(--panel-muted);
  line-height: 1.5;
}
.eq-solving div {
  line-height: 1.6;
}
.eq-steps {
  margin-top: 10px;
  font-family: var(--mono);
  font-size: 12.5px;
  color: var(--panel-muted);
  line-height: 1.7;
  display: none;
}
.eq-steps.show {
  display: block;
}
.steps-toggle {
  display: inline-flex;
  gap: 6px;
  align-items: center;
  margin-top: 12px;
  font-size: 12px;
  color: var(--panel-muted);
  cursor: pointer;
}
.hint {
  font-size: 12px;
  color: var(--panel-muted);
  font-style: italic;
  line-height: 1.5;
  width: 100%;
  grid-column: 1 / -1;
}
.hint.empty {
  display: none;
}

/* --- FBD inset (inline with balance equations) ---------------------------- */
#fbd-inset {
  position: static;
  width: 100%;
  max-width: 152px;
  padding: 0;
  justify-self: center;
  align-self: center;
  pointer-events: none;
}
#fbd-inset.collapsed {
  display: none;
}
#fbd-svg {
  width: 100%;
  height: auto;
  display: block;
  background: transparent;
}
.fbd-status {
  display: none;
  margin-top: 2px;
  font-size: 10px;
  font-weight: 700;
  text-align: center;
  min-height: 14px;
}
.fbd-status.balanced {
  color: var(--ok);
}
.fbd-status.slip {
  color: var(--bad);
}

/* --- bank-angle readout (bottom-left) ------------------------------------ */
.bank-control {
  position: absolute;
  left: calc(16px + env(safe-area-inset-left));
  bottom: calc(16px + env(safe-area-inset-bottom));
  pointer-events: none;
}
.bank-instruction {
  margin: 0 0 6px 2px;
  color: rgba(255, 255, 255, 0.94);
  font-family: var(--font);
  font-size: 12px;
  font-weight: 400;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.45);
}
.bank-readout {
  background: var(--panel-bg);
  border-radius: 12px;
  padding: 10px 16px;
  font-family: var(--mono);
  font-size: 16px;
  font-weight: 700;
  color: var(--accent);
}

/* --- bank-angle gizmo (bottom-left, drag up/down to bank) ----------------- */
.readout {
  position: absolute;
  left: calc(16px + env(safe-area-inset-left));
  bottom: calc(16px + env(safe-area-inset-bottom));
  background: var(--panel-bg);
  border-radius: 12px;
  padding: 8px 12px;
  cursor: ns-resize;
  touch-action: none;
}
#bank-gizmo {
  width: 170px;
  height: auto;
  display: block;
}

/* --- 3-tier equation overlay -------------------------------------------- */
.eq-compact-line .known {
  color: var(--accent);
  font-weight: 700;
}
.help-section {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px dashed rgba(255, 255, 255, 0.15);
  cursor: pointer;
  border-radius: 8px;
}
.help-section:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}
.help-toggle {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  color: var(--accent);
  font-family: var(--font);
  font-size: 12.5px;
  cursor: pointer;
  padding: 2px 0;
}
.eq-help {
  display: none;
  margin-top: 8px;
  font-family: var(--mono);
  font-size: 12.5px;
  color: #d8e6f5;
  line-height: 1.7;
}
.eq-help.show {
  display: block;
}
.eq-help .hint,
.eq-block .hint {
  margin-top: 8px;
  font-style: italic;
  color: var(--panel-muted);
  font-family: var(--font);
  font-size: 12px;
  line-height: 1.5;
}
.atan-table {
  margin-top: 10px;
  border-collapse: collapse;
  font-family: var(--mono);
  font-size: 12px;
}
.atan-table th,
.atan-table td {
  border: 1px solid rgba(255, 255, 255, 0.15);
  padding: 2px 12px;
  text-align: center;
}
.atan-table th {
  color: var(--panel-muted);
}
.atan-table td {
  color: var(--accent);
}

/* --- speedometer gauge (bottom-right, also the launch button) ------------- */
.gauge {
  position: absolute;
  right: calc(24px + env(safe-area-inset-right));
  bottom: calc(24px + env(safe-area-inset-bottom));
  width: 124px;
  height: 124px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-family: var(--mono);
  color: var(--panel-fg);
  /* Conic ring = speed fraction (--p 0..1); inner disc punched out on top. */
  background:
    radial-gradient(
      circle at 50% 50%,
      rgba(18, 26, 36, 0.96) 60%,
      transparent 61%
    ),
    conic-gradient(
    var(--accent) calc(var(--p, 0) * 360deg),
    rgba(255, 255, 255, 0.12) 0
  );
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.4);
  transition: transform 0.08s ease;
  touch-action: manipulation;
}
.gauge:active {
  transform: scale(0.95);
}
.gauge.ready {
  background:
    radial-gradient(
      circle at 50% 50%,
      rgba(18, 26, 36, 0.96) 60%,
      transparent 61%
    ),
    conic-gradient(var(--car), #ff8a6e);
}
.gauge-val {
  font-size: 32px;
  font-weight: 800;
  line-height: 1;
}
.gauge.ready .gauge-val {
  font-size: 30px;
  color: var(--accent);
}
.gauge-unit {
  font-size: 11px;
  color: var(--panel-muted);
  letter-spacing: 0.1em;
  margin-top: 3px;
}

/* --- center banner ------------------------------------------------------- */
.banner {
  position: absolute;
  top: 38%;
  left: 50%;
  transform: translate(-50%, -50%);
  min-width: min(480px, calc(100vw - 48px));
  padding: 18px 28px 20px;
  border-radius: 16px;
  background: rgba(18, 26, 36, 0.78);
  border: 1px solid rgba(255, 255, 255, 0.14);
  box-shadow: 0 12px 34px rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  text-align: center;
  font-size: 56px;
  font-weight: 900;
  letter-spacing: 0.02em;
  text-shadow: 0 4px 18px rgba(0, 0, 0, 0.4);
  pointer-events: none;
  transition: opacity 0.2s ease;
}
.banner.win {
  color: var(--ok);
}
.banner.fail {
  color: var(--bad);
}
.banner .sub {
  display: block;
  font-size: 18px;
  font-weight: 600;
  margin-top: 8px;
  color: var(--panel-fg);
}
.hidden {
  opacity: 0;
  visibility: hidden;
}

/* Narrow screens: shrink the equation panel so it doesn't crowd the car. */
@media (max-width: 720px) {
  .hud-left-stack {
    top: calc(60px + env(safe-area-inset-top));
  }
  #equations {
    width: 100%;
    font-size: 13px;
  }
  .eq-compact {
    font-size: 12.5px;
    gap: 8px;
  }
  .eq-toggle {
    width: 26px;
  }
  .eq-balance-row:has(#fbd-inset:not(.collapsed)) {
    grid-template-columns: minmax(max-content, 1fr) minmax(104px, 0.62fr);
    column-gap: 10px;
    row-gap: 8px;
  }
  #fbd-inset {
    max-width: 140px;
  }
  .banner {
    font-size: 38px;
    padding: 14px 18px 16px;
  }
  .banner .sub {
    font-size: 15px;
  }
  .pedal {
    width: 92px;
    height: 92px;
    font-size: 12px;
  }
}
