/* ==========================================================================
   Grid Card Design v5.0 (FINAL)
   Target: Absolute precision using Class Selectors + Corrected Indices.
   ========================================================================== */

/* CONTAINER & GRID */
body.view-grid-active .container {
    padding: 20px;
    max-width: 100%;
}

body.view-grid-active .table-container {
    display: block !important;
    width: 100% !important;
    overflow: visible !important;
}

body.view-grid-active #browseTable,
body.view-grid-active #searchResultsTable {
    display: block !important;
    width: 100% !important;
    border: none;
}

body.view-grid-active thead {
    display: none !important;
}

/* FLEXBOX LAYOUT REPLACEMENT */
body.view-grid-active tbody {
    display: flex !important;
    flex-wrap: wrap !important;
    justify-content: flex-start !important;
    /* Start aligned but handle gaps */
    gap: 16px;
    width: 100% !important;
    padding-bottom: 50px;
}


/* CARD BASE */
body.view-grid-active tr {
    display: flex !important;
    flex-direction: column;
    position: relative;
    background: #18181b;
    border: 1px solid #27272a;
    border-radius: 12px;
    overflow: hidden !important;
    height: 300px;
    transition: transform 0.2s, box-shadow 0.2s;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
    padding: 0 !important;
    margin: 0 !important;

    /* Flex Sizing Logic */
    flex-grow: 1 !important;
    flex-basis: 225px !important;
    /* Slightly increased base size */
    max-width: 100% !important;
    /* Moderate expansion limit */
    box-sizing: border-box !important;
}

/* 
   Calculated Breakpoints to prevent Column Count Drop 
   Equation: Width > (Count * Basis) + Gaps + Padding 
*/

/* At 950px, we can safely switch to 300px bases while keeping 3 columns */
@media (min-width: 950px) {
    body.view-grid-active tr {
        flex-basis: 300px !important;
    }
}

/* At 1550px, we can safely switch to 360px bases while keeping 4 columns */
@media (min-width: 1550px) {
    body.view-grid-active tr {
        flex-basis: 360px !important;
    }
}

/* At 2250px, we can safely switch to 420px bases while keeping 5 columns */
@media (min-width: 2250px) {
    body.view-grid-active tr {
        flex-basis: 420px !important;
    }
}

body.view-grid-active tr:hover {
    transform: translateY(-4px);
    box-shadow: 0 12px 30px rgba(0, 0, 0, 0.5);
    border-color: #3f3f46;
}

/* SHARED TD RESET */
body.view-grid-active td {
    display: block !important;
    padding: 0 !important;
    border: none !important;
    width: auto;
    position: static;
    box-sizing: border-box !important;
}

/* 0. HIDE INDEX COLUMN (#1) */
body.view-grid-active td:nth-child(1) {
    display: none !important;
}

/* 1. THUMBNAIL (Top - #2 / .thumbnail-cell) */
body.view-grid-active .thumbnail-cell,
body.view-grid-active td:nth-child(2) {
    height: 200px;
    background: #000;
    position: relative;
    order: 1;
    width: 100% !important;
}

body.view-grid-active .grid-preview-container,
body.view-grid-active .grid-preview-img {
    width: 100% !important;
    height: 100% !important;
    object-fit: cover !important;
}

/* 2. INDEX OVERLAY (Top Left) */
body.view-grid-active tr::after {
    content: attr(data-index);
    position: absolute;
    top: 12px;
    left: 12px;
    color: #fff;
    font-size: 16px;
    font-weight: 800;
    z-index: 150;
    pointer-events: none;
    text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.9);
}

/* 3. DATE OVERLAY (Top Right - .date-cell / #7) */
body.view-grid-active .date-cell,
body.view-grid-active td:nth-child(7) {
    position: absolute !important;
    top: 10px !important;
    right: 10px !important;
    z-index: 150 !important;
    background: rgba(0, 0, 0, 0.7) !important;
    color: #fff !important;
    padding: 3px 8px !important;
    border-radius: 4px;
    font-size: 12px !important;
    font-family: monospace;
    font-weight: 700;
}

/* 4. TITLE (Middle - .title-cell / #5) */
body.view-grid-active .title-cell,
body.view-grid-active td:nth-child(5) {
    order: 2;
    padding: 12px 14px 4px 14px !important;
    height: 60px;
    width: 100% !important;
    max-width: 100% !important;
    /* CRITICAL: Prevent title from pushing card width */
    overflow: hidden !important;
}

body.view-grid-active .grid-title-text {
    font-size: 15px;
    font-weight: 600;
    color: #fff;
    line-height: 1.4;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-align: left;
}

/* 5. FOOTER METADATA (Type #3, Res #4, Size #6) */

/* Type badge */
body.view-grid-active td:nth-child(3) {
    position: absolute !important;
    bottom: 10px !important;
    left: 14px !important;
    z-index: 160 !important;
}

/* Res badge */
body.view-grid-active td:nth-child(4) {
    position: absolute !important;
    bottom: 10px !important;
    left: 68px !important;
    z-index: 160 !important;
}

/* Size Text */
body.view-grid-active .size-cell,
body.view-grid-active td:nth-child(6) {
    position: absolute !important;
    bottom: 15px !important;
    left: 125px !important;
    z-index: 160 !important;
    color: #4ade80 !important;
    font-size: 13px !important;
    font-weight: 600;
    white-space: nowrap;
}

/* 6. OPEN BUTTON (Bottom Right - .actions-cell / #8) */
body.view-grid-active .actions-cell,
body.view-grid-active td:nth-child(8) {
    position: absolute !important;
    bottom: 12px !important;
    right: 12px !important;
    z-index: 160 !important;
}

body.view-grid-active .copy-btn {
    background: linear-gradient(135deg, #4f46e5, #4338ca) !important;
    border: none;
    border-radius: 6px;
    padding: 6px 14px;
    color: white !important;
    font-weight: 600;
    font-size: 13px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
}

body.view-grid-active .copy-btn::before {
    content: none !important;
}

/* BADGE STYLES */
body.view-grid-active .type-badge,
body.view-grid-active .res-badge {
    padding: 2px 0 !important;
    font-size: 11px !important;
    border-radius: 4px !important;
    color: white !important;
    font-weight: 700 !important;
    text-transform: uppercase !important;
    display: inline-block !important;
    line-height: 1 !important;
    min-width: 48px !important;
    text-align: center !important;
}

body.view-grid-active .type-badge {
    background: #3b82f6 !important;
}

body.view-grid-active .res-badge {
    background: #ef4444 !important;
}

/* MOBILE / NARROW CARD OPTIMIZATION */
@media (max-width: 768px) {

    /* Mobile Flex Sizing */
    body.view-grid-active tr {
        flex-basis: 195px !important;
        /* Allow smaller cards on mobile */
        max-width: 100% !important;
        /* Allow full width on very small screens */
        min-height: 320px;
        height: auto !important;
        padding-bottom: 50px !important;
    }

    body.view-grid-active .thumbnail-cell,
    body.view-grid-active td:nth-child(2) {
        height: 180px !important;
        width: 100% !important;
        margin: 0 !important;
    }

    /* Adjust for 2-column mode (cards ~180-250px) */
    body.view-grid-active .type-badge,
    body.view-grid-active .res-badge {
        font-size: 11px !important;
        padding: 3px 0 !important;
    }

    body.view-grid-active td:nth-child(4) {
        /* Res badge */
        left: 68px !important;
    }

    body.view-grid-active td:nth-child(6) {
        /* Size cell */
        left: 130px !important;
        font-size: 12px !important;
    }

    body.view-grid-active .copy-btn {
        padding: 4px 10px !important;
        font-size: 12px !important;
    }
}

/* Extra small width handling */
@media (max-width: 480px) {
    body.view-grid-active td:nth-child(6) {
        /* Hide size text if too narrow for badges + button */
        display: none !important;
    }
}