*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:           #f8f9fa;
  --surface:      #ffffff;
  --border:       #e2e8f0;
  --text:         #1a202c;
  --text-muted:   #718096;
  --primary:      #4f46e5;
  --primary-h:    #4338ca;
  --danger:       #e53e3e;
  --radius:       8px;
  --shadow:       0 1px 3px rgba(0,0,0,0.1);
  --sidebar-bg:   #1e1b4b;
  --sidebar-text: #c7d2fe;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}

a { color: var(--primary); text-decoration: none; }

button {
  cursor: pointer;
  font: inherit;
  border: none;
  border-radius: var(--radius);
  padding: 0.4rem 0.9rem;
  transition: background 0.15s;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-h); }
.btn-ghost  { background: transparent; color: var(--text); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--bg); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-sm { padding: 0.2rem 0.55rem; font-size: 12px; }

input, textarea, select {
  font: inherit;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.4rem 0.7rem;
  background: var(--surface);
  color: var(--text);
  outline: none;
  width: 100%;
}
input:focus, textarea:focus { border-color: var(--primary); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem;
}

.tag {
  display: inline-flex; align-items: center;
  padding: 0.15rem 0.5rem; border-radius: 999px;
  font-size: 12px; font-weight: 500;
  background: #e0e7ff; color: #3730a3;
}

mark { background: #fef08a; border-radius: 2px; }
.muted { color: var(--text-muted); }
.error-msg { color: var(--danger); font-size: 13px; margin-top: 0.4rem; }

/* ── Layout ────────────────────────────────────────────── */
.layout { display: flex; height: 100vh; overflow: hidden; }

.sidebar {
  width: 220px; min-width: 220px;
  background: var(--sidebar-bg); color: var(--sidebar-text);
  display: flex; flex-direction: column;
  overflow-y: auto;
}
.sidebar-header { padding: 1rem 1rem 0.75rem; border-bottom: 1px solid rgba(255,255,255,0.1); }
.logo { color: #fff; font-size: 1.1rem; font-weight: 700; }

.nav-links { padding: 0.5rem; border-bottom: 1px solid rgba(255,255,255,0.1); }
.nav-links a {
  display: flex; align-items: center; gap: 0.5rem;
  padding: 0.45rem 0.6rem; border-radius: 6px;
  color: var(--sidebar-text); font-weight: 500;
  transition: background 0.15s;
}
.nav-links a:hover, .nav-links a.active { background: rgba(255,255,255,0.1); color: #fff; }

.sidebar-tags { flex: 1; padding: 0.5rem; overflow-y: auto; }
.sidebar-section {
  font-size: 11px; font-weight: 600; letter-spacing: 0.05em;
  text-transform: uppercase; color: #818cf8;
  padding: 0.3rem 0.6rem 0.4rem;
  display: flex; justify-content: space-between; align-items: center;
}
.clear-btn { background: none; border: none; color: #818cf8; font-size: 11px; padding: 0; }

.tag-item {
  display: flex; align-items: center; gap: 0.4rem; width: 100%;
  padding: 0.3rem 0.6rem; border-radius: 6px;
  background: none; color: var(--sidebar-text); text-align: left; font-size: 13px;
  transition: background 0.15s;
}
.tag-item:hover { background: rgba(255,255,255,0.08); }
.tag-item.selected { background: rgba(99,102,241,0.3); color: #fff; }
.tag-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.tag-count { margin-left: auto; font-size: 11px; opacity: 0.6; }

.sidebar-footer { padding: 0.75rem; }
.logout-btn { width: 100%; background: none; color: #818cf8; border: 1px solid rgba(255,255,255,0.15); font-size: 13px; }
.logout-btn:hover { background: rgba(255,255,255,0.08); }

.main { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
.topbar { padding: 0.65rem 1.25rem; border-bottom: 1px solid var(--border); background: var(--surface); }
.topbar input { max-width: 480px; }
.content { flex: 1; overflow-y: auto; padding: 1.25rem 1.5rem; min-height: 0; display: flex; flex-direction: column; }

/* ── Modal ─────────────────────────────────────────────── */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.45);
  display: flex; align-items: center; justify-content: center; z-index: 100;
}
.modal { width: 100%; max-width: 420px; display: flex; flex-direction: column; gap: 1rem; }
.modal h3 { font-size: 1rem; font-weight: 700; }
.modal-actions { display: flex; justify-content: flex-end; gap: 0.5rem; }
.modal label { display: flex; flex-direction: column; gap: 0.3rem; font-weight: 500; }

.tag-checkboxes { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.tag-check { display: flex; align-items: center; gap: 0.25rem; cursor: pointer; font-size: 13px; }
.tag-check input { width: auto; }

/* ── Files page ────────────────────────────────────────── */
.page-header { display: flex; align-items: center; gap: 1rem; margin-bottom: 1rem; }
.page-header h2 { font-size: 1.2rem; font-weight: 700; }
.count { font-size: 0.85rem; color: var(--text-muted); font-weight: 400; margin-left: 0.3rem; }

.drop-zone {
  border: 2px dashed var(--border); border-radius: var(--radius);
  padding: 1.25rem; text-align: center; color: var(--text-muted);
  margin-bottom: 1rem; transition: all 0.2s; font-size: 13px; cursor: pointer;
}
.drop-zone.active { border-color: var(--primary); background: #eef2ff; color: var(--primary); }

.file-table { width: 100%; border-collapse: collapse; }
.file-table th { text-align: left; padding: 0.45rem 0.7rem; border-bottom: 2px solid var(--border); font-size: 12px; color: var(--text-muted); }
.file-table td { padding: 0.55rem 0.7rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
.file-table tr:hover td { background: #f7f8ff; }
.file-table .tags-cell { display: flex; flex-wrap: wrap; gap: 3px; }
.file-table .actions { display: flex; gap: 0.3rem; }
.ocr-done  { color: #38a169; }
.ocr-failed { color: var(--danger); }

/* ── Notes page ────────────────────────────────────────── */
.notes-layout { display: flex; flex: 1; min-height: 0; }
.note-list { width: 250px; min-width: 250px; border-right: 1px solid var(--border); overflow-y: auto; background: var(--surface); }
.note-list-header { padding: 0.6rem; display: flex; gap: 0.4rem; border-bottom: 1px solid var(--border); }
.note-list-header input { flex: 1; }
.note-item {
  display: flex; flex-direction: column; align-items: flex-start;
  width: 100%; padding: 0.65rem 0.9rem; border-bottom: 1px solid var(--border);
  background: none; text-align: left; cursor: pointer; transition: background 0.15s; border-left: 3px solid transparent;
}
.note-item:hover { background: #f7f8ff; }
.note-item.active { background: #eef2ff; border-left-color: var(--primary); }
.note-item-title { font-weight: 500; font-size: 13px; }
.note-item-date { font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.note-item-tags { display: flex; flex-wrap: wrap; gap: 3px; margin-top: 4px; }
.note-item-tags .tag { font-size: 10px; padding: 0.08rem 0.3rem; }

.editor-area { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
.editor-header { display: flex; align-items: center; gap: 0.75rem; padding: 0.75rem 1.25rem; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.title-input { font-size: 1.15rem; font-weight: 700; border: none; border-bottom: 2px solid transparent; border-radius: 0; padding: 0.1rem 0; flex: 1; min-width: 200px; }
.title-input:focus { border-bottom-color: var(--primary); outline: none; }
.save-status { font-size: 12px; color: var(--text-muted); }
.toolbar { display: flex; flex-wrap: wrap; gap: 3px; padding: 0.4rem 1.25rem; border-bottom: 1px solid var(--border); }
.toolbar button { padding: 0.2rem 0.5rem; background: none; border: 1px solid var(--border); font-size: 12px; border-radius: 4px; }
.toolbar button:hover { background: var(--bg); }
.toolbar button.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.editor-body { flex: 1; overflow-y: auto; padding: 1rem 1.25rem; outline: none; line-height: 1.75; }
.editor-body h2 { font-size: 1.2rem; margin: 1rem 0 0.3rem; }
.editor-body h3 { font-size: 1rem; margin: 0.8rem 0 0.3rem; }
.editor-body ul, .editor-body ol { padding-left: 1.5rem; }
.editor-body li { margin: 0.2rem 0; }
.editor-body li.task-item { list-style: none; display: flex; align-items: flex-start; gap: 0.4rem; margin-left: -1.5rem; }
.editor-body li.task-item input { width: auto; margin-top: 3px; }

/* ── Tag Autocomplete ──────────────────────────────────────────────────── */
.tag-autocomplete-host {
  position: relative;
}
.tag-input-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  align-items: center;
  min-height: 2rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.25rem 0.4rem;
  background: var(--surface);
  cursor: text;
}
.tag-autocomplete-input {
  border: none;
  outline: none;
  background: transparent;
  padding: 0.1rem 0.2rem;
  min-width: 80px;
  flex: 1;
  font: inherit;
  font-size: 13px;
}
.tag-badge-removable {
  display: inline-flex;
  align-items: center;
  gap: 0.2rem;
  border-radius: 4px;
  padding: 0.15rem 0.4rem;
  font-size: 12px;
  font-weight: 500;
}
.tag-badge-remove {
  background: transparent;
  border: none;
  padding: 0;
  cursor: pointer;
  font-size: 13px;
  line-height: 1;
  color: inherit;
  opacity: 0.7;
  border-radius: 2px;
}
.tag-badge-remove:hover { opacity: 1; }
.tag-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 100;
  max-height: 200px;
  overflow-y: auto;
}
.tag-dropdown-item {
  padding: 0.4rem 0.7rem;
  cursor: pointer;
  font-size: 13px;
}
.tag-dropdown-item:hover,
.tag-dropdown-item.active {
  background: var(--bg);
}
.no-note-msg { display: flex; align-items: center; justify-content: center; flex: 1; color: var(--text-muted); }

/* ── Document-link panel (notes editor) ───────────────────────────────────── */
#doc-link-panel {
  position: relative;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
#doc-link-search {
  width: 100%;
  font-size: 13px;
}
#doc-link-results {
  margin-top: 0.4rem;
  max-height: 180px;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
#doc-link-results:empty {
  display: none;
}
.doc-link-result {
  padding: 0.35rem 0.6rem;
  cursor: pointer;
  font-size: 13px;
  border-bottom: 1px solid var(--border);
}
.doc-link-result:last-child { border-bottom: none; }
.doc-link-result:hover { background: var(--bg); }
.doc-link-empty {
  padding: 0.5rem 0.6rem;
  font-size: 13px;
  color: var(--text-muted);
}

/* ── Linked-files list (notes editor) ─────────────────────────────────────── */
#linked-files-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  padding: 0 0.75rem;
}
#linked-files-list:empty { padding: 0; }
.linked-file-row {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.2rem 0.5rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 12px;
}
.linked-file-row a {
  color: var(--primary);
  font-weight: 500;
  text-decoration: none;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.linked-file-row a:hover { text-decoration: underline; }

/* ── Sort controls (note list) ────────────────────────────────────────────── */
#sort-controls {
  display: flex;
  gap: 0.4rem;
  align-items: center;
  padding: 0.4rem 0.6rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
#sort-controls #sort-field {
  flex: 1;
  font-size: 12px;
  padding: 0.25rem 0.4rem;
}
#sort-controls #sort-dir {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.2rem 0.5rem;
  font-size: 14px;
  line-height: 1;
  color: var(--text);
}
#sort-controls #sort-dir:hover { background: var(--bg); }

/* ── Console panel ─────────────────────────────────────────────────────────── */
.console-panel {
  display: none;
  position: fixed;
  right: 0; top: 0; bottom: 0;
  width: 440px;
  flex-direction: column;
  background: #0d1117;
  border-left: 1px solid #30363d;
  z-index: 200;
  box-shadow: -4px 0 20px rgba(0,0,0,0.45);
}
.console-panel.console-open { display: flex; }

.console-toolbar {
  display: flex;
  gap: 0.5rem;
  padding: 0.5rem;
  background: #161b22;
  align-items: center;
  flex-shrink: 0;
  border-bottom: 1px solid #30363d;
}
.console-toolbar input, .console-toolbar select {
  flex: 1;
  font-size: 12px;
  background: #0d1117;
  color: #e6edf3;
  border: 1px solid #30363d;
  border-radius: 4px;
  padding: 2px 6px;
}

.console-reconnecting {
  display: none;
  background: #d29922;
  color: #000;
  text-align: center;
  padding: 3px;
  font-size: 12px;
  flex-shrink: 0;
}

.console-output {
  flex: 1;
  overflow-y: auto;
  font-family: monospace;
  font-size: 12px;
  background: #0d1117;
  color: #e6edf3;
  padding: 0.5rem;
}

.console-empty {
  display: none;
  font-size: 13px;
  color: #8b949e;
  padding: 1rem;
  text-align: center;
}

.log-line { padding: 2px 0; white-space: pre-wrap; word-break: break-all; }
.log-ts { color: #8b949e; margin-right: 0.5rem; font-size: 11px; }
.log-level { font-weight: bold; margin-right: 0.5rem; min-width: 5ch; display: inline-block; }
.log-msg { }

.log-error .log-level { color: #f85149; }
.log-warn .log-level { color: #d29922; }
.log-info .log-level { color: #e6edf3; }
.log-debug .log-level, .log-trace .log-level { color: #8b949e; }
.log-fatal .log-level { color: #ff7b72; }
.log-unknown .log-level { color: #6e7681; }

.log-line.hidden { display: none; }
