<?php
/**
 * Admin Settings Page HTML & Telemetry Popup
 *
 * Renders the full settings interface with five tabs (General, Styles,
 * Login Attempts, Login History, Privacy) and the first-run telemetry
 * opt-in popup.
 *
 * @package AdminLoginGuardBranding
 */

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Render the plugin settings page.
 *
 * Outputs the complete admin settings interface including tab navigation,
 * form fields grouped by section, the sidebar with plugin promotions,
 * and the sticky save button.
 *
 * @since 1.0.0
 *
 * @return void
 */
function admilogu_settings_page()
{
    ?>
    <div class="wrap adminlgb-settings-wrap">
        <h1><?php esc_html_e('Admin Login Guard & Branding', 'admin-login-guard-branding'); ?></h1>
        <p class="adminlgb-intro">
            <?php esc_html_e('Secure your WordPress admin area with custom login slugs, detailed logs, and attempt limiting.', 'admin-login-guard-branding'); ?>
        </p>
        <div class="adminlgb-dashboard-layout">
            <aside class="adminlgb-sidebar-nav">
                <div class="adminlgb-nav-menu">
                    <div class="adminlgb-nav-menu-title">QUICK MENU</div>
                    <div class="nav-tab-wrapper adminlgb-nav-tab-wrapper">
                        <a href="#general" class="nav-tab nav-tab-active" data-tab="general">
                            <span class="dashicons dashicons-admin-settings"></span>
                            <div class="adminlgb-tab-info">
                                <span
                                    class="adminlgb-tab-title"><?php esc_html_e('General Settings', 'admin-login-guard-branding'); ?></span>
                                <span
                                    class="adminlgb-tab-text"><?php esc_html_e('Main Configuration', 'admin-login-guard-branding'); ?></span>
                            </div>
                        </a>
                        <a href="#styles" class="nav-tab" data-tab="styles">
                            <span class="dashicons dashicons-art"></span>
                            <div class="adminlgb-tab-info">
                                <span
                                    class="adminlgb-tab-title"><?php esc_html_e('Styles', 'admin-login-guard-branding'); ?></span>
                                <span
                                    class="adminlgb-tab-text"><?php esc_html_e('Customize Looks', 'admin-login-guard-branding'); ?></span>
                            </div>
                        </a>
                        <a href="#advanced" class="nav-tab" data-tab="advanced">
                            <span class="dashicons dashicons-editor-code"></span>
                            <div class="adminlgb-tab-info">
                                <span
                                    class="adminlgb-tab-title"><?php esc_html_e('Advanced', 'admin-login-guard-branding'); ?></span>
                                <span
                                    class="adminlgb-tab-text"><?php esc_html_e('Custom Code', 'admin-login-guard-branding'); ?></span>
                            </div>
                        </a>
                        <a href="#attempts" class="nav-tab" data-tab="attempts">
                            <span class="dashicons dashicons-shield"></span>
                            <div class="adminlgb-tab-info">
                                <span
                                    class="adminlgb-tab-title"><?php esc_html_e('Login Attempts', 'admin-login-guard-branding'); ?></span>
                                <span
                                    class="adminlgb-tab-text"><?php esc_html_e('Security Incidents', 'admin-login-guard-branding'); ?></span>
                            </div>
                        </a>
                        <a href="#loginfailed" class="nav-tab" data-tab="loginfailed">
                            <span class="dashicons dashicons-warning"></span>
                            <div class="adminlgb-tab-info">
                                <span
                                    class="adminlgb-tab-title"><?php esc_html_e('Failed Logins', 'admin-login-guard-branding'); ?></span>
                                <span
                                    class="adminlgb-tab-text"><?php esc_html_e('Security Audit', 'admin-login-guard-branding'); ?></span>
                            </div>
                        </a>
                        <a href="#loginsuccess" class="nav-tab" data-tab="loginsuccess">
                            <span class="dashicons dashicons-yes-alt"></span>
                            <div class="adminlgb-tab-info">
                                <span
                                    class="adminlgb-tab-title"><?php esc_html_e('Successful Logins', 'admin-login-guard-branding'); ?></span>
                                <span
                                    class="adminlgb-tab-text"><?php esc_html_e('Access Logs', 'admin-login-guard-branding'); ?></span>
                            </div>
                        </a>
                        <a href="#privacy" class="nav-tab" data-tab="privacy">
                            <span class="dashicons dashicons-privacy"></span>
                            <div class="adminlgb-tab-info">
                                <span
                                    class="adminlgb-tab-title"><?php esc_html_e('Privacy', 'admin-login-guard-branding'); ?></span>
                                <span
                                    class="adminlgb-tab-text"><?php esc_html_e('Data & Privacy', 'admin-login-guard-branding'); ?></span>
                            </div>
                        </a>
                    </div>
                </div>
                <div class="adminlgb-plugin-card adminlgb-plugin-card--highlight">
                    <div class="adminlgb-plugin-card__content">
                        <div class="adminlgb-plugin-thumb__logo--under">
                            <img src="<?php echo esc_url(admilogu_URL . 'assets/images/speedy-go.gif'); ?>"
                                alt="<?php esc_attr_e('Speedy Go', 'admin-login-guard-branding'); ?>">
                        </div>
                        <div class="adminlgb-plugin-card__head">
                            <h4><?php esc_html_e('SpeedyGo', 'admin-login-guard-branding'); ?></h4>
                            <span
                                class="adminlgb-plugin-badge adminlgb-plugin-badge--cta"><?php esc_html_e('Featured', 'admin-login-guard-branding'); ?></span>
                        </div>
                        <p><?php esc_html_e('Optimize your WordPress site performance with advanced caching and speed optimization tools.', 'admin-login-guard-branding'); ?>
                        </p>
                        <div class="adminlgb-plugin-card__cta">
                            <a href="https://wordpress.org/plugins/speedy-go/" target="_blank" rel="noopener noreferrer"
                                aria-label="View Speedy Go plugin" class="adminlgb-plugin-link button button-primary">
                                <?php esc_html_e('View Plugin', 'admin-login-guard-branding'); ?>
                            </a>
                        </div>
                    </div>
                </div>
            </aside>

            <main class="adminlgb-main-area">
                <form method="post" action="options.php" class="adminlgb-main-form">

                    <div class="adminlgb-actions-sticky">
                        <?php submit_button(__('Save Changes', 'admin-login-guard-branding'), 'primary', 'submit', false); ?>
                    </div>
                    <?php settings_fields('admilogu_settings_group'); ?>

                    <!-- General Settings -->
                    <div id="tab-general" class="adminlgb_tab-content adminlgb-section-card">
                        <div class="adminlgb-section-card-header">
                            <h2><?php esc_html_e('General Configuration', 'admin-login-guard-branding'); ?></h2>
                            <p class="description">
                                <?php esc_html_e('Setup your custom login URL and redirect behaviors.', 'admin-login-guard-branding'); ?>
                            </p>
                        </div>
                        <div class="adminlgb-section-body">
                            <table class="form-table">
                                <tr>
                                    <th><?php esc_html_e('Enable Custom Login URL', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <label class="adminlgb-toggle">
                                            <input type="hidden" name="admilogu_enable_custom_login" value="0">
                                            <input type="checkbox" name="admilogu_enable_custom_login" value="1"
                                                id="adminlgb_enable_custom_login" <?php checked(get_option('admilogu_enable_custom_login', 1), 1); ?>>
                                            <span class="adminlgb-toggle__slider"></span>
                                            <span
                                                class="adminlgb-toggle__label"><?php esc_html_e('Replace wp-login.php with a custom URL slug', 'admin-login-guard-branding'); ?></span>
                                        </label>
                                        <p class="description">
                                            <?php esc_html_e('When disabled, the default WordPress login URL (wp-login.php) will be used.', 'admin-login-guard-branding'); ?>
                                        </p>
                                    </td>
                                </tr>
                                <tr id="adminlgb_row-custom-slug">
                                    <th><?php esc_html_e('Custom Login URL Slug', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <div class="adminlgb-input-group">
                                            <?php
                                            $slug_value = get_option('admilogu_login_slug');
                                            $conflict_url = admilogu_check_login_url_conflict();
                                            if ($conflict_url) {
                                                // Extract slug from conflict URL
                                                $path = trim(wp_parse_url($conflict_url, PHP_URL_PATH), '/');
                                                $home_path = trim(wp_parse_url(home_url(), PHP_URL_PATH), '/');

                                                // Remove home path from slug if present
                                                if (!empty($home_path) && strpos($path, $home_path) === 0) {
                                                    $path = substr($path, strlen($home_path));
                                                    $path = trim($path, '/');
                                                }

                                                $slug_value = $path;
                                            }
                                            ?>
                                            <input type="text" name="admilogu_login_slug" id="adminlgb_login_slug"
                                                value="<?php echo esc_attr($slug_value); ?>" required>
                                            <button type="button" class="button button-primary"
                                                id="adminlgb_copy_login_url"><?php esc_html_e('Copy URL', 'admin-login-guard-branding'); ?></button>
                                        </div>
                                        <p class="description">
                                            <?php esc_html_e('New login URL:', 'admin-login-guard-branding'); ?>
                                            <code><?php echo esc_url(site_url()); ?>/<span id="adminlgb_slug_preview"><?php echo esc_html($slug_value); ?></span></code>
                                        </p>
                                    </td>
                                </tr>
                                <tr>
                                    <th><?php esc_html_e('Hide "Back to Home" Link', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <label class="adminlgb-toggle">
                                            <input type="checkbox" name="admilogu_hide_backtoblog" value="1" <?php checked(get_option('admilogu_hide_backtoblog'), 1); ?>>
                                            <span class="adminlgb-toggle__slider"></span>
                                            <span
                                                class="adminlgb-toggle__label"><?php esc_html_e('Hide the "Back to Home" link on login page', 'admin-login-guard-branding'); ?></span>
                                        </label>
                                    </td>
                                </tr>
                                <tr>
                                    <th><?php esc_html_e('Redirect on Failure', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <?php
                                        $selected_page = get_option('admilogu_fail_redirect_page', 0);
                                        wp_dropdown_pages(array(
                                            'name' => 'admilogu_fail_redirect_page',
                                            'show_option_none' => esc_html__('Default (404 Page)', 'admin-login-guard-branding'),
                                            'option_none_value' => '0',
                                            'selected' => absint($selected_page),
                                            'class' => 'regular-text'
                                        ));
                                        ?>
                                        <p class="description">
                                            <?php esc_html_e('Where to redirect users when access is denied.', 'admin-login-guard-branding'); ?>
                                        </p>
                                    </td>
                                </tr>
                                <tr>
                                    <th><?php esc_html_e('Shortcut Protection', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <label class="adminlgb-toggle">
                                            <input type="hidden" name="admilogu_block_shortcuts" value="0">
                                            <input type="checkbox" name="admilogu_block_shortcuts" value="1" <?php checked(get_option('admilogu_block_shortcuts', 1), 1); ?>>
                                            <span class="adminlgb-toggle__slider"></span>
                                            <span
                                                class="adminlgb-toggle__label"><?php esc_html_e('Block common shortcuts (/login, /admin, /dashboard)', 'admin-login-guard-branding'); ?></span>
                                        </label>
                                        <p class="description">
                                            <?php esc_html_e('Disable this if you have a custom user-facing login page on your site.', 'admin-login-guard-branding'); ?>
                                        </p>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>

                    <!-- Styles Settings -->
                    <div id="tab-styles" class="adminlgb_tab-content adminlgb-section-card" style="display:none;">
                        <div class="adminlgb-section-card-header">
                            <h2><?php esc_html_e('Visual Customization', 'admin-login-guard-branding'); ?></h2>
                            <p class="description">
                                <?php esc_html_e('Match the login page to your brand.', 'admin-login-guard-branding'); ?>
                            </p>
                        </div>
                        <div class="adminlgb-section-body">
                            <table class="form-table">

                                <tr id="adminlgb_row-admin-logo">
                                    <th><?php esc_html_e('Admin Login Logo', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <?php $logo_url = get_option('admilogu_logo_url'); ?>
                                        <div class="adminlgb-media-group">
                                            <div class="adminlgb-image-preview-wrapper <?php echo empty($logo_url) ? 'hidden' : ''; ?>"
                                                id="adminlgb_logo_preview_container">
                                                <div class="adminlgb-image-preview">
                                                    <img src="<?php echo esc_url($logo_url); ?>" alt="Logo Preview">
                                                </div>
                                            </div>

                                            <div class="adminlgb-media-actions">
                                                <input type="hidden" id="adminlgb_logo_url" name="admilogu_logo_url"
                                                    value="<?php echo esc_attr($logo_url); ?>">

                                                <button type="button" class="button adminlgb_media-upload"
                                                    data-target="#adminlgb_logo_url">
                                                    <?php esc_html_e('Select Image', 'admin-login-guard-branding'); ?>
                                                </button>

                                                <button type="button"
                                                    class="button adminlgb_media-remove <?php echo empty($logo_url) ? 'hidden' : ''; ?>"
                                                    data-target="#adminlgb_logo_url">
                                                    <?php esc_html_e('Remove', 'admin-login-guard-branding'); ?>
                                                </button>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                <!-- Hide Logo -->
                                <tr>
                                    <th><?php esc_html_e('Hide Admin Login Logo', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <label class="adminlgb-toggle">
                                            <input type="checkbox" name="admilogu_hide_logo" value="1" <?php checked(get_option('admilogu_hide_logo', 0), 1); ?>>
                                            <span class="adminlgb-toggle__slider"></span>
                                            <span
                                                class="adminlgb-toggle__label"><?php esc_html_e('Hide the logo completely', 'admin-login-guard-branding'); ?></span>
                                        </label>
                                    </td>
                                </tr>
                                <!-- Logo Dimensions -->
                                <tr id="adminlgb_row-logo-dimensions">
                                    <th><?php esc_html_e('Logo Dimensions', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <div class="adminlgb-grid-row">
                                            <div class="adminlgb-grid-item">
                                                <label><?php esc_html_e('Width', 'admin-login-guard-branding'); ?></label>
                                                <input type="text" name="admilogu_logo_width"
                                                    value="<?php echo esc_attr(get_option('admilogu_logo_width', '100%')); ?>">
                                                <p class="description">
                                                    <?php esc_html_e('Default: 100%', 'admin-login-guard-branding'); ?>
                                                </p>
                                            </div>
                                            <div class="adminlgb-grid-item">
                                                <label><?php esc_html_e('Height', 'admin-login-guard-branding'); ?></label>
                                                <input type="text" name="admilogu_logo_height"
                                                    value="<?php echo esc_attr(get_option('admilogu_logo_height', '80px')); ?>">
                                                <p class="description">
                                                    <?php esc_html_e('Default: 80px', 'admin-login-guard-branding'); ?>
                                                </p>
                                            </div>
                                        </div>
                                    </td>
                                </tr>

                                <!-- Logo Details -->
                                <tr id="adminlgb_row-logo-details">
                                    <th><?php esc_html_e('Logo Details', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <div class="adminlgb-grid-row">
                                            <div class="adminlgb-grid-item">
                                                <label><?php esc_html_e('Title Attribute', 'admin-login-guard-branding'); ?></label>
                                                <input type="text" name="admilogu_logo_title" class="regular-text"
                                                    value="<?php echo esc_attr(get_option('admilogu_logo_title', '')); ?>">
                                            </div>
                                            <div class="adminlgb-grid-item">
                                                <label><?php esc_html_e('Link URL', 'admin-login-guard-branding'); ?></label>
                                                <input type="url" name="admilogu_logo_link" class="regular-text"
                                                    value="<?php echo esc_url(get_option('admilogu_logo_link', 'https://wordpress.org/')); ?>">
                                            </div>
                                        </div>
                                    </td>
                                </tr>

                                <tr id="adminlgb_row-logo-link-target">
                                    <th><?php esc_html_e('Open Link in New Tab', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <label class="adminlgb-toggle">
                                            <input type="checkbox" name="admilogu_logo_target_blank" value="1" <?php checked(get_option('admilogu_logo_target_blank', 1), 1); ?>>
                                            <span class="adminlgb-toggle__slider"></span>
                                            <span
                                                class="adminlgb-toggle__label"><?php esc_html_e('Open logo link in a new tab', 'admin-login-guard-branding'); ?></span>
                                        </label>
                                    </td>
                                </tr>

                                <?php $bg_type = get_option('admilogu_bg_type'); ?>
                                <tr>
                                    <th><?php esc_html_e('Background Type', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <div class="adminlgb-radio-group">
                                            <label class="<?php echo $bg_type === 'image' ? 'active' : ''; ?>">
                                                <input type="radio" name="admilogu_bg_type" value="image" <?php checked($bg_type, 'image'); ?>>
                                                <?php esc_html_e('Image', 'admin-login-guard-branding'); ?>
                                            </label>
                                            <label class="<?php echo $bg_type === 'video' ? 'active' : ''; ?>">
                                                <input type="radio" name="admilogu_bg_type" value="video" <?php checked($bg_type, 'video'); ?>>
                                                <?php esc_html_e('Video', 'admin-login-guard-branding'); ?>
                                            </label>
                                            <label class="<?php echo $bg_type === 'color' ? 'active' : ''; ?>">
                                                <input type="radio" name="admilogu_bg_type" value="color" <?php checked($bg_type, 'color'); ?>>
                                                <?php esc_html_e('Color', 'admin-login-guard-branding'); ?>
                                            </label>
                                        </div>
                                    </td>
                                </tr>

                                <tr id="adminlgb_row-bg-image">
                                    <th><?php esc_html_e('Background Image', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <?php $bg_url = get_option('admilogu_bg_image'); ?>
                                        <div class="adminlgb-media-group">
                                            <div class="adminlgb-image-preview-wrapper <?php echo empty($bg_url) ? 'hidden' : ''; ?>"
                                                id="adminlgb_bg_image_preview_container">
                                                <div class="adminlgb-image-preview">
                                                    <img src="<?php echo esc_url($bg_url); ?>" alt="Background Preview">
                                                </div>
                                            </div>

                                            <div class="adminlgb-media-actions">
                                                <input type="hidden" id="adminlgb_bg_image" name="admilogu_bg_image"
                                                    value="<?php echo esc_attr($bg_url); ?>">

                                                <button type="button" class="button adminlgb_media-upload"
                                                    data-target="#adminlgb_bg_image">
                                                    <?php esc_html_e('Select Image', 'admin-login-guard-branding'); ?>
                                                </button>

                                                <button type="button"
                                                    class="button adminlgb_media-remove <?php echo empty($bg_url) ? 'hidden' : ''; ?>"
                                                    data-target="#adminlgb_bg_image">
                                                    <?php esc_html_e('Remove', 'admin-login-guard-branding'); ?>
                                                </button>
                                            </div>
                                        </div>
                                    </td>
                                </tr>

                                <tr id="adminlgb_row-bg-video">
                                    <th><?php esc_html_e('Background Video', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <?php $video_url = get_option('admilogu_bg_video'); ?>
                                        <div class="adminlgb-media-group">
                                            <div class="adminlgb-video-preview-wrapper <?php echo empty($video_url) ? 'hidden' : ''; ?>"
                                                id="adminlgb_bg_video_preview_container">
                                                <div class="adminlgb-video-preview">
                                                    <video src="<?php echo esc_url($video_url); ?>" controls
                                                        style="max-width:300px;"></video>
                                                </div>
                                            </div>

                                            <div class="adminlgb-media-actions">
                                                <input type="hidden" id="adminlgb_bg_video" name="admilogu_bg_video"
                                                    value="<?php echo esc_attr($video_url); ?>">

                                                <button type="button" class="button adminlgb_media-upload" data-type="video"
                                                    data-target="#adminlgb_bg_video">
                                                    <?php esc_html_e('Select Video', 'admin-login-guard-branding'); ?>
                                                </button>

                                                <button type="button"
                                                    class="button adminlgb_media-remove <?php echo empty($video_url) ? 'hidden' : ''; ?>"
                                                    data-target="#adminlgb_bg_video">
                                                    <?php esc_html_e('Remove', 'admin-login-guard-branding'); ?>
                                                </button>
                                            </div>
                                        </div>
                                        <p class="description">
                                            <strong><?php esc_html_e('Recommendations:', 'admin-login-guard-branding'); ?></strong><br>
                                        <ul>
                                            <li><?php esc_html_e('Use MP4 format with H.264 codec for best compatibility', 'admin-login-guard-branding'); ?>
                                            </li>
                                            <li><?php esc_html_e('Keep file size under 5MB for optimal loading speed', 'admin-login-guard-branding'); ?>
                                            </li>
                                            <li><?php esc_html_e('Recommended resolution: 1920x1080 (Full HD) or lower', 'admin-login-guard-branding'); ?>
                                            </li>
                                            <li><?php esc_html_e('Use short loops (5-15 seconds) for seamless playback', 'admin-login-guard-branding'); ?>
                                            </li>
                                            <li><?php esc_html_e('Compress videos using tools like HandBrake or FFmpeg', 'admin-login-guard-branding'); ?>
                                            </li>
                                        </ul>
                                        </p>
                                    </td>
                                </tr>

                                <tr id="adminlgb_row-bg-video-mute">
                                    <th><?php esc_html_e('Mute Video', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <label class="adminlgb-toggle">
                                            <input type="checkbox" name="admilogu_bg_video_mute" value="1" <?php checked(get_option('admilogu_bg_video_mute', 1), 1); ?>>
                                            <span class="adminlgb-toggle__slider"></span>
                                            <span
                                                class="adminlgb-toggle__label"><?php esc_html_e('Mute the background video', 'admin-login-guard-branding'); ?></span>
                                        </label>
                                        <p class="description">
                                            <?php esc_html_e('Recommended: Keep muted for better user experience. Most browsers block autoplay with sound.', 'admin-login-guard-branding'); ?>
                                        </p>
                                    </td>
                                </tr>

                                <tr id="adminlgb_row-bg-color">
                                    <th><?php esc_html_e('Background Color', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <input type="text" name="admilogu_bg_color" class="adminlgb_color-picker"
                                            value="<?php echo esc_attr(get_option('admilogu_bg_color')); ?>"
                                            data-default-color="#f0f0f1">
                                    </td>
                                </tr>
                                <tr>
                                    <th><?php esc_html_e('Vertically Center Form', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <label class="adminlgb-toggle">
                                            <input type="checkbox" name="admilogu_center_form" value="1" <?php checked(get_option('admilogu_center_form', 0), 1); ?>>
                                            <span class="adminlgb-toggle__slider"></span>
                                            <span
                                                class="adminlgb-toggle__label"><?php esc_html_e('Center the login form vertically on the page', 'admin-login-guard-branding'); ?></span>
                                        </label>
                                    </td>
                                </tr>
                                <tr>
                                    <th><?php esc_html_e('Custom Google Font', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <select name="admilogu_custom_font">
                                            <option value="" <?php selected(get_option('admilogu_custom_font'), ''); ?>>
                                                <?php esc_html_e('System Default', 'admin-login-guard-branding'); ?>
                                            </option>
                                            <option value="Inter" <?php selected(get_option('admilogu_custom_font'), 'Inter'); ?>>Inter</option>
                                            <option value="Roboto" <?php selected(get_option('admilogu_custom_font'), 'Roboto'); ?>>Roboto</option>
                                            <option value="Open Sans" <?php selected(get_option('admilogu_custom_font'), 'Open Sans'); ?>>Open Sans</option>
                                            <option value="Poppins" <?php selected(get_option('admilogu_custom_font'), 'Poppins'); ?>>Poppins</option>
                                            <option value="Montserrat" <?php selected(get_option('admilogu_custom_font'), 'Montserrat'); ?>>Montserrat</option>
                                        </select>
                                    </td>
                                </tr>
                            </table>

                            <div class="adminlgb-colors-grid">
                                <div class="adminlgb-color-item">
                                    <label><?php esc_html_e('Form Background', 'admin-login-guard-branding'); ?></label>
                                    <input type="text" name="admilogu_loginform_bg_color" class="adminlgb_color-picker"
                                        value="<?php echo esc_attr(get_option('admilogu_loginform_bg_color')); ?>"
                                        data-default-color="#ffffff">
                                </div>
                                <div class="adminlgb-color-item">
                                    <label><?php esc_html_e('Form Border', 'admin-login-guard-branding'); ?></label>
                                    <input type="text" name="admilogu_loginform_border_color" class="adminlgb_color-picker"
                                        value="<?php echo esc_attr(get_option('admilogu_loginform_border_color')); ?>"
                                        data-default-color="#c3c4c7">
                                </div>
                                <div class="adminlgb-color-item">
                                    <label><?php esc_html_e('Form Labels', 'admin-login-guard-branding'); ?></label>
                                    <input type="text" name="admilogu_loginform_label_color" class="adminlgb_color-picker"
                                        value="<?php echo esc_attr(get_option('admilogu_loginform_label_color')); ?>"
                                        data-default-color="#3c434a">
                                </div>
                                <div class="adminlgb-color-item">
                                    <label><?php esc_html_e('Links Color', 'admin-login-guard-branding'); ?></label>
                                    <input type="text" name="admilogu_lostpwd_color" class="adminlgb_color-picker"
                                        value="<?php echo esc_attr(get_option('admilogu_lostpwd_color')); ?>"
                                        data-default-color="#2271b1">
                                </div>
                                <div class="adminlgb-color-item">
                                    <label><?php esc_html_e('Links Hover', 'admin-login-guard-branding'); ?></label>
                                    <input type="text" name="admilogu_lostpwd_hover_color" class="adminlgb_color-picker"
                                        value="<?php echo esc_attr(get_option('admilogu_lostpwd_hover_color')); ?>"
                                        data-default-color="#135e96">
                                </div>
                                <div class="adminlgb-color-item">
                                    <label><?php esc_html_e('"Back to Home" Color', 'admin-login-guard-branding'); ?></label>
                                    <input type="text" name="admilogu_backtoblog_color" class="adminlgb_color-picker"
                                        value="<?php echo esc_attr(get_option('admilogu_backtoblog_color')); ?>"
                                        data-default-color="#2271b1">
                                </div>
                                <div class="adminlgb-color-item">
                                    <label><?php esc_html_e('"Back to Home" Hover', 'admin-login-guard-branding'); ?></label>
                                    <input type="text" name="admilogu_backtoblog_hover_color" class="adminlgb_color-picker"
                                        value="<?php echo esc_attr(get_option('admilogu_backtoblog_hover_color')); ?>"
                                        data-default-color="#135e96">
                                </div>
                                <div class="adminlgb-color-item">
                                    <label><?php esc_html_e('Admin Logo Background', 'admin-login-guard-branding'); ?></label>
                                    <input type="text" name="admilogu_logo_bg_color" class="adminlgb_color-picker"
                                        value="<?php echo esc_attr(get_option('admilogu_logo_bg_color')); ?>"
                                        data-default-color="">
                                </div>
                                <div class="adminlgb-color-item">
                                    <label><?php esc_html_e('Button Background', 'admin-login-guard-branding'); ?></label>
                                    <input type="text" name="admilogu_btn_bg_color" class="adminlgb_color-picker"
                                        value="<?php echo esc_attr(get_option('admilogu_btn_bg_color')); ?>"
                                        data-default-color="#2271b1">
                                </div>
                                <div class="adminlgb-color-item">
                                    <label><?php esc_html_e('Button Text', 'admin-login-guard-branding'); ?></label>
                                    <input type="text" name="admilogu_btn_text_color" class="adminlgb_color-picker"
                                        value="<?php echo esc_attr(get_option('admilogu_btn_text_color')); ?>"
                                        data-default-color="#ffffff">
                                </div>
                                <div class="adminlgb-color-item">
                                    <label><?php esc_html_e('Button Bg Hover', 'admin-login-guard-branding'); ?></label>
                                    <input type="text" name="admilogu_btn_bg_hover_color" class="adminlgb_color-picker"
                                        value="<?php echo esc_attr(get_option('admilogu_btn_bg_hover_color')); ?>"
                                        data-default-color="#135e96">
                                </div>
                                <div class="adminlgb-color-item">
                                    <label><?php esc_html_e('Button Text Hover', 'admin-login-guard-branding'); ?></label>
                                    <input type="text" name="admilogu_btn_text_hover_color" class="adminlgb_color-picker"
                                        value="<?php echo esc_attr(get_option('admilogu_btn_text_hover_color')); ?>"
                                        data-default-color="#ffffff">
                                </div>
                            </div>

                        </div>
                    </div>

                    <!-- Advanced Settings -->
                    <div id="tab-advanced" class="adminlgb_tab-content adminlgb-section-card" style="display:none;">
                        <div class="adminlgb-section-card-header">
                            <h2><?php esc_html_e('Advanced Customizations', 'admin-login-guard-branding'); ?></h2>
                            <p class="description">
                                <?php esc_html_e('Inject custom code and footer text. For advanced users only.', 'admin-login-guard-branding'); ?>
                            </p>
                        </div>
                        <div class="adminlgb-section-body">
                            <table class="form-table">
                                <tr>
                                    <th scope="row">
                                        <label
                                            for="admilogu_custom_footer"><?php esc_html_e('Custom Login Footer', 'admin-login-guard-branding'); ?></label>
                                    </th>
                                    <td>
                                        <?php wp_editor(get_option('admilogu_custom_footer'), 'admilogu_custom_footer', array('textarea_name' => 'admilogu_custom_footer', 'textarea_rows' => 6, 'media_buttons' => false)); ?>
                                        <p class="description" style="margin-top: 5px;">
                                            <?php esc_html_e('Supports HTML. Displayed at the very bottom of the login page.', 'admin-login-guard-branding'); ?>
                                        </p>
                                    </td>
                                </tr>
                                <tr>
                                    <th scope="row">
                                        <label
                                            for="admilogu_custom_css"><?php esc_html_e('Custom CSS', 'admin-login-guard-branding'); ?></label>
                                    </th>
                                    <td>
                                        <textarea name="admilogu_custom_css" id="admilogu_custom_css" rows="8"
                                            class="large-text code"
                                            style="font-family: monospace; background: #f6f7f7; border: 1px solid #c3c4c7; padding: 10px; border-radius: 4px;"><?php echo esc_textarea(get_option('admilogu_custom_css')); ?></textarea>
                                        <p class="description">
                                            <?php esc_html_e('Add your own CSS rules. Do not include <style> tags.', 'admin-login-guard-branding'); ?>
                                        </p>
                                    </td>
                                </tr>
                                <tr>
                                    <th scope="row">
                                        <label
                                            for="admilogu_custom_js"><?php esc_html_e('Custom JS', 'admin-login-guard-branding'); ?></label>
                                    </th>
                                    <td>
                                        <textarea name="admilogu_custom_js" id="admilogu_custom_js" rows="8"
                                            class="large-text code"
                                            style="font-family: monospace; background: #f6f7f7; border: 1px solid #c3c4c7; padding: 10px; border-radius: 4px;"><?php echo esc_textarea(get_option('admilogu_custom_js')); ?></textarea>
                                        <p class="description">
                                            <?php esc_html_e('Add your own JavaScript. Do not include <script> tags.', 'admin-login-guard-branding'); ?>
                                        </p>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>

                    <!-- Attempts Settings -->
                    <div id="tab-attempts" class="adminlgb_tab-content adminlgb-section-card" style="display:none;">
                        <div class="adminlgb-section-card-header">
                            <h2><?php esc_html_e('Security Policies', 'admin-login-guard-branding'); ?></h2>
                            <p class="description">
                                <?php esc_html_e('Configure lockout rules to prevent brute force attacks.', 'admin-login-guard-branding'); ?>
                            </p>
                        </div>
                        <div class="adminlgb-section-body">
                            <table class="form-table">
                                <tr>
                                    <th><?php esc_html_e('Two-Factor Authentication (2FA)', 'admin-login-guard-branding'); ?>
                                    </th>
                                    <td>
                                        <label class="adminlgb-toggle">
                                            <input type="hidden" name="admilogu_enable_2fa" value="0">
                                            <input type="checkbox" name="admilogu_enable_2fa" value="1" <?php checked(get_option('admilogu_enable_2fa', 0), 1); ?>>
                                            <span class="adminlgb-toggle__slider"></span>
                                            <span
                                                class="adminlgb-toggle__label"><?php esc_html_e('Enable Two-Factor Authentication', 'admin-login-guard-branding'); ?></span>
                                        </label>
                                        <p class="description">
                                            <?php esc_html_e('When enabled, users can opt-in to require an email verification code upon login from their Profile settings.', 'admin-login-guard-branding'); ?>
                                        </p>
                                    </td>
                                </tr>

                                <tr>
                                    <th><?php esc_html_e('Max Login Attempts', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <input type="number" name="admilogu_max_attempts" min="1" step="1"
                                            value="<?php echo esc_attr(get_option('admilogu_max_attempts')); ?>">
                                        <p class="description">
                                            <?php esc_html_e('Number of allowed failed attempts before lockout.', 'admin-login-guard-branding'); ?>
                                        </p>
                                    </td>
                                </tr>

                                <tr>
                                    <th><?php esc_html_e('Lockout Duration (minutes)', 'admin-login-guard-branding'); ?>
                                    </th>
                                    <td>
                                        <input type="number" name="admilogu_lockout_duration" min="1" step="1"
                                            value="<?php echo esc_attr(get_option('admilogu_lockout_duration')); ?>">
                                        <p class="description">
                                            <?php esc_html_e('How long the user will be locked out after exceeding max attempts.', 'admin-login-guard-branding'); ?>
                                        </p>
                                    </td>
                                </tr>

                                <tr>
                                    <th><?php esc_html_e('IP Whitelist', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <textarea name="admilogu_ip_whitelist" rows="5" cols="50" class="large-text"
                                            placeholder="127.0.0.1"><?php echo esc_textarea(get_option('admilogu_ip_whitelist')); ?></textarea>
                                        <p class="description">
                                            <?php esc_html_e('Enter IP addresses to whitelist, one per line. Whitelisted IPs will never be locked out.', 'admin-login-guard-branding'); ?>
                                        </p>
                                    </td>
                                </tr>

                                <tr>
                                    <th><?php esc_html_e('IP Blacklist', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <textarea name="admilogu_ip_blacklist" rows="5" cols="50" class="large-text"
                                            placeholder="192.168.1.1"><?php echo esc_textarea(get_option('admilogu_ip_blacklist')); ?></textarea>
                                        <p class="description">
                                            <?php esc_html_e('Enter IP addresses to blacklist permanently, one per line. Blacklisted IPs will never be allowed to access the login page.', 'admin-login-guard-branding'); ?>
                                        </p>
                                    </td>
                                </tr>

                                <tr>
                                    <th><?php esc_html_e('Admin Lockout Alert', 'admin-login-guard-branding'); ?></th>
                                    <td>
                                        <label class="adminlgb-toggle">
                                            <input type="hidden" name="admilogu_notify_admin_lockout" value="0">
                                            <input type="checkbox" name="admilogu_notify_admin_lockout" value="1" <?php checked(get_option('admilogu_notify_admin_lockout', 0), 1); ?>>
                                            <span class="adminlgb-toggle__slider"></span>
                                            <span
                                                class="adminlgb-toggle__label"><?php esc_html_e('Send email to admin when an IP is locked out', 'admin-login-guard-branding'); ?></span>
                                        </label>
                                    </td>
                                </tr>

                                <tr>
                                    <th><?php esc_html_e('New IP Login Alert (Users)', 'admin-login-guard-branding'); ?>
                                    </th>
                                    <td>
                                        <label class="adminlgb-toggle">
                                            <input type="hidden" name="admilogu_notify_user_new_ip" value="0">
                                            <input type="checkbox" name="admilogu_notify_user_new_ip" value="1" <?php checked(get_option('admilogu_notify_user_new_ip', 0), 1); ?>>
                                            <span class="adminlgb-toggle__slider"></span>
                                            <span
                                                class="adminlgb-toggle__label"><?php esc_html_e('Send email to users when they log in from a new IP address', 'admin-login-guard-branding'); ?></span>
                                        </label>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>

                    <!-- Failed Logins -->
                    <div id="tab-loginfailed" class="adminlgb_tab-content adminlgb-section-card" style="display:none;">
                        <div class="adminlgb-section-card-header">
                            <h2><?php esc_html_e('Failed Login History', 'admin-login-guard-branding'); ?></h2>
                            <p class="description">
                                <?php esc_html_e('Audit log of all failed login attempts.', 'admin-login-guard-branding'); ?>
                            </p>
                        </div>
                        <div class="adminlgb-section-body" style="padding-top: 20px;">
                            <?php admilogu_history_page('failed'); ?>
                        </div>
                    </div>

                    <!-- Successful Logins -->
                    <div id="tab-loginsuccess" class="adminlgb_tab-content adminlgb-section-card" style="display:none;">
                        <div class="adminlgb-section-card-header">
                            <h2><?php esc_html_e('Successful Logins', 'admin-login-guard-branding'); ?></h2>
                            <p class="description">
                                <?php esc_html_e('Audit log of all successful logins.', 'admin-login-guard-branding'); ?>
                            </p>
                        </div>
                        <div class="adminlgb-section-body" style="padding-top: 20px;">
                            <?php admilogu_history_page('success'); ?>
                        </div>
                    </div>

                    <!-- Privacy & Telemetry -->
                    <div id="tab-privacy" class="adminlgb_tab-content adminlgb-section-card" style="display:none;">
                        <div class="adminlgb-section-card-header">
                            <h2><?php esc_html_e('Privacy & Telemetry', 'admin-login-guard-branding'); ?></h2>
                            <p class="description">
                                <?php esc_html_e('Manage your diagnostic data sharing preferences.', 'admin-login-guard-branding'); ?>
                            </p>
                        </div>
                        <div class="adminlgb-section-body">
                            <div class="adminlgb-telemetry-container">
                                <p class="adminlgb-telemetry-main-label">
                                    <strong><?php esc_html_e('Telemetry Preference:', 'admin-login-guard-branding'); ?></strong>
                                </p>

                                <?php $optin = get_option('admilogu_tracking_optin', 'no'); ?>

                                <div class="adminlgb-telemetry-cards">
                                    <label
                                        class="adminlgb-telemetry-card <?php echo $optin === 'yes' ? 'is-active' : ''; ?>">
                                        <input type="radio" name="admilogu_tracking_optin" value="yes" <?php checked('yes', $optin); ?>>
                                        <div class="adminlgb-telemetry-card-content">
                                            <div class="adminlgb-telemetry-card-header">
                                                <span class="adminlgb-telemetry-radio-custom"></span>
                                                <div class="adminlgb-telemetry-card-titles">
                                                    <strong><?php esc_html_e('Opt in (recommended)', 'admin-login-guard-branding'); ?></strong>
                                                    <p><?php esc_html_e('Help us prioritize compatibility updates.', 'admin-login-guard-branding'); ?>
                                                    </p>
                                                </div>
                                            </div>
                                            <div class="adminlgb-telemetry-card-divider"></div>
                                            <div class="adminlgb-telemetry-card-footer">
                                                <p><?php esc_html_e('Enabling this sends minimal anonymous diagnostics. No content or personal info is collected.', 'admin-login-guard-branding'); ?>
                                                </p>
                                            </div>
                                        </div>
                                    </label>

                                    <label
                                        class="adminlgb-telemetry-card <?php echo $optin === 'no' ? 'is-active' : ''; ?>">
                                        <input type="radio" name="admilogu_tracking_optin" value="no" <?php checked('no', $optin); ?>>
                                        <div class="adminlgb-telemetry-card-content">
                                            <div class="adminlgb-telemetry-card-header">
                                                <span class="adminlgb-telemetry-radio-custom"></span>
                                                <div class="adminlgb-telemetry-card-titles">
                                                    <strong><?php esc_html_e('Opt out', 'admin-login-guard-branding'); ?></strong>
                                                    <p><?php esc_html_e('We will never collect diagnostics from this site.', 'admin-login-guard-branding'); ?>
                                                    </p>
                                                </div>
                                            </div>
                                            <div class="adminlgb-telemetry-card-divider"></div>
                                            <div class="adminlgb-telemetry-card-footer">
                                                <p><?php esc_html_e('Opting out ensures no telemetry is sent. You can opt back in at any time from this page.', 'admin-login-guard-branding'); ?>
                                                </p>
                                            </div>
                                        </div>
                                    </label>
                                </div>
                            </div>
                        </div>
                    </div>
        </div>
    </div>

    </form>
    </main>
    </div>
    </div>
    <?php
}

/**
 * Render the telemetry opt-in popup modal.
 *
 * Displayed once after plugin activation via the `admin_footer` hook.
 * Offers the user a choice to allow or deny anonymous diagnostic data
 * collection. The choice is processed via AJAX by the telemetry module.
 *
 * @since 1.0.0
 *
 * @return void
 */
function admilogu_render_telemetry_popup()
{
    ?>
    <div class="adminlgb-telemetry-modal">
        <div class="adminlgb-telemetry-content">
            <span
                class="adminlgb-telemetry-badge"><?php esc_html_e('Help Improve This Plugin', 'admin-login-guard-branding'); ?></span>
            <h2><?php esc_html_e('Share Anonymous Diagnostics', 'admin-login-guard-branding'); ?></h2>
            <p class="adminlgb-telemetry-intro">
                <?php esc_html_e('Help us prioritize compatibility updates by sharing anonymous site diagnostics. No personal data is collected.', 'admin-login-guard-branding'); ?>
            </p>

            <div class="adminlgb-telemetry-collect">
                <h3><?php esc_html_e('We collect:', 'admin-login-guard-branding'); ?></h3>
                <ul class="adminlgb-telemetry-list">
                    <li><?php esc_html_e('WordPress, PHP, and plugin version numbers', 'admin-login-guard-branding'); ?>
                    </li>
                    <li><?php esc_html_e('Theme name/version and locale', 'admin-login-guard-branding'); ?></li>
                    <li><?php esc_html_e('Multisite status and a hashed site identifier', 'admin-login-guard-branding'); ?>
                    </li>
                </ul>
            </div>

            <p class="adminlgb-telemetry-note">
                <?php esc_html_e('No personal data or content is collected. You can opt-out anytime from Settings.', 'admin-login-guard-branding'); ?>
            </p>

            <div class="adminlgb-telemetry-actions">
                <button type="button"
                    class="adminlgb-telemetry-btn adminlgb-telemetry-btn--allow adminlgb-js-telemetry-allow">
                    <?php esc_html_e('Allow & Continue', 'admin-login-guard-branding'); ?>
                    <span class="adminlgb-telemetry-loader"><span class="adminlgb-spinner"></span></span>
                </button>
                <button type="button"
                    class="adminlgb-telemetry-btn adminlgb-telemetry-btn--deny adminlgb-js-telemetry-deny">
                    <?php esc_html_e('Don\'t want to improve', 'admin-login-guard-branding'); ?>
                    <span class="adminlgb-telemetry-loader"><span class="adminlgb-spinner"></span></span>
                </button>
            </div>
        </div>
    </div>
    <?php
}
