/* === 2048 Game Styles === */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --bg: #faf8ef;
    --text: #776e65;
    --text-light: #eee4da;
    --tile-bg: #cdc1b4;
    --grid-bg: #bbada0;
    --btn-bg: #8f7a66;
    --btn-hover: #9f8b7a;
    --btn-primary: #f65e3b;
    --btn-primary-hover: #e55430;
    --radius: 8px;
    --container-max: 520px;
    --gap: 12px;
    --grid-pad: 8px;
}

body {
    font-family: 'Segoe UI', 'Clear Sans', 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    display: flex;
    justify-content: center;
    min-height: 100vh;
    padding: 16px;
}

.container {
    width: 100%;
    max-width: var(--container-max);
}

/* Header */
header {
    margin-bottom: 16px;
}

.title-row {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 10px;
}

h1 {
    font-size: 64px;
    font-weight: 800;
    line-height: 1;
    color: var(--text);
}

.scores-box {
    display: flex;
    gap: 8px;
}

.score-box {
    background: var(--grid-bg);
    border-radius: var(--radius);
    padding: 6px 18px;
    text-align: center;
    min-width: 80px;
}

.score-label {
    display: block;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    color: var(--text-light);
    letter-spacing: 1px;
}

.score-value {
    display: block;
    font-size: 22px;
    font-weight: 800;
    color: #fff;
}

.control-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
}

.tagline {
    font-size: 14px;
    color: var(--text);
}

.btn-group {
    display: flex;
    gap: 6px;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 10px 18px;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
    color: #f9f6f2;
    background: var(--btn-bg);
    transition: background 0.15s, transform 0.1s, opacity 0.15s;
}

.btn:hover {
    background: var(--btn-hover);
}

.btn:active {
    transform: scale(0.96);
}

.btn-primary {
    background: var(--btn-primary);
}

.btn-primary:hover {
    background: var(--btn-primary-hover);
}

.btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    transform: none;
}

/* Game Container - NO padding here, handled by the inner elements */
.game-container {
    position: relative;
    width: 100%;
    background: var(--grid-bg);
    border-radius: var(--radius);
    padding: var(--grid-pad);
    touch-action: none;
    margin-bottom: 20px;
    overflow: hidden;
}

/* Grid background — computed positions match JS */
.grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: var(--gap);
    width: 100%;
}

.cell {
    aspect-ratio: 1;
    background: rgba(238, 228, 218, 0.35);
    border-radius: 4px;
}

/* Tiles layer — exact pixel overlay on top of grid */
.tile-container {
    position: absolute;
    top: var(--grid-pad);
    left: var(--grid-pad);
    right: var(--grid-pad);
    bottom: var(--grid-pad);
    pointer-events: none;
    will-change: transform;
}

.tile {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 800;
    border-radius: 4px;
    z-index: 1;
    will-change: transform, left, top;
    backface-visibility: hidden;
}

.tile-new {
    animation: tile-appear 0.2s ease;
}

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

@keyframes tile-appear {
    0% {
        transform: scale(0);
        opacity: 0;
    }
    50% {
        transform: scale(1.1);
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

@keyframes tile-pop {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.18);
    }
    100% {
        transform: scale(1);
    }
}

/* Tile sizes - these are fallback; JS sets exact font-size */
.tile-2 { background: #eee4da; color: #776e65; }
.tile-4 { background: #ede0c8; color: #776e65; }
.tile-8 { background: #f2b179; color: #f9f6f2; }
.tile-16 { background: #f59563; color: #f9f6f2; }
.tile-32 { background: #f67c5f; color: #f9f6f2; }
.tile-64 { background: #f65e3b; color: #f9f6f2; }
.tile-128 { background: #edcf72; color: #f9f6f2; box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.24); }
.tile-256 { background: #edcc61; color: #f9f6f2; box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.32); }
.tile-512 { background: #edc850; color: #f9f6f2; box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.4); }
.tile-1024 { background: #edc53f; color: #f9f6f2; box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.5); }
.tile-2048 { background: #edc22e; color: #f9f6f2; box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.6); }
.tile-super { background: #3c3a32; color: #f9f6f2; }

/* Game Message Overlay */
.game-message {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(238, 228, 218, 0.73);
    border-radius: var(--radius);
    z-index: 20;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 24px;
    gap: 12px;
}

.game-message.show {
    display: flex;
}

.game-message p {
    font-size: 48px;
    font-weight: 800;
    color: var(--text);
}

.leaderboard-form {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    max-width: 320px;
    width: 100%;
}

.leaderboard-form.hidden {
    display: none;
}

.leaderboard-form label {
    font-size: 15px;
    font-weight: 600;
    color: var(--text);
}

.name-input-row {
    display: flex;
    gap: 8px;
    width: 100%;
}

.name-input-row input {
    flex: 1;
    padding: 10px 14px;
    border: 2px solid var(--tile-bg);
    border-radius: 6px;
    font-size: 16px;
    outline: none;
    background: #fff;
    color: var(--text);
}

.name-input-row input:focus {
    border-color: var(--btn-primary);
}

.name-input-row .btn {
    white-space: nowrap;
}

/* Leaderboard Section */
.leaderboard-section {
    background: #fff;
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    margin-bottom: 20px;
}

.leaderboard-section h2 {
    font-size: 20px;
    margin-bottom: 12px;
    color: var(--text);
}

.leaderboard-empty {
    color: #999;
    font-size: 14px;
    text-align: center;
    padding: 12px;
}

.leaderboard-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
}

.leaderboard-table th,
.leaderboard-table td {
    padding: 8px 10px;
    text-align: left;
    border-bottom: 1px solid #f0f0f0;
}

.leaderboard-table th {
    font-weight: 700;
    color: #999;
    text-transform: uppercase;
    font-size: 11px;
    letter-spacing: 0.5px;
}

.leaderboard-table th:nth-child(1),
.leaderboard-table td:nth-child(1) {
    width: 40px;
    text-align: center;
}

.leaderboard-table th:nth-child(3),
.leaderboard-table td:nth-child(3),
.leaderboard-table th:nth-child(4),
.leaderboard-table td:nth-child(4) {
    width: 80px;
    text-align: right;
}

.leaderboard-table td.rank {
    font-weight: 800;
    color: #bbb;
}

.leaderboard-table .rank-1 { color: #f5a623; }
.leaderboard-table .rank-2 { color: #9b9b9b; }
.leaderboard-table .rank-3 { color: #cd7f32; }

footer {
    text-align: center;
    color: #999;
    font-size: 13px;
    padding: 12px 0;
}

/* Responsive */
@media (max-width: 540px) {
    :root {
        --gap: 8px;
        --grid-pad: 6px;
    }
    body { padding: 8px; }
    h1 { font-size: 44px; }
    .score-box { padding: 4px 12px; min-width: 60px; }
    .score-value { font-size: 18px; }
    .game-message p { font-size: 32px; }
    .btn { padding: 8px 12px; font-size: 12px; }
}

@media (max-width: 380px) {
    .control-row { flex-direction: column; align-items: stretch; }
    .btn-group { justify-content: center; }
}
