/* 2048 經典色調樣式表 */
:root {
    --bg-page: #faf8ef;
    --bg-container: #bbada0;
    --bg-cell: #ccc0b3;
    --text-dark: #776e65;
    --text-light: #f9f6f2;
    
    /* 4x4 網格設定，小螢幕下會動態縮小 */
    --tile-size: 90px;
    --tile-gap: 12px;
    --container-padding: 12px;
    --grid-width: calc((var(--tile-size) * 4) + (var(--tile-gap) * 3) + (var(--container-padding) * 2));
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none; /* 防止滑動時頁面滾動與縮放 */
}

body {
    font-family: 'Outfit', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background-color: var(--bg-page);
    color: var(--text-dark);
    min-height: 100vh;
    min-height: 100dvh;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* 主應用容器 */
.app-container {
    width: 100%;
    max-width: var(--grid-width);
    height: calc(100vh - 44px);
    height: calc(100dvh - 44px);
    margin-top: 44px; /* 保留頂部共用導覽列空間 */
    padding: 12px 16px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

/* 頂部資訊區 */
.game-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    margin-bottom: 8px;
}

.logo-text {
    font-size: 2.8rem;
    font-weight: 800;
    color: var(--text-dark);
    line-height: 1;
}

.subtitle-text {
    font-size: 0.8rem;
    color: #8f7a66;
    margin-top: 4px;
    font-weight: 600;
}

.header-right {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 8px;
}

.scores-container {
    display: flex;
    gap: 6px;
}

.score-box {
    background: #bbada0;
    padding: 6px 12px;
    border-radius: 6px;
    color: #ffffff;
    text-align: center;
    min-width: 65px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
}

.box-label {
    font-size: 0.65rem;
    font-weight: 800;
    color: #eee4da;
    display: block;
    letter-spacing: 0.5px;
}

.box-val {
    font-family: 'Share Tech Mono', monospace;
    font-size: 1.15rem;
    font-weight: bold;
    display: block;
}

.action-btn {
    background: #8f7a66;
    color: #ffffff;
    border: none;
    border-radius: 6px;
    padding: 6px 14px;
    font-size: 0.85rem;
    font-weight: 700;
    cursor: pointer;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    transition: background-color 0.2s ease, transform 0.1s ease;
}

.action-btn:hover {
    background: #7f6a56;
}

.action-btn:active {
    transform: scale(0.97);
}

/* 遊戲網格區域 */
.game-area {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 0;
    padding: 10px 0;
}

.grid-container {
    width: var(--grid-width);
    height: var(--grid-width);
    background: var(--bg-container);
    border-radius: 12px;
    padding: var(--container-padding);
    position: relative;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

.grid-row {
    display: flex;
    margin-bottom: var(--tile-gap);
}

.grid-row:last-child {
    margin-bottom: 0;
}

.grid-cell {
    width: var(--tile-size);
    height: var(--tile-size);
    margin-right: var(--tile-gap);
    background: var(--bg-cell);
    border-radius: 8px;
}

.grid-cell:last-child {
    margin-right: 0;
}

/* 數字方塊容器與單一方塊 */
.tile-container {
    position: absolute;
    top: var(--container-padding);
    left: var(--container-padding);
    width: calc(100% - (var(--container-padding) * 2));
    height: calc(100% - (var(--container-padding) * 2));
    z-index: 2;
    pointer-events: none;
}

.tile {
    position: absolute;
    width: var(--tile-size);
    height: var(--tile-size);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 800;
    font-size: 2.2rem;
    scale: 1;
    /* CSS GPU 硬體加速過渡動畫 */
    transition: transform 120ms ease-in-out;
    animation: spawn 180ms ease-out;
}

/* 經典 2048 配色樣式 */
.tile-2 { background: #eee4da; color: var(--text-dark); font-size: 2.4rem; }
.tile-4 { background: #ede0c8; color: var(--text-dark); font-size: 2.4rem; }
.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: 1.8rem; box-shadow: 0 0 10px rgba(237, 207, 114, 0.4); }
.tile-256 { background: #5cbf7a; color: var(--text-light); font-size: 1.8rem; box-shadow: 0 0 14px rgba(92, 191, 122, 0.5); }
.tile-512 { background: #52a3f7; color: var(--text-light); font-size: 1.8rem; box-shadow: 0 0 18px rgba(82, 163, 247, 0.6); }
.tile-1024 { background: #af7af2; color: var(--text-light); font-size: 1.5rem; box-shadow: 0 0 22px rgba(175, 122, 242, 0.7); }
.tile-2048 { 
    background: #edc22e; 
    color: var(--text-light); 
    font-size: 1.5rem; 
    box-shadow: 0 0 28px rgba(237, 194, 46, 0.9);
    animation: pulse 1.5s infinite alternate; 
}
/* 2048 以上方塊 */
.tile-super { background: #3c3a32; color: var(--text-light); font-size: 1.5rem; }

/* 合併時的彈出 (Pop) 動化 */
.tile-merged {
    animation: pop 140ms ease-out;
}

/* 4x4 方塊坐標轉換 */
.tile.tile-position-1-1 { transform: translate(0, 0); }
.tile.tile-position-2-1 { transform: translate(calc(var(--tile-size) + var(--tile-gap)), 0); }
.tile.tile-position-3-1 { transform: translate(calc((var(--tile-size) + var(--tile-gap)) * 2), 0); }
.tile.tile-position-4-1 { transform: translate(calc((var(--tile-size) + var(--tile-gap)) * 3), 0); }

.tile.tile-position-1-2 { transform: translate(0, calc(var(--tile-size) + var(--tile-gap))); }
.tile.tile-position-2-2 { transform: translate(calc(var(--tile-size) + var(--tile-gap)), calc(var(--tile-size) + var(--tile-gap))); }
.tile.tile-position-3-2 { transform: translate(calc((var(--tile-size) + var(--tile-gap)) * 2), calc(var(--tile-size) + var(--tile-gap))); }
.tile.tile-position-4-2 { transform: translate(calc((var(--tile-size) + var(--tile-gap)) * 3), calc(var(--tile-size) + var(--tile-gap))); }

.tile.tile-position-1-3 { transform: translate(0, calc((var(--tile-size) + var(--tile-gap)) * 2)); }
.tile.tile-position-2-3 { transform: translate(calc(var(--tile-size) + var(--tile-gap)), calc((var(--tile-size) + var(--tile-gap)) * 2)); }
.tile.tile-position-3-3 { transform: translate(calc((var(--tile-size) + var(--tile-gap)) * 2), calc((var(--tile-size) + var(--tile-gap)) * 2)); }
.tile.tile-position-4-3 { transform: translate(calc((var(--tile-size) + var(--tile-gap)) * 3), calc((var(--tile-size) + var(--tile-gap)) * 2)); }

.tile.tile-position-1-4 { transform: translate(0, calc((var(--tile-size) + var(--tile-gap)) * 3)); }
.tile.tile-position-2-4 { transform: translate(calc(var(--tile-size) + var(--tile-gap)), calc((var(--tile-size) + var(--tile-gap)) * 3)); }
.tile.tile-position-3-4 { transform: translate(calc((var(--tile-size) + var(--tile-gap)) * 2), calc((var(--tile-size) + var(--tile-gap)) * 3)); }
.tile.tile-position-4-4 { transform: translate(calc((var(--tile-size) + var(--tile-gap)) * 3), calc((var(--tile-size) + var(--tile-gap)) * 3)); }

/* 覆蓋畫面（遊戲結束或贏得大獎） */
.overlay-screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(238, 228, 218, 0.73);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 12px;
    z-index: 10;
    animation: fadeIn 0.4s ease-out;
}

.overlay-content {
    text-align: center;
}

.overlay-content h2 {
    font-size: 2.2rem;
    font-weight: 800;
    color: var(--text-dark);
    margin-bottom: 8px;
}

.overlay-content p {
    font-size: 0.95rem;
    color: #8f7a66;
    margin-bottom: 20px;
    font-weight: 600;
}

.primary-btn {
    background: #8f7a66;
    color: #ffffff;
    border: none;
    border-radius: 6px;
    padding: 10px 24px;
    font-size: 0.95rem;
    font-weight: 800;
    cursor: pointer;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
    transition: background-color 0.2s ease, transform 0.1s ease;
}

.primary-btn:hover {
    background: #7f6a56;
}

.primary-btn:active {
    transform: scale(0.97);
}

.game-instruction {
    font-size: 0.72rem;
    color: #8f7a66;
    line-height: 1.5;
    text-align: center;
    border-top: 1px solid #e5dec9;
    padding-top: 12px;
    margin-top: 8px;
}

.hidden {
    display: none !important;
}

/* ==========================================
   動畫效果 keyframes
   ========================================== */
@keyframes spawn {
    0% { scale: 0; opacity: 0; }
    100% { scale: 1; opacity: 1; }
}

@keyframes pop {
    0% { scale: 1; }
    50% { scale: 1.25; }
    100% { scale: 1; }
}

@keyframes pulse {
    0% { box-shadow: 0 0 15px rgba(237, 194, 46, 0.7); }
    100% { box-shadow: 0 0 35px rgba(237, 194, 46, 0.95); }
}

@keyframes fadeIn {
    0% { opacity: 0; }
    100% { opacity: 1; }
}

/* ==========================================
   行動端與平板媒體查詢響應式適配
   ========================================== */
@media (max-width: 520px) {
    :root {
        /* 動態計算寬度，留出 32px padding 空間 */
        --tile-size: calc((100vw - 32px - (var(--container-padding) * 2) - (10px * 3)) / 4);
        --tile-gap: 8px;
        --container-padding: 8px;
    }
    
    .app-container {
        padding: 8px 12px;
    }

    .logo-text {
        font-size: 2.2rem;
    }

    .subtitle-text {
        font-size: 0.7rem;
    }

    .score-box {
        padding: 4px 8px;
        min-width: 55px;
    }

    .box-val {
        font-size: 0.95rem;
    }

    .action-btn {
        padding: 5px 12px;
        font-size: 0.78rem;
    }
    
    .grid-cell {
        border-radius: 6px;
    }
    .tile {
        border-radius: 6px;
    }
    .leaderboard-section {
        padding: 6px 8px;
        margin-top: 4px;
    }
    .leaderboard-table {
        font-size: 0.75rem;
    }
    .leaderboard-table th, .leaderboard-table td {
        padding: 2px 4px;
    }
    .game-instruction {
        padding-top: 6px;
        margin-top: 4px;
        font-size: 0.65rem;
    }
}

/* ==========================================
   排行榜與暱稱輸入 Modal 樣式
   ========================================== */
.leaderboard-section {
    background: var(--bg-container);
    border-radius: 8px;
    padding: 10px 14px;
    margin-top: 10px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
}

.leaderboard-header h3 {
    font-size: 0.9rem;
    color: #eee4da;
    font-weight: 800;
    margin-bottom: 8px;
    text-align: center;
    letter-spacing: 0.5px;
}

.leaderboard-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8rem;
    color: #ffffff;
}

.leaderboard-table th, .leaderboard-table td {
    padding: 5px 8px;
    text-align: center;
}

.leaderboard-table th {
    color: #eee4da;
    font-weight: 700;
    border-bottom: 1.5px solid rgba(238, 228, 218, 0.25);
}

.leaderboard-table td {
    border-bottom: 1px solid rgba(238, 228, 218, 0.12);
    font-weight: 600;
}

.leaderboard-table tr:last-child td {
    border-bottom: none;
}

.leaderboard-rank-1 { color: #f2b179; font-weight: 800; }
.leaderboard-rank-2 { color: #edcf72; font-weight: 800; }
.leaderboard-rank-3 { color: #eee4da; font-weight: 800; }

.modal-card {
    background: #faf8ef;
    padding: 24px;
    border-radius: 12px;
    width: 85%;
    max-width: 310px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
    border: 3px solid #bbada0;
    animation: pop 180ms ease-out;
}

#player-name-input {
    width: 100%;
    padding: 10px;
    border: 2px solid #bbada0;
    border-radius: 6px;
    font-size: 1rem;
    font-weight: 700;
    color: var(--text-dark);
    background: #ffffff;
    text-align: center;
    outline: none;
    transition: border-color 0.2s;
}

#player-name-input:focus {
    border-color: #8f7a66;
}

