/*
 * css/paper.css (v4.0.0)
 * What it does: Styles the A4 question paper in both live preview and print, including vertical arithmetic, long division, and KaTeX spacing.
 * Talks to: Loaded by index.html; used by js/paperRenderer.js and js/mathEngine.js; paired with css/print.css.
 */

@font-face {
  font-family: 'Arimo';
  src: local('Arimo'), url('../fonts/Arimo-Regular.ttf') format('truetype');
  font-weight: 400;
  font-display: swap;
}
@font-face {
  font-family: 'Arimo';
  src: local('Arimo Bold'), url('../fonts/Arimo-Bold.ttf') format('truetype');
  font-weight: 700;
  font-display: swap;
}

/* Printed paper is ALWAYS pure black text on pure white paper */
.a4-sheet {
  --paper-bg: #FFFFFF;
  --paper-ink: #000000;
  --paper-rule: #999999;
  --paper-border: #000000;
  --paper-font-pt: 12pt;

  width: 210mm;
  min-height: 297mm;
  background: var(--paper-bg);
  color: var(--paper-ink);
  font-family: Arial, 'Arimo', 'Palanquin', 'Noto Sans Devanagari', sans-serif;
  font-size: var(--paper-font-pt);
  line-height: 1.48;
  padding: 12mm 12mm 14mm 12mm;
  position: relative;
  box-sizing: border-box;
}

.a4-sheet * {
  box-sizing: border-box;
  color: var(--paper-ink);
}

.a4-sheet .hi-run {
  font-family: 'Palanquin', 'Noto Sans Devanagari', sans-serif;
  font-size: 1.04em;
  line-height: 1.55;
}

.a4-sheet .qp-title .hi-run,
.a4-sheet .qp-sec-head .hi-run {
  font-family: 'Palanquin Dark', 'Palanquin', 'Noto Sans Devanagari', sans-serif;
  font-weight: 700;
}

/* 1. Full-width single wide header banner */
.qp-banner { width: 100%; margin-bottom: 3mm; text-align: center; }
.qp-banner img { width: 100%; height: auto; display: block; max-height: 36mm; object-fit: contain; }
.qp-banner-fallback {
  border: 1.5pt solid var(--paper-border);
  padding: 3mm;
  text-align: center;
  font-weight: 700;
  font-size: 15pt;
}

/* 2. Centered exam title */
.qp-title {
  text-align: center;
  font-weight: 700;
  font-size: 1.15em;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  margin: 2mm 0 2.5mm;
}

/* 3. Bordered 2-row metadata table */
.qp-meta-table { width: 100%; border-collapse: collapse; margin-bottom: 2.5mm; font-size: 0.96em; }
.qp-meta-table td { border: 0.9pt solid var(--paper-border); padding: 1.4mm 2.5mm; width: 50%; }
.qp-meta-table td:last-child { text-align: right; }

/* 4. Optional student line */
.qp-student-line {
  display: flex;
  justify-content: space-between;
  gap: 4mm;
  border: 0.8pt solid var(--paper-border);
  padding: 1.8mm 2.5mm;
  margin-bottom: 2.5mm;
  font-size: 0.95em;
}
.qp-student-line span { display: inline-flex; align-items: flex-end; gap: 1.5mm; flex: 1; }
.qp-student-blank { flex: 1; border-bottom: 0.7pt solid var(--paper-border); min-width: 18mm; height: 1em; }

/* 5. General instructions */
.qp-instructions { margin: 0 0 2.5mm; font-size: 0.9em; }
.qp-instructions-title { font-weight: 700; margin-bottom: 1mm; }
.qp-instructions ol { margin: 0; padding-left: 5mm; }
.qp-instructions li { margin-bottom: 0.6mm; }

/* 6. Thin rule */
.qp-divider { border: none; border-top: 0.9pt solid var(--paper-border); margin: 2mm 0 3.5mm; }

/* 7. Section heading */
.qp-section { margin-bottom: 3.5mm; }
.qp-sec-head {
  font-weight: 700;
  font-size: 1.04em;
  padding: 1.2mm 0;
  border-bottom: 0.8pt solid var(--paper-border);
  margin-bottom: 2.5mm;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  break-after: avoid;
  page-break-after: avoid;
}
.qp-sec-note { font-style: italic; font-size: 0.92em; margin: -1.5mm 0 2mm; break-after: avoid; page-break-after: avoid; }

/* 8. Question blocks */
.qp-question {
  margin-bottom: 3mm;
  break-inside: avoid;
  page-break-inside: avoid;
  orphans: 3;
  widows: 3;
}
.qp-question.allow-long-split { break-inside: auto; page-break-inside: auto; }
.qp-q-stem-wrap { break-inside: avoid; page-break-inside: avoid; break-after: avoid; page-break-after: avoid; }
.qp-q-row { display: flex; justify-content: space-between; align-items: baseline; gap: 3mm; }
.qp-q-main { flex: 1; }
.qp-q-num { font-weight: 700; margin-right: 1.5mm; }
.qp-q-marks { font-weight: 700; white-space: nowrap; }

/* Blanks & Math Boxes */
.qp-blank {
  display: inline-block;
  min-width: 24mm;
  border-bottom: 0.8pt solid var(--paper-border);
  vertical-align: baseline;
  margin: 0 1mm;
}
.math-series-blank {
  display: inline-block;
  min-width: 14mm;
  border-bottom: 0.8pt solid currentColor;
  margin: 0 2mm;
}
.math-circle-box {
  display: inline-block;
  width: 6.5mm;
  height: 6.5mm;
  border: 0.9pt solid currentColor;
  border-radius: 50%;
  vertical-align: middle;
  margin: 0 2mm;
}
.math-sq-box {
  display: inline-block;
  width: 6.5mm;
  height: 6.5mm;
  border: 0.9pt solid currentColor;
  vertical-align: middle;
  margin: 0 1.5mm;
}
.math-money { white-space: nowrap; }
.math-display-block { margin: 2mm 0; text-align: center; }

/* Unparsed math error highlight (visible in preview & editor) */
.math-error {
  background: #FCECEB;
  color: #B3261E !important;
  border: 1px dashed #B3261E;
  padding: 0 3px;
  border-radius: 3px;
  font-family: monospace;
}

/* === Vertical Arithmetic & Long Division === */
.math-v-grid {
  display: grid;
  gap: 5mm 8mm;
  margin: 2.5mm 0 2mm 6mm;
  break-inside: avoid;
  page-break-inside: avoid;
}
.math-v-grid.cols-1 { grid-template-columns: max-content; }
.math-v-grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
.math-v-grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
.math-v-grid.cols-4 { grid-template-columns: repeat(4, 1fr); }

.math-v-item {
  display: inline-flex;
  align-items: flex-start;
  gap: 2.5mm;
  break-inside: avoid;
  page-break-inside: avoid;
}
.math-v-label { font-weight: 700; padding-top: 1mm; }

.math-v-table {
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
  font-size: 1.05em;
}
.math-v-table td {
  text-align: center;
  min-width: 5.8mm;
  padding: 0.6mm 1mm;
  line-height: 1.25;
}
.math-v-table .mv-op { font-weight: 700; padding-right: 2mm; text-align: right; }
.math-v-table .mv-dot { min-width: 2.5mm; padding: 0; font-weight: 700; }
.math-v-table .mv-pv { font-size: 0.72em; font-weight: 700; border-bottom: 0.5pt solid #777777; }
.math-v-table .mv-carry span {
  display: inline-block;
  width: 4.2mm;
  height: 4.2mm;
  border: 0.5pt solid #666666;
}
.math-v-table.boxed-digits .mv-digit { border: 0.5pt solid #888888; }
.math-v-table .mv-last-operand td { border-bottom: 1pt solid currentColor; padding-bottom: 1.2mm; }
.math-v-table .mv-work-cell { height: 6.5mm; border-bottom: 0.4pt dotted #888888; }
.math-v-table .mv-total-line td { border-top: 1pt solid currentColor; height: 1mm; padding: 0; }
.math-v-table .mv-ans-row td { height: 8mm; }

/* Long division bracket */
.math-div-box {
  display: inline-block;
  font-variant-numeric: tabular-nums;
  font-size: 1.05em;
  min-width: 28mm;
}
.math-div-quotient { height: 7mm; }
.math-div-row { display: inline-flex; align-items: stretch; }
.math-div-divisor { padding: 1mm 2.5mm 1mm 0; font-weight: 600; }
.math-div-dividend {
  border-top: 1pt solid currentColor;
  border-left: 1pt solid currentColor;
  border-top-left-radius: 5px;
  padding: 1mm 3mm 1mm 2.5mm;
  letter-spacing: 0.08em;
}
.math-div-work { height: 22mm; }

/* Word box, MCQ, T/F, Match, Subparts, Images, Answer Space */
.qp-word-box {
  border: 0.8pt solid var(--paper-border);
  padding: 1.5mm 3mm;
  margin: 1.5mm 0 2mm 6mm;
  text-align: center;
  font-weight: 600;
  break-inside: avoid;
}
.qp-mcq-grid { margin: 1.5mm 0 0 6mm; display: grid; gap: 1.2mm 4mm; break-inside: avoid; page-break-inside: avoid; }
.qp-mcq-grid.cols-4 { grid-template-columns: repeat(4, 1fr); }
.qp-mcq-grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
.qp-mcq-grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
.qp-mcq-grid.cols-1 { grid-template-columns: 1fr; }
.qp-mcq-opt { display: flex; gap: 1.5mm; align-items: baseline; }
.qp-mcq-lbl { font-weight: 700; }
.qp-tf-box {
  display: inline-block;
  border: 0.8pt solid var(--paper-border);
  padding: 0.3mm 2.5mm;
  font-size: 0.88em;
  font-weight: 700;
  margin-left: 2mm;
  white-space: nowrap;
}
.qp-match-table {
  width: calc(100% - 6mm);
  margin: 2mm 0 0 6mm;
  border-collapse: collapse;
  break-inside: avoid;
  page-break-inside: avoid;
}
.qp-match-table th, .qp-match-table td { border: 0.8pt solid var(--paper-border); padding: 1.4mm 2.5mm; vertical-align: top; width: 50%; }
.qp-match-table th { text-align: center; font-weight: 700; }

.qp-match-dots { width: calc(100% - 6mm); margin: 3mm 0 0 6mm; border-collapse: collapse; break-inside: avoid; page-break-inside: avoid; }
.qp-match-dots td { padding: 3.5mm 2mm; vertical-align: middle; }
.qp-match-dots .col-left { width: 38%; }
.qp-match-dots .col-gap { width: 24%; text-align: center; }
.qp-match-dots .col-right { width: 38%; }
.qp-dot { display: inline-block; width: 2.6mm; height: 2.6mm; border-radius: 50%; background: var(--paper-ink); vertical-align: middle; }

.qp-subparts { margin: 1.5mm 0 0 6mm; }
.qp-subpart { display: flex; justify-content: space-between; gap: 3mm; margin-bottom: 1.2mm; }
.qp-subpart-lbl { font-weight: 700; margin-right: 1.5mm; }
.qp-q-img-wrap { text-align: center; margin: 2mm 0; break-inside: avoid; }
.qp-q-img-wrap img { max-width: 70mm; height: auto; display: inline-block; }

.qp-answer-lines { margin: 1.5mm 0 1mm 6mm; }
.qp-ruled-line { height: 7.5mm; border-bottom: 0.45pt solid var(--paper-rule); break-inside: avoid; }
.qp-draw-box { margin: 2mm 0 1mm 6mm; border: 0.8pt solid var(--paper-border); break-inside: avoid; page-break-inside: avoid; }
.qp-write-box { margin: 2mm 0 1mm 6mm; border: 0.8pt solid var(--paper-border); min-height: 16mm; break-inside: avoid; }

/* Preview-only guides & watermark */
.preview-page-guide { position: absolute; left: 0; right: 0; border-top: 1.5px dashed #CC3300; pointer-events: none; z-index: 5; }
.preview-page-guide span {
  position: absolute; right: 4mm; top: -10px;
  background: #FFFFFF; color: #CC3300; font-size: 9pt; font-weight: 700; padding: 0 4px;
}
.qp-preview-footer {
  margin-top: 8mm; padding-top: 2mm; border-top: 0.6pt solid var(--paper-rule);
  display: flex; justify-content: space-between; font-size: 8.5pt;
}
.qp-watermark {
  position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%) rotate(-35deg);
  font-size: 96pt; font-weight: 700; color: rgba(0, 0, 0, 0.09) !important;
  pointer-events: none; z-index: 50; letter-spacing: 0.12em; user-select: none;
}
.a4-sheet.in-preview .qp-watermark { position: absolute; top: 135mm; }

/* Print Test Page Calibration */
.cal-box-100 {
  width: 100mm; height: 100mm; border: 1pt solid #000000; margin: 6mm auto;
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 4mm;
}
.cal-ruler-h { display: flex; width: 180mm; border-top: 1pt solid #000000; border-left: 1pt solid #000000; margin: 4mm auto; }
.cal-tick { width: 10mm; height: 6mm; border-right: 1pt solid #000000; font-size: 7pt; text-align: right; padding-right: 1mm; }
.cal-margin-frame { border: 0.6pt dashed #000000; padding: 4mm; min-height: 265mm; }