:root {
  --font-ui:
    "IBM Plex Sans", ui-sans-serif, system-ui, -apple-system, sans-serif;
  --font-mono:
    "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  /* light chrome — restrained, single accent, hairline borders */
  --surface: #ffffff;
  --surface-2: #f4f6f8;
  --surface-3: #eceff2;
  --line: #e4e7ec;
  --line-2: #d4d9e0;
  --txt: #1b2230;
  --mut: #69727f;
  --accent: #2563eb;
  --accent-h: #1d4ed8;
  --accent-soft: #e8f0fe;
  /* drawing canvases — hosts may theme these; documents keep paper colors */
  --canvas: #f4f6f8;
  --canvas-grid: #dce4eb;
  --canvas-axis: #b1c0cc;
  --canvas-ink: #253d50;
  --canvas-text: #586779;
  --canvas-face: #d5dce2;
  --canvas-face-selected: #c1d5ef;
  /* dark editor (GitHub Dark Dimmed) */
  --editor-bg: #22272e;
  --editor-line: #2d333b;
  --split: 25%;
  --radius: 8px;
}
* {
  box-sizing: border-box;
}
[hidden] {
  display: none !important;
}
html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  font-family: var(--font-ui);
  background: var(--surface);
  color: var(--txt);
}
#app {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  height: 100%;
}

.icon {
  width: 15px;
  height: 15px;
  flex: none;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- header: app bar + tool bar ---------- */
header {
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
}
.bar-app {
  padding-bottom: 7px;
}
.bar-tools {
  padding-top: 7px;
  padding-bottom: 9px;
  border-top: 1px solid var(--line);
  background: var(--surface-2);
}
.grow {
  flex: 1;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  user-select: none;
  margin-right: 6px;
}
.mark {
  width: 22px;
  height: 22px;
  flex: none;
  stroke: var(--accent);
  fill: none;
  stroke-width: 1.5;
  stroke-linejoin: round;
  stroke-linecap: round;
}
.wm {
  font: 700 15px/1 var(--font-ui);
  letter-spacing: 0.05em;
  color: var(--txt);
}
.wm-sub {
  font-weight: 500;
  letter-spacing: 0.01em;
  color: var(--mut);
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font: 500 13px/1 var(--font-ui);
  color: var(--txt);
  background: var(--surface);
  border: 1px solid var(--line-2);
  border-radius: var(--radius);
  padding: 7px 11px;
  cursor: pointer;
  white-space: nowrap;
  transition:
    background 0.12s,
    border-color 0.12s,
    color 0.12s;
}
.btn:hover {
  background: var(--surface-2);
  border-color: #c2c8d0;
}
.btn:active {
  background: var(--surface-3);
}
.btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}
.btn.primary:hover {
  background: var(--accent-h);
  border-color: var(--accent-h);
}
.btn.active {
  background: var(--accent-soft);
  border-color: #b9d0fb;
  color: var(--accent);
}

.view-switch {
  position: relative;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  padding: 4px;
  border: 1px solid var(--line-2);
  border-radius: 999px;
  background: var(--surface-3);
  flex: none;
}
.view-switch::before {
  content: "";
  position: absolute;
  top: 4px;
  bottom: 4px;
  left: 4px;
  width: calc((100% - 8px) / 3);
  border-radius: 999px;
  background: var(--surface);
  box-shadow: 0 1px 4px #12203026;
  transform: translateX(calc(var(--pane-index, 0) * 100%));
  transition: transform 0.22s ease;
}
.view-switch label {
  position: relative;
  text-align: center;
  border-radius: 999px;
}
.view-switch input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}
.view-switch span {
  display: block;
  padding: 7px 16px;
  font: 500 13px/1 var(--font-ui);
  color: var(--mut);
  pointer-events: none;
}
.view-switch input:checked + span {
  color: var(--accent);
}
.view-switch input:focus-visible + span {
  outline: 2px solid var(--accent);
  border-radius: 999px;
}

/* ---------- dropdown menus ---------- */
.menu {
  position: relative;
  display: inline-flex;
}
.caret {
  transition: transform 0.15s;
  opacity: 0.7;
}
.menu.open .caret {
  transform: rotate(180deg);
}
.menu-pop {
  position: absolute;
  top: calc(100% + 7px);
  left: 0;
  min-width: 212px;
  background: var(--surface);
  border: 1px solid var(--line-2);
  border-radius: 10px;
  padding: 6px;
  display: none;
  z-index: 60;
  box-shadow:
    0 12px 30px rgba(18, 28, 45, 0.14),
    0 2px 6px rgba(18, 28, 45, 0.08);
}
.menu.open .menu-pop {
  display: block;
  animation: pop 0.12s ease;
}
.menu-pop.right {
  left: auto;
  right: 0;
}
@keyframes pop {
  from {
    opacity: 0;
    transform: translateY(-4px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}
.menu-pop button {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  background: transparent;
  border: 0;
  border-radius: 7px;
  padding: 8px 10px;
  color: var(--txt);
  font: 500 13px/1 var(--font-ui);
  cursor: pointer;
  text-align: left;
}
.menu-pop button .lbl {
  flex: 1;
}
.menu-pop button:hover {
  background: var(--accent-soft);
  color: var(--accent);
}
.menu-sep {
  height: 1px;
  background: var(--line);
  margin: 6px 4px;
}
.snap-x {
  flex: none;
  margin-left: 6px;
  padding: 0 5px;
  border-radius: 5px;
  color: var(--mut);
  font-weight: 600;
}
.snap-x:hover {
  background: #fbe3e3;
  color: #c00;
}

/* ---------- section view select ---------- */
.seltag {
  font: 500 11px/1 var(--font-ui);
  color: var(--mut);
  margin-left: 4px;
}
select.btn {
  padding-right: 26px;
  cursor: pointer;
}
select.btn option {
  background: var(--surface);
  color: var(--txt);
}

/* ---------- main split ---------- */
main {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);
  min-height: 0;
}
/* editor + preview live side by side in #workspace */
#workspace {
  display: grid;
  min-width: 0;
  min-height: 0;
  grid-template-columns: var(--split, 1fr) 6px minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);
}
#editorWrap {
  grid-column: 1;
  grid-row: 1;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: var(--editor-bg);
}
/* Inputs form + job-book panel (both swap for the code editor) */
#formPanel {
  grid-column: 1;
  grid-row: 1;
  min-width: 0;
  min-height: 0;
  overflow: auto;
  display: flex;
  background: var(--surface);
  padding: 22px 20px;
  text-align: center;
}
.form-body {
  margin: auto;
  width: 100%;
  max-width: 300px;
}
.form-h {
  font: 600 14px/1.3 var(--font-ui);
  margin: 0 0 4px;
}
.form-sub {
  font: 400 12px/1.5 var(--font-ui);
  color: var(--mut);
  margin: 0 0 20px;
}
.form-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 0 auto 14px;
  max-width: 290px;
}
.form-row label {
  font: 500 12px/1.3 var(--font-ui);
  color: var(--txt);
}
.stepper {
  display: flex;
  align-items: stretch;
  gap: 6px;
}
.step {
  flex: none;
  width: 38px;
  padding: 0;
  justify-content: center;
  font: 500 17px/1 var(--font-ui);
  color: var(--mut);
}
.step:hover {
  color: var(--txt);
}
.form-row input {
  flex: 1;
  min-width: 0;
  font: 500 13px/1 var(--font-ui);
  color: var(--txt);
  background: var(--surface);
  border: 1px solid var(--line-2);
  border-radius: var(--radius);
  padding: 9px 11px;
  width: 100%;
  text-align: center;
  -moz-appearance: textfield;
}
.form-row input::-webkit-outer-spin-button,
.form-row input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
.form-row input:hover {
  border-color: #c2c8d0;
}
.form-row input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  border-color: var(--accent);
}
.form-row input[aria-invalid="true"] {
  border-color: #c62828;
  background: #fdf3f3;
}
.form-err {
  margin: 0;
  font: 500 11px/1.45 var(--font-ui);
  color: #c62828;
}
.form-err:empty {
  display: none;
}
.form-go {
  display: flex;
  width: 100%;
  max-width: 290px;
  margin: 8px auto 0;
  padding: 14px;
  justify-content: center;
  font: 600 14px/1 var(--font-ui);
}
.form-go:disabled {
  background: var(--surface-3);
  border-color: var(--line-2);
  color: var(--mut);
  cursor: default;
}
.form-card {
  display: block;
  margin: 0 auto;
  max-width: 300px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface-2);
}
.form-card .icon {
  width: 18px;
  height: 18px;
  color: var(--mut);
  margin-bottom: 8px;
}
.form-card b {
  display: block;
  font: 600 13px/1.4 var(--font-ui);
  margin-bottom: 4px;
}
.form-card p {
  margin: 0;
  font: 400 12px/1.6 var(--font-ui);
  color: var(--mut);
}
.spin {
  width: 20px;
  height: 20px;
  margin: 0 auto 10px;
  border-radius: 50%;
  border: 2px solid var(--line-2);
  border-top-color: var(--accent);
  animation: spin 0.7s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
#loadingOverlay {
  position: fixed;
  inset: 0;
  width: 100vw;
  max-width: none;
  height: auto;
  max-height: none;
  margin: 0;
  padding: 24px;
  border: 0;
  outline: 0;
  background: var(--surface-2);
  color: var(--txt);
  text-align: center;
  z-index: 100;
}
#loadingOverlay[open] {
  display: grid;
  place-items: center;
}
#loadingOverlay .spin {
  width: 32px;
  height: 32px;
  margin-bottom: 20px;
  border-width: 3px;
}
#loadingMessage {
  font: 600 18px/1.4 var(--font-ui);
  margin: 0 0 8px;
}
@media (prefers-reduced-motion: reduce) {
  .spin {
    animation-duration: 2.4s;
  }
  .view-switch::before,
  #term .chev {
    transition: none;
  }
}
#bookPanel {
  grid-column: 1;
  grid-row: 1;
  min-width: 0;
  min-height: 0;
  overflow: auto;
  background: #fff;
  padding: 20px 26px;
  font:
    12px/1.5 ui-monospace,
    SFMono-Regular,
    Menlo,
    monospace;
  color: #1a1f27;
}
.vsplit {
  position: relative;
  background: var(--line-2);
  cursor: col-resize;
  transition: background 0.12s;
}
.vsplit:hover,
.vsplit.dragging {
  background: var(--accent);
}
.vsplit::after {
  content: "";
  position: absolute;
  inset: 0 -3px;
}

/* dark editor — GitHub Dark Dimmed */
.CodeMirror {
  height: 100%;
  font: 13.5px/1.55 var(--font-mono);
  background: var(--editor-bg);
  color: #adbac7;
}
.CodeMirror-gutters {
  background: var(--editor-bg);
  border-right: 1px solid var(--editor-line);
}
.CodeMirror-linenumber {
  color: #545d68;
}
.CodeMirror-cursor {
  border-left: 1.6px solid #adbac7;
}
.CodeMirror-activeline-background {
  background: #2d333b66;
}
.CodeMirror-selected {
  background: #316dca40 !important;
}
.cm-s-default .cm-comment {
  color: #768390;
  font-style: italic;
}
.cm-s-default .cm-keyword {
  color: #f47067;
}
.cm-s-default .cm-string,
.cm-s-default .cm-string-2 {
  color: #96d0ff;
}
.cm-s-default .cm-number {
  color: #6cb6ff;
}
.cm-s-default .cm-def {
  color: #dcbdfb;
}
.cm-s-default .cm-builtin {
  color: #6cb6ff;
}
.cm-s-default .cm-variable,
.cm-s-default .cm-variable-2,
.cm-s-default .cm-property {
  color: #adbac7;
}
.cm-s-default .cm-operator {
  color: #6cb6ff;
}
.cm-s-default .cm-meta {
  color: #768390;
}

/* ---------- viewer (light) ---------- */
#viewer {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: var(--canvas);
}
#viewport {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}
#viewerTools {
  display: flex;
  align-items: center;
  gap: 2px;
  flex: none;
  overflow-x: auto;
  padding: 6px 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  scrollbar-width: thin;
}
#viewerTools .btn {
  justify-content: center;
  flex: none;
  padding: 6px;
  width: 44px;
  height: 44px;
  border-color: transparent;
  border-radius: 5px;
}
#viewerTools .btn[aria-pressed="true"] {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: #b9d0fb;
}
#viewerTools .btn:disabled {
  opacity: 0.35;
  cursor: default;
}
#viewerTools .icon {
  width: 28px;
  height: 28px;
}
#viewerTools .tool-sep {
  height: 28px;
  width: 1px;
  background: var(--line);
  margin: 0 5px;
  flex: none;
}
#viewCaption {
  position: absolute;
  left: 8px;
  top: 8px;
  padding: 4px;
  border-radius: 4px;
  font: 500 11px/1 var(--font-ui);
  color: var(--canvas-text);
  background: var(--canvas);
  pointer-events: none;
}
#viewer canvas {
  display: block;
}
#measureOverlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  overflow: hidden;
}
#measureOverlay line {
  stroke: var(--accent);
  stroke-width: 2;
}
#measureOverlay circle {
  fill: var(--accent);
  stroke: white;
  stroke-width: 2;
}
#measureOverlay .vertex {
  fill: white;
  stroke: var(--accent);
  stroke-width: 1.5;
}
#measureOverlay .preview {
  fill: var(--accent);
  stroke: white;
  stroke-width: 3;
}
#measureOverlay text {
  font: 500 12px var(--font-ui);
  fill: var(--accent);
  paint-order: stroke;
  stroke: var(--canvas);
  stroke-width: 4;
  stroke-linejoin: round;
}
#measureOverlay .components line {
  stroke: #6f8dc7;
  stroke-width: 1.25;
  stroke-dasharray: 4 4;
}
#measureOverlay .components text {
  fill: var(--mut);
  font-size: 11px;
  text-anchor: middle;
}
#measureOverlay .distance text {
  font-weight: 600;
}
#measurePanel {
  position: absolute;
  right: 12px;
  bottom: 12px;
  width: 280px;
  max-width: calc(100% - 24px);
  max-height: calc(100% - 52px);
  overflow: auto;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--line-2);
  border-radius: var(--radius);
  box-shadow: 0 4px 16px #12203014;
  font: 12px/1.5 var(--font-ui);
}
.measure-head,
.measure-modes {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 10px;
}
#measureMove {
  flex: 1;
  border: 0;
  padding: 0;
  background: none;
  color: inherit;
  text-align: left;
  font: 700 14px var(--font-ui);
  cursor: grab;
  touch-action: none;
  user-select: none;
}
#measureMove:active {
  cursor: grabbing;
}
#measureMove:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}
.measure-head .btn {
  margin-left: auto;
  padding: 3px 8px;
}
.measure-modes label,
#measureSnapLabel {
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
}
#measurePanel input {
  accent-color: var(--accent);
  margin: 0;
}
#measureResult p {
  margin: 8px 0;
  overflow-wrap: anywhere;
}
#measureResult strong {
  font: 600 20px/1.4 var(--font-ui);
  color: var(--accent);
}
#measureResult dl {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 12px;
  margin: 10px 0;
}
#measureResult dd {
  margin: 0;
  text-align: right;
}
#measureClear {
  margin-top: 4px;
}
/* Job-book contents (BOM table on top, part drawing cards below) */
#bookPanel table {
  border-collapse: collapse;
  font:
    11px/1.5 ui-monospace,
    SFMono-Regular,
    Menlo,
    monospace;
  white-space: nowrap;
  margin-bottom: 30px;
}
#bookPanel th,
#bookPanel td {
  border: 1px solid #555;
  padding: 2px 9px;
  text-align: left;
}
#bookPanel th {
  font-weight: 600;
}
#bookPanel td.num {
  text-align: right;
}
#bookPanel .cards {
  display: flex;
  flex-wrap: wrap;
  gap: 36px 46px;
  align-items: flex-end;
}
.pcard {
  text-align: center;
}
.pcard .views {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 14px;
}
.pcard svg {
  display: block;
}
.pcard .psolid {
  fill: none;
  stroke: #1a1f27;
  stroke-width: 1.2;
}
.pcard .phidden {
  stroke: #7a828e;
  stroke-width: 1;
  stroke-dasharray: 3 3;
}
.pcard .pbend {
  stroke: #5a626e;
  stroke-width: 1;
  stroke-dasharray: 7 3 1.5 3;
}
.pcard .ppointer {
  stroke: #8a94a3;
  stroke-width: 0.8;
}
.pcard .pdim {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: var(--dim-font, 12px);
  fill: #1a1f27;
}
.pcard .pdimline {
  stroke: #1a1f27;
  stroke-width: 0.9;
}
.pcard .parrow {
  fill: #1a1f27;
}
.pcard .pdesc {
  margin-top: 7px;
  white-space: nowrap;
}
.pcard .pitem {
  display: inline-block;
  margin-top: 5px;
  border: 1.4px solid #1a1f27;
  border-radius: 999px;
  padding: 1px 12px;
}
.pcard .pqty {
  margin-top: 3px;
}
#status {
  position: absolute;
  right: 12px;
  top: 12px;
  font: 600 11.5px/1 var(--font-ui);
  padding: 5px 11px;
  border-radius: 20px;
  background: color-mix(in srgb, var(--surface) 85%, transparent);
  color: var(--txt);
  border: 1px solid var(--line-2);
  letter-spacing: 0.01em;
  box-shadow: 0 1px 3px rgba(18, 28, 45, 0.08);
}

/* ---------- output terminal ---------- */
:is(#planOutput, #term) {
  flex: none;
  min-width: 0;
  position: relative;
  background: var(--surface);
  border-top: 1px solid var(--line);
  color: var(--mut);
}

:is(#planOutputResize, #termResize) {
  position: absolute;
  top: -4px;
  left: 0;
  right: 0;
  height: 8px;
  cursor: ns-resize;
  touch-action: none;
  z-index: 1;
}

:is(#planOutput, #term) summary {
  cursor: pointer;
  list-style: none;
  min-height: 36px;
  height: auto;
}

:is(#planOutput, #term) summary::-webkit-details-marker {
  display: none;
}

:is(#planOutput, #term):not([open]) .term-head {
  border-bottom: 0;
}

:is(#planOutput, #term):not([open]) .chev {
  transform: rotate(-90deg);
}

:is(#planOutputBody, #log) {
  max-height: min(200px, 28vh);
  overflow: auto;
  padding: 4px 14px;
  font: 12px/1.55 var(--font-mono);
  overflow-wrap: anywhere;
}

.term-head {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 32px;
  flex: 0 0 32px;
  padding: 0 10px;
  border-bottom: 1px solid var(--line);
}
.term-toggle {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  background: transparent;
  border: 0;
  cursor: pointer;
  color: var(--mut);
  font: 600 11px/1 var(--font-ui);
  letter-spacing: 0.09em;
  text-transform: uppercase;
  padding: 6px 7px;
  border-radius: 6px;
}
.term-toggle:hover {
  background: var(--surface-2);
  color: var(--txt);
}
.term-toggle .chev {
  transition: transform 0.15s;
}
#log {
  margin: 0;
  padding-block: 12px;
  white-space: pre-wrap;
}
:is(#planOutput, #term) :focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}
.err {
  color: #b42318;
}
