/* 2048 Game Styles */
.game-2048-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 20px;
    max-width: 600px;
    margin: 0 auto;
    margin: 0 auto;
    font-family: 'Clear Sans', 'Helvetica Neue', Arial, sans-serif;
    overflow-y: auto;
    /* Allow scrolling if it doesn't fit */
    height: 100%;
    justify-content: flex-start;
    /* Align from top */
}

.game-header {
    display: flex;
    justify-content: space-between;
    width: 100%;
    margin-bottom: 20px;
    align-items: center;
}

.game-title {
    font-size: 3rem;
    font-weight: bold;
    color: #776e65;
    margin: 0;
}

.dark-mode .game-title {
    color: #f9f6f2;
}

.scores-container {
    display: flex;
    gap: 10px;
}

.score-box {
    background: #bbada0;
    padding: 10px 20px;
    border-radius: 6px;
    color: white;
    text-align: center;
    min-width: 80px;
    position: relative;
}

.score-box .score-label {
    font-size: 0.8rem;
    text-transform: uppercase;
    color: #eee4da;
    display: block;
}

.score-box .score-value {
    font-size: 1.5rem;
    font-weight: bold;
    display: block;
}

.score-animation {
    position: absolute;
    right: 30px;
    top: 10px;
    font-size: 25px;
    font-weight: bold;
    color: rgba(119, 110, 101, 0.9);
    z-index: 100;
    animation: move-up-fade 600ms ease-in;
    pointer-events: none;
}

@keyframes move-up-fade {
    0% {
        opacity: 1;
        top: 25px;
    }

    100% {
        opacity: 0;
        top: -50px;
    }
}


.game-controls-bar {
    width: 100%;
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
}

.game-intro {
    font-size: 1.1rem;
    color: #776e65;
}

.dark-mode .game-intro {
    color: #eee4da;
}

.restart-btn {
    background: #8f7a66;
    color: white;
    border: none;
    border-radius: 3px;
    padding: 0 20px;
    font-weight: bold;
    cursor: pointer;
    font-size: 1rem;
    height: 40px;
    transition: background 0.3s;
}

.restart-btn:hover {
    background: #7f6a56;
}

/* Grid Board */
.game-board {
    position: relative;
    padding: 15px;
    background: #bbada0;
    border-radius: 6px;
    width: 380px;
    height: 380px;
    box-sizing: border-box;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: 15px;
    touch-action: none;
    /* Prevent scrolling on mobile */
}

.grid-cell {
    width: 100%;
    height: 100%;
    background: rgba(238, 228, 218, 0.35);
    border-radius: 3px;
}

.tile-container {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    padding: 15px;
    pointer-events: none;
    /* Allow clicks to pass through if needed, though mostly visual */
}

.tile {
    position: absolute;
    width: 72.5px;
    /* (380 - 15*5) / 4 = 76.25 ... roughly. Let's use calc */
    width: calc((100% - 15px * 5) / 4);
    height: calc((100% - 15px * 5) / 4);
    border-radius: 3px;
    background: #eee4da;
    color: #776e65;
    font-weight: bold;
    text-align: center;
    font-size: 35px;
    line-height: 72.5px;
    /* Match height rough estimate */
    line-height: 2.1;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: transform 100ms ease-in-out, opacity 100ms ease-in-out;
    z-index: 10;
}

/* Position mapping */
/* We will use CSS variables or direct style injection for positions in JS for smoother sliding.
   Alternatively, predefined classes for positions. 
   4x4 grid. 
   Pos (row, col) 0-3.
   Top/Left offset = 15px + (row/col) * (cell_size + 15px)
*/

.tile-inner {
    width: 100%;
    height: 100%;
    border-radius: 3px;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* Tile Colors */
.tile-2 .tile-inner {
    background: #eee4da;
    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0), inset 0 0 0 1px rgba(255, 255, 255, 0);
}

.tile-4 .tile-inner {
    background: #ede0c8;
    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0), inset 0 0 0 1px rgba(255, 255, 255, 0);
}

.tile-8 .tile-inner {
    color: #f9f6f2;
    background: #f2b179;
}

.tile-16 .tile-inner {
    color: #f9f6f2;
    background: #f59563;
}

.tile-32 .tile-inner {
    color: #f9f6f2;
    background: #f67c5f;
}

.tile-64 .tile-inner {
    color: #f9f6f2;
    background: #f65e3b;
}

.tile-128 .tile-inner {
    color: #f9f6f2;
    background: #edcf72;
    font-size: 30px;
    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.2381), inset 0 0 0 1px rgba(255, 255, 255, 0.14286);
}

.tile-256 .tile-inner {
    color: #f9f6f2;
    background: #edcc61;
    font-size: 30px;
    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.31746), inset 0 0 0 1px rgba(255, 255, 255, 0.19048);
}

.tile-512 .tile-inner {
    color: #f9f6f2;
    background: #edc850;
    font-size: 30px;
    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.39683), inset 0 0 0 1px rgba(255, 255, 255, 0.2381);
}

.tile-1024 .tile-inner {
    color: #f9f6f2;
    background: #edc53f;
    font-size: 25px;
    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.47619), inset 0 0 0 1px rgba(255, 255, 255, 0.28571);
}

.tile-2048 .tile-inner {
    color: #f9f6f2;
    background: #edc22e;
    font-size: 25px;
    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.55556), inset 0 0 0 1px rgba(255, 255, 255, 0.33333);
}

.tile-super .tile-inner {
    color: #f9f6f2;
    background: #3c3a32;
    font-size: 20px;
}

/* New & Merged Animations */
.tile-new .tile-inner {
    animation: appear 200ms ease;
}

@keyframes appear {
    0% {
        opacity: 0;
        transform: scale(0);
    }

    100% {
        opacity: 1;
        transform: scale(1);
    }
}

.tile-merged .tile-inner {
    z-index: 20;
    animation: pop 200ms ease;
}

@keyframes pop {
    0% {
        transform: scale(1);
    }

    50% {
        transform: scale(1.2);
    }

    100% {
        transform: scale(1);
    }
}

/* GameOver / Win Overlay */
.game-message {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(238, 228, 218, 0.5);
    z-index: 100;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    display: none;
    border-radius: 6px;
    animation: fade-in 800ms ease 1200ms;
    animation-fill-mode: both;
}

.game-message.game-over {
    display: flex;
    background: rgba(238, 228, 218, 0.73);
}

.game-message.game-won {
    display: flex;
    background: rgba(237, 194, 46, 0.5);
    color: #f9f6f2;
}

.dark-mode .game-message {
    background: rgba(45, 55, 72, 0.8);
    color: #eee4da;
}

.game-message p {
    font-size: 60px;
    font-weight: bold;
    margin: 0 0 20px;
    line-height: 60px;
}

.game-message .actions {
    display: flex;
    gap: 10px;
}

/* Responsive */
@media screen and (max-width: 520px) {
    .game-board {
        width: 280px;
        height: 280px;
        padding: 10px;
        gap: 10px;
    }

    .tile {
        width: calc((100% - 10px * 5) / 4);
        height: calc((100% - 10px * 5) / 4);
        line-height: calc((280px - 10px * 5) / 4);
    }

    .game-title {
        font-size: 2rem;
    }

    .game-intro {
        font-size: 0.9rem;
    }

    .tile .tile-inner {
        font-size: 25px;
    }

    .tile-128 .tile-inner,
    .tile-256 .tile-inner,
    .tile-512 .tile-inner {
        font-size: 20px;
    }

    .tile-1024 .tile-inner,
    .tile-2048 .tile-inner {
        font-size: 15px;
    }
}

/* Leaderboard Modal */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    display: none;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(2px);
    opacity: 0;
    transition: opacity 0.3s ease;
}

.modal-overlay.active {
    display: flex;
    opacity: 1;
}

.modal-content {
    background: white;
    width: 90%;
    max-width: 500px;
    border-radius: 12px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
    overflow: hidden;
    transform: translateY(20px);
    transition: transform 0.3s ease;
}

.dark-mode .modal-content {
    background: #1a202c;
    color: #fff;
}

.modal-overlay.active .modal-content {
    transform: translateY(0);
}

.modal-header {
    padding: 15px 20px;
    background: #f7fafc;
    border-bottom: 1px solid #edf2f7;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.dark-mode .modal-header {
    background: #2d3748;
    border-bottom-color: #4a5568;
}

.modal-header h3 {
    margin: 0;
    color: #2d3748;
    font-size: 1.2rem;
}

.dark-mode .modal-header h3 {
    color: #fff;
}

.close-btn {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: #718096;
}

.dark-mode .close-btn {
    color: #a0aec0;
}

.modal-body {
    padding: 20px;
    max-height: 70vh;
    overflow-y: auto;
}

.leaderboard-table {
    width: 100%;
    border-collapse: collapse;
}

.leaderboard-table th {
    padding: 10px;
    text-align: left;
    border-bottom: 2px solid #edf2f7;
    font-weight: 600;
    color: #718096;
    background: #f8fafc;
}

.leaderboard-table td {
    padding: 10px;
    text-align: left;
    border-bottom: 1px solid #edf2f7;
    color: #2d3748;
}

.dark-mode .leaderboard-table th {
    background: #2d3748;
    color: #cbd5e0;
    border-bottom-color: #4a5568;
}

.dark-mode .leaderboard-table td {
    color: #e2e8f0;
    border-bottom-color: #4a5568;
}

.leaderboard-table tr:last-child td {
    border-bottom: none;
}