/* ============================================================
   YZ.Notes — design tokens (dark default, light via [data-theme])
   ============================================================ */
:root {
  color-scheme: dark;
  --bg: #101418;
  --surface: #171c22;
  --surface-2: #1e252d;
  --border: #2a323c;
  --text: #e6ebf0;
  --text-2: #aab4bf;
  --text-3: #7a8590;
  --accent: #6c5ce7;
  --accent-hover: #7d6ff0;
  --accent-soft: rgba(108, 92, 231, 0.16);
  --danger: #ef4444;
  --danger-soft: rgba(239, 68, 68, 0.14);
  --success: #22c55e;
  --warning: #f59e0b;
  --fs-xs: 0.75rem;
  --fs-sm: 0.85rem;
  --fs-md: 0.95rem;
  --fs-lg: 1.15rem;
  --fs-xl: 1.4rem;
  --fs-2xl: 1.8rem;
  --r-sm: 6px;
  --r-md: 10px;
  --r-lg: 14px;
  --shadow-pop: 0 8px 30px rgba(0, 0, 0, 0.45);
  --sidebar-width: 260px;
  --sidebar-collapsed-width: 64px;
  --header-height: 52px;
}

[data-theme="light"] {
  color-scheme: light;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --border: #d8dde3;
  --text: #1d232a;
  --text-2: #4c5661;
  --text-3: #7a8590;
  --shadow-pop: 0 8px 30px rgba(20, 30, 40, 0.18);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: var(--fs-md);
}

a { color: var(--accent); }

/* ============================================================
   Buttons / forms
   ============================================================ */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: var(--r-sm);
  padding: 8px 14px;
  font-size: var(--fs-sm);
  font-family: inherit;
  cursor: pointer;
  background: var(--surface-2);
  color: var(--text);
  transition: background 0.15s, border-color 0.15s;
}
.btn:hover { background: var(--border); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { background: transparent; color: var(--text-2); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn.danger, .context-menu-item.danger { color: var(--danger); }

.form-group { margin-bottom: 14px; display: flex; flex-direction: column; gap: 6px; }
.form-label { font-size: var(--fs-sm); color: var(--text-2); }
.form-hint { font-size: var(--fs-xs); color: var(--text-3); margin: -8px 0 12px; }
.form-input, select.form-input {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  color: var(--text);
  padding: 9px 11px;
  font-size: var(--fs-md);
  font-family: inherit;
  width: 100%;
}
.form-input:focus { outline: none; border-color: var(--accent); }

/* Color swatches */
.color-swatches { display: flex; flex-wrap: wrap; gap: 6px; }
.color-swatch, .color-swatch-current {
  width: 24px; height: 24px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
}
.color-swatch.selected, .color-swatch-current.selected { border-color: var(--text); }

/* ============================================================
   App shell / sidebar
   ============================================================ */
#app { display: flex; min-height: 100vh; }

#sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: var(--sidebar-width);
  height: 100vh;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  overflow-x: hidden;
  transition: transform 0.2s ease, width 0.2s ease;
  z-index: 30;
}

#main {
  flex: 1;
  margin-left: var(--sidebar-width);
  min-width: 0;
  transition: margin-left 0.2s ease;
  display: flex;
  flex-direction: column;
}

#app-header {
  display: flex;
  align-items: center;
  gap: 4px;
  height: var(--header-height);
  padding: 0 12px;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 10;
}

.menu-btn { display: none; }
.collapse-btn-rail { display: none; color: var(--text-3); }

.sidebar-inner { display: flex; flex-direction: column; gap: 10px; padding: 14px 12px; min-height: 100%; }
.sidebar-brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: var(--fs-lg); padding: 2px 4px 6px; }
.sidebar-brand-icon { font-size: 1.3rem; }

.sidebar-search { position: relative; }
.sidebar-search-results { display: flex; flex-direction: column; gap: 2px; margin-top: 6px; }
.sidebar-search-result {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 6px;
  text-align: left;
  background: transparent;
  border: none;
  color: var(--text);
  padding: 6px 8px;
  border-radius: var(--r-sm);
  cursor: pointer;
  font-size: var(--fs-sm);
}
.sidebar-search-result:hover { background: var(--surface-2); }
.search-result-snippet { flex-basis: 100%; color: var(--text-3); font-size: var(--fs-xs); }
.search-result-snippet mark { background: var(--accent-soft); color: var(--text); }
.sidebar-search-empty { color: var(--text-3); font-size: var(--fs-sm); padding: 4px; }

.sidebar-nav { display: flex; flex-direction: column; gap: 2px; }
.sidebar-nav-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: var(--r-sm);
  color: var(--text-2);
  text-decoration: none;
  font-size: var(--fs-md);
}
.sidebar-nav-link:hover { background: var(--surface-2); color: var(--text); }
.sidebar-nav-link.active { background: var(--accent-soft); color: var(--text); }

.sidebar-filter-row { display: flex; gap: 4px; align-items: center; }
.sidebar-icon-btn { padding: 6px 10px; font-size: var(--fs-lg); line-height: 1; }

/* Document tree */
.sidebar-tree { display: flex; flex-direction: column; gap: 1px; flex: 1; }
.doc-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border-radius: var(--r-sm);
  cursor: pointer;
  user-select: none;
  min-width: 0;
}
.doc-item:hover { background: var(--surface-2); }
.doc-tree-item.active { background: var(--accent-soft); }
.doc-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: var(--fs-sm); }
.category-item { font-weight: 600; margin-top: 6px; }
.sub-documents { padding-left: 14px; border-left: 1px solid var(--border); margin-left: 10px; }
.collapse-btn {
  background: none;
  border: none;
  color: var(--text-3);
  cursor: pointer;
  font-size: 9px;
  padding: 2px 4px;
  line-height: 1;
}
.collapse-spacer { width: 17px; flex: none; }
.entity-icon { flex: none; }
.entity-dot, .doc-dot {
  width: 9px; height: 9px;
  border-radius: 50%;
  display: inline-block;
  flex: none;
}
.drop-target-active { outline: 2px dashed var(--accent); outline-offset: -2px; }
.sidebar-empty { color: var(--text-3); font-size: var(--fs-sm); padding: 8px 4px; }
.sidebar-new-doc { justify-content: center; margin-top: auto; }

/* Desktop rail collapse */
@media (min-width: 769px) {
  .collapse-btn-rail { display: inline-flex; }
  body.sidebar-collapsed #sidebar { width: var(--sidebar-collapsed-width); }
  body.sidebar-collapsed #main { margin-left: var(--sidebar-collapsed-width); }
  body.sidebar-collapsed .sidebar-collapse-hide,
  body.sidebar-collapsed .category-select,
  body.sidebar-collapsed .sidebar-search,
  body.sidebar-collapsed .sidebar-new-doc { display: none !important; }
  body.sidebar-collapsed .sidebar-brand { justify-content: center; padding-left: 0; }
}

/* Mobile drawer */
@media (max-width: 768px) {
  #sidebar { transform: translateX(-100%); width: min(85vw, var(--sidebar-width)); box-shadow: none; }
  body.sidebar-open #sidebar { transform: translateX(0); box-shadow: var(--shadow-pop); }
  #main { margin-left: 0; }
  .menu-btn { display: inline-flex; }
}

/* ============================================================
   Context menu
   ============================================================ */
.context-menu {
  position: fixed;
  z-index: 100;
  min-width: 200px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-pop);
  padding: 5px;
  display: flex;
  flex-direction: column;
}
.context-menu-item {
  background: none;
  border: none;
  color: var(--text);
  text-align: left;
  padding: 8px 12px;
  border-radius: var(--r-sm);
  cursor: pointer;
  font-size: var(--fs-sm);
  font-family: inherit;
}
.context-menu-item:hover { background: var(--surface-2); }
.context-menu-item.danger:hover { background: var(--danger-soft); }
.context-menu-separator { height: 1px; background: var(--border); margin: 5px 4px; }

/* ============================================================
   Modal
   ============================================================ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 16px;
}
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  width: min(460px, 100%);
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow-pop);
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-title { margin: 0; font-size: var(--fs-lg); }
.modal-body { padding: 20px; }
.modal-footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.close-btn { font-size: var(--fs-lg); }

/* ============================================================
   Toast
   ============================================================ */
.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 18px;
  border-radius: var(--r-md);
  box-shadow: var(--shadow-pop);
  z-index: 300;
  font-size: var(--fs-sm);
  transition: opacity 0.3s;
}
.toast-error { border-color: var(--danger); }
.toast-hide { opacity: 0; }

/* ============================================================
   Home / archive views
   ============================================================ */
#view-content { padding: 8px 24px 40px; flex: 1; min-width: 0; }

.view-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.view-header h1 { font-size: var(--fs-2xl); margin: 0; }

.empty-state { text-align: center; padding: 70px 20px; display: flex; flex-direction: column; align-items: center; gap: 12px; }
.empty-state-icon { font-size: 3rem; }
.empty-state-text { color: var(--text-2); max-width: 480px; }

.document-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px; }
.document-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 14px 16px;
  text-align: left;
  cursor: pointer;
  color: var(--text);
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-family: inherit;
}
.document-card:hover { border-color: var(--accent); }
.document-card-title { display: flex; gap: 8px; font-weight: 600; }
.document-card-meta { color: var(--text-3); font-size: var(--fs-xs); }

.archive-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 8px;
  border-bottom: 1px solid var(--border);
}
.archive-title { background: none; border: none; color: var(--text); font-size: var(--fs-md); cursor: pointer; padding: 0; }
.archive-title:hover { color: var(--accent); }
.archive-meta { color: var(--text-3); font-size: var(--fs-xs); margin-left: auto; }

/* ============================================================
   Editor view
   ============================================================ */
.editor-view { display: flex; flex-direction: column; gap: 10px; height: calc(100vh - var(--header-height) - 48px); min-height: 420px; }

.archived-banner {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--warning);
  color: #1d232a;
  border-radius: var(--r-sm);
  padding: 8px 14px;
  font-size: var(--fs-sm);
}

.breadcrumb { display: flex; gap: 6px; align-items: center; font-size: var(--fs-sm); color: var(--text-3); flex-wrap: wrap; }
.breadcrumb a { color: var(--text-3); text-decoration: none; }
.breadcrumb a:hover { color: var(--accent); text-decoration: underline; }
.breadcrumb-current { color: var(--text-2); }

.editor-title-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.editor-title { font-size: var(--fs-xl); margin: 0; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.editor-actions { display: flex; gap: 2px; align-items: center; }
.mode-btn.active { background: var(--accent-soft); color: var(--text); }
.editor-meta-btn { font-size: var(--fs-lg); }

.editor-body { flex: 1; display: flex; gap: 14px; min-height: 0; }
.editor-pane { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; }
.preview-pane {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 18px 22px;
}

.editor-body.mode-write .preview-pane { display: none; }
.editor-body.mode-preview .editor-pane { display: none; }
@media (max-width: 768px) {
  .editor-body.mode-split .preview-pane { display: none; }
  .editor-view { height: calc(100dvh - var(--header-height) - 40px); }
}

.editor-footer { display: flex; justify-content: flex-end; min-height: 20px; }
.editor-status { font-size: var(--fs-xs); color: var(--text-3); }
.editor-status.status-unsaved { color: var(--warning); }
.editor-status.status-error { color: var(--danger); }
.editor-status.status-saved { color: var(--success); }

/* EasyMDE theming to match the app */
.CodeMirror, .editor-toolbar { background: var(--surface) !important; border-color: var(--border) !important; }
.CodeMirror { color: var(--text) !important; height: 100% !important; border-radius: 0 0 var(--r-sm) var(--r-sm); }
.editor-toolbar { border-radius: var(--r-sm) var(--r-sm) 0 0; }
.editor-toolbar a { color: var(--text-2) !important; }
.editor-toolbar a.active, .editor-toolbar a:hover { background: var(--surface-2) !important; }
.EasyMDEContainer .CodeMirror-cursor { border-left-color: var(--text) !important; }
.editor-pane .EasyMDEContainer { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.editor-pane .CodeMirror { flex: 1; }

/* ============================================================
   Markdown preview typography (markdown-body)
   ============================================================ */
.markdown-body { line-height: 1.65; word-wrap: break-word; }
.markdown-body h1, .markdown-body h2, .markdown-body h3 { line-height: 1.3; margin: 1.2em 0 0.5em; }
.markdown-body h1 { font-size: var(--fs-2xl); border-bottom: 1px solid var(--border); padding-bottom: 6px; }
.markdown-body h2 { font-size: var(--fs-xl); }
.markdown-body h3 { font-size: var(--fs-lg); }
.markdown-body p, .markdown-body ul, .markdown-body ol { margin: 0.5em 0; }
.markdown-body code {
  background: var(--surface-2);
  border-radius: 4px;
  padding: 2px 6px;
  font-size: 0.88em;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
.markdown-body pre { background: var(--surface-2); border-radius: var(--r-sm); padding: 14px; overflow-x: auto; }
.markdown-body pre code { background: none; padding: 0; }
.markdown-body blockquote { border-left: 3px solid var(--accent); margin: 0.6em 0; padding: 2px 14px; color: var(--text-2); }
.markdown-body table { border-collapse: collapse; margin: 0.8em 0; display: block; overflow-x: auto; }
.markdown-body th, .markdown-body td { border: 1px solid var(--border); padding: 6px 12px; }
.markdown-body img { max-width: 100%; border-radius: var(--r-sm); }
.markdown-body hr { border: none; border-top: 1px solid var(--border); margin: 1.4em 0; }
.wikilink-missing { color: var(--text-3); text-decoration: underline dotted; cursor: help; }
