<?php
// Protection from direct access
if (!defined('ABSPATH')) {
    exit;
}

// Подключаем только базовый файл генерации, функционал expert/longread отсутствует в Lite
require_once AI_CONTENT_WIZARD_PATH . 'includes/single-post-generation.php';

function enqueue_single_post_script($hook) {
    if (!isset($_GET['page']) || $_GET['page'] !== 'ai_content_wizard_create_post') {
        return;
    }

    $js_file_path = AI_CONTENT_WIZARD_PATH . 'js/single-post-generation.js';
    $js_version   = AI_CONTENT_WIZARD_VERSION . '-' . (file_exists($js_file_path) ? filemtime($js_file_path) : time());

    wp_enqueue_script(
        'ai-content-wizard-single-post-script',
        AI_CONTENT_WIZARD_URL . 'js/single-post-generation.js',
        array('jquery'),
        $js_version,
        true
    );

    $initial_settings = [
        'format'               => ai_content_wizard_get_setting('ai_content_wizard_format', 'post'),
        'textLength'           => ai_content_wizard_get_setting('ai_content_wizard_text_length', '500'),
        'writingStyle'         => ai_content_wizard_get_setting('ai_content_wizard_writing_style', 'formal'),
        'targetAudience'       => ai_content_wizard_get_setting('ai_content_wizard_target_audience', 'general'),
        'targetRegion'         => ai_content_wizard_get_setting('ai_content_wizard_target_region', ''),
        'additionalRequests'   => ai_content_wizard_get_setting('ai_content_wizard_additional_requests', ''),
        'enableImageGeneration'=> ai_content_wizard_get_setting('ai_content_wizard_enable_image_generation', 0),
        'siteType'             => ai_content_wizard_get_setting('ai_content_wizard_site_type', 'store'),
        'postStatus'           => ai_content_wizard_get_setting('ai_content_wizard_post_status', 'publish'),
        'enableAdditionalImages' => 0,
        'numberOfAdditionalImages' => 0,
        'textModel'            => ai_content_wizard_get_setting('ai_content_wizard_text_model', 'gpt-4o-mini'),
    ];

    wp_localize_script('ai-content-wizard-single-post-script', 'initialSettings', $initial_settings);

    wp_localize_script('ai-content-wizard-single-post-script', 'ACW_I18N', [
        'resume_title'    => __('Request interrupted', 'ai-content-wizard'),
        'resume_msg'      => __('The server ran out of processing time. Do you want to continue generation from the last saved step?', 'ai-content-wizard'),
        'resume_continue' => __('Continue', 'ai-content-wizard'),
        'resume_cancel'   => __('Cancel', 'ai-content-wizard'),
        'error_generic'   => __('An error occurred while processing your request.', 'ai-content-wizard'),
    ]);

    $css_file_path = AI_CONTENT_WIZARD_PATH . 'css/single-post-styles.css';
    $css_version   = AI_CONTENT_WIZARD_VERSION . '-' . (file_exists($css_file_path) ? filemtime($css_file_path) : time());

    wp_enqueue_style(
        'ai-content-wizard-single-post-style',
        AI_CONTENT_WIZARD_URL . 'css/single-post-styles.css',
        array(),
        $css_version
    );
}
add_action('admin_enqueue_scripts', 'enqueue_single_post_script');

function ai_content_wizard_create_post_page() {
    $categories = get_categories(array('hide_empty' => false));

    $languages = [
        'en_US' => 'English (United States)',
        'ru_RU' => 'Русский',
        'uk'    => 'Українська',
    ];

    $current_language = get_locale();
    $default_language = array_key_exists($current_language, $languages) ? $current_language : 'en_US';

    $selected_post_status = ai_content_wizard_get_setting('ai_content_wizard_post_status', 'publish');

    if (isset($_POST['ai_content_wizard_generate_post'])) {
        error_log('[create-post-page.php] Начало обработки POST-запроса: ' . print_r($_POST, true));

        $category_id = isset($_POST['category']) ? intval($_POST['category']) : 0;
        $post_topic = isset($_POST['post_topic']) ? sanitize_text_field($_POST['post_topic']) : '';
        $selected_language = isset($_POST['language_select']) ? sanitize_text_field($_POST['language_select']) : $default_language;
        $single_post_additional_request = isset($_POST['single_post_additional_request']) ? sanitize_textarea_field($_POST['single_post_additional_request']) : '';

        $author = get_current_user_id();
        $selected_post_status = ai_content_wizard_get_setting('ai_content_wizard_post_status', 'publish');
        $post_mode = 'normal';

        ai_content_wizard_generate_single_post(
            $category_id,
            $post_topic,
            '',
            $selected_language,
            $selected_post_status,
            $single_post_additional_request,
            $author
        );
    }

    $text_model_names = [
        'gpt-3.5-turbo'         => 'GPT-3.5-turbo',
        'gpt-3.5-turbo-16k'     => 'GPT-3.5-turbo 16k',
        'gpt-4'                 => 'GPT-4',
        'gpt-4-turbo'           => 'GPT-4 Turbo',
        'gpt-4o-mini'           => 'GPT-4o mini',
        'gpt-5-mini'            => 'GPT-5 mini',
    ];

    $text_model = ai_content_wizard_get_setting('ai_content_wizard_text_model', 'gpt-4o-mini');
    $text_model_display_name = array_key_exists($text_model, $text_model_names) ? $text_model_names[$text_model] : 'Unknown Model';

    $current_settings = [
        'Format'            => ai_content_wizard_get_setting('ai_content_wizard_format', 'post'),
        'Text Length'       => ai_content_wizard_get_setting('ai_content_wizard_text_length', '500'),
        'Site Type'         => ai_content_wizard_get_setting('ai_content_wizard_site_type', 'store'),
        'Writing Style'     => ai_content_wizard_get_setting('ai_content_wizard_writing_style', 'formal'),
        'Target Audience'   => ai_content_wizard_get_setting('ai_content_wizard_target_audience', 'general'),
        'Region'            => ai_content_wizard_get_setting('ai_content_wizard_target_region', ''),
        'Post Status'       => ai_content_wizard_get_setting('ai_content_wizard_post_status', 'publish'),
        'Additional Images' => '0 (PRO)',
    ];

    $upgrade_url = 'https://aiwizard.buyreadysite.com/';
    ?>
    <div class="acw-page-header">
        <p class="acw-pill ghost"><?php esc_html_e('Single Post Studio', 'ai-content-wizard'); ?></p>
        <h1><?php esc_html_e('Create publication-ready drafts in one flow', 'ai-content-wizard'); ?></h1>
        <p class="acw-page-description"><?php esc_html_e('Lite mirrors the Pro dashboard so editors instantly see which premium workflow steps are locked.', 'ai-content-wizard'); ?></p>
        <div class="acw-hero-actions acw-page-actions">
            <a class="aiw-btn aiw-btn-primary" href="#ai_content_wizard_form"><?php esc_html_e('Start writing', 'ai-content-wizard'); ?></a>
            <a class="aiw-btn aiw-btn-ghost" target="_blank" rel="noopener" href="<?php echo esc_url($upgrade_url); ?>">
                <?php esc_html_e('Explore Pro workflow', 'ai-content-wizard'); ?>
            </a>
        </div>
    </div>

    <section class="acw-lite-hero acw-lite-hero-detached">
        <div class="acw-lite-hero-copy">
            <h2><?php esc_html_e('Standard briefing', 'ai-content-wizard'); ?></h2>
            <p><?php esc_html_e('Set topic and category - your saved AI profile keeps tone, SEO, and media aligned.', 'ai-content-wizard'); ?></p>
            <div class="acw-hero-quick-brief">
                <div class="acw-hero-quick-brief-item">
                    <strong><?php esc_html_e('Auto applies', 'ai-content-wizard'); ?></strong>
                    <span><?php esc_html_e('Saved tone, SEO preset, and media slots', 'ai-content-wizard'); ?></span>
                </div>
                <div class="acw-hero-quick-brief-item">
                    <strong><?php esc_html_e('Output package', 'ai-content-wizard'); ?></strong>
                    <span><?php esc_html_e('Draft + CTA checklist + summary', 'ai-content-wizard'); ?></span>
                </div>
            </div>
            <ul class="acw-hero-meta">
                <li><span><?php esc_html_e('Workflow', 'ai-content-wizard'); ?></span><strong><?php esc_html_e('Single Post', 'ai-content-wizard'); ?></strong></li>
                <li><span><?php esc_html_e('Model', 'ai-content-wizard'); ?></span><strong><?php echo esc_html(strtoupper($text_model_display_name)); ?></strong></li>
                <li><span><?php esc_html_e('Avg. ETA', 'ai-content-wizard'); ?></span><strong><?php esc_html_e('1-2 min', 'ai-content-wizard'); ?></strong></li>
            </ul>
        </div>
        <div class="acw-hero-side-card">
            <p class="acw-pill ghost">PRO+</p>
            <h3><?php esc_html_e('Why upgrade', 'ai-content-wizard'); ?></h3>
            <ul class="acw-pro-checklist">
                <li><?php esc_html_e('Long-form expert workflows', 'ai-content-wizard'); ?></li>
                <li><?php esc_html_e('Auto translations with multilingual sync', 'ai-content-wizard'); ?></li>
                <li><?php esc_html_e('Additional images and SEO blocks', 'ai-content-wizard'); ?></li>
            </ul>
            <a class="aiw-btn aiw-btn-ghost" target="_blank" rel="noopener" href="<?php echo esc_url($upgrade_url); ?>"><?php esc_html_e('View Pro plans', 'ai-content-wizard'); ?></a>
        </div>
    </section>

    <?php if ( class_exists( 'AiContentWizard_PromoPilot' ) ) : ?>
        <div class="acw-promopilot-placement">
            <?php AiContentWizard_PromoPilot::render_settings_banner(); ?>
        </div>
    <?php endif; ?>

    <div class="ai-content-wizard-page-container">
        <div class="mode-toggle">
            <div class="mode-help-icon mode-help-left" data-tooltip="<?php echo esc_attr__('Standard mode for everyday publishing tasks.', 'ai-content-wizard'); ?>">
                <svg width="28" height="28" viewBox="0 0 24 24" fill="currentColor"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"/></svg>
            </div>
            <div class="toggle-wrapper">
                <span class="toggle-label"><?php esc_html_e('Standard Mode', 'ai-content-wizard'); ?></span>
                <label class="switch is-locked">
                    <input type="checkbox" id="post_mode_toggle" name="post_mode" value="longread" disabled>
                    <span class="slider round"></span>
                </label>
                <span class="toggle-label longread-label">
                    <?php esc_html_e('Longread Mode', 'ai-content-wizard'); ?>
                    <span class="acw-pro-chip">PRO</span>
                </span>
            </div>
            <div class="mode-help-icon mode-help-right" data-tooltip="<?php echo esc_attr__('Pro longreads unlock 20–100k character expert productions.', 'ai-content-wizard'); ?>">
                <svg width="28" height="28" viewBox="0 0 24 24" fill="currentColor"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"/></svg>
            </div>
        </div>
        <p class="acw-mode-hint" id="acw-longread-lock"><?php esc_html_e('Longread Mode is available only in Pro. The toggle stays for interface parity.', 'ai-content-wizard'); ?></p>

        <div class="acw-primary-column">
            <div class="ai-content-wizard-form-container">
                <form method="post" action="" id="ai_content_wizard_form" class="acw-form-stack">
                    <div class="acw-form-card">
                        <div class="acw-form-card-head">
                            <p class="acw-pill ghost"><?php esc_html_e('Briefing', 'ai-content-wizard'); ?></p>
                            <h3><?php esc_html_e('Tell us what to create', 'ai-content-wizard'); ?></h3>
                            <p><?php esc_html_e('Fill only the essentials - your global AI profile keeps tone, SEO, and CTAs consistent.', 'ai-content-wizard'); ?></p>
                        </div>
                        <div class="acw-form-card-body">
                            <div class="acw-form-row acw-two-columns">
                                <div class="acw-form-field">
                                    <label for="category"><?php esc_html_e('Select Category:', 'ai-content-wizard'); ?> <span class="required">*</span><span class="info-icon dashicons dashicons-editor-help" onclick="showPopup('CategoryInfo')"></span></label>
                                    <select id="category" name="category" required>
                                        <option value=""><?php esc_html_e('Choose Category', 'ai-content-wizard'); ?></option>
                                        <?php foreach ($categories as $category) : ?>
                                            <option value="<?php echo esc_attr($category->term_id); ?>"><?php echo esc_html($category->name); ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                <div class="acw-form-field">
                                    <label for="language_select"><?php esc_html_e('Language:', 'ai-content-wizard'); ?><span class="info-icon dashicons dashicons-editor-help" onclick="showPopup('LanguageInfo')"></span></label>
                                    <select id="language_select" name="language_select">
                                        <?php foreach ($languages as $locale => $language_name) : ?>
                                            <option value="<?php echo esc_attr($locale); ?>" <?php selected($locale, $default_language); ?>><?php echo esc_html($language_name); ?></option>
                                        <?php endforeach; ?>
                                        <optgroup label="<?php esc_html_e('Available in Pro', 'ai-content-wizard'); ?>">
                                            <?php
                                            $pro_languages = [
                                                'es_ES' => 'Español',
                                                'fr_FR' => 'Français',
                                                'de_DE' => 'Deutsch',
                                                'it_IT' => 'Italiano',
                                                'pt_PT' => 'Português',
                                                'ja' => '日本語',
                                                'zh_CN' => '中文 (中国)',
                                                'he_IL' => 'עברית',
                                                'sq_AL' => 'Shqip',
                                                'hr_HR' => 'Hrvatski',
                                                'sr_RS' => 'Српски',
                                                'mk_MK' => 'Македонски',
                                                'et_EE' => 'Eesti',
                                                'pl_PL' => 'Polski',
                                                'bg_BG' => 'Български',
                                                'tr_TR' => 'Türkçe',
                                                'ro_RO' => 'Română',
                                                'el_GR' => 'Ελληνικά',
                                                'cs_CZ' => 'Čeština',
                                                'sk_SK' => 'Slovenčina',
                                                'hu_HU' => 'Magyar',
                                                'lt_LT' => 'Lietuvių',
                                                'lv_LV' => 'Latviešu',
                                                'fi_FI' => 'Suomi',
                                                'sv_SE' => 'Svenska',
                                                'no_NO' => 'Norsk',
                                                'da_DK' => 'Dansk',
                                                'nl_NL' => 'Nederlands',
                                                'sl_SI' => 'Slovenščina',
                                                'bs_BA' => 'Bosanski',
                                                'ka_GE' => 'ქართული',
                                                'hy_AM' => 'Հայերեն',
                                                'az_AZ' => 'Azərbaycan',
                                                'kk_KZ' => 'Қазақ тілі',
                                                'uz_UZ' => 'Oʻzbek',
                                                'mn_MN' => 'Монгол',
                                                'vi_VN' => 'Tiếng Việt',
                                                'th_TH' => 'ไทย',
                                                'ko_KR' => '한국어',
                                            ];
                                            foreach ($pro_languages as $locale => $language_name) {
                                                echo '<option value="' . esc_attr($locale) . '" disabled>' . esc_html($language_name . ' (PRO)') . '</option>';
                                            }
                                            ?>
                                        </optgroup>
                                    </select>
                                </div>
                            </div>

                            <div class="acw-form-field">
                                <label for="post_topic"><?php esc_html_e('Post Topic:', 'ai-content-wizard'); ?> <span class="required">*</span><span class="info-icon dashicons dashicons-editor-help" onclick="showPopup('PostTopicInfo')"></span></label>
                                <input type="text" id="post_topic" name="post_topic" placeholder="<?php esc_html_e('Example: SEO Tips for Beginners', 'ai-content-wizard'); ?>" required>
                            </div>
                        </div>
                    </div>

                    <div class="acw-form-card acw-locked-card">
                        <div class="acw-form-card-head">
                            <p class="acw-pill ghost"><?php esc_html_e('Pro workflow', 'ai-content-wizard'); ?></p>
                            <h3><?php esc_html_e('Advanced instructions stay visible', 'ai-content-wizard'); ?></h3>
                            <p><?php esc_html_e('Lite mirrors the Pro flow so teams know exactly which steps unlock after upgrading.', 'ai-content-wizard'); ?></p>
                        </div>
                        <div class="acw-form-card-body">
                            <div class="acw-form-field acw-locked-field">
                                <label for="single_post_additional_request"><?php esc_html_e('Additional Request (PRO):', 'ai-content-wizard'); ?><span class="info-icon dashicons dashicons-editor-help" onclick="showPopup('AdditionalRequestInfo')"></span></label>
                                <textarea id="single_post_additional_request" name="single_post_additional_request" rows="2" placeholder="<?php esc_html_e('Add structured briefs, FAQ, CTA blocks...', 'ai-content-wizard'); ?>" disabled></textarea>
                                <span class="acw-locked-note"><?php esc_html_e('Unlocks with Pro upgrade.', 'ai-content-wizard'); ?></span>
                            </div>

                            <div class="acw-form-field acw-locked-field">
                                <label for="related_articles"><?php esc_html_e('Related Articles (PRO):', 'ai-content-wizard'); ?><span class="info-icon dashicons dashicons-editor-help" onclick="showPopup('RelatedArticlesInfo')"></span></label>
                                <input type="text" id="related_articles" name="related_articles" placeholder="<?php esc_html_e('Example: https://example.com/related-article', 'ai-content-wizard'); ?>" disabled>
                            </div>

                            <div class="acw-form-row acw-two-columns">
                                <div class="acw-form-field acw-locked-field">
                                    <label for="single_post_status"><?php esc_html_e('Post Status (PRO):', 'ai-content-wizard'); ?><span class="info-icon dashicons dashicons-editor-help" onclick="showPopup('StatusInfo')"></span></label>
                                    <select id="single_post_status" name="single_post_status" disabled>
                                        <option value="publish" <?php selected($selected_post_status, 'publish'); ?>><?php esc_html_e('Published', 'ai-content-wizard'); ?></option>
                                        <option value="draft" <?php selected($selected_post_status, 'draft'); ?>><?php esc_html_e('Draft', 'ai-content-wizard'); ?></option>
                                        <option value="pending" <?php selected($selected_post_status, 'pending'); ?>><?php esc_html_e('Pending Review', 'ai-content-wizard'); ?></option>
                                        <option value="private" <?php selected($selected_post_status, 'private'); ?>><?php esc_html_e('Private', 'ai-content-wizard'); ?></option>
                                        <option value="future" <?php selected($selected_post_status, 'future'); ?>><?php esc_html_e('Scheduled', 'ai-content-wizard'); ?></option>
                                        <option value="trash" <?php selected($selected_post_status, 'trash'); ?>><?php esc_html_e('Trash', 'ai-content-wizard'); ?></option>
                                    </select>
                                </div>
                                <div class="acw-form-field acw-locked-field">
                                    <label><?php esc_html_e('Auto Translation', 'ai-content-wizard'); ?></label>
                                    <div class="acw-locked-note"><?php esc_html_e('Auto translation with Polylang/WPML sync is available in Pro.', 'ai-content-wizard'); ?></div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="acw-upgrade-banner">
                        <div>
                            <strong><?php esc_html_e('Need custom instructions and longer posts?', 'ai-content-wizard'); ?></strong>
                            <p><?php esc_html_e('Pro unlocks extra languages, background queues, and extended publishing statuses.', 'ai-content-wizard'); ?></p>
                        </div>
                        <a class="aiw-btn aiw-btn-primary" target="_blank" rel="noopener" href="<?php echo esc_url($upgrade_url); ?>"><?php esc_html_e('Unlock Pro', 'ai-content-wizard'); ?></a>
                    </div>

                    <div class="acw-form-cta">
                        <button type="submit" name="ai_content_wizard_generate_post" class="acw-generate-button">
                            <span class="acw-generate-icon" aria-hidden="true">
                                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="28" height="28" fill="none" stroke="currentColor" stroke-width="2">
                                    <path d="M12 5v9" />
                                    <path d="M5 12h14" />
                                    <circle cx="12" cy="12" r="9" />
                                </svg>
                            </span>
                            <span class="acw-generate-copy">
                                <strong><?php esc_html_e('Generate Post', 'ai-content-wizard'); ?></strong>
                                <small><?php esc_html_e('Average turnaround 1-2 minutes', 'ai-content-wizard'); ?></small>
                            </span>
                            <span class="acw-generate-arrow" aria-hidden="true">
                                <span class="dashicons dashicons-arrow-right-alt"></span>
                            </span>
                        </button>
                        <p class="acw-cta-note"><?php esc_html_e('The post saves as draft if publishing fails, so nothing gets lost mid-flow.', 'ai-content-wizard'); ?></p>
                    </div>

                    <div id="acw-pre-submit" class="acw-pre-submit">
                        <ul id="acw-pre-submit-list" class="acw-pre-submit-list">
                            <li id="acw-check-category" class="acw-check fail">
                                <span class="dashicons dashicons-no"></span>
                                <span class="acw-check-text"><?php echo esc_html__('Select a category', 'ai-content-wizard'); ?></span>
                            </li>
                            <li id="acw-check-topic" class="acw-check fail">
                                <span class="dashicons dashicons-no"></span>
                                <span class="acw-check-text"><?php echo esc_html__('Enter a topic', 'ai-content-wizard'); ?></span>
                            </li>
                        </ul>
                        <div id="acw-timeout-warning" class="acw-timeout-warning">
                            <?php
                            $sys_url = admin_url('admin.php?page=ai_content_wizard_system_check');
                            printf(
                                /* translators: %s is a link to the diagnostics page. */
                                esc_html__('If your server timeout is low, generation may not complete in one go. You can resume it. %s.', 'ai-content-wizard'),
                                '<a href="' . esc_url($sys_url) . '" target="_blank" rel="noopener">' . esc_html__('Open diagnostics', 'ai-content-wizard') . '</a>'
                            );
                            ?>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <div class="ai-content-wizard-settings-container">
            <div class="acw-settings-card">
                <div class="acw-settings-head">
                    <p class="acw-pill ghost"><?php esc_html_e('Global settings', 'ai-content-wizard'); ?></p>
                    <h3><?php esc_html_e('Current AI profile', 'ai-content-wizard'); ?></h3>
                    <div class="acw-settings-meta">
                        <span class="acw-model-chip"><?php echo esc_html(strtoupper($text_model_display_name)); ?></span>
                        <a href="<?php echo esc_url(admin_url('admin.php?page=ai_content_wizard_api_settings')); ?>" class="acw-inline-link"><?php esc_html_e('Change model', 'ai-content-wizard'); ?></a>
                        <span class="info-icon dashicons dashicons-editor-help" onclick="showPopup('GlobalInfo')"></span>
                    </div>
                </div>
                <div class="acw-settings-description">
                    <p><?php echo esc_html__('Standard mode uses the profile from Settings → Global AI Profile.', 'ai-content-wizard'); ?></p>
                </div>
                <div class="settings-display">
                    <?php foreach ($current_settings as $setting_name => $setting_value) : ?>
                        <div class="setting-item">
                            <span class="setting-name"><strong><?php echo esc_html($setting_name); ?>:</strong> <?php echo esc_html($setting_value); ?></span>
                            <?php if ($setting_name === 'Additional Images' || $setting_name === 'Region') : ?>
                                <span class="pro-badge">PRO</span>
                            <?php endif; ?>
                            <a href="<?php echo esc_url(admin_url('admin.php?page=ai_content_wizard_settings')); ?>" class="edit-setting-link"><?php esc_html_e('Edit', 'ai-content-wizard'); ?></a>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>

            <div class="acw-upgrade-card">
                <h3><?php esc_html_e('Background generation queue', 'ai-content-wizard'); ?></h3>
                <p><?php esc_html_e('Pro processes batches in the background and pings you when drafts are ready.', 'ai-content-wizard'); ?></p>
                <a class="aiw-btn aiw-btn-primary" target="_blank" rel="noopener" href="<?php echo esc_url($upgrade_url); ?>"><?php esc_html_e('Upgrade to Pro', 'ai-content-wizard'); ?></a>
            </div>

            <div id="costCard" class="acw-cost-card">
                <span class="label"><?php esc_html_e('Estimated cost', 'ai-content-wizard'); ?></span>
                <strong id="estimated_cost_value">$0.725</strong>
                <p><?php esc_html_e('Live estimate based on your current text length and provider.', 'ai-content-wizard'); ?></p>
            </div>
        </div>
    </div>

    <div id="acw-resume-modal" class="acw-modal-overlay" style="display:none;">
        <div class="acw-modal" role="dialog" aria-modal="true" aria-labelledby="acw-resume-title">
            <div class="acw-modal-header">
                <div class="acw-modal-icon" aria-hidden="true">
                    <svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M12 9v4"></path>
                        <circle cx="12" cy="17" r="1"></circle>
                        <path d="M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"></path>
                    </svg>
                </div>
                <h3 id="acw-resume-title" class="acw-modal-title"><?php echo esc_html__('Request interrupted', 'ai-content-wizard'); ?></h3>
                <button type="button" class="acw-modal-close" aria-label="Close" onclick="var b=document.getElementById('acw-resume-cancel'); if(b) b.click();">×</button>
            </div>
            <div class="acw-modal-body" id="acw-resume-body">
                <p class="acw-modal-text"><?php echo esc_html__('The server ran out of processing time. Do you want to continue generation from the last saved step?', 'ai-content-wizard'); ?></p>
                <div class="acw-error-details" style="display:none;"></div>
            </div>
            <div class="acw-modal-actions">
                <button type="button" class="button button-primary acw-primary" id="acw-resume-continue"><?php echo esc_html__('Continue', 'ai-content-wizard'); ?></button>
                <button type="button" class="button acw-secondary" id="acw-resume-cancel"><?php echo esc_html__('Cancel', 'ai-content-wizard'); ?></button>
            </div>
        </div>
    </div>

    <div id="preload" style="display:none;">
        <div class="acw-generation-modal">
            <button type="button" class="acw-modal-close-btn" onclick="document.getElementById('preload').style.display='none';" aria-label="Close">
                <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <line x1="18" y1="6" x2="6" y2="18"/>
                    <line x1="6" y1="6" x2="18" y2="18"/>
                </svg>
            </button>

            <div class="acw-gen-header">
                <div class="acw-modern-spinner" aria-hidden="true">
                    <div class="acw-spinner-ring"></div>
                    <div class="acw-spinner-ring"></div>
                    <div class="acw-spinner-core">
                        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
                            <path d="M12 2v4m0 12v4M4.93 4.93l2.83 2.83m8.48 8.48l2.83 2.83M2 12h4m12 0h4M4.93 19.07l2.83-2.83m8.48-8.48l2.83-2.83"/>
                        </svg>
                    </div>
                </div>
                <h2 id="generating_content_text" class="acw-gen-title"><?php esc_html_e('Generating Content...', 'ai-content-wizard'); ?></h2>
                <p class="acw-gen-subtitle" id="tips"></p>
            </div>

            <div id="progress_stage_container" class="acw-stages-wrapper">
                <div class="acw-current-stage">
                    <span id="current_stage_text" class="acw-stage-name"><?php esc_html_e('Keyword Generation', 'ai-content-wizard'); ?></span>
                </div>
                <div id="progress_bar" class="acw-progress-stages">
                    <div class="stage pending" data-tooltip="<?php esc_attr_e('Keyword Generation', 'ai-content-wizard'); ?>">
                        <div class="tooltip-icon">
                            <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <polyline points="6 9 12 15 18 9" />
                            </svg>
                        </div>
                        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <circle cx="11" cy="11" r="8" />
                            <line x1="21" y1="21" x2="16.65" y2="16.65" />
                        </svg>
                    </div>
                    <div class="stage pending" data-tooltip="<?php esc_attr_e('Content Creation', 'ai-content-wizard'); ?>">
                        <div class="tooltip-icon">
                            <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <polyline points="6 9 12 15 18 9" />
                            </svg>
                        </div>
                        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <path d="M12 20h9" />
                            <path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z" />
                        </svg>
                    </div>
                    <div class="stage pending" data-tooltip="<?php esc_attr_e('Additional Images', 'ai-content-wizard'); ?>">
                        <div class="tooltip-icon">
                            <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <polyline points="6 9 12 15 18 9" />
                            </svg>
                        </div>
                        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <rect x="3" y="3" width="18" height="18" rx="2" ry="2" />
                            <circle cx="8.5" cy="8.5" r="1.5" />
                            <polyline points="21 15 16 10 5 21" />
                        </svg>
                    </div>
                    <div class="stage pending" data-tooltip="<?php esc_attr_e('Featured Image', 'ai-content-wizard'); ?>">
                        <div class="tooltip-icon">
                            <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <polyline points="6 9 12 15 18 9" />
                            </svg>
                        </div>
                        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2" />
                        </svg>
                    </div>
                    <div class="stage pending" data-tooltip="<?php esc_attr_e('SEO Optimization', 'ai-content-wizard'); ?>">
                        <div class="tooltip-icon">
                            <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <polyline points="6 9 12 15 18 9" />
                            </svg>
                        </div>
                        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <path d="M4 15s1-1 4-1 5 2 8 2 4-1 4-1V3s-1 1-4 1-5-2-8-2-4 1-4 1z" />
                            <line x1="4" y1="22" x2="4" y2="15" />
                        </svg>
                    </div>
                    <div class="stage pending" data-tooltip="<?php esc_attr_e('ALT Texts', 'ai-content-wizard'); ?>">
                        <div class="tooltip-icon">
                            <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <polyline points="6 9 12 15 18 9" />
                            </svg>
                        </div>
                        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <path d="M21 4H3v16h18V4zM9 9h6M9 13h3" />
                        </svg>
                    </div>
                    <div class="stage pending translation-stage" data-tooltip="<?php esc_attr_e('Translating to Other Languages', 'ai-content-wizard'); ?>" style="display:none;">
                        <div class="tooltip-icon">
                            <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <polyline points="6 9 12 15 18 9" />
                            </svg>
                        </div>
                        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <circle cx="12" cy="12" r="10" />
                            <line x1="2" y1="12" x2="22" y2="12" />
                            <path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z" />
                        </svg>
                    </div>
                    <div class="stage pending" data-tooltip="<?php esc_attr_e('Done', 'ai-content-wizard'); ?>">
                        <div class="tooltip-icon">
                            <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <polyline points="6 9 12 15 18 9" />
                            </svg>
                        </div>
                        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <polyline points="20 6 9 17 4 12" />
                        </svg>
                    </div>
                </div>
            </div>

            <div class="acw-translation-panel" id="translation_progress_panel" style="display:none;">
                <div class="acw-trans-header">
                    <span class="acw-trans-icon">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <circle cx="12" cy="12" r="10"/>
                            <line x1="2" y1="12" x2="22" y2="12"/>
                            <path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z"/>
                        </svg>
                    </span>
                    <span class="acw-trans-title"><?php esc_html_e('Translations', 'ai-content-wizard'); ?></span>
                    <span class="acw-trans-current" data-role="current-language">—</span>
                </div>
                <div class="acw-trans-progress">
                    <div class="acw-trans-fill" style="width:0%"></div>
                </div>
                <div class="acw-trans-meta">
                    <span class="acw-trans-count"><span data-role="completed">0</span> / <span data-role="total">0</span></span>
                    <span class="acw-trans-status" data-role="status-text"></span>
                </div>
            </div>

            <div id="generation_timer" class="acw-gen-timer">
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <circle cx="12" cy="12" r="10"/>
                    <polyline points="12 6 12 12 16 14"/>
                </svg>
                <span id="timer_label"><?php esc_html_e('Time Elapsed', 'ai-content-wizard'); ?>:</span>
                <span id="timer_display">00:00</span>
            </div>

            <div id="post_generation_buttons" class="acw-gen-actions" style="display:none;">
                <a href="#" id="view_post" class="acw-btn acw-btn-primary">
                    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/>
                        <circle cx="12" cy="12" r="3"/>
                    </svg>
                    <?php esc_html_e('View Post', 'ai-content-wizard'); ?>
                </a>
                <button id="generate_another_post" class="acw-btn acw-btn-secondary">
                    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M21.5 2v6h-6M2.5 22v-6h6M2 11.5a10 10 0 0 1 18.8-4.3M22 12.5a10 10 0 0 1-18.8 4.2"/>
                    </svg>
                    <?php esc_html_e('Generate Another', 'ai-content-wizard'); ?>
                </button>
            </div>
        </div>
    </div>

        <style>
                  /* ===== ACW Resume Modal (modern) ===== */
                  .acw-modal-overlay{position:fixed; inset:0; z-index:100000; background:rgba(0,0,0,.45); backdrop-filter:saturate(120%) blur(4px);} 
                  .acw-modal{position:absolute; left:50%; top:50%; transform:translate(-50%,-50%); width:min(720px, calc(100% - 40px)); border-radius:16px; overflow:hidden; background:linear-gradient(180deg, #ffffff 0%, #fbfbfb 100%); box-shadow:0 20px 60px rgba(0,0,0,.25), 0 2px 10px rgba(0,0,0,.08); animation:acwFadeUp .18s ease-out;} 
                  .acw-modal-header{display:flex; align-items:center; gap:12px; padding:18px 20px; background:linear-gradient(135deg,#1f2937 0%,#111827 100%); color:#fff;} 
                  .acw-modal-icon{display:flex; align-items:center; justify-content:center; width:36px; height:36px; border-radius:10px; background:linear-gradient(135deg,#22d3ee,#14b8a6);} 
                  .acw-modal-title{margin:0; font-size:18px; font-weight:700; letter-spacing:.2px;} 
                  .acw-modal-close{margin-left:auto; width:36px; height:36px; border:none; background:transparent; color:#fff; font-size:24px; line-height:1; cursor:pointer; opacity:.85;} 
                  .acw-modal-close:hover{opacity:1;} 
                  .acw-modal-body{padding:20px 22px; color:#1d2327;} 
                  .acw-modal-text{margin:0 0 10px 0; font-size:15px; line-height:1.6;} 
                  .acw-error-details{margin-top:10px; padding:10px 12px; border-radius:10px; background:#fff7ed; color:#9a3412; border:1px solid #fed7aa; font-size:12px; line-height:1.5; white-space:pre-wrap; word-break:break-word;} 
                  .acw-modal-actions{display:flex; gap:10px; justify-content:flex-end; padding:16px 20px; background:#f8fafc; border-top:1px solid #eef2f7;} 
                  .acw-primary{min-width:120px; height:36px; border-radius:8px; box-shadow:0 6px 16px rgba(16,185,129,.25); background:linear-gradient(135deg,#10b981,#059669)!important; border:none; font-weight:600;} 
                  .acw-primary:hover{filter:brightness(1.05);} 
                  .acw-secondary{min-width:110px; height:36px; border-radius:8px; background:#ffffff; border:1px solid #e5e7eb; font-weight:500;} 
                  .acw-secondary:hover{background:#f8fafc;} 
                  @keyframes acwFadeUp{from{opacity:0; transform:translate(-50%,-54%);} to{opacity:1; transform:translate(-50%,-50%);} }
                  @media (max-width: 480px){ .acw-modal{width:calc(100% - 24px);} .acw-modal-title{font-size:16px;} }

                  /* checks colors */
                  .acw-check.fail .dashicons{color:#dc2626}
                                    .acw-check.ok .dashicons{color:#16a34a}
        </style>

                                <!-- Попапы с информацией/подсказками -->
                <div id="ai-content-wizard-tooltips" style="display: none;">
                    <!-- Подсказка для 'Category' -->
                    <div id="CategoryInfo" class="ai-content-wizard-tooltip">
                        <h2><?php esc_html_e('Select Category', 'ai-content-wizard'); ?></h2>
                        <p>
                            <?php esc_html_e('Choosing the correct category for your post is crucial for several reasons:', 'ai-content-wizard'); ?>
                        </p>
                        <ul>
                            <li>
                                <?php esc_html_e('Organization: Categories help organize your content efficiently. A well-categorized post is easier for users to find and navigate.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <?php esc_html_e('SEO Benefits: Proper categorization can improve your website’s SEO. Search engines better understand the structure of your site and the relevance of your content, potentially leading to better rankings.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <?php esc_html_e('User Experience: Categories enhance the user experience by grouping similar content together, making it more accessible for your audience to discover related posts.', 'ai-content-wizard'); ?>
                            </li>
                        </ul>
                        <p>
                            <?php esc_html_e('Select the category that best fits the topic of your post. If necessary, you can create new categories to suit your content.', 'ai-content-wizard'); ?>
                        </p>
                    </div>

                    <!-- Подсказка для 'Post Topic' -->
                    <div id="PostTopicInfo" class="ai-content-wizard-tooltip">
                        <h2><?php esc_html_e('Post Topic', 'ai-content-wizard'); ?></h2>
                        <p>
                            <?php esc_html_e('The topic of your post is crucial for guiding the AI in generating relevant content. A well-defined topic helps in creating focused and coherent content.', 'ai-content-wizard'); ?>
                        </p>
                        <ul>
                            <li>
                                <?php esc_html_e('Clarity: A clear and concise topic allows the AI to understand the subject matter better, resulting in more accurate and relevant content generation.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <?php esc_html_e('Audience Engagement: A specific topic is more likely to engage your target audience, as it addresses their interests and needs more directly.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <?php esc_html_e('SEO Optimization: A focused topic contributes to better SEO, as it allows for the use of targeted keywords and phrases.', 'ai-content-wizard'); ?>
                            </li>
                        </ul>
                        <p>
                            <?php esc_html_e('Ensure to provide a specific and relevant topic for each post to maximize the effectiveness of AI-generated content.', 'ai-content-wizard'); ?>
                        </p>
                    </div>

                    <!-- Подсказка для 'Related Articles' -->
                    <div id="RelatedArticlesInfo" class="ai-content-wizard-tooltip">
                        <h2><?php esc_html_e('Related Articles', 'ai-content-wizard'); ?></h2>
                        <p>
                            <?php esc_html_e('Including related articles can enhance the context and depth of your AI-generated content. This field is optional but can be used effectively for several purposes:', 'ai-content-wizard'); ?>
                        </p>
                        <ul>
                            <li>
                                <?php esc_html_e('Continuation of a Topic: Link to an article you wish to continue or expand upon. This helps the AI to maintain thematic consistency and relevance.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <?php esc_html_e('Reference Material: Provide links to articles that contain background information or data that could be beneficial for creating your new post.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <?php esc_html_e('Enhancing SEO: Linking to related articles can improve SEO by creating a network of relevant content, increasing the likelihood of higher search engine rankings.', 'ai-content-wizard'); ?>
                            </li>
                        </ul>
                        <p>
                            <?php esc_html_e('Simply enter the URLs of related articles in this field. The AI will use this information to create content that is more aligned with the given references.', 'ai-content-wizard'); ?>
                        </p>
                    </div>

                    <!-- Подсказка для 'Additional Request' -->
                    <div id="AdditionalRequestInfo" class="ai-content-wizard-tooltip">
                        <h2><?php esc_html_e('Additional Request', 'ai-content-wizard'); ?></h2>
                        <p>
                            <?php esc_html_e('Use this field to provide any extra instructions or preferences for the AI to consider while generating content. Examples include:', 'ai-content-wizard'); ?>
                        </p>
                        <ul>
                            <li>
                                <?php esc_html_e('Adding a FAQ section at the end of the article.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <?php esc_html_e('Including specific keyword phrases more frequently.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <?php esc_html_e('Tailoring the tone or style to match a particular brand voice.', 'ai-content-wizard'); ?>
                            </li>
                        </ul>
                        <p>
                            <?php esc_html_e('Providing clear additional requests can help the AI generate more targeted and useful content for your specific needs.', 'ai-content-wizard'); ?>
                        </p>
                    </div>

                    <!-- Подсказка для 'Language Selection' -->
                    <div id="LanguageInfo" class="ai-content-wizard-tooltip">
                        <h2><?php esc_html_e('Language Selection', 'ai-content-wizard'); ?></h2>
                        <p>
                            <?php esc_html_e('Choosing the correct language is vital for generating content that resonates with your target audience. It ensures that the AI understands and generates content in the selected language:', 'ai-content-wizard'); ?>
                        </p>
                        <ul>
                            <li>
                                <?php esc_html_e('Target Audience: Select the language that your audience speaks and understands. This enhances readability and engagement.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <?php esc_html_e('Content Relevance: Content created in the appropriate language is more likely to be relevant and appealing to users.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <?php esc_html_e('SEO Optimization: Content in the correct language can improve SEO for your target region, enhancing visibility on search engines.', 'ai-content-wizard'); ?>
                            </li>
                        </ul>
                        <p>
                            <?php esc_html_e('The language selection influences how the AI interprets prompts and generates content. Be sure to choose the language that aligns with your content strategy and audience preferences.', 'ai-content-wizard'); ?>
                        </p>
                    </div>

                    <!-- Подсказка для 'Post Status' -->
                    <div id="StatusInfo" class="ai-content-wizard-tooltip">
                        <h2><?php esc_html_e('Post Status', 'ai-content-wizard'); ?></h2>
                        <p>
                            <?php esc_html_e('Selecting the appropriate status for your post is important for managing your content workflow and visibility:', 'ai-content-wizard'); ?>
                        </p>
                        <ul>
                            <li>
                                <strong><?php esc_html_e('Published:', 'ai-content-wizard'); ?></strong>
                                <?php esc_html_e('The post will be immediately visible on your website.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <strong><?php esc_html_e('Draft:', 'ai-content-wizard'); ?></strong>
                                <?php esc_html_e('Save your post as a draft, making it unavailable to the public until you decide to publish it.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <strong><?php esc_html_e('Pending Review:', 'ai-content-wizard'); ?></strong>
                                <?php esc_html_e('Mark the post for review by an editor or administrator before it goes live.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <strong><?php esc_html_e('Private:', 'ai-content-wizard'); ?></strong>
                                <?php esc_html_e('Make the post visible only to yourself and selected users.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <strong><?php esc_html_e('Scheduled:', 'ai-content-wizard'); ?></strong>
                                <?php esc_html_e('Set the post to be published automatically at a future date and time.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <strong><?php esc_html_e('Trash:', 'ai-content-wizard'); ?></strong>
                                <?php esc_html_e('Move the post to the trash bin. It can be restored or permanently deleted later.', 'ai-content-wizard'); ?>
                            </li>
                        </ul>
                        <p>
                            <?php esc_html_e('Choose the status that best suits your current need for the post. This helps in organizing and scheduling your content effectively.', 'ai-content-wizard'); ?>
                        </p>
                    </div>

                    <!-- Подсказка для 'Global Settings' -->
                    <div id="GlobalInfo" class="ai-content-wizard-tooltip">
                        <h2><?php esc_html_e('Global Settings', 'ai-content-wizard'); ?></h2>
                        <p>
                            <?php esc_html_e('Global settings play a crucial role in influencing the AI-generated content. Before starting your content generation, it’s important to review these settings to ensure they align with your requirements:', 'ai-content-wizard'); ?>
                        </p>
                        <ul>
                            <li>
                                <strong><?php esc_html_e('Format:', 'ai-content-wizard'); ?></strong>
                                <?php esc_html_e('Defines the structure of the generated content. Different formats like posts, recipes, or reviews guide the AI to create content in specific styles.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <strong><?php esc_html_e('Text Length:', 'ai-content-wizard'); ?></strong>
                                <?php esc_html_e('Determines the length of the generated content. It influences how detailed or concise the content will be.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <strong><?php esc_html_e('Site Type:', 'ai-content-wizard'); ?></strong>
                                <?php esc_html_e('Indicates the nature of your site, such as a store, blog, or educational platform, guiding the AI to tailor content accordingly.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <strong><?php esc_html_e('Writing Style:', 'ai-content-wizard'); ?></strong>
                                <?php esc_html_e('Sets the tone and style of writing, from formal to informal, persuasive to narrative.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <strong><?php esc_html_e('Target Audience:', 'ai-content-wizard'); ?></strong>
                                <?php esc_html_e('Specifies the intended audience, ensuring that the content is appropriate and engaging for them.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <strong><?php esc_html_e('Custom Theme:', 'ai-content-wizard'); ?></strong>
                                <?php esc_html_e('Allows for focusing on a specific theme or subject matter, making the content more relevant.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <strong><?php esc_html_e('Additional Requests:', 'ai-content-wizard'); ?></strong>
                                <?php esc_html_e('Enables you to provide extra instructions or preferences for the AI to consider.', 'ai-content-wizard'); ?>
                            </li>
                            <li>
                                <strong><?php esc_html_e('Region:', 'ai-content-wizard'); ?></strong>
                                <?php esc_html_e('Tailors the content to a specific geographical area, enhancing its relevance to local audiences.', 'ai-content-wizard'); ?>
                            </li>
                        </ul>
                        <p>
                            <?php esc_html_e('Reviewing and adjusting these settings ensures that the AI understands your content goals and generates material that meets your specific needs.', 'ai-content-wizard'); ?>
                        </p>
                    </div>
                </div>
				
            </form>
        </div><!-- /ai-content-wizard-form-container -->
    
        <!-- Popup для объяснения, почему Longread Mode недоступен -->
        <div id="longread-mode-popup" class="ai-content-wizard-tooltip" style="display: none;">
            <h2><?php esc_html_e('Longread Mode Unavailable', 'ai-content-wizard'); ?></h2>
            <p>
                <?php esc_html_e('Longread mode is available only in the Pro version.', 'ai-content-wizard'); ?>
            </p>
        </div>
    
        <?php
        // Массив подсказок для модального окна
        $tips = [
            __('Remember, the best titles are both informative and intriguing!', 'ai-content-wizard'),
            __('Using active voice in your articles can make them more engaging.', 'ai-content-wizard'),
            __('Lists are like potato chips for readers: They can never have just one.', 'ai-content-wizard'),
            __('A pinch of humor in your content keeps the boredom at bay.', 'ai-content-wizard'),
            __('SEO tip: Sprinkle your keywords like you’re seasoning a gourmet dish - just enough to tantalize.', 'ai-content-wizard'),
            __('Ever tried writing with your non-dominant hand? That’s how readers feel about jargon. Keep it simple.', 'ai-content-wizard'),
            __('Breaking news: Readers love short paragraphs. Keep yours snappy!', 'ai-content-wizard'),
            __('An image can speak a thousand words, but please, no selfies with your articles.', 'ai-content-wizard'),
            __('Call-to-action is your secret handshake with readers. Make sure it’s firm!', 'ai-content-wizard'),
            __('Who said facts have to be boring? Spice them up with a dash of creativity.', 'ai-content-wizard'),
            __('Your conclusion is your mic drop. Make it epic.', 'ai-content-wizard'),
            __('If at first you don’t succeed, delete all evidence you ever tried. Or just edit ruthlessly.', 'ai-content-wizard'),
            __('Articles without images are like ice cream without toppings. Functional, but why would you?', 'ai-content-wizard'),
            __('Engagement tip: End with a question, or a cliffhanger. Will they or won’t they click?', 'ai-content-wizard')
        ];

        $tips_json = wp_json_encode($tips);
        ?>

        <script type="text/javascript">
        // Передаем массив подсказок в JavaScript
        var tips = <?php echo $tips_json; ?>;
        var lastTipIndex = -1;
        var longreadLastTipIndex = -1;
        </script>
    </div><!-- /ai-content-wizard-page-container -->
    <?php
    echo '<script type="text/javascript">
        var globalSettings = ' . wp_json_encode($current_settings) . ';
    </script>';

    echo '<script>
    (function($){
        function setCheck($li, ok){
            var $icon = $li.find(".dashicons");
            $li.removeClass("ok fail").addClass(ok ? "ok" : "fail");
            if(ok){ $icon.removeClass("dashicons-no").addClass("dashicons-yes"); }
            else   { $icon.removeClass("dashicons-yes").addClass("dashicons-no"); }
        }

        function updatePreSubmit(){
            var hasCat = !!$("#category").val();
            var hasTopic = !!($.trim($("#post_topic").val() || ""));
            setCheck($("#acw-check-category"), hasCat);
            setCheck($("#acw-check-topic"), hasTopic);
        }

        $(document).on("input change", "#category,#post_topic", updatePreSubmit);
        $(document).on("submit", "#ai_content_wizard_form", function(e){
            updatePreSubmit();
            var anyFail = $("#acw-pre-submit .acw-check.fail").length > 0;
            if(anyFail){
                e.preventDefault();
                try {
                    $("html, body").animate({ scrollTop: $("#acw-pre-submit").offset().top - 80 }, 200);
                } catch(err) {}
                return false;
            }
        });

        $(function(){ updatePreSubmit(); });
    })(jQuery);
    </script>';
}

add_action('wp_ajax_get_category_settings', 'ai_content_wizard_get_category_settings');
function ai_content_wizard_get_category_settings() {
    $category_id = 'category-' . intval($_POST['category_id']);
    $settings = [
        'format' => ai_content_wizard_get_category_setting($category_id, 'format', ai_content_wizard_get_setting('ai_content_wizard_format')),
        'textLength' => ai_content_wizard_get_category_setting($category_id, 'text_length', ai_content_wizard_get_setting('ai_content_wizard_text_length')),
        'writingStyle' => ai_content_wizard_get_category_setting($category_id, 'writing_style', ai_content_wizard_get_setting('ai_content_wizard_writing_style')),
        'targetAudience' => ai_content_wizard_get_category_setting($category_id, 'target_audience', ai_content_wizard_get_setting('ai_content_wizard_target_audience')),
        'targetRegion' => ai_content_wizard_get_category_setting($category_id, 'target_region', ai_content_wizard_get_setting('ai_content_wizard_target_region')),
        'additionalRequests' => ai_content_wizard_get_category_setting($category_id, 'additional_requests', ai_content_wizard_get_setting('ai_content_wizard_additional_requests')),
        'enableImageGeneration' => ai_content_wizard_get_category_setting($category_id, 'enable_image_generation', ai_content_wizard_get_setting('ai_content_wizard_enable_image_generation')),
        'siteType' => ai_content_wizard_get_category_setting($category_id, 'site_type', ai_content_wizard_get_setting('ai_content_wizard_site_type')),
        'customTheme' => ai_content_wizard_get_category_setting($category_id, 'custom_theme', ai_content_wizard_get_setting('ai_content_wizard_custom_theme')),
        'postStatus' => ai_content_wizard_get_category_setting($category_id, 'post_status', ai_content_wizard_get_setting('ai_content_wizard_post_status')),
        'enableAdditionalImages' => ai_content_wizard_get_category_setting($category_id, 'enable_additional_images', ai_content_wizard_get_setting('ai_content_wizard_enable_additional_images')),
        'numberOfAdditionalImages' => ai_content_wizard_get_category_setting($category_id, 'additional_images_count', ai_content_wizard_get_setting('ai_content_wizard_additional_images_count')),
    ];
    wp_send_json_success($settings);
}

