/* 全域 CSS 變數定義：包含光亮模式與基礎尺寸 */
:root {
    --bg-color: #faf8ef;
    --grid-bg: #bbada0;
    --cell-empty: #cdc1b4;
    --text-dark: #776e65;
    --text-light: #f9f6f2;
    --cell-size: 80px;
    --gap: 10px;
}

/* 深色模式變數覆寫 */
[data-theme="dark"] {
    --bg-color: #1a1a2e;
    --grid-bg: #16213e;
    --cell-empty: #0f3460;
    --text-dark: #e94560;
    --text-light: #f9f6f2;
}

/* 基礎重置 */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Fredoka', cursive;
    background: var(--bg-color);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    user-select: none;
    /* 防止遊戲中選取到文字 */
    overflow: hidden;
    /* 防止行動版滑動時頁面跟著捲動 */
}

/* 頂部標題與分數欄佈局 */
.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: calc(var(--cell-size) * 4 + var(--gap) * 5);
    margin-bottom: 20px;
}

.title {
    font-size: 48px;
    color: var(--text-dark);
    display: flex;
    align-items: baseline;
    gap: 10px;
}

.version {
    font-size: 14px;
    opacity: 0.5;
    font-weight: normal;
}


.score-container {
    display: flex;
    gap: 10px;
    align-items: center;
}

/* 功能按鈕樣式 (主題、音效、AI) */
.theme-btn {
    background: var(--grid-bg);
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 6px;
    font-size: 20px;
    cursor: pointer;
    transition: transform 0.1s;
    display: flex;
    justify-content: center;
    align-items: center;
}

.theme-btn:hover {
    transform: scale(1.1);
}

.theme-btn.active {
    background: #edc22e;
    /* AI 執行中變換顏色 */
}

.theme-btn:active {
    transform: scale(0.95);
}

/* 分數顯示方塊 */
.score-box {
    background: var(--grid-bg);
    padding: 10px 20px;
    border-radius: 6px;
    text-align: center;
    color: var(--text-light);
}

.score-label {
    font-size: 12px;
    text-transform: uppercase;
}

.score-value {
    font-size: 24px;
    font-weight: bold;
}

/* 遊戲網格主要容器 */
.game-wrapper {
    display: flex;
    gap: 20px;
    align-items: flex-start;
}

.game-container {
    position: relative;
    background: var(--grid-bg);
    padding: var(--gap);
    border-radius: 10px;
}

/* 悔棋按鈕 */
.undo-btn {
    position: absolute;
    bottom: -45px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--grid-bg);
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 6px;
    font-size: 20px;
    cursor: pointer;
    transition: transform 0.1s, opacity 0.2s;
    z-index: 200;
}

.undo-btn:hover:not(:disabled) {
    transform: translateX(-50%) scale(1.1);
}

.undo-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

/* 背景網格定義 */
.grid {
    display: grid;
    grid-template-columns: repeat(4, var(--cell-size));
    grid-template-rows: repeat(4, var(--cell-size));
    gap: var(--gap);
}

/* 單個空格樣式 */
.cell {
    background: var(--cell-empty);
    border-radius: 6px;
    width: var(--cell-size);
    height: var(--cell-size);
}

/* 動態方塊渲染層 */
.tile-container {
    position: absolute;
    top: var(--gap);
    left: var(--gap);
    width: calc(100% - var(--gap) * 2);
    height: calc(100% - var(--gap) * 2);
    pointer-events: none;
    /* 確保不阻擋滑動事件 */
}

/* 單個數字方塊基礎樣式 */
.tile {
    position: absolute;
    width: var(--cell-size);
    height: var(--cell-size);
    border-radius: 6px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 32px;
    font-weight: bold;
    transition: transform 0.15s ease-in-out;
    /* 移動平滑動畫 */
    z-index: 10;
}

/* 出現與合併動畫 */
.tile-new {
    animation: appear 0.2s ease;
}

.tile-merged {
    animation: pop 0.2s ease;
}

@keyframes appear {
    0% {
        transform: scale(0);
        opacity: 0;
    }

    100% {
        transform: scale(1);
        opacity: 1;
    }
}

@keyframes pop {
    0% {
        transform: scale(1);
    }

    50% {
        transform: scale(1.2);
    }

    100% {
        transform: scale(1);
    }
}

/* 方塊顏色設定 - 經典模式 */
.tile-2 {
    background: #eee4da;
    color: var(--text-dark);
}

.tile-4 {
    background: #ede0c8;
    color: var(--text-dark);
}

.tile-8 {
    background: #f2b179;
    color: var(--text-light);
}

.tile-16 {
    background: #f59563;
    color: var(--text-light);
}

.tile-32 {
    background: #f67c5f;
    color: var(--text-light);
}

.tile-64 {
    background: #f65e3b;
    color: var(--text-light);
}

.tile-128 {
    background: #edcf72;
    color: var(--text-light);
    font-size: 28px;
}

.tile-256 {
    background: #edcc61;
    color: var(--text-light);
    font-size: 28px;
}

.tile-512 {
    background: #edc850;
    color: var(--text-light);
    font-size: 28px;
}

.tile-1024 {
    background: #edc53f;
    color: var(--text-light);
    font-size: 22px;
}

.tile-2048 {
    background: #edc22e;
    color: var(--text-light);
    font-size: 22px;
}

.tile-super {
    background: #3c3a32;
    color: var(--text-light);
    font-size: 18px;
}

/* 方塊顏色設定 - 深色/霓虹模式 */
[data-theme="dark"] .tile-2 {
    background: #2d2d44;
    color: #fff;
}

[data-theme="dark"] .tile-4 {
    background: #3d3d5c;
    color: #fff;
}

[data-theme="dark"] .tile-8 {
    background: #e94560;
    color: #fff;
}

[data-theme="dark"] .tile-16 {
    background: #ff6b6b;
    color: #fff;
}

[data-theme="dark"] .tile-32 {
    background: #ff8e72;
    color: #fff;
}

[data-theme="dark"] .tile-64 {
    background: #ffa07a;
    color: #fff;
}

[data-theme="dark"] .tile-128 {
    background: #ffd93d;
    color: #fff;
    font-size: 28px;
}

[data-theme="dark"] .tile-256 {
    background: #ffec42;
    color: #333;
    font-size: 28px;
}

[data-theme="dark"] .tile-512 {
    background: #c8e600;
    color: #333;
    font-size: 28px;
}

[data-theme="dark"] .tile-1024 {
    background: #6ecf00;
    color: #fff;
    font-size: 22px;
}

[data-theme="dark"] .tile-2048 {
    background: #00cf95;
    color: #fff;
    font-size: 22px;
}

[data-theme="dark"] .tile-super {
    background: #009ecf;
    color: #fff;
    font-size: 18px;
}

/* 覆蓋層樣式：遊戲結束、獲勝、提示器 */
.game-over {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(238, 228, 218, 0.73);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    border-radius: 10px;
    z-index: 100;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
}

[data-theme="dark"] .game-over {
    background: rgba(22, 33, 62, 0.85);
}

.game-over.active {
    opacity: 1;
    pointer-events: all;
}

.swipe-indicator {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 60px;
    opacity: 0;
    pointer-events: none;
    z-index: 50;
    color: var(--text-light);
    /* 提高對比 */
}

.swipe-indicator.show {
    opacity: 0.6;
    animation: swipePulse 0.3s ease-out;
}

.combo-display {
    position: absolute;
    top: 20%;
    right: 10px;
    font-size: 24px;
    font-weight: bold;
    color: #f65e3b;
    opacity: 0;
    pointer-events: none;
    z-index: 50;
}

.combo-display.show {
    opacity: 1;
    animation: comboPop 0.3s ease-out;
}

@keyframes comboPop {
    0% {
        transform: scale(0.5);
    }

    50% {
        transform: scale(1.3);
    }

    100% {
        transform: scale(1);
    }
}

@keyframes swipePulse {
    0% {
        transform: translate(-50%, -50%) scale(0.5);
        opacity: 0;
    }

    50% {
        transform: translate(-50%, -50%) scale(1.2);
        opacity: 0.8;
    }

    100% {
        transform: translate(-50%, -50%) scale(1);
        opacity: 0.6;
    }
}

.win-display {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(237, 194, 46, 0.8);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    border-radius: 10px;
    z-index: 100;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
}

.win-display.active {
    opacity: 1;
    pointer-events: all;
}

.win-display h2 {
    font-size: 40px;
    color: var(--text-light);
    margin-bottom: 10px;
}

.win-display p {
    font-size: 20px;
    color: var(--text-light);
    margin-bottom: 20px;
}

.game-over h2 {
    font-size: 40px;
    color: var(--text-dark);
    margin-bottom: 20px;
}

/* 通用按鈕樣式 */
.restart-btn {
    background: #8f7a66;
    color: var(--text-light);
    border: none;
    padding: 12px 24px;
    font-size: 18px;
    border-radius: 6px;
    cursor: pointer;
    font-family: inherit;
    transition: transform 0.1s;
}

.restart-btn:hover {
    transform: scale(1.05);
}

.restart-btn:active {
    transform: scale(0.95);
}

/* 指示與說明文字 */
.instructions {
    margin-top: 20px;
    color: var(--text-dark);
    font-size: 16px;
    text-align: center;
}

/* 鍵盤提示佈局 */
.key-hints {
    display: flex;
    gap: 8px;
    margin-top: 10px;
}

.key {
    width: 36px;
    height: 36px;
    background: var(--grid-bg);
    border-radius: 6px;
    display: flex;
    justify-content: center;
    align-items: center;
    color: var(--text-light);
    font-size: 18px;
    font-weight: bold;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.key-row {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.key-group {
    display: flex;
    gap: 4px;
}

/* 規則面版樣式 */
.rules-panel {
    background: var(--grid-bg);
    padding: 15px;
    border-radius: 10px;
    color: var(--text-light);
    min-width: 150px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.rules-panel h3 {
    font-size: 18px;
    margin-bottom: 12px;
    text-align: center;
}

.rules-list {
    list-style: none;
    font-size: 14px;
    line-height: 1.8;
}

.rule-example {
    display: inline-block;
    background: #eee4da;
    color: var(--text-dark);
    padding: 2px 8px;
    border-radius: 4px;
    font-weight: bold;
    font-size: 12px;
    min-width: 24px;
    text-align: center;
}

/* 響應式：手機版適配 (通常網格縮小) */
@media (max-width: 480px) {
    :root {
        --cell-size: 65px;
        --gap: 8px;
    }

    body {
        padding: 5px;
    }

    .header {
        width: 100%;
        flex-direction: column;
        gap: 10px;
        padding: 0 10px;
    }

    .title {
        font-size: 36px;
    }

    .game-wrapper {
        flex-direction: column;
        align-items: center;
        gap: 15px;
    }

    .rules-panel {
        width: calc(var(--cell-size) * 4 + var(--gap) * 5);
        padding: 12px;
    }

    .key-hints {
        display: none;
        /* 行動裝置隱藏按鍵提示 */
    }
}

@media (max-width: 360px) {
    :root {
        --cell-size: 55px;
        --gap: 6px;
    }
}