/* ───────── 기본/리셋 ───────── */
:root {
  --brand: #6c5ce7;
  --brand-2: #a29bfe;
  --bg: #f5f5f7;
  --card: #ffffff;
  --ink: #2d3436;
  --muted: #8a8f98;
  --line: #e7e7ee;
  --shadow: 0 6px 20px rgba(0, 0, 0, 0.08);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
  color: var(--ink); background: var(--bg);
  overscroll-behavior: none;
}
#app, .screen { height: 100%; }
.hidden { display: none !important; }
button { font-family: inherit; cursor: pointer; border: none; background: none; }

/* ───────── 갤러리 ───────── */
#gallery {
  overflow-y: auto; padding: 0 16px calc(24px + env(safe-area-inset-bottom));
  overscroll-behavior-y: contain; /* 당겨서 새로고침(실수 초기화 위험) 차단 */
}
.g-header {
  padding: calc(28px + env(safe-area-inset-top)) 4px 14px;
}
.g-header h1 { margin: 0; font-size: 30px; font-weight: 800; letter-spacing: -0.5px; }
.g-sub { margin: 6px 0 0; color: var(--muted); font-size: 14px; }

/* 계정/백업 줄 */
.g-account {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  margin-top: 10px;
}
.g-account-who { font-size: 13px; font-weight: 700; color: var(--ink); }
.g-sync { font-size: 12px; color: var(--muted); }
.g-account-btn {
  font-size: 13px; font-weight: 700; color: var(--ink);
  background: var(--card); border: 1px solid var(--line);
  padding: 7px 11px; border-radius: 10px;
}
.g-account-btn.primary { color: #fff; background: var(--brand); border-color: var(--brand); }

/* 로그인 화면 */
.auth-overlay {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(20, 20, 30, 0.55); backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.auth-box {
  background: var(--card); border-radius: 24px; padding: 24px;
  width: 100%; max-width: 340px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 10px;
  animation: rise 0.35s ease;
}
.auth-box h2 { margin: 0; font-size: 22px; text-align: center; }
.auth-sub { margin: 0 0 6px; color: var(--muted); font-size: 13px; text-align: center; line-height: 1.5; }
.auth-input {
  padding: 13px; border-radius: 12px; border: 1.5px solid var(--line);
  font-size: 15px; font-family: inherit; outline: none;
}
.auth-input:focus { border-color: var(--brand); }
.auth-msg { min-height: 20px; font-size: 14px; font-weight: 600; color: #e05252; text-align: center; }
.auth-msg.ok { color: #2e9e5b; }
button:disabled { opacity: 0.55; }
.auth-close { padding: 8px; color: var(--muted); font-size: 14px; }

/* "또는" 구분선 */
.auth-or {
  display: flex; align-items: center; gap: 10px;
  color: var(--muted); font-size: 12px; margin: 2px 0;
}
.auth-or::before, .auth-or::after {
  content: ""; flex: 1; height: 1px; background: var(--line);
}
/* 소셜 로그인 버튼 */
.auth-social {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  padding: 13px; border-radius: 12px; font-size: 15px; font-weight: 600;
  font-family: inherit; border: 1.5px solid var(--line); cursor: pointer;
}
.auth-social:active { transform: scale(0.98); }
.auth-social .auth-social-ic {
  display: inline-flex; align-items: center; justify-content: center;
  width: 20px; height: 20px; border-radius: 50%; font-size: 13px; font-weight: 800;
}
.auth-social.google { background: #fff; color: #3c4043; border-color: #dadce0; }
.auth-social.google .auth-social-ic { color: #4285f4; }
.auth-social.kakao { background: #fee500; color: #191600; border-color: #fee500; }
.auth-social.naver { background: #03c75a; color: #ffffff; border-color: #03c75a; }
.auth-social.naver .auth-social-ic { color: #ffffff; font-weight: 900; }

.g-grid {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px;
}
@media (min-width: 640px) { .g-grid { grid-template-columns: repeat(3, 1fr); } }

.g-card {
  display: flex; flex-direction: column; text-align: left;
  background: var(--card); border-radius: 18px; overflow: hidden;
  box-shadow: var(--shadow); padding: 0; transition: transform 0.12s ease;
}
.g-card:active { transform: scale(0.97); }
.g-thumb {
  position: relative; aspect-ratio: 1 / 1; background: #fff;
  display: flex; align-items: center; justify-content: center;
  border-bottom: 1px solid var(--line);
}
.thumb-svg { width: 100%; height: 100%; display: block; }
.g-badge {
  position: absolute; top: 8px; right: 8px;
  background: var(--brand); color: #fff; font-size: 11px; font-weight: 700;
  padding: 4px 8px; border-radius: 999px;
}
.g-meta { padding: 10px 12px 12px; }
.g-name { font-weight: 700; font-size: 15px; }
.g-bar { margin-top: 8px; height: 6px; background: var(--line); border-radius: 999px; overflow: hidden; }
.g-bar-fill { height: 100%; background: linear-gradient(90deg, var(--brand), var(--brand-2)); border-radius: 999px; transition: width 0.3s; }

/* ───────── 색칠 화면 ───────── */
#canvas { display: flex; flex-direction: column; height: 100%; }
.c-top {
  display: flex; align-items: center; gap: 10px;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  border-bottom: 1px solid var(--line); background: var(--card);
}
.c-back { font-size: 16px; font-weight: 600; color: var(--brand); padding: 6px 4px; }
.c-title { flex: 1; text-align: center; font-weight: 700; font-size: 16px; }
.c-progress {
  min-width: 52px; text-align: right; font-weight: 700; color: var(--brand); font-size: 15px;
}
.c-preview-btn {
  font-weight: 700; font-size: 13px; color: var(--brand);
  background: #efecfd; padding: 7px 10px; border-radius: 10px;
}
.c-mute-btn { font-size: 16px; padding: 5px 6px; }

/* 남은 조각 찾기 버튼 */
.c-findbtn {
  position: absolute; right: 12px; bottom: 14px; z-index: 5;
  width: 48px; height: 48px; border-radius: 50%;
  background: var(--card); box-shadow: var(--shadow);
  border: 1px solid var(--line); font-size: 20px;
}
.c-findbtn:active { transform: scale(0.94); }

/* 힌트/찾기 반짝임 */
.region.hintflash { animation: hintFlash 0.6s ease-in-out 3; }
@keyframes hintFlash {
  0%, 100% { fill: #ffffff; }
  50% { fill: #ffd23f; }
}

/* 길잡이 화살표(물감 길게 누르기) */
.c-guide-arrow {
  position: absolute; z-index: 7; display: none;
  color: #e23e3e; font-size: 42px; line-height: 1;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
  pointer-events: none;
  animation: guidePulse 0.9s ease-in-out infinite;
}
@keyframes guidePulse {
  0%, 100% { opacity: 0.7; }
  50% { opacity: 1; }
}

/* 상단 토스트(중요 경고) */
#app-toast {
  position: fixed; top: calc(10px + env(safe-area-inset-top)); left: 12px; right: 12px;
  z-index: 100; background: #d64545; color: #fff;
  padding: 13px 16px; border-radius: 14px;
  font-size: 14px; font-weight: 700; line-height: 1.45;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  opacity: 0; transform: translateY(-16px); pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
}
#app-toast.show { opacity: 1; transform: none; }

/* 갤러리 통계 줄 */
.g-stats { margin: 8px 0 0; font-size: 13px; font-weight: 600; color: var(--muted); }

/* 완성본 미리보기 저장 버튼 */
.c-preview-save {
  padding: 12px 18px; border-radius: 12px; font-weight: 700; font-size: 14px;
  background: var(--brand); color: #fff;
}

/* ── 미니맵 ── */
.c-minimap {
  position: absolute; top: 10px; right: 10px; width: 108px;
  background: #fff; border-radius: 10px; overflow: hidden;
  box-shadow: var(--shadow); border: 1px solid var(--line);
  z-index: 5; line-height: 0;
}
.c-minimap canvas { width: 100%; display: block; }
.c-minimap-view {
  position: absolute; box-sizing: border-box;
  border: 2px solid var(--brand); border-radius: 2px;
  background: rgba(108, 92, 231, 0.08); pointer-events: none;
}

/* ── 완성본 미리보기 ── */
.c-preview {
  position: absolute; inset: 0; z-index: 6;
  background: rgba(250, 250, 251, 0.98);
  display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 12px;
  padding: 16px;
}
.c-preview canvas {
  max-width: 100%; max-height: 84%; width: auto; height: auto;
  border-radius: 10px; box-shadow: var(--shadow);
}
.c-preview-label { font-size: 13px; color: var(--muted); font-weight: 600; }

.c-stage {
  flex: 1; overflow: hidden; position: relative;
  background: #fafafb; touch-action: none;
  display: flex; align-items: center; justify-content: center;
}
.c-viewport {
  width: 100%; height: 100%; max-width: 100vh;
  transform-origin: center center;
  display: flex; align-items: center; justify-content: center;
  /* will-change: transform 금지 — 대형 SVG가 통째로 레이어가 되면
     고배율에서 흐릿한 텍스처만 확대되고 재래스터가 멈춘다.
     대신 화면 밖 조각을 숨기는 컬링(engine._updateCulling)으로 해결 */
}
.region.cull, .region-num.cull { display: none; }
.c-svg { width: 100%; height: 100%; display: block; }

.region {
  fill: #ffffff; stroke: #cfcfd6; stroke-width: 2.5;
  transition: fill 0.15s ease;
}
.region.target {
  stroke: #2e2e3a; stroke-width: 4;            /* 진한 테두리 */
  fill: #ece7fd;                               /* 옅은 보라 배경(칠할 곳 표시) */
  animation: targetPulse 1.4s ease-in-out infinite;
}
.region-num.target-num { fill: #2e2e3a; font-weight: 800; }
.region.filled { stroke: rgba(0,0,0,0.12); }
.region.pop { animation: pop 0.28s ease; }
.region.shake { animation: shake 0.32s ease; }
.region-num {
  fill: #7a828c; font-weight: 700; text-anchor: middle;
  dominant-baseline: central; pointer-events: none; user-select: none;
  text-rendering: optimizeSpeed;
}
/* 사진 도안 축소 상태: 번호 숨김(LOD) — 확대하면 나타남 */
.c-svg.nums-off .region-num { display: none; }

@keyframes targetPulse {
  0%, 100% { stroke-opacity: 0.5; }
  50% { stroke-opacity: 1; }
}
.c-svg.huge .region.target { animation: none; stroke-opacity: 1; }
@keyframes pop {
  0% { transform: scale(1); } 40% { transform: scale(1.06); } 100% { transform: scale(1); }
  transform-box: fill-box; transform-origin: center;
}
.region.pop { transform-box: fill-box; transform-origin: center; }
@keyframes shake {
  0%,100% { transform: translateX(0); }
  25% { transform: translateX(-3px); } 75% { transform: translateX(3px); }
}

/* ───────── 팔레트 ───────── */
.c-palette {
  display: flex; gap: 10px; overflow-x: auto;
  padding: 12px 14px calc(12px + env(safe-area-inset-bottom));
  background: var(--card); border-top: 1px solid var(--line);
  scrollbar-width: none;
}
.c-palette::-webkit-scrollbar { display: none; }
/* 진행률 링(원 둘레) — 배경을 conic-gradient로 채워 표시 */
.swatch-wrap {
  flex: 0 0 auto; padding: 3px; border-radius: 50%;
  background: conic-gradient(var(--line) 0, var(--line) 0);
  transition: transform 0.12s ease;
}
.swatch {
  position: relative; display: block;
  width: 48px; height: 48px; border-radius: 50%;
  box-shadow: inset 0 0 0 2px rgba(0,0,0,0.06), 0 0 0 2px #fff inset;
  transition: transform 0.12s ease;
}
.swatch-num {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 17px; color: #fff;
  text-shadow: 0 1px 3px rgba(0,0,0,0.4);
}
.swatch.active { transform: scale(1.1); box-shadow: 0 0 0 3px var(--brand); }
.swatch.done::after {
  content: "✓"; position: absolute; right: -4px; top: -4px;
  width: 18px; height: 18px; border-radius: 50%;
  background: #2eb872; color: #fff; font-size: 12px; font-weight: 800;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}
.swatch.donePop { animation: pop 0.45s ease; }

/* 색 완료 파티클 터짐 */
.burst { position: fixed; z-index: 45; pointer-events: none; }
.burst i {
  position: absolute; left: -4px; top: -4px;
  width: 9px; height: 9px; border-radius: 50%;
  animation: burstFly 0.65s ease-out forwards;
}
@keyframes burstFly {
  0% { transform: translate(0, 0) scale(1); opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) scale(0.3); opacity: 0; }
}

/* ───────── 사진 변환 화면 ───────── */
#converter { display: flex; flex-direction: column; height: 100%; }
.cv-body {
  flex: 1; overflow-y: auto; padding: 18px 16px calc(24px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 14px;
}
.cv-pick {
  padding: 14px; border-radius: 14px; font-weight: 700; font-size: 16px;
  background: var(--card); color: var(--brand); border: 2px dashed var(--brand-2);
}
.cv-preview {
  background: var(--card); border-radius: 16px; box-shadow: var(--shadow);
  min-height: 220px; display: flex; align-items: center; justify-content: center;
  overflow: hidden; padding: 8px;
}
.cv-preview .thumb-svg, .cv-src { max-width: 100%; max-height: 330px; border-radius: 10px; }
.cv-hint { color: var(--muted); font-size: 14px; padding: 30px 10px; text-align: center; }
.cv-seg-wrap { display: flex; align-items: center; gap: 12px; }
.cv-seg-label { width: 62px; font-weight: 700; font-size: 14px; color: var(--ink); }
.cv-seg { flex: 1; display: flex; background: var(--card); border-radius: 12px; padding: 4px; box-shadow: var(--shadow); }
.cv-seg button {
  flex: 1; padding: 10px 4px; border-radius: 9px; font-weight: 700; font-size: 14px; color: var(--muted);
}
.cv-seg button.on { background: var(--brand); color: #fff; }
.cv-info { text-align: center; color: var(--muted); font-size: 14px; min-height: 18px; }
.cv-action {
  padding: 15px; border-radius: 14px; font-weight: 800; font-size: 16px;
  background: var(--card); color: var(--ink); box-shadow: var(--shadow);
}
.cv-action:disabled { opacity: 0.5; }
.cv-action.primary { background: var(--brand); color: #fff; }

/* 갤러리: 추가 카드/삭제 버튼 */
.g-add-thumb { background: linear-gradient(135deg, #f0eeff, #e8f6ff); }
.g-add-plus { font-size: 52px; color: var(--brand); font-weight: 300; }
.g-del {
  position: absolute; top: 8px; left: 8px; width: 26px; height: 26px;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,0.45); color: #fff; border-radius: 50%;
  font-size: 13px; line-height: 1;
}
.g-share {
  position: absolute; top: 8px; left: 40px; width: 26px; height: 26px;
  display: flex; align-items: center; justify-content: center;
  background: rgba(108,92,231,0.75); color: #fff; border-radius: 50%;
  font-size: 12px; line-height: 1;
}

/* 사진 도안: 얇은 윤곽선(색이 많고 영역이 작아서) */
.custom-art .region { stroke-width: 0.55; stroke: #c9ccd4; }
.custom-art .region.target { stroke-width: 1.6; stroke: #2e2e3a; }
.custom-art .region.filled { stroke-width: 2.2; } /* stroke 색은 JS가 fill과 동일하게 지정 */

/* ───────── 완성 오버레이 ───────── */
.overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(20, 20, 30, 0.55); backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.o-box {
  background: var(--card); border-radius: 24px; padding: 24px;
  width: 100%; max-width: 340px; text-align: center; box-shadow: var(--shadow);
  animation: rise 0.35s ease;
}
@keyframes rise { from { transform: translateY(20px) scale(0.96); opacity: 0; } to { transform: none; opacity: 1; } }
.o-thumb {
  width: 160px; height: 160px; margin: 0 auto 14px;
  border-radius: 16px; overflow: hidden; box-shadow: var(--shadow);
}
.o-thumb .thumb-svg { width: 100%; height: 100%; }
.o-box h2 { margin: 4px 0 6px; font-size: 24px; }
.o-box p { margin: 0 0 18px; color: var(--muted); font-size: 14px; }
.o-btns { display: flex; flex-direction: column; gap: 10px; }
.o-btn {
  padding: 13px; border-radius: 14px; font-weight: 700; font-size: 15px;
  background: var(--line); color: var(--ink);
}
.o-btn.primary { background: var(--brand); color: #fff; }

.o-confetti { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
.o-confetti i {
  position: absolute; top: -20px; width: 10px; height: 14px; border-radius: 2px;
  animation: fall 2.4s linear infinite;
}
@keyframes fall {
  0% { transform: translateY(-20px) rotate(0); opacity: 1; }
  100% { transform: translateY(105vh) rotate(540deg); opacity: 0.9; }
}
