:root {
  --bg: #faf8ef;
  --ink: #776e65;
  --ink-strong: #4a4038;
  --panel: #ffffff;
  --border: #e3ddd0;
  --accent: #8f7a66;
  --accent-strong: #6d5c4d;
  --tile-empty: #cdc1b4;
  --good: #6a9c78;
  --danger: #b6564b;
  --gold: #b8860b;
}

:root[data-theme="dark"] {
  --bg: #232220;
  --ink: #b8afa2;
  --ink-strong: #ece3d6;
  --panel: #33302b;
  --border: #4a463f;
  --accent: #a08a72;
  --accent-strong: #b89e84;
  --tile-empty: #4a453d;
  --good: #7fb08c;
  --danger: #d0736a;
  --gold: #d6a520;
}
:root[data-theme="dark"] .board { background: #4a443b; }
:root[data-theme="dark"] .cell { background: rgba(255, 255, 255, 0.05); }
:root[data-theme="dark"] .ad-banner { background: #2c2a26; }
:root[data-theme="dark"] .achv-item.done { background: rgba(255, 255, 255, 0.06); }

/* 접근성: 모션 최소화(OS 설정 또는 게임 설정) */
@media (prefers-reduced-motion: reduce) {
  .tile.spawn, .tile.merge, .board.danger, .combo-badge.show { animation: none !important; }
}
.no-motion .tile.spawn, .no-motion .tile.merge,
.no-motion .board.danger, .no-motion .combo-badge.show { animation: none !important; }

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Malgun Gothic", sans-serif;
  min-height: 100vh;
  overscroll-behavior: none;   /* 당겨서 새로고침/바운스 방지 */
}

/* ---------- 광고 배너 ---------- */
.ad-banner {
  width: 100%;
  min-height: 64px;
  background: #eee7da;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-size: 13px;
  color: #9a8f80;
  padding: 8px;
}
:root[data-theme="dark"] .ad-banner { background: #2c2a26; }
.ad-bottom { border-bottom: none; border-top: 1px solid var(--border); }

/* ---------- 레이아웃 ---------- */
.app {
  max-width: 420px;
  margin: 0 auto;
  padding: 16px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.topbar h1 {
  font-size: 22px;
  margin: 0;
  color: var(--ink-strong);
}

.stats {
  display: flex;
  gap: 6px;
}

.stat-box {
  background: var(--accent);
  color: #fff;
  border-radius: 6px;
  padding: 4px 10px;
  text-align: center;
  min-width: 54px;
}

.coin-box { background: var(--gold); }

.stat-label {
  display: block;
  font-size: 9px;
  opacity: 0.85;
  letter-spacing: 0.5px;
}

.stat-value {
  display: block;
  font-size: 15px;
  font-weight: 700;
}

/* ---------- 모드 바 ---------- */
.mode-bar {
  display: flex;
  gap: 6px;
  margin-bottom: 10px;
}

.mode-btn {
  flex: 1;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--ink);
  padding: 8px 6px;
  min-height: 42px;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
}
.mode-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
.streak-btn { flex: 0 0 auto; }

/* ---------- 툴바 ---------- */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 12px;
}

.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--ink-strong);
  padding: 7px 10px;
  min-height: 42px;          /* 접근성: 터치 영역 확보 */
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
}

.btn:hover { background: #f3efe6; }
.btn:active { transform: translateY(1px); }

.btn-item em { font-style: normal; color: var(--gold); font-size: 11px; margin-left: 3px; }

/* 아이콘 버튼 */
.icon-btn { font-size: 19px; min-width: 46px; padding: 6px 9px; }
.icon-btn em { font-size: 10px; }

/* 꾹 눌러 보는 설명 툴팁 */
.hold-tip {
  position: fixed;
  transform: translate(-50%, -100%);
  background: rgba(50, 48, 42, 0.96);
  color: #fff;
  font-size: 12px;
  padding: 6px 10px;
  border-radius: 6px;
  white-space: nowrap;
  pointer-events: none;
  z-index: 200;
}

.btn-shop {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
.btn-shop:hover { background: var(--accent-strong); }

.btn-ad {
  background: #4a7c8c;
  color: #fff;
  border-color: #4a7c8c;
}
.btn-ad:hover { background: #3d6a78; }

.btn-share {
  background: #6a9c78;
  color: #fff;
  border-color: #6a9c78;
}
.btn-share:hover { background: #5a8868; }

.btn-danger {
  background: var(--danger);
  color: #fff;
  border-color: var(--danger);
}

.btn[disabled] { opacity: 0.45; cursor: not-allowed; }

/* ---------- 보드 ---------- */
.board-wrap { position: relative; }

/* 보드 위 스와이프는 게임 조작 전용 — 브라우저 스크롤/줌 차단 */
.board-wrap, .board {
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.board {
  background: #bbada0;
  border-radius: 8px;
  padding: 8px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 8px;
  aspect-ratio: 1 / 1;
  width: 100%;
  position: relative;
}

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

/* 위기 연출: 빈칸이 거의 없을 때 보드 경고 */
@keyframes boardDanger {
  0%, 100% { box-shadow: 0 0 0 0 rgba(182, 86, 75, 0); }
  50% { box-shadow: 0 0 0 4px rgba(182, 86, 75, 0.65); }
}
.board.danger { animation: boardDanger 900ms ease-in-out infinite; }

/* 콤보 배지 */
.combo-badge {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(60, 58, 50, 0.92);
  color: #ffd24c;
  font-weight: 700;
  font-size: 15px;
  padding: 6px 14px;
  border-radius: 20px;
  pointer-events: none;
  opacity: 0;
  z-index: 5;
}
@keyframes comboPop {
  0% { opacity: 0; transform: translateX(-50%) scale(0.6); }
  20% { opacity: 1; transform: translateX(-50%) scale(1.12); }
  70% { opacity: 1; transform: translateX(-50%) scale(1); }
  100% { opacity: 0; transform: translateX(-50%) scale(1); }
}
.combo-badge.show { animation: comboPop 900ms ease-out; }

.tile-layer {
  position: absolute;
  inset: 8px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 8px;
  pointer-events: none;
}

.tile {
  position: relative;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 22px;
  color: var(--ink-strong);
  background: var(--tile-empty);
  transition: transform 120ms ease;
}

/* 타일 등장/병합 애니메이션 (손맛) */
@keyframes tileSpawn { from { transform: scale(0); } to { transform: scale(1); } }
@keyframes tileMerge { 0% { transform: scale(1); } 40% { transform: scale(1.22); } 100% { transform: scale(1); } }
.tile.spawn { animation: tileSpawn 130ms ease-out; }
.tile.merge { animation: tileMerge 160ms ease-out; }

/* 동물 진화 테마 타일 */
.tile.animal svg {
  width: 84%;
  height: 84%;
  display: block;
}
.tile .val-badge {
  position: absolute;
  bottom: 3px;
  right: 5px;
  font-size: 10px;
  font-weight: 700;
  color: rgba(70, 54, 33, 0.62);
  line-height: 1;
  pointer-events: none;
}

.tile[data-bomb-target="1"] {
  outline: 2px dashed var(--danger);
  cursor: pointer;
  pointer-events: auto;
}

.hint {
  font-size: 12px;
  color: #7d7466;   /* 접근성: 대비 상향(4.5:1 확보) */
  text-align: center;
  margin: 10px 0 0;
}

.footer {
  display: flex;
  justify-content: space-between;
  margin-top: 18px;
  font-size: 11px;
  color: #b8ad9d;
}

#adminLink { cursor: pointer; text-decoration: underline dotted; }

/* ---------- 모달 ---------- */
.modal-overlay[hidden] { display: none; }

.modal-overlay:not([hidden]) {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}

.modal {
  background: var(--panel);
  border-radius: 10px;
  padding: 18px;
  width: 100%;
  max-width: 420px;
  max-height: 86vh;
  overflow-y: auto;
}

.modal-sm { max-width: 300px; }
.modal-lg { max-width: 520px; }

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

.modal-head h2 { margin: 0; font-size: 18px; color: var(--ink-strong); }

.close-btn {
  border: none;
  background: none;
  font-size: 20px;
  cursor: pointer;
  color: var(--ink);
  line-height: 1;
  min-width: 44px;
  min-height: 44px;
}

/* 튜토리얼 / 설정 / 업적 */
.tut-demo { font-size: 30px; text-align: center; margin: 12px 0 4px; letter-spacing: 2px; }
.settings-actions { flex-wrap: wrap; justify-content: flex-start; }
.achv-list { display: flex; flex-direction: column; gap: 6px; }
.achv-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  opacity: 0.55;
}
.achv-item.done { opacity: 1; background: #f3efe6; }
.achv-item b { display: block; font-size: 13px; color: var(--ink-strong); }
.achv-item span { font-size: 11px; color: #8a8073; }

.modal-desc { font-size: 13px; color: #9a8f80; }

.section-title {
  font-size: 13px;
  color: var(--ink-strong);
  margin: 16px 0 8px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 4px;
}

.shop-list { display: flex; flex-direction: column; gap: 8px; }

.shop-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
}

.shop-item .name { font-weight: 600; font-size: 13px; color: var(--ink-strong); }
.shop-item .desc { font-size: 11px; color: #a99f92; }

.fine-print { font-size: 10px; color: #b8ad9d; margin-top: 10px; }

.result-score { font-size: 15px; }
.result-percentile {
  background: #eef6ee;
  border: 1px solid #cfe6cf;
  color: var(--good);
  padding: 10px 12px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 14px;
}
.result-coins { font-size: 13px; color: var(--gold); }
.result-record { font-size: 13px; color: var(--accent-strong); font-weight: 600; margin: 2px 0; }

.ad-actions { justify-content: stretch; }
.ad-actions .btn-ad, .ad-actions .btn-item { flex: 1; }

/* ---------- 출석 스트릭 트랙 ---------- */
.streak-track {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  margin: 12px 0;
}
.streak-day {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 2px;
  text-align: center;
  font-size: 10px;
  color: #a99f92;
}
.streak-day .day-coin { display: block; font-weight: 700; color: var(--gold); font-size: 11px; }
.streak-day.done { background: #f3efe6; }
.streak-day.today {
  background: var(--gold);
  border-color: var(--gold);
  color: #fff;
}
.streak-day.today .day-coin { color: #fff; }

/* ---------- 광고 시뮬레이션 ---------- */
.ad-sim { text-align: center; padding: 16px 8px; }
.ad-sim-badge {
  display: inline-block;
  background: #4a7c8c;
  color: #fff;
  font-size: 10px;
  padding: 2px 8px;
  border-radius: 4px;
  letter-spacing: 1px;
}
.ad-sim-text { font-size: 14px; color: var(--ink-strong); margin: 14px 0 6px; }
.ad-sim-timer {
  font-size: 40px;
  font-weight: 700;
  color: #4a7c8c;
  margin: 6px 0;
}

.modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
  justify-content: flex-end;
}

.pin-input, .text-input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  margin-top: 6px;
}

.pin-error { color: var(--danger); font-size: 12px; }

.admin-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.admin-grid .stat {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 12px;
}
.admin-grid .stat b { display: block; font-size: 16px; color: var(--ink-strong); }

.admin-price-list { display: flex; flex-direction: column; gap: 6px; }
.admin-price-list .price-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}
.admin-price-list input { width: 90px; padding: 5px 7px; border: 1px solid var(--border); border-radius: 6px; }

.admin-row { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; font-size: 13px; }

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink-strong);
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 13px;
  z-index: 100;
}

/* ---------- 분위기 배경 (테마별) ---------- */
body {
  background-image: url("bg-light.webp");
  background-size: cover;
  background-position: center top;
  background-repeat: no-repeat;
  background-attachment: fixed;
}
:root[data-theme="dark"] body { background-image: url("bg-dark.webp"); }

/* 하단 AdMob 배너가 화면을 가리지 않도록 여백 확보 (배너가 뜰 때만 적용) */
body.has-banner { padding-bottom: 60px; }
