/* 2048 Game Styles */

/* CSS Variables for consistent colors and values */
:root {
  /* Color palette */
  --bg-color: #fafafa;
  --text-color: #333;
  --text-light: #666;
  --btn-color: #8f7a66;
  --btn-hover-color: #9f8a76;
  --score-bg-color: #888;
  --score-hover-color: #666;
  --canvas-bg-color: #bbb;
  --game-message-bg: rgba(255, 255, 255, 0.8);
  --selector-bg-color: #f9f6f2;
  --grid-bg-color: #bbada0;
  --grid-cell-color: #cdc1b4;

  /* Layout */
  --border-radius-sm: 3px;
  --border-radius: 4px;
  --border-radius-lg: 8px;
  --container-width: 500px;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  background-color: var(--bg-color);
  color: var(--text-color);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

#layout {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: var(--container-width);
  margin: 40px auto;
}

/* Header & title styles */
.game-header {
  display: flex;
  justify-content: space-between;
  width: 100%;
  margin-bottom: 20px;
}

.title-section {
  display: flex;
  flex-direction: column;
}

.title {
  font-size: 60px;
  font-weight: bold;
  color: var(--text-color);
  margin: 0;
}

.subtitle {
  font-size: 14px;
  color: var(--text-light);
}

/* Score display */
.scores-container {
  display: flex;
  gap: 10px;
}

.score-box {
  background-color: var(--score-bg-color);
  color: white;
  border-radius: var(--border-radius);
  padding: 5px 15px;
  min-width: 80px;
  height: 64px;
  text-align: center;
}

.score-title {
  font-size: 12px;
  text-transform: uppercase;
}

.score-value {
  font-size: 20px;
  font-weight: bold;
}

/* Game intro and controls */
.game-intro {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.game-explanation {
  color: var(--text-light);
  font-size: 14px;
  line-height: 1.5;
}

/* New Game container */
.new-game-container {
  display: flex;
  justify-content: flex-end;
}

/* Game controls container below the canvas */
.game-controls {
  display: flex;
  justify-content: center;
  gap: 15px;
  width: 100%;
  margin-top: 15px;
  margin-bottom: 15px;
}

/* Styling for control buttons (undo/redo) */
.control-button {
  background-color: var(--score-bg-color);
  color: white;
  border: none;
  border-radius: var(--border-radius);
  padding: 10px 20px;
  min-width: 100px;
  font-weight: bold;
  cursor: pointer;
  transition: background-color 0.2s;
}

.control-button:hover:not(:disabled) {
  background-color: var(--score-hover-color);
}

.control-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.restart-button {
  background-color: var(--btn-color);
  color: white;
  border: none;
  border-radius: var(--border-radius);
  padding: 10px 20px;
  font-weight: bold;
  cursor: pointer;
  transition: background-color 0.2s;
}

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

/* Game board */
.game-container {
  position: relative;
}

canvas {
  background-color: var(--canvas-bg-color);
  border-radius: var(--border-radius-lg);
  margin-bottom: 0; /* Removed bottom margin since we now have controls below */
}

/* Game message display */
.game-message {
  display: none;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: var(--game-message-bg);
  z-index: 100;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: var(--border-radius-lg);
  text-align: center;
}

.game-message.game-over,
.game-message.game-won {
  display: flex;
}

.game-message p {
  font-size: 28px;
  font-weight: bold;
  margin-bottom: 20px;
}

/* Info elements */
.keyboard-hint {
  color: var(--text-light);
  font-size: 14px;
  text-align: center;
  margin-bottom: 15px;
}

.copyright {
  margin-top: 40px;
  color: var(--text-light);
  font-size: 14px;
  text-align: center;
}

.copyright a {
  color: var(--score-bg-color);
  text-decoration: none;
}

/* Common toggle styles */
.theme-toggle,
.grid-size-toggle,
.strategy-toggle {
  margin-top: 0;
  width: 100%;
  margin-bottom: 15px;
}

.toggle-button {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 15px;
  background-color: var(--btn-color);
  color: white;
  border: none;
  border-radius: var(--border-radius);
  font-weight: bold;
  cursor: pointer;
  transition: background-color 0.2s;
}

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

.toggle-icon {
  fill: white;
  transition: transform 0.3s ease;
}

.theme-toggle.open .toggle-icon,
.strategy-toggle.open .toggle-icon,
.grid-size-toggle.open .toggle-icon {
  transform: rotate(180deg);
}

/* Selector panels */
.theme-selector {
  display: none;
  background-color: var(--selector-bg-color);
  border-radius: var(--border-radius);
  margin-top: 5px;
  padding: 10px 15px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.theme-toggle.open .theme-selector,
.strategy-toggle.open .theme-selector,
.grid-size-toggle.open .theme-selector {
  display: block;
}

.theme-option {
  display: flex;
  align-items: center;
  margin: 8px 0;
}

.theme-option label {
  margin-left: 8px;
  cursor: pointer;
}

/* Color theme previews */
.color-preview {
  display: flex;
  margin-left: auto;
}

.color-preview span {
  display: block;
  width: 18px;
  height: 18px;
  margin-left: 2px;
  border-radius: var(--border-radius-sm);
}

/* Classic theme colors */
.classic-preview span:nth-child(1) { background-color: #eee4da; }
.classic-preview span:nth-child(2) { background-color: #f2b179; }
.classic-preview span:nth-child(3) { background-color: #f65e3b; }
.classic-preview span:nth-child(4) { background-color: #edc22e; }

/* Cool theme colors */
.cool-preview span:nth-child(1) { background-color: #d6eaf8; }
.cool-preview span:nth-child(2) { background-color: #85c1e9; }
.cool-preview span:nth-child(3) { background-color: #1a5276; }
.cool-preview span:nth-child(4) { background-color: #117864; }

/* Vibrant theme colors */
.vibrant-preview span:nth-child(1) { background-color: #f8dff3; }
.vibrant-preview span:nth-child(2) { background-color: #e8a1de; }
.vibrant-preview span:nth-child(3) { background-color: #b648b8; }
.vibrant-preview span:nth-child(4) { background-color: #471e85; }

/* Grayscale theme colors */
.grayscale-preview span:nth-child(1) { background-color: #f0f0f0; }
.grayscale-preview span:nth-child(2) { background-color: #c0c0c0; }
.grayscale-preview span:nth-child(3) { background-color: #808080; }
.grayscale-preview span:nth-child(4) { background-color: #404040; }

/* Grid size preview styles */
.grid-preview {
  display: flex;
  margin-left: auto;
}

.grid-preview-container {
  display: grid;
  gap: 2px;
  width: 60px;
  height: 60px;
  border-radius: var(--border-radius-sm);
  background-color: var(--grid-bg-color);
  padding: 2px;
}

.grid-preview-container div {
  background-color: var(--grid-cell-color);
  border-radius: 2px;
}

/* Grid preview templates */
.grid-3-preview .grid-preview-container {
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
}

.grid-4-preview .grid-preview-container {
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
}

.grid-5-preview .grid-preview-container {
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(5, 1fr);
}

.grid-6-preview .grid-preview-container {
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: repeat(6, 1fr);
}

/* Strategy selector styles */
.strategy-preview {
  display: flex;
  margin-left: auto;
  justify-content: center;
  align-items: center;
}

.strategy-icon {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 40px;
  height: 40px;
  border-radius: var(--border-radius);
  background-color: var(--selector-bg-color);
  padding: 8px;
}

.strategy-icon svg {
  width: 24px;
  height: 24px;
}

/* Media queries for responsiveness */
@media screen and (max-width: 520px) {
  #layout {
    padding: 0 20px;
  }

  .game-header {
    flex-direction: column;
    align-items: center;
  }

  .title-section {
    margin-bottom: 15px;
    align-items: center;
  }

  .title {
    font-size: 40px;
  }

  .game-intro {
    flex-direction: column;
    gap: 15px;
  }

  .new-game-container {
    width: 100%;
    justify-content: center;
  }

  .game-controls {
    flex-wrap: wrap;
  }

  .theme-toggle,
  .strategy-toggle,
  .grid-size-toggle {
    padding: 0 5px;
  }
}

@media screen and (max-width: 350px) {
  .scores-container {
    flex-direction: column;
  }

  .game-controls {
    flex-direction: column;
    align-items: center;
  }

  .control-button {
    width: 100%;
  }
}