:root {
  /* palette (light) */
  --accent: #5c5edc;
  --accent-dark: #3e3e98;
  --muted: #8687b5;
  --nav-strong: #5b5c94;
  --ink: #292929;
  --ink-soft: #57585c;
  --ink-2: #3f4046;
  --bg: #f4f4f7;
  --sidebar-bg: #e6e6ec;
  --field-bg: #ffffff;
  --rule: #d3d3e0;
  --focus-ring: #9a9bf5;
  --twisty: #9a9ac0;
  --mark-bg: #fff1a8;
  --mark-ink: inherit;
  --hit-bg: #ececf2;
  --hit-ink: #55565e;
  --s-h3: #6c6d76;
  --scrim: rgba(20, 20, 40, 0.35);
  --on-accent: #ffffff;
  --flash-bg: #e5e5fb;

  /* layout & reading */
  --sidebar-width: 320px;
  --reading-scale: 1;
  --measure: none;
  --serif: Georgia, Cambria, "Times New Roman", Times, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

/* dark palette: system default, unless the reader forced light */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --accent: #9698ee;
    --accent-dark: #b9baf5;
    --muted: #8a8ab0;
    --nav-strong: #b7b8e0;
    --ink: #d9d9e3;
    --ink-soft: #a7a7b6;
    --ink-2: #c2c2cf;
    --bg: #16161c;
    --sidebar-bg: #1d1d26;
    --field-bg: #24242f;
    --rule: #33333f;
    --focus-ring: #6a6cc4;
    --twisty: #6a6a82;
    --mark-bg: #6f5a1c;
    --mark-ink: #f7ecc6;
    --hit-bg: #24242f;
    --hit-ink: #a7a7b6;
    --s-h3: #9a9ab0;
    --scrim: rgba(0, 0, 0, 0.55);
    --on-accent: #16161c;
    --flash-bg: #33335c;
  }
}

/* dark palette: forced by the reader (wins in both directions) */
:root[data-theme="dark"] {
  --accent: #9698ee;
  --accent-dark: #b9baf5;
  --muted: #8a8ab0;
  --nav-strong: #b7b8e0;
  --ink: #d9d9e3;
  --ink-soft: #a7a7b6;
  --ink-2: #c2c2cf;
  --bg: #16161c;
  --sidebar-bg: #1d1d26;
  --field-bg: #24242f;
  --rule: #33333f;
  --focus-ring: #6a6cc4;
  --twisty: #6a6a82;
  --mark-bg: #6f5a1c;
  --mark-ink: #f7ecc6;
  --hit-bg: #24242f;
  --hit-ink: #a7a7b6;
  --s-h3: #9a9ab0;
  --scrim: rgba(0, 0, 0, 0.55);
  --on-accent: #16161c;
  --flash-bg: #33335c;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
html, body { margin: 0; padding: 0; }

body {
  display: flex;
  align-items: stretch;
  min-height: 100vh;
  font-family: var(--serif);
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-dark); }
mark { background: var(--mark-bg); color: var(--mark-ink); padding: 0 .05em; border-radius: 2px; }

/* ---------- sidebar ---------- */
#sidebar {
  flex: 0 0 var(--sidebar-width);
  position: sticky;
  top: 0;
  align-self: flex-start;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--rule);
}

/* drag handle between the menu and the content */
#resizer {
  flex: none;
  width: 7px;
  margin: 0 -4px 0 -3px;
  position: sticky;
  top: 0;
  align-self: flex-start;
  height: 100vh;
  z-index: 15;
  cursor: col-resize;
  touch-action: none;
  background: transparent;
  transition: background 0.15s;
}
#resizer:hover,
#resizer:focus-visible,
#resizer.dragging {
  background: var(--accent);
  outline: none;
}
body.resizing { cursor: col-resize; user-select: none; }

.brand {
  padding: 1.5rem 1.25rem 0.9rem;
  font-family: var(--sans);
  white-space: nowrap;
}
.brand a {
  font-weight: 800;
  letter-spacing: 0.3em;
  text-transform: uppercase;
  font-size: 1.05rem;
  color: var(--accent);
}
#version {
  margin-left: 0.5em;
  font-size: 0.7rem;
  color: var(--muted);
  letter-spacing: normal;
}

#search-form {
  padding: 0 1.25rem 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}
.search-row {
  display: flex;
  gap: 0.4rem;
}
#search-input {
  flex: 1 1 auto;
  min-width: 0;
  padding: 0.45em 0.6em;
  border: 2px solid var(--accent);
  border-radius: 3px;
  background: var(--field-bg);
  color: var(--ink);
  font: inherit;
  outline: none;
}
#search-input:focus { border-color: var(--focus-ring); }
#search-go {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 0.8em;
  border: 2px solid var(--accent);
  border-radius: 3px;
  background: var(--accent);
  color: var(--field-bg);
  font: inherit;
  cursor: pointer;
}
#search-go:hover { background: var(--accent-dark); border-color: var(--accent-dark); }
#search-go:focus-visible { outline: 2px solid var(--focus-ring); outline-offset: 1px; }
#search-scope {
  font: inherit;
  font-size: 0.78rem;
  padding: 0.25em 0.3em;
  border: 1px solid var(--rule);
  border-radius: 3px;
  background: var(--sidebar-bg);
  color: var(--muted);
}

#quick-links {
  display: flex;
  gap: 1rem;
  padding: 0 1.25rem 0.7rem;
  font-family: var(--sans);
  font-size: 0.78rem;
}
#quick-links button,
#quick-links a {
  border: 0;
  background: none;
  padding: 0;
  font: inherit;
  color: var(--accent);
  cursor: pointer;
}

#nav {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 0.25rem 0.75rem 2rem;
  font-family: var(--sans);
  font-size: 0.82rem;
  line-height: 1.4;
}

.nav-list { list-style: none; margin: 0; padding: 0; }
.nav-list .nav-list {
  display: none;
  margin-left: 0.65em;
  padding-left: 0.35em;
  border-left: 1px solid var(--rule);
}
.nav-list li.open > .nav-list { display: block; }

.nav-row { display: flex; align-items: baseline; gap: 0.15em; }

.nav-link {
  flex: 1 1 auto;
  min-width: 0;
  padding: 0.3em 0.4em 0.3em 0;
  color: var(--muted);
  overflow-wrap: anywhere;
}
.nav-link:hover { color: var(--accent); }
.nav-link.current { color: var(--accent); font-weight: 700; }
.depth-0 > li > .nav-row > .nav-link { color: var(--nav-strong); font-weight: 600; }

.twisty, .twisty-spacer {
  flex: 0 0 1.25em;
  width: 1.25em;
  height: 1.25em;
  padding: 0;
  border: 0;
  background: none;
  color: var(--twisty);
  font: inherit;
  line-height: 1.25em;
  text-align: center;
  cursor: pointer;
  transition: transform 0.15s ease;
}
.twisty:hover { color: var(--accent); }
li.open > .nav-row > .twisty { transform: rotate(90deg); }

/* ---------- content ---------- */
#content {
  flex: 1 1 auto;
  min-width: 0;
  position: relative;
  padding: 2.5rem 2rem 6rem;
}
#content > * { max-width: var(--measure); }

.info {
  padding: 18vh 1em 0;
  text-align: center;
  color: var(--ink-soft);
  font-weight: 700;
}

.home {
  padding: 22vh 1em 0;
  text-align: center;
}
.home h1 {
  margin: 0;
  font-size: 2.4rem;
  font-weight: 800;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--accent);
}
.home-version {
  margin: 0.6em 0 0;
  font-family: var(--sans);
  font-size: 0.85rem;
  color: var(--muted);
}
.home-actions {
  margin-top: 1.6rem;
  font-family: var(--sans);
  font-size: 0.95rem;
  display: flex;
  gap: 1rem;
  justify-content: center;
  align-items: baseline;
}
.home-actions button {
  border: 0;
  background: none;
  padding: 0;
  font: inherit;
  color: var(--accent);
  cursor: pointer;
}
.home-recent {
  margin: 3rem auto 0;
  max-width: 30rem;
  text-align: left;
  font-family: var(--sans);
}
.home-recent h2 {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin: 0 0 0.6em;
}
.home-recent ul { list-style: none; margin: 0; padding: 0; }
.home-recent li { margin: 0.3em 0; font-size: 0.9rem; }

.doc { font-size: calc(1.09rem * var(--reading-scale)); line-height: 1.62; }
.doc h1 { font-size: 1.7rem; line-height: 1.25; margin: 0 0 0.7em; }
.doc h2 {
  font-size: 1.32rem;
  line-height: 1.3;
  margin: 1.7em 0 0.15em;
  scroll-margin-top: 1.5rem;
}
.doc h3 {
  font-size: 1.08rem;
  color: var(--ink-2);
  margin: 1.4em 0 0.15em;
  scroll-margin-top: 1.5rem;
}
.doc h2:first-child, .doc h3:first-child { margin-top: 0; }
.doc p { margin: 0 0 0.95em; }
.doc .para { scroll-margin-top: 5rem; }
.doc em.emphasis { font-style: normal; text-decoration: underline; text-underline-offset: 2px; }
.doc .para.flash { animation: flash 1.7s ease-out; border-radius: 3px; }
@keyframes flash {
  0%, 25% { background: var(--flash-bg); box-shadow: 0 0 0 6px var(--flash-bg); }
  100% { background: transparent; box-shadow: 0 0 0 6px transparent; }
}
.doc a.source {
  font-family: var(--sans);
  font-size: 0.85rem;
  text-decoration: underline;
}

.section-list {
  list-style: none;
  padding: 0;
  font-family: var(--sans);
  font-size: 0.95rem;
  line-height: 1.5;
}
.section-list li { margin: 0.35em 0; }

/* ---------- search results ---------- */
.search { font-family: var(--sans); }
.search-head {
  color: var(--accent);
  border-bottom: 1px solid var(--accent);
  padding-bottom: 0.4em;
  margin: 0 0 1.2em;
  font-size: 0.95rem;
}
.s-doc { margin-bottom: 1.7em; }
.s-doc h2 {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--nav-strong);
  margin: 0 0 0.4em;
}
.s-doc h3 {
  font-size: 0.88rem;
  color: var(--s-h3);
  font-weight: 600;
  margin: 0.9em 0 0.3em;
}
.s-hit {
  display: block;
  padding: 0.4em 0 0.4em 1em;
  margin-left: 0.2em;
  border-left: 2px solid var(--rule);
  color: var(--hit-ink);
  font-size: 0.9rem;
  line-height: 1.5;
}
.s-hit:hover,
.s-hit.selected { color: var(--ink); border-left-color: var(--accent); background: var(--hit-bg); }

.doc mark { scroll-margin-top: 6rem; }

/* ---------- breadcrumb + section nav ---------- */
.crumbs {
  font-family: var(--sans);
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 1.5rem;
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.35em;
}
.crumbs a { color: var(--muted); }
.crumbs a:hover { color: var(--accent); }
.crumb-sep { color: var(--rule); }

.sec-nav {
  display: flex;
  justify-content: space-between;
  gap: 1.5rem;
  margin-top: 3.5rem;
  padding-top: 1.2rem;
  border-top: 1px solid var(--rule);
  font-family: var(--sans);
  font-size: 0.9rem;
}
.sec-nav a { max-width: 46%; }
.sec-nav a:last-child { margin-left: auto; text-align: right; }

/* ---------- document tags + related docs ---------- */
.doc-tags {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: -0.6em 0 1.8em;
  padding: 0;
  font-family: var(--sans);
  font-size: 0.78rem;
}
.doc-tags li {
  padding: 0.2em 0.75em;
  border: 1px solid var(--rule);
  border-radius: 999px;
  color: var(--ink-soft);
}
.doc-tags a { color: var(--ink-soft); }
.doc-tags a:hover { color: var(--accent); }

.related-docs {
  margin-top: 2.5rem;
  padding-top: 1.2rem;
  border-top: 1px solid var(--rule);
  font-family: var(--sans);
}
.related-docs h2 {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin: 0 0 0.6em;
}
.related-docs ul { list-style: none; margin: 0; padding: 0; font-size: 0.92rem; line-height: 1.75; }

/* ---------- author index ---------- */
.author-index,
.author-page { font-family: var(--sans); }
.author-index h1,
.author-page h1 {
  font-family: var(--serif);
  font-size: 1.7rem;
  line-height: 1.25;
  margin: 0 0 1em;
}
.author-index .count,
.author-page .count {
  font-size: 0.75rem;
  font-weight: 400;
  color: var(--muted);
}

.ai-group { margin: 0 0 1.7em; }
.ai-group h2 {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin: 0 0 0.5em;
  padding-bottom: 0.3em;
  border-bottom: 1px solid var(--rule);
}
.ai-group ul {
  list-style: none;
  margin: 0;
  padding: 0;
  columns: 2;
  column-gap: 2.5rem;
  font-size: 0.95rem;
  line-height: 1.65;
}
.ai-group li { break-inside: avoid; }

.author-page .ap-lead {
  margin: -0.4em 0 1.8em;
  font-size: 1rem;
}
.ap-group { margin: 0 0 1.6em; }
.ap-group h2 {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin: 0 0 0.5em;
}
.ap-group ul {
  list-style: none;
  margin: 0;
  padding: 0;
  font-size: 0.98rem;
  line-height: 1.75;
}
.author-page .ctx { color: var(--muted); }

/* ---------- reading controls ---------- */
#reading-controls {
  flex: none;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.55rem 1.25rem;
  border-top: 1px solid var(--rule);
  font-family: var(--sans);
}
.rc-group { display: flex; gap: 0.1rem; }
#reading-controls button {
  min-width: 1.9em;
  height: 1.9em;
  padding: 0 0.35em;
  border: 1px solid var(--rule);
  border-radius: 3px;
  background: var(--field-bg);
  color: var(--muted);
  font: inherit;
  font-size: 0.78rem;
  line-height: 1;
  cursor: pointer;
}
.rc-group button:not(:first-child) { border-left: 0; }
#reading-controls button:hover { color: var(--accent); }
#reading-controls button[aria-pressed="true"] {
  color: var(--on-accent);
  background: var(--accent);
  border-color: var(--accent);
}

/* ---------- mobile ---------- */
#topbar { display: none; }

#menu-toggle {
  flex: none;
  width: 2.3rem;
  height: 2.3rem;
  font-size: 1.1rem;
  border: 0;
  border-radius: 5px;
  background: var(--accent);
  color: var(--on-accent);
  cursor: pointer;
}

#topbar-brand {
  font-family: var(--sans);
  font-weight: 800;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  font-size: 1rem;
  color: var(--accent);
}
#topbar-version {
  margin-left: 0.6em;
  font-size: 0.65rem;
  font-weight: 400;
  letter-spacing: normal;
  color: var(--muted);
}

#sidebar-scrim {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 18;
  background: var(--scrim);
}

@media (max-width: 640px) {
  body { display: block; }
  #resizer { display: none; }

  #topbar {
    display: flex;
    align-items: center;
    gap: 0.8rem;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 25;
    padding: 0.55rem 1rem;
    background: var(--sidebar-bg);
    border-bottom: 1px solid var(--rule);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12);
  }
  body.menu-open #topbar { display: none; }

  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 20;
    width: min(84vw, var(--sidebar-width));
    height: 100vh;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
  }
  body.menu-open #sidebar { transform: none; }
  body.menu-open #sidebar-scrim { display: block; }

  #content { padding: 4.5rem 1.25rem 5rem; }
  .ai-group ul { columns: 1; }
}
