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

:root {
    --bg-color: #faf8ef;
    --text-color: #776e65;
    --board-bg: #bbada0;
    --cell-bg: rgba(238, 228, 218, 0.35);
    --btn-bg: #8f7a66;
    --btn-hover: #9f8b77;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
    background: var(--bg-color);
    color: var(--text-color);
    display: flex;
    justify-content: center;
    align-items: flex-start;
    min-height: 100dvh;
    -webkit-tap-highlight-color: transparent;
    touch-action: none;
    user-select: none;
    transition: background 0.3s, color 0.3s;
}

/* Dark mode */
body.dark-mode {
    --bg-color: #1a1a1a;
    --text-color: #e0e0e0;
    --board-bg: #3a3a3a;
    --cell-bg: rgba(80, 80, 80, 0.5);
    --btn-bg: #555;
    --btn-hover: #666;
}

#app {
    width: 100%;
    max-width: 500px;
    padding: 16px;
}

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

.header-right {
    display: flex;
    gap: 8px;
    align-items: center;
}

.theme-toggle {
    background: var(--btn-bg);
    border: none;
    border-radius: 6px;
    width: 40px;
    height: 40px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

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

.theme-icon {
    font-size: 20px;
    line-height: 1;
}

.lang-link {
    background: var(--btn-bg);
    color: #f9f6f2;
    text-decoration: none;
    padding: 10px 16px;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 600;
    transition: background 0.2s;
}

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

h1 {
    font-size: 48px;
    font-weight: 700;
    color: var(--text-color);
}

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

.score-label {
    font-size: 12px;
    color: #eee4da;
    text-transform: uppercase;
}

.score-value {
    font-size: 20px;
    font-weight: 600;
    color: white;
}

.game-intro {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
    font-size: 14px;
    color: var(--text-color);
}

.game-hint {
    font-size: 12px;
    color: var(--text-color);
    opacity: 0.6;
    margin-bottom: 16px;
}

.buttons {
    display: flex;
    gap: 8px;
}

#restart-btn, #retry-btn, #undo-btn {
    background: var(--btn-bg);
    color: #f9f6f2;
    border: none;
    padding: 10px 20px;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
}

#restart-btn:hover, #retry-btn:hover, #undo-btn:hover {
    background: var(--btn-hover);
}

#undo-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

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

#board {
    position: relative;
    width: 100%;
    height: 100%;
}

.grid-cell {
    position: absolute;
    background: rgba(238, 228, 218, 0.35);
    border-radius: 6px;
}

.tile {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    transition: transform 100ms ease-in-out;
    will-change: transform;
    z-index: 1;
}

.tile-inner {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    border-radius: 6px;
    /* Visual texture */
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* Glow effect for high-value tiles */
.tile-128 .tile-inner, .tile-256 .tile-inner, .tile-512 .tile-inner, 
.tile-1024 .tile-inner, .tile-2048 .tile-inner, .tile-super .tile-inner {
    box-shadow: 0 0 15px rgba(243, 215, 116, 0.4), 0 2px 4px rgba(0, 0, 0, 0.1);
}

.tile-2048 .tile-inner, .tile-super .tile-inner {
    box-shadow: 0 0 25px rgba(237, 194, 46, 0.6), 0 4px 8px rgba(0, 0, 0, 0.15);
}

.tile-new .tile-inner {
    animation: tile-appear 200ms ease 100ms;
    animation-fill-mode: backwards;
}

.tile-merged {
    z-index: 20;
}

.tile-merged .tile-inner {
    animation: tile-pop 200ms ease;
}

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

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

/* Score change animation */
.score-increase {
    animation: score-pop 0.4s ease-out;
}

@keyframes score-pop {
    0% { 
        transform: translateY(0);
        opacity: 1;
    }
    100% { 
        transform: translateY(-20px);
        opacity: 0;
    }
}

/* Color theme - gradients for depth */
.tile-2    { background: linear-gradient(135deg, #eee4da 0%, #e8dcc8 100%); color: #776e65; }
.tile-4    { background: linear-gradient(135deg, #ede0c8 0%, #e6d4b8 100%); color: #776e65; }
.tile-8    { background: linear-gradient(135deg, #f2b179 0%, #eda566 100%); color: #f9f6f2; }
.tile-16   { background: linear-gradient(135deg, #f59563 0%, #f18851 100%); color: #f9f6f2; }
.tile-32   { background: linear-gradient(135deg, #f67c5f 0%, #f26e4d 100%); color: #f9f6f2; }
.tile-64   { background: linear-gradient(135deg, #f65e3b 0%, #f25029 100%); color: #f9f6f2; }
.tile-128  { background: linear-gradient(135deg, #edcf72 0%, #e8c861 100%); color: #f9f6f2; font-size: 0.85em; }
.tile-256  { background: linear-gradient(135deg, #edcc61 0%, #e8c550 100%); color: #f9f6f2; font-size: 0.85em; }
.tile-512  { background: linear-gradient(135deg, #edc850 0%, #e8c13f 100%); color: #f9f6f2; font-size: 0.85em; }
.tile-1024 { background: linear-gradient(135deg, #edc53f 0%, #e8be2e 100%); color: #f9f6f2; font-size: 0.75em; }
.tile-2048 { background: linear-gradient(135deg, #edc22e 0%, #e8bb1d 100%); color: #f9f6f2; font-size: 0.75em; }
.tile-super { background: linear-gradient(135deg, #3c3a32 0%, #33312a 100%); color: #f9f6f2; font-size: 0.7em; }

#game-over {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10;
}

#game-over.hidden {
    display: none;
}

#game-over .overlay {
    width: 100%;
    height: 100%;
    background: rgba(238, 228, 218, 0.73);
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
}

#game-over .message {
    font-size: 48px;
    font-weight: 700;
    color: #776e65;
}

#win-message {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10;
}

#win-message.hidden {
    display: none;
}

#win-message .overlay {
    width: 100%;
    height: 100%;
    background: rgba(237, 194, 46, 0.5);
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
}

#win-message .message {
    font-size: 48px;
    font-weight: 700;
    color: #f9f6f2;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.win-buttons {
    display: flex;
    gap: 12px;
}

#keep-playing-btn, #new-game-btn {
    background: #8f7a66;
    color: #f9f6f2;
    border: none;
    padding: 10px 20px;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
}

#keep-playing-btn:hover, #new-game-btn:hover {
    background: #9f8b77;
}