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

body {
  margin: 0;
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  color: #e5e5e5;
  background: #121212;
  min-height: 100vh;
  padding: 0.5rem;
}

.header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
  padding: 0.5rem 0;
}

.back-link {
  font-size: 0.95rem;
  color: #a0a0a0;
  text-decoration: none;
}

.back-link:hover,
.back-link:focus {
  color: #f5f5f5;
  text-decoration: underline;
}

.header h1 {
  margin: 0;
  font-size: 1.75rem;
  color: #f5f5f5;
}

.timer {
  font-variant-numeric: tabular-nums;
  font-size: 1rem;
  color: #a0a0a0;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

#open-stats {
  min-height: 44px;
  min-width: 44px;
  padding: 0.5rem 0.75rem;
  font-size: 1rem;
  font-weight: 500;
  border: 1px solid #505050;
  border-radius: 6px;
  background: #252525;
  color: #e5e5e5;
  cursor: pointer;
}

#open-stats:hover,
#open-stats:focus {
  background: #333;
  border-color: #666;
}

#hint {
  min-height: 44px;
  min-width: 44px;
  padding: 0.5rem 0.75rem;
  font-size: 1rem;
  font-weight: 500;
  border: 1px solid #505050;
  border-radius: 6px;
  background: #252525;
  color: #e5e5e5;
  cursor: pointer;
}

#hint:hover,
#hint:focus {
  background: #333;
  border-color: #666;
}

#difficulty {
  min-height: 44px;
  padding: 0.5rem 0.75rem;
  font-size: 1rem;
  border: 1px solid #505050;
  border-radius: 6px;
  background: #252525;
  color: #e5e5e5;
}

#difficulty option {
  background: #252525;
  color: #e5e5e5;
}

#new-game {
  min-height: 44px;
  min-width: 44px;
  padding: 0.5rem 1rem;
  font-size: 1rem;
  font-weight: 500;
  border: 1px solid #505050;
  border-radius: 6px;
  background: #404040;
  color: #f5f5f5;
  cursor: pointer;
}

#new-game:hover,
#new-game:focus {
  background: #505050;
}

#new-game:active {
  background: #333;
}

.main {
  max-width: 480px;
  margin: 0 auto;
}

/* Sudoku grid: 9x9, square, responsive */
.sudoku-grid {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  width: min(90vmin, 400px);
  aspect-ratio: 1;
  margin: 0 auto 1.5rem;
  border: 2px solid #404040;
  background: #404040;
  gap: 1px;
}

.sudoku-grid .cell {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  background: #252525;
  font-size: clamp(14px, 4vmin, 22px);
  font-weight: 500;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  border: none;
  padding: 0;
  color: #e5e5e5;
}

.sudoku-grid .cell.given {
  font-weight: 700;
  color: #f5f5f5;
  cursor: default;
}

.sudoku-grid .cell.filled {
  color: #93c5fd;
}

.sudoku-grid .cell.selected {
  background: #1e3a5f;
}

.sudoku-grid .cell.error {
  background: #451a1a;
  color: #f87171;
}

.sudoku-grid .cell.cell-hinted {
  background: #422006;
  color: #fbbf24;
  font-weight: 700;
  box-shadow: inset 0 0 0 2px #d97706;
}

.sudoku-grid .cell.cell-hinted.selected {
  background: #78350f;
  box-shadow: inset 0 0 0 2px #d97706;
}

.sudoku-grid .cell:nth-child(9n) {
  margin-right: 0;
}

/* 3x3 box borders: thicker every 3rd */
.sudoku-grid .cell:nth-child(3n) {
  border-right: 2px solid #404040;
}

.sudoku-grid .cell:nth-child(n+19):nth-child(-n+27),
.sudoku-grid .cell:nth-child(n+46):nth-child(-n+54),
.sudoku-grid .cell:nth-child(n+73):nth-child(-n+81) {
  border-bottom: 2px solid #404040;
}

.sudoku-grid .cell:nth-child(9n+1) {
  border-left: 2px solid #404040;
}

.sudoku-grid .cell:nth-child(-n+9) {
  border-top: 2px solid #404040;
}

/* Number controls */
.number-controls {
  margin-bottom: 1.5rem;
}

.number-controls-label {
  margin: 0 0 0.5rem;
  font-size: 0.9rem;
  color: #a0a0a0;
}

.number-buttons {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 0.5rem;
  max-width: 280px;
  margin: 0 auto;
}

.num-btn {
  min-height: 44px;
  padding: 0.5rem;
  font-size: 1.1rem;
  font-weight: 500;
  border: 1px solid #505050;
  border-radius: 8px;
  background: #252525;
  color: #e5e5e5;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.num-btn:hover,
.num-btn:focus {
  background: #333;
  border-color: #666;
}

.num-btn:active {
  background: #404040;
}

.num-btn-clear {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.5rem;
}

.clear-icon {
  width: 22px;
  height: 22px;
  pointer-events: none;
  stroke: currentColor;
}

/* Stats modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal[hidden] {
  display: none;
}

.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  cursor: pointer;
}

.modal-content {
  position: relative;
  width: 100%;
  max-width: 400px;
  max-height: 85vh;
  overflow: auto;
  background: #1e1e1e;
  border: 1px solid #333;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  padding: 1.25rem;
  color: #e5e5e5;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.25rem;
  color: #f5f5f5;
}

.modal-close {
  width: 36px;
  height: 36px;
  padding: 0;
  font-size: 1.5rem;
  line-height: 1;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: #a0a0a0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.modal-close:hover,
.modal-close:focus {
  background: #333;
  color: #f5f5f5;
}

.total-games {
  margin: 0 0 1rem;
  font-weight: 600;
  color: #e5e5e5;
}

.history-heading {
  margin: 0 0 0.5rem;
  font-size: 1rem;
  color: #a0a0a0;
}

.history-list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.history-list li {
  padding: 0.35rem 0;
  border-bottom: 1px solid #333;
  font-size: 0.9rem;
  color: #a0a0a0;
}

.history-list li:last-child {
  border-bottom: none;
}

.clear-history-btn {
  display: block;
  margin-top: 1.25rem;
  padding: 0.5rem 0.75rem;
  font-size: 0.9rem;
  border: 1px solid #dc2626;
  border-radius: 6px;
  background: transparent;
  color: #f87171;
  cursor: pointer;
}

.clear-history-btn:hover,
.clear-history-btn:focus {
  background: #451a1a;
}

/* Win message */
.win-message {
  margin-bottom: 1rem;
  padding: 0.75rem;
  background: #14532d;
  color: #86efac;
  border-radius: 8px;
  font-weight: 500;
  text-align: center;
}
