:root {
  color-scheme: light;
  --ink: #101b2d;
  --ink-soft: #546176;
  --canvas: #e9eef5;
  --surface: #ffffff;
  --surface-2: #f4f6f9;
  --line: #c9d1dc;
  --accent: #215fd1;
  --accent-soft: #dce8ff;
  --amber: #ffb000;
  --danger: #c73737;
  --shadow: 0 20px 60px rgba(16, 27, 45, 0.12);
  font-family: Inter, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --ink: #f3f6fb;
  --ink-soft: #9daabd;
  --canvas: #0c1422;
  --surface: #152134;
  --surface-2: #1b2940;
  --line: #33445f;
  --accent: #79a8ff;
  --accent-soft: #203b68;
  --danger: #ff7777;
  --shadow: 0 24px 70px rgba(0, 0, 0, 0.32);
}

* { box-sizing: border-box; }
html { min-width: 320px; background: var(--canvas); }
body { margin: 0; min-height: 100vh; color: var(--ink); background: var(--canvas); }
button, select, input { font: inherit; }
button, select { color: inherit; }
button { -webkit-tap-highlight-color: transparent; }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 45%, transparent); outline-offset: 2px; }

.app-shell { width: min(1120px, calc(100% - 32px)); margin: 0 auto; padding: 20px 0 48px; }
.topbar { min-height: 64px; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 20px; }
.brand { color: var(--ink); text-decoration: none; display: inline-flex; align-items: center; gap: 10px; width: max-content; font-size: 1.125rem; font-weight: 800; letter-spacing: .01em; }
.brand-mark { width: 30px; aspect-ratio: 1; display: grid; grid-template-columns: 1fr 1fr; gap: 3px; padding: 4px; border-radius: 8px; background: var(--ink); }
.brand-mark i { border-radius: 2px; background: var(--surface); }
.brand-mark i:nth-child(3) { background: var(--amber); }
.game-tabs { display: flex; padding: 4px; border: 1px solid var(--line); border-radius: 13px; background: color-mix(in srgb, var(--surface) 85%, transparent); }
.tab { min-width: 96px; min-height: 40px; padding: 0 18px; border: 0; border-radius: 9px; background: transparent; cursor: pointer; font-weight: 700; }
.tab.is-active { color: #fff; background: #101b2d; box-shadow: 0 4px 12px rgba(16, 27, 45, .18); }
:root[data-theme="dark"] .tab.is-active { color: #101b2d; background: #f3f6fb; }
.header-actions { justify-self: end; display: flex; gap: 8px; }
.icon-button { width: 42px; height: 42px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); cursor: pointer; font-size: 1.2rem; }

.game-view { margin-top: 18px; padding: clamp(20px, 3.5vw, 42px); border: 1px solid color-mix(in srgb, var(--line) 82%, transparent); border-radius: 28px; background: var(--surface); box-shadow: var(--shadow); }
.game-heading { display: flex; align-items: end; justify-content: space-between; gap: 20px; margin-bottom: 18px; }
.eyebrow { margin: 0 0 3px; color: var(--ink-soft); font-size: .78rem; font-weight: 800; letter-spacing: .12em; text-transform: uppercase; }
h1 { margin: 0; font-size: clamp(1.9rem, 5vw, 3rem); line-height: 1; letter-spacing: -.04em; }
.select-field { display: grid; gap: 5px; color: var(--ink-soft); font-size: .78rem; font-weight: 700; }
select { min-height: 42px; padding: 0 36px 0 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); font-weight: 700; }
.stats-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1px; margin-bottom: 22px; overflow: hidden; border: 1px solid var(--line); border-radius: 14px; background: var(--line); }
.stats-row > div { min-height: 58px; display: grid; place-content: center; text-align: center; background: var(--surface-2); }
.stats-row span, .counter span { color: var(--ink-soft); font-size: .72rem; font-weight: 700; }
.stats-row strong { font-variant-numeric: tabular-nums; font-size: 1rem; }

.sudoku-layout { display: grid; grid-template-columns: minmax(320px, 580px) minmax(210px, 280px); justify-content: center; align-items: center; gap: clamp(26px, 5vw, 64px); }
.sudoku-board { width: min(100%, 580px); aspect-ratio: 1; display: grid; grid-template-columns: repeat(9, 1fr); overflow: hidden; border: 3px solid var(--ink); border-radius: 8px; background: var(--ink); box-shadow: 0 10px 30px rgba(16, 27, 45, .12); }
.sudoku-cell { position: relative; display: grid; place-items: center; padding: 0; border: 0; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); background: var(--surface); color: var(--accent); cursor: pointer; font-size: clamp(1.05rem, 3.3vw, 2rem); font-weight: 650; }
.sudoku-cell:nth-child(3n) { border-right: 3px solid var(--ink); }
.sudoku-cell:nth-child(9n) { border-right: 0; }
.sudoku-cell:nth-child(n+19):nth-child(-n+27), .sudoku-cell:nth-child(n+46):nth-child(-n+54) { border-bottom: 3px solid var(--ink); }
.sudoku-cell:nth-child(n+73) { border-bottom: 0; }
.sudoku-cell.is-given { color: var(--ink); font-weight: 800; }
.sudoku-cell.is-peer { background: color-mix(in srgb, var(--accent-soft) 50%, var(--surface)); }
.sudoku-cell.is-same { background: color-mix(in srgb, var(--accent-soft) 85%, var(--surface)); }
.sudoku-cell.is-selected { background: var(--accent-soft); box-shadow: inset 0 0 0 3px var(--accent); z-index: 1; }
.sudoku-cell.is-error { color: var(--danger); background: color-mix(in srgb, var(--danger) 13%, var(--surface)); }
.notes-grid { position: absolute; inset: 4px; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); align-items: center; color: var(--ink-soft); font-size: clamp(.45rem, 1.2vw, .7rem); font-weight: 600; }
.sudoku-controls { display: grid; gap: 18px; }
.number-pad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.number-pad button { aspect-ratio: 1.25; min-height: 48px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface-2); color: var(--accent); cursor: pointer; font-size: 1.45rem; font-weight: 800; }
.number-pad button:hover, .tool-grid button:hover { background: var(--accent-soft); }
.tool-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tool-grid button { min-height: 54px; display: grid; place-items: center; gap: 2px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface-2); cursor: pointer; font-size: .78rem; font-weight: 700; }
.tool-grid button span { font-size: 1.2rem; }
.tool-grid button[aria-pressed="true"] { color: var(--accent); background: var(--accent-soft); border-color: var(--accent); }
.primary-button { min-height: 48px; padding: 0 22px; border: 0; border-radius: 12px; color: white; background: var(--accent); cursor: pointer; font-weight: 800; box-shadow: 0 7px 16px color-mix(in srgb, var(--accent) 25%, transparent); }

.mine-console { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; margin: 8px auto 0; padding: 12px clamp(12px, 3vw, 24px); border: 3px solid #717784; border-top-color: #f4f5f6; border-left-color: #f4f5f6; background: #c5c8ce; color: #111; }
.counter { display: grid; justify-items: start; }
.counter:last-child { justify-items: end; }
.mine-console .counter span { color: #303744; }
.counter strong { min-width: 82px; padding: 4px 8px; border: 3px inset #e5e5e5; color: #ff3232; background: #240909; font-family: Consolas, monospace; font-size: clamp(1.5rem, 5vw, 2.2rem); line-height: 1; letter-spacing: .08em; font-variant-numeric: tabular-nums; }
.face-button { width: 52px; height: 52px; border: 4px outset #ececec; background: #c5c8ce; cursor: pointer; font-size: 1.65rem; line-height: 1; }
.face-button:active { border-style: inset; }
.mine-board-frame { max-width: 100%; margin: 0 auto; overflow: auto; overscroll-behavior: contain; border: 5px inset #e6e6e6; background: #b8bcc3; }
.mine-board { --mine-cell: 30px; width: max-content; min-width: 100%; display: grid; grid-template-columns: repeat(var(--cols, 16), var(--mine-cell)); justify-content: center; background: #b8bcc3; }
.mine-cell { width: var(--mine-cell); height: var(--mine-cell); display: grid; place-items: center; padding: 0; border: 3px outset #e9e9e9; color: #111; background: #c5c8ce; cursor: pointer; font: 800 .98rem/1 Consolas, monospace; }
.mine-cell.is-revealed { border: 1px solid #8b9099; background: #d7d9dd; cursor: default; }
.mine-cell.is-mine { background: #ff6262; }
.mine-cell[data-adjacent="1"] { color: #174dc7; }
.mine-cell[data-adjacent="2"] { color: #16752a; }
.mine-cell[data-adjacent="3"] { color: #cf2828; }
.mine-cell[data-adjacent="4"] { color: #5223a5; }
.mine-cell[data-adjacent="5"] { color: #8c1e1e; }
.mine-cell[data-adjacent="6"] { color: #007d89; }
.mine-cell[data-adjacent="7"] { color: #111; }
.mine-cell[data-adjacent="8"] { color: #666; }
.mine-help { margin: 12px 0 0; color: var(--ink-soft); text-align: center; font-size: .83rem; }
.mine-mode { display: none; grid-template-columns: 1fr 1fr; gap: 4px; margin: 10px 0; padding: 4px; border: 1px solid var(--line); border-radius: 12px; }
.mine-mode button { min-height: 40px; border: 0; border-radius: 8px; background: transparent; font-weight: 700; }
.mine-mode button.is-active { color: #fff; background: var(--accent); }

dialog { width: min(420px, calc(100% - 32px)); padding: 0; border: 1px solid var(--line); border-radius: 20px; color: var(--ink); background: var(--surface); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(6, 12, 22, .56); backdrop-filter: blur(4px); }
.result-dialog form, .settings-dialog form { padding: 30px; display: grid; gap: 16px; text-align: center; }
.result-dialog h2, .result-dialog p, .settings-dialog h2 { margin: 0; }
.result-icon { font-size: 2.5rem; color: var(--amber); }
.settings-dialog label { display: grid; gap: 6px; text-align: left; color: var(--ink-soft); font-size: .84rem; font-weight: 700; }
.settings-dialog input { min-height: 44px; padding: 0 12px; border: 1px solid var(--line); border-radius: 10px; color: var(--ink); background: var(--surface-2); font-size: 1rem; }
.dialog-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.dialog-actions button:not(.primary-button) { min-height: 48px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface-2); }
.form-error { min-height: 1.2em; margin: 0; color: var(--danger); font-size: .85rem; }
[hidden] { display: none !important; }

@media (max-width: 760px) {
  .app-shell { width: min(100% - 20px, 620px); padding-top: 10px; }
  .topbar { grid-template-columns: auto 1fr auto; gap: 8px; }
  .brand > span:last-child { display: none; }
  .game-tabs { justify-self: center; }
  .tab { min-width: 72px; padding: 0 12px; }
  .header-actions { gap: 4px; }
  .icon-button { width: 40px; height: 40px; }
  .game-view { margin-top: 10px; padding: 18px 12px 24px; border-radius: 20px; }
  .game-heading { align-items: center; }
  h1 { font-size: 2rem; }
  .stats-row { margin-bottom: 14px; }
  .sudoku-layout { grid-template-columns: 1fr; gap: 18px; }
  .sudoku-board { width: min(100%, 540px); justify-self: center; }
  .sudoku-controls { width: min(100%, 540px); justify-self: center; gap: 12px; }
  .number-pad { grid-template-columns: repeat(9, 1fr); gap: 4px; }
  .number-pad button { aspect-ratio: .82; min-width: 0; min-height: 42px; border-radius: 8px; font-size: 1.15rem; }
  .tool-grid { grid-template-columns: repeat(3, 1fr); gap: 4px; }
  .tool-grid button { min-width: 0; min-height: 50px; padding: 2px; font-size: .7rem; }
  .mine-mode { display: grid; }
  .mine-board { --mine-cell: 28px; }
  .mine-board.is-wide { min-width: calc(var(--cols) * var(--mine-cell)); justify-content: start; }
}

@media (max-width: 430px) {
  .eyebrow { display: none; }
  .select-field { font-size: 0; }
  .select-field select { max-width: 164px; font-size: .8rem; }
  .sudoku-cell { font-size: 1.16rem; }
  .notes-grid { inset: 2px; font-size: .43rem; }
  .counter strong { min-width: 68px; font-size: 1.45rem; }
  .mine-console { padding-inline: 8px; }
}

@media (prefers-reduced-motion: no-preference) {
  .game-view { animation: rise .28s ease-out; }
  .sudoku-cell, .number-pad button, .tab { transition: background-color .14s ease, color .14s ease, transform .14s ease; }
  .number-pad button:active { transform: translateY(1px) scale(.97); }
  @keyframes rise { from { opacity: 0; transform: translateY(8px); } }
}
