:root {
  --bg: #ffffff;
  --bg-soft: #fafaf9;
  --bg-sidebar: #f7f7f5;
  --fg: #111111;
  --fg-muted: #6b6b6b;
  --fg-faint: #9a9a9a;
  --border: #ececec;
  --border-strong: #d8d8d6;
  --hover: #f0efed;
  --active: #e7e6e3;
  --accent: #111;
  --danger: #b91c1c;
  --shadow: 0 8px 24px rgba(15,15,15,.08), 0 2px 6px rgba(15,15,15,.05);
  --radius: 6px;
  --radius-lg: 10px;
  font-family: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  font-feature-settings: "cv11","ss01","ss03";
}
@supports (font-variation-settings: normal) {
  :root { font-family: "Inter var", ui-sans-serif, system-ui, sans-serif; }
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #191919;
    --bg-soft: #202020;
    --bg-sidebar: #161616;
    --fg: #f1f1ee;
    --fg-muted: #a4a4a0;
    --fg-faint: #707070;
    --border: #2a2a2a;
    --border-strong: #353535;
    --hover: #232323;
    --active: #2c2c2c;
    --accent: #f1f1ee;
    --danger: #ef4444;
    --shadow: 0 8px 24px rgba(0,0,0,.4), 0 2px 6px rgba(0,0,0,.35);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--fg);
  display: grid;
  grid-template-columns: 264px 1fr;
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; cursor: pointer; }

/* ── Sidebar ────────────────────────────────────────── */
#sidebar {
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100vh;
  position: sticky;
  top: 0;
  padding: 18px 8px 12px;
}
.brand {
  font-weight: 600;
  font-size: 13px;
  letter-spacing: 0.02em;
  color: var(--fg-muted);
  padding: 0 10px 10px;
  text-transform: uppercase;
}
.ghost-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  background: transparent;
  border: none;
  color: var(--fg-muted);
  padding: 6px 10px;
  text-align: left;
  border-radius: var(--radius);
  font-size: 14px;
}
.ghost-btn:hover { background: var(--hover); color: var(--fg); }
.ghost-btn.danger { color: var(--danger); }
.ghost-btn.danger:hover { background: var(--hover); }
.ghost-btn span { font-weight: 600; }

#page-tree {
  margin-top: 6px;
  flex: 1;
  overflow-y: auto;
  padding-bottom: 16px;
}

.tree-row {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 3px 6px 3px 4px;
  border-radius: var(--radius);
  color: var(--fg);
  cursor: pointer;
  user-select: none;
  position: relative;
}
.tree-row:hover { background: var(--hover); }
.tree-row.active { background: var(--active); }
.tree-row.drop-target { box-shadow: inset 0 -2px 0 var(--accent); }
.tree-toggle {
  width: 18px;
  height: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  color: var(--fg-faint);
  flex-shrink: 0;
  background: transparent;
  border: none;
}
.tree-toggle:hover { background: var(--active); color: var(--fg); }
.tree-toggle svg { width: 10px; height: 10px; transition: transform .15s; }
.tree-toggle.expanded svg { transform: rotate(90deg); }
.tree-icon { font-size: 14px; width: 18px; text-align: center; flex-shrink: 0; }
.tree-title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 14px;
  color: var(--fg);
}
.tree-row.untitled .tree-title { color: var(--fg-faint); font-style: italic; }
.tree-add {
  opacity: 0;
  width: 22px; height: 22px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 4px;
  color: var(--fg-faint);
  background: transparent;
  border: none;
  font-size: 16px;
  line-height: 1;
}
.tree-row:hover .tree-add { opacity: 1; }
.tree-add:hover { background: var(--active); color: var(--fg); }
.tree-children { padding-left: 14px; }

.footer {
  border-top: 1px solid var(--border);
  padding: 10px;
  font-size: 11px;
}
.muted { color: var(--fg-faint); }

/* ── Main ──────────────────────────────────────────── */
#editor {
  height: 100vh;
  overflow-y: auto;
  background: var(--bg);
}

.empty {
  display: grid;
  place-items: center;
  height: 100%;
  padding: 40px;
}
.empty-card {
  max-width: 420px;
  text-align: center;
}
.empty-card h2 { margin: 0 0 6px; font-weight: 600; font-size: 22px; }
.empty-card p { color: var(--fg-muted); margin: 0 0 18px; }
.primary-btn {
  background: var(--accent);
  color: var(--bg);
  border: none;
  padding: 9px 16px;
  border-radius: var(--radius);
  font-weight: 500;
  font-size: 14px;
}
.primary-btn:hover { opacity: .9; }

#page {
  max-width: 760px;
  margin: 0 auto;
  padding: 80px 56px 240px;
}
.page-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 18px;
}
.icon-btn {
  font-size: 32px;
  background: transparent;
  border: none;
  padding: 4px 8px;
  border-radius: var(--radius);
  line-height: 1;
}
.icon-btn:hover { background: var(--hover); }
#page-title {
  flex: 1;
  font-size: 38px;
  font-weight: 700;
  letter-spacing: -0.02em;
  border: none;
  background: transparent;
  outline: none;
  color: var(--fg);
  padding: 4px 0;
}
#page-title::placeholder { color: var(--fg-faint); }
.page-actions { display: flex; gap: 6px; opacity: 0; transition: opacity .15s; }
.page-header:hover .page-actions { opacity: 1; }

/* ── Blocks ─────────────────────────────────────────── */
#blocks { display: flex; flex-direction: column; }
.block {
  display: grid;
  grid-template-columns: 22px 22px 1fr;
  align-items: start;
  gap: 2px;
  padding: 1px 0;
  position: relative;
}
.block-handle, .block-add {
  opacity: 0;
  width: 22px;
  height: 26px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  color: var(--fg-faint);
  border-radius: 4px;
  flex-shrink: 0;
  transition: opacity .12s;
  margin-top: 2px;
}
.block:hover .block-handle,
.block:hover .block-add { opacity: 1; }
.block-handle { cursor: grab; }
.block-handle:hover, .block-add:hover { background: var(--hover); color: var(--fg); }
.block-handle:active { cursor: grabbing; }
.block-add { font-size: 16px; }

.block-content {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  min-width: 0;
  padding: 3px 2px;
  border-radius: 4px;
}
.block-text {
  flex: 1;
  min-width: 0;
  outline: none;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  caret-color: var(--fg);
  padding: 0 2px;
}
.block-text:empty::before {
  content: attr(data-placeholder);
  color: var(--fg-faint);
  pointer-events: none;
}

.block-children { padding-left: 24px; }

/* Drag states */
.block.dragging { opacity: 0.4; }
.block.drop-above::before,
.block.drop-below::after {
  content: "";
  position: absolute;
  left: 44px; right: 0;
  height: 2px;
  background: var(--accent);
  border-radius: 1px;
}
.block.drop-above::before { top: -1px; }
.block.drop-below::after { bottom: -1px; }
.block.drop-into > .block-content { box-shadow: inset 0 0 0 2px var(--accent); border-radius: 4px; }

/* Block type styles */
.block.h1 .block-text { font-size: 28px; font-weight: 700; line-height: 1.25; letter-spacing: -0.015em; padding-top: 14px; }
.block.h2 .block-text { font-size: 22px; font-weight: 700; line-height: 1.3; letter-spacing: -0.01em; padding-top: 12px; }
.block.h3 .block-text { font-size: 18px; font-weight: 600; line-height: 1.35; padding-top: 8px; }
.block.quote .block-content { border-left: 3px solid var(--border-strong); padding-left: 14px; margin: 4px 0; }
.block.quote .block-text { color: var(--fg-muted); font-style: italic; }
.block.bullet .block-prefix { color: var(--fg); padding-top: 0; line-height: 1.55; }
.block.todo .block-prefix { padding-top: 4px; }
.block.todo input[type=checkbox] {
  accent-color: var(--accent);
  width: 14px; height: 14px;
}
.block.todo.done .block-text { color: var(--fg-faint); text-decoration: line-through; }
.block.divider .block-content { padding: 8px 2px; }
.block.divider hr { border: none; border-top: 1px solid var(--border-strong); margin: 0; flex: 1; }
.block.code .block-content { background: var(--bg-soft); border: 1px solid var(--border); border-radius: 4px; padding: 10px 12px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13.5px; }
.block.code .block-text { font-family: inherit; }

/* ── Slash menu / popover ──────────────────────────── */
.popover {
  position: absolute;
  z-index: 100;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  width: 240px;
  padding: 6px;
  max-height: 320px;
  overflow-y: auto;
}
.popover-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--fg-faint);
  padding: 6px 10px 4px;
}
#slash-list { list-style: none; margin: 0; padding: 0; }
#slash-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
}
#slash-list li:hover, #slash-list li[aria-selected=true] { background: var(--hover); }
#slash-list li .si {
  width: 28px; height: 28px;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 14px; font-weight: 600;
  flex-shrink: 0;
}
#slash-list li .sd { display: flex; flex-direction: column; min-width: 0; }
#slash-list li .sd b { font-weight: 500; }
#slash-list li .sd small { color: var(--fg-faint); font-size: 12px; }

/* ── Toast ─────────────────────────────────────────── */
.toast {
  position: fixed;
  bottom: 18px; left: 50%;
  transform: translateX(-50%);
  background: var(--accent);
  color: var(--bg);
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 13px;
  box-shadow: var(--shadow);
  z-index: 1000;
  animation: toast-in .2s ease-out;
}
@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 8px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

@media (max-width: 720px) {
  body { grid-template-columns: 1fr; }
  #sidebar { position: fixed; z-index: 50; height: 100vh; width: 264px; transform: translateX(-100%); transition: transform .2s; }
  body.sidebar-open #sidebar { transform: translateX(0); }
  #page { padding: 56px 20px 200px; }
  #page-title { font-size: 30px; }
}
