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

body {
  margin: 0;
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  color: #e5e5e5;
  background: #121212;
  min-height: 100vh;
  padding: 0.5rem;
  -webkit-tap-highlight-color: transparent;
}

.header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem 1rem;
  margin-bottom: 1rem;
}

.back-link {
  font-size: 0.95rem;
  color: #a0a0a0;
  text-decoration: none;
}

.back-link:hover,
.back-link:focus {
  color: #f5f5f5;
  text-decoration: underline;
}

.header h1 {
  margin: 0;
  font-size: 1.75rem;
  color: #f5f5f5;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-left: auto;
}

.score-box {
  padding: 0.35rem 0.75rem;
  background: #252525;
  border-radius: 8px;
  border: 1px solid #333;
}

.score-label {
  display: block;
  font-size: 0.7rem;
  color: #a0a0a0;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.score-value {
  font-size: 1.25rem;
  font-weight: 700;
  color: #f5f5f5;
}

#new-game,
#open-stats {
  min-height: 44px;
  min-width: 44px;
  padding: 0.5rem 0.75rem;
  font-size: 0.95rem;
  font-weight: 500;
  border: 1px solid #505050;
  border-radius: 8px;
  background: #252525;
  color: #e5e5e5;
  cursor: pointer;
}

#new-game:hover,
#new-game:focus,
#open-stats:hover,
#open-stats:focus {
  background: #333;
  border-color: #666;
}

.main {
  max-width: 420px;
  margin: 0 auto;
  position: relative;
}

.game-over {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.85);
  border-radius: 12px;
  z-index: 10;
  padding: 1.5rem;
}

#game-over[hidden] {
  display: none !important;
}

.game-over p {
  margin: 0 0 0.5rem;
  font-size: 1.5rem;
  font-weight: 700;
  color: #f5f5f5;
}

.game-over-score {
  margin-bottom: 1rem !important;
  font-size: 1.1rem !important;
  font-weight: 500 !important;
  color: #a0a0a0 !important;
}

#try-again {
  min-height: 44px;
  padding: 0.5rem 1.25rem;
  font-size: 1rem;
  font-weight: 600;
  border: 1px solid #505050;
  border-radius: 8px;
  background: #404040;
  color: #f5f5f5;
  cursor: pointer;
}

#try-again:hover,
#try-again:focus {
  background: #505050;
}

.grid-container {
  position: relative;
  width: min(92vmin, 380px);
  margin: 0 auto 1rem;
  aspect-ratio: 1;
  border-radius: 12px;
  overflow: hidden;
  background: #252525;
  border: 2px solid #333;
  touch-action: none;
}

.grid-background {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 8px;
  padding: 8px;
}

.cell-bg {
  background: #1a1a1a;
  border-radius: 6px;
}

.tiles {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 8px;
  padding: 8px;
  pointer-events: none;
}

.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(18px, 5vmin, 32px);
  font-weight: 700;
  border-radius: 6px;
  pointer-events: none;
  transition: transform 0.15s ease-out;
}

.tile.tile-sliding {
  transition-duration: 0.22s;
  transition-timing-function: cubic-bezier(0.22, 0.61, 0.36, 1);
  will-change: transform;
}

.tile[hidden] {
  display: none;
}

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

.tile.tile-merge {
  animation: tileMerge 0.28s ease-out;
}

@keyframes tileSpawn {
  from {
    transform: scale(0);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes tileMerge {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.12);
  }
  100% {
    transform: scale(1);
  }
}

.tile-2 { background: #3d3d2e; color: #e5e5e5; }
.tile-4 { background: #4a4a35; color: #e5e5e5; }
.tile-8 { background: #6b6b44; color: #f5f5f5; }
.tile-16 { background: #8b8b2e; color: #121212; }
.tile-32 { background: #a0a028; color: #121212; }
.tile-64 { background: #b8b820; color: #121212; }
.tile-128 { background: #c9c918; color: #121212; }
.tile-256 { background: #d4d40e; color: #121212; }
.tile-512 { background: #e0e008; color: #121212; }
.tile-1024 { background: #e8e800; color: #121212; font-size: clamp(14px, 4vmin, 26px); }
.tile-2048 { background: #f0f000; color: #121212; font-size: clamp(14px, 4vmin, 26px); }
.tile-4096,
.tile-8192,
.tile-16384 { background: #ffff00; color: #121212; font-size: clamp(12px, 3vmin, 22px); }

.hint {
  margin: 0;
  font-size: 0.85rem;
  color: #707070;
  text-align: center;
}

/* Stats modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal[hidden] {
  display: none;
}

.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  cursor: pointer;
}

.modal-content {
  position: relative;
  width: 100%;
  max-width: 400px;
  max-height: 85vh;
  overflow: auto;
  background: #1e1e1e;
  border: 1px solid #333;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  padding: 1.25rem;
  color: #e5e5e5;
}

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

.modal-header h2 {
  margin: 0;
  font-size: 1.25rem;
  color: #f5f5f5;
}

.modal-close {
  width: 36px;
  height: 36px;
  padding: 0;
  font-size: 1.5rem;
  line-height: 1;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: #a0a0a0;
  cursor: pointer;
}

.modal-close:hover,
.modal-close:focus {
  background: #333;
  color: #f5f5f5;
}

.total-games {
  margin: 0 0 1rem;
  font-weight: 600;
  color: #e5e5e5;
}

.history-heading {
  margin: 0 0 0.5rem;
  font-size: 1rem;
  color: #a0a0a0;
}

.history-list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.history-list li {
  padding: 0.35rem 0;
  border-bottom: 1px solid #333;
  font-size: 0.9rem;
  color: #a0a0a0;
}

.history-list li:last-child {
  border-bottom: none;
}

.clear-history-btn {
  display: block;
  margin-top: 1.25rem;
  padding: 0.5rem 0.75rem;
  font-size: 0.9rem;
  border: 1px solid #dc2626;
  border-radius: 6px;
  background: transparent;
  color: #f87171;
  cursor: pointer;
}

.clear-history-btn:hover,
.clear-history-btn:focus {
  background: #451a1a;
}
