/* ===== CSS Reset & Base ===== */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --board-size: 460px;
    --cell-gap: 12px;
    --cell-size: calc((var(--board-size) - 5 * var(--cell-gap)) / 4);
    --radius: 14px;
    --bg-dark: #0f0f1a;
    --bg-card: rgba(255, 255, 255, 0.04);
    --bg-board: #1a1a2e;
    --bg-cell: rgba(255, 255, 255, 0.06);
    --accent-1: #f97316;
    --accent-2: #ec4899;
    --accent-3: #8b5cf6;
    --text-primary: #f1f5f9;
    --text-muted: #94a3b8;
    --glow-orange: rgba(249, 115, 22, 0.35);
    --glow-pink: rgba(236, 72, 153, 0.25);
    --transition-fast: 0.15s ease;
    --transition-normal: 0.25s ease;
}

html, body {
    height: 100%;
    font-family: 'Outfit', sans-serif;
    background: var(--bg-dark);
    color: var(--text-primary);
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

body {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

/* ===== Animated Background ===== */
.bg-particles {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 0;
    overflow: hidden;
}

.bg-particles .particle {
    position: absolute;
    border-radius: 50%;
    opacity: 0;
    animation: floatParticle 12s infinite ease-in-out;
}

@keyframes floatParticle {
    0% { opacity: 0; transform: translateY(0) scale(0.5); }
    20% { opacity: 0.6; }
    80% { opacity: 0.4; }
    100% { opacity: 0; transform: translateY(-100vh) scale(1.2); }
}

/* ===== Game Container ===== */
.game-container {
    position: relative;
    z-index: 1;
    width: 100%;
    max-width: 500px;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

/* ===== Header ===== */
.game-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.title-group {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.game-title {
    font-size: 3rem;
    font-weight: 900;
    letter-spacing: -2px;
    background: linear-gradient(135deg, var(--accent-1), var(--accent-2), var(--accent-3));
    background-size: 200% 200%;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    animation: gradientShift 4s ease infinite;
    line-height: 1;
}

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

.game-subtitle {
    font-size: 0.85rem;
    color: var(--text-muted);
    font-weight: 600;
    letter-spacing: 1px;
    text-transform: uppercase;
}

/* ===== Score Boxes ===== */
.score-group {
    display: flex;
    gap: 10px;
}

.score-box {
    background: linear-gradient(145deg, rgba(249, 115, 22, 0.15), rgba(236, 72, 153, 0.1));
    border: 1px solid rgba(249, 115, 22, 0.2);
    border-radius: 12px;
    padding: 8px 16px;
    text-align: center;
    min-width: 80px;
    backdrop-filter: blur(10px);
    transition: transform var(--transition-fast), box-shadow var(--transition-fast);
}

.score-box:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px var(--glow-orange);
}

.score-box.best {
    background: linear-gradient(145deg, rgba(139, 92, 246, 0.15), rgba(236, 72, 153, 0.1));
    border-color: rgba(139, 92, 246, 0.2);
}

.score-box.best:hover {
    box-shadow: 0 6px 20px var(--glow-pink);
}

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

.score-value {
    display: block;
    font-size: 1.4rem;
    font-weight: 800;
    color: var(--text-primary);
    transition: transform 0.1s ease;
}

.score-value.bump {
    transform: scale(1.3);
}

/* ===== Controls Bar ===== */
.controls-bar {
    display: flex;
    justify-content: flex-end;
}

.btn-new-game {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 20px;
    border: none;
    border-radius: 10px;
    font-family: 'Outfit', sans-serif;
    font-size: 0.85rem;
    font-weight: 700;
    color: white;
    background: linear-gradient(135deg, var(--accent-1), var(--accent-2));
    cursor: pointer;
    transition: transform var(--transition-fast), box-shadow var(--transition-fast), filter var(--transition-fast);
    box-shadow: 0 4px 15px var(--glow-orange);
}

.btn-new-game:hover {
    transform: translateY(-2px) scale(1.02);
    box-shadow: 0 8px 25px var(--glow-orange);
    filter: brightness(1.1);
}

.btn-new-game:active {
    transform: translateY(0) scale(0.98);
}

/* ===== Game Board ===== */
.board-wrapper {
    position: relative;
    width: var(--board-size);
    height: var(--board-size);
    margin: 0 auto;
}

.board {
    width: 100%;
    height: 100%;
    background: var(--bg-board);
    border-radius: 18px;
    padding: var(--cell-gap);
    position: relative;
    box-shadow:
        0 0 0 1px rgba(255, 255, 255, 0.06),
        0 20px 60px rgba(0, 0, 0, 0.5),
        inset 0 1px 0 rgba(255, 255, 255, 0.05);
}

.grid-bg {
    display: grid;
    grid-template-columns: repeat(4, var(--cell-size));
    grid-template-rows: repeat(4, var(--cell-size));
    gap: var(--cell-gap);
}

.grid-cell-bg {
    border-radius: var(--radius);
    background: var(--bg-cell);
}

.tile-container {
    position: absolute;
    top: var(--cell-gap);
    left: var(--cell-gap);
    right: var(--cell-gap);
    bottom: var(--cell-gap);
}

/* ===== Tile ===== */
.tile {
    position: absolute;
    width: var(--cell-size);
    height: var(--cell-size);
    border-radius: var(--radius);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 800;
    font-size: 2rem;
    transition: top 0.12s ease, left 0.12s ease;
    z-index: 10;
    overflow: hidden;
}

.tile img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: var(--radius);
}

.tile .tile-number {
    position: absolute;
    bottom: 4px;
    right: 6px;
    font-size: 0.65rem;
    font-weight: 800;
    color: rgba(255, 255, 255, 0.85);
    text-shadow: 0 1px 3px rgba(0,0,0,0.6);
    z-index: 2;
    background: rgba(0,0,0,0.35);
    padding: 1px 6px;
    border-radius: 6px;
    line-height: 1.4;
}

/* Tile color scheme */
.tile[data-value="2"]    { background: #eee4da; color: #776e65; }
.tile[data-value="4"]    { background: #ede0c8; color: #776e65; }
.tile[data-value="8"]    { background: #f2b179; color: #fff; }
.tile[data-value="16"]   { background: #f59563; color: #fff; }
.tile[data-value="32"]   { background: #f67c5f; color: #fff; }
.tile[data-value="64"]   { background: #f65e3b; color: #fff; }
.tile[data-value="128"]  { background: linear-gradient(135deg, #edcf72, #edc850); color: #fff; box-shadow: 0 0 20px rgba(237, 207, 114, 0.4); }
.tile[data-value="256"]  { background: linear-gradient(135deg, #edcc61, #edc53f); color: #fff; box-shadow: 0 0 25px rgba(237, 204, 97, 0.5); }
.tile[data-value="512"]  { background: linear-gradient(135deg, #edc850, #edc22e); color: #fff; box-shadow: 0 0 30px rgba(237, 200, 80, 0.5); }
.tile[data-value="1024"] { background: linear-gradient(135deg, #edc53f, #f0b000); color: #fff; box-shadow: 0 0 35px rgba(237, 197, 63, 0.6); }
.tile[data-value="2048"] { background: linear-gradient(135deg, #edc22e, #f7a600); color: #fff; box-shadow: 0 0 50px rgba(237, 194, 46, 0.8); animation: tilePulse2048 1.5s infinite alternate; }

@keyframes tilePulse2048 {
    0% { box-shadow: 0 0 30px rgba(237, 194, 46, 0.5); }
    100% { box-shadow: 0 0 60px rgba(247, 166, 0, 0.9), 0 0 100px rgba(237, 194, 46, 0.3); }
}

/* Tile Animations */
@keyframes tileAppear {
    0% { transform: scale(0); opacity: 0; }
    50% { transform: scale(1.15); }
    100% { transform: scale(1); opacity: 1; }
}

@keyframes tileMerge {
    0% { transform: scale(1); }
    30% { transform: scale(1.25); }
    100% { transform: scale(1); }
}

.tile.tile-new {
    animation: tileAppear 0.2s ease forwards;
}

.tile.tile-merged {
    animation: tileMerge 0.2s ease forwards;
    z-index: 20;
}

/* ===== Game Over Overlay ===== */
.game-overlay {
    position: absolute;
    inset: 0;
    border-radius: 18px;
    background: rgba(15, 15, 26, 0.85);
    backdrop-filter: blur(8px);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.4s ease;
    z-index: 100;
}

.game-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.overlay-content {
    text-align: center;
    display: flex;
    flex-direction: column;
    gap: 16px;
    align-items: center;
}

.overlay-title {
    font-size: 2.5rem;
    font-weight: 900;
    background: linear-gradient(135deg, var(--accent-1), var(--accent-2));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.overlay-score {
    font-size: 1.1rem;
    color: var(--text-muted);
}

.overlay-score strong {
    color: var(--text-primary);
    font-weight: 800;
}

.overlay-btn {
    margin-top: 8px;
}

/* ===== Instructions ===== */
.instructions {
    text-align: center;
    color: var(--text-muted);
    font-size: 0.8rem;
}

.instructions kbd {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 6px;
    padding: 2px 8px;
    font-family: 'Outfit', sans-serif;
    font-size: 0.75rem;
    font-weight: 600;
}

/* ===== Score Add Animation ===== */
.score-add {
    position: absolute;
    font-size: 1rem;
    font-weight: 800;
    color: var(--accent-1);
    pointer-events: none;
    animation: scoreFloat 0.8s ease-out forwards;
    z-index: 50;
}

@keyframes scoreFloat {
    0% { opacity: 1; transform: translateY(0); }
    100% { opacity: 0; transform: translateY(-40px); }
}

/* ===== Responsive ===== */
@media (max-width: 520px) {
    :root {
        --board-size: calc(100vw - 40px);
        --cell-gap: 8px;
    }

    .game-container {
        padding: 12px;
        gap: 12px;
    }

    .game-title {
        font-size: 2.2rem;
    }

    .score-box {
        padding: 6px 10px;
        min-width: 62px;
    }

    .score-value {
        font-size: 1.1rem;
    }

    .tile {
        font-size: 1.3rem;
    }

    .tile .tile-number {
        font-size: 0.55rem;
        padding: 1px 4px;
    }

    .overlay-title {
        font-size: 2rem;
    }
}

@media (max-width: 360px) {
    :root {
        --cell-gap: 6px;
    }

    .game-title {
        font-size: 1.8rem;
    }

    .tile {
        font-size: 1rem;
    }
}

/* ===== Multiplayer & Menus ===== */
.menu-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 15, 26, 0.95);
    backdrop-filter: blur(10px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition-normal);
}

.menu-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.menu-content {
    background: var(--bg-board);
    padding: 40px;
    border-radius: 24px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.5), inset 0 1px 0 rgba(255, 255, 255, 0.05);
    text-align: center;
    width: 90%;
    max-width: 400px;
    display: flex;
    flex-direction: column;
    gap: 24px;
}

.menu-buttons {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-top: 10px;
}

.btn-primary, .btn-secondary {
    padding: 14px 24px;
    border: none;
    border-radius: 12px;
    font-family: 'Outfit', sans-serif;
    font-size: 1.1rem;
    font-weight: 700;
    cursor: pointer;
    transition: transform var(--transition-fast), box-shadow var(--transition-fast);
}

.btn-primary {
    background: linear-gradient(135deg, var(--accent-1), var(--accent-2));
    color: white;
    box-shadow: 0 4px 15px var(--glow-orange);
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px var(--glow-orange);
}

.btn-secondary {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-primary);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.btn-secondary:hover {
    background: rgba(255, 255, 255, 0.1);
    transform: translateY(-2px);
}

.btn-back {
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-family: 'Outfit', sans-serif;
    font-size: 0.9rem;
    margin-top: 10px;
    cursor: pointer;
}

.btn-back:hover {
    color: var(--text-primary);
    text-decoration: underline;
}

.lobby-section {
    background: rgba(0, 0, 0, 0.2);
    padding: 16px;
    border-radius: 16px;
}

.lobby-section h3 {
    margin-bottom: 12px;
    font-size: 1.2rem;
}

.code-box {
    background: rgba(255, 255, 255, 0.1);
    padding: 10px;
    border-radius: 8px;
    font-family: monospace;
    font-size: 1.2rem;
    letter-spacing: 2px;
    margin: 8px 0;
    user-select: all;
    border: 1px dashed rgba(255, 255, 255, 0.3);
}

.input-code {
    width: 100%;
    padding: 12px;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 8px;
    color: white;
    font-family: monospace;
    font-size: 1.1rem;
    text-align: center;
    margin-bottom: 12px;
}

.input-code:focus {
    outline: none;
    border-color: var(--accent-1);
}

.status-text {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-top: 8px;
    min-height: 20px;
}

.divider {
    font-size: 0.9rem;
    color: var(--text-muted);
    position: relative;
}

.divider::before, .divider::after {
    content: '';
    position: absolute;
    top: 50%;
    width: 40%;
    height: 1px;
    background: rgba(255, 255, 255, 0.1);
}

.divider::before { left: 0; }
.divider::after { right: 0; }

/* Multiplayer Header & Board adjustments */
.multi-header {
    display: flex;
    justify-content: space-between;
    background: rgba(0,0,0,0.2);
    padding: 12px 20px;
    border-radius: 16px;
    margin-bottom: -4px;
}

.timer-box, .opponent-box {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.timer-label, .opponent-label {
    font-size: 0.65rem;
    font-weight: 700;
    color: var(--text-muted);
    letter-spacing: 1px;
}

.timer-value {
    font-size: 1.6rem;
    font-weight: 900;
    color: var(--accent-1);
    font-variant-numeric: tabular-nums;
}

.timer-value.urgent {
    color: #ef4444;
    animation: pulse 1s infinite;
}

.opponent-score {
    font-size: 1.4rem;
    font-weight: 800;
}

.opponent-board {
    position: absolute;
    right: -100px;
    top: 0;
    background: rgba(0,0,0,0.3);
    padding: 6px;
    border-radius: 12px;
}

.mini-grid {
    display: grid;
    grid-template-columns: repeat(4, 20px);
    grid-template-rows: repeat(4, 20px);
    gap: 3px;
}

.mini-cell {
    background: var(--bg-cell);
    border-radius: 3px;
}

.mini-cell[data-val="2"] { background: #eee4da; }
.mini-cell[data-val="4"] { background: #ede0c8; }
.mini-cell[data-val="8"] { background: #f2b179; }
.mini-cell[data-val="16"] { background: #f59563; }
.mini-cell[data-val="32"] { background: #f67c5f; }
.mini-cell[data-val="64"] { background: #f65e3b; }
.mini-cell[data-val="128"] { background: #edcf72; }
.mini-cell[data-val="256"] { background: #edcc61; }
.mini-cell[data-val="512"] { background: #edc850; }
.mini-cell[data-val="1024"] { background: #edc53f; }
.mini-cell[data-val="2048"] { background: #edc22e; }

@media (max-width: 768px) {
    .opponent-board {
        position: relative;
        right: auto;
        top: auto;
        margin-bottom: 12px;
        display: flex;
        justify-content: center;
        background: transparent;
    }
}
