:root {
  --bg: #0f1117;
  --panel: #171a23;
  --panel-2: #1e2230;
  --border: #2a2f3f;
  --text: #e6e8ef;
  --muted: #8b90a3;
  --accent: #4f8cff;
  --accent-hover: #3d76e8;
  --danger: #e5534b;
  --danger-hover: #cc4038;
  --radius: 10px;
  --shadow: 0 8px 30px rgba(0,0,0,.45);
}

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

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ---------- Views ---------- */
.view { min-height: 100vh; }

/* ---------- Login ---------- */
.login-card {
  max-width: 380px;
  margin: 18vh auto 0;
  padding: 32px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  text-align: center;
}
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card form { margin-top: 18px; display: flex; flex-direction: column; gap: 10px; }
.error-text { color: var(--danger); margin-top: 12px; min-height: 1.2em; }

/* ---------- Inputs / buttons ---------- */
input[type="password"],
input[type="search"],
.title-input,
.meta-input,
.content-input,
.modal-input {
  width: 100%;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 10px 12px;
  font-size: 14px;
  outline: none;
  transition: border-color .15s;
}
input:focus, textarea:focus { border-color: var(--accent); }

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  padding: 9px 14px;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  transition: background .15s, border-color .15s;
  white-space: nowrap;
}
.btn:hover { background: #262c3d; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: rgba(229,83,75,.12); border-color: var(--danger); }
.btn.small { padding: 5px 10px; font-size: 12.5px; border-radius: 6px; }
.btn-block { width: 100%; }

/* ---------- Topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 10px; }
.brand { font-size: 17px; margin: 0; }

/* ---------- Layout ---------- */
.layout {
  display: grid;
  grid-template-columns: 300px 1fr;
  height: calc(100vh - 61px);
}
@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; height: auto; }
  .sidebar { border-right: none; border-bottom: 1px solid var(--border); max-height: 260px; }
}

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 14px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.search { flex-shrink: 0; }

/* Quick tools + filters */
.side-section { display: flex; flex-direction: column; gap: 4px; }
.side-section-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin-bottom: 2px;
}
.quick-tool {
  appearance: none;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  text-align: left;
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 13.5px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  transition: background .12s;
}
.quick-tool:hover { background: var(--panel-2); }
.quick-tool.active { background: var(--panel-2); border-color: var(--accent); }
.quick-tool .count { color: var(--muted); font-size: 11.5px; }

.tag-cloud { display: flex; flex-wrap: wrap; gap: 6px; }
.tag-chip {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 12px;
  padding: 4px 9px;
  border-radius: 20px;
  cursor: pointer;
  transition: background .12s, border-color .12s;
}
.tag-chip:hover { background: #262c3d; }
.tag-chip.active { background: rgba(79,140,255,.14); border-color: var(--accent); color: #bcd2ff; }
.tag-chip .count { color: var(--muted); margin-left: 4px; font-size: 11px; }

.note-list { list-style: none; margin: 0; padding: 0; flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; }
.note-list li { cursor: pointer; }
.note-item {
  padding: 10px 12px;
  border: 1px solid transparent;
  border-radius: 8px;
  transition: background .12s;
  word-break: break-word;
}
.note-item:hover { background: var(--panel-2); }
.note-item.active { background: var(--panel-2); border-color: var(--accent); }
.note-item .note-title { font-size: 14px; font-weight: 600; display: flex; align-items: center; gap: 6px; }
.note-item .note-title .cat-pill { font-size: 10.5px; font-weight: 400; color: var(--accent); border: 1px solid var(--border); border-radius: 10px; padding: 0 6px; white-space: nowrap; }
.note-item .note-date { font-size: 11.5px; color: var(--muted); margin-top: 2px; }
.note-item .note-tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.note-item .note-tags .mini-tag { font-size: 10.5px; color: var(--muted); background: var(--panel-2); border-radius: 8px; padding: 1px 6px; }
.note-count { font-size: 12px; text-align: center; }

/* ---------- Editor ---------- */
.editor-area { display: flex; flex-direction: column; padding: 18px 24px; overflow: hidden; min-height: 0; }
#editor-view { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.empty-state { margin: auto; text-align: center; }

.editor-header { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: 8px; }
.title-input { max-width: 460px; font-size: 17px; font-weight: 600; }
.editor-meta { display: flex; align-items: center; gap: 8px; margin-left: auto; }

.editor-meta-row { display: flex; gap: 10px; margin-bottom: 8px; flex-wrap: wrap; }
.meta-input { max-width: 300px; font-size: 13px; padding: 7px 10px; }
.meta-input:nth-child(2) { max-width: 340px; }

/* ---------- Markdown toolbar ---------- */
.md-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
  margin-bottom: 8px;
  padding: 6px 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.md-toolbar .sep { width: 1px; height: 18px; background: var(--border); margin: 0 4px; }
.tb-btn {
  appearance: none;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  min-width: 30px;
  height: 28px;
  padding: 0 7px;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
  transition: background .12s;
  font-family: inherit;
}
.tb-btn:hover { background: var(--panel-2); }
.tb-btn:active { background: #2a3044; }
.tb-btn.tb-label { cursor: default; font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.tb-btn.tb-label:hover { background: transparent; }

/* ---------- Editor body ---------- */
.editor-body { display: flex; flex: 1; gap: 16px; min-height: 0; }
.content-input {
  flex: 1;
  min-width: 0;
  resize: none;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 14px;
  line-height: 1.6;
  height: 100%;
  background: var(--panel);
}
.preview {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px 24px;
}
/* markdown-it output */
.preview h1, .preview h2, .preview h3 { margin-top: 0; }
.preview h1 { border-bottom: 1px solid var(--border); padding-bottom: .3em; }
.preview pre {
  background: #0b0d12;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  overflow-x: auto;
}
.preview code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; }
.preview :not(pre) > code { background: rgba(79,140,255,.12); padding: 2px 5px; border-radius: 4px; color: #9fbfff; }
.preview blockquote { border-left: 3px solid var(--accent); margin-left: 0; padding-left: 14px; color: var(--muted); }
.preview a { color: var(--accent); }
.preview img { max-width: 100%; border-radius: 6px; }
.preview table { border-collapse: collapse; }
.preview th, .preview td { border: 1px solid var(--border); padding: 6px 10px; }

/* ---------- Modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 16px;
}
.modal-card {
  width: 100%;
  max-width: 480px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--shadow);
}
.modal-card h2 { margin-top: 0; }
.modal-actions { display: flex; gap: 8px; margin-top: 14px; }
.modal-input { margin-top: 8px; }