/**
 * Gallery Display Styles
 * Styles for gallery rendering in editor and published views
 */

/* Quill Editor Gallery Block */
.quill-gallery-block {
    margin: 1rem 0;
    border: 2px solid #e5e7eb;
    border-radius: 0.5rem;
    background: #fafafa;
    transition: all 0.2s;
    cursor: default;
}

.quill-gallery-block:hover {
    border-color: #5C4033;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}

.quill-gallery-block.ql-selected {
    border-color: #5C4033;
    box-shadow: 0 0 0 3px rgba(92, 64, 51, 0.1);
}

/* Gallery Header (Editor View) */
.gallery-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    background: white;
    border-bottom: 1px solid #e5e7eb;
    border-radius: 0.5rem 0.5rem 0 0;
}

.gallery-info {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.gallery-icon {
    font-size: 1.25rem;
}

.gallery-title {
    font-weight: 500;
    color: #374151;
    font-size: 0.875rem;
}

.gallery-controls {
    display: flex;
    gap: 0.5rem;
}

.gallery-edit-btn,
.gallery-delete-btn {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.375rem 0.75rem;
    border: 1px solid #d1d5db;
    background: white;
    border-radius: 0.375rem;
    font-size: 0.75rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

.gallery-edit-btn {
    color: #5C4033;
    border-color: #5C4033;
}

.gallery-edit-btn:hover {
    background: #5C4033;
    color: white;
}

.gallery-delete-btn {
    color: #dc2626;
    border-color: #dc2626;
}

.gallery-delete-btn:hover {
    background: #dc2626;
    color: white;
}

.gallery-edit-btn svg,
.gallery-delete-btn svg {
    width: 14px;
    height: 14px;
}

/* Gallery Preview Grid (Editor View) */
.gallery-preview-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(80px, 1fr));
    gap: 0.5rem;
    padding: 1rem;
    max-height: 200px;
    overflow: hidden;
}

.preview-thumb {
    position: relative;
    aspect-ratio: 1;
    border-radius: 0.375rem;
    overflow: hidden;
    background: #f3f4f6;
}

.preview-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.preview-caption {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: linear-gradient(transparent, rgba(0, 0, 0, 0.7));
    color: white;
    padding: 0.25rem 0.5rem;
    font-size: 0.625rem;
    line-height: 1.2;
}

/* Hide editor-specific elements EVERYWHERE in preview/published contexts */
body:not(.admin-editor) .gallery-header,
body:not(.admin-editor) .gallery-controls,
body:not(.admin-editor) .gallery-edit-btn,
body:not(.admin-editor) .gallery-delete-btn,
body:not(.admin-editor) .gallery-preview-grid,
body:not(.admin-editor) .preview-thumb,
.published-gallery .gallery-header,
.published-gallery .gallery-controls,
.published-gallery .gallery-edit-btn,
.published-gallery .gallery-delete-btn,
.published-gallery .gallery-preview-grid,
.published-gallery .preview-thumb {
    display: none !important;
    visibility: hidden !important;
    opacity: 0 !important;
    height: 0 !important;
    overflow: hidden !important;
}

/* Hide editor styling from quill-gallery-block in preview contexts */
body:not(.admin-editor) .quill-gallery-block {
    background: transparent !important;
    border: none !important;
    padding: 0 !important;
    margin: 1.6875rem 0 !important;
}

/* Show images in quill-gallery-block even in preview if JavaScript fails */
body:not(.admin-editor) .quill-gallery-block .preview-thumb {
    display: none !important; /* Still hide thumbnails */
}

body:not(.admin-editor) .quill-gallery-block .preview-thumb img {
    display: none !important; /* Hide thumbnail images */
}

/* Published Gallery Styles */
.published-gallery {
    margin: 1.6875rem 0; /* 27px to match paragraph spacing (1.5em at 1.125rem font) */
    max-width: 100%;
    display: block !important;
    visibility: visible !important;
    opacity: 1 !important;
    background: none !important;
    border: none !important;
    padding: 0 !important;
}

/* Default styling for galleries without explicit resolution */
.published-gallery:not([data-resolution-type]) {
    max-width: 800px;
    margin-left: auto;
    margin-right: auto;
}

.published-gallery:not([data-resolution-type]) .gallery-grid {
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
}

.published-gallery .gallery-grid {
    display: grid;
    gap: 1rem;
    /* Default grid columns are now controlled by resolution-specific rules above */
}

.published-gallery .gallery-item {
    margin: 0;
    padding: 0;
    position: relative;
    /* Clean design without borders or effects */
}

/* Reset figure element browser defaults */
.published-gallery figure {
    margin: 0;
    padding: 0;
}

.published-gallery .gallery-item:hover {
    /* Subtle hover effect without affecting layout */
}

.published-gallery .gallery-item img {
    width: 100%;
    height: auto;
    display: block !important;
    visibility: visible !important;
    opacity: 1 !important;
    margin: 0;
    padding: 0;
    vertical-align: bottom; /* Eliminate any baseline gap */
    transition: opacity 0.2s;
}

.published-gallery .gallery-item:hover img {
    opacity: 0.95; /* Subtle opacity change on hover */
}

.published-gallery .gallery-item figcaption {
    padding: 0;  /* Completely remove all padding */
    margin: 0;  /* Override browser default margins */
    margin-top: 6px; /* Small gap for better visual balance */
    text-align: center;
    font-style: italic;
    color: #6b7280;
    font-size: 0.875rem;
    line-height: 1.2; /* Tighter line height */
    /* background removed - transparent */
}

/* Resolution-specific styling - Controls individual image widths, not container */
.published-gallery[data-resolution-type="preset"][data-preset="small"] .gallery-grid {
    grid-template-columns: repeat(auto-fill, minmax(min(270px, 100%), 1fr));
}

.published-gallery[data-resolution-type="preset"][data-preset="small"] .gallery-item {
    max-width: 270px;
}

.published-gallery[data-resolution-type="preset"][data-preset="medium"] .gallery-grid {
    grid-template-columns: repeat(auto-fill, minmax(min(400px, 100%), 1fr));
}

.published-gallery[data-resolution-type="preset"][data-preset="medium"] .gallery-item {
    max-width: 400px;
}

.published-gallery[data-resolution-type="preset"][data-preset="large"] .gallery-grid {
    grid-template-columns: repeat(auto-fill, minmax(min(1200px, 100%), 1fr));
}

.published-gallery[data-resolution-type="preset"][data-preset="large"] .gallery-item {
    max-width: 1200px;
}

/* Custom resolution styling - Apply dimensions to images, not container */
.published-gallery[data-resolution-type="custom"] .gallery-grid {
    grid-template-columns: repeat(auto-fill, minmax(min(var(--custom-width, 200px), 100%), 1fr));
}

.published-gallery[data-resolution-type="custom"] .gallery-item {
    max-width: var(--custom-width, none);
}

.published-gallery[data-resolution-type="custom"] img {
    width: 100%;
    max-height: var(--custom-height, none);
    height: var(--custom-height, auto);
}

/* Aspect ratio handling */
.published-gallery[data-maintain-aspect="true"] img {
    object-fit: contain;
    width: 100%;
    height: auto;
}

.published-gallery[data-maintain-aspect="false"] img {
    object-fit: cover;
    width: 100%;
    height: var(--custom-height, 300px);
}

/* Lightbox Integration (if needed) */
.gallery-item[data-lightbox] {
    cursor: pointer;
}

.gallery-item[data-lightbox]:after {
    content: '🔍';
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    background: rgba(0, 0, 0, 0.7);
    color: white;
    border-radius: 50%;
    width: 2rem;
    height: 2rem;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.875rem;
    opacity: 0;
    transition: opacity 0.2s;
}

.gallery-item[data-lightbox]:hover:after {
    opacity: 1;
}

/* Gallery Item Button */
.gallery-item-button {
    display: block;  /* Changed from inline-block to block for consistent width */
    width: 100%;  /* Full width of parent container */
    max-width: 280px;  /* Consistent maximum width */
    margin: 0.5rem auto 0;  /* Center the button */
    padding: 0.25rem 1.25rem;  /* 4px vertical - ultra-thin */

    /* Flat warm beige - sophisticated neutral */
    background: #F5F1ED;

    color: #4B5563 !important;
    text-decoration: none !important;
    text-shadow: none;

    border: none;  /* Borderless for clean modern aesthetic */
    border-radius: 0.5rem;  /* 8px for modern look */

    font-size: 0.8125rem;
    font-weight: 600;
    font-family: 'Inter', sans-serif;
    text-align: center;

    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.04);

    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    cursor: pointer;

    /* Prevent any underline flickering during transitions */
    text-decoration-line: none !important;
    text-decoration-style: none !important;
    text-decoration-color: transparent !important;
    /* Handle text overflow gracefully */
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.gallery-item-button:hover {
    background: #EDE9E5;  /* Slightly darker warm beige */
    color: #4B5563 !important;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.06), 0 1px 2px rgba(0, 0, 0, 0.03);
    transform: translateY(-2px) scale(1.02);
    text-decoration: none !important;
    text-decoration-line: none !important;
    text-decoration-style: none !important;
    text-decoration-color: transparent !important;
}

.gallery-item-button:active {
    transform: translateY(0);
    text-decoration: none !important;
}

.gallery-item-button:focus {
    text-decoration: none !important;
    outline: 2px solid #D1D5DB;  /* Lighter grey for subtle focus */
    outline-offset: 0;  /* No gap - outline flush with button edge */
}

.gallery-item-button:visited {
    color: #4B5563 !important;
    text-decoration: none !important;
}

/* Center the button in the figure */
figure.gallery-item {
    display: flex;
    flex-direction: column;
    align-items: center;
}

figure.gallery-item img {
    width: 100%;
}

figure.gallery-item figcaption {
    width: 100%;
}

/* Preview indicator for buttons in editor */
.preview-has-button {
    position: absolute;
    top: 0.25rem;
    right: 0.25rem;
    background: #5C4033;
    color: white;
    width: 1.5rem;
    height: 1.5rem;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    opacity: 0.9;
    pointer-events: none;
}

/* Gallery Error Display */
.gallery-error {
    padding: 1rem;
    background: #fef2f2;
    border: 1px solid #fecaca;
    border-radius: 0.5rem;
    color: #dc2626;
    text-align: center;
    font-style: italic;
}

/* Loading States */
.gallery-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    color: #6b7280;
}

.gallery-loading:before {
    content: '';
    width: 1rem;
    height: 1rem;
    border: 2px solid #e5e7eb;
    border-top-color: #5C4033;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-right: 0.5rem;
}

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

/* Responsive Adjustments */
@media (max-width: 640px) {
    .published-gallery {
        margin: 1.6875rem 0; /* Keep same 27px spacing on mobile for consistency */
    }

    .gallery-grid {
        gap: 0.75rem;
    }

    .gallery-item figcaption {
        padding: 0;  /* Match desktop - no padding at all */
        margin: 0;  /* Override browser defaults on mobile too */
        margin-top: 6px; /* Match desktop spacing */
        font-size: 0.8125rem;
        line-height: 1.2; /* Tighter line height */
    }

    .gallery-item-button {
        max-width: 100%;  /* Full width on mobile for better touch targets */
        padding: 0.25rem 1rem;  /* Match desktop ultra-thin padding */
        font-size: 0.8125rem;
    }

    /* Responsive grid adjustments for mobile */
    .published-gallery[data-resolution-type="preset"][data-preset="small"] .gallery-grid {
        grid-template-columns: repeat(auto-fill, minmax(min(200px, 100%), 1fr));
    }

    .published-gallery[data-resolution-type="preset"][data-preset="medium"] .gallery-grid {
        grid-template-columns: repeat(auto-fill, minmax(min(270px, 100%), 1fr));
    }

    .published-gallery[data-resolution-type="preset"][data-preset="large"] .gallery-grid {
        grid-template-columns: 1fr; /* Single column on mobile for large images */
    }

    /* Custom dimensions on mobile */
    .published-gallery[data-resolution-type="custom"] .gallery-grid {
        grid-template-columns: repeat(auto-fill, minmax(min(var(--custom-width, 200px), 100%), 1fr));
    }

    .gallery-preview-grid {
        grid-template-columns: repeat(auto-fit, minmax(60px, 1fr));
        gap: 0.375rem;
        padding: 0.75rem;
    }

    .gallery-header {
        padding: 0.5rem 0.75rem;
    }

    .gallery-title {
        font-size: 0.8125rem;
    }

    .gallery-controls {
        gap: 0.375rem;
    }

    .gallery-edit-btn,
    .gallery-delete-btn {
        padding: 0.25rem 0.5rem;
        font-size: 0.6875rem;
    }

    .gallery-edit-btn svg,
    .gallery-delete-btn svg {
        width: 12px;
        height: 12px;
    }
}

/* Print Styles */
@media print {
    .gallery-header,
    .gallery-controls {
        display: none;
    }

    .quill-gallery-block {
        border: none;
        background: transparent;
        break-inside: avoid;
    }

    .published-gallery {
        break-inside: avoid;
    }

    .gallery-item {
        break-inside: avoid;
    }
}

/* High Contrast Mode Support */
@media (prefers-contrast: high) {
    .quill-gallery-block {
        border-width: 3px;
    }

    .gallery-header {
        background: #f9fafb;
    }

    .gallery-item {
        border: 2px solid #374151;
    }

    .gallery-edit-btn,
    .gallery-delete-btn {
        border-width: 2px;
    }
}

/* Reduced Motion Support */
@media (prefers-reduced-motion: reduce) {
    .gallery-edit-btn,
    .gallery-delete-btn,
    .quill-gallery-block {
        transition: none;
    }

    .gallery-loading:before {
        animation: none;
    }
}

/* Dark Mode Support (if applicable) */
@media (prefers-color-scheme: dark) {
    .quill-gallery-block {
        background: #1f2937;
        border-color: #4b5563;
    }

    .gallery-header {
        background: #111827;
        border-color: #4b5563;
    }

    .gallery-title {
        color: #e5e7eb;
    }

    .gallery-edit-btn {
        background: #1f2937;
        color: #D88C9A;
        border-color: #D88C9A;
    }

    .gallery-delete-btn {
        background: #1f2937;
        color: #f87171;
        border-color: #f87171;
    }

    .gallery-item figcaption {
        /* background removed - transparent */
        color: #d1d5db;
    }

    .gallery-item-button {
        /* Same warm beige in dark mode for consistency */
        background: #F5F1ED;
        color: #1F2937 !important;
        text-decoration: none !important;
        text-shadow: none;
        border: none;
        border-radius: 0.5rem;
        font-size: 0.8125rem;
        font-weight: 600;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.04);
    }

    .gallery-item-button:hover {
        background: #EDE9E5;  /* Same warm beige hover in dark mode */
        color: #1F2937 !important;
        text-decoration: none !important;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.06), 0 1px 2px rgba(0, 0, 0, 0.03);
        transform: translateY(-2px) scale(1.02);
    }

    .gallery-item-button:visited {
        color: #1F2937 !important;
        text-decoration: none !important;
    }

    .preview-has-button {
        background: #D88C9A;
        color: #1f2937;
    }

    .gallery-error {
        background: #451a1a;
        border-color: #991b1b;
        color: #f87171;
    }
}