:root {
  --bg: #0e1014;
  --bg-elev: #161a21;
  --bg-elev-2: #1d222b;
  --border: #262d39;
  --text: #e6e9ef;
  --text-dim: #8a93a3;
  --accent: #6da7ff;
  --accent-2: #4f86d8;
  --danger: #e35d6a;
  --ok: #4cc28b;
  --warn: #f0b34d;
  --mono: ui-monospace, SFMono-Regular, "JetBrains Mono", Menlo, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  display: flex;
  flex-direction: column;
}
code, pre, .mono { font-family: var(--mono); }
.muted { color: var(--text-dim); }
.hidden { display: none !important; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.topbar h1 {
  font-size: 14px;
  font-weight: 600;
  margin: 0;
  letter-spacing: 0.02em;
}
.spacer { flex: 1; }
.icon-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text);
  cursor: pointer;
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 16px;
  display: none;
}
.btn {
  background: var(--bg-elev-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-family: inherit;
}
.btn:hover:not(:disabled) { background: #2a3140; }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.primary { background: var(--accent-2); border-color: var(--accent); color: white; }
.btn.primary:hover:not(:disabled) { background: var(--accent); }
.btn.danger { background: var(--danger); border-color: #c8404e; color: white; }
.btn.ghost { background: transparent; }
.status { padding: 2px 8px; font-size: 12px; }
.status.dirty { color: var(--warn); }
.status.ok { color: var(--ok); }
.status.error { color: var(--danger); }

/* ---------- Layout ---------- */
.layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  flex: 1 1 auto;
  min-height: 0;
}
.sidebar {
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.sidebar input[type="search"] {
  margin: 8px;
  padding: 6px 8px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-family: inherit;
  font-size: 13px;
}
.sidebar input[type="search"]:focus { outline: 1px solid var(--accent); }
.tree, .search-results {
  overflow-y: auto;
  flex: 1 1 auto;
  padding: 0 6px 12px;
}
.search-results {
  flex: 0 0 auto;
  max-height: 220px;
  border-bottom: 1px solid var(--border);
}
.search-hit {
  padding: 6px 8px;
  border-radius: 4px;
  cursor: pointer;
}
.search-hit:hover { background: var(--bg-elev-2); }
.search-hit .path { color: var(--accent); font-size: 12px; }
.search-hit .excerpt { color: var(--text-dim); font-size: 12px; margin-top: 2px; }

/* ---------- File tree ---------- */
.tree ul { list-style: none; padding-left: 14px; margin: 0; }
.tree > ul { padding-left: 6px; }
.tree li { margin: 1px 0; }
.tree .node {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 3px 6px;
  border-radius: 4px;
  cursor: pointer;
  user-select: none;
}
.tree .node:hover { background: var(--bg-elev-2); }
.tree .node.active { background: var(--accent-2); color: white; }
.tree .node .label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree .node .mtime { color: var(--text-dim); font-size: 11px; margin-left: 6px; }
.tree .node.active .mtime { color: rgba(255,255,255,0.8); }
.tree .folder > .node::before { content: "▸"; color: var(--text-dim); font-size: 10px; width: 10px; }
.tree .folder.open > .node::before { content: "▾"; }
.tree .file > .node::before { content: "·"; color: var(--text-dim); width: 10px; }
.tree .folder:not(.open) > ul { display: none; }

/* ---------- Main ---------- */
.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}
.breadcrumb {
  padding: 8px 14px;
  border-bottom: 1px solid var(--border);
  font-family: var(--mono);
  font-size: 12px;
}
.editor-host {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
}
.editor-host .cm-editor { height: 100%; font-size: 14px; }
.editor-host .cm-scroller { font-family: var(--mono); }
.preview {
  padding: 24px 32px;
  overflow-y: auto;
  line-height: 1.6;
  flex: 1 1 auto;
}
.preview h1, .preview h2, .preview h3 { color: var(--text); margin-top: 1.4em; }
.preview pre { background: var(--bg-elev); padding: 12px; border-radius: 6px; overflow-x: auto; }
.preview code { background: var(--bg-elev); padding: 1px 4px; border-radius: 3px; font-size: 0.95em; }
.preview a { color: var(--accent); }

/* ---------- Modals ---------- */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.modal-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  padding: 18px;
  width: min(440px, 92vw);
  border-radius: 10px;
}
.modal-card h2 { margin: 0 0 8px 0; font-size: 16px; }
.modal-card input {
  width: 100%; margin: 10px 0;
  padding: 8px; background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  font-family: var(--mono);
}
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }

/* ---------- Login ---------- */
.login-page {
  display: flex; align-items: center; justify-content: center;
}
.login-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px 30px;
  width: min(380px, 92vw);
}
.login-card h1 { font-size: 18px; margin: 0 0 4px; }
.login-card form { display: flex; flex-direction: column; gap: 10px; margin-top: 16px; }
.login-card input {
  padding: 10px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
}
.login-card input:focus { outline: 1px solid var(--accent); }
.login-card button {
  background: var(--accent-2);
  color: white;
  border: none;
  border-radius: 6px;
  padding: 10px;
  cursor: pointer;
  font-size: 14px;
}
.login-card button:hover { background: var(--accent); }
.login-card .error { color: var(--danger); min-height: 1.2em; margin-top: 8px; font-size: 13px; }

/* ---------- Mobile ---------- */
@media (max-width: 720px) {
  .icon-btn { display: inline-block; }
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: fixed; left: 0; top: 49px; bottom: 0; width: 80vw; z-index: 50; transform: translateX(-100%); transition: transform 0.18s; }
  .sidebar.open { transform: translateX(0); }
  .topbar h1 { font-size: 13px; }
  .topbar .btn { padding: 5px 8px; font-size: 12px; }
}
