/* ============================================================
 * 3D Flip Cards – Centered Flip + Zoom
 * 480ms smooth transition, prefers-reduced-motion safe
 * ============================================================ */

/* ---- Overlay (focus backdrop) ---- */
.card-overlay {
  position: fixed;
  inset: 0;
  z-index: 9998;
  background: rgba(0, 0, 0, 0.55);
  opacity: 0;
  display: none;
  transition: opacity 160ms ease-out;
  pointer-events: auto;
  -webkit-tap-highlight-color: transparent;
}

.card-overlay--visible {
  opacity: 1;
}

/* ---- Placeholder (occupies space while card is fixed) ---- */
.card-flip-placeholder {
  border-radius: 18px;
  background: transparent;
  pointer-events: none;
  /* Keep exact same box so grid doesn't reflow */
}

/* ---- Container ---- */
.projects-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
  gap: var(--space-lg);
  position: relative;
}

/* ---- Card Wrapper ---- */
.project-card {
  perspective: 1200px;
  cursor: pointer;
  position: relative;
  border: none !important;
  background: transparent !important;
  padding: 0 !important;
  overflow: visible !important;
  transition: z-index 0s 0.48s;
}

.project-card.flipped {
  z-index: 10000;
  transition: z-index 0s 0s;
}

/* ---- Inner: the flipping element ---- */
.card-inner {
  position: relative;
  width: 100%;
  min-height: 300px;
  transform-style: preserve-3d;
  border-radius: 18px;
  /* Default transition used by CSS hover tilt; JS overrides for flip */
  transition: transform 480ms cubic-bezier(.22,.9,.28,1);
  box-shadow:
    0 8px 32px rgba(0, 0, 0, 0.3),
    0 0 0 1px rgba(255, 255, 255, 0.02);
}

/* will-change hints set by JS during animation; CSS fallback for flipped state */
.project-card.flipped,
.project-card.flipped .card-inner {
  will-change: transform;
}

/* Subtle tilt on hover (non-flipped) */
.project-card:not(.flipped) .card-inner:hover {
  transform: rotateY(-4deg) rotateX(2deg) translateZ(8px);
  transition: transform 0.35s cubic-bezier(0.2, 0.9, 0.3, 1);
}

/* Shadow states */
.project-card:not(.flipped):hover .card-inner {
  box-shadow:
    0 20px 60px rgba(0, 0, 0, 0.4),
    0 0 40px rgba(14, 165, 164, 0.08);
}

.project-card.flipped .card-inner {
  box-shadow:
    0 25px 70px rgba(0, 0, 0, 0.5),
    0 0 60px rgba(14, 165, 164, 0.15),
    0 0 120px rgba(14, 165, 164, 0.05);
}

/* ---- Shared face styles ---- */
.card-side {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border-radius: 18px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* ---- FRONT ---- */
.card-front {
  background: linear-gradient(
    165deg,
    rgba(14, 165, 164, 0.06) 0%,
    rgba(255, 255, 255, 0.03) 40%,
    rgba(6, 182, 212, 0.04) 100%
  );
  border: 1px solid var(--border-subtle);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  transform: rotateY(0deg);
}

/* Gradient border on hover */
.card-front::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 18px;
  padding: 1px;
  background: linear-gradient(
    135deg,
    var(--accent-primary),
    transparent 40%,
    transparent 60%,
    var(--accent-secondary)
  );
  -webkit-mask:
    linear-gradient(#fff 0 0) content-box,
    linear-gradient(#fff 0 0);
  mask:
    linear-gradient(#fff 0 0) content-box,
    linear-gradient(#fff 0 0);
  -webkit-mask-composite: xor;
  mask-composite: exclude;
  opacity: 0;
  transition: opacity 0.5s ease;
  pointer-events: none;
}

.project-card:not(.flipped):hover .card-front::before {
  opacity: 1;
}

/* Shine sweep */
.card-front::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(
    105deg,
    transparent 40%,
    rgba(255, 255, 255, 0.04) 45%,
    rgba(255, 255, 255, 0.08) 50%,
    rgba(255, 255, 255, 0.04) 55%,
    transparent 60%
  );
  transform: translateX(-100%);
  transition: none;
  pointer-events: none;
}

.project-card:not(.flipped):hover .card-front::after {
  transform: translateX(100%);
  transition: transform 0.8s ease;
}

/* Icon area */
.project-image {
  height: 140px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  background: linear-gradient(180deg, rgba(14, 165, 164, 0.05) 0%, transparent 100%);
}

.project-icon {
  font-size: 3.5rem;
  opacity: 0.6;
  filter: saturate(0.8);
  transition: all 0.5s cubic-bezier(0.2, 0.9, 0.3, 1);
}

.project-card:not(.flipped):hover .project-icon {
  opacity: 1;
  filter: saturate(1.2);
  transform: scale(1.15) translateY(-4px);
  text-shadow: 0 8px 32px rgba(14, 165, 164, 0.5);
}

/* Glow orb behind icon */
.project-image::after {
  content: '';
  position: absolute;
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: radial-gradient(circle, var(--accent-glow), transparent 70%);
  opacity: 0;
  transition: opacity 0.5s, transform 0.5s;
  transform: scale(0.5);
  pointer-events: none;
}

.project-card:not(.flipped):hover .project-image::after {
  opacity: 0.6;
  transform: scale(1.2);
}

/* Front content */
.project-content {
  padding: var(--space-md) var(--space-lg) var(--space-lg);
  flex: 1;
  display: flex;
  flex-direction: column;
}

.project-title {
  font-size: var(--text-lg);
  font-weight: 600;
  margin-bottom: var(--space-xs);
  color: var(--text-primary);
}

.project-desc {
  font-size: var(--text-sm);
  color: var(--text-secondary);
  line-height: 1.6;
}

/* Flip hint */
.card-front .flip-hint {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px var(--space-lg) 14px;
  font-size: var(--text-xs);
  color: var(--text-muted);
  opacity: 0;
  transition: opacity 0.4s;
}

.project-card:not(.flipped):hover .flip-hint {
  opacity: 1;
}

/* ---- BACK ---- */
.card-back {
  transform: rotateY(180deg);
  background: linear-gradient(
    165deg,
    rgba(14, 165, 164, 0.08) 0%,
    rgba(10, 22, 40, 0.95) 40%,
    rgba(6, 182, 212, 0.06) 100%
  );
  border: 1px solid rgba(14, 165, 164, 0.2);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  padding: var(--space-lg);
}

/* Pulsing glow border on back */
.card-back::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 18px;
  padding: 1px;
  background: conic-gradient(
    from 0deg,
    var(--accent-primary),
    var(--accent-secondary),
    transparent,
    var(--accent-primary)
  );
  -webkit-mask:
    linear-gradient(#fff 0 0) content-box,
    linear-gradient(#fff 0 0);
  mask:
    linear-gradient(#fff 0 0) content-box,
    linear-gradient(#fff 0 0);
  -webkit-mask-composite: xor;
  mask-composite: exclude;
  animation: glowPulseBack 3s ease-in-out infinite alternate;
  opacity: 0.3;
  pointer-events: none;
}

@keyframes glowPulseBack {
  0% { opacity: 0.3; }
  100% { opacity: 0.7; }
}

.project-back-content {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: relative;
  z-index: 1;
}

.project-back-content h4 {
  font-size: var(--text-base);
  font-weight: 600;
  color: var(--accent-primary);
  margin-bottom: var(--space-sm);
  padding-bottom: var(--space-xs);
  border-bottom: 1px solid rgba(14, 165, 164, 0.2);
}

.project-back-content p {
  font-size: var(--text-sm);
  color: var(--text-secondary);
  line-height: 1.7;
  margin-bottom: var(--space-sm);
}

.project-back-content ul {
  list-style: none;
  padding: 0;
  margin: 0 0 var(--space-md) 0;
}

.project-back-content ul li {
  font-size: var(--text-sm);
  color: var(--text-secondary);
  padding: 4px 0 4px 16px;
  position: relative;
}

.project-back-content ul li::before {
  content: '›';
  position: absolute;
  left: 0;
  color: var(--accent-primary);
  font-weight: 700;
}

/* Close button */
.project-close {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: auto;
  padding: 8px 18px;
  border-radius: 24px;
  background: rgba(14, 165, 164, 0.1);
  border: 1px solid rgba(14, 165, 164, 0.3);
  color: var(--accent-primary);
  cursor: pointer;
  font-size: var(--text-sm);
  font-family: var(--font-sans);
  transition: all 0.3s ease;
  align-self: flex-start;
}

.project-close:hover {
  background: rgba(14, 165, 164, 0.2);
  border-color: var(--accent-primary);
  transform: translateX(-4px);
  box-shadow: 0 4px 20px rgba(14, 165, 164, 0.2);
}

/* ---- Staggered entrance ---- */
.project-card {
  opacity: 0;
  transform: translateY(40px) !important;
}

.project-card.visible {
  opacity: 1;
  transform: translateY(0) !important;
  transition: opacity 0.6s cubic-bezier(0.2, 0.9, 0.3, 1),
              transform 0.6s cubic-bezier(0.2, 0.9, 0.3, 1);
}

.project-card:nth-child(1).visible { transition-delay: 0s !important; }
.project-card:nth-child(2).visible { transition-delay: 0.15s !important; }
.project-card:nth-child(3).visible { transition-delay: 0.3s !important; }
.project-card:nth-child(4).visible { transition-delay: 0.45s !important; }

/* ---- Focus Visible ---- */
.project-card:focus-visible .card-inner {
  outline: 2px solid var(--accent-primary);
  outline-offset: 4px;
  border-radius: 18px;
}

.project-close:focus-visible {
  outline: 2px solid var(--accent-primary);
  outline-offset: 3px;
  box-shadow: 0 0 0 4px rgba(14, 165, 164, 0.2);
}

/* ---- RESPONSIVE ---- */
@media (max-width: 768px) {
  .projects-grid {
    grid-template-columns: 1fr;
  }

  .project-card {
    perspective: 800px;
  }

  .card-inner {
    min-height: 260px;
  }

  .project-card:not(.flipped) .card-inner:hover {
    transform: none;
  }

  .flip-hint {
    opacity: 0.6 !important;
  }
}

/* ---- Reduced motion ---- */
@media (prefers-reduced-motion: reduce) {
  .card-inner {
    transition: none !important;
  }

  .card-overlay {
    transition: none !important;
  }

  .project-card {
    opacity: 1 !important;
    transform: none !important;
  }

  .card-front::after,
  .card-front::before,
  .card-back::before {
    animation: none !important;
    transition: none !important;
  }

  /* Still show flipped state — just instant, no scale */
  .project-card.flipped .card-inner {
    transform: rotateY(180deg);
  }
}
