<?php
/**
 * Mail Doctor settings scaffold.
 *
 * @var string               $mail_doctor_active_tab
 * @var string               $mail_doctor_section
 * @var string               $mail_doctor_base_url
 * @var array<string,mixed>  $mail_doctor_settings
 * @var array<string,string> $mail_doctor_provider_options
 * @var array<string,string> $mail_doctor_sections
 * @var array<int,array<string,mixed>> $mail_doctor_profile_rows
 * @var array<string,string> $mail_doctor_profile_choices
 * @var string $mail_doctor_active_profile
 * @var array<string,array<string,string>> $mail_doctor_profile_assignments
 *
 * @package MailDoctor
 */

use MailDoctor\Admin;
use MailDoctor\Helpers\Settings as HelperSettings;

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

$mail_doctor_tabs                  = Admin::get_tabs();
$mail_doctor_woocommerce_available = isset( $mail_doctor_woocommerce_available ) ? (bool) $mail_doctor_woocommerce_available : false;
$mail_doctor_woo_email_rows        = isset( $mail_doctor_woo_email_rows ) && is_array( $mail_doctor_woo_email_rows ) ? $mail_doctor_woo_email_rows : array();
$mail_doctor_woo_preview_emails    = isset( $mail_doctor_woo_preview_emails ) && is_array( $mail_doctor_woo_preview_emails ) ? $mail_doctor_woo_preview_emails : array();
$mail_doctor_woo_email_total       = count( $mail_doctor_woo_email_rows );

$mail_doctor_providers = array(
	'mail'      => array(
		'label'        => esc_html__( 'Default PHP Mail', 'mail-doctor' ),
		'logo'         => 'php.svg',
		'service_link' => '',
		'doc_link'     => '',
	),
	'brevo'     => array(
		'label'        => esc_html__( 'Brevo', 'mail-doctor' ),
		'logo'         => 'brevo.svg',
		'service_link' => 'https://www.brevo.com/',
		'doc_link'     => 'https://developers.brevo.com/docs/how-to-send-emails-via-smtp',
	),
	'gmail'     => array(
		'label'        => esc_html__( 'Google / Gmail', 'mail-doctor' ),
		'logo'         => 'google.svg',
		'service_link' => 'https://workspace.google.com/',
		'doc_link'     => 'https://support.google.com/a/answer/176600',
	),
	'outlook'   => array(
		'label'        => esc_html__( 'Outlook / Office 365', 'mail-doctor' ),
		'logo'         => 'microsoft.svg',
		'service_link' => 'https://outlook.live.com/',
		'doc_link'     => 'https://support.microsoft.com/en-us/office/pop-imap-and-smtp-settings-for-outlook-com-d088b986-291d-42b8-9564-9c414e2aa040',
	),
	'sendgrid'  => array(
		'label'        => esc_html__( 'SendGrid', 'mail-doctor' ),
		'logo'         => 'sendgrid.svg',
		'service_link' => 'https://sendgrid.com/',
		'doc_link'     => 'https://docs.sendgrid.com/for-developers/sending-email/getting-started-smtp',
	),
	'mailgun'   => array(
		'label'        => esc_html__( 'Mailgun', 'mail-doctor' ),
		'logo'         => 'mailgun.svg',
		'service_link' => 'https://www.mailgun.com/',
		'doc_link'     => 'https://help.mailgun.com/hc/en-us/articles/203362140-How-Do-I-Send-an-Email-Using-SMTP',
	),
	'ses'       => array(
		'label'        => esc_html__( 'Amazon SES', 'mail-doctor' ),
		'logo'         => 'aws.svg',
		'service_link' => 'https://aws.amazon.com/ses/',
		'doc_link'     => 'https://docs.aws.amazon.com/ses/latest/dg/send-email-smtp.html',
	),
	'postmark'  => array(
		'label'        => esc_html__( 'Postmark', 'mail-doctor' ),
		'logo'         => 'postmark.svg',
		'service_link' => 'https://postmarkapp.com/',
		'doc_link'     => 'https://postmarkapp.com/developer/user-guide/send-email-with-smtp',
	),
	'sparkpost' => array(
		'label'        => esc_html__( 'SparkPost', 'mail-doctor' ),
		'logo'         => 'sparkpost.svg',
		'service_link' => 'https://www.sparkpost.com/',
		'doc_link'     => 'https://developers.sparkpost.com/api/smtp/',
	),
	'smtp'      => array(
		'label'        => esc_html__( 'Other SMTP', 'mail-doctor' ),
		'logo'         => 'smtp.svg',
		'service_link' => '',
		'doc_link'     => 'https://wpdoctorpro.com/docs/mail-doctor/welcome-to-mail-doctor',
	),
);
$mail_doctor_woo_overrides_total   = 0;
$mail_doctor_woo_disabled_total    = 0;
foreach ( $mail_doctor_woo_email_rows as $mail_doctor_woo_row_snapshot ) {
	if ( ! empty( $mail_doctor_woo_row_snapshot['overridden'] ) ) {
		++$mail_doctor_woo_overrides_total;
	}
	if ( empty( $mail_doctor_woo_row_snapshot['current_enabled'] ) ) {
		++$mail_doctor_woo_disabled_total;
	}
}
$mail_doctor_profile_rows         = isset( $mail_doctor_profile_rows ) && is_array( $mail_doctor_profile_rows ) ? $mail_doctor_profile_rows : array();
$mail_doctor_profile_count        = count( $mail_doctor_profile_rows );
$mail_doctor_active_profile       = $mail_doctor_active_profile ?? '';
$mail_doctor_profile_choices      = isset( $mail_doctor_profile_choices ) && is_array( $mail_doctor_profile_choices ) ? $mail_doctor_profile_choices : array();
$mail_doctor_active_profile_label = $mail_doctor_profile_choices[ $mail_doctor_active_profile ] ?? ( $mail_doctor_profile_choices['default'] ?? esc_html__( 'Default SMTP Settings', 'mail-doctor' ) );
$mail_doctor_profile_roles_total  = 0;
$mail_doctor_profile_sites_total  = 0;
$mail_doctor_profile_emails_total = 0;
$mail_doctor_recipe_placeholders  = isset( $mail_doctor_recipe_placeholders ) && is_array( $mail_doctor_recipe_placeholders ) ? $mail_doctor_recipe_placeholders : array();

foreach ( $mail_doctor_profile_rows as $mail_doctor_profile_snapshot ) {
	$mail_doctor_rules                 = isset( $mail_doctor_profile_snapshot['scope']['rules'] ) && is_array( $mail_doctor_profile_snapshot['scope']['rules'] ) ? $mail_doctor_profile_snapshot['scope']['rules'] : array();
	$mail_doctor_profile_roles_total  += isset( $mail_doctor_rules['roles'] ) && is_array( $mail_doctor_rules['roles'] ) ? count( $mail_doctor_rules['roles'] ) : 0;
	$mail_doctor_profile_sites_total  += isset( $mail_doctor_rules['sites'] ) && is_array( $mail_doctor_rules['sites'] ) ? count( $mail_doctor_rules['sites'] ) : 0;
	$mail_doctor_profile_emails_total += isset( $mail_doctor_rules['emails'] ) && is_array( $mail_doctor_rules['emails'] ) ? count( $mail_doctor_rules['emails'] ) : 0;
}
$mail_doctor_api_keys         = isset( $mail_doctor_api_keys ) && is_array( $mail_doctor_api_keys ) ? $mail_doctor_api_keys : array();
$mail_doctor_api_key_count    = count( $mail_doctor_api_keys );
$mail_doctor_transport_key    = $mail_doctor_settings['transport'] ?? 'smtp';
$mail_doctor_transport_label  = $mail_doctor_provider_options[ $mail_doctor_transport_key ] ?? ucwords( str_replace( array( '-', '_' ), ' ', $mail_doctor_transport_key ) );
$mail_doctor_scope_label      = ( 'woo' === ( $mail_doctor_settings['scope'] ?? 'all' ) )
	? esc_html__( 'WooCommerce only', 'mail-doctor' )
	: esc_html__( 'All emails', 'mail-doctor' );
$mail_doctor_fallback_enabled = ! empty( $mail_doctor_settings['fallback_transport'] );
$mail_doctor_cron_active      = ! empty( $mail_doctor_settings['cron_active'] );
$mail_doctor_webhook_enabled  = ! empty( $mail_doctor_settings['webhook_enabled'] );
$mail_doctor_retention_days   = isset( $mail_doctor_settings['retention_sent'] ) ? (int) $mail_doctor_settings['retention_sent'] : 90;
$mail_doctor_async_choices    = HelperSettings::async_mode_choices();
$mail_doctor_async_key        = $mail_doctor_settings['async_mode'] ?? 'sync';
$mail_doctor_async_label      = $mail_doctor_async_choices[ $mail_doctor_async_key ] ?? ucwords( str_replace( '_', ' ', $mail_doctor_async_key ) );

$mail_doctor_section_meta = array(
	'smtp'         => array(
		'label'   => $mail_doctor_transport_label,
		'class'   => 'status-positive',
		'tooltip' => esc_html__( 'Active transport', 'mail-doctor' ),
	),
	'profiles'     => array(
		'label'   => sprintf(
			/* translators: %d: number of stored profiles */
			_n( '%d profile', '%d profiles', $mail_doctor_profile_count, 'mail-doctor' ),
			$mail_doctor_profile_count
		),
		'class'   => $mail_doctor_profile_count ? 'status-neutral' : 'status-muted',
		'tooltip' => esc_html__( 'Reusable SMTP/API configurations', 'mail-doctor' ),
	),
	'retry'        => array(
		'label'   => $mail_doctor_cron_active ? esc_html__( 'Queue ready', 'mail-doctor' ) : esc_html__( 'Cron missing', 'mail-doctor' ),
		'class'   => $mail_doctor_cron_active ? 'status-positive' : 'status-warning',
		'tooltip' => esc_html__( 'Background dispatcher status', 'mail-doctor' ),
	),
	'logging'      => array(
		'label'   => sprintf(
			/* translators: %d: retention days */
			esc_html__( '%d-day window', 'mail-doctor' ),
			$mail_doctor_retention_days
		),
		'class'   => 'status-neutral',
		'tooltip' => esc_html__( 'Log retention period', 'mail-doctor' ),
	),
	'woocommerce'  => array(
		'label'   => $mail_doctor_woocommerce_available ? esc_html__( 'Woo active', 'mail-doctor' ) : esc_html__( 'Woo missing', 'mail-doctor' ),
		'class'   => $mail_doctor_woocommerce_available ? 'status-positive' : 'status-warning',
		'tooltip' => esc_html__( 'WooCommerce availability', 'mail-doctor' ),
	),

	'advanced'     => array(
		'label'   => $mail_doctor_async_label,
		'class'   => 'status-neutral',
		'tooltip' => esc_html__( 'Async delivery mode', 'mail-doctor' ),
	),
);
?>
<div class="wrap maildoctor-admin-wrap">

	<?php settings_errors( 'maildoctor' ); ?>

	<div class="mail-doctor-settings">
		<nav class="mail-doctor-settings-nav" aria-label="<?php esc_attr_e( 'Settings sections', 'mail-doctor' ); ?>">
			<button type="button" class="mail-doctor-settings-nav__toggle" aria-expanded="false"
				aria-controls="mail-doctor-settings-nav-list">
				<span
					class="mail-doctor-settings-nav__toggle-label"><?php esc_html_e( 'Settings sections', 'mail-doctor' ); ?></span>
				<span class="dashicons dashicons-menu" aria-hidden="true"></span>
			</button>
			<ul id="mail-doctor-settings-nav-list">
				<?php
				$mail_doctor_section_icons = array(
					'smtp'         => 'dashicons-email-alt2',
					'profiles'     => 'dashicons-id-alt',
					'retry'        => 'dashicons-update',
					'logging'      => 'dashicons-shield',
					'woocommerce'  => 'dashicons-cart',

					'advanced'     => 'dashicons-admin-tools',
				);
				foreach ( $mail_doctor_sections as $mail_doctor_section_slug => $mail_doctor_section_label ) :
					$mail_doctor_icon = $mail_doctor_section_icons[ $mail_doctor_section_slug ] ?? 'dashicons-admin-generic';
					$mail_doctor_meta = $mail_doctor_section_meta[ $mail_doctor_section_slug ] ?? null;
					?>
					<li>
						<a href="<?php echo esc_url( add_query_arg( array( 'section' => $mail_doctor_section_slug ), $mail_doctor_base_url ) ); ?>"
							class="<?php echo esc_attr( $mail_doctor_section === $mail_doctor_section_slug ? 'is-active' : '' ); ?>"
							aria-current="<?php echo esc_attr( $mail_doctor_section === $mail_doctor_section_slug ? 'page' : 'false' ); ?>">
							<span class="dashicons <?php echo esc_attr( $mail_doctor_icon ); ?>" aria-hidden="true"></span>
							<?php echo esc_html( $mail_doctor_section_label ); ?>
							<?php if ( $mail_doctor_meta && ! empty( $mail_doctor_meta['label'] ) ) : ?>
								<span
									class="mail-doctor-settings-nav__badge <?php echo esc_attr( 'mail-doctor-settings-nav__badge--' . ( $mail_doctor_meta['class'] ?? 'status-neutral' ) ); ?>"
									title="<?php echo esc_attr( $mail_doctor_meta['tooltip'] ?? '' ); ?>">
									<?php echo esc_html( $mail_doctor_meta['label'] ); ?>
								</span>
							<?php endif; ?>
						</a>
					</li>
				<?php endforeach; ?>
			</ul>
		</nav>

		<div class="mail-doctor-settings-content">
			<form method="post" action="<?php echo esc_url( $mail_doctor_base_url ); ?>">
				<?php wp_nonce_field( 'mail_doctor_settings_nonce', 'mail_doctor_settings_nonce' ); ?>
				<input type="hidden" name="section" value="<?php echo esc_attr( $mail_doctor_section ); ?>" />

				<?php if ( 'smtp' === $mail_doctor_section ) : ?>
					<header class="mail-doctor-settings-header">
						<span
							class="mail-doctor-settings-header__eyebrow"><?php esc_html_e( 'Deliverability', 'mail-doctor' ); ?></span>
						<h2 class="mail-doctor-settings-header__title"><?php esc_html_e( 'SMTP & Identity', 'mail-doctor' ); ?>
						</h2>
						<p class="mail-doctor-settings-header__description">
							<?php esc_html_e( 'Configure the primary transport used for WooCommerce and WordPress transactional emails.', 'mail-doctor' ); ?>
						</p>
					</header>

					<div class="mail-doctor-section-summary" role="list">
						<div class="mail-doctor-section-summary__item" role="listitem">
							<span
								class="mail-doctor-section-summary__eyebrow"><?php esc_html_e( 'Active transport', 'mail-doctor' ); ?></span>
							<strong class="mail-doctor-section-summary__value"><?php echo esc_html( $mail_doctor_transport_label ); ?></strong>
						</div>
						<div class="mail-doctor-section-summary__item" role="listitem">
							<span class="mail-doctor-section-summary__eyebrow"><?php esc_html_e( 'Scope', 'mail-doctor' ); ?></span>
							<strong class="mail-doctor-section-summary__value"><?php echo esc_html( $mail_doctor_scope_label ); ?></strong>
						</div>
						<div class="mail-doctor-section-summary__item" role="listitem">
							<span class="mail-doctor-section-summary__eyebrow"><?php esc_html_e( 'Fallback', 'mail-doctor' ); ?></span>
							<strong
								class="mail-doctor-section-summary__value"><?php echo $mail_doctor_fallback_enabled ? esc_html__( 'Enabled', 'mail-doctor' ) : esc_html__( 'Disabled', 'mail-doctor' ); ?></strong>
						</div>
						<div class="mail-doctor-section-summary__item" role="listitem">
							<span class="mail-doctor-section-summary__eyebrow"><?php esc_html_e( 'Testing', 'mail-doctor' ); ?></span>
							<strong
								class="mail-doctor-section-summary__value"><?php esc_html_e( 'Run delivery checks below', 'mail-doctor' ); ?></strong>
						</div>
					</div>

					<div class="mail-doctor-settings-stack">
						<div class="mail-doctor-smtp-grid">
							<section class="mail-doctor-card mail-doctor-settings-panel">
								<header class="mail-doctor-settings-panel__header">
									<span
										class="mail-doctor-settings-panel__eyebrow"><?php esc_html_e( 'Defaults', 'mail-doctor' ); ?></span>
									<h3 class="mail-doctor-settings-panel__title">
										<?php esc_html_e( 'Sender identity', 'mail-doctor' ); ?>
									</h3>
								</header>

								<div class="mail-doctor-field-block">
									<span class="mail-doctor-field-block__label"><?php esc_html_e( 'Scope', 'mail-doctor' ); ?></span>
									<div class="mail-doctor-field-block__control">
										<label><input type="radio" name="mail_doctor_settings[scope]" value="all" <?php checked( $mail_doctor_settings['scope'], 'all' ); ?> />
											<?php esc_html_e( 'All emails', 'mail-doctor' ); ?></label>
										<label><input type="radio" name="mail_doctor_settings[scope]" value="woo" <?php checked( $mail_doctor_settings['scope'], 'woo' ); ?> <?php disabled( ! $mail_doctor_woocommerce_available ); ?> />
											<?php esc_html_e( 'Only WooCommerce emails', 'mail-doctor' ); ?>
											<?php if ( ! $mail_doctor_woocommerce_available ) : ?>
												<span style="font-size: 11px; color: #a10f1f;">(<?php esc_html_e( 'WooCommerce inactive', 'mail-doctor' ); ?>)</span>
											<?php endif; ?>
										</label>
									</div>
								</div>
								<div class="mail-doctor-field-block">
									<label class="mail-doctor-field-block__label"
										for="mail-doctor-from-name"><?php esc_html_e( 'From name', 'mail-doctor' ); ?></label>
									<div class="mail-doctor-field-block__control">
										<input type="text" id="mail-doctor-from-name" name="mail_doctor_settings[from_name]"
											value="<?php echo esc_attr( $mail_doctor_settings['from_name'] ); ?>" class="regular-text"
											placeholder="<?php esc_attr_e( 'e.g. My Website Team', 'mail-doctor' ); ?>" />
										<p class="description">
											<?php esc_html_e( 'The default sender name that recipients will see in their inbox (e.g., Acme Store).', 'mail-doctor' ); ?>
										</p>
									</div>
								</div>
								<div class="mail-doctor-field-block">
									<label class="mail-doctor-field-block__label"
										for="mail-doctor-from-email"><?php esc_html_e( 'From email', 'mail-doctor' ); ?></label>
									<div class="mail-doctor-field-block__control">
										<input type="email" id="mail-doctor-from-email" name="mail_doctor_settings[from_email]"
											value="<?php echo esc_attr( $mail_doctor_settings['from_email'] ); ?>" class="regular-text"
											placeholder="<?php esc_attr_e( 'e.g. support@yourdomain.com', 'mail-doctor' ); ?>" />
										<p class="description mail-doctor-warning-placeholder">
											<?php esc_html_e( 'Ensure the domain matches your authenticated sender.', 'mail-doctor' ); ?>
										</p>
									</div>
								</div>
								<div class="mail-doctor-field-block">
									<span
										class="mail-doctor-field-block__label"><?php esc_html_e( 'Lock sender identity', 'mail-doctor' ); ?></span>
									<div class="mail-doctor-field-block__control">
										<label>
											<input type="hidden" name="mail_doctor_settings[force_from_email_lock]" value="0" />
											<input type="checkbox" name="mail_doctor_settings[force_from_email_lock]" value="1" <?php checked( ! empty( $mail_doctor_settings['force_from_email_lock'] ) ); ?> />
											<?php esc_html_e( 'Force this From email on every message, even when other plugins try to change it.', 'mail-doctor' ); ?>
										</label>
										<label>
											<input type="hidden" name="mail_doctor_settings[force_from_name_lock]" value="0" />
											<input type="checkbox" name="mail_doctor_settings[force_from_name_lock]" value="1" <?php checked( ! empty( $mail_doctor_settings['force_from_name_lock'] ) ); ?> />
											<?php esc_html_e( 'Force the From name above on every message.', 'mail-doctor' ); ?>
										</label>
										<p class="description">
											<?php esc_html_e( 'Enable these safeguards when you want Mail Doctor to keep sender details consistent across the entire site.', 'mail-doctor' ); ?>
										</p>
									</div>
								</div>

							</section>

							<section class="mail-doctor-card mail-doctor-settings-panel">
								<header class="mail-doctor-settings-panel__header">
									<span
										class="mail-doctor-settings-panel__eyebrow"><?php esc_html_e( 'Routing', 'mail-doctor' ); ?></span>
									<h3 class="mail-doctor-settings-panel__title">
										<?php esc_html_e( 'Mailer Service Routing', 'mail-doctor' ); ?>
									</h3>
								</header>

								<div class="mail-doctor-field-block mail-doctor-field-block--provider-grid" style="display: block;">
									<label class="mail-doctor-field-block__label" style="margin-bottom: 12px; display: block; font-weight: 600;">
										<?php esc_html_e( 'Select Mailer Service', 'mail-doctor' ); ?>
									</label>
									<div class="mail-doctor-provider-grid">
										<?php foreach ( $mail_doctor_providers as $mail_doctor_prov_key => $mail_doctor_prov_details ) : ?>
											<?php 
											$mail_doctor_is_active = ( $mail_doctor_settings['provider'] === $mail_doctor_prov_key );
											$mail_doctor_logo_url = MAIL_DOCTOR_URL . 'assets/img/providers/' . $mail_doctor_prov_details['logo'];
											?>
											<label class="mail-doctor-provider-card <?php echo $mail_doctor_is_active ? 'is-active' : ''; ?>" data-provider="<?php echo esc_attr( $mail_doctor_prov_key ); ?>">
												<input type="radio" name="mail_doctor_settings[provider]" value="<?php echo esc_attr( $mail_doctor_prov_key ); ?>" <?php checked( $mail_doctor_settings['provider'], $mail_doctor_prov_key ); ?> />
												<div class="mail-doctor-provider-card__logo">
													<img src="<?php echo esc_url( $mail_doctor_logo_url ); ?>" alt="<?php echo esc_attr( $mail_doctor_prov_details['label'] ); ?>" />
												</div>
												<span class="mail-doctor-provider-card__label">
													<?php echo esc_html( $mail_doctor_prov_details['label'] ); ?>
												</span>
											</label>
										<?php endforeach; ?>
									</div>
									<input type="hidden" id="mail-doctor-provider" value="<?php echo esc_attr( $mail_doctor_settings['provider'] ); ?>" />
									<input type="hidden" id="mail-doctor-transport" name="mail_doctor_settings[transport]" value="<?php echo esc_attr( $mail_doctor_settings['transport'] ); ?>" />
									<script>
										var mailDoctorProvidersData = <?php echo wp_json_encode( $mail_doctor_providers ); ?>;
									</script>
								</div>

								<div id="mail-doctor-default-mail-notice" class="mail-doctor-card" style="margin-top: 16px; background: rgba(220, 53, 69, 0.05); border-left: 4px solid #dc3545; padding: 16px; <?php echo ( $mail_doctor_settings['provider'] === 'mail' ) ? '' : 'display: none;'; ?>">
									<h4 style="margin: 0 0 8px; color: #dc3545; font-weight: 600; font-size: 15px;">
										<span class="dashicons dashicons-warning" style="vertical-align: middle; margin-right: 4px;"></span>
										<?php esc_html_e( 'Default PHP Mail is selected', 'mail-doctor' ); ?>
									</h4>
									<p style="margin: 0; font-size: 13px; line-height: 1.5; color: var(--mail-doctor-color-muted);">
										<?php esc_html_e( 'This option uses your server\'s default mail capability (PHP mail()). It is not recommended for production sites as emails are highly likely to be flagged as spam or rejected by recipient servers.', 'mail-doctor' ); ?>
									</p>
								</div>
							</section>

							<section class="mail-doctor-card mail-doctor-settings-panel mail-doctor-settings-panel--transport">
								<header class="mail-doctor-settings-panel__header">
									<span
										class="mail-doctor-settings-panel__eyebrow"><?php esc_html_e( 'Credentials', 'mail-doctor' ); ?></span>
									<h3 class="mail-doctor-settings-panel__title">
										<?php esc_html_e( 'Connection details', 'mail-doctor' ); ?>
									</h3>
								</header>
								<p class="mail-doctor-settings-panel__hint" id="mail-doctor-connection-hint" style="font-size: 14px; line-height: 1.5; padding: 10px 14px; background: #f8f9fa; border-left: 4px solid var(--mail-doctor-color-primary); border-radius: 4px; display: flex; align-items: center; gap: 8px;">
									<span class="dashicons dashicons-info-outline" style="color: var(--mail-doctor-color-primary);"></span>
									<span>
										<?php
										printf(
											wp_kses(
												/* translators: 1: Mailer name, 2: Mailer service link, 3: Documentation link */
												__( 'You have selected <strong>%1$s</strong>. <span class="mail-doctor-connection-links" style="margin-left: 8px;">%2$s %3$s</span>', 'mail-doctor' ),
												array(
													'strong' => array(
														'id' => array(),
													),
													'span' => array(
														'class' => array(),
														'style' => array(),
													),
													'a' => array(
														'href' => array(),
														'target' => array(),
														'rel' => array(),
														'class' => array(),
														'id' => array(),
														'style' => array(),
													),
												)
											),
											'<strong id="mail-doctor-selected-mailer-name">' . esc_html( $mail_doctor_providers[ $mail_doctor_settings['provider'] ]['label'] ?? '' ) . '</strong>',
											'<a href="#" target="_blank" rel="noopener noreferrer" id="mail-doctor-selected-mailer-link" style="color: var(--mail-doctor-color-primary); text-decoration: none; font-weight: 500;">' . esc_html__( 'Mailer Service', 'mail-doctor' ) . '</a>',
											'<a href="#" target="_blank" rel="noopener noreferrer" id="mail-doctor-selected-mailer-doc" style="color: var(--mail-doctor-color-primary); text-decoration: none; font-weight: 500; margin-left: 12px;">' . esc_html__( 'Documentation', 'mail-doctor' ) . '</a>'
										);
										?>
									</span>
								</p>
								<table class="form-table mail-doctor-settings-table mail-doctor-settings-table--transport">
									<tbody>
										<tr class="mail-doctor-provider-row mail-doctor-provider-smtp-details">
											<th scope="row"><label
													for="mail-doctor-host"><?php esc_html_e( 'SMTP host', 'mail-doctor' ); ?></label>
											</th>
											<td>
												<input type="text" id="mail-doctor-host" name="mail_doctor_settings[host]"
													value="<?php echo esc_attr( $mail_doctor_settings['host'] ); ?>"
													class="regular-text" placeholder="<?php esc_attr_e( 'e.g. smtp.mailgun.org or smtp.sendgrid.net', 'mail-doctor' ); ?>" />
												<p class="description">
													<?php esc_html_e( 'The server address of your SMTP server host.', 'mail-doctor' ); ?>
												</p>
											</td>
										</tr>
										<tr class="mail-doctor-provider-row mail-doctor-provider-smtp-details">
											<th scope="row"><label
													for="mail-doctor-port"><?php esc_html_e( 'Port', 'mail-doctor' ); ?></label>
											</th>
											<td>
												<input type="number" id="mail-doctor-port" name="mail_doctor_settings[port]"
													value="<?php echo esc_attr( $mail_doctor_settings['port'] ); ?>" class="small-text"
													min="1" placeholder="587" />
												<p class="description">
													<?php esc_html_e( 'SMTP port. Usually 587 (TLS/STARTTLS), 465 (SSL), or 25.', 'mail-doctor' ); ?>
												</p>
											</td>
										</tr>
										<tr class="mail-doctor-provider-row mail-doctor-provider-smtp-details">
											<th scope="row"><label
													for="mail-doctor-encryption"><?php esc_html_e( 'Encryption', 'mail-doctor' ); ?></label>
											</th>
											<td>
												<select id="mail-doctor-encryption" name="mail_doctor_settings[encryption]">
													<option value="none" <?php selected( $mail_doctor_settings['encryption'], 'none' ); ?>><?php esc_html_e( 'None', 'mail-doctor' ); ?></option>
													<option value="tls" <?php selected( $mail_doctor_settings['encryption'], 'tls' ); ?>>
														<?php esc_html_e( 'TLS', 'mail-doctor' ); ?>
													</option>
													<option value="ssl" <?php selected( $mail_doctor_settings['encryption'], 'ssl' ); ?>>
														<?php esc_html_e( 'SSL', 'mail-doctor' ); ?>
													</option>
												</select>
											</td>
										</tr>
										<tr class="mail-doctor-provider-row mail-doctor-provider-smtp-details">
											<th scope="row"><label
													for="mail-doctor-auth"><?php esc_html_e( 'Authentication', 'mail-doctor' ); ?></label>
											</th>
											<td>
												<select id="mail-doctor-auth" name="mail_doctor_settings[auth]">
													<option value="login" <?php selected( $mail_doctor_settings['auth'], 'login' ); ?>>
														<?php esc_html_e( 'Username / password', 'mail-doctor' ); ?>
													</option>
												</select>
											</td>
										</tr>
										<tr class="mail-doctor-provider-row mail-doctor-provider-smtp-credentials">
											<th scope="row"><label id="mail-doctor-username-label"
													for="mail-doctor-username"><?php esc_html_e( 'Username', 'mail-doctor' ); ?></label>
											</th>
											<td>
												<input type="text" id="mail-doctor-username" name="mail_doctor_settings[username]"
													value="<?php echo esc_attr( $mail_doctor_settings['username'] ); ?>"
													class="regular-text" autocomplete="off" placeholder="<?php esc_attr_e( 'e.g. api_key or postmaster@yourdomain.com', 'mail-doctor' ); ?>" />
												<p id="mail-doctor-username-desc" class="description">
													<?php esc_html_e( 'The SMTP login username credential.', 'mail-doctor' ); ?>
												</p>
											</td>
										</tr>
										<tr class="mail-doctor-provider-row mail-doctor-provider-smtp-credentials">
											<th scope="row"><label id="mail-doctor-password-label"
													for="mail-doctor-password"><?php esc_html_e( 'Password / API key', 'mail-doctor' ); ?></label>
											</th>
											<td>
												<div class="mail-doctor-password-field">
													<?php
													$mail_doctor_password_placeholder = isset( $mail_doctor_recipe_placeholders['password'] ) && '' !== $mail_doctor_recipe_placeholders['password']
														? $mail_doctor_recipe_placeholders['password']
														: esc_html__( 'Enter to update', 'mail-doctor' );
													?>
													<input type="password" id="mail-doctor-password" name="mail_doctor_settings[password]"
														value="<?php echo '' !== $mail_doctor_settings['password'] ? esc_attr( str_repeat( '•', 10 ) ) : ''; ?>"
														class="regular-text" autocomplete="new-password"
														placeholder="<?php echo esc_attr( $mail_doctor_password_placeholder ); ?>" />
													<button type="button" class="button-link mail-doctor-toggle-password"
														data-target="mail-doctor-password"
														data-label-show="<?php echo esc_attr__( 'Show password', 'mail-doctor' ); ?>"
														data-label-hide="<?php echo esc_attr__( 'Hide password', 'mail-doctor' ); ?>"
														aria-pressed="false">
														<span class="dashicons dashicons-visibility"
															aria-hidden="true"></span>
														<span class="dashicons dashicons-hidden" aria-hidden="true"></span>
														<span
															class="screen-reader-text"><?php esc_html_e( 'Show password', 'mail-doctor' ); ?></span>
													</button>
												</div>
												<p id="mail-doctor-password-desc" class="description">
													<?php esc_html_e( 'The SMTP account password or API secret key credential.', 'mail-doctor' ); ?>
												</p>
											</td>
										</tr>
										<tr class="mail-doctor-provider-row mail-doctor-provider-mailgun">
											<th scope="row"><label
													for="mail-doctor-mailgun-key"><?php esc_html_e( 'Mailgun API key', 'mail-doctor' ); ?></label>
											</th>
											<td><input type="text" id="mail-doctor-mailgun-key" name="mail_doctor_settings[mailgun_api_key]"
													value="<?php echo esc_attr( $mail_doctor_settings['mailgun_api_key'] ); ?>"
													class="regular-text" autocomplete="off" />
												<p class="description">
													<?php esc_html_e( 'Use a Private API key from the Mailgun dashboard.', 'mail-doctor' ); ?>
												</p>
											</td>
										</tr>
										<tr class="mail-doctor-provider-row mail-doctor-provider-mailgun">
											<th scope="row"><label
													for="mail-doctor-mailgun-domain"><?php esc_html_e( 'Mailgun domain', 'mail-doctor' ); ?></label>
											</th>
											<td><input type="text" id="mail-doctor-mailgun-domain"
													name="mail_doctor_settings[mailgun_api_domain]"
													value="<?php echo esc_attr( $mail_doctor_settings['mailgun_api_domain'] ); ?>"
													class="regular-text" placeholder="mg.example.com" />
												<p class="description">
													<?php esc_html_e( 'Enter the Mailgun sending domain used for REST API calls.', 'mail-doctor' ); ?>
												</p>
											</td>
										</tr>
										<tr class="mail-doctor-provider-row mail-doctor-provider-mailgun">
											<th scope="row"><label
													for="mail-doctor-mailgun-endpoint"><?php esc_html_e( 'Mailgun API endpoint', 'mail-doctor' ); ?></label>
											</th>
											<td><input type="text" id="mail-doctor-mailgun-endpoint"
													name="mail_doctor_settings[mailgun_api_endpoint]"
													value="<?php echo esc_attr( $mail_doctor_settings['mailgun_api_endpoint'] ); ?>"
													class="regular-text" placeholder="api.mailgun.net" />
												<p class="description">
													<?php esc_html_e( 'Override when using a dedicated Mailgun EU domain (api.eu.mailgun.net).', 'mail-doctor' ); ?>
												</p>
											</td>
										</tr>
										<tr class="mail-doctor-provider-row mail-doctor-provider-sendgrid">
											<th scope="row"><label
													for="mail-doctor-sendgrid-key"><?php esc_html_e( 'SendGrid API key', 'mail-doctor' ); ?></label>
											</th>
											<td><input type="text" id="mail-doctor-sendgrid-key" name="mail_doctor_settings[sendgrid_api_key]"
													value="<?php echo esc_attr( $mail_doctor_settings['sendgrid_api_key'] ); ?>"
													class="regular-text" autocomplete="off" />
												<p class="description">
													<?php esc_html_e( 'Generate a restricted Mail Send API key inside the SendGrid dashboard.', 'mail-doctor' ); ?>
												</p>
											</td>
										</tr>
										<tr class="mail-doctor-provider-row mail-doctor-provider-postmark">
											<th scope="row"><label
													for="mail-doctor-postmark-token"><?php esc_html_e( 'Postmark server token', 'mail-doctor' ); ?></label>
											</th>
											<td><input type="text" id="mail-doctor-postmark-token"
													name="mail_doctor_settings[postmark_api_token]"
													value="<?php echo esc_attr( $mail_doctor_settings['postmark_api_token'] ); ?>"
													class="regular-text" autocomplete="off" />
												<p class="description">
													<?php esc_html_e( 'Use the Server API token for the stream that should send WooCommerce email.', 'mail-doctor' ); ?>
												</p>
											</td>
										</tr>
										<tr class="mail-doctor-provider-row mail-doctor-provider-sparkpost">
											<th scope="row"><label
													for="mail-doctor-sparkpost-key"><?php esc_html_e( 'SparkPost API key', 'mail-doctor' ); ?></label>
											</th>
											<td><input type="text" id="mail-doctor-sparkpost-key"
													name="mail_doctor_settings[sparkpost_api_key]"
													value="<?php echo esc_attr( $mail_doctor_settings['sparkpost_api_key'] ); ?>"
													class="regular-text" autocomplete="off" />
												<p class="description">
													<?php esc_html_e( 'Create an API key with the Transmissions permission in SparkPost.', 'mail-doctor' ); ?>
												</p>
											</td>
										</tr>
										<tr class="mail-doctor-provider-row mail-doctor-provider-sparkpost">
											<th scope="row"><label
													for="mail-doctor-sparkpost-endpoint"><?php esc_html_e( 'SparkPost API base', 'mail-doctor' ); ?></label>
											</th>
											<td><input type="text" id="mail-doctor-sparkpost-endpoint"
													name="mail_doctor_settings[sparkpost_api_endpoint]"
													value="<?php echo esc_attr( $mail_doctor_settings['sparkpost_api_endpoint'] ); ?>"
													class="regular-text" placeholder="https://api.sparkpost.com" />
												<p class="description">
													<?php esc_html_e( 'Override when using SparkPost EU (https://api.eu.sparkpost.com).', 'mail-doctor' ); ?>
												</p>
											</td>
										</tr>
										<tr class="mail-doctor-provider-row mail-doctor-provider-ses">
											<th scope="row"><label
													for="mail-doctor-ses-key"><?php esc_html_e( 'SES access key ID', 'mail-doctor' ); ?></label>
											</th>
											<td><input type="text" id="mail-doctor-ses-key" name="mail_doctor_settings[ses_api_key]"
													value="<?php echo esc_attr( $mail_doctor_settings['ses_api_key'] ); ?>"
													class="regular-text" autocomplete="off" />
												<p class="description">
													<?php esc_html_e( 'Use an IAM user or SMTP credential that is authorised to send through Amazon SES.', 'mail-doctor' ); ?>
												</p>
											</td>
										</tr>
										<tr class="mail-doctor-provider-row mail-doctor-provider-ses">
											<th scope="row"><label
													for="mail-doctor-ses-secret"><?php esc_html_e( 'SES secret access key', 'mail-doctor' ); ?></label>
											</th>
											<td>
												<div class="mail-doctor-password-field">
													<input type="password" id="mail-doctor-ses-secret"
														name="mail_doctor_settings[ses_api_secret]"
														value="<?php echo '' !== $mail_doctor_settings['ses_api_secret'] ? esc_attr( str_repeat( '•', 10 ) ) : ''; ?>"
														class="regular-text" autocomplete="new-password"
														placeholder="<?php esc_attr_e( 'Enter to update', 'mail-doctor' ); ?>" />
													<button type="button" class="button-link mail-doctor-toggle-password"
														data-target="mail-doctor-ses-secret"
														data-label-show="<?php echo esc_attr__( 'Show secret', 'mail-doctor' ); ?>"
														data-label-hide="<?php echo esc_attr__( 'Hide secret', 'mail-doctor' ); ?>"
														aria-pressed="false">
														<span class="dashicons dashicons-visibility"
															aria-hidden="true"></span>
														<span class="dashicons dashicons-hidden" aria-hidden="true"></span>
														<span
															class="screen-reader-text"><?php esc_html_e( 'Show secret', 'mail-doctor' ); ?></span>
													</button>
												</div>
											</td>
										</tr>
										<tr class="mail-doctor-provider-row mail-doctor-provider-ses">
											<th scope="row"><label
													for="mail-doctor-ses-token"><?php esc_html_e( 'SES session token', 'mail-doctor' ); ?></label>
											</th>
											<td><input type="text" id="mail-doctor-ses-token"
													name="mail_doctor_settings[ses_api_session_token]"
													value="<?php echo esc_attr( $mail_doctor_settings['ses_api_session_token'] ); ?>"
													class="regular-text" autocomplete="off" />
												<p class="description">
													<?php esc_html_e( 'Optional: provide when using temporary STS credentials.', 'mail-doctor' ); ?>
												</p>
											</td>
										</tr>
										<tr class="mail-doctor-provider-row mail-doctor-provider-ses">
											<th scope="row"><label
													for="mail-doctor-ses-region"><?php esc_html_e( 'SES region', 'mail-doctor' ); ?></label>
											</th>
											<td>
												<select id="mail-doctor-ses-region" name="mail_doctor_settings[ses_api_region]">
													<?php foreach ( $mail_doctor_ses_regions as $mail_doctor_region_code => $mail_doctor_region_label ) : ?>
														<option value="<?php echo esc_attr( $mail_doctor_region_code ); ?>" <?php selected( $mail_doctor_settings['ses_api_region'], $mail_doctor_region_code ); ?>>
															<?php echo esc_html( $mail_doctor_region_label ); ?>
														</option>
													<?php endforeach; ?>
												</select>
												<p class="description">
													<?php esc_html_e( 'Select the Amazon SES region that matches your verified identities.', 'mail-doctor' ); ?>
												</p>
											</td>
										</tr>
										<tr class="mail-doctor-provider-row mail-doctor-provider-generic">
											<th scope="row"><label
													for="mail-doctor-generic-endpoint"><?php esc_html_e( 'Endpoint URL', 'mail-doctor' ); ?></label>
											</th>
											<td><input type="url" id="mail-doctor-generic-endpoint"
													name="mail_doctor_settings[generic_api_endpoint]"
													value="<?php echo esc_attr( $mail_doctor_settings['generic_api_endpoint'] ); ?>"
													class="regular-text"
													placeholder="https://example.com/maildoctor-webhook" />
												<p class="description">
													<?php esc_html_e( 'Mail Doctor posts JSON payloads to this HTTPS endpoint.', 'mail-doctor' ); ?>
												</p>
											</td>
										</tr>
										<tr class="mail-doctor-provider-row mail-doctor-provider-generic">
											<th scope="row"><label
													for="mail-doctor-generic-method"><?php esc_html_e( 'HTTP method', 'mail-doctor' ); ?></label>
											</th>
											<td>
												<select id="mail-doctor-generic-method" name="mail_doctor_settings[generic_api_method]">
													<option value="POST" <?php selected( strtoupper( $mail_doctor_settings['generic_api_method'] ), 'POST' ); ?>>
														<?php esc_html_e( 'POST', 'mail-doctor' ); ?>
													</option>
													<option value="PUT" <?php selected( strtoupper( $mail_doctor_settings['generic_api_method'] ), 'PUT' ); ?>>
														<?php esc_html_e( 'PUT', 'mail-doctor' ); ?>
													</option>
												</select>
											</td>
										</tr>
										<tr class="mail-doctor-provider-row mail-doctor-provider-generic">
											<th scope="row"><label
													for="mail-doctor-generic-token"><?php esc_html_e( 'Bearer token', 'mail-doctor' ); ?></label>
											</th>
											<td><input type="text" id="mail-doctor-generic-token"
													name="mail_doctor_settings[generic_api_auth_token]"
													value="<?php echo esc_attr( $mail_doctor_settings['generic_api_auth_token'] ); ?>"
													class="regular-text" autocomplete="off" />
												<p class="description">
													<?php esc_html_e( 'Optional: included as an Authorization header when calling your endpoint.', 'mail-doctor' ); ?>
												</p>
											</td>
										</tr>
										<tr class="mail-doctor-provider-row mail-doctor-provider-all">
											<th scope="row"><label for="mail-doctor-profile-save-toggle"><?php esc_html_e( 'Save as Profile', 'mail-doctor' ); ?></label></th>
											<td>
												<label>
													<input type="checkbox" id="mail-doctor-profile-save-toggle" />
													<?php esc_html_e( 'Save this connection configuration as a reusable profile.', 'mail-doctor' ); ?>
												</label>
												
												<div id="mail-doctor-profile-save-name-container" style="display: none; margin-top: 10px;">
													<input type="text" id="mail-doctor-profile-save-name" name="mail_doctor_profile_save[name]" value="" class="regular-text" placeholder="<?php esc_attr_e( 'e.g. SendGrid Marketing', 'mail-doctor' ); ?>" />
													<p class="description">
														<?php esc_html_e( 'Your profile will be saved when you save the current settings.', 'mail-doctor' ); ?>
													</p>
												</div>

												<script>
													document.addEventListener('DOMContentLoaded', function() {
														var toggle = document.getElementById('mail-doctor-profile-save-toggle');
														var container = document.getElementById('mail-doctor-profile-save-name-container');
														var nameInput = document.getElementById('mail-doctor-profile-save-name');
														
														if (toggle && container) {
															toggle.addEventListener('change', function() {
																container.style.display = this.checked ? 'block' : 'none';
																if (!this.checked && nameInput) {
																	nameInput.value = '';
																}
															});
														}
													});
												</script>
											</td>
										</tr>
									</tbody>
								</table>
							</section>

						<div class="mail-doctor-smtp-actions">
							<section
								class="mail-doctor-card mail-doctor-settings-panel mail-doctor-settings-panel--actions mail-doctor-smtp-actions__panel">
								<header class="mail-doctor-settings-panel__header">
									<span
										class="mail-doctor-settings-panel__eyebrow"><?php esc_html_e( 'Diagnostics', 'mail-doctor' ); ?></span>
									<h3 class="mail-doctor-settings-panel__title">
										<?php esc_html_e( 'Testing tools', 'mail-doctor' ); ?>
									</h3>
								</header>
								<div class="mail-doctor-settings-actions">
									<div class="mail-doctor-settings-actions__email">
										<label for="mail-doctor-test-email"><?php esc_html_e( 'Recipient email', 'mail-doctor' ); ?></label>
										<input type="email" id="mail-doctor-test-email" name="mail_doctor_test_email"
											value="<?php echo esc_attr( wp_get_current_user()->user_email ); ?>"
											class="regular-text" placeholder="<?php esc_attr_e( 'Enter email address', 'mail-doctor' ); ?>" />
									</div>
									<div class="mail-doctor-settings-actions__buttons">
										<button type="button"
											class="button button-secondary mail-doctor-test-email-button"><?php esc_html_e( 'Send test email', 'mail-doctor' ); ?></button>
										<button type="button"
											class="button mail-doctor-test-connection-button"><?php esc_html_e( 'Test connection', 'mail-doctor' ); ?></button>
									</div>
									<div class="mail-doctor-test-results mail-doctor-test-results--inline" role="status"
										aria-live="polite" aria-busy="false"></div>
								</div>

						</div>

					<?php elseif ( 'profiles' === $mail_doctor_section ) : ?>
						<header class="mail-doctor-settings-header">
							<span
								class="mail-doctor-settings-header__eyebrow"><?php esc_html_e( 'Flexibility', 'mail-doctor' ); ?></span>
							<h2 class="mail-doctor-settings-header__title">
								<?php esc_html_e( 'SMTP Profiles', 'mail-doctor' ); ?>
							</h2>
							<p class="mail-doctor-settings-header__description">
								<?php esc_html_e( 'Create additional SMTP or API transports and switch between them without overwriting your primary configuration.', 'mail-doctor' ); ?>
							</p>
						</header>

						<div class="mail-doctor-section-summary" role="list">
							<div class="mail-doctor-section-summary__item" role="listitem">
								<span
									class="mail-doctor-section-summary__eyebrow"><?php esc_html_e( 'Stored profiles', 'mail-doctor' ); ?></span>
								<strong
									class="mail-doctor-section-summary__value"><?php echo esc_html( $mail_doctor_section_meta['profiles']['label'] ?? '' ); ?></strong>
							</div>
							<div class="mail-doctor-section-summary__item" role="listitem">
								<span
									class="mail-doctor-section-summary__eyebrow"><?php esc_html_e( 'Active profile', 'mail-doctor' ); ?></span>
								<strong
									class="mail-doctor-section-summary__value"><?php echo esc_html( $mail_doctor_active_profile_label ); ?></strong>
							</div>
							<div class="mail-doctor-section-summary__item" role="listitem">
								<span
									class="mail-doctor-section-summary__eyebrow"><?php esc_html_e( 'Routing targets', 'mail-doctor' ); ?></span>
								<strong class="mail-doctor-section-summary__value">
									<?php
									printf(
										/* translators: 1: role count, 2: site count, 3: email template count */
										esc_html__( '%1$d roles · %2$d sites · %3$d emails', 'mail-doctor' ),
										(int) $mail_doctor_profile_roles_total,
										(int) $mail_doctor_profile_sites_total,
										(int) $mail_doctor_profile_emails_total
									);
									?>
								</strong>
							</div>
						</div>

						<div class="mail-doctor-settings-stack">
							<section class="mail-doctor-card mail-doctor-settings-panel mail-doctor-settings-panel--profiles-list">
								<header class="mail-doctor-settings-panel__header">
									<span
										class="mail-doctor-settings-panel__eyebrow"><?php esc_html_e( 'Library', 'mail-doctor' ); ?></span>
									<h3 class="mail-doctor-settings-panel__title">
										<?php esc_html_e( 'Stored profiles', 'mail-doctor' ); ?>
									</h3>
								</header>
								<div class="mail-doctor-settings-panel__body mail-doctor-profile-list">
									<?php if ( empty( $mail_doctor_profile_rows ) ) : ?>
										<div class="mail-doctor-empty mail-doctor-empty--panel">
											<span class="dashicons dashicons-id-alt" aria-hidden="true"></span>
											<div class="mail-doctor-empty__content">
												<h4><?php esc_html_e( 'No profiles saved yet', 'mail-doctor' ); ?></h4>
												<p><?php esc_html_e( 'Capture your current SMTP credentials as a profile to swap transports without retyping secrets.', 'mail-doctor' ); ?>
												</p>
												<p class="mail-doctor-empty__hint">
													<?php esc_html_e( 'Return to SMTP & Identity to snapshot the active configuration.', 'mail-doctor' ); ?>
												</p>
											</div>
										</div>
									<?php else : ?>
										<div class="mail-doctor-tab-layout mail-doctor-profile-layout" data-mail-doctor-tab-menu>
											<nav class="mail-doctor-tab-menu mail-doctor-profile-menu"
												aria-label="<?php esc_attr_e( 'Stored profiles', 'mail-doctor' ); ?>"
												role="tablist">
												<ul>
													<?php foreach ( $mail_doctor_profile_rows as $mail_doctor_profile_index => $mail_doctor_profile ) : ?>
														<?php
														$mail_doctor_rules      = isset( $mail_doctor_profile['scope']['rules'] ) && is_array( $mail_doctor_profile['scope']['rules'] ) ? $mail_doctor_profile['scope']['rules'] : array();
														$mail_doctor_roles      = isset( $mail_doctor_rules['roles'] ) && is_array( $mail_doctor_rules['roles'] ) ? $mail_doctor_rules['roles'] : array();
														$mail_doctor_sites      = isset( $mail_doctor_rules['sites'] ) && is_array( $mail_doctor_rules['sites'] ) ? $mail_doctor_rules['sites'] : array();
														$mail_doctor_emails     = isset( $mail_doctor_rules['emails'] ) && is_array( $mail_doctor_rules['emails'] ) ? $mail_doctor_rules['emails'] : array();
														$mail_doctor_priority   = isset( $mail_doctor_rules['priority'] ) ? (int) $mail_doctor_rules['priority'] : 0;
														$mail_doctor_panel_id   = 'mail-doctor-profile-panel-' . (int) $mail_doctor_profile['id'] . '-' . (int) $mail_doctor_profile_index;
														$mail_doctor_host_label = trim( (string) ( $mail_doctor_profile['host'] ?? '' ) );
														if ( ! empty( $mail_doctor_profile['port'] ) ) {
															$mail_doctor_host_label = $mail_doctor_host_label ? $mail_doctor_host_label . ':' . (string) $mail_doctor_profile['port'] : (string) $mail_doctor_profile['port'];
														}
														if ( '' === $mail_doctor_host_label ) {
															$mail_doctor_host_label = esc_html__( 'Host not configured', 'mail-doctor' );
														}
														$mail_doctor_assignment_count = count( $mail_doctor_roles ) + count( $mail_doctor_sites ) + count( $mail_doctor_emails );
														?>
														<li>
															<button type="button"
																class="mail-doctor-tab-menu__link mail-doctor-profile-menu__link<?php echo esc_attr( 0 === $mail_doctor_profile_index ? ' is-active' : '' ); ?>"
																id="<?php echo esc_attr( $mail_doctor_panel_id ); ?>-tab" role="tab"
																data-mail-doctor-tab-target="<?php echo esc_attr( $mail_doctor_panel_id ); ?>"
																aria-controls="<?php echo esc_attr( $mail_doctor_panel_id ); ?>"
																aria-selected="<?php echo esc_attr( 0 === $mail_doctor_profile_index ? 'true' : 'false' ); ?>"
																tabindex="<?php echo esc_attr( 0 === $mail_doctor_profile_index ? '0' : '-1' ); ?>">
																<span class="dashicons dashicons-id-alt" aria-hidden="true"></span>
																<span
																	class="mail-doctor-tab-menu__label mail-doctor-profile-menu__label"><?php echo esc_html( $mail_doctor_profile['name'] ); ?></span>
																<span
																	class="mail-doctor-tab-menu__sublabel mail-doctor-profile-menu__sublabel"><?php echo esc_html( $mail_doctor_host_label ); ?></span>
																<?php if ( $mail_doctor_priority ) : ?>
																	<span class="mail-doctor-tab-menu__badge mail-doctor-tab-menu__badge--priority">
																	<?php
																	/* translators: %d: priority number */
																	printf( esc_html__( 'Priority %d', 'mail-doctor' ), (int) $mail_doctor_priority );
																	?>
																	</span>
																<?php endif; ?>
																<?php if ( $mail_doctor_assignment_count ) : ?>
																	<span class="mail-doctor-tab-menu__badge">
																	<?php
																	/* translators: %d: count of targets */
																	printf( esc_html__( '%d targets', 'mail-doctor' ), (int) $mail_doctor_assignment_count );
																	?>
																	</span>
																<?php endif; ?>
															</button>
														</li>
													<?php endforeach; ?>
												</ul>
											</nav>

											<div class="mail-doctor-tab-panels mail-doctor-profile-panels">
												<?php foreach ( $mail_doctor_profile_rows as $mail_doctor_profile_index => $mail_doctor_profile ) : ?>
													<?php
													$mail_doctor_row_key            = 'id_' . (int) $mail_doctor_profile['id'];
													$mail_doctor_secret_ref         = isset( $mail_doctor_profile['secret_ref'] ) ? (string) $mail_doctor_profile['secret_ref'] : '';
													$mail_doctor_rules              = isset( $mail_doctor_profile['scope']['rules'] ) && is_array( $mail_doctor_profile['scope']['rules'] ) ? $mail_doctor_profile['scope']['rules'] : array();
													$mail_doctor_roles              = isset( $mail_doctor_rules['roles'] ) && is_array( $mail_doctor_rules['roles'] ) ? $mail_doctor_rules['roles'] : array();
													$mail_doctor_sites              = isset( $mail_doctor_rules['sites'] ) && is_array( $mail_doctor_rules['sites'] ) ? $mail_doctor_rules['sites'] : array();
													$mail_doctor_emails             = isset( $mail_doctor_rules['emails'] ) && is_array( $mail_doctor_rules['emails'] ) ? $mail_doctor_rules['emails'] : array();
													$mail_doctor_priority           = isset( $mail_doctor_rules['priority'] ) ? (int) $mail_doctor_rules['priority'] : 0;
													$mail_doctor_auth_method        = isset( $mail_doctor_profile['auth'] ) ? $mail_doctor_profile['auth'] : 'password';
													$mail_doctor_oauth_meta         = isset( $mail_doctor_profile['scope']['oauth'] ) && is_array( $mail_doctor_profile['scope']['oauth'] ) ? $mail_doctor_profile['scope']['oauth'] : array();
													$mail_doctor_created_at_display = '';
													if ( ! empty( $mail_doctor_profile['created_at'] ) ) {
														$mail_doctor_created_at_display = wp_date( get_option( 'date_format' ), strtotime( $mail_doctor_profile['created_at'] ) );
													}
													$mail_doctor_panel_id = 'mail-doctor-profile-panel-' . (int) $mail_doctor_profile['id'] . '-' . (int) $mail_doctor_profile_index;
													?>
													<?php
													$mail_doctor_panel_host_label = trim( (string) ( $mail_doctor_profile['host'] ?? '' ) );
													if ( ! empty( $mail_doctor_profile['port'] ) ) {
														$mail_doctor_panel_host_label = $mail_doctor_panel_host_label ? $mail_doctor_panel_host_label . ':' . (string) $mail_doctor_profile['port'] : (string) $mail_doctor_profile['port'];
													}
													if ( '' === $mail_doctor_panel_host_label ) {
														$mail_doctor_panel_host_label = esc_html__( 'Host not configured', 'mail-doctor' );
													}
													$mail_doctor_panel_is_active = 0 === $mail_doctor_profile_index;
													?>
													<section id="<?php echo esc_attr( $mail_doctor_panel_id ); ?>"
														class="mail-doctor-tab-pane mail-doctor-profile-pane<?php echo esc_attr( $mail_doctor_panel_is_active ? ' is-active' : '' ); ?>"
														role="tabpanel" aria-labelledby="<?php echo esc_attr( $mail_doctor_panel_id ); ?>-tab"
														aria-hidden="<?php echo esc_attr( $mail_doctor_panel_is_active ? 'false' : 'true' ); ?>"
														data-mail-doctor-tab-panel="<?php echo esc_attr( $mail_doctor_panel_id ); ?>"<?php echo $mail_doctor_panel_is_active ? '' : ' hidden'; ?>>
														<header class="mail-doctor-tab-pane__header mail-doctor-profile-pane__header">
															<div class="mail-doctor-tab-pane__headline mail-doctor-profile-pane__headline">
																<strong
																	class="mail-doctor-tab-pane__title mail-doctor-profile-pane__title"><?php echo esc_html( $mail_doctor_profile['name'] ); ?></strong>
																<span
																	class="mail-doctor-chip mail-doctor-chip--muted"><?php echo esc_html( $mail_doctor_panel_host_label ); ?></span>
																<?php if ( $mail_doctor_priority ) : ?>
																	<span class="mail-doctor-chip mail-doctor-chip--priority">
																	<?php
																	/* translators: %d: priority number */
																	printf( esc_html__( 'Priority %d', 'mail-doctor' ), (int) $mail_doctor_priority );
																	?>
																	</span>
																<?php endif; ?>
																<?php if ( 'oauth2' === $mail_doctor_auth_method ) : ?>
																	<span
																		class="mail-doctor-chip mail-doctor-chip--accent"><?php esc_html_e( 'OAuth2', 'mail-doctor' ); ?></span>
																<?php endif; ?>
															</div>
															<div class="mail-doctor-tab-pane__meta mail-doctor-profile-pane__meta">
																<?php if ( ! empty( $mail_doctor_roles ) ) : ?>
																	<span class="mail-doctor-chip mail-doctor-chip--compact">
																	<?php
																	/* translators: %d: role count */
																	printf( esc_html__( '%d roles', 'mail-doctor' ), count( $mail_doctor_roles ) );
																	?>
																	</span>
																<?php endif; ?>
																<?php if ( ! empty( $mail_doctor_sites ) ) : ?>
																	<span class="mail-doctor-chip mail-doctor-chip--compact">
																	<?php
																	/* translators: %d: site count */
																	printf( esc_html__( '%d sites', 'mail-doctor' ), count( $mail_doctor_sites ) );
																	?>
																	</span>
																<?php endif; ?>
																<?php if ( ! empty( $mail_doctor_emails ) ) : ?>
																	<span class="mail-doctor-chip mail-doctor-chip--compact">
																	<?php
																	/* translators: %d: email count */
																	printf( esc_html__( '%d emails', 'mail-doctor' ), count( $mail_doctor_emails ) );
																	?>
																	</span>
																<?php endif; ?>
																<?php if ( $mail_doctor_created_at_display ) : ?>
																	<span
																		class="mail-doctor-chip mail-doctor-chip--muted"><?php echo esc_html( $mail_doctor_created_at_display ); ?></span>
																<?php endif; ?>
															</div>
														</header>

														<div class="mail-doctor-tab-pane__body mail-doctor-profile-pane__body">
															<input type="hidden"
																name="mail_doctor_profiles[<?php echo esc_attr( $mail_doctor_row_key ); ?>][id]"
																value="<?php echo esc_attr( (string) $mail_doctor_profile['id'] ); ?>" />
															<input type="hidden"
																name="mail_doctor_profiles[<?php echo esc_attr( $mail_doctor_row_key ); ?>][secret_ref]"
																value="<?php echo esc_attr( $mail_doctor_secret_ref ); ?>" />
															<input type="hidden"
																name="mail_doctor_profiles[<?php echo esc_attr( $mail_doctor_row_key ); ?>][slug]"
																value="<?php echo esc_attr( $mail_doctor_profile['scope']['slug'] ?? '' ); ?>" />
															<input type="hidden"
																name="mail_doctor_profiles[<?php echo esc_attr( $mail_doctor_row_key ); ?>][priority]"
																value="<?php echo esc_attr( (string) $mail_doctor_priority ); ?>" />
															<input type="hidden"
																name="mail_doctor_profiles[<?php echo esc_attr( $mail_doctor_row_key ); ?>][auth]"
																value="<?php echo esc_attr( $mail_doctor_auth_method ); ?>" />
															<?php foreach ( $mail_doctor_roles as $mail_doctor_role_key ) : ?>
																<input type="hidden"
																	name="mail_doctor_profiles[<?php echo esc_attr( $mail_doctor_row_key ); ?>][roles][]"
																	value="<?php echo esc_attr( $mail_doctor_role_key ); ?>" />
															<?php endforeach; ?>
															<?php foreach ( $mail_doctor_sites as $mail_doctor_site_id ) : ?>
																<input type="hidden"
																	name="mail_doctor_profiles[<?php echo esc_attr( $mail_doctor_row_key ); ?>][sites][]"
																	value="<?php echo esc_attr( (string) $mail_doctor_site_id ); ?>" />
															<?php endforeach; ?>
															<?php foreach ( $mail_doctor_emails as $mail_doctor_email_key ) : ?>
																<input type="hidden"
																	name="mail_doctor_profiles[<?php echo esc_attr( $mail_doctor_row_key ); ?>][emails][]"
																	value="<?php echo esc_attr( $mail_doctor_email_key ); ?>" />
															<?php endforeach; ?>
															<?php if ( ! empty( $mail_doctor_oauth_meta ) ) : ?>
																<?php foreach ( array( 'provider', 'client_id', 'scope', 'tenant' ) as $mail_doctor_oauth_field ) : ?>
																	<?php if ( isset( $mail_doctor_oauth_meta[ $mail_doctor_oauth_field ] ) ) : ?>
																		<input type="hidden"
																			name="mail_doctor_profiles[<?php echo esc_attr( $mail_doctor_row_key ); ?>][oauth][<?php echo esc_attr( $mail_doctor_oauth_field ); ?>]"
																			value="<?php echo esc_attr( $mail_doctor_oauth_meta[ $mail_doctor_oauth_field ] ); ?>" />
																	<?php endif; ?>
																<?php endforeach; ?>
															<?php endif; ?>

															<table class="form-table mail-doctor-profile-table">
																<tr>
																	<th scope="row"><label
																			for="mail-doctor-profile-name-<?php echo esc_attr( $mail_doctor_profile['id'] ); ?>"><?php esc_html_e( 'Profile name', 'mail-doctor' ); ?></label>
																	</th>
																	<td><input type="text"
																			id="mail-doctor-profile-name-<?php echo esc_attr( $mail_doctor_profile['id'] ); ?>"
																			name="mail_doctor_profiles[<?php echo esc_attr( $mail_doctor_row_key ); ?>][name]"
																			value="<?php echo esc_attr( $mail_doctor_profile['name'] ); ?>"
																			class="regular-text" required /></td>
																</tr>
																<tr>
																	<th scope="row"><label
																			for="mail-doctor-profile-host-<?php echo esc_attr( $mail_doctor_profile['id'] ); ?>"><?php esc_html_e( 'SMTP host', 'mail-doctor' ); ?></label>
																	</th>
																	<td><input type="text"
																			id="mail-doctor-profile-host-<?php echo esc_attr( $mail_doctor_profile['id'] ); ?>"
																			name="mail_doctor_profiles[<?php echo esc_attr( $mail_doctor_row_key ); ?>][host]"
																			value="<?php echo esc_attr( $mail_doctor_profile['host'] ); ?>"
																			class="regular-text" /></td>
																</tr>
																<tr>
																	<th scope="row"><label
																			for="mail-doctor-profile-port-<?php echo esc_attr( $mail_doctor_profile['id'] ); ?>"><?php esc_html_e( 'Port', 'mail-doctor' ); ?></label>
																	</th>
																	<td><input type="number"
																			id="mail-doctor-profile-port-<?php echo esc_attr( $mail_doctor_profile['id'] ); ?>"
																			name="mail_doctor_profiles[<?php echo esc_attr( $mail_doctor_row_key ); ?>][port]"
																			value="<?php echo esc_attr( $mail_doctor_profile['port'] ); ?>"
																			class="small-text" min="1" /></td>
																</tr>
																<tr>
																	<th scope="row"><label
																			for="mail-doctor-profile-secure-<?php echo esc_attr( $mail_doctor_profile['id'] ); ?>"><?php esc_html_e( 'Encryption', 'mail-doctor' ); ?></label>
																	</th>
																	<td>
																		<select
																			id="mail-doctor-profile-secure-<?php echo esc_attr( $mail_doctor_profile['id'] ); ?>"
																			name="mail_doctor_profiles[<?php echo esc_attr( $mail_doctor_row_key ); ?>][secure]">
																			<option value="none" <?php selected( $mail_doctor_profile['secure'], 'none' ); ?>>
																				<?php esc_html_e( 'None', 'mail-doctor' ); ?>
																			</option>
																			<option value="tls" <?php selected( $mail_doctor_profile['secure'], 'tls' ); ?>>
																				<?php esc_html_e( 'TLS', 'mail-doctor' ); ?>
																			</option>
																			<option value="ssl" <?php selected( $mail_doctor_profile['secure'], 'ssl' ); ?>>
																				<?php esc_html_e( 'SSL', 'mail-doctor' ); ?>
																			</option>
																		</select>
																	</td>
																</tr>
																<tr>
																	<th scope="row"><label
																			for="mail-doctor-profile-username-<?php echo esc_attr( $mail_doctor_profile['id'] ); ?>"><?php esc_html_e( 'Username', 'mail-doctor' ); ?></label>
																	</th>
																	<td><input type="text"
																			id="mail-doctor-profile-username-<?php echo esc_attr( $mail_doctor_profile['id'] ); ?>"
																			name="mail_doctor_profiles[<?php echo esc_attr( $mail_doctor_row_key ); ?>][username]"
																			value="<?php echo esc_attr( $mail_doctor_profile['user'] ); ?>"
																			class="regular-text" autocomplete="off" /></td>
																</tr>
																<tr>
																	<th scope="row"><label
																			for="mail-doctor-profile-password-<?php echo esc_attr( $mail_doctor_profile['id'] ); ?>"><?php esc_html_e( 'Password', 'mail-doctor' ); ?></label>
																	</th>
																	<td>
																		<input type="password"
																			id="mail-doctor-profile-password-<?php echo esc_attr( $mail_doctor_profile['id'] ); ?>"
																			name="mail_doctor_profiles[<?php echo esc_attr( $mail_doctor_row_key ); ?>][password]"
																			class="regular-text" autocomplete="new-password" />
																		<p class="description">
																			<?php
																			if ( $mail_doctor_secret_ref ) {
																				esc_html_e( 'Leave blank to keep the stored password. Enter a new value to rotate the secret.', 'mail-doctor' );
																			} else {
																				esc_html_e( 'Stored securely. Provide a password if the SMTP server requires authentication.', 'mail-doctor' );
																			}
																			?>
																		</p>
																	</td>
																</tr>
																<tr>
																	<th scope="row"><label
																			for="mail-doctor-profile-from-email-<?php echo esc_attr( $mail_doctor_profile['id'] ); ?>"><?php esc_html_e( 'From email override', 'mail-doctor' ); ?></label>
																	</th>
																	<td><input type="email"
																			id="mail-doctor-profile-from-email-<?php echo esc_attr( $mail_doctor_profile['id'] ); ?>"
																			name="mail_doctor_profiles[<?php echo esc_attr( $mail_doctor_row_key ); ?>][from_email]"
																			value="<?php echo esc_attr( $mail_doctor_profile['from_email'] ?? '' ); ?>"
																			class="regular-text" /></td>
																</tr>
																<tr>
																	<th scope="row"><label
																			for="mail-doctor-profile-from-name-<?php echo esc_attr( $mail_doctor_profile['id'] ); ?>"><?php esc_html_e( 'From name override', 'mail-doctor' ); ?></label>
																	</th>
																	<td><input type="text"
																			id="mail-doctor-profile-from-name-<?php echo esc_attr( $mail_doctor_profile['id'] ); ?>"
																			name="mail_doctor_profiles[<?php echo esc_attr( $mail_doctor_row_key ); ?>][from_name]"
																			value="<?php echo esc_attr( $mail_doctor_profile['from_name'] ?? '' ); ?>"
																			class="regular-text" /></td>
																</tr>
																<tr>
																	<th scope="row">
																		<?php esc_html_e( 'Remove profile', 'mail-doctor' ); ?>
																	</th>
																	<td>
																		<label>
																			<input type="checkbox"
																				name="mail_doctor_profiles[<?php echo esc_attr( $mail_doctor_row_key ); ?>][_delete]"
																				value="1" />
																			<?php esc_html_e( 'Delete this profile on save (logs using it will remain intact).', 'mail-doctor' ); ?>
																		</label>
																	</td>
																</tr>
															</table>
														</div>
													</section>
												<?php endforeach; ?>
											</div>
										</div>
									<?php endif; ?>
								</div>
							</section>


							<section class="mail-doctor-card mail-doctor-settings-panel mail-doctor-settings-panel--profiles-active">
								<header class="mail-doctor-settings-panel__header">
									<span
										class="mail-doctor-settings-panel__eyebrow"><?php esc_html_e( 'Routing', 'mail-doctor' ); ?></span>
									<h3 class="mail-doctor-settings-panel__title">
										<?php esc_html_e( 'Active profile', 'mail-doctor' ); ?>
									</h3>
								</header>
								<p class="mail-doctor-settings-panel__hint">
									<?php esc_html_e( 'Choose which profile Mail Doctor should use by default. “Default SMTP Settings” maps to the SMTP & Identity configuration.', 'mail-doctor' ); ?>
								</p>
								<select name="mail_doctor_settings[active_profile]" class="mail-doctor-select">
									<?php foreach ( $mail_doctor_profile_choices as $mail_doctor_profile_value => $mail_doctor_profile_label ) : ?>
										<option value="<?php echo esc_attr( $mail_doctor_profile_value ); ?>" <?php selected( $mail_doctor_active_profile, (string) $mail_doctor_profile_value ); ?>>
											<?php echo esc_html( $mail_doctor_profile_label ); ?>
										</option>
									<?php endforeach; ?>
								</select>
							</section>
						</div>

					<?php elseif ( 'retry' === $mail_doctor_section ) : ?>
						<header class="mail-doctor-settings-header">
							<span
								class="mail-doctor-settings-header__eyebrow"><?php esc_html_e( 'Resilience', 'mail-doctor' ); ?></span>
							<h2 class="mail-doctor-settings-header__title">
								<?php esc_html_e( 'Retry & Queue', 'mail-doctor' ); ?>
							</h2>
							<p class="mail-doctor-settings-header__description">
								<?php esc_html_e( 'Adjust how Mail Doctor retries failed emails and monitors the outbound queue.', 'mail-doctor' ); ?>
							</p>
						</header>

						<div class="mail-doctor-section-summary" role="list">
							<div class="mail-doctor-section-summary__item" role="listitem">
								<span
									class="mail-doctor-section-summary__eyebrow"><?php esc_html_e( 'Dispatcher', 'mail-doctor' ); ?></span>
								<strong
									class="mail-doctor-section-summary__value"><?php echo $mail_doctor_cron_active ? esc_html__( 'Running', 'mail-doctor' ) : esc_html__( 'Attention needed', 'mail-doctor' ); ?></strong>
							</div>
							<div class="mail-doctor-section-summary__item" role="listitem">
								<span
									class="mail-doctor-section-summary__eyebrow"><?php esc_html_e( 'Maximum attempts', 'mail-doctor' ); ?></span>
								<strong
									class="mail-doctor-section-summary__value"><?php echo esc_html( (string) ( $mail_doctor_settings['max_attempts'] ?? 5 ) ); ?></strong>
							</div>
							<div class="mail-doctor-section-summary__item" role="listitem">
								<span
									class="mail-doctor-section-summary__eyebrow"><?php esc_html_e( 'Backoff schedule', 'mail-doctor' ); ?></span>
								<strong
									class="mail-doctor-section-summary__value"><?php esc_html_e( '5m → 15m → 1h → 4h → 12h', 'mail-doctor' ); ?></strong>
							</div>
						</div>

						<div class="mail-doctor-settings-stack">
							<section class="mail-doctor-card mail-doctor-settings-panel">
								<header class="mail-doctor-settings-panel__header">
									<span
										class="mail-doctor-settings-panel__eyebrow"><?php esc_html_e( 'Queue settings', 'mail-doctor' ); ?></span>
									<h3 class="mail-doctor-settings-panel__title">
										<?php esc_html_e( 'Retries & cadence', 'mail-doctor' ); ?>
									</h3>
								</header>
								<table class="form-table mail-doctor-settings-table">
									<tbody>
										<tr>
											<th scope="row"><label
													for="mail-doctor-max-attempts"><?php esc_html_e( 'Maximum attempts', 'mail-doctor' ); ?></label>
											</th>
											<td><input type="number" id="mail-doctor-max-attempts" name="mail_doctor_settings[max_attempts]"
													value="<?php echo esc_attr( $mail_doctor_settings['max_attempts'] ); ?>" min="1"
													class="small-text" /></td>
										</tr>
										<tr>
											<th scope="row"><?php esc_html_e( 'Backoff schedule', 'mail-doctor' ); ?>
											</th>
											<td>
												<p class="description">
													<?php esc_html_e( '5m → 15m → 1h → 4h → 12h', 'mail-doctor' ); ?>
												</p>
											</td>
										</tr>
										<tr>
											<th scope="row"><?php esc_html_e( 'CRON status', 'mail-doctor' ); ?></th>
											<td>
												<span
													class="mail-doctor-badge <?php echo $mail_doctor_settings['cron_active'] ? 'is-ok' : 'is-fail'; ?>"><?php echo $mail_doctor_settings['cron_active'] ? esc_html__( 'Active', 'mail-doctor' ) : esc_html__( 'Missing', 'mail-doctor' ); ?></span>
												<p class="description">
													<?php esc_html_e( 'Mail Doctor relies on WP-Cron or a real cron to process the queue.', 'mail-doctor' ); ?>
												</p>
											</td>
										</tr>
									</tbody>
								</table>
								<div class="mail-doctor-settings-actions">
									<button type="button" class="button"
										id="mail-doctor-run-queue-now"><?php esc_html_e( 'Run queue now', 'mail-doctor' ); ?></button>
								</div>
							</section>
						</div>

					<?php elseif ( 'logging' === $mail_doctor_section ) : ?>
						<header class="mail-doctor-settings-header">
							<span
								class="mail-doctor-settings-header__eyebrow"><?php esc_html_e( 'Compliance', 'mail-doctor' ); ?></span>
							<h2 class="mail-doctor-settings-header__title">
								<?php esc_html_e( 'Logging & Privacy', 'mail-doctor' ); ?>
							</h2>
							<p class="mail-doctor-settings-header__description">
								<?php esc_html_e( 'Control how long Mail Doctor stores email metadata and whether message content is redacted.', 'mail-doctor' ); ?>
							</p>
						</header>

						<div class="mail-doctor-section-summary" role="list">
							<div class="mail-doctor-section-summary__item" role="listitem">
								<span
									class="mail-doctor-section-summary__eyebrow"><?php esc_html_e( 'Retention (sent)', 'mail-doctor' ); ?></span>
								<strong class="mail-doctor-section-summary__value">
								<?php
								/* translators: %d: retention days */
								echo esc_html( sprintf( esc_html__( '%d days', 'mail-doctor' ), (int) ( $mail_doctor_settings['retention_sent'] ?? 90 ) ) );
								?>
								</strong>
							</div>
							<div class="mail-doctor-section-summary__item" role="listitem">
								<span
									class="mail-doctor-section-summary__eyebrow"><?php esc_html_e( 'Payload storage', 'mail-doctor' ); ?></span>
								<strong
									class="mail-doctor-section-summary__value"><?php echo ! empty( $mail_doctor_settings['redact_content'] ) ? esc_html__( 'Redacted', 'mail-doctor' ) : esc_html__( 'Full copies', 'mail-doctor' ); ?></strong>
							</div>

						</div>

						<div class="mail-doctor-settings-stack">
							<section class="mail-doctor-card mail-doctor-settings-panel mail-doctor-settings-panel--logging">
								<?php
								$mail_doctor_retention_sent_days    = isset( $mail_doctor_settings['retention_sent'] ) ? (int) $mail_doctor_settings['retention_sent'] : 90;
								$mail_doctor_retention_failed_days  = isset( $mail_doctor_settings['retention_failed'] ) ? (int) $mail_doctor_settings['retention_failed'] : 30;
								$mail_doctor_retention_payload_days = isset( $mail_doctor_settings['retention_payload'] ) ? (int) $mail_doctor_settings['retention_payload'] : 7;
								$mail_doctor_retention_menu_hint    = sprintf(
									/* translators: 1: days sent emails kept, 2: days failed emails kept, 3: days payload kept. */
									esc_html__( 'Sent %1$d · Failed %2$d · Payload %3$d days', 'mail-doctor' ),
									$mail_doctor_retention_sent_days,
									$mail_doctor_retention_failed_days,
									$mail_doctor_retention_payload_days
								);
								$mail_doctor_privacy_mask_label = ! empty( $mail_doctor_settings['mask_recipients'] ) ? esc_html__( 'Masking on', 'mail-doctor' ) : esc_html__( 'Masking off', 'mail-doctor' );
								$mail_doctor_privacy_body_label = ! empty( $mail_doctor_settings['redact_content'] ) ? esc_html__( 'Payload redacted', 'mail-doctor' ) : esc_html__( 'Payload stored', 'mail-doctor' );
								$mail_doctor_privacy_menu_hint  = $mail_doctor_privacy_mask_label . ' · ' . $mail_doctor_privacy_body_label;

								?>
								<div class="mail-doctor-tab-layout mail-doctor-logging-layout" data-mail-doctor-tab-menu>
									<nav class="mail-doctor-tab-menu mail-doctor-logging-menu"
										aria-label="<?php esc_attr_e( 'Logging and privacy options', 'mail-doctor' ); ?>"
										role="tablist">
										<ul>
											<li>
												<button type="button" class="mail-doctor-tab-menu__link is-active"
													id="mail-doctor-logging-retention-tab" role="tab"
													data-mail-doctor-tab-target="mail-doctor-logging-retention"
													aria-controls="mail-doctor-logging-retention" aria-selected="true" tabindex="0">
													<span class="dashicons dashicons-backup" aria-hidden="true"></span>
													<span
														class="mail-doctor-tab-menu__label"><?php esc_html_e( 'Retention', 'mail-doctor' ); ?></span>
													<span
														class="mail-doctor-tab-menu__sublabel"><?php echo esc_html( $mail_doctor_retention_menu_hint ); ?></span>
												</button>
											</li>
											<li>
												<button type="button" class="mail-doctor-tab-menu__link" id="mail-doctor-logging-privacy-tab"
													role="tab" data-mail-doctor-tab-target="mail-doctor-logging-privacy"
													aria-controls="mail-doctor-logging-privacy" aria-selected="false" tabindex="-1">
													<span class="dashicons dashicons-shield-alt" aria-hidden="true"></span>
													<span
														class="mail-doctor-tab-menu__label"><?php esc_html_e( 'Privacy', 'mail-doctor' ); ?></span>
													<span
														class="mail-doctor-tab-menu__sublabel"><?php echo esc_html( $mail_doctor_privacy_menu_hint ); ?></span>
												</button>
											</li>

											<li>
												<button type="button" class="mail-doctor-tab-menu__link"
													id="mail-doctor-logging-maintenance-tab" role="tab"
													data-mail-doctor-tab-target="mail-doctor-logging-maintenance"
													aria-controls="mail-doctor-logging-maintenance" aria-selected="false"
													tabindex="-1">
													<span class="dashicons dashicons-database" aria-hidden="true"></span>
													<span
														class="mail-doctor-tab-menu__label"><?php esc_html_e( 'Maintenance', 'mail-doctor' ); ?></span>
													<span
														class="mail-doctor-tab-menu__sublabel"><?php esc_html_e( 'Export or purge logs', 'mail-doctor' ); ?></span>
												</button>
											</li>
										</ul>
									</nav>

									<div class="mail-doctor-tab-panels mail-doctor-logging-panels">
										<section id="mail-doctor-logging-retention" class="mail-doctor-tab-pane mail-doctor-logging-pane is-active"
											role="tabpanel" aria-labelledby="mail-doctor-logging-retention-tab" aria-hidden="false"
											data-mail-doctor-tab-panel="mail-doctor-logging-retention">
											<header class="mail-doctor-tab-pane__header mail-doctor-logging-pane__header">
												<span
													class="mail-doctor-tab-pane__eyebrow"><?php esc_html_e( 'Retention', 'mail-doctor' ); ?></span>
												<h3 class="mail-doctor-tab-pane__title">
													<?php esc_html_e( 'Log window', 'mail-doctor' ); ?>
												</h3>
												<p class="mail-doctor-tab-pane__hint">
													<?php esc_html_e( 'Control how long Mail Doctor keeps successful, failed, and payload data before purging.', 'mail-doctor' ); ?>
												</p>
											</header>
											<div class="mail-doctor-tab-pane__body">
												<table class="form-table mail-doctor-settings-table">
													<tbody>
														<tr>
															<th scope="row"><label
																	for="mail-doctor-retention-sent"><?php esc_html_e( 'Retention for sent emails (days)', 'mail-doctor' ); ?></label>
															</th>
															<td><input type="number" id="mail-doctor-retention-sent"
																	name="mail_doctor_settings[retention_sent]"
																	value="<?php echo esc_attr( $mail_doctor_settings['retention_sent'] ); ?>"
																	class="small-text" min="1" /></td>
														</tr>
														<tr>
															<th scope="row"><label
																	for="mail-doctor-retention-failed"><?php esc_html_e( 'Retention for failed emails (days)', 'mail-doctor' ); ?></label>
															</th>
															<td><input type="number" id="mail-doctor-retention-failed"
																	name="mail_doctor_settings[retention_failed]"
																	value="<?php echo esc_attr( $mail_doctor_settings['retention_failed'] ); ?>"
																	class="small-text" min="1" /></td>
														</tr>
														<tr>
															<th scope="row"><label
																	for="mail-doctor-retention-payload"><?php esc_html_e( 'Retention for payload (days)', 'mail-doctor' ); ?></label>
															</th>
															<td><input type="number" id="mail-doctor-retention-payload"
																	name="mail_doctor_settings[retention_payload]"
																	value="<?php echo esc_attr( $mail_doctor_settings['retention_payload'] ); ?>"
																	class="small-text" min="1" /></td>
														</tr>
													</tbody>
												</table>
											</div>
										</section>

										<section id="mail-doctor-logging-privacy" class="mail-doctor-tab-pane mail-doctor-logging-pane" role="tabpanel"
											aria-labelledby="mail-doctor-logging-privacy-tab" aria-hidden="true"
											data-mail-doctor-tab-panel="mail-doctor-logging-privacy" hidden>
											<header class="mail-doctor-tab-pane__header mail-doctor-logging-pane__header">
												<span
													class="mail-doctor-tab-pane__eyebrow"><?php esc_html_e( 'Privacy', 'mail-doctor' ); ?></span>
												<h3 class="mail-doctor-tab-pane__title">
													<?php esc_html_e( 'Redaction options', 'mail-doctor' ); ?>
												</h3>
												<p class="mail-doctor-tab-pane__hint">
													<?php esc_html_e( 'Tune masking and redaction policies to satisfy compliance rules without losing necessary insight.', 'mail-doctor' ); ?>
												</p>
											</header>
											<div class="mail-doctor-tab-pane__body">
												<table class="form-table mail-doctor-settings-table">
													<tbody>
														<tr>
															<th scope="row">
																<?php esc_html_e( 'Mask recipient emails', 'mail-doctor' ); ?>
															</th>
															<td><label><input type="hidden"
																		name="mail_doctor_settings[mask_recipients]" value="0" />
																	<input type="checkbox"
																		name="mail_doctor_settings[mask_recipients]" value="1" <?php checked( $mail_doctor_settings['mask_recipients'], true ); ?> />
																	<?php esc_html_e( 'Hash recipient addresses in logs.', 'mail-doctor' ); ?></label>
															</td>
														</tr>
														<tr>
															<th scope="row">
																<?php esc_html_e( 'Redact headers and bodies', 'mail-doctor' ); ?>
															</th>
															<td><label><input type="hidden"
																		name="mail_doctor_settings[redact_content]" value="0" />
																	<input type="checkbox"
																		name="mail_doctor_settings[redact_content]" value="1" <?php checked( $mail_doctor_settings['redact_content'], true ); ?> />
																	<?php esc_html_e( 'Store metadata only.', 'mail-doctor' ); ?></label>
															</td>
														</tr>
													</tbody>
												</table>
											</div>
										</section>



										<section id="mail-doctor-logging-maintenance" class="mail-doctor-tab-pane mail-doctor-logging-pane"
											role="tabpanel" aria-labelledby="mail-doctor-logging-maintenance-tab" aria-hidden="true"
											data-mail-doctor-tab-panel="mail-doctor-logging-maintenance" hidden>
											<header class="mail-doctor-tab-pane__header mail-doctor-logging-pane__header">
												<span
													class="mail-doctor-tab-pane__eyebrow"><?php esc_html_e( 'Maintenance', 'mail-doctor' ); ?></span>
												<h3 class="mail-doctor-tab-pane__title">
													<?php esc_html_e( 'Exports & purge', 'mail-doctor' ); ?>
												</h3>
												<p class="mail-doctor-tab-pane__hint">
													<?php esc_html_e( 'Quickly export recent activity or purge everything when resetting environments.', 'mail-doctor' ); ?>
												</p>
											</header>
											<div class="mail-doctor-tab-pane__body" id="mail-doctor-export-tools">
												<table class="form-table mail-doctor-settings-table">
													<tbody>
														<tr>
															<th scope="row"><label
																	for="mail-doctor-cleanup-on-uninstall"><?php esc_html_e( 'Full data removal on uninstall', 'mail-doctor' ); ?></label>
															</th>
															<td>
																<label>
																	<input type="checkbox"
																		id="mail-doctor-cleanup-on-uninstall"
																		name="mail_doctor_settings[cleanup_on_uninstall]"
																		value="1" <?php checked( ! empty( $mail_doctor_settings['cleanup_on_uninstall'] ), true ); ?> />
																	<?php esc_html_e( 'When this plugin is deleted, also remove all Mail Doctor data (logs, archived logs, profiles, suppressions, secrets, settings, and API keys). This action is irreversible.', 'mail-doctor' ); ?>
																</label>
															</td>
														</tr>
													</tbody>
												</table>
												<div class="mail-doctor-settings-actions">
													<button type="button" class="button button-secondary"
														id="mail-doctor-purge-logs"
														data-confirm="<?php esc_attr_e( 'Are you sure you want to purge all logs?', 'mail-doctor' ); ?>"><?php esc_html_e( 'Purge all logs', 'mail-doctor' ); ?></button>
												</div>
											</div>
										</section>
									</div>
								</div>
							</section>
						</div>

					<?php elseif ( 'woocommerce' === $mail_doctor_section ) : ?>
						<header class="mail-doctor-settings-header">
							<span
								class="mail-doctor-settings-header__eyebrow"><?php esc_html_e( 'WooCommerce', 'mail-doctor' ); ?></span>
							<h2 class="mail-doctor-settings-header__title">
								<?php esc_html_e( 'WooCommerce Email Control', 'mail-doctor' ); ?>
							</h2>
							<p class="mail-doctor-settings-header__description">
								<?php esc_html_e( 'Enable or suppress WooCommerce emails and generate previews without leaving the dashboard.', 'mail-doctor' ); ?>
							</p>
						</header>

						<div class="mail-doctor-section-summary" role="list">
							<div class="mail-doctor-section-summary__item" role="listitem">
								<span
									class="mail-doctor-section-summary__eyebrow"><?php esc_html_e( 'Plugin status', 'mail-doctor' ); ?></span>
								<strong
									class="mail-doctor-section-summary__value"><?php echo $mail_doctor_woocommerce_available ? esc_html__( 'WooCommerce active', 'mail-doctor' ) : esc_html__( 'WooCommerce inactive', 'mail-doctor' ); ?></strong>
							</div>
							<div class="mail-doctor-section-summary__item" role="listitem">
								<span
									class="mail-doctor-section-summary__eyebrow"><?php esc_html_e( 'Overrides', 'mail-doctor' ); ?></span>
								<strong class="mail-doctor-section-summary__value">
									<?php
									printf(
										/* translators: 1: number of overridden templates, 2: total templates */
										esc_html__( '%1$d of %2$d templates', 'mail-doctor' ),
										(int) $mail_doctor_woo_overrides_total,
										(int) $mail_doctor_woo_email_total
									);
									?>
								</strong>
							</div>
							<div class="mail-doctor-section-summary__item" role="listitem">
								<span
									class="mail-doctor-section-summary__eyebrow"><?php esc_html_e( 'Disabled templates', 'mail-doctor' ); ?></span>
								<strong
									class="mail-doctor-section-summary__value"><?php echo esc_html( (string) $mail_doctor_woo_disabled_total ); ?></strong>
							</div>
						</div>

						<div class="mail-doctor-settings-stack">
							<input type="hidden" id="mail-doctor-preview-email-setting" name="mail_doctor_settings[preview_email]"
								value="<?php echo esc_attr( $mail_doctor_settings['preview_email'] ); ?>" />
							<input type="hidden" id="mail-doctor-preview-order-setting" name="mail_doctor_settings[preview_order]"
								value="<?php echo esc_attr( $mail_doctor_settings['preview_order'] ); ?>" />

							<?php if ( ! $mail_doctor_woocommerce_available ) : ?>
								<section class="mail-doctor-card mail-doctor-settings-panel">
									<div class="notice notice-warning inline">
										<p><?php esc_html_e( 'WooCommerce must be active to manage email overrides. Activate WooCommerce to control these toggles.', 'mail-doctor' ); ?>
										</p>
									</div>
								</section>
							<?php else : ?>
								<section class="mail-doctor-card mail-doctor-settings-panel mail-doctor-settings-panel--woocommerce">
									<header class="mail-doctor-settings-panel__header">
										<span
											class="mail-doctor-settings-panel__eyebrow"><?php esc_html_e( 'Overrides', 'mail-doctor' ); ?></span>
										<h3 class="mail-doctor-settings-panel__title">
											<?php esc_html_e( 'Transactional emails', 'mail-doctor' ); ?>
										</h3>
									</header>
									<?php if ( empty( $mail_doctor_woo_email_rows ) ) : ?>
										<div class="mail-doctor-empty mail-doctor-empty--panel">
											<span class="dashicons dashicons-email-alt" aria-hidden="true"></span>
											<div class="mail-doctor-empty__content">
												<h4><?php esc_html_e( 'WooCommerce emails unavailable', 'mail-doctor' ); ?>
												</h4>
												<p><?php esc_html_e( 'Activate WooCommerce or ensure its email classes are loaded to manage overrides here.', 'mail-doctor' ); ?>
												</p>
												<a class="button button-secondary"
													href="https://woocommerce.com/document/managing-emails/" target="_blank"
													rel="noopener noreferrer"><?php esc_html_e( 'Learn more', 'mail-doctor' ); ?></a>
											</div>
										</div>
									<?php else : ?>
										<?php
										$mail_doctor_inherit_hint = esc_html__( 'Mail Doctor inherits the WooCommerce setting.', 'mail-doctor' );
										$mail_doctor_enable_hint  = esc_html__( 'Mail Doctor forces this email on.', 'mail-doctor' );
										$mail_doctor_disable_hint = esc_html__( 'Mail Doctor suppresses this email.', 'mail-doctor' );
										?>
										<p class="description">
											<?php esc_html_e( 'Mail Doctor overrides run through the WooCommerce email filters and apply immediately.', 'mail-doctor' ); ?>
										</p>
										<div class="mail-doctor-woo-table" data-mail-doctor-woo-table
											data-confirm-enable="<?php esc_attr_e( 'Enable all Mail Doctor overrides?', 'mail-doctor' ); ?>"
											data-confirm-disable="<?php esc_attr_e( 'Disable all Mail Doctor overrides?', 'mail-doctor' ); ?>"
											data-confirm-match="<?php esc_attr_e( 'Reset overrides to match the default WooCommerce settings?', 'mail-doctor' ); ?>">
											<div class="mail-doctor-woo-table__actions"
												aria-label="<?php esc_attr_e( 'Bulk overrides', 'mail-doctor' ); ?>">
												<button type="button" class="button button-secondary mail-doctor-woo-table__action"
													data-mail-doctor-woo-action="enable"><?php esc_html_e( 'Enable all overrides', 'mail-doctor' ); ?></button>
												<button type="button" class="button button-secondary mail-doctor-woo-table__action"
													data-mail-doctor-woo-action="disable"><?php esc_html_e( 'Disable all overrides', 'mail-doctor' ); ?></button>
												<button type="button" class="button button-link mail-doctor-woo-table__action"
													data-mail-doctor-woo-action="match"><?php esc_html_e( 'Match WooCommerce defaults', 'mail-doctor' ); ?></button>
											</div>
											<table id="mail-doctor-woocommerce-overrides" class="widefat mail-doctor-table mail-doctor-woo-table__grid">
												<thead>
													<tr>
														<th scope="col" class="mail-doctor-woo-table__heading mail-doctor-woo-table__heading--email">
															<?php esc_html_e( 'Email template', 'mail-doctor' ); ?>
														</th>
														<th scope="col" class="mail-doctor-woo-table__heading mail-doctor-woo-table__heading--status">
															<?php esc_html_e( 'WooCommerce status', 'mail-doctor' ); ?>
														</th>
														<th scope="col"
															class="mail-doctor-woo-table__heading mail-doctor-woo-table__heading--override">
															<?php esc_html_e( 'Mail Doctor override', 'mail-doctor' ); ?>
														</th>
													</tr>
												</thead>
												<tbody>
													<?php foreach ( $mail_doctor_woo_email_rows as $mail_doctor_email_row ) : ?>
														<?php
														$mail_doctor_status_class         = $mail_doctor_email_row['default_enabled'] ? 'is-ok' : 'is-fail';
														$mail_doctor_status_label         = $mail_doctor_email_row['default_enabled'] ? esc_html__( 'Enabled in WooCommerce', 'mail-doctor' ) : esc_html__( 'Disabled in WooCommerce', 'mail-doctor' );
														$mail_doctor_override_class       = $mail_doctor_email_row['current_enabled'] ? 'is-ok' : 'is-fail';
														$mail_doctor_override_state_label = $mail_doctor_email_row['current_enabled'] ? esc_html__( 'Enabled', 'mail-doctor' ) : esc_html__( 'Disabled', 'mail-doctor' );
														$mail_doctor_override_hint        = $mail_doctor_email_row['overridden']
															? ( $mail_doctor_email_row['current_enabled'] ? $mail_doctor_enable_hint : $mail_doctor_disable_hint )
															: $mail_doctor_inherit_hint;
														$mail_doctor_checkbox_id          = 'mail-doctor-override-' . sanitize_html_class( $mail_doctor_email_row['id'] );
														$mail_doctor_default_enabled      = $mail_doctor_email_row['default_enabled'] ? '1' : '0';
														?>
														<tr class="mail-doctor-woo-table__row"
															data-default-enabled="<?php echo esc_attr( $mail_doctor_default_enabled ); ?>"
															data-hint-enabled="<?php echo esc_attr( $mail_doctor_enable_hint ); ?>"
															data-hint-disabled="<?php echo esc_attr( $mail_doctor_disable_hint ); ?>"
															data-hint-inherit="<?php echo esc_attr( $mail_doctor_inherit_hint ); ?>"
															data-label-enabled="<?php echo esc_attr__( 'Enabled', 'mail-doctor' ); ?>"
															data-label-disabled="<?php echo esc_attr__( 'Disabled', 'mail-doctor' ); ?>"
															data-label-inherit="<?php echo esc_attr__( 'Inherited', 'mail-doctor' ); ?>">
															<td class="mail-doctor-woo-table__cell mail-doctor-woo-table__cell--email"
																data-label="<?php esc_attr_e( 'Email template', 'mail-doctor' ); ?>">
																<div class="mail-doctor-woo-table__email">
																	<strong
																		class="mail-doctor-woo-table__title"><?php echo esc_html( $mail_doctor_email_row['title'] ); ?></strong>
																	<?php if ( ! empty( $mail_doctor_email_row['description'] ) ) : ?>
																		<p class="description mail-doctor-woo-table__summary">
																			<?php echo esc_html( $mail_doctor_email_row['description'] ); ?>
																		</p>
																	<?php endif; ?>
																</div>
															</td>
															<td class="mail-doctor-woo-table__cell mail-doctor-woo-table__cell--status"
																data-label="<?php esc_attr_e( 'WooCommerce status', 'mail-doctor' ); ?>">
																<span
																	class="mail-doctor-badge mail-doctor-badge--status <?php echo esc_attr( $mail_doctor_status_class ); ?>"><?php echo esc_html( $mail_doctor_status_label ); ?></span>
															</td>
															<td class="mail-doctor-woo-table__cell mail-doctor-woo-table__cell--override"
																data-label="<?php esc_attr_e( 'Mail Doctor override', 'mail-doctor' ); ?>">
																<input type="hidden"
																	name="mail_doctor_wc_emails[<?php echo esc_attr( $mail_doctor_email_row['id'] ); ?>]"
																	value="0" />
																<div class="mail-doctor-toggle mail-doctor-woo-toggle">
																	<input type="checkbox" id="<?php echo esc_attr( $mail_doctor_checkbox_id ); ?>"
																		class="mail-doctor-toggle__control"
																		name="mail_doctor_wc_emails[<?php echo esc_attr( $mail_doctor_email_row['id'] ); ?>]"
																		value="1" <?php checked( $mail_doctor_email_row['current_enabled'], true ); ?>
																		data-mail-doctor-woo-toggle
																		data-mail-doctor-woo-default="<?php echo esc_attr( $mail_doctor_default_enabled ); ?>" />
																	<label class="mail-doctor-toggle__label"
																		for="<?php echo esc_attr( $mail_doctor_checkbox_id ); ?>">
																		<span class="mail-doctor-toggle__track" aria-hidden="true"><span
																				class="mail-doctor-toggle__thumb"></span></span>
																		<span
																			class="mail-doctor-toggle__text"><?php esc_html_e( 'Send with Mail Doctor', 'mail-doctor' ); ?></span>
																	</label>
																	<button type="button"
																		class="dashicons dashicons-editor-help mail-doctor-toggle__help"
																		aria-label="<?php esc_attr_e( 'Learn how Mail Doctor overrides this email', 'mail-doctor' ); ?>"
																		data-mail-doctor-tooltip="<?php echo esc_attr( $mail_doctor_override_hint ); ?>"></button>
																</div>
																<p class="description mail-doctor-woo-table__description">
																	<span
																		class="mail-doctor-badge mail-doctor-badge--status <?php echo esc_attr( $mail_doctor_override_class ); ?>"
																		data-mail-doctor-state-badge><?php echo esc_html( $mail_doctor_override_state_label ); ?></span>
																	<span class="mail-doctor-woo-table__hint"
																		data-mail-doctor-state-hint><?php echo esc_html( $mail_doctor_override_hint ); ?></span>
																</p>
															</td>
														</tr>
													<?php endforeach; ?>
												</tbody>
											</table>
										</div>
									<?php endif; ?>
								</section>

								<section class="mail-doctor-card mail-doctor-settings-panel mail-doctor-settings-panel--woocommerce-preview">
									<header class="mail-doctor-settings-panel__header">
										<span
											class="mail-doctor-settings-panel__eyebrow"><?php esc_html_e( 'Preview', 'mail-doctor' ); ?></span>
										<h3 class="mail-doctor-settings-panel__title">
											<?php esc_html_e( 'Email preview', 'mail-doctor' ); ?>
										</h3>
									</header>
									<?php if ( ! empty( $mail_doctor_woo_preview_emails ) ) : ?>
										<?php
										$mail_doctor_selected_preview_email_title = '';
										foreach ( $mail_doctor_woo_preview_emails as $mail_doctor_preview_email ) {
											if ( $mail_doctor_settings['preview_email'] === $mail_doctor_preview_email['id'] ) {
												$mail_doctor_selected_preview_email_title = $mail_doctor_preview_email['title'];
												break;
											}
										}

										$mail_doctor_preview_context_parts = array_filter(
											array(
												$mail_doctor_selected_preview_email_title ? sprintf(
													/* translators: %s: Email title */
													esc_html__( 'Email: %s', 'mail-doctor' ),
													$mail_doctor_selected_preview_email_title
												) : '',
												! empty( $mail_doctor_settings['preview_order'] ) ? sprintf(
													/* translators: %s: Order ID */
													esc_html__( 'Order: #%s', 'mail-doctor' ),
													$mail_doctor_settings['preview_order']
												) : '',
											)
										);
										?>
										<div class="mail-doctor-woo-preview">
											<?php if ( ! empty( $mail_doctor_preview_context_parts ) ) : ?>
												<nav class="mail-doctor-woo-preview__breadcrumbs"
													aria-label="<?php esc_attr_e( 'Preview context', 'mail-doctor' ); ?>">
													<span class="dashicons dashicons-admin-links" aria-hidden="true"></span>
													<?php foreach ( $mail_doctor_preview_context_parts as $mail_doctor_preview_part ) : ?>
														<span class="mail-doctor-woo-preview__crumb"><?php echo esc_html( $mail_doctor_preview_part ); ?></span>
													<?php endforeach; ?>
												</nav>
											<?php endif; ?>
											<div class="mail-doctor-woo-preview__controls">
												<label for="mail-doctor-preview-email"
													class="screen-reader-text"><?php esc_html_e( 'WooCommerce email', 'mail-doctor' ); ?></label>
												<select id="mail-doctor-preview-email" class="mail-doctor-select"
													data-placeholder="<?php esc_attr_e( 'Select email', 'mail-doctor' ); ?>">
													<option value=""></option>
													<?php foreach ( $mail_doctor_woo_preview_emails as $mail_doctor_preview_email ) : ?>
														<option value="<?php echo esc_attr( $mail_doctor_preview_email['id'] ); ?>" <?php selected( $mail_doctor_settings['preview_email'], $mail_doctor_preview_email['id'] ); ?>>
															<?php echo esc_html( $mail_doctor_preview_email['title'] ); ?>
														</option>
													<?php endforeach; ?>
												</select>
												<label for="mail-doctor-preview-order"
													class="screen-reader-text"><?php esc_html_e( 'Order ID', 'mail-doctor' ); ?></label>
												<input type="number" id="mail-doctor-preview-order" class="small-text"
													value="<?php echo esc_attr( $mail_doctor_settings['preview_order'] ); ?>"
													placeholder="<?php esc_attr_e( 'Order ID (optional)', 'mail-doctor' ); ?>"
													min="1" />
												<button type="button" class="button button-secondary mail-doctor-preview-action"
													data-mode="render"><?php esc_html_e( 'Preview', 'mail-doctor' ); ?></button>
												<button type="button" class="button mail-doctor-preview-action"
													data-mode="send"><?php esc_html_e( 'Send preview', 'mail-doctor' ); ?></button>
											</div>
											<p class="description">
												<?php esc_html_e( 'Leave Order ID blank and Mail Doctor will use your most recent order as the preview context.', 'mail-doctor' ); ?>
											</p>
											<div class="mail-doctor-woo-preview__status" aria-live="polite"></div>
											<div class="mail-doctor-woo-preview__output"></div>
										</div>
									<?php else : ?>
										<div class="notice notice-info inline">
											<p><?php esc_html_e( 'No WooCommerce emails were detected. Confirm WooCommerce is fully loaded before accessing this page.', 'mail-doctor' ); ?>
											</p>
										</div>
									<?php endif; ?>
								</section>
							<?php endif; ?>
						</div>



					<?php elseif ( 'advanced' === $mail_doctor_section ) : ?>
						<header class="mail-doctor-settings-header">
							<span
								class="mail-doctor-settings-header__eyebrow"><?php esc_html_e( 'Power users', 'mail-doctor' ); ?></span>
							<h2 class="mail-doctor-settings-header__title"><?php esc_html_e( 'Advanced', 'mail-doctor' ); ?>
							</h2>
							<p class="mail-doctor-settings-header__description">
								<?php esc_html_e( 'Tune experimental behaviour. Some toggles are read-only until supporting services are available.', 'mail-doctor' ); ?>
							</p>
						</header>

						<div class="mail-doctor-section-summary" role="list">
							<div class="mail-doctor-section-summary__item" role="listitem">
								<span
									class="mail-doctor-section-summary__eyebrow"><?php esc_html_e( 'Async mode', 'mail-doctor' ); ?></span>
								<strong class="mail-doctor-section-summary__value"><?php echo esc_html( $mail_doctor_async_label ); ?></strong>
							</div>
							<div class="mail-doctor-section-summary__item" role="listitem">
								<span
									class="mail-doctor-section-summary__eyebrow"><?php esc_html_e( 'Fallback', 'mail-doctor' ); ?></span>
								<strong
									class="mail-doctor-section-summary__value"><?php echo $mail_doctor_fallback_enabled ? esc_html__( 'wp_mail enabled', 'mail-doctor' ) : esc_html__( 'Fallback off', 'mail-doctor' ); ?></strong>
							</div>
							<div class="mail-doctor-section-summary__item" role="listitem">
								<span
									class="mail-doctor-section-summary__eyebrow"><?php esc_html_e( 'Debug logging', 'mail-doctor' ); ?></span>
								<strong
									class="mail-doctor-section-summary__value"><?php echo ! empty( $mail_doctor_settings['debug_logging'] ) ? esc_html__( 'Verbose', 'mail-doctor' ) : esc_html__( 'Standard', 'mail-doctor' ); ?></strong>
							</div>
						</div>

						<div class="mail-doctor-settings-stack">
							<section class="mail-doctor-card mail-doctor-settings-panel">
								<header class="mail-doctor-settings-panel__header">
									<span
										class="mail-doctor-settings-panel__eyebrow"><?php esc_html_e( 'Delivery', 'mail-doctor' ); ?></span>
									<h3 class="mail-doctor-settings-panel__title">
										<?php esc_html_e( 'Async mode', 'mail-doctor' ); ?>
									</h3>
								</header>
								<table class="form-table mail-doctor-settings-table">
									<tbody>
										<tr>
											<th scope="row"><label
													for="mail-doctor-async-mode"><?php esc_html_e( 'Async delivery mode', 'mail-doctor' ); ?></label>
											</th>
											<td>
												<select id="mail-doctor-async-mode" name="mail_doctor_settings[async_mode]" class="mail-doctor-select">
													<?php foreach ( HelperSettings::async_mode_choices() as $mail_doctor_mode_key => $mail_doctor_mode_label ) : ?>
														<?php $mail_doctor_disabled = ( 'action_scheduler' === $mail_doctor_mode_key && ! HelperSettings::action_scheduler_available() ); ?>
														<option value="<?php echo esc_attr( $mail_doctor_mode_key ); ?>" <?php selected( $mail_doctor_settings['async_mode'], $mail_doctor_mode_key ); ?><?php echo $mail_doctor_disabled ? ' disabled="disabled"' : ''; ?>>
															<?php echo esc_html( $mail_doctor_mode_label ); ?>
														</option>
													<?php endforeach; ?>
												</select>
												<p class="description">
													<?php esc_html_e( 'Control how Mail Doctor defers email delivery during the request lifecycle.', 'mail-doctor' ); ?>
												</p>
												<?php if ( ! HelperSettings::action_scheduler_available() ) : ?>
													<p class="description mail-doctor-field-note mail-doctor-field-note--warning">
														<?php esc_html_e( 'Install Action Scheduler to enable the background queue option.', 'mail-doctor' ); ?>
													</p>
												<?php endif; ?>
											</td>
										</tr>
									</tbody>
								</table>
							</section>

							<section class="mail-doctor-card mail-doctor-settings-panel">
								<header class="mail-doctor-settings-panel__header">
									<span
										class="mail-doctor-settings-panel__eyebrow"><?php esc_html_e( 'Failover', 'mail-doctor' ); ?></span>
									<h3 class="mail-doctor-settings-panel__title">
										<?php esc_html_e( 'Fallback handling', 'mail-doctor' ); ?>
									</h3>
								</header>
								<table class="form-table mail-doctor-settings-table">
									<tbody>
										<tr>
											<th scope="row"><label
													for="mail-doctor-fallback"><?php esc_html_e( 'Fallback to wp_mail()', 'mail-doctor' ); ?></label>
											</th>
											<td>
												<label>
													<input type="hidden" name="mail_doctor_settings[fallback_transport]" value="0" />
													<input type="checkbox" id="mail-doctor-fallback"
														name="mail_doctor_settings[fallback_transport]" value="1" <?php checked( $mail_doctor_settings['fallback_transport'], true ); ?> />
													<?php esc_html_e( 'Automatically defer to WordPress wp_mail() when Mail Doctor cannot reach the configured transport.', 'mail-doctor' ); ?>
												</label>
												<p class="description">
													<?php
													if ( ! empty( $mail_doctor_settings['fallback_transport'] ) ) {
														esc_html_e( 'Mail Doctor will log and surface each wp_mail() fallback so you can triage delivery issues quickly.', 'mail-doctor' );
													} else {
														esc_html_e( 'Enable this if you prefer Mail Doctor to try wp_mail() as a safety net when SMTP/API requests fail.', 'mail-doctor' );
													}
													?>
												</p>
											</td>
										</tr>
									</tbody>
								</table>
							</section>

							<section class="mail-doctor-card mail-doctor-settings-panel">
								<header class="mail-doctor-settings-panel__header">
									<span
										class="mail-doctor-settings-panel__eyebrow"><?php esc_html_e( 'Diagnostics', 'mail-doctor' ); ?></span>
									<h3 class="mail-doctor-settings-panel__title">
										<?php esc_html_e( 'Debug logging', 'mail-doctor' ); ?>
									</h3>
								</header>
								<table class="form-table mail-doctor-settings-table">
									<tbody>
										<tr>
											<th scope="row"><label
													for="mail-doctor-debug"><?php esc_html_e( 'Debug logging', 'mail-doctor' ); ?></label>
											</th>
											<td><label><input type="hidden" name="mail_doctor_settings[debug_logging]" value="0" />
													<input type="checkbox" id="mail-doctor-debug" name="mail_doctor_settings[debug_logging]"
														value="1" <?php checked( $mail_doctor_settings['debug_logging'], true ); ?> />
													<?php esc_html_e( 'Write verbose Mail Doctor logs (use sparingly).', 'mail-doctor' ); ?></label>
											</td>
										</tr>
									</tbody>
								</table>
							</section>

							<section class="mail-doctor-card mail-doctor-settings-panel">
								<header class="mail-doctor-settings-panel__header">
									<span
										class="mail-doctor-settings-panel__eyebrow"><?php esc_html_e( 'Guided setup', 'mail-doctor' ); ?></span>
									<h3 class="mail-doctor-settings-panel__title">
										<?php esc_html_e( 'Rerun onboarding', 'mail-doctor' ); ?>
									</h3>
								</header>
								<div class="mail-doctor-settings-panel__body">
									<p>
										<?php esc_html_e( 'Launch the onboarding wizard again to revisit provider setup and DNS checks, even if you have already configured Mail Doctor.', 'mail-doctor' ); ?>
									</p>
									<p class="description mail-doctor-field-note mail-doctor-field-note--warning">
										<?php esc_html_e( 'This resets your onboarding progress but does not change saved settings until you complete and save the wizard.', 'mail-doctor' ); ?>
									</p>
									<a class="button button-primary"
										href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=mail-doctor-onboarding&mail_doctor_onboarding_restart=1' ), 'mail_doctor_onboarding_restart' ) ); ?>">
										<?php esc_html_e( 'Open onboarding', 'mail-doctor' ); ?>
									</a>
								</div>
							</section>
						</div>

					<?php endif; ?>


					<?php
					/**
					 * Allow extensions to render additional settings sections.
					 *
					 * @since 1.0.0
					 *
					 * @param string               $mail_doctor_section  Current section key.
					 * @param array<string, mixed> $mail_doctor_settings Current settings array.
					 */
					do_action( 'mail_doctor_settings_section', $mail_doctor_section, $mail_doctor_settings );
					?>

					<p class="submit">
						<button type="submit"
							class="button button-primary"><?php esc_html_e( 'Save changes', 'mail-doctor' ); ?></button>
					</p>
			</form>
		</div>
	</div>
</div>