/* Vault Reader — premium, calm, dark-first reading UI */

:root {
  --bg: #f7f6f3;
  --bg-elevated: #ffffff;
  --bg-sidebar: #f0efeb;
  --text: #1c1b19;
  --text-dim: #55534c;
  --text-faint: #86837a;
  --border: #dedbd2;
  --border-soft: #e8e5dc;
  --accent: #8a5cf6;
  --accent-soft: rgba(138, 92, 246, 0.12);
  --code-bg: #eeece5;
  --link: #6c3fd6;
  --shadow: 0 1px 2px rgba(20, 18, 12, 0.06), 0 8px 24px rgba(20, 18, 12, 0.06);
  --radius: 10px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", "Roboto Mono", Menlo, Consolas, monospace;
  --body-font: var(--sans);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14131a;
    --bg-elevated: #1a1922;
    --bg-sidebar: #17161e;
    --text: #ebe9f2;
    --text-dim: #a9a6b8;
    --text-faint: #706d80;
    --border: #2b2934;
    --border-soft: #232230;
    --accent: #a381ff;
    --accent-soft: rgba(163, 129, 255, 0.16);
    --code-bg: #201f29;
    --link: #b39bff;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 12px 32px rgba(0, 0, 0, 0.35);
  }
}

:root[data-theme="light"] {
  --bg: #f7f6f3; --bg-elevated: #ffffff; --bg-sidebar: #f0efeb; --text: #1c1b19;
  --text-dim: #55534c; --text-faint: #86837a; --border: #dedbd2; --border-soft: #e8e5dc;
  --accent: #8a5cf6; --accent-soft: rgba(138, 92, 246, 0.12); --code-bg: #eeece5; --link: #6c3fd6;
  --shadow: 0 1px 2px rgba(20, 18, 12, 0.06), 0 8px 24px rgba(20, 18, 12, 0.06);
}
:root[data-theme="dark"] {
  --bg: #14131a; --bg-elevated: #1a1922; --bg-sidebar: #17161e; --text: #ebe9f2;
  --text-dim: #a9a6b8; --text-faint: #706d80; --border: #2b2934; --border-soft: #232230;
  --accent: #a381ff; --accent-soft: rgba(163, 129, 255, 0.16); --code-bg: #201f29; --link: #b39bff;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 12px 32px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

.app { display: flex; flex-direction: column; height: 100vh; }

/* ---------- Topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 56px;
  min-height: 56px;
  padding: 0 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elevated);
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 8px; }
.brand-mark { color: var(--accent); font-size: 18px; }
.brand-name { font-weight: 600; letter-spacing: -0.01em; font-size: 15px; }
.topbar-spacer { flex: 1; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  min-width: 44px;
  min-height: 44px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}
.icon-btn:hover { background: var(--accent-soft); color: var(--text); }
.icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.serif-glyph { font-family: var(--serif); font-size: 15px; font-weight: 600; }
.icon-moon { display: none; }
@media (prefers-color-scheme: dark) { .icon-sun { display: none; } .icon-moon { display: block; } }
:root[data-theme="dark"] .icon-sun { display: none; }
:root[data-theme="dark"] .icon-moon { display: block; }
:root[data-theme="light"] .icon-sun { display: block; }
:root[data-theme="light"] .icon-moon { display: none; }

/* ---------- Layout ---------- */
.body-row { flex: 1; display: flex; min-height: 0; }

.sidebar {
  width: 300px;
  min-width: 300px;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: margin-left 0.22s ease, opacity 0.18s ease;
}
.sidebar.collapsed { margin-left: -300px; opacity: 0; pointer-events: none; }

.folder-controls {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 12px 12px 0;
}
.folder-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.folder-btn:hover { background: var(--accent-soft); border-color: var(--accent); }
.folder-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.folder-btn.reopen { color: var(--accent); }
.folder-btn span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.empty-fallback-note {
  max-width: 44ch;
  margin: 12px auto 0;
  font-size: 13px;
  color: var(--text-faint);
}

.filter-box {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 12px;
  padding: 0 10px;
  height: 40px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
}
.filter-icon { color: var(--text-faint); flex-shrink: 0; }
.filter-box input {
  flex: 1;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
  font-size: 14px;
  font-family: var(--sans);
  height: 100%;
}
.filter-box input::placeholder { color: var(--text-faint); }
.filter-box:focus-within { border-color: var(--accent); }

.tree {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px 16px;
}

.tree-folder { user-select: none; }
.tree-folder-label {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 36px;
  padding: 0 8px;
  border-radius: 6px;
  cursor: pointer;
  color: var(--text-dim);
  font-size: 12.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.tree-folder-label:hover { background: var(--accent-soft); }
.tree-folder-label .chevron {
  display: inline-flex;
  transition: transform 0.15s ease;
  color: var(--text-faint);
  flex-shrink: 0;
}
.tree-folder.collapsed > .tree-folder-label .chevron { transform: rotate(-90deg); }
.tree-folder.collapsed > .tree-children { display: none; }
.tree-children { margin-left: 14px; border-left: 1px solid var(--border-soft); padding-left: 6px; }

.tree-file {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 8px;
  border-radius: 6px;
  cursor: pointer;
  color: var(--text);
  font-size: 14px;
  line-height: 1.3;
  text-decoration: none;
  border: 1px solid transparent;
}
.tree-file .file-dot { color: var(--text-faint); flex-shrink: 0; font-size: 10px; }
.tree-file:hover { background: var(--accent-soft); }
.tree-file.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.tree-file.kbd-selected { outline: 2px solid var(--accent); outline-offset: -2px; }
.tree-empty { padding: 24px 12px; color: var(--text-faint); font-size: 13px; text-align: center; }

/* ---------- Reader ---------- */
.reader {
  flex: 1;
  overflow-y: auto;
  background: var(--bg);
  outline: none;
}
.reader-inner {
  max-width: 72ch;
  margin: 0 auto;
  padding: 56px 24px 120px;
}
.empty-state { color: var(--text-faint); padding-top: 20vh; text-align: center; font-size: 15px; }
.empty-state kbd {
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 5px;
  padding: 1px 7px;
  font-family: var(--mono);
  font-size: 12px;
  background: var(--bg-elevated);
}

.doc {
  font-family: var(--body-font);
  font-size: 17px;
  line-height: 1.75;
  color: var(--text);
}
.doc.serif { --body-font: var(--serif); }

.doc h1, .doc h2, .doc h3, .doc h4, .doc h5, .doc h6 {
  font-family: var(--sans);
  font-weight: 700;
  letter-spacing: -0.015em;
  color: var(--text);
  line-height: 1.25;
  margin: 1.7em 0 0.6em;
}
.doc.serif h1, .doc.serif h2, .doc.serif h3, .doc.serif h4, .doc.serif h5, .doc.serif h6 { font-family: var(--serif); }
.doc h1 { font-size: 1.9em; margin-top: 0; border-bottom: 1px solid var(--border); padding-bottom: 0.3em; }
.doc h2 { font-size: 1.5em; }
.doc h3 { font-size: 1.25em; }
.doc h4 { font-size: 1.08em; }
.doc h5, .doc h6 { font-size: 1em; color: var(--text-dim); }

.doc p { margin: 0 0 1.1em; }
.doc a { color: var(--link); text-decoration: underline; text-decoration-color: color-mix(in srgb, var(--link) 40%, transparent); text-underline-offset: 2px; }
.doc a:hover { text-decoration-color: var(--link); }
.doc strong { font-weight: 700; }
.doc em { font-style: italic; }

.doc ul, .doc ol { margin: 0 0 1.1em; padding-left: 1.5em; }
.doc li { margin: 0.3em 0; }
.doc li.task-item { list-style: none; margin-left: -1.5em; padding-left: 0; display: flex; align-items: flex-start; gap: 0.5em; }
.doc li.task-item input[type="checkbox"] {
  margin-top: 0.35em;
  width: 16px; height: 16px;
  accent-color: var(--accent);
  flex-shrink: 0;
}

.doc blockquote {
  margin: 0 0 1.1em;
  padding: 0.4em 1em;
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  color: var(--text-dim);
  border-radius: 0 6px 6px 0;
}
.doc blockquote p:last-child { margin-bottom: 0; }

.doc code {
  font-family: var(--mono);
  font-size: 0.87em;
  background: var(--code-bg);
  padding: 0.15em 0.4em;
  border-radius: 4px;
  color: var(--text);
}
.doc pre {
  background: var(--code-bg);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 14px 16px;
  overflow-x: auto;
  margin: 0 0 1.3em;
}
.doc pre code {
  background: none;
  padding: 0;
  font-size: 0.85em;
  line-height: 1.6;
  white-space: pre;
}

.doc img { max-width: 100%; border-radius: 8px; display: block; margin: 1em 0; }

.doc hr {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 2.4em 0;
}

.doc table {
  width: 100%;
  border-collapse: collapse;
  margin: 0 0 1.4em;
  font-size: 0.92em;
  display: block;
  overflow-x: auto;
}
.doc th, .doc td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  text-align: left;
}
.doc th { background: var(--bg-sidebar); font-weight: 600; }
.doc tr:nth-child(even) td { background: color-mix(in srgb, var(--bg-sidebar) 40%, transparent); }

.doc .wikilink { color: var(--accent); font-weight: 500; }
.doc .wikilink.broken { color: var(--text-faint); text-decoration: line-through; cursor: default; }

/* Frontmatter chip block */
.frontmatter {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 2em;
  padding: 14px 16px;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  background: var(--bg-elevated);
  box-shadow: var(--shadow);
}
.fm-chip {
  display: flex;
  align-items: baseline;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--code-bg);
  font-size: 12.5px;
  font-family: var(--mono);
}
.fm-key { color: var(--text-faint); }
.fm-val { color: var(--text); }

/* ---------- Responsive / mobile-first collapse ---------- */
@media (max-width: 760px) {
  .sidebar {
    position: fixed;
    top: 56px;
    bottom: 0;
    left: 0;
    z-index: 15;
    width: 86vw;
    max-width: 340px;
    box-shadow: var(--shadow);
  }
  .sidebar.collapsed { margin-left: -100vw; }
  .reader-inner { padding: 32px 18px 100px; }
  .doc { font-size: 16px; }
}

/* Scrollbars, subtle */
.tree::-webkit-scrollbar, .reader::-webkit-scrollbar { width: 10px; }
.tree::-webkit-scrollbar-thumb, .reader::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 6px;
  border: 2px solid transparent;
  background-clip: content-box;
}
