/* The demo page's one stylesheet: the layout (a navy sidebar, then the view, controls and results)
   and colours follow solidmesh's and manifold-rust's demo sites, which are light-only, so this is
   too (`color-scheme: light` keeps a dark-mode browser's form controls legible on the light page).
   At phone width the sidebar becomes a block at the top, and everything stacks in one column with
   no horizontal scroll. */

:root {
  color-scheme: light;
  --sidebar-width: 260px;
  --bg: #f6f7f9;
  --bg-card: #ffffff;
  --text: #14213d;
  --text-secondary: #4a5568;
  --text-muted: #8a94a6;
  --accent: #2a9d8f;
  --accent-light: #e3f4f1;
  --danger: #c0392b;
  --border: #dde2ea;
  --border-light: #eef1f5;
  --sidebar-bg: #14213d;
  --sidebar-text: #c9d1e0;
  --radius: 8px;
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
}

/* ===================== Sidebar ===================== */

#sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: var(--sidebar-width);
  height: 100vh;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  overflow-y: auto;
  z-index: 100;
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  padding: 24px 20px 16px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}

.logo {
  font-size: 1.5rem;
  font-weight: 700;
  color: #fff;
  letter-spacing: -0.5px;
}

.tagline {
  font-size: 0.82rem;
  color: rgba(255,255,255,0.45);
  margin-top: 4px;
}

.honesty {
  padding: 16px 20px;
  flex: 1;
  font-size: 0.86rem;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.honesty strong { color: #fff; }

.sidebar-footer {
  padding: 16px 20px;
  border-top: 1px solid rgba(255,255,255,0.08);
  font-size: 0.78rem;
}

.license { color: rgba(255,255,255,0.35); }
.license a { color: rgba(255,255,255,0.6); }

/* ===================== Main content ===================== */

#main-content {
  margin-left: var(--sidebar-width);
  min-height: 100vh;
  padding: 18px 24px;
}

.demo-page {
  max-width: 1320px;
  margin: 0 auto;
}

.demo-header { margin-bottom: 14px; }

.demo-header h2 { font-size: 1.4rem; margin-bottom: 2px; }

.demo-header p { color: var(--text-secondary); font-size: 0.92rem; }

.demo-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 290px;
  grid-template-rows: minmax(300px, calc(100vh - 340px)) auto;
  grid-template-areas:
    "view controls"
    "results controls";
  gap: 14px;
}

.demo-canvas-area {
  grid-area: view;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  position: relative;
  min-height: 300px;
}

.demo-canvas-area canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
}

/* The status badge on the view: what the solve is doing and that the old result is showing. */
.viewer-state {
  position: absolute;
  top: 10px;
  right: 10px;
  padding: 4px 10px;
  border-radius: 12px;
  background: rgba(20, 33, 61, 0.85);
  color: #fff;
  font-size: 0.78rem;
  opacity: 0;
  transition: opacity 0.15s;
  pointer-events: none;
}

.viewer-state.visible { opacity: 1; }

.legend {
  position: absolute;
  top: 10px;
  left: 10px;
  width: 170px;
  padding: 8px 10px;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.74rem;
  pointer-events: none;
}

.legend.muted { opacity: 0.45; }

.legend-title { font-weight: 600; margin-bottom: 4px; }

.legend-scale {
  display: flex;
  gap: 8px;
  align-items: stretch;
  height: 86px;
}

.legend-bar {
  width: 14px;
  border-radius: 3px;
}

.legend-labels {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  font-weight: 600;
}

.legend-note { color: var(--text-secondary); margin-top: 6px; line-height: 1.3; }

.exaggeration {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.82rem;
  font-weight: 600;
}

.demo-controls {
  grid-area: controls;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  align-self: start;
}

/* ===================== Controls ===================== */

.control-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.control-group label {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-secondary);
}

.slider-value {
  font-weight: 600;
  color: var(--accent);
  text-align: right;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 6px;
  border-radius: 3px;
  background: var(--border);
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

input[type="range"]::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

input:focus-visible, select:focus-visible, button:focus-visible {
  outline: 2px solid var(--text);
  outline-offset: 2px;
}

.slider-hold { display: flex; }

/* On a touch screen the wrapper takes the slider's input (src/slider-touch.ts): vertical pans
   scroll the page, and only a clear sideways drag moves the value. */
@media (pointer: coarse) {
  .slider-hold {
    touch-action: pan-y;
    padding: 8px 0;
    margin: -8px 0;
  }

  .slider-hold input[type="range"] { pointer-events: none; }
}

select {
  width: 100%;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: #fff;
  font-size: 0.9rem;
  color: var(--text);
}

.segmented {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
}

.segmented button {
  flex: 1;
  padding: 6px 4px;
  border: none;
  border-right: 1px solid var(--border);
  background: #fff;
  color: var(--text-secondary);
  font-size: 0.82rem;
  cursor: pointer;
}

.segmented button:last-child { border-right: none; }

.segmented button.active {
  background: var(--accent);
  color: #fff;
}

.control-btn {
  padding: 4px 10px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 0.8rem;
  cursor: pointer;
}

.control-btn:hover { background: #23897d; }

/* ===================== Results ===================== */

.results {
  grid-area: results;
  padding: 12px 14px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  min-width: 0;
}

.results-head {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 12px;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 6px;
}

.results-head h3 { font-size: 1rem; }

.status { font-size: 0.8rem; color: var(--text-secondary); }

.state-pill {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 10px;
  background: var(--accent-light);
  color: #1b7a6f;
  font-weight: 600;
}

/* A new study has started but this table still shows the previous one's numbers. */
.results.stale .comparison-table, .results.stale .peak-row { opacity: 0.55; }

/* The estimate is still moving: live numbers, dimmed, and no difference read as a verdict. */
.results.solving .peak-row { opacity: 0.6; }
.comparison-table tr.row-solving td { color: var(--text-muted); }
.comparison-table td.difference { font-weight: 600; }
.comparison-table tr.row-solving td.difference { font-weight: 400; font-style: italic; }

/* A finished result outside the linear model: the warning, and a comparison marked as void. */
.model-warning {
  display: none;
  margin: 2px 0 8px;
  padding: 8px 10px;
  background: #fff8e1;
  border: 1px solid #e9c46a;
  border-radius: 6px;
  color: #6d4c00;
  font-size: 0.86rem;
}

.model-warning.visible { display: block; }

.comparison-table tr.row-not-meaningful td { color: var(--text-muted); text-decoration: line-through; }
.comparison-table tr.row-not-meaningful td.difference { text-decoration: none; color: #6d4c00; font-weight: 600; }
.results.outside-model .peak-row { opacity: 0.7; }

.comparison-table table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.84rem;
}

.comparison-table th, .comparison-table td {
  padding: 4px 8px 4px 0;
  border-bottom: 1px solid var(--border-light);
  text-align: left;
  vertical-align: top;
}

.comparison-table th { font-weight: 600; font-size: 0.78rem; color: var(--text-muted); }

.comparison-table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

.comparison-table small { color: var(--text-muted); display: block; font-size: 0.74rem; }

.comparison-table .num small { display: inline; }

.comparison-note { font-size: 0.86rem; color: var(--text-secondary); }

.peak-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 28px;
  margin-top: 10px;
}

.peak .k { display: block; font-size: 0.78rem; color: var(--text-muted); }

.peak .v { font-size: 1.3rem; font-weight: 700; color: var(--text); }

.note-line {
  margin-top: 6px;
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.error-line {
  margin-top: 6px;
  font-size: 0.82rem;
  color: var(--danger);
}

/* ===================== Phone width ===================== */

@media (max-width: 900px) {
  #sidebar {
    position: static;
    width: 100%;
    height: auto;
  }

  .sidebar-footer { display: none; }

  #main-content {
    margin-left: 0;
    padding: 12px;
  }

  .demo-layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto;
    grid-template-areas:
      "view"
      "controls"
      "results";
  }

  .demo-canvas-area {
    height: 72vh;
    min-height: 340px;
  }

  .legend { width: 140px; }

  .demo-controls { align-self: auto; }
}
