<?php
/**
 * Admin interface for ArkHost Security Pack.
 *
 * @package ArkHost_Security_Pack
 */

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Admin class for settings page and UI.
 */
class ARKSP_Admin {

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
		add_action( 'admin_init', array( $this, 'register_settings' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) );
		add_action( 'wp_dashboard_setup', array( $this, 'add_dashboard_widget' ) );
		add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 );
		add_filter( 'plugin_action_links_' . ARKSP_PLUGIN_BASENAME, array( $this, 'plugin_action_links' ) );
		add_filter( 'submenu_file', array( $this, 'highlight_submenu' ) );

		// AJAX handlers.
		add_action( 'wp_ajax_arksp_clear_logs', array( $this, 'ajax_clear_logs' ) );
		add_action( 'wp_ajax_arksp_export_logs', array( $this, 'ajax_export_logs' ) );
		add_action( 'wp_ajax_arksp_whitelist_ip', array( $this, 'ajax_whitelist_ip' ) );
		add_action( 'wp_ajax_arksp_unblock_ip', array( $this, 'ajax_unblock_ip' ) );
		add_action( 'wp_ajax_arksp_clear_lockouts', array( $this, 'ajax_clear_lockouts' ) );
		add_action( 'wp_ajax_arksp_run_file_scan', array( $this, 'ajax_run_file_scan' ) );
		add_action( 'wp_ajax_arksp_run_malware_scan', array( $this, 'ajax_run_malware_scan' ) );
		add_action( 'wp_ajax_arksp_reset_file_baseline', array( $this, 'ajax_reset_file_baseline' ) );
		add_action( 'wp_ajax_arksp_clear_malware_results', array( $this, 'ajax_clear_malware_results' ) );
		add_action( 'wp_ajax_arksp_download_geo_db', array( $this, 'ajax_download_geo_db' ) );
		add_action( 'wp_ajax_arksp_export_settings', array( $this, 'ajax_export_settings' ) );
		add_action( 'wp_ajax_arksp_import_settings', array( $this, 'ajax_import_settings' ) );
		add_action( 'wp_ajax_arksp_reset_settings', array( $this, 'ajax_reset_settings' ) );
		add_action( 'wp_ajax_arksp_test_email', array( $this, 'ajax_test_email' ) );
		add_action( 'wp_ajax_arksp_force_logout_all', array( $this, 'ajax_force_logout_all' ) );
		add_action( 'wp_ajax_arksp_quarantine_file', array( $this, 'ajax_quarantine_file' ) );
		add_action( 'wp_ajax_arksp_restore_file', array( $this, 'ajax_restore_file' ) );
		add_action( 'wp_ajax_arksp_delete_quarantined', array( $this, 'ajax_delete_quarantined' ) );
		add_action( 'wp_ajax_arksp_delete_wp_file', array( $this, 'ajax_delete_wp_file' ) );
	}

	/**
	 * Add admin menu.
	 */
	public function add_admin_menu() {
		// Main menu item.
		add_menu_page(
			__( 'ArkHost Security Pack', 'arkhost-security-pack' ),
			__( 'Security', 'arkhost-security-pack' ),
			'manage_options',
			'arkhost-security-pack',
			array( $this, 'render_settings_page' ),
			'dashicons-shield-alt',
			80
		);

		// Submenus for each tab.
		$submenus = array(
			'status'    => __( 'Status', 'arkhost-security-pack' ),
			'login'     => __( 'Login Protection', 'arkhost-security-pack' ),
			'ip'        => __( 'IP Control', 'arkhost-security-pack' ),
			'hardening' => __( 'Hardening', 'arkhost-security-pack' ),
			'headers'   => __( 'Security Headers', 'arkhost-security-pack' ),
			'2fa'       => __( '2FA', 'arkhost-security-pack' ),
			'scanner'   => __( 'Scanner', 'arkhost-security-pack' ),
			'logs'      => __( 'Activity Log', 'arkhost-security-pack' ),
			'tools'     => __( 'Tools', 'arkhost-security-pack' ),
		);

		foreach ( $submenus as $slug => $title ) {
			add_submenu_page(
				'arkhost-security-pack',
				$title . ' - ' . __( 'ArkHost Security Pack', 'arkhost-security-pack' ),
				$title,
				'manage_options',
				'status' === $slug ? 'arkhost-security-pack' : 'arkhost-security-pack&tab=' . $slug,
				array( $this, 'render_settings_page' )
			);
		}
	}

	/**
	 * Highlight the correct submenu based on current tab.
	 *
	 * @param string $submenu_file The current submenu file.
	 * @return string
	 */
	public function highlight_submenu( $submenu_file ) {
		global $pagenow;

		if ( 'admin.php' !== $pagenow ) {
			return $submenu_file;
		}

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';

		if ( 'arkhost-security-pack' !== $page ) {
			return $submenu_file;
		}

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$tab = isset( $_GET['tab'] ) ? sanitize_text_field( wp_unslash( $_GET['tab'] ) ) : 'status';

		if ( 'status' === $tab ) {
			return 'arkhost-security-pack';
		}

		return 'arkhost-security-pack&tab=' . $tab;
	}

	/**
	 * Add plugin action links (left side - Settings, Deactivate, etc.).
	 *
	 * @param array $links Existing links.
	 * @return array
	 */
	public function plugin_action_links( $links ) {
		$settings_link = sprintf(
			'<a href="%s">%s</a>',
			admin_url( 'admin.php?page=arkhost-security-pack' ),
			__( 'Settings', 'arkhost-security-pack' )
		);
		array_unshift( $links, $settings_link );
		return $links;
	}

	/**
	 * Add plugin row meta links (right side - after version).
	 *
	 * @param array  $links Existing meta links.
	 * @param string $file  Plugin file.
	 * @return array
	 */
	public function plugin_row_meta( $links, $file ) {
		if ( ARKSP_PLUGIN_BASENAME !== $file ) {
			return $links;
		}

		$links[] = sprintf(
			'<a href="%s" target="_blank">%s</a>',
			'https://arkhost.com/products-menu.php',
			__( 'Get Hosting', 'arkhost-security-pack' )
		);

		return $links;
	}

	/**
	 * Add dashboard widget.
	 */
	public function add_dashboard_widget() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		wp_add_dashboard_widget(
			'arksp_security_widget',
			__( 'Security Status', 'arkhost-security-pack' ),
			array( $this, 'render_dashboard_widget' )
		);
	}

	/**
	 * Render dashboard widget.
	 */
	public function render_dashboard_widget() {
		$checks        = $this->get_security_checks();
		$enabled_count = 0;
		$total_count   = count( $checks );

		foreach ( $checks as $check ) {
			if ( $check['status'] ) {
				$enabled_count++;
			}
		}

		$score = $total_count > 0 ? round( ( $enabled_count / $total_count ) * 100 ) : 0;

		// Get recent activity stats.
		$stats = ARKSP_Activity_Log::get_stats( 7 );
		?>
		<div class="arksp-widget-score <?php echo $score >= 80 ? 'good' : ( $score >= 50 ? 'warning' : 'bad' ); ?>">
			<div class="arksp-widget-score-value"><?php echo esc_html( $score ); ?>%</div>
			<div class="arksp-widget-score-label"><?php esc_html_e( 'Security Score', 'arkhost-security-pack' ); ?></div>
		</div>

		<div class="arksp-widget-stats">
			<div>
				<div class="arksp-widget-stat-value"><?php echo esc_html( $stats['login_success'] ?? 0 ); ?></div>
				<div class="arksp-widget-stat-label"><?php esc_html_e( 'Logins (7d)', 'arkhost-security-pack' ); ?></div>
			</div>
			<div>
				<div class="arksp-widget-stat-value" style="color: #dba617;"><?php echo esc_html( $stats['login_failed'] ?? 0 ); ?></div>
				<div class="arksp-widget-stat-label"><?php esc_html_e( 'Failed (7d)', 'arkhost-security-pack' ); ?></div>
			</div>
			<div>
				<div class="arksp-widget-stat-value" style="color: #d63638;"><?php echo esc_html( $stats['lockout'] ?? 0 ); ?></div>
				<div class="arksp-widget-stat-label"><?php esc_html_e( 'Lockouts (7d)', 'arkhost-security-pack' ); ?></div>
			</div>
		</div>

		<div class="arksp-widget-links">
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=arkhost-security-pack' ) ); ?>"><?php esc_html_e( 'Dashboard', 'arkhost-security-pack' ); ?></a>
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=arkhost-security-pack&tab=logs' ) ); ?>"><?php esc_html_e( 'Activity Log', 'arkhost-security-pack' ); ?></a>
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=arkhost-security-pack&tab=scanner' ) ); ?>"><?php esc_html_e( 'Scanner', 'arkhost-security-pack' ); ?></a>
		</div>
		<?php
	}

	/**
	 * Register settings.
	 */
	public function register_settings() {
		register_setting( 'arksp_settings', 'arksp_settings', array( $this, 'sanitize_settings' ) );
	}

	/**
	 * Sanitize settings.
	 *
	 * @param array $input Input settings.
	 * @return array
	 */
	public function sanitize_settings( $input ) {
		// Start with existing settings to preserve values from other tabs.
		$existing  = get_option( 'arksp_settings', array() );
		$sanitized = is_array( $existing ) ? $existing : array();
		$defaults  = ARKSP_Plugin::get_default_settings();

		// Determine which tab is being saved (from hidden field).
		$current_tab = isset( $input['arksp_current_tab'] ) ? sanitize_text_field( $input['arksp_current_tab'] ) : '';

		// Only sanitize settings for the current tab to avoid resetting other tabs.
		// If no tab specified (e.g., import), sanitize all provided settings.

		if ( '' === $current_tab || 'login' === $current_tab ) {
			// Login Protection - only update if this tab is submitted or field is present.
			$sanitized['login_limit_enabled']    = ! empty( $input['login_limit_enabled'] );
			$sanitized['login_max_attempts']     = isset( $input['login_max_attempts'] ) ? absint( $input['login_max_attempts'] ) : ( $sanitized['login_max_attempts'] ?? 5 );
			$sanitized['login_lockout_duration'] = isset( $input['login_lockout_duration'] ) ? absint( $input['login_lockout_duration'] ) : ( $sanitized['login_lockout_duration'] ?? 15 );
			$sanitized['login_rename_enabled']   = ! empty( $input['login_rename_enabled'] );
			$sanitized['login_custom_url']       = isset( $input['login_custom_url'] ) ? ARKSP_Helper::sanitize_login_slug( $input['login_custom_url'] ) : ( $sanitized['login_custom_url'] ?? '' );
			$sanitized['hide_wp_admin']          = ! empty( $input['hide_wp_admin'] );
			$sanitized['honeypot_enabled']       = ! empty( $input['honeypot_enabled'] );
			$sanitized['honeypot_ban_duration']  = isset( $input['honeypot_ban_duration'] ) ? absint( $input['honeypot_ban_duration'] ) : ( $sanitized['honeypot_ban_duration'] ?? 60 );
			$sanitized['hide_login_errors']      = ! empty( $input['hide_login_errors'] );
			$sanitized['admin_login_notify']     = ! empty( $input['admin_login_notify'] );

			// Login Access Restriction.
			$sanitized['admin_access_restriction'] = ! empty( $input['admin_access_restriction'] );
			if ( 'login' === $current_tab ) {
				$sanitized['admin_allowed_countries'] = isset( $input['admin_allowed_countries'] ) && is_array( $input['admin_allowed_countries'] )
					? array_map( 'sanitize_text_field', $input['admin_allowed_countries'] )
					: array();
			} elseif ( isset( $input['admin_allowed_countries'] ) ) {
				$sanitized['admin_allowed_countries'] = is_array( $input['admin_allowed_countries'] )
					? array_map( 'sanitize_text_field', $input['admin_allowed_countries'] )
					: array();
			}
			$sanitized['admin_allowed_ips'] = isset( $input['admin_allowed_ips'] ) ? sanitize_textarea_field( $input['admin_allowed_ips'] ) : ( $sanitized['admin_allowed_ips'] ?? '' );

			// Email Alerts (also on login tab).
			$sanitized['email_alerts_enabled']   = ! empty( $input['email_alerts_enabled'] );
			$sanitized['email_alerts_address']   = isset( $input['email_alerts_address'] ) ? sanitize_email( $input['email_alerts_address'] ) : ( $sanitized['email_alerts_address'] ?? '' );
			$sanitized['email_alert_threshold']  = isset( $input['email_alert_threshold'] ) ? absint( $input['email_alert_threshold'] ) : ( $sanitized['email_alert_threshold'] ?? 3 );
		}

		if ( '' === $current_tab || 'ip' === $current_tab ) {
			// IP Control.
			$sanitized['ip_whitelist'] = isset( $input['ip_whitelist'] ) ? sanitize_textarea_field( $input['ip_whitelist'] ) : ( $sanitized['ip_whitelist'] ?? '' );
			$sanitized['ip_blacklist'] = isset( $input['ip_blacklist'] ) ? sanitize_textarea_field( $input['ip_blacklist'] ) : ( $sanitized['ip_blacklist'] ?? '' );
			$sanitized['auto_blacklist_enabled']   = ! empty( $input['auto_blacklist_enabled'] );
			$sanitized['auto_blacklist_threshold'] = isset( $input['auto_blacklist_threshold'] ) ? max( 1, absint( $input['auto_blacklist_threshold'] ) ) : ( $sanitized['auto_blacklist_threshold'] ?? 3 );

			// Geo Blocking.
			$sanitized['geo_blocking_enabled']  = ! empty( $input['geo_blocking_enabled'] );
			// For geo_blocked_countries, if the tab is IP and field not set, it means nothing selected.
			if ( 'ip' === $current_tab ) {
				$sanitized['geo_blocked_countries'] = isset( $input['geo_blocked_countries'] ) && is_array( $input['geo_blocked_countries'] )
					? array_map( 'sanitize_text_field', $input['geo_blocked_countries'] )
					: array();
			} elseif ( isset( $input['geo_blocked_countries'] ) ) {
				$sanitized['geo_blocked_countries'] = is_array( $input['geo_blocked_countries'] )
					? array_map( 'sanitize_text_field', $input['geo_blocked_countries'] )
					: array();
			}
			$sanitized['geo_database_path'] = isset( $input['geo_database_path'] ) ? sanitize_text_field( $input['geo_database_path'] ) : ( $sanitized['geo_database_path'] ?? '' );
		}

		if ( '' === $current_tab || 'hardening' === $current_tab ) {
			// Hardening.
			$sanitized['disable_xmlrpc']               = ! empty( $input['disable_xmlrpc'] );
			$sanitized['disable_file_editing']         = ! empty( $input['disable_file_editing'] );
			$sanitized['disable_application_passwords'] = ! empty( $input['disable_application_passwords'] );
			$sanitized['restrict_rest_api']            = ! empty( $input['restrict_rest_api'] );
			$sanitized['remove_wp_version']            = ! empty( $input['remove_wp_version'] );
			$sanitized['remove_feed_links']            = ! empty( $input['remove_feed_links'] );
			$sanitized['add_security_headers']         = ! empty( $input['add_security_headers'] );
			$sanitized['disable_user_enumeration']     = ! empty( $input['disable_user_enumeration'] );
			$sanitized['disable_pingbacks']            = ! empty( $input['disable_pingbacks'] );
		}

		if ( '' === $current_tab || 'headers' === $current_tab ) {
			// Security Headers.
			if ( isset( $input['security_headers'] ) && is_array( $input['security_headers'] ) ) {
				$sanitized['security_headers'] = array_map( 'sanitize_text_field', $input['security_headers'] );
			}
		}

		if ( '' === $current_tab || '2fa' === $current_tab ) {
			// Two-Factor.
			$sanitized['two_factor_enabled']       = ! empty( $input['two_factor_enabled'] );
			$sanitized['two_factor_enforce_admin'] = ! empty( $input['two_factor_enforce_admin'] );
		}

		if ( '' === $current_tab || 'scanner' === $current_tab ) {
			// File Integrity.
			$sanitized['file_integrity_enabled'] = ! empty( $input['file_integrity_enabled'] );

			// Malware Scanner.
			$sanitized['malware_scan_enabled'] = ! empty( $input['malware_scan_enabled'] );
		}

		if ( '' === $current_tab || 'logs' === $current_tab ) {
			// Activity Log.
			$sanitized['log_retention_days'] = isset( $input['log_retention_days'] ) ? absint( $input['log_retention_days'] ) : ( $sanitized['log_retention_days'] ?? 30 );
		}

		// Flush rewrite rules if login URL changed.
		if (
			( isset( $existing['login_custom_url'] ) && $existing['login_custom_url'] !== ( $sanitized['login_custom_url'] ?? '' ) ) ||
			( isset( $existing['login_rename_enabled'] ) && $existing['login_rename_enabled'] !== ( $sanitized['login_rename_enabled'] ?? false ) )
		) {
			flush_rewrite_rules();
		}

		return $sanitized;
	}

	/**
	 * Enqueue admin assets.
	 *
	 * @param string $hook Admin page hook.
	 */
	public function enqueue_admin_assets( $hook ) {
		$is_plugin_page = ( 'toplevel_page_arkhost-security-pack' === $hook || false !== strpos( $hook, 'security_page_arkhost-security-pack' ) );
		$is_dashboard   = ( 'index.php' === $hook );

		if ( ! $is_plugin_page && ! $is_dashboard ) {
			return;
		}

		wp_enqueue_style(
			'arksp-admin',
			ARKSP_PLUGIN_URL . 'assets/css/admin.css',
			array(),
			ARKSP_VERSION
		);

		if ( $is_plugin_page ) {
			wp_enqueue_script(
				'arksp-admin',
				ARKSP_PLUGIN_URL . 'assets/js/admin.js',
				array( 'jquery' ),
				ARKSP_VERSION,
				true
			);

			wp_localize_script( 'arksp-admin', 'arkspAdmin', array(
				'nonce'      => wp_create_nonce( 'arksp_admin' ),
				'adminEmail' => get_option( 'admin_email' ),
				'currentIp'  => ARKSP_Helper::get_client_ip(),
				'loginUrl'   => wp_login_url(),
				'strings'    => array(
					'confirmDeleteFile'     => __( 'Delete this file?', 'arkhost-security-pack' ),
					'deleting'              => __( 'Deleting...', 'arkhost-security-pack' ),
					'failedDeleteFile'      => __( 'Failed to delete file.', 'arkhost-security-pack' ),
					'deleteBtn'             => __( 'Delete', 'arkhost-security-pack' ),
					'sending'               => __( 'Sending...', 'arkhost-security-pack' ),
					'testEmailSent'         => __( 'Test email sent!', 'arkhost-security-pack' ),
					'requestFailed'         => __( 'Request failed.', 'arkhost-security-pack' ),
					'downloading'           => __( 'Downloading...', 'arkhost-security-pack' ),
					'downloadGeoDb'         => __( 'Download IP2Location Lite DB', 'arkhost-security-pack' ),
					'confirmResetHeaders'   => __( 'Reset all security headers to their default values?', 'arkhost-security-pack' ),
					'scanning'              => __( 'Scanning...', 'arkhost-security-pack' ),
					'scanningMalware'       => __( 'Scanning... This may take a while.', 'arkhost-security-pack' ),
					'confirmQuarantine'     => __( 'Quarantine this file? It will be moved to a safe location and cannot execute.', 'arkhost-security-pack' ),
					'moving'                => __( 'Moving...', 'arkhost-security-pack' ),
					'failedQuarantine'      => __( 'Failed to quarantine file.', 'arkhost-security-pack' ),
					'quarantineBtn'         => __( 'Quarantine', 'arkhost-security-pack' ),
					'confirmRestore'        => __( 'Restore this file to its original location? Make sure it is safe before restoring.', 'arkhost-security-pack' ),
					'restoring'             => __( 'Restoring...', 'arkhost-security-pack' ),
					'failedRestore'         => __( 'Failed to restore file.', 'arkhost-security-pack' ),
					'restoreBtn'            => __( 'Restore', 'arkhost-security-pack' ),
					'confirmDeletePermanent' => __( 'Permanently delete this file? This cannot be undone.', 'arkhost-security-pack' ),
					'confirmClearLogs'      => __( 'Are you sure you want to clear all logs? This cannot be undone.', 'arkhost-security-pack' ),
					'ipWhitelisted'         => __( 'IP added to whitelist!', 'arkhost-security-pack' ),
					'error'                 => __( 'Error', 'arkhost-security-pack' ),
					'confirmClearLockouts'  => __( 'Are you sure you want to clear all lockouts?', 'arkhost-security-pack' ),
					'lockoutsCleared'       => __( 'All lockouts cleared.', 'arkhost-security-pack' ),
					'confirmForceLogout'    => __( 'Are you sure? ALL users (including you) will be logged out immediately.', 'arkhost-security-pack' ),
					'sessionsTerminated'    => __( 'All sessions terminated. Redirecting to login...', 'arkhost-security-pack' ),
					'selectFileToImport'    => __( 'Please select a file to import.', 'arkhost-security-pack' ),
					'settingsImported'      => __( 'Settings imported successfully!', 'arkhost-security-pack' ),
					'confirmResetSettings'  => __( 'Are you sure you want to reset all settings to defaults? This cannot be undone.', 'arkhost-security-pack' ),
				),
			) );
		}
	}

	/**
	 * Render settings page.
	 */
	public function render_settings_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		// Get current tab.
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$current_tab = isset( $_GET['tab'] ) ? sanitize_text_field( wp_unslash( $_GET['tab'] ) ) : 'status';

		$tabs = array(
			'status'    => __( 'Status', 'arkhost-security-pack' ),
			'login'     => __( 'Login Protection', 'arkhost-security-pack' ),
			'ip'        => __( 'IP Control', 'arkhost-security-pack' ),
			'hardening' => __( 'Hardening', 'arkhost-security-pack' ),
			'headers'   => __( 'Security Headers', 'arkhost-security-pack' ),
			'2fa'       => __( '2FA', 'arkhost-security-pack' ),
			'scanner'   => __( 'Scanner', 'arkhost-security-pack' ),
			'logs'      => __( 'Activity Log', 'arkhost-security-pack' ),
			'tools'     => __( 'Tools', 'arkhost-security-pack' ),
		);
		?>
		<div class="wrap arksp-wrap">
			<h1><?php esc_html_e( 'ArkHost Security Pack', 'arkhost-security-pack' ); ?></h1>

			<nav class="nav-tab-wrapper arksp-tabs">
				<?php foreach ( $tabs as $tab => $label ) : ?>
					<a href="<?php echo esc_url( add_query_arg( 'tab', $tab ) ); ?>"
					   class="nav-tab <?php echo $tab === $current_tab ? 'nav-tab-active' : ''; ?>">
						<?php echo esc_html( $label ); ?>
					</a>
				<?php endforeach; ?>
			</nav>

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

				switch ( $current_tab ) {
					case 'status':
						$this->render_status_tab();
						break;
					case 'login':
						$this->render_login_tab();
						break;
					case 'ip':
						$this->render_ip_tab();
						break;
					case 'hardening':
						$this->render_hardening_tab();
						break;
					case 'headers':
						$this->render_headers_tab();
						break;
					case '2fa':
						$this->render_2fa_tab();
						break;
					case 'scanner':
						$this->render_scanner_tab();
						break;
					case 'logs':
						$this->render_logs_tab();
						break;
					case 'tools':
						$this->render_tools_tab();
						break;
				}

				if ( ! in_array( $current_tab, array( 'status', 'logs', 'tools' ), true ) ) {
					submit_button();
				}
				?>
			</form>

			<p class="arksp-footer">
				<?php
				printf(
					/* translators: %s: ArkHost link */
					esc_html__( 'ArkHost Security Pack by %s - Free forever, no bullshit.', 'arkhost-security-pack' ),
					'<a href="https://arkhost.com" target="_blank">ArkHost</a>'
				);
				?>
			</p>
		</div>
		<?php
	}

	/**
	 * Render status/checklist tab.
	 */
	private function render_status_tab() {
		// Gather all security status information.
		$checks = $this->get_security_checks();
		$enabled_count = 0;
		$total_count = count( $checks );

		foreach ( $checks as $check ) {
			if ( $check['status'] ) {
				$enabled_count++;
			}
		}

		$score = $total_count > 0 ? round( ( $enabled_count / $total_count ) * 100 ) : 0;
		?>
		<div class="arksp-section">
			<h2><?php esc_html_e( 'Security Status', 'arkhost-security-pack' ); ?></h2>

			<div class="arksp-stats">
				<div class="arksp-stat <?php echo $score >= 80 ? 'arksp-stat-success' : ( $score >= 50 ? 'arksp-stat-warning' : 'arksp-stat-danger' ); ?>">
					<span class="arksp-stat-value"><?php echo esc_html( $score ); ?>%</span>
					<span class="arksp-stat-label"><?php esc_html_e( 'Security Score', 'arkhost-security-pack' ); ?></span>
				</div>
				<div class="arksp-stat arksp-stat-success">
					<span class="arksp-stat-value"><?php echo esc_html( $enabled_count ); ?></span>
					<span class="arksp-stat-label"><?php esc_html_e( 'Protections Active', 'arkhost-security-pack' ); ?></span>
				</div>
				<div class="arksp-stat <?php echo ( $total_count - $enabled_count ) > 0 ? 'arksp-stat-warning' : 'arksp-stat-info'; ?>">
					<span class="arksp-stat-value"><?php echo esc_html( $total_count - $enabled_count ); ?></span>
					<span class="arksp-stat-label"><?php esc_html_e( 'Not Enabled', 'arkhost-security-pack' ); ?></span>
				</div>
			</div>

			<table class="widefat striped" style="margin-top: 20px;">
				<thead>
					<tr>
						<th style="width: 40px;"><?php esc_html_e( 'Status', 'arkhost-security-pack' ); ?></th>
						<th><?php esc_html_e( 'Security Feature', 'arkhost-security-pack' ); ?></th>
						<th><?php esc_html_e( 'Description', 'arkhost-security-pack' ); ?></th>
						<th style="width: 100px;"><?php esc_html_e( 'Action', 'arkhost-security-pack' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $checks as $key => $check ) : ?>
					<tr>
						<td>
							<?php if ( $check['status'] ) : ?>
								<span style="color: #46b450; font-size: 18px;">&#10003;</span>
							<?php else : ?>
								<span style="color: #dc3232; font-size: 18px;">&#10007;</span>
							<?php endif; ?>
						</td>
						<td><strong><?php echo esc_html( $check['name'] ); ?></strong></td>
						<td><span class="description"><?php echo esc_html( $check['description'] ); ?></span></td>
						<td>
							<?php if ( ! empty( $check['tab'] ) ) : ?>
								<a href="<?php echo esc_url( add_query_arg( 'tab', $check['tab'] ) ); ?>" class="button button-small">
									<?php echo $check['status'] ? esc_html__( 'Configure', 'arkhost-security-pack' ) : esc_html__( 'Enable', 'arkhost-security-pack' ); ?>
								</a>
							<?php endif; ?>
						</td>
					</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'Quick Info', 'arkhost-security-pack' ); ?></h2>
			<table class="form-table">
				<tr>
					<th><?php esc_html_e( 'Your IP Address', 'arkhost-security-pack' ); ?></th>
					<td><code><?php echo esc_html( ARKSP_Helper::get_client_ip() ); ?></code></td>
				</tr>
				<tr>
					<th><?php esc_html_e( 'WordPress Version', 'arkhost-security-pack' ); ?></th>
					<td><?php echo esc_html( get_bloginfo( 'version' ) ); ?></td>
				</tr>
				<tr>
					<th><?php esc_html_e( 'PHP Version', 'arkhost-security-pack' ); ?></th>
					<td>
						<?php echo esc_html( PHP_VERSION ); ?>
						<?php if ( version_compare( PHP_VERSION, '8.0', '<' ) ) : ?>
							<span class="arksp-status arksp-status-warning"><?php esc_html_e( 'Update recommended', 'arkhost-security-pack' ); ?></span>
						<?php endif; ?>
					</td>
				</tr>
				<tr>
					<th><?php esc_html_e( 'HTTPS', 'arkhost-security-pack' ); ?></th>
					<td>
						<?php if ( is_ssl() ) : ?>
							<span class="arksp-status arksp-status-ok"><?php esc_html_e( 'Active', 'arkhost-security-pack' ); ?></span>
						<?php else : ?>
							<span class="arksp-status arksp-status-error"><?php esc_html_e( 'Not active', 'arkhost-security-pack' ); ?></span>
							<span class="description"><?php esc_html_e( 'HTTPS is strongly recommended', 'arkhost-security-pack' ); ?></span>
						<?php endif; ?>
					</td>
				</tr>
				<tr>
					<th><?php esc_html_e( 'Debug Mode', 'arkhost-security-pack' ); ?></th>
					<td>
						<?php if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) : ?>
							<span class="arksp-status arksp-status-warning"><?php esc_html_e( 'Enabled', 'arkhost-security-pack' ); ?></span>
							<span class="description"><?php esc_html_e( 'Disable WP_DEBUG in production', 'arkhost-security-pack' ); ?></span>
						<?php else : ?>
							<span class="arksp-status arksp-status-ok"><?php esc_html_e( 'Disabled', 'arkhost-security-pack' ); ?></span>
						<?php endif; ?>
					</td>
				</tr>
				<tr>
					<th><?php esc_html_e( 'Database Prefix', 'arkhost-security-pack' ); ?></th>
					<td>
						<?php global $wpdb; ?>
						<?php if ( 'wp_' === $wpdb->prefix ) : ?>
							<span class="arksp-status arksp-status-warning"><code>wp_</code></span>
							<span class="description"><?php esc_html_e( 'Default prefix - consider changing for new installs', 'arkhost-security-pack' ); ?></span>
						<?php else : ?>
							<span class="arksp-status arksp-status-ok"><code><?php echo esc_html( $wpdb->prefix ); ?></code></span>
						<?php endif; ?>
					</td>
				</tr>
				<tr>
					<th><?php esc_html_e( 'Admin Username', 'arkhost-security-pack' ); ?></th>
					<td>
						<?php if ( username_exists( 'admin' ) ) : ?>
							<span class="arksp-status arksp-status-warning"><?php esc_html_e( 'Exists', 'arkhost-security-pack' ); ?></span>
							<span class="description"><?php esc_html_e( '"admin" username is commonly targeted', 'arkhost-security-pack' ); ?></span>
						<?php else : ?>
							<span class="arksp-status arksp-status-ok"><?php esc_html_e( 'Not found', 'arkhost-security-pack' ); ?></span>
						<?php endif; ?>
					</td>
				</tr>
				<?php
				$custom_login = ARKSP_Plugin::get_setting( 'custom_login_url', '' );
				if ( ! empty( $custom_login ) ) :
				?>
				<tr>
					<th><?php esc_html_e( 'Custom Login URL', 'arkhost-security-pack' ); ?></th>
					<td>
						<code><?php echo esc_url( home_url( '/' . $custom_login . '/' ) ); ?></code>
						<span class="description"><?php esc_html_e( 'Bookmark this!', 'arkhost-security-pack' ); ?></span>
					</td>
				</tr>
				<?php endif; ?>
			</table>
		</div>

		<?php
		// Check for files that expose WordPress version.
		$readme_exists  = file_exists( ABSPATH . 'readme.html' );
		$license_exists = file_exists( ABSPATH . 'license.txt' );

		if ( $readme_exists || $license_exists ) :
		?>
		<div class="arksp-section">
			<h2><?php esc_html_e( 'Recommended Actions', 'arkhost-security-pack' ); ?></h2>
			<div class="notice notice-warning inline" style="margin: 0 0 15px;">
				<p><?php esc_html_e( 'These files expose your WordPress version and can be safely removed.', 'arkhost-security-pack' ); ?></p>
			</div>
			<table class="widefat striped">
				<thead>
					<tr>
						<th><?php esc_html_e( 'File', 'arkhost-security-pack' ); ?></th>
						<th><?php esc_html_e( 'Status', 'arkhost-security-pack' ); ?></th>
						<th style="width: 100px;"><?php esc_html_e( 'Action', 'arkhost-security-pack' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php if ( $readme_exists ) : ?>
					<tr>
						<td><code>readme.html</code></td>
						<td><span class="arksp-status arksp-status-warning"><?php esc_html_e( 'Exists', 'arkhost-security-pack' ); ?></span></td>
						<td>
							<button type="button" class="button button-small arksp-delete-file" data-file="readme.html">
								<?php esc_html_e( 'Delete', 'arkhost-security-pack' ); ?>
							</button>
						</td>
					</tr>
					<?php endif; ?>
					<?php if ( $license_exists ) : ?>
					<tr>
						<td><code>license.txt</code></td>
						<td><span class="arksp-status arksp-status-warning"><?php esc_html_e( 'Exists', 'arkhost-security-pack' ); ?></span></td>
						<td>
							<button type="button" class="button button-small arksp-delete-file" data-file="license.txt">
								<?php esc_html_e( 'Delete', 'arkhost-security-pack' ); ?>
							</button>
						</td>
					</tr>
					<?php endif; ?>
				</tbody>
			</table>
		</div>
		<?php endif; ?>
		<?php
	}

	/**
	 * Get security checks for status tab.
	 *
	 * @return array
	 */
	private function get_security_checks() {
		$checks = array();

		// Login Protection.
		$checks['login_limit'] = array(
			'name'        => __( 'Login Attempt Limiting', 'arkhost-security-pack' ),
			'description' => __( 'Limits failed login attempts to prevent brute force attacks', 'arkhost-security-pack' ),
			'status'      => ARKSP_Plugin::get_setting( 'login_limit_enabled', true ),
			'tab'         => 'login',
		);

		$checks['honeypot'] = array(
			'name'        => __( 'Login Honeypot', 'arkhost-security-pack' ),
			'description' => __( 'Hidden field that traps automated bots', 'arkhost-security-pack' ),
			'status'      => ARKSP_Plugin::get_setting( 'honeypot_enabled', true ),
			'tab'         => 'login',
		);

		$checks['hide_login_errors'] = array(
			'name'        => __( 'Login Errors Hidden', 'arkhost-security-pack' ),
			'description' => __( 'Generic error message prevents username enumeration', 'arkhost-security-pack' ),
			'status'      => ARKSP_Plugin::get_setting( 'hide_login_errors', true ),
			'tab'         => 'login',
		);

		// Hardening.
		$checks['xmlrpc'] = array(
			'name'        => __( 'XML-RPC Disabled', 'arkhost-security-pack' ),
			'description' => __( 'Blocks XML-RPC endpoint commonly used in attacks', 'arkhost-security-pack' ),
			'status'      => ARKSP_Plugin::get_setting( 'disable_xmlrpc', true ),
			'tab'         => 'hardening',
		);

		$checks['file_editing'] = array(
			'name'        => __( 'File Editor Disabled', 'arkhost-security-pack' ),
			'description' => __( 'Prevents editing theme/plugin files from dashboard', 'arkhost-security-pack' ),
			'status'      => ARKSP_Plugin::get_setting( 'disable_file_editing', true ),
			'tab'         => 'hardening',
		);

		$checks['rest_api'] = array(
			'name'        => __( 'REST API Restricted', 'arkhost-security-pack' ),
			'description' => __( 'Requires authentication for REST API access', 'arkhost-security-pack' ),
			'status'      => ARKSP_Plugin::get_setting( 'restrict_rest_api', true ),
			'tab'         => 'hardening',
		);

		$checks['user_enum'] = array(
			'name'        => __( 'User Enumeration Blocked', 'arkhost-security-pack' ),
			'description' => __( 'Prevents attackers from discovering usernames', 'arkhost-security-pack' ),
			'status'      => ARKSP_Plugin::get_setting( 'disable_user_enumeration', true ),
			'tab'         => 'hardening',
		);

		$checks['pingbacks'] = array(
			'name'        => __( 'Pingbacks Disabled', 'arkhost-security-pack' ),
			'description' => __( 'Prevents pingback-based DDoS amplification', 'arkhost-security-pack' ),
			'status'      => ARKSP_Plugin::get_setting( 'disable_pingbacks', true ),
			'tab'         => 'hardening',
		);

		$checks['wp_version'] = array(
			'name'        => __( 'WP Version Hidden', 'arkhost-security-pack' ),
			'description' => __( 'Hides WordPress version from public view', 'arkhost-security-pack' ),
			'status'      => ARKSP_Plugin::get_setting( 'remove_wp_version', true ),
			'tab'         => 'hardening',
		);

		$checks['security_headers'] = array(
			'name'        => __( 'Security Headers', 'arkhost-security-pack' ),
			'description' => __( 'HTTP headers that enable browser security features', 'arkhost-security-pack' ),
			'status'      => ARKSP_Plugin::get_setting( 'add_security_headers', true ),
			'tab'         => 'headers',
		);

		// 2FA - only green if enforced for admins (just "enabled" means users CAN set it up, not that they have).
		$two_fa_enabled  = ARKSP_Plugin::get_setting( 'two_factor_enabled', false );
		$two_fa_enforced = ARKSP_Plugin::get_setting( 'two_factor_enforce_admin', false );
		$checks['two_factor'] = array(
			'name'        => __( 'Two-Factor Authentication', 'arkhost-security-pack' ),
			'description' => $two_fa_enabled && ! $two_fa_enforced
				? __( 'Available but not enforced for admins', 'arkhost-security-pack' )
				: __( 'Enforced for administrator accounts', 'arkhost-security-pack' ),
			'status'      => $two_fa_enabled && $two_fa_enforced,
			'tab'         => '2fa',
		);

		// Monitoring.
		$checks['file_integrity'] = array(
			'name'        => __( 'File Integrity Monitoring', 'arkhost-security-pack' ),
			'description' => __( 'Detects unauthorized changes to WordPress core', 'arkhost-security-pack' ),
			'status'      => ARKSP_Plugin::get_setting( 'file_integrity_enabled', true ),
			'tab'         => 'scanner',
		);

		$checks['malware_scan'] = array(
			'name'        => __( 'Malware Scanning', 'arkhost-security-pack' ),
			'description' => __( 'Scans files for malicious code patterns', 'arkhost-security-pack' ),
			'status'      => ARKSP_Plugin::get_setting( 'malware_scan_enabled', true ),
			'tab'         => 'scanner',
		);

		// Optional features (not counted negatively if disabled).
		$checks['custom_login'] = array(
			'name'        => __( 'Custom Login URL', 'arkhost-security-pack' ),
			'description' => __( 'Hides wp-login.php from automated scanners', 'arkhost-security-pack' ),
			'status'      => ARKSP_Plugin::get_setting( 'login_rename_enabled', false ),
			'tab'         => 'login',
		);

		$checks['geo_blocking'] = array(
			'name'        => __( 'Country Blocking', 'arkhost-security-pack' ),
			'description' => __( 'Blocks access from specific countries', 'arkhost-security-pack' ),
			'status'      => ARKSP_Plugin::get_setting( 'geo_blocking_enabled', false ),
			'tab'         => 'ip',
		);

		$checks['auto_blacklist'] = array(
			'name'        => __( 'Auto-Blacklist Repeat Offenders', 'arkhost-security-pack' ),
			'description' => __( 'Permanently blocks IPs with repeated lockouts', 'arkhost-security-pack' ),
			'status'      => ARKSP_Plugin::get_setting( 'auto_blacklist_enabled', false ),
			'tab'         => 'ip',
		);

		// Environment checks (no tab - these are recommendations).
		global $wpdb;
		$checks['ssl'] = array(
			'name'        => __( 'SSL/HTTPS', 'arkhost-security-pack' ),
			'description' => is_ssl()
				? __( 'Site is served over HTTPS', 'arkhost-security-pack' )
				: __( 'Site should use HTTPS for security', 'arkhost-security-pack' ),
			'status'      => is_ssl(),
			'tab'         => '',
		);

		$checks['debug_mode'] = array(
			'name'        => __( 'Debug Mode Disabled', 'arkhost-security-pack' ),
			'description' => defined( 'WP_DEBUG' ) && WP_DEBUG
				? __( 'WP_DEBUG is enabled - disable in production', 'arkhost-security-pack' )
				: __( 'Debug mode is properly disabled', 'arkhost-security-pack' ),
			'status'      => ! ( defined( 'WP_DEBUG' ) && WP_DEBUG ),
			'tab'         => '',
		);

		$checks['db_prefix'] = array(
			'name'        => __( 'Database Prefix Changed', 'arkhost-security-pack' ),
			'description' => 'wp_' === $wpdb->prefix
				? __( 'Using default wp_ prefix - consider changing', 'arkhost-security-pack' )
				: __( 'Using custom database prefix', 'arkhost-security-pack' ),
			'status'      => 'wp_' !== $wpdb->prefix,
			'tab'         => '',
		);

		$admin_user = get_user_by( 'login', 'admin' );
		$checks['admin_username'] = array(
			'name'        => __( 'No "admin" Username', 'arkhost-security-pack' ),
			'description' => $admin_user
				? __( 'Default "admin" username exists - consider renaming', 'arkhost-security-pack' )
				: __( 'No user with "admin" username', 'arkhost-security-pack' ),
			'status'      => ! $admin_user,
			'tab'         => '',
		);

		$checks['php_version'] = array(
			'name'        => __( 'PHP Version', 'arkhost-security-pack' ),
			'description' => version_compare( PHP_VERSION, '8.0', '<' )
				? sprintf(
					/* translators: %s: PHP version */
					__( 'PHP %s is outdated - update recommended', 'arkhost-security-pack' ),
					PHP_VERSION
				)
				: sprintf(
					/* translators: %s: PHP version */
					__( 'Running PHP %s', 'arkhost-security-pack' ),
					PHP_VERSION
				),
			'status'      => version_compare( PHP_VERSION, '8.0', '>=' ),
			'tab'         => '',
		);

		// Check if auto-updates are enabled for core.
		$auto_updates_enabled = defined( 'WP_AUTO_UPDATE_CORE' ) && WP_AUTO_UPDATE_CORE;
		// Also check the database option.
		if ( ! $auto_updates_enabled ) {
			$auto_updates_enabled = 'true' === get_site_option( 'auto_update_core_major' ) ||
			                        'true' === get_site_option( 'auto_update_core_minor' );
		}
		$checks['auto_updates'] = array(
			'name'        => __( 'Auto-Updates Enabled', 'arkhost-security-pack' ),
			'description' => $auto_updates_enabled
				? __( 'WordPress core auto-updates are enabled', 'arkhost-security-pack' )
				: __( 'Enable auto-updates for security patches', 'arkhost-security-pack' ),
			'status'      => $auto_updates_enabled,
			'tab'         => '',
		);

		return $checks;
	}

	/**
	 * Render login protection tab.
	 */
	private function render_login_tab() {
		?>
		<input type="hidden" name="arksp_settings[arksp_current_tab]" value="login" />
		<div class="arksp-section">
			<h2><?php esc_html_e( 'Login Access Restriction', 'arkhost-security-pack' ); ?></h2>
			<p class="description">
				<?php esc_html_e( 'Only listed IPs/countries can access login. Different from global whitelist (which bypasses everything).', 'arkhost-security-pack' ); ?>
			</p>
			<?php
			$geo_blocking = arksp()->get_component( 'geo_blocking' );
			$db_exists    = $geo_blocking && $geo_blocking->database_exists();
			if ( ! $db_exists ) :
				?>
				<div class="notice notice-warning inline" style="margin: 10px 0;">
					<p>
						<strong><?php esc_html_e( 'GeoIP Database Required', 'arkhost-security-pack' ); ?></strong><br>
						<?php
						printf(
							/* translators: %s: Link to IP Control tab */
							esc_html__( 'Country detection requires the GeoIP database. Please download it from the %s tab.', 'arkhost-security-pack' ),
							'<a href="' . esc_url( admin_url( 'admin.php?page=arkhost-security-pack&tab=ip' ) ) . '"><strong>' . esc_html__( 'IP Control', 'arkhost-security-pack' ) . '</strong></a>'
						);
						?>
					</p>
				</div>
			<?php else : ?>
				<div class="notice notice-success inline" style="margin: 10px 0;">
					<p><?php esc_html_e( 'GeoIP database installed.', 'arkhost-security-pack' ); ?></p>
				</div>
			<?php endif; ?>
			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Enable', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[admin_access_restriction]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'admin_access_restriction', false ) ); ?> />
							<?php esc_html_e( 'Restrict login page access by country/IP', 'arkhost-security-pack' ); ?>
						</label>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Allowed Countries', 'arkhost-security-pack' ); ?></th>
					<td>
						<?php
						$countries_with_flags = ARKSP_Helper::get_countries_with_flags();
						$admin_countries      = ARKSP_Plugin::get_setting( 'admin_allowed_countries', array() );
						?>
						<select name="arksp_settings[admin_allowed_countries][]" multiple="multiple" size="8" class="arksp-country-select" style="min-width: 300px;">
							<?php foreach ( $countries_with_flags as $code => $name ) : ?>
								<option value="<?php echo esc_attr( $code ); ?>"
									<?php selected( in_array( $code, $admin_countries, true ) ); ?>>
									<?php echo esc_html( $name ); ?>
								</option>
							<?php endforeach; ?>
						</select>
						<p class="description"><?php esc_html_e( 'Ctrl/Cmd to select multiple. Empty = all countries.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Allowed IPs', 'arkhost-security-pack' ); ?></th>
					<td>
						<textarea name="arksp_settings[admin_allowed_ips]" rows="4" class="large-text code"><?php
							echo esc_textarea( ARKSP_Plugin::get_setting( 'admin_allowed_ips', '' ) );
						?></textarea>
						<p class="description">
							<?php esc_html_e( 'One IP or CIDR per line.', 'arkhost-security-pack' ); ?>
							<?php
							/* translators: %s: Current user IP address */
							printf( esc_html__( 'Your IP: %s', 'arkhost-security-pack' ), '<code>' . esc_html( ARKSP_Helper::get_client_ip() ) . '</code>' );
							?>
						</p>
					</td>
				</tr>
			</table>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'Email Alerts', 'arkhost-security-pack' ); ?></h2>
			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Enable', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[email_alerts_enabled]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'email_alerts_enabled', false ) ); ?> />
							<?php esc_html_e( 'Send email alerts for security events', 'arkhost-security-pack' ); ?>
						</label>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Email Address', 'arkhost-security-pack' ); ?></th>
					<td>
						<input type="email" name="arksp_settings[email_alerts_address]" id="arksp-alert-email"
							   value="<?php echo esc_attr( ARKSP_Plugin::get_setting( 'email_alerts_address', get_option( 'admin_email' ) ) ); ?>" class="regular-text" />
						<button type="button" class="button" id="arksp-test-email">
							<?php esc_html_e( 'Send Test Email', 'arkhost-security-pack' ); ?>
						</button>
						<span id="arksp-test-email-result" style="margin-left: 10px;"></span>
						<p class="description"><?php esc_html_e( 'Leave empty to use the site admin email.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Alert Threshold', 'arkhost-security-pack' ); ?></th>
					<td>
						<input type="number" name="arksp_settings[email_alert_threshold]" min="1" max="100"
							   value="<?php echo esc_attr( ARKSP_Plugin::get_setting( 'email_alert_threshold', 3 ) ); ?>" class="small-text" />
					</td>
				</tr>
			</table>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'Login Attempt Limiting', 'arkhost-security-pack' ); ?></h2>
			<p class="description"><?php esc_html_e( 'Blocks IPs that fail too many login attempts. Stops brute force attacks.', 'arkhost-security-pack' ); ?></p>
			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Enable', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[login_limit_enabled]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'login_limit_enabled', true ) ); ?> />
							<?php esc_html_e( 'Limit failed login attempts', 'arkhost-security-pack' ); ?>
						</label>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Max Attempts', 'arkhost-security-pack' ); ?></th>
					<td>
						<input type="number" name="arksp_settings[login_max_attempts]" min="1" max="100"
							   value="<?php echo esc_attr( ARKSP_Plugin::get_setting( 'login_max_attempts', 5 ) ); ?>" class="small-text" />
						<p class="description"><?php esc_html_e( 'Number of failed attempts before lockout.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Lockout Duration', 'arkhost-security-pack' ); ?></th>
					<td>
						<input type="number" name="arksp_settings[login_lockout_duration]" min="1" max="1440"
							   value="<?php echo esc_attr( ARKSP_Plugin::get_setting( 'login_lockout_duration', 15 ) ); ?>" class="small-text" />
						<?php esc_html_e( 'minutes', 'arkhost-security-pack' ); ?>
					</td>
				</tr>
			</table>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'Custom Login URL', 'arkhost-security-pack' ); ?></h2>
			<p class="description"><?php esc_html_e( 'Hides wp-login.php. Bots can\'t attack what they can\'t find.', 'arkhost-security-pack' ); ?></p>
			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Enable', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[login_rename_enabled]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'login_rename_enabled', false ) ); ?> />
							<?php esc_html_e( 'Use custom login URL', 'arkhost-security-pack' ); ?>
						</label>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Custom URL', 'arkhost-security-pack' ); ?></th>
					<td>
						<code><?php echo esc_url( home_url( '/' ) ); ?></code>
						<input type="text" name="arksp_settings[login_custom_url]"
							   value="<?php echo esc_attr( ARKSP_Plugin::get_setting( 'login_custom_url', '' ) ); ?>" class="regular-text" />
						<p class="description"><?php esc_html_e( 'Letters, numbers, and hyphens only. Example: my-secret-login', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Hide wp-admin', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[hide_wp_admin]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'hide_wp_admin', false ) ); ?> />
							<?php esc_html_e( 'Return 404 for /wp-admin when not logged in', 'arkhost-security-pack' ); ?>
						</label>
						<p class="description"><?php esc_html_e( 'Also blocks /wp-login.php with 404.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
			</table>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'Honeypot Protection', 'arkhost-security-pack' ); ?></h2>
			<p class="description"><?php esc_html_e( 'Hidden field that catches bots. Humans can\'t see it, bots fill it out and get blocked.', 'arkhost-security-pack' ); ?></p>
			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Enable', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[honeypot_enabled]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'honeypot_enabled', true ) ); ?> />
							<?php esc_html_e( 'Enable honeypot bot detection', 'arkhost-security-pack' ); ?>
						</label>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Ban Duration', 'arkhost-security-pack' ); ?></th>
					<td>
						<input type="number" name="arksp_settings[honeypot_ban_duration]" min="1" max="1440"
							   value="<?php echo esc_attr( ARKSP_Plugin::get_setting( 'honeypot_ban_duration', 60 ) ); ?>" class="small-text" />
						<?php esc_html_e( 'minutes', 'arkhost-security-pack' ); ?>
					</td>
				</tr>
			</table>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'Additional Login Security', 'arkhost-security-pack' ); ?></h2>
			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Hide Login Errors', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[hide_login_errors]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'hide_login_errors', true ) ); ?> />
							<?php esc_html_e( 'Show generic error message on failed login', 'arkhost-security-pack' ); ?>
						</label>
						<p class="description"><?php esc_html_e( 'Stops username enumeration.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Admin Login Alerts', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[admin_login_notify]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'admin_login_notify', false ) ); ?> />
							<?php esc_html_e( 'Email notification when admin logs in from new IP', 'arkhost-security-pack' ); ?>
						</label>
					</td>
				</tr>
			</table>
		</div>
		<?php
	}

	/**
	 * Render IP control tab.
	 */
	private function render_ip_tab() {
		$blocked_ips = array();
		$ip_control  = arksp()->get_component( 'ip_control' );
		if ( $ip_control ) {
			$blocked_ips = $ip_control->get_blocked_ips();
		}

		$geo_blocking = arksp()->get_component( 'geo_blocking' );
		$db_info      = $geo_blocking ? $geo_blocking->get_database_info() : array( 'exists' => false );
		$countries    = ARKSP_Helper::get_countries_with_flags();
		?>
		<input type="hidden" name="arksp_settings[arksp_current_tab]" value="ip" />
		<div class="arksp-section">
			<h2><?php esc_html_e( 'Country Blocking', 'arkhost-security-pack' ); ?></h2>
			<p class="arksp-description">
				<strong><?php esc_html_e( 'Warning:', 'arkhost-security-pack' ); ?></strong>
				<?php esc_html_e( 'Blocks the ENTIRE site, not just login.', 'arkhost-security-pack' ); ?>
			</p>
			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Enable', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[geo_blocking_enabled]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'geo_blocking_enabled', false ) ); ?> />
							<?php esc_html_e( 'Enable country-based blocking', 'arkhost-security-pack' ); ?>
						</label>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'GeoIP Database', 'arkhost-security-pack' ); ?></th>
					<td>
						<?php if ( $db_info['exists'] ) : ?>
							<span class="arksp-status arksp-status-ok">
								<?php esc_html_e( 'Installed', 'arkhost-security-pack' ); ?>
							</span>
							<span class="description">
								<?php echo esc_html( $db_info['type'] ); ?> -
								<?php echo esc_html( size_format( $db_info['size'] ) ); ?>
								<?php if ( ! empty( $db_info['modified'] ) ) : ?>
									<?php
									/* translators: %s: Database last modified date */
									printf( '- ' . esc_html__( 'Updated: %s', 'arkhost-security-pack' ), esc_html( date_i18n( get_option( 'date_format' ), $db_info['modified'] ) ) );
									?>
								<?php endif; ?>
							</span>
							<br><br>
							<button type="button" class="button" id="arksp-download-geo-db">
								<?php esc_html_e( 'Re-download Database', 'arkhost-security-pack' ); ?>
							</button>
							<p class="description">
								<?php esc_html_e( 'Download the latest version of the IP2Location LITE database.', 'arkhost-security-pack' ); ?>
							</p>
						<?php else : ?>
							<span class="arksp-status arksp-status-warning">
								<?php esc_html_e( 'Not installed', 'arkhost-security-pack' ); ?>
							</span>
							<button type="button" class="button" id="arksp-download-geo-db">
								<?php esc_html_e( 'Download IP2Location Lite DB', 'arkhost-security-pack' ); ?>
							</button>
							<p class="description">
								<?php esc_html_e( 'Free IP2Location LITE database. No account required.', 'arkhost-security-pack' ); ?>
							</p>
						<?php endif; ?>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Blocked Countries', 'arkhost-security-pack' ); ?></th>
					<td>
						<select name="arksp_settings[geo_blocked_countries][]" multiple="multiple" size="10" class="arksp-country-select">
							<?php
							$blocked = ARKSP_Plugin::get_setting( 'geo_blocked_countries', array() );
							foreach ( $countries as $code => $name ) :
								?>
								<option value="<?php echo esc_attr( $code ); ?>"
									<?php selected( in_array( $code, $blocked, true ) ); ?>>
									<?php echo esc_html( $name ); ?>
								</option>
							<?php endforeach; ?>
						</select>
						<p class="description"><?php esc_html_e( 'Ctrl/Cmd to select multiple.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
			</table>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'IP Whitelist (Global Bypass)', 'arkhost-security-pack' ); ?></h2>
			<p class="arksp-description"><?php esc_html_e( 'These IPs bypass all security checks.', 'arkhost-security-pack' ); ?></p>
			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Whitelisted IPs', 'arkhost-security-pack' ); ?></th>
					<td>
						<textarea name="arksp_settings[ip_whitelist]" rows="6" class="large-text code"><?php
							echo esc_textarea( ARKSP_Plugin::get_setting( 'ip_whitelist', '' ) );
						?></textarea>
						<p class="description">
							<?php esc_html_e( 'One IP or CIDR per line.', 'arkhost-security-pack' ); ?>
							<?php
							/* translators: %s: Current user IP address */
							printf( esc_html__( 'Your IP: %s', 'arkhost-security-pack' ), '<code>' . esc_html( ARKSP_Helper::get_client_ip() ) . '</code>' );
							?>
						</p>
					</td>
				</tr>
			</table>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'IP Blacklist', 'arkhost-security-pack' ); ?></h2>
			<p class="description"><?php esc_html_e( 'Permanently blocked IPs. They can\'t access anything.', 'arkhost-security-pack' ); ?></p>
			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Blacklisted IPs', 'arkhost-security-pack' ); ?></th>
					<td>
						<textarea name="arksp_settings[ip_blacklist]" rows="6" class="large-text code"><?php
							echo esc_textarea( ARKSP_Plugin::get_setting( 'ip_blacklist', '' ) );
						?></textarea>
						<p class="description"><?php esc_html_e( 'One IP or CIDR per line. # for comments.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Auto-Blacklist', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[auto_blacklist_enabled]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'auto_blacklist_enabled', false ) ); ?> />
							<?php esc_html_e( 'Automatically blacklist repeat offenders', 'arkhost-security-pack' ); ?>
						</label>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Auto-Blacklist Threshold', 'arkhost-security-pack' ); ?></th>
					<td>
						<input type="number" name="arksp_settings[auto_blacklist_threshold]" min="1" max="20"
							value="<?php echo esc_attr( ARKSP_Plugin::get_setting( 'auto_blacklist_threshold', 3 ) ); ?>" class="small-text" />
						<?php esc_html_e( 'lockouts', 'arkhost-security-pack' ); ?>
					</td>
				</tr>
			</table>
		</div>

		<?php if ( ! empty( $blocked_ips ) ) : ?>
		<div class="arksp-section">
			<h2><?php esc_html_e( 'Currently Locked Out IPs', 'arkhost-security-pack' ); ?></h2>
			<table class="widefat striped">
				<thead>
					<tr>
						<th><?php esc_html_e( 'IP Address', 'arkhost-security-pack' ); ?></th>
						<th><?php esc_html_e( 'Failed Attempts', 'arkhost-security-pack' ); ?></th>
						<th><?php esc_html_e( 'Locked Until', 'arkhost-security-pack' ); ?></th>
						<th><?php esc_html_e( 'Actions', 'arkhost-security-pack' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $blocked_ips as $lockout ) : ?>
					<tr>
						<td><code><?php echo esc_html( $lockout->ip_address ); ?></code></td>
						<td><?php echo esc_html( $lockout->failed_attempts ); ?></td>
						<td><?php echo esc_html( $lockout->lockout_until ); ?></td>
						<td>
							<button type="button" class="button button-small arksp-unblock-ip"
									data-ip="<?php echo esc_attr( $lockout->ip_address ); ?>">
								<?php esc_html_e( 'Unblock', 'arkhost-security-pack' ); ?>
							</button>
						</td>
					</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		</div>
		<?php endif; ?>
		<?php
	}

	/**
	 * Render hardening tab.
	 */
	private function render_hardening_tab() {
		?>
		<input type="hidden" name="arksp_settings[arksp_current_tab]" value="hardening" />
		<div class="arksp-section">
			<h2><?php esc_html_e( 'Security Hardening', 'arkhost-security-pack' ); ?></h2>
			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Disable XML-RPC', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[disable_xmlrpc]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'disable_xmlrpc', true ) ); ?> />
							<?php esc_html_e( 'Disable XML-RPC endpoint', 'arkhost-security-pack' ); ?>
						</label>
						<p class="description"><?php esc_html_e( 'Disable unless needed for Jetpack or WordPress mobile app.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Disable File Editing', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[disable_file_editing]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'disable_file_editing', true ) ); ?> />
							<?php esc_html_e( 'Disable plugin and theme file editor', 'arkhost-security-pack' ); ?>
						</label>
						<p class="description"><?php esc_html_e( 'Removes Theme Editor and Plugin Editor from dashboard.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Disable Application Passwords', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[disable_application_passwords]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'disable_application_passwords', false ) ); ?> />
							<?php esc_html_e( 'Disable WordPress application passwords', 'arkhost-security-pack' ); ?>
						</label>
						<p class="description"><?php esc_html_e( 'Disable if you don\'t use third-party apps.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Restrict REST API', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[restrict_rest_api]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'restrict_rest_api', true ) ); ?> />
							<?php esc_html_e( 'Require authentication for REST API access', 'arkhost-security-pack' ); ?>
						</label>
						<p class="description"><?php esc_html_e( 'Blocks anonymous REST API access. oEmbed still works.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Remove WP Version', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[remove_wp_version]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'remove_wp_version', true ) ); ?> />
							<?php esc_html_e( 'Hide WordPress version information', 'arkhost-security-pack' ); ?>
						</label>
						<p class="description"><?php esc_html_e( 'Removes version from source, feeds, and script URLs.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Remove Feed Links', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[remove_feed_links]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'remove_feed_links', false ) ); ?> />
							<?php esc_html_e( 'Remove RSS feed links from HTML head', 'arkhost-security-pack' ); ?>
						</label>
						<p class="description"><?php esc_html_e( 'Only enable if you don\'t use RSS.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Security Headers', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[add_security_headers]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'add_security_headers', true ) ); ?> />
							<?php esc_html_e( 'Add HTTP security headers', 'arkhost-security-pack' ); ?>
						</label>
						<p class="description"><?php esc_html_e( 'Configure in Security Headers tab.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Block User Enumeration', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[disable_user_enumeration]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'disable_user_enumeration', true ) ); ?> />
							<?php esc_html_e( 'Prevent username discovery', 'arkhost-security-pack' ); ?>
						</label>
						<p class="description"><?php esc_html_e( 'Blocks ?author=N scans and hides usernames from REST API.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Disable Pingbacks', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[disable_pingbacks]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'disable_pingbacks', true ) ); ?> />
							<?php esc_html_e( 'Disable pingbacks and trackbacks', 'arkhost-security-pack' ); ?>
						</label>
						<p class="description"><?php esc_html_e( 'Rarely used legitimately, often abused for DDoS.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
			</table>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'Always Active', 'arkhost-security-pack' ); ?></h2>
			<p class="description">
				<?php esc_html_e( 'The following security measures are automatically applied:', 'arkhost-security-pack' ); ?>
			</p>
			<ul style="list-style: disc; margin-left: 20px; color: #666;">
				<li><?php esc_html_e( 'RSD (Really Simple Discovery) link removed from HTML head', 'arkhost-security-pack' ); ?></li>
				<li><?php esc_html_e( 'Windows Live Writer manifest link removed', 'arkhost-security-pack' ); ?></li>
				<li><?php esc_html_e( 'Shortlink removed from HTML head', 'arkhost-security-pack' ); ?></li>
			</ul>
		</div>
		<?php
	}

	/**
	 * Render security headers tab.
	 */
	private function render_headers_tab() {
		$hardening       = new ARKSP_Hardening();
		$default_headers = $hardening->get_default_headers();
		$headers         = ARKSP_Plugin::get_setting( 'security_headers', $default_headers );
		?>
		<input type="hidden" name="arksp_settings[arksp_current_tab]" value="headers" />
		<div class="arksp-section">
			<h2><?php esc_html_e( 'Security Headers Configuration', 'arkhost-security-pack' ); ?></h2>
			<p class="description"><?php esc_html_e( 'HTTP headers that tell browsers to enable security features. Leave empty to disable.', 'arkhost-security-pack' ); ?></p>
			<div class="notice notice-info inline" style="margin: 10px 0;">
				<p>
					<?php
					printf(
						/* translators: %s: Link to securityheaders.com */
						esc_html__( 'Test your headers at %s after saving.', 'arkhost-security-pack' ),
						'<a href="https://securityheaders.com/" target="_blank" rel="noopener">securityheaders.com</a>'
					);
					?>
					<button type="button" class="button button-small" id="arksp-reset-headers" style="margin-left: 15px;">
						<?php esc_html_e( 'Reset to Defaults', 'arkhost-security-pack' ); ?>
					</button>
				</p>
			</div>

			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'X-Content-Type-Options', 'arkhost-security-pack' ); ?></th>
					<td>
						<input type="text" name="arksp_settings[security_headers][x_content_type_options]" id="header-x_content_type_options"
							   value="<?php echo esc_attr( $headers['x_content_type_options'] ?? 'nosniff' ); ?>" class="regular-text"
							   data-default="nosniff" />
						<p class="description"><?php esc_html_e( 'Prevents MIME-sniffing attacks.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'X-Frame-Options', 'arkhost-security-pack' ); ?></th>
					<td>
						<select name="arksp_settings[security_headers][x_frame_options]" id="header-x_frame_options" data-default="SAMEORIGIN">
							<option value="" <?php selected( empty( $headers['x_frame_options'] ) ); ?>><?php esc_html_e( 'Disabled', 'arkhost-security-pack' ); ?></option>
							<option value="DENY" <?php selected( $headers['x_frame_options'] ?? '', 'DENY' ); ?>>DENY</option>
							<option value="SAMEORIGIN" <?php selected( $headers['x_frame_options'] ?? '', 'SAMEORIGIN' ); ?>>SAMEORIGIN</option>
						</select>
						<p class="description"><?php esc_html_e( 'Clickjacking protection. DENY = no iframes, SAMEORIGIN = same domain only.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'X-XSS-Protection', 'arkhost-security-pack' ); ?></th>
					<td>
						<input type="text" name="arksp_settings[security_headers][x_xss_protection]" id="header-x_xss_protection"
							   value="<?php echo esc_attr( $headers['x_xss_protection'] ?? '1; mode=block' ); ?>" class="regular-text"
							   data-default="1; mode=block" />
						<p class="description"><?php esc_html_e( 'Legacy XSS filter. Only matters for old browsers.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Referrer-Policy', 'arkhost-security-pack' ); ?></th>
					<td>
						<select name="arksp_settings[security_headers][referrer_policy]" id="header-referrer_policy" data-default="strict-origin-when-cross-origin">
							<option value="" <?php selected( empty( $headers['referrer_policy'] ) ); ?>><?php esc_html_e( 'Disabled', 'arkhost-security-pack' ); ?></option>
							<option value="no-referrer" <?php selected( $headers['referrer_policy'] ?? '', 'no-referrer' ); ?>>no-referrer</option>
							<option value="no-referrer-when-downgrade" <?php selected( $headers['referrer_policy'] ?? '', 'no-referrer-when-downgrade' ); ?>>no-referrer-when-downgrade</option>
							<option value="origin" <?php selected( $headers['referrer_policy'] ?? '', 'origin' ); ?>>origin</option>
							<option value="origin-when-cross-origin" <?php selected( $headers['referrer_policy'] ?? '', 'origin-when-cross-origin' ); ?>>origin-when-cross-origin</option>
							<option value="same-origin" <?php selected( $headers['referrer_policy'] ?? '', 'same-origin' ); ?>>same-origin</option>
							<option value="strict-origin" <?php selected( $headers['referrer_policy'] ?? '', 'strict-origin' ); ?>>strict-origin</option>
							<option value="strict-origin-when-cross-origin" <?php selected( $headers['referrer_policy'] ?? '', 'strict-origin-when-cross-origin' ); ?>>strict-origin-when-cross-origin</option>
						</select>
						<p class="description"><?php esc_html_e( 'Controls referrer info sent to other sites.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Permissions-Policy', 'arkhost-security-pack' ); ?></th>
					<td>
						<input type="text" name="arksp_settings[security_headers][permissions_policy]" id="header-permissions_policy"
							   value="<?php echo esc_attr( $headers['permissions_policy'] ?? 'geolocation=(), microphone=(), camera=()' ); ?>" class="large-text"
							   data-default="geolocation=(), microphone=(), camera=()" />
						<p class="description"><?php esc_html_e( 'Empty () disables the feature. Controls browser API access.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Content-Security-Policy', 'arkhost-security-pack' ); ?></th>
					<td>
						<textarea name="arksp_settings[security_headers][content_security_policy]" id="header-content_security_policy" rows="4" class="large-text code" data-default=""><?php
							echo esc_textarea( $headers['content_security_policy'] ?? '' );
						?></textarea>
						<p style="margin-top: 8px;">
							<button type="button" class="button button-small" id="arksp-load-example-csp">
								<?php esc_html_e( 'Load Example CSP', 'arkhost-security-pack' ); ?>
							</button>
							<span class="description" style="margin-left: 10px;"><?php esc_html_e( 'Loads a basic WordPress-compatible CSP as a starting point.', 'arkhost-security-pack' ); ?></span>
						</p>
						<p class="description">
							<strong><?php esc_html_e( 'Warning:', 'arkhost-security-pack' ); ?></strong> <?php esc_html_e( 'Incorrect CSP can break your site. Test thoroughly. Leave empty to disable.', 'arkhost-security-pack' ); ?>
						</p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Strict-Transport-Security', 'arkhost-security-pack' ); ?></th>
					<td>
						<input type="text" name="arksp_settings[security_headers][strict_transport_security]" id="header-strict_transport_security"
							   value="<?php echo esc_attr( $headers['strict_transport_security'] ?? 'max-age=31536000; includeSubDomains' ); ?>" class="large-text"
							   data-default="max-age=31536000; includeSubDomains" />
						<p class="description"><?php esc_html_e( 'Forces HTTPS. 31536000 = 1 year.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
			</table>
		</div>
		<?php
	}

	/**
	 * Render 2FA tab.
	 */
	private function render_2fa_tab() {
		// Count users with 2FA enabled.
		$users_with_2fa = get_users( array(
			'meta_key'   => '_arksp_2fa_enabled', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Required for 2FA user lookup.
			'meta_value' => '1', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- Required for 2FA user lookup.
			'fields'     => 'ID',
		) );
		$total_admins   = count( get_users( array(
			'role'   => 'administrator',
			'fields' => 'ID',
		) ) );
		$admins_with_2fa = 0;
		foreach ( $users_with_2fa as $user_id ) {
			if ( user_can( $user_id, 'manage_options' ) ) {
				$admins_with_2fa++;
			}
		}
		?>
		<input type="hidden" name="arksp_settings[arksp_current_tab]" value="2fa" />
		<div class="arksp-section">
			<h2><?php esc_html_e( 'Two-Factor Authentication', 'arkhost-security-pack' ); ?></h2>
			<p class="description"><?php esc_html_e( 'Requires a code from your phone app after password. Stops attackers even if they steal your password.', 'arkhost-security-pack' ); ?></p>

			<?php if ( count( $users_with_2fa ) > 0 || $total_admins > 0 ) : ?>
			<div class="arksp-stats" style="margin: 15px 0;">
				<div class="arksp-stat">
					<span class="arksp-stat-value"><?php echo esc_html( count( $users_with_2fa ) ); ?></span>
					<span class="arksp-stat-label"><?php esc_html_e( 'Users with 2FA', 'arkhost-security-pack' ); ?></span>
				</div>
				<div class="arksp-stat">
					<span class="arksp-stat-value"><?php echo esc_html( $admins_with_2fa . '/' . $total_admins ); ?></span>
					<span class="arksp-stat-label"><?php esc_html_e( 'Admins with 2FA', 'arkhost-security-pack' ); ?></span>
				</div>
			</div>
			<?php endif; ?>

			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Enable 2FA', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[two_factor_enabled]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'two_factor_enabled', false ) ); ?> />
							<?php esc_html_e( 'Allow users to enable Two-Factor Authentication', 'arkhost-security-pack' ); ?>
						</label>
						<p class="description"><?php esc_html_e( 'When enabled, users can set up 2FA from their Profile page.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Enforce for Admins', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[two_factor_enforce_admin]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'two_factor_enforce_admin', false ) ); ?> />
							<?php esc_html_e( 'Require 2FA for administrator accounts', 'arkhost-security-pack' ); ?>
						</label>
						<p class="description"><?php esc_html_e( 'Admins must set up 2FA to access dashboard.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
			</table>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'Compatible Authenticator Apps', 'arkhost-security-pack' ); ?></h2>
			<p class="description"><?php esc_html_e( 'Any TOTP app works:', 'arkhost-security-pack' ); ?></p>
			<ul style="list-style: disc; margin-left: 20px;">
				<li><strong>Google Authenticator</strong> - <?php esc_html_e( 'iOS & Android', 'arkhost-security-pack' ); ?></li>
				<li><strong>Authy</strong> - <?php esc_html_e( 'iOS, Android, Desktop (with cloud backup)', 'arkhost-security-pack' ); ?></li>
				<li><strong>Microsoft Authenticator</strong> - <?php esc_html_e( 'iOS & Android', 'arkhost-security-pack' ); ?></li>
				<li><strong>1Password</strong> - <?php esc_html_e( 'iOS, Android, Desktop', 'arkhost-security-pack' ); ?></li>
				<li><strong>Bitwarden</strong> - <?php esc_html_e( 'All platforms (premium feature)', 'arkhost-security-pack' ); ?></li>
			</ul>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'How it works', 'arkhost-security-pack' ); ?></h2>
			<ol>
				<li><?php esc_html_e( 'Enable 2FA above and save settings.', 'arkhost-security-pack' ); ?></li>
				<li>
					<?php
					printf(
						/* translators: %s: Link to profile page */
						esc_html__( 'Users go to their %s and click "Set Up 2FA".', 'arkhost-security-pack' ),
						'<a href="' . esc_url( admin_url( 'profile.php' ) ) . '">' . esc_html__( 'Profile page', 'arkhost-security-pack' ) . '</a>'
					);
					?>
				</li>
				<li><?php esc_html_e( 'Scan the QR code with your authenticator app or enter the secret key manually.', 'arkhost-security-pack' ); ?></li>
				<li><?php esc_html_e( 'Enter the 6-digit verification code to confirm setup.', 'arkhost-security-pack' ); ?></li>
				<li><?php esc_html_e( 'On next login, enter your password as usual, then the 2FA code from your app.', 'arkhost-security-pack' ); ?></li>
			</ol>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'Backup Codes', 'arkhost-security-pack' ); ?></h2>
			<p class="description">
				<?php esc_html_e( '10 one-time backup codes are generated at setup.', 'arkhost-security-pack' ); ?>
			</p>
			<ul style="list-style: disc; margin-left: 20px; color: #666;">
				<li><?php esc_html_e( 'Codes are shown ONCE at setup - users must save them immediately', 'arkhost-security-pack' ); ?></li>
				<li><?php esc_html_e( 'Codes are stored securely hashed (not viewable later)', 'arkhost-security-pack' ); ?></li>
				<li><?php esc_html_e( 'Users can regenerate codes from their Profile (invalidates old codes)', 'arkhost-security-pack' ); ?></li>
			</ul>
		</div>

		<?php
	}

	/**
	 * Render scanner tab.
	 */
	private function render_scanner_tab() {
		$file_integrity = new ARKSP_File_Integrity();
		$file_results   = $file_integrity->get_last_scan_results();

		$malware_scanner = new ARKSP_Malware_Scanner();
		$malware_results = $malware_scanner->get_last_scan_results();

		$file_changes_count   = count( $file_results['changes']['modified'] ?? array() );
		$malware_issues_count = count( $malware_results['results'] ?? array() );
		?>
		<input type="hidden" name="arksp_settings[arksp_current_tab]" value="scanner" />

		<?php if ( $file_results['time'] > 0 || $malware_results['time'] > 0 ) : ?>
		<div class="arksp-stats" style="margin-bottom: 20px;">
			<div class="arksp-stat">
				<span class="arksp-stat-value" style="color: <?php echo $file_changes_count > 0 ? '#dc3232' : '#46b450'; ?>;">
					<?php echo $file_changes_count > 0 ? esc_html( $file_changes_count ) : '✓'; ?>
				</span>
				<span class="arksp-stat-label"><?php esc_html_e( 'Core File Issues', 'arkhost-security-pack' ); ?></span>
			</div>
			<div class="arksp-stat">
				<span class="arksp-stat-value" style="color: <?php echo $malware_issues_count > 0 ? '#dc3232' : '#46b450'; ?>;">
					<?php echo $malware_issues_count > 0 ? esc_html( $malware_issues_count ) : '✓'; ?>
				</span>
				<span class="arksp-stat-label"><?php esc_html_e( 'Suspicious Files', 'arkhost-security-pack' ); ?></span>
			</div>
		</div>
		<?php endif; ?>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'File Integrity Monitoring', 'arkhost-security-pack' ); ?></h2>
			<p class="description"><?php esc_html_e( 'Detects if core WordPress files have been modified. Catches hacked files.', 'arkhost-security-pack' ); ?></p>
			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Enable', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[file_integrity_enabled]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'file_integrity_enabled', true ) ); ?> />
							<?php esc_html_e( 'Monitor WordPress core files for changes', 'arkhost-security-pack' ); ?>
						</label>
						<p class="description"><?php esc_html_e( 'Runs automatically once per day.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Last Scan', 'arkhost-security-pack' ); ?></th>
					<td>
						<?php if ( $file_results['time'] > 0 ) : ?>
							<?php echo esc_html( human_time_diff( $file_results['time'] ) . ' ' . __( 'ago', 'arkhost-security-pack' ) ); ?>
						<?php else : ?>
							<?php esc_html_e( 'Never', 'arkhost-security-pack' ); ?>
						<?php endif; ?>
						<button type="button" class="button" id="arksp-run-file-scan">
							<?php esc_html_e( 'Run Scan Now', 'arkhost-security-pack' ); ?>
						</button>
						<?php if ( $file_results['time'] > 0 ) : ?>
							<button type="button" class="button" id="arksp-reset-baseline">
								<?php esc_html_e( 'Reset Baseline', 'arkhost-security-pack' ); ?>
							</button>
						<?php endif; ?>
					</td>
				</tr>
			</table>

			<?php if ( ! empty( $file_results['changes']['modified'] ) ) : ?>
			<h3><?php esc_html_e( 'Modified Core Files', 'arkhost-security-pack' ); ?></h3>
			<div class="notice notice-warning inline" style="margin: 10px 0;">
				<p><?php esc_html_e( 'Files differ from WordPress.org. Could be: WP update (normal), manual edit, or compromise.', 'arkhost-security-pack' ); ?></p>
			</div>
			<table class="widefat striped">
				<thead>
					<tr>
						<th><?php esc_html_e( 'File', 'arkhost-security-pack' ); ?></th>
						<th><?php esc_html_e( 'Status', 'arkhost-security-pack' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $file_results['changes']['modified'] as $file ) : ?>
					<tr>
						<td><code><?php echo esc_html( is_array( $file ) ? $file['file'] : $file ); ?></code></td>
						<td><span class="arksp-status arksp-status-warning"><?php esc_html_e( 'Modified', 'arkhost-security-pack' ); ?></span></td>
					</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
			<p style="margin-top: 10px;">
				<button type="button" class="button" id="arksp-dismiss-file-changes">
					<?php esc_html_e( 'Dismiss & Update Baseline', 'arkhost-security-pack' ); ?>
				</button>
				<span class="description" style="margin-left: 10px;"><?php esc_html_e( 'Use this after verifying the changes are legitimate.', 'arkhost-security-pack' ); ?></span>
			</p>
			<?php elseif ( $file_results['time'] > 0 ) : ?>
			<div class="notice notice-success inline" style="margin: 10px 0;">
				<p><?php esc_html_e( 'All core files match official WordPress.org checksums.', 'arkhost-security-pack' ); ?></p>
			</div>
			<?php endif; ?>
		</div>

		<?php
		?>
		<div class="arksp-section">
			<h2><?php esc_html_e( 'Malware Scanner', 'arkhost-security-pack' ); ?></h2>
			<p class="arksp-description"><?php esc_html_e( 'Looks for backdoors, web shells, and suspicious code in plugins, themes, and uploads.', 'arkhost-security-pack' ); ?></p>

			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Enable', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[malware_scan_enabled]" value="1"
								<?php checked( ARKSP_Plugin::get_setting( 'malware_scan_enabled', true ) ); ?> />
							<?php esc_html_e( 'Enable scheduled malware scanning', 'arkhost-security-pack' ); ?>
						</label>
						<p class="description"><?php esc_html_e( 'Runs weekly. Manual scans anytime.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Last Scan', 'arkhost-security-pack' ); ?></th>
					<td>
						<?php
						$scan_stats = get_option( 'arksp_malware_scan_stats', array() );
						if ( $malware_results['time'] > 0 ) :
						?>
							<?php echo esc_html( human_time_diff( $malware_results['time'] ) . ' ' . __( 'ago', 'arkhost-security-pack' ) ); ?>
							<?php if ( ! empty( $scan_stats['files_scanned'] ) ) : ?>
								<span class="description" style="margin-left: 10px;">
									<?php
									printf(
										/* translators: 1: Number of files, 2: Duration in seconds */
										esc_html__( '(%1$s files in %2$ss)', 'arkhost-security-pack' ),
										esc_html( number_format_i18n( $scan_stats['files_scanned'] ) ),
										esc_html( $scan_stats['duration'] )
									);
									?>
								</span>
							<?php endif; ?>
						<?php else : ?>
							<?php esc_html_e( 'Never', 'arkhost-security-pack' ); ?>
						<?php endif; ?>
						<button type="button" class="button" id="arksp-run-malware-scan">
							<?php esc_html_e( 'Run Scan Now', 'arkhost-security-pack' ); ?>
						</button>
						<span id="arksp-scan-status"></span>
					</td>
				</tr>
			</table>

			<?php if ( ! empty( $malware_results['results'] ) ) : ?>
			<h3><?php esc_html_e( 'Suspicious Files Found', 'arkhost-security-pack' ); ?></h3>
			<div class="notice notice-warning inline" style="margin: 10px 0;">
				<p>
					<?php esc_html_e( 'Review each finding - some may be false positives.', 'arkhost-security-pack' ); ?>
				</p>
			</div>
			<table class="widefat striped">
				<thead>
					<tr>
						<th><?php esc_html_e( 'File', 'arkhost-security-pack' ); ?></th>
						<th><?php esc_html_e( 'Finding', 'arkhost-security-pack' ); ?></th>
						<th style="width: 120px;"><?php esc_html_e( 'Actions', 'arkhost-security-pack' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $malware_results['results'] as $file => $findings ) : ?>
					<tr data-file="<?php echo esc_attr( $file ); ?>">
						<td><code><?php echo esc_html( str_replace( ABSPATH, '', $file ) ); ?></code></td>
						<td>
							<?php foreach ( $findings as $finding ) : ?>
								<span class="arksp-badge" style="background-color: <?php echo esc_attr( ARKSP_Malware_Scanner::get_severity_color( $finding['severity'] ) ); ?>;">
									<?php echo esc_html( strtoupper( $finding['severity'] ) ); ?>
								</span>
								<?php echo esc_html( $finding['name'] ); ?>
								<span class="description" style="display: block; font-size: 12px; color: #666;">
									<?php echo esc_html( $finding['description'] ); ?>
								</span>
							<?php endforeach; ?>
						</td>
						<td>
							<button type="button" class="button button-small arksp-quarantine-file" data-file="<?php echo esc_attr( $file ); ?>">
								<?php esc_html_e( 'Quarantine', 'arkhost-security-pack' ); ?>
							</button>
						</td>
					</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
			<?php endif; ?>

			<?php if ( ! empty( $malware_results['results'] ) ) : ?>
			<p style="margin-top: 10px;">
				<button type="button" class="button" id="arksp-clear-malware-results">
					<?php esc_html_e( 'Clear Results', 'arkhost-security-pack' ); ?>
				</button>
			</p>
			<?php elseif ( $malware_results['time'] > 0 ) : ?>
			<div class="notice notice-success inline" style="margin: 10px 0;">
				<p><?php esc_html_e( 'No malware or suspicious files detected.', 'arkhost-security-pack' ); ?></p>
			</div>
			<?php endif; ?>

			<?php
			$quarantined_files = $malware_scanner->get_quarantined_files();
			if ( ! empty( $quarantined_files ) ) :
			?>
			<h3 style="margin-top: 20px;"><?php esc_html_e( 'Quarantined Files', 'arkhost-security-pack' ); ?></h3>
			<table class="widefat striped" style="margin-top: 10px;">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Original Location', 'arkhost-security-pack' ); ?></th>
						<th><?php esc_html_e( 'Quarantined', 'arkhost-security-pack' ); ?></th>
						<th><?php esc_html_e( 'Size', 'arkhost-security-pack' ); ?></th>
						<th style="width: 180px;"><?php esc_html_e( 'Actions', 'arkhost-security-pack' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $quarantined_files as $name => $meta ) : ?>
					<tr data-quarantine-name="<?php echo esc_attr( $name ); ?>">
						<td><code><?php echo esc_html( $meta['relative_path'] ?? $meta['original_name'] ); ?></code></td>
						<td><?php echo esc_html( human_time_diff( $meta['quarantined_at'] ) . ' ' . __( 'ago', 'arkhost-security-pack' ) ); ?></td>
						<td><?php echo esc_html( size_format( $meta['file_size'] ?? 0 ) ); ?></td>
						<td>
							<button type="button" class="button button-small arksp-restore-file" data-name="<?php echo esc_attr( $name ); ?>">
								<?php esc_html_e( 'Restore', 'arkhost-security-pack' ); ?>
							</button>
							<button type="button" class="button button-small arksp-delete-quarantined" data-name="<?php echo esc_attr( $name ); ?>" style="color: #a00;">
								<?php esc_html_e( 'Delete', 'arkhost-security-pack' ); ?>
							</button>
						</td>
					</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
			<?php endif; ?>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'Scan Coverage', 'arkhost-security-pack' ); ?></h2>

			<h3><?php esc_html_e( 'Directories Scanned', 'arkhost-security-pack' ); ?></h3>
			<ul style="list-style: disc; margin-left: 20px; color: #666;">
				<li><strong><?php esc_html_e( 'File Integrity:', 'arkhost-security-pack' ); ?></strong> <?php esc_html_e( 'wp-admin/, wp-includes/, and root WordPress files', 'arkhost-security-pack' ); ?></li>
				<li><strong><?php esc_html_e( 'Malware Scanner:', 'arkhost-security-pack' ); ?></strong> <?php esc_html_e( 'wp-content/plugins/, wp-content/themes/, wp-content/uploads/, wp-content/mu-plugins/', 'arkhost-security-pack' ); ?></li>
			</ul>

			<h3><?php esc_html_e( 'Detection Methods', 'arkhost-security-pack' ); ?></h3>
			<table class="widefat striped" style="max-width: 600px;">
				<tr>
					<td><strong><?php esc_html_e( 'Checksum verification', 'arkhost-security-pack' ); ?></strong></td>
					<td><?php esc_html_e( 'Compares core files against WordPress.org checksums', 'arkhost-security-pack' ); ?></td>
					<td><span style="color: green;">✓ <?php esc_html_e( '100% accurate', 'arkhost-security-pack' ); ?></span></td>
				</tr>
				<tr>
					<td><strong><?php esc_html_e( 'Pattern-based', 'arkhost-security-pack' ); ?></strong></td>
					<td><?php esc_html_e( 'Scans code for suspicious patterns and functions', 'arkhost-security-pack' ); ?></td>
					<td><span style="color: orange;">⚠ <?php esc_html_e( 'May have false positives', 'arkhost-security-pack' ); ?></span></td>
				</tr>
			</table>
		</div>
		<?php
	}

	/**
	 * Render logs tab.
	 */
	private function render_logs_tab() {
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$page        = isset( $_GET['paged'] ) ? max( 1, absint( $_GET['paged'] ) ) : 1;
		$per_page    = 50;
		$offset      = ( $page - 1 ) * $per_page;

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$filter_type = isset( $_GET['event_type'] ) ? sanitize_text_field( wp_unslash( $_GET['event_type'] ) ) : '';

		$logs = ARKSP_Activity_Log::get_logs( array(
			'limit'      => $per_page,
			'offset'     => $offset,
			'event_type' => $filter_type,
		) );

		$total = ARKSP_Activity_Log::get_log_count( array( 'event_type' => $filter_type ) );
		$pages = ceil( $total / $per_page );

		$stats = ARKSP_Activity_Log::get_stats( 30 );
		?>
		<input type="hidden" name="arksp_settings[arksp_current_tab]" value="logs" />
		<div class="arksp-section">
			<h2><?php esc_html_e( 'Activity Log', 'arkhost-security-pack' ); ?></h2>
			<p class="description"><?php esc_html_e( 'Login attempts, lockouts, and blocks. See who\'s trying to get in.', 'arkhost-security-pack' ); ?></p>

			<h3><?php esc_html_e( 'Statistics (Last 30 Days)', 'arkhost-security-pack' ); ?></h3>
			<div class="arksp-stats">
				<div class="arksp-stat arksp-stat-success">
					<span class="arksp-stat-value"><?php echo esc_html( $stats['login_success'] ); ?></span>
					<span class="arksp-stat-label"><?php esc_html_e( 'Successful Logins', 'arkhost-security-pack' ); ?></span>
				</div>
				<div class="arksp-stat arksp-stat-warning">
					<span class="arksp-stat-value"><?php echo esc_html( $stats['login_failed'] ); ?></span>
					<span class="arksp-stat-label"><?php esc_html_e( 'Failed Logins', 'arkhost-security-pack' ); ?></span>
				</div>
				<div class="arksp-stat arksp-stat-danger">
					<span class="arksp-stat-value"><?php echo esc_html( $stats['lockout'] ); ?></span>
					<span class="arksp-stat-label"><?php esc_html_e( 'Lockouts', 'arkhost-security-pack' ); ?></span>
				</div>
				<div class="arksp-stat arksp-stat-info">
					<span class="arksp-stat-value"><?php echo esc_html( $stats['ip_blocked'] + $stats['geo_blocked'] ); ?></span>
					<span class="arksp-stat-label"><?php esc_html_e( 'Blocks', 'arkhost-security-pack' ); ?></span>
				</div>
			</div>

			<h3><?php esc_html_e( 'Log Events', 'arkhost-security-pack' ); ?></h3>

			<div class="tablenav top">
				<div class="alignleft actions">
					<select id="arksp-filter-type">
						<option value=""><?php esc_html_e( 'All Events', 'arkhost-security-pack' ); ?></option>
						<option value="login_success" <?php selected( $filter_type, 'login_success' ); ?>><?php esc_html_e( 'Login Success', 'arkhost-security-pack' ); ?></option>
						<option value="login_failed" <?php selected( $filter_type, 'login_failed' ); ?>><?php esc_html_e( 'Login Failed', 'arkhost-security-pack' ); ?></option>
						<option value="lockout" <?php selected( $filter_type, 'lockout' ); ?>><?php esc_html_e( 'Lockout', 'arkhost-security-pack' ); ?></option>
						<option value="ip_blocked" <?php selected( $filter_type, 'ip_blocked' ); ?>><?php esc_html_e( 'IP Blocked', 'arkhost-security-pack' ); ?></option>
						<option value="geo_blocked" <?php selected( $filter_type, 'geo_blocked' ); ?>><?php esc_html_e( 'Geo Blocked', 'arkhost-security-pack' ); ?></option>
					</select>
					<button type="button" class="button" id="arksp-filter-logs"><?php esc_html_e( 'Filter', 'arkhost-security-pack' ); ?></button>
				</div>
				<div class="alignright">
					<button type="button" class="button" id="arksp-export-logs"><?php esc_html_e( 'Export CSV', 'arkhost-security-pack' ); ?></button>
					<button type="button" class="button" id="arksp-clear-logs"><?php esc_html_e( 'Clear All Logs', 'arkhost-security-pack' ); ?></button>
				</div>
			</div>

			<table class="widefat striped">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Time', 'arkhost-security-pack' ); ?></th>
						<th><?php esc_html_e( 'Event', 'arkhost-security-pack' ); ?></th>
						<th style="width: 50px; text-align: center;"><?php esc_html_e( 'Country', 'arkhost-security-pack' ); ?></th>
						<th><?php esc_html_e( 'IP Address', 'arkhost-security-pack' ); ?></th>
						<th><?php esc_html_e( 'Username', 'arkhost-security-pack' ); ?></th>
						<th><?php esc_html_e( 'Details', 'arkhost-security-pack' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php if ( empty( $logs ) ) : ?>
					<tr>
						<td colspan="6"><?php esc_html_e( 'No activity logged yet.', 'arkhost-security-pack' ); ?></td>
					</tr>
					<?php else : ?>
						<?php foreach ( $logs as $log ) : ?>
						<tr>
							<td><?php echo esc_html( $log->created_at ); ?></td>
							<td>
								<span class="arksp-event-<?php echo esc_attr( $log->event_type ); ?>">
									<?php echo esc_html( ARKSP_Activity_Log::get_event_label( $log->event_type ) ); ?>
								</span>
							</td>
							<td style="text-align: center;" title="<?php echo esc_attr( $log->country_code ? $log->country_code : '' ); ?>">
								<?php if ( $log->country_code ) : ?>
									<?php echo esc_html( ARKSP_Helper::get_country_flag( $log->country_code ) ); ?>
								<?php else : ?>
									-
								<?php endif; ?>
							</td>
							<td><code><?php echo esc_html( $log->ip_address ); ?></code></td>
							<td><?php echo esc_html( $log->username ? $log->username : '-' ); ?></td>
							<td><?php echo esc_html( $log->details ? $log->details : '-' ); ?></td>
						</tr>
						<?php endforeach; ?>
					<?php endif; ?>
				</tbody>
			</table>

			<?php if ( $pages > 1 ) : ?>
			<div class="tablenav bottom">
				<div class="tablenav-pages">
					<?php
					echo wp_kses_post( paginate_links( array(
						'base'    => add_query_arg( 'paged', '%#%' ),
						'format'  => '',
						'current' => $page,
						'total'   => $pages,
					) ) );
					?>
				</div>
			</div>
			<?php endif; ?>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'Email Alerts', 'arkhost-security-pack' ); ?></h2>

			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Enable Email Alerts', 'arkhost-security-pack' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="arksp_settings[email_alerts_enabled]" value="1" <?php checked( ARKSP_Plugin::get_setting( 'email_alerts_enabled', false ) ); ?> />
							<?php esc_html_e( 'Send email notifications for security events', 'arkhost-security-pack' ); ?>
						</label>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Alert Email Address', 'arkhost-security-pack' ); ?></th>
					<td>
						<input type="email" name="arksp_settings[email_alerts_address]"
							   value="<?php echo esc_attr( ARKSP_Plugin::get_setting( 'email_alerts_address', get_option( 'admin_email' ) ) ); ?>" class="regular-text" />
						<p class="description"><?php esc_html_e( 'Leave empty to use the site admin email.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Alert Threshold', 'arkhost-security-pack' ); ?></th>
					<td>
						<input type="number" name="arksp_settings[email_alert_threshold]" min="1" max="50"
							   value="<?php echo esc_attr( ARKSP_Plugin::get_setting( 'email_alert_threshold', 3 ) ); ?>" class="small-text" />
						<?php esc_html_e( 'failed login attempts', 'arkhost-security-pack' ); ?>
					</td>
				</tr>
			</table>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'Log Settings', 'arkhost-security-pack' ); ?></h2>

			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Log Retention', 'arkhost-security-pack' ); ?></th>
					<td>
						<input type="number" name="arksp_settings[log_retention_days]" min="1" max="365"
							   value="<?php echo esc_attr( ARKSP_Plugin::get_setting( 'log_retention_days', 30 ) ); ?>" class="small-text" />
						<?php esc_html_e( 'days', 'arkhost-security-pack' ); ?>
						<p class="description"><?php esc_html_e( 'Auto-deleted daily.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
			</table>
			<?php submit_button(); ?>
		</div>
		<?php
	}

	/**
	 * Render tools tab.
	 */
	private function render_tools_tab() {
		$current_ip   = ARKSP_Helper::get_client_ip();
		$geo_blocking = arksp()->get_component( 'geo_blocking' );
		$geo_info     = $geo_blocking ? $geo_blocking->get_database_info() : array( 'exists' => false );
		$country_code = $geo_blocking && $current_ip ? $geo_blocking->get_country_code( $current_ip ) : null;
		?>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'Your Current IP', 'arkhost-security-pack' ); ?></h2>

			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Your IP Address', 'arkhost-security-pack' ); ?></th>
					<td>
						<code style="font-size: 14px; padding: 5px 10px;"><?php echo esc_html( $current_ip ); ?></code>
						<?php if ( $country_code ) : ?>
							<?php echo esc_html( ARKSP_Helper::get_country_flag( $country_code ) . ' ' . $country_code ); ?>
						<?php endif; ?>
						<?php
						$ip_control = arksp()->get_component( 'ip_control' );
						$is_whitelisted = $ip_control && $ip_control->is_whitelisted( $current_ip );
						?>
						<?php if ( $is_whitelisted ) : ?>
							<span style="color: green; margin-left: 10px;">✓ <?php esc_html_e( 'Whitelisted', 'arkhost-security-pack' ); ?></span>
						<?php else : ?>
							<span style="color: orange; margin-left: 10px;">○ <?php esc_html_e( 'Not whitelisted', 'arkhost-security-pack' ); ?></span>
						<?php endif; ?>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Add to Global Whitelist', 'arkhost-security-pack' ); ?></th>
					<td>
						<?php if ( ! $is_whitelisted ) : ?>
							<button type="button" class="button button-primary" id="arksp-whitelist-my-ip"><?php esc_html_e( 'Whitelist My IP', 'arkhost-security-pack' ); ?></button>
							<span id="arksp-whitelist-status"></span>
							<p class="description"><?php esc_html_e( 'Whitelisted IPs bypass all security checks.', 'arkhost-security-pack' ); ?></p>
						<?php else : ?>
							<span style="color: green;">✓ <?php esc_html_e( 'Your IP is already in the global whitelist.', 'arkhost-security-pack' ); ?></span>
						<?php endif; ?>
					</td>
				</tr>
			</table>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'Lockout Recovery', 'arkhost-security-pack' ); ?></h2>

			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Method 1: Wait', 'arkhost-security-pack' ); ?></th>
					<td>
						<?php
						printf(
							/* translators: %d: lockout duration in minutes */
							esc_html__( 'Lockouts automatically expire after %d minutes.', 'arkhost-security-pack' ),
							absint( ARKSP_Plugin::get_setting( 'login_lockout_duration', 15 ) )
						);
						?>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Method 2: FTP/File Manager', 'arkhost-security-pack' ); ?></th>
					<td>
						<?php esc_html_e( 'Rename or delete the plugin folder via FTP:', 'arkhost-security-pack' ); ?>
						<code>wp-content/plugins/arkhost-security-pack</code>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Method 3: Database', 'arkhost-security-pack' ); ?></th>
					<td>
						<?php esc_html_e( 'Run this SQL query in phpMyAdmin to clear all lockouts:', 'arkhost-security-pack' ); ?>
						<br><code>TRUNCATE TABLE <?php echo esc_html( $GLOBALS['wpdb']->prefix ); ?>arksp_lockouts;</code>
					</td>
				</tr>
			</table>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'Manage Lockouts', 'arkhost-security-pack' ); ?></h2>

			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Clear All Lockouts', 'arkhost-security-pack' ); ?></th>
					<td>
						<button type="button" class="button" id="arksp-clear-lockouts"><?php esc_html_e( 'Clear All Lockouts', 'arkhost-security-pack' ); ?></button>
						<span id="arksp-lockout-status"></span>
						<p class="description"><?php esc_html_e( 'Removes all temporary IP lockouts from the database.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
			</table>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'Session Management', 'arkhost-security-pack' ); ?></h2>

			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Force Logout All Users', 'arkhost-security-pack' ); ?></th>
					<td>
						<button type="button" class="button" id="arksp-force-logout-all"><?php esc_html_e( 'Logout All Users', 'arkhost-security-pack' ); ?></button>
						<span id="arksp-logout-status"></span>
						<p class="description"><?php esc_html_e( 'Everyone (including you) will need to log in again.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
			</table>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'Import / Export Settings', 'arkhost-security-pack' ); ?></h2>

			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Export Settings', 'arkhost-security-pack' ); ?></th>
					<td>
						<button type="button" class="button" id="arksp-export-settings"><?php esc_html_e( 'Export Settings', 'arkhost-security-pack' ); ?></button>
						<p class="description"><?php esc_html_e( 'Download your current settings as a JSON file.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Import Settings', 'arkhost-security-pack' ); ?></th>
					<td>
						<input type="file" id="arksp-import-file" accept=".json" />
						<button type="button" class="button" id="arksp-import-settings"><?php esc_html_e( 'Import Settings', 'arkhost-security-pack' ); ?></button>
						<span id="arksp-import-status"></span>
						<p class="description"><?php esc_html_e( 'Upload a previously exported settings file.', 'arkhost-security-pack' ); ?></p>
					</td>
				</tr>
			</table>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'System Information', 'arkhost-security-pack' ); ?></h2>

			<table class="widefat striped">
				<tr>
					<td style="width: 200px;"><strong><?php esc_html_e( 'WordPress Version', 'arkhost-security-pack' ); ?></strong></td>
					<td><?php echo esc_html( get_bloginfo( 'version' ) ); ?></td>
				</tr>
				<tr>
					<td><strong><?php esc_html_e( 'PHP Version', 'arkhost-security-pack' ); ?></strong></td>
					<td><?php echo esc_html( PHP_VERSION ); ?></td>
				</tr>
				<tr>
					<td><strong><?php esc_html_e( 'Plugin Version', 'arkhost-security-pack' ); ?></strong></td>
					<td><?php echo esc_html( ARKSP_VERSION ); ?></td>
				</tr>
				<tr>
					<td><strong><?php esc_html_e( 'Server IP Address', 'arkhost-security-pack' ); ?></strong></td>
					<td>
						<?php
						$server_ip = ARKSP_Helper::get_server_ip();
						if ( $server_ip ) :
							?>
							<code><?php echo esc_html( $server_ip ); ?></code>
						<?php else : ?>
							<em><?php esc_html_e( 'Unable to detect', 'arkhost-security-pack' ); ?></em>
						<?php endif; ?>
					</td>
				</tr>
				<tr>
					<td><strong><?php esc_html_e( 'HTTPS', 'arkhost-security-pack' ); ?></strong></td>
					<td>
						<?php if ( is_ssl() ) : ?>
							<span style="color: green;">✓</span> <?php esc_html_e( 'Yes', 'arkhost-security-pack' ); ?>
						<?php else : ?>
							<span style="color: red;">✗</span> <?php esc_html_e( 'No', 'arkhost-security-pack' ); ?>
							<em>(<?php esc_html_e( 'HTTPS is recommended', 'arkhost-security-pack' ); ?>)</em>
						<?php endif; ?>
					</td>
				</tr>
				<tr>
					<td><strong><?php esc_html_e( 'Geo Database', 'arkhost-security-pack' ); ?></strong></td>
					<td>
						<?php if ( $geo_info['exists'] ) : ?>
							<span style="color: green;">✓</span> <?php esc_html_e( 'Installed', 'arkhost-security-pack' ); ?>
							(<?php echo esc_html( size_format( $geo_info['size'] ) ); ?>)
						<?php else : ?>
							<span style="color: orange;">○</span> <?php esc_html_e( 'Not installed', 'arkhost-security-pack' ); ?>
							<em>(<?php esc_html_e( 'Required for geo-blocking', 'arkhost-security-pack' ); ?>)</em>
						<?php endif; ?>
					</td>
				</tr>
				<tr>
					<td><strong><?php esc_html_e( 'Custom Login URL', 'arkhost-security-pack' ); ?></strong></td>
					<td>
						<?php
						$custom_url = ARKSP_Plugin::get_setting( 'login_custom_url', '' );
						if ( ARKSP_Plugin::get_setting( 'login_rename_enabled', false ) && ! empty( $custom_url ) ) :
							?>
							<code><?php echo esc_html( home_url( '/' . $custom_url ) ); ?></code>
						<?php else : ?>
							<?php esc_html_e( 'Default (wp-login.php)', 'arkhost-security-pack' ); ?>
						<?php endif; ?>
					</td>
				</tr>
				<tr>
					<td><strong><?php esc_html_e( 'Server Software', 'arkhost-security-pack' ); ?></strong></td>
					<td><?php echo esc_html( isset( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) : __( 'Unknown', 'arkhost-security-pack' ) ); ?></td>
				</tr>
			</table>
		</div>

		<div class="arksp-section">
			<h2><?php esc_html_e( 'Danger Zone', 'arkhost-security-pack' ); ?></h2>
			<p class="arksp-description" style="color: #d63638;"><?php esc_html_e( 'These actions are destructive and cannot be undone.', 'arkhost-security-pack' ); ?></p>

			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Reset All Settings', 'arkhost-security-pack' ); ?></th>
					<td>
						<button type="button" class="button" id="arksp-reset-settings" style="border-color: #d63638; color: #d63638;"><?php esc_html_e( 'Reset to Defaults', 'arkhost-security-pack' ); ?></button>
					</td>
				</tr>
			</table>
		</div>
		<?php
	}

	/**
	 * AJAX: Clear logs.
	 */
	public function ajax_clear_logs() {
		check_ajax_referer( 'arksp_admin' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Permission denied.', 'arkhost-security-pack' ) ) );
		}

		ARKSP_Activity_Log::clear_all_logs();
		wp_send_json_success();
	}

	/**
	 * AJAX: Whitelist IP.
	 *
	 * Adds an IP to the whitelist setting.
	 */
	public function ajax_whitelist_ip() {
		check_ajax_referer( 'arksp_admin' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Permission denied.', 'arkhost-security-pack' ) ) );
		}

		$ip = isset( $_POST['ip'] ) ? sanitize_text_field( wp_unslash( $_POST['ip'] ) ) : '';

		if ( empty( $ip ) || ! filter_var( $ip, FILTER_VALIDATE_IP ) ) {
			wp_send_json_error( array( 'message' => __( 'Invalid IP address.', 'arkhost-security-pack' ) ) );
		}

		// Get current whitelist.
		$whitelist = ARKSP_Plugin::get_setting( 'ip_whitelist', '' );

		// Check if IP is already in whitelist.
		$whitelist_array = array_filter( array_map( 'trim', explode( "\n", $whitelist ) ) );
		if ( in_array( $ip, $whitelist_array, true ) ) {
			wp_send_json_error( array( 'message' => __( 'IP is already whitelisted.', 'arkhost-security-pack' ) ) );
		}

		// Add IP to whitelist.
		$whitelist_array[] = $ip;
		$new_whitelist     = implode( "\n", $whitelist_array );

		ARKSP_Plugin::update_setting( 'ip_whitelist', $new_whitelist );

		// Also remove from lockouts if present.
		$ip_control = arksp()->get_component( 'ip_control' );
		if ( $ip_control ) {
			$ip_control->unblock_ip( $ip );
		}

		wp_send_json_success();
	}

	/**
	 * AJAX: Run file scan.
	 */
	public function ajax_run_file_scan() {
		check_ajax_referer( 'arksp_admin' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Permission denied.', 'arkhost-security-pack' ) ) );
		}

		$file_integrity = new ARKSP_File_Integrity();
		$results        = $file_integrity->scan_core_files( true );

		wp_send_json_success( $results );
	}

	/**
	 * AJAX: Run malware scan.
	 */
	public function ajax_run_malware_scan() {
		check_ajax_referer( 'arksp_admin' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Permission denied.', 'arkhost-security-pack' ) ) );
		}

		// Increase time limit for potentially long scans.
		// phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged -- Required for comprehensive malware scanning.
		set_time_limit( 300 );

		$scanner = new ARKSP_Malware_Scanner();
		$results = $scanner->scan_files();

		update_option( ARKSP_Malware_Scanner::RESULTS_OPTION, $results );
		update_option( ARKSP_Malware_Scanner::LAST_SCAN_OPTION, time() );

		wp_send_json_success( $results );
	}

	/**
	 * AJAX: Reset file integrity baseline.
	 */
	public function ajax_reset_file_baseline() {
		check_ajax_referer( 'arksp_admin' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Permission denied.', 'arkhost-security-pack' ) ) );
		}

		$file_integrity = new ARKSP_File_Integrity();
		$file_integrity->reset_baseline();

		// Run a fresh scan to establish new baseline.
		$file_integrity->scan_core_files( true );

		wp_send_json_success();
	}

	/**
	 * AJAX: Clear malware scan results.
	 */
	public function ajax_clear_malware_results() {
		check_ajax_referer( 'arksp_admin' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Permission denied.', 'arkhost-security-pack' ) ) );
		}

		$scanner = new ARKSP_Malware_Scanner();
		$scanner->clear_results();

		wp_send_json_success();
	}

	/**
	 * AJAX: Download GeoIP database.
	 */
	public function ajax_download_geo_db() {
		check_ajax_referer( 'arksp_admin' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Permission denied.', 'arkhost-security-pack' ) ) );
		}

		$geo_blocking = arksp()->get_component( 'geo_blocking' );
		if ( ! $geo_blocking ) {
			wp_send_json_error( array( 'message' => __( 'Geo blocking not available.', 'arkhost-security-pack' ) ) );
		}

		$result = $geo_blocking->download_database();

		if ( is_wp_error( $result ) ) {
			wp_send_json_error( array( 'message' => $result->get_error_message() ) );
		}

		wp_send_json_success();
	}

	/**
	 * AJAX: Send test email.
	 */
	public function ajax_test_email() {
		check_ajax_referer( 'arksp_admin' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Permission denied.', 'arkhost-security-pack' ) ) );
		}

		$email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : '';

		if ( empty( $email ) ) {
			$email = get_option( 'admin_email' );
		}

		if ( ! is_email( $email ) ) {
			wp_send_json_error( array( 'message' => __( 'Invalid email address.', 'arkhost-security-pack' ) ) );
		}

		$site_name = get_bloginfo( 'name' );
		$subject   = sprintf(
			/* translators: %s: Site name */
			__( '[%s] ArkHost Security Pack - Test Email', 'arkhost-security-pack' ),
			$site_name
		);

		$message = sprintf(
			/* translators: 1: Site name, 2: Site URL */
			__( "This is a test email from ArkHost Security Pack.\n\nIf you received this email, your email alerts are configured correctly.\n\nSite: %1\$s\nURL: %2\$s", 'arkhost-security-pack' ),
			$site_name,
			home_url()
		);

		$sent = wp_mail( $email, $subject, $message );

		if ( $sent ) {
			wp_send_json_success();
		} else {
			wp_send_json_error( array( 'message' => __( 'Failed to send email. Check your server mail configuration.', 'arkhost-security-pack' ) ) );
		}
	}

	/**
	 * AJAX: Force logout all users.
	 */
	public function ajax_force_logout_all() {
		check_ajax_referer( 'arksp_admin' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Permission denied.', 'arkhost-security-pack' ) ) );
		}

		// Get all users and destroy their sessions.
		$users = get_users( array( 'fields' => 'ID' ) );
		foreach ( $users as $user_id ) {
			$sessions = WP_Session_Tokens::get_instance( $user_id );
			$sessions->destroy_all();
		}

		// Log the action.
		ARKSP_Activity_Log::log( 'force_logout', ARKSP_Helper::get_client_ip(), wp_get_current_user()->user_login, __( 'All user sessions terminated', 'arkhost-security-pack' ) );

		wp_send_json_success();
	}

	/**
	 * AJAX: Export settings.
	 */
	public function ajax_export_settings() {
		check_ajax_referer( 'arksp_admin' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Permission denied.', 'arkhost-security-pack' ) ) );
		}

		$settings = get_option( 'arksp_settings', array() );

		wp_send_json_success( $settings );
	}

	/**
	 * AJAX: Import settings.
	 */
	public function ajax_import_settings() {
		check_ajax_referer( 'arksp_admin' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Permission denied.', 'arkhost-security-pack' ) ) );
		}

		// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- JSON is decoded then sanitized via sanitize_settings().
		$settings_json = isset( $_POST['settings'] ) ? wp_unslash( $_POST['settings'] ) : '';

		if ( empty( $settings_json ) ) {
			wp_send_json_error( array( 'message' => __( 'No settings data provided.', 'arkhost-security-pack' ) ) );
		}

		$settings = json_decode( $settings_json, true );

		if ( null === $settings ) {
			wp_send_json_error( array( 'message' => __( 'Invalid JSON format.', 'arkhost-security-pack' ) ) );
		}

		// Sanitize the imported settings (individual values are sanitized here).
		$sanitized = $this->sanitize_settings( $settings );

		update_option( 'arksp_settings', $sanitized );

		wp_send_json_success();
	}

	/**
	 * AJAX: Export logs as CSV.
	 */
	public function ajax_export_logs() {
		check_ajax_referer( 'arksp_admin' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Permission denied.', 'arkhost-security-pack' ) ) );
		}

		$logs = ARKSP_Activity_Log::get_logs( array( 'limit' => 10000 ) );

		$csv_lines   = array();
		$csv_lines[] = 'Time,Event,IP Address,Country,Username,Details,User Agent';

		foreach ( $logs as $log ) {
			$csv_lines[] = sprintf(
				'"%s","%s","%s","%s","%s","%s","%s"',
				str_replace( '"', '""', $log->created_at ),
				str_replace( '"', '""', ARKSP_Activity_Log::get_event_label( $log->event_type ) ),
				str_replace( '"', '""', $log->ip_address ),
				str_replace( '"', '""', $log->country_code ? $log->country_code : '' ),
				str_replace( '"', '""', $log->username ? $log->username : '' ),
				str_replace( '"', '""', $log->details ? $log->details : '' ),
				str_replace( '"', '""', $log->user_agent ? $log->user_agent : '' )
			);
		}

		wp_send_json_success( array( 'csv' => implode( "\n", $csv_lines ) ) );
	}

	/**
	 * AJAX: Unblock a single IP.
	 */
	public function ajax_unblock_ip() {
		check_ajax_referer( 'arksp_admin' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Permission denied.', 'arkhost-security-pack' ) ) );
		}

		$ip = isset( $_POST['ip'] ) ? sanitize_text_field( wp_unslash( $_POST['ip'] ) ) : '';

		if ( empty( $ip ) || ! filter_var( $ip, FILTER_VALIDATE_IP ) ) {
			wp_send_json_error( array( 'message' => __( 'Invalid IP address.', 'arkhost-security-pack' ) ) );
		}

		$ip_control = arksp()->get_component( 'ip_control' );
		if ( $ip_control ) {
			$ip_control->unblock_ip( $ip );
		}

		wp_send_json_success();
	}

	/**
	 * AJAX: Clear all lockouts.
	 */
	public function ajax_clear_lockouts() {
		check_ajax_referer( 'arksp_admin' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Permission denied.', 'arkhost-security-pack' ) ) );
		}

		global $wpdb;

		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Admin action to clear lockouts.
		$wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}arksp_lockouts" );

		wp_send_json_success();
	}

	/**
	 * AJAX: Reset all settings to defaults.
	 */
	public function ajax_reset_settings() {
		check_ajax_referer( 'arksp_admin' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Permission denied.', 'arkhost-security-pack' ) ) );
		}

		$defaults = ARKSP_Plugin::get_default_settings();
		update_option( 'arksp_settings', $defaults );

		wp_send_json_success();
	}

	/**
	 * AJAX: Quarantine a suspicious file.
	 */
	public function ajax_quarantine_file() {
		check_ajax_referer( 'arksp_admin' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Permission denied.', 'arkhost-security-pack' ) ) );
		}

		$file_path = isset( $_POST['file_path'] ) ? sanitize_text_field( wp_unslash( $_POST['file_path'] ) ) : '';

		if ( empty( $file_path ) ) {
			wp_send_json_error( array( 'message' => __( 'No file specified.', 'arkhost-security-pack' ) ) );
		}

		$scanner = new ARKSP_Malware_Scanner();
		$result  = $scanner->quarantine_file( $file_path );

		if ( is_wp_error( $result ) ) {
			wp_send_json_error( array( 'message' => $result->get_error_message() ) );
		}

		wp_send_json_success( $result );
	}

	/**
	 * AJAX: Restore a file from quarantine.
	 */
	public function ajax_restore_file() {
		check_ajax_referer( 'arksp_admin' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Permission denied.', 'arkhost-security-pack' ) ) );
		}

		$quarantine_name = isset( $_POST['quarantine_name'] ) ? sanitize_file_name( wp_unslash( $_POST['quarantine_name'] ) ) : '';

		if ( empty( $quarantine_name ) ) {
			wp_send_json_error( array( 'message' => __( 'No file specified.', 'arkhost-security-pack' ) ) );
		}

		$scanner = new ARKSP_Malware_Scanner();
		$result  = $scanner->restore_file( $quarantine_name );

		if ( is_wp_error( $result ) ) {
			wp_send_json_error( array( 'message' => $result->get_error_message() ) );
		}

		wp_send_json_success( $result );
	}

	/**
	 * AJAX: Delete a quarantined file permanently.
	 */
	public function ajax_delete_quarantined() {
		check_ajax_referer( 'arksp_admin' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Permission denied.', 'arkhost-security-pack' ) ) );
		}

		$quarantine_name = isset( $_POST['quarantine_name'] ) ? sanitize_file_name( wp_unslash( $_POST['quarantine_name'] ) ) : '';

		if ( empty( $quarantine_name ) ) {
			wp_send_json_error( array( 'message' => __( 'No file specified.', 'arkhost-security-pack' ) ) );
		}

		$scanner = new ARKSP_Malware_Scanner();
		$result  = $scanner->delete_quarantined_file( $quarantine_name );

		if ( is_wp_error( $result ) ) {
			wp_send_json_error( array( 'message' => $result->get_error_message() ) );
		}

		wp_send_json_success( $result );
	}

	/**
	 * AJAX: Delete WordPress info files (readme.html, license.txt).
	 */
	public function ajax_delete_wp_file() {
		check_ajax_referer( 'arksp_admin' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Permission denied.', 'arkhost-security-pack' ) ) );
		}

		$file = isset( $_POST['file'] ) ? sanitize_file_name( wp_unslash( $_POST['file'] ) ) : '';

		// Only allow specific safe files to be deleted.
		$allowed_files = array( 'readme.html', 'license.txt' );

		if ( ! in_array( $file, $allowed_files, true ) ) {
			wp_send_json_error( array( 'message' => __( 'Invalid file.', 'arkhost-security-pack' ) ) );
		}

		$file_path = ABSPATH . $file;

		if ( ! file_exists( $file_path ) ) {
			wp_send_json_error( array( 'message' => __( 'File not found.', 'arkhost-security-pack' ) ) );
		}

		// phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink
		if ( ! unlink( $file_path ) ) {
			wp_send_json_error( array( 'message' => __( 'Failed to delete file. Check file permissions.', 'arkhost-security-pack' ) ) );
		}

		wp_send_json_success();
	}
}
