* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0;
  background-color: var(--paper);
  color: var(--ink);
  font-family: var(--font-serif);
  -webkit-font-smoothing: antialiased;
  transition: background-color 0.25s ease, color 0.25s ease;
}
body {
  min-height: 100vh;
  display: flex; flex-direction: column; align-items: center;
  padding: calc(env(safe-area-inset-top) + 20px) 16px calc(env(safe-area-inset-bottom) + 26px);
  gap: 13px;
  -webkit-user-select: none; user-select: none;
  overscroll-behavior: none;
  /* 宣纸: warm vignette over faint paper grain — both fixed, behind content */
  background-color: var(--paper);
  background-image:
    radial-gradient(ellipse 118% 88% at 50% 36%, transparent 52%, var(--vignette) 100%),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.8' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.04'/%3E%3C/svg%3E");
  background-repeat: no-repeat, repeat;
  background-size: cover, 160px 160px;
  background-attachment: fixed, fixed;
}

/* ---- header ---- */
header { text-align: center; }
header h1 {
  margin: 0;
  font-size: 27px; font-weight: 600;
  letter-spacing: 0.5em; text-indent: 0.5em;
  color: var(--accent);
}
header .sub {
  margin-top: 3px;
  font-size: 10px; letter-spacing: 0.4em; text-indent: 0.4em;
  color: var(--ink-soft); opacity: 0.7; text-transform: uppercase;
}

/* ---- settings button ---- */
.settings-btn {
  position: fixed; top: calc(env(safe-area-inset-top) + 14px); right: 16px;
  width: 38px; height: 38px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--paper-deep);
  color: var(--accent); font-size: 18px; cursor: pointer; z-index: 5;
  display: flex; align-items: center; justify-content: center;
  transition: transform var(--motion), background var(--motion);
}
.settings-btn:active { transform: scale(0.92); background: var(--highlight); }

/* ---- immersive: hide all chrome, enlarge the board ---- */
body.immersive header,
body.immersive .difficulty,
body.immersive .toolbar,
body.immersive footer { display: none; }
body.immersive { padding-top: calc(env(safe-area-inset-top) + 58px); gap: 16px; }
body.immersive .board-wrap,
body.immersive .controls { width: min(96vw, 540px); }

/* ---- difficulty pills ---- */
.difficulty {
  display: flex; gap: 3px;
  background: var(--paper-deep); border: 1px solid var(--line);
  border-radius: var(--radius-pill); padding: 3px;
}
.difficulty button {
  border: none; background: transparent; color: var(--ink-soft);
  font-family: inherit; font-size: 12.5px; letter-spacing: 0.02em;
  padding: 5px 10px; border-radius: var(--radius-pill); cursor: pointer;
  transition: background var(--motion), color var(--motion);
}
.difficulty button.active { background: var(--accent); color: var(--paper); }

/* ---- toolbar stats (kept quiet so the board leads) ---- */
.toolbar { display: flex; align-items: center; gap: 13px; }
.stat { display: flex; flex-direction: column; align-items: center; min-width: 50px; }
.stat .label { font-size: 9px; letter-spacing: 0.22em; text-transform: uppercase; color: var(--ink-soft); opacity: 0.8; }
.stat .value { font-size: 16px; color: var(--accent); font-variant-numeric: tabular-nums; }
.divider { width: 1px; height: 24px; background: var(--line); }

/* ---- board ---- */
.board-wrap { position: relative; width: min(92vw, 460px); flex: 0 0 auto; }
.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr); /* equal row heights — empty rows must not collapse */
  width: 100%; aspect-ratio: 1 / 1;
  background: var(--paper);
  border: 2.5px solid var(--line-bold);
  box-shadow: 0 8px 30px rgba(60, 50, 30, 0.12), 0 1px 0 rgba(255,255,255,0.5) inset;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .board { box-shadow: 0 8px 30px rgba(0,0,0,0.4); }
}
:root[data-theme="dark"] .board { box-shadow: 0 8px 30px rgba(0,0,0,0.4); }

.cell {
  position: relative;
  display: flex; align-items: center; justify-content: center;
  border-right: 1px solid var(--line); border-bottom: 1px solid var(--line);
  font-size: clamp(20px, 5.5vw, 30px);
  font-variant-numeric: lining-nums tabular-nums;
  color: var(--entry); cursor: pointer;
  transition: background 0.1s ease;
}
.cell:nth-child(9n) { border-right: none; }
.cell:nth-child(n+73) { border-bottom: none; }
.cell[data-col="2"], .cell[data-col="5"] { border-right: 2px solid var(--line-bold); }
.cell[data-row="2"], .cell[data-row="5"] { border-bottom: 2px solid var(--line-bold); }

.cell.given { color: var(--given); font-weight: 600; }
.cell.peer { background: var(--peer); }
.cell.same-digit { background: var(--highlight); }
/* selection: quiet ink wash + thin ink-blue ring, not a solid fill */
.cell.selected { background: var(--accent-wash); box-shadow: inset 0 0 0 2px var(--accent); }
.cell.hinted { box-shadow: inset 0 0 0 2px var(--accent); background: var(--highlight); }

.cell .digit { display: inline-block; animation: ink-bloom var(--motion); }
@keyframes ink-bloom {
  from { opacity: 0; transform: scale(0.72); filter: blur(0.5px); }
  to   { opacity: 1; transform: scale(1);    filter: blur(0); }
}

.cell.reject { animation: reject-flash 380ms ease-out; }
@keyframes reject-flash {
  0%   { background: var(--error-wash); }
  25%  { transform: translateX(-2px); }
  45%  { transform: translateX(2px); }
  65%  { transform: translateX(-1px); }
  100% { background: transparent; transform: translateX(0); }
}

.notes {
  position: absolute; inset: 3px;
  display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr);
  font-size: clamp(7px, 1.8vw, 10px); color: var(--ink-soft); pointer-events: none;
}
.notes span { display: flex; align-items: center; justify-content: center; }

/* ---- veil while generating ---- */
.board-veil {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  background: color-mix(in srgb, var(--paper) 88%, transparent);
  color: var(--ink-soft); font-size: 14px; letter-spacing: 0.08em;
}
.veil-ink {
  width: 32px; height: 32px; border-radius: 50%;
  background: radial-gradient(circle, var(--accent) 0%, transparent 70%);
  animation: ink-pulse 1.4s ease-in-out infinite;
}
@keyframes ink-pulse { 0%,100% { transform: scale(0.6); opacity: 0.4; } 50% { transform: scale(1.1); opacity: 0.9; } }

/* ---- controls ---- */
.controls { display: flex; flex-direction: column; gap: 12px; width: min(92vw, 460px); }
/* secondary tools — quiet, borderless, compact */
.actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; }
.actions button {
  border: none; background: transparent; color: var(--ink-soft);
  font-family: inherit; font-size: 11px; letter-spacing: 0.04em;
  padding: 7px 4px 6px; border-radius: var(--radius);
  cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 4px;
  transition: transform var(--motion), background var(--motion), color var(--motion);
}
.actions button:active { transform: scale(0.94); background: var(--paper-deep); }
.actions button.active { background: var(--accent-wash); color: var(--accent); }
.actions button .ico { line-height: 0; color: var(--ink); }
.actions button .ico svg { display: block; }
.actions button.active .ico { color: var(--accent); }

/* primary input — clean paper tiles, no borders */
.pad { display: grid; grid-template-columns: repeat(9, 1fr); gap: 5px; }
.pad button {
  position: relative; aspect-ratio: 1 / 1.06;
  display: flex; align-items: center; justify-content: center;
  border: none; background: var(--paper-deep); color: var(--accent);
  font-family: inherit; font-size: clamp(20px, 5.2vw, 27px);
  font-variant-numeric: lining-nums; border-radius: var(--radius);
  cursor: pointer; transition: transform var(--motion), background var(--motion);
}
.pad button:active { transform: scale(0.93); background: var(--accent-wash); }
.pad button .count {
  position: absolute; top: 3px; right: 4px;
  font-size: 9px; color: var(--ink-soft); opacity: 0.65;
  font-variant-numeric: tabular-nums;
}
.pad button.done { opacity: 0.3; pointer-events: none; }

footer { font-size: 11px; color: var(--ink-soft); letter-spacing: 0.12em; text-align: center; }

/* ---- overlay / modal ---- */
.overlay {
  position: fixed; inset: 0; z-index: 10;
  background: rgba(40, 34, 22, 0.45);
  display: flex; align-items: center; justify-content: center;
  animation: fade var(--motion);
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.modal {
  background: var(--paper); border: 1px solid var(--line-bold);
  border-radius: var(--radius-modal); padding: 30px 30px 26px;
  text-align: center; box-shadow: 0 20px 60px rgba(0,0,0,0.25);
  width: min(86vw, 340px);
  animation: rise var(--motion);
}
@keyframes rise { from { transform: translateY(16px); } to { transform: translateY(0); } }
.modal h2 { margin: 0 0 12px; color: var(--accent); font-size: 24px; letter-spacing: 0.1em; }
.modal p { margin: 6px 0; color: var(--ink-soft); font-size: 14px; }

.row-toggle, .row-seg {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 2px; font-size: 16px; color: var(--ink);
  border-bottom: 1px solid var(--line); text-align: left;
}
.row-toggle small { display: block; font-size: 11px; color: var(--ink-soft); opacity: 0.8; margin-top: 2px; }
.seg { display: flex; gap: 4px; }
.seg button {
  padding: 6px 11px; background: var(--paper-deep); border: 1px solid var(--line);
  border-radius: var(--radius); color: var(--ink-soft); font-family: inherit; font-size: 13px; cursor: pointer;
}
.seg button.active { background: var(--accent); border-color: var(--accent); color: var(--paper); }
.row-danger {
  width: 100%; margin-top: 14px; padding: 12px;
  background: none; border: 1px solid var(--error); border-radius: var(--radius);
  color: var(--error); font-family: inherit; font-size: 14px; cursor: pointer;
}
.modal-close, .primary {
  width: 100%; margin-top: 16px; padding: 13px; border-radius: var(--radius-pill);
  font-family: inherit; font-size: 15px; cursor: pointer; letter-spacing: 0.08em;
}
.modal-close { background: var(--paper-deep); border: 1px solid var(--line); color: var(--ink-soft); }
.primary { background: var(--accent); border: none; color: var(--paper); }
.primary:active, .modal-close:active { transform: scale(0.98); }

input[type="checkbox"] {
  appearance: none; -webkit-appearance: none;
  width: 46px; height: 28px; border-radius: 14px;
  background: var(--paper-deep); border: 1px solid var(--line);
  position: relative; cursor: pointer; transition: background var(--motion);
}
input[type="checkbox"]::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 22px; height: 22px; border-radius: 50%;
  background: var(--ink-soft); transition: transform var(--motion), background var(--motion);
}
input[type="checkbox"]:checked { background: var(--accent-soft); border-color: var(--accent); }
input[type="checkbox"]:checked::after { transform: translateX(18px); background: var(--accent); }

.seal {
  width: 60px; height: 60px; margin: 0 auto 12px;
  display: flex; align-items: center; justify-content: center;
  background: var(--error); color: #f5f1e8;
  font-size: 30px; border-radius: 6px; transform: rotate(-4deg);
}

.hint-toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 18px);
  transform: translateX(-50%); z-index: 30; max-width: 88%;
  background: var(--ink); color: var(--paper);
  padding: 12px 16px; border-radius: 10px;
  font-size: 14px; line-height: 1.5; letter-spacing: 0.02em;
  box-shadow: 0 4px 18px rgba(0,0,0,0.25); animation: rise var(--motion);
}

[hidden] { display: none !important; }
