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

body {
  margin: 0;
  font-family: system-ui, sans-serif;
  background: #fafafa;
  overflow: hidden;
}

#app {
  display: flex;
  height: 100vh;
  width: 100vw;
}

/* Sidebar */
#sidebar-left {
  width: 16rem;
  border-right: 1px solid #e4e4e7;
  background: #ffffff;
  padding: 1rem;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.sidebar-logo {
  display: block;
  width: 100%;
  height: auto;
  margin-bottom: 1rem;
}

#sidebar-left h2 {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  margin: 0 0 1rem;
}

/* Canvas area */
main {
  position: relative;
  flex: 1;
  overflow: auto;
  background: #e4e4e7;
  padding: 2rem;
}

/* Letter page (8.5×11" at 96 PPI) — JS overrides width/height via inline style on init */
#canvas {
  width: 816px;
  height: 1056px;
  position: relative;
  margin: 0 auto;
  background-color: #ffffff;
  box-shadow: 0 25px 50px -12px rgb(0 0 0 / 0.25);
  --grid-line: rgba(55, 65, 81, 0.45);
  touch-action: none;
}

.engineering-grid {
  background-image:
    linear-gradient(to right, var(--grid-line) 1px, transparent 1px),
    linear-gradient(to bottom, var(--grid-line) 1px, transparent 1px);
  background-size: 20px 20px;
}

/* Margin guide overlay */
#margin-guide,
.page-guide {
  position: absolute;
  border: 1px dashed rgba(59, 130, 246, 0.45);
  background-origin: border-box; /* grid tiles start at outer edge = margins.left/top, matching snap positions */
  pointer-events: none;
  z-index: 0;
}

/* Page separator bar rendered between sheet pages */
.page-sep {
  position: absolute;
  left: 0;
  right: 0;
  height: 14px;
  background: #cbd5e1;
  pointer-events: none;
  z-index: 9;
  display: flex;
  align-items: center;
  justify-content: flex-end;
}
.page-sep span {
  font-size: 9px;
  font-family: monospace;
  color: #475569;
  padding: 0 8px;
  letter-spacing: 0.04em;
}

/* Page numbers — always visible */
.page-num {
  position: absolute;
  font-size: 9px;
  font-family: system-ui, sans-serif;
  color: #475569;
  pointer-events: none;
  z-index: 0;
}

/* Draggable blocks */
.block {
  position: absolute;
  background: #ffffff;
  border: 1px solid #e4e4e7;
  box-shadow: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  padding: 1rem;
  cursor: grab;
  min-width: 10rem;
  z-index: 1;
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
}

.block.selected {
  border-color: #3b82f6;
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.35);
}

/* Rubber-band selection rectangle */
#selection-band {
  position: absolute;
  border: 1px solid #3b82f6;
  background: rgba(59, 130, 246, 0.08);
  pointer-events: none;
  display: none;
  z-index: 9998;
}
#selection-band.active {
  display: block;
}

.block h2 {
  font-weight: 700;
  font-size: 0.875rem;
  margin: 0;
  outline: none;
}

/* Editable text content inside blocks */
.block-text {
  outline: none;
  min-height: 1.25rem;
  font-size: 0.875rem;
  line-height: 1.5;
  white-space: pre-wrap;
}

/* Markdown text block */
.text-block {
  min-width: 14rem;
}

.md-view {
  min-height: 1.5rem;
  cursor: text;
  font-size: 0.875rem;
  /* Follows the block's line-spacing setting the same way formula rows do (1 / 1.5 / 2). */
  line-height: calc(1.6 * var(--block-line-space, 1));
  color: inherit;
}
.md-view p {
  margin: 0 0 0.5em;
}
.md-view p:last-child {
  margin-bottom: 0;
}
.md-view h1 {
  font-size: 1.25rem;
  font-weight: 700;
  margin: 0.2em 0 0.4em;
  border-bottom: 1px solid #e4e4e7;
  padding-bottom: 0.2em;
}
.md-view h2 {
  font-size: 1.05rem;
  font-weight: 700;
  margin: 0.2em 0 0.35em;
}
.md-view h3 {
  font-size: 0.925rem;
  font-weight: 700;
  margin: 0.2em 0 0.3em;
}
.md-view h4 {
  font-size: 0.875rem;
  font-weight: 700;
  margin: 0.2em 0 0.25em;
}
.md-view strong {
  font-weight: 700;
}
.md-view em {
  font-style: italic;
}
.md-view code {
  font-family: 'Courier New', monospace;
  font-size: 0.82em;
  background: #f3f4f6;
  border-radius: 3px;
  padding: 0.1em 0.3em;
}
.md-view pre {
  background: #f3f4f6;
  border-radius: 4px;
  padding: 0.6em 0.8em;
  overflow-x: auto;
  margin: 0.4em 0;
}
.md-view pre code {
  background: none;
  padding: 0;
  font-size: 0.8rem;
}
.md-view ul,
.md-view ol {
  margin: 0.25em 0 0.5em 1.4em;
  padding: 0;
}
.md-view li {
  margin-bottom: 0.15em;
}
.md-view hr {
  border: none;
  border-top: 1px solid #e4e4e7;
  margin: 0.6em 0;
}

.md-placeholder {
  color: #a1a1aa;
  font-size: 0.8rem;
  font-style: italic;
}

/* Blockquote */
.md-view blockquote {
  margin: 0.3em 0 0.5em 0;
  padding: 0.25em 0.75em;
  border-left: 3px solid #d1d5db;
  color: #6b7280;
  background: #f9fafb;
  border-radius: 0 3px 3px 0;
}
.md-view blockquote p {
  margin: 0;
}

/* Links and images */
.md-view a {
  color: #2563eb;
  text-decoration: underline;
}
.md-view a:hover {
  color: #1d4ed8;
}
.md-view img.md-img {
  max-width: 100%;
  height: auto;
  border-radius: 3px;
  display: block;
  margin: 0.3em 0;
}

/* Task list items */
.md-view .task-item {
  list-style: none;
  margin-left: -1.2em;
}
.md-view .task-item input[type="checkbox"] {
  margin-right: 0.4em;
  vertical-align: middle;
}

/* A block type this build cannot render. Read-only on purpose: the previous fallback was an
   editable div that overwrote the block's stored content with its own flattened text. */
.block-unsupported {
  padding: 0.5rem 0.25rem 0.5rem 0.6rem;
  border-left: 3px solid #f59e0b;
}
.block-unsupported-label {
  font-size: 0.8rem;
  font-weight: 700;
  color: #b45309;
}
.block-unsupported-note {
  font-size: 0.72rem;
  color: #6b7280;
  margin-top: 0.15rem;
}

/* Math display — set from the sidebar (Math Display), stored per browser.
   --math-font is the face for every rendered expression and result; --math-text-scale scales
   formula text; --math-sup-size sizes every sup/sub.
   Only fonts that ship with the OS are offered: the app is an offline-capable PWA, so a webfont
   would either need bundling or fail to load exactly when someone is working without a network. */
:root {
  --math-font: 'Cambria', 'Georgia', serif;
  --math-text-scale: 1;
  --math-sup-size: 0.65em;
}

/* Force LINING figures on every rendered expression and result.
   Georgia, Palatino and Constantia default to old-style (text) figures, where 3 4 5 7 9 hang
   below the baseline and 6 8 rise above it — handsome in prose, wrong on a calculation sheet,
   and reported as "letters and numbers shifted down" (2026-09-23). This fixes any face that
   carries the `lnum` OpenType feature; classic Georgia does not, which is why the default is now
   Cambria and the font list marks the old-style faces. */
.md-math,
.formula-cell,
.formula-result,
.plot-cell {
  font-variant-numeric: lining-nums;
}

/* Inline math rendered by prettifyExpr inside markdown */
.md-math {
  font-family: var(--math-font);
  font-size: calc(0.9em * var(--math-text-scale));
}
.md-math sup,
.formula-cell sup,
.formula-result sup,
.plot-cell sup,
.md-math sub,
.formula-cell sub,
.formula-result sub,
.plot-cell sub {
  font-size: var(--math-sup-size);
}
.md-math sup {
  vertical-align: super;
  line-height: 0;
}
.md-math sub {
  vertical-align: sub;
  line-height: 0;
}

/* Block / display math ($$...$$) */
.md-math-block {
  margin: 0.6em 0;
  padding: 0.4em 0;
  font-size: 1em;
}
/* Each equation row: left-spacer | centered equation | right number */
.eq-row {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 0.5em;
}
.eq-row > span:first-child {
  display: block;
} /* left spacer */
.eq-num {
  text-align: right;
  font-size: 0.85em;
  color: #6b7280;
  white-space: nowrap;
  padding-right: 0.25em;
}
.eq-ref {
  color: #2563eb;
  text-decoration: none;
  font-size: 0.9em;
}
.eq-ref:hover {
  text-decoration: underline;
}
.eq-ref-missing {
  color: #ef4444;
}

/* Markdown toolbar */
.md-toolbar {
  display: none;
  flex-wrap: wrap;
  gap: 2px;
  padding: 3px 4px;
  background: #f1f5f9;
  border: 1px solid #cbd5e1;
  border-bottom: none;
  border-radius: 3px 3px 0 0;
}
.md-toolbar button {
  padding: 1px 5px;
  font-size: 0.72rem;
  font-family: inherit;
  background: #ffffff;
  border: 1px solid #cbd5e1;
  border-radius: 3px;
  cursor: pointer;
  line-height: 1.5;
  color: #374151;
  min-width: 1.6em;
  text-align: center;
}
.md-toolbar button:hover {
  background: #e0e7ff;
  border-color: #6366f1;
  color: #4338ca;
}
.tb-sep {
  width: 1px;
  background: #cbd5e1;
  margin: 2px 1px;
  align-self: stretch;
}

.md-edit {
  display: none;
  width: 100%;
  min-height: 6rem;
  box-sizing: border-box;
  font-family: 'Courier New', monospace;
  font-size: 0.8rem;
  line-height: 1.5;
  border: 1px solid #3b82f6;
  border-radius: 3px;
  padding: 0.4rem 0.5rem;
  background: #f8faff;
  color: inherit;
  resize: none;
  outline: none;
  overflow: hidden;
}

/* Right-edge resize handle for text blocks */
.text-resize-handle {
  position: absolute;
  right: -22px;
  top: 0;
  bottom: 0;
  width: 44px;
  cursor: ew-resize;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
  touch-action: none;
}
.text-resize-handle::after {
  content: '';
  width: 3px;
  height: 24px;
  background: #d1d5db;
  border-radius: 2px;
  opacity: 0;
  transition: opacity 150ms;
}
.block:hover .text-resize-handle::after,
.block.selected .text-resize-handle::after {
  opacity: 1;
}

/* Right-edge resize handle for formula/summary blocks */
.formula-resize-handle {
  position: absolute;
  right: -22px;
  top: 0;
  bottom: 0;
  width: 44px;
  cursor: ew-resize;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
  touch-action: none;
}
.formula-resize-handle::after {
  content: '';
  width: 3px;
  height: 24px;
  background: #d1d5db;
  border-radius: 2px;
  opacity: 0;
  transition: opacity 150ms;
}
.block:hover .formula-resize-handle::after,
.block.selected .formula-resize-handle::after {
  opacity: 1;
}

/* Placeholder for empty contenteditable elements */
[contenteditable][data-placeholder]:empty::before {
  content: attr(data-placeholder);
  color: #a1a1aa;
  pointer-events: none;
}

/* Math block layout */
.math-title {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #71717a;
  margin-bottom: 0.5rem;
}

.math-row,
.math-result-row {
  display: flex;
  align-items: baseline;
  gap: 0.375rem;
  margin-bottom: 0.25rem;
}

.math-label {
  font-size: 0.8rem;
  font-style: italic;
  width: 3rem;
  flex-shrink: 0;
  color: #52525b;
}

.math-unit {
  font-size: 0.75rem;
  color: #a1a1aa;
}

.math-divider {
  border: none;
  /* The rules that structure a calculation sheet — heading from work, annotations from work —
     are drawn in ink, not in the pale grey used for incidental UI chrome. */
  border-top: 1px solid #18181b;
  margin: 0.5rem 0;
}

.math-result-value {
  font-family: ui-monospace, monospace;
  font-size: 0.875rem;
  font-weight: 600;
}

/* Numeric input inside math blocks */
.block-input {
  width: 7rem;
  border: none;
  border-bottom: 1px solid #e4e4e7;
  outline: none;
  background: transparent;
  font-family: ui-monospace, monospace;
  font-size: 0.875rem;
  color: inherit;
  padding: 0 0.125rem;
  text-align: right;
}

/* Page size controls */
.page-controls {
  margin-bottom: 0.75rem;
}

.page-size-select {
  width: 100%;
  border: 1px solid #e4e4e7;
  border-radius: 0.25rem;
  background: #f3f4f6;
  font-family: inherit;
  font-size: 0.8rem;
  padding: 0.35rem 0.375rem;
  color: inherit;
  cursor: pointer;
  outline: none;
}

.page-size-select:focus {
  border-color: #3b82f6;
}

/* Margin heading + unit toggle row */
.margin-heading-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}

.margin-heading-row h2 {
  margin: 0;
}

.unit-toggle {
  padding: 0.15rem 0.45rem;
  border: 1px solid #e4e4e7;
  border-radius: 0.25rem;
  background: #f3f4f6;
  font-family: ui-monospace, monospace;
  font-size: 0.72rem;
  cursor: pointer;
  color: #52525b;
}

.unit-toggle:hover {
  border-color: #3b82f6;
  color: #2563eb;
}

/* Sidebar toggle button */
.view-toggle {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  width: 100%;
  padding: 0.375rem 0.5rem;
  margin-bottom: 0.75rem;
  background: #f3f4f6;
  border: 1px solid #e4e4e7;
  border-radius: 0.25rem;
  cursor: pointer;
  font-size: 0.8rem;
  font-family: inherit;
  color: #52525b;
  text-align: left;
}

.view-toggle.active {
  background: #eff6ff;
  border-color: #3b82f6;
  color: #2563eb;
}

.grid-density {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
  font-size: 0.75rem;
  color: #71717a;
}

.grid-density input[type="range"] {
  flex: 1;
  accent-color: #3b82f6;
  cursor: pointer;
}

/* Margin inputs grid */
.margin-inputs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.375rem;
  margin-bottom: 1rem;
}

.margin-field {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  font-size: 0.75rem;
  color: #71717a;
}

.margin-field input[type="number"] {
  width: 100%;
  border: 1px solid #e4e4e7;
  border-radius: 0.25rem;
  background: #f3f4f6;
  font-family: ui-monospace, monospace;
  font-size: 0.8rem;
  padding: 0.2rem 0.375rem;
  outline: none;
  color: inherit;
}

.margin-field input[type="number"]:focus {
  border-color: #3b82f6;
}

/* Cursor position readout */
#cursor-coords {
  font-family: ui-monospace, monospace;
  font-size: 0.8rem;
  color: #f97316;
  margin-bottom: 1rem;
}

/* Placement mode */
#canvas.placement-mode,
#canvas.placement-mode .block {
  cursor: crosshair;
}

/* Grid cursor: crosshair marker at current grid intersection */
#grid-cursor {
  position: absolute;
  width: 0;
  height: 0;
  pointer-events: none;
  z-index: 9999;
  top: 0;
  left: 0;
  overflow: visible;
}

/* Right-click context menu */
#ctx-menu {
  position: fixed;
  background: #ffffff;
  border: 1px solid #e4e4e7;
  border-radius: 0.25rem;
  box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);
  padding: 0.25rem 0;
  z-index: 10000;
  display: none;
  min-width: 10rem;
}

#ctx-menu button {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0.375rem 0.75rem;
  border: none;
  background: none;
  font-size: 0.8rem;
  font-family: inherit;
  cursor: pointer;
  color: #ef4444;
}

#ctx-menu button:hover {
  background: #fee2e2;
}

/* Formula section in context menu */
.ctx-section-header {
  padding: 0.2rem 0.75rem 0.1rem;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #9ca3af;
  pointer-events: none;
}

.ctx-sep {
  margin: 0.25rem 0;
  border: none;
  border-top: 1px solid #e4e4e7;
}

/* Non-destructive context menu buttons (add actions) */
#ctx-menu button.ctx-neutral-btn {
  color: #374151;
}
#ctx-menu button.ctx-neutral-btn:hover {
  background: #f0fdf4;
  color: #059669;
}
#ctx-menu button.ctx-neutral-btn:disabled {
  color: #d1d5db;
  cursor: default;
}
#ctx-menu button.ctx-neutral-btn:disabled:hover {
  background: none;
}

/* A block whose geometry belongs to a purchased template: no resize handles, and no grab
   cursor, because neither does anything. The drag handler and the label refuse independently —
   this rule removes the affordance, it is not what enforces the lock. */
/* Matches all six: formula/figure/text -resize-handle, plot -right-/-bottom-handle,
   figure -bottom-handle. A suffix selector missed half of them. */
.block--fixed [class*='-handle'] {
  display: none !important;
}
.block--fixed {
  cursor: default;
}

/* A labelled dropdown in the context menu — used where there are too many choices for the
   side-by-side button rows below (the two dozen unit kinds an input row can require). */
.ctx-field {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.15rem 0.75rem 0.3rem;
  font-size: 0.72rem;
  color: #6b7280;
  white-space: nowrap;
}
.ctx-select {
  flex: 1;
  min-width: 0;
  padding: 0.15rem 0.2rem;
  font: inherit;
  font-size: 0.72rem;
  color: #374151;
  background: #fff;
  border: 1px solid #d4d4d8;
  border-radius: 3px;
}
.ctx-select:focus {
  outline: none;
  border-color: #3b82f6;
}

/* Line spacing 1.0 / 1.5 / 2.0 — three side-by-side choices, the current one marked */
.ctx-spacing-row {
  display: flex;
  gap: 0.25rem;
  padding: 0.1rem 0.75rem 0.25rem;
}
#ctx-menu button.ctx-spacing-btn {
  flex: 1;
  padding: 0.2rem 0;
  text-align: center;
  border: 1px solid #e4e4e7;
  border-radius: 0.25rem;
  font-variant-numeric: tabular-nums;
}
#ctx-menu button.ctx-spacing-btn.active {
  border-color: #059669;
  background: #f0fdf4;
  color: #059669;
  font-weight: 600;
}

/* ── Formula blocks ─────────────────────────────────────────────────────── */
.formula-block {
  min-width: 220px;
  font-size: 0.78rem;
}

/* Summary Block — identical to formula block but with a green left accent */
.summary-block {
  border-left: 3px solid #22c55e !important;
}

/* Sidebar badge marking section-only modules */
.module-section-badge {
  margin-left: auto;
  font-size: 0.65rem;
  font-weight: 700;
  color: #64748b;
  background: #e2e8f0;
  border-radius: 3px;
  padding: 0 4px;
  line-height: 1.4;
}

.formula-label {
  font-weight: 600;
  font-size: 0.78rem;
  padding: 2px 2px 3px;
  outline: none;
  border-bottom: 1px solid transparent;
  min-height: 1em;
}
.formula-label:focus {
  border-bottom-color: #3b82f6;
}
.formula-label:empty::before {
  content: attr(data-placeholder);
  color: #9ca3af;
}

.formula-rows {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin: 4px 0 2px;
  position: relative;
}

/* Group wrapper for if/for...end blocks — desc and ref span the full block height */
.formula-block-group {
  display: flex;
  flex-direction: row;
}

.formula-block-inner {
  display: flex;
  flex-direction: column;
  flex: 1;
  gap: 2px;
}

/* Remove depth-based padding inside a group (structural nesting handles indentation) */
.formula-block-inner .formula-row {
  padding-left: 0;
}

.formula-row {
  display: flex;
  align-items: flex-start;
  gap: 0;
  padding-left: calc(var(--depth, 0) * 16px);
  /* Line spacing (1 / 1.5 / 2, Word-style). Each row carries its OWN spacing in --row-space and
     nothing else feeds it — the block-level control works by stamping its value into every row,
     so there is exactly one source of truth and no block-vs-row cascade to reason about.
     Not a flex `gap`: a gap is uniform and cannot vary row by row.
     Split HALF ABOVE and HALF BELOW so the extra space belongs to this row, the way a taller line
     does in Word. As a bottom margin it read as a gap donated to the row underneath, which made a
     row's setting look like it applied to its neighbour (both reported 2026-09-23).
     Scales with --math-text-scale so spacing stays proportional at every text size. */
  padding-block: calc((var(--row-space, 1) - 1) * 0.4rem * var(--math-text-scale));
}

/* Control-flow rows align with the formula column when per-row desc column is present */
.formula-rows.has-any-row-desc .formula-row--control {
  padding-left: calc(186px + var(--depth, 0) * 16px);
}
/* Inside a group, per-row desc is always suppressed — no offset needed for control rows */
.formula-rows.has-any-row-desc .formula-block-inner .formula-row--control {
  padding-left: 0;
}

/* Description column */
.formula-desc-wrap {
  display: none;
  flex-shrink: 0;
  width: 180px;
  padding-right: 6px;
  margin-right: 6px;
  border-right: 1px solid #18181b; /* ink — see .math-divider */
  align-self: stretch;
  flex-direction: column;
}

/* Group-level spanning desc/ref: blue border; shown when any row (incl. if/for) has annotation */
.formula-rows.has-any-desc .formula-block-group > .formula-desc-wrap {
  display: flex;
  border-right: 2px solid #93c5fd;
}
.formula-rows.has-any-ref .formula-block-group > .formula-ref-wrap {
  display: flex;
  align-items: flex-start;
  border-left: 2px solid #93c5fd;
}
/* Show desc column only when a row explicitly has a desc (> direct child — excludes group wrap) */
.formula-row.has-desc > .formula-desc-wrap {
  display: flex;
}
/* When any regular row has a desc, show all row-level desc-wraps for alignment */
.formula-rows.has-any-row-desc .formula-row > .formula-desc-wrap {
  display: flex;
}
/* Per-row desc-wraps are always suppressed inside groups — the group spanning wrap is the only annotation */
.formula-block-group .formula-row > .formula-desc-wrap {
  display: none !important;
}

.formula-desc-cell {
  outline: none;
  cursor: text;
  font-size: 0.8rem;
  line-height: 1.5;
  color: #111827;
  padding: 1px 2px;
  border-bottom: 1px solid transparent;
  white-space: normal;
  word-break: break-word;
  font-family: system-ui, sans-serif;
  flex: 1;
}
.formula-desc-cell:focus {
  font-family: 'Courier New', monospace;
  font-size: 0.74rem;
  color: inherit;
  border-bottom: 1px solid #3b82f6;
}
.formula-desc-cell:empty::before {
  content: attr(data-placeholder);
  color: #d1d5db;
  font-style: italic;
  pointer-events: none;
}
.formula-desc-cell strong {
  font-weight: 700;
}
.formula-desc-cell em {
  font-style: italic;
}
.formula-desc-cell code {
  font-family: 'Courier New', monospace;
  font-size: 0.85em;
  background: #f3f4f6;
  border-radius: 2px;
  padding: 0 2px;
}
.formula-desc-cell .md-math {
  font-family: var(--math-font);
  font-size: 0.85em;
}

/* Reference column (right of formula) */
.formula-ref-wrap {
  display: none;
  flex-shrink: 0;
  width: 150px;
  padding-left: 6px;
  margin-left: 6px;
  border-left: 1px solid #18181b; /* ink — see .math-divider */
  align-self: stretch;
}
.formula-row.has-ref > .formula-ref-wrap {
  display: flex;
  align-items: flex-start;
}
/* When any regular row has a ref, show all row-level ref-wraps for alignment */
.formula-rows.has-any-row-ref .formula-row > .formula-ref-wrap {
  display: flex;
  align-items: flex-start;
}
/* Per-row ref-wraps are always suppressed inside groups — the group spanning wrap is the only annotation */
.formula-block-group .formula-row > .formula-ref-wrap {
  display: none !important;
}

.formula-ref-cell {
  outline: none;
  cursor: text;
  font-size: 0.8rem;
  line-height: 1.5;
  color: #111827;
  padding: 1px 2px;
  border-bottom: 1px solid transparent;
  white-space: normal;
  word-break: break-word;
  font-family: 'Courier New', monospace;
  flex: 1;
  align-self: stretch;
}
.formula-ref-cell:focus {
  color: inherit;
  border-bottom: 1px solid #3b82f6;
}
.formula-ref-cell:empty::before {
  content: attr(data-placeholder);
  color: #d1d5db;
  font-style: italic;
  pointer-events: none;
}

/* Right side: formula + result */
.formula-expr-side {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
}

/* Single cell: shows rendered math when unfocused, raw text when focused */
.formula-cell {
  flex: 1;
  min-width: 0;
  outline: none;
  cursor: text;
  padding: 1px 2px;
  border-bottom: 1px solid transparent;
  line-height: 1.5;
  white-space: normal;
  overflow-wrap: break-word;
  overflow: visible;
  /* Unfocused: serif for rendered math */
  font-family: var(--math-font);
  font-size: calc(0.8rem * var(--math-text-scale));
}
.formula-cell:focus {
  /* Focused: monospace for editing raw text */
  font-family: 'Courier New', monospace;
  font-size: 0.74rem;
  border-bottom-color: #3b82f6;
}
.formula-cell:hover:not(:focus) {
  border-bottom-color: #d4d4d8;
}
.formula-cell:empty::before,
.formula-cell[data-placeholder]:focus:empty::before {
  content: attr(data-placeholder);
  color: #c4c4c7;
  font-family: 'Courier New', monospace;
  font-size: 0.74rem;
  pointer-events: none;
}
.formula-cell sup {
  vertical-align: super;
  line-height: 0;
}
.formula-cell sub {
  vertical-align: sub;
  line-height: 0;
}

/* ---------------------------------------------------------------------------
   Author-declared input rows

   The name is the template's, the value is the user's. They read as one line but are two
   columns: the name column shrinks to its content and sits hard against the value so
   `P = 10 kip` still scans as a single equation rather than a form.
   --------------------------------------------------------------------------- */
/* Any cell that cannot be typed into: the locked NAME of an input row, and every cell of a
   row locked outright. No hover underline and no text cursor — nothing here responds to a
   click, so it must not advertise that it might. */
.formula-cell--locked,
.formula-cell--readonly {
  cursor: default;
  border-bottom-color: transparent;
}
.formula-cell--locked:hover,
.formula-cell--readonly:hover {
  border-bottom-color: transparent;
}
/* An empty locked cell has no placeholder to offer — there is nothing the user can put there. */
.formula-cell--readonly:empty::before {
  content: '';
}

/* The name half of an input row shrinks to its content so `P = 10 kip` reads as one equation. */
.formula-row--input .formula-cell--locked {
  flex: 0 0 auto;
  padding-right: 0;
}
.formula-row--input .formula-cell--locked::after {
  content: ' =';
  color: #71717a;
}
.formula-input-value {
  flex: 1;
  min-width: 2.5rem;
  outline: none;
  cursor: text;
  padding: 1px 4px;
  margin-left: 4px;
  line-height: 1.5;
  white-space: normal;
  overflow-wrap: break-word;
  font-family: var(--math-font);
  font-size: calc(0.8rem * var(--math-text-scale));
  font-variant-numeric: lining-nums;
  /* Always editable, and it looks it — a faint well marks the one part of a locked
     template the reader is meant to fill in. */
  background: #f4f4f5;
  border: 1px solid #e4e4e7;
  border-radius: 3px;
}
.formula-input-value:focus {
  font-family: 'Courier New', monospace;
  font-size: 0.74rem;
  background: #fff;
  border-color: #3b82f6;
}
.formula-input-value:hover:not(:focus) {
  border-color: #d4d4d8;
}
.formula-input-value:empty::before {
  content: attr(data-placeholder);
  color: #a1a1aa;
  font-family: 'Courier New', monospace;
  font-size: 0.74rem;
  pointer-events: none;
}
.formula-input-value--bad {
  border-color: #dc2626;
  background: #fef2f2;
}

/* On a printed sheet an input is just a number: the well and its border are screen
   affordances and would read as a form field on a stamped calculation. */
@media print {
  .formula-input-value {
    background: none;
    border-color: transparent;
    padding-left: 0;
  }
}

/* Stacked fraction: numerator over denominator with a rule line */
.frac {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  vertical-align: middle;
  font-size: 0.85em;
  line-height: 1.2;
  margin: 0 2px;
}
.frac > span:first-child {
  border-bottom: 1px solid currentColor;
  padding: 0 3px 1px;
  text-align: center;
}
.frac > span:last-child {
  padding: 1px 3px 0;
  text-align: center;
}
/* Matrix — bracketed grid, used for typed literals (renderMatrixLiteral) and results
   (matrixResultHtml). Column count is set inline. Brackets are the left/right borders with short
   top/bottom serifs drawn by the corner gradients. */
.mat {
  display: inline-grid;
  column-gap: 0.9em;
  row-gap: 0.1em;
  vertical-align: middle;
  padding: 0.1em 0.45em;
  margin: 0 2px;
  justify-items: end;
  border-left: 1px solid currentColor;
  border-right: 1px solid currentColor;
  background:
    linear-gradient(currentColor, currentColor) top left / 0.3em 1px no-repeat,
    linear-gradient(currentColor, currentColor) bottom left / 0.3em 1px no-repeat,
    linear-gradient(currentColor, currentColor) top right / 0.3em 1px no-repeat,
    linear-gradient(currentColor, currentColor) bottom right / 0.3em 1px no-repeat;
  line-height: 1.3;
  white-space: nowrap;
}
/* Σ / Π / ∫ with stacked limits (renderBigOp in markdown.ts) */
.bigop {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  vertical-align: middle;
  line-height: 1;
  margin: 0 3px 0 1px;
}
.bigop-sym {
  font-size: 1.55em;
  line-height: 1;
}
.bigop-lim {
  /* The limits on Σ / Π / ∫ are read like subscripts, so they follow the same setting rather than a
     fixed size of their own (they were stuck at 0.62em until 2.3.16). */
  font-size: var(--math-sup-size);
  line-height: 1.1;
  white-space: nowrap;
}
.fp-eq {
  font-style: normal;
}
.fp-unit {
  font-style: italic;
  font-size: 0.82em;
  color: #6b7280;
  margin-left: 0.2em;
}

.formula-result {
  /* A result is part of the calculation, so it is set in the same serif face, size and colour as
     the expression that produced it. Only a status — err, OK, NG — earns a colour of its own. */
  font-family: var(--math-font);
  font-size: calc(0.8rem * var(--math-text-scale));
  color: inherit;
  white-space: nowrap;
  min-width: 56px;
  text-align: right;
  /* Tabular as well as lining: this is a right-aligned column of numbers, so equal-width digits
     make successive results line up on the decimal instead of drifting. */
  font-variant-numeric: lining-nums tabular-nums;
}
.formula-sep {
  font-family: 'Courier New', monospace;
  font-size: 0.74rem;
  color: #6b7280;
  flex-shrink: 0;
  -webkit-user-select: none;
  user-select: none;
}
.result-unit {
  font-style: italic;
  font-size: 0.85em;
  opacity: 0.75;
  margin-left: 0.15em;
}
.formula-fn {
  color: #7c3aed;
  font-style: italic;
  font-size: 0.7rem;
}
.formula-error {
  color: #dc2626;
  cursor: help;
  font-weight: 600;
}
/* A comparison is a check — it reads OK or NG, the way it would on a hand calculation. */
.formula-check-ok {
  color: #047857;
  font-weight: 700;
}
.formula-check-ng {
  color: #dc2626;
  font-weight: 700;
}
.formula-inactive {
  color: #d1d5db;
  font-style: italic;
}
.formula-cond-true {
  color: #059669;
  font-size: 0.72rem;
}
.formula-cond-false {
  color: #9ca3af;
  font-size: 0.72rem;
}
.formula-loop-count {
  color: #3b82f6;
  font-size: 0.72rem;
}

/* ── Control-flow rows ──────────────────────────────────────────────────── */
.formula-row--control {
  align-items: center;
  background: #f9fafb;
  border-radius: 3px;
  margin-top: 1px;
  min-height: calc(1.2rem + 3px);
}
.formula-row--inactive > .formula-expr-side > .formula-cell {
  opacity: 0.4;
}

/* Keyword badge (if / elseif / else / end / for) */
.formula-keyword {
  display: inline-block;
  font-family: 'Courier New', monospace;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 3px;
  margin-right: 4px;
  white-space: nowrap;
  flex-shrink: 0;
  -webkit-user-select: none;
  user-select: none;
}
.formula-keyword--if,
.formula-keyword--elseif {
  background: #dbeafe;
  color: #1d4ed8;
}
.formula-keyword--else {
  background: #fef3c7;
  color: #92400e;
}
.formula-keyword--end {
  background: #f3f4f6;
  color: #6b7280;
}
.formula-keyword--for {
  background: #d1fae5;
  color: #065f46;
}

/* ── Plot block ─────────────────────────────────────────────────────────── */
.plot-block {
  min-width: 300px;
  padding: 0.5rem 0.5rem 0.4rem;
}

.plot-controls {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  margin-bottom: 0.4rem;
}

.plot-row {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  flex-wrap: wrap;
}

.plot-label {
  font-size: 0.7rem;
  color: #6b7280;
  white-space: nowrap;
  -webkit-user-select: none;
  user-select: none;
}

.plot-input {
  font-family: 'Courier New', monospace;
  font-size: 0.75rem;
  border: 1px solid #d1d5db;
  border-radius: 3px;
  padding: 2px 5px;
  background: #f9fafb;
  color: inherit;
  min-width: 0;
}
.plot-input:focus {
  outline: 1px solid #3b82f6;
  border-color: #3b82f6;
}

.plot-expr {
  flex: 1;
  min-width: 8rem;
}
.plot-xvar {
  width: 2.2rem;
  text-align: center;
}
.plot-range {
  min-width: 3rem;
  max-width: 5.5rem;
  text-align: center;
}
.plot-fill-label {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.7rem;
  color: #6b7280;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
  margin-left: 0.25rem;
  white-space: nowrap;
}
.plot-fill-check {
  cursor: pointer;
}

/* contentEditable math-preview cells inside plot blocks */
.plot-cell {
  outline: none;
  cursor: text;
  line-height: 1.5;
  font-family: var(--math-font);
  font-size: calc(0.8rem * var(--math-text-scale));
}
.plot-cell:focus {
  font-family: 'Courier New', monospace;
  font-size: 0.75rem;
}
.plot-cell:empty::before,
.plot-cell[data-placeholder]:focus:empty::before {
  content: attr(data-placeholder);
  color: #c4c4c7;
  font-family: 'Courier New', monospace;
  font-size: 0.74rem;
  pointer-events: none;
}
.plot-cell sup {
  vertical-align: super;
  line-height: 0;
}
.plot-cell sub {
  vertical-align: sub;
  line-height: 0;
}

.plot-svg-wrap {
  line-height: 0;
}
.plot-svg-wrap svg {
  max-width: 100%;
  height: auto;
  display: block;
}

.plot-err {
  font-size: 0.7rem;
  color: #ef4444;
  margin-top: 0.2rem;
  min-height: 1em;
}

/* Right-edge resize handle for plot blocks */
.plot-right-handle {
  position: absolute;
  right: -22px;
  top: 0;
  bottom: 0;
  width: 44px;
  cursor: ew-resize;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
  touch-action: none;
}
.plot-right-handle::after {
  content: '';
  width: 3px;
  height: 24px;
  background: #d1d5db;
  border-radius: 2px;
  opacity: 0;
  transition: opacity 150ms;
}
.block:hover .plot-right-handle::after,
.block.selected .plot-right-handle::after {
  opacity: 1;
}

/* Bottom resize handle for plot blocks */
.plot-bottom-handle {
  position: absolute;
  bottom: -22px;
  left: 0;
  right: 0;
  height: 44px;
  cursor: ns-resize;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
  touch-action: none;
}
.plot-bottom-handle::after {
  content: '';
  height: 3px;
  width: 32px;
  background: #d1d5db;
  border-radius: 2px;
  opacity: 0;
  transition: opacity 150ms;
}
.block:hover .plot-bottom-handle::after,
.block.selected .plot-bottom-handle::after {
  opacity: 1;
}

/* Plot right-click marker popup */
.plot-ctx-popup {
  position: fixed;
  z-index: 9999;
  background: #ffffff;
  border: 1px solid #e5e7eb;
  border-radius: 6px;
  padding: 6px 8px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
  font-size: 0.78rem;
  /* Column + gap so the x row, y row and Clear All stack evenly instead of
     touching — the popup grew from one row to three. */
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.plot-ctx-row {
  display: flex;
  align-items: center;
  gap: 5px;
}
/* Out-of-bounds / invalid-entry message. Wraps rather than widening the popup. */
.plot-ctx-msg {
  color: #b91c1c;
  font-size: 0.72rem;
  line-height: 1.35;
  max-width: 220px;
}
.plot-ctx-label {
  color: #6b7280;
  flex-shrink: 0;
}
.plot-ctx-input {
  width: 6rem;
  font-size: 0.78rem;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  padding: 2px 5px;
  outline: none;
}
.plot-ctx-input:focus {
  border-color: #2563eb;
}
.plot-ctx-btn {
  font-size: 0.72rem;
  padding: 2px 7px;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  background: #f9fafb;
  cursor: pointer;
  white-space: nowrap;
}
.plot-ctx-btn:hover {
  background: #f3f4f6;
}
.plot-ctx-btn-primary {
  background: #2563eb;
  border-color: #2563eb;
  color: #fff;
}
.plot-ctx-btn-primary:hover {
  background: #1d4ed8;
}

.module-item.custom {
  background: #eff6ff;
}
.module-item.custom:hover {
  background: #dbeafe;
}

/* Custom Tools heading with inline Import button */
h2.custom-tools-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 1rem;
}

.import-tools-btn {
  font-size: 0.6rem;
  font-family: inherit;
  padding: 2px 6px;
  border: 1px solid #d1d5db;
  border-radius: 3px;
  background: none;
  cursor: pointer;
  color: #6b7280;
  white-space: nowrap;
}
.import-tools-btn:hover {
  border-color: #3b82f6;
  color: #2563eb;
  background: #eff6ff;
}

/* Import tools modal */
.import-modal-overlay {
  position: fixed;
  inset: 0;
  background: rgb(0 0 0 / 0.45);
  z-index: 20000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.import-modal {
  background: #ffffff;
  border-radius: 0.5rem;
  box-shadow: 0 20px 40px rgb(0 0 0 / 0.25);
  padding: 1.25rem 1.5rem;
  min-width: 260px;
  max-width: 360px;
  width: 90vw;
}

.import-modal h3 {
  margin: 0 0 0.25rem;
  font-size: 0.95rem;
  font-weight: 700;
}

.import-modal-sub {
  margin: 0 0 0.75rem;
  font-size: 0.75rem;
  color: #6b7280;
}

.import-modal-list {
  max-height: 240px;
  overflow-y: auto;
  border: 1px solid #e4e4e7;
  border-radius: 0.25rem;
  padding: 0.25rem 0;
  margin-bottom: 0.75rem;
}

.import-tool-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.3rem 0.6rem;
  cursor: pointer;
  font-size: 0.8rem;
}
.import-tool-row:hover {
  background: #f4f4f5;
}
.import-tool-row input[type="checkbox"] {
  flex-shrink: 0;
  cursor: pointer;
}
.import-tool-row input[type="checkbox"]:disabled {
  cursor: default;
}

.import-tool-exists {
  font-size: 0.68rem;
  color: #9ca3af;
  margin-left: auto;
  white-space: nowrap;
}

.import-modal-btns {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
}

.import-modal-btns button {
  font-family: inherit;
  font-size: 0.78rem;
  padding: 0.3rem 0.75rem;
  border-radius: 0.25rem;
  border: 1px solid #d1d5db;
  background: #f9fafb;
  cursor: pointer;
}
.import-modal-btns button:hover {
  background: #f3f4f6;
}

.import-confirm-btn {
  background: #2563eb !important;
  border-color: #1d4ed8 !important;
  color: #ffffff !important;
}
.import-confirm-btn:hover {
  background: #1d4ed8 !important;
}

/* ctx-menu: Save as Tool is not red */
#ctx-menu .ctx-save-btn {
  color: #b45309;
}
#ctx-menu .ctx-save-btn:hover {
  background: #fffbeb;
  color: #92400e;
}

.mod-delete {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 0.75rem;
  color: #9ca3af;
  padding: 0 2px;
  line-height: 1;
  margin-left: auto;
  flex-shrink: 0;
}
.mod-delete:hover {
  color: #ef4444;
}

/* Sidebar module items */
.module-item {
  display: flex;
  align-items: center;
  padding: 0.5rem;
  margin-bottom: 0.5rem;
  background: #f3f4f6;
  border-radius: 0.25rem;
  cursor: grab;
  transition: transform 150ms cubic-bezier(0.4, 0, 0.2, 1);
}

.module-item:active {
  transform: scale(0.95);
}

.module-item.placing {
  background: #eff6ff;
  outline: 1px solid #3b82f6;
  color: #2563eb;
}

.module-item span + span {
  margin-left: 0.5rem;
  font-size: 0.875rem;
}

/* Print */
@page {
  margin: 0;
  size: letter portrait;
}

@media print {
  body {
    overflow: visible;
    background: white;
  }

  #sidebar-left {
    display: none;
  }

  main {
    overflow: visible;
    padding: 0;
    background: white;
  }

  #app {
    display: block;
    height: auto;
    width: auto;
  }

  main {
    position: static;
  }

  #canvas {
    /* JS beforeprint handler converts inline px to in; this is a fallback */
    margin: 0 !important;
    padding: 0 !important;
    box-shadow: none !important;
    background-color: white !important;
    overflow: hidden;
  }

  #margin-guide,
  .page-guide {
    border: 1px solid #64748b;
    z-index: 99999;
    pointer-events: none;
    background-color: transparent !important;
    background-image: none !important;
  }

  .block {
    box-shadow: none;
    border-color: transparent;
    background: white !important;
  }

  .block.selected {
    box-shadow: none;
    border-color: transparent;
  }
  #selection-band {
    display: none !important;
  }
  #grid-cursor {
    display: none !important;
  }
  .page-sep {
    display: none !important;
  }
  .page-num {
    display: block !important;
  }

  /* Hide empty placeholder text so it doesn't appear in print/PDF */
  .formula-desc-cell:empty::before,
  .formula-ref-cell:empty::before,
  [contenteditable][data-placeholder]:empty::before {
    content: none !important;
  }
}

/* ── Figure block ─────────────────────────────────────────────────────────── */
.figure-block {
  padding: 0;
  display: flex;
  flex-direction: column;
  min-width: 80px;
  min-height: 60px;
  box-sizing: border-box;
  position: relative;
}

.figure-label {
  font-size: 0.7rem;
  font-weight: 600;
  color: #6b7280;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 3px 6px 2px;
  border-bottom: 1px solid #e4e4e7;
  flex-shrink: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.figure-img-wrap {
  flex: 1 1 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: #f9fafb;
  cursor: default;
}

.figure-img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
}

.figure-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  color: #9ca3af;
  font-size: 0.75rem;
  text-align: center;
  cursor: pointer;
  padding: 0.5rem;
  box-sizing: border-box;
  -webkit-user-select: none;
  user-select: none;
}
.figure-placeholder:hover {
  color: #6b7280;
  background: #f3f4f6;
}

.figure-caption {
  font-size: 0.75rem;
  color: #374151;
  text-align: center;
  padding: 3px 6px 4px;
  border-top: 1px solid #e4e4e7;
  outline: none;
  flex-shrink: 0;
  min-height: 1.2rem;
  white-space: pre-wrap;
}
.figure-caption:empty::before {
  content: attr(data-placeholder);
  color: #d1d5db;
  pointer-events: none;
}

/* Right-edge resize handle for figure blocks */
.figure-resize-handle {
  position: absolute;
  right: -22px;
  top: 0;
  bottom: 0;
  width: 44px;
  cursor: ew-resize;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 3;
  touch-action: none;
}
.figure-resize-handle::after {
  content: '';
  width: 3px;
  height: 24px;
  background: #d1d5db;
  border-radius: 2px;
  opacity: 0;
  transition: opacity 150ms;
}
.block:hover .figure-resize-handle::after,
.block.selected .figure-resize-handle::after {
  opacity: 1;
}

/* Bottom resize handle for figure blocks */
.figure-bottom-handle {
  position: absolute;
  bottom: -22px;
  left: 0;
  right: 0;
  height: 44px;
  cursor: ns-resize;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 3;
  touch-action: none;
}
.figure-bottom-handle::after {
  content: '';
  height: 3px;
  width: 32px;
  background: #d1d5db;
  border-radius: 2px;
  opacity: 0;
  transition: opacity 150ms;
}
.block:hover .figure-bottom-handle::after,
.block.selected .figure-bottom-handle::after {
  opacity: 1;
}

@media print {
  .figure-resize-handle {
    display: none !important;
  }
  .figure-bottom-handle {
    display: none !important;
  }
  .formula-resize-handle {
    display: none !important;
  }
  .plot-right-handle {
    display: none !important;
  }
  .plot-bottom-handle {
    display: none !important;
  }
  .figure-placeholder {
    display: none !important;
  }
}

/* ── Section block ─────────────────────────────────────────────────────────── */
.section-block {
  /* Translucent so the page engineering grid shows through */
  background: rgba(241, 245, 249, 0.12) !important;
  border: 1px solid #cbd5e1;
  border-left: 4px solid var(--section-color, #3b82f6);
  box-shadow: none;
  box-sizing: border-box;
  min-width: 0;
  min-height: 80px;
  /* Section sits behind its absolutely-positioned child blocks */
  z-index: 0;
  padding: 0;
  cursor: default;
}
/* Children need higher z-index so they appear above the section background */
.section-content > .block {
  z-index: 1;
}

.section-header {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0 0.5rem;
  height: 20px;
  box-sizing: border-box;
  border-bottom: 1px solid #e2e8f0;
  cursor: grab;
  background: rgba(255,255,255,0.55);
  -webkit-user-select: none;
  user-select: none;
  overflow: hidden;
}
.section-header:active {
  cursor: grabbing;
}

.section-toggle {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 0.65rem;
  color: #64748b;
  padding: 0 2px;
  line-height: 1;
  flex-shrink: 0;
}
.section-toggle:hover {
  color: #1d4ed8;
}

.section-title {
  font-size: 0.8rem;
  font-weight: 700;
  font-family: 'Courier New', monospace;
  color: #1e293b;
  outline: none;
  flex: 1;
  min-width: 4rem;
  border-bottom: 1px solid transparent;
}
.section-title:focus {
  border-bottom-color: #3b82f6;
}
.section-title:empty::before {
  content: attr(data-placeholder);
  color: #94a3b8;
  font-style: italic;
  font-weight: 400;
}

.section-summary {
  padding: 0 0.6rem;
  height: 20px;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  font-size: 0.7rem;
  font-family: 'Courier New', monospace;
  color: #000000;
  font-weight: 700;
  background: rgba(255,255,255,0.4);
  border-bottom: 1px solid #e2e8f0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.section-summary-empty {
  color: #94a3b8;
  font-style: italic;
  font-weight: 400;
}
/* A summary output whose variable is not defined in this section. Shown rather than dropped: a
   missing line reads as "nothing to report", and it used to silently print a same-named global. */
.section-summary-missing {
  color: #dc2626;
  font-weight: 700;
}
.section-cmp-pass {
  color: #16a34a;
  font-weight: 600;
}
.section-cmp-fail {
  color: #dc2626;
  font-weight: 600;
}
/* A check that could not be evaluated — neither pass nor fail. Amber, not red, because the
   check is broken rather than failing; it used to be dropped, which read as a pass. */
.section-cmp-error {
  color: #b45309;
  font-weight: 600;
  cursor: help;
}

.section-content {
  position: relative; /* children are absolute within this */
  min-height: 60px;
  background: rgba(248, 250, 252, 0.5);
}
.section-content.collapsed {
  display: none;
}

/* When collapsed, let the block shrink to header + summary height only */
.section-block:has(> .section-content.collapsed) {
  min-height: 0;
}

/* Drop-target highlight when dragging a non-section block over an open section */
.section-block.section-drop-target {
  background: rgba(219, 234, 254, 0.35) !important;
  border-color: #3b82f6;
  box-shadow: 0 0 0 2px #93c5fd;
}

/* Pull-down resize handle at the bottom of a section */
.section-resize-handle {
  height: 44px;
  cursor: ns-resize;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: transparent;
  touch-action: none;
}
.section-resize-handle::after {
  content: '';
  width: 40px;
  height: 3px;
  background: #94a3b8;
  border-radius: 2px;
  opacity: 0;
  transition: opacity 150ms;
}
.section-block:hover .section-resize-handle::after,
.section-block.selected .section-resize-handle::after {
  opacity: 1;
}

/* ── Touch-device resize handle enhancements ─────────────────────────────── */
/* (hover: none) + (pointer: coarse) = touch screen, not mouse/trackpad       */
@media (hover: none) and (pointer: coarse) {
  /* Always show indicators on selected blocks — hover doesn't exist on touch */
  .block.selected .text-resize-handle::after,
  .block.selected .formula-resize-handle::after,
  .block.selected .plot-right-handle::after,
  .block.selected .plot-bottom-handle::after,
  .block.selected .figure-resize-handle::after,
  .block.selected .figure-bottom-handle::after,
  .section-block.selected .section-resize-handle::after {
    opacity: 1;
  }

  /* Larger, more visible grip indicators on touch */
  .text-resize-handle::after,
  .formula-resize-handle::after,
  .figure-resize-handle::after,
  .plot-right-handle::after {
    width: 5px;
    height: 44px;
    background: #94a3b8;
  }
  .plot-bottom-handle::after,
  .figure-bottom-handle::after {
    height: 5px;
    width: 44px;
    background: #94a3b8;
  }
  .section-resize-handle::after {
    width: 64px;
    height: 5px;
  }

  /* Engaged state — lights up the moment a finger lands on the handle */
  .text-resize-handle.handle-active::after,
  .formula-resize-handle.handle-active::after,
  .figure-resize-handle.handle-active::after,
  .plot-right-handle.handle-active::after {
    background: #3b82f6;
    width: 5px;
    height: 44px;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.25);
    border-radius: 3px;
  }
  .plot-bottom-handle.handle-active::after,
  .figure-bottom-handle.handle-active::after {
    background: #3b82f6;
    height: 6px;
    width: 52px;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.25);
    border-radius: 3px;
  }
  .section-resize-handle.handle-active::after {
    background: #3b82f6;
    height: 6px;
    width: 80px;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.25);
    border-radius: 3px;
  }
}

@media print {
  .section-block {
    background: white !important;
    border-left-color: #94a3b8 !important;
    box-shadow: none;
  }
  .section-summary {
    background: #f8fafc !important;
  }
  .section-content {
    background: transparent !important;
  }
  .section-resize-handle {
    display: none !important;
  }
}

/* ── Title block ─────────────────────────────────────────────────────────── */
.title-block {
  padding: 0 !important;
  cursor: default !important;
  border: 1px solid #94a3b8;
  background: #ffffff !important;
  box-shadow: none;
  overflow: hidden;
}

.title-block-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  font-family: system-ui, sans-serif;
  /* Fixed total height: 4 rows × 28px = 112px; rows never grow beyond this */
  height: 112px;
}

.title-block-table tr {
  height: 28px;
  max-height: 28px;
  overflow: hidden;
}

/* Outer border matches the block border — cells use inner borders only */
.title-block-table td {
  border: 1px solid #94a3b8;
  vertical-align: middle;
}

/* Logo cell — left column, rowspan 4 */
.tb-logo {
  width: 160px;
  min-width: 160px;
  max-width: 160px;
  overflow: hidden;
  vertical-align: middle !important;
  cursor: pointer;
  position: relative;
  border-right: 2px solid #475569 !important;
}

.tb-logo-img {
  display: block;
  width: 100%;
  max-height: 112px; /* total title block height — never taller */
  height: auto;
  object-fit: contain;
  object-fit: contain;
  padding: 4px;
}

.tb-logo-ph {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  min-height: 60px;
  gap: 4px;
  color: #94a3b8;
  font-size: 0.65rem;
  text-align: center;
  padding: 4px;
  -webkit-user-select: none;
  user-select: none;
}

.tb-label {
  font-size: 0.58rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #64748b;
  padding: 4px 5px;
  line-height: 1;
  -webkit-user-select: none;
  user-select: none;
  flex-shrink: 0;
}

.tb-value {
  outline: none;
  cursor: text;
  font-size: 0.8rem;
  font-family: system-ui, sans-serif;
  color: #1e293b;
  padding: 4px 5px;
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-word;
  min-height: 1.2em;
}

.tb-value:focus {
  background: rgba(59, 130, 246, 0.05);
}

.tb-value:empty::before {
  content: attr(data-placeholder);
  color: #cbd5e1;
  pointer-events: none;
}

/* Narrow cells (By, Sheet No., Date, Job No.) vs wide cells (Project, Subject) */
.tb-wide {
  width: auto;
}
.tb-narrow {
  width: 90px;
  min-width: 70px;
  max-width: 110px;
}
.tb-blank {
  background: transparent;
}
.tb-sheet-num {
  cursor: default;
  color: #475569;
  font-family: ui-monospace, monospace;
  font-size: 0.75rem;
  text-align: center;
  -webkit-user-select: none;
  user-select: none;
}

/* Row separator inside the data area */
.title-block-table tr:first-child td,
.title-block-table tr:last-child td {
  border-top: 1px solid #94a3b8;
  border-bottom: 1px solid #94a3b8;
}

/* Thicker top border — engineering style */
.title-block-table {
  border-top: 2px solid #475569;
  border-bottom: 2px solid #475569;
}

@media print {
  .title-block {
    background: white !important;
    border-color: #64748b;
  }
  .title-block-table td {
    border-color: #64748b;
  }
  .title-block-table {
    border-top-color: #1e293b;
    border-bottom-color: #1e293b;
  }
  .tb-logo {
    border-right-color: #1e293b !important;
  }
  .tb-value:empty::before {
    content: none !important;
  }
}

.sidebar-license {
  display: block;
  font-size: 10px;
  color: #94a3b8;
  text-decoration: none;
  text-align: center;
  padding: 2px 8px 8px;
  line-height: 1.4;
}

.sidebar-license:hover {
  text-decoration: underline;
}

.sidebar-version {
  font-size: 10px;
  color: #64748b;
  padding: 0 0.5rem 0.25rem;
  line-height: 1.4;
}

/* ── Auth panel ─────────────────────────────────────────────────────────── */
.auth-panel {
  font-size: 0.8rem;
}

/* ── Pro-locked module items ────────────────────────────────────────────── */
.module-item.module-locked {
  opacity: 0.55;
  cursor: not-allowed;
}

.module-item.module-locked:active {
  transform: none;
}

.module-pro-badge {
  margin-left: auto;
  font-size: 0.6rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  padding: 0.1rem 0.35rem;
  border-radius: 3px;
  background: #0284c7;
  color: #fff;
  white-space: nowrap;
}

/* Math Display controls (sidebar) */
.math-size-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
  font-size: 0.75rem;
  color: #374151;
}
.math-size-row select {
  font-family: inherit;
  font-size: 0.75rem;
  padding: 2px 4px;
  border: 1px solid #e4e4e7;
  border-radius: 0.25rem;
  background: #ffffff;
  cursor: pointer;
  /* The Font row previews each face in its own font, so widths vary — keep it inside the sidebar. */
  max-width: 9.5rem;
}

/* Comparison operator inside rendered math (≥ ≤ ≠ =) */
.fp-cmp {
  font-style: normal;
  padding: 0 0.15em;
}
