:root {
    --card-size: clamp(72px, 20vw, 98px);
    --card-radius: 14px;
    --card-gap: clamp(8px, 2vw, 14px);
    --back-color-a: #7c3aed;
    --back-color-b: #4f46e5;
    --text-dark: #2f3542;
}

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

body {
    min-height: 100dvh;
    background:
        radial-gradient(circle at 20% 18%, rgba(250, 204, 21, 0.18), transparent 28%),
        radial-gradient(circle at 88% 78%, rgba(244, 114, 182, 0.2), transparent 28%),
        linear-gradient(160deg, #1e1b4b 0%, #312e81 50%, #4c1d95 100%);
    font-family: 'Fredoka One', 'Nunito', cursive;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    align-items: center;
    user-select: none;
    -webkit-user-select: none;
}

button {
    font: inherit;
}

.pairs-display {
    grid-column: 3;
    justify-self: end;
    display: flex;
    align-items: center;
    gap: 4px;
    font-family: 'Fredoka One', cursive;
    font-size: 1.2rem;
    line-height: 1;
    color: #fff;
    background: rgba(255, 255, 255, 0.16);
    border: 1.5px solid rgba(255, 255, 255, 0.24);
    padding: 8px 12px;
    border-radius: 999px;
    backdrop-filter: blur(8px);
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.22);
}

.pairs-icon {
    color: #f472b6;
    font-size: 1rem;
    margin-left: 2px;
}

.memory-main {
    flex: 1;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 86px 16px 24px;
}

.memory-grid {
    display: grid;
    grid-template-columns: repeat(4, var(--card-size));
    grid-template-rows: repeat(3, var(--card-size));
    gap: var(--card-gap);
    justify-content: center;
    align-content: center;
}

.memory-card {
    width: var(--card-size);
    height: var(--card-size);
    border: 0;
    background: transparent;
    cursor: pointer;
    perspective: 700px;
    -webkit-tap-highlight-color: transparent;
}

.memory-card:active {
    transform: scale(0.96);
}

.card-inner {
    position: relative;
    display: block;
    width: 100%;
    height: 100%;
    transform-style: preserve-3d;
    transition: transform 0.35s ease;
    border-radius: var(--card-radius);
}

.memory-card.is-flipped .card-inner {
    transform: rotateY(180deg);
}

.card-front,
.card-back {
    position: absolute;
    inset: 0;
    border-radius: var(--card-radius);
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
}

.card-back {
    background-color: var(--back-color-a);
    background-image:
        radial-gradient(circle, rgba(255, 255, 255, 0.18) 1px, transparent 1.5px),
        linear-gradient(135deg, var(--back-color-a), var(--back-color-b));
    background-size: 16px 16px, 100% 100%;
    box-shadow:
        0 5px 14px rgba(0, 0, 0, 0.36),
        inset 0 1px 0 rgba(255, 255, 255, 0.24);
}

.card-back::after {
    content: '⭐';
    font-size: clamp(1.8rem, 7vw, 2.3rem);
    line-height: 1;
    opacity: 0.95;
    filter: drop-shadow(0 3px 2px rgba(0, 0, 0, 0.18));
}

.card-front {
    background: #ffffff;
    transform: rotateY(180deg);
    box-shadow: 0 5px 14px rgba(0, 0, 0, 0.22);
    font-size: clamp(2.25rem, 8vw, 3.25rem);
    line-height: 1;
}

.memory-card.is-matched .card-front {
    background: linear-gradient(135deg, #dcfce7, #a7f3d0);
    box-shadow:
        0 0 0 3px #34d399,
        0 5px 14px rgba(52, 211, 153, 0.3);
}

.memory-card.is-matched .card-inner {
    animation: card-match 0.42s ease forwards;
}

.win-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.58);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    backdrop-filter: blur(4px);
    padding: 20px;
}

.win-overlay[hidden] {
    display: none;
}

.win-box {
    width: min(92vw, 360px);
    background: #fff;
    border-radius: 24px;
    padding: 36px 28px;
    text-align: center;
    box-shadow: 0 24px 60px rgba(0, 0, 0, 0.32);
    animation: pop-in 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.win-emoji {
    font-size: 3.5rem;
    line-height: 1;
    margin-bottom: 12px;
}

.win-title {
    font-family: 'Fredoka One', cursive;
    font-size: 1.8rem;
    color: #7c3aed;
    margin-bottom: 24px;
}

.win-replay-btn {
    background: linear-gradient(135deg, #7c3aed, #4f46e5);
    color: #fff;
    border: none;
    border-radius: 999px;
    padding: 14px 28px;
    font-family: 'Fredoka One', cursive;
    font-size: 1.25rem;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 10px;
    box-shadow: 0 4px 16px rgba(124, 58, 237, 0.4);
    min-height: 56px;
}

.win-replay-btn:active {
    transform: scale(0.96);
}

@keyframes card-match {
    0% { transform: rotateY(180deg) scale(1); }
    40% { transform: rotateY(180deg) scale(1.18); }
    70% { transform: rotateY(180deg) scale(0.93); }
    100% { transform: rotateY(180deg) scale(1); }
}

@keyframes pop-in {
    from {
        transform: scale(0.5);
        opacity: 0;
    }
    to {
        transform: scale(1);
        opacity: 1;
    }
}

@keyframes confetti-fall {
    0% {
        transform: translateY(-10vh) rotate(0deg);
        opacity: 1;
    }
    100% {
        transform: translateY(110vh) rotate(720deg);
        opacity: 0;
    }
}

.confetti {
    position: fixed;
    top: -10px;
    width: 10px;
    height: 14px;
    pointer-events: none;
    z-index: 110;
    animation-name: confetti-fall;
    animation-timing-function: linear;
    animation-fill-mode: forwards;
}

@media (max-width: 520px) {
    :root {
        --card-size: clamp(68px, 21vw, 86px);
        --card-gap: 9px;
    }

    .memory-main {
        padding-top: 80px;
    }

    .pairs-display {
        font-size: 0.95rem;
        padding: 7px 9px;
    }

    .win-title {
        font-size: 1.5rem;
    }
}

@media (max-width: 360px) {
    :root {
        --card-size: 66px;
        --card-gap: 7px;
    }

    .memory-main {
        padding-left: 8px;
        padding-right: 8px;
    }
}
