:root {
  --bg: #ffffff;
  --text: #2a2620;
  --text-muted: #8b8478;
  --accent: #5a4f44;
  --rail-bg: #f4f1ec;
  --border: #888888;
  --header-bg: #1a1612;
  --header-fg: #ffffff;

  --cell-default:       #ffffff;
  --cell-physical:      #d8d3cb;
  --cell-life:          #f4ddd1;
  --cell-thinking:      #d8e8d4;
  --cell-akasha:        #f7d585;
  --cell-akasha-light:  #fce8b8;
  --cell-inset:         #cad9e8;

  --hover-border: #2a2620;
  --selected-border: #b85c2a;
  --thread-glow: 0 0 0 3px rgba(184, 92, 42, 0.35);

  --font-prose: "Iowan Old Style", "Charter", "Cambria", Georgia, "Times New Roman", serif;
  --font-ui:    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --col-max:    1560px;
  --rail-w:     400px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-prose);
  font-size: 16px;
  line-height: 1.5;
  min-height: 100vh;
}

/* ── Header ────────────────────────────────────── */
#page-header {
  max-width: var(--col-max);
  margin: 0 auto;
  padding: 2rem 2rem 1rem;
}
.title-block h1 {
  margin: 0 0 0.25rem;
  font-size: 1.5rem;
  font-weight: 500;
  letter-spacing: 0.01em;
}
.muted { color: var(--text-muted); margin: 0; font-size: 0.85rem; }

#modes {
  display: flex;
  gap: 0.25rem;
  font-family: var(--font-ui);
  font-size: 0.85rem;
}
.mode-btn {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--text-muted);
  padding: 0.4rem 0.9rem;
  border-radius: 2px;
  cursor: pointer;
  font-family: inherit;
  font-size: inherit;
  transition: all 200ms;
}
.mode-btn:hover { color: var(--text); border-color: var(--text); }
.mode-btn.active { color: var(--bg); background: var(--text); border-color: var(--text); }

#trace-bar {
  margin-top: 1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: baseline;
  font-family: var(--font-ui);
  font-size: 0.9rem;
}
#thread-select {
  font-family: inherit; font-size: inherit;
  padding: 0.3rem 0.5rem;
  border: 1px solid var(--border);
  background: white;
  border-radius: 2px;
}
.thread-description { flex-basis: 100%; font-style: italic; }

/* ── Schema grid ────────────────────────────────── */
/* #schema-root spans the full viewport so the scroll container can use the
   entire width. Page-chrome padding lives on the title block / legend / footer. */
#schema-root {
  margin: 0 auto;
  padding: 1rem 0 3rem;
  position: relative;
}

/* Scroll container — wraps the table and scrolls horizontally when natural
   width exceeds the viewport. Inner padding gives breathing room while the
   sticky-left column anchors against the left edge of this container. */
.scroll-container {
  position: relative;
  overflow-x: auto;
  overflow-y: visible;
  padding: 0 2rem;
  -webkit-overflow-scrolling: touch;
}

/* Right-edge gradient fade — appears only when scrollable + not at end.
   Pure CSS overlay, pointer-events:none so it doesn't intercept clicks. */
.scroll-container::after {
  content: '';
  position: sticky;
  display: none;
  right: 0; top: 0; bottom: 0;
  width: 32px;
  background: linear-gradient(to right, rgba(255,255,255,0), rgba(255,255,255,1));
  pointer-events: none;
  z-index: 4;
}
.scroll-container.is-scrollable:not(.scrolled-to-end)::after { display: block; }

#schema-grid {
  border-collapse: collapse;
  table-layout: fixed;
  font-family: var(--font-ui);
  font-size: 0.78rem;
  /* width is set inline by render.js to the sum of grid.columnWidthsPx.
     margin-inline: auto centers the table when the scroll container is
     wider than the natural width; auto collapses to 0 when narrower so
     the natural overflow-scroll behavior is preserved. */
  margin-inline: auto;
}

/* ── Sticky-left column (col 1: plane numbers) ──
   Cells in data-col 1 stick to the left edge during horizontal scroll.
   Opaque background prevents scrolling content from bleeding through.
   Box-shadow at the right edge provides a separator that's more robust than
   a border (collapsed-table borders flicker across the sticky boundary).
   Shadow only appears once scrolled to keep the unscrolled view clean. */
#schema-grid td.sticky-left {
  position: sticky;
  left: 0;
  z-index: 1;
  background: var(--bg);
}
/* Sticky-left needs an opaque bg, but cell-variant bgs (header, color-*) should
   win over the default. Higher-specificity overrides for variants that may land
   in the sticky column: */
#schema-grid td.cell.header.sticky-left { background: var(--header-bg); }
#schema-grid td.cell.color-physical.sticky-left { background: var(--cell-physical); }
#schema-grid td.cell.color-life.sticky-left { background: var(--cell-life); }
#schema-grid td.cell.color-thinking.sticky-left { background: var(--cell-thinking); }
#schema-grid td.cell.color-akasha.sticky-left,
#schema-grid td.cell.color-akasha-main.sticky-left { background: var(--cell-akasha); }
#schema-grid td.cell.color-akasha-light.sticky-left { background: var(--cell-akasha-light); }
#schema-grid td.cell.color-inset.sticky-left { background: var(--cell-inset); }
.scroll-container.scrolled-from-start #schema-grid td.sticky-left {
  box-shadow: 2px 0 0 rgba(0, 0, 0, 0.08);
}
/* Buffer-row sticky-left cells: keep them transparent-ish via the page bg so
   the vertical buffer-row gap continues to read as transparent at the sticky
   column position too (both are page-white, so visually identical). */
#schema-grid td.sticky-buffer { background: var(--bg); }

/* Empty (filler) cells — no borders, no background */
#schema-grid td.empty {
  background: transparent;
  border: none;
}

/* Buffer rows — visual breathing room below header / above citations.
   border: none lets adjacent cells' borders show through per-segment in border-collapse,
   so plane row 2 top borders and plane row 9 bottom borders remain visible at the
   columns where bordered cells exist. */
#schema-grid td.buffer {
  background: transparent;
  border: none;
  height: 1.2rem;
  padding: 0;
}

/* Buffer column — thin vertical gap after "spirit on this plane" */
#schema-grid td.buffer-col {
  background: transparent;
  border: none;
  padding: 0;
}

/* Plane rows — uniform height matching the original (~5rem fits 4-line italic note in single row) */
#schema-grid tr.plane-row {
  height: 5rem;
}

/* Default content cell — table-cell layout */
td.cell {
  border: 1px solid var(--border);
  padding: 0.85rem 0.7rem;
  background: var(--cell-default);
  vertical-align: middle;
  text-align: center;
  line-height: 1.3;
  overflow-wrap: normal;
  word-break: keep-all;
  hyphens: none;
  height: 3.6rem;
}

td.cell.header {
  background: var(--header-bg);
  color: var(--header-fg);
  font-weight: 500;
  font-size: 0.78rem;
  letter-spacing: 0.02em;
  height: 2.8rem;
}

td.cell.num {
  color: var(--text-muted);
  font-weight: 500;
  font-size: 0.85rem;
  padding: 0.5rem 0.25rem;
  text-align: center;
  vertical-align: middle;
}

/* Italic gray notes — no border, transparent background, centered */
td.cell.note {
  border: none;
  background: transparent;
  font-style: italic;
  color: var(--text-muted);
  font-size: 0.72rem;
  line-height: 1.4;
  padding: 0.5rem 0.4rem;
  vertical-align: middle;
  text-align: center;
}

td.cell.citation {
  font-size: 0.7rem;
  padding: 0.85rem 0.7rem;
  font-family: var(--font-ui);
  background: var(--cell-default);
  vertical-align: middle;
}
td.cell.citation a {
  display: block;
  color: var(--text);
  text-decoration: none;
}
td.cell.citation a:hover { text-decoration: underline; }
td.cell.citation .citation-sub {
  display: block;
  color: var(--text-muted);
  font-size: 0.66rem;
  margin-top: 0.15rem;
}

/* Color variants */
td.cell.color-physical      { background: var(--cell-physical); }
td.cell.color-life          { background: var(--cell-life); }
td.cell.color-thinking      { background: var(--cell-thinking); }
td.cell.color-akasha        { background: var(--cell-akasha); border: none; }       /* counter-image cells: no border */
td.cell.color-akasha-main   { background: var(--cell-akasha); }                     /* akashic record: keeps default 1px border */
td.cell.color-akasha-light  { background: var(--cell-akasha-light); border: none; }
td.cell.color-inset         { background: var(--cell-inset); border: none; }

td.cell .annotation {
  display: block;
  font-size: 0.66rem;
  color: var(--text);
  opacity: 0.7;
  margin-top: 0.35rem;
  font-style: italic;
  line-height: 1.25;
  overflow-wrap: break-word;
}

/* Bulleted content cells — hyphen-bulleted, left-aligned, small font.
   Hanging indent so wrapped lines align with the bullet's first text char. */
td.cell.bulleted {
  text-align: left;
  vertical-align: top;
  padding: 0.6rem 0.55rem;
}
td.cell ul.cell-bullets {
  list-style: none;
  padding: 0;
  margin: 0;
  font-size: 0.7rem;
  line-height: 1.4;
}
td.cell ul.cell-bullets li {
  margin: 0 0 0.25rem 0;
  padding-left: 0.7rem;
  position: relative;
}
td.cell ul.cell-bullets li::before {
  content: '-';
  position: absolute;
  left: 0;
  top: 0;
}
td.cell ul.cell-bullets li:last-child { margin-bottom: 0; }
td.cell ul.cell-bullets li.muted {
  color: var(--text-muted);
  font-style: italic;
}

/* Multi-line text (newlines preserved). Used for cells with `\n` in text. */
td.cell.multiline {
  white-space: pre-line;
}

/* Compact font — matches the bullet-list font-size for visual consistency
   within a content area (e.g. all cells under a single header). */
td.cell.compact { font-size: 0.7rem; line-height: 1.4; }

/* Cell-level muted — entire cell content rendered in muted/gray text.
   Used for "still to come" / future-stage cells (e.g. cells above the
   peak row in evolutionary schemas like FMC00.077). */
td.cell.muted { color: var(--text-muted); }

/* Interactive states */
body.mode-inspect .cell.has-concept,
body.mode-trace .cell.has-concept {
  cursor: pointer;
}
body.mode-inspect .cell.has-concept:hover,
body.mode-trace .cell.has-concept:hover {
  outline: 2px solid var(--hover-border);
  outline-offset: -1px;
  z-index: 2;
}
.cell.selected {
  outline: 2px solid var(--selected-border) !important;
  outline-offset: -1px;
  z-index: 3;
}
.cell.thread-highlighted {
  box-shadow: var(--thread-glow);
  z-index: 2;
}

/* ── Legend ─────────────────────────────────────── */
/* Capped to --col-max and centered, same as page header/footer.
   justify-content: center centers the swatch+label items within the row, so
   the legend reads as visually centered under the schema. */
#legend {
  max-width: var(--col-max);
  margin: 2rem auto 0;
  padding: 0 2rem;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 1.5rem;
  font-family: var(--font-ui);
  font-size: 0.75rem;
  color: var(--text-muted);
}
.legend-item { display: flex; align-items: center; gap: 0.4rem; }
.swatch {
  display: inline-block; width: 0.85rem; height: 0.85rem;
  border: 1px solid var(--border);
}
.swatch-physical      { background: var(--cell-physical); }
.swatch-life          { background: var(--cell-life); }
.swatch-thinking      { background: var(--cell-thinking); }
.swatch-akasha        { background: var(--cell-akasha); }
.swatch-akasha-light  { background: var(--cell-akasha-light); }
.swatch-inset         { background: var(--cell-inset); }

/* Citations container is no longer a separate block — citations live in the grid */
#citations { display: none; }

/* ── Tooltip ────────────────────────────────────── */
#tooltip {
  position: fixed;
  background: var(--text);
  color: var(--bg);
  padding: 0.5rem 0.75rem;
  font-family: var(--font-ui);
  font-size: 0.78rem;
  line-height: 1.4;
  max-width: 280px;
  border-radius: 2px;
  pointer-events: none;
  z-index: 100;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

/* ── Rail (Source mode) ─────────────────────────── */
#rail {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: var(--rail-w);
  max-width: 100vw;
  background: var(--rail-bg);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  z-index: 50;
  transform: translateX(0);
  transition: transform 300ms ease;
  font-family: var(--font-prose);
  font-size: 0.92rem;
  line-height: 1.55;
}
#rail[hidden] { display: block; transform: translateX(100%); pointer-events: none; visibility: hidden; }

#rail-close {
  position: absolute; top: 0.75rem; right: 0.75rem;
  background: transparent; border: none;
  font-size: 1.6rem; line-height: 1; cursor: pointer;
  color: var(--text-muted); padding: 0.2rem 0.5rem;
}
#rail-close:hover { color: var(--text); }

#rail-content { padding: 2rem 1.75rem 3rem; }
#rail-content h2 { margin: 0 0 0.25rem; font-size: 1.25rem; font-weight: 500; }
#rail-content .gloss { color: var(--text-muted); font-style: italic; margin-bottom: 1.5rem; }
#rail-content section { margin-top: 1.75rem; }
#rail-content section h3 {
  font-family: var(--font-ui); font-size: 0.7rem;
  letter-spacing: 0.08em; text-transform: uppercase;
  font-weight: 500; color: var(--text-muted);
  margin: 0 0 0.6rem;
  border-bottom: 1px solid var(--border); padding-bottom: 0.3rem;
}
#rail-content blockquote {
  margin: 0;
  padding: 0 0.75rem;
  border-left: 2px solid var(--accent);
  color: var(--text);
  font-style: italic;
}
#rail-content blockquote p { margin: 0; }
#rail-content blockquote footer {
  margin-top: 0.6rem; font-style: normal;
  font-size: 0.78rem; color: var(--text-muted);
}
#rail-content a { color: var(--accent); }
#rail-content ul { padding-left: 1.2rem; margin: 0.4rem 0; }
#rail-content li { margin: 0.25rem 0; }
.loading { color: var(--text-muted); font-style: italic; }

/* ── Footer ─────────────────────────────────────── */
#page-footer {
  max-width: var(--col-max);
  margin: 0 auto;
  padding: 2rem 2rem 3rem;
  font-family: var(--font-ui);
  font-size: 0.72rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
}
#page-footer p { margin: 0.25rem 0; }
#page-footer a { color: inherit; }

/* ── Responsive ─────────────────────────────────── */
/* Schema renders at its natural width (sum of column widths in px). The
   .scroll-container handles overflow on narrow viewports — we don't compress
   or shrink the table, since wrapping at narrower widths breaks visual fidelity. */
@media (max-width: 700px) {
  body { font-size: 15px; }
  #page-header { padding: 1.25rem 1rem 0.5rem; }
  .scroll-container { padding: 0 1rem; }
}

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