* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Arial', sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
}

.reels-container {
    width: 350px;
    height: 600px;
    background: #000;
    border-radius: 20px;
    overflow: hidden;
    position: relative;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
}

.reels-header {
    background: rgba(0, 0, 0, 0.8);
    padding: 15px;
    text-align: center;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: 10;
}

.reels-header h2 {
    color: white;
    font-size: 18px;
    font-weight: bold;
}

.reels-wrapper {
    height: 100%;
    position: relative;
    overflow: hidden;
}

.reel-item {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    transform: translateY(100%);
    transition: all 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}

.reel-item.active {
    opacity: 1;
    transform: translateY(0);
}

.reel-item.prev {
    transform: translateY(-100%);
    opacity: 0;
}

.video-container {
    width: 100%;
    height: 100%;
    position: relative;
}

.video-container iframe {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.reel-overlay {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: linear-gradient(transparent, rgba(0, 0, 0, 0.8));
    padding: 20px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
}

.reel-info {
    flex: 1;
    color: white;
}

.reel-info h3 {
    font-size: 16px;
    margin-bottom: 5px;
    font-weight: bold;
}

.reel-info p {
    font-size: 14px;
    margin-bottom: 3px;
    opacity: 0.9;
}

.description {
    font-size: 13px !important;
    opacity: 0.8 !important;
    line-height: 1.3;
}

.reel-actions {
    display: flex;
    flex-direction: column;
    gap: 15px;
    align-items: center;
}

.action-btn {
    background: none;
    border: none;
    color: white;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
    transition: transform 0.2s;
}

.action-btn:hover {
    transform: scale(1.1);
}

.action-btn .icon {
    font-size: 24px;
    filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.5));
}

.action-btn .count {
    font-size: 12px;
    font-weight: bold;
}

.like-btn.liked .icon {
    color: #ff3040;
}

.navigation {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    flex-direction: column;
    gap: 10px;
    z-index: 10;
}

.nav-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    color: white;
    font-size: 16px;
    cursor: pointer;
    backdrop-filter: blur(10px);
    transition: all 0.3s;
}

.nav-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: scale(1.1);
}

.nav-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.reel-indicators {
    position: absolute;
    top: 70px;
    right: 15px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 10;
}

.indicator {
    width: 3px;
    height: 20px;
    background: rgba(255, 255, 255, 0.4);
    border-radius: 2px;
    transition: all 0.3s;
}

.indicator.active {
    background: white;
    height: 25px;
}

/* Mobile Responsiveness */
@media (max-width: 480px) {
    .reels-container {
        width: 100%;
        height: 100vh;
        border-radius: 0;
        max-width: 400px;
    }
    
    body {
        padding: 0;
    }
}

/* Loading Animation */
.loading {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: white;
    font-size: 18px;
}

.loading::after {
    content: '';
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-top: 2px solid white;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-left: 10px;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Smooth transitions */
.reel-item {
    transition: all 0.5s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

/* Custom scrollbar for webkit browsers */
::-webkit-scrollbar {
    display: none;
}

class DailymotionReelsWidget {
    constructor() {
        this.currentIndex = 0;
        this.reels = document.querySelectorAll('.reel-item');
        this.indicators = document.querySelectorAll('.indicator');
        this.prevBtn = document.querySelector('.prev-btn');
        this.nextBtn = document.querySelector('.next-btn');
        this.isTransitioning = false;
        
        this.init();
    }

    init() {
        this.setupEventListeners();
        this.updateNavigation();
        this.setupTouchGestures();
        this.setupKeyboardNavigation();
        this.autoPlay();
    }

    setupEventListeners() {
        // Navigation buttons
        this.prevBtn.addEventListener('click', () => this.previousReel());
        this.nextBtn.addEventListener('click', () => this.nextReel());

        // Action buttons
        document.querySelectorAll('.like-btn').forEach(btn => {
            btn.addEventListener('click', (e) => this.handleLike(e));
        });

        document.querySelectorAll('.comment-btn').forEach(btn => {
            btn.addEventListener('click', (e) => this.handleComment(e));
        });

        document.querySelectorAll('.share-btn').forEach(btn => {
            btn.addEventListener('click', (e) => this.handleShare(e));
        });

        // Indicator clicks
        this.indicators.forEach((indicator, index) => {
            indicator.addEventListener('click', () => this.goToReel(index));
        });
    }

    setupTouchGestures() {
        let startY = 0;
        let endY = 0;
        const container = document.querySelector('.reels-container');

        container.addEventListener('touchstart', (e) => {
            startY = e.touches[0].clientY;
        });

        container.addEventListener('touchend', (e) => {
            endY = e.changedTouches[0].clientY;
            this.handleSwipe(startY, endY);
        });

        // Mouse wheel support
        container.addEventListener('wheel', (e) => {
            e.preventDefault();
            if (e.deltaY > 0) {
                this.nextReel();
            } else {
                this.previousReel();
            }
        });
    }

    setupKeyboardNavigation() {
        document.addEventListener('keydown', (e) => {
            switch(e.key) {
                case 'ArrowUp':
                    e.preventDefault();
                    this.previousReel();
                    break;
                case 'ArrowDown':
                    e.preventDefault();
                    this.nextReel();
                    break;
                case ' ':
                    e.preventDefault();
                    this.togglePlayPause();
                    break;
            }
        });
    }

    handleSwipe(startY, endY) {
        const threshold = 50;
        const diff = startY - endY;

        if (Math.abs(diff) > threshold) {
            if (diff > 0) {
                this.nextReel();
            } else {
                this.previousReel();
            }
        }
    }

    nextReel() {
        if (this.isTransitioning) return;
        
        if (this.currentIndex < this.reels.length - 1) {
            this.goToReel(this.currentIndex + 1);
        }
    }

    previousReel() {
        if (this.isTransitioning) return;
        
        if (this.currentIndex > 0) {
            this.goToReel(this.currentIndex - 1);
        }
    }

    goToReel(index) {
        if (this.isTransitioning || index === this.currentIndex) return;
        
        this.isTransitioning = true;
        
        // Remove active class from current reel
        this.reels[this.currentIndex].classList.remove('active');
        this.indicators[this.currentIndex].classList.remove('active');
        
        // Add active class to new reel
        this.currentIndex = index;
        this.reels[this.currentIndex].classList.add('active');
        this.indicators[this.currentIndex].classList.add('active');
        
        this.updateNavigation();
        
        // Reset transition flag
        setTimeout(() => {
            this.isTransitioning = false;
        }, 500);

        // Load video if needed
        this.loadVideo(index);
    }

    updateNavigation() {
        this.prevBtn.disabled = this.currentIndex === 0;
        this.nextBtn.disabled = this.currentIndex === this.reels.length - 1;
    }

    loadVideo(index) {
        const reel = this.reels[index];
        const iframe = reel.querySelector('iframe');
        const videoId = reel.dataset.videoId;
        
        if (iframe && videoId) {
            // Update iframe src to trigger video load
            iframe.src = `https://www.dailymotion.com/embed/video/${videoId}?autoplay=1&mute=0`;
        }
    }

    handleLike(e) {
        e.preventDefault();
        const btn = e.currentTarget;
        const countSpan = btn.querySelector('.count');
        let count = parseInt(countSpan.textContent.replace('K', '00').replace('.', ''));
        
        btn.classList.toggle('liked');
        
        if (btn.classList.contains('liked')) {
            count += 1;
            this.showFeedback('Liked! ❤️');
        } else {
            count -= 1;
        }
        
        // Format count back
        if (count >= 1000) {
            countSpan.textContent = (count / 1000).toFixed(1) + 'K';
        } else {
            countSpan.textContent = count.toString();
        }
    }

    handleComment(e) {
        e.preventDefault();
        this.showFeedback('Comments coming soon! 💬');
    }

    handleShare(e) {
        e.preventDefault();
        const reel = this.reels[this.currentIndex];
        const videoId = reel.dataset.videoId;
        const shareUrl = `https://www.dailymotion.com/video/${videoId}`;
        
        if (navigator.share) {
            navigator.share({
                title: 'Check out this video!',
                url: shareUrl
            });
        } else {
            navigator.clipboard.writeText(shareUrl);
            this.showFeedback('Link copied! 📤');
        }
    }

    showFeedback(message) {
        const feedback = document.createElement('div');
        feedback.className = 'feedback-message';
        feedback.textContent = message;
        feedback.style.cssText = `
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: rgba(0, 0, 0, 0.8);
            color: white;
            padding: 10px 20px;
            border-radius: 20px;
            z-index: 1000;
            font-size: 14px;
            pointer-events: none;
        `;
        
        document.body.appendChild(feedback);
        
        setTimeout(() => {
            feedback.remove();
        }, 2000);
    }

    togglePlayPause() {
        const currentReel = this.reels[this.currentIndex];
        const iframe = currentReel.querySelector('iframe');
        
        // This would require Dailymotion Player API for full control
        this.showFeedback('Use video controls to play/pause');
    }

    autoPlay() {
        // Optional: Auto-advance reels every 10 seconds
        setInterval(() => {
            if (this.currentIndex < this.reels.length - 1) {
                this.nextReel();
            } else {
                this.goToReel(0); // Loop back to first
            }
        }, 10000);
    }
}

// Initialize the widget when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
    new DailymotionReelsWidget();
});

// Add some sample video data (you can fetch this from an API)
const sampleVideos = [
    {
        id: 'x7tgad0',
        title: 'Amazing Nature Video',
        username: 'naturelover',
        description: 'Beautiful landscapes from around the world 🌍',
        likes: 1200,
        comments: 89,
        shares: 45
    },
    {
        id: 'x7u8vh2',
        title: 'Cooking Tutorial',
        username: 'chefmaster',
        description: 'Learn to cook amazing dishes! 👨‍🍳',
        likes: 856,
        comments: 23,
        shares: 12
    },
    {
        id: 'x7v9kl3',
        title: 'Music Performance',
        username: 'musicstar',
        description: 'Live acoustic session 🎵 #music #live',
        likes: 2100,
        comments: 156,
        shares: 78
    }
];
