/* ============================================================
   PERSONAL WEBSITE — MASTER STYLESHEET
   Design system: Research Brand (Practical Typography x Gwern.net)
   Site-specific: Homepage grid, navigation, settings toolbar,
                  search, help, reader mode, link previews
   ============================================================ */

/* --- CSS Custom Properties (Design Tokens) --- */
:root {
  /* Palette — light, airy, and restrained. Practical Typography aesthetic. */
  --bg-primary:       #FEFEFE;
  --bg-secondary:     #F9F8F6;
  --bg-tertiary:      #F4F3F0;
  --bg-highlight:     #FEFCF0;
  --bg-aside:         #F5FAF7;

  --text-primary:     #333333;
  --text-secondary:   #5C5C5C;
  --text-tertiary:    #8E8E8E;
  --text-muted:       #B0B0B0;

  --accent-link:      #B44040;
  --accent-link-hover:#CC5555;
  --accent-dropcap:   #B44040;
  --accent-highlight: #E0C66A;
  --accent-green:     #5A9E7E;
  --accent-blue:      #5B8FA8;

  --border-light:     #E5E2DC;
  --border-medium:    #CCC8C0;

  --hover-bg:         #FDF6F4;
  --mark-bg:          #FFF8DC;
  --callout-border:   #EDE8CC;

  /* Typography */
  --font-serif:       "Source Serif 4", "Apple Garamond", "Baskerville", "Libre Baskerville", "Georgia", "Times New Roman", serif;
  --font-sans:        "Source Sans 3", "Helvetica Neue", "Helvetica", "Segoe UI", "Trebuchet MS", sans-serif;
  --font-mono:        "IBM Plex Mono", "JetBrains Mono", "Fira Code", "Liberation Mono", "Consolas", monospace;

  --font-serif-fallback: "Apple Garamond", "Baskerville", "Libre Baskerville", "Georgia", "Times New Roman", serif;
  --font-sans-fallback:  "Helvetica Neue", "Helvetica", "Segoe UI", "Trebuchet MS", sans-serif;
  --font-mono-fallback:  "JetBrains Mono", "Fira Code", "Liberation Mono", "Consolas", monospace;

  /* Scale */
  --size-base:        19px;
  --size-small:       15px;
  --size-caption:     13px;
  --size-h1:          2.4rem;
  --size-h2:          1.8rem;
  --size-h3:          1.35rem;
  --size-h4:          1.1rem;

  /* Spacing */
  --line-height:      1.58;
  --line-height-tight:1.3;
  --paragraph-spacing:1.2em;
  --section-spacing:  3rem;
  --body-max-width:   720px;
  --body-wide:        920px;
  --side-padding:     24px;
}

/* --- Dark Mode (automatic via media query) --- */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg-primary:       #1E1E1E;
    --bg-secondary:     #262625;
    --bg-tertiary:      #2E2E2C;
    --bg-highlight:     #2C2A1F;
    --bg-aside:         #1F2C26;

    --text-primary:     #D5D3CE;
    --text-secondary:   #A5A29C;
    --text-tertiary:    #7A7874;
    --text-muted:       #555350;

    --accent-link:      #D88E7A;
    --accent-link-hover:#E8A898;
    --accent-dropcap:   #CC5555;
    --accent-highlight: #D4B65A;
    --accent-green:     #7CC4A0;
    --accent-blue:      #82B8D0;

    --border-light:     #3A3A3A;
    --border-medium:    #4A4A4A;

    --hover-bg:         #2C2220;
    --mark-bg:          #3D3518;
    --callout-border:   #4A4530;
  }
}

/* --- Dark Mode (manual toggle) --- */
:root[data-theme="dark"] {
  --bg-primary:       #1E1E1E;
  --bg-secondary:     #262625;
  --bg-tertiary:      #2E2E2C;
  --bg-highlight:     #2C2A1F;
  --bg-aside:         #1F2C26;

  --text-primary:     #D5D3CE;
  --text-secondary:   #A5A29C;
  --text-tertiary:    #7A7874;
  --text-muted:       #555350;

  --accent-link:      #D88E7A;
  --accent-link-hover:#E8A898;
  --accent-dropcap:   #CC5555;
  --accent-highlight: #D4B65A;
  --accent-green:     #7CC4A0;
  --accent-blue:      #82B8D0;

  --border-light:     #3A3A3A;
  --border-medium:    #4A4A4A;

  --hover-bg:         #2C2220;
  --mark-bg:          #3D3518;
  --callout-border:   #4A4530;
}

/* --- Light Mode (manual toggle, overrides dark OS preference) --- */
:root[data-theme="light"] {
  --bg-primary:       #FEFEFE;
  --bg-secondary:     #F9F8F6;
  --bg-tertiary:      #F4F3F0;
  --bg-highlight:     #FEFCF0;
  --bg-aside:         #F5FAF7;

  --text-primary:     #333333;
  --text-secondary:   #5C5C5C;
  --text-tertiary:    #8E8E8E;
  --text-muted:       #B0B0B0;

  --accent-link:      #B44040;
  --accent-link-hover:#CC5555;
  --accent-dropcap:   #B44040;
  --accent-highlight: #E0C66A;
  --accent-green:     #5A9E7E;
  --accent-blue:      #5B8FA8;

  --border-light:     #E5E2DC;
  --border-medium:    #CCC8C0;

  --hover-bg:         #FDF6F4;
  --mark-bg:          #FFF8DC;
  --callout-border:   #EDE8CC;
}

/* ============================================================
   PALETTE VARIANTS
   Activated via data-palette attribute on <html>.
   Default (no attribute) = "Research" palette (warm red accents).
   ============================================================ */

/* --- Butterick palette (Practical Typography) --- */
:root[data-palette="butterick"] {
  --accent-link:      #993333;
  --accent-link-hover:#AA3333;
  --accent-dropcap:   #993333;
  --accent-highlight: #CCBBAA;
  --accent-green:     #667766;
  --accent-blue:      #667788;
  --hover-bg:         #FBF3F3;
  --mark-bg:          #FFF5EE;
  --callout-border:   #DDD5CC;
}
@media (prefers-color-scheme: dark) {
  :root[data-palette="butterick"]:not([data-theme="light"]) {
    --accent-link:      #D4937D;
    --accent-link-hover:#E0A898;
    --accent-dropcap:   #BB6655;
    --accent-highlight: #8C7A6A;
    --accent-green:     #7A9A7A;
    --accent-blue:      #8A9AAA;
    --hover-bg:         #2E2422;
    --mark-bg:          #332A24;
    --callout-border:   #4A4035;
  }
}
:root[data-palette="butterick"][data-theme="dark"] {
  --accent-link:      #D4937D;
  --accent-link-hover:#E0A898;
  --accent-dropcap:   #BB6655;
  --accent-highlight: #8C7A6A;
  --accent-green:     #7A9A7A;
  --accent-blue:      #8A9AAA;
  --hover-bg:         #2E2422;
  --mark-bg:          #332A24;
  --callout-border:   #4A4035;
}

/* --- Academic palette (traditional, conservative) --- */
:root[data-palette="academic"] {
  --accent-link:      #2B5C8A;
  --accent-link-hover:#3A6FA0;
  --accent-dropcap:   #2B5C8A;
  --accent-highlight: #C4B078;
  --accent-green:     #4A7A5A;
  --accent-blue:      #4A6A8A;
  --hover-bg:         #F0F4F8;
  --mark-bg:          #FFF8E0;
  --callout-border:   #D8D0B0;
}
@media (prefers-color-scheme: dark) {
  :root[data-palette="academic"]:not([data-theme="light"]) {
    --accent-link:      #6EAAD0;
    --accent-link-hover:#88BEE0;
    --accent-dropcap:   #5090B8;
    --accent-highlight: #9A8A5A;
    --accent-green:     #6AAA7A;
    --accent-blue:      #6A9ABB;
    --hover-bg:         #1E2630;
    --mark-bg:          #302A1E;
    --callout-border:   #3A3828;
  }
}
:root[data-palette="academic"][data-theme="dark"] {
  --accent-link:      #6EAAD0;
  --accent-link-hover:#88BEE0;
  --accent-dropcap:   #5090B8;
  --accent-highlight: #9A8A5A;
  --accent-green:     #6AAA7A;
  --accent-blue:      #6A9ABB;
  --hover-bg:         #1E2630;
  --mark-bg:          #302A1E;
  --callout-border:   #3A3828;
}

/* --- Fallback font mode (activated via data-fonts="fallback") --- */
:root[data-fonts="fallback"] {
  --font-serif: var(--font-serif-fallback);
  --font-sans:  var(--font-sans-fallback);
  --font-mono:  var(--font-mono-fallback);
}

/* ============================================================
   BASE RESET & BODY
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html {
  font-size: var(--size-base);
  scroll-behavior: smooth;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

body {
  font-family: var(--font-serif);
  color: var(--text-primary);
  background: var(--bg-primary);
  line-height: var(--line-height);
  max-width: 1000px;
  margin: 0 auto;
  padding: 0;
}

/* ============================================================
   TYPOGRAPHY
   ============================================================ */
h1, h2, h3, h4 {
  font-family: var(--font-sans);
  font-weight: 700;
  line-height: var(--line-height-tight);
  color: var(--text-primary);
  letter-spacing: -0.01em;
}

h1 {
  font-size: var(--size-h1);
  margin: 0 0 0.4em 0;
  font-weight: 800;
  text-align: center;
}

h2 {
  font-size: var(--size-h2);
  margin: var(--section-spacing) 0 0.6em 0;
  padding-top: 1rem;
  border-top: 1px solid var(--border-light);
}

h3 {
  font-size: var(--size-h3);
  margin: 2rem 0 0.5em 0;
  color: var(--text-secondary);
}

h4 {
  font-size: var(--size-h4);
  margin: 1.5rem 0 0.4em 0;
  font-weight: 600;
  font-variant: small-caps;
  letter-spacing: 0.03em;
  color: var(--text-secondary);
}

p {
  margin-bottom: var(--paragraph-spacing);
  hanging-punctuation: first last;
}

p:last-child {
  margin-bottom: 0;
}

/* Small caps for emphasis */
.sc {
  font-variant: small-caps;
  letter-spacing: 0.05em;
  font-size: 1.05em;
}

/* Drop cap for opening paragraphs */
.drop-cap::first-letter {
  float: left;
  font-size: 3.5em;
  line-height: 0.8;
  padding: 0.05em 0.1em 0 0;
  font-weight: 700;
  color: var(--accent-dropcap);
  font-family: var(--font-serif);
}

strong { font-weight: 650; }
em { font-style: italic; }

/* New thought — small caps for section openers (Tufte pattern) */
.newthought {
  font-variant: small-caps;
  font-size: 1.1em;
  letter-spacing: 0.05em;
}

/* ============================================================
   LINKS
   ============================================================ */
a {
  color: var(--accent-link);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: all 0.2s ease;
}

a:hover {
  color: var(--accent-link-hover);
  border-bottom-color: var(--accent-link-hover);
  background: var(--hover-bg);
  border-radius: 2px;
  padding: 0 0.1em;
}

/* Internal links — Gwern-style dotted underline */
.internal-link {
  border-bottom: 1px dotted var(--accent-link);
  text-decoration: none;
  transition: color 0.15s ease, border-color 0.15s ease, background-color 0.15s ease;
  position: relative;
}

.internal-link:hover {
  color: var(--accent-link-hover);
  background-color: var(--hover-bg);
  border-bottom-style: solid;
}

/* External-link indicator — a small ↗ on links that leave the site.
   Scoped to <main> so it never decorates the header social icons or logo,
   and skips any link that already wraps an icon (svg/img). */
main a[href^="http"]:not(:has(svg)):not(:has(img))::after {
  content: "\2197";
  display: inline-block;
  font-family: var(--font-sans);
  font-size: 0.62em;
  line-height: 1;
  vertical-align: text-top;
  margin-left: 0.12em;
  color: var(--text-muted);
  border-bottom: none;
}

/* ============================================================
   SUBTITLE & METADATA
   ============================================================ */
.subtitle {
  font-family: var(--font-sans);
  font-size: var(--size-small);
  color: var(--text-tertiary);
  font-weight: 400;
  margin: 0 0 1.5em 0;
  line-height: 1.4;
  text-align: left;
  display: block;
}

/* Status badge next to a title (e.g. `badge: working draft`) */
.page-badge {
  display: inline-block;
  margin: 0 0 1.5em 0;
  padding: 0.2em 0.7em;
  font-family: var(--font-sans);
  font-size: var(--size-caption);
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--accent-dropcap);
  background: var(--mark-bg);
  border: 1px solid var(--callout-border);
  border-radius: 999px;
  line-height: 1.3;
  vertical-align: middle;
}

/* Multiple badges sit in a row with a small gap */
.page-badge + .page-badge {
  margin-left: 0.4em;
}

/* "synthesized" — cool sage tone to distinguish from the amber "working draft" */
.page-badge--synthesized {
  color: var(--accent-green);
  background: color-mix(in srgb, var(--accent-green) 13%, transparent);
  border-color: color-mix(in srgb, var(--accent-green) 38%, transparent);
}

/* When a badge is followed by a subtitle, tighten the badge's bottom gap */
.page-badge + .subtitle {
  margin-top: -0.6em;
}

.metadata {
  font-family: var(--font-sans);
  font-size: var(--size-small);
  color: var(--text-tertiary);
  margin-bottom: 2rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border-light);
}

/* ============================================================
   ABSTRACT / SUMMARY BOX
   ============================================================ */
.abstract {
  background: var(--bg-secondary);
  border-left: 2px solid var(--border-medium);
  padding: 1.2em 1.5em;
  margin: 1.5em 0 2em 0;
  font-size: 0.95rem;
  color: var(--text-secondary);
  border-radius: 0 3px 3px 0;
}
.abstract p:last-child { margin-bottom: 0; }

/* ============================================================
   BLOCKQUOTES
   ============================================================ */
blockquote {
  border-left: 2px solid var(--accent-highlight);
  padding: 0.8em 1.2em;
  margin: 1.5em 0;
  background: var(--bg-highlight);
  color: var(--text-secondary);
  font-style: italic;
  border-radius: 0 3px 3px 0;
}

blockquote p {
  text-align: left;
}

blockquote p:last-child { margin-bottom: 0; }

blockquote footer, blockquote cite {
  display: block;
  font-style: normal;
  font-size: 0.9rem;
  margin-top: 0.5rem;
  color: var(--text-muted);
}

blockquote footer::before {
  content: '\2014\00a0';
}

/* Epigraph — centered opening quote */
.epigraph {
  font-style: italic;
  margin: 2rem 3rem;
  border-left: none;
  text-align: right;
  padding: 0;
  color: var(--text-secondary);
  background: none;
}

/* ============================================================
   LISTS
   ============================================================ */
ul, ol {
  margin: 0 0 var(--paragraph-spacing) 1.8em;
}

li {
  margin-bottom: 0.4em;
}

li > ul, li > ol {
  margin-top: 0.4em;
  margin-bottom: 0;
}

/* ============================================================
   CODE
   ============================================================ */
code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  background: var(--bg-tertiary);
  padding: 0.15em 0.4em;
  border-radius: 3px;
  color: var(--text-primary);
}

pre {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-light);
  border-radius: 4px;
  padding: 1.2em 1.4em;
  overflow-x: auto;
  margin: 1.5em 0;
  line-height: 1.5;
  font-size: 0.85rem;
}

pre code {
  background: none;
  padding: 0;
  border-radius: 0;
}

/* ============================================================
   TABLES
   ============================================================ */
table {
  width: 100%;
  border-collapse: collapse;
  margin: 1.5em 0;
  font-size: 0.92rem;
  font-family: var(--font-sans);
}

thead {
  border-bottom: 2px solid var(--border-medium);
}

th {
  text-align: left;
  padding: 0.6em 0.8em;
  font-weight: 600;
  font-variant: small-caps;
  letter-spacing: 0.04em;
  color: var(--text-secondary);
  font-size: 0.9em;
}

td {
  padding: 0.5em 0.8em;
  border-bottom: 1px solid var(--border-light);
  vertical-align: top;
}

tr:hover td {
  background: var(--bg-secondary);
}

/* ============================================================
   MATHEMATICAL CONTENT (KaTeX)
   ============================================================ */
.katex-display {
  margin: 1.5em 0;
  padding: 0.8em 0;
  overflow-x: auto;
  overflow-y: hidden;
}

.equation-numbered {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1em;
  margin: 1.5em 0;
}

.equation-numbered .eq-number {
  font-family: var(--font-sans);
  font-size: var(--size-small);
  color: var(--text-tertiary);
  white-space: nowrap;
}

/* Theorem / Definition / Proof boxes */
.theorem-box {
  background: var(--bg-secondary);
  border-left: 2px solid var(--accent-blue);
  padding: 1em 1.4em;
  margin: 1.5em 0;
  border-radius: 0 3px 3px 0;
}

.theorem-box .theorem-title {
  font-family: var(--font-sans);
  font-weight: 700;
  font-variant: small-caps;
  letter-spacing: 0.04em;
  color: var(--accent-blue);
  margin-bottom: 0.4em;
  font-size: 0.95rem;
}

.definition-box {
  background: var(--bg-aside);
  border-left: 2px solid var(--accent-green);
  padding: 1em 1.4em;
  margin: 1.5em 0;
  border-radius: 0 3px 3px 0;
}

.definition-box .def-title {
  font-family: var(--font-sans);
  font-weight: 700;
  font-variant: small-caps;
  letter-spacing: 0.04em;
  color: var(--accent-green);
  margin-bottom: 0.4em;
  font-size: 0.95rem;
}

.proof-box {
  padding: 0.5em 0 0.5em 1.4em;
  border-left: 1px dashed var(--border-medium);
  margin: 1em 0;
  color: var(--text-secondary);
  font-size: 0.95rem;
}

.proof-box::after {
  content: "\25AE";
  display: block;
  text-align: right;
  color: var(--text-tertiary);
  margin-top: 0.5em;
}

/* ============================================================
   SIDENOTES (Gwern-style)
   ============================================================ */
.sidenote-ref {
  font-size: 0.75em;
  vertical-align: super;
  color: var(--accent-link);
  cursor: pointer;
  font-family: var(--font-sans);
  font-weight: 600;
}

.sidenote {
  font-size: var(--size-small);
  color: var(--text-tertiary);
  line-height: 1.4;
  font-family: var(--font-sans);
  margin: 0.8em 0;
  padding: 0.6em 1em;
  background: var(--bg-aside);
  border-radius: 3px;
  border-left: 2px solid var(--accent-green);
}

@media (min-width: 1100px) {
  .sidenote {
    float: right;
    clear: right;
    width: 220px;
    margin-right: -260px;
    margin-top: 0;
    margin-bottom: 0.5em;
    position: relative;
  }
}

/* ============================================================
   FIGURES & CAPTIONS
   ============================================================ */
figure {
  margin: 2em 0;
  text-align: center;
}

figure img {
  max-width: 100%;
  height: auto;
  border-radius: 3px;
}

figcaption {
  font-family: var(--font-sans);
  font-size: var(--size-small);
  color: var(--text-tertiary);
  margin-top: 0.6em;
  line-height: 1.4;
  text-align: center;
}

/* ============================================================
   CALLOUT / HIGHLIGHT BOX
   ============================================================ */
.callout {
  background: var(--bg-highlight);
  border: 1px solid var(--callout-border);
  padding: 1em 1.4em;
  margin: 1.5em 0;
  border-radius: 3px;
  font-size: 0.95rem;
}
.callout p:last-child { margin-bottom: 0; }

/* ============================================================
   INLINE HIGHLIGHT
   ============================================================ */
mark, .highlight {
  background: var(--mark-bg);
  padding: 0.1em 0.25em;
  border-radius: 2px;
  color: var(--text-primary);
}

/* ============================================================
   HORIZONTAL RULE / SECTION BREAK
   ============================================================ */
hr {
  border: none;
  text-align: center;
  margin: var(--section-spacing) 0;
  color: var(--text-muted);
}

hr::after {
  content: "\2042";
  font-size: 1.2em;
  letter-spacing: 0.5em;
}

.section-break {
  text-align: center;
  margin: var(--section-spacing) 0;
  color: var(--text-muted);
  font-size: 1.2em;
  letter-spacing: 0.5em;
}

.section-break:empty::before {
  content: "\2042";
}

/* ============================================================
   TABLE OF CONTENTS
   ============================================================ */
.toc {
  font-family: var(--font-sans);
  font-size: var(--size-small);
  background: var(--bg-secondary);
  padding: 1.2em 1.5em;
  border-radius: 4px;
  margin: 2em 0;
}

.toc-title {
  font-weight: 700;
  font-variant: small-caps;
  letter-spacing: 0.05em;
  margin-bottom: 0.6em;
  color: var(--text-secondary);
}

.toc ol {
  margin: 0;
  padding-left: 1.4em;
}

.toc li {
  margin-bottom: 0.3em;
}

.toc a {
  color: var(--text-secondary);
  border-bottom: none;
}

.toc a:hover {
  color: var(--accent-link);
}

/* ============================================================
   FOOTNOTES
   ============================================================ */
.footnotes {
  font-size: var(--size-small);
  color: var(--text-secondary);
  border-top: 1px solid var(--border-light);
  margin-top: var(--section-spacing);
  padding-top: 1.5em;
}

/* ============================================================
   CATEGORY INDEX LISTING
   ============================================================ */
.category-listing {
  margin-top: var(--section-spacing);
}

/* Section label — quiet, uppercase, not a competing heading */
.category-listing h2 {
  font-family: var(--font-sans);
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.13em;
  color: var(--text-tertiary);
  margin: 0 0 1.25em 0;
  padding-top: 1.25rem;
  border-top: 1px solid var(--border-light);
}

.category-item {
  display: block;
  padding: 1.05em 1.3em;
  margin-bottom: 0.7em;
  background: var(--bg-secondary);
  border: 1px solid var(--border-light);
  border-radius: 8px;
  text-decoration: none;
  transition: border-color 0.18s ease, background 0.18s ease,
              transform 0.18s ease, box-shadow 0.18s ease;
}

.category-item:hover {
  border-color: var(--accent-link);
  background: var(--hover-bg);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  /* Re-assert geometry: the global `a:hover` rule otherwise collapses padding
     to 0 0.1em and shrinks the radius, which resizes the card and causes hover
     flicker. No transform here — moving the box also triggers edge flicker. */
  padding: 1.05em 1.3em;
  border-radius: 8px;
}

/* `.category-item` prefix raises specificity above the global `.content-page h3/p`
   rules, which would otherwise inject heading/paragraph margins into the cards. */
.category-item .category-item-title {
  font-family: var(--font-sans);
  font-size: 1.12rem;
  font-weight: 600;
  color: var(--accent-link);
  margin: 0 0 0.3em 0;
  padding-top: 0;
  border-top: none;
  display: flex;
  align-items: center;
}

/* Arrow affordance that slides on hover */
.category-item .category-item-title::after {
  content: "→";
  margin-left: auto;
  padding-left: 0.6em;
  color: var(--text-muted);
  font-weight: 400;
  transition: transform 0.18s ease, color 0.18s ease;
}

.category-item:hover .category-item-title {
  color: var(--accent-link-hover);
}

.category-item:hover .category-item-title::after {
  color: var(--accent-link-hover);
  transform: translateX(4px);
}

.category-item .category-item-excerpt {
  color: var(--text-secondary);
  font-size: 0.92rem;
  line-height: 1.5;
  margin: 0;
  text-align: left;
  hyphens: none;
  -webkit-hyphens: none;
}

.category-item-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin-top: 0.65em;
}

.category-item-tags .tag {
  background: var(--bg-tertiary);
  color: var(--text-tertiary);
  padding: 0.15em 0.5em;
  border-radius: 3px;
  font-size: var(--size-caption);
  font-family: var(--font-sans);
}

/* Sequential prev / up·home / next nav at the bottom of every generated page.
   The :any-link / :hover selectors intentionally outrank the header `nav a` rules. */
.page-nav {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
  margin-top: var(--section-spacing);
  padding-top: 1.1rem;
  border-top: 1px solid var(--border-light);
  font-family: var(--font-sans);
  font-size: var(--size-small);
}

/* Three equal slots → prev left, mid centered, next right */
.page-nav-slot { flex: 1 1 0; min-width: 0; }
.page-nav-prev { text-align: left; }
.page-nav-mid  { text-align: center; }
.page-nav-next { text-align: right; }

.page-nav a:any-link {
  color: var(--text-secondary);
  font-weight: 500;
  text-transform: none;
  letter-spacing: normal;
  padding: 0;
  border: none;
  border-radius: 0;
  background: none;
  text-decoration: none;
  transition: color 0.18s ease;
}

.page-nav a:hover {
  color: var(--accent-link);
  background: none;
}

.page-nav a.page-nav-mid-link:any-link { color: var(--text-tertiary); }

.page-nav-arrow {
  color: var(--text-muted);
  transition: transform 0.18s ease;
  display: inline-block;
}
.page-nav-prev a:hover .page-nav-arrow { transform: translateX(-3px); }
.page-nav-next a:hover .page-nav-arrow { transform: translateX(3px); }

/* ============================================================
   WIDE INDEX LAYOUT
   Index pages that show a card grid use a wider container; prose
   inside stays at the reading measure, the grid + nav span wider.
   ============================================================ */
.content-page.content-page--index {
  max-width: var(--body-wide);
}

/* Prose on index pages fills the full wide container (same width as the card
   grid and the article pages), so the text and cards share both edges. */

/* Responsive card grid — two columns once there's room, single column below */
@media (min-width: 720px) {
  .content-page--index .category-listing {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 1rem 1.25rem;
    align-items: start;
  }
  .content-page--index .category-listing h2 {
    grid-column: 1 / -1;
  }
  .content-page--index .category-listing .category-item {
    margin-bottom: 0;
  }
}

/* ============================================================
   HEADER & NAVIGATION (site-specific)
   ============================================================ */
header {
  background-color: var(--bg-secondary);
  border-bottom: 1px solid var(--border-medium);
  padding: 0.5rem 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 0;
  width: 100%;
  box-sizing: border-box;
}

.logo {
  text-decoration: none;
  display: flex;
  align-items: center;
  margin-right: 0.75rem;
}

.logo:hover {
  background: none;
  padding: 0;
  border-radius: 0;
}

.logo img {
  height: 30px;
  width: auto;
  /* Logo art is light two-tone text on a transparent background. Dark themes
     show it as-is; light themes invert it to dark text, softened via opacity. */
}

/* Light mode logo — auto detection */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) .logo img {
    filter: invert(1);
    opacity: 0.5;
  }
}
/* Light mode logo — manual toggle */
:root[data-theme="light"] .logo img {
  filter: invert(1);
  opacity: 0.5;
}
/* Dark mode logo — manual toggle (overrides light-OS auto) */
:root[data-theme="dark"] .logo img {
  filter: none;
  opacity: 1;
}

nav {
  display: flex;
  gap: 0;
}

nav a {
  color: var(--text-primary);
  text-decoration: none;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border-medium);
  border-left: none;
  border-bottom: none;
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  transition: background-color 0.2s ease;
  background: none;
  border-radius: 0;
}

nav a:first-child {
  border-left: 1px solid var(--border-medium);
}

nav a:hover {
  background-color: var(--bg-tertiary);
  color: var(--text-primary);
  padding: 0.5rem 0.75rem;
  border-radius: 0;
}

nav a.nav-active {
  color: var(--text-primary);
  background-color: rgba(128, 128, 128, 0.12);
  font-weight: 700;
}

nav a:not(.nav-active) {
  color: var(--text-muted);
}

nav a svg {
  width: 14px;
  height: 14px;
  vertical-align: -2px;
}

/* ============================================================
   MAIN CONTENT
   ============================================================ */
main {
  padding: 2rem;
}

/* ============================================================
   HOMEPAGE — Intro & Grid Layout (site-specific)
   ============================================================ */
.intro-section {
  margin-bottom: 2rem;
}

.intro-section p {
  margin-bottom: 1rem;
}

.content-sections {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 2rem;
  margin-top: 2rem;
}

.content-sections section {
  border: 1px dotted var(--border-light);
  padding: 1rem;
  background: rgba(255, 255, 255, 0.02);
  transition: border-color 0.2s ease;
}

.content-sections section:hover {
  border-color: var(--accent-link);
  border-style: solid;
}

/* Homepage section headers — small caps */
.content-sections section h2 {
  font-family: var(--font-sans);
  font-size: 0.85rem;
  font-weight: 600;
  font-style: normal;
  text-transform: uppercase;
  letter-spacing: 2px;
  margin-top: 0;
  margin-bottom: 1rem;
  border-top: none;
  border-bottom: 1px solid var(--border-light);
  padding-top: 0;
  padding-bottom: 0.5rem;
  color: var(--text-muted);
}

/* Article lists */
.article-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.article-list li {
  margin-bottom: 0.6rem;
}

.bullet-icon {
  text-decoration: none;
  display: inline;
  margin-right: 0.15em;
  font-style: normal;
  color: var(--text-muted);
  border-bottom: none;
}

.bullet-icon svg {
  width: 14px;
  height: 14px;
  vertical-align: -2px;
}

/* "New" sparkle icon — highlights new/notable items */
.bullet-new {
  color: var(--accent-highlight);
}

/* ============================================================
   LINK PREVIEWS — hover card + inline transclusion
   ============================================================ */
.link-preview {
  position: absolute;
  z-index: 1000;
  width: 440px;
  max-width: 92vw;
  background: var(--bg-secondary);
  border: 1px solid var(--border-medium);
  border-radius: 8px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.18);
  font-family: var(--font-serif);
  color: var(--text-primary);
  overflow: hidden;
}

.lp-head {
  padding: 0.7rem 1rem 0.55rem;
  border-bottom: 1px solid var(--border-light);
}
.lp-title {
  font-family: var(--font-sans);
  font-weight: 600;
  font-size: 1.02rem;
  color: var(--accent-link);
  text-decoration: none;
  border: none;
}
.lp-title:hover { color: var(--accent-link-hover); background: none; }
.lp-badges { margin-top: 0.4em; }
.lp-badges .page-badge { margin: 0 0.3em 0 0; }

.lp-body.is-clipped {
  -webkit-mask-image: linear-gradient(to bottom, #000 70%, transparent);
  mask-image: linear-gradient(to bottom, #000 70%, transparent);
}

.lp-actions {
  display: flex;
  gap: 0.5rem;
  padding: 0.55rem 1rem;
  border-top: 1px solid var(--border-light);
  background: var(--bg-tertiary);
}
.lp-action,
.lp-action:any-link {
  font-family: var(--font-sans);
  font-size: var(--size-caption);
  font-weight: 600;
  color: var(--text-secondary);
  background: none;
  border: 1px solid var(--border-medium);
  border-radius: 5px;
  padding: 0.25em 0.7em;
  cursor: pointer;
  text-decoration: none;
  transition: color 0.15s ease, border-color 0.15s ease;
}
.lp-action:hover { color: var(--accent-link); border-color: var(--accent-link); background: none; }

/* Strip .content-page framing inside previews so it fits the card/aside */
.link-preview .content-page,
.lp-transclusion .content-page {
  max-width: none;
  margin: 0;
  padding: 0;
}
.link-preview .lp-body {
  padding: 0.65rem 1rem 0.25rem;
  max-height: 15rem;
  overflow: hidden;
  font-size: 0.92rem;
  line-height: 1.5;
}
.link-preview .lp-body > :first-child { margin-top: 0; }

/* Inline transclusion (the "Expand here" action) */
.lp-transclusion {
  display: block;
  margin: 1.5rem 0;
  border: 1px solid var(--border-medium);
  border-left: 3px solid var(--accent-link);
  border-radius: 6px;
  background: var(--bg-secondary);
  overflow: hidden;
}
.lp-tr-head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.55rem 1rem;
  border-bottom: 1px solid var(--border-light);
  background: var(--bg-tertiary);
}
.lp-tr-title {
  font-family: var(--font-sans);
  font-weight: 600;
  color: var(--accent-link);
  text-decoration: none;
  border: none;
}
.lp-tr-badges { display: inline-flex; gap: 0.3em; }
.lp-tr-link {
  margin-left: auto;
  font-family: var(--font-sans);
  color: var(--text-tertiary);
  text-decoration: none;
  border: none;
}
.lp-tr-link:hover { color: var(--accent-link); background: none; }
.lp-tr-close {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1.25rem;
  line-height: 1;
  color: var(--text-tertiary);
  padding: 0 0.15em;
}
.lp-tr-close:hover { color: var(--accent-link); }
.lp-transclusion .lp-tr-body { padding: 1rem 1.25rem; }

.article-list a {
  color: var(--accent-link);
  font-family: var(--font-serif);
  font-size: 16px;
  line-height: 1.4;
}

.article-list a:hover {
  color: var(--accent-link-hover);
}

/* ============================================================
   FOOTER
   ============================================================ */
footer {
  margin-top: 4rem;
  padding: 2rem;
  border-top: 1px solid var(--border-light);
  text-align: center;
  color: var(--text-muted);
  font-size: 0.85rem;
}

/* ============================================================
   CONTENT PAGE STYLING
   ============================================================ */
.content-page {
  max-width: var(--body-wide);
  margin: 0 auto;
  padding: 2rem;
  line-height: var(--line-height);
}

.content-page h1 {
  font-size: var(--size-h1);
  margin-bottom: 0.5rem;
}

.content-page h2 {
  font-size: var(--size-h2);
  margin-top: 2.5rem;
  margin-bottom: 1rem;
}

.content-page h3 {
  font-size: var(--size-h3);
  margin-top: 1.5rem;
  margin-bottom: 0.5rem;
}

/* Content paragraphs — justified with hyphens */
.content-page p {
  margin-bottom: var(--paragraph-spacing);
  text-align: justify;
  hyphens: auto;
  -webkit-hyphens: auto;
}

/* Content lists */
.content-page ul, .content-page ol {
  margin: var(--paragraph-spacing) 0;
  padding-left: 1.5rem;
}

.content-page li {
  margin-bottom: 0.4rem;
  line-height: 1.6;
}

/* Nested sub-lists sit snug under their parent item (not the full 1.2em
   paragraph gap, which made them read as a separate list). */
.content-page li > ul,
.content-page li > ol {
  margin: 0.2rem 0 0.3rem;
}

/* Images & figures */
.content-page img {
  max-width: 100%;
  height: auto;
  display: block;
  border-radius: 6px;
  border: 1px solid var(--border-light);
}

.content-page figure.content-figure {
  margin: 2rem auto;
  text-align: center;
}

.content-page figure.content-figure img {
  margin: 0 auto;
}

.content-page figcaption {
  font-family: var(--font-sans);
  font-size: var(--size-small);
  color: var(--text-tertiary);
  line-height: 1.45;
  margin-top: 0.7em;
}

/* Image gallery / grid (:::gallery ... :::) — responsive, wraps automatically */
.content-page .gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
  margin: 2rem 0;
  align-items: start;
}

.content-page .gallery figure.content-figure {
  margin: 0;            /* grid gap handles spacing */
}

.content-page .gallery figure.content-figure img {
  width: 100%;          /* fill the tile */
}

.content-page .gallery figcaption {
  font-size: var(--size-caption);
  margin-top: 0.4em;
}

/* Hidden sections */
section[style*="display: none"] {
  display: none;
}

/* ============================================================
   FLOATING SETTINGS TOOLBAR (site-specific)
   ============================================================ */
.settings-floating {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 999;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  opacity: 1;
  transition: opacity 0.3s ease;
}

.settings-floating.faded {
  opacity: 0.15;
}

.settings-floating:hover {
  opacity: 1;
}

.sf-btn {
  background: var(--bg-secondary);
  border: 1px solid var(--border-medium);
  color: var(--text-primary);
  padding: 0.5rem;
  cursor: pointer;
  transition: background-color 0.2s ease, border-color 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 4px;
}

.sf-btn:hover {
  background-color: var(--bg-tertiary);
  border-color: var(--accent-link);
}

.sf-btn svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

.sf-gear {
  border-radius: 4px;
}

.sf-menu {
  display: none;
  flex-direction: column;
  gap: 2px;
}

.settings-floating.expanded .sf-menu {
  display: flex;
}

/* ============================================================
   SEARCH OVERLAY (site-specific)
   ============================================================ */
.search-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  z-index: 2000;
  display: flex;
  justify-content: center;
  padding-top: 15vh;
}

.search-container {
  background: var(--bg-secondary);
  border: 1px solid var(--border-medium);
  border-radius: 6px;
  width: 90%;
  max-width: 600px;
  max-height: 70vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
}

.search-container input {
  width: 100%;
  padding: 1rem 1.25rem;
  background: transparent;
  border: none;
  border-bottom: 1px solid var(--border-light);
  color: var(--text-primary);
  font-family: var(--font-serif);
  font-size: 18px;
  outline: none;
}

.search-container input::placeholder {
  color: var(--text-muted);
}

.search-results {
  overflow-y: auto;
  padding: 0.5rem;
}

.search-result-item {
  padding: 0.75rem 1rem;
  border-radius: 4px;
  cursor: pointer;
  display: block;
  text-decoration: none;
  color: var(--text-primary);
  border-bottom: none;
}

.search-result-item:hover,
.search-result-item.active {
  background: rgba(128, 128, 128, 0.1);
}

.search-result-item .result-title {
  font-weight: bold;
  color: var(--accent-link);
  margin-bottom: 0.25rem;
}

.search-result-item .result-excerpt {
  font-size: 14px;
  color: var(--text-muted);
  line-height: 1.4;
}

.search-no-results {
  padding: 2rem;
  text-align: center;
  color: var(--text-muted);
}

/* ============================================================
   HELP MODAL (site-specific)
   ============================================================ */
.help-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  z-index: 2000;
  display: flex;
  justify-content: center;
  align-items: center;
}

.help-modal {
  background: var(--bg-secondary);
  border: 1px solid var(--border-medium);
  border-radius: 6px;
  padding: 2rem;
  max-width: 500px;
  width: 90%;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
}

.help-modal h3 {
  margin-top: 0;
  margin-bottom: 1rem;
  font-size: 1.2rem;
  font-style: normal;
  color: var(--text-primary);
}

.help-modal dl {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.5rem 1rem;
}

.help-modal dt {
  font-weight: bold;
  color: var(--accent-link);
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.help-modal dt svg {
  width: 16px;
  height: 16px;
}

.help-modal dd {
  color: var(--text-secondary);
  margin: 0;
}

.help-modal .help-close {
  margin-top: 1.5rem;
  text-align: right;
}

.help-modal .help-close button {
  background: transparent;
  border: 1px solid var(--border-light);
  color: var(--text-primary);
  padding: 0.5rem 1.5rem;
  cursor: pointer;
  border-radius: 4px;
  font-family: var(--font-serif);
}

.help-modal .help-close button:hover {
  background: var(--bg-tertiary);
}

/* ============================================================
   READER MODE (site-specific)
   ============================================================ */
.reader-mode main {
  max-width: 700px;
  margin: 0 auto;
}

.reader-mode .content-sections {
  grid-template-columns: 1fr;
}

.reader-mode nav {
  display: none;
}

/* ============================================================
   RESPONSIVE
   ============================================================ */
@media (max-width: 768px) {
  :root {
    --size-base: 17px;
    --size-h1: 1.9rem;
    --size-h2: 1.5rem;
    --body-max-width: 100%;
    --side-padding: 16px;
  }

  header {
    flex-direction: column;
    gap: 1rem;
    padding: 1rem;
  }

  nav {
    flex-wrap: wrap;
  }

  nav a {
    font-size: 11px;
    padding: 0.5rem 0.75rem;
  }

  main {
    padding: 1rem;
  }

  .content-sections {
    grid-template-columns: 1fr;
    gap: 2rem;
  }

  .content-page p {
    text-align: left;
    hyphens: none;
  }

  .epigraph {
    margin: 1.5rem 1rem;
  }
}

/* ============================================================
   PRINT
   ============================================================ */
@media print {
  body {
    font-size: 11pt;
    color: #000;
    background: #fff;
    max-width: 100%;
  }

  h2 {
    border-top: none;
    page-break-after: avoid;
  }

  nav, .settings-floating {
    display: none;
  }

  .content-sections section {
    break-inside: avoid;
  }

  .theorem-box, .definition-box, .callout, pre {
    page-break-inside: avoid;
  }

  a {
    color: #000;
    border-bottom: none;
  }

  a[href^="http"]::after {
    content: " (" attr(href) ")";
    font-size: 0.8em;
    color: #666;
  }
}
