/* nTrack app-local styles. Layout + domain widgets + markdown prose.
   Shared buttons/modals/toasts/search-bar/auth-overlay/user-menu/scrollbars
   come from /ui/ui.css. Colors always reference semantic tokens. */

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--ui-bg);
  color: var(--ui-text);
  font-family: var(--ui-font, system-ui, sans-serif);
  display: flex;
  flex-direction: column;
}

/* ── header ─────────────────────────────────────────────────────── */
header {
  flex: 0 0 auto;
  border-bottom: 1px solid var(--ui-border);
  background: var(--ui-panel);
}
.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--ui-space-3, 12px);
  padding: 10px 16px;
}
.header-brand { display: flex; align-items: center; gap: 10px; }
.header-brand h1 {
  margin: 0;
  font-size: 1.15rem;
  letter-spacing: 0.5px;
  color: var(--ui-accent);
}
.icon-btn {
  background: none;
  border: none;
  color: var(--ui-text);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--ui-radius, 8px);
}
.icon-btn:hover { background: var(--ui-panel-hover); }
.sidebar-toggle { display: none; }

/* ── layout: sidebar | list | editor ───────────────────────────── */
.layout {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: 320px 1fr;
  min-height: 0;
  overflow: hidden;
}

.sidebar {
  border-right: 1px solid var(--ui-border);
  background: var(--ui-panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
/* tab strip: folder icon | search icon */
.sidebar-tabs {
  display: flex;
  gap: 4px;
  padding: 8px;
  border-bottom: 1px solid var(--ui-border);
  flex: 0 0 auto;
}
.sidebar-tab {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: none;
  color: var(--ui-muted);
  padding: 8px;
  border-radius: var(--ui-radius, 8px);
  cursor: pointer;
  font-size: 1.15rem;
  line-height: 1;
}
.sidebar-tab:hover { background: var(--ui-panel-hover); color: var(--ui-text); }
.sidebar-tab.is-active { background: var(--ui-panel-2); color: var(--ui-accent); }
/* panels share the area below the tab strip; only one shows at a time */
.sidebar-panel { flex: 1 1 auto; min-height: 0; }
.sidebar-panel[data-panel="folders"] { overflow-y: auto; padding: 12px 8px; }
.sidebar-panel[data-panel="notes"] { display: flex; flex-direction: column; overflow: hidden; }
/* Must come last: equal specificity to the [data-panel] rules, so source
   order decides — [hidden] has to win to actually hide the notes panel. */
.sidebar-panel[hidden] { display: none; }
.sidebar-nav { display: flex; flex-direction: column; gap: 4px; }
.sidebar-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.sidebar-section-label {
  text-transform: uppercase;
  font-size: 0.7rem;
  letter-spacing: 0.6px;
  color: var(--ui-muted);
  margin: 14px 8px 4px;
}
.sidebar-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  border: none;
  background: none;
  color: var(--ui-text);
  text-align: left;
  padding: 7px 12px;
  border-radius: var(--ui-radius, 8px);
  cursor: pointer;
  font-size: 0.9rem;
}
.sidebar-item:hover { background: var(--ui-panel-hover); }
.sidebar-item.is-active { background: var(--ui-panel-2); color: var(--ui-accent); }
.sidebar-icon { width: 1.1em; text-align: center; opacity: 0.85; }
.folder-name { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder-count { color: var(--ui-muted); font-size: 0.78rem; }

/* folder + note tree (Folders tab) */
.tree-toolbar { display: flex; gap: 8px; padding: 10px 10px 6px; flex: 0 0 auto; }
.tree-toolbar-btn { flex: 1 1 0; font-size: 0.85rem; padding: 6px 8px; }
.folder-tree { list-style: none; margin: 0; padding: 0 6px 16px; }
.tree-row { position: relative; display: flex; align-items: center; }
.tree-item {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  border: none;
  background: none;
  color: var(--ui-text);
  text-align: left;
  padding: 6px 8px;
  border-radius: var(--ui-radius, 8px);
  cursor: pointer;
  font-size: 0.9rem;
}
.tree-item:hover { background: var(--ui-panel-hover); }
.tree-item.is-active { background: var(--ui-panel-2); color: var(--ui-accent); }
.tree-twisty { flex: 0 0 auto; width: 1em; text-align: center; font-size: 0.7rem; color: var(--ui-muted); }
.tree-icon { flex: 0 0 auto; opacity: 0.85; }
.note-item .tree-icon { opacity: 0.6; }
.tree-label { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-empty { padding: 12px 10px; }

.folder-menu-btn {
  opacity: 0;
  border: none;
  background: none;
  color: var(--ui-muted);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--ui-radius, 8px);
  font-size: 1rem;
}
.tree-row:hover .folder-menu-btn,
.folder-menu-btn:focus-visible { opacity: 1; }
.folder-menu-btn:hover { background: var(--ui-panel-hover); color: var(--ui-text); }

.tag-cloud { list-style: none; margin: 0; padding: 0 4px; display: flex; flex-wrap: wrap; gap: 4px; }
.tag-pill {
  border: 1px solid var(--ui-border);
  background: var(--ui-panel-2);
  color: var(--ui-text);
  border-radius: var(--ui-radius-pill, 999px);
  padding: 3px 9px;
  font-size: 0.78rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
.tag-pill:hover { background: var(--ui-panel-hover); }
.tag-pill.is-active { background: var(--ui-accent); color: var(--ui-on-accent); border-color: var(--ui-accent); }
.tag-pill-count { opacity: 0.65; font-size: 0.72rem; }
.tag-empty { padding: 2px 6px; font-size: 0.8rem; }

/* ── note list (lives inside the sidebar's Search/Notes panel) ──── */
.list-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--ui-border);
  flex: 0 0 auto;
}
.search-mount { flex: 1 1 auto; }
.search-mount .search-bar { width: 100%; }
.new-note-btn { white-space: nowrap; }
.list-title { margin: 10px 14px 6px; font-size: 0.95rem; color: var(--ui-muted); font-weight: 600; }

.note-list { list-style: none; margin: 0; padding: 0 8px 16px; overflow-y: auto; min-height: 0; flex: 1 1 auto; }
.note-row {
  padding: 10px 12px;
  border-radius: var(--ui-radius, 8px);
  cursor: pointer;
  border: 1px solid transparent;
}
.note-row:hover { background: var(--ui-panel-hover); }
.note-row.is-active { background: var(--ui-panel-2); border-color: var(--ui-border); }
.note-row-title { font-weight: 600; font-size: 0.92rem; margin-bottom: 2px; }
.note-row-excerpt {
  color: var(--ui-muted);
  font-size: 0.8rem;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.note-row-tags { margin-top: 5px; display: flex; flex-wrap: wrap; gap: 4px; }
.note-row-tag { font-size: 0.72rem; color: var(--ui-accent); background: var(--ui-panel-2); padding: 1px 6px; border-radius: var(--ui-radius-pill, 999px); }

.empty-state { padding: 24px 16px; color: var(--ui-muted); text-align: center; font-size: 0.9rem; }

/* ── editor ─────────────────────────────────────────────────────── */
.editor-pane { display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.editor-empty { margin: auto; display: flex; flex-direction: column; gap: 14px; align-items: center; }
.editor { display: flex; flex-direction: column; min-height: 0; flex: 1 1 auto; }

.editor-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--ui-border);
  flex-wrap: wrap;
}
.note-title-input {
  flex: 1 1 240px;
  background: none;
  border: none;
  color: var(--ui-text);
  font-size: 1.25rem;
  font-weight: 600;
  padding: 4px 0;
}
.note-title-input:focus { outline: none; }
.editor-head-actions { display: flex; align-items: center; gap: 8px; }
.note-folder-select {
  background: var(--ui-panel-2);
  border: 1px solid var(--ui-border);
  color: var(--ui-text);
  border-radius: var(--ui-radius, 8px);
  padding: 5px 8px;
  font-size: 0.82rem;
  max-width: 160px;
}

.mode-switch { display: inline-flex; border: 1px solid var(--ui-border); border-radius: var(--ui-radius, 8px); overflow: hidden; }
.mode-btn {
  border: none;
  background: var(--ui-panel);
  color: var(--ui-muted);
  padding: 5px 10px;
  font-size: 0.78rem;
  cursor: pointer;
}
.mode-btn:hover { background: var(--ui-panel-hover); color: var(--ui-text); }
.mode-btn.is-active { background: var(--ui-accent); color: var(--ui-on-accent); }
.save-status { font-size: 0.76rem; color: var(--ui-muted); white-space: nowrap; }
.save-status.is-unsaved { color: var(--ui-warning); }
.save-status.is-saved { color: var(--ui-muted); }
.btn-icon {
  border: none; background: none; cursor: pointer; font-size: 1rem;
  padding: 5px 8px; border-radius: var(--ui-radius, 8px); color: var(--ui-muted);
}
.btn-icon:hover { background: var(--ui-panel-hover); }
.btn-danger-ghost:hover { color: var(--ui-danger); }

.editor-body {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  gap: 0;
}
.editor-body[data-mode="split"] { grid-template-columns: 1fr 1fr; }
.editor-body[data-mode="edit"] { grid-template-columns: 1fr; }
.editor-body[data-mode="preview"] { grid-template-columns: 1fr; }
.editor-body[data-mode="edit"] .preview { display: none; }
.editor-body[data-mode="preview"] .note-body-input { display: none; }

.note-body-input {
  border: none;
  border-right: 1px solid var(--ui-border);
  background: var(--ui-bg);
  color: var(--ui-text);
  resize: none;
  padding: 16px 20px;
  font-family: var(--ui-font-mono, ui-monospace, monospace);
  font-size: 0.9rem;
  line-height: 1.6;
  overflow-y: auto;
}
.note-body-input:focus { outline: none; }
.editor-body[data-mode="edit"] .note-body-input { border-right: none; }
.preview { overflow-y: auto; padding: 16px 24px; }

.backlinks { border-top: 1px solid var(--ui-border); padding: 10px 20px; background: var(--ui-panel); }
.backlinks-label { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.6px; color: var(--ui-muted); margin-bottom: 6px; }
.backlinks-list { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.backlink {
  border: 1px solid var(--ui-border);
  background: var(--ui-panel-2);
  color: var(--ui-text);
  border-radius: var(--ui-radius, 8px);
  padding: 3px 9px;
  font-size: 0.8rem;
  cursor: pointer;
}
.backlink:hover { border-color: var(--ui-accent); color: var(--ui-accent); }

.prompt-input {
  width: 100%;
  background: var(--ui-panel-2);
  border: 1px solid var(--ui-border);
  color: var(--ui-text);
  border-radius: var(--ui-radius, 8px);
  padding: 8px 10px;
  font-size: 0.95rem;
}

/* keyboard shortcuts overlay */
.shortcuts-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 8px 16px;
  margin: 0;
  align-items: center;
}
.shortcuts-grid dt { margin: 0; }
.shortcuts-grid dd { margin: 0; color: var(--ui-muted); }
.shortcuts-grid kbd {
  display: inline-block;
  background: var(--ui-panel-2);
  border: 1px solid var(--ui-border);
  border-radius: 6px;
  padding: 2px 8px;
  font-family: var(--ui-font-mono, ui-monospace, monospace);
  font-size: 0.8rem;
  white-space: nowrap;
}

/* ── markdown prose (.md-body) — used by preview AND the embed view ── */
.md-body { line-height: 1.65; font-size: 0.95rem; word-wrap: break-word; }
.md-body > *:first-child { margin-top: 0; }
.md-body h1, .md-body h2, .md-body h3, .md-body h4, .md-body h5, .md-body h6 {
  line-height: 1.3; margin: 1.2em 0 0.5em; font-weight: 650;
}
.md-body h1 { font-size: 1.6rem; border-bottom: 1px solid var(--ui-border); padding-bottom: 0.2em; }
.md-body h2 { font-size: 1.35rem; border-bottom: 1px solid var(--ui-border); padding-bottom: 0.15em; }
.md-body h3 { font-size: 1.15rem; }
.md-body h4 { font-size: 1rem; }
.md-body p { margin: 0.6em 0; }
.md-body a { color: var(--ui-accent); text-decoration: none; }
.md-body a:hover { text-decoration: underline; }
.md-body ul, .md-body ol { margin: 0.5em 0; padding-left: 1.6em; }
.md-body li { margin: 0.2em 0; }
.md-body code {
  background: var(--ui-panel-2);
  border-radius: 4px;
  padding: 0.1em 0.35em;
  font-family: var(--ui-font-mono, ui-monospace, monospace);
  font-size: 0.86em;
}
.md-body pre {
  background: var(--ui-panel-2);
  border: 1px solid var(--ui-border);
  border-radius: var(--ui-radius, 8px);
  padding: 12px 14px;
  overflow-x: auto;
}
.md-body pre code { background: none; padding: 0; font-size: 0.85rem; }
.md-body blockquote {
  margin: 0.7em 0;
  padding: 0.2em 0 0.2em 1em;
  border-left: 3px solid var(--ui-accent);
  color: var(--ui-muted);
}
.md-body hr { border: none; border-top: 1px solid var(--ui-border); margin: 1.4em 0; }
.md-body img { max-width: 100%; height: auto; border-radius: var(--ui-radius, 8px); }
/* task list (- [ ] / - [x]) */
/* No negative margin on the <li> itself — that would shift its content box
   (and any nested list) left, eating per-level indentation. Instead hang just
   the checkbox into the gutter so the box stays put and nesting indents fully,
   matching plain lists. */
.md-body li.md-task { list-style: none; }
.md-task-check { margin-left: -1.4em; margin-right: 0.5em; vertical-align: middle; accent-color: var(--ui-accent); cursor: pointer; }
.md-task-check:disabled { cursor: default; }
.md-task.is-checked > .md-task-label { text-decoration: line-through; color: var(--ui-muted); }

.md-table { border-collapse: collapse; margin: 0.7em 0; width: 100%; }
.md-table th, .md-table td { border: 1px solid var(--ui-border); padding: 6px 10px; text-align: left; }
.md-table th { background: var(--ui-panel-2); }

.md-wikilink {
  color: var(--ui-accent);
  border-bottom: 1px dotted var(--ui-accent);
  cursor: pointer;
}
.md-wikilink.is-missing { color: var(--ui-muted); border-bottom-color: var(--ui-muted); border-bottom-style: dashed; }
.md-tag {
  color: var(--ui-accent);
  background: var(--ui-panel-2);
  border-radius: var(--ui-radius-pill, 999px);
  padding: 0 7px;
  font-size: 0.85em;
  cursor: pointer;
}
.md-tag:hover { text-decoration: none; background: var(--ui-panel-hover); }

/* ── embed view (iframe) ────────────────────────────────────────── */
.embed-body { background: var(--ui-bg); color: var(--ui-text); margin: 0; padding: 0; display: block; }
.embed-root { padding: 16px 20px; }
.embed-title { margin-top: 0; }
.embed-signin { display: flex; flex-direction: column; gap: 12px; align-items: flex-start; }

/* ── responsive ─────────────────────────────────────────────────── */
@media (max-width: 860px) {
  .sidebar-toggle { display: inline-block; }
  .layout { grid-template-columns: 1fr; position: relative; }
  /* Sidebar becomes a slide-in drawer so the editor gets the full screen. */
  .sidebar {
    position: absolute;
    z-index: var(--ui-z-modal, 50);
    top: 0; bottom: 0; left: 0;
    width: min(86vw, 320px);
    transform: translateX(-100%);
    transition: transform 0.18s ease;
  }
  .sidebar.is-open { transform: translateX(0); }
  .sidebar-backdrop {
    position: absolute; inset: 0; z-index: calc(var(--ui-z-modal, 50) - 1);
    background: rgba(0, 0, 0, 0.4);
  }
  /* Split view is unusable on a narrow screen — hide the Split button so the
     mode switch is a straight Edit ⇄ Preview toggle. (applyEditorMode() also
     coerces a stored 'split' preference to 'edit' on mobile.) */
  .mode-btn[data-mode="split"] { display: none; }
  /* No hover on touch — keep the row action (⋯) menus always visible. */
  .folder-menu-btn { opacity: 1; }
}
