/* Modals */

/* Modal Animation */
@keyframes modalFadeSlide {
    0% {
        opacity: 0;
        transform: translateY(20px);
    }
    100% {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes modalFadeSlideReverse {
    0% {
        opacity: 1;
        transform: translateY(0);
    }
    100% {
        opacity: 0;
        transform: translateY(20px);
    }
}

.modal {
    display: none; /* Hidden by default */
    position: fixed;
    top: calc(var(--header-height, 80px) + 1px); /* Small gap to prevent border overlap */
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.9); /* Opaque to cover homepage */
    z-index: 90000;
    outline: none; /* Remove focus border */
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    flex-direction: column;
    min-height: calc(100vh - var(--header-height, 80px) - 1px); /* Fill viewport */
    overflow: hidden; /* Prevent overflow when hidden */
    box-sizing: border-box; /* Include borders/padding in width */
    will-change: display; /* Optimize rendering */
}

.modal:focus,
.modal-scrollable:focus,
.modal * {
    outline: none !important;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
}

.modal.show {
    display: flex; /* Show when active */
    animation: modalFadeSlide 0.3s ease-out forwards;
    overflow: auto; /* Allow scrolling when open */
}

.modal.closing {
    animation: modalFadeSlideReverse 0.3s ease-out forwards;
}

.modal-scrollable {
    position: relative;
    flex-grow: 1;
    padding: 1rem;
    overflow-y: auto; /* Scrollable content */
    overflow-x: hidden; /* Prevent horizontal overflow */
    background: var(--card-background);
    border: 2px solid var(--accent-primary);
    border-top: none; /* Remove top border to prevent overlap with nav */
    border-bottom: none; /* Remove bottom border to align with footer */
    border-radius: 0; /* Remove rounded corners */
    box-shadow: 0 5px 15px var(--element-shadow);
    margin: 0 auto;
    width: 100%;
    max-width: 1200px; /* Match --content-max-width */
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    z-index: 90001; /* Below modal-close */
}

.modal-footer {
    background: rgb(42, 50, 42); /* Opaque, no transparency */
    padding: 0.5rem;
    border-top: 1px solid var(--accent-primary);
    z-index: 2000; /* Same level as modal-scrollable */
    /* Part of the flex flow, not fixed */
}

.modal-button-group {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 0.5rem; /* Consistent gap */
    width: 100%;
    max-width: 500px;
    margin: 0 auto;
    box-sizing: border-box;
}

.modal-close {
    background: var(--accent-primary);
    color: var(--background-primary);
    border: none;
    font-size: 2rem;
    line-height: 1; /* Reset line-height */
    width: 2.5rem;
    height: 2.5rem;
    border-radius: 50%;
    position: sticky;
    top: 0.5rem;
    right: 0.5rem;
    margin-left: auto;
    cursor: pointer;
    transition: background-color 0.3s ease, transform 0.2s ease;
    z-index: 90002; /* Above modal content */
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-close:hover {
    background: var(--accent-hover);
    transform: scale(1.1);
}

.modal-close-bottom {
    background: transparent;
    color: var(--accent-primary);
    border: 1px solid var(--accent-primary);
    font: 700 1rem 'Montserrat', Arial, sans-serif;
    cursor: pointer;
    border-radius: 8px;
    width: 100%;
    max-width: 100%;
    transition: background-color 0.3s ease, box-shadow 0.3s ease, transform 0.2s ease;
    box-shadow: 4px 4px 12px var(--button-shadow);
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-close-bottom:hover {
    background: rgba(154, 184, 122, 0.1);
    box-shadow: 3px 3px 8px var(--button-shadow);
    transform: translateY(-1px);
}

.modal-scrollable h3 {
    font: 700 1.8rem 'Montserrat', Arial, sans-serif;
    margin-bottom: 0.5rem;
    text-shadow: 2px 2px 4px var(--text-shadow);
    text-align: center;
}

.modal-scrollable h4 {
    font: 700 1.4rem 'Montserrat', Arial, sans-serif;
    color: #BCD89A;
    margin-bottom: 1rem;
    text-align: center;
}

.modal-scrollable p {
    font: 1rem 'Open Sans', Arial, sans-serif;
    margin-bottom: 1rem;
    line-height: 1.6;
}

.modal-gallery {
    display: flex;
    flex-direction: row; /* Default to row */
    justify-content: center; /* Center images */
    align-items: center;
    overflow-x: hidden; /* Prevent horizontal scrolling */
    overflow-y: hidden; /* Prevent vertical scrolling */
    gap: 1rem;
    margin-bottom: 1.5rem;
    padding-bottom: 0.5rem;
}

.modal-gallery img {
    width: 200px;
    height: 150px; /* 200px * (3/4) for 4:3 aspect ratio */
    object-fit: cover;
    border-radius: 8px;
    flex-shrink: 0;
}

.modal-features {
    list-style: disc;
    padding-left: 1.5rem;
    margin-bottom: 1.5rem;
}

.modal-features li {
    font: 1rem 'Open Sans', Arial, sans-serif;
    margin-bottom: 0.5rem;
}

.modal .button-outline,
.modal .button-primary {
    margin: 0;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Modal-Specific Media Queries for Testing */

/* Media Query 1: Portrait Phone (≤ 767px) */
@media (orientation: portrait) and (max-width: 767px) {
    .modal {
        top: calc(var(--header-height, 80px) + 1px);
        overflow: hidden; /* Prevent overflow */
    }
    .modal-button-group {
        flex-direction: column;
        gap: 0.5rem;
        width: 100%;
    }
    .modal-scrollable h3 {
        font-size: 1.6rem;
        margin-bottom: 0.3rem;
    }
    .modal-scrollable h4 {
        font-size: 1.2rem;
        margin-bottom: 0.5rem;
    }
    .modal-scrollable p {
        font-size: 0.9rem;
        margin-bottom: 0.5rem;
        line-height: 1.4;
    }
    .modal-gallery {
        margin-bottom: 1rem;
        padding-bottom: 0.3rem;
        flex-direction: column; /* Stack images in column */
    }
    .modal-gallery img {
        width: 150px;
        height: 112px; /* 150px * (3/4) for 4:3 aspect ratio */
    }
    .modal-features {
        margin-bottom: 1rem;
        padding-left: 1.2rem;
    }
    .modal-features li {
        font-size: 0.9rem;
        margin-bottom: 0.3rem;
    }
    .modal .button-outline,
    .modal .button-primary {
        font-size: 1rem;
        height: 2rem; /* Match modal-close-bottom */
        min-height: 2rem; /* Override global min-height */
        padding: 0.5rem 1rem; /* Standardize padding */
        max-width: 100%;
        min-width: 0; /* Allow shrinking */
        line-height: 1; /* Reset line-height */
    }
    .modal-close-bottom {
        font-size: 1rem; /* Match other buttons */
        height: 2rem;
        padding: 0.5rem 1rem; /* Standardize padding */
        line-height: 1; /* Reset line-height */
    }
    .modal-close {
        top: 0.5rem;
        right: 0.5rem;
    }
}

/* Media Query 2: Small Landscape (≤ 767px) */
@media (orientation: landscape) and (max-width: 767px) {
    .modal {
        top: calc(var(--header-height, 80px) + 1px);
        overflow: hidden; /* Prevent overflow */
    }
    .modal-button-group {
        flex-direction: row;
        justify-content: space-between;
        gap: 0.2rem;
        width: 100%;
    }
    .modal-scrollable h3 {
        font-size: 1.2rem;
        margin-bottom: 0.2rem;
    }
    .modal-scrollable h4 {
        font-size: 1rem;
        margin-bottom: 0.3rem;
    }
    .modal-scrollable p {
        font-size: 0.8rem;
        margin-bottom: 0.3rem;
        line-height: 1.3;
    }
    .modal-gallery {
        margin-bottom: 0.5rem;
        padding-bottom: 0.2rem;
        flex-direction: row; /* Keep row if width allows */
    }
    /* If width is less than 380px, stack images */
    @media (max-width: 380px) {
        .modal-gallery {
            flex-direction: column; /* Stack images */
        }
    }
    .modal-gallery img {
        width: 120px;
        height: 90px; /* 120px * (3/4) for 4:3 aspect ratio */
    }
    .modal-features {
        margin-bottom: 0.5rem;
        padding-left: 1rem;
    }
    .modal-features li {
        font-size: 0.8rem;
        margin-bottom: 0.2rem;
    }
    .modal .button-outline,
    .modal .button-primary {
        flex: 1 1 50%;
        max-width: calc(50% - 0.1rem);
        padding: 0.15rem 0.3rem;
        font-size: 0.8rem;
        height: 2rem;
        min-height: 2rem;
        border-radius: 4px;
    }
    .modal-close-bottom {
        flex: 1 1 50%;
        max-width: calc(50% - 0.1rem);
        padding: 0.15rem 0.3rem;
        font-size: 0.8rem;
        height: 2rem;
        min-height: 2rem;
        border-radius: 4px;
    }
    .modal-close {
        top: 0.5rem;
        right: 0.5rem;
    }
}

/* Media Query 3: Tablets and Medium Screens (768px - 1023px) */
@media (min-width: 768px) and (max-width: 1023px) {
    .modal {
        top: calc(var(--header-height, 80px) + 1px);
        overflow: hidden; /* Prevent overflow */
    }
    .modal-button-group {
        flex-direction: column;
        gap: 0.5rem;
        width: 100%;
    }
    .modal-scrollable h3 {
        font-size: 1.8rem;
    }
    .modal-scrollable h4 {
        font-size: 1.32rem;
    }
    .modal-scrollable p {
        font-size: 1.1rem;
    }
    .modal-gallery {
        flex-direction: row; /* Keep row */
    }
    /* If width is less than 560px, stack images */
    @media (max-width: 560px) {
        .modal-gallery {
            flex-direction: column; /* Stack images */
        }
    }
    .modal-gallery img {
        width: 180px;
        height: 135px; /* 180px * (3/4) for 4:3 aspect ratio */
    }
    .modal-features li {
        font-size: 0.95rem;
    }
    .modal .button-outline,
    .modal .button-primary {
        padding: 0.5rem;
        font-size: 0.8rem;
        height: 34px;
        min-height: 34px;
        white-space: normal;
    }
    .modal-close-bottom {
        font-size: 0.8rem;
        padding: 0.5rem;
        height: 34px;
        min-height: 34px;
        white-space: normal;
    }
    .modal-close {
        top: 0.5rem;
        right: 0.5rem;
    }
}

/* Media Query 4: Tablet Landscape (768px - 1023px) */
@media (orientation: landscape) and (min-width: 768px) and (max-width: 1023px) {
    .modal {
        top: calc(var(--header-height, 80px) + 1px);
        overflow: hidden; /* Prevent overflow */
    }
    .modal-button-group {
        flex-direction: row;
        gap: 0.5rem;
        width: 100%;
    }
    .modal-scrollable h3 {
        font-size: 1.6rem;
    }
    .modal-scrollable h4 {
        font-size: 1.3rem;
    }
    .modal-scrollable p {
        font-size: 1rem;
    }
    .modal-gallery {
        flex-direction: row; /* Keep row */
    }
    /* If width is less than 560px, stack images */
    @media (max-width: 560px) {
        .modal-gallery {
            flex-direction: column; /* Stack images */
        }
    }
    .modal-gallery img {
        width: 180px;
        height: 135px;
    }
    .modal-features li {
        font-size: 0.95rem;
    }
    .modal .button-outline,
    .modal .button-primary {
        padding: 0.5rem;
        font-size: 0.8rem;
        height: 34px;
        min-height: 34px;
        white-space: normal;
    }
    .modal-close-bottom {
        font-size: 0.8rem;
        padding: 0.5rem;
        height: 34px;
        min-height: 34px;
        white-space: normal;
    }
    .modal-close {
        top: 0.5rem;
        right: 0.5rem;
    }
}

/* Media Query 5: Larger Screens (≥ 1024px) */
@media (min-width: 1024px) {
    .modal {
        top: calc(var(--header-height, 80px) + 1px);
        overflow: hidden; /* Prevent overflow */
    }
    .modal-button-group {
        flex-direction: row;
        flex-wrap: nowrap;
        gap: 1rem;
        width: 100%;
        max-width: 800px;
        padding: 0 1rem;
    }
    .modal-scrollable h3 {
        font-size: 2.2rem;
    }
    .modal-scrollable h4 {
        font-size: 1.8rem;
    }
    .modal-scrollable p {
        font-size: 1.3rem;
    }
    .modal-gallery {
        flex-direction: row; /* Keep row */
    }
    /* If width is less than 770px, stack images */
    @media (max-width: 770px) {
        .modal-gallery {
            flex-direction: column; /* Stack images */
        }
    }
    .modal-gallery img {
        width: 250px;
        height: 187px; /* 250px * (3/4) for 4:3 aspect ratio */
    }
    .modal-features li {
        font-size: 1.2rem;
    }
    .modal .button-outline,
    .modal .button-primary {
        flex: 1 1 calc(33.33% - 0.5rem); /* 3 buttons, each ~33% */
        width: calc(33.33% - 0.5rem);
        max-width: calc(33.33% - 0.5rem);
        padding: 0.5rem;
        font-size: 1rem;
        height: 40px;
        min-height: 40px;
        white-space: normal;
        border-radius: 10px;
    }
    .modal-close-bottom {
        flex: 1 1 calc(33.33% - 0.5rem);
        width: calc(33.33% - 0.5rem);
        max-width: calc(33.33% - 0.5rem);
        padding: 0.5rem;
        font-size: 1rem;
        height: 40px;
        min-height: 40px;
        white-space: normal;
        border-radius: 10px;
    }
    .modal-close {
        top: 0.5rem;
        right: 0.5rem;
    }
}