:root {
  --bg: #fafafa; --fg: #1f2328; --muted: #656d76; --border: #d8dee4;
  --panel: #fff; --accent: #2563eb; --ok: #1a7f37; --code-bg: #f3f4f6; --mark: #fde68a;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115; --fg: #e6e8eb; --muted: #9aa4af; --border: #2b3139;
    --panel: #161a20; --accent: #6ea8fe; --ok: #3fb950; --code-bg: #1d222a; --mark: #6b5a12;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; display: flex; min-height: 100vh; background: var(--bg); color: var(--fg);
  font: 16px/1.6 system-ui, -apple-system, "Segoe UI", sans-serif; }
button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- sidebar ---------- */
#sidebar { width: 300px; flex-shrink: 0; position: sticky; top: 0; height: 100vh; overflow-y: auto;
  padding: 20px 16px; border-right: 1px solid var(--border); background: var(--panel); }
.side-head { display: flex; align-items: center; justify-content: space-between; }
#sidebar h1 { margin: 0; font-size: 1.25rem; }
#close-btn { display: none; border: 0; background: none; font-size: 1.3rem; padding: 6px 10px; color: var(--muted); }
.sub { margin: 2px 0 12px; color: var(--muted); font-size: .85rem; }
.search input { width: 100%; padding: 9px 12px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--fg); font-size: 16px; }
.search input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
#overall { font-size: .85rem; color: var(--muted); margin: 14px 0; }
.bar { height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; margin-top: 4px; }
.bar > div { height: 100%; background: var(--ok); transition: width .3s; }
#nav a { display: block; padding: 9px 10px; border-radius: 6px; color: var(--fg); text-decoration: none; font-size: .92rem; }
#nav a:hover { background: var(--code-bg); }
#nav a.active { background: var(--code-bg); font-weight: 600; box-shadow: inset 3px 0 var(--accent); }
#nav a .count { float: right; color: var(--muted); font-size: .8rem; }
#nav a.complete .count { color: var(--ok); }

/* ---------- sign-in ---------- */
#auth { margin: -4px 0 14px; font-size: .85rem; }
#auth .hint { color: var(--muted); margin-bottom: 6px; }
#auth input { width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); color: var(--fg); font-size: 16px; }
#auth form button { margin-top: 6px; width: 100%; padding: 8px; border: 0; border-radius: 8px; background: var(--accent); color: #fff; }
#auth .err { color: #cf222e; min-height: 1.2em; margin-top: 4px; }
.linkbtn { background: none; border: 0; padding: 0; color: var(--muted); text-decoration: underline; font-size: .8rem; }

/* ---------- search results ---------- */
#results .none { color: var(--muted); font-size: .9rem; padding: 8px 2px; }
#results .meta { color: var(--muted); font-size: .8rem; margin-bottom: 6px; }
.hit { display: block; width: 100%; text-align: left; background: none; border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; margin-bottom: 8px; }
.hit:hover, .hit:focus-visible { border-color: var(--accent); outline: none; }
.hit .crumb { font-size: .75rem; color: var(--muted); }
.hit .lang { display: inline-block; font-size: .65rem; text-transform: uppercase; letter-spacing: .05em;
  background: var(--accent); color: #fff; border-radius: 4px; padding: 0 5px; margin-right: 6px; }
.hit .snip { font-size: .85rem; margin-top: 3px; overflow-wrap: anywhere; }
.hit pre.snip { margin: 4px 0 0; padding: 6px 8px; font: .75rem/1.4 ui-monospace, Consolas, monospace;
  white-space: pre-wrap; border: 0; background: var(--code-bg); border-radius: 6px; overflow: hidden; }
mark { background: var(--mark); color: inherit; border-radius: 2px; padding: 0 1px; }
.flash { animation: flash 1.8s ease-out; }
@keyframes flash { 0%, 40% { background: var(--mark); } 100% { background: transparent; } }

/* ---------- content ---------- */
main { flex: 1; min-width: 0; padding: 32px 40px 80px; }
article { max-width: 920px; margin: 0 auto; }
article h1 { font-size: 2rem; margin-top: 0; line-height: 1.25; }
article h2 { margin-top: 2.2rem; padding-top: 1rem; border-top: 1px solid var(--border); display: flex;
  align-items: center; gap: 10px; flex-wrap: wrap; scroll-margin-top: 70px; }
article h2 .title { flex: 1 1 auto; min-width: 12ch; }
article h3 { margin-top: 1.6rem; scroll-margin-top: 70px; }
.pill { font-size: .8rem; font-weight: 500; color: var(--muted); display: inline-flex; align-items: center;
  gap: 6px; padding: 5px 11px; border: 1px solid var(--border); border-radius: 999px; user-select: none;
  background: none; line-height: 1.3; }
.understood { cursor: pointer; }
.understood input { accent-color: var(--ok); width: 17px; height: 17px; cursor: pointer; }
.understood.on { color: var(--ok); border-color: var(--ok); }
.note-btn.has { color: var(--accent); border-color: var(--accent); }
.note-btn[aria-expanded="true"] { background: var(--code-bg); }
.note-panel { margin: -4px 0 14px; }
.note-panel textarea { width: 100%; min-height: 90px; resize: vertical; padding: 10px 12px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--panel); color: var(--fg); font: 16px/1.5 system-ui, sans-serif; }
.note-panel textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.note-panel .status { font-size: .75rem; color: var(--muted); height: 1.2em; }
table { border-collapse: collapse; width: 100%; margin: 1rem 0; font-size: .9rem; display: block; overflow-x: auto; }
th, td { border: 1px solid var(--border); padding: 7px 10px; text-align: left; vertical-align: top; }
th { background: var(--code-bg); }
code { background: var(--code-bg); padding: 1px 5px; border-radius: 4px; font: .88em ui-monospace, Consolas, monospace; }
pre { background: var(--code-bg); padding: 14px; border-radius: 8px; overflow-x: auto; border: 1px solid var(--border); position: relative; }
pre code { background: none; padding: 0; font-size: .85rem; line-height: 1.45; }
pre[data-lang]::before { content: attr(data-lang); position: absolute; top: 4px; right: 10px; font-size: .7rem;
  text-transform: uppercase; color: var(--muted); letter-spacing: .05em; }
blockquote { margin: 1rem 0; padding: .1rem 1rem; border-left: 4px solid var(--accent); background: var(--code-bg); border-radius: 0 6px 6px 0; }
a { color: var(--accent); }
ul.tasks { list-style: none; padding-left: 4px; }
.pager { display: flex; justify-content: space-between; gap: 10px; margin-top: 3rem; }
.pager a { padding: 10px 14px; border: 1px solid var(--border); border-radius: 8px; text-decoration: none; font-size: .9rem; }

/* ---------- mobile ---------- */
#topbar, #backdrop { display: none; }
@media (max-width: 800px) {
  body { flex-direction: column; font-size: 16px; }
  #topbar { display: flex; align-items: center; gap: 12px; position: sticky; top: 0; z-index: 20;
    padding: 8px 12px; padding-top: calc(8px + env(safe-area-inset-top)); background: var(--panel);
    border-bottom: 1px solid var(--border); }
  #topbar .brand { font-weight: 600; flex: 1; }
  #topbar-progress { font-size: .8rem; color: var(--muted); }
  #menu-btn { border: 1px solid var(--border); background: none; border-radius: 8px; font-size: 1.2rem;
    width: 42px; height: 40px; }
  #sidebar { position: fixed; z-index: 40; top: 0; left: 0; bottom: 0; width: min(86vw, 340px); height: auto;
    transform: translateX(-100%); transition: transform .22s ease; border-right: 1px solid var(--border);
    padding-top: calc(16px + env(safe-area-inset-top)); box-shadow: none; }
  body.menu-open #sidebar { transform: none; box-shadow: 4px 0 24px rgba(0, 0, 0, .3); }
  body.menu-open { overflow: hidden; }
  #close-btn { display: block; }
  #backdrop { position: fixed; inset: 0; z-index: 30; background: rgba(0, 0, 0, .45); }
  body.menu-open #backdrop { display: block; }
  main { padding: 18px 14px 60px; }
  article h1 { font-size: 1.5rem; }
  article h2 { font-size: 1.25rem; }
  article h2 .title { flex-basis: 100%; }
  pre { padding: 10px; margin-left: -4px; margin-right: -4px; }
  pre code { font-size: .8rem; }
  .pager { flex-direction: column; }
  .pager span { display: block; }
  .pager a { display: block; text-align: center; }
}
@media (prefers-reduced-motion: reduce) { #sidebar, .bar > div { transition: none; } .flash { animation: none; } }
#g-btn { margin: 8px 0; min-height: 44px; }
#auth form { margin-top: 8px; }

/* ---------- pre-rendered pages ---------- */
.brand { font-size: 1.25rem; font-weight: 700; color: var(--fg); text-decoration: none; }
#topbar .brand { font-size: 1rem; font-weight: 600; flex: 1; }
.lede { font-size: 1.15rem; color: var(--muted); margin-top: -.5rem; }
.muted { color: var(--muted); font-size: .92rem; }
.chapter-list { padding-left: 1.2rem; }
.chapter-list li { margin-bottom: .8rem; }
article h2.plain { display: block; }
.cta { display: inline-block; padding: 10px 16px; border-radius: 8px; background: var(--accent); color: #fff; text-decoration: none; font-weight: 600; }
#auth .who { margin-bottom: 4px; }
#auth { margin-top: 0; }
#nav a { display: block; }

.side-foot { margin-top: 24px; padding-top: 12px; border-top: 1px solid var(--border); font-size: .8rem; color: var(--muted); }
.side-foot a { color: var(--muted); }
