/*
 * SleeveSync rarity finishes for card images and card frames.
 *
 * Usage: add `rarity rarity--{common|uncommon|rare|ultra-rare|secret-rare|custom}` to the element that
 * frames a card image, and put <span class="rarity-fx" aria-hidden="true"></span> inside it.
 *   - `rarity--sealed`  unpulled card on a pack page: dimmed, faint static edge only.
 *   - `rarity--custom`  non-standard category: plain edge in --rarity-color (the DB colour).
 * Only frames and images get these classes; never buttons. Animations only use transform and opacity,
 * and all of them stop under prefers-reduced-motion (the static finish stays).
 */

/* Zero-specificity base so a component's own position/radius (e.g. absolutely placed tiles) still wins. */
:where(.rarity) {
  position: relative;
  border-radius: var(--rarity-radius);
}

.rarity {
  --rarity-bw: 1px;
  --rarity-radius: 0.75rem;
  --rarity-color: #6b7280;
  --rarity-glow: none;
  isolation: isolate;
  overflow: hidden;
  padding: var(--rarity-bw);
  border: 0 !important;
  background: var(--rarity-color);
  box-shadow: var(--rarity-glow);
}

.rarity > img,
.rarity > .tcg-card__placeholder {
  position: relative;
  z-index: 1;
  display: block;
  border-radius: calc(var(--rarity-radius) - var(--rarity-bw));
}

/* Animated border ring (Ultra Rare and Secret Rare): an oversized conic gradient that rotates behind the image. */
.rarity::before {
  content: none;
  position: absolute;
  z-index: 0;
  left: 50%;
  top: 50%;
  width: 200%;
  aspect-ratio: 1;
  translate: -50% -50%;
  pointer-events: none;
}

/* Finish layer above the image (foil, holo, sheen) and a second layer for sparkles. */
.rarity-fx {
  position: absolute;
  inset: var(--rarity-bw);
  z-index: 3;
  overflow: hidden;
  border-radius: calc(var(--rarity-radius) - var(--rarity-bw));
  pointer-events: none;
}

.rarity-fx::before,
.rarity-fx::after {
  content: none;
  position: absolute;
  pointer-events: none;
}

/* Common: matte, thin grey edge, no effect. */
.rarity--common {
  --rarity-color: #6b7280;
}

/* Uncommon: green edge and a subtle static sheen. */
.rarity--uncommon {
  --rarity-bw: 2px;
  --rarity-color: #22c55e;
  --rarity-glow: 0 0 10px rgba(34, 197, 94, 0.28);
}

.rarity--uncommon .rarity-fx::before {
  content: '';
  inset: 0;
  background: linear-gradient(130deg, transparent 30%, rgba(255, 255, 255, 0.16) 48%, transparent 62%);
  mix-blend-mode: soft-light;
}

/* Rare: blue edge glow and a slow diagonal foil shimmer sweep. */
.rarity--rare {
  --rarity-bw: 2px;
  --rarity-color: #60a5fa;
  --rarity-glow: 0 0 14px rgba(96, 165, 250, 0.5), 0 0 2px rgba(191, 219, 254, 0.8);
}

.rarity--rare .rarity-fx::before {
  content: '';
  top: -50%;
  bottom: -50%;
  left: -100%;
  width: 100%;
  background: linear-gradient(105deg, transparent 25%, rgba(191, 219, 254, 0.1) 40%, rgba(255, 255, 255, 0.5) 50%, rgba(191, 219, 254, 0.1) 60%, transparent 75%);
  mix-blend-mode: overlay;
  transform: translateX(0) rotate(12deg);
  animation: rarity-sweep 5.5s ease-in-out infinite;
  will-change: transform;
}

/* Ultra Rare: rainbow holo foil, pink glow and an animated gradient border. */
.rarity--ultra-rare {
  --rarity-bw: 3px;
  --rarity-color: #e879f9;
  --rarity-glow: 0 0 22px rgba(232, 121, 249, 0.6), 0 0 6px rgba(244, 114, 182, 0.7);
  background: #6d28d9;
}

.rarity--ultra-rare::before {
  content: '';
  background: conic-gradient(from 0deg, #f472b6, #a855f7, #22d3ee, #34d399, #facc15, #fb923c, #f472b6);
  animation: rarity-spin 6s linear infinite;
  will-change: transform;
}

.rarity--ultra-rare .rarity-fx::before {
  content: '';
  inset: -60%;
  background: linear-gradient(115deg,
    rgba(255, 0, 128, 0.0) 0%, rgba(255, 0, 128, 0.55) 12%, rgba(255, 200, 0, 0.5) 24%,
    rgba(0, 255, 160, 0.5) 36%, rgba(0, 200, 255, 0.55) 48%, rgba(160, 80, 255, 0.55) 60%,
    rgba(255, 0, 128, 0.5) 72%, rgba(255, 200, 0, 0.45) 84%, rgba(255, 0, 128, 0.0) 100%);
  mix-blend-mode: color-dodge;
  opacity: 0.38;
  animation: rarity-holo 7s ease-in-out infinite alternate;
  will-change: transform;
}

/* Secret Rare: gold foil, sparkles, animated gold border and the strongest glow. */
.rarity--secret-rare {
  --rarity-bw: 3px;
  --rarity-color: #fbbf24;
  --rarity-glow: 0 0 30px rgba(251, 191, 36, 0.75), 0 0 64px rgba(245, 158, 11, 0.35), 0 0 4px rgba(254, 243, 199, 0.9);
  background: #b45309;
}

.rarity--secret-rare::before {
  content: '';
  background: conic-gradient(from 0deg, #b45309, #fcd34d, #fffbeb, #f59e0b, #b45309, #fde68a, #fffbeb, #d97706, #b45309);
  animation: rarity-spin 4s linear infinite;
  will-change: transform;
}

.rarity--secret-rare .rarity-fx::before {
  content: '';
  inset: -40%;
  background:
    repeating-linear-gradient(60deg, rgba(255, 236, 170, 0) 0 6px, rgba(255, 236, 170, 0.22) 6px 7px),
    repeating-linear-gradient(-60deg, rgba(255, 215, 120, 0) 0 9px, rgba(255, 215, 120, 0.18) 9px 10px),
    linear-gradient(120deg, rgba(180, 83, 9, 0.35), rgba(253, 230, 138, 0.55) 45%, rgba(255, 251, 235, 0.7) 50%, rgba(253, 230, 138, 0.55) 55%, rgba(180, 83, 9, 0.35));
  mix-blend-mode: overlay;
  opacity: 0.6;
  animation: rarity-gold 9s ease-in-out infinite alternate;
  will-change: transform;
}

.rarity--secret-rare .rarity-fx::after {
  content: '';
  inset: 0;
  background:
    radial-gradient(circle at 18% 22%, #fff 0 1.5px, transparent 2.5px),
    radial-gradient(circle at 72% 14%, #fffbeb 0 1px, transparent 2px),
    radial-gradient(circle at 84% 58%, #fff 0 1.5px, transparent 2.5px),
    radial-gradient(circle at 30% 70%, #fef3c7 0 1px, transparent 2px),
    radial-gradient(circle at 58% 86%, #fff 0 1.5px, transparent 2.5px),
    radial-gradient(circle at 46% 40%, #fffbeb 0 1px, transparent 2px),
    radial-gradient(circle at 10% 90%, #fff 0 1px, transparent 2px);
  opacity: 0.9;
  animation: rarity-twinkle 2.4s ease-in-out infinite;
  will-change: opacity, transform;
}

/* Non-standard category: plain edge in the category's own colour. */
.rarity--custom {
  --rarity-bw: 2px;
}

/* Unpulled card on a pack page: dimmed, faint static edge, no finish or glow. */
.rarity--sealed {
  --rarity-glow: none;
  background: color-mix(in srgb, var(--rarity-color) 45%, #0b0e14);
}

.rarity--sealed::before,
.rarity--sealed .rarity-fx {
  display: none;
}

.rarity--sealed > img {
  filter: saturate(0.8) brightness(0.82);
}

/* Overlay pills inside a frame stay above the finish; the PULLED overlay (z-index 20) stays on top. */
.rarity > .item-category-pill--overlay {
  z-index: 4;
}

.ss-feed__thumb.rarity { --rarity-radius: 0.5rem; }
.channel-pack-tile__card.rarity { --rarity-radius: 0.3rem; --rarity-bw: 1px; }

/* Small thumbnails (feeds, tiles): thinner edges and softer glow so the effect stays proportional. */
.rarity--sm {
  --rarity-radius: 0.5rem;
}

.rarity--sm.rarity--ultra-rare,
.rarity--sm.rarity--secret-rare {
  --rarity-bw: 2px;
}

.rarity--sm.rarity--rare { --rarity-glow: 0 0 8px rgba(96, 165, 250, 0.5); }
.rarity--sm.rarity--ultra-rare { --rarity-glow: 0 0 12px rgba(232, 121, 249, 0.6); }
.rarity--sm.rarity--secret-rare { --rarity-glow: 0 0 14px rgba(251, 191, 36, 0.75), 0 0 28px rgba(245, 158, 11, 0.3); }

@keyframes rarity-spin {
  to { transform: rotate(1turn); }
}

@keyframes rarity-sweep {
  0%, 35% { transform: translateX(0) rotate(12deg); }
  75%, 100% { transform: translateX(300%) rotate(12deg); }
}

@keyframes rarity-holo {
  from { transform: translate3d(-12%, -8%, 0); }
  to { transform: translate3d(12%, 8%, 0); }
}

@keyframes rarity-gold {
  from { transform: translate3d(-8%, 4%, 0); }
  to { transform: translate3d(8%, -4%, 0); }
}

@keyframes rarity-twinkle {
  0%, 100% { opacity: 0.25; transform: scale(1); }
  50% { opacity: 1; transform: scale(1.04); }
}

/* Reduced motion: no animation anywhere in the system; the static finish stays. */
@media (prefers-reduced-motion: reduce) {
  .rarity::before,
  .rarity-fx::before,
  .rarity-fx::after {
    animation: none !important;
  }

  .rarity--rare .rarity-fx::before {
    transform: translateX(150%) rotate(12deg);
  }
}
