/**
 * C4 Universe — Advanced Animations & Effects
 * P2: Full Polish
 */

/* ========================================
   LAYOUT STABILITY (Prevent CLS)
   ======================================== */

/* Reserve space for images before they load */
img {
  max-width: 100%;
  height: auto;
}

/* Aspect ratio containers */
.aspect-square {
  aspect-ratio: 1 / 1;
}

.aspect-video {
  aspect-ratio: 16 / 9;
}

.aspect-hero {
  aspect-ratio: 21 / 9;
}

/* Content visibility for off-screen sections */
.section-hidden {
  content-visibility: auto;
  contain-intrinsic-size: 0 500px;
}

/* Prevent layout shift during font loading */
html {
  font-display: swap;
}

/* ========================================
   PAGE TRANSITIONS
   ======================================== */
.page-transition-overlay {
  position: fixed;
  inset: 0;
  background: var(--deep-space);
  z-index: 9999;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s ease;
}

.page-transition-overlay.active {
  opacity: 1;
  pointer-events: all;
}

/* Smooth scroll behavior */
html {
  scroll-behavior: smooth;
}

/* ========================================
   SCROLL-TRIGGERED ANIMATIONS
   ======================================== */
.reveal {
  opacity: 0;
  transform: translateY(30px);
  transition: opacity 0.6s ease, transform 0.6s ease;
}

.reveal.visible {
  opacity: 1;
  transform: translateY(0);
}

.reveal-left {
  opacity: 0;
  transform: translateX(-50px);
  transition: opacity 0.6s ease, transform 0.6s ease;
}

.reveal-left.visible {
  opacity: 1;
  transform: translateX(0);
}

.reveal-right {
  opacity: 0;
  transform: translateX(50px);
  transition: opacity 0.6s ease, transform 0.6s ease;
}

.reveal-right.visible {
  opacity: 1;
  transform: translateX(0);
}

.reveal-scale {
  opacity: 0;
  transform: scale(0.9);
  transition: opacity 0.6s ease, transform 0.6s ease;
}

.reveal-scale.visible {
  opacity: 1;
  transform: scale(1);
}

/* Stagger children */
.stagger-children > * {
  opacity: 0;
  transform: translateY(20px);
  transition: opacity 0.4s ease, transform 0.4s ease;
}

.stagger-children.visible > *:nth-child(1) { transition-delay: 0s; }
.stagger-children.visible > *:nth-child(2) { transition-delay: 0.1s; }
.stagger-children.visible > *:nth-child(3) { transition-delay: 0.2s; }
.stagger-children.visible > *:nth-child(4) { transition-delay: 0.3s; }
.stagger-children.visible > *:nth-child(5) { transition-delay: 0.4s; }
.stagger-children.visible > *:nth-child(6) { transition-delay: 0.5s; }

.stagger-children.visible > * {
  opacity: 1;
  transform: translateY(0);
}

/* ========================================
   HOVER EFFECTS
   ======================================== */
.hover-lift {
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.hover-lift:hover {
  transform: translateY(-8px);
  box-shadow: 0 12px 40px rgba(0,0,0,0.4);
}

.hover-glow {
  transition: box-shadow 0.3s ease;
}

.hover-glow:hover {
  box-shadow: 0 0 30px rgba(255,215,0,0.3);
}

.hover-scale {
  transition: transform 0.3s ease;
}

.hover-scale:hover {
  transform: scale(1.05);
}

/* Card tilt effect */
.tilt-card {
  transform-style: preserve-3d;
  transition: transform 0.3s ease;
}

.tilt-card:hover {
  transform: perspective(1000px) rotateX(2deg) rotateY(-2deg) translateZ(10px);
}

/* Magnetic button */
.magnetic-btn {
  transition: transform 0.2s ease;
}

/* ========================================
   PARALLAX EFFECTS
   ======================================== */
.parallax-layer {
  will-change: transform;
}

.parallax-slow {
  transform: translateZ(-10px) scale(2);
}

/* ========================================
   PARTICLE CONNECTIONS
   ======================================== */
.particle-connection {
  position: absolute;
  stroke: rgba(255,215,0,0.2);
  stroke-width: 1;
  pointer-events: none;
}

/* ========================================
   LOADING STATES
   ======================================== */
.skeleton {
  background: linear-gradient(
    90deg,
    rgba(255,255,255,0.05) 0%,
    rgba(255,255,255,0.1) 50%,
    rgba(255,255,255,0.05) 100%
  );
  background-size: 200% 100%;
  animation: skeleton-loading 1.5s infinite;
  border-radius: 4px;
}

@keyframes skeleton-loading {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ========================================
   TEXT ANIMATIONS
   ======================================== */
.typewriter {
  overflow: hidden;
  border-right: 2px solid var(--color-phi);
  white-space: nowrap;
  animation: typing 3s steps(40, end), blink-caret 0.75s step-end infinite;
}

@keyframes typing {
  from { width: 0; }
  to { width: 100%; }
}

@keyframes blink-caret {
  from, to { border-color: transparent; }
  50% { border-color: var(--color-phi); }
}

/* Gradient text animation */
.gradient-text-animate {
  background: linear-gradient(
    90deg,
    var(--color-phi),
    var(--color-fra-r),
    var(--color-fra-f),
    var(--color-phi)
  );
  background-size: 300% 100%;
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: gradient-shift 3s ease infinite;
}

@keyframes gradient-shift {
  0%, 100% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
}

/* ========================================
   SHIMMER EFFECTS
   ======================================== */
.shimmer {
  position: relative;
  overflow: hidden;
}

.shimmer::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(
    90deg,
    transparent 0%,
    rgba(255,255,255,0.1) 50%,
    transparent 100%
  );
  transform: translateX(-100%);
  animation: shimmer 2s infinite;
}

@keyframes shimmer {
  100% { transform: translateX(100%); }
}

/* ========================================
   PULSE EFFECTS
   ======================================== */
.pulse-glow {
  animation: pulse-glow 2s ease-in-out infinite;
}

@keyframes pulse-glow {
  0%, 100% { box-shadow: 0 0 20px rgba(255,215,0,0.3); }
  50% { box-shadow: 0 0 40px rgba(255,215,0,0.6); }
}

.pulse-ring {
  position: relative;
}

.pulse-ring::before {
  content: '';
  position: absolute;
  inset: -4px;
  border: 2px solid var(--color-phi);
  border-radius: inherit;
  animation: pulse-ring 2s ease-out infinite;
  opacity: 0;
}

@keyframes pulse-ring {
  0% { transform: scale(1); opacity: 1; }
  100% { transform: scale(1.2); opacity: 0; }
}

/* ========================================
   READING PROGRESS BAR
   ======================================== */
.reading-progress {
  position: fixed;
  top: 0;
  left: 0;
  height: 3px;
  background: linear-gradient(90deg, var(--color-phi), var(--color-fra-r));
  z-index: 10001;
  transition: width 0.1s ease;
}

/* ========================================
   SCROLLBAR STYLING
   ======================================== */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: rgba(0,0,0,0.2);
}

::-webkit-scrollbar-thumb {
  background: rgba(255,215,0,0.3);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: rgba(255,215,0,0.5);
}

/* ========================================
   FOCUS VISIBLE
   ======================================== */
:focus-visible {
  outline: 2px solid var(--color-phi);
  outline-offset: 4px;
  border-radius: 2px;
}

/* Skip link enhancement */
.skip-link:focus {
  top: 1rem;
  animation: slide-down 0.3s ease;
}

@keyframes slide-down {
  from { top: -100%; }
  to { top: 1rem; }
}

/* ========================================
   TOAST NOTIFICATIONS
   ======================================== */
.toast-container {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  z-index: 10000;
  pointer-events: none;
}

.toast {
  background: rgba(10,10,18,0.95);
  border: 1px solid rgba(255,215,0,0.3);
  border-radius: 8px;
  padding: 1rem 1.5rem;
  min-width: 300px;
  max-width: 400px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.4);
  pointer-events: all;
  transform: translateX(100%);
  opacity: 0;
  transition: all 0.4s ease;
}

.toast.visible {
  transform: translateX(0);
  opacity: 1;
}

.toast.success { border-color: var(--color-o2); }
.toast.error { border-color: var(--color-o0); }
.toast.info { border-color: var(--color-fra-f); }

.toast-title {
  font-weight: 600;
  color: var(--star-white);
  margin-bottom: 0.25rem;
}

.toast-message {
  font-size: 0.875rem;
  color: rgba(224,224,255,0.8);
}

.toast-close {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  background: none;
  border: none;
  color: rgba(224,224,255,0.5);
  cursor: pointer;
  font-size: 1.25rem;
  line-height: 1;
}

.toast-close:hover {
  color: var(--star-white);
}

/* ========================================
   SEARCH OVERLAY
   ======================================== */
.search-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.9);
  backdrop-filter: blur(8px);
  z-index: 10000;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 15vh;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

.search-overlay.active {
  opacity: 1;
  pointer-events: all;
}

.search-container {
  width: 90%;
  max-width: 600px;
  background: var(--glass-bg);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 16px;
  padding: 1.5rem;
  transform: translateY(-20px);
  transition: transform 0.3s ease;
}

.search-overlay.active .search-container {
  transform: translateY(0);
}

.search-input-wrapper {
  position: relative;
  margin-bottom: 1rem;
}

.search-input {
  width: 100%;
  padding: 1rem 1rem 1rem 3rem;
  background: rgba(0,0,0,0.3);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 8px;
  color: var(--star-white);
  font-size: 1rem;
}

.search-input:focus {
  outline: none;
  border-color: var(--color-phi);
}

.search-icon {
  position: absolute;
  left: 1rem;
  top: 50%;
  transform: translateY(-50%);
  color: rgba(224,224,255,0.5);
}

.search-results {
  max-height: 400px;
  overflow-y: auto;
}

.search-result-item {
  padding: 0.75rem;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.2s ease;
}

.search-result-item:hover {
  background: rgba(255,255,255,0.05);
}

.search-result-title {
  font-weight: 500;
  color: var(--star-white);
  margin-bottom: 0.25rem;
}

.search-result-snippet {
  font-size: 0.875rem;
  color: rgba(224,224,255,0.6);
}

.search-no-results {
  text-align: center;
  padding: 2rem;
  color: rgba(224,224,255,0.5);
}

/* ========================================
   KEYBOARD SHORTCUTS HINT
   ======================================== */
.keyboard-shortcuts {
  position: fixed;
  bottom: 1rem;
  left: 1rem;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(4px);
  border-radius: 8px;
  padding: 0.5rem 1rem;
  font-size: 0.75rem;
  color: rgba(224,224,255,0.5);
  z-index: 100;
}

.keyboard-shortcuts kbd {
  background: rgba(255,255,255,0.1);
  padding: 0.125rem 0.375rem;
  border-radius: 4px;
  font-family: inherit;
  color: rgba(224,224,255,0.8);
}

/* ========================================
   PRINT STYLES
   ======================================== */
@media print {
  .starfield,
  .main-nav,
  .reading-progress,
  .keyboard-shortcuts,
  .toast-container,
  .search-overlay {
    display: none !important;
  }
  
  body {
    background: white;
    color: black;
  }
  
  .hero-cosmic {
    min-height: auto;
    padding: 2rem 0;
  }
  
  .section-title {
    page-break-after: avoid;
  }
  
  .functor-card,
  .app-card,
  .layer-card {
    break-inside: avoid;
  }
}

/* ========================================
   REDUCED MOTION
   ======================================== */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
  
  .parallax-layer {
    transform: none !important;
  }
}
