* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    /* Light Theme (Vibrant) */
    --bg-gradient: linear-gradient(135deg, #ff6b9d 0%, #c44569 25%, #f8b500 50%, #ff3838 75%, #ff6b9d 100%);
    --card-bg: rgba(255, 255, 255, 0.25);
    --card-border: rgba(255, 255, 255, 0.3);
    --text-primary: #ffffff;
    --text-secondary: rgba(255, 255, 255, 0.95);
    --text-tertiary: rgba(255, 255, 255, 0.85);
    --text-contrast: #2d3436;
    --timer-bg: rgba(255, 255, 255, 0.2);
    --timer-fill: linear-gradient(90deg, #00ff88, #00cc6a);
    --tile-empty: rgba(255, 255, 255, 0.15);
    --btn-primary: linear-gradient(135deg, #00d4aa, #01a3a4);
    --btn-secondary: linear-gradient(135deg, #ff6b9d, #ee5a6f);
    --btn-theme: linear-gradient(135deg, #ffa726, #ff7043);
    --modal-bg: linear-gradient(135deg, #667eea, #764ba2);
    --glow-color: rgba(255, 107, 157, 0.6);
    --lives-bg: rgba(255, 255, 255, 0.9);
    --lives-text: #2d3436;
    --level-bg: rgba(255, 255, 255, 0.9);
    --level-text: #2d3436;
}

[data-theme="dark"] {
    /* Dark Theme (Vibrant) */
    --bg-gradient: linear-gradient(135deg, #0f0f23 0%, #1a1a2e 25%, #16213e 50%, #0f3460 75%, #533483 100%);
    --card-bg: rgba(0, 0, 0, 0.4);
    --card-border: rgba(255, 255, 255, 0.1);
    --text-primary: #ffffff;
    --text-secondary: rgba(255, 255, 255, 0.9);
    --text-tertiary: rgba(255, 255, 255, 0.7);
    --text-contrast: #ffffff;
    --timer-bg: rgba(0, 0, 0, 0.3);
    --timer-fill: linear-gradient(90deg, #00ff88, #00cc6a);
    --tile-empty: rgba(255, 255, 255, 0.08);
    --btn-primary: linear-gradient(135deg, #00d4aa, #01a3a4);
    --btn-secondary: linear-gradient(135deg, #ff6b9d, #ee5a6f);
    --btn-theme: linear-gradient(135deg, #ffa726, #ff7043);
    --modal-bg: linear-gradient(135deg, #1a1a2e, #16213e);
    --glow-color: rgba(0, 255, 136, 0.4);
    --lives-bg: rgba(255, 255, 255, 0.15);
    --lives-text: #ffffff;
    --level-bg: rgba(0, 0, 0, 0.8);
    --level-text: #ffffff;
}

body {
    font-family: 'Arial', sans-serif;
    background: var(--bg-gradient);
    min-height: 100vh;
    min-height: 100dvh;
    overflow-x: hidden;
    position: fixed;
    width: 100%;
    height: 100%;
    touch-action: manipulation;
    transition: all 0.3s ease;
}

.game-container {
    max-width: 400px;
    margin: 0 auto;
    padding: 10px;
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    position: relative;
}

.header {
    text-align: center;
    color: var(--text-primary);
    margin-bottom: 15px;
    padding-top: env(safe-area-inset-top, 10px);
}

.title {
    font-size: 2.5em;
    font-weight: bold;
    text-shadow: 2px 2px 8px rgba(0,0,0,0.5);
    margin-bottom: 5px;
    background: linear-gradient(45deg, #ff6b9d, #ffa726, #00d4aa, #667eea);
    background-size: 400% 400%;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    animation: gradientShift 3s ease-in-out infinite;
}

@keyframes gradientShift {
    0%, 100% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
}

.stats {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--card-bg);
    backdrop-filter: blur(15px);
    border-radius: 15px;
    padding: 10px 15px;
    margin-bottom: 15px;
    border: 1px solid var(--card-border);
    box-shadow: 0 8px 32px rgba(0,0,0,0.2);
}

.stat-item {
    text-align: center;
    color: var(--text-primary);
}

.stat-label {
    font-size: 0.8em;
    color: var(--text-tertiary);
}

.stat-value {
    font-size: 1.2em;
    font-weight: bold;
    color: var(--text-primary);
}

.lives {
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--lives-bg);
    color: var(--lives-text);
    padding: 5px 12px;
    border-radius: 20px;
    font-weight: bold;
    font-size: 1.1em;
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
    border: 2px solid rgba(255,255,255,0.3);
    min-width: 45px;
}

.lives-icon {
    margin-right: 5px;
    font-size: 1.2em;
}

.timer-bar {
    height: 8px;
    background: var(--timer-bg);
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: 15px;
    box-shadow: inset 0 2px 4px rgba(0,0,0,0.2);
}

.timer-fill {
    height: 100%;
    background: var(--timer-fill);
    border-radius: 4px;
    transition: width 0.1s linear;
    box-shadow: 0 0 15px var(--glow-color);
}

.game-board {
    background: var(--card-bg);
    backdrop-filter: blur(20px);
    border-radius: 20px;
    padding: 15px;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: 10px;
    aspect-ratio: 1;
    box-shadow: 0 20px 40px rgba(0,0,0,0.2);
    position: relative;
    margin-bottom: 15px;
    border: 1px solid var(--card-border);
}

.tile {
    background: var(--tile-empty);
    border-radius: 15px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.8em;
    font-weight: bold;
    color: white;
    transition: all 0.15s ease-in-out;
    position: relative;
    overflow: hidden;
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

.tile::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(45deg, transparent 0%, rgba(255,255,255,0.1) 50%, transparent 100%);
    transform: translateX(-100%);
    transition: transform 0.6s;
}

.tile:hover::before {
    transform: translateX(100%);
}

.tile-2 { background: linear-gradient(135deg, #ff6b9d, #ff5983); box-shadow: 0 0 20px rgba(255, 107, 157, 0.4); }
.tile-4 { background: linear-gradient(135deg, #4ecdc4, #26d0ce); box-shadow: 0 0 20px rgba(78, 205, 196, 0.4); }
.tile-8 { background: linear-gradient(135deg, #45b7d1, #0abde3); box-shadow: 0 0 20px rgba(69, 183, 209, 0.4); }
.tile-16 { background: linear-gradient(135deg, #ffa726, #ff9f43); box-shadow: 0 0 20px rgba(255, 167, 38, 0.4); }
.tile-32 { background: linear-gradient(135deg, #a29bfe, #6c5ce7); box-shadow: 0 0 20px rgba(162, 155, 254, 0.4); }
.tile-64 { background: linear-gradient(135deg, #fd79a8, #e84393); box-shadow: 0 0 20px rgba(253, 121, 168, 0.4); }
.tile-128 { background: linear-gradient(135deg, #00b894, #00cec9); box-shadow: 0 0 25px rgba(0, 184, 148, 0.5); }
.tile-256 { background: linear-gradient(135deg, #e17055, #d63031); box-shadow: 0 0 25px rgba(225, 112, 85, 0.5); }
.tile-512 { background: linear-gradient(135deg, #0984e3, #74b9ff); box-shadow: 0 0 25px rgba(9, 132, 227, 0.5); }
.tile-1024 { background: linear-gradient(135deg, #fdcb6e, #e17055); font-size: 1.4em; box-shadow: 0 0 30px rgba(253, 203, 110, 0.6); }
.tile-2048 { background: linear-gradient(135deg, #fd79a8, #6c5ce7); font-size: 1.4em; animation: glow 2s ease-in-out infinite; box-shadow: 0 0 35px rgba(253, 121, 168, 0.8); }
.tile-4096 { background: linear-gradient(135deg, #00ff88, #00d4aa); font-size: 1.2em; animation: glow 2s ease-in-out infinite; box-shadow: 0 0 40px rgba(0, 255, 136, 0.8); }
.tile-8192 { background: linear-gradient(135deg, #ff3838, #ff6b9d); font-size: 1.1em; animation: glow 1.5s ease-in-out infinite; box-shadow: 0 0 45px rgba(255, 56, 56, 0.9); }

@keyframes glow {
    0%, 100% { box-shadow: 0 0 20px var(--glow-color); transform: scale(1); }
    50% { box-shadow: 0 0 40px var(--glow-color); transform: scale(1.05); }
}

.controls {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 10px;
}

.btn {
    flex: 1;
    padding: 12px;
    border: none;
    border-radius: 15px;
    font-size: 1em;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.3s ease;
    background: var(--card-bg);
    backdrop-filter: blur(10px);
    color: var(--text-primary);
    border: 2px solid var(--card-border);
    box-shadow: 0 4px 15px rgba(0,0,0,0.2);
}

.btn:hover {
    background: var(--card-border);
    transform: translateY(-2px);
    box-shadow: 0 10px 25px rgba(0,0,0,0.3);
}

.btn:active {
    transform: translateY(0);
}

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

.btn-secondary {
    background: var(--btn-secondary);
    border-color: transparent;
}

.btn-theme {
    background: var(--btn-theme);
    border-color: transparent;
    max-width: 80px;
}

.instructions {
    color: var(--text-secondary);
    text-align: center;
    font-size: 0.9em;
    line-height: 1.4;
    padding-bottom: env(safe-area-inset-bottom, 10px);
    text-shadow: 1px 1px 2px rgba(0,0,0,0.3);
}

.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.8);
    backdrop-filter: blur(5px);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal-content {
    background: var(--modal-bg);
    padding: 30px;
    border-radius: 20px;
    text-align: center;
    color: var(--text-primary);
    max-width: 300px;
    width: 90%;
    box-shadow: 0 30px 60px rgba(0,0,0,0.4);
    border: 1px solid var(--card-border);
}

.modal h2 {
    margin-bottom: 15px;
    font-size: 1.8em;
}

.modal p {
    margin-bottom: 20px;
    line-height: 1.5;
}

.paused {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0,0,0,0.9);
    color: var(--text-primary);
    padding: 20px;
    border-radius: 15px;
    font-size: 1.5em;
    font-weight: bold;
    z-index: 100;
    box-shadow: 0 20px 40px rgba(0,0,0,0.5);
}

.tile-new {
    animation: tileAppear 0.3s ease-out;
}

.tile-merge {
    animation: tileMerge 0.3s ease-out;
}

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

@keyframes tileMerge {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.2);
    }
    100% {
        transform: scale(1);
    }
}

.level-indicator {
    position: absolute;
    top: 10px;
    right: 15px;
    background: var(--level-bg);
    backdrop-filter: blur(10px);
    padding: 8px 12px;
    border-radius: 12px;
    font-size: 0.9em;
    font-weight: bold;
    color: var(--level-text);
    border: 2px solid rgba(255,255,255,0.3);
    box-shadow: 0 4px 15px rgba(0,0,0,0.2);
    text-shadow: none;
}

@media (max-height: 700px) {
    .title { font-size: 2em; }
    .game-board { padding: 10px; gap: 8px; }
    .tile { font-size: 1.5em; border-radius: 10px; }
    .controls { margin-bottom: 5px; }
    .stats { padding: 8px 12px; margin-bottom: 10px; }
    .timer-bar { margin-bottom: 10px; }
}

@media (max-height: 600px) {
    .title { font-size: 1.8em; margin-bottom: 3px; }
    .header { margin-bottom: 10px; }
    .stats { padding: 6px 10px; margin-bottom: 8px; }
    .timer-bar { margin-bottom: 8px; height: 6px; }
    .game-board { padding: 8px; gap: 6px; margin-bottom: 10px; }
    .tile { font-size: 1.3em; border-radius: 8px; }
    .btn { padding: 10px; font-size: 0.9em; }
    .instructions { font-size: 0.8em; }
    .level-indicator { padding: 6px 10px; font-size: 0.8em; }
}