/* ===========================
   CSS Variables & Theming
   =========================== */
:root {
  /* Light theme (default) */
  --bg-primary: #f4f6f9;
  --bg-secondary: #ffffff;
  --bg-tertiary: #e8ecf1;
  --text-primary: #1a1a2e;
  --text-secondary: #555770;
  --border-color: #c5cad3;
  --section-border-color: #1a1a2e;
  --accent: #4361ee;
  --accent-hover: #3a56d4;
  --accent-light: rgba(67, 97, 238, 0.1);
  --highlight: rgba(67, 97, 238, 0.15);
  --selected: rgba(67, 97, 238, 0.25);
  --same-number: rgba(67, 97, 238, 0.12);
  --success: #2ec4b6;
  --given-color: #1a1a2e;
  --user-color: #3a56d4;
  --note-color: #7c8aa5;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.08);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.12);
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --transition-fast: 150ms ease;
  --transition-normal: 250ms ease;

  /* Sizing */
  --cell-size: min(7.5vw, calc((100vh - 280px) / 9), 64px);
  --board-gap: 1px;
  --box-border-width: 2.5px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg-primary: #0f0f1a;
    --bg-secondary: #1a1a2e;
    --bg-tertiary: #252540;
    --text-primary: #e8ecf1;
    --text-secondary: #9ca3b4;
    --border-color: #3d3d60;
    --section-border-color: #e8ecf1;
    --accent: #5c7cfa;
    --accent-hover: #748ffc;
    --accent-light: rgba(92, 124, 250, 0.12);
    --highlight: rgba(92, 124, 250, 0.18);
    --selected: rgba(92, 124, 250, 0.3);
    --same-number: rgba(92, 124, 250, 0.14);
    --success: #38d9a9;
    --given-color: #e8ecf1;
    --user-color: #748ffc;
    --note-color: #6c7a96;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.5);
  }
}

/* ===========================
   Reset & Base
   =========================== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}

body {
  font-family: 'Segoe UI', system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1rem clamp(0.75rem, 3vw, 2rem);
  transition: background-color var(--transition-normal), color var(--transition-normal);
  overflow-x: hidden;
}

/* ===========================
   Header
   =========================== */
.game-header {
  width: 100%;
  max-width: 560px;
  text-align: center;
  margin-bottom: 1rem;
}

.game-title {
  font-size: clamp(1.5rem, 4vw, 2rem);
  font-weight: 800;
  letter-spacing: -0.02em;
  background: linear-gradient(135deg, var(--accent), #7c5cfc);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 0.25rem;
}

.game-subtitle {
  font-size: 0.85rem;
  color: var(--text-secondary);
}

/* ===========================
   Stats Bar
   =========================== */
.stats-bar {
  display: flex;
  justify-content: center;
  gap: clamp(1rem, 4vw, 2rem);
  width: 100%;
  max-width: 560px;
  margin-bottom: 0.75rem;
  padding: 0.5rem 0;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
}

.stat-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-secondary);
  font-weight: 600;
}

.stat-value {
  font-size: clamp(1rem, 3vw, 1.25rem);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--text-primary);
}

.difficulty-badge {
  display: inline-block;
  padding: 0.15rem 0.6rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.difficulty-easy { background: var(--success); color: #fff; }
.difficulty-medium { background: #f4a261; color: #fff; }
.difficulty-hard { background: #e76f51; color: #fff; }
.difficulty-expert { background: #9b5de5; color: #fff; }
.difficulty-master { background: linear-gradient(135deg, #f72585, #7209b7); color: #fff; }

/* ===========================
   Controls Row
   =========================== */
.controls-row {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  max-width: 560px;
  margin-bottom: 0.75rem;
  flex-wrap: wrap;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  min-height: 44px;
  min-width: 44px;
  padding: 0.45rem 0.9rem;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition-fast);
  background: var(--bg-secondary);
  color: var(--text-primary);
  box-shadow: var(--shadow-sm);
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

.btn:disabled,
.num-btn:disabled,
.action-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
  box-shadow: var(--shadow-sm);
}

.btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn:active {
  transform: translateY(0);
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn.active {
  background: var(--accent);
  color: #fff;
}

.btn-icon {
  padding: 0.45rem;
  font-size: 1.1rem;
}

/* Difficulty selector */
.difficulty-selector {
  position: relative;
}

.difficulty-dropdown {
  display: none;
  position: absolute;
  top: calc(100% + 4px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-secondary);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  padding: 0.35rem;
  z-index: 100;
  min-width: 140px;
}

.difficulty-dropdown.open {
  display: block;
  animation: fadeInDown 200ms ease;
}

@keyframes fadeInDown {
  from { opacity: 0; transform: translateX(-50%) translateY(-6px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

.diff-option {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.55rem 0.75rem;
  border: none;
  background: transparent;
  border-radius: var(--radius-sm);
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-primary);
  cursor: pointer;
  transition: background var(--transition-fast);
  text-align: left;
}

.diff-option:hover, .diff-option:focus-visible {
  background: var(--accent-light);
}

.diff-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.diff-dot.easy   { background: #2ec4b6; }
.diff-dot.medium { background: #f4a261; }
.diff-dot.hard   { background: #e76f51; }
.diff-dot.expert { background: #9b5de5; }
.diff-dot.master { background: linear-gradient(135deg, #f72585, #7209b7); }

/* ===========================
   Sudoku Board (CSS Grid)
   =========================== */
.board-wrapper {
  width: 100%;
  max-width: 560px;
  aspect-ratio: 1 / 1;
  margin-bottom: 0.75rem;
}

.sudoku-board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  gap: var(--board-gap);
  width: 100%;
  height: 100%;
  background: var(--border-color);
  border-radius: var(--radius-md);
  overflow: hidden;
  box-shadow: var(--shadow-lg);
  aspect-ratio: 1 / 1;
}

.board-wrapper.paused .sudoku-board {
  pointer-events: none;
}

.board-wrapper.paused .cell {
  color: transparent;
}

.board-wrapper.paused .cell .cell-value,
.board-wrapper.paused .cell .notes-grid {
  visibility: hidden;
}

.board-wrapper.paused .sudoku-board::after {
  content: 'Paused';
  position: absolute;
  inset: 0;
  z-index: 4;
  display: flex;
  align-items: center;
  justify-content: center;
  background: color-mix(in srgb, var(--bg-secondary) 88%, transparent);
  color: var(--text-primary);
  font-size: clamp(1.5rem, 8vw, 3rem);
  font-weight: 800;
  letter-spacing: 0;
}

/* ===========================
   Individual Cells
   =========================== */
.cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-secondary);
  font-size: clamp(0.85rem, 3vw, 1.25rem);
  font-weight: 600;
  color: var(--user-color);
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: background-color var(--transition-fast), box-shadow var(--transition-fast);
}

.cell:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  z-index: 2;
}

/* Given (prefilled) cells */
.cell.given {
  color: var(--given-color);
  font-weight: 800;
  cursor: default;
}

/* User-entered cells */
.cell:not(.given):empty::before {
  content: '';
}

/* Highlighted (same row/col/box as selected) */
.board-wrapper.selected .cell.highlighted {
  background: var(--highlight);
}

/* Same number highlight */
.board-wrapper.selected .cell.same-number {
  background: var(--same-number);
}

/* Selected cell */
.cell.selected {
  background: var(--selected) !important;
  box-shadow: inset 0 0 0 2px var(--accent);
  z-index: 1;
}

/* ===========================
   3x3 Box Separators
   =========================== */
.cell.box-right,
.cell.box-bottom,
.cell.cell-bottom {
  border-right: 0;
  border-bottom: 0;
}

.cell[data-col="2"],
.cell[data-col="5"] {
  border-right: var(--box-border-width) solid var(--section-border-color);
}

.cell[data-row="2"],
.cell[data-row="5"] {
  border-bottom: var(--box-border-width) solid var(--section-border-color);
}

/* Outer board thick border ring */
.sudoku-board::before {
  content: '';
  position: absolute;
  inset: 0;
  border: var(--box-border-width) solid var(--section-border-color);
  border-radius: var(--radius-md);
  pointer-events: none;
  z-index: 3;
}

/* ===========================
   Notes Grid (pencil marks inside a cell)
   =========================== */
.notes-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  width: 100%;
  height: 100%;
  position: absolute;
  inset: 0;
}

.note-digit {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(0.4rem, 1.5vw, 0.65rem);
  color: var(--note-color);
  font-weight: 500;
}

.note-digit.active {
  color: var(--accent);
  font-weight: 700;
}

/* Main cell value displayed when not in notes mode */
.cell-value {
  position: relative;
  z-index: 1;
  transition: transform var(--transition-fast);
}

/* ===========================
   Number Pad (below board)
   =========================== */
.numpad-wrapper {
  width: 100%;
  max-width: 560px;
  margin-bottom: 0.75rem;
}

.numpad {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: clamp(3px, 1vw, 6px);
  width: 100%;
}

.num-btn {
  aspect-ratio: 1 / 1.15;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 48px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-size: clamp(1rem, 3.5vw, 1.5rem);
  font-weight: 700;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: all var(--transition-fast);
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  position: relative;
}

.num-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
  background: var(--accent-light);
}

.num-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.num-btn:active {
  transform: translateY(0);
}

.num-btn.completed {
  opacity: 0.35;
  pointer-events: none;
}

.num-btn .count-badge {
  position: absolute;
  top: 2px;
  right: 4px;
  font-size: 0.5rem;
  color: var(--text-secondary);
  font-weight: 600;
}

/* ===========================
   Action Buttons Row (below numpad)
   =========================== */
.actions-row {
  display: flex;
  justify-content: center;
  gap: 0.4rem;
  width: 100%;
  max-width: 560px;
  flex-wrap: wrap;
}

.action-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0.35rem 0.7rem;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-size: 1rem;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: all var(--transition-fast);
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.action-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

.action-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.action-btn.active {
  background: var(--accent);
  color: #fff;
}

/* ===========================
   Theme Toggle
   =========================== */
.theme-toggle {
  position: fixed;
  top: 1rem;
  right: 1rem;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-secondary);
  color: var(--text-primary);
  box-shadow: var(--shadow-md);
  cursor: pointer;
  transition: all var(--transition-fast);
  border: none;
  font-size: 1.2rem;
}

.theme-toggle:hover {
  transform: scale(1.1);
  box-shadow: var(--shadow-lg);
}

.theme-toggle:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ===========================
   Win Overlay
   =========================== */
.win-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  z-index: 200;
  align-items: center;
  justify-content: center;
}

.win-overlay.show {
  display: flex;
}

.win-card {
  background: var(--bg-secondary);
  border-radius: var(--radius-lg);
  padding: clamp(1.5rem, 4vw, 2.5rem);
  text-align: center;
  box-shadow: var(--shadow-lg);
  max-width: 360px;
  width: 90%;
  animation: popIn 400ms ease;
}

@keyframes popIn {
  from { opacity: 0; transform: scale(0.85); }
  to   { opacity: 1; transform: scale(1); }
}

.win-card h2 {
  font-size: 1.75rem;
  margin-bottom: 0.5rem;
}

.win-stats {
  color: var(--text-secondary);
  margin-bottom: 1.25rem;
  line-height: 1.6;
}

/* ===========================
   Responsive Breakpoints
   =========================== */
@media (max-width: 380px) {
  .controls-row { gap: 0.35rem; }
  .btn { padding: 0.4rem 0.65rem; font-size: 0.75rem; }
}

@media (min-width: 1200px) {
  :root {
    --cell-size: 64px;
  }
}

/* ===========================
   Utility & Animations
   =========================== */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border-width: 0;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.08); }
}

.cell.just-placed .cell-value {
  animation: pulse 300ms ease;
}
