/* =============================================================
   SureDash Post Image Grid — image-grid.css
   Version: 5.0.0
   ============================================================= */


/* =============================================================
   1. Feed card grid — shared item styles
   ============================================================= */

.spig-grid {
    width: 100%;
    border-radius: 8px;
    overflow: hidden;
    margin: 8px 0;
    line-height: 0;
}

.spig-item {
    overflow: hidden;
    cursor: pointer;
    position: relative;
    background: #1a1a1a;
    line-height: 0;
}

.spig-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    transition: transform 0.2s ease;
}

.spig-item:hover img {
    transform: scale(1.03);
}


/* =============================================================
   2. Grid layouts — count 1 through 6
   CSS only defines layouts up to 6. The JS DISPLAY_CAP constant
   must match: posts with 7–10 images show 6 tiles + "+N" overlay.

   Counts 2–6 use aspect-ratio: 1/1 on tiles so all grids are
   square-cropped regardless of original image orientation.
   Count 1 is never cropped — always shown at natural size.
   Count 3: right two tiles are square; left tile spans both rows
            and becomes a natural tall portrait strip.
   Count 5: first tile is a wide landscape hero (2:1 ratio auto-
            computed from 3-column layout); tiles 2–5 are square.
   ============================================================= */

/* ── Count 1: centered, natural height, no crop ─────────────── */
.spig-grid.spig-count-1 {
    max-width: 65%; /* fallback before orientation is detected */
    border-radius: 8px;
    margin-left: auto;
    margin-right: auto;
}

.spig-grid.spig-count-1.spig-portrait  { max-width: 55%; }
.spig-grid.spig-count-1.spig-landscape { max-width: 75%; }

.spig-grid.spig-count-1 .spig-item {
    border-radius: 8px;
    height: auto;
    max-height: none;
    overflow: visible;
}

.spig-grid.spig-count-1 .spig-item img {
    height: auto;
    width: 100%;
    object-fit: contain;
    background: transparent;
}

/* ── Count 2: two equal square columns ──────────────────────── */
.spig-grid.spig-count-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 3px;
}

.spig-grid.spig-count-2 .spig-item {
    aspect-ratio: 1 / 1;
}

/* ── Count 3: left tall portrait strip, right 2 squares ─────── */
.spig-grid.spig-count-3 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto auto;
    gap: 3px;
    max-width: 75%;
    margin-left: auto;
    margin-right: auto;
}

.spig-grid.spig-count-3 .spig-item:first-child {
    grid-row: 1 / 3;
    /* Height is determined by the two square siblings:
       fills both rows naturally for a portrait strip. */
}

.spig-grid.spig-count-3 .spig-item:not(:first-child) {
    aspect-ratio: 1 / 1;
}

/* ── Count 4: 2×2 square grid ───────────────────────────────── */
.spig-grid.spig-count-4 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto auto;
    gap: 3px;
}

.spig-grid.spig-count-4 .spig-item {
    aspect-ratio: 1 / 1;
}

/* ── Count 5: wide landscape hero + 4 squares ───────────────── */
/* 3-column layout. Item 1 spans cols 1–2 (hero, fixed 2:1 ratio).
   Item 2 sits in col 3, row 1, stretches to match hero height
   (no aspect-ratio — grid fills it to same row height as item 1).
   Items 3–5 fill row 2 across all 3 columns, all square.          */
.spig-grid.spig-count-5 {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: auto auto;
    gap: 3px;
    max-width: 75%;
    margin-left: auto;
    margin-right: auto;
}

.spig-grid.spig-count-5 .spig-item:nth-child(1) {
    grid-column: 1 / 3;
    aspect-ratio: 2 / 1; /* Sets row 1 height; item 2 stretches to match. */
}

.spig-grid.spig-count-5 .spig-item:nth-child(2) {
    grid-column: 3 / 4;
    aspect-ratio: 1 / 1; /* Matches row height set by hero's 2/1 ratio — no gap. */
}

.spig-grid.spig-count-5 .spig-item:nth-child(n+3) {
    aspect-ratio: 1 / 1;
}

/* ── Count 6: 3×2 square grid ───────────────────────────────── */
.spig-grid.spig-count-6 {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: auto auto;
    gap: 3px;
}

.spig-grid.spig-count-6 .spig-item {
    aspect-ratio: 1 / 1;
}


/* =============================================================
   3. "+N more" overflow overlay
   ============================================================= */

.spig-item.spig-has-more img {
    filter: brightness(0.45);
}

.spig-more-label {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 28px;
    font-weight: 700;
    pointer-events: none;
    z-index: 2;
    text-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}


/* =============================================================
   4. Mobile — two columns for all multi-image layouts
   ============================================================= */

@media (max-width: 580px) {
    .spig-grid:not(.spig-count-1) {
        grid-template-columns: 1fr 1fr !important;
        grid-template-rows: auto !important;
        height: auto !important;
    }
    .spig-grid:not(.spig-count-1) .spig-item {
        aspect-ratio: auto !important;
        height: 140px;
        grid-column: auto !important;
        grid-row: auto !important;
    }
    .spig-grid.spig-count-1 { max-width: 100%; }
    /* Restore full width on small screens for centered grids */
    .spig-grid.spig-count-3,
    .spig-grid.spig-count-5 {
        max-width: 100% !important;
    }
}


/* =============================================================
   5. Lightbox
   Only rendered when enableLightbox: true in SPIGConfig (default).
   ============================================================= */

#spig-lightbox {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 999999;
    align-items: center;
    justify-content: center;
}

#spig-lightbox.spig-open {
    display: flex;
}

.spig-lb-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.92);
}

.spig-lb-content {
    position: relative;
    z-index: 2;
    max-width: 90vw;
    max-height: 90vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

.spig-lb-img {
    max-width: 90vw;
    max-height: 88vh;
    object-fit: contain;
    border-radius: 4px;
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.6);
    display: block;
}

.spig-lb-close {
    position: absolute;
    top: 16px;
    right: 20px;
    z-index: 3;
    background: rgba(0, 0, 0, 0.5);
    border: none;
    color: #fff;
    font-size: 32px;
    line-height: 1;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
}

.spig-lb-close:hover { background: rgba(255, 255, 255, 0.2); }

.spig-lb-prev,
.spig-lb-next {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    z-index: 3;
    background: rgba(0, 0, 0, 0.45);
    border: none;
    color: #fff;
    font-size: 40px;
    line-height: 1;
    width: 52px;
    height: 52px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
    padding: 0;
}

.spig-lb-prev { left: 16px; }
.spig-lb-next { right: 16px; }

.spig-lb-prev:hover,
.spig-lb-next:hover { background: rgba(255, 255, 255, 0.2); }

.spig-lb-counter {
    position: absolute;
    bottom: 16px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 3;
    color: rgba(255, 255, 255, 0.7);
    font-size: 13px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: rgba(0, 0, 0, 0.4);
    padding: 4px 12px;
    border-radius: 100px;
}

body.spig-lb-open { overflow: hidden; }


/* =============================================================
   6. Create / Edit modal — image zone strip
   Fixed-height horizontal thumbnail strip injected between the
   Jodit editor and the hashtag picker / modal footer.
   ============================================================= */

.spig-modal-zone {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    gap: 6px;
    padding: 0 16px;
    overflow-x: auto;
    overflow-y: hidden;
    min-height: 0;
    max-height: 0;
    border-top: 0px solid #e8e8e8;
    border-bottom: 0px solid #e8e8e8;
    background: #fafafa;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    scrollbar-color: #ccc transparent;
    /* Smooth height expansion — prevents layout jump */
    transition: min-height 0.2s ease, max-height 0.2s ease,
                padding 0.2s ease, border-width 0.2s ease;
}

.spig-modal-zone.spig-zone-active {
    min-height: 84px;
    max-height: 84px;
    padding: 8px 16px;
    border-top-width: 1px;
    border-bottom-width: 1px;
}

.spig-modal-zone::-webkit-scrollbar       { height: 4px; }
.spig-modal-zone::-webkit-scrollbar-track { background: transparent; }
.spig-modal-zone::-webkit-scrollbar-thumb { background: #ccc; border-radius: 2px; }

/* Individual thumbnail */
.spig-zone-thumb {
    flex: 0 0 auto;
    position: relative;
    width: 60px;
    height: 60px;
    border-radius: 6px;
    overflow: hidden;
    background: #1a1a1a;
    cursor: default;
}

.spig-zone-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    pointer-events: none;
}

/* Remove (×) button on each thumbnail */
.spig-zone-remove {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    border: none;
    background: rgba(0, 0, 0, 0.65);
    color: #fff;
    font-size: 12px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    z-index: 2;
    transition: background 0.15s;
}

.spig-zone-remove:hover { background: rgba(0, 0, 0, 0.9); }

/* Cap warning banner above the hashtag picker */
.spig-cap-warning {
    font-size: 12px;
    color: #b45309;
    background: #fef3c7;
    border: 1px solid #fcd34d;
    border-radius: 0;
    padding: 5px 16px;
    text-align: center;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}


/* =============================================================
   7. Jodit editor — image visibility and text jump prevention
   ============================================================= */

/* Hide all uploaded images inside the Jodit WYSIWYG editor.
   Images are invisible to the user during creation/editing but
   remain in the DOM so Jodit syncs them to the hidden textarea
   and SureDash saves them with the post content.
   The zone strip (section 6 above) provides the image management UI. */
#portal-post-creation-modal .jodit-wysiwyg img,
#portal-thread-edit-modal .jodit-wysiwyg img {
    display: none !important;
}

/* When the zone strip is active, add bottom padding to the Jodit
   editor so that text content does not jump when the strip appears.
   .spig-has-zone is added to .portal-modal-body by JS. */
.spig-has-zone .jodit .jodit-workplace .jodit-wysiwyg,
.spig-has-zone .jodit .jodit-workplace .jodit-wysiwyg_iframe,
.spig-has-zone .jodit-container .jodit-workplace .jodit-wysiwyg,
.spig-has-zone .jodit-container .jodit-workplace .jodit-wysiwyg_iframe {
    padding-bottom: 90px !important;
}
