/* ====== reset & base ====== */
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: #f5f6f8;
  color: #1d1f23;
  line-height: 1.5;
}
h1, h2, h3 { margin: 0; font-weight: 600; }
.muted { color: #888; font-size: 0.9rem; }
.hint { color: #888; font-size: 0.85rem; margin: 0.5rem 0 0; }

/* ====== layout ====== */
.topbar {
  padding: 1.5rem 1.5rem 1rem;
  background: linear-gradient(135deg, #2c5fb8, #4a8de8);
  color: #fff;
}
.topbar h1 { font-size: 1.4rem; }
.topbar .tagline { margin: 0.3rem 0 0; opacity: 0.9; font-size: 0.95rem; }

.container {
  max-width: 980px;
  margin: 0 auto;
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.bottombar {
  text-align: center;
  padding: 1.5rem;
  color: #999;
  font-size: 0.85rem;
}

/* ====== intro / FAQ card (SEO + 用户帮助) ====== */
.intro-card {
  font-size: 0.92rem;
  line-height: 1.7;
  color: #333;
}
.intro-card p { margin: 0 0 0.6rem; }
.intro-card details {
  border: 1px solid #e0e3e8;
  border-radius: 6px;
  padding: 0.5rem 0.8rem;
  background: #fafbfc;
}
.intro-card details[open] { background: #f1f5fb; border-color: #c8d6ed; }
.intro-card summary {
  cursor: pointer;
  list-style: none;
  position: relative;
  padding-right: 1.4rem;
  color: #2c5fb8;
}
.intro-card summary::after {
  content: "+";
  position: absolute;
  right: 0.2rem;
  top: 50%;
  transform: translateY(-50%);
  font-weight: 600;
  color: #888;
}
.intro-card details[open] summary::after { content: "−"; }

/* ====== cards ====== */
.card {
  background: #fff;
  border-radius: 10px;
  padding: 1rem 1.25rem;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}
.card-hd {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}
.card-hd h2 { font-size: 1rem; }

/* ====== preview strip ====== */
.preview-strip {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 6px;
}
.preview-strip img {
  height: 160px;
  width: auto;
  border: 1px solid #e0e3e8;
  border-radius: 4px;
  flex-shrink: 0;
  background: #fafafa;
  cursor: zoom-in;
  transition: transform 0.15s, box-shadow 0.15s;
}
.preview-strip img:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(44, 95, 184, 0.25);
  border-color: #4a8de8;
}

/* ====== lightbox ====== */
.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.88);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  cursor: zoom-out;
}
.lightbox[hidden] { display: none; }
.lb-img {
  max-width: 95vw;
  max-height: 92vh;
  object-fit: contain;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.6);
  background: #fff;
  cursor: default;
  user-select: none;
}
.lb-close, .lb-nav {
  position: absolute;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: inherit;
  line-height: 1;
  transition: background 0.15s;
}
.lb-close:hover, .lb-nav:hover { background: rgba(255, 255, 255, 0.3); }
.lb-close {
  top: 1rem;
  right: 1rem;
  width: 44px;
  height: 44px;
  font-size: 28px;
}
.lb-nav {
  top: 50%;
  transform: translateY(-50%);
  width: 56px;
  height: 56px;
  font-size: 36px;
}
.lb-prev { left: 1rem; }
.lb-next { right: 1rem; }
.lb-nav:disabled { opacity: 0.25; cursor: not-allowed; }
.lb-caption {
  position: absolute;
  bottom: 1.2rem;
  left: 50%;
  transform: translateX(-50%);
  color: #ddd;
  font-size: 0.95rem;
  background: rgba(0, 0, 0, 0.5);
  padding: 0.3rem 0.9rem;
  border-radius: 4px;
}

/* ====== unit chips ====== */
.unit-chips {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(96px, 1fr));
  gap: 0.5rem;
}
.unit-chip {
  border: 1.5px solid #d0d4da;
  background: #fff;
  border-radius: 8px;
  padding: 0.5rem 0.4rem;
  cursor: pointer;
  text-align: center;
  font-family: inherit;
  font-size: 0.85rem;
  color: #444;
  transition: all 0.12s;
  line-height: 1.3;
}
.unit-chip:hover { border-color: #4a8de8; }
.unit-chip .label { font-weight: 600; font-size: 0.95rem; color: #222; }
.unit-chip .count  { color: #888; font-size: 0.8rem; }
.unit-chip.selected {
  background: #2c5fb8;
  border-color: #2c5fb8;
  color: #fff;
}
.unit-chip.selected .label,
.unit-chip.selected .count { color: #fff; }

/* source picker row (above range toolbar) */
.source-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin: 0 0 0.7rem;
}
.source-label {
  font-size: 0.88rem;
  color: #555;
  white-space: nowrap;
}
.source-row select {
  flex: 1;
  font-size: 0.95rem;
  padding: 0.35rem 0.5rem;
  border: 1px solid #d0d4da;
  border-radius: 5px;
  background: #fff;
}

/* range toolbar (above chips) */
.range-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0 0 0.6rem;
  gap: 0.5rem;
}
.range-toolbar .hint { margin: 0; }
.range-actions { display: flex; gap: 0.4rem; }
.btn-mini {
  font-family: inherit;
  font-size: 0.8rem;
  padding: 0.25rem 0.6rem;
  border: 1px solid #c8ccd1;
  background: #fff;
  border-radius: 4px;
  cursor: pointer;
  color: #555;
}
.btn-mini:hover { background: #f0f3f7; border-color: #4a8de8; color: #2c5fb8; }

/* ====== opts grid ====== */
.opts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 0.75rem 1rem;
}
.opts label {
  display: flex;
  flex-direction: column;
  font-size: 0.88rem;
  color: #555;
  gap: 0.25rem;
}
.opts label.check {
  flex-direction: row;
  align-items: center;
  gap: 0.4rem;
  align-self: end;
}
.opts input, .opts select {
  font-size: 0.95rem;
  padding: 0.35rem 0.5rem;
  border: 1px solid #d0d4da;
  border-radius: 5px;
  background: #fff;
}

/* ====== buttons ====== */
.actions {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
}
.btn {
  padding: 0.55rem 1.1rem;
  font-size: 0.95rem;
  border: 1px solid #c8ccd1;
  background: #fff;
  border-radius: 6px;
  cursor: pointer;
}
.btn:hover:not(:disabled) { background: #f5f7fa; }
.btn.primary {
  background: #2c5fb8;
  color: #fff;
  border-color: #2c5fb8;
}
.btn.primary:hover { background: #234d96; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ====== sheets (字帖 pages) ====== */
#sheets {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  align-items: center;
}
.sheet {
  background: #fff;
  width: 210mm;
  height: 297mm;
  padding: 12mm 14mm;
  box-shadow: 0 2px 10px rgba(0,0,0,0.08);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.sheet-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  border-bottom: 1px solid #ddd;
  padding-bottom: 6px;
  margin-bottom: 8mm;
  font-size: 11pt;
  color: #444;
}
.sheet-header .title { font-size: 13pt; font-weight: 600; color: #222; }
.sheet-header .blank { display: inline-block; min-width: 60px; border-bottom: 1px solid #888; }

/* one row = one character */
.char-row {
  display: flex;
  flex-direction: column;
  margin-bottom: 2mm;
  page-break-inside: avoid;
  break-inside: avoid;
}
.char-row:last-child { margin-bottom: 0; }
.pinyin-row {
  display: flex;
  height: 6mm;
  align-items: end;
  margin-bottom: 1mm;
}
.pinyin-cell {
  flex: 0 0 16mm;
  text-align: center;
  font-family: "Times New Roman", serif;
  font-size: 9pt;
  color: #555;
  border-bottom: 1px dashed #ccc;
}
.cell-row { display: flex; }

/* ====== 字格 cells ====== */
/* Cross lines drawn with pseudo-element borders (always print, regardless of
   "background graphics" toggle). Diagonals (米字格) need background-image so
   we force print-color-adjust:exact globally on .sheet. */
.cell {
  flex: 0 0 16mm;
  height: 16mm;
  border: 1px solid #d44;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}
/* horizontal mid line */
.cell::before {
  content: "";
  position: absolute;
  left: -1px; right: -1px;
  top: calc(50% - 0.5px);
  border-top: 1px solid #d44;
  pointer-events: none;
}
/* vertical mid line */
.cell::after {
  content: "";
  position: absolute;
  top: -1px; bottom: -1px;
  left: calc(50% - 0.5px);
  border-left: 1px solid #d44;
  pointer-events: none;
}

/* 田字格 — cross is dashed */
.grid-tian .cell::before { border-top-style: dashed; }
.grid-tian .cell::after  { border-left-style: dashed; }

/* 米字格 — solid cross + solid diagonals (asterisk) */
.grid-mi .cell {
  background-image:
    linear-gradient(45deg,  transparent calc(50% - 0.5px), #d44 calc(50% - 0.5px), #d44 calc(50% + 0.5px), transparent calc(50% + 0.5px)),
    linear-gradient(-45deg, transparent calc(50% - 0.5px), #d44 calc(50% - 0.5px), #d44 calc(50% + 0.5px), transparent calc(50% + 0.5px));
}

/* inner-box: only visible in 回宫格 */
.inner-box { display: none; }
.grid-hui .inner-box {
  display: block;
  position: absolute;
  left: 25%; right: 25%; top: 25%; bottom: 25%;
  border: 1px dashed #d44;
  pointer-events: none;
}

/* the actual character */
.cell .ch {
  font-family: "KaiTi", "Kaiti SC", "STKaiti", "BiauKai", "AR PL UKai CN", serif;
  font-size: 36pt;
  line-height: 1;
  user-select: none;
  position: relative;   /* anchor for 回宫格 inner box */
  z-index: 1;
}
.cell.trace .ch { color: #d8d8d8; }
.cell.blank .ch { display: none; }

/* ====== print ====== */
/* force background images / colors to print (米字格 diagonals + grid lines) */
.sheet, .sheet * {
  -webkit-print-color-adjust: exact;
  print-color-adjust: exact;
  color-adjust: exact;
}

@media print {
  @page { size: A4; margin: 0; }
  html, body { background: #fff; margin: 0; padding: 0; }
  .topbar, .bottombar, .container > .card, .actions, .card-hd, .lightbox { display: none !important; }
  .container { max-width: none; padding: 0; gap: 0; }
  #result-card { display: block !important; padding: 0; margin: 0; box-shadow: none; background: transparent; }
  #sheets { gap: 0; align-items: stretch; }
  .sheet {
    box-shadow: none;
    page-break-after: always;
    break-after: page;
    margin: 0;
    width: 210mm;
    height: 297mm;
    overflow: hidden;
  }
  .sheet:last-child { page-break-after: auto; break-after: auto; }
  /* let the browser keep each row whole when it auto-paginates */
  .char-row, .pinyin-row, .cell-row { page-break-inside: avoid; break-inside: avoid; }
}
