:root {
  --bg-color: #faf8ef;
  --grid-bg-color: #bbada0;
  --empty-cell-color: #cdc1b4;
  --text-color: #776e65;
  --text-light: #f9f6f2;
  
  /* Cupcake Colors */
  --tile-2-bg: #fff4e6; /* Vanilla */
  --tile-2-color: #776e65;
  
  --tile-4-bg: #ffe0e9; /* Pink */
  --tile-4-color: #776e65;

  --tile-8-bg: #fff9db; /* Yellow */
  --tile-8-color: #776e65;

  --tile-16-bg: #e6fcf5; /* Mint */
  --tile-16-color: #776e65;

  --tile-32-bg: #e7f5ff; /* Blue */
  --tile-32-color: #776e65;

  --tile-64-bg: #f3d9fa; /* Lavender */
  --tile-64-color: #f9f6f2;

  --tile-128-bg: #d9480f; /* Choco - Deep Orange/Brownish */
  --tile-128-color: #f9f6f2;

  --tile-256-bg: #f06595; /* Raspberry */
  --tile-256-color: #f9f6f2;

  --tile-512-bg: #fd7e14; /* Orange */
  --tile-512-color: #f9f6f2;

  --tile-1024-bg: #20c997; /* Teal */
  --tile-1024-color: #f9f6f2;

  --tile-2048-bg: #fcc419; /* Gold */
  --tile-2048-color: #f9f6f2;

  --tile-super-bg: #343a40;
  --tile-super-color: #f9f6f2;
}

body {
  margin: 0;
  padding: 0;
  background-color: var(--bg-color);
  color: var(--text-color);
  font-family: 'Open Sans', "Helvetica Neue", Arial, sans-serif;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}

.container {
  width: 100%;
  max-width: 500px;
  padding: 20px;
  box-sizing: border-box;
}

header {
  display: flex;
  flex-direction: column;
  margin-bottom: 20px;
}

.header-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.title {
  font-family: 'Fredoka One', cursive;
  font-size: 48px;
  margin: 0;
  color: #776e65;
}

.subtitle {
  color: #ff8787;
}

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

.score-box {
  background: #bbada0;
  padding: 5px 15px;
  border-radius: 6px;
  color: white;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 50px;
}

.score-label {
  font-size: 11px;
  text-transform: uppercase;
  color: #eee4da;
  font-weight: bold;
}

#score, #best-score {
  font-size: 20px;
  font-weight: bold;
}

.header-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.game-intro {
  margin: 0;
  font-size: 16px;
  line-height: 1.4;
}

#new-game-btn {
  background: #8f7a66;
  color: #f9f6f2;
  border: none;
  border-radius: 6px;
  padding: 10px 20px;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  outline: none;
  transition: background 0.2s;
}

#new-game-btn:hover {
  background: #7f6a56;
}

/* Game Board */
.game-container {
  position: relative;
  width: 100%;
  padding-bottom: 100%; /* Aspect Ratio 1:1 */
  background: var(--grid-bg-color);
  border-radius: 10px;
  touch-action: none; /* Prevent default scroll on mobile */
  margin-bottom: 20px;
}

.grid-container {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 15px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 15px;
}

.grid-cell {
  background: var(--empty-cell-color);
  border-radius: 6px;
}

.tile-container {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 15px;
  pointer-events: none; /* Let clicks pass through */
}

.tile {
  position: absolute;
  width: calc(25% - 11.25px); /* (100% - 3 * 15px gap) / 4 */
  height: calc(25% - 11.25px);
  border-radius: 6px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  font-weight: bold;
  font-size: 30px;
  transition: transform 100ms ease-in-out;
  animation: appear 200ms ease;
  z-index: 10;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

.tile-inner {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
}

.cupcake-emoji {
    font-size: 40px;
    line-height: 1;
    margin-bottom: 5px;
}

.cupcake-val {
    font-size: 14px;
    font-family: 'Fredoka One', cursive;
}

/* Responsive Tile Sizes */
@media (max-width: 520px) {
  .tile {
    font-size: 24px;
  }
  .cupcake-emoji {
      font-size: 32px;
  }
  .cupcake-val {
      font-size: 12px;
  }
  .grid-container, .tile-container {
    padding: 10px;
    gap: 10px;
  }
  .tile {
      width: calc(25% - 7.5px);
      height: calc(25% - 7.5px);
  }
  .title {
      font-size: 32px;
  }
}

/* Tile Colors & Images */
.tile {
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  color: #f9f6f2;
  text-shadow: 1px 1px 2px rgba(0,0,0,0.6);
}

.tile-inner {
  background: rgba(0, 0, 0, 0.2); /* Slight overlay for readability */
  border-radius: 6px;
}

.cupcake-emoji {
  display: none; /* Hide emoji as we use images */
}

.cupcake-val {
  font-size: 24px;
  font-weight: 800;
  z-index: 2;
}

/* Specific Tile Styles */
.tile-2 { background-image: url('assets/cupcake-2.webp'); }
.tile-4 { background-image: url('assets/cupcake-4.webp'); }
.tile-8 { background-image: url('assets/cupcake-8.webp'); }
.tile-16 { background-image: url('assets/cupcake-16.webp'); }
.tile-32 { background-image: url('assets/cupcake-32.webp'); }
.tile-64 { background-image: url('assets/cupcake-64.webp'); }
.tile-128 { background-image: url('assets/cupcake-128.webp'); }
.tile-256 { background-image: url('assets/cupcake-256.webp'); }
.tile-512 { background-image: url('assets/cupcake-512.webp'); }
.tile-1024 { background-image: url('assets/cupcake-1024.webp'); }
.tile-2048 { background-image: url('assets/cupcake-2048.webp'); }

/* Higher levels */
.tile-4096 { background-image: url('assets/cupcake-4096.webp'); font-size: 30px; }
.tile-8192 { background-image: url('assets/cupcake-8192.webp'); font-size: 30px; }
.tile-16384 { background-image: url('assets/cupcake-16384.webp'); font-size: 24px; }
.tile-32768 { background-image: url('assets/cupcake-32768.webp'); font-size: 24px; }
.tile-65536 { background-image: url('assets/cupcake-65536.webp'); font-size: 24px; }
.tile-131072 { background-image: url('assets/cupcake-131072.webp'); font-size: 20px; }

.tile-super { background-image: url('assets/cupcake-131072.webp'); filter: hue-rotate(135deg); }


/* Fix filter affecting text color too much?
   The filter applies to the whole element. 
   To fix text color, we might need to counter-rotate or put text outside the filtered element.
   But for a simple game, hue-rotating the whole tile is an acceptable aesthetic choice 
   as long as text is white/black (grayscale isn't affected by hue-rotate much, except for tinting).
   White #f9f6f2 will stay mostly white. 
*/


/* Animations */
@keyframes appear {
  0% { opacity: 0; transform: scale(0); }
  100% { opacity: 1; transform: scale(1); }
}

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

.tile-merged {
  z-index: 20;
  animation: pop 200ms ease 100ms;
  animation-fill-mode: backwards;
}

/* Game Message (Game Over / Win) */
.game-message {
  display: none;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(238, 228, 218, 0.73);
  z-index: 100;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  animation: fade-in 800ms ease 1200ms;
  animation-fill-mode: both;
}

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

.game-message.game-won {
  display: flex;
  background: rgba(237, 194, 46, 0.5);
  color: #f9f6f2;
}

.game-message p {
  font-size: 60px;
  font-weight: bold;
  height: 60px;
  line-height: 60px;
  margin-top: 222px;
}

.game-message .lower {
  display: block;
  margin-top: 59px;
}

.game-message a {
  display: inline-block;
  background: #8f7a66;
  border-radius: 3px;
  padding: 0 20px;
  text-decoration: none;
  color: #f9f6f2;
  height: 40px;
  line-height: 42px;
  cursor: pointer;
  margin-left: 9px;
}

.controls-hint {
    margin-top: 20px;
    font-size: 14px;
    color: #776e65;
    text-align: center;
}

/* Particles */
.particle {
  position: absolute;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  pointer-events: none;
  z-index: 50;
  animation: fade-out 0.8s forwards;
}

@keyframes fade-out {
  0% { opacity: 1; transform: translate(0, 0) scale(1); }
  100% { opacity: 0; transform: translate(var(--tx), var(--ty)) scale(0); }
}