/* ============================================================
   MD Editor — styles
   ============================================================ */

/* ---------- Theme tokens ---------- */
:root {
  --bg: #ffffff;
  --bg-panel: #f6f8fa;
  --border: #d0d7de;
  --text: #1f2328;
  --muted: #656d76;
  --accent: #0969da;
  --on-accent: #ffffff; /* text on an --accent background (WCAG AA) */
  --code-bg: #f6f8fa;
  --toolbar-bg: #ffffff;
  --err: #f85149; /* error red — readable on both themes */
}
[data-theme="dark"] {
  --bg: #0d1117;
  --bg-panel: #161b22;
  --border: #30363d;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #58a6ff;
  --on-accent: #0d1117; /* white on this light blue only reaches 2.5:1 — use dark text */
  --code-bg: #161b22;
  --toolbar-bg: #010409;
}

/* ---------- Base layout ---------- */
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}

/* ---------- Header / toolbar ---------- */
header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--toolbar-bg);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
header .brand {
  font-weight: 700;
  letter-spacing: -0.5px;
  margin-inline-end: 8px;
  font-size: 15px;
}
header .spacer { flex: 1; }
#viewBtn { display: none; } /* only useful when panes stack (mobile) */

/* The header's DOM order is the narrow-screen one (primary actions first,
   secondary folded into #moreMenu). `order` restores the wide-screen
   arrangement, and `display: contents` dissolves the ⋯ wrapper so its buttons
   sit directly in the header row — same layout as before this menu existed. */
header .brand   { order: 0; }
#exportMenu     { order: 3; }
#copyMenu       { order: 4; }
header .spacer  { order: 5; }
#readBtn        { order: 7; }
#viewBtn        { order: 8; }
#themeBtn       { order: 10; }
#ghLink         { order: 11; }
#moreMenu, #moreMenu > .menu-list { display: contents; }
#moreBtn { display: none; }
#clearBtn { order: 1; }
#openBtn  { order: 2; }
#syncBtn  { order: 6; }
#dirBtn   { order: 9; }
/* Most icons are a narrow-screen affordance, so they stay hidden here; the
   four that were always part of the wide-screen label keep showing. */
header .ic { display: none; margin-inline-end: 5px; }
header .brand .ic,
#viewBtn .ic, #dirBtn .ic, #themeBtn .ic { display: inline-flex; }
header .ic {
  align-items: center;
  vertical-align: -0.18em;   /* sit on the text baseline, not above it */
}
header .ic svg {
  width: 1.05em;
  height: 1.05em;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
header .brand .ic svg { width: 1.15em; height: 1.15em; }

/* Buttons */
button {
  font: inherit;
  font-size: 13px;
  padding: 5px 10px;
  background: var(--bg-panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover { border-color: var(--accent); color: var(--accent); }
button.active { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
button.active:hover { color: var(--on-accent); filter: brightness(1.08); }

/* Copy dropdown menu */
.menu { position: relative; display: inline-block; }
.menu-list {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 28px rgba(0,0,0,.28);
  display: none;
  flex-direction: column;
  min-width: 150px;
  overflow: hidden;
  z-index: 50;
}
.menu.open .menu-list { display: flex; }
.menu-list button {
  border: none;
  border-radius: 0;
  background: none;
  text-align: left;
  padding: 8px 14px;
}
.menu-list button:hover { background: var(--bg-panel); color: var(--accent); }

/* GitHub link */
#ghLink {
  display: inline-flex;
  align-items: center;
  color: var(--muted);
  padding: 4px;
  border-radius: 6px;
}
#ghLink:hover { color: var(--text); background: var(--bg-panel); }

/* ---------- Editor / preview panes ---------- */
.main {
  flex: 1;
  display: flex;
  min-height: 0;
}
.pane { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.edit-pane { flex: 0 0 50%; }
.preview-pane { flex: 1 1 0; position: relative; }

/* Draggable divider */
.divider {
  flex: 0 0 6px;
  cursor: col-resize;
  background: var(--border);
  position: relative;
}
.divider::before {
  content: "";
  position: absolute;
  inset: 0 -4px;
}
.divider:hover, .divider.dragging { background: var(--accent); }

textarea {
  flex: 1;
  width: 100%;
  resize: none;
  border: none;
  outline: none;
  padding: 20px;
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  font-size: 14px;
  line-height: 1.6;
  background: var(--bg);
  color: var(--text);
  tab-size: 2;
}

/* ---------- Rendered preview ---------- */
.preview {
  flex: 1;
  overflow-y: auto;
  padding: 20px 28px;
  line-height: 1.6;
}
.preview:focus { outline: none; }
.preview h1, .preview h2 { border-bottom: 1px solid var(--border); padding-bottom: .3em; }
.preview h1 { font-size: 1.9em; }
.preview h2 { font-size: 1.5em; }
.preview code {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: .15em .4em;
  font-family: ui-monospace, Consolas, monospace;
  font-size: 85%;
}
.preview pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 14px;
  overflow-x: auto;
}
/* highlight.js colours the tokens, but the block keeps OUR themed background:
   its `.hljs` background rule loses to this more specific selector on purpose. */
.preview pre code { border: none; padding: 0; background: none; }
.preview blockquote {
  margin: 0;
  padding: 0 1em;
  color: var(--muted);
  border-inline-start: 3px solid var(--border);
}
.preview table { border-collapse: collapse; }
.preview th, .preview td { border: 1px solid var(--border); padding: 6px 12px; }
.preview img { max-width: 100%; }
/* contenteditable forces an I-beam everywhere — links click, so they point */
.preview a { color: var(--accent); cursor: pointer; }
.preview hr { border: none; border-top: 1px solid var(--border); }
.preview ul, .preview ol { padding-inline-start: 1.6em; }
.preview ul.task-list { list-style: none; padding-inline-start: 0.3em; }
.preview li.task-item { list-style: none; }
.preview li.task-item input[type="checkbox"] {
  margin-inline-end: 6px;
  vertical-align: middle;
  cursor: pointer;
}
/* Mermaid diagrams (rendered by app.js §3b) */
.preview .mermaid-block {
  margin: 1em 0;
  text-align: center;
  overflow-x: auto;   /* a wide diagram scrolls inside its block, never the pane */
  break-inside: avoid; /* PDF export (pagebreak mode "css"): don't slice a diagram */
}
.preview .mermaid-block svg { max-width: 100%; height: auto; }
.preview .mermaid-error {
  color: var(--err);
  font-size: 13px;
  text-align: start;
  margin-bottom: 6px;
}
/* YAML front matter — a dim metadata panel, not part of the prose */
.preview .front-matter {
  color: var(--muted);
  background: var(--code-bg);
  border-inline-start: 3px solid var(--border);
  font-size: 85%;
  margin-bottom: 1.2em;
}

/* Footnotes (collected at the end of the document by app.js §1) */
.preview .fn-sep { margin-top: 2.5em; }
.preview .footnotes { font-size: .9em; color: var(--muted); }
.preview .footnotes li { margin: .3em 0; }
.preview sup.fn-ref a,
.preview .fn-back { text-decoration: none; }
.preview .fn-back { margin-inline-start: 4px; }

/* LaTeX math (rendered by app.js §3c) */
.preview .math-block {
  margin: 1em 0;
  text-align: center;
  overflow-x: auto;    /* a wide equation scrolls inside its block */
  break-inside: avoid;
}
.preview :first-child { margin-top: 0; }

/* ---------- Reading mode ---------- */
body.reading header,
body.reading footer,
body.reading .edit-pane,
body.reading .divider { display: none; }
body.reading .preview {
  /* centre a comfortable text column, but keep the scrollbar at the pane edge */
  padding-inline: max(28px, calc((100% - 840px) / 2));
  padding-block: 40px 72px;
}
body.reading .preview input[type="checkbox"] { pointer-events: none; }
.read-exit {
  display: none;
  position: fixed;
  top: 16px;
  inset-inline-end: 36px; /* clear of the preview's scrollbar (~15px) */
  z-index: 200;
  width: 34px;
  height: 34px;
  padding: 0;
  border-radius: 50%;
  box-shadow: 0 4px 16px rgba(0,0,0,.25);
}
body.reading .read-exit { display: block; }

/* ---------- Footer ---------- */
footer {
  padding: 4px 12px;
  font-size: 12px;
  color: var(--muted);
  border-top: 1px solid var(--border);
  background: var(--toolbar-bg);
  display: flex;
  align-items: center;
  gap: 16px;
}
footer .spacer { flex: 1; }
/* underlined, not colour-only: the link sits inside muted text, so colour alone
   doesn't distinguish it for low-vision users */
footer a { color: var(--accent); text-decoration: underline; }

/* ---------- Floating formatting toolbar (editable preview) ---------- */
.fmtbar {
  position: fixed;
  z-index: 150;
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 4px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 6px 24px rgba(0,0,0,.3);
}
.fmtbar[hidden] { display: none; }
.fmtbar button {
  min-width: 30px;
  padding: 5px 7px;
  font-size: 13px;
  line-height: 1;
  border: none;
  background: none;
  border-radius: 5px;
}
.fmtbar button:hover { background: var(--bg); color: var(--accent); }
/* Heading submenu: compact, each item previews its own level's weight/size */
#hMenu .menu-list { min-width: 64px; }
#hMenu .menu-list [data-cmd="h1"] { font-size: 16px; font-weight: 700; }
#hMenu .menu-list [data-cmd="h2"] { font-size: 15px; font-weight: 700; }
#hMenu .menu-list [data-cmd="h3"] { font-size: 14px; font-weight: 600; }
#hMenu .menu-list [data-cmd="h4"] { font-size: 13px; font-weight: 600; }
#hMenu .menu-list [data-cmd="h5"] { font-size: 12px; font-weight: 500; }
#hMenu .menu-list [data-cmd="h6"] { font-size: 11px; font-weight: 500; }
.fmtbar .sep { width: 1px; align-self: stretch; background: var(--border); margin: 3px; }

/* ---------- Find & replace bar ---------- */
.findbar {
  position: fixed;
  top: 52px;
  inset-inline-end: 16px;
  z-index: 120;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 6px 24px rgba(0,0,0,.28);
  flex-wrap: wrap;
  max-width: calc(100vw - 32px);
}
.findbar[hidden] { display: none; }
.findbar input {
  font: inherit;
  font-size: 13px;
  padding: 4px 8px;
  width: 140px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 5px;
  outline: none;
}
.findbar input:focus { border-color: var(--accent); }
.findbar button { padding: 4px 8px; }
.find-count { font-size: 11px; color: var(--muted); min-width: 44px; text-align: center; }

/* ---------- Drag & drop import overlay ---------- */
.dropzone {
  position: fixed;
  inset: 0;
  z-index: 250;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,.45);
  pointer-events: none;
}
.dropzone.show { display: flex; }
.dropzone .box {
  padding: 26px 42px;
  border: 2px dashed var(--accent);
  border-radius: 14px;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  box-shadow: 0 12px 48px rgba(0,0,0,.4);
}

/* ---------- Link status bar (hover URL, like the browser's own) ---------- */
.link-status {
  position: fixed;
  bottom: 0;
  inset-inline-start: 0;
  z-index: 90;
  max-width: 60vw;
  padding: 3px 10px;
  font-size: 12px;
  color: var(--muted);
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-bottom: none;
  border-inline-start: none;
  border-start-end-radius: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  direction: ltr;        /* URLs read left-to-right even in an RTL document */
  pointer-events: none;  /* never block a click underneath */
}
.link-status[hidden] { display: none; }

/* ---------- Toast notifications ---------- */
#toasts {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
  pointer-events: none;
}
.toast {
  background: var(--bg-panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 13px;
  box-shadow: 0 6px 24px rgba(0,0,0,.25);
  animation: toast-in .2s ease;
}
.toast.err { border-left-color: var(--err); }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
.toast.out { opacity: 0; transform: translateY(8px); transition: .25s ease; }

/* ---------- Modal dialog ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  animation: toast-in .15s ease;
}
.modal {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 22px 24px;
  max-width: 340px;
  box-shadow: 0 12px 48px rgba(0,0,0,.4);
}
.modal p { margin: 0 0 14px; font-size: 15px; }
.modal-input {
  width: 100%;
  padding: 8px 10px;
  margin: 0 0 16px;
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  outline: none;
}
.modal-input:focus { border-color: var(--accent); }
.table-fields { display: flex; gap: 16px; margin: 0 0 16px; }
.table-fields label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 12px;
  color: var(--muted);
}
.table-fields .modal-input { width: 90px; margin: 0; }
.modal .actions { display: flex; gap: 10px; justify-content: flex-end; }
.modal .primary { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
.modal .primary:hover { filter: brightness(1.08); color: var(--on-accent); }

/* ---------- Print / Export→PDF (the browser's print engine) ----------
   Zero @page margin leaves the browser nowhere to draw its own header/footer
   (URL, title, date), so none appears; the .preview padding recreates the page
   margins instead. Trade-off: on multi-page documents the inner page breaks
   run closer to the paper edge than a real margin would. */
@page { margin: 0; }
@media print {
  header, footer, #toasts, #fmtbar, #findbar, #linkStatus, #readExit,
  .edit-pane, .divider { display: none !important; }
  body, .main, .preview-pane, .preview {
    display: block !important;
    height: auto !important;
    overflow: visible !important;
  }
  .preview {
    /* every rule inside .preview reads the theme tokens and custom properties
       inherit — re-pointing them here turns the whole subtree light */
    --bg: #ffffff;
    --text: #000000;
    --muted: #555555;
    --border: #cccccc;
    --code-bg: #f5f5f5;
    --accent: #0645ad;
    color: var(--text);
    background: var(--bg);
    padding: 15mm 18mm;
  }
}

/* ---------- Mobile: stacked panes ---------- */
@media (max-width: 720px) {
  /* One row of glyphs. The header used to wrap onto three rows and eat ~100px
     of a screen that has little to spare, so captions are dropped and the
     secondary actions move into the ⋯ sheet. */
  header { gap: 4px; padding: 6px 8px; flex-wrap: nowrap; }
  header .spacer { display: none; }
  header .brand { margin-inline-end: 2px; }
  header .brand .lbl { display: none; }   /* just the pencil, as a mark */
  header button { padding: 6px 9px; font-size: 13px; }
  header .lbl { display: none; }
  header .ic { display: inline-flex; margin: 0; vertical-align: middle; }
  header .ic svg { width: 17px; height: 17px; }
  /* Restate the order with the same id-level specificity as the wide-screen
     rules above, otherwise those win here and the row comes out scrambled.
     Reading order: mark, then the actions, then ⋯ pinned to the far end. */
  header .brand { order: 0; }
  #viewBtn      { order: 1; }
  #readBtn      { order: 2; }
  #exportMenu   { order: 3; }
  #copyMenu     { order: 4; }
  #themeBtn     { order: 5; }
  #moreMenu     { order: 6; margin-inline-start: auto; }

  /* the ⋯ sheet: a real dropdown again, laid out as a grid of labelled cells */
  #moreMenu { display: inline-block; position: relative; }
  #moreBtn { display: inline-block; }
  #moreMenu > .menu-list {
    display: none;
    position: absolute;
    top: calc(100% + 6px);
    inset-inline-end: 0;
    inset-inline-start: auto;
    grid-template-columns: repeat(4, minmax(64px, 1fr));
    gap: 6px;
    padding: 8px;
    min-width: 0;
    overflow: visible;
  }
  #moreMenu.open > .menu-list { display: grid; }
  #moreMenu .menu-list button {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
    padding: 8px 4px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 11px;
  }
  #moreMenu .menu-list .lbl { display: block; }   /* the sheet is labelled */
  #moreMenu .menu-list .ic svg { width: 19px; height: 19px; }
  /* the GitHub mark is nice-to-have; keep it out of the one-row budget */
  #ghLink { display: none; }

  /* touch scrollbars are overlay, so the desktop scrollbar clearance just
     strands the ✕ in the middle of the text — tuck it back to the corner */
  .read-exit { top: 10px; inset-inline-end: 10px; }

  .main { flex-direction: column; }
  .divider { cursor: row-resize; }
  .divider::before { inset: -4px 0; }
  #viewBtn { display: inline-block; }
  .preview-pane { border-top: 1px solid var(--border); }
  body.mobile-edit .preview-pane { display: none; }
  body.mobile-preview .edit-pane { display: none; }
  /* single-pane modes have nothing to resize between */
  body.mobile-edit .divider, body.mobile-preview .divider { display: none; }
}
