/* ------------------------------------------------------------------ *
 *  Markdown Reader — reading-view styling
 * ------------------------------------------------------------------ */

:root {
  --accent: #1f6f4a; /* dark green (default) */
  --accent-strong: #1a5c3e;
  --accent-tint: rgba(31, 111, 74, 0.14);
  --accent-tint-strong: rgba(31, 111, 74, 0.24);
  /* Link colour — a darkened-for-contrast accent on light backgrounds (recomputed
     from JS in applyAccent), lightened instead on dark backgrounds below. Meets
     WCAG 1.4.3 AA (4.5:1) for whatever accent the user picks. */
  --accent-readable: #1a5c3e;
  --link: var(--accent-readable);
  /* Boundary colour for interactive controls (inputs, buttons) — ≥ 3:1 per WCAG
     1.4.11. `--border` stays for hairline separators, which are exempt. */
  --control-border: rgba(0, 0, 0, 0.45);

  --font-ui: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Helvetica Neue', sans-serif;
  --font-reading:
    -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'New York', 'Helvetica Neue', sans-serif;
  --font-mono: 'SF Mono', ui-monospace, 'JetBrains Mono', Menlo, Consolas, monospace;

  --reading-size: 16px;

  /* Light palette */
  --bg: #ffffff;
  --bg-elevated: #f5f5f7;
  --bg-sidebar: rgba(245, 245, 247, 0.72);
  --titlebar: rgba(255, 255, 255, 0.68);
  --text: #1d1d1f;
  --text-secondary: #6e6e73;
  --border: rgba(0, 0, 0, 0.09);
  --code-bg: #f5f5f7;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.18);

  --hl-comment: #8a8a8f;
  --hl-keyword: var(--accent);
  --hl-string: #b3541e;
  --hl-number: #1c6fb3;
  --hl-title: #7b3fb3;
  --hl-attr: #1c6fb3;

  /* GitHub-alert accents — the title text sits on a 10% tint of this colour, so
     each value clears WCAG 1.4.3 AA (≥ 4.5:1) there; dark variants below. */
  --alert-note: #0b5a94;
  --alert-tip: #136c39;
  --alert-important: #6639ba;
  --alert-warning: #7d5a00;
  --alert-caution: #b62324;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-appearance='light']) {
    --bg: #1e1e1e;
    --bg-elevated: #2a2a2c;
    --bg-sidebar: rgba(30, 30, 30, 0.66);
    --titlebar: rgba(40, 40, 42, 0.6);
    --text: #f5f5f7;
    --text-secondary: #9b9ba0;
    --border: rgba(255, 255, 255, 0.1);
    --code-bg: #2a2a2c;
    --shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
    --control-border: rgba(255, 255, 255, 0.45);
    --link: color-mix(in srgb, var(--accent) 55%, #ffffff);

    --hl-comment: #7d7d85;
    --hl-string: #e0955f;
    --hl-number: #6cb6ff;
    --hl-title: #c799e8;
    --hl-attr: #6cb6ff;

    --alert-note: #79c0ff;
    --alert-tip: #3fb950;
    --alert-important: #d2a8ff;
    --alert-warning: #e3b341;
    --alert-caution: #ff9492;
  }
}

[data-appearance='dark'] {
  --bg: #1e1e1e;
  --bg-elevated: #2a2a2c;
  --bg-sidebar: rgba(30, 30, 30, 0.66);
  --titlebar: rgba(40, 40, 42, 0.6);
  --text: #f5f5f7;
  --text-secondary: #9b9ba0;
  --border: rgba(255, 255, 255, 0.1);
  --code-bg: #2a2a2c;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  --control-border: rgba(255, 255, 255, 0.45);
  --link: color-mix(in srgb, var(--accent) 55%, #ffffff);

  --hl-comment: #7d7d85;
  --hl-string: #e0955f;
  --hl-number: #6cb6ff;
  --hl-title: #c799e8;
  --hl-attr: #6cb6ff;

  --alert-note: #79c0ff;
  --alert-tip: #3fb950;
  --alert-important: #d2a8ff;
  --alert-warning: #e3b341;
  --alert-caution: #ff9492;
}

* {
  box-sizing: border-box;
}

/* `hidden` attribute must win over the display rules below (#id selectors
   otherwise out-rank the UA `[hidden]` rule and the element stays visible). */
[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font-ui);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* ---------- Title bar ---------- */

#titlebar {
  -webkit-app-region: drag;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 52px;
  padding: 0 16px 0 84px; /* leave room for macOS traffic lights */
  background: var(--titlebar);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--border);
  flex: none;
}

/* Windows / Linux keep the OS title bar above; this is just a toolbar row. */
#app[data-platform='win32'] #titlebar,
#app[data-platform='linux'] #titlebar {
  padding-left: 16px;
  height: 46px;
  -webkit-app-region: no-drag;
}
#app[data-platform='win32'] #doc-title,
#app[data-platform='linux'] #doc-title {
  font-size: 12.5px;
}

/* When the tab strip is showing it carries the traffic lights and the
   document name, so the toolbar row below it slims down. */
#app.tabbed #titlebar {
  padding-left: 16px;
  height: 44px;
}
#app.tabbed #doc-title {
  display: none;
}

/* ---------- Tab strip ---------- */

#tabstrip {
  -webkit-app-region: drag;
  display: flex;
  align-items: flex-end;
  gap: 4px;
  height: 40px;
  padding: 0 8px 0 84px; /* room for macOS traffic lights */
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  flex: none;
}
#app[data-platform='win32'] #tabstrip,
#app[data-platform='linux'] #tabstrip {
  padding-left: 8px;
}
.tabstrip-drag {
  -webkit-app-region: drag;
  align-self: stretch;
  width: 4px;
  flex: none;
}
#tabs {
  -webkit-app-region: no-drag;
  display: flex;
  align-items: flex-end;
  gap: 2px;
  flex: 1;
  min-width: 0;
  overflow: hidden;
}

.tab {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 30px;
  min-width: 66px;
  max-width: 220px;
  padding: 0 8px 0 12px;
  border-radius: 8px 8px 0 0;
  font-size: 12px;
  color: var(--text-secondary);
  background: transparent;
  cursor: default;
  user-select: none;
  flex: 0 1 200px;
}
.tab:hover {
  background: color-mix(in srgb, var(--text) 6%, transparent);
}
.tab.active {
  background: var(--bg);
  color: var(--text);
  box-shadow: 0 -1px 0 var(--border) inset;
}
.tab-name {
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tab.dirty .tab-name::before {
  content: '● ';
  color: var(--accent);
  font-size: 0.8em;
}
.tab-close {
  -webkit-app-region: no-drag;
  flex: none;
  width: 16px;
  height: 16px;
  display: grid;
  place-items: center;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: var(--text-secondary);
  font-size: 9px;
  cursor: pointer;
  opacity: 0;
}
.tab:hover .tab-close,
.tab.active .tab-close {
  opacity: 0.75;
}
.tab-close:hover {
  opacity: 1;
  background: color-mix(in srgb, var(--text) 12%, transparent);
}
.tab-add {
  -webkit-app-region: no-drag;
  flex: none;
  width: 26px;
  height: 26px;
  margin-bottom: 2px;
  display: grid;
  place-items: center;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-secondary);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
}
.tab-add:hover {
  background: color-mix(in srgb, var(--text) 8%, transparent);
  color: var(--text);
}

.titlebar-drag {
  flex: 1;
  overflow: hidden;
}

#doc-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  display: block;
}

.titlebar-actions {
  -webkit-app-region: no-drag;
  display: flex;
  gap: 8px;
}

.icon-btn {
  -webkit-app-region: no-drag;
  border: 1px solid var(--control-border);
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 12px;
  font-weight: 500;
  padding: 5px 12px;
  border-radius: 7px;
  cursor: pointer;
  transition:
    background 0.12s ease,
    transform 0.06s ease;
}
.icon-btn:hover {
  background: var(--accent-tint);
}
.icon-btn:active {
  transform: scale(0.96);
}
/* Draw the eye to Save while there are unsaved changes. */
#app.dirty #btn-save {
  border-color: var(--accent-strong);
  background: var(--accent-strong);
  color: #fff;
}
#app.dirty #btn-save:hover {
  filter: brightness(1.06);
}

/* ---------- Body layout ---------- */

#body {
  display: flex;
  flex: 1;
  min-height: 0;
}

#sidebar {
  width: 240px;
  flex: none;
  background: var(--bg-sidebar);
  backdrop-filter: saturate(180%) blur(20px);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 12px 8px 24px;
  transition:
    margin-left 0.22s ease,
    opacity 0.18s ease;
}

#app.sidebar-hidden #sidebar {
  margin-left: -240px;
  opacity: 0;
  pointer-events: none;
}

.sidebar-head {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-secondary);
  padding: 6px 10px 10px;
}

#toc a {
  display: block;
  font-size: 12.5px;
  color: var(--text-secondary);
  text-decoration: none;
  padding: 5px 10px;
  border-radius: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#toc a:hover {
  background: var(--accent-tint);
  color: var(--text);
}
#toc a.active {
  background: var(--accent-strong);
  color: #fff;
}
#toc a.lvl-2 {
  padding-left: 22px;
}
#toc a.lvl-3 {
  padding-left: 34px;
  font-size: 12px;
}

/* ---------- Content ---------- */

#content {
  flex: 1;
  min-width: 0;
  position: relative;
  overflow-y: auto;
}

#split {
  display: contents;
}

#markdown {
  max-width: var(--reading-width, 760px);
  margin: 0 auto;
  padding: 48px 56px 120px;
}

/* ---------- Edit mode (split editor | preview) ---------- */

#editor-pane {
  display: none;
}
#editor,
#cm-host {
  display: none;
}

#app.editing #content {
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
#app.editing #split {
  display: flex;
  flex: 1;
  min-height: 0;
}
#app.editing #editor-pane {
  display: block;
  position: relative;
  flex: 1 1 50%;
  min-width: 0;
  height: 100%;
  border-right: 1px solid var(--border);
  background: var(--bg);
  overflow: hidden;
}
#app.editing #editor-pane #editor {
  display: block;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 24px 26px 80px;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 13.5px;
  line-height: 1.7;
  tab-size: 2;
  white-space: pre;
  overflow: auto;
}
#app.editing.cm #editor-pane #editor {
  display: none;
}
#app.editing.cm #editor-pane #cm-host {
  display: block;
  height: 100%;
}
#cm-host .cm-editor {
  height: 100%;
}
/* Vim mode command line / status (from @replit/codemirror-vim). */
#cm-host .cm-vim-panel,
#cm-host .cm-panel.cm-vim-panel {
  background: var(--bg-elevated);
  color: var(--text);
  border-top: 1px solid var(--border);
  font: 12px var(--font-mono);
  padding: 3px 8px;
}
#cm-host .cm-vim-panel input {
  color: var(--text);
  font: inherit;
}
#cm-host .cm-fat-cursor {
  background: var(--accent) !important;
  color: var(--bg) !important;
}
/* Floating GFM table toolbar. */
#cm-host .cm-table-toolbar {
  position: absolute;
  top: 8px;
  right: 14px;
  z-index: 5;
  display: flex;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-elevated);
  box-shadow: var(--shadow);
}
#cm-host .cm-table-toolbar button {
  border: none;
  border-radius: 5px;
  padding: 4px 7px;
  font: 11px var(--font-ui);
  color: var(--text);
  background: transparent;
  cursor: pointer;
  white-space: nowrap;
}
#cm-host .cm-table-toolbar button:hover {
  background: var(--accent-tint);
}
@media (prefers-reduced-motion: reduce) {
  #cm-host .cm-table-toolbar {
    box-shadow: none;
  }
}
#app.editing #markdown {
  flex: 1 1 50%;
  min-width: 0;
  max-width: none;
  height: 100%;
  margin: 0;
  padding: 32px 40px 100px;
  overflow-y: auto;
}
#app.editing #btn-edit {
  background: var(--accent-strong);
  color: #fff;
  border-color: var(--accent-strong);
}

/* dirty marker */
#app.dirty #doc-title::before {
  content: '● ';
  color: var(--accent);
}

#empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 8px;
  color: var(--text-secondary);
  padding: 40px;
}
#app.has-doc #empty-state {
  display: none;
}
#app:not(.has-doc) #markdown {
  display: none;
}

.empty-logo {
  display: block;
  width: 72px;
  height: 72px;
  margin-bottom: 6px;
  filter: drop-shadow(0 9px 18px rgba(18, 78, 55, 0.2));
  user-select: none;
}
#empty-state h1 {
  font-size: 22px;
  color: var(--text);
  margin: 0;
}
#empty-state .hint {
  font-size: 12px;
  max-width: 420px;
  line-height: 1.5;
  margin-top: 6px;
}
#empty-state .hint a {
  color: var(--link);
}

.empty-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  margin-top: 14px;
}
.primary-btn,
.ghost-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  font-size: 13px;
  font-weight: 600;
  line-height: 1.15;
  padding: 9px 20px;
  border-radius: 9px;
  cursor: pointer;
  text-decoration: none;
  transition:
    background 0.12s ease,
    transform 0.06s ease;
}
.primary-btn {
  background: var(--accent-strong);
  color: #fff;
}
.primary-btn:hover {
  background: var(--accent-strong);
  filter: brightness(0.92);
}
.ghost-btn {
  background: var(--bg-elevated);
  color: var(--text);
  border: 1px solid var(--control-border);
}
.ghost-btn:hover {
  background: var(--accent-tint);
}
.primary-btn:active,
.ghost-btn:active {
  transform: scale(0.97);
}

/* drag & drop */
#app.drag-over #content::after {
  content: 'Drop to open';
  position: absolute;
  inset: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  font-weight: 600;
  color: var(--link);
  border: 2.5px dashed var(--accent);
  border-radius: 16px;
  background: var(--accent-tint);
  z-index: 20;
}

/* ---------- Remote-image banner ---------- */

#remote-banner,
#disk-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  max-width: 760px;
  margin: 16px auto 0;
  padding: 9px 14px;
  font-size: 12.5px;
  color: var(--text-secondary);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 9px;
}
#disk-banner {
  border-color: var(--accent);
}
#disk-banner .mini-btn {
  border: 1px solid var(--control-border);
  padding: 3px 9px;
  border-radius: 6px;
}

/* Draft-recovery banner — a full-width strip under the toolbar, so it is
   visible and clickable even with no document open (the empty state). */
#draft-banner {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 8px 16px;
  font-size: 12.5px;
  color: var(--text);
  background: var(--accent-tint);
  border-bottom: 1px solid var(--accent);
}
#draft-banner b {
  font-weight: 600;
}
#draft-banner .mini-btn {
  border: 1px solid var(--control-border);
  background: var(--bg);
  padding: 3px 10px;
  border-radius: 6px;
}
#draft-banner .draft-restore {
  border-color: var(--accent-strong);
  background: var(--accent-strong);
  color: #fff;
}
#remote-banner b {
  color: var(--text);
  font-weight: 600;
}
#remote-banner .remote-load {
  border: 1px solid var(--accent);
  color: var(--accent);
  padding: 4px 10px;
  border-radius: 6px;
  white-space: nowrap;
}

.markdown-body img.img-blocked,
.markdown-body img.img-missing {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 120px;
  min-height: 44px;
  padding: 8px 14px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  color: var(--text-secondary);
  font-family: var(--font-ui);
  font-size: 12px;
}

.toc-empty {
  font-size: 12px;
  color: var(--text-secondary);
  padding: 6px 10px;
}

/* ---------- Find bar ---------- */

#find-bar {
  position: sticky;
  top: 0;
  z-index: 15;
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 12px auto 0;
  max-width: 760px;
  padding: 8px 12px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
}
#find-input {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 13px;
  outline: none;
}
#find-count {
  font-size: 11px;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}
.mini-btn {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 12px;
  cursor: pointer;
  padding: 3px 6px;
  border-radius: 5px;
}
.mini-btn:hover {
  background: var(--accent-tint);
  color: var(--text);
}

mark.find-hit {
  background: var(--accent-tint);
  color: inherit;
  border-radius: 2px;
}
mark.find-hit.current {
  background: var(--accent-strong);
  color: #fff;
}

/* ---------- Markdown typography ---------- */

.markdown-body {
  font-family: var(--font-reading);
  font-size: var(--reading-size);
  line-height: 1.68;
  color: var(--text);
  word-wrap: break-word;
}
.markdown-body > *:first-child {
  margin-top: 0;
}
.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4 {
  font-family: var(--font-ui);
  font-weight: 700;
  line-height: 1.25;
  margin: 1.8em 0 0.6em;
  scroll-margin-top: 24px;
}
.markdown-body h1 {
  font-size: 1.9em;
  letter-spacing: -0.02em;
}
.markdown-body h2 {
  font-size: 1.45em;
  letter-spacing: -0.015em;
  padding-bottom: 0.3em;
  border-bottom: 1px solid var(--border);
}
.markdown-body h3 {
  font-size: 1.2em;
}
.markdown-body h4 {
  font-size: 1.02em;
  color: var(--text-secondary);
}
.markdown-body p {
  margin: 0 0 1.1em;
}
.markdown-body a {
  color: var(--link);
  text-decoration: none;
}
.markdown-body a:hover {
  text-decoration: underline;
}
/* `headerLink()` wraps the whole heading text in this anchor — keep it looking
   like plain heading text, tint only on hover. */
.markdown-body .header-anchor {
  color: inherit;
  text-decoration: none;
}
.markdown-body .header-anchor:hover {
  color: var(--accent);
}
.markdown-body ul,
.markdown-body ol {
  margin: 0 0 1.1em;
  padding-left: 1.6em;
}
.markdown-body li {
  margin: 0.25em 0;
}
.markdown-body li::marker {
  color: var(--accent);
}
.markdown-body blockquote {
  margin: 0 0 1.1em;
  padding: 0.6em 1.2em;
  border-left: 4px solid var(--accent);
  background: var(--accent-tint-strong, var(--accent-tint));
  border-radius: 0 10px 10px 0;
  color: var(--text);
}
.markdown-body blockquote p {
  margin: 0.5em 0;
}
.markdown-body blockquote > *:first-child {
  margin-top: 0;
}
.markdown-body blockquote > *:last-child {
  margin-bottom: 0;
}
.markdown-body blockquote a {
  color: var(--link);
}
.markdown-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2em 0;
}
.markdown-body img {
  max-width: 100%;
  border-radius: 10px;
}
.markdown-body code {
  font-family: var(--font-mono);
  font-size: 0.86em;
  background: var(--code-bg);
  padding: 0.15em 0.4em;
  border-radius: 5px;
}
.markdown-body pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px;
  overflow-x: auto;
  margin: 0 0 1.2em;
  line-height: 1.55;
}
.markdown-body pre code {
  background: none;
  padding: 0;
  font-size: 0.84em;
}

/* code block + copy button */
.markdown-body .code-block {
  position: relative;
  margin: 0 0 1.2em;
}
.markdown-body .code-block pre {
  margin: 0;
}
.code-copy {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 3px 9px;
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 500;
  color: var(--text-secondary);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  opacity: 0;
  transition:
    opacity 0.12s ease,
    color 0.12s ease;
}
.markdown-body .code-block:hover .code-copy,
.code-copy:focus-visible {
  opacity: 1;
}
.code-copy:hover {
  color: var(--text);
}
.code-copy.done {
  color: #fff;
  background: var(--accent-strong);
  border-color: var(--accent-strong);
  opacity: 1;
}

/* mermaid */
.markdown-body .mermaid-figure {
  margin: 0 0 1.4em;
  text-align: center;
  overflow-x: auto;
}
.markdown-body .mermaid-figure svg {
  max-width: 100%;
  height: auto;
}
.markdown-body .mermaid-error {
  margin: 0 0 1.2em;
  padding: 12px 14px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  color: var(--text-secondary);
  font-size: 13px;
}

/* KaTeX */
.markdown-body .katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  padding: 4px 0;
}
.markdown-body .katex {
  font-size: 1.05em;
}
.markdown-body table {
  border-collapse: collapse;
  width: 100%;
  margin: 0 0 1.2em;
  font-size: 0.94em;
}
.markdown-body th,
.markdown-body td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  text-align: left;
}
.markdown-body th {
  background: var(--bg-elevated);
  font-weight: 600;
}
.markdown-body tr:nth-child(even) td {
  background: var(--accent-tint);
}
.markdown-body input[type='checkbox'] {
  accent-color: var(--accent);
  margin-right: 0.4em;
}

/* ---------- GFM extras: alerts, task lists, footnotes, misc ---------- */

.markdown-body .markdown-alert {
  margin: 0 0 1.2em;
  padding: 10px 16px;
  border-left: 3px solid var(--alert-color, var(--accent));
  border-radius: 0 8px 8px 0;
  background: color-mix(in srgb, var(--alert-color, var(--accent)) 10%, transparent);
}
.markdown-body .markdown-alert > :first-child {
  margin-top: 0;
}
.markdown-body .markdown-alert > :last-child {
  margin-bottom: 0;
}
.markdown-body .markdown-alert-title {
  display: flex;
  align-items: center;
  gap: 6px;
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 0.9em;
  color: var(--alert-color, var(--accent));
  margin-bottom: 0.4em;
}
.markdown-body .markdown-alert-title svg {
  fill: currentColor;
}
/* Alert accents resolve to theme-aware, AA-safe tokens (defined at the top). */
.markdown-body .markdown-alert-note {
  --alert-color: var(--alert-note);
}
.markdown-body .markdown-alert-tip {
  --alert-color: var(--alert-tip);
}
.markdown-body .markdown-alert-important {
  --alert-color: var(--alert-important);
}
.markdown-body .markdown-alert-warning {
  --alert-color: var(--alert-warning);
}
.markdown-body .markdown-alert-caution {
  --alert-color: var(--alert-caution);
}

.markdown-body ul.contains-task-list {
  list-style: none;
  padding-left: 0.4em;
}
.markdown-body .task-list-item {
  display: flex;
  align-items: flex-start;
  gap: 0.5em;
}
.markdown-body .task-list-item::marker {
  content: none;
}
.markdown-body .task-list-item-checkbox {
  margin-top: 0.35em;
  cursor: pointer;
}
.markdown-body .task-list-item.enabled .task-list-item-checkbox {
  cursor: pointer;
}

.markdown-body .footnotes {
  margin-top: 2.4em;
  padding-top: 1em;
  border-top: 1px solid var(--border);
  font-size: 0.9em;
  color: var(--text-secondary);
}
.markdown-body .footnotes ol {
  padding-left: 1.4em;
}
.markdown-body .footnote-ref a,
.markdown-body .footnote-backref {
  color: var(--link);
  text-decoration: none;
  font-weight: 500;
}
.markdown-body sup,
.markdown-body sub {
  line-height: 0;
}
.markdown-body mark {
  background: var(--accent-tint-strong);
  color: inherit;
  border-radius: 2px;
  padding: 0.05em 0.2em;
}
.markdown-body ins {
  text-decoration: none;
  border-bottom: 2px solid var(--accent);
}
.markdown-body abbr[title] {
  text-decoration-style: dotted;
  text-underline-offset: 2px;
  cursor: help;
}
.markdown-body dl {
  margin: 0 0 1.1em;
}
.markdown-body dt {
  font-weight: 600;
  margin-top: 0.6em;
}
.markdown-body dd {
  margin: 0.2em 0 0.2em 1.4em;
  color: var(--text-secondary);
}

/* code block language label + wrap toggle */
.code-block {
  --code-wrap: pre;
}
.code-lang {
  position: absolute;
  top: 8px;
  left: 12px;
  font-family: var(--font-ui);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-secondary);
  opacity: 0;
  transition: opacity 0.12s ease;
}
.markdown-body .code-block:hover .code-lang {
  opacity: 0.7;
}
.code-block.wrap pre code {
  white-space: pre-wrap;
  word-break: break-word;
}

/* image lightbox */
#lightbox {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  background: rgba(0, 0, 0, 0.82);
  cursor: zoom-out;
}
#lightbox img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 8px;
  box-shadow: 0 20px 70px rgba(0, 0, 0, 0.5);
}
.markdown-body img:not(.img-blocked):not(.img-missing) {
  cursor: zoom-in;
}

/* mermaid: export-on-hover */
.mermaid-figure {
  position: relative;
}
.mermaid-export {
  position: absolute;
  top: 6px;
  right: 6px;
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.12s ease;
}
.mermaid-figure:hover .mermaid-export {
  opacity: 1;
}
.mermaid-export button {
  font-family: var(--font-ui);
  font-size: 10px;
  font-weight: 600;
  padding: 2px 7px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--bg);
  color: var(--text-secondary);
  cursor: pointer;
}
.mermaid-export button:hover {
  color: var(--text);
}

/* ---------- Document metadata card ---------- */

.markdown-body .doc-meta {
  margin: 0 0 1.6em;
  padding: 4px 16px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: 10px;
  font-family: var(--font-ui);
  font-size: 0.86em;
}
.markdown-body .doc-meta-row {
  display: flex;
  gap: 14px;
  padding: 7px 0;
}
.markdown-body .doc-meta-row + .doc-meta-row {
  border-top: 1px solid var(--border);
}
.markdown-body .doc-meta-label {
  flex: none;
  width: 108px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-size: 0.82em;
  font-weight: 600;
  padding-top: 1px;
}
.markdown-body .doc-meta-value {
  flex: 1;
  min-width: 0;
  color: var(--text);
  word-break: break-word;
}
.markdown-body .doc-meta-value a {
  color: var(--link);
}

/* ---------- Syntax highlighting (highlight.js classes) ---------- */

.hljs-comment,
.hljs-quote {
  color: var(--hl-comment);
  font-style: italic;
}
.hljs-keyword,
.hljs-selector-tag,
.hljs-built_in,
.hljs-literal {
  color: var(--hl-keyword);
}
.hljs-string,
.hljs-regexp,
.hljs-addition {
  color: var(--hl-string);
}
.hljs-number,
.hljs-symbol,
.hljs-bullet {
  color: var(--hl-number);
}
.hljs-title,
.hljs-section,
.hljs-name {
  color: var(--hl-title);
}
.hljs-attr,
.hljs-attribute,
.hljs-variable,
.hljs-template-variable,
.hljs-type {
  color: var(--hl-attr);
}
.hljs-emphasis {
  font-style: italic;
}
.hljs-strong {
  font-weight: 700;
}

/* ---------- Preferences ---------- */

#prefs {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.28);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 90px;
  z-index: 40;
}

/* ---------- Remote filesystem connections ---------- */

#remote-dialog {
  position: fixed;
  inset: 0;
  z-index: 58;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 7vh 18px 24px;
  overflow-y: auto;
  background: rgba(0, 0, 0, 0.32);
}
.remote-panel {
  width: min(660px, 96vw);
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--bg);
  box-shadow: var(--shadow);
}
.remote-subtitle {
  margin: 4px 0 0;
  color: var(--text-secondary);
  font-size: 12px;
}
#remote-saved {
  display: grid;
  gap: 6px;
  margin: 12px 0;
}
.remote-saved-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--bg-elevated);
}
.remote-saved-row .remote-saved-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  font-weight: 600;
}
.remote-protocol-label {
  color: var(--text-secondary);
  font-size: 11px;
  text-transform: uppercase;
}
#remote-form {
  padding-top: 12px;
  border-top: 1px solid var(--border);
}
.remote-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.remote-grid label {
  display: grid;
  gap: 5px;
  color: var(--text-secondary);
  font-size: 12px;
}
.remote-grid input,
.remote-grid select {
  box-sizing: border-box;
  width: 100%;
  height: 34px;
  padding: 0 9px;
  border: 1px solid var(--control-border);
  border-radius: 7px;
  outline: none;
  color: var(--text);
  background: var(--bg-elevated);
  font: 13px var(--font-ui);
}
.remote-grid input:focus,
.remote-grid select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-tint);
}
.remote-grid small {
  color: var(--text-secondary);
  font-size: 10px;
}
.remote-full {
  grid-column: 1 / -1;
}
#remote-status {
  min-height: 18px;
  margin: 10px 0 2px;
  color: var(--text-secondary);
  font-size: 12px;
}
#remote-status.error {
  color: #b5473c;
}
#remote-status.success {
  color: var(--accent);
}
.remote-actions {
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  gap: 8px;
  align-items: center;
}
.remote-actions .primary-btn,
.remote-actions .ghost-btn,
.remote-saved-row .ghost-btn {
  padding: 7px 12px;
  font-size: 12px;
}

@media (max-width: 600px) {
  .remote-grid {
    grid-template-columns: 1fr;
  }
  .remote-full {
    grid-column: auto;
  }
  .remote-actions {
    grid-template-columns: 1fr 1fr;
  }
  .remote-actions > span {
    display: none;
  }
}
.prefs-panel {
  width: min(760px, 94vw);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 0 20px 22px;
  max-height: calc(100vh - 110px);
  overflow-y: auto;
  overscroll-behavior: contain;
  /* two-up landscape layout: settings flow left column then right */
  display: grid;
  grid-template-columns: 1fr 1fr;
  column-gap: 28px;
  align-content: start;
}
.prefs-head,
.prefs-foot,
#api-detail {
  grid-column: 1 / -1;
}
.prefs-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  background: var(--bg);
  padding: 14px 0 10px;
  margin-bottom: 2px;
  z-index: 1;
}
/* narrow windows: single column */
@media (max-width: 620px) {
  .prefs-panel {
    width: min(380px, 94vw);
    grid-template-columns: 1fr;
  }
}
.prefs-head h2 {
  font-size: 15px;
  margin: 0;
}
.prefs-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
  border-top: 1px solid var(--border);
}
.prefs-row > label {
  font-size: 13px;
  color: var(--text-secondary);
  flex: none;
}
#swatches {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}
.swatch {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  transition: transform 0.08s ease;
}
.swatch:hover {
  transform: scale(1.12);
}
.swatch.active {
  border-color: var(--text);
  box-shadow: 0 0 0 2px var(--bg);
}
input[type='color'] {
  width: 44px;
  height: 26px;
  border: 1px solid var(--control-border);
  border-radius: 6px;
  background: none;
  cursor: pointer;
  padding: 2px;
}
.seg {
  display: flex;
  background: var(--bg-elevated);
  border-radius: 8px;
  padding: 2px;
}
.seg button {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 6px;
  cursor: pointer;
}
.seg button.active {
  background: var(--accent-strong);
  color: #fff;
}
input[type='range'] {
  accent-color: var(--accent);
  width: 140px;
}

#api-detail {
  padding: 4px 2px 10px;
}
.api-field {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 6px 0;
  font-size: 11px;
  color: var(--text-secondary);
}
.api-field > span:first-child {
  width: 42px;
  flex: none;
}
.api-field code {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 3px 7px;
  border-radius: 5px;
  background: var(--code-bg);
  color: var(--text);
  font: 11px var(--font-mono);
}
.api-field .mini-btn {
  border: 1px solid var(--control-border);
  padding: 2px 7px;
  border-radius: 5px;
  flex: none;
}
.api-note {
  margin: 8px 0 0;
  font-size: 10.5px;
  line-height: 1.5;
  color: var(--text-secondary);
}

.prefs-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-secondary);
}
.prefs-foot a {
  color: var(--link);
  text-decoration: none;
}
.prefs-foot a:hover {
  text-decoration: underline;
}

::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}
::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 6px;
  border: 2px solid transparent;
  background-clip: content-box;
}
::-webkit-scrollbar-thumb:hover {
  background: var(--text-secondary);
  background-clip: content-box;
}

/* ---------- Accessibility ---------- */

:focus-visible {
  outline: 2px solid var(--link);
  outline-offset: 2px;
  border-radius: 4px;
}

/* Skip-link / programmatic focus target: the scroll + heading position are the
   visible feedback; no ring needed on the non-interactive container. */
#markdown:focus {
  outline: none;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}

/* ---------- Print / PDF export ---------- */

@media print {
  #titlebar,
  #sidebar,
  #find-bar,
  #remote-banner,
  #prefs,
  #remote-dialog,
  .code-copy {
    display: none !important;
  }
  html,
  body,
  #app,
  #body,
  #content {
    overflow: visible !important;
    height: auto !important;
    display: block !important;
  }
  #content {
    position: static !important;
  }
  #markdown {
    max-width: none;
    padding: 0;
  }
  .markdown-body {
    color: #000;
  }
  .markdown-body pre,
  .markdown-body .code-block,
  .markdown-body blockquote,
  .markdown-body .mermaid-figure,
  .markdown-body table {
    break-inside: avoid;
  }
  .markdown-body a {
    color: #000;
    text-decoration: underline;
  }
}

/* ---------- Doc stats · sidebar tabs · file tree · palette ---------- */

#doc-stats {
  margin-left: 12px;
  font-size: 11px;
  font-weight: 400;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}
#app:not(.has-doc) #doc-stats {
  display: none;
}
.titlebar-drag {
  display: flex;
  align-items: baseline;
}

.sidebar-tabs {
  display: flex;
  gap: 2px;
  padding: 2px 8px 8px;
}
.side-tab {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 4px 8px;
  border-radius: 6px;
  cursor: pointer;
}
.side-tab.active {
  color: var(--text);
  background: var(--accent-tint);
}

#filetree,
#backlinks {
  font-size: 12.5px;
}
#backlinks {
  margin-top: 16px;
  border-top: 1px solid var(--border);
  padding-top: 8px;
}
ul.tree,
ul.tree ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
.tree-dir,
.tree-file {
  padding: 4px 8px;
  border-radius: 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
  color: var(--text-secondary);
}
.tree-dir {
  font-weight: 600;
  color: var(--text);
}
.tree-file:hover,
.tree-dir:hover {
  background: var(--accent-tint);
  color: var(--text);
}
.tree-file.current {
  background: var(--accent-strong);
  color: #fff;
}
.markdown-body a.wikilink {
  color: var(--link);
  border-bottom: 1px dashed var(--link);
  text-decoration: none;
}

/* #tag chips */
.md-tag {
  display: inline-block;
  padding: 0 6px;
  border-radius: 5px;
  font-size: 0.86em;
  font-weight: 600;
  line-height: 1.5;
  color: var(--accent-readable, var(--link));
  background: var(--accent-tint);
  text-decoration: none;
  border-bottom: none;
  white-space: nowrap;
}
.md-tag:hover {
  background: var(--accent-tint-strong);
}
#tagtree .sidebar-head {
  margin-bottom: 8px;
}
.tag-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.tag-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  border: none;
  background: transparent;
  padding: 4px 6px;
  border-radius: 6px;
  cursor: pointer;
  text-align: left;
}
.tag-row:hover {
  background: var(--bg-elevated);
}
.tag-count {
  font-size: 11px;
  color: var(--text-secondary);
}
.tag-empty {
  color: var(--text-secondary);
  font-size: 12px;
  padding: 4px 6px;
}

/* command palette / quick open */
#palette {
  position: fixed;
  inset: 0;
  z-index: 55;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 12vh;
  background: rgba(0, 0, 0, 0.28);
}
.palette-panel {
  width: min(620px, 92vw);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
#palette-input {
  width: 100%;
  border: none;
  border-bottom: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  font-size: 15px;
  padding: 14px 18px;
  outline: none;
}
#palette-list {
  list-style: none;
  margin: 0;
  padding: 6px;
  max-height: 50vh;
  overflow-y: auto;
}
#palette-list li {
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
  font-variant-numeric: tabular-nums;
}
#palette-list li.active {
  background: var(--accent-strong);
  color: #fff;
}

/* find bar with replace row */
#find-bar.with-replace {
  flex-wrap: wrap;
}
#replace-input {
  flex: 1 1 100%;
  order: 5;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 13px;
  outline: none;
  padding-top: 6px;
  border-top: 1px solid var(--border);
  margin-top: 4px;
}

/* line-numbers preference for code blocks */
#app.line-numbers .code-block pre {
  counter-reset: ln;
}
#app.line-numbers .code-block pre code {
  counter-increment: none;
}

/* tab: pinned + context menu */
.tab.pinned {
  min-width: 0;
  max-width: 120px;
  flex: 0 0 auto;
}
.tab.pinned .tab-close {
  opacity: 0.6;
  font-size: 10px;
}
.context-menu {
  position: fixed;
  z-index: 70;
  min-width: 160px;
  padding: 4px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
}
.context-menu button {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 12.5px;
  padding: 6px 10px;
  border-radius: 5px;
  cursor: pointer;
}
.context-menu button:hover:not(:disabled) {
  background: var(--accent-strong);
  color: #fff;
}
.context-menu button:disabled {
  opacity: 0.4;
}

/* diff / compare view */
.diff-banner {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  padding: 8px 14px;
  font-size: 12.5px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-elevated);
}
pre.diff {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 0;
  overflow-x: auto;
  font-size: 12.5px;
  line-height: 1.55;
}
.diff-line {
  padding: 0 14px;
  white-space: pre;
  font-family: var(--font-mono);
}
.diff-line.add {
  background: color-mix(in srgb, #2f8f5b 16%, transparent);
}
.diff-line.del {
  background: color-mix(in srgb, #c0392b 16%, transparent);
}
.diff-line.ctx {
  color: var(--text-secondary);
}

/* aria live announcer */
#announcer {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

/* ---------- Web build ---------- */

/* No native frame or traffic lights in the browser. */
#app[data-platform='web'] #titlebar {
  padding-left: 16px;
  height: 46px;
}
#app[data-platform='web'] #tabstrip {
  padding-left: 10px;
}
#app[data-platform='web'].tabbed #titlebar {
  padding-left: 16px;
}

.web-badge {
  font-size: 0.42em;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  vertical-align: middle;
  padding: 3px 7px;
  margin-left: 8px;
  border-radius: 999px;
  background: var(--accent-soft, color-mix(in srgb, var(--accent) 16%, transparent));
  color: var(--accent);
}

/* discard-changes prompt (web has no native dialog) */
.web-modal {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: grid;
  place-items: center;
  background: color-mix(in srgb, #000 42%, transparent);
}
.web-modal-panel {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px 22px;
  max-width: 380px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}
.web-modal-panel p {
  margin: 0 0 16px;
}
.web-modal-row {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}
.web-modal-row button {
  padding: 7px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-primary);
  font: inherit;
  cursor: pointer;
}
.web-modal-row button.primary-btn {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* skip-to-content link (WCAG 2.4.1) */
.skip-link {
  position: fixed;
  top: 8px;
  left: 8px;
  z-index: 10000;
  padding: 8px 14px;
  background: var(--accent);
  color: #fff;
  border-radius: 8px;
  font: 500 13px var(--font-ui);
  text-decoration: none;
  transform: translateY(-150%);
  transition: transform 0.12s ease;
}
.skip-link:focus {
  transform: translateY(0);
}

.hint-legal {
  font-size: 12px;
  color: var(--text-secondary);
}
.hint-legal a {
  white-space: nowrap;
  color: var(--link);
}
