:root {
  --bg: #fbfaf7;
  --surface: #ffffff;
  --sidebar: #f3f1ec;
  --text: #1f2328;
  --muted: #5d636b;
  --rule: #e2ded6;
  --accent: #0f6b62;
  --accent-soft: #e3f1ee;
  --accent-text: #0b5149;
  --quote: #f6f4ef;
  --hint: #fff6df;
  --hint-rule: #e6c46a;
  --answer: #eaf3f1;
  --code: #f1efea;
  --sans: "Source Sans 3", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --serif: "Source Serif 4", Georgia, "Times New Roman", serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --topbar: 56px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15181b;
    --surface: #1c2024;
    --sidebar: #1a1d21;
    --text: #e6e3dc;
    --muted: #a3a8ae;
    --rule: #30353b;
    --accent: #5cc2b3;
    --accent-soft: #1d3532;
    --accent-text: #8adccf;
    --quote: #1f2428;
    --hint: #2d2818;
    --hint-rule: #8f7630;
    --answer: #1b2d2a;
    --code: #23272c;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { scroll-padding-top: calc(var(--topbar) + 16px); -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 18px/1.6 var(--sans);
}

a { color: var(--accent-text); text-decoration-thickness: 1px; text-underline-offset: 2px; }
a:hover { text-decoration-thickness: 2px; }
a.ext::after { content: "\2197"; font-size: 0.75em; margin-left: 0.15em; vertical-align: 0.15em; text-decoration: none; display: inline-block; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 2px; }

.skip { position: absolute; left: -999px; top: 8px; background: var(--surface); padding: 8px 12px; z-index: 20; }
.skip:focus { left: 8px; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  height: var(--topbar);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 24px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--rule);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; color: var(--text); text-decoration: none; font-size: 17px; }
.brand-mark { width: 22px; height: 22px; border-radius: 6px; background: var(--accent); box-shadow: inset -6px -6px 0 color-mix(in srgb, var(--accent) 60%, #000); }
.brand-sub { font-weight: 400; color: var(--muted); }
.menu {
  display: none;
  font: inherit; font-size: 15px; font-weight: 600;
  color: var(--text); background: var(--surface);
  border: 1px solid var(--rule); border-radius: 8px; padding: 6px 12px; cursor: pointer;
}

/* Layout */
.layout { display: grid; grid-template-columns: 280px minmax(0, 1fr); min-height: calc(100vh - var(--topbar)); }
.sidebar {
  position: sticky; top: var(--topbar); align-self: start;
  height: calc(100vh - var(--topbar)); overflow-y: auto;
  background: var(--sidebar); border-right: 1px solid var(--rule);
  padding: 20px 14px 40px;
}
main { padding: 40px 48px 80px; }
article { max-width: 46rem; margin: 0 auto; }
.pager { max-width: 46rem; margin: 0 auto; }

/* Sidebar contents */
.toc ul { list-style: none; margin: 0 0 8px; padding: 0; }
.toc-group { margin: 18px 10px 6px; font-size: 12px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
.toc a {
  display: flex; gap: 10px; align-items: baseline;
  padding: 6px 10px; border-radius: 8px;
  color: var(--text); text-decoration: none; font-size: 15.5px; line-height: 1.35;
}
.toc a:hover { background: color-mix(in srgb, var(--accent-soft) 70%, transparent); }
.toc a[aria-current="page"] { background: var(--accent-soft); color: var(--accent-text); font-weight: 600; }
.toc-num {
  flex: none; width: 22px; height: 22px; border-radius: 50%;
  display: inline-grid; place-items: center;
  font-size: 12px; font-weight: 700; color: var(--muted);
  border: 1px solid var(--rule); background: var(--surface);
  transform: translateY(-1px);
}
.toc a[aria-current="page"] .toc-num { background: var(--accent); border-color: var(--accent); color: var(--surface); }

/* Typography */
.page-head { margin-bottom: 28px; }
.eyebrow { margin: 0 0 4px; font-size: 14px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: var(--accent-text); }
h1, h2, h3, h4 { font-family: var(--serif); line-height: 1.25; text-wrap: balance; }
h1 { font-size: 2.3rem; margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 1.5rem; margin: 2.4em 0 0.6em; padding-top: 0.9em; border-top: 1px solid var(--rule); }
h3 { font-size: 1.2rem; margin: 1.8em 0 0.5em; }
h4 { font-size: 1.05rem; margin: 1.5em 0 0.4em; }
p, ul, ol { margin: 0 0 1em; }
li { margin: 0.3em 0; }
li > p { margin-bottom: 0.5em; }
strong { font-weight: 700; }
hr { border: 0; border-top: 1px solid var(--rule); margin: 2em 0; }

.lead p { font-size: 1.2rem; line-height: 1.5; }
.lab-meta p {
  font-size: 15px; color: var(--muted);
  padding: 10px 14px; border-radius: 10px;
  background: var(--surface); border: 1px solid var(--rule);
}

/* Source passages and answer cards */
blockquote {
  margin: 1em 0 1.4em; padding: 14px 18px;
  background: var(--quote); border-left: 4px solid var(--accent); border-radius: 0 10px 10px 0;
}
blockquote > :last-child { margin-bottom: 0; }

/* Tables */
.table-wrap { overflow-x: auto; margin: 1.2em 0 1.6em; border: 1px solid var(--rule); border-radius: 10px; background: var(--surface); }
table { border-collapse: collapse; width: 100%; font-size: 16px; line-height: 1.45; }
th, td { padding: 9px 12px; border-bottom: 1px solid var(--rule); vertical-align: top; text-align: left; }
th { background: var(--sidebar); font-weight: 700; }
tr:last-child td { border-bottom: 0; }
td:not(:last-child), th:not(:last-child) { border-right: 1px solid var(--rule); }
li .table-wrap { margin: 0.8em 0; }

/* Hints and worked answers */
details { margin: 1.2em 0; border: 1px solid var(--rule); border-radius: 10px; background: var(--surface); }
details > summary {
  cursor: pointer; list-style: none;
  padding: 12px 16px; font-weight: 600;
  display: flex; align-items: center; gap: 10px;
}
details > summary::-webkit-details-marker { display: none; }
details > summary::before {
  content: ""; flex: none; width: 8px; height: 8px;
  border-right: 2px solid currentColor; border-bottom: 2px solid currentColor;
  transform: rotate(-45deg); transition: transform 0.15s;
}
details[open] > summary::before { transform: rotate(45deg); }
details > :not(summary) { margin-left: 16px; margin-right: 16px; }
details > :last-child { margin-bottom: 16px; }
details[open] > summary { border-bottom: 1px solid var(--rule); margin-bottom: 14px; }
details.hint { background: var(--hint); border-color: var(--hint-rule); }
details.hint[open] > summary { border-color: var(--hint-rule); }
details.answer { background: var(--answer); border-color: color-mix(in srgb, var(--accent) 40%, var(--rule)); }
details.answer > summary { color: var(--accent-text); }

/* Code */
code { font-family: var(--mono); font-size: 0.88em; background: var(--code); padding: 0.1em 0.35em; border-radius: 4px; }
pre { background: var(--code); padding: 14px 16px; border-radius: 10px; overflow-x: auto; line-height: 1.45; }
pre code { background: none; padding: 0; font-size: 14.5px; }

/* Workbook callout */
.callout {
  margin: 0 0 28px; padding: 16px 18px; border-radius: 12px;
  background: var(--accent-soft); border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
}
.callout p { margin: 0 0 10px; }
.callout > :last-child { margin-bottom: 0; }
.button {
  display: inline-block; padding: 9px 16px; border-radius: 8px;
  background: var(--accent); color: var(--surface); font-weight: 700; text-decoration: none;
}
.button:hover { filter: brightness(1.08); }

/* Previous / next */
.pager { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 56px; padding-top: 24px; border-top: 1px solid var(--rule); }
.pager a {
  display: block; padding: 14px 16px; border: 1px solid var(--rule); border-radius: 10px;
  background: var(--surface); text-decoration: none; color: var(--text);
}
.pager a:hover { border-color: var(--accent); }
.pager small { display: block; font-size: 13px; color: var(--muted); }
.pager span { font-weight: 600; color: var(--accent-text); }
.pager .next { text-align: right; grid-column: 2; }

/* Small screens */
@media (max-width: 900px) {
  body { font-size: 17px; }
  .topbar { padding: 0 16px; }
  .brand-sub { display: none; }
  .menu { display: inline-block; }
  .layout { display: block; }
  .sidebar {
    display: none; position: fixed; inset: var(--topbar) 0 0 0; z-index: 9;
    height: auto; border-right: 0;
  }
  .sidebar.open { display: block; }
  main { padding: 24px 16px 64px; }
  h1 { font-size: 1.85rem; }
  .pager { grid-template-columns: 1fr; }
  .pager .next { grid-column: 1; }
}

@media print {
  .topbar, .sidebar, .pager, .skip { display: none; }
  .layout { display: block; }
  main { padding: 0; }
  body { background: #fff; color: #000; font-size: 12pt; }
  a { color: inherit; }
  .table-wrap { overflow: visible; }
}
