:root {
  --bg-deep: #000000;
  --bg-panel: #12141c;
  --bg-panel-raised: #1a1d28;
  --border: #2a2416;
  --accent-mars: #ff6b35;
  --accent-mars-dim: #e25822;
  --accent-amber: #ffb703;
  --accent-cyan: #3bd1ff;
  --text-primary: #e8eef7;
  --text-dim: #8a9ab0;
  --status-green: #3ddc84;
  --status-red: #ff4d4d;
  --status-yellow: #ffc94d;
  --status-gray: #5a6b82;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg-deep);
  color: var(--text-primary);
  font-family: "JetBrains Mono", monospace;
}

body {
  background: var(--bg-deep);
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  position: relative;
}

#mars-bg {
  position: fixed;
  inset: 0;
  z-index: 0;
  display: block;
  width: 100%;
  height: 100%;
}

header.mission-header,
.cockpit-body {
  position: relative;
  z-index: 1;
}

h1,
h2,
h3 {
  font-family: "Orbitron", sans-serif;
  letter-spacing: 0.04em;
  margin: 0;
}

/* ---------- Header ---------- */

header.mission-header {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  padding: 0.75rem 1.5rem;
  background: linear-gradient(180deg, rgba(18, 20, 28, 0.55), rgba(10, 11, 14, 0.55));
  backdrop-filter: blur(16px) saturate(150%);
  -webkit-backdrop-filter: blur(16px) saturate(150%);
  border-bottom: 1px solid var(--accent-mars-dim);
  box-shadow: 0 1px 12px -2px rgba(255, 107, 53, 0.35);
  flex-shrink: 0;
}

header.mission-header img.badge {
  width: 42px;
  height: 42px;
  object-fit: cover;
  border-radius: 50%;
  border: 1px solid rgba(255, 107, 53, 0.45);
  background: #0b0c10;
  flex-shrink: 0;
}

.mission-title {
  flex: 1;
}

.mission-title h1 {
  font-size: 1.25rem;
  color: var(--text-primary);
}

.mission-title .subtitle {
  font-size: 0.7rem;
  color: var(--text-dim);
  letter-spacing: 0.08em;
}

.mission-meta {
  display: flex;
  gap: 1.25rem;
  font-size: 0.7rem;
  color: var(--text-dim);
  text-align: right;
}

.mission-meta strong {
  display: block;
  color: var(--accent-cyan);
  font-size: 0.75rem;
  letter-spacing: 0.06em;
}

/* ---------- Layout ---------- */

.cockpit-body {
  flex: 1;
  display: flex;
  min-height: 0;
}

nav.sidebar {
  width: 190px;
  flex-shrink: 0;
  background: rgba(18, 20, 28, 0.5);
  backdrop-filter: blur(16px) saturate(150%);
  -webkit-backdrop-filter: blur(16px) saturate(150%);
  border-right: 1px solid var(--border);
  padding: 1rem 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.nav-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-dim);
  font-family: "Orbitron", sans-serif;
  font-size: 0.7rem;
  letter-spacing: 0.06em;
  text-align: left;
  padding: 0.6rem 0.75rem;
  border-radius: 4px;
  cursor: pointer;
}

.nav-btn:hover {
  border-color: var(--border);
  color: var(--text-primary);
}

.nav-btn.active {
  background: var(--bg-panel-raised);
  border-color: var(--accent-mars);
  color: var(--accent-mars);
  box-shadow: 0 0 10px -2px rgba(255, 107, 53, 0.5);
}

main.content {
  flex: 1;
  overflow: auto;
  padding: 1.25rem;
}

.panel {
  display: none;
}

.panel.active {
  display: block;
}

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

.card {
  background: rgba(18, 20, 28, 0.55);
  backdrop-filter: blur(14px) saturate(150%);
  -webkit-backdrop-filter: blur(14px) saturate(150%);
  border: 1px solid rgba(226, 88, 34, 0.35);
  border-radius: 6px;
  padding: 1rem;
  margin-bottom: 1rem;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.card:hover {
  border-color: var(--accent-mars);
  box-shadow: 0 0 16px -4px rgba(255, 107, 53, 0.45);
}

.card h2 {
  font-size: 0.85rem;
  color: var(--accent-mars);
  margin-bottom: 0.75rem;
  text-transform: uppercase;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

/* A grid track sized `1fr` still has min-width:auto, so a single child wider
   than its track silently widens the whole column instead of being clipped --
   the "grid blowout". That is what let an oversized map canvas grow the
   Navigation panel without bound (docs/technical.tex). min-width:0 makes the
   tracks hold their share whatever the content does. */
.grid-2 > * {
  min-width: 0;
}

.grid-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
  margin-bottom: 1rem;
}

/* ---------- Status indicator ---------- */

.status-line {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8rem;
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--status-gray);
  box-shadow: 0 0 6px currentColor;
}

.status-dot.connected {
  background: var(--status-green);
  color: var(--status-green);
}

.status-dot.connecting {
  background: var(--status-yellow);
  color: var(--status-yellow);
}

.status-dot.error,
.status-dot.closed {
  background: var(--status-red);
  color: var(--status-red);
}

/* ---------- Battery gauge ---------- */

.battery-gauge {
  width: 100%;
  height: 18px;
  background: var(--bg-panel-raised);
  border: 1px solid var(--border);
  border-radius: 3px;
  overflow: hidden;
  margin: 0.5rem 0;
}

.battery-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent-mars-dim), var(--accent-mars));
  transition: width 0.4s ease;
}

/* ---------- Sensor list ---------- */

.sensor-row {
  display: grid;
  grid-template-columns: 16px 1fr auto;
  align-items: center;
  gap: 0.6rem;
  padding: 0.4rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.75rem;
}

.sensor-row:last-child {
  border-bottom: none;
}

.sensor-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--status-gray);
}

.sensor-note {
  color: var(--text-dim);
}

/* ---------- 3D viewer ---------- */

#viewer3d-container {
  width: 100%;
  height: calc(100vh - 220px);
  min-height: 300px;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
}

/* ---------- Camera & Teleop ---------- */

.camera-frame {
  position: relative;
  aspect-ratio: 16 / 9;
  background: #000;
  border: 1px solid var(--border);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.camera-frame img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.camera-placeholder {
  color: var(--text-dim);
  font-size: 0.75rem;
  text-align: center;
  padding: 1rem;
}

.teleop-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}

.control-mode-switch {
  display: flex;
  gap: 0.4rem;
}

.control-mode-btn {
  background: var(--bg-panel-raised);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-family: "Orbitron", sans-serif;
  font-size: 0.7rem;
  letter-spacing: 0.05em;
  padding: 0.45rem 0.9rem;
  border-radius: 4px;
  cursor: pointer;
}

.control-mode-btn:hover {
  border-color: var(--accent-mars);
  color: var(--accent-mars);
}

.control-mode-btn.active {
  background: var(--bg-panel-raised);
  border-color: var(--accent-cyan);
  color: var(--accent-cyan);
  box-shadow: 0 0 10px -2px rgba(59, 209, 255, 0.5);
}

#joystick-base {
  width: 140px;
  height: 140px;
  border-radius: 50%;
  background: radial-gradient(circle, var(--bg-panel-raised), var(--bg-panel));
  border: 1px solid var(--accent-mars-dim);
  box-shadow: 0 0 0 1px rgba(255, 107, 53, 0.15), inset 0 0 20px -6px rgba(255, 107, 53, 0.4);
  position: relative;
  touch-action: none;
  transition: box-shadow 0.15s ease;
}

#joystick-base.active {
  box-shadow: 0 0 0 3px rgba(255, 107, 53, 0.35), inset 0 0 24px -4px rgba(255, 107, 53, 0.6);
}

#joystick-knob {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ffb08a, var(--accent-mars));
  position: absolute;
  top: 50%;
  left: 50%;
  margin: -25px 0 0 -25px;
  transition: transform 0.05s linear;
  box-shadow: 0 0 14px 2px var(--accent-mars);
}

.wasd {
  display: grid;
  grid-template-columns: repeat(3, 36px);
  grid-template-rows: repeat(2, 36px);
  gap: 4px;
  font-size: 0.7rem;
}

.wasd span {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-panel-raised);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-dim);
}

.wasd span.active {
  border-color: var(--accent-mars);
  color: var(--bg-deep);
  background: var(--accent-mars);
  box-shadow: 0 0 10px var(--accent-mars);
}

#key-w {
  grid-column: 2;
  grid-row: 1;
}

#key-a {
  grid-column: 1;
  grid-row: 2;
}

#key-s {
  grid-column: 2;
  grid-row: 2;
}

#key-d {
  grid-column: 3;
  grid-row: 2;
}

#teleop-readout {
  font-size: 0.75rem;
  color: var(--accent-cyan);
}

.note {
  font-size: 0.68rem;
  color: var(--text-dim);
  margin-top: 0.5rem;
  line-height: 1.4;
}

/* ---------- Speed limiter ---------- */

.speed-control {
  width: 100%;
  max-width: 260px;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.speed-control .speed-control-label {
  display: flex;
  justify-content: space-between;
  font-size: 0.7rem;
  color: var(--text-dim);
}

.speed-control .speed-control-label strong {
  color: var(--accent-amber);
}

#speed-slider {
  width: 100%;
  accent-color: var(--accent-mars);
}

.speed-presets {
  display: flex;
  gap: 0.4rem;
  justify-content: center;
}

.speed-preset-btn {
  background: var(--bg-panel-raised);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-family: "JetBrains Mono", monospace;
  font-size: 0.65rem;
  padding: 0.2rem 0.5rem;
  border-radius: 3px;
  cursor: pointer;
}

.speed-preset-btn:hover {
  border-color: var(--accent-mars);
  color: var(--accent-mars);
}

/* ---------- Navigation / MINS ---------- */

.map-frame {
  width: 100%;
  height: calc(100vh - 380px);
  min-height: 280px;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  background: #0a0d14;
}

.map-frame canvas {
  display: block;
}

.map-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  margin-top: 0.6rem;
  font-size: 0.72rem;
  color: var(--text-dim);
}

.map-legend .swatch {
  display: inline-block;
  width: 12px;
  height: 3px;
  margin-right: 0.4rem;
  vertical-align: middle;
  border-radius: 2px;
}

.kv {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.35rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.75rem;
}

.kv span {
  color: var(--text-dim);
  flex-shrink: 0;
}

.kv strong {
  color: var(--accent-cyan);
  font-weight: 600;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

pre.mermaid {
  background: transparent;
  overflow-x: auto;
  text-align: center;
  margin: 0;
}

/* ---------- Docs ---------- */

.doc-frame {
  width: 100%;
  height: calc(100vh - 320px);
  min-height: 300px;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  background: #fff;
}

.doc-frame iframe {
  width: 100%;
  height: 100%;
  border: none;
}

/* ---------- E-STOP ---------- */

#estop-btn {
  width: 140px;
  height: 56px;
  border-radius: 8px;
  border: 2px solid var(--status-red);
  background: linear-gradient(180deg, #3a0f0f, #1a0505);
  color: var(--status-red);
  font-family: "Orbitron", sans-serif;
  font-weight: 900;
  font-size: 1rem;
  letter-spacing: 0.1em;
  cursor: pointer;
  box-shadow: 0 0 14px -2px rgba(255, 77, 77, 0.6);
  animation: estop-pulse 2.2s ease-in-out infinite;
}

#estop-btn:hover {
  box-shadow: 0 0 22px 0 rgba(255, 77, 77, 0.85);
}

#estop-btn.active {
  background: linear-gradient(180deg, var(--status-red), #8a1c1c);
  color: #1a0505;
  animation: none;
  box-shadow: 0 0 24px 4px rgba(255, 77, 77, 0.9);
}

@keyframes estop-pulse {
  0%,
  100% {
    box-shadow: 0 0 10px -2px rgba(255, 77, 77, 0.5);
  }
  50% {
    box-shadow: 0 0 20px 2px rgba(255, 77, 77, 0.85);
  }
}

#estop-status {
  font-size: 0.68rem;
  color: var(--accent-amber);
  letter-spacing: 0.05em;
  min-height: 1em;
}

/* ---------- Ops panel: pre-flight + run recorder ---------- */

.status-dot.warn {
  background: var(--status-yellow);
  color: var(--status-yellow);
}

.ready-banner {
  padding: 0.55rem 0.7rem;
  border-radius: 3px;
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  margin-bottom: 0.7rem;
  border: 1px solid;
}

.ready-banner.ok {
  color: var(--status-green);
  border-color: var(--status-green);
  background: rgba(61, 220, 132, 0.08);
}

.ready-banner.bad {
  color: var(--status-red);
  border-color: var(--status-red);
  background: rgba(255, 77, 77, 0.08);
}

.preflight-row {
  display: grid;
  grid-template-columns: 14px 1fr auto auto;
  align-items: center;
  gap: 0.6rem;
  padding: 0.4rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.7rem;
}

.preflight-row:last-of-type {
  border-bottom: none;
}

.preflight-label code {
  color: var(--text-dim);
  font-size: 0.64rem;
}

.preflight-via {
  color: var(--text-dim);
  font-size: 0.6rem;
  font-style: italic;
}

.preflight-hz {
  color: var(--accent-cyan);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.preflight-group {
  color: var(--text-dim);
  font-size: 0.6rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.recorder-controls {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 0.5rem 0.7rem;
  font-size: 0.72rem;
  margin-bottom: 0.7rem;
}

.recorder-controls select,
.recorder-controls input {
  background: var(--bg-panel-raised);
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--text-primary);
  font-family: inherit;
  font-size: 0.72rem;
  padding: 0.35rem 0.5rem;
  width: 100%;
}

.recorder-buttons {
  grid-column: 1 / -1;
  display: flex;
  gap: 0.5rem;
}

.nav-wheels-toggle {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-top: 0.6rem;
  font-size: 0.72rem;
  color: var(--text-dim);
  cursor: pointer;
}

.btn {
  flex: 1;
  padding: 0.5rem 0.7rem;
  border-radius: 3px;
  border: 1px solid var(--border);
  background: var(--bg-panel-raised);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.btn:hover:not(:disabled) {
  border-color: var(--accent-mars);
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-go {
  border-color: var(--status-green);
  color: var(--status-green);
}

.btn-stop {
  border-color: var(--status-red);
  color: var(--status-red);
}

.run-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 0.8rem;
  padding: 0.4rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.7rem;
}

.run-row:last-child {
  border-bottom: none;
}

.run-name {
  overflow-wrap: anywhere;
}

.run-flag {
  color: var(--accent-amber);
  font-size: 0.6rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.run-size,
.run-date {
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

pre.cmd {
  background: var(--bg-panel-raised);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0.6rem 0.8rem;
  font-size: 0.68rem;
  color: var(--accent-cyan);
  overflow-x: auto;
  line-height: 1.5;
}

/* The `hidden` attribute is used to toggle several panels (camera feed,
   gamepad hint, the Ops agent-offline/live pair). Make it authoritative so a
   later display rule on one of those elements cannot silently un-hide it. */
[hidden] {
  display: none !important;
}
