/**
 * Sudoku Game Styles
 * Adaptive styles with CSS variables, light/dark theme support via prefers-color-scheme
 * Optimized for 480×600 iframe embedding
 */

:root {
  /* Light theme colors */
  --bg-primary: #ffffff;
  --bg-secondary: #f8f9fa;
  --text-primary: #1a1a1a;
  --text-secondary: #6b7280;
  --border-color: #e5e7eb;
  --border-thick: #374151;
  --cell-bg: #ffffff;
  --cell-bg-active: #eff6ff;
  --cell-bg-highlight: #f0f9ff;
  --cell-bg-same-number: #dbeafe;
  --cell-bg-conflict: #fee2e2;
  --cell-bg-correct: #d1fae5;
  --cell-text: #1a1a1a;
  --cell-text-given: #111827;
  --cell-text-user: #2563eb;
  --btn-primary-bg: #2563eb;
  --btn-primary-hover: #1d4ed8;
  --btn-primary-text: #ffffff;
  --btn-secondary-bg: #f3f4f6;
  --btn-secondary-hover: #e5e7eb;
  --btn-secondary-text: #374151;
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  --cell-active-glow: rgba(37, 99, 235, 0.25);
  --transition: 150ms ease-in-out;
}

@media (prefers-color-scheme: dark) {
  :root {
    /* Dark theme colors */
    --bg-primary: #111827;
    --bg-secondary: #1f2937;
    --text-primary: #f9fafb;
    --text-secondary: #9ca3af;
    --border-color: #374151;
    --border-thick: #6b7280;
    --cell-bg: #1f2937;
    --cell-bg-active: #1e3a8a;
    --cell-bg-highlight: #1e40af;
    --cell-bg-same-number: #1e3a8a;
    --cell-bg-conflict: #7f1d1d;
    --cell-bg-correct: #065f46;
    --cell-text: #f9fafb;
    --cell-text-given: #ffffff;
    --cell-text-user: #60a5fa;
    --btn-primary-bg: #3b82f6;
    --btn-primary-hover: #2563eb;
    --btn-primary-text: #ffffff;
    --btn-secondary-bg: #374151;
    --btn-secondary-hover: #4b5563;
    --btn-secondary-text: #f9fafb;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
    --cell-active-glow: rgba(59, 130, 246, 0.35);
  }
  
  .sudoku-cell.same-number {
    box-shadow: 0 0 0 1px var(--btn-primary-bg), 0 0 8px rgba(59, 130, 246, 0.4);
  }
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
  width: 100vw;
  height: 100vh;
  touch-action: manipulation;
}

.sudoku-root {
  width: 100%;
  height: 100%;
  min-width: 480px;
  min-height: 600px;
  display: flex;
  flex-direction: column;
  padding: 4px;
  max-width: 480px;
  max-height: 600px;
  margin: 0 auto;
  overflow: hidden;
  position: relative;
}

/* Header */
.game-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 2px;
  margin-bottom: 2px;
  flex-shrink: 0;
  gap: 8px;
  margin-left: 30px;
  margin-right: 30px;
}

.header-left {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
  padding-left: 0;
}

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

.game-header .btn-primary {
  margin-right: 0;
}

.btn-secondary {
  background-color: var(--btn-secondary-bg);
  color: var(--btn-secondary-text);
}

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

.progress-indicator {
  font-size: 11px;
  color: var(--text-secondary);
  font-weight: 500;
}

.game-title {
  font-size: 20px;
  font-weight: 700;
  color: var(--text-primary);
  margin: 0;
}

.btn {
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 500;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: background-color var(--transition), transform var(--transition);
  min-height: 36px;
  min-width: 36px;
  touch-action: manipulation;
  user-select: none;
}

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

.btn-primary {
  background-color: var(--btn-primary-bg);
  color: var(--btn-primary-text);
}

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

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

/* Main game area */
.game-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  /*justify-content: space-between;*/
  gap: 2px;
  overflow: hidden;
  min-height: 0;
  padding: 0;
}

.grid-container {
  width: 100%;
  max-width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-shrink: 1;
  min-height: 0;
  max-height: calc(100% - 140px);
}

.sudoku-grid {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  gap: 0;
  border: 2px solid var(--border-thick);
  background-color: var(--bg-primary);
  aspect-ratio: 1;
  width: 100%;
  max-width: 85%;
  flex-shrink: 1;
  touch-action: none;
}

.sudoku-cell {
  aspect-ratio: 1;
  border: 1px solid var(--border-color);
  background-color: var(--cell-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(14px, 4vw, 16px);
  font-weight: 500;
  color: var(--cell-text);
  cursor: pointer;
  user-select: none;
  transition: background-color var(--transition), transform var(--transition), border var(--transition), box-shadow var(--transition);
  position: relative;
  min-height: 0;
  min-width: 0;
}

/* Animation classes */
.sudoku-cell.cell-enter {
  animation: cellEnter 200ms ease-out;
}

.sudoku-cell.cell-exit {
  animation: cellExit 200ms ease-out;
}

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

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

/* Thick borders for 3×3 blocks */
/* Right border for columns 3 and 6 (cells 3, 6, 12, 15, 21, 24, etc.) */
.sudoku-cell:nth-child(9n+3),
.sudoku-cell:nth-child(9n+6) {
  border-right: 2px solid var(--border-thick);
}

/* Bottom border for rows 3 and 6 (cells 19-27, 46-54) */
.sudoku-cell:nth-child(n+19):nth-child(-n+27),
.sudoku-cell:nth-child(n+46):nth-child(-n+54) {
  border-bottom: 2px solid var(--border-thick);
}

/* Cell states */
.sudoku-cell.given {
  color: var(--cell-text-given);
  font-weight: 600;
  background-color: var(--cell-bg);
}

.sudoku-cell.user-input {
  color: var(--cell-text-user);
}

.sudoku-cell.active {
  background-color: var(--cell-bg-active);
  z-index: 1;
  border: 2px solid var(--btn-primary-bg) !important;
  box-shadow: 0 0 0 2px var(--cell-active-glow), 0 2px 6px rgba(0, 0, 0, 0.15);
  transform: scale(1.03);
}

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

.sudoku-cell.conflict {
  background-color: var(--cell-bg-conflict);
}

.sudoku-cell.same-number {
  background-color: var(--cell-bg-same-number);
  border: 2px solid var(--btn-primary-bg);
  box-shadow: 0 0 0 1px var(--btn-primary-bg), 0 0 8px rgba(37, 99, 235, 0.3);
  z-index: 1;
}

.sudoku-cell.correct:not(.conflict):not(.active):not(.highlight) {
  background-color: var(--cell-bg-correct);
}

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

/* Numpad */
.numpad-container {
  width: 100%;
  display: flex;
  justify-content: center;
  flex-shrink: 0;
  padding: 0;
  margin-top: 10px;
  transition: opacity var(--transition), transform var(--transition), max-height var(--transition);
  max-height: 200px;
  overflow: hidden;
}

.numpad-container.hidden {
  opacity: 0;
  max-height: 0;
  margin-top: 0;
  transform: translateY(-10px);
  pointer-events: none;
}

.numpad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 3px;
  width: 100%;
  max-width: 260px;
}

.numpad-btn {
  min-height: 28px;
  min-width: 28px;
  padding: 4px;
  font-size: 13px;
  font-weight: 600;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  background-color: var(--btn-secondary-bg);
  color: var(--btn-secondary-text);
  cursor: pointer;
  transition: background-color var(--transition), transform var(--transition), border-color var(--transition);
  touch-action: manipulation;
  user-select: none;
}

.numpad-btn:hover {
  background-color: var(--btn-secondary-hover);
  border-color: var(--border-thick);
}

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

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

.numpad-btn.active {
  background-color: var(--btn-primary-bg);
  color: var(--btn-primary-text);
  border-color: var(--btn-primary-bg);
}

.numpad-btn-erase {
  grid-column: span 3;
  font-size: 11px;
  font-weight: 500;
  margin-top: 1px;
  min-height: 26px;
  padding: 3px;
}

/* Status message */
.status-message {
  position: absolute;
  top: -100px;
  left: 50%;
  transform: translateX(-50%);
  padding: 8px 16px;
  background-color: var(--bg-secondary);
  color: var(--text-primary);
  border-radius: 6px;
  font-size: 14px;
  opacity: 0;
  transition: opacity var(--transition), top var(--transition);
  pointer-events: none;
  z-index: 1000;
}

.status-message.show {
  top: 60px;
  opacity: 1;
}

/* Responsive adjustments for very small screens */
@media (max-width: 320px) {
  .game-title {
    font-size: 20px;
  }

  .btn {
    padding: 6px 12px;
    font-size: 12px;
  }

  .numpad-btn {
    font-size: 16px;
    padding: 10px;
  }
}

/* Modal */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--transition), visibility var(--transition);
}

.modal.show {
  opacity: 1;
  visibility: visible;
}

.modal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(2px);
}

.modal-content {
  position: relative;
  background-color: var(--bg-primary);
  border-radius: 8px;
  max-width: 90%;
  max-height: 90vh;
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow-md);
  z-index: 2001;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transform: scale(0.95);
  transition: transform var(--transition);
}

.modal.show .modal-content {
  transform: scale(1);
}

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

.modal-header h2 {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  color: var(--text-primary);
}

.modal-close {
  background: none;
  border: none;
  font-size: 28px;
  line-height: 1;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: background-color var(--transition), color var(--transition);
}

.modal-close:hover {
  background-color: var(--bg-secondary);
  color: var(--text-primary);
}

.modal-close:focus-visible {
  outline: 2px solid var(--btn-primary-bg);
  outline-offset: 2px;
}

.modal-body {
  padding: 16px;
  overflow-y: auto;
  color: var(--text-primary);
}

.modal-body ol,
.modal-body ul {
  margin: 0 0 16px 0;
  padding-left: 24px;
}

.modal-body li {
  margin-bottom: 8px;
  line-height: 1.5;
}

.modal-body h3 {
  margin: 16px 0 8px 0;
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
}

/* Win Modal Styles */
.win-modal-content {
  animation: winModalEnter 0.4s ease-out;
  background: linear-gradient(135deg, var(--btn-primary-bg) 0%, var(--btn-primary-hover) 100%);
  border: 2px solid var(--btn-primary-bg);
}

.win-modal-header {
  border-bottom: none;
  padding: 24px 16px 16px;
  text-align: center;
}

.win-title {
  font-size: 28px;
  font-weight: 700;
  color: var(--btn-primary-text);
  margin: 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  animation: winTitlePulse 1.5s ease-in-out infinite;
}

.win-modal-body {
  text-align: center;
  padding: 16px 24px;
  background-color: var(--bg-primary);
}

.win-message {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 8px 0;
}

.win-submessage {
  font-size: 16px;
  color: var(--text-secondary);
  margin: 0;
}

.win-modal-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  padding: 16px 24px 24px;
  background-color: var(--bg-primary);
}

.win-modal-actions .btn {
  min-width: 120px;
  font-size: 14px;
  font-weight: 600;
}

@keyframes winModalEnter {
  0% {
    transform: scale(0.8) translateY(-20px);
    opacity: 0;
  }
  50% {
    transform: scale(1.05) translateY(5px);
  }
  100% {
    transform: scale(1) translateY(0);
    opacity: 1;
  }
}

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

/* Ensure no horizontal scroll */
html, body {
  overflow-x: hidden;
  width: 100%;
}

