/* Apptura Badges: product badges on the cards and the product page gallery.
   Uses the theme tokens (apptura_mlayawkobaya main.css) with fallbacks, so it also works on the stock theme.
   Positions are logical (start / end), so they follow the page direction (RTL Arabic, LTR English):
   stored "top-left" = top-start (on the Arabic store: top right), as on the old store.
   O-142: every badge sits in its own corner, at the inset of the theme's own badges (cards 8px, product page 12px);
   on the product page the theme control of that corner (zoom, image counter) moves next to the badges. O-143: the
   card's wishlist heart is beside the add-to-cart button (theme), so nothing moves on cards. Layout: library
   Badges::injectInto(). The px numbers below match Badges::METRICS. */

/* ---------- Corner container (stock OpenCart card; theme column created when the theme printed none) ---------- */
.apptura-badges {
  position: absolute;
  z-index: var(--z-card-control, 2);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--sp-1, 4px);
  max-inline-size: calc(100% - 2 * var(--sp-2, 8px));
  pointer-events: none; /* the card's stretched name link stays clickable under the badges */
}
.apptura-badges--top-start { inset-block-start: var(--sp-2, 8px); inset-inline-start: var(--sp-2, 8px); }
.apptura-badges--top-end { inset-block-start: var(--sp-2, 8px); inset-inline-end: var(--sp-2, 8px); align-items: flex-end; }
.apptura-badges--bottom-start { inset-block-end: var(--sp-2, 8px); inset-inline-start: var(--sp-2, 8px); }
.apptura-badges--bottom-end { inset-block-end: var(--sp-2, 8px); inset-inline-end: var(--sp-2, 8px); align-items: flex-end; }

/* Badges joined to the theme's own badge column (sale % / out of stock): each badge is a flex item of that column */
.apptura-badges-inline { display: contents; }
/* Top-start badges only (the O-143 card has no top control, so no row): the theme column, or the theme-classed
   column made for them, spans the media between the two insets, so a long text badge gets its ellipsis instead of
   widening the column past the edge (the column is shrink-to-fit and a text badge does not wrap). The badges stay
   at its start (align-items). */
.c-product-card__media > .c-product-card__badges:is(:has(> .apptura-badges-inline), [data-apptura-badges]) { inset-inline-end: var(--sp-2, 8px); }

/* Stock OpenCart card (theme not active) */
.product-thumb .image { position: relative; }

/* ---------- Rows (theme card media, product gallery stage) ----------
   One flex row across the top and one across the bottom: start column, end column. Being one row, the two sides
   can never meet: our columns shrink first (flex-shrink 1000: images scale down, text gets an ellipsis), the
   theme's own sale / out-of-stock column last. A column with a control never gets narrower than the control. */
.apptura-badges-row {
  position: absolute;
  z-index: var(--z-card-control, 2);
  inset-inline: var(--sp-2, 8px);
  display: flex;
  align-items: flex-start;
  gap: var(--sp-1, 4px);
  pointer-events: none;
}
.apptura-badges-row--top { inset-block-start: var(--sp-2, 8px); }
.apptura-badges-row--bottom { inset-block-end: var(--sp-2, 8px); align-items: flex-end; }
.p-product-gallery__stage > .apptura-badges-row { inset-inline: var(--sp-3, 12px); }
.p-product-gallery__stage > .apptura-badges-row--top { inset-block-start: var(--sp-3, 12px); }
.p-product-gallery__stage > .apptura-badges-row--bottom { inset-block-end: var(--sp-3, 12px); }

.apptura-badges-row > .apptura-badges,
.apptura-badges-row > .apptura-badges-col {
  position: relative;
  inset: auto;
  z-index: auto;
  flex: 0 1 auto;
  min-inline-size: 0;
  max-inline-size: none;
}
.apptura-badges-row > .apptura-badges,
.apptura-badges-row > .apptura-badges--column { flex-shrink: 1000; }
.apptura-badges-row > .apptura-badges--top-end,
.apptura-badges-row > .apptura-badges--bottom-end { margin-inline-start: auto; align-items: flex-end; }
.apptura-badges-row > .apptura-badges--with-control { min-inline-size: calc(var(--tap, 44px) - var(--sp-1, 4px)); }
/* The theme's sale / out-of-stock badge, when its column is in a row: clipped only if the card is tiny */
.apptura-badges-col > .c-badge { max-inline-size: 100%; overflow: hidden; }

/* Badge list of one corner: clipped to the room of the corner (only reached with many text badges) */
.apptura-badges__list {
  display: flex;
  flex-direction: column;
  align-items: inherit;
  gap: var(--sp-1, 4px);
  min-block-size: 0;
  max-inline-size: 100%;
  overflow: hidden;
}
/* Height of an image badge (P). The two corners of a side share its height: P = (side height - px of everything
   else on that side) / image badges on that side (--apptura-* from the event, Badges::vars()). Card: the card is an
   inline-size container and its media is square, so 100cqi = media height; side height = 100cqi - 2 x 8px.
   Product page: the stage is made an inline-size container (square too); below 768px (no gallery arrows) as on the
   cards with 2 x 12px; from 768px each corner keeps to its half: 50cqi - 12px inset - 22px (half an arrow) - 8px.
   */
.apptura-badges-host { container-type: inline-size; }
/* Theme listing "list view": the card is wide but its media is a small square beside the text, so there the media
   is the container (100cqi = media height again) */
.p-listing-results--list .c-product-card__media:has([data-apptura-badges]) { container-type: inline-size; }
/* Computed on the element that carries the numbers (a var() inside a custom property is resolved where it is set) */
.c-product-card :is(.apptura-badges__list, .apptura-badges-inline, .apptura-badges--column) {
  --apptura-p: max(16px, (100cqi - 2 * var(--sp-2, 8px) - var(--apptura-side, 0px)) / var(--apptura-img, 1));
}
.p-product-gallery__stage :is(.apptura-badges__list, .apptura-badges-inline, .apptura-badges--column) {
  --apptura-p: max(16px, (100cqi - 2 * var(--sp-3, 12px) - var(--apptura-side, 0px)) / var(--apptura-img, 1));
}
/* A list never takes more than what is left by the other corner of its side, or half the side when both corners
   are too full (so the two can never meet), minus its control. A list that is still too full (several text badges
   in one corner of a small card) wraps its last badges into a next column beside the first one, towards the middle
   (end lists: wrap-reverse, so the first column stays in the corner); what does not fit the width is clipped. */
.c-product-card .apptura-badges__list {
  --apptura-h: (100cqi - 2 * var(--sp-2, 8px));
}
.p-product-gallery__stage .apptura-badges__list {
  --apptura-h: (100cqi - 2 * var(--sp-3, 12px));
}
:is(.c-product-card, .p-product-gallery__stage) .apptura-badges__list {
  flex-wrap: wrap;
  align-content: flex-start;
  max-block-size: calc(min(var(--apptura-own, 0) * var(--apptura-p) + var(--apptura-fix, 0px), max(var(--apptura-h) - var(--apptura-other, 0px) - (var(--apptura-img, 1) - var(--apptura-own, 0)) * var(--apptura-p), (var(--apptura-h) - 8px) / 2)) - var(--apptura-ctl, 0px) + 1px);
}
/* wrap-reverse swaps the cross axis: flex-start is now the inline end, the corner */
:is(.c-product-card, .p-product-gallery__stage) :is(.apptura-badges--top-end, .apptura-badges--bottom-end) > .apptura-badges__list { flex-wrap: wrap-reverse; align-items: flex-start; }
@media (min-width: 768px) {
  .p-product-gallery__stage :is(.apptura-badges__list, .apptura-badges-inline, .apptura-badges--column) {
    --apptura-p: max(16px, (50cqi - 42px - var(--apptura-fix, 0px)) / max(1, var(--apptura-own, 1)));
  }
  .p-product-gallery__stage .apptura-badges__list { max-block-size: calc(50cqi - 42px - var(--apptura-ctl, 0px)); }
}

/* ---------- Product page controls moved into a corner column (O-142) ----------
   They leave their absolute position for the column and stay clickable (the column itself is not). */
.apptura-badges--with-control > .p-product-gallery__zoom,
.apptura-badges--with-control > .p-product-gallery__count { position: relative; inset: auto; flex: none; }
.apptura-badges--with-control > button { pointer-events: auto; }
/* Zoom button: stays at the theme's 8px from the edges (the row is at 12px) */
.p-product-gallery__stage .apptura-badges--with-control { gap: var(--sp-2, 8px); }
.apptura-badges--with-control > .p-product-gallery__zoom { margin-inline-end: calc(var(--sp-2, 8px) - var(--sp-3, 12px)); }
.apptura-badges--with-control > .p-product-gallery__zoom:first-child { margin-block-start: calc(var(--sp-2, 8px) - var(--sp-3, 12px)); }

/* ---------- Text badge: the theme's .c-badge shape (24px, 30px on the product page), colours from the badge ---------- */
.apptura-badge--text {
  display: block;
  box-sizing: border-box;
  max-inline-size: 100%;
  block-size: 1.5rem;
  padding: 0 var(--sp-2, 8px);
  border-radius: var(--r-sm, 8px);
  background: var(--apptura-badge-bg, #ca3531);
  color: var(--apptura-badge-fg, #ffffff);
  font-family: var(--font-display, inherit);
  font-size: var(--fs-2xs, 0.75rem);
  font-weight: var(--fw-medium, 500);
  line-height: 1.5rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  unicode-bidi: plaintext; /* an English text on the Arabic store is cut at its own end */
  flex: none;
}
.apptura-badge--text.apptura-badge--lg { block-size: 1.875rem; line-height: 1.875rem; padding-inline: var(--sp-3, 12px); font-size: var(--fs-sm, 0.875rem); }

/* ---------- Image badge ----------
   The theme styles every image inside the card media (full size, padding, blend, hover zoom): same specificity,
   loaded later, so these win. Stock card: a fixed width. */
.apptura-badge--image,
.c-product-card .c-product-card__media img.apptura-badge--image {
  display: block;
  inline-size: 72px;
  inline-size: min(96px, 36cqi);
  block-size: auto;
  max-inline-size: none;
  padding: 0;
  object-fit: contain;
  mix-blend-mode: normal;
  transform: none;
  transition: none;
  flex: none;
}
/* Theme card / gallery: width and height both auto, so the image keeps its ratio inside two limits: the width of
   its column (max 96px on cards, 140px on the product page) and its height share P (--apptura-p, above). */
.c-product-card .c-product-card__media img.apptura-badge--image {
  inline-size: auto;
  max-inline-size: min(96px, 36cqi, 100%);
  max-block-size: var(--apptura-p);
}
.p-product-gallery__stage img.apptura-badge--image {
  inline-size: auto;
  max-inline-size: min(140px, 30cqi, 100%);
  max-block-size: var(--apptura-p);
}
