:root {
  --bg: #eef3f8;
  --surface: #ffffff;
  --ink: #1b2430;
  --muted: #697386;
  --line: #cfd8e6;
  --line-strong: #2f3a4a;
  --blue: #1769d2;
  --blue-soft: #e8f2ff;
  --blue-mid: #cfe4ff;
  --green: #1aa37a;
  --green-soft: #e4f7f0;
  --red: #d64b4b;
  --red-soft: #fff0f0;
  --gold: #b7791f;
  --gold-soft: #fff6df;
  --shadow: 0 18px 42px rgba(30, 45, 70, 0.13);
  --radius: 8px;
  --board-size: min(560px, max(220px, calc(100dvh - 328px)));
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  background:
    linear-gradient(180deg, rgba(255,255,255,0.96), rgba(238,243,248,0.98)),
    repeating-linear-gradient(90deg, rgba(23,105,210,0.028) 0 1px, transparent 1px 54px);
  color: var(--ink);
  font-family: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", sans-serif;
  letter-spacing: 0;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button,
input {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

button:disabled {
  cursor: default;
}

.app-shell {
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr;
  grid-template-columns: minmax(0, 1fr);
  width: 100%;
  min-width: 0;
  max-width: 680px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) 14px calc(14px + env(safe-area-inset-bottom));
}

.top-bar {
  height: 58px;
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
}

.brand {
  justify-self: center;
  display: inline-flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
  color: var(--ink);
  font-weight: 800;
}

.brand-mark {
  width: 27px;
  height: 27px;
  display: inline-grid;
  place-items: center;
}

.brand-mark svg {
  width: 100%;
  height: 100%;
}

.brand-mark rect {
  fill: var(--surface);
  stroke: var(--blue);
  stroke-width: 2;
}

.brand-mark path {
  fill: none;
  stroke: var(--line);
  stroke-width: 1.5;
}

.brand-text {
  font-size: 18px;
  line-height: 1;
}

.icon-button {
  width: 40px;
  height: 40px;
  display: inline-grid;
  place-items: center;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  font-size: 23px;
}

.icon-button:active {
  background: var(--blue-soft);
  color: var(--blue);
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto auto auto auto auto;
  align-content: start;
  gap: 13px;
}

.game-layout > *, .top-bar > *, .sheet, .result-card {
  min-width: 0;
}

.progress-row {
  display: grid;
  grid-template-columns: minmax(90px, 1fr) auto auto;
  align-items: center;
  gap: 9px;
  min-height: 20px;
  padding: 0 2px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}

.progress-track {
  height: 4px;
  overflow: hidden;
  border-radius: 4px;
  background: #dbe4ee;
}

.progress-track span {
  display: block;
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: var(--green);
  transition: width 220ms ease;
}

.save-status {
  color: #4e6b63;
  white-space: nowrap;
}

.save-status::before {
  content: "✓";
  margin-right: 4px;
  color: var(--green);
}

.save-status.saving::before {
  content: "•";
  color: var(--gold);
}

.save-status.save-error {
  color: var(--red);
}

.save-status.save-error::before {
  content: "!";
  color: var(--red);
}

.status-strip {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.status-item {
  min-height: 54px;
  display: grid;
  align-content: center;
  gap: 2px;
  padding: 8px 10px;
  border: 1px solid rgba(207, 216, 230, 0.8);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.76);
  color: var(--muted);
  box-shadow: 0 6px 18px rgba(30, 45, 70, 0.05);
  text-align: left;
}

.status-item span {
  font-size: 12px;
  line-height: 1.15;
}

.status-item strong {
  display: block;
  color: var(--ink);
  font-size: 17px;
  line-height: 1.05;
  white-space: nowrap;
}

.status-button {
  width: 100%;
}

.board-zone {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  place-items: center;
}

.board-frame {
  width: min(100%, var(--board-size));
  max-width: 100%;
  aspect-ratio: 1;
  padding: 0;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.sudoku-board {
  container-type: inline-size;
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  border: 2px solid var(--line-strong);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--line-strong);
  touch-action: manipulation;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  padding: 0;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  color: var(--blue);
  transition: background-color 110ms ease, color 110ms ease;
}

.cell.block-right {
  border-right-color: var(--line-strong);
  border-right-width: 2px;
}

.cell.block-bottom {
  border-bottom-color: var(--line-strong);
  border-bottom-width: 2px;
}

.cell.edge-right {
  border-right: 0;
}

.cell.edge-bottom {
  border-bottom: 0;
}

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

.cell.same {
  background: var(--green-soft);
}

.cell.selected {
  background: var(--blue-mid);
  z-index: 1;
}

.cell.conflict,
.cell.wrong {
  background: var(--red-soft);
}

.cell.bump {
  animation: bump 150ms ease-out;
}

.value {
  font-size: clamp(20px, 6.25cqi, 35px);
  font-weight: 650;
  line-height: 1;
}

.given .value {
  color: var(--ink);
  font-weight: 800;
}

.wrong .value,
.conflict .value {
  color: var(--red);
}

.notes {
  position: absolute;
  inset: 8%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  align-items: center;
  justify-items: center;
  color: #7890a8;
  font-size: clamp(8px, 2.3cqi, 14px);
  font-weight: 700;
  line-height: 1;
}

.tool-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.tool-button {
  height: 62px;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 3px;
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.82);
  color: var(--muted);
  box-shadow: 0 6px 18px rgba(30, 45, 70, 0.06);
}

.tool-button:active,
.tool-button.active {
  background: var(--blue-soft);
  color: var(--blue);
}

.tool-button:disabled {
  opacity: 0.38;
}

.tool-icon {
  font-size: 24px;
  line-height: 1;
}

.tool-button span:last-child {
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
}

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

.number-button {
  height: clamp(48px, 9.5vw, 64px);
  display: grid;
  place-items: center;
  align-content: center;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--blue);
  box-shadow: 0 8px 19px rgba(30, 45, 70, 0.09);
}

.number-button strong {
  font-size: 28px;
  line-height: 0.98;
}

.number-button span {
  color: var(--muted);
  font-size: 10px;
  line-height: 1;
}

.number-button:active {
  background: var(--blue-soft);
}

.number-button.done {
  color: #94a3b8;
  background: #eef2f7;
  box-shadow: none;
}

.game-actions {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 9px;
}

.primary-button,
.secondary-button {
  min-height: 50px;
  border-radius: var(--radius);
  padding: 0 16px;
  font-weight: 800;
}

.primary-button {
  background: var(--blue);
  color: #fff;
  box-shadow: 0 14px 28px rgba(47, 128, 237, 0.23);
}

.secondary-button {
  background: var(--surface);
  color: var(--blue);
  box-shadow: 0 8px 20px rgba(30, 45, 70, 0.08);
}

.sheet-backdrop,
.result-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  place-items: end center;
  padding: 18px;
  background: rgba(19, 31, 48, 0.38);
}

.sheet-backdrop[hidden],
.result-backdrop[hidden] {
  display: none;
}

.sheet,
.result-card {
  width: min(100%, 520px);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.sheet {
  padding: 14px;
  max-height: 90dvh;
  overflow-y: auto;
}

.number-pad.grid-pad { grid-template-columns: repeat(3, minmax(0, 1fr)); max-width: 360px; width: 100%; justify-self: center; }
.number-pad.grid-pad .number-button { height: 48px; }
.number-button.active { background: var(--green-soft); outline: 2px solid var(--green); }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 3px solid var(--green); outline-offset: 2px; }
.record-summary { white-space: pre-line; font-size: 12px; line-height: 1.8; overflow-wrap: anywhere; color: var(--muted); }
.switch-row select { min-height: 44px; border: 1px solid var(--line); border-radius: 4px; background: white; padding: 4px 8px; }
.hint-panel { border-left: 3px solid var(--green); padding: 8px 12px; font-size: 14px; overflow-wrap: anywhere; }
.hint-panel[hidden] { display: none; }
.hint-panel p { margin: 0 0 8px; }
.game-actions button { padding: 6px; font-size: 13px; overflow-wrap: anywhere; }
@media (orientation: landscape) and (max-height: 500px) {
  :root { --board-size: min(65vh, 360px); }
}

.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 44px;
  margin-bottom: 8px;
}

.sheet h2,
.result-card h2 {
  margin: 0;
  font-size: 21px;
}

.difficulty-list {
  display: grid;
  gap: 8px;
}

.difficulty-option {
  min-height: 58px;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 12px;
  padding: 9px 13px;
  border-radius: var(--radius);
  background: #f7f9fc;
  color: var(--ink);
  text-align: left;
}

.difficulty-option strong {
  display: block;
  font-size: 17px;
}

.difficulty-option span {
  color: var(--muted);
  font-size: 12px;
}

.difficulty-option.active {
  background: var(--blue-soft);
  outline: 2px solid var(--blue);
}

.difficulty-dot {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--surface);
  color: var(--blue);
  font-weight: 900;
}

.switch-row {
  min-height: 58px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 8px 0;
  border-top: 1px solid #edf1f6;
  font-weight: 750;
}

.switch-row > span, .stats-heading > div { min-width: 0; overflow-wrap: anywhere; }
.switch-row input, .switch-row select, .local-only { flex-shrink: 0; }

.switch-row input {
  width: 48px;
  height: 28px;
  accent-color: var(--blue);
}

.local-stats {
  margin-top: 12px;
  padding-top: 16px;
  border-top: 1px solid #e6edf4;
}

.stats-heading {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 12px;
}

.stats-heading h3 {
  margin: 2px 0 0;
  font-size: 18px;
}

.section-kicker,
.local-only {
  color: var(--muted);
  font-size: 11px;
  font-weight: 750;
}

.local-only {
  padding: 4px 7px;
  border: 1px solid var(--line);
  border-radius: 4px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  overflow: hidden;
  border: 1px solid #e1e9f1;
  border-radius: var(--radius);
  background: #f7f9fc;
}

.stats-grid div {
  display: grid;
  gap: 3px;
  padding: 13px 8px;
  text-align: center;
}

.stats-grid div + div {
  border-left: 1px solid #e1e9f1;
}

.stats-grid strong {
  color: var(--ink);
  font-size: 19px;
  overflow-wrap: anywhere;
}

.stats-grid span {
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}

.result-backdrop {
  place-items: center;
}

.result-card {
  padding: 25px 20px 20px;
  text-align: center;
}

.result-badge {
  width: 58px;
  height: 58px;
  display: grid;
  place-items: center;
  margin: 0 auto 12px;
  border-radius: 50%;
  background: var(--green-soft);
  color: var(--green);
  font-size: 32px;
  font-weight: 900;
}

.result-card p {
  margin: 8px 0 18px;
  color: var(--muted);
  font-weight: 650;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  z-index: 30;
  max-width: min(calc(100% - 28px), 440px);
  overflow-wrap: anywhere;
  transform: translateX(-50%) translateY(16px);
  opacity: 0;
  pointer-events: none;
  padding: 11px 14px;
  border-radius: var(--radius);
  background: #172033;
  color: #fff;
  font-weight: 800;
  box-shadow: 0 16px 36px rgba(23, 32, 51, 0.25);
  transition: opacity 170ms ease, transform 170ms ease;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.is-busy .app-shell {
  cursor: progress;
}

@keyframes bump {
  0% { transform: scale(1); }
  50% { transform: scale(0.92); }
  100% { transform: scale(1); }
}

@media (min-width: 760px) {
  :root {
    --board-size: min(52vh, 500px);
  }

  .app-shell {
    padding-top: 12px;
    padding-bottom: 12px;
  }

  .top-bar {
    height: 52px;
  }

  .game-layout {
    gap: 10px;
  }

  .status-item {
    min-height: 50px;
  }

  .tool-button {
    height: 56px;
  }

  .number-button {
    height: 54px;
  }

  .primary-button,
  .secondary-button {
    min-height: 48px;
  }
}

@media (max-height: 680px) {
  :root {
    --board-size: max(220px, calc(100dvh - 292px));
  }

  .game-layout {
    gap: 9px;
  }

  .top-bar {
    height: 50px;
  }

  .status-item {
    min-height: 48px;
  }

  .tool-button {
    height: 52px;
  }
}

@media (max-width: 390px) {
  .progress-row {
    grid-template-columns: 1fr auto;
  }

  .progress-track {
    grid-column: 1 / -1;
    grid-row: 2;
  }
}
