:root {
  color-scheme: light;
  --bg: #f4f0e8;
  --ink: #1f2933;
  --muted: #66727f;
  --panel: #fffdf8;
  --line: #d8d0c2;
  --soft: #ede6d9;
  --cell: #fbfaf6;
  --cell-alt: #f0eadf;
  --accent: #227c7a;
  --accent-2: #c05a36;
  --accent-3: #465f9e;
  --surface: #ffffff;
  --surface-soft: #faf7ef;
  --given: #111820;
  --selected: #d9ece9;
  --related: #ece6d9;
  --same-number: #dbe6fa;
  --board-line: #1f2933;
  --button-bg: #1f2933;
  --button-ink: #ffffff;
  --texture: rgba(31, 41, 51, .04);
  --top-sheen: rgba(255, 255, 255, .72);
  --panel-bg: rgba(255, 253, 248, .94);
  --backdrop: rgba(31, 41, 51, .28);
  --shadow: 0 18px 42px rgba(57, 45, 31, .13);
  --radius: 8px;
  --board-size: min(92vw, 64vh, 590px);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #161a1c;
  --ink: #edf1f2;
  --muted: #a9b4b8;
  --panel: #202629;
  --line: #3b464a;
  --soft: #2a3235;
  --cell: #1d2326;
  --cell-alt: #242c2f;
  --accent: #48bbb5;
  --accent-2: #ef8b68;
  --accent-3: #91a9ed;
  --surface: #252c30;
  --surface-soft: #20272a;
  --given: #f4f6f7;
  --selected: #244b49;
  --related: #30383b;
  --same-number: #273d5d;
  --board-line: #75848a;
  --button-bg: #e4eaec;
  --button-ink: #182024;
  --texture: rgba(227, 236, 239, .035);
  --top-sheen: rgba(100, 126, 134, .08);
  --panel-bg: rgba(32, 38, 41, .96);
  --backdrop: rgba(0, 0, 0, .56);
  --shadow: 0 18px 42px rgba(0, 0, 0, .28);
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }

body {
  margin: 0;
  background:
    linear-gradient(90deg, var(--texture) 1px, transparent 1px),
    linear-gradient(var(--texture) 1px, transparent 1px),
    linear-gradient(180deg, var(--top-sheen), transparent 280px),
    var(--bg);
  background-size: 30px 30px;
  color: var(--ink);
  overflow-x: hidden;
}

button, select {
  border-radius: var(--radius);
  color: inherit;
  font: inherit;
  -webkit-tap-highlight-color: transparent;
}

button {
  border: 0;
  cursor: pointer;
  touch-action: manipulation;
}

button:focus-visible,
select:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.app {
  width: min(1240px, 100%);
  min-height: 100dvh;
  margin: 0 auto;
  padding: max(14px, env(safe-area-inset-top)) 14px max(18px, env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.brand {
  display: flex;
  align-items: center;
  min-width: 0;
  gap: 12px;
}

.mark {
  display: grid;
  grid-template-columns: repeat(2, 22px);
  gap: 3px;
  padding: 7px;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.mark span {
  display: grid;
  width: 22px;
  height: 22px;
  place-items: center;
  border-radius: 4px;
  background: var(--soft);
  color: var(--accent);
  font-size: .72rem;
  font-weight: 950;
}

.mark span:nth-child(2) { background: var(--accent); }
.mark span:nth-child(3) { color: var(--accent-2); }
.mark span:nth-child(4) { background: var(--accent-3); color: white; font-size: .62rem; }

h1 {
  margin: 0;
  font-size: clamp(1.55rem, 5vw, 2.5rem);
  line-height: 1;
  letter-spacing: 0;
}

.brand p {
  margin: 5px 0 0;
  color: var(--muted);
  font-weight: 750;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.select-wrap select {
  width: 64px;
  min-height: 44px;
  border: 1px solid var(--line);
  background: var(--panel);
  font-weight: 900;
  text-align: center;
}

.icon-button {
  display: grid;
  width: 44px;
  height: 44px;
  place-items: center;
  border: 1px solid var(--line);
  background: var(--panel);
  box-shadow: 0 8px 20px rgba(57, 45, 31, .09);
}

.icon-button svg {
  width: 23px;
  height: 23px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2.2;
}

.theme-icon-sun { display: none; }
html[data-theme="dark"] .theme-icon-moon { display: none; }
html[data-theme="dark"] .theme-icon-sun { display: block; }

.game-shell {
  display: grid;
  grid-template-columns: minmax(210px, 250px) minmax(420px, 620px) minmax(230px, 290px);
  align-items: start;
  justify-content: center;
  gap: 16px;
  min-width: 0;
}

.panel {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel-bg);
  box-shadow: var(--shadow);
  padding: 14px;
}

.control-panel, .assistant-panel {
  display: grid;
  gap: 12px;
}

.field {
  display: grid;
  gap: 6px;
}

.field span,
.stat-grid span {
  display: block;
  color: var(--muted);
  font-size: .76rem;
  font-weight: 900;
  text-transform: uppercase;
}

.field select {
  min-height: 44px;
  border: 1px solid var(--line);
  background: var(--surface);
  font-weight: 900;
  padding: 0 10px;
}

.field small {
  color: var(--muted);
  font-size: .76rem;
  line-height: 1.35;
}

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

.stat-grid > div {
  min-height: 70px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: linear-gradient(180deg, var(--surface), var(--surface-soft));
  padding: 10px;
}

.stat-grid strong {
  display: block;
  margin-top: 7px;
  font-size: 1.28rem;
  line-height: 1;
}

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.text-button {
  min-height: 44px;
  background: var(--button-bg);
  color: var(--button-ink);
  font-weight: 900;
  padding: 0 11px;
}

.text-button.secondary {
  border: 1px solid var(--line);
  background: linear-gradient(180deg, var(--surface), var(--surface-soft));
  color: var(--ink);
}

.text-button:disabled {
  cursor: not-allowed;
  opacity: .45;
}

.board-zone {
  display: grid;
  justify-items: center;
  gap: 10px;
  min-width: 0;
}

.status {
  min-height: 24px;
  color: var(--muted);
  font-weight: 850;
  text-align: center;
}

.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  width: var(--board-size);
  height: var(--board-size);
  border: 3px solid var(--board-line);
  border-radius: 8px;
  background: var(--board-line);
  box-shadow: 0 22px 48px rgba(57, 45, 31, .19);
  overflow: hidden;
}

.cell {
  position: relative;
  display: grid;
  min-width: 0;
  min-height: 0;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 0;
  background: var(--cell);
  color: var(--ink);
  font-size: clamp(1.28rem, 5vw, 2.25rem);
  font-weight: 850;
  line-height: 1;
  user-select: none;
}

.cell:nth-child(6n + 1) {
  background: var(--cell-alt);
}

.cell.box-right { border-right: 3px solid var(--board-line); }
.cell.box-bottom { border-bottom: 3px solid var(--board-line); }
.cell.given { color: var(--given); font-weight: 950; }
.cell.selected { background: var(--selected); box-shadow: inset 0 0 0 3px var(--accent); }
.cell.related { background: var(--related); }
.cell.same-number { background: var(--same-number); color: var(--accent-3); }
.cell.user { color: var(--accent); }
.notes {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  width: 100%;
  height: 100%;
  padding: 5%;
  color: var(--muted);
  font-size: clamp(.48rem, 1.7vw, .76rem);
  font-weight: 900;
  line-height: 1;
}

.notes span {
  display: grid;
  place-items: center;
}

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

.board-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  width: var(--board-size);
  gap: 6px;
}

.number-button {
  display: grid;
  min-height: 50px;
  place-items: center;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--accent);
  font-size: 1.35rem;
  font-weight: 950;
  box-shadow: 0 8px 18px rgba(57, 45, 31, .08);
}

.number-button.done {
  color: var(--muted);
  opacity: .55;
}

.number-pad.note-mode .number-button {
  color: var(--accent-3);
  outline: 2px solid rgba(70, 95, 158, .24);
  outline-offset: -3px;
}

.toggle-row {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--soft);
}

.board-mode-controls {
  width: var(--board-size);
}

.mode-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 44px;
}

.mode-label {
  margin: 0;
  color: var(--muted);
  font-size: .78rem;
  font-weight: 900;
  text-transform: uppercase;
}

.undo-button {
  min-height: 44px;
  border: 0;
  background: transparent;
  color: var(--accent);
  font-weight: 900;
  padding: 0 4px;
  height: 44px;
}

.undo-button::before {
  content: "\21B6";
  font-size: 1.3rem;
  line-height: 1;
  margin-right: 5px;
}

.undo-button:disabled {
  color: var(--muted);
  cursor: not-allowed;
  opacity: .5;
}

.segmented {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  background: transparent;
  font-weight: 900;
}

.segmented svg {
  width: 18px;
  height: 18px;
  margin-right: 6px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.segmented.active {
  background: var(--panel);
  box-shadow: 0 4px 14px rgba(57, 45, 31, .11);
}

.coach,
.progress-list {
  border-top: 1px solid var(--line);
  padding-top: 12px;
}

.coach h2,
.progress-list h2 {
  margin: 0 0 7px;
  font-size: 1rem;
}

.coach p {
  margin: 0;
  color: var(--muted);
  line-height: 1.45;
  font-weight: 750;
}

#digitProgress {
  display: grid;
  gap: 7px;
}

.digit-row {
  display: grid;
  grid-template-columns: 24px 1fr 24px;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-weight: 900;
}

.digit-row .bar {
  height: 8px;
  border-radius: 999px;
  background: var(--soft);
  overflow: hidden;
}

.digit-row .bar span {
  display: block;
  height: 100%;
  width: 0;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--accent), var(--accent-3));
}

.toast {
  position: fixed;
  left: 50%;
  bottom: max(18px, env(safe-area-inset-bottom));
  z-index: 20;
  max-width: min(92vw, 430px);
  transform: translate(-50%, 18px);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
  color: var(--ink);
  font-weight: 850;
  opacity: 0;
  pointer-events: none;
  padding: 11px 14px;
  transition: opacity .18s ease, transform .18s ease;
}

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

.dialog {
  width: min(92vw, 430px);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
  box-shadow: var(--shadow);
  padding: 0;
}

.dialog::backdrop {
  background: var(--backdrop);
}

.dialog form {
  display: grid;
  gap: 12px;
  margin: 0;
  padding: 18px;
}

.dialog h2,
.dialog p {
  margin: 0;
}

.dialog p,
.dialog li {
  color: var(--muted);
  line-height: 1.45;
}

.dialog ul {
  display: grid;
  gap: 6px;
  margin: 0;
  padding-left: 20px;
}

@media (max-width: 1200px) {
  :root { --board-size: min(94vw, 58vh, 590px); }

  .game-shell {
    grid-template-columns: 1fr;
  }

  .board-zone {
    order: -1;
  }

  .control-panel {
    grid-template-columns: minmax(150px, 1fr) 2fr;
    align-items: end;
  }

  .stat-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-column: 1 / -1;
  }

  .actions {
    grid-column: 1 / -1;
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .assistant-panel {
    grid-template-columns: minmax(180px, 240px) 1fr;
  }

  .progress-list {
    border-top: 0;
    padding-top: 0;
  }
}

@media (max-width: 560px) {
  :root { --board-size: min(94vw, 430px); }

  .app { padding-inline: 9px; }
  .brand p { display: none; }
  .mark { grid-template-columns: repeat(2, 18px); }
  .mark span { width: 18px; height: 18px; font-size: .62rem; }

  .top-actions {
    gap: 6px;
  }

  .control-panel,
  .assistant-panel {
    grid-template-columns: 1fr;
  }

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

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

  .cell {
    font-size: clamp(1.05rem, 7vw, 1.7rem);
  }

  .number-pad {
    gap: 4px;
  }

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

  .notes {
    padding: 3%;
    font-size: clamp(.5rem, 2.5vw, .7rem);
  }
}

@media (max-width: 700px) and (orientation: landscape) {
  :root { --board-size: min(88vw, 76dvh, 430px); }

  .cell {
    font-size: clamp(1rem, 4vw, 1.45rem);
  }
}

@media (max-width: 380px) {
  .mark { display: none; }
  .select-wrap select { width: 52px; min-height: 44px; }
}

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