:root {
  --bg: #faf8ef;
  --board-bg: #bbada0;
  --cell-bg: #cdc1b4;
  --text: #776e65;
  --text-light: #f9f6f2;
  --score-bg: #bbada0;
  --button-bg: #8f7a66;
  --button-bg-hover: #9f8a76;
  --board-padding: 8px;
  --cell-gap: 8px;
  --cell-size: calc((100cqi - 3 * var(--cell-gap)) / 4);

  --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;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1c1c;
    --board-bg: #3a3530;
    --cell-bg: #4a4540;
    --text: #e8e8e8;
  }
  /* Overlays: light text on a dark backdrop so it's legible. */
  #game-over-overlay, #win-overlay {
    background: rgba(28, 28, 28, 0.85);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}

#app {
  max-width: 520px;
  margin: 0 auto;
  padding: 16px;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
header h1 { margin: 0; font-size: 56px; font-weight: 800; }
.scores { display: flex; gap: 8px; }
.score-box {
  background: var(--score-bg);
  color: var(--text-light);
  padding: 6px 14px;
  border-radius: 6px;
  text-align: center;
  min-width: 70px;
}
.score-box .label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  opacity: 0.85;
}
.score-box div + div { font-size: 20px; font-weight: 700; }

#board {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  background: var(--board-bg);
  border-radius: 8px;
  padding: var(--board-padding);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--cell-gap);
  touch-action: none;
  user-select: none;
}
.cell {
  background: var(--cell-bg);
  border-radius: 4px;
}
.tile-layer {
  position: absolute;
  inset: var(--board-padding);
  pointer-events: none;
  container-type: inline-size;
}
.tile {
  position: absolute;
  width: var(--cell-size);
  height: var(--cell-size);
  border-radius: 4px;
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: clamp(20px, 8vw, 48px);
  background: var(--tile-2);
  color: var(--text);
  transition: transform 100ms ease-out;
  will-change: transform;
}
.tile-2    { background: var(--tile-2); color: #776e65; }
.tile-4    { background: var(--tile-4); color: #776e65; }
.tile-8    { background: var(--tile-8);   color: var(--text-light); }
.tile-16   { background: var(--tile-16);  color: var(--text-light); }
.tile-32   { background: var(--tile-32);  color: var(--text-light); }
.tile-64   { background: var(--tile-64);  color: var(--text-light); }
.tile-128  { background: var(--tile-128); color: var(--text-light); font-size: clamp(18px, 7vw, 42px); }
.tile-256  { background: var(--tile-256); color: var(--text-light); font-size: clamp(18px, 7vw, 42px); }
.tile-512  { background: var(--tile-512); color: var(--text-light); font-size: clamp(18px, 7vw, 42px); }
.tile-1024 { background: var(--tile-1024); color: var(--text-light); font-size: clamp(15px, 6vw, 36px); }
.tile-2048 { background: var(--tile-2048); color: var(--text-light); font-size: clamp(15px, 6vw, 36px); }
.tile-4096, .tile-8192 { background: var(--tile-super); color: var(--text-light); font-size: clamp(15px, 6vw, 36px); }

.tile-new      { animation: spawn 100ms ease-out; }
.tile-merged   { animation: pulse 150ms ease-out; }
.tile-fade-out { animation: fadeOut 200ms ease-out forwards; pointer-events: none; }
@keyframes fadeOut { to { opacity: 0; } }
@keyframes spawn { from { transform: scale(0) translate(var(--last-x, 0), var(--last-y, 0)); } }
@keyframes pulse {
  0%   { transform-origin: center; }
  40%  { filter: brightness(1.15); }
  100% { filter: brightness(1); }
}

.controls {
  display: flex;
  gap: 8px;
  margin: 12px 0;
}
.controls button {
  flex: 1;
  padding: 12px;
  font-size: 16px;
  font-weight: 600;
  border: 0;
  border-radius: 6px;
  background: var(--button-bg);
  color: var(--text-light);
  cursor: pointer;
  min-height: 44px;
}
.controls button:hover { background: var(--button-bg-hover); }
.controls button:disabled { opacity: 0.5; cursor: not-allowed; }

#game-over-overlay, #win-overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(238, 228, 218, 0.73);
  backdrop-filter: blur(2px);
  text-align: center;
  z-index: 10;
}
#game-over-overlay[hidden], #win-overlay[hidden] { display: none; }
#game-over-overlay p, #win-overlay p {
  font-size: 32px;
  font-weight: 800;
  color: var(--text);
}
#game-over-overlay button, #win-overlay button {
  margin-top: 16px;
  padding: 12px 24px;
  font-size: 16px;
  font-weight: 600;
  border: 0;
  border-radius: 6px;
  background: var(--button-bg);
  color: var(--text-light);
  cursor: pointer;
}

.ad-slot { display: flex; align-items: center; justify-content: center; }
#ad-bottom {
  width: 100%;
  height: 50px;
  margin-top: 16px;
}
#ad-side { display: none; }
.ad-placeholder {
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.05);
  border: 1px dashed rgba(0,0,0,0.2);
  display: grid;
  place-items: center;
  font-size: 12px;
  color: rgba(0,0,0,0.5);
}
.ads-disabled .ad-slot { display: none !important; }

@media (min-width: 768px) {
  body { display: grid; grid-template-columns: 1fr min-content; gap: 16px; max-width: 800px; margin: 0 auto; align-items: start; padding: 24px; }
  #app { max-width: none; padding: 0; }
  #ad-bottom { display: none; }
  #ad-side {
    display: flex;
    width: 160px;
    height: 600px;
    position: sticky;
    top: 24px;
  }
}
