/* ─── 리셋 및 전역 변수 ─── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:           #f8f9fa;
  --surface:      #ffffff;
  --border:       #dee2e6;
  --text:         #212529;
  --muted:        #6c757d;
  --primary:      #4361ee;
  --primary-dark: #3a0ca3;
  --correct:      #40c057;   /* 올바른 위치 조각 색 */
  --swap-hover:   #f59f00;   /* 드래그 hover 색 */
  --radius:       8px;
  --shadow:       0 2px 8px rgba(0,0,0,0.08);
  --hud-height:   56px;
}

html, body { height: 100%; overflow: hidden; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Apple SD Gothic Neo', sans-serif;
  background: var(--bg);
  color: var(--text);
  /* iOS pull-to-refresh / 텍스트 자동 리사이즈 / 탭 하이라이트 차단 — 게임 중 시스템 제스처가 끼어들지 않게 */
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

/* dvh: iOS Safari 주소창 노출/숨김에 따라 100vh가 변동 → 보드/HUD가 잘리는 문제 방지.
   미지원 브라우저는 위 100vh로 폴백. */
#app {
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  position: relative;
  overflow: hidden;
  /* viewport-fit=cover 로 노치/홈 인디케이터 안쪽까지 콘텐츠가 흘러들 수 있어
     안전 영역만큼 padding으로 쳐내 시스템 UI에 가리지 않게 함. */
  padding-top:    env(safe-area-inset-top);
  padding-right:  env(safe-area-inset-right);
  padding-bottom: env(safe-area-inset-bottom);
  padding-left:   env(safe-area-inset-left);
}

/* ─── 화면 전환 ─── */
.screen { position: absolute; inset: 0; display: none; flex-direction: column; overflow: hidden; }
.screen.active { display: flex; }
.hidden { display: none !important; }

/* ─── 공통 버튼 ─── */
.btn {
  padding: 10px 20px;
  border: none; border-radius: var(--radius);
  font-size: 14px; font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
  white-space: nowrap; line-height: 1;
  /* 모바일 더블탭 줌 차단 + 300ms 클릭 지연 제거 — 버튼 누르면 곧바로 반응 */
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-primary  { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); }
.btn-secondary { background: var(--surface); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--bg); }
.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--bg); }
.btn-sm   { padding: 6px 12px; font-size: 12px; }
.btn-text { background: none; border: none; font-size: 12px; color: var(--muted); cursor: pointer; text-decoration: underline; }

/* ─── 홈 화면 (sticky 새 퍼즐 만들기 + 단일 피드) ─── */
#screen-home { background: var(--bg); }

/* 스크롤 컨테이너 — sticky의 부모 역할. 좁은 너비 유지(가독성) + overflow-y auto */
.home-scroll {
  flex: 1;
  overflow-y: auto;
  width: 100%;
  /* iOS 관성 스크롤 부드럽게 */
  -webkit-overflow-scrolling: touch;
}

/* sticky 새 퍼즐 만들기 영역 — 모던 SNS 트렌드 차용: 작은 wordmark + 단일 인라인 액션 카드.
   장식적 텍스트(tagline, hint)를 제거하고 액션만 남겨 sticky 면적 최소화. */
.home-create {
  position: sticky;
  top: 0;
  z-index: 5;
  background: rgba(248, 249, 250, 0.92);
  /* 모던 글래스 효과 — 피드가 sticky 뒤로 흘러도 부드럽게 비침 */
  backdrop-filter: saturate(180%) blur(10px);
  -webkit-backdrop-filter: saturate(180%) blur(10px);
  padding: 8px 16px 10px;
  display: flex; flex-direction: column;
  gap: 8px;
  max-width: 480px; margin: 0 auto;
}
/* sticky가 콘텐츠와 겹치지 않게 살짝 그라데이션으로 시각 분리 */
.home-create::after {
  content: '';
  position: absolute;
  left: 0; right: 0; bottom: -10px; height: 10px;
  background: linear-gradient(to bottom, rgba(0,0,0,0.06), transparent);
  pointer-events: none;
}

.home-brand h1 {
  font-size: 18px; font-weight: 800; letter-spacing: -0.5px;
  color: var(--primary);
  display: inline-flex; align-items: center; gap: 4px;
}
.home-brand .brand-mark { font-size: 16px; }
/* tagline은 기본(컴팩트) 모드에선 숨김. 빈 피드 모드에서만 노출. */
.home-tagline { display: none; }

/* 우상단 작은 계정 버튼 — 부담 없이, 익명 사용자도 안 거슬리게.
   home-create 가 sticky(=relative containing block) 라서 absolute 기준이 됨. */
.auth-btn {
  position: absolute;
  top: 8px; right: 12px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 11px; color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
  touch-action: manipulation;
  max-width: 180px; overflow: hidden; text-overflow: ellipsis;
  z-index: 1;
}
.auth-btn:hover { background: var(--surface); color: var(--text); border-color: var(--primary); }
@media (pointer: coarse) {
  .auth-btn { padding: 6px 12px; font-size: 12px; min-height: 32px; }
}

/* ─── 빈 피드 모드 — 큰 가운데 정렬 업로드 카드 (첫 사용자 환영 화면) ─── */
.home-scroll.is-empty {
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.home-scroll.is-empty .home-create {
  position: static;
  background: transparent;
  backdrop-filter: none; -webkit-backdrop-filter: none;
  padding: 32px 20px;
  gap: 18px;
  text-align: center;
  align-items: center;
}
.home-scroll.is-empty .home-create::after { display: none; }
.home-scroll.is-empty .home-brand h1 { font-size: 28px; gap: 6px; }
.home-scroll.is-empty .home-brand .brand-mark { font-size: 22px; }
.home-scroll.is-empty .home-tagline {
  display: block;
  margin-top: 6px;
  color: var(--muted);
  font-size: 14px;
}
.home-scroll.is-empty .upload-zone {
  flex-direction: column;
  padding: 32px 24px;
  gap: 12px;
  text-align: center;
  border-width: 2px;
}
.home-scroll.is-empty .upload-icon { font-size: 44px; }
.home-scroll.is-empty .upload-cta {
  white-space: normal;
  font-size: 15px;
  font-weight: 600;
}
.home-scroll.is-empty .upload-buttons { width: 100%; justify-content: center; gap: 8px; }
.home-scroll.is-empty .home-feed { display: none; }

/* 피드 영역 */
.home-feed {
  max-width: 480px; margin: 0 auto;
  padding: 16px 20px 24px;
  display: flex; flex-direction: column; gap: 10px;
}
.home-feed-title {
  font-size: 14px; font-weight: 700; color: var(--muted);
  letter-spacing: 0.5px;
  padding: 0 4px;
}
.home-feed-list { display: flex; flex-direction: column; gap: 10px; }

/* 카드 안 뱃지 — 만듦 / 플레이 구분 */
.feed-badge {
  display: inline-block;
  font-size: 10px; font-weight: 700;
  padding: 2px 6px;
  border-radius: 10px;
  letter-spacing: 0.3px;
  vertical-align: middle;
  margin-left: 6px;
}
.feed-badge.created { background: #e7f5ff; color: #1971c2; }
.feed-badge.played  { background: #fff4e6; color: #d9480f; }

/* placeholder 화면 (history, preview 등 — Phase 2+에서 채움) */
.placeholder-main {
  flex: 1;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 40px 20px; gap: 8px; text-align: center;
}
.placeholder-icon  { font-size: 56px; line-height: 1; margin-bottom: 8px; }
.placeholder-title { font-size: 22px; font-weight: 700; color: var(--text); }
.placeholder-text  { color: var(--muted); font-size: 14px; }

/* ─── 닉네임 입력 / 랭킹 / 공유 prompt 공통 ─── */
.submit-main {
  flex: 1;
  display: flex; flex-direction: column;
  align-items: center;
  gap: 12px; padding: 24px 20px;
  max-width: 440px; width: 100%;
  margin: 0 auto;
  overflow-y: auto;
}

/* share-prompt / preview / leaderboard 가 공유하는 썸네일 래퍼 */
.share-thumb-wrap {
  border-radius: var(--radius); overflow: hidden;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}
.share-thumb-wrap canvas { display: block; }
#leaderboard-thumb { width: 96px;  height: 96px;  }

/* preview 화면의 난이도·조각수 메타, share-prompt 라벨 */
.share-meta { color: var(--muted); font-size: 13px; }

.share-label {
  font-size: 13px; font-weight: 600;
  align-self: flex-start; margin-top: 8px; color: var(--text);
}

#submit-nickname-input,
#share-prompt-title-input {
  width: 100%;
  padding: 12px 14px;
  /* 16px 미만이면 iOS Safari가 포커스 시 자동 확대 → 16px 이상으로 고정 */
  font-size: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
}
#submit-nickname-input:focus,
#share-prompt-title-input:focus {
  outline: none;
  border-color: var(--primary);
}

.share-hint {
  font-size: 11px; color: #adb5bd;
  align-self: flex-end; margin-top: -4px;
}

#btn-confirm-submit {
  width: 100%; padding: 14px; font-size: 16px;
  margin-top: 8px;
}

/* ─── 친구 공유 prompt (솔로 완성 후) ─── */
.share-prompt-main {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; gap: 12px; padding: 20px;
  max-width: 440px; width: 100%; margin: 0 auto;
  overflow-y: auto;
}
.share-prompt-icon { font-size: 56px; line-height: 1; }
.share-prompt-headline { font-size: 18px; font-weight: 700; text-align: center; }
#share-prompt-thumb { width: 140px; height: 140px; }
#btn-share-prompt-share { width: 100%; padding: 14px; font-size: 16px; margin-top: 8px; }
#btn-share-prompt-skip  { margin-top: 4px; }

/* ─── 공유 퍼즐 미리보기 ─── */
.preview-main {
  flex: 1; display: flex; flex-direction: column;
  align-items: center;
  gap: 14px; padding: 16px 20px;
  max-width: 480px; width: 100%;
  margin: 0 auto;
  overflow-y: auto;
}
.preview-state {
  display: flex; flex-direction: column;
  align-items: center; gap: 14px;
  width: 100%;
}

/* 상단 카드 — 썸네일 + 제목/난이도 */
.preview-header-card {
  display: flex; gap: 14px; align-items: center;
  padding: 12px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 100%;
}
.preview-thumb-wrap {
  border-radius: 6px; overflow: hidden;
  border: 1px solid var(--border);
  flex-shrink: 0;
}
#preview-thumb { width: 96px; height: 96px; display: block; }
.preview-header-info {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column; gap: 4px;
}
.preview-puzzle-title {
  font-size: 18px; font-weight: 700;
  word-break: break-all;
  line-height: 1.3;
}

/* 랭킹 섹션 */
.preview-leaderboard-section {
  width: 100%;
  display: flex; flex-direction: column; gap: 6px;
}
.preview-leaderboard-label {
  font-size: 14px; font-weight: 700; color: var(--text);
  display: flex; align-items: center; gap: 6px;
  padding: 0 4px;
}
.preview-lb-count { font-size: 12px; color: var(--muted); font-weight: 400; }

.preview-play-btn { width: 100%; padding: 14px; font-size: 16px; }
.preview-report-btn { margin-top: 4px; align-self: center; font-size: 11px; }

/* ─── 닉네임 입력 (랭킹 미리보기) ─── */
.submit-rank-msg {
  font-size: 22px; font-weight: 800;
  text-align: center; color: var(--text);
  margin-top: 4px;
}
.submit-rank-sub {
  font-size: 13px; color: var(--muted); text-align: center;
  margin-top: -4px;
}
.submit-preview-list { width: 100%; }

/* 미리보기 안의 "내 기록" 행 — 노랑 배경 + 굵은 좌측 강조 바 */
.leaderboard-row.is-user-preview {
  background: #fff3cd;
  font-weight: 700;
  border-left: 3px solid var(--swap-hover);
  animation: row-pulse 1.6s ease-in-out infinite;
}
@keyframes row-pulse {
  0%, 100% { background: #fff3cd; }
  50%      { background: #ffe69c; }
}
/* 닉네임 비어있을 때 placeholder 스타일 */
.submit-name-placeholder {
  color: var(--muted) !important;
  font-style: italic;
  font-weight: normal !important;
}
#btn-skip-submit { margin-top: 4px; }

/* ─── 랭킹 ─── */
.leaderboard-main {
  flex: 1; display: flex; flex-direction: column;
  gap: 16px; padding: 16px 20px;
  max-width: 480px; width: 100%; margin: 0 auto;
  overflow-y: auto;
}
.leaderboard-info {
  display: flex; gap: 12px; align-items: center;
  padding: 12px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius);
}
.leaderboard-thumb-wrap {
  border-radius: 4px; overflow: hidden;
  border: 1px solid var(--border);
  flex-shrink: 0;
}
.leaderboard-meta { color: var(--muted); font-size: 13px; }

.leaderboard-list {
  display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden;
}
.leaderboard-row {
  display: grid;
  grid-template-columns: 40px 1fr auto auto;
  gap: 8px; padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  align-items: center;
  font-size: 14px;
}
.leaderboard-row:last-child { border-bottom: none; }
.leaderboard-row.is-me {
  background: #f0f4ff;
  font-weight: 700;
}
.leaderboard-rank { font-weight: 700; color: var(--muted); text-align: center; }
.leaderboard-rank.top1 { color: #f59f00; }
.leaderboard-rank.top2 { color: #adb5bd; }
.leaderboard-rank.top3 { color: #c08e3a; }
.leaderboard-name { font-weight: 600; }
.leaderboard-time { font-variant-numeric: tabular-nums; }
.leaderboard-moves { font-size: 12px; color: var(--muted); }
.leaderboard-empty {
  padding: 32px 16px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}

.leaderboard-actions {
  display: flex; gap: 10px;
}
.leaderboard-actions .btn { flex: 1; }

/* ─── 피드 카드 (홈 단일 피드에서 사용) ─── */
.history-card {
  display: grid;
  grid-template-columns: 72px 1fr;
  gap: 12px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  align-items: stretch;
}
.history-thumb {
  width: 72px; height: 72px;
  border-radius: 6px; overflow: hidden;
  border: 1px solid var(--border);
  background: var(--bg);
}
.history-thumb canvas, .history-thumb img {
  width: 100%; height: 100%; display: block; object-fit: cover;
}
.history-info {
  display: flex; flex-direction: column;
  gap: 4px; min-width: 0;
}
.history-title {
  font-size: 15px; font-weight: 700;
  color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.history-meta {
  font-size: 12px; color: var(--muted);
}
.history-stat {
  font-size: 12px; color: var(--text);
  font-variant-numeric: tabular-nums;
}
.history-actions {
  display: flex; gap: 6px; margin-top: 6px; flex-wrap: wrap;
}
.history-actions .btn { padding: 5px 10px; font-size: 11px; }
/* 이모지만 들어있는 작은 정사각형 액션 — 폭 절약, 의미는 aria-label/title로 보강 */
.history-actions .btn.btn-icon { padding: 5px 8px; font-size: 14px; line-height: 1; }

/* 인라인 액션 카드 — 가로 row: [아이콘] [CTA 텍스트] [파일/카메라 버튼] */
.upload-zone {
  width: 100%;
  border: 1.5px dashed var(--border); border-radius: var(--radius);
  background: var(--surface);
  padding: 10px 12px;
  display: flex; align-items: center;
  gap: 10px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.upload-zone:hover, .upload-zone.drag-over { border-color: var(--primary); background: #f0f4ff; }
.upload-icon { font-size: 22px; line-height: 1; flex-shrink: 0; }
.upload-cta {
  flex: 1; min-width: 0;
  font-size: 14px; font-weight: 600; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.upload-buttons { display: flex; gap: 6px; flex-shrink: 0; }
/* 카메라 촬영 버튼 — 터치 기기에서만 노출 */
.upload-camera-btn { display: none; }
@media (pointer: coarse) {
  .upload-camera-btn { display: inline-flex; align-items: center; }
}

/* 사진 선택 후 컴팩트 미리보기 — 가로 row: [썸네일] [정보+변경] [다음 →] */
.preview-wrap {
  width: 100%;
  display: flex; align-items: center;
  gap: 10px;
  padding: 8px 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.preview-wrap img {
  width: 56px; height: 56px;
  flex-shrink: 0;
  object-fit: cover;
  border-radius: 6px;
  border: 1px solid var(--border);
}
.preview-meta {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column; gap: 2px;
}
.preview-meta-text { font-size: 13px; font-weight: 600; color: var(--text); }
#btn-change { font-size: 11px; padding: 0; align-self: flex-start; min-height: 0; }
.btn-next-compact { width: auto; padding: 10px 16px; font-size: 14px; flex-shrink: 0; }

/* ─── 난이도 선택 화면 ─── */
.sub-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface); flex-shrink: 0;
}
.sub-header h2 { font-size: 15px; font-weight: 700; }

.difficulty-main {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  gap: 20px; padding: 24px 20px; overflow-y: auto;
}

#thumb-wrap img {
  width: 150px; height: 110px; object-fit: cover;
  border-radius: var(--radius); border: 1px solid var(--border);
}
.guide-text { color: var(--muted); font-size: 14px; }

.diff-cards { display: flex; gap: 10px; width: 100%; max-width: 440px; }
.diff-card {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  gap: 6px; padding: 18px 8px;
  background: var(--surface); border: 2px solid var(--border); border-radius: var(--radius);
  cursor: pointer; transition: border-color 0.15s, background 0.15s;
}
.diff-card:hover    { border-color: var(--primary); }
.diff-card.selected { border-color: var(--primary); background: #f0f4ff; }
.diff-name  { font-weight: 700; font-size: 15px; }
.diff-grid  { font-size: 13px; color: var(--muted); }
.diff-count { font-size: 12px; color: #adb5bd; }
#btn-start  { width: 100%; max-width: 320px; padding: 14px; font-size: 16px; }

/* ─── 게임 화면 ─── */
#screen-game { background: var(--bg); }

#hud {
  height: var(--hud-height);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 12px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow); flex-shrink: 0; gap: 8px;
}
.hud-center { display: flex; align-items: center; gap: 14px; }
#timer {
  font-size: 22px; font-weight: 700;
  font-variant-numeric: tabular-nums; letter-spacing: 1px;
  color: var(--primary);
}
#piece-count { font-size: 13px; color: var(--muted); }

/* ─── 모바일 터치 타깃 보강 ─── */
/* iOS HIG / Material 권장 44×44 미달인 작은 버튼들을 터치 기기에서만 키움.
   PC에선 작은 버튼이 정보 밀도에 도움이 되므로 유지. */
@media (pointer: coarse) {
  .btn-sm    { padding: 10px 14px; font-size: 13px; min-height: 40px; }
  .btn-text  { padding: 10px 12px; font-size: 13px; min-height: 40px; }
  .history-actions .btn { padding: 9px 14px; font-size: 12px; min-height: 38px; }
  .history-actions .btn.btn-icon { padding: 9px 10px; font-size: 16px; min-width: 38px; }
}

/* 게임 영역 — 트레이 없이 보드만 */
#game-area {
  flex: 1; display: flex; overflow: hidden;
}

#board-container {
  flex: 1; display: flex;
  align-items: center; justify-content: center;
  gap: 20px; padding: 16px; overflow: hidden;
}

@media (max-width: 767px) {
  /* 모바일: 미리보기 → 보드 세로 스택. 가로로 16px 여백을 두어 보드가 화면 가장자리에 붙지 않게 */
  #board-container {
    flex-direction: column;
    justify-content: flex-start;
    padding: 8px 16px;
    gap: 6px;
  }
  #preview-panel { order: -1; flex-shrink: 0; gap: 2px; }
  #preview-panel .preview-label { font-size: 9px; letter-spacing: 0.5px; }
}

/* ─── 퍼즐 보드 ─── */
#puzzle-board {
  display: grid;
  background: #ced4da;
  gap: 2px; padding: 2px;
  border-radius: 4px; flex-shrink: 0;
  box-shadow: var(--shadow);
}

/* ─── 보드 셀 ─── */
/* canvas는 자기 영역 + bleed 만큼 옆으로 튀어나오게 절대 배치되고,
   clip-path가 셀 경계까지만 보여줌. merge-* 클래스가 붙으면 clip-path가 해당 면으로
   2px 확장되어 인접 조각의 픽셀(bleed)이 gap을 자연스럽게 메움 → 이미지가 이어진 것처럼 보임. */
.board-cell {
  background: #e9ecef;
  border-radius: 2px;
  position: relative;
  overflow: visible;        /* canvas가 셀 밖으로 살짝 나오도록 */
  cursor: grab;
  transition: opacity 0.15s;
  clip-path: inset(0);      /* 기본: 셀 경계까지만 보임 (bleed 숨김) */
  touch-action: none;       /* 모바일에서 보드 위 터치가 스크롤·줌으로 새지 않게 — pointermove가 안정적으로 발사 */
  /* iOS에서 long-press 시 텍스트 선택/이미지 저장 콜아웃 차단 — 드래그 중 끼어들지 않게 */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
.board-cell:active { cursor: grabbing; }

/* canvas는 셀보다 사방 bleed-disp 만큼 큼 — 핵심 영역(piece)이 셀에 정확히 들어맞도록 음수 오프셋 */
.board-cell canvas {
  position: absolute;
  top:  calc(-1 * var(--bleed-disp, 0px));
  left: calc(-1 * var(--bleed-disp, 0px));
  width:  calc(100% + 2 * var(--bleed-disp, 0px));
  height: calc(100% + 2 * var(--bleed-disp, 0px));
  display: block;
  pointer-events: none;
}

/* 합쳐진 면이면 그쪽 2px gap까지 clip 영역 확장 → 인접 픽셀 노출 */
.board-cell.merge-r:not(.merge-b)          { clip-path: inset(0 -2px 0 0); }
.board-cell.merge-b:not(.merge-r)          { clip-path: inset(0 0 -2px 0); }
.board-cell.merge-r.merge-b.merge-br       { clip-path: inset(0 -2px -2px 0); }
/* L자 형태(우/하 모두 합쳐졌지만 대각은 다른 그룹) — 우하단 코너는 제외 */
.board-cell.merge-r.merge-b:not(.merge-br) {
  clip-path: polygon(
    0 0,
    calc(100% + 2px) 0,
    calc(100% + 2px) 100%,
    100% 100%,
    100% calc(100% + 2px),
    0 calc(100% + 2px)
  );
}

/* 합체 순간 짧은 흰색 페이드 */
.board-cell.just-merged::after {
  content: '';
  position: absolute; inset: 0;
  background: #ffffff;
  pointer-events: none;
  z-index: 3;
  animation: snap-flash 0.22s ease-out forwards;
}
@keyframes snap-flash {
  0%   { opacity: 0.35; }
  100% { opacity: 0; }
}

/* 드래그 hover 표시 — clip-path 안쪽에 안전하게 보이도록 ::before 사용 */
.board-cell.hover-swap::before {
  content: '';
  position: absolute; inset: 0;
  pointer-events: none;
  z-index: 4;
  border: 3px solid var(--swap-hover);
}

/* 모바일: 드래그 시 grab 커서 (시각 피드백) */
@media (pointer: coarse) {
  .board-cell:active { cursor: grabbing; }
}

/* ─── 드래그 고스트 ─── */
.drag-ghost {
  position: fixed; pointer-events: none; z-index: 9999;
  border-radius: 3px;
  border: 2px solid var(--primary);
  box-shadow: 0 6px 20px rgba(0,0,0,0.25);
  opacity: 0.88; overflow: hidden;
  will-change: left, top;
}
.drag-ghost canvas { display: block; width: 100%; height: 100%; }

/* ─── 완성본 미리보기 ─── */
#preview-panel {
  display: flex; flex-direction: column; align-items: center;
  gap: 6px; flex-shrink: 0;
}
.preview-label {
  font-size: 10px; color: var(--muted);
  text-transform: uppercase; letter-spacing: 1px;
}
#preview-canvas {
  border-radius: 4px; border: 1px solid var(--border); display: block;
}

/* ─── 컨페티 ─── */
/* 모든 화면 위에 떠야 하므로 높은 z-index. pointer-events:none이라 클릭은 통과 */
#confetti-canvas {
  position: fixed; inset: 0; pointer-events: none; z-index: 9999;
}

/* ─── 로그인 모달 ─── */
.auth-modal {
  position: fixed; inset: 0;
  z-index: 10000;
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
}
.auth-modal-backdrop {
  position: absolute; inset: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
.auth-modal-card {
  position: relative;
  width: 100%; max-width: 360px;
  background: var(--surface);
  border-radius: 16px;
  padding: 24px 20px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
  display: flex; flex-direction: column;
  gap: 12px;
}
.auth-modal-close {
  position: absolute; top: 10px; right: 10px;
  background: transparent; border: none;
  font-size: 18px; color: var(--muted);
  cursor: pointer; padding: 6px 10px;
  border-radius: 6px;
  touch-action: manipulation;
}
.auth-modal-close:hover { background: var(--bg); color: var(--text); }
.auth-modal-card h2 {
  font-size: 18px; font-weight: 700; color: var(--text);
}
.auth-modal-sub {
  font-size: 13px; color: var(--muted); line-height: 1.5;
}

.auth-step {
  display: flex; flex-direction: column;
  gap: 8px; margin-top: 8px;
}
.auth-step label {
  font-size: 12px; font-weight: 600; color: var(--muted);
}
.auth-step input {
  width: 100%;
  padding: 12px 14px;
  font-size: 16px;   /* iOS auto-zoom 방지 */
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
}
.auth-step input:focus { outline: none; border-color: var(--primary); }
.auth-sent-icon { font-size: 36px; text-align: center; line-height: 1; }
.auth-sent-msg  { font-size: 14px; text-align: center; line-height: 1.5; color: var(--text); }
.auth-sent-sub  { font-size: 11px; text-align: center; line-height: 1.5; color: var(--muted); margin-top: 4px; }
#auth-code-target { color: var(--primary); }
#auth-code-input {
  text-align: center;
  font-size: 22px;
  letter-spacing: 8px;
  font-variant-numeric: tabular-nums;
}
.auth-primary-btn { width: 100%; padding: 14px; font-size: 15px; margin-top: 4px; }
#auth-back-email   { margin-top: 4px; align-self: center; }
.auth-error {
  font-size: 12px; color: #c92a2a; text-align: center;
  background: #ffe3e3; padding: 8px 12px; border-radius: 6px;
}

/* OAuth 섹션 */
.auth-divider {
  display: flex; align-items: center;
  gap: 10px;
  color: var(--muted); font-size: 11px;
  margin: 10px 0 4px;
}
.auth-divider::before, .auth-divider::after {
  content: ''; flex: 1; height: 1px; background: var(--border);
}

.auth-oauth-btn {
  width: 100%;
  padding: 11px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  font-size: 14px; font-weight: 600;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  gap: 10px;
  font-family: inherit;
  touch-action: manipulation;
  transition: background 0.15s, border-color 0.15s;
}
.auth-oauth-btn .auth-oauth-icon {
  display: inline-flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
/* Google — Material 가이드: 흰 배경 + 회색 테두리 */
.auth-google-btn { background: #fff; color: #1f1f1f; border-color: #dadce0; }
.auth-google-btn:hover { background: #f8f9fa; }
/* Apple — Apple HIG: 검정 배경 + 흰 글씨 */
.auth-apple-btn { background: #000; color: #fff; border-color: #000; }
.auth-apple-btn:hover { background: #1a1a1a; }
.auth-apple-btn .auth-oauth-icon { color: #fff; margin-bottom: 1px; }
