:root {
  --bg: #0f172a;
  --bg-elev: #1e293b;
  --bg-input: #0b1220;
  --fg: #e2e8f0;
  --fg-muted: #94a3b8;
  --accent: #38bdf8;
  --accent-hover: #0ea5e9;
  --danger: #f87171;
  --border: #334155;
  --shadow: 0 8px 24px rgba(0,0,0,0.4);
  --radius: 10px;
}

[data-theme="light"] {
  --bg: #f8fafc;
  --bg-elev: #ffffff;
  --bg-input: #f1f5f9;
  --fg: #0f172a;
  --fg-muted: #475569;
  --accent: #0284c7;
  --accent-hover: #0369a1;
  --danger: #dc2626;
  --border: #cbd5e1;
  --shadow: 0 8px 24px rgba(15,23,42,0.1);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--fg);
  -webkit-font-smoothing: antialiased;
  font-size: 15px;
  line-height: 1.5;
}

#app { min-height: 100%; display: flex; flex-direction: column; }

.muted { color: var(--fg-muted); }
.small { font-size: 0.85em; }
.error-message { color: var(--danger); margin: 0; min-height: 1.2em; }

button {
  font: inherit;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--fg);
  border-radius: var(--radius);
  padding: 0.55em 1em;
  transition: background 0.12s, border-color 0.12s;
}
button:hover { border-color: var(--accent); }
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #021a2b;
  font-weight: 600;
}
button.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
button.ghost { background: transparent; }
button.danger { color: var(--danger); border-color: var(--danger); }
button.danger:hover { background: var(--danger); color: white; }

input, textarea, select {
  font: inherit;
  background: var(--bg-input);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.55em 0.8em;
  width: 100%;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

label { display: block; margin-bottom: 0.8em; }
label > span { display: block; margin-bottom: 0.3em; color: var(--fg-muted); font-size: 0.9em; }

/* Auth */
.auth-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  gap: 1.5rem;
}
.auth-header { text-align: center; }
.auth-header h1 { margin: 0 0 0.4em; font-size: clamp(2rem, 5vw, 2.6rem); letter-spacing: -0.02em; }
.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow);
  width: min(420px, 100%);
}
.tabs { display: flex; margin: -1.5rem -1.5rem 1rem; border-bottom: 1px solid var(--border); }
.tab {
  flex: 1;
  border: none;
  border-radius: 0;
  background: transparent;
  padding: 0.9em 1em;
  border-bottom: 2px solid transparent;
}
.tab.active { border-bottom-color: var(--accent); color: var(--accent); }
form .primary { width: 100%; padding: 0.8em; }

/* App shell */
.app-shell {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1em;
  padding: 0.8em 1em;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { font-weight: 700; letter-spacing: 0.02em; color: var(--accent); }
.topbar-actions { display: flex; gap: 0.4em; flex-wrap: wrap; }
.topbar-actions button { padding: 0.45em 0.8em; font-size: 0.9em; }

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 320px 1fr;
  min-height: 0;
}
.sidebar {
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.search-row { padding: 0.8em; display: flex; gap: 0.5em; border-bottom: 1px solid var(--border); }
.search-row input { flex: 1; }
.tag-filter { padding: 0.5em 0.8em; display: flex; gap: 0.3em; flex-wrap: wrap; border-bottom: 1px solid var(--border); }
.tag-pill {
  font-size: 0.8em;
  padding: 0.25em 0.6em;
  border: 1px solid var(--border);
  border-radius: 999px;
  cursor: pointer;
  background: transparent;
  color: var(--fg-muted);
}
.tag-pill.active { background: var(--accent); color: #021a2b; border-color: var(--accent); }
.note-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.note-item {
  padding: 0.9em 1em;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.note-item:hover, .note-item.active { background: rgba(56,189,248,0.08); }
.note-item h3 { margin: 0 0 0.2em; font-size: 1em; }
.note-item p { margin: 0; font-size: 0.85em; color: var(--fg-muted); }

.editor { padding: 1.2em; overflow-y: auto; }
.note-editor { display: flex; flex-direction: column; gap: 0.8em; max-width: 800px; margin: 0 auto; }
.note-editor textarea { min-height: 300px; resize: vertical; font-family: ui-monospace, "SF Mono", Menlo, monospace; }
.editor-toolbar { display: flex; align-items: center; gap: 0.6em; flex-wrap: wrap; }
.preview {
  border-top: 1px solid var(--border);
  padding-top: 1em;
  margin-top: 0.5em;
  word-wrap: break-word;
}
.preview pre {
  background: var(--bg-input);
  padding: 0.8em;
  border-radius: var(--radius);
  overflow-x: auto;
}
.preview code { font-family: ui-monospace, "SF Mono", Menlo, monospace; }

.empty-state { max-width: 480px; margin: 4em auto; text-align: center; color: var(--fg-muted); }

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 1em;
}
.modal {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: min(720px, 100%);
  max-height: 90vh;
  overflow-y: auto;
  padding: 1em 1.4em;
  box-shadow: var(--shadow);
}
.modal header { display: flex; align-items: center; justify-content: space-between; gap: 1em; }
.audit-table { width: 100%; border-collapse: collapse; font-size: 0.9em; }
.audit-table th, .audit-table td { text-align: left; padding: 0.5em; border-bottom: 1px solid var(--border); }
.audit-table th { color: var(--fg-muted); font-weight: 600; }

/* Mobile */
@media (max-width: 720px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { max-height: 50vh; }
  .topbar-actions button { font-size: 0.8em; padding: 0.4em 0.6em; }
}

/* Toast */
.toast {
  position: fixed;
  bottom: 1em;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-elev);
  border: 1px solid var(--border);
  padding: 0.7em 1.1em;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 100;
  animation: slideUp 0.2s ease-out;
}
.toast.error { border-color: var(--danger); color: var(--danger); }
@keyframes slideUp {
  from { transform: translate(-50%, 1em); opacity: 0; }
  to { transform: translate(-50%, 0); opacity: 1; }
}
