/* THE TAPE and THE PILE.
   The materials come from /ui/base.css: .paper .torn-bottom .mono .print .sticker.
   This file adds only what the two parts need on top: layout, print-in-steps, deal and slam.
   Link it after base.css. Root defaults sit in :where(), so any page rule wins over them. */

/* ── THE TAPE ──────────────────────────────────────────────────────────────── */

.tape {
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

:where(.tape) {
  /* thermal print fades: the newest line has the full ink, older lines a lighter one */
  --tape-ink: var(--print, #2b2b33);
  --tape-ink-2: #4c4c52;
  --tape-ink-3: #6e6d71;
  max-height: 100%; /* never longer than the box the page gives it: from there it scrolls inside */
  padding: 10px 12px 18px;
  color: var(--tape-ink);
}

@supports (color: color-mix(in srgb, red 50%, white)) {
  :where(.tape) {
    --tape-ink-2: color-mix(in srgb, var(--tape-ink) 84%, var(--paper, #fbfaf5));
    --tape-ink-3: color-mix(in srgb, var(--tape-ink) 68%, var(--paper, #fbfaf5));
  }
}

/* the part of the strip you see. It only scrolls when the page fixed the height. */
.tape__window {
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  scrollbar-width: none;
}

.tape__window::-webkit-scrollbar {
  display: none;
}

.tape__roll {
  margin: 0;
  padding: 0;
  list-style: none;
  font-family: var(--f-mono, 'Space Mono', ui-monospace, monospace);
  font-size: var(--tape-size, clamp(12px, 3.1vw, 15px));
  font-weight: 400;
  font-variant-ligatures: none;
  line-height: 1.45;
  text-transform: none; /* wallets and hashes keep their case */
  white-space: pre-wrap; /* two spaces are the columns of a receipt */
  overflow-wrap: anywhere;
}

.tape__line {
  min-height: 1.45em; /* an empty line is a blank row of paper */
  margin: 0;
  padding: 1px 0;
  color: var(--tape-ink-3);
}

.tape__line:nth-child(-n + 6) {
  color: var(--tape-ink-2);
}

.tape__line:first-child {
  color: var(--tape-ink);
}

.tape__line.tape__line--red {
  color: var(--red, #d4202a);
}

/* double-height print: twice the type size, squeezed back to the normal width.
   It looks like scaleY(2), and a bold line that wraps still takes the right room. */
.tape__line--bold .tape__ink {
  display: block;
  width: 200%;
  font-size: 2em;
  font-weight: 700;
  transform: scaleX(0.5);
  transform-origin: 0 50%;
}

/* a new line prints in 8 steps, left to right, like a print head */
.tape__line.is-new {
  animation: tape-print 240ms steps(8, end) backwards;
  animation-delay: var(--tape-wait, 0ms);
}

@keyframes tape-print {
  from {
    clip-path: inset(0 100% 0 0);
  }
  to {
    clip-path: inset(0 0 0 0);
  }
}

/* ── THE PILE ──────────────────────────────────────────────────────────────── */

.pile {
  position: relative;
  min-width: 0;
}

:where(.pile) {
  --pile-cols: 1; /* most columns the pile may use */
  --pile-min: 320px; /* a column is never narrower than this (or than the box, on a phone) */
  --pile-card: 460px; /* a past card is a mini card: it never grows wider than this */
  --pile-shift: 4px; /* how far a card may land left or right of its place */
  --pile-lift: 3px; /* and above or below it */
  --pile-room: 8px; /* room for the hard shadow of a card (it falls right and down) */
  --pile-stagger: 0px; /* side by side, columns do not start on one line */
}

/* marks the top of the pile for the in-view check. Never seen. */
.pile__top {
  position: absolute;
  top: 0;
  left: 0;
  width: 1px;
  height: 100%;
  max-height: 220px;
  visibility: hidden;
  pointer-events: none;
}

/* up to --pile-cols columns, fewer when the box is narrow, never wider than the box */
.pile__list {
  display: grid;
  grid-template-columns: repeat(
    auto-fill,
    minmax(min(100%, max(var(--pile-min), (100% - 1px) / var(--pile-cols))), 1fr)
  );
  align-items: start;
  margin: 0;
  padding: 0;
  list-style: none;
}

/* one place on the pile. content-visibility cuts everything at the edge of this box,
   so the padding is the room a crooked card needs: its step aside, its turned corners (6px),
   its shadow, and a little more above and below for strips and stamps that stick out. */
.pile__slot {
  box-sizing: border-box;
  min-width: 0;
  padding: calc(var(--pile-lift) + 10px) calc(var(--pile-shift) + 6px + var(--pile-room))
    calc(var(--pile-lift) + 8px + var(--pile-room)) calc(var(--pile-shift) + 6px);
  content-visibility: auto;
  contain-intrinsic-block-size: auto 170px; /* a fixed guess: a measured one fed back into itself and grew without end */
}

/* dealt by hand: --rot --dx --dy come from a hash of the drop id, so a card always lands the same way */
.pile__deal {
  max-width: var(--pile-card);
  margin-inline: auto;
  transform: translate(calc(var(--dx, 0) * var(--pile-shift)), calc(var(--dy, 0) * var(--pile-lift)))
    rotate(calc(var(--rot, 0) * 1deg));
}

.pile__more {
  display: flex;
  justify-content: center;
  padding: 14px 12px 24px;
}

.pile__empty {
  margin: 0;
  padding: 8px 10px 16px;
}

@media (max-width: 899.98px) {
  /* a card that slams in is wider than a phone for a few frames: cut it at the pile, do not widen the page */
  .pile {
    overflow-x: clip;
  }
}

@media (min-width: 900px) {
  :where(.pile) {
    --pile-cols: 2;
    --pile-shift: 9px;
    --pile-lift: 7px;
    --pile-room: 12px;
    --pile-stagger: 22px;
  }
}

@media (min-width: 900px) and (max-width: 1299.98px) {
  .pile__slot:nth-child(2n) {
    margin-top: var(--pile-stagger);
  }
}

@media (min-width: 1300px) {
  :where(.pile) {
    --pile-cols: 3;
  }

  .pile__slot:nth-child(3n + 2) {
    margin-top: var(--pile-stagger);
  }

  .pile__slot:nth-child(3n) {
    margin-top: calc(var(--pile-stagger) * 0.4);
  }
}

/* add(): the new card slams on top */
.pile__slot.is-slam {
  z-index: 2;
  content-visibility: visible; /* or the card is cut while it is bigger than its place */
  animation: pile-slam 160ms cubic-bezier(0.7, 0, 1, 0.5) both;
}

@keyframes pile-slam {
  from {
    transform: scale(1.25);
  }
  to {
    transform: none;
  }
}

/* DEAL MORE: the next cards are thrown down one after the other */
.pile__slot.is-dealt {
  content-visibility: visible;
  animation: pile-deal 150ms cubic-bezier(0.7, 0, 1, 0.5) both;
  animation-delay: calc(var(--i, 0) * 34ms);
}

@keyframes pile-deal {
  0% {
    transform: translate(12px, -24px) rotate(3deg) scale(1.1);
    visibility: hidden;
  }
  1% {
    visibility: visible;
  }
  100% {
    transform: none;
    visibility: visible;
  }
}

@media (prefers-reduced-motion: reduce) {
  .tape__line.is-new,
  .pile__slot.is-slam,
  .pile__slot.is-dealt {
    animation: none;
  }
}
