@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@400;600;800&display=swap');

/* ===== 라이트 모드 기본값 ===== */
:root {
    --bg-color: #f0f4f8;
    --board-bg-odd: #f8f9fa;
    --board-bg-even: #e9ecef;
    --primary-color: #ff4757;
    --text-dark: #2f3542;
    --tile-size: 40px;
    --body-bg: linear-gradient(135deg, #e0eafc, #cfdef3);
    --wrapper-bg: white;
    --wrapper-shadow: 0 20px 50px rgba(0, 0, 0, 0.1);
    --wrapper-border: 1px solid rgba(255, 255, 255, 0.4);
    --top-bar-bg: #f1f2f6;
    --top-bar-border: 1px solid #dfe4ea;
    --timer-bg: #ced6e0;
    --body-before-opacity: 0;
}

/* ===== 다크 모드: 네온 갤러리 (프리미엄 업그레이드) ===== */
[data-theme="dark"] {
    --bg-color: #0f0f1a;
    --board-bg-odd: rgba(22, 33, 62, 0.6);
    --board-bg-even: rgba(22, 33, 62, 0.3);
    --primary-color: #ff4757; /* 네온 레드 */
    --text-dark: #f0f0f8;
    --tile-size: 40px;
    --body-bg: #0f0f1a;
    --wrapper-bg: rgba(22, 33, 62, 0.85);
    --wrapper-shadow: 0 10px 40px rgba(0, 0, 0, 0.5), 0 0 20px rgba(108, 99, 255, 0.2);
    --wrapper-border: 1px solid rgba(108, 99, 255, 0.3);
    --top-bar-bg: rgba(0, 0, 0, 0.4);
    --top-bar-border: 1px solid rgba(108, 99, 255, 0.2);
    --timer-bg: rgba(255, 255, 255, 0.1);
    --body-before-opacity: 1;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Outfit', sans-serif;
    background: var(--body-bg);
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    user-select: none;
}

body::before {
    content: '';
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: 
      radial-gradient(ellipse at 20% 0%, rgba(108, 99, 255, 0.15) 0%, transparent 50%),
      radial-gradient(ellipse at 80% 100%, rgba(167, 139, 250, 0.1) 0%, transparent 50%);
    pointer-events: none;
    z-index: 0;
    opacity: var(--body-before-opacity, 0);
    transition: opacity 0.4s ease;
}

#game-wrapper {
    background: var(--wrapper-bg);
    box-shadow: var(--wrapper-shadow);
    border-radius: 16px;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 15px;
    border: var(--wrapper-border);
    position: relative;
    z-index: 10;
    max-width: 95vw;
    width: fit-content;
    margin: 0 auto;
}

/* 유저 요청: 세로 모드에서는 좌우 여백을 반으로 줄이고 게임 영역을 극대화 */
@media (orientation: portrait) {
    #game-wrapper {
        padding: 10px;
        width: 98%;
        max-width: 500px; /* 세로 모드 제한 */
        box-sizing: border-box;
        gap: 10px; /* 메뉴간 세로 간격도 살짝 줄여줌 */
    }
}

/* Header */
#top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--top-bar-bg);
    padding: 10px 15px;
    border-radius: 8px;
    border: var(--top-bar-border);
}

#timer-container {
    flex-grow: 1;
    height: 18px;
    background: var(--timer-bg);
    border-radius: 9px;
    overflow: hidden;
    margin-right: 20px;
    position: relative;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);
}

#timer-bar {
    height: 100%;
    width: 100%;
    background: linear-gradient(90deg, #ff6b81, var(--primary-color));
    transition: width 0.1s linear, background-color 0.3s;
}

#score-area {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 0;
}

#score-display {
    font-size: 28px;
    font-weight: 800;
    color: var(--primary-color);
    min-width: 60px;
    text-align: right;
    line-height: 1;
}

#best-area {
    font-size: 11px;
    font-weight: 600;
    color: #a4b0be;
    text-align: right;
    letter-spacing: 0.5px;
}

#best-area span {
    color: #ffa502;
    font-weight: 800;
}

/* Board */
#board-container {
    /* width and height set dynamically by JS */
    box-sizing: content-box;
    margin: 0 auto; /* 타일 그리드를 wrapper 영역의 정중앙에 정렬 */
    /* Ensure border doesn't shrink the content area */
    background-image: linear-gradient(45deg, var(--board-bg-even) 25%, transparent 25%, transparent 75%, var(--board-bg-even) 75%, var(--board-bg-even)),
        linear-gradient(45deg, var(--board-bg-even) 25%, transparent 25%, transparent 75%, var(--board-bg-even) 75%, var(--board-bg-even));
    background-size: var(--tile-size) var(--tile-size);
    background-position: 0 0, calc(var(--tile-size) / 2) calc(var(--tile-size) / 2);
    background-color: var(--board-bg-odd);
    position: relative;
    border: 2px solid #ced6e0;
    border-radius: 4px;
    box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.05);
    cursor: pointer;
    overflow: visible;
    /* Prevent clipping during animations */
}

/* Base style for tiles */
.tile {
    width: var(--tile-size);
    height: var(--tile-size);
    position: absolute;
    border-radius: 6px;
    transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275), opacity 0.3s;
    box-shadow: inset 0 3px 5px rgba(255, 255, 255, 0.7), inset 0 -3px 5px rgba(0, 0, 0, 0.15), 0 2px 4px rgba(0, 0, 0, 0.2);
    box-sizing: border-box;
    border: 1px solid rgba(255, 255, 255, 0.3);
    pointer-events: none;
    /* Make tiles click-through so we can click the board under them */
}

/* Tile entry animation */
.tile-enter {
    animation: popIn 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

@keyframes popIn {
    0% {
        transform: scale(0);
        opacity: 0;
    }

    100% {
        transform: scale(1);
        opacity: 1;
    }
}

/* --- New Scanning Animations --- */
.scan-center {
    position: absolute;
    width: 24px;
    height: 24px;
    border: 3px solid #00b894;
    /* Greenish ring like in photo */
    border-radius: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
    z-index: 6;
    animation: pulseRing 0.4s ease-out forwards;
}

@keyframes pulseRing {
    0% {
        transform: translate(-50%, -50%) scale(0.5);
        opacity: 1;
    }

    100% {
        transform: translate(-50%, -50%) scale(1.5);
        opacity: 0;
    }
}

.scan-circle {
    position: absolute;
    width: 16px;
    height: 16px;
    border: 3px solid rgba(178, 190, 195, 0.8);
    /* Grey ring */
    border-radius: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
    z-index: 7;
    transition: left 0.25s cubic-bezier(0.25, 1, 0.5, 1), top 0.25s cubic-bezier(0.25, 1, 0.5, 1);
}

.scan-circle.matched {
    border-color: #fdcb6e;
    /* Highlight when matching */
    background-color: rgba(253, 203, 110, 0.3);
}

/* --- Trail marks along scan path --- */
.scan-trail {
    position: absolute;
    width: 14px;
    height: 14px;
    border: 2px solid rgba(178, 190, 195, 0.6);
    border-radius: 50%;
    transform: translate(-50%, -50%) scale(0);
    pointer-events: none;
    z-index: 4;
    animation: trailAppear 0.2s ease-out forwards;
}

@keyframes trailAppear {
    0% {
        transform: translate(-50%, -50%) scale(0);
        opacity: 0;
    }

    100% {
        transform: translate(-50%, -50%) scale(1);
        opacity: 1;
    }
}

.scan-trail-out {
    animation: trailFade 0.4s ease-out forwards !important;
}

@keyframes trailFade {
    0% {
        transform: translate(-50%, -50%) scale(1);
        opacity: 1;
    }

    100% {
        transform: translate(-50%, -50%) scale(0);
        opacity: 0;
    }
}

/* --- Improved Bounce & Fly Away (Match) Animation --- */
.tile-match {
    animation: flyAway 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

@keyframes flyAway {
    0% {
        transform: scale(1);
        opacity: 1;
    }

    40% {
        transform: scale(1.2) translateY(-10px);
        opacity: 0.9;
    }

    100% {
        transform: scale(0) translateY(-30px) rotate(45deg);
        opacity: 0;
    }
}

/* Footer */
#bottom-bar {
    display: flex;
    flex-wrap: wrap; /* 유저 요청: 공간 부족시 버튼들을 자동으로 아랫줄로 넘김 */
    justify-content: center; /* 줄바꿈 시 레이아웃 균형을 위해 가운데 정렬 */
    align-items: center;
    padding: 0 5px;
    gap: 12px;
}

.logo {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 800;
    color: #ffa502;
    font-size: 20px;
    text-shadow: 1px 1px 0px rgba(0, 0, 0, 0.1);
    white-space: nowrap;
}

#difficulty-badge {
    font-size: 11px;
    background: #ff4757;
    color: white;
    padding: 3px 8px;
    border-radius: 12px;
    font-weight: 800;
    text-shadow: none;
    letter-spacing: 0.5px;
    box-shadow: 0 2px 4px rgba(255, 71, 87, 0.4);
}

.server-best-banner {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #f1f2f6, #dfe4ea);
    padding: 4px 12px;
    border-radius: 8px;
    border: 1px solid #ced6e0;
    min-width: 100px;
}

.sb-label {
    font-size: 10px;
    font-weight: 800;
    color: #747d8c;
    letter-spacing: 0.5px;
    line-height: 1;
}

#server-best-display {
    font-size: 18px;
    font-weight: 800;
    color: #1e90ff;
    line-height: 1.1;
    text-shadow: 1px 1px 0px rgba(255, 255, 255, 0.8);
}

.footer-buttons {
    display: flex;
    gap: 8px;
    align-items: center;
}

#reset-btn,
#settings-btn {
    padding: 8px 20px;
    border: 1px solid #ced6e0;
    background: #f1f2f6;
    border-radius: 4px;
    font-weight: 600;
    color: #57606f;
    cursor: pointer;
    transition: all 0.2s;
    font-family: inherit;
    font-size: 14px;
}

#settings-btn {
    padding: 8px 14px;
    font-size: 18px;
}

#reset-btn:hover,
#settings-btn:hover {
    background: #dfe4ea;
    color: #2f3542;
}

/* Settings Overlay */
#settings-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(4px);
    z-index: 150;
    display: flex;
    justify-content: center;
    align-items: center;
}

#settings-panel {
    background: rgba(255, 255, 255, 0.95);
    padding: 30px 35px;
    border-radius: 16px;
    box-shadow: 0 15px 40px rgba(0, 0, 0, 0.2);
    min-width: 320px;
    text-align: center;
    animation: slideUp 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

#settings-panel h2 {
    margin-bottom: 20px;
    font-size: 22px;
    color: var(--text-dark);
}

.settings-row {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 15px;
    font-size: 15px;
    color: #57606f;
}

.settings-row label {
    min-width: 100px;
    text-align: left;
    font-weight: 600;
}

.settings-row input[type="range"] {
    flex: 1;
    height: 6px;
    -webkit-appearance: none;
    appearance: none;
    background: #dfe4ea;
    border-radius: 3px;
    outline: none;
    cursor: pointer;
}

.settings-row input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: linear-gradient(135deg, #1e90ff, #5352ed);
    cursor: pointer;
    box-shadow: 0 2px 6px rgba(83, 82, 237, 0.4);
}

.settings-row span {
    min-width: 40px;
    font-weight: 600;
    color: var(--text-dark);
}

/* Difficulty Options Styling */
.difficulty-row {
    margin-bottom: 20px;
}

.difficulty-options {
    flex: 1;
    display: flex;
    gap: 8px;
    justify-content: flex-end;
}

.diff-btn {
    display: inline-block;
    padding: 6px 12px;
    background: #f1f2f6;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 700;
    color: #57606f;
    cursor: pointer;
    border: 2px solid transparent;
    transition: all 0.2s ease;
}

.diff-btn input[type="radio"] {
    display: none;
}

.diff-btn:hover {
    background: #dfe4ea;
}

.diff-btn:has(input[type="radio"]:checked) {
    background: rgba(30, 144, 255, 0.1);
    border-color: #1e90ff;
    color: #1e90ff;
}

#settings-close {
    margin-top: 10px;
    padding: 10px 30px;
    background: linear-gradient(135deg, #1e90ff, #5352ed);
    color: white;
    border: none;
    border-radius: 20px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    transition: transform 0.2s;
}

#settings-close:hover {
    transform: translateY(-2px);
}

/* Improved Modal */
#modal-emoji {
    font-size: 48px;
    margin-bottom: 5px;
    animation: bounceIn 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes bounceIn {
    0% {
        transform: scale(0);
    }

    60% {
        transform: scale(1.2);
    }

    100% {
        transform: scale(1);
    }
}

.modal-score-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;
    border-bottom: 1px solid #f1f2f6;
    font-size: 18px;
}

.modal-score-row:last-child {
    border-bottom: none;
}

.modal-score-label {
    font-size: 13px;
    font-weight: 600;
    color: #a4b0be;
    text-transform: uppercase;
    letter-spacing: 1px;
}

#modal-score,
#modal-best {
    font-size: 28px;
    font-weight: 800;
    color: var(--primary-color);
}

#modal-new-record {
    color: #ffa502;
    font-weight: 800;
    font-size: 16px;
    margin: 8px 0 0;
    animation: pulse 0.8s ease-in-out infinite alternate;
}

@keyframes pulse {
    from {
        opacity: 0.7;
        transform: scale(0.97);
    }

    to {
        opacity: 1;
        transform: scale(1.03);
    }
}

/* Help Panel */
#help-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(4px);
    z-index: 150;
    display: flex;
    justify-content: center;
    align-items: center;
}

#help-panel {
    background: rgba(255, 255, 255, 0.97);
    padding: 30px 35px;
    border-radius: 20px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2);
    max-width: 380px;
    width: 90%;
    animation: slideUp 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

#help-panel h2 {
    text-align: center;
    margin-bottom: 18px;
    font-size: 20px;
    color: var(--text-dark);
}

.help-list {
    list-style: none;
    padding: 0;
    margin: 0 0 20px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.help-list li {
    font-size: 14px;
    color: #57606f;
    line-height: 1.5;
    padding: 10px 14px;
    background: #f8f9fa;
    border-radius: 10px;
}

.help-list li strong {
    color: var(--text-dark);
}

#help-close {
    display: block;
    width: 100%;
    padding: 12px;
    background: linear-gradient(135deg, #2ed573, #1e90ff);
    color: white;
    border: none;
    border-radius: 20px;
    font-size: 15px;
    font-weight: 700;
    cursor: pointer;
    font-family: inherit;
    transition: transform 0.2s;
}

#help-close:hover {
    transform: translateY(-2px);
}

/* Penalty flash */
#penalty-flash {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 71, 87, 0.3);
    z-index: 99;
    pointer-events: none;
    animation: flashOut 0.5s forwards;
}

.hidden {
    display: none !important;
}

@keyframes flashOut {
    0% {
        opacity: 1;
    }

    100% {
        opacity: 0;
    }
}

.shake {
    animation: shake 0.4s;
}

@keyframes shake {
    0% {
        transform: translateX(0);
    }

    20% {
        transform: translateX(-10px);
    }

    40% {
        transform: translateX(10px);
    }

    60% {
        transform: translateX(-10px);
    }

    80% {
        transform: translateX(10px);
    }

    100% {
        transform: translateX(0);
    }
}

/* Modal */
#modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(5px);
    z-index: 100;
    display: flex;
    justify-content: center;
    align-items: center;
}

#modal-box {
    background: rgba(255, 255, 255, 0.9);
    padding: 40px;
    border-radius: 20px;
    text-align: center;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.5);
    transform: translateY(20px);
    animation: slideUp 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

@keyframes slideUp {
    to {
        transform: translateY(0);
    }
}

#modal-title {
    font-size: 36px;
    color: var(--text-dark);
    margin-bottom: 5px;
}

#modal-desc {
    color: #747d8c;
    margin-bottom: 25px;
}

#modal-score-wrap {
    font-size: 24px;
    font-weight: 600;
    background: #f1f2f6;
    padding: 15px 30px;
    border-radius: 12px;
    margin-bottom: 25px;
    color: #2f3542;
}

#modal-score {
    color: var(--primary-color);
    font-size: 32px;
    font-weight: 800;
}

#modal-restart {
    background: linear-gradient(135deg, #1e90ff, #5352ed);
    color: white;
    border: none;
    padding: 12px 30px;
    font-size: 18px;
    border-radius: 30px;
    cursor: pointer;
    font-weight: 600;
    font-family: inherit;
    box-shadow: 0 4px 15px rgba(83, 82, 237, 0.4);
    transition: transform 0.2s, box-shadow 0.2s;
}

#modal-restart:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(83, 82, 237, 0.6);
}

/* 12 Tile Colors Generation Using HSL or predefined colors for premium look */
.color-0 {
    background: linear-gradient(135deg, #ff6b81, #ff4757);
}

/* Red */
.color-1 {
    background: linear-gradient(135deg, #ffa502, #eaaa00);
}

/* Orange */
.color-2 {
    background: linear-gradient(135deg, #eccc68, #d4a300);
}

/* Yellow */
.color-3 {
    background: linear-gradient(135deg, #7bed9f, #2ed573);
}

/* Green */
.color-4 {
    background: linear-gradient(135deg, #70a1ff, #1e90ff);
}

/* Blue */
.color-5 {
    background: linear-gradient(135deg, #a4b0be, #747d8c);
}

/* Grey */
.color-6 {
    background: linear-gradient(135deg, #ff7f50, #ff6348);
}

/* Coral */
.color-7 {
    background: linear-gradient(135deg, #9b59b6, #8e44ad);
}

/* Purple */
.color-8 {
    background: linear-gradient(135deg, #ff9ff3, #f368e0);
}

/* Pink */
.color-9 {
    background: linear-gradient(135deg, #00d2d3, #01a3a4);
}

/* Teal/Cyan */
.color-10 {
    background: linear-gradient(135deg, #badc58, #6ab04c);
}

/* Light Green */
.color-11 {
    background: linear-gradient(135deg, #d2b48c, #cd853f);
}

/* Brown */