<?php
if ( ! defined( 'ABSPATH' ) ) exit;
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template-scoped local variables.
$tab  = isset( $tab ) ? $tab : 'dashboard';
$opts = Falcify_Settings::get();

// Backward compatibility : anciens slugs d'onglets.
$tab_aliases = array(
    'overview'   => 'dashboard',
    'history'    => 'contents',
    'connection' => 'settings',
    'post-types' => 'settings',
    'appearance' => 'settings',
    'compat'     => 'settings',
);
if ( isset( $tab_aliases[ $tab ] ) ) {
    $tab = $tab_aliases[ $tab ];
}

$tabs = array(
    'dashboard' => array( 'label' => __( 'Tableau de bord', 'falcify-connector' ), 'icon' => '🏠' ),
    'contents'  => array( 'label' => __( 'Mes contenus', 'falcify-connector' ),    'icon' => '📄' ),
    'stats'     => array( 'label' => __( 'Statistiques', 'falcify-connector' ),    'icon' => '📊' ),
    'pictos'    => array( 'label' => __( 'Pictogrammes', 'falcify-connector' ),    'icon' => '🖼️' ),
    'settings'  => array( 'label' => __( 'Paramètres', 'falcify-connector' ),      'icon' => '⚙️' ),
    'support'   => array( 'label' => __( 'Support', 'falcify-connector' ),         'icon' => '💬' ),
    'help'      => array( 'label' => __( 'Aide', 'falcify-connector' ),            'icon' => '❓' ),
);

$api_key      = trim( (string) $opts['api_key'] );
$is_connected = $api_key !== '';

// Compteurs simples (pas d'appel API supplémentaire).
$recent       = Falcify_History::recent( 50 );
$recent_count = count( $recent );
$recent5      = array_slice( $recent, 0, 5 );

$total_words  = 0;
$total_falc   = 0;
foreach ( $recent as $rp ) {
    $total_words += (int) get_post_meta( $rp->ID, '_falcify_word_count', true );
    if ( get_post_meta( $rp->ID, '_falcify_content', true ) ) $total_falc++;
}
// Estimation : 12 minutes économisées par contenu simplifié.
$time_saved_min   = $total_falc * 12;
$time_saved_hours = $time_saved_min >= 60 ? round( $time_saved_min / 60, 1 ) : 0;
?>
<?php
// Onboarding guidé 1 minute (Générer → Vérifier → Publier).
$uid = get_current_user_id();
$onb_done = (bool) get_user_meta( $uid, 'falcify_onboarding_done', true );
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only UI toggle
$force_tour = isset( $_GET['falcify_tour'] ) && '1' === sanitize_text_field( wp_unslash( $_GET['falcify_tour'] ) );
$show_tour  = $is_connected && ( ! $onb_done || $force_tour );
?>
<div class="wrap falcify-wrap falcify-app" data-falcify-tour-autostart="<?php echo $show_tour ? '1' : '0'; ?>">
    <header class="falcify-header">
        <div class="falcify-brand">
            <span class="falcify-logo" aria-hidden="true">
                <svg viewBox="0 0 758 763" xmlns="http://www.w3.org/2000/svg" focusable="false">
                    <path d="M134.769 1.19581C140.626 0.806813 146.495 0.599818 152.365 0.575818C258.11 -0.707182 366.552 0.559809 472.492 0.522809L565.352 0.503827C596.162 0.501827 633.432 -2.25717 662.082 9.90983C727.452 37.6688 757.172 92.1988 757.322 161.382L757.282 398.72L757.332 468.83C757.352 492.707 758.302 520.808 752.882 543.842C746.422 570.656 732.702 595.186 713.222 614.716C672.402 655.296 635.242 657.676 582.252 657.596L518.792 657.546C491.822 657.496 428.342 654.516 405.482 660.606C386.812 665.576 370.412 677.766 354.211 687.876L281.223 733.426C265.929 742.926 239.456 761.866 222.262 762.636C211.758 763.186 201.499 759.336 193.96 751.996C189.518 747.586 186.321 742.086 184.691 736.046C180.668 721.556 182.361 671.426 182.511 653.046C135.606 655.336 97.6496 653.486 58.8446 623.846C27.7706 599.976 7.32369 564.856 1.91569 526.051C-0.691311 506.294 0.262656 474.51 0.268656 453.678L0.289652 337.269L0.216654 209.729C0.182654 185.72 -1.05033 147.991 2.76267 125.483C6.93567 100.763 17.2577 77.4888 32.7777 57.8008C58.2307 25.0868 93.8767 6.11081 134.769 1.19581Z" fill="#FFFFFF"/>
                    <path d="M500.712 193.882C541.302 188.086 578.962 216.119 585.062 256.665C591.162 297.211 563.412 335.085 522.912 341.487C481.982 347.957 443.622 319.846 437.452 278.87C431.292 237.893 459.682 199.74 500.712 193.882Z" fill="#68C198"/>
                </svg>
            </span>
            <div>
                <h1>Falcify</h1>
                <p><?php esc_html_e( 'Votre assistant pour rendre vos contenus faciles à comprendre', 'falcify-connector' ); ?></p>
            </div>
        </div>
        <span class="falcify-version">v<?php echo esc_html( FALCIFY_VERSION ); ?></span>
    </header>

    <nav class="falcify-tabs falcify-tabs-flat" aria-label="<?php esc_attr_e( 'Navigation Falcify', 'falcify-connector' ); ?>">
        <?php foreach ( $tabs as $k => $info ): ?>
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=falcify-connector&tab=' . $k ) ); ?>"
               class="falcify-tab <?php echo $tab === $k ? 'is-active' : ''; ?>">
                <span class="falcify-tab-ico" aria-hidden="true"><?php echo esc_html( $info['icon'] ); ?></span>
                <span><?php echo esc_html( $info['label'] ); ?></span>
            </a>
        <?php endforeach; ?>
    </nav>

    <div class="falcify-content">

    <?php if ( $tab === 'dashboard' ): ?>
        <?php if ( $is_connected ) { include FALCIFY_DIR . 'templates/onboarding-tour.php'; } ?>


        <?php if ( ! $is_connected ): ?>
            <div class="falcify-onboarding">
                <span class="falcify-onb-step">1 minute pour démarrer</span>
                <h2><?php esc_html_e( 'Bienvenue sur Falcify', 'falcify-connector' ); ?></h2>
                <p><?php esc_html_e( 'Connectez votre compte Falcify pour transformer vos textes en versions faciles à comprendre, en un clic, depuis WordPress.', 'falcify-connector' ); ?></p>
                <ol class="falcify-onb-steps">
                    <li><strong><?php esc_html_e( 'Créez votre compte', 'falcify-connector' ); ?></strong><br><?php esc_html_e( 'Sur falcify.fr, en 30 secondes.', 'falcify-connector' ); ?></li>
                    <li><strong><?php esc_html_e( 'Copiez votre clé', 'falcify-connector' ); ?></strong><br><?php esc_html_e( 'Depuis votre tableau de bord Falcify.', 'falcify-connector' ); ?></li>
                    <li><strong><?php esc_html_e( 'Collez-la ici', 'falcify-connector' ); ?></strong><br><?php esc_html_e( 'Et générez votre première version facile à lire.', 'falcify-connector' ); ?></li>
                </ol>
                <div class="falcify-onb-cta">
                    <a class="button button-primary button-hero" href="<?php echo esc_url( admin_url( 'admin.php?page=falcify-connector&tab=settings' ) ); ?>">
                        <?php esc_html_e( 'Connecter Falcify', 'falcify-connector' ); ?>
                    </a>
                    <a class="button button-link" href="<?php echo esc_url( $opts['saas_url'] ); ?>" target="_blank" rel="noopener">
                        <?php esc_html_e( 'Créer un compte', 'falcify-connector' ); ?>
                    </a>
                </div>
            </div>
        <?php else: ?>

            <section class="falcify-hello">
                <div>
                    <h2><?php
                        $hour = (int) current_time( 'H' );
                        $greet = $hour < 18 ? __( 'Bonjour', 'falcify-connector' ) : __( 'Bonsoir', 'falcify-connector' );
                        echo esc_html( $greet . ', ' . wp_get_current_user()->display_name );
                    ?> 👋</h2>
                    <p class="falcify-muted"><?php esc_html_e( 'Voici l\'impact de Falcify sur votre site.', 'falcify-connector' ); ?></p>
                    <p class="falcify-muted falcify-tour-replay"><a href="#" data-tour-open>↻ <?php esc_html_e( 'Revoir la visite guidée (1 min)', 'falcify-connector' ); ?></a></p>
                </div>
                <div class="falcify-hello-status">
                    <span class="falcify-pill falcify-pill-ok" data-connection-pill>
                        <span class="falcify-dot is-ok"></span>
                        <span data-connection-text><?php esc_html_e( 'Vérification…', 'falcify-connector' ); ?></span>
                    </span>
                </div>
            </section>

            <section class="falcify-metrics">
                <div class="falcify-metric">
                    <span class="falcify-metric-ico" aria-hidden="true">📚</span>
                    <span class="falcify-metric-value"><?php echo esc_html( number_format_i18n( $total_falc ) ); ?></span>
                    <span class="falcify-metric-label"><?php esc_html_e( 'contenus simplifiés', 'falcify-connector' ); ?></span>
                </div>
                <div class="falcify-metric">
                    <span class="falcify-metric-ico" aria-hidden="true">✍️</span>
                    <span class="falcify-metric-value"><?php echo esc_html( number_format_i18n( $total_words ) ); ?></span>
                    <span class="falcify-metric-label"><?php esc_html_e( 'mots rendus accessibles', 'falcify-connector' ); ?></span>
                </div>
                <div class="falcify-metric">
                    <span class="falcify-metric-ico" aria-hidden="true">⏱️</span>
                    <span class="falcify-metric-value">
                        <?php if ( $time_saved_hours > 0 ) {
                            /* translators: %s: number of hours */
                            /* translators: %s = nombre d'heures économisées (ex. 1,5) */
                        printf( esc_html__( '%sh', 'falcify-connector' ), esc_html( number_format_i18n( $time_saved_hours, 1 ) ) );
                        } else {
                            /* translators: %s: number of minutes */
                            /* translators: %s = nombre de minutes économisées */
                        printf( esc_html__( '%s min', 'falcify-connector' ), esc_html( number_format_i18n( $time_saved_min ) ) );
                        } ?>
                    </span>
                    <span class="falcify-metric-label"><?php esc_html_e( 'de réécriture économisés', 'falcify-connector' ); ?></span>
                </div>
            </section>

            <section class="falcify-quick">
                <div class="falcify-quick-card">
                    <div>
                        <h3><?php esc_html_e( 'Simplifier un nouveau contenu', 'falcify-connector' ); ?></h3>
                        <p class="falcify-muted"><?php esc_html_e( 'Ouvrez n\'importe quelle page ou article. Falcify ajoute automatiquement un encadré « Version facile à lire » dans l\'éditeur.', 'falcify-connector' ); ?></p>
                    </div>
                    <a class="button button-primary button-hero" href="<?php echo esc_url( admin_url( 'edit.php?post_type=page' ) ); ?>">
                        <?php esc_html_e( 'Choisir une page', 'falcify-connector' ); ?>
                    </a>
                </div>
            </section>

            <section class="falcify-recent">
                <header class="falcify-section-head">
                    <h3><?php esc_html_e( 'Activité récente', 'falcify-connector' ); ?></h3>
                    <a href="<?php echo esc_url( admin_url( 'admin.php?page=falcify-connector&tab=contents' ) ); ?>"><?php esc_html_e( 'Tout voir', 'falcify-connector' ); ?> →</a>
                </header>
                <?php if ( empty( $recent5 ) ): ?>
                    <div class="falcify-empty">
                        <p><?php esc_html_e( 'Aucun contenu simplifié pour l\'instant. Ouvrez une page pour commencer.', 'falcify-connector' ); ?></p>
                    </div>
                <?php else: ?>
                    <ul class="falcify-recent-list">
                        <?php foreach ( $recent5 as $p ):
                            $st = get_post_meta( $p->ID, '_falcify_status', true );
                            $dt = get_post_meta( $p->ID, '_falcify_last_generated_at', true );
                            $state_label = array(
                                'validated' => __( 'Conforme FALC', 'falcify-connector' ),
                                'draft'     => __( 'À vérifier', 'falcify-connector' ),
                                'published' => __( 'Publié', 'falcify-connector' ),
                            );
                            $state_class = array(
                                'validated' => 'falcify-pill-ok',
                                'draft'     => 'falcify-pill-warn',
                                'published' => 'falcify-pill-info',
                            );
                            $st_key = $st && isset( $state_label[ $st ] ) ? $st : 'draft';
                        ?>
                            <li class="falcify-recent-item">
                                <a class="falcify-recent-title" href="<?php echo esc_url( get_edit_post_link( $p->ID ) ); ?>"><?php echo esc_html( get_the_title( $p ) ); ?></a>
                                <span class="falcify-pill <?php echo esc_attr( $state_class[ $st_key ] ); ?>"><?php echo esc_html( $state_label[ $st_key ] ); ?></span>
                                <span class="falcify-recent-date"><?php echo esc_html( $dt ); ?></span>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </section>

        <?php endif; ?>

    <?php elseif ( $tab === 'contents' ): ?>
        <section class="falcify-section-head">
            <h2><?php esc_html_e( 'Mes contenus simplifiés', 'falcify-connector' ); ?></h2>
            <p class="falcify-muted"><?php esc_html_e( 'Tous vos contenus avec une version facile à lire.', 'falcify-connector' ); ?></p>
        </section>
        <?php if ( empty( $recent ) ): ?>
            <div class="falcify-empty">
                <p><?php esc_html_e( 'Aucun contenu simplifié pour l\'instant.', 'falcify-connector' ); ?></p>
                <a class="button button-primary" href="<?php echo esc_url( admin_url( 'edit.php?post_type=page' ) ); ?>"><?php esc_html_e( 'Choisir une page', 'falcify-connector' ); ?></a>
            </div>
        <?php else: ?>
            <table class="widefat falcify-history">
                <thead><tr>
                    <th><?php esc_html_e( 'Titre', 'falcify-connector' ); ?></th>
                    <th><?php esc_html_e( 'Type', 'falcify-connector' ); ?></th>
                    <th><?php esc_html_e( 'Statut', 'falcify-connector' ); ?></th>
                    <th><?php esc_html_e( 'Mots', 'falcify-connector' ); ?></th>
                    <th><?php esc_html_e( 'Mis à jour', 'falcify-connector' ); ?></th>
                    <th></th>
                </tr></thead>
                <tbody>
                <?php foreach ( $recent as $p ):
                    $st = get_post_meta( $p->ID, '_falcify_status', true );
                    $wc = get_post_meta( $p->ID, '_falcify_word_count', true );
                    $dt = get_post_meta( $p->ID, '_falcify_last_generated_at', true );
                ?>
                    <tr>
                        <td><strong><?php echo esc_html( get_the_title( $p ) ); ?></strong></td>
                        <td><code><?php echo esc_html( $p->post_type ); ?></code></td>
                        <td><span class="falcify-status falcify-status-<?php echo esc_attr( $st ?: 'none' ); ?>"><?php echo esc_html( $st ?: __( 'brouillon', 'falcify-connector' ) ); ?></span></td>
                        <td><?php echo esc_html( $wc ); ?></td>
                        <td><?php echo esc_html( $dt ); ?></td>
                        <td><a class="button button-small" href="<?php echo esc_url( get_edit_post_link( $p->ID ) ); ?>"><?php esc_html_e( 'Modifier', 'falcify-connector' ); ?></a></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        <?php endif; ?>

    <?php elseif ( $tab === 'stats' ): ?>
        <section class="falcify-section-head">
            <h2><?php esc_html_e( 'Statistiques', 'falcify-connector' ); ?></h2>
            <p class="falcify-muted"><?php esc_html_e( 'L\'impact de Falcify sur l\'accessibilité de votre site.', 'falcify-connector' ); ?></p>
        </section>
        <section class="falcify-metrics">
            <div class="falcify-metric">
                <span class="falcify-metric-ico" aria-hidden="true">📚</span>
                <span class="falcify-metric-value"><?php echo esc_html( number_format_i18n( $total_falc ) ); ?></span>
                <span class="falcify-metric-label"><?php esc_html_e( 'contenus simplifiés', 'falcify-connector' ); ?></span>
            </div>
            <div class="falcify-metric">
                <span class="falcify-metric-ico" aria-hidden="true">✍️</span>
                <span class="falcify-metric-value"><?php echo esc_html( number_format_i18n( $total_words ) ); ?></span>
                <span class="falcify-metric-label"><?php esc_html_e( 'mots rendus accessibles', 'falcify-connector' ); ?></span>
            </div>
            <div class="falcify-metric">
                <span class="falcify-metric-ico" aria-hidden="true">⏱️</span>
                <span class="falcify-metric-value">
                    <?php if ( $time_saved_hours > 0 ) {
                        /* translators: %s = nombre d'heures économisées (ex. 1,5) */
                        printf( esc_html__( '%sh', 'falcify-connector' ), esc_html( number_format_i18n( $time_saved_hours, 1 ) ) );
                    } else {
                        /* translators: %s = nombre de minutes économisées */
                        printf( esc_html__( '%s min', 'falcify-connector' ), esc_html( number_format_i18n( $time_saved_min ) ) );
                    } ?>
                </span>
                <span class="falcify-metric-label"><?php esc_html_e( 'de réécriture économisés', 'falcify-connector' ); ?></span>
            </div>
        </section>
        <div class="falcify-card">
            <h3><?php esc_html_e( 'Consommation de votre forfait', 'falcify-connector' ); ?></h3>
            <div class="falcify-usage" data-usage>
                <p class="falcify-muted"><?php esc_html_e( 'Chargement…', 'falcify-connector' ); ?></p>
            </div>
            <p><button class="button falcify-btn-refresh-usage"><?php esc_html_e( 'Actualiser', 'falcify-connector' ); ?></button></p>
        </div>

    <?php elseif ( $tab === 'pictos' ): ?>
        <form method="post" action="options.php" class="falcify-form">
            <?php settings_fields( 'falcify_settings_group' ); ?>
            <section class="falcify-section-head">
                <h2><?php esc_html_e( 'Pictogrammes', 'falcify-connector' ); ?></h2>
                <p class="falcify-muted"><?php esc_html_e( 'Ajoutez automatiquement de petites images d\'aide (📞 ✉️ 📍 📅) devant les phrases pour guider la lecture.', 'falcify-connector' ); ?></p>
            </section>

            <label class="falcify-toggle-card">
                <input type="checkbox" name="<?php echo esc_attr( Falcify_Settings::OPTION ); ?>[show_pictos]" value="1" <?php checked( ! empty( $opts['show_pictos'] ), 1 ); ?>>
                <span class="falcify-toggle-text">
                    <strong><?php esc_html_e( 'Ajouter automatiquement des pictogrammes', 'falcify-connector' ); ?></strong>
                    <em><?php esc_html_e( 'Recommandé. Améliore la compréhension visuelle.', 'falcify-connector' ); ?></em>
                </span>
            </label>

            <details class="falcify-advanced">
                <summary><?php esc_html_e( 'Réglages avancés', 'falcify-connector' ); ?></summary>
                <fieldset>
                    <legend><?php esc_html_e( 'Où afficher les pictogrammes', 'falcify-connector' ); ?></legend>
                    <label class="falcify-switch"><input type="checkbox" name="<?php echo esc_attr( Falcify_Settings::OPTION ); ?>[show_pictos_pages]" value="1" <?php checked( ! empty( $opts['show_pictos_pages'] ), 1 ); ?>><?php esc_html_e( 'Sur les pages', 'falcify-connector' ); ?></label>
                    <label class="falcify-switch"><input type="checkbox" name="<?php echo esc_attr( Falcify_Settings::OPTION ); ?>[show_pictos_posts]" value="1" <?php checked( ! empty( $opts['show_pictos_posts'] ), 1 ); ?>><?php esc_html_e( 'Sur les articles', 'falcify-connector' ); ?></label>
                    <label class="falcify-switch"><input type="checkbox" name="<?php echo esc_attr( Falcify_Settings::OPTION ); ?>[show_pictos_shortcode]" value="1" <?php checked( ! empty( $opts['show_pictos_shortcode'] ), 1 ); ?>><?php esc_html_e( 'Dans les shortcodes', 'falcify-connector' ); ?></label>
                </fieldset>
                <fieldset>
                    <legend><?php esc_html_e( 'Apparence', 'falcify-connector' ); ?></legend>
                    <label><?php esc_html_e( 'Taille des pictogrammes (px)', 'falcify-connector' ); ?>
                        <input type="number" id="falcify-picto-size-input" step="1" min="40" max="200" name="<?php echo esc_attr( Falcify_Settings::OPTION ); ?>[picto_size]" value="<?php echo esc_attr( $opts['picto_size'] ); ?>">
                        <em class="falcify-muted"><?php esc_html_e( 'Recommandé : 100px (FALC standard), 65px (compact), 130px (grand).', 'falcify-connector' ); ?></em>
                        <span class="falcify-picto-presets">
                            <button type="button" class="button button-small" data-picto-size="60"><?php esc_html_e( 'S', 'falcify-connector' ); ?></button>
                            <button type="button" class="button button-small" data-picto-size="80"><?php esc_html_e( 'M', 'falcify-connector' ); ?></button>
                            <button type="button" class="button button-small" data-picto-size="100"><?php esc_html_e( 'L (FALC)', 'falcify-connector' ); ?></button>
                            <button type="button" class="button button-small" data-picto-size="130"><?php esc_html_e( 'XL', 'falcify-connector' ); ?></button>
                            <button type="button" class="button button-small" data-picto-size="160"><?php esc_html_e( 'XXL', 'falcify-connector' ); ?></button>
                        </span>
                    </label>
                    <label><?php esc_html_e( 'Espacement à droite (px)', 'falcify-connector' ); ?>
                        <input type="number" step="1" min="0" max="60" name="<?php echo esc_attr( Falcify_Settings::OPTION ); ?>[picto_spacing]" value="<?php echo esc_attr( $opts['picto_spacing'] ); ?>">
                    </label>
                    <label><?php esc_html_e( 'Couleur', 'falcify-connector' ); ?>
                        <input type="text" name="<?php echo esc_attr( Falcify_Settings::OPTION ); ?>[picto_color]" value="<?php echo esc_attr( $opts['picto_color'] ); ?>" placeholder="#12294C">
                    </label>
                    <div class="falcify-picto-live-wrap">
                        <span class="falcify-muted"><?php esc_html_e( 'Aperçu :', 'falcify-connector' ); ?></span>
                        <span class="falcify-picto-live" id="falcify-picto-live" style="--falc-picto-size:<?php echo esc_attr( $opts['picto_size'] ); ?>px">
                            <span class="falc-picto" role="img" aria-label="aperçu">
                                <img src="<?php echo esc_url( FALCIFY_URL . 'assets/pictos/12-7-france-carte.png' ); ?>" alt="" width="32" height="32" />
                            </span>
                            <?php esc_html_e( 'Exemple de phrase facile à lire.', 'falcify-connector' ); ?>
                        </span>
                    </div>
                </fieldset>
            </details>

            <p><button type="submit" class="button button-primary"><?php esc_html_e( 'Enregistrer', 'falcify-connector' ); ?></button></p>
            <?php echo Falcify_Settings::declare_fields( array( 'show_pictos', 'show_pictos_pages', 'show_pictos_posts', 'show_pictos_shortcode' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
        </form>

    <?php elseif ( $tab === 'settings' ): ?>
        <section class="falcify-section-head">
            <h2><?php esc_html_e( 'Paramètres', 'falcify-connector' ); ?></h2>
            <p class="falcify-muted"><?php esc_html_e( 'Tout ce dont vous avez besoin pour configurer Falcify.', 'falcify-connector' ); ?></p>
        </section>

        <form method="post" action="options.php" class="falcify-form">
            <?php settings_fields( 'falcify_settings_group' ); ?>

            <details class="falcify-advanced" open>
                <summary><?php esc_html_e( '🔑 Connexion à Falcify', 'falcify-connector' ); ?></summary>
                <p class="falcify-status-line">
                    <span class="falcify-dot <?php echo $is_connected ? 'is-ok' : 'is-err'; ?>" data-connection-dot></span>
                    <span data-connection-text>
                        <?php echo $is_connected ? esc_html__( 'Vérification…', 'falcify-connector' ) : esc_html__( 'Non connecté', 'falcify-connector' ); ?>
                    </span>
                </p>
                <label><?php esc_html_e( 'Clé API', 'falcify-connector' ); ?>
                    <input type="password" name="<?php echo esc_attr( Falcify_Settings::OPTION ); ?>[api_key]" value="<?php echo esc_attr( $opts['api_key'] ); ?>" autocomplete="off">
                </label>
                <p class="falcify-muted"><?php esc_html_e( 'Récupérez votre clé sur falcify.fr.', 'falcify-connector' ); ?></p>
                <p>
                    <button type="button" class="button falcify-btn-test"><?php esc_html_e( 'Tester la connexion', 'falcify-connector' ); ?></button>
                </p>
            </details>

            <details class="falcify-advanced">
                <summary><?php esc_html_e( '📄 Sur quels contenus activer Falcify', 'falcify-connector' ); ?></summary>
                <div class="falcify-checks">
                    <?php foreach ( Falcify_Post_Types::get_public_post_types() as $pt => $obj ): ?>
                        <label class="falcify-check">
                            <input type="checkbox" name="<?php echo esc_attr( Falcify_Settings::OPTION ); ?>[enabled_post_types][]" value="<?php echo esc_attr( $pt ); ?>" <?php checked( in_array( $pt, (array) $opts['enabled_post_types'], true ) ); ?>>
                            <span><?php echo esc_html( $obj->labels->name ); ?> <code><?php echo esc_html( $pt ); ?></code></span>
                        </label>
                    <?php endforeach; ?>
                </div>
                <?php echo Falcify_Settings::declare_fields( array( 'enabled_post_types' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
            </details>

            <details class="falcify-advanced">
                <summary><?php esc_html_e( '🎨 Apparence du bouton « Lire facilement »', 'falcify-connector' ); ?></summary>
                <label class="falcify-switch">
                    <input type="checkbox" name="<?php echo esc_attr( Falcify_Settings::OPTION ); ?>[auto_inject]" value="1" <?php checked( $opts['auto_inject'], 1 ); ?>>
                    <?php esc_html_e( 'Afficher automatiquement le bouton « Lire facilement »', 'falcify-connector' ); ?>
                </label>
                <label><?php esc_html_e( 'Position', 'falcify-connector' ); ?>
                    <select name="<?php echo esc_attr( Falcify_Settings::OPTION ); ?>[button_position]">
                        <?php foreach ( array(
                            'before'   => __( 'Avant le contenu', 'falcify-connector' ),
                            'after'    => __( 'Après le contenu', 'falcify-connector' ),
                            'both'     => __( 'Avant et après', 'falcify-connector' ),
                            'floating' => __( 'Bouton flottant', 'falcify-connector' ),
                        ) as $k => $v ): ?>
                            <option value="<?php echo esc_attr( $k ); ?>" <?php selected( $opts['button_position'], $k ); ?>><?php echo esc_html( $v ); ?></option>
                        <?php endforeach; ?>
                    </select>
                </label>
                <label><?php esc_html_e( 'Texte du bouton « facile à lire »', 'falcify-connector' ); ?>
                    <input type="text" name="<?php echo esc_attr( Falcify_Settings::OPTION ); ?>[button_text_falc]" value="<?php echo esc_attr( $opts['button_text_falc'] ); ?>">
                </label>
                <label><?php esc_html_e( 'Texte du bouton « version normale »', 'falcify-connector' ); ?>
                    <input type="text" name="<?php echo esc_attr( Falcify_Settings::OPTION ); ?>[button_text_normal]" value="<?php echo esc_attr( $opts['button_text_normal'] ); ?>">
                </label>
                <label><?php esc_html_e( 'Couleur du bouton', 'falcify-connector' ); ?>
                    <input type="text" name="<?php echo esc_attr( Falcify_Settings::OPTION ); ?>[button_color]" value="<?php echo esc_attr( $opts['button_color'] ); ?>" placeholder="#12294C">
                </label>
                <label class="falcify-switch">
                    <input type="checkbox" name="<?php echo esc_attr( Falcify_Settings::OPTION ); ?>[high_contrast]" value="1" <?php checked( $opts['high_contrast'], 1 ); ?>>
                    <?php esc_html_e( 'Mode contraste élevé', 'falcify-connector' ); ?>
                </label>
                <label class="falcify-switch">
                    <input type="checkbox" name="<?php echo esc_attr( Falcify_Settings::OPTION ); ?>[strict_mode]" value="1" <?php checked( $opts['strict_mode'], 1 ); ?>>
                    <?php esc_html_e( 'Niveau facile renforcé (phrases ≤ 12 mots)', 'falcify-connector' ); ?>
                </label>
                <?php echo Falcify_Settings::declare_fields( array( 'auto_inject', 'high_contrast', 'strict_mode' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
            </details>

            <details class="falcify-advanced">
                <summary><?php esc_html_e( '⚙️ Compatibilité (Elementor, Divi, WPBakery…)', 'falcify-connector' ); ?></summary>
                <label class="falcify-switch">
                    <input type="checkbox" name="<?php echo esc_attr( Falcify_Settings::OPTION ); ?>[builder_mode]" value="1" <?php checked( $opts['builder_mode'], 1 ); ?>>
                    <?php esc_html_e( 'Mode compatibilité constructeur', 'falcify-connector' ); ?>
                </label>
                <label><?php esc_html_e( 'Sélecteur CSS du contenu (optionnel)', 'falcify-connector' ); ?>
                    <input type="text" name="<?php echo esc_attr( Falcify_Settings::OPTION ); ?>[content_selector]" value="<?php echo esc_attr( $opts['content_selector'] ); ?>" placeholder=".entry-content, .elementor, .main-content">
                </label>
                <label class="falcify-switch">
                    <input type="checkbox" name="<?php echo esc_attr( Falcify_Settings::OPTION ); ?>[load_assets_only_active]" value="1" <?php checked( $opts['load_assets_only_active'], 1 ); ?>>
                    <?php esc_html_e( 'Charger CSS/JS uniquement sur les contenus simplifiés', 'falcify-connector' ); ?>
                </label>
                <?php echo Falcify_Settings::declare_fields( array( 'builder_mode', 'load_assets_only_active' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
            </details>

            <p><button type="submit" class="button button-primary button-hero"><?php esc_html_e( 'Enregistrer les paramètres', 'falcify-connector' ); ?></button></p>
        </form>

    <?php elseif ( $tab === 'support' ): ?>
        <?php
        $api_key    = trim( (string) Falcify_Settings::get_value( 'api_key' ) );
        $plan       = 'free';
        $is_paid    = false;
        $billing_url = trailingslashit( Falcify_Settings::get_value( 'saas_url' ) ) . 'dashboard/billing';
        $contact_url = trailingslashit( Falcify_Settings::get_value( 'saas_url' ) ) . 'contact';
        if ( $api_key !== '' ) {
            $u = Falcify_Usage::get();
            if ( $u && ! empty( $u['subscription'] ) ) {
                $plan    = isset( $u['subscription']['plan'] ) ? $u['subscription']['plan'] : 'free';
                $is_paid = in_array( $plan, array( 'starter', 'pro', 'business', 'enterprise' ), true );
            }
        }
        ?>
        <?php if ( ! $is_paid ): ?>
            <div class="falcify-support-hero">
                <div class="falcify-support-hero-inner">
                    <span class="falcify-support-badge"><?php esc_html_e( 'Réservé aux abonnements payants', 'falcify-connector' ); ?></span>
                    <h2><?php esc_html_e( 'Un support humain. Une réponse rapide.', 'falcify-connector' ); ?></h2>
                    <p class="falcify-support-lead"><?php esc_html_e( 'Avec un abonnement payant, ouvrez un ticket directement depuis WordPress.', 'falcify-connector' ); ?></p>
                    <div class="falcify-support-cta">
                        <a class="button button-hero" href="<?php echo esc_url( $billing_url ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'Débloquer le support prioritaire', 'falcify-connector' ); ?></a>
                        <a class="button button-link" href="<?php echo esc_url( $contact_url ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'Une urgence ? Nous écrire', 'falcify-connector' ); ?></a>
                    </div>
                </div>
            </div>
        <?php else: ?>
            <div class="falcify-support-active">
                <span class="falcify-support-badge falcify-support-badge-on"><?php esc_html_e( 'Support prioritaire activé', 'falcify-connector' ); ?></span>
                <h2><?php esc_html_e( 'Ouvrir un ticket support', 'falcify-connector' ); ?></h2>
                <p class="falcify-muted"><?php
                    /* translators: %s: plan name */
                    printf( esc_html__( 'Plan %s — Notre équipe vous répond sous 24h ouvrées.', 'falcify-connector' ), '<strong>' . esc_html( ucfirst( $plan ) ) . '</strong>' );
                ?></p>
                <form class="falcify-support-form" id="falcify-support-form">
                    <div class="falcify-support-row">
                        <label><?php esc_html_e( 'Catégorie', 'falcify-connector' ); ?>
                            <select name="category" required>
                                <option value="installation"><?php esc_html_e( 'Installation', 'falcify-connector' ); ?></option>
                                <option value="connection"><?php esc_html_e( 'Connexion API', 'falcify-connector' ); ?></option>
                                <option value="generation" selected><?php esc_html_e( 'Génération facile à lire', 'falcify-connector' ); ?></option>
                                <option value="billing"><?php esc_html_e( 'Abonnement', 'falcify-connector' ); ?></option>
                                <option value="bug"><?php esc_html_e( 'Bug', 'falcify-connector' ); ?></option>
                                <option value="other"><?php esc_html_e( 'Autre', 'falcify-connector' ); ?></option>
                            </select>
                        </label>
                        <label><?php esc_html_e( 'Priorité', 'falcify-connector' ); ?>
                            <select name="priority">
                                <option value="low"><?php esc_html_e( 'Basse', 'falcify-connector' ); ?></option>
                                <option value="normal" selected><?php esc_html_e( 'Normale', 'falcify-connector' ); ?></option>
                                <option value="high"><?php esc_html_e( 'Haute', 'falcify-connector' ); ?></option>
                                <option value="urgent"><?php esc_html_e( 'Urgente', 'falcify-connector' ); ?></option>
                            </select>
                        </label>
                    </div>
                    <label><?php esc_html_e( 'Sujet', 'falcify-connector' ); ?>
                        <input type="text" name="subject" maxlength="200" required>
                    </label>
                    <label><?php esc_html_e( 'Décrivez votre problème', 'falcify-connector' ); ?>
                        <textarea name="message" rows="7" required></textarea>
                    </label>
                    <div class="falcify-support-actions">
                        <button type="submit" class="button button-primary button-hero"><?php esc_html_e( 'Envoyer le ticket', 'falcify-connector' ); ?></button>
                        <span class="falcify-support-status" data-support-status></span>
                    </div>
                </form>
            </div>
        <?php endif; ?>

    <?php elseif ( $tab === 'help' ): ?>
        <div class="falcify-card">
            <h3><?php esc_html_e( 'Premiers pas', 'falcify-connector' ); ?></h3>
            <ol>
                <li><?php
                    /* translators: %s: link to falcify.fr */
                    printf( wp_kses_post( __( 'Créez un compte sur %s', 'falcify-connector' ) ), '<a href="' . esc_url( $opts['saas_url'] ) . '">Falcify</a>' );
                ?></li>
                <li><?php esc_html_e( 'Copiez votre clé API depuis votre tableau de bord', 'falcify-connector' ); ?></li>
                <li><?php esc_html_e( 'Collez-la dans Paramètres → Connexion', 'falcify-connector' ); ?></li>
                <li><?php esc_html_e( 'Ouvrez une page et cliquez sur « Générer la version facile à lire »', 'falcify-connector' ); ?></li>
            </ol>
            <h3><?php esc_html_e( 'Qu\'est-ce qu\'un texte facile à lire ?', 'falcify-connector' ); ?></h3>
            <p><?php esc_html_e( 'Le « Facile à Lire et à Comprendre » (FALC) est une méthode européenne pour rendre l\'information accessible aux personnes en situation de handicap intellectuel, aux personnes âgées, aux primo-arrivants, etc.', 'falcify-connector' ); ?></p>
            <h3><?php esc_html_e( 'Besoin d\'aide ?', 'falcify-connector' ); ?></h3>
            <p><a class="button" href="<?php echo esc_url( $opts['saas_url'] ); ?>"><?php esc_html_e( 'Centre d\'aide Falcify', 'falcify-connector' ); ?></a></p>
        </div>

    <?php endif; ?>
    </div>
</div>
