/* ============================================================
   Compendium — minimal academic surface
   Type:   Space Grotesk (display) · Source Serif 4 (body)
           IBM Plex Mono (labels, code)
   ============================================================ */

:root {
  --bg: #fbfaf6;
  --ink: #1c1c20;
  --muted: #71706a;
  --line: #e3e1d7;
  --accent: #2742c8;
  --tile-hover: #f4f2ea;
  --code-bg: #f1efe7;

  --display: "Space Grotesk", system-ui, sans-serif;
  --body: "Source Serif 4", Georgia, serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171b;
    --ink: #e8e6dd;
    --muted: #97958c;
    --line: #2b2e35;
    --accent: #9caeff;
    --tile-hover: #1b1e24;
    --code-bg: #1d2026;
  }
}
:root[data-theme="light"] {
  --bg: #fbfaf6; --ink: #1c1c20; --muted: #71706a; --line: #e3e1d7;
  --accent: #2742c8; --tile-hover: #f4f2ea; --code-bg: #f1efe7;
}
:root[data-theme="dark"] {
  --bg: #15171b; --ink: #e8e6dd; --muted: #97958c; --line: #2b2e35;
  --accent: #9caeff; --tile-hover: #1b1e24; --code-bg: #1d2026;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--body);
  font-size: 17px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
}

a { color: inherit; }

.page {
  max-width: 920px;
  margin: 0 auto;
  padding: 0 1.25rem 4rem;
}

/* ---------- header / footer ---------- */

.site-head {
  max-width: 920px;
  margin: 0 auto;
  padding: 1.4rem 1.25rem 2.6rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.site-mark {
  font-family: var(--display);
  font-weight: 600;
  font-size: 0.95rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  text-decoration: none;
}
.site-mark:hover { color: var(--accent); }

.theme-toggle {
  appearance: none;
  background: none;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  width: 32px; height: 32px;
  display: grid; place-items: center;
  cursor: pointer;
}
.theme-toggle:hover { color: var(--accent); border-color: var(--accent); }
.theme-toggle:focus-visible,
a:focus-visible, .tile:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.site-foot {
  max-width: 920px;
  margin: 0 auto;
  padding: 1.2rem 1.25rem 2.2rem;
  display: flex;
  align-items: center;
  gap: 0.8rem;
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.foot-rule { flex: 1; height: 1px; background: var(--line); }

/* ---------- hero (homepage) ---------- */

.eyebrow {
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 0.9rem;
}

.hero { padding: 1rem 0 2.8rem; }

.hero-title {
  font-family: var(--display);
  font-weight: 600;
  font-size: clamp(2.4rem, 6vw, 3.6rem);
  letter-spacing: -0.02em;
  line-height: 1.05;
  margin: 0 0 0.8rem;
}

.hero-sub {
  font-size: 1.05rem;
  color: var(--muted);
  max-width: 46ch;
  margin: 0;
}

/* ---------- breadcrumbs / section heads ---------- */

.crumbs {
  font-family: var(--mono);
  font-size: 0.74rem;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin-bottom: 2rem;
  overflow-wrap: anywhere;
}
.crumbs a { text-decoration: none; }
.crumbs a:hover { color: var(--accent); }
.crumb-sep { margin: 0 0.45em; opacity: 0.55; }

.section-head { margin-bottom: 2.2rem; }
.section-head h1 {
  font-family: var(--display);
  font-weight: 600;
  font-size: clamp(1.8rem, 4.5vw, 2.5rem);
  letter-spacing: -0.015em;
  margin: 0 0 0.45rem;
}
.section-meta {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
}

/* ---------- the lattice of tiles ---------- */

.lattice {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 1px;
  background: var(--line);
  border: 1px solid var(--line);
}

.lattice-break {
  grid-column: 1 / -1;
  height: 0;
  background: none;
  border: none;
}

.tile {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  min-height: 170px;
  padding: 1.15rem 1.2rem 1.1rem;
  background: var(--bg);
  text-decoration: none;
  transition: background-color 0.18s ease;
}
.tile:hover { background: var(--tile-hover); }

.tile-eyebrow {
  font-family: var(--mono);
  font-size: 0.68rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}

.tile-title {
  font-family: var(--display);
  font-weight: 500;
  font-size: 1.18rem;
  line-height: 1.25;
  letter-spacing: -0.01em;
  max-width: 14ch;
}
.tile:hover .tile-title { color: var(--accent); }

.tile-summary {
  font-size: 0.86rem;
  line-height: 1.45;
  color: var(--muted);
  max-width: 30ch;
  padding-right: 3rem;
  margin-top: auto;
}

.tile-glyph {
  position: absolute;
  right: 1rem;
  bottom: 0.95rem;
  color: var(--muted);
  opacity: 0.85;
}
.tile-glyph svg { width: 36px; height: 36px; display: block; }
.tile-glyph svg * {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 100;
  stroke-dashoffset: 0;
}
.tile:hover .tile-glyph { color: var(--accent); }
.tile:hover .tile-glyph svg * { animation: draw 0.9s ease forwards; }

@keyframes draw {
  from { stroke-dashoffset: 100; }
  to   { stroke-dashoffset: 0; }
}

/*
@media (prefers-reduced-motion: reduce) {
  .tile:hover .tile-glyph svg * { animation: none; }
  html { scroll-behavior: auto; }
}
*/

.empty {
  color: var(--muted);
  font-style: italic;
}

/* ---------- article prose ---------- */

.prose { max-width: 70ch; }

.prose-head { margin-bottom: 2.2rem; }
.prose-head h1 {
  font-family: var(--display);
  font-weight: 600;
  font-size: clamp(1.9rem, 4.5vw, 2.5rem);
  letter-spacing: -0.015em;
  line-height: 1.12;
  margin: 0 0 0.6rem;
}
.prose-summary {
  margin: 0;
  font-size: 1.05rem;
  color: var(--muted);
  font-style: italic;
}

.prose h2, .prose h3 {
  font-family: var(--display);
  font-weight: 600;
  letter-spacing: -0.01em;
  margin: 2.4rem 0 0.8rem;
}
.prose h2 {
  font-size: 1.45rem;
  padding-top: 1.2rem;
  border-top: 1px solid var(--line);
}
.prose h3 { font-size: 1.15rem; }

.prose p { margin: 0 0 1.05rem; }

.prose a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 2px; }

.prose blockquote {
  margin: 1.4rem 0;
  padding: 0.2rem 0 0.2rem 1.1rem;
  border-left: 2px solid var(--accent);
  color: var(--muted);
}

.prose code {
  font-family: var(--mono);
  font-size: 0.84em;
  background: var(--code-bg);
  padding: 0.12em 0.38em;
  border-radius: 4px;
}
.prose pre {
  background: var(--code-bg);
  border: 1px solid var(--line);
  padding: 1rem 1.1rem;
  overflow-x: auto;
  border-radius: 6px;
  line-height: 1.55;
}
.prose pre code { background: none; padding: 0; font-size: 0.82rem; }

.prose ul, .prose ol { padding-left: 1.4rem; margin: 0 0 1.05rem; }
.prose li { margin-bottom: 0.35rem; }

.prose img { max-width: 100%; height: auto; }

.prose table {
  border-collapse: collapse;
  width: 100%;
  margin: 1.4rem 0;
  font-size: 0.92rem;
}
.prose th, .prose td {
  border: 1px solid var(--line);
  padding: 0.5rem 0.7rem;
  text-align: left;
}
.prose th { font-family: var(--display); font-weight: 500; }

/* display math: allow horizontal scroll on small screens */
.prose .katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  padding: 0.2rem 0;
}

.back {
  margin-top: 3rem;
  font-family: var(--mono);
  font-size: 0.78rem;
}
.back a { text-decoration: none; color: var(--muted); }
.back a:hover { color: var(--accent); }

@media (max-width: 540px) {
  .lattice { grid-template-columns: 1fr 1fr; }
  .tile { min-height: 140px; padding: 0.9rem; }
  .tile-summary { display: none; }
}
@media (max-width: 380px) {
  .lattice { grid-template-columns: 1fr; }
}

/* ============================================================
   v2 additions: search, envs, tags, nav, diagrams, code, print
   ============================================================ */

/* extra tokens */
:root {
  --syn-str: #316e45; --syn-num: #a25b1e; --syn-fn: #7a3fa0; --syn-com: var(--muted);
  --env-bg: #f5f3eb;
}
@media (prefers-color-scheme: dark) {
  :root { --syn-str: #9ecfa8; --syn-num: #e0b083; --syn-fn: #c9a6e8; --env-bg: #1b1e24; }
}
:root[data-theme="light"] { --syn-str: #316e45; --syn-num: #a25b1e; --syn-fn: #7a3fa0; --env-bg: #f5f3eb; }
:root[data-theme="dark"]  { --syn-str: #9ecfa8; --syn-num: #e0b083; --syn-fn: #c9a6e8; --env-bg: #1b1e24; }

/* header tool buttons */
.head-tools { display: flex; gap: 0.5rem; align-items: center; }
.head-btn {
  appearance: none; background: none;
  border: 1px solid var(--line); border-radius: 999px;
  color: var(--muted);
  width: 32px; height: 32px;
  display: grid; place-items: center;
  cursor: pointer; text-decoration: none;
}
.head-btn:hover { color: var(--accent); border-color: var(--accent); }
.head-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.theme-toggle { display: none; } /* superseded by .head-btn */

/* footer links */
.site-foot a { color: var(--muted); text-decoration: none; }
.site-foot a:hover { color: var(--accent); }
.foot-sep { opacity: 0.5; }

/* article meta */
.prose-date {
  margin: 0.5rem 0 0;
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.draft-badge {
  display: inline-block;
  margin: 0 0 0.8rem;
  padding: 0.2rem 0.6rem;
  border: 1px dashed var(--accent);
  border-radius: 4px;
  font-family: var(--mono);
  font-size: 0.7rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
}

/* PDF pages: embedded viewer + view switch */
.pdf-page { max-width: 920px; }
.pdf-switch {
  display: inline-flex;
  margin-top: 1rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  overflow: hidden;
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0.06em;
}
.pdf-switch-opt {
  padding: 0.35rem 0.9rem;
  color: var(--muted);
  text-decoration: none;
}
.pdf-switch-opt + .pdf-switch-opt { border-left: 1px solid var(--line); }
.pdf-switch-opt.is-active { background: var(--accent); color: var(--bg); }
a.pdf-switch-opt:hover { color: var(--accent); }
a.pdf-switch-opt.is-active:hover { color: var(--bg); }
.pdf-frame {
  margin: 1.5rem 0 0.6rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
  background: var(--code-bg);
}
.pdf-frame iframe {
  display: block;
  width: 100%;
  height: 82vh;
  border: 0;
}
.pdf-fallback { font-size: 0.8rem; color: var(--muted); }

/* theorem-style environments */
.env {
  margin: 1.5rem 0;
  padding: 0.9rem 1.15rem;
  background: var(--env-bg);
  border-left: 2px solid var(--accent);
  border-radius: 0 6px 6px 0;
}
.env > p { margin-bottom: 0.6rem; }
.env > p:last-child { margin-bottom: 0; }
.env-label {
  font-family: var(--display);
  font-weight: 600;
  font-size: 0.95rem;
  letter-spacing: 0.01em;
}
.env-name { font-weight: 400; color: var(--muted); }
.env-definition, .env-example, .env-remark { border-left-color: var(--muted); }
.env-proof { background: none; border-left: 1px solid var(--line); }
.env-proof .env-label { font-weight: 400; font-style: italic; font-family: var(--body); }
.env-proof::after { content: "∎"; display: block; text-align: right; margin-top: -1.2rem; color: var(--muted); }

/* wiki-links */
.prose a.wikilink { text-decoration-style: dotted; }
.wikilink-missing {
  color: var(--muted);
  border-bottom: 1px dashed var(--muted);
  cursor: help;
}

/* footnotes */
.footnotes-sep { border: none; border-top: 1px solid var(--line); margin: 2.5rem 0 1rem; }
.footnotes { font-size: 0.88rem; color: var(--muted); }
.footnote-ref a, .footnote-backref { text-decoration: none; }

/* tags */
.tag-row { margin-top: 2.2rem; display: flex; flex-wrap: wrap; gap: 0.45rem; }
.tag {
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0.05em;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.18rem 0.65rem;
  text-decoration: none;
}
.tag:hover { color: var(--accent); border-color: var(--accent); }
.tile-compact { min-height: 100px; }

/* backlinks */
.backlinks {
  margin-top: 2.2rem;
  padding: 0.9rem 1.15rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  max-width: 70ch;
}
.backlinks-label {
  margin: 0 0 0.4rem;
  font-family: var(--mono);
  font-size: 0.7rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}
.backlinks ul { margin: 0; padding-left: 1.2rem; }
.backlinks a { color: var(--accent); text-decoration: none; }
.backlinks a:hover { text-decoration: underline; }

/* prev / next */
.pn {
  margin-top: 2.6rem;
  max-width: 70ch;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1px;
  background: var(--line);
  border: 1px solid var(--line);
}
.pn a {
  display: flex; flex-direction: column; gap: 0.25rem;
  padding: 0.85rem 1rem;
  background: var(--bg);
  text-decoration: none;
}
.pn a:hover { background: var(--tile-hover); }
.pn-next { text-align: right; }
.pn-dir {
  font-family: var(--mono);
  font-size: 0.68rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
}
.pn-title { font-family: var(--display); font-weight: 500; font-size: 0.98rem; }
.pn a:hover .pn-title { color: var(--accent); }
.pn span:empty { background: var(--bg); }

/* diagrams (build-time mermaid SVG, or client-rendered fallback) */
.diagram, .prose pre.mermaid {
  margin: 1.6rem 0;
  padding: 1rem;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow-x: auto;
}
.diagram svg { max-width: 100%; height: auto; display: block; margin: 0 auto; }
:root[data-theme="dark"] .diagram, :root[data-theme="dark"] .prose pre.mermaid { background: #e9e7df; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .diagram,
  :root:not([data-theme="light"]) .prose pre.mermaid { background: #e9e7df; }
}

/* syntax highlighting (Prism token theme on our variables) */
.prose pre[class*="language-"] { color: var(--ink); }
.token.comment, .token.prolog, .token.cdata { color: var(--syn-com); font-style: italic; }
.token.keyword, .token.atrule, .token.important, .token.tag { color: var(--accent); }
.token.string, .token.char, .token.attr-value, .token.regex { color: var(--syn-str); }
.token.number, .token.boolean, .token.constant, .token.symbol { color: var(--syn-num); }
.token.function, .token.class-name, .token.selector, .token.attr-name { color: var(--syn-fn); }
.token.operator, .token.punctuation { color: var(--muted); }

/* pagefind UI on our tokens */
#search { margin-top: 0.5rem; }
:root {
  --pagefind-ui-scale: 0.9;
  --pagefind-ui-primary: var(--accent);
  --pagefind-ui-text: var(--ink);
  --pagefind-ui-background: var(--bg);
  --pagefind-ui-border: var(--line);
  --pagefind-ui-tag: var(--tile-hover);
  --pagefind-ui-border-width: 1px;
  --pagefind-ui-border-radius: 6px;
  --pagefind-ui-font: var(--body);
}

/* Turing machine widget */
.tm { margin: 1.8rem 0; padding: 1.1rem; border: 1px solid var(--line); border-radius: 6px; font-family: var(--mono); }
.tm-tape { display: flex; gap: 3px; overflow-x: auto; padding: 0.4rem 0 0.7rem; }
.tm-cell {
  min-width: 34px; height: 38px;
  display: grid; place-items: center;
  border: 1px solid var(--line); border-radius: 4px;
  font-size: 0.95rem;
}
.tm-cell.head { border-color: var(--accent); color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.tm-status { font-size: 0.74rem; color: var(--muted); margin: 0.2rem 0 0.7rem; letter-spacing: 0.04em; }
.tm-status .halted { color: var(--accent); }
.tm-controls { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.tm-controls button {
  font-family: var(--mono); font-size: 0.76rem;
  background: none; color: var(--ink);
  border: 1px solid var(--line); border-radius: 4px;
  padding: 0.35rem 0.8rem; cursor: pointer;
}
.tm-controls button:hover { border-color: var(--accent); color: var(--accent); }
.tm-controls input {
  font-family: var(--mono); font-size: 0.8rem;
  background: none; color: var(--ink);
  border: 1px solid var(--line); border-radius: 4px;
  padding: 0.32rem 0.55rem; width: 9ch;
}

/* print: just the article, in ink */
@media print {
  .site-head, .site-foot, .crumbs, .back, .pn, .tag-row, .backlinks, .tm-controls { display: none !important; }
  body { background: #fff; color: #000; font-size: 11pt; }
  .page { max-width: none; padding: 0; }
  .prose { max-width: none; }
  .prose a { color: #000; text-decoration: none; }
  .prose a[href^="http"]::after { content: " (" attr(href) ")"; font-size: 0.8em; color: #444; }
  .env { border-left: 2px solid #000; background: none; }
  .diagram, .prose pre { border-color: #999; background: #fff; }
}
