.nbsphinx-gallery {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); /* Increased min-width for larger items */
    gap: 20px; /* Increased gap for more breathing room */
    margin-top: 1.5em;
    margin-bottom: 1.5em;
}

.nbsphinx-gallery > a {
    background-color: #f8f9fa; /* A light background color for the card */
    border: 1px solid #dee2e6; /* A more defined, solid border */
    border-radius: 8px; /* Softer, rounded corners */
    text-align: left; /* Align text to the left for better readability */
    text-decoration: none; /* remove underline from links */
    color: #212529; /* A dark color for the text for better contrast */
    padding: 15px; /* Increased padding inside the card */
    transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out; /* Smooth transitions for hover effects */
    display: flex; /* Use flexbox for better content alignment */
    flex-direction: column; /* Stack content vertically */
}

.nbsphinx-gallery > a:hover {
    transform: translateY(-5px); /* Lift the card on hover */
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); /* Add a shadow for depth on hover */
    border-color: #007bff; /* Highlight with a primary color on hover */
}

/* Container for the thumbnail image */
.nbsphinx-gallery > a > div:first-child {
    height: 180px; /* A larger, fixed height for the image container */
    display: flex;
    align-items: center; /* Center the image vertically */
    justify-content: center; /* Center the image horizontally */
    margin-bottom: 15px; /* More space between the image and the title */
    background-color: #ffffff; /* White background for the image area */
    border-radius: 4px; /* Slightly rounded corners for the image container */
    overflow: hidden; /* Hide anything that spills out of the image container */
}

.nbsphinx-gallery img {
    max-width: 100%;
    max-height: 100%;
    object-fit: cover; /* Ensure the image covers the area without distortion */
}

/* Styling for the title text */
.nbsphinx-gallery .gallery-text {
    font-size: 1.0rem !important;
    line-height: 1.3;
    margin-top: 8px; /* Add some space above the title */
}

/* Target gallery captions - these are likely generated by nblinkgallery directive */
h2.caption-text,
p.caption-text,
.caption-text,
.gallery-caption,
.nbsphinx-gallery-caption,
h2:has(+ .nbsphinx-gallery),
p:has(+ .nbsphinx-gallery),
.nbsphinx-gallery + h2,
.nbsphinx-gallery + p,
/* Sphinx-generated gallery captions */
.toctree-wrapper p.caption,
.rubric,
h2.rubric,
p.rubric {
    font-size: 1.8rem !important;
    font-weight: 700 !important;
    margin-bottom: 1rem !important;
    margin-top: 2rem !important;
    line-height: 1.2 !important;
    /* Use CSS variables that adapt to theme, with fallbacks */
    color: var(--color-foreground-primary, var(--color-brand-primary, #333)) !important;
}

/* Fallback for explicit dark mode support */
@media (prefers-color-scheme: dark) {
    h2.caption-text,
    p.caption-text,
    .caption-text,
    .gallery-caption,
    .nbsphinx-gallery-caption,
    h2:has(+ .nbsphinx-gallery),
    p:has(+ .nbsphinx-gallery),
    .nbsphinx-gallery + h2,
    .nbsphinx-gallery + p,
    .toctree-wrapper p.caption,
    .rubric,
    h2.rubric,
    p.rubric {
        color: var(--color-foreground-primary, var(--color-brand-primary, #e0e0e0)) !important;
    }
}

/* Additional Furo theme specific selectors */
[data-theme="dark"] h2.caption-text,
[data-theme="dark"] p.caption-text,
[data-theme="dark"] .caption-text,
[data-theme="dark"] .gallery-caption,
[data-theme="dark"] .nbsphinx-gallery-caption,
[data-theme="dark"] h2:has(+ .nbsphinx-gallery),
[data-theme="dark"] p:has(+ .nbsphinx-gallery),
[data-theme="dark"] .nbsphinx-gallery + h2,
[data-theme="dark"] .nbsphinx-gallery + p,
[data-theme="dark"] .toctree-wrapper p.caption,
[data-theme="dark"] .rubric,
[data-theme="dark"] h2.rubric,
[data-theme="dark"] p.rubric {
    color: var(--color-foreground-primary, #e0e0e0) !important;
}

