/**
 * Compact product cards (v2), wherever the shared card renders: shop /
 * category / brand grids, homepage and PDP carousels, My Account
 * recommendations, the promotions page and the 404 page.
 *
 * Scoped to body.dds-cards-v2, which inc/functions/product-cards-v2.php adds
 * only while the redesign is enabled (admin preview first). Layout and spacing
 * only: markup, content and behaviour of the cards are unchanged. Carousel item
 * counts are fitted to the compact card in assets/js/product-cards-v2.js.
 *
 * Card anatomy (browser DOM, woocommerce/content-product.php):
 *   li.product.type-product
 *     .product-card-tags            absolute, top right of the image
 *     .yith-wcwl-add-to-wishlist    absolute, top left
 *     a.woocommerce-LoopProduct-link > img + img.hover-image
 *     .woocommerce-archive-title-box
 *       a.woocommerce-LoopProduct-link > h5 + span.price
 *       a.button
 *       div.quantity                (hidden inline on restricted cards - never set display on it here)
 */

body.dds-cards-v2 {
    --dds-card-gap: 16px;
    --dds-card-min: 170px;
    --dds-card-pad: 12px;
    --dds-card-title-size: 15px;
    --dds-card-title-lh: 1.35;
    --dds-card-price-size: 18px;
    --dds-card-qty-size: 13px;
    --dds-card-qty-btn: 22px;
    --dds-card-control-h: 34px;
    --dds-card-btn-h: 40px;
    --dds-card-btn-size: 15px;
}

/* ---- Shop / category / brand grid: max 4 per row (acceptance criteria) ---- */

body.dds-cards-v2 #shop-products ul.products.row {
    display: grid !important;
    grid-template-columns: repeat(auto-fill, minmax(max(var(--dds-card-min), calc((100% - 3 * var(--dds-card-gap)) / 4)), 1fr));
    gap: var(--dds-card-gap);
    align-items: stretch;
    margin: 8px 0 0;
    padding: 0 !important;
}

body.dds-cards-v2 #shop-products ul.products.row::before,
body.dds-cards-v2 #shop-products ul.products.row::after {
    display: none;
}

/* ---- Sale / promotions page: no column cap; every row, including a
        part-filled last row, fills from the centre out (a grid would
        left-align the last row) ---- */

body.dds-cards-v2 .single-offer-wrapper .product-list-inner.products {
    display: flex !important;
    /* !important: legacy mobile .woocommerce ul.products { flex-direction: column } */
    flex-direction: row !important;
    flex-wrap: wrap;
    justify-content: center !important;
    align-items: stretch;
    gap: var(--dds-card-gap) !important;
    /* The list is full-bleed; keep tiles off the viewport edge */
    padding: 0 24px;
    box-sizing: border-box;
}

/* Breathing room between the offer description, the tiles and the button */
body.dds-cards-v2 .single-offer-wrapper .product-list-inner.products {
    margin: 32px 0 40px !important;
}

/* Fixed tile width so tiles stay the same size in every row */
body.dds-cards-v2 .single-offer-wrapper .product-list-inner.products > li.product.type-product {
    flex: 0 0 220px;
}

/* WooCommerce's clearfix ::before would take the first grid cell and push the
   first row one column right of the rows below it. */
body.dds-cards-v2 .single-offer-wrapper .product-list-inner.products::before,
body.dds-cards-v2 .single-offer-wrapper .product-list-inner.products::after {
    display: none !important;
}

/* ---- Carousels: equal-height slides. Item count: product-cards-v2.js ---- */

body.dds-cards-v2 :is(#owl-products, #owl-products-best, #owl-products-newinstock, #owl-products-related, #owl-products-recent) .owl-stage {
    display: flex;
}

body.dds-cards-v2 :is(#owl-products, #owl-products-best, #owl-products-newinstock, #owl-products-related, #owl-products-recent) .owl-item {
    display: flex;
    flex-shrink: 0;
}

/* Arrows centred on the card image; product-cards-v2.js measures --dds-nav-top */
body.dds-cards-v2 :is(#owl-products, #owl-products-best, #owl-products-newinstock, #owl-products-related, #owl-products-recent) .owl-nav button {
    top: var(--dds-nav-top, 35%);
    transform: translateY(-50%);
}

/* Owl marks the nav disabled when every product fits; legacy CSS forces it on */
body.dds-cards-v2 :is(#owl-products, #owl-products-best, #owl-products-newinstock, #owl-products-related, #owl-products-recent) .owl-nav.disabled {
    display: none !important;
}

/* PDP Recently Viewed / You Might Also Like: the product content above sits in
   centred .container.col-lg-10 boxes, but this col-lg-10 has no .container, so
   it hugged the left edge. Centre it the same way and line the heading and
   cards up with the edge of the boxes above. */
@media (min-width: 673px) {
    body.dds-cards-v2 div.product > .col-lg-10:has(:is(#owl-products-related, #owl-products-recent)) {
        margin-left: auto !important;
        margin-right: auto !important;
        padding-left: 0 !important;
        padding-right: 0 !important;
        max-width: 1440px; /* same caps as .container in assets/css/style.css */
    }
}

@media (min-width: 673px) and (max-width: 1584px) {
    body.dds-cards-v2 div.product > .col-lg-10:has(:is(#owl-products-related, #owl-products-recent)) { max-width: 1280px; }
}

@media (min-width: 673px) and (max-width: 1312px) {
    body.dds-cards-v2 div.product > .col-lg-10:has(:is(#owl-products-related, #owl-products-recent)) { max-width: 1024px; }
}

/* Tablet: the boxes above go full width of the product row */
@media (min-width: 673px) and (max-width: 1056px) {
    body.dds-cards-v2 div.product > .col-lg-10:has(:is(#owl-products-related, #owl-products-recent)) {
        flex-basis: auto !important;
        width: 100% !important;
        max-width: none;
    }
}

/* New in Stock and My Account wrap each card in a .swiper-slide */
body.dds-cards-v2 :is(#owl-products, #owl-products-best, #owl-products-newinstock, #owl-products-related, #owl-products-recent) .owl-item > .swiper-slide {
    display: flex;
    width: 100%;
}

/* ---- Card ---- */

body.dds-cards-v2 li.product.type-product {
    position: relative;
    display: flex !important;
    flex-direction: column;
    flex: 1 1 0%;
    min-width: 0;
    width: auto !important;
    max-width: none !important;
    margin: 0 !important;
    /* !important: legacy .col-xs-12 / .owl-item .product { padding: 0 !important } */
    padding: var(--dds-card-pad) !important;
    float: none;
    clear: none;
    box-sizing: border-box;
    list-style: none;
    container: dds-card / inline-size;
}

/* Image */
body.dds-cards-v2 li.product.type-product > a.woocommerce-LoopProduct-link {
    display: block;
    position: relative;
    height: auto;
    aspect-ratio: 5 / 3;
    flex: 0 0 auto;
    border-radius: 8px;
    overflow: hidden;
}

body.dds-cards-v2 li.product.type-product > a.woocommerce-LoopProduct-link img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    object-fit: contain !important;
}

/* Tags and wishlist sit on the image */
body.dds-cards-v2 li.product.type-product .product-card-tags {
    top: calc(var(--dds-card-pad) + 6px);
    right: calc(var(--dds-card-pad) + 6px);
    padding-top: 0;
    gap: 4px;
    /* Stop short of the wishlist heart in the top-left corner (promo-tags.php
       sets calc(100% - 20px), which ran tags over it) */
    max-width: max(55%, calc(100% - 2 * var(--dds-card-pad) - 40px)) !important;
}

body.dds-cards-v2 li.product.type-product .product-card-tags span.outlet-tag,
body.dds-cards-v2 li.product.type-product .product-card-tags .onsale,
body.dds-cards-v2 li.product.type-product .product-card-tags .tag-limited-stock,
body.dds-cards-v2 li.product.type-product .product-card-tags .dds-promo-tag {
    font-size: 10px;
    line-height: 1.2;
    padding: 3px 8px;
    letter-spacing: 0.02em;
}

/* Campaign labels carry the coupon code, so on narrow tiles wrap onto a second
   line instead of cutting it off (promo-tags.php sets nowrap + ellipsis).
   Labels longer than two lines still end in an ellipsis. */
body.dds-cards-v2 li.product.type-product .product-card-tags .dds-promo-tag {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    white-space: normal;
    overflow-wrap: anywhere;
    text-align: center;
    border-radius: 8px;
}

/* ---- Body: title / qty + price / button ---- */

body.dds-cards-v2 li.product.type-product .woocommerce-archive-title-box {
    flex: 1 1 auto;
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    grid-template-rows: auto auto 1fr;
    grid-template-areas:
        "title title"
        "qty   price"
        "btn   btn";
    align-items: center;
    column-gap: 0;
    min-height: 0;
    padding: 10px 0 0;
}

/* Inner link only wraps title + price; let them take part in the grid */
body.dds-cards-v2 li.product.type-product .woocommerce-archive-title-box > a.woocommerce-LoopProduct-link {
    display: contents;
}

/* Title: always two lines tall, clipped after two */
body.dds-cards-v2 li.product.type-product .woocommerce-archive-title-box h5.woocommerce-loop-product__title {
    grid-area: title;
    position: static;
    display: -webkit-box !important;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    overflow: hidden;
    font-size: var(--dds-card-title-size);
    line-height: var(--dds-card-title-lh);
    height: calc(2em * var(--dds-card-title-lh));
    min-height: 0 !important;
    margin: 0 0 10px !important;
    padding: 0;
    word-break: break-word;
}

/* Price */
body.dds-cards-v2 li.product.type-product .woocommerce-archive-title-box .price {
    grid-area: price;
    display: flex !important;
    flex-wrap: wrap;
    align-items: center;
    gap: 2px 6px;
    min-height: var(--dds-card-control-h);
    margin: 0 !important;
    padding: 0;
    font-size: var(--dds-card-price-size) !important;
    line-height: 1.2;
}

body.dds-cards-v2 li.product.type-product .woocommerce-archive-title-box .price * {
    display: inline;
}

body.dds-cards-v2 li.product.type-product .woocommerce-archive-title-box .price .woocommerce-Price-amount {
    font-size: var(--dds-card-price-size);
    line-height: 1.2;
    font-weight: 600;
}

body.dds-cards-v2 li.product.type-product .woocommerce-archive-title-box .price del {
    position: static;
    order: 2;
}

body.dds-cards-v2 li.product.type-product .woocommerce-archive-title-box .price del .woocommerce-Price-amount {
    font-size: 13px !important;
    font-weight: 400;
}

/* Quantity: compact pill next to the price */
body.dds-cards-v2 li.product.type-product .woocommerce-archive-title-box .quantity {
    grid-area: qty;
    box-sizing: border-box;
    width: auto;
    height: var(--dds-card-control-h);
    margin: 0 8px 0 0;
    padding: 0 4px;
    gap: 2px;
    border-radius: 8px;
}

body.dds-cards-v2 li.product.type-product .woocommerce-archive-title-box .quantity .qty-btn {
    flex: 0 0 auto;
    width: var(--dds-card-qty-btn);
    height: var(--dds-card-qty-btn);
    padding: 0;
    border: none;
    font-size: var(--dds-card-qty-size);
    line-height: 1;
    /* The -/+ glyphs sit below centre in Plus Jakarta Sans; draw the signs as
       centred bars instead and keep the text node for screen readers. */
    color: transparent !important;
    background-image: linear-gradient(#fff, #fff);
    background-position: center;
    background-size: 50% 2px;
    background-repeat: no-repeat;
}

body.dds-cards-v2 li.product.type-product .woocommerce-archive-title-box .quantity .qty-btn.plus {
    background-image: linear-gradient(#fff, #fff), linear-gradient(#fff, #fff);
    background-size: 50% 2px, 2px 50%;
}

/* Wide enough for a two-digit quantity at any card size */
body.dds-cards-v2 li.product.type-product .woocommerce-archive-title-box .quantity input {
    min-width: 0;
    width: calc(2ch + 6px);
    height: 100%;
    padding: 0;
    font-size: var(--dds-card-qty-size);
    background: transparent;
}

/* Button: full width, pinned to the bottom so all card types line up */
body.dds-cards-v2 li.product.type-product .woocommerce-archive-title-box > .button {
    grid-area: btn;
    align-self: end;
    box-sizing: border-box;
    width: 100%;
    white-space: nowrap;
    /* !important: legacy .add_to_cart_button { height: 56px !important; font-size: 16px !important } */
    height: var(--dds-card-btn-h) !important;
    font-size: var(--dds-card-btn-size) !important;
    margin: 12px 0 0 !important;
    padding: 0 10px;
    line-height: 1.2;
}

/* Cart icon only on a real Add To Cart. View Options (variable), Read More
   (restricted .contact_us_button, and non-purchasable products, which have no
   data-product_id) get none. */
body.dds-cards-v2 li.product.type-product .add_to_cart_button:is(.product_type_variable, .contact_us_button, :not(.ajax_add_to_cart), :not([data-product_id]))::after {
    display: none !important;
}

body.dds-cards-v2 li.product.type-product .woocommerce-archive-title-box > style {
    display: none;
}

/* ---- Mobile: two per row, tighter gutters ---- */

@media (max-width: 672px) {
    body.dds-cards-v2 {
        --dds-card-gap: 10px;
        --dds-card-pad: 8px;
    }

    body.dds-cards-v2 #shop-products ul.products.row {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        width: 100%;
    }

    body.dds-cards-v2 .single-offer-wrapper .product-list-inner.products {
        width: 100%;
        padding: 0 16px;
        margin: 24px 0 32px !important;
    }

    /* Two per row; an odd last tile sits centred */
    body.dds-cards-v2 .single-offer-wrapper .product-list-inner.products > li.product.type-product {
        flex: 0 0 calc((100% - var(--dds-card-gap)) / 2);
    }

    /* Smaller arrows over the outer edge of the two visible card images */
    body.dds-cards-v2 :is(#owl-products, #owl-products-best, #owl-products-newinstock, #owl-products-related, #owl-products-recent) .owl-nav button {
        width: 36px;
        height: 36px;
    }

    body.dds-cards-v2 :is(#owl-products, #owl-products-best, #owl-products-newinstock, #owl-products-related, #owl-products-recent) .owl-nav button::before {
        font-size: 24px;
    }
}

/* ---- Narrow cards: shrink controls by card width, not viewport width,
        because the sidebar / carousel changes how wide a card is ---- */

@container dds-card (max-width: 229px) {
    .woocommerce-archive-title-box {
        --dds-card-title-size: 14px;
        --dds-card-price-size: 16px;
        --dds-card-qty-size: 12px;
        --dds-card-qty-btn: 20px;
        --dds-card-control-h: 30px;
        --dds-card-btn-h: 36px;
        --dds-card-btn-size: 14px;
    }

    .woocommerce-archive-title-box .quantity {
        margin-right: 6px !important;
    }

    .woocommerce-archive-title-box > .button {
        padding: 0 8px !important;
    }
}

@container dds-card (max-width: 175px) {
    .woocommerce-archive-title-box {
        --dds-card-title-size: 13px;
        --dds-card-price-size: 14px;
        --dds-card-qty-btn: 18px;
        --dds-card-control-h: 28px;
        --dds-card-btn-h: 34px;
        --dds-card-btn-size: 13px;
    }

    .woocommerce-archive-title-box .quantity {
        margin-right: 5px !important;
        padding: 0 2px !important;
    }

    .woocommerce-archive-title-box > .button {
        margin-top: 8px !important;
    }
}

@container dds-card (max-width: 150px) {
    .woocommerce-archive-title-box {
        --dds-card-price-size: 13px;
        --dds-card-qty-size: 11px;
        --dds-card-qty-btn: 16px;
    }

    .woocommerce-archive-title-box .quantity {
        margin-right: 4px !important;
    }
}
