:root {
  --desk: #e9e7ee;
  --page: #ffffff;
  --ink: #1d1c22;
  --muted: #6b6878;
  --rule: #d9d6e1;
  --link: #4b3f9e;
  --header-bg: #ffffff;
  --shadow: 0 1px 3px rgba(40, 30, 70, 0.25), 0 10px 30px rgba(40, 30, 70, 0.28);
  --code-bg: #f3f1f7;
  --sketch-bg: #fbfaf7;
  --sketch-filter: none;
  --sidebar-w: 260px;
  --header-h: 44px;
}

html[data-theme="dark"] {
  --desk: #1e1c25;
  --page: #2a2833;
  --ink: #e6e3ee;
  --muted: #9b97a8;
  --rule: #3d3a48;
  --link: #b3a6f5;
  --header-bg: #2a2833;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.7), 0 10px 30px rgba(0, 0, 0, 0.65);
  --code-bg: #1e1c25;
  --sketch-bg: #23212b;
  --sketch-filter: invert(1) hue-rotate(180deg);
}

* { box-sizing: border-box; }

html { font-size: 16px; }

body {
  margin: 0;
  background: var(--desk);
  color: var(--ink);
  font-family: Georgia, "Times New Roman", serif;
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

a { color: var(--link); }

/* Header */

.header {
  position: sticky;
  top: 0;
  z-index: 20;
  height: var(--header-h);
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0 1rem;
  background: var(--header-bg);
  border-bottom: 1px solid var(--rule);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 0.9rem;
}

.site-title {
  color: var(--ink);
  text-decoration: none;
  font-weight: 600;
}

.nav { display: flex; gap: 1rem; margin-left: auto; }
.nav a { color: var(--muted); text-decoration: none; }
.nav a:hover, .nav a.active { color: var(--ink); }

.theme-button {
  background: none;
  border: 1px solid var(--rule);
  border-radius: 4px;
  color: var(--muted);
  cursor: pointer;
  font: inherit;
  line-height: 1;
  padding: 0.3rem 0.5rem;
}
.theme-button:hover { color: var(--ink); }
.theme-button .moon, html[data-theme="dark"] .theme-button .sun { display: none; }
html[data-theme="dark"] .theme-button .moon { display: inline; }

.menu-button {
  display: none;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  width: 28px;
  height: 28px;
  cursor: pointer;
}
.menu-button span {
  display: block;
  height: 2px;
  background: var(--ink);
}

/* Layout */

.layout {
  display: flex;
  flex: 1;
  align-items: flex-start;
}

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  position: sticky;
  top: var(--header-h);
  max-height: calc(100vh - var(--header-h));
  overflow-y: auto;
  padding: 1.5rem 1rem;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 0.85rem;
}

.sidebar-heading {
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 0.5rem;
}
.sidebar-heading + * + .sidebar-heading { margin-top: 1.5rem; }

.sidebar ul, .sidebar ol { list-style: none; margin: 0; padding: 0; }
.sidebar ul ul { padding-left: 0.9rem; }
.sidebar li { margin: 0.2rem 0; }
.sidebar a { color: var(--ink); text-decoration: none; }
.sidebar a:hover { text-decoration: underline; }
.sidebar a.current { color: var(--link); font-weight: 600; }
.contents > li > a { font-weight: 600; }
.contents { margin-bottom: 1.5rem; }
.toc a { color: var(--muted); }
.toc ul ul { padding-left: 0.9rem; }

.sidebar-scrim { display: none; }

/* The page */

.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 2rem 0;
  gap: 2rem;
}

.page {
  width: 100%;
  max-width: 760px;
  margin: 0;
  padding: 3rem 3.5rem;
  background: var(--page);
  box-shadow: var(--shadow);
}

.title { margin-top: 0; font-size: 2rem; line-height: 1.2; }
.meta { color: var(--muted); font-size: 0.9rem; margin-top: -0.5rem; }

.page h2, .page h3 { margin-top: 2rem; line-height: 1.25; }
.page h2 { font-size: 1.4rem; }
.page h3 { font-size: 1.15rem; }

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

.page blockquote {
  margin: 1rem 0;
  padding-left: 1rem;
  border-left: 3px solid var(--rule);
  color: var(--muted);
}

.page code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.875em;
  background: var(--code-bg);
  padding: 0.1em 0.3em;
  border-radius: 3px;
}
.page pre {
  overflow-x: auto;
  padding: 1rem;
  background: var(--code-bg) !important;
  border-radius: 4px;
  line-height: 1.45;
}
.page pre code { background: none; padding: 0; font-size: 0.85rem; }

.page table { border-collapse: collapse; width: 100%; margin: 1rem 0; }
.page th, .page td { border: 1px solid var(--rule); padding: 0.4rem 0.6rem; text-align: left; }
.page th { background: var(--code-bg); }

.page hr { border: 0; border-top: 1px solid var(--rule); margin: 2rem 0; }

.post-list { list-style: none; padding: 0; }
.post-list li { margin: 0 0 1.25rem; }
.post-list li a { font-size: 1.1rem; }
.post-list time { display: block; color: var(--muted); font-size: 0.85rem; }
.post-list p { margin: 0.25rem 0 0; }

.footer {
  padding: 1rem;
  text-align: center;
  color: var(--muted);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 0.8rem;
}

/* Mobile: the sidebar becomes a drawer */

@media (max-width: 900px) {
  .menu-button { display: flex; }

  .sidebar {
    position: fixed;
    top: var(--header-h);
    left: 0;
    bottom: 0;
    max-height: none;
    background: var(--page);
    border-right: 1px solid var(--rule);
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    z-index: 15;
  }
  .sidebar-toggle:checked ~ .layout .sidebar { transform: none; }

  .sidebar-scrim {
    display: block;
    position: fixed;
    inset: var(--header-h) 0 0 0;
    background: rgba(0, 0, 0, 0.3);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
    z-index: 10;
  }
  .sidebar-toggle:checked ~ .layout .sidebar-scrim { opacity: 1; pointer-events: auto; }

  .main { padding: 1rem; gap: 1rem; }
  .page { padding: 1.5rem 1.25rem; }
  .title { font-size: 1.6rem; }
  .nav { display: none; }
}

/* Hand drawn sketches from the reMarkable. A paper card with a pencil line
   border, slightly off white so a white png does not vanish into the page.
   In dark mode the png is inverted so black ink reads as light ink. */

.sketch {
  margin: 1.75rem 0;
  padding: 1rem;
  background: var(--sketch-bg);
  border: 1px solid var(--rule);
  border-radius: 6px;
  box-shadow: 0 1px 2px rgba(40, 30, 70, 0.08);
  text-align: center;
}
.sketch img {
  display: block;
  margin: 0 auto;
  max-width: 100%;
  height: auto;
  filter: var(--sketch-filter);
  mix-blend-mode: multiply;
}
html[data-theme="dark"] .sketch img { mix-blend-mode: screen; }
.sketch figcaption {
  margin-top: 0.75rem;
  color: var(--muted);
  font-size: 0.85rem;
  font-style: italic;
}
.sketch-wide {
  margin-left: -3.5rem;
  margin-right: -3.5rem;
  border-left: 0;
  border-right: 0;
  border-radius: 0;
}
.sketch-small img { max-width: 60%; }

@media (max-width: 900px) {
  .sketch-wide { margin-left: -1.25rem; margin-right: -1.25rem; }
  .sketch-small img { max-width: 100%; }
}

/* Book outline in the sidebar */

.sidebar-heading a { color: inherit; text-decoration: none; }
.part { margin: 0 0 0.5rem; }
.part summary {
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  padding: 0.15rem 0;
}
.part summary::-webkit-details-marker { display: none; }
.part summary::before {
  content: "";
  flex: none;
  width: 0;
  height: 0;
  border: 4px solid transparent;
  border-left: 5px solid var(--muted);
  margin-right: -0.2rem;
  transform: translateY(-1px);
  transition: transform 0.15s ease;
}
.part[open] summary::before { transform: rotate(90deg) translateX(-1px); }
.part summary a { font-weight: 600; }
.part-label {
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  white-space: nowrap;
}
.chapters { padding-left: 1rem !important; margin: 0.2rem 0 0.4rem !important; }
.chapters li { margin: 0; }
.chapters a { display: flex; gap: 0.5rem; padding: 0.12rem 0; }
.chapters .num { flex: none; width: 2.2rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.chapters .name { flex: 1; }
.chapters .badge { margin-left: 0.4rem; }
.badge {
  display: inline-block;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 0.6rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--link);
  border: 1px solid currentColor;
  border-radius: 3px;
  padding: 0 0.3rem;
  line-height: 1.5;
  vertical-align: middle;
}

/* Chapter and part pages */

.crumb {
  margin: 0 0 0.5rem;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}
.crumb a { color: inherit; text-decoration: none; }
.crumb a:hover { color: var(--ink); }
.title .num { color: var(--muted); font-weight: normal; margin-right: 0.25rem; }
.chapter-list { padding-left: 0; list-style: none; }
.chapter-list li { margin: 0.35rem 0; }
.chapter-list a { text-decoration: none; }
.chapter-list a:hover { text-decoration: underline; }
.chapter-list .num { display: inline-block; width: 2.6rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.outline-part { margin-top: 2.5rem; }
.outline-part h2 { margin: 0 0 0.25rem; }
.outline-part h2 a { color: inherit; text-decoration: none; }
.outline-part .summary { margin: 0 0 0.75rem; color: var(--muted); }

.pager {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin-top: 3rem;
  padding-top: 1rem;
  border-top: 1px solid var(--rule);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 0.9rem;
}
.pager a { text-decoration: none; max-width: 48%; }
.pager a span { display: block; font-size: 0.7rem; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
.pager .next { margin-left: auto; text-align: right; }

/* Concepts and implementation modes */

.mode {
  display: flex;
  border: 1px solid var(--rule);
  border-radius: 4px;
  overflow: hidden;
}
.mode button {
  background: none;
  border: 0;
  color: var(--muted);
  cursor: pointer;
  font: inherit;
  font-size: 0.8rem;
  padding: 0.3rem 0.6rem;
}
.mode button + button { border-left: 1px solid var(--rule); }
html[data-mode="concepts"] .mode [data-mode="concepts"],
html[data-mode="implementation"] .mode [data-mode="implementation"] {
  background: var(--desk);
  color: var(--ink);
}

.impl {
  margin: 1.75rem 0;
  border: 1px solid var(--rule);
  border-left: 3px solid var(--link);
  border-radius: 4px;
  background: var(--code-bg);
}
.impl-head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.9rem;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 0.8rem;
}
.impl-tag {
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--link);
}
.impl-title { color: var(--ink); }
.impl-toggle {
  display: none;
  margin-left: auto;
  background: none;
  border: 1px solid var(--rule);
  border-radius: 3px;
  color: var(--muted);
  cursor: pointer;
  font: inherit;
  font-size: 0.75rem;
  padding: 0.1rem 0.5rem;
}
.impl-toggle .hide { display: none; }
.impl-body { padding: 0 1rem 0.25rem; }
.impl-body > :first-child { margin-top: 0.25rem; }
.impl-body pre { background: var(--page) !important; }

html[data-mode="concepts"] .impl-toggle { display: inline-block; }
html[data-mode="concepts"] .impl:not(.shown) .impl-body { display: none; }
html[data-mode="concepts"] .impl.shown .impl-toggle .show { display: none; }
html[data-mode="concepts"] .impl.shown .impl-toggle .hide { display: inline; }
html[data-mode="concepts"] .toc .impl-heading { display: none; }

@media (max-width: 900px) {
  .mode { margin-left: auto; }
  .mode button { padding: 0.3rem 0.45rem; font-size: 0.75rem; }
}

/* Side notes. A yellow bubble that hangs in the right margin when the screen
   is wide enough and drops inline otherwise. */

.note {
  --note-bg: #fff8c4;
  --note-ink: #3d3610;
  --note-rule: #eadf8a;
  background: var(--note-bg);
  color: var(--note-ink);
  border: 1px solid var(--note-rule);
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(60, 50, 0, 0.18), 0 8px 18px rgba(60, 50, 0, 0.16);
  padding: 0.6rem 0.85rem;
  margin: 1.25rem 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 0.82rem;
  line-height: 1.45;
}
.note > :first-child { margin-top: 0; }
.note > :last-child { margin-bottom: 0; }
.note a { color: inherit; }
.note code { background: rgba(0, 0, 0, 0.07); }
html[data-theme="dark"] .note {
  --note-bg: #4a4320;
  --note-ink: #f3ecc0;
  --note-rule: #6a6030;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.5), 0 8px 18px rgba(0, 0, 0, 0.4);
}

.page { position: relative; }
.page:target { scroll-margin-top: calc(var(--header-h) + 1rem); }
.permalink {
  margin-top: 2.5rem;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 0.8rem;
}
.permalink a { color: var(--muted); text-decoration: none; }
.permalink a:hover { color: var(--link); text-decoration: underline; }

@media (min-width: 1360px) {
  .note {
    position: absolute;
    right: -230px;
    width: 200px;
    margin: 0;
  }
  .note::before {
    content: "";
    position: absolute;
    left: -7px;
    top: 14px;
    width: 12px;
    height: 12px;
    background: var(--note-bg);
    border-left: 1px solid var(--note-rule);
    border-bottom: 1px solid var(--note-rule);
    transform: rotate(45deg);
  }
}

/* Info boxes. Purple, always in the flow of the page. Side notes stay yellow. */

.info {
  --note-bg: #efeafc;
  --note-ink: #2e2550;
  --note-rule: #cfc4f0;
  background: var(--note-bg);
  color: var(--note-ink);
  border: 1px solid var(--note-rule);
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(50, 30, 100, 0.18), 0 8px 18px rgba(50, 30, 100, 0.16);
  padding: 0.9rem 1.1rem;
  margin: 1.5rem 0;
}
.info > :first-child { margin-top: 0; }
.info > :last-child { margin-bottom: 0; }
.info a { color: inherit; }
.info code { background: rgba(0, 0, 0, 0.07); }
.info-title {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  margin: 0 0 0.4rem;
  opacity: 0.8;
}
html[data-theme="dark"] .info {
  --note-bg: #3a3260;
  --note-ink: #e8e1ff;
  --note-rule: #574c8a;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.5), 0 8px 18px rgba(0, 0, 0, 0.4);
}
