:root {
  --bg: #f8fafc; --fg: #0f172a; --muted: #64748b; --panel: #ffffff; --border: #e2e8f0;
  --primary: #1e3a8a; --primary-fg: #ffffff; --accent: #2563eb;
  --vault-bg: #ffffff; --vault-border: #cbd5e1;
  --ai-bg: #faf5ff; --ai-border: #a855f7; --ai-fg: #6b21a8;
  --ok: #16a34a; --bad: #dc2626; --warn: #d97706;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a; --fg: #e2e8f0; --muted: #94a3b8; --panel: #1e293b; --border: #334155;
    --primary: #3b82f6; --primary-fg: #ffffff; --accent: #60a5fa;
    --vault-bg: #1e293b; --vault-border: #475569;
    --ai-bg: #2e1065; --ai-border: #a855f7; --ai-fg: #d8b4fe;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--fg); font: 16px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; }
main { max-width: 860px; margin: 0 auto; padding: 1rem; padding-bottom: calc(2rem + env(safe-area-inset-bottom)); }
body.admin main { max-width: 1100px; }
main.narrow { max-width: 420px; }
h1 { font-size: 1.15rem; margin: 0; }
h2 { font-size: 1.05rem; margin: 0 0 .75rem; }
h3 { font-size: .95rem; margin: 1.25rem 0 .25rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.error { color: var(--bad); }
.spacer { flex: 1; }

.bar { display: flex; align-items: center; gap: .75rem; padding: .6rem 1rem; padding-top: calc(.6rem + env(safe-area-inset-top));
  background: var(--primary); color: var(--primary-fg); position: sticky; top: 0; z-index: 5; }
.bar a, .bar .muted { color: inherit; opacity: .85; }
.status { font-size: .75rem; padding: .1rem .5rem; border-radius: 999px; background: rgba(255,255,255,.2); }
.status[data-state="online"] { background: var(--ok); }

.panel { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 1rem; margin-bottom: 1rem; }
.row { display: flex; gap: .5rem; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.row.end { justify-content: flex-end; margin-top: 1rem; }
.topic-row { margin-top: .75rem; }

button, select, input, textarea { font: inherit; color: inherit; }
button { background: var(--primary); color: var(--primary-fg); border: 0; border-radius: 8px; padding: .55rem .9rem; cursor: pointer; min-height: 40px; }
button.secondary { background: transparent; color: var(--accent); border: 1px solid var(--accent); }
button.ghost { background: transparent; color: inherit; border: 1px solid transparent; }
button:disabled { opacity: .5; cursor: default; }
button.small { min-height: 0; padding: .2rem .5rem; font-size: .85rem; }
select, input[type="text"], input[type="password"], input[type="number"], input:not([type]), textarea {
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: .5rem .6rem; max-width: 100%; }
textarea { width: 100%; resize: vertical; }
select { min-height: 40px; }

.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; align-items: center; gap: .5rem; padding: .5rem 0; border-top: 1px solid var(--border); }
.list li:first-child { border-top: 0; }
.list .grow { flex: 1; min-width: 0; }

.card .meta { display: flex; flex-wrap: wrap; gap: .4rem; margin-bottom: .6rem; }
.chip { font-size: .75rem; padding: .1rem .55rem; border-radius: 999px; border: 1px solid var(--border); color: var(--muted); }
.chip.ai { border-color: var(--ai-border); color: var(--ai-fg); }
.chip.warn { border-color: var(--warn); color: var(--warn); }
.prompt { font-size: 1.15rem; margin-bottom: .75rem; }
.card textarea { min-height: 110px; margin-bottom: .75rem; }
.rating { display: grid; grid-template-columns: repeat(4, 1fr); gap: .5rem; margin-top: 1rem; }
.rating button[data-rating="0"] { background: var(--bad); }
.rating button[data-rating="1"] { background: var(--warn); }
.rating button[data-rating="2"] { background: var(--accent); }
.rating button[data-rating="3"] { background: var(--ok); }

.section { border-radius: 10px; padding: .75rem 1rem; margin-top: 1rem; }
.section > header { display: flex; align-items: center; gap: .5rem; margin-bottom: .4rem; font-weight: 600; }
.section.vault { background: var(--vault-bg); border: 1px solid var(--vault-border); }
.section.ai { background: var(--ai-bg); border: 2px dashed var(--ai-border); }
.section.ai > header { color: var(--ai-fg); }
.section details > summary { cursor: pointer; font-weight: 600; margin-bottom: .4rem; }
.section .note { font-size: .75rem; color: var(--muted); margin-top: .5rem; }
.loading { color: var(--muted); font-style: italic; }

.recall { white-space: pre-wrap; overflow-wrap: anywhere; }
.prose { overflow-wrap: anywhere; }
.prose > :first-child { margin-top: 0; }
.prose > :last-child { margin-bottom: 0; }
.prose h1, .prose h2, .prose h3, .prose h4, .prose h5, .prose h6 { line-height: 1.25; margin: 1.1em 0 .4em; }
.prose h1 { font-size: 1.5rem; }
.prose h2 { font-size: 1.3rem; }
.prose h3 { font-size: 1.15rem; }
.prose h4, .prose h5, .prose h6 { font-size: 1rem; }
.prose p { margin: .6em 0; }
.prose ul, .prose ol { padding-left: 1.5rem; margin: .6em 0; }
.prose li > p { margin: .2em 0; }
.prose ul.contains-task-list { list-style: none; padding-left: .25rem; }
.prose .task-list-item-checkbox { margin-right: .4rem; }
.prose blockquote { margin: .8em 0; padding: .1em 1rem; border-left: 4px solid var(--border); color: var(--muted); }
.prose hr { border: 0; border-top: 1px solid var(--border); margin: 1.2em 0; }
.prose a { color: var(--accent); }
.prose img { max-width: 100%; height: auto; border-radius: 6px; vertical-align: middle; }
.prose .missing-image { color: var(--muted); font-style: italic; }
.prose .footnotes { font-size: .85rem; color: var(--muted); }
.prose .footnote-ref a, .prose .footnote-backref { text-decoration: none; }
.prose pre { overflow-x: auto; background: var(--bg); padding: .6rem; border-radius: 8px; }
.prose pre code { padding: 0; background: none; }
.prose code { background: var(--bg); padding: .05rem .3rem; border-radius: 4px; font-size: .9em; }
.prose table { border-collapse: collapse; display: block; overflow-x: auto; max-width: 100%; margin: .8em 0; }
.prose th, .prose td { border: 1px solid var(--border); padding: .25rem .5rem; }
.prose th { background: var(--bg); }

dialog { background: var(--panel); color: var(--fg); border: 1px solid var(--border); border-radius: 12px; width: min(560px, 94vw); max-height: 90vh; padding: 1rem; }
dialog::backdrop { background: rgba(0,0,0,.5); }
.settings label { display: block; margin-top: .5rem; font-size: .9rem; }
.settings input { width: 100%; }
.subs { max-height: 220px; overflow: auto; border: 1px solid var(--border); border-radius: 8px; padding: .4rem .6rem; }
.subs label { display: flex; gap: .5rem; align-items: center; margin: .15rem 0; }

.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(1rem + env(safe-area-inset-bottom)); background: #0f172a; color: #fff;
  padding: .6rem 1rem; border-radius: 8px; max-width: 90vw; z-index: 20; box-shadow: 0 4px 16px rgba(0,0,0,.3); }

/* admin */
.tabs { display: flex; gap: .25rem; flex-wrap: wrap; margin-bottom: 1rem; }
.tabs button { background: transparent; color: var(--fg); border: 1px solid var(--border); }
.tabs button.active { background: var(--primary); color: var(--primary-fg); border-color: var(--primary); }
.split { display: grid; grid-template-columns: minmax(240px, 1fr) 2fr; gap: 1rem; }
@media (max-width: 760px) { .split { grid-template-columns: 1fr; } }
.tree, .tree ul { list-style: none; margin: 0; padding-left: 1rem; }
.tree { padding-left: 0; }
.tree .node { display: flex; align-items: center; gap: .4rem; padding: .2rem 0; }
.tree .node button.name { background: none; color: inherit; padding: .1rem .3rem; min-height: 0; text-align: left; }
.tree .node button.name.selected { font-weight: 700; color: var(--accent); }
.tree select { min-height: 0; padding: .1rem .3rem; font-size: .8rem; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { text-align: left; padding: .4rem .5rem; border-top: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 600; border-top: 0; }
.dot { display: inline-block; width: .6rem; height: .6rem; border-radius: 50%; background: var(--muted); margin-right: .3rem; }
.dot.on { background: var(--ok); }
.grid { display: grid; gap: .6rem; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); align-items: end; }
.grid > h2, .grid > p, .grid > .row, .grid > fieldset { grid-column: 1 / -1; }
.grid label { display: flex; flex-direction: column; gap: .2rem; font-size: .9rem; }
.grid label.check { flex-direction: row; align-items: center; gap: .5rem; }
.grid input, .grid select { width: 100%; }
.grid label.check input { width: auto; }
fieldset { border: 1px solid var(--border); border-radius: 8px; }
fieldset label { display: flex; flex-direction: row; gap: .5rem; align-items: center; }
fieldset input { width: auto; }
.qlist { margin: .5rem 0; padding-left: 1.2rem; }
