/* Animation keyframes */
@keyframes drop-in {
    from {
        transform: translateY(-100%);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

@keyframes fade-out {
    from {
        transform: translateY(0);
        opacity: 1;
    }
    to {
        transform: translateY(-20%);
        opacity: 0;
    }
}

/* Modal window (including black background) */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: rgba(0, 0, 0, 0.8);
    -webkit-tap-highlight-color: transparent; /* Prevent tap highlight on mobile */
}

.modal.show {
    display: flex;
    justify-content: center;
    align-items: center;
}

.modal-window {
    background-color: var(--background-color);
    width: 90%;
    height: 90%;
    border-radius: 10px;
    position: relative;
    transform: translateY(-100%);
    opacity: 0;
    max-width: 1200px;
    overflow: hidden; /* Hide overflow from window itself */
}

.modal.show .modal-window {
    animation: drop-in 0.3s ease-out forwards;
}

.modal.closing .modal-window {
    animation: fade-out 0.3s ease-in forwards;
}

/* Close button */
.modal-close-btn {
    position: absolute;
    top: 20px;
    right: 20px;
    width: 30px;
    height: 30px;
    border: var(--btn-color) 0.1rem solid;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--btn-color);
    background-color: var(--background-color);
    font-size: 20px;
    font-weight: bold;
    cursor: pointer;
    z-index: 1000;
    transition: background-color 0.2s;
    -webkit-tap-highlight-color: transparent; /* Prevent tap highlight */
    touch-action: manipulation; /* Improves touch response */
}

.modal-close-btn:hover {
    color: var(--background-color);
    background-color: var(--btn-color);
    border: var(--background-color) 0.1rem solid;
}

/* Modal content styles */
.modal-container {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    padding: 5%;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch; /* Enable smooth scrolling on iOS */
    box-sizing: border-box;
    z-index: 1; /* Ensure container is above background but below close button */
}

/* Critical for mobile scrolling - apply to both container and dynamic content */
.modal-container, .modal-dynamic-content {
    touch-action: pan-y; /* Allow vertical scrolling */
    -ms-touch-action: pan-y;
    overscroll-behavior: contain; /* Prevent pull-to-refresh and overscroll effects */
    -ms-scroll-chaining: none;
}

.modal-title {
    text-align: center;
    color: var(--title-color);
    font-weight: 600;
    font-size: 2rem;
    margin: 1rem 0;
}

.modal-subtitle {
    text-align: left;
    color: var(--title-color);
    font-weight: 600;
    font-size: 1.5rem;
    margin-top: 2rem;
    margin-bottom: 0.5rem;
}

.modal-text {
    padding-top: 0.5rem;
    text-align: justify;
    font-size: 1.1rem;
}

.modal-image {
    width: 100%;
    max-width: 800px;
    object-fit: contain;
    display: block;
    margin: auto;
    padding-top: 2rem;
}

.modal-images-container {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: stretch;
    gap: 0.5rem; /* Consistent gap between images */
    width: 100%;
    margin: 0 auto;
}

.modal-images-container > .modal-image {
    flex: 0 1 calc(50% - 0.25rem); /* Precise width calculation accounting for gap */
    object-fit: contain;
    margin: 0;
    box-sizing: border-box; /* Include padding and border in width calculation */
}

.modal-caption {
    text-align: center;
    font-size: 1.25rem;
    margin-top: 1rem;
    margin-bottom: 2rem;
}

.modal-list {
    padding-top: 0.5rem;
    padding-left: 1rem;
    text-align: justify;
    font-size: 1.1rem;
    list-style-type: disc;
    color: var(--paragraph-color);
}

.modal-list li {
    margin-bottom: 0.5rem;
}


/* Add this to fix iOS scrolling issues */
@media (pointer: coarse) {
    .modal-container {
        /* Will use native scrolling on touch devices */
        overflow-y: scroll;
        /* For smoother scrolling */
        -webkit-overflow-scrolling: touch;
    }
}

/* Enhanced touch support for mobile devices */
.modal-dynamic-content {
    touch-action: pan-y;
}