/* ═══════════════════════════════════════════════════════════════
   MD Meld — Modern UI
   ═══════════════════════════════════════════════════════════════ */

/* ── Reset ─────────────────────────────────────────────────── */
* { margin: 0; padding: 0; box-sizing: border-box; }

/* ── Design tokens ─────────────────────────────────────────── */
:root {
  /* Brand */
  --p:        #4f46e5;   /* primary indigo */
  --p-lt:     #818cf8;   /* lighter variant */
  --p-dk:     #3730a3;   /* darker variant */
  --p-muted:  #eef2ff;   /* pale tint for backgrounds */
  --p-ring:   #c7d2fe;   /* focus ring */

  /* Surface & text */
  --text:     #1e293b;
  --text-2:   #64748b;   /* secondary */
  --text-3:   #94a3b8;   /* placeholder / faint */
  --bg:       #f0f4f8;   /* page canvas */
  --surface:  #ffffff;
  --surface-2:#f8fafc;   /* slightly off-white */
  --border:   #e2e8f0;
  --border-2: #cbd5e1;
  --hover:    #f1f5f9;

  /* Buttons */
  --btn-bg:     #ede9fe;
  --btn-fg:     #4f46e5;
  --btn-border: #c4b5fd;

  /* Semantic */
  --danger:    #ef4444;
  --danger-bg: #fef2f2;
  --success:   #22c55e;

  /* Radii */
  --r-xs:  4px;
  --r-sm:  7px;
  --r:     11px;
  --r-lg:  16px;
  --r-pill:99px;

  /* Shadows */
  --sh-xs: 0 1px 2px rgba(0,0,0,.05), 0 1px 2px rgba(0,0,0,.04);
  --sh-sm: 0 2px 5px rgba(0,0,0,.06), 0 1px 3px rgba(0,0,0,.04);
  --sh:    0 4px 12px rgba(0,0,0,.07), 0 2px 4px rgba(0,0,0,.05);
  --sh-md: 0 8px 24px rgba(0,0,0,.09), 0 3px 8px rgba(0,0,0,.05);
  --sh-lg: 0 16px 40px rgba(0,0,0,.11), 0 6px 14px rgba(0,0,0,.06);

  /* Motion */
  --t:  .14s ease;
  --ts: .24s ease;
}

/* ── Dark-mode token overrides ─────────────────────────────── */
body.dark-mode {
  --p:        #818cf8;
  --p-lt:     #a5b4fc;
  --p-dk:     #6366f1;
  --p-muted:  #1e1b4b;
  --p-ring:   #4338ca;

  --text:     #e2e8f0;
  --text-2:   #94a3b8;
  --text-3:   #475569;
  --bg:       #0b1120;
  --surface:  #131928;
  --surface-2:#192236;
  --border:   #1e2d42;
  --border-2: #253a52;
  --hover:    #1a2640;

  --btn-bg:     #1e1b4b;
  --btn-fg:     #818cf8;
  --btn-border: #312e81;

  --danger:    #f87171;
  --danger-bg: #2a0a0a;
}

/* ═══════════════════════════════════════════════════════════════
   BASE
   ═══════════════════════════════════════════════════════════════ */
html { background: var(--bg); min-height: 100%; }

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont,
               'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
  line-height: var(--ls-body, 1.7);
  font-size: var(--fs-base, 1em);
  color: var(--text);
  background: var(--bg);
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px 60px;
  transition: background var(--ts), color var(--ts);
}

/* ═══════════════════════════════════════════════════════════════
   LOGO
   ═══════════════════════════════════════════════════════════════ */
#logo {
  display: flex;
  align-items: center;
  padding: 16px 4px 0;
}
#logo .logo-text {
  font-size: .82em;
  font-weight: 800;
  letter-spacing: .22em;
  text-transform: uppercase;
  background: linear-gradient(125deg, var(--p) 0%, var(--p-lt) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
@media print { #logo { display: none !important; } }

/* ═══════════════════════════════════════════════════════════════
   CONTROL PANEL
   ═══════════════════════════════════════════════════════════════ */
/* ── Panel collapse button (✕ inside the panel) ──────────────── */
#panel-close-btn {
  position: absolute;
  top: 11px;
  right: 14px;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-3);
  font-size: .88em;
  padding: 4px 7px;
  border-radius: var(--r-xs);
  line-height: 1;
  transition: color var(--t), background var(--t);
  z-index: 10;
}
#panel-close-btn:hover { color: var(--text); background: var(--hover); }

/* ── Mini widget — visible only when panel is collapsed ──────── */
#panel-widget {
  display: none;
  position: fixed;
  top: 14px;
  left: 14px;
  z-index: 500;
  align-items: center;
  gap: 7px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-pill);
  padding: 8px 16px 8px 12px;
  box-shadow: var(--sh-md);
  cursor: pointer;
  transition: box-shadow var(--t), border-color var(--t);
  user-select: none;
}
#panel-widget:hover { box-shadow: var(--sh-lg); border-color: var(--p-ring); }
#panel-widget .pw-icon {
  font-size: .95em;
  color: var(--text-2);
  line-height: 1;
}
#panel-widget .pw-label {
  font-size: .75em;
  font-weight: 800;
  letter-spacing: .2em;
  text-transform: uppercase;
  background: linear-gradient(125deg, var(--p) 0%, var(--p-lt) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* ── Collapsed state ─────────────────────────────────────────── */
body.panel-collapsed #panel-widget        { display: flex; }
body.panel-collapsed #file-input-container { display: none; }
body.panel-collapsed #logo                 { display: none; }

#file-input-container {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 18px 22px 0;
  margin: 10px 0 22px;
  box-shadow: var(--sh-md);
  position: sticky;
  top: 8px;
  z-index: 200;
  /* needed so the absolute-positioned ✕ button is contained */
  isolation: isolate;
  transition: background var(--ts), border-color var(--ts), box-shadow var(--ts);
}

/* ── Document title ─────────────────────────────────────────── */
#file-title {
  font-size: 1.1em;
  font-weight: 700;
  color: var(--text);
  padding: 3px 6px;
  margin-bottom: 14px;
  min-height: 1.5em;
  cursor: text;
  outline: none;
  border-radius: var(--r-xs);
  border-bottom: 2px solid transparent;
  transition: border-color var(--t), background var(--t);
  line-height: 1.4;
}
#file-title:empty::before {
  content: 'Document title…';
  color: var(--text-3);
  font-weight: 400;
  font-style: italic;
  font-size: .95em;
}
#file-title:focus {
  background: var(--p-muted);
  border-bottom-color: var(--p);
}

/* ── Load buttons — style <label> elements as pill buttons ──── */
#file-input-container > br { display: none; }    /* remove the layout line-breaks */

label[for="md-file"],
label[for="md-folder"] {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 14px;
  background: var(--btn-bg);
  color: var(--btn-fg);
  border: 1px solid var(--btn-border);
  border-radius: var(--r-pill);
  font-size: .78em;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  margin-bottom: 12px;
  transition: background var(--t), box-shadow var(--t), transform var(--t);
}
label[for="md-file"] strong,
label[for="md-folder"] strong { font-weight: inherit; }
label[for="md-file"]:hover,
label[for="md-folder"]:hover {
  background: var(--p-muted);
  box-shadow: var(--sh-xs);
  transform: translateY(-1px);
}
label[for="md-file"]:active,
label[for="md-folder"]:active { transform: none; box-shadow: none; }

label[for="md-folder"] { margin-left: 8px !important; }

#md-file { display: none; }  /* hide native file input; label click opens the dialog */

/* ── File queue ─────────────────────────────────────────────── */
#file-queue {
  list-style: none;
  margin: 0 0 10px;
  padding: 0;
  max-height: 170px;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
#file-queue::-webkit-scrollbar { width: 3px; }
#file-queue::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

#file-queue li {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 6px 10px;
  margin-bottom: 3px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  font-size: .83em;
  user-select: none;
  transition: background var(--t), border-color var(--t), box-shadow var(--t);
}
#file-queue li:hover {
  background: var(--hover);
  border-color: var(--border-2);
  box-shadow: var(--sh-xs);
}
#file-queue li.dnd-dragging { opacity: .3; }
#file-queue li.dnd-over {
  border-color: var(--p);
  background: var(--p-muted);
  box-shadow: 0 0 0 2px var(--p-ring);
}

.q-drag-handle {
  cursor: grab;
  color: var(--text-3);
  font-size: 1em;
  padding: 0 3px;
  flex-shrink: 0;
  transition: color var(--t);
}
#file-queue li:hover .q-drag-handle { color: var(--text-2); }

.q-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-xs);
  cursor: pointer;
  font-size: .68em;
  line-height: 1;
  padding: 3px 7px;
  color: var(--text-2);
  flex-shrink: 0;
  transition: background var(--t), color var(--t), border-color var(--t);
}
.q-btn:hover:not(:disabled) {
  background: var(--p-muted);
  color: var(--p);
  border-color: var(--btn-border);
}
.q-btn:disabled { opacity: .28; cursor: default; }

#file-queue li .file-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
  padding: 1px 4px;
  border-radius: var(--r-xs);
  font-weight: 500;
  color: var(--text);
  transition: color var(--t);
}
#file-queue li .file-name:hover { color: var(--p); }

.file-rename-input {
  flex: 1;
  font-size: inherit;
  font-family: inherit;
  padding: 2px 7px;
  border: 1px solid var(--p);
  border-radius: var(--r-xs);
  outline: none;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 0 0 2px var(--p-ring);
}

#file-queue li .file-size {
  color: var(--text-3);
  font-size: .76em;
  padding: 0 4px;
  flex-shrink: 0;
  font-variant-numeric: tabular-nums;
}

#file-queue li .file-remove {
  cursor: pointer;
  color: var(--text-3);
  flex-shrink: 0;
  padding: 2px 6px;
  border-radius: var(--r-xs);
  font-size: .95em;
  line-height: 1;
  transition: color var(--t), background var(--t);
}
#file-queue li .file-remove:hover {
  color: var(--danger);
  background: var(--danger-bg);
}

/* ── Controls row ───────────────────────────────────────────── */
#controls-row {
  border-top: 1px solid var(--border);
  padding-top: 0;
  margin: 0;
  position: relative;
}

/* ── Tab bar — underline style ──────────────────────────────── */
.tab-bar {
  display: flex;
  gap: 0;
  overflow-x: auto;
  scrollbar-width: none;
}
.tab-bar::-webkit-scrollbar { display: none; }

.tab {
  padding: 9px 18px;
  font-size: .72em;
  font-weight: 600;
  letter-spacing: .06em;
  text-transform: uppercase;
  cursor: pointer;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-3);
  user-select: none;
  white-space: nowrap;
  transition: color var(--t), border-color var(--t);
}
.tab:hover { color: var(--text-2); }
.tab.active {
  color: var(--p);
  border-bottom-color: var(--p);
  font-weight: 700;
}

/* ── Tab panels ─────────────────────────────────────────────── */
.tab-panels {
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding: 10px 2px 14px;
  min-height: 42px;
  border-radius: 0 0 var(--r-lg) var(--r-lg);
}
.tab-panel {
  display: none;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  position: relative;
}
.tab-panel.active { display: flex; }

/* ── Toggle buttons ─────────────────────────────────────────── */
.toggle-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 5px 13px;
  border-radius: var(--r-pill);
  font-size: .76em;
  font-weight: 600;
  cursor: pointer;
  background: var(--btn-bg);
  color: var(--btn-fg);
  border: 1px solid var(--btn-border);
  user-select: none;
  line-height: 1.4;
  transition: background var(--t), box-shadow var(--t), transform var(--t);
}
.toggle-btn:hover {
  background: var(--p-muted);
  box-shadow: var(--sh-xs);
  transform: translateY(-1px);
}
.toggle-btn:active { transform: none; box-shadow: none; }
.toggle-btn.active-btn {
  background: var(--p);
  color: #fff;
  border-color: var(--p-dk);
}

/* ═══════════════════════════════════════════════════════════════
   LOADING OVERLAY
   ═══════════════════════════════════════════════════════════════ */
#loading {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .38);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  z-index: 9999;
  align-items: center;
  justify-content: center;
}
#loading-modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 28px 44px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  box-shadow: var(--sh-lg);
  font-size: .88em;
  color: var(--text-2);
  letter-spacing: .02em;
}
.loading-spinner {
  width: 26px;
  height: 26px;
  border: 2.5px solid var(--border-2);
  border-top-color: var(--p);
  border-radius: 50%;
  animation: _spin .7s linear infinite;
}
@keyframes _spin { to { transform: rotate(360deg); } }

/* ═══════════════════════════════════════════════════════════════
   CONTENT CANVAS
   ═══════════════════════════════════════════════════════════════ */
#content {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 44px 56px;
  box-shadow: var(--sh-md);
  min-height: 200px;
  transition: background var(--ts), border-color var(--ts);
}

/* ── Headings ───────────────────────────────────────────────── */
h1 {
  font-size: var(--fs-h1, 2.5em);
  margin: calc(var(--ls-body,1.7) * 1em) 0 calc(var(--ls-body,1.7) * .4em);
  color: var(--text);
  font-weight: 800;
  border-bottom: 3px solid var(--border-2);
  padding-bottom: 14px;
  line-height: 1.2;
  letter-spacing: -.01em;
}
h1:first-child { margin-top: 0; }

h2 {
  font-size: var(--fs-h2, 1.9em);
  margin: calc(var(--ls-body,1.7) * .9em) 0 calc(var(--ls-body,1.7) * .3em);
  color: var(--p);
  font-weight: 700;
  border-left: 4px solid var(--p);
  padding-left: 16px;
  line-height: 1.3;
}

h3 {
  font-size: var(--fs-h3, 1.4em);
  margin: calc(var(--ls-body,1.7) * .7em) 0 calc(var(--ls-body,1.7) * .25em);
  color: var(--p-dk);
  font-weight: 700;
  line-height: 1.35;
}
body.dark-mode h3 { color: var(--p-lt); }

h4 {
  font-size: var(--fs-h4, 1.15em);
  margin: calc(var(--ls-body,1.7) * .5em) 0 calc(var(--ls-body,1.7) * .2em);
  color: var(--text);
  font-weight: 700;
}

h5 {
  font-size: var(--fs-h5, 1em);
  margin: calc(var(--ls-body,1.7) * .4em) 0 calc(var(--ls-body,1.7) * .15em);
  color: var(--text-2);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
}

h6 {
  font-size: .95em;
  margin: calc(var(--ls-body,1.7) * .35em) 0 calc(var(--ls-body,1.7) * .12em);
  color: var(--text-2);
  font-weight: 600;
}

/* ── Body copy ──────────────────────────────────────────────── */
p {
  margin-bottom: calc(var(--ls-body,1.7) * .3em);
  text-align: justify;
  line-height: var(--ls-para, 1.85);
  color: var(--text);
}
strong { color: var(--p-dk); font-weight: 700; }
body.dark-mode strong { color: var(--p-lt); }
em { font-style: italic; color: var(--text-2); }

a { color: var(--p); text-decoration: none; transition: color var(--t); }
a:hover { color: var(--p-dk); text-decoration: underline; }
body.dark-mode a { color: var(--p-lt); }

hr { border: none; border-top: 1.5px solid var(--border); margin: 38px 0; }

ul { margin: calc(var(--ls-body,1.7) * .3em) 0 calc(var(--ls-body,1.7) * .3em) 28px; list-style-type: disc; }
ol { margin: calc(var(--ls-body,1.7) * .3em) 0 calc(var(--ls-body,1.7) * .3em) 28px; }
li { margin-bottom: calc(var(--ls-body,1.7) * .15em); line-height: var(--ls-para, 1.85); }
li strong { color: var(--p-dk); }
body.dark-mode li strong { color: var(--p-lt); }

/* ── Inline code ────────────────────────────────────────────── */
code {
  background: var(--p-muted);
  color: var(--p);
  padding: 2px 7px;
  border-radius: var(--r-xs);
  font-family: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: .87em;
  font-weight: 500;
}
body.dark-mode code {
  background: #1e1b4b;
  color: var(--p-lt);
}

/* ── Code blocks ────────────────────────────────────────────── */
pre {
  background: #0f1629;
  color: #e2e8f0;
  border-radius: var(--r);
  padding: 20px 24px;
  overflow-x: auto;
  margin: calc(var(--ls-body,1.7) * .5em) 0;
  font-size: .82em;
  line-height: 1.7;
  font-family: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
  border: 1px solid #1e293b;
  box-shadow: var(--sh-sm);
  position: relative;
}
body.dark-mode pre { background: #060b16; border-color: #1a2438; }
pre code { background: none; padding: 0; color: inherit; font-size: inherit; font-weight: 400; }

/* ── Copy button (on pre blocks) ───────────────────────────── */
.code-copy-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  padding: 3px 11px;
  font-size: .7em;
  font-weight: 600;
  letter-spacing: .04em;
  background: rgba(255,255,255,.1);
  color: #94a3b8;
  border: 1px solid rgba(255,255,255,.14);
  border-radius: var(--r-pill);
  cursor: pointer;
  opacity: 0;
  font-family: inherit;
  transition: opacity var(--t), background var(--t), color var(--t);
}
pre:hover .code-copy-btn { opacity: 1; }
.code-copy-btn:hover { background: rgba(255,255,255,.2); color: #fff; }
@media print { .code-copy-btn { display: none !important; } }

/* ── Tables ─────────────────────────────────────────────────── */
table {
  width: 100%;
  border-collapse: collapse;
  margin: calc(var(--ls-body,1.7) * .5em) 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
  overflow: hidden;
  box-shadow: var(--sh-xs);
  font-size: .94em;
}
thead { display: table-header-group; }
th {
  background: var(--p);
  color: #fff;
  padding: 12px 16px;
  text-align: left;
  font-weight: 600;
  font-size: .86em;
  letter-spacing: .04em;
  overflow-wrap: break-word;
  word-break: normal;
  hyphens: auto;
}
td {
  padding: 11px 16px;
  border-bottom: 1px solid var(--border);
  overflow-wrap: break-word;
  word-break: normal;
  hyphens: auto;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:nth-child(even) { background: var(--surface-2); }
tbody tr:hover { background: var(--p-muted); transition: background var(--t); }

body.dark-mode table { background: var(--surface); border-color: var(--border); }
body.dark-mode th { background: var(--p-dk); }
body.dark-mode td { border-bottom-color: var(--border); }
body.dark-mode tbody tr:nth-child(even) { background: var(--surface-2); }
body.dark-mode tbody tr:hover { background: var(--hover); }

/* ── Blockquote ─────────────────────────────────────────────── */
blockquote {
  border-left: 4px solid var(--p-lt);
  padding: 14px 20px;
  margin: calc(var(--ls-body,1.7) * .5em) 0;
  background: var(--p-muted);
  border-radius: 0 var(--r-sm) var(--r-sm) 0;
  color: var(--text-2);
  font-style: italic;
}
body.dark-mode blockquote { background: var(--p-muted); color: var(--text-2); }

/* ── Mermaid diagrams ───────────────────────────────────────── */
.mermaid {
  background: var(--surface-2);
  padding: 24px 20px;
  border-radius: var(--r);
  border: 1px solid var(--border);
  margin: calc(var(--ls-body,1.7) * .5em) 0;
  text-align: center;
  box-shadow: var(--sh-xs);
  overflow: hidden;
}
.mermaid svg { max-width: 100%; height: auto; }
.mermaid .node .label { font-size: 14px; white-space: normal; text-align: left; overflow-wrap: break-word; }
.mermaid .node .label b,
.mermaid .node .label strong { font-weight: 700; color: inherit; }
.mermaid .edgeLabel { font-size: 11px; }
.mermaid .edgeLabel .label { font-size: 11px; }
.mermaid .edgeLabel tspan { font-size: 11px; }

/* ── Mindmap central (root) node — bold white centred text ── */
.mermaid svg .mindmap-node--root text,
.mermaid svg .mindmap-node--root tspan {
  font-weight: 700 !important;
  fill: #ffffff !important;
  text-anchor: middle !important;
}
/* foreignObject / HTML-rendered text path */
.mermaid svg .mindmap-node--root foreignObject div,
.mermaid svg .mindmap-node--root foreignObject span {
  font-weight: 700 !important;
  color: #ffffff !important;
  text-align: center !important;
}

body.dark-mode .mermaid { background: var(--surface-2); border-color: var(--border); }

/* ═══════════════════════════════════════════════════════════════
   TOC — fixed floating panel, toggleable from the View tab
   ═══════════════════════════════════════════════════════════════ */
#toc {
  /* Fixed positioning: out of document flow, follows scroll */
  position: fixed;
  right: 24px;
  top: 50%;
  /* Parked off-screen to the right when hidden */
  transform: translateY(-50%) translateX(calc(100% + 40px));
  opacity: 0;
  pointer-events: none;

  width: 256px;
  max-height: 72vh;
  display: flex;
  flex-direction: column;

  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
  box-shadow: var(--sh-lg);
  font-size: .81em;
  overflow: hidden;
  z-index: 300;

  transition: transform var(--ts), opacity var(--ts);
}

/* Visible state — slides in from the right */
#toc.toc-visible {
  transform: translateY(-50%) translateX(0);
  opacity: 1;
  pointer-events: auto;
}

#toc-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: linear-gradient(125deg, var(--p) 0%, var(--p-lt) 100%);
  color: #fff;
  font-weight: 700;
  user-select: none;
  letter-spacing: .03em;
  flex-shrink: 0;
}
#toc-title { flex: 1; font-size: .95em; }
#toc-close {
  cursor: pointer;
  font-size: 1.1em;
  line-height: 1;
  opacity: .75;
  padding: 0 2px;
  transition: opacity var(--t);
}
#toc-close:hover { opacity: 1; }

#toc-body {
  padding: 6px 0;
  overflow-y: auto;
  flex: 1;
  min-height: 0;   /* required for flex + overflow-y to work */
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
#toc-body::-webkit-scrollbar { width: 3px; }
#toc-body::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

#toc-body a {
  display: block;
  padding: 5px 14px;
  color: var(--text-2);
  text-decoration: none;
  border-left: 2px solid transparent;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  transition: background var(--t), border-color var(--t), color var(--t);
}
#toc-body a:hover {
  background: var(--p-muted);
  border-left-color: var(--p);
  color: var(--p);
}

body.dark-mode #toc { background: var(--surface); border-color: var(--border); }
body.dark-mode #toc-body a { color: var(--text-2); }
body.dark-mode #toc-body a:hover { background: var(--hover); color: var(--p-lt); border-left-color: var(--p-lt); }

/* Narrow screens — pin to bottom-right instead of mid-right */
@media (max-width: 700px) {
  #toc {
    top: auto;
    bottom: 20px;
    right: 12px;
    width: calc(100vw - 32px);
    max-width: 320px;
    max-height: 60vh;
    transform: translateX(calc(100% + 24px));
  }
  #toc.toc-visible { transform: translateX(0); }
}

@media print { #toc { display: none !important; } }

/* ═══════════════════════════════════════════════════════════════
   STATS BAR
   ═══════════════════════════════════════════════════════════════ */
#stats-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 18px;
  align-items: center;
  padding: 9px 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
  margin-bottom: 16px;
  font-size: .76em;
  color: var(--text-2);
  box-shadow: var(--sh-xs);
}
.stat-item { white-space: nowrap; display: flex; align-items: center; gap: 4px; }
.stat-detail {
  color: var(--text-3);
  font-size: .9em;
  flex: 1;
  text-align: right;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
body.dark-mode #stats-bar { background: var(--surface); border-color: var(--border); }
@media print { #stats-bar { display: none !important; } }

/* ═══════════════════════════════════════════════════════════════
   SEARCH PANEL
   ═══════════════════════════════════════════════════════════════ */
#search-panel {
  position: absolute;
  top: 10px;
  right: 44px;   /* shifted left to clear the ✕ collapse button at far right */
  z-index: 50;
}
#search-bar {
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-pill);
  padding: 5px 6px 5px 12px;
  width: 34px;
  overflow: hidden;
  cursor: pointer;
  box-shadow: var(--sh-xs);
  transition: width var(--ts), border-color var(--t), box-shadow var(--t);
}
#search-panel.search-open #search-bar {
  width: 270px;
  border-color: var(--p);
  box-shadow: 0 0 0 3px var(--p-ring);
  background: var(--surface);
}
#search-toggle { cursor: pointer; font-size: .9em; flex-shrink: 0; }
#search-input {
  flex: 1;
  border: none;
  outline: none;
  font-size: .8em;
  font-family: inherit;
  padding: 2px 0;
  min-width: 0;
  display: none;
  background: transparent;
  color: var(--text);
}
#search-panel.search-open #search-input { display: block; }
#search-close {
  cursor: pointer;
  font-size: 1.1em;
  color: var(--text-3);
  padding: 0 3px;
  display: none;
  line-height: 1;
  transition: color var(--t);
}
#search-close:hover { color: var(--text); }
#search-panel.search-open #search-close { display: block; }

#search-results {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  width: 360px;
  max-height: 300px;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
  box-shadow: var(--sh-md);
  display: none;
}
#search-panel.search-open #search-results { display: block; }

.search-result-item {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  font-size: .79em;
  transition: background var(--t);
}
.search-result-item:hover { background: var(--hover); }
.search-result-file { font-weight: 700; color: var(--p); cursor: pointer; }
.search-result-file:hover { text-decoration: underline; }
.search-result-snippet {
  display: block;
  color: var(--text-2);
  margin-top: 3px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 1.4;
}
.search-result-snippet mark {
  background: #fde68a;
  color: #1e293b;
  padding: 0 2px;
  border-radius: 2px;
}
body.dark-mode .search-result-snippet mark { background: #78350f; color: #fef3c7; }
.search-result-empty { padding: 20px; text-align: center; color: var(--text-3); font-size: .84em; }

body.dark-mode #search-bar { background: var(--surface-2); border-color: var(--border); }
body.dark-mode #search-input { color: var(--text); }
body.dark-mode #search-results { background: var(--surface); border-color: var(--border); }
body.dark-mode .search-result-item:hover { background: var(--hover); }

/* ═══════════════════════════════════════════════════════════════
   COLLAPSIBLE SECTIONS
   ═══════════════════════════════════════════════════════════════ */
#content h2.collapsed::after,
#content h3.collapsed::after,
#content h4.collapsed::after,
#content h5.collapsed::after,
#content h6.collapsed::after {
  content: ' ▶';
  font-size: .55em;
  color: var(--text-3);
  margin-left: 8px;
  vertical-align: middle;
}

/* ═══════════════════════════════════════════════════════════════
   SECTION EDITOR MODAL
   ═══════════════════════════════════════════════════════════════ */
#se-modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.46);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  z-index: 1000;
  align-items: center;
  justify-content: center;
}
#se-modal-content {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  width: 90%;
  max-width: 680px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--sh-lg);
}
#se-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: var(--r-lg) var(--r-lg) 0 0;
}
#se-modal-title { font-weight: 700; font-size: 1em; color: var(--text); }
#se-modal-close {
  cursor: pointer;
  font-size: 1.4em;
  color: var(--text-3);
  line-height: 1;
  transition: color var(--t);
}
#se-modal-close:hover { color: var(--danger); }
.se-body { padding: 14px 18px; overflow-y: auto; flex: 1; }
.se-tabs { display: flex; gap: 5px; flex-wrap: wrap; margin-bottom: 14px; }
.se-tab {
  padding: 5px 14px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: var(--r-pill);
  cursor: pointer;
  font-size: .78em;
  font-weight: 600;
  color: var(--text-2);
  transition: background var(--t), color var(--t), border-color var(--t);
}
.se-tab.active { background: var(--p); color: #fff; border-color: var(--p); }
.se-section-item {
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  margin-bottom: 6px;
  overflow: hidden;
  transition: border-color var(--t);
}
.se-section-item:hover { border-color: var(--border-2); }
.se-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: var(--surface-2);
  font-size: .82em;
}
.se-section-title { font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.se-section-len { color: var(--text-3); font-size: .82em; margin: 0 8px; }
.se-section-actions { display: flex; gap: 3px; }
.se-section-actions button {
  padding: 3px 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--r-xs);
  cursor: pointer;
  font-size: .76em;
  line-height: 1;
  color: var(--text-2);
  transition: background var(--t), color var(--t), border-color var(--t);
}
.se-section-actions button:hover {
  background: var(--p-muted);
  color: var(--p);
  border-color: var(--btn-border);
}
.se-section-preview {
  padding: 7px 12px;
  font-size: .75em;
  color: var(--text-3);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-style: italic;
}
body.dark-mode #se-modal-content { background: var(--surface); border-color: var(--border); }
body.dark-mode #se-modal-header { background: var(--surface-2); }
body.dark-mode .se-section-item { border-color: var(--border); }
body.dark-mode .se-section-header { background: var(--surface-2); }
body.dark-mode .se-section-actions button { background: var(--surface); border-color: var(--border); }

/* ═══════════════════════════════════════════════════════════════
   SESSION MENU
   ═══════════════════════════════════════════════════════════════ */
#session-menu {
  display: none;
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
  box-shadow: var(--sh-md);
  z-index: 50;
  min-width: 160px;
  overflow: hidden;
}
#session-menu.session-menu-open { display: block; }
#session-menu button {
  display: block;
  width: 100%;
  padding: 9px 16px;
  border: none;
  background: none;
  text-align: left;
  cursor: pointer;
  font-size: .82em;
  font-family: inherit;
  color: var(--text);
  transition: background var(--t), color var(--t);
}
#session-menu button:hover { background: var(--hover); color: var(--p); }
body.dark-mode #session-menu { background: var(--surface); border-color: var(--border); }

/* ═══════════════════════════════════════════════════════════════
   CUSTOM CSS PANEL
   ═══════════════════════════════════════════════════════════════ */
#custom-css-panel { display: none; position: absolute; top: calc(100% + 6px); right: 0; z-index: 50; }
#custom-css-panel.custom-css-open { display: block; }
#custom-css-textarea {
  width: 340px;
  height: 200px;
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
  font-size: .78em;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--r);
  box-shadow: var(--sh-md);
  resize: vertical;
  background: var(--surface);
  color: var(--text);
  outline: none;
  transition: border-color var(--t);
}
#custom-css-textarea:focus { border-color: var(--p); }
body.dark-mode #custom-css-textarea { background: var(--surface); color: var(--text); border-color: var(--border); }

/* ═══════════════════════════════════════════════════════════════
   SPELLCHECK PANEL
   ═══════════════════════════════════════════════════════════════ */
#spellcheck-panel {
  display: none;
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  width: 280px;
  max-height: 280px;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
  box-shadow: var(--sh-md);
  z-index: 50;
}
#spellcheck-panel.spellcheck-open { display: block; }
#spellcheck-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-size: .82em;
  font-weight: 700;
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 1;
}
#spellcheck-close {
  cursor: pointer;
  font-size: 1.2em;
  color: var(--text-3);
  line-height: 1;
  transition: color var(--t);
}
#spellcheck-close:hover { color: var(--danger); }
#spellcheck-results { padding: 4px 0; }
.spellcheck-word { padding: 5px 14px; font-size: .79em; border-bottom: 1px solid var(--border); color: var(--text-2); }
.spellcheck-clean { padding: 20px; text-align: center; color: var(--success); font-size: .84em; }
body.dark-mode #spellcheck-panel { background: var(--surface); border-color: var(--border); }
body.dark-mode #spellcheck-header { background: var(--surface); }

/* ═══════════════════════════════════════════════════════════════
   FIND & REPLACE PANEL
   ═══════════════════════════════════════════════════════════════ */
#find-replace-panel {
  display: none;
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  width: 310px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
  box-shadow: var(--sh-md);
  z-index: 50;
  padding: 14px;
}
#find-replace-panel.fr-open { display: block; }
.fr-row { display: flex; gap: 5px; align-items: center; margin-bottom: 7px; }
#find-replace-panel input {
  flex: 1;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  font-size: .79em;
  font-family: inherit;
  outline: none;
  background: var(--surface-2);
  color: var(--text);
  transition: border-color var(--t), box-shadow var(--t);
}
#find-replace-panel input:focus {
  border-color: var(--p);
  box-shadow: 0 0 0 2px var(--p-ring);
  background: var(--surface);
}
#find-replace-panel button {
  padding: 6px 11px;
  border: 1px solid var(--btn-border);
  background: var(--btn-bg);
  color: var(--btn-fg);
  border-radius: var(--r-sm);
  cursor: pointer;
  font-size: .74em;
  font-family: inherit;
  font-weight: 600;
  white-space: nowrap;
  transition: background var(--t), box-shadow var(--t);
}
#find-replace-panel button:hover { background: var(--p-muted); box-shadow: var(--sh-xs); }
#fr-close {
  position: absolute;
  top: 8px;
  right: 12px;
  cursor: pointer;
  font-size: 1.2em;
  color: var(--text-3);
  line-height: 1;
  transition: color var(--t);
}
#fr-close:hover { color: var(--danger); }
#fr-results { margin-top: 6px; font-size: .79em; color: var(--text-2); }
body.dark-mode #find-replace-panel { background: var(--surface); border-color: var(--border); }
body.dark-mode #find-replace-panel input { background: var(--surface-2); color: var(--text); border-color: var(--border); }

/* ═══════════════════════════════════════════════════════════════
   MULTI-FILE LAYOUT  (.file-page wrapping)
   ═══════════════════════════════════════════════════════════════ */
.file-page {
  page-break-after: always;
  margin-bottom: 3em;
  padding-bottom: 1.5em;
  border-bottom: 1px dashed var(--border-2);
}
.file-page:last-child { page-break-after: avoid; border-bottom: none; }

.page-header {
  display: block;
  text-align: center;
  font-size: 9pt;
  color: var(--text-3);
  padding-bottom: 6pt;
  margin-bottom: 12pt;
  border-bottom: .5pt solid var(--border-2);
  letter-spacing: .07em;
  text-transform: uppercase;
  font-weight: 500;
}
.page-footer {
  display: block;
  text-align: center;
  font-size: 9pt;
  color: var(--text-3);
  padding-top: 12pt;
  margin-top: 18pt;
}
.page-footer .page-num { display: inline-block; padding-top: 6pt; }
.page-footer .page-num::before {
  content: '';
  display: block;
  width: 40px;
  margin: 0 auto 5pt;
  border-top: .5pt solid var(--border-2);
}

body.print-hide-header .page-header,
body.print-hide-footer  .page-footer { display: none !important; }

#content .file-page h1:first-of-type {
  font-size: 3.2em;
  text-align: center;
  font-weight: 900;
  border-bottom-width: 4px;
  margin-top: .6em;
  margin-bottom: 1em;
}

/* ═══════════════════════════════════════════════════════════════
   FONT / SPACING SCALE VARIANTS
   ═══════════════════════════════════════════════════════════════ */
.ls-dense  { --ls-body: 1.3; --ls-para: 1.4; }
.ls-medium { --ls-body: 1.7; --ls-para: 1.85; }
.fs-normal { --fs-base:1em;   --fs-h1:2.5em;  --fs-h2:1.9em;  --fs-h3:1.4em;  --fs-h4:1.15em; --fs-h5:1em; }
.fs-small  { --fs-base:.85em; --fs-h1:2.1em;  --fs-h2:1.6em;  --fs-h3:1.2em;  --fs-h4:1em;    --fs-h5:.9em; }
.mg-normal { --page-margin: 20mm; }
.mg-narrow { --page-margin: 10mm; }

/* ═══════════════════════════════════════════════════════════════
   RESPONSIVE
   ═══════════════════════════════════════════════════════════════ */
@media (max-width: 860px) {
  #content { padding: 28px 24px; }
  h1 { font-size: 2em; }
  h2 { font-size: 1.55em; }
  table { font-size: .92em; }
  th, td { padding: 10px 12px; }
}
@media (max-width: 560px) {
  body { padding: 0 10px 40px; }
  #file-input-container { padding: 14px 14px 0; border-radius: var(--r); }
  #content { padding: 20px 16px; border-radius: var(--r); }
  h1 { font-size: 1.65em; }
  .tab { padding: 8px 12px; }
}

/* ═══════════════════════════════════════════════════════════════
   PRESET SELECTOR  (#preset-select in View panel)
   ═══════════════════════════════════════════════════════════════ */
.preset-label {
  font-size: .74em;
  font-weight: 600;
  color: var(--text-2);
  margin-right: 5px;
  user-select: none;
}
#preset-select {
  padding: 5px 26px 5px 10px;
  border: 1px solid var(--btn-border);
  background-color: var(--btn-bg);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 10 10'%3E%3Cpath fill='%234f46e5' d='M5 7L0 2h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  background-size: 9px;
  color: var(--btn-fg);
  border-radius: var(--r-pill);
  font-size: .76em;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  outline: none;
  transition: border-color var(--t), box-shadow var(--t), background-color var(--t);
}
#preset-select:hover { background-color: var(--p-muted); box-shadow: var(--sh-xs); }
#preset-select:focus { border-color: var(--p); box-shadow: 0 0 0 2px var(--p-ring); }
body.dark-mode #preset-select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 10 10'%3E%3Cpath fill='%23818cf8' d='M5 7L0 2h10z'/%3E%3C/svg%3E");
}

/* Palette select — same visual style as #preset-select */
#palette-select {
  padding: 5px 26px 5px 10px;
  border: 1px solid var(--btn-border);
  background-color: var(--btn-bg);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 10 10'%3E%3Cpath fill='%234f46e5' d='M5 7L0 2h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  background-size: 9px;
  color: var(--btn-fg);
  border-radius: var(--r-pill);
  font-size: .76em;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  outline: none;
  transition: border-color var(--t), box-shadow var(--t), background-color var(--t);
}
#palette-select:hover { background-color: var(--p-muted); box-shadow: var(--sh-xs); }
#palette-select:focus { border-color: var(--p); box-shadow: 0 0 0 2px var(--p-ring); }
body.dark-mode #palette-select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 10 10'%3E%3Cpath fill='%23818cf8' d='M5 7L0 2h10z'/%3E%3C/svg%3E");
}

/* ═══════════════════════════════════════════════════════════════
   COLOR PALETTES  (body.palette-*)
   Each palette overrides the colour design tokens so the entire
   UI (buttons, headings, borders, magazine accents, etc.) shifts
   colour automatically.  Indigo is the default (:root), so it
   needs no class.
   ═══════════════════════════════════════════════════════════════ */

/* ── Teal ────────────────────────────────────────────────── */
body.palette-teal {
  --p:        #0d9488;
  --p-lt:     #2dd4bf;
  --p-dk:     #0f766e;
  --p-muted:  #ccfbf1;
  --p-ring:   #99f6e4;
  --btn-bg:   #ccfbf1;
  --btn-fg:   #0d9488;
  --btn-border:#5eead4;
}
body.dark-mode.palette-teal {
  --p:        #2dd4bf;
  --p-lt:     #5eead4;
  --p-dk:     #0d9488;
  --p-muted:  rgba(13,148,136,.18);
  --p-ring:   rgba(45,212,191,.30);
  --btn-bg:   rgba(13,148,136,.18);
  --btn-fg:   #2dd4bf;
  --btn-border:rgba(45,212,191,.30);
}

/* ── Crimson ─────────────────────────────────────────────── */
body.palette-crimson {
  --p:        #e11d48;
  --p-lt:     #fb7185;
  --p-dk:     #be123c;
  --p-muted:  #fff1f2;
  --p-ring:   #fecdd3;
  --btn-bg:   #fff1f2;
  --btn-fg:   #e11d48;
  --btn-border:#fda4af;
}
body.dark-mode.palette-crimson {
  --p:        #fb7185;
  --p-lt:     #fda4af;
  --p-dk:     #e11d48;
  --p-muted:  rgba(225,29,72,.18);
  --p-ring:   rgba(251,113,133,.30);
  --btn-bg:   rgba(225,29,72,.18);
  --btn-fg:   #fb7185;
  --btn-border:rgba(251,113,133,.30);
}

/* ── Emerald ─────────────────────────────────────────────── */
body.palette-emerald {
  --p:        #059669;
  --p-lt:     #34d399;
  --p-dk:     #047857;
  --p-muted:  #d1fae5;
  --p-ring:   #6ee7b7;
  --btn-bg:   #d1fae5;
  --btn-fg:   #059669;
  --btn-border:#6ee7b7;
}
body.dark-mode.palette-emerald {
  --p:        #34d399;
  --p-lt:     #6ee7b7;
  --p-dk:     #059669;
  --p-muted:  rgba(5,150,105,.18);
  --p-ring:   rgba(52,211,153,.30);
  --btn-bg:   rgba(5,150,105,.18);
  --btn-fg:   #34d399;
  --btn-border:rgba(52,211,153,.30);
}

/* ── Greyscale ───────────────────────────────────────────── */
body.palette-greyscale {
  --p:        #374151;
  --p-lt:     #6b7280;
  --p-dk:     #1f2937;
  --p-muted:  #f3f4f6;
  --p-ring:   #d1d5db;
  --btn-bg:   #f3f4f6;
  --btn-fg:   #374151;
  --btn-border:#d1d5db;
}
body.dark-mode.palette-greyscale {
  --p:        #9ca3af;
  --p-lt:     #d1d5db;
  --p-dk:     #6b7280;
  --p-muted:  rgba(55,65,81,.22);
  --p-ring:   rgba(156,163,175,.30);
  --btn-bg:   rgba(55,65,81,.22);
  --btn-fg:   #d1d5db;
  --btn-border:rgba(156,163,175,.30);
}

/* ═══════════════════════════════════════════════════════════════
   PRESET: MAGAZINE  (body.preset-magazine)
   All magazine rules are scoped here so switching to Normal is
   a single classList.remove — no DOM changes needed for CSS.
   DOM changes (wrappers, kickers, classes) are handled by magazine.js.
   ═══════════════════════════════════════════════════════════════ */

/* ── Base typography ─────────────────────────────────────── */
body.preset-magazine #content {
  line-height: 1.9;
}
body.preset-magazine #content > p,
body.preset-magazine #content .file-page > p {
  text-align: justify;
  -webkit-hyphens: auto;
  hyphens: auto;
}
body.preset-magazine #content h1 {
  font-size: 3em;
  letter-spacing: -.025em;
  text-align: center;
  border-bottom: 4px double var(--border-2);
  padding-bottom: 20px;
  margin-bottom: .7em;
}
body.preset-magazine #content h2 {
  font-size: 1.8em;
  border-left: none;
  padding-left: 0;
  border-top: 3px solid var(--p);
  padding-top: 14px;
  margin-top: 2.8em;
  clear: both;          /* always start H2 below any floated figure */
}
body.preset-magazine #content h3 {
  font-style: italic;
  color: var(--p-dk);
  clear: both;
}

/* ── Section kicker (number above H2 heading text) ───────── */
body.preset-magazine .mag-kicker {
  display: block;
  font-size: .62em;
  font-style: normal;
  font-weight: 700;
  letter-spacing: .18em;
  text-transform: uppercase;
  color: var(--p);
  margin-bottom: 5px;
  font-family: inherit;
}

/* ── Figure wrappers ─────────────────────────────────────── */
/* Hidden by default — only visible in magazine mode */
.mag-fig-wrapper { display: contents; }   /* passthrough when not in magazine */

body.preset-magazine .mag-fig-wrapper {
  display: block;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r);
  overflow: hidden;
  box-shadow: var(--sh-sm);
}

/* Small figure — 28% float */
body.preset-magazine .mag-fig-wrapper.mag-sm.mag-right {
  float: right;
  width: 28%;
  margin: 4px 0 18px 24px;
  clear: right;
}
body.preset-magazine .mag-fig-wrapper.mag-sm.mag-left {
  float: left;
  width: 28%;
  margin: 4px 24px 18px 0;
  clear: left;
}

/* Medium figure — 44% float */
body.preset-magazine .mag-fig-wrapper.mag-md.mag-right {
  float: right;
  width: 44%;
  margin: 4px 0 22px 28px;
  clear: right;
}
body.preset-magazine .mag-fig-wrapper.mag-md.mag-left {
  float: left;
  width: 44%;
  margin: 4px 28px 22px 0;
  clear: left;
}

/* Full-width figure */
body.preset-magazine .mag-fig-wrapper.mag-full {
  float: none;
  width: 100%;
  margin: 28px 0;
  clear: both;
}

/* Mermaid inside wrapper: use all available width, no extra margin */
body.preset-magazine .mag-fig-wrapper .mermaid {
  margin: 0;
  width: 100%;
}
body.preset-magazine .mag-fig-wrapper .mermaid svg {
  max-width: 100%;
  height: auto;
}

/* ── Captions ─────────────────────────────────────────────── */
.mag-caption { display: none; }

body.preset-magazine .mag-caption {
  display: block;
  font-size: .74em;
  font-style: italic;
  color: var(--text-3);
  text-align: center;
  padding: 6px 10px 8px;
  border-top: 1px solid var(--border);
  background: var(--surface-2);
  line-height: 1.35;
}

/* ── Pull quotes ─────────────────────────────────────────── */
body.preset-magazine #content blockquote.mag-pullquote {
  background: none;
  border-left: none;
  border-top: 3px solid var(--p);
  border-bottom: 3px solid var(--p);
  padding: 22px 28px;
  margin: 36px auto;
  max-width: 72%;
  font-size: 1.35em;
  font-style: italic;
  font-weight: 700;
  color: var(--p-dk);
  text-align: center;
  line-height: 1.5;
  clear: both;
  border-radius: 0;
}
body.preset-magazine #content blockquote.mag-pullquote > p {
  margin: 0;
  text-align: center;
  color: inherit;
}

/* ── Grid pairs (Strategy A — diagram + text side by side) ── */
body.preset-magazine .mag-grid-pair {
  display: grid;
  /* grid-template-columns is set per-pair by JS (smart ratio); 1fr 1fr is the fallback */
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 28px;
  align-items: center;  /* vertically centre columns against each other */
  margin: 20px 0 32px;
  clear: both;
}

/* When diagram is on the right, swap visual column order */
body.preset-magazine .mag-grid-pair.mag-grid-fig-right .mag-grid-fig { order: 2; }
body.preset-magazine .mag-grid-pair.mag-grid-fig-right .mag-grid-text { order: 1; }

/* Figure column — no extra framing; diagram/table keeps its own standalone appearance */
body.preset-magazine .mag-grid-fig {
  min-width: 0;
}
body.preset-magazine .mag-grid-fig .mermaid {
  margin: 0;
  width: 100%;
}
body.preset-magazine .mag-grid-fig .mermaid svg {
  max-width: 100%;
  height: auto;
  display: block;
}

/* Text column — vertically centered, editorial feel */
body.preset-magazine .mag-grid-text {
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 8px 0;
}
body.preset-magazine .mag-grid-text > p {
  margin: 0;
  /* JS sets --text-scale and --text-lh on the parent .mag-grid-pair based on column width */
  font-size: calc(1em * var(--text-scale, 1.35));
  line-height: var(--text-lh, 2.1);
  text-align: justify;
  -webkit-hyphens: auto;
  hyphens: auto;
  color: var(--text);
  letter-spacing: 0.01em;
}

/* Decorative left rule on text column when diagram is left */
body.preset-magazine .mag-grid-pair.mag-grid-fig-left .mag-grid-text {
  border-left: 3px solid var(--p-muted);
  padding-left: 20px;
}
/* Right rule when diagram is right */
body.preset-magazine .mag-grid-pair.mag-grid-fig-right .mag-grid-text {
  border-right: 3px solid var(--p-muted);
  padding-right: 20px;
}

/* Table inside grid pair — always fully visible, no scrolling */
/* Table inside grid pair — same look as wide-view tables; only layout constraints added */
body.preset-magazine .mag-grid-table {
  overflow: visible;
}
body.preset-magazine .mag-grid-table table {
  width: 100%;
  min-width: 0;
  table-layout: fixed;  /* share available width evenly so content never overflows */
  word-break: break-word;
  /* font-size, border-radius, box-shadow etc. all inherit from the global table rule */
}
body.preset-magazine .mag-grid-table th,
body.preset-magazine .mag-grid-table td {
  white-space: normal;
  word-wrap: break-word;
  /* padding inherits from global th / td rules */
}

/* Dark mode grid pair */
body.dark-mode.preset-magazine .mag-grid-pair.mag-grid-fig-left .mag-grid-text {
  border-left-color: var(--border-2);
}
body.dark-mode.preset-magazine .mag-grid-pair.mag-grid-fig-right .mag-grid-text {
  border-right-color: var(--border-2);
}

/* ── Card lists ─────────────────────────────────────────── */
body.preset-magazine ul.mag-cards {
  list-style: none;
  margin-left: 0;
  padding-left: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-top: 1em;
  margin-bottom: 1.2em;
  clear: both;
}
body.preset-magazine ul.mag-cards li {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-top: 3px solid var(--p);
  border-radius: var(--r-sm);
  padding: 12px 14px;
  font-size: .9em;
  line-height: 1.55;
  box-shadow: var(--sh-xs);
}
/* If list item starts with bold text, treat it as a card title */
body.preset-magazine ul.mag-cards li > strong:first-child,
body.preset-magazine ul.mag-cards li > b:first-child {
  display: block;
  font-size: .82em;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--p);
  margin-bottom: 4px;
}

/* ── Dark mode adjustments ───────────────────────────────── */
body.dark-mode.preset-magazine #content h1 { border-bottom-color: var(--border-2); }
body.dark-mode.preset-magazine #content h2 { border-top-color: var(--p-lt); }
body.dark-mode.preset-magazine #content h3 { color: var(--p-lt); }
body.dark-mode.preset-magazine .mag-kicker { color: var(--p-lt); }
body.dark-mode.preset-magazine #content blockquote.mag-pullquote {
  color: var(--p-lt);
  border-color: var(--p-lt);
}
body.dark-mode.preset-magazine ul.mag-cards li { border-top-color: var(--p-lt); }

/* ═══════════════════════════════════════════════════════════════
   PRINT
   Three problems solved here:
   1. print-color-adjust: exact  →  backgrounds actually render in PDF
   2. CSS variable reset         →  dark-mode variables don't bleed through
   3. Explicit element colors    →  consistent with screen appearance
   ═══════════════════════════════════════════════════════════════ */
@media print {
  /* ── 1. Force every background / color to print ───────────── */
  * {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
    color-adjust: exact !important;
  }

  /* ── 2. Reset CSS variables to light values ────────────────── */
  /*    (fixes dark-mode users: their --p, --surface etc. would  */
  /*     otherwise produce dark output on a white page)          */
  body, body.dark-mode {
    --p:        #4f46e5;
    --p-lt:     #818cf8;
    --p-dk:     #3730a3;
    --p-muted:  #eef2ff;
    --text:     #1e293b;
    --text-2:   #64748b;
    --text-3:   #94a3b8;
    --surface:  #ffffff;
    --surface-2:#f8fafc;
    --border:   #e2e8f0;
    --border-2: #cbd5e1;
    background: white !important;
    max-width: none !important;
    padding: 0 !important;
    color: #1e293b !important;
  }

  /* ── Hide UI chrome ────────────────────────────────────────── */
  #file-input-container,
  #logo,
  #toc,
  #stats-bar,
  .toggle-btn,
  #preset-select,
  #palette-select,
  .preset-label,
  #loading { display: none !important; }

  /* ── Strip the browser card look from #content ─────────────── */
  #content {
    box-shadow: none !important;
    border: none !important;
    border-radius: 0 !important;
    padding: 0 !important;
    background: white !important;
  }

  /* ── 3. Explicit element colours (match screen exactly) ─────── */
  h1 { color: #1e293b !important; border-bottom-color: #cbd5e1 !important; }
  h2 { color: #3730a3 !important; border-left-color: #4f46e5 !important; }
  h3 { color: #3730a3 !important; }
  h4 { color: #1e293b !important; }
  h5, h6 { color: #64748b !important; }
  p, li { color: #1e293b !important; }
  strong { color: #3730a3 !important; }
  em { color: #64748b !important; }
  a { color: #4f46e5 !important; }

  /* Code blocks — keep the dark background in the PDF */
  pre {
    white-space: pre-wrap;
    background: #0f1629 !important;
    color: #e2e8f0 !important;
    border-color: #1e293b !important;
  }
  /* Inline code — keep the indigo tint */
  code { background: #eef2ff !important; color: #4f46e5 !important; }
  pre code { background: none !important; color: inherit !important; }

  /* Tables — keep the coloured header */
  table { border-color: #e2e8f0 !important; background: white !important; }
  th { background: #4f46e5 !important; color: white !important; }
  tbody tr:nth-child(even) { background: #f8fafc !important; }
  td { border-bottom-color: #e2e8f0 !important; color: #1e293b !important; }

  /* Blockquote — keep the indigo tint */
  blockquote {
    background: #eef2ff !important;
    border-left-color: #818cf8 !important;
    color: #64748b !important;
  }

  /* File-page structure */
  .file-page { page-break-after: always; border-bottom: none; margin: 0; padding: 0; }
  .file-page:last-child { page-break-after: avoid; }

  /* ── Magazine preset — print overrides ──────────────────── */
  body.preset-magazine #content h1 { color: #1e293b !important; border-bottom-color: #cbd5e1 !important; }
  body.preset-magazine #content h2 {
    color: #3730a3 !important;
    border-left: none !important;
    border-top: 3px solid #4f46e5 !important;
    padding-left: 0 !important;
    clear: both !important;
  }
  body.preset-magazine #content blockquote.mag-pullquote {
    background: none !important;
    border-left: none !important;
    border-top: 3px solid #4f46e5 !important;
    border-bottom: 3px solid #4f46e5 !important;
    color: #3730a3 !important;
  }
  body.preset-magazine .mag-kicker { color: #4f46e5 !important; }

  /* Float-based figure layout carries into PDF — explicit sizing for safety */
  body.preset-magazine .mag-fig-wrapper {
    page-break-inside: avoid;
    break-inside: avoid;
  }
  body.preset-magazine .mag-fig-wrapper.mag-sm.mag-right { float: right; width: 28%; margin: 4px 0 18px 24px; }
  body.preset-magazine .mag-fig-wrapper.mag-sm.mag-left  { float: left;  width: 28%; margin: 4px 24px 18px 0; }
  body.preset-magazine .mag-fig-wrapper.mag-md.mag-right { float: right; width: 44%; margin: 4px 0 22px 28px; }
  body.preset-magazine .mag-fig-wrapper.mag-md.mag-left  { float: left;  width: 44%; margin: 4px 28px 22px 0; }
  body.preset-magazine .mag-fig-wrapper.mag-full         { float: none;  width: 100%; margin: 28px 0; clear: both; }

  /* Cards print as grid */
  body.preset-magazine ul.mag-cards {
    display: grid !important;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)) !important;
    list-style: none !important;
    padding-left: 0 !important;
  }
  body.preset-magazine ul.mag-cards li {
    border-top: 3px solid #4f46e5 !important;
    background: #f8fafc !important;
    color: #1e293b !important;
  }

  /* Caption visible in print */
  body.preset-magazine .mag-caption {
    display: block !important;
    color: #94a3b8 !important;
    font-style: italic !important;
  }

  /* Grid pairs — preserve two-column in PDF (ratio carried via inline style) */
  body.preset-magazine .mag-grid-pair {
    display: grid !important;
    /* grid-template-columns intentionally NOT overridden — inline style set by JS carries through */
    gap: 24px !important;
    page-break-inside: avoid;
    break-inside: avoid;
  }
  body.preset-magazine .mag-grid-fig-right .mag-grid-fig { order: 2 !important; }
  body.preset-magazine .mag-grid-fig-right .mag-grid-text { order: 1 !important; }
  body.preset-magazine .mag-grid-pair.mag-grid-fig-left .mag-grid-text {
    border-left: 3px solid #e2e8f0 !important;
    padding-left: 18px !important;
  }
  body.preset-magazine .mag-grid-pair.mag-grid-fig-right .mag-grid-text {
    border-right: 3px solid #e2e8f0 !important;
    padding-right: 18px !important;
  }
  body.preset-magazine .mag-grid-text > p { color: #1e293b !important; }
  body.preset-magazine .mag-grid-table table {
    table-layout: fixed !important;
    width: 100% !important;
    word-break: break-word !important;
  }
  body.preset-magazine .mag-grid-table th,
  body.preset-magazine .mag-grid-table td {
    white-space: normal !important;
    word-wrap: break-word !important;
  }
}
