* {
  box-sizing: border-box;
}

:root {
  --ink: #111827;
  --paper: #fffaf0;
  --given: #d8d0c0;
  --page: #f4f1e9;
  --panel: #ffffff;
  --muted: #64748b;
  --accent: #2563eb;
  --danger: #fecaca;
  --success: #bbf7d0;
  --line-thin: 1px;
  --line-thick: 4px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html {
  min-height: 100%;
  background: var(--page);
}

body {
  margin: 0;
  min-height: 100%;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at top left, rgba(255,255,255,0.95), transparent 38rem),
    linear-gradient(135deg, #f8f5ee, #e9eef7);
  color: var(--ink);
}

button,
.cell {
  -webkit-tap-highlight-color: transparent;
}

.app {
  width: min(100%, 760px);
  margin: 0 auto;
  padding: clamp(10px, 3vw, 20px);
  padding-bottom: calc(18px + var(--safe-bottom));
}

.hero {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  align-items: end;
  margin-bottom: 12px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--muted);
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-size: clamp(0.64rem, 2.4vw, 0.76rem);
}

h1 {
  margin: 0;
  font-size: clamp(2.35rem, 12vw, 5rem);
  line-height: 0.95;
  letter-spacing: -0.075em;
}

.subtitle {
  margin: 7px 0 0;
  color: #52627a;
  font-size: clamp(0.9rem, 3.4vw, 1rem);
  max-width: 34rem;
}

.stats,
.game-card,
.rules {
  background: rgba(255,255,255,0.94);
  border-radius: clamp(18px, 5vw, 28px);
  box-shadow: 0 16px 40px rgba(23, 32, 51, 0.11);
}

.stats {
  padding: 10px 14px;
  min-width: 104px;
  text-align: center;
}

.stats span,
.stats strong {
  display: block;
}

.stats span {
  color: var(--muted);
  font-weight: 800;
  font-size: 0.82rem;
}

.stats strong {
  font-size: clamp(1.35rem, 6vw, 1.9rem);
  letter-spacing: -0.04em;
}

.game-card {
  padding: clamp(10px, 3.6vw, 18px);
}

.message {
  min-height: 1.7rem;
  margin-bottom: 10px;
  font-weight: 850;
  color: #334155;
  font-size: clamp(0.95rem, 3.8vw, 1.08rem);
}

.board-wrap {
  width: 100%;
  display: grid;
  place-items: center;
}

.grid {
  display: grid;
  width: min(100%, 530px, calc(100vw - 40px));
  aspect-ratio: 1 / 1;
  background: var(--paper);
  border: var(--line-thick) solid var(--ink);
  border-radius: 6px;
  overflow: hidden;
  gap: 0;
  touch-action: manipulation;
}

.cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--paper);
  color: var(--ink);
  font-size: clamp(1.55rem, 10.5vw, 3rem);
  font-weight: 900;
  line-height: 1;
  cursor: pointer;
  user-select: none;
  min-width: 0;
  min-height: 0;
}

.cell::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  border-style: solid;
  border-color: var(--ink);
  border-width: var(--bt, 1px) var(--br, 1px) var(--bb, 1px) var(--bl, 1px);
}

.cell.given {
  background: var(--given);
}

.cell.selected::after {
  content: "";
  position: absolute;
  inset: 5px;
  border: 3px solid var(--accent);
  border-radius: 2px;
  pointer-events: none;
}

.cell.conflict {
  background: var(--danger);
}

.cell.solved {
  background: var(--success);
}

.number-pad {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: clamp(7px, 2.4vw, 10px);
  width: min(100%, 530px, calc(100vw - 40px));
  margin: 12px auto 10px;
}

.actions {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: clamp(7px, 2.4vw, 10px);
  width: min(100%, 530px, calc(100vw - 40px));
  margin: 0 auto;
}

.number-pad button,
.actions button {
  border: 0;
  border-radius: clamp(12px, 3.5vw, 16px);
  min-height: clamp(46px, 12vw, 58px);
  padding: 10px 8px;
  background: var(--ink);
  color: #fff;
  font-weight: 900;
  font-size: clamp(1rem, 4.8vw, 1.28rem);
  cursor: pointer;
  box-shadow: 0 2px 0 rgba(0,0,0,0.08);
}

.rules {
  margin-top: 12px;
  padding: clamp(14px, 4vw, 20px);
}

.rules h2 {
  margin: 0 0 8px;
  font-size: clamp(1.45rem, 6vw, 2rem);
}

.rules p {
  margin: 8px 0;
  color: #263449;
  line-height: 1.45;
}

@media (max-width: 520px) {
  .app {
    padding-left: 8px;
    padding-right: 8px;
  }

  .hero {
    grid-template-columns: 1fr;
    align-items: stretch;
  }

  .stats {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-inline: 16px;
  }

  .game-card {
    border-radius: 22px;
  }

  .grid,
  .number-pad,
  .actions {
    width: min(100%, calc(100vw - 28px));
  }

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

  .subtitle {
    display: none;
  }
}

@media (max-height: 720px) and (max-width: 520px) {
  .eyebrow,
  .rules {
    display: none;
  }

  h1 {
    font-size: 2.2rem;
  }

  .stats {
    padding-top: 8px;
    padding-bottom: 8px;
  }

  .message {
    margin-bottom: 8px;
  }

  .number-pad {
    margin-top: 10px;
  }

  .number-pad button,
  .actions button {
    min-height: 44px;
  }
}
