/*
 * Red Stamp physical motion sidecar.
 *
 * Load after styles.css. The parent can use the existing .stamp-hit hook or
 * call RedStampStampMotion.trigger(".security-desk"). No markup is required;
 * the companion script creates the temporary ink impression.
 */

:root {
  --stamp-motion-red: #c9362c;
  --stamp-motion-red-ink: rgba(187, 39, 34, 0.78);
}

/* One short recoil for the desk, synchronized with the stamp's contact. */
.security-desk.stamp-hit,
.security-desk.stamp-motion-active {
  isolation: isolate;
  transform-origin: 50% 100%;
  animation: stamp-motion-desk-jolt 820ms cubic-bezier(0.22, 0.75, 0.28, 1) both;
  will-change: transform;
}

/* The existing red-stamp artwork supplies the physical object. */
.security-desk.stamp-hit .desk-stamp-art,
.security-desk.stamp-motion-active .desk-stamp-art {
  transform-origin: 50% 92%;
  animation: stamp-motion-wind-up-impact 820ms cubic-bezier(0.2, 0.82, 0.28, 1) both;
  will-change: transform, filter;
}

/* The document is created in the desk panel so the stamp has a surface to hit. */
.security-desk .stamp-motion-document {
  position: absolute;
  right: 9%;
  bottom: 5px;
  z-index: 1;
  display: block;
  width: clamp(96px, 10vw, 148px);
  height: clamp(58px, 6.2vw, 78px);
  overflow: hidden;
  border: 1px solid rgba(198, 173, 128, 0.62);
  border-radius: 1px;
  background:
    linear-gradient(135deg, rgba(255, 245, 211, 0.98), rgba(202, 183, 142, 0.98)),
    #e7d4a9;
  box-shadow: 0 7px 12px rgba(0, 0, 0, 0.48), inset 0 0 0 1px rgba(255, 250, 221, 0.42);
  color: #382b21;
  opacity: 0;
  pointer-events: none;
  transform: translate3d(0, 24px, 0) rotate(-8deg) scale(0.82);
  transform-origin: 50% 100%;
  filter: saturate(0.84) contrast(0.96);
}

.security-desk .stamp-motion-document::before,
.security-desk .stamp-motion-document::after {
  position: absolute;
  content: "";
  pointer-events: none;
}

.security-desk .stamp-motion-document::before {
  top: 0;
  right: 0;
  width: 16px;
  height: 16px;
  border-bottom: 1px solid rgba(116, 88, 62, 0.35);
  border-left: 1px solid rgba(116, 88, 62, 0.35);
  background: linear-gradient(135deg, rgba(255, 246, 214, 0.1) 47%, rgba(124, 96, 64, 0.28) 49% 53%, rgba(242, 224, 182, 0.86) 55%);
}

.security-desk .stamp-motion-document::after {
  inset: 5px;
  border: 1px solid rgba(116, 88, 62, 0.22);
  box-shadow: inset 0 0 0 1px rgba(255, 251, 226, 0.24);
}

.stamp-motion-document-fold,
.stamp-motion-document-header,
.stamp-motion-document-title,
.stamp-motion-document-lines,
.stamp-motion-document-seal,
.stamp-motion-document-case,
.stamp-motion-document-footer {
  position: absolute;
  z-index: 1;
  display: block;
}

.stamp-motion-document-header {
  top: 8px;
  left: 9px;
  max-width: calc(100% - 28px);
  overflow: hidden;
  color: #6a3028;
  font-family: "Courier New", monospace;
  font-size: 6px;
  font-weight: 700;
  letter-spacing: 0.08em;
  line-height: 1;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stamp-motion-document-title {
  top: 20px;
  left: 9px;
  color: #34261e;
  font-family: "Courier New", monospace;
  font-size: clamp(7px, 0.7vw, 10px);
  font-weight: 700;
  letter-spacing: 0.04em;
  line-height: 1;
}

.stamp-motion-document-lines {
  top: 34px;
  left: 9px;
  width: 52%;
  height: 17px;
  background: repeating-linear-gradient(to bottom, rgba(69, 54, 41, 0.4) 0 1px, transparent 1px 5px);
  opacity: 0.76;
}

.stamp-motion-document-seal {
  right: 10px;
  bottom: 12px;
  display: grid;
  width: 24px;
  height: 24px;
  place-items: center;
  border: 1px solid rgba(142, 38, 31, 0.72);
  border-radius: 50%;
  color: rgba(142, 38, 31, 0.76);
  font-family: Georgia, serif;
  font-size: 13px;
  font-weight: 700;
  transform: rotate(-12deg);
}

.stamp-motion-document-case {
  bottom: 7px;
  left: 9px;
  color: #775945;
  font-family: "Courier New", monospace;
  font-size: 6px;
  letter-spacing: 0.08em;
}

.stamp-motion-document-footer {
  right: 9px;
  bottom: 5px;
  color: rgba(71, 51, 35, 0.62);
  font-family: "Courier New", monospace;
  font-size: 5px;
  letter-spacing: 0.04em;
}

.security-desk .stamp-motion-document-issued {
  opacity: 0.94;
  transform: translate3d(0, 0, 0) rotate(-4deg) scale(1);
  filter: saturate(0.84) contrast(0.96);
}

.security-desk .stamp-motion-document-active {
  animation: stamp-motion-document-reveal 820ms cubic-bezier(0.18, 0.82, 0.28, 1) both;
  will-change: opacity, transform, filter;
}

/* The script appends this to .desk-panel so the impression lands under the art. */
.security-desk .stamp-motion-ink {
  position: absolute;
  right: 22%;
  bottom: 19%;
  z-index: 3;
  display: block;
  width: clamp(34px, 4.6vw, 52px);
  height: clamp(18px, 2.7vw, 30px);
  border: 2px solid #7e211e;
  border-radius: 47% 53% 42% 58%;
  background:
    radial-gradient(ellipse at 48% 47%, var(--stamp-motion-red-ink) 0 42%, rgba(160, 30, 29, 0.48) 67%, transparent 79%),
    repeating-linear-gradient(103deg, rgba(218, 62, 49, 0.32) 0 2px, rgba(122, 28, 27, 0.2) 2px 4px);
  box-shadow: inset 0 0 0 1px rgba(235, 102, 78, 0.18);
  opacity: 0;
  pointer-events: none;
  transform: translate3d(0, 4px, 0) rotate(-5deg) scale(0.18, 0.28);
  transform-origin: 50% 50%;
}

.security-desk .stamp-motion-ink-issued {
  opacity: 0.62;
  transform: translate3d(0, 1px, 0) rotate(-4deg) scale(1, 0.7);
}

.security-desk .desk-panel {
  position: relative;
  overflow: visible;
}

/* Generated desk-level impact plate. It is deliberately much larger than the
 * normal stamp sprite so the player can read the contact, paper, and fresh ink
 * even in the full-room view. The dark plate blends into the desk. */
.security-desk .stamp-motion-impact {
  position: absolute;
  right: -88px;
  bottom: -106px;
  z-index: 7;
  display: block;
  width: clamp(230px, 22vw, 340px);
  height: auto;
  aspect-ratio: 3 / 2;
  object-fit: contain;
  object-position: center;
  opacity: 0;
  pointer-events: none;
  transform: translate3d(18px, 30px, 0) rotate(-7deg) scale(0.42);
  transform-origin: 68% 73%;
  filter: saturate(1.16) contrast(1.1) drop-shadow(0 17px 15px rgba(0, 0, 0, 0.72));
  mix-blend-mode: screen;
}

.security-desk .stamp-motion-impact-active {
  animation: stamp-motion-impact-plate 960ms cubic-bezier(0.16, 0.78, 0.23, 1) both;
  will-change: opacity, transform;
}

.security-desk .stamp-motion-ink::before,
.security-desk .stamp-motion-ink::after {
  position: absolute;
  content: "";
  pointer-events: none;
}

.security-desk .stamp-motion-ink::before {
  inset: 3px 5px;
  border: 1px solid rgba(241, 104, 78, 0.45);
  border-radius: 51% 49% 55% 45%;
}

.security-desk .stamp-motion-ink::after {
  inset: 7px 10px;
  border-top: 1px solid rgba(111, 22, 23, 0.52);
  border-bottom: 1px solid rgba(235, 86, 65, 0.31);
  transform: rotate(-2deg);
}

.security-desk.stamp-hit .stamp-motion-ink,
.security-desk.stamp-motion-active .stamp-motion-ink {
  animation: stamp-motion-ink-bloom 780ms cubic-bezier(0.19, 0.8, 0.28, 1) both;
}

/* The red desk edge carries a small, hard-edged authority response. */
.security-desk.stamp-hit .desk-edge,
.security-desk.stamp-motion-active .desk-edge {
  animation: stamp-motion-authority-edge 820ms ease-out both;
}

/* Existing decision controls receive a restrained authorization rule. */
.scene-decision.admit.stamp-motion-authority,
.decision-button.admit.stamp-motion-authority {
  position: relative;
  animation: stamp-motion-authority 820ms ease-out both;
}

.scene-decision.admit.stamp-motion-authority::after,
.decision-button.admit.stamp-motion-authority::after {
  position: absolute;
  bottom: 3px;
  left: 8px;
  width: 0;
  height: 1px;
  background: var(--stamp-motion-red);
  content: "";
  opacity: 0;
  pointer-events: none;
  transform-origin: left center;
  animation: stamp-motion-authority-rule 820ms ease-out both;
}

/* Useful when the host keeps the original, non-full-screen gate visible. */
.entry-frame.stamp-motion-gate::before,
.entry-frame.stamp-motion-gate::after {
  animation: stamp-motion-gate-response 820ms ease-out both;
}

@keyframes stamp-motion-wind-up-impact {
  0% {
    filter: drop-shadow(0 7px 6px rgba(0, 0, 0, 0.62));
    transform: translate3d(0, 0, 0) rotate(7deg) scale(1);
  }

  10% {
    filter: drop-shadow(0 5px 5px rgba(0, 0, 0, 0.7));
    transform: translate3d(-2px, 2px, 0) rotate(8deg) scale(0.985, 0.98);
  }

  23% {
    filter: drop-shadow(0 13px 8px rgba(0, 0, 0, 0.5));
    transform: translate3d(-3px, -21px, 0) rotate(-2deg) scale(1.04);
  }

  38% {
    filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.76));
    transform: translate3d(-25px, 15px, 0) rotate(8deg) scale(0.94, 0.96);
  }

  46% {
    filter: drop-shadow(0 6px 5px rgba(0, 0, 0, 0.66));
    transform: translate3d(-27px, 21px, 0) rotate(7deg) scale(0.91, 0.93);
  }

  56% {
    transform: translate3d(-22px, 9px, 0) rotate(6deg) scale(0.97, 0.98);
  }

  72% {
    filter: drop-shadow(0 7px 6px rgba(0, 0, 0, 0.62));
    transform: translate3d(-7px, -2px, 0) rotate(7deg) scale(1.01);
  }

  100% {
    filter: drop-shadow(0 7px 6px rgba(0, 0, 0, 0.62));
    transform: translate3d(0, 0, 0) rotate(7deg) scale(1);
  }
}

@keyframes stamp-motion-impact-plate {
  0% {
    opacity: 0;
    transform: translate3d(18px, 30px, 0) rotate(-7deg) scale(0.42);
  }

  23% {
    opacity: 0.96;
    transform: translate3d(4px, 8px, 0) rotate(-4deg) scale(0.82);
  }

  42% {
    opacity: 1;
    transform: translate3d(0, 0, 0) rotate(-2deg) scale(1.08);
  }

  55% {
    opacity: 0.98;
    transform: translate3d(0, 0, 0) rotate(-2deg) scale(0.98);
  }

  78% {
    opacity: 0.34;
    transform: translate3d(-3px, -4px, 0) rotate(-1deg) scale(0.94);
  }

  100% {
    opacity: 0;
    transform: translate3d(-4px, -8px, 0) rotate(0deg) scale(0.9);
  }
}

@keyframes stamp-motion-document-reveal {
  0% {
    opacity: 0;
    transform: translate3d(0, 24px, 0) rotate(-8deg) scale(0.82);
    filter: saturate(0.65) contrast(0.86) brightness(0.75);
  }

  19% {
    opacity: 0.96;
    transform: translate3d(-4px, 5px, 0) rotate(-6deg) scale(0.96);
    filter: saturate(0.8) contrast(0.92) brightness(0.9);
  }

  34%,
  100% {
    opacity: 0.94;
    transform: translate3d(0, 0, 0) rotate(-4deg) scale(1);
    filter: saturate(0.84) contrast(0.96) brightness(1);
  }
}

@keyframes stamp-motion-desk-jolt {
  0%,
  22% {
    transform: translate3d(0, 0, 0) rotate(0deg);
  }

  28% {
    transform: translate3d(-2px, 1px, 0) rotate(-0.18deg);
  }

  33% {
    transform: translate3d(2px, -1px, 0) rotate(0.15deg);
  }

  39% {
    transform: translate3d(-1px, 0, 0) rotate(-0.06deg);
  }

  48%,
  100% {
    transform: translate3d(0, 0, 0) rotate(0deg);
  }
}

@keyframes stamp-motion-ink-bloom {
  0%,
  31% {
    opacity: 0;
    transform: translate3d(0, 4px, 0) rotate(-5deg) scale(0.18, 0.28);
  }

  43% {
    opacity: 0.9;
    transform: translate3d(0, 0, 0) rotate(-3deg) scale(1.12, 0.72);
  }

  51% {
    opacity: 0.84;
    transform: translate3d(0, 0, 0) rotate(-4deg) scale(0.98, 0.84);
  }

  67% {
    opacity: 0.68;
    transform: translate3d(0, 1px, 0) rotate(-4deg) scale(1.04, 0.72);
  }

  100% {
    opacity: 0.62;
    transform: translate3d(0, 1px, 0) rotate(-4deg) scale(1, 0.7);
  }
}

@keyframes stamp-motion-authority-edge {
  0%,
  100% {
    opacity: 1;
    transform: scaleX(1);
  }

  34% {
    opacity: 0.78;
    transform: scaleX(0.985);
  }

  47% {
    opacity: 1;
    transform: scaleX(1.018);
  }
}

@keyframes stamp-motion-authority {
  0%,
  100% {
    border-color: rgba(126, 164, 135, 0.55);
    box-shadow: none;
  }

  38% {
    border-color: rgba(205, 58, 44, 0.84);
    box-shadow: inset 0 0 0 1px rgba(205, 58, 44, 0.32);
  }

  60% {
    border-color: rgba(150, 174, 133, 0.68);
    box-shadow: inset 0 0 0 1px rgba(150, 174, 133, 0.16);
  }
}

@keyframes stamp-motion-authority-rule {
  0%,
  18% {
    width: 0;
    opacity: 0;
  }

  42%,
  70% {
    width: calc(100% - 16px);
    opacity: 0.72;
  }

  100% {
    width: calc(100% - 16px);
    opacity: 0;
  }
}

@keyframes stamp-motion-gate-response {
  0%,
  100% {
    filter: brightness(1);
    opacity: 0.85;
  }

  42% {
    filter: brightness(1.28);
    opacity: 1;
  }

  62% {
    filter: brightness(1.06);
    opacity: 0.9;
  }
}

@media (prefers-reduced-motion: reduce) {
  .security-desk .stamp-motion-impact-active {
    animation: none;
    opacity: 0.96;
    transform: translate3d(0, 0, 0) rotate(-2deg) scale(0.96);
  }

  .security-desk.stamp-hit,
  .security-desk.stamp-motion-active {
    animation: none !important;
    transform: none !important;
    will-change: auto;
  }

  .security-desk.stamp-hit .desk-stamp-art,
  .security-desk.stamp-motion-active .desk-stamp-art {
    animation: none !important;
    filter: drop-shadow(0 7px 6px rgba(0, 0, 0, 0.62)) !important;
    transform: rotate(7deg) !important;
    will-change: auto;
  }

  .security-desk .stamp-motion-document-active,
  .security-desk .stamp-motion-document-issued {
    animation: none !important;
    opacity: 0.94;
    filter: saturate(0.84) contrast(0.96);
    transform: translate3d(0, 0, 0) rotate(-4deg) scale(1);
    will-change: auto;
  }

  .security-desk.stamp-hit .stamp-motion-ink,
  .security-desk.stamp-motion-active .stamp-motion-ink {
    animation: none !important;
    opacity: 0.62;
    transform: translate3d(0, 1px, 0) rotate(-4deg) scale(1, 0.7);
  }

  .security-desk .stamp-motion-ink-issued {
    opacity: 0.62;
    transform: translate3d(0, 1px, 0) rotate(-4deg) scale(1, 0.7);
  }

  .security-desk.stamp-hit .desk-edge,
  .security-desk.stamp-motion-active .desk-edge,
  .scene-decision.admit.stamp-motion-authority,
  .decision-button.admit.stamp-motion-authority,
  .entry-frame.stamp-motion-gate::before,
  .entry-frame.stamp-motion-gate::after {
    animation: none !important;
  }

  .scene-decision.admit.stamp-motion-authority,
  .decision-button.admit.stamp-motion-authority {
    border-color: rgba(150, 174, 133, 0.68);
    box-shadow: inset 0 0 0 1px rgba(150, 174, 133, 0.16);
  }

  .scene-decision.admit.stamp-motion-authority::after,
  .decision-button.admit.stamp-motion-authority::after {
    width: calc(100% - 16px);
    background: var(--stamp-motion-red);
    opacity: 0.62;
    animation: none !important;
  }
}
