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

body {
  font-family: "Clear Sans", "Helvetica Neue", Arial, sans-serif;
  background: #faf8ef;
  color: #776e65;
  margin: 0;
  padding: 0;
}

.container {
  width: 500px;
  margin: 0 auto;
  padding: 20px 0 40px;
}

/* ── Header ── */
.header {
  margin-bottom: 16px;
}

.title-area {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.title {
  font-size: 80px;
  font-weight: 700;
  color: #776e65;
  line-height: 1;
}

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

.score-container {
  background: #bbada0;
  border-radius: 3px;
  padding: 10px 20px;
  text-align: center;
  min-width: 80px;
}

.score-title {
  font-size: 13px;
  font-weight: 700;
  color: #eee4da;
  letter-spacing: 1px;
}

.score-value {
  font-size: 25px;
  font-weight: 700;
  color: #fff;
  line-height: 1.2;
}

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

.subtitle p {
  font-size: 18px;
  color: #776e65;
}

.btn-group {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.new-game-btn {
  background: #8f7a66;
  border: none;
  border-radius: 3px;
  color: #f9f6f2;
  font-size: 18px;
  font-weight: 700;
  font-family: inherit;
  padding: 10px 20px;
  cursor: pointer;
  white-space: nowrap;
}

.new-game-btn:hover {
  background: #9f8b77;
}

/* ── Undo button (header) ── */
.undo-btn {
  background: #7a9a6e;
  border: none;
  border-radius: 3px;
  color: #f9f6f2;
  font-size: 18px;
  font-weight: 700;
  font-family: inherit;
  padding: 10px 20px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s;
}

.undo-btn:hover:not(:disabled) {
  background: #8ab87d;
}

.undo-btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.help-toggle-btn {
  background: #a08060;
  border: none;
  border-radius: 3px;
  color: #f9f6f2;
  font-size: 18px;
  font-weight: 700;
  font-family: inherit;
  padding: 10px 20px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s;
}

.help-toggle-btn:hover:not(:disabled) {
  background: #b09070;
}

.help-toggle-btn[aria-expanded="true"] {
  background: #6d5a4a;
}

.help-toggle-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

/* ── Help dropdown panel ── */
.help-panel {
  overflow: hidden;
  max-height: 0;
  transition: max-height 0.22s ease, padding 0.22s ease;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  background: #f0e8e0;
  border-radius: 3px;
  margin-bottom: 0;
}

.help-panel.open {
  max-height: 120px;
  padding: 10px 12px;
  margin-bottom: 10px;
}

.help-action-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  background: #8f7a66;
  border: none;
  border-radius: 3px;
  color: #f9f6f2;
  font-size: 15px;
  font-weight: 700;
  font-family: inherit;
  padding: 8px 14px;
  cursor: pointer;
  transition: background 0.15s;
  white-space: nowrap;
}

.help-action-btn:hover {
  background: #a08070;
}

.help-action-icon {
  font-size: 17px;
  line-height: 1;
}

/* ── Instruction bar ── */
.help-instruction {
  display: none;
  align-items: center;
  justify-content: space-between;
  background: #f9f0dc;
  border: 2px solid #edcf72;
  border-radius: 3px;
  padding: 9px 14px;
  margin-bottom: 10px;
  font-size: 15px;
  font-weight: 700;
  color: #776e65;
}

.help-instruction.active {
  display: flex;
}

.help-cancel-btn {
  background: transparent;
  border: 2px solid #bbada0;
  border-radius: 3px;
  color: #776e65;
  font-size: 13px;
  font-weight: 700;
  font-family: inherit;
  padding: 4px 10px;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
  margin-left: 12px;
}

.help-cancel-btn:hover {
  background: #e8dfd5;
}

/* ── Hint overlay ── */
.hint-overlay {
  position: absolute;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  pointer-events: none;
  opacity: 0;
}

.hint-overlay.active {
  animation: hint-fade 2.2s ease forwards;
}

@keyframes hint-fade {
  0%   { opacity: 0; }
  12%  { opacity: 1; }
  70%  { opacity: 1; }
  100% { opacity: 0; }
}

.hint-arrow {
  font-size: 120px;
  line-height: 1;
  color: #fff;
  text-shadow:
    0 4px 24px rgba(0, 0, 0, 0.55),
    0 0  40px rgba(237, 194, 46, 0.7);
  animation: none;
}

.hint-overlay.active .hint-arrow {
  animation: hint-bounce 2.2s ease forwards;
}

@keyframes hint-bounce {
  0%   { transform: scale(0.4); }
  18%  { transform: scale(1.15); }
  28%  { transform: scale(1); }
  70%  { transform: scale(1); }
  100% { transform: scale(0.85); }
}

/* ── Game Container ── */
.game-container {
  position: relative;
  background: #bbada0;
  border-radius: 6px;
  padding: 15px;
  margin-bottom: 20px;
  touch-action: none;
  user-select: none;
}

/* ── Grid ── */
.grid-container {
  position: relative;
  z-index: 1;
}

.grid-row {
  display: flex;
  gap: 15px;
  margin-bottom: 15px;
}

.grid-row:last-child {
  margin-bottom: 0;
}

.grid-cell {
  width: 106.25px;
  height: 106.25px;
  background: rgba(238, 228, 218, 0.35);
  border-radius: 3px;
  flex-shrink: 0;
}

/* ── Tile Container ── */
.tile-container {
  position: absolute;
  top: 15px;
  left: 15px;
  right: 15px;
  bottom: 15px;
  z-index: 2;
}

/* ── Tiles ── */
.tile {
  position: absolute;
  width: 106.25px;
  height: 106.25px;
  border-radius: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 55px;
  font-weight: 700;
  transition: transform 0.1s ease, top 0.1s ease, left 0.1s ease;
}

.tile.tile-new {
  animation: appear 0.2s ease forwards;
}

.tile.tile-merged {
  animation: pop 0.2s ease forwards;
  z-index: 20;
}

@keyframes appear {
  0%   { transform: scale(0); }
  100% { transform: scale(1); }
}

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

/* Tile colours */
.tile-2   { background: #eee4da; color: #776e65; }
.tile-4   { background: #ede0c8; color: #776e65; }
.tile-8   { background: #f2b179; color: #f9f6f2; }
.tile-16  { background: #f59563; color: #f9f6f2; }
.tile-32  { background: #f67c5f; color: #f9f6f2; }
.tile-64  { background: #f65e3b; color: #f9f6f2; }
.tile-128 { background: #edcf72; color: #f9f6f2; font-size: 45px; }
.tile-256 { background: #edcc61; color: #f9f6f2; font-size: 45px; }
.tile-512 { background: #edc850; color: #f9f6f2; font-size: 45px; }
.tile-1024 { background: #edc53f; color: #f9f6f2; font-size: 35px; }
.tile-2048 { background: #edc22e; color: #f9f6f2; font-size: 35px; box-shadow: 0 0 30px 10px rgba(243,215,116,.24), inset 0 0 0 1px rgba(255,255,255,.14); }
.tile-super { background: #3c3a32; color: #f9f6f2; font-size: 30px; }

/* ── Game message overlay ── */
.game-message {
  display: none;
  position: absolute;
  inset: 0;
  background: rgba(238, 228, 218, 0.73);
  border-radius: 6px;
  z-index: 100;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.game-message.active {
  display: flex;
}

.game-message p {
  font-size: 60px;
  font-weight: 700;
  color: #776e65;
  margin-bottom: 30px;
}

.game-message.game-over p {
  color: #776e65;
}

.game-message.game-won p {
  color: #f9f6f2;
}

.game-message.game-won {
  background: rgba(237, 194, 46, 0.5);
}

.lower {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: center;
}

.retry-button {
  background: #8f7a66;
  border: none;
  border-radius: 3px;
  color: #f9f6f2;
  font-size: 18px;
  font-weight: 700;
  font-family: inherit;
  padding: 10px 20px;
  cursor: pointer;
}

.retry-button:hover {
  background: #9f8b77;
}

/* ── Undo button inside game-over overlay ── */
.undo-overlay-btn {
  background: #f59563;
  border: none;
  border-radius: 3px;
  color: #f9f6f2;
  font-size: 18px;
  font-weight: 700;
  font-family: inherit;
  padding: 10px 20px;
  cursor: pointer;
  transition: background 0.15s;
}

.undo-overlay-btn:hover:not(:disabled) {
  background: #f67c5f;
}

.undo-overlay-btn:disabled {
  opacity: 0.45;
  cursor: default;
}

/* ── Tile selection (swap / remove / change modes) ── */
.game-container.select-mode .tile {
  cursor: pointer;
  transition: transform 0.1s ease, top 0.1s ease, left 0.1s ease, opacity 0.1s ease;
}

.game-container.select-mode .tile:hover {
  opacity: 0.8;
  transform: scale(0.93);
}

.tile.tile-selected {
  outline: 3px solid #fff;
  outline-offset: 2px;
  transform: scale(1.06) !important;
  z-index: 25;
}

/* ── Value picker overlay ── */
.value-picker {
  display: none;
  position: absolute;
  inset: 0;
  z-index: 70;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: rgba(187, 173, 160, 0.88);
  border-radius: 6px;
}

.value-picker.active {
  display: flex;
}

.vp-title {
  font-size: 20px;
  font-weight: 700;
  color: #f9f6f2;
  text-shadow: 0 1px 4px rgba(0,0,0,0.3);
}

.vp-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  padding: 0 12px;
  width: 100%;
  box-sizing: border-box;
}

.vp-btn {
  border: none;
  border-radius: 3px;
  font-family: inherit;
  font-weight: 700;
  font-size: 18px;
  padding: 12px 0;
  cursor: pointer;
  transition: transform 0.1s ease, opacity 0.1s ease;
}

.vp-btn:hover {
  transform: scale(1.08);
  opacity: 0.9;
}

.vp-cancel-btn {
  background: transparent;
  border: 2px solid #f9f6f2;
  border-radius: 3px;
  color: #f9f6f2;
  font-size: 14px;
  font-weight: 700;
  font-family: inherit;
  padding: 6px 18px;
  cursor: pointer;
}

.vp-cancel-btn:hover {
  background: rgba(255,255,255,0.15);
}

/* ── Footer ── */
.game-explanation {
  font-size: 16px;
  color: #776e65;
  margin-bottom: 10px;
  line-height: 1.5;
}

.important {
  color: #f65e3b;
}

/* ── Responsive / Mobile ── */
@media (max-width: 520px) {
  .container {
    width: 100%;
    padding: 15px;
  }

  .title {
    font-size: 52px;
  }

  .score-container {
    padding: 8px 12px;
    min-width: 60px;
  }

  .score-title {
    font-size: 11px;
  }

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

  .subtitle p {
    font-size: 14px;
  }

  .new-game-btn,
  .help-toggle-btn,
  .undo-btn {
    font-size: 14px;
    padding: 8px 10px;
  }

  .undo-overlay-btn,
  .retry-button {
    font-size: 16px;
    padding: 9px 16px;
  }

  .help-action-btn {
    font-size: 13px;
    padding: 7px 10px;
  }

  .hint-arrow {
    font-size: 80px;
  }

  .vp-btn {
    font-size: 14px;
    padding: 10px 0;
  }

  .vp-grid {
    gap: 6px;
  }

  .game-container {
    padding: 10px;
  }

  .grid-row {
    gap: 10px;
    margin-bottom: 10px;
  }

  .grid-cell {
    /* fill available width: 4 cells + 3 gaps of 10px, 2×10px padding */
    width: calc((100vw - 30px - 30px - 30px) / 4);
    height: calc((100vw - 30px - 30px - 30px) / 4);
  }

  .tile {
    width: calc((100vw - 30px - 30px - 30px) / 4);
    height: calc((100vw - 30px - 30px - 30px) / 4);
    font-size: 32px;
  }

  .tile-128, .tile-256, .tile-512 { font-size: 26px; }
  .tile-1024, .tile-2048           { font-size: 20px; }
  .tile-super                       { font-size: 16px; }

  .tile-container {
    top: 10px;
    left: 10px;
    right: 10px;
    bottom: 10px;
  }

  .game-message p {
    font-size: 36px;
  }
}
