:root {
  --board-bg: #bbada0;
  --cell-bg: #cdc1b4;
  --cell-empty-bg: rgba(238, 228, 218, 0.35);
  --overlay-bg: rgba(238, 228, 218, 0.85);
  --text-dark: #776e65;
  --text-light: #f9f6f2;
  --tile-text: #776e65;
  --page-bg: #faf8ef;
  --gap: 12px;
  --tile-size: 100px;
}

body.dark-mode {
  --board-bg: #3a3632;
  --cell-bg: #4a4540;
  --cell-empty-bg: rgba(255, 255, 255, 0.06);
  --overlay-bg: rgba(26, 24, 22, 0.9);
  --text-dark: #e8e4de;
  --page-bg: #161412;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  overscroll-behavior: none;
}

body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--page-bg);
  font-family: "Trebuchet MS", "Segoe UI", Verdana, sans-serif;
  color: var(--text-dark);
  transition: background-color 0.3s ease, color 0.3s ease;
}

.wrap {
  width: min(94vw, 480px);
  max-height: 96dvh;
  margin: 0 auto;
  overflow: hidden;
}

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

h1 {
  font-size: 2.4rem;
  margin: 0;
  color: var(--text-dark);
  transition: color 0.3s ease;
}

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

.score-tile {
  background: var(--board-bg);
  border-radius: 6px;
  padding: 8px 14px;
  text-align: center;
  color: var(--text-light);
  min-width: 56px;
  transition: background-color 0.3s ease;
}

.score-tile .label {
  font-size: 0.65rem;
  text-transform: uppercase;
  opacity: 0.8;
}

.score-tile div:last-child:not(.label) { font-size: 1.2rem; font-weight: bold; }

.bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.bar p { margin: 0; font-size: 0.95rem; }

.controls {
  display: flex;
  align-items: center;
  gap: 12px;
}

.hard-mode-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}

.hard-mode-toggle input {
  accent-color: #8f7a66;
  width: 16px;
  height: 16px;
  cursor: pointer;
}

#new-game {
  background: #8f7a66;
  color: var(--text-light);
  border: none;
  border-radius: 6px;
  padding: 10px 16px;
  font-weight: bold;
  cursor: pointer;
}

#new-game:hover { background: #9f8b76; }

.board-container {
  position: relative;
  touch-action: none;
}

.board {
  position: relative;
  background: var(--board-bg);
  border-radius: 8px;
  padding: var(--gap);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--gap);
  aspect-ratio: 1 / 1;
  transition: background-color 0.3s ease;
}

.cell {
  background: var(--cell-empty-bg);
  border-radius: 6px;
  transition: background-color 0.3s ease;
}

.tile {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  font-weight: bold;
  font-size: 2rem;
  background: var(--cell-bg);
  color: var(--tile-text);
  transition: top 0.18s ease-in-out, left 0.18s ease-in-out;
  z-index: 2;
}

.tile.new { animation: pop 0.15s ease; }
.tile.merged { animation: pop 0.15s ease; z-index: 3; }

@keyframes pop {
  0% { transform: scale(0.5); }
  70% { transform: scale(1.08); }
  100% { transform: scale(1); }
}

.tile[data-value="2"] { background: #eee4da; }
.tile[data-value="4"] { background: #ede0c8; }
.tile[data-value="8"] { background: #f2b179; color: var(--text-light); }
.tile[data-value="16"] { background: #f59563; color: var(--text-light); }
.tile[data-value="32"] { background: #f67c5f; color: var(--text-light); }
.tile[data-value="64"] { background: #f65e3b; color: var(--text-light); }
.tile[data-value="128"] { background: #edcf72; color: var(--text-light); font-size: 1.7rem; }
.tile[data-value="256"] { background: #edcc61; color: var(--text-light); font-size: 1.7rem; }
.tile[data-value="512"] { background: #edc850; color: var(--text-light); font-size: 1.7rem; }
.tile[data-value="1024"] { background: #edc53f; color: var(--text-light); font-size: 1.4rem; }

.tile[data-value="2048"] {
  background: linear-gradient(90deg, #ff0040, #ff8c00, #ffef00, #00e050, #00c8ff, #7b2ff7, #ff00c8, #ff0040);
  background-size: 400% 400%;
  animation: rainbow-shift 3s ease infinite;
  color: #fff;
  font-size: 1.4rem;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.45);
  box-shadow: 0 0 14px rgba(255, 255, 255, 0.55);
}

.tile.new[data-value="2048"],
.tile.merged[data-value="2048"] {
  animation: pop 0.15s ease, rainbow-shift 3s ease infinite;
}

@keyframes rainbow-shift {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

.message {
  position: absolute;
  inset: 0;
  background: var(--overlay-bg);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  z-index: 10;
  transition: background-color 0.3s ease;
}

.message.hidden { display: none; }

.message p {
  font-size: 1.6rem;
  font-weight: bold;
  color: var(--text-dark);
  margin: 0;
}

.lose-gif {
  max-width: 70%;
  max-height: 40%;
  border-radius: 8px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.2);
}

.lose-gif.hidden {
  display: none;
}

#try-again {
  background: #8f7a66;
  color: var(--text-light);
  border: none;
  border-radius: 6px;
  padding: 10px 20px;
  font-weight: bold;
  cursor: pointer;
}

.hint {
  text-align: center;
  font-size: 0.85rem;
  opacity: 0.7;
  margin-top: 14px;
}

@media (max-width: 420px) {
  h1 { font-size: 1.8rem; }
  .tile { font-size: 1.4rem; }
  .tile[data-value="128"], .tile[data-value="256"], .tile[data-value="512"] { font-size: 1.2rem; }
  .tile[data-value="1024"], .tile[data-value="2048"] { font-size: 1rem; }
}
