:root {
    --card-bg: #ffffff;
    --card-back-bg: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    --accent-color: #ffeb3b;
    --text-color: #333333;
    /* Dark text for light background */
    --bg-dark: rgba(0, 0, 0, 0.4);
}

.game-container {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 20px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    padding: 15px;
    max-width: 1100px;
    width: 100%;
    margin: 100px auto 20px;
    /* Space for fixed header */
    display: block;
    color: var(--text-color);
    font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.header h1 {
    font-size: 1.6em;
    color: #667eea;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 0;
}

.game-icon-small {
    height: 40px;
    width: auto;
    object-fit: contain;
}

.back-btn {
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
    color: white;
    border: none;
    padding: 12px 24px;
    border-radius: 25px;
    font-size: 1em;
    font-weight: bold;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
    box-shadow: 0 4px 15px rgba(245, 87, 108, 0.4);
}

.back-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(245, 87, 108, 0.6);
}

.game-info {
    display: flex;
    justify-content: space-around;
    gap: 10px;
    margin-bottom: 20px;
    background: transparent;
    padding: 0;
    box-shadow: none;
    backdrop-filter: none;
}

.info-item {
    background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
    padding: 10px 18px;
    border-radius: 12px;
    text-align: center;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
    flex: 1;
    min-width: 100px;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.info-label {
    font-size: 0.95em;
    font-weight: bold;
    color: #555;
    display: block;
    margin-bottom: 3px;
    opacity: 1;
}

.info-value {
    font-size: 1.5em;
    font-weight: bold;
    color: #333;
}

.game-screen {
    position: relative;
    min-height: 400px;
    background: #f0f0f0;
    border-radius: 15px;
    box-shadow: inset 0 4px 15px rgba(0, 0, 0, 0.1);
    padding: 15px;
    overflow: hidden;
}

.memory-grid {
    display: grid;
    gap: 10px;
    margin: 0 auto;
    perspective: 1000px;
    justify-content: center;
}

/* Response grid sizing */
.memory-grid.grid-4 {
    grid-template-columns: repeat(4, 1fr);
    max-width: 500px;
}

.memory-grid.grid-6 {
    grid-template-columns: repeat(6, 1fr);
    max-width: 700px;
}

.memory-grid.grid-8 {
    grid-template-columns: repeat(8, 1fr);
    max-width: 850px;
}

/* Mobile Optimization */
@media (max-width: 768px) {
    .game-container {
        margin: 80px auto 10px;
        /* Space for fixed header */
        padding: 10px;
        max-width: 100%;
    }

    .header {
        flex-direction: row;
        margin-bottom: 15px;
        padding: 0 5px;
    }

    .header h1 {
        font-size: 1.4rem;
    }

    .back-btn {
        padding: 6px 12px;
        font-size: 0.9rem;
    }

    .game-info {
        padding: 10px 5px;
        margin-bottom: 15px;
        gap: 5px;
    }

    .info-label {
        font-size: 0.7rem;
        display: none;
        /* Hide text labels on very small screens if needed, or keep compact */
    }

    /* Show icons only on very small screens? or just stack compactly */
    .info-item {
        min-width: 60px;
    }

    .info-label {
        display: block;
        font-size: 0.7rem;
        margin-bottom: 0;
    }

    .info-value {
        font-size: 1rem;
    }

    .game-screen {
        padding: 10px 5px;
        min-height: 50vh;
    }

    .memory-grid {
        gap: 4px;
        /* Tighter gap for mobile */
        width: 100%;
    }

    /* Force correct columns even on mobile to maintain game geometry */
    /* Adjust max-width to ensure they fit */
    .memory-grid.grid-4 {
        max-width: 100%;
    }

    .memory-grid.grid-6 {
        max-width: 100%;
        grid-template-columns: repeat(6, 1fr) !important;
    }

    .memory-grid.grid-8 {
        max-width: 100%;
        grid-template-columns: repeat(8, 1fr) !important;
    }

    .memory-card {
        border-radius: 4px;
    }

    .card-face {
        border-width: 1px;
        border-radius: 4px;
        font-size: 1.4rem;
        /* Smaller emojis */
    }
}

@media (max-width: 480px) {

    /* Even smaller adjustments for narrow phones */
    .card-face {
        font-size: 1.1rem;
    }

    .header h1 {
        font-size: 1.2rem;
    }

    /* If 8x8 is too small, maybe we restrict it? or just let it be tiny */
    .memory-grid.grid-8 .card-face {
        font-size: 0.9rem;
    }
}

.memory-card {
    aspect-ratio: 1;
    position: relative;
    cursor: pointer;
    transform-style: preserve-3d;
    transition: transform 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.memory-card.flipped,
.memory-card.matched {
    transform: rotateY(180deg);
}

.memory-card.matched {
    transform: rotateY(180deg) scale(0.92);
    opacity: 0.6;
    cursor: default;
    filter: grayscale(0.5);
}

.card-face {
    position: absolute;
    width: 100%;
    height: 100%;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2.2rem;
    border-radius: 8px;
    border: 2px solid rgba(255, 255, 255, 0.1);
    user-select: none;
}

@media (max-width: 400px) {
    .card-face {
        font-size: 1.6rem;
    }
}

.card-front {
    background: var(--card-bg);
    transform: rotateY(180deg);
    z-index: 2;
}

.card-back {
    background: var(--card-back-bg);
    color: white;
    transform: rotateY(0deg);
    z-index: 1;
}

.card-back::after {
    content: "?";
    font-weight: 800;
    font-size: 1.5rem;
    opacity: 0.3;
}

/* Screens */
.start-screen,
.game-over {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(10px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    border-radius: 20px;
}

.hidden {
    display: none !important;
}

.start-content,
.game-over-content {
    background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
    padding: 30px;
    border-radius: 20px;
    text-align: center;
    max-width: 500px;
    width: 90%;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
    position: relative;
    z-index: 1000;
    animation: slideIn 0.5s ease-out;
}

@keyframes slideIn {
    from {
        transform: scale(0.8);
        opacity: 0;
    }

    to {
        transform: scale(1);
        opacity: 1;
    }
}

.start-content h2,
.game-over-content h2 {
    font-size: 2em;
    color: #667eea;
    margin-bottom: 10px;
    line-height: 1.3;
}

.start-content p {
    font-size: 1.05em;
    color: #555;
    margin-bottom: 15px;
}

.setup-group {
    margin: 20px 0;
    text-align: left;
}

.setup-group label {
    display: block;
    margin-bottom: 8px;
    font-weight: 600;
    font-size: 0.95rem;
    color: #444;
}

.setup-group input {
    width: 100%;
    padding: 12px;
    border-radius: 8px;
    border: 2px solid #667eea;
    background: white;
    color: #333;
    font-size: 1rem;
    text-align: center;
}

.grid-options,
.theme-options {
    display: flex;
    gap: 8px;
}

.grid-btn,
.theme-btn {
    flex: 1;
    padding: 12px 5px;
    background: white;
    border: 2px solid #667eea;
    color: #667eea;
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.3s;
    font-size: 0.9rem;
    font-weight: 600;
}

.grid-btn:hover,
.theme-btn:hover {
    background: #f0f4ff;
}

.grid-btn.selected,
.theme-btn.selected {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border-color: #667eea;
    box-shadow: 0 4px 10px rgba(102, 126, 234, 0.3);
}

.game-start-btn {
    width: 100%;
    padding: 15px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: none;
    border-radius: 30px;
    font-size: 1.2rem;
    font-weight: bold;
    cursor: pointer;
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
    transition: all 0.3s;
    margin-top: 15px;
}

.game-start-btn:hover {
    transform: translateY(-3px);
    box-shadow: 0 8px 25px rgba(102, 126, 234, 0.6);
}

.record-box {
    margin: 20px 0;
    padding: 15px;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 12px;
}

.restart-btn {
    padding: 12px 30px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: none;
    border-radius: 25px;
    font-weight: bold;
    font-size: 1.1rem;
    cursor: pointer;
    transition: all 0.3s;
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.restart-btn:hover {
    transform: translateY(-3px);
    box-shadow: 0 8px 25px rgba(102, 126, 234, 0.6);
}

.bottom-ad {
    margin-top: 30px;
    text-align: center;
}