/* ═══════════════════════════════════════════════════════════
   메모리 매칭 게임 — Midnight Obsidian 디자인 시스템
   ═══════════════════════════════════════════════════════════ */

/* ── 폰트 임포트 ── */
@import url('https://cdn.jsdelivr.net/gh/orioncactus/pretendard@v1.3.9/dist/web/variable/pretendardvariable.min.css');

/* ── CSS 변수 ── */
:root {
  /* 색상 팔레트 */
  --bg:          #070810;
  --surface:     #0f1023;
  --surface-2:   #161830;
  --surface-3:   #1e2040;
  --surface-glass: rgba(15, 16, 35, 0.7);
  --text:        #e8e8f4;
  --text-muted:  #7878a0;
  --text-dim:    #4a4a72;
  --border:      rgba(255, 255, 255, 0.07);
  --border-subtle: rgba(255, 255, 255, 0.04);

  /* 액센트 */
  --accent:      #7c6aff;
  --accent-2:    #a78bfa;
  --accent-glow: rgba(124, 106, 255, 0.25);
  --gold:        #f5c842;
  --gold-glow:   rgba(245, 200, 66, 0.2);

  /* 플레이어 색상 */
  --p1: #ef4444;
  --p2: #3b82f6;
  --p3: #10b981;
  --p4: #f59e0b;

  /* 그림자 */
  --shadow-xs: 0 1px 2px rgba(0,0,0,.3);
  --shadow-sm: 0 2px 8px rgba(0,0,0,.4), 0 1px 2px rgba(0,0,0,.3);
  --shadow-md: 0 4px 20px rgba(0,0,0,.5), 0 1px 3px rgba(0,0,0,.4);
  --shadow-lg: 0 12px 48px rgba(0,0,0,.6), 0 4px 12px rgba(0,0,0,.4);
  --shadow-xl: 0 24px 72px rgba(0,0,0,.7), 0 8px 24px rgba(0,0,0,.5);

  /* 레이아웃 */
  --radius-xs: 6px;
  --radius-sm: 10px;
  --radius-md: 16px;
  --radius-lg: 24px;
  --radius-xl: 32px;

  /* 이징 */
  --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --ease-out:    cubic-bezier(0.22, 1, 0.36, 1);
  --ease-in-out: cubic-bezier(0.65, 0, 0.35, 1);

  /* 타이포그래피 */
  --font-body: 'Pretendard Variable', 'Pretendard', -apple-system, sans-serif;
  --font-display: 'Fraunces', Georgia, serif;
  --font-mono: 'JetBrains Mono', 'Courier New', monospace;
}

/* ── 라이트 모드 (시스템 설정 따름) ── */
@media (prefers-color-scheme: light) {
  :root {
    --bg:          #f0f0f8;
    --surface:     #ffffff;
    --surface-2:   #f5f5fd;
    --surface-3:   #ebebf8;
    --surface-glass: rgba(255, 255, 255, 0.8);
    --text:        #111128;
    --text-muted:  #5a5a88;
    --text-dim:    #9898b8;
    --border:      rgba(0, 0, 0, 0.08);
    --border-subtle: rgba(0, 0, 0, 0.04);
    --shadow-xs: 0 1px 2px rgba(0,0,0,.06);
    --shadow-sm: 0 2px 8px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
    --shadow-md: 0 4px 20px rgba(0,0,0,.10), 0 1px 3px rgba(0,0,0,.06);
    --shadow-lg: 0 12px 48px rgba(0,0,0,.12), 0 4px 12px rgba(0,0,0,.07);
  }
}

/* ── 리셋 & 베이스 ── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
}

body {
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  min-height: 100vh;
  overflow-x: hidden;
  font-feature-settings: 'ss01', 'kern';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* 전역 스크롤바 */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 99px; }

/* 포커스 링 */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
  border-radius: var(--radius-xs);
}

button { cursor: pointer; border: none; font-family: inherit; }
input, textarea { font-family: inherit; }
img { display: block; max-width: 100%; }

/* ══════════════════════════════════════════════════════════
   화면 전환
══════════════════════════════════════════════════════════ */

.screen {
  position: fixed;
  inset: 0;
  display: none;
  opacity: 0;
}

.screen.active {
  display: flex;
  flex-direction: column;
  animation: screenIn 0.4s var(--ease-out) forwards;
}

@keyframes screenIn {
  from { opacity: 0; transform: translateY(12px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ══════════════════════════════════════════════════════════
   배경 오브
══════════════════════════════════════════════════════════ */

.screen-bg {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 0;
}

.bg-orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.35;
}

.bg-orb-1 {
  width: min(60vw, 500px);
  height: min(60vw, 500px);
  top: -15%;
  left: -10%;
  background: radial-gradient(circle, #3b1fa8 0%, transparent 70%);
  animation: orbFloat 18s ease-in-out infinite alternate;
}

.bg-orb-2 {
  width: min(50vw, 400px);
  height: min(50vw, 400px);
  bottom: -10%;
  right: -5%;
  background: radial-gradient(circle, #1a3a8a 0%, transparent 70%);
  animation: orbFloat 22s ease-in-out infinite alternate-reverse;
}

.bg-orb-3 {
  width: min(40vw, 320px);
  height: min(40vw, 320px);
  top: 40%;
  left: 55%;
  background: radial-gradient(circle, #4a1060 0%, transparent 70%);
  animation: orbFloat 26s ease-in-out infinite alternate;
}

@keyframes orbFloat {
  from { transform: translate(0, 0) scale(1); }
  to   { transform: translate(4%, 6%) scale(1.05); }
}

/* ══════════════════════════════════════════════════════════
   설정 화면
══════════════════════════════════════════════════════════ */

#screen-setup {
  overflow: hidden;
}

.setup-scroll {
  flex: 1;
  overflow-y: auto;
  padding: clamp(1.5rem, 4vw, 3rem) clamp(1rem, 4vw, 2rem)
           calc(clamp(1.5rem, 4vw, 3rem) + env(safe-area-inset-bottom));
  padding-left:  calc(clamp(1rem, 4vw, 2rem) + env(safe-area-inset-left));
  padding-right: calc(clamp(1rem, 4vw, 2rem) + env(safe-area-inset-right));
  position: relative;
  z-index: 1;
}

#setup-container {
  max-width: 900px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: clamp(1.5rem, 3vw, 2rem);
}

/* ── 로딩 ── */
.setup-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  padding: 4rem 0;
  color: var(--text-muted);
}

.loading-spinner {
  width: 36px;
  height: 36px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ── 헤더 ── */
.setup-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding-bottom: 0.5rem;
}

.setup-logo {
  width: 52px;
  height: 52px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  box-shadow: 0 0 24px var(--accent-glow), var(--shadow-sm);
  flex-shrink: 0;
}

.setup-title {
  font-family: var(--font-display);
  font-size: clamp(1.75rem, 4vw, 2.5rem);
  font-weight: 900;
  letter-spacing: -0.03em;
  line-height: 1;
  color: var(--text);
}

.setup-subtitle {
  font-size: 0.875rem;
  color: var(--text-muted);
  margin-top: 0.25rem;
  letter-spacing: -0.01em;
}

/* ── 설정 그리드 ── */
.setup-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: clamp(0.75rem, 2vw, 1rem);
}

@media (min-width: 600px) {
  .setup-grid {
    grid-template-columns: 1fr 1fr;
  }
}

@media (min-width: 900px) {
  .setup-grid {
    grid-template-columns: 1fr 1fr;
  }
}

/* ── 설정 카드 ── */
.setup-card {
  background: var(--surface-glass);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: clamp(1.25rem, 3vw, 1.75rem);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  box-shadow: var(--shadow-md);
  transition: transform 0.2s var(--ease-spring), box-shadow 0.2s;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.setup-card:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-lg);
}

.setup-card.disabled {
  opacity: 0.5;
  pointer-events: none;
}

.setup-card-header {
  display: flex;
  align-items: center;
  gap: 0.625rem;
}

.setup-card-header h2 {
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--text);
}

.setup-card-icon {
  width: 18px;
  height: 18px;
  color: var(--accent-2);
  flex-shrink: 0;
}

/* ── 플레이어 수 버튼 ── */
.player-count-row {
  display: flex;
  gap: 0.5rem;
}

.count-btn {
  flex: 1;
  padding: 0.625rem 0;
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 0.9rem;
  font-weight: 600;
  transition: all 0.18s var(--ease-spring);
}

.count-btn:hover {
  background: var(--surface-2);
  color: var(--text);
  transform: translateY(-1px);
}

.count-btn.active {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border-color: transparent;
  color: white;
  box-shadow: 0 4px 16px var(--accent-glow);
  transform: translateY(-1px);
}

/* ── 플레이어 입력 ── */
#player-inputs {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.player-input-row {
  display: flex;
  align-items: center;
  gap: 0.625rem;
}

.player-input-row.hidden { display: none; }

.player-color-dot {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  flex-shrink: 0;
  cursor: pointer;
  transition: transform 0.18s var(--ease-spring), box-shadow 0.18s;
  box-shadow: 0 0 0 2px transparent;
}

.player-color-dot:hover {
  transform: scale(1.2);
  box-shadow: 0 0 0 3px var(--border);
}

.player-name-input {
  flex: 1;
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.75rem;
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 500;
  transition: border-color 0.15s, box-shadow 0.15s;
  min-width: 0;
}

.player-name-input::placeholder { color: var(--text-dim); }

.player-name-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

/* ── 타일 옵션 ── */
.tile-options {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.tile-btn {
  flex: 1;
  min-width: 4rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
  padding: 0.625rem 0.5rem;
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  transition: all 0.18s var(--ease-spring);
  cursor: pointer;
}

.tile-btn:hover:not(.disabled) {
  background: var(--surface-2);
  color: var(--text);
  transform: translateY(-1px);
}

.tile-btn.active {
  background: var(--surface-2);
  border-color: var(--accent);
  color: var(--accent-2);
  box-shadow: 0 0 12px var(--accent-glow);
}

.tile-btn.disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.tile-btn-num {
  font-family: var(--font-mono);
  font-size: 1.1rem;
  font-weight: 600;
  line-height: 1;
}

.tile-btn-grid {
  font-size: 0.72rem;
  color: var(--text-dim);
}

.tile-btn.active .tile-btn-grid { color: var(--accent); }

/* ── 모드 탭 ── */
.mode-tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.375rem;
}

@media (max-width: 400px) {
  .mode-tabs { grid-template-columns: repeat(2, 1fr); }
}

.mode-tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
  padding: 0.625rem 0.25rem;
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.18s var(--ease-spring);
  color: var(--text-muted);
}

.mode-tab:hover {
  background: var(--surface-2);
  color: var(--text);
  transform: translateY(-1px);
}

.mode-tab.active {
  background: var(--surface-2);
  border-color: var(--accent);
  color: var(--accent-2);
  box-shadow: 0 0 12px var(--accent-glow);
}

.mode-tab-label {
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  white-space: nowrap;
}

.mode-tab-desc {
  font-size: 0.66rem;
  color: var(--text-dim);
  white-space: nowrap;
}

.mode-tab.active .mode-tab-desc { color: var(--accent); opacity: 0.8; }

.mode-hint {
  font-size: 0.78rem;
  color: var(--text-muted);
  line-height: 1.5;
  padding: 0.5rem 0.75rem;
  background: color-mix(in srgb, var(--accent) 8%, var(--surface-3));
  border-radius: var(--radius-xs);
  border-left: 2px solid var(--accent);
}

/* 텍스트 입력 영역 */
.text-input-area {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  animation: slideDown 0.2s var(--ease-out);
}

.text-input-area.hidden { display: none; }

@keyframes slideDown {
  from { opacity: 0; transform: translateY(-8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.words-textarea {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.625rem 0.75rem;
  color: var(--text);
  font-size: 0.875rem;
  line-height: 1.5;
  resize: vertical;
  min-height: 72px;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.words-textarea::placeholder { color: var(--text-dim); }

.words-textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.text-input-hint {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.25rem;
  font-size: 0.76rem;
}

.word-count { font-family: var(--font-mono); }
.word-count.ok  { color: #10b981; }
.word-count.warn { color: #f59e0b; }
.hint-text { color: var(--text-dim); }

.mode-disabled-msg {
  font-size: 0.82rem;
  color: var(--text-dim);
  font-style: italic;
}

/* ── 카테고리 선택 ── */
.category-selector {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding-top: 0.25rem;
}

.category-selector-label {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--text-muted);
  letter-spacing: 0.03em;
  text-transform: uppercase;
}

.category-btns {
  display: flex;
  flex-wrap: wrap;
  gap: 0.375rem;
}

.category-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.35rem 0.7rem;
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: 99px;
  color: var(--text-muted);
  font-size: 0.78rem;
  font-weight: 600;
  transition: all 0.15s var(--ease-spring);
  white-space: nowrap;
}

.category-btn:hover {
  background: color-mix(in srgb, var(--accent) 12%, var(--surface-2));
  border-color: var(--accent);
  color: var(--accent-2);
  transform: translateY(-1px);
}

.category-count {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  color: var(--text-dim);
  background: var(--surface);
  padding: 0.05rem 0.35rem;
  border-radius: 99px;
}

.category-btn:hover .category-count {
  color: var(--accent);
  background: var(--accent-glow);
}

/* ── 카드 종류 선택 ── */
.section-label {
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--text-muted);
  letter-spacing: 0.03em;
  text-transform: uppercase;
  margin-bottom: 0.5rem;
}

.card-type-tabs {
  display: flex;
  gap: 0.375rem;
  margin-bottom: 0.5rem;
}

.card-type-btn {
  flex: 1;
  padding: 0.5rem 0.25rem;
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 0.82rem;
  font-weight: 600;
  transition: all 0.18s var(--ease-spring);
}

.card-type-btn:hover {
  background: var(--surface-2);
  color: var(--text);
  transform: translateY(-1px);
}

.card-type-btn.active {
  background: var(--surface-2);
  border-color: var(--accent);
  color: var(--accent-2);
  box-shadow: 0 0 10px var(--accent-glow);
}

.card-type-path {
  font-size: 0.72rem;
  font-family: var(--font-mono);
  color: var(--text-dim);
  margin-bottom: 0.25rem;
}

.optional-badge {
  display: inline-block;
  font-size: 0.68rem;
  font-weight: 600;
  background: var(--surface-3);
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 0.1rem 0.45rem;
  border-radius: 99px;
  margin-left: 0.375rem;
  vertical-align: middle;
}

.override-notice {
  font-size: 0.76rem;
  color: #f59e0b;
  padding: 0.35rem 0.625rem;
  background: rgba(245, 158, 11, 0.08);
  border-radius: var(--radius-xs);
  border-left: 2px solid #f59e0b;
}

/* ── 이미지 업로드 ── */
.image-upload-area {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.image-status {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.img-count-badge {
  font-size: 0.8rem;
  font-weight: 600;
  background: var(--accent-glow);
  color: var(--accent-2);
  padding: 0.2rem 0.6rem;
  border-radius: 99px;
  border: 1px solid var(--accent);
}

.img-warn {
  font-size: 0.78rem;
  color: #f59e0b;
}

.img-none {
  font-size: 0.8rem;
  color: var(--text-dim);
}

.image-upload-buttons {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.image-thumbs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.375rem;
}

.image-thumb {
  width: 48px;
  height: 48px;
  border-radius: var(--radius-xs);
  overflow: hidden;
  background: var(--surface-3);
  border: 1px solid var(--border);
}

.image-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.image-thumb.more {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text-muted);
}

/* ── 버튼 공통 ── */
.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.75rem 1.5rem;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: white;
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  transition: all 0.2s var(--ease-spring);
  box-shadow: 0 4px 16px var(--accent-glow);
  white-space: nowrap;
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px var(--accent-glow);
  filter: brightness(1.1);
}

.btn-primary:active { transform: translateY(0); }
.btn-primary:disabled,
.btn-primary.disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.btn-secondary {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.55rem 1rem;
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.875rem;
  font-weight: 600;
  transition: all 0.18s var(--ease-spring);
}

.btn-secondary:hover {
  background: var(--surface-2);
  transform: translateY(-1px);
}

.btn-ghost {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.55rem 1rem;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 0.875rem;
  font-weight: 500;
  transition: all 0.18s;
}

.btn-ghost:hover { color: var(--text); border-color: var(--text-muted); }
.btn-danger { color: #ef4444 !important; }
.btn-danger:hover { border-color: #ef4444 !important; }

/* ── 시작 버튼 (풋터) ── */
.setup-footer {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
  padding-top: 0.5rem;
}

.validation-msg {
  font-size: 0.85rem;
  color: #f59e0b;
  min-height: 1.2em;
  text-align: center;
}

.btn-start {
  display: inline-flex;
  align-items: center;
  gap: 0.75rem;
  padding: clamp(0.875rem, 2vw, 1rem) clamp(2rem, 5vw, 3rem);
  background: linear-gradient(135deg, var(--accent) 0%, #a78bfa 50%, var(--gold) 100%);
  color: white;
  border-radius: var(--radius-lg);
  font-size: clamp(1rem, 2.5vw, 1.125rem);
  font-weight: 700;
  letter-spacing: -0.01em;
  transition: all 0.25s var(--ease-spring);
  box-shadow: 0 4px 24px var(--accent-glow), 0 8px 32px rgba(245, 200, 66, 0.15);
  background-size: 200% 100%;
  animation: gradientShift 4s ease infinite;
  min-width: 200px;
  justify-content: center;
}

.btn-start:hover {
  transform: translateY(-3px) scale(1.02);
  box-shadow: 0 8px 32px var(--accent-glow), 0 12px 40px rgba(245, 200, 66, 0.2);
}

.btn-start:active { transform: translateY(-1px) scale(1); }

.btn-start:disabled,
.btn-start.disabled {
  background: var(--surface-3);
  box-shadow: none;
  animation: none;
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

@keyframes gradientShift {
  0%, 100% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
}

/* ══════════════════════════════════════════════════════════
   게임 화면
══════════════════════════════════════════════════════════ */

#screen-game {
  display: none;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

/* 현재 플레이어 배경 오버레이 */
#turn-bg {
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  transition: background-color 0.8s var(--ease-out);
}

#screen-game.active {
  display: flex;
}

/* ── HUD ── */
#hud {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: clamp(0.375rem, 1.5vw, 0.75rem);
  padding: clamp(0.5rem, 1.5vw, 0.75rem) clamp(0.625rem, 2.5vw, 1.25rem);
  padding-top: calc(clamp(0.5rem, 1.5vw, 0.75rem) + env(safe-area-inset-top));
  background: var(--surface-glass);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  z-index: 10;
  position: relative;
}

/* 가로 모드: HUD를 측면에 */
@media (orientation: landscape) and (max-height: 600px) {
  #screen-game {
    flex-direction: row;
  }
  #hud {
    flex-direction: column;
    width: clamp(100px, 20vw, 180px);
    border-bottom: none;
    border-right: 1px solid var(--border);
    padding: 1rem 0.75rem;
    height: 100%;
    justify-content: space-between;
    align-items: stretch;
  }
  #current-turn-display { order: -1; align-items: flex-start; }
  #players-hud { flex-direction: column; flex: 1; max-width: 100%; }
  #hud-right { flex-direction: column; align-items: flex-start; }
  #timer-area { order: -1; }
}

#players-hud {
  display: flex;
  gap: 0.375rem;
  flex: 0 0 auto;
  min-width: 0;
  overflow: hidden;
}

/* 현재 턴 이름 표시 (HUD 중앙) */
#current-turn-display {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  min-width: 0;
  padding: 0 0.5rem;
  gap: 0.05rem;
}

.turn-label {
  font-size: 0.58rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--player-color);
  opacity: 0.8;
  line-height: 1;
}

.turn-name {
  font-family: var(--font-display);
  font-size: clamp(1rem, 4vw, 1.65rem);
  font-weight: 900;
  letter-spacing: -0.025em;
  color: var(--player-color);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
  line-height: 1.1;
  text-shadow: 0 0 24px color-mix(in srgb, var(--player-color) 55%, transparent);
  animation: turnNameIn 0.35s var(--ease-spring);
}

@keyframes turnNameIn {
  from { opacity: 0.2; transform: scale(0.85) translateY(-4px); }
  to   { opacity: 1;   transform: scale(1)    translateY(0); }
}

/* HUD 우측 (타이머 + 컨트롤) */
#hud-right {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  flex-shrink: 0;
}

.player-chip {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.375rem 0.625rem;
  border-radius: var(--radius-sm);
  background: var(--surface-3);
  border: 2px solid transparent;
  transition: all 0.25s var(--ease-spring);
  min-width: 0;
  flex-shrink: 1;
  position: relative;
  overflow: hidden;
}

.player-chip.active {
  background: color-mix(in srgb, var(--player-color) 15%, var(--surface-2));
  border-color: var(--player-color);
  box-shadow: 0 0 16px color-mix(in srgb, var(--player-color) 30%, transparent);
}

.player-chip-color {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--player-color);
  flex-shrink: 0;
  transition: transform 0.2s var(--ease-spring);
}

.player-chip.active .player-chip-color {
  transform: scale(1.3);
}

.player-chip-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
  line-height: 1.2;
}

.player-chip-name {
  font-size: clamp(0.65rem, 1.5vw, 0.8rem);
  font-weight: 600;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 8ch;
}

.player-chip.active .player-chip-name { color: var(--text); }

.player-chip-score {
  font-family: var(--font-mono);
  font-size: clamp(0.8rem, 2vw, 1rem);
  font-weight: 600;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.player-chip-indicator {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: var(--player-color);
  animation: indicatorPulse 1.5s ease-in-out infinite;
}

@keyframes indicatorPulse {
  0%, 100% { opacity: 0.7; }
  50% { opacity: 1; }
}

#timer-area {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  color: var(--text-muted);
  white-space: nowrap;
  flex-shrink: 0;
}

#timer-display {
  font-family: var(--font-mono);
  font-size: clamp(0.9rem, 2.5vw, 1.1rem);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  min-width: 4ch;
}

#game-controls {
  display: flex;
  gap: 0.375rem;
  flex-shrink: 0;
}

.icon-btn {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  transition: all 0.18s var(--ease-spring);
  flex-shrink: 0;
}

.icon-btn:hover { color: var(--text); background: var(--surface-2); transform: scale(1.05); }
.icon-btn.muted { color: var(--text-dim); }

/* ── 게임 보드 ── */
#game-board-wrap {
  flex: 1;
  min-height: 0;       /* flex child가 줄어들 수 있도록 */
  overflow: hidden;    /* 카드가 뷰포트 밖으로 나가지 않도록 */
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(0.5rem, 1.5vw, 1rem);
  padding-bottom: calc(clamp(0.5rem, 1.5vw, 1rem) + env(safe-area-inset-bottom));
  position: relative;
  z-index: 1;
}

#game-board {
  /* columns, gap, card-w는 JS의 _fitCards()가 직접 설정 */
  display: grid;
  place-content: center;
}

/* ── 게임 카드 ── */
.game-card {
  /* 크기는 JS의 _fitCards()가 --card-w로 설정 */
  width: var(--card-w, 72px);
  aspect-ratio: 3 / 4;
  cursor: pointer;
  perspective: 1000px;
  -webkit-perspective: 1000px;
  will-change: transform;
  min-width: 0;
  flex-shrink: 0;
}

.game-card:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
  border-radius: var(--radius-md);
}

.card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  -webkit-transform-style: preserve-3d;
  transition: transform 0.42s cubic-bezier(0.34, 1.56, 0.64, 1);
  will-change: transform;
  border-radius: var(--radius-md);
}

.game-card.flipped .card-inner,
.game-card.matched .card-inner {
  transform: rotateY(180deg);
}

.card-back,
.card-front {
  position: absolute;
  inset: 0;
  border-radius: var(--radius-md);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
}

/* 카드 뒷면 — CSS-only 다이아몬드 패턴 */
.card-back {
  background-color: #111230;
  background-image:
    radial-gradient(ellipse at 50% 30%, rgba(124, 106, 255, 0.18) 0%, transparent 60%),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='40' height='40'%3E%3Cpath d='M20 2 L38 20 L20 38 L2 20 Z' fill='none' stroke='rgba(124%2C106%2C255%2C0.25)' stroke-width='1'/%3E%3Ccircle cx='20' cy='20' r='2' fill='rgba(124%2C106%2C255%2C0.3)'/%3E%3C/svg%3E"),
    linear-gradient(135deg, #13153a 0%, #0a0c22 50%, #13153a 100%);
  box-shadow: var(--shadow-sm), inset 0 1px 0 rgba(255,255,255,0.06);
  border: 1px solid rgba(124, 106, 255, 0.15);
  transition: box-shadow 0.2s;
}

.game-card:hover:not(.flipped):not(.matched) .card-back {
  box-shadow: var(--shadow-md), 0 0 20px var(--accent-glow);
  border-color: rgba(124, 106, 255, 0.35);
}

.card-back-pattern {
  position: absolute;
  inset: 0;
  background:
    repeating-linear-gradient(
      45deg,
      transparent,
      transparent 14px,
      rgba(124, 106, 255, 0.04) 14px,
      rgba(124, 106, 255, 0.04) 15px
    ),
    repeating-linear-gradient(
      -45deg,
      transparent,
      transparent 14px,
      rgba(124, 106, 255, 0.04) 14px,
      rgba(124, 106, 255, 0.04) 15px
    );
}

.card-back-logo {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent-2);
}

/* 카드 앞면 */
.card-front {
  transform: rotateY(180deg);
  background: var(--surface-2);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(4px, 1.5vw, 12px);
  box-shadow: var(--shadow-sm);
}

/* 선택 색상 테두리 */
.game-card.flipped:not(.matched) .card-front {
  border: 2px solid var(--select-color, var(--accent));
  box-shadow: 0 0 16px color-mix(in srgb, var(--select-color, var(--accent)) 40%, transparent);
}

/* 매칭된 카드 */
.game-card.matched .card-front {
  border: 2px solid var(--match-color, var(--gold));
  background: color-mix(in srgb, var(--match-color, var(--gold)) 8%, var(--surface-2));
  box-shadow: 0 0 20px color-mix(in srgb, var(--match-color, var(--gold)) 30%, transparent);
}

/* 매치 플래시 */
.match-flash {
  animation: matchFlash 0.5s var(--ease-out);
}

@keyframes matchFlash {
  0%   { filter: brightness(2.5); }
  100% { filter: brightness(1); }
}

/* 흔들림 애니메이션 */
.card-shake .card-inner {
  animation: cardShake 0.45s cubic-bezier(0.36, 0.07, 0.19, 0.97);
}

@keyframes cardShake {
  0%,100% { transform: rotateY(180deg) translateX(0); }
  20%     { transform: rotateY(180deg) translateX(-8px); }
  40%     { transform: rotateY(180deg) translateX(8px); }
  60%     { transform: rotateY(180deg) translateX(-5px); }
  80%     { transform: rotateY(180deg) translateX(5px); }
}

/* 카드 콘텐츠 */
.card-image {
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: calc(var(--radius-md) - 4px);
}

.card-number {
  font-family: var(--font-mono);
  font-size: clamp(1.2rem, 4vw, 2.5rem);
  font-weight: 600;
  color: var(--accent-2);
  font-variant-numeric: tabular-nums;
  text-shadow: 0 0 20px var(--accent-glow);
}

.card-text {
  font-family: var(--font-body);
  font-size: clamp(0.65rem, 3vw, 1.5rem);
  font-weight: 700;
  color: var(--text);
  text-align: center;
  word-break: break-word;
  hyphens: auto;
  line-height: 1.2;
  padding: 0.25em;
}

/* 이미지 없을 때 fallback */
.card-no-img {
  background: linear-gradient(135deg, var(--surface-3), var(--surface-2));
}

/* 스켈레톤 shimmer */
.card-image[src=""] {
  background: linear-gradient(90deg, var(--surface-3) 25%, var(--surface-2) 50%, var(--surface-3) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
}

@keyframes shimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ══════════════════════════════════════════════════════════
   일시정지 오버레이
══════════════════════════════════════════════════════════ */

#pause-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
}

#pause-overlay[hidden] { display: none; }

.pause-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(7, 8, 16, 0.7);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.pause-panel {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: clamp(2rem, 5vw, 3rem);
  text-align: center;
  box-shadow: var(--shadow-xl);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.25rem;
  min-width: 240px;
}

.pause-icon {
  width: 56px;
  height: 56px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
}

.pause-panel h2 {
  font-family: var(--font-display);
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

/* ══════════════════════════════════════════════════════════
   승리 화면
══════════════════════════════════════════════════════════ */

#screen-win {
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

#confetti-canvas {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
}

.win-container {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: min(480px, calc(100vw - 2rem));
  padding: 1rem;
}

.win-panel {
  background: var(--surface-glass);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: clamp(2rem, 6vw, 3rem);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  box-shadow: var(--shadow-xl), 0 0 60px var(--accent-glow);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
  animation: winPanelIn 0.5s var(--ease-spring);
}

@keyframes winPanelIn {
  from { opacity: 0; transform: scale(0.85) translateY(20px); }
  to   { opacity: 1; transform: scale(1) translateY(0); }
}

.win-title-area {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}

.win-icon { font-size: 3rem; line-height: 1; }

.win-headline {
  font-family: var(--font-display);
  font-size: clamp(1.75rem, 5vw, 2.5rem);
  font-weight: 900;
  letter-spacing: -0.03em;
  line-height: 1.1;
  color: var(--text);
}

.win-badge {
  font-size: 0.85rem;
  font-weight: 700;
  background: linear-gradient(135deg, var(--gold), #ffa040);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: -0.01em;
}

.win-results-area { width: 100%; }

/* 1인 결과 */
.solo-results {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.result-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.625rem 0;
  border-bottom: 1px solid var(--border-subtle);
  font-size: 0.9rem;
  color: var(--text-muted);
}

.result-row:last-child { border-bottom: none; }
.result-val {
  font-family: var(--font-mono);
  font-weight: 600;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}
.result-row.muted { opacity: 0.6; }

/* 다인 랭킹 */
.rank-table {
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
}

.rank-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.625rem 0.875rem;
  background: var(--surface-3);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  transition: background 0.15s;
}

.rank-row.winner {
  background: color-mix(in srgb, var(--gold) 10%, var(--surface-3));
  border-color: color-mix(in srgb, var(--gold) 30%, transparent);
}

.rank-num {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-dim);
  width: 1.5ch;
}

.rank-row.winner .rank-num { color: var(--gold); }

.rank-color {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.rank-name {
  flex: 1;
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text);
}

.rank-score {
  font-family: var(--font-mono);
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.rank-row.winner .rank-score { color: var(--gold); }

.rank-footer {
  padding-top: 0.375rem;
  font-size: 0.78rem;
  color: var(--text-dim);
  text-align: center;
  font-family: var(--font-mono);
}

.win-actions {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
  flex-wrap: wrap;
  width: 100%;
}

.win-actions .btn-primary { flex: 1; min-width: 130px; }
.win-actions .btn-ghost   { flex: 1; min-width: 130px; justify-content: center; }

/* ══════════════════════════════════════════════════════════
   반응형
══════════════════════════════════════════════════════════ */

/* 작은 폰 세로 */
@media (max-width: 360px) {
  .player-chip-name { max-width: 6ch; }
  .tile-options { flex-wrap: wrap; }
  .tile-btn { min-width: 3.5rem; }
}

/* 데스크탑 */
@media (min-width: 1024px) {
  #game-board {
    gap: clamp(8px, 1vw, 14px);
  }

  .game-card {
    min-height: auto;
  }
}

/* 와이드 */
@media (min-width: 1440px) {
  #game-board {
    max-width: min(
      calc(var(--cols, 4) * (90px + 14px)),
      calc(100vw - 4rem)
    );
  }
}

/* 고해상도 DPR */
@media (-webkit-min-device-pixel-ratio: 2) {
  .card-back {
    /* 고해상도에서 패턴 더 선명하게 */
    background-size: auto, 40px 40px, auto;
  }
}

/* 모션 감소 선호 */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.1ms !important;
  }
}

/* 다크 모드 강제 (data-theme 속성으로 오버라이드 가능) */
[data-theme="light"] {
  --bg:          #f0f0f8;
  --surface:     #ffffff;
  --surface-2:   #f5f5fd;
  --surface-3:   #ebebf8;
  --surface-glass: rgba(255, 255, 255, 0.85);
  --text:        #111128;
  --text-muted:  #5a5a88;
  --text-dim:    #9898b8;
  --border:      rgba(0, 0, 0, 0.08);
  --border-subtle: rgba(0, 0, 0, 0.04);
  --shadow-sm: 0 2px 8px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
  --shadow-md: 0 4px 20px rgba(0,0,0,.10);
  --shadow-lg: 0 12px 48px rgba(0,0,0,.12);
  --shadow-xl: 0 24px 72px rgba(0,0,0,.14);
}

[data-theme="light"] .card-back {
  background-color: #dde0f8;
  background-image:
    radial-gradient(ellipse at 50% 30%, rgba(124, 106, 255, 0.12) 0%, transparent 60%),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='40' height='40'%3E%3Cpath d='M20 2 L38 20 L20 38 L2 20 Z' fill='none' stroke='rgba(80%2C60%2C200%2C0.2)' stroke-width='1'/%3E%3Ccircle cx='20' cy='20' r='2' fill='rgba(80%2C60%2C200%2C0.25)'/%3E%3C/svg%3E"),
    linear-gradient(135deg, #c8cdf0 0%, #d8dcf8 50%, #c8cdf0 100%);
  border-color: rgba(100, 80, 220, 0.2);
}
