/* ──────────────────────────────────────────────────────────────────────────
   Markdown Reader & Markdown → PDF Converter
   Built on the shared Generic Soft UI design system (dark.css / light.css /
   shared.css). Default theme is LIGHT. PDF export is ALWAYS rendered light.
   ────────────────────────────────────────────────────────────────────────── */

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

/* ── HEADER ── */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 22px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  gap: 16px;
  box-shadow: var(--shadow);
  z-index: 10;
}
.header-left { display: flex; align-items: baseline; gap: 14px; flex-wrap: wrap; }
.logo { font-size: 1.2rem; font-weight: 800; letter-spacing: -0.04em; }
.logo span { color: var(--accent); }
.tagline { font-size: 0.78rem; color: var(--muted); }

/* ── DROP ZONE ── */
#dropZone {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin: 28px;
  padding: 40px;
  border: 2px dashed var(--border);
  border-radius: 16px;
  background: var(--card-bg);
  cursor: pointer;
  transition: all 0.2s;
  position: relative;
  overflow: hidden;
  text-align: center;
}
#dropZone:hover, #dropZone.dragover { border-color: var(--accent); background: rgba(90,145,232,0.05); }
#dropZone input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; height: 100%; }
#dropZone .btn-secondary { position: relative; z-index: 2; }
.drop-icon { font-size: 52px; }
.drop-title { font-size: 1.1rem; font-weight: 700; }
.drop-sub { font-size: 0.82rem; color: var(--muted); }
.drop-badges { display: flex; gap: 6px; margin-top: 4px; }
.fmt-badge {
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 2px 8px;
  font-size: 0.72rem;
  font-family: 'DM Mono', monospace;
  color: var(--muted);
}

/* ── WORKSPACE ── */
#workspace { flex: 1; display: none; flex-direction: column; min-height: 0; }
#workspace.active { display: flex; }

/* ── TOOLBAR ── */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
  padding: 10px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  position: sticky;
  top: 0;
  z-index: 5;
}
.tb-toggles { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.tb-toggle { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.tb-toggle-label { font-size: 0.82rem; font-weight: 600; color: var(--text); white-space: nowrap; }
.tb-actions { display: flex; align-items: center; gap: 10px; }

/* ── DOCUMENT SCROLL AREA ── */
.doc-scroll {
  flex: 1;
  overflow-y: auto;
  background: var(--bg-subtle);
  padding: 28px 16px 48px;
}
.md-paper {
  max-width: 820px;
  margin: 0 auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 56px 64px;
}

/* ── MARKDOWN SOURCE EDITOR ── */
.md-editor {
  display: none;
  max-width: 820px;
  width: 100%;
  margin: 0 auto;
  min-height: 70vh;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 28px 32px;
  font-family: 'DM Mono', monospace;
  font-size: 0.88rem;
  line-height: 1.7;
  color: var(--text);
  resize: vertical;
  outline: none;
  transition: border-color 0.15s;
}
.md-editor:focus { border-color: var(--accent); }
.doc-scroll.editing .md-paper { display: none; }
.doc-scroll.editing .md-editor { display: block; }

/* ============================================================
   MARKDOWN BODY TYPOGRAPHY  (shared by screen view + PDF pages)
   ============================================================ */
.md-body {
  font-family: 'DM Sans', system-ui, -apple-system, sans-serif;
  font-size: 16px;
  line-height: 1.7;
  color: var(--text);
  word-wrap: break-word;
}
.md-body > *:first-child { margin-top: 0 !important; }
.md-body > *:last-child  { margin-bottom: 0 !important; }

.md-body h1, .md-body h2, .md-body h3,
.md-body h4, .md-body h5, .md-body h6 {
  font-weight: 800;
  line-height: 1.25;
  letter-spacing: -0.01em;
  margin: 1.6em 0 0.6em;
  color: var(--text);
}
.md-body h1 { font-size: 2rem;    border-bottom: 2px solid var(--border); padding-bottom: .3em; letter-spacing: -0.02em; }
.md-body h2 { font-size: 1.55rem; border-bottom: 1px solid var(--border); padding-bottom: .25em; }
.md-body h3 { font-size: 1.28rem; }
.md-body h4 { font-size: 1.1rem; }
.md-body h5 { font-size: 1rem; }
.md-body h6 { font-size: 0.9rem; color: var(--muted); }

.md-body p { margin: 0 0 1em; }
.md-body a { color: var(--accent-strong); text-decoration: underline; text-underline-offset: 0.15em; }
.md-body strong { font-weight: 700; }
.md-body ul, .md-body ol { margin: 0 0 1em; padding-left: 1.6em; }
.md-body li { margin: 0.3em 0; }
.md-body li > ul, .md-body li > ol { margin: 0.3em 0; }

.md-body blockquote {
  margin: 0 0 1em;
  padding: 0.4em 1.1em;
  border-left: 4px solid var(--accent);
  background: var(--input-bg);
  border-radius: 0 8px 8px 0;
  color: var(--muted);
}
.md-body blockquote p:last-child { margin-bottom: 0; }

.md-body hr { border: none; border-top: 1px solid var(--border); margin: 2em 0; }

.md-body img { max-width: 100%; height: auto; border-radius: 8px; }

/* Inline + block code */
.md-body code {
  font-family: 'DM Mono', monospace;
  font-size: 0.88em;
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 0.12em 0.4em;
}
.md-body pre {
  margin: 0 0 1.1em;
  padding: 16px 18px;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow-x: auto;
  line-height: 1.55;
}
.md-body pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 0.85rem;
  white-space: pre;
}

/* Tables */
.md-body table {
  width: 100%;
  border-collapse: collapse;
  margin: 0 0 1.2em;
  font-family: 'DM Sans', sans-serif;
  font-size: 0.92rem;
}

/* Wide tables that overflow are wrapped (on screen only) in a scroll box the
   user can drag/pan horizontally — no need to scroll to the bottom of the page
   to reach a horizontal scrollbar. This wrapper never reaches the PDF: PDF
   pagination reads the bare <table>, so print/export behaviour is unchanged. */
.md-body .table-scroll {
  overflow-x: auto;
  max-width: 100%;
  margin: 0 0 1.2em;
  border-radius: 8px;
  scrollbar-width: thin;
  overscroll-behavior-x: contain;
}
.md-body .table-scroll > table { margin: 0; }
.md-body .table-scroll.is-scrollable { cursor: grab; }
.md-body .table-scroll.is-scrollable.dragging {
  cursor: grabbing;
  user-select: none;
}
.md-body .table-scroll::-webkit-scrollbar { height: 9px; }
.md-body .table-scroll::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 5px;
}
.md-body .table-scroll::-webkit-scrollbar-thumb:hover { background: var(--muted); }
.md-body th, .md-body td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  text-align: left;
  white-space: normal;
  vertical-align: top;
}
.md-body thead th {
  background: var(--input-bg);
  font-weight: 700;
  color: var(--text);
  text-transform: none;
  letter-spacing: 0;
  font-size: 0.92rem;
}
.md-body tbody tr:nth-child(even) td { background: rgba(0,0,0,0.015); }

/* Sticky table header (screen view only).
   A floating clone of a table's header row, pinned to the top of the
   document scroll area while a long table is scrolled through, so column
   headings stay visible down to the last row. It is built and positioned
   in JS and lives in the scroll container — never in #mdBody — so PDF
   pagination (which reads the bare <table> from #mdBody) is unaffected. */
.md-sticky-thead {
  position: fixed;
  overflow: hidden;          /* clips the clone down to just the header row */
  z-index: 4;                /* above the paper, below the sticky toolbar */
  display: none;
  margin: 0;
  pointer-events: none;      /* let drag-to-pan reach the table underneath */
  background: var(--panel);
  box-shadow: var(--shadow);
}
.md-sticky-thead.visible { display: block; }
.md-sticky-thead table { margin: 0 !important; }
.md-sticky-thead tbody { visibility: hidden; }

/* Task list checkboxes */
.md-body input[type=checkbox] { margin-right: 6px; transform: translateY(1px); }

/* Mermaid diagram blocks */
.md-body .mermaid {
  margin: 0 0 1.2em;
  text-align: center;
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  overflow-x: auto;
}
.md-body .mermaid svg { max-width: 100%; height: auto; }

/* ── AUTO TABLE OF CONTENTS ── */
.md-toc {
  margin: 0 0 1.6em;
  padding: 18px 22px;
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.md-toc-title {
  font-weight: 800;
  font-size: 1.05rem;
  margin-bottom: 10px;
  letter-spacing: -0.01em;
}
.md-toc ul { list-style: none; margin: 0; padding: 0; }
.md-toc li { margin: 3px 0; line-height: 1.45; }
.md-toc a { color: var(--accent-strong); text-decoration: none; font-size: 0.92rem; }
.md-toc a:hover { text-decoration: underline; }
.md-toc .lvl-2 { padding-left: 0; }
.md-toc .lvl-3 { padding-left: 16px; }
.md-toc .lvl-4 { padding-left: 32px; }
.md-toc .lvl-5, .md-toc .lvl-6 { padding-left: 48px; }

/* ============================================================
   PDF PAGINATION STAGE  (offscreen on screen, shown only in print)
   ============================================================ */
#pdfStage {
  position: absolute;
  left: -100000px;
  top: 0;
  width: 794px;
}
.pdf-page {
  position: relative;
  width: 794px;
  min-height: 1123px;
  height: 1123px;
  padding: 72px 64px;
  background: #ffffff;
  overflow: hidden;
  box-sizing: border-box;
}
.pdf-page-content { position: relative; z-index: 1; }
.pdf-watermark {
  position: absolute;
  right: 30px;
  bottom: 26px;
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 7px;
  font-family: 'DM Sans', sans-serif;
  font-size: 10.5px;
  font-weight: 600;
  color: #9aa7bd;
  opacity: 0.78;
  pointer-events: none;
}
.pdf-watermark img { width: 15px; height: 15px; opacity: 0.85; }
/* Centred page number on every PDF page */
.pdf-pagenum {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 30px;
  z-index: 2;
  text-align: center;
  font-family: 'DM Sans', sans-serif;
  font-size: 11px;
  color: #9aa7bd;
  pointer-events: none;
}

/* Dedicated Table-of-Contents page(s). Selectors are scoped under .md-body so
   they outrank the generic .md-body ul/li/a typography rules. */
.md-body .pdf-toc-title {
  font-weight: 800;
  font-size: 1.7rem;
  letter-spacing: -0.02em;
  margin: 0 0 0.9em;
  padding-bottom: 0.3em;
  border-bottom: 2px solid var(--border);
}
.md-body .pdf-toc-list { list-style: none; margin: 0; padding: 0; }
.md-body .pdf-toc-list li {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin: 0.5em 0;
  line-height: 1.4;
}
.md-body .pdf-toc-list li.lvl-1 { font-weight: 700; }
.md-body .pdf-toc-list li.lvl-2 { padding-left: 0; }
.md-body .pdf-toc-list li.lvl-3 { padding-left: 18px; }
.md-body .pdf-toc-list li.lvl-4 { padding-left: 36px; }
.md-body .pdf-toc-list li.lvl-5,
.md-body .pdf-toc-list li.lvl-6 { padding-left: 54px; font-size: 0.95em; }
.md-body .pdf-toc-label {
  flex: 0 1 auto;
  color: var(--text);
  text-decoration: none;
}
.md-body .pdf-toc-dots {
  flex: 1 1 auto;
  min-width: 14px;
  border-bottom: 1px dotted var(--muted);
  position: relative;
  bottom: 0.25em;
  opacity: 0.55;
}
.md-body .pdf-toc-pageno {
  flex: 0 0 auto;
  padding-left: 4px;
  color: var(--muted);
  font-size: 0.9em;
  font-variant-numeric: tabular-nums;
}

/* ── PRINT TIP MODAL ── */
#printTip {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 9100;
  background: rgba(15,23,40,0.45);
  backdrop-filter: blur(2px);
  align-items: center;
  justify-content: center;
  padding: 20px;
}
#printTip.visible { display: flex; }
.print-tip-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow-lg);
  padding: 24px 26px;
  max-width: 440px;
  width: 100%;
}
.print-tip-title { font-weight: 800; font-size: 1.05rem; letter-spacing: -0.01em; margin-bottom: 10px; }
.print-tip-text { font-size: 0.9rem; line-height: 1.6; color: var(--text); margin: 0 0 12px; }
.print-tip-do, .print-tip-dont {
  font-size: 0.84rem;
  line-height: 1.55;
  border-radius: 9px;
  padding: 9px 12px;
  margin-bottom: 8px;
}
.print-tip-do {
  background: var(--positive-bg, rgba(46,160,67,0.10));
  border: 1px solid var(--positive-border, rgba(46,160,67,0.35));
  color: var(--positive-em, var(--text));
  font-weight: 600;
}
.print-tip-do span { font-weight: 400; color: var(--muted); }
.print-tip-dont {
  background: var(--negative-bg, rgba(220,70,70,0.08));
  border: 1px solid var(--negative-border, rgba(220,70,70,0.30));
  color: var(--negative-em, var(--text));
}
.print-tip-skip {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 0.82rem;
  color: var(--muted);
  margin: 14px 0 16px;
  cursor: pointer;
  user-select: none;
}
.print-tip-actions { display: flex; justify-content: flex-end; gap: 10px; }

/* ── PROGRESS OVERLAY ── */
#pdfOverlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 9000;
  background: rgba(15,23,40,0.45);
  backdrop-filter: blur(2px);
  align-items: center;
  justify-content: center;
}
#pdfOverlay.visible { display: flex; }
.pdf-overlay-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow-lg);
  padding: 26px 34px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}
.pdf-overlay-msg { font-weight: 600; font-size: 0.92rem; color: var(--text); }
.spinner {
  width: 34px; height: 34px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

/* ── FOOTER ── */
.page-footer { flex-shrink: 0; padding: 16px 20px; }
.page-footer-info { margin-top: 0; }
.page-footer-info a { color: var(--accent); text-decoration: underline; text-underline-offset: 0.14em; }

/* ── RESPONSIVE ── */
@media (max-width: 720px) {
  .md-paper { padding: 32px 22px; }
  .toolbar { padding: 10px 14px; }
  .tb-toggles { gap: 12px; }
  .header { flex-wrap: wrap; }
}

/* ============================================================
   PRINT  — ALWAYS LIGHT, paginated pages only
   ============================================================ */
@page { size: 794px 1123px; margin: 0; }

@media print {
  html, body { height: auto; background: #ffffff !important; }
  body > *:not(#pdfStage) { display: none !important; }
  #pdfStage {
    position: static !important;
    left: 0 !important;
    width: auto !important;
    display: block !important;
  }
  .pdf-page {
    page-break-after: always;
    break-after: page;
    box-shadow: none !important;
  }
  .pdf-page:last-child { page-break-after: auto; break-after: auto; }
  /* Force light tokens during print regardless of on-screen theme */
  .pdf-page, .pdf-page * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
