/* ==========================================================================
   TABLE MASTER STYLESHEET
   Single source of truth for all table styling
   ========================================================================== */

/* --------------------------------------------------------------------------
   1. CSS VARIABLES - Single source of truth for all dimensions
   -------------------------------------------------------------------------- */
:root {
    /* Column widths - Desktop (≥768px) */
    --col-product-width: 256px;
    --col-price100-width: 88px;
    --col-size-width: 72px;
    --col-stock-width: 112px;
    --col-price-width: 100px;
    --col-type-width: 96px;
    --col-alcohol-width: 80px;
    --col-sweetness-width: 72px;

    /* Column widths - Mobile (<768px) */
    --col-product-width-mobile: 130px;
    --col-price100-width-mobile: 72px;
    --col-size-width-mobile: 60px;
    --col-stock-width-mobile: 84px;
    --col-price-width-mobile: 80px;
    --col-type-width-mobile: 80px;
    --col-alcohol-width-mobile: 64px;
    --col-sweetness-width-mobile: 64px;

    /* Mobile Landscape (480px - 767px) - Fit 8 columns */
    @media (min-width: 480px) and (max-width: 767px) {
        :root {
            --col-price-width-mobile: 70px;
            --col-type-width-mobile: 70px;
            --col-alcohol-width-mobile: 50px;
            --col-sweetness-width-mobile: 80px;
        }
    }

    /* Typography */
    --font-header: 0.9rem;
    --font-cell: 0.85rem;
    --font-weight-header: 600;

    /* Colors */
    --color-header-bg: #f9fafb;
    --color-header-text: #1f2937;
    --color-header-border: #e5e7eb;
    --color-sorted-bg: #eff6ff;
    --color-sorted-text: #1e40af;
}

/* --------------------------------------------------------------------------
   2. TABLE CONTAINER - Horizontal scroll wrapper
   -------------------------------------------------------------------------- */
.table-container {
    width: 100%;
    overflow-x: auto;
    overflow-y: visible;
    -webkit-overflow-scrolling: touch;
    position: relative;
}

/* Prevent scroll jumps while loading */
html.no-initial-scroll .table-container {
    overflow-x: hidden !important;
}

/* --------------------------------------------------------------------------
   3. BASE TABLE STRUCTURE
   -------------------------------------------------------------------------- */
table.col-locked-table {
    table-layout: fixed !important;
    width: 100% !important;
    border-collapse: collapse;
    background: #ffffff;
}

table.col-locked-table thead {
    position: sticky;
    top: 0;
    z-index: 10;
    background: var(--color-header-bg);
    width: 100% !important;
    /* Ensure thead covers full width on mobile */
}

/* Ensure thead extends to right edge on mobile */
@media (max-width: 768px) {

    /* Fill white space on right side of header - use container background */
    .table-container {
        background: linear-gradient(to bottom,
                var(--color-header-bg) 0,
                var(--color-header-bg) 2.5rem,
                #ffffff 2.5rem,
                #ffffff 100%) !important;
        position: relative !important;
    }

    /* Add border that extends full width - positioned at header bottom */
    /* Use a data attribute or class to avoid conflict with scroll hints */
    .table-container::after {
        content: '';
        position: absolute;
        top: 2.5rem;
        left: 0;
        right: 0;
        height: 2px;
        background: var(--color-header-border);
        pointer-events: none;
        z-index: 3;
    }

    /* Override scroll hint ::after on mobile to show border instead */
    .table-container::after {
        opacity: 1 !important;
        background: var(--color-header-border) !important;
        width: 100% !important;
        bottom: auto !important;
        top: 2.5rem !important;
        height: 2px !important;
    }

    table.col-locked-table {
        width: auto !important;
        /* Let table size to its content */
        min-width: 100% !important;
        /* But ensure it's at least container width */
        background: transparent !important;
        /* Transparent so container background shows */
        position: relative;
        z-index: 1;
    }

    table.col-locked-table thead {
        width: 100% !important;
        background: var(--color-header-bg) !important;
        position: relative;
        z-index: 2;
    }

    table.col-locked-table thead tr {
        width: 100% !important;
        background: var(--color-header-bg) !important;
        display: table-row !important;
    }

    /* Ensure all header cells have grey background */
    table.col-locked-table thead th {
        background: var(--color-header-bg) !important;
    }

    /* Remove border from header cells since container::before provides it */
    table.col-locked-table thead th {
        border-bottom: none !important;
    }

    /* Ensure tbody has white background */
    table.col-locked-table tbody {
        background: #ffffff !important;
    }
}

/* --------------------------------------------------------------------------
   4. COLUMN DEFINITIONS - Via colgroup (proper way)
   -------------------------------------------------------------------------- */

/* Mobile widths */
table.col-locked-table colgroup col:nth-child(1) {
    width: var(--col-product-width-mobile);
}

table.col-locked-table colgroup col:nth-child(2) {
    width: var(--col-price100-width-mobile);
}

table.col-locked-table colgroup col:nth-child(3) {
    width: var(--col-size-width-mobile);
}

table.col-locked-table colgroup col:nth-child(4) {
    width: var(--col-stock-width-mobile);
}

table.col-locked-table colgroup col:nth-child(5) {
    width: var(--col-price-width-mobile);
}

table.col-locked-table colgroup col:nth-child(6) {
    width: var(--col-type-width-mobile);
}

table.col-locked-table colgroup col:nth-child(7) {
    width: var(--col-alcohol-width-mobile);
}

table.col-locked-table colgroup col:nth-child(8) {
    width: var(--col-sweetness-width-mobile);
}

/* Desktop widths */
@media (min-width: 768px) {
    table.col-locked-table colgroup col:nth-child(1) {
        width: var(--col-product-width);
    }

    table.col-locked-table colgroup col:nth-child(2) {
        width: var(--col-price100-width);
    }

    table.col-locked-table colgroup col:nth-child(3) {
        width: var(--col-size-width);
    }

    table.col-locked-table colgroup col:nth-child(4) {
        width: var(--col-stock-width);
    }

    table.col-locked-table colgroup col:nth-child(5) {
        width: var(--col-price-width);
    }

    table.col-locked-table colgroup col:nth-child(6) {
        width: var(--col-type-width);
    }

    table.col-locked-table colgroup col:nth-child(7) {
        width: var(--col-alcohol-width);
    }

    table.col-locked-table colgroup col:nth-child(8) {
        width: var(--col-sweetness-width);
    }
}

/* --------------------------------------------------------------------------
   5. HEADER STYLING - Uniform across all columns
   -------------------------------------------------------------------------- */
table.col-locked-table th {
    font-size: var(--font-header);
    font-weight: var(--font-weight-header);
    color: var(--color-header-text);
    background-color: var(--color-header-bg);
    border-bottom: 2px solid var(--color-header-border);
    padding: 0.5rem 0.4rem;
    text-align: center;
    vertical-align: middle;
    white-space: nowrap;
    text-transform: none;
    letter-spacing: normal;
    line-height: 1.4;
}

/* First column (Product) - left aligned */
table.col-locked-table th:nth-child(1) {
    text-align: left;
}

/* Specifically for the combined Price header - ensure it fits */
table.col-locked-table th:nth-child(2) {
    height: auto !important;
    min-height: 3.5rem !important; /* Ensure enough height for 2 lines */
    overflow: visible !important;
    white-space: normal !important;
    padding-top: 0.25rem !important;
    padding-bottom: 0.25rem !important;
    vertical-align: middle !important;
}

/* Sorted column - blue text to indicate selection */
table.col-locked-table th.sorted-asc,
table.col-locked-table th.sorted-desc {
    background-color: var(--color-header-bg);
    color: #1e40af !important; /* blue-800 */
    font-weight: var(--font-weight-header);
}

/* Dark mode support for sorted column - same blue as light mode */
@media (prefers-color-scheme: dark) {
    table.col-locked-table th.sorted-asc,
    table.col-locked-table th.sorted-desc {
        background-color: var(--color-header-bg);
        color: #1e40af !important; /* blue-800 - same as light mode */
    }
}

/* Price/100mL header - uniform styling */
.header-price {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    gap: 0.1rem;
    line-height: 1.2;
}

.price-header__action,
.price-header__action--primary,
.price-header__action--secondary {
    background: none;
    border: none;
    padding: 0;
    margin: 0;
    font-size: var(--font-header);
    font-weight: var(--font-weight-header);
    color: inherit;
    cursor: pointer;
    text-transform: none;
    letter-spacing: normal;
    opacity: 1;
}

/* Sorted Price/100mL - blue color */
th.sorted-asc .price-header__action,
th.sorted-desc .price-header__action {
    font-weight: var(--font-weight-header);
    color: #1e40af !important; /* blue-800 */
}

/* Dark mode support for sorted Price/100mL - same blue as light mode */
@media (prefers-color-scheme: dark) {
    th.sorted-asc .price-header__action,
    th.sorted-desc .price-header__action {
        color: #1e40af !important; /* blue-800 - same as light mode */
    }
}

/* --------------------------------------------------------------------------
   6. CELL STYLING
   -------------------------------------------------------------------------- */
table.col-locked-table td {
    padding: 0.5rem 0.4rem;
    border-bottom: 1px solid var(--color-header-border);
    font-size: var(--font-cell);
    text-align: center;
    vertical-align: middle;
    line-height: 1.4;
}

/* First column (Product) - left aligned */
table.col-locked-table td:nth-child(1) {
    text-align: left;
}

/* Skeleton rows */
table.col-locked-table .table-skeleton td {
    background: #fff;
    color: transparent;
    pointer-events: none;
    /* Match real row dimensions exactly to prevent layout shift */
    padding: 0.5rem 0.4rem !important;
    font-size: 0.85rem !important;
    line-height: 1.4 !important;
    vertical-align: middle !important;
    border-bottom: 1px solid #e5e7eb !important;
}

/* --------------------------------------------------------------------------
   7. RESPONSIVE BEHAVIOR
   -------------------------------------------------------------------------- */

/* Mobile: Ensure horizontal scroll works */
@media (max-width: 767px) {
    .table-container {
        overflow-x: auto;
    }

    /* Product column narrower on mobile */
    table.col-locked-table th:nth-child(1),
    table.col-locked-table td:nth-child(1) {
        min-width: var(--col-product-width-mobile);
        max-width: var(--col-product-width-mobile);
    }
}

/* Desktop: Allow more generous widths */
@media (min-width: 768px) {

    table.col-locked-table th:nth-child(1),
    table.col-locked-table td:nth-child(1) {
        min-width: var(--col-product-width);
        max-width: var(--col-product-width);
    }
}

/* --------------------------------------------------------------------------
   8. PERFORMANCE OPTIMIZATIONS
   -------------------------------------------------------------------------- */

/* GPU acceleration for smooth scrolling */
table.col-locked-table {
    transform: translateZ(0);
    will-change: scroll-position;
}

/* Prevent font snapping during load */
html.no-initial-scroll table.col-locked-table,
html.no-initial-scroll table.col-locked-table th,
html.no-initial-scroll table.col-locked-table td {
    -webkit-font-smoothing: subpixel-antialiased;
    backface-visibility: hidden;
}

/* Progressive Column Visibility */
.table-container.hide-col-5 .col-5 {
    display: none !important;
}

.table-container.hide-col-6 .col-6 {
    display: none !important;
}

.table-container.hide-col-7 .col-7 {
    display: none !important;
}

.table-container.hide-col-8 .col-8 {
    display: none !important;
}

/* Prevent mid-word breaks globally in table */
.table-container td,
.table-container th {
    word-break: normal !important;
    overflow-wrap: normal !important;
}

/* --------------------------------------------------------------------------
   9. VISUAL POLISH (User Requests)
   -------------------------------------------------------------------------- */

/* Global link styling - no underline, inherit color by default */
/* Exception: availability-store-link and product links should be blue */
a:not(.availability-store-link):not(.product-name a) {
    text-decoration: none;
    color: inherit;
}

/* Product Name Links - Blue */
.product-name a,
.product-name a:visited {
    color: #2563eb !important;
    text-decoration: none;
    font-weight: 600;
}

.product-name a:hover {
    text-decoration: underline;
}

/* 2. Stock Count Green */
.availability-stock {
    color: #16a34a;
    /* green-600 */
    font-weight: 600;
}

/* 3. Price Column Formatting (Regular vs Sale) */
.price-combo__total {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    line-height: 1.2;
}

.price-regular {
    text-decoration: line-through;
    color: #6b7280;
    /* gray-500 */
    font-size: 0.75rem;
}

.price-current--sale {
    color: #b91c1c !important;
    /* red-700 - Matches sale badge and price/100mL */
    font-weight: 700;
}

/* 4. Prevent double scrollbars */
body {
    overflow-x: hidden;
    /* Prevent body horizontal scroll */
}

.table-container {
    overflow-y: hidden;
    /* Prevent container vertical scroll */
}

/* 8. Availability store links - blue color */
.availability-store-link,
.availability-store-link:visited,
.availability-store-link:hover,
.availability-store-link:active,
.check-inventory-link,
.check-inventory-link:visited,
.check-inventory-link:hover,
.check-inventory-link:active {
    color: #2563eb !important;
    text-decoration: none !important;
}