*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --board-bg: #bbada0;
  --cell-bg: rgba(238, 228, 218, 0.35);
  --page-bg: #faf8ef;
  --text-dark: #776e65;
  --text-light: #f9f6f2;
  --btn-bg: #8f7a66;
  --btn-hover: #9f8b77;
  --btn-text: #f9f6f2;
  --board-gap: 12px;
  --cell-radius: 6px;
  --tile-radius: 6px;
  --header-height: auto;
}

html, body {
  height: 100%;
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: linear-gradient(135deg, #faf8ef 0%, #f0e6d3 50%, #faf8ef 100%);
  color: var(--text-dark);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px);
}

#game-container {
  width: 100%;
  max-width: 500px;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px 12px 8px;
  gap: 10px;
}

/* ─── HEADER ─── */
#game-header {
  width: 100%;
}

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

#game-title {
  font-size: 2.8rem;
  font-weight: 900;
  color: var(--text-dark);
  line-height: 1;
  letter-spacing: -1px;
}

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

.score-box {
  background: var(--board-bg);
  border-radius: 6px;
  padding: 6px 16px;
  text-align: center;
  min-width: 72px;
  position: relative;
}

.score-label {
  display: block;
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: #eee4da;
}

.score-value {
  display: block;
  font-size: 1.25rem;
  font-weight: 800;
  color: #fff;
  line-height: 1.2;
  transition: transform 0.15s ease;
}

.score-value.bump {
  animation: score-bump 0.3s ease;
}

@keyframes score-bump {
  0% { transform: scale(1); }
  40% { transform: scale(1.25); }
  100% { transform: scale(1); }
}

.new-best {
  display: none;
  position: absolute;
  top: -8px;
  right: -8px;
  background: #ff6b6b;
  color: #fff;
  font-size: 0.5rem;
  font-weight: 800;
  padding: 2px 5px;
  border-radius: 8px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  animation: pulse-badge 0.8s ease-in-out infinite;
}

.new-best.visible {
  display: block;
}

@keyframes pulse-badge {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

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

.tagline {
  font-size: 0.8rem;
  color: #776e65;
  opacity: 0.8;
}

.header-buttons {
  display: flex;
  gap: 6px;
}

/* ─── BUTTONS ─── */
.btn {
  background: var(--btn-bg);
  color: var(--btn-text);
  border: none;
  border-radius: 6px;
  padding: 8px 16px;
  font-size: 0.85rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  white-space: nowrap;
}

.btn:hover {
  background: var(--btn-hover);
}

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

.btn-primary {
  background: #8f7a66;
}

.btn-sm {
  padding: 5px 10px;
  font-size: 0.75rem;
}

/* ─── BOARD ─── */
#game-main {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

#board-wrapper {
  width: 100%;
  display: flex;
  justify-content: center;
}

#board {
  position: relative;
  background: var(--board-bg);
  border-radius: 8px;
  padding: var(--board-gap);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  aspect-ratio: 1 / 1;
  width: 100%;
}

.grid-bg {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--board-gap);
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  padding: var(--board-gap);
}

.grid-cell {
  background: var(--cell-bg);
  border-radius: var(--cell-radius);
}

#tile-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  padding: var(--board-gap);
  pointer-events: none;
}

/* ─── TILES ─── */
.tile {
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: 800;
  border-radius: var(--tile-radius);
  transition: top 0.12s ease, left 0.12s ease;
  will-change: top, left, transform;
  z-index: 10;
}

.tile.merged {
  z-index: 20;
}

/* Tile value styles */
.tile-2 {
  background: linear-gradient(135deg, #eee4da, #e8ddd0);
  color: #776e65;
  font-size: 2rem;
  box-shadow: 0 2px 6px rgba(0,0,0,0.08);
}

.tile-4 {
  background: linear-gradient(135deg, #ede0c8, #e6d5b8);
  color: #776e65;
  font-size: 2rem;
  box-shadow: 0 2px 6px rgba(0,0,0,0.08);
}

.tile-8 {
  background: linear-gradient(135deg, #f2b179, #e8a462);
  color: var(--text-light);
  font-size: 2rem;
  box-shadow: 0 3px 8px rgba(0,0,0,0.12);
}

.tile-16 {
  background: linear-gradient(135deg, #f59563, #e8854a);
  color: var(--text-light);
  font-size: 1.8rem;
  box-shadow: 0 3px 8px rgba(0,0,0,0.12);
}

.tile-32 {
  background: linear-gradient(135deg, #f67c5f, #e86840);
  color: var(--text-light);
  font-size: 1.8rem;
  box-shadow: 0 4px 10px rgba(0,0,0,0.15);
}

.tile-64 {
  background: linear-gradient(135deg, #f65e3b, #e04e2a);
  color: var(--text-light);
  font-size: 1.8rem;
  box-shadow: 0 4px 10px rgba(0,0,0,0.15);
}

.tile-128 {
  background: linear-gradient(135deg, #edcf72, #dfc060);
  color: var(--text-light);
  font-size: 1.5rem;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15), 0 0 12px rgba(237, 207, 114, 0.3);
}

.tile-256 {
  background: linear-gradient(135deg, #edcc61, #ddb84e);
  color: var(--text-light);
  font-size: 1.5rem;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15), 0 0 16px rgba(237, 204, 97, 0.35);
}

.tile-512 {
  background: linear-gradient(135deg, #edc850, #ddb33d);
  color: var(--text-light);
  font-size: 1.5rem;
  box-shadow: 0 4px 14px rgba(0,0,0,0.18), 0 0 20px rgba(237, 200, 80, 0.4);
}

.tile-1024 {
  background: linear-gradient(135deg, #edc53f, #dcb02c);
  color: var(--text-light);
  font-size: 1.2rem;
  box-shadow: 0 5px 16px rgba(0,0,0,0.2), 0 0 28px rgba(237, 197, 63, 0.5);
}

.tile-2048 {
  background: linear-gradient(135deg, #edc22e, #d9a81a);
  color: var(--text-light);
  font-size: 1.2rem;
  box-shadow: 0 5px 20px rgba(0,0,0,0.25), 0 0 40px rgba(237, 194, 46, 0.6);
  animation: tile-2048-glow 2s ease-in-out infinite;
}

@keyframes tile-2048-glow {
  0%, 100% { box-shadow: 0 5px 20px rgba(0,0,0,0.25), 0 0 40px rgba(237, 194, 46, 0.6); }
  50% { box-shadow: 0 5px 25px rgba(0,0,0,0.3), 0 0 60px rgba(237, 194, 46, 0.8); }
}

.tile-super {
  background: linear-gradient(135deg, #3c3a32, #1a1915);
  color: #f9f6f2;
  font-size: 1rem;
  box-shadow: 0 5px 20px rgba(0,0,0,0.3), 0 0 50px rgba(255, 215, 0, 0.5);
  animation: tile-super-glow 1.5s ease-in-out infinite;
}

@keyframes tile-super-glow {
  0%, 100% { box-shadow: 0 5px 20px rgba(0,0,0,0.3), 0 0 50px rgba(255, 215, 0, 0.5); }
  50% { box-shadow: 0 5px 30px rgba(0,0,0,0.4), 0 0 70px rgba(255, 215, 0, 0.8); }
}

/* ─── TILE ANIMATIONS ─── */
@keyframes tile-pop {
  0% { transform: scale(1); }
  40% { transform: scale(1.2); }
  100% { transform: scale(1); }
}

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

.tile.pop {
  animation: tile-pop 0.2s ease-in-out;
  z-index: 30;
}

.tile.spawn {
  animation: tile-spawn 0.15s ease-out;
}

/* ─── GAME ACTIONS ─── */
#game-actions {
  display: flex;
  gap: 8px;
}

/* ─── MOBILE CONTROLS ─── */
#mobile-controls {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  margin-top: 4px;
}

.ctrl-row {
  display: flex;
  gap: 4px;
}

.ctrl-btn {
  width: 60px;
  height: 60px;
  border: none;
  border-radius: 10px;
  background: var(--board-bg);
  color: #fff;
  font-size: 1.4rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.1s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
}

.ctrl-btn:active {
  background: #6d5d4e;
  transform: scale(0.92);
}

/* ─── OVERLAYS ─── */
.overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(238, 228, 218, 0.73);
  z-index: 100;
  justify-content: center;
  align-items: center;
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  padding: 20px;
}

.overlay.active {
  display: flex;
}

.overlay-content {
  background: #faf8ef;
  border-radius: 12px;
  padding: 32px 40px;
  text-align: center;
  max-width: 380px;
  width: 100%;
  box-shadow: 0 10px 40px rgba(0,0,0,0.2);
  animation: modal-in 0.25s ease-out;
}

@keyframes modal-in {
  0% { transform: scale(0.85); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

.overlay-icon {
  font-size: 3rem;
  margin-bottom: 8px;
}

.overlay-title {
  font-size: 1.8rem;
  font-weight: 900;
  color: var(--text-dark);
  margin-bottom: 8px;
}

.overlay-score {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-dark);
  margin-bottom: 4px;
}

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

/* ─── HOW TO PLAY MODAL ─── */
.modal {
  background: #faf8ef;
  border-radius: 12px;
  padding: 28px 32px;
  max-width: 440px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 10px 40px rgba(0,0,0,0.2);
  animation: modal-in 0.25s ease-out;
}

.modal-title {
  font-size: 1.6rem;
  font-weight: 900;
  text-align: center;
  margin-bottom: 16px;
  color: var(--text-dark);
}

.howto-section {
  margin-bottom: 14px;
}

.howto-section h3 {
  font-size: 0.95rem;
  font-weight: 800;
  margin-bottom: 4px;
  color: var(--text-dark);
}

.howto-section p {
  font-size: 0.85rem;
  line-height: 1.5;
  color: #555;
}

kbd {
  background: var(--board-bg);
  color: #fff;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 700;
  font-family: inherit;
}

.merge-example {
  background: var(--board-bg);
  color: #fff;
  padding: 4px 12px;
  border-radius: 4px;
  font-weight: 700;
  font-size: 0.85rem;
  margin: 3px 0;
  display: inline-block;
  margin-right: 4px;
}

.modal .btn {
  display: block;
  margin: 16px auto 0;
  min-width: 140px;
}

/* ─── CONFETTI CANVAS ─── */
#confetti-canvas {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 200;
}

/* ═══════════════════════════════════════════
   RESPONSIVE — LANDSCAPE DESKTOP
   ═══════════════════════════════════════════ */
@media (min-width: 769px) and (min-height: 500px) {
  body {
    align-items: center;
  }

  #game-container {
    max-width: 900px;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    align-items: flex-start;
    gap: 30px;
    padding: 20px 30px;
  }

  #game-header {
    width: 220px;
    flex-shrink: 0;
  }

  #game-title {
    font-size: 3.2rem;
  }

  .scores {
    flex-direction: column;
    gap: 8px;
  }

  .score-box {
    min-width: 100px;
    padding: 8px 18px;
  }

  .score-value {
    font-size: 1.4rem;
  }

  #game-main {
    width: auto;
  }

  #board-wrapper {
    width: 460px;
  }

  #game-actions {
    margin-top: 8px;
  }

  #mobile-controls {
    display: none !important;
  }
}

/* ═══════════════════════════════════════════
   RESPONSIVE — LARGE DESKTOP / 4K
   ═══════════════════════════════════════════ */
@media (min-width: 1400px) {
  #board-wrapper {
    width: 560px;
  }

  :root {
    --board-gap: 14px;
  }

  .tile-2, .tile-4, .tile-8 { font-size: 2.4rem; }
  .tile-16, .tile-32, .tile-64 { font-size: 2.2rem; }
  .tile-128, .tile-256, .tile-512 { font-size: 1.8rem; }
  .tile-1024, .tile-2048 { font-size: 1.5rem; }
  .tile-super { font-size: 1.2rem; }
}

/* ═══════════════════════════════════════════
   RESPONSIVE — TABLETS
   ═══════════════════════════════════════════ */
@media (max-width: 768px) and (min-width: 501px) {
  #board-wrapper {
    width: 420px;
  }

  :root {
    --board-gap: 10px;
  }

  #game-title {
    font-size: 2.4rem;
  }

  #mobile-controls {
    display: flex;
  }

  .ctrl-btn {
    width: 56px;
    height: 56px;
  }
}

/* ═══════════════════════════════════════════
   RESPONSIVE — MOBILE PORTRAIT
   ═══════════════════════════════════════════ */
@media (max-width: 500px) {
  body {
    align-items: flex-start;
  }

  #game-container {
    max-width: 100%;
    padding: 10px 10px 6px;
    gap: 8px;
  }

  #game-header {
    width: 100%;
  }

  #game-title {
    font-size: 2rem;
  }

  .scores {
    gap: 6px;
  }

  .score-box {
    min-width: 60px;
    padding: 4px 10px;
  }

  .score-label {
    font-size: 0.55rem;
  }

  .score-value {
    font-size: 1rem;
  }

  #board-wrapper {
    width: 100%;
    max-width: 360px;
  }

  :root {
    --board-gap: 8px;
  }

  .tile-2, .tile-4 { font-size: 1.6rem; }
  .tile-8 { font-size: 1.6rem; }
  .tile-16, .tile-32, .tile-64 { font-size: 1.4rem; }
  .tile-128, .tile-256, .tile-512 { font-size: 1.15rem; }
  .tile-1024, .tile-2048 { font-size: 0.95rem; }
  .tile-super { font-size: 0.78rem; }

  #mobile-controls {
    display: flex;
  }

  .ctrl-btn {
    width: 54px;
    height: 54px;
    font-size: 1.2rem;
  }

  .tagline {
    display: none;
  }
}

/* ═══════════════════════════════════════════
   RESPONSIVE — VERY SMALL SCREENS
   ═══════════════════════════════════════════ */
@media (max-width: 360px) {
  :root {
    --board-gap: 6px;
  }

  #game-title {
    font-size: 1.6rem;
  }

  .score-box {
    min-width: 52px;
    padding: 3px 8px;
  }

  .score-value {
    font-size: 0.9rem;
  }

  .tile-2, .tile-4 { font-size: 1.3rem; }
  .tile-8 { font-size: 1.3rem; }
  .tile-16, .tile-32, .tile-64 { font-size: 1.1rem; }
  .tile-128, .tile-256, .tile-512 { font-size: 0.9rem; }
  .tile-1024, .tile-2048 { font-size: 0.75rem; }
  .tile-super { font-size: 0.62rem; }

  .ctrl-btn {
    width: 48px;
    height: 48px;
    font-size: 1.1rem;
  }

  .btn {
    padding: 6px 12px;
    font-size: 0.75rem;
  }

  .btn-sm {
    padding: 4px 8px;
    font-size: 0.65rem;
  }
}

/* ═══════════════════════════════════════════
   LANDSCAPE MOBILE
   ═══════════════════════════════════════════ */
@media (max-height: 500px) and (orientation: landscape) {
  body {
    align-items: center;
  }

  #game-container {
    flex-direction: row;
    flex-wrap: nowrap;
    gap: 16px;
    padding: 8px 16px;
    max-width: 100%;
  }

  #game-header {
    width: 160px;
    flex-shrink: 0;
  }

  #game-title {
    font-size: 1.8rem;
  }

  .scores {
    flex-direction: column;
    gap: 4px;
  }

  .score-box {
    padding: 3px 10px;
  }

  #board-wrapper {
    width: min(55vh, 320px);
  }

  :root {
    --board-gap: 6px;
  }

  .tile-2, .tile-4 { font-size: 1.2rem; }
  .tile-8 { font-size: 1.2rem; }
  .tile-16, .tile-32, .tile-64 { font-size: 1rem; }
  .tile-128, .tile-256, .tile-512 { font-size: 0.85rem; }
  .tile-1024, .tile-2048 { font-size: 0.7rem; }

  #mobile-controls {
    display: none !important;
  }

  #game-actions {
    flex-direction: column;
    gap: 4px;
  }
}
