/* 2048 Game Styles
   Modern, responsive design with smooth animations and accessibility features
   Supports both light and dark themes with clean, readable typography
*/

/* CSS Custom Properties for theming and consistent spacing */
:root {
  /* Light theme colors */
  --bg-color: #faf8ef;
  --container-bg: #bbada0;
  --text-primary: #776e65;
  --text-light: #f9f6f2;
  --cell-bg: rgba(238, 228, 218, 0.35);
  --modal-bg: rgba(255, 255, 255, 0.95);
  --modal-overlay: rgba(0, 0, 0, 0.5);
  --button-primary: #8f7a66;
  --button-secondary: #edc53f;
  --shadow-color: rgba(0, 0, 0, 0.1);
  
  /* Tile colors - carefully chosen for accessibility and visual appeal */
  --tile-2: #eee4da;
  --tile-4: #ede0c8;
  --tile-8: #f2b179;
  --tile-16: #f59563;
  --tile-32: #f67c5f;
  --tile-64: #f65e3b;
  --tile-128: #edcf72;
  --tile-256: #edcc61;
  --tile-512: #edc850;
  --tile-1024: #edc53f;
  --tile-2048: #edc22e;
  --tile-super: #3c3a32;
  
  /* Animation and spacing */
  --animation-speed: 150ms;
  --border-radius: 6px;
  --cell-size: 70px;
  --cell-gap: 10px;
  --grid-size: calc(4 * var(--cell-size) + 5 * var(--cell-gap));
}

/* Dark theme overrides */
[data-theme="dark"] {
  --bg-color: #1a1a1a;
  --container-bg: #2d2d2d;
  --text-primary: #e0e0e0;
  --text-light: #f9f6f2;
  --cell-bg: rgba(255, 255, 255, 0.1);
  --modal-bg: rgba(45, 45, 45, 0.95);
  --modal-overlay: rgba(0, 0, 0, 0.7);
  --button-primary: #6d6d6d;
  --shadow-color: rgba(0, 0, 0, 0.3);
  
  /* Darker tile variants for better contrast */
  --tile-2: #3a3a3a;
  --tile-4: #4a4a4a;
  --tile-8: #5a4a3a;
  --tile-16: #6a5a4a;
  --tile-32: #7a6a5a;
  --tile-64: #8a7a6a;
  --tile-128: #9a8a7a;
  --tile-256: #aa9a8a;
  --tile-512: #baaa9a;
  --tile-1024: #cabaaa;
  --tile-2048: #dac0aa;
  --tile-super: #f0f0f0;
}

/* Animation speed variants */
[data-animation="fast"] {
  --animation-speed: 75ms;
}

[data-animation="slow"] {
  --animation-speed: 300ms;
}

/* Base styles and reset */
* {
  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-color);
  color: var(--text-primary);
  line-height: 1.6;
  transition: background-color var(--animation-speed) ease, color var(--animation-speed) ease;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

/* Theme toggle button */
.theme-toggle {
  position: fixed;
  top: 20px;
  right: 20px;
  background: var(--button-primary);
  color: var(--text-light);
  border: none;
  border-radius: 50%;
  width: 50px;
  height: 50px;
  cursor: pointer;
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 10px var(--shadow-color);
  transition: all var(--animation-speed) ease;
  z-index: 1000;
}

.theme-toggle:hover {
  transform: scale(1.1);
  box-shadow: 0 4px 20px var(--shadow-color);
}

.theme-toggle:focus {
  outline: 2px solid var(--button-secondary);
  outline-offset: 2px;
}

/* Main game container */
.game-container {
  width: 100%;
  max-width: 500px;
  text-align: center;
}

/* Header section */
.game-header {
  margin-bottom: 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 20px;
}

.game-title {
  font-size: 3.5rem;
  font-weight: bold;
  color: var(--text-primary);
  margin: 0;
  text-shadow: 2px 2px 4px var(--shadow-color);
}

.score-container {
  display: flex;
  gap: 15px;
}

.score-box {
  background: var(--container-bg);
  padding: 15px 20px;
  border-radius: var(--border-radius);
  min-width: 80px;
  box-shadow: 0 2px 8px var(--shadow-color);
}

.score-label {
  font-size: 0.9rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-light);
  margin-bottom: 5px;
}

.score-value {
  font-size: 1.8rem;
  font-weight: bold;
  color: var(--text-light);
  transition: transform var(--animation-speed) ease;
}

.score-value.updated {
  transform: scale(1.2);
}

/* Game info and controls */
.game-info {
  margin-bottom: 25px;
}

.game-description {
  font-size: 1rem;
  margin-bottom: 15px;
  color: var(--text-primary);
}

.controls {
  display: flex;
  justify-content: center;
  gap: 15px;
  flex-wrap: wrap;
}

/* Button styles */
.btn {
  padding: 12px 24px;
  border: none;
  border-radius: var(--border-radius);
  cursor: pointer;
  font-size: 1rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  transition: all var(--animation-speed) ease;
  box-shadow: 0 2px 8px var(--shadow-color);
}

.btn:focus {
  outline: 2px solid var(--button-secondary);
  outline-offset: 2px;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none !important;
}

.btn:not(:disabled):hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px var(--shadow-color);
}

.btn:not(:disabled):active {
  transform: translateY(0);
}

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

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

/* Game board container */
.game-board-container {
  position: relative;
  margin: 0 auto 25px;
  width: var(--grid-size);
  height: var(--grid-size);
}

/* Grid background */
.game-board {
  background: var(--container-bg);
  border-radius: var(--border-radius);
  width: 100%;
  height: 100%;
  position: relative;
  box-shadow: 0 4px 20px var(--shadow-color);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--cell-gap);
  padding: var(--cell-gap);
}

.grid-cell {
  background: var(--cell-bg);
  border-radius: var(--border-radius);
  transition: background-color var(--animation-speed) ease;
}

/* Tile container for positioning animated tiles */
.tile-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

/* Individual tile styles */
.tile {
  position: absolute;
  width: var(--cell-size);
  height: var(--cell-size);
  border-radius: var(--border-radius);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 2rem;
  transition: all var(--animation-speed) ease;
  box-shadow: 0 2px 8px var(--shadow-color);
  z-index: 10;
}

/* Tile positioning based on grid coordinates */
.tile[data-row="0"] { top: var(--cell-gap); }
.tile[data-row="1"] { top: calc(var(--cell-gap) + var(--cell-size) + var(--cell-gap)); }
.tile[data-row="2"] { top: calc(var(--cell-gap) + 2 * (var(--cell-size) + var(--cell-gap))); }
.tile[data-row="3"] { top: calc(var(--cell-gap) + 3 * (var(--cell-size) + var(--cell-gap))); }

.tile[data-col="0"] { left: var(--cell-gap); }
.tile[data-col="1"] { left: calc(var(--cell-gap) + var(--cell-size) + var(--cell-gap)); }
.tile[data-col="2"] { left: calc(var(--cell-gap) + 2 * (var(--cell-size) + var(--cell-gap))); }
.tile[data-col="3"] { left: calc(var(--cell-gap) + 3 * (var(--cell-size) + var(--cell-gap))); }

/* Tile colors based on value */
.tile-2 { background: var(--tile-2); color: var(--text-primary); font-size: 1.8rem; }
.tile-4 { background: var(--tile-4); color: var(--text-primary); font-size: 1.8rem; }
.tile-8 { background: var(--tile-8); color: var(--text-light); font-size: 1.8rem; }
.tile-16 { background: var(--tile-16); color: var(--text-light); font-size: 1.7rem; }
.tile-32 { background: var(--tile-32); color: var(--text-light); font-size: 1.7rem; }
.tile-64 { background: var(--tile-64); color: var(--text-light); font-size: 1.6rem; }
.tile-128 { background: var(--tile-128); color: var(--text-light); font-size: 1.5rem; }
.tile-256 { background: var(--tile-256); color: var(--text-light); font-size: 1.5rem; }
.tile-512 { background: var(--tile-512); color: var(--text-light); font-size: 1.4rem; }
.tile-1024 { background: var(--tile-1024); color: var(--text-light); font-size: 1.3rem; }
.tile-2048 { background: var(--tile-2048); color: var(--text-light); font-size: 1.3rem; }
.tile-super { background: var(--tile-super); color: var(--text-light); font-size: 1.2rem; }

/* Tile animations */
.tile-new {
  animation: tile-appear var(--animation-speed) ease;
}

.tile-merged {
  animation: tile-merge calc(var(--animation-speed) * 1.5) ease;
  z-index: 20;
}

@keyframes tile-appear {
  0% {
    transform: scale(0);
    opacity: 0;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes tile-merge {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.2);
  }
  100% {
    transform: scale(1);
  }
}

/* Game statistics */
.game-stats {
  display: flex;
  justify-content: center;
  gap: 30px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 1rem;
}

.stat-label {
  font-weight: 600;
  color: var(--text-primary);
}

.stat-value {
  font-weight: bold;
  color: var(--button-primary);
}

/* Modal styles */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: var(--modal-overlay);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(5px);
  transition: opacity var(--animation-speed) ease;
}

.modal.hidden {
  opacity: 0;
  pointer-events: none;
}

.modal-content {
  background: var(--modal-bg);
  padding: 40px 30px;
  border-radius: calc(var(--border-radius) * 2);
  text-align: center;
  max-width: 400px;
  width: 90%;
  box-shadow: 0 10px 30px var(--shadow-color);
  transform: scale(1);
  transition: transform var(--animation-speed) ease;
}

.modal.hidden .modal-content {
  transform: scale(0.9);
}

.modal-title {
  font-size: 2.5rem;
  margin-bottom: 20px;
  color: var(--text-primary);
}

.modal-message {
  font-size: 1.2rem;
  margin-bottom: 30px;
  color: var(--text-primary);
  line-height: 1.5;
}

.modal-buttons {
  display: flex;
  justify-content: center;
  gap: 15px;
  flex-wrap: wrap;
}

/* Settings modal specific styles */
.settings-content {
  text-align: left;
  margin-bottom: 30px;
}

.setting-item {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.setting-item label {
  font-weight: 600;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: 8px;
}

.setting-select {
  padding: 8px 12px;
  border: 1px solid var(--container-bg);
  border-radius: var(--border-radius);
  background: var(--bg-color);
  color: var(--text-primary);
  font-size: 1rem;
}

.setting-select:focus {
  outline: 2px solid var(--button-secondary);
  outline-offset: 1px;
}

/* Help overlay */
.help-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: var(--modal-overlay);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  transition: opacity var(--animation-speed) ease;
}

.help-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.help-content {
  background: var(--modal-bg);
  padding: 30px;
  border-radius: calc(var(--border-radius) * 2);
  max-width: 400px;
  width: 90%;
  box-shadow: 0 10px 30px var(--shadow-color);
}

.help-content h3 {
  margin-bottom: 20px;
  color: var(--text-primary);
  text-align: center;
}

.help-content ul {
  list-style: none;
  margin-bottom: 20px;
}

.help-content li {
  margin-bottom: 10px;
  color: var(--text-primary);
}

.help-content kbd {
  background: var(--container-bg);
  color: var(--text-light);
  padding: 4px 8px;
  border-radius: 4px;
  font-family: monospace;
  font-size: 0.9rem;
  font-weight: bold;
}

.help-content p {
  color: var(--text-primary);
  text-align: center;
  font-style: italic;
}

/* Accessibility - Screen reader only content */
.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;
}

/* Responsive design */
@media (max-width: 600px) {
  body {
    padding: 10px;
  }
  
  .game-header {
    flex-direction: column;
    text-align: center;
    gap: 15px;
  }
  
  .game-title {
    font-size: 2.5rem;
  }
  
  .score-container {
    justify-content: center;
  }
  
  .controls {
    gap: 10px;
  }
  
  .btn {
    padding: 10px 20px;
    font-size: 0.9rem;
  }
  
  .game-stats {
    gap: 20px;
  }
  
  .theme-toggle {
    top: 10px;
    right: 10px;
    width: 45px;
    height: 45px;
    font-size: 18px;
  }
  
  /* Smaller tiles on mobile */
  :root {
    --cell-size: 60px;
    --cell-gap: 8px;
  }
  
  .tile {
    font-size: 1.5rem;
  }
  
  .tile-2, .tile-4 { font-size: 1.4rem; }
  .tile-8, .tile-16, .tile-32 { font-size: 1.3rem; }
  .tile-64, .tile-128, .tile-256 { font-size: 1.2rem; }
  .tile-512, .tile-1024, .tile-2048, .tile-super { font-size: 1rem; }
}

@media (max-width: 400px) {
  :root {
    --cell-size: 50px;
    --cell-gap: 6px;
  }
  
  .game-title {
    font-size: 2rem;
  }
  
  .score-box {
    padding: 10px 15px;
    min-width: 70px;
  }
  
  .score-value {
    font-size: 1.5rem;
  }
  
  .tile {
    font-size: 1.2rem;
  }
  
  .tile-2, .tile-4 { font-size: 1.1rem; }
  .tile-8, .tile-16, .tile-32, .tile-64 { font-size: 1rem; }
  .tile-128, .tile-256, .tile-512 { font-size: 0.9rem; }
  .tile-1024, .tile-2048, .tile-super { font-size: 0.8rem; }
}

/* Focus management for keyboard navigation */
.game-board:focus {
  outline: 3px solid var(--button-secondary);
  outline-offset: 3px;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
  :root {
    --shadow-color: rgba(0, 0, 0, 0.3);
  }
  
  .tile {
    border: 2px solid currentColor;
  }
  
  .btn {
    border: 2px solid currentColor;
  }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  
  :root {
    --animation-speed: 0ms;
  }
}

/* Print styles */
@media print {
  .theme-toggle,
  .modal,
  .help-overlay {
    display: none;
  }
  
  .game-container {
    max-width: none;
  }
}
