:root {
  --bg: #f4f5f7;
  --panel-bg: #ffffff;
  --border: #dcdfe4;
  --text: #1c2530;
  --muted: #6b7480;
  --accent: #2f6df6;

  --cell: 15mm;
  --step-cell: 6mm; /* stroke-order guide: kept small so it does not distract */
  --cell-border: #d3d7dd;
  --cell-border-model: #2b323c;
  --model-ink: #14181d;
  --faint-ink: #c6cad0;

  --hanzi-font: "LXGW WenKai", "Kaiti SC", KaiTi, STKaiti, serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Noto Sans SC", -apple-system, BlinkMacSystemFont, "Segoe UI",
    Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ---------- App layout: panel + preview as one centred group ---------- */
.app {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 22px;
  height: 100vh;
  overflow: hidden;
  padding: 14px;
}

.panel {
  flex: 0 0 360px;
  padding: 22px;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  max-height: 100%;
  overflow-y: auto;
}

.panel h1 {
  margin: 0 0 16px;
  font-size: 20px;
  font-weight: 600;
}

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field > span { font-size: 13px; font-weight: 500; color: var(--muted); }

textarea,
input[type="text"],
input[type="number"] {
  width: 100%;
  padding: 9px 11px;
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  outline: none;
}

textarea { resize: vertical; line-height: 1.7; min-height: 120px; }

textarea:focus,
input:focus { border-color: var(--accent); }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }

.field-inline {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.field-inline > span { white-space: nowrap; }
.field-inline input { width: 72px; text-align: center; }

.opts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px 16px;
  margin-bottom: 20px;
}

.check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  margin-bottom: 0;
  white-space: nowrap;
}
.check input { width: 16px; height: 16px; flex: none; }

.btn {
  margin-top: 6px;
  padding: 10px 22px;
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border: 0;
  border-radius: 8px;
  cursor: pointer;
}
.btn:hover { background: #245ad4; }

/* ---------- Preview: scaled A4 page (also the print target) ---------- */
.preview {
  --fit: 1;
  flex: 1 1 0;
  min-width: 0;
  max-width: 800px;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* fills the space above pager / note; A4 is scaled to fit inside here */
.page-viewport {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.page-frame {
  flex: none;
  width: calc(210mm * var(--fit));
  height: calc(297mm * var(--fit));
}

.page {
  width: 210mm;
  height: 297mm;
  padding: 12mm;
  background: #fff;
  border: 1px solid var(--border);
  box-shadow: 0 6px 30px rgba(20, 30, 45, 0.12);
  overflow: hidden;
  transform: scale(var(--fit));
  transform-origin: top left;
}

.preview-note { flex: none; margin: 8px 0 0; font-size: 12px; color: var(--muted); text-align: center; }
#steps-status { color: var(--accent); }
#steps-status:not(:empty)::after { content: " · "; color: var(--muted); }

/* pager (on-screen page navigation) */
.pager {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  margin-top: 12px;
  font-size: 13px;
  color: var(--muted);
}
.pager button {
  width: 32px;
  height: 32px;
  font-size: 16px;
  line-height: 1;
  color: var(--text);
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}
.pager button:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.pager button:disabled { opacity: 0.4; cursor: default; }

.sheet { width: 100%; }

.sheet-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  border-bottom: 1.5px solid var(--text);
  padding-bottom: 6px;
  margin-bottom: 14px;
}
.sheet-title { font-size: 22px; font-weight: 600; }
.sheet-author { font-size: 12px; color: var(--muted); }

.empty { color: var(--muted); font-size: 14px; text-align: center; margin-top: 40px; }

.entry { margin-bottom: 10px; }

.entry-label { font-size: 13px; margin-bottom: 5px; break-after: avoid; }
.entry-label .hz { font-size: 16px; margin: 0 4px; font-family: var(--hanzi-font); }
.entry-label .hz-glyph { display: inline-block; }
.entry-label .hz-glyph svg {
  display: inline-block;
  width: 1.2em;
  height: 1.2em;
  vertical-align: -0.22em;
}
.entry-label .py { color: #3a4250; font-style: italic; }
.entry-label .hv { color: var(--muted); font-size: 12px; letter-spacing: 0.02em; }
.entry-label .note { color: var(--muted); }

.grid-row { display: flex; gap: 1mm; margin-bottom: 1mm; break-inside: avoid; }

.cell,
.step-cell {
  flex: 0 0 auto;
  border: 1px solid var(--cell-border);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Cg stroke='%23cfd4da' stroke-width='1' stroke-dasharray='4 3'%3E%3Cline x1='50' y1='0' x2='50' y2='100'/%3E%3Cline x1='0' y1='50' x2='100' y2='50'/%3E%3Cline x1='0' y1='0' x2='100' y2='100'/%3E%3Cline x1='100' y1='0' x2='0' y2='100'/%3E%3C/g%3E%3C/svg%3E");
  background-size: 100% 100%;
}

.cell {
  width: var(--cell);
  height: var(--cell);
  display: flex;
  align-items: center;
  justify-content: center;
}

.cell span {
  font-family: var(--hanzi-font);
  font-size: calc(var(--cell) * 0.72);
  line-height: 1;
  color: var(--faint-ink);
}

.cell svg { display: block; width: 100%; height: 100%; }

.cell.model { border-color: var(--cell-border-model); }
.cell.model span { color: var(--model-ink); }

/* stroke-order steps */
.stroke-steps {
  display: flex;
  flex-wrap: wrap;
  gap: 1mm;
  margin: 1mm 0 2mm;
  break-inside: avoid;
}

.step-cell {
  position: relative;
  width: var(--step-cell);
  height: var(--step-cell);
}
.step-cell svg { display: block; width: 100%; height: 100%; }
.step-cell em {
  position: absolute;
  right: 0.4mm;
  bottom: 0.1mm;
  font-style: normal;
  font-size: 2mm;
  line-height: 1;
  color: var(--muted);
}

/* ---------- Print ---------- */
@media print {
  @page { size: A4; margin: 12mm; }

  html, body { background: #fff; }
  .no-print { display: none !important; }

  .app { display: block; height: auto; overflow: visible; padding: 0; }
  .preview { display: block; height: auto; max-width: none; overflow: visible; padding: 0; background: #fff; }
  .page-viewport { display: block; }
  .page-frame { width: auto; height: auto; }

  .page {
    width: auto;
    height: auto;
    padding: 0;
    border: 0;
    box-shadow: none;
    overflow: visible;
    transform: none;
  }

  /* print every page; each page body starts a fresh sheet */
  .page-body { display: block !important; }
  .page-body { break-after: page; }
  .page-body:last-child { break-after: auto; }
  .page-body .entry { break-inside: avoid; }
}

@media (max-width: 900px) {
  .app { flex-direction: column; align-items: stretch; height: auto; overflow: visible; }
  .panel { flex: none; max-height: none; overflow-y: visible; }
  .preview { flex: none; max-width: none; height: auto; overflow: visible; }
  .page-viewport { display: block; }
}
