/* Rarity finishes (Cards/CardFinish.js): a frame per rarity and, for some, an animated effect over
   the card art. A host is any box holding a `.finish`: it carries `r-<key>` and `--rarity`, clips its
   content and is positioned. Sizes use cqw so a 66px thumb and a 168px plate share proportions. */

.r-uncommon,
.r-rare,
.r-elemental {
	--glow: 0 0 10px color-mix(in srgb, var(--rarity) 45%, transparent);
}

.r-enchanted,
.r-blessed,
.r-cursed,
.r-foil {
	--glow: 0 0 16px color-mix(in srgb, var(--rarity) 65%, transparent);
}

:has(> .finish) {
	isolation: isolate;
	container-type: inline-size;
}

/* No z-index or transform here: the layers must blend with the art below. */
.finish {
	position: absolute;
	inset: 0;
	border-radius: inherit;
	pointer-events: none;
}

.finish::before,
.finish::after,
.finish > i {
	content: '';
	position: absolute;
	pointer-events: none;
}

/* The frame is a ring: the padding box minus the content box. */
.finish::after {
	inset: 0;
	border-radius: inherit;
	padding: var(--frame-w);
	background: var(--frame);
	-webkit-mask:
		linear-gradient(#000 0 0) content-box,
		linear-gradient(#000 0 0);
	-webkit-mask-composite: xor;
	mask:
		linear-gradient(#000 0 0) content-box exclude,
		linear-gradient(#000 0 0);
}

/* --- Frames ------------------------------------------------------------------------------------ */

/* Matte pewter */
.r-common .finish::after {
	--frame-w: max(1.5px, 1.8cqw);
	--frame: linear-gradient(160deg, #e4e4e4, #9a9a9a 40%, #5e5e5e);
}

/* Blue double rule */
.r-uncommon .finish::after {
	--frame-w: 4px;
	--frame: transparent;
	box-shadow:
		inset 0 0 0 1.5px #4f8cff,
		inset 0 0 0 2.5px rgba(4, 16, 44, 0.85),
		inset 0 0 0 4px #a9c8ff;
}

/* Bevelled gold */
.r-rare .finish::after {
	--frame-w: max(2px, 3cqw);
	--frame: linear-gradient(135deg, #fff6d2 0%, #f2cf63 16%, #a87a14 40%, #5e4106 58%, #e2b443 80%, #fff0b8 100%);
}

/* Iridescent rim; hue-rotate keeps the loop seamless. */
.r-foil .finish::after {
	--frame-w: max(2px, 3cqw);
	--frame: linear-gradient(115deg, #ff6ad6, #ffd66e 20%, #7dffa0 40%, #6ee2ff 60%, #a07dff 80%, #ff6ad6);
	animation: finish-hue 5s linear infinite;
}

/* Emerald frame with bright corner brackets */
.r-elemental .finish::after {
	--frame-w: max(2px, 3cqw);
	--frame: linear-gradient(160deg, #bff5d9, #2dbf7e 30%, #0c6a44 60%, #4fdc9a);
}

.r-elemental .finish::before {
	--b: max(1.5px, 2.4cqw);
	--l: 20cqw;
	--c: #c2ffe0;
	inset: max(4px, 5.5cqw);
	background:
		linear-gradient(var(--c) 0 0) top left / var(--l) var(--b),
		linear-gradient(var(--c) 0 0) top left / var(--b) var(--l),
		linear-gradient(var(--c) 0 0) top right / var(--l) var(--b),
		linear-gradient(var(--c) 0 0) top right / var(--b) var(--l),
		linear-gradient(var(--c) 0 0) bottom left / var(--l) var(--b),
		linear-gradient(var(--c) 0 0) bottom left / var(--b) var(--l),
		linear-gradient(var(--c) 0 0) bottom right / var(--l) var(--b),
		linear-gradient(var(--c) 0 0) bottom right / var(--b) var(--l);
	background-repeat: no-repeat;
	filter: drop-shadow(0 0 1px #043a24) drop-shadow(0 0 3px #22e08f);
}

/* Violet line, a mist of light inside it and a dotted rune ring */
.r-enchanted .finish::after {
	--frame-w: 7cqw;
	--frame: transparent;
	box-shadow:
		inset 0 0 0 2px #a46cff,
		inset 0 0 0 3px rgba(30, 8, 60, 0.8),
		inset 0 0 4cqw 1cqw rgba(138, 63, 224, 0.75);
}

.r-enchanted .finish::before {
	inset: max(5px, 6.5cqw);
	border: max(1.5px, 1.8cqw) dotted #d7b8ff;
	border-radius: 3px;
	filter: drop-shadow(0 0 1px #2a0a5a) drop-shadow(0 0 2px #a46cff);
}

/* Rose pearl, a white hairline inside it and gems in the corners */
.r-blessed .finish::after {
	--frame-w: max(2.5px, 4cqw);
	--frame: linear-gradient(135deg, #ffe6f3 0%, #ff8fc8 16%, #a8306e 34%, #ffc4e2 52%, #d04f90 72%, #ffe2f0 100%);
}

.r-blessed .finish::before {
	--gem: radial-gradient(closest-side, #fff 0 25%, #ffb6da 45%, rgba(255, 111, 184, 0) 100%);
	--gs: max(8px, 10cqw);
	inset: max(2.5px, 4cqw);
	border-radius: 3px;
	background:
		var(--gem) 0 0 / var(--gs) var(--gs),
		var(--gem) 100% 0 / var(--gs) var(--gs),
		var(--gem) 0 100% / var(--gs) var(--gs),
		var(--gem) 100% 100% / var(--gs) var(--gs);
	background-repeat: no-repeat;
	box-shadow:
		inset 0 0 0 1px rgba(255, 240, 248, 0.95),
		inset 0 0 5cqw rgba(255, 120, 190, 0.5);
}

/* Blackened iron with blood-red edges and a pulsing crimson vignette */
.r-cursed .finish::after {
	--frame-w: max(2.5px, 4cqw);
	--frame: linear-gradient(135deg, #d0213e, #3a0009 18%, #0e0002 50%, #3a0009 82%, #d0213e);
}

.r-cursed .finish::before {
	inset: 0;
	box-shadow: inset 0 0 7cqw rgba(150, 8, 32, 0.7);
	animation: finish-pulse 2.6s ease-in-out infinite;
}

/* --- Effects ----------------------------------------------------------------------------------- */

/* Sparkles (Blessed): a four-ray star that flares, turns and fades. */
.finish > .spark {
	left: var(--x);
	top: var(--y);
	width: calc(var(--s) * 34%);
	aspect-ratio: 1;
	background:
		radial-gradient(closest-side, #fff 0 10%, rgba(255, 205, 232, 0.9) 22%, rgba(255, 130, 195, 0) 62%),
		radial-gradient(50% 5% at 50% 50%, #fff 20%, rgba(255, 225, 242, 0)),
		radial-gradient(5% 50% at 50% 50%, #fff 20%, rgba(255, 225, 242, 0));
	mix-blend-mode: screen;
	opacity: 0;
	animation: finish-twinkle var(--t) ease-in-out var(--d) infinite;
}

/* Light catching the laminate (Rare): a static gloss plus two glare bands sweeping across. The
   sweep runs in cqw, not %, so the narrow band travels as far as the wide one and leaves the card. */
.finish > .glare {
	inset: 0;
	overflow: hidden;
	border-radius: inherit;
	background: linear-gradient(135deg, rgba(255, 255, 255, 0.22), rgba(255, 255, 255, 0) 38%);
	mix-blend-mode: screen;
}

.finish > .glare::before,
.finish > .glare::after {
	content: '';
	position: absolute;
	top: -20%;
	bottom: -20%;
	left: 0;
	width: 34%;
	background: linear-gradient(90deg, rgba(255, 255, 255, 0), rgba(255, 248, 230, 0.75) 50%, rgba(255, 255, 255, 0));
	transform: translateX(-100cqw) skewX(-24deg);
	animation: finish-glare 5.5s ease-in-out infinite;
}

.finish > .glare::after {
	width: 12%;
	opacity: 0.8;
	animation-delay: 0.22s;
}

/* Holographic rainbow (Foil): the baked foil overlay, drifting. Overlay for colour, screen for lift. */
.finish > .holo {
	inset: 0;
	overflow: hidden;
	border-radius: inherit;
}

.finish > .holo::before,
.finish > .holo::after {
	content: '';
	position: absolute;
	inset: -60% -200%;
	background: repeating-linear-gradient(
		90deg,
		hsl(0 100% 70%) 0%,
		hsl(60 100% 70%) 2.5%,
		hsl(120 100% 70%) 5%,
		hsl(180 100% 70%) 7.5%,
		hsl(240 100% 70%) 10%,
		hsl(300 100% 70%) 12.5%,
		hsl(360 100% 70%) 15%
	);
	transform: rotate(50deg);
	animation: finish-holo 7s linear infinite;
}

.finish > .holo::before {
	mix-blend-mode: overlay;
	opacity: 0.7;
}

.finish > .holo::after {
	mix-blend-mode: screen;
	opacity: 0.22;
}

/* Smoke (Cursed): two turbulence layers drifting across at different speeds, thicker at the bottom. */
.r-cursed .finish {
	--smoke: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='256' height='256'%3E%3Cfilter id='s' x='0' y='0' width='100%25' height='100%25'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.014 0.024' numOctaves='4' seed='11' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0.04 0 0 0 0 0.02 0 0 0 0 0.04 5.5 0 0 0 -2.3'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23s)'/%3E%3C/svg%3E");
}

.finish > .smoke {
	top: -10%;
	bottom: -10%;
	left: 0;
	width: 200%;
	background: var(--smoke) 0 0 / 50% 100% repeat;
	-webkit-mask: linear-gradient(to top, #000 15%, rgba(0, 0, 0, 0.6) 55%, rgba(0, 0, 0, 0.15));
	mask: linear-gradient(to top, #000 15%, rgba(0, 0, 0, 0.6) 55%, rgba(0, 0, 0, 0.15));
	opacity: 0.78;
	animation: finish-smoke 11s linear infinite;
}

.finish > .smoke + .smoke {
	background-size: 50% 70%;
	opacity: 0.58;
	animation-duration: 17s;
	animation-direction: reverse;
}

/* --- Motion ------------------------------------------------------------------------------------ */

@keyframes finish-hue {
	to {
		filter: hue-rotate(360deg);
	}
}

@keyframes finish-pulse {
	50% {
		box-shadow: inset 0 0 3.5cqw rgba(150, 8, 32, 0.35);
	}
}

@keyframes finish-twinkle {
	0% {
		opacity: 0;
		transform: translate(-50%, -50%) scale(0.1) rotate(0deg);
	}
	35% {
		opacity: 1;
		transform: translate(-50%, -50%) scale(1) rotate(35deg);
	}
	70%,
	100% {
		opacity: 0;
		transform: translate(-50%, -50%) scale(0.1) rotate(90deg);
	}
}

@keyframes finish-glare {
	0% {
		transform: translateX(-100cqw) skewX(-24deg);
	}
	45%,
	100% {
		transform: translateX(160cqw) skewX(-24deg);
	}
}

@keyframes finish-holo {
	from {
		transform: rotate(50deg) translateX(0);
	}
	to {
		transform: rotate(50deg) translateX(15%);
	}
}

@keyframes finish-smoke {
	from {
		transform: translateX(-50%);
	}
	to {
		transform: translateX(0);
	}
}

@media (prefers-reduced-motion: reduce) {
	.finish::before,
	.finish::after,
	.finish > i,
	.finish > i::before,
	.finish > i::after {
		animation: none !important;
	}

	.finish > .spark {
		opacity: 0.85;
		transform: translate(-50%, -50%) scale(0.7) rotate(20deg);
	}
}
