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

:root {
  --orange:        #FF6B2B;
  --orange-hot:    #ff3d00;
  --bg:            #0f0f0f;
  --board-bg:      #1a1a1a;
  --cell-bg:       #242424;
  --text-muted:    rgba(255,255,255,0.38);
  --text-dim:      rgba(255,255,255,0.55);
  --radius-board:  16px;
  --radius-tile:   12px;
  --gap:           10px;
  --cell-size:     clamp(64px, 18vw, 110px);
  --font-title:    'Bricolage Grotesque', sans-serif;
  --font-ui:       'Geist', sans-serif;
  --transition-slide: 100ms ease;
}

html, body {
  height: 100%;
}

body {
  background: var(--bg);
  color: #fff;
  font-family: var(--font-ui);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  padding: 0 12px 32px;
}

/* ─── Canvas confetti overlay ──────────────────────────────────── */
#confetti {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 999;
}

/* ─── Layout wrapper ───────────────────────────────────────────── */
.wrapper {
  width: 100%;
  max-width: 520px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding-top: 28px;
}

/* ─── Header ───────────────────────────────────────────────────── */
.header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.header-left {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.logo {
  font-family: var(--font-title);
  font-weight: 800;
  font-size: clamp(36px, 10vw, 56px);
  line-height: 0.88;
  color: var(--orange);
  letter-spacing: -1.5px;
  display: flex;
  flex-direction: column;
  animation: logo-glow 3s ease-in-out infinite;
  text-shadow:
    0 0 20px rgba(255,107,43,0.6),
    0 0 60px rgba(255,107,43,0.2);
}

@keyframes logo-glow {
  0%, 100% {
    text-shadow:
      0 0 20px rgba(255,107,43,0.6),
      0 0 60px rgba(255,107,43,0.2);
  }
  50% {
    text-shadow:
      0 0 30px rgba(255,107,43,0.9),
      0 0 80px rgba(255,107,43,0.4),
      0 0 120px rgba(255,107,43,0.15);
  }
}

.logo.win-pulse {
  animation: logo-win-pulse 0.6s ease-in-out 3;
}

@keyframes logo-win-pulse {
  0%, 100% { text-shadow: 0 0 20px rgba(255,107,43,0.6); }
  50% {
    text-shadow:
      0 0 40px rgba(255,107,43,1),
      0 0 100px rgba(255,107,43,0.6),
      0 0 160px rgba(255,107,43,0.3);
  }
}

.subtitle {
  font-family: var(--font-ui);
  font-size: 13px;
  color: var(--text-dim);
  font-weight: 400;
  max-width: 220px;
  line-height: 1.4;
}

.subtitle strong {
  color: rgba(255,255,255,0.75);
  font-weight: 600;
}

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

/* ─── Score boxes ──────────────────────────────────────────────── */
.scores {
  display: flex;
  gap: 8px;
}

.score-box {
  background: #1e1e1e;
  border: 1px solid rgba(255,255,255,0.06);
  border-radius: 10px;
  padding: 8px 16px;
  text-align: center;
  min-width: 70px;
  position: relative;
  overflow: visible;
}

.score-label {
  display: block;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 2px;
}

.score-value {
  display: block;
  font-family: var(--font-title);
  font-weight: 700;
  font-size: 22px;
  color: #fff;
  line-height: 1;
}

/* Score pop animation */
.score-pop {
  position: absolute;
  top: -4px;
  left: 50%;
  transform: translateX(-50%);
  font-family: var(--font-title);
  font-size: 15px;
  font-weight: 700;
  color: var(--orange);
  pointer-events: none;
  animation: score-float 800ms ease-out forwards;
  white-space: nowrap;
  z-index: 100;
}

@keyframes score-float {
  0%   { opacity: 1; transform: translateX(-50%) translateY(0); }
  100% { opacity: 0; transform: translateX(-50%) translateY(-36px); }
}

/* ─── Buttons ──────────────────────────────────────────────────── */
.buttons {
  display: flex;
  gap: 8px;
}

.btn {
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 600;
  padding: 9px 18px;
  border-radius: 100px;
  border: 1px solid rgba(255,255,255,0.1);
  background: #1e1e1e;
  color: rgba(255,255,255,0.8);
  cursor: pointer;
  transition: background 140ms ease, color 140ms ease, border-color 140ms ease, box-shadow 140ms ease;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}

.btn:hover {
  background: var(--orange);
  color: #fff;
  border-color: var(--orange);
  box-shadow: 0 0 16px rgba(255,107,43,0.35);
}

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

.btn-secondary {
  background: transparent;
  border-color: rgba(255,255,255,0.12);
  color: var(--text-dim);
}

.btn-secondary:hover {
  background: rgba(255,107,43,0.15);
  border-color: var(--orange);
  color: var(--orange);
  box-shadow: none;
}

/* ─── Game Board ───────────────────────────────────────────────── */
.game-container {
  position: relative;
  width: 100%;
}

.board {
  position: relative;
  background: var(--board-bg);
  border-radius: var(--radius-board);
  box-shadow:
    0 0 40px rgba(255,107,43,0.08),
    0 0 0 1px rgba(255,255,255,0.04),
    inset 0 1px 0 rgba(255,255,255,0.04);
  padding: var(--gap);
  width: 100%;
  aspect-ratio: 1;
  user-select: none;
  -webkit-user-select: none;
}

.board-cells,
.tiles-container {
  position: absolute;
  inset: var(--gap);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--gap);
}

/* ─── Cells (empty squares) ────────────────────────────────────── */
.cell {
  background: var(--cell-bg);
  border-radius: var(--radius-tile);
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.4);
}

/* ─── Tiles ─────────────────────────────────────────────────────── */
.tiles-container {
  pointer-events: none;
}

.tile {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-tile);
  border: 1px solid rgba(255,255,255,0.08);
  font-family: var(--font-title);
  font-weight: 800;
  line-height: 1;
  transition: transform var(--transition-slide), top var(--transition-slide), left var(--transition-slide);
  will-change: transform, top, left;
  backface-visibility: hidden;
}

/* Tile font sizes based on value */
.tile[data-value="2"],
.tile[data-value="4"],
.tile[data-value="8"] {
  font-size: clamp(22px, 6vw, 44px);
}
.tile[data-value="16"],
.tile[data-value="32"],
.tile[data-value="64"] {
  font-size: clamp(20px, 5.5vw, 40px);
}
.tile[data-value="128"],
.tile[data-value="256"],
.tile[data-value="512"] {
  font-size: clamp(17px, 4.6vw, 34px);
}
.tile[data-value="1024"],
.tile[data-value="2048"] {
  font-size: clamp(14px, 3.8vw, 28px);
}
.tile[data-value="4096"],
.tile[data-value="8192"],
.tile[data-value="16384"] {
  font-size: clamp(12px, 3.2vw, 24px);
}

/* ─── Tile colour gradients ─────────────────────────────────────── */
.tile[data-value="2"]    { background: linear-gradient(135deg, #e8e0d5, #d4c9b8); color: #3a2e1e; }
.tile[data-value="4"]    { background: linear-gradient(135deg, #f0e6c8, #ddd0a0); color: #3a2e1e; }
.tile[data-value="8"]    { background: linear-gradient(135deg, #f7c07a, #e8963c); color: #fff; }
.tile[data-value="16"]   { background: linear-gradient(135deg, #f4a26b, #e0723a); color: #fff; }
.tile[data-value="32"]   { background: linear-gradient(135deg, #f07a5f, #d94f35); color: #fff; }
.tile[data-value="64"]   { background: linear-gradient(135deg, #e84e2a, #c42e10); color: #fff; }
.tile[data-value="128"]  { background: linear-gradient(135deg, #f5d76e, #e0b830); color: #fff; }
.tile[data-value="256"]  { background: linear-gradient(135deg, #f0c93a, #d4a010); color: #fff; }
.tile[data-value="512"]  { background: linear-gradient(135deg, #e8b820, #c49000); color: #fff; }
.tile[data-value="1024"] { background: linear-gradient(135deg, #7c3aed, #4c1d95); color: #fff; }
.tile[data-value="2048"] {
  background: linear-gradient(135deg, #FF6B2B, #ff3d00);
  color: #fff;
  animation: shimmer-sweep 2s linear infinite;
  background-size: 200% 100%;
}
.tile[data-value="4096"],
.tile[data-value="8192"],
.tile[data-value="16384"],
.tile[data-value="32768"] {
  background: conic-gradient(from 0deg, #0ea5e9, #6366f1, #ec4899, #0ea5e9);
  color: #fff;
  animation: galaxy-spin 3s linear infinite;
}

@keyframes shimmer-sweep {
  0%   { background-position: 200% center; }
  100% { background-position: -200% center; }
}

@keyframes galaxy-spin {
  from { filter: hue-rotate(0deg); }
  to   { filter: hue-rotate(360deg); }
}

/* ─── Tile spawn animation ──────────────────────────────────────── */
.tile-new {
  animation: tile-spawn 200ms cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

@keyframes tile-spawn {
  0%   { transform: scale(0); opacity: 0; }
  60%  { transform: scale(1.12); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

/* ─── Tile merge animation ──────────────────────────────────────── */
.tile-merge {
  animation: tile-pop 180ms cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
  z-index: 10;
}

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

/* Glow pulse for 128+ merges */
.tile-merge-glow::after {
  content: '';
  position: absolute;
  inset: -8px;
  border-radius: calc(var(--radius-tile) + 8px);
  background: radial-gradient(circle, rgba(255,200,50,0.5) 0%, transparent 70%);
  animation: glow-pulse 400ms ease-out forwards;
  pointer-events: none;
  z-index: -1;
}

@keyframes glow-pulse {
  0%   { opacity: 1; transform: scale(0.8); }
  100% { opacity: 0; transform: scale(1.6); }
}

/* Halo ring for 512+ merges */
.tile-merge-halo::before {
  content: '';
  position: absolute;
  inset: -4px;
  border-radius: calc(var(--radius-tile) + 4px);
  border: 3px solid rgba(255,215,0,0.8);
  animation: halo-expand 500ms ease-out forwards;
  pointer-events: none;
  z-index: -1;
}

@keyframes halo-expand {
  0%   { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(1.8); }
}

/* Lightning flicker for 1024 merges */
.tile-merge-lightning::before {
  content: '';
  position: absolute;
  inset: -6px;
  border-radius: calc(var(--radius-tile) + 6px);
  background: conic-gradient(
    rgba(139,92,246,0.9) 0deg 30deg,
    transparent 30deg 60deg,
    rgba(139,92,246,0.9) 60deg 90deg,
    transparent 90deg 180deg,
    rgba(167,139,250,0.7) 180deg 210deg,
    transparent 210deg 270deg,
    rgba(139,92,246,0.9) 270deg 300deg,
    transparent 300deg 360deg
  );
  animation: lightning-flicker 300ms ease-out forwards;
  pointer-events: none;
  z-index: -1;
}

@keyframes lightning-flicker {
  0%   { opacity: 1; transform: scale(1) rotate(0deg); }
  33%  { opacity: 0.7; transform: scale(1.3) rotate(30deg); }
  66%  { opacity: 1; transform: scale(1.5) rotate(-20deg); }
  100% { opacity: 0; transform: scale(1.8) rotate(45deg); }
}

/* ─── Overlays ───────────────────────────────────────────────────── */
.overlay {
  position: absolute;
  inset: 0;
  border-radius: var(--radius-board);
  background: rgba(10, 10, 10, 0.62);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  animation: overlay-in 250ms ease forwards;
}

.overlay[hidden] {
  display: none;
}

@keyframes overlay-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.overlay-content {
  text-align: center;
  padding: 32px 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.overlay-title {
  font-family: var(--font-title);
  font-weight: 800;
  font-size: clamp(36px, 10vw, 56px);
  line-height: 1.1;
  color: #fff;
}

.win-title {
  background: linear-gradient(135deg, #FF6B2B, #ffd700, #FF6B2B);
  background-size: 200% auto;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  animation: gradient-shift 2s linear infinite;
}

@keyframes gradient-shift {
  0%   { background-position: 0% center; }
  100% { background-position: 200% center; }
}

.overlay-score-text {
  font-family: var(--font-ui);
  font-size: 16px;
  color: var(--text-dim);
  font-weight: 500;
}

.overlay-score-text span {
  color: #fff;
  font-weight: 700;
}

.overlay-buttons {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
  margin-top: 4px;
}

.btn-overlay {
  font-size: 14px;
  padding: 11px 24px;
}

/* ─── How to play ─────────────────────────────────────────────── */
.how-to-play {
  font-size: 13px;
  line-height: 1.65;
  color: var(--text-muted);
  font-weight: 400;
}

.how-to-play strong {
  color: rgba(255,255,255,0.6);
  font-weight: 600;
}

/* ─── Footer ─────────────────────────────────────────────────── */
.footer {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 4px;
}

.footer-credit {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.5;
}

.footer-credit a,
.footer-cross-promo a {
  color: var(--text-muted);
  text-decoration: none;
  transition: color 140ms ease;
}

.footer-credit a:hover,
.footer-cross-promo a:hover {
  color: var(--orange);
}

.footer-cross-promo {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.5;
}

/* ─── Power-ups ──────────────────────────────────────────────── */
.powerups {
  display: flex;
  gap: 8px;
}

.powerup-btn {
  flex: 1;
  position: relative;
  background: #1a1a1a;
  border: 1px solid rgba(255,255,255,0.07);
  border-radius: 14px;
  padding: 14px 8px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  cursor: pointer;
  font-family: var(--font-ui);
  transition: background 160ms ease, border-color 160ms ease, box-shadow 160ms ease, opacity 160ms ease;
  -webkit-tap-highlight-color: transparent;
}

.powerup-btn:hover:not(.locked) {
  background: #222;
  border-color: rgba(255,255,255,0.14);
}

.powerup-btn:active:not(.locked) {
  transform: scale(0.97);
}

.powerup-btn.locked {
  opacity: 0.38;
  cursor: not-allowed;
}

.powerup-btn.active {
  background: rgba(255,107,43,0.1);
  border-color: var(--orange);
  box-shadow: 0 0 20px rgba(255,107,43,0.25), inset 0 0 12px rgba(255,107,43,0.06);
}

/* Badge — use counter */
.powerup-badge {
  position: absolute;
  top: 7px;
  right: 7px;
  font-family: var(--font-title);
  font-weight: 700;
  font-size: 11px;
  line-height: 1;
  background: var(--orange);
  color: #fff;
  border-radius: 100px;
  padding: 3px 6px;
  min-width: 20px;
  text-align: center;
}

.powerup-badge[hidden] { display: none; }

/* Icon */
.powerup-icon {
  font-size: 20px;
  line-height: 1;
  color: rgba(255,255,255,0.75);
  transition: color 160ms ease;
}

.powerup-btn.active .powerup-icon {
  color: var(--orange);
}

/* Label */
.powerup-label {
  font-size: 13px;
  font-weight: 600;
  color: rgba(255,255,255,0.8);
  line-height: 1;
}

/* Sub-label */
.powerup-sub {
  font-size: 10px;
  font-weight: 400;
  color: var(--text-muted);
  line-height: 1.2;
  text-align: center;
  min-height: 12px;
  transition: color 160ms ease;
}

.powerup-btn.active .powerup-sub {
  color: rgba(255,107,43,0.8);
}

.powerup-btn.unavailable {
  opacity: 0.3;
  cursor: default;
}

/* ─── Tile interaction (swap / delete modes) ─────────────────── */
.board.mode-swap .tiles-container,
.board.mode-delete .tiles-container {
  pointer-events: auto;
}

.board.mode-swap .tile,
.board.mode-delete .tile {
  cursor: pointer;
  transition:
    top var(--transition-slide),
    left var(--transition-slide),
    filter 120ms ease,
    transform 120ms ease,
    outline 120ms ease;
}

.board.mode-swap .tile:hover,
.board.mode-delete .tile:hover {
  filter: brightness(1.2);
  transform: scale(1.06);
  z-index: 20;
}

/* First-tile selection ring for swap */
.tile.swap-selected {
  outline: 3px solid var(--orange);
  outline-offset: -3px;
  filter: brightness(1.15);
  z-index: 20;
}

/* Delete hover: highlight matching, dim others */
.tile.will-delete {
  outline: 3px solid rgba(255,107,43,0.9);
  outline-offset: -3px;
  filter: brightness(1.25);
  z-index: 20;
}

.tile.wont-delete {
  filter: brightness(0.45);
}

/* Tile vanish animation (delete power-up) */
.tile-vanish {
  animation: tile-vanish 220ms ease-out forwards;
  pointer-events: none;
}

@keyframes tile-vanish {
  0%   { transform: scale(1);   opacity: 1; }
  60%  { transform: scale(1.1); opacity: 0.6; }
  100% { transform: scale(0);   opacity: 0; }
}

/* ─── Responsive adjustments ──────────────────────────────────── */
@media (max-width: 400px) {
  :root {
    --gap: 8px;
  }

  .wrapper {
    gap: 14px;
    padding-top: 18px;
  }

  .logo {
    font-size: 44px;
  }

  .scores {
    gap: 6px;
  }

  .score-box {
    padding: 7px 12px;
    min-width: 60px;
  }

  .score-value {
    font-size: 18px;
  }

  .buttons {
    gap: 6px;
  }

  .btn {
    font-size: 12px;
    padding: 8px 14px;
  }
}

@media (max-width: 340px) {
  .header {
    flex-direction: column;
  }

  .header-right {
    align-items: flex-start;
    width: 100%;
  }

  .scores {
    width: 100%;
  }

  .score-box {
    flex: 1;
  }

  .buttons {
    width: 100%;
  }

  .btn {
    flex: 1;
    text-align: center;
  }
}
