:root {
  --bg: #f5f7f4;
  --paper: #ffffff;
  --paper-soft: #e9efe9;
  --ink: #171a16;
  --muted: #66706a;
  --line: rgba(25, 42, 32, 0.14);
  --accent: #a33d32;
  --accent-dark: #7a2e27;
  --blue: #215f76;
  --code: #101614;
  --shadow: 0 24px 70px rgba(22, 34, 27, 0.1);
  --sans: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans SC", sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", "Noto Serif SC", "Songti SC", serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
  font-size: 18px;
}

body {
  margin: 0;
  color: var(--ink);
  font-family: var(--serif);
  background:
    linear-gradient(90deg, rgba(32, 42, 35, 0.055) 1px, transparent 1px),
    linear-gradient(180deg, #fbfcf8 0%, var(--bg) 38%, #e9efec 100%);
  background-size: 72px 72px, auto;
}

a {
  color: inherit;
  text-decoration: none;
}

.site-hero {
  padding: 22px clamp(18px, 4vw, 56px) 48px;
}

.topbar,
.hero-grid,
.reader-shell {
  width: min(1200px, 100%);
  margin: 0 auto;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: center;
  font-family: var(--sans);
  font-size: 0.86rem;
}

.topbar div {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.brand,
.topbar a:not(.brand),
.hero-actions a {
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 253, 248, 0.72);
}

.brand {
  padding: 9px 13px;
  font-weight: 800;
}

.topbar a:not(.brand),
.hero-actions a {
  padding: 9px 12px;
  color: var(--muted);
}

.hero-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(280px, 430px);
  gap: 32px;
  align-items: end;
  padding-top: 86px;
}

.eyebrow,
.toc-title,
.source-note {
  margin: 0;
  color: var(--accent);
  font-family: var(--sans);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.hero-copy h1 {
  margin: 14px 0 0;
  max-width: 13ch;
  font-size: clamp(2.8rem, 7vw, 5.9rem);
  line-height: 0.98;
  letter-spacing: 0;
}

.lead {
  max-width: 720px;
  margin: 24px 0 0;
  color: #4e493f;
  font-size: clamp(1.05rem, 2vw, 1.28rem);
  line-height: 1.85;
}

.hero-actions {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  margin-top: 30px;
  font-family: var(--sans);
}

.hero-actions .primary-action {
  border-color: transparent;
  background: var(--ink);
  color: #fffaf0;
}

.pattern-panel {
  margin: 0;
  padding: 26px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #15211e;
  color: #fbfff6;
  box-shadow: var(--shadow);
}

.pattern-panel p {
  margin: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 1.08rem;
  line-height: 1.75;
}

.pattern-panel span {
  display: block;
  margin-top: 22px;
  color: #c4d0c7;
  line-height: 1.7;
}

.reader-shell {
  display: grid;
  grid-template-columns: 270px minmax(0, 820px);
  gap: 52px;
  align-items: start;
  padding: 18px clamp(18px, 4vw, 56px) 80px;
}

.reading-progress {
  position: fixed;
  z-index: 20;
  top: 0;
  left: 0;
  width: 100%;
  height: 3px;
  background: transparent;
}

.reading-progress span {
  display: block;
  width: 0;
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--blue));
}

.chapter-top {
  padding: 18px clamp(18px, 3vw, 44px) 22px;
}

.chapter-masthead {
  width: min(980px, 100%);
  margin: 58px auto 0;
  padding-bottom: 20px;
}

.chapter-masthead h1 {
  max-width: 15ch;
  margin: 14px 0 0;
  font-size: clamp(2.35rem, 6vw, 5.2rem);
  line-height: 1.02;
  letter-spacing: 0;
}

.chapter-pager,
.bottom-pager {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin-top: 26px;
  font-family: var(--sans);
}

.page-link {
  display: grid;
  gap: 4px;
  min-height: 72px;
  padding: 13px 15px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.72);
}

.page-link span {
  color: var(--muted);
  font-size: 0.76rem;
}

.page-link strong {
  overflow-wrap: anywhere;
  font-size: 0.92rem;
  line-height: 1.35;
}

.page-link.is-next {
  text-align: right;
}

.page-link.is-disabled {
  color: #9aa29c;
}

.chapter-layout {
  display: grid;
  grid-template-columns: minmax(180px, 230px) minmax(0, 740px) minmax(190px, 250px);
  gap: clamp(18px, 3vw, 34px);
  align-items: start;
  width: min(1320px, 100%);
  margin: 0 auto;
  padding: 10px clamp(16px, 3vw, 42px) 76px;
}

.course-rail,
.chapter-toc {
  position: sticky;
  top: 18px;
  max-height: calc(100vh - 36px);
  overflow: auto;
  font-family: var(--sans);
}

.course-rail {
  padding: 8px 0;
}

.course-links {
  display: grid;
  gap: 3px;
  margin-top: 12px;
}

.course-links a {
  display: grid;
  grid-template-columns: 36px 1fr;
  gap: 7px;
  align-items: center;
  min-height: 34px;
  padding: 5px 7px;
  border-radius: 6px;
  color: #3c463f;
  font-size: 0.78rem;
  line-height: 1.25;
}

.course-links span {
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.72rem;
}

.course-links a:hover,
.course-links a.is-current {
  background: #e6eee9;
  color: var(--accent-dark);
}

.chapter-toc {
  border: 0;
  background: transparent;
  backdrop-filter: none;
}

.chapter-toc .toc-links a {
  grid-template-columns: 28px 1fr;
  padding: 6px 5px;
  font-size: 0.78rem;
}

.chapter-toc .toc-links a.level-3 {
  margin-left: 18px;
  color: #58635d;
}

.chapter-article {
  padding: clamp(26px, 5vw, 58px) clamp(22px, 5vw, 68px) 64px;
}

.chapter-article > h1:first-of-type {
  display: none;
}

.chapter-index {
  margin-bottom: 42px;
  padding-bottom: 36px;
  border-bottom: 1px solid var(--line);
}

.chapter-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 18px;
}

.chapter-card {
  display: grid;
  gap: 5px;
  min-height: 104px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8faf7;
  font-family: var(--sans);
}

.chapter-card span {
  color: var(--blue);
  font-family: var(--mono);
  font-size: 0.76rem;
}

.chapter-card strong {
  font-size: 1rem;
}

.chapter-card small {
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.5;
}

.chapter-card:hover {
  border-color: rgba(163, 61, 50, 0.38);
  background: #fffdfa;
}

.chapter-en {
  color: var(--muted);
  font-family: var(--sans);
  font-size: 1rem;
}

.chapter-thesis {
  padding: 18px 20px;
  border-left: 4px solid var(--accent);
  background: #eef4ef;
  color: #2f3a33;
  font-size: 1.05rem;
}

.toc {
  position: sticky;
  top: 18px;
  max-height: calc(100vh - 36px);
  padding: 18px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 253, 248, 0.74);
  backdrop-filter: blur(10px);
}

.toc-title {
  margin-bottom: 12px;
}

.toc-links {
  display: grid;
  gap: 6px;
}

.toc-links a {
  display: grid;
  grid-template-columns: 32px 1fr;
  gap: 8px;
  align-items: baseline;
  padding: 8px 6px;
  border-radius: 6px;
  color: #3e3a32;
  font-family: var(--sans);
  font-size: 0.82rem;
  line-height: 1.35;
}

.toc-links a:hover,
.toc-links a.is-active {
  background: #e5ece6;
  color: var(--accent-dark);
}

.toc-links span {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.article {
  min-width: 0;
  padding: 38px clamp(20px, 4vw, 62px) 58px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  box-shadow: var(--shadow);
}

.source-note {
  margin-bottom: 30px;
  letter-spacing: 0;
  text-transform: none;
}

.source-note a,
.article a {
  color: var(--accent-dark);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 0.16em;
}

.article h1,
.article h2,
.article h3,
.article h4 {
  margin: 0;
  scroll-margin-top: 28px;
  line-height: 1.25;
  letter-spacing: 0;
}

.article h1 {
  margin-bottom: 26px;
  font-size: clamp(2.1rem, 5vw, 3.4rem);
}

.article h2 {
  margin-top: 64px;
  padding-top: 26px;
  border-top: 1px solid var(--line);
  font-size: clamp(1.55rem, 3vw, 2.2rem);
}

.article h3 {
  margin-top: 38px;
  font-size: 1.28rem;
}

.article h4 {
  margin-top: 30px;
  font-size: 1.05rem;
}

.article p,
.article li,
.article blockquote {
  line-height: 1.92;
}

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

.article img {
  display: block;
  max-width: 100%;
  height: auto;
}

.media-figure {
  margin: 28px 0;
  padding: 18px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8faf7;
}

.section-rule {
  margin: 34px 0;
  border: 0;
  border-top: 1px solid var(--line);
}

.article ul,
.article ol {
  margin: 18px 0 0;
  padding-left: 1.35em;
}

.article li + li {
  margin-top: 8px;
}

.article blockquote {
  margin: 24px 0;
  padding: 18px 22px;
  border-left: 4px solid var(--accent);
  background: #eef4ef;
}

.article blockquote p {
  margin: 0;
}

.article code {
  padding: 0.12em 0.34em;
  border-radius: 4px;
  background: #e7ece7;
  font-family: var(--mono);
  font-size: 0.9em;
}

.code-block {
  margin: 26px 0;
  overflow: hidden;
  border-radius: 8px;
  background: var(--code);
  color: #f4f1e8;
}

.code-block figcaption {
  padding: 10px 14px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  color: #bbb3a1;
  font-family: var(--sans);
  font-size: 0.78rem;
}

.code-block pre {
  margin: 0;
  padding: 18px;
  overflow: auto;
  font-size: 0.86rem;
  line-height: 1.7;
}

.code-block code {
  padding: 0;
  border-radius: 0;
  background: transparent;
  color: inherit;
}

.table-wrap {
  width: 100%;
  margin: 24px 0;
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}

table {
  width: 100%;
  min-width: 620px;
  border-collapse: collapse;
  font-family: var(--sans);
  font-size: 0.9rem;
}

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

th {
  background: var(--paper-soft);
  font-weight: 800;
}

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

@media (max-width: 900px) {
  html {
    font-size: 17px;
  }

  .site-hero {
    padding: 14px 12px 26px;
  }

  .topbar {
    align-items: flex-start;
  }

  .hero-grid {
    grid-template-columns: 1fr;
    padding-top: 48px;
  }

  .hero-copy h1 {
    max-width: 9ch;
  }

  .reader-shell {
    display: block;
    padding: 0 12px 50px;
  }

  .chapter-top {
    padding: 14px 12px 10px;
  }

  .chapter-masthead {
    margin-top: 42px;
  }

  .chapter-layout {
    display: block;
    padding: 0 12px 56px;
  }

  .course-rail {
    position: relative;
    top: auto;
    max-height: none;
    margin-bottom: 12px;
    padding: 12px;
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.8);
  }

  .course-links {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding-bottom: 4px;
  }

  .course-links a {
    min-width: 150px;
  }

  .chapter-toc {
    display: none;
  }

  .toc {
    position: relative;
    top: auto;
    max-height: none;
    margin-bottom: 14px;
  }

  .toc-links {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding-bottom: 4px;
  }

  .toc-links a {
    min-width: 220px;
  }

  .article {
    padding: 26px 18px 42px;
  }

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

  .page-link.is-next {
    text-align: left;
  }
}

@media (max-width: 520px) {
  .topbar,
  .topbar div,
  .hero-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .topbar a,
  .hero-actions a {
    text-align: center;
  }

  .hero-copy h1 {
    font-size: 2.55rem;
  }

  .pattern-panel {
    padding: 20px;
  }
}
