* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Clear Sans", "Helvetica Neue", Arial, sans-serif;
  color: #776e65;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: #faf8ef;
  -webkit-tap-highlight-color: transparent;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: clamp(6px, 1.5vh, 14px);
  touch-action: none;
}

.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: min(100%, 460px);
  height: 100%;
  max-height: 100%;
  gap: clamp(6px, 1.2vh, 12px);
  margin: 0 auto;
}

.head {
  width: 100%;
  flex-shrink: 0;
}

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

.title {
  font-size: clamp(1.8rem, 7vw, 3.2rem);
  font-weight: bold;
  color: #776e65;
  line-height: 1;
}

.scores-container {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.score {
  color: #f9f6f2;
  background-color: #bbada0;
  padding: 6px 12px;
  font-size: clamp(0.85rem, 2.8vw, 1.05rem);
  font-weight: bold;
  border-radius: 4px;
  min-width: 88px;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}

.new-game-btn {
  background-color: #8f7a66;
  color: #f9f6f2;
  border: none;
  padding: 6px 12px;
  font-size: clamp(0.8rem, 2.6vw, 0.95rem);
  font-weight: bold;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.2s ease;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}

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

.header-intro {
  font-size: clamp(0.8rem, 2.5vw, 0.95rem);
  color: #776e65;
}

.header-intro b {
  color: #776e65;
}

.board-wrapper {
  position: relative;
  width: min(100%, 400px, 62vh);
  flex-shrink: 0;
}

/* Responsive square board that fits the card */
.board {
  --gap: clamp(4px, 1.2vw, 8px);
  width: 100%;
  aspect-ratio: 1 / 1;
  background: #bbada0;
  border-radius: 6px;
  padding: var(--gap);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--gap);
  box-sizing: border-box;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.12);
  touch-action: none;
}

.game-message {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(238, 228, 218, 0.73);
  border-radius: 6px;
  z-index: 10;
}

.game-message.hidden {
  display: none;
}

.game-message-inner {
  text-align: center;
  padding: 16px;
}

.game-message-title {
  font-size: clamp(1.4rem, 5vw, 2rem);
  font-weight: bold;
  color: #776e65;
  margin-bottom: 8px;
}

.game-message-text {
  font-size: clamp(0.85rem, 2.5vw, 1rem);
  color: #776e65;
  margin-bottom: 14px;
}

.game-message-buttons {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}

.game-message-btn-secondary {
  background-color: #bbada0;
}

.game-message-btn-secondary:hover,
.game-message-btn-secondary:active {
  background-color: #a8998f;
}

.tile {
  width: 100%;
  height: 100%;
  margin: 0;
  border-radius: 4px;
  font-size: clamp(1.1rem, 6vw, 2.2rem);
  font-weight: bold;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(238, 228, 218, 0.35);
  transition: transform 0.1s ease;
}

/* Tile Colors */
.b2 { background-color: #eee4da; color: #776e65; }
.b4 { background-color: #ede0c8; color: #776e65; }
.b8 { background-color: #f2b179; color: #f9f6f2; }
.b16 { background-color: #f59563; color: #f9f6f2; }
.b32 { background-color: #f67c5f; color: #f9f6f2; }
.b64 { background-color: #f65e3b; color: #f9f6f2; }
.b128 { background-color: #edcf72; color: #f9f6f2; font-size: clamp(0.95rem, 5vw, 1.8rem); }
.b256 { background-color: #edcc61; color: #f9f6f2; font-size: clamp(0.95rem, 5vw, 1.8rem); }
.b512 { background-color: #edc850; color: #f9f6f2; font-size: clamp(0.95rem, 5vw, 1.8rem); }
.b1024 { background-color: #edc53f; color: #f9f6f2; font-size: clamp(0.85rem, 4.2vw, 1.5rem); }
.b2048 { background-color: #edc22e; color: #f9f6f2; font-size: clamp(0.85rem, 4.2vw, 1.5rem); }
.b4096 { background-color: #fe3d3d; color: #f9f6f2; font-size: clamp(0.85rem, 4.2vw, 1.5rem); }
.b8192 { background-color: #ff2020; color: #f9f6f2; font-size: clamp(0.85rem, 4.2vw, 1.5rem); }

.footer {
  width: 100%;
  flex-shrink: 0;
  text-align: center;
}

.footer p {
  font-size: clamp(0.7rem, 2.2vw, 0.9rem);
  line-height: 1.35;
  color: #776e65;
}

/* Touch D-pad — mobile / touch devices only (never on desktop web) */
.touch-controls {
  display: none !important;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.arrow-keys {
  display: flex;
  gap: 6px;
  align-items: center;
  justify-content: center;
}

.key {
  height: 46px;
  width: 46px;
  border: none;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #bbada0;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  transition: background-color 0.15s ease, transform 0.08s ease;
  color: #fff;
  padding: 0;
}

.key:hover,
.key:active,
.key.active {
  background-color: #8f7a66;
  transform: scale(0.94);
}

.key svg {
  width: 22px;
  height: 22px;
  stroke: white;
  pointer-events: none;
}

/* Real phones / tablets only */
@media (hover: none) and (pointer: coarse) {
  .touch-controls {
    display: flex !important;
  }

  .footer {
    display: none;
  }
}

@media (max-height: 560px) {
  .header-intro {
    display: none;
  }

  .title {
    font-size: clamp(1.5rem, 6vw, 2.2rem);
  }

  .board-wrapper {
    width: min(100%, 340px, 52vh);
  }

  .key {
    width: 40px;
    height: 40px;
  }
}
