/*
  The Covet card pack. Used by card-pack.html on its own and by the deck
  section on the home page. card-pack.js builds the cards inside any
  element marked data-card-pack.

  Each card is 2.75in wide by 4.75in tall. The script works out the fan
  once per resize and gives every card its resting spot (--x, --y, --r).
  Everything that moves is a transform or an opacity.
*/

.pack {
  --card-w: min(2.2in, 12.5vw, calc(42vh * 2.75 / 4.75));
  --zoom: 2;
  --flip-time: 560ms;
  --ripple: 0ms;
  --ease-open: cubic-bezier(0.22, 1.1, 0.36, 1);
  --ease-close: cubic-bezier(0.5, 0, 0.2, 1);
  --ease-pop: cubic-bezier(0.25, 1.25, 0.4, 1);
  --ease-settle: cubic-bezier(0.3, 0.8, 0.3, 1);
  position: relative;
  margin: 1.6rem auto calc(var(--card-w) * 0.45);
  width: var(--card-w);
  height: calc(var(--card-w) * 4.75 / 2.75);
  cursor: pointer;
  outline: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: pan-y;
}

/* The flip runs across an open fan as a wave. A closed stack turns at once. */
.pack.is-open { --ripple: 7ms; }

.pack:focus-visible {
  outline: 1px solid rgba(212, 175, 55, 0.85);
  outline-offset: 10px;
}

.pack-shadow {
  position: absolute;
  z-index: 0;
  left: 12%;
  bottom: 1%;
  width: 76%;
  height: 16px;
  background: radial-gradient(ellipse, rgba(0, 0, 0, 0.5), transparent 70%);
  filter: blur(7px);
  pointer-events: none;
  transform: scaleX(1);
  transition: transform 0.7s var(--ease-close), opacity 0.7s ease;
}

.pack.is-open .pack-shadow {
  transform: scaleX(3.6);
  opacity: 0.85;
}

/* Closed: a slightly untidy stack. */
.pack-card {
  position: absolute;
  inset: 0;
  z-index: calc(var(--i) + 1);
  transform-origin: 50% 60%;
  transform: translate3d(var(--sx), var(--sy), 0) rotate(var(--sr)) scale(1);
  transition: transform 0.55s var(--ease-close) var(--close-wait);
  contain: layout style;
  user-select: none;
}

/* Cards get their own layers only while the pack is on screen. */
.pack.is-near .pack-card,
.pack.is-near .pack-shadow { will-change: transform; }

/* Open: every card on its own spot along the arc. */
.pack.is-open .pack-card {
  transform: translate3d(var(--x), var(--y), 0) rotate(var(--r)) scale(1);
  transition: transform 0.72s var(--ease-open) var(--open-wait);
}

/* Once the fan is being explored, cards answer straight away. */
.pack.is-open.is-live .pack-card {
  transition: transform 0.36s var(--ease-settle) 0s;
}

/* A card on its way back down stays above its neighbours until it lands. */
.pack .pack-card.was-hot { z-index: 150; }

/* The card under the pointer grows where it sits. --hx and --hy only move
   it far enough to stay in view. will-change comes off so the browser
   redraws it sharp at the larger size. */
.pack.is-open .pack-card.is-hot {
  z-index: 200;
  will-change: auto;
  transform:
    translate3d(calc(var(--x) + var(--hx, 0px)), calc(var(--y) + var(--hy, 0px)), 0)
    rotate(var(--hr, var(--r)))
    scale(var(--zoom));
  transition: transform 0.34s var(--ease-pop) 0s;
}

/* The part that turns over. Rim, backing, and shadow travel with it. */
.pack-turn {
  --flip-wait: calc(var(--i) * var(--ripple));
  position: absolute;
  inset: 0;
  border-radius: 7.16% / 4.15%;
  background: #2a2623;
  box-shadow:
    0 0 0 1px rgba(176, 98, 52, 0.95),
    0 0 7px 1px rgba(206, 132, 74, 0.55),
    0 12px 22px rgba(0, 0, 0, 0.3);
}

.pack.is-flipped .pack-turn { animation: pack-turn-over var(--flip-time) var(--flip-wait); }
.pack.is-unflipped .pack-turn { animation: pack-turn-back var(--flip-time) var(--flip-wait); }

/* Turn to edge-on, swap the picture, then finish the turn. No 3D layers
   are kept around while the cards are at rest. */
@keyframes pack-turn-over {
  0% { transform: perspective(1000px) rotateY(0deg); animation-timing-function: cubic-bezier(0.55, 0, 0.9, 0.6); }
  50% { transform: perspective(1000px) rotateY(90deg); }
  50.01% { transform: perspective(1000px) rotateY(-90deg); animation-timing-function: cubic-bezier(0.1, 0.4, 0.3, 1); }
  100% { transform: perspective(1000px) rotateY(0deg); }
}

@keyframes pack-turn-back {
  0% { transform: perspective(1000px) rotateY(0deg); animation-timing-function: cubic-bezier(0.55, 0, 0.9, 0.6); }
  50% { transform: perspective(1000px) rotateY(-90deg); }
  50.01% { transform: perspective(1000px) rotateY(90deg); animation-timing-function: cubic-bezier(0.1, 0.4, 0.3, 1); }
  100% { transform: perspective(1000px) rotateY(0deg); }
}

/* Lighter inner line of the copper rim. */
.pack-turn::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 2;
  border: 1px solid rgba(236, 190, 142, 0.8);
  border-radius: inherit;
  pointer-events: none;
}

/* Warm glow for the lifted card. It fades in so nothing repaints. */
.pack-turn::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  border-radius: inherit;
  box-shadow:
    0 0 0 1px rgba(214, 150, 96, 1),
    0 0 12px 2px rgba(220, 148, 86, 0.7),
    0 22px 34px rgba(0, 0, 0, 0.42);
  opacity: 0;
  transition: opacity 0.3s ease;
  pointer-events: none;
}

.pack-card.is-hot .pack-turn::after { opacity: 1; }

.pack-face {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: block;
  width: 100%;
  max-width: none;
  height: 100%;
  object-fit: cover;
  border-radius: inherit;
  /* The picture changes at the halfway point, when the card is edge-on. */
  transition: visibility 0s linear calc(var(--flip-wait) + var(--flip-time) / 2);
  -webkit-user-drag: none;
}

.pack-back { visibility: hidden; }
.pack.is-flipped .pack-front { visibility: hidden; }
.pack.is-flipped .pack-back { visibility: visible; }

/* One line of help for a mouse, another for a finger. */
.pack-for-tap { display: none; }

@media (hover: none), (pointer: coarse) {
  .pack-for-hover { display: none; }
  .pack-for-tap { display: inline; }
}

@media (max-width: 800px) {
  .pack {
    --card-w: min(1.5in, 22vw, calc(34vh * 2.75 / 4.75));
  }
}

@media (prefers-reduced-motion: reduce) {
  .pack-card, .pack-shadow, .pack-face, .pack-turn::after { transition: none !important; }
  .pack-turn { animation: none !important; }
}
