:root {
  --fs: 15px;
}

[data-theme="light"] {
  --bg: #f6f7f9;
  --bg-elev: #ffffff;
  --bg-sunken: #eef0f3;
  --text: #1c2024;
  --text-dim: #687076;
  --border: #e1e4e8;
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --danger: #d64545;
  --code-bg: #f1f3f5;
  --shadow: 0 1px 3px rgba(0, 0, 0, .08), 0 1px 2px rgba(0, 0, 0, .06);
}

[data-theme="dark"] {
  --bg: #16181d;
  --bg-elev: #1e2127;
  --bg-sunken: #121419;
  --text: #e6e8eb;
  --text-dim: #9ba1a6;
  --border: #2a2e37;
  --accent: #4f8cff;
  --accent-text: #ffffff;
  --danger: #ff6b6b;
  --code-bg: #0f1115;
  --shadow: 0 1px 3px rgba(0, 0, 0, .4);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  display: flex;
  flex-direction: column;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: var(--fs);
  color: var(--text);
  background: var(--bg);
}

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 14px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.topbar-left { display: flex; align-items: center; gap: 10px; }
.topbar-center { flex: 1; display: flex; justify-content: center; }
.topbar-right { display: flex; align-items: center; gap: 6px; }
.brand {
  font-size: 16px;
  font-weight: 700;
  margin: 0;
  white-space: nowrap;
}
.search {
  width: 100%;
  max-width: 440px;
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-sunken);
  color: var(--text);
  font-size: 14px;
}
.search:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.select {
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-sunken);
  color: var(--text);
  font-size: 13px;
}

.icon-btn, .btn, .tool {
  font-family: inherit;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--bg-sunken);
  color: var(--text);
  border-radius: 8px;
  transition: background .12s, border-color .12s, transform .05s;
}
.icon-btn { padding: 6px 9px; font-size: 14px; line-height: 1; }
.icon-btn:hover, .btn:hover, .tool:hover { background: var(--border); }
.icon-btn:active, .btn:active, .tool:active { transform: translateY(1px); }

.btn { padding: 7px 12px; font-size: 13px; font-weight: 600; }
.btn-primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn-primary:hover { filter: brightness(1.05); }

/* Layout */
.layout { flex: 1; display: flex; min-height: 0; }

.sidebar {
  width: 280px;
  flex: 0 0 280px;
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.sidebar.hidden { display: none; }
.sidebar-actions {
  display: flex;
  gap: 8px;
  padding: 10px;
  border-bottom: 1px solid var(--border);
}
.sidebar-actions .btn-primary { flex: 1; }

.note-list { list-style: none; margin: 0; padding: 6px; overflow-y: auto; flex: 1; }
.empty-list { padding: 24px; text-align: center; color: var(--text-dim); font-size: 13px; }

.note-item {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 2px;
  border: 1px solid transparent;
}
.note-item:hover { background: var(--bg-sunken); }
.note-item.active { background: var(--bg-sunken); border-color: var(--accent); }
.note-item .ni-title {
  font-weight: 600;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.note-item .pin-dot { color: var(--accent); flex: 0 0 auto; }
.note-item .ni-snippet {
  color: var(--text-dim);
  font-size: 12px;
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.note-item .ni-date { color: var(--text-dim); font-size: 11px; margin-top: 3px; }

/* Editor area */
.editor-area { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }

.editor-toolbar {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elev);
  flex-wrap: wrap;
}
.tool-group { display: flex; gap: 4px; flex-wrap: wrap; }
.tool { padding: 5px 9px; font-size: 13px; }
.tool.danger { color: var(--danger); }
.tool.danger:hover { background: var(--danger); color: #fff; }
.tool.active { border-color: var(--accent); color: var(--accent); }

/* Segmented view-mode control */
.view-modes { gap: 0; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.view-modes .vm {
  font-family: inherit;
  cursor: pointer;
  border: none;
  border-right: 1px solid var(--border);
  background: var(--bg-sunken);
  color: var(--text-dim);
  padding: 5px 12px;
  font-size: 13px;
  font-weight: 600;
}
.view-modes .vm:last-child { border-right: none; }
.view-modes .vm:hover { background: var(--border); }
.view-modes .vm.active { background: var(--accent); color: var(--accent-text); }

/* Export dropdown menu */
.export-wrap { position: relative; display: inline-block; }
.menu {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 30;
  min-width: 168px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 4px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
}
.menu[hidden] { display: none; }
.menu button {
  font-family: inherit;
  cursor: pointer;
  text-align: left;
  border: none;
  background: none;
  color: var(--text);
  padding: 8px 10px;
  border-radius: 7px;
  font-size: 13px;
}
.menu button:hover { background: var(--bg-sunken); }

.panes { flex: 1; display: flex; min-height: 0; }
.pane { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.pane-preview { border-left: 1px solid var(--border); }
.pane-label {
  flex: 0 0 auto;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-dim);
  padding: 6px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elev);
}

/* View modes: edit-only, preview-only, or split. */
.panes.mode-edit .pane-preview { display: none; }
.panes.mode-preview .pane-editor { display: none; }
/* In a single-pane mode the label is redundant. */
.panes.mode-edit .pane-label,
.panes.mode-preview .pane-label { display: none; }

.editor {
  flex: 1;
  width: 100%;
  resize: none;
  border: none;
  outline: none;
  padding: 18px 22px;
  font-family: ui-monospace, SFMono-Regular, "Cascadia Code", Consolas, monospace;
  font-size: calc(var(--fs) - 1px);
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  tab-size: 2;
}
.preview {
  flex: 1;
  width: 100%;
  overflow-y: auto;
  padding: 18px 26px;
  background: var(--bg-elev);
  line-height: 1.65;
}

.statusbar {
  display: flex;
  gap: 16px;
  align-items: center;
  padding: 6px 14px;
  border-top: 1px solid var(--border);
  background: var(--bg-elev);
  font-size: 12px;
  color: var(--text-dim);
  flex: 0 0 auto;
}
.statusbar .saved { margin-left: auto; }
.statusbar .saved.dirty { color: var(--accent); }

.no-note {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: var(--bg);
  color: var(--text-dim);
}
.no-note.hidden { display: none; }

/* Markdown preview typography */
.markdown-body h1, .markdown-body h2, .markdown-body h3,
.markdown-body h4, .markdown-body h5, .markdown-body h6 {
  margin: 1.2em 0 .5em;
  line-height: 1.25;
}
.markdown-body h1 { font-size: 1.8em; border-bottom: 1px solid var(--border); padding-bottom: .25em; }
.markdown-body h2 { font-size: 1.45em; border-bottom: 1px solid var(--border); padding-bottom: .2em; }
.markdown-body h3 { font-size: 1.2em; }
.markdown-body p { margin: .6em 0; }
.markdown-body a { color: var(--accent); }
.markdown-body code {
  background: var(--code-bg);
  padding: .15em .4em;
  border-radius: 5px;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: .9em;
}
.markdown-body pre {
  background: var(--code-bg);
  padding: 14px 16px;
  border-radius: 10px;
  overflow-x: auto;
}
.markdown-body pre code { background: none; padding: 0; }
.markdown-body blockquote {
  margin: .6em 0;
  padding: .2em 1em;
  border-left: 4px solid var(--accent);
  color: var(--text-dim);
}
.markdown-body ul, .markdown-body ol { padding-left: 1.5em; margin: .5em 0; }
.markdown-body li.task { list-style: none; margin-left: -1.2em; }
.markdown-body hr { border: none; border-top: 1px solid var(--border); margin: 1.2em 0; }
.markdown-body img { max-width: 100%; border-radius: 8px; }
.markdown-body table { border-collapse: collapse; }

/* Toast */
.toast {
  position: fixed;
  bottom: 22px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 13px;
  box-shadow: var(--shadow);
  z-index: 50;
  opacity: 0;
  transition: opacity .2s;
}
.toast.show { opacity: 1; }

@media (max-width: 760px) {
  .sidebar { position: absolute; z-index: 20; height: 100%; box-shadow: var(--shadow); }
  /* Split is too cramped on phones: collapse it to the editor pane. */
  .panes.mode-split .pane-preview { display: none; }
  .panes.mode-split .pane-label { display: none; }
  .topbar-center { display: none; }
}
