
* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  background: #f5efe6;
  font-family: Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

button {
  padding: 0;
  border: none;
  background: none;
  cursor: pointer;
  touch-action: manipulation;
}

.game-container {
  width: min(94vw, 480px);
  margin: 0 auto;
  padding: 12px 0 24px;
}

/* 배경 이미지 */


/* 배경 이미지 및 게임판 */
.board {
  position: relative;

  /* 세로형 원본 이미지의 비율 유지 */
  width: min(100vw, 56.25dvh);
  height: auto;
  aspect-ratio: 1152 / 2048;

  margin: 0 auto;
  background-image: url("./images/2048게임_게임 배경 + 칸.jpg");
  background-size: 100% 100%;
  background-position: center;
  background-repeat: no-repeat;

  overflow: hidden;
  isolation: isolate;
  flex: 0 0 auto;
}

.tiles {
  position: absolute;
  left: 14%;
  top: 200%;
  width: 72%;
  height: 40%;

  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(0, 1fr));

  transform: translateY(0);
  z-index: 2;
}

.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
}

.tile-inner {
  width: 88%;
  height: 88%;
  display: flex;
  align-items: center;
  justify-content: center;

  border-radius: 8px;
  background: #eee4da;
  color: #66584a;

  font-size: clamp(18px, 6vw, 34px);
  font-weight: 800;
  overflow: hidden;
}

.tile[data-value="2"] .tile-inner {
  background: #eee4da;
}

.tile[data-value="4"] .tile-inner {
  background: #ede0c8;
}

.tile[data-value="8"] .tile-inner {
  background: #f2b179;
  color: white;
}

.tile[data-value="16"] .tile-inner {
  background: #f59563;
  color: white;
}

.tile[data-value="32"] .tile-inner {
  background: #f67c5f;
  color: white;
}

.tile[data-value="64"] .tile-inner {
  background: #f65e3b;
  color: white;
}

.tile[data-value="128"] .tile-inner,
.tile[data-value="256"] .tile-inner,
.tile[data-value="512"] .tile-inner {
  background: #edcf72;
  color: white;
  font-size: clamp(16px, 5vw, 29px);
}

.tile[data-value="1024"] .tile-inner,
.tile[data-value="2048"] .tile-inner {
  background: #edc53f;
  color: white;
  font-size: clamp(13px, 4.5vw, 25px);
}

/* 되돌리기 / 재시도 버튼 */
.game-buttons {
  position: absolute;
  left: 170px;
  right: 0;
  top: 32%;
  z-index: 20;

  display: flex;
  justify-content: center;
  align-items: center;
  gap: 2px;

  margin: 0;
}

.game-buttons button {
  width: 40px;
  aspect-ratio: 1 / 1;
  flex: 0 0 auto;
  overflow: hidden;
  border-radius: 8px;
}

.game-buttons img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.game-buttons button:active {
  transform: scale(0.94);
}

/* 게임 오버 */
/* 게임오버 전체 영역 */
.message {
  position: absolute;
  inset: 0;
  z-index: 30;
  background: transparent;
}

/* 게임오버 이미지: 타일 영역과 같은 위치 */
.gameover-image {
  position: absolute;
  left: 14%;
  top: 39%;
  width: 72%;
  height: 40%;
  object-fit: contain;
  pointer-events: none;
}

/* 점수와 홈 버튼: 기존 버튼 영역 */
.gameover-results {
  position: absolute;
  left: 0;
  right: 0;
  top: 30%;

  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
}

/* 최고 점수와 현재 점수 */
.result-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 48px;

  color: #ffffff;
  font-size: clamp(14px, 4vw, 22px);
  font-weight: bold;
  text-shadow: 0 1px 3px #000000;
}

.result-item img {
  display: block;
  width: clamp(35px, 10vw, 55px);
  height: clamp(35px, 10vw, 55px);
  object-fit: contain;
}

#home-btn {
  width: clamp(48px, 13vw, 65px);
  aspect-ratio: 1 / 1;
  flex: 0 0 auto;
}

#home-btn img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

#home-btn:active {
  transform: scale(0.94);
}

.hidden {
  display: none !important;
}

@keyframes appear {
  from {
    opacity: 0.5;
    transform: scale(0.85);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}
/* 모든 숫자 블록을 정사각형으로 유지 */
.tile-inner {
  width: 88%;
  height: auto;
  aspect-ratio: 1 / 1;
  flex-shrink: 0;
}
.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
}

.tile-inner {
  width: 88%;
  height: 88%;
  aspect-ratio: 1 / 1;
  overflow: hidden;
}

.tile-inner img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}
/* 타일 레이어를 배경 위에 표시 */
.tiles {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(0, 1fr));
  z-index: 2;
}

.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
}

.tile-inner {
  width: 88%;
  height: 88%;
  aspect-ratio: 1 / 1;
  overflow: hidden;
}

.tile-inner img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}
/* 타일 위치 */
.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
}

/* 정사각형 타일 이미지 */
.tile-inner {
  position: relative;
  width: 88%;
  height: auto;
  aspect-ratio: 1 / 1;
  flex-shrink: 0;
  overflow: hidden;
  border-radius: 6px;
}

/* 타일 이미지 */
.tile-inner img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* 이미지 위쪽 작은 숫자 */
.tile-inner::after {
  content: attr(data-value);
  position: absolute;
  top: 3px;
  left: 4px;
  z-index: 2;

  font-size: clamp(9px, 1.2vw, 14px);
  font-weight: bold;
  line-height: 1;
  color: #ffffff;
  text-shadow: 0 1px 3px #000000;
  pointer-events: none;
}
#board {
  position: relative !important;
}

#board #tiles {
  position: absolute !important;
  left: 14% !important;
  top: 39% !important;
  width: 72% !important;
  height: 40% !important;
  z-index: 10;
}
/* 타일 이동 애니메이션 */

.tile {
  will-change: transform;
}


@keyframes tileAppear {
  from {
    opacity: 0.4;
    transform: scale(0.7);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}
/* 타일 이동 애니메이션 */
#tiles .tile {
  transition: transform 160ms ease-in-out, opacity 160ms ease;
  will-change: transform;
}

/* 새 타일 등장 애니메이션 */
#tiles .tile.tile-new {
  animation: tileSpawn 160ms ease-out;
}

@keyframes tileSpawn {
  from {
    opacity: 0;
    scale: 0.5;
  }
  to {
    opacity: 1;
    scale: 1;
  }
}
#tiles .tile {
  transition: none;
  will-change: transform;
}

#tiles .tile.tile-new {
  animation: tileSpawn 160ms ease-out;
}

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

  to {
    opacity: 1;
    transform: scale(1);
  }
}

/* 게임오버 화면에서 기존 되돌리기·재시도 버튼 숨기기 */
#board:has(#message:not(.hidden)) .game-buttons {
  display: none;
}

/* ===== 게임오버 화면 요소 위치 조정 ===== */

/* BEST와 SCORE 영역 전체 */
#board .gameover-results {
  position: absolute;
  top: 32%;
  left: 35%;
  width: 48%;
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  gap: 10px;
  margin: 0;
  z-index: 35;
}

/* BEST / SCORE 각각의 영역 */
#board .result-item {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 5px;
  min-width: 0;
  flex: 1;
  margin: 0;
}

/* BEST / SCORE 아이콘 이미지 */
#board .result-item img {
  display: block;
  width: 100%;
  height: auto;
  object-fit: contain;
}

/* 점수 숫자 */
#board .result-item span {
  display: block;
  margin: 0;
  font-size: clamp(14px, 4vw, 22px);
  line-height: 1.2;
  text-align: center;
  transform: translateY(-30px);
}

/* 홈 버튼 */
#board #home-btn {
  position: absolute !important;
  left: -70px !important;
  top: 0px !important;
  width: 40px;
  height: 40px;
  padding: 0;
  margin: 0;
  border: none;
  background: transparent;
  transform: none !important;
  z-index: 40;
}

/* 홈 버튼 안의 이미지 */
#board #home-btn img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}
/* 모바일 화면 고정 */
html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

/* 게임 화면을 화면 중앙에 고정 */
.game-container {
  width: 100%;
  height: 100%;
  height: 100dvh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  touch-action: none;
}

/* 게임판 크기 유지 */
.board {
  flex: 0 0 auto;
  touch-action: none;
}