:root {
  --bg: #ffffff;
  --ink: #2c3a52;
  --muted: #8a97ab;
  --clue-bg: #e8eefc;
  --grid-line: #c3cbd9;
  --fill: #2c3a52;
  --accent: #2f7cf6;
  --danger: #e8412e;
  --cell-bg: #ffffff;
  --surf: #f4f7fc;
  --card: #ffffff;
  --btn-bg: #eef2f9;
}
:root[data-theme="dark"] {
  --btn-bg: #222c3e;
  --bg: #12161f;
  --ink: #dbe4f0;
  --muted: #7d8aa0;
  --clue-bg: #24304a;
  --grid-line: #3a465e;
  --fill: #dbe4f0;
  --accent: #4a90ff;
  --danger: #ff6a58;
  --cell-bg: #1b2333;
  --surf: #171d29;
  --card: #232c3e;
}

/* 접근성: OS 또는 설정이 모션 최소화를 요청하면 애니메이션 끔 */
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
.no-motion * { animation: none !important; transition: 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;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* ---------- 상단 ---------- */
.topbar {
  width: 100%;
  max-width: 460px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
}
.topbar .icon {
  border: none;
  background: none;
  font-size: 26px;
  color: var(--accent);
  cursor: pointer;
  min-width: 44px;
  min-height: 44px;
}
.title { text-align: center; }
.level-name { font-size: 17px; font-weight: 700; }
.level-diff { font-size: 12px; color: var(--muted); }

/* ---------- 상태바 (코인 + 상점) ---------- */
.statusbar {
  width: 100%;
  max-width: 460px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  margin-bottom: 4px;
}
.coins-pill {
  background: var(--clue-bg);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 15px;
  font-weight: 700;
  color: var(--ink);
}
.pill-btn {
  border: none;
  background: var(--accent);
  color: #fff;
  border-radius: 999px;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  min-height: 40px;
}

.lives {
  display: flex;
  gap: 6px;
  justify-content: center;
  font-size: 22px;
  margin: 4px 0 14px;
  min-height: 26px;
}
.heart-lost { filter: grayscale(1); opacity: 0.3; }

/* ---------- 퍼즐 ---------- */
.puzzle-wrap {
  width: 100%;
  max-width: 460px;
  padding: 0 12px;
  touch-action: none;      /* 드래그가 화면 스크롤로 새지 않게 */
  user-select: none;
  -webkit-user-select: none;
}
.puzzle {
  /* 격자 크기에 따라 JS가 바꿔 넣는 값 (5×5 → 10×10) */
  --clue-font: 15px;
  --clue-min: 58px;
  --cell-font: 22px;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);   /* minmax(0,·) 로 내용 최소크기 아래로도 축소 → 화면 밖으로 안 넘침 */
  grid-template-rows: auto 1fr;
  gap: 6px;
}
.corner {}

.col-clues { display: grid; gap: 3px; }
.col-clues .clue {
  background: var(--clue-bg);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  padding: 5px 2px;
  min-height: var(--clue-min);
  font-size: var(--clue-font);
  font-weight: 600;
  line-height: 1.15;
}

.row-clues { display: grid; gap: 3px; }
.row-clues .clue {
  background: var(--clue-bg);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 5px;
  padding: 2px 7px;
  min-width: var(--clue-min);
  font-size: var(--clue-font);
  font-weight: 600;
}
.clue.done { background: none; color: var(--muted); }

.grid {
  display: grid;
  gap: 0;
  border: 2px solid var(--ink);
  background: var(--grid-line);
}
.cell {
  aspect-ratio: 1 / 1;
  min-width: 0;              /* 칸이 내용(✕)보다 커지지 않도록 축소 허용 */
  overflow: hidden;
  background: var(--cell-bg);
  border: 0.5px solid var(--grid-line);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--cell-font);
  font-weight: 700;
  color: var(--ink);
  cursor: pointer;
}

/* 접근성: OS가 모션 최소화를 요청하면 애니메이션 끔 */
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
.cell.filled { background: var(--fill); }
.cell.marked::after { content: "✕"; }

/* ---------- 하단 툴바 ---------- */
.toolbar {
  display: flex;
  align-items: center;
  gap: 26px;
  margin: 26px 0 20px;
}
.mode-switch {
  display: flex;
  background: var(--btn-bg);
  border-radius: 999px;
  padding: 4px;
}
.mode-btn {
  border: none;
  background: none;
  width: 62px;
  height: 50px;
  border-radius: 999px;
  font-size: 20px;
  color: var(--muted);
  cursor: pointer;
}
.mode-btn.active {
  background: var(--card);
  color: var(--ink);
  box-shadow: 0 1px 4px rgba(0,0,0,0.12);
}

/* ---------- 설정 / 출석 ---------- */
.win-overlay.settings { background: var(--surf); align-items: flex-start; }
.set-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; padding: 12px 4px; font-size: 15px; color: var(--ink);
  border-bottom: 1px solid var(--grid-line);
}
.set-row label { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.set-row input[type="checkbox"] { width: 20px; height: 20px; }
#langSelect {
  padding: 8px 10px; border: 1px solid var(--grid-line); border-radius: 8px;
  background: var(--card); color: var(--ink); font-size: 14px;
}
.streak-track { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; margin: 14px 0 20px; }
.streak-day {
  border: 1px solid rgba(255,255,255,0.4); border-radius: 8px; padding: 6px 2px;
  text-align: center; font-size: 10px; color: rgba(255,255,255,0.85);
}
.streak-day b { display: block; font-size: 12px; }
.streak-day.today { background: rgba(255,255,255,0.9); color: #d99a12; border-color: #fff; }
.round-btn {
  border: none;
  background: var(--btn-bg);
  width: 58px;
  height: 58px;
  border-radius: 999px;
  font-size: 24px;
  color: var(--ink);
  cursor: pointer;
}
.round-btn:disabled { opacity: 0.4; cursor: default; }

.hint-btn {
  position: relative;
  border: none;
  background: var(--btn-bg);
  width: 58px;
  height: 58px;
  border-radius: 999px;
  font-size: 22px;
  cursor: pointer;
}
.hint-count {
  position: absolute;
  top: -4px;
  right: -4px;
  background: var(--accent);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  min-width: 22px;
  height: 22px;
  border-radius: 999px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ---------- 클리어/실패 화면 ---------- */
.win-overlay[hidden] { display: none; }   /* hidden 속성이 확실히 이기도록 */

.win-overlay:not([hidden]) {
  position: fixed;
  inset: 0;
  background: radial-gradient(circle at 50% 40%, #4aa8ff, #1f7fe0);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
  user-select: none;
  -webkit-user-select: none;
}
.win-overlay.lose { background: radial-gradient(circle at 50% 40%, #ff8f7a, #d8442f); }
.win-inner { text-align: center; color: #fff; width: 100%; max-width: 360px; }
.win-title { font-size: 34px; margin: 0 0 6px; text-shadow: 0 2px 8px rgba(0,0,0,0.18); }
.win-sub { font-size: 14px; opacity: 0.95; margin-bottom: 22px; }

.reveal-frame {
  background: #fff;
  border-radius: 16px;
  padding: 12px;
  position: relative;
  box-shadow: 0 8px 24px rgba(0,0,0,0.2);
  margin-bottom: 34px;
}
.reveal {
  display: grid;
  gap: 0;
  aspect-ratio: 1 / 1;
  border-radius: 6px;
  overflow: hidden;
}
.reveal div { width: 100%; height: 100%; }
.reveal-name {
  position: absolute;
  bottom: -14px;
  left: 50%;
  transform: translateX(-50%);
  background: #fff;
  color: var(--ink);
  font-weight: 700;
  padding: 6px 20px;
  border-radius: 999px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

.next-btn {
  width: 100%;
  border: none;
  background: rgba(255,255,255,0.22);
  color: #fff;
  font-size: 18px;
  font-weight: 700;
  padding: 16px;
  border-radius: 14px;
  cursor: pointer;
}
.next-btn:active { transform: translateY(1px); }
.next-btn + .next-btn { margin-top: 10px; }
.next-btn.ad-btn { background: #2f7cf6; font-size: 16px; }
.next-btn.ghost { background: rgba(255,255,255,0.14); font-size: 15px; padding: 12px; }

/* 보상형 광고 시뮬 */
.win-overlay.adsim { background: #1b1e2b; }
.ad-badge {
  display: inline-block;
  background: #3a4a6a;
  color: #fff;
  font-size: 12px;
  letter-spacing: 2px;
  padding: 3px 12px;
  border-radius: 6px;
  margin-bottom: 18px;
}
.ad-timer { font-size: 64px; font-weight: 800; color: #fff; margin-bottom: 14px; }

/* ---------- 컬렉션(도감) ---------- */
.win-overlay.collection { background: var(--surf); align-items: flex-start; }
.coll-inner {
  width: 100%;
  max-width: 460px;
  margin: 0 auto;
  color: var(--ink);
}
.coll-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}
.coll-head h2 { font-size: 22px; margin: 0; }
.close-x {
  border: none;
  background: none;
  font-size: 20px;
  color: var(--muted);
  cursor: pointer;
  min-width: 44px;
  min-height: 44px;
}
.coll-count { font-size: 13px; color: var(--muted); margin-bottom: 14px; }
.coll-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}
.coll-card {
  background: var(--card);
  border-radius: 12px;
  padding: 8px;
  text-align: center;
  box-shadow: 0 2px 8px rgba(0,0,0,0.07);
}
.coll-art {
  display: grid;
  aspect-ratio: 1 / 1;
  border-radius: 6px;
  overflow: hidden;
  margin-bottom: 6px;
}
.coll-art div { width: 100%; height: 100%; }
.coll-card .cname { font-size: 12px; font-weight: 700; }
.coll-card.locked .coll-art {
  background: #e3e8f0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  color: #b7c0cd;
}
.coll-card.locked .cname { color: var(--muted); }

/* ---------- 상점 ---------- */
.win-overlay.shop { background: var(--surf); align-items: flex-start; overflow-y: auto; }
.shop-inner { width: 100%; max-width: 460px; margin: 0 auto; color: var(--ink); padding-bottom: 30px; }
.shop-coins { font-size: 15px; margin: 2px 0 6px; }
.shop-coins b { color: #d99a12; font-size: 18px; }
.shop-sec {
  font-size: 14px; margin: 18px 0 8px;
  border-bottom: 1px solid var(--grid-line); padding-bottom: 5px;
}
.shop-item {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--card); border-radius: 12px; padding: 12px 14px; margin-bottom: 8px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
}
.shop-item .sname { font-weight: 700; font-size: 14px; }
.shop-item .sdesc { font-size: 11px; color: var(--muted); margin-top: 2px; }
.buy-btn {
  border: none; background: var(--accent); color: #fff;
  border-radius: 999px; padding: 9px 16px; font-size: 13px; font-weight: 700;
  cursor: pointer; white-space: nowrap; min-height: 40px;
}
.buy-btn.owned { background: #cfd6e2; color: #7a8598; cursor: default; }
.buy-btn.iap { background: #d99a12; }
.fine { font-size: 10px; color: var(--muted); margin-top: 12px; }

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

/* ---------- 튜토리얼 ---------- */
.win-overlay.tut { background: var(--surf); align-items: center; overflow-y: auto; }
.tut-inner {
  width: 100%; max-width: 420px; margin: auto; color: var(--ink);
  padding: 24px 22px 30px; text-align: left;
}
.tut-inner .coll-head { justify-content: center; margin-bottom: 18px; }
.tut-inner .coll-head h2 { font-size: 22px; }
.tut-demo {
  display: flex; align-items: center; justify-content: center; gap: 12px;
  background: var(--card); border-radius: 14px; padding: 16px; margin-bottom: 6px;
}
.tut-clue {
  font-weight: 800; font-size: 20px; color: var(--accent); letter-spacing: 3px;
  background: var(--clue-bg); padding: 6px 12px; border-radius: 8px;
}
.tut-arrow { font-size: 20px; color: var(--muted); }
.tut-cells { display: inline-flex; gap: 4px; }
.tut-cells i {
  width: 30px; height: 30px; border: 1.5px solid var(--grid-line);
  border-radius: 5px; display: flex; align-items: center; justify-content: center;
  font-size: 15px; color: var(--muted); background: var(--cell-bg);
}
.tut-cells i.fill { background: var(--fill); border-color: var(--fill); }
.tut-step {
  font-size: 15px; line-height: 1.55; margin: 14px 2px; color: var(--ink);
}
.tut-step b { color: var(--accent); }
.tut-inner .next-btn { margin-top: 18px; }

/* ---------- 분위기 배경 (테마별) ---------- */
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; }
