:root {
  --bg: #ffffff;
  --bg-alt: #f6f8fa;
  --border: #d8dee4;
  --text: #1f2328;
  --muted: #656d76;
  --accent: #0969da;
  --accent-soft: #ddf4ff;
  --toolbar-bg: #ffffff;
  --code-bg: #f6f8fa;
  --sidebar-bg: #f6f8fa;
}
[data-theme="dark"] {
  --bg: #0d1117;
  --bg-alt: #161b22;
  --border: #30363d;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #4493f8;
  --accent-soft: #11304e;
  --toolbar-bg: #161b22;
  --code-bg: #161b22;
  --sidebar-bg: #0b0f14;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  overflow: hidden;
}

#app { display: flex; height: 100vh; }
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

/* ---------- Sidebar ---------- */
#sidebar {
  width: 230px;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: margin-left .18s ease;
}
body.sidebar-hidden #sidebar { margin-left: -231px; }
.brand {
  display: flex; align-items: center; gap: 8px;
  padding: 14px 16px 4px; color: var(--accent);
}
.brand svg { width: 18px; height: 18px; }
.brand-mark {
  font-weight: 700; font-size: 18px; letter-spacing: -.01em; color: var(--text);
}
.sidebar-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px 12px; font-weight: 600; font-size: 12px;
  text-transform: uppercase; letter-spacing: .04em; color: var(--muted);
}
.sidebar-actions { padding: 6px 12px 4px; }
.btn-primary {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; padding: 9px 12px;
  background: var(--accent); color: #fff; border: 1px solid var(--accent);
  border-radius: 8px; font-weight: 600; font-size: 13px;
  box-shadow: 0 1px 2px rgba(0,0,0,.12); transition: filter .12s ease, transform .08s ease;
}
.btn-primary:hover { filter: brightness(1.08); border-color: var(--accent); }
.btn-primary:active { transform: translateY(1px); }
.btn-primary svg { width: 16px; height: 16px; }
#docList { list-style: none; margin: 0; padding: 0 8px; flex: 1; overflow: auto; }
.doc-item {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px; border-radius: 7px; cursor: pointer;
  font-size: 14px; color: var(--text);
}
.doc-item:hover { background: var(--bg-alt); }
.doc-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.doc-item .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc-item .del { opacity: 0; border: 0; background: none; cursor: pointer; color: var(--muted); font-size: 14px; }
.doc-item:hover .del { opacity: 1; }
.doc-item .meta { font-size: 11px; color: var(--muted); }
.sidebar-foot {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px; border-top: 1px solid var(--border);
}

/* ---------- Buttons ---------- */
button {
  font: inherit; font-size: 13px; color: var(--text);
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 6px; padding: 5px 9px; cursor: pointer; line-height: 1.2; white-space: nowrap;
}
button:hover { background: var(--bg-alt); border-color: var(--accent); }
button:active { transform: translateY(1px); }
.icon-only, .link-btn { border: 0; background: none; padding: 4px 6px; }
.link-btn { color: var(--muted); font-size: 12px; display: inline-flex; align-items: center; gap: 4px; }
.link-btn:hover { color: var(--accent); background: none; }

/* Borderless icon buttons (toolbar, actions, sidebar) */
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 30px; height: 30px; padding: 0;
  border: 1px solid transparent; background: none; color: var(--muted);
  border-radius: 6px; transition: background .12s ease, color .12s ease;
  text-decoration: none;
}
.icon-btn:hover { background: var(--bg-alt); border-color: transparent; color: var(--text); }
.icon-btn:active { transform: translateY(1px); }
.icon-btn.active { background: var(--accent-soft); color: var(--accent); }

/* Icon + label buttons (e.g. Palette) */
.btn-text { display: inline-flex; align-items: center; gap: 6px; padding: 5px 11px; color: var(--text); }
.btn-text:hover { background: var(--bg-alt); border-color: var(--accent); }

/* Lucide SVG sizing across chrome */
.icon-btn svg { width: 17px; height: 17px; }
.btn-text svg { width: 15px; height: 15px; }
.link-btn svg { width: 13px; height: 13px; }
.doc-item .del svg { width: 15px; height: 15px; display: block; }
.topbar svg, .sidebar-head svg, .sidebar-foot svg, .statusbar svg { stroke-width: 2; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px; background: var(--toolbar-bg);
  border-bottom: 1px solid var(--border); flex-wrap: wrap;
}
.doc-title {
  font-weight: 600; font-size: 15px; color: var(--text);
  background: none; border: 1px solid transparent; border-radius: 6px;
  padding: 4px 8px; min-width: 120px; max-width: 220px;
}
.doc-title:hover { border-color: var(--border); }
.doc-title:focus { outline: none; border-color: var(--accent); background: var(--bg); }
.toolbar, .actions { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.actions { margin-left: auto; }
.toolbar .sep { width: 1px; height: 20px; background: var(--border); margin: 0 4px; }

/* ---------- Workspace ---------- */
.workspace { flex: 1; display: flex; min-height: 0; position: relative; }
.pane { overflow: auto; min-width: 0; }
.editor-pane { flex: 1 1 50%; position: relative; }
.preview-pane { flex: 1 1 50%; background: var(--bg); }
#divider { width: 6px; cursor: col-resize; background: var(--border); flex: 0 0 auto; }
#divider:hover { background: var(--accent); }

#editor {
  width: 100%; height: 100%; border: 0; resize: none; outline: none; padding: 20px;
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  font-size: 15px; line-height: 1.6; color: var(--text); background: var(--bg); tab-size: 2;
}
.markdown-body { padding: 20px 28px; max-width: 900px; }

body.no-preview .preview-pane, body.no-preview #divider { display: none; }
/* !important so a width left behind by dragging the divider can't keep the editor at half. */
body.no-preview .editor-pane { flex: 1 1 100% !important; }

/* ---------- Zen mode ---------- */
body.zen #sidebar, body.zen .topbar, body.zen .statusbar,
body.zen .preview-pane, body.zen #divider { display: none; }
body.zen .editor-pane { flex: 1 1 100% !important; }
body.zen #editor { max-width: 740px; margin: 0 auto; padding: 8vh 24px; }

/* ---------- Floating exit-zen button (only visible in zen mode) ---------- */
#zenExit { display: none; }
body.zen #zenExit {
  display: inline-flex; align-items: center; gap: 6px;
  position: fixed; top: 16px; right: 20px; z-index: 60;
  padding: 6px 12px; border-radius: 8px;
  background: var(--bg); border: 1px solid var(--border); color: var(--muted);
  box-shadow: 0 4px 14px rgba(0,0,0,.12);
  opacity: .5; transition: opacity .15s ease, color .15s ease, border-color .15s ease;
}
body.zen #zenExit:hover { opacity: 1; color: var(--text); border-color: var(--accent); }
#zenExit svg { width: 15px; height: 15px; }

/* ---------- Status bar ---------- */
.statusbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 5px 14px; font-size: 12px; color: var(--muted);
  background: var(--toolbar-bg); border-top: 1px solid var(--border);
}
#saveStatus { font-weight: 600; }
#saveStatus.unsaved { color: var(--accent); }
.stats { display: flex; align-items: center; gap: 6px; }

/* ---------- Slash menu ---------- */
.slash-menu {
  position: absolute; z-index: 30; min-width: 220px; max-height: 260px; overflow: auto;
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 8px 28px rgba(0,0,0,.18); padding: 6px;
}
.slash-item {
  display: flex; align-items: center; gap: 10px; padding: 7px 9px;
  border-radius: 7px; cursor: pointer; font-size: 14px;
}
.slash-item .ic { width: 22px; display: inline-flex; justify-content: center; color: var(--muted); }
.slash-item .ic svg { width: 16px; height: 16px; }
.slash-item.sel .ic, .slash-item:hover .ic { color: var(--accent); }
.slash-item .desc { color: var(--muted); font-size: 12px; margin-left: auto; }
.slash-item.sel, .slash-item:hover { background: var(--accent-soft); }

/* ---------- Find panel ---------- */
.find-panel {
  position: absolute; top: 10px; right: 10px; z-index: 25;
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 6px 22px rgba(0,0,0,.16); padding: 8px; width: 330px;
}
.find-row { display: flex; align-items: center; gap: 5px; margin: 3px 0; }
.find-row input[type="text"], .find-row input:not([type]) {
  flex: 1; padding: 5px 8px; border: 1px solid var(--border);
  border-radius: 6px; background: var(--bg); color: var(--text);
}
.find-count { font-size: 12px; color: var(--muted); min-width: 38px; text-align: center; }
.chk { display: flex; align-items: center; gap: 3px; font-size: 12px; color: var(--muted); cursor: pointer; }

/* ---------- Overlays (palette + modal) ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0,0,0,.35); display: flex; justify-content: center;
  align-items: flex-start; padding-top: 12vh;
}
/* `.overlay`'s display:flex overrides the UA [hidden] rule, so make hidden win explicitly. */
.overlay[hidden] { display: none; }
.palette-box, .modal-box {
  width: min(560px, 92vw); background: var(--bg); border: 1px solid var(--border);
  border-radius: 12px; box-shadow: 0 16px 50px rgba(0,0,0,.3); overflow: hidden;
}
#paletteInput {
  width: 100%; padding: 14px 16px; border: 0; border-bottom: 1px solid var(--border);
  font-size: 16px; background: var(--bg); color: var(--text); outline: none;
}
#paletteList, #historyList, #templatesList { list-style: none; margin: 0; padding: 6px; max-height: 50vh; overflow: auto; }

/* Template picker — card gallery */
.modal-box-wide { width: min(760px, 94vw); }
.tpl-sub { margin: 0; padding: 12px 16px 0; font-size: 13px; color: var(--muted); }
.tpl-sub kbd {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11px;
  background: var(--bg-alt); border: 1px solid var(--border); border-radius: 4px; padding: 1px 5px;
}
.tpl-grid {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px;
  padding: 16px; max-height: 66vh; overflow: auto;
}
.tpl-card {
  display: flex; flex-direction: column; gap: 9px; padding: 16px;
  border: 1px solid var(--border); border-radius: 12px; cursor: pointer;
  background: var(--bg); transition: border-color .12s ease, transform .1s ease, box-shadow .12s ease;
}
.tpl-card:hover {
  border-color: var(--accent); transform: translateY(-2px);
  box-shadow: 0 8px 22px rgba(0,0,0,.14);
}
.tpl-card .tpl-ic {
  width: 40px; height: 40px; display: inline-flex; align-items: center; justify-content: center;
  border-radius: 10px; background: var(--accent-soft); color: var(--accent);
}
.tpl-card .tpl-ic svg { width: 21px; height: 21px; }
.tpl-card .tpl-name { font-size: 14px; font-weight: 600; }
.tpl-card .tpl-desc { font-size: 12px; color: var(--muted); line-height: 1.45; }
@media (max-width: 680px) { .tpl-grid { grid-template-columns: repeat(2, 1fr); } }
.palette-item {
  display: flex; align-items: center; gap: 10px; padding: 9px 12px;
  border-radius: 7px; cursor: pointer; font-size: 14px;
}
.palette-item .key { margin-left: auto; color: var(--muted); font-size: 12px; }
.palette-item.sel, .palette-item:hover { background: var(--accent-soft); }
.modal-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 16px; border-bottom: 1px solid var(--border); font-weight: 600;
}
.hist-item { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 7px; font-size: 14px; }
.hist-item:hover { background: var(--bg-alt); }
.hist-item .when { flex: 1; }
.hist-item .prev { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }

/* ---------- Rendered markdown ---------- */
.markdown-body { line-height: 1.65; word-wrap: break-word; }
.markdown-body > *:first-child { margin-top: 0; }
.markdown-body h1, .markdown-body h2 { border-bottom: 1px solid var(--border); padding-bottom: .3em; }
.markdown-body h1 { font-size: 1.9em; } .markdown-body h2 { font-size: 1.5em; } .markdown-body h3 { font-size: 1.25em; }
.markdown-body a { color: var(--accent); text-decoration: none; }
.markdown-body a:hover { text-decoration: underline; }
.markdown-body code {
  background: var(--code-bg); padding: .2em .4em; border-radius: 5px; font-size: 85%;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.markdown-body pre {
  background: var(--code-bg); padding: 14px 16px; border-radius: 8px;
  overflow: auto; border: 1px solid var(--border); position: relative;
}
.markdown-body pre code { background: none; padding: 0; font-size: 90%; }
.copy-btn {
  position: absolute; top: 8px; right: 8px; padding: 3px 8px; font-size: 12px;
  opacity: 0; transition: opacity .12s;
}
.markdown-body pre:hover .copy-btn { opacity: 1; }
.markdown-body blockquote { margin: 0; padding: 0 1em; color: var(--muted); border-left: .25em solid var(--border); }
.markdown-body table { border-collapse: collapse; display: block; overflow: auto; }
.markdown-body th, .markdown-body td { border: 1px solid var(--border); padding: 6px 13px; }
.markdown-body tr:nth-child(2n) { background: var(--bg-alt); }
.markdown-body img { max-width: 100%; border-radius: 6px; }
.markdown-body hr { border: 0; border-top: 1px solid var(--border); margin: 24px 0; }
.markdown-body ul, .markdown-body ol { padding-left: 2em; }
.markdown-body .mermaid { text-align: center; background: var(--bg); }
.markdown-body .frontmatter {
  background: var(--bg-alt); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 14px; margin-bottom: 16px; font-size: 13px;
}
.markdown-body .frontmatter b { color: var(--muted); }
.toc { background: var(--bg-alt); border: 1px solid var(--border); border-radius: 8px; padding: 10px 18px; }
.toc-title { font-weight: 600; font-size: 12px; text-transform: uppercase; color: var(--muted); }

/* ---------- Print / PDF ---------- */
@media print {
  /* Print background fills (code blocks, table stripes, diagram backgrounds) instead of dropping them. */
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  #sidebar, .topbar, .statusbar, #divider, .editor-pane, .find-panel, .slash-menu, #zenExit { display: none !important; }
  body, #app, #main, .workspace { display: block; height: auto; overflow: visible; }
  .preview-pane { overflow: visible; }
  .markdown-body { max-width: none; }
  /* Keep diagrams on a clean white card and stop them splitting across pages. */
  .markdown-body .mermaid { background: #fff; break-inside: avoid; page-break-inside: avoid; }
  .markdown-body pre, .markdown-body table, .markdown-body img { break-inside: avoid; page-break-inside: avoid; }
}

/* ---------- Responsive ---------- */
@media (max-width: 760px) {
  .workspace { flex-direction: column; }
  #divider { display: none; }
  .preview-pane { border-top: 1px solid var(--border); }
}
