@import url('https://fonts.googleapis.com/css2?family=Lexend:wght@300;400;600;700;900&display=swap');

/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: 'Lexend', sans-serif; background-color: #f8fafc; touch-action: manipulation; -webkit-tap-highlight-color: transparent; min-height: 100vh; }

/* ===== Layout ===== */
.app-container { min-height: 100vh; background-color: #f8fafc; display: flex; flex-direction: column; align-items: center; padding: 1rem; user-select: none; }

/* ===== Header ===== */
.header { width: 100%; max-width: 56rem; display: flex; flex-direction: column; justify-content: space-between; align-items: center; margin-bottom: 1rem; gap: 0.75rem; }
.header-title h1 { font-size: 1.5rem; font-weight: 900; color: #4f46e5; letter-spacing: -0.025em; display: flex; align-items: center; gap: 0.5rem; }
.level-badge { font-size: 0.75rem; background-color: #e0e7ff; color: #4f46e5; padding: 0.25rem 0.75rem; border-radius: 9999px; text-transform: uppercase; }

/* ===== Item Toolbar ===== */
.item-toolbar { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 0.5rem; background: white; padding: 0.5rem; border-radius: 1rem; box-shadow: 0 1px 2px rgba(0,0,0,0.05); border: 1px solid #e2e8f0; }
.item-btn { display: flex; align-items: center; gap: 0.25rem; padding: 0.4rem 0.6rem; font-size: 0.75rem; border-radius: 0.75rem; border: none; cursor: pointer; font-family: 'Lexend', sans-serif; font-weight: 700; transition: all 0.2s; }
.item-btn:disabled { cursor: not-allowed; }
.item-count { font-weight: 900; margin-left: 0.2rem; }
.item-plus { font-size: 0.6rem; background-color: #3b82f6; color: white; padding: 0.125rem 0.25rem; border-radius: 0.25rem; margin-left: 0.2rem; }
.item-btn-peek { background-color: #fffbeb; color: #b45309; }
.item-btn-peek:hover:not(:disabled) { background-color: #fef3c7; }
.item-btn-peek:disabled { background-color: #f8fafc; color: #cbd5e1; }
.item-btn-time { background-color: #eff6ff; color: #1d4ed8; }
.item-btn-time:hover:not(:disabled) { background-color: #dbeafe; }
.item-btn-time:disabled { background-color: #f8fafc; color: #cbd5e1; opacity: 0.8; }
.item-btn-time:disabled .item-plus { background-color: #cbd5e1; }
.item-btn-lucky { background-color: #f0fdf4; color: #15803d; }
.item-btn-lucky:hover:not(:disabled) { background-color: #dcfce7; }
.item-btn-lucky .lucky-text { font-size: 0.7rem; font-weight: 700; }

/* ===== Main Game Card ===== */
.game-card { width: 100%; max-width: 56rem; background: white; border-radius: 1rem; box-shadow: 0 10px 40px -10px rgba(79, 70, 229, 0.15); padding: 0.75rem; border: 1px solid #eef2ff; position: relative; overflow: hidden; display: flex; flex-direction: column; }

/* ===== Stats Bar ===== */
.stats-bar { display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 0.5rem; padding: 0 0.25rem; gap: 0.5rem; }
.stat-group { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.stat-group.center { align-items: center; }
.stat-group.right { align-items: flex-end; }
.stats-label { font-size: 9px; font-weight: 700; color: #94a3b8; text-transform: uppercase; letter-spacing: 0.05em; }
.timer-container { display: flex; align-items: center; gap: 0.25rem; margin-top: 0.25rem; font-size: 1rem; font-weight: 900; color: #334155; padding: 0.2rem 0.5rem; background-color: #f1f5f9; border-radius: 0.5rem; transition: all 0.3s; min-width: 4rem; justify-content: center; }
.timer-container.urgent { background-color: #fee2e2; color: #ef4444; animation: pulse 1s infinite; }
.score-container { display: flex; align-items: center; gap: 0.25rem; margin-top: 0.25rem; }
.score-value { font-size: 1.25rem; font-weight: 900; color: #f59e0b; }
.combo-badge { font-size: 0.55rem; font-weight: 900; background-color: #ef4444; color: white; padding: 0.15rem 0.3rem; border-radius: 0.5rem; animation: bounce 0.5s infinite alternate; white-space: nowrap; }
.pairs-value { font-size: 1rem; font-weight: 900; color: #4f46e5; margin-top: 0.25rem; }
.pairs-separator { color: #cbd5e1; font-weight: 400; }

/* ===== Feedback ===== */
.feedback-container { text-align: center; min-height: 1.5rem; margin-bottom: 0.5rem; }
.feedback-text { font-size: 0.9rem; font-weight: 700; color: #334155; transition: all 0.2s; display: inline-block; }
.feedback-text.danger { color: #ef4444; animation: pulse 1s infinite; }
.countdown-text { display: flex; align-items: center; gap: 0.25rem; }

/* ===== Grid Area ===== */
.grid-area { flex-grow: 1; display: flex; align-items: center; justify-content: center; padding: 0.25rem; border-radius: 1rem; }

/* ===== Start Screen ===== */
.start-screen { display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 1rem 0; gap: 1rem; }
.start-emoji { font-size: 2rem; line-height: 1; transition: transform 0.3s; }
.start-emoji:hover { transform: scale(1.1); }
.start-btn { padding: 0.8rem 1.5rem; background: linear-gradient(135deg, #4f46e5, #7c3aed); color: white; border: none; border-radius: 1rem; font-size: 1.1rem; font-weight: 900; font-family: 'Lexend', sans-serif; box-shadow: 0 10px 20px -5px rgba(79, 70, 229, 0.4); cursor: pointer; }
.rank-btn { padding: 0.6rem 1.2rem; background: #f1f5f9; color: #475569; border: none; border-radius: 0.75rem; font-weight: 700; font-family: 'Lexend', sans-serif; cursor: pointer; font-size: 0.9rem; transition: background 0.2s; }
.rank-btn:hover { background: #e2e8f0; }
.start-features { display: flex; flex-direction: column; align-items: center; gap: 0.5rem; color: #94a3b8; font-size: 0.75rem; font-weight: 500; }

/* ===== Tile Grid ===== */
.tile-grid { display: grid; gap: 0.25rem; width: 100%; height: 100%; max-height: 60vh; }
.tile { position: relative; width: 100%; aspect-ratio: 1 / 1; perspective: 1000px; -webkit-perspective: 1000px; cursor: pointer; }
.tile.disabled { pointer-events: none; }
.flip-card-inner { position: relative; width: 100%; height: 100%; text-align: center; transition: transform 0.6s cubic-bezier(0.4, 0, 0.2, 1); transform-style: preserve-3d; -webkit-transform-style: preserve-3d; }
.flip-card-inner.is-flipped { transform: rotateY(180deg); -webkit-transform: rotateY(180deg); }
.flip-card-front, .flip-card-back { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; -webkit-backface-visibility: hidden; border-radius: 0.5rem; display: flex; align-items: center; justify-content: center; }
.flip-card-front { background: linear-gradient(135deg, #6366f1, #9333ea); box-shadow: 0 2px 8px rgba(99, 102, 241, 0.3); border: 1px solid rgba(255,255,255,0.2); }
.flip-card-front .question-mark { color: white; opacity: 0.2; font-size: 1.5rem; font-weight: 700; }
.flip-card-back { transform: rotateY(180deg); -webkit-transform: rotateY(180deg); background: white; box-shadow: 0 2px 4px rgba(0,0,0,0.08); border: 2px solid #e0e7ff; }
.flip-card-back.matched { background-color: #f0fdf4; border-color: #bbf7d0; }
.tile-emoji { font-size: 1.75rem; filter: drop-shadow(0 1px 1px rgba(0,0,0,0.05)); transition: transform 0.3s; }

/* ===== Overlay (Win/Lose) ===== */
.overlay { position: absolute; inset: 0; z-index: 30; display: flex; flex-direction: column; align-items: center; justify-content: center; background: rgba(255,255,255,0.95); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); animation: fadeZoomIn 0.5s ease-out; padding: 1rem; box-sizing: border-box; overflow-y: auto; }
.overlay-emoji { font-size: 3.5rem; margin-bottom: 0.5rem; animation: bounce 1s infinite; }
.overlay-title { font-size: 1.75rem; font-weight: 900; font-style: italic; margin-bottom: 0.25rem; }
.overlay-title.won { color: #16a34a; }
.overlay-title.lost { color: #dc2626; }
.overlay-feedback { color: #475569; text-align: center; width: 100%; margin-bottom: 1rem; font-weight: 700; font-size: 0.85rem; line-height: 1.3; }

/* 점수 제출 폼 */
.score-submit-area { display: flex; flex-direction: column; gap: 0.5rem; width: 100%; max-width: 14rem; margin-bottom: 1rem; padding: 0.75rem; background: #eff6ff; border-radius: 1rem; border: 1px solid #bfdbfe; }
.name-input { padding: 0.6rem; border-radius: 0.5rem; border: 1px solid #93c5fd; font-family: 'Lexend', sans-serif; font-size: 0.9rem; text-align: center; font-weight: 600; color: #1e3a8a; outline: none; }
.name-input:focus { border-color: #3b82f6; box-shadow: 0 0 0 2px rgba(59,130,246,0.2); }
.submit-btn { padding: 0.6rem; background: #3b82f6; color: white; border: none; border-radius: 0.5rem; font-family: 'Lexend', sans-serif; font-weight: 700; cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 0.4rem; }
.submit-btn:disabled { background: #94a3b8; cursor: not-allowed; }
.score-success { font-size: 0.9rem; font-weight: 700; color: #15803d; margin-bottom: 1rem; display: flex; align-items: center; gap: 0.4rem; }

.overlay-buttons { display: flex; flex-direction: column; gap: 0.5rem; width: 100%; max-width: 14rem; }
.btn-next, .btn-restart { padding: 0.75rem 1rem; border: none; border-radius: 0.75rem; font-size: 0.9rem; font-weight: 900; font-family: 'Lexend', sans-serif; cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 0.5rem; width: 100%; }
.btn-next { background-color: #4f46e5; color: white; box-shadow: 0 4px 14px rgba(79, 70, 229, 0.3); }
.btn-restart.secondary { background-color: #f1f5f9; color: #475569; }
.btn-restart.primary { background-color: #4f46e5; color: white; }

/* ===== Leaderboard Modal ===== */
.leaderboard-modal { position: absolute; inset: 0; z-index: 40; background: rgba(0,0,0,0.5); display: flex; justify-content: center; align-items: center; padding: 1rem; animation: fadeZoomIn 0.3s ease-out; }
.leaderboard-content { background: white; width: 100%; max-width: 24rem; border-radius: 1.5rem; padding: 1.5rem; box-shadow: 0 25px 50px -12px rgba(0,0,0,0.25); position: relative; max-height: 90vh; display: flex; flex-direction: column; }
.close-btn { position: absolute; top: 1rem; right: 1rem; background: #f1f5f9; border: none; width: 2rem; height: 2rem; border-radius: 50%; display: flex; align-items: center; justify-content: center; cursor: pointer; color: #64748b; transition: 0.2s; }
.close-btn:hover { background: #e2e8f0; color: #0f172a; }
.leaderboard-content h2 { text-align: center; color: #1e293b; font-size: 1.25rem; margin-bottom: 1rem; font-weight: 900; display: flex; align-items: center; justify-content: center; gap: 0.5rem; }
.leaderboard-list { flex-grow: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 0.4rem; padding-right: 0.5rem; }
.leaderboard-list::-webkit-scrollbar { width: 4px; }
.leaderboard-list::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }
.empty-rank { text-align: center; color: #94a3b8; font-size: 0.9rem; padding: 2rem 0; }
.rank-item { display: flex; align-items: center; padding: 0.4rem 0.75rem; background: #f8fafc; border-radius: 0.65rem; border: 1px solid #e2e8f0; }
.rank-num { font-weight: 900; font-size: 1rem; color: #94a3b8; width: 1.5rem; }
.rank-num.top-1 { color: #eab308; font-size: 1.25rem; }
.rank-num.top-2 { color: #94a3b8; font-size: 1.15rem; }
.rank-num.top-3 { color: #b45309; font-size: 1.1rem; }
.rank-name { flex-grow: 1; font-weight: 700; color: #334155; margin-left: 0.5rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-score { font-weight: 900; color: #4f46e5; text-align: right; }
.rank-score small { display: block; font-size: 0.65rem; color: #64748b; font-weight: 600; }

/* ===== Tip ===== */
.tip-bar { margin-top: 1rem; color: #94a3b8; font-size: 0.7rem; text-align: center; font-weight: 500; max-width: 28rem; background: rgba(255,255,255,0.5); padding: 0.5rem 1rem; border-radius: 9999px; border: 1px solid #e2e8f0; }
.tip-label { color: #6366f1; text-transform: uppercase; font-weight: 900; margin-right: 0.25rem; }
.tip-highlight { color: #2563eb; }

/* ===== Animations ===== */
@keyframes pulse { 0%, 100% { opacity: 1; transform: scale(1); } 50% { opacity: 0.8; transform: scale(1.05); } }
@keyframes bounce { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-3px); } }
@keyframes fadeZoomIn { from { opacity: 0; transform: scale(0.9); } to { opacity: 1; transform: scale(1); } }
@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }
.fa-spin { animation: spin 1s linear infinite; }

/* ===== Desktop & Tablet ===== */
@media (min-width: 768px) {
  .app-container { padding: 2rem; }
  .game-card { padding: 2rem; border-radius: 1.5rem; }
  .header { flex-direction: row; margin-bottom: 1.5rem; }
  .header-title h1 { font-size: 1.875rem; }
  
  .item-btn { padding: 0.5rem 1rem; font-size: 0.875rem; }
  .item-plus { font-size: 0.65rem; margin-left: 0.25rem; }
  
  .stats-bar { margin-bottom: 1rem; padding: 0 0.5rem; gap: 0; }
  .stats-label { font-size: 10px; }
  .timer-container { font-size: 1.25rem; min-width: 5rem; }
  .score-value { font-size: 1.75rem; }
  .combo-badge { font-size: 0.65rem; padding: 0.2rem 0.4rem; }
  .pairs-value { font-size: 1.25rem; }
  
  .feedback-container { min-height: 1.75rem; margin-bottom: 1rem; }
  .feedback-text { font-size: 1.125rem; }
  
  .start-emoji { font-size: 3rem; }
  .start-features { flex-direction: row; gap: 1rem; font-size: 0.875rem; }
  .start-btn { padding: 1.25rem 4rem; font-size: 1.5rem; }
  .rank-btn { font-size: 1rem; padding: 0.8rem 1.5rem; }
  
  .tile-grid { gap: 0.5rem; max-height: 70vh; }
  .flip-card-front, .flip-card-back { border-radius: 1rem; }
  .flip-card-front .question-mark { font-size: 2.5rem; }
  .tile-emoji { font-size: 3.5rem; }
  
  .overlay-emoji { font-size: 8rem; margin-bottom: 1rem; }
  .overlay-title { font-size: 3.5rem; margin-bottom: 0.75rem; }
  .overlay-feedback { font-size: 1.125rem; margin-bottom: 2.5rem; max-width: 24rem; line-height: 1.4; }
  
  .score-submit-area { max-width: 32rem; flex-direction: row; padding: 1rem; }
  .name-input { font-size: 1rem; padding: 0.8rem; flex-grow: 1; }
  .submit-btn { font-size: 1rem; padding: 0.8rem 1.5rem; }
  
  /* [수정됨] PC 모드에서 버튼들 나란히 중앙 정렬 유지 */
  .overlay-buttons { flex-direction: row; gap: 1rem; max-width: none; justify-content: center; align-items: center; }
  .btn-next, .btn-restart { padding: 1.25rem 3rem; font-size: 1.25rem; border-radius: 1rem; width: auto; }

  .leaderboard-content { max-width: 28rem; padding: 2rem; }
  .leaderboard-content h2 { font-size: 1.5rem; margin-bottom: 1.5rem; }
  .rank-item { padding: 1rem; font-size: 1.1rem; }
  .rank-score { font-size: 1.2rem; }
  
  .tip-bar { margin-top: 1.5rem; font-size: 0.75rem; padding: 0.75rem 1.5rem; }
}