/* ---------- notes shell ---------- */
.notes-shell {
  display: grid;
  grid-template-columns: auto 8px minmax(0, 1fr);
  gap: 0;
  height: calc(100dvh - 52px - 48px);
  min-height: 400px;
  margin: -24px;
  padding: 16px 16px 16px 24px;
}
/* Closed tree: rail + editor only — two tracks for two children.
   (Three tracks here strands the editor in a 0-width track.) */
.notes-shell.tree-closed { grid-template-columns: 44px minmax(0, 1fr); }
.tree-rail {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 4px;
}
.tree-rail button { font-size: 16px; padding: 6px 10px; }
.notes-tree {
  overflow-y: auto;
  padding-right: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}
.tree-tools { display: flex; gap: 6px; }
.tree-tools button { flex: 1; font-size: 12px; padding: 5px 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-resize { cursor: col-resize; touch-action: none; border-radius: 4px; }
.tree-resize:hover { background: var(--border); }
.notes-ed {
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding: 0 8px;
  overflow: hidden;
}
.drawer-btn { align-self: flex-start; margin-bottom: 6px; font-size: 13px; }
button.ghost { background: none; }

.tree-folder { margin-left: 0; }
.tree-folder .folder-head {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 8px;
  border-radius: 6px;
  cursor: pointer;
  color: var(--dim);
  font-size: 13px;
}
.tree-folder .folder-head:hover { background: var(--panel2); color: var(--text); }
.tree-folder .folder-head.selected { color: var(--accent); background: var(--panel2); }
.tree-folder .folder-head .caret { width: 12px; flex-shrink: 0; cursor: pointer; }
.tree-folder .folder-kids { margin-left: 14px; }
.tree-doc {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 8px 5px 26px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  color: var(--dim);
}
.tree-doc:hover { background: var(--panel2); color: var(--text); }
.tree-doc.selected { color: var(--accent); background: var(--panel2); }
.node-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-item-btns { display: flex; gap: 2px; }
.tree-item-btns button { padding: 6px 9px; font-size: 12px; border: none; background: none; color: var(--dim); }
.tree-item-btns button:hover { color: var(--red); }
.inline-prompt { font-size: 13px; padding: 4px 8px; }

#ctx-menu {
  position: fixed;
  z-index: 300;
  min-width: 180px;
  background: var(--panel2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px;
  display: flex;
  flex-direction: column;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.6);
}
#ctx-menu button { background: none; border: none; text-align: left; padding: 8px 10px; border-radius: 6px; }
#ctx-menu button:hover { background: var(--panel); color: var(--accent); }

/* ---------- editor ---------- */
.note-crumbs { color: var(--dim); font-size: 12px; margin-bottom: 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.note-bar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 10px; }
.note-bar input#note-title { flex: 1; font-weight: 600; font-size: 16px; min-width: 160px; }
.note-bar select { width: auto; }
.note-actions { display: flex; gap: 8px; align-items: center; }
.note-actions button { padding: 7px 10px; font-size: 13px; }
.save-state { font-size: 12px; color: var(--dim); white-space: nowrap; }

/* live overlay: mirror + transparent textarea share exact metrics */
.live { position: relative; flex: 1; min-height: 0; }
.mirror, #note-body {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 15px;
  line-height: 1.7;
  padding: 14px 16px;
  margin: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
  tab-size: 2;
  width: 100%;
  height: 100%;
}
.mirror {
  position: absolute;
  inset: 0;
  /* Same scrollport as the textarea, including its own (inert) scrollbar:
     otherwise on systems with classic scrollbars the textarea wraps
     narrower, the mirror ends up shorter, and scroll clamps mid-note. */
  overflow-x: hidden;
  overflow-y: scroll;
  background: var(--panel);
  color: var(--text);
  pointer-events: none;
}
#note-body {
  position: absolute;
  inset: 0;
  background: transparent;
  color: transparent;
  caret-color: var(--accent);
  resize: none;
  overflow-x: hidden;
  overflow-y: scroll;
}
#note-body::selection { background: rgba(240, 240, 240, 0.25); }
#note-body:focus { outline: none; border-color: var(--dim); }

/* markdown tokens (mirror only).
   Hard rule: color / weight / decoration / background ONLY — never
   font-size, padding, margin, border, or indent. The textarea shares the
   mirror's exact metrics, so any layout change drifts the caret. */
.tk-mark { opacity: 0.35; }
.tk-h1, .tk-h2, .tk-h3 { font-weight: 700; color: #fff; }
.tk-h1 { text-decoration: underline; text-decoration-color: var(--dim); text-underline-offset: 4px; }
.tk-b { font-weight: 700; }
.tk-i { font-style: italic; }
.tk-s { text-decoration: line-through; opacity: 0.7; }
.tk-code { background: var(--bg); border-radius: 4px; }
.tk-codeblock { background: var(--bg); border-radius: 4px; }
/* NOTE: code spans must stay inline. display:block inside <pre> turns
   every inter-line newline into a phantom empty line, making the mirror
   taller than the textarea and clamping scroll. */
.tk-fence { opacity: 0.4; }
.tk-quote { color: var(--dim); }
.tk-li .tk-mark { opacity: 0.5; }
.tk-hr { opacity: 0.5; }
.tk-link { text-decoration: underline; }
.tk-tag { color: var(--amber); }
.tk-wiki { color: var(--green); }

.md-preview {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 18px 22px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
  font-size: 16px;
  line-height: 1.7;
}
.md-preview h1, .md-preview h2, .md-preview h3 { margin: 0.7em 0 0.4em; letter-spacing: -0.3px; }
.md-preview h1 { font-size: 22px; }
.md-preview h2 { font-size: 19px; }
.md-preview h3 { font-size: 17px; }
.md-preview p { margin: 0.6em 0; }
.md-preview ul, .md-preview ol { margin: 0.6em 0 0.6em 1.4em; }
.md-preview li { margin: 0.2em 0; }
.md-preview code { background: var(--bg); border: 1px solid var(--border); border-radius: 4px; padding: 1px 6px; font-size: 13.5px; }
.md-preview pre { background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 12px; overflow-x: auto; margin: 0.7em 0; }
.md-preview pre code { border: none; padding: 0; background: none; }
.md-preview a { color: var(--accent); }
.md-preview blockquote { border-left: 3px solid var(--border); margin: 0.6em 0; padding: 2px 0 2px 14px; color: var(--dim); }
.md-preview hr { border: none; border-top: 1px solid var(--border); margin: 1.2em 0; }
.md-preview table { margin: 0.7em 0; }

/* ---------- shares ---------- */
details.shares-details { margin-top: 10px; }
details.shares-details summary { cursor: pointer; color: var(--dim); font-size: 12px; text-transform: uppercase; letter-spacing: 1px; padding: 6px 0; }
.share-bar {
  display: flex;
  gap: 8px;
  align-items: center;
  background: var(--panel2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}
.share-bar input[readonly] { flex: 1; min-width: 200px; }
.shares-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 8px;
  border-radius: 6px;
  font-size: 13px;
  color: var(--dim);
}
.shares-row:hover { background: var(--panel2); }
.shares-row .doc-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* drawer (mobile) */
.drawer-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.6); z-index: 140; }
@media (max-width: 700px) {
  .notes-shell { grid-template-columns: minmax(0, 1fr); margin: -14px; padding: 14px; }
  .notes-tree.drawer {
    position: fixed; top: 0; left: 0; bottom: 0;
    width: min(300px, 84vw); z-index: 150;
    background: var(--panel);
    border-right: 1px solid var(--border);
    padding: 14px;
    transform: translateX(-105%); transition: transform 0.18s ease;
  }
  .notes-tree.drawer.open { transform: none; }
  .tree-resize { display: none; }
}
