<?php
/**
 * Minecraft Modern Theme - Customizer Settings
 * FIXES:
 *  - Google Font via wp_enqueue_style() statt <link> direkt in wp_head
 *  - slider_loop Setting ergänzt (war in slider-init.js referenziert, aber nie definiert)
 *  - Scroll-to-Top Toggle ergänzt
 *  - footer_copyright Default mit aktuellem Jahr zur Laufzeit
 *  - Import_Export_Control Klasse aus dem Callback extrahiert
 */

// =========================================================================
// Import/Export Control – außerhalb des Callbacks definiert
// =========================================================================
if ( class_exists('WP_Customize_Control') && ! class_exists('MM_Import_Export_Control') ) :
class MM_Import_Export_Control extends WP_Customize_Control {
    public $type = 'mm_import_export';

    public function render_content() {
        $export_url = admin_url('admin-post.php?action=export_theme_settings');
        $nonce      = wp_create_nonce('theme-import-nonce');
        ?>
        <div class="mm-import-export-wrapper">
            <p class="description" style="margin-bottom:16px;">
                <strong><?php _e('Hinweis:', 'minecraft-modern-theme'); ?></strong>
                <?php _e('Hier kannst du alle Theme-Einstellungen und Inhalte sichern und wiederherstellen.', 'minecraft-modern-theme'); ?>
            </p>

            <div style="background:#e7f3ff;border:1px solid #b3d9ff;border-radius:4px;padding:12px;margin-bottom:16px;font-size:12px;line-height:1.5;">
                <strong>📦 Was wird gesichert:</strong><br>
                ✓ Customizer-Einstellungen (Farben, Social Links, Menü-Design, etc.)<br>
                ✓ Livestream API Keys (YouTube, Twitch)<br>
                ✓ Homepage-Seite (Titel, Inhalt, Highlight-Bild)<br>
                ✓ Navigation Menüs inkl. aller Items & Struktur<br>
                ✓ Widget-Konfigurationen<br>
                ✓ Team-Mitglieder (mit UUID, Avatar, Banner)<br>
                ✓ FAQ-Einträge & Kategorien<br>
                ✓ Custom CSS<br>
                ✓ Announcement-Bar Einstellungen
            </div>

            <a href="<?php echo esc_url($export_url); ?>" class="button button-primary" style="display:inline-flex;align-items:center;gap:6px;margin-bottom:20px;">
                <span class="dashicons dashicons-download"></span>
                <?php _e('Einstellungen exportieren', 'minecraft-modern-theme'); ?>
            </a>

            <hr style="margin:16px 0;">

            <label style="display:block;margin-bottom:8px;font-weight:600;">
                <?php _e('Backup wiederherstellen:', 'minecraft-modern-theme'); ?>
            </label>
            <input type="file" id="mm-import-file" accept=".json" style="width:100%;margin-bottom:10px;">
            <button type="button" class="button" id="mm-import-btn" disabled style="display:inline-flex;align-items:center;gap:6px;">
                <span class="dashicons dashicons-upload"></span>
                <?php _e('Einstellungen importieren', 'minecraft-modern-theme'); ?>
            </button>

            <p class="description" style="margin-top:12px;padding:10px;background:#fff3cd;border-left:4px solid #ffc107;color:#856404;">
                ⚠️ <?php _e('Beim Import werden alle aktuellen Einstellungen überschrieben!', 'minecraft-modern-theme'); ?>
            </p>
        </div>

        <script>
        (function($){
            var ajaxUrl = '<?php echo esc_js(admin_url('admin-ajax.php')); ?>';
            var nonce   = '<?php echo esc_js($nonce); ?>';

            $('#mm-import-file').on('change', function(){
                $('#mm-import-btn').prop('disabled', $(this).val() === '');
            });

            $('#mm-import-btn').on('click', function(){
                var file = $('#mm-import-file')[0].files[0];
                if (!file) return;
                if (!confirm('<?php echo esc_js(__('Alle aktuellen Einstellungen werden überschrieben. Fortfahren?', 'minecraft-modern-theme')); ?>')) return;

                var $btn = $(this).prop('disabled', true).text('<?php echo esc_js(__('Importiere…', 'minecraft-modern-theme')); ?>');
                var fd   = new FormData();
                fd.append('import_file', file);
                fd.append('action', 'import_theme_settings');
                fd.append('nonce', nonce);

                $.ajax({ url: ajaxUrl, type: 'POST', data: fd, processData: false, contentType: false,
                    success: function(r){
                        if (r.success) { alert('✅ ' + r.data); location.reload(); }
                        else { alert('❌ ' + r.data); $btn.prop('disabled', false).text('<?php echo esc_js(__('Einstellungen importieren', 'minecraft-modern-theme')); ?>'); }
                    },
                    error: function(){ alert('<?php echo esc_js(__('Technischer Fehler.', 'minecraft-modern-theme')); ?>'); $btn.prop('disabled', false); }
                });
            });
        })(jQuery);
        </script>
        <?php
    }
}
endif;


// =========================================================================
// Customizer Register
// =========================================================================
function minecraft_modern_customize_register( $wp_customize ) {
    // =========================================================================
    // 9. Virtueller Assistent
    // =========================================================================
    // BUG-FIX: add_section() muss VOR add_setting()/add_control() stehen.
    // Vorher war add_setting/add_control für 'assistant_avatar_view' aufgerufen
    // bevor die Section 'assistant_settings' überhaupt registriert war.
    $wp_customize->add_section( 'assistant_settings', array(
        'title'       => __( 'Virtueller Assistent', 'minecraft-modern-theme' ),
        'priority'    => 80,
        'description' => __( 'Steuert den virtuellen Assistenten im Frontend. Avatar basiert auf Minecraft-UUID.', 'minecraft-modern-theme' ),
    ) );

    $wp_customize->add_setting( 'assistant_enabled', array(
        'default'           => false,
        'sanitize_callback' => 'wp_validate_boolean',
    ) );
    $wp_customize->add_control( 'assistant_enabled', array(
        'label'   => __( 'Virtuellen Assistenten aktivieren', 'minecraft-modern-theme' ),
        'section' => 'assistant_settings',
        'type'    => 'checkbox',
    ) );

    $wp_customize->add_setting( 'assistant_minecraft_uuid', array(
        'default'           => '',
        'sanitize_callback' => 'sanitize_text_field',
    ) );
    $wp_customize->add_control( 'assistant_minecraft_uuid', array(
        'label'       => __( 'Minecraft UUID für Avatar', 'minecraft-modern-theme' ),
        'section'     => 'assistant_settings',
        'type'        => 'text',
        'description' => __( 'Gib die Minecraft-UUID für den Avatar des Assistenten ein.', 'minecraft-modern-theme' ),
    ) );

    $wp_customize->add_setting( 'assistant_avatar_view', array(
        'default'           => 'head',
        'sanitize_callback' => function ( $v ) {
            return in_array( $v, array( 'head', 'body', '3dhead' ), true ) ? $v : 'head';
        },
    ) );
    $wp_customize->add_control( 'assistant_avatar_view', array(
        'label'       => __( 'Avatar-Ansicht', 'minecraft-modern-theme' ),
        'section'     => 'assistant_settings',
        'type'        => 'select',
        'choices'     => array(
            'head'   => __( 'Kopf', 'minecraft-modern-theme' ),
            'body'   => __( 'Ganzkörper', 'minecraft-modern-theme' ),
            '3dhead' => __( '3D Kopf', 'minecraft-modern-theme' ),
        ),
        'description' => __( 'Wähle, wie der Minecraft-Avatar im Assistenten angezeigt wird.', 'minecraft-modern-theme' ),
    ) );

    // BUG-FIX: assistant_position war in assistant-widget.php in einem eigenen
    // customize_register-Hook registriert – also BEVOR diese Section existierte.
    // Jetzt korrekt hier innerhalb von minecraft_modern_customize_register().
    $wp_customize->add_setting( 'assistant_position', array(
        'default'           => 'bottom_right',
        'transport'         => 'refresh',
        'sanitize_callback' => function ( $v ) {
            $allowed = array( 'bottom_right', 'bottom_left', 'top_right', 'top_left' );
            return in_array( $v, $allowed, true ) ? $v : 'bottom_right';
        },
    ) );
    $wp_customize->add_control( 'assistant_position', array(
        'label'   => __( 'Position des Assistenten', 'minecraft-modern-theme' ),
        'section' => 'assistant_settings',
        'type'    => 'select',
        'choices' => array(
            'bottom_right' => __( 'Unten rechts', 'minecraft-modern-theme' ),
            'bottom_left'  => __( 'Unten links', 'minecraft-modern-theme' ),
            'top_right'    => __( 'Oben rechts', 'minecraft-modern-theme' ),
            'top_left'     => __( 'Oben links', 'minecraft-modern-theme' ),
        ),
    ) );

    // =========================================================================
    // 1. HEADER SLIDER
    // =========================================================================
    $wp_customize->add_section( 'header_slider', array(
        'title'       => __('Header Slider', 'minecraft-modern-theme'),
        'priority'    => 20,
        'description' => __('Konfiguriere den großen Slider auf der Startseite.', 'minecraft-modern-theme'),
    ) );

    $wp_customize->add_setting( 'slider_enabled', array( 'default' => false, 'transport' => 'refresh', 'sanitize_callback' => 'wp_validate_boolean' ) );
    $wp_customize->add_control( 'slider_enabled', array( 'label' => __('Header Slider aktivieren', 'minecraft-modern-theme'), 'section' => 'header_slider', 'type' => 'checkbox' ) );

    // FIX: slider_loop war in slider-init.js als sliderSettings.loop referenziert, aber nie definiert
    $wp_customize->add_setting( 'slider_loop', array( 'default' => true, 'transport' => 'refresh', 'sanitize_callback' => 'wp_validate_boolean' ) );
    $wp_customize->add_control( 'slider_loop', array(
        'label'       => __('Slider Endlos-Loop aktivieren', 'minecraft-modern-theme'),
        'description' => __('Der Slider springt nach dem letzten Slide wieder zum ersten zurück.', 'minecraft-modern-theme'),
        'section'     => 'header_slider',
        'type'        => 'checkbox',
    ) );

    for ( $i = 1; $i <= 5; $i++ ) {
        $wp_customize->add_setting( 'slider_image_' . $i, array( 'sanitize_callback' => 'esc_url_raw' ) );
        $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'slider_image_' . $i, array(
            'label' => sprintf( __('Banner %d - Bild', 'minecraft-modern-theme'), $i ), 'section' => 'header_slider',
        ) ) );
        $wp_customize->add_setting( 'slider_title_' . $i, array( 'sanitize_callback' => 'sanitize_text_field' ) );
        $wp_customize->add_control( 'slider_title_' . $i, array( 'label' => sprintf( __('Banner %d - Titel', 'minecraft-modern-theme'), $i ), 'section' => 'header_slider', 'type' => 'text' ) );
        $wp_customize->add_setting( 'slider_subtitle_' . $i, array( 'sanitize_callback' => 'sanitize_text_field' ) );
        $wp_customize->add_control( 'slider_subtitle_' . $i, array( 'label' => sprintf( __('Banner %d - Untertitel', 'minecraft-modern-theme'), $i ), 'section' => 'header_slider', 'type' => 'text' ) );
    }

    $wp_customize->add_setting( 'slider_font_family', array( 'default' => 'Raleway', 'sanitize_callback' => 'sanitize_text_field' ) );
    $wp_customize->add_control( 'slider_font_family', array(
        'label' => __('Schriftart', 'minecraft-modern-theme'), 'section' => 'header_slider', 'type' => 'select',
        'choices' => array( 'Raleway' => 'Raleway', 'Poppins' => 'Poppins', 'Montserrat' => 'Montserrat', 'Oswald' => 'Oswald', 'Roboto' => 'Roboto', 'Lato' => 'Lato' ),
    ) );

    $wp_customize->add_setting( 'slider_font_size', array( 'default' => 'mittel', 'sanitize_callback' => 'sanitize_text_field' ) );
    $wp_customize->add_control( 'slider_font_size', array(
        'label' => __('Schriftgröße', 'minecraft-modern-theme'), 'section' => 'header_slider', 'type' => 'select',
        'choices' => array( 'klein' => __('Klein', 'minecraft-modern-theme'), 'mittel' => __('Mittel', 'minecraft-modern-theme'), 'gross' => __('Groß', 'minecraft-modern-theme'), 'extra-gross' => __('Extra Groß', 'minecraft-modern-theme') ),
    ) );

    $wp_customize->add_setting( 'slider_font_color', array( 'default' => '#ffffff', 'sanitize_callback' => 'sanitize_hex_color' ) );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'slider_font_color', array(
        'label' => __('Schriftfarbe', 'minecraft-modern-theme'), 'section' => 'header_slider',
    ) ) );

    $wp_customize->add_setting( 'header_height', array( 'default' => 'mittel', 'sanitize_callback' => 'sanitize_text_field' ) );
    $wp_customize->add_control( 'header_height', array(
        'label' => __('Header-Höhe', 'minecraft-modern-theme'), 'section' => 'header_slider', 'type' => 'select',
        'choices' => array( 'klein' => __('Klein', 'minecraft-modern-theme'), 'mittel' => __('Mittel', 'minecraft-modern-theme'), 'gross' => __('Groß', 'minecraft-modern-theme') ),
    ) );

    $wp_customize->add_setting( 'slider_hide_arrows', array( 'default' => false, 'sanitize_callback' => 'wp_validate_boolean' ) );
    $wp_customize->add_control( 'slider_hide_arrows', array( 'label' => __('Pfeile ausblenden', 'minecraft-modern-theme'), 'section' => 'header_slider', 'type' => 'checkbox' ) );

    $wp_customize->add_setting( 'slider_hide_pagination', array( 'default' => false, 'sanitize_callback' => 'wp_validate_boolean' ) );
    $wp_customize->add_control( 'slider_hide_pagination', array( 'label' => __('Paginierung (Punkte) ausblenden', 'minecraft-modern-theme'), 'section' => 'header_slider', 'type' => 'checkbox' ) );


    // =========================================================================
    // 2. HERO SECTION (Fallback)
    // =========================================================================
    $wp_customize->add_section( 'hero_section', array(
        'title'    => __('Startseiten-Hero (wenn Slider deaktiviert)', 'minecraft-modern-theme'),
        'priority' => 21,
    ) );

    $wp_customize->add_setting( 'hero_title',    array( 'default' => __('Willkommen auf unserem Server', 'minecraft-modern-theme'), 'sanitize_callback' => 'sanitize_text_field' ) );
    $wp_customize->add_control( 'hero_title',    array( 'label' => __('Haupttitel', 'minecraft-modern-theme'),  'section' => 'hero_section', 'type' => 'text' ) );
    $wp_customize->add_setting( 'hero_subtitle', array( 'default' => __('Trete einer Community voller Abenteuer bei.', 'minecraft-modern-theme'), 'sanitize_callback' => 'sanitize_text_field' ) );
    $wp_customize->add_control( 'hero_subtitle', array( 'label' => __('Untertitel', 'minecraft-modern-theme'), 'section' => 'hero_section', 'type' => 'text' ) );
    $wp_customize->add_setting( 'hero_bg_image', array( 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'hero_bg_image', array( 'label' => __('Hintergrundbild', 'minecraft-modern-theme'), 'section' => 'hero_section' ) ) );
    $wp_customize->add_setting( 'hero_button_1_text', array( 'default' => __('Zum Forum', 'minecraft-modern-theme'), 'sanitize_callback' => 'sanitize_text_field' ) );
    $wp_customize->add_control( 'hero_button_1_text', array( 'label' => __('Button 1 Text', 'minecraft-modern-theme'), 'section' => 'hero_section', 'type' => 'text' ) );
    $wp_customize->add_setting( 'hero_button_1_url',  array( 'default' => '#', 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_control( 'hero_button_1_url',  array( 'label' => __('Button 1 URL', 'minecraft-modern-theme'),  'section' => 'hero_section', 'type' => 'url' ) );
    $wp_customize->add_setting( 'hero_button_2_text', array( 'default' => __('Zum Teamspeak', 'minecraft-modern-theme'), 'sanitize_callback' => 'sanitize_text_field' ) );
    $wp_customize->add_control( 'hero_button_2_text', array( 'label' => __('Button 2 Text', 'minecraft-modern-theme'), 'section' => 'hero_section', 'type' => 'text' ) );
    $wp_customize->add_setting( 'hero_button_2_url',  array( 'default' => '#', 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_control( 'hero_button_2_url',  array( 'label' => __('Button 2 URL', 'minecraft-modern-theme'),  'section' => 'hero_section', 'type' => 'url' ) );
    $wp_customize->add_setting( 'show_home_title', array( 'default' => false, 'sanitize_callback' => 'wp_validate_boolean' ) );
    $wp_customize->add_control( 'show_home_title', array( 'label' => __('Seitentitel "Home" anzeigen', 'minecraft-modern-theme'), 'section' => 'hero_section', 'type' => 'checkbox' ) );


    // =========================================================================
    // 3. FARBEN & DARSTELLUNG
    // =========================================================================
    $wp_customize->add_section( 'theme_appearance_settings', array(
        'title' => __('Farben & Darstellung', 'minecraft-modern-theme'), 'priority' => 30,
    ) );

    $wp_customize->add_setting( 'theme_color_preset', array( 'default' => 'classic', 'sanitize_callback' => 'sanitize_key', 'transport' => 'refresh' ) );
    $wp_customize->add_control( 'theme_color_preset', array(
        'label'    => __('Theme Preset (Farbschema)', 'minecraft-modern-theme'),
        'section'  => 'theme_appearance_settings',
        'type'     => 'select',
        'priority' => 1,
        'choices'  => array(
            'classic' => __('Classic Minecraft (Diamant-Blau)', 'minecraft-modern-theme'),
            'nether'  => __('Nether (Lava-Rot)', 'minecraft-modern-theme'),
            'end'     => __('The End (Ender-Purpur)', 'minecraft-modern-theme'),
        ),
    ) );

    $wp_customize->add_setting( 'primary_accent_color', array( 'default' => '#00d4ff', 'sanitize_callback' => 'sanitize_hex_color', 'transport' => 'refresh' ) );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'primary_accent_color', array( 'label' => __('Akzentfarbe', 'minecraft-modern-theme'), 'section' => 'theme_appearance_settings' ) ) );

    $wp_customize->add_setting( 'default_theme_mode', array( 'default' => 'dark', 'sanitize_callback' => 'sanitize_key' ) );
    $wp_customize->add_control( 'default_theme_mode', array(
        'label'   => __('Standard-Theme-Modus', 'minecraft-modern-theme'),
        'section' => 'theme_appearance_settings',
        'type'    => 'radio',
        'choices' => array( 'dark' => __('Dark Mode', 'minecraft-modern-theme'), 'light' => __('Light Mode', 'minecraft-modern-theme') ),
    ) );

    // FIX: Scroll-to-Top war nie im Customizer steuerbar
    $wp_customize->add_setting( 'show_scroll_to_top', array( 'default' => true, 'sanitize_callback' => 'wp_validate_boolean' ) );
    $wp_customize->add_control( 'show_scroll_to_top', array(
        'label'       => __('Scroll-to-Top Button anzeigen', 'minecraft-modern-theme'),
        'description' => __('Zeigt einen Button unten rechts zum Hochscrollen an.', 'minecraft-modern-theme'),
        'section'     => 'theme_appearance_settings',
        'type'        => 'checkbox',
    ) );


    // =========================================================================
    // 4. SIDEBAR
    // =========================================================================
    $wp_customize->add_section( 'sidebar_settings', array( 'title' => __('Sidebar Einstellungen', 'minecraft-modern-theme'), 'priority' => 35 ) );
    $wp_customize->add_setting( 'homepage_sidebar_enabled',  array( 'default' => false,   'sanitize_callback' => 'wp_validate_boolean' ) );
    $wp_customize->add_control( 'homepage_sidebar_enabled',  array( 'label' => __('Sidebar auf Startseite aktivieren', 'minecraft-modern-theme'), 'section' => 'sidebar_settings', 'type' => 'checkbox' ) );
    $wp_customize->add_setting( 'homepage_sidebar_position', array( 'default' => 'right',  'sanitize_callback' => 'sanitize_key' ) );
    $wp_customize->add_control( 'homepage_sidebar_position', array(
        'label' => __('Sidebar Position', 'minecraft-modern-theme'), 'section' => 'sidebar_settings', 'type' => 'select',
        'choices' => array( 'left' => __('Links', 'minecraft-modern-theme'), 'right' => __('Rechts', 'minecraft-modern-theme') ),
    ) );


    // =========================================================================
    // 5. SOCIAL MEDIA
    // =========================================================================
    $wp_customize->add_section( 'social_links', array( 'title' => __('Social Media Links', 'minecraft-modern-theme'), 'priority' => 40 ) );
    $social_platforms = array(
        'bluesky' => 'BlueSky', 'discord' => 'Discord', 'youtube' => 'YouTube', 'twitter' => 'Twitter (X)',
        'facebook' => 'Facebook', 'instagram' => 'Instagram', 'tiktok' => 'TikTok',
        'twitch' => 'Twitch', 'steam' => 'Steam', 'github' => 'GitHub',
        'linkedin' => 'LinkedIn', 'pinterest' => 'Pinterest', 'reddit' => 'Reddit',
        'mastodon' => 'Mastodon', 'threads' => 'Threads', 'kickstarter' => 'Kickstarter',
        'teamspeak' => 'Teamspeak', 'spotify' => 'Spotify', 'stoat' => 'Stoat',
    );
    foreach ( $social_platforms as $key => $label ) {
        $wp_customize->add_setting( 'social_' . $key, array( 'sanitize_callback' => 'esc_url_raw' ) );
        $wp_customize->add_control( 'social_' . $key, array( 'label' => $label . ' URL', 'section' => 'social_links', 'type' => 'url' ) );
    }

    // =========================================================================
    // 6. FOOTER
    // =========================================================================
    $wp_customize->add_section( 'footer_settings', array( 'title' => __('Footer-Einstellungen', 'minecraft-modern-theme'), 'priority' => 50 ) );

    // FIX: Default mit aktuellem Jahr zur Laufzeit auswerten, nicht bei class-load
    $wp_customize->add_setting( 'footer_copyright', array(
        'default'           => '',  // Leer = dynamisch berechnet in footer.php
        'sanitize_callback' => 'wp_kses_post',
    ) );
    $wp_customize->add_control( 'footer_copyright', array(
        'label'       => __('Copyright-Text', 'minecraft-modern-theme'),
        'description' => __('Leer lassen für automatischen Text mit aktuellem Jahr.', 'minecraft-modern-theme'),
        'section'     => 'footer_settings',
        'type'        => 'textarea',
    ) );

    $wp_customize->add_setting( 'footer_impressum_url',   array( 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_control( 'footer_impressum_url',   array( 'label' => __('URL für Impressum', 'minecraft-modern-theme'),   'section' => 'footer_settings', 'type' => 'url' ) );
    $wp_customize->add_setting( 'footer_datenschutz_url', array( 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_control( 'footer_datenschutz_url', array( 'label' => __('URL für Datenschutz', 'minecraft-modern-theme'), 'section' => 'footer_settings', 'type' => 'url' ) );
    $wp_customize->add_setting( 'show_footer_credit', array( 'default' => true, 'sanitize_callback' => 'wp_validate_boolean' ) );
    $wp_customize->add_control( 'show_footer_credit', array( 'label' => __('Footer-Credit anzeigen', 'minecraft-modern-theme'), 'section' => 'footer_settings', 'type' => 'checkbox' ) );


    // =========================================================================
    // 7. FAQ & TEAM
    // =========================================================================
    $wp_customize->add_section( 'faq_settings',  array( 'title' => __('FAQ Einstellungen', 'minecraft-modern-theme'),  'priority' => 60 ) );
    $wp_customize->add_setting( 'faq_enabled',   array( 'default' => true, 'sanitize_callback' => 'wp_validate_boolean' ) );
    $wp_customize->add_control( 'faq_enabled',   array( 'label' => __('FAQ System aktivieren', 'minecraft-modern-theme'),  'section' => 'faq_settings',  'type' => 'checkbox' ) );

    $wp_customize->add_section( 'team_settings', array( 'title' => __('Team Einstellungen', 'minecraft-modern-theme'), 'priority' => 65 ) );
    $wp_customize->add_setting( 'team_enabled',  array( 'default' => true, 'sanitize_callback' => 'wp_validate_boolean' ) );
    $wp_customize->add_control( 'team_enabled',  array( 'label' => __('Team Showcase aktivieren', 'minecraft-modern-theme'), 'section' => 'team_settings', 'type' => 'checkbox' ) );


    // =========================================================================
    // 8. LOGIN
    // =========================================================================
    $wp_customize->add_section( 'login_settings', array( 'title' => __('Login-Einstellungen', 'minecraft-modern-theme'), 'priority' => 70 ) );
    $wp_customize->add_setting( 'login_background_image', array( 'sanitize_callback' => 'esc_url_raw', 'transport' => 'refresh' ) );
    $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'login_background_image', array( 'label' => __('Login-Hintergrundbild', 'minecraft-modern-theme'), 'section' => 'login_settings' ) ) );
    $wp_customize->add_setting( 'login_logo', array( 'sanitize_callback' => 'esc_url_raw', 'transport' => 'refresh' ) );
    $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'login_logo', array( 'label' => __('Login-Logo', 'minecraft-modern-theme'), 'section' => 'login_settings' ) ) );

    for ( $i = 1; $i <= 5; $i++ ) {
        $wp_customize->add_setting( 'login_avatar_uuid_' . $i, array( 'sanitize_callback' => 'sanitize_text_field', 'transport' => 'refresh' ) );
        $wp_customize->add_control( 'login_avatar_uuid_' . $i, array(
            'label'   => sprintf( __('Avatar %d UUID', 'minecraft-modern-theme'), $i ),
            'section' => 'login_settings',
            'type'    => 'text',
        ) );
    }

    $wp_customize->add_setting( 'login_avatar_slider_speed', array( 'default' => 4, 'sanitize_callback' => 'absint', 'transport' => 'refresh' ) );
    $wp_customize->add_control( 'login_avatar_slider_speed', array(
        'label'       => __('Avatar-Wechsel (Sekunden)', 'minecraft-modern-theme'),
        'section'     => 'login_settings',
        'type'        => 'number',
        'input_attrs' => array( 'min' => 2, 'max' => 10, 'step' => 1 ),
    ) );


    // =========================================================================
    // 8.5. VIDEO / LIVESTREAM EINSTELLUNGEN
    // =========================================================================
    $wp_customize->add_section( 'video_livestream_settings', array(
        'title'       => __('Video & Livestream', 'minecraft-modern-theme'),
        'description' => __('Einstellungen für YouTube Livestream-Erkennung. Hauptmethode: Livestream-Posts unter "Livestreams" erstellen. Optional: Zusätzlichen Hauptkanal hier eintragen.', 'minecraft-modern-theme'),
        'priority'    => 75,
    ) );

    $wp_customize->add_setting( 'youtube_api_key', array(
        'default'           => '',
        'sanitize_callback' => 'sanitize_text_field',
        'transport'         => 'refresh',
    ) );
    $wp_customize->add_control( 'youtube_api_key', array(
        'label'       => __('YouTube API Key', 'minecraft-modern-theme'),
        'description' => __('Erforderlich für automatische YouTube Live-Erkennung. <a href="https://console.cloud.google.com/" target="_blank">Hier API Key erstellen</a>', 'minecraft-modern-theme'),
        'section'     => 'video_livestream_settings',
        'type'        => 'text',
        'input_attrs' => array(
            'placeholder' => 'AIzaSyD...',
        ),
    ) );

    $wp_customize->add_setting( 'twitch_client_id', array(
        'default'           => '',
        'sanitize_callback' => 'sanitize_text_field',
        'transport'         => 'refresh',
    ) );
    $wp_customize->add_control( 'twitch_client_id', array(
        'label'       => __('Twitch Client ID', 'minecraft-modern-theme'),
        'description' => __('Erforderlich für Twitch Live-Erkennung. <a href="https://dev.twitch.tv/console/apps" target="_blank">Hier App erstellen</a>', 'minecraft-modern-theme'),
        'section'     => 'video_livestream_settings',
        'type'        => 'text',
        'input_attrs' => array(
            'placeholder' => 'xxxxxxxxxxxxxx',
        ),
    ) );

    $wp_customize->add_setting( 'twitch_client_secret', array(
        'default'           => '',
        'sanitize_callback' => 'sanitize_text_field',
        'transport'         => 'refresh',
    ) );
    $wp_customize->add_control( 'twitch_client_secret', array(
        'label'       => __('Twitch Client Secret', 'minecraft-modern-theme'),
        'description' => __('Nur für Live-Prüfung. Wird serverseitig genutzt.', 'minecraft-modern-theme'),
        'section'     => 'video_livestream_settings',
        'type'        => 'text',
        'input_attrs' => array(
            'placeholder' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        ),
    ) );

    // =========================================================================
    // 9. EXPORT / IMPORT
    // =========================================================================
    $wp_customize->add_section( 'theme_mods_import_export', array(
        'title'    => __('Einstellungen sichern', 'minecraft-modern-theme'),
        'priority' => 999,
    ) );
    $wp_customize->add_setting( 'import_export_placeholder', array( 'sanitize_callback' => 'sanitize_text_field' ) );
    $wp_customize->add_control( new MM_Import_Export_Control( $wp_customize, 'import_export_placeholder', array(
        'section' => 'theme_mods_import_export',
    ) ) );
}
add_action( 'customize_register', 'minecraft_modern_customize_register' );


// =========================================================================
// DYNAMISCHES CSS – FIX: Google Font via wp_enqueue_style(), nicht <link> in wp_head
// =========================================================================
function minecraft_modern_enqueue_dynamic_styles() {
    $font = get_theme_mod( 'slider_font_family', 'Raleway' );

    // Google Font sauber einbinden
    $font_url = 'https://fonts.googleapis.com/css2?family=' . urlencode($font) . ':wght@400;600;700&display=swap';
    wp_enqueue_style( 'minecraft-modern-google-font', $font_url, array(), null );

    // Dynamisches CSS als inline style
    $accent_color          = get_theme_mod( 'primary_accent_color', '#00d4ff' );
    $slider_color          = get_theme_mod( 'slider_font_color', '#ffffff' );
    $slider_size_setting   = get_theme_mod( 'slider_font_size', 'mittel' );
    $header_height_setting = get_theme_mod( 'header_height', 'mittel' );

    $height_map = array( 'klein' => '200px', 'mittel' => '300px', 'gross' => '400px' );
    $header_height_value = isset($height_map[$header_height_setting]) ? $height_map[$header_height_setting] : '300px';

    $font_sizes = array(
        'klein'       => array( 'title' => '2.5rem', 'subtitle' => '1.2rem' ),
        'mittel'      => array( 'title' => '3.5rem', 'subtitle' => '1.4rem' ),
        'gross'       => array( 'title' => '4.5rem', 'subtitle' => '1.6rem' ),
        'extra-gross' => array( 'title' => '5.5rem', 'subtitle' => '1.8rem' ),
    );
    $sizes = isset($font_sizes[$slider_size_setting]) ? $font_sizes[$slider_size_setting] : $font_sizes['mittel'];

    $css = "
        :root {
            --primary-accent: " . esc_attr($accent_color) . ";
            --header-height: " . esc_attr($header_height_value) . ";
        }
        .slider-title, .slider-subtitle, .hero-title, .hero-subtitle, .hero-button-1, .hero-button-2 {
            font-family: '" . esc_attr($font) . "', sans-serif;
            color: " . esc_attr($slider_color) . ";
        }
        .slider-title, .hero-title   { font-size: " . esc_attr($sizes['title'])    . "; }
        .slider-subtitle, .hero-subtitle { font-size: " . esc_attr($sizes['subtitle']) . "; }
        .site-header  { border-bottom: 4px solid var(--primary-accent); }
        .hero-slider  { border-bottom: 4px solid var(--primary-accent); }
        .site-footer  { border-top:    4px solid var(--primary-accent); }
    ";

    wp_add_inline_style( 'minecraft-modern-style', $css );
}
add_action( 'wp_enqueue_scripts', 'minecraft_modern_enqueue_dynamic_styles', 20 );


// =========================================================================
// PRESET LOGIC (JS im Customizer)
// =========================================================================
function minecraft_preset_customize_js() { ?>
    <script>
    (function($){
        var presetColors = { classic: '#00d4ff', nether: '#ff3333', end: '#aa00ff' };
        wp.customize.bind('ready', function(){
            wp.customize('theme_color_preset', function(setting){
                setting.bind(function(to){
                    if (presetColors[to]) {
                        wp.customize('primary_accent_color').set(presetColors[to]);
                    }
                });
            });
        });
    })(jQuery);
    </script>
<?php }
add_action( 'customize_controls_print_footer_scripts', 'minecraft_preset_customize_js' );


// =========================================================================
// FIX: slider_loop an JS übergeben (in functions.php in wp_localize_script ergänzen)
// Der folgende Filter hängt den Wert an sliderSettings an.
// Alternativ: direkt in functions.php bei wp_localize_script 'loop' hinzufügen.
// =========================================================================
add_filter( 'minecraft_modern_slider_settings', function( $settings ) {
    $settings['loop'] = get_theme_mod('slider_loop', true) ? '1' : '0';
    return $settings;
} );