<?php

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

class Sabriel_Module_Documentazione extends Sabriel_Module {

	private $in_export = false;

	public function get_id() {
		return 'documentazione';
	}

	public function register_hooks() {
		add_action( 'admin_menu', [ $this, 'add_submenu' ], 37 );
		add_action( 'admin_post_sabriel_ai_export_doc', [ $this, 'scarica_export_html' ] );
	}

	public function scarica_export_html() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'Access denied', 'sabriel-ai' ) );
		}
		check_admin_referer( 'sabriel_ai_export_doc' );

		$validi = array_values( Sabriel_Module_Settings::lingue_locali() );

		$predefinito = in_array( get_user_locale(), $validi, true ) ? get_user_locale() : 'en_US';
		$loc         = isset( $_GET['loc'] ) ? sanitize_text_field( wp_unslash( $_GET['loc'] ) ) : $predefinito;
		if ( ! in_array( $loc, $validi, true ) ) {
			$loc = $predefinito;
		}

		$html = $this->genera_export_html( $loc );
		$this->in_export = false;
		$nome = 'documentation-sabriel-ai-' . strtolower( substr( $loc, 0, 2 ) ) . '.html';

		nocache_headers();
		header( 'Content-Type: text/html; charset=utf-8' );
		header( 'Content-Disposition: attachment; filename="' . $nome . '"' );
		header( 'Content-Length: ' . strlen( $html ) );
		echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- documento HTML generato dal plugin, ogni parte variabile e' gia' protetta dove viene composta.
		exit;
	}

	public function add_submenu() {
		add_submenu_page(
			'sabriel-ai',
			__( 'Documentation', 'sabriel-ai' ),
			__( 'Documentation', 'sabriel-ai' ),
			'manage_options',
			'sabriel-ai-docs',
			[ $this, 'render_page' ]
		);
	}

	private function css_condiviso() {
		return '
		.sab-doc-wrap{display:flex;gap:0;align-items:flex-start;max-width:1400px}
		.sab-doc-nav-col{position:sticky;top:48px;width:260px;flex-shrink:0;align-self:flex-start;max-height:calc(100vh - 80px);overflow-y:auto;border-radius:10px 0 0 10px}
		.sab-doc-nav{width:100%;box-sizing:border-box;background:#fff;border:1px solid #e5e5e5;border-radius:10px 0 0 10px;padding:18px 8px}
		.sab-doc-nav a{display:block;padding:7px 14px;font-size:13px;color:#444;text-decoration:none;border-radius:7px;line-height:1.4}
		.sab-doc-nav a:hover{background:#f0f5ff;color:#1461CC}
		.sab-doc-nav a.sab-doc-nav-sezione{font-weight:700;color:#111;margin-top:10px;font-size:13.5px}
		.sab-doc-nav a.sab-doc-nav-sotto{padding-left:26px;font-size:12.5px;color:#666}
		.sab-doc-main{flex:1;min-width:0;padding:0 0 60px 36px}
		.sab-doc-sezione{scroll-margin-top:30px;margin-bottom:56px;padding-bottom:8px;border-bottom:1px solid #eee}
		.sab-doc-sezione:last-child{border-bottom:none}
		.sab-doc-sezione h1{font-size:26px;margin:0 0 6px;color:#111;display:flex;align-items:center;gap:10px}
		.sab-doc-sezione > p.sab-doc-intro{color:#666;font-size:14px;margin:0 0 26px;max-width:680px;line-height:1.6}
		.sab-doc-voce{scroll-margin-top:30px;margin:30px 0}
		.sab-doc-voce h2{font-size:17px;margin:0 0 8px;color:#1461CC}
		.sab-doc-voce .sab-doc-percorso{font-size:12px;color:#999;font-style:italic;margin-bottom:10px}
		.sab-doc-voce p{font-size:13.5px;line-height:1.65;color:#333;max-width:680px;margin:0 0 10px}
		.sab-doc-voce p strong{color:#111}
		.sab-doc-live{background:#fafbfc;border:1px solid #e8e8e8;border-radius:12px;padding:22px;margin:14px 0 18px;max-width:680px}
		.sab-doc-live-label{font-size:10.5px;text-transform:uppercase;letter-spacing:.05em;color:#aaa;font-weight:700;margin-bottom:14px}
		.sab-doc-tip{background:#eff6ff;border:1px solid #bfdbfe;color:#1e3a5f;font-size:13px;padding:12px 16px;border-radius:8px;margin:10px 0;max-width:680px;line-height:1.55}
		.sab-doc-warn{background:#fffbeb;border:1px solid #fde68a;color:#78350f;font-size:13px;padding:12px 16px;border-radius:8px;margin:10px 0;max-width:680px;line-height:1.55}
		.sabdoc-toggle{position:relative;display:inline-block;width:42px;height:24px;flex-shrink:0}
		.sabdoc-toggle input{opacity:0;width:0;height:0;position:absolute}
		.sabdoc-toggle-slider{position:absolute;cursor:pointer;top:0;left:0;right:0;bottom:0;background:#d5d5d5;border-radius:24px;transition:background .2s}
		.sabdoc-toggle-slider:before{position:absolute;content:"";height:18px;width:18px;left:3px;top:3px;background:#fff;border-radius:50%;transition:transform .2s;box-shadow:0 1px 3px rgba(0,0,0,.25)}
		.sabdoc-toggle input:checked + .sabdoc-toggle-slider{background:#1461CC}
		.sabdoc-toggle input:checked + .sabdoc-toggle-slider:before{transform:translateX(18px)}
		.sabdoc-toggle-row{display:flex;align-items:center;justify-content:space-between;gap:16px;padding:14px 16px;background:#fff;border-radius:10px;border:1px solid #eee}
		.sabdoc-chat-frame{background:#f0f2f5;border-radius:14px;padding:16px;max-width:340px}
		.sabdoc-chat-bubble-bot{background:#fff;border-radius:14px 14px 14px 3px;padding:10px 14px;font-size:12.5px;color:#222;margin-bottom:8px;max-width:80%;box-shadow:0 1px 2px rgba(0,0,0,.06)}
		.sabdoc-chat-bubble-user{background:#1461CC;color:#fff;border-radius:14px 14px 3px 14px;padding:10px 14px;font-size:12.5px;margin-bottom:8px;max-width:80%;margin-left:auto}
		.sabdoc-badge-verde{display:inline-flex;align-items:center;gap:5px;background:#e8f8ee;color:#1a7d3a;font-size:12px;font-weight:700;padding:4px 11px;border-radius:20px}
		.sabdoc-badge-rossa{display:inline-flex;align-items:center;gap:5px;background:#fdeeee;color:#b91c1c;font-size:12px;font-weight:700;padding:4px 11px;border-radius:20px}
		.sabdoc-color-swatch{display:inline-block;width:34px;height:34px;border-radius:8px;border:2px solid #fff;box-shadow:0 0 0 1.5px #ddd;vertical-align:middle;margin-right:8px}
		.sabdoc-pill-pro{display:inline-flex;align-items:center;justify-content:center;gap:4px;background:#1461CC;color:#fff;font-size:10px;font-weight:800;padding:5px 11px;border-radius:20px}
		.sab-doc-banner{background:linear-gradient(135deg,#0d0d0d 0%,#1a1a2e 100%);border-radius:14px;padding:36px 40px;margin-bottom:6px;position:relative;overflow:hidden}
		.sab-doc-banner:before{content:"";position:absolute;top:-40px;right:-40px;width:220px;height:220px;border-radius:50%;background:radial-gradient(circle,rgba(20,97,204,.35) 0%,transparent 70%)}
		.sab-doc-banner-badge{display:inline-flex;align-items:center;gap:8px;background:rgba(255,255,255,.1);color:#9CC3F0;font-size:12px;font-weight:700;padding:6px 14px;border-radius:20px;margin-bottom:14px;letter-spacing:.02em;line-height:1}
		.sab-doc-banner h1{color:#fff;font-size:32px;margin:0 0 8px;font-weight:800}
		.sab-doc-banner p{color:#cbd5e1;font-size:14px;margin:0;max-width:560px;line-height:1.6}
		.sab-doc-lang-switch{position:absolute;top:32px;right:36px}
		.sab-doc-lang-btn{display:flex;align-items:center;gap:8px;background:rgba(255,255,255,.12);color:#fff;border:1px solid rgba(255,255,255,.2);border-radius:8px;padding:8px 12px;font-size:12.5px;cursor:pointer;font-family:inherit}
		.sab-doc-lang-panel{position:absolute;top:calc(100% + 6px);right:0;background:#fff;border-radius:10px;box-shadow:0 8px 26px rgba(0,0,0,.18);padding:6px;min-width:190px;max-height:280px;overflow-y:auto;z-index:50}
		.sab-doc-lang-panel a{display:flex;align-items:center;gap:9px;padding:8px 10px;border-radius:7px;color:#222;text-decoration:none;font-size:13px}
		.sab-doc-lang-panel a:hover{background:#f0f5ff}
		';
	}

	private function live_motori_ai() {
		if ( ! class_exists( 'Sabriel_Module_AI' ) ) {
			return '';
		}
		$attivo = Sabriel_Module_AI::provider_attivo();
		$html   = '<div style="display:flex;gap:10px;flex-wrap:wrap">';
		foreach ( Sabriel_Module_AI::PROVIDERS as $slug => $info ) {
			$e_attivo = ( $slug === $attivo );
			$grezza   = (string) get_option( $info[1], '' );
			$leggibile = '' !== $grezza && class_exists( 'Sabriel_Crypto' ) && '' !== (string) Sabriel_Crypto::decrypt( $grezza );
			if ( $leggibile ) {
				$stato = __( 'Key saved', 'sabriel-ai' );
			} elseif ( '' !== $grezza ) {
				$stato = __( 'Key unreadable', 'sabriel-ai' );
			} else {
				$stato = __( 'No key', 'sabriel-ai' );
			}
			$html .= '<div style="flex:1;min-width:150px;border:' . ( $e_attivo ? '2px solid #1461CC;background:#f0f5ff' : '1.5px solid #e0e0e0' ) . ';border-radius:10px;padding:12px;text-align:center">'
				. '<div style="font-weight:700;font-size:13px;color:' . ( $e_attivo ? '#111' : '#999' ) . '">' . esc_html( $info[0] ) . '</div>'
				. ( $e_attivo ? '<div style="font-size:11px;color:#1461CC;margin-top:4px">&#10003; ' . esc_html__( 'active', 'sabriel-ai' ) . '</div>' : '' )
				. '<div style="font-size:10.5px;color:#888;margin-top:4px">' . esc_html( $stato ) . '</div>'
				. '</div>';
		}
		return $html . '</div>';
	}

	private function live_toggle( $etichetta, $acceso = true ) {
		return '<div class="sabdoc-toggle-row"><span style="font-size:13.5px;font-weight:600;color:#111">' . esc_html( $etichetta ) . '</span><span class="sabdoc-toggle"><input type="checkbox" ' . ( $acceso ? 'checked' : '' ) . ' disabled><span class="sabdoc-toggle-slider"></span></span></div>';
	}

	private function live_chat_esempio( $msg_bot, $msg_utente ) {
		return '<div class="sabdoc-chat-frame"><div class="sabdoc-chat-bubble-bot">' . esc_html( $msg_bot ) . '</div><div class="sabdoc-chat-bubble-user">' . esc_html( $msg_utente ) . '</div></div>';
	}

	private function live_colori( $colori ) {
		$html = '<div>';
		foreach ( $colori as $nome => $hex ) {
			$html .= '<div style="display:inline-block;text-align:center;margin-right:18px"><span class="sabdoc-color-swatch" style="background:' . esc_attr( $hex ) . '"></span><div style="font-size:11px;color:#888;margin-top:4px">' . esc_html( $nome ) . '</div></div>';
		}
		return $html . '</div>';
	}

	private function con_lingua( $loc, $render ) {
		global $l10n;
		$originale = isset( $l10n['sabriel-ai'] ) ? $l10n['sabriel-ai'] : null;
		$switched  = ( $loc && $loc !== determine_locale() ) ? switch_to_locale( $loc ) : false;

		if ( ! class_exists( 'MO' ) ) {
			require_once ABSPATH . WPINC . '/pomo/mo.php';
		}
		$mo = new MO();
		if ( 'en_US' !== $loc ) {
			$mofile = SABRIEL_AI_DIR . 'languages/sabriel-ai-' . $loc . '.mo';
			if ( is_readable( $mofile ) ) {
				$mo->import_from_file( $mofile );
			}
		}

		$l10n['sabriel-ai'] = $mo; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- si sostituisce il solo dizionario di questo plugin e viene rimesso a posto poche righe piu' sotto.

		$render();

		if ( null !== $originale ) {
			$l10n['sabriel-ai'] = $originale; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- ripristino del dizionario originale.
		} else {
			unset( $l10n['sabriel-ai'] );
		}
		if ( $switched ) {
			restore_previous_locale();
		}
	}

	private function genera_export_html( $loc ) {
		$this->in_export = true;

		ob_start();
		$this->con_lingua( $loc, function () { $this->render_indice(); } );
		$indice = ob_get_clean();

		ob_start();
		$this->con_lingua( $loc, function () {
			$this->sezione_avvio_guidato();
			$this->sezione_motore_ai();
			$this->sezione_personalizzazione();
			$this->sezione_generali();
			$this->sezione_listino();
			$this->sezione_comportamento();
			$this->sezione_impostazioni_pagine();

			$this->sezione_dashboard();
			$this->sezione_stato_sistema();
			$this->sezione_privacy();

			$this->sezione_premium();
		} );
		$corpo = ob_get_clean();

		ob_start();
		$this->con_lingua( $loc, function () {

			echo '<span data-h="t">' . __( 'Documentation', 'sabriel-ai' ) . '</span>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- protetto con esc_html() al momento della stampa, vedi sopra.
			echo '<span data-h="s">' . __( 'Every setting of the plugin, explained: what it really does and how to configure it best.', 'sabriel-ai' ) . '</span>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- idem.
		} );
		$testi = ob_get_clean();
		$titolo = '';
		$sotto  = '';
		if ( preg_match( '/data-h="t">(.*?)<\/span>/s', $testi, $m ) ) { $titolo = $m[1]; }
		if ( preg_match( '/data-h="s">(.*?)<\/span>/s', $testi, $m ) ) { $sotto = $m[1]; }

		$css       = $this->css_condiviso();
		$lang_attr = esc_attr( substr( $loc, 0, 2 ) );

		$html  = '<!DOCTYPE html>' . "\n";
		$html .= '<html lang="' . $lang_attr . '">' . "\n<head>\n";
		$html .= '<meta charset="utf-8">' . "\n";
		$html .= '<meta name="viewport" content="width=device-width, initial-scale=1">' . "\n";
		$html .= '<title>' . esc_html( $titolo ) . ' | Sabriel AI</title>' . "\n";
		$html .= '<style>' . "\n";
		$html .= 'body{margin:0;background:#f6f7f9;color:#1a1a1a;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;line-height:1.6}' . "\n";
		$html .= '.sab-doc-page{max-width:1400px;margin:0 auto;padding:28px 28px 80px}' . "\n";
		$html .= $css . "\n";

		$html .= '[data-doc-nav-lang]{display:block !important}[data-doc-lang]{display:block !important}' . "\n";
		$html .= '</style>' . "\n</head>\n<body>\n";
		$html .= '<div class="sab-doc-page">' . "\n";
		$html .= '<div class="sab-doc-banner"><div class="sab-doc-banner-badge"><span>Sabriel AI</span></div>';
		$html .= '<h1>' . esc_html( $titolo ) . '</h1><p>' . esc_html( $sotto ) . '</p></div>' . "\n";

		$html .= '<div class="sab-doc-wrap"><div class="sab-doc-nav-col">' . $indice . '</div>';
		$html .= '<div class="sab-doc-main">' . $corpo . '</div></div>' . "\n";
		$html .= '</div>' . "\n</body>\n</html>";

		return $html;
	}

	public function render_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'Access denied', 'sabriel-ai' ) );
		}

		$lingue_disponibili = Sabriel_Module_Settings::lingue_locale_bandiera();
		$locale_corrente = get_user_locale();
		if ( ! isset( $lingue_disponibili[ $locale_corrente ] ) ) {

			$locale_corrente = 'en_US';
		}
		?>
		<div class="wrap">
		<style><?php echo $this->css_condiviso(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- HTML costruito nel plugin: le parti variabili sono gia' protette dentro il metodo. ?></style>
		<div class="sab-doc-banner-wrap" style="position:relative">
		<div class="sab-doc-banner">
			<div class="sab-doc-banner-badge"><?php echo Sabriel_Base::icona_libro(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- HTML costruito nel plugin: le parti variabili sono gia' protette dentro il metodo. ?><span>Sabriel AI</span></div>
			<h1><?php esc_html_e( 'Documentation', 'sabriel-ai' ); ?></h1>
			<p><?php esc_html_e( 'Every setting of the plugin, explained: what it really does and how to configure it best.', 'sabriel-ai' ); ?></p>
		</div>
		</div>
		<p style="color:#888;font-size:12.5px;max-width:680px;margin:14px 0 20px">💡 <?php esc_html_e( 'Click an item in the index to jump straight to that point.', 'sabriel-ai' ); ?></p>
		<div style="display:flex;align-items:center;gap:10px;flex-wrap:wrap;background:#fff;border:1px solid #e5e5e5;border-radius:10px;padding:12px 16px;margin:0 0 22px;max-width:680px">
			<span style="font-size:12.5px;color:#555">📄 <?php esc_html_e( 'Export this documentation as an HTML page (with the pricing table at the bottom) to publish on your own site:', 'sabriel-ai' ); ?></span>
			<form method="get" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="display:flex;align-items:center;gap:8px;margin:0">
				<input type="hidden" name="action" value="sabriel_ai_export_doc">
				<?php wp_nonce_field( 'sabriel_ai_export_doc' ); ?>
				<?php

				?>
				<span id="sab-doc-loc-bandiera" style="flex-shrink:0;line-height:0"><?php echo Sabriel_Base::bandiera_svg( $locale_corrente ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- HTML costruito nel plugin: le parti variabili sono gia' protette dentro il metodo. ?></span>
				<select name="loc" id="sab-doc-loc-select" style="padding:7px 10px;border:1px solid #ccc;border-radius:7px;font-size:12.5px">
					<?php foreach ( $lingue_disponibili as $loc => $info ) : ?>
					<option value="<?php echo esc_attr( $loc ); ?>" <?php selected( $loc, $locale_corrente ); ?>><?php echo esc_html( $info[0] ); ?></option>
					<?php endforeach; ?>
				</select>
				<script>
				(function(){
					var mappaDocB = {
						<?php foreach ( array_keys( $lingue_disponibili ) as $sab_doc_cod ) : ?>
						'<?php echo esc_js( $sab_doc_cod ); ?>': <?php echo wp_json_encode( Sabriel_Base::bandiera_svg( $sab_doc_cod ) ); ?>,
						<?php endforeach; ?>
					};
					var b = document.getElementById('sab-doc-loc-bandiera'), s = document.getElementById('sab-doc-loc-select');
					if (b && s) { s.addEventListener('change', function(){ if (mappaDocB[s.value]) b.innerHTML = mappaDocB[s.value]; }); }
				})();
				</script>
				<button type="submit" class="button button-primary" style="font-size:12.5px"><?php esc_html_e( 'Download HTML', 'sabriel-ai' ); ?></button>
			</form>
		</div>
		<div class="sab-doc-wrap">
			<div class="sab-doc-nav-col">
			<?php

			echo '<div data-doc-nav-lang="' . esc_attr( $locale_corrente ) . '">';
			$this->con_lingua( $locale_corrente, function () { $this->render_indice(); } );
			echo '</div>';
			?>
			</div>
			<div class="sab-doc-main">
				<?php
					echo '<div data-doc-lang="' . esc_attr( $locale_corrente ) . '">';
					$this->con_lingua( $locale_corrente, function () {
						$this->sezione_avvio_guidato();
						$this->sezione_motore_ai();
						$this->sezione_personalizzazione();
						$this->sezione_generali();
			$this->sezione_listino();
			$this->sezione_comportamento();
						$this->sezione_impostazioni_pagine();

						$this->sezione_dashboard();
						$this->sezione_stato_sistema();
						$this->sezione_privacy();
						$this->sezione_premium();
					} );
					echo '</div>';
				?>
			</div>
		</div>
		</div>
		<script>
		(function(){
			document.querySelectorAll('[data-doc-target]').forEach(function(link){
				link.addEventListener('click', function(e){
					e.preventDefault();
					var id = link.getAttribute('data-doc-target');
					var contenitore = document.querySelector('[data-doc-lang]');
					if (!contenitore) return;
					var bersaglio = contenitore.querySelector('[id="' + id + '"]');
					if (bersaglio) bersaglio.scrollIntoView({ behavior: 'smooth', block: 'start' });
				});
			});
		})();
		</script>
		<?php
	}

	private function render_indice() {
		$voci = [
			'avvio-guidato'   => [ __( 'Setup wizard', 'sabriel-ai' ), [] ],
			'motore-ai'       => [ __( 'AI Engine', 'sabriel-ai' ), [ 'motore-modello' => __( 'Model', 'sabriel-ai' ), 'motore-gemini' => 'Google Gemini', 'motore-openai' => 'ChatGPT (OpenAI)', 'motore-claude' => 'Claude (Anthropic)' ] ],
			'personalizzazione' => [ __( 'Customization', 'sabriel-ai' ), [ 'pers-identita' => __( 'Identity', 'sabriel-ai' ), 'pers-credito' => __( 'Credit in the chat', 'sabriel-ai' ), 'pers-colori' => __( 'Colors', 'sabriel-ai' ) ] ],
			'generali'        => [ __( 'Settings → General', 'sabriel-ai' ), [] ],
			'listino'         => [ __( 'Settings → Service price list', 'sabriel-ai' ), [] ],
			'comportamento'   => [ __( 'Behavior', 'sabriel-ai' ), [ 'comp-tono' => __( 'Tone and behavior', 'sabriel-ai' ), 'comp-riepilogo' => __( 'When the chat summary is sent', 'sabriel-ai' ), 'comp-cronologia' => __( 'Conversation history', 'sabriel-ai' ), 'comp-lingua' => __( 'Bot language', 'sabriel-ai' ), 'comp-orari' => __( 'Opening hours', 'sabriel-ai' ), 'comp-pagamento' => __( 'Payment structure', 'sabriel-ai' ), 'comp-valuta' => __( 'Currency', 'sabriel-ai' ) ] ],
			'pagine'          => [ __( 'Settings → Pages', 'sabriel-ai' ), [ 'pagine-link' => __( 'Page links', 'sabriel-ai' ) ] ],
			'dashboard'       => [ __( 'Dashboard', 'sabriel-ai' ), [] ],
			'stato-sistema'   => [ __( 'System status', 'sabriel-ai' ), [] ],
			'privacy'         => [ __( 'Privacy and data', 'sabriel-ai' ), [ 'privacy-uscita' => __( 'What leaves your site', 'sabriel-ai' ), 'privacy-conservazione' => __( 'What stays saved', 'sabriel-ai' ), 'priv-statistiche' => __( 'Collect visit statistics', 'sabriel-ai' ), 'privacy-limiti' => __( 'Limits worth knowing', 'sabriel-ai' ) ] ],
		];

		$voci['premium'] = [ __( 'Complete edition', 'sabriel-ai' ), [] ];

		echo '<div class="sab-doc-nav">';
		foreach ( $voci as $id => $dati ) {
			echo '<a href="#' . esc_attr( $id ) . '" data-doc-target="' . esc_attr( $id ) . '" class="sab-doc-nav-sezione">' . esc_html( $dati[0] ) . '</a>';
			foreach ( $dati[1] as $sub_id => $sub_nome ) {
				echo '<a href="#' . esc_attr( $sub_id ) . '" data-doc-target="' . esc_attr( $sub_id ) . '" class="sab-doc-nav-sotto">' . esc_html( $sub_nome ) . '</a>';
			}
		}
		echo '</div>';
	}

	private function sezione_avvio_guidato() {
		?>
		<div class="sab-doc-sezione" id="avvio-guidato">
			<h1><?php echo Sabriel_Base::icona( 'razzo', 26 ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- SVG statico scritto nel plugin. ?> <?php esc_html_e( 'Setup wizard', 'sabriel-ai' ); ?></h1>
			<p class="sab-doc-intro"><?php esc_html_e( 'The first screen you see after activating the plugin. It exists for a specific reason: the most important fields (AI engine, language, basic identity) are scattered across different pages of the panel. Without a guide, a first-time administrator would have to discover on their own where each one is. The wizard lines them up, one after another, in the order they\'re really needed. Finishing it isn\'t mandatory: you can skip it at any time (the "Skip" button at the top doesn\'t lose anything you\'ve already written) and configure everything later from the normal pages, where exactly the same fields live: the wizard isn\'t a "special" place, it\'s just the same configuration presented in a more convenient order the first time.', 'sabriel-ai' ); ?></p>
			<div class="sab-doc-voce">
				<h2><?php esc_html_e( 'Bot language', 'sabriel-ai' ); ?></h2>
				<p class="sab-doc-percorso"><?php echo esc_html( 'Sabriel AI' . ' → ' . __( 'Setup wizard', 'sabriel-ai' ) ); ?></p>
				<p><strong><?php esc_html_e( 'What it does:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Which language the bot replies in to clients in the chat. This is not the language of the panel you are looking at: they are two separate things and they never affect one another. You choose the panel\'s language in the dropdown provided (under Settings, General tab): as long as «Same as the site» is set there, the panel follows the WordPress language; if instead you pick a language of your own, from that moment the panel stays on it even when the site language changes, until you set «Same as the site» again.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Why this distinction exists:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Whoever installs the plugin and whoever uses it to talk to the bot are almost always different people: you configure the site in Italian, but the customers writing in chat could be Spanish, English, any nationality. If the panel and the bot shared the same language, changing one would change the other without you wanting it: that\'s why they\'re two completely separate settings, even though they resemble each other.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'How to configure it best:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Choose your customers\' main language. If your audience writes in multiple languages, don\'t worry: by default the bot always replies in the language the customer uses to write, whatever it is. This choice only serves as a starting point for the first greeting, even before the customer has written anything the bot can use to understand the right language.', 'sabriel-ai' ); ?></p>
			</div>
		</div>
		<?php
	}

	private function sezione_motore_ai() {
		?>
		<div class="sab-doc-sezione" id="motore-ai">
			<h1><?php echo Sabriel_Base::icona( 'robot', 26 ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- SVG statico scritto nel plugin. ?> <?php esc_html_e( 'AI Engine', 'sabriel-ai' ); ?></h1>
			<p class="sab-doc-intro"><?php esc_html_e( 'Here you choose which artificial intelligence the bot will use to understand and reply to customers, and enter the key that connects it to your account. This is probably the first page to actually configure: without a valid key, the bot can\'t respond to anyone.', 'sabriel-ai' ); ?></p>
			<div class="sab-doc-tip">💡 <?php esc_html_e( 'Why you need YOUR OWN key and not one already included in the plugin: every conversation the bot processes has a real cost, paid to the chosen AI service (Google, OpenAI or Anthropic), based on how much is written and read. If the plugin included a shared key, that cost would fall on whoever created it, for every site using it: unsustainable. With your own key, the cost stays yours and under your direct control: you see the invoices, set spending limits, change engine whenever you want, never going through any intermediary.', 'sabriel-ai' ); ?></div>
			<div class="sab-doc-voce">
				<h2><?php esc_html_e( 'Engine choice', 'sabriel-ai' ); ?></h2>
				<p class="sab-doc-percorso"><?php echo esc_html( 'Sabriel AI' . ' → ' . __( 'AI Engine', 'sabriel-ai' ) ); ?></p>
				<p><strong><?php esc_html_e( 'What it does:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Determines which AI service processes every customer message. You can change it at any time, even with conversations already in progress.', 'sabriel-ai' ); ?></p>
				<div class="sab-doc-live">
					<div class="sab-doc-live-label"><?php esc_html_e( 'How you see it in the panel', 'sabriel-ai' ); ?></div>
					<?php echo $this->live_motori_ai(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- HTML costruito nel plugin: le parti variabili sono gia' protette dentro il metodo. ?>
				</div>
				<p><strong><?php esc_html_e( 'Why changing engine doesn\'t break anything:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'The chosen engine affects ONLY how the bot processes the NEXT message: your configuration, texts and already recorded data stay exactly where they were. You can try an engine for a week, switch it, and no customer data is touched: it is a switch on "who thinks", not on "what has already been saved".', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'How to configure it best:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Google Gemini is the cheapest choice and has a generous free tier, good for starting without spending anything at first. ChatGPT and Claude are excellent alternatives if you already have a subscription or a personal preference: the quality of responses for the type of conversations this bot handles (commercial chat, not complex creative tasks) is comparable across all three.', 'sabriel-ai' ); ?></p>
			</div>
			<div class="sab-doc-voce" id="motore-modello">
				<h2><?php esc_html_e( 'Model choice', 'sabriel-ai' ); ?></h2>
				<p class="sab-doc-percorso"><?php echo esc_html( 'Sabriel AI' . ' → ' . __( 'AI Engine', 'sabriel-ai' ) ); ?></p>
				<p><strong><?php esc_html_e( 'What it does:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Each engine (Gemini, ChatGPT, Claude) offers several models, from the cheapest and fastest to the most powerful. Here you choose which model the active engine uses. Each option shows the indicative price per million tokens, that is how much processing the messages costs, so you can decide by budget, and the recommended one is already selected by default.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Which to choose:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'For a commercial chat like this one, the recommended, already preselected model is almost always the best choice: it is the cheapest one that handles this kind of conversation well. Move up to a more powerful model only if you notice weak replies; moving down is pointless, since the recommended one is already the most sensible, cost-effective tier.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Custom:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'If the AI service releases a new model not yet in the list, you can type its exact name by hand using the custom option. The prices shown are verified as of the date indicated in the panel and may change over time: the official price list of the AI service, linked next to it, always prevails.', 'sabriel-ai' ); ?></p>
			</div>
			<div class="sab-doc-voce" id="motore-gemini">
				<h2><?php esc_html_e( 'How to get a Google Gemini key (free)', 'sabriel-ai' ); ?></h2>
				<p>1. <?php esc_html_e( 'Go to aistudio.google.com and sign in with a Google account.', 'sabriel-ai' ); ?></p>
				<p>2. <?php esc_html_e( 'Click "Get API key", then "Create API key".', 'sabriel-ai' ); ?></p>
				<p>3. <?php esc_html_e( 'Copy the generated key and paste it into the panel field. New keys begin with "AQ."; older ones begin with "AIza" and Google stops accepting them in September 2026, so if yours is an old one create a new key now.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Good to know:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Google\'s free tier has a limit on requests per minute and per day: plenty for a bot on a site with normal traffic, but if your site gets an unusual wave of visitors one day, you might temporarily exhaust it. Google always shows current usage on the same page where you created the key.', 'sabriel-ai' ); ?></p>
			</div>
			<div class="sab-doc-voce" id="motore-openai">
				<h2><?php esc_html_e( 'How to get an OpenAI (ChatGPT) key', 'sabriel-ai' ); ?></h2>
				<p>1. <?php esc_html_e( 'Go to platform.openai.com and create an account (different from the normal ChatGPT account: this is for developers, requires a payment method).', 'sabriel-ai' ); ?></p>
				<p>2. <?php esc_html_e( 'Go to "API keys", click "Create new secret key".', 'sabriel-ai' ); ?></p>
				<p>3. <?php esc_html_e( 'Copy the key right away (starts with "sk-..."): it won\'t be visible anymore after closing that window.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Good to know:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'unlike Gemini, there\'s no free tier here: every conversation has a cost, charged to the linked payment method. OpenAI lets you set a maximum monthly spending cap in your account\'s billing settings, useful as a safety net.', 'sabriel-ai' ); ?></p>
			</div>
			<div class="sab-doc-voce" id="motore-claude">
				<h2><?php esc_html_e( 'How to get an Anthropic (Claude) key', 'sabriel-ai' ); ?></h2>
				<p>1. <?php esc_html_e( 'Go to console.anthropic.com and create an account (requires a payment method).', 'sabriel-ai' ); ?></p>
				<p>2. <?php esc_html_e( 'Go to "API Keys", click "Create Key".', 'sabriel-ai' ); ?></p>
				<p>3. <?php esc_html_e( 'Copy the key (starts with "sk-ant-...") and paste it into the panel.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Good to know:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'like OpenAI, requires a payment method and doesn\'t have a permanent free tier (they sometimes offer trial credits for new accounts). Here too you can set spending limits from the console.', 'sabriel-ai' ); ?></p>
				<div class="sab-doc-warn">⚠️ <?php esc_html_e( 'When the server allows it (almost always) every key is encrypted before being saved in the database, and not even by looking inside WordPress or the database can it be read in clear. If your server is missing the PHP OpenSSL extension, encryption is not possible: the System status page tells you so plainly, in the «Key encryption available» row. In any case, do not share the key with anyone: it is tied to your account and to your usage costs, exactly like a password.', 'sabriel-ai' ); ?></div>
			</div>
		</div>
		<?php
	}

	private function sezione_personalizzazione() {
		?>
		<div class="sab-doc-sezione" id="personalizzazione">
			<h1><?php echo Sabriel_Base::icona( 'tavolozza', 26 ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- SVG statico scritto nel plugin. ?> <?php esc_html_e( 'Customization', 'sabriel-ai' ); ?></h1>
			<p class="sab-doc-intro"><?php esc_html_e( 'The look of the chat widget: everything the customer sees visually, unlike Behaviour (the next section), which is about what the bot SAYS, not how it looks.', 'sabriel-ai' ); ?></p>
			<div class="sab-doc-voce" id="pers-identita">
				<h2><?php esc_html_e( 'Identity: Bot name and Logo', 'sabriel-ai' ); ?></h2>
				<p class="sab-doc-percorso"><?php echo esc_html( 'Sabriel AI' . ' → ' . __( 'Customization', 'sabriel-ai' ) ); ?></p>
				<p><strong><?php esc_html_e( 'What it does:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'The name the bot uses to introduce itself and the logo shown in the chat header and as the button icon.', 'sabriel-ai' ); ?></p>
				<div class="sab-doc-live">
					<div class="sab-doc-live-label"><?php esc_html_e( 'How your customers see it', 'sabriel-ai' ); ?></div>
					<?php

					$sab_doc_bot   = trim( (string) Sabriel_Module_Branding::get( 'bot_name' ) );
					$sab_doc_ditta = trim( (string) Sabriel_Module_Settings::get( 'business_name' ) );
					if ( '' !== $sab_doc_bot && '' !== $sab_doc_ditta ) {
						$sab_doc_saluto = sprintf(
							/* translators: 1: bot name; 2: business name */
							__( 'Hi! I\'m %1$s, the digital assistant of %2$s. How can I help you?', 'sabriel-ai' ),
							$sab_doc_bot,
							$sab_doc_ditta
						);
					} else {
						$sab_doc_saluto = __( 'Hi! I\'m Luna, the digital assistant for Fiori & Co. How can I help you?', 'sabriel-ai' );
					}
					echo $this->live_chat_esempio( $sab_doc_saluto, __( 'I\'d like some information', 'sabriel-ai' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- HTML costruito nel plugin: le parti variabili sono gia' protette dentro il metodo.
					?>
				</div>
				<p><strong><?php esc_html_e( 'Why a proper name instead of "Virtual assistant":', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'A name gives the customer something to address ("Hi Luna, I have a question") instead of talking to an anonymous entity: a small difference, but in the writer\'s experience it changes the tone of the whole conversation, making it feel more like writing to a person at the company than filling out a form.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'How to configure it best:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'A short, easy-to-say name. A square, simple logo that stays readable even very small: the chat button icon is tiny, a logo with too much detail or small text gets completely lost at that size.', 'sabriel-ai' ); ?></p>
			</div>
			<div class="sab-doc-voce" id="pers-credito">
				<h2><?php esc_html_e( 'The "Powered by Sabriel AI" credit', 'sabriel-ai' ); ?></h2>
				<p class="sab-doc-percorso"><?php echo esc_html( 'Sabriel AI' . ' \u{2192} ' . __( 'Customization', 'sabriel-ai' ) ); ?></p>
				<p><strong><?php esc_html_e( 'What it does:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Adds one small line under the chat window, linking to the plugin author. It is off when you install the plugin and stays off until you turn it on yourself: nothing of ours appears on your site by default.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Why it is off and not on:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'The WordPress.org rules require every credit and every author link inside a plugin to be optional and hidden by default on the public site. So this is your decision, not ours, and the plugin works exactly the same either way.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'When it makes sense to turn it on:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'On your own site, if the plugin is doing its job and the line does not bother you: it is the simplest way to support something that is given away for free. On a site you built for a client, leave it off: there the chat should carry their name, not ours.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Good to know:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Resetting the appearance does not touch this switch: it is a choice you made, not a colour.', 'sabriel-ai' ); ?></p>
			</div>
			<div class="sab-doc-voce" id="pers-colori">
				<h2><?php esc_html_e( 'Colors', 'sabriel-ai' ); ?></h2>
				<p class="sab-doc-percorso"><?php echo esc_html( 'Sabriel AI' . ' → ' . __( 'Customization', 'sabriel-ai' ) ); ?></p>
				<p><strong><?php esc_html_e( 'What it does:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'The primary color tints the chat header, the button, and the bot\'s messages. The secondary is used for accents and details.', 'sabriel-ai' ); ?></p>
				<div class="sab-doc-live">
					<div class="sab-doc-live-label"><?php esc_html_e( 'How you see it in the panel', 'sabriel-ai' ); ?></div>
					<?php

					$sab_colori_doc = [
						__( 'Primary', 'sabriel-ai' )  => Sabriel_Module_Branding::get( 'primary_color' ),
						__( 'Secondary', 'sabriel-ai' ) => Sabriel_Module_Branding::get( 'secondary_color' ),
					];
					echo $this->live_colori( $sab_colori_doc ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- HTML costruito nel plugin: le parti variabili sono gia' protette dentro il metodo.
					?>
				</div>
				<p><strong><?php esc_html_e( 'How to configure it best:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Use your brand colors: it\'s the first thing a visitor notices about the widget. The preview updates live as you change them, so try different combinations before saving. Avoid colors that are too light for the main one: the bot messages\' white text must remain readable over it.', 'sabriel-ai' ); ?></p>
			</div>
		</div>
		<?php
	}

	private function sezione_generali() {
		?>
		<div class="sab-doc-sezione" id="generali">
			<h1><?php echo Sabriel_Base::icona( 'etichetta', 26 ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- SVG statico scritto nel plugin. ?> <?php esc_html_e( 'Settings → General', 'sabriel-ai' ); ?></h1>
			<p class="sab-doc-intro"><?php esc_html_e( 'Your business details. These are not decoration: the bot reads them on every reply and uses them to introduce itself and to say how you can be reached.', 'sabriel-ai' ); ?></p>
			<div class="sab-doc-voce">
				<h2><?php esc_html_e( 'Who you are and how to reach you', 'sabriel-ai' ); ?></h2>
				<p class="sab-doc-percorso"><?php echo esc_html( 'Sabriel AI' . ' → ' . __( 'Settings', 'sabriel-ai' ) . ' → ' . __( 'General', 'sabriel-ai' ) ); ?></p>
				<p><strong><?php esc_html_e( 'What it does:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Business name, owner\'s name, tagline, support email, phone, WhatsApp number and website. The bot uses them to introduce itself and to tell the customer how to reach you: email and phone when someone asks for a contact, the WhatsApp number in the button the visitor sees when the bot hands over to a person.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Good to know:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'The WhatsApp number must be written with the international prefix and without the plus sign: that is the format WhatsApp expects, and with the plus the button leads to a non-existent number. The phone, instead, is written out in full, as on a business card: that one is only meant to be read.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'How to set it up best:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Fill in at least the business name, the support email and the website: those three end up everywhere. If you leave the support email empty the plugin uses the WordPress administrator\'s one, which is often not the address you want customers to write to.', 'sabriel-ai' ); ?></p>
			</div>
			<div class="sab-doc-voce">
				<h2><?php esc_html_e( 'Address, VAT number, year founded, service area', 'sabriel-ai' ); ?></h2>
				<p class="sab-doc-percorso"><?php echo esc_html( 'Sabriel AI' . ' → ' . __( 'Settings', 'sabriel-ai' ) . ' → ' . __( 'General', 'sabriel-ai' ) ); ?></p>
				<p><strong><?php esc_html_e( 'What it does:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'All optional. Address and VAT number let the bot answer when someone asks where you are or who they are dealing with. Founding year and service area let the bot answer questions like "how long have you been working?" or "do you work outside the area?" without making anything up.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'How to set it up best:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'If you only work remotely, write it in the service area instead of leaving it empty: the bot stops answering vaguely about the area and says it plainly. If you have no VAT number, leave the field empty: the footer closes up by itself with no gaps.', 'sabriel-ai' ); ?></p>
			</div>
		</div>
		<?php
	}

	private function sezione_listino() {
		?>
		<div class="sab-doc-sezione" id="listino">
			<h1><?php echo Sabriel_Base::icona( 'lista', 26 ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- SVG statico scritto nel plugin. ?> <?php esc_html_e( 'Settings → Service price list', 'sabriel-ai' ); ?></h1>
			<p class="sab-doc-intro"><?php esc_html_e( 'The list of what you sell, with prices. This is the tab that decides the quality of the answers about cost: the bot does not invent figures, it takes these.', 'sabriel-ai' ); ?></p>
			<div class="sab-doc-voce">
				<h2><?php esc_html_e( 'The price list rows', 'sabriel-ai' ); ?></h2>
				<p class="sab-doc-percorso"><?php echo esc_html( 'Sabriel AI' . ' → ' . __( 'Settings', 'sabriel-ai' ) . ' → ' . __( 'Price list', 'sabriel-ai' ) ); ?></p>
				<p><strong><?php esc_html_e( 'What it does:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Each row has a name, a price and a short description. The bot reads them all in every conversation and uses them to answer "how much does it cost?" with your real figures.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Good to know:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'The price is a text field, not a number: you can write "from 900", "1,200 – 1,800", "on request". The bot repeats it exactly as you wrote it. Writing a range is perfectly fine; writing two figures without explaining which is which is not.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'How to set it up best:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'A few clear rows beat twenty rows that look alike: the bot has to be able to tell one service from another. In the description write what is included, not adjectives: that is the part the bot repeats to the customer when asked the difference between two items. If you would rather not show prices in the chat, leave the list empty: the bot says so instead of improvising.', 'sabriel-ai' ); ?></p>
			</div>
		</div>
		<?php
	}

	private function sezione_comportamento() {
		?>
		<div class="sab-doc-sezione" id="comportamento">
			<h1><?php echo Sabriel_Base::icona( 'cervello', 26 ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- SVG statico scritto nel plugin. ?> <?php esc_html_e( 'Behavior', 'sabriel-ai' ); ?></h1>
			<p class="sab-doc-intro"><?php esc_html_e( 'The most important section for the bot\'s "education": here you write how it should talk, not what it sells. Every field in here ends up in the instructions the bot reads with every single response it gives: it\'s the part closest to "how one of your employees would behave on their first day if you yourself explained how to talk to customers".', 'sabriel-ai' ); ?></p>
			<div class="sab-doc-tip">💡 <?php esc_html_e( 'The average customer writes fast and imprecisely. The bot stays impeccable even with the fields below left at their default values: the most delicate behaviours (when to hand over to a human, how to handle awkward questions about prices) are already solidly written into the bot\'s basic operation, BEFORE you write anything here. These fields fine-tune the tone: you are not building safe behaviour from scratch, that already exists; you are adjusting the personality on top of a base that holds anyway.', 'sabriel-ai' ); ?></div>
			<div class="sab-doc-voce" id="comp-riepilogo">
				<h2><?php esc_html_e( 'When the chat summary is sent', 'sabriel-ai' ); ?></h2>
				<p><strong><?php esc_html_e( 'How it works:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'the conversation summary is not emailed only when the visitor closes the chat with the X. It is also sent when they leave the page without closing anything: they change page, go back, close the tab, or — on a phone — switch to another app or turn the screen off. Every send contains the whole conversation from the beginning, so the last one to arrive is always the most complete and the earlier ones can be discarded.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Why there are two routes:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'on a computer the page really does close and the browser gives warning in time. On a phone it does not: the system freezes the page instead of closing it, and that warning never comes. That is why the plugin also listens for the moment the page simply stops being visible, which on a phone always happens. There is nothing to set up: it works on its own.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'What to expect while testing:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'the copy sent to the CUSTOMER is capped at three emails per hour per address, and that cap is deliberate: the address is typed by the visitor and nobody verifies it, so without the cap someone could bombard a person with emails sent from your domain. If you are testing the bot with your own address and nothing arrives on the fourth try within the same hour, it is not a fault: it is that cap. The copy that reaches YOU as the owner has no cap, but it has to be switched on under Settings → Email notifications to you.', 'sabriel-ai' ); ?></p>
			</div>
			<div class="sab-doc-voce" id="comp-cronologia">
				<h2><?php esc_html_e( 'Conversation history', 'sabriel-ai' ); ?></h2>
				<p><strong><?php esc_html_e( 'The text of a conversation is never shortened:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'whatever was written stays there, from the first message to the last. What does have a limit is something else, and there are three separate things that are easy to mistake for a cut.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( '1. After five minutes of silence a new conversation begins.', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'If the visitor stops and picks up again later, the history shows two entries instead of one. Nothing has been lost: they are two conversations.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( '2. The last thirty conversations are kept.', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'When the thirty-first arrives, the oldest one drops out. The cap exists because the history lives in the visitor\'s browser storage, which is small and shared with everything else on the site: without a limit it would fill up and stop saving the very conversation in progress. Thirty conversations are far more than a real visitor ever has.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( '3. The bot remembers the last sixty exchanges.', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'In a very long conversation it may forget how things started, but the text stays written and readable: it is its memory that stops there, not the history.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'If browser storage is full:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'the plugin makes room on its own by discarding the oldest conversations and keeping the one in progress, which is the only one that cannot be recovered. If even that is not enough, the visitor gets a notice in the chat instead of carrying on believing everything is being saved.', 'sabriel-ai' ); ?></p>
			</div>
			<div class="sab-doc-voce" id="comp-tono">
				<h2><?php esc_html_e( 'Bot behavior and tone', 'sabriel-ai' ); ?></h2>
				<p class="sab-doc-percorso"><?php echo esc_html( 'Sabriel AI' . ' → ' . __( 'Settings', 'sabriel-ai' ) . ' → ' . __( 'Behavior', 'sabriel-ai' ) ); ?></p>
				<p><strong><?php esc_html_e( 'What it does:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'The description, written in your own words, of how the bot should talk: how formal it is, what to emphasize, phrases to avoid or use, how to handle specific requests from your industry.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Why the quality of this text matters more than any other field:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'A vague instruction ("be professional") gives the bot nothing specific to YOUR field to base itself on: it stays generic, and sounds like any other virtual assistant. A concrete instruction ("never use the word \'cheap\', let\'s say \'affordable\'; if they ask for a discount, reply that our prices are already calculated to the minimum to guarantee work quality") gives the bot a real distinctive character, that sounds like YOUR business and not like any old chatbot.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'How to configure it best:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Write it as you\'d explain it to a new colleague on their first day of work: simple sentences, no need for technical language. Think of the 3-4 questions you get most often and how you\'d want them handled, write them here explicitly instead of hoping the bot will "figure it out on its own". The presets for business type fill the field with an already-written starting point: start with one close to your field and edit it with your business\'s real details, instead of writing everything from scratch or leaving it generic.', 'sabriel-ai' ); ?></p>
				<div class="sab-doc-tip">💡 <?php esc_html_e( 'The bot works out on its own when a request is not your trade. It tells from what you write here and from your price list: if you run a restaurant and someone asks for a website, it says so kindly and does not treat it as a custom job. You do not have to write «we don\'t do this» yourself.', 'sabriel-ai' ); ?></div>
			</div>
			<div class="sab-doc-voce" id="comp-lingua">
				<h2><?php esc_html_e( 'Bot language', 'sabriel-ai' ); ?></h2>
				<p class="sab-doc-percorso"><?php echo esc_html( 'Sabriel AI' . ' → ' . __( 'Settings', 'sabriel-ai' ) . ' → ' . __( 'Behavior', 'sabriel-ai' ) ); ?></p>
				<p><strong><?php esc_html_e( 'What it does:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'The language your bot speaks to your customers in: the wording of the widget and the first greeting, and — depending on the switch below — the language it replies in during the chat.', 'sabriel-ai' ); ?></p>
				<div class="sab-doc-live">
					<div class="sab-doc-live-label"><?php esc_html_e( 'How you see it in the panel', 'sabriel-ai' ); ?></div>
					<?php

					echo $this->live_toggle( Sabriel_Module_Settings::etichetta_lingua_bot(), true ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- HTML costruito nel plugin: le parti variabili sono gia' protette dentro il metodo.
					?>
				</div>
				<p><strong><?php esc_html_e( 'Why "on" is almost always the right advice:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Imagine a German tourist writing in German to your Italian bot: with this on, the bot replies in German, which is the natural and welcoming experience. If you turn it off, the bot always replies only in the language you chose above, even to those writing in another one.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'How to set it up best:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Leave it on (it is the default): the bot adapts by itself to the customer\'s language, whatever it is, even one that is not among the nine in the list. Turn it off only if you have a specific reason to want replies always in the same language.', 'sabriel-ai' ); ?></p>
				<div class="sab-doc-tip">💡 <?php esc_html_e( 'The language chosen above still counts in both cases: it always decides the language of the buttons and of the widget\'s wording, and the language the assistant greets in first. The switch only concerns which language the assistant replies in.', 'sabriel-ai' ); ?></div>
				<div class="sab-doc-tip">💡 <?php esc_html_e( 'With the switch on, the bot follows the customer\'s language from their very first message, even if it is just a «hello». The language chosen above stays as the fallback and is used only when the message makes it impossible to tell which language it is written in (digits only, emoji only, or mixed languages): never a random one. With the switch off, a customer writing in another language is not enough to make the bot switch: they have to ask.', 'sabriel-ai' ); ?></div>
			</div>
			<div class="sab-doc-voce" id="comp-orari">
				<h2><?php esc_html_e( 'Opening hours', 'sabriel-ai' ); ?></h2>
				<p class="sab-doc-percorso"><?php echo esc_html( 'Sabriel AI' . ' → ' . __( 'Settings', 'sabriel-ai' ) . ' → ' . __( 'Behavior', 'sabriel-ai' ) ); ?></p>
				<p><strong><?php esc_html_e( 'What it does:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'They let the bot know, in ANY conversation, whether the business is open or closed right now, and when it reopens. If you say you are closed on Tuesdays and a customer writes on a Tuesday, the bot says so and offers the first available day instead of letting them believe someone will reply straight away.', 'sabriel-ai' ); ?></p>
				<div class="sab-doc-live">
					<div class="sab-doc-live-label"><?php esc_html_e( 'How you see it in the panel', 'sabriel-ai' ); ?></div>
					<div style="display:grid;grid-template-columns:110px auto 1fr 1fr;gap:8px;align-items:center;padding:8px 10px;background:#fff;border-radius:8px;border:1px solid #eee;font-size:13px">
						<span style="font-weight:600"><?php esc_html_e( 'Monday', 'sabriel-ai' ); ?></span>
						<label style="display:flex;align-items:center;gap:6px;font-size:12px;color:#666"><input type="checkbox" checked disabled> <?php esc_html_e( 'Open', 'sabriel-ai' ); ?></label>
						<input type="time" value="09:00" disabled style="padding:6px 8px;border:1px solid #ccc;border-radius:6px;font-size:12.5px">
						<input type="time" value="18:30" disabled style="padding:6px 8px;border:1px solid #ccc;border-radius:6px;font-size:12.5px">
					</div>
				</div>
				<p><strong><?php esc_html_e( 'Why the second use counts as much as the first:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'A customer writing at 11pm asking "can I come now?" deserves an answer that takes reality into account: if the bot didn\'t know the hours, it might answer ambiguously or assume you\'re always open. With hours configured, the bot can naturally say "we\'re closed right now, we reopen tomorrow at 9" instead of leaving the customer confused.', 'sabriel-ai' ); ?></p>
				<div class="sab-doc-warn">⚠️ <?php esc_html_e( 'On first installation the opening hours are deliberately EMPTY: any preset schedule would be wrong for most businesses, and the bot would tell your customers as if it were true. Until you set your own, the bot never says whether you are open or closed and points to direct contact, and System status keeps a red light on this item. Set them as soon as you install the plugin, especially if you work evenings, weekends, or have a mid-week closing day.', 'sabriel-ai' ); ?></div>
				<p><strong><?php esc_html_e( 'If you work past midnight:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'feel free to write a shift that crosses midnight, for example from 22:00 to 02:00. On saving, the plugin splits it by itself into two pieces (that day\'s evening and the next day\'s night) and tells you so. It is needed because one in the morning already belongs to the next day: without the split the assistant would tell customers you are closed while you are in fact open.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'How to configure it best:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Switch closing days off completely (use the "Open" checkbox, do not just leave the times at zero): a day left "open" by mistake with random times makes the assistant say you are open when you are not.', 'sabriel-ai' ); ?></p>
				<div class="sab-doc-tip">💡 <?php esc_html_e( 'The split is only how the plugin stores it: the bot does not read it out split. It says «Monday 15:00-02:00», not «Monday until 00:00» and «Tuesday from 00:00», which made customers think you close at midnight.', 'sabriel-ai' ); ?></div>
			</div>
			<div class="sab-doc-voce" id="comp-pagamento">
				<h2><?php esc_html_e( 'Payment structure and installments', 'sabriel-ai' ); ?></h2>
				<p class="sab-doc-percorso"><?php echo esc_html( 'Sabriel AI' . ' → ' . __( 'Settings', 'sabriel-ai' ) . ' → ' . __( 'Behavior', 'sabriel-ai' ) ); ?></p>
				<p><strong><?php esc_html_e( 'What it does:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'How the payment for a project is split (deposit, halfway through the work, on final delivery) and whether to offer instalments above a threshold amount you choose. The bot uses this structure when a customer asks how payment works.', 'sabriel-ai' ); ?></p>
				<div class="sab-doc-live">
					<div class="sab-doc-live-label"><?php esc_html_e( 'How you see it in the panel', 'sabriel-ai' ); ?></div>
					<?php

					$sab_doc_str = Sabriel_Module_Settings::get( 'pagamento_struttura' ) ?: '25_25_50';
					if ( 'personalizzato' === $sab_doc_str ) {
						$sab_doc_acc  = (int) Sabriel_Module_Settings::get( 'pagamento_acconto_pct' );
						$sab_doc_meta = (int) Sabriel_Module_Settings::get( 'pagamento_meta_pct' );
					} elseif ( 'consegna' === $sab_doc_str ) {
						$sab_doc_acc  = 0;
						$sab_doc_meta = 0;
					} elseif ( '50_50' === $sab_doc_str ) {
						$sab_doc_acc  = 50;
						$sab_doc_meta = 0;
					} else {
						$sab_doc_acc  = 25;
						$sab_doc_meta = 25;
					}
					$sab_doc_fine = max( 0, 100 - $sab_doc_acc - $sab_doc_meta );
					$sab_doc_fasi = [
						[ $sab_doc_acc,  '#1461CC', __( 'Deposit', 'sabriel-ai' ) ],
						[ $sab_doc_meta, '#3b82c4', __( 'Halfway', 'sabriel-ai' ) ],
						[ $sab_doc_fine, '#10B981', __( 'On delivery', 'sabriel-ai' ) ],
					];
					$sab_doc_fasi = array_values( array_filter( $sab_doc_fasi, static function ( $f ) { return $f[0] > 0; } ) );
					?>
					<div style="display:flex;height:34px;border-radius:8px;overflow:hidden;font-size:11px;color:#fff;font-weight:700">
						<?php foreach ( $sab_doc_fasi as $f ) : ?>
						<div style="width:<?php echo (int) $f[0]; ?>%;background:<?php echo esc_attr( $f[1] ); ?>;display:flex;align-items:center;justify-content:center"><?php echo (int) $f[0]; ?>%</div>
						<?php endforeach; ?>
					</div>
					<div style="display:flex;font-size:10.5px;color:#888;margin-top:6px">
						<?php foreach ( $sab_doc_fasi as $f ) : ?>
						<div style="width:<?php echo (int) $f[0]; ?>%;text-align:center"><?php echo esc_html( $f[2] ); ?></div>
						<?php endforeach; ?>
					</div>
				</div>
				<p><strong><?php esc_html_e( 'Why the percentages must add up to exactly 100:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'The bot repeats these percentages to the customer as if they were your terms: if they added up to, say, 90 instead of 100, you would be telling whoever writes to you that a tenth of the price does not have to be paid.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Why installments are off by default and should be turned on carefully:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'When this feature is on, the bot promises the customer the possibility of paying in installments: it\'s a real commercial promise, not a decorative option. If you don\'t actually handle installment payments, a customer expecting this possibility after the bot proposed it might end up disappointed or confused when they find out it\'s not really available.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'How to configure it best:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'The percentages together must add up to 100%. If you never work with a halfway intermediate payment, leave that field empty or at zero: it\'s not mandatory to split it into three parts, you can use just deposit and final balance. Turn on installments only if you actually offer them in practice.', 'sabriel-ai' ); ?></p>
			</div>
			<div class="sab-doc-voce" id="comp-valuta">
				<h2><?php esc_html_e( 'Currency', 'sabriel-ai' ); ?></h2>
				<p class="sab-doc-percorso"><?php echo esc_html( 'Sabriel AI' . ' → ' . __( 'Settings', 'sabriel-ai' ) . ' → ' . __( 'Behavior', 'sabriel-ai' ) ); ?></p>
				<p><strong><?php esc_html_e( 'What it does:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Sets the currency used in the prices the customer sees when the bot writes an amount in chat. Choose one of the most common currencies or enter a custom one, and decide whether the symbol goes after the amount (1,200 €) or before ($1,200). The default is Euro.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'How to configure it best:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Choose the currency you actually work in and write the price list in that same currency: the bot will use the symbol you set here, but the figures stay the ones you write in the price list. If your currency is not in the list, use custom and enter code and symbol by hand.', 'sabriel-ai' ); ?></p>
			</div>
		</div>
		<?php
	}

	private function sezione_impostazioni_pagine() {
		?>
		<div class="sab-doc-sezione" id="pagine">
			<h1><?php echo Sabriel_Base::icona( 'catena', 26 ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- SVG statico scritto nel plugin. ?> <?php esc_html_e( 'Settings → Pages', 'sabriel-ai' ); ?></h1>
			<p class="sab-doc-intro"><?php esc_html_e( 'Where the bot finds links to share and where it takes examples of work/products from: two different ways of connecting the bot to content that already exists on your site, instead of having to rewrite it by hand.', 'sabriel-ai' ); ?></p>
			<div class="sab-doc-voce" id="pagine-link">
				<h2><?php esc_html_e( 'Links to site pages', 'sabriel-ai' ); ?></h2>
				<p class="sab-doc-percorso"><?php echo esc_html( 'Sabriel AI' . ' → ' . __( 'Settings', 'sabriel-ai' ) . ' → ' . __( 'Pages', 'sabriel-ai' ) ); ?></p>
				<p><strong><?php esc_html_e( 'What it does:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'A label + link list the bot can share in chat when useful to the conversation. The customer always sees a normal clickable link, never the label: that\'s only for you to recognize them here in the panel.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Why the bot doesn\'t find these pages on its own:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'The bot doesn\'t "browse" your site like a visitor would: it doesn\'t know a "Who we are" page exists unless you explicitly point it out here. This list is literally its map of pages it can cite.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'How to configure it best:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Add the most important pages a customer might want to explore during a conversation: services, about us, contact, FAQ. If you add a row labelled exactly "privacy", the bot knows it is your privacy notice and offers it when someone asks how you handle data: check that it really points to the right page.', 'sabriel-ai' ); ?></p>
			</div>
		</div>
		<?php
	}

	private function sezione_dashboard() {
		?>
		<div class="sab-doc-sezione" id="dashboard">
			<h1><?php echo Sabriel_Base::icona( 'grafico', 26 ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- SVG statico scritto nel plugin. ?> <?php esc_html_e( 'Dashboard', 'sabriel-ai' ); ?></h1>
			<p class="sab-doc-intro"><?php esc_html_e( 'The first page of the menu. It shows how the site is doing and how the bot is being used, and it holds the tool you need while you are still testing.', 'sabriel-ai' ); ?></p>
			<div class="sab-doc-voce">
				<h2><?php esc_html_e( 'General site analytics', 'sabriel-ai' ); ?></h2>
				<p class="sab-doc-percorso"><?php echo esc_html( 'Sabriel AI' . ' → ' . __( 'Dashboard', 'sabriel-ai' ) ); ?></p>
				<p><strong><?php esc_html_e( 'What it does:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Total visits, distinct visitors, chats opened, daily average, attempts stopped by the anti-abuse limits and a ranking of the most visited pages over the last 30 days. Counting is anonymous and cookie-free: each visitor is recognised only for that same day, never tracked over time, and the data stays on your site.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'What it is for:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'To see whether the bot is opened or ignored, and on which pages. If there are visits but few chats opened, the problem is usually the button: a colour that stands out too little, its position, or the welcome message switched off.', 'sabriel-ai' ); ?></p>
			</div>
			<div class="sab-doc-voce">
				<h2><?php esc_html_e( 'Unlock chat limit', 'sabriel-ai' ); ?></h2>
				<p class="sab-doc-percorso"><?php echo esc_html( 'Sabriel AI' . ' → ' . __( 'Dashboard', 'sabriel-ai' ) . ' → ' . __( 'Tools', 'sabriel-ai' ) ); ?></p>
				<p><strong><?php esc_html_e( 'What it does:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Removes the "you have written too many messages today" block for everyone. Useful when you are the one who hit the limit while testing the bot: real customers, if they are not blocked, will not even notice.', 'sabriel-ai' ); ?></p>
			</div>
		</div>
		<?php
	}

	private function sezione_stato_sistema() {
		?>
		<div class="sab-doc-sezione" id="stato-sistema">
			<h1><?php echo Sabriel_Base::icona( 'spunta', 26 ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- SVG statico scritto nel plugin. ?> <?php esc_html_e( 'System status', 'sabriel-ai' ); ?></h1>
			<p class="sab-doc-intro"><?php esc_html_e( 'A snapshot of how the bot is configured right now: green means correctly set, red indicates something still missing, almost always with a direct link to fix it right away without having to look for it yourself in the menu.', 'sabriel-ai' ); ?></p>
			<div class="sab-doc-voce">
				<div class="sab-doc-live">
					<div class="sab-doc-live-label"><?php esc_html_e( 'How you see it in the panel', 'sabriel-ai' ); ?></div>
					<div style="display:flex;gap:10px;flex-wrap:wrap">
						<span class="sabdoc-badge-verde">✓ <?php echo esc_html( _x( 'Configured', 'status badge example', 'sabriel-ai' ) ); ?></span>
						<span class="sabdoc-badge-rossa">✗ <?php esc_html_e( 'Still empty', 'sabriel-ai' ); ?></span>
					</div>
				</div>
				<p><strong><?php esc_html_e( 'Why there\'s a page dedicated just to "checking":', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'The bot\'s settings are scattered across many different pages: without a summary, it would be easy to forget you left an important field empty on a page you don\'t visit often (the AI key, for example, is configured once and then it\'s easy to forget it exists). This page gathers everything in one place, so a single glance is enough to know if something\'s missing.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Live key test:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'sends a real test message to the chosen AI engine: confirms everything really works, from the key through to the response, not just that the field was filled with something that looks like a valid key.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'If you import the wrong file you can go back:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'importing replaces the whole configuration, so before doing it the plugin sets the current one aside and shows you a button to put it back as it was. If the file you pick contains no Sabriel AI setting at all, it is rejected without touching anything, and when it does go through, the plugin tells you how many settings it applied: if you read one where you expected a hundred, you notice straight away.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Are the keys really encrypted?', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'One line on the page answers this question by checking the server, not by guessing. On some very old hosting the component needed for encryption is missing: in that case a red box appears explaining what to ask your host\'s support team, instead of letting you believe your keys are safe when they are not.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Support email', 'sabriel-ai' ); ?>:</strong> <?php esc_html_e( 'if you do not give one of your own, the WordPress administrator\'s address is published in its place: in the chat and in the code of every page of the site. This line tells you right away, instead of letting you find out from the spam that starts arriving.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Duplicate copies of the plugin:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'If old Sabriel AI code pasted in by hand is still active on the site (for example from before it became a plugin), the panel detects it and names it. This is the situation that throws sites into error during a migration, and without this indicator it would be extremely hard to work out.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Export / Import configuration:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'downloads a file with all your current settings: handy as a backup before major changes, or to copy the same configuration onto another site with similar characteristics. The AI engine keys are not included in the export (they stay encrypted and separate for safety): after an import they will have to be entered again by hand.', 'sabriel-ai' ); ?> <?php esc_html_e( 'One thing worth knowing: a file exported from the complete edition also carries appearance values that this edition has no control for. Those are skipped on import, and whatever your site already had is left as it was. Everything this edition does manage comes across normally, so a configuration moved between two sites running this edition arrives complete.', 'sabriel-ai' ); ?></p>
				<div class="sab-doc-tip">💡 <?php esc_html_e( 'Check this page as soon as you finish configuring a new site, and every time the bot seems to behave strangely: often the cause of unexpected behavior is exactly a red line left here, not a real error in how the bot works.', 'sabriel-ai' ); ?></div>
			</div>
		</div>

		<?php
	}

	private function sezione_privacy() {
		$motore = class_exists( 'Sabriel_Module_AI' ) ? Sabriel_Module_AI::PROVIDERS[ Sabriel_Module_AI::provider_attivo() ][0] : '';
		?>
		<div class="sab-doc-sezione" id="privacy">
			<h1><?php echo Sabriel_Base::icona( 'lucchetto', 22 ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- SVG statico scritto nel plugin. ?> <?php esc_html_e( 'Privacy and data', 'sabriel-ai' ); ?></h1>
			<p class="sab-doc-intro"><?php esc_html_e( 'This page lists, without beating about the bush, which data leaves your site, which is kept and for how long, and which limits are worth knowing about. It is there to help you write your site\'s privacy policy: nobody can do that for you, because it depends on how you use the plugin and where you are based.', 'sabriel-ai' ); ?></p>
			<div class="sab-doc-warn">⚠️ <?php esc_html_e( 'This page describes how the plugin works, it is not legal advice. If you collect customer data in the European Union, have it read by whoever looks after your privacy compliance.', 'sabriel-ai' ); ?></div>

			<div class="sab-doc-voce" id="privacy-uscita">
				<h2><?php esc_html_e( 'What leaves your site', 'sabriel-ai' ); ?></h2>
				<p><strong><?php esc_html_e( 'The AI service.', 'sabriel-ai' ); ?></strong>
				<?php
				printf(
					/* translators: %s: name of the active AI engine, e.g. Google Gemini */
					esc_html__( 'Every message the visitor writes in the chat, together with the attachments they upload and your business information (price list, opening hours, contact details), is sent to the AI service you have chosen, currently %s. That is how the bot works: without sending it, there would be no reply. How that data is handled and for how long is decided by the AI service, not by this plugin: its terms are what apply.', 'sabriel-ai' ),
					esc_html( $motore )
				);
				?>
				</p>
				<div class="sab-doc-warn">⚠️ <?php esc_html_e( 'One special case to be aware of: Anthropic\'s Fable 5 model requires data to be kept for 30 days and is not available with the no-retention mode. If that is a problem for your clients, pick a different model on the AI Engine page.', 'sabriel-ai' ); ?></div>
				<p><strong><?php esc_html_e( 'Nothing else.', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Outside this one case the plugin sends data to no one else: not to whoever created it, not to analytics services, not to advertising networks.', 'sabriel-ai' ); ?></p>
			</div>

			<div class="sab-doc-voce" id="privacy-conservazione">
				<h2><?php esc_html_e( 'What stays saved, and where', 'sabriel-ai' ); ?></h2>
				<p><strong><?php esc_html_e( 'Visits to the site.', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'The plugin counts page visits and how many chats are opened. To avoid counting the same person twice, it gives every visitor a random code, saved in their browser. There is no name, no email, no IP address: just a code and the paths visited, kept for 90 days and then deleted automatically.', 'sabriel-ai' ); ?></p>
				<div class="sab-doc-warn">⚠️ <?php esc_html_e( 'Worth knowing: this count does not ask the visitor for consent, so if your site uses a cookie banner remember to mention it in your privacy notice alongside your other analytics tools. It can be switched off: Settings, Limits tab. Turn it off and nothing is counted at all.', 'sabriel-ai' ); ?> <strong>&laquo;<?php echo esc_html( __( 'Collect visit statistics', 'sabriel-ai' ) ); ?>&raquo;</strong></div>
			</div>
			<div class="sab-doc-voce" id="priv-statistiche">
				<h2><?php esc_html_e( 'Collect visit statistics', 'sabriel-ai' ); ?></h2>
				<p><strong><?php esc_html_e( 'What it counts:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'page views, how many different visitors and how many chats were opened. It is stored on your own site, day by day, and nothing is sent anywhere.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Why it matters in the EU:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'to tell one visitor from another the widget builds a short code out of the browser itself: screen size, language, time zone and similar. No cookie is written and nothing leaves your site, but under European rules reading those characteristics still counts as accessing the visitor\'s device, so it has to be declared, or consented to, like any other analytics. If you would rather not deal with it, switch it off: the chat keeps working, only the counters go quiet.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'Where the switch is:', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Settings, Limits tab. System status also shows whether it is on or off, so you can check it in one glance.', 'sabriel-ai' ); ?> <strong>&laquo;<?php echo esc_html( __( 'Collect visit statistics', 'sabriel-ai' ) ); ?>&raquo;</strong></p>
				<p><strong><?php esc_html_e( 'The conversations and the contacts.', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'The traffic counts and the whole configuration stay inside your WordPress, in your database: they are not copied anywhere. Conversations are not saved on the site: they go to the AI engine and end there.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'The AI service keys.', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'They are saved encrypted, so not even by looking inside the database can they be read in clear. For that to be possible the PHP OpenSSL extension is needed: it is there almost always, and the System status page tells you whether it really is on your server. If it is missing, the key stays written in clear and there you see it stated plainly. If one day the WordPress security keys change or you move the site, the encrypted keys may become unreadable: the panel tells you and you just enter them again.', 'sabriel-ai' ); ?></p>
				<p><strong><?php esc_html_e( 'When you uninstall.', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'If you leave the relevant checkbox on in Settings, uninstalling the plugin deletes all of its data.', 'sabriel-ai' ); ?></p>
			</div>

			<div class="sab-doc-voce" id="privacy-limiti">
				<h2><?php esc_html_e( 'Limits worth knowing', 'sabriel-ai' ); ?></h2>
				<p><?php esc_html_e( 'These are two deliberate behaviours, not defects. We write them here because in some businesses they matter.', 'sabriel-ai' ); ?></p>
				<p><strong>1. <?php esc_html_e( 'The chat summary goes to the address written in the widget.', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'Anyone chatting can have a copy of the conversation sent to them by email. That address is not verified with a second confirmation message: it works like the "send me a copy" forms found on many sites. It means someone could enter another person\'s address and have that copy delivered to them. To limit this, each address can receive at most three summaries per hour, and at the bottom of the email there is always a line explaining where it comes from and that it can be ignored.', 'sabriel-ai' ); ?></p>
				<p><strong>2. <?php esc_html_e( 'Behind certain protection services all visitors look like a single one.', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'The anti-abuse brakes count requests per connection. The plugin already recognises Cloudflare and works out who the real visitor is. If your site sits behind a different system instead (nginx, Varnish, AWS or Sucuri, for example), every visitor may look like the same connection: in that case the limits run out far sooner than expected. It is fixed by telling the plugin which system to trust.', 'sabriel-ai' ); ?></p>
				<div class="sab-doc-tip">💡 <?php esc_html_e( 'For developers: the systems to be treated as trusted are added with the sabriel_ai_range_proxy_attendibili filter.', 'sabriel-ai' ); ?></div>
			</div>
		</div>
		<?php
	}

	private function sezione_premium() {
		?>
		<div class="sab-doc-sezione" id="premium">
			<h1><?php echo Sabriel_Base::icona( 'lucchetto', 22 ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- SVG statico scritto nel plugin. ?> <?php esc_html_e( 'Complete edition', 'sabriel-ai' ); ?></h1>
			<p class="sab-doc-intro"><?php esc_html_e( 'This edition is free and complete in itself: chat with three AI engines to choose from, a customisable widget, nine languages, anti-abuse limits, traffic analysis and direct contact by email. There are no locked features to unlock and no licences to activate: what you see in the panel is all there is.', 'sabriel-ai' ); ?></p>
			<p><?php esc_html_e( 'There is also a paid full edition, sold separately, which adds things not included here: appointments in chat, PDF quotes, lead scoring, a site check on a prospective customer, portfolio, recoverable conversation history, reading of your site\'s pages, a Mailchimp connection and a protection pack for WordPress. The "Complete edition" page in the menu lists them one by one and leads to the purchase.', 'sabriel-ai' ); ?></p>
			<?php if ( class_exists( 'Sabriel_Module_Prezzi' ) ) { Sabriel_Module_Prezzi::render_tabella_piani( [ 'id' => 'sabdoc', 'pubblico' => $this->in_export ] ); } ?>
			<div class="sab-doc-voce">
				<p><strong><?php esc_html_e( 'Why the boundary is "how many sites" and not "which functions":', 'sabriel-ai' ); ?></strong> <?php esc_html_e( 'An agency managing 10 sites for different clients does not need different features from someone with a single site: it needs to install the same plugin several times. That is why the two paid plans contain the same things and differ only in the number of sites.', 'sabriel-ai' ); ?></p>
			</div>
		</div>
		<?php
	}

}
