/* ── Reset & Base ─────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #1a1b26;
  --bg2: #24253a;
  --bg3: #2d2e44;
  --fg: #c9d1d9;
  --fg2: #8b949e;
  --accent: #7aa2f7;
  --accent2: #bb9af7;
  --green: #9ece6a;
  --orange: #ff9e64;
  --red: #f7768e;
  --border: #363b54;
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
  --sidebar-w: 260px;
  --right-w: 320px;
  --topbar-h: 44px;
  --nav-h: 44px;
}

html, body { height: 100%; overflow: hidden; }
body {
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.6;
}

/* ── Scrollbar ───────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--fg2); }

/* ── Navigation Menu ─────────────────────────────────────────── */
#nav-menu {
  position: fixed; top: 0; left: 0; right: 0;
  height: var(--nav-h);
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 16px;
  z-index: 200;
}
#nav-menu .logo {
  font-weight: 700; font-size: 14px;
  color: var(--accent2); letter-spacing: 1px;
  margin-right: 8px;
}
.nav-links {
  display: flex;
  gap: 4px;
  flex: 1;
}
.nav-links .nav-link {
  color: var(--fg2);
  text-decoration: none;
  font-size: 13px;
  font-weight: 500;
  padding: 6px 12px;
  border-radius: 4px;
  transition: 0.15s;
}
.nav-links .nav-link:hover {
  color: var(--fg);
  background: var(--bg3);
}
.nav-links .nav-link.active {
  color: var(--accent);
  background: var(--bg3);
}
.nav-right {
  display: flex;
  align-items: center;
  gap: 8px;
}
.auth-badge {
  font-size: 14px;
  opacity: 0.7;
}
.nav-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--fg2);
  padding: 4px 14px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  transition: 0.15s;
}
.nav-btn:hover {
  background: var(--bg3);
  color: var(--fg);
  border-color: var(--accent);
}
#btn-login {
  background: var(--accent);
  color: var(--bg);
  border-color: var(--accent);
}
#btn-login:hover {
  background: #8ab4f8;
  border-color: #8ab4f8;
}

/* ── Login Modal ─────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
}
.modal-box {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 32px;
  width: 360px;
  max-width: 90vw;
  box-shadow: 0 8px 32px rgba(0,0,0,0.4);
}
.modal-box h2 {
  font-size: 20px;
  margin-bottom: 4px;
  color: var(--fg);
}
.modal-hint {
  font-size: 13px;
  color: var(--fg2);
  margin-bottom: 20px;
}
.modal-box input[type="password"] {
  width: 100%;
  padding: 10px 14px;
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--fg);
  border-radius: 6px;
  font-size: 14px;
  outline: none;
  transition: 0.15s;
  margin-bottom: 16px;
}
.modal-box input[type="password"]:focus {
  border-color: var(--accent);
}
.modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}
.modal-btn {
  padding: 8px 20px;
  border-radius: 6px;
  border: 1px solid var(--border);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: 0.15s;
}
.modal-btn.primary {
  background: var(--accent);
  color: var(--bg);
  border-color: var(--accent);
}
.modal-btn.primary:hover { background: #8ab4f8; }
.modal-btn.secondary {
  background: none;
  color: var(--fg2);
}
.modal-btn.secondary:hover { background: var(--bg3); color: var(--fg); }
.modal-error {
  color: var(--red);
  font-size: 13px;
  margin-top: 12px;
  text-align: center;
}

/* ── Topbar ──────────────────────────────────────────────────── */
#topbar {
  position: fixed; top: var(--nav-h); left: 0; right: 0;
  height: var(--topbar-h);
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center;
  padding: 0 16px;
  gap: 12px;
  z-index: 100;
}
#topbar .note-title {
  flex: 1; font-size: 13px; color: var(--fg2);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.topbar-actions { display: flex; gap: 4px; }
.topbar-actions button {
  background: none; border: 1px solid var(--border);
  color: var(--fg2); padding: 4px 10px; border-radius: 4px;
  cursor: pointer; font-size: 14px; transition: 0.15s;
}
.topbar-actions button:hover {
  background: var(--bg3); color: var(--fg); border-color: var(--accent);
}

/* ── App Layout ──────────────────────────────────────────────── */
#app {
  display: flex;
  height: calc(100vh - var(--topbar-h) - var(--nav-h));
  margin-top: calc(var(--topbar-h) + var(--nav-h));
}

/* ── Sidebar ─────────────────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  overflow: hidden;
}
.sidebar-section { padding: 8px 10px; }
.sidebar-section + .sidebar-section { border-top: 1px solid var(--border); }
.section-label {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px;
  color: var(--fg2); margin-bottom: 6px; font-weight: 600;
}

#search-input {
  width: 100%; padding: 6px 10px;
  background: var(--bg3); border: 1px solid var(--border);
  color: var(--fg); border-radius: 4px; font-size: 13px;
  outline: none; transition: 0.15s;
}
#search-input:focus { border-color: var(--accent); }

#file-tree, #search-results {
  overflow-y: auto; flex: 1;
  max-height: calc(100vh - var(--topbar-h) - var(--nav-h) - 120px);
}
#search-results-container { flex: 1; overflow-y: auto; }

.tree-item, .result-item {
  display: flex; align-items: center;
  padding: 3px 8px; border-radius: 3px; cursor: pointer;
  font-size: 13px; white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; transition: 0.1s;
}
.tree-item:hover, .result-item:hover { background: var(--bg3); }
.tree-item.active, .result-item.active { background: var(--bg3); color: var(--accent); }
.tree-item.folder { color: var(--fg2); font-size: 12px; font-weight: 600; }
.tree-item .icon { width: 18px; text-align: center; flex-shrink: 0; }
.tree-item .name { margin-left: 4px; overflow: hidden; text-overflow: ellipsis; }
.tree-children { padding-left: 16px; }

.result-item .excerpt {
  font-size: 11px; color: var(--fg2); margin-top: 2px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.empty-state { color: var(--fg2); font-size: 13px; padding: 20px; text-align: center; }

/* ── Editor Pane ─────────────────────────────────────────────── */
#editor-pane {
  flex: 1; display: flex; flex-direction: column; overflow: hidden;
  position: relative;
}

#empty-state {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  height: 100%; color: var(--fg2);
}
#empty-state .empty-icon { font-size: 48px; margin-bottom: 16px; opacity: 0.4; }
#empty-state h2 { font-size: 20px; margin-bottom: 8px; color: var(--fg); }
#empty-state code { background: var(--bg3); padding: 2px 6px; border-radius: 3px; font-size: 13px; }

.split-view { display: flex; height: 100%; }
#edit-pane, #preview-pane { flex: 1; overflow-y: auto; position: relative; }
#edit-pane { border-right: 1px solid var(--border); }

#editor {
  width: 100%; height: 100%; padding: 24px 28px;
  background: var(--bg); color: var(--fg);
  border: none; outline: none; resize: none;
  font-family: var(--font-mono); font-size: 14px;
  line-height: 1.7; tab-size: 2;
}
#editor::placeholder { color: var(--fg2); opacity: 0.5; }

#preview {
  padding: 24px 28px; max-width: 720px; min-height: 100%;
  line-height: 1.7;
}
#preview h1, #preview h2, #preview h3, #preview h4 {
  margin-top: 1.2em; margin-bottom: 0.4em; color: var(--fg); font-weight: 600;
}
#preview h1 { font-size: 1.6em; border-bottom: 1px solid var(--border); padding-bottom: 6px; }
#preview h2 { font-size: 1.3em; }
#preview h3 { font-size: 1.1em; }
#preview p { margin-bottom: 0.8em; }
#preview a { color: var(--accent); text-decoration: none; }
#preview a:hover { text-decoration: underline; }
#preview code {
  background: var(--bg3); padding: 2px 5px; border-radius: 3px;
  font-family: var(--font-mono); font-size: 13px;
}
#preview pre { background: var(--bg3); padding: 12px 16px; border-radius: 6px; overflow-x: auto; margin-bottom: 1em; }
#preview pre code { background: none; padding: 0; }
#preview blockquote {
  border-left: 3px solid var(--accent); padding-left: 12px;
  color: var(--fg2); margin-bottom: 0.8em;
}
#preview ul, #preview ol { padding-left: 24px; margin-bottom: 0.8em; }
#preview li { margin-bottom: 0.3em; }
#preview img { max-width: 100%; border-radius: 4px; }
#preview hr { border: none; border-top: 1px solid var(--border); margin: 1.5em 0; }
#preview table { border-collapse: collapse; width: 100%; margin-bottom: 1em; }
#preview th, #preview td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
#preview th { background: var(--bg3); font-weight: 600; }
#preview .tag { color: var(--accent2); }
#preview .wiki-link { color: var(--green); cursor: pointer; }
#preview .wiki-link:hover { text-decoration: underline; }
#preview .wiki-link.missing { color: var(--red); }

/* ── Right Panel ─────────────────────────────────────────────── */
#right-panel {
  width: var(--right-w);
  min-width: var(--right-w);
  background: var(--bg2);
  border-left: 1px solid var(--border);
  display: flex; flex-direction: column;
  overflow: hidden;
}
.right-tabs { display: flex; border-bottom: 1px solid var(--border); }
.right-tabs .tab {
  flex: 1; padding: 8px; text-align: center; font-size: 12px;
  font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px;
  background: none; border: none; color: var(--fg2); cursor: pointer;
  border-bottom: 2px solid transparent; transition: 0.15s;
}
.right-tabs .tab:hover { color: var(--fg); }
.right-tabs .tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.right-content { display: none; flex: 1; overflow-y: auto; }
.right-content.active { display: flex; flex-direction: column; }

#backlinks-panel { padding: 12px; }
#backlinks-panel .hint { color: var(--fg2); font-size: 13px; text-align: center; margin-top: 20px; }
.backlink-item {
  padding: 8px 10px; border-radius: 4px; cursor: pointer;
  margin-bottom: 4px; transition: 0.1s;
}
.backlink-item:hover { background: var(--bg3); }
.backlink-item .bl-name { font-size: 13px; color: var(--accent); font-weight: 500; }
.backlink-item .bl-snippet { font-size: 12px; color: var(--fg2); margin-top: 2px; }

#graph-panel { padding: 0; }
#graph-svg { width: 100%; height: 100%; }

/* ── Toast ───────────────────────────────────────────────────── */
#toast {
  position: fixed; bottom: 20px; right: 20px;
  background: var(--bg3); border: 1px solid var(--border);
  color: var(--fg); padding: 10px 18px; border-radius: 6px;
  font-size: 13px; opacity: 0; transition: opacity 0.3s;
  z-index: 999; pointer-events: none;
}
#toast.show { opacity: 1; }

/* ── Page View ───────────────────────────────────────────────── */
#page-view {
  line-height: 1.7;
}
#page-view h1, #page-view h2, #page-view h3, #page-view h4 {
  margin-top: 1.2em; margin-bottom: 0.4em; color: var(--fg); font-weight: 600;
}
#page-view h1 { font-size: 2em; border-bottom: 1px solid var(--border); padding-bottom: 8px; }
#page-view h2 { font-size: 1.5em; }
#page-view h3 { font-size: 1.2em; }
#page-view p { margin-bottom: 0.8em; }
#page-view a { color: var(--accent); text-decoration: none; }
#page-view a:hover { text-decoration: underline; }
#page-view code {
  background: var(--bg3); padding: 2px 5px; border-radius: 3px;
  font-family: var(--font-mono); font-size: 13px;
}
#page-view pre { background: var(--bg3); padding: 12px 16px; border-radius: 6px; overflow-x: auto; margin-bottom: 1em; }
#page-view pre code { background: none; padding: 0; }
#page-view blockquote {
  border-left: 3px solid var(--accent); padding-left: 12px;
  color: var(--fg2); margin-bottom: 0.8em;
}
#page-view ul, #page-view ol { padding-left: 24px; margin-bottom: 0.8em; }
#page-view li { margin-bottom: 0.3em; }
#page-view img { max-width: 100%; border-radius: 4px; }
#page-view table { border-collapse: collapse; width: 100%; margin-bottom: 1em; }
#page-view th, #page-view td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
#page-view th { background: var(--bg3); font-weight: 600; }
#page-view .wiki-link { color: var(--green); cursor: pointer; }
#page-view .wiki-link:hover { text-decoration: underline; }
#page-view .wiki-link.missing { color: var(--red); }
