/**
 * Custom shop/category/search product tile.
 * Pairs with woocommerce/content-product.php and product-tile.js.
 *
 * The grid's resting density is unchanged — only the ONE tile currently
 * being hovered grows, temporarily painting above its neighbours, and
 * snaps back the moment the pointer leaves.
 *
 * GEOMETRY is deliberately expressed as custom properties at the top of
 * each block so the fan can be retuned against real card art without
 * hunting through rules. --pcf-fan-spacing is set per tile by the JS,
 * because it depends on how many cards that particular product has.
 */

/* ============================================
   TUNABLES
   ============================================ */

.pcf-tile {
	/* How much the hovered image grows. A fan needs the room: its cards are
	   58% of the tile, and growing it is what makes them big enough to pick
	   between. A single printing is just the one card, so it grows much
	   less — at 1.9x it was obnoxiously large on its own, and stretched its
	   300px thumbnail to nearly 600px tall at a full-width window, which
	   read as fuzzy however it was rendered. */
	--pcf-tile-scale: 1.9;
	--pcf-tile-scale-single: 1.3;

	/* Fan card box, as % of the media box. Card and media share the 5:7
	   ratio, so the card's height as a share of the media's height is the
	   same number as its width — which is why --pcf-card-top is simply
	   (100 - --pcf-card-w) / 2 to centre the band. Keep the two in step. */
	--pcf-card-w: 58%;
	--pcf-card-top: 21%;

	/* How far apart the card CENTRES are spread, end to end, as % of the
	   media width. This one value drives three things that have to agree:
	   the cards' translateX, the hit zones' width and position, and where
	   the quick-add badge sits. The JS reads it and derives all three, so
	   they cannot fall out of step — change it here only.

	   Cards rotate about their own centre (not a pivot below them, as an
	   earlier version did). A pivot below swings a rotated card sideways
	   as well as tilting it, by an amount that grows with the angle — so
	   card centres no longer matched their zones, and there was no badge
	   position that was both on the card and inside the cursor's own zone.
	   Rotating about the centre keeps horizontal position governed purely
	   by translateX, which is what makes the three line up by
	   construction.

	   SPREAD is the distance between the outermost card centres at a FULL
	   fan. The gap between neighbouring centres is therefore
	   spread / (5 - 1), and that gap is held constant no matter how many
	   cards a product actually has — a two-card fan uses one gap rather
	   than stretching the pair out to the full spread, which looked like
	   two unrelated images rather than a hand.

	   KEEPING EVERY CARD VISIBLE is handled by the stacking order, not by
	   the geometry: cards step down in z from whichever one is in focus,
	   so each card is only ever covered by its neighbour on the side
	   facing the focused card, and always keeps a sliver one gap wide.
	   That holds for any card width, which is why the cards can be this
	   large. (A flat left-to-right z-order does NOT hold: the focused card
	   swallows its neighbour's sliver and the card above covers the rest,
	   which is what used to blank out everything but the far right card.
	   See applyStack() in product-tile.js.) */
	--pcf-fan-spread: 80%;
	--pcf-fan-angle: 5deg;

	/* Where the quick-add badge sits ON the active card, measured from
	   that card's own centre — dx as % of the media width, dy as % of its
	   height — then rotated with the card so it reads as attached to it
	   rather than floating over the fan. Low and a little right of centre:
	   away from the middle of the art, where someone hurrying to open the
	   product is most likely to click.

	   Two limits bound these, and both were measured, not guessed:

	   dy must stay inside the card. The card's half-height is
	   --pcf-card-w / 2 of the media height (29% here), so 18% puts the
	   badge about 60% of the way down towards the bottom edge. Anything
	   at or past 29% floats it off the card entirely.

	   dx is bounded by the hit zone: the badge must stay inside the active
	   card's own zone, or the pointer crosses into the next card's zone on
	   the way to it and swaps the card out from under the click. The
	   budget is mostly already spent — turning the offset through the
	   card's rotation shifts the outer cards' badges sideways by about
	   4.4%, and half the badge is another 4.5%, against a half-gap of 10%.
	   Hence 0: horizontally centred on the card, low down, which is off
	   the middle of the art anyway. */
	--pcf-badge-dx: 0%;
	--pcf-badge-dy: 18%;

	/* Badge size, as % of the media width. Small enough to be an unlikely
	   accidental target, large enough to stay an easy deliberate one —
	   ~34px against the enlarged tile. Also has to fit within a hit zone
	   alongside the rotation offset above — see --pcf-badge-dx. The
	   outermost cards are the binding case and had only ~1px to spare at
	   9%, so this is deliberately a notch smaller than it needs to look
	   right, to leave the constraint some room. */
	--pcf-badge-size: 8%;

	/* The single printing's smaller zoom would shrink its badge along with
	   it, so the badge's share of the tile grows by the same ratio and it
	   ends up exactly the size it is on a fan. No hit zones to fit inside
	   there, so none of the limits above apply. */
	--pcf-badge-size-single: calc(var(--pcf-badge-size) * var(--pcf-tile-scale) / var(--pcf-tile-scale-single));

	/* Backdrop panel behind the fan. Gaps are pure visual margin — the
	   JS already accounts for how far the rotated cards actually reach,
	   so these sit outside that. Bottom is deliberately the tightest of
	   the three so the hand reads as resting on the surface.

	   NOTE on radius: the tile itself is square-cornered (0px), so there
	   was no tile radius to inherit. This follows the fan cards' rounding
	   instead, which is the only curve in the tile as it stands. */
	--pcf-panel-gap-x: 4%;
	--pcf-panel-gap-top: 5%;
	--pcf-panel-gap-bottom: 1.5%;
	--pcf-panel-radius: 6px;

	--pcf-accent: #c0531a;
	--pcf-accent-dark: #a5460f;
}

/* ============================================
   TILE SHELL
   ============================================ */

/* The grid item itself owns the positioning context, so this is where the
   hovered tile gets lifted above its neighbours. Putting the z-index on an
   inner wrapper instead would do nothing at all: a z-index only competes
   with siblings, so tiles later in the DOM would keep painting on top. */
.pcf-tile {
	position: relative;
}

.pcf-tile:hover {
	z-index: 30;
}

/* Astra sets overflow:hidden on shop-loop items, which would crop the
   scaled media back to the tile's resting box and defeat the whole
   mechanic. */
ul.products li.product.pcf-tile,
.woocommerce ul.products li.product.pcf-tile {
	overflow: visible;
}

/* Same story one level up: the main content column clips too, which would
   shave the outer edge off tiles in the first and last grid columns. Safe
   to release here because <body> already carries overflow-x:hidden, so
   nothing can introduce a horizontal scrollbar. */
.woocommerce main.page-holder,
.woocommerce-page main.page-holder {
	overflow: visible;
}

/* ============================================
   MEDIA — the only part that scales
   ============================================ */

.pcf-tile-media {
	position: relative;
	display: block;
	aspect-ratio: 5 / 7;
	transform-origin: bottom center;
	transition: transform 0.2s ease;
	/* Its own stacking context, so the foil overlay's z-index and
	   mix-blend-mode stay scoped to this tile. That used to come from
	   will-change: transform, which cannot be used here: Chrome rasterises
	   a will-change: transform element once at its resting size and only
	   stretches that bitmap afterwards, so a larger image swapped in on
	   hover (scan-toggle.js's upgradeImage()) changed nothing on screen — a
	   1024px file looked exactly as soft as the thumbnail. The transition
	   itself still runs on the compositor. */
	isolation: isolate;
}

.pcf-tile-media:hover {
	transform: scale(var(--pcf-tile-scale));
}

/* No fan means a single card, which takes the smaller zoom. has-fan is set
   by the JS only once cards were actually built, so this also covers a
   variable product whose filtered pool came back empty. Swapping the
   variables rather than restating the transform leaves the mobile
   `transform: none` below in charge on small screens. */
.pcf-tile-media:not(.has-fan) {
	--pcf-tile-scale: var(--pcf-tile-scale-single);
	--pcf-badge-size: var(--pcf-badge-size-single);
}

.pcf-tile-media-link {
	position: absolute;
	inset: 0;
	display: block;
	overflow: hidden;
	/* Card corners. Measured from the die-cut in Scryfall's transparent
	   rendition and shared with the fan cards and the single-product image —
	   see CARD SILHOUETTE in proxy-complex-core-features/assets/
	   foil-effects.css for the measurement and for why it is an x / y pair
	   rather than one percentage. */
	border-radius: var(--pcf-card-radius, 3.99% / 2.85%);
}

/* Astra ships `.woocommerce-js ul.products li.product a img { height: auto }`,
   which outweighs a plain `.pcf-tile-media-link img` and leaves the image
   at its natural square ratio inside the taller 5:7 box — a dead strip
   under every thumbnail. Matched here with enough specificity to win
   outright rather than reaching for !important. */
ul.products li.product.pcf-tile .pcf-tile-media-link img {
	display: block;
	width: 100%;
	height: 100%;
	object-fit: cover;
	transition: opacity 0.2s ease;
}

/* Once the fan is out it already contains this exact photo as its front
   card, so leaving the original showing behind them made the same image
   serve as both fan member and backdrop. The parchment panel takes over.
   Scoped to .has-fan (set by the JS only when cards were actually built)
   so a single-printing tile — which has no fan — keeps its image. */
ul.products li.product.pcf-tile .pcf-tile-media.has-fan:hover .pcf-tile-media-link img {
	opacity: 0;
}

/* ============================================
   CAPTION — never scales, never moves
   ============================================ */

.pcf-tile-caption {
	display: block;
	margin-top: 0.6em;
	color: inherit;
	text-decoration: none;
}

.pcf-tile-caption:hover,
.pcf-tile-caption:focus,
.pcf-tile-caption:visited {
	color: inherit;
	text-decoration: none;
}

.pcf-tile-title {
	display: block;
	font-weight: 600;
}

.pcf-tile-price {
	display: block;
	margin-top: 0.2em;
	color: var(--pcf-accent);
}

.pcf-tile-price del {
	color: inherit;
	opacity: 0.5;
	margin-right: 0.4em;
}

.pcf-tile-price ins {
	text-decoration: none;
}

.pcf-tile-data {
	display: none;
}

/* ============================================
   FAN
   Revealed by :hover rather than a JS-toggled class on purpose — a pure
   CSS reveal cannot desync from the pointer, so the fan can never be
   left stuck open. The JS only tracks which card is active, and clears
   that on the same element's mouseleave.
   ============================================ */

/* ============================================
   BACKDROP PANEL
   A plain parchment surface for the hand to sit on. Replaces the older
   arrangement where the default variation's own photo showed through
   behind the fan and did double duty as both a fan member and the
   backdrop — see the .has-fan rule further down, which retires the photo
   while the fan is out.
   ============================================ */

/* left/width/top/height come from the JS, which measures how far the
   rotated cards actually reach. */
.pcf-fan-backdrop {
	position: absolute;
	/* Under every fan card, not merely the inactive ones. The fan is given
	   its own z-index below and stacks its cards internally, so one value
	   beneath the whole group is enough and cannot be leapfrogged by
	   whichever card is currently focused. */
	z-index: 5;
	border-radius: var(--pcf-panel-radius);
	background: linear-gradient(180deg, #f9dfbe 0%, #e8c6a0 100%);
	border: 1px solid rgba(67, 47, 31, 0.22);
	box-shadow: 0 6px 18px rgba(0, 0, 0, 0.28);
	opacity: 0;
	pointer-events: none;
	/* Matches the media's own transform timing so the panel arrives with
	   the zoom rather than as a second, separate movement. */
	transition: opacity 0.2s ease;
}

.pcf-tile-media:hover .pcf-fan-backdrop {
	opacity: 1;
}

.pcf-fan {
	position: absolute;
	inset: 0;
	/* Above the backdrop as a single group. The cards' own z-indexes then
	   only compete with each other inside this, which is what keeps the
	   backdrop underneath all of them regardless of which is focused. */
	z-index: 10;
	opacity: 0;
	transition: opacity 0.18s ease;
	pointer-events: none;
}

.pcf-tile-media:hover .pcf-fan {
	opacity: 1;
}

.pcf-fan-card {
	position: absolute;
	top: var(--pcf-card-top);
	left: 50%;
	width: var(--pcf-card-w);
	aspect-ratio: 5 / 7;
	margin-left: calc(var(--pcf-card-w) / -2);
	/* Was a flat 3%, which resolved against height on the vertical axis and
	   so drew a taller corner than a card actually has. Now the same
	   measured card radius the tile and product image use. */
	border-radius: var(--pcf-card-radius, 3.99% / 2.85%);
	overflow: hidden;
	box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
	transform-origin: 50% 50%;
	transition: transform 0.18s ease, box-shadow 0.18s ease;
	/* z-index is owned by applyStack() in the JS — it depends on which
	   card is in focus, so it can't be expressed statically here. Values
	   stay below .pcf-fan-hitzones (30) so the zones keep the hover. */
}

/* Resting: stacked dead centre, so the fan grows out of the front image
   rather than appearing from nowhere. */
.pcf-tile-media:hover .pcf-fan-card {
	transform:
		translateX(calc(var(--pcf-fan-offset, 0) * var(--pcf-fan-spacing, 0%)))
		rotate(calc(var(--pcf-fan-offset, 0) * var(--pcf-fan-angle)));
}

/* Same specificity guard as the base image above. */
ul.products li.product.pcf-tile .pcf-fan-card img {
	display: block;
	width: 100%;
	height: 100%;
	object-fit: cover;
}

/* Last slot when there are more printings than the fan can hold. Takes a
   real card's place rather than being squeezed in beside them, so the fan
   never grows past five. */
.pcf-fan-card.is-see-all {
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 6%;
	background: #3d2410;
	color: #f5ddb5;
	text-align: center;
	font-size: 0.8em;
	font-weight: 600;
	line-height: 1.25;
}

/* The active card stays exactly where it sits in the fan — same
   translateX, same rotation — and is only raised to the top of the stack
   and grown slightly. Pulling it to dead centre instead (as an earlier
   version did) collapsed the fan every time you moved along it and, worse,
   put the badge somewhere the cursor couldn't reach without crossing
   another card's zone. Scale is about the card's own centre, so this
   changes no position at all. */
.pcf-tile-media:hover .pcf-fan-card.is-active {
	transform:
		translateX(calc(var(--pcf-fan-offset, 0) * var(--pcf-fan-spacing, 0%)))
		rotate(calc(var(--pcf-fan-offset, 0) * var(--pcf-fan-angle)))
		scale(1.05);
	box-shadow: 0 6px 18px rgba(0, 0, 0, 0.55);
}

/* ============================================
   HIT ZONES
   N non-overlapping rectangles tiling the whole hoverable area, mapped 1:1
   to card order by position and completely decoupled from the cards'
   overlapping shapes and paint order. Hit-testing the rotated cards
   themselves would let whichever card paints on top win the hover even
   where a different one looks like it's under the cursor, so sweeping
   across the fan would skip cards unpredictably.
   There is deliberately no gap anywhere: every point over the fan belongs
   to exactly one card, so the pointer is never in a spot where nothing is
   in focus.
   ============================================ */

/* left/width are set by the JS from --pcf-fan-spread so the strip lines up
   with the cards, one zone centred on each card's centre. */
.pcf-fan-hitzones {
	position: absolute;
	top: 0;
	height: 100%;
	display: flex;
	z-index: 30;
	pointer-events: none;
}

.pcf-tile-media:hover .pcf-fan-hitzones {
	pointer-events: auto;
}

.pcf-fan-hitzone {
	height: 100%;
	/* Widths are computed exactly and already sum to the strip; don't let
	   flex rebalance them or the zones would drift off their cards. */
	flex: 0 0 auto;
}

/* ============================================
   QUICK-ADD BADGE
   Sits at the centre of the active card. Because each zone is centred on
   its own card's centre (see --pcf-fan-spread), that point is always
   inside the zone the cursor is already in — so reaching the badge can
   never cross into a neighbouring card's zone and swap the card out from
   under the click, whichever card in the fan is active.
   `left` is set per active card by the JS; a sibling of the zones rather
   than a child of a card, because a card's z-index makes it its own
   stacking context and a badge nested inside one could never paint above
   the separate hit-zone layer no matter its z-index.
   ============================================ */

/* top / left / rotation are all written by the JS, per active card. */
.pcf-fan-badge {
	position: absolute;
	z-index: 40;
	display: flex;
	align-items: center;
	justify-content: center;
	width: var(--pcf-badge-size);
	aspect-ratio: 1;
	padding: 0;
	border: none;
	border-radius: 50%;
	background: var(--pcf-accent);
	color: #fff;
	cursor: pointer;
	box-shadow: 0 2px 8px rgba(0, 0, 0, 0.45);
	/* visibility (not just opacity) so it leaves the tab order while hidden */
	visibility: hidden;
	opacity: 0;
	/* Only the fade is animated. Sliding the badge from card to card read
	   as fussy, so it simply belongs to whichever card is up. */
	transition: opacity 0.15s ease;
}

/* Two stages: present-but-quiet once a card is active, full strength once
   the cursor is actually on it. */
.pcf-tile-media.is-card-active .pcf-fan-badge {
	visibility: visible;
	opacity: 0.4;
}

.pcf-tile-media.is-card-active .pcf-fan-badge:hover,
.pcf-tile-media.is-card-active .pcf-fan-badge:focus-visible {
	opacity: 1;
	background: var(--pcf-accent-dark);
}

/* Same outline cart used by the search autocomplete's add button, so the
   two read as the same control. */
.pcf-fan-badge svg {
	width: 58%;
	height: 58%;
	fill: none;
	stroke: currentColor;
	stroke-width: 2;
}

.pcf-fan-badge.is-adding {
	opacity: 0.5;
	pointer-events: none;
}

.pcf-fan-badge.is-added {
	background: #2e7d32;
	opacity: 1;
}

/* ============================================
   Mobile keeps its own pattern and is not built here — an earlier
   persistent-icons + options-sheet attempt was reverted per owner
   feedback. Below this width, and on a touchscreen at any width (where a
   tap would only trigger the hover), the tile is just the plain image +
   caption links, and product-tile.js builds no interactive layer at all.
   ============================================ */

@media (max-width: 1025px), (hover: none) {
	.pcf-tile-media {
		transition: none;
	}

	.pcf-tile-media:hover {
		transform: none;
	}
}

/* Reduced motion drops the animation but keeps the size change: growing
   the tile is what makes the cards selectable at all, so removing it
   would leave these users with the cramped fan this rework exists to fix.
   It just happens instantly instead of gliding. */
@media (prefers-reduced-motion: reduce) {
	.pcf-tile-media,
	.pcf-fan,
	.pcf-fan-card,
	.pcf-fan-badge {
		transition: none;
	}
}
