/**
 * print.css — Print preview overlay and @media print rules.
 *
 * Structure:
 *   1. On-screen preview (overlay, toolbar, A4 sheet frame)
 *   2. Printed content (markdown typography)
 *   3. Cover page and table of contents
 *   4. Binder sections (one page per document)
 *   5. @media print + @page
 *
 * Print defaults: DIN A4, colour, images and graphics. The colour requirement
 * is met with `print-color-adjust: exact`, which makes Chrome, Safari and
 * Firefox print backgrounds even when the browser's "Background graphics"
 * option is off.
 */

/* ===== 1. On-Screen Preview ===== */
.print-overlay {
  position: fixed;
  inset: 0;
  z-index: 3000;
  background: #d9d2c6;
  overflow: auto;
  display: none;
}

.print-overlay.open {
  display: block;
}

/** While the preview is up, only the preview scrolls. */
body.print-overlay-open {
  overflow: hidden;
}

.print-toolbar {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  background: var(--color-surface-2, #fffdf8);
  border-bottom: 1px solid var(--color-border, #d4c9b8);
}

.print-toolbar-title {
  flex: 1;
  min-width: 0;
  font-weight: 600;
  color: var(--color-text, #3d3229);
}

/** Non-interactive reminder of the settings applied at print time. */
.print-toolbar-hint {
  flex-shrink: 0;
  padding: 2px 8px;
  border: 1px solid var(--color-border, #d4c9b8);
  border-radius: 999px;
  background: var(--color-bg, #f5f0e8);
  color: var(--color-text-muted, #8a7e72);
  font-size: 0.72rem;
  white-space: nowrap;
}

/**
 * The A4 sheet. On screen it is framed so the user can see the page they are
 * about to print; in print the frame is stripped and the sheet fills the page.
 */
.print-page {
  width: 210mm;
  max-width: 100%;
  margin: 24px auto 48px;
  padding: 18mm 16mm;
  background: #fff;
  color: #1a1a1a;
  box-shadow: 0 2px 12px rgba(40, 34, 28, 0.22);
}

/* ===== 2. Printed Content ===== */
.print-content {
  max-width: none;
  font-family: var(--font-serif, Georgia, serif);
  font-size: 11pt;
  line-height: 1.65;
  color: #1a1a1a;
  word-wrap: break-word;
  orphans: 3;
  widows: 3;
}

.print-empty {
  color: #8a7e72;
  font-style: italic;
}

.print-content h1,
.print-content h2,
.print-content h3,
.print-content h4,
.print-content h5,
.print-content h6 {
  font-family: var(--font-serif, Georgia, serif);
  line-height: 1.25;
  margin: 1.4em 0 0.5em;
  break-after: avoid;
  page-break-after: avoid;
}

.print-content h1 { font-size: 1.9rem; }
.print-content h2 { font-size: 1.5rem; }
.print-content h3 { font-size: 1.2rem; }
.print-content h4 { font-size: 1rem; }

.print-content p {
  margin: 0.7em 0;
}

.print-content a {
  color: var(--color-primary, #6b5b4e);
  text-decoration: underline;
}

.print-content img {
  max-width: 100%;
  height: auto;
  break-inside: avoid;
  page-break-inside: avoid;
}

/* Scoped to the construct, not the print sheet: public pastes and comments
 * render the same DOM through markdown-print.js. */
.print-image {
  display: inline-block;
  max-width: 100%;
  vertical-align: top;
  break-inside: avoid;
  page-break-inside: avoid;
}

.print-image-link {
  display: block;
  max-width: 100%;
}

.print-image img {
  display: block;
  max-width: 100%;
  height: auto;
}

.print-image-caption {
  display: block;
  margin-top: 0.5em;
  color: #6a737d;
  font-size: 0.875em;
  font-style: italic;
  text-align: center;
  break-inside: avoid;
  page-break-inside: avoid;
}

.print-content code {
  font-family: var(--font-mono, Menlo, monospace);
  font-size: 0.85em;
  background: #f1ede6;
  border-radius: 3px;
  padding: 1px 4px;
}

.print-content pre {
  background: #f7f4ef;
  border: 1px solid var(--color-border, #d4c9b8);
  border-radius: 6px;
  padding: 12px;
  overflow: auto;
  margin: 0.9em 0;
  break-inside: avoid;
  page-break-inside: avoid;
}

.print-content pre code {
  background: none;
  border: none;
  padding: 0;
}

.print-content blockquote {
  margin: 0.9em 0;
  padding-left: 16px;
  border-left: 3px solid var(--color-border-2, #c8bba8);
  color: #5f564c;
  break-inside: avoid;
}

.print-content blockquote p {
  margin: 0.3em 0;
}

.print-content hr {
  border: none;
  border-top: 1px solid var(--color-border, #d4c9b8);
  margin: 1.6em 0;
}

.print-content ul,
.print-content ol {
  margin: 0.7em 0;
  padding-left: 1.6em;
}

.print-content li {
  margin: 0.2em 0;
}

.print-content li > input[type="checkbox"] {
  margin-right: 6px;
  transform: translateY(1px);
}

.print-content table {
  border-collapse: collapse;
  width: 100%;
  margin: 0.9em 0;
  font-family: var(--font-sans, system-ui, sans-serif);
  font-size: 0.9em;
}

/** Repeat the header row when a table spans a page break. */
.print-content thead {
  display: table-header-group;
}

.print-content tr {
  break-inside: avoid;
  page-break-inside: avoid;
}

.print-content th,
.print-content td {
  border: 1px solid var(--color-border, #d4c9b8);
  padding: 6px 10px;
  text-align: left;
  vertical-align: top;
}

.print-content th {
  background: #efebe3;
  font-weight: 600;
}

/* ===== Segment element ===== */
.print-content .seg {
  margin: 0.9em 0;
  break-inside: avoid;
  page-break-inside: avoid;
}

.print-content .seg-row {
  display: flex;
  align-items: stretch;
}

.print-content .seg-row:first-child .seg-left,
.print-content .seg-row:first-child .seg-right {
  padding-top: 4px;
}

.print-content .seg-row:last-child .seg-left,
.print-content .seg-row:last-child .seg-right {
  padding-bottom: 4px;
}

.print-content .seg-left,
.print-content .seg-right {
  display: inline-block;
  padding: 1px 10px;
  white-space: pre-wrap;
}

.print-content .seg-left {
  flex: 1;
}

.print-content .seg-right {
  flex: 1;
  border-left: 1px solid; /* color set inline from the stored background */
}

.print-content .seg-row-left-only .seg-left {
  border-radius: 6px;
}

/* ===== 3. Cover Page and Table of Contents ===== */
.print-cover-title {
  font-family: var(--font-serif, Georgia, serif);
  font-size: 2.4rem;
  line-height: 1.15;
  font-weight: 600;
  margin: 0 0 0.2em;
  color: #1a1a1a;
}

.print-cover-description {
  font-family: var(--font-serif, Georgia, serif);
  font-size: 1.05rem;
  font-style: italic;
  color: #5f564c;
  margin: 0 0 1.2em;
}

.print-cover-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 28px;
  padding: 10px 0;
  border-top: 1px solid var(--color-border, #d4c9b8);
  border-bottom: 1px solid var(--color-border, #d4c9b8);
  font-family: var(--font-sans, system-ui, sans-serif);
  font-size: 0.85rem;
}

.print-cover-meta-item {
  display: flex;
  flex-direction: column;
}

.print-cover-meta-label {
  text-transform: uppercase;
  letter-spacing: 0.07em;
  font-size: 0.68rem;
  color: #8a7e72;
}

.print-cover-meta-value {
  font-weight: 600;
  color: #1a1a1a;
}

.print-cover-tags,
.print-doc-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin: 12px 0 0;
}

.print-tag {
  display: inline-block;
  padding: 1px 8px;
  border: 1px solid var(--color-border-2, #c8bba8);
  border-radius: 999px;
  background: #f7f4ef;
  color: #5f564c;
  font-family: var(--font-sans, system-ui, sans-serif);
  font-size: 0.72rem;
  white-space: nowrap;
}

.print-toc-heading {
  font-family: var(--font-sans, system-ui, sans-serif);
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: #8a7e72;
  margin: 2.2em 0 0.6em;
  break-after: avoid;
  page-break-after: avoid;
}

.print-toc {
  font-family: var(--font-sans, system-ui, sans-serif);
  font-size: 0.92rem;
  line-height: 1.5;
}

.print-toc-row {
  display: flex;
  align-items: baseline;
  gap: 4px;
  padding-left: calc(var(--depth, 0) * 1.1em);
}

.print-toc-label {
  flex-shrink: 0;
}

.print-toc-folder {
  margin-top: 0.7em;
  font-weight: 700;
  color: #1a1a1a;
  break-after: avoid;
  page-break-after: avoid;
}

.print-toc-folder .print-toc-label {
  padding-bottom: 2px;
  border-bottom: 1px solid var(--color-border, #d4c9b8);
}

.print-toc-doc {
  color: #3d3229;
}

/** Dot leader between a document title and the right margin. */
.print-toc-dots {
  flex: 1;
  overflow: hidden;
  min-width: 1em;
}

.print-toc-dots::after {
  content: ' . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .';
  color: #b6ab9c;
}

/* ===== 4. Binder Sections ===== */
/** Every document starts a fresh page; the cover is the exception. */
.print-doc {
  break-before: page;
  page-break-before: always;
}

.print-doc--cover {
  break-before: auto;
  page-break-before: auto;
}

/**
 * Never let the first section emit a leading blank sheet — which would happen
 * for a single-document print, where the only `.print-doc` is also the first
 * child of the sheet.
 */
.print-page > .print-doc:first-child {
  break-before: auto;
  page-break-before: auto;
}

.print-doc-head {
  margin-bottom: 1.2em;
  padding-bottom: 0.5em;
  border-bottom: 1px solid var(--color-border, #d4c9b8);
  break-after: avoid;
  page-break-after: avoid;
}

.print-doc-breadcrumb {
  font-family: var(--font-sans, system-ui, sans-serif);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #8a7e72;
  margin-bottom: 4px;
}

.print-doc-title {
  font-family: var(--font-serif, Georgia, serif);
  font-size: 1.9rem;
  line-height: 1.2;
  font-weight: 600;
  margin: 0;
  color: #1a1a1a;
}

.print-doc-meta {
  margin: 8px 0 0;
  font-family: var(--font-sans, system-ui, sans-serif);
  font-size: 0.75rem;
  color: #8a7e72;
}

.print-doc-meta-item {
  margin-right: 12px;
}

/**
 * A folder band, printed at the top of the first page of its section.
 *
 * binder-print.js places the band *inside* the first `.print-doc` of the
 * section, because every `.print-doc` forces a page break and a band emitted
 * as a sibling would be stranded at the foot of the previous page. Being the
 * first child of a page, it needs no break control of its own; `break-after:
 * avoid` is kept only so it can never separate from the heading below it.
 */
.print-section {
  break-after: avoid;
  page-break-after: avoid;
  padding: 10px 0 6px;
  margin: 0;
  border-bottom: 2px solid var(--color-primary, #6b5b4e);
  margin-left: calc(var(--depth, 0) * 1.1em);
}

.print-section-label {
  font-family: var(--font-sans, system-ui, sans-serif);
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--color-primary, #6b5b4e);
}

/* ===== 5. Print Media ===== */
@media print {
  /**
   * Print nothing but the preview. This replaces an explicit list of #app,
   * #modal-container and #toast-container, and additionally covers
   * #auth-overlay and the body-appended .notification-panel, which would
   * otherwise land on the page.
   *
   * With no preview open the result is a blank sheet, which is deliberate:
   * printing the live editor would put CodeMirror chrome on paper.
   */
  body > *:not(.print-overlay) {
    display: none !important;
  }

  /**
   * The toolbar lives *inside* the overlay, so the rule above cannot reach it.
   * It is screen-only chrome and must be hidden explicitly, otherwise "Print /
   * Save as PDF" and the A4 hint print on top of the first page.
   */
  .print-toolbar {
    display: none !important;
  }

  /**
   * Force backgrounds and graphics to print regardless of the browser's
   * "Background graphics" setting. Required for the segment chips, code
   * shading, table headers and tag pills.
   */
  .print-overlay,
  .print-overlay * {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  /**
   * Release the app's viewport lock. `main.css` sets
   * `html, body { height: 100%; overflow: hidden }` for the on-screen app; in
   * print that pins the body to a single viewport height, so everything past
   * the first page is *clipped* rather than paginated. Auto height plus visible
   * overflow is what lets the document fragment across pages at all.
   */
  html,
  body {
    height: auto;
    min-height: 0;
    margin: 0;
    padding: 0;
    overflow: visible;
    background: #fff;
  }

  /**
   * The preview's scroll lock is a screen affordance. The class is still on
   * <body> while printing (it is only cleared in close()), so it has to be
   * released here or it re-applies `overflow: hidden` over the rule above.
   */
  body.print-overlay-open {
    overflow: visible;
  }

  .print-overlay,
  .print-overlay.open {
    position: static;
    inset: auto;
    display: block;
    width: auto;
    height: auto;
    max-width: none;
    max-height: none;
    overflow: visible;
    background: #fff;
  }

  /** Strip the screen preview frame — the sheet *is* the page now. */
  .print-page {
    width: auto;
    max-width: none;
    margin: 0;
    padding: 0;
    box-shadow: none;
  }

  .print-content a {
    color: #000;
  }

  /** Keep link-preview cards on one page; cards are decorative, not text. */
  .lp-card,
  .lp-widget {
    break-inside: avoid;
  }

  .lp-click {
    display: none;
  }

  /** Galleries print as a stack of plates — the carousel chrome is screen-only. */
  .cm-gallery {
    display: block;
    max-width: none;
  }

  .cm-gallery-viewport {
    display: block;
    position: static;
    aspect-ratio: auto;
    overflow: visible;
  }

  .cm-gallery-slide {
    display: block;
    position: static;
    width: auto;
    height: auto;
    opacity: 1;
  }

  .cm-gallery-slide img {
    max-width: 100%;
    height: auto;
  }

  .cm-gallery-caption,
  .cm-gallery-pagination,
  .cm-gallery-tools,
  .cm-gallery-arrow {
    display: none;
  }

  /** An image and its caption must never be split across pages. */
  .cm-gallery,
  .gallery-static,
  .gallery-static-item {
    break-inside: avoid;
  }

  /** DIN A4, portrait. A user-selected paper size still wins in the dialog. */
  @page {
    size: A4 portrait;
    margin: 18mm 16mm;
  }
}
