/* ============================================================
   Pi, Explained — styles (warm & relaxed)
   ============================================================ */

:root {
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas,
    "Liberation Mono", monospace;

  --radius: 12px;
  --radius-sm: 8px;
  --topbar-h: 62px;
  --sidebar-w: 280px;
  --content-max: 860px;
}

/* ---------- light theme: warm paper ---------- */
:root[data-theme="light"] {
  --bg: #faf6ee;
  --bg-gradient:
    radial-gradient(1100px 520px at 85% -6%, rgba(14, 122, 107, 0.08), transparent 62%),
    radial-gradient(950px 480px at -8% 22%, rgba(224, 148, 60, 0.09), transparent 58%),
    linear-gradient(180deg, #fcf9f3 0%, #f8f3e9 100%);
  --bg-soft: #f2ebdd;
  --bg-code: #f1e9d9;
  --bg-card: #fffdf9;
  --border: #e8dfcc;
  --border-strong: #d9cdb3;
  --text: #332e26;
  --text-soft: #5d564a;
  --text-faint: #7f7666;
  --accent: #0e7a6b;
  --accent-soft: rgba(14, 122, 107, 0.1);
  --accent-hover: #0a6156;
  --shadow: 0 1px 3px rgba(122, 100, 60, 0.1), 0 1px 2px rgba(122, 100, 60, 0.06);
  --shadow-lg: 0 10px 28px rgba(122, 100, 60, 0.16);
  --pre-bg: #2a251d;
  --pre-text: #efe9dc;
  --badge-core-bg: rgba(217, 132, 42, 0.14);
  --badge-core-text: #a05a10;
  --badge-adv-bg: rgba(146, 89, 168, 0.13);
  --badge-adv-text: #7a3d92;
  --badge-beg-bg: rgba(37, 140, 90, 0.13);
  --badge-beg-text: #166a43;
  --table-stripe: #f5efe2;
}

/* ---------- dark theme: warm dusk, never pitch black ---------- */
:root[data-theme="dark"] {
  --bg: #211d18;
  --bg-gradient:
    radial-gradient(1100px 520px at 85% -6%, rgba(82, 199, 178, 0.07), transparent 62%),
    radial-gradient(950px 480px at -8% 22%, rgba(224, 160, 90, 0.06), transparent 58%),
    linear-gradient(180deg, #242019 0%, #1e1a15 100%);
  --bg-soft: #2b2620;
  --bg-code: #302a23;
  --bg-card: #2a251f;
  --border: #3c352b;
  --border-strong: #514838;
  --text: #ede6d9;
  --text-soft: #c4bba9;
  --text-faint: #9d9381;
  --accent: #56c8b3;
  --accent-soft: rgba(86, 200, 179, 0.13);
  --accent-hover: #7fd9c8;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
  --shadow-lg: 0 10px 28px rgba(0, 0, 0, 0.45);
  --pre-bg: #16130f;
  --pre-text: #e8e1d3;
  --badge-core-bg: rgba(240, 158, 70, 0.16);
  --badge-core-text: #f0b26e;
  --badge-adv-bg: rgba(190, 130, 220, 0.16);
  --badge-adv-text: #d3a6ec;
  --badge-beg-bg: rgba(80, 200, 140, 0.15);
  --badge-beg-text: #7fd6a8;
  --table-stripe: #26211b;
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
  scroll-padding-top: calc(var(--topbar-h) + 20px);
}

body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: 17px;
  line-height: 1.75;
  color: var(--text);
  background-color: var(--bg);
  background-image: var(--bg-gradient);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
}

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

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

/* ============================================================
   Top bar
   ============================================================ */
.topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--topbar-h);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  z-index: 50;
}

.topbar-inner {
  max-width: 1440px;
  margin: 0 auto;
  height: 100%;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 20px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 18px;
  color: var(--text);
}

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

.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), #d99a3d);
  color: #fff;
  font-size: 19px;
  font-weight: 700;
}

.top-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 10px;
}

.lang-switch {
  display: inline-flex;
  align-items: center;
  padding: 7px 16px;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  font-size: 14.5px;
  font-weight: 600;
  color: var(--text-soft);
  background: color-mix(in srgb, var(--bg-card) 60%, transparent);
  transition: all 0.15s;
}

.lang-switch:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-soft);
}

.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  background: color-mix(in srgb, var(--bg-card) 60%, transparent);
  cursor: pointer;
  font-size: 16px;
  transition: all 0.15s;
}

.theme-toggle:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

:root[data-theme="light"] .icon-sun { display: none; }
:root[data-theme="dark"] .icon-moon { display: none; }

.nav-toggle {
  display: none;
  width: 38px;
  height: 38px;
  border: 1px solid var(--border-strong);
  border-radius: 9px;
  background: transparent;
  color: var(--text);
  font-size: 17px;
  cursor: pointer;
}

/* ============================================================
   Layout
   ============================================================ */
.layout {
  display: flex;
  max-width: 1440px;
  margin: 0 auto;
  padding-top: var(--topbar-h);
}

.sidebar {
  position: sticky;
  top: var(--topbar-h);
  width: var(--sidebar-w);
  flex-shrink: 0;
  height: calc(100vh - var(--topbar-h));
  overflow-y: auto;
  padding: 26px 16px 40px;
  border-right: 1px solid var(--border);
}

.sidebar-overlay {
  display: none;
}

.main {
  flex: 1;
  min-width: 0;
  padding: 42px 52px 24px;
}

/* ---------- sidebar nav ---------- */
.chapter-nav {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.side-group {
  margin: 20px 10px 7px;
  font-size: 12.5px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-faint);
}

.side-link {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 8px 11px;
  border-radius: var(--radius-sm);
  font-size: 15.5px;
  color: var(--text-soft);
  transition: all 0.12s;
}

.side-link:hover {
  background: var(--bg-soft);
  color: var(--text);
}

.side-link.active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.side-num {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-faint);
  flex-shrink: 0;
}

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

.side-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ============================================================
   Home page
   ============================================================ */
.page-home .main {
  max-width: 1000px;
}

.home-hero {
  padding: 52px 0 38px;
  text-align: center;
}

.hero-badge {
  display: inline-block;
  padding: 6px 16px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 14.5px;
  font-weight: 600;
  margin-bottom: 20px;
}

.hero-title {
  font-size: clamp(34px, 5vw, 54px);
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 1.2;
  margin: 0 0 20px;
  background: linear-gradient(120deg, var(--text) 55%, var(--accent));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.hero-intro {
  max-width: 700px;
  margin: 0 auto 28px;
  color: var(--text-soft);
  font-size: 18px;
}

.hero-actions {
  margin-bottom: 16px;
}

.btn {
  display: inline-block;
  padding: 13px 30px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 16.5px;
  transition: all 0.15s;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  box-shadow: var(--shadow);
}

.btn-primary:hover {
  background: var(--accent-hover);
  color: #fff;
  transform: translateY(-1px);
  box-shadow: var(--shadow-lg);
}

.hero-note {
  color: var(--text-faint);
  font-size: 15px;
  max-width: 600px;
  margin: 0 auto;
}

.home-section-title {
  font-size: 24px;
  font-weight: 700;
  margin: 42px 0 22px;
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(290px, 1fr));
  gap: 18px;
  padding-bottom: 44px;
}

.card {
  display: flex;
  flex-direction: column;
  padding: 22px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
  box-shadow: var(--shadow);
  transition: all 0.18s;
  color: inherit;
}

.card:hover {
  border-color: var(--accent);
  transform: translateY(-3px);
  box-shadow: var(--shadow-lg);
  color: inherit;
}

.card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 13px;
}

.card-num {
  font-family: var(--font-mono);
  font-size: 15px;
  font-weight: 600;
  color: var(--text-faint);
}

.card-title {
  margin: 0 0 9px;
  font-size: 18.5px;
  font-weight: 700;
  line-height: 1.4;
}

.card-desc {
  margin: 0;
  font-size: 15px;
  color: var(--text-soft);
  line-height: 1.65;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.badge {
  display: inline-block;
  padding: 3px 12px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
}

.badge-beginner {
  background: var(--badge-beg-bg);
  color: var(--badge-beg-text);
}

.badge-core {
  background: var(--badge-core-bg);
  color: var(--badge-core-text);
}

.badge-advanced {
  background: var(--badge-adv-bg);
  color: var(--badge-adv-text);
}

/* ============================================================
   Chapter page
   ============================================================ */
.article {
  max-width: var(--content-max);
  margin: 0 auto;
}

.article-head {
  margin-bottom: 34px;
  padding-bottom: 26px;
  border-bottom: 1px solid var(--border);
}

.crumbs {
  font-size: 14.5px;
  color: var(--text-faint);
  margin-bottom: 15px;
}

.crumbs a {
  color: var(--text-faint);
}

.crumbs a:hover {
  color: var(--accent);
}

.crumb-sep {
  margin: 0 8px;
}

.article-title {
  font-size: clamp(27px, 3.5vw, 37px);
  font-weight: 800;
  letter-spacing: -0.015em;
  line-height: 1.32;
  margin: 0 0 17px;
}

.article-meta {
  display: flex;
  align-items: center;
  gap: 13px;
  flex-wrap: wrap;
}

.meta-desc {
  font-size: 15.5px;
  color: var(--text-soft);
}

/* ---------- prose ---------- */
.prose {
  font-size: 17px;
}

.prose > *:first-child {
  margin-top: 0;
}

.prose h2 {
  font-size: 25px;
  font-weight: 750;
  margin: 50px 0 17px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
  letter-spacing: -0.01em;
}

.prose h3 {
  font-size: 20px;
  font-weight: 700;
  margin: 34px 0 13px;
}

.prose h4 {
  font-size: 17.5px;
  font-weight: 700;
  margin: 26px 0 11px;
}

.prose p {
  margin: 15px 0;
}

.prose ul,
.prose ol {
  padding-left: 27px;
  margin: 15px 0;
}

.prose li {
  margin: 7px 0;
}

.prose li::marker {
  color: var(--accent);
}

.prose strong {
  font-weight: 700;
}

.prose blockquote {
  margin: 20px 0;
  padding: 14px 20px;
  border-left: 4px solid var(--accent);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  background: var(--bg-soft);
  color: var(--text-soft);
  font-size: 16px;
}

.prose blockquote p {
  margin: 6px 0;
}

.prose hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 42px 0;
}

.prose code {
  font-family: var(--font-mono);
  font-size: 0.87em;
  padding: 2px 7px;
  border-radius: 6px;
  background: var(--bg-code);
  border: 1px solid var(--border);
}

.prose pre {
  margin: 20px 0;
  padding: 18px 20px;
  border-radius: var(--radius);
  background: var(--pre-bg);
  color: var(--pre-text);
  overflow-x: auto;
  line-height: 1.65;
  font-size: 14.5px;
  border: 1px solid var(--border);
}

.prose pre code {
  padding: 0;
  background: transparent;
  border: none;
  color: inherit;
  font-size: inherit;
  white-space: pre;
}

.prose img {
  max-width: 100%;
  height: auto;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: #fff;
  display: block;
  margin: 20px auto 6px;
}

.prose em {
  color: var(--text-soft);
}

/* ---------- figure captions (built by main.js) ---------- */
.prose figure.diagram {
  margin: 24px 0;
  text-align: center;
}

.prose figure.diagram p {
  margin: 0;
}

.prose figure.diagram img {
  margin: 0 auto;
}

.prose figure.diagram figcaption {
  margin-top: 10px;
  font-size: 14.5px;
  font-style: italic;
  color: var(--text-faint);
}

/* ---------- tables ---------- */
.prose table {
  width: 100%;
  border-collapse: collapse;
  margin: 20px 0;
  font-size: 15.5px;
  display: block;
  overflow-x: auto;
}

.prose thead th {
  text-align: left;
  font-weight: 700;
  background: var(--bg-soft);
  color: var(--text);
  padding: 11px 15px;
  border: 1px solid var(--border);
  white-space: nowrap;
}

.prose tbody td {
  padding: 10px 15px;
  border: 1px solid var(--border);
  vertical-align: top;
}

.prose tbody tr:nth-child(even) {
  background: var(--table-stripe);
}

/* ---------- pager ---------- */
.pager {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-top: 58px;
}

.pager-link {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 17px 21px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
  transition: all 0.15s;
  min-width: 0;
}

.pager-link:hover {
  border-color: var(--accent);
  box-shadow: var(--shadow);
}

.pager-next {
  text-align: right;
  align-items: flex-end;
}

.pager-dir {
  font-size: 13.5px;
  color: var(--text-faint);
  font-weight: 600;
}

.pager-title {
  font-weight: 650;
  font-size: 16.5px;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

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

/* ---------- footer ---------- */
.site-footer {
  max-width: var(--content-max);
  margin: 58px auto 0;
  padding: 26px 0 18px;
  border-top: 1px solid var(--border);
  font-size: 14.5px;
  color: var(--text-faint);
}

.site-footer p {
  margin: 5px 0;
}

.site-footer a {
  color: var(--text-faint);
}

.site-footer a:hover {
  color: var(--accent);
}

/* ============================================================
   Responsive
   ============================================================ */
@media (max-width: 960px) {
  body {
    font-size: 16.5px;
  }

  .sidebar {
    position: fixed;
    left: 0;
    top: var(--topbar-h);
    z-index: 40;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    background: var(--bg);
    box-shadow: var(--shadow-lg);
  }

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

  body.nav-open .sidebar-overlay {
    display: block;
    position: fixed;
    inset: var(--topbar-h) 0 0 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 30;
  }

  .nav-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .main {
    padding: 28px 20px 20px;
  }

  .prose {
    font-size: 16.5px;
  }

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

  .pager-next {
    text-align: left;
    align-items: flex-start;
  }

  .brand-text {
    font-size: 16px;
  }
}
