/* --- 보드 --- */
.board {
  position: relative;
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  aspect-ratio: 1 / 1;
  background: #bbada0;
  border-radius: 12px;
  padding: 10px;
  box-sizing: border-box;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

/* 빈 셀 그리드 (배경) */
.board .cells {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 10px;
  width: 100%;
  height: 100%;
}

.board .cell {
  background: rgba(238, 228, 218, 0.35);
  border-radius: 8px;
}

/* 타일은 absolute + 퍼센트 좌표 */
.board .tiles {
  position: absolute;
  top: 10px;
  left: 10px;
  right: 10px;
  bottom: 10px;
  pointer-events: none;
}

.tile {
  position: absolute;
  width: calc((100% - 30px) / 4);
  height: calc((100% - 30px) / 4);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  color: #776e65;
  background: #eee4da;
  font-size: clamp(18px, 5vw, 32px);
  box-sizing: border-box;
  will-change: transform, left, top;
  transition:
    left 170ms cubic-bezier(0.22, 0.61, 0.36, 1),
    top 170ms cubic-bezier(0.22, 0.61, 0.36, 1),
    background-color 180ms ease,
    color 180ms ease;
}

/* 새로 스폰된 타일: 작게 나타났다가 살짝 튀며 안정화 */
.tile.tile-new {
  animation: tile-spawn 220ms cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* 합쳐져서 새로 생긴 타일: 기본 크기에서 잠깐 부풀었다 돌아옴 */
.tile.tile-merged {
  animation: tile-pop 220ms cubic-bezier(0.34, 1.56, 0.64, 1);
  z-index: 2;
}

@keyframes tile-spawn {
  0%   { transform: scale(0); opacity: 0; }
  60%  { transform: scale(1.08); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

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

/* 모션 민감 사용자 배려 */
@media (prefers-reduced-motion: reduce) {
  .tile {
    transition: none;
  }
  .tile.tile-new, .tile.tile-merged {
    animation: none;
  }
}

/* 타일 이동: 퍼센트 + gap(10px) 보정용 calc */
.tile[data-x="0"] { left: calc((100% + 10px) * 0 / 4); }
.tile[data-x="1"] { left: calc((100% + 10px) * 1 / 4); }
.tile[data-x="2"] { left: calc((100% + 10px) * 2 / 4); }
.tile[data-x="3"] { left: calc((100% + 10px) * 3 / 4); }
.tile[data-y="0"] { top: calc((100% + 10px) * 0 / 4); }
.tile[data-y="1"] { top: calc((100% + 10px) * 1 / 4); }
.tile[data-y="2"] { top: calc((100% + 10px) * 2 / 4); }
.tile[data-y="3"] { top: calc((100% + 10px) * 3 / 4); }

/* classic 팔레트 기본 (theme.css 없어도 표시되도록) */
.tile[data-val="2"]    { background: #eee4da; color: #776e65; }
.tile[data-val="4"]    { background: #ede0c8; color: #776e65; }
.tile[data-val="8"]    { background: #f2b179; color: #f9f6f2; }
.tile[data-val="16"]   { background: #f59563; color: #f9f6f2; }
.tile[data-val="32"]   { background: #f67c5f; color: #f9f6f2; }
.tile[data-val="64"]   { background: #f65e3b; color: #f9f6f2; }
.tile[data-val="128"]  { background: #edcf72; color: #f9f6f2; font-size: clamp(16px, 4.5vw, 28px); }
.tile[data-val="256"]  { background: #edcc61; color: #f9f6f2; font-size: clamp(16px, 4.5vw, 28px); }
.tile[data-val="512"]  { background: #edc850; color: #f9f6f2; font-size: clamp(16px, 4.5vw, 28px); }
.tile[data-val="1024"] { background: #edc53f; color: #f9f6f2; font-size: clamp(14px, 4vw, 24px); }
.tile[data-val="2048"] { background: #edc22e; color: #f9f6f2; font-size: clamp(14px, 4vw, 24px); }
.tile[data-val="4096"],
.tile[data-val="8192"],
.tile[data-val="16384"],
.tile[data-val="32768"],
.tile[data-val="65536"],
.tile[data-val="131072"] { background: #3c3a32; color: #f9f6f2; font-size: clamp(12px, 3.5vw, 20px); }

/* --- 게임오버/승리 오버레이 --- */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(238, 228, 218, 0.7);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(2px);
  z-index: 10;
}
.overlay.hidden { display: none; }
.overlay-inner { text-align: center; padding: 20px; }
.overlay-title { font-size: 28px; font-weight: 800; color: #776e65; margin-bottom: 8px; }
.overlay-sub { font-size: 14px; color: #776e65; margin-bottom: 16px; }
.overlay-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.overlay-btn {
  padding: 10px 18px;
  border-radius: 8px;
  background: #8f7a66;
  color: #fff;
  font-weight: 600;
  font-size: 14px;
  border: 0;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
}
.overlay-btn.primary { background: #776e65; }
