:root {
  --bg: #f5f7fb;
  --surface: #ffffff;
  --text: #182030;
  --muted: #5a6475;
  --board-bg: #dce2ec;
  --grid-cell-bg: rgba(255, 255, 255, 0.38);
  --secondary-border: #c7d1e2;
  --secondary-border-hover: #8ea0bf;
  --secondary-text: #30405f;
  --secondary-bg: #ffffff;
  --score-shadow: rgba(24, 32, 48, 0.08);
  --board-shadow: rgba(24, 32, 48, 0.14);
  --tile-shadow: rgba(0, 0, 0, 0.08);
  --overlay-bg: rgba(24, 32, 48, 0.55);

  --gap: 10px;
  --radius: 18px;
  --cell-size: min(18vw, 88px);
  --tap-size: 44px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1420;
    --surface: #1a2030;
    --text: #e6ebf5;
    --muted: #98a4ba;
    --board-bg: #232b3d;
    --grid-cell-bg: rgba(255, 255, 255, 0.06);
    --secondary-border: #3a4560;
    --secondary-border-hover: #5a6890;
    --secondary-text: #cfd6e6;
    --secondary-bg: #1a2030;
    --score-shadow: rgba(0, 0, 0, 0.3);
    --board-shadow: rgba(0, 0, 0, 0.5);
    --tile-shadow: rgba(0, 0, 0, 0.4);
    --overlay-bg: rgba(0, 0, 0, 0.65);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  overflow-x: hidden;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  font-family: "Inter", "Segoe UI", system-ui, sans-serif;
  background: radial-gradient(circle at 20% 10%, var(--surface), var(--bg));
  color: var(--text);
  display: grid;
  place-items: center;
  padding: 18px;
}

.app {
  width: min(96vw, 480px);
  max-width: 100%;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
}

.topbar > div:first-child {
  min-width: 0;
  flex: 1 1 160px;
}

.actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

h1 {
  margin: 0;
  font-size: clamp(2rem, 8vw, 3rem);
  line-height: 1.1;
}

.subtitle {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.new-game-btn,
.secondary-btn {
  min-height: var(--tap-size);
  min-width: var(--tap-size);
  font-weight: 600;
  border-radius: 12px;
  padding: 11px 14px;
  cursor: pointer;
  transition: transform 140ms ease, filter 140ms ease, border-color 140ms ease, color 140ms ease;
}

.new-game-btn {
  border: 0;
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  color: #fff;
}

.new-game-btn:hover {
  filter: brightness(1.05);
  transform: translateY(-1px);
}

.secondary-btn {
  border: 1px solid var(--secondary-border);
  background: var(--secondary-bg);
  color: var(--secondary-text);
}

.secondary-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  border-color: var(--secondary-border-hover);
}

.secondary-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.scoreboard {
  margin-top: 16px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.score-card {
  background: var(--surface);
  border-radius: 14px;
  padding: 12px;
  box-shadow: 0 8px 18px var(--score-shadow);
}

.score-card span {
  display: block;
  color: var(--muted);
  font-size: 0.85rem;
}

.score-card strong {
  display: block;
  margin-top: 4px;
  font-size: 1.4rem;
}

.board-wrapper {
  position: relative;
  margin-top: 14px;
}

.board {
  position: relative;
  width: calc(var(--cell-size) * 4 + var(--gap) * 5);
  height: calc(var(--cell-size) * 4 + var(--gap) * 5);
  max-width: 100%;
  background: var(--board-bg);
  border-radius: var(--radius);
  margin-inline: auto;
  padding: var(--gap);
  box-shadow: 0 10px 26px var(--board-shadow);
  touch-action: none;
  user-select: none;
}

.grid-cells {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--gap);
  width: 100%;
  height: 100%;
}

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

.tiles {
  position: absolute;
  inset: var(--gap);
}

.tile {
  position: absolute;
  width: var(--cell-size);
  height: var(--cell-size);
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: calc(var(--cell-size) * 0.42);
  transition: transform 160ms ease-in-out;
  color: #182030;
  box-shadow: 0 6px 12px var(--tile-shadow);
  will-change: transform;
}

.tile.v128,
.tile.v256,
.tile.v512 {
  font-size: calc(var(--cell-size) * 0.36);
}

.tile.v1024,
.tile.v2048,
.tile.v4096,
.tile.v8192 {
  font-size: calc(var(--cell-size) * 0.30);
}

.tile.new {
  animation: pop-in 160ms ease;
}

.tile.merge {
  animation: merge-pop 180ms ease;
}

@keyframes pop-in {
  from { transform: scale(0.45); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

@keyframes merge-pop {
  0% { transform: scale(1); }
  45% { transform: scale(1.14); }
  100% { transform: scale(1); }
}

.tile.v2 { background: #e3f2fd; }
.tile.v4 { background: #bbdefb; }
.tile.v8 { background: #90caf9; }
.tile.v16 { background: #80cbc4; }
.tile.v32 { background: #a5d6a7; }
.tile.v64 { background: #ffe082; }
.tile.v128 { background: #ffcc80; }
.tile.v256 { background: #ffab91; }
.tile.v512 { background: #f48fb1; }
.tile.v1024 { background: #ce93d8; color: #fff; }
.tile.v2048 { background: #7c4dff; color: #fff; }
.tile.v4096,
.tile.v8192 { background: #4527a0; color: #fff; }

.status-overlay {
  position: absolute;
  inset: 0;
  background: var(--overlay-bg);
  border-radius: var(--radius);
  display: grid;
  place-items: center;
  text-align: center;
  padding: 16px;
}

.status-overlay p {
  color: #fff;
  font-size: clamp(1.05rem, 4vw, 1.4rem);
  margin: 0 0 14px;
}

.overlay-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}

.hidden {
  display: none;
}

/* Tablet və böyük telefonlar */
@media (min-width: 700px) {
  :root {
    --cell-size: min(14vw, 112px);
    --gap: 12px;
  }

  .app {
    width: min(94vw, 560px);
  }
}

/* Kiçik telefon */
@media (max-width: 420px) {
  :root {
    --cell-size: min(20vw, 74px);
    --gap: 8px;
  }

  .subtitle {
    font-size: 0.85rem;
  }

  .new-game-btn,
  .secondary-btn {
    padding: 9px 12px;
    font-size: 0.9rem;
  }
}

/* Çox kiçik telefon */
@media (max-width: 360px) {
  :root {
    --cell-size: min(19vw, 64px);
    --gap: 6px;
  }

  body {
    padding: 10px;
  }

  .app {
    width: 100%;
  }

  .topbar {
    gap: 10px;
  }

  h1 {
    font-size: 1.8rem;
  }
}

/* Landscape mobil — vertikal yer az olduqda hündürlüyə uyğun ölçü */
@media (orientation: landscape) and (max-height: 500px) {
  :root {
    --cell-size: min(11vh, 64px);
    --gap: 6px;
  }

  body {
    padding: 8px;
    align-items: start;
  }

  h1 {
    font-size: 1.6rem;
  }

  .subtitle {
    display: none;
  }

  .scoreboard {
    margin-top: 10px;
  }

  .board-wrapper {
    margin-top: 10px;
  }

  .score-card {
    padding: 8px 12px;
  }

  .score-card strong {
    font-size: 1.1rem;
  }
}

/* İstifadəçi azaldılmış animasiya istəyirsə */
@media (prefers-reduced-motion: reduce) {
  .tile {
    transition: none;
  }

  .tile.new,
  .tile.merge {
    animation: none;
  }

  .new-game-btn,
  .secondary-btn {
    transition: none;
  }

  .new-game-btn:hover,
  .secondary-btn:hover:not(:disabled) {
    transform: none;
  }
}
