/* Memory Match Game Styles */

/* Disable body scroll when popup is open */
html.popup-open {
    overflow: hidden;
    height: 100%;
}

/* Memory Game Section */
.memory-game-section {
    width: 100%;
    margin: 0 auto;
}

.memory-game-card {
    padding: 20px 15px;
    margin-bottom: 30px;
}


@media (max-width: 480px) {
    .memory-game-card {
        padding: 8px 5px;
    }
    
    .memory-grid {
        padding: 5px;
        gap: 4px;
    }
    
    .memory-grid.grid-4x4 {
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 5px;
        padding: 6px;
    }
    
    .memory-grid.grid-6x6 {
        grid-template-columns: repeat(6, minmax(0, 1fr));
        gap: 3px;
        padding: 5px;
    }
    
    .memory-grid.grid-8x8 {
        grid-template-columns: repeat(8, minmax(0, 1fr));
        gap: 2px;
        padding: 4px;
    }
}

.memory-game-title {
    font-size: 2.5rem;
    font-weight: 800;
    text-align: center;
    margin-bottom: 10px;
    color: var(--text-primary);
}

.memory-game-subtitle {
    text-align: center;
    font-size: 1.1rem;
    color: var(--text-secondary);
    margin-bottom: 15px;
}

/* Game Stats Bar */
.memory-stats-bar {
    display: flex;
    justify-content: center;
    gap: 30px;
    margin-bottom: 10px;
    padding: 15px;
    background: var(--chip-bg);
    border: 1px solid var(--chip-border);
    border-radius: 12px;
    flex-wrap: wrap;
}

.memory-stat {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 1rem;
}

.memory-stat-label {
    font-weight: 600;
    color: var(--text-secondary);
}

.memory-stat-value {
    font-weight: 700;
    color: var(--accent-1);
    font-size: 1.1rem;
    font-family: inherit;
}

.is-mobile .memory-stats-bar {
    gap: 15px;
    padding: 12px;
}

.is-mobile .memory-stat {
    font-size: 0.9rem;
}

.is-mobile .memory-stat-value {
    font-size: 1rem;
}

/* Memory Grid Container */
.memory-grid-container {
    width: 100%;
    display: flex;
    justify-content: center;
    margin-bottom: 15px;
    box-sizing: border-box;
    padding: 0;
}

.memory-grid {
    display: grid;
    gap: 10px;
    padding: 10px;
    background: var(--chip-bg);
    border: 2px solid var(--chip-border);
    border-radius: 12px;
    box-sizing: border-box;
    width: fit-content;
    perspective: 1000px;
    -webkit-perspective: 1000px;
    margin: 0 auto;
}

/* Grid sizes based on difficulty - dynamic width based on card max-width */
.memory-grid.grid-4x4 {
    grid-template-columns: repeat(4, 100px);
    width: fit-content;
    gap: 8px;
}

.memory-grid.grid-6x6 {
    grid-template-columns: repeat(6, 100px);
    width: fit-content;
    gap: 8px;
}

.memory-grid.grid-8x8 {
    grid-template-columns: repeat(8, 100px);
    width: fit-content;
    gap: 6px;
}

/* Memory Card */
.memory-card {
    aspect-ratio: 1;
    width: 100%;
    max-width: 100px;
    /* Remove background - faces will have their own backgrounds */
    border: none;
    border-radius: 8px;
    cursor: pointer;
    position: relative;
    transform-style: preserve-3d;
    -webkit-transform-style: preserve-3d;
    transform-origin: center center;
    transition: transform 0.3s ease-in-out;
    overflow: visible;
    will-change: transform;
}

.memory-card:hover:not(.flipped):not(.matched):not(.disabled) {
    transform: translateY(-2px) rotateY(0deg);
}

.memory-card:hover:not(.flipped):not(.matched):not(.disabled) .memory-card-back {
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.15);
}

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

.memory-card.disabled {
    cursor: not-allowed;
    opacity: 0.6;
}

.memory-card.matched {
    /* Matched cards stay flipped and visible */
    cursor: default;
    pointer-events: none;
}

.memory-card.matched .memory-card-front {
    border-color: var(--accent-3);
    box-shadow: 0 0 20px color-mix(in srgb, var(--accent-3) 40%, transparent);
    /* Front face should be visible when card is rotated 180deg */
    opacity: 1 !important;
    visibility: visible !important;
    display: flex !important;
}

.memory-card.matched .memory-card-back {
    /* Back face should be hidden when card is rotated 180deg */
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

/* Card Faces */
.memory-card-face {
    position: absolute;
    width: 100%;
    height: 100%;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    opacity: 1;
    visibility: visible;
}

.memory-card-back {
    background: linear-gradient(135deg, var(--accent-1) 0%, var(--accent-2) 100%);
    background-size: cover;
    background-position: center;
    transform: rotateY(0deg);
    -webkit-transform: rotateY(0deg);
    pointer-events: none;
    border: 2px solid var(--card-border-color);
    box-shadow: var(--card-shadow);
    z-index: 1;
    opacity: 1;
    visibility: visible;
    display: flex;
}

.memory-card-back::before {
    content: '?';
    font-size: 2.5rem;
    font-weight: 800;
    color: rgba(255, 255, 255, 0.9);
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.memory-card-front {
    background: var(--card-bg);
    transform: rotateY(180deg);
    -webkit-transform: rotateY(180deg);
    font-size: 2.5rem;
    user-select: none;
    color: var(--text-primary);
    font-weight: normal;
    line-height: 1;
    white-space: nowrap;
    box-sizing: border-box;
    pointer-events: none;
    text-align: center;
    /* Ensure content is visible */
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    /* Front face should be hidden when card is not flipped */
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    /* Force hardware acceleration */
    will-change: transform;
    border: 2px solid var(--card-border-color);
    box-shadow: var(--card-shadow);
    /* Ensure text/emoji is visible */
    opacity: 0;
    visibility: hidden;
    z-index: 2;
    display: flex;
    transition: opacity 0.3s ease-in-out, visibility 0.3s ease-in-out;
    -webkit-transition: opacity 0.3s ease-in-out, visibility 0.3s ease-in-out;
}

.memory-card-back {
    transition: opacity 0.3s ease-in-out, visibility 0.3s ease-in-out;
    -webkit-transition: opacity 0.3s ease-in-out, visibility 0.3s ease-in-out;
}

/* Desktop: Show front face when flipped using 3D transform with smooth transitions */
@media (min-width: 580px) {
    .memory-card.flipped .memory-card-front,
    .memory-card.matched .memory-card-front {
        opacity: 1;
        visibility: visible;
    }
    
    .memory-card.flipped .memory-card-back,
    .memory-card.matched .memory-card-back {
        opacity: 0;
        visibility: hidden;
    }
}

.memory-grid.grid-4x4 .memory-card-front {
    font-size: 3rem;
}

.memory-grid.grid-6x6 .memory-card-front {
    font-size: 2rem;
}

.memory-grid.grid-8x8 .memory-card-front {
    font-size: 1.5rem;
}

/* Card sizes - controlled by grid columns */
.memory-card {
    min-width: 0;
    min-height: 0;
}

/* Shake animation for mismatch */
@keyframes shake {
    0%, 100% { transform: translateX(0) rotateY(180deg); }
    10%, 30%, 50%, 70%, 90% { transform: translateX(-5px) rotateY(180deg); }
    20%, 40%, 60%, 80% { transform: translateX(5px) rotateY(180deg); }
}

.memory-card.mismatch {
    animation: shake 0.5s ease-in-out;
}

/* Start Button */
.memory-controls-bottom {
    display: flex;
    justify-content: center;
    margin-top: 0;
}

.memory-start-btn {
    display: block;
    width: 100%;
    max-width: 280px;
    padding: 14px;
    font-size: 15px;
    font-weight: 800;
    font-family: inherit;
    border-radius: 16px;
    border: 2px solid var(--modal-btn-border);
    background: linear-gradient(135deg, var(--accent-3), var(--accent-1));
    color: #fff;
    cursor: pointer;
    transition: all 0.2s ease;
    text-align: center;
    text-transform: uppercase;
    letter-spacing: 1px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.memory-start-btn:hover:not(:disabled) {
    transform: scale(1.05) rotate(1deg);
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25);
}

.memory-start-btn:active:not(:disabled) {
    transform: scale(0.98);
}

.memory-start-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
}

/* Pause/Resume button styling - unselected state like difficulty popup */
.memory-start-btn.is-paused {
    background: var(--chip-bg);
    color: var(--text-primary);
    border-color: var(--chip-border);
    animation: none;
}


/* Memory Stats Popup */
.memory-stats-popup .stats-popup {
    max-width: 500px;
}

/* Ensure stats popup close button uses gradient style */
.memory-stats-popup .stats-popup-close {
    background: linear-gradient(135deg, var(--accent-3), var(--accent-1));
    color: #fff;
    border: 2px solid var(--modal-btn-border);
    border-radius: 16px;
    padding: 14px;
    font-size: 15px;
    font-weight: 800;
    font-family: inherit;
    text-transform: uppercase;
    letter-spacing: 1px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.memory-stats-popup .stats-popup-close:hover {
    transform: scale(1.05) rotate(1deg);
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25);
}

.memory-stats-popup .stats-popup-close:active {
    transform: scale(0.98);
}

/* Memory Modal (Win/Lose) */
.memory-modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
    animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.memory-modal {
    background: var(--card-bg);
    border: 2px solid var(--card-border-color);
    border-radius: 16px;
    padding: 40px;
    max-width: 500px;
    width: 90%;
    text-align: center;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    animation: slideUp 0.3s ease;
}

@keyframes slideUp {
    from {
        transform: translateY(30px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.memory-modal-emoji {
    font-size: 4rem;
    margin-bottom: 20px;
}

.memory-modal-title {
    font-size: 2rem;
    font-weight: 800;
    margin-bottom: 15px;
    color: var(--text-primary);
}

.memory-modal-message {
    font-size: 1.1rem;
    color: var(--text-secondary);
    margin-bottom: 20px;
}

.memory-modal-stats {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 25px;
    padding: 15px;
    background: var(--chip-bg);
    border-radius: 8px;
}

.memory-modal-stat {
    display: flex;
    justify-content: space-between;
    font-size: 1rem;
}

.memory-modal-stat-label {
    font-weight: 600;
    color: var(--text-secondary);
}

.memory-modal-stat-value {
    font-weight: 700;
    color: var(--accent-1);
}

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

.memory-modal-btn {
    padding: 12px 24px;
    font-size: 1rem;
    font-weight: 700;
    font-family: inherit;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    text-decoration: none;
    display: inline-block;
}

.memory-modal-btn.restart {
    background: linear-gradient(135deg, var(--accent-3), var(--accent-1));
    color: #fff;
    border: 2px solid var(--modal-btn-border);
    border-radius: 16px;
    padding: 14px;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 1px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.memory-modal-btn.restart:hover {
    transform: scale(1.05) rotate(1deg);
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25);
}

.memory-modal-btn.close {
    background: linear-gradient(135deg, var(--accent-2), var(--accent-1));
    color: #fff;
    border: 2px solid var(--modal-btn-border);
    border-radius: 16px;
    padding: 14px;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 1px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.memory-modal-btn.close:hover {
    transform: scale(1.05) rotate(1deg);
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25);
}

.memory-modal-btn.support {
    margin-top: 1rem;
    background: linear-gradient(135deg, #ffd93d, #ff6b9d);
    color: #ffffff;
    border: 2px solid var(--modal-btn-border);
    border-radius: 16px;
    padding: 10px 20px;
    font-size: 14px;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 1px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    animation: supportPulse 2s ease-in-out infinite;
    margin-top: 1rem;
}

@keyframes supportPulse {
    0%, 100% {
        box-shadow: 0 6px 15px rgba(0, 0, 0, 0.2);
    }
    50% {
        box-shadow: 0 6px 20px rgba(255, 217, 61, 0.5);
    }
}

.memory-modal-btn.support:hover {
    animation: none;
    transform: scale(1.05) rotate(1deg);
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25);
}

/* Tablet adjustments */
@media (min-width: 769px) and (max-width: 1359px) {
    .memory-grid.grid-4x4 {
        grid-template-columns: repeat(4, 100px);
        width: fit-content;
    }
    
    .memory-grid.grid-6x6 {
        grid-template-columns: repeat(6, 100px);
        width: fit-content;
    }
    
    .memory-grid.grid-8x8 {
        grid-template-columns: repeat(8, 100px);
        width: fit-content;
    }
}

/* Small screens - allow wrapping when cards don't fit */
@media (max-width: 579px) {
    .memory-grid-container {
        width: 100%;
        box-sizing: border-box;
        justify-content: flex-start;
    }
    
    .memory-grid {
        width: 100% !important;
        max-width: 100% !important;
        display: grid !important;
        grid-auto-flow: row;
        grid-auto-rows: minmax(60px, auto);
        box-sizing: border-box;
        /* Override fit-content from base styles */
        min-width: 0;
        perspective: 1000px;
        -webkit-perspective: 1000px;
    }
    
    /* Small screens (579px and below): Use JavaScript-controlled visibility */
    /* Disable 3D transforms - JavaScript handles visibility via inline styles */
    .memory-card {
        min-height: 60px;
        /* Remove 3D transform - JS will handle visibility */
        transform: none !important;
        -webkit-transform: none !important;
    }
    
    .memory-card.flipped,
    .memory-card.matched {
        /* No 3D transform - JS handles visibility */
        transform: none !important;
        -webkit-transform: none !important;
    }
    
    .memory-card-front {
        /* No 3D transform - JS handles visibility */
        transform: none !important;
        -webkit-transform: none !important;
        backface-visibility: visible !important;
        -webkit-backface-visibility: visible !important;
        font-size: 2rem !important;
    }
    
    .memory-card-back {
        /* No 3D transform - JS handles visibility */
        transform: none !important;
        -webkit-transform: none !important;
        backface-visibility: visible !important;
        -webkit-backface-visibility: visible !important;
    }
    
    .memory-card {
        transition: transform 0.3s ease-in-out;
        -webkit-transition: transform 0.3s ease-in-out;
    }
    
    .memory-card .memory-card-back,
    .memory-card .memory-card-front {
        display: flex !important;
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        transition: opacity 0.3s ease-in-out, visibility 0.3s ease-in-out, transform 0.3s ease-in-out;
        -webkit-transition: opacity 0.3s ease-in-out, visibility 0.3s ease-in-out, transform 0.3s ease-in-out;
        backface-visibility: hidden;
        -webkit-backface-visibility: hidden;
    }
    
    /* Ensure back face is visible by default */
    .memory-card:not(.flipped):not(.matched) .memory-card-back {
        opacity: 1 !important;
        visibility: visible !important;
        z-index: 2 !important;
        transform: rotateY(0deg) scale(1) !important;
        -webkit-transform: rotateY(0deg) scale(1) !important;
    }
    
    .memory-card:not(.flipped):not(.matched) .memory-card-front {
        opacity: 0 !important;
        visibility: hidden !important;
        z-index: 1 !important;
        transform: rotateY(180deg) scale(0.9) !important;
        -webkit-transform: rotateY(180deg) scale(0.9) !important;
    }
    
    /* Card flip animation - rotate the card container */
    .memory-card.flipped,
    .memory-card.matched {
        transform: rotateY(180deg) !important;
        -webkit-transform: rotateY(180deg) !important;
    }
    
    /* Flipped state - show front face */
    .memory-card.flipped .memory-card-back,
    .memory-card.matched .memory-card-back {
        opacity: 0 !important;
        visibility: hidden !important;
        z-index: 1 !important;
        transform: rotateY(180deg) scale(0.9) !important;
        -webkit-transform: rotateY(180deg) scale(0.9) !important;
    }
    
    .memory-card.flipped .memory-card-front,
    .memory-card.matched .memory-card-front {
        opacity: 1 !important;
        visibility: visible !important;
        z-index: 10 !important;
        transform: rotateY(0deg) scale(1) !important;
        -webkit-transform: rotateY(0deg) scale(1) !important;
    }
    
    /* Use auto-fill with fixed width to force wrapping */
    /* When container is too narrow, cards wrap to new line */
    .memory-grid.grid-4x4 {
        grid-template-columns: repeat(auto-fill, minmax(60px, 60px)) !important;
        width: 100% !important;
        max-width: 100% !important;
        gap: 5px;
        padding: 5px;
    }
    
    .memory-grid.grid-6x6 {
        grid-template-columns: repeat(auto-fill, minmax(60px, 60px)) !important;
        width: 100% !important;
        max-width: 100% !important;
        gap: 4px;
        padding: 5px;
    }
    
    .memory-grid.grid-8x8 {
        grid-template-columns: repeat(auto-fill, minmax(60px, 60px)) !important;
        width: 100% !important;
        max-width: 100% !important;
        gap: 3px;
        padding: 4px;
    }
    
    .memory-card {
        min-width: 60px;
        max-width: 60px;
        min-height: 60px;
        width: 100%;
        height: auto;
        aspect-ratio: 1;
        box-sizing: border-box;
        overflow: hidden;
    }
}

/* Responsive Design */
@media (max-width: 768px) {
    .memory-game-card {
        padding: 12px 8px;
        margin-bottom: 20px;
    }
    
    .memory-game-title {
        font-size: 1.5rem;
        margin-bottom: 8px;
    }
    
    .memory-game-subtitle {
        font-size: 0.9rem;
        margin-bottom: 10px;
    }
    
    .memory-grid {
        width: fit-content;
        max-width: 100%;
    }
    
    /* For screens between 580px and 768px, use dynamic width */
    @media (min-width: 580px) {
        .memory-grid.grid-4x4 {
            grid-template-columns: repeat(4, 100px);
            width: fit-content;
        }
        
        .memory-grid.grid-6x6 {
            grid-template-columns: repeat(6, 100px);
            width: fit-content;
        }
        
        .memory-grid.grid-8x8 {
            grid-template-columns: repeat(8, 100px);
            width: fit-content;
        }
    }
    
    .memory-stats-bar {
        gap: 8px;
        padding: 10px;
        margin-bottom: 20px;
        flex-wrap: wrap;
    }
    
    .memory-stat {
        font-size: 0.8rem;
        gap: 4px;
    }
    
    .memory-stat-value {
        font-size: 0.9rem;
    }
    
    .memory-controls-bottom {
        margin-top: 0;
    }
    
    .memory-start-btn {
        padding: 10px 24px;
        font-size: 1rem;
    }
    
    .memory-modal {
        padding: 25px 15px;
        width: 95%;
    }
    
    .memory-modal-title {
        font-size: 1.3rem;
    }
    
    .memory-modal-emoji {
        font-size: 2.5rem;
    }
}

/* Desktop adjustments for ad layout */
@media (min-width: 1360px) {
    .memory-grid-container {
        max-width: calc(100% - 200px);
        margin-left: auto;
        margin-right: auto;
        padding: 0;
    }
    
    .memory-grid-container .memory-grid.grid-4x4 {
        max-width: calc(4 * 100px + 3 * 8px + 2 * 10px);
    }
    
    .memory-grid-container .memory-grid.grid-6x6 {
        max-width: calc(6 * 100px + 5 * 8px + 2 * 10px);
    }
    
    .memory-grid-container .memory-grid.grid-8x8 {
        max-width: calc(8 * 100px + 7 * 6px + 2 * 10px);
    }
}

/* Ensure grid is visible on all themes */
[data-theme="pastel"] .memory-grid {
    background: rgba(255, 255, 255, 0.55);
    border-color: rgba(11, 16, 42, 0.16);
}

[data-theme="neon"] .memory-grid {
    background: rgba(0, 0, 0, 0.4);
    border-color: rgba(34, 211, 238, 0.3);
}

[data-theme="dark"] .memory-grid {
    background: rgba(0, 0, 0, 0.5);
    border-color: rgba(245, 158, 11, 0.3);
}

[data-theme="clean"] .memory-grid {
    background: rgba(0, 0, 0, 0.45);
    border-color: rgba(34, 197, 94, 0.3);
}
