:root {
  --bg: #fafafa;
  --bg-elev: #ffffff;
  --bg-soft: #f1f3f5;
  --border: #e5e7eb;
  --border-strong: #d4d4d8;
  --text: #1f2328;
  --text-soft: #6b7280;
  --text-faint: #9ca3af;
  --accent: #2563eb;       /* variables / blue */
  --accent-soft: rgba(37, 99, 235, 0.15);
  --operator: #6b7280;     /* operators / grey */
  --result: #16a34a;       /* answer / green */
  --op-green: #166534;     /* operators (+ - * / ^ =) / dark green */
  --error: #dc2626;
  --error-soft: #fee2e2;
  --header: #111827;
  --bullet: #6366f1;
  --string: #b45309;
  --selection: rgba(147, 178, 255, 0.45);
  --result-stale: #9ca3af;
  --error-bg: rgba(220, 38, 38, 0.08);
  --scrollbar: rgba(0, 0, 0, 0.12);
  --scrollbar-hover: rgba(0, 0, 0, 0.2);
  --shadow: 0 8px 30px rgba(15, 23, 42, 0.12);
  --radius: 12px;
  --font-mono: ui-monospace, SFMono-Regular, "JetBrains Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --line-height: 22px;
  --editor-padding: 10px;
  --editor-reserve: 12px;   /* right padding of the editor; layoutGutters() widens it to the answer-column width ONLY when a math formula would otherwise collide with its answer (otherwise text + math both keep full width) */
  --editor-font-size: 14px;
}

/* ---------- dark theme tokens ---------- */
:root[data-theme="dark"] {
  --bg: #0f1115;
  --bg-elev: #181b22;
  --bg-soft: #232732;
  --border: #2a2f3a;
  --border-strong: #3a4150;
  --text: #e6e8ee;
  --text-soft: #9aa3b2;
  --text-faint: #5f6776;
  --accent: #60a5fa;
  --accent-soft: rgba(96, 165, 250, 0.22);
  --operator: #9aa3b2;
  --result: #4ade80;
  --op-green: #4ade80;
  --error: #f87171;
  --error-soft: rgba(248, 113, 113, 0.18);
  --header: #f3f4f6;
  --bullet: #a5b4fc;
  --string: #fbbf24;
  --selection: rgba(96, 165, 250, 0.35);
  --result-stale: #6b7280;
  --error-bg: rgba(248, 113, 113, 0.14);
  --scrollbar: rgba(255, 255, 255, 0.14);
  --scrollbar-hover: rgba(255, 255, 255, 0.28);
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.55);
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="system"] {
    --bg: #0f1115;
    --bg-elev: #181b22;
    --bg-soft: #232732;
    --border: #2a2f3a;
    --border-strong: #3a4150;
    --text: #e6e8ee;
    --text-soft: #9aa3b2;
    --text-faint: #5f6776;
    --accent: #60a5fa;
    --accent-soft: rgba(96, 165, 250, 0.22);
    --operator: #9aa3b2;
    --result: #4ade80;
  --op-green: #4ade80;
    --error: #f87171;
    --error-soft: rgba(248, 113, 113, 0.18);
    --header: #f3f4f6;
    --bullet: #a5b4fc;
    --string: #fbbf24;
    --selection: rgba(96, 165, 250, 0.35);
    --result-stale: #6b7280;
    --error-bg: rgba(248, 113, 113, 0.14);
    --scrollbar: rgba(255, 255, 255, 0.14);
    --scrollbar-hover: rgba(255, 255, 255, 0.28);
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.55);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 13px;
  overflow: hidden;
  user-select: none;
}

body {
  display: flex;
  flex-direction: column;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

::selection {
  background: var(--selection);
}
/* The textarea text is invisible (overlay shows the colors). Use a
   translucent selection so the overlay's colored text shows THROUGH the
   highlight rectangle. */
.editor::selection {
  background: var(--selection);
  color: transparent;
}

/* ---------- title bar ---------- */
.title-bar {
  display: flex;
  align-items: center;
  height: 34px;
  padding: 0 8px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  border-top-left-radius: var(--radius);
  border-top-right-radius: var(--radius);
  -webkit-app-region: drag;
}

.drag-handle {
  flex: 0 0 14px;
  height: 18px;
  background-image: radial-gradient(circle, var(--text-faint) 1px, transparent 1.2px);
  background-size: 4px 4px;
  background-position: 0 2px;
  background-repeat: repeat;
  opacity: 0.65;
  margin-right: 8px;
}

.left-actions {
  display: flex;
  gap: 4px;
  -webkit-app-region: no-drag;
}

.title-spacer {
  flex: 1;
}

.mode-toggle {
  display: inline-flex;
  margin-right: 8px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 2px;
  -webkit-app-region: no-drag;
}
.mode-btn {
  border: 0;
  background: transparent;
  padding: 2px 10px;
  font-size: 11px;
  font-weight: 500;
  color: var(--text-soft);
  border-radius: 5px;
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease;
}
.mode-btn:hover { color: var(--text); }
.mode-btn.active {
  background: var(--bg-elev);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.06);
}

.actions {
  display: flex;
  gap: 4px;
  -webkit-app-region: no-drag;
}

.icon-btn {
  width: 26px;
  height: 24px;
  border: none;
  background: transparent;
  color: var(--text-soft);
  font-size: 13px;
  border-radius: 6px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 120ms ease, color 120ms ease;
}
.icon-btn:hover { background: var(--bg-soft); color: var(--text); }
.icon-btn.close:hover { background: var(--error-soft); color: var(--error); }
.icon-btn.active {
  background: var(--accent-soft);
  color: var(--accent);
}
.icon-btn.active:hover {
  background: var(--accent-soft);
  color: var(--accent);
}

.icon-btn { position: relative; }
.icon-btn.has-update::after {
  content: "";
  position: absolute;
  top: 3px;
  right: 3px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 1.5px var(--bg);
  pointer-events: none;
}

/* ---------- tab bar (toggled by the tabs button) ---------- */
.tab-bar {
  display: flex;
  align-items: center;
  gap: 4px;
  max-height: 0;
  padding: 0 6px;
  overflow: hidden;
  background: var(--bg-elev);
  border-bottom: 1px solid transparent;
  opacity: 0;
  -webkit-app-region: no-drag;
  transition: max-height 170ms ease, opacity 130ms ease, border-color 170ms ease;
}
.tab-bar.open {
  max-height: 30px;
  opacity: 1;
  border-bottom-color: var(--border);
}

/* the strip that holds the chips. It hugs its content (flex-grow 0) so the
   chevron + "+" sit right after the last tab and slide right as tabs are added,
   Chrome-style. layoutTabs() temporarily adds .tab-measuring to make the strip
   fill the bar while it measures the room available for overflow. */
.tab-strip {
  position: relative;
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 0 1 auto;
  min-width: 0;
  height: 100%;
  overflow: hidden;
}
.tab-bar.tab-measuring .tab-strip { flex-grow: 1; }

/* a tab "chip" — browser style: name + an always-visible close (×).
   Its height matches the bar so the active fill goes edge to edge. */
.tab-chip {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 24px;
  padding: 0 5px 0 10px;
  border-radius: 6px;
  font-size: 12px;
  color: var(--text-soft);
  cursor: pointer;
  white-space: nowrap;
  flex: 0 0 auto;
  max-width: 170px;
  transition: background 120ms ease, color 120ms ease;
}
.tab-chip:hover { background: var(--bg-soft); color: var(--text); }
.tab-chip.active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
  border-radius: 7px 7px 0 0;   /* Chrome shape: rounded top, square bottom */
}
.tab-chip.dragging { opacity: 0.55; }
.tab-chip.tab-hidden { display: none; }

/* Chrome-style pill separator between tabs (hidden around active/hovered) */
.tab-strip .tab-chip:not(:first-child)::before {
  content: "";
  position: absolute;
  left: -3px;
  top: 50%;
  transform: translateY(-50%);
  width: 2px;
  height: 13px;
  border-radius: 99px;
  background: var(--border);
  opacity: 1;
  transition: opacity 120ms ease;
  pointer-events: none;
}
.tab-strip .tab-chip:hover::before,
.tab-strip .tab-chip:hover + .tab-chip::before,
.tab-strip .tab-chip.active + .tab-chip::before,
.tab-strip .tab-chip.dragging::before,
.tab-strip .tab-chip.dragging + .tab-chip::before {
  opacity: 0;
}

/* active tab bottom corners flare OUT and blend into the strip below via two
   radial-masked "ears" — applies whether active naturally OR surfaced from the
   dropdown. top:auto cancels the separator's top:50% so the ears sit at the
   bottom corners (not floating in the middle). */
.tab-strip .tab-chip.active::before,
.tab-strip .tab-chip.active::after {
  content: "";
  position: absolute;
  top: auto;
  bottom: 0;
  width: 7px;
  height: 7px;
  background: var(--accent-soft);
  border-radius: 0;
  opacity: 1;
  transform: none;
  transition: none;
  pointer-events: none;
}
.tab-strip .tab-chip.active::before {
  left: -7px;
  -webkit-mask: radial-gradient(circle at top left, transparent 7px, #000 7.5px);
          mask: radial-gradient(circle at top left, transparent 7px, #000 7.5px);
}
.tab-strip .tab-chip.active::after {
  right: -7px;
  -webkit-mask: radial-gradient(circle at top right, transparent 7px, #000 7.5px);
          mask: radial-gradient(circle at top right, transparent 7px, #000 7.5px);
}

/* a tab surfaced from the overflow dropdown: extra left room + a double pill
   marker ("| |") so it's clearly pulled in from the menu. The marker is a real
   child element (not a pseudo) so its two pills are guaranteed equal and don't
   collide with the active ears above. */
.tab-chip.from-overflow { margin-left: 18px; }
/* Marker before a tab pulled in from the overflow: two small bars standing in
   for the tabs hidden in between. The element is a wider invisible hit area (the
   bars are the only visible part) so it's easy to hover; on hover it shows a
   styled tooltip explaining what it is (wired in layoutTabs). */
.tab-overflow-mark {
  position: absolute;
  left: -16px;
  top: 0;
  bottom: 0;
  width: 16px;
  cursor: help;
}
.tab-overflow-mark::before,
.tab-overflow-mark::after {
  content: "";
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 2px;
  height: 13px;
  border-radius: 99px;
  background: var(--border);
}
.tab-overflow-mark::before { left: 0; }
.tab-overflow-mark::after  { left: 5px; }

.tab-chip-name {
  flex: 0 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}
/* close (×) — always visible, like a regular browser tab */
.tab-chip-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 17px;
  height: 17px;
  border-radius: 4px;
  font-size: 14px;
  line-height: 1;
  flex: 0 0 auto;
  /* Pale "disarmed" red on a background tab: visible but not inviting, and a
     click just selects the tab (see renderTabBar) so you can't close it by
     mis-clicking while switching. Middle-click closes any tab. */
  color: color-mix(in srgb, var(--error) 45%, transparent);
  transition: background 100ms ease, color 100ms ease;
}
/* Active tab: the × is "armed" — solid red, a click closes, clear hover target. */
.tab-chip.active .tab-chip-close { color: var(--error); }
.tab-chip.active .tab-chip-close:hover { background: var(--error-soft); color: var(--error); }

/* overflow chevron + "+" new-tab button — trail the last tab, always visible */
.tab-overflow-btn,
.tab-bar-add {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 24px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-soft);
  line-height: 1;
  cursor: pointer;
  flex: 0 0 auto;
  -webkit-app-region: no-drag;
  transition: background 120ms ease, color 120ms ease;
}
/* Overflow chevron renders as a small accent pill showing how many tabs are
   hidden (e.g. "3 ⌄"). It only appears when tabs overflow, so the count makes
   its purpose unmistakable. */
.tab-overflow-btn {
  width: auto;
  min-width: 34px;
  gap: 2px;
  padding: 0 7px 0 8px;
  border-radius: 12px;
  background: var(--bg-soft);
  color: var(--text-soft);
  font-size: 11px;
  font-weight: 600;
}
.tab-bar-add { width: 24px; font-size: 17px; }
.tab-overflow-btn:hover { background: var(--bg-soft); color: var(--text); filter: brightness(0.94); }
.tab-bar-add:hover { background: var(--bg-soft); color: var(--text); }
.tab-overflow-btn[hidden] { display: none; }
.ov-count { line-height: 1; }

.tab-bar .tab-rename-input { width: 96px; }

/* overflow popup — the list of clipped tabs */
.tab-overflow-popup { min-width: 150px; }
.tab-overflow-popup .ov-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 6px 5px 9px;
  border-radius: 5px;
  font-size: 12px;
  color: var(--text);
  white-space: nowrap;
  cursor: pointer;
  transition: background 100ms ease;
}
.tab-overflow-popup .ov-name {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tab-overflow-popup .ov-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 17px;
  height: 17px;
  border-radius: 4px;
  font-size: 14px;
  line-height: 1;
  flex: 0 0 auto;
  color: var(--text-faint);
  transition: background 100ms ease, color 100ms ease;
}
.tab-overflow-popup .ov-row:hover { background: var(--bg-soft); }
.tab-overflow-popup .ov-row:hover .ov-close { color: var(--text-soft); }
.tab-overflow-popup .ov-close:hover { background: var(--error-soft); color: var(--error); }

/* reorder mode — drag ALL tabs into a new order, then click Done */
.tab-overflow-popup .ov-reorder-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 2px 4px 6px;
  margin-bottom: 2px;
  border-bottom: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-faint);
}
.tab-overflow-popup .ov-done {
  border: none;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 11px;
  font-weight: 600;
  padding: 3px 12px;
  border-radius: 5px;
  cursor: pointer;
  transition: background 100ms ease, color 100ms ease;
}
.tab-overflow-popup .ov-done:hover { background: var(--accent); color: #fff; }
.reorder-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 8px;
  border-radius: 5px;
  font-size: 12px;
  color: var(--text);
  white-space: nowrap;
  cursor: grab;
  transition: background 100ms ease;
}
.reorder-row:hover { background: var(--bg-soft); }
.reorder-row.active { color: var(--accent); font-weight: 600; }
.reorder-row.dragging { opacity: 0.5; background: var(--bg-soft); }
.reorder-handle {
  flex: 0 0 auto;
  color: var(--text-faint);
  font-size: 13px;
  line-height: 1;
}
.reorder-name {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* right-click context menu for a tab (Rename / Close) */
.tab-context-menu {
  position: fixed;
  z-index: 70;
  min-width: 132px;
  padding: 4px;
  background: var(--bg-elev);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  box-shadow: var(--shadow);
  font-family: var(--font-ui);
  font-size: 12px;
}
.tab-context-menu[hidden] { display: none; }
.tab-context-menu .ctx-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 5px;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  transition: background 100ms ease;
}
.tab-context-menu .ctx-item:hover { background: var(--bg-soft); }
.tab-context-menu .ctx-item.danger:hover { background: var(--error-soft); color: var(--error); }
.tab-context-menu .ctx-separator {
  height: 1px;
  margin: 4px 6px;
  background: var(--border);
}
/* Mode word in the line-switch menu, color-coded to match the editor (green =
   math, blue = text) so the target mode is unmistakable. */
.tab-context-menu .ctx-mode-math { color: var(--result); font-weight: 700; }
.tab-context-menu .ctx-mode-text { color: var(--accent); font-weight: 700; }
.tab-context-menu .ctx-key {
  margin-left: auto;
  padding-left: 16px;
  color: var(--text-faint);
  font-size: 11px;
}

/* ---------- editor area ---------- */
.editor-shell {
  flex: 1;
  display: flex;
  min-height: 0;
  background: var(--bg);
  overflow: hidden;
  position: relative;
}

/* The old right-hand result column is retired — results now render inline at the
   right edge of each math line (see .result-overlay). Keep the element in the
   DOM but hidden so layout reclaims its width for the editor. */
.result-gutter { display: none; }

.line-gutter, .result-gutter {
  flex: 0 0 auto;
  font-family: var(--font-mono);
  font-size: var(--editor-font-size);
  line-height: var(--line-height);
  color: var(--text-faint);
  padding: var(--editor-padding) 8px;
  overflow: hidden;
  background: var(--bg);
  user-select: none;
}

.line-gutter {
  text-align: right;
  min-width: 38px;
  border-right: 1px solid var(--border);
}

.result-gutter {
  text-align: left;
  min-width: 110px;
  color: var(--result);
  border-left: 1px solid var(--border);
  font-weight: 600;
}

.line-gutter .row {
  display: flex;
  align-items: flex-start;
  justify-content: flex-end;
  white-space: nowrap;
  position: relative;         /* for the wrapped-line marker (::before) */
}
/* The number sits in an inline pill so hover/active/ref highlights hug it
   instead of filling (and clipping at) the full gutter width. */
.line-gutter .lnum {
  display: inline-block;
  padding: 0 5px;
  border-radius: 5px;
  transition: background 90ms ease, color 90ms ease;
}
/* Every line gets a faint pill marker just to the RIGHT of its number. It's
   inset 35% of a line-height from the top (and a touch more from the bottom), so
   a single-row line shows a short "elongated dot", while a wrapped line's marker
   stretches into a bar spanning its rows — so wrapping reads as one long line.
   The pill radius gives the soft rounded ends. */
.line-gutter .row::before {
  content: "";
  position: absolute;
  right: -4px;                 /* just right of the (right-aligned) number, clear of its pill */
  top: calc(var(--line-height) * 0.35);
  bottom: calc(var(--line-height) * 0.35 - 2px);   /* a touch longer at the bottom */
  width: 1.5px;                /* same width for the short dots and the longer wrapped bars */
  border-radius: 1px;
  background: var(--text-faint);
  opacity: 0.4;
}

/* Each line number is a button: click it to insert that line's L-ref at the
   caret. Pointer + a blue pill on hover (and a darker press state) make the
   affordance obvious. */
.line-gutter .row[data-line] { cursor: pointer; }
/* Highlight ONLY the line the caret is on (not on hover). */
.line-gutter .row.current .lnum { background: rgba(100, 116, 139, 0.22); }
.line-gutter .row[data-line]:active .lnum { background: rgba(100, 116, 139, 0.34); }
.result-gutter .row {
  display: flex;
  align-items: flex-end;
  justify-content: flex-start;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  position: relative;
}

.result-gutter .row.error {
  color: var(--error);
  font-weight: 500;
}
.result-gutter .row.error[data-tooltip] {
  cursor: help;
}
/* Calm, standardized error styling in the result column: undefined names and
   mid-typing/incomplete read as faint grey; genuine errors use a muted (not
   aggressive) error color. Both are dotted-underlined "N/A". */
.result-gutter .row.err-faint,
.result-gutter .row.err-calm {
  font-weight: 500;
  text-decoration: underline dotted;
  text-underline-offset: 2px;
}
.result-gutter .row.err-faint { color: var(--text-faint); }
.result-gutter .row.err-calm { color: color-mix(in srgb, var(--error) 74%, var(--text-faint)); }
.result-gutter .row.err-faint[data-tooltip],
.result-gutter .row.err-calm[data-tooltip] {
  cursor: help;
}
.result-gutter .row.stale {
  color: var(--result-stale);
  font-weight: 500;
}
.result-gutter .row.empty {
  color: transparent;
}
.result-gutter .row:not(.empty):hover {
  background: rgba(22, 163, 74, 0.08);
  border-radius: 3px;
}
.result-gutter .copy-icon {
  position: absolute;
  right: 3px;
  bottom: 2px;
  opacity: 0;
  pointer-events: none;
  color: var(--text-soft);
  line-height: 1;
  transition: opacity 120ms ease;
}
.result-gutter .row:not(.empty):hover .copy-icon {
  opacity: 0.6;
}
.result-gutter .copy-icon.copied {
  opacity: 1;
  color: var(--result);
}
.copy-svg {
  width: 10px;
  height: 10px;
  vertical-align: middle;
  display: inline-block;
}
:root[data-theme="dark"] .result-gutter .row:not(.empty):hover,
:root[data-theme="system"] .result-gutter .row:not(.empty):hover {
  background: rgba(74, 222, 128, 0.08);
}
@media (prefers-color-scheme: dark) {
  :root[data-theme="system"] .result-gutter .row:not(.empty):hover {
    background: rgba(74, 222, 128, 0.08);
  }
}

/* ---------- inline result overlay: the green "= answer" on math lines ---------- */
.result-overlay {
  position: absolute;
  inset: 0;
  z-index: 3;                  /* above the textarea (z2) so chips show + are clickable */
  pointer-events: none;        /* but let clicks fall through to the textarea... */
  overflow: hidden;
  color: var(--result);
  font-weight: 600;
  font-family: var(--font-mono);
  font-size: var(--editor-font-size);
  line-height: var(--line-height);
  /* Chips sit 12px from the right edge and paint over the editor (z-index 3)
     with an opaque backdrop, so on a long math line the formula's end tucks
     neatly under the "= answer" rather than colliding with it. */
  padding: var(--editor-padding) 12px;
}
.result-overlay .row {
  display: flex;
  align-items: flex-end;       /* sit at the bottom of a wrapped line, like the old column */
  justify-content: flex-end;   /* pinned to the right edge */
}
.result-overlay .res {
  pointer-events: auto;        /* ...except the chip itself, which is hoverable/clickable */
  /* Opaque backdrop (matches the editor bg, so it's invisible over empty space
     but cleanly hides a long formula's tail that runs under the result). When
     the caret is in this math line the active/hover rule below swaps in a
     translucent green so the formula shows through for editing. */
  background: var(--bg-elev);
  max-width: none;             /* layoutGutters() sets an explicit min/max-width so every "=" lines up in a column */
  padding: 0 1px 0 6px;
  border-radius: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: left;            /* with the JS-set min-width, lines the "=" up in a column */
  cursor: pointer;
}
.result-overlay .res .eq { color: var(--result); margin-right: 2px; }
.result-overlay .res:hover,
.result-overlay .res.active { background: rgba(22, 163, 74, 0.12); }
:root[data-theme="dark"] .result-overlay .res.active,
:root[data-theme="system"] .result-overlay .res.active { background: rgba(74, 222, 128, 0.12); }
.result-overlay .res.stale { color: var(--result-stale); }
.result-overlay .res.err-faint { color: var(--text-faint); font-weight: 500; }
.result-overlay .res.err-calm { color: color-mix(in srgb, var(--error) 74%, var(--text-faint)); font-weight: 500; }
.result-overlay .res.err-faint .eq,
.result-overlay .res.err-calm .eq { color: var(--result); }   /* keep the "=" green even on errors */
.result-overlay .res .copy-icon {
  opacity: 0;
  margin-left: 5px;
  color: var(--text-soft);
  transition: opacity 120ms ease;
}
.result-overlay .res:hover .copy-icon { opacity: 0.6; }
.result-overlay .res .copy-icon.copied { opacity: 1; color: var(--result); }
:root[data-theme="dark"] .result-overlay .res:hover,
:root[data-theme="system"] .result-overlay .res:hover { background: rgba(74, 222, 128, 0.12); }

/* Math lines: a green underline marks the number as math when it's NOT the
   current line; the current math line (caret in it) gets the green highlight
   fill instead (no underline). */
.line-gutter .row.math-line .lnum {
  color: var(--result);
  font-weight: 600;
  text-decoration: underline;
  text-decoration-color: var(--result);
  text-underline-offset: 2px;
}
.line-gutter .row.current.math-line .lnum {
  background: rgba(22, 163, 74, 0.26);
  text-decoration: none;
}

/* inline ""L#"" references shown inside text lines */
.tk-textref { color: var(--result); font-weight: 600; cursor: pointer; }
.tk-textref:hover { background: rgba(22, 163, 74, 0.12); border-radius: 3px; }
.tk-textref-raw { color: var(--result); opacity: 0.8; }
.tk-textref-unresolved { color: var(--text-faint); opacity: 0.75; }

/* ---------- editor stack: textarea + overlay ---------- */
.editor-stack {
  flex: 1;
  position: relative;
  min-width: 0;
  background: var(--bg-elev);
  overflow: hidden;
}

.editor, .syntax-overlay, .measure, .find-layer {
  font-family: var(--font-mono);
  font-size: var(--editor-font-size);
  line-height: var(--line-height);
  /* Top/bottom/left padding live on the container; the RIGHT padding lives on
     each line block (.ed-line / .ov-line) so lines can wrap at different widths:
     math lines reserve the answer column (--editor-reserve) while text lines use
     the full width. A single textarea can't do that; per-line blocks can. */
  padding: var(--editor-padding) 0 var(--editor-padding) 12px;
  margin: 0;
  border: 0;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: break-word;
  letter-spacing: 0;
  tab-size: 4;
}

.syntax-overlay {
  position: absolute;
  inset: 0;
  overflow: auto;
  pointer-events: none;
  color: var(--text);
  background: transparent;
  z-index: 1;
}

/* One block per logical line, in BOTH the editor (.ed-line) and the colored
   overlay / find layers (.ov-line). The right padding is the per-line wrap
   width: text lines keep (nearly) the full width; math lines reserve the answer
   column so the formula wraps before the "= answer". */
.ed-line, .ov-line {
  display: block;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: break-word;
  padding-right: 12px;
  min-height: var(--line-height);
}
.ed-line.ed-math, .ov-line.ov-math {
  padding-right: var(--editor-reserve);
}

/* Obsidian-style conceal: collapse the inline bold/italic/underline markers on
   every line except the one the caret is on. The editor's transparent .ed-mk and
   the overlay's colored .md-marker wrap the SAME characters, so hiding both keeps
   the two layers character-aligned (and only inline markers hide — list bullets
   .md-bullet and header hashes .md-h-marker are untouched). */
/* Inline markers (** * __) collapse whenever the caret is off the line. */
.ed-line.ed-conceal .ed-mk,
.ov-line.ov-conceal .md-marker { display: none; }
/* A task line's "- " bullet collapses unless the caret is in its marker region
   (ed-bullet-conceal / ov-bullet-conceal are toggled per caret column, not just
   per line — see applyEditorConceal / highlightNote). */
.ed-line.ed-bullet-conceal .ed-task-mk,
.ov-line.ov-bullet-conceal .md-task-bullet { display: none; }

.editor {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  background: transparent;
  color: transparent;          /* the colored overlay shows the text; this layer holds the caret */
  caret-color: var(--text);
  outline: none;
  z-index: 2;
  user-select: text;
  cursor: text;
}
/* The contenteditable's line blocks inherit the transparent color; the caret
   and (translucent) selection are what's visible here. */
.editor .ed-line { color: transparent; }

.measure {
  position: absolute;
  visibility: hidden;
  pointer-events: none;
  top: 0;
  left: 0;
  right: 0;
  height: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ---------- find (Ctrl+F) ---------- */
/* A highlight layer behind the syntax overlay. Its own text is transparent;
   only the <mark> backgrounds show, sitting behind the colored overlay text.
   Shares the editor's font/wrap rules above, so match boxes align exactly. */
/* overflow:auto (not hidden) so it reserves the same 8px scrollbar gutter as
   the textarea/overlay when content overflows — otherwise its wider content
   would wrap differently and the match boxes would drift out of alignment. Its
   scrollbar sits behind the textarea's (z-index), so only one is ever seen. */
.find-layer {
  position: absolute;
  inset: 0;
  overflow: auto;
  pointer-events: none;
  color: transparent;
  background: transparent;
  z-index: 0;
}
.find-layer .ov-line {
  display: block;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: break-word;
}
.find-hit {
  color: transparent;
  background: rgba(250, 204, 21, 0.45);
  border-radius: 2px;
}
.find-hit.current {
  background: rgba(249, 115, 22, 0.72);
}
:root[data-theme="dark"] .find-hit,
:root[data-theme="system"] .find-hit { background: rgba(250, 204, 21, 0.36); }
:root[data-theme="dark"] .find-hit.current,
:root[data-theme="system"] .find-hit.current { background: rgba(249, 115, 22, 0.85); }
@media (prefers-color-scheme: dark) {
  :root[data-theme="system"] .find-hit { background: rgba(250, 204, 21, 0.36); }
  :root[data-theme="system"] .find-hit.current { background: rgba(249, 115, 22, 0.85); }
}

.find-bar {
  position: absolute;
  top: 8px;
  right: 14px;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 1px;
  padding: 3px 4px 3px 11px;
  background: var(--bg-elev);
  border: 1px solid var(--border-strong);
  border-radius: 9px;
  box-shadow: 0 4px 16px rgba(15, 23, 42, 0.18);
  font-family: var(--font-ui);
  transition: background 130ms ease, border-color 130ms ease;
}
.find-bar[hidden] { display: none; }
/* Non-empty query with no matches: the whole bar goes pale red (keep the
   neutral border — no red outline). */
.find-bar.no-results {
  background: var(--error-soft);
}
.find-bar.no-results .find-count { color: var(--error); }
.find-input {
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
  width: 58px;          /* resting ~6 chars; JS grows it to fit the query */
  max-width: 240px;
  font-size: 13px;
  font-family: inherit;
  padding: 3px 2px;
  transition: width 70ms ease-out;
}
.find-input::placeholder { color: var(--text-faint); }
.find-count {
  color: var(--text-faint);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  padding: 0 6px;
  min-width: 40px;
  text-align: right;
}
.find-divider {
  width: 1px;
  align-self: stretch;
  margin: 4px 4px 4px 2px;
  background: var(--border);
}
.find-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text-soft);
  cursor: pointer;
  transition: background 90ms ease, color 90ms ease;
}
.find-btn:hover:not(:disabled) {
  background: var(--accent-soft);
  color: var(--text);
}
.find-btn:disabled {
  opacity: 0.35;
  cursor: default;
}
.find-btn.find-close:hover {
  background: var(--error-soft);
  color: var(--error);
}

/* ---------- syntax tokens ---------- */
.tk-num { color: var(--text); }
.tk-op  { color: var(--op-green); }
.tk-var { color: var(--accent); font-weight: 500; }
.tk-fn  { color: #7c3aed; }
.tk-pct { color: var(--operator); }
.tk-bps { color: var(--operator); }
.tk-paren { color: var(--text-faint); }
.tk-lref { color: #0ea5e9; font-weight: 500; }
.tk-comment { color: var(--text-faint); font-style: italic; }
.tk-directive {
  color: #7c3aed;
  font-weight: 600;
  background: var(--accent-soft);
  padding: 0 4px;
  border-radius: 4px;
}
.tk-lrange { color: #0ea5e9; font-weight: 500; }
.tk-currency { color: var(--text-faint); }
.tk-excel { color: #0d9488; font-weight: 600; }
.tk-reserved-x {
  color: var(--error);
  text-decoration: underline wavy;
  text-decoration-thickness: 1px;
}

/* ---------- click-to-highlight token references ---------- */
.tk-hl-ref {
  background: var(--accent-soft);
  border-radius: 3px;
}
.line-gutter .row.hl-lref .lnum {
  color: #0ea5e9;
  background: rgba(14, 165, 233, 0.12);
}
:root[data-theme="dark"] .line-gutter .row.hl-lref .lnum,
:root[data-theme="system"] .line-gutter .row.hl-lref .lnum {
  color: #38bdf8;
  background: rgba(56, 189, 248, 0.12);
}
@media (prefers-color-scheme: dark) {
  :root[data-theme="system"] .line-gutter .row.hl-lref .lnum {
    color: #38bdf8;
    background: rgba(56, 189, 248, 0.12);
  }
}

/* ---------- command menu (slash + L) ---------- */
.cmd-menu {
  position: fixed;
  z-index: 50;
  background: var(--bg-elev);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  box-shadow: var(--shadow);
  font-family: var(--font-ui);
  font-size: 12px;
  min-width: 200px;
  max-width: 320px;
  /* sized in JS to fit ~5 visible items */
  overflow-y: auto;
  padding: 4px;
}
.cmd-menu[hidden] { display: none; }
.cmd-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 5px;
  cursor: pointer;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
}
.cmd-item .cmd-label {
  overflow: hidden;
  text-overflow: ellipsis;
  font-family: var(--font-mono);
  font-size: 12px;
}
.cmd-item .cmd-hint {
  color: var(--text-faint);
  font-size: 11px;
  flex-shrink: 0;
}
.cmd-item.active {
  background: var(--accent-soft);
  color: var(--accent);
}
.cmd-item.active .cmd-hint { color: var(--accent); opacity: 0.85; }
.cmd-empty {
  padding: 8px 10px;
  color: var(--text-faint);
  font-size: 11px;
  font-style: italic;
}

/* ---------- result gutter: link-style errors ---------- */
.result-gutter .row.link-error {
  color: var(--accent);
  text-decoration: underline;
  cursor: help;
  font-weight: 500;
}
.result-gutter .row.link-error.excel {
  color: var(--error);
}

/* ---------- signature tooltip (Excel-style intellisense) ---------- */
.signature-tooltip {
  position: fixed;
  z-index: 40;
  max-width: 380px;
  padding: 6px 10px;
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  box-shadow: var(--shadow);
  font-family: var(--font-ui);
  font-size: 11px;
  line-height: 1.45;
  pointer-events: none;
}
.signature-tooltip[hidden] { display: none; }
.signature-tooltip .sig-name {
  font-family: var(--font-mono);
  font-weight: 600;
  color: var(--text);
}
.signature-tooltip .sig-args {
  font-family: var(--font-mono);
  color: var(--text-soft);
}
.signature-tooltip .sig-arg-active {
  color: var(--accent);
  font-weight: 600;
}
.signature-tooltip .sig-desc {
  margin-top: 4px;
  color: var(--text-soft);
  font-size: 11px;
}

/* ---------- variables popup (ƒ button hover) ---------- */
.vars-btn {
  font-style: italic;
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: 14px;
}
.vars-popup {
  position: fixed;
  z-index: 60;
  min-width: 180px;
  max-width: 320px;
  max-height: min(260px, calc(100vh - 48px));
  overflow-y: auto;
  padding: 4px;
  background: var(--bg-elev);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  box-shadow: var(--shadow);
  font-family: var(--font-ui);
  font-size: 12px;
}
.vars-popup[hidden] { display: none; }
.vars-popup .vars-empty {
  padding: 8px 10px;
  color: var(--text-faint);
  font-size: 11px;
  font-style: italic;
}
.popup-footer {
  border-top: 1px solid var(--border);
  padding: 4px 0 0 0;
  margin-top: 4px;
  position: sticky;
  bottom: -4px;
  background: var(--bg-elev);
}
.popup-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-soft);
  color: var(--text);
  border: 1px solid transparent;
  border-radius: 4px;
  padding: 6px;
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  transition: background 100ms;
}
.popup-btn:hover {
  background: var(--border);
}
.tab-rename-input {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--accent);
  border-radius: 4px;
  font-family: var(--font-ui);
  font-size: 11px;
  padding: 2px 4px;
  width: 100px;
  outline: none;
}
.vars-popup .vars-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  padding: 5px 8px;
  border-radius: 5px;
  white-space: nowrap;
  font-family: var(--font-mono);
  font-size: 12px;
}
.vars-popup .vars-row + .vars-row {
  border-top: 1px solid var(--border);
}
.vars-popup .vars-row {
  cursor: pointer;
  transition: background 100ms ease;
  border-radius: 4px;
}
.vars-popup .vars-row:hover {
  background: var(--bg-soft);
}
.vars-popup .vars-name {
  color: var(--accent);
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
}
.vars-popup .vars-value {
  color: var(--result);
  font-weight: 600;
  flex-shrink: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}
.vars-popup .vars-line {
  color: var(--text-faint);
  font-size: 10px;
  margin-left: 6px;
}

/* ---------- hover tooltip (custom, used for reserved-error rows) ---------- */
.hover-tooltip {
  position: fixed;
  z-index: 50;
  max-width: 320px;
  padding: 6px 10px;
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  box-shadow: var(--shadow);
  font-family: var(--font-ui);
  font-size: 11px;
  line-height: 1.45;
  white-space: pre-line;   /* honor \n in multi-line tips (e.g. the unknown-variable hint) while still wrapping */
  pointer-events: none;
}
.hover-tooltip[hidden] { display: none; }
/* line-number tooltip: title line + the math/text type words highlighted */
.hover-tooltip .tip-title { font-weight: 600; margin-bottom: 3px; }
.hover-tooltip .tip-type-math { color: var(--result); font-weight: 700; }
.hover-tooltip .tip-type-text { color: var(--accent); font-weight: 700; }

/* markdown */
.md-h1 { color: var(--header); font-weight: 700; }
.md-h2 { color: var(--header); font-weight: 700; }
.md-h3 { color: var(--header); font-weight: 600; }
.md-h-marker { color: var(--text-faint); font-weight: 400; }
.md-bullet { color: var(--bullet); font-weight: 700; }
.md-bold { font-weight: 700; color: var(--text); }
.md-italic { font-style: italic; color: var(--text); }
.md-underline { text-decoration: underline; text-underline-offset: 2px; }
.md-marker { color: var(--text-faint); }
.md-task-box {
  display: inline-flex;
  width: 3ch;
  min-width: 3ch;
  align-items: center;
  justify-content: center;
  vertical-align: -2px;
}
.md-task-square {
  position: relative;
  width: 13px;
  height: 13px;
  border: 1.5px solid var(--text-faint);
  border-radius: 3px;
  background: var(--bg-elev);
}
.md-task-box.checked .md-task-square {
  border-color: var(--accent);
  background: var(--accent);
}
.md-task-box.checked .md-task-square::after {
  content: "";
  position: absolute;
  left: 3px;
  top: 1px;
  width: 4px;
  height: 7px;
  border: solid #fff;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}
.md-task-text.checked {
  color: var(--text-soft);
  text-decoration: line-through;
  text-decoration-thickness: 1px;
}

/* ---------- status bar ---------- */
.status-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 24px;
  padding: 0 10px;
  background: var(--bg-elev);
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-soft);
  border-bottom-left-radius: var(--radius);
  border-bottom-right-radius: var(--radius);
}
.status-msg.ok { color: var(--result); }
.status-msg.err { color: var(--error); }
.hint { color: var(--text-faint); }

.zoom-indicator {
  font-variant-numeric: tabular-nums;
  font-size: 11px;
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--bg-soft);
  color: var(--text-soft);
  transition: opacity 200ms ease;
}
.zoom-indicator[hidden] { display: none; }
.zoom-indicator.flash {
  background: var(--accent-soft);
  color: var(--accent);
}

/* ---------- scrollbars (subtle) ---------- */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--scrollbar); border-radius: 8px; }
::-webkit-scrollbar-thumb:hover { background: var(--scrollbar-hover); }
::-webkit-scrollbar-track { background: transparent; }
