:root {
  --reader-scale: 1;
  --bg: #f4f1ea;
  --paper: #fffdf8;
  --paper-soft: #f8f3e9;
  --ink: #241f1a;
  --muted: #756c60;
  --faint: #d8cdbc;
  --line: #ded3c2;
  --accent: #b34d2f;
  --accent-2: #1d665f;
  --accent-3: #d7a23a;
  --shadow: 0 24px 70px rgba(63, 49, 32, 0.16);
  --mono: "SFMono-Regular", "Menlo", "Consolas", monospace;
  --serif: "Iowan Old Style", "Palatino Linotype", "Book Antiqua", Georgia, serif;
  --sans: "Avenir Next", "Gill Sans", "Trebuchet MS", sans-serif;
}

:root[data-theme="dark"] {
  --bg: #191815;
  --paper: #23211d;
  --paper-soft: #2c2923;
  --ink: #f5efe2;
  --muted: #bdb29f;
  --faint: #4b4439;
  --line: #3b362e;
  --accent: #df7a54;
  --accent-2: #78bcb1;
  --accent-3: #dfb85a;
  --shadow: 0 24px 70px rgba(0, 0, 0, 0.32);
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background:
    linear-gradient(90deg, rgba(36, 31, 26, 0.035) 1px, transparent 1px) 0 0 / 38px 38px,
    radial-gradient(circle at 12% 4%, rgba(179, 77, 47, 0.13), transparent 34%),
    linear-gradient(135deg, var(--bg), var(--paper-soft));
  font-family: var(--sans);
}

body::before {
  position: fixed;
  inset: 0;
  pointer-events: none;
  content: "";
  opacity: 0.16;
  background-image:
    repeating-linear-gradient(0deg, rgba(70, 57, 42, 0.18) 0 1px, transparent 1px 4px);
  mix-blend-mode: multiply;
}

a {
  color: var(--accent-2);
}

button,
input {
  font: inherit;
}

.progress {
  position: fixed;
  inset: 0 0 auto;
  z-index: 50;
  height: 4px;
  background: linear-gradient(90deg, var(--accent), var(--accent-3), var(--accent-2));
  transform: scaleX(0);
  transform-origin: left;
}

.app-shell {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr) 270px;
  gap: 24px;
  width: min(1680px, 100%);
  margin: 0 auto;
  padding: 22px;
}

.sidebar,
.outline {
  position: sticky;
  top: 22px;
  height: calc(100vh - 44px);
}

.sidebar {
  overflow: hidden;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: color-mix(in srgb, var(--paper) 88%, transparent);
  box-shadow: var(--shadow);
}

.brand {
  display: grid;
  grid-template-columns: 56px 1fr;
  gap: 14px;
  padding: 20px;
  border-bottom: 1px solid var(--line);
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 56px;
  height: 70px;
  color: #fffaf0;
  background: var(--ink);
  border-radius: 4px;
  font-family: var(--serif);
  font-size: 34px;
  font-weight: 700;
  box-shadow: inset 0 -8px 0 var(--accent);
}

.brand-kicker {
  margin: 0 0 7px;
  color: var(--accent);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.12em;
}

.brand h1 {
  margin: 0;
  font-family: var(--serif);
  font-size: 26px;
  line-height: 1.1;
  letter-spacing: 0;
}

.search-box {
  display: block;
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
}

.search-box span {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.search-box input {
  width: 100%;
  height: 40px;
  padding: 0 12px;
  color: var(--ink);
  background: var(--paper-soft);
  border: 1px solid var(--line);
  border-radius: 6px;
  outline: none;
}

.search-box input:focus {
  border-color: var(--accent-2);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent-2) 18%, transparent);
}

.toc {
  overflow: auto;
  padding: 12px 12px 20px;
}

.toc-group {
  margin-bottom: 15px;
}

.toc-group p {
  margin: 8px 8px;
  color: var(--accent);
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.toc-link {
  display: grid;
  width: 100%;
  gap: 3px;
  margin-bottom: 3px;
  padding: 10px 11px;
  color: var(--ink);
  text-align: left;
  background: transparent;
  border: 0;
  border-left: 3px solid transparent;
  border-radius: 6px;
  cursor: pointer;
}

.toc-link:hover {
  background: color-mix(in srgb, var(--accent-3) 12%, transparent);
}

.toc-link.active {
  background: color-mix(in srgb, var(--accent) 11%, transparent);
  border-left-color: var(--accent);
}

.toc-link span {
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
}

.toc-link strong {
  font-size: 14px;
  line-height: 1.3;
}

.toc-empty {
  margin: 14px 8px;
  padding: 14px;
  color: var(--muted);
  background: var(--paper-soft);
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 13px;
  line-height: 1.5;
}

.reader {
  min-width: 0;
}

.topbar {
  position: sticky;
  top: 22px;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 58px;
  margin-bottom: 18px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: color-mix(in srgb, var(--paper) 84%, transparent);
  box-shadow: 0 12px 34px rgba(60, 45, 28, 0.1);
}

.chapter-signal {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--muted);
  font-size: 13px;
}

.chapter-signal strong {
  color: var(--ink);
  font-family: var(--mono);
}

.reader-tools {
  display: flex;
  gap: 7px;
}

.tool-button,
.icon-button,
.chapter-nav button {
  height: 36px;
  padding: 0 12px;
  color: var(--ink);
  background: var(--paper-soft);
  border: 1px solid var(--line);
  border-radius: 6px;
  cursor: pointer;
}

.tool-button:hover,
.icon-button:hover,
.chapter-nav button:hover {
  border-color: var(--accent);
}

.mobile-only {
  display: none;
}

.hero-panel {
  position: relative;
  overflow: hidden;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 20px;
  margin-bottom: 16px;
  padding: clamp(22px, 3vw, 34px);
  color: #fffaf0;
  background:
    linear-gradient(120deg, rgba(35, 31, 25, 0.96), rgba(48, 61, 56, 0.94)),
    repeating-linear-gradient(90deg, transparent 0 18px, rgba(255, 255, 255, 0.05) 18px 19px);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.hero-panel::after {
  position: absolute;
  right: 24px;
  bottom: -32px;
  content: "AGENT";
  color: rgba(255, 255, 255, 0.055);
  font-family: var(--serif);
  font-size: 96px;
  font-weight: 900;
  letter-spacing: 0;
}

.hero-copy {
  position: relative;
  z-index: 1;
}

.eyebrow {
  margin: 0 0 10px;
  color: var(--accent-3);
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.hero-copy h2 {
  max-width: 660px;
  margin: 0;
  font-family: var(--serif);
  font-size: clamp(32px, 3.1vw, 44px);
  line-height: 1.16;
  letter-spacing: 0;
}

.hero-copy p:last-child {
  max-width: 650px;
  margin: 14px 0 0;
  color: rgba(255, 250, 240, 0.82);
  font-size: 16px;
  line-height: 1.58;
}

.hero-stats {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  max-width: 560px;
  gap: 10px;
}

.hero-stats div {
  padding: 12px 14px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.07);
}

.hero-stats strong {
  display: block;
  font-family: var(--serif);
  font-size: 27px;
  line-height: 1;
}

.hero-stats span {
  color: rgba(255, 250, 240, 0.72);
  font-size: 13px;
}

.article-card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.article-meta {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 18px clamp(22px, 6vw, 74px);
  color: var(--muted);
  border-bottom: 1px solid var(--line);
  font-size: 13px;
  font-weight: 800;
}

.article-content {
  max-width: 860px;
  margin: 0 auto;
  padding: clamp(26px, 5vw, 58px);
  font-size: calc(19px * var(--reader-scale));
  line-height: 1.86;
}

.article-content h1 {
  margin: 0 0 24px;
  font-family: var(--serif);
  font-size: calc(40px * var(--reader-scale));
  line-height: 1.16;
  letter-spacing: 0;
}

.article-content h2 {
  margin: 44px 0 14px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
  font-family: var(--serif);
  font-size: calc(27px * var(--reader-scale));
  line-height: 1.24;
  letter-spacing: 0;
}

.article-content h3 {
  margin: 34px 0 12px;
  color: var(--accent);
  font-size: calc(19px * var(--reader-scale));
  line-height: 1.4;
}

.article-content p {
  margin: 0 0 18px;
}

.article-content ul,
.article-content ol {
  margin: 0 0 22px;
  padding-left: 1.3em;
}

.article-content li {
  margin: 5px 0;
}

.article-content blockquote {
  margin: 28px 0;
  padding: 18px 22px;
  color: var(--ink);
  background: color-mix(in srgb, var(--accent-3) 14%, transparent);
  border-left: 4px solid var(--accent-3);
  border-radius: 0 7px 7px 0;
  font-family: var(--serif);
  font-size: 1.08em;
}

.article-content code {
  padding: 0.12em 0.32em;
  background: var(--paper-soft);
  border: 1px solid var(--line);
  border-radius: 4px;
  font-family: var(--mono);
  font-size: 0.86em;
}

.article-content pre {
  overflow: auto;
  margin: 28px 0;
  padding: 20px;
  color: #f4ead8;
  background: #24211d;
  border-radius: 8px;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
}

.article-content pre code {
  padding: 0;
  color: inherit;
  background: transparent;
  border: 0;
  font-size: 14px;
  line-height: 1.65;
}

.table-wrap {
  overflow: auto;
  margin: 28px 0;
  border: 1px solid var(--line);
  border-radius: 8px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9em;
}

th,
td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

th {
  background: var(--paper-soft);
  color: var(--accent);
}

.chapter-nav {
  display: flex;
  justify-content: space-between;
  margin: 18px 0 44px;
}

.chapter-nav button {
  min-width: 116px;
}

.chapter-nav button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.outline {
  display: grid;
  align-content: start;
  gap: 16px;
}

.outline-card,
.note-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: color-mix(in srgb, var(--paper) 88%, transparent);
  box-shadow: 0 18px 54px rgba(63, 49, 32, 0.12);
}

.outline-card {
  overflow: hidden;
}

.outline-card p,
.note-card p {
  margin: 0;
  padding: 14px 16px;
  color: var(--accent);
  border-bottom: 1px solid var(--line);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.08em;
}

#outlineList {
  display: grid;
  gap: 2px;
  max-height: 52vh;
  overflow: auto;
  padding: 10px;
}

#outlineList a,
#outlineList span {
  display: block;
  padding: 8px;
  color: var(--muted);
  border-radius: 5px;
  font-size: 13px;
  line-height: 1.35;
  text-decoration: none;
}

#outlineList a:hover {
  color: var(--ink);
  background: var(--paper-soft);
}

.note-card {
  padding-bottom: 16px;
}

.note-card strong,
.note-card span {
  display: block;
  padding: 0 16px;
}

.note-card strong {
  margin-top: 14px;
  font-family: var(--serif);
  font-size: 20px;
  line-height: 1.25;
}

.note-card span {
  margin-top: 8px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.55;
}

.scrim {
  display: none;
}

@media (max-width: 1180px) {
  .app-shell {
    grid-template-columns: 300px minmax(0, 1fr);
  }

  .outline {
    display: none;
  }

  .hero-panel {
    padding: 24px;
  }

  .hero-copy h2 {
    max-width: 620px;
    font-size: clamp(30px, 4vw, 38px);
  }

  .hero-copy p:last-child {
    font-size: 15px;
  }

  .article-content h1 {
    font-size: calc(36px * var(--reader-scale));
  }

  .article-content h2 {
    font-size: calc(25px * var(--reader-scale));
  }
}

@media (max-width: 820px) {
  .app-shell {
    display: block;
    padding: 12px;
  }

  .mobile-only {
    display: inline-grid;
    place-items: center;
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 60;
    width: min(86vw, 340px);
    height: 100vh;
    border-radius: 0;
    transform: translateX(-104%);
    transition: transform 220ms ease;
  }

  .menu-open .sidebar {
    transform: translateX(0);
  }

  .scrim {
    position: fixed;
    inset: 0;
    z-index: 55;
    display: none;
    background: rgba(20, 17, 13, 0.45);
  }

  .menu-open .scrim {
    display: block;
  }

  .topbar {
    top: 8px;
    margin-bottom: 12px;
  }

  .chapter-signal {
    font-size: 12px;
  }

  .hero-panel {
    grid-template-columns: 1fr;
    gap: 16px;
    padding: 20px;
  }

  .hero-copy h2 {
    max-width: 100%;
    font-size: clamp(28px, 8.6vw, 34px);
    line-height: 1.18;
  }

  .hero-copy p:last-child {
    margin-top: 12px;
    font-size: 14px;
    line-height: 1.55;
  }

  .hero-stats {
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
  }

  .hero-stats div {
    padding: 10px;
  }

  .hero-stats strong {
    font-size: 23px;
  }

  .hero-stats span {
    font-size: 12px;
  }

  .article-content {
    padding: 26px 20px;
    font-size: calc(17px * var(--reader-scale));
  }

  .article-content h1 {
    margin-bottom: 20px;
    font-size: calc(30px * var(--reader-scale));
    line-height: 1.2;
  }

  .article-content h2 {
    margin-top: 38px;
    font-size: calc(23px * var(--reader-scale));
  }
}

@media print {
  body {
    background: #fff;
  }

  .sidebar,
  .topbar,
  .hero-panel,
  .outline,
  .chapter-nav,
  .progress {
    display: none;
  }

  .app-shell {
    display: block;
    padding: 0;
  }

  .article-card {
    border: 0;
    box-shadow: none;
  }
}
