:root {
    --bg-gradient: linear-gradient(135deg, #0f172a 0%, #1e1b4b 100%);
    --panel-bg: rgba(30, 41, 59, 0.7);
    --board-bg: #090d16;
    --cell-bg: rgba(255, 255, 255, 0.05);
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --btn-bg: #3b82f6;
    --btn-hover: #2563eb;
    --btn-text: #ffffff;
    --shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.3), 0 10px 10px -5px rgba(0, 0, 0, 0.2);
    --border-radius: 16px;
    --tile-radius: 10px;
    
    /* Tile Colors Mapping */
    --tile-2-bg: #eee4da; --tile-2-color: #776e65;
    --tile-4-bg: #ede0c8; --tile-4-color: #776e65;
    --tile-8-bg: #f2b179; --tile-8-color: #f9f6f2;
    --tile-16-bg: #f59563; --tile-16-color: #f9f6f2;
    --tile-32-bg: #f67c5f; --tile-32-color: #f9f6f2;
    --tile-64-bg: #f65e3b; --tile-64-color: #f9f6f2;
    --tile-128-bg: #edcf72; --tile-128-color: #f9f6f2;
    --tile-256-bg: #edcc61; --tile-256-color: #f9f6f2;
    --tile-512-bg: #edc850; --tile-512-color: #f9f6f2;
    --tile-1024-bg: #edc53f; --tile-1024-color: #f9f6f2;
    --tile-2048-bg: linear-gradient(135deg, #facc15 0%, #eab308 100%); --tile-2048-color: #ffffff;
    --tile-super-bg: #333333; --tile-super-color: #ffffff;
}

[data-theme="light"] {
    --bg-gradient: linear-gradient(135deg, #f1f5f9 0%, #cbd5e1 100%);
    --panel-bg: rgba(255, 255, 255, 0.8);
    --board-bg: #bbada0;
    --cell-bg: rgba(238, 228, 218, 0.35);
    --text-main: #334155;
    --text-muted: #64748b;
    --btn-bg: #475569;
    --btn-hover: #334155;
    --shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.05);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    user-select: none;
    -webkit-user-select: none;
}

body {
    background: var(--bg-gradient);
    color: var(--text-main);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow-x: hidden;
    transition: background 0.3s ease, color 0.3s ease;
}

.game-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    max-width: 500px;
    padding: 20px;
}

/* Header Styles */
.header {
    width: 100%;
    margin-bottom: 20px;
}

.header-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
}

.title {
    font-size: 2.8rem;
    font-weight: 800;
    letter-spacing: -1px;
    line-height: 1;
}

.subtitle {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-top: 4px;
}

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

.score-box {
    background: var(--panel-bg);
    padding: 8px 14px;
    border-radius: var(--tile-radius);
    display: flex;
    flex-direction: column;
    align-items: center;
    min-width: 70px;
    box-shadow: var(--shadow);
    backdrop-filter: blur(8px);
    position: relative;
}

.score-label {
    font-size: 0.65rem;
    font-weight: 700;
    color: var(--text-muted);
    letter-spacing: 0.5px;
}

.score-value {
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--text-main);
}

.score-increment {
    position: absolute;
    top: -10px;
    right: 50%;
    transform: translateX(50%);
    color: #10b981;
    font-weight: bold;
    font-size: 0.9rem;
    opacity: 0;
    pointer-events: none;
    animation: floatUp 0.8s ease-out forwards;
}

@keyframes floatUp {
    0% { opacity: 1; transform: translate(50%, 0); }
    100% { opacity: 0; transform: translate(50%, -25px); }
}

.toolbar {
    display: flex;
    gap: 10px;
    align-items: center;
    justify-content: flex-end;
}

.btn {
    border: none;
    cursor: pointer;
    border-radius: var(--tile-radius);
    font-weight: 600;
    transition: transform 0.1s ease, background 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

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

.primary-btn {
    background: var(--btn-bg);
    color: var(--btn-text);
    padding: 10px 20px;
    font-size: 0.95rem;
    box-shadow: var(--shadow);
}

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

.secondary-btn {
    background: #64748b;
    color: white;
    padding: 10px 20px;
    font-size: 0.95rem;
}

.secondary-btn:hover {
    background: #475569;
}

.icon-btn {
    background: var(--panel-bg);
    color: var(--text-main);
    width: 40px;
    height: 40px;
    backdrop-filter: blur(8px);
    box-shadow: var(--shadow);
}

.icon-btn svg {
    width: 20px;
    height: 20px;
}

/* Theme Icon switching */
[data-theme="dark"] .moon-icon,
[data-theme="light"] .sun-icon {
    display: none;
}

/* Sound Icon switching */
.sound-off { display: none; }
body.muted .sound-on { display: none; }
body.muted .sound-off { display: block; }


/* Main Content & Board */
.main-content {
    position: relative;
    width: 100%;
}

.board-container {
    position: relative;
    background: var(--board-bg);
    padding: 12px;
    border-radius: var(--border-radius);
    width: 100%;
    aspect-ratio: 1 / 1;
    box-shadow: var(--shadow);
}

.grid-container {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: 12px;
    width: 100%;
    height: 100%;
}

.grid-cell {
    background: var(--cell-bg);
    border-radius: var(--tile-radius);
    width: 100%;
    height: 100%;
}

.tile-container {
    position: absolute;
    top: 12px;
    left: 12px;
    right: 12px;
    bottom: 12px;
    pointer-events: none;
}

/* Tiles */
.tile {
    position: absolute;
    width: calc((100% - 36px) / 4);
    height: calc((100% - 36px) / 4);
    background: #eee4da;
    border-radius: var(--tile-radius);
    display: flex;
    justify-content: center;
    align-items: center;
    font-weight: 800;
    font-size: 1.8rem;
    transition: transform 0.15s ease-in-out, background-color 0.15s ease-in-out;
    box-shadow: 0 4px 10px rgba(0,0,0,0.1);
}

/* Dynamic Tile Positioning (Calculated via JS or dynamic classes) */
.tile-2 { background: var(--tile-2-bg); color: var(--tile-2-color); }
.tile-4 { background: var(--tile-4-bg); color: var(--tile-4-color); }
.tile-8 { background: var(--tile-8-bg); color: var(--tile-8-color); }
.tile-16 { background: var(--tile-16-bg); color: var(--tile-16-color); font-size: 1.6rem; }
.tile-32 { background: var(--tile-32-bg); color: var(--tile-32-color); font-size: 1.6rem; }
.tile-64 { background: var(--tile-64-bg); color: var(--tile-64-color); font-size: 1.6rem; }
.tile-128 { background: var(--tile-128-bg); color: var(--tile-128-color); font-size: 1.4rem; }
.tile-256 { background: var(--tile-256-bg); color: var(--tile-256-color); font-size: 1.4rem; }
.tile-512 { background: var(--tile-512-bg); color: var(--tile-512-color); font-size: 1.4rem; }
.tile-1024 { background: var(--tile-1024-bg); color: var(--tile-1024-color); font-size: 1.2rem; }
.tile-2048 { background: var(--tile-2048-bg); color: var(--tile-2048-color); font-size: 1.2rem; box-shadow: 0 0 20px rgba(234, 179, 8, 0.6); }
.tile-super { background: var(--tile-super-bg); color: var(--tile-super-color); font-size: 1rem; }

/* Tile Animations */
.tile-new {
    animation: appear 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

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

.tile-merged {
    animation: pop 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

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

/* Game Message Overlays */
.game-message {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(15, 23, 42, 0.85);
    backdrop-filter: blur(6px);
    border-radius: var(--border-radius);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease-in-out;
    z-index: 100;
}

[data-theme="light"] .game-message {
    background: rgba(241, 245, 249, 0.85);
}

.game-message.show {
    opacity: 1;
    pointer-events: auto;
}

.game-message p {
    font-size: 2.2rem;
    font-weight: 800;
    margin-bottom: 20px;
    color: var(--text-main);
    padding: 0 20px;
}

.message-actions {
    display: flex;
    gap: 12px;
}

.hidden {
    display: none !important;
}

/* Footer Info */
.footer {
    margin-top: 20px;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.8rem;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.copyright {
    font-size: 0.75rem;
    opacity: 0.7;
}

/* Mobile Adjustments */
@media (max-width: 480px) {
    .game-container {
        padding: 10px;
    }
    .title {
        font-size: 2.2rem;
    }
    .tile {
        font-size: 1.4rem;
    }
    .tile-16, .tile-32, .tile-64 { font-size: 1.2rem; }
    .tile-128, .tile-256, .tile-512 { font-size: 1rem; }
    .tile-1024, .tile-2048 { font-size: 0.85rem; }
}