/* ============ 练习纸排版（预览即打印效果） ============ */

.sheet {
  width: 210mm;
  min-height: 297mm;
  flex: none;
  padding: 15mm;
  display: flex;
  flex-direction: column;
  background: #fff;
  color: #000;
  font-family: "Helvetica Neue", Helvetica, Arial, "PingFang SC",
    "Microsoft YaHei", sans-serif;
  font-variant-numeric: tabular-nums;
  box-shadow: 0 1px 2px rgba(23, 32, 51, 0.08), 0 14px 36px -10px rgba(23, 32, 51, 0.22);
}

body.landscape .sheet {
  width: 297mm;
  min-height: 210mm;
}

/* ---------- 页眉 ---------- */

.sheet-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 8mm;
  padding-bottom: 3mm;
  margin-bottom: 6mm;
  border-bottom: 2px solid #000;
}

.sheet-title {
  margin: 0;
  font-size: 21px;
  font-weight: 700;
  letter-spacing: 2px;
  white-space: nowrap;
}

.sheet-meta {
  font-size: 13px;
  white-space: nowrap;
}

.sheet-meta .line {
  display: inline-block;
  height: 1em;
  margin: 0 1px;
  border-bottom: 1px solid #000;
  vertical-align: -2px;
}

.sheet-meta .w-s { width: 9mm; }
.sheet-meta .w-m { width: 14mm; }
.sheet-meta .w-l { width: 20mm; }
.sheet-meta .gap { display: inline-block; width: 5mm; }

/* ---------- 题目网格：先横后竖，行高 15mm 留足手写空间 ---------- */

/* 行高由 render.js 按当页题数内联给定（grid-template-rows，15~24mm），这里的 15mm 只是兜底 */
.sheet-grid {
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  grid-auto-rows: 15mm;
  align-content: start;
  column-gap: 5mm;
}

/* 每 10 题一组的分隔虚线：落在 0 高的网格行里，即两行题目的交界处 */
.group-rule {
  grid-column: 1 / -1;
  align-self: start;
  border-top: 1.5px dashed #777;
}

.q {
  display: flex;
  align-items: center;
  gap: 2mm;
  overflow: hidden; /* 兜底：极端参数下缩到最小字号仍放不下时，不侵入邻格 */
  break-inside: avoid;
  page-break-inside: avoid;
}

.q-num {
  flex: none;
  min-width: 6mm; /* (51)~(100) 比圆圈数字宽，用 min-width 防止挤压换行 */
  white-space: nowrap;
  font-size: 13px;
}

.q-body {
  min-width: 0;
}

/* 答案页的凑十/破十提示：算式下方一行小灰字，供家长照着讲 */
.q.has-hint {
  align-items: flex-start;
  padding-top: 1mm;
}

.q.has-hint .q-body {
  display: flex;
  flex-direction: column;
}

.q-hint {
  margin-top: 0.5mm;
  font-size: 11px;
  line-height: 1.3;
  color: #666;
}

.q-expr {
  font-size: 24px; /* ≈ 18pt，适合低年级 */
  letter-spacing: 0.5px;
  white-space: nowrap;
}

/* 缩到最小字号仍放不下时由 fitSheet 打上：改为折行，绝不裁切算式与答案 */
.q.q-wrap {
  align-items: flex-start;
  padding-top: 1mm;
}

.q.q-wrap .q-expr {
  white-space: normal;
  line-height: 1.35;
}

.sheet[data-cols="4"] .q-expr {
  font-size: 19px;
}

.q-expr .op {
  padding: 0 0.06em;
}

/* 挖空下划线 */
.q-expr .blank {
  display: inline-block;
  width: 1.8em;
  height: 1em;
  border-bottom: 2px solid #000;
  vertical-align: -3px;
}

.q-expr .blank-ans {
  width: 2.6em;
}

/* 答案页里被挖的值 */
.q-expr .filled {
  font-weight: 700;
}

/* ---------- 页脚页码 ---------- */

.sheet-foot {
  position: relative;
  margin-top: auto;
  padding-top: 3mm;
  text-align: center;
  font-size: 11px;
}

/* 纸上的落款：纸会被带到学校、被别的家长看到。页码居中不动，落款靠右、浅灰小字 */
.sheet-foot::after {
  content: "纸和笔 · 一页纸，五分钟";
  position: absolute;
  right: 0;
  bottom: 0;
  font-size: 9px;
  letter-spacing: 0.5px;
  color: #9a9a9a;
}

/* ============ 打印样式 ============ */

@media print {
  body {
    margin: 0;
    background: #fff !important;
    background-image: none !important;
  }

  /* 只出纸：外壳上的东西一律不上纸（视频、识字图例这类板块自己的屏幕件也在这里一并收掉） */
  .rail,
  .work-head,
  .panel,
  .topic-nav,
  .notice,
  .confirm-bar,
  .mark-legend,
  .video-box,
  .account-dialog,
  .zhizhi-play-btn,
  .zhizhi-play-dialog {
    display: none !important;
  }

  /*
   * 屏幕上骨架是「一屏装下、纸张区自己滚」（.app 定高 100vh + overflow:hidden），
   * 打印时必须整个还原成普通文档流 —— 否则只印得出第一屏那一页。
   */
  .app,
  .work,
  .work-body {
    display: block;
    height: auto;
    overflow: visible;
  }

  .preview {
    padding: 0;
    overflow: visible;
    background: none;
  }

  #preview {
    display: block;
    zoom: 1 !important; /* 还原窄屏预览缩放，打印始终 1:1 */
  }

  /* 页面尺寸由 @page 控制（见 index.html 中 #page-style，随纸张方向切换）。
     不给 .sheet 定高（定高差 1mm 就会把页脚挤到下一页），改为固定题目网格的
     最小高度 = 每页行数 × 15mm 行高，页脚紧随其后 —— 总高度确定且小于内容区。 */
  .sheet {
    width: auto;
    min-height: 0;
    height: auto;
    padding: 0;
    margin: 0;
    box-shadow: none;
    break-after: page;
    page-break-after: always;
    /* 答案页的完成图/路线靠背景色区分（数织黑格、迷宫路线灰），
       浏览器默认不印背景——这里显式要求印出来 */
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  /* 注意：必须连 min-height 一起覆盖 —— 屏幕规则 body.landscape .sheet
     的优先级高于此处的 .sheet，否则打印时纸张仍被撑到 210mm 高。 */
  body.landscape .sheet {
    width: auto;
    min-height: 0;
    height: auto;
  }

  .sheet-grid {
    min-height: 225mm; /* 纵向 15 行 × 15mm（与 render.js 的 ROWS 对应） */
  }

  body.landscape .sheet-grid {
    min-height: 135mm; /* 横向 9 行 × 15mm */
  }

  .sheet:last-child {
    break-after: auto;
    page-break-after: auto;
  }
}

/* ---------- 纸角二维码（扫码登记，js/sheets.js 插进第一张题目页的页眉末尾） ---------- */
.sheet-qr {
  display: flex;
  align-items: flex-end;
  gap: 1.5mm;
  flex: none;
  margin-left: -2mm; /* 贴近右侧的得分栏 */
}

.sheet-qr svg {
  display: block;
  width: 13mm; /* 11mm 时 150dpi 栅格偶尔解不出；13mm 每格约 0.45mm，手机近拍稳 */
  height: 13mm;
}

.sheet-qr small {
  font-size: 8px;
  line-height: 1.25;
  color: #000;
  text-align: right;
  white-space: nowrap;
}

/* 页脚左下角的二维码：页脚撑高到二维码的高度，页码与落款贴底，二维码贴左下 */
.sheet-foot.has-qr {
  min-height: 13mm;
  padding-top: 2mm;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.sheet-qr-foot {
  position: absolute;
  left: 0;
  bottom: 0;
  margin: 0;
  align-items: flex-end;
}

.sheet-qr-foot small {
  text-align: left;
}

/* 有二维码的页眉省一点横向空间，长标题（如「火柴棒算式 · 第 1 关 拿走一根」）不至于折行 */
.sheet-head:has(.sheet-qr) {
  gap: 5mm;
}
.sheet-head:has(.sheet-qr) .sheet-meta .gap {
  width: 3mm;
}
.sheet-head:has(.sheet-qr) .sheet-meta .w-l {
  width: 16mm;
}
