:root {
    --bg-gradient-1: #c2e9fb;
    --bg-gradient-2: #a1c4fd;
    --primary-color: #ff4757;
    --secondary-color: #1e90ff;
    --text-color: #2f3542;
    --card-bg-front: #ffffff;
    --card-bg-back: #ffa502;
    --card-bg-back-hover: #ff9f43;
    --success-color: #2ed573;
    --board-bg: rgba(255, 255, 255, 0.95);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: 'FbSpacer', 'Nunito', sans-serif;
    user-select: none;
}

body {
    background: linear-gradient(135deg, var(--bg-gradient-1), var(--bg-gradient-2), #fecfef, #ff9a9e);
    background-size: 400% 400%;
    animation: gradientBG 15s ease infinite;
    color: var(--text-color);
    display: flex;
    justify-content: center;
    align-items: flex-start;
    min-height: 100vh;
    overflow-x: hidden;
    overflow-y: auto;
    padding: 20px;
    -webkit-tap-highlight-color: transparent;
}

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

/* --- Start Menu Styles --- */
.menu-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(161, 196, 253, 0.8);
    backdrop-filter: blur(8px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
    transition: opacity 0.5s ease;
}

.menu-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

.menu-content {
    background-color: white;
    padding: 40px;
    border-radius: 40px;
    text-align: center;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2), inset 0 0 0 6px rgba(161, 196, 253, 0.3);
    border: 6px solid #fff;
    max-width: 600px;
    width: 90%;
}

.menu-content h1 {
    font-size: 3.5rem;
    font-weight: 900;
    margin-bottom: 10px;
    color: var(--primary-color);
    text-shadow: 2px 2px 0px rgba(0, 0, 0, 0.1);
    letter-spacing: -1px;
}

.menu-content h2 {
    font-size: 1.8rem;
    font-weight: 700;
    color: #57606f;
    margin-bottom: 30px;
}

.timer-selection {
    margin-bottom: 30px;
    background: rgba(161, 196, 253, 0.15);
    padding: 20px;
    border-radius: 25px;
    border: 2px dashed rgba(161, 196, 253, 0.5);
}

.timer-selection h3 {
    margin-bottom: 15px;
    font-size: 1.3rem;
    color: var(--text-color);
    font-weight: 800;
}

.timer-buttons {
    display: flex;
    justify-content: center;
    gap: 15px;
    flex-wrap: wrap;
}

.timer-btn,
.diff-btn {
    background-color: #fff;
    color: #57606f;
    border: 4px solid #dfe4ea;
    border-radius: 20px;
    padding: 10px 20px;
    font-size: 1.1rem;
    font-weight: 800;
    cursor: pointer;
    transition: transform 0.15s, box-shadow 0.15s, border-color 0.2s;
    box-shadow: 0 6px 0 #dfe4ea;
    outline: none;
}

.timer-btn:hover,
.diff-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 0 #dfe4ea;
}

.timer-btn.selected,
.diff-btn.selected {
    background-color: var(--secondary-color);
    color: white;
    border-color: var(--secondary-color);
    box-shadow: 0 6px 0 #0056b3;
    transform: translateY(2px);
}

.btn-start {
    font-size: 2rem;
    padding: 20px 60px;
    background-color: var(--success-color);
    color: #fff;
    border: none;
    border-radius: 50px;
    font-weight: 900;
    cursor: pointer;
    box-shadow: 0 8px 0 #26b160, 0 15px 20px rgba(0,0,0,0.1);
    transition: transform 0.1s, box-shadow 0.1s;
    text-transform: uppercase;
    letter-spacing: 1px;
    outline: none;
}

.btn-start:hover { transform: translateY(-2px); box-shadow: 0 10px 0 #26b160, 0 18px 25px rgba(0,0,0,0.15); }
.btn-start:active { transform: translateY(6px); box-shadow: 0 2px 0 #26b160; }

.hidden {
    display: none !important;
}

/* --- Game Container --- */
.game-container {
    background-color: var(--board-bg);
    border-radius: 40px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2), inset 0 0 0 6px rgba(161, 196, 253, 0.3);
    border: 6px solid #fff;
    padding: 4vw;
    width: 100%;
    max-width: 1000px;
    display: flex;
    flex-direction: column;
    align-items: center;
    margin: auto;
}

header {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 25px;
    flex-wrap: wrap;
    gap: 15px;
}

h1 {
    font-size: 2.5rem;
    font-weight: 900;
    color: var(--primary-color);
    text-shadow: 2px 2px 0px rgba(0, 0, 0, 0.05);
    margin: 0;
}

.stats {
    display: flex;
    gap: 20px;
}

.stat-box {
    background-color: white;
    padding: 8px 25px;
    border-radius: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    box-shadow: 0 8px 15px rgba(0, 0, 0, 0.05);
    border: 4px solid var(--secondary-color);
}

.stat-label {
    font-size: 0.9rem;
    font-weight: 800;
    color: #a4b0be;
    text-transform: uppercase;
}

.stat-value {
    font-size: 1.8rem;
    font-weight: 900;
    color: var(--secondary-color);
}

.btn {
    background-color: var(--primary-color);
    color: white;
    border: none;
    border-radius: 50px;
    padding: 15px 30px;
    font-size: 1.3rem;
    font-weight: 900;
    cursor: pointer;
    box-shadow: 0 8px 0 #e84118, 0 15px 20px rgba(0,0,0,0.1);
    transition: transform 0.1s, box-shadow 0.1s;
    text-transform: uppercase;
    letter-spacing: 1px;
    outline: none;
}

.btn:hover { transform: translateY(-2px); box-shadow: 0 10px 0 #e84118, 0 18px 25px rgba(0,0,0,0.15); }
.btn:active { transform: translateY(6px); box-shadow: 0 2px 0 #e84118; }

.btn-large {
    font-size: 1.5rem;
    padding: 15px 40px;
    margin-top: 20px;
}

.game-board {
    display: grid;
    gap: 12px;
    width: 100%;
}

/* Grid column definitions – applied via JS through data-cols attribute (CSP-safe) */
.game-board[data-cols="2"] { grid-template-columns: repeat(2, 1fr); }
.game-board[data-cols="3"] { grid-template-columns: repeat(3, 1fr); }
.game-board[data-cols="4"] { grid-template-columns: repeat(4, 1fr); }
.game-board[data-cols="5"] { grid-template-columns: repeat(5, 1fr); }
.game-board[data-cols="6"] { grid-template-columns: repeat(6, 1fr); }
.game-board[data-cols="7"] { grid-template-columns: repeat(7, 1fr); }
.game-board[data-cols="8"] { grid-template-columns: repeat(8, 1fr); }

.card {
    aspect-ratio: 1;
    perspective: 1000px;
    cursor: pointer;
    border-radius: 16px;
    width: 100%;
    height: 100%;
}

.card-inner {
    width: 100%;
    height: 100%;
    transition: transform 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    transform-style: preserve-3d;
    position: relative;
    border-radius: 16px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

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

.card-front,
.card-back {
    width: 100%;
    height: 100%;
    position: absolute;
    backface-visibility: hidden;
    border-radius: 16px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}

.card-back {
    background-color: var(--card-bg-back);
    background-image: radial-gradient(circle at center, rgba(255, 255, 255, 0.4) 0%, transparent 70%);
    border: 3px solid rgba(255, 255, 255, 0.5);
    font-size: 3rem;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: background-color 0.2s;
}

.card:hover .card-back {
    background-color: var(--card-bg-back-hover);
}

.card-back::after {
    content: '?';
    color: white;
    font-size: clamp(1.2rem, 5vw, 3rem);
    font-weight: 900;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}

.card-front {
    background-color: var(--card-bg-front);
    transform: rotateY(180deg);
    border: 3px solid var(--secondary-color);
}

.card.matched .card-front {
    background-color: var(--success-color);
    border-color: #00c853;
}

.card-front .animal-emoji {
    font-size: clamp(1.4rem, 6vw, 3.5rem);
    margin-top: 5px;
    line-height: 1;
}

.card-front .animal-name {
    font-size: clamp(0.55rem, 2vw, 0.8rem);
    font-weight: 800;
    text-transform: capitalize;
    margin-top: 2px;
    color: var(--text-color);
    text-align: center;
    line-height: 1;
    padding: 0 2px;
}

.timer-alert {
    color: #f44336;
    animation: flashAlert 1s infinite alternate;
}

@keyframes flashAlert {
    from {
        color: #f44336;
    }

    to {
        color: #d32f2f;
        transform: scale(1.05);
    }
}


/* --- Responsive Adjustments --- */
@media (max-width: 600px) {
    body {
        padding: 10px;
        padding-top: 70px; /* space for fixed back button */
    }

    .menu-overlay {
        align-items: flex-start;
        padding-top: 80px;
        /* Space for the back button */
        padding-bottom: 20px;
        overflow-y: auto;
    }

    .menu-content {
        padding: 20px;
        margin: auto;
        /* Centers effectively, allows scrolling if larger */
        width: 95%;
    }

    .menu-content h1 {
        font-size: 2.2rem;
    }

    .menu-content h2 {
        font-size: 1.3rem;
        margin-bottom: 15px;
    }

    .timer-selection {
        padding: 15px;
        margin-bottom: 15px;
        width: 100%;
    }

    .timer-btn,
    .diff-btn {
        padding: 10px 15px;
        font-size: 1rem;
        min-height: 48px;
        flex: 1 1 45%;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .btn-start {
        font-size: 1.4rem;
        padding: 15px 30px;
        min-height: 56px;
        width: 100%;
    }

    .btn-back-hub {
        top: 10px;
        right: 10px;
        font-size: 0.9rem;
        padding: 10px 15px;
        min-height: 48px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    /* --- Header: stack vertically on phones --- */
    header {
        flex-direction: column;
        align-items: center;
        gap: 10px;
        margin-bottom: 15px;
    }

    header h1 {
        font-size: 1.4rem;
        text-align: center;
    }

    .stats {
        flex-wrap: wrap;
        justify-content: center;
        gap: 8px;
        width: 100%;
    }

    .stat-box {
        padding: 6px 12px;
        flex: 1 1 auto;
        min-width: 70px;
    }

    .stat-label {
        font-size: 0.7rem;
    }

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

    .btn {
        font-size: 1rem;
        padding: 10px 20px;
    }

    /* --- Game Container --- */
    .game-container {
        padding: 12px;
        border-radius: 24px;
        border-width: 4px;
    }

    /* --- Cards --- */
    .game-board {
        gap: 8px;
    }

    .card-front .animal-emoji {
        font-size: clamp(2rem, 12vw, 4rem);
    }

    .card-front .animal-name {
        font-size: clamp(0.75rem, 3.5vw, 1rem);
    }

    .card-back::after {
        font-size: clamp(1.8rem, 10vw, 3.5rem);
    }

    .card {
        border-radius: 12px;
    }

    .card-inner {
        border-radius: 12px;
    }

    .card-front,
    .card-back {
        border-radius: 12px;
    }

}

/* Handle short screens (landscape mobile) */
@media (max-height: 700px) {
    .menu-overlay {
        align-items: flex-start;
        padding-top: 70px;
        padding-bottom: 20px;
        overflow-y: auto;
    }

    .menu-content {
        margin: auto;
    }
}