/* ophedocs styles — served at /static/styles.css and linked from the layout. */

/* Theme tokens use light-dark() so each colour is defined once. CSS picks
   between the two values based on the resolved color-scheme:
   - default (`color-scheme: light dark`) follows the OS preference;
   - `:root[data-theme="dark"]` forces dark for the whole subtree;
   - `:root[data-theme="light"]` forces light.
   The data-theme attr is set by themePreInit / theme-toggle scripts. */
:root {
  color-scheme: light dark;
  --bg: light-dark(#fafaf7, #15171a);
  --fg: light-dark(#1a1a1a, #e8e8e3);
  --muted: light-dark(#595959, #b6b6ae);
  --accent: light-dark(#1d4ed8, #93c5fd);
  /* Semi-transparent so nested marks for overlapping threads layer their
     colour — the shared region renders visibly darker than single spans. */
  --highlight: light-dark(rgba(255, 226, 56, 0.45), rgba(212, 185, 74, 0.32));
  --highlight-fg: light-dark(#1a1a1a, #fef3c7);
  --highlight-border: light-dark(#b8941a, #d4b94a);
  --highlight-resolved: light-dark(#d6e8c4, #2f4422);
  --highlight-resolved-fg: light-dark(#1a1a1a, #d9fdd3);
  --highlight-orphaned: light-dark(#e7e5e4, #2e2c2a);
  --highlight-orphaned-fg: light-dark(#1a1a1a, #c0bdb8);
  --border: light-dark(#d8d8d0, #353841);
  --panel: light-dark(#fdfdfb, #1c1f23);
  --doc-bg: light-dark(#ffffff, #1a1d21);
  --doc-flash: light-dark(#fff7c2, #3a330d);
  --code-bg: light-dark(#ececea, #2a2c30);
  --code-block-bg: light-dark(#f4f4ef, #0c0d0f);
  --code-block-fg: light-dark(#1a1a1a, #e8e8e3);
  --thread-bg: light-dark(#ffffff, #1f2227);
  --tooltip-bg: light-dark(rgba(26, 26, 26, 0.95), rgba(245, 245, 240, 0.96));
  --tooltip-fg: light-dark(#ffffff, #15171a);
  --tooltip-ok: light-dark(#4ade80, #15803d);
  --tooltip-err: light-dark(#fca5a5, #b91c1c);
  /* Height of the top header.bar — exposed as a token so fixed-position
     overlays (orphaned drawer, etc.) can pin themselves below it without
     hardcoding the value. Padding 0.6rem * 2 + ~1.4rem content + 1px
     border. */
  --bar-height: 2.6rem;
  --ok: light-dark(#15803d, #4ade80);
  --warn: light-dark(#a16207, #fbbf24);
  --pill-open: light-dark(#fef08a, #59500d);
  --pill-open-fg: light-dark(#3f3a08, #fef9c3);
  --pill-resolved: light-dark(#bbf7d0, #2c5b35);
  --pill-resolved-fg: light-dark(#14532d, #d9fdd3);
  --pill-orphaned: light-dark(#d4d4d4, #3a3a3a);
  --pill-orphaned-fg: light-dark(#2c2c2c, #e0e0dc);
  --button-hover-bg: light-dark(rgba(0, 0, 0, 0.06), rgba(255, 255, 255, 0.08));
  --button-copied-bg: light-dark(rgba(22, 163, 74, 0.18), rgba(74, 222, 128, 0.18));
  --mark-outline-hover: light-dark(rgba(0, 0, 0, 0.2), rgba(255, 255, 255, 0.25));
  --avatar-ai-bg: light-dark(#dbeafe, #1e293b);
  --avatar-default-bg: light-dark(#e5e5e0, #2c2e33);
}
:root[data-theme="dark"] { color-scheme: dark; }
:root[data-theme="light"] { color-scheme: light; }

/* Cross-fade colour-related properties on theme toggle so backgrounds,
   text, borders, and SVG fills/strokes (D2 diagrams) all ease in step
   instead of snapping to the new palette. Longhand `transition-*` so
   element-specific transitions that use the `transition` shorthand
   don't blow this away — and `:where()` to keep the specificity at 0. */
:where(*, *::before, *::after) {
  transition-property: background-color, color, border-color, outline-color, fill, stroke, box-shadow;
  transition-duration: 0.2s;
  transition-timing-function: ease;
}

* { box-sizing: border-box; }
/* Suppress the WebKit/Chromium grey-blue rectangle that flashes on tap
   for any clickable element. Inherited, so set once on the root. */
html { -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.scroll-container {
  flex: 1 1 0;
  overflow-y: auto;
  scrollbar-gutter: stable;
}

/* Top bar. */
header.bar {
  flex: 0 0 auto;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.6rem 1rem;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
/* Narrow viewports surface the TOC toggle (hidden above 1100px), so
   suddenly two icon buttons sit side-by-side at the left of the bar.
   The 1rem gap that's right for narrative items reads as too loose
   between adjacent icon-sized buttons — tighten it across the bar at
   the same breakpoint. */
@media (max-width: 1100px) {
  header.bar { gap: 0.5rem; }
}
/* Buttons in the bar need a positioned containing block so their
   ::after tooltip pseudo anchors against the button itself, not some
   grandparent. .icon-button already declares this; the toggles
   (.theme-toggle, .version-toggle, .toc-toggle, .orphaned-toggle)
   would otherwise lack one. */
header.bar button { position: relative; }
header.bar h1 { font-size: 14px; margin: 0; font-weight: 600; }
header.bar .doc-id {
  color: var(--muted);
  font-family: ui-monospace, Menlo, Consolas, monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  /* Flex children default to `min-width: auto` (= content), which means
     they refuse to shrink below their text and push siblings off the
     bar. Releasing the floor lets the ellipsis actually kick in. */
  min-width: 0;
}
header.bar .spacer { flex: 1; }
.status { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; color: var(--muted); }
.status.ok { color: var(--ok); }
.status.warn { color: var(--warn); }

/* ============================================================
   Version-history dropdown — replaces the static version pill in the
   top bar with a click-to-open list streamed from /docs/:id/versions.
   ============================================================ */
.version-control { position: relative; display: inline-flex; align-items: center; }
button.version-toggle {
  background: none;
  border: none;
  padding: 0.2rem 0.35rem;
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  border-radius: 4px;
  cursor: pointer;
  font: inherit;
  color: var(--muted);
}
button.version-toggle:hover {
  color: var(--fg);
  background: var(--button-hover-bg);
}
button.version-toggle .material-icons { font-size: 18px; }
ul.version-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  margin: 0;
  padding: 0.25rem 0;
  list-style: none;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.18);
  min-width: 320px;
  max-height: 60vh;
  overflow-y: auto;
  z-index: 110;
}
@media (max-width: 900px) {
  ul.version-dropdown { max-width: calc(100vw - 1rem); }
}
ul.version-dropdown[hidden] { display: none; }
ul.version-dropdown .version-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
}
ul.version-dropdown .version-row:last-child { border-bottom: none; }
ul.version-dropdown .version-row.current {
  background: var(--button-hover-bg);
}
ul.version-dropdown .version-row .avatar-stack {
  position: relative;
  flex: 0 0 auto;
}
ul.version-dropdown .version-row .avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: block;
}
ul.version-dropdown .version-row .tool-badge {
  position: absolute;
  right: -3px;
  bottom: -3px;
  width: 14px;
  height: 14px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 50%;
  color: var(--accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
ul.version-dropdown .version-row .tool-badge img,
ul.version-dropdown .version-row .tool-badge svg {
  width: 11px;
  height: 11px;
  display: block;
}
ul.version-dropdown .version-row .version-meta {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  flex: 1 1 auto;
  min-width: 0;
  white-space: nowrap;
}
ul.version-dropdown .version-row .version-row-top {
  display: flex;
  gap: 0.5rem;
  align-items: baseline;
  justify-content: space-between;
}
ul.version-dropdown .version-row .version-author {
  color: var(--fg);
  font-weight: 500;
}
ul.version-dropdown .version-row time {
  color: var(--muted);
  font-size: 11px;
}
ul.version-dropdown .version-row .version-summary {
  color: var(--fg);
  font-size: 12px;
  white-space: normal;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
ul.version-dropdown .version-row .version-hash {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 11px;
  color: var(--muted);
  background: none;
  padding: 0;
}
/* Spinner shown at the bottom of the dropdown while versions stream in.
   The list element has data-loading initially; htmx-sse's hx-on handler
   removes it on the 'done' event. CSS-only — driven by attribute. */
ul.version-dropdown[data-loading]::after {
  content: '';
  display: block;
  width: 14px;
  height: 14px;
  margin: 0.6rem auto;
  border: 2px solid var(--border);
  border-top-color: var(--muted);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}
header.bar button.theme-toggle,
header.bar button.toc-toggle,
header.bar a.home-link {
  background: none;
  border: none;
  padding: 0.2rem 0.35rem;
  display: inline-flex;
  align-items: center;
  border-radius: 4px;
  cursor: pointer;
  color: var(--muted);
  text-decoration: none;
}
header.bar button.theme-toggle:hover,
header.bar button.toc-toggle:hover,
header.bar a.home-link:hover {
  color: var(--fg);
  background: var(--button-hover-bg);
}
header.bar button.theme-toggle .material-icons,
header.bar button.toc-toggle .material-icons,
header.bar a.home-link .material-icons { font-size: 18px; }
/* TOC toggle is visible on narrow widths only; the TOC sidebar is hidden then. */
header.bar button.toc-toggle { display: none; }
@media (max-width: 1100px) {
  header.bar button.toc-toggle { display: inline-flex; }
}

/* Three-column layout: TOC | doc | threads sidebar. .doc-grid is the
   positioning containing block for absolute-anchored thread cards, so the
   <mark> anchors inside <main> are visible to them. */
.doc-grid {
  position: relative;
  display: grid;
  grid-template-columns: 200px minmax(0, 1fr) 320px;
  gap: 2rem;
  max-width: 1320px;
  margin: 0 auto;
  padding: 0 1.25rem;
  min-height: 100%;
  align-items: start;
  /* Pin element to the doc body's left edge: TOC width + grid gap. */
  --doc-start: calc(200px + 2rem);
}
@media (max-width: 1100px) {
  .doc-grid { grid-template-columns: minmax(0, 1fr) 320px; --doc-start: 0; }
  nav.toc { display: none; }
  /* Expanded-over-doc TOC mode (toggle button on narrow). */
  body.toc-open nav.toc {
    display: block;
    position: fixed;
    top: 3rem;
    left: 0.5rem;
    width: min(280px, calc(100vw - 1rem));
    max-height: calc(100vh - 3.5rem);
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.75rem;
    z-index: 200;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.25);
  }
}
@media (max-width: 900px) {
  /* Narrow: thin gutter; threads collapse to avatar circles, expand-as-popover. */
  .doc-grid { grid-template-columns: minmax(0, 1fr) 40px; --doc-start: 0; gap: 0.5rem; }
  aside.threads .thread.is-active {
    width: min(320px, calc(100vw - 1rem));
    z-index: 50;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.25);
  }
}

/* Table of contents. Sticky inside the scroll-container so it stays in
   place from scroll=0 (no initial scroll-with-content phase). */
nav.toc {
  position: sticky;
  top: 0;
  align-self: start;
  padding-top: 1.5rem;
  font-size: 13px;
  max-height: calc(100vh - 5rem);
  overflow-y: auto;
  scrollbar-width: thin;
}
nav.toc .toc-title {
  font-size: 12px;
  font-weight: 600;
  margin: 0 0 0.5rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
nav.toc .toc-list { list-style: none; padding: 0; margin: 0; }
nav.toc .toc-item { line-height: 1.4; }
nav.toc .toc-item a {
  color: var(--muted);
  text-decoration: none;
  display: block;
  padding: 0.2rem 0.5rem;
  border-left: 2px solid transparent;
  padding-left: calc(0.5rem + var(--indent, 0) * 0.75rem);
}
nav.toc .toc-item a:hover { color: var(--fg); border-left-color: var(--border); }
nav.toc .toc-item.toc-level-1 { --indent: 0; }
nav.toc .toc-item.toc-level-1 a { font-weight: 600; color: var(--fg); }
nav.toc .toc-item.toc-level-2 { --indent: 1; }
nav.toc .toc-item.toc-level-3 { --indent: 2; }
nav.toc .toc-item.toc-level-4 { --indent: 3; font-size: 12px; }
nav.toc .toc-item.toc-level-5 { --indent: 4; font-size: 12px; }
nav.toc .toc-item.toc-level-6 { --indent: 5; font-size: 12px; }

/* Doc body. Stretches to fill the doc-grid vertically so the doc bg
   runs edge-to-edge regardless of content length. */
main.doc {
  align-self: stretch;
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  padding: 1.5rem 2rem 4rem;
  background: var(--doc-bg);
}
main.doc.flash { background: var(--doc-flash); }
main.doc h1, main.doc h2, main.doc h3, main.doc h4, main.doc h5, main.doc h6 {
  scroll-margin-top: 4rem;
  font-weight: 600;
}
/* Flash a heading briefly when a #heading-link is navigated to, so the
   reader can spot what was being pointed at. The keyframe runs once per
   class application; the class is re-added via reflow on repeat hashes. */
main.doc :is(h1, h2, h3, h4, h5, h6).heading-flash {
  animation: heading-flash 1.4s ease-out;
}
@keyframes heading-flash {
  0%   { background: var(--doc-flash); }
  100% { background: transparent; }
}
main.doc h1 { font-size: 2rem; margin-top: 0; }
main.doc h2 { font-size: 1.5rem; border-bottom: 1px solid var(--border); padding-bottom: 0.3rem; }
main.doc h3 { font-size: 1.2rem; }
main.doc p { margin: 0.8em 0; }
main.doc code {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 0.9em;
  background: var(--code-bg);
  padding: 0.1em 0.35em;
  border-radius: 3px;
}
main.doc pre {
  background: var(--code-block-bg);
  color: var(--code-block-fg);
  padding: 1rem;
  border-radius: 6px;
  overflow-x: auto;
}
main.doc div.d2-diagram {
  background: var(--doc-bg);
  text-align: center;
  padding: 1rem 0;
  margin: 1rem 0;
}
main.doc div.d2-diagram svg {
  max-width: 100%;
  height: auto;
}
main.doc div.d2-diagram.d2-error {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 12px;
  color: var(--warn);
  background: var(--code-bg);
  text-align: left;
  padding: 0.5rem 0.75rem;
  border-left: 3px solid var(--warn);
}
main.doc pre code { background: none; padding: 0; color: inherit; }
main.doc a { color: var(--accent); text-decoration: none; }
main.doc a:hover { text-decoration: underline; }
main.doc blockquote {
  margin: 1rem 0;
  padding: 0.5rem 1rem;
  border-left: 3px solid var(--border);
  color: var(--muted);
}
main.doc mark.thread-highlight {
  background: var(--highlight);
  color: var(--highlight-fg);
  padding: 0.05em 0.15em;
  border-radius: 2px;
  cursor: pointer;
  outline: 2px solid transparent;
}
main.doc mark.thread-highlight:hover { outline-color: var(--mark-outline-hover); }
main.doc mark.thread-highlight.is-active { outline-color: var(--accent); }
main.doc mark[data-thread-status="resolved"] { background: transparent; color: inherit; }
main.doc mark[data-thread-status="resolved"].is-active {
  background: var(--highlight-resolved);
  color: var(--highlight-resolved-fg);
}
main.doc mark[data-thread-status="orphaned"] {
  background: var(--highlight-orphaned);
  color: var(--highlight-orphaned-fg);
  text-decoration: line-through dotted;
}
main.doc table { border-collapse: collapse; }
main.doc th, main.doc td { border: 1px solid var(--border); padding: 0.4rem 0.6rem; }

/* Per-heading icon button (copy-link variant): hidden until hovered.
   Scales with the heading font-size, so revert the base square sizing
   and let padding define the hover pill. */
main.doc :is(h1, h2, h3, h4, h5, h6) > button.icon-button.heading-copy {
  opacity: 0;
  transform: translateX(-6px);
  margin-left: 0.4rem;
  vertical-align: middle;
  font-size: inherit;
  width: auto;
  height: auto;
  padding: 0.1rem 0.25rem;
  transition: opacity 0.18s ease, transform 0.18s ease;
}
main.doc :is(h1, h2, h3, h4, h5, h6) > button.icon-button.heading-copy .material-icons { font-size: 0.7em; }
main.doc :is(h1, h2, h3, h4, h5, h6):hover > button.icon-button.heading-copy {
  opacity: 0.5;
  transform: translateX(0);
}
main.doc :is(h1, h2, h3, h4, h5, h6) > button.icon-button.heading-copy:hover { opacity: 1; }
main.doc :is(h1, h2, h3, h4, h5, h6) > button.icon-button.heading-copy.copied,
main.doc :is(h1, h2, h3, h4, h5, h6) > button.icon-button.heading-copy.failed {
  opacity: 1;
  transform: translateX(0);
}

/* Threads margin. */
aside.threads { font-size: 14px; padding-top: 1.5rem; }
aside.threads .threads-header {
  display: flex;
  flex-direction: row;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 0.1rem 0.5rem;
  margin-bottom: 0.75rem;
  /* Stay within the gutter's width so the count never falls off-screen. */
  max-width: 100%;
  overflow-wrap: anywhere;
}
/* On narrow screens hide the "Threads" title; only the count stays. */
@media (max-width: 900px) {
  aside.threads .threads-header h2 { display: none; }
}
aside.threads h2 {
  font-size: 14px;
  font-weight: 600;
  margin: 0;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
aside.threads .threads-count { font-size: 11px; color: var(--muted); }
/* On narrow screens the dot separator drops out and the two counts stack
   onto their own lines. */
@media (max-width: 900px) {
  aside.threads .threads-count-sep { display: none; }
  aside.threads .threads-count-total,
  aside.threads .threads-count-open { display: block; }
}
aside.threads .muted { color: var(--muted); font-size: 13px; }
aside.threads .threads-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
aside.threads .thread {
  background: var(--thread-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.75rem;
  /* Above header.bar (z-index: 100) so descendant tooltips paint over
     the bar instead of being trapped in this card's stacking context. */
  z-index: 101;
  transform-origin: top right;
  /* Smooth transition between AVATAR / EXPANDED / ACTIVE states. */
  transition:
    width 0.2s ease,
    height 0.2s ease,
    padding 0.2s ease,
    background-color 0.2s ease,
    border-color 0.2s ease,
    border-radius 0.2s ease,
    box-shadow 0.2s ease,
    transform 0.16s ease,
    filter 0.2s ease,
    left 0.2s ease,
    top 0.2s ease;
}
aside.threads .thread:hover { z-index: 102; }
aside.threads .thread.is-active {
  /* Active card scales up to feel raised; siblings stay put. */
  transform: scale(1.04);
  z-index: 103;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.15), 0 6px 18px rgba(0, 0, 0, 0.08);
}

@supports (anchor-name: --x) {
  aside.threads .threads-list { display: block; min-height: 0; }
  aside.threads .thread {
    position: absolute;
    right: 1.25rem;
    width: min(320px, calc(100vw - 2.5rem));
  }
}

/* ============================================================
   THREAD CARD STATES (same markup for open & resolved)

   - EXPANDED: full card visible. Default on wide screens for non-active,
     non-resolved threads. Styled by the base .thread rule above.
   - AVATAR:   collapsed circle showing only .thread-avatar. Default on
     narrow screens for any non-active thread, AND on wide screens for
     non-active resolved threads.
   - ACTIVE:   full card with elevated styling (.is-active above). Same
     visual on every viewport.

   .thread-avatar holds the slot content: <img> for open threads,
   <span.material-icons>check for resolved. It's hidden in EXPANDED /
   ACTIVE and revealed only in AVATAR.
   ============================================================ */

/* AVATAR element — hidden by default, shown in AVATAR state below. */
aside.threads .thread > .thread-avatar { display: none; }
aside.threads .thread > img.thread-avatar { object-fit: cover; }
aside.threads .thread.thread-resolved > span.thread-avatar {
  background: var(--ok);
  color: var(--bg);
  font-size: 16px;
  font-weight: 700;
  align-items: center;
  justify-content: center;
}

/* CROSS-FADE strategy: the inner .thread-content stays at the full
   "expanded" width regardless of state, so its layout never reflows
   during the size transition (the violent text rewrap users complained
   about). The outer .thread shrinks/grows; overflow: hidden clips the
   inner content during the avatar phase. The .thread-avatar element is
   an absolute overlay that cross-fades with .thread-content. */
:root { interpolate-size: allow-keywords; } /* lets height: auto animate */
aside.threads .thread { overflow: hidden; }
/* Hover / active cards are at full size — overflow: hidden is only needed
   while the card is shrunk to an avatar circle. Switching to visible here
   lets descendant tooltips (icon-button ::after, tool-badge ::after) escape
   the card's bounds. */
aside.threads .thread:hover,
aside.threads .thread.is-active {
  overflow: visible;
}
aside.threads .thread > .thread-content {
  /* Match the expanded card's CONTENT box (full width minus L+R padding)
     so content fills the active card edge-to-edge. The width is fixed
     (independent of the outer's current size) — that's the no-reflow
     trick: when the outer shrinks to a 24/28px circle, this content
     stays laid out at its full size and is clipped by overflow: hidden. */
  width: calc(min(320px, calc(100vw - 2.5rem)) - 1.5rem);
  transition: opacity 0.15s ease;
}
aside.threads .thread > .thread-avatar {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  /* Inherit the parent's border-radius so the avatar's own rectangular
     bg stays clipped to the card's shape — needed because the parent
     drops `overflow: hidden` on hover (so tooltips can escape), which
     would otherwise let this rectangle bleed past the rounded outline. */
  border-radius: inherit;
  transition: opacity 0.15s ease;
}

/* AVATAR state — narrow viewport: every non-active thread INSIDE the
   anchored threads-list. Orphaned threads live in their own drawer
   (aside.orphaned-drawer) and stay full-size there regardless of width. */
@media (max-width: 900px) {
  aside.threads > .threads-list > .thread:not(.is-active) {
    width: 28px;
    height: 28px;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: 50%;
    cursor: pointer;
  }
  aside.threads > .threads-list > .thread:not(.is-active) > .thread-content {
    opacity: 0;
    pointer-events: none;
  }
  aside.threads > .threads-list > .thread:not(.is-active) > .thread-avatar { opacity: 1; }
}

/* AVATAR state — wide viewport: only non-active resolved threads
   (open ones stay EXPANDED). The circle is smaller and pinned to the
   left edge of the gutter so it doesn't compete with full cards. */
@media (min-width: 901px) {
  aside.threads .thread.thread-resolved:not(.is-active) {
    width: 24px;
    height: 24px;
    padding: 0;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    /* Gutter left edge = full card's left edge = (100% - card width - right offset). */
    left: calc(100% - 320px - 1.25rem);
    right: auto;
  }
  aside.threads .thread.thread-resolved:not(.is-active) > .thread-content {
    opacity: 0;
    pointer-events: none;
  }
  aside.threads .thread.thread-resolved:not(.is-active) > .thread-avatar { opacity: 1; }
  /* Active resolved cards keep the same left-anchored positioning as the
     avatar circle so the width transition interpolates cleanly. Without
     this the box's `right` would flip from `auto` to `1.25rem` (and `left`
     from a number to `auto`) and the browser jumps instead of animating. */
  aside.threads .thread.thread-resolved.is-active {
    left: calc(100% - 320px - 1.25rem);
    right: auto;
  }
}

/* ============================================================
   Version-view mode — when /docs/:id?version=<vid> is loaded, the page
   renders that historical body (read-only) and the threads gutter is
   replaced with a VersionInfoPanel. A banner sits above the doc-grid.
   ============================================================ */
body.viewing-version main.doc { opacity: 0.95; }
/* Threads (and the orphaned-threads drawer) track current state, not the
   historical version being viewed — hide the toggle entirely. */
body.viewing-version #ophedocs-orphaned-toggle { display: none; }
body.viewing-version aside.orphaned-drawer { display: none; }
/* Historic view: TOC and the version-info panel pin in place while the
   doc body scrolls. The banner sits in the body's flex column ABOVE
   the scroll-container (alongside header.bar), so the scroll-container
   itself starts below it — sticky top: 0 here is therefore flush
   against the banner's bottom with no transition phase. */
body.viewing-version aside.version-info-panel {
  position: sticky;
  top: 0;
  align-self: start;
  max-height: calc(100vh - 5rem);
  overflow-y: auto;
}
.version-banner {
  /* Sits between header.bar and .scroll-container in the body's flex
     column, sharing the header's "fixed at top" treatment — the
     scrollbar starts below it. */
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1.25rem;
  background: var(--doc-flash);
  color: var(--fg);
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.version-banner code {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 12px;
  background: none;
  padding: 0;
}
.version-banner .spacer { flex: 1; }
.version-banner-back {
  display: inline-flex;
  align-items: center;
  gap: 0.2rem;
  color: var(--accent);
  text-decoration: none;
  font-weight: 500;
}
/* Underline only on the text — not the icon, where the underline sits
   well below the glyph and looks misaligned with the text's underline. */
.version-banner-back:hover .version-banner-back-text { text-decoration: underline; }
.version-banner-back .material-icons { font-size: 16px; text-decoration: none; }

aside.version-info-panel {
  /* override aside.threads' weird sticky / gutter styling for this mode */
  font-size: 13px;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
aside.version-info-panel .version-info-header {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-size: 11px;
  font-weight: 600;
}
aside.version-info-panel .version-info-header h2 { font-size: 11px; margin: 0; }
aside.version-info-panel .version-info-icon { font-size: 18px; color: var(--accent); }
aside.version-info-panel .version-info-author {
  display: flex;
  gap: 0.6rem;
  align-items: center;
}
aside.version-info-panel .version-info-author-meta {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}
aside.version-info-panel .version-info-author-meta .version-info-name {
  font-weight: 600;
  color: var(--fg);
}
aside.version-info-panel .version-info-author-meta time {
  color: var(--muted);
  font-size: 11px;
}
aside.version-info-panel .version-info-summary {
  margin: 0;
  color: var(--fg);
  font-size: 13px;
  line-height: 1.4;
  white-space: pre-wrap;
}
aside.version-info-panel .version-info-hashes {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}
aside.version-info-panel .version-info-hash {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 11px;
  color: var(--muted);
  background: var(--code-bg);
  padding: 0.2rem 0.4rem;
  border-radius: 3px;
  word-break: break-all;
  display: inline-flex;
  align-items: center;
  gap: 0.2rem;
}
aside.version-info-panel .version-info-parent .material-icons {
  font-size: 14px;
}
aside.version-info-panel .version-info-back {
  display: inline-flex;
  align-items: center;
  gap: 0.2rem;
  color: var(--accent);
  text-decoration: none;
  font-weight: 500;
  margin-top: auto;
}
aside.version-info-panel .version-info-back:hover { text-decoration: underline; }
aside.version-info-panel .version-info-back .material-icons { font-size: 16px; }

/* Version dropdown rows are clickable links spanning the whole row.
   Padding lives on the anchor so the entire padded area is clickable
   (with padding on the <li>, the click target was the inner <a> only). */
ul.version-dropdown .version-row .version-row-link {
  display: flex;
  flex: 1;
  align-items: center;
  gap: 0.6rem;
  padding: 0.5rem 0.75rem;
  text-decoration: none;
  color: inherit;
}
ul.version-dropdown .version-row:hover { background: var(--button-hover-bg); }

/* ============================================================
   Orphaned-threads drawer — full-height side panel pinned to the right
   edge of the viewport. Collapsed (default) it's 0px wide and clipped;
   `body.orphaned-open` slides it out to its full width with its own
   internal scrollbar. The open/close toggle lives in the top header bar.
   ============================================================ */
aside.orphaned-drawer {
  position: fixed;
  /* Sits below the site-wide header bar so the bar's controls stay
     visible while the drawer is open. The bar's height is exposed as
     a CSS token (see :root). */
  top: var(--bar-height);
  right: 0;
  bottom: 0;
  width: min(360px, calc(100vw - 2rem));
  /* Slide via transform rather than animating width so the drawer can
     stay overflow: visible — otherwise overflow: hidden (needed during
     a width 0 → full slide) would clip descendant tooltips that
     extend outside a card's bounds. */
  transform: translateX(100%);
  background: var(--panel);
  border-left: 1px solid var(--border);
  border-top: 1px solid var(--border);
  box-shadow: -8px 0 24px rgba(0, 0, 0, 0.08);
  display: flex;
  flex-direction: column;
  /* Above the anchored thread cards in the gutter (z: 101–103), which
     the drawer slides over. Stays below the version dropdown (z: 110). */
  z-index: 105;
  transition: transform 0.18s ease, background-color 0.2s ease, color 0.2s ease, border-color 0.2s ease;
  font-size: 14px;
}
body.orphaned-open aside.orphaned-drawer {
  transform: translateX(0);
}
aside.orphaned-drawer .orphaned-list {
  list-style: none;
  padding: 0.75rem;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  /* The list owns the scroll inside the drawer. */
  flex: 1 1 auto;
  overflow-y: auto;
}
aside.orphaned-drawer .muted {
  padding: 0.75rem;
  color: var(--muted);
  font-size: 13px;
}
/* Inside the drawer, cards are normal flow (no anchor positioning). */
aside.orphaned-drawer .thread {
  position: static;
  width: 100%;
}
/* Resolved threads inside the drawer must NOT collapse to the avatar
   circle — their context (the drawer) already conveys "orphaned", and
   resolved-vs-open is shown via the status pill. Override the avatar-
   collapse rules from the threads-list selectors above. */
aside.orphaned-drawer .thread.thread-resolved:not(.is-active) {
  width: 100%;
  height: auto;
  padding: 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: default;
  left: auto;
  right: auto;
}
aside.orphaned-drawer .thread.thread-resolved:not(.is-active) > .thread-content {
  opacity: 1;
  pointer-events: auto;
}
aside.orphaned-drawer .thread.thread-resolved:not(.is-active) > .thread-avatar {
  opacity: 0;
}

/* View Transitions for thread cards: the runtime wraps outerHTML swaps in
   document.startViewTransition() when a card has data-transition. Each card
   carries a unique view-transition-name (set inline in threadsSidebar.tsx)
   so the API pairs old & new and animates size/position between them — for
   the open → resolved collapse from full card to 24px circle, especially.
   The default 250ms crossfade is fine; we just slow it slightly so the
   large size delta is legible, and disable it under reduced motion. */
::view-transition-old(*),
::view-transition-new(*) {
  animation-duration: 0.32s;
}
@media (prefers-reduced-motion: reduce) {
  ::view-transition-old(*),
  ::view-transition-new(*) {
    animation: none;
  }
}

/* Slot wraps the toggle so SSE can swap it in/out; collapse it out of the
   flex layout when no orphans exist so it doesn't contribute a gap. */
header.bar #ophedocs-orphaned-slot:empty { display: none; }
/* The header-bar toggle that opens the drawer reuses theme-toggle styling. */
header.bar button.orphaned-toggle {
  background: none;
  border: none;
  padding: 0.2rem 0.35rem;
  display: inline-flex;
  align-items: center;
  border-radius: 4px;
  cursor: pointer;
  color: var(--muted);
}
header.bar button.orphaned-toggle:hover {
  color: var(--fg);
  background: var(--button-hover-bg);
}
header.bar button.orphaned-toggle .material-icons { font-size: 18px; }
/* Alert variant: at least one orphaned thread is still open. */
header.bar button.orphaned-toggle.orphaned-toggle-alert {
  color: var(--tooltip-err);
}
header.bar button.orphaned-toggle.orphaned-toggle-alert:hover {
  color: var(--tooltip-err);
  background: var(--button-hover-bg);
}
aside.threads .thread-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.4rem;
}
aside.threads .thread-id { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; color: var(--muted); }
aside.threads .thread-actions { display: inline-flex; align-items: center; gap: 0.4rem; }

/* Generic icon-only button used across the page (copy ID, copy link,
   per-heading link, thread status toggle, etc.). Square so the hover
   pill is symmetric around the 16px icon. */
button.icon-button {
  background: none;
  border: none;
  padding: 0;
  width: 24px;
  height: 24px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  border-radius: 4px;
  cursor: pointer;
  opacity: 0.45;
  color: var(--muted);
  transition: opacity 0.15s ease, background-color 0.2s ease, color 0.2s ease;
  vertical-align: middle;
  position: relative;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
button.icon-button:hover { opacity: 1; background: var(--button-hover-bg); }
button.icon-button.copied { opacity: 1; background: var(--button-copied-bg); color: var(--ok); }
button.icon-button .material-icons { font-size: 16px; }
aside.threads .thread:hover button.icon-button { opacity: 0.85; }
/* Status toggle on a resolved thread reads as "Mark unresolved" — same
   check icon as the open-state "Mark resolved" but tinted green so the
   visual signal "this thread is already done" is immediate. */
aside.threads .thread.thread-resolved button.thread-status-toggle,
aside.threads .thread.thread-resolved button.thread-status-toggle:hover {
  color: var(--ok);
}
/* AI action buttons need a host to postMessage to — the same-origin
   browser viewer has no parent host, so hide them outside widget mode. */
body:not(.widget) .ai-action { display: none; }

/* "Open with Claude" header link — opens claude.ai/new in a new tab
   with a prompt that asks Claude to read this doc. Hidden inside the
   MCP widget (the user is already in a Claude chat). Styled to match
   the surrounding .icon-button copy buttons. */
.claude-open-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  padding: 0;
  line-height: 1;
  border-radius: 4px;
  color: var(--muted);
  text-decoration: none;
  opacity: 0.45;
  transition: opacity 0.15s ease, background-color 0.2s ease;
  position: relative;
  user-select: none;
}
.claude-open-link:hover { opacity: 1; background: var(--button-hover-bg); }
.claude-open-link img { display: block; width: 14px; height: 14px; }
body.widget .claude-open-link { display: none; }

/* Status pills. */
aside.threads .status-pill {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  background: var(--pill-open);
  color: var(--pill-open-fg);
}
aside.threads .status-pill.status-resolved { background: var(--pill-resolved); color: var(--pill-resolved-fg); }
aside.threads .status-pill.status-orphaned { background: var(--pill-orphaned); color: var(--pill-orphaned-fg); }

/* Comment formatting. */
aside.threads .comments {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
aside.threads .comment {
  border-top: 1px solid var(--border);
  padding-top: 0.5rem;
  font-size: 13px;
}
aside.threads .comment:first-child { border-top: none; padding-top: 0; }
aside.threads .comment .comment-meta {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 0.25rem;
}
aside.threads .comment .author { font-weight: 600; font-size: 12px; }
aside.threads .comment .avatar-stack {
  position: relative;
  display: inline-block;
  flex-shrink: 0;
}
aside.threads .comment .avatar {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--avatar-default-bg);
  display: block;
}
/* Tool badge overlaid on the bottom-right of the avatar; hover shows the
   tool name via the data-tooltip ::after rule below. */
aside.threads .comment .tool-badge {
  position: absolute;
  right: -3px;
  bottom: -3px;
  width: 13px;
  height: 13px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 50%;
  color: var(--accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 9px !important;
  line-height: 1;
}
/* The img/svg is clipped to a circle directly so the badge itself doesn't
   need overflow: hidden — that would also clip the ::after tooltip. */
aside.threads .comment .tool-badge img,
aside.threads .comment .tool-badge svg {
  width: 11px;
  height: 11px;
  display: block;
  border-radius: 50%;
}
aside.threads .comment .tool-badge::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 4px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--tooltip-bg);
  color: var(--tooltip-fg);
  padding: 0.2rem 0.45rem;
  border-radius: 4px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 11px;
  font-weight: 500;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  letter-spacing: 0.02em;
  line-height: 1.4;
}
@media (hover: hover) {
  aside.threads .comment .tool-badge:hover::after {
    opacity: 1;
    transition: opacity 0.12s ease;
  }
}
aside.threads .comment.author-ai .avatar { background: var(--avatar-ai-bg); }
aside.threads .comment .time {
  font-size: 11px;
  color: var(--muted);
  font-family: ui-monospace, Menlo, Consolas, monospace;
}
aside.threads .comment .body { font-size: 13px; }
aside.threads .comment .body p { margin: 0.3em 0; }
aside.threads .comment .body p:first-child { margin-top: 0; }
aside.threads .comment .body p:last-child { margin-bottom: 0; }
aside.threads .comment .body code {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 0.9em;
  background: var(--code-bg);
  padding: 0.05em 0.3em;
  border-radius: 3px;
}
aside.threads .comment .body em { font-style: italic; }
aside.threads .comment .body strong { font-weight: 600; }
aside.threads .comment .body a { color: var(--accent); text-decoration: none; }
aside.threads .comment .body a:hover { text-decoration: underline; }

/* Tooltip on any button (or anchor) with a `data-tooltip` attribute.
   Plain CSS positioning relative to the element's `position: relative`
   (or own) containing block. Default placement is above the element. */
:is(button, a)[data-tooltip]::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 200;
  background: var(--tooltip-bg);
  color: var(--tooltip-fg);
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
  font-size: 11px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-weight: 500;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  letter-spacing: 0.02em;
  line-height: 1.4;
}
/* Buttons in the top header bar render their tooltip BELOW — above
   would clip into the viewport edge (body has `overflow: hidden` so
   anything above the bar is invisible). */
header.bar :is(button, a)[data-tooltip]::after {
  bottom: auto;
  top: calc(100% + 6px);
}
/* Pointer devices: tooltip on hover. Touch devices: tooltip while the
   finger is actively on the button (:active fires on touchstart and clears
   on touchend, so a long-press shows it and a quick tap barely flashes it
   before the click handler adds .copied and the "✓ Copied" flash takes
   over). The (hover: hover) gate avoids the sticky pseudo-hover after a
   tap that would otherwise replay the label right after "✓ Copied" fades.
   The .copied / .failed flash works everywhere because it's class-driven. */
@media (hover: hover) {
  :is(button, a)[data-tooltip]:hover::after {
    opacity: 1;
    transition: opacity 0.12s ease;
  }
}
@media (hover: none) {
  :is(button, a)[data-tooltip]:active::after {
    opacity: 1;
    transition: opacity 0.05s ease;
  }
}
button.icon-button.copied::after,
button.icon-button.failed::after {
  animation: tooltip-flash 1.5s ease forwards;
}
button.icon-button.copied::after { content: '✓ Copied'; color: var(--tooltip-ok); }
button.icon-button.failed::after { content: '✗ Failed'; color: var(--tooltip-err); }
@keyframes tooltip-flash {
  0%   { opacity: 0; }
  8%   { opacity: 1; }
  73%  { opacity: 1; }
  100% { opacity: 0; }
}

/* ---------- Standalone pages (homepage + 404) ----------
   The doc viewer's Layout.tsx wraps every doc in the .doc-grid; the
   homepage and 404 page are simple centered cards instead and don't
   share that grid. Theme tokens (--bg, --fg, …) still apply. Homepage-
   specific styles live in static/homepage.css and are loaded only by
   Homepage. The notFound and theme-toggle-fixed rules below are shared
   between the standalone pages. */

body.standalone { overflow: auto; }

/* 404 page (notFound.tsx). Centered card, similar tokens to .home. */
.notfound {
  max-width: 520px;
  margin: 0 auto;
  padding: 5rem 1.5rem;
  text-align: center;
}
.notfound .code {
  font-size: 4rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--muted);
  margin: 0;
  line-height: 1;
}
.notfound h1 {
  margin: 0.75rem 0 0.5rem;
  font-size: 1.5rem;
  letter-spacing: -0.01em;
}
.notfound p { color: var(--muted); margin: 0 0 1.5rem; line-height: 1.6; }
.notfound a {
  display: inline-block;
  padding: 0.55rem 1.1rem;
  border-radius: 4px;
  background: var(--accent);
  color: light-dark(#ffffff, #0b1220);
  font-weight: 600;
  text-decoration: none;
}
.notfound a:hover { filter: brightness(1.08); }

/* Fixed-position theme toggle used by the standalone pages (homepage +
   404). The doc viewer's Layout.tsx uses the bar-embedded .theme-toggle
   above; this is the stand-in for pages that don't have a header bar. */
.theme-toggle-fixed {
  position: fixed;
  top: 1rem;
  right: 1rem;
  width: 2.2rem;
  height: 2.2rem;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--fg);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.theme-toggle-fixed:hover { background: var(--button-hover-bg); }
.theme-toggle-fixed .material-icons { font-size: 1.2rem; }
