class LiteraryTheme {
    constructor() {
        this.init();
    }

    init() {
        this.setupScrollToTop();
        this.setupMobileMenu();
        this.ensureLoadingHidden(); // 确保加载动画被隐藏
        this.setupSmoothScroll();
    }

    // 确保加载动画被隐藏（三重保险）
    ensureLoadingHidden() {
        const loadingElement = document.getElementById('loadingPlaceholder');
        if (loadingElement) {
            loadingElement.style.display = 'none';
            loadingElement.classList.add('loading-hidden');
        }
    }

    setupScrollToTop() {
        const scrollButton = document.createElement('button');
        scrollButton.innerHTML = '↑';
        scrollButton.className = 'scroll-to-top';
        scrollButton.setAttribute('aria-label', '回到顶部');
        scrollButton.setAttribute('title', '回到顶部');
        
        document.body.appendChild(scrollButton);

        // 滚动显示/隐藏按钮
        const toggleScrollButton = () => {
            if (window.pageYOffset > 300) {
                scrollButton.classList.add('show');
            } else {
                scrollButton.classList.remove('show');
            }
        };

        window.addEventListener('scroll', toggleScrollButton);
        
        // 点击回到顶部
        scrollButton.addEventListener('click', () => {
            window.scrollTo({
                top: 0,
                behavior: 'smooth'
            });
        });

        // 初始检查
        toggleScrollButton();
    }

    setupMobileMenu() {
        if (window.innerWidth <= 768) {
            const nav = document.querySelector('.main-navigation');
            const headerInner = document.querySelector('.header-inner');
            
            if (nav && headerInner) {
                // 创建移动菜单切换按钮
                const menuToggle = document.createElement('button');
                menuToggle.innerHTML = '☰';
                menuToggle.className = 'mobile-menu-toggle';
                menuToggle.setAttribute('aria-label', '切换菜单');
                
                // 样式
                menuToggle.style.cssText = `
                    display: block;
                    background: rgba(255, 255, 255, 0.2);
                    color: white;
                    border: none;
                    padding: 10px 15px;
                    border-radius: var(--border-radius);
                    cursor: pointer;
                    font-size: 1.2rem;
                    transition: var(--transition);
                `;
                
                headerInner.appendChild(menuToggle);
                
                // 移动端导航样式
                nav.style.cssText = `
                    display: none;
                    width: 100%;
                    margin-top: 15px;
                    background: rgba(255, 255, 255, 0.1);
                    backdrop-filter: blur(10px);
                    border-radius: var(--border-radius);
                    padding: 15px;
                `;
                
                nav.querySelector('ul').style.cssText = `
                    flex-direction: column;
                    gap: 10px;
                    align-items: stretch;
                `;
                
                nav.querySelectorAll('.nav-link').forEach(link => {
                    link.style.cssText = `
                        display: block;
                        padding: 12px 15px;
                        border-radius: var(--border-radius);
                        text-align: center;
                        background: rgba(255, 255, 255, 0.1);
                    `;
                });
                
                // 切换菜单
                menuToggle.addEventListener('click', () => {
                    const isHidden = nav.style.display === 'none';
                    nav.style.display = isHidden ? 'block' : 'none';
                    menuToggle.innerHTML = isHidden ? '✕' : '☰';
                    menuToggle.style.background = isHidden ? 'rgba(255, 255, 255, 0.3)' : 'rgba(255, 255, 255, 0.2)';
                });
            }
        }
    }

    setupSmoothScroll() {
        const internalLinks = document.querySelectorAll('a[href^="#"]');
        internalLinks.forEach(link => {
            link.addEventListener('click', (e) => {
                const href = link.getAttribute('href');
                if (href !== '#') {
                    e.preventDefault();
                    const target = document.querySelector(href);
                    if (target) {
                        const headerHeight = document.querySelector('.site-header').offsetHeight;
                        const targetPosition = target.offsetTop - headerHeight - 20;
                        
                        window.scrollTo({
                            top: targetPosition,
                            behavior: 'smooth'
                        });
                    }
                }
            });
        });
    }
}

// 简化工具函数
const LiteraryUtils = {
    // 防抖函数
    debounce: (func, wait) => {
        let timeout;
        return function executedFunction(...args) {
            const later = () => {
                clearTimeout(timeout);
                func(...args);
            };
            clearTimeout(timeout);
            timeout = setTimeout(later, wait);
        };
    },
    
    // 检测移动设备
    isMobile: () => {
        return window.innerWidth <= 768;
    }
};

// 优化初始化 - 立即执行，不等待DOMContentLoaded
(function() {
    // 立即隐藏加载动画
    const loadingElement = document.getElementById('loadingPlaceholder');
    if (loadingElement) {
        loadingElement.style.display = 'none';
    }
    
    // 初始化主题
    window.literaryTheme = new LiteraryTheme();
    
    // 错误处理
    window.addEventListener('error', (e) => {
        console.error('文学主题JavaScript错误:', e.error);
    });
})();

// 导出到全局作用域
window.LiteraryTheme = LiteraryTheme;
window.LiteraryUtils = LiteraryUtils;