/* views/print.css — the print screen and its paper documents (views/print.js).
   On screen: options on the left, a white "paper" preview on the right.
   In print: only the document, black on white, A4, sensible page breaks.
   Sizes are in pt/mm inside the document because paper is measured that way. */

/* ── The screen: options | preview ───────────────────────────────── */
.print-page {
  display: grid; grid-template-columns: 340px minmax(0, 1fr); gap: var(--s-5); align-items: start;
  max-width: 1320px; margin-inline: auto; padding: var(--s-5) var(--gutter) var(--s-8);
}
.print-controls {
  position: sticky; top: calc(var(--bar-h) + var(--s-3));
  max-height: calc(100dvh - var(--bar-h) - var(--s-6));
  display: flex; flex-direction: column; padding: 0; overflow: hidden;
}
/* Options scroll, the two actions stay in reach at the bottom of the card. */
.print-options { display: grid; gap: var(--s-4); padding: var(--s-5); overflow-y: auto; min-height: 0; }
.print-controls h1 { font-size: var(--text-xl); letter-spacing: -.015em; }
.print-lead { margin-top: calc(var(--s-2) * -1); }
.print-controls fieldset { border: 0; padding: 0; margin: 0; display: grid; gap: var(--s-3); min-width: 0; }
.print-controls legend { font-weight: 700; font-size: var(--text-sm); margin-bottom: var(--s-2); padding: 0; }
.print-controls .seg { flex-wrap: wrap; }
.print-controls fieldset + fieldset { padding-top: var(--s-4); border-top: 1px solid var(--line); }

.checks { display: flex; flex-wrap: wrap; gap: var(--s-1) var(--s-4); }

.ctl-grid { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s-3); }
.ctl-grid label { display: grid; gap: 4px; font-size: var(--text-sm); color: var(--ink-2); }
.ctl-grid select {
  min-height: var(--tap); padding: 0 var(--s-2); border-radius: var(--r-sm);
  border: 1px solid var(--line); background: var(--surface); color: var(--ink); font-size: var(--text-sm);
}

@media (pointer: coarse), (max-width: 640px) { .ctl-grid select { font-size: 16px; } }   /* no iPhone zoom on focus */

.layout-pick { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s-2); }
.layout-opt {
  display: grid; gap: 2px; text-align: start; padding: var(--s-2) var(--s-3); border-radius: var(--r-md);
  border: 1.5px solid var(--line); background: var(--surface); transition: border-color var(--fast);
}
.layout-opt b { font-size: var(--text-sm); }
.layout-opt span { font-size: var(--text-xs); color: var(--ink-3); line-height: 1.3; }
.layout-opt:hover { border-color: var(--line-strong); }
.layout-opt[aria-pressed="true"] { border-color: var(--brand); background: var(--brand-soft); }
.layout-opt[aria-pressed="true"] b { color: var(--brand); }
.print-go {
  display: grid; gap: var(--s-2); flex: none;
  padding: var(--s-4) var(--s-5) var(--s-5);
  background: var(--surface); border-top: 1px solid var(--line);
}

/* ── The paper ───────────────────────────────────────────────────── */
/* The preview is a real A4 sheet: 210mm wide with the same 12mm margins
   the PDF uses, so the line breaks on screen match the downloaded file.
   On narrow screens JS scales this whole sheet down to fit. */
.print-preview { min-width: 0; overflow: hidden; }
.print-doc {
  width: 210mm; max-width: none; margin-inline: auto;
  background: #fff; color: #111;
  padding: 12mm; border-radius: var(--r-md);
  box-shadow: var(--shadow-2); border: 1px solid var(--line);
  font-size: 10pt; line-height: 1.45;
  transform-origin: top left;
}
.print-doc.size-small { font-size: 9pt; }
.print-doc.size-large { font-size: 12.5pt; }
.print-doc p { max-width: none; }

.p-head {
  display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap;
  padding-bottom: 8px; margin-bottom: 10px; border-bottom: 2px solid #111; font-size: .95em;
}
.p-brand { display: inline-flex; align-items: center; gap: 6px; font-family: var(--font-display); font-weight: 700; }
.p-brand .brand-mark { width: 18px; height: 18px; }
.p-name { display: grid; grid-template-columns: 2fr 1fr 1fr; gap: 12px; margin-bottom: 12px; font-size: .85em; color: #555; }
.p-name span { border-bottom: 1px solid #999; padding: 10px 0 2px; }
.p-howto { margin-bottom: 10px; color: #444; font-size: .9em; }
.p-meta { display: block; color: #666; font-size: .82em; }
.p-sense { color: #555; font-weight: 400; }
.p-w { font-family: var(--font-word); font-size: 1.1em; }
.p-ipa { display: block; color: #444; font-size: .9em; }
.p-ar { font-family: var(--font-arabic); font-size: 1.1em; }
.p-note { display: block; margin-top: 3px; color: #5a4a00; font-size: .85em; }
.p-n { color: #888; font-size: .82em; width: 2.2em; }
.p-empty-note { color: #666; padding: 24px 0; }

/* Word sheet and self-test tables */
.p-table { width: 100%; border-collapse: collapse; }
.p-table th { text-align: start; font-size: .8em; font-weight: 600; color: #555; border-bottom: 1px solid #999; padding: 4px 6px; }
.p-table td { padding: 6px; border-bottom: 1px solid #ddd; vertical-align: top; }
.p-table tr { break-inside: avoid; }
.p-sheet .p-word { width: 26%; }
.p-sheet .p-def i { display: block; color: #444; margin-top: 2px; }
.p-sheet .p-ar, .p-ar-h { width: 22%; text-align: right; }

.p-test .p-prompt { width: 30%; }
.p-test .p-line { width: 28%; }
/* One clean writing rule. (A CSS gradient filled the cell as a grey band
   when html2canvas drew the PDF, so the rule is a real border instead.) */
.p-answer-rule { display: block; height: 1.15em; margin-top: .15em; border-bottom: 1px solid #b9b9b9; }
.p-test .p-fold { width: 0; padding: 0; border-left: 1.5px dashed #888; }
.p-test .p-answer { width: 30%; padding-inline-start: 10px; }
.p-test .p-tick { width: 2.4em; }
.p-test .p-tick span { display: block; width: 14px; height: 14px; border: 1.2px solid #888; border-radius: 3px; margin: 2px auto 0; }

/* Compact list: two columns, one line per word. Tighter than the table
   layouts so a typical 25–30 word list still fits on one A4 page. */
.p-compact { columns: 2; column-gap: 10mm; padding-inline-start: 1.6em; line-height: 1.3; }
.p-compact li { break-inside: avoid; padding: 2px 0; border-bottom: 1px solid #eee; }
.p-compact .ipa { color: #555; font-size: .85em; }
.p-compact .p-ar { display: block; text-align: right; }

/* Fill the gap */
.p-bank { display: flex; flex-wrap: wrap; gap: 6px 10px; padding: 8px 10px; margin-bottom: 12px; border: 1.2px solid #999; border-radius: 6px; }
.p-bank b { width: 100%; font-size: .8em; color: #555; }
.p-bank span { font-family: var(--font-word); }
.p-gaps { display: grid; gap: 7px; padding-inline-start: 1.6em; }
.p-gaps li { break-inside: avoid; }
.p-blank { display: inline-block; width: 30mm; border-bottom: 1.2px solid #444; margin: 0 4px; }
.p-blank.is-tight { margin-inline-end: 0; }   /* "Eventually, …" not "____ ," */
.p-hint-ar { color: #666; font-size: .95em; }

/* Matching: words (numbers) | meanings (letters) */
.p-match { display: grid; grid-template-columns: 1fr 1fr; gap: 10mm; padding: 4px 0 8px; border-bottom: 1px solid #ddd; margin-bottom: 8px; break-inside: avoid; }
.p-match .p-meta { line-height: 1.3; }
.p-match ol { display: grid; gap: 5px; padding-inline-start: 1.8em; }
.p-box { display: inline-block; width: 18px; height: 18px; border: 1.2px solid #777; border-radius: 3px; vertical-align: middle; margin-inline-end: 8px; }
/* Keep the Arabic meaning and its English definition on the same left
   edge as the letter (a., b.), instead of floating Arabic to the far right. */
.p-match .p-ar { display: block; text-align: left; }

/* Write a sentence: two ruled lines per word, tight enough to keep the
   sheet to a sensible number of pages. */
.p-write { display: grid; gap: 10px; padding-inline-start: 1.6em; }
.p-write li { break-inside: avoid; }
.p-write-head { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.p-write-head .p-ar { margin-left: auto; }   /* the Arabic meaning sits at the right edge */
.p-rule { display: block; height: 8mm; border-bottom: 1px solid #aaa; }

/* Answer key: flows after the last question on a fold line. A learner can
   fold the paper there to hide the answers; no extra blank page. */
.p-answers { margin-top: 16px; break-inside: avoid; }
.p-fold-hint {
  border-top: 1.5px dashed #999; padding-top: 4px; margin-bottom: 8px;
  color: #666; font-size: .82em;
}
.p-key { margin-top: 0; padding-top: 0; border-top: 0; }
.p-key h3 { font-size: 1em; margin-bottom: 6px; }
.p-key ol { columns: 4; padding-inline-start: 1.6em; font-size: .9em; }

/* Cut-out cards: 2 × 4 per A4 page */
.p-cards {
  display: grid; grid-template-columns: 1fr 1fr; grid-auto-rows: 64mm;
  margin-bottom: 14mm;
}
.p-card {
  display: grid; align-content: center; justify-items: center; gap: 4px; text-align: center;
  padding: 8mm; border-right: 1px dashed #999; border-bottom: 1px dashed #999;
}
/* The cutting grid is drawn by the cells, not the container, and the top
   row has no top border: a border exactly on the page break gets left
   behind as a stray dashed line above the footer. The paper edge is the
   top cut line. */
.p-card:nth-child(odd) { border-left: 1px dashed #999; }
.p-front .p-w { font-size: 20pt; line-height: 1.1; }
.p-back .p-ar { font-size: 15pt; }
.p-back .p-def { font-size: .95em; }
.p-back i { font-size: .85em; color: #444; }

/* Download PDF: render the preview exactly like paper (A4 text width = 186mm) */
.print-doc.pdf-render {
  width: 186mm; max-width: none; padding: 0; border: 0; border-radius: 0; box-shadow: none; overflow: visible;
}
.print-doc.pdf-render .no-print { display: none !important; }
.print-doc.pdf-render .ar-word { display: inline-block; margin-inline-start: .28em; }
.print-doc.pdf-render .ar-word:first-child { margin-inline-start: 0; }
.print-doc.pdf-render.layout-cards .p-head, .print-doc.pdf-render.layout-cards .p-name { display: none; }
.print-doc.pdf-render .p-cards { margin: 0; grid-auto-rows: 66mm; break-after: page; page-break-after: always; }
.print-doc.pdf-render .p-cards:last-child { break-after: auto; page-break-after: auto; }

/* ── Smaller screens: options above, preview below ───────────────── */
@media (max-width: 960px) {
  .print-page { grid-template-columns: 1fr; }
  .print-controls { position: static; max-height: none; overflow: visible; }
  .print-options { overflow: visible; }
  .print-go { position: static; }
}
@media (max-width: 640px) {
  .print-controls h1 { font-size: var(--text-xl); }
  .print-page { padding-top: var(--s-4); padding-bottom: calc(78px + var(--s-4)); }
  /* Keep the action in reach while the learner scrolls the options. The
     paper preview can be long, but Download PDF never leaves the screen. */
  .print-go {
    position: fixed; z-index: 17; left: 0; right: 0;
    bottom: calc(60px + env(safe-area-inset-bottom));
    display: flex; align-items: center; gap: var(--s-2);
    padding: 8px var(--gutter) calc(8px + env(safe-area-inset-bottom) / 2);
    background: var(--surface); border-top: 1px solid var(--line);
    box-shadow: 0 -8px 24px -20px rgb(20 27 45 / .45);
  }
  .print-go .btn { margin: 0; }
  .print-go .btn-primary { flex: 1 1 auto; min-width: 0; }
  .print-go [data-action="print-now"] { flex: 0 0 54px; width: 54px; padding: 0; }
  .print-now-label, .print-go .hint { display: none; }
}

/* ── Printing ───────────────────────────────────────────────────── */
@page { size: A4; margin: 12mm; }

@media print {
  body { background: #fff; }
  .app-bar, .no-print, .toast, .break-card, .confetti, .skip-link { display: none !important; }
  .print-page { display: block; padding: 0; max-width: none; }
  .print-preview { height: auto !important; overflow: visible; }
  .print-doc {
    width: auto; padding: 0; border: 0; box-shadow: none; border-radius: 0;
    overflow: visible; transform: none !important;
  }
  .view-enter { animation: none; }
  .p-table thead { display: table-header-group; }      /* repeat headers on every page */
  /* A hair less cell padding than the screen so a 25–30 word table fills
     its pages instead of leaving one orphan row on a third sheet. */
  .p-table td { padding: 5px 6px; }
  /* Cards: no header on paper, so every sheet holds exactly 4 rows and the
     mirrored back page lines up with its front when printed double-sided. */
  .layout-cards .p-head, .layout-cards .p-name { display: none; }
  .p-cards { break-after: page; margin: 0; grid-auto-rows: 67mm; }
  .p-cards:last-child { break-after: auto; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
