:root {
  --bg: #ffffff;
  --sidebar: #f9f9f9;
  --bubble: #e9e9e980;
  --text: #0d0d0d;
  --text2: #5d5d5d;
  --border: #0000001a;
  --composer: #ffffff;
  --composer-shadow: 0 0 0 1px #0000000f, 0 4px 16px #0000000d;
  --hover: #ececec;
  --active: #e3e3e3;
  --send-bg: #000000;
  --send-fg: #ffffff;
  --send-disabled: #d7d7d7;
  --inline-code: #ececec;
  --error-bg: #fef2f2;
  --error-border: #fca5a5;
  --error-text: #b91c1c;
  --chip-border: #0000001f;
  --amber-bg: #fffbeb;
  --amber-border: #f59e0b66;
  --amber-text: #92400e;
  --green-bg: #f0fdf4;
  --green-border: #22c55e66;
  --green-text: #15803d;
  --grey-bg: #f4f4f5;
  --grey-border: #0000001f;
  --grey-text: #52525b;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #212121;
    --sidebar: #181818;
    --bubble: #303030;
    --text: #ececec;
    --text2: #b4b4b4;
    --border: #ffffff1a;
    --composer: #303030;
    --composer-shadow: 0 0 0 1px #ffffff0d;
    --hover: #ffffff12;
    --active: #ffffff1c;
    --send-bg: #ffffff;
    --send-fg: #000000;
    --send-disabled: #676767;
    --inline-code: #ffffff1a;
    --error-bg: #ef44441a;
    --error-border: #ef444466;
    --error-text: #fca5a5;
    --chip-border: #ffffff26;
    --amber-bg: #f59e0b1f;
    --amber-border: #f59e0b66;
    --amber-text: #fcd34d;
    --green-bg: #22c55e1a;
    --green-border: #22c55e66;
    --green-text: #86efac;
    --grey-bg: #ffffff12;
    --grey-border: #ffffff26;
    --grey-text: #b4b4b4;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, -apple-system, system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
a { color: inherit; }

.app { display: flex; height: 100dvh; width: 100%; overflow: hidden; }

/* ---------- left rail ---------- */
.rail {
  width: 280px; flex: 0 0 280px;
  background: var(--sidebar);
  display: flex; flex-direction: column;
  padding: 8px; overflow: hidden; z-index: 30;
}
.rail-top { display: flex; align-items: center; justify-content: space-between; height: 44px; padding: 0 4px 0 10px; }
.brand { font-weight: 600; font-size: 16px; text-decoration: none; }
.icon-btn {
  width: 36px; height: 36px; border-radius: 8px;
  display: inline-flex; align-items: center; justify-content: center; color: var(--text2);
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.only-mobile { display: none; }

.rail-search { padding: 6px 4px 10px; }
.rail-search input {
  width: 100%; height: 38px; padding: 0 14px;
  border: 1px solid var(--chip-border); border-radius: 19px;
  background: var(--composer); color: var(--text); font: inherit; font-size: 14px; outline: none;
}
.rail-search input:focus { border-color: var(--text2); }

.tree { flex: 1; overflow-y: auto; overflow-x: hidden; padding-bottom: 8px; }
.tree-empty { color: var(--text2); font-size: 13px; padding: 10px; }
.unit-block { margin-bottom: 8px; }
.tree-row {
  display: flex; align-items: center; gap: 8px; width: 100%;
  padding: 7px 10px; border-radius: 10px; font-size: 14px; text-align: left;
  color: inherit; text-decoration: none;
}
.tree-row:hover { background: var(--hover); }
.tree-row.active { background: var(--active); }
.tree-row .t { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-row.unit { font-weight: 600; }
.tree-row.topic { padding-left: 24px; color: var(--text2); font-size: 13.5px; }
.tree-row.topic.active { color: var(--text); }
.tree-row.sheet { padding-left: 24px; font-size: 13px; color: var(--text2); }
.tree-sub { font-size: 12px; color: var(--text2); padding: 2px 10px 6px 24px; }
.dot { width: 6px; height: 6px; border-radius: 50%; flex: 0 0 6px; }
.dot.auto { background: #f59e0b; }
.dot.draft { background: #9ca3af; }
.dot.stub { background: #c4b5fd; }
.dot.ok { background: #22c55e; }

.rail-foot { border-top: 1px solid var(--border); padding: 10px; font-size: 13px; color: var(--text2); }
.rail-foot a { text-decoration: none; }
.rail-foot a:hover { color: var(--text); }

/* ---------- main ---------- */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; background: var(--bg); }
.topbar {
  height: 52px; flex: 0 0 52px; display: flex; align-items: center; gap: 6px;
  padding: 0 12px; border-bottom: 1px solid var(--border);
}
.crumbs { font-size: 13px; color: var(--text2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.crumbs a { text-decoration: none; }
.crumbs a:hover { color: var(--text); }
.crumbs .sep { padding: 0 6px; opacity: .6; }

.pane { flex: 1; overflow-y: auto; padding: 28px 16px 80px; outline: none; }
.card-wrap { max-width: 768px; margin: 0 auto; }

.kind-label { font-size: 12px; letter-spacing: .06em; text-transform: uppercase; color: var(--text2); }
.card-title { font-size: 28px; font-weight: 600; margin: 6px 0 10px; line-height: 1.25; }
.card-desc { font-size: 17px; color: var(--text2); margin: 0 0 18px; line-height: 1.55; }

.badge {
  display: inline-flex; align-items: center; gap: 8px;
  font-size: 13px; line-height: 1.4; padding: 8px 12px; border-radius: 10px;
  border: 1px solid var(--grey-border); background: var(--grey-bg); color: var(--grey-text);
  margin: 0 0 20px;
}
.badge.auto { border-color: var(--amber-border); background: var(--amber-bg); color: var(--amber-text); }
.badge.ok { border-color: var(--green-border); background: var(--green-bg); color: var(--green-text); }
.badge.stub { border-color: #c4b5fd; background: #f5f3ff; color: #5b21b6; }
.banner-warn { border: 1px solid var(--amber-border); background: var(--amber-bg); color: var(--amber-text); border-radius: 10px; padding: 10px 12px; margin: 0 0 14px; font-size: 13px; line-height: 1.5; }
.banner-warn a { color: inherit; font-weight: 600; }
@media (prefers-color-scheme: dark) { .badge.stub { border-color: #4c1d95; background: #2e1065; color: #ddd6fe; } }

.section { margin: 26px 0 0; }
.section > h2 {
  font-size: 13px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--text2); font-weight: 600; margin: 0 0 10px;
}
.section > p { margin: 0 0 10px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--chip-border); border-radius: 999px;
  padding: 6px 13px; font-size: 13.5px; text-decoration: none; color: inherit;
  background: none;
}
.chip:hover { background: var(--hover); }
.chip-note { font-size: 12.5px; color: var(--text2); margin: 0 0 8px; }

.list { list-style: none; margin: 0; padding: 0; }
.list > li {
  border: 1px solid var(--border); border-radius: 12px;
  padding: 12px 14px; margin-bottom: 10px; background: var(--bubble);
}
.list .li-title { font-weight: 600; font-size: 15px; }
.list .li-sub { color: var(--text2); font-size: 13.5px; margin-top: 4px; line-height: 1.5; }
.li-meta { font-size: 12px; color: var(--text2); margin-top: 6px; }
.tag {
  display: inline-block; font-size: 11px; text-transform: uppercase; letter-spacing: .04em;
  border: 1px solid var(--chip-border); border-radius: 6px; padding: 1px 6px; margin-right: 6px; color: var(--text2);
}

.eq { border: 1px solid var(--border); border-radius: 12px; padding: 14px; margin-bottom: 12px; }
.eq-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.eq-title { font-weight: 600; font-size: 15px; }
.eq-id { font-size: 12px; color: var(--text2); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.eq-math { margin: 10px 0 6px; overflow-x: auto; }
.eq-math .katex { font-size: 1.15em; }
.assump { margin-top: 8px; font-size: 13.5px; }
.assump summary { cursor: pointer; color: var(--text2); font-size: 13px; }
.assump ul { margin: 8px 0 0; padding-left: 1.3em; }
.assump li { margin: 3px 0; }
.assump .bad { color: var(--error-text); }
.assump-title { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--text2); margin: 10px 0 4px; }

.src { list-style: none; margin: 0; padding: 0; font-size: 14px; }
.src li { padding: 7px 0; border-bottom: 1px solid var(--border); }
.src li:last-child { border-bottom: 0; }
.src a { color: #3b82f6; text-decoration: none; }
.src a:hover { text-decoration: underline; }
.src .pages { color: var(--text2); margin-left: 6px; font-size: 13px; }

.notice {
  border: 1px solid var(--border); border-radius: 12px; padding: 18px;
  color: var(--text2); font-size: 15px; line-height: 1.6;
}
.notice h1 { font-size: 20px; color: var(--text); margin: 0 0 10px; }
.notice code { background: var(--inline-code); padding: 2px 5px; border-radius: 6px; font-size: .9em; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.notice a { color: #3b82f6; }
.err { border-color: var(--error-border); background: var(--error-bg); color: var(--error-text); }

.results-head { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--text2); padding: 4px 10px 6px; }

/* markdown, matched to the chat app */
.md { line-height: 1.65; overflow-wrap: break-word; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 14px; }
.md h1, .md h2, .md h3, .md h4 { line-height: 1.3; margin: 22px 0 10px; font-weight: 600; }
.md h1 { font-size: 1.5em; } .md h2 { font-size: 1.28em; } .md h3 { font-size: 1.12em; } .md h4 { font-size: 1em; }
.md ul, .md ol { padding-left: 1.6em; margin: 0 0 14px; }
.md li { margin: 4px 0; }
.md a { color: #3b82f6; }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 24px 0; }
.md blockquote { margin: 0 0 14px; padding: 2px 0 2px 14px; border-left: 3px solid var(--border); color: var(--text2); }
.md code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .875em; }
.md :not(pre) > code { background: var(--inline-code); padding: 2px 5px; border-radius: 6px; }
.md pre { background: var(--inline-code); padding: 12px; border-radius: 10px; overflow-x: auto; }
.md table { border-collapse: collapse; margin: 0 0 14px; display: block; overflow-x: auto; max-width: 100%; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 12px; text-align: left; }
.md th { font-weight: 600; background: var(--hover); }
.md .katex-display { overflow-x: auto; overflow-y: hidden; padding: 4px 0; margin: 10px 0 14px; }
.md .katex { font-size: 1.06em; }

/* ---------- mobile ---------- */
.backdrop { display: none; }
@media (max-width: 860px) {
  .only-mobile { display: inline-flex; }
  .rail {
    position: fixed; top: 0; bottom: 0; left: 0;
    transform: translateX(-100%); transition: transform .2s ease;
    box-shadow: 0 0 40px #00000026;
  }
  .app.rail-open .rail { transform: translateX(0); }
  .app.rail-open .backdrop { display: block; position: fixed; inset: 0; background: #00000059; z-index: 20; }
  .pane { padding: 20px 16px 72px; }
  .card-title { font-size: 23px; }
}

/* ---------- passcode gate (same as the chat app) ---------- */
.gate {
  position: fixed; inset: 0; z-index: 100;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
}
.gate[hidden] { display: none; }
.gate-card {
  width: 100%; max-width: 360px;
  display: flex; flex-direction: column; align-items: stretch; text-align: center;
}
.gate-title { font-size: 32px; font-weight: 600; margin: 0 0 8px; }
.gate-sub { color: var(--text2); margin: 0 0 24px; font-size: 16px; }
.gate-input {
  width: 100%; height: 52px; padding: 0 20px;
  border: 1px solid var(--chip-border); border-radius: 26px;
  background: var(--composer); color: var(--text);
  font: inherit; font-size: 16px; outline: none;
}
.gate-input:focus { border-color: var(--text2); }
.gate-input.invalid { border-color: var(--error-text); }
.gate-error { color: var(--error-text); font-size: 14px; text-align: left; margin: 8px 20px 0; }
.gate-error[hidden] { display: none; }
.gate-btn {
  width: 100%; height: 52px; margin-top: 16px; border-radius: 26px;
  background: var(--send-bg); color: var(--send-fg);
  font-weight: 500; font-size: 16px;
}
.gate-btn:disabled { background: var(--send-disabled); cursor: default; }
