:root {
    --modal-height: 85vh;
    --modal-padding: 30px;
    --modal-overlay: rgba(74, 55, 40, 0.95); /* Deep Earth Brown for modal overlay */
    --modal-accent-line: rgba(74, 55, 40, 0.1);
}

/* Force Scrollbar */
html {
    overflow-y: scroll;
}

/* Gallery Layout */
.gallery-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 30px;
    padding: 20px;
}

/* --- Thumbnail (Cropped) --- */
.gallery-item {
    aspect-ratio: 1 / 1; /* Forces the square 'frame' */
    overflow: hidden;    /* Clips anything outside the square */
    border: var(--border-thickness) solid var(--earth-brown);
    background: var(--bg-color); /* Use the paper color here too, avoid pure white */
    transition: transform 0.8s cubic-bezier(0.22, 1, 0.36, 1), 
                box-shadow 0.8s ease,
                border-color 0.8s ease;
}

.gallery-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;   /* Fills the square by cropping the sides/top */
    transition: transform 0.8s cubic-bezier(0.22, 1, 0.36, 1),
                filter 0.8s ease;
    opacity: 0.95; /* Start slightly dimmed for eye comfort */
}

.gallery-item:hover{
    /* Soft 'life' and a color shift */
    transform: translateY(-5px); 
    border-color: var(--fox-copper);
    box-shadow: 0 10px 20px rgba(74, 55, 40, 0.1);
}

.gallery-item:hover img {
    transform: scale(1.03);
    opacity: 1;    /* Gently brightens from starting opacity */
    filter: saturate(1.1);
}



/* Modal Styling */
.modal {
    display: none; 
    position: fixed;
    z-index: 2000;
    top: 0; left: 0;
    width: 100%; height: 100%;
    background-color: var(--modal-overlay); 
    justify-content: center;
    align-items: center;
    animation: fadeInOverlay 0.7s ease forwards;
}

/* --- Modal (Scaled to Fit / Never Cropped) --- */
.modal-content {
    /* Unified Paper Background */
    background-color: #ebe7de; 
    border: var(--border-thickness) solid var(--earth-brown);
    max-width: 90%;
    max-height: var(--modal-height);
    display: flex;
    flex-direction: row;
    gap: 0px;
    padding: var(--modal-padding);
    overflow: hidden;
    align-items: center;
    box-sizing: border-box;
    animation: slideUpPaper 0.7s cubic-bezier(0.165, 0.84, 0.44, 1) forwards;
}

#modalImg {
    /* Fixed Width Constraint */
    width: 60%; 
    /* Max-height ensures it scales down if it's too tall */
    max-height: calc(var(--modal-height) - (var(--modal-padding) * 2)); /* The Parent's (modal-content)  height - parent's padding x 2 */
    object-fit: scale-down; 
    display: block;
    height: auto; /* Maintains original proportions */
}

/*--- Modal Animations ---*/
@keyframes fadeInOverlay {
    from { background-color: rgba(74, 55, 40, 0); }
    to { background-color: rgba(74, 55, 40, 0.95); }
}

@keyframes slideUpPaper {
    from {
        opacity: 0;
        transform: translateY(30px) scale(0.98); /* Starts lower and slightly smaller */
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1); /* Settles into place */
    }
}

.modal-text {
    flex: 0 0 40%;      /* Grow: 0, Shrink: 0, Basis: 40% */
    width: 40%;         /* Explicit width for Safari */
    box-sizing: border-box;      
    padding-left: 40px;     /* Space between art and story */
    background-color: #ebe7de; 
    max-height: 100%;
    overflow-y: auto;                /* If the story is long, it scrolls independently */
    border-left: 1px solid var(--modal-accent-line); 
}

.modal-text h3 {
    font-family: 'DM Serif Display', serif;
    color: var(--forest-green);
    margin-top: 0;
    font-size: 2.2rem;
}

.modal-text p {
    font-family: 'Inter', sans-serif;
    line-height: 1.8;
    color: var(--earth-brown);
}

/* This ensures the close button stands out against the dark overlay */
.close-modal {
    position: absolute;
    top: 20px;
    right: 30px;
    font-size: 45px;
    color: var(--bg-color); /* Using the bg color for now - may need to set this as a new color/variable later */
    cursor: pointer;
    line-height: 1;
    transition: transform 0.2s ease;
}

.close-modal:hover {
    transform: scale(1.2);
    color: var(--fox-copper);
}

.modal-image-frame {
    flex: 0 0 60%;      /* Grow: 0, Shrink: 0, Basis: 60% */
    width: 60%;         /* Explicit width for Safari - it was being fussy in testing */
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;       /* Ensure it spans the full height of the modal */
    min-height: 75vh;
}

#modalImg {
    width: auto;
    max-width: 100%;    /* Never wider than the 60% frame */
    max-height: calc(var(--modal-height) - (var(--modal-padding) * 2));
    object-fit: contain;
    display: block;
    opacity: 0;
    transition: opacity 0.5s ease-in;
    filter: blur(2px);
}

/* This class will be added by JS once the image is ready */
#modalImg.loaded {
    opacity: 1;
    filter: blur(0);
}

/* --- Mobile Refinement for Modal --- */
@media (max-width: 768px) {
    .modal-content {
        flex-direction: column; /* Stack image on top of text */
        max-height: 90vh;       /* Give it a bit more room on tall phones */
        width: 95%;            /* Fill more of the screen width */
        padding: 15px;         /* Tighten padding to save space */
        overflow-y: auto;      /* Ensure the whole modal can scroll if needed */
    }

    .modal-image-frame {
        width: 100%;
        flex: 0 0 auto; /* Let the height be the boss on mobile */
        /* Setting a 'placeholder' height for the image area.  45vh matches #modalImg */
        height: 45vh; 
        min-height: 250px; 
        
        display: flex;
        justify-content: center;
        align-items: center;
    }

    #modalImg {
        width: auto;
        max-width: 100%;
        max-height: 100%;      /* Limit image height so text is visible below it */
        margin-bottom: 20px;   /* Space between art and title */
    }

    .modal-text {
        width: 100%;           /* Text takes full width */
        padding: 0 10px 20px;  /* Minimal padding */
        border-left: none;     /* Remove that vertical line on mobile */
        text-align: center;    /* Center text for a better mobile 'portrait' look */
    }

    .modal-text h3 {
        font-size: 1.5rem;     /* Significantly smaller than desktop's 2.2rem */
        margin-bottom: 10px;
        line-height: 1.2;
    }

    .modal-text p {
        font-size: 0.95rem;    /* Slightly smaller paragraph text */
        line-height: 1.5;
    }

    .close-modal {
        top: 10px;
        right: 15px;
        font-size: 30px;       /* Smaller close button so it's not in the way */
        color: var(--earth-brown); /* Darker color so it's visible on the paper bg */
    }
}