/* Learn Agents from Pi — Kami design language.
   Warm parchment canvas, single ink-blue accent, serif-led hierarchy. */

:root {
  color-scheme: light;
  --parchment: #f5f4ed;
  --ivory: #faf9f5;
  --warm-sand: #e8e6dc;
  --brand: #1B365D;
  --brand-light: #2D5A8A;
  --brand-tint: #E4ECF5;
  --brand-tint-soft: #EEF2F7;
  --brand-tint-line: #D0DCE9;
  --near-black: #141413;
  --dark-warm: #3d3d3a;
  --olive: #504e49;
  --stone: #6b6a64;
  --border: #e8e6dc;
  --border-soft: #e5e3d8;
  --serif: Charter, "Bitstream Charter", Georgia, Palatino, "Source Han Serif SC", "Noto Serif CJK SC", "Songti SC", "STSong", "SimSun", "Times New Roman", serif;
  --body-font: var(--serif);
  --latin-ui: "Avenir Next", "Segoe UI", "Helvetica Neue", Helvetica, Arial, sans-serif;
  --mono: "JetBrains Mono", "SF Mono", "Fira Code", "Cascadia Code", Consolas, Menlo, monospace;
  --body-line: 1.58;
}

/* Chinese pages: serif carries headings, a readable CJK sans carries body. */
html[lang="zh-CN"] {
  --body-font: -apple-system, "PingFang SC", "Hiragino Sans GB", "Source Han Sans SC", "Noto Sans SC", "Microsoft YaHei", sans-serif;
  --body-line: 1.68;
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  background: var(--parchment);
  color: var(--near-black);
  font-family: var(--body-font);
  font-size: 16px;
  line-height: var(--body-line);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

a {
  color: inherit;
}

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

::selection {
  background: var(--brand-tint-line);
}

:focus-visible {
  outline: 2px solid var(--brand-light);
  outline-offset: 2px;
}

h1,
h2,
h3 {
  font-family: var(--serif);
  font-weight: 500;
  line-height: 1.22;
  color: var(--near-black);
  /* Charter falls back to oldstyle figures; chapter numbers need lining digits. */
  font-variant-numeric: lining-nums;
}

/* Reading progress rail */
.progress {
  position: fixed;
  inset: 0 auto auto 0;
  height: 2px;
  width: 0;
  background: var(--brand);
  z-index: 20;
}

/* ---------- Chrome: headers, language switch, footer ---------- */

.brand {
  font-family: var(--serif);
  font-weight: 500;
  font-size: 1.02rem;
  color: var(--near-black);
  text-decoration: none;
  white-space: nowrap;
}

.brand:hover {
  color: var(--brand);
}

.home-header,
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 64px;
  padding: 0 32px;
}

.home-header {
  max-width: 1120px;
  margin: 0 auto;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 12;
  background: var(--parchment);
  border-bottom: 1px solid var(--border-soft);
}

.home-nav,
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 22px;
}

.text-link {
  color: var(--brand);
  font-family: var(--latin-ui);
  font-size: 0.86rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-decoration: none;
  white-space: nowrap;
}

.text-link:hover {
  color: var(--brand-light);
}

.lang-link {
  color: var(--stone);
  font-family: var(--latin-ui);
  font-size: 0.86rem;
  font-weight: 500;
  letter-spacing: 0.02em;
  text-decoration: none;
  white-space: nowrap;
}

.lang-link:hover {
  color: var(--brand);
}

.site-footer {
  max-width: 1120px;
  margin: 0 auto;
  padding: 28px 32px 40px;
  border-top: 1px solid var(--border-soft);
  color: var(--stone);
  font-size: 0.84rem;
}

.site-footer p {
  margin: 0;
}

/* ---------- Shared editorial pieces ---------- */

.eyebrow,
.section-num {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 0 18px;
  color: var(--stone);
  font-family: var(--latin-ui);
  font-size: 0.76rem;
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.eyebrow::before,
.section-num::before {
  content: "";
  width: 18px;
  height: 2px;
  background: var(--brand);
  flex-shrink: 0;
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 46px;
  padding: 0 28px;
  border: 1.5px solid var(--brand);
  border-radius: 999px;
  font-family: var(--latin-ui);
  font-size: 0.92rem;
  font-weight: 500;
  letter-spacing: 0.01em;
  text-decoration: none;
  transition: background 0.15s ease, color 0.15s ease, transform 0.15s ease;
}

.button.primary {
  background: var(--brand);
  color: var(--ivory);
}

.button.primary:hover {
  background: var(--brand-light);
  border-color: var(--brand-light);
  transform: translateY(-1px);
}

.button.ghost {
  background: transparent;
  color: var(--brand);
}

.button.ghost:hover {
  background: var(--brand-tint-soft);
  transform: translateY(-1px);
}

/* ---------- Home ---------- */

.hero {
  display: grid;
  grid-template-columns: minmax(0, 1.05fr) minmax(320px, 0.95fr);
  gap: 64px;
  align-items: center;
  max-width: 1120px;
  margin: 0 auto;
  padding: 84px 32px 92px;
}

.hero-copy {
  max-width: 640px;
}

.hero h1 {
  margin: 0;
  font-size: clamp(2.3rem, 4.6vw, 3.7rem);
  line-height: 1.16;
  text-wrap: balance;
}

/* CJK display lines: keep words intact, break only at punctuation and spaces.
   The title string carries zero-width-space hints at phrase boundaries. */
html[lang="zh-CN"] .hero h1 {
  font-size: clamp(1.75rem, 3.3vw, 2.6rem);
  line-height: 1.28;
  word-break: keep-all;
}

.lead {
  margin: 24px 0 0;
  max-width: 560px;
  color: var(--olive);
  font-size: 1.12rem;
  line-height: 1.62;
  text-wrap: pretty;
}

.hero-chips {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  margin: 22px 0 0;
  color: var(--stone);
  font-family: var(--latin-ui);
  font-size: 0.82rem;
  font-weight: 500;
  letter-spacing: 0.03em;
}

.hero-chips span:not(:last-child)::after {
  content: "·";
  margin: 0 10px;
  color: #b8b7b0;
}

.hero-actions {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  margin-top: 34px;
}

/* Restrained figure: one hairline frame, the diagram's own parchment fills it. */
.diagram {
  margin: 0;
  padding: 0;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 1px 2px rgba(20, 20, 19, 0.04);
}

.principles-section,
.contents {
  max-width: 1120px;
  margin: 0 auto;
  padding: 72px 32px;
  border-top: 1px solid var(--border-soft);
}

.principles {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 40px;
}

.principle {
  padding-top: 20px;
  border-top: 1px solid var(--border);
}

.principle h2 {
  margin: 0 0 10px;
  font-size: 1.22rem;
}

.principle p {
  margin: 0;
  color: var(--olive);
  font-size: 0.95rem;
}

.section-heading {
  max-width: 720px;
  margin-bottom: 40px;
}

.section-heading h2 {
  margin: 0;
  font-size: clamp(1.6rem, 3vw, 2.2rem);
  text-wrap: balance;
}

.chapter-list {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  column-gap: 56px;
}

.chapter-item {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr);
  gap: 4px;
  padding: 20px 0;
  border-top: 1px solid var(--border);
  text-decoration: none;
}

.chapter-no {
  color: var(--brand);
  font-family: var(--latin-ui);
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  font-variant-numeric: lining-nums tabular-nums;
  padding-top: 4px;
}

.chapter-name {
  display: block;
  font-family: var(--serif);
  font-weight: 500;
  font-size: 1.06rem;
  line-height: 1.35;
  color: var(--near-black);
}

.chapter-item:hover .chapter-name {
  color: var(--brand);
}

.chapter-desc {
  display: block;
  margin-top: 6px;
  color: var(--olive);
  font-size: 0.88rem;
  line-height: 1.55;
}

/* ---------- 404 ---------- */

.notfound {
  display: flex;
  flex-direction: column;
  justify-content: center;
  max-width: 720px;
  min-height: min(calc(100svh - 240px), 640px);
  margin: 0 auto;
  padding: 72px 32px 96px;
  text-align: center;
}

.notfound .eyebrow {
  justify-content: center;
}

.notfound h1 {
  margin: 0;
  font-size: clamp(1.8rem, 4vw, 2.8rem);
}

.notfound .lead {
  margin-left: auto;
  margin-right: auto;
}

.notfound .hero-actions {
  justify-content: center;
}

/* ---------- Documentation shell ---------- */

.reader-shell {
  display: grid;
  grid-template-columns: 216px minmax(0, 720px) 188px;
  justify-content: center;
  gap: 56px;
  max-width: 1320px;
  margin: 0 auto;
  padding: 44px 32px 96px;
}

.sidebar,
.toc {
  position: sticky;
  top: 88px;
  align-self: start;
  max-height: calc(100svh - 112px);
  overflow: auto;
  scrollbar-width: thin;
}

.chapter-nav {
  display: grid;
  gap: 2px;
}

.chapter-nav a {
  display: grid;
  grid-template-columns: 30px minmax(0, 1fr);
  gap: 8px;
  padding: 7px 0 7px 14px;
  border-left: 2px solid transparent;
  color: var(--olive);
  font-size: 0.86rem;
  line-height: 1.4;
  text-decoration: none;
}

.chapter-nav a:hover {
  color: var(--near-black);
}

.chapter-nav a[aria-current="page"] {
  border-left-color: var(--brand);
  color: var(--brand);
}

.chapter-nav span {
  color: var(--stone);
  font-family: var(--latin-ui);
  font-size: 0.74rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  font-variant-numeric: lining-nums tabular-nums;
  padding-top: 2px;
}

.chapter-nav a[aria-current="page"] span {
  color: var(--brand);
}

.toc {
  padding-top: 14px;
  border-top: 1px solid var(--border);
}

.toc-title {
  margin: 0 0 10px;
  color: var(--stone);
  font-family: var(--latin-ui);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.toc a {
  display: block;
  padding: 4px 0;
  color: var(--olive);
  font-size: 0.82rem;
  line-height: 1.4;
  text-decoration: none;
}

.toc a:hover {
  color: var(--near-black);
}

.toc a.is-active {
  color: var(--brand);
}

.toc-level-3 {
  padding-left: 12px !important;
}

.course-reader {
  min-width: 0;
}

/* ---------- Chapter prose ---------- */

/* TOC anchor jumps must clear the sticky topbar. */
.chapter h1,
.chapter h2,
.chapter h3 {
  scroll-margin-top: 84px;
}

.chapter h1 {
  margin: 0 0 28px;
  font-size: clamp(1.9rem, 3.6vw, 2.5rem);
  line-height: 1.2;
  text-wrap: balance;
}

.chapter h2 {
  margin: 2.4em 0 0.7em;
  font-size: 1.42rem;
}

.chapter h3 {
  margin: 1.8em 0 0.6em;
  font-size: 1.14rem;
}

.chapter p,
.chapter li {
  font-size: 1rem;
}

.chapter ul,
.chapter ol {
  padding-left: 1.4rem;
  margin: 1em 0;
}

.chapter li {
  margin: 0.3em 0;
}

.chapter li::marker {
  color: var(--brand);
}

.chapter a {
  color: var(--brand);
  text-decoration: underline;
  text-decoration-color: var(--brand-tint-line);
  text-underline-offset: 3px;
}

.chapter a:hover {
  text-decoration-color: var(--brand);
}

.chapter strong {
  font-weight: 600;
}

.chapter code {
  font-family: var(--mono);
  font-size: 0.86em;
  background: var(--brand-tint);
  color: var(--brand);
  border: 1px solid var(--brand-tint-line);
  border-radius: 4px;
  padding: 0.08em 0.34em;
  font-variant-numeric: lining-nums tabular-nums;
}

.chapter pre {
  overflow-x: auto;
  margin: 26px 0;
  padding: 18px 22px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--ivory);
  font-size: 13.5px;
  line-height: 1.55;
}

.chapter pre code {
  display: block;
  min-width: max-content;
  background: transparent;
  border: 0;
  padding: 0;
  color: var(--near-black);
  font-size: inherit;
}

/* Build-time syntax tokens — Kami light palette, existing tokens only. */
.tok-k {
  color: var(--brand);
}

.tok-s {
  color: var(--olive);
}

.tok-c {
  color: var(--stone);
}

.tok-n {
  color: var(--dark-warm);
}

.chapter blockquote {
  margin: 24px 0;
  padding: 2px 0 2px 18px;
  border-left: 2px solid var(--brand);
  color: var(--olive);
}

.chapter blockquote p {
  margin: 0;
}

/* Quiet borderless pager */
.pager {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  margin-top: 72px;
  padding-top: 26px;
  border-top: 1px solid var(--border);
}

.pager a {
  display: grid;
  gap: 6px;
  text-decoration: none;
}

.pager a:active {
  opacity: 0.6;
}

.pager-label {
  color: var(--stone);
  font-family: var(--latin-ui);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.pager-title {
  font-family: var(--serif);
  font-weight: 500;
  font-size: 1.04rem;
  line-height: 1.35;
  color: var(--brand);
}

.pager a[rel="next"] {
  text-align: right;
}

/* ---------- Tablet ---------- */

@media (max-width: 1080px) {
  .hero {
    grid-template-columns: 1fr;
    gap: 44px;
    min-height: auto;
    padding-top: 44px;
  }

  .diagram {
    max-width: 640px;
  }

  .reader-shell {
    grid-template-columns: minmax(0, 1fr);
    gap: 0;
    padding-top: 0;
  }

  .toc {
    display: none;
  }

  /* Sidebar becomes a horizontal chapter strip under the topbar. */
  .sidebar {
    position: static;
    max-height: none;
    margin: 0 -32px;
    padding: 0 32px;
    border-bottom: 1px solid var(--border-soft);
  }

  .chapter-nav {
    display: flex;
    gap: 4px;
    overflow-x: auto;
    scrollbar-width: none;
  }

  .chapter-nav::-webkit-scrollbar {
    display: none;
  }

  .chapter-nav a {
    display: flex;
    align-items: baseline;
    gap: 7px;
    flex-shrink: 0;
    padding: 12px 10px;
    border-left: 0;
    border-bottom: 2px solid transparent;
    white-space: nowrap;
  }

  .chapter-nav a[aria-current="page"] {
    border-bottom-color: var(--brand);
  }

  .course-reader {
    padding-top: 36px;
  }
}

@media (max-width: 880px) {
  .chapter-list {
    grid-template-columns: 1fr;
  }

  .principles {
    grid-template-columns: 1fr;
    gap: 28px;
  }
}

/* ---------- Phone ---------- */

@media (max-width: 680px) {
  .home-header,
  .topbar {
    padding: 0 20px;
  }

  .home-nav,
  .topbar-actions {
    gap: 14px;
  }

  .home-nav .text-link {
    display: none;
  }

  .hero,
  .principles-section,
  .contents,
  .reader-shell,
  .site-footer,
  .notfound {
    padding-left: 20px;
    padding-right: 20px;
  }

  .hero {
    padding-top: 36px;
    padding-bottom: 56px;
  }

  .principles-section,
  .contents {
    padding-top: 48px;
    padding-bottom: 48px;
  }

  .sidebar {
    margin: 0 -20px;
    padding: 0 20px;
  }

  .chapter h1 {
    font-size: 1.7rem;
  }

  .chapter pre {
    font-size: 11.5px;
    padding: 14px 16px;
  }

  .pager {
    grid-template-columns: 1fr;
    gap: 20px;
  }

  .pager a[rel="next"] {
    text-align: left;
  }
}
