/* Themes.
 *
 * --canvas-bg is the ISOMETRIC's paper and stays white in every theme; a
 * fabrication drawing is read on white and inverting it would misrepresent what
 * the DXF and the PDF actually look like.  Form controls therefore take
 * --input-bg, NOT --canvas-bg: on a dark theme a control on white paper with
 * --text on it is white on white, which is exactly how the colour-by dropdown
 * became unreadable.  The 3D scene is a model, not paper, so it does follow the
 * theme through --v3d-*. */
:root {
  --bg: #f5f6f8;
  --panel-bg: #ffffff;
  --input-bg: #ffffff;
  --canvas-bg: #ffffff;
  --text: #1a1d23;
  --muted: #5b6270;
  --border: #d7dbe0;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --error-bg: #fdecec;
  --error-text: #b3261e;
  --radius: 8px;

  --v3d-bg: #e6ebf1;
  --v3d-wire: #47546b;
  --v3d-tag-bg: rgba(255, 255, 255, .93);
  --v3d-tag-fg: #8a4b00;
  --v3d-tag-line: #8c97a8;
  --v3d-detail-edge: #7a8394;
  --v3d-ui-bg: rgba(255, 255, 255, .9);
  --v3d-ui-fg: #1a1d23;
  --v3d-ui-border: #c2c9d3;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #14161a;
  --panel-bg: #1c1f26;
  --input-bg: #232733;
  --canvas-bg: #ffffff;
  --text: #e8eaed;
  --muted: #9aa1ac;
  --border: #333844;
  --accent: #4f8cff;
  --accent-hover: #6ea0ff;
  --error-bg: #3a1f1f;
  --error-text: #ff8a80;

  --v3d-bg: #10141b;
  --v3d-wire: #aebbcb;
  --v3d-tag-bg: rgba(18, 22, 30, .9);
  --v3d-tag-fg: #ffd479;
  --v3d-tag-line: #6f7d92;
  --v3d-detail-edge: #5c6980;
  --v3d-ui-bg: rgba(24, 29, 38, .85);
  --v3d-ui-fg: #dfe4ec;
  --v3d-ui-border: #3a4250;
}

/* Modern: near-black chrome, one saturated accent, generous corner radius. */
html[data-theme="modern"] {
  color-scheme: dark;
  --bg: #0d0f14;
  --panel-bg: #161a22;
  --input-bg: #1f242f;
  --canvas-bg: #ffffff;
  --text: #eef1f6;
  --muted: #949eb0;
  --border: #262c38;
  --accent: #6d5efc;
  --accent-hover: #8b7ffd;
  --error-bg: #2e1b26;
  --error-text: #ff8ab0;
  --radius: 12px;

  --v3d-bg: #0a0c11;
  --v3d-wire: #b9c4d6;
  --v3d-tag-bg: rgba(14, 17, 23, .92);
  --v3d-tag-fg: #c9c2ff;
  --v3d-tag-line: #6d5efc;
  --v3d-detail-edge: #6d5efc;
  --v3d-ui-bg: rgba(22, 26, 34, .88);
  --v3d-ui-fg: #eef1f6;
  --v3d-ui-border: #333a49;
}

/* Classic: drafting-office paper, square corners, ink-blue accent. */
html[data-theme="classic"] {
  color-scheme: light;
  --bg: #e9e6dd;
  --panel-bg: #f5f3ec;
  --input-bg: #ffffff;
  --canvas-bg: #ffffff;
  --text: #1b1a15;
  --muted: #5d584c;
  --border: #b6b0a0;
  --accent: #14487f;
  --accent-hover: #0e3560;
  --error-bg: #f6e2df;
  --error-text: #9c2118;
  --radius: 2px;

  --v3d-bg: #dcd9cd;
  --v3d-wire: #3c4452;
  --v3d-tag-bg: rgba(255, 255, 253, .95);
  --v3d-tag-fg: #7a3f00;
  --v3d-tag-line: #8a8474;
  --v3d-detail-edge: #8a8474;
  --v3d-ui-bg: rgba(245, 243, 236, .94);
  --v3d-ui-fg: #1b1a15;
  --v3d-ui-border: #a9a291;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

/* ── App shell ── */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  padding: 14px 18px;
  gap: 10px;
}

.app-header h1 { margin: 0 0 2px; font-size: 1.2rem; }
.app-header p  { margin: 0; color: var(--muted); font-size: .82rem; }

/* ── Controls ── */
.controls {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* Top strip: always visible — file picker + quick-action buttons + toggle */
.controls-topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

/* Secondary controls: collapsible on mobile, always open on desktop */
.controls-panel {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

/* Toggle button: hidden on desktop */
.controls-toggle { display: none; }

.btn, .file-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 13px;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: .84rem;
  color: var(--text);
  white-space: nowrap;
}
.btn:hover:not(:disabled), .file-label:hover { border-color: var(--accent); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.active { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.file-label input { display: none; }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }

.switch { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.switch button {
  padding: 7px 12px;
  background: var(--panel-bg);
  color: var(--text);
  border: none;
  cursor: pointer;
  font-size: .82rem;
}
.switch button.active { background: var(--accent); color: #fff; }

.toggles {
  display: flex;
  align-items: center;
  gap: 13px;
  flex-wrap: wrap;
  font-size: .81rem;
  color: var(--muted);
}
.toggles label { display: inline-flex; align-items: center; gap: 5px; cursor: pointer; }

.file-name {
  color: var(--muted);
  font-size: .82rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 180px;
}

.breaks { display: inline-flex; align-items: center; gap: 6px; }
.breaks-label { font-size: .81rem; color: var(--muted); white-space: nowrap; }
.breaks-input {
  width: 140px;
  padding: 5px 8px;
  font-size: .82rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--input-bg);
  color: var(--text);
}
.breaks-input:focus { outline: none; border-color: var(--accent); }

/* Theme picker.  A cycling button was fine for two themes and is guesswork at
 * four, so the palettes are named in a list. */
.theme-select {
  padding: 7px 9px;
  font-size: .82rem;
  background: var(--input-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
}
.theme-select:focus { outline: none; border-color: var(--accent); }

/* Every native dropdown, everywhere.  Firefox and Chrome draw the POPUP from
 * the option's own background, which does not inherit the select's -- so an
 * unstyled option falls back to white while the text stays light. */
select option {
  background: var(--panel-bg);
  color: var(--text);
}

/* ── Status ── */
.status {
  padding: 7px 12px;
  border-radius: 6px;
  font-size: .83rem;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  width: fit-content;
}
.status.error { background: var(--error-bg); color: var(--error-text); border-color: var(--error-text); }

/* ── Preview ── */
.preview {
  flex: 1;
  min-height: 0;
  background: var(--canvas-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  position: relative;
}

.preview-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--muted);
  font-size: .92rem;
  padding: 20px;
  text-align: center;
}

svg.iso { width: 100%; height: 100%; display: block; cursor: grab; touch-action: none; }
svg.iso:active { cursor: grabbing; }

/* ── Overlays ── */
.overlay {
  position: absolute;
  z-index: 2;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: .78rem;
  color: var(--text);
}
.badge { top: 10px; left: 10px; padding: 5px 10px; font-weight: 600; }
.sheet-pager {
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  padding: 4px 8px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.sheet-pager button {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 5px;
  cursor: pointer;
  padding: 3px 9px;
  font-size: .8rem;
}
.sheet-pager button:disabled { opacity: .4; cursor: not-allowed; }
.reset-btn { top: 10px; right: 10px; padding: 5px 10px; cursor: pointer; }

.legend {
  bottom: 10px;
  left: 10px;
  display: flex;
  gap: 10px;
  padding: 5px 10px;
  flex-wrap: wrap;
  color: var(--muted);
  max-width: calc(100% - 20px);
}
.legend span { display: inline-flex; align-items: center; gap: 4px; }
.swatch { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }

/* ── Stress legend ── */
.stress-legend {
  bottom: 10px;
  right: 10px;
  padding: 7px 10px;
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.stress-title { font-weight: 600; letter-spacing: .03em; }
.stress-note  { color: var(--muted); font-size: .72rem; }
.stress-row   { display: flex; gap: 7px; margin-top: 5px; }
.stress-bar {
  width: 13px;
  height: 92px;
  border-radius: 3px;
  border: 1px solid var(--border);
  display: block;
  background: linear-gradient(to top,
    rgb(13,38,230) 0%, rgb(0,191,242) 25%, rgb(26,204,51) 50%,
    rgb(250,217,13) 75%, rgb(230,26,13) 100%);
}
.stress-scale {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  color: var(--muted);
  font-size: .72rem;
}

/* ════════════════════════════════════════════════
   Mobile  ≤ 660 px
   ════════════════════════════════════════════════ */
@media (max-width: 660px) {

  /* Shrink shell padding and gaps */
  .app-shell { padding: 8px 10px; gap: 6px; }

  /* Header: one compact line instead of two */
  .app-header { display: flex; align-items: baseline; gap: 10px; }
  .app-header h1 { font-size: .95rem; white-space: nowrap; }
  .app-header p  { font-size: .74rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  /* Show the toggle; hide secondary controls until open */
  .controls-toggle  { display: inline-flex; }
  .controls-panel   { display: none; }
  .controls-panel.open { display: flex; }

  /* Top-bar: file + name fill available space, quick buttons stay right */
  .controls-topbar  { flex-wrap: nowrap; gap: 6px; }
  .file-name        { flex: 1; min-width: 0; max-width: none; }

  /* Smaller buttons on mobile */
  .btn, .file-label { padding: 6px 10px; font-size: .80rem; }
  .switch button    { padding: 6px 9px;  font-size: .78rem; }

  /* Narrower text inputs */
  .breaks-input { width: 90px; }

  /* Toggles: compact 2-column grid */
  .toggles {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px 16px;
    width: 100%;
    font-size: .80rem;
  }

  /* Legend: icon-only swatches to save width */
  .legend { gap: 8px; font-size: .72rem; padding: 4px 8px; }
  .legend span span { display: none; } /* hide text labels, keep swatches */

  /* Pager: tighter */
  .sheet-pager { gap: 5px; font-size: .76rem; }
  .sheet-pager button { padding: 3px 7px; font-size: .76rem; }

  /* Badge: shorter text via CSS, smaller font */
  .badge { font-size: .72rem; padding: 4px 8px; }

  /* Stress legend: smaller */
  .stress-bar   { height: 70px; }
  .stress-legend { padding: 5px 8px; }
}

/* ── 3D model viewer ──────────────────────────────────────────────────────
   Canvas left, control rail right.  The rail scrolls on its own: the
   inspector lists every column CAESAR holds for an element, which is far
   taller than the viewport on a full element record. */
.viewer3d-host { padding: 0; }

.v3d-root {
  display: flex;
  height: 100%;
  min-height: 0;
}

/* Fullscreen.  The browser hands the element the whole screen and nothing else:
 * outside .app-shell it has no height to inherit and no background of its own,
 * so both are restated here. */
.v3d-root:fullscreen {
  width: 100vw;
  height: 100vh;
  border: none;
  border-radius: 0;
  background: var(--panel-bg);
}

.v3d-canvas {
  flex: 1;
  min-width: 0;
  position: relative;
}
.v3d-canvas canvas { display: block; }

/* Node callouts: HTML bubbles laid out over the canvas by the viewer, which
 * positions each one with a transform.  The layer never takes the pointer, so
 * a bubble cannot swallow a click meant for the pipe behind it. */
.v3d-tags {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 1;
}
.v3d-tag {
  position: absolute;
  top: 0;
  left: 0;
  padding: 1px 6px 2px;
  border: 1px solid var(--v3d-tag-line);
  border-radius: 9px;
  background: var(--v3d-tag-bg);
  color: var(--v3d-tag-fg);
  font: 600 11px/1.35 ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: nowrap;
  will-change: transform;
}
/* Leader from the bubble's lower-left corner down to the node it belongs to,
 * which the viewer places exactly one TAG_OFFSET away on both axes. */
.v3d-tag::before {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  width: 18px;
  height: 1px;
  background: var(--v3d-tag-line);
  transform-origin: 0 50%;
  transform: rotate(135deg);
}

/* Navigation sits on the canvas, bottom-left, where the cursor already is. */
.v3d-overlay {
  position: absolute;
  left: 10px;
  bottom: 10px;
  display: flex;
  gap: 6px;
  z-index: 2;
}
.v3d-obtn {
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--v3d-ui-border);
  border-radius: 6px;
  background: var(--v3d-ui-bg);
  color: var(--v3d-ui-fg);
  font-size: .95rem;
  line-height: 1;
  cursor: pointer;
}
.v3d-obtn:hover { border-color: var(--accent); color: var(--accent); }
.v3d-obtn.active {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.v3d-rail {
  width: 300px;
  flex: 0 0 300px;
  border-left: 1px solid var(--border);
  background: var(--panel-bg);
  padding: 12px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  font-size: .82rem;
}

/* Folded away by the ▤ button, so the model gets the full width. */
.v3d-root.rail-off .v3d-rail { display: none; }

/* Grip along the bottom edge for setting the viewer's height.  It sits over
 * the canvas rather than beside it, so giving the model more of the page does
 * not cost a strip of the page to the handle itself. */
.v3d-grip {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 9px;
  cursor: ns-resize;
  z-index: 3;
  touch-action: none;
}
.v3d-grip::before {
  content: "";
  display: block;
  width: 46px;
  height: 3px;
  margin: 3px auto 0;
  border-radius: 2px;
  background: var(--border);
}
.v3d-grip:hover::before { background: var(--accent); }
/* Fullscreen is the whole screen by definition -- there is no height to set. */
.v3d-root:fullscreen .v3d-grip { display: none; }

.v3d-lbl {
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  font-size: .70rem;
  letter-spacing: .04em;
}

.v3d-select {
  width: 100%;
  padding: 7px 8px;
  background: var(--input-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 7px;
  font-size: .82rem;
}
.v3d-select:focus { outline: none; border-color: var(--accent); }

/* Colour-scale legend: one block per band, labelled with the band's own range.
 * A continuous gradient bar cannot be read back off the model -- you can see
 * that a run is greenish but not which value that is.  Banded, the colour on
 * the pipe matches exactly one block, and the block carries the number. */
/* The key floats in the top right of the model, not down in the rail, so the
 * colour on the pipe and the block it matches are in the same glance.  It is
 * capped in height and scrolls: a six-band scale is short, but a categorical
 * key on a model with every support type is not. */
.v3d-legend {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 2;
  min-width: 132px;
  max-width: 250px;
  max-height: calc(100% - 80px);
  overflow-y: auto;
  padding: 7px 9px 8px;
  border: 1px solid var(--v3d-ui-border);
  border-radius: 8px;
  background: var(--v3d-ui-bg);
  color: var(--v3d-ui-fg);
}
.v3d-legend-title {
  font-weight: 700;
  font-size: .68rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  opacity: .7;
  margin-bottom: 5px;
}
/* The unit, once, under the heading -- so no block has to carry it.  The pull
 * up closes the gap the title leaves when there is no unit line to fill it. */
.v3d-legend-unit {
  font-size: .68rem;
  opacity: .55;
  margin: -4px 0 5px;
}
/* Over the model the labels have to hold their own against the pipe behind
 * them, so they take the overlay's foreground rather than the muted grey they
 * wore in the rail. */
.v3d-legend .v3d-block,
.v3d-legend .v3d-key,
.v3d-legend .v3d-legend-flat { color: var(--v3d-ui-fg); }
.v3d-legend .v3d-legend-flat { background: none; padding: 0; }
/* One column: a floating panel that widens to two would start covering model. */
.v3d-legend .v3d-keys { grid-template-columns: 1fr; }

.v3d-blocks { display: flex; flex-direction: column; gap: 2px; }
.v3d-block {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: .74rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.v3d-block i {
  width: 22px;
  height: 13px;
  flex: 0 0 22px;
  border-radius: 3px;
  border: 1px solid rgba(0, 0, 0, .25);
}
.v3d-block.on { color: var(--text); font-weight: 700; }

/* the same block, inline beside a value in the inspector */
.v3d-chip {
  display: inline-block;
  width: 20px;
  height: 11px;
  border-radius: 3px;
  border: 1px solid rgba(0, 0, 0, .3);
  vertical-align: -1px;
  margin-right: 5px;
}
.v3d-legend-flat {
  font-size: .78rem;
  color: var(--muted);
  padding: 6px 8px;
  background: var(--bg);
  border-radius: 6px;
}
.v3d-legend-flat b { color: var(--text); }

/* categorical key: two columns of swatches, so a dozen entries stay short */
.v3d-keys {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3px 10px;
}
.v3d-key {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: .74rem;
  color: var(--muted);
}
.v3d-key i {
  width: 11px;
  height: 11px;
  flex: 0 0 11px;
  border-radius: 3px;
  border: 1px solid rgba(0, 0, 0, .25);
}

.v3d-toggles { display: flex; flex-direction: column; gap: 6px; }
.v3d-check {
  display: flex;
  align-items: center;
  gap: 7px;
  cursor: pointer;
  color: var(--muted);
}

.v3d-views { display: flex; flex-wrap: wrap; gap: 5px; }
.v3d-btn {
  flex: 1 0 auto;
  padding: 5px 9px;
  background: var(--input-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  font-size: .76rem;
}
.v3d-btn:hover { border-color: var(--accent); }

/* inspector */
.v3d-inspector {
  border-top: 1px solid var(--border);
  padding-top: 10px;
  margin-top: 2px;
}
.v3d-hint { color: var(--muted); font-size: .78rem; line-height: 1.45; }
.v3d-ihead { font-weight: 700; margin-bottom: 4px; }
.v3d-iname { font-weight: 400; color: var(--muted); }
.v3d-itags {
  font-size: .74rem;
  color: var(--muted);
  margin-bottom: 7px;
}

.v3d-itable {
  width: 100%;
  border-collapse: collapse;
  font-size: .74rem;
}
.v3d-itable td {
  padding: 3px 4px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.v3d-itable td:first-child {
  color: var(--muted);
  width: 52%;
  word-break: break-word;
}
.v3d-itable td:last-child { text-align: right; font-variant-numeric: tabular-nums; }
.v3d-itable i { color: var(--muted); font-size: .70rem; }

/* the field the model is currently coloured by */
.v3d-hi td {
  background: var(--bg);
  font-weight: 700;
  color: var(--text);
}

@media (max-width: 900px) {
  .v3d-root { flex-direction: column; }
  .v3d-canvas { flex: 1 1 55%; min-height: 260px; }
  .v3d-rail {
    width: 100%;
    flex: 1 1 45%;
    border-left: none;
    border-top: 1px solid var(--border);
  }
}
