/* Themes are CSS-var palettes. :root = Dark (the default, and the no-JS / pre-paint fallback);
   [data-theme] blocks override. `kind`/`surface-2`/`line` are node-detail tokens.
   `--tone-*` are theme-aware semantic text colors (error/info/ok/warn) for `component`/`markdown`
   text on a themed surface — tuned to clear WCAG AA on each theme (a fixed hex can't, since the
   surface flips light↔dark). Flow-node tones stay hex (they sit on the canvas, not a surface). */
:root {
  --bg: #18181b; --fg: #e4e4e7; --muted: #a1a1aa; --kind: #8e8e98;
  --border: #3f3f46; --surface: #27272a; --surface-2: #1f1f23; --active: #3f3f46; --line: #52525b;
  --accent: #fbbf24;   /* "updated" indicator */
  --live: #22c55e;     /* connection healthy */
  --focus: #60a5fa;    /* keyboard focus ring */
  --tone-error: #f87171; --tone-info: #60a5fa; --tone-ok: #4ade80; --tone-warn: #fbbf24;
  /* Theme-scoped fonts: default to the app's system stacks; a theme may override (e.g.
     google-cloud → Roboto/Google-Sans-ish). The chrome/body read var(--font); code reads
     var(--font-mono). No web-font is loaded (the viewer stays self-contained/offline), so a
     theme-specific stack falls back to the closest installed family. */
  --font: ui-monospace, SFMono-Regular, Menlo, monospace;
  --font-sans: ui-sans-serif, system-ui, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, monospace;
  --radius: 8px;
  --tap: 44px;         /* min touch target (iPad) */
  color-scheme: dark;
}
:root[data-theme="midnight"] {  /* deep navy dark, cyan-led (issue #107 §5) */
  --bg: #0b1120; --fg: #e2e8f0; --muted: #94a3b8; --kind: #7c8aa3;
  --border: #243049; --surface: #162033; --surface-2: #0f1830; --active: #1d2b45; --line: #38486a;
  --accent: #38bdf8; --live: #22c55e; --focus: #38bdf8; color-scheme: dark;
  --tone-error: #f87171; --tone-info: #38bdf8; --tone-ok: #4ade80; --tone-warn: #fbbf24;
}
:root[data-theme="oled"] {  /* OLED true black, violet-led (issue #107 §5) */
  --bg: #000000; --fg: #f4f4f5; --muted: #a1a1aa; --kind: #8e8e98;
  --border: #1c1c1f; --surface: #0d0d0f; --surface-2: #000000; --active: #18181b; --line: #2a2a2e;
  --accent: #a78bfa; --live: #34d399; --focus: #818cf8; color-scheme: dark;
  --tone-error: #f87171; --tone-info: #818cf8; --tone-ok: #34d399; --tone-warn: #fbbf24;
}
:root[data-theme="light"] {
  --bg: #ffffff; --fg: #18181b; --muted: #52525b; --kind: #71717a;
  --border: #d4d4d8; --surface: #f4f4f5; --surface-2: #e8e8ea; --active: #e4e4e7; --line: #a1a1aa;
  --accent: #b45309; --live: #16a34a; --focus: #2563eb; color-scheme: light;
  --tone-error: #dc2626; --tone-info: #2563eb; --tone-ok: #15803d; --tone-warn: #a16207;
}
:root[data-theme="paper"] {  /* warm cream light, terracotta accent (issue #107 §5) */
  --bg: #f6f1e7; --fg: #3a342c; --muted: #6b5f4f; --kind: #837663;
  --border: #ddd2bd; --surface: #efe7d6; --surface-2: #e7dcc6; --active: #e7dcc6; --line: #c4b699;
  --accent: #c2410c; --live: #15803d; --focus: #9a3412; color-scheme: light;
  --tone-error: #b91c1c; --tone-info: #9a3412; --tone-ok: #15803d; --tone-warn: #92400e;
}
/* Google Cloud Console look (light): GM2 palette — Google Blue primary on white/grey surfaces,
   Material neutral greys for borders/text, Google semantic green/red/amber. Self-contained block
   so merges stay clean. Sources cited in PR: GM2 tokens #1a73e8 (Blue, primary action),
   #4285f4 (Blue 500), #5f6368 (Grey 700, secondary text), #dadce0 (Grey 300, borders),
   #f1f3f4 (Grey 100, surfaces), #202124 (Grey 900, text), #1e8e3e (Green 600),
   #d93025 (Red 600), #f9ab00 (Yellow 600). Fonts: Google Sans/Roboto with system fallbacks
   (no web-font loaded). `--tone-*` darkened to clear WCAG AA (≥4.5:1) on the #f1f3f4 surface. */
:root[data-theme="google-cloud"] {
  --bg: #ffffff; --fg: #202124; --muted: #5f6368; --kind: #5f6368;
  --border: #dadce0; --surface: #f1f3f4; --surface-2: #e8eaed; --active: #e8f0fe; --line: #bdc1c6;
  --accent: #1a73e8;   /* Google Blue — "updated" indicator */
  --live: #1e8e3e;     /* Google Green — connection healthy */
  --focus: #1a73e8;    /* Google Blue — keyboard focus ring */
  color-scheme: light;
  --tone-error: #c5221f; --tone-info: #1967d2; --tone-ok: #188038; --tone-warn: #8f6000;
  --font: 'Google Sans', 'Roboto', 'Helvetica Neue', Arial, sans-serif;
  --font-sans: 'Google Sans', 'Roboto', 'Helvetica Neue', Arial, sans-serif;
  --font-mono: 'Roboto Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
}
/* System: follow the OS. Dark is the :root default; apply Light when the OS prefers light. */
@media (prefers-color-scheme: light) {
  :root[data-theme="system"], :root:not([data-theme]) {
    --bg: #ffffff; --fg: #18181b; --muted: #52525b; --kind: #71717a;
    --border: #d4d4d8; --surface: #f4f4f5; --surface-2: #e8e8ea; --active: #e4e4e7; --line: #a1a1aa;
    --accent: #b45309; --live: #16a34a; --focus: #2563eb; color-scheme: light;
    --tone-error: #dc2626; --tone-info: #2563eb; --tone-ok: #15803d; --tone-warn: #a16207;
  }
}
* { box-sizing: border-box; }
/* Mobile tap polish: kill the grey iOS tap-flash on custom controls and the legacy double-tap delay. */
button, a, select, input, textarea, [role="button"], .scope, .tc-console-chip {
  touch-action: manipulation; -webkit-tap-highlight-color: transparent;
}
body {
  margin: 0; display: flex;
  height: 100vh; height: 100dvh;   /* dvh avoids iOS Safari URL-bar clipping */
  font-family: var(--font);
  background: var(--bg); color: var(--fg);
  overflow-x: hidden;   /* backstop: the app should never scroll horizontally on mobile */
  /* iOS notch / home-indicator safe areas (viewport-fit=cover). Insets are 0 on devices without them,
     so this is a no-op on Android/desktop and only pads in-flow content (header, sidebar, stage) on
     notched iPhones. Fixed overlays (#statuses, #tc-console) carry their own env() offsets below. */
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

#sidebar { width: 220px; flex-shrink: 0; border-right: 1px solid var(--border); padding: 8px; overflow-y: auto; }
#sidebar h1 { font-size: 12px; text-transform: uppercase; color: var(--muted); margin: 4px 6px 8px; }
#scopes { display: flex; flex-direction: column; gap: 4px; }

/* Scope rows are real <button>s: keyboard-focusable, 44px touch targets. */
.scope {
  display: flex; align-items: center; justify-content: space-between; gap: 6px;
  width: 100%; min-height: var(--tap); padding: 7px 10px;
  font: inherit; text-align: left; color: var(--fg);
  background: transparent; border: 1px solid transparent; border-radius: 6px;
  cursor: pointer;
  transition: background .15s ease, border-color .15s ease;
}
.scope:hover { background: var(--surface); }
.scope:active { background: var(--active); }
.scope.active { background: var(--active); border-color: var(--border); }
.scope:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
/* two-line label: small dimmed project + the distinguishing agent name (wraps up to 2 lines) */
.scope-label { display: flex; flex-direction: column; gap: 1px; min-width: 0; overflow: hidden; }
.scope-proj { font-size: 10px; line-height: 1.2; color: var(--muted); text-transform: uppercase; letter-spacing: .3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.scope-agent { font-size: 13px; line-height: 1.25; word-break: break-word; overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 2; line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.scope .dot { color: var(--accent); flex-shrink: 0; align-self: flex-start; margin-top: 2px; }
/* A scope and its token-gated × clear button share a row. */
.scope-row { display: flex; align-items: stretch; gap: 2px; }
.scope-row .scope { flex: 1; width: auto; min-width: 0; }
.scope-clear {
  flex-shrink: 0; width: 40px; min-height: var(--tap);
  display: inline-flex; align-items: center; justify-content: center;
  background: none; border: 1px solid transparent; border-radius: 6px;
  color: var(--muted); font: inherit; font-size: 18px; line-height: 1; cursor: pointer;
  opacity: .45; transition: opacity .15s ease, color .15s ease, background .15s ease;
}
.scope-clear:hover { opacity: 1; color: #f87171; background: var(--surface); }
.scope-clear:focus-visible { opacity: 1; outline: 2px solid var(--focus); outline-offset: 2px; }
/* Clear-all: a quiet, destructive-on-hover footer action below the scope list. */
.scope-clearall {
  margin-top: 8px; width: 100%; min-height: var(--tap); padding: 7px 10px;
  background: transparent; border: 1px solid var(--border); border-radius: 6px;
  color: var(--muted); font: inherit; font-size: 12px; cursor: pointer;
  transition: color .15s ease, border-color .15s ease, background .15s ease;
}
.scope-clearall:hover { color: #f87171; border-color: #f8717159; background: var(--surface); }
.scope-clearall:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
/* "Welcome" home row — return to the intro gallery; set apart from the scope list */
.scope-home { font-weight: 600; margin-bottom: 6px; border-bottom: 1px solid var(--border); border-radius: 6px; }
.empty-hint { color: var(--muted); font-size: 12px; padding: 6px 8px; margin: 0; }

main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
header { display: flex; gap: 16px; align-items: center; padding: 8px 12px; border-bottom: 1px solid var(--border); }
/* ≥44px hit area (iPad tap target); the svg stays ~16px, centered. */
#sidebar-toggle { display: inline-flex; align-items: center; justify-content: center; min-width: var(--tap); min-height: var(--tap); padding: 0; background: none; border: 1px solid transparent; border-radius: 6px; color: var(--muted); cursor: pointer; flex-shrink: 0; }
#sidebar-toggle:hover { background: var(--surface); color: var(--fg); }
#sidebar-toggle:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.sidebar-collapsed #sidebar { display: none; }
#title { font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* Unified header toolbar. Follow / Console / History are toggle buttons (aria-pressed = on);
   Share / Export are actions. All share one button language so the bar reads as one tidy row;
   thin separators group toggles | actions | theme. Heights match the sidebar toggle + theme select
   (var(--tap)) so the whole header sits on one baseline. */
/* nowrap: the toolbar stays one row and the title ellipsizes to make room (phones re-enable wrap
   below, dropping the cluster to its own row). 8px gap keeps adjacent touch targets from crowding. */
.header-actions { display: flex; align-items: center; gap: 8px; margin-left: auto; flex-wrap: nowrap; justify-content: flex-end; }
.tc-tool {
  position: relative; display: inline-flex; align-items: center; gap: 6px; flex: none;
  min-height: var(--tap); padding: 0 10px;
  font: inherit; font-size: 13px; line-height: 1; color: var(--fg);
  background: var(--surface); border: 1px solid var(--border); border-radius: 7px; cursor: pointer;
  transition: background .15s ease, border-color .15s ease, color .15s ease;
}
.tc-tool[hidden] { display: none; }
.tc-tool svg { width: 16px; height: 16px; flex: none; }
.tc-tool:hover { border-color: var(--muted); background: var(--active); }
.tc-tool:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
/* Pressed (panel open / mode on) — accent border + text + a tinted fill, so the on-state reads at a
   glance (not by color alone) and clearly beats the neutral hover. Placed after :hover so it holds
   while hovered; the color-mix fill is progressive (older engines keep the --active fallback). */
.tc-tool[aria-pressed="true"] { color: var(--focus); border-color: var(--focus); background: var(--active); }
@supports (background: color-mix(in srgb, red, blue)) {
  .tc-tool[aria-pressed="true"] { background: color-mix(in srgb, var(--focus) 15%, var(--surface)); }
}
/* Experimental tools (Console/History) carry data-beta — the word "beta" shows in the tooltip and the
   opened panel's header. We deliberately DON'T put a corner dot here: a colored dot top-right reads as
   an unread-notification badge (and on some themes matches the on-state accent), which misleads. */
.tc-tool-sep { flex: none; align-self: center; width: 1px; height: 22px; background: var(--border); margin: 0 2px; }
.export-menu { position: fixed; z-index: 1000; display: flex; flex-direction: column; gap: 4px; padding: 6px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 8px 28px rgba(0,0,0,.35); }
.export-menu .tc-btn { justify-content: flex-start; }
.share-popover { position: fixed; z-index: 1000; width: min(360px, calc(100vw - 16px)); display: flex; flex-direction: column; gap: 8px; padding: 12px; background: var(--surface); color: var(--fg); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 8px 28px rgba(0,0,0,.35); }
.share-pop-note { margin: 0; font-size: 12.5px; line-height: 1.45; color: var(--muted); }
.share-pop-row { display: flex; gap: 6px; }
.share-pop-url { flex: 1; min-width: 0; padding: 6px 8px; font: inherit; font-size: 12px; color: var(--fg); background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; }
.share-pop-revoke { align-self: flex-start; }
/* Connection status sits with the title (left), not in the action cluster. */
#status { color: var(--muted); font-size: 12px; flex: none; white-space: nowrap; }
/* Share view (/s/<token>): one board, read-only — hide all chrome that reveals other boards/controls.
   Export + Theme stay; the toggles, sharing, and group separators go. */
body.share-mode #sidebar, body.share-mode #sidebar-toggle, body.share-mode #share,
body.share-mode #follow, body.share-mode #console-toggle, body.share-mode #history-toggle,
body.share-mode .tc-tool-sep { display: none !important; }
/* PDF export = the browser print dialog: print only the board, drop all chrome — including the
   bottom-left history/console panels and the history preview banner (fixed/body-level overlays the
   print would otherwise capture on top of the board). */
@media print {
  #sidebar, header, #statuses, #scope-intro, #tc-console, #tc-history, #history-banner,
  .export-menu, .share-popover, .tc-modal-overlay, .tc-ct-toggle { display: none !important; }
  body { display: block; height: auto; padding: 0; }
  main, #stage { display: block; overflow: visible; height: auto; }
  #stage { padding: 0; }
  #diagram { transform: none !important; } /* print at full size, not the on-screen fit-scale */
}
.live-dot { color: var(--live); display: inline-block; animation: pulse 2s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .35; } }

#theme {
  font: inherit; font-size: 12px; color: var(--fg); background: var(--surface);
  border: 1px solid var(--border); border-radius: 6px; padding: 2px 6px; min-height: var(--tap); cursor: pointer;
}
#theme:hover { border-color: var(--muted); }
#theme:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

#stage { flex: 1; overflow: auto; padding: 12px; }
/* Demo-only intro bar: a one-line description of what each showcase scope is for, shown between
   the header and the scrollable stage. Populated by viewer.ts for the /w/demo/ scopes only. */
#scope-intro {
  display: flex; gap: 8px; align-items: baseline;
  padding: 10px 14px; border-bottom: 1px solid var(--border);
  background: var(--surface-2); font-size: 13px; line-height: 1.5;
}
#scope-intro[hidden] { display: none; } /* restore hiding (the id rule above beats UA [hidden]) */
#scope-intro .scope-intro-tag {
  flex: none; font-size: 11px; font-weight: 700; letter-spacing: .04em;
  text-transform: uppercase; color: var(--accent);
}
#scope-intro .scope-intro-text { min-width: 0; color: var(--fg); }
#diagram { margin: 0; transform-origin: top left; transition: transform .15s ease-out; }
.diagram-pre { margin: 0; line-height: 1.1; }

/* Empty / waiting state — never a blank canvas. */
.empty-state { color: var(--muted); padding: 24px 8px; max-width: 60ch; }
.empty-title { font-size: 16px; color: var(--fg); margin: 0 0 6px; }
.empty-sub { font-size: 13px; margin: 0; line-height: 1.6; }
.empty-state code { background: var(--surface); padding: 1px 5px; border-radius: 4px; }

/* Shown while a lazy renderer chunk downloads */
.loading { color: var(--muted); padding: 16px 8px; font-size: 13px; animation: blink 1.2s ease-in-out infinite; }
@keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: .45; } }

.panes { display: flex; gap: 16px; align-items: stretch; }
/* columns wrap fluidly: a pane keeps a comfortable min width and drops to the next row when the
   viewport (or aspect ratio) can't fit them side by side, instead of squishing indefinitely. */
.panes-columns { flex-direction: row; flex-wrap: wrap; }
.panes-columns > .pane { flex: 1 1 340px; min-width: 0; }
.panes-rows { flex-direction: column; }
/* grid auto-fits as many columns as fit at a readable min width, reflowing rows fluidly as the
   screen/aspect-ratio changes (2×2 on a wide screen → 1 column on a phone, no squishing). */
.panes-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 480px), 1fr)); }
.panes-rows > .pane { width: 100%; }
.panes-grid > .pane { min-width: 0; }
.pane { border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; min-width: 0; }
.pane-bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 6px; padding-bottom: 4px; border-bottom: 1px solid var(--border); }
.pane-title { color: var(--muted); font-size: 12px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* ≥44px hit area (iPad tap target); the svg stays ~14px, centered, visually subtle. */
.pane-max { flex-shrink: 0; display: inline-flex; align-items: center; justify-content: center; min-width: var(--tap); min-height: var(--tap); margin: -8px -8px -8px 0; background: none; border: 0; color: var(--muted); cursor: pointer; opacity: .5; padding: 0; border-radius: 6px; line-height: 0; }
.pane-max:hover { opacity: 1; background: var(--surface); color: var(--fg); }
.pane-max:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; opacity: 1; }
.pane--max { position: fixed; inset: 8px; z-index: 1000; margin: 0; background: var(--bg); overflow: auto; box-shadow: 0 0 0 100vmax rgba(0, 0, 0, 0.6); }
.pane--max .tc-flow-wrap { height: calc(100dvh - 64px) !important; }
/* A maximized pane is position:fixed; drop any ASCII fit transform on #diagram so the
   overlay anchors to the viewport (a transformed ancestor would capture fixed positioning). */
body:has(.pane--max) #diagram { transform: none !important; }

/* flow sizing: default height when the spec sets none; a STANDALONE flow (direct child of
   #diagram) fills the stage instead of sitting short with empty space below. An inline
   spec.height (paned/explicit) overrides both. */
.tc-flow-wrap { height: min(70vh, 600px); }
#diagram > .tc-flow-wrap { height: calc(100dvh - 92px); }

/* flow legend overlay */
.tc-legend { background: var(--surface); border: 1px solid var(--border); box-shadow: 0 2px 8px rgba(0, 0, 0, 0.18); border-radius: 6px; padding: 7px 10px; font-size: 11px; color: var(--fg); display: flex; flex-direction: column; gap: 3px; font-family: var(--font-sans); }
.tc-legend-item { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.tc-legend-swatch { width: 12px; height: 12px; border-radius: 3px; border: 2px solid; box-sizing: border-box; flex-shrink: 0; }

/* Transient status overlays — toasts / progress / loaders (pushed via the status API) */
#statuses { position: fixed; top: calc(12px + env(safe-area-inset-top)); right: calc(12px + env(safe-area-inset-right)); z-index: 1200; display: flex; flex-direction: column; gap: 8px; width: min(340px, calc(100vw - 24px)); pointer-events: none; }
.tc-status { pointer-events: auto; display: flex; align-items: flex-start; gap: 10px; padding: 10px 12px; background: var(--surface); border: 1px solid var(--border); border-left: 3px solid var(--muted); border-radius: 8px; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35); color: var(--fg); font-family: var(--font-sans); font-size: 13px; opacity: 1; transform: translateX(0); transition: opacity .2s ease, transform .2s ease; }
.tc-status--out { opacity: 0; transform: translateX(12px); }
.tc-status-icon { flex-shrink: 0; display: flex; width: 16px; height: 16px; margin-top: 1px; color: var(--muted); }
.tc-status-icon svg { width: 16px; height: 16px; }
.tc-status-body { flex: 1; min-width: 0; }
.tc-status-title { font-weight: 600; }
.tc-status-msg { color: var(--muted); line-height: 1.4; overflow-wrap: anywhere; }
.tc-status-scope { font-size: 11px; color: var(--muted); margin-top: 3px; opacity: 0.8; }
.tc-status-close { flex-shrink: 0; appearance: none; background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 18px; line-height: 1; width: 24px; height: 24px; border-radius: 6px; padding: 0; }
.tc-status-close:hover { background: var(--active); color: var(--fg); }
.tc-status-close:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }
/* level accents (icon color + left border) */
.tc-status--success { border-left-color: #22c55e; } .tc-status--success .tc-status-icon { color: #22c55e; }
.tc-status--info { border-left-color: #60a5fa; } .tc-status--info .tc-status-icon { color: #60a5fa; }
.tc-status--warn { border-left-color: #fbbf24; } .tc-status--warn .tc-status-icon { color: #fbbf24; }
.tc-status--error { border-left-color: #ef4444; } .tc-status--error .tc-status-icon { color: #ef4444; }
/* progress bar */
.tc-progress-row { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.tc-progress { flex: 1; height: 6px; background: var(--bg); border-radius: 999px; overflow: hidden; }
.tc-progress-bar { height: 100%; background: #60a5fa; border-radius: 999px; transition: width .3s ease; }
.tc-status--success .tc-progress-bar { background: #22c55e; }
.tc-status--error .tc-progress-bar { background: #ef4444; }
.tc-progress-pct { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; min-width: 30px; text-align: right; }
/* loader spinner */
.tc-spinner { display: inline-block; width: 14px; height: 14px; border: 2px solid var(--border); border-top-color: var(--focus); border-radius: 50%; animation: tc-spin .7s linear infinite; }
@keyframes tc-spin { to { transform: rotate(360deg); } }
/* Brief "what changed" highlight on a patched element. In-place patches update smoothly (no
   flicker), which can hide the change from a glancing viewer — this fades a theme-accent ring
   around just the patched node/stat. The global prefers-reduced-motion rule disables it. */
.tc-changed { animation: tc-changed 1s ease-out; }
@keyframes tc-changed { 0% { box-shadow: 0 0 0 3px var(--accent); } 100% { box-shadow: 0 0 0 3px transparent; } }
/* phone: full-width status strip */
@media (max-width: 640px) { #statuses { left: calc(12px + env(safe-area-inset-left)); right: calc(12px + env(safe-area-inset-right)); width: auto; } }

/* `component` content stays inside its box — never spills out the right of its pane:
   long words/text wrap, code/pre wraps, and a wide table scrolls within its own frame.
   min-width:0 on descendants lets flex children (e.g. nowrap Groups, Timeline items) shrink
   and wrap instead of pushing the container wider. */
.tc-component { max-width: 100%; min-width: 0; overflow-wrap: anywhere; }
.tc-component * { min-width: 0; }
.tc-component pre, .tc-component code { white-space: pre-wrap; overflow-wrap: anywhere; word-break: break-word; }
.tc-component :where(table) { max-width: 100%; }
.tc-component [class*="Table-root"], .tc-component [class*="ScrollArea"] { overflow-x: auto; }

/* Interactive checklist: the Mantine Checkbox already follows the theme (it reads --mantine-*
   vars under the MantineProvider). A roomier hit target makes items comfortable to tap on a
   touchscreen (iPad / phone), where this surface is most useful. */
.tc-checklist [class*="Checkbox-body"] { align-items: center; }
.tc-checklist [class*="Checkbox-labelWrapper"] { padding-inline-start: 4px; }
/* Editable-checklist 44px tap rows are scoped to phones (see the @media block below) — on desktop the
   compact rows are fine for a mouse and keep dense lists tight. Read-only lists stay compact always. */

/* Video thumbnail card (Video component): a 16:9 poster with a centered play button; clicking
   opens the video or (embed:true) swaps to a youtube-nocookie iframe. Theme-aware via vars. */
.tc-video { width: 100%; }
.tc-video-frame {
  position: relative; width: 100%; aspect-ratio: 16 / 9; overflow: hidden;
  border-radius: 8px; background: var(--surface-2); border: 1px solid var(--border);
}
.tc-video-thumb { width: 100%; height: 100%; object-fit: cover; display: block; }
.tc-video-noimg { background: linear-gradient(135deg, var(--surface), var(--surface-2)); }
.tc-video-iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; }
.tc-video-play {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: 64px; height: 44px; border-radius: 12px; background: rgba(0, 0, 0, 0.65);
  display: flex; align-items: center; justify-content: center;
  transition: background .15s ease, transform .15s ease; pointer-events: none;
}
.tc-video-play::after {
  content: ""; width: 0; height: 0; margin-left: 3px;
  border-style: solid; border-width: 9px 0 9px 15px; border-color: transparent transparent transparent #fff;
}
.tc-video:hover .tc-video-play { background: #ef4444; transform: translate(-50%, -50%) scale(1.06); }
.tc-video:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 8px; }
.tc-video-dur {
  position: absolute; right: 8px; bottom: 8px; padding: 1px 5px; border-radius: 4px;
  background: rgba(0, 0, 0, 0.8); color: #fff; font-size: 12px; font-variant-numeric: tabular-nums;
}
.tc-video-meta { padding: 8px 2px 0; }
.tc-video-title { font-weight: 600; font-size: 14px; color: var(--fg); line-height: 1.3; }
.tc-video-channel { font-size: 12px; color: var(--muted); margin-top: 2px; }

/* Markdown content (markdown type) */
.markdown-body { color: var(--fg); line-height: 1.6; max-width: 80ch; font-family: var(--font-sans); }
.markdown-body > :first-child { margin-top: 0; }
.markdown-body h1, .markdown-body h2, .markdown-body h3, .markdown-body h4 { line-height: 1.25; margin: 1.2em 0 0.5em; }
.markdown-body h1 { font-size: 1.7em; border-bottom: 1px solid var(--border); padding-bottom: .2em; }
.markdown-body h2 { font-size: 1.4em; border-bottom: 1px solid var(--border); padding-bottom: .2em; }
.markdown-body h3 { font-size: 1.15em; }
.markdown-body p, .markdown-body ul, .markdown-body ol, .markdown-body blockquote, .markdown-body table { margin: 0 0 1em; }
.markdown-body a { color: var(--focus); }
.markdown-body code { background: var(--surface); padding: .15em .4em; border-radius: 4px; font-family: var(--font-mono); font-size: .9em; }
/* Wrap long code lines instead of clipping them. The viewer is a glanceable display, so a
   horizontal scrollbar inside a pane is invisible/unreachable and the line tail (often a
   trailing comment) silently disappears. pre-wrap wraps at whitespace while preserving
   indentation; overflow:auto stays as the fallback for a single unbreakable long token. This
   mirrors the `.tc-component pre` behavior so both code paths read consistently. */
.markdown-body pre { background: var(--surface); padding: 12px 14px; border-radius: 8px; overflow: auto; white-space: pre-wrap; }
.markdown-body pre code { background: none; padding: 0; }
.markdown-body blockquote { border-left: 3px solid var(--border); margin-left: 0; padding: 0 0 0 12px; color: var(--muted); }
.markdown-body table { border-collapse: collapse; }
.markdown-body th, .markdown-body td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.markdown-body th { background: var(--surface); }
.markdown-body img { max-width: 100%; }
.markdown-body hr { border: 0; border-top: 1px solid var(--border); margin: 1.5em 0; }

/* --- Confirm modal (theme-aware; replaces browser confirm/prompt) ------------------------- */
.tc-modal-overlay {
  position: fixed; inset: 0; z-index: 2000;
  display: flex; align-items: center; justify-content: center;
  padding: 16px; background: rgba(0, 0, 0, 0.5);
  animation: tc-modal-fade 0.12s ease-out;
}
.tc-modal {
  width: min(92vw, 380px); box-sizing: border-box;
  background: var(--surface); color: var(--fg);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 18px 18px 14px; box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
  font-family: ui-sans-serif, system-ui, sans-serif;
  animation: tc-modal-pop 0.12s ease-out;
}
.tc-modal-title { font-size: 15px; font-weight: 600; margin-bottom: 6px; }
.tc-modal-body { font-size: 13px; color: var(--muted); line-height: 1.5; margin-bottom: 16px; }
.tc-modal-actions { display: flex; justify-content: flex-end; gap: 8px; }
.tc-btn {
  font: inherit; font-size: 13px; line-height: 1; padding: 0 14px; min-height: 34px;
  border: 1px solid transparent; border-radius: 7px; cursor: pointer;
}
.tc-btn-ghost { background: transparent; color: var(--fg); border-color: var(--border); }
.tc-btn-ghost:hover { background: var(--active); }
.tc-btn-primary { background: var(--focus); color: #fff; }
.tc-btn-primary:hover { filter: brightness(1.08); }
.tc-btn-danger { background: #ef4444; color: #fff; }
.tc-btn-danger:hover { background: #dc2626; }
.tc-btn:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
@keyframes tc-modal-fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes tc-modal-pop { from { opacity: 0; transform: translateY(6px) scale(0.98); } to { opacity: 1; transform: none; } }

/* --- Experimental agent↔human console (issue #108; off by default) ------------------------- */
.beta-tag {
  font-size: 9px; line-height: 1; text-transform: uppercase; letter-spacing: .4px;
  padding: 2px 4px; border-radius: 4px; background: var(--surface); color: var(--muted);
  border: 1px solid var(--border);
}
#tc-console {
  position: fixed; right: calc(16px + env(safe-area-inset-right)); bottom: calc(16px + env(safe-area-inset-bottom)); z-index: 1100;
  width: min(340px, calc(100vw - 32px)); max-height: min(60vh, 520px);
  display: none; flex-direction: column;
  background: var(--surface); color: var(--fg);
  border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
  font-family: ui-sans-serif, system-ui, sans-serif; font-size: 13px;
  animation: tc-modal-pop 0.14s ease-out;
}
body.console-open #tc-console { display: flex; }

/* Board history (rewind & inspect) — a toggleable panel (bottom-left, mirrors the console) + a
   preview banner above the stage while viewing a past version. */
#tc-history {
  position: fixed; left: calc(16px + env(safe-area-inset-left)); bottom: calc(16px + env(safe-area-inset-bottom)); z-index: 1100;
  width: min(320px, calc(100vw - 32px)); max-height: min(60vh, 520px);
  display: none; flex-direction: column;
  background: var(--surface); color: var(--fg);
  border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
  font-family: ui-sans-serif, system-ui, sans-serif; font-size: 13px;
  animation: tc-modal-pop 0.14s ease-out;
}
body.history-open #tc-history { display: flex; }
.tc-history-head { display: flex; align-items: center; gap: 8px; padding: 9px 12px; border-bottom: 1px solid var(--border); }
.tc-history-title { font-weight: 600; }
.tc-history-close { margin-left: auto; background: none; border: none; color: var(--muted); font-size: 18px; line-height: 1; cursor: pointer; }
.tc-history-list { overflow-y: auto; padding: 6px; display: flex; flex-direction: column; gap: 2px; }
.tc-history-row { display: flex; gap: 8px; align-items: baseline; width: 100%; text-align: left; padding: 7px 9px; border: 1px solid transparent; border-radius: 7px; background: transparent; color: var(--fg); font: inherit; cursor: pointer; }
.tc-history-row:hover { background: var(--active); }
.tc-history-row.active { border-color: var(--accent); }
.tc-history-row .k { flex: none; font-size: 11px; text-transform: uppercase; letter-spacing: .03em; color: var(--accent); width: 58px; }
.tc-history-row.live .k { color: var(--live); }
.tc-history-row .s { min-width: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tc-history-row .t { flex: none; color: var(--muted); font-size: 11px; }
.tc-history-empty { padding: 14px; color: var(--muted); }
#history-banner { display: flex; align-items: center; gap: 10px; padding: 8px 14px; border-bottom: 1px solid var(--border); background: var(--surface-2); font-size: 13px; }
#history-banner[hidden] { display: none; }
#history-banner .hb-text { color: var(--fg); }
#history-banner .hb-actions { margin-left: auto; display: flex; gap: 6px; }
.tc-console-head {
  display: flex; align-items: center; gap: 8px; padding: 9px 12px;
  border-bottom: 1px solid var(--border);
}
.tc-console-title { font-weight: 600; display: inline-flex; align-items: center; gap: 6px; }
.tc-console-scope { flex: 1; min-width: 0; font-size: 11px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tc-console-close {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: var(--tap); min-height: var(--tap); margin: -6px -6px -6px 0; /* 44px hit area without bloating the header bar */
  background: none; border: 0; color: var(--muted); font-size: 18px; line-height: 1;
  cursor: pointer; padding: 0; border-radius: 6px;
}
.tc-console-close:hover { color: var(--fg); background: var(--active); }
.tc-console-chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px 12px; border-bottom: 1px solid var(--border); }
.tc-console-chips--empty { display: none; }
.tc-console-chip {
  font: inherit; font-size: 12px; line-height: 1; padding: 6px 10px;
  background: var(--surface-2); color: var(--fg); border: 1px solid var(--border);
  border-radius: 999px; cursor: pointer; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.tc-console-chip:hover { border-color: var(--focus); background: var(--active); }
.tc-console-chip:disabled { opacity: 0.5; cursor: default; }
.tc-console-chip:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.tc-console-log {
  flex: 1; min-height: 80px; overflow-y: auto; padding: 10px 12px;
  display: flex; flex-direction: column; gap: 6px;
}
.tc-console-empty { color: var(--muted); font-size: 12px; line-height: 1.5; }
.tc-console-row {
  padding: 6px 9px; border-radius: 8px; max-width: 90%; word-break: break-word; white-space: pre-wrap;
  background: var(--surface-2); border: 1px solid var(--border);
}
.tc-console-row--sent { align-self: flex-end; background: var(--focus); color: #fff; border-color: transparent; }
.tc-console-row--message { align-self: flex-end; background: var(--focus); color: #fff; border-color: transparent; }
.tc-console-row--action { align-self: flex-start; color: var(--muted); font-size: 12px; }
/* Delivery status under a sent message (sending / sent / failed) — the feedback that the message
   went through and what you're now waiting on. */
.tc-console-status { display: block; margin-top: 3px; font-size: 10.5px; line-height: 1.3; opacity: 0.9; }
.tc-console-row--sent .tc-console-status { color: rgba(255, 255, 255, 0.85); }
.tc-console-status[data-state="failed"] { color: #fecaca; cursor: pointer; text-decoration: underline; }
/* Read receipt: the agent has read this message (the ✓ in the copy) — brighten it so "read" reads
   distinctly from the "waiting/unread" state on the blue sent bubble. */
.tc-console-status[data-state="read"] { opacity: 1; }
.tc-console-row--sent .tc-console-status[data-state="read"] { color: #c7f9d9; }
/* Header summary: are the human's messages still sitting unread in the agent's inbox, or all read? */
.tc-console-read { padding: 2px 12px 0; font-size: 11px; line-height: 1.5; font-weight: 500; }
.tc-console-read:empty { display: none; padding: 0; }
.tc-console-read[data-state="unread"] { color: #f59e0b; }
.tc-console-read[data-state="read"] { color: #22c55e; }
.tc-console-input { display: flex; gap: 8px; align-items: flex-end; padding: 10px 12px; border-top: 1px solid var(--border); }
.tc-console-input textarea {
  flex: 1; resize: none; font: inherit; font-size: 16px; line-height: 1.4; /* 16px: iOS auto-zooms inputs <16px on focus */
  background: var(--bg); color: var(--fg); border: 1px solid var(--border);
  border-radius: 8px; padding: 7px 9px; max-height: 120px;
}
.tc-console-input textarea:focus { outline: none; border-color: var(--focus); }
.tc-console-input textarea:disabled { opacity: 0.6; cursor: not-allowed; } /* no scope selected — composer bound to current scope */
.tc-console-send {
  font: inherit; font-size: 13px; line-height: 1; padding: 0 14px; min-height: var(--tap);
  background: var(--focus); color: #fff; border: 1px solid transparent; border-radius: 8px; cursor: pointer;
}
.tc-console-send:hover { filter: brightness(1.08); }
.tc-console-send:disabled { opacity: 0.5; cursor: not-allowed; filter: none; }
.tc-console-send:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
/* Tablet / narrow desktop (e.g. iPad portrait ~834px): keep the whole toolbar on ONE row by going
   icon-only, instead of letting the labelled buttons wrap to an untidy second row. Labels return on
   wider desktops where there's room. Each tool stays a ≥44px square tap target. */
@media (max-width: 1024px) {
  .tc-tool .tc-tool-label { display: none; }
  .tc-tool { min-width: var(--tap); justify-content: center; padding: 0 8px; }
  /* React Flow zoom/fit controls default to 26px — too small for touch on tablets too (iPad
     portrait/landscape), not just phones. Scope under .react-flow__controls to beat its own rule. */
  .react-flow__controls .react-flow__controls-button { width: var(--tap); height: var(--tap); }
  .react-flow__controls .react-flow__controls-button svg { max-width: 18px; max-height: 18px; }
}
@media (max-width: 640px) {
  /* Phone: the toolbar can't share a row with the title — title takes the first row, the action
     cluster drops to its own row below (already icon-only from the rule above). Drop the group
     separators (they'd dangle at row ends when the cluster wraps) and let the icons sit in one
     left-aligned strip. */
  header { gap: 8px 10px; flex-wrap: wrap; padding: 8px 10px; }
  #title { flex: 1 1 60%; }
  /* Keep the cluster to ONE row under the title: the icon buttons stay fixed and the theme select
     flex-shrinks to fill whatever's left, so the header is a tidy 2 rows (not 3) down to ~360px. */
  .header-actions { width: 100%; margin-left: 0; justify-content: flex-start; flex-wrap: nowrap; gap: 8px; }
  .tc-tool-sep { display: none; }
  #theme { flex: 1 1 auto; min-width: 0; max-width: 160px; }
  #scope-intro { padding: 8px 10px; font-size: 12px; }
  #tc-console { left: calc(12px + env(safe-area-inset-left)); right: calc(12px + env(safe-area-inset-right)); bottom: calc(12px + env(safe-area-inset-bottom)); width: auto; max-height: 50vh; }
  /* React Flow zoom/fit controls default to 26px — too small for touch. Scope under .react-flow__controls
     (specificity 0,2,0) so we beat React Flow's own .react-flow__controls-button rule from the lazy chunk. */
  .react-flow__controls .react-flow__controls-button { width: var(--tap); height: var(--tap); }
  .react-flow__controls .react-flow__controls-button svg { max-width: 18px; max-height: 18px; }
  /* Leaflet zoom +/− default to ~30px — too small for touch. Scope under #diagram (id specificity)
     to beat Leaflet's own .leaflet-touch .leaflet-bar a rule from the dynamically-injected chunk. */
  #diagram .leaflet-touch .leaflet-control-zoom a { width: var(--tap); height: var(--tap); line-height: var(--tap); }
  /* Editable checklists: ≥44px tap rows on phones (box + label centered, label fills the row). */
  .tc-checklist--editable [class*="Checkbox-body"] { min-height: var(--tap); }
  .tc-checklist--editable [class*="Checkbox-labelWrapper"] { flex: 1; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}

/* Narrow screens (phone): sidebar becomes a horizontal strip on top. */
@media (max-width: 640px) {
  body { flex-direction: column; }
  #sidebar { width: 100%; max-height: 35vh; border-right: 0; border-bottom: 1px solid var(--border); }
  #scopes { flex-direction: row; flex-wrap: wrap; }
  .scope { width: auto; }
}

/* --- calltree: one call-hierarchy model, two views (compact diff outline ⇄ flow graph) --------- */
.tc-calltree { display: flex; flex-direction: column; gap: 10px; padding: 4px 2px; min-width: 0; }
.tc-ct-head { display: flex; align-items: center; gap: 12px; }
.tc-ct-title { font-weight: 600; font-size: 15px; font-family: var(--font-sans); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* segmented Tree | Graph toggle — mirrors the header toggle-button language (pressed = accent fill). */
.tc-ct-toggle { display: inline-flex; margin-left: auto; flex: none; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.tc-ct-tbtn { font: inherit; font-size: 12px; line-height: 1; min-height: var(--tap); padding: 0 14px; border: 0; background: var(--surface); color: var(--fg); cursor: pointer; transition: background .15s ease, color .15s ease; }
.tc-ct-tbtn + .tc-ct-tbtn { border-left: 1px solid var(--border); }
.tc-ct-tbtn:hover { background: var(--active); }
.tc-ct-tbtn[aria-pressed="true"] { color: var(--focus); background: var(--active); }
@supports (background: color-mix(in srgb, red, blue)) {
  .tc-ct-tbtn[aria-pressed="true"] { background: color-mix(in srgb, var(--focus) 15%, var(--surface)); }
}
.tc-ct-tbtn:focus-visible { outline: 2px solid var(--focus); outline-offset: -2px; }
/* legend chips (shown only for a diff) */
.tc-ct-legend { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 4px; font-family: var(--font-sans); }
.tc-ct-chip { display: inline-flex; align-items: center; gap: 6px; font-size: 11px; color: var(--muted); }
.tc-ct-chip-dot { width: 9px; height: 9px; border-radius: 3px; flex: none; }
/* compact diff outline */
/* Cap the outline width so right-aligned notes stay near their rows on wide desktops (on iPad/phone
   the viewport is already narrower than this, so it's full-width there). */
.tc-ct-tree { display: flex; flex-direction: column; gap: 3px; font-family: var(--font-mono); max-width: 860px; }
.tc-ct-row { display: flex; align-items: center; gap: 8px; min-width: 0; padding-block: 2px; }
.tc-ct-bar { width: 3px; height: 16px; border-radius: 2px; flex: none; }
.tc-ct-sign { width: 12px; flex: none; text-align: center; font-weight: 700; font-size: 13px; }
/* label/file/note all stay one line but shrink+ellipsize under pressure, so a long name or note
   never forces horizontal scroll on a narrow screen (the row owns the width). */
.tc-ct-label { font-size: 13px; color: var(--fg); white-space: nowrap; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.tc-ct-file { font-size: 11.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 0 1 auto; }
.tc-ct-note { margin-left: auto; padding-left: 12px; font-size: 11.5px; color: var(--muted); flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; font-family: var(--font-sans); white-space: nowrap; }
/* graph view: let the nested flow fill the stage. Scope the viewport-fill to a STANDALONE board
   (direct #diagram child) — mirroring flow.tsx's own `#diagram > .tc-flow-wrap` rule — so inside a
   `panes` pane it falls back to flow's default min(70vh,600px) instead of overflowing the pane. */
.tc-ct-graph { flex: 1; min-height: 0; }
#diagram > .tc-calltree .tc-ct-graph .tc-flow-wrap { height: calc(100dvh - 168px); }
