@import url('https://fonts.googleapis.com/css2?family=Fredoka:wght@400;600&display=swap');

/* GLOBAL RESET & LOCK */
* { box-sizing: border-box; }
html, body {
    width: 100%; overflow-x: hidden;
    overscroll-behavior: none; touch-action: pan-y;
}

:root {
    --bg-color: #3f0636;
    --text-color: #ffffff;
    --card-back: #e84545; /* The color of the card when face down */
    --card-front: #fff;   /* Background behind the character image */
    --accent: #ffd700;
}

[data-theme="light"] {
    --bg-color: #f4f4f9;
    --text-color: #333;
    --card-back: #ff6b6b;
    --card-front: #fff;
    --accent: #2e86de;
}

body {
    font-family: 'Fredoka', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    margin: 0;
    display: flex; flex-direction: column; align-items: center;
    min-height: 100vh;
    padding: 20px 0;
}

/* Header & Stats (Same as Puzzle) */
header {
    width: 100%; max-width: 500px; padding: 0 20px;
    display: flex; justify-content: space-between; align-items: center;
    margin-bottom: 20px;
}
.stats-bar {
    display: flex; gap: 20px; font-size: 1.2rem; font-weight: 600;
    margin-bottom: 20px;
    background: rgba(255, 255, 255, 0.1);
    padding: 10px 25px; border-radius: 50px;
}

/* --- MEMORY GRID LAYOUT --- */
#memory-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    
    /* Tighter gap for mobile reliability */
    gap: 6px; 
    
    /* Responsive Frame */
    width: 92vw;       /* Use slightly more screen real estate */
    max-width: 380px;
    margin: 0 auto 30px auto; 
    
    background-color: var(--board-bg);
    
    /* Tighter padding to prevent overflow */
    padding: 10px;       
    
    border-radius: 20px;
    border: 8px solid var(--text-color);
    box-shadow: 0 15px 35px rgba(0,0,0,0.4);
    perspective: 1000px;
}

/* The box inside the overlay */
.popup-frame {
    background-color: var(--bg-color); /* Match theme background */
    padding: 40px;
    border-radius: 24px;
    border: 6px solid var(--accent); /* Gold/Blue border */
    box-shadow: 0 20px 50px rgba(0,0,0,0.5);
    
    display: flex; 
    flex-direction: column; 
    align-items: center;
    max-width: 90%;
    animation: popIn 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275); /* Bouncy entrance */
}

/* Optional Bouncy Animation */
@keyframes popIn {
    0% { transform: scale(0.5); opacity: 0; }
    100% { transform: scale(1); opacity: 1; }
}

/* --- THE CARD (Container) --- */
.card {
    background-color: transparent;
    cursor: pointer;
    position: relative;
    transform-style: preserve-3d;
    transition: transform 0.4s;
    
    /* THE FIXES: */
    width: 100%;       /* Fill the column */
    min-width: 0;      /* <--- CRITICAL: Prevents grid blowout */
    aspect-ratio: 1/1; /* Keep it square */
}

/* Optional: Make them slightly bigger on Desktop/Tablets */
@media (min-width: 400px) {
    .card {
        width: 80px;
        height: 80px;
    }
}

/* Flip Action */
.card.flip {
    transform: rotateY(180deg);
}

/* --- FRONT & BACK FACES --- */
.front-face, .back-face {
    width: 100%; height: 100%;
    position: absolute;
    border-radius: 8px;
    backface-visibility: hidden; /* Hides the back when flipped */
    display: flex; justify-content: center; align-items: center;
    box-shadow: 0 4px 6px rgba(0,0,0,0.3);
    border: 2px solid rgba(255,255,255,0.2);
}

/* FACE UP (The Character) */
.front-face {
    background-color: var(--card-front);
    transform: rotateY(180deg); /* Pre-rotated so it matches the flip */
}
.front-face img {
    width: 85%;        /* Slightly smaller to ensure it doesn't touch edges */
    height: 85%;
    object-fit: contain;
    
    /* Prevent image from pushing the cell open */
    max-width: 100%;   
    display: block;
}

/* FACE DOWN (The Pattern/Logo) */
.back-face {
    background-color: var(--card-back);
    font-size: 2rem; color: rgba(255,255,255,0.5);
}
/* Optional: Add a Question Mark on the back */
.back-face::after {
    content: "?";
}

/* --- OVERLAY --- */
#game-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.9);
    display: flex; flex-direction: column; justify-content: center; align-items: center;
    z-index: 100;
    opacity: 0; pointer-events: none; transition: opacity 0.3s;
    text-align: center;
}
#game-overlay.active { opacity: 1; pointer-events: all; }

.stats-box {
    background: rgba(255,255,255,0.1);
    padding: 20px; border-radius: 12px; margin: 20px 0;
    min-width: 200px;
}

/* BUTTONS */
button {
    padding: 12px 24px; cursor: pointer; border: none; border-radius: 12px;
    font-family: 'Fredoka', sans-serif; font-weight: 600; font-size: 1rem;
    color: white; box-shadow: 0 4px 0 rgba(0,0,0,0.2);
    transition: transform 0.1s;
}
button:active { transform: translateY(4px); box-shadow: none; }
.btn-easy { background-color: #00b894; }
.btn-med { background-color: var(--accent); color: #333; }
#theme-btn { padding: 5px; width: 40px; height: 40px; border-radius: 50%; background: rgba(255,255,255,0.2); }