/* ==========================================================================
   Active Portfolio Management — reference site
   Style: "claude" (warm ivory, terracotta, literary serif)
   Design tokens as CSS custom properties; all components read only tokens.
   ========================================================================== */

:root {
  --bg: #f0eee6;         --surface: #faf9f5;    --surface-2: #e8e6dc;
  --ink: #141413;        --muted: #6e6d66;
  --accent: #d97757;     --on-accent: #ffffff;
  --accent-soft: rgba(217, 119, 87, 0.12);
  --border: rgba(20, 20, 19, 0.10);
  --border-strong: rgba(20, 20, 19, 0.18);
  --radius: 12px;        --radius-lg: 18px;
  --shadow: 0 1px 3px rgba(20, 20, 19, 0.07);
  --shadow-lg: 0 6px 24px rgba(20, 20, 19, 0.10);
  --font-display: ui-serif, Georgia, 'Times New Roman', serif;
  --font-body: system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-mono: ui-monospace, 'SF Mono', Menlo, monospace;
  --ok: #7d9a6f;  --warn: #c58c33;  --err: #b0552f;
  --paper: #ffffff;      /* light chip for formula/figure images (dark-mode safe) */
  --sidebar-w: 320px;
}

:root[data-theme="dark"], :root.dark {
  --bg: #1f1e1b; --surface: #262521; --surface-2: #302e29;
  --ink: #f0eee6; --muted: #a3a094;
  --border: rgba(240, 238, 230, 0.12);
  --border-strong: rgba(240, 238, 230, 0.22);
  --accent: #e08a6b; --accent-soft: rgba(224, 138, 107, 0.15);
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 6px 24px rgba(0, 0, 0, 0.4);
  --paper: #f7f5ef;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1f1e1b; --surface: #262521; --surface-2: #302e29;
    --ink: #f0eee6; --muted: #a3a094;
    --border: rgba(240, 238, 230, 0.12);
    --border-strong: rgba(240, 238, 230, 0.22);
    --accent: #e08a6b; --accent-soft: rgba(224, 138, 107, 0.15);
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 6px 24px rgba(0, 0, 0, 0.4);
    --paper: #f7f5ef;
  }
}

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

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

a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
a:hover { text-decoration-thickness: 2px; }
code, kbd { font-family: var(--font-mono); font-size: 0.9em; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

/* ============================== Layout =================================== */
.app { display: grid; grid-template-columns: var(--sidebar-w) 1fr; min-height: 100vh; }

.sidebar {
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
  background: var(--surface); border-right: 1px solid var(--border);
  padding: 22px 18px 40px; display: flex; flex-direction: column; gap: 16px;
}
.brand { display: block; text-decoration: none; color: var(--ink); }
.brand h1 {
  font-family: var(--font-display); font-weight: 600; font-size: 1.32rem;
  margin: 0 0 3px; line-height: 1.2; letter-spacing: -0.01em;
}
.brand .byline { font-size: 0.78rem; color: var(--muted); margin: 0; }

.search-box { position: relative; }
.search-box input {
  width: 100%; padding: 9px 12px 9px 34px; border-radius: var(--radius);
  border: 1px solid var(--border-strong); background: var(--bg); color: var(--ink);
  font-family: var(--font-body); font-size: 0.9rem;
}
.search-box .s-icon { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 0.9rem; }
.search-box kbd {
  position: absolute; right: 9px; top: 50%; transform: translateY(-50%);
  font-size: 0.7rem; color: var(--muted); border: 1px solid var(--border);
  border-radius: 5px; padding: 1px 5px; background: var(--surface-2);
}
.search-results {
  list-style: none; margin: 6px 0 0; padding: 0; max-height: 320px; overflow-y: auto;
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg);
  display: none;
}
.search-results.show { display: block; }
.search-results li a {
  display: block; padding: 8px 12px; color: var(--ink); text-decoration: none;
  border-bottom: 1px solid var(--border); font-size: 0.85rem;
}
.search-results li:last-child a { border-bottom: none; }
.search-results li a:hover, .search-results li a.active { background: var(--accent-soft); }
.search-results .sr-ch { color: var(--muted); font-size: 0.72rem; }
.search-results .sr-empty { padding: 12px; color: var(--muted); font-size: 0.85rem; }

nav.toc { display: flex; flex-direction: column; gap: 2px; }
.toc .part { margin-top: 12px; }
.toc .part-hd {
  display: flex; align-items: center; gap: 6px; width: 100%; text-align: left;
  background: none; border: none; cursor: pointer; color: var(--muted);
  font-family: var(--font-body); font-weight: 600; font-size: 0.72rem;
  letter-spacing: 0.06em; text-transform: uppercase; padding: 6px 6px; border-radius: 8px;
}
.toc .part-hd:hover { color: var(--ink); }
.toc .part-hd .chev { transition: transform 0.18s ease; font-size: 0.65rem; }
.toc .part.collapsed .chev { transform: rotate(-90deg); }
.toc .part.collapsed .chapters { display: none; }
.toc .chapters { display: flex; flex-direction: column; gap: 1px; }
.toc a.ch {
  display: flex; align-items: baseline; gap: 8px; text-decoration: none;
  color: var(--ink); padding: 7px 10px; border-radius: 8px; font-size: 0.9rem;
  line-height: 1.35; border-left: 2px solid transparent;
}
.toc a.ch:hover { background: var(--surface-2); }
.toc a.ch.active { background: var(--accent-soft); border-left-color: var(--accent); font-weight: 600; }
.toc a.ch .ch-num { color: var(--muted); font-size: 0.78rem; font-variant-numeric: tabular-nums; min-width: 1.5em; }
.toc a.ch.visited .ch-num::after { content: " ✓"; color: var(--ok); }
.toc .extra-link {
  display: flex; align-items: center; gap: 8px; text-decoration: none; color: var(--ink);
  padding: 8px 10px; border-radius: 8px; font-size: 0.9rem; margin-top: 4px;
}
.toc .extra-link:hover { background: var(--surface-2); }
.toc .extra-link.active { background: var(--accent-soft); border-left: 2px solid var(--accent); font-weight: 600; }

.side-foot { margin-top: auto; padding-top: 16px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.theme-toggle, .side-foot a {
  background: none; border: 1px solid var(--border-strong); border-radius: 999px;
  color: var(--muted); cursor: pointer; font-size: 0.78rem; padding: 5px 12px;
  font-family: var(--font-body); text-decoration: none; display: inline-flex; align-items: center; gap: 5px;
}
.theme-toggle:hover, .side-foot a:hover { color: var(--ink); border-color: var(--accent); }

/* ============================== Content ================================= */
main.content { padding: 0; min-width: 0; }
.reader { max-width: 760px; margin: 0 auto; padding: 40px 40px 120px; }
.page-progress { position: fixed; top: 0; left: var(--sidebar-w); right: 0; height: 3px; background: transparent; z-index: 30; }
.page-progress > i { display: block; height: 100%; width: 0; background: var(--accent); transition: width 0.1s linear; }

.crumbs { font-size: 0.8rem; color: var(--muted); margin-bottom: 14px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.crumbs .part-tag { text-transform: uppercase; letter-spacing: 0.06em; font-weight: 600; }

h1.ch-title { font-family: var(--font-display); font-weight: 600; font-size: 2.2rem; line-height: 1.15; letter-spacing: -0.015em; margin: 0 0 6px; }
.ch-meta { color: var(--muted); font-size: 0.85rem; margin-bottom: 24px; display: flex; gap: 14px; flex-wrap: wrap; }
.ch-meta .pill { background: var(--surface-2); border-radius: 999px; padding: 3px 11px; font-size: 0.76rem; }

.hero-summary {
  font-family: var(--font-display); font-size: 1.24rem; line-height: 1.5;
  color: var(--ink); border-left: 3px solid var(--accent); padding: 4px 0 4px 20px;
  margin: 0 0 28px; font-style: italic;
}

.keyideas { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 20px 24px; margin: 0 0 34px; }
.keyideas h2 { font-family: var(--font-body); font-size: 0.76rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin: 0 0 12px; }
.keyideas ul { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 9px; }
.keyideas li { line-height: 1.5; }

section.ch-section { margin: 0 0 38px; scroll-margin-top: 20px; }
section.ch-section > h2 {
  font-family: var(--font-display); font-weight: 600; font-size: 1.5rem; letter-spacing: -0.01em;
  margin: 34px 0 16px; padding-bottom: 6px; border-bottom: 1px solid var(--border);
}

/* Typed blocks */
.blk { margin: 0 0 18px; }
.blk-p { line-height: 1.7; }
.blk-p strong { font-weight: 650; }

.def-card {
  background: var(--surface); border: 1px solid var(--border); border-left: 3px solid var(--accent);
  border-radius: var(--radius); padding: 14px 18px;
}
.def-card .term { font-family: var(--font-display); font-weight: 600; font-size: 1.05rem; margin-bottom: 4px; }
.def-card .def-body { line-height: 1.6; }
.def-card .def-plain { color: var(--muted); font-size: 0.9rem; margin-top: 6px; font-style: italic; }

.formula-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; overflow-x: auto;
}
.formula-card .fx { text-align: center; margin: 2px 0 6px; overflow-x: auto; }
.formula-card .fx-fallback { font-family: var(--font-mono); font-size: 0.92rem; background: var(--surface-2); padding: 10px 14px; border-radius: 8px; display: block; white-space: pre-wrap; }
.formula-card .fx-plain { color: var(--ink); font-size: 0.92rem; text-align: center; }
.formula-card .fx-note { color: var(--muted); font-size: 0.82rem; margin-top: 8px; border-top: 1px dashed var(--border); padding-top: 8px; }
/* image on a light paper chip so scans stay readable in dark mode */
.paper-chip { background: var(--paper); border: 1px solid var(--border-strong); border-radius: 10px; padding: 12px; display: inline-block; max-width: 100%; }
.paper-chip img { max-width: 100%; height: auto; display: block; }

figure.fig-card { margin: 0 0 18px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
figure.fig-card figcaption { color: var(--muted); font-size: 0.83rem; margin-top: 10px; text-align: center; }
figure.fig-card .fig-ref { font-variant-numeric: tabular-nums; }

.callout { border-radius: var(--radius); padding: 14px 18px 14px 46px; position: relative; border: 1px solid var(--border); }
.callout .ic { position: absolute; left: 15px; top: 13px; font-size: 1.1rem; }
.callout.intuition { background: var(--accent-soft); border-color: color-mix(in srgb, var(--accent) 30%, transparent); }
.callout.example { background: color-mix(in srgb, var(--ok) 12%, transparent); border-color: color-mix(in srgb, var(--ok) 30%, transparent); }
.callout.warning { background: color-mix(in srgb, var(--warn) 14%, transparent); border-color: color-mix(in srgb, var(--warn) 32%, transparent); }
.callout .co-label { font-weight: 600; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); display: block; margin-bottom: 2px; }

.tbl-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); }
table.data { border-collapse: collapse; width: 100%; font-size: 0.9rem; }
table.data th, table.data td { text-align: left; padding: 9px 13px; border-bottom: 1px solid var(--border); }
table.data thead th { background: var(--surface-2); font-weight: 600; font-size: 0.82rem; }
table.data tbody tr:last-child td { border-bottom: none; }
table.data td { font-variant-numeric: tabular-nums; }

blockquote.book-quote {
  margin: 0 0 18px; padding: 4px 0 4px 20px; border-left: 3px solid var(--border-strong);
  font-family: var(--font-display); font-style: italic; font-size: 1.08rem; color: var(--ink);
}
blockquote.book-quote .q-ref { display: block; font-family: var(--font-body); font-style: normal; font-size: 0.78rem; color: var(--muted); margin-top: 6px; }

/* Chapter nav (prev/next) */
.ch-nav { display: flex; justify-content: space-between; gap: 14px; margin-top: 50px; padding-top: 24px; border-top: 1px solid var(--border); }
.ch-nav a { flex: 1; text-decoration: none; color: var(--ink); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px; background: var(--surface); }
.ch-nav a:hover { border-color: var(--accent); }
.ch-nav a.next { text-align: right; }
.ch-nav .nav-dir { font-size: 0.72rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.ch-nav .nav-ttl { font-family: var(--font-display); font-size: 0.98rem; }
.ch-nav a.disabled { visibility: hidden; }

/* refs */
.refs-box { margin-top: 34px; font-size: 0.85rem; color: var(--muted); }
.refs-box h3 { font-size: 0.76rem; text-transform: uppercase; letter-spacing: 0.08em; margin: 0 0 8px; }

/* ============================ Landing ================================== */
.landing { max-width: 900px; margin: 0 auto; padding: 56px 40px 120px; }
.landing .hero-title { font-family: var(--font-display); font-size: 2.9rem; font-weight: 600; letter-spacing: -0.02em; line-height: 1.08; margin: 0 0 10px; }
.landing .hero-sub { font-size: 1.15rem; color: var(--muted); max-width: 60ch; margin: 0 0 14px; }
.landing .hero-authors { font-size: 0.95rem; color: var(--muted); margin: 0 0 34px; }
.landing .why { font-family: var(--font-display); font-size: 1.2rem; line-height: 1.6; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 24px 28px; margin: 0 0 20px; }
.progress-row { display: flex; align-items: center; gap: 16px; margin: 0 0 40px; flex-wrap: wrap; }
.progress-bar { flex: 1; min-width: 180px; height: 10px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.progress-bar > i { display: block; height: 100%; background: var(--accent); width: 0; transition: width 0.4s ease; }
.progress-label { font-size: 0.86rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.btn { display: inline-flex; align-items: center; gap: 7px; background: var(--accent); color: var(--on-accent); border: none; border-radius: 999px; padding: 9px 18px; font-size: 0.9rem; font-family: var(--font-body); cursor: pointer; text-decoration: none; }
.btn:hover { filter: brightness(1.05); }
.btn.outline { background: none; color: var(--ink); border: 1px solid var(--border-strong); }
.btn.outline:hover { border-color: var(--accent); color: var(--accent); }

.starts { margin: 0 0 44px; }
.starts h2, .part-grid h2 { font-family: var(--font-body); font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin: 0 0 14px; }
.start-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 14px; }
.start-card { text-decoration: none; color: var(--ink); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; transition: border-color 0.15s ease, transform 0.15s ease; }
.start-card:hover { border-color: var(--accent); transform: translateY(-2px); }
.start-card .sc-num { font-size: 0.74rem; color: var(--accent); font-weight: 600; }
.start-card .sc-ttl { font-family: var(--font-display); font-size: 1.05rem; margin: 4px 0 6px; line-height: 1.25; }
.start-card .sc-why { font-size: 0.85rem; color: var(--muted); line-height: 1.45; }

.part-grid { margin-bottom: 40px; }
.part-block { margin-bottom: 26px; }
.part-block .part-name { font-family: var(--font-display); font-size: 1.15rem; font-weight: 600; margin: 0 0 12px; padding-bottom: 6px; border-bottom: 1px solid var(--border); }
.chip-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; }
.ch-chip { text-decoration: none; color: var(--ink); display: flex; gap: 10px; align-items: baseline; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.ch-chip:hover { border-color: var(--accent); }
.ch-chip .cc-num { color: var(--muted); font-size: 0.8rem; font-variant-numeric: tabular-nums; }
.ch-chip .cc-ttl { font-size: 0.9rem; line-height: 1.3; }
.ch-chip.visited { border-left: 3px solid var(--ok); }
.ch-chip .cc-check { color: var(--ok); margin-left: auto; font-size: 0.8rem; }

/* ============================ Cloze cards ============================== */
.cloze-view { max-width: 760px; margin: 0 auto; padding: 44px 40px 120px; }
.cloze-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 8px; flex-wrap: wrap; }
.cloze-view h1 { font-family: var(--font-display); font-size: 2rem; margin: 0; }
.cloze-controls { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin: 16px 0 26px; }
.cloze-filter { padding: 7px 12px; border-radius: 999px; border: 1px solid var(--border-strong); background: var(--bg); color: var(--ink); font-family: var(--font-body); font-size: 0.85rem; }
.cloze-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; margin-bottom: 14px; }
.cloze-card .cz-text { font-size: 1.05rem; line-height: 1.7; }
.cloze-blank { background: var(--accent-soft); border-bottom: 2px solid var(--accent); border-radius: 4px; padding: 1px 10px; cursor: pointer; color: transparent; user-select: none; transition: color 0.15s ease; min-width: 40px; display: inline-block; }
.cloze-blank.revealed { color: var(--ink); background: color-mix(in srgb, var(--ok) 16%, transparent); border-bottom-color: var(--ok); cursor: default; }
.cloze-card .cz-meta { display: flex; justify-content: space-between; align-items: center; margin-top: 12px; gap: 10px; }
.cloze-card .cz-extra { color: var(--muted); font-size: 0.83rem; }
.cloze-card .cz-tag { font-size: 0.72rem; color: var(--muted); background: var(--surface-2); border-radius: 999px; padding: 2px 9px; white-space: nowrap; }
.cloze-card .cz-reveal-all { background: none; border: 1px solid var(--border-strong); border-radius: 999px; color: var(--muted); cursor: pointer; font-size: 0.76rem; padding: 3px 10px; }
.cloze-card .cz-reveal-all:hover { color: var(--accent); border-color: var(--accent); }
.export-note { margin-top: 28px; padding: 16px 20px; background: var(--surface); border: 1px dashed var(--border-strong); border-radius: var(--radius); font-size: 0.85rem; color: var(--muted); }
.export-note code { background: var(--surface-2); padding: 1px 6px; border-radius: 5px; }

/* ============================ Mobile / drawer ========================== */
.menu-btn { display: none; }
.scrim { display: none; }
@media (max-width: 1000px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; z-index: 50; top: 0; left: 0; width: min(88vw, var(--sidebar-w));
    transform: translateX(-102%); transition: transform 0.22s ease; box-shadow: var(--shadow-lg);
  }
  .app.drawer-open .sidebar { transform: translateX(0); }
  .app.drawer-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,0.35); z-index: 40; }
  .menu-btn {
    display: inline-flex; align-items: center; gap: 7px; position: fixed; top: 12px; left: 12px; z-index: 35;
    background: var(--surface); border: 1px solid var(--border-strong); border-radius: 999px;
    padding: 8px 14px; cursor: pointer; color: var(--ink); font-family: var(--font-body); box-shadow: var(--shadow);
  }
  .page-progress { left: 0; }
  .reader, .landing, .cloze-view { padding-left: 22px; padding-right: 22px; padding-top: 62px; }
  body { font-size: 16px; }
}

/* ============================ Print ==================================== */
@media print {
  .sidebar, .menu-btn, .ch-nav, .theme-toggle, .search-box, .page-progress, .scrim { display: none !important; }
  .app { grid-template-columns: 1fr; }
  .reader, .landing, .cloze-view { max-width: 100%; padding: 0; }
  body { background: #fff; color: #000; font-size: 12pt; }
  .def-card, .formula-card, .callout, figure.fig-card, .keyideas, .cloze-card { page-break-inside: avoid; border: 1px solid #ccc; background: #fff; }
  .cloze-blank { color: #000 !important; background: none; border-bottom: 1px solid #000; }
  a { color: #000; }
}

/* small helper */
.hidden { display: none !important; }
.spin { width: 26px; height: 26px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: sp 0.8s linear infinite; margin: 80px auto; }
@keyframes sp { to { transform: rotate(360deg); } }
.load-msg { text-align: center; color: var(--muted); margin-top: 12px; }
