/* Kagora — Figma/Adobe-style app shell on a "warm technical instrument" skin.
   The design merges two of Jonas's own repos:
     • org/jonasjohansson/play-plus  — warm-charcoal dark palette, white accent,
       Atkinson (Next sans + Mono) fonts, 8px radius, 2px borders, MONO for
       labels/data.
     • org/svartljus/calculed         — the calm 8-hue strip palette + the
       light/dark support Kagora already had.

   Theming is now token-driven (not the system Canvas/CanvasText keywords) so a
   user-controllable toggle can override the OS. `:root[data-theme]` selects the
   set; absent that, prefers-color-scheme picks. The canvas reads these same
   custom properties via getComputedStyle (see canvas.js _theme), so the shell
   and the stage switch together and are deterministic under headless testing. */

@font-face {
  font-family: 'Atkinson';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url('../fonts/AtkinsonHyperlegibleNext-Regular.woff2') format('woff2');
}
@font-face {
  font-family: 'Atkinson';
  font-style: normal;
  font-weight: 700;
  font-display: swap;
  src: url('../fonts/AtkinsonHyperlegibleNext-Bold.woff2') format('woff2');
}
@font-face {
  font-family: 'Atkinson Mono';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url('../fonts/AtkinsonHyperlegibleMono-Regular.woff2') format('woff2');
}
@font-face {
  font-family: 'Atkinson Mono';
  font-style: normal;
  font-weight: 700;
  font-display: swap;
  src: url('../fonts/AtkinsonHyperlegibleMono-Bold.woff2') format('woff2');
}

/* ---------- theme token sets ----------
   Three layers decide the active set, most specific wins:
     1. :root[data-theme="dark"]  / [="light"]  — explicit user toggle
     2. @media (prefers-color-scheme: dark)      — OS default when unset
     3. :root base                                — warm-light fallback
   Every surface/line/ink below is a flat token (no currentColor mixing) so the
   canvas can read the exact same value via getComputedStyle. */

:root {
  color-scheme: light dark;

  /* Warm-light (default / OS-light). Counterpart of the play-plus dark family. */
  --bg:          #f7f5f2;
  --bg-surface:  #ffffff;
  --bg-elevated: #ffffff;
  --fg:          #2b2926;
  --fg-title:    #1a1918;
  --fg-muted:    #6b6660;
  --fg-disabled: #b3ada4;
  --border-color:#d8d3cc;
  --accent:      #1a1918;          /* charcoal accent in light */
  --accent-ink:  #f7f5f2;          /* text/icon on an accent fill */
  --radius:      8px;
  --border-w:    2px;

  /* Derived lines & surfaces (token-based so the canvas can mirror them) */
  --rule:  var(--border-w) solid var(--border-color);
  --rule-color: var(--border-color);
  --muted: var(--fg-muted);
  --soft:  var(--bg);
  --soft-2: color-mix(in srgb, var(--fg) 8%, var(--bg-surface));

  /* Status colors — read on both warm-light and warm-dark */
  --ok:    #16a34a;
  --warn:  #dc2626;
  --amber: #d97706;

  /* Canvas-specific tokens (read by canvas.js _theme via getComputedStyle).
     Signal = clear blue, power = warm red/amber; tuned to sit on warm surfaces. */
  --canvas-stage:    #efece7;
  --canvas-grid:     #d8d3cc;
  --canvas-node-fill:#ffffff;
  --canvas-port-ring:#ffffff;
  --canvas-signal:   #2f6df6;      /* data / signal cable + data-out ports */
  --canvas-signal-in:#16a34a;      /* data-in ports */
  --canvas-power:    #d9483b;      /* power cable + power ports */

  /* Per-strip-card hue palette (calm, distinguishable; from calculed). */
  --strip-hue-1: #fde68a;  /* amber  */
  --strip-hue-2: #bfdbfe;  /* blue   */
  --strip-hue-3: #bbf7d0;  /* mint   */
  --strip-hue-4: #fbcfe8;  /* pink   */
  --strip-hue-5: #c7d2fe;  /* indigo */
  --strip-hue-6: #fed7aa;  /* peach  */
  --strip-hue-7: #d8b4fe;  /* violet */
  --strip-hue-8: #99f6e4;  /* teal   */

  /* Typography */
  --font:      'Atkinson', system-ui, -apple-system, sans-serif;
  --font-sans: 'Atkinson', system-ui, -apple-system, sans-serif;
  --font-mono: 'Atkinson Mono', 'Courier New', Courier, monospace;
  --fs-mini:  0.7rem;
  --fs-small: 0.85rem;
  --fs-base:  13px;
  --fs-large: 1.1rem;

  /* Spacing scale */
  --s-1: 0.25rem;
  --s-2: 0.5rem;
  --s-3: 0.75rem;
  --s-4: 1rem;
  --s-5: 1.25rem;
  --s-6: 1.5rem;
  --s-8: 2rem;

  /* Radii — instrument feel: small controls 6px, surfaces 8px (--radius). */
  --r-sm: 6px;
  --r-md: var(--radius);

  /* Transitions */
  --t-fast: 0.1s;
  --t-base: 0.15s;

  /* Shell metrics */
  --topbar-h: 44px;
  --sidebar-w: 288px;

  /* Print page margin (used by the @media print Layout-only sheet). */
  --print-margin: 12mm;

  font: var(--fs-base)/1.45 var(--font);
  accent-color: var(--accent);
}

/* Warm-dark token set (from play-plus). Two selectors share it: an explicit
   user toggle (data-theme="dark") and OS-dark *only when the user hasn't
   chosen* (handled by the @media block below, which is overridden by an
   explicit light toggle since :root[data-theme="light"] sets the light set). */
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg:          #1a1918;
  --bg-surface:  #23211f;
  --bg-elevated: #2b2926;
  --fg:          #e0e0e0;
  --fg-title:    #f0f0f0;
  --fg-muted:    #888888;
  --fg-disabled: #555555;
  --border-color:#333333;
  --accent:      #ffffff;          /* white accent in dark */
  --accent-ink:  #1a1918;
  --soft:        var(--bg-surface);
  --soft-2:      var(--bg-elevated);

  --canvas-stage:    #211f1d;
  --canvas-grid:     #3a3633;
  --canvas-node-fill:#2b2926;
  --canvas-port-ring:#1a1918;
  --canvas-signal:   #4d8bff;      /* clear blue on charcoal */
  --canvas-signal-in:#3fbf6a;      /* lifted green for data-in */
  --canvas-power:    #e8694f;      /* warm red/amber on charcoal */
}

/* OS-dark default — applies only when no explicit theme is set. An explicit
   [data-theme="light"]/[="dark"] always wins (higher/equal specificity, later
   or attribute-scoped), so the toggle overrides the OS. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]):not([data-theme="dark"]) {
    color-scheme: dark;
    --bg:          #1a1918;
    --bg-surface:  #23211f;
    --bg-elevated: #2b2926;
    --fg:          #e0e0e0;
    --fg-title:    #f0f0f0;
    --fg-muted:    #888888;
    --fg-disabled: #555555;
    --border-color:#333333;
    --accent:      #ffffff;
    --accent-ink:  #1a1918;
    --soft:        var(--bg-surface);
    --soft-2:      var(--bg-elevated);

    --canvas-stage:    #211f1d;
    --canvas-grid:     #3a3633;
    --canvas-node-fill:#2b2926;
    --canvas-port-ring:#1a1918;
    --canvas-signal:   #4d8bff;
    --canvas-signal-in:#3fbf6a;
    --canvas-power:    #e8694f;
  }
}

* { box-sizing: border-box; }
input, select, button, textarea, output { font-family: inherit; }

html, body {
  margin: 0;
  height: 100%;
  /* Panel surface = the warm bg; the stage tints itself separately. */
  background: var(--bg);
  color: var(--fg);
  font: var(--fs-base)/1.45 var(--font);
}
.muted { color: var(--muted); }
.small { font-size: var(--fs-small); }

/* ---------- shell grid ---------- */
#app {
  display: grid;
  grid-template-columns: 1fr var(--sidebar-w);
  grid-template-rows: var(--topbar-h) 1fr;
  grid-template-areas:
    "topbar  topbar"
    "stage   sidebar";
  height: 100vh;
  width: 100vw;
}
/* Non-Layout views (Library, BOM) hide the floating tool toolbar; it reappears
   in Layout with its tool state intact (it's only display-toggled). */
#app[data-view="library"] #toolrail,
#app[data-view="bom"] #toolrail { display: none; }

/* ---------- top bar ---------- */
#topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  gap: var(--s-3);
  padding: 0 var(--s-3);
  border-bottom: var(--rule);
  background: var(--bg);
}
.topbar-actions { display: flex; align-items: center; gap: var(--s-1); }
.topbar-right { display: flex; align-items: center; gap: var(--s-2); margin-left: auto; }
.topbar-divider {
  width: 1px;
  align-self: stretch;
  margin: 0.5rem var(--s-2);
  background: var(--rule-color);
}

/* Menu popovers (File / Import / Export) */
.menu { position: relative; }
.menu > summary {
  list-style: none;
  cursor: pointer;
  font: inherit;
  font-size: var(--fs-small);
  padding: 0.35rem 0.6rem;
  border-radius: var(--r-sm);
  color: inherit;
  user-select: none;
}
.menu > summary::-webkit-details-marker { display: none; }
.menu > summary:hover,
.menu[open] > summary { background: var(--soft); }
.menu-pop {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  z-index: 60;
  min-width: 13rem;
  background: var(--bg-elevated);
  border: var(--rule);
  border-radius: var(--r-md);
  box-shadow: 0 8px 28px color-mix(in srgb, var(--fg-title) 22%, transparent);
  padding: var(--s-1);
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.menu-item {
  font: inherit;
  font-size: var(--fs-small);
  text-align: left;
  padding: 0.4rem 0.6rem;
  border: 0;
  border-radius: var(--r-sm);
  background: transparent;
  color: inherit;
  cursor: pointer;
  white-space: nowrap;
}
.menu-item:hover { background: var(--soft-2); }
.menu-sep { height: 1px; margin: var(--s-1) 0; background: var(--rule-color); }
/* Inline checkbox row inside a menu popover (e.g. the Export "Background"
   toggle). Reads like a menu item but stays put on click instead of acting. */
.menu-check {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  font-size: var(--fs-small);
  padding: 0.4rem 0.6rem;
  border-radius: var(--r-sm);
  cursor: pointer;
  white-space: nowrap;
}
.menu-check:hover { background: var(--soft-2); }
.menu-check input { cursor: pointer; margin: 0; }

/* Icon buttons (undo/redo/gear/close) */
.icon-btn {
  font: inherit;
  font-size: 1rem;
  line-height: 1;
  width: 1.9rem;
  height: 1.9rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: var(--rule);
  border-radius: var(--r-sm);
  background: transparent;
  color: inherit;
  cursor: pointer;
  transition: background var(--t-fast), border-color var(--t-fast);
}
.icon-btn:hover:not(:disabled) { background: var(--soft); }
.icon-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* View switcher (segmented) */
.view-switch {
  display: inline-flex;
  border: var(--rule);
  border-radius: var(--r-sm);
  overflow: hidden;
}
.view-tab {
  font-family: var(--font-mono);
  font-size: var(--fs-small);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 0.3rem 0.7rem;
  border: 0;
  border-right: var(--rule);
  background: transparent;
  color: inherit;
  cursor: pointer;
}
.view-tab:last-child { border-right: 0; }
.view-tab:hover:not(:disabled):not(.active) { background: var(--soft); }
.view-tab.active { background: var(--accent); color: var(--accent-ink); }
.view-tab:disabled { opacity: 0.4; cursor: not-allowed; }

/* Render-mode switch (Live | Brass tacks) — same segmented look as view-switch. */
.mode-switch {
  display: inline-flex;
  border: var(--rule);
  border-radius: var(--r-sm);
  overflow: hidden;
}
.mode-tab {
  font-family: var(--font-mono);
  font-size: var(--fs-small);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 0.3rem 0.7rem;
  border: 0;
  border-right: var(--rule);
  background: transparent;
  color: inherit;
  cursor: pointer;
  white-space: nowrap;
}
.mode-tab:last-child { border-right: 0; }
.mode-tab:hover:not(.active) { background: var(--soft); }
.mode-tab.active { background: var(--accent); color: var(--accent-ink); }
/* Pulse-style toggle is disabled (greyed) while in schematic mode. */
#pulse-style:disabled { opacity: 0.35; cursor: not-allowed; }

/* ---------- floating tool toolbar ---------- */
/* Compact toolbar overlaid on the TOP-LEFT of the canvas (Miro/Figma-style).
   Absolutely positioned inside #stage-wrap, above the canvas; the warm-
   instrument elevated surface with a 2px border + subtle shadow. The legend is
   drawn bottom-left, so they don't collide. */
#toolrail {
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 5;
  background: var(--bg-elevated);
  border: var(--rule);
  border-radius: var(--radius);
  box-shadow: 0 4px 16px color-mix(in srgb, var(--fg-title) 16%, transparent);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: var(--s-2);
  padding: var(--s-2);
}
.tool-group {
  display: inline-flex;
  flex-direction: column;
  border: var(--rule);
  border-radius: var(--r-sm);
  overflow: hidden;
}
/* Divider between the tool group and the view-controls group. */
.tool-group + .tool-group {
  margin-top: var(--s-1);
  position: relative;
}
.tool-group + .tool-group::before {
  content: "";
  position: absolute;
  top: calc(var(--s-1) * -1 - 1px);
  left: 10%;
  right: 10%;
  height: 1px;
  background: var(--rule-color);
}
.tool {
  font-family: var(--font-mono);
  font-size: var(--fs-mini);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0.5rem 0.2rem;
  border: 0;
  border-bottom: var(--rule);
  background: transparent;
  color: inherit;
  cursor: pointer;
  text-align: center;
  transition: background var(--t-fast), color var(--t-fast);
}
.tool:last-child { border-bottom: 0; }
.tool:hover:not(:disabled):not(.active) { background: var(--soft); }
.tool.active { background: var(--accent); color: var(--accent-ink); }
.tool:disabled { opacity: 0.4; cursor: not-allowed; }

/* Problems panel — live validation list in the sidebar (errors first). Empty
   when the design is clean (renderProblems writes nothing). */
.problem-count {
  font-family: var(--font-mono); font-size: var(--fs-mini);
  padding: 0.05rem 0.4rem; border-radius: 999px; margin-left: 0.3rem;
}
.problem-count.err { background: color-mix(in srgb, var(--warn, #df5a48) 22%, transparent); color: var(--warn, #df5a48); }
.problem-count.warn { background: color-mix(in srgb, var(--color-ac230, #e0a93b) 24%, transparent); color: var(--color-ac230, #b07d1a); }
#panel-problems { display: flex; flex-direction: column; gap: 0.2rem; }
.problem-row {
  display: flex; align-items: center; gap: var(--s-1); width: 100%;
  text-align: left; border: 0; background: none; cursor: pointer;
  padding: 0.3rem 0.4rem; border-radius: var(--r-sm); color: inherit;
  font-size: var(--fs-mini);
}
.problem-row:hover { background: var(--soft); }
.problem-dot { flex: 0 0 auto; width: 7px; height: 7px; border-radius: 50%; }
.problem-row.error .problem-dot { background: var(--warn, #df5a48); }
.problem-row.warn .problem-dot { background: var(--color-ac230, #e0a93b); }
.problem-msg { flex: 1 1 auto; }

/* Floating Library/Add palette — top-right of the stage, mirroring #toolrail
   (top-left). The toggle is a single .tool button; the popover is an elevated
   surface card listing placeable types, scrollable, hidden until toggled. */
#palette-float {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: var(--s-2);
}
#palette-float #palette-toggle {
  border: var(--rule);
  border-radius: var(--r-sm);
  background: var(--bg-elevated);
  box-shadow: 0 4px 16px color-mix(in srgb, var(--fg-title) 16%, transparent);
  padding: 0.5rem 0.7rem;
}
#palette-float #palette-toggle[aria-expanded="true"] { background: var(--accent); color: var(--accent-ink); }
#palette-pop {
  width: 260px;
  max-height: min(70vh, 640px);
  overflow-y: auto;
  background: var(--bg-elevated);
  border: var(--rule);
  border-radius: var(--radius);
  box-shadow: 0 8px 28px color-mix(in srgb, var(--fg-title) 22%, transparent);
  padding: var(--s-2);
}
#palette-pop[hidden] { display: none; }
/* The palette panel inside the popover drops its own card chrome (the popover
   provides it) so it reads as one surface. */
#palette-pop #panel-inventory { border: 0; padding: 0; background: none; box-shadow: none; }
/* Non-Layout views hide the floating palette, exactly like the toolrail. */
#app[data-view="library"] #palette-float,
#app[data-view="bom"] #palette-float { display: none; }

/* ---------- center stage ---------- */
#stage-wrap {
  grid-area: stage;
  position: relative;
  overflow: hidden;
  /* Canvas paints its own theme-aware background; this just matches behind it. */
  background: var(--canvas-stage);
}
#stage { display: block; width: 100%; height: 100%; cursor: default; }
#stage.panning { cursor: grabbing; }

/* ---------- right sidebar (properties) ---------- */
#sidebar {
  grid-area: sidebar;
  border-left: var(--rule);
  background: var(--bg);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
#palette-host { flex: 0 0 auto; display: flex; flex-direction: column; }
#layers-host  { flex: 0 0 auto; }
/* The per-selection inspector is the only region that flexes + scrolls, so
   selecting a strip never shoves the palette/layers around. */
#inspector {
  padding: var(--s-3) var(--s-4);
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
}

.panel {
  padding: var(--s-3) var(--s-4);
  border-bottom: var(--rule);
}
.panel h2,
#inspector h2 {
  margin: 0 0 var(--s-2);
  font-family: var(--font-mono);
  font-size: var(--fs-mini);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  font-weight: 700;
}

/* Collapsible panel group (Layers) */
.panel-group { border-bottom: var(--rule); }
.panel-group-summary {
  cursor: pointer;
  list-style: none;
  padding: var(--s-3) var(--s-4);
  font-family: var(--font-mono);
  font-size: var(--fs-mini);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  font-weight: 700;
  user-select: none;
}
.panel-group-summary::-webkit-details-marker { display: none; }
.panel-group-summary::before { content: "▸ "; display: inline-block; color: var(--muted); }
.panel-group[open] > .panel-group-summary::before { content: "▾ "; }
.panel-group > .panel { border-top: var(--rule); border-bottom: 0; }
.panel-group:not([open]) > .panel { display: none; }

/* ---------- inventory (lists Library-designed types as placeable items) ---------- */
.inv-group { margin-bottom: var(--s-2); }
.inv-group:last-child { margin-bottom: 0; }
.inv-group-title {
  font-size: var(--fs-mini);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin: 0 0 var(--s-1);
}

/* ---------- palette strip-type chips ---------- */
.ptype {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  width: 100%;
  text-align: left;
  font: inherit;
  padding: 0.4rem 0.6rem;
  margin-bottom: var(--s-1);
  border: var(--rule);
  border-radius: var(--r-md);
  background: var(--soft);
  color: inherit;
  cursor: pointer;
  transition: background var(--t-fast), border-color var(--t-fast);
}
.ptype:hover { background: var(--soft-2); }
.ptype.active { border-color: var(--accent); background: var(--soft-2); }
.ptype-name { font-weight: 700; font-size: var(--fs-small); }
.ptype-meta { font-family: var(--font-mono); font-size: var(--fs-mini); color: var(--muted); font-variant-numeric: tabular-nums; }

/* ---------- layers panel rows ---------- */
.layer-row {
  display: flex;
  align-items: center;
  gap: var(--s-1);
  padding: 0.25rem 0.3rem;
  margin-bottom: 0.2rem;
  border: 1px solid transparent;
  border-radius: var(--r-md);
  cursor: pointer;
}
.layer-row:hover { background: var(--soft); }
.layer-row.active { border-color: var(--accent); background: var(--soft-2); }
.layer-row .layer-name {
  flex: 1 1 auto;
  width: 100%;
  min-width: 0;
  font-size: var(--fs-small);
}
.layer-btn {
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: var(--fs-small);
  line-height: 1;
  padding: 2px 3px;
  border-radius: var(--r-sm);
  color: inherit;
}
.layer-btn:hover { background: var(--soft-2); }
.layer-btn.layer-active.on { color: var(--accent); }
.layer-btn.layer-up, .layer-btn.layer-down { color: var(--muted); font-size: 0.7rem; }
.layer-btn.layer-up:hover, .layer-btn.layer-down:hover { color: inherit; }
.layer-btn.layer-del { color: var(--muted); font-weight: 700; }
.layer-btn.layer-del:hover { color: var(--warn); }
.layer-btn:disabled { opacity: 0.3; cursor: default; }
.layer-swatch {
  width: 20px;
  height: 20px;
  padding: 0;
  border: var(--rule);
  border-radius: var(--r-sm);
  cursor: pointer;
  flex: 0 0 auto;
}

/* ---------- groups panel (mirrors layers) ---------- */
/* Mains phases panel rows: leg name + load readout; amps go red over 16 A. */
.phase-row { display: flex; align-items: center; justify-content: space-between; gap: var(--s-1); padding: 0.2rem 0.3rem; font-size: var(--fs-mini); }
.phase-name { font-family: var(--font-mono); font-weight: 700; }
.phase-meta { font-family: var(--font-mono); }
.phase-amps.over { color: var(--warn); font-weight: 700; }

.group-row {
  display: flex;
  align-items: center;
  gap: var(--s-1);
  padding: 0.25rem 0.3rem;
  margin-bottom: 0.2rem;
  border: 1px solid transparent;
  border-radius: var(--r-md);
  cursor: pointer;
}
.group-row:hover { background: var(--soft); }
.group-row .group-name {
  flex: 1 1 auto;
  width: 100%;
  min-width: 0;
  font-size: var(--fs-small);
}
.group-btn {
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: var(--fs-small);
  line-height: 1;
  padding: 2px 3px;
  border-radius: var(--r-sm);
  color: inherit;
}
.group-btn:hover { background: var(--soft-2); }
.group-btn.group-ungroup { color: var(--muted); flex: 0 0 auto; }
.group-btn.group-ungroup:hover { color: var(--warn); }
.group-swatch {
  width: 20px;
  height: 20px;
  padding: 0;
  border: var(--rule);
  border-radius: var(--r-sm);
  cursor: pointer;
  flex: 0 0 auto;
}

/* ---------- controller output rows ---------- */
.out-row { display: flex; align-items: center; gap: var(--s-1); margin-bottom: 0.3rem; }
.out-row input { flex: 1 1 auto; width: 100%; }
/* read-only output readout in the main inspector (no editable input) */
.out-row.readonly { justify-content: space-between; }
.out-row.readonly .out-flag { flex: 0 0 auto; }
.ctrl-out-readout { margin: 0.25rem 0 0.4rem; }
/* Editable per-output budget rows inside the Library's aligned table. Grid
   layout so id | budget input | wired-flag line up with the .out-head columns. */
.ctrl-out-table .out-row {
  display: grid;
  grid-template-columns: 5rem 1fr auto;
  align-items: center;
  gap: var(--s-2);
  margin-bottom: 0.3rem;
}
.ctrl-out-table .out-row input { width: 100%; }
.ctrl-out-table .out-flag { text-align: right; justify-self: end; }

/* Structured wiring: per-output daisy-chain builder in the controller inspector */
.ctrl-out-wiring { margin: 0.25rem 0 0.4rem; }
.chain-output { margin-bottom: 0.6rem; padding-bottom: 0.5rem; border-bottom: 1px solid var(--color-border, rgba(128,128,128,0.18)); }
.chain-output:last-child { border-bottom: 0; }
.chain-output .out-row { justify-content: space-between; }
.chain-list { margin: 0.25rem 0; padding-left: 0.4rem; }
.chain-strip { display: flex; align-items: center; justify-content: space-between; gap: var(--s-1); padding: 0.12rem 0; }
.chain-rm {
  flex: 0 0 auto; border: 0; background: none; cursor: pointer;
  color: var(--muted, #8a847b); font-size: var(--fs-mini); line-height: 1; padding: 0.1rem 0.3rem; border-radius: 4px;
}
.chain-rm:hover { color: var(--warn, #df5a48); background: rgba(223,90,72,0.12); }
.chain-add { width: 100%; }

/* ---------- BOM view (own destination, like Library) ---------- */
/* Occupies the whole content area (stage + sidebar columns) when active. Hidden
   in Layout/Library; shown when #app[data-view="bom"]. Driven by #app[data-view]. */
#bom-view {
  grid-column: 1 / -1;
  grid-row: 2 / 3;
  display: none;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  background: var(--bg);
}
#app[data-view="bom"] #bom-view { display: flex; }
#app[data-view="bom"] #stage-wrap,
#app[data-view="bom"] #sidebar { display: none; }
.bom-view-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-3);
  padding: var(--s-3) var(--s-5);
  border-bottom: var(--rule);
  flex: 0 0 auto;
}
.bom-view-head h2 {
  margin: 0;
  font-family: var(--font-mono);
  font-size: var(--fs-mini);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  font-weight: 700;
}
.bom-view-actions { display: flex; gap: var(--s-2); }
#bom {
  overflow-y: auto;
  flex: 1 1 auto;
  padding: var(--s-6) var(--s-5) var(--s-8);
}

/* The materials document — a centered, max-width page that reads as a printed
   spec sheet rather than a panel widget. */
.bom-doc {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: var(--s-6);
}
.bom-doc-title { margin: 0; }
.bom-doc-title h1 {
  margin: 0 0 var(--s-1);
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--fg-title);
  letter-spacing: -0.01em;
}
.bom-doc-sub {
  margin: 0;
  font-family: var(--font-mono);
  font-size: var(--fs-mini);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

/* Summary block: labelled stat cells in a responsive grid. */
.bom-summary {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1px;
  background: var(--rule-color);
  border: var(--rule);
  border-radius: var(--r-md);
  overflow: hidden;
}
@media (max-width: 600px) {
  .bom-summary { grid-template-columns: repeat(2, 1fr); }
}
.bom-stat {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: var(--s-3) var(--s-4);
  background: var(--bg-surface);
}
.bom-stat-label {
  font-family: var(--font-mono);
  font-size: var(--fs-mini);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.bom-stat-value {
  font-family: var(--font-mono);
  font-size: 1.35rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--fg-title);
  line-height: 1.1;
}
.bom-stat-sub {
  font-size: var(--fs-mini);
  color: var(--muted);
}
.bom-stat.warn .bom-stat-value { color: var(--amber); }
.bom-stat.warn .bom-stat-label { color: var(--amber); }

/* Each line-item group: a mono uppercase section header over a CalcuLED table. */
.bom-doc-section { margin: 0; }
.bom-section-head {
  margin: 0 0 var(--s-2);
  padding-bottom: var(--s-1);
  border-bottom: var(--rule);
  font-family: var(--font-mono);
  font-size: var(--fs-mini);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  font-weight: 700;
}

/* CalcuLED-style table — the whole table reads as an instrument: MONO for both
   the column headers and the data cells, with tabular-nums on the numeric cols. */
.bom-table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--font-mono);
  font-size: var(--fs-small);
}
.bom-table th {
  text-align: left;
  font-weight: 700;
  color: var(--muted);
  padding: 0.4rem 0.9rem 0.4rem 0;
  border-bottom: var(--rule);
  text-transform: uppercase;
  font-size: var(--fs-mini);
  letter-spacing: 0.06em;
  white-space: nowrap;
}
.bom-table td {
  padding: 0.4rem 0.9rem 0.4rem 0;
  vertical-align: baseline;
  border-bottom: 1px dashed color-mix(in srgb, var(--fg) 16%, transparent);
  white-space: nowrap;
}
.bom-table tbody td:first-child { font-weight: 700; color: var(--fg); }
.bom-table th.num, .bom-table td.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.bom-table th:last-child, .bom-table td:last-child { padding-right: 0; }
.bom-table tbody tr:last-child td { border-bottom: 0; }
.bom-table tfoot td {
  padding: 0.5rem 0.9rem 0 0;
  border-top: var(--rule);
  border-bottom: 0;
  font-variant-numeric: tabular-nums;
}
.bom-table tfoot td:last-child { padding-right: 0; }

/* Stock check: needed vs available with an over/under flag. */
.bom-stock {
  border: var(--rule);
  border-radius: var(--r-md);
  padding: var(--s-2) var(--s-4);
  font-family: var(--font-mono);
  font-size: var(--fs-small);
  background: var(--bg-surface);
}
.bom-stock-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 0.3rem 0;
}
.bom-stock-row .num { font-variant-numeric: tabular-nums; font-weight: 700; }
.bom-stock-label { color: var(--muted); font-family: var(--font-sans); }
.bom-stock-flag {
  border-top: var(--rule);
  margin-top: 0.2rem;
  padding-top: 0.5rem;
  font-weight: 700;
}
.bom-stock.over { border-color: var(--warn); background: color-mix(in srgb, var(--warn) 8%, var(--bg-surface)); }
.bom-stock.over .bom-stock-flag,
.bom-stock.over .bom-stock-flag .num { color: var(--warn); }
.bom-stock.ok .bom-stock-flag,
.bom-stock.ok .bom-stock-flag .num { color: var(--ok); }

/* ---------- system power reconciliation (placed PSU vs draw) ---------- */
.bom-system-power {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: var(--s-4);
  border: var(--rule);
  border-radius: var(--r-md);
  padding: var(--s-2) var(--s-4);
  margin-bottom: var(--s-4);
  font-family: var(--font-mono);
  font-size: var(--fs-small);
  background: var(--bg-surface);
}
.bom-system-power-label { color: var(--muted); font-family: var(--font-sans); font-weight: 700; }
.bom-system-power-figure { font-variant-numeric: tabular-nums; font-weight: 700; }
.bom-system-power.ok { border-color: var(--ok); }
.bom-system-power.ok .bom-system-power-figure { color: var(--ok); }
.bom-system-power.over { border-color: var(--warn); background: color-mix(in srgb, var(--warn) 8%, var(--bg-surface)); }
.bom-system-power.over .bom-system-power-figure { color: var(--warn); }

/* ---------- inspector form ---------- */
#inspector h3.sub,
.dialog-body h3.sub {
  margin: var(--s-4) 0 var(--s-2);
  font-family: var(--font-mono);
  font-size: var(--fs-mini);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  font-weight: 700;
}
.fld {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  margin-bottom: var(--s-2);
  font-size: var(--fs-small);
  color: var(--muted);
}
.fld input, .fld select, .sel {
  font: inherit;
  font-size: var(--fs-base);
  color: inherit;
  padding: 0.3rem 0.45rem;
  border: var(--rule);
  border-radius: var(--r-sm);
  background: var(--bg-surface);
}
.fld input:focus, .fld select:focus, .sel:focus { outline: none; border-color: var(--accent); }
.fld input[type="number"] { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
/* Library "View product ↗" link — a quiet inline anchor under the model fields. */
.lib-product-link {
  display: inline-block;
  margin-bottom: var(--s-2);
  font-size: var(--fs-small);
  color: var(--accent);
  text-decoration: none;
}
.lib-product-link:hover { text-decoration: underline; }
.fld-check {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: var(--s-2);
  margin-bottom: var(--s-2);
  font-size: var(--fs-small);
  color: inherit;
  cursor: pointer;
}
.fld-check input { width: auto; margin: 0; }
.fld input[type="color"] { padding: 2px; height: 28px; cursor: pointer; }
.fld input[type="range"] { padding: 0; border: 0; }

.btn-row { display: flex; gap: var(--s-2); margin-top: var(--s-2); }
.btn-row.wrap { flex-wrap: wrap; }
.btn {
  font: inherit;
  font-size: var(--fs-small);
  padding: 0.4rem 0.75rem;
  border: var(--rule);
  border-radius: var(--r-sm);
  background: var(--soft);
  color: inherit;
  cursor: pointer;
  transition: background var(--t-fast), border-color var(--t-fast);
}
.btn:hover:not(:disabled) { background: var(--soft-2); border-color: var(--accent); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn.ghost { background: transparent; }
.btn.ghost:hover:not(:disabled) { background: var(--soft); }
.btn.mini { padding: 0.25rem 0.5rem; font-size: var(--fs-mini); }

/* ---------- electrical warnings ---------- */
.warn-line {
  font-size: var(--fs-small);
  color: var(--amber);
  background: color-mix(in srgb, var(--amber) 14%, var(--bg-surface));
  border: 1px solid color-mix(in srgb, var(--amber) 40%, transparent);
  border-radius: var(--r-sm);
  padding: 0.4rem 0.5rem;
  margin: var(--s-1) 0;
}
.ok-line {
  font-size: var(--fs-small);
  color: var(--ok);
  background: color-mix(in srgb, var(--ok) 12%, var(--bg-surface));
  border: 1px solid color-mix(in srgb, var(--ok) 38%, transparent);
  border-radius: var(--r-sm);
  padding: 0.4rem 0.5rem;
  margin: var(--s-1) 0;
}
.out-flag { font-family: var(--font-mono); font-size: var(--fs-mini); flex: 0 0 auto; }
.out-flag.over { color: var(--warn); font-weight: 700; }
.out-flag.ok { color: var(--muted); font-variant-numeric: tabular-nums; }

/* ---------- settings modal ---------- */
dialog#settings-dialog {
  border: var(--rule);
  border-radius: var(--r-md);
  padding: 0;
  width: min(30rem, 92vw);
  background: var(--bg-elevated);
  color: inherit;
  box-shadow: 0 16px 48px color-mix(in srgb, var(--fg-title) 30%, transparent);
}
dialog#settings-dialog::backdrop { background: rgba(0, 0, 0, 0.4); }
.dialog-form { display: flex; flex-direction: column; }
.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--s-3) var(--s-4);
  border-bottom: var(--rule);
}
.dialog-head h2 {
  margin: 0;
  font-size: var(--fs-large);
  font-weight: 700;
}
.dialog-body { padding: var(--s-4); display: grid; gap: var(--s-2); }
.dialog-body .settings-section + .settings-section {
  margin-top: var(--s-2);
  padding-top: var(--s-2);
  border-top: var(--rule);
}
.dialog-body .settings-section h3.sub:first-child { margin-top: 0; }
.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(8.5rem, 1fr));
  gap: var(--s-2) var(--s-4);
}
.dialog-foot {
  display: flex;
  justify-content: flex-end;
  gap: var(--s-2);
  padding: var(--s-3) var(--s-4);
  border-top: var(--rule);
}

/* ---------- library view (fixture editor) ---------- */
/* The library occupies the whole content area (stage + sidebar cols) when
   active. In Layout mode it's hidden and the canvas regions show; in Library
   mode the canvas regions are hidden and this takes over. Driven by
   #app[data-view]. */
#library-view {
  grid-column: 1 / -1;
  grid-row: 2 / 3;
  display: none;
  grid-template-columns: 19rem 1fr;
  min-height: 0;
  overflow: hidden;
  background: var(--bg);
}
#app[data-view="library"] #library-view { display: grid; }
#app[data-view="library"] #stage-wrap,
#app[data-view="library"] #sidebar { display: none; }

#library-list {
  border-right: var(--rule);
  overflow-y: auto;
  padding: 0;
  display: flex;
  flex-direction: column;
}
#library-editor {
  overflow-y: auto;
  padding: var(--s-6) var(--s-6) var(--s-8);
  min-width: 0;
}

/* List header: title + the grouped "New …" action row (sticky at the top). */
.lib-list-head {
  position: sticky;
  top: 0;
  z-index: 1;
  padding: var(--s-3) var(--s-4);
  border-bottom: var(--rule);
  background: var(--bg);
}
.lib-list-title {
  margin: 0 0 var(--s-2);
  font-family: var(--font-mono);
  font-size: var(--fs-mini);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  font-weight: 700;
}
.lib-groups { padding: var(--s-3) var(--s-4) var(--s-5); }

.lib-group { margin-bottom: var(--s-5); }
.lib-group:last-child { margin-bottom: 0; }
/* Section header: the section name + count on the left, its own "+ Add" button
   on the right. Each section thus owns the affordance for adding that kind. */
.lib-group-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-2);
  margin: 0 0 var(--s-2);
  padding-bottom: var(--s-1);
  border-bottom: var(--rule);
}
.lib-group-title {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  font-family: var(--font-mono);
  font-size: var(--fs-mini);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  font-weight: 700;
  margin: 0;
}
.lib-group-add { flex: none; white-space: nowrap; }
.lib-group-count {
  font-variant-numeric: tabular-nums;
  color: var(--fg-muted);
  background: var(--soft-2);
  border-radius: 999px;
  padding: 0.05rem 0.45rem;
  font-size: 0.65rem;
}
.lib-group-empty { margin: 0; }
.lib-cards { display: flex; flex-direction: column; gap: var(--s-1); }
.lib-item {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  width: 100%;
  text-align: left;
  font: inherit;
  padding: var(--s-2) var(--s-3);
  border: var(--rule);
  border-radius: var(--r-md);
  background: var(--bg-surface);
  color: inherit;
  cursor: pointer;
  transition: border-color var(--t-fast), background var(--t-fast);
}
.lib-item:hover { background: var(--soft-2); }
.lib-item.active {
  border-color: var(--accent);
  background: var(--soft-2);
  box-shadow: inset 3px 0 0 var(--accent);
}
.lib-item-name { font-weight: 700; font-size: var(--fs-small); color: var(--fg-title); }
.lib-item-meta { font-family: var(--font-mono); font-size: var(--fs-mini); color: var(--muted); font-variant-numeric: tabular-nums; }

/* Editor pane — a constrained, well-spaced document, CENTERED within the pane.
   The two-pane (list | editor) spans the full content width; the editor column
   is wide, so without centering the form sat crammed against the left with a
   large dead gap on the right. margin-inline:auto balances it (mirrors the
   centered BOM document) so the layout reads evenly instead of one-sided. */
.lib-editor-doc { max-width: 38rem; margin-inline: auto; }
.lib-editor-head { margin: 0 0 var(--s-5); }
.lib-editor-block + .lib-editor-block { margin-top: var(--s-6); }
.lib-editor-block .sub:first-child { margin-top: 0; }
.lib-editor-foot {
  margin-top: var(--s-6);
  padding-top: var(--s-4);
  border-top: var(--rule);
}
#library-editor h2 { margin: 0; font-size: var(--fs-large); font-weight: 700; color: var(--fg-title); }
#library-editor .lib-kind {
  display: block;
  margin-bottom: 0.2rem;
  font-family: var(--font-mono);
  font-size: var(--fs-mini);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  font-weight: 700;
}
#library-editor .lib-empty { color: var(--muted); }
.lib-derived {
  margin: var(--s-3) 0 0;
  padding: var(--s-3);
  border: var(--rule);
  border-radius: var(--r-md);
  background: var(--soft);
  font-family: var(--font-mono);
  font-size: var(--fs-small);
  font-variant-numeric: tabular-nums;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
  gap: 0.3rem var(--s-3);
}
/* Allow long labels to wrap within their cell instead of overflowing into the
   next column (which made "0.40×Draw…" collide). */
.lib-derived span { margin-right: 0; white-space: normal; overflow-wrap: anywhere; }
.lib-fields { max-width: none; }

/* Per-output budget table (controller types) — aligned grid with a header. */
.ctrl-out-table { margin-top: var(--s-2); }
.out-head {
  font-family: var(--font-mono);
  font-size: var(--fs-mini);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  padding-bottom: var(--s-1);
  margin-bottom: var(--s-1);
  border-bottom: var(--rule);
}
.out-head span:last-child { text-align: right; }
.out-id {
  font-family: var(--font-mono);
  font-size: var(--fs-small);
  color: var(--muted);
}

/* ports / channels editor — a tidy aligned table with a header row. */
.port-table { display: flex; flex-direction: column; gap: 0.3rem; }
.port-row {
  display: grid;
  grid-template-columns: 1.6fr 6rem 1fr 2rem;
  gap: var(--s-2);
  align-items: center;
}
.port-head {
  font-family: var(--font-mono);
  font-size: var(--fs-mini);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  padding-bottom: var(--s-1);
  margin-bottom: 0.1rem;
  border-bottom: var(--rule);
}
.port-head span { padding-left: 0.1rem; }
/* Match the warm field styling used by .fld inputs so the channel editor reads
   as part of the same instrument (2px border, 8px-family radius, warm surface)
   instead of falling back to the UA form-control fill. */
.port-row input, .port-row select {
  width: 100%;
  font: inherit;
  font-size: var(--fs-base);
  color: inherit;
  padding: 0.3rem 0.45rem;
  border: var(--rule);
  border-radius: var(--r-sm);
  background: var(--bg-surface);
}
.port-row input:focus, .port-row select:focus { outline: none; border-color: var(--accent); }
.port-row [data-pf="del"] { justify-self: center; }

/* per-type cable color list (settings) */
.cable-color-row {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  margin-bottom: 0.3rem;
  font-size: var(--fs-small);
}
.cable-color-row .cc-name { flex: 1 1 auto; color: var(--muted); }
.cable-color-row input[type="color"] { width: 2.4rem; height: 26px; padding: 2px; cursor: pointer; }

/* ---------- toast ---------- */
#toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%) translateY(8px);
  background: var(--accent);
  color: var(--accent-ink);
  padding: 0.55rem 1rem;
  border-radius: var(--r-md);
  font-size: var(--fs-base);
  box-shadow: 0 6px 22px color-mix(in srgb, var(--fg-title) 25%, transparent);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s, transform 0.18s;
  z-index: 80;
  max-width: 60vw;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast[data-kind="error"] { background: var(--warn); color: #fff; }
#toast[data-kind="warn"]  { background: var(--amber); color: #fff; }
#toast[data-kind="ok"]    { background: var(--ok); color: #fff; }

/* ---------- print: Layout-only ----------
   When the Layout view is active, Cmd/Ctrl+P (and the Export ▸ Print/PDF action)
   should print ONLY the Layout canvas + its on-canvas legend — no top bar, no
   right sidebar (Library/Layers/Inspector), no floating tool rail, no other
   chrome. The <canvas> rasterizes for print; we scale it to fit the page
   (width:auto so its intrinsic buffer aspect is kept, capped to the page box) so
   it's never clipped. The legend is painted INTO the canvas (screen-space,
   bottom-left), so printing the canvas prints the legend with it. The active
   theme is honored — the canvas already painted its themed strips/legend; the
   user can flip to no-stage-bg via the Export "Background" toggle (raster route)
   if a dark backdrop would waste ink. Library/BOM views print as they appear. */
@media print {
  @page { margin: var(--print-margin); }

  /* Reset the app grid so the stage can take the full printable page. */
  #app[data-view="layout"] {
    display: block;
    height: auto;
    width: auto;
  }
  /* Hide every piece of chrome around the Layout. */
  #app[data-view="layout"] #topbar,
  #app[data-view="layout"] #sidebar,
  #app[data-view="layout"] #toolrail,
  #app[data-view="layout"] #library-view,
  #app[data-view="layout"] #bom-view,
  #toast,
  #settings-dialog {
    display: none !important;
  }
  /* Let the stage flow on the page (no fixed grid box / no clipping overflow). */
  #app[data-view="layout"] #stage-wrap {
    position: static;
    overflow: visible;
    background: transparent;
    width: auto;
    height: auto;
  }
  /* Scale the canvas raster to the page width, keeping its aspect (height:auto)
     and never exceeding the printable height, so the whole Layout fits. */
  #app[data-view="layout"] #stage {
    width: 100% !important;
    height: auto !important;
    max-height: 100vh;
    object-fit: contain;
  }
}
