* {
    box-sizing: border-box;
}

:root {
    --bg: #faf8ef;
    --text: #776e65;
    --board: #bbada0;
    --empty: #cdc1b4;
    --button: #8f7a66;
    --success: #3d8c40;
    --danger: #b23b3b;
    --info: #6d5c4b;
}

body {
    margin: 0;
    font-family: Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
}

.container {
    max-width: 440px;
    margin: 40px auto;
    padding: 16px;
}

.topbar {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 16px;
}

h1 {
    margin: 0;
    font-size: 48px;
    line-height: 1;
}

.subtitle {
    margin: 8px 0 0;
    font-size: 14px;
}

.score-wrapper {
    display: flex;
    gap: 10px;
    position: relative;
}

.score-box {
    position: relative;
    background: var(--board);
    color: white;
    padding: 10px 16px;
    border-radius: 8px;
    text-align: center;
    min-width: 96px;
}

.score-box span {
    display: block;
    font-size: 12px;
    text-transform: uppercase;
}

.score-box strong {
    font-size: 22px;
}

.score-gain {
    position: absolute;
    right: 10px;
    top: 8px;
    font-size: 18px;
    font-weight: bold;
    color: #fff7d6;
    opacity: 0;
    pointer-events: none;
    transform: translateY(0);
}

.score-gain.show {
    animation: floatUp 700ms ease-out;
}

.status-area {
    min-height: 42px;
    margin-bottom: 12px;
}

.status-message {
    padding: 10px 12px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: bold;
    opacity: 1;
    transition: opacity 180ms ease;
}

.status-message.hidden {
    opacity: 0;
    visibility: hidden;
}

.status-message.info {
    background: #eee4da;
    color: var(--info);
}

.status-message.success {
    background: #d9f0db;
    color: var(--success);
}

.status-message.danger {
    background: #f6dada;
    color: var(--danger);
}

.board-wrapper {
    position: relative;
}

.board {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
    background: var(--board);
    padding: 10px;
    border-radius: 10px;
    margin-bottom: 16px;
}

.board.shake {
    animation: shake 220ms ease-in-out;
}

.tile {
    width: 100%;
    aspect-ratio: 1 / 1;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    font-weight: bold;
    background: var(--empty);
    color: var(--text);
    user-select: none;
    transform-origin: center;
    transition: transform 120ms ease, background 120ms ease, color 120ms ease;
}

.tile-0 {
    background: var(--empty);
    color: transparent;
}

.tile-2 {
    background: #eee4da;
}

.tile-4 {
    background: #ede0c8;
}

.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;
    font-size: 24px;
}

.tile-256 {
    background: #edcc61;
    color: #f9f6f2;
    font-size: 24px;
}

.tile-512 {
    background: #edc850;
    color: #f9f6f2;
    font-size: 24px;
}

.tile-1024 {
    background: #edc53f;
    color: #f9f6f2;
    font-size: 20px;
}

.tile-2048 {
    background: #edc22e;
    color: #f9f6f2;
    font-size: 20px;
}

.tile-4096,
.tile-8192,
.tile-16384 {
    background: #3c3a32;
    color: #f9f6f2;
    font-size: 20px;
}

.tile-new {
    animation: popIn 180ms ease-out;
}

.tile-merged {
    animation: mergePulse 220ms ease-out;
}

.controls {
    display: flex;
    gap: 10px;
    margin-bottom: 12px;
}

button {
    flex: 1;
    padding: 12px;
    border: none;
    border-radius: 8px;
    background: var(--button);
    color: white;
    font-size: 16px;
    cursor: pointer;
    transition: transform 120ms ease, opacity 120ms ease;
}

button:hover {
    opacity: 0.95;
}

button:active {
    transform: scale(0.98);
}

.help {
    font-size: 14px;
}

@keyframes popIn {
    0% {
        transform: scale(0.65);
        opacity: 0.4;
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

@keyframes mergePulse {
    0% {
        transform: scale(0.95);
    }
    50% {
        transform: scale(1.12);
    }
    100% {
        transform: scale(1);
    }
}

@keyframes shake {
    0% { transform: translateX(0); }
    25% { transform: translateX(-6px); }
    50% { transform: translateX(6px); }
    75% { transform: translateX(-4px); }
    100% { transform: translateX(0); }
}

@keyframes floatUp {
    0% {
        opacity: 0;
        transform: translateY(8px);
    }
    20% {
        opacity: 1;
    }
    100% {
        opacity: 0;
        transform: translateY(-18px);
    }
}

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    z-index: 1000;
}

.modal-overlay.hidden {
    display: none;
}

.hidden {
    display: none !important;
}

.modal {
    width: 100%;
    max-width: 360px;
    background: #fffaf3;
    color: var(--text);
    border-radius: 12px;
    padding: 24px 20px;
    box-shadow: 0 16px 40px rgba(0, 0, 0, 0.18);
    text-align: center;
}

.modal h2 {
    margin: 0 0 12px;
    font-size: 28px;
}

.modal p {
    margin: 0 0 20px;
    font-size: 16px;
    line-height: 1.4;
    white-space: pre-line;
}

.modal-actions {
    display: flex;
    gap: 10px;
}

.modal-actions button {
    flex: 1;
}

button.secondary {
    background: #b6a89a;
}