/* ── Reset & Base ─────────────────────────────────────── */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  font-family: 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
  background: #1a1a2e;
  color: #f0f0f0;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* ── Back Link ───────────────────────────────────────── */
.back-link {
  position: fixed;
  top: 16px;
  left: 18px;
  color: #8888aa;
  text-decoration: none;
  font-size: 15px;
  z-index: 100;
  transition: color 0.2s;
}
.back-link:hover {
  color: #ccccee;
}

/* ── Layout ──────────────────────────────────────────── */
.game-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 60px 16px 32px;
  max-width: 700px;
  margin: 0 auto;
}

.title {
  font-size: clamp(36px, 8vmin, 56px);
  font-weight: 800;
  letter-spacing: 2px;
  margin-bottom: 10px;
  color: #edc22e;
  text-shadow: 0 2px 8px rgba(237, 194, 46, 0.25);
}

/* ── Score Bar ────────────────────────────────────────── */
.score-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 24px;
  margin-bottom: 12px;
  width: 100%;
  max-width: min(85vmin, 500px);
}

.score-label {
  font-size: clamp(18px, 4vmin, 24px);
  font-weight: 700;
  color: #f0f0f0;
}

.new-game-btn {
  padding: 8px 18px;
  font-size: 14px;
  font-weight: 700;
  border: none;
  border-radius: 6px;
  background: #8f7a66;
  color: #f9f6f2;
  cursor: pointer;
  transition: background 0.2s;
}
.new-game-btn:hover {
  background: #a08b7a;
}

/* ── Difficulty Bar ──────────────────────────────────── */
.difficulty-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.diff-btn {
  padding: 7px 16px;
  font-size: 13px;
  font-weight: 700;
  border: 2px solid #555;
  border-radius: 6px;
  background: transparent;
  color: #aaa;
  cursor: pointer;
  transition: background 0.2s, color 0.2s, border-color 0.2s;
}
.diff-btn:hover {
  border-color: #888;
  color: #ddd;
}
.diff-btn.active {
  background: #edc22e;
  color: #1a1a2e;
  border-color: #edc22e;
}

/* ── Grid Container ──────────────────────────────────── */
.game-container {
  position: relative;
  width: min(85vmin, 500px);
  height: min(85vmin, 500px);
  border-radius: 10px;
  overflow: hidden;
  background: #bbada0;
  user-select: none;
  touch-action: none;
}

/* ── Background Cells ────────────────────────────────── */
.grid-background {
  position: absolute;
  inset: 0;
  display: grid;
  /* columns/rows set dynamically via JS */
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: calc(min(85vmin, 500px) * 0.028);
  padding: calc(min(85vmin, 500px) * 0.028);
}

.cell {
  background: #cdc1b4;
  border-radius: 6px;
}

/* ── Tile Layer ──────────────────────────────────────── */
.tile-layer {
  position: absolute;
  inset: 0;
  /* tiles positioned absolutely inside here */
}

/* ── Tile ─────────────────────────────────────────────── */
.tile {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  border-radius: 6px;
  transition: top 150ms ease, left 150ms ease;
  z-index: 1;
}

/* ── Tile Size & Font ────────────────────────────────── */
.tile .tile-inner {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  border-radius: 6px;
  font-weight: 800;
}

/* ── Tile Values ─────────────────────────────────────── */
.tile[data-value="2"]    .tile-inner { background: #eee4da; color: #776e65; }
.tile[data-value="4"]    .tile-inner { background: #ede0c8; color: #776e65; }
.tile[data-value="8"]    .tile-inner { background: #f2b179; color: #f9f6f2; }
.tile[data-value="16"]   .tile-inner { background: #f59563; color: #f9f6f2; }
.tile[data-value="32"]   .tile-inner { background: #f67c5f; color: #f9f6f2; }
.tile[data-value="64"]   .tile-inner { background: #f65e3b; color: #f9f6f2; }
.tile[data-value="128"]  .tile-inner { background: #edcf72; color: #f9f6f2; }
.tile[data-value="256"]  .tile-inner { background: #edcc61; color: #f9f6f2; }
.tile[data-value="512"]  .tile-inner { background: #edc850; color: #f9f6f2; }
.tile[data-value="1024"] .tile-inner { background: #edc53f; color: #f9f6f2; }
.tile[data-value="2048"] .tile-inner { background: #edc22e; color: #f9f6f2; }
.tile[data-value="4096"] .tile-inner { background: #b784e0; color: #f9f6f2; }
.tile[data-value="super"] .tile-inner { background: #3c3a32; color: #f9f6f2; }

/* ── Animations ──────────────────────────────────────── */
@keyframes pop-in {
  0%   { transform: scale(0); opacity: 0; }
  60%  { transform: scale(1.12); opacity: 1; }
  100% { transform: scale(1); }
}

@keyframes merge-pulse {
  0%   { transform: scale(1); }
  40%  { transform: scale(1.22); }
  100% { transform: scale(1); }
}

.tile-new .tile-inner {
  animation: pop-in 200ms ease forwards;
}

.tile-merged .tile-inner {
  animation: merge-pulse 200ms ease forwards;
  z-index: 2;
}

/* ── Overlays ────────────────────────────────────────── */
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  z-index: 50;
  opacity: 1;
  transition: opacity 0.35s ease;
}

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

.overlay-gameover {
  background: rgba(26, 26, 46, 0.78);
}

.overlay-win {
  background: rgba(237, 194, 46, 0.22);
  backdrop-filter: blur(3px);
}

.overlay-content {
  text-align: center;
}

.overlay-content h2 {
  font-size: clamp(28px, 6vmin, 42px);
  margin-bottom: 12px;
}

.overlay-content p {
  font-size: clamp(16px, 3.5vmin, 22px);
  margin-bottom: 20px;
  color: #ccc;
}

.overlay-btn {
  padding: 10px 28px;
  margin: 6px 8px;
  font-size: 16px;
  font-weight: 700;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  background: #8f7a66;
  color: #f9f6f2;
  transition: background 0.2s;
}
.overlay-btn:hover {
  background: #a08b7a;
}

/* ── Hint ────────────────────────────────────────────── */
.hint {
  margin-top: 16px;
  font-size: 14px;
  color: #666680;
}

/* ── How to Play ─────────────────────────────────────── */
.how-to-play {
  margin-top: 24px;
  width: 100%;
  max-width: min(85vmin, 500px);
  background: #16213e;
  border: 1px solid #2a2a4a;
  border-radius: 8px;
  overflow: hidden;
}

.how-to-play summary {
  padding: 12px 18px;
  font-size: 16px;
  font-weight: 700;
  color: #edc22e;
  cursor: pointer;
  user-select: none;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
}

.how-to-play summary::-webkit-details-marker {
  display: none;
}

.how-to-play summary::before {
  content: '\25B6';
  font-size: 11px;
  transition: transform 0.2s;
  display: inline-block;
}

.how-to-play[open] summary::before {
  transform: rotate(90deg);
}

.how-to-play-content {
  padding: 4px 18px 16px;
}

.how-to-play-content ul {
  list-style: none;
  padding: 0;
}

.how-to-play-content li {
  position: relative;
  padding: 6px 0 6px 20px;
  font-size: 14px;
  color: #bbb;
  line-height: 1.5;
}

.how-to-play-content li::before {
  content: '\2022';
  position: absolute;
  left: 4px;
  color: #edc22e;
  font-weight: bold;
}

.how-to-play-content strong {
  color: #f0f0f0;
}

/* ── Responsive tweaks ───────────────────────────────── */
@media (max-width: 560px) {
  .game-container {
    width: calc(90vw - 16px);
    height: calc(90vw - 16px);
  }
  .grid-background {
    gap: calc((90vw - 16px) * 0.028);
    padding: calc((90vw - 16px) * 0.028);
  }
  .score-bar {
    max-width: calc(90vw - 16px);
  }
  .how-to-play {
    max-width: calc(90vw - 16px);
  }
  .back-link {
    font-size: 13px;
    top: 10px;
    left: 12px;
  }
  .game-wrapper {
    padding-top: 44px;
  }
}
