/* ==========================================================================
   Envelope Float Component (Adapted from FolderFloat reference)
   Smooth 3D flap opening, staggered spring rise, zero-gravity floating,
   Matter.js collision physics, draggable cards, and graceful close.
   ========================================================================== */

:root {
  --ef-open: 520ms;
  --ef-close: 312ms;
  --ef-stagger: 45ms;
  --ef-bounce: 0.3;
  --ef-tilt: 8deg;
  --ef-lift: 26px;
  --ef-drift: 0.5;
  --ef-n: 4;
  --ef-spring: cubic-bezier(0.34, 1.57, 0.64, 1);
  --ef-ease-out: cubic-bezier(0.23, 1, 0.32, 1);
}

/* Ensure envelope wrapper contains interaction zone gracefully */
.process-envelope-wrapper {
  position: relative;
  width: 100%;
  max-width: 1260px;
  margin: 140px auto 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  perspective: 1600px;
  perspective-origin: 50% 40%;
  z-index: 2;
}

/* 3D Envelope Stage: Unified hover/focus trigger */
.env-stage {
  position: relative;
  width: 100%;
  max-width: 660px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  outline: none;
  border-radius: 28px;
  transform-style: preserve-3d;
  transition: max-width 0.75s var(--ef-ease-out);
  z-index: 2;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.env-stage:focus-visible {
  outline: 2px solid #55ddff;
  outline-offset: 8px;
  box-shadow: 0 0 24px rgba(85, 221, 255, 0.4);
}

/* Envelope shell in 3D */
.env-3d {
  position: relative;
  width: 100%;
  height: 420px;
  border-radius: 26px;
  border: 1.5px solid rgba(85, 221, 255, 0.25);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.7);
  transform-style: preserve-3d;
  transition: transform 0.65s var(--ef-ease-out), border-color 0.4s ease;
  overflow: visible !important;
}

/* Flap 3D rotation based on open state */
.env-top-flap {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 48%;
  transform-origin: 50% 0%;
  transform-style: preserve-3d;
  z-index: 8;
  border: none !important;
  pointer-events: none;
  transform: perspective(800px) rotateX(0deg);
  transition: transform var(--ef-open) var(--ef-ease-out);
}

.env-stage[data-open] .env-top-flap {
  transform: perspective(800px) rotateX(-165deg);
}

/* Flap polygon styling */
.env-flap-polygon {
  width: 100%;
  height: 100%;
  border: none !important;
  clip-path: polygon(0 0, 100% 0, 50% 100%);
  background: linear-gradient(180deg, #18264e 0%, #111b38 60%, #091024 100%);
  box-shadow: 0 16px 36px rgba(0, 0, 0, 0.6);
  position: relative;
}

.env-flap-polygon::after {
  content: "";
  position: absolute;
  inset: 0;
  clip-path: polygon(0 0, 100% 0, 50% 100%);
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.09) 0%, transparent 60%);
}

/* Wax seal badge smoothly yields when open */
.env-seal-wrap {
  position: absolute;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 10;
  pointer-events: auto;
  transition: transform 0.5s var(--ef-ease-out), opacity 0.4s ease;
}

.env-stage[data-open] .env-seal-wrap {
  transform: translate(-50%, -120px) scale(0.65);
  opacity: 0;
  pointer-events: none;
}

/* Invisible bridge extending over cards so hover doesn't break */
.env-cards-container {
  position: absolute;
  top: 0;
  left: 50%;
  width: 0;
  height: 0;
  z-index: 12;
  pointer-events: none;
}

.env-stage[data-open] .env-cards-container {
  pointer-events: auto;
}

/* Hover bridge area over cloud */
.env-stage[data-open] .env-cards-container::before {
  content: "";
  position: absolute;
  bottom: -40px;
  left: -580px;
  width: 1160px;
  height: 640px;
  pointer-events: auto;
  z-index: -1;
}

.env-cards-grid {
  position: absolute;
  top: 0;
  left: 0;
  width: 0;
  height: 0;
}

/* Floating Card Base Styling */
.env-stage .env-card,
.env-card {
  position: absolute !important;
  top: 0 !important;
  left: 50% !important;
  width: 290px;
  min-height: 184px;
  margin: 0;
  padding: 22px 22px 20px;
  box-sizing: border-box;
  background: linear-gradient(165deg, rgba(16, 24, 50, 0.94) 0%, rgba(9, 14, 30, 0.97) 100%);
  border: 1.5px solid rgba(132, 103, 255, 0.3);
  border-radius: 20px;
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  box-shadow: 0 14px 36px rgba(2, 6, 20, 0.65), 0 0 18px rgba(132, 103, 255, 0.15), inset 0 1px 1px rgba(255, 255, 255, 0.12);
  display: flex;
  flex-direction: column;
  cursor: pointer;
  outline: none;
  opacity: 0;
  transform: translate(-50%, 60px) scale(0.65);
  transform-origin: 50% 50%;
  pointer-events: none;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  z-index: 15;
  transition:
    transform var(--ef-close) var(--ef-ease-out) calc((var(--ef-n) - 1 - var(--i)) * var(--ef-stagger) * 0.5),
    opacity 180ms ease calc((var(--ef-n) - 1 - var(--i)) * var(--ef-stagger) * 0.5 + var(--ef-close) * 0.4),
    border-color 0.25s ease,
    box-shadow 0.25s ease;
}

.env-card:focus-visible {
  outline: 2px solid #55ddff;
  outline-offset: 4px;
  border-color: rgba(85, 221, 255, 0.8);
  box-shadow: 0 0 25px rgba(85, 221, 255, 0.5);
}

/* Open State: Staggered spring rise into floating positions */
.env-stage[data-open] .env-card {
  opacity: 1 !important;
  transform: translate(calc(-50% + var(--x)), var(--y)) rotate(var(--r)) scale(1) !important;
  pointer-events: auto;
  transition:
    transform var(--ef-open) var(--ef-spring) calc(var(--i) * var(--ef-stagger)),
    opacity 200ms ease calc(var(--i) * var(--ef-stagger)),
    border-color 0.25s ease,
    box-shadow 0.25s ease;
}

/* Physics Live State: Smooth Matter.js positioning */
.env-stage[data-live] .env-card {
  cursor: grab;
  transition: border-color 0.25s ease, box-shadow 0.25s ease;
}

.env-card[data-drag] {
  cursor: grabbing !important;
  z-index: 30 !important;
  border-color: rgba(85, 221, 255, 0.7) !important;
  box-shadow: 0 20px 48px rgba(2, 6, 20, 0.85), 0 0 28px rgba(85, 221, 255, 0.35) !important;
}

@media (hover: hover) and (pointer: fine) {
  .env-stage[data-open] .env-card:hover:not([data-drag]) {
    border-color: rgba(85, 221, 255, 0.6);
    box-shadow: 0 18px 42px rgba(132, 103, 255, 0.32), 0 0 26px rgba(85, 221, 255, 0.28);
  }
}

/* Floating Card Typography & Layout */
.env-card-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.env-card-num {
  font-size: 13.5px;
  font-weight: 800;
  letter-spacing: 0.08em;
  background: linear-gradient(135deg, #55ddff 0%, #8467ff 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.env-card-top svg {
  color: #94a3b8;
  transition: color 0.25s ease, transform 0.25s ease;
}

.env-card:hover .env-card-top svg {
  color: #55ddff;
  transform: translate(2px, -2px);
}

.env-card h3 {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.025em;
  color: #ffffff;
  margin: 0 0 8px;
  line-height: 1.25;
}

.env-card p {
  font-size: 12.5px;
  line-height: 1.55;
  color: #94a3b8;
  margin: 0;
}

/* Front Pocket */
.env-front-pocket {
  position: absolute;
  inset: 0;
  border-radius: 0 0 26px 26px;
  z-index: 6;
  background: linear-gradient(165deg, rgba(18, 28, 56, 0.98) 0%, rgba(9, 15, 32, 0.99) 100%);
  border: none !important;
  box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.4);
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  padding: 30px;
  overflow: hidden;
  transition: transform 0.65s var(--ef-ease-out), opacity 0.4s ease;
}

.env-front-pocket::before {
  content: "";
  position: absolute;
  inset: 0;
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.06) 0%, transparent 45%),
    linear-gradient(225deg, rgba(255, 255, 255, 0.06) 0%, transparent 45%);
  pointer-events: none;
}

/* Closed state content */
.env-closed-content {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  text-align: center;
  padding: 0 24px 32px;
  z-index: 7;
  transition: opacity 0.35s ease, transform 0.35s ease;
  pointer-events: none;
}

.env-stage[data-open] .env-closed-content {
  opacity: 0;
  transform: translateY(20px) scale(0.94);
}

/* Opened state final CTA area */
.env-opened-cta-area {
  display: flex;
  width: 100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 14px;
  padding-top: 18px;
  border-top: 1px solid rgba(132, 103, 255, 0.22);
  margin-top: 4px;
  opacity: 0;
  transform: translateY(18px);
  pointer-events: none;
  transition: opacity 0.45s var(--ef-ease-out) 0.2s, transform 0.45s var(--ef-ease-out) 0.2s;
}

.env-stage[data-open] .env-opened-cta-area {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

.env-final-msg {
  font-size: clamp(20px, 2.2vw, 28px);
  font-weight: 750;
  letter-spacing: -0.03em;
  margin: 0;
  background: linear-gradient(135deg, #ffffff 10%, #d2d8fe 50%, #8467ff 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: 0 4px 22px rgba(132, 103, 255, 0.35);
}

.env-final-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 32px;
  font-size: 15px;
  font-weight: 650;
  letter-spacing: -0.01em;
  color: #ffffff;
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 50%, #9333ea 100%);
  border: 1px solid rgba(255, 255, 255, 0.24);
  border-radius: 999px;
  box-shadow: 0 8px 25px rgba(124, 58, 237, 0.48), 0 0 22px rgba(79, 70, 229, 0.38);
  text-decoration: none;
  transition: all 0.3s cubic-bezier(0.22, 1, 0.36, 1);
  cursor: pointer;
}

.env-final-btn:hover {
  transform: translateY(-2px) scale(1.03);
  box-shadow: 0 12px 34px rgba(124, 58, 237, 0.65), 0 0 32px rgba(85, 221, 255, 0.55);
  border-color: rgba(255, 255, 255, 0.45);
}

/* Floor Shadow under envelope */
.env-floor-shadow {
  position: absolute;
  bottom: 8px;
  left: 50%;
  transform: translateX(-50%);
  width: 76%;
  height: 36px;
  border-radius: 50%;
  background: radial-gradient(ellipse at center, rgba(0, 0, 0, 0.78) 0%, rgba(13, 22, 45, 0.45) 45%, transparent 75%);
  filter: blur(14px);
  pointer-events: none;
  z-index: 1;
  transition: all 0.65s var(--ef-ease-out);
}

.env-stage[data-open] .env-floor-shadow {
  width: 90%;
  height: 48px;
  bottom: -4px;
  background: radial-gradient(ellipse at center, rgba(0, 0, 0, 0.85) 0%, rgba(15, 23, 42, 0.52) 50%, transparent 75%);
  opacity: 0.85;
}

/* Responsive adjustments for tablet and mobile */
@media (max-width: 900px) {
  .env-stage .env-card,
  .env-card {
    width: 240px;
    min-height: 168px;
    padding: 18px 18px 16px;
  }
  .env-card h3 {
    font-size: 15.5px;
  }
  .env-card p {
    font-size: 11.8px;
  }
  .env-stage {
    max-width: 580px;
  }
}

@media (max-width: 640px) {
  .process-envelope-wrapper {
    margin: 100px auto 0;
  }
  .env-stage {
    max-width: 100%;
  }
  .env-3d {
    height: 380px;
  }
  .env-stage .env-card,
  .env-card {
    width: 200px;
    min-height: 152px;
    padding: 14px 14px 12px;
    border-radius: 16px;
  }
  .env-card h3 {
    font-size: 14px;
  }
  .env-card p {
    font-size: 11px;
    line-height: 1.45;
  }
}

/* Accessibility: prefers-reduced-motion */
@media (prefers-reduced-motion: reduce) {
  .env-top-flap {
    transition: opacity 200ms ease;
  }
  .env-stage[data-open] .env-top-flap {
    transform: none;
    opacity: 0.2;
  }
  .env-card {
    transition: opacity 250ms ease !important;
  }
  .env-stage[data-open] .env-card {
    transform: translate(calc(-50% + var(--x)), var(--y)) scale(1) !important;
    transition: opacity 250ms ease calc(var(--i) * var(--ef-stagger)) !important;
  }
}
