:root {
  --bg: #f4f1ea;
  --bg-strong: #e2e8f0;
  --panel: #fffdf8;
  --panel-soft: #f7f3eb;
  --line: rgba(36, 47, 66, 0.14);
  --line-strong: #243042;
  --text: #1b2533;
  --muted: #677487;
  --accent: #c5532f;
  --accent-dark: #8f371d;
  --accent-soft: rgba(197, 83, 47, 0.1);
  --success-soft: rgba(55, 111, 84, 0.12);
  --success-text: #376f54;
  --danger-soft: rgba(164, 63, 54, 0.16);
  --danger-text: #9b352a;
  --shadow: 0 18px 42px rgba(32, 48, 71, 0.12);
}

* {
  box-sizing: border-box;
}

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

body {
  min-height: 100vh;
  background:
    radial-gradient(circle at top left, rgba(255, 255, 255, 0.88), transparent 28%),
    linear-gradient(160deg, var(--bg), var(--bg-strong));
  color: var(--text);
  font-family: "Avenir Next", "Trebuchet MS", "Segoe UI", sans-serif;
}

button,
select {
  font: inherit;
}

button {
  touch-action: manipulation;
}

.app-shell {
  width: 100%;
  min-height: 100vh;
  margin: 0;
  padding: 0;
}

.hero-card,
.board-card,
.controls-card {
  background: var(--panel);
}

.brand-title {
  margin: 0;
  color: var(--accent-dark);
}

.stats-panel {
  display: grid;
}

.stat-card {
  min-width: 0;
}

.stat-label {
  display: block;
  margin: 0;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.stat-card strong {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.difficulty-card select {
  width: 100%;
  border: 1px solid var(--line);
  background: #ffffff;
  color: var(--text);
}

.game-layout {
  display: grid;
}

.board-card {
  min-width: 0;
}

.board-topbar {
  display: flex;
}

.status-badge {
  width: 100%;
  background: var(--success-soft);
  color: var(--success-text);
}

.board-frame {
  width: 100%;
}

.board-grid {
  display: grid;
  grid-template-columns: repeat(9, minmax(0, 1fr));
  grid-template-rows: repeat(9, minmax(0, 1fr));
  width: 100%;
  aspect-ratio: 1 / 1;
  border: 2px solid var(--accent);
  background: #ffffff;
  overflow: hidden;
}

.cell {
  width: 100%;
  height: 100%;
  aspect-ratio: 1 / 1;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid rgba(36, 48, 66, 0.08);
  background: #ffffff;
  color: var(--text);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background-color 0.12s ease, color 0.12s ease;
}

.cell:hover {
  background: #faf5ef;
}

.cell:focus-visible,
.primary-btn:focus-visible,
.secondary-btn:focus-visible,
.keypad-btn:focus-visible,
select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.cell.thick-right {
  border-right: 3px solid var(--line-strong);
}

.cell.thick-bottom {
  border-bottom: 3px solid var(--line-strong);
}

.cell.preset {
  background: #edf1f6;
  font-weight: 800;
}

.cell.selected {
  background: #ffe7df;
}

.cell.peer {
  background: #fbf6f0;
}

.cell.same-value {
  background: #eef6ef;
}

.cell.conflict {
  background: #ffe3df;
  color: var(--danger-text);
}

.cell-value {
  font-weight: 700;
  line-height: 1;
}

.cell:not(.preset) .cell-value {
  color: var(--accent-dark);
}

.notes-grid {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  align-items: center;
  justify-items: center;
}

.notes-grid span {
  line-height: 1;
  color: var(--muted);
}

.action-grid {
  display: grid;
}

.primary-btn,
.secondary-btn,
.keypad-btn {
  border: 1px solid var(--line);
  background: #ffffff;
  color: var(--text);
  cursor: pointer;
}

.primary-btn {
  background: linear-gradient(135deg, var(--accent), #dc8a5f);
  color: #fffaf5;
  font-weight: 700;
}

.secondary-btn.is-active {
  background: var(--accent-soft);
  color: var(--accent-dark);
}

.danger-btn {
  color: var(--danger-text);
}

.keypad-card,
.help-card {
  background: var(--panel-soft);
  border: 1px solid var(--line);
}

.section-title {
  margin: 0;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.keypad {
  display: grid;
}

.keypad-btn {
  aspect-ratio: 1 / 1;
  font-weight: 700;
}

.help-card ul {
  margin: 0;
  color: var(--muted);
}

/* Mobile */
@media (max-width: 899px) {
  .app-shell {
    display: grid;
    grid-template-rows: auto 1fr;
    background: var(--panel);
  }

  .hero-card,
  .board-card,
  .controls-card {
    width: 100%;
    margin: 0;
    border: 0;
    box-shadow: none;
    border-radius: 0;
  }

  .hero-card {
    border-bottom: 1px solid var(--line);
    padding: 0.45rem 0 0;
  }

  .brand-title {
    padding: 0 0.7rem 0.35rem;
    font-size: 0.98rem;
    line-height: 1.05;
  }

  .stats-panel {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 0;
    border-top: 1px solid var(--line);
  }

  .stat-card {
    padding: 0.24rem 0.12rem 0.34rem;
    border-right: 1px solid var(--line);
    text-align: center;
  }

  .stat-card:last-child {
    border-right: 0;
  }

  .stat-label {
    font-size: 0.48rem;
    line-height: 1;
  }

  .stat-card strong,
  .difficulty-card select {
    font-size: 0.72rem;
    line-height: 1;
  }

  .difficulty-card select {
    padding: 0;
    height: 0.9rem;
    border-width: 0;
    background: transparent;
    text-align: center;
    appearance: none;
  }

  .game-layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto;
    gap: 0;
  }

  .board-card {
    padding: 0;
  }

  .board-topbar {
    padding: 0;
  }

  .status-badge {
    margin: 0;
    padding: 0.12rem 0.4rem 0.16rem;
    border-top: 1px solid var(--line);
    border-bottom: 1px solid var(--line);
    font-size: 0.72rem;
    line-height: 1;
    text-align: center;
  }

  .board-frame {
    padding: 0;
  }

  .board-grid {
    border-left: 0;
    border-right: 0;
    border-top: 0;
  }

  .cell-value {
    font-size: clamp(1rem, 5.7vw, 1.55rem);
  }

  .notes-grid span {
    font-size: clamp(0.34rem, 2vw, 0.62rem);
  }

  .controls-card {
    border-top: 1px solid var(--line);
    padding: 0;
  }

  .action-grid {
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 0;
  }

  .primary-btn,
  .secondary-btn {
    min-height: 2.15rem;
    padding: 0.18rem;
    border-width: 0 1px 1px 0;
    font-size: 0.66rem;
    line-height: 1.05;
  }

  .action-grid button:last-child {
    border-right-width: 0;
  }

  .keypad-card,
  .help-card {
    border-width: 0;
    background: transparent;
  }

  .keypad-card {
    width: 50%;
    margin: 0 auto;
  }

  .section-title,
  .help-card {
    display: none;
  }

  .keypad {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0;
    border-top: 1px solid var(--line);
  }

  .keypad-btn {
    border-width: 0 1px 1px 0;
    font-size: 1.3rem;
  }

  .keypad-btn:nth-child(3n) {
    border-right-width: 0;
  }
}

/* Desktop */
@media (min-width: 900px) {
  body {
    padding: 1.2rem;
  }

  .app-shell {
    max-width: 1220px;
    margin: 0 auto;
    padding: 1rem;
  }

  .hero-card,
  .board-card,
  .controls-card {
    border: 1px solid var(--line);
    border-radius: 28px;
    box-shadow: var(--shadow);
  }

  .hero-card {
    padding: 1.35rem 1.5rem;
  }

  .brand-title {
    font-size: 1.8rem;
    line-height: 1;
    letter-spacing: 0.03em;
  }

  .stats-panel {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 0.9rem;
    margin-top: 1rem;
  }

  .stat-card {
    padding: 0.95rem 1rem;
    background: #ffffff;
    border: 1px solid var(--line);
    border-radius: 18px;
  }

  .stat-label {
    font-size: 0.74rem;
    line-height: 1.1;
  }

  .stat-card strong,
  .difficulty-card select {
    margin-top: 0.35rem;
    font-size: 1.15rem;
    line-height: 1.1;
  }

  .difficulty-card select {
    padding: 0.5rem 0.75rem;
    border-radius: 12px;
  }

  .game-layout {
    grid-template-columns: minmax(0, 1.35fr) minmax(320px, 0.78fr);
    gap: 1rem;
    margin-top: 1rem;
    align-items: start;
  }

  .board-card {
    padding: 1.2rem;
  }

  .board-topbar {
    margin-bottom: 0.9rem;
  }

  .status-badge {
    padding: 0.85rem 1rem;
    border-radius: 16px;
    font-size: 0.96rem;
    line-height: 1.15;
    text-align: center;
  }

  .board-grid {
    border-radius: 22px;
  }

  .cell-value {
    font-size: clamp(1.25rem, 2vw, 2rem);
  }

  .notes-grid span {
    font-size: clamp(0.42rem, 0.8vw, 0.72rem);
  }

  .controls-card {
    padding: 1.1rem;
  }

  .action-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.75rem;
  }

  .primary-btn,
  .secondary-btn {
    min-height: 3.1rem;
    padding: 0.8rem 0.9rem;
    border-radius: 16px;
    font-size: 0.92rem;
    line-height: 1.15;
  }

  .primary-btn:hover,
  .secondary-btn:hover,
  .keypad-btn:hover {
    transform: translateY(-1px);
  }

  .keypad-card,
  .help-card {
    padding: 1rem;
    border-radius: 20px;
    margin-top: 1rem;
  }

  .section-title {
    font-size: 0.78rem;
    line-height: 1;
    margin-bottom: 0.85rem;
  }

  .keypad {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.7rem;
  }

  .keypad-btn {
    border-radius: 16px;
    font-size: 1.55rem;
    box-shadow: inset 0 -1px 0 rgba(36, 47, 66, 0.06);
  }

  .help-card {
    display: block;
  }

  .help-card ul {
    padding-left: 1.1rem;
    line-height: 1.55;
  }
}
