/* ---------- The Heartbeat Deck (public/js/pulse-deck.js) ----------
   Card faces, the card back and the deck stack. Every rule is scoped to .hb-card, .hb-stack or the
   sprite. A card takes its height from its host's --h; width, radius and every detail of the
   artwork follow from it, so the same card reads from a 24 px stack to a 200 px close-up.

   Tokens (optional; set them on any ancestor, the defaults are the deck's own palette):
     --hb-card-paper   card paper            (default --hb-silver-2, #f1edf8)
     --hb-card-red     hearts and diamonds   (#c8172f)
     --hb-card-ink     clubs and spades      (#121216)
     --hb-card-silver  frames, court detail  (#9ea3ad)
     --hb-card-jewel   the Queen's jewel and the joker's index (#c8172f)
     --hb-card-rim     the hairline round the face (#16161c at 22%)
     --hb-card-sheen   opacity of the white gloss from the top (1; 0 on a black deck)
     --hb-card-glow    opacity of the violet light from the top (0; 1 on a black deck)
     --hb-back-base    back field            (#111114)
     --hb-back-line    back line work        (--hb-silver, #cfc7e6)
     --hb-back-accent  back accents: the rosettes, the inner lines (#c13cff)
   The Obsidian Violet deck (player-rami.css) is these tokens on one host: black paper, violet
   hearts and diamonds, silver clubs and spades, the glow instead of the gloss.
     --hb-card-shadow  the contact shadow under each side
     --hb-card-dim     opacity of the drawn sides (dim a card with this, not with opacity on the
                       card: opacity or filter on a .hb-card flattens its 3D flip in Chromium, and
                       a turned card would then show its face mirrored)
     --hb-card-filter  filter on the drawn sides (same reason) */

.hbd-sprite { position: absolute; width: 0; height: 0; overflow: hidden; pointer-events: none; }

/* Colour roles inside the sprite: document rules reach the <use> copies, and var() resolves on the
   card that shows them, so a host can recolour one card, a hand or the whole table. */
.hbd-paper { color: var(--hb-card-paper, var(--hb-silver-2, #f1edf8)); }
.hbd-red { color: var(--hb-card-red, #c8172f); }
.hbd-ink { color: var(--hb-card-ink, #121216); }
.hbd-silver { color: var(--hb-card-silver, #9ea3ad); }
.hbd-jewel { color: var(--hb-card-jewel, #c8172f); }
.hbd-base { color: var(--hb-back-base, #111114); }
.hbd-line { color: var(--hb-back-line, var(--hb-silver, #cfc7e6)); }
.hbd-accent { color: var(--hb-back-accent, #c13cff); }
.hbd-rim { color: var(--hb-card-rim, #16161c); }
.hbd-sheen { opacity: var(--hb-card-sheen, 1); }
.hbd-glow { opacity: var(--hb-card-glow, 0); }

/* The card. `.card.hb-card` outranks the site's `.card` and `.card.back` (style.css) for the box,
   background, border, shadow and decoration; `.pa .card` keeps the same sizing formula. */
.card.hb-card {
  position: relative; display: block; flex: none;
  width: calc(var(--h, 96px) * .7); height: var(--h, 96px);
  padding: 0; border: 0; border-radius: calc(var(--h, 96px) * .1);
  background: none; box-shadow: none; overflow: visible;
  transform-style: preserve-3d;
  -webkit-appearance: none; appearance: none;
  -webkit-tap-highlight-color: transparent; touch-action: manipulation;
  /* no CSS motion of its own: flips and travel are the motion engine's (WAAPI), hover and
     selection transitions belong to the host */
  animation: none; transition: none;
}
.card.hb-card::before, .card.hb-card::after { content: none; }
.hb-card .corner, .hb-card .face { display: none; }

.hb-card > .hb-front, .hb-card > .hb-back {
  position: absolute; inset: 0; display: block; border-radius: inherit; overflow: hidden;
  -webkit-backface-visibility: hidden; backface-visibility: hidden;
  box-shadow: var(--hb-card-shadow, 0 1px 2px rgba(0, 0, 0, .5), 0 6px 14px -6px rgba(0, 0, 0, .7));
  opacity: var(--hb-card-dim, 1); filter: var(--hb-card-filter, none);
}
.hb-card > .hb-back {
  transform: rotateY(180deg);
  /* a light rim, like a card edge catching the light: it keeps a dark back visible on a dark
     table down to 24 px, where the artwork's own frame is finer than a pixel */
  box-shadow: 0 0 0 max(.6px, calc(var(--h, 96px) * .006)) rgba(207, 199, 230, .34),
    var(--hb-card-shadow, 0 1px 2px rgba(0, 0, 0, .5), 0 6px 14px -6px rgba(0, 0, 0, .7));
}
/* Turned over: the motion engine animates this flip on its ghosts and on revealed cards. */
.card.hb-card[data-side="back"] { transform: rotateY(180deg); }
/* A back-only card (HBDeck.back) has no front to turn to: its back is always the visible side,
   whatever it is rotated or faded by. The back artwork is symmetric, so seen from behind it is
   the same drawing. */
.hb-card.back > .hb-back { transform: none; -webkit-backface-visibility: visible; backface-visibility: visible; }
.hb-card svg { display: block; width: 100%; height: 100%; overflow: hidden; }

/* A skinned card (HBDeck skins, e.g. the Chkobba deck): the deck's own artwork, one image per side,
   on white card stock with a fine dark edge and the printed deck's small corners. */
.card.hb-card.hb-skin { border-radius: calc(var(--h, 96px) * .04); }
.hb-card.hb-skin > .hb-front, .hb-card.hb-skin > .hb-back { background: #fff; }
.hb-card.hb-skin > .hb-front, .hb-card.hb-skin > .hb-back {
  box-shadow: inset 0 0 0 max(.6px, calc(var(--h, 96px) * .007)) #1b1b1f,
    var(--hb-card-shadow, 0 1px 2px rgba(0, 0, 0, .5), 0 6px 14px -6px rgba(0, 0, 0, .7));
}
/* the artwork's canvas is the card less a 2-unit margin (85 x 127 of 89 x 131) */
.hb-card.hb-skin img {
  position: absolute; left: 2.25%; top: 1.5%; width: 95.5%; height: 97%;
  display: block; object-fit: contain; pointer-events: none; user-select: none; -webkit-user-drag: none;
}

/* The Rami deck's files carry the whole card, corners included: no inset, a larger corner radius and
   a fine warm edge. */
.card.hb-card.hb-skin-rami { border-radius: calc(var(--h, 96px) * .055); }
.hb-card.hb-skin-rami > .hb-front, .hb-card.hb-skin-rami > .hb-back { background: #fff; box-shadow: inset 0 0 0 max(.6px, calc(var(--h, 96px) * .006)) rgba(30, 20, 40, .35), var(--hb-card-shadow, 0 1px 2px rgba(0, 0, 0, .5), 0 6px 14px -6px rgba(0, 0, 0, .7)); }
.hb-card.hb-skin-rami img { left: 0; top: 0; width: 100%; height: 100%; }

/* ---------- The deck stack (HBDeck.stack) ----------
   One card box; the backs share it and step up and slightly left by --i (0 at the bottom). The
   step scales with the card and is capped, so a small deck stays tidy and a large one stays
   crisp. Each back's light rim shows where the card above leaves it uncovered, so the layers read
   as card edges, and only the bottom card casts the contact shadow. */
.hb-stack { position: relative; display: grid; width: fit-content; height: fit-content; }
.hb-stack > .card.hb-card.back {
  --hb-step: clamp(.6px, calc(var(--h, 96px) * .014), 2px);
  grid-area: 1 / 1; position: relative; top: 0; left: 0; margin: 0;
  transform: translate(calc(var(--i, 0) * var(--hb-step) * -.45), calc(var(--i, 0) * var(--hb-step) * -1));
}
.hb-stack > .card.hb-card.back > .hb-back {
  box-shadow: 0 0 0 max(.6px, calc(var(--h, 96px) * .006)) rgba(207, 199, 230, .34), 0 .5px .5px rgba(0, 0, 0, .7);
}
.hb-stack > .card.hb-card.back:first-child > .hb-back {
  box-shadow: 0 0 0 max(.6px, calc(var(--h, 96px) * .006)) rgba(207, 199, 230, .34),
    var(--hb-card-shadow, 0 1px 2px rgba(0, 0, 0, .5), 0 6px 14px -6px rgba(0, 0, 0, .7));
}

/* ---------- The 3D stack (HBDeck.stack(n, { depth: true })) ----------
   On the Obsidian Pulse stage the deck lies on the CSS 3D table plane, so its thickness is real:
   every back is a layer one card thickness (0.6 px of the plane) above the one below, exactly
   aligned, and the stack keeps 3D so the camera sees the layers. From the camera only the near
   edge of each lower card shows, a strip finer than a pixel, so each lower back wears a pale inset
   line on its edges: together the strips read as the pale side of a deck, card edges, not a smear
   of back artwork. The top card (the last child, the one that leaves first) keeps its full art and
   rim; the bottom card keeps the contact shadow; no other layer casts one. */
.hb-stack[data-depth] { transform-style: preserve-3d; -webkit-transform-style: preserve-3d; }
.hb-stack[data-depth] > .card.hb-card.back {
  --hb-thick: .6px;
  transform: translate3d(0, 0, calc(var(--i, 0) * var(--hb-thick)));
}
.hb-stack[data-depth] > .card.hb-card.back > .hb-back {
  box-shadow: 0 0 0 max(.6px, calc(var(--h, 96px) * .006)) rgba(207, 199, 230, .34);
}
.hb-stack[data-depth] > .card.hb-card.back:not(:last-child) > .hb-back {
  box-shadow: inset 0 0 0 1.2px rgba(214, 216, 222, .46), 0 0 0 .5px rgba(0, 0, 0, .35);
}
.hb-stack[data-depth] > .card.hb-card.back:first-child > .hb-back {
  box-shadow: inset 0 0 0 1.2px rgba(214, 216, 222, .46),
    var(--hb-card-shadow, 0 1px 2px rgba(0, 0, 0, .5), 0 6px 14px -6px rgba(0, 0, 0, .7));
}
.hb-stack[data-depth] > .card.hb-card.back:first-child:last-child > .hb-back {
  box-shadow: 0 0 0 max(.6px, calc(var(--h, 96px) * .006)) rgba(207, 199, 230, .34),
    var(--hb-card-shadow, 0 1px 2px rgba(0, 0, 0, .5), 0 6px 14px -6px rgba(0, 0, 0, .7));
}

/* The side of the stack: a solid pale band, the cards' edges seen from the camera. The layers'
   own edge strips are a third of a plane px each (a card is 0.6 thick and the camera sees its
   side at about 32 degrees), which a 1x screen loses below a device pixel, so the stack also
   wears four real faces standing on the bottom card's edges, as tall as the stack (--n cards,
   the count HBDeck.stack draws, times the thickness): paper-coloured, a touch darker toward the
   felt, with a faint line per card for a close look. Each face shows only from its own side
   (backface hidden), so the camera sees the near face and the side it looks along, and a deck of
   thirty keeps a side of three to six screen px at the table's live scales. The faces lie in the
   stack's 3D context (a pseudo-element of a preserve-3d box is one of its 3D children). */
.hb-stack[data-depth]::before, .hb-stack[data-depth]::after,
.hb-stack[data-depth] > .card.hb-card.back:first-child::before,
.hb-stack[data-depth] > .card.hb-card.back:first-child::after {
  content: ""; position: absolute; display: block; margin: 0; padding: 0; border: 0;
  --hb-side: calc(var(--n, 1) * var(--hb-thick, .6px));
  background:
    repeating-linear-gradient(to bottom, rgba(255, 255, 255, .12) 0 .25px, transparent .25px .6px),
    linear-gradient(to bottom, #d9dbe0, #b4b7be);
  -webkit-backface-visibility: hidden; backface-visibility: hidden;
  pointer-events: none;
}
/* near: standing on the bottom edge, facing the player */
.hb-stack[data-depth]::before {
  left: 0; bottom: 0; width: 100%; height: var(--hb-side);
  transform-origin: 50% 100% 0; transform: rotateX(-90deg);
}
/* far: standing on the top edge, facing away */
.hb-stack[data-depth]::after {
  left: 0; top: 0; width: 100%; height: var(--hb-side);
  transform-origin: 50% 0 0; transform: rotateX(90deg);
}
/* left and right: standing on the side edges, facing out */
.hb-stack[data-depth] > .card.hb-card.back:first-child::before {
  left: 0; top: 0; width: var(--hb-side); height: 100%;
  transform-origin: 0 50% 0; transform: rotateY(-90deg);
}
.hb-stack[data-depth] > .card.hb-card.back:first-child::after {
  right: 0; top: 0; width: var(--hb-side); height: 100%;
  transform-origin: 100% 50% 0; transform: rotateY(90deg);
}
/* The Lite performance profile (.pa-lite on the felt): a stack is its card layers alone, without
   the four standing faces (each a 3D-transformed gradient the browser composites separately). */
.pa-felt.pa-lite .hb-stack[data-depth]::before, .pa-felt.pa-lite .hb-stack[data-depth]::after,
.pa-felt.pa-lite .hb-stack[data-depth] > .card.hb-card.back:first-child::before,
.pa-felt.pa-lite .hb-stack[data-depth] > .card.hb-card.back:first-child::after { display: none; }

@media (forced-colors: active) {
  .hb-card { forced-color-adjust: none; }
}
