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

/* CSS Variables for Theme Support */
:root {
    /* Default Theme - Modern Professional */
    --bg-primary: #f8f9fa;
    --bg-secondary: #e9ecef;
    --text-primary: #343a40;
    --text-secondary: #6c757d;
    --container-bg: #ffffff;
    --game-container-bg: #1e3a8a;
    --game-board-bg: #1e3a8a;
    --tile-bg: #e2e8f0;
    --score-bg: #1e3a8a;
    --button-bg: #3b82f6;
    --button-hover-bg: #2563eb;
    --border-color: #cbd5e1;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.1);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.1);
    --border-radius-sm: 8px;
    --border-radius-md: 12px;
    --border-radius-lg: 16px;
    --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Cute Theme */
body.theme-cute {
    --bg-primary: #fff0f5;
    --bg-secondary: #ffd1dc;
    --text-primary: #343a40;
    --text-secondary: #6c757d;
    --container-bg: #ffffff;
    --game-container-bg: #ff69b4;
    --game-board-bg: #ff69b4;
    --tile-bg: #ffc0cb;
    --score-bg: #ff69b4;
    --button-bg: #ff1493;
    --button-hover-bg: #c71585;
    --border-color: #ffb6c1;
}

/* Dark Theme */
body.theme-dark {
    --bg-primary: #1a1a1a;
    --bg-secondary: #2d2d2d;
    --text-primary: #e2e8f0;
    --text-secondary: #94a3b8;
    --container-bg: #1f2937;
    --game-container-bg: #374151;
    --game-board-bg: #4b5563;
    --tile-bg: #6b7280;
    --score-bg: #4b5563;
    --button-bg: #3b82f6;
    --button-hover-bg: #2563eb;
    --border-color: #4b5563;
}

/* Pastel Theme */
body.theme-pastel {
    --bg-primary: #f5f5f5;
    --bg-secondary: #e8eaf6;
    --text-primary: #343a40;
    --text-secondary: #6c757d;
    --container-bg: #ffffff;
    --game-container-bg: #90caf9;
    --game-board-bg: #90caf9;
    --tile-bg: #bbdefb;
    --score-bg: #90caf9;
    --button-bg: #64b5f6;
    --button-hover-bg: #42a5f5;
    --border-color: #e3f2fd;
}

body {
    font-family: 'Inter', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, var(--bg-primary) 0%, var(--bg-secondary) 100%);
    color: var(--text-primary);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    user-select: none;
    overflow: hidden;
    transition: var(--transition);
}

/* Visually hidden elements for accessibility */
.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Theme Selector Style */
.theme-selector {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    font-weight: 500;
}

.theme-selector select {
    padding: 8px 12px;
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius-sm);
    background: var(--container-bg);
    color: var(--text-primary);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
    box-shadow: var(--shadow-sm);
    outline: none;
}

.theme-selector select:hover {
    border-color: var(--button-bg);
    box-shadow: var(--shadow-md);
}

.theme-selector select:focus {
    border-color: var(--button-bg);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.3);
}

body.theme-cute .theme-selector select:focus {
    box-shadow: 0 0 0 3px rgba(255, 20, 147, 0.3);
}

body.theme-dark .theme-selector select:focus {
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.3);
}

body.theme-pastel .theme-selector select:focus {
    box-shadow: 0 0 0 3px rgba(100, 181, 246, 0.3);
}

.container {
    text-align: center;
    width: 480px;
    padding: 30px;
    background: var(--container-bg);
    border-radius: var(--border-radius-lg);
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border-color);
    transition: var(--transition);
}

.game-title {
    font-size: 72px;
    font-weight: 900;
    margin: 0 0 20px 0;
    color: var(--text-primary);
    letter-spacing: -1px;
    transition: var(--transition);
    line-height: 1;
}

body.theme-cute .game-title {
    color: #ff1493;
}

body.theme-dark .game-title {
    color: #e2e8f0;
}

body.theme-pastel .game-title {
    color: #42a5f5;
}

.stats {
    display: flex;
    gap: 20px;
    margin-bottom: 25px;
    justify-content: center;
}

.score-container, .best-container {
    background: var(--score-bg);
    color: white;
    padding: 16px 28px;
    border-radius: var(--border-radius-md);
    min-width: 120px;
    box-shadow: var(--shadow-md);
    transition: var(--transition);
}

.score-container:hover, .best-container:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
}

.score-label {
    font-size: 12px;
    text-transform: uppercase;
    color: rgba(255, 255, 255, 0.9);
    font-weight: 600;
    letter-spacing: 1px;
    margin-bottom: 4px;
}

.score-value {
    font-size: 32px;
    font-weight: 700;
    line-height: 1;
}

.game-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 25px;
    gap: 15px;
    flex-wrap: nowrap;
}

@media (max-width: 992px) {
    .game-info {
        flex-wrap: wrap;
    }
}

@media (max-width: 768px) {
    .game-info {
        flex-direction: column;
        align-items: center;
        gap: 15px;
    }
    
    .instructions {
        text-align: center;
    }
    
    .theme-controls {
        justify-content: center;
    }
}

#new-game {
    background: var(--button-bg);
    color: white;
    border: none;
    padding: 14px 28px;
    border-radius: var(--border-radius-md);
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    box-shadow: var(--shadow-md);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    outline: none;
}

#new-game:hover {
    background: var(--button-hover-bg);
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
}

#new-game:active {
    transform: translateY(0);
}

.instructions {
    font-size: 14px;
    color: var(--text-secondary);
    font-weight: 500;
    flex: 1;
    min-width: 150px;
}

.game-container {
    background: var(--game-container-bg);
    padding: 20px;
    border-radius: var(--border-radius-lg);
    width: 440px;
    height: 440px;
    position: relative;
    box-shadow: var(--shadow-md);
    border: 1px solid var(--border-color);
    transition: var(--transition);
}

#game-board {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: 16px;
    width: 100%;
    height: 100%;
    background: var(--game-board-bg);
    border-radius: var(--border-radius-md);
    position: relative;
}

.tile {
    background: var(--tile-bg);
    border-radius: var(--border-radius-md);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 36px;
    font-weight: 700;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    position: absolute;
    width: 88px;
    height: 88px;
    box-shadow: var(--shadow-md);
    z-index: 10;
    opacity: 1;
    transform: scale(1);
    font-family: inherit;
}

@keyframes tileAppear {
    from {
        opacity: 0;
        transform: scale(0.8);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

.tile-moving {
    transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 20;
    transform: scale(1.05);
}

.tile-merged {
    animation: tileMerge 0.4s cubic-bezier(0.4, 0, 0.2, 1) forwards;
    z-index: 30;
    opacity: 1;
    transform: scale(1);
}

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

.tile-2 {
    background: linear-gradient(135deg, #ffeb3b 0%, #fff59d 100%);
    color: #333;
    border: 2px solid #ffd54f;
    box-shadow: 0 4px 12px rgba(255, 235, 59, 0.3);
}

.tile-4 {
    background: linear-gradient(135deg, #66bb6a 0%, #81c784 100%);
    color: white;
    border: 2px solid #4caf50;
    box-shadow: 0 4px 12px rgba(102, 187, 106, 0.3);
}

.tile-8 {
    background: linear-gradient(135deg, #42a5f5 0%, #64b5f6 100%);
    color: white;
    border: 2px solid #2196f3;
    box-shadow: 0 4px 12px rgba(66, 165, 245, 0.3);
}

.tile-16 {
    background: linear-gradient(135deg, #ab47bc 0%, #ba68c8 100%);
    color: white;
    border: 2px solid #9c27b0;
    box-shadow: 0 4px 12px rgba(171, 71, 188, 0.3);
}

.tile-32 {
    background: linear-gradient(135deg, #ec407a 0%, #f06292 100%);
    color: white;
    border: 2px solid #e91e63;
    box-shadow: 0 4px 12px rgba(236, 64, 122, 0.3);
}

.tile-64 {
    background: linear-gradient(135deg, #ff7043 0%, #ff8a65 100%);
    color: white;
    border: 2px solid #ff5722;
    box-shadow: 0 4px 12px rgba(255, 112, 67, 0.3);
}

.tile-128 {
    background: linear-gradient(135deg, #5c6bc0 0%, #7986cb 100%);
    color: white;
    font-size: 28px;
    border: 2px solid #3f51b5;
    box-shadow: 0 6px 16px rgba(92, 107, 192, 0.4);
    animation: tileGlow 1.5s ease-in-out infinite alternate;
}

.tile-256 {
    background: linear-gradient(135deg, #26c6da 0%, #4dd0e1 100%);
    color: white;
    font-size: 28px;
    border: 2px solid #00bcd4;
    box-shadow: 0 6px 16px rgba(38, 198, 218, 0.4);
    animation: tileGlow 1.5s ease-in-out infinite alternate;
}

.tile-512 {
    background: linear-gradient(135deg, #8d6e63 0%, #a1887f 100%);
    color: white;
    font-size: 28px;
    border: 2px solid #795548;
    box-shadow: 0 6px 16px rgba(141, 110, 99, 0.4);
    animation: tileGlow 1.5s ease-in-out infinite alternate;
}

.tile-1024 {
    background: linear-gradient(135deg, #d4e157 0%, #dce775 100%);
    color: #333;
    font-size: 24px;
    border: 2px solid #cddc39;
    box-shadow: 0 8px 20px rgba(212, 225, 87, 0.5);
    animation: tileGlow 1.2s ease-in-out infinite alternate;
}

.tile-2048 {
    background: linear-gradient(135deg, #ffca28 0%, #ffd54f 100%);
    color: #333;
    font-size: 24px;
    border: 2px solid #ffc107;
    box-shadow: 0 8px 20px rgba(255, 202, 40, 0.5);
    animation: tileGlow 1s ease-in-out infinite alternate;
}

.tile-4096, .tile-8192 {
    background: linear-gradient(135deg, #ef5350 0%, #e57373 100%);
    color: white;
    font-size: 20px;
    border: 2px solid #f44336;
    box-shadow: 0 8px 20px rgba(239, 83, 80, 0.5);
    animation: tileGlow 0.8s ease-in-out infinite alternate;
}

@keyframes tileGlow {
    from {
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    }
    to {
        box-shadow: 0 8px 24px rgba(255, 255, 0, 0.4);
    }
}

.game-message {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.85);
    display: none;
    justify-content: center;
    align-items: center;
    font-size: 48px;
    font-weight: 800;
    color: white;
    flex-direction: column;
    gap: 20px;
    border-radius: var(--border-radius-lg);
    z-index: 100;
    backdrop-filter: blur(4px);
}

.game-message.visible {
    display: flex;
    animation: fadeIn 0.3s ease-out;
}

.game-message button {
    background: var(--button-bg);
    color: white;
    border: none;
    padding: 16px 32px;
    font-size: 18px;
    border-radius: var(--border-radius-md);
    cursor: pointer;
    font-weight: 600;
    transition: var(--transition);
    box-shadow: var(--shadow-md);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    outline: none;
}

.game-message button:hover {
    background: var(--button-hover-bg);
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
}

.game-message button:active {
    transform: translateY(0);
}

.tile-new {
    animation: tileNewAppear 0.3s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

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

.tile-merged {
    animation: tileMerge 0.4s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

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

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

@keyframes fadeIn {
    0% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}

/* Score change animation */
.score-value {
    transition: var(--transition);
}

.score-changed {
    animation: scoreBump 0.4s ease-out;
}

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

/* Responsive design */
@media (max-width: 520px) {
    .container {
        width: 95%;
        padding: 20px;
    }
    
    .game-title {
        font-size: 56px;
    }
    
    .game-container {
        width: 100%;
        height: auto;
        aspect-ratio: 1;
        padding: 15px;
    }
    
    .tile {
        width: calc(25% - 12px);
        height: calc(25% - 12px);
        font-size: 24px;
    }
    
    .tile-128, .tile-256, .tile-512 {
        font-size: 20px;
    }
    
    .tile-1024, .tile-2048 {
        font-size: 18px;
    }
    
    .tile-4096, .tile-8192 {
        font-size: 16px;
    }
    
    .game-info {
        flex-direction: column;
        gap: 15px;
        text-align: center;
    }
    
    #new-game {
        width: 100%;
    }
    
    .theme-selector {
        width: 100%;
        justify-content: center;
    }
}

/* Focus states for accessibility */
button:focus {
    outline: 2px solid var(--button-bg);
    outline-offset: 2px;
}

.theme-selector select:focus {
    outline: 2px solid var(--button-bg);
    outline-offset: 2px;
}

/* Theme Controls */
.theme-controls {
    display: flex;
    gap: 15px;
    align-items: center;
}

/* Background Color Selector */
.bg-color-selector {
    display: flex;
    align-items: center;
    gap: 8px;
}

.bg-color-selector input[type="color"] {
    width: 40px;
    height: 40px;
    border: 2px solid var(--border-color);
    border-radius: var(--border-radius-sm);
    cursor: pointer;
    transition: var(--transition);
    box-shadow: var(--shadow-sm);
    outline: none;
}

.bg-color-selector input[type="color"]:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
    border-color: var(--button-bg);
}

.bg-color-selector input[type="color"]:focus {
    outline: 2px solid var(--button-bg);
    outline-offset: 2px;
}

/* Reduced motion for accessibility */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}