/* Algorithm guide v3 — theme tokens, typography, page layout.
   Light "Paper" is the default; dark "Slate" via [data-theme="dark"] or the OS setting. */

@font-face {
  font-family: "Source Sans 3";
  src: url("../../lib/fonts/source-sans-3-wght-normal.woff2") format("woff2");
  font-weight: 200 900; font-style: normal; font-display: swap;
}
@font-face {
  font-family: "Source Sans 3";
  src: url("../../lib/fonts/source-sans-3-wght-italic.woff2") format("woff2");
  font-weight: 200 900; font-style: italic; font-display: swap;
}
@font-face {
  font-family: "JetBrains Mono";
  src: url("../../lib/fonts/jetbrains-mono-wght-normal.woff2") format("woff2");
  font-weight: 100 800; font-style: normal; font-display: swap;
}

:root {
  color-scheme: light;
  /* surfaces */
  --page: #F1EBDF;
  --sheet: #F8F4EC;
  --sheet-2: #ECE4D5;
  --rule: #DCD2C0;
  --grid: #E7DECE;
  /* text */
  --ink: #2A2621;
  --ink-soft: #5F574C;
  --ink-faint: #6C6255;
  /* role inks (same meaning in every lesson) */
  --blue: #2D5A9E;   --blue-wash: #DDE5F1;
  --plum: #87407A;   --plum-wash: #F0DFEB;
  --amber: #87590A;  --hl: #F2D98A;
  --green: #276B40;  --green-wash: #D8EADC;
  --red: #B03A32;    --red-wash: #F4DAD6;
  --anchor: #4A4339; --anchor-wash: #E3DACB;
  --water: #BBD4E2;  --water-line: #4F84A6;
  --flag0: #EBC0B7;  --flag1: #F7F1E4;  --flag2: #B3C8E2;  --flag0-ink: #A34A3D;  --flag2-ink: #3F6596;
  /* code */
  --code-bg: #EFE8DA;
  --tok-keyword: #87407A;
  --tok-func: #2D5A9E;
  --tok-string: #276B40;
  --tok-number: #87520A;
  --tok-comment: #6B6254;
  --tok-type: #5B4C9A;
  --tok-punct: #5F574C;
  /* misc */
  --focus: #2D5A9E;
  --shadow: 0 1px 0 rgba(60, 45, 20, .06), 0 8px 24px -12px rgba(60, 45, 20, .18);
  --radius: 10px;
  --font-text: "Source Sans 3", "Source Sans Pro", -apple-system, "Segoe UI", system-ui, sans-serif;
  --font-code: "JetBrains Mono", ui-monospace, "SF Mono", Menlo, monospace;
  /* type scale 1.25 from 18px */
  --fs-0: 0.889rem;  /* 16 */
  --fs-1: 1rem;      /* 18 */
  --fs-2: 1.25rem;   /* 22.5 */
  --fs-3: 1.563rem;  /* 28 */
  --fs-4: 1.953rem;  /* 35 */
  --fs-5: 2.441rem;  /* 44 */
  --measure: 68ch;
  --wide: 1100px;
  --topbar: 56px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #201E1B; --sheet: #2A2723; --sheet-2: #33302B; --rule: #3D3833; --grid: #312D29;
    --ink: #E4DDD0; --ink-soft: #ACA395; --ink-faint: #A39A8D;
    --blue: #8DB0E8; --blue-wash: #2C3547;
    --plum: #D69BC8; --plum-wash: #44303F;
    --amber: #E2B45A; --hl: #5C4B1D;
    --green: #7FC395; --green-wash: #263A2D;
    --red: #E58C80; --red-wash: #4B2B27;
    --anchor: #CFC6B8; --anchor-wash: #3A352F;
    --water: #2F5468; --water-line: #7FB1CE;
    --flag0: #6E3A33; --flag1: #4A453D; --flag2: #34496A; --flag0-ink: #E59A8D; --flag2-ink: #9DB8E0;
    --code-bg: #262320;
    --tok-keyword: #D69BC8; --tok-func: #8DB0E8; --tok-string: #8FCB9F; --tok-number: #E2B45A;
    --tok-comment: #A0978A; --tok-type: #C2B2E6; --tok-punct: #ACA395;
    --focus: #8DB0E8;
    --shadow: 0 1px 0 rgba(0, 0, 0, .25), 0 10px 28px -14px rgba(0, 0, 0, .6);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #201E1B; --sheet: #2A2723; --sheet-2: #33302B; --rule: #3D3833; --grid: #312D29;
  --ink: #E4DDD0; --ink-soft: #ACA395; --ink-faint: #A39A8D;
  --blue: #8DB0E8; --blue-wash: #2C3547;
  --plum: #D69BC8; --plum-wash: #44303F;
  --amber: #E2B45A; --hl: #5C4B1D;
  --green: #7FC395; --green-wash: #263A2D;
  --red: #E58C80; --red-wash: #4B2B27;
  --anchor: #CFC6B8; --anchor-wash: #3A352F;
  --water: #2F5468; --water-line: #7FB1CE;
  --flag0: #6E3A33; --flag1: #4A453D; --flag2: #34496A; --flag0-ink: #E59A8D; --flag2-ink: #9DB8E0;
  --code-bg: #262320;
  --tok-keyword: #D69BC8; --tok-func: #8DB0E8; --tok-string: #8FCB9F; --tok-number: #E2B45A;
  --tok-comment: #A0978A; --tok-type: #C2B2E6; --tok-punct: #ACA395;
  --focus: #8DB0E8;
  --shadow: 0 1px 0 rgba(0, 0, 0, .25), 0 10px 28px -14px rgba(0, 0, 0, .6);
}

/* ---------- base ---------- */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html { font-size: 18px; -webkit-text-size-adjust: 100%; scroll-padding-top: calc(var(--topbar) + 16px); }
body {
  margin: 0; background: var(--page); color: var(--ink);
  font-family: var(--font-text); font-size: var(--fs-1); line-height: 1.6;
  font-feature-settings: "kern", "liga"; -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
::selection { background: var(--hl); color: var(--ink); }
a { color: var(--blue); text-decoration-thickness: 1px; text-underline-offset: 3px; }
a:hover { text-decoration-thickness: 2px; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
code, kbd, pre { font-family: var(--font-code); font-variant-ligatures: none; font-feature-settings: "liga" 0, "calt" 0; }
:not(pre) > code {
  font-size: .86em; background: var(--code-bg); border: 1px solid var(--rule);
  padding: .06em .32em; border-radius: 5px; white-space: nowrap;
}
strong { font-weight: 650; }
hr { border: 0; border-top: 1px solid var(--rule); margin: 2.5rem 0; }

h1, h2, h3, h4 { line-height: 1.2; font-weight: 700; letter-spacing: -0.012em; margin: 0; }
h1 { font-size: var(--fs-5); letter-spacing: -0.02em; }
h2 { font-size: var(--fs-4); margin-top: 3.6rem; margin-bottom: 1rem; }
h3 { font-size: var(--fs-3); margin-top: 2.6rem; margin-bottom: .6rem; font-weight: 650; }
h4 { font-size: var(--fs-2); margin-top: 1.8rem; margin-bottom: .4rem; font-weight: 650; }
p, ul, ol, blockquote { margin: 0 0 1rem; }
li { margin: .25rem 0; }
li > ul, li > ol { margin: .25rem 0; }
blockquote {
  margin-left: 0; padding: .7rem 1.1rem; border-left: 3px solid var(--rule);
  background: var(--sheet); border-radius: 0 var(--radius) var(--radius) 0; color: var(--ink);
}
blockquote p:last-child { margin-bottom: 0; }

/* ---------- page shell ---------- */
.topbar {
  position: sticky; top: 0; z-index: 40; height: var(--topbar);
  display: flex; align-items: center; gap: 12px; padding: 0 16px;
  background: color-mix(in srgb, var(--page) 88%, transparent);
  backdrop-filter: saturate(1.2) blur(8px); -webkit-backdrop-filter: saturate(1.2) blur(8px);
  border-bottom: 1px solid var(--rule);
}
.brand { display: flex; align-items: baseline; gap: 10px; color: var(--ink); text-decoration: none; font-weight: 700; white-space: nowrap; }
.brand small { font-weight: 500; color: var(--ink-soft); font-size: var(--fs-0); }
.topbar .here { color: var(--ink-soft); font-size: var(--fs-0); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.topbar .spacer { flex: 1; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 36px; min-width: 36px; padding: 0 10px; border-radius: 8px;
  border: 1px solid var(--rule); background: var(--sheet); color: var(--ink);
  font: inherit; font-size: var(--fs-0); cursor: pointer;
}
.icon-btn:hover { border-color: var(--ink-faint); }
.icon-btn svg { width: 18px; height: 18px; }
.menu-btn { display: none; }
.seg .short { display: none; }

.seg { display: inline-flex; border: 1px solid var(--rule); border-radius: 8px; overflow: hidden; background: var(--sheet); }
.seg button {
  font: inherit; font-size: var(--fs-0); border: 0; background: transparent; color: var(--ink-soft);
  padding: 6px 12px; cursor: pointer; line-height: 1.2;
}
.seg button + button { border-left: 1px solid var(--rule); }
.seg button[aria-pressed="true"] { background: var(--ink); color: var(--page); }

.shell {
  display: grid; grid-template-columns: 272px minmax(0, 1fr) 232px;
  min-height: calc(100vh - var(--topbar));
}
.rail {
  position: sticky; top: var(--topbar); align-self: start;
  height: calc(100vh - var(--topbar)); overflow-y: auto; padding: 20px 14px 40px;
  scrollbar-width: thin;
}
.rail-left { border-right: 1px solid var(--rule); }
.rail h2 { font-size: var(--fs-0); color: var(--ink-soft); font-weight: 600; margin: 0 6px 10px; letter-spacing: 0; }
.rail-group { margin-bottom: 14px; }
.rail-group > span { display: block; font-size: .78rem; color: var(--ink-faint); margin: 10px 8px 4px; }
.rail ol { list-style: none; padding: 0; margin: 0; }
.rail li { margin: 0; }
.rail-lesson {
  display: grid; grid-template-columns: 22px 1fr auto; align-items: center; gap: 6px;
  padding: 5px 8px; border-radius: 7px; color: var(--ink); text-decoration: none; font-size: .9rem; line-height: 1.3;
}
.rail-lesson:hover { background: var(--sheet); }
.rail-lesson .n { font-family: var(--font-code); font-size: .72rem; color: var(--ink-faint); }
.rail-lesson .st { font-size: .8rem; width: 1.1em; text-align: center; }
.rail-lesson .st.done { color: var(--green); }
.rail-lesson .st.doing { color: var(--amber); }
.rail-lesson[aria-current="page"] { background: var(--sheet); box-shadow: inset 3px 0 0 var(--blue); font-weight: 600; }
.rail-lesson.is-soon { color: var(--ink-faint); cursor: default; }
.rail-lesson.is-soon:hover { background: transparent; }
.rail-lesson .soon { font-size: .7rem; color: var(--ink-faint); }

.rail-right { padding-top: 32px; }
.toc a {
  display: block; padding: 4px 10px; color: var(--ink-soft); text-decoration: none;
  font-size: .86rem; line-height: 1.35; border-left: 2px solid var(--rule);
}
.toc a.lvl3 { padding-left: 22px; font-size: .82rem; }
.toc a:hover { color: var(--ink); }
.toc a.is-active { color: var(--ink); border-left-color: var(--blue); font-weight: 600; }

main { min-width: 0; }
.lesson { max-width: var(--wide); margin: 0 auto; padding: 36px 44px 140px; }
.lesson > *:not(.wide):not(.stage):not(figure):not(.table-wrap):not(.code-tabs):not(.quiz):not(section):not(.lesson-hero),
.lesson section > *:not(.stage):not(figure):not(.table-wrap):not(.code-tabs):not(.quiz):not(section):not(.wide):not(.flavors) {
  max-width: var(--measure);
}

.backdrop { display: none; }

@media (max-width: 1360px) {
  .shell { grid-template-columns: 256px minmax(0, 1fr); }
  .rail-right { display: none; }
}
@media (max-width: 1020px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .menu-btn { display: inline-flex; }
  .rail-left {
    position: fixed; top: var(--topbar); left: 0; bottom: auto; width: min(86vw, 320px); z-index: 50;
    align-self: auto; height: calc(100dvh - var(--topbar));
    background: var(--page); transform: translateX(-102%); transition: transform .22s ease;
    box-shadow: var(--shadow);
  }
  body.rail-open .rail-left { transform: none; }
  body.rail-open .backdrop { display: block; position: fixed; inset: var(--topbar) 0 0 0; background: rgba(0,0,0,.25); z-index: 45; }
  .lesson { padding: 24px 16px 120px; }
  .topbar .here { display: none; }
}
@media (max-width: 560px) {
  html { font-size: 17px; }
  :not(pre) > code { white-space: normal; overflow-wrap: anywhere; }
  h1 { font-size: var(--fs-4); }
  h2 { font-size: var(--fs-3); }
  h3 { font-size: var(--fs-2); }
  .brand small { display: none; }
  .seg .long { display: none; } .seg .short { display: inline; }
  #theme-btn span { display: none; }
  .topbar { gap: 8px; padding: 0 10px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0s !important; animation-duration: 0s !important; animation-iteration-count: 1 !important; scroll-behavior: auto !important; }
}
