:root {
  --ink: #111;
  --muted: #666;
  --line: #c9c9c9;
  --paper: #ffffff;
  --desk: #f1f4f7;
  --accent: #244e91;
  --accent-2: #0a7c66;
  --danger: #a33636;
  --panel: #ffffff;
  --shadow: 0 14px 40px rgba(30, 42, 68, .14);
  --font-body: "PingFang TC", "Microsoft JhengHei", "Noto Sans TC", "Heiti TC", Arial, sans-serif;
  --font-serif: "PMingLiU", "MingLiU", "Songti TC", serif;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  color: var(--ink);
  font-family: var(--font-body);
  background: var(--desk);
}
button, input, select, textarea { font-family: inherit; }
button {
  cursor: pointer;
  border: 1px solid #b8c3d7;
  background: #fff;
  padding: 8px 12px;
  border-radius: 10px;
  color: #14325f;
  font-weight: 600;
}
button:hover { border-color: var(--accent); box-shadow: 0 2px 8px rgba(36,78,145,.12); }
button:active { transform: translateY(1px); }
pre { white-space: pre-wrap; word-break: break-word; }
#appShell { display: flex; min-height: 100vh; }
.control-panel {
  width: 410px;
  max-width: 100vw;
  background: var(--panel);
  border-right: 1px solid #dfe4ee;
  padding: 16px;
  overflow-y: auto;
  height: 100vh;
  position: sticky;
  top: 0;
}
.app-header { display: flex; gap: 12px; align-items: center; margin-bottom: 14px; }
.app-header .app-title { flex: 1; min-width: 0; }
.app-header .preview-toggle { flex-shrink: 0; align-self: flex-start; }
.logo-mark {
  width: 54px; height: 54px; border-radius: 16px;
  background: linear-gradient(135deg, #1b6ec2, #2ba77a);
  color: #fff; display: grid; place-items: center;
  font-weight: 900; letter-spacing: 1px; box-shadow: var(--shadow);
  flex-shrink: 0;
}
.app-header h1 { margin: 0; font-size: 20px; }
.app-header p { margin: 3px 0 0; color: var(--muted); font-size: 12px; }
details { border: 1px solid #e0e5ef; border-radius: 14px; padding: 10px 12px; margin: 10px 0; background: #fbfcff; }
summary { cursor: pointer; font-weight: 800; color: #203a63; margin-bottom: 8px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: #4b5a73; font-weight: 700; min-width: 0; }
input, select, textarea {
  border: 1px solid #ccd5e5;
  border-radius: 9px;
  padding: 7px 8px;
  background: #fff;
  font-size: 13px;
  color: #1d2430;
  width: 100%;
  min-width: 0;
  max-width: 100%;
  box-sizing: border-box;
}
input[readonly] { background: #f4f6fa; color: #5a6478; cursor: not-allowed; }
textarea { width: 100%; resize: vertical; line-height: 1.45; }
.grid { display: grid; gap: 8px; }
.grid.two { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
.grid .wide { grid-column: 1 / -1; }
.button-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.button-row.prominent button { background: #14325f; color: #fff; border-color: #14325f; }
.button-row.prominent button:first-child { background: var(--accent-2); border-color: var(--accent-2); }
.small-note { font-size: 12px; color: var(--muted); line-height: 1.45; margin: 6px 0 8px; }
.topic-list { display: grid; gap: 6px; margin-bottom: 8px; }
.topic-pill {
  display: grid; grid-template-columns: 20px 1fr auto; gap: 8px; align-items: start;
  background: #fff; border: 1px solid #dfe6f2; border-radius: 10px; padding: 8px;
}
.topic-pill strong { color: #122d55; }
.topic-pill span { display: block; color: var(--muted); font-size: 11px; margin-top: 2px; }
.topic-pill em { font-style: normal; color: #fff; background: #60769b; border-radius: 999px; padding: 2px 7px; font-size: 11px; }
.range-line { display: grid; gap: 7px; grid-template-columns: 1fr 1fr 1fr; align-items: end; margin-top: 10px; }
.range-line > span { grid-column: 1 / -1; font-weight: 800; color: #4b5a73; font-size: 12px; }
.check-row { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 10px; }
.check-row label { flex-direction: row; align-items: center; font-weight: 600; }
.stats-panel { background: #fff; border-radius: 10px; border: 1px solid #e5eaf2; padding: 10px; font-size: 12px; color: #233; line-height: 1.55; }
.stats-panel h4 { margin: 8px 0 4px; font-size: 12px; color: #14325f; font-weight: 800; letter-spacing: 0.5px; }
.stats-panel h4:first-child { margin-top: 0; }
.qc-row { display: flex; align-items: center; gap: 6px; padding: 3px 0; border-bottom: 1px dashed #eef0f4; }
.qc-row:last-child { border-bottom: none; }
.qc-status { display: inline-flex; align-items: center; justify-content: center; min-width: 18px; height: 18px; border-radius: 50%; font-size: 11px; font-weight: 900; flex-shrink: 0; }
.qc-pass { background: #d8f4e2; color: #1c7a3e; }
.qc-fail { background: #fde2e2; color: #a33636; }
.qc-warn { background: #fff1cc; color: #946700; }
.qc-fixed { background: #ddeafe; color: #1d4ed8; }
.qc-label { flex: 1; }
.qc-detail { color: var(--muted); font-size: 11px; }
.qc-summary { padding: 6px 8px; border-radius: 8px; margin-bottom: 8px; font-weight: 700; }
.qc-summary.ok { background: #ebf9f0; color: #1c7a3e; }
.qc-summary.fix { background: #eaf2ff; color: #1d4ed8; }
.qc-summary.bad { background: #fdecec; color: #a33636; }
.topic-distribution { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; font-size: 11px; }
.topic-distribution > .label { color: #233; }
.topic-distribution > .value { color: var(--muted); font-variant-numeric: tabular-nums; }
.workspace { flex: 1; overflow: auto; height: 100vh; min-width: 0; }
.toolbar { position: sticky; top: 0; z-index: 10; background: rgba(241,244,247,.94); backdrop-filter: blur(8px); padding: 10px 18px; border-bottom: 1px solid #dfe4ee; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.toolbar-spacer { flex: 1; }
.preview-toggle {
  background: linear-gradient(135deg, #1b6ec2, #2ba77a);
  color: #fff;
  border-color: transparent;
  font-weight: 700;
  padding: 8px 16px;
  box-shadow: 0 2px 8px rgba(36,78,145,.18);
  white-space: nowrap;
  word-break: keep-all;
  flex-shrink: 0;
  min-width: 64px;
}
.preview-toggle:hover { box-shadow: 0 4px 14px rgba(36,78,145,.28); }
.preview-toggle.active { background: linear-gradient(135deg, #a33636, #d97706); }
.preview-area { transform-origin: top center; padding: 22px 0 80px; }

/* ===== Preview Mode: hide control panel, paper auto-fit screen ===== */
body.preview-mode .control-panel { display: none; }
body.preview-mode .workspace { width: 100%; }
body.preview-mode .preview-area { padding: 14px 0 60px; transform: none !important; }
body.preview-mode .paper-stack { padding: 0 12px; }
body.preview-mode .paper {
  width: 100%;
  max-width: 210mm;
  min-height: 0;
  height: auto;
  padding: 14mm 10mm 12mm;
  box-shadow: 0 6px 22px rgba(34,45,72,.16);
}
.empty-state { width: min(680px, calc(100% - 40px)); margin: 80px auto; background: #fff; border: 1px dashed #b7c3d6; border-radius: 20px; padding: 50px; text-align: center; color: #60708a; }
.modal { position: fixed; inset: 0; background: rgba(15, 23, 42, .45); display: grid; place-items: center; z-index: 99; }
.modal[hidden] { display: none !important; }
.modal-card { width: min(900px, 92vw); max-height: 86vh; overflow: auto; background: #fff; border-radius: 18px; padding: 24px; box-shadow: var(--shadow); position: relative; }
.modal-close { position: absolute; right: 14px; top: 14px; border-radius: 99px; font-size: 20px; padding: 3px 10px; }

.paper-stack { display: grid; gap: 24px; justify-content: center; }
.paper {
  width: 210mm;
  min-height: 297mm;
  box-sizing: border-box;
  background: var(--paper);
  margin: 0 auto;
  padding: 18mm 16mm 15mm;
  box-shadow: 0 0 0 1px rgba(0,0,0,.05), 0 18px 42px rgba(34,45,72,.18);
  position: relative;
  display: flex;
  flex-direction: column;
  font-family: var(--font-serif);
  font-size: 13.5pt;
  line-height: 1.38;
}
.paper-content {
  flex: 1;
  display: flex;
  flex-direction: column;
}
.paper.cream { background: #fffdf7; }
.exam-header { text-align: center; border-bottom: 2px solid #111; padding-bottom: 8px; margin-bottom: 8px; }
.exam-header .school { font-size: 17pt; font-weight: 800; letter-spacing: 1px; }
.exam-header .title { font-size: 20pt; font-weight: 900; margin-top: 2px; }
.exam-header .meta { font-family: var(--font-body); font-size: 10.5pt; color: #111; display: flex; justify-content: center; gap: 12px; margin-top: 5px; }
.student-row { display: grid; grid-template-columns: 1fr 1fr 1fr auto; gap: 10px; align-items: end; font-family: var(--font-body); font-size: 10.5pt; margin: 10px 0 8px; }
.student-field { border-bottom: 1px solid #111; min-height: 24px; display: flex; align-items: flex-end; }
.seat-boxes { display: flex; gap: 3px; }
.seat-boxes span { width: 18px; height: 24px; border: 1px solid #111; display: inline-block; }
.instructions { border: 1px solid #111; padding: 6px 9px; margin: 8px 0 12px; font-family: var(--font-body); font-size: 10.5pt; line-height: 1.45; }
.instructions ol { margin: 4px 0 0 18px; padding: 0; }
.section-title {
  font-family: var(--font-body);
  font-size: 12pt;
  font-weight: 900;
  border-top: 2px solid #111;
  border-bottom: 1px solid #111;
  padding: 4px 2px;
  margin: 18px 0 10px;
  display: flex; justify-content: space-between; align-items: center;
  break-after: avoid-page;
  page-break-after: avoid;
  break-inside: avoid;
  page-break-inside: avoid;
}
.exam-section { break-inside: auto; }
.exam-header { break-inside: avoid; break-after: avoid-page; page-break-after: avoid; }
.instructions { break-inside: avoid; break-after: avoid-page; }
.student-row { break-inside: avoid; }
.q { position: relative; margin: 12px 0 14px; break-inside: avoid; page-break-inside: avoid; }
.q-stem { padding-right: 42px; }
.qno { font-weight: 800; margin-right: 3px; }
.mark { position: absolute; right: 0; top: 0; font-family: var(--font-body); font-size: 10pt; color: #333; }
.options { display: grid; grid-template-columns: 1fr 1fr; gap: 5px 18px; margin: 6px 0 4px 24px; }
.options div { min-height: 22px; }
.answer-blank { display: inline-block; border-bottom: 1px solid #111; min-width: 118px; height: 20px; vertical-align: bottom; }
.long-lines { margin: 8px 0 4px 22px; }
.line { display: block; border-bottom: 1px solid #111; height: 35px; }
.tiny-lines .line { height: 28px; }
.tf-grid { margin-left: 22px; }
.match-table, .rubric-table { width: 100%; border-collapse: collapse; margin: 6px 0; font-family: var(--font-body); font-size: 10.5pt; }
.match-table th, .match-table td, .rubric-table th, .rubric-table td { border: 1px solid #111; padding: 5px 6px; vertical-align: top; }
.match-table th, .rubric-table th { background: #f2f2f2; }
.source-box { border: 1px solid #111; padding: 8px 10px; margin: 6px 0 8px; background: rgba(0,0,0,.02); }
.source-title { text-align: center; font-weight: 900; font-family: var(--font-body); margin-bottom: 4px; }
.teacher-tag { font-family: var(--font-body); color: #6d4f00; background: #fff1bf; border: 1px solid #e0c15a; border-radius: 6px; padding: 2px 5px; font-size: 9pt; margin-left: 6px; }
.footer { margin-top: 15px; border-top: 1px solid #111; color: #333; display: flex; justify-content: space-between; font-family: var(--font-body); font-size: 10pt; padding-top: 4px; }
.answer-page { font-family: var(--font-body); font-size: 10.5pt; }
.answer-page h2 { margin: 0 0 4px; font-size: 16pt; text-align: center; }
.answer-subtitle { text-align: center; color: #555; font-size: 10pt; margin-bottom: 10px; padding-bottom: 6px; border-bottom: 1px solid #aaa; }
.answer-list { columns: 2; column-gap: 30px; }
.answer-item { break-inside: avoid; margin-bottom: 5px; }
.badge { display: inline-block; border: 1px solid #8fa1c0; border-radius: 999px; padding: 1px 7px; font-size: 9pt; color: #234; margin-left: 4px; font-family: var(--font-body); }
.variant-break { break-after: page; page-break-after: always; }
.quality-report { font-family: var(--font-body); font-size: 9.5pt; color: #333; border: 1px solid #bbb; padding: 7px; margin-top: 9px; }

@media print {
  html, body { background: #fff; margin: 0; padding: 0; }
  .no-print, .control-panel, .toolbar, .empty-state, .modal { display: none !important; }
  #appShell { display: block; }
  .workspace { overflow: visible; height: auto; display: block; }
  .preview-area { padding: 0; transform: none !important; }
  .paper-stack { display: block; }
  .paper {
    width: auto;
    min-height: 0;
    height: auto;
    box-sizing: border-box;
    box-shadow: none;
    margin: 0;
    padding: 0;
    display: block;
    page-break-after: always;
    break-after: page;
    position: relative;
    background: #fff;
  }
  .paper.cream { background: #fff; }
  .paper:last-child { page-break-after: auto; break-after: auto; }
  .paper-content { display: block; }
  .footer { display: none; }
  @page {
    size: A4;
    margin: 18mm 16mm 18mm;
    @bottom-center {
      content: "第 " counter(page) " 頁，共 " counter(pages) " 頁";
      font-family: "PingFang TC", "Microsoft JhengHei", "Noto Sans TC", sans-serif;
      font-size: 9pt;
      color: #333;
    }
  }
  .teacher-tag { display: none; }
}

/* ===== Mobile / tablet responsive (≤ 900px) ===== */
@media (max-width: 900px) {
  #appShell { flex-direction: column; }
  .control-panel {
    width: 100%;
    max-width: 100%;
    height: auto;
    max-height: 60vh;
    position: relative;
    border-right: none;
    border-bottom: 1px solid #dfe4ee;
  }
  .workspace { height: auto; min-height: 60vh; }
  .toolbar { padding: 8px 12px; }
  .preview-toggle { font-size: 13px; padding: 7px 10px; }

  .paper {
    width: 100%;
    max-width: 210mm;
    min-height: 0;
    height: auto;
    padding: 12mm 8mm 10mm;
    margin: 0 auto;
    font-size: 12pt;
  }
  .paper-stack { padding: 0 10px; gap: 16px; }

  .grid.two { grid-template-columns: 1fr; }
  .check-row { grid-template-columns: 1fr; }
  .range-line { grid-template-columns: 1fr 1fr 1fr; }
  .options { grid-template-columns: 1fr; }
  .student-row { grid-template-columns: 1fr 1fr; }
  .answer-list { columns: 1; }
}

/* ===== Small phone (≤ 480px) ===== */
@media (max-width: 480px) {
  .paper { padding: 10mm 6mm 8mm; font-size: 11.5pt; }
  .exam-header .school { font-size: 14pt; }
  .exam-header .title { font-size: 16pt; }
  .toolbar { padding: 6px 8px; gap: 4px; }
  .toolbar button { padding: 6px 8px; font-size: 12px; }
  body.preview-mode .paper { padding: 8mm 5mm 7mm; font-size: 11pt; }
}
