/* ============================================================
   The Recursive Spiral — Web Edition
   Editorial systems-book. Near-black + warm ivory, one deep teal accent.
   Typography-led, grayscale-safe.
   ============================================================ */

/* ---- Fonts (self-hosted, no external CDN) ----
   Display: an editorial serif. Body: a highly readable serif.
   We load local WOFF2 if present; otherwise fall back to a robust serif stack. */
@font-face {
  font-family: "SpiralDisplay";
  src: local("Iowan Old Style"), local("Palatino Linotype"), local("Palatino"),
       local("Georgia");
  font-weight: 400 700;
  font-display: swap;
}

:root {
  /* Color — light (warm ivory / near-black), one deep teal accent */
  --bg:        #F5F2EA;   /* warm ivory */
  --surface:   #FBF9F3;
  --surface-2: #EEEAE0;
  --border:    #D8D2C4;
  --border-strong: #B9B2A0;
  --text:      #1C1A16;   /* near-black, warm */
  --text-muted:#5F5A50;
  --text-faint:#8B857A;
  --accent:    #0B5C63;   /* deep teal */
  --accent-hover: #0A464C;
  --accent-soft: rgba(11, 92, 99, 0.10);
  --selection: rgba(11, 92, 99, 0.18);

  /* Truth-label neutrals (grayscale-safe; distinguished by weight + text) */
  --label-bg: rgba(28,26,22,0.055);
  --label-border: rgba(28,26,22,0.16);
  --label-accent-bg: rgba(11,92,99,0.10);
  --label-accent-border: rgba(11,92,99,0.32);

  /* Type scale */
  --font-display: "SpiralDisplay", "Iowan Old Style", "Palatino Linotype",
                  Palatino, Georgia, "Times New Roman", serif;
  --font-body: Charter, "Bitstream Charter", "Iowan Old Style", "Sitka Text",
               Cambria, Georgia, serif;
  --font-mono: "SFMono-Regular", "SF Mono", "JetBrains Mono", "Menlo",
               Consolas, monospace;

  --measure: 65ch;
  --step--1: clamp(0.82rem, 0.80rem + 0.1vw, 0.88rem);
  --step-0:  clamp(1.06rem, 1.02rem + 0.2vw, 1.18rem);
  --step-1:  clamp(1.30rem, 1.20rem + 0.5vw, 1.55rem);
  --step-2:  clamp(1.65rem, 1.45rem + 1.0vw, 2.15rem);
  --step-3:  clamp(2.1rem, 1.7rem + 2.0vw, 3.2rem);
  --step-hero: clamp(2.6rem, 1.9rem + 3.6vw, 4.8rem);

  --space-1: 0.5rem;
  --space-2: 1rem;
  --space-3: 1.5rem;
  --space-4: 2.5rem;
  --space-5: 4rem;
  --space-6: 6rem;

  --radius: 3px;
  --sidebar-w: 20rem;
  --topbar-h: 3.75rem;
  --content-max: 44rem;

  --shadow: 0 1px 2px rgba(28,26,22,0.04), 0 8px 28px rgba(28,26,22,0.06);
}

:root[data-theme="dark"] {
  --bg:        #14130F;
  --surface:   #1A1814;
  --surface-2: #221F19;
  --border:    #34302A;
  --border-strong: #4A453C;
  --text:      #E7E2D6;
  --text-muted:#A39D8F;
  --text-faint:#78726657;
  --text-faint:#7C766A;
  --accent:    #57A9B0;
  --accent-hover: #7CC3C9;
  --accent-soft: rgba(87,169,176,0.14);
  --selection: rgba(87,169,176,0.22);
  --label-bg: rgba(231,226,214,0.06);
  --label-border: rgba(231,226,214,0.16);
  --label-accent-bg: rgba(87,169,176,0.12);
  --label-accent-border: rgba(87,169,176,0.34);
  --shadow: 0 1px 2px rgba(0,0,0,0.3), 0 8px 30px rgba(0,0,0,0.4);
}

/* ---- Reset & base ---- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  font-size: var(--step-0);
  line-height: 1.65;
  font-feature-settings: "kern" 1, "liga" 1, "onum" 1;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  transition: background 0.3s ease, color 0.3s ease;
}
::selection { background: var(--selection); }

h1, h2, h3, h4 {
  font-family: var(--font-display);
  font-weight: 600;
  line-height: 1.14;
  letter-spacing: -0.01em;
  color: var(--text);
}
a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hover); text-decoration: underline; text-underline-offset: 2px; }

:focus-visible {
  outline: 2.5px solid var(--accent);
  outline-offset: 2px;
  border-radius: 2px;
}

img { max-width: 100%; height: auto; }
hr { border: none; border-top: 1px solid var(--border); margin: var(--space-4) 0; }

/* ---- Skip link ---- */
.skip-link {
  position: absolute; left: 0.5rem; top: 0.5rem;
  transform: translateY(calc(-100% - 1rem));
  background: var(--accent); color: #fff; padding: 0.6rem 1rem;
  border-radius: var(--radius); z-index: 200; transition: transform 0.2s ease;
}
.skip-link:focus { transform: translateY(0); text-decoration: none; color: #fff; }

/* ---- Top bar ---- */
.topbar {
  position: sticky; top: 0; z-index: 100;
  height: var(--topbar-h);
  display: flex; align-items: center; gap: var(--space-2);
  padding: 0 var(--space-3);
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar .brand { display: flex; align-items: center; gap: 0.65rem; font-family: var(--font-display); font-weight: 600; color: var(--text); }
.topbar .brand:hover { text-decoration: none; }
.topbar .brand svg { width: 30px; height: 30px; color: var(--accent); flex: none; }
.topbar .brand .brand-text { font-size: 1.05rem; letter-spacing: -0.01em; }
.topbar .brand .brand-sub { display: block; font-family: var(--font-body); font-size: 0.68rem; color: var(--text-muted); letter-spacing: 0.02em; font-weight: 400; }
.topbar .spacer { flex: 1; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 2.5rem; height: 2.5rem; border-radius: var(--radius);
  background: transparent; border: 1px solid transparent; color: var(--text-muted);
  cursor: pointer; transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn[aria-pressed="true"] { color: var(--accent); }

.menu-toggle { display: none; }

/* ---- Reading progress ---- */
.progress-track {
  position: fixed; top: var(--topbar-h); left: 0; right: 0; height: 3px;
  background: transparent; z-index: 99;
}
.progress-bar { height: 100%; width: 0; background: var(--accent); transition: width 0.1s linear; }

/* ---- Layout ---- */
.layout { display: grid; grid-template-columns: var(--sidebar-w) 1fr; align-items: start; }
.sidebar-backdrop { grid-column: 1 / -1; grid-row: 1; }  /* keep out of column flow on desktop */
@media (min-width: 861px) { .sidebar-backdrop { display: none !important; } }
.sidebar {
  position: sticky; top: var(--topbar-h);
  height: calc(100vh - var(--topbar-h));
  overflow-y: auto; overscroll-behavior: contain;
  border-right: 1px solid var(--border);
  padding: var(--space-3) var(--space-2) var(--space-5) var(--space-3);
  background: var(--surface);
}
.main { min-width: 0; }

/* ---- TOC in sidebar ---- */
.toc-search { width: 100%; margin-bottom: var(--space-3); }
.toc-search input {
  width: 100%; padding: 0.55rem 0.75rem; font: inherit; font-size: 0.9rem;
  background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text);
}
.toc-search input::placeholder { color: var(--text-faint); }
.toc { font-size: 0.9rem; line-height: 1.4; }
.toc .toc-part { margin-top: var(--space-3); }
.toc .toc-part:first-child { margin-top: 0; }
.toc details { margin: 0; }
.toc summary {
  list-style: none; cursor: pointer; padding: 0.4rem 0.5rem;
  font-family: var(--font-display); font-weight: 600; font-size: 0.82rem;
  letter-spacing: 0.03em; text-transform: uppercase; color: var(--text-muted);
  display: flex; align-items: center; gap: 0.4rem; border-radius: var(--radius);
}
.toc summary::-webkit-details-marker { display: none; }
.toc summary:hover { background: var(--surface-2); color: var(--text); }
.toc summary .caret { transition: transform 0.2s; font-size: 0.7rem; color: var(--text-faint); }
.toc details[open] summary .caret { transform: rotate(90deg); }
.toc ul { list-style: none; margin: 0.15rem 0 0.5rem; padding: 0; }
.toc li a {
  display: block; padding: 0.32rem 0.5rem 0.32rem 1.4rem; border-radius: var(--radius);
  color: var(--text); border-left: 2px solid transparent;
}
.toc li a:hover { background: var(--surface-2); text-decoration: none; }
.toc li a.active { color: var(--accent); border-left-color: var(--accent); background: var(--accent-soft); font-weight: 600; }
.toc li a .ch-num { color: var(--text-faint); font-variant-numeric: tabular-nums; margin-right: 0.4rem; }
.toc li a.active .ch-num { color: var(--accent); }

/* ---- Reading column ---- */
.reader { max-width: var(--content-max); margin: 0 auto; padding: var(--space-5) var(--space-3) var(--space-6); }
.reader-body { max-width: var(--measure); }
.reader-body p, .reader-body ul, .reader-body ol, .reader-body blockquote,
.reader-body .table-wrap, .reader-body .math-block, .reader-body pre { max-width: var(--measure); }

.page-eyebrow {
  font-family: var(--font-body); font-size: var(--step--1); letter-spacing: 0.14em;
  text-transform: uppercase; color: var(--text-muted); margin: 0 0 var(--space-2);
}
.page-title { font-size: var(--step-3); margin: 0 0 var(--space-3); }
.page-lede { font-size: var(--step-1); color: var(--text-muted); font-style: italic; margin: 0 0 var(--space-4); line-height: 1.4; }

.reader-body p { margin: 0 0 var(--space-3); }
.reader-body h3.deep-heading {
  font-size: var(--step-1); margin: var(--space-5) 0 var(--space-2);
  scroll-margin-top: calc(var(--topbar-h) + 1rem); position: relative;
}
.reader-body h3 .anchor {
  position: absolute; left: -1.3rem; color: var(--text-faint); opacity: 0;
  font-weight: 400; transition: opacity 0.15s; text-decoration: none;
}
.reader-body h3:hover .anchor, .reader-body h3 .anchor:focus { opacity: 1; }

.reader-body ul, .reader-body ol { margin: 0 0 var(--space-3); padding-left: 1.4rem; }
.reader-body li { margin-bottom: 0.4rem; }

.reader-body blockquote {
  margin: var(--space-4) 0; padding: var(--space-2) var(--space-3);
  border-left: 3px solid var(--accent); background: var(--surface);
  font-family: var(--font-display); font-size: var(--step-1); font-style: italic; color: var(--text-muted);
}
.reader-body code {
  font-family: var(--font-mono); font-size: 0.86em;
  background: var(--surface-2); padding: 0.1em 0.35em; border-radius: 3px;
}
.reader-body pre.code-block {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: var(--space-2) var(--space-3); overflow-x: auto; margin: var(--space-3) 0;
  font-family: var(--font-mono); font-size: 0.82rem; line-height: 1.55;
}
.reader-body pre.code-block code { background: none; padding: 0; font-size: inherit; }

/* ---- Tables ---- */
.table-wrap { overflow-x: auto; margin: var(--space-3) 0; border: 1px solid var(--border); border-radius: var(--radius); }
table { border-collapse: collapse; width: 100%; font-size: 0.92rem; font-variant-numeric: tabular-nums lining-nums; }
th, td { padding: 0.5rem 0.8rem; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
thead th { background: var(--surface-2); font-family: var(--font-display); font-weight: 600; font-size: 0.85rem; border-bottom: 2px solid var(--border-strong); }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--accent-soft); }

/* ---- Math ---- */
math { font-size: 1.02em; }
.math-block { margin: var(--space-3) 0; text-align: center; overflow-x: auto; }
.math-block math[display="block"] { font-size: 1.14em; }
math mtable { border-spacing: 0.4em 0.2em; }
.math-fallback { font-family: var(--font-mono); font-size: 0.85em; background: var(--surface-2); padding: 0.1em 0.3em; }

/* ---- Truth labels ---- */
.truth-label {
  display: inline-block; font-family: var(--font-body); font-size: 0.66em;
  font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase;
  padding: 0.12em 0.5em; border-radius: 2px; vertical-align: 0.08em;
  background: var(--label-bg); border: 1px solid var(--label-border); color: var(--text);
  white-space: nowrap;
}
.truth-canon { background: var(--label-accent-bg); border-color: var(--label-accent-border); color: var(--accent); }
.truth-fact { border-left: 3px solid var(--text); }
.truth-method { border-left: 3px solid var(--accent); }
.truth-interpretation { border-style: dashed; }
.truth-case { border-radius: 10px; }

/* ---- Footnote refs & endnotes ---- */
sup.fn-ref a { font-size: 0.72em; text-decoration: none; padding: 0 0.1em; font-variant-numeric: tabular-nums; }
sup.fn-ref a:hover { text-decoration: underline; }
.endnotes { margin-top: var(--space-4); font-size: 0.92rem; }
.endnotes ol { padding-left: 1.6rem; }
.endnotes li { margin-bottom: var(--space-2); color: var(--text-muted); scroll-margin-top: calc(var(--topbar-h) + 1rem); }
.endnotes li:target { background: var(--accent-soft); }

/* ---- Chapter nav (prev/next) ---- */
.chapter-nav {
  display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-2);
  margin-top: var(--space-6); padding-top: var(--space-4); border-top: 1px solid var(--border);
  max-width: var(--measure);
}
.chapter-nav a {
  display: block; padding: var(--space-2) var(--space-3); border: 1px solid var(--border);
  border-radius: var(--radius); background: var(--surface); color: var(--text);
  transition: border-color 0.15s, background 0.15s;
}
.chapter-nav a:hover { border-color: var(--accent); background: var(--surface-2); text-decoration: none; }
.chapter-nav a.next { text-align: right; }
.chapter-nav .nav-dir { display: block; font-size: var(--step--1); text-transform: uppercase; letter-spacing: 0.1em; color: var(--text-muted); margin-bottom: 0.25rem; }
.chapter-nav .nav-title { font-family: var(--font-display); font-weight: 600; font-size: 1rem; }
.chapter-nav .placeholder { visibility: hidden; }

/* ---- Figures ---- */
figure { margin: var(--space-4) 0; max-width: var(--measure); }
figure img { border: 1px solid var(--border); border-radius: var(--radius); background: #fff; }
:root[data-theme="dark"] figure img { filter: invert(0.92) hue-rotate(180deg); }
figcaption { font-size: var(--step--1); color: var(--text-muted); margin-top: var(--space-1); font-style: italic; }

/* ---- Status / provenance banner ---- */
.status-badge {
  display: inline-flex; align-items: center; gap: 0.5rem; font-size: var(--step--1);
  padding: 0.35rem 0.75rem; border: 1px solid var(--label-accent-border);
  background: var(--label-accent-bg); border-radius: 100px; color: var(--accent); font-weight: 600;
  letter-spacing: 0.02em;
}
.status-badge .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); }

/* ---- Search modal ---- */
.search-modal { position: fixed; inset: 0; z-index: 300; display: none; }
.search-modal[open], .search-modal.open { display: block; }
.search-backdrop { position: absolute; inset: 0; background: rgba(20,19,15,0.5); backdrop-filter: blur(3px); }
.search-panel {
  position: relative; max-width: 40rem; margin: 8vh auto 0; background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow); overflow: hidden;
}
.search-panel input {
  width: 100%; padding: 1.1rem 1.3rem; font: inherit; font-size: 1.1rem; border: none;
  background: transparent; color: var(--text); border-bottom: 1px solid var(--border);
}
.search-panel input:focus { outline: none; }
.search-results { max-height: 60vh; overflow-y: auto; }
.search-results a {
  display: block; padding: 0.8rem 1.3rem; border-bottom: 1px solid var(--border); color: var(--text);
}
.search-results a:hover, .search-results a.sel { background: var(--accent-soft); text-decoration: none; }
.search-results .r-kind { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.1em; color: var(--text-muted); }
.search-results .r-title { font-family: var(--font-display); font-weight: 600; }
.search-results .r-snippet { font-size: 0.85rem; color: var(--text-muted); margin-top: 0.15rem; }
.search-results mark { background: var(--accent-soft); color: var(--accent); padding: 0 0.1em; border-radius: 2px; }
.search-hint { padding: 0.6rem 1.3rem; font-size: 0.78rem; color: var(--text-faint); border-top: 1px solid var(--border); display: flex; gap: 1rem; }
.search-hint kbd { font-family: var(--font-mono); font-size: 0.72rem; background: var(--surface-2); border: 1px solid var(--border); border-radius: 3px; padding: 0.05em 0.4em; }

/* ============ Homepage ============ */
.home-wrap { max-width: 60rem; margin: 0 auto; padding: var(--space-5) var(--space-3) var(--space-6); }
.hero { text-align: center; padding: var(--space-5) 0 var(--space-4); }
.hero .mark { width: 96px; height: 96px; color: var(--accent); margin: 0 auto var(--space-3); }
.hero h1 { font-size: var(--step-hero); margin: 0 0 var(--space-2); letter-spacing: -0.02em; }
.hero .subtitle { font-size: var(--step-1); color: var(--text-muted); font-style: italic; margin: 0 auto var(--space-3); max-width: 34ch; }
.hero .byline { font-family: var(--font-body); font-size: var(--step-0); color: var(--text); letter-spacing: 0.02em; }
.hero .byline .imprint { color: var(--text-muted); }
.hero .edition { font-size: var(--step--1); color: var(--text-faint); letter-spacing: 0.08em; text-transform: uppercase; margin-top: var(--space-2); }
.hero-cta { display: flex; gap: var(--space-2); justify-content: center; flex-wrap: wrap; margin-top: var(--space-4); }
.btn {
  display: inline-flex; align-items: center; gap: 0.5rem; padding: 0.7rem 1.4rem;
  border-radius: var(--radius); font-family: var(--font-body); font-weight: 600; font-size: 0.98rem;
  border: 1px solid var(--accent); background: var(--accent); color: #fff; cursor: pointer;
  transition: background 0.15s, transform 0.15s;
}
.btn:hover { background: var(--accent-hover); text-decoration: none; color: #fff; transform: translateY(-1px); }
.btn.secondary { background: transparent; color: var(--accent); }
.btn.secondary:hover { background: var(--accent-soft); color: var(--accent); }

.home-section { margin-top: var(--space-6); }
.home-section h2 { font-size: var(--step-2); margin: 0 0 var(--space-3); }
.blurb { max-width: 65ch; margin: 0 auto var(--space-3); color: var(--text); }
.blurb.center { text-align: center; }

.label-legend { display: grid; grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr)); gap: var(--space-2); margin-top: var(--space-3); }
.label-legend .lg-item { padding: var(--space-2) var(--space-3); border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.label-legend .lg-item p { margin: 0.5rem 0 0; font-size: 0.9rem; color: var(--text-muted); }

.part-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr)); gap: var(--space-2); margin-top: var(--space-3); }
.part-card { padding: var(--space-3); border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); transition: border-color 0.15s, transform 0.15s; display: block; color: var(--text); }
.part-card:hover { border-color: var(--accent); transform: translateY(-2px); text-decoration: none; }
.part-card .pc-eyebrow { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.12em; color: var(--text-muted); }
.part-card .pc-title { font-family: var(--font-display); font-weight: 600; font-size: 1.15rem; margin: 0.4rem 0; }
.part-card .pc-count { font-size: 0.82rem; color: var(--text-faint); }

/* ============ TOC page ============ */
.toc-page { max-width: 52rem; margin: 0 auto; padding: var(--space-5) var(--space-3) var(--space-6); }
.toc-page .toc-part-block { margin-bottom: var(--space-4); }
.toc-page .toc-part-head { font-family: var(--font-display); font-size: var(--step-1); color: var(--text); border-bottom: 1px solid var(--border); padding-bottom: 0.4rem; margin: 0 0 var(--space-2); }
.toc-page ol { list-style: none; padding: 0; margin: 0; }
.toc-page ol li { display: flex; align-items: baseline; gap: 0.8rem; padding: 0.45rem 0; border-bottom: 1px dotted var(--border); }
.toc-page ol li a { flex: 1; color: var(--text); font-size: 1.05rem; }
.toc-page ol li a:hover { color: var(--accent); }
.toc-page .ci { color: var(--text-faint); font-variant-numeric: tabular-nums; min-width: 2.5rem; font-family: var(--font-display); }

/* ---- Footer ---- */
.site-footer { border-top: 1px solid var(--border); padding: var(--space-4) var(--space-3); background: var(--surface); font-size: 0.85rem; color: var(--text-muted); }
.site-footer .fwrap { max-width: 60rem; margin: 0 auto; display: flex; flex-wrap: wrap; gap: var(--space-3); justify-content: space-between; }
.site-footer a { color: var(--text-muted); }
.site-footer a:hover { color: var(--accent); }
.site-footer .prov { max-width: 40rem; font-size: 0.8rem; color: var(--text-faint); line-height: 1.5; }

/* ---- Responsive ---- */
@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }
  .menu-toggle { display: inline-flex; }
  .sidebar {
    position: fixed; top: var(--topbar-h); left: 0; bottom: 0; width: min(85vw, 22rem);
    z-index: 90; transform: translateX(-105%); transition: transform 0.25s ease;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-backdrop { display: none; position: fixed; inset: var(--topbar-h) 0 0; background: rgba(20,19,15,0.4); z-index: 89; }
  .sidebar-backdrop.open { display: block; }
  .topbar .brand .brand-sub { display: none; }
  .chapter-nav { grid-template-columns: 1fr; }
}

/* ---- Print ---- */
@media print {
  .topbar, .sidebar, .sidebar-backdrop, .progress-track, .chapter-nav,
  .search-modal, .icon-btn, .menu-toggle, .hero-cta, .skip-link { display: none !important; }
  .layout { display: block; }
  body { background: #fff; color: #000; font-size: 11pt; }
  .reader, .home-wrap, .toc-page { max-width: none; padding: 0; }
  .reader-body { max-width: none; }
  a { color: #000; text-decoration: none; }
  .reader-body h3.deep-heading { page-break-after: avoid; }
  table, figure, pre, blockquote { page-break-inside: avoid; }
  .truth-label { border: 1px solid #000 !important; background: none !important; color: #000 !important; }
  sup.fn-ref a::after { content: ""; }
  :root[data-theme="dark"] figure img { filter: none; }
}
