/* Modal overlay - full screen dark background */
.gallery-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.9);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    /* Critical: Prevent modal content from overflowing viewport */
    padding: 2.5vw; /* Responsive padding (2.5% of viewport width) */
    overflow: hidden; /* No scroll on overlay itself */
}

/* Modal content wrapper - STRICT viewport limits + fit to image */
.modal-content {
    position: relative;
    /* 1. Hard limit: Never exceed 90% of viewport width/height */
    max-width: 90vw;
    max-height: 90vh;
    /* 2. Fit to image size (but not exceed above limits) */
    display: inline-block;
    background-color: #000;
    padding: 1rem;
    border-radius: 8px;
    /* Ensure container doesn't overflow even if image is malformed */
    overflow: hidden;
}

/* Modal image - Double constraints (container + viewport) */
.modal-image {
    /* 1. Fit to modal container (max 90vw/90vh) */
    max-width: 100%;
    max-height: 100%;
    /* 2. Fallback: Never exceed viewport directly */
    max-width: calc(90vw - 2rem); /* Subtract container padding */
    max-height: calc(90vh - 2rem); /* Subtract container padding */
    /* Preserve aspect ratio (non-negotiable) */
    width: auto;
    height: auto;
    /* Center image inside container */
    display: block;
    margin: 0 auto;
    border-radius: 4px;
}

/* Image title - Safe positioning (never off-screen) */
.image-title {
    position: relative;
    margin-top: 1rem;
    color: white;
    font-size: clamp(1rem, 2vw, 1.2rem); /* Responsive font size */
    text-align: center;
    width: 100%;
    /* Prevent title overflow */
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    padding: 0 0.5rem;
}

/* Close button - Fixed + safe positioning (never off-screen) */
.close-btn {
    position: fixed;
    top: 2vw; /* Responsive top position */
    right: 2vw; /* Responsive right position */
    color: white;
    font-size: clamp(2rem, 5vw, 3rem); /* Responsive size */
    font-weight: bold;
    cursor: pointer;
    transition: color 0.3s ease;
    z-index: 1001;
    /* Prevent button from being too close to edge */
    padding: 0.5rem;
}

.close-btn:hover {
    color: #2196F3;
}

/* Navigation buttons (prev/next) - Fixed + safe positioning */
.nav-btn {
    position: fixed;
    top: 50%;
    transform: translateY(-50%);
    color: white;
    font-size: clamp(2rem, 5vw, 3rem); /* Responsive size */
    font-weight: bold;
    cursor: pointer;
    padding: 0 1rem;
    transition: color 0.3s ease, opacity 0.3s ease;
    user-select: none;
    opacity: 1;
    z-index: 1001;
    /* Prevent buttons from overlapping image on small screens */
    background-color: rgba(0, 0, 0, 0.3);
    border-radius: 50%;
    width: clamp(3rem, 8vw, 4rem);
    height: clamp(3rem, 8vw, 4rem);
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Disabled state for navigation buttons */
.nav-btn.disabled {
    opacity: 0.3;
    cursor: not-allowed;
    pointer-events: none;
}

/* Previous button - Safe left position */
.prev-btn {
    left: 2vw;
}

/* Next button - Safe right position */
.next-btn {
    right: 2vw;
}

/* Hover effect for active navigation buttons */
.nav-btn:hover:not(.disabled) {
    color: #2196F3;
    background-color: rgba(0, 0, 0, 0.5);
}

/* Mobile optimization (small screens) */
@media (max-width: 480px) {
    .gallery-modal {
        padding: 1rem;
    }
    .modal-content {
        padding: 0.5rem;
        max-width: 95vw;
        max-height: 95vh;
    }
    .modal-image {
        max-width: calc(95vw - 1rem);
        max-height: calc(95vh - 1rem);
    }
    .nav-btn {
        width: clamp(2.5rem, 10vw, 3.5rem);
        height: clamp(2.5rem, 10vw, 3.5rem);
    }
}