/* === Reset & Base === */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg: #f5f0e8;
    --bg-card: #ffffff;
    --grid-border: #c0b8a8;
    --cell-border: #d8d0c4;
    --number-bg: #1a1a1a;
    --number-color: #ffffff;
    --path-color: #6c63ff;
    --path-light: #e8e6ff;
    --active-color: #b8b2ff;
    --text-primary: #1a1a1a;
    --text-secondary: #666;
    --btn-primary: #1a1a1a;
    --btn-secondary: #e8e4dc;
    --success: #4caf50;
    --grid-size: min(calc(100vw - 32px), 500px);
    --cell-size: calc(var(--grid-size) / 7);
}

html, body {
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text-primary);
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    user-select: none;
}

/* === Screens === */
.screen {
    display: none;
    width: 100%;
    height: 100%;
}
.screen.active {
    display: flex;
    justify-content: center;
    align-items: center;
}

/* === Menu Screen === */
#screen-menu.screen.active {
    overflow-y: auto;
}

.menu-container {
    text-align: center;
    padding: 20px;
    width: 100%;
    max-width: 400px;
}

.game-title {
    font-size: 34px;
    font-weight: 800;
    line-height: 1.1;
    margin-bottom: 12px;
    letter-spacing: -1px;
}

.game-inspired {
    font-size: 12px;
    color: #999;
    margin-bottom: 16px;
    letter-spacing: 0.5px;
}
.game-inspired strong {
    color: #0a66c2;
}

.game-subtitle {
    font-size: 15px;
    color: var(--text-secondary);
    margin-bottom: 24px;
    line-height: 1.5;
}

.difficulty-buttons {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 20px;
}

.btn-difficulty {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 18px 24px;
    background: var(--bg-card);
    border: 2px solid var(--grid-border);
    border-radius: 16px;
    cursor: pointer;
    transition: all 0.15s;
}
.btn-difficulty:active {
    transform: scale(0.97);
    background: var(--btn-secondary);
}

.diff-label {
    font-size: 20px;
    font-weight: 700;
}
.diff-desc {
    font-size: 14px;
    color: var(--text-secondary);
}

/* Hell 난이도 버튼 */
.btn-difficulty.btn-hell {
    border-color: #e53935;
    background: #fff5f5;
}
.btn-difficulty.btn-hell:active {
    background: #ffebee;
}
.btn-difficulty.btn-hell .diff-label {
    color: #e53935;
}

.best-records {
    font-size: 13px;
    color: var(--text-secondary);
    line-height: 1.8;
}

/* === Game Screen === */
#screen-game {
    flex-direction: column;
    align-items: center;
    padding: 12px 16px;
    padding-top: env(safe-area-inset-top, 12px);
    padding-bottom: env(safe-area-inset-bottom, 12px);
    overflow-y: hidden;
}

.game-container {
    width: 100%;
    max-width: 520px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding-bottom: 20px;
}

/* Top Bar */
.top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 4px;
}

.btn-back {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 1.5px solid var(--grid-border);
    background: var(--bg-card);
    cursor: pointer;
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
    display: flex;
    justify-content: center;
    align-items: center;
    flex-shrink: 0;
    transition: all 0.12s;
    font-family: inherit;
}
.btn-back:active {
    transform: scale(0.9);
}

.timer-area {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 18px;
    font-weight: 600;
}
.timer-icon {
    font-size: 16px;
}

.progress-area {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-secondary);
    background: var(--btn-secondary);
    padding: 4px 12px;
    border-radius: 12px;
}

/* Buttons */
.btn {
    border: none;
    cursor: pointer;
    font-family: inherit;
    font-weight: 600;
    transition: all 0.12s;
}
.btn:active {
    transform: scale(0.95);
}

.btn-small {
    padding: 8px 16px;
    font-size: 14px;
    background: var(--bg-card);
    border: 1.5px solid var(--grid-border);
    border-radius: 20px;
}

.btn-action {
    flex: 1;
    padding: 14px 0;
    font-size: 16px;
    border-radius: 12px;
}

.btn-primary {
    background: var(--btn-primary);
    color: #fff;
}
.btn-secondary {
    background: var(--btn-secondary);
    color: var(--text-primary);
}

/* === Audio Toggle === */
.menu-top-actions {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 8px;
}

.btn-audio-toggle {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 1.5px solid var(--grid-border);
    background: var(--bg-card);
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 18px;
    position: relative;
    transition: all 0.12s;
    flex-shrink: 0;
}
.btn-audio-toggle:active {
    transform: scale(0.9);
}

.btn-audio-toggle.muted .audio-icon {
    opacity: 0.35;
}
.btn-audio-toggle.muted::after {
    content: '';
    position: absolute;
    width: 2px;
    height: 60%;
    background: var(--text-secondary);
    transform: rotate(45deg);
    border-radius: 1px;
}

.top-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* === Grid === */
.grid-wrapper {
    position: relative;
    width: var(--grid-size);
    height: var(--grid-size);
    margin: 0 auto;
}

.grid {
    width: 100%;
    height: 100%;
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    grid-template-rows: repeat(7, 1fr);
    border: 2px solid var(--grid-border);
    border-radius: 12px;
    overflow: hidden;
    background: var(--bg-card);
    touch-action: none;
}

/* 게임 화면 전체 터치 동작 차단 (pull-to-refresh 방지) */
#screen-game {
    touch-action: none;
}

.path-svg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 2;
}

/* === Cell === */
.cell {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 0.5px solid var(--cell-border);
    z-index: 1;
}

/* 벽 (Hell 난이도) */
.cell.wall-right  { border-right:  2.5px solid var(--number-bg) !important; }
.cell.wall-bottom { border-bottom: 2.5px solid var(--number-bg) !important; }
.cell.wall-left   { border-left:   2.5px solid var(--number-bg) !important; }
.cell.wall-top    { border-top:    2.5px solid var(--number-bg) !important; }

/* 숫자 마커 */
.cell-number {
    width: 70%;
    height: 70%;
    background: var(--number-bg);
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    color: var(--number-color);
    font-size: calc(var(--cell-size) * 0.35);
    font-weight: 700;
    z-index: 5;
    position: relative;
}

/* 시작점 셀 (숫자 1) - 경로가 비어있을 때 강조 */
.cell.start-hint .cell-number {
    box-shadow: 0 0 0 3px var(--path-color), 0 0 14px rgba(108, 99, 255, 0.5);
    animation: start-pulse 1.5s ease-in-out infinite;
}

@keyframes start-pulse {
    0%, 100% { box-shadow: 0 0 0 3px var(--path-color), 0 0 14px rgba(108, 99, 255, 0.5); }
    50% { box-shadow: 0 0 0 5px var(--path-color), 0 0 22px rgba(108, 99, 255, 0.7); }
}

/* 다음 숫자 셀 강조 */
.cell.next-number .cell-number {
    box-shadow: 0 0 0 2px var(--path-color), 0 0 8px rgba(108, 99, 255, 0.3);
}

/* 경로 채워진 셀 */
.cell.filled {
    background: var(--path-light);
}

.cell.filled .cell-number {
    background: var(--path-color);
}

/* 활성 셀 (현재 위치) */
.cell.active {
    background: var(--active-color);
}

/* 경로 연결선 */
.cell .connector {
    position: absolute;
    background: var(--path-color);
    z-index: 3;
    border-radius: 3px;
}
.cell .connector.top    { width: 14%; height: 52%; top: 0; left: 50%; transform: translateX(-50%); }
.cell .connector.bottom { width: 14%; height: 52%; bottom: 0; left: 50%; transform: translateX(-50%); }
.cell .connector.left   { height: 14%; width: 52%; left: 0; top: 50%; transform: translateY(-50%); }
.cell .connector.right  { height: 14%; width: 52%; right: 0; top: 50%; transform: translateY(-50%); }

.cell .connector-dot {
    position: absolute;
    width: 20%;
    height: 20%;
    background: var(--path-color);
    border-radius: 50%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 4;
}

/* Bottom Bar */
.bottom-bar {
    display: flex;
    gap: 12px;
}

/* How to Play */
.how-to-play {
    background: var(--bg-card);
    border: 1.5px solid var(--grid-border);
    border-radius: 12px;
    overflow: hidden;
}

.how-to-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 18px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
}
.chevron {
    font-size: 12px;
    transition: transform 0.2s;
}
.how-to-play.collapsed .chevron {
    transform: rotate(180deg);
}
.how-to-play.collapsed .how-to-content {
    display: none;
}

.how-to-content {
    display: flex;
    justify-content: space-around;
    padding: 0 18px 18px;
}
.how-to-item {
    text-align: center;
}
.how-to-icon {
    font-size: 24px;
    font-weight: 700;
    margin-bottom: 6px;
    color: var(--path-color);
}
.how-to-text {
    font-size: 13px;
    color: var(--text-secondary);
}

/* === Modal === */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.5);
    z-index: 100;
    justify-content: center;
    align-items: center;
}
.modal.active {
    display: flex;
}

.modal-content {
    background: var(--bg-card);
    border-radius: 20px;
    padding: 36px 28px;
    text-align: center;
    width: 85%;
    max-width: 340px;
    animation: popIn 0.3s ease;
}

@keyframes popIn {
    from { transform: scale(0.8); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

.clear-title {
    font-size: 28px;
    margin-bottom: 12px;
}
.clear-time {
    font-size: 20px;
    font-weight: 600;
    margin-bottom: 4px;
}
.clear-hints {
    font-size: 14px;
    color: var(--text-secondary);
    margin-bottom: 24px;
}
.clear-buttons {
    display: flex;
    gap: 10px;
}
.clear-buttons .btn {
    flex: 1;
    padding: 14px;
    border-radius: 12px;
    font-size: 15px;
}

/* === Combo Badge === */
.combo-badge {
    font-size: 14px;
    font-weight: 700;
    color: var(--path-color);
    background: var(--path-light);
    padding: 4px 12px;
    border-radius: 12px;
    opacity: 0;
    transform: scale(0.8);
    transition: opacity 0.2s, transform 0.2s;
    pointer-events: none;
}
.combo-badge.active {
    opacity: 1;
    transform: scale(1);
}
.combo-badge.pop {
    animation: comboPop 0.25s ease;
}

@keyframes comboPop {
    0% { transform: scale(1); }
    50% { transform: scale(1.2); }
    100% { transform: scale(1); }
}

/* === Combo Level Path Colors === */
/* Level 1 (x2): 약간 진한 보라 */
.grid.combo-level-1 .cell.filled {
    background: #ddd8ff;
}
.grid.combo-level-1 .cell.active {
    background: #a89eff;
}

/* Level 2 (x3): 더 진한 보라 */
.grid.combo-level-2 .cell.filled {
    background: #cdc6ff;
}
.grid.combo-level-2 .cell.active {
    background: #9488ff;
}

/* Level 3 (x4): 보라 + glow */
.grid.combo-level-3 .cell.filled {
    background: #beb5ff;
    box-shadow: inset 0 0 8px rgba(108, 99, 255, 0.3);
}
.grid.combo-level-3 .cell.active {
    background: #8377ff;
    box-shadow: inset 0 0 12px rgba(108, 99, 255, 0.4);
}

/* Level 4 (MAX): 골드 + strong glow */
.grid.combo-level-4 .cell.filled {
    background: #fff3d0;
    box-shadow: inset 0 0 8px rgba(255, 193, 7, 0.3);
}
.grid.combo-level-4 .cell.filled .connector,
.grid.combo-level-4 .cell.filled .connector-dot {
    background: #f0a500;
}
.grid.combo-level-4 .cell.active {
    background: #ffe082;
    box-shadow: inset 0 0 12px rgba(255, 193, 7, 0.5);
}

/* Clear modal score/combo */
.clear-score {
    font-size: 22px;
    font-weight: 700;
    color: var(--path-color);
    margin-bottom: 4px;
}
.clear-combo {
    font-size: 14px;
    color: var(--text-secondary);
    margin-bottom: 4px;
}

/* === Animations === */
@keyframes pulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.15); }
    100% { transform: scale(1); }
}
.cell.reached .cell-number {
    animation: pulse 0.3s ease;
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-4px); }
    75% { transform: translateX(4px); }
}
.grid.shake {
    animation: shake 0.3s ease;
}

/* 클리어 시 셀 순차 하이라이트 */
@keyframes cellClear {
    0% { background: var(--path-light); }
    50% { background: var(--success); }
    100% { background: #c8e6c9; }
}
.cell.clear-anim {
    animation: cellClear 0.4s ease forwards;
}

/* ===========================================
   멀티플레이어 스타일
   =========================================== */

/* --- 멀티플레이 메뉴 버튼 --- */
.menu-divider {
    height: 1px;
    background: var(--cell-border);
    margin: 8px 0;
}

.btn-multiplayer {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 14px;
    border: 2px solid var(--path-color);
    border-radius: 12px;
    background: linear-gradient(135deg, #e8d5f5, #f0e0ff);
    cursor: pointer;
    transition: transform 0.1s, box-shadow 0.1s;
    font-family: inherit;
}
.btn-multiplayer:active {
    transform: scale(0.97);
}
.btn-multiplayer .diff-label {
    font-weight: 700;
    font-size: 18px;
    color: var(--path-color);
}
.btn-multiplayer .diff-desc {
    font-size: 12px;
    color: #666;
}

/* --- 로비 화면 --- */
.lobby-container {
    max-width: 400px;
    margin: 0 auto;
    padding: 40px 24px;
    text-align: center;
}
.lobby-title {
    font-size: 24px;
    font-weight: 800;
    color: var(--text-color);
    margin-bottom: 24px;
}
.lobby-section {
    display: none;
}
.lobby-section.active {
    display: block;
}
.lobby-status {
    font-size: 16px;
    color: #666;
    margin: 20px 0;
}
.lobby-stats {
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 12px;
    min-height: 18px;
}
.lobby-error {
    color: #e53935;
    font-size: 14px;
    min-height: 20px;
    margin-bottom: 8px;
    opacity: 0;
    transition: opacity 0.3s;
}
.lobby-error.visible {
    opacity: 1;
}

/* 난이도 선택 */
.lobby-difficulty-select {
    display: flex;
    gap: 8px;
    justify-content: center;
    margin-bottom: 20px;
}
.lobby-difficulty {
    padding: 10px 16px;
    border: 2px solid var(--cell-border);
    border-radius: 10px;
    background: white;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    transition: border-color 0.2s, background 0.2s;
}
.lobby-difficulty.selected {
    border-color: var(--path-color);
    background: var(--path-light);
}
.lobby-difficulty[data-difficulty="hell"].selected {
    border-color: #e53935;
    background: #ffebee;
}

/* 방 참가 */
.lobby-divider {
    color: #999;
    font-size: 14px;
    margin: 16px 0;
}
.lobby-join {
    display: flex;
    gap: 8px;
    justify-content: center;
    align-items: center;
}
.input-room-code {
    width: 120px;
    font-size: 22px;
    font-weight: 700;
    text-align: center;
    letter-spacing: 4px;
    text-transform: uppercase;
    padding: 10px;
    border: 2px solid var(--cell-border);
    border-radius: 10px;
    background: white;
    font-family: inherit;
    outline: none;
}
.input-room-code:focus {
    border-color: var(--path-color);
}

/* 대기 화면 */
.lobby-label {
    font-size: 14px;
    color: #999;
    margin-bottom: 4px;
}
.room-code-display {
    font-size: 48px;
    font-weight: 800;
    letter-spacing: 8px;
    color: var(--path-color);
    margin: 8px 0;
}
.lobby-hint {
    font-size: 13px;
    color: #999;
    margin-bottom: 16px;
}
.waiting-dots {
    font-size: 24px;
    color: var(--path-color);
}
.waiting-dots span {
    animation: waitBlink 1.4s infinite;
}
.waiting-dots span:nth-child(2) { animation-delay: 0.2s; }
.waiting-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes waitBlink {
    0%, 20% { opacity: 0; }
    50% { opacity: 1; }
    100% { opacity: 0; }
}
/* 빠른 대전 버튼 */
.btn-quick-match {
    width: 100%;
    padding: 16px 0;
    font-size: 18px;
    font-weight: 700;
    color: #fff;
    background: var(--path-color);
    border: none;
    border-radius: 12px;
    cursor: pointer;
    font-family: inherit;
    transition: all 0.12s;
}
.btn-quick-match:active {
    transform: scale(0.97);
    opacity: 0.9;
}

/* 비번방 섹션 */
.private-room-section {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.lobby-back-btn {
    margin-top: 32px;
}

/* --- 상대 진행바 --- */
.opponent-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    background: #fff3f3;
    border: 1px solid #ffcdd2;
    border-radius: 8px;
    margin-bottom: 4px;
    font-size: 13px;
    color: #c62828;
}
.opponent-progress-track {
    flex: 1;
    height: 4px;
    background: #ffcdd2;
    border-radius: 2px;
    overflow: hidden;
}
.opponent-progress-fill {
    height: 100%;
    width: 0%;
    background: #e53935;
    border-radius: 2px;
    transition: width 0.3s ease;
}
.opponent-bar.finished {
    background: #fce4ec;
    border-color: #e53935;
}

/* --- 카운트다운 오버레이 --- */
.countdown-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 12px;
    background: rgba(245, 240, 232, 0.9);
    z-index: 50;
    pointer-events: auto;
}
.countdown-vs {
    font-size: 16px;
    font-weight: 700;
    color: var(--text-secondary);
    text-align: center;
    line-height: 1.5;
}
.countdown-vs .vs-label {
    font-size: 13px;
    color: #999;
    margin: 2px 0;
}
.countdown-vs .vs-name {
    font-size: 17px;
    color: var(--text-primary);
}
.countdown-number {
    font-size: 72px;
    font-weight: 800;
    color: var(--path-color);
}

/* --- 멀티플레이 결과 모달 --- */
.mp-result-title {
    font-size: 32px;
    font-weight: 800;
    margin-bottom: 8px;
}
.mp-result-title.mp-win {
    color: var(--success);
}
.mp-result-title.mp-lose {
    color: #e53935;
}
.mp-result-title.mp-disconnect {
    color: #999;
}
.mp-result-detail {
    font-size: 16px;
    color: #666;
    margin-bottom: 16px;
}
.rematch-indicator {
    font-size: 14px;
    color: var(--path-color);
    animation: pulse 1s infinite;
    margin-bottom: 8px;
}
.rematch-waiting {
    font-size: 14px;
    color: #999;
    margin-bottom: 8px;
}

/* --- 닉네임 모달 --- */
.nickname-modal {
    display: flex;
    flex-direction: column;
    gap: 16px;
}
.nickname-title {
    font-size: 22px;
    font-weight: 800;
}
.input-nickname {
    width: 100%;
    font-size: 18px;
    font-weight: 600;
    text-align: center;
    padding: 12px;
    border: 2px solid var(--cell-border);
    border-radius: 12px;
    background: white;
    font-family: inherit;
    outline: none;
}
.input-nickname:focus {
    border-color: var(--path-color);
}
