/* Add these optimizations at the top of the file */
* {
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

:root {
    --primary-color: #FFE4B5;    /* Warmer, more playful beige */
    --secondary-color: #FFF3E0;  /* Lighter warm background */
    --accent-color: #FF8C42;     /* Bright orange */
    --text-color: #5D4037;       /* Warm brown */
    --card-back: #FFF3E0;        /* Light warm background */
    --success-color: #4CAF50;    /* Bright green */
    --highlight-color: #FFD700;  /* Gold */
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background-color: var(--secondary-color);
    color: var(--text-color);
    background-image: linear-gradient(120deg, #FFF3E0 0%, #FFE4B5 100%);
    margin: 0;
    padding: 0;
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.container {
    height: 100vh;
    display: flex;
    flex-direction: column;
    padding: 0;
    max-width: 100%;
}

header {
    text-align: center;
    padding: 0.8rem;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 20px;
    box-shadow: 0 4px 15px rgba(255, 140, 66, 0.2);
    margin: 0.5rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
    animation: slideDown 0.5s ease-out;
    will-change: transform;
}

.game-title {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-weight: bold;
    color: var(--accent-color);
    font-size: 2rem;
    margin: 0;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
    animation: bounce 1s ease-in-out infinite;
    will-change: transform;
    letter-spacing: 0.5px;
}

.game-stats {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    justify-content: center;
    font-size: 1rem;
}

.game-stats span {
    background: rgba(255, 255, 255, 0.9);
    padding: 0.5rem 1rem;
    border-radius: 25px;
    box-shadow: 0 4px 15px rgba(255, 140, 66, 0.15);
    transition: transform 0.3s ease;
    will-change: transform;
}

.game-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    padding: 0.5rem;
    padding-bottom: 80px;
    overflow: hidden;
}

.game-board {
    width: 100%;
    max-width: 600px;
    aspect-ratio: 1;
    display: grid;
    gap: 10px;
    padding: 0.5rem;
    margin: 0 auto;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    opacity: 0;
    transform: scale(0.95);
}

/* Level-specific grid layouts optimized for mobile */
.game-board.level-1 {
    grid-template-columns: repeat(2, 1fr);
}

.game-board.level-2 {
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
}

.game-board.level-3,
.game-board.level-4 {
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
}

.game-board.level-5,
.game-board.level-6 {
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
}

.game-board.level-7,
.game-board.level-8,
.game-board.level-9,
.game-board.level-10 {
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
}

.game-board.ready {
    opacity: 1;
    transform: scale(1);
}

.controls {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.8rem;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
    z-index: 1000;
    animation: slideUp 0.5s ease-out;
    width: 100%;
    box-sizing: border-box;
}

.btn-lg {
    padding: 0.8rem 1.5rem;
    font-size: 1rem;
    border-radius: 25px;
    min-width: 140px;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    border: none;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
    will-change: transform;
}

.btn-primary {
    background: linear-gradient(45deg, #FF8C42, #FFB347);
    color: white;
}

.btn-info {
    background: linear-gradient(45deg, #4A90E2, #67B8F7);
    color: white;
}

.btn-success {
    background: linear-gradient(45deg, #4CAF50, #81C784);
    color: white;
}

/* Card styles optimized for performance */
.card {
    aspect-ratio: 1;
    border-radius: 12px;
    background-color: var(--card-back);
    cursor: pointer;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1),
                box-shadow 0.3s ease;
    transform-style: preserve-3d;
    position: relative;
    box-shadow: 0 3px 6px rgba(139, 69, 19, 0.2);
    will-change: transform;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    transform: translateZ(0);
    -webkit-transform: translateZ(0);
    perspective: 1000px;
}

.card img {
    width: 92%;
    height: 92%;
    object-fit: cover;
    border-radius: 10px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    opacity: 0;
    transition: opacity 0.1s ease-in;
    will-change: opacity;
    transform: translateZ(0);
}

.card.flipped img {
    opacity: 1;
}

.card-front,
.card-back {
    position: absolute;
    width: 100%;
    height: 100%;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s ease-out;
    will-change: transform;
    transform: translateZ(0);
    -webkit-transform: translateZ(0);
}

.card.checking {
    box-shadow: 0 0 20px rgba(255, 215, 0, 0.6);
    transform: scale(1.02);
}

.card.matched {
    animation: matchSuccess 0.5s ease-out;
    box-shadow: 0 0 25px rgba(50, 205, 50, 0.6);
}

.card.mismatched {
    animation: mismatchShake 0.5s ease-in-out;
    box-shadow: 0 0 20px rgba(220, 20, 60, 0.6);
}

.card-front {
    background-color: var(--primary-color);
    transform: rotateY(180deg);
}

.card-back {
    background-color: var(--card-back);
    transform: rotateY(0deg);
}

.card.flipped .card-front {
    transform: rotateY(0deg);
}

.card.flipped .card-back {
    transform: rotateY(-180deg);
}

/* Error Handling Styles - replaces previous error styles */
.card.error .card-front {
    background-color: #FFF3E0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 0.5rem;
    padding: 1rem;
    text-align: center;
    color: #FF8C42;
}

.card.error .card-front::before {
    content: '⚠️';
    font-size: 2rem;
}

.card.error .card-front::after {
    content: 'Image unavailable';
    font-size: 1rem;
}


/* Animation keyframes */
@keyframes slideDown {
    from { transform: translateY(-20px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

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

@keyframes bounce {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-5px); }
}

/* Modal styles */
.modal-content {
    background-color: var(--secondary-color);
    border: none;
    border-radius: 20px; /* Updated border-radius */
    box-shadow: 0 8px 30px rgba(255, 140, 66, 0.3);
    overflow: hidden; /* Added overflow: hidden */
}

.modal-header {
    border-bottom: none;
    padding: 1.5rem 2rem;
    display: flex; /* Added display: flex */
    align-items: center; /* Added align-items */
    justify-content: space-between; /* Added justify-content */
}

.modal-title {
    font-size: 1.8rem;
    color: var(--accent-color);
    text-align: center;
    width: 100%;
    margin: 0; /* Added margin: 0 */
}

.modal-header .btn-close {
    display: none; /* Hide the default X button */
}

.btn-close {
    font-size: 1.5rem;
    padding: 0.5rem 1rem;
    background: var(--accent-color);
    color: white;
    border: none;
    border-radius: 20px;
    opacity: 1;
}

.modal-body {
    padding: 1.5rem;
    max-height: 80vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.modal-footer {
    padding: 1rem;
    border-top: 1px solid rgba(0,0,0,0.1);
    display: flex;
    justify-content: center;
}

.modal-close-btn {
    background: var(--accent-color);
    color: white;
    padding: 0.8rem 2rem;
    border: none;
    border-radius: 25px;
    font-size: 1.1rem;
    box-shadow: 0 4px 15px rgba(255, 140, 66, 0.2);
}


/* Leaderboard styles */
.leaderboard-entry {
    padding: 1rem;
    margin: 0.5rem 0;
    background: rgba(255, 255, 255, 0.7);
    border-radius: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 1rem;
    transition: transform 0.2s;
}

.leaderboard-instructions {
    text-align: center;
    color: var(--text-color);
    font-size: 1rem;
    margin-bottom: 1rem;
    padding: 0.5rem;
    background: rgba(255,255,255,0.5);
    border-radius: 10px;
}

.leaderboard-title {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.leaderboard-title i {
    font-size: 1.8rem;
    color: var(--accent-color);
}

/* Media queries for larger screens */
@media (min-width: 768px) {
    .game-title {
        font-size: 2.5rem;
    }

    .game-stats {
        font-size: 1.2rem;
    }

    .btn-lg {
        font-size: 1.1rem;
        min-width: 160px;
    }

    .game-board {
        max-width: 800px;
    }

    .leaderboard-entry {
        font-size: 1.2rem;
        padding: 1.2rem;
    }
}

/* Retain the player name modal styles */
#playerNameModal .modal-content {
    background: linear-gradient(135deg, var(--secondary-color) 0%, #FFF 100%);
    border: none;
    border-radius: 25px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
    overflow: hidden;
}

#playerNameModal .modal-header {
    background: transparent;
    padding: 2rem 1rem;
}

#playerNameModal .modal-title {
    color: var(--accent-color);
    font-size: 2.5rem;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
    animation: bounce 1s ease-in-out infinite;
}

.floating-clouds {
    position: absolute;
    width: 100%;
    top: 0;
    left: 0;
    pointer-events: none;
}

.floating-clouds span {
    position: absolute;
    font-size: 2rem;
    opacity: 0.7;
    animation: float 6s ease-in-out infinite;
}

.floating-clouds span:nth-child(1) {
    left: 10%;
    animation-delay: 0s;
}

.floating-clouds span:nth-child(2) {
    left: 50%;
    animation-delay: -2s;
}

.floating-clouds span:nth-child(3) {
    left: 80%;
    animation-delay: -4s;
}

.name-prompt {
    font-size: 1.8rem;
    color: var(--text-color);
    margin: 2rem 0;
    animation: fadeIn 0.5s ease-out;
}

.name-input-container {
    position: relative;
    max-width: 80%;
    margin: 0 auto;
}

#player-name-input {
    font-size: 1.4rem;
    padding: 1rem 1.5rem;
    border-radius: 15px;
    border: 3px solid var(--accent-color);
    background: rgba(255, 255, 255, 0.9);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
    transition: all 0.3s ease;
}

#player-name-input:focus {
    transform: scale(1.02);
    box-shadow: 0 6px 20px rgba(255, 140, 66, 0.2);
    border-color: var(--highlight-color);
}

.input-feedback {
    position: absolute;
    bottom: -40px;
    left: 0;
    width: 100%;
    text-align: center;
    font-size: 1.2rem;
    opacity: 0;
    transform: translateY(10px);
    transition: all 0.3s ease;
}

.input-feedback.show {
    opacity: 1;
    transform: translateY(0);
}

.feedback-emoji {
    font-size: 1.6rem;
    margin-right: 0.5rem;
}

.feedback-text {
    color: var(--accent-color);
}

#start-with-name {
    background: linear-gradient(45deg, var(--accent-color), #FFB347);
    border: none;
    padding: 1rem 2rem;
    font-size: 1.4rem;
    border-radius: 25px;
    transform: translateY(0);
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

#start-with-name:not(:disabled):hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 25px rgba(255, 140, 66, 0.3);
}

#start-with-name:disabled {
    opacity: 0.7;
    cursor: not-allowed;
}

@keyframes float {
    0%, 100% {
        transform: translateY(0);
    }
    50% {
        transform: translateY(-20px);
    }
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes bounce {
    0%, 100% {
        transform: translateY(0);
    }
    50% {
        transform: translateY(-10px);
    }
}

@keyframes matchSuccess {
    0% { transform: scale(1); }
    50% { transform: scale(1.15); }
    100% { transform: scale(1); }
}

@keyframes mismatchShake {
    0%, 100% { transform: translateX(0); }
    20%, 60% { transform: translateX(-8px); }
    40%, 80% { transform: translateX(8px); }
}

.loading-spinner {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 1000;
    background-color: rgba(255, 255, 255, 0.95);
    padding: 1.2rem;
    border-radius: 12px;
    text-align: center;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    backdrop-filter: blur(5px);
}

.victory-title {
    color: var(--accent-color);
    font-size: 2.5rem;
    text-shadow: 2px 2px 4px rgba(139, 69, 19, 0.1);
    animation: bounce 1s ease-in-out infinite;
}

.victory-message {
    font-size: 1.8rem;
    margin: 2rem 0;
}

.level-complete {
    color: var(--accent-color);
    font-weight: bold;
    margin-bottom: 1rem;
}

.highlight-text {
    color: var(--highlight-color);
    font-size: 2.2rem;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}

.celebration-stars {
    font-size: 3rem;
    margin-bottom: 1rem;
    animation: twinkle 1.5s ease-in-out infinite;
}

.top-players h3 {
    color: var(--accent-color);
    font-size: 2rem;
    margin-bottom: 1rem;
}

.leaderboard-entry:hover {
    transform: scale(1.02);
    box-shadow: 0 6px 20px rgba(255, 140, 66, 0.2);
}

.leaderboard-entry .player-rank {
    font-size: 1.6rem;
    font-weight: bold;
    color: var(--accent-color);
    margin-right: 1rem;
}

.leaderboard-entry .player-name {
    flex-grow: 1;
    text-align: left;
    margin: 0 1rem;
}

.leaderboard-entry .player-score {
    color: var(--success-color);
}

@keyframes twinkle {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.7; transform: scale(1.1); }
}

.no-scores {
    font-size: 1.6rem;
    color: var(--accent-color);
    text-align: center;
    padding: 2rem;
}

@media (orientation: portrait) {
    .game-container {
        flex: 1;
        padding-bottom: 80px;
        height: calc(100vh - 180px);
        overflow: hidden;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .game-board {
        width: 95vmin;
        height: 95vmin;
        max-width: 95vw;
        max-height: calc(100vh - 250px);
        margin: 0 auto;
        display: grid;
        gap: 4px;
    }

    /* Adjust grid layouts for portrait mode */
    .game-board.level-1,
    .game-board.level-2 {
        grid-template-columns: repeat(2, 1fr);
        gap: 6px;
        max-width: 85vw;
    }

    .game-board.level-3,
    .game-board.level-4,
    .game-board.level-5,
    .game-board.level-6 {
        grid-template-columns: repeat(3, 1fr);
        gap: 4px;
        max-width: 85vw;
    }

    .game-board.level-7,
    .game-board.level-8,
    .game-board.level-9,
    .game-board.level-10 {
        grid-template-columns: repeat(4, 1fr);
        gap: 3px;
        max-width: 90vw;
    }

    .card {
        width: 100%;
        aspect-ratio: 1;
        max-width: none;
    }

    .card img {
        width: 85%;
        height: 85%;
    }

    .game-stats {
        font-size: 0.9rem;
        gap: 0.5rem;
    }

    .game-stats span {
        padding: 0.4rem 0.8rem;
    }

    /* Adjust header for smaller screens */
    header {
        padding: 0.5rem;
        margin: 0.3rem;
    }

    .game-title {
        font-size: 1.5rem;
        margin-bottom: 0.3rem;
    }
}

@media (orientation: landscape) {
    .container {
        display: grid;
        grid-template-rows: auto 1fr auto;
        height: 100vh;
        max-height: 100vh;
        overflow: hidden;
        gap: 0.5rem;
        padding: 0.5rem;
    }

    header {
        grid-row: 1;
        text-align: center;
        padding: 0.8rem;
        background: rgba(255, 255, 255, 0.95);
        border-radius: 20px;
        box-shadow: 0 4px 15px rgba(255, 140, 66, 0.2);
        margin-bottom: 0.5rem;
        display: flex;
        justify-content: center;
        align-items: center;
        gap: 2rem;
        animation: slideDown 0.5s ease-out;
    }


    .game-stats {
        display: flex;
        gap: 1.5rem;
        font-size: 1.2rem;
    }

    .game-stats span {
        background: rgba(255, 255, 255, 0.9);
        padding: 0.6rem 1.2rem;
        border-radius: 25px;
        box-shadow: 0 4px 15px rgba(255, 140, 66, 0.15);
        transition: transform 0.3s ease;
    }

    .game-stats span:hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 20px rgba(255, 140, 66, 0.2);
    }

    .game-container {
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
        padding: 0.5rem;
        padding-bottom: 80px;
        height: calc(100vh - 140px);
    }

    .game-board {
        width: 95%;
        height: calc(100vh - 180px);
        gap: 10px;
        margin: 0 auto;
        display: grid;
        align-items: center;
        justify-content: center;
    }

    .controls {
        grid-row: 3;
        display: flex;
        justify-content: center;
        gap: 1.5rem;
        padding: 1rem;
        background: rgba(255, 255, 255, 0.95);
        border-radius: 20px;
        box-shadow: 0 -4px 15px rgba(255, 140, 66, 0.2);
        z-index: 1000;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        backdrop-filter: blur(5px);
        animation: slideUp 0.5s ease-out;
    }

    .btn-lg {
        padding: 0.8rem 1.5rem;
        font-size: 1.1rem;
        border-radius: 25px;
        min-width: 160px;
        transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
        border: none;
        box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
        position: relative;
        overflow: hidden;
    }

    .btn-lg::after {
        content: '';
        position: absolute;
        top: -50%;
        left: -50%;
        width: 200%;
        height: 200%;
        background: rgba(255, 255, 255, 0.2);
        transform: rotate(45deg);
        transition: all 0.3s ease;
        opacity: 0;
    }

    .btn-lg:hover {
        transform: translateY(-4px) scale(1.05);
        box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
    }

    .btn-lg:hover::after {
        opacity: 1;
        transform: rotate(45deg) translate(50%, 50%);
    }

    .btn-info {
        background: linear-gradient(45deg, #4A90E2, #67B8F7);
        color: white;
    }

    #next-level-container {
        position: fixed;
        bottom: 5rem;
        right: 1rem;
        z-index: 1001;
        background: rgba(255, 255, 255, 0.95);
        padding: 1rem 1.5rem;
        border-radius: 20px;
        box-shadow: 0 4px 20px rgba(255, 140, 66, 0.3);
        backdrop-filter: blur(5px);
        animation: bounce 0.5s ease-out;
    }
    .game-board {
        height: 100%;
        max-height: calc(100vh - 180px);
    }
}

/* Mobile-specific optimizations */
@media (hover: none) and (pointer: coarse) {
    .card {
        touch-action: manipulation;
        -webkit-touch-callout: none;
        -webkit-user-select: none;
        user-select: none;
        -webkit-transform-style: preserve-3d;
        transform-style: preserve-3d;
    }

    .card.ready:active {
        transform: scale(0.98);
        transition: transform 0.1s ease-out;
    }
}

/* Auto-progress countdown styling */
.auto-progress-countdown {
    margin-top: 1rem;
    font-size: 1.1rem;
    color: var(--accent-color);
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0% { opacity: 0.6; }
    50% { opacity: 1; }
    100% { opacity: 0.6; }
}

.card.loading {
    animation: cardPulse 1.5s infinite;
}

@keyframes cardPulse {
    0% { opacity: 0.7; }
    50% { opacity: 0.9; }
    100% { opacity: 0.7; }
}

.celebration-container {
    cursor: pointer;
    transition: background-color 0.2s ease;
    padding: 2rem;
    border-radius: 20px;
}

.celebration-container:hover {
    background-color: rgba(255, 255, 255, 0.1);
}

/* Update controls layout for mobile */
.controls {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.8rem;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
    z-index: 1000;
    animation: slideUp 0.5s ease-out;
    width: 100%;
    box-sizing: border-box;
}

/* Adjust button sizes for mobile */
@media (max-width: 768px) {
    .controls {
        padding: 0.5rem;
        gap: 0.4rem;
    }

    .btn-lg {
        padding: 0.6rem 1rem;
        font-size: 0.9rem;
        min-width: auto;
        white-space: nowrap;
    }

    .btn-lg i {
        margin-right: 0.3rem;
    }

    /* Ensure game container doesn't overlap with controls */
    .game-container {
        padding-bottom: 90px;
    }

    /* Adjust header spacing */
    header {
        margin-bottom: 0.3rem;
    }
}