/* Colours come from one variable set per theme. `data-theme` is stamped on
   <html> by theme.js before first paint, so there is no flash of the wrong
   palette. Everything below refers to variables only. */

:root,
:root[data-theme="dark"] {
  --bg: #14161a;
  --bg-panel: #1a1d23;
  --bg-elevated: #212530;
  --bg-hover: #2b3040;
  --border: #2c313c;
  --border-hover: #3d445a;
  --text: #dfe3ea;
  --text-dim: #8b93a3;
  --accent: #4c9aff;
  --accent-dim: #35507a;
  --accent-hover: #3f5f92;
  --danger: #f2555a;
  --danger-bg: #58252a;
  --ok: #3fb950;
  --warn: #e3b341;
  --active-line: rgba(255, 255, 255, 0.035);
  --backdrop: rgba(0, 0, 0, 0.55);
  --shadow: 0 14px 38px rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg: #ffffff;
  --bg-panel: #f6f7f9;
  --bg-elevated: #eceef2;
  --bg-hover: #dfe3ea;
  --border: #d6dae1;
  --border-hover: #b9c0cb;
  --text: #1f2328;
  --text-dim: #656d76;
  --accent: #2563eb;
  --accent-dim: #dbe7fd;
  --accent-hover: #c7dafc;
  --danger: #cf222e;
  --danger-bg: #ffe0e2;
  --ok: #1a7f37;
  --warn: #9a6700;
  --active-line: rgba(0, 0, 0, 0.04);
  --backdrop: rgba(15, 20, 30, 0.35);
  --shadow: 0 14px 38px rgba(15, 23, 42, 0.18);
  color-scheme: light;
}

:root {
  --radius: 6px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
}

button, input, select, textarea {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}

button {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 5px 10px;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
}
button:hover:not(:disabled) { background: var(--bg-hover); border-color: var(--border-hover); }
button:disabled { opacity: 0.45; cursor: default; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
button.primary { background: var(--accent-dim); border-color: var(--accent); }
button.primary:hover:not(:disabled) { background: var(--accent-hover); }
button.danger:hover:not(:disabled) { background: var(--danger-bg); border-color: var(--danger); }

input[type="text"], input[type="password"], select {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 9px;
  outline: none;
}
input:focus, select:focus { border-color: var(--accent); }

.icon-btn {
  padding: 2px 7px;
  font-size: 16px;
  line-height: 1.25;
  background: none;
  border-color: transparent;
  color: var(--text-dim);
}
.icon-btn:hover:not(:disabled) { color: var(--text); background: var(--bg-elevated); border-color: var(--border); }

/* --- Login ---------------------------------------------------------------- */

.login-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.login-card {
  width: 320px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 28px 26px;
}
.login-card h1 { margin: 0 0 4px; font-size: 20px; }
.login-card p.sub { margin: 0 0 22px; color: var(--text-dim); font-size: 13px; }
.login-card label { display: block; margin-bottom: 6px; color: var(--text-dim); font-size: 12px; }
.login-card input { width: 100%; margin-bottom: 14px; }
.login-card button[type="submit"] { width: 100%; padding: 8px; }
.login-error {
  min-height: 18px;
  margin-top: 12px;
  color: var(--danger);
  font-size: 13px;
  text-align: center;
}

.corner-theme { position: fixed; top: 14px; right: 14px; }

/* --- App layout ----------------------------------------------------------- */

.app {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100%;
}

.sidebar {
  display: flex;
  flex-direction: column;
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
  min-width: 0;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px 8px;
}
.sidebar-head .brand { font-weight: 600; letter-spacing: 0.02em; }
.sidebar-head .head-right { display: flex; align-items: center; gap: 6px; min-width: 0; }
.sidebar-head .user { color: var(--text-dim); font-size: 12px; overflow: hidden; text-overflow: ellipsis; }
.sidebar-head .user a { color: inherit; }

.sidebar-tools {
  display: flex;
  gap: 6px;
  padding: 0 12px 8px;
}
.sidebar-tools button { flex: 1; font-size: 12px; padding: 5px 4px; }
.sidebar-tools button.compact { flex: none; font-size: 14px; padding: 4px 9px; line-height: 1.1; }
.sidebar-tools button.busy { opacity: 0.55; }

.sidebar-search { padding: 0 12px 10px; }
.sidebar-search input { width: 100%; }

.tree {
  flex: 1;
  overflow: auto;
  padding: 0 6px 12px;
}

.tree ul { list-style: none; margin: 0; padding-left: 12px; }
.tree > ul { padding-left: 4px; }

.node {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px;
  border-radius: 4px;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
.node:hover { background: var(--bg-elevated); }
.node.active { background: var(--accent-dim); }
.node .icon { width: 14px; text-align: center; color: var(--text-dim); flex: none; }
.node .label { overflow: hidden; text-overflow: ellipsis; flex: 1; min-width: 0; }
.node .actions { display: none; gap: 2px; flex: none; }
.node:hover .actions { display: flex; }
.node .actions button {
  background: none;
  border: none;
  padding: 1px 4px;
  color: var(--text-dim);
  font-size: 12px;
  line-height: 1;
}
.node .actions button:hover { color: var(--text); background: var(--bg-hover); }
.node .actions button.del:hover { color: var(--danger); }

.tree .collapsed + ul { display: none; }

.empty-hint { padding: 16px 12px; color: var(--text-dim); font-size: 13px; line-height: 1.5; }

/* --- Editor pane ---------------------------------------------------------- */

.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-panel);
}
.toolbar input.title { flex: 1; min-width: 120px; font-family: var(--mono); }
.toolbar .spacer { flex: none; width: 1px; height: 22px; background: var(--border); }
.toolbar select { max-width: 130px; }

.dirty-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: transparent;
  flex: none;
}
.dirty-dot.on { background: var(--warn); }

.editor-host { flex: 1; min-height: 0; position: relative; }
.editor-host .CodeMirror { height: 100%; font-family: var(--mono); font-size: 13px; }

/* Blend whichever CodeMirror theme is active into the app's own palette */
.editor-host .CodeMirror,
.editor-host .CodeMirror-gutters {
  background: var(--bg);
  color: var(--text);
}
.editor-host .CodeMirror-gutters { border-right: 1px solid var(--border); }
.editor-host .CodeMirror-linenumber { color: var(--text-dim); }
.editor-host .CodeMirror-activeline-background { background: var(--active-line); }
.editor-host .CodeMirror-cursor { border-left-color: var(--text); }
.editor-host .CodeMirror-foldmarker {
  color: var(--accent);
  text-shadow: none;
  font-family: var(--mono);
}
.editor-host .CodeMirror-matchingbracket {
  color: var(--accent) !important;
  border-bottom: 1px solid var(--accent);
}

.placeholder-pane {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 10px;
  color: var(--text-dim);
  background: var(--bg);
  text-align: center;
  padding: 24px;
}

.statusbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 5px 14px;
  border-top: 1px solid var(--border);
  background: var(--bg-panel);
  color: var(--text-dim);
  font-size: 12px;
}
.statusbar .grow { flex: 1; }
.statusbar .msg.error { color: var(--danger); }
.statusbar .msg.ok { color: var(--ok); }

/* --- Modals and dialogs --------------------------------------------------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: var(--backdrop);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
}
.modal-backdrop[hidden] { display: none; }

.modal {
  width: min(560px, 100%);
  max-height: 78vh;
  display: flex;
  flex-direction: column;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
}
.modal header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}
.modal .body { overflow: auto; padding: 8px 16px 16px; }
.modal footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

.dialog { width: min(440px, 100%); }
.dialog .body { padding: 16px; }
.dialog .dialog-message {
  margin: 0 0 12px;
  color: var(--text-dim);
  font-size: 13px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}
.dialog .dialog-message:last-child { margin-bottom: 0; }
.dialog .dialog-input {
  width: 100%;
  font-family: var(--mono);
}
.dialog .dialog-hint {
  margin: 8px 0 0;
  color: var(--text-dim);
  font-size: 12px;
}
.dialog[data-danger="true"] .dialog-ok {
  background: var(--danger-bg);
  border-color: var(--danger);
}

.trash-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.trash-row:last-child { border-bottom: none; }
.trash-row .path { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; font-family: var(--mono); font-size: 12px; }
.trash-row .when { color: var(--text-dim); font-size: 12px; flex: none; }

@media (max-width: 720px) {
  .app { grid-template-columns: 1fr; grid-template-rows: 40vh 1fr; }
  .sidebar { border-right: none; border-bottom: 1px solid var(--border); }
}
