/* === CSS 變量 === */
:root {
    --bg-primary: #1a1a2e;
    --bg-secondary: #16213e;
    --bg-board: #0f3460;
    --bg-cell: #1a1a4e;
    --text-primary: #e0e0ff;
    --text-secondary: #a0a0cc;
    --accent: #e94560;
    --accent-glow: rgba(233, 69, 96, 0.5);
    --neon-blue: #00d4ff;
    --neon-blue-glow: rgba(0, 212, 255, 0.4);
    --neon-purple: #b44dff;
    --neon-purple-glow: rgba(180, 77, 255, 0.4);
    --score-bg: linear-gradient(135deg, #e94560, #b44dff);
    --btn-bg: linear-gradient(135deg, #e94560, #c23672);
    --btn-hover: linear-gradient(135deg, #ff5a7a, #e94560);
    --board-radius: 16px;
    --transition: 0.3s ease;
}

[data-theme="light"] {
    --bg-primary: #faf8ef;
    --bg-secondary: #f0ede4;
    --bg-board: #bbada0;
    --bg-cell: #cdc1b4;
    --text-primary: #776e65;
    --text-secondary: #8a7e72;
    --accent: #f65e3b;
    --accent-glow: rgba(246, 94, 59, 0.3);
    --neon-blue: #2196f3;
    --neon-blue-glow: rgba(33, 150, 243, 0.3);
    --neon-purple: #9c27b0;
    --neon-purple-glow: rgba(156, 39, 176, 0.3);
    --score-bg: linear-gradient(135deg, #8f7a66, #bbada0);
    --btn-bg: linear-gradient(135deg, #8f7a66, #a08b77);
    --btn-hover: linear-gradient(135deg, #9f8b77, #8f7a66);
    --board-radius: 16px;
}

/* === 全局樣式 === */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: "Helvetica Neue", "Noto Sans TC", Arial, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    margin: 0;
    padding: 0;
    font-size: 18px;
    min-height: 100vh;
    transition: background var(--transition), color var(--transition);
    overflow-x: hidden;
}

/* 背景動畫粒子 */
body::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background:
        radial-gradient(circle at 20% 80%, var(--neon-purple-glow) 0%, transparent 50%),
        radial-gradient(circle at 80% 20%, var(--neon-blue-glow) 0%, transparent 50%),
        radial-gradient(circle at 50% 50%, var(--accent-glow) 0%, transparent 70%);
    pointer-events: none;
    z-index: 0;
    animation: bgPulse 8s ease-in-out infinite alternate;
}

@keyframes bgPulse {
    0% { opacity: 0.3; }
    100% { opacity: 0.7; }
}

.container {
    position: relative;
    z-index: 1;
    width: 100%;
    max-width: 500px;
    margin: 0 auto;
    padding: 20px;
}

/* === 標題和分數 === */
header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

h1 {
    font-size: 4rem;
    font-weight: 900;
    margin: 0;
    background: linear-gradient(135deg, var(--accent), var(--neon-purple), var(--neon-blue));
    background-size: 200% 200%;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    animation: gradientShift 3s ease-in-out infinite;
    filter: drop-shadow(0 0 20px var(--accent-glow));
    letter-spacing: -2px;
}

@keyframes gradientShift {
    0%, 100% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
}

.scores-container {
    display: flex;
    gap: 10px;
}

.score-container, .best-container {
    position: relative;
    background: var(--score-bg);
    padding: 8px 16px;
    border-radius: 12px;
    color: white;
    text-align: center;
    min-width: 85px;
    box-shadow: 0 4px 15px var(--accent-glow);
    transition: transform 0.2s, box-shadow 0.2s;
}

.score-container:hover, .best-container:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px var(--accent-glow);
}

.score-label {
    font-size: 0.7rem;
    text-transform: uppercase;
    opacity: 0.8;
    letter-spacing: 1px;
}

#score, #best-score {
    font-size: 1.5rem;
    font-weight: bold;
}

/* 分數增加動畫 */
.score-pop {
    animation: scorePop 0.3s ease-out;
}

@keyframes scorePop {
    0% { transform: scale(1); }
    50% { transform: scale(1.2); }
    100% { transform: scale(1); }
}

/* 分數浮動文字 */
.score-addition {
    position: absolute;
    top: -5px;
    right: 10px;
    font-size: 1.1rem;
    font-weight: bold;
    color: #fff;
    text-shadow: 0 0 10px var(--accent-glow);
    pointer-events: none;
    animation: scoreFloat 0.8s ease-out forwards;
    z-index: 200;
}

@keyframes scoreFloat {
    0% {
        opacity: 1;
        transform: translateY(0);
    }
    100% {
        opacity: 0;
        transform: translateY(-40px);
    }
}

/* === 遊戲介紹區域 === */
.game-intro {
    margin-bottom: 16px;
}

.instructions {
    margin-bottom: 12px;
    line-height: 1.6;
    color: var(--text-secondary);
}

.instructions strong {
    color: var(--accent);
}

.game-controls {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    gap: 10px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}

/* === 按鈕樣式 === */
button {
    background: var(--btn-bg);
    color: white;
    border: none;
    border-radius: 10px;
    padding: 10px 20px;
    font-weight: bold;
    cursor: pointer;
    font-size: 0.95rem;
    transition: all var(--transition);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    position: relative;
    overflow: hidden;
}

button::after {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
    transition: left 0.5s;
}

button:hover {
    background: var(--btn-hover);
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
}

button:hover::after {
    left: 100%;
}

button:active {
    transform: translateY(0);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

/* 撤銷按鈕 */
#undo-button {
    background: linear-gradient(135deg, #555, #777);
    font-size: 1.2rem;
    padding: 8px 16px;
}

#undo-button:hover {
    background: linear-gradient(135deg, #666, #888);
}

#undo-button:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    transform: none;
}

/* === 音效控制 === */
.sound-control {
    display: flex;
    align-items: center;
    gap: 6px;
    color: var(--text-secondary);
    font-size: 0.9rem;
}

.sound-control input[type="checkbox"] {
    width: 18px;
    height: 18px;
    accent-color: var(--accent);
    cursor: pointer;
}

/* === 主題切換 === */
.theme-toggle {
    background: linear-gradient(135deg, #333, #555);
    color: white;
    border: none;
    border-radius: 50%;
    width: 40px;
    height: 40px;
    font-size: 1.2rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition);
    box-shadow: 0 2px 10px rgba(0,0,0,0.3);
    padding: 0;
}

[data-theme="light"] .theme-toggle {
    background: linear-gradient(135deg, #ddd, #bbb);
}

.theme-toggle:hover {
    transform: rotate(30deg) scale(1.1);
}

/* === 遊戲容器 === */
.game-container {
    position: relative;
    background: var(--bg-board);
    border-radius: var(--board-radius);
    width: 100%;
    padding-bottom: 100%;
    margin-bottom: 16px;
    overflow: hidden;
    box-shadow:
        0 0 30px var(--neon-blue-glow),
        0 8px 32px rgba(0, 0, 0, 0.4),
        inset 0 0 60px rgba(0, 0, 0, 0.1);
    transition: background var(--transition), box-shadow var(--transition);
}

[data-theme="light"] .game-container {
    box-shadow:
        0 4px 20px rgba(0, 0, 0, 0.15),
        inset 0 0 30px rgba(0, 0, 0, 0.05);
}

/* 螢幕震動 */
.game-container.shake {
    animation: shake 0.3s ease-out;
}

@keyframes shake {
    0%, 100% { transform: translate(0, 0); }
    10% { transform: translate(-3px, -2px); }
    20% { transform: translate(3px, 1px); }
    30% { transform: translate(-2px, 3px); }
    40% { transform: translate(2px, -2px); }
    50% { transform: translate(-1px, 2px); }
    60% { transform: translate(1px, -1px); }
    70% { transform: translate(-2px, 1px); }
    80% { transform: translate(2px, -1px); }
    90% { transform: translate(-1px, 1px); }
}

#game-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border-radius: var(--board-radius);
}

/* === 虛擬方向鍵 (D-pad) === */
.dpad-container {
    display: none;
    justify-content: center;
    margin-bottom: 16px;
}

.dpad {
    display: grid;
    grid-template-areas:
        ". up ."
        "left . right"
        ". down .";
    grid-template-columns: 60px 60px 60px;
    grid-template-rows: 60px 60px 60px;
    gap: 6px;
}

.dpad-btn {
    background: var(--btn-bg);
    color: white;
    border: none;
    border-radius: 14px;
    font-size: 1.6rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
    transition: all 0.15s;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    touch-action: manipulation;
    padding: 0;
    width: 60px;
    height: 60px;
}

.dpad-btn:active {
    transform: scale(0.9);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
    filter: brightness(0.8);
}

.dpad-btn.up { grid-area: up; }
.dpad-btn.down { grid-area: down; }
.dpad-btn.left { grid-area: left; }
.dpad-btn.right { grid-area: right; }

/* === 浮動訊息 === */
.floating-messages {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 50;
}

.floating-message {
    position: absolute;
    transform: translateX(-50%);
    padding: 6px 14px;
    background: linear-gradient(135deg, rgba(233, 69, 96, 0.9), rgba(180, 77, 255, 0.9));
    border-radius: 20px;
    box-shadow: 0 2px 15px var(--accent-glow);
    text-align: center;
    font-weight: bold;
    color: white !important;
    opacity: 1;
    transition: all 1s ease-out;
    pointer-events: none;
    z-index: 100;
    text-shadow: 0 0 8px rgba(255, 255, 255, 0.5);
}

[data-theme="light"] .floating-message {
    background: linear-gradient(135deg, rgba(246, 94, 59, 0.9), rgba(156, 39, 176, 0.9));
}

/* === 遊戲結束訊息 === */
.game-message {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(26, 26, 46, 0.85);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    z-index: 100;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    padding: 20px;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.5s;
    border-radius: var(--board-radius);
}

[data-theme="light"] .game-message {
    background: rgba(238, 228, 218, 0.85);
}

.game-message.game-over, .game-message.game-won {
    opacity: 1;
    pointer-events: auto;
}

.game-message.game-won {
    background: rgba(26, 26, 46, 0.9);
}

[data-theme="light"] .game-message.game-won {
    background: rgba(237, 194, 46, 0.6);
}

.game-message p {
    font-size: 2rem;
    font-weight: bold;
    margin-bottom: 20px;
    color: white;
    text-shadow: 0 0 20px var(--accent-glow);
}

[data-theme="light"] .game-message p {
    color: #776e65;
    text-shadow: none;
}

.game-message-buttons {
    display: flex;
    gap: 10px;
}

/* === 頁腳 === */
.footer {
    text-align: center;
    margin-top: 20px;
    font-size: 0.9rem;
    opacity: 0.7;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 15px;
}

.footer p {
    margin-bottom: 5px;
    color: var(--text-secondary);
}

.course-button {
    display: inline-block;
    background: var(--btn-bg);
    color: white;
    padding: 8px 20px;
    border-radius: 10px;
    font-weight: bold;
    text-decoration: none;
    font-size: 1rem;
    transition: all var(--transition);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
}

.course-button:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 25px var(--accent-glow);
    filter: brightness(1.1);
}

/* === 響應式設計 === */
@media (max-width: 520px) {
    .container {
        padding: 12px;
    }

    h1 {
        font-size: 3rem;
    }

    #score, #best-score {
        font-size: 1.2rem;
    }

    .game-intro, .game-controls {
        margin-bottom: 12px;
    }

    button {
        padding: 8px 14px;
        font-size: 0.85rem;
    }

    .control-tips {
        display: none;
    }
}

/* 觸控設備顯示 D-pad */
@media (hover: none) and (pointer: coarse) {
    .dpad-container {
        display: flex;
    }
}

/* === 動畫效果 === */
@keyframes pop {
    0% { transform: scale(0.8); opacity: 0.5; }
    100% { transform: scale(1); opacity: 1; }
}

@keyframes fadeIn {
    0% { opacity: 0; }
    100% { opacity: 1; }
}

@keyframes move {
    0% { transform: translateY(-20px); }
    100% { transform: translateY(0); }
}

/* 頁面載入動畫 */
.container {
    animation: fadeIn 0.5s ease-out;
}
