:root {
  --bg: #fafafa;
  --surface: #ffffff;
  --ink: #161616;
  --muted: #5c5f66;
  --rule: #e2e3e6;
  --accent: #9b1c1c;
  --accent-soft: #f7e9e8;
  --mark: #fff1a8;
  --serif: "IBM Plex Serif", Georgia, serif;
  --sans: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  --topbar: 56px;
  --toc-width: 300px;
  --measure: 40rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121316;
    --surface: #1a1c20;
    --ink: #e9e9ea;
    --muted: #9da1a9;
    --rule: #2b2e34;
    --accent: #e8918b;
    --accent-soft: #2d1b1c;
    --mark: #5a4c10;
  }
}

* { box-sizing: border-box; }

html { scroll-padding-top: calc(var(--topbar) + 16px); }

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

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; text-underline-offset: 3px; }

/* Top bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  height: var(--topbar);
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 0 24px;
  background: var(--bg);
  border-bottom: 1px solid var(--rule);
}
.brand {
  font-family: var(--serif);
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--ink);
  white-space: nowrap;
}
.brand em { font-weight: 400; }
.brand:hover { text-decoration: none; }
.search { flex: 1; max-width: 340px; margin-left: auto; }
.search input {
  width: 100%;
  height: 34px;
  padding: 0 12px;
  border: 1px solid var(--rule);
  border-radius: 4px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: 0.9rem;
}
.search input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.toplinks { display: flex; gap: 8px; }
.toplinks a {
  padding: 6px 12px;
  border: 1px solid var(--rule);
  border-radius: 4px;
  background: var(--surface);
  color: var(--ink);
  font-size: 0.9rem;
  font-weight: 500;
}
.toplinks a:hover { border-color: var(--accent); color: var(--accent); text-decoration: none; }
.toc-toggle { display: none; }

/* Layout */
.layout {
  display: grid;
  grid-template-columns: var(--toc-width) minmax(0, 1fr);
}

/* Contents sidebar */
.toc {
  position: sticky;
  top: var(--topbar);
  height: calc(100vh - var(--topbar));
  overflow-y: auto;
  padding: 20px 14px 40px 18px;
  border-right: 1px solid var(--rule);
  font-size: 0.85rem;
  line-height: 1.35;
}
.toc details { margin-bottom: 2px; }
.toc summary {
  list-style: none;
  cursor: pointer;
  padding: 6px 8px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.toc summary::-webkit-details-marker { display: none; }
.toc summary::before {
  content: "";
  width: 6px;
  height: 6px;
  flex: none;
  border-right: 1.5px solid var(--muted);
  border-bottom: 1.5px solid var(--muted);
  transform: rotate(-45deg);
  transition: transform 0.15s;
}
.toc details[open] > summary::before { transform: rotate(45deg); }
.toc summary a { color: var(--ink); font-weight: 500; }
.toc summary:hover { background: var(--accent-soft); }
.toc summary a[aria-current="page"] { color: var(--accent); }
.toc ul { list-style: none; margin: 2px 0 8px; padding: 0 0 0 22px; }
.toc li a {
  display: flex;
  gap: 6px;
  padding: 4px 8px;
  border-left: 2px solid var(--rule);
  color: var(--muted);
}
.toc li a:hover { color: var(--ink); text-decoration: none; border-left-color: var(--muted); }
.toc li a[aria-current="page"] {
  color: var(--accent);
  border-left-color: var(--accent);
  background: var(--accent-soft);
}

/* Main column */
.main {
  padding: 48px 40px 32px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  min-height: calc(100vh - var(--topbar));
}
.main > * { width: 100%; max-width: var(--measure); margin-left: auto; margin-right: auto; }

.eyebrow { margin: 0 0 10px; font-size: 0.9rem; color: var(--muted); }
.eyebrow a { color: inherit; }
.eyebrow a:hover { color: var(--accent); }

h1, h2, h3 { font-family: var(--serif); font-weight: 600; line-height: 1.22; text-wrap: balance; }
h1 { font-size: 2.1rem; margin: 0 0 20px; }

/* Article-style body text: justified with hyphenation (needs lang="en" on <html>), ~70 characters per line */
.prose {
  font-family: var(--serif);
  font-size: 1.1rem;
  line-height: 1.72;
  text-align: justify;
  hyphens: auto;
  -webkit-hyphens: auto;
  hyphenate-limit-chars: 5 2 2;
  font-kerning: normal;
  font-variant-ligatures: common-ligatures;
}
.prose p { margin: 0 0 1em; }
.prose h1 { font-size: 2rem; }
.prose h2 { font-size: 1.3rem; margin: 2em 0 0.6em; }
.prose table { width: 100%; border-collapse: collapse; font-family: var(--sans); font-size: 0.9rem; margin: 0.5em 0 1.5em; }
.prose th, .prose td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--rule); vertical-align: top; }
.prose th { font-weight: 600; }
.prose code { font-size: 0.85em; padding: 1px 5px; border-radius: 3px; background: var(--accent-soft); }

.cites { white-space: nowrap; }
/* Part and topic numbers: display only, recomputed from the current order */
.num { color: var(--muted); font-weight: inherit; font-variant-numeric: tabular-nums; flex: none; }
.toc .num { min-width: 1.4em; text-align: right; }
.topic-index .num { min-width: 1.6em; text-align: right; }
a.cite { color: var(--muted); font-family: var(--sans); font-size: 0.85em; }
a.cite:hover { color: var(--accent); }

/* Home: the wordmark alone, centred on the screen (the bottom padding offsets the top bar, plus a slight optical lift) */
.main:has(.home-center) { padding-top: 0; padding-bottom: 0; }
.home-center {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding-bottom: calc(var(--topbar) + 4vh);
}
.logo {
  margin: 0;
  font-family: var(--serif);
  font-size: clamp(3rem, 8vw, 6rem);
  font-weight: 600;
  letter-spacing: -0.02em;
  text-align: center;
}
.logo em { font-weight: 400; }

/* Part pages are indexes */
.page-head { margin-bottom: 28px; }
.summary { color: var(--muted); font-size: 1.02rem; margin: 0; }
.topic-index { list-style: none; margin: 0 auto; padding: 0; border-top: 1px solid var(--rule); }
.topic-index li {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid var(--rule);
}
.topic-index a {
  flex: 1;
  display: flex;
  gap: 10px;
  padding: 11px 2px;
  font-family: var(--serif);
  font-size: 1.05rem;
  color: var(--ink);
}
.topic-index a:hover { color: var(--accent); text-decoration: none; }

/* Topic pages */
.topic h1 { font-size: 2rem; }

.pager {
  display: flex;
  justify-content: space-between;
  gap: 32px;
  margin-top: 48px;
  padding-top: 20px;
  border-top: 1px solid var(--rule);
}
.pager a {
  max-width: 45%;
  color: var(--ink);
  font-size: 0.95rem;
  line-height: 1.4;
}
.pager a:hover { color: var(--accent); text-decoration: none; }
.pager a span { display: block; margin-bottom: 3px; font-size: 0.8rem; color: var(--muted); }
.pager .next { margin-left: auto; text-align: right; }

/* Discussion under a topic */
.comments { margin-top: 56px; padding-top: 24px; border-top: 1px solid var(--rule); }
.comments h2 { font-size: 1.3rem; margin: 0 0 16px; }
.comment { padding: 16px 0; border-bottom: 1px solid var(--rule); }
.comment.pending { opacity: 0.75; }
.comment header { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 12px; font-size: 0.85rem; color: var(--muted); }
.comment .who { color: var(--ink); font-weight: 600; font-size: 0.92rem; }
.comment .via { color: var(--muted); font-size: 0.8rem; }
.comment .badge { color: var(--accent); }
.comment-body { font-family: var(--serif); font-size: 1rem; line-height: 1.65; margin-top: 6px; }
.comment-body p { margin: 0 0 0.6em; }
.revision-note { margin: 4px 0 0; font-size: 0.8rem; color: var(--muted); }
.empty { color: var(--muted); font-size: 0.92rem; }
.notice { padding: 10px 12px; border-radius: 4px; background: var(--accent-soft); font-size: 0.9rem; }
.comment-form { margin-top: 24px; }
.comment-form label { display: block; margin-bottom: 6px; font-size: 0.85rem; color: var(--muted); }
.comment-form textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--rule);
  border-radius: 4px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-family: var(--serif);
  font-size: 1rem;
  line-height: 1.5;
  resize: vertical;
}
.comment-form textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.form-row { margin-top: 10px; display: flex; flex-wrap: wrap; align-items: center; gap: 14px; }
.fineprint { color: var(--muted); font-size: 0.82rem; }
.fineprint a { color: var(--muted); text-decoration: underline; }
.comments button {
  padding: 8px 16px;
  border: 1px solid var(--accent);
  border-radius: 4px;
  background: var(--accent);
  color: var(--bg);
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
}
.comments button.link { padding: 0; border: 0; background: none; color: var(--muted); font-size: 0.82rem; text-decoration: underline; }
.signout { margin-top: 16px; }
.signin { margin-top: 20px; padding: 20px; border: 1px solid var(--rule); border-radius: 6px; background: var(--surface); }
.signin-label { margin: 0 0 14px; font-weight: 500; }
.signin-buttons { display: flex; flex-wrap: wrap; gap: 10px; }
.signin-buttons form { margin: 0; }
.signin .fineprint { margin: 14px 0 0; }
.comments button.provider {
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  min-width: 13.5rem; padding: 9px 18px;
  background: var(--bg); color: var(--ink); border-color: var(--rule); font-weight: 500;
  transition: border-color 0.15s, background-color 0.15s;
}
.comments button.provider:hover { border-color: var(--muted); background: var(--surface); }
.comments button.provider:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.comments button.provider img { flex: none; }
@media (max-width: 480px) {
  .signin { padding: 16px; }
  .signin-buttons form, .comments button.provider { width: 100%; }
}
.mod-actions { display: flex; gap: 14px; margin-top: 6px; }

/* Sign-in pages (allauth) */
.auth-page { max-width: 28rem; margin: 64px auto; padding: 0 16px; }

/* References and search */
.refs { padding-left: 2.2em; font-size: 0.92rem; line-height: 1.5; }
.refs li { padding: 6px 8px; margin-bottom: 2px; border-radius: 3px; }
.refs li p { margin: 0; }
.refs li:target { background: var(--mark); }
.results { list-style: none; padding: 0; }
.results li { padding: 16px 0; border-bottom: 1px solid var(--rule); }
.results a { font-family: var(--serif); font-size: 1.1rem; font-weight: 500; }
.result-part { display: block; color: var(--muted); font-size: 0.8rem; margin-top: 2px; }
.results p { margin: 8px 0 0; font-family: var(--serif); color: var(--muted); }
mark { background: var(--mark); color: inherit; padding: 0 2px; border-radius: 2px; }


/* Small screens: contents become a slide-over panel */
@media (max-width: 960px) {
  .layout { display: block; }
  .toc-toggle {
    display: inline-block;
    padding: 6px 12px;
    border: 1px solid var(--rule);
    border-radius: 4px;
    background: var(--surface);
    color: var(--ink);
    font: inherit;
    font-size: 0.85rem;
    cursor: pointer;
  }
  .toc {
    position: fixed;
    top: var(--topbar);
    left: 0;
    z-index: 15;
    width: min(340px, 88vw);
    background: var(--bg);
    transform: translateX(-100%);
    transition: transform 0.2s;
  }
  body.toc-open .toc { transform: none; box-shadow: 0 0 40px rgba(0, 0, 0, 0.15); }
  .main { padding: 32px 16px 24px; }
  .topbar { gap: 12px; padding: 0 16px; }
}
@media (max-width: 640px) {
  .toplinks { display: none; }
  h1, .topic h1 { font-size: 1.7rem; }
  .prose { font-size: 1.02rem; }
  .pager { flex-direction: column; gap: 18px; }
  .pager a { max-width: none; }
  .prose { text-align: left; hyphens: manual; -webkit-hyphens: manual; }
}
