/* 小学数学计算题排版工具样式：迁移自 templates/index.html。 */

:root {
  --bg: #edf3ff;
  --panel: #ffffff;
  --ink: #162033;
  --muted: #64748b;
  --line: #dbe4f0;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --accent: #f59e0b;
  --success: #059669;
  --danger: #dc2626;
  --paper-width: 210mm;
  --paper-padding: 15mm;
  --question-font: 16px;
  --question-gap: 20px;
  --question-columns: 3;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Times New Roman", "SimSun", serif;
  background: linear-gradient(180deg, #dfeaff 0, var(--bg) 260px);
  color: var(--ink);
}

.app-shell,
button,
input,
select {
  font-family: "Times New Roman", "SimSun", serif;
}

button, input, select { font: inherit; }

.app-shell {
  max-width: 1440px;
  margin: 0 auto;
  padding: 24px;
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 20px;
}

.title h1 {
  margin: 0 0 8px;
  font-size: 28px;
  letter-spacing: -0.02em;
}

.title p {
  margin: 0;
  color: var(--muted);
  line-height: 1.7;
}

.badges {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.badge {
  border: 1px solid rgba(37, 99, 235, 0.2);
  background: rgba(255, 255, 255, 0.7);
  color: #1e40af;
  padding: 7px 11px;
  border-radius: 999px;
  font-size: 13px;
  white-space: nowrap;
}

.badge-button {
  cursor: pointer;
}

.badge-button:hover {
  background: rgba(37, 99, 235, 0.12);
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 20px;
  align-items: start;
}

.panel {
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid rgba(148, 163, 184, 0.32);
  border-radius: 20px;
  box-shadow: 0 16px 45px rgba(15, 23, 42, 0.08);
}

.control-panel {
  position: static;
  padding: 20px 22px;
}

/* 上下布局下，控制面板按 section 横向铺平：每个 section 占满整行，
   内部的 2×2 输入网格在宽屏下自动展平成 1×N。 */
.control-panel form {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.control-panel .section .grid-2 {
  /* 控制面板里的填写框统一固定 4 列，
     4 项的行整齐铺满；2 项的行让第一项落在第 1 列、第二项落在第 4 列，
     这样"题量"对齐"每行题数"，"随机种子"对齐"页边距 mm"，
     宽度与位置完全一致。 */
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-auto-rows: auto;
  align-items: start;
  gap: 12px 16px;
}

.control-panel .section .grid-2 > .field {
  margin-bottom: 0;
}

/* 当一个 grid-2 里恰好只有 2 个 .field 时，第二项跳到最后一列。 */
.control-panel .section .grid-2 > .field:nth-child(1):nth-last-child(2) {
  grid-column: 1;
}
.control-panel .section .grid-2 > .field:nth-child(2):nth-last-child(1) {
  grid-column: 4;
}

@media (max-width: 980px) {
  .control-panel .section .grid-2 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .control-panel .section .grid-2 > .field:nth-child(1):nth-last-child(2) {
    grid-column: 1;
  }
  .control-panel .section .grid-2 > .field:nth-child(2):nth-last-child(1) {
    grid-column: 2;
  }
}

@media (max-width: 560px) {
  .control-panel .section .grid-2 {
    grid-template-columns: minmax(0, 1fr);
  }
  .control-panel .section .grid-2 > .field {
    grid-column: 1 !important;
  }
}

.section {
  padding: 14px 0;
  border-bottom: 1px solid var(--line);
}

.section:first-child { padding-top: 0; }
.section:last-child { border-bottom: 0; padding-bottom: 0; }

.section-title {
  margin: 0 0 12px;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.field {
  display: grid;
  gap: 7px;
  margin-bottom: 12px;
}

/* 锁定 label 行高，避免不同字符（如"页边距 mm"含拉丁字符）导致
   行内基线偏移，进而让同一行的 input 起始位置错开。 */
.field > label {
  display: block;
  line-height: 22px;
  min-height: 22px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.field:last-child { margin-bottom: 0; }

label {
  color: #334155;
  font-size: 13px;
  font-weight: 700;
}

input, select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
  color: var(--ink);
  padding: 10px 12px;
  outline: none;
}

input:focus, select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.toggle-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 11px;
  color: #334155;
  background: #fff;
  cursor: pointer;
  font-size: 13px;
  user-select: none;
}

.toggle input { width: auto; }

.actions {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 10px;
  margin-top: 14px;
}

.btn {
  border: 0;
  border-radius: 13px;
  padding: 11px 14px;
  cursor: pointer;
  font-weight: 800;
  transition: 0.18s ease;
}

.btn:disabled { opacity: 0.6; cursor: wait; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-dark { background: #111827; color: #fff; }
.btn-soft { background: #eaf1ff; color: #1e40af; }
.btn-accent { background: #fff7ed; color: #c2410c; }

.status {
  min-height: 22px;
  margin-top: 12px;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.6;
}

.status.success { color: var(--success); }
.status.error { color: var(--danger); }

.preview-panel {
  padding: 18px;
  overflow: hidden;
}

.preview-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}

.preview-toolbar h2 { margin: 0; font-size: 18px; }
.preview-toolbar .hint { color: var(--muted); font-size: 13px; }

.paper-wrap {
  overflow: auto;
  padding: 18px;
  border-radius: 16px;
  background: #d8e0ec;
  min-height: 720px;
  max-height: calc(100vh - 120px);
}

.paper {
  width: var(--paper-width);
  min-height: 297mm;
  margin: 0 auto;
  background: #fff;
  padding: var(--paper-padding);
  color: #111827;
  font-family: "Times New Roman", "SimSun", serif;
  box-shadow: 0 12px 30px rgba(15, 23, 42, 0.16);
}

.worksheet-title {
  text-align: center;
  margin-bottom: 12px;
}

.worksheet-title h2 {
  margin: 0 0 8px;
  font-size: 22px;
  font-family: "SimSun", "Times New Roman", serif;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.student-line {
  display: flex;
  justify-content: center;
  gap: 32px;
  font-size: 14px;
  margin-bottom: 18px;
}

.unit-line {
  color: #475569;
  border: 1px solid #e2e8f0;
  background: #f8fafc;
  padding: 8px 10px;
  border-radius: 8px;
  margin-bottom: 16px;
  font-size: 13px;
}

.section-heading {
  font-size: 18px;
  margin: 18px 0 16px;
  font-family: "SimSun", "Times New Roman", serif;
  font-weight: 700;
}

.question-grid {
  display: grid;
  grid-template-columns: repeat(var(--question-columns), minmax(0, 1fr));
  column-gap: 26px;
  row-gap: var(--question-gap);
  font-size: var(--question-font);
  line-height: 1.5;
}

.question-item {
  display: grid;
  grid-template-columns: 2.15em minmax(0, 1fr);
  column-gap: 0.18em;
  align-items: baseline;
  break-inside: avoid;
  min-height: 30px;
  min-width: 0;
}

.question-no {
  display: block;
  min-width: 1.8em;
  font-family: "Times New Roman", serif;
  font-weight: 600;
  text-align: right;
  color: #111827;
}

.question-body {
  display: flex;
  align-items: baseline;
  min-width: 0;
  white-space: nowrap;
  letter-spacing: -0.01em;
  font-family: "Times New Roman", "SimSun", serif;
  font-weight: 400;
  color: #111827;
  overflow: hidden;
}

.question-expr {
  flex: 0 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: clip;
}

.answer-blank {
  display: inline-block;
  flex: 1 1 2.8em;
  min-width: 2.8em;
  max-width: 4.6em;
  height: 0.85em;
  margin-left: 0.08em;
  border-bottom: 1.4px solid currentColor;
  transform: translateY(-0.05em);
}

.answer {
  display: none;
  color: #2563eb;
  font-weight: 700;
  margin-left: 4px;
}

.paper.show-answer .answer { display: inline; }
.paper.answer-only .question-body { color: #64748b; }
.paper.answer-only .answer { display: inline; color: #111827; }

.answer-sheet {
  margin-top: 22px;
  padding-top: 14px;
  border-top: 2px dashed #cbd5e1;
  display: none;
}

.paper.separate-answer .answer-sheet { display: block; }
.paper.separate-answer .question-grid .answer { display: none; }

.empty-state {
  text-align: center;
  color: var(--muted);
  padding: 80px 20px;
}

.bridge-box {
  background: #f8fafc;
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 13px;
  line-height: 1.7;
}

.bridge-box strong { color: var(--ink); }

.topic-card-list {
  display: block;
  max-height: 320px;
  overflow: auto;
  padding: 0 4px 4px 0;
  position: relative;
}

.grade-tab-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px 0 10px;
  margin-bottom: 10px;
  position: sticky;
  top: 0;
  z-index: 10;
  background: linear-gradient(180deg, #ffffff 0%, #ffffff 72%, rgba(255, 255, 255, 0.86) 100%);
  box-shadow: 0 8px 14px rgba(15, 23, 42, 0.06);
}

.grade-tab {
  flex: 0 0 auto;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #f8fafc;
  color: #334155;
  padding: 8px 12px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 800;
  white-space: nowrap;
  transition: 0.18s ease;
}

.grade-tab:hover {
  border-color: rgba(37, 99, 235, 0.35);
  color: #1d4ed8;
  background: #eff6ff;
}

.grade-tab.active {
  border-color: var(--primary);
  background: var(--primary);
  color: #fff;
  box-shadow: 0 8px 18px rgba(37, 99, 235, 0.22);
}

.grade-tab-count {
  opacity: 0.72;
  font-weight: 700;
  margin-left: 4px;
}

.grade-topic-panel {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #fff;
  overflow: hidden;
}

.grade-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: #f8fafc;
  border-bottom: 1px solid var(--line);
  font-weight: 800;
}

.grade-card-body {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  padding: 10px 12px;
}

.topic-option {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 8px;
  align-items: start;
  color: #334155;
  font-size: 13px;
  line-height: 1.45;
  cursor: pointer;
  min-height: 58px;
  padding: 8px;
  border: 1px solid #e8eef7;
  border-radius: 10px;
  background: #fbfdff;
  transition: 0.18s ease;
}

.topic-option:hover {
  border-color: rgba(37, 99, 235, 0.32);
  background: #f5f9ff;
}

.topic-option input {
  width: auto;
  margin-top: 3px;
}

.topic-option small {
  display: block;
  color: var(--muted);
  margin-top: 2px;
}

.selected-topic-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-height: 34px;
  padding: 10px;
  border: 1px dashed #cbd5e1;
  border-radius: 12px;
  background: #f8fafc;
}

.selected-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  border: 1px solid rgba(37, 99, 235, 0.22);
  background: #eff6ff;
  color: #1e40af;
  border-radius: 999px;
  padding: 6px 9px;
  font-size: 12px;
}

.selected-chip button {
  width: 18px;
  height: 18px;
  line-height: 18px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  color: #1e40af;
  background: rgba(37, 99, 235, 0.12);
  cursor: pointer;
  font-weight: 900;
}

.empty-selected {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.8;
}

/* 历史记录抽屉 */
.history-drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 360px;
  max-width: 92vw;
  background: #fff;
  box-shadow: -16px 0 40px rgba(15, 23, 42, 0.18);
  border-left: 1px solid var(--line);
  z-index: 80;
  transform: translateX(100%);
  transition: transform 0.24s ease;
  display: flex;
  flex-direction: column;
}

.history-drawer[data-open="1"] {
  transform: translateX(0);
}

.history-drawer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.history-drawer-header h3 {
  margin: 0;
  font-size: 16px;
}

.history-drawer-close {
  border: 0;
  background: transparent;
  font-size: 18px;
  cursor: pointer;
  color: var(--muted);
}

.history-drawer-body {
  flex: 1;
  overflow: auto;
  padding: 12px 16px;
}

.history-empty {
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  padding: 28px 12px;
}

.history-item {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  margin-bottom: 10px;
  background: #f8fafc;
}

.history-item-title {
  font-weight: 700;
  font-size: 14px;
  color: var(--ink);
  margin-bottom: 4px;
  word-break: break-all;
}

.history-item-meta {
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 8px;
}

.history-item-actions {
  display: flex;
  gap: 8px;
}

.history-item-actions button {
  flex: 1;
  border: 0;
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 12px;
  cursor: pointer;
  font-weight: 700;
}

.history-item-actions .restore { background: var(--primary); color: #fff; }
.history-item-actions .delete { background: #fee2e2; color: var(--danger); }

.history-mask {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.32);
  z-index: 79;
  display: none;
}

.history-mask[data-open="1"] {
  display: block;
}

@media (max-width: 980px) {
  .topbar { flex-direction: column; }
  .badges { justify-content: flex-start; }
}

@media print {
  body { background: #fff; }
  .topbar, .control-panel, .preview-toolbar, .history-drawer, .history-mask { display: none !important; }
  .app-shell { padding: 0; max-width: none; }
  .workspace { display: block; }
  .preview-panel { padding: 0; border: 0; box-shadow: none; }
  .paper-wrap { padding: 0; background: #fff; max-height: none; overflow: visible; }
  .paper {
    width: auto;
    min-height: auto;
    box-shadow: none;
    margin: 0;
    padding: 12mm;
  }
  @page { size: A4; margin: 8mm; }
}
