/* 2048 — Web (AdSense 전용 폴더). 광고 SDK 코드 없음. */

:root {
  --bg: #faf8ef;
  --text: #776e65;
  --board-bg: #bbada0;
  --cell-bg: #cdc1b4;
  --tile-text-light: #f9f6f2;
  --tile-text-dark: #776e65;

  /* 보드 크기: 화면에 맞춰 반응형. 셀/간격은 여기서 파생 */
  --board: min(92vw, 480px);
  --gap: calc(var(--board) * 0.028);
  --cell: calc((var(--board) - var(--gap) * 5) / 4);
  --radius: calc(var(--board) * 0.012);
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Clear Sans", "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

body {
  display: flex;
  justify-content: center;
  padding: 16px;
  /* svh(small viewport height): 모바일 주소창 등으로 흔들리지 않는 안정적 높이 → 불필요한 스크롤 방지 */
  min-height: 100svh;
}

.container {
  width: var(--board);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ---------- 화면 전환 ---------- */
#app { width: var(--board); position: relative; }

.screen { display: none; width: 100%; }
.screen.active { display: block; }
.screen--center.active {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 90svh;
}

/* ---------- 인트로(스플래시) ---------- */
.intro { text-align: center; animation: introIn 0.6s ease; }
@keyframes introIn {
  from { opacity: 0; transform: scale(0.8); }
  to   { opacity: 1; transform: scale(1); }
}
.intro-logo {
  font-size: clamp(72px, 26vw, 130px);
  font-weight: 900;
  color: #edc22e;
  text-shadow: 0 4px 0 #bbada0;
  line-height: 1;
}
.intro-brand {
  margin-top: 12px;
  font-size: clamp(14px, 4vw, 20px);
  font-weight: 700;
  letter-spacing: 0.4em;
  color: #bbada0;
}

/* ---------- 메인 메뉴 ---------- */
.menu { text-align: center; width: 100%; }
.menu-title {
  font-size: clamp(56px, 20vw, 96px);
  font-weight: 900;
  color: var(--text);
  line-height: 1;
}
.menu-tag { margin: 8px 0 28px; font-size: 16px; color: #8f8678; }
.menu-buttons { display: flex; flex-direction: column; gap: 12px; align-items: center; }
.menu-foot { margin-top: 22px; font-size: 14px; color: #8f8678; }

.btn-lg {
  width: 100%;
  max-width: 280px;
  padding: 16px;
  font-size: 18px;
  border-radius: 8px;
}
.btn-outline {
  background: transparent;
  color: #8f7a66;
  box-shadow: inset 0 0 0 2px #8f7a66;
}
.btn-outline:hover { background: rgba(143, 122, 102, 0.1); }

.icon-btn {
  position: absolute;
  top: 0; right: 0;
  background: var(--board-bg);
  color: #fff;
  border: none;
  width: 42px; height: 42px;
  border-radius: 8px;
  font-size: 20px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.header .icon-btn { position: static; width: 40px; height: 40px; font-size: 26px; }

/* ---------- 헤더 ---------- */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.title {
  font-size: clamp(40px, 14vw, 70px);
  font-weight: 800;
  color: var(--text);
  line-height: 1;
}

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

.score-box {
  background: var(--board-bg);
  border-radius: 6px;
  padding: 8px 14px;
  min-width: 70px;
  text-align: center;
  display: flex;
  flex-direction: column;
}

.score-label {
  color: #eee4da;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.05em;
}

.score-value { color: #fff; font-size: 22px; font-weight: 800; }

/* ---------- 서브헤더 ---------- */
.subheader {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.intro { font-size: 15px; }

.btn {
  background: #8f7a66;
  color: #f9f6f2;
  border: none;
  border-radius: 6px;
  padding: 10px 18px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s ease;
}
.btn:hover { background: #7c6a58; }
.btn:active { transform: translateY(1px); }

/* ---------- 광고 자리 ---------- */
.ad-slot {
  min-height: 0; /* 비어 있을 땐 공간 차지 안 함. 광고 넣을 때 조정 */
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ---------- 보드 ---------- */
.board {
  position: relative;
  width: var(--board);
  height: var(--board);
  background: var(--board-bg);
  border-radius: calc(var(--radius) * 3);
  padding: var(--gap);
  touch-action: none; /* 스와이프가 페이지 스크롤로 새지 않게 */
}

.grid-bg {
  position: absolute;
  inset: var(--gap);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--gap);
}

.grid-cell {
  background: var(--cell-bg);
  border-radius: var(--radius);
}

.tiles {
  position: absolute;
  inset: var(--gap);
}

/* ---------- 타일 ---------- */
.tile {
  position: absolute;
  width: var(--cell);
  height: var(--cell);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: calc(var(--cell) * 0.45);
  background: #eee4da;
  color: var(--tile-text-dark);
  /* --x, --y(열/행 인덱스)로 위치 계산 → transform 트랜지션으로 슬라이드 */
  transform: translate(
    calc((var(--cell) + var(--gap)) * var(--x)),
    calc((var(--cell) + var(--gap)) * var(--y))
  );
  transition: transform 0.12s ease-in-out;
  will-change: transform;
}

/* 새로 등장한 타일 */
.tile-new { animation: appear 0.18s ease; }
@keyframes appear {
  0%   { opacity: 0; transform:
           translate(calc((var(--cell) + var(--gap)) * var(--x)),
                     calc((var(--cell) + var(--gap)) * var(--y))) scale(0); }
  100% { opacity: 1; transform:
           translate(calc((var(--cell) + var(--gap)) * var(--x)),
                     calc((var(--cell) + var(--gap)) * var(--y))) scale(1); }
}

/* 병합된 타일: 살짝 커졌다 돌아오는 팝 */
.tile-merged { animation: pop 0.16s ease; z-index: 2; }
@keyframes pop {
  0%   { transform:
           translate(calc((var(--cell) + var(--gap)) * var(--x)),
                     calc((var(--cell) + var(--gap)) * var(--y))) scale(1); }
  50%  { transform:
           translate(calc((var(--cell) + var(--gap)) * var(--x)),
                     calc((var(--cell) + var(--gap)) * var(--y))) scale(1.18); }
  100% { transform:
           translate(calc((var(--cell) + var(--gap)) * var(--x)),
                     calc((var(--cell) + var(--gap)) * var(--y))) scale(1); }
}

/* 숫자별 색상 */
.tile[data-value="2"]    { background: #eee4da; }
.tile[data-value="4"]    { background: #ede0c8; }
.tile[data-value="8"]    { background: #f2b179; color: var(--tile-text-light); }
.tile[data-value="16"]   { background: #f59563; color: var(--tile-text-light); }
.tile[data-value="32"]   { background: #f67c5f; color: var(--tile-text-light); }
.tile[data-value="64"]   { background: #f65e3b; color: var(--tile-text-light); }
.tile[data-value="128"]  { background: #edcf72; color: var(--tile-text-light); font-size: calc(var(--cell) * 0.38); }
.tile[data-value="256"]  { background: #edcc61; color: var(--tile-text-light); font-size: calc(var(--cell) * 0.38); }
.tile[data-value="512"]  { background: #edc850; color: var(--tile-text-light); font-size: calc(var(--cell) * 0.38); }
.tile[data-value="1024"] { background: #edc53f; color: var(--tile-text-light); font-size: calc(var(--cell) * 0.30); }
.tile[data-value="2048"] { background: #edc22e; color: var(--tile-text-light); font-size: calc(var(--cell) * 0.30); }
.tile[data-value="4096"],
.tile[data-value="8192"] { background: #3c3a32; color: var(--tile-text-light); font-size: calc(var(--cell) * 0.28); }

/* ---------- 오버레이 (승리/게임오버) ---------- */
.overlay {
  position: absolute;
  inset: 0;
  border-radius: calc(var(--radius) * 3);
  background: rgba(238, 228, 218, 0.73);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 10;
  animation: fade 0.4s ease;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.overlay-msg { font-size: clamp(28px, 8vw, 44px); font-weight: 800; color: var(--text); }
.overlay-actions { display: flex; gap: 10px; }

.hidden { display: none !important; }

/* ---------- 하단 안내 ---------- */
.hint { font-size: 13px; line-height: 1.5; color: #8f8678; }
.hint strong { color: var(--text); }

/* ---------- 게임 화면 헤더 ---------- */
.title--sm { font-size: clamp(28px, 9vw, 44px); }
.intro-line { font-size: 14px; }

/* ---------- 게임오버 모달 (전체화면 다이얼로그) ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(40, 36, 32, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
  animation: fade 0.25s ease;
}
.modal.hidden { display: none !important; }

.over-card {
  background: #faf8ef;
  border-radius: 16px;
  padding: 26px 22px;
  width: 100%;
  max-width: 360px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.35);
  display: flex;
  flex-direction: column;
  gap: 14px;
  text-align: center;
}
.over-card .overlay-msg { font-size: clamp(30px, 8vw, 40px); font-weight: 800; color: var(--text); }
.overlay-score { font-size: 20px; font-weight: 700; color: #8f7a66; margin-top: -6px; }
.quit-sub { font-size: 15px; font-weight: 600; color: #8f7a66; margin-top: -4px; }
.quit-card { max-width: 320px; }
.quit-card .overlay-msg { font-size: clamp(26px, 7vw, 34px); line-height: 1.3; }

/* ---------- 게임 방법 모달 ---------- */
.howto-card { max-width: 400px; text-align: left; }
.howto-title { text-align: center; font-size: clamp(24px, 7vw, 30px); }
.howto-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin: 2px 0;
  padding: 0;
  max-height: 58svh;
  overflow-y: auto;
}
.howto-list li {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  font-size: 15px;
  line-height: 1.5;
  color: #6b6660;
}
.howto-list b { color: var(--text); }
.howto-ico { font-size: 22px; flex: none; width: 30px; text-align: center; line-height: 1.4; }

.submit-block {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
}
.name-input {
  width: 100%;
  padding: 15px 16px;
  border: 2px solid #bbada0;
  border-radius: 10px;
  font-size: 18px;
  text-align: center;
  color: var(--text);
  background: #fff;
  outline: none;
}
.name-input::placeholder { color: #bdb3a6; }
.name-input:focus { border-color: #8f7a66; box-shadow: 0 0 0 3px rgba(143, 122, 102, 0.15); }

.btn-block { width: 100%; padding: 14px; font-size: 16px; }
.submit-result { font-size: 15px; font-weight: 700; color: #776e65; margin: -4px 0; }

/* 리워드 광고 영역 */
.reward-block {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  border-radius: 12px;
  background: rgba(46, 204, 113, 0.08);
  border: 1px dashed rgba(46, 204, 113, 0.5);
}
.reward-label { font-size: 13px; font-weight: 700; color: #8f8678; }
.reward-sub { font-weight: 600; opacity: 0.85; font-size: 0.85em; }

.btn-reward  { background: #2ecc71; }
.btn-reward:hover  { background: #27ae60; }
.btn-reward2 { background: #3498db; }
.btn-reward2:hover { background: #2c80b4; }
.btn:disabled { opacity: 0.55; cursor: default; }

.over-bottom { display: flex; gap: 10px; margin-top: 6px; }
.over-bottom .btn { flex: 1; }

/* ---------- 랭킹 화면 ---------- */
.ranking-note { font-size: 13px; color: #8f8678; margin-bottom: 6px; }
.ranking-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.ranking-row {
  display: flex;
  align-items: center;
  background: #eee4da;
  border-radius: 6px;
  padding: 12px 14px;
  font-size: 16px;
}
.ranking-row:nth-child(1) { background: #f7e3a1; }
.ranking-row:nth-child(2) { background: #ece0c8; }
.rank-pos { width: 40px; font-weight: 800; color: #8f7a66; }
.rank-name { flex: 1; font-weight: 700; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-score { font-weight: 800; color: #8f7a66; }
.ranking-loading, .ranking-empty {
  list-style: none; text-align: center; color: #8f8678; padding: 20px; font-size: 15px;
}
