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

body {
    background: #1a1a2e;
    color: #e0e0e0;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    overflow-x: hidden;
    -webkit-tap-highlight-color: transparent;
}

/* ── Back Link ───────────────────────────────────────── */
.back-link {
    position: absolute;
    top: 1rem;
    left: 1rem;
    color: #7f8fa6;
    text-decoration: none;
    font-size: 0.95rem;
    transition: color 0.2s;
    z-index: 10;
}

.back-link:hover {
    color: #e0e0e0;
}

/* ── Container ───────────────────────────────────────── */
.container {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    max-width: 700px;
    padding: 1rem 1rem 2rem;
    margin-top: 2rem;
}

.title {
    font-size: 2rem;
    font-weight: 700;
    color: #e2e2e2;
    margin-bottom: 0.75rem;
    letter-spacing: 0.02em;
}

/* ── Stats Bar ───────────────────────────────────────── */
.stats-bar {
    display: flex;
    justify-content: center;
    gap: 2.5rem;
    margin-bottom: 1rem;
    width: 100%;
}

.stat {
    font-size: 1.1rem;
    color: #a0b0c0;
    font-weight: 500;
}

.stat span {
    color: #e0e0e0;
    font-weight: 700;
}

/* ── Difficulty Bar ─────────────────────────────────── */
.difficulty-bar {
    display: flex;
    justify-content: center;
    gap: 0.5rem;
    margin-bottom: 1.25rem;
}

.diff-btn {
    padding: 0.4rem 1.2rem;
    font-size: 0.9rem;
    font-weight: 600;
    color: #a0b0c0;
    background: #16213e;
    border: 2px solid #0f3460;
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.2s, color 0.2s, border-color 0.2s;
}

.diff-btn:hover {
    background: #1a4a7a;
    color: #e0e0e0;
}

.diff-btn.active {
    background: #0f3460;
    color: #ffffff;
    border-color: #3a7bd5;
}

/* ── Grid ────────────────────────────────────────────── */
.grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: clamp(6px, 1.5vmin, 12px);
    width: 100%;
    max-width: 560px;
    perspective: 900px;
    touch-action: manipulation;
}

.grid.cols-3 {
    grid-template-columns: repeat(3, 1fr);
    max-width: 420px;
}

.grid.cols-4 {
    grid-template-columns: repeat(4, 1fr);
    max-width: 560px;
}

.grid.cols-5 {
    grid-template-columns: repeat(5, 1fr);
    max-width: 650px;
}

/* ── Card ────────────────────────────────────────────── */
.card {
    position: relative;
    aspect-ratio: 1;
    min-width: 100px;
    min-height: 100px;
    cursor: pointer;
    -webkit-user-select: none;
    user-select: none;
    touch-action: manipulation;
}

.card-inner {
    position: relative;
    width: 100%;
    height: 100%;
    transition: transform 0.5s ease;
    transform-style: preserve-3d;
}

.card.flipped .card-inner,
.card.matched .card-inner {
    transform: rotateY(180deg);
}

.card-face {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 10px;
    border: 2px solid #16213e;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
}

/* Back face (face-down, "?") */
.card-back {
    background: #0f3460;
    font-size: clamp(1.5rem, 5vmin, 2.5rem);
    color: rgba(255, 255, 255, 0.35);
    font-weight: 700;
}

/* Front face (emoji) */
.card-front {
    background: #16213e;
    transform: rotateY(180deg);
    font-size: clamp(1.8rem, 6vmin, 3rem);
}

/* Matched state */
.card.matched {
    pointer-events: none;
}

.card.matched .card-front {
    opacity: 0.55;
}

/* Match-found pulse */
@keyframes matchPulse {
    0%   { transform: rotateY(180deg) scale(1); }
    40%  { transform: rotateY(180deg) scale(1.12); }
    100% { transform: rotateY(180deg) scale(1); }
}

.card.match-anim .card-inner {
    animation: matchPulse 0.45s ease;
}

/* Deal-in animation */
@keyframes dealIn {
    0% {
        opacity: 0;
        transform: translateY(40px) scale(0.7);
    }
    100% {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.card.deal {
    opacity: 0;
    animation: dealIn 0.35s ease forwards;
}

/* ── Button ──────────────────────────────────────────── */
.btn {
    margin-top: 1.5rem;
    padding: 0.7rem 2rem;
    font-size: 1rem;
    font-weight: 600;
    color: #e0e0e0;
    background: #0f3460;
    border: 2px solid #16213e;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.2s, transform 0.1s;
}

.btn:hover {
    background: #1a4a7a;
}

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

/* ── How to Play ─────────────────────────────────────── */
.how-to-play {
    margin-top: 1.5rem;
    width: 100%;
    max-width: 560px;
    background: #16213e;
    border: 2px solid #0f3460;
    border-radius: 10px;
    overflow: hidden;
}

.how-to-play summary {
    padding: 0.75rem 1.25rem;
    font-size: 1rem;
    font-weight: 600;
    color: #a0b0c0;
    cursor: pointer;
    transition: color 0.2s, background 0.2s;
    list-style: none;
}

.how-to-play summary::-webkit-details-marker {
    display: none;
}

.how-to-play summary::before {
    content: '▸ ';
    display: inline;
    transition: transform 0.2s;
}

.how-to-play[open] summary::before {
    content: '▾ ';
}

.how-to-play summary:hover {
    color: #e0e0e0;
    background: rgba(15, 52, 96, 0.4);
}

.how-to-play ul {
    padding: 0.5rem 1.25rem 1rem 2.25rem;
    list-style: disc;
}

.how-to-play li {
    font-size: 0.92rem;
    color: #a0b0c0;
    line-height: 1.7;
}

/* ── Win Overlay ─────────────────────────────────────── */
.overlay {
    position: fixed;
    inset: 0;
    background: rgba(10, 10, 20, 0.88);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.4s ease;
}

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

.overlay-content {
    text-align: center;
    background: #16213e;
    padding: 2.5rem 3rem;
    border-radius: 16px;
    border: 2px solid #0f3460;
    transform: scale(0.85);
    transition: transform 0.4s ease;
}

.overlay.visible .overlay-content {
    transform: scale(1);
}

.win-title {
    font-size: 2.2rem;
    margin-bottom: 0.75rem;
    color: #ffffff;
}

.win-text {
    font-size: 1.15rem;
    color: #a0b0c0;
    margin-bottom: 1.5rem;
}

/* ── Responsive ──────────────────────────────────────── */
@media (max-width: 480px) {
    .container {
        margin-top: 3rem;
        padding: 0.5rem 0.75rem 1.5rem;
    }

    .title {
        font-size: 1.5rem;
    }

    .stats-bar {
        gap: 1.5rem;
    }

    .stat {
        font-size: 1rem;
    }

    .card {
        min-width: 70px;
        min-height: 70px;
    }

    .grid {
        max-width: 95vw;
    }

    .grid.cols-3 {
        max-width: 95vw;
    }

    .grid.cols-4 {
        max-width: 95vw;
    }

    .grid.cols-5 {
        max-width: 95vw;
    }

    /* ~22vw per card for 4 columns with small gaps */
    .grid.cols-4 {
        grid-template-columns: repeat(4, 22vw);
        justify-content: center;
    }

    .grid.cols-3 {
        grid-template-columns: repeat(3, 28vw);
        justify-content: center;
    }

    .grid.cols-5 {
        grid-template-columns: repeat(5, 17vw);
        justify-content: center;
    }

    .how-to-play {
        max-width: 95vw;
    }

    .diff-btn {
        padding: 0.35rem 0.9rem;
        font-size: 0.85rem;
    }
}

@media (max-height: 600px) {
    .container {
        margin-top: 2.5rem;
    }

    .grid {
        max-width: 55vh;
    }
}
