:root {
  color-scheme: light;
  --bg: #f7f4ee;
  --surface: #fffdf8;
  --ink: #19232d;
  --muted: #68717c;
  --line: #c9d0d6;
  --line-strong: #3f4852;
  --accent: #0f8b8d;
  --accent-soft: #dff3f1;
  --warn: #c74343;
  --warn-soft: #ffe3df;
  --given: #edf0f2;
  --shadow: 0 18px 45px rgba(33, 42, 53, 0.14);
  --body-bg:
    radial-gradient(circle at 20% 0%, rgba(15, 139, 141, 0.12), transparent 28rem),
    linear-gradient(145deg, #f7f4ee, #eef5f4 58%, #f8f0ec);
  --panel: rgba(255, 253, 248, 0.82);
  --board-panel: rgba(255, 253, 248, 0.74);
  --same-bg: #7fd0c8;
  --same-ink: #043f43;
  --line-bg: #b9e4df;
  --key-bg: #1c2b35;
  --gold: #f0c75e;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #101820;
  --surface: #17222c;
  --ink: #edf5f7;
  --muted: #94a8b2;
  --line: #31434e;
  --line-strong: #9bb7c3;
  --accent: #4dd6cd;
  --accent-soft: #203f43;
  --warn: #ff7a73;
  --warn-soft: #442528;
  --given: #22313c;
  --shadow: 0 18px 48px rgba(0, 0, 0, 0.34);
  --body-bg:
    radial-gradient(circle at 18% 0%, rgba(77, 214, 205, 0.13), transparent 24rem),
    linear-gradient(145deg, #0d131a, #13212a 58%, #0b1218);
  --panel: rgba(23, 34, 44, 0.88);
  --board-panel: rgba(15, 25, 33, 0.84);
  --same-bg: #246f73;
  --same-ink: #efffff;
  --line-bg: #1f4d52;
  --key-bg: #0f1a22;
  --gold: #d7b354;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  overflow-x: hidden;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--body-bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
}

button,
select {
  font: inherit;
}

.app {
  width: 100%;
  max-width: 34rem;
  min-height: 100vh;
  margin: 0 auto;
  padding: max(0.65rem, env(safe-area-inset-top)) 1rem max(1.25rem, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 0.62rem;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  min-width: 0;
}

p {
  min-height: 1.4rem;
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
  overflow-wrap: anywhere;
}

.timer {
  flex: 0 0 auto;
  min-width: 5.6rem;
  padding: 0.48rem 0.7rem;
  border: 1px solid rgba(63, 72, 82, 0.12);
  border-radius: 0.5rem;
  background: var(--panel);
  box-shadow: 0 8px 22px rgba(33, 42, 53, 0.08);
  text-align: center;
  font-variant-numeric: tabular-nums;
  font-size: 1.12rem;
  font-weight: 700;
}

.controls {
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) repeat(6, minmax(0, 1fr));
  gap: 0.38rem;
}

.field {
  min-width: 0;
  display: grid;
  gap: 0.25rem;
}

.field span {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

select,
button {
  min-width: 0;
  min-height: 2.35rem;
  border: 0;
  border-radius: 0.5rem;
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1px 0 rgba(63, 72, 82, 0.12), 0 10px 24px rgba(33, 42, 53, 0.08);
}

button {
  cursor: pointer;
  touch-action: manipulation;
  font-weight: 700;
  transition: transform 120ms ease, filter 120ms ease, background-color 120ms ease;
}

button:active {
  transform: scale(0.97);
  filter: brightness(0.96);
}

select {
  width: 100%;
  padding: 0 0.7rem;
  font-weight: 700;
}

.tool-button {
  min-height: 2.35rem;
  display: grid;
  grid-template-rows: 1fr auto;
  place-items: center;
  gap: 0.06rem;
  padding: 0.22rem 0.18rem;
  font-size: 0.68rem;
  line-height: 1;
}

.tool-icon {
  display: block;
  font-size: 1.04rem;
  line-height: 1;
}

.board-wrap {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  padding: 0.55rem;
  border-radius: 0.65rem;
  background: rgba(255, 253, 248, 0.74);
  background: var(--board-panel);
  box-shadow: var(--shadow);
}

.board {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(9, minmax(0, 1fr));
  grid-template-rows: repeat(9, minmax(0, 1fr));
  border: 2px solid var(--line-strong);
  background: var(--line-strong);
  overflow: hidden;
}

.cell {
  width: 100%;
  min-width: 0;
  min-height: 0;
  height: 100%;
  padding: 0;
  display: grid;
  place-items: center;
  border: 0;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  background: var(--surface);
  box-shadow: none;
  color: var(--accent);
  font-size: clamp(1.1rem, 6.8vw, 2.25rem);
  font-weight: 500;
  line-height: 1;
  transition: background-color 120ms ease, color 120ms ease, filter 120ms ease, transform 120ms ease;
}

.cell:active {
  transform: scale(0.94);
  filter: brightness(0.94);
}

.cell:nth-child(3n) {
  border-right: 2px solid var(--line-strong);
}

.cell:nth-child(n + 19):nth-child(-n + 27),
.cell:nth-child(n + 46):nth-child(-n + 54) {
  border-bottom: 2px solid var(--line-strong);
}

.cell:nth-child(9n) {
  border-right: 0;
}

.cell:nth-child(n + 73) {
  border-bottom: 0;
}

.cell.given {
  background: var(--given);
  color: var(--ink);
  font-weight: 600;
}

.cell.selected {
  background: #cdece9;
  outline: 3px solid var(--accent);
  outline-offset: -3px;
}

.cell.peer {
  background: var(--accent-soft);
}

.cell.number-line {
  background: var(--line-bg);
}

.cell.same {
  background: var(--same-bg);
  color: var(--same-ink);
  box-shadow: inset 0 0 0 1px rgba(4, 63, 67, 0.18);
}

.cell.error {
  background: var(--warn-soft);
  color: var(--warn);
}

.cell.hint {
  animation: flash 0.8s ease;
}

.notes {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  place-items: center;
  padding: 0.1rem;
  color: #4f6971;
  color: var(--muted);
  font-size: clamp(0.42rem, 2.5vw, 0.72rem);
  font-weight: 500;
  line-height: 1;
}

.notes span {
  min-width: 0;
  opacity: 0.88;
}

.keypad {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 0.5rem;
}

.keypad button {
  min-height: 3.15rem;
  display: grid;
  place-items: center;
  gap: 0.05rem;
  background: var(--key-bg);
  color: #fff;
  font-size: 1.18rem;
  font-weight: 600;
}

.keypad button.depleted {
  visibility: hidden;
  pointer-events: none;
}

.keypad button.active-number {
  background: #0f8b8d;
  background: var(--accent);
  color: #082024;
}

.key-number {
  line-height: 1;
}

.key-count {
  color: rgba(255, 255, 255, 0.68);
  font-size: 0.68rem;
  font-weight: 500;
  line-height: 1;
}

.keypad .wide {
  grid-column: span 1;
  background: var(--gold);
  color: #26313b;
  font-size: 1rem;
}

.controls .active {
  background: var(--accent);
  color: #fff;
}

.stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.5rem;
}

.stats div {
  min-width: 0;
  min-height: 4rem;
  padding: 0.65rem;
  border-radius: 0.5rem;
  background: rgba(255, 253, 248, 0.82);
  background: var(--panel);
  box-shadow: 0 8px 22px rgba(33, 42, 53, 0.08);
  text-align: center;
}

.stats span {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

.stats strong {
  display: block;
  margin-top: 0.15rem;
  font-size: 1.45rem;
}

@keyframes flash {
  0%, 100% { filter: brightness(1); }
  35% { filter: brightness(1.24); }
}

.victory {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  background: rgba(4, 9, 16, 0.82);
}

.victory[hidden] {
  display: none;
}

#fireworks {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.victory-panel {
  position: relative;
  z-index: 1;
  width: min(82vw, 20rem);
  padding: 1.25rem;
  border: 1px solid rgba(255, 255, 255, 0.32);
  border-radius: 0.75rem;
  background: rgba(255, 253, 248, 0.12);
  color: #fff;
  text-align: center;
  box-shadow: 0 28px 70px rgba(0, 0, 0, 0.36);
  backdrop-filter: blur(12px);
}

.victory-panel span {
  display: block;
  color: #41ead4;
  font-size: 0.9rem;
  font-weight: 700;
}

.victory-panel strong {
  display: block;
  margin: 0.15rem 0 0.85rem;
  font-size: clamp(2.2rem, 12vw, 3.5rem);
  line-height: 1;
}

.victory-info {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.45rem;
  margin: 0 0 1rem;
}

.victory-info div {
  padding: 0.48rem 0.3rem;
  border-radius: 0.5rem;
  background: rgba(255, 255, 255, 0.12);
}

.victory-info dt,
.victory-info dd {
  margin: 0;
}

.victory-info dt {
  color: rgba(255, 255, 255, 0.72);
  font-size: 0.72rem;
}

.victory-info dd {
  margin-top: 0.12rem;
  font-size: 1rem;
  font-weight: 700;
}

.victory-panel button {
  width: 100%;
  background: #f9d65c;
  color: #15212b;
}

.confirm-layer {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: rgba(7, 13, 24, 0.58);
}

.confirm-layer[hidden] {
  display: none;
}

.confirm-panel {
  width: min(88vw, 21rem);
  padding: 1rem;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 0.75rem;
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.28);
}

.confirm-panel strong {
  display: block;
  font-size: 1.2rem;
}

.confirm-panel p {
  margin-top: 0.35rem;
}

.confirm-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.6rem;
  margin-top: 1rem;
}

.confirm-actions button:last-child {
  background: var(--accent);
  color: #fff;
}

@media (max-width: 430px) {
  .app {
    gap: 0.55rem;
    padding-inline: 0.55rem;
  }

  .controls {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .field {
    grid-column: 1 / -1;
  }

  .timer {
    min-width: 4.9rem;
    padding-inline: 0.65rem;
    font-size: 1.05rem;
  }
}

@media (max-width: 360px) {
  .app {
    padding-inline: 0.45rem;
  }

  .board-wrap {
    padding: 0.35rem;
  }

  .controls,
  .keypad,
  .stats {
    gap: 0.38rem;
  }

  .controls button {
    font-size: 0.62rem;
  }

  .keypad button {
    min-height: 2.85rem;
  }
}
