<?php
/**
 * Landing Page Editor - MontaLoja SuperAdmin
 * Editor visual para configurar a landing page
 */
require_once __DIR__ . '/../../../includes/config.php';
require_once __DIR__ . '/../../../includes/landing-functions.php';

// Verificar autenticacao Super Admin
if (!isSuperAdmin()) {
    header('Location: /pages/login.php');
    exit;
}

$superAdmin = getSuperAdmin();
$pdo = getDB();
$message = '';
$messageType = '';

// Verificar mensagem de sessão
if (isset($_SESSION['landing_message'])) {
    $message = $_SESSION['landing_message'];
    $messageType = $_SESSION['landing_message_type'] ?? 'info';
    unset($_SESSION['landing_message'], $_SESSION['landing_message_type']);
}

// Carregar todas as configurações
$config = getAllLandingConfig($pdo);

// Extrair variáveis para uso no template
$siteName = $config['header_logo_text'] ?? 'MontaLoja';
$siteDescription = $config['seo_description'] ?? '';
$headerLogo = $config['header_logo'] ?? '/assets/images/logo.png';
$headerLogoType = $config['header_logo_type'] ?? 'text';
$headerLogoDark = $config['header_logo_dark'] ?? '';
$navMenu = json_decode($config['header_menu'] ?? '[]', true) ?: [
    ['label' => 'Recursos', 'url' => '#recursos', 'icon' => 'solar:star-bold', 'active' => 1],
    ['label' => 'Preços', 'url' => '#precos', 'icon' => 'solar:tag-price-bold', 'active' => 1],
    ['label' => 'Depoimentos', 'url' => '#depoimentos', 'icon' => 'solar:chat-round-like-bold', 'active' => 1]
];

// Hero
$heroBadge = $config['hero_badge'] ?? '';
$heroTitle = $config['hero_title'] ?? '';
$heroSubtitle = $config['hero_subtitle'] ?? '';
$heroCtaPrimary = $config['hero_btn_primary_text'] ?? '';
$heroCtaSecondary = $config['hero_btn_secondary_text'] ?? '';
$heroImage = $config['hero_image'] ?? '';
$showHero = ($config['show_hero'] ?? '1') === '1';

// Features
$featuresTitle = $config['features_title'] ?? '';
$featuresSubtitle = $config['features_subtitle'] ?? '';
$featuresItems = json_decode($config['features_items'] ?? '[]', true) ?: [];
$showFeatures = ($config['show_features'] ?? '1') === '1';

// Pricing
$pricingTitle = $config['pricing_title'] ?? '';
$pricingSubtitle = $config['pricing_subtitle'] ?? '';
$showPricing = ($config['show_pricing'] ?? '1') === '1';

// Testimonials
$testimonialsTitle = $config['testimonials_title'] ?? '';
$testimonialsSubtitle = $config['testimonials_subtitle'] ?? '';
$showTestimonials = ($config['show_testimonials'] ?? '1') === '1';

// CTA
$ctaTitle = $config['cta_title'] ?? '';
$ctaSubtitle = $config['cta_subtitle'] ?? '';
$ctaButton = $config['cta_btn_text'] ?? '';
$showCta = ($config['show_cta'] ?? '1') === '1';

// Cores
$colors = json_decode($config['colors'] ?? '{}', true) ?: [
    'primary' => '#CCA43B',
    'secondary' => '#1A1A1A',
    'accent' => '#FFD700'
];

$pageTitle = 'Editor da Landing Page';
$currentPage = 'landing';

include __DIR__ . '/../includes/header.php';
?>
    <div class="app-container">
        <?php include __DIR__ . '/../includes/sidebar.php'; ?>

        <main class="main-content">
            <?php include __DIR__ . '/../includes/topbar.php'; ?>

            <div class="content-area">
                <?php if ($message): ?>
                    <div class="alert alert-<?= $messageType ?>">
                        <iconify-icon icon="<?= $messageType === 'success' ? 'solar:check-circle-bold' : 'solar:danger-triangle-bold' ?>"></iconify-icon>
                        <?= htmlspecialchars($message) ?>
                    </div>
                <?php endif; ?>

                <form id="landing-form" method="POST" action="process.php" enctype="multipart/form-data">
                    <!-- Tabs Navigation -->
                    <div class="tabs-nav">
                        <button type="button" class="tab-btn active" data-tab="geral">
                            <iconify-icon icon="solar:settings-bold"></iconify-icon> Geral
                        </button>
                        <button type="button" class="tab-btn" data-tab="hero">
                            <iconify-icon icon="solar:star-bold"></iconify-icon> Hero
                        </button>
                        <button type="button" class="tab-btn" data-tab="features">
                            <iconify-icon icon="solar:widget-bold"></iconify-icon> Recursos
                        </button>
                        <button type="button" class="tab-btn" data-tab="pricing">
                            <iconify-icon icon="solar:tag-price-bold"></iconify-icon> Preços
                        </button>
                        <button type="button" class="tab-btn" data-tab="testimonials">
                            <iconify-icon icon="solar:chat-round-like-bold"></iconify-icon> Depoimentos
                        </button>
                        <button type="button" class="tab-btn" data-tab="cta">
                            <iconify-icon icon="solar:call-chat-bold"></iconify-icon> CTA
                        </button>
                        <button type="button" class="tab-btn" data-tab="colors">
                            <iconify-icon icon="solar:pallete-2-bold"></iconify-icon> Cores
                        </button>
                    </div>

                    <!-- Tab: Geral -->
                    <div class="tab-content active" id="tab-geral">
                        <div class="panel">
                            <div class="section-header">
                                <h3 class="section-title">
                                    <iconify-icon icon="solar:settings-bold"></iconify-icon>
                                    Configurações Gerais
                                </h3>
                                <a href="/" class="btn btn-secondary" target="_blank">
                                    <iconify-icon icon="solar:eye-bold"></iconify-icon>
                                    Ver Site
                                </a>
                            </div>
                            <!-- Tipo de Logo -->
                            <div class="form-group">
                                <label class="form-label">Tipo de Logo</label>
                                <div class="logo-type-selector">
                                    <label class="radio-card">
                                        <input type="radio" name="header_logo_type" value="text" <?= $headerLogoType === 'text' ? 'checked' : '' ?>>
                                        <div class="radio-card-content">
                                            <iconify-icon icon="solar:text-bold"></iconify-icon>
                                            <span>Texto</span>
                                        </div>
                                    </label>
                                    <label class="radio-card">
                                        <input type="radio" name="header_logo_type" value="image" <?= $headerLogoType === 'image' ? 'checked' : '' ?>>
                                        <div class="radio-card-content">
                                            <iconify-icon icon="solar:gallery-bold"></iconify-icon>
                                            <span>Imagem</span>
                                        </div>
                                    </label>
                                </div>
                            </div>

                            <div class="grid-2">
                                <div class="form-group">
                                    <label class="form-label">Nome do Site</label>
                                    <input type="text" name="header_logo_text" class="form-input" value="<?= htmlspecialchars($siteName) ?>">
                                </div>
                                <div class="form-group">
                                    <label class="form-label">Descrição do Site (SEO)</label>
                                    <input type="text" name="seo_description" class="form-input" value="<?= htmlspecialchars($siteDescription) ?>">
                                </div>
                            </div>

                            <!-- Upload de Logo -->
                            <div class="form-group logo-upload-section" id="logoUploadSection">
                                <label class="form-label">Logo da Página</label>
                                <div class="logo-preview-container">
                                    <div class="logo-preview" id="logoPreview">
                                        <?php if (!empty($headerLogo) && $headerLogoType === 'image'): ?>
                                            <img src="<?= htmlspecialchars($headerLogo) ?>" alt="Logo atual" id="currentLogo">
                                        <?php else: ?>
                                            <div class="logo-placeholder">
                                                <iconify-icon icon="solar:gallery-add-bold" width="48"></iconify-icon>
                                                <span>Nenhum logo enviado</span>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                    <div class="logo-upload-actions">
                                        <label class="btn btn-primary btn-sm">
                                            <iconify-icon icon="solar:upload-bold"></iconify-icon>
                                            Enviar Logo
                                            <input type="file" name="header_logo_file" accept="image/*" onchange="previewLogo(this)" style="display: none;">
                                        </label>
                                        <input type="hidden" name="header_logo" value="<?= htmlspecialchars($headerLogo) ?>">
                                        <p class="text-muted text-sm mt-2">Formatos: PNG, JPG, SVG. Tamanho máximo: 2MB</p>
                                    </div>
                                </div>
                            </div>

                            <!-- Upload de Logo Dark -->
                            <div class="form-group logo-upload-section" id="logoDarkUploadSection">
                                <label class="form-label">
                                    <iconify-icon icon="solar:moon-bold" style="margin-right: 0.5rem;"></iconify-icon>
                                    Logo para Modo Escuro
                                </label>
                                <p class="text-muted text-sm mb-2">Este logo será exibido quando o usuário estiver no modo escuro</p>
                                <div class="logo-preview-container">
                                    <div class="logo-preview logo-preview-dark" id="logoDarkPreview">
                                        <?php if (!empty($headerLogoDark) && $headerLogoType === 'image'): ?>
                                            <img src="<?= htmlspecialchars($headerLogoDark) ?>" alt="Logo Dark atual" id="currentLogoDark">
                                        <?php else: ?>
                                            <div class="logo-placeholder">
                                                <iconify-icon icon="solar:moon-bold" width="48"></iconify-icon>
                                                <span>Nenhum logo dark enviado</span>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                    <div class="logo-upload-actions">
                                        <label class="btn btn-secondary btn-sm">
                                            <iconify-icon icon="solar:upload-bold"></iconify-icon>
                                            Enviar Logo Dark
                                            <input type="file" name="header_logo_dark_file" accept="image/*" onchange="previewLogoDark(this)" style="display: none;">
                                        </label>
                                        <input type="hidden" name="header_logo_dark" value="<?= htmlspecialchars($headerLogoDark) ?>">
                                        <p class="text-muted text-sm mt-2">Opcional. Use um logo claro para fundos escuros</p>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- Menu de Navegação -->
                        <div class="panel">
                            <div class="section-header">
                                <h3 class="section-title">
                                    <iconify-icon icon="solar:hamburger-menu-bold"></iconify-icon>
                                    Menu de Navegação
                                </h3>
                                <button type="button" id="btn-add-menu-item" class="btn btn-primary btn-sm">
                                    <iconify-icon icon="solar:add-circle-bold"></iconify-icon>
                                    Adicionar Item
                                </button>
                            </div>
                            <p style="margin-bottom: 1rem; color: var(--text-muted); font-size: 0.85rem; display: flex; align-items: center; gap: 0.5rem;">
                                <iconify-icon icon="solar:info-circle-linear"></iconify-icon>
                                Arraste os itens pela alça para reordenar
                            </p>

                            <div id="menu-editor-list" class="menu-sortable">
                                <?php foreach ($navMenu as $index => $item): ?>
                                <div class="menu-compact-item" data-index="<?= $index ?>" draggable="true">
                                    <div class="menu-compact-handle" title="Arraste para reordenar">
                                        <iconify-icon icon="solar:hamburger-menu-linear" width="18"></iconify-icon>
                                    </div>
                                    <div class="menu-compact-icon">
                                        <iconify-icon icon="<?= htmlspecialchars($item['icon'] ?? 'solar:link-bold') ?>" width="20"></iconify-icon>
                                    </div>
                                    <input type="text" name="menu_label[]" class="menu-compact-input" value="<?= htmlspecialchars($item['label'] ?? '') ?>" placeholder="Nome do menu">
                                    <input type="text" name="menu_url[]" class="menu-compact-input" value="<?= htmlspecialchars($item['url'] ?? '#') ?>" placeholder="Link">
                                    <input type="text" name="menu_icon[]" class="menu-compact-input menu-icon-field" value="<?= htmlspecialchars($item['icon'] ?? 'solar:link-bold') ?>" placeholder="Ícone" oninput="updateIconPreview(this)">
                                    <label class="menu-compact-toggle" title="Ativo/Inativo">
                                        <input type="checkbox" name="menu_active[<?= $index ?>]" <?= ($item['active'] ?? 1) ? 'checked' : '' ?>>
                                        <span class="toggle-indicator"></span>
                                    </label>
                                    <button type="button" onclick="removeMenuItem(this)" class="menu-compact-delete" title="Remover">
                                        <iconify-icon icon="solar:trash-bin-trash-bold" width="18"></iconify-icon>
                                    </button>
                                </div>
                                <?php endforeach; ?>
                            </div>
                        </div>
                        <!-- Botões do Header -->
                        <div class="panel">
                            <div class="section-header">
                                <h3 class="section-title">
                                    <iconify-icon icon="solar:cursor-bold"></iconify-icon>
                                    Botões do Header
                                </h3>
                            </div>
                            <p style="margin-bottom: 1rem; color: var(--text-muted); font-size: 0.85rem; display: flex; align-items: center; gap: 0.5rem;">
                                <iconify-icon icon="solar:info-circle-linear"></iconify-icon>
                                Configure os botões "Entrar" e "Começar Grátis" que aparecem no cabeçalho
                            </p>
                            
                            <div class="grid-2" style="margin-bottom: 1.5rem;">
                                <div style="background: var(--bg-darker); border-radius: 12px; padding: 1.25rem; border: 1px solid var(--border);">
                                    <h4 style="font-size: 0.9rem; font-weight: 600; margin-bottom: 1rem; display: flex; align-items: center; gap: 0.5rem; color: var(--text-primary);">
                                        <iconify-icon icon="solar:login-2-bold" style="color: var(--gold);"></iconify-icon>
                                        Botão "Entrar"
                                    </h4>
                                    <div class="form-group" style="margin-bottom: 0.75rem;">
                                        <label class="form-label">Texto do Botão</label>
                                        <input type="text" name="header_btn_login_text" class="form-input" value="<?= htmlspecialchars($config['header_btn_login_text'] ?? 'Entrar') ?>" placeholder="Ex: Entrar, Login, Acessar">
                                    </div>
                                    <div class="form-group" style="margin-bottom: 0;">
                                        <label class="form-label">Link de Destino</label>
                                        <input type="text" name="header_btn_login_link" class="form-input" value="<?= htmlspecialchars($config['header_btn_login_link'] ?? '/pages/login.php') ?>" placeholder="Ex: /pages/login.php">
                                    </div>
                                </div>
                                
                                <div style="background: var(--bg-darker); border-radius: 12px; padding: 1.25rem; border: 1px solid var(--border);">
                                    <h4 style="font-size: 0.9rem; font-weight: 600; margin-bottom: 1rem; display: flex; align-items: center; gap: 0.5rem; color: var(--text-primary);">
                                        <iconify-icon icon="solar:rocket-bold" style="color: var(--gold);"></iconify-icon>
                                        Botão "Começar Grátis"
                                    </h4>
                                    <div class="form-group" style="margin-bottom: 0.75rem;">
                                        <label class="form-label">Texto do Botão</label>
                                        <input type="text" name="header_btn_cta_text" class="form-input" value="<?= htmlspecialchars($config['header_btn_cta_text'] ?? 'Começar Grátis') ?>" placeholder="Ex: Começar Grátis, Criar Loja">
                                    </div>
                                    <div class="form-group" style="margin-bottom: 0;">
                                        <label class="form-label">Link de Destino</label>
                                        <input type="text" name="header_btn_cta_link" class="form-input" value="<?= htmlspecialchars($config['header_btn_cta_link'] ?? '#precos') ?>" placeholder="Ex: #precos, /pages/cadastro/">
                                    </div>
                                </div>
                            </div>
                            
                            <div style="background: rgba(204, 164, 59, 0.1); border-radius: 8px; padding: 1rem; border: 1px solid rgba(204, 164, 59, 0.2);">
                                <p style="font-size: 0.8rem; color: var(--text-secondary); margin: 0; display: flex; align-items: center; gap: 0.5rem;">
                                    <iconify-icon icon="solar:lightbulb-bolt-bold" style="color: var(--gold);"></iconify-icon>
                                    <strong>Dica:</strong> Use "#precos" para rolar até a seção de preços ou "/pages/cadastro/" para ir direto ao cadastro
                                </p>
                            </div>
                        </div>
                    </div>

                    <!-- Tab: Hero -->
                    <div class="tab-content" id="tab-hero">
                        <div class="panel">
                            <div class="section-header">
                                <h3 class="section-title">
                                    <iconify-icon icon="solar:star-bold"></iconify-icon>
                                    Seção Hero
                                </h3>
                                <label class="toggle-switch">
                                    <input type="checkbox" name="show_hero" <?= $showHero ? 'checked' : '' ?>>
                                    <span class="toggle-slider"></span>
                                    <span class="toggle-label">Ativo</span>
                                </label>
                            </div>

                            <div class="form-group">
                                <label class="form-label">Badge</label>
                                <input type="text" name="hero_badge" class="form-input" value="<?= htmlspecialchars($heroBadge) ?>" placeholder="Ex: 🚀 Novidade">
                            </div>

                            <div class="form-group">
                                <label class="form-label">Título Principal</label>
                                <input type="text" name="hero_title" class="form-input" value="<?= htmlspecialchars($heroTitle) ?>">
                            </div>

                            <div class="form-group">
                                <label class="form-label">Subtítulo</label>
                                <textarea name="hero_subtitle" class="form-input" style="min-height: 100px; resize: vertical;"><?= htmlspecialchars($heroSubtitle) ?></textarea>
                            </div>

                            <div class="grid-2">
                                <div class="form-group">
                                    <label class="form-label">Texto Botão Primário</label>
                                    <input type="text" name="hero_btn_primary_text" class="form-input" value="<?= htmlspecialchars($heroCtaPrimary) ?>">
                                </div>
                                <div class="form-group">
                                    <label class="form-label">Texto Botão Secundário</label>
                                    <input type="text" name="hero_btn_secondary_text" class="form-input" value="<?= htmlspecialchars($heroCtaSecondary) ?>">
                                </div>
                            </div>

                            <!-- Imagem do Hero -->
                            <div class="form-group" style="margin-top: 1.5rem; padding-top: 1.5rem; border-top: 1px solid var(--border);">
                                <label class="form-label" style="display: flex; align-items: center; gap: 0.5rem;">
                                    <iconify-icon icon="solar:gallery-bold"></iconify-icon>
                                    Imagem do Hero (Banner)
                                </label>
                                <p style="font-size: 0.8rem; color: var(--text-muted); margin-bottom: 0.75rem;">
                                    Faça upload de uma imagem para substituir o mockup animado. Se vazio, será exibido o mockup padrão. Tamanho recomendado: 600x500px
                                </p>
                                <div style="display: flex; gap: 1rem; align-items: flex-start; flex-wrap: wrap;">
                                    <div style="flex: 1; min-width: 200px;">
                                        <input type="file" name="hero_image_file" id="hero_image_file" class="form-input" accept="image/*" onchange="previewHeroImage(this)">
                                        <input type="hidden" name="hero_image" id="hero_image_hidden" value="<?= htmlspecialchars($heroImage) ?>">
                                    </div>
                                    <?php if (!empty($heroImage)): ?>
                                    <button type="button" onclick="clearHeroImage()" class="btn btn-danger btn-sm" title="Remover imagem" style="height: fit-content;">
                                        <iconify-icon icon="solar:trash-bin-trash-bold"></iconify-icon>
                                        Remover
                                    </button>
                                    <?php endif; ?>
                                </div>
                                <div id="hero-image-preview" style="margin-top: 1rem; <?= empty($heroImage) ? 'display:none;' : '' ?>">
                                    <?php if (!empty($heroImage)): ?>
                                    <img src="<?= htmlspecialchars($heroImage) ?>" alt="Preview" style="max-height: 200px; max-width: 100%; border-radius: 12px; border: 2px solid var(--border); box-shadow: 0 4px 12px rgba(0,0,0,0.1);">
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Tab: Features -->
                    <div class="tab-content" id="tab-features">
                        <div class="panel">
                            <div class="section-header">
                                <h3 class="section-title">
                                    <iconify-icon icon="solar:widget-bold"></iconify-icon>
                                    Seção de Recursos
                                </h3>
                                <label class="toggle-switch">
                                    <input type="checkbox" name="show_features" <?= $showFeatures ? 'checked' : '' ?>>
                                    <span class="toggle-slider"></span>
                                    <span class="toggle-label">Ativo</span>
                                </label>
                            </div>

                            <div class="grid-2">
                                <div class="form-group">
                                    <label class="form-label">Título da Seção</label>
                                    <input type="text" name="features_title" class="form-input" value="<?= htmlspecialchars($featuresTitle) ?>">
                                </div>
                                <div class="form-group">
                                    <label class="form-label">Subtítulo</label>
                                    <input type="text" name="features_subtitle" class="form-input" value="<?= htmlspecialchars($featuresSubtitle) ?>">
                                </div>
                            </div>
                        </div>

                        <div class="panel">
                            <div class="section-header">
                                <h3 class="section-title">Itens de Recursos</h3>
                                <button type="button" onclick="addFeature()" class="btn btn-primary btn-sm">
                                    <iconify-icon icon="solar:add-circle-bold"></iconify-icon>
                                    Adicionar Recurso
                                </button>
                            </div>
                            <div id="features-list" class="feature-cards-grid">
                                <?php foreach ($featuresItems as $i => $feature): ?>
                                <div class="feature-card" style="animation: fadeIn 0.3s ease">
                                    <div class="feature-card-header">
                                        <div class="feature-icon-preview" id="icon-preview-<?= $i ?>">
                                            <iconify-icon icon="<?= htmlspecialchars($feature['icon'] ?? 'solar:star-bold') ?>"></iconify-icon>
                                        </div>
                                        <div style="flex: 1;">
                                            <div style="font-weight: 600; color: var(--text-primary); font-size: 1rem;"><?= htmlspecialchars($feature['title'] ?? 'Novo Recurso') ?></div>
                                            <div style="font-size: 0.8rem; color: var(--text-muted); margin-top: 0.25rem;">Recurso #<?= $i + 1 ?></div>
                                        </div>
                                        <span class="feature-card-number">#<?= $i + 1 ?></span>
                                    </div>
                                    <div class="feature-card-body">
                                        <div class="feature-icon-input-group">
                                            <div class="form-group" style="margin-bottom: 0;">
                                                <label class="form-label">
                                                    <iconify-icon icon="solar:pallete-2-bold" style="margin-right: 0.25rem;"></iconify-icon>
                                                    Código do Ícone
                                                </label>
                                                <input type="text" name="feature_icon[]" class="form-input" value="<?= htmlspecialchars($feature['icon'] ?? '') ?>" oninput="updateFeatureIconPreview(this)" placeholder="solar:star-bold">
                                            </div>
                                        </div>
                                        <div class="form-group" style="margin-bottom: 0;">
                                            <label class="form-label">
                                                <iconify-icon icon="solar:text-bold" style="margin-right: 0.25rem;"></iconify-icon>
                                                Título
                                            </label>
                                            <input type="text" name="feature_title[]" class="form-input" value="<?= htmlspecialchars($feature['title'] ?? '') ?>" placeholder="Título do recurso">
                                        </div>
                                        <div class="form-group" style="margin-bottom: 0;">
                                            <label class="form-label">
                                                <iconify-icon icon="solar:document-text-bold" style="margin-right: 0.25rem;"></iconify-icon>
                                                Descrição
                                            </label>
                                            <textarea name="feature_description[]" class="form-input" style="min-height: 70px; resize: vertical;" placeholder="Descrição do recurso"><?= htmlspecialchars($feature['description'] ?? '') ?></textarea>
                                        </div>
                                    </div>
                                    <div class="feature-card-actions">
                                        <button type="button" onclick="removeFeatureCard(this)" class="btn btn-danger btn-sm">
                                            <iconify-icon icon="solar:trash-bin-trash-bold"></iconify-icon>
                                            Remover
                                        </button>
                                    </div>
                                </div>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    </div>

                    <!-- Tab: Pricing -->
                    <div class="tab-content" id="tab-pricing">
                        <div class="panel">
                            <div class="section-header">
                                <h3 class="section-title">
                                    <iconify-icon icon="solar:tag-price-bold"></iconify-icon>
                                    Seção de Preços
                                </h3>
                                <label class="toggle-switch">
                                    <input type="checkbox" name="show_pricing" <?= $showPricing ? 'checked' : '' ?>>
                                    <span class="toggle-slider"></span>
                                    <span class="toggle-label">Ativo</span>
                                </label>
                            </div>
                            <div class="grid-2">
                                <div class="form-group">
                                    <label class="form-label">Título</label>
                                    <input type="text" name="pricing_title" class="form-input" value="<?= htmlspecialchars($pricingTitle) ?>">
                                </div>
                                <div class="form-group">
                                    <label class="form-label">Subtítulo</label>
                                    <input type="text" name="pricing_subtitle" class="form-input" value="<?= htmlspecialchars($pricingSubtitle) ?>">
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Tab: Testimonials -->
                    <div class="tab-content" id="tab-testimonials">
                        <div class="panel">
                            <div class="section-header">
                                <h3 class="section-title">
                                    <iconify-icon icon="solar:chat-round-like-bold"></iconify-icon>
                                    Seção de Depoimentos
                                </h3>
                                <label class="toggle-switch">
                                    <input type="checkbox" name="show_testimonials" <?= $showTestimonials ? 'checked' : '' ?>>
                                    <span class="toggle-slider"></span>
                                    <span class="toggle-label">Ativo</span>
                                </label>
                            </div>
                            <div class="grid-2">
                                <div class="form-group">
                                    <label class="form-label">Título</label>
                                    <input type="text" name="testimonials_title" class="form-input" value="<?= htmlspecialchars($testimonialsTitle) ?>">
                                </div>
                                <div class="form-group">
                                    <label class="form-label">Subtítulo</label>
                                    <input type="text" name="testimonials_subtitle" class="form-input" value="<?= htmlspecialchars($testimonialsSubtitle) ?>">
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Tab: CTA -->
                    <div class="tab-content" id="tab-cta">
                        <div class="panel">
                            <div class="section-header">
                                <h3 class="section-title">
                                    <iconify-icon icon="solar:call-chat-bold"></iconify-icon>
                                    Seção CTA (Call to Action)
                                </h3>
                                <label class="toggle-switch">
                                    <input type="checkbox" name="show_cta" <?= $showCta ? 'checked' : '' ?>>
                                    <span class="toggle-slider"></span>
                                    <span class="toggle-label">Ativo</span>
                                </label>
                            </div>
                            <div class="form-group">
                                <label class="form-label">Título</label>
                                <input type="text" name="cta_title" class="form-input" value="<?= htmlspecialchars($ctaTitle) ?>">
                            </div>
                            <div class="form-group">
                                <label class="form-label">Subtítulo</label>
                                <input type="text" name="cta_subtitle" class="form-input" value="<?= htmlspecialchars($ctaSubtitle) ?>">
                            </div>
                            <div class="form-group">
                                <label class="form-label">Texto do Botão</label>
                                <input type="text" name="cta_btn_text" class="form-input" value="<?= htmlspecialchars($ctaButton) ?>">
                            </div>
                        </div>
                    </div>

                    <!-- Tab: Colors -->
                    <div class="tab-content" id="tab-colors">
                        <div class="panel">
                            <div class="section-header">
                                <h3 class="section-title">
                                    <iconify-icon icon="solar:pallete-2-bold"></iconify-icon>
                                    Esquema de Cores
                                </h3>
                            </div>
                            <div class="grid-3">
                                <div class="form-group">
                                    <label class="form-label">Cor Primária</label>
                                    <input type="color" name="style_primary_color" class="form-input" value="<?= htmlspecialchars($colors['primary'] ?? '#CCA43B') ?>" style="height: 50px; padding: 5px;">
                                </div>
                                <div class="form-group">
                                    <label class="form-label">Cor Secundária</label>
                                    <input type="color" name="style_secondary_color" class="form-input" value="<?= htmlspecialchars($colors['secondary'] ?? '#1A1A1A') ?>" style="height: 50px; padding: 5px;">
                                </div>
                                <div class="form-group">
                                    <label class="form-label">Cor de Destaque</label>
                                    <input type="color" name="style_accent_color" class="form-input" value="<?= htmlspecialchars($colors['accent'] ?? '#FFD700') ?>" style="height: 50px; padding: 5px;">
                                </div>
                            </div>
                        </div>
                    </div>

                </form>
                
                <!-- Botão de Salvar Fixo -->
                <div class="save-bar">
                    <button type="submit" form="landing-form" class="btn btn-primary btn-lg">
                        <iconify-icon icon="solar:diskette-bold"></iconify-icon>
                        Salvar Configurações
                    </button>
                </div>
            </div>
        </main>
    </div>

    <style>
        /* Logo Type Selector */
        .logo-type-selector {
            display: flex;
            gap: 1rem;
        }

        .radio-card {
            flex: 1;
            cursor: pointer;
        }

        .radio-card input {
            display: none;
        }

        .radio-card-content {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            gap: 0.5rem;
            padding: 1rem;
            background: var(--bg-input);
            border: 2px solid var(--border-light);
            border-radius: var(--radius-md);
            transition: all 0.3s ease;
        }

        .radio-card-content iconify-icon {
            font-size: 1.5rem;
            color: var(--text-muted);
        }

        .radio-card-content span {
            font-size: 0.875rem;
            color: var(--text-muted);
        }

        .radio-card input:checked + .radio-card-content {
            border-color: var(--gold);
            background: rgba(204, 164, 59, 0.1);
        }

        .radio-card input:checked + .radio-card-content iconify-icon,
        .radio-card input:checked + .radio-card-content span {
            color: var(--gold);
        }

        /* Logo Preview */
        .logo-preview-container {
            display: flex;
            gap: 1.5rem;
            align-items: flex-start;
        }

        .logo-preview {
            width: 200px;
            height: 100px;
            background: var(--bg-input);
            border: 2px dashed var(--border-light);
            border-radius: var(--radius-md);
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
        }

        .logo-preview img {
            max-width: 100%;
            max-height: 100%;
            object-fit: contain;
        }

        .logo-placeholder {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 0.5rem;
            color: var(--text-muted);
        }

        /* Logo Preview Dark - fundo escuro para visualizar logo claro */
        .logo-preview-dark {
            background: var(--bg-darker);
            border-color: var(--border-dark);
        }

        .logo-preview-dark .logo-placeholder {
            color: var(--text-muted);
        }

        .logo-preview-dark .logo-placeholder iconify-icon {
            color: var(--text-muted);
        }

        .logo-placeholder span {
            font-size: 0.75rem;
        }

        .logo-upload-actions {
            display: flex;
            flex-direction: column;
        }

        @media (max-width: 768px) {
            .logo-preview-container {
                flex-direction: column;
            }
            .logo-preview {
                width: 100%;
            }
        }

        /* Tabs */
        .tabs-nav {
            display: flex;
            gap: 0.5rem;
            margin-bottom: 1.5rem;
            background: var(--bg-panel);
            padding: 0.5rem;
            border-radius: var(--radius-lg);
            border: 1px solid var(--border-light);
            flex-wrap: wrap;
            overflow-x: auto;
        }

        .tab-btn {
            padding: 0.75rem 1.25rem;
            background: transparent;
            border: none;
            color: var(--text-muted);
            font-size: 0.9rem;
            font-weight: 500;
            cursor: pointer;
            border-radius: var(--radius-md);
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 0.5rem;
            white-space: nowrap;
        }

        .tab-btn:hover {
            background: var(--bg-input);
            color: var(--text-primary);
        }

        .tab-btn.active {
            background: var(--gold);
            color: var(--text-primary);
        }

        .tab-content {
            display: none;
        }

        .tab-content.active {
            display: block;
            animation: fadeIn 0.3s ease;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }

        /* Toggle Switch */
        .toggle-switch {
            position: relative;
            display: inline-flex;
            align-items: center;
            gap: 0.75rem;
            cursor: pointer;
        }

        .toggle-switch input {
            opacity: 0;
            width: 0;
            height: 0;
            position: absolute;
        }

        .toggle-slider {
            width: 48px;
            height: 26px;
            background: var(--bg-input);
            border-radius: 26px;
            position: relative;
            transition: 0.3s;
            border: 1px solid var(--border-light);
        }

        .toggle-slider:before {
            content: '';
            position: absolute;
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background: white;
            top: 2px;
            left: 3px;
            transition: 0.3s;
        }

        .toggle-switch input:checked + .toggle-slider {
            background: var(--gold);
            border-color: var(--gold);
        }

        .toggle-switch input:checked + .toggle-slider:before {
            transform: translateX(22px);
        }

        .toggle-label {
            font-size: 0.875rem;
            color: var(--text-muted);
        }

        /* Toggle Small */
        .toggle-switch-sm {
            position: relative;
            display: inline-block;
            width: 40px;
            height: 22px;
        }

        .toggle-switch-sm input {
            opacity: 0;
            width: 0;
            height: 0;
        }

        .toggle-slider-sm {
            position: absolute;
            cursor: pointer;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: var(--bg-input);
            border: 1px solid var(--border-light);
            transition: 0.3s;
            border-radius: 22px;
        }

        .toggle-slider-sm:before {
            position: absolute;
            content: "";
            height: 16px;
            width: 16px;
            left: 2px;
            bottom: 2px;
            background-color: white;
            transition: 0.3s;
            border-radius: 50%;
        }

        .toggle-switch-sm input:checked + .toggle-slider-sm {
            background-color: var(--gold);
            border-color: var(--gold);
        }

        .toggle-switch-sm input:checked + .toggle-slider-sm:before {
            transform: translateX(18px);
        }

        /* Items List */
        .items-list {
            display: flex;
            flex-direction: column;
            gap: 1rem;
        }

        /* Feature Cards - Design Moderno */
        .feature-cards-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
            gap: 1.5rem;
        }

        .feature-card {
            background: linear-gradient(145deg, var(--bg-input) 0%, rgba(212, 175, 55, 0.03) 100%);
            border: 1px solid var(--border-light);
            border-radius: var(--radius-lg);
            padding: 1.5rem;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }

        .feature-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 3px;
            background: linear-gradient(90deg, var(--gold), var(--gold-light));
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .feature-card:hover {
            border-color: var(--gold);
            transform: translateY(-2px);
            box-shadow: var(--shadow-lg);
        }

        .feature-card:hover::before {
            opacity: 1;
        }

        .feature-card-header {
            display: flex;
            align-items: center;
            gap: 1rem;
            margin-bottom: 1.25rem;
            padding-bottom: 1rem;
            border-bottom: 1px solid var(--border-light);
        }

        .feature-icon-preview {
            width: 56px;
            height: 56px;
            background: linear-gradient(135deg, var(--gold) 0%, var(--gold-light) 100%);
            border-radius: var(--radius-md);
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.75rem;
            color: #1A1A1A;
            flex-shrink: 0;
            box-shadow: var(--shadow-sm);
        }

        .feature-card-number {
            background: var(--bg-dark);
            color: var(--gold);
            padding: 0.25rem 0.75rem;
            border-radius: 20px;
            font-size: 0.75rem;
            font-weight: 600;
            margin-left: auto;
        }

        .feature-card-body {
            display: flex;
            flex-direction: column;
            gap: 1rem;
        }

        .feature-icon-input-group {
            display: flex;
            gap: 0.75rem;
            align-items: flex-end;
        }

        .feature-icon-input-group .form-group {
            flex: 1;
        }

        .feature-card-actions {
            display: flex;
            justify-content: flex-end;
            margin-top: 1rem;
            padding-top: 1rem;
            border-top: 1px solid var(--border-light);
        }

        .menu-item-card {
            background: var(--bg-input);
            border: 1px solid var(--border-light);
            border-radius: var(--radius-lg);
            padding: 1.25rem;
            transition: all 0.3s ease;
        }

        .menu-item-card:hover {
            border-color: var(--gold);
        }

        .item-header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 1rem;
            padding-bottom: 0.75rem;
            border-bottom: 1px solid var(--border-light);
        }

        .item-number {
            background: var(--gold);
            color: var(--bg-dark);
            padding: 0.25rem 0.75rem;
            border-radius: 20px;
            font-size: 0.8rem;
            font-weight: 600;
        }

        /* Alert */
        .alert {
            padding: 1rem 1.25rem;
            border-radius: var(--radius-md);
            margin-bottom: 1.5rem;
            display: flex;
            align-items: center;
            gap: 0.75rem;
        }

        .alert-success {
            background: var(--bg-success);
            border: 1px solid var(--border-success);
            color: var(--success);
        }

        .alert-error {
            background: var(--bg-danger);   
            border: 1px solid var(--border-danger); 
            color: var(--danger);
        }

        .mt-2 { margin-top: 0.5rem; }

        /* Save Bar */
        .save-bar {
            position: sticky;
            bottom: 0;
            left: 0;
            right: 0;
            background: var(--bg-panel);
            border-top: 1px solid var(--border-light);
            padding: 1rem 2rem;
            display: flex;
            justify-content: flex-end;
            gap: 1rem;
            z-index: 100;
            box-shadow: 0 -2px 8px var(--shadow);   
        }

        .btn-lg {
            padding: 0.875rem 2rem;
            font-size: 1rem;
        }

        @media (max-width: 768px) {
            .tabs-nav {
                flex-wrap: nowrap;
                overflow-x: auto;
                -webkit-overflow-scrolling: touch;
            }
            
            .tab-btn {
                padding: 0.625rem 1rem;
                font-size: 0.85rem;
            }
        }

        /* Menu Sortable Styles */
        .menu-sortable-list {
            display: flex;
            flex-direction: column;
            gap: 1rem;
        }

        .menu-item-card {
            background: var(--bg-darker);
            border: 2px solid var(--border);
            border-radius: 12px;
            padding: 1rem;
            display: flex;
            gap: 1rem;
            align-items: flex-start;
            transition: all 0.3s ease;
            position: relative;
        }

        .menu-item-card.dragging {
            opacity: 0.5;
            transform: scale(0.98);
            border-color: var(--gold);
        }

        .menu-item-card.drag-over {
            border-color: var(--gold);
            background: var(--bg-darker);
            transform: var(--scale-up);
        }

        .menu-item-drag-handle {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 32px;
            height: 32px;
            background: var(--bg-card);
            border: 1px solid var(--border);
            border-radius: 8px;
            cursor: grab;
            color: var(--text-secondary);
            transition: all 0.2s;
            flex-shrink: 0;
        }

        .menu-item-drag-handle:hover {
            background: var(--gold);
            color: var(--bg-dark);
            border-color: var(--gold);
        }

        .menu-item-drag-handle:active {
            cursor: grabbing;
        }

        .menu-item-icon-preview {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 48px;
            height: 48px;
            background: linear-gradient(135deg, rgba(204, 164, 59, 0.1), rgba(204, 164, 59, 0.05));
            border: 1px solid rgba(204, 164, 59, 0.2);
            border-radius: 10px;
            color: var(--gold);
            flex-shrink: 0;
        }

        .menu-item-content {
            flex: 1;
            display: flex;
            flex-direction: column;
            gap: 1rem;
        }

        .menu-item-top {
            display: flex;
            align-items: center;
            justify-content: space-between;
        }

        .menu-item-number {
            font-size: 0.75rem;
            font-weight: 600;
            color: var(--gold);
            background: rgba(204, 164, 59, 0.1);
            padding: 0.25rem 0.625rem;
            border-radius: 6px;
        }

        .menu-item-fields {
            display: grid;
            grid-template-columns: 2fr 2fr 2fr;
            gap: 1rem;
        }

        .menu-item-fields .form-group {
            margin-bottom: 0;
        }

        .menu-item-fields .form-label {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 0.5rem;
        }

        .menu-remove-btn {
            align-self: flex-start;
        }

        @media (max-width: 768px) {
            .menu-item-fields {
                grid-template-columns: 1fr;
            }
            
            .menu-item-card {
                flex-direction: column;
            }
            
            .menu-item-drag-handle,
            .menu-item-icon-preview {
                width: 100%;
            }
        }

        /* ===== Menu Compact Styles ===== */
        .menu-sortable {
            display: flex;
            flex-direction: column;
            gap: 0.5rem;
        }

        .menu-compact-item {
            display: flex;
            align-items: center;
            gap: 0.75rem;
            padding: 0.75rem;
            background: var(--bg-input);
            border: 1px solid var(--border);
            border-radius: 8px;
            transition: all 0.2s ease;
        }

        .menu-compact-item:hover {
            border-color: var(--gold);
            box-shadow: 0 2px 8px var(--shadow);
        }

        .menu-compact-item.dragging {
            opacity: 0.5;
            transform: scale(0.98);
            box-shadow: 0 8px 24px var(--shadow);
        }

        .menu-compact-item.drag-over {
            border-color: var(--gold);
            background: var(--bg-darker);
        }

        /* SortableJS Styles */
        .sortable-ghost {
            opacity: 0.4;
            background: var(--bg-input);
            border: 2px dashed var(--gold);
        }

        .sortable-chosen {
            background: var(--bg-highlight);
            box-shadow: 0 4px 12px var(--shadow);
        }

        .sortable-drag {
            opacity: 1 !important;
            background: var(--bg-highlight) !important;
            box-shadow: 0 8px 24px var(--shadow);
        }

        .menu-compact-handle {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 32px;
            height: 32px;
            background: var(--bg-card);
            border: 1px solid var(--border-light);
            border-radius: 6px;
            cursor: grab;
            color: var(--text-secondary);
            transition: all 0.2s;
            flex-shrink: 0;
        }

        .menu-compact-handle:hover {
            background: var(--gold);
            color: var(--bg-dark);  
            border-color: var(--gold);
        }

        .menu-compact-handle:active {
            cursor: grabbing;
        }

        .menu-compact-icon {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 40px;
            height: 40px;
            background: var(--bg-darker);
            border: 1px solid var(--border-light);
            border-radius: 50%;
            color:  var(--gold);
            flex-shrink: 0;
        }

        .menu-compact-input {
            flex: 1;
            min-width: 0;
            padding: 0.5rem 0.75rem;
            background: var(--bg-input);
            border: 1px solid var(--border-light);
            border-radius: 6px;
            font-size: 0.875rem;
            color: var(--text-primary);
            transition: all 0.2s;
        }

        .menu-compact-input:focus {
            outline: none;
            border-color: var(--gold);
            background: var(--bg-light);
            box-shadow: var(--shadow);
        }

        .menu-compact-input::placeholder {
            color: var(--text-muted);
        }

        .menu-icon-field {
            max-width: 160px;
        }

        .menu-compact-toggle {
            position: relative;
            display: inline-flex;
            align-items: center;
            cursor: pointer;
            flex-shrink: 0;
        }

        .menu-compact-toggle input {
            position: absolute;
            opacity: 0;
            width: 0;
            height: 0;
        }

        .toggle-indicator {
            width: 18px;
            height: 18px;
            background: var(--bg-input);
            border: 2px solid var(--border-light);  
            border-radius: 4px;
            transition: all 0.2s;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .toggle-indicator::after {
            content: '✓';
            color: var(--bg-dark);
            font-size: 11px;
            font-weight: bold;
            opacity: 0;
            transition: opacity 0.2s;
        }

        .menu-compact-toggle input:checked + .toggle-indicator {
            background: var(--gold);
            border-color: var(--gold);
        }

        .menu-compact-toggle input:checked + .toggle-indicator::after {
            opacity: 1;
        }

        .menu-compact-delete {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 32px;
            height: 32px;
            background: var(--bg-error);
            border: 1px solid var(--border-error);
            border-radius: 6px;
            color: var(--text-error);
            cursor: pointer;
            transition: all 0.2s;
            flex-shrink: 0;
        }

        .menu-compact-delete:hover {
            background: var(--error);
            color: var(--text-error);
            border-color: var(--error);
        }

        @media (max-width: 768px) {
            .menu-compact-item {
                flex-wrap: wrap;
            }
            
            .menu-compact-input {
                min-width: 100%;
            }
            
            .menu-icon-field {
                max-width: none;
            }
        }

        </style>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/Sortable/1.14.0/Sortable.min.js"></script>
    <script>
        // Tab switching
        document.querySelectorAll('.tab-btn').forEach(btn => {
            btn.addEventListener('click', function() {
                const tabId = this.dataset.tab;
                
                document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
                document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
                
                this.classList.add('active');
                document.getElementById('tab-' + tabId).classList.add('active');
            });
        });

        // Add menu item

        // Remove item
        function removeItem(btn) {
            const card = btn.closest('.menu-item-card');
            if (!card) return;
            
            if (!confirm('Deseja remover este item?')) return;
            
            card.style.opacity = '0';
            card.style.transform = 'translateX(-20px)';
            setTimeout(() => card.remove(), 300);
        }

        // Preview logo
        function previewLogo(input) {
            if (input.files && input.files[0]) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    const preview = document.getElementById('logoPreview');
                    preview.innerHTML = '<img src="' + e.target.result + '" alt="Preview">';
                };
                reader.readAsDataURL(input.files[0]);
            }
        }

        // Preview logo dark
        function previewLogoDark(input) {
            if (input.files && input.files[0]) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    const preview = document.getElementById('logoDarkPreview');
                    preview.innerHTML = '<img src="' + e.target.result + '" alt="Preview Dark">';
                };
                reader.readAsDataURL(input.files[0]);
            }
        }

        // Toggle logo upload section based on logo type
        function toggleLogoSection() {
            const logoType = document.querySelector('input[name="header_logo_type"]:checked').value;
            const uploadSection = document.getElementById('logoUploadSection');
            const uploadDarkSection = document.getElementById('logoDarkUploadSection');
            if (logoType === 'image') {
                uploadSection.style.display = 'block';
                if (uploadDarkSection) uploadDarkSection.style.display = 'block';
            } else {
                uploadSection.style.display = 'none';
                if (uploadDarkSection) uploadDarkSection.style.display = 'none';
            }
        }

        // Initialize and add listeners
        document.querySelectorAll('input[name="header_logo_type"]').forEach(radio => {
            radio.addEventListener('change', toggleLogoSection);
        });
        toggleLogoSection();

        // Add feature
        let featureCounter = <?= count($featuresItems) ?>;
        
        function addFeature() {
            const container = document.getElementById('features-list');
            featureCounter++;
            const uniqueId = 'new-' + Date.now();

            const html = `
                <div class="feature-card" style="animation: fadeIn 0.3s ease">
                    <div class="feature-card-header">
                        <div class="feature-icon-preview" id="icon-preview-${uniqueId}">
                            <iconify-icon icon="solar:star-bold"></iconify-icon>
                        </div>
                        <div style="flex: 1;">
                            <div style="font-weight: 600; color: var(--text-primary); font-size: 1rem;">Novo Recurso</div>
                            <div style="font-size: 0.8rem; color: var(--text-muted); margin-top: 0.25rem;">Recurso #${featureCounter}</div>
                        </div>
                        <span class="feature-card-number">#${featureCounter}</span>
                    </div>
                    <div class="feature-card-body">
                        <div class="feature-icon-input-group">
                            <div class="form-group" style="margin-bottom: 0;">
                                <label class="form-label">
                                    <iconify-icon icon="solar:pallete-2-bold" style="margin-right: 0.25rem;"></iconify-icon>
                                    Código do Ícone
                                </label>
                                <input type="text" name="feature_icon[]" class="form-input" value="solar:star-bold" oninput="updateFeatureIconPreview(this)" placeholder="solar:star-bold">
                            </div>
                        </div>
                        <div class="form-group" style="margin-bottom: 0;">
                            <label class="form-label">
                                <iconify-icon icon="solar:text-bold" style="margin-right: 0.25rem;"></iconify-icon>
                                Título
                            </label>
                            <input type="text" name="feature_title[]" class="form-input" placeholder="Título do recurso">
                        </div>
                        <div class="form-group" style="margin-bottom: 0;">
                            <label class="form-label">
                                <iconify-icon icon="solar:document-text-bold" style="margin-right: 0.25rem;"></iconify-icon>
                                Descrição
                            </label>
                            <textarea name="feature_description[]" class="form-input" style="min-height: 70px; resize: vertical;" placeholder="Descrição do recurso"></textarea>
                        </div>
                    </div>
                    <div class="feature-card-actions">
                        <button type="button" onclick="removeFeatureCard(this)" class="btn btn-danger btn-sm">
                            <iconify-icon icon="solar:trash-bin-trash-bold"></iconify-icon>
                            Remover
                        </button>
                    </div>
                </div>
            `;

            container.insertAdjacentHTML('beforeend', html);
        }

        function updateFeatureIconPreview(input) {
            const card = input.closest('.feature-card');
            const iconPreview = card.querySelector('.feature-icon-preview iconify-icon');
            const iconValue = input.value.trim() || 'solar:star-bold';
            iconPreview.setAttribute('icon', iconValue);
        }

        function removeFeatureCard(btn) {
            const card = btn.closest('.feature-card');
            card.style.animation = 'fadeOut 0.3s ease';
            setTimeout(() => card.remove(), 300);
        }
        // ========================================
        // HERO IMAGE UPLOAD
        // ========================================
        
        function previewHeroImage(input) {
            const preview = document.getElementById('hero-image-preview');
            const img = preview.querySelector('img');
            
            if (input.files && input.files[0]) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    img.src = e.target.result;
                    preview.style.display = 'block';
                };
                reader.readAsDataURL(input.files[0]);
            }
        }
        
        function clearHeroImage() {
            if (!confirm('Deseja remover a imagem do Hero?')) return;
            
            const preview = document.getElementById('hero-image-preview');
            const hiddenInput = document.querySelector('input[name="hero_image"]');
            const fileInput = document.getElementById('hero_image_file');
            
            preview.style.display = 'none';
            preview.querySelector('img').src = '';
            hiddenInput.value = '';
            fileInput.value = '';
        }

        // ========================================
        // EDITOR DE MENU - SortableJS
        // ========================================
        
        let menuSortable = null;
        
        // Inicializa o SortableJS no container de menu
        function initMenuEditor() {
            const container = document.getElementById('menu-editor-list');
            if (!container) {
                console.log('Container menu-editor-list não encontrado');
                return;
            }
            
            // Inicializa o SortableJS
            menuSortable = new Sortable(container, {
                handle: '.menu-compact-handle',
                animation: 150,
                ghostClass: 'sortable-ghost',
                chosenClass: 'sortable-chosen',
                dragClass: 'sortable-drag',
                onEnd: function(evt) {
                    updateMenuIndexes();
                }
            });
            
            // Event listener para o botão de adicionar
            const addBtn = document.getElementById('btn-add-menu-item');
            if (addBtn) {
                addBtn.addEventListener('click', function(e) {
                    e.preventDefault();
                    addMenuItem('Novo Item', '#');
                });
            }
            
            // Event delegation para botões de remover
            container.addEventListener('click', function(e) {
                if (e.target.closest('.btn-remove-menu-item')) {
                    e.preventDefault();
                    removeMenuItem(e.target.closest('.btn-remove-menu-item'));
                }
            });
            
            // Event delegation para atualizar preview do ícone
            container.addEventListener('input', function(e) {
                if (e.target.classList.contains('menu-icon-field')) {
                    updateIconPreview(e.target);
                }
            });
        }
        
        // Adiciona um novo item ao menu
        function addMenuItem(texto, link) {
            const container = document.getElementById('menu-editor-list');
            if (!container) return;
            
            const index = container.querySelectorAll('.menu-compact-item').length;
            
            const itemHtml = `
                <div class="menu-compact-item" data-index="${index}" style="animation: fadeIn 0.3s ease">
                    <div class="menu-compact-handle" title="Arraste para reordenar">
                        <iconify-icon icon="solar:hamburger-menu-linear" width="18"></iconify-icon>
                    </div>
                    <div class="menu-compact-icon">
                        <iconify-icon icon="solar:link-bold" width="20"></iconify-icon>
                    </div>
                    <input type="text" name="menu_label[]" class="menu-compact-input" value="${texto || ''}" placeholder="Nome do menu">
                    <input type="text" name="menu_url[]" class="menu-compact-input" value="${link || '#'}" placeholder="Link">
                    <input type="text" name="menu_icon[]" class="menu-compact-input menu-icon-field" value="solar:link-bold" placeholder="Ícone">
                    <label class="menu-compact-toggle" title="Ativo/Inativo">
                        <input type="checkbox" name="menu_active[${index}]" checked>
                        <span class="toggle-indicator"></span>
                    </label>
                    <button type="button" class="menu-compact-delete btn-remove-menu-item" title="Remover">
                        <iconify-icon icon="solar:trash-bin-trash-bold" width="18"></iconify-icon>
                    </button>
                </div>
            `;
            
            container.insertAdjacentHTML('beforeend', itemHtml);
            updateMenuIndexes();
            
            // Scroll para o novo item
            const newItem = container.lastElementChild;
            if (newItem) {
                newItem.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
            }
        }
        
        // Remove um item do menu
        function removeMenuItem(btn) {
            const item = btn.closest('.menu-compact-item');
            if (!item) return;
            
            if (!confirm('Deseja remover este item do menu?')) return;
            
            item.style.transition = 'all 0.3s ease';
            item.style.opacity = '0';
            item.style.transform = 'translateX(-20px)';
            
            setTimeout(function() {
                item.remove();
                updateMenuIndexes();
            }, 300);
        }
        
        // Atualiza os índices dos itens após reordenação
        function updateMenuIndexes() {
            const container = document.getElementById('menu-editor-list');
            if (!container) return;
            
            const items = container.querySelectorAll('.menu-compact-item');
            items.forEach(function(item, index) {
                item.setAttribute('data-index', index);
                // Atualiza o name do checkbox de ativo
                const checkbox = item.querySelector('input[type="checkbox"]');
                if (checkbox) {
                    checkbox.name = 'menu_active[' + index + ']';
                }
            });
        }
        
        // Atualiza o preview do ícone quando o valor muda
        function updateIconPreview(input) {
            const item = input.closest('.menu-compact-item');
            if (!item) return;
            
            const iconBox = item.querySelector('.menu-compact-icon iconify-icon');
            if (iconBox && input.value.trim()) {
                iconBox.setAttribute('icon', input.value.trim());
            }
        }
        
        // Inicializar quando a página carregar
        document.addEventListener('DOMContentLoaded', function() {
            initMenuEditor();
        });
        
        </script>
<?php include __DIR__ . '/../includes/footer.php'; ?>
