/* =============================================================
   Frontend System Design — theme
   Single source of styling for every generated page.
   ============================================================= */

:root {
  --bg:        #fbfbfd;
  --surface:   #ffffff;
  --surface-2: #f3f4f8;
  --border:    #e2e4ec;
  --border-2:  #ccd0dd;
  --text:      #14161c;
  --muted:     #5b6070;
  --faint:     #878da0;
  --accent:    #3b4ee0;
  --accent-2:  #0a7f8f;
  --accent-bg: #eef0ff;
  --diagram-bg: #f7f8fc;
  --diagram-fg: #24303f;
  --shadow: 0 1px 2px rgba(16, 20, 40, .05), 0 8px 24px -16px rgba(16, 20, 40, .18);
  --radius: 10px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Inter, Roboto, sans-serif;
  --sidebar-w: 300px;
  --measure: 74ch;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:        #0d0f14;
    --surface:   #14171f;
    --surface-2: #1a1e28;
    --border:    #252a36;
    --border-2:  #333a4a;
    --text:      #e8eaf0;
    --muted:     #9aa1b4;
    --faint:     #6f7689;
    --accent:    #8b9bff;
    --accent-2:  #4fd1c5;
    --accent-bg: #1b2040;
    --diagram-bg: #10141c;
    --diagram-fg: #c3ccdd;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px -16px rgba(0, 0, 0, .8);
  }
}

:root[data-theme="light"] {
  --bg: #fbfbfd; --surface: #ffffff; --surface-2: #f3f4f8; --border: #e2e4ec;
  --border-2: #ccd0dd; --text: #14161c; --muted: #5b6070; --faint: #878da0;
  --accent: #3b4ee0; --accent-2: #0a7f8f; --accent-bg: #eef0ff;
  --diagram-bg: #f7f8fc; --diagram-fg: #24303f;
}
:root[data-theme="dark"] {
  --bg: #0d0f14; --surface: #14171f; --surface-2: #1a1e28; --border: #252a36;
  --border-2: #333a4a; --text: #e8eaf0; --muted: #9aa1b4; --faint: #6f7689;
  --accent: #8b9bff; --accent-2: #4fd1c5; --accent-bg: #1b2040;
  --diagram-bg: #10141c; --diagram-fg: #c3ccdd;
}

/* ------------------------------------------------------------- base ------- */

*, *::before, *::after { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: 5rem; }

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

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; border-radius: 4px; }

.skip-link {
  position: absolute; left: -9999px; top: 0; z-index: 100;
  background: var(--accent); color: #fff; padding: .6rem 1rem; border-radius: 0 0 8px 0;
}
.skip-link:focus { left: 0; }

/* ----------------------------------------------------------- topbar ------- */

.topbar {
  position: sticky; top: 0; z-index: 40;
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem; padding: .7rem 1.25rem;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: .6rem; color: var(--text); font-weight: 600; }
.brand:hover { text-decoration: none; }
.brand-mark {
  font-family: var(--mono); font-size: .72rem; letter-spacing: .06em;
  background: var(--accent); color: #fff; padding: .28rem .45rem; border-radius: 6px;
}
.brand-text { font-size: .95rem; }
.topbar-actions { display: flex; gap: .5rem; }
.icon-btn {
  font: inherit; font-size: .82rem; color: var(--muted);
  background: var(--surface); border: 1px solid var(--border);
  padding: .35rem .7rem; border-radius: 7px; cursor: pointer;
}
.icon-btn:hover { color: var(--text); border-color: var(--border-2); }
#nav-toggle { display: none; }

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

.shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  gap: 2.5rem;
  max-width: 1400px;
  margin: 0 auto;
  padding: 2rem 1.5rem 4rem;
}
.main { min-width: 0; max-width: var(--measure); margin-inline: auto; width: 100%; }

/* ---------------------------------------------------------- sidebar ------- */

.sidebar {
  position: sticky; top: 4.2rem;
  align-self: start;
  max-height: calc(100vh - 6rem);
  overflow-y: auto;
  font-size: .875rem;
  padding-right: .5rem;
}
.nav-heading {
  margin: 0 0 .9rem; font-size: .68rem; letter-spacing: .14em;
  text-transform: uppercase; color: var(--faint); font-weight: 600;
}
.nav-chapter { margin-bottom: .35rem; }
.nav-chapter-title {
  display: flex; align-items: center; gap: .55rem;
  padding: .4rem .55rem; border-radius: 7px;
  color: var(--text); font-weight: 600;
}
.nav-chapter-title:hover { background: var(--surface-2); text-decoration: none; }
.nav-chapter-title .num {
  font-family: var(--mono); font-size: .7rem; color: var(--faint);
  border: 1px solid var(--border); border-radius: 5px;
  min-width: 1.4rem; text-align: center; padding: .05rem 0;
}
.nav-lessons { list-style: none; margin: .2rem 0 .6rem; padding: 0 0 0 .5rem; border-left: 1px solid var(--border); }
.nav-chapter:not(.is-open) .nav-lessons { display: none; }
.nav-lessons a {
  display: flex; gap: .55rem; padding: .3rem .55rem; border-radius: 6px;
  color: var(--muted); line-height: 1.4;
}
.nav-lessons a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav-lessons a[aria-current="page"] {
  background: var(--accent-bg); color: var(--accent); font-weight: 600;
}
.nav-lessons .num { font-family: var(--mono); font-size: .72rem; opacity: .75; min-width: 1.7rem; }

/* -------------------------------------------------------- page head ------- */

.crumbs { display: flex; gap: .45rem; font-size: .8rem; color: var(--faint); margin-bottom: 1.4rem; flex-wrap: wrap; }
.crumbs a { color: var(--muted); }

.eyebrow {
  margin: 0 0 .5rem; font-size: .7rem; letter-spacing: .13em; text-transform: uppercase;
  color: var(--accent-2); font-weight: 700;
}
h1 { font-size: clamp(1.7rem, 1.2rem + 1.6vw, 2.4rem); line-height: 1.18; letter-spacing: -.02em; margin: 0 0 1rem; }

.oneline, .example, .goal {
  position: relative; margin: 0 0 .8rem; padding: .85rem 1rem .85rem 1rem;
  background: var(--surface); border: 1px solid var(--border);
  border-left: 3px solid var(--accent); border-radius: var(--radius);
  font-size: 1.02rem;
}
.example { border-left-color: var(--accent-2); font-size: .95rem; color: var(--muted); }
.oneline .k, .example .k, .goal .k {
  display: block; font-size: .64rem; letter-spacing: .12em; text-transform: uppercase;
  color: var(--faint); font-weight: 700; margin-bottom: .25rem;
}

.lesson-meta {
  display: flex; flex-wrap: wrap; gap: .5rem; list-style: none; margin: 1.1rem 0 0; padding: 0;
}
.lesson-meta li {
  font-size: .78rem; color: var(--muted);
  background: var(--surface-2); border: 1px solid var(--border);
  padding: .3rem .6rem; border-radius: 999px;
}
.lesson-meta .k { color: var(--faint); margin-right: .35rem; font-size: .68rem; text-transform: uppercase; letter-spacing: .08em; }

/* --------------------------------------------------------------- toc ------ */

.toc {
  margin: 2rem 0; padding: .9rem 1.1rem;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius);
}
.toc-heading { margin: 0 0 .5rem; font-size: .66rem; letter-spacing: .12em; text-transform: uppercase; color: var(--faint); font-weight: 700; }
.toc ul { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: .3rem .9rem; }
.toc a { font-size: .84rem; color: var(--muted); }
.toc a:hover, .toc a.is-active { color: var(--accent); }

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

.prose { font-size: 1rem; }
.prose h2 {
  margin: 2.8rem 0 .9rem; font-size: 1.35rem; letter-spacing: -.01em;
  padding-bottom: .4rem; border-bottom: 1px solid var(--border);
}
.prose h3 { margin: 2.2rem 0 .7rem; font-size: 1.08rem; }
.prose h4 { margin: 1.6rem 0 .5rem; font-size: .95rem; color: var(--muted); }
.prose p { margin: 0 0 1rem; }
.prose ul, .prose ol { margin: 0 0 1.1rem; padding-left: 1.3rem; }
.prose li { margin-bottom: .4rem; }
.prose li::marker { color: var(--faint); }
.prose hr { border: 0; border-top: 1px solid var(--border); margin: 2.5rem 0; }
.prose strong { font-weight: 650; }

.anchor { margin-left: .4rem; color: var(--border-2); opacity: 0; font-weight: 400; }
.prose h2:hover .anchor, .prose h3:hover .anchor { opacity: 1; }

.prose :not(pre) > code {
  font-family: var(--mono); font-size: .86em;
  background: var(--surface-2); border: 1px solid var(--border);
  padding: .1em .38em; border-radius: 5px; white-space: nowrap;
}

blockquote {
  margin: 1.4rem 0; padding: .9rem 1.1rem;
  background: var(--accent-bg); border: 1px solid var(--border);
  border-left: 3px solid var(--accent); border-radius: var(--radius);
  font-size: .98rem;
}
blockquote p { margin: 0; }

/* ------------------------------------------------ code + diagram blocks --- */

.code {
  margin: 1.5rem 0; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--diagram-bg); overflow: hidden; box-shadow: var(--shadow);
}
.code figcaption {
  display: flex; align-items: center; justify-content: space-between;
  padding: .4rem .6rem .4rem .8rem;
  background: var(--surface-2); border-bottom: 1px solid var(--border);
}
.code .tag {
  font-family: var(--mono); font-size: .66rem; letter-spacing: .1em; text-transform: uppercase;
  color: var(--faint); font-weight: 600;
}
.code.is-diagram .tag { color: var(--accent-2); }
.code .copy {
  font: inherit; font-size: .7rem; color: var(--muted); cursor: pointer;
  background: transparent; border: 1px solid var(--border-2); border-radius: 5px; padding: .15rem .5rem;
}
.code .copy:hover { color: var(--text); }
.code pre {
  margin: 0; padding: 1rem 1.1rem; overflow-x: auto;
  font-family: var(--mono); font-size: .8rem; line-height: 1.5;
  color: var(--diagram-fg); tab-size: 2;
}
.code.is-diagram pre { font-size: .78rem; line-height: 1.45; letter-spacing: 0; }
.code pre code { font-family: inherit; background: none; border: 0; padding: 0; white-space: pre; }

/* ------------------------------------------------------------ tables ------ */

.table-wrap { overflow-x: auto; margin: 1.4rem 0; border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; font-size: .9rem; background: var(--surface); }
th, td { text-align: left; padding: .65rem .85rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { background: var(--surface-2); font-size: .74rem; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); font-weight: 700; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }

/* ------------------------------------------------------------- pager ------ */

.pager { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin-top: 3.5rem; }
.pager-link {
  display: flex; flex-direction: column; gap: .2rem; padding: .9rem 1rem;
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface);
  color: var(--text);
}
.pager-link:hover { border-color: var(--accent); text-decoration: none; }
.pager-link.is-empty { border: 0; background: none; }
.pager-next { text-align: right; }
.pager-label { font-size: .7rem; letter-spacing: .1em; text-transform: uppercase; color: var(--faint); font-weight: 700; }
.pager-title { font-size: .92rem; font-weight: 600; }

/* ------------------------------------------------------------- cards ------ */

.section-title { font-size: .72rem; letter-spacing: .13em; text-transform: uppercase; color: var(--faint); margin: 2.5rem 0 1rem; }
.cards { list-style: none; margin: 1.5rem 0; padding: 0; display: grid; gap: .7rem; }
.card a {
  display: flex; gap: 1rem; padding: 1rem 1.1rem;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
}
.card a:hover { border-color: var(--accent); text-decoration: none; box-shadow: var(--shadow); }
.card-num {
  font-family: var(--mono); font-size: .8rem; color: var(--accent);
  background: var(--accent-bg); border-radius: 6px; padding: .25rem .5rem; height: fit-content; min-width: 2.5rem; text-align: center;
}
.card-body { display: flex; flex-direction: column; gap: .25rem; min-width: 0; }
.card-body strong { font-size: 1rem; }
.card-line { font-size: .88rem; color: var(--muted); }
.card-meta { font-size: .74rem; color: var(--faint); font-family: var(--mono); }

/* --------------------------------------------------------- chapter/hero --- */

.chapter-head, .hero { margin-bottom: 1.5rem; }
.chapter-num {
  display: inline-block; font-family: var(--mono); font-size: .85em; color: var(--accent);
  background: var(--accent-bg); border-radius: 8px; padding: .1em .45em; margin-right: .3rem;
}
.hero-line { font-size: 1.1rem; color: var(--muted); max-width: 52ch; }
.hero-stats { display: flex; flex-wrap: wrap; gap: 1.5rem; margin-top: 1.5rem; font-size: .85rem; color: var(--muted); }
.hero-stats strong { font-size: 1.5rem; color: var(--text); display: block; font-family: var(--mono); }

.capstone {
  margin-top: 3rem; padding: 1.2rem 1.3rem;
  background: var(--surface-2); border: 1px dashed var(--border-2); border-radius: var(--radius);
}
.capstone h2 { margin: 0 0 .5rem; font-size: .72rem; letter-spacing: .13em; text-transform: uppercase; color: var(--accent-2); }
.capstone p { margin: 0; color: var(--muted); }

/* ------------------------------------------------------------ footer ------ */

.sitefoot {
  border-top: 1px solid var(--border); padding: 1.5rem;
  text-align: center; font-size: .8rem; color: var(--faint);
}
.sitefoot code { font-family: var(--mono); font-size: .95em; }

/* -------------------------------------------------------- responsive ------ */

@media (max-width: 900px) {
  #nav-toggle { display: inline-block; }
  .shell { grid-template-columns: minmax(0, 1fr); gap: 0; padding: 1.25rem 1.1rem 3rem; }
  .sidebar {
    display: none; position: static; max-height: none;
    margin-bottom: 1.5rem; padding: 1rem;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  }
  .sidebar.is-open { display: block; }
  .nav-chapter .nav-lessons { display: block; }
  .pager { grid-template-columns: 1fr; }
  .code pre { font-size: .72rem; }
}

@media print {
  .topbar, .sidebar, .pager, .toc, .copy, .sitefoot, .skip-link { display: none !important; }
  .shell { display: block; }
  .code { break-inside: avoid; }
}
