/* ============================================================
   Sudoku — style.css
   Mobile-first, responsive, CSS custom properties for theming
   ============================================================ */

:root {
  /* Colors */
  --color-bg: #ffffff;
  --color-text: #1a1a1a;
  --color-text-muted: #6b7280;

  --color-header-bg: #ffffff;
  --color-header-border: #e5e7eb;

  --color-cell-bg: #ffffff;
  --color-cell-border-thin: #c8d0e0;
  --color-cell-border-thick: #344861;

  --color-cell-selected: #bbdefb;      /* Light blue — selected */
  --color-cell-highlight: #e8f4fd;     /* Same row/col/box */
  --color-cell-same-num: #c8e6f9;      /* Same digit */
  --color-cell-clue: #f0f2f5;          /* Given clue (not editable) */
  --color-cell-error: #ffd7d7;         /* Error — wrong digit */
  --color-cell-error-border: #e53e3e;

  --color-clue-text: #344861;
  --color-user-text: #1a6fa8;
  --color-pencil-text: #5a7fa8;
  --color-error-text: #e53e3e;

  --color-diff-btn: #f3f4f6;
  --color-diff-btn-active: #344861;
  --color-diff-btn-text: #374151;
  --color-diff-btn-active-text: #ffffff;

  --color-num-btn-bg: #f3f4f6;
  --color-num-btn-text: #1a1a1a;
  --color-num-btn-hover: #e5e7eb;

  --color-action-btn-bg: #f3f4f6;
  --color-action-btn-active-bg: #344861;
  --color-action-btn-active-text: #ffffff;

  --color-btn-primary-bg: #344861;
  --color-btn-primary-text: #ffffff;
  --color-btn-secondary-bg: #f3f4f6;
  --color-btn-secondary-text: #374151;
  --color-btn-danger-bg: #e53e3e;
  --color-btn-danger-text: #ffffff;

  --color-modal-overlay: rgba(0, 0, 0, 0.6);
  --color-modal-bg: #ffffff;
  --color-modal-border: #e5e7eb;

  --color-win-accent: #344861;

  /* Sizing */
  --cell-size: min(calc((100vw - 16px) / 9), 56px);
  --board-size: calc(var(--cell-size) * 9);

  /* Border widths */
  --border-thin: 1px;
  --border-thick: 3px;

  /* Typography */
  --font-main: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif;
  --font-mono: 'Courier New', Courier, monospace;
}

/* ============================================================
   Reset
   ============================================================ */

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

html, body {
  height: 100%;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-main);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  overflow-x: hidden;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100%;
  max-width: 600px;
  margin: 0 auto;
  padding: 0 8px;
}

/* ============================================================
   Header
   ============================================================ */

header {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 4px;
  border-bottom: 1px solid var(--color-header-border);
  flex-shrink: 0;
}

header h1 {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  color: var(--color-clue-text);
}

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

#btn-settings,
#btn-stats {
  background: none;
  border: 1px solid var(--color-cell-border-thin);
  color: var(--color-text);
  border-radius: 6px;
  width: 36px;
  height: 36px;
  cursor: pointer;
  font-size: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, border-color 0.15s;
}

#btn-settings:hover,
#btn-stats:hover {
  background: var(--color-diff-btn);
  border-color: var(--color-cell-border-thick);
}

#btn-settings[aria-pressed="true"] {
  background: var(--color-diff-btn-active);
  color: var(--color-diff-btn-active-text);
  border-color: var(--color-diff-btn-active);
}

/* ============================================================
   Difficulty nav
   ============================================================ */

#difficulty-nav {
  display: flex;
  gap: 6px;
  padding: 8px 0;
  width: 100%;
  justify-content: center;
  flex-shrink: 0;
}

.diff-btn {
  flex: 1;
  max-width: 90px;
  padding: 7px 4px;
  background: var(--color-diff-btn);
  color: var(--color-diff-btn-text);
  border: 1.5px solid var(--color-cell-border-thin);
  border-radius: 6px;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
  touch-action: manipulation;
  min-height: 36px;
}

.diff-btn[aria-pressed="true"] {
  background: var(--color-diff-btn-active);
  color: var(--color-diff-btn-active-text);
  border-color: var(--color-diff-btn-active);
}

.diff-btn:hover:not([aria-pressed="true"]) {
  background: var(--color-num-btn-hover);
  border-color: var(--color-cell-border-thick);
}

/* ============================================================
   Timer bar
   ============================================================ */

#timer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 4px 2px;
  flex-shrink: 0;
}

#timer {
  font-size: 1.1rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--color-clue-text);
  letter-spacing: 0.05em;
}

#btn-new-game-header {
  padding: 6px 14px;
  background: var(--color-btn-primary-bg);
  color: var(--color-btn-primary-text);
  border: none;
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  min-height: 34px;
  touch-action: manipulation;
  transition: opacity 0.15s;
}

#btn-new-game-header:hover {
  opacity: 0.85;
}

/* ============================================================
   Board
   ============================================================ */

main {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px 0;
  width: 100%;
}

#board {
  display: grid;
  grid-template-rows: repeat(9, var(--cell-size));
  grid-template-columns: repeat(9, var(--cell-size));
  /* No explicit width/height — grid auto-sizes from track definitions.
     With border-box, explicit width: --board-size would shrink the content
     area by the border widths, causing cells to overflow and clip the outer
     border on all sides. Auto-sizing keeps content = 9×cell-size exactly. */
  border: var(--border-thick) solid var(--color-cell-border-thick);
  background: var(--color-cell-border-thick);
  gap: 0;
  touch-action: manipulation;
}

/* board-row is a CSS grid row container */
.board-row {
  display: contents; /* let cells participate directly in the grid */
}

/* ============================================================
   Cells
   ============================================================ */

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  background: var(--color-cell-bg);
  position: relative;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  outline: none;

  /* Default thin borders (overridden by box boundaries below) */
  border-top: var(--border-thin) solid var(--color-cell-border-thin);
  border-left: var(--border-thin) solid var(--color-cell-border-thin);
  border-bottom: none;
  border-right: none;
}

/* Remove top border on first row cells */
.cell[data-row="0"] {
  border-top: none;
}

/* Remove left border on first col cells */
.cell[data-col="0"] {
  border-left: none;
}

/* Thick right border on cells that end a box column (col 2 and col 5) */
.cell[data-col="2"],
.cell[data-col="5"] {
  border-right: var(--border-thick) solid var(--color-cell-border-thick);
}

/* Thick bottom border on cells that end a box row (row 2 and row 5) */
.cell[data-row="2"],
.cell[data-row="5"] {
  border-bottom: var(--border-thick) solid var(--color-cell-border-thick);
}

/* Focus ring */
.cell:focus-visible {
  outline: 2px solid #1a6fa8;
  outline-offset: -2px;
  z-index: 2;
}

/* ============================================================
   Cell states
   ============================================================ */

.cell.cell-clue {
  background: var(--color-cell-clue);
  cursor: default;
}

.cell.cell-selected {
  background: var(--color-cell-selected);
  z-index: 1;
}

.cell.cell-highlight {
  background: var(--color-cell-highlight);
}

.cell.cell-same-num {
  background: var(--color-cell-same-num);
}

/* Error state — invalid user entry */
.cell.cell-error {
  background: var(--color-cell-error);
}

/* ============================================================
   Cell value display
   ============================================================ */

.cell-value {
  font-size: calc(var(--cell-size) * 0.52);
  font-weight: 700;
  line-height: 1;
  color: var(--color-clue-text);
  display: block;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.cell[data-is-clue="false"] .cell-value,
.cell.cell-user .cell-value {
  color: var(--color-user-text);
}

.cell.cell-error .cell-value {
  color: var(--color-error-text);
}

/* Hide value when cell has pencil marks and no confirmed value */
.cell.pencil-mode-active .cell-value:empty ~ .pencil-grid {
  display: grid;
}

/* ============================================================
   Pencil marks: 3x3 sub-grid within each cell
   ============================================================ */

.pencil-grid {
  display: none;  /* shown only when cell has pencil marks */
  position: absolute;
  inset: 1px;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  pointer-events: none;
}

.pencil-grid span {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell-size) * 0.22);
  font-weight: 500;
  color: var(--color-pencil-text);
  line-height: 1;
}

/* game.js toggles this class to show pencil grid */
.cell.has-pencil .pencil-grid {
  display: grid;
}

.cell.has-pencil .cell-value {
  display: none;
}

/* When cell has a confirmed value, show value and hide pencil grid */
.cell.has-value .cell-value {
  display: block;
}

.cell.has-value .pencil-grid {
  display: none;
}

/* ============================================================
   Cell pop animation on number entry
   ============================================================ */

@keyframes cellPop {
  0%   { transform: scale(1); }
  40%  { transform: scale(1.12); }
  70%  { transform: scale(0.95); }
  100% { transform: scale(1); }
}

.cell.pop {
  animation: cellPop 0.2s ease-out;
}

/* ============================================================
   Number pad
   ============================================================ */

#numpad {
  width: 100%;
  flex-shrink: 0;
  padding: 6px 0 4px;
}

#numpad-digits {
  display: flex;
  gap: 4px;
  justify-content: center;
  margin-bottom: 6px;
}

.num-btn {
  flex: 1;
  max-width: 52px;
  height: 52px;
  min-height: 44px;
  background: var(--color-num-btn-bg);
  color: var(--color-num-btn-text);
  border: 1.5px solid var(--color-cell-border-thin);
  border-radius: 6px;
  font-size: 1.3rem;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
  transition: background 0.1s, transform 0.1s;
}

.num-btn:hover,
.num-btn:active {
  background: var(--color-num-btn-hover);
}

.num-btn:active {
  transform: scale(0.94);
}

.num-btn[data-count="0"] {
  opacity: 0.35;
  pointer-events: none;
}

#numpad-actions {
  display: flex;
  gap: 6px;
  justify-content: center;
}

.action-btn {
  flex: 1;
  max-width: 80px;
  height: 44px;
  min-height: 44px;
  background: var(--color-action-btn-bg);
  color: var(--color-num-btn-text);
  border: 1.5px solid var(--color-cell-border-thin);
  border-radius: 6px;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
  transition: background 0.1s, color 0.1s;
}

.action-btn:hover {
  background: var(--color-num-btn-hover);
}

#btn-pencil[aria-pressed="true"] {
  background: var(--color-action-btn-active-bg);
  color: var(--color-action-btn-active-text);
  border-color: var(--color-action-btn-active-bg);
}

.action-btn:disabled {
  opacity: 0.35;
  pointer-events: none;
}

/* ============================================================
   Focus styles
   ============================================================ */

button:focus-visible {
  outline: 2px solid #1a6fa8;
  outline-offset: 2px;
}

/* ============================================================
   Modals
   ============================================================ */

.modal {
  position: fixed;
  inset: 0;
  background: var(--color-modal-overlay);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 16px;
}

.modal.hidden {
  display: none;
}

.modal-content {
  background: var(--color-modal-bg);
  border: 1px solid var(--color-modal-border);
  border-radius: 12px;
  padding: 28px 24px 24px;
  max-width: 400px;
  width: 100%;
  position: relative;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 12px;
  background: none;
  border: none;
  color: var(--color-text-muted);
  font-size: 1.1rem;
  cursor: pointer;
  padding: 6px;
  border-radius: 4px;
  line-height: 1;
  min-width: 32px;
  min-height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-close:hover {
  background: var(--color-diff-btn);
  color: var(--color-text);
}

.modal-content h2 {
  text-align: center;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--color-clue-text);
  margin-bottom: 18px;
}

.modal-content h3 {
  text-align: center;
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--color-text-muted);
  margin: 18px 0 10px;
}

.modal-body-text {
  text-align: center;
  color: var(--color-text-muted);
  font-size: 0.9rem;
  margin-bottom: 20px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}

/* ============================================================
   Win modal
   ============================================================ */

.modal-win-content {
  text-align: center;
}

#win-celebration {
  font-size: 2.5rem;
  margin: 8px 0 12px;
  animation: celebrationBounce 0.6s ease-out;
}

@keyframes celebrationBounce {
  0%   { transform: scale(0.5); opacity: 0; }
  60%  { transform: scale(1.2); opacity: 1; }
  80%  { transform: scale(0.9); }
  100% { transform: scale(1); }
}

.win-detail {
  font-size: 1rem;
  color: var(--color-text-muted);
  margin-bottom: 6px;
}

.win-best {
  font-size: 1rem;
  font-weight: 700;
  color: #d97706;
  margin: 8px 0 12px;
  animation: newBestPulse 0.5s ease-out;
}

@keyframes newBestPulse {
  0%   { transform: scale(0.8); opacity: 0; }
  50%  { transform: scale(1.1); }
  100% { transform: scale(1); opacity: 1; }
}

/* ============================================================
   Stats modal
   ============================================================ */

.stats-section {
  margin-bottom: 4px;
}

.stats-row {
  display: flex;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
}

.stat-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 64px;
}

.stat-value {
  font-size: 2rem;
  font-weight: 700;
  color: var(--color-clue-text);
}

.stat-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--color-text-muted);
  text-align: center;
}

/* Best times per difficulty */
.best-times-grid {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 18px;
}

.best-time-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 10px;
  background: var(--color-diff-btn);
  border-radius: 6px;
}

.best-time-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--color-text);
}

.best-time-value {
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
  color: var(--color-clue-text);
  font-weight: 700;
}

/* Reset stats section */
#reset-stats-section {
  text-align: center;
  margin-top: 4px;
}

#reset-confirm {
  margin-top: 12px;
}

#reset-confirm p {
  font-size: 0.85rem;
  color: var(--color-text-muted);
  margin-bottom: 10px;
}

#reset-confirm .btn-danger,
#reset-confirm .btn-secondary {
  margin: 0 4px;
}

/* ============================================================
   Settings modal
   ============================================================ */

.settings-list {
  display: flex;
  flex-direction: column;
  gap: 0;
  margin-top: 8px;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 0;
  border-bottom: 1px solid var(--color-cell-border-thin);
}

.settings-row:last-child {
  border-bottom: none;
}

.settings-label {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.settings-name {
  font-size: 0.95rem;
  font-weight: 500;
  color: var(--color-text);
}

.settings-desc {
  font-size: 0.78rem;
  color: var(--color-text-muted);
}

/* Toggle switch */
.toggle-btn {
  flex-shrink: 0;
  position: relative;
  width: 48px;
  height: 26px;
  border-radius: 13px;
  border: none;
  background: var(--color-cell-border-thin);
  cursor: pointer;
  transition: background 0.2s;
  padding: 0;
}

.toggle-btn[aria-checked="true"] {
  background: var(--color-btn-primary-bg);
}

.toggle-thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #ffffff;
  transition: transform 0.2s;
  pointer-events: none;
}

.toggle-btn[aria-checked="true"] .toggle-thumb {
  transform: translateX(22px);
}

/* ============================================================
   Buttons
   ============================================================ */

.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 12px 28px;
  background: var(--color-btn-primary-bg);
  color: var(--color-btn-primary-text);
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  letter-spacing: 0.04em;
  min-height: 44px;
  touch-action: manipulation;
  transition: opacity 0.15s;
}

.btn-primary:hover {
  opacity: 0.85;
}

.btn-secondary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 10px 20px;
  background: var(--color-btn-secondary-bg);
  color: var(--color-btn-secondary-text);
  border: 1.5px solid var(--color-cell-border-thin);
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  min-height: 44px;
  touch-action: manipulation;
  transition: background 0.15s;
}

.btn-secondary:hover {
  background: var(--color-num-btn-hover);
}

.btn-danger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 10px 20px;
  background: var(--color-btn-danger-bg);
  color: var(--color-btn-danger-text);
  border: none;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  min-height: 44px;
  touch-action: manipulation;
  transition: opacity 0.15s;
}

.btn-danger:hover {
  opacity: 0.85;
}

.hidden {
  display: none !important;
}

/* ============================================================
   Responsive — Desktop
   ============================================================ */

@media (min-width: 480px) {
  :root {
    --cell-size: 52px;
  }

  body {
    padding: 0 16px;
  }

  .num-btn {
    max-width: 60px;
    height: 56px;
    font-size: 1.4rem;
  }

  .action-btn {
    max-width: 90px;
    font-size: 0.8rem;
  }
}

@media (min-width: 560px) {
  :root {
    --cell-size: 56px;
  }
}

/* ============================================================
   Responsive — Very small screens
   ============================================================ */

@media (max-width: 340px) {
  :root {
    --cell-size: calc((100vw - 8px) / 9);
  }

  .num-btn {
    font-size: 1.1rem;
    height: 44px;
  }

  .action-btn {
    font-size: 0.7rem;
    padding: 0 4px;
  }

  header h1 {
    font-size: 1.1rem;
  }
}

/* ============================================================
   Safe area for mobile browsers with home indicator
   ============================================================ */

#numpad {
  padding-bottom: max(8px, env(safe-area-inset-bottom, 8px));
}

/* ============================================================
   Dark mode
   ============================================================ */

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #1a1a2e;
    --color-text: #e8eaf0;
    --color-text-muted: #8892a4;

    --color-header-bg: #1a1a2e;
    --color-header-border: #2e3348;

    --color-cell-bg: #16213e;
    --color-cell-border-thin: #2e3a50;
    --color-cell-border-thick: #7b9cc4;

    --color-cell-selected: #1e3a5f;
    --color-cell-highlight: #1a2e45;
    --color-cell-same-num: #1d3550;
    --color-cell-clue: #0f172a;
    --color-cell-error: #3d1515;
    --color-cell-error-border: #ef4444;

    --color-clue-text: #c5d8f0;
    --color-user-text: #60a5fa;
    --color-pencil-text: #7bafd4;
    --color-error-text: #f87171;

    --color-diff-btn: #1e2d42;
    --color-diff-btn-active: #3b82f6;
    --color-diff-btn-text: #c5d8f0;
    --color-diff-btn-active-text: #ffffff;

    --color-num-btn-bg: #1e2d42;
    --color-num-btn-text: #e8eaf0;
    --color-num-btn-hover: #253550;

    --color-action-btn-bg: #1e2d42;
    --color-action-btn-active-bg: #3b82f6;
    --color-action-btn-active-text: #ffffff;

    --color-btn-primary-bg: #3b82f6;
    --color-btn-primary-text: #ffffff;
    --color-btn-secondary-bg: #1e2d42;
    --color-btn-secondary-text: #c5d8f0;
    --color-btn-danger-bg: #dc2626;
    --color-btn-danger-text: #ffffff;

    --color-modal-overlay: rgba(0, 0, 0, 0.75);
    --color-modal-bg: #16213e;
    --color-modal-border: #2e3a50;

    --color-win-accent: #60a5fa;
  }
}
