<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound, WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Template-scope variables and single-post admin meta reads, not globals or slow queries.
if ( ! defined( 'ABSPATH' ) ) { exit; }

/**
 * Global settings page (Lite version).
 * Variables: $options (current saved options array), $saved (bool)
 */

$is_pro = function_exists( 'tnc_flipbook_3d_is_pro' ) && tnc_flipbook_3d_is_pro();

$tabs = array(
    'general'       => __( 'General',        'tnc-flipbook-3d-lite' ),
    'appearance'    => __( 'Display',        'tnc-flipbook-3d-lite' ),
    'toolbar'       => __( 'Toolbar',        'tnc-flipbook-3d-lite' ),
    'integrations'  => __( 'Integrations',   'tnc-flipbook-3d-lite' ),
    'accessibility' => __( 'Accessibility',  'tnc-flipbook-3d-lite' ),
    'advanced'      => __( 'Advanced',       'tnc-flipbook-3d-lite' ),
);

$tabs = apply_filters( 'tncfb3d_settings_tabs', $tabs );

// Tabs that show a Pro teaser instead of real settings
$locked_settings_tabs = array();

// Pro-only toolbar items
$pro_toolbar_items = array();

// Helper for option value
if ( ! function_exists( 'tncfb3d_opt' ) ) {
    function tncfb3d_opt( $options, $key, $default = '' ) {
        return isset( $options[ $key ] ) ? $options[ $key ] : $default;
    }
}
?>
<div class="wrap tncfb-settings-wrap tnc-flipbook-admin">
    <h1>
        <img src="<?php echo esc_url( TNC_FLIPBOOK_3D_URL . 'assets/flipbook/tnc-flipbook-icon-color.png' ); ?>" alt="" style="width:28px;height:28px;margin-right:8px;vertical-align:middle;" />
        <?php esc_html_e( 'TNC FlipBook 3D Lite - Global Settings', 'tnc-flipbook-3d-lite' ); ?>
    </h1>

    <?php if ( $saved ) : ?>
        <div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Settings saved.', 'tnc-flipbook-3d-lite' ); ?></p></div>
    <?php endif; ?>

    <p class="description">
        <?php esc_html_e( 'These are the default values for all flipbooks. Individual flipbooks can override toolbar and general settings.', 'tnc-flipbook-3d-lite' ); ?>
    </p>

    <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
        <input type="hidden" name="action" value="tncfb3d_save_settings" />
        <?php wp_nonce_field( 'tncfb3d_save_settings', 'tncfb3d_settings_nonce' ); ?>
        <input type="hidden" name="tncfb3d_settings_tab" id="tncfb3d_settings_tab" value="" />

        <!-- Tab navigation -->
        <nav class="tncfb-settings-nav" role="tablist">
            <?php foreach ( $tabs as $tab_key => $tab_label ) :
                $is_locked = in_array( $tab_key, $locked_settings_tabs, true );
                $classes = 'tncfb-settings-nav-btn';
                if ( $is_locked ) { $classes .= ' tncfb-tab-locked'; }
            ?>
                <button type="button"
                        role="tab"
                        class="<?php echo esc_attr( $classes ); ?>"
                        data-tab="<?php echo esc_attr( $tab_key ); ?>">
                    <?php echo esc_html( $tab_label ); ?>
                </button>
            <?php endforeach; ?>
        </nav>

        <!-- ═══════════════════════════════════════════════════════════════
             GENERAL
             ═══════════════════════════════════════════════════════════════ -->
        <div id="tncfb-settings-general" class="tncfb-settings-section">
            <table class="form-table">
                <tr>
                    <th><label for="tncfb3d_defaultViewMode"><?php esc_html_e( 'Default View Mode', 'tnc-flipbook-3d-lite' ); ?></label><?php tncfb3d_tip( 'defaultViewMode' ); ?></th>
                    <td>
                        <?php
                        $dvm = tncfb3d_opt( $options, 'defaultViewMode', 'flipbook' );
                        $vm_options = array(
                            'flipbook'   => __( 'FlipBook (3D/2D Page Flip)',     'tnc-flipbook-3d-lite' ),
                            'single'     => __( 'Single Page Flip',               'tnc-flipbook-3d-lite' ),
                            'vertical'   => __( 'Vertical Scroll (PDF Viewer)',   'tnc-flipbook-3d-lite' ),
                            'horizontal' => __( 'Horizontal Scroll (PDF Viewer)', 'tnc-flipbook-3d-lite' ),
                        );
                        ?>
                        <div class="tncfb-layout-cards">
                            <?php foreach ( $vm_options as $vm_key => $vm_label ) : ?>
                                <label class="tncfb-layout-card <?php echo $dvm === $vm_key ? 'is-active' : ''; ?>"
                                       data-value="<?php echo esc_attr( $vm_key ); ?>">
                                    <input type="radio"
                                           name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[defaultViewMode]"
                                           value="<?php echo esc_attr( $vm_key ); ?>"
                                           <?php checked( $dvm, $vm_key ); ?> />
                                    <?php TNC_Flipbook_3D_Helpers::render_view_mode_svg( $vm_key ); ?>
                                    <span class="tncfb-layout-card-label"><?php echo esc_html( $vm_label ); ?></span>
                                </label>
                            <?php endforeach; ?>
                        </div>
                        <p class="description"><?php esc_html_e( 'The view mode shown when the FlipBook first loads.', 'tnc-flipbook-3d-lite' ); ?></p>
                    </td>
                </tr>
                <tr>
                    <th><label for="tncfb3d_pageMode"><?php esc_html_e( 'Page Mode', 'tnc-flipbook-3d-lite' ); ?></label><?php tncfb3d_tip( 'pageMode' ); ?></th>
                    <td>
                        <?php $pm = tncfb3d_opt( $options, 'pageMode', 'odd' ); ?>
                        <select id="tncfb3d_pageMode" name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[pageMode]">
                            <option value="odd" <?php selected( $pm, 'odd' ); ?>><?php esc_html_e( 'Cover First (1, 2-3, 4-5...)', 'tnc-flipbook-3d-lite' ); ?></option>
                            <option value="even" <?php selected( $pm, 'even' ); ?>><?php esc_html_e( 'Spreads First (1-2, 3-4, 5-6...)', 'tnc-flipbook-3d-lite' ); ?></option>
                        </select>
                        <p class="description"><?php esc_html_e( 'How pages are paired into spreads. Cover First shows page 1 alone as a cover.', 'tnc-flipbook-3d-lite' ); ?></p>
                    </td>
                </tr>
                <tr>
                    <th><label for="tncfb3d_defaultZoom"><?php esc_html_e( 'Default Zoom', 'tnc-flipbook-3d-lite' ); ?></label></th>
                    <td>
                        <?php $dz = tncfb3d_opt( $options, 'defaultZoom', 'auto' ); ?>
                        <select id="tncfb3d_defaultZoom" name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[defaultZoom]">
                            <option value="auto" <?php selected( $dz, 'auto' ); ?>><?php esc_html_e( 'Automatic Zoom', 'tnc-flipbook-3d-lite' ); ?></option>
                            <option value="page-fit" <?php selected( $dz, 'page-fit' ); ?>><?php esc_html_e( 'Page Fit', 'tnc-flipbook-3d-lite' ); ?></option>
                            <option value="page-width" <?php selected( $dz, 'page-width' ); ?>><?php esc_html_e( 'Page Width', 'tnc-flipbook-3d-lite' ); ?></option>
                            <option value="50" <?php selected( $dz, '50' ); ?>>50%</option>
                            <option value="75" <?php selected( $dz, '75' ); ?>>75%</option>
                            <option value="100" <?php selected( $dz, '100' ); ?>>100%</option>
                            <option value="125" <?php selected( $dz, '125' ); ?>>125%</option>
                            <option value="150" <?php selected( $dz, '150' ); ?>>150%</option>
                            <option value="200" <?php selected( $dz, '200' ); ?>>200%</option>
                            <option value="250" <?php selected( $dz, '250' ); ?>>250%</option>
                            <option value="300" <?php selected( $dz, '300' ); ?>>300%</option>
                            <option value="400" <?php selected( $dz, '400' ); ?>>400%</option>
                        </select>
                        <p class="description"><?php esc_html_e( 'The zoom level when the FlipBook first loads.', 'tnc-flipbook-3d-lite' ); ?></p>
                    </td>
                </tr>
                <tr>
                    <th><?php esc_html_e( 'Mobile Single Page', 'tnc-flipbook-3d-lite' ); ?></th>
                    <td>
                        <label class="tncfb-switcher-wrap">
                            <input type="hidden" name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[forceSinglePageMobile]" value="" />
                            <input type="checkbox"
                                   name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[forceSinglePageMobile]"
                                   value="1" <?php checked( tncfb3d_opt( $options, 'forceSinglePageMobile', true ) ); ?> />
                            <span class="tncfb-switcher-slider"></span>
                            <span class="tncfb-switcher-label"><?php esc_html_e( 'Force single page mode on mobile', 'tnc-flipbook-3d-lite' ); ?></span>
                        </label>
                        <p class="description"><?php esc_html_e( 'Automatically switch to single page view on screens narrower than 768px for easier reading on phones.', 'tnc-flipbook-3d-lite' ); ?></p>
                    </td>
                </tr>
                <?php $tncfb3d_languages = TNC_Flipbook_3D_Helpers::get_languages(); ?>
                <tr>
                    <th><label for="tncfb3d_language"><?php esc_html_e( 'Default Language', 'tnc-flipbook-3d-lite' ); ?></label><?php tncfb3d_tip( 'language' ); ?></th>
                    <td>
                        <select id="tncfb3d_language"
                                name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[language]"
                                style="min-width:280px;">
                            <?php foreach ( $tncfb3d_languages as $group_label => $langs ) : ?>
                                <optgroup label="<?php echo esc_attr( $group_label ); ?>">
                                    <?php foreach ( $langs as $code => $name ) : ?>
                                        <option value="<?php echo esc_attr( $code ); ?>" <?php selected( tncfb3d_opt( $options, 'language', 'en' ), $code ); ?>>
                                            <?php echo esc_html( $name . ' (' . $code . ')' ); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </optgroup>
                            <?php endforeach; ?>
                        </select>
                        <?php /* translators: %d: number of available UI languages. */ ?>
                        <p class="description"><?php echo esc_html( sprintf( __( 'Language for all FlipBook UI text. Supports %d languages.', 'tnc-flipbook-3d-lite' ), TNC_Flipbook_3D_Helpers::count_languages() ) ); ?></p>
                    </td>
                </tr>
                <tr>
                    <th><?php esc_html_e( 'Deep Linking', 'tnc-flipbook-3d-lite' ); ?><?php tncfb3d_tip( 'deepLinking' ); ?></th>
                    <td>
                        <label class="tncfb-switcher-wrap">
                            <input type="hidden" name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[deepLinking]" value="" />
                            <input type="checkbox"
                                   name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[deepLinking]"
                                   value="1" <?php checked( tncfb3d_opt( $options, 'deepLinking', true ) ); ?> />
                            <span class="tncfb-switcher-slider"></span>
                            <span class="tncfb-switcher-label"><?php esc_html_e( 'Update URL hash on page change (#page/N) for shareable page links', 'tnc-flipbook-3d-lite' ); ?></span>
                        </label>
                    </td>
                </tr>
                <tr>
                    <th><label for="tncfb3d_linkTarget"><?php esc_html_e( 'Link Target', 'tnc-flipbook-3d-lite' ); ?></label><?php tncfb3d_tip( 'linkTarget' ); ?></th>
                    <td>
                        <select id="tncfb3d_linkTarget" name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[linkTarget]">
                            <option value="new" <?php selected( tncfb3d_opt( $options, 'linkTarget', 'new' ), 'new' ); ?>><?php esc_html_e( 'New tab', 'tnc-flipbook-3d-lite' ); ?></option>
                            <option value="same" <?php selected( tncfb3d_opt( $options, 'linkTarget', 'new' ), 'same' ); ?>><?php esc_html_e( 'Same tab', 'tnc-flipbook-3d-lite' ); ?></option>
                        </select>
                        <p class="description"><?php esc_html_e( 'Whether PDF links open in a new browser tab or the current one.', 'tnc-flipbook-3d-lite' ); ?></p>
                    </td>
                </tr>
                <tr>
                    <th><?php esc_html_e( 'PDF Link Overlay', 'tnc-flipbook-3d-lite' ); ?><?php tncfb3d_tip( 'pdfLinkOverlay' ); ?></th>
                    <td>
                        <label class="tncfb-switcher-wrap">
                            <input type="hidden" name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[pdfLinkOverlay]" value="" />
                            <input type="checkbox"
                                   name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[pdfLinkOverlay]"
                                   value="1" <?php checked( tncfb3d_opt( $options, 'pdfLinkOverlay', true ) ); ?> />
                            <span class="tncfb-switcher-slider"></span>
                            <span class="tncfb-switcher-label"><?php esc_html_e( 'Show clickable links and annotations embedded in the PDF', 'tnc-flipbook-3d-lite' ); ?></span>
                        </label>
                    </td>
                </tr>
                <tr>
                    <th>
                        <?php esc_html_e( 'Attribution Badge', 'tnc-flipbook-3d-lite' ); ?>
                    </th>
                    <td>
                        <label class="tncfb-switcher-wrap">
                            <?php if ( $is_pro ) : ?>
                                <input type="hidden" name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[show_attribution]" value="" />
                                <input type="checkbox"
                                       name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[show_attribution]"
                                       value="1" <?php checked( tncfb3d_opt( $options, 'show_attribution', false ) ); ?> />
                            <?php else : ?>
                                <input type="hidden" name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[show_attribution]" value="" />
                                <input type="checkbox"
                                       name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[show_attribution]"
                                       value="1" <?php checked( tncfb3d_opt( $options, 'show_attribution', false ) ); ?> />
                            <?php endif; ?>
                            <span class="tncfb-switcher-slider"></span>
                            <span class="tncfb-switcher-label"><?php esc_html_e( 'Show "Powered by TNC FlipBook 3D" in the viewer', 'tnc-flipbook-3d-lite' ); ?></span>
                        </label>
                        <?php if ( $is_pro ) : ?>
                            <p class="description"><?php esc_html_e( 'Help other people discover TNC FlipBook 3D by showing a small badge in the corner of your flipbooks. We really appreciate it!', 'tnc-flipbook-3d-lite' ); ?></p>
                        <?php else : ?>
                            <p class="description"><?php esc_html_e( 'Show a small "Powered by TNC FlipBook 3D" badge in the corner of your flipbooks. Off by default - turn it on to support the plugin.', 'tnc-flipbook-3d-lite' ); ?></p>
                        <?php endif; ?>
                    </td>
                </tr>
            </table>
        </div>

        <!-- ═══════════════════════════════════════════════════════════════
             DISPLAY (partial free: color scheme, theme, render mode, toolbar layout)
             ═══════════════════════════════════════════════════════════════ -->
        <div id="tncfb-settings-appearance" class="tncfb-settings-section">
            <table class="form-table">
                <!-- ── Chrome (surface palette) ────────────────────────── -->
                <tr>
                    <th><?php esc_html_e( 'Color Scheme', 'tnc-flipbook-3d-lite' ); ?><?php tncfb3d_tip( 'chrome' ); ?></th>
                    <td>
                        <?php
                        // All chrome surface palettes are available.
                        $lite_chromes = array(
                            'slate'    => array( 'label' => __( 'Slate',    'tnc-flipbook-3d-lite' ), 'desc' => __( 'Neutral cool-gray surfaces.', 'tnc-flipbook-3d-lite' ),               'preview' => 'linear-gradient(to bottom right, #1c1c22 50%, #f5f5f7 50%)', 'pro' => false ),
                            'paper'    => array( 'label' => __( 'Paper',    'tnc-flipbook-3d-lite' ), 'desc' => __( 'Clean off-white surfaces.', 'tnc-flipbook-3d-lite' ),                 'preview' => 'linear-gradient(to bottom right, #ffffff 50%, #1c1c22 50%)', 'pro' => false  ),
                            'midnight' => array( 'label' => __( 'Midnight', 'tnc-flipbook-3d-lite' ), 'desc' => __( 'Deep blue-black tones.', 'tnc-flipbook-3d-lite' ),                    'preview' => 'linear-gradient(to bottom right, #1a2540 50%, #dde4f3 50%)', 'pro' => false  ),
                            'ocean'    => array( 'label' => __( 'Ocean',    'tnc-flipbook-3d-lite' ), 'desc' => __( 'Cool teal/blue tint.', 'tnc-flipbook-3d-lite' ),                     'preview' => 'linear-gradient(to bottom right, #1d4148 50%, #cfe6ea 50%)', 'pro' => false  ),
                            'forest'   => array( 'label' => __( 'Forest',   'tnc-flipbook-3d-lite' ), 'desc' => __( 'Subtle green tint.', 'tnc-flipbook-3d-lite' ),                       'preview' => 'linear-gradient(to bottom right, #1f3a26 50%, #cfe2d4 50%)', 'pro' => false  ),
                            'glass'    => array( 'label' => __( 'Glass',    'tnc-flipbook-3d-lite' ), 'desc' => __( 'Translucent frosted surfaces.', 'tnc-flipbook-3d-lite' ),             'preview' => 'linear-gradient(135deg, rgba(20,20,24,0.55), rgba(80,80,90,0.35))', 'pro' => false ),
                            'sepia'    => array( 'label' => __( 'Sepia',    'tnc-flipbook-3d-lite' ), 'desc' => __( 'Warm cream tones for editorial reading.', 'tnc-flipbook-3d-lite' ),   'preview' => 'linear-gradient(to bottom right, #4a341f 50%, #f4ecd8 50%)', 'pro' => false  ),
                        );
                        ?>
                        <div class="tncfb-chrome-cards">
                            <?php foreach ( $lite_chromes as $ch_key => $ch ) : ?>
                                <label class="tncfb-chrome-card <?php echo $ch_key === ( tncfb3d_opt( $options, 'chrome', 'slate' ) ) ? 'is-active' : ''; ?>"
                                       data-value="<?php echo esc_attr( $ch_key ); ?>"
                                       <?php if ( $ch['pro'] ) : ?>style="opacity:0.55;pointer-events:none;cursor:not-allowed;"<?php endif; ?>>
                                    <input type="radio"
                                           name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[chrome]"
                                           value="<?php echo esc_attr( $ch_key ); ?>"
                                           <?php checked( $ch_key, tncfb3d_opt( $options, 'chrome', 'slate' ) ); ?>
                                           <?php disabled( $ch['pro'], true ); ?> />
                                    <span class="tncfb-chrome-card-preview" style="background:<?php echo esc_attr( $ch['preview'] ); ?>"></span>
                                    <span class="tncfb-chrome-card-body">
                                        <span class="tncfb-chrome-card-label">
                                            <?php echo esc_html( $ch['label'] ); ?>
                                            <?php if ( $ch['pro'] ) : ?><?php endif; ?>
                                        </span>
                                        <span class="tncfb-chrome-card-desc"><?php echo esc_html( $ch['desc'] ); ?></span>
                                    </span>
                                </label>
                            <?php endforeach; ?>
                        </div>
                        <p class="description" style="margin-top:10px;"><?php esc_html_e( 'Surface palette for the toolbar, sidebars, and popups.', 'tnc-flipbook-3d-lite' ); ?></p>
                    </td>
                </tr>
                <!-- Brand Color -->
                <tr>
                    <th><label for="tncfb3d_brandColor"><?php esc_html_e( 'Brand Color', 'tnc-flipbook-3d-lite' ); ?></label></th>
                    <td>
                        <?php $brand_current = tncfb3d_opt( $options, 'brandColor', '#4a9eff' ); ?>
                        <div class="tncfb-color-wrap">
                            <input type="color" id="tncfb3d_brandColor_picker" value="<?php echo esc_attr( $brand_current ); ?>" data-target="tncfb3d_brandColor" />
                            <input type="text" id="tncfb3d_brandColor" class="tncfb-color-text" name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[brandColor]" value="<?php echo esc_attr( $brand_current ); ?>" placeholder="#rrggbb" maxlength="9" />
                        </div>
                        <p class="description"><?php esc_html_e( 'Accent color for buttons, active icons, focus rings, and highlights across the viewer.', 'tnc-flipbook-3d-lite' ); ?></p>
                    </td>
                </tr>
                <!-- Hardcover Color -->
                <tr>
                    <th><label for="tncfb3d_coverColor"><?php esc_html_e( 'Hardcover Color', 'tnc-flipbook-3d-lite' ); ?></label></th>
                    <td>
                        <?php $cover_current = tncfb3d_opt( $options, 'coverColor', '' ); ?>
                        <div class="tncfb-color-wrap">
                            <input type="color" id="tncfb3d_coverColor_picker" value="<?php echo esc_attr( '' !== $cover_current ? $cover_current : '#1c1c22' ); ?>" data-target="tncfb3d_coverColor" />
                            <input type="text" id="tncfb3d_coverColor" class="tncfb-color-text" name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[coverColor]" value="<?php echo esc_attr( $cover_current ); ?>" placeholder="<?php esc_attr_e( 'Auto (from chrome)', 'tnc-flipbook-3d-lite' ); ?>" maxlength="9" />
                        </div>
                        <p class="description"><?php esc_html_e( 'Hardcover wrap color when FlipBook Style is Hardcover. Leave blank to derive from the chrome.', 'tnc-flipbook-3d-lite' ); ?></p>
                    </td>
                </tr>
                <tr>
                    <th><label for="tncfb3d_colorMode"><?php esc_html_e( 'Default Theme', 'tnc-flipbook-3d-lite' ); ?><?php tncfb3d_tip( 'colorMode' ); ?></label></th>
                    <td>
                        <?php $cm = tncfb3d_opt( $options, 'colorMode', 'auto' ); ?>
                        <select id="tncfb3d_colorMode" name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[colorMode]">
                            <option value="auto"  <?php selected( $cm, 'auto' );  ?>><?php esc_html_e( 'Auto (use chrome default)', 'tnc-flipbook-3d-lite' ); ?></option>
                            <option value="dark"  <?php selected( $cm, 'dark' );  ?>><?php esc_html_e( 'Dark',  'tnc-flipbook-3d-lite' ); ?></option>
                            <option value="light" <?php selected( $cm, 'light' ); ?>><?php esc_html_e( 'Light', 'tnc-flipbook-3d-lite' ); ?></option>
                        </select>
                        <p class="description"><?php esc_html_e( 'Initial polarity on load. Readers can flip via the toolbar toggle.', 'tnc-flipbook-3d-lite' ); ?></p>
                    </td>
                </tr>

                <!-- ── Render Mode ────────────────────────────── -->
                <tr>
                    <th><label for="tncfb3d_renderMode"><?php esc_html_e( 'Render Mode', 'tnc-flipbook-3d-lite' ); ?></label><?php tncfb3d_tip( 'renderMode' ); ?></th>
                    <td>
                        <?php $rm = tncfb3d_opt( $options, 'renderMode', 'auto' ); ?>
                        <select id="tncfb3d_renderMode" name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[renderMode]">
                            <option value="auto" <?php selected( $rm, 'auto' ); ?>><?php esc_html_e( 'Auto (3D if WebGL, 2D fallback)', 'tnc-flipbook-3d-lite' ); ?></option>
                            <option value="3d" <?php selected( $rm, '3d' ); ?>><?php esc_html_e( '3D Only', 'tnc-flipbook-3d-lite' ); ?></option>
                            <option value="2d" <?php selected( $rm, '2d' ); ?>><?php esc_html_e( '2D CSS Only', 'tnc-flipbook-3d-lite' ); ?></option>
                        </select>
                        <p class="description"><?php esc_html_e( 'Auto detects WebGL support and falls back to a CSS-based 2D flip mode.', 'tnc-flipbook-3d-lite' ); ?></p>
                    </td>
                </tr>
                <!-- Flip Sound -->
                <tr>
                    <th><label for="tncfb3d_flipSound"><?php esc_html_e( 'Flip Sound', 'tnc-flipbook-3d-lite' ); ?></label></th>
                    <td>
                        <?php $fs = tncfb3d_opt( $options, 'flipSound', '1' ); ?>
                        <select id="tncfb3d_flipSound" name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[flipSound]">
                            <?php for ( $i = 1; $i <= 13; $i++ ) : ?>
                                <?php /* translators: %d: page-flip sound preset number */ ?>
                                <option value="<?php echo esc_attr( $i ); ?>" <?php selected( $fs, (string) $i ); ?>><?php printf( esc_html__( 'Sound %d', 'tnc-flipbook-3d-lite' ), absint( $i ) ); ?></option>
                            <?php endfor; ?>
                            <option value="none" <?php selected( $fs, 'none' ); ?>><?php esc_html_e( 'None (Silent)', 'tnc-flipbook-3d-lite' ); ?></option>
                        </select>
                        <p class="description"><?php esc_html_e( 'Sound played when a page turns.', 'tnc-flipbook-3d-lite' ); ?></p>
                    </td>
                </tr>

            </table>

            <!-- Pro teaser for remaining display settings -->
            <?php
            $pro_teaser = array(
                'title' => __( 'More Display Settings in Pro', 'tnc-flipbook-3d-lite' ),
                'features' => array(
                    __( '4 flip effects: Realistic, Hard Cover, Slide, Fade', 'tnc-flipbook-3d-lite' ),
                    __( '3 book styles: Default, Hardcover, Spiral', 'tnc-flipbook-3d-lite' ),
                    __( 'Page curl intensity, glossy reflections, paper types', 'tnc-flipbook-3d-lite' ),
                    __( '13 flip sound effects + custom upload', 'tnc-flipbook-3d-lite' ),
                    __( 'Custom color palette (4 color pickers)', 'tnc-flipbook-3d-lite' ),
                    __( 'Custom brand logo (dark + light)', 'tnc-flipbook-3d-lite' ),
                ),
            );
            include TNC_FLIPBOOK_3D_DIR . 'admin/views/pro-teaser-inline.php';
            ?>
        </div>

        <!-- ═══════════════════════════════════════════════════════════════
             TOOLBAR (grouped with section headings)
             ═══════════════════════════════════════════════════════════════ -->
        <div id="tncfb-settings-toolbar" class="tncfb-settings-section">
            <?php if ( $is_pro ) : ?>
            <p class="description"><?php esc_html_e( 'Enable or disable each toolbar item by default. Individual flipbooks can override these.', 'tnc-flipbook-3d-lite' ); ?></p>
            <?php else : ?>
            <p class="description">
                
            </p>
            <?php endif; ?>
            <table class="form-table">
                <tr>
                    <th><label><?php esc_html_e( 'Toolbar Layout', 'tnc-flipbook-3d-lite' ); ?></label><?php tncfb3d_tip( 'toolbarLayout' ); ?></th>
                    <td>
                        <?php
                        $tl = tncfb3d_opt( $options, 'toolbarLayout', 'bottom-center' );
                        $tl_options = array(
                            'bottom-center'    => array( 'label' => __( 'Bottom Center',     'tnc-flipbook-3d-lite' ), 'pro' => false ),
                            'top-center'       => array( 'label' => __( 'Top Center',        'tnc-flipbook-3d-lite' ), 'pro' => false ),
                            'bottom-fullwidth' => array( 'label' => __( 'Bottom Full Width', 'tnc-flipbook-3d-lite' ), 'pro' => false ),
                            'top-fullwidth'    => array( 'label' => __( 'Top Full Width',    'tnc-flipbook-3d-lite' ), 'pro' => false ),
                        );
                        ?>
                        <div class="tncfb-layout-cards">
                            <?php foreach ( $tl_options as $tl_key => $tl_opt ) :
                                $is_pro_locked = false;
                                ?>
                                <label class="tncfb-layout-card <?php echo ( $tl === $tl_key && ! $is_pro_locked ) ? 'is-active' : ''; ?> <?php echo $is_pro_locked ? 'is-pro-locked' : ''; ?>"
                                       data-value="<?php echo esc_attr( $tl_key ); ?>">
                                    <input type="radio"
                                           name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[toolbarLayout]"
                                           value="<?php echo esc_attr( $tl_key ); ?>"
                                           <?php checked( $tl, $tl_key ); ?>
                                           <?php disabled( $is_pro_locked, true ); ?> />
                                    <?php TNC_Flipbook_3D_Helpers::render_toolbar_layout_svg( $tl_key ); ?>
                                    <span class="tncfb-layout-card-label">
                                        <?php echo esc_html( $tl_opt['label'] ); ?>
                                        <?php if ( $is_pro_locked ) : ?>
                                            
                                        <?php endif; ?>
                                    </span>
                                </label>
                            <?php endforeach; ?>
                        </div>
                    </td>
                </tr>
                <?php
                $toolbar_groups = array(
                    __( 'Navigation', 'tnc-flipbook-3d-lite' ) => array( 'pageIndicator', 'navigation', 'firstLast', 'singlePage', 'autoPlay' ),
                    __( 'Content Tools', 'tnc-flipbook-3d-lite' ) => array( 'search', 'zoom', 'thumbnails', 'bookmarks', 'notes', 'formViewer' ),
                    __( 'Actions', 'tnc-flipbook-3d-lite' ) => array( 'share', 'download', 'print', 'sound', 'fullscreen', 'readingMode', 'readAloud' ),
                    __( 'Interface', 'tnc-flipbook-3d-lite' ) => array( 'themeToggle', 'logo', 'tilt', 'rotate' ),
                );
                $toolbar_items = array();
                foreach ( $toolbar_groups as $items ) {
                    $toolbar_items = array_merge( $toolbar_items, $items );
                }
                $current_group = '';
                foreach ( $toolbar_items as $item ) :
                    $key = 'toolbar_' . $item;
                    // Lite: toolbar item visibility is a Pro-only control, so
                    // every item is locked (disabled toggle + Pro badge).
                    $is_locked_item = false; // WordPress.org build: toolbar items are configurable.

                    // Insert group heading
                    foreach ( $toolbar_groups as $group_label => $group_items ) {
                        if ( $group_items[0] === $item && $current_group !== $group_label ) {
                            $current_group = $group_label;
                            echo '<tr><td colspan="2" style="padding:0;"><h3 style="font-size:14px;font-weight:600;color:#1d2327;margin:24px 0 8px;padding-top:16px;border-top:1px solid #e8e8eb;">' . esc_html( $group_label ) . '</h3></td></tr>';
                            break;
                        }
                    }
                ?>
                    <tr<?php echo $is_locked_item ? ' style="opacity:0.5;"' : ''; ?>>
                        <th>
                            <?php echo esc_html( ucfirst( preg_replace( '/([A-Z])/', ' $1', $item ) ) ); ?><?php tncfb3d_tip( 'toolbar_' . $item ); ?>
                            <?php if ( $is_locked_item ) : ?>
                                
                            <?php endif; ?>
                        </th>
                        <td>
                            <label class="tncfb-switcher-wrap">
                                <?php if ( $is_locked_item ) : ?>
                                    <?php // Visual-only: no name => not submitted, so the shared option isn't polluted with forced-off values (which would also hide the toolbar after a Lite->Pro upgrade). Reflects the viewer default: Pro-only items off, the rest on. ?>
                                    <input type="checkbox" value="1"
                                           <?php checked( ! in_array( $item, $pro_toolbar_items, true ) ); ?>
                                           disabled />
                                <?php else : ?>
                                    <input type="hidden" name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[<?php echo esc_attr( $key ); ?>]" value="" />
                                    <input type="checkbox"
                                           name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[<?php echo esc_attr( $key ); ?>]"
                                           value="1" <?php checked( tncfb3d_opt( $options, $key, true ) ); ?> />
                                <?php endif; ?>
                                <span class="tncfb-switcher-slider"></span>
                            </label>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </table>
        </div>

        <!-- ═══════════════════════════════════════════════════════════════
             PROTECTION (Pro teaser)
             ═══════════════════════════════════════════════════════════════ -->
        <div id="tncfb-settings-protection" class="tncfb-settings-section">
            <?php
            $pro_teaser = array(
                'title' => __( 'Content Protection / DRM', 'tnc-flipbook-3d-lite' ),
                'icon'  => 'dashicons-shield',
                'features' => array(
                    __( 'Text & image watermark system', 'tnc-flipbook-3d-lite' ),
                    __( 'Disable right-click, keyboard shortcuts, image drag', 'tnc-flipbook-3d-lite' ),
                    __( 'DevTools detection with blur or warning', 'tnc-flipbook-3d-lite' ),
                    __( 'Admin bypass for testing', 'tnc-flipbook-3d-lite' ),
                ),
            );
            include TNC_FLIPBOOK_3D_DIR . 'admin/views/pro-teaser.php';
            ?>
        </div>

        <!-- ═══════════════════════════════════════════════════════════════
             INTEGRATIONS
             ═══════════════════════════════════════════════════════════════ -->
        <div id="tncfb-settings-integrations" class="tncfb-settings-section">
            <p class="description"><?php esc_html_e( 'These API keys are not used by the core plugin. They are stored here centrally so that compatible addons (Chatbot, Summarization, etc.) can access them when installed. Keys are stored server-side only - never exposed to the browser.', 'tnc-flipbook-3d-lite' ); ?></p>

            <?php
            // AI Services - always available (used by Chatbot addon, etc.)
            $ai_fields = array(
                'openai_key' => array(
                    'label' => __( 'OpenAI API Key', 'tnc-flipbook-3d-lite' ),
                    'desc'  => sprintf(
                        /* translators: %s: link to platform.openai.com */
                        __( 'Not used by the core plugin. Required by addons such as Chatbot and Summarization. Generate a key at %s.', 'tnc-flipbook-3d-lite' ),
                        '<a href="https://platform.openai.com/api-keys" target="_blank" rel="noopener noreferrer">platform.openai.com</a>'
                    ),
                ),
                'anthropic_key' => array(
                    'label' => __( 'Anthropic API Key', 'tnc-flipbook-3d-lite' ),
                    'desc'  => sprintf(
                        /* translators: %s: link to Anthropic Console */
                        __( 'Not used by the core plugin. Required by addons that offer Claude-based AI features. Get your key from the %s.', 'tnc-flipbook-3d-lite' ),
                        '<a href="https://console.anthropic.com/settings/keys" target="_blank" rel="noopener noreferrer">Anthropic Console</a>'
                    ),
                ),
                'google_ai_key' => array(
                    'label' => __( 'Google Gemini API Key', 'tnc-flipbook-3d-lite' ),
                    'desc'  => sprintf(
                        /* translators: %s: link to Google AI Studio */
                        __( 'Not used by the core plugin. Required by addons that offer Gemini-based AI features. Create a free API key at %s.', 'tnc-flipbook-3d-lite' ),
                        '<a href="https://aistudio.google.com/apikey" target="_blank" rel="noopener noreferrer">Google AI Studio</a>'
                    ),
                ),
            );

            /**
             * Filter the integration field groups for Lite.
             * Addons can add their own fields here.
             */
            $ai_fields = apply_filters( 'tncfb3d_integration_fields_lite', $ai_fields, $options );
            ?>
            <details class="tncfb-accordion-item" open>
                <summary class="tncfb-accordion-header"><?php esc_html_e( 'AI Services', 'tnc-flipbook-3d-lite' ); ?></summary>
                <div class="tncfb-accordion-body">
                    <table class="form-table">
                        <?php foreach ( $ai_fields as $fk => $fd ) :
                            $fval = isset( $options[ $fk ] ) ? $options[ $fk ] : '';
                            $ftype = isset( $fd['type'] ) ? $fd['type'] : 'password';
                        ?>
                            <tr>
                                <th><label for="tncfb3d_<?php echo esc_attr( $fk ); ?>"><?php echo esc_html( $fd['label'] ); ?></label><?php if ( function_exists( 'tncfb3d_tip' ) ) { tncfb3d_tip( $fk ); } ?></th>
                                <td>
                                    <input type="<?php echo esc_attr( $ftype ); ?>"
                                           id="tncfb3d_<?php echo esc_attr( $fk ); ?>"
                                           name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[<?php echo esc_attr( $fk ); ?>]"
                                           value="<?php echo esc_attr( $fval ); ?>"
                                           class="large-text"
                                           <?php if ( ! empty( $fd['placeholder'] ) ) : ?>placeholder="<?php echo esc_attr( $fd['placeholder'] ); ?>"<?php endif; ?>
                                           autocomplete="off" />
                                    <?php if ( ! empty( $fd['desc'] ) ) : ?>
                                        <p class="description"><?php echo wp_kses( $fd['desc'], array( 'a' => array( 'href' => array(), 'target' => array(), 'rel' => array() ), 'code' => array() ) ); ?></p>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </table>
                </div>
            </details>

            <?php
            // Pro-only integrations teaser
            $pro_teaser = array(
                'title' => __( 'More Integrations in Pro', 'tnc-flipbook-3d-lite' ),
                'features' => array(
                    __( 'Google Analytics 4 tracking', 'tnc-flipbook-3d-lite' ),
                    __( '5 TTS providers: Google, ElevenLabs, OpenAI, Azure, Amazon', 'tnc-flipbook-3d-lite' ),
                    __( 'WooCommerce purchase gating', 'tnc-flipbook-3d-lite' ),
                ),
            );
            include TNC_FLIPBOOK_3D_DIR . 'admin/views/pro-teaser-inline.php';

            // Allow addons to inject additional sections
            do_action( 'tncfb3d_settings_integrations_after', $options );
            ?>
        </div>

        <!-- ═══════════════════════════════════════════════════════════════
             ACCESSIBILITY (real settings - Free feature)
             ═══════════════════════════════════════════════════════════════ -->
        <div id="tncfb-settings-accessibility" class="tncfb-settings-section">
            <table class="form-table">
                <?php
                $a11y_flags = array(
                    'a11y_enabled'                   => array( 'label' => __( 'Enable Accessibility Features', 'tnc-flipbook-3d-lite' ), 'desc' => __( 'Master switch for all WCAG 2.1 AA accessibility features below.', 'tnc-flipbook-3d-lite' ) ),
                    'a11y_skipLinks'                  => array( 'label' => __( 'Skip Links', 'tnc-flipbook-3d-lite' ), 'desc' => __( 'Add hidden skip-to-content links for keyboard and screen reader users.', 'tnc-flipbook-3d-lite' ) ),
                    'a11y_keyboardShortcuts'          => array( 'label' => __( 'Keyboard Shortcuts', 'tnc-flipbook-3d-lite' ), 'desc' => __( 'Allow arrow keys, spacebar, and other shortcuts to navigate the flipbook.', 'tnc-flipbook-3d-lite' ) ),
                    'a11y_screenReaderAnnouncements'  => array( 'label' => __( 'Screen Reader Announcements', 'tnc-flipbook-3d-lite' ), 'desc' => __( 'Announce page changes and actions via ARIA live regions for screen readers.', 'tnc-flipbook-3d-lite' ) ),
                    'a11y_focusIndicators'            => array( 'label' => __( 'Focus Indicators', 'tnc-flipbook-3d-lite' ), 'desc' => __( 'Show visible outlines on focused elements for keyboard navigation.', 'tnc-flipbook-3d-lite' ) ),
                    'a11y_reducedMotion'              => array( 'label' => __( 'Respect Reduced Motion Preference', 'tnc-flipbook-3d-lite' ), 'desc' => __( 'Disable Page Flip animations when the user has requested reduced motion in their OS settings.', 'tnc-flipbook-3d-lite' ) ),
                    'a11y_highContrastMode'           => array( 'label' => __( 'High Contrast Mode Support', 'tnc-flipbook-3d-lite' ), 'desc' => __( 'Adapt the UI when the browser is in high contrast or forced-colors mode.', 'tnc-flipbook-3d-lite' ) ),
                    'a11y_keyboardShortcutsHelp'      => array( 'label' => __( 'Show Keyboard Shortcuts Help Panel', 'tnc-flipbook-3d-lite' ), 'desc' => __( 'Display a help panel listing all available keyboard shortcuts (press ? to open).', 'tnc-flipbook-3d-lite' ) ),
                );
                foreach ( $a11y_flags as $ak => $af ) :
                ?>
                    <tr>
                        <th><?php echo esc_html( $af['label'] ); ?><?php tncfb3d_tip( $ak ); ?></th>
                        <td>
                            <label class="tncfb-switcher-wrap">
                                <input type="hidden" name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[<?php echo esc_attr( $ak ); ?>]" value="" />
                                <input type="checkbox"
                                       name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[<?php echo esc_attr( $ak ); ?>]"
                                       value="1" <?php checked( tncfb3d_opt( $options, $ak, true ) ); ?> />
                                <span class="tncfb-switcher-slider"></span>
                            </label>
                            <p class="description"><?php echo esc_html( $af['desc'] ); ?></p>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </table>
        </div>

        <!-- ═══════════════════════════════════════════════════════════════
             ADVANCED (Text Selection, Render Scale, Custom CSS teaser, Danger Zone)
             ═══════════════════════════════════════════════════════════════ -->
        <div id="tncfb-settings-advanced" class="tncfb-settings-section">
            <table class="form-table">
                <tr>
                    <th><label for="tncfb3d_renderScale"><?php esc_html_e( 'Render Scale', 'tnc-flipbook-3d-lite' ); ?></label><?php tncfb3d_tip( 'renderScale' ); ?></th>
                    <td>
                        <input type="number" id="tncfb3d_renderScale"
                               name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[renderScale]"
                               value="<?php echo esc_attr( tncfb3d_opt( $options, 'renderScale', 2 ) ); ?>"
                               min="0.5" max="4" step="0.5" class="small-text" />
                        <p class="description"><?php esc_html_e( 'Higher = crisper text but more memory (2 recommended).', 'tnc-flipbook-3d-lite' ); ?></p>
                    </td>
                </tr>
                <tr>
                    <th><label for="tncfb3d_flipDuration"><?php esc_html_e( 'Flip Duration (ms)', 'tnc-flipbook-3d-lite' ); ?></label><?php tncfb3d_tip( 'flipDuration' ); ?></th>
                    <td>
                        <input type="number" id="tncfb3d_flipDuration"
                               name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[flipDuration]"
                               value="<?php echo esc_attr( tncfb3d_opt( $options, 'flipDuration', 1200 ) ); ?>"
                               min="100" max="3000" step="100" class="small-text" />
                        <p class="description"><?php esc_html_e( 'How long a single Page Flip animation takes. Lower values feel snappier.', 'tnc-flipbook-3d-lite' ); ?></p>
                    </td>
                </tr>
            </table>

            <!-- Pro teaser for Custom CSS -->
            <?php
            $pro_teaser = array(
                'title' => __( 'Custom CSS', 'tnc-flipbook-3d-lite' ),
                'icon'  => 'dashicons-editor-code',
                'features' => array(
                    __( 'CodeMirror-powered CSS editor with syntax highlighting', 'tnc-flipbook-3d-lite' ),
                    __( 'Global CSS applied inside every flipbook viewer', 'tnc-flipbook-3d-lite' ),
                    __( 'Per-flipbook CSS that loads after global styles', 'tnc-flipbook-3d-lite' ),
                ),
            );
            include TNC_FLIPBOOK_3D_DIR . 'admin/views/pro-teaser.php';
            ?>

            <!-- Danger Zone (Free) -->
            <details class="tncfb-accordion-item" style="border-color:#d63638;margin-top:2em;">
                <summary class="tncfb-accordion-header" style="color:#d63638;">
                    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#d63638" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="vertical-align:-3px;margin-right:4px;"><path d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg>
                    <?php esc_html_e( 'Danger Zone', 'tnc-flipbook-3d-lite' ); ?>
                </summary>
                <div class="tncfb-accordion-body">
                    <div style="padding:16px;background:#fef2f2;border:1px solid #fecaca;border-radius:8px;">
                        <label style="display:flex;align-items:flex-start;gap:8px;cursor:pointer;">
                            <input type="checkbox"
                                   name="<?php echo esc_attr( TNC_FLIPBOOK_3D_OPTION ); ?>[delete_data_on_uninstall]"
                                   value="1" <?php checked( tncfb3d_opt( $options, 'delete_data_on_uninstall', false ) ); ?>
                                   style="margin-top:2px;" />
                            <div>
                                <strong style="color:#991b1b;"><?php esc_html_e( 'Delete all data on uninstall', 'tnc-flipbook-3d-lite' ); ?></strong>
                                <p style="margin:4px 0 0;color:#7f1d1d;font-size:13px;">
                                    <?php esc_html_e( 'When enabled, deleting this plugin will permanently remove ALL flipbook posts, settings, and data. This cannot be undone. Leave unchecked to keep your data safe if you plan to reinstall later.', 'tnc-flipbook-3d-lite' ); ?>
                                </p>
                            </div>
                        </label>
                    </div>
                </div>
            </details>
        </div>

        <?php
        // Allow addons to render additional settings tab content
        $builtin_tabs = array( 'general', 'toolbar', 'appearance', 'protection', 'integrations', 'accessibility', 'advanced' );
        foreach ( $tabs as $stk => $stl ) {
            if ( ! in_array( $stk, $builtin_tabs, true ) ) {
                echo '<div id="tncfb-settings-' . esc_attr( $stk ) . '" class="tncfb-settings-section">';
                do_action( "tncfb3d_settings_tab_content_{$stk}", $options );
                echo '</div>';
            }
        }
        ?>

        <?php submit_button( __( 'Save Settings', 'tnc-flipbook-3d-lite' ) ); ?>
    </form>
</div><!-- .wrap -->
