:root {
  color-scheme: light;
  --bg: #f3f1ec;
  --paper: #fffefb;
  --ink: #1f2421;
  --muted: #737771;
  --line: #cfd2cb;
  --line-strong: #20251f;
  --accent: #2f5d50;
  --accent-soft: #e3ece8;
  --danger: #c23934;
  --danger-soft: #fbe7e5;
  --success: #2e765b;
  --success-soft: #e6f3ed;
  --hint: #8a641f;
  --hint-soft: #f6eedc;
  --shadow: 0 18px 50px rgba(28, 34, 30, 0.08);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
}

button, select, input { font: inherit; }
button, select { min-height: 42px; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.45; }

.app-shell {
  width: min(1420px, calc(100% - 32px));
  margin: 0 auto;
  padding: 30px 0 56px;
}

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

.eyebrow, .puzzle-kicker {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.16em;
}

h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: clamp(1.7rem, 4vw, 2.4rem); letter-spacing: -0.04em; }

.topbar-status {
  display: flex;
  gap: 10px;
  color: var(--muted);
  font-size: 0.9rem;
}

.topbar-status span {
  padding: 7px 10px;
  border: 1px solid rgba(0,0,0,.08);
  border-radius: 999px;
  background: rgba(255,255,255,.55);
}

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 10px;
  padding: 14px;
  border: 1px solid rgba(0,0,0,.07);
  border-radius: 18px;
  background: rgba(255,255,255,.76);
  box-shadow: 0 10px 30px rgba(28,34,30,.04);
  backdrop-filter: blur(10px);
}

.controls label:not(.check-label) {
  display: grid;
  gap: 6px;
  min-width: 130px;
}

.controls label > span, .secondary-options span {
  font-size: 0.78rem;
  font-weight: 750;
  color: var(--muted);
}

select, button {
  border: 1px solid rgba(31,36,33,.16);
  border-radius: 11px;
  background: #fff;
  color: var(--ink);
  padding: 0 14px;
}

button:hover { border-color: rgba(31,36,33,.34); }
button.primary { border-color: var(--accent); background: var(--accent); color: #fff; font-weight: 800; }
button.ghost { background: transparent; }

.check-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 42px;
  padding: 0 8px;
  white-space: nowrap;
}

.check-label input { width: 17px; height: 17px; accent-color: var(--accent); }
.check-label.compact { min-height: auto; padding: 0; }

.secondary-options {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin: 10px 2px 20px;
  color: var(--muted);
  font-size: 0.8rem;
}
.secondary-options p { margin: 0; }

.empty-state {
  display: grid;
  place-items: center;
  min-height: 460px;
  padding: 40px;
  border: 1px dashed rgba(31,36,33,.18);
  border-radius: 26px;
  text-align: center;
  color: var(--muted);
}
.empty-state h2 { margin: 18px 0 8px; color: var(--ink); }
.empty-state p { margin-bottom: 0; }
.empty-icon {
  display: grid;
  place-items: center;
  width: 96px;
  height: 96px;
  border-radius: 28px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 1.6rem;
  font-weight: 900;
}

.sheet {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr));
  gap: 18px;
}

.puzzle-card {
  min-width: 0;
  padding: 18px;
  border: 1px solid rgba(31,36,33,.09);
  border-radius: 22px;
  background: var(--paper);
  box-shadow: var(--shadow);
}

.puzzle-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}
.puzzle-head h2 { margin: 0; font-size: 1rem; }
.puzzle-state {
  padding: 6px 9px;
  border-radius: 999px;
  background: #f0f1ee;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
}
.puzzle-card.is-complete .puzzle-state { background: var(--success-soft); color: var(--success); }
.puzzle-card.is-failed .puzzle-state { background: var(--danger-soft); color: var(--danger); }

.board-wrap { position: relative; }
.sudoku-board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  width: min(100%, 560px);
  aspect-ratio: 1;
  margin: 0 auto;
  border: 2px solid var(--line-strong);
  background: var(--line-strong);
  gap: 1px;
  overflow: hidden;
  touch-action: manipulation;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  border: 0;
  border-radius: 0;
  padding: 0;
  background: var(--paper);
  color: var(--ink);
  font-size: clamp(1rem, 3.2vw, 1.55rem);
  font-weight: 620;
  line-height: 1;
}
.cell:nth-child(3n):not(:nth-child(9n)) { box-shadow: inset -2px 0 0 var(--line-strong); }
.cell:nth-child(n+19):nth-child(-n+27),
.cell:nth-child(n+46):nth-child(-n+54) { box-shadow: inset 0 -2px 0 var(--line-strong); }
.cell:nth-child(3n):not(:nth-child(9n)):nth-child(n+19):nth-child(-n+27),
.cell:nth-child(3n):not(:nth-child(9n)):nth-child(n+46):nth-child(-n+54) { box-shadow: inset -2px 0 0 var(--line-strong), inset 0 -2px 0 var(--line-strong); }

.cell.given { background: #f3f2ee; font-weight: 850; }
.cell.user-value { color: var(--accent); }
.cell.hint-value { background: var(--hint-soft); color: var(--hint); }
.cell.selected { outline: 3px solid var(--accent); outline-offset: -3px; z-index: 2; }
.cell.peer { background: #f3f7f5; }
.cell.same-value { background: var(--accent-soft); }
.cell.wrong { animation: wrongFlash .55s ease; background: var(--danger-soft); color: var(--danger); }
.cell:focus-visible { outline: 3px solid var(--accent); outline-offset: -3px; z-index: 3; }

@keyframes wrongFlash {
  0%, 100% { transform: scale(1); }
  35% { transform: scale(.91); }
  65% { transform: scale(1.04); }
}

.result-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 5px;
  background: rgba(255,254,251,.9);
  text-align: center;
  backdrop-filter: blur(2px);
}
.result-overlay[hidden] { display: none; }
.result-overlay strong { font-size: 1.4rem; }
.result-overlay span { color: var(--muted); font-size: .85rem; }

.digit-progress {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 5px;
  margin: 12px 0 10px;
}
.digit-check {
  display: grid;
  place-items: center;
  min-height: 28px;
  border: 1px solid rgba(31,36,33,.1);
  border-radius: 8px;
  color: var(--muted);
  font-size: .75rem;
  font-weight: 800;
}
.digit-check input { position: absolute; opacity: 0; pointer-events: none; }
.digit-check.done { border-color: transparent; background: var(--success-soft); color: var(--success); }
.digit-check.done::before { content: "✓"; margin-right: 2px; }

.number-pad {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  gap: 5px;
  margin-bottom: 10px;
}
.number-pad button { min-height: 36px; padding: 0; font-weight: 800; }
.number-pad button[data-value="0"] { color: var(--muted); }

.puzzle-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 7px;
}
.puzzle-actions button { min-height: 36px; padding: 0 11px; font-size: .8rem; }
.mistakes { margin-left: auto; color: var(--muted); font-size: .78rem; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 20;
  transform: translate(-50%, 20px);
  padding: 10px 14px;
  border-radius: 12px;
  background: #20251f;
  color: #fff;
  font-size: .85rem;
  opacity: 0;
  pointer-events: none;
  transition: .2s ease;
}
.toast.show { transform: translate(-50%, 0); opacity: 1; }

@media (max-width: 720px) {
  .app-shell { width: min(100% - 18px, 1420px); padding-top: 18px; }
  .topbar { align-items: start; }
  .topbar-status { flex-direction: column; gap: 5px; text-align: right; }
  .controls > * { flex: 1 1 125px; }
  .controls button { flex-basis: 110px; }
  .secondary-options { align-items: start; flex-direction: column; }
  .secondary-options p { display: none; }
  .puzzle-card { padding: 12px; border-radius: 16px; }
  .number-pad { grid-template-columns: repeat(5, 1fr); }
  .cell { font-size: clamp(1rem, 5vw, 1.35rem); }
}

@media print {
  @page { size: A4 portrait; margin: 8mm; }
  html, body { background: #fff; }
  body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .no-print { display: none !important; }
  .app-shell { width: auto; margin: 0; padding: 0; }
  .sheet { display: grid; gap: 4mm; height: 281mm; }
  .sheet[data-count="1"] { grid-template-columns: 1fr; align-content: center; }
  .sheet[data-count="2"] { grid-template-columns: 1fr; grid-template-rows: repeat(2, 1fr); }
  .sheet[data-count="3"], .sheet[data-count="4"] { grid-template-columns: repeat(2, 1fr); grid-template-rows: repeat(2, 1fr); }
  .sheet[data-count="5"], .sheet[data-count="6"] { grid-template-columns: repeat(2, 1fr); grid-template-rows: repeat(3, 1fr); }
  .puzzle-card { display: flex; flex-direction: column; justify-content: center; padding: 3mm; border: 1px solid #bbb; border-radius: 0; box-shadow: none; break-inside: avoid; }
  .puzzle-head { margin-bottom: 2mm; }
  .puzzle-head h2 { font-size: 9pt; }
  .puzzle-kicker, .puzzle-state { font-size: 6.5pt; }
  .sudoku-board { width: auto; height: min(100%, 118mm); max-width: 100%; max-height: 100%; }
  .sheet[data-count="2"] .sudoku-board { height: 112mm; }
  .sheet[data-count="3"] .sudoku-board,
  .sheet[data-count="4"] .sudoku-board { height: 92mm; }
  .sheet[data-count="5"] .sudoku-board,
  .sheet[data-count="6"] .sudoku-board { height: 69mm; }
  .cell { font-size: 13pt; }
  .sheet[data-count="5"] .cell, .sheet[data-count="6"] .cell { font-size: 9pt; }
  .digit-progress { margin: 2mm 0 0; gap: 1mm; }
  .digit-check { min-height: 5mm; border-radius: 0; font-size: 6.5pt; }
  .result-overlay { display: none !important; }
}

/* SUDOSUDO_SINGLE_A4_V2 */
@media print {
  @page {
    size: A4 portrait;
    margin: 8mm;
  }

  html,
  body {
    width: 194mm !important;
    height: 281mm !important;
    min-width: 194mm !important;
    min-height: 281mm !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: hidden !important;
    background: #fff !important;
  }

  body {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }

  .no-print {
    display: none !important;
  }

  .app-shell {
    width: 194mm !important;
    height: 281mm !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: hidden !important;
  }

  .sheet {
    width: 194mm !important;
    height: 281mm !important;
    max-width: 194mm !important;
    max-height: 281mm !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: hidden !important;
    align-content: stretch !important;
    grid-auto-flow: row !important;
    page-break-inside: avoid !important;
    break-inside: avoid-page !important;
    page-break-after: avoid !important;
  }

  .puzzle-card {
    min-width: 0 !important;
    min-height: 0 !important;
    overflow: hidden !important;

    display: grid !important;
    grid-template-rows:
      auto
      minmax(0, 1fr)
      auto !important;

    align-items: stretch !important;
    justify-content: stretch !important;

    padding: 3mm !important;
    border-radius: 0 !important;
    box-shadow: none !important;

    page-break-inside: avoid !important;
    break-inside: avoid-page !important;
  }

  .puzzle-head {
    margin-bottom: 1.5mm !important;
  }

  .board-wrap {
    min-width: 0 !important;
    min-height: 0 !important;
    display: grid !important;
    place-items: center !important;
    overflow: hidden !important;
  }

  .sudoku-board {
    max-width: 100% !important;
    max-height: 100% !important;
    aspect-ratio: 1 / 1 !important;
    margin: auto !important;
  }

  /* 1 題 */
  .sheet[data-count="1"] .sudoku-board {
    width: 170mm !important;
    height: 170mm !important;
  }

  /* 2 題 */
  .sheet[data-count="2"] .sudoku-board {
    width: 108mm !important;
    height: 108mm !important;
  }

  /* 3–4 題 */
  .sheet[data-count="3"] .sudoku-board,
  .sheet[data-count="4"] .sudoku-board {
    width: 82mm !important;
    height: 82mm !important;
  }

  /* 5–6 題 */
  .sheet[data-count="5"] .sudoku-board,
  .sheet[data-count="6"] .sudoku-board {
    width: 64mm !important;
    height: 64mm !important;
  }

  .digit-progress {
    flex: 0 0 auto !important;
    margin-top: 1.5mm !important;
  }

  .result-overlay {
    display: none !important;
  }
}


/* SUDOSUDO_GRID_9X9_FIX_V1
   修正手機版 button 全域 min-height:42px 導致第 9 列被裁切 */
.sudoku-board {
  grid-template-rows: repeat(9, minmax(0, 1fr));
}

.sudoku-board > .cell {
  min-height: 0;
  min-block-size: 0;
  -webkit-appearance: none;
  appearance: none;
}
