/* MD Editor: the app's own styles. Plain CSS, no build step. */

/* ------------------------------------------------------------------ Tokens */

:root {
  color-scheme: light;
  --font-ui: "Inter Variable", system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-code: "JetBrains Mono Variable", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;

  --radius-sm: 6px;
  --radius: 8px;
  --radius-lg: 12px;
  --topbar-h: 52px;
  --sidebar-w: 264px;
  --tabbar-h: 40px;

  --bg: #ffffff;
  --bg-app: #f6f7f9;
  --bg-subtle: #f3f4f6;
  --bg-muted: #e9ecef;
  --bg-hover: rgba(15, 23, 42, 0.06);
  --bg-active: rgba(47, 111, 235, 0.11);
  --surface: #ffffff;
  --border: #e3e6ea;
  --border-strong: #cdd2d9;
  --text: #1f2328;
  --text-muted: #59616b;
  --text-faint: #8a919a;
  --accent: #2f6feb;
  --accent-hover: #2560d6;
  --accent-text: #ffffff;
  --accent-soft: rgba(47, 111, 235, 0.12);
  --danger: #cf222e;
  --danger-soft: rgba(207, 34, 46, 0.1);
  --success: #1a7f37;
  --warning: #9a6700;
  --warning-soft: rgba(212, 167, 44, 0.16);
  --selection: rgba(47, 111, 235, 0.22);
  --selection-match: rgba(47, 111, 235, 0.13);
  --search-match: rgba(255, 196, 0, 0.38);
  --search-match-selected: rgba(255, 140, 0, 0.55);
  --mark-bg: #fff1a8;
  --mark-text: inherit;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.07);
  --shadow: 0 6px 20px rgba(15, 23, 42, 0.12);
  --shadow-lg: 0 18px 50px rgba(15, 23, 42, 0.22);
  --code-bg: #f4f5f7;
  --editor-gutter: #fafbfc;
  --editor-active-line: rgba(47, 111, 235, 0.05);

  /* Syntax colors (code blocks, Source view) */
  --tok-keyword: #cf222e;
  --tok-string: #0a3069;
  --tok-number: #0550ae;
  --tok-comment: #6e7781;
  --tok-function: #8250df;
  --tok-type: #953800;
  --tok-property: #0550ae;
  --tok-tag: #116329;
  --tok-variable: #1f2328;
  --tok-meta: #6e7781;
  --tok-heading: #0550ae;
  --tok-link: #0969da;
  --tok-quote: #57606a;
  --tok-inserted: #116329;
  --tok-inserted-bg: #dafbe1;
  --tok-deleted: #82071e;
  --tok-deleted-bg: #ffebe9;

  /* The open document's font (set per document by the font picker) */
  --doc-font: var(--font-ui);
  --doc-size: 17px;
  --doc-leading: 1.7;
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: #17191e;
  --bg-app: #121418;
  --bg-subtle: #1c1f25;
  --bg-muted: #262a32;
  --bg-hover: rgba(255, 255, 255, 0.06);
  --bg-active: rgba(91, 141, 239, 0.18);
  --surface: #1e2128;
  --border: #2c3038;
  --border-strong: #3b414b;
  --text: #e4e6ea;
  --text-muted: #a2a9b3;
  --text-faint: #737b86;
  --accent: #5b8def;
  --accent-hover: #74a0f3;
  --accent-text: #0d1117;
  --accent-soft: rgba(91, 141, 239, 0.18);
  --danger: #ff6b6b;
  --danger-soft: rgba(255, 107, 107, 0.14);
  --success: #3fb950;
  --warning: #d29922;
  --warning-soft: rgba(210, 153, 34, 0.18);
  --selection: rgba(91, 141, 239, 0.32);
  --selection-match: rgba(91, 141, 239, 0.18);
  --search-match: rgba(255, 196, 0, 0.28);
  --search-match-selected: rgba(255, 140, 0, 0.45);
  --mark-bg: rgba(255, 213, 79, 0.3);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow: 0 6px 20px rgba(0, 0, 0, 0.45);
  --shadow-lg: 0 18px 50px rgba(0, 0, 0, 0.6);
  --code-bg: #22262e;
  --editor-gutter: #1b1e24;
  --editor-active-line: rgba(255, 255, 255, 0.035);

  --tok-keyword: #ff7b72;
  --tok-string: #a5d6ff;
  --tok-number: #79c0ff;
  --tok-comment: #8b949e;
  --tok-function: #d2a8ff;
  --tok-type: #ffa657;
  --tok-property: #79c0ff;
  --tok-tag: #7ee787;
  --tok-variable: #e4e6ea;
  --tok-meta: #8b949e;
  --tok-heading: #79c0ff;
  --tok-link: #58a6ff;
  --tok-quote: #9aa3ad;
  --tok-inserted: #aff5b4;
  --tok-inserted-bg: rgba(46, 160, 67, 0.15);
  --tok-deleted: #ffdcd7;
  --tok-deleted-bg: rgba(248, 81, 73, 0.15);
}

/* Sepia: warm paper for long reading. A light theme, so diagrams and exports use light colors. */
[data-theme="sepia"] {
  color-scheme: light;
  --bg: #f8f1e3;
  --bg-app: #f0e7d5;
  --bg-subtle: #efe5d1;
  --bg-muted: #e6d9bf;
  --bg-hover: rgba(91, 70, 44, 0.07);
  --bg-active: rgba(155, 93, 39, 0.13);
  --surface: #fbf6ec;
  --border: #e2d5bc;
  --border-strong: #cdbd9e;
  --text: #3b3024;
  --text-muted: #6b5d4d;
  --text-faint: #978873;
  --accent: #9b5d27;
  --accent-hover: #874f1f;
  --accent-text: #ffffff;
  --accent-soft: rgba(155, 93, 39, 0.13);
  --danger: #b3261e;
  --danger-soft: rgba(179, 38, 30, 0.1);
  --success: #3f7d20;
  --warning: #8a5a00;
  --warning-soft: rgba(196, 140, 40, 0.18);
  --selection: rgba(155, 93, 39, 0.22);
  --selection-match: rgba(155, 93, 39, 0.13);
  --search-match: rgba(230, 170, 0, 0.36);
  --search-match-selected: rgba(230, 120, 0, 0.5);
  --mark-bg: #f6dd8a;
  --shadow-sm: 0 1px 2px rgba(59, 48, 36, 0.08);
  --shadow: 0 6px 20px rgba(59, 48, 36, 0.14);
  --shadow-lg: 0 18px 50px rgba(59, 48, 36, 0.24);
  --code-bg: #efe4cf;
  --editor-gutter: #f3ead9;
  --editor-active-line: rgba(155, 93, 39, 0.06);

  --tok-keyword: #a4262c;
  --tok-string: #2f6b2f;
  --tok-number: #0b5394;
  --tok-comment: #776851;
  --tok-function: #7a3e9d;
  --tok-type: #8c4a00;
  --tok-property: #0b5394;
  --tok-tag: #2f6b2f;
  --tok-variable: #3b3024;
  --tok-meta: #776851;
  --tok-heading: #8c4a00;
  --tok-link: #1f5f99;
  --tok-quote: #6b5d4d;
  --tok-inserted: #2f6b2f;
  --tok-inserted-bg: #e3efcf;
  --tok-deleted: #8b1e1e;
  --tok-deleted-bg: #f6dcd2;
}

/* How wide Markdown pages are (Settings, or the font picker; kept on this device). */
:root { --page-width: 820px; }
[data-width="narrow"] { --page-width: 660px; }
[data-width="wide"] { --page-width: 1080px; }
[data-width="full"] { --page-width: 100%; }

/* ------------------------------------------------------------------ Base */

*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg-app);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

::selection { background: var(--selection); }

[hidden] { display: none !important; }

a { color: var(--accent); }

code, kbd, pre { font-family: var(--font-code); }

kbd {
  font-size: 0.85em;
  padding: 1px 5px;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: var(--bg-subtle);
}

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

.muted { color: var(--text-muted); }
.faint { color: var(--text-faint); }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 32px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
}
.btn:hover { background: var(--bg-subtle); border-color: var(--border-strong); }
.btn:focus-visible, .icon-btn:focus-visible, .seg button:focus-visible, .tab:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.btn:disabled { opacity: 0.5; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { filter: brightness(0.95); background: var(--danger); }
.btn-quiet { border-color: transparent; background: transparent; }
.btn-quiet:hover { background: var(--bg-hover); border-color: transparent; }
.btn-small { height: 26px; padding: 0 8px; font-size: 13px; }
.btn-large { height: 40px; padding: 0 18px; font-size: 15px; }
.btn svg, .icon-btn svg { flex: none; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.icon-btn.active { background: var(--bg-active); color: var(--accent); }
.icon-btn-small { width: 24px; height: 24px; }

.icon { display: inline-flex; width: 16px; height: 16px; flex: none; }
.icon svg { width: 100%; height: 100%; }

/* Form fields */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label, .field-label { font-weight: 600; font-size: 13px; }
.field .hint, .hint { margin: 0; font-size: 12.5px; color: var(--text-muted); }
.field-row { display: flex; gap: 12px; }
.field-row > .field { flex: 1; }
.checkbox { display: flex; align-items: center; gap: 8px; cursor: pointer; font-size: 13.5px; }
.checkbox input { width: 16px; height: 16px; accent-color: var(--accent); }

input[type="text"], input[type="search"], input[type="password"], input:not([type]), input[type="number"], select, textarea {
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  min-width: 0;
}
textarea { height: auto; padding: 8px 10px; resize: vertical; }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
select { padding-right: 28px; }

.form-error {
  margin: 0 0 14px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  background: var(--danger-soft);
  color: var(--danger);
  font-size: 13.5px;
}

/* Segmented control (Read / Edit / Source) */
.seg {
  display: inline-flex;
  padding: 2px;
  border-radius: var(--radius-sm);
  background: var(--bg-muted);
}
.seg button {
  height: 26px;
  padding: 0 10px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}
.seg button:hover { color: var(--text); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow-sm); }
.seg button:disabled { opacity: 0.45; cursor: default; }

.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: -3px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ------------------------------------------------------------------ Layout */

.app {
  display: grid;
  grid-template-rows: var(--topbar-h) 1fr;
  height: 100vh;
  height: 100dvh;
}

.app[data-auth="true"] .guest-only,
.app:not([data-auth="true"]) .auth-only { display: none !important; }

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 20;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-right: 8px;
  color: var(--text);
  font-weight: 650;
  font-size: 15px;
  text-decoration: none;
  white-space: nowrap;
}
.topbar-search { flex: 0 1 420px; min-width: 120px; margin-left: 8px; }
.topbar-search input {
  width: 100%;
  height: 34px;
  padding-left: 34px;
  border-color: var(--border);
  background: var(--bg-subtle) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='none' stroke='%238a919a' stroke-width='2' stroke-linecap='round' viewBox='0 0 24 24'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='m20 20-3.5-3.5'/%3E%3C/svg%3E") no-repeat 11px center;
}
.topbar-search input:focus { background-color: var(--bg); }
.topbar-spacer { flex: 1; }
.topbar-actions { display: flex; align-items: center; gap: 6px; }
#account-button { max-width: 180px; overflow: hidden; text-overflow: ellipsis; }

.workspace {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  min-height: 0;
}
.app:not([data-auth="true"]) .workspace,
[data-sidebar="hidden"] .workspace { grid-template-columns: minmax(0, 1fr); }
[data-sidebar="hidden"] .sidebar { display: none; }

.main {
  display: grid;
  grid-template-rows: auto 1fr;
  min-width: 0;
  min-height: 0;
  background: var(--bg);
}

.panes { position: relative; min-height: 0; overflow: hidden; }

/* Full-screen reading: only the open document and its toolbar. */
[data-fullscreen="on"] .app { grid-template-rows: minmax(0, 1fr); }
[data-fullscreen="on"] .topbar,
[data-fullscreen="on"] .sidebar,
[data-fullscreen="on"] .tabbar { display: none !important; }
[data-fullscreen="on"] .workspace { grid-template-columns: minmax(0, 1fr) !important; }
[data-fullscreen="on"] .main { grid-template-rows: minmax(0, 1fr); }
.pane { position: absolute; inset: 0; display: flex; flex-direction: column; min-height: 0; background: var(--bg); }
.pane[hidden] { display: none; }

.loading-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  height: 100%;
  color: var(--text-muted);
}

/* ------------------------------------------------------------------ Tabs */

.tabbar {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  height: var(--tabbar-h);
  padding: 0 8px;
  overflow-x: auto;
  overflow-y: hidden;
  background: var(--bg-app);
  border-bottom: 1px solid var(--border);
  scrollbar-width: thin;
}
.tab {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  flex: none;
  max-width: 240px;
  height: 32px;
  padding: 0 6px 0 10px;
  border: 1px solid transparent;
  border-bottom: 0;
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  cursor: pointer;
  user-select: none;
}
.tab:hover { background: var(--bg-hover); color: var(--text); }
.tab[aria-selected="true"] {
  background: var(--bg);
  border-color: var(--border);
  color: var(--text);
  margin-bottom: -1px;
  height: 33px;
}
.tab-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tab-home { padding: 0 12px; font-weight: 550; }
.tab-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--text-faint);
  cursor: pointer;
  flex: none;
}
.tab-close:hover { background: var(--bg-hover); color: var(--text); }
.tab-dirty .tab-close::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
}
.tab-dirty .tab-close svg { display: none; }
.tab-dirty .tab-close:hover::before { display: none; }
.tab-dirty .tab-close:hover svg { display: block; }
.tab-badge {
  font-size: 10.5px;
  font-weight: 600;
  padding: 0 5px;
  border-radius: 4px;
  background: var(--bg-muted);
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

/* ------------------------------------------------------------------ Sidebar */

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow-y: auto;
  padding: 10px 8px 16px;
  background: var(--bg-app);
  border-right: 1px solid var(--border);
}
.side-section { margin-bottom: 14px; }
.side-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 26px;
  padding: 0 6px 0 10px;
  color: var(--text-faint);
  font-size: 11.5px;
  font-weight: 650;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.side-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  height: 30px;
  padding: 0 6px 0 10px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text);
  font-size: 13.5px;
  text-align: left;
  cursor: pointer;
}
.side-item:hover { background: var(--bg-hover); }
.side-item.active { background: var(--bg-active); color: var(--accent); font-weight: 550; }
.side-item .icon { color: var(--text-muted); }
.side-item.active .icon { color: var(--accent); }
.side-item-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-count { color: var(--text-faint); font-size: 12px; font-variant-numeric: tabular-nums; }
.side-link {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  min-width: 0;
  height: 100%;
  color: inherit;
  text-decoration: none;
  border-radius: var(--radius-sm);
}
.side-link:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.side-item .side-more { display: none; flex: none; }
.side-item:hover .side-more, .side-item:focus-within .side-more, .side-item .side-more[aria-expanded="true"] { display: inline-flex; }
.side-item:has(.side-more):hover .side-count,
.side-item:has(.side-more):focus-within .side-count,
.side-item:has(.side-more[aria-expanded="true"]) .side-count { display: none; }
/* Touch screens have no hover: always show the menu buttons. */
@media (hover: none) {
  .side-item .side-more { display: inline-flex; }
  .doc-actions .icon-btn { opacity: 1; }
  .side-item:has(.side-more) .side-count { display: none; }
  .milkdown .milkdown-code-block .tools .language-button,
  .milkdown .milkdown-code-block .tools .tools-button-group button { opacity: 1; }
}
/* A code block's language and Copy buttons in the visual editor also show while you type in it. */
.milkdown .milkdown-code-block:focus-within .tools .language-button,
.milkdown .milkdown-code-block:focus-within .tools .tools-button-group button { opacity: 1; }
.side-heading .icon-btn { color: var(--text-faint); }
.side-heading .icon-btn:hover { color: var(--text); }
.side-caret {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  margin-left: -4px;
  margin-right: -4px;
  border: 0;
  background: transparent;
  color: var(--text-faint);
  cursor: pointer;
  transition: transform 0.12s;
  flex: none;
}
.side-caret.open { transform: rotate(90deg); }
.side-caret.empty { visibility: hidden; }
.side-item.drop-target { background: var(--accent-soft); outline: 2px dashed var(--accent); outline-offset: -2px; }
.side-empty { padding: 2px 10px; color: var(--text-faint); font-size: 12.5px; }
.side-footer { margin-top: auto; padding-top: 8px; border-top: 1px solid var(--border); }

/* ------------------------------------------------------------------ Library list */

.home { overflow-y: auto; }
.home-inner { width: 100%; max-width: 1040px; margin: 0 auto; padding: 22px 28px 60px; }
.home-header {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 14px;
}
.home-title { display: flex; align-items: baseline; gap: 10px; flex: 1; min-width: 200px; }
.home-title h1 { margin: 0; font-size: 22px; font-weight: 680; letter-spacing: -0.01em; }
.home-title .count { color: var(--text-faint); font-size: 14px; }
.home-controls { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.layout-switch button { display: inline-flex; align-items: center; padding: 0 8px; }
.home-controls select { height: 32px; font-size: 13px; }
.breadcrumbs { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; margin: -4px 0 12px; color: var(--text-muted); font-size: 13px; }
.breadcrumbs button { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 0; font-size: 13px; }

.doc-list { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--border); }
.doc-row {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr) auto;
  gap: 4px 12px;
  align-items: center;
  padding: 11px 10px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.doc-row:hover { background: var(--bg-subtle); }
.doc-row:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.doc-row.dragging { opacity: 0.5; }
.doc-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: var(--radius-sm);
  background: var(--bg-muted);
  color: var(--text-muted);
}
.doc-icon.markdown { background: var(--accent-soft); color: var(--accent); }
.doc-icon.folder { background: var(--warning-soft); color: var(--warning); }
.doc-main { min-width: 0; }
.doc-title {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
  font-size: 14.5px;
}
.doc-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; margin-top: 2px; color: var(--text-muted); font-size: 12.5px; }
.doc-summary {
  display: -webkit-box;
  margin-top: 3px;
  overflow: hidden;
  color: var(--text-muted);
  font-size: 13px;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
}
.doc-summary mark, .doc-title mark { background: var(--mark-bg); color: var(--mark-text); border-radius: 2px; padding: 0 1px; }
.doc-actions { display: flex; align-items: center; gap: 2px; }
.doc-actions .icon-btn { opacity: 0; }
.doc-row:hover .doc-actions .icon-btn, .doc-actions .icon-btn:focus-visible, .doc-actions .icon-btn.on, .doc-actions .icon-btn[aria-expanded="true"] { opacity: 1; }
.doc-actions .icon-btn.on { color: #e3a008; }

/* The library as a grid of cards. */
.doc-list.doc-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 12px;
  border-top: 0;
}
.doc-grid .doc-row {
  position: relative;
  grid-template-columns: 34px minmax(0, 1fr);
  align-items: start;
  align-content: start;
  gap: 10px;
  min-height: 160px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
}
.doc-grid .doc-row:hover { border-color: var(--border-strong); background: var(--bg-subtle); }
.doc-grid .doc-row:focus-visible { outline-offset: 2px; }
.doc-grid .folder-row { min-height: 0; }
.doc-grid .doc-main { grid-column: 1 / -1; }
.doc-grid .doc-actions { position: absolute; top: 10px; right: 8px; }
.doc-grid .doc-title {
  display: -webkit-box;
  white-space: normal;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
}
.doc-grid .doc-summary { margin-top: 6px; -webkit-line-clamp: 4; line-clamp: 4; }
.tag-chip {
  display: inline-flex;
  align-items: center;
  height: 20px;
  padding: 0 7px;
  border-radius: 10px;
  background: var(--bg-muted);
  color: var(--text-muted);
  font-size: 11.5px;
  font-weight: 550;
  white-space: nowrap;
}

.empty-state {
  max-width: 460px;
  margin: 60px auto;
  text-align: center;
  color: var(--text-muted);
}
.empty-state h2 { margin: 12px 0 6px; color: var(--text); font-size: 18px; }
.empty-state p { margin: 0 0 16px; }
.empty-state .icon { width: 40px; height: 40px; color: var(--text-faint); }
.empty-actions { display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; }

/* Welcome screen for visitors */
.welcome { display: flex; align-items: center; justify-content: center; height: 100%; padding: 24px; overflow-y: auto; }
.dropzone {
  width: min(640px, 100%);
  padding: 48px 32px;
  border: 2px dashed var(--border-strong);
  border-radius: 18px;
  background: var(--bg-subtle);
  text-align: center;
}
.dropzone h1 { margin: 14px 0 6px; font-size: 24px; letter-spacing: -0.01em; }
.dropzone p { margin: 0 auto 20px; max-width: 460px; color: var(--text-muted); }
.dropzone .icon { width: 44px; height: 44px; color: var(--accent); }
.dropzone-note { margin-top: 18px !important; font-size: 13px; }
.drop-hint {
  margin-top: 24px;
  padding: 14px;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  color: var(--text-muted);
  font-size: 13px;
  text-align: center;
}

.drop-overlay {
  position: fixed;
  inset: 0;
  z-index: 90;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(47, 111, 235, 0.1);
  border: 3px dashed var(--accent);
  pointer-events: none;
}
.drop-overlay-card {
  padding: 16px 24px;
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow-lg);
  font-size: 16px;
  font-weight: 600;
}

/* ------------------------------------------------------------------ Document pane */

.doc-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 46px;
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  flex-wrap: wrap;
}
.doc-toolbar .doc-name {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1 1 160px;
}
.doc-name-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
  font-size: 14px;
}
button.doc-name-title { border: 0; background: none; padding: 2px 4px; margin-left: -4px; border-radius: 4px; cursor: text; text-align: left; }
button.doc-name-title:hover { background: var(--bg-hover); }
.doc-name-badge { flex: none; }
.toolbar-group { display: flex; align-items: center; gap: 4px; }
.toolbar-sep { width: 1px; height: 22px; background: var(--border); margin: 0 4px; }

.doc-notice {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--warning-soft);
  font-size: 13px;
}
.doc-notice.info { background: var(--accent-soft); }
.doc-notice-text { flex: 1; }

.doc-content {
  position: relative;
  /* Editor layers (CodeMirror panels use z-index 300) stay below the app's menus. */
  isolation: isolate;
  flex: 1;
  min-height: 0;
  overflow: auto;
  font-family: var(--doc-font);
  font-size: var(--doc-size);
  line-height: var(--doc-leading);
}
.doc-content.editor-host { overflow: hidden; display: flex; flex-direction: column; }
.doc-content.editor-host > .cm-editor { flex: 1; min-height: 0; }

/* The editor and, beside it, the outline. */
.doc-body { position: relative; display: flex; flex: 1; min-height: 0; }
.doc-body > .doc-content { flex: 1; min-width: 0; }

.statusbar {
  display: flex;
  align-items: center;
  gap: 14px;
  height: 28px;
  padding: 0 12px;
  border-top: 1px solid var(--border);
  background: var(--bg-app);
  color: var(--text-muted);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
}
.statusbar .grow { flex: 1; }
.status-save { display: inline-flex; align-items: center; gap: 6px; }
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-faint); }
.status-save[data-state="saved"] .status-dot { background: var(--success); }
.status-save[data-state="dirty"] .status-dot { background: var(--warning); }
.status-save[data-state="saving"] .status-dot { background: var(--accent); animation: pulse 1s infinite; }
.status-save[data-state="error"] .status-dot,
.status-save[data-state="conflict"] .status-dot,
.status-save[data-state="offline"] .status-dot { background: var(--danger); }
.status-save[data-state="locked"] .status-dot { background: var(--warning); }
@keyframes pulse { 50% { opacity: 0.3; } }
.status-btn {
  height: 22px;
  padding: 0 6px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: inherit;
  font-size: 12px;
  cursor: pointer;
}
.status-btn:hover { background: var(--bg-hover); color: var(--text); }
.status-link { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 0; font-size: 12px; }

/* ------------------------------------------------------------------ Rendered Markdown (viewer) */

.md-body {
  max-width: var(--page-width);
  margin: 0 auto;
  padding: 36px 40px 80px;
  color: var(--text);
  font-family: var(--doc-font);
  font-size: var(--doc-size);
  line-height: var(--doc-leading);
  overflow-wrap: break-word;
}
.md-body > :first-child { margin-top: 0; }
.md-body h1, .md-body h2, .md-body h3, .md-body h4, .md-body h5, .md-body h6 {
  margin: 1.6em 0 0.6em;
  line-height: 1.25;
  font-weight: 680;
  letter-spacing: -0.01em;
  scroll-margin-top: 16px;
}
.md-body h1 { font-size: 2em; padding-bottom: 0.25em; border-bottom: 1px solid var(--border); }
.md-body h2 { font-size: 1.5em; padding-bottom: 0.25em; border-bottom: 1px solid var(--border); }
.md-body h3 { font-size: 1.25em; }
.md-body h4 { font-size: 1.05em; }
.md-body h5 { font-size: 0.95em; }
.md-body h6 { font-size: 0.9em; color: var(--text-muted); }
.md-body p, .md-body ul, .md-body ol, .md-body blockquote, .md-body table, .md-body pre, .md-body dl, .md-body details { margin: 0 0 1em; }
.md-body ul, .md-body ol { padding-left: 1.8em; }
.md-body li + li { margin-top: 0.25em; }
.md-body li > ul, .md-body li > ol { margin: 0.25em 0 0; }
.md-body .task-list-item { list-style: none; }
.md-body .task-list-item input { margin: 0 0.45em 0 -1.4em; vertical-align: -1px; accent-color: var(--accent); }
.md-body .contains-task-list { padding-left: 1.6em; }
.md-body a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; text-decoration-thickness: 1px; }
.md-body .header-anchor {
  margin-left: 0.3em;
  color: var(--text-faint);
  text-decoration: none;
  opacity: 0;
  font-weight: 400;
}
.md-body h1:hover .header-anchor, .md-body h2:hover .header-anchor, .md-body h3:hover .header-anchor,
.md-body h4:hover .header-anchor, .md-body h5:hover .header-anchor, .md-body h6:hover .header-anchor { opacity: 1; }
.md-body blockquote {
  padding: 0.1em 1em;
  border-left: 4px solid var(--border-strong);
  color: var(--text-muted);
}
.md-body hr { height: 0; margin: 2em 0; border: 0; border-top: 2px solid var(--border); }
.md-body img { max-width: 100%; height: auto; border-radius: 4px; }
.md-body img.img-broken {
  display: inline-block;
  min-width: 140px;
  min-height: 40px;
  padding: 8px 10px;
  border: 1px dashed var(--border-strong);
  color: var(--text-faint);
  font-size: 0.8em;
}
.md-body table { display: block; width: max-content; max-width: 100%; overflow: auto; border-collapse: collapse; font-size: 0.94em; }
.md-body th, .md-body td { padding: 6px 13px; border: 1px solid var(--border-strong); }
.md-body th { background: var(--bg-subtle); font-weight: 650; }
.md-body tr:nth-child(2n) td { background: color-mix(in srgb, var(--bg-subtle) 55%, transparent); }
.md-body code {
  padding: 0.15em 0.35em;
  border-radius: 4px;
  background: var(--code-bg);
  font-size: 0.86em;
}
.md-body pre {
  position: relative;
  padding: 14px 16px;
  overflow: auto;
  border-radius: var(--radius);
  background: var(--code-bg);
  font-size: 0.84em;
  line-height: 1.55;
}
.md-body pre code { padding: 0; background: none; font-size: inherit; white-space: pre; }
.md-body .code-lang {
  position: absolute;
  top: 7px;
  right: 10px;
  color: var(--text-faint);
  font-family: var(--font-ui);
  font-size: 11px;
  text-transform: lowercase;
  pointer-events: none;
  transition: opacity 0.12s;
}
.md-body .code-tools {
  position: absolute;
  top: 6px;
  right: 6px;
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.12s;
}
.md-body pre:hover .code-tools, .md-body .code-tools:focus-within { opacity: 1; }
.md-body pre:hover .code-lang, .md-body pre:focus-within .code-lang { opacity: 0; }
.md-body .copy-code {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 24px;
  padding: 0 8px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--surface);
  color: var(--text-muted);
  font-family: var(--font-ui);
  font-size: 11.5px;
  cursor: pointer;
}
.md-body .copy-code:hover { color: var(--text); border-color: var(--border-strong); }
@media (hover: none) {
  .md-body .code-tools { opacity: 1; }
  .md-body .code-lang { display: none; }
}
.md-body kbd { font-size: 0.8em; }
.md-body mark { background: var(--mark-bg); color: inherit; }
.md-body details > summary { cursor: pointer; font-weight: 600; }
.md-body dt { font-weight: 650; }
.md-body dd { margin: 0 0 0.5em 1.5em; }
.md-body .footnotes { font-size: 0.88em; color: var(--text-muted); }
.md-body .footnotes-sep { margin-top: 3em; }
.md-body sup.footnote-ref a { text-decoration: none; }

/* GitHub-style callouts: > [!NOTE] */
.md-body .markdown-alert {
  margin: 0 0 1em;
  padding: 0.5em 1em;
  border-left: 4px solid var(--alert-color, var(--accent));
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  background: color-mix(in srgb, var(--alert-color, var(--accent)) 7%, transparent);
}
.md-body .markdown-alert > :last-child { margin-bottom: 0; }
.md-body .markdown-alert-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 0.3em !important;
  color: var(--alert-color);
  font-weight: 650;
}
.md-body .markdown-alert-title svg { fill: currentColor; width: 16px; height: 16px; }
.md-body .markdown-alert-note { --alert-color: #2f6feb; }
.md-body .markdown-alert-tip { --alert-color: #1a7f37; }
.md-body .markdown-alert-important { --alert-color: #8250df; }
.md-body .markdown-alert-warning { --alert-color: #9a6700; }
.md-body .markdown-alert-caution { --alert-color: #cf222e; }
[data-theme="dark"] .md-body .markdown-alert-note { --alert-color: #5b8def; }
[data-theme="dark"] .md-body .markdown-alert-tip { --alert-color: #3fb950; }
[data-theme="dark"] .md-body .markdown-alert-important { --alert-color: #ab7df8; }
[data-theme="dark"] .md-body .markdown-alert-warning { --alert-color: #d29922; }
[data-theme="dark"] .md-body .markdown-alert-caution { --alert-color: #f85149; }

/* Front matter card */
.fm-card {
  margin: 0 0 2em;
  padding: 14px 18px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-subtle);
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.5;
}
.fm-title { margin: 0 0 4px; font-size: 18px; font-weight: 680; }
.fm-line { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; color: var(--text-muted); font-size: 13px; }
.fm-card details { margin-top: 8px; }
.fm-card summary { color: var(--text-muted); font-size: 12.5px; cursor: pointer; }
.fm-card table { margin-top: 8px; border-collapse: collapse; font-size: 12.5px; }
.fm-card th, .fm-card td { padding: 3px 12px 3px 0; text-align: left; vertical-align: top; border: 0; background: none !important; }
.fm-card th { color: var(--text-muted); font-weight: 600; }
.fm-card td { font-family: var(--font-code); white-space: pre-wrap; }
/* In the visual editor the card lines up with the text column below it. */
.fm-card.fm-visual { width: calc(100% - 120px); max-width: 700px; margin: 24px auto 0; }
.fm-note { margin: 10px 0 0; color: var(--text-muted); font-size: 12.5px; }
.text-button {
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent);
  font: inherit;
  cursor: pointer;
}
.text-button:hover { text-decoration: underline; }

/* Plain text view for visitors and printing */
.text-view {
  margin: 0;
  padding: 24px 28px 60px;
  font-family: var(--doc-font);
  font-size: var(--doc-size);
  line-height: var(--doc-leading);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  tab-size: 4;
}

/* Syntax colors for highlight.js output in rendered code blocks */
.hljs { color: var(--text); }
.hljs-keyword, .hljs-selector-tag, .hljs-doctag, .hljs-template-tag, .hljs-type.hljs-keyword { color: var(--tok-keyword); }
.hljs-string, .hljs-regexp, .hljs-char.escape_, .hljs-template-variable { color: var(--tok-string); }
.hljs-number, .hljs-literal, .hljs-symbol, .hljs-bullet, .hljs-variable.constant_ { color: var(--tok-number); }
.hljs-comment, .hljs-code, .hljs-formula { color: var(--tok-comment); font-style: italic; }
.hljs-title.function_, .hljs-title, .hljs-function .hljs-title { color: var(--tok-function); }
.hljs-title.class_, .hljs-type, .hljs-built_in, .hljs-class .hljs-title { color: var(--tok-type); }
.hljs-attr, .hljs-attribute, .hljs-property, .hljs-params, .hljs-selector-attr, .hljs-selector-class, .hljs-selector-id { color: var(--tok-property); }
.hljs-name, .hljs-tag, .hljs-selector-pseudo { color: var(--tok-tag); }
.hljs-variable, .hljs-variable.language_ { color: var(--tok-variable); }
.hljs-meta, .hljs-meta .hljs-keyword, .hljs-subst { color: var(--tok-meta); }
.hljs-section { color: var(--tok-heading); font-weight: 650; }
.hljs-link { color: var(--tok-link); text-decoration: underline; }
.hljs-quote { color: var(--tok-quote); }
.hljs-emphasis { font-style: italic; }
.hljs-strong { font-weight: 700; }
.hljs-addition { color: var(--tok-inserted); background: var(--tok-inserted-bg); }
.hljs-deletion { color: var(--tok-deleted); background: var(--tok-deleted-bg); }

/* ------------------------------------------------------------------ Visual editor (Milkdown Crepe) */

.visual-host { position: relative; min-height: 100%; }
.doc-content .milkdown {
  --crepe-base-font-size: 15px;
  --crepe-color-background: var(--bg);
  --crepe-color-on-background: var(--text);
  --crepe-color-surface: var(--bg-subtle);
  --crepe-color-surface-low: var(--bg-muted);
  --crepe-color-on-surface: var(--text);
  --crepe-color-on-surface-variant: var(--text-muted);
  /* Crepe draws its icons (toolbars, menus, list bullets, handles) and its caret in this colour and
     makes its lines from a fifth of it, so it is a text colour, not a border one. */
  --crepe-color-outline: var(--text-muted);
  --crepe-color-primary: var(--accent);
  --crepe-color-secondary: var(--accent-soft);
  --crepe-color-on-secondary: var(--text);
  --crepe-color-inverse: var(--text);
  --crepe-color-on-inverse: var(--bg);
  --crepe-color-inline-code: var(--tok-keyword);
  --crepe-color-error: var(--danger);
  --crepe-color-hover: var(--bg-hover);
  --crepe-color-selected: var(--bg-active);
  --crepe-color-inline-area: var(--bg-muted);
  --crepe-font-title: var(--doc-font);
  --crepe-font-default: var(--font-ui);
  --crepe-font-code: var(--font-code);
  --crepe-shadow-1: var(--shadow-sm);
  --crepe-shadow-2: var(--shadow);
  min-height: 100%;
  background: var(--bg);
}
.doc-content .milkdown .ProseMirror {
  max-width: var(--page-width);
  margin: 0 auto;
  padding: 28px 60px 120px;
  font-family: var(--doc-font);
  font-size: var(--doc-size);
  line-height: var(--doc-leading);
  outline: none;
}
.doc-content .milkdown .ProseMirror p,
.doc-content .milkdown .ProseMirror li { line-height: var(--doc-leading); }
/* Headings match the Read view. */
.doc-content .milkdown .ProseMirror h1,
.doc-content .milkdown .ProseMirror h2,
.doc-content .milkdown .ProseMirror h3,
.doc-content .milkdown .ProseMirror h4,
.doc-content .milkdown .ProseMirror h5,
.doc-content .milkdown .ProseMirror h6 { font-weight: 680; line-height: 1.25; letter-spacing: -0.01em; }
.doc-content .milkdown .ProseMirror h1 { font-size: 2em; }
.doc-content .milkdown .ProseMirror h2 { font-size: 1.5em; }
.doc-content .milkdown .ProseMirror h3 { font-size: 1.25em; }
.doc-content .milkdown .ProseMirror h4 { font-size: 1.05em; }
.doc-content .milkdown .ProseMirror h5 { font-size: 0.95em; }
.doc-content .milkdown .ProseMirror h6 { font-size: 0.9em; color: var(--text-muted); }
.doc-content .milkdown .ProseMirror ::selection { background: var(--selection); }
.doc-content .milkdown .milkdown-top-bar {
  z-index: 5;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.doc-content .milkdown .ProseMirror a { color: var(--accent); }
.doc-content .milkdown .ProseMirror blockquote { border-left-color: var(--border-strong); }
.doc-content .milkdown .ProseMirror code { background: var(--code-bg); }
.doc-content .milkdown .ProseMirror img { max-width: 100%; }

/* ------------------------------------------------------------------ CodeMirror (text editor and Source view)
   Only the top-level editor: code blocks inside the visual editor keep their own look. */

.doc-content > .cm-editor { height: 100%; font-size: var(--doc-size); }
.doc-content > .cm-editor .cm-scroller { font-family: var(--doc-font); line-height: var(--doc-leading); }
.doc-content > .cm-editor .cm-content { padding: 14px 0 35vh; }
.doc-content > .cm-editor .cm-line { padding: 0 18px; }
.doc-content > .cm-editor .cm-gutters { font-family: var(--font-code); font-size: max(11px, 0.78em); }
.doc-content > .cm-editor .cm-lineNumbers .cm-gutterElement { padding: 0 10px 0 14px; min-width: 44px; }
/* Markdown source reads best as a centred column. */
.doc-content > .cm-editor.source-editor .cm-content { max-width: calc(var(--page-width) + 60px); margin: 0 auto; }
.cm-editor .cm-panels { font-family: var(--font-ui); font-size: 13px; }
.cm-editor .cm-panel.cm-search {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  padding: 8px 36px 8px 12px;
}
.cm-editor .cm-panel.cm-search br { display: none; }
.cm-editor .cm-panel.cm-search input.cm-textfield {
  height: 28px;
  margin: 0;
  padding: 0 8px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--bg);
  font-size: 13px;
}
.cm-editor .cm-panel.cm-search input[type="checkbox"] { margin: 0 2px 0 0; accent-color: var(--accent); }
.cm-editor .cm-panel.cm-search label { display: inline-flex; align-items: center; gap: 3px; font-size: 12.5px; color: var(--text-muted); }
.cm-editor .cm-panel.cm-search button.cm-button {
  height: 28px;
  margin: 0;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  background-image: none;
  color: var(--text);
  font-size: 12.5px;
  cursor: pointer;
}
.cm-editor .cm-panel.cm-search button.cm-button:hover { background: var(--bg-subtle); }
.cm-editor .cm-panel.cm-search button[name="close"] {
  position: absolute;
  top: 8px;
  right: 10px;
  width: 24px;
  height: 24px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--text-muted);
  font-size: 18px;
  cursor: pointer;
}
.cm-editor .cm-panel.cm-gotoLine { padding: 8px 12px; }
.cm-editor .cm-panel.cm-gotoLine input { height: 28px; margin: 0 6px; }

/* ------------------------------------------------------------------ Menus, popovers, dialogs, toasts */

.menu, .popover {
  position: fixed;
  z-index: 100;
  min-width: 200px;
  max-width: 340px;
  max-height: calc(100vh - 24px);
  overflow-y: auto;
  padding: 5px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 32px;
  padding: 0 10px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--text);
  font-size: 13.5px;
  text-align: left;
  cursor: pointer;
}
.menu-item:hover, .menu-item:focus-visible { background: var(--bg-hover); outline: none; }
.menu-item:disabled { opacity: 0.45; cursor: default; background: transparent; }
.menu-item.danger { color: var(--danger); }
.menu-item .icon { color: var(--text-muted); }
.menu-item.danger .icon { color: var(--danger); }
.menu-item .menu-check { width: 16px; color: var(--accent); font-weight: 700; }
.menu-item-label { flex: 1; }
.menu-item-hint { color: var(--text-faint); font-size: 12px; }
.menu-sep { height: 1px; margin: 5px 4px; background: var(--border); }
.menu-heading { padding: 6px 10px 4px; color: var(--text-faint); font-size: 11.5px; font-weight: 650; text-transform: uppercase; letter-spacing: 0.05em; }
.menu-item.has-submenu > .icon:last-child { color: var(--text-faint); }
.menu-item.menu-back { font-weight: 600; }
.menu-note { max-width: 290px; margin: 0; padding: 4px 10px 6px; color: var(--text-faint); font-size: 12px; line-height: 1.4; }

dialog.dialog {
  width: min(480px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
}
dialog.dialog.wide { width: min(640px, calc(100vw - 32px)); }
dialog.dialog::backdrop { background: rgba(10, 14, 20, 0.45); }
.dialog-form { display: flex; flex-direction: column; max-height: calc(100vh - 50px); }
.dialog-head { display: flex; align-items: center; gap: 10px; padding: 16px 20px 6px; }
.dialog-head h2 { flex: 1; margin: 0; font-size: 17px; font-weight: 680; }
.dialog-body { padding: 10px 20px 6px; overflow-y: auto; }
.dialog-body > p:first-child { margin-top: 0; }
.dialog-foot { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 8px; padding: 12px 20px 18px; }
.dialog-foot .spacer { flex: 1; }
.settings-section { padding: 4px 0 12px; border-bottom: 1px solid var(--border); margin-bottom: 14px; }
.settings-section:last-child { border-bottom: 0; margin-bottom: 0; }
.settings-section h3 { margin: 0 0 10px; font-size: 13px; font-weight: 680; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; }
.settings-grid { display: grid; grid-template-columns: 1.4fr 0.8fr 0.8fr; gap: 10px; align-items: end; }
.settings-grid .field { margin: 0; }
.settings-grid.settings-grid-2 { grid-template-columns: 1fr 1fr; }
.settings-grid + .field { margin-top: 14px; }

.toasts {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: min(420px, calc(100vw - 32px));
}
.toast {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  font-size: 13.5px;
  animation: toast-in 0.18s ease-out;
}
.toast.error { border-left-color: var(--danger); }
.toast.success { border-left-color: var(--success); }
.toast.warning { border-left-color: var(--warning); }
.toast-text { flex: 1; }
.toast button { flex: none; }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } }

/* Font picker */
.font-picker { width: 300px; padding: 10px; }
.font-list { display: flex; flex-direction: column; gap: 2px; max-height: 300px; overflow-y: auto; margin-bottom: 10px; }
.font-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  height: 34px;
  padding: 0 10px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  font-size: 15px;
  text-align: left;
  cursor: pointer;
}
.font-option:hover { background: var(--bg-hover); }
.font-option[aria-checked="true"] { border-color: var(--accent); background: var(--accent-soft); }
.font-option small { color: var(--text-faint); font-family: var(--font-ui); font-size: 11px; }
.font-controls { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; padding-top: 10px; border-top: 1px solid var(--border); }
.font-controls label { display: block; margin-bottom: 4px; color: var(--text-muted); font-size: 12px; font-weight: 600; }
.stepper { display: flex; align-items: center; gap: 4px; }
.stepper output { min-width: 34px; text-align: center; font-variant-numeric: tabular-nums; }
.font-controls select { width: 100%; height: 30px; }
.font-reset { margin-top: 10px; width: 100%; }
.page-width-control { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--border); }
.page-width-label { color: var(--text-muted); font-size: 12px; font-weight: 600; }
.seg-small button { height: 24px; padding: 0 8px; font-size: 12px; }

/* ------------------------------------------------------------------ Setup page */

.setup-page { min-height: 100vh; padding: 40px 16px; background: var(--bg-app); }
.setup-card {
  max-width: 620px;
  margin: 0 auto;
  padding: 28px 32px 32px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.setup-brand { display: flex; gap: 14px; align-items: flex-start; margin-bottom: 18px; }
.setup-brand h1 { margin: 0 0 4px; font-size: 22px; }
.setup-brand p { margin: 0; }
.setup-card h2 { margin: 22px 0 10px; font-size: 15px; }
.checks { list-style: none; margin: 0; padding: 0; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.check { display: flex; gap: 12px; padding: 10px 14px; border-bottom: 1px solid var(--border); }
.check:last-child { border-bottom: 0; }
.check > div { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.check strong { font-size: 13.5px; }
.check-detail { color: var(--text-muted); font-size: 12.5px; overflow-wrap: anywhere; }
.check-fix { color: var(--text); font-size: 12.5px; }
.check-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  font-size: 12px;
  font-weight: 700;
  color: #fff;
  background: var(--text-faint);
}
.check-ok .check-icon { background: var(--success); }
.check-warn .check-icon { background: var(--warning); }
.check-fail .check-icon { background: var(--danger); }
.check-fail { background: var(--danger-soft); }
.setup-blocked { margin-top: 16px; font-weight: 600; }
.setup-form { margin-top: 4px; }
.setup-form input { height: 38px; font-size: 15px; }
.setup-form code { font-size: 12px; overflow-wrap: anywhere; }
.setup-form .btn { margin-top: 6px; width: 100%; }

/* ------------------------------------------------------------------ Panes, notices and the document toolbar */

.pane.home { overflow-y: auto; }
.pane-loading { position: absolute; inset: 0; z-index: 2; height: auto; background: var(--bg); }
.doc-content:focus { outline: none; }
.doc-notices:empty { display: none; }
.doc-notice .icon { color: var(--warning); }
.doc-notice.info .icon { color: var(--accent); }
.doc-notice .btn-small { flex: none; }
.doc-folder {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
  max-width: 200px;
  padding: 2px 6px;
  border-radius: 4px;
  color: var(--text-muted);
  font-size: 12.5px;
  text-decoration: none;
  white-space: nowrap;
}
.doc-folder span { overflow: hidden; text-overflow: ellipsis; }
.doc-folder:hover { background: var(--bg-hover); color: var(--text); }
.icon-btn.favorite-on, .icon-btn.favorite-on:hover { color: #e3a008; }
.tab .icon { color: var(--text-muted); }
.tab[aria-selected="true"] .icon { color: var(--accent); }
.doc-folder-name { display: inline-flex; align-items: center; gap: 3px; }
.folder-row .doc-title { font-weight: 550; }

/* Dialog parts */
.dialog-message {
  margin: 0 0 14px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  background: var(--accent-soft);
  font-size: 13.5px;
}
.form-warning {
  margin: 0 0 12px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  background: var(--warning-soft);
  color: var(--text);
  font-size: 13px;
}
.settings-checks { display: flex; flex-wrap: wrap; gap: 8px 22px; margin-top: 12px; }
.settings-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.settings-section .field:last-child { margin-bottom: 0; }

.folder-picker {
  display: flex;
  flex-direction: column;
  gap: 1px;
  max-height: min(320px, 50vh);
  overflow-y: auto;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.folder-option {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 32px;
  padding: 0 10px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--text);
  font-size: 13.5px;
  text-align: left;
  cursor: pointer;
}
.folder-option:hover { background: var(--bg-hover); }
.folder-option[aria-selected="true"] { background: var(--bg-active); color: var(--accent); font-weight: 550; }
.folder-option .icon { color: var(--text-muted); }
.folder-option[aria-selected="true"] .icon { color: var(--accent); }
.folder-picker-foot { margin: 10px 0 4px; }

.chips-box {
  min-height: 40px;
  padding: 5px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--bg);
  cursor: text;
}
.chips-box:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.chips { display: flex; flex-wrap: wrap; align-items: center; gap: 5px; }
.chip-list { display: contents; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  height: 26px;
  padding: 0 4px 0 9px;
  border-radius: 13px;
  background: var(--accent-soft);
  color: var(--text);
  font-size: 13px;
  font-weight: 550;
}
.chip-remove {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}
.chip-remove:hover { background: var(--bg-hover); color: var(--text); }
.chips input {
  flex: 1;
  min-width: 140px;
  height: 26px;
  padding: 0 4px;
  border: 0;
  background: transparent;
  box-shadow: none !important;
}

.popover.font-popover { padding: 0; }
.font-note { margin: 8px 0 0 !important; text-align: center; }

/* ------------------------------------------------------------------ Outline */

.outline-panel {
  display: flex;
  flex-direction: column;
  flex: none;
  width: 250px;
  min-height: 0;
  border-left: 1px solid var(--border);
  background: var(--bg-app);
  font-family: var(--font-ui);
}
.outline-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 8px 4px 14px;
}
.outline-title { color: var(--text-faint); font-size: 11.5px; font-weight: 650; text-transform: uppercase; letter-spacing: 0.05em; }
.outline-list { flex: 1; min-height: 0; margin: 0; padding: 2px 6px 24px; overflow-y: auto; list-style: none; }
.outline-item { margin: 0; }
.outline-link {
  display: block;
  width: 100%;
  padding: 4px 8px 4px calc(8px + var(--depth, 0) * 14px);
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  line-height: 1.4;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}
.outline-item[style*="--depth: 0"] > .outline-link { color: var(--text); font-weight: 550; }
.outline-link:hover { background: var(--bg-hover); color: var(--text); }
.outline-link.active { background: var(--accent-soft); color: var(--accent) !important; }
.outline-empty { margin: 6px 14px; color: var(--text-faint); font-size: 12.5px; line-height: 1.45; }

/* ------------------------------------------------------------------ Find and replace in the visual editor */

.doc-find:empty { display: none; }
.find-bar {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-app);
  font-family: var(--font-ui);
}
.find-row { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; }
.find-input {
  flex: 0 1 280px;
  min-width: 120px;
  height: 28px;
  margin-right: 4px;
  font-size: 13px;
}
.find-input.invalid { border-color: var(--danger); box-shadow: 0 0 0 3px var(--danger-soft); }
.find-toggle {
  min-width: 28px;
  height: 26px;
  padding: 0 6px;
  border: 1px solid transparent;
  border-radius: 5px;
  background: transparent;
  color: var(--text-muted);
  font-family: var(--font-code);
  font-size: 12px;
  cursor: pointer;
}
.find-toggle:hover { background: var(--bg-hover); color: var(--text); }
.find-toggle[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.find-count { min-width: 76px; margin: 0 4px; color: var(--text-muted); font-size: 12.5px; }
.find-row .btn-small { margin-right: 2px; }
.ProseMirror .ProseMirror-search-match { background: var(--search-match); border-radius: 2px; }
.ProseMirror .ProseMirror-active-search-match { background: var(--search-match-selected); border-radius: 2px; }

/* ------------------------------------------------------------------ Emoji picker */

.popover.emoji-popover { width: 344px; max-width: calc(100vw - 16px); padding: 0; overflow: hidden; }
.emoji-picker { display: flex; flex-direction: column; height: min(380px, 70vh); }
.emoji-search { margin: 10px 10px 6px; height: 32px; }
.emoji-tabs { display: flex; gap: 2px; padding: 0 8px 6px; border-bottom: 1px solid var(--border); overflow-x: auto; scrollbar-width: none; }
.emoji-tab {
  flex: none;
  width: 32px;
  height: 30px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}
.emoji-tab:hover, .emoji-tab:focus-visible { background: var(--bg-hover); outline: none; }
.emoji-grid { flex: 1; min-height: 0; padding: 0 8px 8px; overflow-y: auto; }
.emoji-section h4 {
  position: sticky;
  top: 0;
  z-index: 1;
  margin: 0;
  padding: 8px 2px 4px;
  background: var(--surface);
  color: var(--text-faint);
  font-size: 11.5px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.emoji-row { display: grid; grid-template-columns: repeat(auto-fill, minmax(36px, 1fr)); }
.emoji-btn {
  height: 36px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  font-family: "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji", sans-serif;
}
.emoji-btn:hover, .emoji-btn:focus-visible { background: var(--bg-hover); outline: none; }
.emoji-empty { margin: 16px 4px; color: var(--text-faint); font-size: 13px; text-align: center; }
.emoji-footer { padding: 7px 12px; border-top: 1px solid var(--border); color: var(--text-faint); font-size: 12px; }
/* :smile: suggestions in the Source view */
.cm-tooltip-autocomplete .cm-completionLabel { font-family: var(--font-ui); }

/* ------------------------------------------------------------------ Code files */

.doc-icon.code { background: rgba(130, 80, 223, 0.12); color: #8250df; }
[data-theme="dark"] .doc-icon.code { background: rgba(210, 168, 255, 0.14); color: #d2a8ff; }
.doc-language { padding: 0 6px; border-radius: 4px; background: var(--bg-muted); font-size: 11.5px; font-weight: 550; }
.doc-locked, .doc-locked-mark { display: inline-flex; align-items: center; gap: 3px; color: var(--warning); }
.icon-btn.lock-on, .icon-btn.lock-on:hover { color: var(--warning); background: var(--warning-soft); }

.popover:has(.lang-picker) { padding: 8px; }
.lang-picker { display: flex; flex-direction: column; gap: 6px; width: 290px; max-width: calc(100vw - 32px); }
.lang-search { height: 32px; }
.lang-list { display: flex; flex-direction: column; gap: 1px; max-height: min(340px, 55vh); overflow-y: auto; }
.lang-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 30px;
  padding: 0 10px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--text);
  font-size: 13.5px;
  text-align: left;
  cursor: pointer;
}
.lang-option:hover, .lang-option:focus-visible { background: var(--bg-hover); outline: none; }
.lang-option.active { color: var(--accent); font-weight: 600; }
.lang-hint { overflow: hidden; color: var(--text-faint); font-family: var(--font-code); font-size: 11.5px; font-weight: 400; white-space: nowrap; text-overflow: ellipsis; }
.lang-empty { margin: 8px 10px; color: var(--text-faint); font-size: 13px; }

/* The code editor: folding, linked lines, spaces and tabs, ligatures. */
.doc-content > .cm-editor.code-editor .cm-content { padding-bottom: 40vh; }
.cm-editor .cm-foldGutter .cm-gutterElement { padding: 0 2px; }
.cm-fold-marker {
  display: inline-block;
  width: 14px;
  color: var(--text-faint);
  font-family: var(--font-ui);
  text-align: center;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.12s;
}
.cm-fold-marker:not(.open) { opacity: 1; color: var(--accent); }
.cm-gutters:hover .cm-fold-marker { opacity: 1; }
@media (hover: none) { .cm-fold-marker { opacity: 1; } }
.cm-editor .cm-foldPlaceholder {
  margin: 0 2px;
  padding: 0 5px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg-muted);
  color: var(--text-muted);
}
.cm-editor .cm-linkedLine { background: var(--accent-soft); }
.cm-editor .cm-trailingSpace { background: var(--danger-soft); }
.no-ligatures .cm-content, .no-ligatures .text-view { font-variant-ligatures: none; font-feature-settings: "liga" 0, "calt" 0; }

/* Highlighted code made outside an editor (printing, exports): the tok-* classes from cm.js. */
.tok-keyword { color: var(--tok-keyword); }
.tok-string { color: var(--tok-string); }
.tok-number { color: var(--tok-number); }
.tok-comment { color: var(--tok-comment); font-style: italic; }
.tok-function { color: var(--tok-function); }
.tok-type { color: var(--tok-type); }
.tok-property { color: var(--tok-property); }
.tok-tag { color: var(--tok-tag); }
.tok-variable { color: var(--tok-variable); }
.tok-meta { color: var(--tok-meta); }
.tok-heading { color: var(--tok-heading); font-weight: 700; }
.tok-link { color: var(--tok-link); }
.tok-quote { color: var(--tok-quote); }
.tok-inserted { color: var(--tok-inserted); }
.tok-deleted { color: var(--tok-deleted); }
.tok-invalid { color: var(--danger); }
.tok-strong { font-weight: 700; }
.tok-emphasis { font-style: italic; }
.tok-strike { text-decoration: line-through; }

/* ------------------------------------------------------------------ Math */

.md-body .math-display {
  display: block;
  margin: 0 0 1em;
  padding: 2px 0;
  overflow-x: auto;
  overflow-y: hidden;
  text-align: center;
}
.md-body .math-display .katex-display { margin: 0; }
.md-body .math-paragraph { overflow-x: auto; overflow-y: hidden; }
.md-body .math-inline .katex { white-space: nowrap; }
.math-error {
  color: var(--danger);
  font-family: var(--font-code);
  font-size: 0.85em;
  white-space: pre-wrap;
}
.md-body .katex, .milkdown .katex { font-size: 1.1em; }
.milkdown .milkdown-code-block .preview { overflow-x: auto; }
.milkdown .milkdown-code-block .preview svg { max-width: 100%; height: auto; }

/* ------------------------------------------------------------------ Diagrams */

.md-body .diagram { margin: 0 0 1.2em; }
.diagram-figure { display: flex; justify-content: center; padding: 6px 0; overflow-x: auto; }
.diagram-figure svg { max-width: 100%; height: auto; }
.diagram-loading { color: var(--text-faint); font-family: var(--font-ui); font-size: 13px; }
.diagram-code { margin-top: 2px; font-family: var(--font-ui); font-size: 12.5px; }
.diagram-code > summary { width: max-content; color: var(--text-faint); cursor: pointer; }
.diagram-code > summary:hover { color: var(--text-muted); }
.md-body .diagram-code pre { margin: 6px 0 0; font-size: 12.5px; }
.diagram-failed .diagram-figure { display: block; }
.diagram-error {
  margin: 0;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  background: var(--danger-soft);
  color: var(--danger);
  font-family: var(--font-ui);
  font-size: 13px;
  line-height: 1.45;
  text-align: left;
}

/* ------------------------------------------------------------------ Table of contents and page breaks */

.md-body nav.toc {
  margin: 0 0 1.6em;
  padding: 12px 18px 12px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-subtle);
  font-size: 0.92em;
  line-height: 1.5;
}
.md-body .toc-title { margin: 0 0 6px; font-family: var(--font-ui); font-size: 12px; font-weight: 650; letter-spacing: 0.05em; text-transform: uppercase; color: var(--text-faint); }
.md-body nav.toc ul { margin: 0; padding-left: 0; list-style: none; }
.md-body nav.toc ul ul { padding-left: 1.2em; }
.md-body nav.toc li + li, .md-body nav.toc li > ul { margin-top: 0.15em; }
.md-body nav.toc a { color: var(--text); text-decoration: none; }
.md-body nav.toc a:hover { color: var(--accent); text-decoration: underline; }

.md-body .page-break {
  position: relative;
  height: 0;
  margin: 2.2em 0;
  border-top: 1px dashed var(--border-strong);
}
.md-body .page-break::after {
  content: "Page break";
  position: absolute;
  top: -0.75em;
  left: 50%;
  transform: translateX(-50%);
  padding: 0 10px;
  background: var(--bg);
  color: var(--text-faint);
  font-family: var(--font-ui);
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

/* ------------------------------------------------------------------ Export */

.export-staging { position: fixed; top: 0; left: -10000px; width: 820px; pointer-events: none; }
.export-dialog .export-pdf { display: flex; flex-direction: column; }
.export-dialog .export-pdf[hidden], .export-dialog .export-html[hidden] { display: none; }
.export-dialog > div:has(> .checkbox) { margin-bottom: 14px; }
.export-dialog .checkbox + .hint { margin: 4px 0 0 24px; }
.export-dialog .hint + .hint { margin-top: 6px; }

/* ------------------------------------------------------------------ Print */

.print-root { display: none; }

@media print {
  body { background: #fff; }
  body.printing .app > *:not(#print-root) { display: none !important; }
  body.printing .app { display: block; height: auto; }
  body.printing #print-root { display: block; }
  #print-root .md-body { max-width: none; padding: 0; color: #000; }
  #print-root .text-view { padding: 0; color: #000; }
  #print-root .code-view { font-family: var(--doc-font); font-size: 0.82em; line-height: 1.5; }
  #print-root pre, #print-root table, #print-root img, #print-root .markdown-alert,
  #print-root .math-display, #print-root .diagram-figure { break-inside: avoid; }
  #print-root h1, #print-root h2, #print-root h3, #print-root h4 { break-after: avoid; }
  #print-root .code-tools, #print-root .code-lang, #print-root .copy-code, #print-root .header-anchor,
  #print-root .diagram-code, #print-root .diagram-loading { display: none !important; }
  #print-root .page-break { height: 0; margin: 0; border: 0; break-after: page; }
  #print-root .page-break::after { display: none; }
  #print-root[data-break-before="h1"] .md-body > h1:not(:first-child),
  #print-root[data-break-before="h2"] .md-body > h1:not(:first-child),
  #print-root[data-break-before="h2"] .md-body > h2:not(:first-child) { break-before: page; }
  #print-root nav.toc { border: 0; background: none; padding: 0; }
  #print-root nav.toc a { color: #000; }
  #print-root .md-body a { color: #0645ad; }
}

/* ------------------------------------------------------------------ Tablets and phones */

@media (max-width: 860px) {
  .workspace { grid-template-columns: minmax(0, 1fr) !important; }
  .sidebar {
    position: fixed;
    top: var(--topbar-h);
    bottom: 0;
    left: 0;
    width: min(300px, 86vw);
    z-index: 30;
    box-shadow: var(--shadow-lg);
  }
  [data-sidebar="hidden"] .sidebar { display: none; }
  .topbar { position: relative; gap: 6px; padding: 0 8px; }
  .brand { margin-right: 2px; }
  .topbar-search { flex: 1 1 200px; margin-left: 2px; }
  .topbar-actions .btn-label { display: none; }
  #new-button, #open-button { width: 34px; padding: 0; justify-content: center; }

  /* The document toolbar: the name on its own line, the view switch and key buttons below. */
  .doc-toolbar { gap: 6px; padding: 6px 8px; }
  .doc-toolbar .doc-name { flex: 1 1 100%; }
  .doc-toolbar .seg { flex: none; }
  .doc-toolbar .toolbar-tools { margin-left: auto; }
  .doc-toolbar .toolbar-tools .btn-label { display: none; }

  /* The outline opens over the text. */
  .outline-panel {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 6;
    width: min(300px, 86vw);
    box-shadow: var(--shadow-lg);
  }
  .outline-link { padding-top: 7px; padding-bottom: 7px; font-size: 14px; }

  .doc-content .milkdown .ProseMirror { padding: 20px 22px 100px; }
  .fm-card.fm-visual { width: calc(100% - 44px); margin-top: 20px; }
  .md-body { padding: 24px 20px 60px; }
  .home-inner { padding: 16px 14px 40px; }
  .find-bar { padding: 8px; }
}

@media (max-width: 560px) {
  :root { --topbar-h: 50px; --tabbar-h: 38px; }
  /* Settings: the font on its own row, size and line spacing below it. */
  .settings-grid { grid-template-columns: 1fr 1fr; }
  .settings-grid:not(.settings-grid-2) > .field:first-child { grid-column: 1 / -1; }
  .settings-grid.settings-grid-2 { grid-template-columns: 1fr; }
  .brand span { display: none; }
  #settings-button { display: none; }
  #account-button { max-width: 96px; }
  /* Search folds down to its icon and opens across the bar when tapped. */
  .topbar-search { flex: 1 1 36px; min-width: 36px; }
  .topbar-search input { padding-right: 4px; }
  .topbar-search:focus-within {
    position: absolute;
    inset: 8px 8px auto 8px;
    z-index: 5;
    margin: 0;
  }
  .topbar-search:focus-within input { height: 34px; box-shadow: var(--shadow); }
  .topbar-search:not(:focus-within) input::placeholder { color: transparent; }

  .doc-row { grid-template-columns: 30px minmax(0, 1fr) auto; }
  .doc-row .doc-icon { width: 30px; height: 30px; }
  .home-controls { flex-wrap: wrap; }
  .tab { max-width: 180px; }

  .doc-content .milkdown .ProseMirror { padding: 16px 16px 90px; }
  .fm-card.fm-visual { width: calc(100% - 32px); }
  .md-body { padding: 18px 16px 56px; }
  .md-body h1 { font-size: 1.7em; }
  .md-body h2 { font-size: 1.35em; }
  .text-view { padding: 16px 14px 48px; }
  .doc-content > .cm-editor .cm-line { padding: 0 10px; }
  .doc-content > .cm-editor .cm-lineNumbers .cm-gutterElement { padding: 0 6px 0 8px; min-width: 32px; }

  /* The status bar keeps the save state, the cursor and the counts. */
  .statusbar { gap: 10px; }
  .status-eol, .status-encoding, .status-indent { display: none; }

  /* Bigger targets for fingers. */
  .menu-item { min-height: 40px; font-size: 14.5px; }
  .find-input { flex: 1 1 100%; margin-right: 0; }
  .find-count { min-width: 0; }
  /* 16px text keeps iPhones from zooming into a field when it gets focus. */
  input[type="text"], input[type="search"], input[type="email"], input[type="password"],
  input[type="url"], input[type="number"], select, textarea { font-size: 16px; }
  dialog.dialog { width: calc(100vw - 20px); max-height: calc(100dvh - 20px); }
}

@media (pointer: coarse) {
  .icon-btn { min-width: 36px; min-height: 36px; }
  .menu-item { min-height: 40px; }
}

/* ------------------------------------------------------------------ Phase 3: full-window tools */

dialog.screen {
  width: calc(100vw - 32px);
  height: calc(100vh - 32px);
  max-width: 1500px;
  max-height: none;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}
dialog.screen[open] { display: flex; flex-direction: column; }
dialog.screen::backdrop { background: rgba(10, 14, 20, 0.45); }
.screen-head {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 14px;
  padding: 10px 12px 10px 18px;
  border-bottom: 1px solid var(--border);
}
.screen-head h2 {
  flex: 1 1 220px;
  min-width: 0;
  margin: 0;
  font-size: 16px;
  font-weight: 680;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.screen-tools { display: flex; align-items: center; flex-wrap: wrap; gap: 8px 12px; }
.screen-body { flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* ------------------------------------------------------------------ Differences */

:root {
  --diff-removed: rgba(207, 34, 46, 0.09);
  --diff-removed-word: rgba(207, 34, 46, 0.26);
  --diff-added: rgba(26, 127, 55, 0.1);
  --diff-added-word: rgba(26, 127, 55, 0.28);
  --diff-removed-text: #b4232c;
  --diff-added-text: #1a7f37;
}
[data-theme="dark"] {
  --diff-removed: rgba(248, 81, 73, 0.14);
  --diff-removed-word: rgba(248, 81, 73, 0.38);
  --diff-added: rgba(63, 185, 80, 0.14);
  --diff-added-word: rgba(63, 185, 80, 0.36);
  --diff-removed-text: #ff8b84;
  --diff-added-text: #56d364;
}
[data-theme="sepia"] {
  --diff-removed: rgba(176, 52, 40, 0.1);
  --diff-removed-word: rgba(176, 52, 40, 0.27);
  --diff-added: rgba(46, 116, 52, 0.11);
  --diff-added-word: rgba(46, 116, 52, 0.28);
}

.diff-view { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.diff-summary { font-size: 13px; color: var(--text-muted); white-space: nowrap; }
.diff-count { font-weight: 650; font-variant-numeric: tabular-nums; }
.diff-count.added { color: var(--diff-added-text); }
.diff-count.removed { color: var(--diff-removed-text); }
.diff-nav { display: flex; gap: 2px; }
.diff-spaces { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-muted); white-space: nowrap; cursor: pointer; }
.diff-head {
  display: grid;
  border-bottom: 1px solid var(--border);
  background: var(--bg-subtle);
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
}
.diff-head.diff-split { grid-template-columns: 1fr 1fr; }
.diff-head-cell { padding: 6px 12px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.diff-head.diff-split .diff-head-cell + .diff-head-cell { border-left: 1px solid var(--border); }
.diff-head-sep { margin: 0 8px; color: var(--text-faint); }
.diff-side-mark { display: inline-block; width: 1.2em; font-weight: 700; }
.diff-side-mark.removed { color: var(--diff-removed-text); }
.diff-side-mark.added { color: var(--diff-added-text); }
.diff-rows {
  flex: 1;
  min-height: 0;
  overflow: auto;
  font-family: var(--font-code);
  font-size: 13px;
  line-height: 1.5;
  tab-size: 4;
  outline: none;
}
.diff-rows:focus-visible { box-shadow: inset 0 0 0 2px var(--accent-soft); }
.diff-row { display: grid; align-items: stretch; }
.diff-split .diff-row { grid-template-columns: minmax(3.2em, max-content) minmax(0, 1fr) minmax(3.2em, max-content) minmax(0, 1fr); }
.diff-inline .diff-row { grid-template-columns: minmax(3.2em, max-content) minmax(3.2em, max-content) 1.6em minmax(0, 1fr); }
.diff-num {
  padding: 0 8px;
  text-align: right;
  color: var(--text-faint);
  background: var(--editor-gutter);
  user-select: none;
  font-variant-numeric: tabular-nums;
}
.diff-split .diff-row > .diff-num:nth-child(3) { border-left: 1px solid var(--border); }
.diff-sign { text-align: center; color: var(--text-faint); user-select: none; }
.diff-text { padding: 0 10px; white-space: pre-wrap; overflow-wrap: anywhere; min-width: 0; }
.diff-num.removed, .diff-sign.removed, .diff-text.removed { background: var(--diff-removed); }
.diff-num.added, .diff-sign.added, .diff-text.added { background: var(--diff-added); }
.diff-sign.removed { color: var(--diff-removed-text); }
.diff-sign.added { color: var(--diff-added-text); }
.diff-num.empty, .diff-text.empty {
  background: repeating-linear-gradient(135deg, transparent 0 6px, var(--bg-hover) 6px 7px);
}
.diff-text.removed .diff-word { background: var(--diff-removed-word); border-radius: 2px; }
.diff-text.added .diff-word { background: var(--diff-added-word); border-radius: 2px; }
.diff-change-start { scroll-margin: 40px; }
.diff-current > * { box-shadow: inset 0 2px 0 var(--accent); }
.diff-fold-row { grid-template-columns: 1fr !important; }
.diff-fold {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  padding: 3px 12px;
  border: 0;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  background: var(--bg-subtle);
  color: var(--text-muted);
  font: 12.5px var(--font-ui);
  cursor: pointer;
  text-align: left;
}
.diff-fold:hover { background: var(--bg-hover); color: var(--text); }
.diff-same-note {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 48px 20px;
  font-family: var(--font-ui);
  font-size: 15px;
  color: var(--text-muted);
}
.diff-same-note .icon { color: var(--success); }

/* ------------------------------------------------------------------ Version history */

.history { flex: 1; min-height: 0; display: grid; grid-template-columns: 280px minmax(0, 1fr); }
.history-side { border-right: 1px solid var(--border); overflow-y: auto; background: var(--bg-subtle); }
.history-list { list-style: none; margin: 0; padding: 6px; }
.history-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 8px 10px;
  border-radius: var(--radius);
  cursor: pointer;
  outline: none;
}
.history-item:hover { background: var(--bg-hover); }
.history-item:focus-visible { box-shadow: 0 0 0 2px var(--accent-soft); }
.history-item[aria-selected="true"] { background: var(--bg-active); }
.history-when { font-size: 13.5px; font-weight: 600; }
.history-meta { display: flex; gap: 10px; font-size: 12px; color: var(--text-muted); }
.history-reason { font-size: 12px; color: var(--warning); }
.history-empty { padding: 28px 16px; text-align: center; color: var(--text-muted); }
.history-empty p { margin: 8px 0 0; font-size: 13px; }
.history-detail { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.history-detail[hidden] { display: none; }
.history-actions { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; padding: 10px 14px 4px; }
.history-actions .spacer { flex: 1; }
.history-note { margin: 4px 14px 10px; font-size: 12.5px; }

/* ------------------------------------------------------------------ Choosing what to compare */

.compare-pick { display: flex; flex-direction: column; gap: 8px; }
.pick-heading { margin: 10px 0 0; font-size: 12px; font-weight: 650; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); }
.pick-heading:first-child { margin-top: 0; }
.pick-list { display: flex; flex-direction: column; max-height: 260px; overflow-y: auto; border: 1px solid var(--border); border-radius: var(--radius); }
.pick-list:empty { display: none; }
.pick-list > .muted { margin: 0; padding: 10px 12px; font-size: 13px; }
.pick-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border: 0;
  border-bottom: 1px solid var(--border);
  background: none;
  color: var(--text);
  font: inherit;
  font-size: 14px;
  text-align: left;
  cursor: pointer;
}
.pick-item:last-child { border-bottom: 0; }
.pick-item:hover, .pick-item:focus-visible { background: var(--bg-hover); outline: none; }
.pick-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pick-sub { font-size: 12px; color: var(--text-muted); white-space: nowrap; }
.compare-pick > .btn { align-self: flex-start; }

.doc-expiry { color: var(--text-muted); }
.doc-expiry.soon { color: var(--danger); }

/* Importing a folder or .zip, and exporting the library */
.import-plan, .import-report { display: flex; flex-direction: column; gap: 12px; }
.import-plan > p, .import-report > p { margin: 0; }
.import-summary { display: flex; gap: 12px; align-items: flex-start; }
.import-summary > svg { flex: none; margin-top: 2px; color: var(--accent); }
.import-summary p { margin: 0 0 4px; }
.import-count { font-weight: 600; }
.import-where { margin: 4px 0 0; }
.import-where-row { display: flex; align-items: center; gap: 8px; min-width: 0; }
.import-where-icon { display: inline-flex; color: var(--text-muted); }
.import-where-path { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.import-new-folder { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.import-new-folder input[type="text"] { flex: 1; min-width: 160px; }
.import-new-folder input[type="text"]:disabled { opacity: 0.55; }
.import-details summary { cursor: pointer; font-size: 13px; color: var(--accent); }
.import-details ul { margin: 8px 0 0; padding-left: 18px; max-height: 200px; overflow-y: auto; font-size: 12.5px; }
.import-details li { margin-bottom: 3px; overflow-wrap: anywhere; }
.import-path { font-family: var(--font-code); font-size: 12px; }
.import-lines { margin: 0; padding-left: 18px; font-size: 13.5px; }
.import-lines li { margin-bottom: 3px; }
dialog.progress-dialog { width: min(460px, calc(100vw - 32px)); }
.progress-label { margin: 0 0 8px; font-weight: 600; }
.progress-bar { width: 100%; height: 8px; accent-color: var(--accent); }
.progress-detail { min-height: 1.4em; margin: 8px 0 0; font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

@media (max-width: 860px) {
  dialog.screen { width: 100vw; height: 100vh; height: 100dvh; max-width: none; border: 0; border-radius: 0; }
  .history { grid-template-columns: 1fr; grid-template-rows: minmax(120px, 32%) minmax(0, 1fr); }
  .history-side { border-right: 0; border-bottom: 1px solid var(--border); }
  .diff-layout { display: none; }
}
