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

:root {
  --bg: #c8e6c9;
  --surface: #e8f5e9;
  --text: #222222;
  --text-muted: #666666;
  --primary: #1a73e8;
  --primary-light: #d2e3fc;
  --error: #d93025;
  --error-light: #fce8e6;
  --success: #1e8e3e;
  --given: #222222;
  --user: #1a73e8;
  --hint-color: #1e8e3e;
  --border: #bdbdbd;
  --border-thick: #333333;
  --cell-selected: #e8f0fe;
  --cell-highlight: #f0f4ff;
  --cell-hover: #f5f5f5;
  --shadow: 0 2px 8px rgba(0,0,0,0.1);
  --radius: 8px;
}

.dark {
  --bg: #121212;
  --surface: #1e1e1e;
  --text: #e0e0e0;
  --text-muted: #999999;
  --primary: #8ab4f8;
  --primary-light: #1c3a5f;
  --error: #f28b82;
  --error-light: #3c1f1e;
  --success: #81c995;
  --given: #e0e0e0;
  --user: #8ab4f8;
  --hint-color: #81c995;
  --border: #444444;
  --border-thick: #aaaaaa;
  --cell-selected: #1c3a5f;
  --cell-highlight: #252530;
  --cell-hover: #2a2a2a;
  --shadow: 0 2px 8px rgba(0,0,0,0.4);
}

@media (prefers-color-scheme: dark) {
  :root:not(.light) {
    --bg: #121212;
    --surface: #1e1e1e;
    --text: #e0e0e0;
    --text-muted: #999999;
    --primary: #8ab4f8;
    --primary-light: #1c3a5f;
    --error: #f28b82;
    --error-light: #3c1f1e;
    --success: #81c995;
    --given: #e0e0e0;
    --user: #8ab4f8;
    --hint-color: #81c995;
    --border: #444444;
    --border-thick: #aaaaaa;
    --cell-selected: #1c3a5f;
    --cell-highlight: #252530;
    --cell-hover: #2a2a2a;
    --shadow: 0 2px 8px rgba(0,0,0,0.4);
  }
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

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

/* ===== Header ===== */
header {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 16px 20px 8px;
  width: 100%;
  max-width: 500px;
}

header h1 {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.5px;
}

#darkModeToggle {
  background: none;
  border: none;
  font-size: 1.4rem;
  cursor: pointer;
  color: var(--text);
  padding: 4px 8px;
  border-radius: 50%;
  transition: background 0.2s;
}

#darkModeToggle:hover {
  background: var(--cell-highlight);
}

/* ===== Main ===== */
main {
  width: 100%;
  max-width: 500px;
  padding: 0 12px 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

/* ===== Controls ===== */
.controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  gap: 8px;
}

.control-group {
  display: flex;
  gap: 8px;
  align-items: center;
}

select {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
}

button {
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}

button:hover {
  background: var(--cell-highlight);
}

button:active {
  transform: scale(0.97);
}

#newGameBtn {
  background: var(--primary);
  color: #ffffff;
  border-color: var(--primary);
  font-weight: 600;
}

#newGameBtn:hover {
  opacity: 0.9;
}

.timer {
  font-size: 1.1rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
  min-width: 60px;
  text-align: right;
}

/* ===== Board ===== */
.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  width: min(100%, 450px);
  aspect-ratio: 1;
  border: 2.5px solid var(--border-thick);
  border-radius: 4px;
  overflow: hidden;
  background: var(--border);
  gap: 1px;
  box-shadow: var(--shadow);
}

.cell {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  font-size: clamp(1rem, 4vw, 1.6rem);
  font-weight: 500;
  cursor: pointer;
  transition: background 0.1s;
  position: relative;
  color: var(--user);
  outline: none;
}

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

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

/* Thick borders for 3x3 blocks */
.cell[data-col="2"],
.cell[data-col="5"] {
  border-right: 2px solid var(--border-thick);
}

.cell[data-row="2"],
.cell[data-row="5"] {
  border-bottom: 2px solid var(--border-thick);
}

/* Cell states */
.cell.given {
  color: var(--given);
  font-weight: 700;
}

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

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

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

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

.cell.hint-cell {
  color: var(--hint-color);
}

.cell.just-placed {
  animation: pop 0.2s ease;
}

@keyframes pop {
  0% { transform: scale(0.8); }
  60% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

/* ===== Number Pad ===== */
.numpad {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  width: min(100%, 450px);
}

.num-btn {
  padding: 12px 0;
  font-size: 1.2rem;
  font-weight: 600;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}

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

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

.erase-btn {
  font-size: 1.3rem;
}

/* ===== Action Buttons ===== */
.actions {
  display: flex;
  gap: 8px;
  width: min(100%, 450px);
}

.actions button {
  flex: 1;
  padding: 10px 0;
  font-weight: 500;
}

/* ===== Message ===== */
.message {
  min-height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  font-weight: 600;
  padding: 8px 16px;
  border-radius: var(--radius);
  text-align: center;
  transition: opacity 0.3s;
}

.message.win {
  color: var(--success);
  background: rgba(30, 142, 62, 0.1);
}

.message.error-msg {
  color: var(--error);
}

/* ===== Help Button ===== */
#helpBtn {
  background: none;
  border: 2px solid var(--border);
  font-size: 1.1rem;
  font-weight: 700;
  cursor: pointer;
  color: var(--text-muted);
  width: 32px;
  height: 32px;
  padding: 0;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, color 0.2s;
}

#helpBtn:hover {
  background: var(--primary);
  color: #ffffff;
  border-color: var(--primary);
}

/* ===== Modal ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 900;
  padding: 16px;
  animation: fadeIn 0.2s ease;
}

.modal-overlay[hidden] {
  display: none;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal {
  background: var(--surface);
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.25);
  max-width: 480px;
  width: 100%;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  animation: slideUp 0.25s ease;
}

@keyframes slideUp {
  from { transform: translateY(20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.modal-header h2 {
  font-size: 1.2rem;
  font-weight: 700;
}

.modal-close {
  background: none;
  border: none;
  font-size: 1.6rem;
  cursor: pointer;
  color: var(--text-muted);
  padding: 0 4px;
  line-height: 1;
  border-radius: 4px;
}

.modal-close:hover {
  color: var(--text);
  background: var(--cell-highlight);
}

.modal-body {
  padding: 16px 20px 24px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.modal-body section {
  margin-bottom: 16px;
}

.modal-body section:last-child {
  margin-bottom: 0;
}

.modal-body h3 {
  font-size: 0.95rem;
  font-weight: 700;
  margin-bottom: 6px;
  color: var(--primary);
}

.modal-body p,
.modal-body li {
  font-size: 0.88rem;
  line-height: 1.5;
  color: var(--text);
}

.modal-body ol,
.modal-body ul {
  padding-left: 20px;
}

.modal-body li {
  margin-bottom: 4px;
}

/* Legend swatches */
.legend-swatch {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 2px;
  vertical-align: middle;
  margin-right: 2px;
}

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

.user-swatch {
  background: var(--user);
}

.hint-swatch {
  background: var(--hint-color);
}

.error-swatch {
  background: var(--error);
}

/* ===== Other Games Link ===== */
#otherGames {
  position: fixed;
  bottom: 16px;
  right: 16px;
  padding: 8px 16px;
  background: var(--primary);
  color: #ffffff;
  font-size: 0.85rem;
  font-weight: 600;
  text-decoration: none;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  transition: opacity 0.15s, transform 0.1s;
  z-index: 100;
}

#otherGames:hover {
  opacity: 0.9;
}

#otherGames:active {
  transform: scale(0.97);
}

/* ===== Confetti Canvas ===== */
#confetti {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1000;
}

/* ===== Responsive ===== */
@media (min-width: 600px) {
  header h1 {
    font-size: 1.8rem;
  }

  .numpad {
    grid-template-columns: repeat(10, 1fr);
  }
}

@media (max-height: 600px) {
  header {
    padding: 8px 20px 4px;
  }

  main {
    gap: 6px;
  }

  .num-btn {
    padding: 8px 0;
  }
}
