/* 문제지(A4) 한 장의 모양과 인쇄 규칙 */
:root {
  --paper-w: 210mm;
  --paper-h: 297mm;
  --ink: #1c2430;
  --ink-soft: #48566b;
  --line: #cfd6df;
  --accent: #2f6f5e;
}

.sheet {
  width: var(--paper-w);
  height: var(--paper-h);
  box-sizing: border-box;
  padding: 14mm 13mm 9mm;
  background: #fff;
  color: var(--ink);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  font-feature-settings: "tnum";
}

/* ── 머리말 ───────────────────────────────────────── */
.sheet-head { flex: 0 0 auto; margin-bottom: 4.5mm; }
.sheet-kicker {
  margin: 0;
  font-size: 8pt;
  letter-spacing: .02em;
  color: #7b8798;
}
.sheet-title {
  margin: 1mm 0 0;
  font-size: 16pt;
  font-weight: 700;
  letter-spacing: -.01em;
  line-height: 1.25;
}
.sheet-sub {
  margin: 1.2mm 0 0;
  font-size: 9pt;
  color: var(--ink-soft);
}
.sheet-fields {
  display: flex;
  align-items: flex-end;
  gap: 5mm;
  margin-top: 3mm;
  padding-bottom: 1.5mm;
  border-bottom: 1px solid var(--line);
  font-size: 9.5pt;
  color: var(--ink-soft);
}
.sheet-fields .field { display: inline-flex; align-items: flex-end; gap: 2mm; white-space: nowrap; }
.sheet-fields .field i { display: inline-block; width: 26mm; border-bottom: 1px dotted #9aa6b5; height: 3.4mm; }
.sheet-fields .field.short i { width: 12mm; }
.sheet-fields .field.score { margin-left: auto; }
.sheet-fields .field.score i { width: 12mm; }

/* ── 문제 배치 ─────────────────────────────────────── */
.sheet-body { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }
.problem-group { display: flex; flex-direction: column; min-height: 0; flex: 0 0 auto; }
.problem-group + .problem-group { margin-top: 4mm; }
.problem-group:last-child { flex: 1 1 auto; }
.group-title {
  margin: 1mm 0 2mm;
  font-size: 9.5pt;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: .04em;
}
.problem-grid {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 2mm 6mm;
  flex: 0 0 auto;
  align-content: space-between;
}
.problem-group:last-child .problem-grid { flex: 1 1 auto; }
.problem-grid.is-vertical { align-content: space-between; row-gap: 10mm; padding: 2mm 0; }
/* 단원평가는 여러 묶음이 한 장에 들어가므로 위에서부터 차례로 쌓는다. */
.sheet--multi .problem-grid { align-content: start; row-gap: 6mm; }
.problem-grid.is-vertical .problem { padding: 0; }
.problem-grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.problem-grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.problem-grid.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.problem-grid.cols-5 { grid-template-columns: repeat(5, minmax(0, 1fr)); }

.problem {
  display: flex;
  gap: 1.6mm;
  min-width: 0;
  padding: 1.2mm 0;
  line-height: 1.45;
  break-inside: avoid;
}
.problem-no {
  flex: 0 0 auto;
  min-width: 4.2mm;
  font-size: 9pt;
  font-weight: 700;
  color: var(--accent);
  text-align: right;
  padding-top: .6mm;
}
.problem-body { min-width: 0; flex: 1 1 auto; }
.stem-line {
  margin: 0;
  font-size: 12.5pt;
  line-height: 1.5;
  word-break: keep-all;
  overflow-wrap: anywhere;
}
.problem-grid.cols-4 .stem-line { font-size: 11.5pt; }
.problem-grid.cols-5 .stem-line { font-size: 10.5pt; }
.problem[data-kind="문장제"] .stem-line { font-size: 10.5pt; line-height: 1.55; }
.problem-grid.cols-2 .stem-line { font-size: 11pt; }

/* 빈칸·답란 */
.blank {
  display: inline-block;
  min-width: 16mm;
  height: 4mm;
  border-bottom: 1px solid #6f7c8c;
  vertical-align: baseline;
}
.answer-area {
  display: flex;
  align-items: flex-end;
  gap: 1.5mm;
  margin-top: 1.6mm;
  font-size: 9.5pt;
  color: var(--ink-soft);
}
.answer-area .blank { min-width: 22mm; }
.answer-label { flex: 0 0 auto; }
.working-label { flex: 0 0 auto; }
.working-line { flex: 1 1 auto; border-bottom: 1px dotted #9aa6b5; height: 4mm; }
.answer-area.has-working { margin-top: 1.2mm; }

/* ── 분수 표기 ─────────────────────────────────────── */
.frac {
  display: inline-flex;
  flex-direction: column;
  align-items: stretch;
  vertical-align: middle;
  margin: 0 .16em;
  font-size: .8em;
  line-height: 1.12;
  text-align: center;
}
.frac-num { padding: 0 .22em; display: block; }
.frac-den { border-top: 1.1px solid currentColor; padding: 0 .22em; display: block; }
.mixed { display: inline-flex; align-items: center; vertical-align: middle; }
.mixed-whole { margin-right: .06em; line-height: 1; }
.answer { font-weight: 700; }
.answer-line { margin: .6mm 0 0; font-size: 10pt; }

/* ── 세로셈 ───────────────────────────────────────── */
.problem--vertical { padding: 1.6mm 0 2mm; }
.problem--vertical .problem-body { display: flex; justify-content: flex-end; padding-right: 2mm; }
.vert {
  display: grid;
  grid-template-columns: 1.3em repeat(var(--cols, 3), 1.1ch);
  font-size: 12.5pt;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0;
  line-height: 1.32;
  justify-items: end;
  align-items: baseline;
}
.v-row { display: contents; }
.v-op { justify-self: start; color: var(--ink); }
.dg { min-width: 1.05em; text-align: right; }
.dg.dot { text-align: center; }
.v-rule {
  grid-column: 1 / -1;
  justify-self: stretch;
  border-top: 1.6px solid var(--ink);
  margin: .35em 0 .35em;
  height: 0;
}
.v-space { grid-column: 1 / -1; height: 13mm; }
.v-answer { color: var(--accent); font-weight: 700; }

/* 나눗셈 세로셈 */
.vdiv { font-size: 12pt; font-variant-numeric: tabular-nums; display: inline-block; }
.vdiv-quot { min-height: 1.35em; text-align: right; padding-right: .55em; color: var(--accent); font-weight: 700; }
.vdiv-line { display: flex; align-items: stretch; }
.vdiv-divisor { padding-right: .25em; }
.vdiv-mark { transform: scaleX(.8); padding-right: .12em; }
.vdiv-dividend { border-top: 1.8px solid var(--ink); padding: .1em .5em 0 .45em; }

/* ── 정답 및 풀이 ──────────────────────────────────── */
.answer-grid {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 3mm 8mm;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
.answer-grid.cols-1 { grid-template-columns: minmax(0, 1fr); }
.answer-grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.answer-item { display: flex; gap: 2mm; border-top: 1px solid #eef1f4; padding-top: 1.6mm; }
.answer-no {
  flex: 0 0 auto;
  min-width: 5mm;
  font-weight: 700;
  font-size: 9.5pt;
  color: var(--accent);
}
.answer-content { min-width: 0; }
.answer-value { margin: 0; font-size: 11pt; font-weight: 700; }
.answer-solution { margin: .6mm 0 0; font-size: 8.5pt; line-height: 1.5; color: var(--ink-soft); word-break: keep-all; }

/* ── 꼬리말 ───────────────────────────────────────── */
.sheet-foot {
  flex: 0 0 auto;
  display: flex;
  justify-content: space-between;
  margin-top: 3mm;
  padding-top: 1.5mm;
  border-top: 1px solid var(--line);
  font-size: 7.5pt;
  color: #8b96a5;
}

/* ── 인쇄 ─────────────────────────────────────────── */
@media print {
  @page { size: A4 portrait; margin: 0; }
  html, body { margin: 0; padding: 0; background: #fff; height: auto !important; overflow: visible !important; }
  body.page-generator { height: auto !important; overflow: visible !important; }
  .app-header, .app-footer, .quick-guide, .grade-bar, .preset-bar,
  .control-rail, .preview-toolbar, .status, .screen-only { display: none !important; }
  .preview-modal { display: none !important; }
  .gen-page, .gen-shell, .app-workspace, .preview-rail, .preview-stage,
  .preview-canvas, .preview-frame {
    display: block !important; margin: 0 !important; padding: 0 !important;
    transform: none !important; width: auto !important; height: auto !important;
    overflow: visible !important; border: 0 !important; border-radius: 0 !important;
    box-shadow: none !important; background: #fff !important; min-height: 0 !important;
  }
  #preview {
    position: static !important; transform: none !important;
    width: auto !important; height: auto !important;
  }
  .preview-empty { display: none !important; }
  .sheet {
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
    width: 210mm;
    height: 297mm;
    box-shadow: none !important;
    border: 0 !important;
    border-radius: 0 !important;
    margin: 0 !important;
    break-after: page;
    page-break-after: always;
    break-inside: avoid;
  }
  .sheet:last-child { break-after: auto; page-break-after: auto; }
  .sheet--answer { break-before: page; }
  /* 정답지만 인쇄 */
  body.print-answers .sheet:not(.sheet--answer) { display: none !important; }
  body.print-answers .sheet--answer { break-before: auto; page-break-before: auto; }
}
/* 1~2학년은 글자를 조금 크게 */
.sheet[data-grade="1"] .stem-line { font-size: 13.5pt; }
.sheet[data-grade="1"] .problem[data-kind="문장제"] .stem-line { font-size: 11.5pt; }
.sheet[data-grade="2"] .stem-line { font-size: 12.8pt; }
.sheet[data-type="문장제"][data-grade="1"] .stem-line { font-size: 12pt; line-height: 1.6; }

/* ── 분수(上下 구조) ───────────────────────────────── */
.frac { display:inline-flex; flex-direction:column; vertical-align:middle;
        text-align:center; line-height:1.05; font-size:.92em; margin:0 2px; }
.frac .n { border-bottom:1.5px solid currentColor; padding:0 4px; }
.frac .d { padding:0 4px; }
