/**
 * RSL Thumbnail Component
 *
 * A comprehensive, accessible-first thumbnail/image gallery component for showcasing
 * images with captions, overlays, buttons, and modal integration.
 *
 * Features:
 * - Auto-generated ARIA labels and ALT tags via JavaScript
 * - Customizable overlay colors and opacity
 * - One or two button configurations
 * - Modal support for enlarged image viewing
 * - Full keyboard navigation
 * - Dark mode support
 * - Responsive design
 * - Reduced motion support
 *
 * @version 1.0.0
 * @license MIT
 */

/* ===================================
   Base Thumbnail Styles
   =================================== */

.rsl-thumbnail {
    position: relative;
    display: flex;
    flex-direction: column;
    background: var(--rsl-bg-primary, #ffffff);
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    cursor: pointer;
    height: 100%;
}

.rsl-thumbnail:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.rsl-thumbnail:focus-within {
    outline: 3px solid var(--rsl-primary, #6E7BFF);
    outline-offset: 2px;
}

/* ===================================
   Image Container
   =================================== */

.rsl-thumbnail-image-container {
    position: relative;
    width: 100%;
    padding-bottom: 66.67%; /* 3:2 aspect ratio by default */
    overflow: hidden;
    background: var(--rsl-bg-secondary, #f8f9fa);
}

.rsl-thumbnail-image-container img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.3s ease;
}

.rsl-thumbnail:hover .rsl-thumbnail-image-container img {
    transform: scale(1.05);
}

/* Aspect Ratio Variants */
.rsl-thumbnail[data-thumbnail-aspect="1:1"] .rsl-thumbnail-image-container {
    padding-bottom: 100%;
}

.rsl-thumbnail[data-thumbnail-aspect="16:9"] .rsl-thumbnail-image-container {
    padding-bottom: 56.25%;
}

.rsl-thumbnail[data-thumbnail-aspect="4:3"] .rsl-thumbnail-image-container {
    padding-bottom: 75%;
}

.rsl-thumbnail[data-thumbnail-aspect="21:9"] .rsl-thumbnail-image-container {
    padding-bottom: 42.86%;
}

/* ===================================
   Overlay Styles
   =================================== */

.rsl-thumbnail-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    opacity: 0;
    transition: opacity 0.3s ease;
    padding: 1.5rem;
}

.rsl-thumbnail:hover .rsl-thumbnail-overlay,
.rsl-thumbnail:focus-within .rsl-thumbnail-overlay {
    opacity: 1;
}

/* Overlay Color Variants */
.rsl-thumbnail[data-overlay-color="primary"] .rsl-thumbnail-overlay {
    background: rgba(110, 123, 255, 0.85);
}

.rsl-thumbnail[data-overlay-color="secondary"] .rsl-thumbnail-overlay {
    background: rgba(108, 117, 125, 0.85);
}

.rsl-thumbnail[data-overlay-color="success"] .rsl-thumbnail-overlay {
    background: rgba(40, 167, 69, 0.85);
}

.rsl-thumbnail[data-overlay-color="danger"] .rsl-thumbnail-overlay {
    background: rgba(220, 53, 69, 0.85);
}

.rsl-thumbnail[data-overlay-color="info"] .rsl-thumbnail-overlay {
    background: rgba(23, 162, 184, 0.85);
}

.rsl-thumbnail[data-overlay-color="warning"] .rsl-thumbnail-overlay {
    background: rgba(255, 193, 7, 0.85);
}

.rsl-thumbnail[data-overlay-color="dark"] .rsl-thumbnail-overlay {
    background: rgba(33, 37, 41, 0.85);
}

.rsl-thumbnail[data-overlay-color="light"] .rsl-thumbnail-overlay {
    background: rgba(248, 249, 250, 0.85);
}

/* Custom Overlay Color - uses CSS variable set by JavaScript */
.rsl-thumbnail[data-overlay-color="custom"] .rsl-thumbnail-overlay {
    background: var(--rsl-thumbnail-overlay-color, rgba(0, 0, 0, 0.85));
}

/* Overlay Opacity Variants */
.rsl-thumbnail[data-overlay-opacity="light"] .rsl-thumbnail-overlay {
    opacity: 0;
}

.rsl-thumbnail[data-overlay-opacity="light"]:hover .rsl-thumbnail-overlay,
.rsl-thumbnail[data-overlay-opacity="light"]:focus-within .rsl-thumbnail-overlay {
    opacity: 0.5;
}

.rsl-thumbnail[data-overlay-opacity="medium"] .rsl-thumbnail-overlay {
    opacity: 0;
}

.rsl-thumbnail[data-overlay-opacity="medium"]:hover .rsl-thumbnail-overlay,
.rsl-thumbnail[data-overlay-opacity="medium"]:focus-within .rsl-thumbnail-overlay {
    opacity: 0.75;
}

.rsl-thumbnail[data-overlay-opacity="heavy"] .rsl-thumbnail-overlay {
    opacity: 0;
}

.rsl-thumbnail[data-overlay-opacity="heavy"]:hover .rsl-thumbnail-overlay,
.rsl-thumbnail[data-overlay-opacity="heavy"]:focus-within .rsl-thumbnail-overlay {
    opacity: 0.95;
}

/* ===================================
   Button Styles
   =================================== */

.rsl-thumbnail-buttons {
    display: flex;
    gap: 0.75rem;
    align-items: center;
    justify-content: center;
    flex-wrap: wrap;
}

.rsl-thumbnail-button,
a.rsl-thumbnail-button {
    padding: 0.625rem 1.25rem;
    background: var(--rsl-thumbnail-btn-bg, rgba(255, 255, 255, 0.95));
    color: var(--rsl-thumbnail-btn-color, #333) !important;
    border: none;
    border-radius: 4px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    text-decoration: none;
}

.rsl-thumbnail-button:hover,
.rsl-thumbnail-button:focus,
a.rsl-thumbnail-button:hover,
a.rsl-thumbnail-button:focus {
    background: var(--rsl-thumbnail-btn-hover-bg, #ffffff);
    color: var(--rsl-thumbnail-btn-hover-color, var(--rsl-thumbnail-btn-color, #333)) !important;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.rsl-thumbnail-button:focus {
    outline: 2px solid #fff;
    outline-offset: 2px;
}

.rsl-thumbnail-button i {
    font-size: 0.875rem;
    color: var(--rsl-thumbnail-btn-icon-color, inherit);
    transition: color 0.2s ease;
}

.rsl-thumbnail-button:hover i,
.rsl-thumbnail-button:focus i {
    color: var(--rsl-thumbnail-btn-icon-hover-color, var(--rsl-thumbnail-btn-icon-color, inherit));
}

/* Primary Button Variant */
.rsl-thumbnail-button.btn-primary {
    background: var(--rsl-primary, #6E7BFF);
    color: white;
}

.rsl-thumbnail-button.btn-primary:hover,
.rsl-thumbnail-button.btn-primary:focus {
    background: var(--rsl-primary-hover, #5568e6);
}

/* ===================================
   Caption/Content Area
   =================================== */

.rsl-thumbnail-content {
    padding: 1.25rem;
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.rsl-thumbnail-header {
    font-size: 1.125rem;
    font-weight: 700;
    color: var(--rsl-text-primary, #1e293b);
    margin: 0;
    line-height: 1.4;
}

.rsl-thumbnail-description {
    font-size: 0.9375rem;
    color: var(--rsl-text-secondary, #64748b);
    margin: 0;
    line-height: 1.6;
    flex: 1;
}

.rsl-thumbnail-link {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    color: var(--rsl-primary, #6E7BFF);
    text-decoration: none;
    font-weight: 600;
    font-size: 0.875rem;
    margin-top: 0.5rem;
    transition: color 0.2s ease;
}

.rsl-thumbnail-link:hover,
.rsl-thumbnail-link:focus {
    color: var(--rsl-primary-hover, #5568e6);
    text-decoration: underline;
}

.rsl-thumbnail-link i {
    font-size: 0.75rem;
}

/* ===================================
   Modal Integration
   =================================== */

.rsl-thumbnail-modal .modal {
    max-width: 90vw;
    max-height: 90vh;
}

.rsl-thumbnail-modal .rsl-thumbnail-modal-image {
    width: 100%;
    height: auto;
    max-height: 70vh;
    object-fit: contain;
    border-radius: 4px;
}

.rsl-thumbnail-modal .modal-body {
    padding: 0;
    overflow: hidden;
}

.rsl-thumbnail-modal .modal-footer {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    align-items: stretch;
}

.rsl-thumbnail-modal .rsl-thumbnail-modal-caption {
    padding: 1rem;
    background: var(--rsl-bg-secondary, #f8f9fa);
    border-radius: 6px;
    order: -1; /* Place caption before button */
    text-align: left;
}

.rsl-thumbnail-modal .rsl-thumbnail-modal-caption h3 {
    margin: 0 0 0.75rem 0;
    color: var(--rsl-text-primary, #1e293b);
    font-size: 1.25rem;
}

.rsl-thumbnail-modal .rsl-thumbnail-modal-caption p {
    margin: 0;
    color: var(--rsl-text-secondary, #64748b);
    line-height: 1.6;
}

.rsl-thumbnail-modal .modal-footer .btn {
    align-self: flex-end;
}

/* ===================================
   Link Wrapper Support
   =================================== */

a.rsl-thumbnail {
    text-decoration: none;
    color: inherit;
}

a.rsl-thumbnail:focus {
    outline: 3px solid var(--rsl-primary, #6E7BFF);
    outline-offset: 2px;
}

/* ===================================
   Loading State
   =================================== */

.rsl-thumbnail.is-loading .rsl-thumbnail-image-container::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 40px;
    height: 40px;
    margin: -20px 0 0 -20px;
    border: 3px solid rgba(110, 123, 255, 0.3);
    border-top-color: var(--rsl-primary, #6E7BFF);
    border-radius: 50%;
    animation: rsl-thumbnail-spin 0.8s linear infinite;
}

@keyframes rsl-thumbnail-spin {
    to {
        transform: rotate(360deg);
    }
}

/* ===================================
   Disabled State
   =================================== */

.rsl-thumbnail[aria-disabled="true"],
.rsl-thumbnail.is-disabled {
    opacity: 0.6;
    cursor: not-allowed;
    pointer-events: none;
}

/* ===================================
   Keyboard Navigation
   =================================== */

.rsl-thumbnail:focus {
    outline: 3px solid var(--rsl-primary, #6E7BFF);
    outline-offset: 2px;
}

.rsl-thumbnail-button:focus-visible {
    outline: 2px solid #fff;
    outline-offset: 2px;
}

/* ===================================
   Dark Mode Support
   =================================== */

[data-theme="dark"] .rsl-thumbnail {
    background: var(--rsl-dark-bg-primary, #1e293b);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
}

[data-theme="dark"] .rsl-thumbnail:hover {
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.6);
}

[data-theme="dark"] .rsl-thumbnail-image-container {
    background: var(--rsl-dark-bg-secondary, #0f172a);
}

[data-theme="dark"] .rsl-thumbnail-header {
    color: var(--rsl-dark-text-primary, #f1f5f9);
}

[data-theme="dark"] .rsl-thumbnail-description {
    color: var(--rsl-dark-text-secondary, #94a3b8);
}

[data-theme="dark"] .rsl-thumbnail-link {
    color: var(--rsl-dark-primary, #818cf8);
}

[data-theme="dark"] .rsl-thumbnail-link:hover {
    color: var(--rsl-dark-primary-hover, #a5b4fc);
}

[data-theme="dark"] .rsl-thumbnail-button,
[data-theme="dark"] a.rsl-thumbnail-button {
    background: rgba(30, 41, 59, 0.95);
    color: #f1f5f9 !important;
}

[data-theme="dark"] .rsl-thumbnail-button:hover,
[data-theme="dark"] .rsl-thumbnail-button:focus,
[data-theme="dark"] a.rsl-thumbnail-button:hover,
[data-theme="dark"] a.rsl-thumbnail-button:focus {
    background: rgba(30, 41, 59, 1);
    color: #ffffff !important;
}

[data-theme="dark"] .rsl-thumbnail-modal .rsl-thumbnail-modal-caption {
    background: var(--rsl-dark-bg-primary, #1e293b);
}

/* ===================================
   Responsive Design
   =================================== */

@media (max-width: 768px) {
    .rsl-thumbnail-content {
        padding: 1rem;
    }

    .rsl-thumbnail-header {
        font-size: 1rem;
    }

    .rsl-thumbnail-description {
        font-size: 0.875rem;
    }

    .rsl-thumbnail-button {
        padding: 0.5rem 1rem;
        font-size: 0.8125rem;
    }

    .rsl-thumbnail-overlay {
        padding: 1rem;
    }
}

@media (max-width: 480px) {
    .rsl-thumbnail-buttons {
        flex-direction: column;
        width: 100%;
        align-items: center;
    }

    .rsl-thumbnail-button {
        width: auto;
        max-width: 80%;
        justify-content: center;
    }
}

/* ===================================
   Reduced Motion Support
   =================================== */

@media (prefers-reduced-motion: reduce) {
    .rsl-thumbnail,
    .rsl-thumbnail-image-container img,
    .rsl-thumbnail-overlay,
    .rsl-thumbnail-button {
        transition: none;
    }

    .rsl-thumbnail:hover {
        transform: none;
    }

    .rsl-thumbnail:hover .rsl-thumbnail-image-container img {
        transform: none;
    }

    .rsl-thumbnail-button:hover {
        transform: none;
    }

    @keyframes rsl-thumbnail-spin {
        to {
            transform: none;
        }
    }
}

/* ===================================
   Print Styles
   =================================== */

@media print {
    .rsl-thumbnail-overlay {
        display: none;
    }

    .rsl-thumbnail {
        box-shadow: none;
        page-break-inside: avoid;
    }

    .rsl-thumbnail-image-container {
        padding-bottom: 0;
        height: auto;
    }

    .rsl-thumbnail-image-container img {
        position: relative;
        height: auto;
    }
}

/* ===================================
   Grid Showcase Integration
   =================================== */

.slot-layout .rsl-thumbnail {
    height: 100%;
}

/* Equal height thumbnails in grid */
[data-thumbnail-equal-height="true"].slot-layout {
    align-items: stretch;
}

[data-thumbnail-equal-height="true"] .slot-item {
    display: flex;
}

[data-thumbnail-equal-height="true"] .rsl-thumbnail {
    display: flex;
    flex-direction: column;
}

[data-thumbnail-equal-height="true"] .rsl-thumbnail-content {
    flex: 1;
}

/* ===================================
   Accessibility Enhancements
   =================================== */

/* Screen reader only text */
.rsl-thumbnail .sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .rsl-thumbnail {
        border: 2px solid currentColor;
    }

    .rsl-thumbnail:focus {
        outline-width: 4px;
    }

    .rsl-thumbnail-button {
        border: 2px solid currentColor;
    }
}
