/* "Best Sellers" block (plp/best-sellers). The cards themselves are the
   catalog's (.plp-product-card, styled by the theme's plp-product-card.css);
   this only lays them out.

   Per breakpoint the block sets two numbers (see PLP_Best_Sellers::render):
   --plp-bs-full-*  columns of the full grid - fixes the card width, so 1-3
                    cards are exactly as wide as in a full row;
   --plp-bs-cols-*  columns actually used = min(full, products found).
   The used tracks are centred, so a short list sits in the middle.
   On desktop a card is also capped at the catalog card width (below).
   Breakpoints: desktop / tablet <=1024px / mobile <=768px / narrow <=420px.
   Columns per breakpoint come from the block settings (automatic: up to 3
   on tablets, 2 on phones, 1 on narrow phones). */
/* Desktop: never wider than a catalog card. The catalog grid is the Kadence
   content width (capped by the viewport) minus its filter sidebar and
   paddings (376px), 4 columns with 15px gaps - 217px on a 1290px content
   width, 237px on 1370px. In a full-width homepage row the cards would
   otherwise grow to 450px+. */
.plp-best-sellers {
    --plp-bs-gap: 15px;
    --plp-bs-card-max: calc((min(100vw, var(--global-content-width, 1290px)) - 376px - 3 * 15px) / 4);
    --plp-bs-full: var(--plp-bs-full-d, 4);
    --plp-bs-cols: var(--plp-bs-cols-d, 4);
    margin-left: 0;
    margin-right: 0;
}

/* One column's width (the grid columns and the carousel slides use it). */
.plp-best-sellers {
    --plp-bs-col-w: min(
        calc((100% - (var(--plp-bs-full) - 1) * var(--plp-bs-gap)) / var(--plp-bs-full)),
        var(--plp-bs-card-max)
    );
}

/* ---------- Title row and "View all" --------------------------------------
   Sizes, weight, colours etc. set in the block come as a <style> scoped to
   the block id (PLP_Best_Sellers_Header); these are only the defaults. */
.plp-best-sellers__head {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 12px 24px;
    margin: 0 0 24px;
}

.plp-best-sellers__title {
    flex: 1 1 auto;
    margin: 0;
}

.plp-best-sellers__head.is-align-center .plp-best-sellers__title {
    text-align: center;
}

.plp-best-sellers__head.is-align-right .plp-best-sellers__title {
    text-align: right;
}

/* Centred title with the button in its row: the title stays in the middle,
   the button goes to the right edge. */
.plp-best-sellers__head.is-align-center.has-view-all {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
}

.plp-best-sellers__head.is-align-center.has-view-all .plp-best-sellers__title {
    grid-column: 2;
}

.plp-best-sellers__head.is-align-center.has-view-all .plp-best-sellers__view-all {
    grid-column: 3;
    justify-self: end;
}

/* With "View all" the title row and the button rows are as wide as the row
   of cards (which is capped and centred), so the button lines up with the
   cards' edge instead of the page's. A title without the button is left
   where it always was. */
.plp-best-sellers__head.has-view-all,
.plp-best-sellers > .plp-best-sellers__view-all {
    max-width: calc(var(--plp-bs-cols) * var(--plp-bs-col-w) + (var(--plp-bs-cols) - 1) * var(--plp-bs-gap));
    margin-left: auto;
    margin-right: auto;
}

/* Right-aligned title: the button takes the left edge. */
.plp-best-sellers__head.is-align-right.has-view-all {
    flex-direction: row-reverse;
}

.plp-best-sellers.has-title-font .plp-best-sellers__title {
    font-family: var(--rv-bs-title-font);
}

.plp-best-sellers__view-all {
    display: flex;
    justify-content: center;
}

.plp-best-sellers__view-all.is-align-left {
    justify-content: flex-start;
}

.plp-best-sellers__view-all.is-align-right {
    justify-content: flex-end;
}

.plp-best-sellers__view-all--title {
    flex: 0 0 auto;
}

.plp-best-sellers__view-all--below_title {
    margin: 0 0 24px;
}

.plp-best-sellers__view-all--bottom {
    margin-top: 24px;
}

.plp-best-sellers__view-all.is-mobile {
    display: none;
}

.plp-best-sellers__view-all-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    white-space: nowrap;
    cursor: pointer;
}

.plp-best-sellers.has-view-all-font .plp-best-sellers__view-all-link {
    font-family: var(--rv-bs-view-all-font);
}

.plp-best-sellers__view-all-link.is-style-outline {
    padding: 10px 24px;
    border: 1px solid currentColor;
    border-radius: var(--rv-btn-radius, 6px);
    color: var(--rv-text, #222);
    background: transparent;
    transition: opacity .15s ease;
}

.plp-best-sellers__view-all-link.is-style-outline:hover,
.plp-best-sellers__view-all-link.is-style-outline:focus {
    color: var(--rv-text, #222);
    opacity: .75;
}

.plp-best-sellers__view-all-link.is-style-link {
    padding: 0;
    color: var(--rv-text, #222);
    text-decoration: underline;
    text-underline-offset: 4px;
}

.plp-best-sellers__view-all-link.is-style-link:hover,
.plp-best-sellers__view-all-link.is-style-link:focus {
    text-decoration-thickness: 2px;
}

@media screen and (max-width: 768px) {
    .plp-best-sellers__view-all.is-desktop {
        display: none;
    }

    .plp-best-sellers__view-all.is-mobile {
        display: flex;
    }

    /* On phones a centred title takes its own row, the button the next. */
    .plp-best-sellers__head.is-align-center.has-view-all {
        display: flex;
        flex-direction: column;
    }

    .plp-best-sellers__head.is-align-center.has-view-all .plp-best-sellers__view-all {
        justify-self: auto;
    }
}

.plp-best-sellers ul.plp-best-sellers__grid {
    display: grid;
    grid-template-columns: repeat(
        var(--plp-bs-cols),
        min(
            calc((100% - (var(--plp-bs-full) - 1) * var(--plp-bs-gap)) / var(--plp-bs-full)),
            var(--plp-bs-card-max)
        )
    );
    justify-content: center;
    gap: 30px var(--plp-bs-gap);
    list-style: none;
    margin: 0;
    padding: 0;
}

/* WooCommerce clears its float grid with ::before/::after on ul.products -
   in a CSS grid those would become empty grid cells. */
.plp-best-sellers ul.plp-best-sellers__grid::before,
.plp-best-sellers ul.plp-best-sellers__grid::after {
    display: none;
}

/* WooCommerce / Kadence float-grid sizing for li.product doesn't apply here. */
.plp-best-sellers ul.plp-best-sellers__grid > li.plp-product-card {
    float: none;
    width: auto;
    max-width: none;
    margin: 0;
    clear: none;
}

@media screen and (max-width: 1024px) {
    /* No sidebar in the catalog here either - cards just fill the columns. */
    .plp-best-sellers {
        --plp-bs-card-max: 100%;
        --plp-bs-full: var(--plp-bs-full-t, 3);
        --plp-bs-cols: var(--plp-bs-cols-t, 3);
    }
}

@media screen and (max-width: 768px) {
    .plp-best-sellers {
        --plp-bs-full: var(--plp-bs-full-m, 2);
        --plp-bs-cols: var(--plp-bs-cols-m, 2);
    }
}

@media screen and (max-width: 420px) {
    .plp-best-sellers {
        --plp-bs-full: var(--plp-bs-full-n, 1);
        --plp-bs-cols: var(--plp-bs-cols-n, 1);
    }
}

/* ---------- Carousel layout (default) -------------------------------------
   Same card width as the grid: the Swiper frame is made exactly as wide as
   the columns in use (each min(full-grid column, catalog card)) and centred,
   so slidesPerView gives the very same card size. */

.plp-best-sellers--carousel .plp-best-sellers__frame {
    position: relative;
    max-width: calc(var(--plp-bs-cols) * var(--plp-bs-col-w) + (var(--plp-bs-cols) - 1) * var(--plp-bs-gap));
    margin: 0 auto;
    overflow: hidden;
}

/* Before Swiper starts (or if it's missing): one row of cards at the same
   width, scrollable sideways - no jump from a wrapped grid to the slider. */
.plp-best-sellers--carousel ul.plp-best-sellers__grid {
    grid-template-columns: none;
    grid-auto-flow: column;
    grid-auto-columns: calc((100% - (var(--plp-bs-cols) - 1) * var(--plp-bs-gap)) / var(--plp-bs-cols));
    justify-content: start;
    overflow-x: auto;
    scroll-snap-type: x mandatory;
}

.plp-best-sellers--carousel ul.plp-best-sellers__grid > li.plp-product-card {
    scroll-snap-align: start;
}

/* Swiper running: its flex track, and spaceBetween instead of the grid gap. */
.plp-best-sellers--carousel.is-ready ul.plp-best-sellers__grid.swiper-wrapper {
    display: flex;
    gap: 0;
    overflow: visible;
    scroll-snap-type: none;
    align-items: stretch;
}

.plp-best-sellers--carousel.is-ready ul.plp-best-sellers__grid > li.swiper-slide {
    flex-shrink: 0;
    box-sizing: border-box;
    height: auto;
}

/* Arrows in their own row under the slider (as in the related-products
   slider). The margin sits on the buttons, so when Swiper locks them
   (everything fits) the row collapses. */
.plp-best-sellers__controls {
    display: flex;
    justify-content: center;
    gap: 12px;
}

.plp-best-sellers--carousel:not(.is-ready) .plp-best-sellers__controls {
    display: none;
}

.plp-best-sellers__nav {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    margin-top: 24px;
    padding: 0;
    border: 1px solid var(--rv-border, #e5e5e5);
    border-radius: 50%;
    background: #fff;
    cursor: pointer;
    transition: border-color .15s ease, opacity .15s ease;
}

.plp-best-sellers__nav:hover,
.plp-best-sellers__nav:focus,
.plp-best-sellers__nav:focus-visible,
.plp-best-sellers__nav:active {
    border-color: var(--rv-accent, #222);
    background: #fff;
}

.plp-best-sellers__nav.swiper-button-lock {
    display: none;
}

.plp-best-sellers__nav.swiper-button-disabled {
    opacity: .35;
    pointer-events: none;
}

.plp-best-sellers__arrow {
    width: 9px;
    height: 9px;
    border-right: 2px solid var(--rv-text, #222);
    border-bottom: 2px solid var(--rv-text, #222);
}

.plp-best-sellers__nav--prev .plp-best-sellers__arrow {
    transform: translateX(2px) rotate(135deg);
}

.plp-best-sellers__nav--next .plp-best-sellers__arrow {
    transform: translateX(-2px) rotate(-45deg);
}

.plp-best-sellers__empty {
    padding: 16px;
    border: 1px dashed #c3c4c7;
    color: #646970;
    font-size: 13px;
}
