<?php if ( ! defined( 'ABSPATH' ) ) { exit; } ?>
    <div id="schemafy-tab-manual" class="schemafy-tab-content-v2<?php echo esc_attr( schemafy_is_active_tab('manual', $active_tab) ); ?>"><!-- Edit mode banner (hidden by default) -->
    <div id="schemafy-manual-edit-banner" class="schemafy-mj-edit-banner" style="display:none;">
        <span class="dashicons dashicons-edit" style="color:#b45309;font-size:15px;width:15px;height:15px;flex-shrink:0;"></span>
        <span><?php esc_html_e('Editing:', 'schemafy'); ?></span>
        <strong class="schemafy-mj-edit-banner-name" id="schemafy-manual-edit-name">—</strong>
        <span style="color:#a16207;"><?php esc_html_e('— any changes will update the existing schema', 'schemafy'); ?></span>
        <button type="button" class="schemafy-mj-cancel-edit" id="schemafy-manual-cancel-edit">
            <?php esc_html_e('Cancel edit', 'schemafy'); ?>
        </button>
    </div>

    <form id="schemafy-manual-form" class="schemafy-form-v2">
    <div class="schemafy-mj-wrap">

        <!-- Config row: URL · Name · Type -->
        <div class="schemafy-mj-config">
            <div class="schemafy-mj-field">
                <label><?php esc_html_e('Target pages', 'schemafy'); ?></label>
                <div class="schemafy-url-dropdown-container" id="schemafy-manual-url-dropdown-container">
                    <input
                        type="text"
                        id="schemafy-manual-url-combobox"
                        class="schemafy-url-combobox"
                        autocomplete="off"
                        placeholder="<?php esc_html_e('Search pages or paste a URL…', 'schemafy'); ?>"
                        style="padding-left:12px;"
                    >
                    <div id="schemafy-manual-url-results" class="schemafy-url-results"></div>
                </div>
                <div class="schemafy-selected-urls schemafy-mj-chips" id="schemafy-manual-selected-urls" style="margin-top:5px;"></div>
                <input type="hidden" name="_target_urls" id="schemafy-manual-target-urls" value="[]">
            </div>
            <div class="schemafy-mj-field">
                <label><?php esc_html_e('Internal name', 'schemafy'); ?> <span style="font-weight:400;text-transform:none;letter-spacing:0;color:#9ca3af;"><?php esc_html_e('(only you see this)', 'schemafy'); ?></span></label>
                <input type="text" id="schemafy-manual-name" name="schema_name" class="schemafy-input-enhanced" required placeholder="<?php esc_html_e('e.g. Homepage Organization', 'schemafy'); ?>">
            </div>
            <div class="schemafy-mj-field">
                <label><?php esc_html_e('Schema type', 'schemafy'); ?></label>
                <select id="schemafy-manual-type" name="schema_type" class="schemafy-select-enhanced" required>
                    <option value=""><?php esc_html_e('Type…', 'schemafy'); ?></option>
                    <option value="Article">📰 <?php esc_html_e('Article', 'schemafy'); ?></option>
                    <option value="Product">🛍️ <?php esc_html_e('Product', 'schemafy'); ?></option>
                    <option value="Organization">🏢 <?php esc_html_e('Organization', 'schemafy'); ?></option>
                    <option value="LocalBusiness">🏪 <?php esc_html_e('Local Business', 'schemafy'); ?></option>
                    <option value="Person">👤 <?php esc_html_e('Person', 'schemafy'); ?></option>
                    <option value="Event">🎉 <?php esc_html_e('Event', 'schemafy'); ?></option>
                    <option value="Recipe">🍳 <?php esc_html_e('Recipe', 'schemafy'); ?></option>
                    <option value="Review">⭐ <?php esc_html_e('Review', 'schemafy'); ?></option>
                    <option value="FAQ">❓ <?php esc_html_e('FAQ', 'schemafy'); ?></option>
                    <option value="WebPage">📄 <?php esc_html_e('Web Page', 'schemafy'); ?></option>
                    <option value="Course">🎓 <?php esc_html_e('Course', 'schemafy'); ?></option>
                    <option value="HowTo">🔧 <?php esc_html_e('How-To', 'schemafy'); ?></option>
                    <option value="JobPosting">💼 <?php esc_html_e('Job Posting', 'schemafy'); ?></option>
                    <option value="Service">🛠️ <?php esc_html_e('Service', 'schemafy'); ?></option>
                    <option value="Video">🎬 <?php esc_html_e('Video', 'schemafy'); ?></option>
                    <option value="Custom">⚙️ <?php esc_html_e('Custom', 'schemafy'); ?></option>
                </select>
            </div>
        </div>

        <!-- Dark editor area -->
        <div class="schemafy-mj-editor-area">

            <!-- Toolbar -->
            <div class="schemafy-mj-toolbar">
                <button type="button" class="schemafy-mj-tbtn" id="schemafy-manual-format" title="<?php esc_html_e('Auto-format JSON', 'schemafy'); ?>">
                    <span class="dashicons dashicons-editor-alignleft"></span>
                    <?php esc_html_e('Format', 'schemafy'); ?>
                </button>
                <button type="button" class="schemafy-mj-tbtn" id="schemafy-manual-validate" title="<?php esc_html_e('Validate JSON', 'schemafy'); ?>">
                    <span class="dashicons dashicons-yes-alt"></span>
                    <?php esc_html_e('Validate', 'schemafy'); ?>
                </button>
                <button type="button" class="schemafy-mj-tbtn" id="schemafy-manual-minify" title="<?php esc_html_e('Minify JSON', 'schemafy'); ?>">
                    <span class="dashicons dashicons-editor-contract"></span>
                    <?php esc_html_e('Minify', 'schemafy'); ?>
                </button>
                <div class="schemafy-mj-tb-sep"></div>
                <button type="button" class="schemafy-mj-copy-btn" id="schemafy-manual-copy" title="<?php esc_html_e('Copy to clipboard', 'schemafy'); ?>">
                    <span class="dashicons dashicons-clipboard"></span>
                    <?php esc_html_e('Copy', 'schemafy'); ?>
                </button>
                <div class="schemafy-mj-tb-sep"></div>
                <button type="button" class="schemafy-mj-tbtn" id="schemafy-toggle-variables-panel" title="<?php esc_html_e('Insert variables', 'schemafy'); ?>">
                    <span class="dashicons dashicons-shortcode"></span>
                    <?php esc_html_e('Variables', 'schemafy'); ?>
                    <span style="font-size:10px;color:#6e7681;margin-left:2px;">▼</span>
                </button>
                <div class="schemafy-mj-tb-sep"></div>
                <!-- Template picker -->
                <div class="schemafy-mj-tpl-wrap" style="position:relative;">
                    <button type="button" class="schemafy-mj-tbtn" id="schemafy-template-trigger" aria-haspopup="true" aria-expanded="false">
                        <span class="dashicons dashicons-media-code"></span>
                        <?php esc_html_e('Templates', 'schemafy'); ?>
                        <span style="font-size:10px;color:#6e7681;margin-left:2px;">▼</span>
                    </button>
                    <div class="schemafy-mj-tpl-dropdown" id="schemafy-template-dropdown" style="display:none;top:calc(100% + 4px);right:auto;left:0;">
                        <div class="schemafy-mj-tpl-head"><?php esc_html_e('Insert template', 'schemafy'); ?></div>
                        <button type="button" class="schemafy-mj-tpl-item" data-template="article"><span class="schemafy-mj-tpl-item-icon">📰</span> <?php esc_html_e('Article', 'schemafy'); ?></button>
                        <button type="button" class="schemafy-mj-tpl-item" data-template="product"><span class="schemafy-mj-tpl-item-icon">🛍️</span> <?php esc_html_e('Product', 'schemafy'); ?></button>
                        <button type="button" class="schemafy-mj-tpl-item" data-template="organization"><span class="schemafy-mj-tpl-item-icon">🏢</span> <?php esc_html_e('Organization', 'schemafy'); ?></button>
                        <button type="button" class="schemafy-mj-tpl-item" data-template="faq"><span class="schemafy-mj-tpl-item-icon">❓</span> <?php esc_html_e('FAQ', 'schemafy'); ?></button>
                        <button type="button" class="schemafy-mj-tpl-item" data-template="localbusiness"><span class="schemafy-mj-tpl-item-icon">🏪</span> <?php esc_html_e('Local Business', 'schemafy'); ?></button>
                        <button type="button" class="schemafy-mj-tpl-item" data-template="event"><span class="schemafy-mj-tpl-item-icon">🎉</span> <?php esc_html_e('Event', 'schemafy'); ?></button>
                    </div>
                </div>
                <button type="button" class="schemafy-mj-tbtn" id="schemafy-manual-clear" title="<?php esc_html_e('Clear editor', 'schemafy'); ?>">
                    <span class="dashicons dashicons-trash"></span>
                    <?php esc_html_e('Clear', 'schemafy'); ?>
                </button>
                <div class="schemafy-mj-tb-spacer"></div>
                <button type="submit" class="schemafy-mj-save-btn">
                    <span class="dashicons dashicons-saved" style="font-size:14px;width:14px;height:14px;"></span>
                    <?php esc_html_e('Save Schema', 'schemafy'); ?>
                </button>
            </div>

            <!-- Variables Panel (Collapsible) -->
            <div id="schemafy-variables-panel" class="schemafy-variables-panel schemafy-mj-vars" style="display: none;">
                <div class="schemafy-variables-header">
                    <span class="dashicons dashicons-info-outline"></span>
                    <strong><?php esc_html_e('Smart Variables', 'schemafy'); ?></strong>
                    <span class="schemafy-variables-hint"><?php esc_html_e('Auto-filled by WordPress — click to insert', 'schemafy'); ?></span>
                </div>
                <div class="schemafy-variables-grid">
                    <!-- Site & SEO Group -->
                    <div class="schemafy-variable-group">
                        <div class="schemafy-variable-group-title">
                            <span class="dashicons dashicons-admin-site-alt3"></span> <?php esc_html_e('Site & SEO', 'schemafy'); ?>
                        </div>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{site_name}}" title="<?php esc_html_e('Your WordPress site name', 'schemafy'); ?>">
                            <code>{{site_name}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Site Name', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{site_url}}" title="<?php esc_html_e('Your site home URL', 'schemafy'); ?>">
                            <code>{{site_url}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Site URL', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{site_description}}" title="<?php esc_html_e('Site tagline/description', 'schemafy'); ?>">
                            <code>{{site_description}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Site Tagline', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{site_language}}" title="<?php esc_html_e('Site language code (e.g. en-US)', 'schemafy'); ?>">
                            <code>{{site_language}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Language', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{current_url}}" title="<?php esc_html_e('Current page full URL', 'schemafy'); ?>">
                            <code>{{current_url}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Page URL', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{current_year}}" title="<?php esc_html_e('Current year (e.g. 2026)', 'schemafy'); ?>">
                            <code>{{current_year}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Current Year', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{meta_title}}" title="<?php esc_html_e('SEO meta title from Yoast, RankMath, etc.', 'schemafy'); ?>">
                            <code>{{meta_title}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('SEO Title', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{meta_description}}" title="<?php esc_html_e('SEO meta description from Yoast, RankMath, etc.', 'schemafy'); ?>">
                            <code>{{meta_description}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('SEO Description', 'schemafy'); ?></span>
                        </button>
                    </div>
                    <!-- Post/Page Group -->
                    <div class="schemafy-variable-group">
                        <div class="schemafy-variable-group-title">
                            <span class="dashicons dashicons-admin-post"></span> <?php esc_html_e('Post / Page', 'schemafy'); ?>
                        </div>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{post_title}}" title="<?php esc_html_e('Post or page title', 'schemafy'); ?>">
                            <code>{{post_title}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Post Title', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{post_excerpt}}" title="<?php esc_html_e('Post excerpt or summary', 'schemafy'); ?>">
                            <code>{{post_excerpt}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Excerpt', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{post_author}}" title="<?php esc_html_e('Post author display name', 'schemafy'); ?>">
                            <code>{{post_author}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Author', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{post_date}}" title="<?php esc_html_e('Publication date (ISO 8601 format)', 'schemafy'); ?>">
                            <code>{{post_date}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Publish Date', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{post_modified}}" title="<?php esc_html_e('Last modified date (ISO 8601 format)', 'schemafy'); ?>">
                            <code>{{post_modified}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Modified Date', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{post_url}}" title="<?php esc_html_e('Post/page permalink URL', 'schemafy'); ?>">
                            <code>{{post_url}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Permalink', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{featured_image}}" title="<?php esc_html_e('Featured image URL (full size)', 'schemafy'); ?>">
                            <code>{{featured_image}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Featured Image', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{category}}" title="<?php esc_html_e('Primary category name', 'schemafy'); ?>">
                            <code>{{category}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Category', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{tags}}" title="<?php esc_html_e('Tags separated by commas', 'schemafy'); ?>">
                            <code>{{tags}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Tags', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{post_id}}" title="<?php esc_html_e('Post/page numeric ID', 'schemafy'); ?>">
                            <code>{{post_id}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Post ID', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{post_type}}" title="<?php esc_html_e('Post type (post, page, product)', 'schemafy'); ?>">
                            <code>{{post_type}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Post Type', 'schemafy'); ?></span>
                        </button>
                    </div>
                    <?php if (class_exists('WooCommerce')) : ?>
                    <!-- WooCommerce Group -->
                    <div class="schemafy-variable-group">
                        <div class="schemafy-variable-group-title">
                            <span class="dashicons dashicons-cart"></span> <?php esc_html_e('WooCommerce', 'schemafy'); ?>
                        </div>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{product_price}}" title="<?php esc_html_e('Current product price', 'schemafy'); ?>">
                            <code>{{product_price}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Price', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{product_regular_price}}" title="<?php esc_html_e('Regular price (before sale)', 'schemafy'); ?>">
                            <code>{{product_regular_price}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Regular Price', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{product_sale_price}}" title="<?php esc_html_e('Sale price', 'schemafy'); ?>">
                            <code>{{product_sale_price}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Sale Price', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{product_currency}}" title="<?php esc_html_e('Store currency code (e.g. USD)', 'schemafy'); ?>">
                            <code>{{product_currency}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Currency', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{product_sku}}" title="<?php esc_html_e('Product SKU', 'schemafy'); ?>">
                            <code>{{product_sku}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('SKU', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{product_stock_status}}" title="<?php esc_html_e('Availability as Schema.org URL', 'schemafy'); ?>">
                            <code>{{product_stock_status}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Stock Status', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{product_short_description}}" title="<?php esc_html_e('Product short description', 'schemafy'); ?>">
                            <code>{{product_short_description}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Short Description', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{product_weight}}" title="<?php esc_html_e('Product weight', 'schemafy'); ?>">
                            <code>{{product_weight}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Weight', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{product_category}}" title="<?php esc_html_e('Primary product category', 'schemafy'); ?>">
                            <code>{{product_category}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Product Category', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{product_rating}}" title="<?php esc_html_e('Average product rating', 'schemafy'); ?>">
                            <code>{{product_rating}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Rating', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{product_review_count}}" title="<?php esc_html_e('Number of reviews', 'schemafy'); ?>">
                            <code>{{product_review_count}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Review Count', 'schemafy'); ?></span>
                        </button>
                        <button type="button" class="schemafy-variable-btn" data-variable="{{product_image}}" title="<?php esc_html_e('Main product image URL', 'schemafy'); ?>">
                            <code>{{product_image}}</code>
                            <span class="schemafy-variable-desc"><?php esc_html_e('Product Image', 'schemafy'); ?></span>
                        </button>
                    </div>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Real-time Error Panel -->
            <div id="schemafy-error-panel" class="schemafy-error-panel schemafy-mj-errors" style="display: none;">
                <div class="schemafy-error-header">
                    <span class="dashicons dashicons-warning"></span>
                    <strong><?php esc_html_e('Issues Detected', 'schemafy'); ?></strong>
                </div>
                <ul id="schemafy-error-list" class="schemafy-error-list"></ul>
            </div>

            <!-- Code editor with line numbers -->
            <div class="schemafy-mj-editor-wrap">
                <div class="schemafy-mj-line-nums" id="schemafy-line-numbers"></div>
                <textarea
                    id="schemafy-manual-json"
                    name="schema_data"
                    class="schemafy-mj-textarea"
                    rows="20"
                    required
                    spellcheck="false"
                    placeholder='{
  "@context": "https://schema.org",
  "@type": "Organization",
  "name": "{{site_name}}",
  "url": "{{site_url}}",
  "description": "{{meta_description}}"
}'
                ></textarea>
            </div>

            <!-- Status bar (VSCode-style) -->
            <div class="schemafy-mj-statusbar">
                <span class="schemafy-mj-stat">
                    <span id="schemafy-json-lines">0</span>&nbsp;<?php esc_html_e('lines', 'schemafy'); ?>
                </span>
                <span class="schemafy-mj-stat">
                    <span id="schemafy-json-chars">0</span>&nbsp;<?php esc_html_e('chars', 'schemafy'); ?>
                </span>
                <span class="schemafy-mj-stat">
                    <span id="schemafy-json-size">0</span>&nbsp;KB
                </span>
                <span class="schemafy-mj-stat schemafy-bracket-status" id="schemafy-bracket-status">
                    <span id="schemafy-bracket-text"><?php esc_html_e('{ } OK', 'schemafy'); ?></span>
                </span>
                <span class="schemafy-mj-stat-sep"></span>
                <div id="schemafy-validation-status" class="schemafy-mj-valstatus" style="display:flex;">
                    <div class="schemafy-validation-badge"></div>
                    <div class="schemafy-validation-message"><?php esc_html_e('Not validated', 'schemafy'); ?></div>
                </div>
            </div>

        </div><!-- /.schemafy-mj-editor-area -->

    </div><!-- /.schemafy-mj-wrap -->
    </form>

    </div>
