/**
 * 2048 Premium CSS Design System
 * Features: Three beautiful themes (Classic Warm, Cyberpunk Neon, Slate Minimalist),
 * smooth compositor-friendly animations, modern glassmorphism components,
 * floating score indicators, and a responsive mobile grid system.
 */

/* ==========================================================================
   1. BASE STYLES & TYPOGRAPHY
   ========================================================================== */
@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@500;700;800&family=Inter:wght@400;600;700;800&display=swap');

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px 16px 40px;
  background-color: var(--bg-color);
  color: var(--text-color);
  transition: background-color 0.4s ease, color 0.4s ease;
  overflow-x: hidden;
}

#app {
  width: 100%;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

h1, h2, h3, .title {
  font-family: 'Outfit', sans-serif;
}

/* ==========================================================================
   2. DESIGN SYSTEM & THEMES
   ========================================================================== */

/* --- CLASSIC WARM THEME (Active by default) --- */
body.theme-classic {
  --bg-color: #faf8ef;
  --text-color: #776e65;
  --board-bg: #bbada0;
  --cell-bg: rgba(238, 228, 218, 0.35);
  --accent-color: #8f7a66;
  --accent-hover: #7f6a56;
  --text-light: #eee4da;
  --box-shadow: 0 4px 12px rgba(0, 0, 0, 0.06);
  --border-radius: 8px;
  --glass-bg: rgba(250, 248, 239, 0.8);
  --glass-border: rgba(143, 122, 102, 0.1);

  /* Tile Palette */
  --tile-2-bg: #eee4da;
  --tile-2-color: #776e65;
  --tile-2-shadow: none;
  
  --tile-4-bg: #ede0c8;
  --tile-4-color: #776e65;
  --tile-4-shadow: none;

  --tile-8-bg: #f2b179;
  --tile-8-color: #f9f6f2;
  --tile-8-shadow: 0 2px 4px rgba(242, 177, 121, 0.3);

  --tile-16-bg: #f59563;
  --tile-16-color: #f9f6f2;
  --tile-16-shadow: 0 2px 6px rgba(245, 149, 99, 0.4);

  --tile-32-bg: #f67c5f;
  --tile-32-color: #f9f6f2;
  --tile-32-shadow: 0 3px 8px rgba(246, 124, 95, 0.4);

  --tile-64-bg: #f65e3b;
  --tile-64-color: #f9f6f2;
  --tile-64-shadow: 0 3px 8px rgba(246, 94, 59, 0.5);

  --tile-128-bg: #edcf72;
  --tile-128-color: #f9f6f2;
  --tile-128-shadow: 0 4px 10px rgba(237, 207, 114, 0.5);

  --tile-256-bg: #edcc61;
  --tile-256-color: #f9f6f2;
  --tile-256-shadow: 0 4px 12px rgba(237, 204, 97, 0.6);

  --tile-512-bg: #edc850;
  --tile-512-color: #f9f6f2;
  --tile-512-shadow: 0 5px 14px rgba(237, 200, 80, 0.6);

  --tile-1024-bg: #edc53f;
  --tile-1024-color: #f9f6f2;
  --tile-1024-shadow: 0 5px 16px rgba(237, 197, 63, 0.7);

  --tile-2048-bg: #edc22e;
  --tile-2048-color: #f9f6f2;
  --tile-2048-shadow: 0 6px 20px rgba(237, 194, 46, 0.8), 0 0 12px rgba(237, 194, 46, 0.4);

  --tile-super-bg: #3c3a32;
  --tile-super-color: #f9f6f2;
  --tile-super-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
}

/* --- CYBERPUNK NEON THEME (Dark mode glow) --- */
body.theme-cyberpunk {
  --bg-color: #0b0c10;
  --text-color: #c5c6c7;
  --board-bg: #1f2833;
  --cell-bg: rgba(11, 12, 16, 0.5);
  --accent-color: #45f3ff;
  --accent-hover: #00d2c4;
  --text-light: #1f2833;
  --box-shadow: 0 0 20px rgba(69, 243, 255, 0.15);
  --border-radius: 12px;
  --glass-bg: rgba(31, 40, 51, 0.85);
  --glass-border: rgba(69, 243, 255, 0.2);

  /* Tile Palette with glowing neon visual aesthetics */
  --tile-2-bg: linear-gradient(135deg, #122329, #1a3c40);
  --tile-2-color: #45f3ff;
  --tile-2-shadow: 0 0 8px rgba(69, 243, 255, 0.2);
  
  --tile-4-bg: linear-gradient(135deg, #1d1b2a, #2d1b4e);
  --tile-4-color: #bd93f9;
  --tile-4-shadow: 0 0 8px rgba(189, 147, 249, 0.2);

  --tile-8-bg: linear-gradient(135deg, #f107a3, #7b2cbf);
  --tile-8-color: #ffffff;
  --tile-8-shadow: 0 0 12px rgba(241, 7, 163, 0.4);

  --tile-16-bg: linear-gradient(135deg, #ff0055, #9e0031);
  --tile-16-color: #ffffff;
  --tile-16-shadow: 0 0 12px rgba(255, 0, 85, 0.4);

  --tile-32-bg: linear-gradient(135deg, #ff3f00, #b21807);
  --tile-32-color: #ffffff;
  --tile-32-shadow: 0 0 14px rgba(255, 63, 0, 0.5);

  --tile-64-bg: linear-gradient(135deg, #ff9f00, #ff5f00);
  --tile-64-color: #ffffff;
  --tile-64-shadow: 0 0 14px rgba(255, 159, 0, 0.5);

  --tile-128-bg: linear-gradient(135deg, #00f5d4, #00bbf9);
  --tile-128-color: #0b0c10;
  --tile-128-shadow: 0 0 16px rgba(0, 245, 212, 0.6);

  --tile-256-bg: linear-gradient(135deg, #00bbf9, #00f5d4);
  --tile-256-color: #ffffff;
  --tile-256-shadow: 0 0 16px rgba(0, 187, 249, 0.6);

  --tile-512-bg: linear-gradient(135deg, #9b5de5, #f15bb5);
  --tile-512-color: #ffffff;
  --tile-512-shadow: 0 0 18px rgba(155, 93, 229, 0.7);

  --tile-1024-bg: linear-gradient(135deg, #ff007f, #7f00ff);
  --tile-1024-color: #ffffff;
  --tile-1024-shadow: 0 0 20px rgba(255, 0, 127, 0.8);

  --tile-2048-bg: linear-gradient(135deg, #ffea00, #ff007f);
  --tile-2048-color: #ffffff;
  --tile-2048-shadow: 0 0 25px rgba(255, 234, 0, 0.9), 0 0 10px rgba(255, 0, 127, 0.5);

  --tile-super-bg: linear-gradient(135deg, #ffffff, #000000);
  --tile-super-color: #ff0055;
  --tile-super-shadow: 0 0 30px rgba(255, 255, 255, 0.5);
}

/* --- SLATE MINIMALIST THEME (High-end tech) --- */
body.theme-slate {
  --bg-color: #1e293b;
  --text-color: #f8fafc;
  --board-bg: #0f172a;
  --cell-bg: rgba(30, 41, 59, 0.5);
  --accent-color: #38bdf8;
  --accent-hover: #0ea5e9;
  --text-light: #0f172a;
  --box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.3), 0 8px 10px -6px rgba(0, 0, 0, 0.3);
  --border-radius: 10px;
  --glass-bg: rgba(15, 23, 42, 0.85);
  --glass-border: rgba(56, 189, 248, 0.15);

  /* Tile Palette with modern sleek metals */
  --tile-2-bg: #334155;
  --tile-2-color: #f8fafc;
  --tile-2-shadow: none;
  
  --tile-4-bg: #475569;
  --tile-4-color: #f8fafc;
  --tile-4-shadow: none;

  --tile-8-bg: #0ea5e9;
  --tile-8-color: #ffffff;
  --tile-8-shadow: 0 4px 6px -1px rgba(14, 165, 233, 0.3);

  --tile-16-bg: #0284c7;
  --tile-16-color: #ffffff;
  --tile-16-shadow: 0 4px 6px -1px rgba(2, 132, 199, 0.3);

  --tile-32-bg: #6366f1;
  --tile-32-color: #ffffff;
  --tile-32-shadow: 0 4px 6px -1px rgba(99, 102, 241, 0.3);

  --tile-64-bg: #4f46e5;
  --tile-64-color: #ffffff;
  --tile-64-shadow: 0 4px 6px -1px rgba(79, 70, 229, 0.3);

  --tile-128-bg: #ec4899;
  --tile-128-color: #ffffff;
  --tile-128-shadow: 0 4px 8px -1px rgba(236, 72, 153, 0.4);

  --tile-256-bg: #db2777;
  --tile-256-color: #ffffff;
  --tile-256-shadow: 0 4px 8px -1px rgba(219, 39, 119, 0.4);

  --tile-512-bg: #10b981;
  --tile-512-color: #ffffff;
  --tile-512-shadow: 0 4px 8px -1px rgba(16, 185, 129, 0.4);

  --tile-1024-bg: #059669;
  --tile-1024-color: #ffffff;
  --tile-1024-shadow: 0 4px 8px -1px rgba(5, 150, 105, 0.4);

  --tile-2048-bg: linear-gradient(135deg, #f59e0b, #eab308);
  --tile-2048-color: #0f172a;
  --tile-2048-shadow: 0 10px 15px -3px rgba(245, 158, 11, 0.4), 0 0 10px rgba(245, 158, 11, 0.2);

  --tile-super-bg: linear-gradient(135deg, #ec4899, #8b5cf6);
  --tile-super-color: #ffffff;
  --tile-super-shadow: 0 10px 15px -3px rgba(139, 92, 246, 0.4);
}


/* ==========================================================================
   3. HEADER & CONTROLS
   ========================================================================== */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}

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

.title {
  font-size: 56px;
  font-weight: 800;
  line-height: 1;
  letter-spacing: -0.04em;
  background: linear-gradient(135deg, var(--text-color), var(--accent-color));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.subtitle {
  font-size: 13px;
  opacity: 0.7;
  font-weight: 500;
  margin-top: 4px;
}

.meta-controls {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
}

.scores-container {
  display: flex;
  gap: 8px;
}

.score-box {
  position: relative;
  background: var(--board-bg);
  border-radius: var(--border-radius);
  padding: 6px 14px;
  text-align: center;
  min-width: 80px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
  transition: background-color 0.4s ease;
}

.score-box .label {
  font-size: 10px;
  font-weight: 700;
  color: var(--text-light);
  opacity: 0.75;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 2px;
}

.score-box .value {
  font-size: 20px;
  font-weight: 800;
  color: #ffffff;
  line-height: 1.2;
  font-family: 'Outfit', sans-serif;
}

/* Floating Score gain popups (+Score) */
@keyframes floatScore {
  0% {
    transform: translateY(0) scale(0.8);
    opacity: 0;
  }
  20% {
    transform: translateY(-8px) scale(1.1);
    opacity: 1;
  }
  100% {
    transform: translateY(-28px) scale(0.9);
    opacity: 0;
  }
}

.score-gain {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  text-align: center;
  font-weight: 800;
  font-size: 18px;
  color: var(--accent-color);
  pointer-events: none;
  animation: floatScore 0.8s cubic-bezier(0.25, 1, 0.5, 1) forwards;
  z-index: 20;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
}

.btn-toolbar {
  display: flex;
  gap: 8px;
  align-items: center;
}

/* Standard Premium Buttons */
.btn {
  background: var(--accent-color);
  color: #ffffff;
  border: none;
  border-radius: var(--border-radius);
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  transition: background-color 0.2s, transform 0.1s, box-shadow 0.2s;
  user-select: none;
}

.btn:hover {
  background: var(--accent-hover);
  box-shadow: 0 4px 8px rgba(0,0,0,0.15);
}

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

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.btn-icon {
  width: 32px;
  height: 32px;
  padding: 0;
  justify-content: center;
  border-radius: 50%;
}

.btn-icon svg {
  width: 16px;
  height: 16px;
  fill: currentColor;
}


/* ==========================================================================
   4. THE GAME BOARD & CANVAS OVERLAY
   ========================================================================== */
.board-wrapper {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  background: var(--board-bg);
  border-radius: var(--border-radius);
  padding: 12px;
  box-shadow: var(--box-shadow);
  transition: background-color 0.4s ease, box-shadow 0.4s ease;
  overflow: hidden;
}

.board-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  width: 100%;
  height: 100%;
}

.cell {
  background: var(--cell-bg);
  border-radius: 6px;
  width: 100%;
  height: 100%;
  transition: background-color 0.4s ease;
}

/* Overlays Layer (Canvas particles / Tiles container) */
.tiles-layer {
  position: absolute;
  top: 12px;
  left: 12px;
  right: 12px;
  bottom: 12px;
  pointer-events: none;
}

.particles-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 2;
}

/* ==========================================================================
   5. TILE RENDERING & ANIMATIONS
   ========================================================================== */
.tile {
  position: absolute;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-family: 'Outfit', sans-serif;
  user-select: none;
  z-index: 1;
  
  /* Transitions: smoothly slide position updates using compositor transform properties */
  transition: translate 0.16s cubic-bezier(0.25, 1, 0.5, 1);
  box-shadow: var(--tile-shadow);
}

/* Default Tile values styling mapper */
.tile-2 { background: var(--tile-2-bg); color: var(--tile-2-color); box-shadow: var(--tile-2-shadow); }
.tile-4 { background: var(--tile-4-bg); color: var(--tile-4-color); box-shadow: var(--tile-4-shadow); }
.tile-8 { background: var(--tile-8-bg); color: var(--tile-8-color); box-shadow: var(--tile-8-shadow); }
.tile-16 { background: var(--tile-16-bg); color: var(--tile-16-color); box-shadow: var(--tile-16-shadow); }
.tile-32 { background: var(--tile-32-bg); color: var(--tile-32-color); box-shadow: var(--tile-32-shadow); }
.tile-64 { background: var(--tile-64-bg); color: var(--tile-64-color); box-shadow: var(--tile-64-shadow); }
.tile-128 { background: var(--tile-128-bg); color: var(--tile-128-color); box-shadow: var(--tile-128-shadow); }
.tile-256 { background: var(--tile-256-bg); color: var(--tile-256-color); box-shadow: var(--tile-256-shadow); }
.tile-512 { background: var(--tile-512-bg); color: var(--tile-512-color); box-shadow: var(--tile-512-shadow); }
.tile-1024 { background: var(--tile-1024-bg); color: var(--tile-1024-color); box-shadow: var(--tile-1024-shadow); }
.tile-2048 { background: var(--tile-2048-bg); color: var(--tile-2048-color); box-shadow: var(--tile-2048-shadow); }
.tile-super { background: var(--tile-super-bg); color: var(--tile-super-color); box-shadow: var(--tile-super-shadow); }

/* Tile Animations */
@keyframes tileAppear {
  0% {
    transform: scale(0);
    opacity: 0;
  }
  70% {
    transform: scale(1.12);
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes tilePop {
  0% {
    transform: scale(1);
  }
  40% {
    transform: scale(1.2);
    z-index: 5;
  }
  100% {
    transform: scale(1);
  }
}

.tile-new {
  animation: tileAppear 0.22s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

.tile-merged {
  animation: tilePop 0.22s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

/* ==========================================================================
   6. OVERLAYS & DIALOGS (Win / Game Over / Stats)
   ========================================================================== */
.overlay {
  position: absolute;
  inset: 0;
  background: var(--glass-bg);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 10;
  border-radius: var(--border-radius);
  border: 1px solid var(--glass-border);
  text-align: center;
  padding: 24px;
  
  /* Modern CSS Overlay Transitions */
  transition: opacity 0.3s ease, display 0.3s allow-discrete;
  opacity: 0;
}

.overlay.active {
  opacity: 1;
}

@starting-style {
  .overlay.active {
    opacity: 0;
  }
}

.overlay-title {
  font-size: 38px;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--text-color);
}

.theme-cyberpunk .overlay-title {
  text-shadow: 0 0 10px rgba(69, 243, 255, 0.5);
}

.overlay-score {
  font-size: 16px;
  font-weight: 600;
  opacity: 0.8;
}

.overlay-buttons {
  display: flex;
  gap: 10px;
}


/* ==========================================================================
   7. LOWER CONTROLS & STATS PANEL
   ========================================================================== */
.below-board {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 4px;
}

.instructions {
  font-size: 12px;
  opacity: 0.65;
  font-weight: 500;
  line-height: 1.4;
}

.instructions strong {
  color: var(--accent-color);
}

.theme-cyberpunk .instructions strong {
  text-shadow: 0 0 4px rgba(69, 243, 255, 0.3);
}

/* Key Guide indicators */
.key-guide {
  display: flex;
  gap: 4px;
  margin-top: 6px;
}

.key-cap {
  background: var(--board-bg);
  color: #ffffff;
  border-radius: 4px;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 700;
  box-shadow: 0 1px 3px rgba(0,0,0,0.15);
  transition: background-color 0.1s, transform 0.1s;
}

.key-cap.active {
  background: var(--accent-color);
  transform: scale(0.9);
}

/* Footer & signature */
footer {
  margin-top: 30px;
  text-align: center;
  font-size: 11px;
  opacity: 0.5;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}


/* ==========================================================================
   8. STATS DASHBOARD DRAWER / MODAL
   ========================================================================== */
.stats-drawer {
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  border-radius: var(--border-radius);
  padding: 16px;
  box-shadow: var(--box-shadow);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.stats-header {
  font-size: 14px;
  font-weight: 800;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--accent-color);
  border-bottom: 1px solid var(--glass-border);
  padding-bottom: 6px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.stat-item {
  background: var(--cell-bg);
  border-radius: 6px;
  padding: 8px 4px;
  text-align: center;
}

.stat-label {
  font-size: 9px;
  font-weight: 700;
  opacity: 0.6;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 2px;
}

.stat-val {
  font-size: 15px;
  font-weight: 800;
  font-family: 'Outfit', sans-serif;
}


/* ==========================================================================
   9. RESPONSIVE BREAKPOINTS
   ========================================================================== */
@media (max-width: 500px) {
  body {
    padding: 10px 12px 24px;
  }
  
  #app {
    max-width: 100%;
    gap: 12px;
  }
  
  .title {
    font-size: 44px;
  }
  
  .board-wrapper {
    padding: 10px;
  }
  
  .board-grid {
    gap: 8px;
  }
  
  .tiles-layer {
    top: 10px;
    left: 10px;
    right: 10px;
    bottom: 10px;
  }
  
  .cell {
    border-radius: 4px;
  }
  
  .tile {
    border-radius: 4px;
  }
  
  .score-box {
    min-width: 68px;
    padding: 4px 8px;
  }
  
  .score-box .value {
    font-size: 16px;
  }
  
  .btn {
    padding: 6px 10px;
    font-size: 12px;
  }
  
  .overlay-title {
    font-size: 30px;
  }
}

@media (max-width: 360px) {
  .title {
    font-size: 36px;
  }
  
  .score-box .value {
    font-size: 14px;
  }
  
  .btn-toolbar {
    gap: 4px;
  }
}
