:root {
  --bg: #0a1122;
  --rail: #0c1830;
  --panel: #0c1426;
  --panel-head: #0c1426;
  --surface: #0b111d;
  --border: #1b263b;
  --border-strong: #26344f;
  --text: #e2e8f0;
  --muted: #8a97ad;
  --faint: #56627a;
  --accent: #22d3ee;
  --accent-soft: rgba(34, 211, 238, 0.12);
  --green: #34d399;
  --warn: #fbbf24;
  --danger: #f87171;
  --btn: #111b2f;
  --btn-hover: #17233b;
  --selection: rgba(56, 139, 253, 0.28);
  --mono: ui-monospace, "JetBrains Mono", "Fira Code", "Cascadia Code", "DejaVu Sans Mono", monospace;
  --sans: "Inter", "Noto Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: dark;
}

[data-theme="light"] {
  --bg: #eef2f7;
  --rail: #e2e8f2;
  --panel: #ffffff;
  --panel-head: #ffffff;
  --surface: #fbfcfe;
  --border: #dbe2ec;
  --border-strong: #c7d1df;
  --text: #1e293b;
  --muted: #5b6b82;
  --faint: #94a3b8;
  --accent: #0891b2;
  --accent-soft: rgba(8, 145, 178, 0.1);
  --green: #059669;
  --warn: #b45309;
  --danger: #dc2626;
  --btn: #f1f5f9;
  --btn-hover: #e2e8f0;
  --selection: rgba(8, 145, 178, 0.2);
  color-scheme: light;
}

* { box-sizing: border-box; }
* { scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }
html, body { height: 100%; }
body {
  margin: 0;
  display: flex;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 var(--sans);
  overflow: hidden;
}
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* --- Rail + sidebar ------------------------------------------------------ */

.rail {
  width: 34px;
  flex: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 10px 0;
  background: var(--rail);
  border-right: 1px solid var(--border);
}
.rail-btn {
  width: 26px;
  height: 26px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 6px;
  background: none;
  color: var(--muted);
  cursor: pointer;
}
.rail-btn svg {
  width: 17px;
  height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.rail-btn:hover { color: var(--text); background: var(--btn-hover); }
.rail-btn.active { color: var(--accent); background: var(--accent-soft); }
.rail-spacer { flex: 1; }
.live-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--faint);
  margin-bottom: 4px;
}
.live-dot.on { background: var(--green); box-shadow: 0 0 0 3px rgba(52, 211, 153, 0.15); }
.live-dot.off { background: var(--danger); }

.sidebar {
  width: 270px;
  flex: none;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--border);
  min-height: 0;
}
.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 12px 8px;
  font-weight: 600;
}
.icon-btn {
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
}
.icon-btn:hover { color: var(--text); }
.sidebar-head-actions { display: flex; align-items: center; gap: 2px; }
#new-file-btn { font-size: 20px; }
.close-file {
  border: 0;
  background: none;
  color: var(--faint);
  font-size: 15px;
  line-height: 1;
  padding: 0 3px;
  border-radius: 4px;
  cursor: pointer;
}
.close-file:hover { color: var(--text); background: var(--btn-hover); }
.CodeMirror pre.CodeMirror-placeholder { color: var(--faint); font-style: italic; }
.empty-state {
  max-width: 460px;
  margin: 12vh auto 0;
  padding: 0 24px;
  text-align: center;
  color: var(--muted);
}
.empty-state h3 { margin: 0 0 8px; color: var(--text); font-size: 18px; }
.empty-state p { margin: 0 0 16px; line-height: 1.6; }
.empty-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.empty-actions .tool { padding: 6px 12px; }
.name-input {
  width: 100%;
  margin: 4px 0 6px;
  padding: 7px 10px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font: 13px var(--mono);
  outline: none;
}
.name-input:focus { border-color: var(--accent); }
.source-bar {
  display: flex;
  gap: 6px;
  padding: 0 10px 8px;
}
.source-bar .tool { flex: 1; padding: 5px 8px; white-space: nowrap; }
.source-bar.stacked { flex-wrap: wrap; }
.source-bar.stacked .source-note { flex-basis: 100%; }
.source-note { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.5; }
.source-note code { font: 11px var(--mono); color: var(--text); }
body.dropping .panes { outline: 2px dashed var(--accent); outline-offset: -6px; border-radius: 12px; }
.filter {
  margin: 0 10px 8px;
  padding: 6px 9px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  outline: none;
}
.filter:focus { border-color: var(--accent); }
.sidebar-list {
  flex: 1;
  overflow: auto;
  padding: 0 6px 10px;
}
.sidebar-foot {
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  color: var(--faint);
  font: 11px var(--mono);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  direction: rtl;
  text-align: left;
}
.sidebar-foot span { direction: ltr; unicode-bidi: isolate; }
.tree-dir {
  padding: 6px 6px 2px;
  color: var(--faint);
  font-size: 11px;
  letter-spacing: 0.02em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tree-file, .tree-folder {
  --depth: 0;
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: none;
  padding: 4px 8px 4px calc(8px + var(--depth) * 14px + 16px);
  border-radius: 5px;
  color: var(--muted);
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tree-file:hover, .tree-file.kbd { background: var(--btn-hover); color: var(--text); }
.tree-file.current { color: var(--accent); background: var(--accent-soft); }
.tree-file .sub { color: var(--faint); font-size: 11px; margin-left: 6px; }
.tree-file.flat { padding-left: 8px; }
.tree-folder {
  display: flex;
  align-items: center;
  gap: 4px;
  padding-left: calc(8px + var(--depth) * 14px);
  border-radius: 5px;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}
.tree-folder:hover { background: var(--btn-hover); }
.tree-folder .chev {
  width: 12px;
  flex: none;
  display: inline-grid;
  place-items: center;
  color: var(--faint);
  transition: transform 0.12s;
}
.tree-folder .chev::before { content: "▸"; font-size: 11px; }
.tree-folder[aria-expanded="true"] .chev { transform: rotate(90deg); }
.tree-folder .name { overflow: hidden; text-overflow: ellipsis; }
.tree-folder .count { margin-left: auto; color: var(--faint); font-size: 11px; }
.empty { color: var(--faint); padding: 10px 8px; }

/* --- Main layout --------------------------------------------------------- */

.app {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 6px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--rail);
}
.brand { display: flex; align-items: center; gap: 8px; min-width: 0; }
.logo {
  width: 34px;
  height: 34px;
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  background: var(--panel);
  font: 700 13px var(--mono);
  letter-spacing: -0.04em;
  white-space: nowrap;
}
.logo span { color: var(--accent); }
.brand-title { font-size: 14px; font-weight: 600; }
.brand-sub {
  color: var(--muted);
  font-size: 11px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.topbar-right { display: flex; align-items: center; gap: 10px; flex: none; }
.pill {
  border: 1px solid var(--border);
  background: var(--btn);
  border-radius: 999px;
  padding: 4px 11px;
  cursor: pointer;
}
.pill:hover { background: var(--btn-hover); }

.seg {
  display: inline-flex;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
}
.seg button {
  border: 0;
  background: none;
  padding: 3px 9px;
  border-radius: 4px;
  color: var(--muted);
  cursor: pointer;
}
.seg button:hover { color: var(--text); }
.seg button.active { color: var(--accent); background: var(--accent-soft); }
.seg-sm button { font-size: 11px; padding: 2px 7px; }

.pill.connected { border-color: color-mix(in srgb, var(--green) 45%, var(--border)); }
.pill.connected::before {
  content: "";
  display: inline-block;
  width: 7px;
  height: 7px;
  margin-right: 6px;
  border-radius: 50%;
  background: var(--green);
  vertical-align: 1px;
}

.dialog {
  width: min(440px, 92vw);
  padding: 18px 20px 16px;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.4);
}
.dialog::backdrop { background: rgba(5, 10, 20, 0.55); }
.dialog h2 { margin: 0 0 10px; font-size: 15px; }
.dialog-close { position: absolute; top: 10px; right: 10px; margin: 0; }
.dialog-body { color: var(--muted); line-height: 1.55; }
.dialog-body p { margin: 0 0 10px; }
.dialog-body code { font: 12px var(--mono); color: var(--text); background: var(--btn); padding: 1px 5px; border-radius: 4px; }
.dialog-body .hint { font-size: 12px; color: var(--faint); }
.dialog-body .error { color: var(--danger); }
.dialog-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; margin-top: 14px; }
.dialog-actions .tool { padding: 5px 12px; }
.dialog-actions .primary { border-color: var(--accent); color: var(--accent); }
.code-input {
  width: 100%;
  margin: 4px 0 8px;
  padding: 8px 10px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font: 600 22px var(--mono);
  letter-spacing: 0.3em;
  text-align: center;
  outline: none;
}
.code-input:focus { border-color: var(--accent); }
.instances { display: flex; flex-direction: column; gap: 6px; margin-top: 6px; }
.instances .tool { display: flex; justify-content: space-between; gap: 10px; padding: 7px 10px; text-align: left; }
.instances .tool span { color: var(--faint); font: 11px var(--mono); }

.banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 10px 12px 0;
  padding: 7px 12px;
  border: 1px solid color-mix(in srgb, var(--warn) 45%, transparent);
  border-radius: 7px;
  background: color-mix(in srgb, var(--warn) 10%, transparent);
  color: var(--text);
}
.banner.error {
  border-color: color-mix(in srgb, var(--danger) 45%, transparent);
  background: color-mix(in srgb, var(--danger) 10%, transparent);
}
.banner-actions { display: flex; gap: 6px; }

.panes {
  flex: 1;
  min-height: 0;
  display: flex;
  padding: 12px;
  --split: 50%;
}
.pane {
  min-width: 0;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--panel);
  overflow: hidden;
}
#editor-pane { flex: 0 0 calc(var(--split) - 6px); }
#preview-pane { flex: 1 1 0; }
.panes[data-layout="editor"] #editor-pane { flex: 1 1 0; }
.panes[data-layout="editor"] #preview-pane,
.panes[data-layout="editor"] .divider,
.panes[data-layout="preview"] #editor-pane,
.panes[data-layout="preview"] .divider { display: none; }

.divider {
  flex: none;
  width: 12px;
  cursor: col-resize;
  position: relative;
}
.divider::after {
  content: "";
  position: absolute;
  inset: 40% 5px;
  border-radius: 2px;
  background: var(--border);
  transition: background 0.15s;
}
.divider:hover::after, .divider.dragging::after { background: var(--accent); }
body.resizing { cursor: col-resize; user-select: none; }
body.resizing iframe, body.resizing .pane-body { pointer-events: none; }

.pane-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  padding: 8px 12px;
  background: var(--panel-head);
  min-height: 40px;
}
.pane-title {
  display: flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
  white-space: nowrap;
}
.dot { width: 7px; height: 7px; border-radius: 50%; flex: none; }
.dot-green { background: var(--green); }
.dot-cyan { background: var(--accent); }
.filename {
  color: var(--muted);
  font: 12px var(--mono);
  overflow: hidden;
  text-overflow: ellipsis;
}
.dirty { color: var(--warn); font-size: 11px; }
.flash {
  color: var(--green);
  font-size: 11px;
  opacity: 0;
  transition: opacity 0.4s;
}
.flash.show { opacity: 1; transition: none; }
.stats { color: var(--faint); font-size: 11px; }
.pane-tools { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.tool {
  border: 1px solid var(--border);
  background: var(--btn);
  border-radius: 5px;
  padding: 3px 9px;
  font-size: 12px;
  cursor: pointer;
}
.tool:hover { background: var(--btn-hover); }
.tool:disabled { opacity: 0.4; cursor: default; }
.menu { position: relative; display: inline-flex; }
.menu-list {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  min-width: 190px;
  padding: 4px;
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  background: var(--panel);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.3);
}
.menu-list button {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  border: 0;
  background: none;
  padding: 6px 10px;
  border-radius: 4px;
  font-size: 12px;
  text-align: left;
  white-space: nowrap;
  cursor: pointer;
}
.menu-list button:hover { background: var(--btn-hover); }
.menu-list button span { color: var(--faint); font-family: var(--mono); }
.check {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
  user-select: none;
}
.check input { margin: 0; accent-color: var(--accent); }

.pane-body {
  flex: 1;
  min-height: 0;
  margin: 0 8px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
}

/* --- Editor (CodeMirror 5) ----------------------------------------------- */

.editor-body { overflow: hidden; }
.editor-body .CodeMirror {
  height: 100%;
  background: transparent;
  color: var(--text);
  font: 12.5px/1.55 var(--mono);
}
.CodeMirror-gutters { background: transparent; border-right: 0; }
.CodeMirror-linenumber { color: var(--faint); padding: 0 14px 0 8px; font-size: 11px; }
.CodeMirror-lines { padding: 10px 0; }
.CodeMirror pre.CodeMirror-line, .CodeMirror pre.CodeMirror-line-like { padding: 0 16px 0 10px; }
.CodeMirror-cursor { border-left: 2px solid var(--accent); }
.CodeMirror-selected, .CodeMirror-focused .CodeMirror-selected,
.CodeMirror-line::selection, .CodeMirror-line > span::selection, .CodeMirror-line > span > span::selection {
  background: var(--selection);
}
.CodeMirror-activeline-background { background: rgba(127, 127, 127, 0.06); }
.cm-s-default .cm-header { color: var(--text); font-weight: 700; }
.cm-s-default .cm-strong { font-weight: 700; }
.cm-s-default .cm-em { font-style: italic; }
.cm-s-default .cm-strikethrough { text-decoration: line-through; }
.cm-s-default .cm-link { color: var(--accent); text-decoration: none; }
.cm-s-default .cm-url, .cm-s-default .cm-string { color: var(--muted); }
.cm-s-default .cm-quote { color: var(--muted); }
.cm-s-default .cm-comment { color: #f59e8b; }
[data-theme="light"] .cm-s-default .cm-comment { color: #b4432c; }
.cm-s-default .cm-variable-2, .cm-s-default .cm-variable-3, .cm-s-default .cm-keyword { color: var(--text); }
.cm-s-default .cm-hr { color: var(--faint); }
.cm-s-default .cm-tag, .cm-s-default .cm-attribute { color: #c4a5f5; }
[data-theme="light"] .cm-s-default .cm-tag, [data-theme="light"] .cm-s-default .cm-attribute { color: #7c3aed; }
.cm-s-default .cm-formatting { color: var(--faint); }
.cm-s-default .cm-formatting-header { color: var(--accent); }
.cm-s-default .cm-formatting-list { color: var(--accent); }
.CodeMirror-matchingbracket { color: inherit !important; }

/* --- Preview ------------------------------------------------------------- */

.preview-body { overflow: auto; scroll-behavior: auto; }
#preview {
  margin: 0 auto;
  padding: 22px 26px 60vh;
}
.preview-body[data-width="narrow"] #preview { max-width: 760px; }
.preview-body[data-width="normal"] #preview { max-width: 1012px; }
.preview-body[data-width="full"] #preview { max-width: none; }

.markdown-body { background: transparent !important; font-size: 15px; }
.task-list-item-checkbox { cursor: pointer; }
.markdown-body img[data-missing] { outline: 1px dashed var(--danger); }

/* "Normal" style: a quieter, bookish alternative to GitHub's look. */
.plain {
  color: var(--text);
  font: 16px/1.7 Georgia, "Iowan Old Style", "Noto Serif", serif;
  word-wrap: break-word;
}
.plain h1, .plain h2, .plain h3, .plain h4, .plain h5, .plain h6 {
  font-family: var(--sans);
  line-height: 1.25;
  margin: 1.6em 0 0.6em;
}
.plain h1 { font-size: 2em; }
.plain h2 { font-size: 1.5em; }
.plain h3 { font-size: 1.25em; }
.plain h6 { color: var(--muted); }
.plain > :first-child { margin-top: 0; }
.plain a { color: var(--accent); }
.plain code {
  font: 0.85em var(--mono);
  background: var(--btn);
  padding: 0.15em 0.35em;
  border-radius: 4px;
}
.plain pre {
  background: var(--btn);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px 14px;
  overflow: auto;
  line-height: 1.45;
}
.plain pre code { background: none; padding: 0; font-size: 13px; }
.plain blockquote {
  margin: 1em 0;
  padding: 0 1em;
  border-left: 3px solid var(--border-strong);
  color: var(--muted);
}
.plain table { border-collapse: collapse; margin: 1em 0; font-family: var(--sans); font-size: 14px; }
.plain th, .plain td { border: 1px solid var(--border-strong); padding: 6px 12px; }
.plain th { background: var(--btn); }
.plain hr { border: 0; border-top: 1px solid var(--border-strong); margin: 2em 0; }
.plain img { max-width: 100%; }
.plain mark { background: color-mix(in srgb, var(--warn) 35%, transparent); color: inherit; }
.plain .task-list-item { list-style: none; }
.plain .task-list-item input { margin: 0 0.4em 0 -1.3em; }
.plain .footnotes { font-size: 0.9em; color: var(--muted); }

/* Brief highlight on blocks that just changed on disk. */
@keyframes changed {
  from { box-shadow: -8px 0 0 -4px var(--accent); }
  to { box-shadow: -8px 0 0 -4px transparent; }
}
#preview .just-changed { animation: changed 1.6s ease-out; }

@media (max-width: 760px) {
  .panes { flex-direction: column; }
  #editor-pane { flex: 1 1 0; }
  .divider { display: none; }
  .panes[data-layout="split"] #preview-pane { margin-top: 12px; }
  .brand-sub { display: none; }
}
