/* CSS Variables for consistent theming and animations */
:root {
    --d: 700ms;
    --e: cubic-bezier(0.19, 1, 0.22, 1);
    --font-sans: 'roboto', sans-serif;
    --font-serif: 'comic sans', serif;
    --primary-light: #6ec3ff;
    --primary-dark: #21bfae;
    --bg-light: linear-gradient(120deg, #4f8cff 0%, #6ec3ff 40%, #a7ffeb 100%),
        radial-gradient(circle at 60% 40%, #6ec3ff33 0%, #f9fafb00 80%);
    --bg-dark: linear-gradient(135deg, #181c2f 0%, #23243a 60%, #21bfae 100%),
        radial-gradient(circle at 70% 30%, #21bfae33 0%, #23243a00 80%);
}

/* Base styles */
body,
html {
    font-family: 'Inter', sans-serif;
    min-height: 100vh;
    background: var(--bg-light);
    transition: background 0.5s;
}

.dark body,
.dark html {
    background: var(--bg-dark);
}

/* Optimized gallery grid with better responsive design */
.gallery-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 1.5rem;
    padding: 2rem;
    max-width: 1400px;
    margin: 0 auto;
}

/* Responsive breakpoints optimized */
@media (min-width: 640px) {
    .gallery-grid {
        grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    }
}

@media (min-width: 768px) {
    .gallery-grid {
        grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    }
}

@media (min-width: 1024px) {
    .gallery-grid {
        grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    }
}

/* Optimized gallery item with better performance */
.gallery-item {
    position: relative;
    display: flex;
    align-items: flex-end;
    overflow: hidden;
    padding: 1rem;
    width: 100%;
    text-align: center;
    color: whitesmoke;
    background-color: #f3f4f6;
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    cursor: pointer;
    aspect-ratio: 3/4;
    will-change: transform;
    transition: transform 0.3s ease, box-shadow 0.3s ease, border 0.3s ease;
    border-radius: 0.75rem;
    border: 3px solid;
    border-image: linear-gradient(135deg,
            #ffd700 0%,
            /* Gold */
            #ffb347 25%,
            /* Light orange */
            #ff6b35 50%,
            /* Orange-red */
            #dc143c 75%,
            /* Crimson */
            #b22222 100%
            /* Fire brick red */
        ) 1;
    /* Fallback for browsers that don't support border-image */
    background: linear-gradient(#f3f4f6, #f3f4f6) padding-box,
        linear-gradient(135deg, #ffd700, #ffb347, #ff6b35, #dc143c, #b22222) border-box;
    animation: shimmer-frame 3s ease-in-out infinite;
}

.dark .gallery-item {
    background-color: #374151;
    background: linear-gradient(#374151, #374151) padding-box,
        linear-gradient(135deg, #ffd700, #ffb347, #ff6b35, #dc143c, #b22222) border-box;
    animation: shimmer-frame 3s ease-in-out infinite;
}

.gallery-item:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 110%;
    background-image: inherit;
    background-size: cover;
    background-position: center;
    transition: transform calc(var(--d) * 1.5) var(--e);
    pointer-events: none;
    border-radius: inherit;
}

.gallery-item:after {
    content: '';
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 200%;
    pointer-events: none;
    background-image: linear-gradient(to bottom,
            hsl(46, 100%, 50%) 0%,
            hsl(34, 100%, 49%) 50%,
            hsl(0, 100%, 51%) 100%,

        );
    transform: translateY(-50%);
    transition: transform calc(var(--d) * 2) var(--e);
    border-radius: inherit;
}

.gallery-overlay {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    padding: 1rem;
    transition: transform var(--d) var(--e);
    z-index: 1;
}

.gallery-overlay>*+* {
    margin-top: 1rem;
}

.gallery-description {
    font-family: var(--font-serif);
    font-size: 1rem;
    font-style: italic;
    line-height: 1.35;
    color: whitesmoke;
    margin: 0;
    text-shadow: 0 2px 8px rgba(0, 0, 0, 0.8);
    display: -webkit-box;
    -webkit-line-clamp: 3;
    line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* Loading states optimized */
.gallery-item.loading {
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: loading 1.5s infinite;
    pointer-events: none;
}

.dark .gallery-item.loading {
    background: linear-gradient(90deg, #2a3441 25%, #1e2936 50%, #2a3441 75%);
    background-size: 200% 100%;
}

@keyframes loading {
    0% {
        background-position: 200% 0;
    }

    100% {
        background-position: -200% 0;
    }
}

@keyframes shimmer-frame {
    0% {
        border-image: linear-gradient(135deg,
                #ffd700 0%, #ffb347 25%, #ff6b35 50%, #dc143c 75%, #b22222 100%) 1;
    }

    50% {
        border-image: linear-gradient(135deg,
                #ffcc00 0%, #ff8c42 25%, #ff4757 50%, #e74c3c 75%, #c0392b 100%) 1;
    }

    100% {
        border-image: linear-gradient(135deg,
                #ffd700 0%, #ffb347 25%, #ff6b35 50%, #dc143c 75%, #b22222 100%) 1;
    }
}

/* Error state */
.gallery-item.error {
    background-color: #fee2e2;
    color: #dc2626;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
}

.dark .gallery-item.error {
    background-color: #7f1d1d;
    color: #fca5a5;
}

.gallery-item.error::before,
.gallery-item.error::after {
    display: none;
}

/* Hover effects for desktop only */
@media (hover: hover) and (min-width: 600px) {
    .gallery-item:hover {
        transform: translateY(-4px);
        box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04),
            0 0 30px rgba(255, 215, 0, 0.4), 0 0 60px rgba(255, 107, 53, 0.2);
        border-width: 4px;
        border-image: linear-gradient(135deg,
                #ffd700 0%,
                /* Brighter gold on hover */
                #ffcc00 25%,
                /* Bright gold */
                #ff8c42 50%,
                /* Bright orange */
                #ff4757 75%,
                /* Bright red */
                #c44569 100%
                /* Deep red */
            ) 1;
        background: linear-gradient(#f3f4f6, #f3f4f6) padding-box,
            linear-gradient(135deg, #ffd700, #ffcc00, #ff8c42, #ff4757, #c44569) border-box;
    }

    .dark .gallery-item:hover {
        background: linear-gradient(#374151, #374151) padding-box,
            linear-gradient(135deg, #ffd700, #ffcc00, #ff8c42, #ff4757, #c44569) border-box;
    }

    .gallery-item:hover:before {
        transform: translateY(-4%);
    }

    .gallery-item:hover:after {
        transform: translateY(-50%);
    }

    .gallery-item:hover .gallery-overlay {
        transform: translateY(0);
    }
}

/* Modal styles optimized */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
    backdrop-filter: blur(4px);
}

.modal.active {
    opacity: 1;
    pointer-events: auto;
}

.modal-content {
    background: #fff;
    border-radius: 1rem;
    max-width: 90vw;
    max-height: 90vh;
    padding: 0;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    display: flex;
    flex-direction: column;
    align-items: center;
    overflow: hidden;
    position: relative;
    transform: scale(0.9);
    transition: transform 0.3s ease;
}

.modal.active .modal-content {
    transform: scale(1);
}

.dark .modal-content {
    background: #23243a;
}

/* Fullscreen modal styles */
.modal.fullscreen {
    background: rgba(0, 0, 0, 0.95);
}

.modal.fullscreen .modal-content {
    max-width: 100vw;
    max-height: 100vh;
    border-radius: 0;
    background: transparent;
}

.modal.fullscreen .modal-image {
    max-width: 100vw;
    max-height: 100vh;
    border-radius: 0;
}

.modal.fullscreen .modal-description {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0, 0, 0, 0.8);
    color: #fff;
    padding: 1rem;
    border-radius: 0;
    backdrop-filter: blur(4px);
}

.modal-image {
    max-width: 60vw;
    max-height: 70vh;
    object-fit: contain;
    border-radius: 1rem 1rem 0 0;
}

.modal-description {
    padding: 1.5rem;
    text-align: center;
    color: #202124;
    font-size: 1.1rem;
    line-height: 1.6;
    max-height: 200px;
    overflow-y: auto;
}

.dark .modal-description {
    color: #e0f7fa;
}

/* Navigation buttons optimized */
.modal-nav,
.lightbox-nav {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background: rgba(0, 0, 0, 0.7);
    color: #fff;
    border: none;
    border-radius: 50%;
    width: 4rem;
    height: 4rem;
    font-size: 1.5rem;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10;
    backdrop-filter: blur(4px);
}

.modal-nav:hover,
.lightbox-nav:hover {
    background: rgba(0, 0, 0, 0.9);
    transform: translateY(-50%) scale(1.1);
}

.modal-nav:disabled,
.lightbox-nav:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: translateY(-50%) scale(0.9);
}

.modal-nav.prev,
.lightbox-nav.prev {
    left: 2rem;
}

.modal-nav.next,
.lightbox-nav.next {
    right: 2rem;
}

/* Close buttons optimized */
.modal-close,
.lightbox-close {
    position: absolute;
    top: 1rem;
    right: 1rem;
    background: rgba(0, 0, 0, 0.7);
    color: #fff;
    border: none;
    border-radius: 50%;
    width: 3rem;
    height: 3rem;
    font-size: 1.5rem;
    cursor: pointer;
    transition: all 0.3s ease;
    z-index: 10;
    backdrop-filter: blur(4px);
}

.modal-close:hover,
.lightbox-close:hover {
    background: rgba(0, 0, 0, 0.9);
    transform: scale(1.1);
}

/* Fullscreen button optimized */
.modal-fullscreen {
    position: absolute;
    top: 1rem;
    right: 4.5rem;
    background: rgba(0, 0, 0, 0.7);
    color: #fff;
    border: none;
    border-radius: 50%;
    width: 3rem;
    height: 3rem;
    cursor: pointer;
    transition: all 0.3s ease;
    z-index: 10;
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-fullscreen:hover {
    background: rgba(0, 0, 0, 0.9);
    transform: scale(1.1);
}

/* Info displays optimized */
.modal-info,
.lightbox-info {
    position: absolute;
    bottom: 2rem;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.8);
    color: #fff;
    padding: 1rem 2rem;
    border-radius: 2rem;
    font-size: 1rem;
    text-align: center;
    max-width: 80%;
    backdrop-filter: blur(4px);
}

.modal-counter,
.lightbox-counter {
    font-weight: 600;
    color: var(--primary-light);
}

.dark .modal-counter,
.dark .lightbox-counter {
    color: var(--primary-dark);
}

/* Lightbox styles optimized */
.lightbox {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.95);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 3000;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
    backdrop-filter: blur(8px);
}

.lightbox.active {
    opacity: 1;
    pointer-events: auto;
}

.lightbox-content {
    position: relative;
    max-width: 95vw;
    max-height: 95vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

.lightbox-image {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    border-radius: 0.5rem;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

/* UI Components optimized */
.back-button {
    background: linear-gradient(90deg, #6366f1 0%, var(--primary-dark) 100%);
    color: #fff;
    border: 1.5px solid #6366f1;
    border-radius: 0.5rem;
    box-shadow: 0 2px 8px #6366f122;
    transition: all 0.3s ease;
    font-weight: 600;
    padding: 0.6rem 1.2rem;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}

.back-button:hover {
    background: linear-gradient(90deg, var(--primary-dark) 0%, #6366f1 100%);
    border: 1.5px solid var(--primary-dark);
    box-shadow: 0 4px 16px var(--primary-dark)33;
    transform: translateY(-1px);
}

.dark .back-button {
    background: linear-gradient(90deg, #e94560 0%, var(--primary-dark) 100%);
    border: 1.5px solid #e94560;
}

.dark .back-button:hover {
    background: linear-gradient(90deg, var(--primary-dark) 0%, #a5b4fc 100%);
    border: 1.5px solid #a5b4fc;
}

.page-title {
    text-align: center;
    font-size: clamp(2rem, 5vw, 2.5rem);
    font-weight: 700;
    color: #202124;
    margin: 2rem 0;
    text-shadow: 0 2px 4px var(--primary-light)22;
}

.dark .page-title {
    color: #e0f7fa;
    text-shadow: 0 2px 4px var(--primary-dark)22;
}

/* Theme toggle optimized */
.theme-toggle {
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 1000;
    background: rgba(223, 245, 255, 0.95);
    border: 2px solid #a7ffeb;
    border-radius: 0.5rem;
    padding: 0.5rem;
    cursor: pointer;
    transition: all 0.3s ease;
    backdrop-filter: blur(4px);
}

.dark .theme-toggle {
    background: #1a2636;
    border: 2px solid #164e63;
}

.theme-toggle:hover {
    transform: scale(1.1);
}

/* Search styles optimized */
.search-highlight {
    background-color: #fef08a;
    padding: 0.1rem 0.2rem;
    border-radius: 0.2rem;
    font-weight: 600;
}

.dark .search-highlight {
    background-color: #fbbf24;
    color: #1f2937;
}

.no-results {
    text-align: center;
    padding: 3rem 1rem;
    color: #6b7280;
    font-size: 1.1rem;
    grid-column: 1 / -1;
}

.dark .no-results {
    color: #9ca3af;
}

.suggestion-chip {
    background: rgba(99, 102, 241, 0.1);
    border: 1px solid rgba(99, 102, 241, 0.3);
    color: #6366f1;
    padding: 0.25rem 0.75rem;
    border-radius: 1rem;
    font-size: 0.75rem;
    cursor: pointer;
    transition: all 0.3s ease;
    font-weight: 500;
}

.suggestion-chip:hover {
    background: rgba(99, 102, 241, 0.2);
    border-color: rgba(99, 102, 241, 0.5);
    transform: translateY(-1px);
}

.dark .suggestion-chip {
    background: rgba(33, 191, 174, 0.1);
    border: 1px solid rgba(33, 191, 174, 0.3);
    color: var(--primary-dark);
}

.dark .suggestion-chip:hover {
    background: rgba(33, 191, 174, 0.2);
    border-color: rgba(33, 191, 174, 0.5);
}

/* Loading indicator optimized */
.loading-indicator {
    text-align: center;
    padding: 2rem;
    color: var(--primary-light);
    font-weight: 600;
    grid-column: 1 / -1;
}

.dark .loading-indicator {
    color: var(--primary-dark);
}

/* Search input optimized */
.search-container {
    max-width: 2xl;
    margin: 0 auto 1.5rem;
    padding: 0 1rem;
}

.search-input {
    width: 100%;
    padding: 0.75rem 1rem 0.75rem 2.5rem;
    border: 1px solid #d1d5db;
    border-radius: 0.5rem;
    background: white;
    color: #1f2937;
    transition: all 0.3s ease;
}

.dark .search-input {
    background: #1f2937;
    border-color: #4b5563;
    color: #f9fafb;
}

.search-input:focus {
    outline: none;
    border-color: var(--primary-light);
    box-shadow: 0 0 0 3px rgba(110, 195, 255, 0.1);
}

.dark .search-input:focus {
    border-color: var(--primary-dark);
    box-shadow: 0 0 0 3px rgba(33, 191, 174, 0.1);
}

/* Responsive optimizations */
@media (max-width: 640px) {
    .gallery-grid {
        padding: 1rem;
        gap: 1rem;
        grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    }

    .modal-nav,
    .lightbox-nav {
        width: 3rem;
        height: 3rem;
    }

    .modal-nav.prev,
    .lightbox-nav.prev {
        left: 1rem;
    }

    .modal-nav.next,
    .lightbox-nav.next {
        right: 1rem;
    }

    .modal-close,
    .lightbox-close {
        width: 2.5rem;
        height: 2.5rem;
        top: 0.5rem;
        right: 0.5rem;
    }

    .modal-fullscreen {
        width: 2.5rem;
        height: 2.5rem;
        top: 0.5rem;
        right: 3.5rem;
    }
}

/* Focus styles for accessibility */
.gallery-item:focus,
.modal-nav:focus,
.lightbox-nav:focus,
.modal-close:focus,
.lightbox-close:focus,
.modal-fullscreen:focus,
.theme-toggle:focus,
.back-button:focus,
.suggestion-chip:focus,
.search-input:focus {
    outline: 2px solid var(--primary-light);
    outline-offset: 2px;
}

.dark .gallery-item:focus,
.dark .modal-nav:focus,
.dark .lightbox-nav:focus,
.dark .modal-close:focus,
.dark .lightbox-close:focus,
.dark .modal-fullscreen:focus,
.dark .theme-toggle:focus,
.dark .back-button:focus,
.dark .suggestion-chip:focus,
.dark .search-input:focus {
    outline-color: var(--primary-dark);
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}