@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@400;500;600;700;800;900&display=swap');

:root {
    --bg-dark: #0f0f1a;
    --bg-card: #1a1a2f;
    --bg-cell: #252542;
    --text: #ffffff;
    --text-dim: rgba(255, 255, 255, 0.5);
    --purple: #a855f7;
    --pink: #ec4899;
    --blue: #3b82f6;
    --cyan: #22d3ee;
    --green: #22c55e;
    --yellow: #facc15;
    --orange: #fb923c;
    --red: #ef4444;
    --glow: rgba(168, 85, 247, 0.35);
    --radius: 20px;
    --radius-sm: 14px;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html,
body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    position: fixed;
    touch-action: none;
}

body {
    font-family:
        'Outfit',
        -apple-system,
        sans-serif;
    background: var(--bg-dark);
    background-image:
        radial-gradient(
            circle at 15% 15%,
            rgba(168, 85, 247, 0.12) 0%,
            transparent 40%
        ),
        radial-gradient(
            circle at 85% 85%,
            rgba(236, 72, 153, 0.1) 0%,
            transparent 40%
        );
    color: var(--text);
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
    user-select: none;
    -webkit-user-select: none;
}

.game-container {
    width: 100%;
    max-width: 380px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

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

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

.logo {
    font-size: 2.4rem;
    font-weight: 900;
    background: linear-gradient(135deg, var(--purple), var(--pink));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    letter-spacing: -1px;
    line-height: 1;
}

.subtitle {
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-dim);
    letter-spacing: 0.5px;
}

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

.score-card {
    background: var(--bg-card);
    border: 1px solid rgba(255, 255, 255, 0.06);
    border-radius: var(--radius-sm);
    padding: 10px 18px;
    text-align: center;
    min-width: 75px;
    position: relative;
    overflow: hidden;
}

.score-card::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(90deg, var(--purple), var(--pink));
    border-radius: 3px 3px 0 0;
}

.score-label {
    font-size: 0.6rem;
    font-weight: 600;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 2px;
}

.score-value {
    font-size: 1.5rem;
    font-weight: 800;
    color: var(--text);
}

.score-value.pop {
    animation: pop 0.25s cubic-bezier(0.36, 1.5, 0.6, 1);
}

@keyframes pop {
    50% {
        transform: scale(1.2);
    }
}

/* ===== CONTROLS ===== */
.controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.btn-group {
    display: flex;
    gap: 8px;
}

.btn {
    border: none;
    border-radius: var(--radius-sm);
    font-family: inherit;
    font-weight: 600;
    cursor: pointer;
    transition:
        transform 0.15s,
        box-shadow 0.15s;
    display: flex;
    align-items: center;
    justify-content: center;
}

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

.btn-ghost {
    width: 48px;
    height: 48px;
    background: var(--bg-card);
    border: 1px solid rgba(255, 255, 255, 0.08);
    color: var(--text-dim);
}

.btn-ghost svg {
    width: 22px;
    height: 22px;
    stroke-width: 2.5;
}

.btn-ghost:active {
    background: var(--bg-cell);
    color: var(--text);
}

.btn-primary {
    background: linear-gradient(135deg, var(--purple), var(--pink));
    color: white;
    padding: 14px 24px;
    font-size: 0.95rem;
    box-shadow: 0 8px 24px var(--glow);
}

.btn-primary:active {
    box-shadow: 0 4px 12px var(--glow);
}

/* ===== GAME BOARD ===== */
.board-wrapper {
    aspect-ratio: 1;
    width: 100%;
}

#game-board {
    width: 100%;
    height: 100%;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: 10px;
    padding: 10px;
    background: var(--bg-card);
    border-radius: var(--radius);
    position: relative;
    box-shadow:
        0 0 0 1px rgba(255, 255, 255, 0.04),
        0 30px 60px -10px rgba(0, 0, 0, 0.5);
}

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

#particle-container {
    position: absolute;
    inset: 10px;
    pointer-events: none;
    overflow: hidden;
    border-radius: calc(var(--radius) - 6px);
    z-index: 50;
}

/* ===== TILES ===== */
.tile {
    position: absolute;
    display: flex;
    justify-content: center;
    align-items: center;
    font-weight: 700;
    font-size: 2rem;
    border-radius: var(--radius-sm);
    color: white;
    transition:
        top 0.12s ease-out,
        left 0.12s ease-out;
    z-index: 10;
    will-change: top, left, transform;
}

.tile-new {
    animation: appear 0.15s cubic-bezier(0.34, 1.4, 0.64, 1);
}

.tile-merged {
    animation: merge 0.2s cubic-bezier(0.34, 1.4, 0.64, 1);
    z-index: 20;
}

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

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

/* Tile colors */
.tile[data-value='1'] {
    background: linear-gradient(140deg, #6366f1, #818cf8);
    box-shadow: 0 6px 20px rgba(99, 102, 241, 0.4);
}
.tile[data-value='2'] {
    background: linear-gradient(140deg, #8b5cf6, #a78bfa);
    box-shadow: 0 6px 20px rgba(139, 92, 246, 0.4);
}
.tile[data-value='4'] {
    background: linear-gradient(140deg, #ec4899, #f472b6);
    box-shadow: 0 6px 20px rgba(236, 72, 153, 0.4);
}
.tile[data-value='8'] {
    background: linear-gradient(140deg, #3b82f6, #60a5fa);
    box-shadow: 0 6px 20px rgba(59, 130, 246, 0.4);
}
.tile[data-value='16'] {
    background: linear-gradient(140deg, #06b6d4, #22d3ee);
    box-shadow: 0 6px 20px rgba(6, 182, 212, 0.4);
}
.tile[data-value='32'] {
    background: linear-gradient(140deg, #22c55e, #4ade80);
    box-shadow: 0 6px 20px rgba(34, 197, 94, 0.4);
}
.tile[data-value='34'] {
    background: linear-gradient(140deg, #f97316, #fdba74);
    box-shadow: 0 6px 20px rgba(249, 115, 22, 0.4);
}
.tile[data-value='64'] {
    background: linear-gradient(140deg, #eab308, #fde047);
    box-shadow: 0 6px 20px rgba(234, 179, 8, 0.4);
}

/* 68 - WINNER! */
.tile[data-value='68'] {
    background: linear-gradient(
        135deg,
        #ff6b6b,
        #feca57,
        #48dbfb,
        #ff9ff3,
        #ff6b6b
    );
    background-size: 400% 400%;
    box-shadow:
        0 0 50px rgba(255, 107, 107, 0.5),
        0 0 100px rgba(254, 202, 87, 0.3),
        inset 0 0 20px rgba(255, 255, 255, 0.2);
    color: #1a1a2e;
    font-size: 1.8rem;
    font-weight: 900;
    animation:
        rainbow 4s linear infinite,
        glow68 2s ease-in-out infinite alternate;
}

@keyframes rainbow {
    0% {
        background-position: 0% 50%;
    }
    100% {
        background-position: 400% 50%;
    }
}

@keyframes glow68 {
    from {
        box-shadow:
            0 0 50px rgba(255, 107, 107, 0.5),
            0 0 100px rgba(254, 202, 87, 0.3);
    }
    to {
        box-shadow:
            0 0 70px rgba(255, 107, 107, 0.7),
            0 0 140px rgba(254, 202, 87, 0.5);
    }
}

/* Higher tiles */
.tile[data-value='128'],
.tile[data-value='136'] {
    background: linear-gradient(140deg, #f43f5e, #fb7185);
    box-shadow: 0 6px 24px rgba(244, 63, 94, 0.45);
    font-size: 1.6rem;
}
.tile[data-value='256'],
.tile[data-value='272'] {
    background: linear-gradient(140deg, #be123c, #fb7185);
    box-shadow: 0 6px 24px rgba(190, 18, 60, 0.45);
    font-size: 1.6rem;
}

/* ===== EFFECTS ===== */
.particle {
    position: absolute;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    pointer-events: none;
    animation: fly 0.5s ease-out forwards;
}

@keyframes fly {
    to {
        opacity: 0;
        transform: translate(var(--vx), var(--vy)) scale(0);
    }
}

.score-popup {
    position: absolute;
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--yellow);
    text-shadow: 0 2px 10px rgba(250, 204, 21, 0.5);
    pointer-events: none;
    animation: float 0.6s ease-out forwards;
    z-index: 60;
}

@keyframes float {
    to {
        opacity: 0;
        transform: translate(-50%, -35px) scale(1.2);
    }
}

.combo-text {
    position: absolute;
    font-size: 1.4rem;
    font-weight: 800;
    background: linear-gradient(135deg, var(--yellow), var(--orange));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    pointer-events: none;
    animation: combo 0.6s ease-out forwards;
    transform: translate(-50%, -50%);
    z-index: 70;
}

@keyframes combo {
    0% {
        opacity: 0;
        transform: translate(-50%, -50%) scale(0.5);
    }
    20% {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1.15);
    }
    100% {
        opacity: 0;
        transform: translate(-50%, -70%) scale(1);
    }
}

/* ===== OVERLAY ===== */
.game-overlay {
    position: absolute;
    inset: 10px;
    background: rgba(15, 15, 26, 0.92);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    border-radius: calc(var(--radius) - 6px);
    z-index: 100;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
    padding: 24px;
}

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

.game-overlay.win {
    background: radial-gradient(
        circle at center,
        rgba(250, 204, 21, 0.12),
        rgba(15, 15, 26, 0.94)
    );
}

#game-message {
    font-size: 2.2rem;
    font-weight: 800;
    margin-bottom: 8px;
}

.game-overlay:not(.win) #game-message {
    color: var(--red);
}

.game-overlay.win #game-message {
    background: linear-gradient(135deg, var(--yellow), var(--orange));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

#game-message-sub {
    font-size: 1rem;
    color: var(--text-dim);
    margin-bottom: 28px;
}

.overlay-buttons {
    display: flex;
    gap: 14px;
}

.overlay-buttons .btn {
    padding: 14px 28px;
    font-size: 1rem;
}

#continue-button {
    background: linear-gradient(135deg, var(--green), #4ade80);
    color: white;
    display: none;
}

#try-again-button {
    background: linear-gradient(135deg, var(--purple), var(--pink));
    color: white;
}

/* ===== FOOTER ===== */
.hint {
    text-align: center;
    font-size: 0.8rem;
    color: var(--text-dim);
}

.hint kbd {
    background: var(--bg-card);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 6px;
    padding: 4px 8px;
    margin: 0 3px;
    font-family: inherit;
}

.hint .mobile {
    display: inline;
}
.hint .desktop {
    display: none;
}

@media (hover: hover) and (pointer: fine) {
    .hint .mobile {
        display: none;
    }
    .hint .desktop {
        display: inline;
    }

    .btn-ghost:hover {
        background: var(--bg-cell);
        color: var(--text);
    }
    .btn-primary:hover {
        transform: translateY(-2px);
        box-shadow: 0 12px 32px var(--glow);
    }
}

/* ===== RESPONSIVE ===== */
@media (max-width: 400px) {
    body {
        padding: 16px;
    }
    .game-container {
        gap: 16px;
    }
    .logo {
        font-size: 2rem;
    }
    .score-card {
        padding: 8px 14px;
        min-width: 65px;
    }
    .score-value {
        font-size: 1.3rem;
    }
    .btn-ghost {
        width: 44px;
        height: 44px;
    }
    .btn-primary {
        padding: 12px 20px;
        font-size: 0.9rem;
    }
    #game-board {
        gap: 8px;
        padding: 8px;
        border-radius: 16px;
    }
    .grid-cell {
        border-radius: 12px;
    }
    .tile {
        font-size: 1.7rem;
        border-radius: 12px;
    }
    .tile[data-value='68'] {
        font-size: 1.5rem;
    }
    .tile[data-value='128'],
    .tile[data-value='136'],
    .tile[data-value='256'],
    .tile[data-value='272'] {
        font-size: 1.3rem;
    }
    #game-message {
        font-size: 1.8rem;
    }
}

@media (max-height: 600px) and (orientation: landscape) {
    body {
        padding: 12px;
    }
    .game-container {
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: center;
        align-items: center;
        gap: 16px;
        max-width: 100%;
    }
    header {
        flex-direction: column;
        gap: 8px;
    }
    .logo {
        font-size: 1.6rem;
    }
    .subtitle {
        display: none;
    }
    .score-card {
        padding: 6px 10px;
        min-width: 55px;
    }
    .score-value {
        font-size: 1.1rem;
    }
    .controls {
        flex-direction: column;
        gap: 8px;
    }
    .btn-ghost {
        width: 38px;
        height: 38px;
    }
    .btn-primary {
        padding: 10px 14px;
        font-size: 0.8rem;
    }
    .board-wrapper {
        height: calc(100vh - 40px);
        width: auto;
        aspect-ratio: 1;
    }
    .hint {
        display: none;
    }
}
