:root {
  --grid-size: clamp(300px, 90vw, 500px);
  --cell-size: calc((var(--grid-size) - 32px) / 9);
  --gap: 2px;
  --box-gap: 4px;
  --box-border: 3px;

  /* Base colors */
  --color-bg: #ffffff;
  --color-surface: #f5f8fa;
  --color-grid: #41566f;
  --color-text: #333333;
  --color-text-muted: #666666;
  --color-text-subtle: #999999;

  /* Interactive */
  --color-primary: #7986b8;
  --color-warning: #dc3545;
  --color-hint: #5a9a7a;

  /* Highlight — flat tints, no gradients (Octile rules) */
  --color-region-bg: lemonchiffon;
  --color-same-number-bg: powderblue;
  --color-selected-border: #7986b8;
  --color-conflict-bg: rgba(220, 53, 69, 0.06);
  --color-conflict-border: #dc3545;

  /* Spacing */
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 12px;
  --space-lg: 16px;
  --space-xl: 24px;

  /* Ad safe area (for native banner overlay) */
  --ad-safe-bottom: 0px; /* Set by AdMobPlatform.showBanner() to 50px */
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: var(--color-bg);
  padding: 20px;
  min-height: 100vh;
  color: var(--color-text);

  /* Add dynamic bottom padding when banner shows (native overlay) */
  padding-bottom: calc(20px + var(--ad-safe-bottom) + env(safe-area-inset-bottom, 0px));
}

/* ===== LAYER 1: HEADER (status) ===== */
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: var(--grid-size);
  margin-bottom: var(--space-lg);
  min-height: 40px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.header-center {
  display: flex;
  align-items: center;
}

.header-right {
  display: flex;
  align-items: center;
  gap: var(--space-md);
}

#pause-btn {
  width: 36px;
  height: 36px;
  background: none;
  border: 1.5px solid var(--color-text-muted);
  border-radius: 8px;
  cursor: pointer;
  font-size: 1rem;
  color: var(--color-text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.15s;
  touch-action: manipulation;
}

#pause-btn:active {
  transform: scale(0.97);
}

#difficulty-label {
  font-size: 1rem;
  font-weight: 500;
  color: var(--color-text);
  cursor: pointer;
}

#timer {
  font-size: 0.875rem;
  color: var(--color-text-muted);
  font-variant-numeric: tabular-nums;
}

#timer.hidden {
  visibility: hidden;
}

#mistakes {
  font-size: 0.875rem;
  color: var(--color-text-muted);
}

#mistakes.hidden {
  display: none;
}

/* ===== LAYER 2: BOARD (focus) ===== */
#sudoku-grid {
  display: grid;
  grid-template-columns: repeat(9, var(--cell-size));
  grid-template-rows: repeat(9, var(--cell-size));
  gap: var(--gap);
  background: var(--color-grid);
  padding: var(--box-gap);
  border-radius: 4px;
  margin-bottom: var(--space-lg);
}

.cell {
  background: var(--color-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell-size) * 0.5);
  font-weight: 500;
  cursor: pointer;
  user-select: none;
  position: relative;
  border: 2px solid transparent;
  transition: background-color 0.12s ease;
  touch-action: manipulation;
}

.cell.fixed {
  background: whitesmoke;
  font-weight: 700;
  color: var(--color-grid);
}

/* Box separators */
.cell[data-col="2"],
.cell[data-col="5"] {
  border-right: var(--box-border) solid var(--color-grid);
}

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

/* Highlight: flat tints only — no gradients, no stripes, no blur */
.cell.region {
  background-color: var(--color-region-bg);
  z-index: 1;
}

.cell.same-number {
  background-color: var(--color-same-number-bg);
  z-index: 2;
}

.cell.selected {
  box-shadow: inset 0 0 0 2px var(--color-selected-border);
  z-index: 10;
}

/* Peer conflict — quiet, recessive */
.cell.conflict {
  background-color: orange;
  box-shadow: inset 0 0 0 1.5px rgba(220, 53, 69, 0.5);
  z-index: 4;
  color: #8b5c5c;
}

/* Primary conflict — the cell the user just placed */
.cell.conflict-primary {
  background-color: cornsilk;
  box-shadow: inset 0 0 0 2px var(--color-conflict-border);
  color: #7a3a3a;
}

/* During conflict, selected border thins to defer to conflict */
.cell.selected.conflict,
.cell.selected.conflict-primary {
  box-shadow: inset 0 0 0 1px var(--color-selected-border), inset 0 0 0 2px var(--color-conflict-border);
}

.cell.long-press-feedback {
  animation: pulse 0.2s ease-in-out;
}

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

/* Cell value */
.cell-value {
  font-size: calc(var(--cell-size) * 0.5);
  font-weight: 500;
}

.cell.fixed .cell-value {
  font-weight: 700;
}

/* Hint source — subtle visual distinction */
.cell-value.hint-source {
  color: var(--color-hint);
}

/* Notes grid (3x3 inside cell) */
.notes-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  width: 100%;
  height: 100%;
  gap: 1px;
}

.note-cell {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell-size) * 0.2);
  color: var(--color-text-subtle);
}

.note-cell.active {
  color: var(--color-text-muted);
  font-weight: 600;
}

/* ===== LAYER 3: KEYPAD (input) ===== */
#number-pad {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: var(--space-sm);
  width: var(--grid-size);
  margin-bottom: var(--space-md);
}

#number-pad button {
  height: 48px;
  font-size: 1.125rem;
  font-weight: 600;
  background: var(--color-surface);
  border: 1.5px solid #ddd;
  border-radius: 8px;
  cursor: pointer;
  transition: transform 0.1s, background-color 0.15s;
  color: var(--color-text);
  touch-action: manipulation;
}

#number-pad button:active {
  transform: scale(0.97);
  background-color: #e8e8e8;
}

#number-pad button.exhausted {
  opacity: 0.25;
  pointer-events: none;
}

/* ===== LAYER 4: TOOLS (operations) ===== */
#tools {
  display: flex;
  gap: var(--space-md);
  justify-content: center;
  width: var(--grid-size);
  margin-bottom: var(--space-lg);
}

.tool-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: var(--space-sm) var(--space-md);
  background: none;
  border: 1.5px solid transparent;
  border-radius: 10px;
  cursor: pointer;
  transition: background-color 0.15s, border-color 0.15s;
  min-width: 56px;
  min-height: 56px;
  touch-action: manipulation;
}

.tool-btn:active {
  transform: scale(0.97);
  background-color: var(--color-surface);
}

.tool-icon {
  font-size: 1.25rem;
  color: var(--color-text-muted);
  line-height: 1;
}

.tool-label {
  font-size: 0.625rem;
  font-weight: 500;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* Notes button — only tool that gets primary color when active */
.tool-btn.active {
  border-color: var(--color-primary);
  background-color: rgba(121, 134, 184, 0.08);
}

.tool-btn.active .tool-icon,
.tool-btn.active .tool-label {
  color: var(--color-primary);
}

body.notes-mode {
  cursor: crosshair;
}

/* ===== PAUSE OVERLAY ===== */
#pause-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 0.95);
  z-index: 500;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-lg);
}

#pause-overlay.visible {
  display: flex;
}

#pause-overlay h2 {
  font-size: 1.5rem;
  color: var(--color-grid);
  margin-bottom: var(--space-sm);
}

.pause-actions {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  width: 200px;
}

.pause-actions button {
  padding: var(--space-md) var(--space-lg);
  font-size: 1rem;
  border: 1.5px solid var(--color-grid);
  border-radius: 8px;
  background: var(--color-bg);
  color: var(--color-grid);
  cursor: pointer;
  transition: background-color 0.15s;
  touch-action: manipulation;
}

.pause-actions button:active {
  background-color: var(--color-surface);
}

.pause-actions button.primary {
  background: var(--color-grid);
  color: white;
}

/* ===== SETTINGS MODAL ===== */
#settings-modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 2000;
  align-items: center;
  justify-content: center;
}

#settings-modal.visible {
  display: flex;
}

#settings-modal .modal-content {
  background: white;
  padding: var(--space-xl);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.2);
  max-width: 360px;
  width: 90%;
}

#settings-modal h2 {
  font-size: 1.25rem;
  color: var(--color-grid);
  margin-bottom: var(--space-lg);
  text-align: center;
}

.setting-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: var(--space-md) 0;
  border-bottom: 1px solid #eee;
}

.setting-row:last-of-type {
  border-bottom: none;
}

.setting-label {
  font-size: 0.9rem;
  color: var(--color-text);
}

/* Toggle switch */
.toggle {
  position: relative;
  width: 44px;
  height: 24px;
  background: #ccc;
  border-radius: 12px;
  cursor: pointer;
  transition: background-color 0.2s;
  border: none;
  padding: 0;
  touch-action: manipulation;
}

.toggle.on {
  background: var(--color-primary);
}

.toggle::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  background: white;
  border-radius: 50%;
  transition: transform 0.2s;
}

.toggle.on::after {
  transform: translateX(20px);
}

/* Segmented control (for mistake limit) */
.segment-control {
  display: flex;
  gap: 0;
  border: 1.5px solid #ddd;
  border-radius: 8px;
  overflow: hidden;
}

.segment-btn {
  padding: 6px 14px;
  font-size: 0.8rem;
  background: var(--color-bg);
  border: none;
  border-right: 1px solid #ddd;
  cursor: pointer;
  color: var(--color-text-muted);
  transition: background-color 0.15s;
  touch-action: manipulation;
}

.segment-btn:last-child {
  border-right: none;
}

.segment-btn.active {
  background: var(--color-primary);
  color: white;
}

.settings-done {
  display: block;
  width: 100%;
  margin-top: var(--space-lg);
  padding: var(--space-md);
  background: var(--color-grid);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  cursor: pointer;
  touch-action: manipulation;
}

/* ===== HELP MODAL ===== */
#help-modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 2000;
  align-items: center;
  justify-content: center;
}

#help-modal.visible {
  display: flex;
}

#help-modal .modal-content {
  background: white;
  padding: var(--space-xl);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.2);
  max-width: 380px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
}

#help-modal h2 {
  font-size: 1.25rem;
  color: var(--color-grid);
  margin-bottom: var(--space-md);
  text-align: center;
}

#help-modal h3 {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--color-grid);
  margin-top: var(--space-lg);
  margin-bottom: var(--space-sm);
}

.help-rule {
  font-size: 0.9rem;
  line-height: 1.6;
  color: var(--color-text);
}

.help-list {
  list-style: none;
  padding: 0;
}

.help-list li {
  font-size: 0.85rem;
  line-height: 1.5;
  color: var(--color-text-muted);
  padding: var(--space-xs) 0;
  border-bottom: 1px solid #f0f0f0;
}

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

.help-contact {
  font-size: 0.8rem;
  color: var(--color-text-subtle);
  margin-top: var(--space-lg);
  text-align: center;
}

.help-contact a {
  color: var(--color-primary);
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* ===== ABOUT MODAL ===== */
#about-modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 2000;
  align-items: center;
  justify-content: center;
}

#about-modal.visible {
  display: flex;
}

#about-modal .modal-content {
  background: white;
  padding: var(--space-xl);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.2);
  max-width: 340px;
  width: 90%;
  text-align: center;
}

#about-modal h2 {
  font-size: 1.25rem;
  color: var(--color-grid);
  margin-bottom: var(--space-lg);
}

.about-app-name {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--color-grid);
  margin-bottom: var(--space-xs);
}

.about-version {
  font-size: 0.8rem;
  color: var(--color-text-subtle);
  margin-bottom: var(--space-xs);
}

.about-byline {
  font-size: 0.85rem;
  color: var(--color-text-muted);
  margin-bottom: var(--space-lg);
}

.about-links {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  align-items: center;
}

.about-link {
  font-size: 0.9rem;
  color: var(--color-primary);
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* ===== DIFFICULTY MODAL ===== */
#difficulty-modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 2000;
  align-items: center;
  justify-content: center;
}

#difficulty-modal.visible {
  display: flex;
}

#difficulty-modal .modal-content {
  background: white;
  padding: var(--space-xl);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.2);
  text-align: center;
  max-width: 400px;
  width: 90%;
}

#difficulty-modal h2 {
  margin-bottom: var(--space-xl);
  color: var(--color-grid);
  font-size: 1.25rem;
}

.difficulty-options {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  margin-bottom: var(--space-lg);
}

.difficulty-btn {
  padding: var(--space-lg);
  background: var(--color-surface);
  border: 1.5px solid #ddd;
  border-radius: 8px;
  cursor: pointer;
  transition: border-color 0.15s;
  display: flex;
  flex-direction: column;
  align-items: center;
  touch-action: manipulation;
}

.difficulty-btn:active {
  border-color: var(--color-primary);
  background-color: rgba(121, 134, 184, 0.06);
}

.difficulty-name {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--color-grid);
  margin-bottom: var(--space-xs);
}

[data-difficulty="EASY"] .difficulty-name {
  color: #5a7a8a;
}

[data-difficulty="HARD"] .difficulty-name {
  color: #2c3e50;
}

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

.close-btn {
  padding: var(--space-sm) var(--space-lg);
  background: none;
  border: 1.5px solid #ccc;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--color-text-muted);
  touch-action: manipulation;
}

/* ===== COMPLETION / GAME OVER MODAL ===== */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.35);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  opacity: 0;
  transition: opacity 0.3s;
}

.modal.show {
  opacity: 1;
}

.modal > .modal-content {
  background: white;
  padding: var(--space-xl);
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
  text-align: center;
  max-width: 90%;
  width: 320px;
  animation: modalSlideIn 0.25s ease-out;
}

@keyframes modalSlideIn {
  from { transform: translateY(-30px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.modal-content h2 {
  font-size: 1.5rem;
  margin-bottom: var(--space-md);
  color: var(--color-grid);
}

.modal-line {
  font-size: 1rem;
  margin-bottom: var(--space-sm);
  color: var(--color-text-muted);
  line-height: 1.6;
}

.modal > .modal-content button {
  background: var(--color-grid);
  color: white;
  border: none;
  padding: var(--space-md) var(--space-xl);
  font-size: 1rem;
  font-weight: 500;
  border-radius: 8px;
  cursor: pointer;
  transition: transform 0.15s;
  min-height: 44px;
  touch-action: manipulation;
  margin-top: var(--space-sm);
}

.modal > .modal-content button:active {
  transform: scale(0.97);
}

.modal > .modal-content button.secondary {
  background: none;
  color: var(--color-text-muted);
  border: 1.5px solid #ddd;
}

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  margin-top: var(--space-md);
}

/* Octile cross-promotion */
.octile-promo {
  font-size: 0.8rem;
  color: var(--color-text-subtle);
  margin-top: var(--space-md);
  text-align: center;
}

.octile-promo a {
  color: var(--color-primary);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: border-bottom-color 0.2s;
  margin-left: 0.25rem;
}

.octile-promo a:hover {
  border-bottom-color: var(--color-primary);
}

/* ===== TOAST NOTIFICATIONS ===== */
#toast-container {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 3000;
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: none;
}

.toast {
  background: white;
  padding: var(--space-md) var(--space-xl);
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  font-size: 0.875rem;
  max-width: 300px;
  text-align: center;
  animation: slideIn 0.3s ease-out, slideOut 0.3s ease-in 2.7s;
  animation-fill-mode: forwards;
  pointer-events: all;
  color: var(--color-text);
}

.toast.info { border-left: 4px solid #5294e2; }
.toast.success { border-left: 4px solid #2ecc71; }
.toast.warning { border-left: 4px solid #f39c12; }
.toast.error { border-left: 4px solid #e74c3c; }

@keyframes slideIn {
  from { transform: translateY(-100%); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

@keyframes slideOut {
  from { transform: translateY(0); opacity: 1; }
  to { transform: translateY(-100%); opacity: 0; }
}

/* ===== RESPONSIVE ===== */
@media (prefers-reduced-motion: reduce) {
  .modal > .modal-content,
  .toast {
    animation: none !important;
  }
}

@media (max-width: 767px) {
  body {
    padding: 10px;
    padding-bottom: calc(10px + var(--ad-safe-bottom) + env(safe-area-inset-bottom, 0px));
  }

  .modal {
    align-items: flex-end;
  }

  .modal > .modal-content {
    width: 100%;
    max-width: 100%;
    max-height: 55vh;
    overflow: auto;
    border-radius: 16px 16px 0 0;
    margin: 0;
    padding-bottom: calc(var(--space-xl) + env(safe-area-inset-bottom, 0px));
    animation: slideUpFromBottom 0.28s ease-out;
  }

  @keyframes slideUpFromBottom {
    from { transform: translateY(100%); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
  }

  #number-pad {
    gap: var(--space-xs);
  }

  #number-pad button {
    font-size: 1rem;
    height: 44px;
  }

  #tools {
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
}

/* Update Banner (OTA) */
#update-banner {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 9999;
  background: linear-gradient(135deg, #0f3460, #1a5276);
  border-bottom: 2px solid #f1c40f;
  padding: 12px 16px;
  text-align: center;
  animation: slideDown 0.3s ease-out;
}
@keyframes slideDown { from { transform: translateY(-100%); } to { transform: translateY(0); } }
#update-banner.show { display: block; }
#update-banner p { color: #eee; font-size: 13px; margin-bottom: 8px; }
#update-banner .update-actions { display: flex; justify-content: center; gap: 12px; }
#update-banner .btn-update {
  padding: 8px 20px; font-size: 14px; font-weight: 700;
  border: none; border-radius: 6px; cursor: pointer;
  background: #f1c40f; color: #1a1a2e;
}
#update-banner .btn-update:hover { background: #f39c12; }
#update-banner .btn-dismiss {
  padding: 8px 16px; font-size: 14px;
  border: 1px solid #555; border-radius: 6px; cursor: pointer;
  background: none; color: #888;
}
#update-banner .btn-dismiss:hover { border-color: #888; color: #bbb; }
#update-banner.force {
  background: linear-gradient(135deg, #b85c00, #c0392b);
  border-bottom-color: #e74c3c;
}
