import { useState, useRef, useEffect } from 'react';
import { Camera, ArrowRight, Edit3, Upload, Type, Calendar, Palette, Edit } from 'lucide-react';
import { Button } from './ui/button';
import { ImageWithFallback } from './figma/ImageWithFallback';
import { motion, useMotionValue, useSpring, useTransform, useInView } from 'motion/react';
import { HandwrittenText, useHandwrittenText } from './HandwrittenText';

interface EditableCardProps {
  isEditing: boolean;
  onEdit: () => void;
  children: React.ReactNode;
  className?: string;
  style?: React.CSSProperties;
  editIcon?: React.ReactNode;
}

export function LandingCustomPostcards() {
  const [editingCard, setEditingCard] = useState<string | null>(null);
  const postcardRef = useRef<HTMLDivElement>(null);
  const sectionRef = useRef<HTMLDivElement>(null);
  
  // 3D tilt effect using mouse position
  const mouseX = useMotionValue(0);
  const mouseY = useMotionValue(0);
  
  // Spring animations for smooth mouse following
  const rotateX = useSpring(useTransform(mouseY, [-0.5, 0.5], [5, -5]), {
    stiffness: 100,
    damping: 15
  });
  const rotateY = useSpring(useTransform(mouseX, [-0.5, 0.5], [-5, 5]), {
    stiffness: 100,
    damping: 15
  });
  
  // Detect when section is in view to trigger handwriting
  const isInView = useInView(sectionRef, { 
    once: true, 
    margin: "-100px",
    amount: 0.3
  });
  
  // Handwritten text control
  const handwrittenControl = useHandwrittenText(false);
  
  // Handle mouse movement for 3D effect
  const handleMouseMove = (event: React.MouseEvent<HTMLDivElement>) => {
    if (!postcardRef.current) return;
    
    const rect = postcardRef.current.getBoundingClientRect();
    const centerX = rect.left + rect.width / 2;
    const centerY = rect.top + rect.height / 2;
    
    const x = (event.clientX - centerX) / (rect.width / 2);
    const y = (event.clientY - centerY) / (rect.height / 2);
    
    mouseX.set(x);
    mouseY.set(y);
  };
  
  const handleMouseLeave = () => {
    mouseX.set(0);
    mouseY.set(0);
  };

  const [postcardData, setPostcardData] = useState({
    photo: "https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=400&h=600&fit=crop&crop=center",
    message: "Greetings from paradise!\\nThe beaches here are absolutely\\nbreathtaking. Wish you were\\nhere to enjoy this amazing\\nsunset with me.",
    date: "July 2024",
    addressLabel: "POSTCARD"
  });

  const handleCreatePostcard = () => {
    console.log('Navigate to postcard creation page...');
  };

  // Start handwriting animation when section comes into view
  useEffect(() => {
    if (isInView && !editingCard) {
      // Delay the start of handwriting to let the postcard animation settle
      const timer = setTimeout(() => {
        handwrittenControl.start();
      }, 1200);
      return () => clearTimeout(timer);
    }
  }, [isInView, editingCard, handwrittenControl]);

  // Reset handwriting when editing
  useEffect(() => {
    if (editingCard === 'message') {
      handwrittenControl.reset();
    }
  }, [editingCard, handwrittenControl]);

  const EditableCard = ({ isEditing, onEdit, children, className = "", style = {}, editIcon }: EditableCardProps) => {
    return (
      <motion.div 
        className={`relative group cursor-pointer ${className}`}
        style={style}
        onClick={(e) => {
          e.stopPropagation();
          onEdit();
        }}
        whileHover={{ 
          scale: 1.02,
          transition: { duration: 0.2 }
        }}
        whileTap={{ scale: 0.98 }}
        animate={isEditing ? {
          boxShadow: "0 0 0 2px rgba(249, 95, 98, 0.5), 0 8px 16px rgba(249, 95, 98, 0.3)"
        } : {}}
      >
        {children}
        
        {/* Animated Edit overlay */}
        <motion.div 
          className="absolute inset-0 bg-black rounded-md flex items-center justify-center"
          initial={{ opacity: 0, backgroundColor: "rgba(0, 0, 0, 0)" }}
          animate={isEditing ? {
            opacity: 1,
            backgroundColor: "rgba(0, 0, 0, 0.2)"
          } : {
            opacity: 0,
            backgroundColor: "rgba(0, 0, 0, 0)"
          }}
          whileHover={!isEditing ? {
            opacity: 1,
            backgroundColor: "rgba(0, 0, 0, 0.1)"
          } : {}}
          transition={{ duration: 0.2 }}
        >
          <motion.div 
            className="bg-white bg-opacity-90 px-2 py-1 rounded-md shadow-lg border border-gray-200 flex items-center gap-1"
            initial={{ scale: 0.8, opacity: 0 }}
            animate={{ scale: 1, opacity: 1 }}
            whileHover={{ scale: 1.05 }}
            transition={{ duration: 0.2 }}
          >
            <motion.div
              animate={isEditing ? { rotate: [0, 10, -10, 0] } : {}}
              transition={{ duration: 2, repeat: isEditing ? Infinity : 0 }}
            >
              {editIcon}
            </motion.div>
            <motion.span 
              className="text-xs text-gray-700 font-medium"
              animate={isEditing ? {
                color: ["#374151", "#F95F62", "#374151"]
              } : {}}
              transition={{ duration: 1, repeat: isEditing ? Infinity : 0 }}
            >
              {isEditing ? 'Editing...' : 'Edit'}
            </motion.span>
          </motion.div>
        </motion.div>
      </motion.div>
    );
  };

  // Ultra-realistic vintage postcard with responsive scaling and animations
  const PostcardFrame = () => {
    return (
      <motion.div 
        className="relative shadow-2xl transform rotate-[0.5deg] w-full h-full"
        initial={{ opacity: 0, scale: 0.9, rotate: 0 }}
        animate={{ opacity: 1, scale: 1, rotate: 0.5 }}
        transition={{ duration: 0.8, ease: "easeOut" }}
        style={{
          background: `
            radial-gradient(circle at 20% 80%, rgba(210, 180, 140, 0.15) 0%, transparent 50%),
            radial-gradient(circle at 80% 20%, rgba(160, 130, 100, 0.1) 0%, transparent 50%),
            radial-gradient(circle at 40% 40%, rgba(190, 160, 120, 0.08) 0%, transparent 50%),
            linear-gradient(135deg, #f8f4e6 0%, #f0e7d3 25%, #ede2d0 50%, #e8dcc8 75%, #e3d5c2 100%)
          `,
          borderRadius: '10px',
          boxShadow: `
            inset 0px 1px 30px rgba(139, 115, 85, 0.2),
            inset 0px -1px 20px rgba(160, 130, 100, 0.15),
            0px 12px 40px rgba(0, 0, 0, 0.15),
            0px 4px 12px rgba(0, 0, 0, 0.1)
          `,
          border: '1px solid rgba(139, 115, 85, 0.2)'
        }}
      >
        {/* Realistic paper texture with variations */}
        <div 
          className="absolute inset-0 opacity-40 rounded-[10px] pointer-events-none"
          style={{
            backgroundImage: `
              url("data:image/svg+xml,%3Csvg width='100' height='100' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23d4af9a' fill-opacity='0.08'%3E%3Cpath d='M11 18c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm48 25c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm-43-7c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm63 31c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM34 90c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm56-76c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM12 86c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm28-65c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm23-11c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-6 60c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm29 22c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zM32 63c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm57-13c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-9-21c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM60 91c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM35 41c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E"),
              radial-gradient(circle at 25% 75%, rgba(180, 150, 120, 0.1) 0%, transparent 40%),
              radial-gradient(circle at 75% 25%, rgba(160, 130, 100, 0.08) 0%, transparent 35%)
            `
          }}
        />

        {/* Age spots and stains */}
        <div className="absolute inset-0 rounded-[10px] pointer-events-none opacity-30">
          <div 
            className="absolute w-8 h-6 rounded-full"
            style={{
              background: 'radial-gradient(ellipse, rgba(160, 120, 80, 0.15) 0%, transparent 70%)',
              top: '15%',
              right: '20%',
              transform: 'rotate(-15deg)'
            }}
          />
          <div 
            className="absolute w-6 h-8 rounded-full"
            style={{
              background: 'radial-gradient(ellipse, rgba(140, 110, 70, 0.12) 0%, transparent 60%)',
              bottom: '25%',
              left: '15%',
              transform: 'rotate(25deg)'
            }}
          />
          <div 
            className="absolute w-4 h-4 rounded-full"
            style={{
              background: 'radial-gradient(circle, rgba(180, 140, 100, 0.2) 0%, transparent 50%)',
              top: '60%',
              right: '10%'
            }}
          />
        </div>

        {/* Corner wear and creases */}
        <div 
          className="absolute top-0 right-0 w-12 h-12 opacity-25"
          style={{
            background: 'radial-gradient(circle at top right, rgba(139, 115, 85, 0.3) 20%, rgba(160, 130, 100, 0.2) 40%, transparent 70%)',
            borderTopRightRadius: '10px'
          }}
        />
        <div 
          className="absolute bottom-0 left-0 w-16 h-16 opacity-20"
          style={{
            background: 'radial-gradient(circle at bottom left, rgba(120, 95, 70, 0.25) 25%, rgba(140, 115, 85, 0.15) 50%, transparent 75%)',
            borderBottomLeftRadius: '10px'
          }}
        />

        {/* Subtle crease lines */}
        <div 
          className="absolute w-full h-px bg-gradient-to-r from-transparent via-amber-800/10 to-transparent opacity-40"
          style={{
            top: '35%',
            transform: 'rotate(-0.5deg)'
          }}
        />

        {/* Animated Vintage Vector Logo - Top Right - Mobile Optimized */}
        <motion.div 
          className="absolute opacity-60 pointer-events-none"
          style={{
            top: '4.3%',
            right: '3.5%',
            width: '7.5%',
            height: '11.5%',
            filter: 'sepia(30%) saturate(120%) hue-rotate(15deg) brightness(85%) contrast(110%)'
          }}
          animate={{
            rotate: [5, 7, 3, 5],
            scale: [1, 1.02, 0.98, 1]
          }}
          transition={{
            duration: 8,
            repeat: Infinity,
            ease: "easeInOut"
          }}
        >
          <ImageWithFallback 
            src="https://images.unsplash.com/photo-1702825328124-dab63d85490e?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3w3Nzg4Nzd8MHwxfHNlYXJjaHwxfHx2aW50YWdlJTIwbG9nbyUyMHZlY3RvciUyMHBvc3RhbCUyMHN0YW1wfGVufDF8fHx8MTc1ODk5MjExN3ww&ixlib=rb-4.1.0&q=80&w=1080&utm_source=figma&utm_medium=referral"
            alt="Vintage logo"
            className="w-full h-full object-contain"
            style={{
              mixBlendMode: 'multiply',
              opacity: 0.8
            }}
          />
          {/* Subtle aging overlay for the vector */}
          <div 
            className="absolute inset-0 rounded-full"
            style={{
              background: `
                radial-gradient(circle at 30% 30%, rgba(160, 130, 90, 0.2) 0%, transparent 60%),
                radial-gradient(circle at 70% 70%, rgba(140, 110, 70, 0.15) 0%, transparent 50%)
              `,
              mixBlendMode: 'multiply'
            }}
          />
        </motion.div>

        {/* Editable Photo Card with realistic mounting - Responsive */}
        <EditableCard
          isEditing={editingCard === 'photo'}
          onEdit={() => setEditingCard(editingCard === 'photo' ? null : 'photo')}
          style={{
            position: 'absolute',
            width: '33.3%',
            height: '72.3%',
            left: '4.9%',
            top: '7.4%',
            transform: 'rotate(-0.8deg)'
          }}
          editIcon={<Upload className="w-3 h-3 text-gray-600" />}
        >
          <div 
            className="w-full h-full rounded-lg overflow-hidden shadow-lg relative"
            style={{
              border: '4px solid #ffffff',
              boxShadow: `
                0px 6px 25px rgba(0, 0, 0, 0.25),
                inset 0px 1px 3px rgba(255, 255, 255, 0.8),
                inset 0px -1px 2px rgba(160, 130, 100, 0.2)
              `
            }}
          >
            <ImageWithFallback
              src={postcardData.photo}
              alt="Beautiful tropical beach"
              className="w-full h-full object-cover"
              style={{
                filter: 'sepia(8%) saturate(110%) contrast(102%) brightness(98%)'
              }}
            />
            {/* Photo aging overlay */}
            <div 
              className="absolute inset-0 pointer-events-none"
              style={{
                background: `
                  radial-gradient(circle at 20% 20%, rgba(255, 235, 205, 0.15) 0%, transparent 40%),
                  radial-gradient(circle at 80% 80%, rgba(200, 170, 130, 0.1) 0%, transparent 35%)
                `,
                mixBlendMode: 'multiply'
              }}
            />
          </div>
        </EditableCard>
        
        {/* Realistic vertical divider with ink bleeding - Responsive */}
        <div 
          className="absolute"
          style={{
            top: '10.6%',
            bottom: '10.6%',
            left: '43.1%',
            width: '2px',
            background: `
              linear-gradient(to bottom,
                transparent 0%,
                rgba(101, 84, 63, 0.4) 10%,
                rgba(101, 84, 63, 0.6) 30%,
                rgba(101, 84, 63, 0.8) 50%,
                rgba(101, 84, 63, 0.6) 70%,
                rgba(101, 84, 63, 0.4) 90%,
                transparent 100%
              )
            `,
            filter: 'blur(0.3px)'
          }}
        />
        
        {/* Ink bleed effect around divider - Responsive */}
        <div 
          className="absolute opacity-20"
          style={{
            top: '10.6%',
            bottom: '10.6%',
            left: '42.8%',
            width: '6px',
            background: `
              linear-gradient(to bottom,
                transparent 0%,
                rgba(101, 84, 63, 0.1) 20%,
                rgba(101, 84, 63, 0.15) 50%,
                rgba(101, 84, 63, 0.1) 80%,
                transparent 100%
              )
            `,
            filter: 'blur(1px)'
          }}
        />
        
        {/* Right side content area - Responsive */}
        <div 
          className="absolute pointer-events-none"
          style={{
            top: '10.6%',
            left: '47.2%',
            width: '47.2%',
            height: '78.7%'
          }}
        >
          {/* Editable Address Label Card - Responsive */}
          <EditableCard
            isEditing={editingCard === 'label'}
            onEdit={() => setEditingCard(editingCard === 'label' ? null : 'label')}
            style={{
              position: 'absolute',
              top: '4.1%',
              left: '0%',
              pointerEvents: 'auto',
              transform: 'rotate(-0.3deg)'
            }}
            editIcon={<Type className="w-3 h-3 text-gray-600" />}
          >
            <div 
              className="px-2 py-1 rounded text-xs md:text-sm"
              style={{
                fontWeight: '600',
                letterSpacing: '2px',
                textTransform: 'uppercase',
                color: 'rgba(101, 84, 63, 0.8)',
                textShadow: '0px 1px 2px rgba(0, 0, 0, 0.1)'
              }}
            >
              {postcardData.addressLabel}
            </div>
          </EditableCard>
          
          {/* Realistic horizontal ruled lines with ink bleeding - Responsive */}
          <div 
            className="absolute pointer-events-none"
            style={{
              top: '14.9%',
              left: '0%',
              width: '94.1%',
              height: '59.5%'
            }}
          >
            {[...Array(9)].map((_, i) => (
              <div key={i} className="relative" style={{ marginBottom: '6.5%' }}>
                {/* Main line */}
                <div 
                  style={{
                    height: '1px',
                    width: i % 2 === 0 ? '100%' : '92%',
                    background: `linear-gradient(to right, 
                      rgba(101, 84, 63, 0.3) 0%, 
                      rgba(101, 84, 63, 0.5) 20%, 
                      rgba(101, 84, 63, 0.6) 50%, 
                      rgba(101, 84, 63, 0.4) 80%, 
                      rgba(101, 84, 63, 0.2) 95%,
                      transparent 100%
                    )`,
                    transform: `rotate(${(Math.random() - 0.5) * 0.5}deg)`
                  }}
                />
                {/* Ink bleed effect */}
                <div 
                  className="absolute top-0 left-0 opacity-30"
                  style={{
                    height: '3px',
                    width: i % 2 === 0 ? '98%' : '90%',
                    background: `linear-gradient(to right, 
                      rgba(101, 84, 63, 0.1) 0%, 
                      rgba(101, 84, 63, 0.2) 30%, 
                      rgba(101, 84, 63, 0.15) 70%, 
                      transparent 100%
                    )`,
                    filter: 'blur(1px)',
                    transform: 'translateY(-1px)'
                  }}
                />
              </div>
            ))}
          </div>
          
          {/* Editable Message Card with Handwritten Animation - Responsive */}
          <EditableCard
            isEditing={editingCard === 'message'}
            onEdit={() => setEditingCard(editingCard === 'message' ? null : 'message')}
            style={{
              position: 'absolute',
              top: '17.6%',
              left: '4.4%',
              width: '88.2%',
              pointerEvents: 'auto',
              transform: 'rotate(-0.7deg)'
            }}
            editIcon={<Edit3 className="w-3 h-3 text-gray-600" />}
          >
            <div className="px-2 py-2 rounded leading-relaxed">
              {editingCard === 'message' ? (
                // Show static text when editing
                <div
                  style={{
                    fontFamily: "'Dancing Script', 'Brush Script MT', cursive",
                    fontSize: 'clamp(14px, 3.6vw, 26px)',
                    lineHeight: '1.7',
                    color: 'rgba(85, 70, 50, 0.9)',
                    textShadow: `
                      1px 1px 2px rgba(0, 0, 0, 0.08),
                      0px 0px 3px rgba(101, 84, 63, 0.1)
                    `,
                    whiteSpace: 'pre-line',
                    filter: 'contrast(110%) brightness(98%)'
                  }}
                >
                  {postcardData.message}
                </div>
              ) : (
                // Show animated handwritten text when not editing
                <HandwrittenText
                  text={postcardData.message}
                  speed={6}
                  startDelay={0}
                  autoStart={false}
                  onComplete={handwrittenControl.onComplete}
                  style={{
                    fontFamily: "'Dancing Script', 'Brush Script MT', cursive",
                    fontSize: 'clamp(14px, 3.6vw, 26px)',
                    lineHeight: '1.7',
                    color: 'rgba(85, 70, 50, 0.9)',
                    textShadow: `
                      1px 1px 2px rgba(0, 0, 0, 0.08),
                      0px 0px 3px rgba(101, 84, 63, 0.1)
                    `,
                    filter: 'contrast(110%) brightness(98%)'
                  }}
                />
              )}
            </div>
          </EditableCard>
          
          {/* Editable Date Card - Responsive */}
          <EditableCard
            isEditing={editingCard === 'date'}
            onEdit={() => setEditingCard(editingCard === 'date' ? null : 'date')}
            style={{
              position: 'absolute',
              bottom: '16.2%',
              right: '7.4%',
              pointerEvents: 'auto',
              transform: 'rotate(-1.5deg)'
            }}
            editIcon={<Calendar className="w-3 h-3 text-gray-600" />}
          >
            <div 
              className="px-2 py-1 rounded text-xs"
              style={{
                fontWeight: '500',
                letterSpacing: '1px',
                color: 'rgba(101, 84, 63, 0.7)',
                textShadow: '0px 1px 1px rgba(0, 0, 0, 0.05)',
                fontFamily: "'Poppins', sans-serif"
              }}
            >
              {postcardData.date}
            </div>
          </EditableCard>
        </div>
        
        {/* Ultra-realistic stamp - Responsive */}
        <EditableCard
          isEditing={editingCard === 'stamp'}
          onEdit={() => setEditingCard(editingCard === 'stamp' ? null : 'stamp')}
          style={{
            position: 'absolute',
            width: '12.5%',
            height: '19.1%',
            right: '3.5%',
            bottom: '5.3%',
            transform: 'rotate(-12deg)'
          }}
          editIcon={<Edit3 className="w-3 h-3 text-gray-600" />}
        >
          <div 
            className="w-full h-full border-2 border-dashed rounded-full flex items-center justify-center relative"
            style={{
              borderColor: 'rgba(139, 115, 85, 0.8)',
              background: `
                radial-gradient(circle at 30% 30%, rgba(220, 190, 150, 0.95) 0%, rgba(200, 170, 130, 0.9) 40%, rgba(180, 150, 110, 0.85) 100%),
                linear-gradient(135deg, rgba(240, 220, 180, 0.3) 0%, transparent 50%)
              `,
              boxShadow: `
                inset 0px 2px 8px rgba(0, 0, 0, 0.25),
                inset 0px -1px 4px rgba(255, 255, 255, 0.2),
                0px 4px 12px rgba(0, 0, 0, 0.15)
              `
            }}
          >
            {/* Stamp aging and wear */}
            <div 
              className="absolute inset-0 rounded-full pointer-events-none"
              style={{
                background: `
                  radial-gradient(circle at 70% 20%, rgba(160, 130, 90, 0.3) 0%, transparent 40%),
                  radial-gradient(circle at 20% 80%, rgba(140, 110, 70, 0.2) 0%, transparent 35%)
                `,
                mixBlendMode: 'multiply'
              }}
            />
            
            {/* Stamp inner details */}
            <div className="absolute inset-3 border border-amber-800/50 rounded-full" />
            <div className="absolute inset-5 border border-amber-800/30 rounded-full" />
            
            {/* Stamp text */}
            <div className="text-center leading-tight z-10" style={{ color: 'rgba(101, 84, 63, 0.9)' }}>
              <div style={{ fontSize: 'clamp(6px, 1.25vw, 9px)', fontWeight: '700', letterSpacing: '1px' }}>
                TRAVEL
              </div>
              <div style={{ fontSize: 'clamp(5px, 0.97vw, 7px)', fontWeight: '600', marginTop: '2px', letterSpacing: '0.5px' }}>
                MEMORIES
              </div>
              <div style={{ fontSize: 'clamp(5px, 0.97vw, 7px)', fontWeight: '500', marginTop: '1px' }}>
                2024
              </div>
            </div>
            
            {/* Stamp perforations with realistic variations */}
            {[...Array(20)].map((_, i) => (
              <div
                key={i}
                className="absolute rounded-full"
                style={{
                  width: '2px',
                  height: '2px',
                  backgroundColor: 'rgba(101, 84, 63, 0.4)',
                  top: '50%',
                  left: '50%',
                  transform: `translate(-50%, -50%) rotate(${i * 18}deg) translateY(-42px)`,
                  opacity: Math.random() > 0.1 ? 1 : 0.3 // Random missing perforations
                }}
              />
            ))}
            
            {/* Stamp smudge mark */}
            <div 
              className="absolute w-3 h-2 opacity-25"
              style={{
                background: 'radial-gradient(ellipse, rgba(101, 84, 63, 0.4) 0%, transparent 70%)',
                bottom: '15%',
                right: '20%',
                transform: 'rotate(20deg)',
                filter: 'blur(0.5px)'
              }}
            />
          </div>
        </EditableCard>
        
        {/* Additional realistic aging effects */}
        <div 
          className="absolute w-3 h-1 opacity-15"
          style={{
            background: 'linear-gradient(45deg, rgba(120, 100, 70, 0.3), transparent)',
            top: '20%',
            left: '60%',
            transform: 'rotate(45deg)',
            filter: 'blur(0.5px)'
          }}
        />
      </motion.div>
    );
  };

  return (
    <section ref={sectionRef} className="py-12 md:py-16 lg:py-20 bg-white relative overflow-hidden">
      {/* Background decorations */}
      <div className="absolute inset-0 opacity-10 overflow-hidden">
        {/* Vintage Stamps */}
        <div className="absolute top-20 left-20 w-16 h-20 bg-warm-coral/30 rounded-sm rotate-12 border-2 border-warm-coral/20"></div>
        <div className="absolute top-40 right-32 w-12 h-16 bg-warm-coral/30 rounded-sm -rotate-6 border-2 border-warm-coral/20"></div>
        <div className="absolute bottom-32 left-40 w-14 h-18 bg-warm-coral/20 rounded-sm rotate-45 border-2 border-warm-coral/15"></div>
        
        {/* Paper Textures */}
        <div className="absolute top-1/3 right-1/4 w-32 h-32 bg-warm-coral/10 rounded-full blur-2xl"></div>
        <div className="absolute bottom-1/3 left-1/4 w-40 h-40 bg-warm-coral/10 rounded-full blur-2xl"></div>
        
        {/* Ink Splatters */}
        <div className="absolute top-1/2 left-1/2 w-8 h-8 bg-warm-coral/20 rounded-full blur-sm"></div>
        <div className="absolute top-1/4 right-1/3 w-6 h-6 bg-warm-coral/20 rounded-full blur-sm"></div>
        <div className="absolute bottom-1/4 left-1/3 w-4 h-4 bg-warm-coral/15 rounded-full blur-sm"></div>
      </div>

      <div className="container mx-auto px-4 relative z-10">
        {/* Header */}
        <div className="text-center mb-12 md:mb-16">
          <div className="inline-flex items-center gap-2 bg-gray-50 px-4 py-2 rounded-full mb-4">
            <Camera className="w-4 h-4 text-warm-coral" />
            <span className="text-sm text-gray-700">Custom Memories</span>
          </div>
          
          <h2 className="text-3xl md:text-4xl lg:text-5xl xl:text-6xl text-gray-900 mb-6">
            <span className="font-light">The Only Card That Sends Your</span>
            <span className="block">
              <span className="font-bold text-warm-coral italic">
                Holiday
              </span>{' '}
              <span className="font-normal">Home.</span>
            </span>
          </h2>
          
          <p className="text-lg md:text-xl text-gray-600 leading-relaxed max-w-3xl mx-auto">
            Transform your travel memories into beautiful, personalized postcards that capture the essence of your adventures.
          </p>
        </div>

        {/* Centered Postcard Preview - Enhanced with Animations */}
        <div className="flex justify-center mb-12 px-4">
          <motion.div 
            className="relative group w-full max-w-4xl"
            initial={{ opacity: 0, y: 30, scale: 0.95 }}
            animate={{ opacity: 1, y: 0, scale: 1 }}
            transition={{ 
              duration: 0.6, 
              ease: [0.25, 0.1, 0.25, 1],
              delay: 0.2
            }}
          >
            {/* Interactive 3D Container */}
            <motion.div 
              ref={postcardRef}
              className="relative w-full overflow-hidden rounded-xl cursor-pointer"
              style={{ 
                aspectRatio: '720 / 470',
                maxWidth: '720px',
                maxHeight: '470px',
                minWidth: '280px',
                minHeight: '183px',
                rotateX,
                rotateY,
                transformStyle: "preserve-3d"
              }}
              onMouseMove={handleMouseMove}
              onMouseLeave={handleMouseLeave}
              whileHover={{ 
                scale: 1.02,
                boxShadow: "0 25px 50px -12px rgba(0, 0, 0, 0.25), 0 0 0 1px rgba(99, 102, 241, 0.1)",
                transition: { duration: 0.3 }
              }}
              animate={{
                y: [0, -8, 0],
              }}
              transition={{
                y: {
                  duration: 4,
                  repeat: Infinity,
                  ease: "easeInOut"
                }
              }}
            >
              <PostcardFrame />
              
              {/* Subtle glow effect */}
              <motion.div
                className="absolute inset-0 rounded-xl opacity-0 pointer-events-none"
                style={{
                  background: "radial-gradient(circle at center, rgba(99, 102, 241, 0.1) 0%, transparent 70%)",
                  filter: "blur(20px)"
                }}
                whileHover={{ opacity: 0.5 }}
                transition={{ duration: 0.3 }}
              />
            </motion.div>

            {/* Animated Edit Instructions */}
            {editingCard && (
              <motion.div 
                className="absolute -top-12 left-1/2 transform -translate-x-1/2 bg-primary text-white px-3 py-2 md:px-4 md:py-2 rounded-lg shadow-lg text-xs md:text-sm font-medium whitespace-nowrap z-20"
                initial={{ opacity: 0, y: 10, scale: 0.9 }}
                animate={{ opacity: 1, y: 0, scale: 1 }}
                exit={{ opacity: 0, y: -10, scale: 0.9 }}
                transition={{ duration: 0.3, ease: "easeOut" }}
              >
                <motion.span
                  animate={{ 
                    color: ["#ffffff", "#e0e7ff", "#ffffff"]
                  }}
                  transition={{ 
                    duration: 2, 
                    repeat: Infinity,
                    ease: "easeInOut"
                  }}
                >
                  Click on any element to edit it
                </motion.span>
                <div className="absolute top-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-l-4 border-r-4 border-t-4 border-transparent border-t-primary"></div>
              </motion.div>
            )}
          </motion.div>
        </div>

        {/* Animated Editing Panel */}
        {editingCard && (
          <motion.div 
            className="max-w-md mx-auto mb-12 p-6 bg-gray-50 rounded-lg border"
            initial={{ opacity: 0, y: 20, scale: 0.95 }}
            animate={{ opacity: 1, y: 0, scale: 1 }}
            exit={{ opacity: 0, y: -20, scale: 0.95 }}
            transition={{ duration: 0.4, ease: "easeOut" }}
          >
            <h3 className="font-medium mb-4 capitalize">Edit {editingCard}</h3>
            {editingCard === 'photo' && (
              <div>
                <label className="block text-sm font-medium mb-2">Photo URL</label>
                <input
                  type="text"
                  value={postcardData.photo}
                  onChange={(e) => setPostcardData(prev => ({ ...prev, photo: e.target.value }))}
                  className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
                  placeholder="Enter image URL"
                />
              </div>
            )}
            {editingCard === 'message' && (
              <div>
                <label className="block text-sm font-medium mb-2">Message</label>
                <textarea
                  value={postcardData.message}
                  onChange={(e) => setPostcardData(prev => ({ ...prev, message: e.target.value }))}
                  className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary h-32 resize-none"
                  placeholder="Enter your message"
                />
              </div>
            )}
            {editingCard === 'date' && (
              <div>
                <label className="block text-sm font-medium mb-2">Date Text</label>
                <input
                  type="text"
                  value={postcardData.date}
                  onChange={(e) => setPostcardData(prev => ({ ...prev, date: e.target.value }))}
                  className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
                  placeholder="Enter date text"
                />
              </div>
            )}
            {editingCard === 'label' && (
              <div>
                <label className="block text-sm font-medium mb-2">Address Label</label>
                <input
                  type="text"
                  value={postcardData.addressLabel}
                  onChange={(e) => setPostcardData(prev => ({ ...prev, addressLabel: e.target.value }))}
                  className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
                  placeholder="Enter label text"
                />
              </div>
            )}
            {editingCard === 'stamp' && (
              <div>
                <p className="text-sm text-gray-600">Stamp design features authentic vintage styling with realistic aging effects.</p>
              </div>
            )}
            <div className="flex gap-2 mt-4">
              <motion.div
                whileHover={{ scale: 1.05 }}
                whileTap={{ scale: 0.95 }}
              >
                <Button
                  onClick={() => setEditingCard(null)}
                  variant="outline"
                  size="sm"
                  className="flex-1"
                >
                  Done
                </Button>
              </motion.div>
            </div>
          </motion.div>
        )}

        {/* Enhanced Call to Action */}
        <div className="text-center">
          <Button 
            onClick={handleCreatePostcard}
            className="bg-primary hover:bg-primary/90 text-white px-8 py-3 rounded-full shadow-lg hover:shadow-xl transition-all duration-300 transform hover:scale-105 mx-auto mb-4"
          >
            Customize Your Postcard
            <ArrowRight className="w-5 h-5 ml-2" />
          </Button>
          <motion.p 
            className="text-gray-600"
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
            transition={{ duration: 0.5, delay: 0.6 }}
          >
            Click on any postcard element above to edit it, or create a completely new design
          </motion.p>
        </div>

        {/* Enhanced Features Section */}
        <div className="grid grid-cols-1 md:grid-cols-3 gap-6 max-w-5xl mx-auto mb-10 mt-2">
          {[
            {
              icon: Palette,
              title: "Authentic Design",
              description: "Realistic vintage styling with aging effect, paper texture, and authentic details"
            },
            {
              icon: Edit,
              title: "Handwritten Style",
              description: "Beautiful cursive fonts with realistic ink bleeding and natural imperfections"
            },
            {
              icon: Camera,
              title: "Easy Customization",
              description: "Click any element to edit photos, messages, and details in real-time"
            }
          ].map((feature, index) => (
            <motion.div
              key={index}
              className="text-center group p-6 rounded-xl bg-white hover:bg-gray-50 transition-all duration-300 border border-gray-100 hover:border-warm-coral/20 hover:shadow-lg"
              initial={{ opacity: 0, y: 20 }}
              animate={{ opacity: 1, y: 0 }}
              transition={{ duration: 0.5, delay: 0.2 + index * 0.1 }}
              whileHover={{
                y: -5,
                transition: { duration: 0.2 }
              }}
            >
              <motion.div
                className="mx-auto mb-4 p-3 rounded-full bg-warm-coral/10 group-hover:bg-warm-coral/20 transition-all duration-300 w-16 h-16 flex items-center justify-center"
                whileHover={{
                  scale: 1.1,
                  rotate: 360,
                  transition: { duration: 0.5 }
                }}
              >
                <feature.icon className="w-8 h-8 text-warm-coral" />
              </motion.div>
              <h3 className="mb-2 text-gray-900 group-hover:text-warm-coral transition-colors duration-300">
                {feature.title}
              </h3>
              <p className="text-gray-600 text-sm leading-relaxed">
                {feature.description}
              </p>
            </motion.div>
          ))}
        </div>
      </div>
    </section>
  );
}