/* CSS Reset and Base Styles */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg-color: #fafaf7;
  --text-color: #1a1a1a;
  --tile-bg: rgba(255, 255, 255, 0.9);
  --tile-border: #e5e5e5;
  --tile-shadow: 0 2px 4px rgba(0, 0, 0, 0.08);
  --selected-color: #3b82f6;
  --home-row-color: #22c55e;
  --row-complete-bg: #22c55e;
  --row-complete-text: #ffffff;
  --win-gold: #fbbf24;
  --header-bg: rgba(255, 255, 255, 0.9);
  --modal-bg: rgba(250, 250, 247, 0.95);
  --modal-bg-dark: rgba(42, 42, 42, 0.95);
  --button-bg: #1a1a1a;
  --button-text: #ffffff;
  --button-hover: #333333;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg-color: #1a1a1a;
    --text-color: #fafaf7;
    --tile-bg: rgba(42, 42, 42, 0.9);
    --tile-border: #3a3a3a;
    --tile-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
    --header-bg: rgba(42, 42, 42, 0.9);
    --modal-bg: rgba(42, 42, 42, 0.95);
    --button-bg: #fafaf7;
    --button-text: #1a1a1a;
    --button-hover: #e5e5e5;
  }
}

/* Force dark mode for testing */
[data-theme="dark"] {
  --bg-color: #1a1a1a;
  --text-color: #fafaf7;
  --tile-bg: rgba(42, 42, 42, 0.9);
  --tile-border: #3a3a3a;
  --tile-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  --header-bg: rgba(42, 42, 42, 0.9);
  --modal-bg: rgba(42, 42, 42, 0.95);
  --button-bg: #fafaf7;
  --button-text: #1a1a1a;
  --button-hover: #e5e5e5;
}

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

body {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background-color: var(--bg-color);
  color: var(--text-color);
  line-height: 1.5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* Header Styles */
header {
  background-color: var(--header-bg);
  border-bottom: 1px solid var(--tile-border);
  padding: 1rem;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.header-content {
  max-width: 600px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 1rem;
}

h1 {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

/* Controls */
.controls {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  flex-wrap: wrap;
}

select, button {
  font-family: inherit;
  font-size: 0.875rem;
  padding: 0.5rem 1rem;
  border-radius: 0.5rem;
  border: 1px solid var(--tile-border);
  background-color: var(--tile-bg);
  color: var(--text-color);
  cursor: pointer;
  transition: all 0.15s ease;
  min-height: 44px;
  min-width: 44px;
}

select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23666' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  padding-right: 2rem;
}

button {
  background-color: var(--button-bg);
  color: var(--button-text);
  border: none;
  font-weight: 500;
}

button:hover {
  background-color: var(--button-hover);
  transform: translateY(-1px);
}

button:active {
  transform: translateY(0);
}

button.secondary {
  background-color: var(--tile-bg);
  color: var(--text-color);
  border: 1px solid var(--tile-border);
}

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

/* Main Game Area */
main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1.5rem 1rem;
  max-width: 600px;
  margin: 0 auto;
  width: 100%;
}

/* Grid Container */
.grid-container {
  width: 100%;
  max-width: 400px;
  aspect-ratio: 1;
  position: relative;
}

#grid {
  display: grid;
  gap: 0.5rem;
  width: 100%;
  height: 100%;
}

/* Tile Styles */
.tile {
  background-color: var(--tile-bg);
  border: 2px solid var(--tile-border);
  border-radius: 0.75rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1.25rem, 8vw, 2rem);
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  transition: all 0.2s ease;
  box-shadow: var(--tile-shadow);
  min-height: 44px;
  min-width: 44px;
  touch-action: manipulation;
}

.tile:hover {
  transform: scale(1.02);
  border-color: #ccc;
}

.tile:active {
  transform: scale(0.98);
}

/* Selected Tile */
.tile.selected {
  border-color: var(--selected-color);
  background-color: rgba(59, 130, 246, 0.1);
  transform: scale(1.05);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

/* Correct Row Hint (dashed border - in right row but wrong position) */
.tile.correct-row {
  border-color: var(--home-row-color);
  border-style: dashed;
  border-width: 3px;
}

/* Correct Position Hint (solid border - in right row and right position) */
.tile.correct-position {
  border-color: var(--home-row-color);
  border-style: solid;
  border-width: 3px;
}

/* Completed Row (filled background - entire row is correct) */
.tile.row-complete {
  background-color: var(--row-complete-bg);
  color: var(--row-complete-text);
  border-color: var(--row-complete-bg);
  border-style: solid;
}

/* Completed Column (if different from row) */
.tile.col-complete:not(.row-complete) {
  background-color: rgba(34, 197, 94, 0.3);
  border-color: var(--home-row-color);
}

/* Win State */
#grid.won .tile {
  animation: winPulse 0.6s ease;
}

#grid.won .tile.row-complete {
  background-color: var(--win-gold);
  border-color: var(--win-gold);
  color: #1a1a1a;
}

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

/* Message Display */
#message {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.9);
  background-color: var(--text-color);
  color: var(--bg-color);
  padding: 1rem 2rem;
  border-radius: 0.75rem;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: all 0.3s ease;
  z-index: 200;
}

#message.show {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

/* Loading State */
.loading {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 1rem;
  color: #666;
  text-align: center;
}

/* Modal Styles */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
  z-index: 300;
  padding: 1rem;
}

.modal.show {
  opacity: 1;
  pointer-events: auto;
}

.modal-content {
  background-color: var(--modal-bg);
  border-radius: 1rem;
  padding: 2rem;
  max-width: 400px;
  width: 100%;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  transform: scale(0.9);
  transition: transform 0.3s ease;
  border: 1px solid var(--tile-border);
}

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

.modal h2 {
  font-size: 1.75rem;
  margin-bottom: 1rem;
}

/* Puzzle Info */
.puzzle-info {
  text-align: center;
  font-size: 0.875rem;
  color: #666;
  margin-bottom: 1rem;
}

/* Stats Bar */
.stats-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  max-width: 400px;
  margin-bottom: 0.5rem;
  padding: 0.75rem 1rem;
  background-color: var(--tile-bg);
  border-radius: 0.75rem;
  border: 1px solid var(--tile-border);
  font-size: 0.875rem;
}

#moves, #streak, #score {
  font-weight: 600;
}

/* Modal Content Enhancements */
.modal .tier {
  font-size: 2rem;
  font-weight: 700;
  color: var(--win-gold);
  margin-bottom: 0.5rem;
}

.modal .moves {
  font-size: 1.25rem;
  color: #666;
  margin-bottom: 1.5rem;
}

.modal .puzzle-score {
  font-size: 3rem;
  font-weight: 700;
  color: var(--home-row-color);
  margin-bottom: 0.5rem;
}

.modal .moves-remaining {
  font-size: 1rem;
  color: #666;
  margin-bottom: 1.5rem;
}

.modal .final-score {
  font-size: 2.5rem;
  font-weight: 700;
  color: var(--text-color);
  margin-bottom: 0.5rem;
}

.modal .new-best {
  font-size: 1rem;
  color: var(--win-gold);
  font-weight: 600;
  margin-bottom: 1rem;
}

.modal .new-best.hidden {
  display: none;
}

.modal .game-over-stats {
  margin: 1.5rem 0;
  padding: 1rem;
  background-color: var(--tile-bg);
  border-radius: 0.5rem;
  border: 1px solid var(--tile-border);
}

.modal .game-over-stats div {
  margin-bottom: 0.5rem;
}

.modal .game-over-stats div:last-child {
  margin-bottom: 0;
}

/* Help Modal */
.help-content {
  text-align: left;
  max-height: 60vh;
  overflow-y: auto;
  padding-right: 0.5rem;
}

.help-content section {
  margin-bottom: 1.5rem;
}

.help-content h3 {
  font-size: 1.125rem;
  margin-bottom: 0.75rem;
  color: var(--text-color);
}

.help-content p {
  margin-bottom: 0.75rem;
  line-height: 1.6;
}

.help-content ul {
  margin-left: 1.5rem;
  margin-bottom: 0.75rem;
}

.help-content li {
  margin-bottom: 0.5rem;
  line-height: 1.5;
}

.help-content .hint-correct-row {
  color: var(--home-row-color);
  font-weight: 600;
}

.help-content .hint-correct-position {
  color: var(--home-row-color);
  font-weight: 600;
}

.help-content .hint-complete {
  color: var(--row-complete-bg);
  font-weight: 600;
}

.help-content em {
  color: #888;
  font-size: 0.875rem;
}

.modal-buttons {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
  flex-wrap: wrap;
}

/* Stats Modal Specific */
.stats-content {
  text-align: left;
  margin: 1.5rem 0;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--tile-border);
}

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

.stat-divider {
  height: 1px;
  background-color: var(--tile-border);
  margin: 1rem 0;
}

.stat-header {
  font-weight: 600;
  margin-bottom: 0.5rem;
  color: #666;
}

/* Settings Modal Specific */
.settings-content {
  text-align: left;
  margin: 1.5rem 0;
}

.setting-row {
  margin-bottom: 1.5rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--tile-border);
}

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

.setting-label {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  font-weight: 500;
  cursor: pointer;
  font-size: 1rem;
}

.setting-label input[type="checkbox"] {
  width: 20px;
  height: 20px;
  min-width: 20px;
  min-height: 20px;
  cursor: pointer;
  accent-color: var(--home-row-color);
}

.setting-description {
  margin-top: 0.5rem;
  margin-left: 2rem;
  font-size: 0.875rem;
  color: #888;
  font-weight: 400;
}

.setting-note {
  margin-top: 1rem;
  padding: 0.75rem;
  background-color: var(--bg-color);
  border-radius: 0.5rem;
  font-size: 0.875rem;
  color: #666;
}

.setting-note p {
  margin: 0;
}

/* Instructions */
.instructions {
  margin-top: 2rem;
  padding: 1rem;
  background-color: var(--tile-bg);
  border-radius: 0.75rem;
  border: 1px solid var(--tile-border);
  font-size: 0.875rem;
  color: #666;
  text-align: center;
  max-width: 400px;
}

.instructions p {
  margin-bottom: 0.5rem;
}

.instructions p:last-child {
  margin-bottom: 0;
}

/* Legend */
.legend {
  display: flex;
  gap: 1rem;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 1rem;
  font-size: 0.75rem;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.legend-box {
  width: 16px;
  height: 16px;
  border-radius: 4px;
  border: 2px solid var(--tile-border);
}

.legend-box.correct-row {
  border-color: var(--home-row-color);
  border-style: dashed;
  border-width: 3px;
}

.legend-box.correct-position {
  border-color: var(--home-row-color);
  border-style: solid;
  border-width: 3px;
}

.legend-box.row-complete {
  background-color: var(--row-complete-bg);
  border-color: var(--row-complete-bg);
}

/* Footer */
footer {
  padding: 1.5rem;
  text-align: center;
  font-size: 0.75rem;
  color: #999;
  border-top: 1px solid var(--tile-border);
}

/* Responsive Adjustments */
@media (max-width: 480px) {
  html {
    font-size: 14px;
  }
  
  h1 {
    font-size: 1.25rem;
  }
  
  .header-content {
    flex-direction: column;
    gap: 0.75rem;
  }
  
  .tile {
    font-size: clamp(1rem, 6vw, 1.5rem);
  }
  
  .modal-content {
    padding: 1.5rem;
  }
}

@media (min-width: 768px) {
  #grid {
    gap: 0.75rem;
  }
  
  .tile {
    font-size: clamp(1.5rem, 5vw, 2.5rem);
  }
}

/* Prevent double-tap zoom on iOS */
@media (pointer: coarse) {
  .tile {
    touch-action: manipulation;
  }
}

/* Animation for tile swap */
@keyframes swapTile {
  0% { transform: scale(1); }
  50% { transform: scale(0.9); }
  100% { transform: scale(1); }
}

.tile.swapping {
  animation: swapTile 0.2s ease;
}

/* Accessibility */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* Focus styles for keyboard navigation */
button:focus-visible,
select:focus-visible,
.tile:focus-visible {
  outline: 2px solid var(--selected-color);
  outline-offset: 2px;
}
