/**
 * H3X Browse by Category / Browse by Brand — shared frontend + editor-preview
 * styles. The tile appearance is driven entirely by CSS custom properties set
 * inline on the .h3x-browse container by the render callback / editor.
 */

.h3x-browse {
    display: grid;
    grid-template-columns: repeat(var(--h3x-cols, 4), minmax(0, 1fr));
    gap: var(--h3x-gap, 1rem);
}

/* Strip layout — tiles take a fixed width, never wrap, and are allowed to
 * overflow. Intended for use inside a Sliding Row (or any scroll container),
 * which owns the overflow-x and gives this block `flex: 0 0 auto` so it keeps
 * its intrinsic (overflowing) width. Standalone, it will simply overflow its
 * parent, so only pick this mode when an ancestor can scroll. */
.h3x-browse.is-strip {
    display: flex;
    flex-wrap: nowrap;
    align-items: stretch;
}

.h3x-browse.is-strip .h3x-browse__item {
    flex: 0 0 var(--h3x-tile-width, 220px);
    width: var(--h3x-tile-width, 220px);
}

.h3x-browse__item {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    aspect-ratio: var(--h3x-aspect, 1 / 1);
    overflow: hidden;
    text-decoration: none;
    background-color: #2b2b2b;
    border-radius: 4px;
    isolation: isolate;
}

.h3x-browse__img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    z-index: 0;
}

.h3x-browse__overlay {
    position: absolute;
    inset: 0;
    background: var(--h3x-overlay, rgba(51, 51, 51, 0.5));
    z-index: 1;
    transition: background 0.2s ease;
}

.h3x-browse__label {
    position: relative;
    z-index: 2;
    color: var(--h3x-text, #ffffff);
    font-size: var(--h3x-text-size, 1.25rem);
    font-weight: 600;
    line-height: 1.2;
    text-align: center;
    padding: 0.5em 0.75em;
    text-wrap: balance;
}

/* Subtle affordance that the whole tile is a link. */
.h3x-browse__item:hover .h3x-browse__img,
.h3x-browse__item:focus-visible .h3x-browse__img {
    transform: scale(1.04);
}

.h3x-browse__img {
    transition: transform 0.35s ease;
}

.h3x-browse__item:focus-visible {
    outline: 2px solid currentColor;
    outline-offset: 2px;
}

/* ---- Random / sliding product-image fallback -------------------------------
 * When a term has no image of its own, the render can drop in product images
 * from that term. One image = a plain static tile (nothing below applies).
 * Several images = this crossfade, which is pure CSS — no JS, no timers.
 *
 * Each frame runs the same keyframes over a full cycle (--h3x-slide-cycle) but
 * is offset by one slot (cycle / N) via an inline animation-delay, so exactly
 * one frame is visible at a time. The keyframes have to be written per count,
 * because the "visible" window is a percentage of the cycle (100 / N) and CSS
 * can't calc() a keyframe selector.
 *
 * Frames sit at opacity 0 by default (and fill-mode stays `none`), so a frame
 * that hasn't started yet during its initial delay is simply invisible. */

.h3x-browse__item.has-slideshow .h3x-browse__img {
    opacity: 0;
    animation-duration: var(--h3x-slide-cycle, 12s);
    animation-iteration-count: infinite;
    animation-timing-function: ease-in-out;
}

.h3x-browse__item.is-slides-2 .h3x-browse__img { animation-name: h3x-bbt-slides-2; }
.h3x-browse__item.is-slides-3 .h3x-browse__img { animation-name: h3x-bbt-slides-3; }
.h3x-browse__item.is-slides-4 .h3x-browse__img { animation-name: h3x-bbt-slides-4; }
.h3x-browse__item.is-slides-5 .h3x-browse__img { animation-name: h3x-bbt-slides-5; }
.h3x-browse__item.is-slides-6 .h3x-browse__img { animation-name: h3x-bbt-slides-6; }

@keyframes h3x-bbt-slides-2 {
    0%   { opacity: 0; }
    3%   { opacity: 1; }
    47%  { opacity: 1; }
    50%  { opacity: 0; }
    100% { opacity: 0; }
}
@keyframes h3x-bbt-slides-3 {
    0%     { opacity: 0; }
    3%     { opacity: 1; }
    30.33% { opacity: 1; }
    33.33% { opacity: 0; }
    100%   { opacity: 0; }
}
@keyframes h3x-bbt-slides-4 {
    0%   { opacity: 0; }
    3%   { opacity: 1; }
    22%  { opacity: 1; }
    25%  { opacity: 0; }
    100% { opacity: 0; }
}
@keyframes h3x-bbt-slides-5 {
    0%   { opacity: 0; }
    3%   { opacity: 1; }
    17%  { opacity: 1; }
    20%  { opacity: 0; }
    100% { opacity: 0; }
}
@keyframes h3x-bbt-slides-6 {
    0%      { opacity: 0; }
    3%      { opacity: 1; }
    13.66%  { opacity: 1; }
    16.66%  { opacity: 0; }
    100%    { opacity: 0; }
}

/* Respect reduced-motion: freeze on the first frame rather than crossfading. */
@media (prefers-reduced-motion: reduce) {
    .h3x-browse__item.has-slideshow .h3x-browse__img {
        animation: none;
        opacity: 0;
    }
    .h3x-browse__item.has-slideshow .h3x-browse__img:first-child {
        opacity: 1;
    }
}

/* The zoom-on-hover affordance is for a single static image; a crossfading
   stack would fight it. */
.h3x-browse__item.has-slideshow:hover .h3x-browse__img {
    transform: none;
}

/* Mobile column count applies to the grid layout only — a strip must keep its
   single non-wrapping line at every width. */
@media (max-width: 600px) {
    .h3x-browse:not(.is-strip) {
        grid-template-columns: repeat(var(--h3x-cols-mobile, 2), minmax(0, 1fr));
    }
}
