/*
 * main.css — layout and chrome of the Braiding Applet.
 * Neutral dark theme. Chart tokens follow the validated data-viz palette (dark mode):
 * categorical slots 1–4 = blue, orange, aqua, yellow; the two yarn families are slot 2 (orange,
 * "+") and slot 3 (aqua, "−") everywhere (3D view and plots).
 */
:root {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface-1: #1a1a19;
  --surface-2: #222220;
  --surface-3: #2c2c2a;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.1);
  --series-1: #3987e5; /* blue   — quasi-static reference */
  --series-2: #d95926; /* orange — "+" yarn family */
  --series-3: #199e70; /* aqua   — "−" yarn family */
  --series-4: #c98500; /* yellow — geodesic reference */
  --status-good: #0ca30c;
  --status-warning: #fab219;
  --status-serious: #ec835a;
  --status-critical: #d03b3b;
  --accent: #3987e5;
  --radius: 8px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --topbar-h: 56px;
  --side-w: clamp(340px, 32vw, 520px);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--page);
  color: var(--text-primary);
  font: 14px/1.45 var(--font);
}

button,
select,
input {
  font: inherit;
  color: inherit;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

/* ── Top bar ─────────────────────────────────────────────────────────────────────────────── */
#topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  min-height: var(--topbar-h);
  padding: 8px 16px;
  padding-top: max(8px, env(safe-area-inset-top, 0px));
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.logo {
  width: 30px;
  height: 30px;
  flex: none;
}

.brand h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 650;
  letter-spacing: 0.01em;
}

.subtitle {
  margin: 0;
  font-size: 12px;
  color: var(--text-muted);
}

.transport {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
  transition: background 0.15s;
}

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

.btn:focus-visible,
.tab:focus-visible,
select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.btn.primary {
  background: #1c5cab;
  border-color: #256abf;
  min-width: 72px;
}

.btn.primary:hover {
  background: #256abf;
}

.btn.small {
  padding: 3px 9px;
  font-size: 12px;
}

.speed {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text-secondary);
  font-size: 13px;
}

.speed select {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 6px;
}

.status {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-left: auto;
  flex-wrap: wrap;
}

.readout {
  font-variant-numeric: tabular-nums;
  color: var(--text-secondary);
  min-width: 72px;
}

.readout .label {
  color: var(--text-muted);
}

.progress {
  width: 120px;
  height: 6px;
  border-radius: 3px;
  background: var(--surface-3);
  overflow: hidden;
}

#progress-bar {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.2s linear;
}

.badges {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-secondary);
}

.badge .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
}

.badge.live {
  color: var(--text-primary);
}

.badge.ok .dot {
  background: var(--status-good);
}

.badge.warning .dot {
  background: var(--status-warning);
}

.badge.serious .dot {
  background: var(--status-serious);
}

.badge.critical .dot {
  background: var(--status-critical);
}

/* ── Main split ──────────────────────────────────────────────────────────────────────────── */
#main {
  flex: 1;
  display: flex;
  min-height: 0;
}

#viewport {
  position: relative;
  flex: 1;
  min-width: 0;
  background: radial-gradient(120% 90% at 50% 35%, #262930 0%, #15171b 55%, #0d0d0d 100%);
  overflow: hidden;
}

#canvas-host,
#canvas-host canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

#gui-host {
  position: absolute;
  top: 10px;
  right: 10px;
  max-height: calc(100% - 20px);
  overflow: auto;
  z-index: 3;
}

#labels-host {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 2;
}

.vec-label {
  position: absolute;
  transform: translate(-50%, -50%);
  font: 600 12px/1 var(--font);
  color: var(--text-primary);
  text-shadow: 0 0 3px #000, 0 0 6px #000;
  white-space: nowrap;
}

.view-buttons {
  position: absolute;
  left: 10px;
  top: 10px;
  display: flex;
  gap: 6px;
  z-index: 3;
  flex-wrap: wrap;
}

.view-buttons .btn {
  background: rgba(26, 26, 25, 0.85);
}

#hud-bottom {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 12px;
  z-index: 4;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  pointer-events: none;
}

#hud-bottom > * {
  pointer-events: auto;
}

.colorbar {
  width: min(300px, 100%);
  padding: 8px 10px;
  border-radius: var(--radius);
  background: rgba(26, 26, 25, 0.88);
  border: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-secondary);
}

.colorbar .cb-title {
  color: var(--text-primary);
  margin-bottom: 4px;
}

.colorbar .cb-ramp {
  height: 10px;
  border-radius: 3px;
}

.colorbar .cb-ticks {
  display: flex;
  justify-content: space-between;
  margin-top: 3px;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
}

.colorbar .cb-keys {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 12px;
  margin-top: 6px;
}

.colorbar .cb-key {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.colorbar .cb-swatch {
  width: 12px;
  height: 12px;
  border-radius: 3px;
}

.message {
  max-width: min(560px, 100%);
  padding: 8px 14px;
  border-radius: var(--radius);
  background: rgba(26, 26, 25, 0.92);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  font-size: 13px;
}

/* ── Side panel ──────────────────────────────────────────────────────────────────────────── */
#sidepanel {
  width: var(--side-w);
  display: flex;
  flex-direction: column;
  background: var(--surface-1);
  border-left: 1px solid var(--border);
  min-height: 0;
}

.tabs {
  display: flex;
  gap: 2px;
  padding: 6px 8px 0;
  border-bottom: 1px solid var(--border);
}

.tab {
  border: none;
  background: none;
  padding: 8px 12px;
  color: var(--text-muted);
  border-bottom: 2px solid transparent;
  cursor: pointer;
}

.tab.active {
  color: var(--text-primary);
  border-bottom-color: var(--accent);
}

.tab-body {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
}

.tab-panel {
  padding: 12px 14px 24px;
}

.tab-panel h2 {
  font-size: 15px;
  margin: 18px 0 6px;
}

.tab-panel h2:first-child {
  margin-top: 4px;
}

.tab-panel h3 {
  font-size: 13px;
  margin: 14px 0 4px;
  color: var(--text-secondary);
}

.tab-panel p,
.tab-panel li {
  color: var(--text-secondary);
}

.tab-panel a {
  color: #86b6ef;
}

.note {
  font-size: 12px;
  color: var(--text-muted);
}

/* ── Plot cards ──────────────────────────────────────────────────────────────────────────── */
.plot-card {
  margin-bottom: 14px;
  padding: 10px 10px 8px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.plot-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 2px 8px;
  margin-bottom: 4px;
}

.plot-title {
  font-weight: 600;
  font-size: 13px;
}

.plot-sub {
  order: 3;
  flex-basis: 100%;
  font-size: 12px;
  color: var(--text-muted);
}

.plot-sub:empty {
  display: none;
}

.plot-actions {
  margin-left: auto;
  display: flex;
  gap: 4px;
}

.plot-actions .btn {
  padding: 1px 7px;
  font-size: 11px;
}

.plot-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 12px;
  font-size: 12px;
  color: var(--text-secondary);
  margin: 2px 0 4px;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.legend-line {
  width: 16px;
  height: 0;
  border-top: 2px solid;
}

.legend-line.dashed {
  border-top-style: dashed;
}

.plot-canvas-wrap {
  position: relative;
  height: 190px;
}

.plot-canvas-wrap canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.plot-tooltip {
  position: absolute;
  pointer-events: none;
  z-index: 2;
  min-width: 120px;
  padding: 6px 8px;
  background: rgba(13, 13, 13, 0.94);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 12px;
  color: var(--text-secondary);
}

.plot-tooltip .tt-x {
  color: var(--text-muted);
  margin-bottom: 3px;
}

.plot-tooltip .tt-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.plot-tooltip .tt-val {
  color: var(--text-primary);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.plot-table-wrap {
  max-height: 220px;
  overflow: auto;
  margin-top: 6px;
}

table.data {
  border-collapse: collapse;
  width: 100%;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

table.data th,
table.data td {
  text-align: right;
  padding: 2px 6px;
  border-bottom: 1px solid var(--grid);
}

table.data th {
  position: sticky;
  top: 0;
  background: var(--surface-2);
  color: var(--text-secondary);
  font-weight: 600;
}

/* ── Theory panel ────────────────────────────────────────────────────────────────────────── */
.live-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  margin: 6px 0 4px;
}

.live-table td {
  padding: 3px 4px;
  border-bottom: 1px solid var(--grid);
  vertical-align: baseline;
}

.live-table td.v {
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--text-primary);
  white-space: nowrap;
}

.live-table td.u {
  color: var(--text-muted);
  width: 48px;
}

.formula {
  overflow-x: auto;
  overflow-y: hidden;
  padding: 2px 0;
}

/* Figures (docs/figures/*.svg): light "paper" cards, readable on the dark panel. */
.theory-fig {
  margin: 10px 0 14px;
}

.theory-fig img {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 8px;
}

.theory-fig figcaption {
  margin-top: 5px;
  font-size: 12px;
  line-height: 1.45;
  color: var(--text-muted);
}

.theory-fig figcaption b {
  color: var(--text-secondary);
  font-weight: 600;
}

.katex {
  font-size: 1.05em;
}

/* ── Profile editor ──────────────────────────────────────────────────────────────────────── */
.profile-canvas-wrap {
  position: relative;
  height: 260px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-1);
}

.profile-canvas-wrap canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
  margin: 8px 0;
}

/* ── lil-gui theming ─────────────────────────────────────────────────────────────────────── */
.lil-gui {
  --background-color: rgba(26, 26, 25, 0.94);
  --widget-color: #2c2c2a;
  --hover-color: #383835;
  --focus-color: #444441;
  --number-color: #86b6ef;
  --string-color: #c3c2b7;
  --font-family: var(--font);
  --width: 290px;
}

/* ── Fatal error overlay ─────────────────────────────────────────────────────────────────── */
.fatal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  background: var(--page);
  color: var(--text-secondary);
}

.fatal[hidden] {
  display: none;
}

[hidden] {
  display: none !important;
}

/* ── Narrow screens: stack the side panel under the 3D view ─────────────────────────────── */
@media (max-width: 900px) {
  #app {
    height: auto;
    min-height: 100dvh;
  }

  #main {
    flex-direction: column;
  }

  #viewport {
    height: 62vh;
    min-height: 360px;
    flex: none;
  }

  #sidepanel {
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--border);
  }

  .tab-body {
    overflow: visible;
  }

  .status {
    margin-left: 0;
  }

  .subtitle {
    display: none;
  }

  .lil-gui {
    --width: 250px;
  }

  /* Camera buttons occupy the first row; the parameter panel starts below them. */
  #gui-host {
    top: 46px;
    max-height: calc(100% - 56px);
  }

  .colorbar {
    width: min(240px, 100%);
    font-size: 11px;
    padding: 6px 8px;
  }
}
