/* ==========================================================================
   app.css — application layout and components
   ========================================================================== */

body {
  display: grid;
  /* minmax(0, 1fr): without the 0 the row grows with its content and the
     screens cannot scroll but get cut off at the bottom. */
  grid-template-rows: auto minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* ---------------- Header ---------------- */
.topbar {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  padding: var(--space-3) var(--space-5);
  background: var(--surface);
  border-bottom: 1px solid var(--border-soft);
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  color: var(--accent);
}

.logo {
  width: 30px;
  height: 30px;
  flex: 0 0 auto;
}

.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.15;
  color: var(--text);
}

.brand-text strong {
  font-size: var(--text-sm);
  letter-spacing: 0.01em;
}

.brand-text em {
  font-style: normal;
  font-size: var(--text-xs);
  color: var(--text-faint);
}

.topbar-meta {
  display: flex;
  gap: var(--space-2);
  margin-left: var(--space-4);
  flex-wrap: wrap;
}

.chip {
  padding: var(--space-1) var(--space-3);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: var(--text-xs);
  color: var(--text-muted);
  white-space: nowrap;
}

.topbar-actions {
  margin-left: auto;
  display: flex;
  gap: var(--space-2);
}

[data-theme='dark'] [data-theme-icon='light'],
[data-theme='light'] [data-theme-icon='dark'] {
  display: none;
}

/* ---------------- Screens ---------------- */
/* #app is the second grid row of the body. Without its own height limit
   the active screen grows beyond the viewport and cannot be scrolled —
   hence a separate grid with minmax(0, 1fr) here. */
#app {
  display: grid;
  grid-template-rows: minmax(0, 1fr);
  min-height: 0;
  overflow: hidden;
}

.screen {
  display: none;
  min-height: 0;
  overflow: hidden;
}

.screen.is-active {
  display: block;
}

.screen-setup.is-active,
.screen-result.is-active {
  overflow-y: auto;
  overscroll-behavior: contain;
}

/* ---------------- Setup ---------------- */
.setup-inner,
.result-inner {
  max-width: 1360px;
  margin: 0 auto;
  padding: clamp(var(--space-6), 4vw, var(--space-12)) var(--space-5) var(--space-16);
}

.eyebrow {
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--accent);
  margin-bottom: var(--space-2);
}

.lede {
  max-width: 62ch;
  color: var(--text-muted);
}

.setup-head {
  margin-bottom: var(--space-8);
}

.setup-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: var(--space-5);
  align-items: start;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
}

.panel-title {
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-faint);
  font-weight: 700;
  margin-bottom: var(--space-3);
}

.panel-title + .panel-title,
.checks + .panel-title,
.spec-table + .panel-title {
  margin-top: var(--space-6);
}

.card-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  margin-bottom: var(--space-4);
}

.card {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: var(--space-1) var(--space-3);
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  cursor: pointer;
  text-align: left;
  transition: border-color 0.18s var(--ease), background 0.18s var(--ease);
}

.card:hover {
  background: var(--surface-3);
}

.card.is-selected {
  border-color: var(--accent);
  background: color-mix(in oklch, var(--accent) 12%, var(--surface-2));
}

.card-name {
  font-weight: 700;
  font-size: var(--text-sm);
}

.card-note {
  grid-column: 1 / -1;
  font-size: var(--text-xs);
  color: var(--text-muted);
}

.card-meta {
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  color: var(--text-faint);
  white-space: nowrap;
}

.difficulty {
  display: inline-flex;
  gap: 3px;
  align-items: center;
}

.difficulty i {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--border);
}

.difficulty i.on {
  background: var(--accent);
}

.setup-options {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  margin: var(--space-4) 0 var(--space-5);
}

.brief-list {
  margin: 0 0 var(--space-4);
  padding-left: var(--space-5);
  font-size: var(--text-sm);
  color: var(--text-muted);
}

.brief-list li {
  margin-bottom: var(--space-2);
}

.brief-geo {
  font-size: var(--text-xs);
  font-family: var(--font-mono);
  color: var(--text-faint);
  background: var(--surface-2);
  border-radius: var(--radius);
  padding: var(--space-3);
  margin-bottom: var(--space-4);
  line-height: 1.6;
}

/* ---------------- Training ---------------- */
.screen-train.is-active {
  display: grid;
  grid-template-columns: 1fr minmax(320px, 27vw);
  min-height: 0;
  height: 100%;
}

.stage {
  position: relative;
  min-height: 0;
  background: #14161a;
  overflow: hidden;
}

#view-main {
  width: 100%;
  height: 100%;
}

.mirrors {
  position: absolute;
  inset: var(--space-3) var(--space-3) auto;
  display: grid;
  grid-template-columns: 1fr 1.05fr 1fr;
  gap: var(--space-3);
  pointer-events: none;
  max-width: 940px;
  margin: 0 auto;
}

.mirror {
  margin: 0;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid oklch(100% 0 0 / 0.14);
  background: #333;
  box-shadow: var(--shadow);
  backdrop-filter: blur(2px);
}

.mirror canvas {
  width: 100%;
  aspect-ratio: 16 / 6;
}

.mirror-centre canvas {
  aspect-ratio: 16 / 4.6;
}

.mirror figcaption {
  font-size: 0.6875rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: oklch(100% 0 0 / 0.62);
  background: oklch(0% 0 0 / 0.55);
  padding: 2px var(--space-2);
  text-align: center;
}

.view-switch {
  position: absolute;
  left: var(--space-3);
  bottom: var(--space-3);
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
  background: oklch(0% 0 0 / 0.4);
  padding: var(--space-2);
  border-radius: var(--radius);
  backdrop-filter: blur(6px);
}

.view-switch .btn {
  --btn-bg: oklch(100% 0 0 / 0.06);
  --btn-border: oklch(100% 0 0 / 0.12);
  color: oklch(96% 0 0 / 0.85);
  min-height: 32px;
  font-size: var(--text-xs);
}

.view-switch .btn.is-active {
  --btn-bg: var(--accent);
  color: var(--accent-ink);
  border-color: transparent;
}

.look-switch {
  position: absolute;
  right: var(--space-3);
  bottom: var(--space-3);
  display: flex;
  gap: var(--space-2);
  background: oklch(0% 0 0 / 0.4);
  padding: var(--space-2);
  border-radius: var(--radius);
  backdrop-filter: blur(6px);
}

.look-switch .btn {
  --btn-bg: oklch(100% 0 0 / 0.06);
  --btn-border: oklch(100% 0 0 / 0.12);
  color: oklch(96% 0 0 / 0.85);
}

.coach {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--space-3) + 52px);
  width: min(560px, calc(100% - var(--space-8)));
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius);
  background: oklch(20% 0.01 250 / 0.86);
  border: 1px solid color-mix(in oklch, var(--accent) 40%, transparent);
  backdrop-filter: blur(8px);
  text-align: center;
}

.coach-title {
  margin: 0;
  font-weight: 700;
  font-size: var(--text-sm);
  color: oklch(95% 0.02 178);
}

.coach-detail {
  margin: var(--space-1) 0 0;
  font-size: var(--text-xs);
  color: oklch(82% 0.01 250);
}

/* ---------------- Cockpit ---------------- */
.cockpit {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  padding: var(--space-4);
  background: var(--surface);
  border-left: 1px solid var(--border-soft);
  overflow-y: auto;
  min-height: 0;
}

.hud {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1px;
  background: var(--border-soft);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  overflow: hidden;
}

.hud-cell {
  background: var(--surface-2);
  padding: var(--space-2) var(--space-3);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.hud-label {
  font-size: 0.6875rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-faint);
}

.hud-value {
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}

.hud-value small {
  font-size: 0.75em;
  color: var(--text-faint);
}

.hud-cell.is-alert .hud-value {
  color: var(--danger);
}

.controls {
  display: grid;
  grid-template-columns: 1.15fr 1fr;
  gap: var(--space-4);
  align-items: start;
}

.wheel-wrap {
  text-align: center;
}

#wheel {
  width: 100%;
  aspect-ratio: 1;
  max-width: 220px;
  margin: 0 auto;
  cursor: grab;
}

#wheel.is-grabbing {
  cursor: grabbing;
}

.wheel-hint {
  margin: var(--space-2) 0 0;
  font-size: 0.6875rem;
  color: var(--text-faint);
}

.pedals {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.gearbox {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--space-1);
}

.btn-gear {
  font-family: var(--font-mono);
  font-weight: 700;
  padding: var(--space-2) 0;
}

.btn-pedal {
  min-height: 56px;
  font-weight: 700;
  background: var(--surface-2);
}

.btn-pedal.is-pressed {
  background: color-mix(in oklch, var(--accent) 28%, var(--surface-2));
  border-color: var(--accent);
}

.btn-pedal-brake.is-pressed {
  background: color-mix(in oklch, var(--danger) 30%, var(--surface-2));
  border-color: var(--danger);
}

.cockpit-actions {
  display: grid;
  gap: var(--space-2);
}

.sim-meta {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  color: var(--text-faint);
}

/* ---------------- Result ---------------- */
.result-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: var(--space-5);
  align-items: start;
}

.panel-wide {
  grid-column: span 2;
}

@media (max-width: 900px) {
  .panel-wide {
    grid-column: span 1;
  }
}

#analysis-canvas {
  width: 100%;
  aspect-ratio: 16 / 9;
  border-radius: var(--radius);
  background: #1c1f23;
  border: 1px solid var(--border-soft);
}

.charts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-3);
  margin-top: var(--space-3);
}

.charts canvas {
  width: 100%;
  aspect-ratio: 16 / 7;
  background: var(--surface-2);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
  list-style: none;
  margin: var(--space-3) 0 0;
  padding: 0;
  font-size: var(--text-xs);
  color: var(--text-muted);
}

.legend li {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.sw {
  width: 14px;
  height: 3px;
  border-radius: 2px;
  display: inline-block;
}

.sw-path {
  background: #70d6c4;
}
.sw-rev {
  background: #8cc8ff;
}
.sw-fwd {
  background: #e8ecf2;
}
.sw-turn {
  background: #70d6c4;
  height: 8px;
  width: 8px;
  border-radius: 50%;
}
.sw-collide {
  background: #ff786a;
  height: 8px;
  width: 8px;
  border-radius: 50%;
}

.checks {
  list-style: none;
  margin: 0 0 var(--space-4);
  padding: 0;
  display: grid;
  gap: var(--space-2);
}

.checks li {
  display: grid;
  grid-template-columns: 18px 1fr auto;
  gap: var(--space-3);
  align-items: baseline;
  font-size: var(--text-sm);
  padding-bottom: var(--space-2);
  border-bottom: 1px solid var(--border-soft);
}

.checks .mark {
  font-weight: 700;
}

.checks .ok .mark {
  color: var(--good);
}

.checks .bad .mark {
  color: var(--danger);
}

.checks .val {
  font-family: var(--font-mono);
  font-size: 0.85em;
  color: var(--text-muted);
}

.insights {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: var(--space-3);
}

.insights li {
  font-size: var(--text-sm);
  padding-left: var(--space-4);
  border-left: 2px solid var(--border);
  color: var(--text-muted);
}

.insights li.good {
  border-color: var(--good);
  color: var(--text);
}

.insights li.warn {
  border-color: var(--warn);
  color: var(--text);
}

.table-scroll {
  overflow-x: auto;
}

.result-actions {
  display: flex;
  gap: var(--space-3);
  margin-top: var(--space-8);
  flex-wrap: wrap;
}

.verdict-pass {
  color: var(--good);
  font-weight: 700;
}

.verdict-fail {
  color: var(--warn);
  font-weight: 700;
}

/* ---------------- Dialog / Toast ---------------- */
.sheet {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  color: var(--text);
  padding: var(--space-6);
  max-width: 780px;
  width: calc(100% - var(--space-8));
  max-height: 86dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  box-shadow: var(--shadow-lg);
}

.sheet::backdrop {
  background: oklch(10% 0.01 250 / 0.6);
  backdrop-filter: blur(3px);
}

.help-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: var(--space-6);
  margin: var(--space-4) 0 var(--space-6);
}

.help-grid dl {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: var(--space-2) var(--space-4);
  margin: 0;
  font-size: var(--text-sm);
}

.help-grid dt {
  font-family: var(--font-mono);
  font-size: 0.85em;
  color: var(--accent);
  white-space: nowrap;
}

.help-grid dd {
  margin: 0;
  color: var(--text-muted);
}

.bullets {
  margin: 0 0 var(--space-4);
  padding-left: var(--space-5);
  font-size: var(--text-sm);
  color: var(--text-muted);
}

.bullets li {
  margin-bottom: var(--space-2);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 132px;
  max-width: min(620px, calc(100% - var(--space-8)));
  transform: translate(-50%, 12px);
  padding: var(--space-3) var(--space-5);
  border-radius: var(--radius);
  background: var(--surface-3);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  font-size: var(--text-sm);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s var(--ease), transform 0.2s var(--ease);
  z-index: 40;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

@media (max-width: 1080px) {
  /* The hint must not cover the mirrors: it stays at the bottom and
     only moves up above the camera bars. */
  .toast {
    top: auto;
    bottom: 8px;
    left: 8px;
    right: 8px;
    transform: none;
    max-width: none;
    font-size: var(--text-xs);
  }

  .toast.is-visible {
    transform: none;
  }
}

/* ---------------- Responsive ---------------- */
@media (max-width: 1080px) {
  .screen-train.is-active {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) minmax(0, auto);
  }

  .cockpit {
    border-left: 0;
    border-top: 1px solid var(--border-soft);
    max-height: 46vh;
    overflow-y: auto;
  }

  .stage {
    min-height: 44vh;
  }

  .controls {
    grid-template-columns: 1fr 1fr;
  }

  #wheel {
    max-width: 170px;
  }

  .hud {
    grid-template-columns: repeat(4, 1fr);
  }
}

@media (max-width: 720px) {
  .topbar-meta {
    display: none;
  }

  .hud {
    grid-template-columns: repeat(2, 1fr);
  }

  .mirror figcaption {
    display: none;
  }

  .charts {
    grid-template-columns: 1fr;
  }

  /* Camera and look bars stack instead of overlapping */
  .view-switch,
  .look-switch {
    left: var(--space-2);
    right: var(--space-2);
    /* flex-start: with centered content the overflow on the left
       would not be reachable. */
    justify-content: flex-start;
    flex-wrap: wrap;
  }

  .view-switch {
    bottom: 52px;
  }

  .look-switch {
    bottom: var(--space-2);
  }

  .view-switch .btn,
  .look-switch .btn {
    min-height: 30px;
    padding-inline: 8px;
    font-size: 0.75rem;
  }

  /* Mirrors stay in one row and small — the main view has priority */
  .mirrors {
    inset: 6px 6px auto;
    gap: 4px;
    max-width: none;
  }

  .mirror canvas {
    aspect-ratio: 16 / 7;
  }

  /* On small devices the view direction is controlled by dragging in the
     view — the extra bar would cover too much of the view. */
  .look-switch {
    display: none;
  }

  .view-switch {
    bottom: var(--space-2);
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 6px;
  }

  .view-switch .btn {
    white-space: nowrap;
  }
}

/* ---------------- Footer ---------------- */
.site-footer {
  margin-top: var(--space-16);
  padding-top: var(--space-8);
  border-top: 1px solid var(--border-soft);
  display: grid;
  grid-template-columns: minmax(0, 1.1fr) minmax(0, 2fr);
  gap: var(--space-8) var(--space-12);
  color: var(--text-muted);
  font-size: var(--text-sm);
}

.footer-brand {
  display: flex;
  gap: var(--space-3);
  align-items: flex-start;
}

.footer-brand .logo {
  width: 28px;
  height: 28px;
  flex: none;
  color: var(--accent);
}

.footer-name {
  margin: 0 0 var(--space-1);
  font-weight: 700;
  color: var(--text);
}

.footer-tagline {
  margin: 0;
  max-width: 38ch;
  line-height: 1.55;
}

.footer-nav {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: var(--space-6);
}

.footer-heading {
  margin: 0 0 var(--space-3);
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-faint);
}

.footer-nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: var(--space-2);
}

.footer-nav a {
  color: var(--text-muted);
  text-decoration: none;
  border-radius: var(--radius-sm);
  transition: color 160ms var(--ease);
}

.footer-nav a:hover {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 3px;
}

.footer-bottom {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: var(--space-2) var(--space-6);
  padding-top: var(--space-5);
  border-top: 1px solid var(--border-soft);
  font-size: var(--text-xs);
  color: var(--text-faint);
}

.footer-bottom p {
  margin: 0;
}

@media (max-width: 900px) {
  .site-footer {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 520px) {
  .footer-nav {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

/* ---------------- Legal dialog ---------------- */
.sheet-legal {
  max-width: 720px;
  padding: 0;
  display: none;
  flex-direction: column;
  overflow: hidden;
}

.sheet-legal[open] {
  display: flex;
}

.legal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-5) var(--space-6);
  border-bottom: 1px solid var(--border-soft);
}

.legal-head h2 {
  margin: 0;
  font-size: var(--text-lg);
}

.legal-body {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: var(--space-5) var(--space-6) var(--space-8);
  line-height: 1.65;
  color: var(--text-muted);
}

.legal-body h3 {
  margin: var(--space-6) 0 var(--space-2);
  font-size: var(--text-base);
  color: var(--text);
}

.legal-body p,
.legal-body ul {
  margin: 0 0 var(--space-3);
  max-width: 68ch;
}

.legal-body ul {
  padding-left: 1.2em;
}

.legal-body li + li {
  margin-top: var(--space-2);
}

.legal-body strong {
  color: var(--text);
}

.legal-body a {
  color: var(--accent);
}

.legal-updated {
  font-size: var(--text-xs);
  color: var(--text-faint);
  font-family: var(--font-mono);
}

/* Cockpit: sections must not be squeezed — the cockpit scrolls instead */
.cockpit > * {
  flex-shrink: 0;
}

/* [hidden] has to win against display:flex */
.look-switch[hidden],
.coach[hidden] {
  display: none !important;
}

/* On medium widths the camera and look bars do not fit side by side:
   the look bar moves above the camera bar. */
@media (max-width: 1500px) and (min-width: 721px) {
  .look-switch {
    bottom: auto;
    top: 50%;
    transform: translateY(-30%);
    flex-direction: column;
  }
}

/* Contact line in the footer brand block */
.footer-contact {
  margin: var(--space-3) 0 0;
  font-size: var(--text-sm);
  color: var(--text-muted);
}
.footer-contact a,
.legal-body a[href^="mailto:"] {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 3px;
  overflow-wrap: anywhere;
}
