:root {
  color-scheme: light dark;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
          Helvetica, Arial, sans-serif;
  --mono: "DejaVu Sans Mono", ui-monospace, SFMono-Regular, Menlo,
          Monaco, Consolas, monospace;
  --text: #111315;
  --muted: #6c727a;
  --border: #e6e8eb;
  --border-subtle: #eff1f3;
  --bg: #ffffff;
  --surface-subtle: #f8f9fa;
  --code-bg: #f4f5f7;
  --accent: #235d0d;
  --accent-hover: #194409;
  --toc-bg: #fafafa;
  --toc-hover: #edf4ea;
  --toc-guide: #e6e8eb;
  --toc-active: #235d0d;
  --toc-active-bg: #eaf3e6;
  --scrollbar-thumb: #c8cdd2;
  --scrollbar-track: transparent;
  --table-header: #f4f5f7;
  --table-stripe: #fafafa;
  --err-bg: #fdf2f1;
  --err-border: #b42318;
  --err-outline: #f0d0cd;
  --err-text: #b42318;
  --out-bg: #f3f7f1;
  --out-border: #235d0d;
  --out-text: #235d0d;
  --toc-width: clamp(256px, 21vw, 304px);
  --notes-width: clamp(410px, 33vw, 560px);
  --content-width: 820px;
}

/* --- Dark Mode --- */
@media (prefers-color-scheme: dark) {
  :root {
    --text: #e6e8eb;
    --muted: #848b94;
    --border: #22262b;
    --border-subtle: #1b1e23;
    --bg: #0e1012;
    --surface-subtle: #14171b;
    --code-bg: #171a1e;
    --accent: #529c36;
    --accent-hover: #6ab54e;
    --toc-bg: #111417;
    --toc-hover: #192415;
    --toc-guide: #22262b;
    --toc-active: #529c36;
    --toc-active-bg: #182613;
    --scrollbar-thumb: #3a4048;
    --table-header: #16191d;
    --table-stripe: #121518;
    --err-bg: #231616;
    --err-border: #f08076;
    --err-outline: #4a2a2a;
    --err-text: #f08076;
    --out-bg: #141c12;
    --out-border: #529c36;
    --out-text: #529c36;
  }

  /* Diagrams use dark ink on transparent or white canvases; keep them legible. */
  img[src$=".svg"],
  .notes-content figure img {
    background: #f8f9fa;
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: 6px;
    filter: brightness(0.94);
  }

  .study-diagram img {
    padding: 0;
    border-radius: 4px;
  }
}

/* --- Base Layout --- */
html {
  scroll-padding-top: 24px;
  background: var(--bg);
}

body {
  box-sizing: border-box;
  margin: 0 0 0 var(--toc-width);
  padding: 44px max(30px, calc((100vw - var(--toc-width) - var(--content-width)) / 2)) 96px;
  font-family: var(--sans);
  font-size: 17.5px;
  line-height: 1.72;
  color: var(--text);
  background: var(--bg);
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
}

::selection {
  background: var(--toc-active-bg);
  color: var(--text);
}

/* --- Headings --- */
h1, h2, h3, h4 {
  margin-top: 1.9em;
  margin-bottom: 0.6em;
  line-height: 1.3;
  color: var(--text);
}

h1 {
  font-size: 2.05em;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-top: 0;
  margin-bottom: 0.55em;
}

#title-block-header h1 {
  margin-top: 0;
  margin-bottom: 0.35em;
}

h2 {
  font-size: 1.48em;
  font-weight: 650;
  letter-spacing: -0.012em;
  margin-top: 2.35em;
  margin-bottom: 0.7em;
  padding-bottom: 0.28em;
  border-bottom: 1px solid var(--border);
}

h3 {
  font-size: 1.18em;
  font-weight: 650;
  margin-top: 1.85em;
  margin-bottom: 0.5em;
}

h4 {
  font-size: 1em;
  font-weight: 600;
  margin-top: 1.3em;
  margin-bottom: 0.45em;
}

h1[id], h2[id], h3[id], h4[id] {
  scroll-margin-top: 24px;
}

/* --- Paragraphs, Lists, Rules & Links --- */
p {
  margin: 0.95em 0;
}

ul, ol {
  margin: 0.8em 0 1.1em;
  padding-left: 1.55em;
}

li {
  margin: 0.35em 0;
}

li > p {
  margin: 0.35em 0;
}

hr {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 1.9em 0;
}

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

a:hover {
  color: var(--accent-hover);
  text-decoration: underline;
}

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

/* --- Centered Definitions & Formulas --- */
.center {
  margin: 1.45em 0;
  padding: 14px 20px;
  text-align: center;
  border-top: 1px solid var(--border-subtle);
  border-bottom: 1px solid var(--border-subtle);
  background: var(--surface-subtle);
}

.center > p {
  margin: 0.4em 0;
}

.center > :first-child {
  margin-top: 0;
}

.center > :last-child {
  margin-bottom: 0;
}

/* --- Chapter Map --- */
.chapter-map {
  margin: 20px 0 30px;
  padding: 15px 20px;
  border-left: 3px solid var(--toc-active);
  border-radius: 0 6px 6px 0;
  background: var(--surface-subtle);
}

.chapter-map > p:first-child {
  margin: 0 0 8px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--toc-active);
}

.chapter-map > ul {
  margin: 0;
  padding-left: 1.3em;
  font-size: 15.75px;
  line-height: 1.58;
}

.chapter-map li {
  margin: 0.3em 0;
}

/* --- Images --- */
p img {
  display: block;
  margin: 22px auto;
  max-width: 100%;
  height: auto;
  border-radius: 6px;
}

/* --- Code Blocks --- */
pre {
  background: var(--code-bg);
  padding: 14px 18px;
  margin: 1.15em 0 1.35em;
  border-radius: 7px;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 14.25px;
  line-height: 1.52;
  tab-size: 4;
  border: 1px solid var(--border);
}

code {
  font-family: var(--mono);
  background: var(--code-bg);
  border: 1px solid var(--border-subtle);
  padding: 0.1em 0.36em;
  border-radius: 4px;
  font-size: 0.86em;
}

pre code {
  background: none;
  border: 0;
  padding: 0;
  font-size: inherit;
  white-space: pre;
}

pre.compile-error {
  background: var(--err-bg);
  border-color: var(--err-outline);
  border-left: 3px solid var(--err-border);
}

pre.compile-error::before {
  content: "Compile error";
  display: block;
  margin-bottom: 8px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--err-outline);
  font-family: var(--sans);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--err-text);
}

pre.output {
  background: var(--out-bg);
  border-left: 3px solid var(--out-border);
}

pre.output::before {
  content: "Program output";
  display: block;
  margin-bottom: 8px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border-subtle);
  font-family: var(--sans);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--out-text);
}

.study-text-diagram {
  margin: 18px 0;
}

.study-text-diagram pre {
  margin: 0;
  background: var(--surface-subtle);
  border-style: dashed;
  font-size: 13.75px;
  line-height: 1.45;
}

/* --- Tables --- */
table {
  width: 100%;
  border-collapse: collapse;
  margin: 24px 0;
  font-size: 15.5px;
  line-height: 1.55;
}

th, td {
  border: 1px solid var(--border);
  padding: 9px 13px;
  text-align: left;
  vertical-align: top;
}

th {
  background: var(--table-header);
  font-weight: 600;
  font-size: 0.93em;
}

tbody tr:nth-child(even) {
  background: var(--table-stripe);
}

.table-nowrap {
  max-width: 100%;
  overflow-x: auto;
  margin: 24px 0;
}

.table-nowrap table {
  display: table;
  width: 100%;
  min-width: 0;
  max-width: none;
  margin: 0;
}

.table-nowrap col {
  width: auto !important;
}

.table-nowrap th,
.table-nowrap td,
.table-nowrap code {
  white-space: nowrap;
}

/* --- Collapsible Examples --- */
details {
  margin: 20px 0;
  padding: 11px 16px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--surface-subtle);
}

summary {
  cursor: pointer;
  font-weight: 600;
  font-size: 15.5px;
}

details:not(.study-notes):not(.code-labs):not(.code-file) > summary:hover {
  color: var(--accent);
}

details:not(.study-notes):not(.code-labs):not(.code-file)[open] > summary {
  margin-bottom: 12px;
}

details > :last-child {
  margin-bottom: 0;
}

/* --- Expandable Study Notes & Code Labs --- */
details.study-notes,
details.code-labs {
  margin: 10px 0 22px;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: var(--bg);
  overflow: hidden;
  overflow: clip; /* unlike hidden, clip lets the open summary stay sticky */
}

details.study-notes + details.code-labs {
  margin-top: -16px;
}

.study-notes > summary,
.code-labs > summary {
  padding: 4px 8px;
  margin-left: -8px;
  border-radius: 5px;
  color: var(--muted);
  background: none;
  font-size: 14px;
  font-weight: 500;
  width: fit-content;
  max-width: 100%;
  transition: color 0.15s ease, background-color 0.15s ease;
}

.code-labs > summary {
  color: var(--accent);
}

.study-notes > summary:hover,
.code-labs > summary:hover {
  color: var(--accent);
  background: var(--surface-subtle);
}

.study-notes > summary:focus-visible,
.code-labs > summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.study-notes[data-notes-active="true"] > summary,
.code-labs[data-notes-active="true"] > summary {
  color: var(--toc-active);
  background: var(--toc-active-bg);
  font-weight: 600;
}

/* Keep the panel title, which also closes it, in view while reading notes. */
.study-notes[open] > summary,
.code-labs[open] > summary {
  position: sticky;
  top: 0;
  z-index: 5;
  width: auto;
  margin-left: 0;
  padding: 9px 0;
  border-radius: 0;
  border-bottom: 1px solid var(--border-subtle);
  background: var(--bg);
  color: var(--text);
  font-weight: 600;
}

/* Linked notes land below the sticky title instead of under it. */
.study-notes [id],
.code-labs [id],
.notes-drawer [id] {
  scroll-margin-top: 4.5rem;
}

.code-labs {
  scroll-margin-top: 24px;
}

.code-labs > .code-labs-content {
  padding: 10px 0 4px 12px;
}

.code-bundle {
  margin: 0 0 18px;
}

.code-bundle:last-child {
  margin-bottom: 0;
}

.code-bundle-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 10px;
  margin-bottom: 8px;
  font-size: 14px;
}

.code-bundle-title {
  font-weight: 600;
  color: var(--text);
}

.code-bundle-link {
  margin-left: auto;
  font-size: 13px;
}

.code-badge {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  border: 1px solid var(--border);
  background: var(--surface-subtle);
  color: var(--muted);
}

.code-badge-example {
  color: var(--toc-active);
  background: var(--toc-active-bg);
  border-color: transparent;
}

details.code-file {
  margin: 6px 0;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-subtle);
}

details.code-file > summary {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 6px 12px;
  font-size: 13.75px;
  font-weight: 600;
  cursor: pointer;
  list-style: none;
}

details.code-file > summary::-webkit-details-marker {
  display: none;
}

details.code-file > summary > .code-file-name::before {
  content: "+  ";
  color: var(--muted);
}

details.code-file[open] > summary > .code-file-name::before {
  content: "-  ";
  color: var(--accent);
}

details.code-file[open] > summary {
  margin-bottom: 8px;
}

.code-file-name {
  font-family: var(--mono);
  color: var(--text);
}

.code-file-meta {
  font-size: 12.5px;
  font-weight: 400;
  color: var(--muted);
}

.code-file-links {
  margin: 0 0 6px;
  font-size: 13px;
}

details.code-file pre {
  margin: 0;
  font-size: 13.5px;
  line-height: 1.48;
  padding: 10px 12px;
}

.notes-content {
  font-size: inherit;
  line-height: inherit;
  overflow-wrap: anywhere;
}

.study-notes > .notes-content {
  padding: 10px 0 4px 16px;
}

.note-group {
  display: grid;
  grid-template-columns: minmax(2.2ch, max-content) minmax(0, 1fr);
  column-gap: 12px;
  align-items: baseline;
  padding: 7px 0;
}

.note-group:first-child { padding-top: 0; }
.note-group:last-child { padding-bottom: 0; }
.note-group > :first-child { grid-column: 1; margin: 0; font-size: inherit; text-align: right; }
.note-group > :not(:first-child) { grid-column: 2; margin-top: 0; margin-left: 0; }
.note-group > :last-child { margin-bottom: 0; }
.note-ref { color: var(--muted); font-size: inherit; font-weight: 600; font-variant-numeric: tabular-nums; }
.note-group p { margin-bottom: 0.6em; }
.note-group ul { min-width: 0; margin: 0.2em 0 0.6em; }
.notes-content ul { padding-left: 1.15em; }
.notes-content li { margin: 0.28em 0; }
.notes-content ul ul { margin: 0.25em 0; padding-left: 1.25em; }
.notes-content h4 { margin: 1.2em 0 0.6em; font-size: 1.05em; }
.notes-content figure { margin: 22px 0; }
.notes-content figure img { display: block; box-sizing: border-box; width: 100%; height: auto; background: #ffffff; }
.notes-content figcaption { margin-top: 8px; color: var(--muted); font-size: 13.75px; line-height: 1.5; }
.study-notes { scroll-margin-top: 24px; }

.note-clarification {
  margin: 14px 0;
  padding: 2px 0 2px 14px;
  border-left: 2px solid var(--accent);
  color: var(--muted);
}

.note-clarification > :first-child { margin-top: 0; }
.note-clarification > :last-child { margin-bottom: 0; }

/* Editable study diagrams remain legible in both color schemes. */
.study-diagram > a { display: block; }
.study-diagram img { padding: 0; border-radius: 4px; }
.study-diagram a:focus-visible { outline: 2px solid var(--accent); }

/* --- Right-Hand Docked Detailed Notes Pane (Desktop >= 1300px) --- */
.notes-drawer {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 25;
  width: var(--notes-width);
  height: 100vh;
  height: 100dvh;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--border);
  background: var(--bg);
}

.notes-drawer[hidden] {
  display: none;
}

.notes-drawer-resizer {
  position: absolute;
  top: 0;
  bottom: 0;
  left: -5px;
  z-index: 30;
  width: 10px;
  cursor: col-resize;
  touch-action: none;
}

.notes-drawer-resizer::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 4px;
  width: 2px;
  background: transparent;
  transition: background-color 0.15s ease;
}

.notes-drawer-resizer:hover::after,
.notes-drawer-resizer:focus-visible::after,
body.is-resizing-notes .notes-drawer-resizer::after {
  background: var(--accent);
}

.notes-drawer-resizer:focus-visible {
  outline: none;
}

body.is-resizing-notes {
  user-select: none;
  cursor: col-resize;
}

.notes-drawer-header {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--toc-bg);
}

.notes-drawer-heading {
  min-width: 0;
}

.notes-drawer-kicker {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--toc-active);
  margin: 0 0 2px;
}

.notes-drawer-title {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 15px;
  font-weight: 650;
  line-height: 1.35;
  color: var(--text);
}

.notes-drawer-actions {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.notes-drawer-btn {
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  line-height: 1.2;
  padding: 5px 9px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
}

.notes-drawer-btn:hover:not(:disabled) {
  background: var(--toc-hover);
  color: var(--toc-active);
  border-color: var(--toc-active);
}

.notes-drawer-btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.notes-drawer-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.notes-drawer-body {
  flex: 1 1 auto;
  overflow-y: auto;
  overscroll-behavior-y: contain;
  scrollbar-gutter: stable;
  scrollbar-width: thin;
  scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);
  padding: 20px 24px 64px;
}

@media (min-width: 1300px) {
  details.study-notes:not([open]) > summary,
  details.code-labs:not([open]) > summary {
    margin-left: auto;
  }

  body.has-notes-drawer {
    --toc-width: clamp(228px, 17vw, 272px);
    margin-right: var(--notes-width);
    padding-left: max(24px, calc((100vw - var(--toc-width) - var(--notes-width) - var(--content-width)) / 2));
    padding-right: max(24px, calc((100vw - var(--toc-width) - var(--notes-width) - var(--content-width)) / 2));
  }
}

@media (max-width: 1299px) {
  .notes-drawer {
    display: none;
  }
}

/* --- Chapter Navigation & Resources --- */
.chapter-nav {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 14px;
  margin: 52px 0 0;
  padding-top: 18px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 14.5px;
}

.chapter-nav-top {
  justify-content: flex-start;
  margin: 0 0 10px;
  padding: 0 0 12px;
  border-top: 0;
  border-bottom: 1px solid var(--border-subtle);
}

.nav-sep {
  color: var(--border);
  user-select: none;
}

.chapter-nav:not(.chapter-nav-top) .nav-sep {
  display: none;
}

.chapter-resources {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
  margin: 0 0 24px;
  padding: 0 0 14px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  color: var(--muted);
}

.chapter-volume {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.01em;
  color: var(--text);
}

.chapter-resource-links {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 10px;
}

/* --- Sidebar Home Button --- */
.page-home {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 20;
  width: var(--toc-width);
  box-sizing: border-box;
  padding: 10px 12px 8px 14px;
  border-right: 1px solid var(--border);
  background: var(--toc-bg);
  margin: 0;
}

.page-home a {
  display: block;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--toc-active);
  background: var(--bg);
  font-size: 14px;
  font-weight: 600;
  line-height: 1.25;
  text-align: center;
  text-decoration: none;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}

.page-home a:hover {
  background: var(--toc-hover);
  border-color: var(--toc-active);
  text-decoration: none;
}

.page-home a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* --- Footnotes --- */
.tbl-fn {
  font-size: 14px;
  color: var(--muted);
  margin-top: 8px;
}

/* --- Table of Contents Sidebar --- */
#TOC {
  position: fixed;
  top: 54px;
  left: 0;
  z-index: 10;
  width: var(--toc-width);
  height: calc(100vh - 54px);
  height: calc(100dvh - 54px);
  box-sizing: border-box;
  overflow-y: auto;
  overscroll-behavior-y: contain;
  scrollbar-gutter: stable;
  scrollbar-width: thin;
  scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);
  padding: 0 12px 40px 14px;
  border-right: 1px solid var(--border);
  background: var(--toc-bg);
  font-size: 13.75px;
  line-height: 1.38;
}

#TOC::-webkit-scrollbar {
  width: 10px;
}

#TOC::-webkit-scrollbar-track {
  background: var(--scrollbar-track);
}

#TOC::-webkit-scrollbar-thumb {
  border: 3px solid var(--toc-bg);
  border-radius: 999px;
  background: var(--scrollbar-thumb);
}

#TOC h2 {
  position: sticky;
  top: 0;
  z-index: 2;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 -4px 8px;
  padding: 16px 8px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--toc-bg);
}

#TOC ul {
  list-style: none;
  margin: 0;
  padding-left: 0;
}

/* Reclaim the extra indent Pandoc adds under the chapter's single H1. */
#TOC > ul > li > ul {
  margin-left: 0;
  padding-left: 0;
  border-left: 0;
}

#TOC > ul > li > ul ul {
  margin: 2px 0 6px 9px;
  padding-left: 9px;
  border-left: 1px solid var(--toc-guide);
}

#TOC li {
  margin: 1px 0;
}

#TOC li[hidden] {
  display: none;
}

#TOC a {
  display: block;
  padding: 5px 8px;
  border-radius: 5px;
  color: var(--muted);
  overflow-wrap: anywhere;
  transition: color 0.15s ease, background-color 0.15s ease;
}

#TOC > ul > li {
  margin-top: 6px;
}

#TOC > ul > li > a {
  color: var(--text);
  font-weight: 700;
  font-size: 14.25px;
}

#TOC > ul > li > ul > li {
  margin-top: 5px;
}

#TOC > ul > li > ul > li > a {
  color: var(--text);
  font-weight: 500;
  font-size: 13.75px;
}

#TOC > ul > li > ul ul a {
  font-size: 13px;
  padding: 4px 8px;
}

#TOC a:hover {
  color: var(--toc-active);
  text-decoration: none;
  background: var(--toc-hover);
}

/* The section being read, marked by site.js. */
#TOC a[aria-current] {
  color: var(--toc-active);
  background: var(--toc-active-bg);
  font-weight: 600;
}

#TOC a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

#TOC code {
  padding: 1px 3px;
  border: 0;
  font-size: 0.9em;
}

/* --- Responsive --- */
@media (max-width: 1100px) {
  body {
    margin-left: 0;
    padding: 24px max(18px, calc((100vw - var(--content-width)) / 2)) 72px;
  }

  #TOC {
    position: relative;
    top: 0;
    width: auto;
    height: auto;
    max-height: min(52dvh, 30rem);
    margin-bottom: 28px;
    padding: 0 12px 24px 14px;
    border: 1px solid var(--border);
    border-radius: 8px;
  }

  #TOC h2 {
    top: 0;
  }

  .page-home {
    display: none;
  }
}

@media (max-width: 780px) {
  body {
    font-size: 16.5px;
  }

  table {
    display: block;
    overflow-x: auto;
    width: max-content;
    min-width: 100%;
    max-width: 100%;
  }

  .chapter-resources {
    flex-direction: column;
    align-items: flex-start;
  }
}

@media (max-width: 600px) {
  .study-notes > .notes-content { padding: 8px 0 0 10px; }
  .note-group { column-gap: 8px; }
  .chapter-map { padding: 12px 15px; }
}

@media print {
  body {
    margin: 0;
    padding: 0;
    font-size: 11pt;
    color: #000000;
    background: #ffffff;
  }

  #TOC,
  .page-home,
  .chapter-nav,
  .chapter-resources,
  .notes-drawer {
    display: none;
  }

  h2, h3 {
    break-after: avoid;
  }

  .study-notes > summary { font-weight: 600; color: #000000; }
  .study-notes[open] > summary { position: static; border-bottom: 0; }
  .study-diagram, .chapter-map, pre { break-inside: avoid; }
}

/* --- Comments & Thalassa CGI --- */
body.cgi-page {
  margin: 56px auto;
  padding: 0 20px 56px;
  max-width: 600px;
  font-size: 15.5px;
  line-height: 1.6;
}

.comments-section {
  margin-top: 40px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
}

.comments-section h2 {
  font-family: var(--mono);
  font-size: 11.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin: 0 0 6px;
  border: 0;
  padding: 0;
}

.comments-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 14px;
  margin: 10px 0 16px;
  font-family: var(--mono);
  font-size: 12.5px;
}

.btn-inline {
  display: inline-block;
  color: var(--accent);
  font-weight: 500;
  text-decoration: none;
}

.btn-inline:hover {
  text-decoration: underline;
  text-underline-offset: 3px;
}

.comments-actions a:not(.btn-inline) {
  color: var(--muted);
  padding-left: 14px;
  border-left: 1px solid var(--border);
  text-decoration: none;
}

.comments-actions a:not(.btn-inline):hover {
  color: var(--text);
  text-decoration: underline;
  text-underline-offset: 3px;
}

.comments-list {
  display: flex;
  flex-direction: column;
}

.comment-card,
.comment {
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.comment-children {
  margin-left: 16px;
  padding-left: 12px;
  border-left: 1px solid var(--border);
  margin-top: 6px;
}

.comment-meta,
.comment_head p {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  margin: 0 0 4px;
}

.comment-title {
  font-size: 14.5px;
  font-weight: 600;
  margin: 0 0 4px;
}

.comment-body p:last-child {
  margin-bottom: 0;
}

.comment-links {
  margin-top: 5px;
  font-family: var(--mono);
  font-size: 12px;
}

.note {
  font-size: 13px;
  color: var(--muted);
}

.cgi-header {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  padding-bottom: 12px;
  margin-bottom: 18px;
  border-bottom: 1px solid var(--border);
}

.cgi-brand {
  font-weight: 600;
  color: var(--text);
  text-decoration: none;
}

.cgi-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  font-family: var(--mono);
  font-size: 12.5px;
}

.messagebox_ok {
  padding: 8px 12px;
  border-left: 2px solid var(--accent);
  background: var(--code-bg);
  font-size: 13.5px;
  margin: 12px 0;
}

.messagebox_err {
  padding: 8px 12px;
  border-left: 2px solid var(--err-text);
  color: var(--err-text);
  background: var(--code-bg);
  font-size: 13.5px;
  margin: 12px 0;
}

.form_item {
  margin-bottom: 12px;
}

.form_item label {
  display: block;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 4px;
}

body.cgi-page input[type="text"],
body.cgi-page textarea {
  width: 100%;
  max-width: 100%;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 14.5px;
}

body.cgi-page input[type="submit"],
body.cgi-page button[type="submit"] {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--code-bg);
  color: var(--text);
  font-family: var(--mono);
  font-size: 13px;
  cursor: pointer;
}

body.cgi-page input[type="submit"]:hover,
body.cgi-page button[type="submit"]:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.tag {
  display: inline-block;
  font-family: var(--mono);
  font-size: 11px;
  padding: 0 5px;
  background: var(--code-bg);
}
