*, *::before, *::after { box-sizing: border-box; }

:root {
  color-scheme: dark;
  --page: #151515;
  --surface: #1c1c1b;
  --inset: #181817;
  --hover: #272727;
  --line: #2e2e2c;
  --line-strong: #41413e;
  --ink: #eeeeeb;
  --muted: #aaa9a5;
  --faint: #7d7c78;
  --focus: #d4d4ce;
  --accent: #3987e5;
  --accent-ink: #8fbcf2;
  --accent-soft: rgba(57, 135, 229, .16);
  --accent-line: rgba(57, 135, 229, .5);
  --second: #d39a2c;
  --second-soft: rgba(211, 154, 44, .17);
  --second-line: rgba(211, 154, 44, .55);
  --shadow: 0 12px 40px rgba(0, 0, 0, .45);
  --bar: rgba(21, 21, 21, .82);
  --scrim: rgba(0, 0, 0, .5);
  --check: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.5 8.5l3 3 6-7' fill='none' stroke='%23fff' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  --sans: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: "JetBrains Mono", ui-monospace, "Cascadia Mono", Consolas, monospace;
  --gutter: clamp(16px, 4vw, 32px);
}

:root[data-theme="light"] {
  color-scheme: light;
  --page: #f7f7f4;
  --surface: #ffffff;
  --inset: #fbfbf9;
  --hover: #eeeee9;
  --line: #e2e2dc;
  --line-strong: #c9c9c1;
  --ink: #232321;
  --muted: #65655f;
  --faint: #8a8983;
  --focus: #565652;
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --accent-soft: rgba(42, 120, 214, .1);
  --accent-line: rgba(42, 120, 214, .45);
  --second: #b07400;
  --second-soft: rgba(237, 161, 0, .14);
  --second-line: rgba(176, 116, 0, .5);
  --shadow: 0 12px 40px rgba(35, 35, 33, .14);
  --bar: rgba(247, 247, 244, .86);
  --scrim: rgba(35, 35, 33, .28);
}

html { scroll-behavior: smooth; scroll-padding-top: 28px; }

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16.5px;
  line-height: 1.7;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

a { color: var(--accent-ink); text-decoration-thickness: 1px; text-underline-offset: 3px; }
a:hover { color: var(--ink); }
button, input, select { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 6px; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.icon { width: 16px; height: 16px; flex: none; }

/* layout */

.layout {
  display: grid;
  grid-template-columns: 252px minmax(0, 740px);
  justify-content: center;
  gap: 72px;
  padding: 0 var(--gutter);
}

.topbar { display: none; }
.scrim { display: none; }

.toc {
  position: sticky;
  top: 0;
  align-self: start;
  height: 100vh;
  overflow-y: auto;
  padding: 36px 6px 32px 0;
  scrollbar-width: thin;
  scrollbar-color: var(--line-strong) transparent;
}

.toc-title {
  display: block;
  color: var(--ink);
  font-weight: 600;
  font-size: 15px;
  line-height: 1.35;
  text-decoration: none;
}

.toc-sub { margin-top: 3px; color: var(--muted); font-size: 13px; }

.progress { margin: 22px 0 24px; }
.progress-text {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}
.progress-bar {
  height: 4px;
  margin-top: 8px;
  border-radius: 2px;
  background: var(--line);
  overflow: hidden;
}
.progress-bar span {
  display: block;
  width: 0;
  height: 100%;
  background: var(--accent);
  transition: width .3s ease;
}

.toc ol { list-style: none; margin: 0; padding: 0; }
.toc-lecture { margin-bottom: 14px; }
.toc-lecture > a {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 3px 0;
  color: var(--ink);
  font-size: 14px;
  font-weight: 600;
  line-height: 1.4;
  text-decoration: none;
}
.toc-count { flex: none; color: var(--faint); font-weight: 500; font-variant-numeric: tabular-nums; }
.toc-topic a {
  display: flex;
  align-items: baseline;
  gap: 9px;
  padding: 3px 0 3px 10px;
  border-left: 2px solid var(--line);
  color: var(--muted);
  font-size: 13.5px;
  line-height: 1.45;
  text-decoration: none;
}
.toc-topic a:hover { color: var(--ink); }
.toc-topic a.current { color: var(--ink); border-left-color: var(--accent); }
.toc-check {
  flex: none;
  width: 12px;
  height: 12px;
  border: 1.5px solid var(--line-strong);
  border-radius: 3px;
  transform: translateY(1.5px);
}
.toc-topic.done .toc-check {
  border-color: var(--accent);
  background: var(--accent) var(--check) center / 10px no-repeat;
}

.toc-foot {
  display: flex;
  margin-top: 22px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.icon-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 32px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 13.5px;
  cursor: pointer;
}
.icon-button:hover { color: var(--ink); background: var(--hover); }
:root[data-theme="light"] .theme-dark-only,
:root[data-theme="dark"] .theme-light-only { display: none; }

/* text */

main { min-width: 0; padding: 60px 0 160px; }

h1 {
  margin: 0 0 14px;
  font-size: 34px;
  line-height: 1.2;
  letter-spacing: -.02em;
  font-weight: 650;
}

.lead { margin: 0 0 12px; color: var(--muted); font-size: 17.5px; line-height: 1.6; }

.lecture { margin-top: 64px; padding-top: 56px; border-top: 1px solid var(--line); }

h2 {
  margin: 0;
  font-size: 27px;
  line-height: 1.25;
  letter-spacing: -.015em;
  font-weight: 650;
}

.topic { margin-top: 44px; }

h3 {
  margin: 0 0 12px;
  font-size: 20.5px;
  line-height: 1.3;
  letter-spacing: -.01em;
  font-weight: 600;
}

h4 { margin: 26px 0 6px; font-size: 16.5px; line-height: 1.4; font-weight: 600; }

p { margin: 0 0 14px; }
ul, ol { margin: 0 0 16px; padding-left: 24px; }
li { margin: 5px 0; padding-left: 2px; }
li::marker { color: var(--faint); }
li > ul { margin: 6px 0 0; }
strong { font-weight: 600; }

.katex { font-size: 1.07em; }
.katex-display { margin: 14px 0 18px; padding: 4px 0; overflow-x: auto; overflow-y: hidden; }

.bits { font-family: var(--mono); font-size: .9em; letter-spacing: .03em; }

.note { margin: 0 0 16px; padding: 1px 0 1px 16px; border-left: 2px solid var(--line-strong); }
.note-label { display: block; color: var(--faint); font-size: 13.5px; }
.note p:last-child { margin-bottom: 0; }

.table-wrap { margin: 6px 0 20px; overflow-x: auto; }
table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 15px;
  font-variant-numeric: tabular-nums;
}
table.data th,
table.data td { padding: 8px 12px 8px 0; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
table.data th { color: var(--muted); font-size: 13.5px; font-weight: 500; }
table.data td:first-child { white-space: nowrap; }

/* studied mark */

.mark {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  margin-top: 6px;
  padding: 6px 12px 6px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--muted);
  font-size: 14px;
  cursor: pointer;
  user-select: none;
}
.mark:hover { color: var(--ink); background: var(--hover); }
.mark input {
  appearance: none;
  width: 16px;
  height: 16px;
  margin: 0;
  border: 1.5px solid var(--line-strong);
  border-radius: 4px;
  cursor: pointer;
}
.mark input:checked { border-color: var(--accent); background: var(--accent) var(--check) center / 12px no-repeat; }
.mark:has(input:checked) { color: var(--ink); }

/* self-check */

.quiz { margin-top: 48px; }
.quiz details { border-bottom: 1px solid var(--line); }
.quiz details:first-of-type { border-top: 1px solid var(--line); }
.quiz summary {
  position: relative;
  padding: 12px 32px 12px 0;
  font-weight: 500;
  list-style: none;
  cursor: pointer;
}
.quiz summary::-webkit-details-marker { display: none; }
.quiz summary::after {
  content: "";
  position: absolute;
  right: 6px;
  top: 20px;
  width: 8px;
  height: 8px;
  border-right: 1.5px solid var(--faint);
  border-bottom: 1.5px solid var(--faint);
  transform: rotate(45deg);
  transition: transform .15s ease;
}
.quiz details[open] summary::after { top: 24px; transform: rotate(-135deg); }
.quiz summary:hover { color: var(--accent-ink); }
.answer { padding: 0 0 14px; }
.answer p:last-child, .answer ul:last-child { margin-bottom: 0; }

/* demos */

.demo {
  margin: 24px 0 28px;
  padding: 18px 20px 20px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
}
.demo-head h4 { margin: 0 0 2px; font-size: 15.5px; }
.demo-head p { margin: 0 0 14px; color: var(--muted); font-size: 14.5px; line-height: 1.55; }

.demo canvas {
  display: block;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--inset);
  touch-action: manipulation;
}
.demo canvas.clickable { cursor: crosshair; }

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 12px 20px;
  margin-top: 14px;
}
.buttons { display: flex; flex-wrap: wrap; gap: 8px; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 34px;
  padding: 0 13px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
}
.btn:hover { background: var(--hover); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { border-color: var(--accent); background: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent); filter: brightness(1.07); }
.btn .icon { width: 14px; height: 14px; }

.slider { display: grid; gap: 2px; flex: 1 1 140px; min-width: 130px; max-width: 210px; }
.sliders {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 10px 24px;
  margin-top: 14px;
}
.sliders .slider { max-width: none; }
.slider-head { display: flex; justify-content: space-between; gap: 8px; color: var(--muted); font-size: 13px; }
.slider-head output { color: var(--ink); font-variant-numeric: tabular-nums; }

input[type="range"] {
  width: 100%;
  height: 20px;
  margin: 0;
  background: transparent;
  appearance: none;
  cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track { height: 4px; border-radius: 2px; background: var(--line-strong); }
input[type="range"]::-moz-range-track { height: 4px; border-radius: 2px; background: var(--line-strong); }
input[type="range"]::-webkit-slider-thumb {
  appearance: none;
  width: 14px;
  height: 14px;
  margin-top: -5px;
  border: 2px solid var(--surface);
  border-radius: 50%;
  background: var(--accent);
}
input[type="range"]::-moz-range-thumb {
  width: 12px;
  height: 12px;
  border: 2px solid var(--surface);
  border-radius: 50%;
  background: var(--accent);
}

.segmented {
  display: inline-flex;
  flex-wrap: wrap;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  overflow: hidden;
}
.segmented label {
  position: relative;
  padding: 6px 12px;
  border-right: 1px solid var(--line);
  color: var(--muted);
  font-size: 13.5px;
  cursor: pointer;
}
.segmented label:last-child { border-right: 0; }
.segmented label:hover { color: var(--ink); }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label:has(input:checked) { background: var(--accent-soft); color: var(--accent-ink); }
.segmented label:has(input:focus-visible) { outline: 2px solid var(--focus); outline-offset: -2px; }

.field { display: grid; gap: 4px; color: var(--muted); font-size: 13px; }
.field input[type="text"],
.field input[type="number"] {
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: var(--inset);
  color: var(--ink);
  font-size: 15px;
}
.field input.mono { font-family: var(--mono); letter-spacing: .04em; }
.field input[aria-invalid="true"] { border-color: #d03b3b; }

.stats { display: flex; flex-wrap: wrap; gap: 10px 30px; margin-top: 14px; }
.stat { display: grid; }
.stat span { color: var(--muted); font-size: 12.5px; }
.stat b { font-size: 15.5px; font-weight: 600; font-variant-numeric: tabular-nums; }

.demo-msg { min-height: 1.6em; margin: 12px 0 0; color: var(--muted); font-size: 14.5px; line-height: 1.55; }
.demo-msg strong { color: var(--ink); }

.legend { display: flex; flex-wrap: wrap; gap: 6px 18px; margin-top: 10px; color: var(--muted); font-size: 13px; }
.legend i { display: inline-block; width: 14px; height: 3px; margin-right: 7px; border-radius: 2px; vertical-align: middle; }
.legend i.legend-dot { width: 10px; height: 10px; border: 1.5px solid var(--ink); border-radius: 50%; }
.legend i.nest { background: var(--surface); }
.legend i.food { background: var(--second); }
.legend i.ant { width: 6px; height: 6px; border: 0; background: var(--ink); }

/* bits */

.bitgrid { display: grid; gap: 8px; margin-top: 4px; padding: 6px 0 4px; overflow-x: auto; overflow-y: hidden; }
.bitline { display: flex; align-items: center; gap: 12px; }
.bitline-name { flex: none; width: 104px; color: var(--muted); font-size: 13.5px; }
.bitrow { position: relative; display: flex; gap: 6px; }
.bit {
  display: grid;
  place-items: center;
  width: 30px;
  height: 34px;
  flex: none;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--inset);
  font-family: var(--mono);
  font-size: 15px;
  font-variant-numeric: tabular-nums;
}
button.bit { padding: 0; cursor: pointer; }
button.bit:hover { border-color: var(--focus); }
.bit.p1 { border-color: var(--accent-line); background: var(--accent-soft); }
.bit.p2 { border-color: var(--second-line); background: var(--second-soft); }
.bit.fixed { border-color: var(--accent-line); background: var(--accent-soft); color: var(--accent-ink); }
.bit.flip { outline: 2px solid var(--ink); outline-offset: 1px; }
.bit.small { width: 22px; height: 26px; font-size: 13px; border-radius: 5px; }

.bitrow.split { gap: 0; }
.sep { position: relative; width: 10px; flex: none; border: 0; padding: 0; background: transparent; }
button.sep { cursor: pointer; }
.sep::before {
  content: "";
  position: absolute;
  left: 4px;
  top: -5px;
  bottom: -5px;
  width: 2px;
  border-radius: 1px;
  background: transparent;
}
button.sep:hover::before { background: var(--line-strong); }
.sep.on::before { background: var(--ink); }

.idx { display: flex; gap: 6px; color: var(--faint); font-size: 11.5px; font-variant-numeric: tabular-nums; }
.idx span { width: 30px; flex: none; text-align: center; }
.span-mark { display: flex; gap: 6px; margin-top: 4px; }
.span-mark span { width: 30px; height: 3px; flex: none; border-radius: 2px; }
.span-mark span.in { background: var(--accent); }

.members { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 8px; font-family: var(--mono); font-size: 14px; }

/* population table */

.pop { width: 100%; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
.pop th, .pop td { padding: 5px 10px 5px 0; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
.pop th { color: var(--muted); font-size: 12.5px; font-weight: 500; }
.pop .bitrow { gap: 3px; }
.pop tr.best td { color: var(--accent-ink); }
.share { position: relative; width: 120px; height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; }
.share span { position: absolute; inset: 0 auto 0 0; background: var(--accent); border-radius: 3px; }

.steps-log { margin: 12px 0 0; padding: 0; list-style: none; color: var(--muted); font-size: 14px; }
.steps-log li { margin: 3px 0; padding: 0; }
.steps-log b { color: var(--ink); font-weight: 500; }

/* selection bars */

.sel-rows { display: grid; gap: 6px; margin-top: 4px; }
.sel-row {
  display: grid;
  grid-template-columns: 34px 74px minmax(0, 1fr) 60px;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}
.sel-row input { width: 74px; height: 30px; padding: 0 8px; border: 1px solid var(--line-strong); border-radius: 7px; background: var(--inset); color: var(--ink); }
.sel-name { color: var(--muted); }
.sel-bar { position: relative; height: 22px; border-radius: 6px; background: var(--inset); border: 1px solid var(--line); overflow: hidden; }
.sel-bar .theory { position: absolute; inset: 0 auto 0 0; background: var(--accent-soft); border-right: 2px solid var(--accent); transition: width .25s ease; }
.sel-bar .emp { position: absolute; top: 3px; bottom: 3px; width: 2px; margin-left: -1px; background: var(--second); transition: left .25s ease; }
.sel-row.picked .sel-bar { border-color: var(--ink); }
.sel-row.in-tour .sel-name { color: var(--ink); }
.sel-row.picked .sel-name { color: var(--accent-ink); font-weight: 600; }
.sel-pct { text-align: right; color: var(--muted); }

.strip { position: relative; display: flex; height: 30px; margin-top: 14px; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.strip span {
  display: grid;
  place-items: center;
  min-width: 0;
  border-right: 1px solid var(--surface);
  background: var(--accent-soft);
  color: var(--muted);
  font-size: 12px;
  overflow: hidden;
}
.strip span:nth-child(even) { background: var(--second-soft); }
.strip span.hit { background: var(--accent); color: #fff; }
.strip-pointer { position: absolute; top: -2px; bottom: -2px; width: 2px; background: var(--ink); transition: left .4s ease; }

/* responsive */

@media (max-width: 1080px) {
  html { scroll-padding-top: 76px; }
  .layout { grid-template-columns: minmax(0, 740px); gap: 0; }
  .topbar {
    position: sticky;
    top: 0;
    z-index: 40;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    height: 56px;
    padding: 0 var(--gutter);
    border-bottom: 1px solid var(--line);
    background: var(--bar);
    backdrop-filter: saturate(1.4) blur(14px);
    -webkit-backdrop-filter: saturate(1.4) blur(14px);
  }
  .topbar-title { overflow: hidden; color: var(--ink); font-weight: 600; font-size: 15px; text-overflow: ellipsis; white-space: nowrap; text-decoration: none; }
  .toc {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 60;
    width: min(330px, 86vw);
    height: auto;
    padding: 24px 20px;
    border-right: 1px solid var(--line);
    background: var(--surface);
    transform: translateX(-102%);
    transition: transform .22s ease;
  }
  .toc.open { transform: none; box-shadow: var(--shadow); }
  .scrim { position: fixed; inset: 0; z-index: 55; display: block; background: var(--scrim); opacity: 0; pointer-events: none; transition: opacity .22s ease; }
  .scrim.open { opacity: 1; pointer-events: auto; }
  main { padding-top: 32px; }
}

@media (max-width: 600px) {
  body { font-size: 16px; }
  h1 { font-size: 28px; }
  h2 { font-size: 23px; }
  h3 { font-size: 19px; }
  .demo { padding: 14px 14px 16px; margin-inline: -6px; }
  .bit { width: 24px; height: 30px; font-size: 14px; border-radius: 5px; }
  .span-mark span, .idx span { width: 24px; }
  .sep { width: 6px; }
  .sep::before { left: 2px; }
  .bitline { gap: 8px; }
  .bitline-name { width: 66px; font-size: 12px; }
  .pop .share { display: none; }
  .katex-display > .katex { font-size: .88em; }
  .pop th, .pop td { padding-right: 8px; }
  .sel-row { grid-template-columns: 28px 62px minmax(0, 1fr) 50px; gap: 8px; }
  .sel-row input { width: 62px; }
  .slider { max-width: none; }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  * { transition: none !important; }
}
