:root {
  --bg: #fbfaf8;
  --paper: #ffffff;
  --side: #f3f1ed;
  --side-hover: #e9e6e0;
  --active: #e0dcd4;
  --text: #1f1d1a;
  --muted: #7a756d;
  --border: #e3dfd8;
  --accent: #2f6fed;
  --danger: #c93c3c;
  --hilite: #fff1a8;
  --code-bg: #f4f2ee;
  --radius: 8px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a1c;
    --paper: #1f1f22;
    --side: #161618;
    --side-hover: #242428;
    --active: #2e2e33;
    --text: #e8e6e3;
    --muted: #8d8a85;
    --border: #2f2f34;
    --accent: #6f9bff;
    --danger: #ef6b6b;
    --hilite: #5b4f12;
    --code-bg: #26262a;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
.i { width: 16px; height: 16px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; vertical-align: -3px; }

/* ---------- login ---------- */
.login { position: fixed; inset: 0; display: grid; place-items: center; background: var(--bg); }
.login-card { width: min(340px, 90vw); display: grid; gap: 12px; padding: 32px; background: var(--paper); border: 1px solid var(--border); border-radius: 14px; }
.login-logo .i { width: 32px; height: 32px; color: var(--accent); }
.login-card h1 { margin: 0 0 8px; font-size: 22px; }
.login-card input { padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg); color: var(--text); font: inherit; }
.login-card button { padding: 10px; border: 0; border-radius: var(--radius); background: var(--accent); color: #fff; font-weight: 600; }
.login-error { color: var(--danger); margin: 0; min-height: 1.2em; font-size: 13px; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: 280px 1fr; height: 100vh; height: 100dvh; }
.sidebar { background: var(--side); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.main { min-width: 0; min-height: 0; display: flex; flex-direction: column; }

.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 12px 8px 16px; }
.brand { font-weight: 700; display: flex; align-items: center; gap: 8px; }
.brand .i { width: 18px; height: 18px; color: var(--accent); }
.sidebar-actions { display: flex; gap: 2px; }
.icon-btn { background: none; border: 0; padding: 6px; border-radius: 6px; line-height: 0; color: var(--muted); }
.icon-btn:hover { color: var(--text); }
.icon-btn.active { color: var(--accent); }
.icon-btn.active .i { fill: currentColor; }
.icon-btn:hover { background: var(--side-hover); }
.icon-btn.active { background: var(--active); }
.icon-btn.danger:hover { color: var(--danger); }
.link-btn { background: none; border: 0; color: var(--muted); font-size: 13px; padding: 4px; display: inline-flex; align-items: center; gap: 6px; }
.link-btn .i { width: 14px; height: 14px; }
.link-btn:hover { color: var(--text); }

.search { margin: 4px 12px 8px; padding: 0 8px 0 10px; display: flex; align-items: center; gap: 8px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--paper); color: var(--muted); }
.search:focus-within { border-color: var(--accent); }
.search input { flex: 1; min-width: 0; border: 0; outline: 0; background: none; color: var(--text); font: inherit; font-size: 14px; padding: 7px 0; }
.search kbd { font: 11px/1 inherit; font-family: inherit; color: var(--muted); border: 1px solid var(--border); border-radius: 4px; padding: 3px 5px; }
.tree { flex: 1; overflow-y: auto; padding: 4px 6px 12px; font-size: 14px; }
.sidebar-foot { padding: 8px 12px; border-top: 1px solid var(--border); }

/* ---------- tree ---------- */
.row {
  display: flex; align-items: center; gap: 6px;
  padding: 4px 6px; border-radius: 6px; cursor: pointer; user-select: none;
  white-space: nowrap; position: relative;
}
.row:hover { background: var(--side-hover); }
.row.active { background: var(--active); font-weight: 500; }
.row.drop { outline: 2px dashed var(--accent); outline-offset: -2px; }
.row .caret { width: 14px; color: var(--muted); transition: transform .12s; flex: none; display: flex; }
.row .caret .i { width: 14px; height: 14px; }
.row .caret.open { transform: rotate(90deg); }
.row .ico { flex: none; display: flex; color: var(--muted); }
.folder-row .ico { color: var(--accent); }
.row .label { overflow: hidden; text-overflow: ellipsis; flex: 1; }
.row .label input { width: 100%; font: inherit; padding: 0 4px; border: 1px solid var(--accent); border-radius: 4px; background: var(--paper); color: var(--text); }
.row .count { color: var(--muted); font-size: 12px; }
.row .actions { display: none; gap: 0; }
.row:hover .actions { display: flex; }
.row:hover .count { display: none; }
.row .actions button { background: none; border: 0; padding: 3px; color: var(--muted); border-radius: 4px; line-height: 0; }
.row .actions .i { width: 14px; height: 14px; }
.row .actions button:hover { color: var(--text); background: var(--active); }
.children { margin-left: 12px; }
.note-row .ico { color: var(--muted); }
.note-row .pin { display: flex; color: var(--accent); }
.note-row .pin .i { width: 12px; height: 12px; fill: currentColor; }
.section-label { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .06em; padding: 12px 8px 4px; }
.tree-empty { color: var(--muted); padding: 8px; font-size: 13px; }

/* ---------- editor ---------- */
.empty { flex: 1; display: grid; place-items: center; color: var(--muted); text-align: center; }
.empty-ico .i { width: 40px; height: 40px; stroke-width: 1.5; color: var(--border); }
.primary { display: inline-flex; align-items: center; gap: 8px; margin-top: 8px; padding: 8px 16px; border: 0; border-radius: var(--radius); background: var(--accent); color: #fff; font-weight: 600; }

.editor-wrap { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.editor-head { display: flex; align-items: center; gap: 6px; padding: 8px 16px; border-bottom: 1px solid var(--border); }
.crumbs { flex: 1; color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.save-state { color: var(--muted); font-size: 12px; min-width: 60px; text-align: right; }
.move-select { max-width: 160px; padding: 3px 6px; border: 1px solid var(--border); border-radius: 6px; background: var(--paper); color: var(--text); font-size: 13px; }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 2px; padding: 6px 16px; border-bottom: 1px solid var(--border); background: var(--bg); position: sticky; top: 0; z-index: 2; }
.toolbar button { background: none; border: 0; padding: 6px; border-radius: 6px; line-height: 0; color: var(--muted); }
.toolbar button:hover { color: var(--text); }
.toolbar button:hover { background: var(--side-hover); }
.toolbar .sep { width: 1px; height: 18px; background: var(--border); margin: 0 4px; }

.paper { flex: 1; overflow-y: auto; padding: 32px max(24px, calc((100% - 820px) / 2)) 120px; background: var(--paper); }
.title { width: 100%; border: 0; outline: 0; background: none; color: var(--text); font-family: inherit; font-size: 30px; font-weight: 700; line-height: 1.25; padding: 0; }
.meta { color: var(--muted); font-size: 12px; margin: 6px 0 20px; }

.editor { outline: none; min-height: 50vh; font-size: 16px; line-height: 1.7; overflow-wrap: anywhere; }
.editor { position: relative; }
.editor.is-empty::before { content: attr(data-placeholder); color: var(--muted); pointer-events: none; position: absolute; top: .35em; }
.editor h1 { font-size: 26px; margin: 1.2em 0 .4em; line-height: 1.3; }
.editor h2 { font-size: 21px; margin: 1.1em 0 .35em; line-height: 1.3; }
.editor h3 { font-size: 17px; margin: 1em 0 .3em; }
.editor p { margin: .35em 0; }
.editor ul, .editor ol { padding-left: 1.6em; margin: .35em 0; }
.editor blockquote { margin: .6em 0; padding: 2px 14px; border-left: 3px solid var(--border); color: var(--muted); }
.editor pre { background: var(--code-bg); padding: 12px 14px; border-radius: var(--radius); font: 13.5px/1.55 ui-monospace, SFMono-Regular, Menlo, monospace; white-space: pre-wrap; overflow-x: auto; }
.editor code { background: var(--code-bg); padding: 1px 5px; border-radius: 4px; font: .9em ui-monospace, SFMono-Regular, Menlo, monospace; }
.editor hr { border: 0; border-top: 1px solid var(--border); margin: 1.4em 0; }
.editor a { color: var(--accent); }
.editor mark { background: var(--hilite); color: inherit; padding: 0 2px; border-radius: 2px; }
.editor img { max-width: 100%; height: auto; border-radius: var(--radius); display: block; margin: 10px 0; cursor: default; }
.editor img.selected { outline: 3px solid var(--accent); }
.editor img.uploading { opacity: .5; }

.editor ul.checklist { list-style: none; padding-left: 4px; }
.editor ul.checklist li { position: relative; padding-left: 28px; }
.editor ul.checklist li::before {
  content: ""; position: absolute; left: 2px; top: .32em; width: 15px; height: 15px;
  border: 1.5px solid var(--muted); border-radius: 4px; cursor: pointer; background: var(--paper);
}
.editor ul.checklist li.done::before { background: var(--accent); border-color: var(--accent); }
.editor ul.checklist li.done::after { content: "✓"; position: absolute; left: 4.5px; top: .05em; color: #fff; font-size: 12px; font-weight: 700; pointer-events: none; }
.editor ul.checklist li.done { color: var(--muted); text-decoration: line-through; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: var(--radius); font-size: 13px; z-index: 50; }

.mobile-only { display: none; }
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 85vw; max-width: 320px; z-index: 20; transform: translateX(-100%); transition: transform .2s; }
  .app.side-open .sidebar { transform: none; box-shadow: 0 0 40px rgba(0,0,0,.25); }
  .mobile-only { display: inline-block; }
  .toolbar { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .toolbar button { padding: 9px; flex: none; }
  .toolbar .i { width: 18px; height: 18px; }
  .icon-btn { padding: 8px; }
  .icon-btn .i { width: 18px; height: 18px; }
  .search kbd { display: none; }
  .editor-head { padding-top: max(8px, env(safe-area-inset-top)); }
  .sidebar { padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom); }
  .row { padding: 8px 6px; }
  .row .actions { display: flex; }
  .row .actions .i { width: 16px; height: 16px; }
  .row .count { display: none; }
  .search input, .move-select { font-size: 16px; } /* stop iOS zoom-on-focus */
  .editor { font-size: 17px; }
  .move-select { max-width: 110px; }
  .paper { padding: 20px 16px 100px; }
  .title { font-size: 24px; }
}
