:root {
  --paper: #f4f1e9;
  --paper-deep: #eae5da;
  --ink: #20231f;
  --muted: #777b70;
  --sage: #647564;
  --sage-dark: #465548;
  --sage-pale: #dfe6dd;
  --sage-wash: #e9eee7;
  --clay: #a75948;
  --clay-pale: #f3dfda;
  --line: rgba(32, 35, 31, 0.19);
  --line-strong: #3a4039;
  --radius: 18px;
  --shadow: 0 24px 70px rgba(47, 48, 41, 0.11);
  --ease: cubic-bezier(.2, .75, .25, 1);
}

* { box-sizing: border-box; }

html { min-width: 320px; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: var(--paper);
  font-family: "Avenir Next", "PingFang SC", "Noto Sans CJK SC", sans-serif;
  -webkit-font-smoothing: antialiased;
}

button { font: inherit; }

button:focus-visible {
  outline: 3px solid rgba(100, 117, 100, .35);
  outline-offset: 3px;
}

.paper-noise {
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: .5;
  background-image:
    radial-gradient(circle at 15% 20%, rgba(255,255,255,.7) 0 1px, transparent 1.5px),
    radial-gradient(circle at 80% 60%, rgba(74,68,54,.055) 0 .8px, transparent 1.3px);
  background-size: 17px 17px, 13px 13px;
  mix-blend-mode: multiply;
}

.topbar {
  position: relative;
  z-index: 2;
  width: min(1180px, calc(100% - 48px));
  height: 92px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgba(32, 35, 31, .11);
}

.brand {
  color: inherit;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  width: 35px;
  height: 35px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3px;
  padding: 5px;
  border: 1.5px solid var(--ink);
}

.brand-mark i { background: var(--sage); }
.brand-mark i:nth-child(2), .brand-mark i:nth-child(3) { background: transparent; border: 1px solid var(--sage); }

.brand strong {
  display: block;
  font: 600 22px/1.1 "Songti SC", "STSong", Georgia, serif;
  letter-spacing: .16em;
}

.brand small {
  display: block;
  margin-top: 3px;
  color: var(--muted);
  font-size: 9px;
  letter-spacing: .28em;
}

.quiet-button {
  border: 0;
  background: transparent;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  gap: 7px;
  cursor: pointer;
  transition: color .2s ease;
}

.quiet-button:hover { color: var(--ink); }
.quiet-button svg, .tool-button svg, .primary-button svg {
  width: 19px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.app-shell {
  position: relative;
  z-index: 1;
  width: min(1080px, calc(100% - 48px));
  margin: 54px auto 70px;
  display: grid;
  grid-template-columns: minmax(420px, 572px) minmax(300px, 370px);
  align-items: start;
  justify-content: space-between;
  gap: 76px;
}

.game-column { min-width: 0; }

.board-wrap {
  position: relative;
  width: min(100%, 572px);
  aspect-ratio: 1;
  padding: 13px;
  background: rgba(255,255,255,.43);
  border: 1px solid rgba(255,255,255,.9);
  border-radius: var(--radius);
  box-shadow: var(--shadow), inset 0 0 0 1px rgba(32,35,31,.045);
}

.sudoku-board {
  --size: 9;
  display: grid;
  grid-template-columns: repeat(var(--size), 1fr);
  width: 100%;
  height: 100%;
  overflow: hidden;
  border: 2px solid var(--line-strong);
  background: rgba(255,255,255,.52);
}

.cell {
  position: relative;
  min-width: 0;
  padding: 0;
  border: 0;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  color: var(--sage-dark);
  background: rgba(255,255,255,.34);
  display: grid;
  place-items: center;
  font-family: "Avenir Next", "PingFang SC", sans-serif;
  font-size: clamp(20px, 4.2vw, 31px);
  font-weight: 520;
  cursor: pointer;
  transition: background-color 140ms ease, color 140ms ease, transform 140ms ease;
}

.sudoku-board[data-size="4"] .cell { font-size: clamp(30px, 7vw, 52px); }
.cell.block-right { border-right: 2px solid var(--line-strong); }
.cell.block-bottom { border-bottom: 2px solid var(--line-strong); }
.cell.edge-right { border-right: 0; }
.cell.edge-bottom { border-bottom: 0; }
.cell:hover:not(.selected) { background: rgba(223, 230, 221, .65); }
.cell.peer { background: rgba(224, 226, 216, .48); }
.cell.same { background: rgba(205, 218, 203, .68); color: var(--sage-dark); }
.cell.selected {
  z-index: 1;
  background: var(--sage-pale);
  box-shadow: inset 0 0 0 2px var(--sage);
}
.cell.given { color: var(--ink); font-weight: 650; }
.cell.wrong { color: var(--clay); background: var(--clay-pale); }
.cell.just-filled { animation: cellPop 220ms var(--ease); }

@keyframes cellPop {
  50% { transform: scale(.88); }
}

.board-loading {
  position: absolute;
  inset: 13px;
  z-index: 4;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 13px;
  color: var(--muted);
  font-size: 13px;
  letter-spacing: .08em;
  background: rgba(244, 241, 233, .9);
  backdrop-filter: blur(5px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s ease;
}

.board-loading.show { opacity: 1; pointer-events: auto; }

.loader {
  width: 27px;
  height: 27px;
  border: 2px solid rgba(100,117,100,.2);
  border-top-color: var(--sage);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.number-pad {
  width: min(100%, 572px);
  margin-top: 21px;
  display: grid;
  grid-template-columns: repeat(var(--size), 1fr);
  gap: 7px;
}

.number-button {
  aspect-ratio: 1.14;
  border: 1px solid rgba(32,35,31,.12);
  border-radius: 10px;
  color: var(--sage-dark);
  background: rgba(255,255,255,.54);
  font-size: 20px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(50,50,44,.035);
  transition: transform .15s var(--ease), background .15s ease, color .15s ease;
}

.number-button:hover { transform: translateY(-2px); background: var(--sage); color: white; }
.number-button:active { transform: translateY(0) scale(.97); }
.number-button.complete { color: rgba(70,85,72,.35); background: rgba(255,255,255,.22); }

.tool-row {
  width: min(100%, 572px);
  margin-top: 14px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 7px;
}

.tool-button {
  min-height: 57px;
  border: 0;
  border-radius: 10px;
  color: var(--muted);
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 13px;
  cursor: pointer;
  transition: background .15s ease, color .15s ease;
}
.tool-button:hover { color: var(--ink); background: rgba(255,255,255,.45); }
.tool-button:disabled { opacity: .35; cursor: default; }
.tool-button b { font-size: 10px; padding: 1px 5px; border-radius: 20px; color: white; background: var(--sage); }

.control-panel { padding-top: 15px; }
.desktop-title p, .mobile-title p {
  margin: 0 0 13px;
  color: var(--sage);
  font-size: 11px;
  font-weight: 650;
  letter-spacing: .22em;
}

.desktop-title h1, .mobile-title h1 {
  margin: 0;
  font: 500 44px/1.27 "Songti SC", "STSong", Georgia, serif;
  letter-spacing: .035em;
}

.desktop-title > span {
  display: block;
  margin-top: 15px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.8;
}

.settings-block { margin-top: 35px; }
.settings-block + .settings-block { margin-top: 23px; }
.setting-heading {
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.setting-heading label { font-size: 13px; font-weight: 650; }
.setting-heading span { color: var(--muted); font-size: 11px; }

.segmented {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  padding: 4px;
  border: 1px solid rgba(32,35,31,.09);
  border-radius: 12px;
  background: rgba(226, 222, 212, .54);
}
.segmented.size, .segmented.difficulty { grid-template-columns: repeat(3, 1fr); }
.segmented button {
  min-height: 39px;
  border: 0;
  border-radius: 8px;
  color: var(--muted);
  background: transparent;
  font-size: 13px;
  cursor: pointer;
  transition: all .18s ease;
}
.segmented button:hover { color: var(--ink); }
.segmented button.active {
  color: var(--ink);
  background: rgba(255,255,255,.82);
  box-shadow: 0 3px 9px rgba(35,38,33,.08);
  font-weight: 650;
}

.stats {
  margin-top: 25px;
  padding: 16px 9px;
  display: grid;
  grid-template-columns: 1fr 1px 1fr 1px 1fr;
  text-align: center;
  border-top: 1px solid rgba(32,35,31,.11);
  border-bottom: 1px solid rgba(32,35,31,.11);
}
.stats > div { display: flex; flex-direction: column; gap: 5px; }
.stats span { color: var(--muted); font-size: 10px; letter-spacing: .08em; }
.stats strong { font-size: 14px; font-variant-numeric: tabular-nums; }
.stats strong b { font: inherit; color: var(--clay); }
.stats > i { width: 1px; height: 26px; align-self: center; background: rgba(32,35,31,.1); }

.primary-button {
  width: 100%;
  min-height: 52px;
  margin-top: 24px;
  padding: 0 20px;
  border: 0;
  border-radius: 11px;
  color: white;
  background: var(--sage-dark);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  font-weight: 650;
  letter-spacing: .08em;
  cursor: pointer;
  box-shadow: 0 12px 24px rgba(70,85,72,.18);
  transition: transform .18s var(--ease), background .18s ease, box-shadow .18s ease;
}
.primary-button:hover { transform: translateY(-2px); background: #39473c; box-shadow: 0 16px 28px rgba(70,85,72,.23); }
.primary-button:active { transform: translateY(0); }
.primary-button svg { width: 17px; }
.keyboard-tip { margin: 13px 0 0; text-align: center; color: var(--muted); font-size: 10px; }
.mobile-title { display: none; }

.toast {
  position: fixed;
  z-index: 10;
  left: 50%;
  bottom: 28px;
  padding: 11px 18px;
  border-radius: 9px;
  color: white;
  background: rgba(32,35,31,.9);
  box-shadow: 0 10px 30px rgba(0,0,0,.18);
  font-size: 13px;
  transform: translate(-50%, 18px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease, transform .2s var(--ease);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.win-dialog {
  width: min(460px, calc(100% - 32px));
  padding: 42px 40px 31px;
  border: 0;
  border-radius: 20px;
  color: var(--ink);
  background: var(--paper);
  text-align: center;
  box-shadow: 0 30px 90px rgba(24,27,23,.28);
}
.win-dialog::backdrop { background: rgba(35,39,34,.46); backdrop-filter: blur(4px); }
.win-dialog[open] { animation: dialogIn .32s var(--ease); }
@keyframes dialogIn { from { opacity: 0; transform: translateY(18px) scale(.97); } }
.completion-mark {
  position: relative;
  width: 70px;
  height: 70px;
  margin: 0 auto 22px;
  padding: 7px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 5px;
  border: 1.5px solid var(--sage);
  transform: rotate(4deg);
}
.completion-mark span { background: var(--sage-pale); }
.completion-mark i {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: white;
  font: normal 30px Georgia, serif;
  text-shadow: 0 1px 5px rgba(0,0,0,.15);
}
.completion-mark::after { content: ""; position: absolute; inset: 13px; border-radius: 50%; background: var(--sage); z-index: -0; }
.completion-mark i { z-index: 1; }
.win-dialog > p { margin: 0 0 8px; color: var(--sage); font-size: 11px; font-weight: 700; letter-spacing: .2em; }
.win-dialog h2 { margin: 0; font: 500 34px/1.3 "Songti SC", "STSong", Georgia, serif; }
.result-line { margin: 22px 0 5px; display: flex; justify-content: center; gap: 12px; color: var(--muted); font-size: 11px; }
.result-line span + span::before { content: "·"; margin-right: 12px; }
.win-dialog .primary-button { margin-top: 25px; }
.dialog-close { margin-top: 14px; border: 0; color: var(--muted); background: transparent; cursor: pointer; font-size: 12px; }
.dialog-close:hover { color: var(--ink); }

@media (max-width: 900px) {
  .app-shell { grid-template-columns: minmax(360px, 1.2fr) minmax(260px, .8fr); gap: 36px; }
  .desktop-title h1 { font-size: 36px; }
}

@media (max-width: 720px) {
  .topbar { width: calc(100% - 32px); height: 72px; }
  .topbar .quiet-button { font-size: 0; }
  .topbar .quiet-button svg { width: 22px; }
  .app-shell { width: calc(100% - 24px); margin: 27px auto 44px; display: flex; flex-direction: column; gap: 26px; }
  .game-column, .board-wrap, .number-pad, .tool-row { width: 100%; max-width: 560px; margin-left: auto; margin-right: auto; }
  .mobile-title { display: block; margin: 0 6px 19px; }
  .mobile-title p { margin-bottom: 7px; }
  .mobile-title h1 { font-size: 27px; }
  .desktop-title { display: none; }
  .control-panel { width: 100%; max-width: 560px; margin: 0 auto; padding: 0; display: flex; flex-direction: column; }
  .settings-block { margin-top: 0; }
  .settings-block + .settings-block { margin-top: 18px; }
  .stats { order: 2; margin-top: 22px; }
  .control-panel .primary-button { order: 3; }
  .keyboard-tip { order: 4; }
  .board-wrap { padding: 8px; border-radius: 14px; }
  .board-loading { inset: 8px; }
  .number-pad { margin-top: 14px; gap: 5px; }
  .number-button { border-radius: 8px; font-size: 18px; }
  .tool-row { margin-top: 7px; }
  .tool-button { min-height: 50px; flex-direction: column; gap: 3px; font-size: 10px; }
  .tool-button svg { width: 18px; }
  .cell { font-size: clamp(18px, 6vw, 30px); }
}

@media (max-width: 390px) {
  .app-shell { width: calc(100% - 16px); }
  .board-wrap { padding: 5px; }
  .board-loading { inset: 5px; }
  .number-pad { gap: 3px; }
  .number-button { border-radius: 6px; font-size: 16px; }
  .result-line { gap: 6px; }
  .result-line span + span::before { margin-right: 6px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; scroll-behavior: auto !important; transition-duration: .01ms !important; }
}
