:root {
  --page: #fbfcff;
  --ink: #2f435f;
  --muted: #748197;
  --line: #c0cad9;
  --heavy-line: #2c415e;
  --blue: #2f5eb7;
  --blue-soft: #b9daf8;
  --blue-wash: #e9f1fb;
  --panel: #e9edf5;
  --panel-strong: #dfe6f1;
  --danger: #ca3d44;
  --danger-bg: #ffe6e8;
  --white: #ffffff;
  --shadow: 0 22px 60px rgba(44, 65, 94, 0.12);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  background:
    linear-gradient(180deg, rgba(240, 245, 252, 0.85), rgba(251, 252, 255, 0.5) 280px),
    var(--page);
  color: var(--ink);
  font-family: ui-rounded, "Avenir Next", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  overflow-x: hidden;
}

button {
  border: 0;
  font: inherit;
  color: inherit;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.app-shell {
  width: min(100vw, 1728px);
  margin: 0 auto;
  padding: clamp(18px, 2.3vw, 40px) clamp(14px, 1.8vw, 24px);
}

.game-area {
  width: 100%;
}

.difficulty-tabs {
  display: flex;
  align-items: center;
  gap: clamp(18px, 2.8vw, 46px);
  margin: 0 0 clamp(18px, 2vw, 32px);
  color: var(--muted);
  font-size: clamp(20px, 2.1vw, 31px);
  font-weight: 800;
  line-height: 1;
  white-space: nowrap;
}

.difficulty-label {
  color: #8795aa;
}

.difficulty-tab {
  padding: 0;
  background: transparent;
  color: var(--muted);
  font-weight: 800;
}

.difficulty-tab[aria-pressed="true"] {
  color: var(--blue);
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(540px, 1fr) minmax(420px, 641px);
  gap: clamp(34px, 2.4vw, 42px);
  align-items: start;
}

.board-wrap {
  position: relative;
  width: min(100%, calc(100vh - 150px));
  min-width: 540px;
  aspect-ratio: 1;
}

.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  width: 100%;
  height: 100%;
  border: 4px solid var(--heavy-line);
  background: var(--white);
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  background: var(--white);
  border-right: 2px solid var(--line);
  border-bottom: 2px solid var(--line);
  color: var(--ink);
  font-size: clamp(42px, 5.2vw, 75px);
  font-weight: 500;
  line-height: 1;
  transition: background-color 140ms ease, color 140ms ease, box-shadow 140ms ease;
}

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

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

.cell[data-col="2"],
.cell[data-col="5"] {
  border-right-color: var(--heavy-line);
  border-right-width: 4px;
}

.cell[data-row="2"],
.cell[data-row="5"] {
  border-bottom-color: var(--heavy-line);
  border-bottom-width: 4px;
}

.cell.related {
  background: var(--blue-wash);
}

.cell.same-number {
  background: #dce9f7;
}

.cell.selected,
.cell.selected.same-number {
  background: var(--blue-soft);
  box-shadow: inset 0 0 0 2px rgba(47, 94, 183, 0.18);
}

.cell.fixed {
  color: var(--ink);
}

.cell.user-value {
  color: var(--blue);
}

.cell.error {
  color: var(--danger);
  background: var(--danger-bg);
}

.cell:focus-visible {
  outline: 4px solid rgba(47, 94, 183, 0.45);
  outline-offset: -4px;
  z-index: 2;
}

.notes-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  width: 76%;
  height: 76%;
  align-items: center;
  justify-items: center;
  color: #60708a;
  font-size: clamp(10px, 1.25vw, 18px);
  font-weight: 800;
  line-height: 1;
}

.pause-panel {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(245, 248, 252, 0.92);
  border: 4px solid var(--heavy-line);
  backdrop-filter: blur(6px);
}

.pause-panel[hidden] {
  display: none;
}

.resume-button {
  min-width: 180px;
  min-height: 72px;
  border-radius: 8px;
  background: var(--blue);
  color: var(--white);
  font-size: 28px;
  font-weight: 900;
  box-shadow: var(--shadow);
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 18px;
  width: 100%;
}

.score-row {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: start;
  min-height: 80px;
  color: var(--muted);
  font-size: clamp(24px, 1.9vw, 31px);
  font-weight: 850;
}

.score-row span {
  display: block;
}

.score-row strong {
  color: var(--ink);
  font-weight: 850;
}

.score-zero {
  padding-top: 2px;
  font-size: clamp(34px, 2.6vw, 42px);
}

.mistakes strong,
.timer strong {
  display: block;
  font-size: clamp(28px, 2.1vw, 34px);
}

.timer {
  justify-self: end;
  text-align: right;
}

.timer-line {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
}

.pause-button {
  display: grid;
  place-items: center;
  width: 62px;
  height: 62px;
  border-radius: 50%;
  background: var(--panel);
  color: #7b8ba4;
}

.pause-bars,
.pause-bars::before {
  display: block;
  width: 5px;
  height: 24px;
  border-radius: 999px;
  background: currentColor;
  content: "";
}

.pause-bars {
  position: relative;
  transform: translateX(-5px);
}

.pause-bars::before {
  position: absolute;
  left: 11px;
  top: 0;
}

.tool-row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: clamp(14px, 2vw, 42px);
  padding: 2px 16px 0;
}

.tool-button {
  position: relative;
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  width: 100%;
  border-radius: 50%;
  background: var(--panel);
  color: var(--blue);
  transition: transform 120ms ease, background-color 120ms ease;
}

.tool-button svg {
  width: 45%;
  height: 45%;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.tool-button:hover,
.tool-button.active {
  background: var(--panel-strong);
  transform: translateY(-1px);
}

.tool-button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.tool-badge {
  position: absolute;
  top: -3px;
  right: -7px;
  min-width: 58px;
  height: 34px;
  padding: 0 9px;
  border-radius: 999px;
  background: #aab3c2;
  color: var(--white);
  font-size: 24px;
  font-weight: 900;
  line-height: 34px;
}

.note-tool.active .tool-badge,
.assist-tool.active .tool-badge {
  background: var(--blue);
}

.number-pad {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 13px;
}

.number-button {
  display: grid;
  place-items: center;
  aspect-ratio: 1.05;
  border-radius: 8px;
  background: var(--panel);
  color: var(--blue);
  font-size: clamp(52px, 6vw, 83px);
  font-weight: 500;
  line-height: 1;
  transition: transform 120ms ease, background-color 120ms ease, opacity 120ms ease;
}

.number-button:hover {
  background: var(--panel-strong);
  transform: translateY(-1px);
}

.number-button.complete {
  opacity: 0.36;
  cursor: not-allowed;
  transform: none;
}

.number-button.complete:hover {
  background: var(--panel);
  transform: none;
}

.number-button.impossible,
.number-button.impossible:hover {
  background: #eef1f6;
  color: #a7b0bf;
  cursor: not-allowed;
  opacity: 1;
  transform: none;
}

.action-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 150px;
  gap: 13px;
}

.new-game-button,
.history-button,
.dialog-button {
  width: 100%;
  min-height: 118px;
  border-radius: 8px;
  background: #5f82c8;
  color: var(--white);
  font-size: clamp(30px, 2.7vw, 42px);
  font-weight: 900;
  box-shadow: 0 14px 24px rgba(60, 99, 178, 0.16);
}

.new-game-button:hover,
.history-button:hover,
.dialog-button:hover {
  background: #5478bf;
}

.history-button {
  background: var(--panel);
  color: var(--blue);
  box-shadow: none;
}

.history-button:hover {
  background: var(--panel-strong);
}

.result-dialog,
.history-dialog {
  width: min(460px, calc(100vw - 32px));
  padding: 0;
  border: 0;
  border-radius: 12px;
  background: var(--white);
  color: var(--ink);
  box-shadow: 0 26px 90px rgba(21, 33, 49, 0.28);
}

.history-dialog {
  width: min(560px, calc(100vw - 32px));
}

.result-dialog::backdrop,
.history-dialog::backdrop {
  background: rgba(33, 45, 64, 0.38);
  backdrop-filter: blur(3px);
}

.dialog-content,
.history-content {
  padding: 30px;
}

.dialog-kicker {
  margin: 0 0 6px;
  color: var(--blue);
  font-size: 18px;
  font-weight: 900;
}

.dialog-content h1 {
  margin: 0 0 24px;
  font-size: 42px;
  line-height: 1.1;
}

.history-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.history-head h1 {
  margin: 0;
  font-size: 36px;
  line-height: 1.1;
}

.history-close-button {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--panel);
  color: var(--muted);
  font-size: 30px;
  line-height: 1;
}

.history-list {
  display: grid;
  gap: 10px;
  max-height: min(52vh, 430px);
  overflow-y: auto;
  padding-right: 4px;
}

.history-empty {
  margin: 0;
  padding: 28px 18px;
  border-radius: 8px;
  background: var(--panel);
  color: var(--muted);
  text-align: center;
  font-size: 18px;
  font-weight: 800;
}

.history-item {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 12px;
  padding: 14px;
  border-radius: 8px;
  background: var(--panel);
}

.history-result {
  min-width: 52px;
  padding: 7px 10px;
  border-radius: 999px;
  background: #dce8fb;
  color: var(--blue);
  text-align: center;
  font-size: 16px;
  font-weight: 900;
}

.history-result.lost {
  background: var(--danger-bg);
  color: var(--danger);
}

.history-main {
  min-width: 0;
}

.history-main strong,
.history-meta {
  display: block;
}

.history-main strong {
  font-size: 19px;
}

.history-meta {
  margin-top: 4px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 800;
}

.history-time {
  color: var(--ink);
  font-size: 22px;
  font-weight: 900;
  white-space: nowrap;
}

.history-clear-button {
  width: 100%;
  min-height: 48px;
  margin-top: 18px;
  border-radius: 8px;
  background: #f1f3f7;
  color: var(--muted);
  font-size: 18px;
  font-weight: 900;
}

.history-clear-button:disabled {
  cursor: not-allowed;
  opacity: 0.48;
}

.dialog-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin: 0 0 24px;
}

.dialog-stats div {
  padding: 14px 12px;
  border-radius: 8px;
  background: var(--panel);
}

.dialog-stats dt,
.dialog-stats dd {
  margin: 0;
}

.dialog-stats dt {
  color: var(--muted);
  font-size: 14px;
  font-weight: 800;
}

.dialog-stats dd {
  margin-top: 6px;
  color: var(--ink);
  font-size: 22px;
  font-weight: 900;
}

.dialog-button {
  min-height: 64px;
  font-size: 28px;
}

.dialog-actions {
  display: grid;
  gap: 10px;
}

.dialog-secondary-button {
  width: 100%;
  min-height: 54px;
  border-radius: 8px;
  background: var(--panel);
  color: var(--blue);
  font-size: 22px;
  font-weight: 900;
}

.dialog-secondary-button[hidden] {
  display: none;
}

@media (max-width: 1080px) {
  .app-shell {
    width: min(100vw, 760px);
  }

  .difficulty-tabs {
    gap: 18px;
    overflow-x: auto;
    padding-bottom: 4px;
  }

  .game-layout {
    display: flex;
    flex-direction: column;
    gap: 20px;
  }

  .board-wrap {
    width: 100%;
    min-width: 0;
  }

  .side-panel {
    gap: 14px;
  }

  .tool-row {
    padding: 0;
    gap: 14px;
  }

  .score-row {
    min-height: 62px;
  }

  .new-game-button {
    min-height: 72px;
  }

  .action-row {
    grid-template-columns: minmax(0, 1fr) 120px;
  }
}

@media (max-width: 560px) {
  .app-shell {
    width: 100%;
    padding:
      max(10px, env(safe-area-inset-top))
      max(8px, env(safe-area-inset-right))
      max(12px, env(safe-area-inset-bottom))
      max(8px, env(safe-area-inset-left));
  }

  .difficulty-tabs {
    gap: 7px;
    margin-bottom: 8px;
    padding-bottom: 2px;
    font-size: 15px;
    scrollbar-width: none;
  }

  .difficulty-tabs::-webkit-scrollbar {
    display: none;
  }

  .difficulty-label {
    flex: 0 0 auto;
    padding: 8px 2px 8px 0;
  }

  .difficulty-tab {
    flex: 0 0 auto;
    min-height: 36px;
    padding: 0 11px;
    border-radius: 999px;
    background: var(--panel);
  }

  .difficulty-tab[aria-pressed="true"] {
    background: #dce8fb;
  }

  .game-layout {
    gap: 10px;
  }

  .board-wrap {
    width: min(100%, calc(100svh - 260px));
    margin: 0 auto;
  }

  .board {
    border-width: 3px;
  }

  .cell {
    border-right-width: 1px;
    border-bottom-width: 1px;
    font-size: clamp(26px, 10vw, 40px);
  }

  .cell[data-col="2"],
  .cell[data-col="5"] {
    border-right-width: 3px;
  }

  .cell[data-row="2"],
  .cell[data-row="5"] {
    border-bottom-width: 3px;
  }

  .score-row {
    grid-template-columns: 1fr auto 1fr;
    min-height: 42px;
    font-size: 14px;
  }

  .score-zero {
    font-size: 22px;
  }

  .mistakes strong,
  .timer strong {
    font-size: 18px;
  }

  .timer-line {
    gap: 8px;
  }

  .pause-button {
    width: 38px;
    height: 38px;
  }

  .pause-bars,
  .pause-bars::before {
    width: 4px;
    height: 17px;
  }

  .pause-bars {
    transform: translateX(-4px);
  }

  .pause-bars::before {
    left: 9px;
  }

  .side-panel {
    gap: 9px;
  }

  .tool-row {
    grid-template-columns: repeat(5, 46px);
    justify-content: center;
    gap: 10px;
  }

  .tool-button {
    width: 46px;
    height: 46px;
    aspect-ratio: auto;
  }

  .tool-badge {
    top: -5px;
    right: -10px;
    min-width: 36px;
    height: 22px;
    padding: 0 7px;
    font-size: 14px;
    line-height: 22px;
  }

  .number-pad {
    grid-template-columns: repeat(9, minmax(0, 1fr));
    gap: 5px;
  }

  .number-button {
    min-height: 48px;
    aspect-ratio: auto;
    border-radius: 7px;
    font-size: clamp(24px, 7.8vw, 34px);
    font-weight: 650;
  }

  .action-row {
    grid-template-columns: minmax(0, 1fr) 86px;
    gap: 8px;
  }

  .new-game-button,
  .history-button {
    min-height: 50px;
    font-size: 22px;
  }

  .history-button {
    font-size: 18px;
  }

  .notes-grid {
    width: 82%;
    height: 82%;
    font-size: clamp(8px, 2.6vw, 12px);
  }

  .resume-button {
    min-width: 136px;
    min-height: 54px;
    font-size: 22px;
  }

  .dialog-content,
  .history-content {
    padding: 24px 18px 18px;
  }

  .dialog-content h1,
  .history-head h1 {
    margin-bottom: 18px;
    font-size: 34px;
  }

  .history-head {
    margin-bottom: 12px;
  }

  .history-close-button {
    width: 38px;
    height: 38px;
    font-size: 27px;
  }

  .history-list {
    max-height: min(55vh, 420px);
  }

  .history-item {
    grid-template-columns: auto 1fr;
    gap: 10px;
    padding: 12px;
  }

  .history-time {
    grid-column: 1 / -1;
    justify-self: end;
    font-size: 19px;
  }

  .dialog-stats {
    gap: 8px;
  }

  .dialog-stats div {
    padding: 12px 8px;
  }

  .dialog-stats dd {
    font-size: 18px;
  }
}

@media (max-width: 380px) {
  .difficulty-tabs {
    font-size: 14px;
  }

  .difficulty-tab {
    min-height: 34px;
    padding: 0 10px;
  }

  .board-wrap {
    width: min(100%, calc(100svh - 248px));
  }

  .tool-row {
    grid-template-columns: repeat(5, 42px);
    gap: 8px;
  }

  .tool-button {
    width: 42px;
    height: 42px;
  }

  .number-button {
    min-height: 44px;
    font-size: 25px;
  }

  .new-game-button,
  .history-button {
    min-height: 46px;
  }
}

@media (max-height: 720px) and (max-width: 560px) {
  .app-shell {
    padding-top: max(6px, env(safe-area-inset-top));
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }

  .difficulty-tabs {
    margin-bottom: 6px;
  }

  .difficulty-tab {
    min-height: 32px;
  }

  .game-layout,
  .side-panel {
    gap: 7px;
  }

  .board-wrap {
    width: min(100%, calc(100svh - 222px));
  }

  .score-row {
    min-height: 36px;
  }

  .tool-row {
    grid-template-columns: repeat(5, 40px);
    gap: 8px;
  }

  .tool-button {
    width: 40px;
    height: 40px;
  }

  .number-button {
    min-height: 40px;
  }

  .action-row {
    grid-template-columns: minmax(0, 1fr) 78px;
    gap: 6px;
  }

  .new-game-button,
  .history-button {
    min-height: 42px;
    font-size: 20px;
  }

  .history-button {
    font-size: 16px;
  }
}

@media (max-height: 560px) and (orientation: landscape) {
  .app-shell {
    width: 100%;
    padding:
      max(6px, env(safe-area-inset-top))
      max(12px, env(safe-area-inset-right))
      max(8px, env(safe-area-inset-bottom))
      max(12px, env(safe-area-inset-left));
  }

  .difficulty-tabs {
    gap: 7px;
    margin-bottom: 6px;
    padding-bottom: 0;
    overflow-x: auto;
    font-size: 14px;
    scrollbar-width: none;
  }

  .difficulty-tabs::-webkit-scrollbar {
    display: none;
  }

  .difficulty-label {
    flex: 0 0 auto;
    padding: 7px 2px 7px 0;
  }

  .difficulty-tab {
    flex: 0 0 auto;
    min-height: 32px;
    padding: 0 10px;
    border-radius: 999px;
    background: var(--panel);
  }

  .difficulty-tab[aria-pressed="true"] {
    background: #dce8fb;
  }

  .game-layout {
    display: grid;
    grid-template-columns: minmax(280px, calc(100svh - 56px)) minmax(320px, 1fr);
    gap: 12px;
    align-items: start;
  }

  .board-wrap {
    width: min(100%, calc(100svh - 56px));
    min-width: 0;
  }

  .board {
    border-width: 3px;
  }

  .cell {
    border-right-width: 1px;
    border-bottom-width: 1px;
    font-size: clamp(24px, 7.2svh, 34px);
  }

  .cell[data-col="2"],
  .cell[data-col="5"] {
    border-right-width: 3px;
  }

  .cell[data-row="2"],
  .cell[data-row="5"] {
    border-bottom-width: 3px;
  }

  .side-panel {
    gap: 7px;
  }

  .score-row {
    min-height: 34px;
    font-size: 13px;
  }

  .score-zero {
    font-size: 20px;
  }

  .mistakes strong,
  .timer strong {
    font-size: 17px;
  }

  .timer-line {
    gap: 7px;
  }

  .pause-button {
    width: 34px;
    height: 34px;
  }

  .pause-bars,
  .pause-bars::before {
    width: 4px;
    height: 15px;
  }

  .pause-bars {
    transform: translateX(-4px);
  }

  .pause-bars::before {
    left: 9px;
  }

  .tool-row {
    grid-template-columns: repeat(5, 38px);
    justify-content: center;
    gap: 8px;
    padding: 0;
  }

  .tool-button {
    width: 38px;
    height: 38px;
    aspect-ratio: auto;
  }

  .tool-badge {
    top: -5px;
    right: -10px;
    min-width: 34px;
    height: 20px;
    padding: 0 6px;
    font-size: 13px;
    line-height: 20px;
  }

  .number-pad {
    grid-template-columns: repeat(9, minmax(0, 1fr));
    gap: 5px;
  }

  .number-button {
    min-height: 38px;
    aspect-ratio: auto;
    border-radius: 7px;
    font-size: 25px;
    font-weight: 650;
  }

  .action-row {
    grid-template-columns: minmax(0, 1fr) 82px;
    gap: 6px;
  }

  .new-game-button,
  .history-button {
    min-height: 40px;
    font-size: 20px;
  }

  .history-button {
    font-size: 16px;
  }

  .notes-grid {
    width: 82%;
    height: 82%;
    font-size: clamp(8px, 2.5svh, 11px);
  }
}
