:root {
  --bg: #0a0c0f;
  --bg-alt: #101318;
  --panel: #13171c;
  --panel-2: #191e25;
  --border: #242a32;
  --border-soft: #1b2027;
  --text: #e4e8ee;
  --muted: #95a0ad;
  --muted-2: #5f6b78;
  --accent: #4c8dff;
  --accent-soft: #14233d;
  --good: #4c8dff;
  --warn: #c9a227;
  --danger: #e0556f;
  --radius: 8px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", "Fira Code", Menlo, Consolas, monospace;
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --shadow: 0 10px 30px rgba(0,0,0,.45);
}
:root[data-theme="light"] {
  --bg: #f6f7f9;
  --bg-alt: #eef0f3;
  --panel: #ffffff;
  --panel-2: #f1f3f6;
  --border: #d7dbe1;
  --border-soft: #e5e8ec;
  --text: #15191f;
  --muted: #5a636e;
  --muted-2: #8b93a0;
  --accent: #1b5fd6;
  --accent-soft: #e6eefc;
  --good: #1b5fd6;
  --warn: #9a7b12;
  --danger: #cc3b55;
  --shadow: 0 8px 24px rgba(20,30,50,.10);
}

* { box-sizing: border-box; scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
body { overflow-x: clip; }   /* clip (not hidden) so it does not break position: sticky */
/* never let a long token (URL, inline code, heading) push past the right edge */
.article, .article p, .article li, .article h1, .article h2, .article h3, .article h4,
.article a, .article code, .article td, .article th,
.tleaf, .tcat-label, .ttopic > summary,
.notes-list a, .pat-list .pat-dir a, .pat-list .pat-dir > summary,
.toc a, .sr-head, .sr-sub, .sr-snip, .refcard, .cf-note, .cf-title {
  overflow-wrap: anywhere; word-break: break-word; min-width: 0;
}
::-webkit-scrollbar { height: 8px; width: 8px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 8px; }
::-webkit-scrollbar-thumb:hover { background: var(--muted-2); }
::-webkit-scrollbar-track { background: transparent; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code, kbd, pre { font-family: var(--mono); }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 50;
  display: flex; align-items: center; gap: 16px;
  padding: 8px 18px;
  background: rgba(10,12,15,.85);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
:root[data-theme="light"] .topbar { background: rgba(246,247,249,.88); }
.brand { display: flex; align-items: center; font-weight: 700; letter-spacing: .2px; font-size: 15px; }
.nav { display: flex; gap: 4px; margin-left: 6px; }
.nav a {
  padding: 6px 12px; border-radius: 8px; color: var(--muted); font-weight: 600; font-size: 14px;
}
.nav a:hover { background: var(--panel); text-decoration: none; color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.topbar .spacer { flex: 1; }
.btn {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 7px 12px; border-radius: 9px; border: 1px solid var(--border);
  background: var(--panel); color: var(--text); font: inherit; font-size: 13px; cursor: pointer;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn kbd {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 5px;
  padding: 1px 6px; font-size: 11px; color: var(--muted);
}
.search-trigger { min-width: 210px; justify-content: space-between; color: var(--muted); }

/* ---------- layout ---------- */
main { max-width: 1180px; margin: 0 auto; padding: 16px 20px 56px; }
.page-head h1 { font-size: 22px; margin: 0 0 3px; letter-spacing: -.2px; }
.page-head p { color: var(--muted); margin: 0 0 14px; max-width: 72ch; font-size: 14px; }

/* ---------- check-first ---------- */
.checkfirst { border: 1px solid var(--border); border-radius: var(--radius); background: var(--panel); margin-bottom: 18px; overflow: hidden; }
.checkfirst > summary {
  list-style: none; cursor: pointer; padding: 10px 14px;
  display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 14px;
  border-left: 3px solid var(--accent);
}
.checkfirst > summary::-webkit-details-marker { display: none; }
.checkfirst > summary .pill { margin-left: auto; font-weight: 500; font-size: 12px; color: var(--muted); }
.checkfirst .cf-body { padding: 4px 14px 14px; }
.cf-group { margin-top: 12px; }
.cf-group h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); margin: 0 0 8px; }
.cf-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(290px, 1fr)); gap: 10px; }
.cf-card { border: 1px solid var(--border-soft); border-radius: var(--radius); background: var(--bg-alt); padding: 10px 12px; }
.cf-card .cf-title { font-weight: 600; font-size: 13.5px; margin-bottom: 7px; }
.cf-card pre {
  margin: 0 0 8px; background: #05080d; border: 1px solid var(--border-soft); border-radius: 8px;
  padding: 10px 12px; font-size: 12.5px; position: relative;
  white-space: pre-wrap; word-break: break-word; overflow-wrap: anywhere;
}
:root[data-theme="light"] .cf-card pre { background: #0d1522; color: #e6edf3; }
.cf-card .cf-note { color: var(--muted); font-size: 13px; }
.cf-card .cf-ref { margin-top: 8px; font-size: 13px; }

/* ---------- browse tree ---------- */
.tree-head { margin-bottom: 14px; }
.tree-filter { width: 100%; margin-top: 10px; font: inherit; font-size: 14px; padding: 9px 12px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--panel); color: var(--text); }
.tree-filter:focus { outline: none; border-color: var(--accent); }
.tree { display: flex; flex-direction: column; gap: 6px; }
.tcat { border: 1px solid var(--border); border-radius: var(--radius); background: var(--panel); overflow: hidden; }
.tcat > summary { list-style: none; cursor: pointer; padding: 11px 14px; display: flex; align-items: baseline; gap: 10px; font-weight: 700; border-left: 3px solid var(--accent); }
.tcat > summary::-webkit-details-marker { display: none; }
.tcat[open] > summary { border-bottom: 1px solid var(--border-soft); }
.tcat-label { font-size: 15px; }
.tcat-desc { color: var(--muted); font-size: 12.5px; font-weight: 400; }
.tcount { margin-left: auto; color: var(--muted-2); font-size: 11.5px; font-weight: 600; border: 1px solid var(--border); border-radius: 999px; padding: 0 8px; }
.ttopic { border-top: 1px solid var(--border-soft); }
.ttopic:first-child { border-top: none; }
.ttopic > summary { list-style: none; cursor: pointer; padding: 8px 14px 8px 20px; display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 13.5px; color: var(--text); }
.ttopic > summary::-webkit-details-marker { display: none; }
.ttopic > summary:hover { background: var(--bg-alt); }
.ttopic > summary::before { content: "▸"; color: var(--muted-2); font-size: 11px; flex: 0 0 auto; }
.ttopic[open] > summary::before { content: "▾"; }
.ttopic-label { flex: 0 0 auto; }
.ttopic-desc { color: var(--muted); font-weight: 400; font-size: 12.5px; }
/* subtopics (4th level) */
.tsub { margin: 0 0 2px 32px; }
.tsub > summary { list-style: none; cursor: pointer; padding: 5px 12px; display: flex; align-items: center; gap: 8px; font-size: 12.5px; font-weight: 600; color: var(--muted); border-radius: 7px; }
.tsub > summary::-webkit-details-marker { display: none; }
.tsub > summary::before { content: "▸"; color: var(--muted-2); font-size: 10px; }
.tsub[open] > summary::before { content: "▾"; }
.tsub > summary:hover { background: var(--bg-alt); color: var(--text); }
.tsub[open] > summary { color: var(--accent); }
.tsub .tleaves { padding-left: 24px; }
/* PAT link row under a topic */
.tpat { display: flex; flex-wrap: wrap; align-items: center; gap: 7px; padding: 6px 14px 8px 32px; }
.tpat-lbl { font-size: 10.5px; letter-spacing: .5px; text-transform: uppercase; color: var(--muted-2); }
.tpat-chip { font-size: 12px; border: 1px solid var(--border); border-radius: 999px; padding: 2px 10px; color: var(--muted); background: var(--bg-alt); }
.tpat-chip:hover { border-color: var(--accent); color: var(--accent); text-decoration: none; }
.tleaves { display: grid; grid-template-columns: repeat(auto-fill, minmax(460px, 1fr)); gap: 2px 18px; padding: 4px 14px 12px 32px; }
.tleaf { display: block; padding: 6px 8px; border-radius: 7px; color: var(--text); font-size: 13px; border: 1px solid transparent; }
.tleaf:hover { background: var(--panel-2); border-color: var(--border); text-decoration: none; }
.tleaf-top { display: flex; align-items: baseline; gap: 8px; }
.tleaf-name { font-weight: 550; color: var(--text); }
.tleaf:hover .tleaf-name { color: var(--accent); }
.tleaf-tag { margin-left: auto; font-size: 10.5px; color: var(--muted-2); white-space: nowrap; font-family: var(--mono); flex: 0 0 auto; }
.tleaf-hint { display: block; color: var(--muted); font-size: 12px; line-height: 1.4; margin-top: 2px; }

/* ---------- category grid ---------- */
.catgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 10px; }
.catcard {
  position: relative; text-align: left; cursor: pointer;
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--panel);
  padding: 12px 13px; color: var(--text); font: inherit; transition: border-color .12s, background .12s;
  border-left: 3px solid var(--cat, var(--accent));
}
.catcard:hover { border-color: var(--accent); background: var(--panel-2); }
.catcard .k { position: absolute; top: 11px; right: 11px; font-size: 11px; color: var(--muted-2); border: 1px solid var(--border); border-radius: 5px; padding: 0 6px; }
.catcard .cat-label { font-size: 16px; font-weight: 700; }
.catcard .cat-desc { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.catcard .cat-meta { color: var(--muted-2); font-size: 11.5px; margin-top: 8px; }

/* ---------- tree walker ---------- */
.crumbs { display: flex; flex-wrap: wrap; gap: 5px; align-items: center; margin-bottom: 12px; font-size: 13px; }
.crumbs button { background: none; border: none; color: var(--accent); cursor: pointer; font: inherit; padding: 2px 4px; border-radius: 5px; }
.crumbs button:hover { background: var(--accent-soft); }
.crumbs .sep { color: var(--muted-2); }
.crumbs .cur { color: var(--muted); }
.node { border: 1px solid var(--border); border-radius: var(--radius); background: var(--panel); padding: 16px 18px; }
.node .q { font-size: 18px; font-weight: 700; margin: 0 0 5px; letter-spacing: -.1px; }
.node .hint { color: var(--muted); margin: 0 0 14px; font-size: 13.5px; }
.options { display: grid; gap: 7px; }
.opt {
  display: flex; align-items: flex-start; gap: 11px; width: 100%; text-align: left;
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg-alt);
  padding: 10px 13px; color: var(--text); font: inherit; cursor: pointer; transition: border-color .1s, background .1s;
}
.opt:hover, .opt:focus-visible { border-color: var(--accent); background: var(--panel-2); outline: none; }
.opt.active { border-color: var(--accent); background: var(--accent-soft); }
.opt .num { flex: 0 0 auto; width: 22px; height: 22px; border-radius: 5px; background: var(--panel); border: 1px solid var(--border); display: grid; place-items: center; font-size: 12px; color: var(--muted); font-weight: 700; }
.opt .lbl { font-size: 14px; font-weight: 500; }
.opt .arrow { margin-left: auto; color: var(--muted-2); align-self: center; }
.opt.leaf .arrow::after { content: "notes"; font-size: 12px; }
.opt.go .arrow::after { content: "→"; font-size: 16px; }

/* ---------- leaf panel ---------- */
.leaf {
  margin-top: 12px; border: 1px solid var(--accent); border-radius: var(--radius);
  background: var(--accent-soft); padding: 14px 16px;
}
.leaf h2 { margin: 0 0 4px; font-size: 17px; }
.leaf .summary { color: var(--text); opacity: .92; margin: 0 0 12px; font-size: 14px; }
.leaf .refs { display: grid; gap: 8px; }
.refcard {
  display: block; border: 1px solid var(--border); border-radius: var(--radius); background: var(--panel);
  padding: 10px 13px; color: var(--text);
}
.refcard:hover { border-color: var(--accent); text-decoration: none; }
.refcard .rh { font-weight: 650; font-size: 14.5px; color: var(--accent); }
.refcard .rwhy { color: var(--muted); font-size: 13px; margin-top: 2px; }
.refcard .rloc { color: var(--muted-2); font-size: 12px; margin-top: 6px; font-family: var(--mono); }
.leaf .chips { margin-top: 14px; display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.leaf .chips .lbl { color: var(--muted); font-size: 13px; }
.chip { border: 1px solid var(--border); background: var(--panel); border-radius: 999px; padding: 4px 11px; font-size: 12.5px; color: var(--text); cursor: pointer; }
.chip:hover { border-color: var(--accent); color: var(--accent); }

/* ---------- browse: notes sidebar + reader + section toc ---------- */
.bwrap { display: grid; grid-template-columns: 258px minmax(0,1fr) 220px; gap: 26px; align-items: start; }
.notes-rail { position: sticky; top: 66px; max-height: calc(100vh - 86px); overflow-y: auto; }
.notes-rail .cat-switch { width: 100%; font: inherit; font-size: 13px; padding: 9px 10px; border-radius: 9px; border: 1px solid var(--border); background: var(--panel); color: var(--text); margin-bottom: 14px; }
.notes-rail .rail-h { text-transform: uppercase; letter-spacing: .6px; color: var(--muted-2); font-size: 11px; margin: 0 0 8px; padding-left: 2px; }
.notes-list { display: flex; flex-direction: column; gap: 2px; }
.notes-list a { display: flex; align-items: center; gap: 8px; padding: 8px 11px; border-radius: 8px; color: var(--muted); font-size: 13.5px; border-left: 3px solid transparent; }
.notes-list a:hover { background: var(--panel); color: var(--text); text-decoration: none; }
.notes-list a.active { background: var(--accent-soft); color: var(--accent); border-left-color: var(--cat, var(--accent)); font-weight: 600; }
.notes-list a .fn-tag { margin-left: auto; font-size: 10px; letter-spacing: .4px; color: var(--cat, var(--accent)); border: 1px solid var(--border); border-radius: 5px; padding: 0 5px; }
.notes-list a .fn-count { margin-left: auto; font-size: 11px; color: var(--muted-2); }
.notes-list a.active .fn-count { margin-left: 8px; }
/* PAT nested dir list */
.pat-list .pat-dir > summary { list-style: none; cursor: pointer; padding: 7px 10px; border-radius: 8px; font-size: 13px; font-weight: 600; color: var(--text); display: flex; align-items: center; gap: 8px; }
.pat-list .pat-dir > summary::-webkit-details-marker { display: none; }
.pat-list .pat-dir > summary::before { content: "▸"; color: var(--muted-2); font-size: 10px; }
.pat-list .pat-dir[open] > summary::before { content: "▾"; }
.pat-list .pat-dir > summary:hover { background: var(--panel); }
.pat-list .pat-dir > summary .fn-count { margin-left: auto; font-size: 11px; color: var(--muted-2); font-weight: 500; }
.pat-list .pat-dir a { display: block; padding: 5px 10px 5px 26px; border-radius: 7px; color: var(--muted); font-size: 12.5px; border-left: 3px solid transparent; }
.pat-list .pat-dir a:hover { background: var(--panel); color: var(--text); text-decoration: none; }
.pat-list .pat-dir a.active { background: var(--accent-soft); color: var(--accent); border-left-color: var(--accent); font-weight: 600; }

.toc { position: sticky; top: 66px; max-height: calc(100vh - 86px); overflow-y: auto; border-left: 1px solid var(--border); padding-left: 14px; font-size: 13px; }
.toc .toc-h { text-transform: uppercase; letter-spacing: .6px; color: var(--muted-2); font-size: 11px; margin: 0 0 8px; }
.toc a { display: block; padding: 3px 0; color: var(--muted); border-left: 2px solid transparent; margin-left: -14px; padding-left: 12px; }
.toc a:hover { color: var(--text); text-decoration: none; }
.toc a.lvl3 { padding-left: 24px; font-size: 12.5px; }
.toc a.active { color: var(--accent); border-left-color: var(--accent); }
.browse-empty { color: var(--muted); padding: 40px 0; }
.article { min-width: 0; }
.article .doc-head { color: var(--muted); font-size: 13px; margin: -4px 0 18px; }
.article h1 { font-size: 28px; margin: 0 0 18px; letter-spacing: -.4px; }
.article h2 { font-size: 21px; margin: 32px 0 12px; padding-top: 8px; border-top: 1px solid var(--border-soft); scroll-margin-top: 72px; }
.article h3 { font-size: 17px; margin: 24px 0 10px; scroll-margin-top: 72px; }
.article h4 { font-size: 15px; margin: 18px 0 8px; color: var(--muted); scroll-margin-top: 72px; }
.article p, .article li { font-size: 15px; }
.article a { word-break: break-word; }
.article code { background: var(--panel-2); border: 1px solid var(--border-soft); border-radius: 5px; padding: .1em .4em; font-size: .88em; }
.article pre { background: #05080d; border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px; overflow-x: auto; position: relative; }
.article pre code { background: none; border: none; padding: 0; font-size: 13px; }
.article blockquote { border-left: 3px solid var(--accent); margin: 14px 0; padding: 2px 16px; color: var(--muted); background: var(--bg-alt); border-radius: 0 8px 8px 0; }
.article table { border-collapse: collapse; width: 100%; margin: 14px 0; font-size: 13.5px; display: block; overflow-x: auto; }
.article th, .article td { border: 1px solid var(--border); padding: 7px 10px; text-align: left; }
.article th { background: var(--panel-2); }
.article img { max-width: 100%; }
.article :target { animation: flash 1.6s ease; }
@keyframes flash { 0% { background: var(--accent-soft); } 100% { background: transparent; } }
.copybtn { position: absolute; top: 7px; right: 7px; font-size: 11px; padding: 3px 9px; border-radius: 6px; border: 1px solid var(--border); background: var(--panel); color: var(--muted); cursor: pointer; opacity: 0; transition: opacity .12s; }
pre:hover .copybtn { opacity: 1; }
.copybtn.ok { color: var(--good); border-color: var(--good); }

/* ---------- search overlay ---------- */
.overlay { position: fixed; inset: 0; z-index: 100; background: rgba(2,5,10,.6); backdrop-filter: blur(3px); display: none; }
.overlay.open { display: block; }
.search-box { max-width: 680px; margin: 68px auto 0; background: var(--panel); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); overflow: hidden; }
.search-box input { width: 100%; border: none; background: none; color: var(--text); font: inherit; font-size: 17px; padding: 16px 18px; outline: none; border-bottom: 1px solid var(--border); }
.search-results { max-height: 62vh; overflow-y: auto; }
.sr { display: block; padding: 9px 16px; cursor: pointer; border-bottom: 1px solid var(--border-soft); }
.sr:hover, .sr.sel { background: var(--bg-alt); }
.sr .sr-head { font-size: 14px; font-weight: 600; color: var(--text); }
.sr .sr-sub { font-size: 11.5px; color: var(--muted-2); font-family: var(--mono); margin-top: 1px; }
.sr .sr-snip { font-size: 12.5px; color: var(--muted); margin-top: 4px; line-height: 1.5; max-height: 3em; overflow: hidden; }
.sr .sr-tag { font-size: 11px; color: var(--warn); border: 1px solid var(--border); border-radius: 5px; padding: 1px 6px; float: right; margin-left: 10px; font-weight: 500; }
.sr .sr-src { font-size: 10px; color: var(--accent); border: 1px solid var(--accent); border-radius: 5px; padding: 1px 6px; float: right; margin-left: 8px; font-weight: 700; letter-spacing: .5px; }
.sr .sr-src.pat { color: var(--muted-2); border-color: var(--border); }
.sr mark { background: var(--accent-soft); color: var(--accent); border-radius: 3px; padding: 0 1px; font-weight: 600; }
.search-foot { padding: 8px 16px; color: var(--muted-2); font-size: 12px; display: flex; gap: 16px; border-top: 1px solid var(--border-soft); }
.search-foot kbd { background: var(--panel-2); border: 1px solid var(--border); border-radius: 4px; padding: 0 5px; }
.search-empty { padding: 26px 18px; color: var(--muted); text-align: center; }

@media (max-width: 1100px) {
  .bwrap { grid-template-columns: 240px minmax(0,1fr); }
  .toc { display: none; }
}
@media (max-width: 820px) {
  .bwrap { grid-template-columns: 1fr; }
  .notes-rail { position: static; max-height: none; margin-bottom: 18px; }
  .nav .lbl-long { display: none; }
  .search-trigger { min-width: 44px; }
  .search-trigger .lbl-long, .search-trigger kbd { display: none; }
}
