/* ==========================================================================
   UNIVERSAL THREE.JS 3D & CINEMATIC MOTION LAYER (v2) — All 141 Templates
   Upgrades every template with:
     1) True Three.js 60 FPS Depth-Displaced 3D Hero + Crisp Optical Loupe
     2) Pinned 360vh 4-Stage Match-Cut 3D Scroll Journey (16 Domain Sculptures)
     3) Staggered Clip-Mask Arabic Slide Reveals & Clickable Vertical Rail
     4) Continuous Scroll-Linked Photo Parallax & Volumetric GPU Closing Mist
     5) Safe 3D Scroll Reveals, Tilt Cards, Magnetic CTAs & Live Counters
   ========================================================================== */

/* 1. Embedded 3D Background Stage Container */
.u3d-bg-stage {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  border-radius: inherit;
  pointer-events: none;
  z-index: 0;
}

.u3d-scrim {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 3;
  border-radius: inherit;
}

.u3d-img-shell {
  position: relative;
  display: block;
  overflow: hidden;
  border-radius: inherit;
}

/* Three.js Live 60 FPS Hero Depth & Optical Loupe Canvas */
.u3d-three-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  border-radius: inherit;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.5s ease;
}

.u3d-three-canvas.is-live {
  opacity: 1;
}

/* 2. Optional Pure-3D Procedural Geometry Canvas (Toggled via 3D Dock or Pure-HTML Heroes) */
.u3d-geo-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  border-radius: inherit;
  pointer-events: none;
  z-index: 1;
  opacity: 0.45;
  transition: opacity 0.48s cubic-bezier(0.22, 1, 0.36, 1), filter 0.48s ease;
}

.u3d-geo-canvas.is-dark-blend {
  mix-blend-mode: screen;
  opacity: 0.32;
}

.u3d-geo-canvas.is-light-blend {
  mix-blend-mode: multiply;
  opacity: 0.18;
}

.u3d-geo-canvas.is-subtle-blend {
  opacity: 0.14;
}

.u3d-geo-canvas.is-pure-3d {
  mix-blend-mode: normal !important;
  opacity: 1 !important;
  z-index: 2;
}

.u3d-geo-canvas.is-hidden-3d {
  opacity: 0 !important;
}

/* ==========================================================================
   3. PINNED 360vh 4-STAGE MATCH-CUT 3D SCROLL JOURNEY (.u3d-chapter)
   ========================================================================== */
.u3d-chapter {
  --u3d-ch-bg: #08090d;
  --u3d-ch-ink: #f3efe6;
  --u3d-ch-soft: rgba(243, 239, 230, 0.78);
  --u3d-ch-acc: var(--cfx-accent, #c9a54c);
  --u3d-ch-acc-ink: #090b0f;
  --u3d-ch-line: rgba(255, 255, 255, 0.14);
  position: relative;
  height: 360vh;
  min-height: 2900px;
  width: 100%;
  background: var(--u3d-ch-bg);
  color: var(--u3d-ch-ink);
  border-top: 1px solid var(--u3d-ch-line);
  border-bottom: 1px solid var(--u3d-ch-line);
  direction: rtl;
  text-align: right;
  font-family: "IBM Plex Sans Arabic", system-ui, sans-serif;
  isolation: isolate;
}

.u3d-chapter.is-boxed {
  border-radius: 24px;
  margin-block: 36px;
  overflow: clip;
  border: 1px solid var(--u3d-ch-line);
}

.u3d-chapter-pin {
  position: sticky;
  top: 0;
  height: 100vh;
  width: 100%;
  overflow: hidden;
  display: flex;
  align-items: center;
}

.u3d-chapter-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  z-index: 1;
}

.u3d-chapter-scrim {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  background:
    radial-gradient(ellipse 78% 78% at 50% 50%, transparent 32%, rgba(6, 7, 11, 0.66) 100%),
    linear-gradient(270deg, rgba(6, 7, 11, 0.84) 0%, rgba(6, 7, 11, 0.42) 38%, transparent 65%);
}

.u3d-chapter-kicker {
  position: absolute;
  top: clamp(78px, 10vh, 98px);
  right: clamp(22px, 4.2vw, 60px);
  left: auto;
  z-index: 6;
  font-family: "JetBrains Mono", "IBM Plex Mono", monospace;
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--u3d-ch-acc);
  direction: ltr;
  unicode-bidi: isolate;
}

/* 4 Choreographed Match-Cut Story Beats */
.u3d-chapter-beats {
  position: relative;
  z-index: 6;
  width: min(1320px, calc(100% - clamp(28px, 6vw, 88px)));
  margin-inline: auto;
  height: 100%;
  pointer-events: none;
}

.u3d-beat {
  position: absolute;
  inset-inline-start: 0;
  max-width: min(500px, 78vw);
  pointer-events: none;
  visibility: hidden;
  transition: visibility 0s linear 0.62s;
}

.u3d-beat--bl {
  bottom: clamp(54px, 8.5vh, 102px);
  top: auto;
}

.u3d-beat--l {
  top: 50%;
  transform: translateY(-50%);
}

.u3d-beat--tl {
  top: clamp(134px, 16vh, 174px);
  bottom: auto;
}

.u3d-beat.is-active {
  visibility: visible;
  pointer-events: auto;
  transition: visibility 0s linear 0s;
}

.u3d-beat-tag {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
  font-family: "JetBrains Mono", "IBM Plex Mono", monospace;
  font-size: 11.5px;
  letter-spacing: 0.1em;
  color: var(--u3d-ch-acc);
  opacity: 0;
  transform: translateY(10px);
  transition: opacity 0.48s cubic-bezier(0.22, 1, 0.36, 1), transform 0.58s cubic-bezier(0.22, 1, 0.36, 1);
}

.u3d-beat.is-active .u3d-beat-tag {
  opacity: 1;
  transform: translateY(0);
}

.u3d-beat-num {
  padding: 2px 7px;
  border: 1px solid color-mix(in srgb, var(--u3d-ch-acc) 55%, transparent);
  color: var(--u3d-ch-acc);
  direction: ltr;
  unicode-bidi: isolate;
}

.u3d-beat h2 {
  font-family: var(--font-display, "Amiri", "Readex Pro", "IBM Plex Sans Arabic", serif);
  font-size: clamp(2.15rem, 4.3vw, 3.85rem);
  font-weight: 700;
  line-height: 1.24;
  letter-spacing: 0;
  color: var(--u3d-ch-ink);
  margin: 0 0 16px;
}

.u3d-beat-em {
  color: var(--u3d-ch-acc);
  font-style: normal;
}

/* Staggered Clip-Mask Arabic Line Wipe */
.u3d-bl {
  display: block;
  overflow: hidden;
  padding: 0.1em 0;
}

.u3d-bl > span {
  display: block;
  opacity: 0;
  transform: translateY(112%);
  transition: transform 0.7s cubic-bezier(0.22, 1, 0.36, 1) var(--d, 0s),
              opacity 0.5s ease var(--d, 0s);
}

.u3d-beat.is-active .u3d-bl > span {
  opacity: 1;
  transform: translateY(0%);
}

.u3d-beat-body {
  font-size: 15.5px;
  line-height: 1.8;
  letter-spacing: 0;
  color: var(--u3d-ch-soft);
  margin: 0 0 22px;
  opacity: 0;
  transform: translateY(14px);
  transition: opacity 0.54s ease 0.14s, transform 0.66s cubic-bezier(0.22, 1, 0.36, 1) 0.14s;
}

.u3d-beat.is-active .u3d-beat-body {
  opacity: 1;
  transform: translateY(0);
}

.u3d-beat-cta {
  opacity: 0;
  transform: translateY(14px);
  transition: opacity 0.54s ease 0.22s, transform 0.66s cubic-bezier(0.22, 1, 0.36, 1) 0.22s;
}

.u3d-beat.is-active .u3d-beat-cta {
  opacity: 1;
  transform: translateY(0);
}

.u3d-ch-btn {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 13px 24px;
  background: var(--u3d-ch-acc);
  color: var(--u3d-ch-acc-ink);
  border: 1px solid var(--u3d-ch-acc);
  font-family: "IBM Plex Sans Arabic", system-ui, sans-serif;
  font-size: 13.5px;
  font-weight: 700;
  letter-spacing: 0;
  text-decoration: none;
  cursor: pointer;
  transition: transform 0.24s ease, filter 0.24s ease;
}

.u3d-ch-btn:hover {
  transform: translateY(-2px);
  filter: brightness(1.08);
}

/* Clickable Vertical 4-Stop Progress Rail (Left Edge in RTL) */
.u3d-rail {
  position: absolute;
  inset-inline-end: clamp(18px, 3.5vw, 50px);
  top: 50%;
  transform: translateY(-50%);
  height: min(340px, 42vh);
  width: 112px;
  z-index: 7;
}

.u3d-rail-track {
  position: absolute;
  inset-inline-end: 4px;
  top: 0;
  bottom: 0;
  width: 1px;
  background: rgba(255, 255, 255, 0.22);
}

.u3d-rail-fill {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 0%;
  background: var(--u3d-ch-acc);
  box-shadow: 0 0 10px var(--u3d-ch-acc);
}

.u3d-rail-stop {
  position: absolute;
  top: calc(var(--at, 0) * 100%);
  inset-inline-end: 0;
  transform: translateY(-50%);
  appearance: none;
  border: 0;
  background: transparent;
  color: rgba(243, 239, 230, 0.56);
  font-family: "JetBrains Mono", "IBM Plex Mono", monospace;
  font-size: 10.5px;
  letter-spacing: 0.08em;
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  padding-block: 4px;
  padding-inline-start: 0;
  padding-inline-end: 20px;
  white-space: nowrap;
  cursor: pointer;
  transition: color 0.24s ease;
}

.u3d-rail-stop i {
  position: absolute;
  inset-inline-end: 0;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #0a0b0f;
  border: 1.5px solid rgba(255, 255, 255, 0.45);
  transition: background-color 0.24s ease, border-color 0.24s ease, transform 0.24s ease, box-shadow 0.24s ease;
}

.u3d-rail-stop.is-passed,
.u3d-rail-stop.is-active {
  color: var(--u3d-ch-ink);
}

.u3d-rail-stop.is-passed i,
.u3d-rail-stop.is-active i {
  background: var(--u3d-ch-acc);
  border-color: var(--u3d-ch-acc);
}

.u3d-rail-stop.is-active i {
  transform: scale(1.38);
  box-shadow: 0 0 12px var(--u3d-ch-acc);
}

/* Bottom-Left Live Scroll Telemetry Readout */
.u3d-telemetry {
  position: absolute;
  left: clamp(20px, 3.6vw, 52px);
  right: auto;
  bottom: clamp(24px, 4.2vh, 44px);
  z-index: 7;
  direction: ltr;
  unicode-bidi: isolate;
  text-align: left;
  pointer-events: none;
}

.u3d-tel-main {
  display: block;
  font-family: "JetBrains Mono", "IBM Plex Mono", monospace;
  font-size: clamp(1.55rem, 2.8vw, 2.35rem);
  font-weight: 600;
  line-height: 1;
  color: var(--u3d-ch-acc);
  font-variant-numeric: tabular-nums;
}

.u3d-tel-sub {
  display: block;
  margin-top: 5px;
  font-family: "JetBrains Mono", "IBM Plex Mono", monospace;
  font-size: 10.5px;
  letter-spacing: 0.14em;
  color: rgba(243, 239, 230, 0.62);
}

/* 4. Closing Volumetric GPU Mist Canvas */
.u3d-closing-steam {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  pointer-events: none;
  z-index: 0;
  opacity: 0.72;
}

/* 5. Interactive Edge-Docked Collapsible 3D Control Pill */
.u3d-dock {
  position: fixed;
  top: 17%;
  left: 0;
  z-index: 8800;
  display: inline-flex;
  align-items: center;
  gap: 0;
  padding: 5px 10px 5px 8px;
  border-radius: 0 999px 999px 0;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-left: 0;
  background: rgba(10, 12, 17, 0.78);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.34);
  color: #f4f5f8;
  font-family: "IBM Plex Sans Arabic", system-ui, sans-serif;
  font-size: 11px;
  line-height: 1;
  direction: rtl;
  user-select: none;
  transition: padding 0.28s cubic-bezier(0.22, 1, 0.36, 1),
              border-color 0.24s ease,
              background-color 0.24s ease;
}

.u3d-dock:hover,
.u3d-dock.is-open {
  gap: 8px;
  padding: 5px 10px 5px 10px;
  background: rgba(10, 12, 17, 0.92);
  border-color: color-mix(in srgb, var(--cfx-accent, #b8ff2e) 55%, rgba(255, 255, 255, 0.22));
}

.u3d-dock-trigger {
  appearance: none;
  border: 0;
  background: transparent;
  color: #f4f5f8;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 2px;
  cursor: pointer;
  font-family: "JetBrains Mono", "IBM Plex Mono", monospace;
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.04em;
  direction: ltr;
  unicode-bidi: isolate;
}

.u3d-dock-dot {
  width: 7px;
  height: 7px;
  border-radius: 999px;
  background: var(--cfx-accent, #b8ff2e);
  box-shadow: 0 0 10px var(--cfx-accent, #b8ff2e);
  animation: u3dPulse 2s ease-in-out infinite;
  flex-shrink: 0;
}

.u3d-dock-tray {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 0;
  opacity: 0;
  overflow: hidden;
  pointer-events: none;
  transition: max-width 0.34s cubic-bezier(0.22, 1, 0.36, 1), opacity 0.24s ease;
}

.u3d-dock:hover .u3d-dock-tray,
.u3d-dock.is-open .u3d-dock-tray {
  max-width: 350px;
  opacity: 1;
  pointer-events: auto;
}

.u3d-dock-coords {
  font-family: "JetBrains Mono", "IBM Plex Mono", monospace;
  font-size: 10px;
  color: rgba(244, 245, 248, 0.82);
  direction: ltr;
  unicode-bidi: isolate;
  white-space: nowrap;
  padding-inline-end: 6px;
  border-inline-end: 1px solid rgba(255, 255, 255, 0.14);
}

.u3d-dock-modes {
  display: inline-flex;
  align-items: center;
  gap: 3px;
}

.u3d-mode-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: rgba(244, 245, 248, 0.72);
  font-family: "IBM Plex Sans Arabic", system-ui, sans-serif;
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0;
  padding: 5px 9px;
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.2s ease, color 0.2s ease;
}

.u3d-mode-btn:hover {
  color: #ffffff;
  background: rgba(255, 255, 255, 0.08);
}

.u3d-mode-btn.is-active {
  background: var(--cfx-accent, #b8ff2e);
  color: #090b0f;
}

.u3d-mode-btn.is-active.is-dark-accent {
  color: #ffffff;
}

@keyframes u3dPulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.38); opacity: 0.52; }
}

/* Keep embedded directory iframe previews fast & clean */
html.cfx-embed-preview .u3d-dock,
html.cfx-embed-preview .u3d-chapter,
html.cfx-embed-preview .u3d-closing-steam {
  display: none !important;
}

/* 6. Immediate Non-Blocking Hero Entrance Choreography */
@keyframes u3dHeroRise {
  0% {
    opacity: 0.01;
    transform: translate3d(0, 18px, 0);
  }
  100% {
    opacity: 1;
    transform: translate3d(0, 0, 0);
  }
}

.u3d-hero-anim {
  animation: u3dHeroRise 0.78s cubic-bezier(0.22, 1, 0.36, 1) var(--u3d-d, 0ms) both;
}

/* 7. Safe 3D Perspective Scroll Reveals for Below-the-Fold Sections & Cards */
.u3d-rev {
  opacity: 0;
  transform: translate3d(0, 26px, 0) scale(0.986);
  transition: opacity 0.68s cubic-bezier(0.22, 1, 0.36, 1),
              transform 0.76s cubic-bezier(0.22, 1, 0.36, 1);
  transition-delay: var(--u3d-d, 0ms);
  will-change: opacity, transform;
}

.u3d-rev.is-in {
  opacity: 1;
  transform: translate3d(0, 0, 0) scale(1);
}

/* 8. Tactile Micro-Depth & Scroll-Linked Parallax on Secondary Images */
.u3d-img-live {
  transition: filter 0.45s ease;
  will-change: transform;
}

/* Responsive Adjustments */
@media (max-width: 820px) {
  .u3d-rail {
    display: none;
  }
  .u3d-beat {
    max-width: calc(100% - 24px);
  }
  .u3d-beat h2 {
    font-size: clamp(1.8rem, 7vw, 2.5rem);
  }
  .u3d-dock {
    top: 14%;
    padding: 4px 8px 4px 6px;
  }
  .u3d-dock-coords {
    display: none;
  }
  .u3d-mode-btn {
    padding: 4px 7px;
    font-size: 10px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .u3d-hero-anim,
  .u3d-rev,
  .u3d-img-live,
  .u3d-dock-dot,
  .u3d-bl > span,
  .u3d-beat-tag,
  .u3d-beat-body,
  .u3d-beat-cta {
    animation: none !important;
    transition: none !important;
    opacity: 1 !important;
    transform: none !important;
  }
}
