*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --c-bg: #f5f5f2;
  --c-surface: #ffffff;
  --c-border-thin: #d0d0cc;
  --c-border-thick: #2a2a2a;
  --c-text: #1a1a1a;
  --c-text-muted: #999;
  --c-given: #1a1a1a;
  --c-user: #1a65c8;
  --c-conflict: #d42b2b;
  --c-note: #5a7aaa;
  --c-selected: #c2d8f8;
  --c-same-num: #98bef0;
  --c-related: #e8f0fb;
  --c-btn: #efefeb;
  --c-btn-hover: #e2e2dc;
  --c-accent: #1a65c8;
  --c-accent-dark: #1250a0;
  --c-accent-light: #e4eef8;
}

@media (prefers-color-scheme: dark) {
  :root {
    --c-bg: #1a1a1e;
    --c-surface: #26262c;
    --c-border-thin: #3a3a44;
    --c-border-thick: #58585f;
    --c-text: #e8e8ee;
    --c-text-muted: #666;
    --c-given: #e8e8ee;
    --c-user: #6aabff;
    --c-conflict: #ff6b6b;
    --c-note: #7a9fcc;
    --c-selected: #1e3a5c;
    --c-same-num: #162d48;
    --c-related: #1e2230;
    --c-btn: #2e2e36;
    --c-btn-hover: #383840;
    --c-accent: #4a90e2;
    --c-accent-dark: #6aabff;
    --c-accent-light: #1a2a3a;
  }
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Segoe UI', sans-serif;
  background: var(--c-bg);
  color: var(--c-text);
  min-height: 100vh;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 20px 12px 32px;
  overflow-x: hidden;
}

.app {
  width: 100%;
  max-width: 460px;
  min-width: 0;
}

/* Header */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

h1 {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.5px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

#timer {
  font-size: 17px;
  font-variant-numeric: tabular-nums;
  font-weight: 500;
  color: var(--c-text-muted);
  min-width: 50px;
  text-align: right;
}

/* Shared button/select base */
select,
button {
  font-family: inherit;
  border-radius: 8px;
  border: 1px solid var(--c-border-thin);
  background: var(--c-btn);
  color: var(--c-text);
  cursor: pointer;
  transition: background 0.12s;
  -webkit-appearance: none;
}

select {
  font-size: 13px;
  padding: 6px 10px;
}

button {
  font-size: 14px;
  padding: 7px 12px;
}

select:hover,
button:hover {
  background: var(--c-btn-hover);
}

#new-game {
  background: var(--c-accent);
  color: white;
  border-color: var(--c-accent);
  font-weight: 500;
}

#new-game:hover {
  background: var(--c-accent-dark);
}

/* Board */
#board {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  border: 2px solid var(--c-border-thick);
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 10px;
  aspect-ratio: 1;
  transition: opacity 0.15s;
}

.cell {
  position: relative;
  background: var(--c-surface);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
  transition: background 0.08s;
  border-right: 1px solid var(--c-border-thin);
  border-bottom: 1px solid var(--c-border-thin);
  min-width: 0;
  min-height: 0;
}

/* Thick borders at 3×3 box boundaries */
.cell.box-l { border-left: 2px solid var(--c-border-thick); }
.cell.box-t { border-top: 2px solid var(--c-border-thick); }

.cell:active {
  opacity: 0.75;
}

/* Highlight order matters: same-num wins over related, selected wins over both */
.cell.related  { background: var(--c-related); }
.cell.same-num { background: var(--c-same-num); }
.cell.selected { background: var(--c-selected); }

/* Number */
.num {
  font-size: clamp(14px, 4.2vw, 24px);
  font-weight: 500;
  line-height: 1;
  color: var(--c-user);
}

.cell.given .num {
  color: var(--c-given);
  font-weight: 650;
}

.cell.conflict .num {
  color: var(--c-conflict);
}

/* Notes grid */
.notes {
  display: none;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  width: 100%;
  height: 100%;
  padding: 2px;
}

.notes span {
  font-size: clamp(6px, 1.5vw, 10px);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--c-note);
  font-weight: 500;
  line-height: 1;
}

/* Controls row */
.controls {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

.controls button {
  flex: 1;
  padding: 10px 4px;
  font-size: 13px;
  font-weight: 500;
}

.controls button.active {
  background: var(--c-accent-light);
  border-color: var(--c-accent);
  color: var(--c-accent);
}

/* Numpad */
#numpad {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 4px;
}

.num-key {
  aspect-ratio: 1;
  font-size: clamp(14px, 3.5vw, 20px);
  font-weight: 600;
  padding: 0;
  border-radius: 8px;
}

/* Win modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(4px);
}

.modal.hidden {
  display: none;
}

.modal-content {
  background: var(--c-surface);
  border-radius: 20px;
  padding: 36px 44px;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.25);
}

.modal-content h2 {
  font-size: 30px;
  font-weight: 700;
  margin-bottom: 8px;
}

.modal-content p {
  color: var(--c-text-muted);
  font-size: 16px;
  margin-bottom: 24px;
}

#play-again {
  background: var(--c-accent);
  color: white;
  border-color: var(--c-accent);
  padding: 12px 28px;
  font-size: 15px;
  font-weight: 500;
}

#play-again:hover {
  background: var(--c-accent-dark);
}

/* Keyboard shortcut hint */
.shortcuts {
  margin-top: 16px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  justify-content: center;
}

.shortcut {
  font-size: 11px;
  color: var(--c-text-muted);
}

kbd {
  font-family: inherit;
  font-size: 10px;
  background: var(--c-btn);
  border: 1px solid var(--c-border-thin);
  border-radius: 4px;
  padding: 1px 5px;
}
