/* ==========================================================================
   Learn You Some Canton — Docs / Reading Stylesheet
   Design Goals:
   1. Bright paper surfaces (Paper default, Cream, Night) with flat Docs chrome
   2. High-legibility Korean + English typography (Sans & Book Serif modes)
   3. Single-column reading measure (~68ch) with inline callouts — no float margins
   4. Full-width vertical/tabbed code notebooks with zero horizontal scrollbars
   5. Spacious, untruncated Table of Contents drawer + instant top chapter jumper
   ========================================================================== */

@import url("https://cdn.jsdelivr.net/gh/orioncactus/pretendard@v1.3.9/dist/web/variable/pretendardvariable-dynamic-subset.min.css");
@import url("https://fonts.googleapis.com/css2?family=Gowun+Batang:wght@400;700&family=Inter:wght@400;500;600;700&family=JetBrains+Mono:ital,wght@0,400;0,500;0,600;1,400&family=Nanum+Myeongjo:wght@400;700&family=Noto+Serif+KR:wght@400;600;700&family=Source+Serif+4:ital,opsz,wght@0,8..60,400;0,8..60,600;0,8..60,700;1,8..60,400&display=swap");

/* --------------------------------------------------------------------------
   1. Theme Tokens — Bright Apple Books Palette
   -------------------------------------------------------------------------- */
:root,
[data-theme="white"],
[data-theme="paper"],
[data-theme="newsprint"] {
  --bg-desk: #ffffff;
  --bg-sheet: #ffffff;
  --bg-header: rgba(250, 249, 246, 0.86);
  --bg-sidebar: #fbfaf7;
  --bg-subtle: #f7f6f2;
  --bg-elevated: #f0eee8;
  --bg-popover: #ffffff;

  --bg-code: #f6f8fa;
  --bg-code-header: #eef1f5;
  --border-code: #e2e6ec;

  --text-primary: #111111;
  --text-body: #111111;
  --text-ink: #111111;
  --text-secondary: #555459;
  --text-muted: #828087;
  --text-code: #24292f;

  --border-hairline: #e8e5dd;
  --border-subtle: #dcd8ce;
  --border-strong: #aba598;

  --accent-primary: #235688;
  --accent-primary-soft: rgba(35, 86, 136, 0.09);
  --accent-warm: #944a26;
  --accent-warm-soft: rgba(148, 74, 38, 0.09);
  --accent-highlight: #fef9e7;
  --accent-sage-soft: #f2f6f3;

  /* Light-mode accessible syntax tokens */
  --syn-kw: #9a3412;
  --syn-ty: #1d4ed8;
  --syn-fn: #6d28d9;
  --syn-st: #15803d;
  --syn-cm: #6e7781;

  --shadow-sheet: none;
  --shadow-popover: 0 12px 36px rgba(20, 20, 18, 0.14), 0 2px 8px rgba(20, 20, 18, 0.06);
  --spot-frame-bg: #f5f2ea;
}

[data-theme="sepia"],
[data-theme="cream"] {
  --bg-desk: #f3eadc;
  --bg-sheet: #fbf6ee;
  --bg-header: rgba(248, 245, 237, 0.88);
  --bg-sidebar: #f7f4ec;
  --bg-subtle: #f5f1e7;
  --bg-elevated: #ebe5d8;
  --bg-popover: #fcfaf4;

  --bg-code: #f4f1e7;
  --bg-code-header: #eae5d7;
  --border-code: #ded8c8;

  --text-primary: #1c140e;
  --text-body: #1c140e;
  --text-ink: #1c140e;
  --text-secondary: #5a554c;
  --text-muted: #847e72;
  --text-code: #292621;

  --border-hairline: #e4dfd2;
  --border-subtle: #d6cfc0;
  --border-strong: #9c9484;

  --accent-primary: #1f4f78;
  --accent-primary-soft: rgba(31, 79, 120, 0.1);
  --accent-warm: #8c4420;
  --accent-warm-soft: rgba(140, 68, 32, 0.1);
  --accent-highlight: #f9f2dc;
  --accent-sage-soft: #eef2ec;

  --syn-kw: #9a3412;
  --syn-ty: #1e40af;
  --syn-fn: #6b21a8;
  --syn-st: #166534;
  --syn-cm: #736d62;

  --shadow-sheet: none;
  --shadow-popover: 0 12px 36px rgba(35, 30, 20, 0.16);
  --spot-frame-bg: #f1ede2;
}

[data-theme="gray"] {
  --bg-desk: #ececf1;
  --bg-sheet: #f7f7fa;
  --bg-header: rgba(242, 242, 247, 0.9);
  --bg-sidebar: #f2f2f7;
  --bg-subtle: #eaeaef;
  --bg-elevated: #e4e4ea;
  --bg-popover: #ffffff;

  --bg-code: #f3f3f6;
  --bg-code-header: #e8e8ee;
  --border-code: #d9d9e2;

  --text-primary: #111111;
  --text-body: #111111;
  --text-ink: #111111;
  --text-secondary: #55555a;
  --text-muted: #8a8a90;
  --text-code: #1c1c1e;

  --border-hairline: #e2e2e8;
  --border-subtle: #d2d2da;
  --border-strong: #9a9aa4;

  --accent-primary: #235688;
  --accent-primary-soft: rgba(35, 86, 136, 0.09);
  --accent-warm: #944a26;
  --accent-warm-soft: rgba(148, 74, 38, 0.09);
  --accent-highlight: #f4f4f8;
  --accent-sage-soft: #eef0f4;

  --syn-kw: #9a3412;
  --syn-ty: #1d4ed8;
  --syn-fn: #6d28d9;
  --syn-st: #15803d;
  --syn-cm: #6e7781;

  --shadow-sheet: none;
  --shadow-popover: 0 12px 36px rgba(20, 20, 24, 0.14), 0 2px 8px rgba(20, 20, 24, 0.06);
  --spot-frame-bg: #eeeef2;
}

[data-theme="dark"],
[data-theme="night"] {
  --bg-desk: #111215;
  --bg-sheet: #191b20;
  --bg-header: rgba(22, 24, 29, 0.86);
  --bg-sidebar: #16181d;
  --bg-subtle: #20232a;
  --bg-elevated: #272b34;
  --bg-popover: #22252d;

  --bg-code: #13151a;
  --bg-code-header: #1c1f27;
  --border-code: #2c313d;

  --text-primary: #f4f1ea;
  --text-body: #f4f1ea;
  --text-ink: #f4f1ea;
  --text-secondary: #a7a39a;
  --text-muted: #78756d;
  --text-code: #e6e4dd;

  --border-hairline: #272a33;
  --border-subtle: #323642;
  --border-strong: #686d7a;

  --accent-primary: #7cb3eb;
  --accent-primary-soft: rgba(124, 179, 235, 0.14);
  --accent-warm: #e49b75;
  --accent-warm-soft: rgba(228, 155, 117, 0.14);
  --accent-highlight: #24221b;
  --accent-sage-soft: #1b2421;

  --syn-kw: #f09a75;
  --syn-ty: #82b8f5;
  --syn-fn: #c9a5f7;
  --syn-st: #94d299;
  --syn-cm: #7e8594;

  --shadow-sheet: none;
  --shadow-popover: 0 16px 42px rgba(0, 0, 0, 0.6);
  --spot-frame-bg: #22242a;
}

/* --------------------------------------------------------------------------
   2. Typography Scales & Korean/English Font Families
   -------------------------------------------------------------------------- */
:root {
  /* gistfish 세리프 명조 스택. 웹 로드는 OFL(Noto Serif KR, Nanum Myeongjo, Source Serif 4). KoPub Batang·New York·Charter는 기기에 있을 때만 */
  --font-sans: "Inter", "Pretendard Variable", "Pretendard", sans-serif;
  --font-serif: "AppleMyungjo", "KoPub Batang", "Nanum Myeongjo", "Batang", "Source Serif 4", "Noto Serif KR", Georgia, serif;
  --font-mono: "JetBrains Mono", ui-monospace, Menlo, Consolas, monospace;

  --font-body-active: var(--font-serif);
  --reading-size: 18px;
  --reading-lh: 1.85;
  --reading-track: 0;
}

[data-font-family="serif"] {
  --font-body-active: var(--font-serif);
  --reading-track: 0;
}

[data-font-family="sans"] {
  --font-body-active: var(--font-sans);
  --reading-track: -0.008em;
}

[data-font-size="14"] { --reading-size: 14px; }
[data-font-size="16"] { --reading-size: 16px; }
[data-font-size="17"] { --reading-size: 17px; }
[data-font-size="18"] { --reading-size: 18px; }
[data-font-size="20"] { --reading-size: 20px; }

[data-line-height="relaxed"] { --reading-lh: 1.76; }
[data-line-height="loose"] { --reading-lh: 1.85; }
[data-line-height="roomy"] { --reading-lh: 2.05; }

* {
  box-sizing: border-box;
}

html {
  font-size: 16px;
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}

strong,
b,
h1,
h2,
h3,
h4,
th {
  font-weight: 500;
}

a,
a:visited {
  color: var(--accent-primary);
  text-decoration: none;
}

a:hover,
a:active {
  text-decoration: none;
}

body {
  margin: 0;
  padding: 0;
  background-color: var(--bg-desk);
  color: var(--text-body);
  font-family: var(--font-sans);
  line-height: 1.5;
  letter-spacing: -0.01em;
  word-break: keep-all;
  overflow-wrap: break-word;
  -webkit-font-smoothing: auto;
  -moz-osx-font-smoothing: auto;
  transition: background-color 0.22s ease, color 0.22s ease;
}

/* Reading Progress Hairline */
.reading-progress {
  position: fixed;
  top: 0;
  left: 0;
  height: 2.5px;
  width: 0%;
  background: linear-gradient(90deg, var(--accent-primary), var(--accent-warm));
  z-index: 1200;
  transition: width 0.12s linear;
}

/* --------------------------------------------------------------------------
   3. Apple Books Translucent Top Toolbar & Aa / Chapter Popovers
   -------------------------------------------------------------------------- */
.site-header {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  z-index: 1000;
  height: 54px;
  transform: translateY(-100%);
  pointer-events: none;
  transition: transform 0.22s ease;
  background: var(--bg-header);
  backdrop-filter: saturate(180%) blur(18px);
  -webkit-backdrop-filter: saturate(180%) blur(18px);
  border-bottom: 1px solid var(--border-hairline);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0 1.15rem;
  font-family: var(--font-sans);
}

body.header-shown .site-header {
  transform: translateY(0);
  pointer-events: auto;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-width: 0;
}

.site-name {
  font-family: var(--font-sans);
  font-size: 0.82rem;
  font-weight: 600;
  letter-spacing: -0.015em;
  color: var(--text-primary);
  text-decoration: none;
  white-space: nowrap;
}

.site-name:hover {
  color: var(--accent-primary);
}

.sidebar-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.38rem;
  background: var(--bg-sheet);
  border: 1px solid var(--border-subtle);
  color: var(--text-primary);
  padding: 0.34rem 0.72rem;
  border-radius: 999px;
  cursor: pointer;
  font-family: var(--font-sans);
  font-size: 0.78rem;
  font-weight: 500;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
}

.sidebar-toggle:hover {
  background: var(--bg-elevated);
  border-color: var(--border-strong);
}

/* Center Quick Chapter & Section Jumper */
.header-center-jumper {
  position: absolute;
  left: 50%;
  top: 0;
  height: 100%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  justify-content: center;
  width: max-content;
  max-width: calc(100% - 13rem);
  z-index: 2;
}

.jumper-trigger {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  max-width: 100%;
  min-width: 0;
  padding: 0.32rem 0.8rem;
  border-radius: 999px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-secondary);
  font-family: var(--font-sans);
  font-size: 0.78rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.jumper-trigger:hover,
.jumper-trigger[aria-expanded="true"] {
  background: var(--bg-sheet);
  border-color: var(--border-subtle);
  color: var(--text-primary);
}

.jumper-trigger-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.jumper-caret {
  flex: none;
  font-size: 0.7rem;
  opacity: 0.7;
}

.jumper-popover {
  display: none;
  position: absolute;
  top: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  width: min(440px, 92vw);
  max-height: 74vh;
  overflow-y: auto;
  background: var(--bg-popover);
  border: 1px solid var(--border-subtle);
  border-radius: 14px;
  box-shadow: var(--shadow-popover);
  padding: 0.9rem;
  z-index: 1100;
  text-align: left;
}

.jumper-popover.open {
  display: block;
}

.jumper-section-label {
  font-size: 0.68rem;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin: 0.25rem 0 0.45rem 0.45rem;
}

.jumper-list {
  list-style: none;
  margin: 0 0 0.65rem;
  padding: 0;
}

.jumper-list a {
  display: block;
  padding: 0.46rem 0.65rem;
  border-radius: 8px;
  color: var(--text-body);
  text-decoration: none;
  font-size: 0.82rem;
  line-height: 1.45;
}

.jumper-list a:hover,
.jumper-list a.active {
  background: var(--accent-primary-soft);
  color: var(--accent-primary);
  font-weight: 500;
}

/* Right Header Controls & Apple Books 'Aa' Popover */
.header-controls {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.45rem;
  flex-shrink: 0;
}

.locale-switch {
  display: inline-flex;
  align-items: center;
  gap: 0.08rem;
  padding: 0.12rem;
  background: var(--bg-sheet);
  border: 1px solid var(--border-subtle);
  border-radius: 999px;
}

.locale-link {
  font-family: var(--font-sans);
  font-size: 0.72rem;
  font-weight: 500;
  letter-spacing: 0.04em;
  color: var(--text-secondary);
  text-decoration: none;
  padding: 0.22rem 0.48rem;
  border-radius: 999px;
}

.locale-link.active {
  color: var(--text-primary);
  background: var(--bg-elevated);
}

.theme-dots {
  display: inline-flex;
  align-items: center;
  gap: 0.32rem;
  padding: 0.24rem 0.42rem;
  background: var(--bg-sheet);
  border: 1px solid var(--border-subtle);
  border-radius: 999px;
}

.theme-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 1.5px solid rgba(0, 0, 0, 0.18);
  cursor: pointer;
  padding: 0;
  transition: transform 0.15s, box-shadow 0.15s;
}

.theme-dot[data-set-theme="paper"] { background: #ffffff; }
.theme-dot[data-set-theme="cream"] { background: #f5efe0; }
.theme-dot[data-set-theme="night"] { background: #1e2028; border-color: rgba(255, 255, 255, 0.3); }

.theme-dot.active {
  transform: scale(1.14);
  box-shadow: 0 0 0 2px var(--accent-primary);
}

.aa-toggle-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  background: var(--bg-sheet);
  border: 1px solid var(--border-subtle);
  color: var(--text-primary);
  padding: 0.32rem 0.72rem;
  border-radius: 999px;
  font-family: var(--font-serif);
  font-size: 0.84rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.aa-toggle-btn:hover,
.aa-toggle-btn[aria-expanded="true"] {
  background: var(--bg-elevated);
  border-color: var(--border-strong);
}

.aa-popover {
  display: none;
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: 340px;
  background: var(--bg-popover);
  border: 1px solid var(--border-subtle);
  border-radius: 16px;
  box-shadow: var(--shadow-popover);
  padding: 1rem 1rem 0.85rem;
  z-index: 1100;
  font-family: var(--font-sans);
}

.aa-popover.open {
  display: block;
}

.aa-row {
  margin-bottom: 0.85rem;
}

.aa-row:last-child {
  margin-bottom: 0;
}

.aa-label {
  display: block;
  font-size: 0.69rem;
  font-weight: 500;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin-bottom: 0.4rem;
}

.aa-segmented {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 3px;
  background: var(--bg-subtle);
  border: 1px solid var(--border-hairline);
  padding: 3px;
  border-radius: 10px;
}

.aa-seg-btn {
  background: transparent;
  border: none;
  border-radius: 7px;
  padding: 0.38rem 0.4rem;
  font-family: var(--font-sans);
  font-size: 0.75rem;
  color: var(--text-secondary);
  cursor: pointer;
  transition: background 0.15s, color 0.15s, box-shadow 0.15s;
  white-space: nowrap;
}

.aa-seg-btn:hover {
  color: var(--text-primary);
}

.aa-seg-btn.active {
  background: var(--bg-sheet);
  color: var(--text-primary);
  font-weight: 500;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.aa-row-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.4rem;
}

.aa-row-head .aa-label {
  margin-bottom: 0;
}

.aa-size-value {
  font-size: 0.75rem;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

.aa-size-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.aa-step-btn {
  width: 2.4rem;
  height: 2rem;
  border-radius: 8px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-sheet);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 0.78rem;
  font-weight: 500;
  cursor: pointer;
}

.aa-step-btn:disabled {
  opacity: 0.4;
  cursor: default;
}

.aa-size-steps {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 2px;
  background: var(--bg-subtle);
  border-radius: 8px;
  padding: 2px;
}

.aa-size-steps .aa-seg-btn {
  font-size: 0.68rem;
  padding: 0.32rem 0;
}

.aa-family .aa-seg-btn[data-set-family="serif"] {
  font-family: var(--font-serif);
}

.aa-themes {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.45rem;
}

.aa-swatch {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.35rem;
  border-radius: 10px;
  border: 1px solid var(--border-subtle);
  padding: 0.45rem 0.2rem 0.4rem;
  cursor: pointer;
  background: var(--bg-sheet);
}

.aa-swatch[data-set-theme="white"] { background: #ffffff; color: #18181b; }
.aa-swatch[data-set-theme="sepia"] { background: #f8f1e3; color: #2f241a; }
.aa-swatch[data-set-theme="gray"] { background: #f2f2f7; color: #1c1c1e; }
.aa-swatch[data-set-theme="dark"] { background: #18181a; color: #f4f4f5; border-color: #38383c; }

.aa-swatch-mark {
  width: 1.7rem;
  height: 1.7rem;
  border-radius: 999px;
  border: 1px solid currentColor;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-serif);
  font-size: 0.68rem;
  font-weight: 500;
}

.aa-swatch.active {
  box-shadow: 0 0 0 2px var(--text-primary);
}

.aa-swatch-name {
  font-size: 0.68rem;
  font-weight: 500;
  line-height: 1;
}

.aa-reset {
  display: block;
  margin-left: auto;
  margin-top: 0.15rem;
  background: none;
  border: none;
  color: var(--text-muted);
  font-family: var(--font-sans);
  font-size: 0.72rem;
  cursor: pointer;
  padding: 0.15rem 0;
}

.aa-reset:hover {
  color: var(--text-primary);
}

/* Legacy header control-group hidden when JS upgrades toolbar, kept clean as fallback */
.control-group {
  display: inline-flex;
  border: 1px solid var(--border-subtle);
  border-radius: 999px;
  overflow: hidden;
  background: var(--bg-sheet);
}

.ctrl-btn {
  background: transparent;
  border: none;
  border-right: 1px solid var(--border-hairline);
  color: var(--text-secondary);
  padding: 0.26rem 0.55rem;
  font-size: 0.74rem;
  cursor: pointer;
}

.ctrl-btn:last-child { border-right: none; }
.ctrl-btn.active {
  background: var(--accent-primary-soft);
  color: var(--accent-primary);
  font-weight: 500;
}

/* --------------------------------------------------------------------------
   4. Main Shell & Generous Untruncated Left Contents Sidebar
   -------------------------------------------------------------------------- */
.book-shell {
  display: block;
  min-height: calc(100vh - 54px);
}

/* Legacy right TOC sidebar retired in favor of unified left TOC */
.page-toc {
  display: none !important;
}

.book-sidebar {
  position: fixed;
  left: 0;
  top: 54px;
  width: min(336px, 88vw);
  height: calc(100vh - 54px);
  z-index: 960;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-hairline);
  box-shadow: none;
  padding: 1.15rem 1.05rem 3.5rem;
  overflow-y: auto;
  font-family: var(--font-sans);
  font-size: 0.82rem;
  transform: translateX(-100%);
  transition: transform 0.22s ease;
}

body.toc-open .book-sidebar {
  transform: translateX(0);
}

body.toc-open .sidebar-backdrop {
  display: block;
}

.sidebar-site-name {
  display: block;
  margin: 0 0 0.9rem;
  font-family: var(--font-sans);
  font-size: 0.95rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.35;
  color: var(--text-primary);
  text-decoration: none;
}

.sidebar-site-name:hover {
  color: var(--accent-primary);
}

.sidebar-toolbar {
  position: sticky;
  top: -1.15rem;
  background: var(--bg-sidebar);
  padding: 0.2rem 0 0.85rem;
  margin-bottom: 0.75rem;
  border-bottom: 1px solid var(--border-hairline);
  z-index: 5;
}

.toc-search-input {
  width: 100%;
  padding: 0.48rem 0.75rem;
  border-radius: 9px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-sheet);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 0.78rem;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.toc-search-input:focus {
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 3px var(--accent-primary-soft);
}

.nav-section {
  margin-bottom: 1.35rem;
}

.nav-part-title {
  font-size: 0.71rem;
  font-weight: 500;
  letter-spacing: 0.02em;
  color: var(--text-muted);
  margin: 0 0 0.45rem 0.35rem;
  line-height: 1.4;
  /* Zero truncation */
  white-space: normal;
  overflow: visible;
}

.nav-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.nav-item {
  margin-bottom: 0.22rem;
}

.nav-link {
  display: block;
  padding: 0.5rem 0.65rem;
  border-radius: 9px;
  color: var(--text-body);
  text-decoration: none;
  line-height: 1.46;
  transition: background 0.14s, color 0.14s;
  /* Explicitly allow clean multi-line wrapping with zero truncation */
  white-space: normal;
  overflow: visible;
  word-break: keep-all;
}

.nav-link:hover {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

.nav-link.active {
  background: var(--accent-primary-soft);
  color: var(--accent-primary);
  font-weight: 500;
}

.nav-link.upcoming {
  opacity: 0.55;
}

.nav-item-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
  margin-bottom: 0.14rem;
}

.nav-num {
  font-family: var(--font-mono);
  font-size: 0.67rem;
  font-weight: 500;
  color: var(--text-muted);
}

.nav-link.active .nav-num {
  color: var(--accent-primary);
}

.nav-item-label {
  display: block;
  font-size: 0.82rem;
  line-height: 1.45;
}

/* Expanded On-Page Sections directly inside Active Chapter TOC */
.nav-sub-list {
  list-style: none;
  margin: 0.28rem 0 0.55rem 0.85rem;
  padding: 0.15rem 0 0.15rem 0.65rem;
  border-left: 1.5px solid var(--border-subtle);
}

.nav-sub-list li {
  margin-bottom: 0.15rem;
}

.nav-sub-list a {
  display: block;
  padding: 0.28rem 0.45rem;
  border-radius: 6px;
  font-size: 0.76rem;
  line-height: 1.42;
  color: var(--text-secondary);
  text-decoration: none;
  white-space: normal;
  word-break: keep-all;
}

.nav-sub-list a:hover,
.nav-sub-list a.active {
  color: var(--accent-primary);
  background: var(--accent-primary-soft);
  font-weight: 500;
}

/* --------------------------------------------------------------------------
   5. Docs Paper Sheet — Single-Column Reading Measure
   -------------------------------------------------------------------------- */
.book-main {
  padding: calc(54px + 1.6rem) 1.75rem 5rem;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  min-width: 0;
}

.chapter-article {
  position: relative;
  width: 100%;
  max-width: 42rem;
  background: var(--bg-sheet);
  border: 1px solid var(--border-hairline);
  border-radius: 8px;
  box-shadow: var(--shadow-sheet);
  padding: 2.75rem 2.5rem 3.5rem;
  transition: background-color 0.22s ease;
}

@media (min-width: 1280px) {
  .chapter-article {
    max-width: 52rem;
  }
}

@media (min-width: 1600px) {
  .chapter-article {
    max-width: 64rem;
  }
}

/* Explanation at body size inside the box; term name is a small caption under it */
.term-figure {
  margin: 1.2rem 0 1.5rem;
}

.margin-note {
  display: block;
  margin: 0;
  padding: 0.95rem 1.15rem;
  background: var(--bg-subtle);
  border: 1px solid var(--border-hairline);
  border-radius: 8px;
  font-family: var(--font-body-active);
  font-size: var(--reading-size);
  font-weight: 400;
  line-height: var(--reading-lh);
  letter-spacing: var(--reading-track);
  color: var(--text-ink, var(--text-body));
  -webkit-font-smoothing: auto;
  -moz-osx-font-smoothing: auto;
}

.margin-note-caption {
  display: block;
  margin: 0.4rem 0 0;
  padding: 0 0.15rem;
  background: none;
  border: none;
  font-family: var(--font-sans);
  font-size: 0.75rem;
  font-weight: 500;
  line-height: 1.4;
  letter-spacing: 0;
  color: var(--text-muted);
}

.margin-note strong {
  color: var(--text-primary);
}

/* --------------------------------------------------------------------------
   6. Editorial Typography & Digestible Reading Rhythm
   -------------------------------------------------------------------------- */
/* Reader font applies to prose only — chrome and headings stay sans */
.chapter-article p,
.chapter-article li,
.chapter-article .chapter-subtitle,
.chapter-article .math-note,
.chapter-article .epigraph,
.chapter-article .callout p,
.chapter-article .takeaways-box li,
.chapter-article .takeaways-box p {
  font-family: var(--font-body-active);
  font-size: var(--reading-size);
  font-weight: 400;
  line-height: var(--reading-lh);
  letter-spacing: var(--reading-track);
  color: var(--text-ink, var(--text-body));
  -webkit-font-smoothing: auto;
  -moz-osx-font-smoothing: auto;
}

.chapter-title {
  font-family: var(--font-sans);
  font-size: 1.86rem;
  font-weight: 500;
  line-height: 1.34;
  letter-spacing: -0.022em;
  margin: 0 0 0.8rem 0;
  color: var(--text-primary);
  word-break: keep-all;
}

.chapter-subtitle {
  font-size: 1.03rem;
  line-height: 1.72;
  color: var(--text-secondary);
  margin: 0 0 1.65rem 0;
  font-weight: 400;
}

.chapter-meta-bar {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  padding: 0.8rem 1.05rem;
  background: var(--bg-subtle);
  border: 1px solid var(--border-hairline);
  border-radius: 12px;
  margin-bottom: 2.3rem;
  font-family: var(--font-sans);
  font-size: 0.79rem;
  color: var(--text-secondary);
}

.companion-badge-link {
  font-family: var(--font-sans);
  font-size: 0.76rem;
  color: var(--accent-primary);
  text-decoration: none;
  font-weight: 500;
  white-space: nowrap;
}


.chapter-article h2 {
  font-family: var(--font-sans);
  font-size: 1.18rem;
  font-weight: 500;
  line-height: 1.42;
  margin: 3.1rem 0 1.05rem;
  padding-top: 0.4rem;
  padding-bottom: 0.55rem;
  border-bottom: 1px solid var(--border-hairline);
  letter-spacing: -0.016em;
  color: var(--text-primary);
  scroll-margin-top: 76px;
}

.chapter-article h3 {
  font-family: var(--font-sans);
  font-size: var(--reading-size);
  line-height: 1.5;
  margin: 2.1rem 0 0.7rem;
  color: var(--text-primary);
  font-weight: 500;
}

.chapter-article > p {
  margin: 0 0 1.15rem;
  color: var(--text-ink, var(--text-body));
  text-align: justify;
}

.chapter-article > p + p {
  text-indent: 1em;
}

.chapter-article ul,
.chapter-article ol {
  margin: 0 0 1.45rem;
  padding-left: 1.35rem;
}

.chapter-article li {
  margin-bottom: 0.58rem;
}

/* Apple Books–like prose: almost no visual chrome on emphasis */
.chapter-article strong {
  font-weight: 500;
  color: inherit;
}

.chapter-article code {
  font-family: var(--font-mono);
  font-size: 0.92em;
  font-weight: 400;
  background: transparent;
  color: inherit;
  padding: 0;
  border: none;
  border-radius: 0;
  margin: 0;
}

/* Journal Pull-Quote / Epigraph */
.epigraph {
  margin: 1.75rem 0 2.35rem;
  padding: 1.25rem 1.45rem;
  border-left: 3px solid var(--accent-primary);
  background: var(--accent-highlight);
  border-radius: 0 12px 12px 0;
  font-family: var(--font-body-active);
  font-size: 0.98rem;
  line-height: 1.82;
  color: var(--text-primary);
}

.epigraph cite {
  display: block;
  margin-top: 0.6rem;
  font-family: var(--font-sans);
  font-style: normal;
  font-size: 0.79rem;
  color: var(--text-secondary);
  font-weight: 500;
}

/* --------------------------------------------------------------------------
   7. Full-Width, Zero-Horizontal-Scroll Code Notebooks (TypeScript ↔ Daml)
   -------------------------------------------------------------------------- */
.code-compare {
  margin: 0.4rem 0 2.1rem;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.code-compare-connector {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.2rem 0;
  font-family: var(--font-sans);
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--accent-primary);
}

.code-compare-connector::before,
.code-compare-connector::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border-hairline);
}

.code-panel,
.single-code {
  background: var(--bg-code);
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid var(--border-code);
}

.single-code {
  margin: 1.75rem 0;
}

.code-panel-header {
  background: var(--bg-code-header);
  padding: 0.55rem 1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.6rem;
  font-family: var(--font-sans);
  font-size: 0.76rem;
  font-weight: 500;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border-code);
}

.code-header-right {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
}

.lang-tag {
  font-family: var(--font-mono);
  padding: 0.12rem 0.48rem;
  border-radius: 999px;
  border: 1px solid var(--border-hairline);
  background: var(--bg-sheet);
  color: var(--text-secondary);
  font-weight: 500;
  font-size: 0.68rem;
}

.copy-code-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.7rem;
  height: 1.7rem;
  padding: 0;
  background: transparent;
  border: none;
  border-radius: 4px;
  color: var(--text-muted);
  cursor: pointer;
}

.copy-code-btn:hover {
  color: var(--text-primary);
  background: var(--bg-subtle);
}

/* Zero horizontal scrolling: clean pre-wrap with comfortable reading size */
.code-panel pre,
.single-code pre {
  margin: 0;
  padding: 1.1rem 1.2rem;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
  overflow-x: visible;
  font-family: var(--font-mono);
  font-size: 0.83rem;
  line-height: 1.74;
  tab-size: 2;
  color: var(--text-code);
}

.code-panel pre code,
.single-code pre code {
  background: none;
  border: none;
  padding: 0;
  margin: 0;
  color: inherit;
  font-size: inherit;
}

.kw { color: var(--syn-kw); font-weight: 500; }
.ty { color: var(--syn-ty); font-weight: 500; }
.cm { color: var(--syn-cm); font-style: italic; }
.fn { color: var(--syn-fn); font-weight: 500; }
.st { color: var(--syn-st); }

/* --------------------------------------------------------------------------
   8. Spot Illustrations, Math Cards, Callouts & Tables
   -------------------------------------------------------------------------- */
.spot-figure {
  margin: 2.3rem 0;
  background: transparent;
  border: none;
  border-radius: 0;
  overflow: visible;
}

.spot-image-frame {
  padding: 0;
  background: var(--spot-frame-bg);
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px solid var(--border-hairline);
  border-radius: 6px;
}

.spot-image-frame img {
  display: block;
  width: 100%;
  max-height: 420px;
  object-fit: contain;
  border-radius: 6px;
}

.spot-caption {
  margin: 0.45rem 0 0;
  padding: 0 0.15rem;
  font-family: var(--font-sans);
  font-size: 0.78rem;
  color: var(--text-muted);
  line-height: 1.58;
  background: transparent;
}

.spot-caption strong {
  font-family: var(--font-sans);
  font-size: 0.74rem;
  font-weight: 500;
  color: var(--text-muted);
  margin-right: 0.35rem;
}

.prompt-drawer {
  margin-top: 0.35rem;
  border: none;
  background: transparent;
  font-family: var(--font-sans);
  font-size: 0.76rem;
}

.prompt-drawer summary {
  padding: 0.35rem 0.15rem;
  cursor: pointer;
  font-size: 0.72rem;
  color: var(--text-muted);
  user-select: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  list-style: none;
}

.prompt-drawer summary::-webkit-details-marker {
  display: none;
}

.prompt-drawer summary:hover {
  color: var(--text-primary);
}

.prompt-drawer-body {
  padding: 0.55rem 0 0.85rem;
  border-top: 1px solid var(--border-hairline);
}

.prompt-meta {
  margin-bottom: 0.55rem;
  color: var(--text-secondary);
  font-size: 0.78rem;
  line-height: 1.6;
}

.prompt-box {
  background: var(--bg-code);
  border: 1px solid var(--border-code);
  color: var(--text-code);
  padding: 0.85rem 1rem;
  border-radius: 8px;
  font-family: var(--font-mono);
  font-size: 0.74rem;
  line-height: 1.6;
  white-space: pre-wrap;
}

.copy-prompt-btn {
  margin-top: 0.55rem;
  background: var(--bg-sheet);
  border: 1px solid var(--border-subtle);
  color: var(--text-primary);
  padding: 0.3rem 0.75rem;
  border-radius: 7px;
  font-family: var(--font-sans);
  font-size: 0.73rem;
  font-weight: 500;
  cursor: pointer;
}

.copy-prompt-btn:hover {
  background: var(--accent-primary);
  border-color: var(--accent-primary);
  color: #ffffff;
}

/* Docs math blocks — formula first, caption as footnote below */
.math-figure {
  margin: 1.9rem 0;
}

.math-card {
  margin: 0;
  padding: 1.25rem 1.35rem;
  background: var(--bg-subtle);
  border: 1px solid var(--border-hairline);
  border-radius: 6px;
}

.math-formula {
  font-family: "STIX Two Math", "Cambria Math", "Source Serif 4", Georgia, serif;
  font-size: 1.2rem;
  line-height: 1.45;
  text-align: center;
  padding: 0.7rem 0.35rem 0.45rem;
  color: var(--text-ink, var(--text-primary));
  overflow-x: auto;
}

.math-formula .katex,
.math-formula .katex-display {
  color: inherit;
}

.math-formula .katex {
  font-size: 1.02em;
}

.math-formula .katex-display {
  margin: 0;
}

.math-inline {
  white-space: nowrap;
}

.math-inline .katex {
  font-size: 1.05em;
  line-height: 1.2;
}

.math-formula + .math-formula {
  padding-top: 0.15rem;
}

.math-bridge {
  margin: 0;
  text-align: center;
  font-family: var(--font-sans);
  font-size: 0.82rem;
  line-height: 1.45;
  color: var(--text-muted);
}

.math-note {
  font-family: var(--font-sans);
  font-size: 0.82rem;
  color: var(--text-secondary);
  margin-top: 0.65rem;
  line-height: 1.68;
}

.math-card-caption {
  margin: 0.45rem 0 0;
  padding: 0 0.15rem;
  font-family: var(--font-sans);
  font-size: 0.74rem;
  font-weight: 500;
  line-height: 1.5;
  color: var(--text-muted);
}

/* Soft Docs callouts — no left accent bars */
.callout {
  margin: 1.95rem 0;
  padding: 1.1rem 1.25rem;
  border-radius: 6px;
  border: 1px solid var(--border-hairline);
  background: var(--bg-subtle);
}

.callout-title {
  font-family: var(--font-sans);
  font-size: 0.76rem;
  font-weight: 500;
  letter-spacing: 0.01em;
  margin-bottom: 0.45rem;
  color: var(--text-muted);
}

.callout.architect,
.callout.butler {
  background: var(--bg-subtle);
}

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

/* FAQ — question, then a hairline row per answer */
.faq {
  margin: 1.8rem 0 2.1rem;
}

.chapter-article .faq-q {
  margin: 0 0 0.35rem;
  font-family: var(--font-sans);
  font-size: var(--reading-size);
  font-weight: 500;
  line-height: 1.5;
  color: var(--text-primary);
}

.faq-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.3rem;
  padding: 0.85rem 0;
  border-top: 1px solid var(--border-hairline);
}

.faq-label {
  font-family: var(--font-sans);
  font-size: 0.84rem;
  font-weight: 500;
  color: var(--text-secondary);
}

.chapter-article .faq-row p {
  margin: 0;
  text-indent: 0;
}

/* Next-chapter aside — a note, not a card */
.chapter-note {
  margin: 1.75rem 0 0;
}

.chapter-article .chapter-note p {
  margin: 0;
  font-family: var(--font-sans);
  font-size: calc(var(--reading-size) * 0.92);
  font-weight: 400;
  line-height: 1.7;
  letter-spacing: 0;
  text-align: left;
  text-indent: 0;
  color: var(--text-secondary);
}

/* Clean Journal Tables */
.table-wrap {
  margin: 1.8rem 0;
  overflow-x: auto;
  border: 1px solid var(--border-hairline);
  border-radius: 12px;
  background: var(--bg-sheet);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--font-sans);
  font-size: 0.83rem;
  line-height: 1.62;
}

th {
  background: var(--bg-subtle);
  text-align: left;
  padding: 0.75rem 0.95rem;
  font-weight: 500;
  color: var(--text-primary);
  border-bottom: 1px solid var(--border-hairline);
}

td {
  padding: 0.75rem 0.95rem;
  border-bottom: 1px solid var(--border-hairline);
  vertical-align: top;
  color: var(--text-body);
}

tr:last-child td {
  border-bottom: none;
}

/* Statute — cited prose between two hairlines. Source stays in the sentence. */
.statute {
  margin: 1.9rem 0;
}

.chapter-article .statute p {
  margin: 0;
  padding: 0.95rem 0;
  border-top: 1px solid var(--border-hairline);
  border-bottom: 1px solid var(--border-hairline);
  text-indent: 0;
}

/* Recap — same heading scale as a section. Names open the sentence. */
.recap {
  margin: 0 0 1.4rem;
}

.chapter-article .recap-list {
  margin-bottom: 0;
}

/* Takeaways & Page Turners */
.takeaways-box {
  margin: 2.9rem 0 2.2rem;
  padding: 1.45rem 1.6rem;
  background: var(--bg-subtle);
  border: 1px solid var(--border-hairline);
  border-radius: 14px;
}

.takeaways-box h3 {
  margin-top: 0;
  font-size: 1.02rem;
}

.chapter-pager {
  margin-top: 3rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border-hairline);
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.5rem;
  font-family: var(--font-sans);
}

.pager-link {
  display: block;
  padding: 0.35rem 0;
  text-decoration: none;
  color: var(--text-primary);
  transition: color 0.15s;
}

.pager-link:hover {
  color: var(--accent-primary);
}

.pager-link.next {
  text-align: right;
}

.pager-label {
  display: block;
  font-family: var(--font-sans);
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-bottom: 0.2rem;
  font-weight: 500;
}

.pager-title {
  font-weight: 500;
  font-size: 0.92rem;
  line-height: 1.42;
  color: inherit;
}

/* Portal title page */
.portal-masthead {
  margin: 0 0 2.4rem;
  padding-bottom: 1.35rem;
  border-bottom: 1px solid var(--border-hairline);
}

.portal-cover {
  display: block;
  width: 9.25rem;
  height: auto;
  margin: 0 0 1.15rem;
  border-radius: 2px;
}

.portal-masthead .chapter-title {
  margin-bottom: 0.4rem;
}

.chapter-article .portal-dek {
  font-family: var(--font-sans);
  font-size: 1rem;
  font-weight: 400;
  line-height: 1.5;
  letter-spacing: -0.011em;
  color: var(--text-secondary);
  margin: 0 0 1rem;
  max-width: 24rem;
}

.portal-start {
  display: inline-block;
  font-family: var(--font-sans);
  font-size: 0.92rem;
  font-weight: 500;
  color: var(--accent-primary);
  text-decoration: none;
}

.portal-start:hover {
  text-decoration: underline;
}

.portal-masthead .prompt-drawer {
  max-width: 36rem;
  margin-top: 1.15rem;
}

.part-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0;
  margin: 1.25rem 0 2.5rem;
  border-top: 1px solid var(--border-hairline);
}

.part-card {
  padding: 1.15rem 0;
  background: transparent;
  border: none;
  border-bottom: 1px solid var(--border-hairline);
  border-radius: 0;
}

.part-card h3 {
  margin: 0 0 0.4rem 0;
  font-size: 1rem;
}

.part-card p {
  margin: 0 0 0.55rem 0;
  font-size: 0.86rem;
  color: var(--text-secondary);
}

/* Mobile & Tablet Drawer Backdrop */
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 54px 0 0 0;
  background: rgba(15, 16, 20, 0.28);
  backdrop-filter: blur(2px);
  z-index: 940;
}

/* --------------------------------------------------------------------------
   9. Responsive Breakpoints
   -------------------------------------------------------------------------- */
@media (max-width: 1080px) {
  .book-main {
    padding: calc(54px + 1rem) 1rem 4rem;
  }

  .chapter-article {
    padding: 2rem 1.5rem 3rem !important;
    border-radius: 6px;
  }
}

@media (max-width: 720px) {
  .site-name {
    display: none;
  }

  .book-main {
    padding: calc(54px + 0.75rem) 0 2.5rem;
  }

  .header-center-jumper {
    max-width: calc(100% - 10.5rem);
  }

  .chapter-title {
    font-size: 1.68rem;
  }

  .chapter-article {
    border: none;
    border-radius: 0;
    box-shadow: none;
    padding: 1.05rem 1rem 2.2rem !important;
  }

  .chapter-pager {
    grid-template-columns: 1fr;
  }

  .pager-link.next {
    text-align: left;
  }
}

/* Design system reference — tabs stay in the document; hash opens a panel */
.ds-tabs {
  position: sticky;
  top: 54px;
  z-index: 30;
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin: 0 0 1.5rem;
  padding: 0.55rem 0;
  background: var(--bg-sheet);
  border-bottom: 1px solid var(--border-hairline);
}

.ds-tab {
  font-family: var(--font-sans);
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--text-secondary);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 0.28rem 0.72rem;
  cursor: pointer;
}

.ds-tab[aria-selected="true"] {
  color: var(--text-primary);
  background: var(--bg-subtle);
  border-color: var(--border-hairline);
}

[data-ds] h2,
[data-ds] h3 {
  scroll-margin-top: 112px;
}

.ds-swatch {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.42rem 0;
  border-bottom: 1px solid var(--border-hairline);
  font-family: var(--font-sans);
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.ds-chip {
  width: 2.4rem;
  height: 1.35rem;
  border-radius: 4px;
  border: 1px solid var(--border-hairline);
  flex: none;
}

.ds-code {
  margin: 0.8rem 0 1.2rem;
  padding: 0.85rem 1rem;
  overflow-x: auto;
  background: var(--bg-code);
  border: 1px solid var(--border-code);
  border-radius: 6px;
  font-family: var(--font-mono);
  font-size: 0.75rem;
  line-height: 1.55;
  color: var(--text-code);
}
