/* kiwi — styles. Light palette on :root, dark palette via media query and data-theme. */
:root {
  --bg: #fafaf8;
  --bg-2: #f1f0ec;
  --bg-3: #e7e5df;
  --fg: #1f1f1d;
  --fg-2: #5c5a54;
  --fg-3: #8c8a83;
  --line: #dedbd3;
  --accent: #2f6fdf;
  --accent-bg: #e6eefc;
  --danger: #c23b3b;
  --missing: #b8381f;
  --mark: #fff0a6;
  --code-bg: #ecebe6;
  --shadow: 0 6px 24px rgba(0,0,0,.12);
  --radius: 8px;
  --sidebar-w: 270px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b1c1e; --bg-2: #232427; --bg-3: #2c2d31; --fg: #e8e6e1; --fg-2: #b3b0a8; --fg-3: #7d7b75;
    --line: #35363b; --accent: #7aa7ff; --accent-bg: #253352; --danger: #ff7b7b; --missing: #ff8a65;
    --mark: #6b5a12; --code-bg: #26272b; --shadow: 0 6px 24px rgba(0,0,0,.5); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1b1c1e; --bg-2: #232427; --bg-3: #2c2d31; --fg: #e8e6e1; --fg-2: #b3b0a8; --fg-3: #7d7b75;
  --line: #35363b; --accent: #7aa7ff; --accent-bg: #253352; --danger: #ff7b7b; --missing: #ff8a65;
  --mark: #6b5a12; --code-bg: #26272b; --shadow: 0 6px 24px rgba(0,0,0,.5); color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { font-size: 16px; }
body { margin: 0; font-family: var(--font); color: var(--fg); background: var(--bg); line-height: 1.55; display: flex; height: 100vh; height: 100dvh; overflow: hidden; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { line-height: 1.25; margin: 1.2em 0 .5em; }
h1 { font-size: 1.9rem; margin-top: .2em; }
h2 { font-size: 1.35rem; }
h3 { font-size: 1.1rem; }
kbd { font: .85em var(--mono); background: var(--bg-3); border: 1px solid var(--line); border-radius: 4px; padding: 0 .35em; }
.muted { color: var(--fg-2); }
.small { font-size: .85rem; }
.badge { font-size: .75rem; background: var(--accent-bg); color: var(--accent); border-radius: 999px; padding: .1em .6em; vertical-align: middle; }
mark { background: var(--mark); color: inherit; border-radius: 2px; padding: 0 .1em; }
input[type=text], input[type=search], input[type=password], select, textarea {
  font: inherit; color: var(--fg); background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: .45em .6em; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
label { display: block; font-size: .9rem; color: var(--fg-2); }
label > input, label > select { margin-top: .25em; }

/* buttons */
.btn { display: inline-block; font: inherit; font-size: .9rem; cursor: pointer; color: var(--fg); background: var(--bg-2); border: 1px solid var(--line); border-radius: 6px; padding: .4em .85em; line-height: 1.3; text-decoration: none; white-space: nowrap; }
.btn:hover { background: var(--bg-3); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.small { font-size: .8rem; padding: .25em .6em; }
button.link { font: inherit; background: none; border: 0; color: var(--accent); cursor: pointer; padding: 0; text-align: left; }
button.link:hover { text-decoration: underline; }
.icon-btn { font: inherit; background: none; border: 0; color: var(--fg-2); cursor: pointer; padding: .2em .4em; border-radius: 4px; display: inline-flex; align-items: center; }
#theme-toggle .icon { width: 18px; height: 18px; }
.icon-btn:hover { background: var(--bg-3); color: var(--fg); }
.danger { color: var(--danger) !important; }

/* sidebar */
.sidebar { order: 0; width: var(--sidebar-w); flex: 0 0 auto; background: var(--bg-2); border-right: 1px solid var(--line); padding: 14px 18px; display: flex; flex-direction: column; gap: 12px; height: 100%; overflow-x: hidden; overflow-y: auto; overscroll-behavior: contain; font-size: .92rem; }
.sidebar-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; }
.brand { font-weight: 700; font-size: 1.15rem; color: var(--fg); }
.brand-emoji { filter: grayscale(1); }
.search input { padding: .4em .6em; }
.sidebar h3 { font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; color: var(--fg-3); margin: 0 0 .3em; }
.sidebar ul { list-style: none; margin: 0; padding: 0; }
.sidebar nav a { color: var(--fg); display: block; padding: .18em .35em; border-radius: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar nav a:hover { background: var(--bg-3); text-decoration: none; }
.sidebar li.current > .node > a, .pinned li.current > a { color: var(--fg); font-weight: 700; }
.tree { line-height: 1.6; }
.tree .node { display: flex; align-items: center; }
.tree .node a { flex: 0 1 auto; min-width: 0; padding-top: .11em; padding-bottom: .11em; }
.tree .count { flex: 0 0 auto; margin-left: .3em; font-size: .78rem; color: var(--fg-3); opacity: .8; font-variant-numeric: tabular-nums; }
.tree .leader { flex: 1 1 0; min-width: .8em; margin: 0 .35em; overflow: hidden; white-space: nowrap; color: var(--fg-3); opacity: .45; }
.tree .leader::before { content: '........................................................................'; letter-spacing: .12em; }
.tree .words { flex: 0 0 auto; font-size: .78rem; color: var(--fg-3); opacity: .8; font-variant-numeric: tabular-nums; text-align: right; }
.tree .tog { flex: 0 0 20px; height: 22px; border: 0; background: none; color: var(--fg-3); cursor: pointer; padding: 0; border-radius: 5px; display: inline-flex; align-items: center; justify-content: center; }
.tree .tog:hover { background: var(--bg-3); color: var(--fg); }
.tree .tog::before { content: ''; width: 7px; height: 7px; margin-left: -2px; border-right: 1.6px solid currentColor; border-bottom: 1.6px solid currentColor; transform: rotate(-45deg); transition: transform .15s; }
.tree li.open > .node > .tog::before { transform: rotate(45deg); margin: -2px 0 0 0; }
.tree .tog.leaf::before { border: 0; }
.tree li > ul { display: none; padding-left: 18px; }
.tree li.open > ul { display: block; }
.tree li.drag-over-into > .node > a { outline: 2px dashed var(--accent); }
.tree li.drag-over-before > .node { box-shadow: 0 -2px 0 var(--accent); }
.tree li.drag-over-after > .node { box-shadow: 0 2px 0 var(--accent); }
.tree li.dragging > .node { opacity: .4; }
.sidebar-actions { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 4px; }
.sidebar-actions form { display: contents; }
.sidebar .sidebar-actions a, .sidebar .sidebar-actions button { font: inherit; background: none; border: 0; cursor: pointer; color: var(--fg-2); display: inline-flex; align-items: center; justify-content: center; padding: 6px; border-radius: 6px; line-height: 0; }
.sidebar .sidebar-actions a:hover, .sidebar .sidebar-actions button:hover { background: var(--bg-3); color: var(--fg); text-decoration: none; }
.sidebar-actions .icon { width: 18px; height: 18px; }
.icon { width: 1em; height: 1em; vertical-align: -.15em; flex: 0 0 auto; }
.btn .icon { margin-right: .15em; }
.menu-items .icon { margin-right: .4em; color: var(--fg-2); }
.menu-items .danger .icon { color: var(--danger); }
.sidebar-resize { position: fixed; top: 0; left: calc(var(--sidebar-w) - 5px); width: 5px; height: 100vh; cursor: col-resize; z-index: 5; }
.sidebar-resize:hover, body.resizing .sidebar-resize { background: var(--accent); opacity: .5; }
body.resizing { cursor: col-resize; user-select: none; }
body.resizing .sidebar { transition: none; }
/* scrollbars: invisible until the area scrolls, then they fade back out */
.main, .sidebar { scrollbar-width: thin; scrollbar-color: transparent transparent; }
.main.scrolling, .sidebar.scrolling { scrollbar-color: var(--fg-3) transparent; }
.main::-webkit-scrollbar, .sidebar::-webkit-scrollbar { width: 10px; height: 10px; }
.main::-webkit-scrollbar-thumb, .sidebar::-webkit-scrollbar-thumb { background: transparent; border: 3px solid transparent; background-clip: padding-box; border-radius: 6px; }
.main.scrolling::-webkit-scrollbar-thumb, .sidebar.scrolling::-webkit-scrollbar-thumb { background: var(--fg-3); background-clip: padding-box; }
.sidebar-toggle { display: none; position: fixed; top: 10px; left: 10px; z-index: 30; font-size: 1.2rem; background: var(--bg-2); border: 1px solid var(--line); border-radius: 6px; padding: .2em .5em; cursor: pointer; color: var(--fg); }

/* main */
.main { flex: 1; min-width: 0; padding: 28px 40px 60px; height: 100%; overflow-y: auto; overscroll-behavior: contain; scrollbar-gutter: stable; }
.main:focus { outline: none; }
.page, .editor-page { max-width: 820px; margin: 0 auto; }
.crumbs { font-size: .85rem; color: var(--fg-3); display: flex; gap: .4em; flex-wrap: wrap; }
.crumbs a { color: var(--fg-2); }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.page-head h1 { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.actions { display: flex; gap: 2px; align-items: center; margin-top: .5em; }
.actions .icon-btn { width: 34px; height: 34px; justify-content: center; padding: 0; border-radius: 8px; color: var(--fg-2); transition: box-shadow .12s, background .12s, color .12s; }
.actions .icon-btn .icon { width: 18px; height: 18px; }
.actions .icon-btn:hover { background: var(--bg); color: var(--fg); box-shadow: var(--shadow); text-decoration: none; }
.actions .is-home { color: var(--fg-3); opacity: .5; cursor: default; }
.actions .is-home:hover { background: none; box-shadow: none; color: var(--fg-3); }
.menu { position: relative; }
.menu summary { list-style: none; }
.menu summary::-webkit-details-marker { display: none; }
.menu-items { position: absolute; right: 0; top: calc(100% + 4px); background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); min-width: 170px; padding: 4px; z-index: 20; display: flex; flex-direction: column; }
.menu-items button { font: inherit; font-size: .9rem; background: none; border: 0; color: var(--fg); text-align: left; padding: .45em .7em; border-radius: 4px; cursor: pointer; }
.menu-items button:hover { background: var(--bg-2); }
.tags { display: flex; flex-wrap: wrap; gap: .4em; margin: .2em 0 1em; }
.tag { font-size: .8rem; background: var(--bg-2); border: 1px solid var(--line); border-radius: 999px; padding: .1em .7em; color: var(--fg-2); }
.tag:hover { background: var(--accent-bg); color: var(--accent); text-decoration: none; }
.tag-cloud { display: flex; flex-wrap: wrap; gap: .5em; }
.tag-cloud .tag { font-size: 1rem; }
.page-section { margin-top: 2.2em; padding-top: 1em; border-top: 1px solid var(--line); }
.page-section h2 { font-size: 1rem; text-transform: uppercase; letter-spacing: .06em; color: var(--fg-3); margin-top: 0; display: flex; align-items: center; gap: 12px; }
ul.plain, ul.files, ul.results { list-style: none; padding: 0; margin: 0; }
ul.plain li, ul.files li { padding: .2em 0; }
ul.files li { display: flex; align-items: center; gap: .6em; }
ul.results li { padding: .6em 0; border-bottom: 1px solid var(--line); }
.result-title { font-weight: 600; font-size: 1.05rem; }
.result-title.missing { color: var(--missing); }
.snippet { margin: .2em 0 0; color: var(--fg-2); font-size: .92rem; }
.page-foot { margin-top: 3em; font-size: .8rem; }
.inline-form { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap; margin: 1em 0; }
.inline-form label { flex: 1; min-width: 200px; }
.row-form { display: flex; gap: 6px; align-items: center; }
.row-form input, .row-form select { width: auto; min-width: 0; }
.stack-form { display: flex; flex-direction: column; gap: 14px; max-width: 480px; }
table.revisions { border-collapse: collapse; width: 100%; }
table.revisions th, table.revisions td { text-align: left; padding: .5em .6em; border-bottom: 1px solid var(--line); }
table.revisions th { font-size: .8rem; text-transform: uppercase; color: var(--fg-3); }

/* diff */
.diff { font: .85rem var(--mono); line-height: 1.7; background: var(--bg-2); border: 1px solid var(--line); border-radius: var(--radius); overflow-x: auto; padding: 10px 0; }
.diff-line { white-space: pre-wrap; padding: 2px 10px; display: flex; gap: 8px; }
.diff-line + .diff-line, .diff-line + .diff-skip, .diff-skip + .diff-line { margin-top: 1.7em; }
.diff-line span { color: var(--fg-3); flex: 0 0 1ch; user-select: none; }
.diff-line.add { background: rgba(60,160,90,.18); }
.diff-line.del { background: rgba(200,60,60,.18); }
.diff-skip { color: var(--fg-3); padding: 6px 10px; }

/* prose (page content and editor) */
.prose { font-size: 1.02rem; line-height: 1.65; overflow-wrap: anywhere; }
.prose > :first-child { margin-top: 0; }
.prose p { margin: .95em 0; }
.prose h1 { font-size: 1.7rem; } .prose h2 { font-size: 1.4rem; } .prose h3 { font-size: 1.15rem; }
.prose ul, .prose ol { padding-left: 1.6em; }
.prose li p { margin: .2em 0; }
.prose blockquote { margin: 1em 0; padding: .2em 1em; border-left: 3px solid var(--line); color: var(--fg-2); }
.prose code { font: .9em var(--mono); background: var(--code-bg); border-radius: 4px; padding: .1em .35em; }
.prose pre { background: var(--code-bg); border-radius: var(--radius); padding: .9em 1em; overflow-x: auto; }
.prose pre code { background: none; padding: 0; font-size: .88em; }
.prose img { max-width: 100%; height: auto; border-radius: 4px; }
.prose hr { border: 0; border-top: 1px solid var(--line); margin: 1.6em 0; }
.prose a.missing { color: var(--missing); border-bottom: 1px dashed var(--missing); }
.prose a[data-wiki] { font-weight: 500; }
.prose table { border-collapse: collapse; width: 100%; margin: 1em 0; table-layout: fixed; overflow: hidden; }
.prose th, .prose td { border: 1px solid var(--line); padding: .35em .6em; vertical-align: top; position: relative; min-width: 3em; }
.prose th { background: var(--bg-2); font-weight: 600; text-align: left; }
.prose th p, .prose td p { margin: 0; }
.prose ul[data-type="taskList"] { list-style: none; padding-left: .2em; }
.prose ul[data-type="taskList"] li { display: flex; gap: .5em; align-items: flex-start; }
.prose ul[data-type="taskList"] li > label { flex: 0 0 auto; margin-top: .35em; }
.prose ul[data-type="taskList"] li > div { flex: 1; }
.prose li[data-checked="true"] > div { color: var(--fg-3); text-decoration: line-through; }
.prose .table-wrap { overflow-x: auto; }

/* editor */
.editor-head { display: flex; gap: 10px; align-items: center; margin: .4em 0 .3em; }
.title-input { font-size: 1.8rem; font-weight: 700; border: 0; background: transparent; padding: .1em 0; flex: 1; border-bottom: 1px solid transparent; border-radius: 0; }
.title-input:focus { outline: none; border-bottom-color: var(--accent); }
.save-status { font-size: .8rem; color: var(--fg-3); min-width: 5em; text-align: right; }
.save-status.error { color: var(--danger); }
.tags-row { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.tags-row input { border: 0; background: transparent; padding: .2em 0; }
.tags-row input:focus { outline: none; border-bottom: 1px solid var(--accent); border-radius: 0; }
.toolbar { display: flex; flex-wrap: wrap; gap: 2px; position: sticky; top: 0; z-index: 10; background: var(--bg); padding: 6px 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.toolbar button { font: inherit; font-size: .85rem; min-width: 30px; height: 28px; padding: 0 7px; background: none; border: 1px solid transparent; border-radius: 5px; color: var(--fg-2); cursor: pointer; }
.toolbar button:hover { background: var(--bg-2); color: var(--fg); }
.toolbar button.active { background: var(--accent-bg); color: var(--accent); }
.toolbar .sep { width: 1px; background: var(--line); margin: 4px 4px; }
.editor { min-height: 50vh; padding: 14px 2px; }
.editor .tiptap { outline: none; min-height: 45vh; }
.editor .tiptap p.is-editor-empty:first-child::before { content: attr(data-placeholder); color: var(--fg-3); float: left; height: 0; pointer-events: none; }
.editor .tiptap .selectedCell::after { content: ''; position: absolute; inset: 0; background: var(--accent-bg); opacity: .5; pointer-events: none; }
.editor .tiptap .column-resize-handle { position: absolute; right: -2px; top: 0; bottom: 0; width: 4px; background: var(--accent); pointer-events: none; }
.editor .tiptap img.ProseMirror-selectednode { outline: 2px solid var(--accent); }
.editor .tiptap .ProseMirror-gapcursor::after { border-top: 1px solid var(--fg); }
.editor .tiptap ul[data-type="taskList"] li > label input { cursor: pointer; }
.editor.drop-target { outline: 2px dashed var(--accent); }
.attachments .upload-btn { cursor: pointer; }
.keys { margin-top: 2em; }

/* suggestion popup (links and slash menu) */
.popup { position: fixed; z-index: 50; background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); min-width: 240px; max-width: 360px; max-height: 300px; overflow-y: auto; padding: 4px; font-size: .92rem; }
.popup .item { padding: .4em .7em; border-radius: 5px; cursor: pointer; display: flex; gap: .6em; align-items: baseline; }
.popup .item.selected { background: var(--accent-bg); color: var(--accent); }
.popup .item small { color: var(--fg-3); margin-left: auto; }
.popup .empty { padding: .4em .7em; color: var(--fg-3); }
.popup .item .create { color: var(--missing); }
.link-popover { position: fixed; z-index: 50; background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 8px; display: flex; gap: 6px; width: 360px; }

/* quick switcher */
.switcher { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 100; display: flex; justify-content: center; align-items: flex-start; padding-top: 12vh; }
.switcher-box { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); width: min(560px, 92vw); padding: 8px; }
.switcher-box input { font-size: 1.05rem; border: 0; border-bottom: 1px solid var(--line); border-radius: 0; padding: .6em .5em; }
.switcher-box input:focus { outline: none; }
.switcher-box ul { list-style: none; margin: 6px 0 0; padding: 0; max-height: 50vh; overflow-y: auto; }
.switcher-box li { padding: .5em .7em; border-radius: 6px; cursor: pointer; display: flex; justify-content: space-between; gap: 1em; }
.switcher-box li.selected { background: var(--accent-bg); color: var(--accent); }
.switcher-box li small { color: var(--fg-3); }
.switcher-hint { font-size: .75rem; color: var(--fg-3); padding: 6px 8px 2px; }

/* dialogs */
dialog { background: var(--bg); color: var(--fg); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; width: min(420px, 92vw); }
dialog::backdrop { background: rgba(0,0,0,.4); }
.dialog-form h2 { margin-top: 0; font-size: 1.2rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

/* auth pages */
body.auth { align-items: center; justify-content: center; padding: 20px; height: auto; min-height: 100vh; overflow: auto; }
.card { background: var(--bg-2); border: 1px solid var(--line); border-radius: 12px; padding: 28px; width: min(420px, 100%); display: flex; flex-direction: column; gap: 14px; }
.card h1 { margin: 0; font-size: 1.5rem; }
.card h2 { font-size: .9rem; text-transform: uppercase; color: var(--fg-3); margin: 1em 0 0; }
.error { color: var(--danger); margin: 0; }
.checks { list-style: none; padding: 0; margin: 0; font-size: .9rem; }
.checks .ok { color: #2e8b57; } .checks .bad { color: var(--danger); }

/* toast */
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--fg); color: var(--bg); padding: .6em 1.1em; border-radius: 999px; font-size: .9rem; z-index: 200; box-shadow: var(--shadow); }

@media (max-width: 860px) {
  .sidebar { position: fixed; left: 0; top: 0; z-index: 25; transform: translateX(-100%); transition: transform .15s; width: min(300px, 85vw); }
  .sidebar-resize { display: none; }
  body.sidebar-open .sidebar { transform: none; }
  .sidebar-toggle { display: block; }
  .main { padding: 60px 18px 40px; }
}
@media print {
  .sidebar, .sidebar-toggle, .actions, .page-foot, .switcher { display: none !important; }
  .main { padding: 0; }
}
