<?php

/**
 * Plugin Name: Uplink7 Automatic Translator
 * Plugin URI: https://uplink7.com/
 * Description: Professional Google Translate Integration with Lazy-Load, Auto-Injection & Reset. Free Edition.
 * Version: 1.4
 * Author: uplink7
 * Author URI: https://uplink7.com
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: uplink7-automatic-translator
 */

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

/**
 * 1. FREEMIUS SDK INITIALIZATION
 */
if ( ! function_exists( 'u7at_fs' ) ) {
	function u7at_fs() {
		global $u7at_fs;

		if ( ! isset( $u7at_fs ) ) {
			$sdk_path = dirname( __FILE__ ) . '/lib/freemius/start.php';
			if ( ! file_exists( $sdk_path ) ) {
				return false;
			}
			require_once $sdk_path;

			$u7at_fs = fs_dynamic_init( [
				'id'               => '24870',
				'slug'             => 'uplink7-automatic-translator',
				'type'             => 'plugin',
				'public_key'       => 'pk_738410f850260427795a2843dcb2d',
				'is_premium'       => false,
				'has_paid_plans'   => true,
				'is_org_compliant' => true,
				'menu'             => [
					'slug'   => 'uplink7-automatic-translator',
					'parent' => [
						'slug' => 'options-general.php',
					],
				],
				'is_live'          => true,
			] );
		}

		return $u7at_fs;
	}

	u7at_fs();
	do_action( 'u7at_fs_loaded' );
}

/**
 * 2. REGISTER OPTIONS
 */
add_action( 'admin_init', function () {
	$text_options = [
		'u7at_default_lang',
		'u7at_inj_header_pos',
		'u7at_inj_footer_pos',
		'u7at_inj_float_pos'
	];
	foreach ( $text_options as $opt ) {
		register_setting( 'u7at-settings-group', $opt, [
			'sanitize_callback' => 'sanitize_text_field',
			'default'           => '',
		] );
	}

	$int_options = [
		'u7at_auto_switch',
		'u7at_native_names',
		'u7at_flag_mode',
		'u7at_text_size',
		'u7at_border_radius',
		'u7at_border_width'
	];
	foreach ( $int_options as $opt ) {
		register_setting( 'u7at-settings-group', $opt, [
			'sanitize_callback' => 'absint',
			'default'           => 0,
		] );
	}

	register_setting( 'u7at-settings-group', 'u7at_bg_color', [
		'sanitize_callback' => 'sanitize_hex_color',
		'default'           => '#ffffff',
	] );
	register_setting( 'u7at-settings-group', 'u7at_text_color', [
		'sanitize_callback' => 'sanitize_hex_color',
		'default'           => '#333333',
	] );
} );

/**
 * 3. ACTIVITY CHECK
 */
function u7at_is_active_here() {
	if ( is_admin() || is_robots() ) {
		return false;
	}
	if ( defined( 'IN_MAINTENANCE' ) || ( function_exists( 'is_feed' ) && is_feed() ) ) {
		return false;
	}
	return true;
}

/**
 * 4. WIDGET GENERATOR (HTML)
 */
function u7at_get_widget_html( $class = '' ) {
	if ( ! u7at_is_active_here() ) {
		return '';
	}

	$bg_color      = get_option( 'u7at_bg_color', '#ffffff' );
	$text_color    = get_option( 'u7at_text_color', '#333333' );
	$text_size     = get_option( 'u7at_text_size', '14' );
	$border_radius = get_option( 'u7at_border_radius', '0' );
	$border_width  = get_option( 'u7at_border_width', '0' );

	$style = sprintf(
		'background:%s; color:%s; font-size:%dpx; border-radius:%dpx; border:%dpx solid #ccc; cursor:pointer; outline:none; appearance:none;',
		$bg_color,
		$text_color,
		absint( $text_size ),
		absint( $border_radius ),
		absint( $border_width )
	);

	$langs = [
		'af' => ['Afrikaans', 'Afrikaans', '🇿🇦'], 'sq' => ['Albanian', 'Shqip', '🇦🇱'], 'am' => ['Amharic', 'አማርኛ', '🇪🇹'],
		'ar' => ['Arabic', 'العربية', '🇸🇦'], 'hy' => ['Armenian', 'Հայερեն', '🇦🇲'], 'az' => ['Azerbaijani', 'Azərbaycan dili', '🇦🇿'],
		'eu' => ['Basque', 'Euskara', '🇪🇸'], 'be' => ['Belarusian', 'Беларуская', '🇧🇾'], 'bn' => ['Bengali', 'বাংলা', '🇧🇩'],
		'bs' => ['Bosnian', 'Bosanski', '🇧🇦'], 'bg' => ['Bulgarian', 'Български', '🇧🇬'], 'ca' => ['Catalan', 'Català', '🇪🇸'],
		'ceb' => ['Cebuano', 'Cebuano', '🇵🇭'], 'ny' => ['Chichewa', 'Chichewa', '🇲🇼'], 'zh-CN' => ['Chinese (Simp)', '中文 (简体)', '🇨🇳'],
		'zh-TW' => ['Chinese (Trad)', '中文 (繁體)', '🇹🇼'], 'co' => ['Corsican', 'Corsu', '🇫🇷'], 'hr' => ['Croatian', 'Hrvatski', '🇭🇷'],
		'cs' => ['Czech', 'Čeština', '🇨🇿'], 'da' => ['Danish', 'Dansk', '🇩🇰'], 'nl' => ['Dutch', 'Nederlands', '🇳🇱'],
		'en' => ['English', 'English', '🇺🇸'], 'eo' => ['Esperanto', 'Esperanto', '🌍'], 'et' => ['Estonian', 'Eesti', '🇪🇪'],
		'tl' => ['Filipino', 'Filipino', '🇵🇭'], 'fi' => ['Finnish', 'Suomi', '🇫🇮'], 'fr' => ['French', 'Français', '🇫🇷'],
		'fy' => ['Frisian', 'Frysk', '🇳🇱'], 'gl' => ['Galician', 'Galego', '🇪🇸'], 'ka' => ['Georgian', 'ქართული', '🇬🇪'],
		'de' => ['German', 'Deutsch', '🇩🇪'], 'el' => ['Greek', 'Ελληνικά', '🇬🇷'], 'gu' => ['Gujarati', 'ગુજરાતી', '🇮🇳'],
		'ht' => ['Haitian Creole', 'Kreyòl ayisyen', '🇭🇹'], 'ha' => ['Hausa', 'Hausa', '🇳🇬'], 'haw' => ['Hawaiian', 'ʻŌlelo Hawaiʻi', '🇺🇸'],
		'he' => ['Hebrew', 'עברית', '🇮🇱'], 'hi' => ['Hindi', 'हिन्दी', '🇮🇳'], 'hmn' => ['Hmong', 'Hmong', '🏳️'],
		'hu' => ['Hungarian', 'Magyar', '🇭🇺'], 'is' => ['Icelandic', 'Íslenska', '🇮🇸'], 'ig' => ['Igbo', 'Igbo', '🇳🇬'],
		'id' => ['Indonesian', 'Indonesia', '🇮🇩'], 'ga' => ['Irish', 'Gaeilge', '🇮🇪'], 'it' => ['Italian', 'Italiano', '🇮🇹'],
		'ja' => ['Japanese', '日本語', '🇯🇵'], 'jv' => ['Javanese', 'Basa Jawa', '🇮🇩'], 'kn' => ['Kannada', 'ಕನ್ನಡ', '🇮🇳'],
		'kk' => ['Kazakh', 'Қазақ тілі', '🇰🇿'], 'km' => ['Khmer', 'ខ្መែር', '🇰🇭'], 'rw' => ['Kinyarwanda', 'Ikinyarwanda', '🇷🇼'],
		'ko' => ['Korean', '한국어', '🇰🇷'], 'ku' => ['Kurdish', 'Kurdî', '🇹🇷'], 'ky' => ['Kyrgyz', 'Кыргызча', '🇰🇬'],
		'lo' => ['Lao', 'ລາວ', '🇱🇦'], 'la' => ['Latin', 'Latina', '✟'], 'lv' => ['Latvian', 'Latviešu', '🇱🇻'],
		'lt' => ['Lithuanian', 'Lietuvių', '🇱🇹'], 'lb' => ['Luxembourgish', 'Lëtzebuergesch', '🇱🇺'], 'mk' => ['Macedonian', 'Македонски', '🇲🇰'],
		'mg' => ['Malagasy', 'Malagasy', '🇲🇬'], 'ms' => ['Malay', 'Bahasa Melayu', '🇲🇾'], 'ml' => ['Malayalam', 'മലയാളം', '🇮🇳'],
		'mt' => ['Maltese', 'Malti', '🇲🇹'], 'mi' => ['Maori', 'Te Reo Māori', '🇳🇿'], 'mr' => ['Marathi', 'မරාठी', '🇮🇳'],
		'mn' => ['Mongolian', 'Мონгол', '🇲🇳'], 'my' => ['Myanmar', 'မြန်မာ', '🇲🇲'], 'ne' => ['Nepali', 'नेपाली', '🇳🇵'],
		'no' => ['Norwegian', 'Norsk', '🇳🇴'], 'or' => ['Odia', 'ଓଡ଼ିଆ', '🇮🇳'], 'ps' => ['Pashto', 'ပښتو', '🇦🇫'],
		'fa' => ['Persian', 'فارسی', '🇮🇷'], 'pl' => ['Polish', 'Polski', '🇵🇱'], 'pt' => ['Portuguese', 'Português', '🇵🇹'],
		'pa' => ['Punjabi', 'ਪੰਜਾਬੀ', '🇮🇳'], 'ro' => ['Romanian', 'Română', '🇷🇴'], 'ru' => ['Russian', 'Русский', '🇷🇺'],
		'sm' => ['Samoan', 'Gagana faʻa Sāmoa', '🇼🇸'], 'gd' => ['Scots Gaelic', 'Gàidhlig', '🏴'], 'sr' => ['Serbian', 'Српски', '🇷🇸'],
		'st' => ['Sesotho', 'Sesotho', '🇱🇸'], 'sn' => ['Shona', 'ChiShona', '🇿🇼'], 'sd' => ['Sindhi', 'سنڌي', '🇵🇰'],
		'si' => ['Sinhala', 'සිංහල', '🇱🇰'], 'sk' => ['Slovak', 'Slovenčina', '🇸🇰'], 'sl' => ['Slovenian', 'Slovenščina', '🇸🇮'],
		'so' => ['Somali', 'Soomaali', '🇸🇴'], 'es' => ['Spanish', 'Español', '🇪🇸'], 'su' => ['Sundanese', 'Basa Sunda', '🇮🇩'],
		'sw' => ['Swahili', 'Kiswahili', '🇰🇪'], 'sv' => ['Swedish', 'Svenska', '🇸🇪'], 'tg' => ['Tajik', 'Тоҷикӣ', 'Т🇯'],
		'ta' => ['Tamil', 'தமிழ்', '🇮🇳'], 'tt' => ['Tatar', 'Татар', '🇷🇺'], 'te' => ['Telugu', 'తెలుగు', '🇮🇳'],
		'th' => ['Thai', 'ไทย', '🇹🇭'], 'tr' => ['Turkish', 'Türkçe', '🇹🇷'], 'tk' => ['Turkmen', 'Türkmen', '🇹🇲'],
		'uk' => ['Ukrainian', 'Українська', '🇺🇦'], 'ur' => ['Urdu', 'اردو', '🇵🇰'], 'ug' => ['Uyghur', 'ئۇيղۇرچە', '🇨🇳'],
		'uz' => ['Uzbek', 'Oʻzbek', '🇺🇿'], 'vi' => ['Vietnamese', 'Tiếng Việt', '🇻🇳'], 'cy' => ['Welsh', 'Cymraeg', '🏴'],
		'xh' => ['Xhosa', 'IsiXhosa', '🇿🇦'], 'yi' => ['Yiddish', 'ייִדיש', '🇮🇱'], 'yo' => ['Yoruba', 'Yorùbá', '🇳🇬'],
		'zu' => ['Zulu', 'IsiZulu', '🇿🇦'],
	];

	$native_names = get_option( 'u7at_native_names' );
	$flag_mode    = get_option( 'u7at_flag_mode' );

	$html  = '<div class="u7at-box ' . esc_attr( $class ) . '">';
	$html .= '<label for="u7translate" style="' . esc_attr( $style ) . '">🌐 Language&nbsp;</label>';
	$html .= '<select id="u7translate" class="u7at-lang-selector" style="' . esc_attr( $style ) . '" onchange="if(window.u7at_do_switch) window.u7at_do_switch(this.value)">';
	$html .= '<option value="">' . esc_html__( 'Select Language', 'uplink7-automatic-translator' ) . '</option>';

	foreach ( $langs as $code => $v ) {
		$name = ( $native_names ? $v[1] : $v[0] );
		$flag = $flag_mode ? $v[2] . ' ' : '';
		$html .= sprintf( 
			'<option value="%s" class="notranslate">%s</option>', 
			esc_attr( $code ), 
			esc_html( $flag . $name ) 
		);
	}

	$html .= '<option value="U7AT_PURGE">' . esc_html__( '↺ Reset', 'uplink7-automatic-translator' ) . '</option>';
	$html .= '</select></div>';

	return $html;
}
add_shortcode( 'u7at_translate', 'u7at_get_widget_html' );

/**
 * 5. ASSETS & ENQUEUING (FRONTEND)
 */
add_action( 'wp_enqueue_scripts', function () {
	if ( ! u7at_is_active_here() ) {
		return;
	}

	$default_lang = get_option( 'u7at_default_lang', 'de' );
	$auto_switch  = get_option( 'u7at_auto_switch' ) ? 'true' : 'false';

	wp_register_script( 'u7at-frontend-logic', false );
	wp_enqueue_script( 'u7at-frontend-logic' );

	$inline_js = "
	window.u7at_googleTranslateElementInit = function() {
		if (typeof google !== 'undefined' && google.translate) {
			new google.translate.TranslateElement({pageLanguage: '" . esc_js( $default_lang ) . "', autoDisplay: false}, 'u7at_google_translate_element');
		}
	};

	window.u7at_load_script = function() {
		if(!window.u7at_s_l) {
			var s = document.createElement('script');
			s.src = 'https://translate.google.com/translate_a/element.js?cb=u7at_googleTranslateElementInit';
			s.setAttribute('data-no-optimize', '1');
			s.setAttribute('data-no-defer', '1');
			s.setAttribute('data-rocketlazyload', 'false'); 
			document.body.appendChild(s);
			window.u7at_s_l = true;
		}
	};

	window.u7at_sync_dropdowns = function(langCode) {
		if(!langCode) return;
		document.querySelectorAll('#u7translate, .u7at-lang-selector').forEach(function(select) {
			select.value = langCode;
		});
	};

	window.u7at_do_switch = function(l) {
		if(!l) return;
		
		if(l === 'U7AT_PURGE') {
			var name = 'googtrans';
			var hostname = window.location.hostname;
			var parts = hostname.split('.');
			// Aggressive cookie purge over all domain levels
			for (var i = 0; i <= parts.length - 2; i++) {
				var domain = parts.slice(i).join('.');
				document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; domain=' + domain + '; SameSite=Lax';
				document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; domain=.' + domain + '; SameSite=Lax';
			}
			document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; SameSite=Lax';
			try { 
				localStorage.removeItem(name); 
				sessionStorage.removeItem(name);
				sessionStorage.setItem('u7at_reset_active', '1');
			} catch(e) {}
			window.location.reload(); 
			return;
		}
		
		var cookieValue = '/" . esc_js( $default_lang ) . "/' + l;
		var cookieOptions = '; path=/; SameSite=Lax;';
		document.cookie = 'googtrans=' + cookieValue + cookieOptions;
		document.cookie = 'googtrans=' + cookieValue + '; domain=.' + window.location.hostname + cookieOptions;
		
		window.u7at_sync_dropdowns(l);
		window.u7at_load_script();

		var count = 0;
		var itv = setInterval(function() {
			var el = document.querySelector('select.goog-te-combo');
			if(el && window.google && window.google.translate) {
				el.value = l;
				el.dispatchEvent(new Event('change', {bubbles:true}));
				clearInterval(itv);
			}
			if(count++ > 250) clearInterval(itv);
		}, 100);
	};

	(function() {
		function getCookie(name) {
			var v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
			return v ? v[2] : null;
		}

		var init = function() {
			var url = new URL(window.location);
			var urlL = url.searchParams.get('lang');
			var cookieVal = getCookie('googtrans');
			
			if(urlL) {
				window.u7at_do_switch(urlL);
				url.searchParams.delete('lang');
				window.history.replaceState({}, document.title, url.toString());
			} else if (cookieVal) {
				var parts = decodeURIComponent(cookieVal).split('/');
				var currentLang = parts[parts.length - 1];
				if(currentLang) {
                    window.u7at_sync_dropdowns(currentLang);
                    window.u7at_load_script();
                }
			} else if (" . esc_js( $auto_switch ) . ") {
				// If reset was just active, we clear the flag but skip detection ONCE for this load
				if(sessionStorage.getItem('u7at_reset_active') === '1') {
					sessionStorage.removeItem('u7at_reset_active'); 
					return;
				}
				
				var userLang = (navigator.language || navigator.userLanguage || 'de').substring(0, 2).toLowerCase();
				var defLang = '" . esc_js( strtolower( $default_lang ) ) . "';
				if(userLang && userLang !== defLang) {
					setTimeout(function() { 
						if(!getCookie('googtrans')) { 
							window.u7at_do_switch(userLang); 
						} 
					}, 1000);
				}
			}
		};

		if (document.readyState === 'complete') {
			init();
		} else {
			window.addEventListener('load', init);
		}
	})();";
	wp_add_inline_script( 'u7at-frontend-logic', $inline_js );

	wp_register_style( 'u7at-frontend-styles', false );
	wp_enqueue_style( 'u7at-frontend-styles' );

	$inline_css = "
        .goog-te-banner-frame, #goog-gt-tt, .skiptranslate { display: none !important; }
        body { top: 0px !important; }
        .u7at-wrap { display: flex; width: 100%; padding: 10px; box-sizing: border-box; }
        .u7at-pos-left { justify-content: flex-start; }
        .u7at-pos-center { justify-content: center; }
        .u7at-pos-right { justify-content: flex-end; }
        .u7at-float-sticky { position: fixed; bottom: 20px; z-index: 999999; }
        .u7at-f-left { left: 20px; }
        .u7at-f-center { left: 50%; transform: translateX(-50%); }
        .u7at-f-right { right: 20px; }
	";
	wp_add_inline_style( 'u7at-frontend-styles', $inline_css );
} );

/**
 * 6. ASSETS & ENQUEUING (ADMIN)
 */
add_action( 'admin_enqueue_scripts', function ( $hook ) {
	if ( 'settings_page_uplink7-automatic-translator' !== $hook ) {
		return;
	}

	wp_register_style( 'u7at-admin-styles', false );
	wp_enqueue_style( 'u7at-admin-styles' );

	$admin_css = "
        .u7at-q { cursor: help; background: #2271b1; color: #fff; width: 18px; height: 18px; display: inline-flex; align-items: center; justify-content: center; border-radius: 50%; font-size: 12px; margin-left: 5px; font-weight: bold; }
        .u7at-box-code { background: #f0f0f1; border: 1px dashed #ccd0d4; padding: 10px; border-radius: 4px; font-family: monospace; }
		.u7at-promo-card { background: #fff; border: 1px solid #ccd0d4; padding: 25px; border-radius: 12px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1); margin-top: 20px; border-left: 5px solid #fbbf24; }
	";
	wp_add_inline_style( 'u7at-admin-styles', $admin_css );
} );

/**
 * 7. HOOKS FOR PLACEMENT
 */
add_action( 'wp_footer', function () {
	if ( ! u7at_is_active_here() ) {
		return;
	}
	?>
    <div id="u7at_google_translate_element" style="display:none!important;"></div>
	<?php
	$footer_pos = get_option( 'u7at_inj_footer_pos' );
	if ( $footer_pos ) {
		printf( '<div class="u7at-wrap u7at-pos-%s">%s</div>', esc_attr( $footer_pos ), u7at_get_widget_html() );
	}

	$float_pos = get_option( 'u7at_inj_float_pos' );
	if ( $float_pos ) {
		printf( '<div class="u7at-float-sticky u7at-f-%s">%s</div>', esc_attr( $float_pos ), u7at_get_widget_html() );
	}
}, 999 );

add_action( 'wp_body_open', function () {
	if ( u7at_is_active_here() ) {
		$header_pos = get_option( 'u7at_inj_header_pos' );
		if ( $header_pos ) {
			printf( '<div class="u7at-wrap u7at-pos-%s">%s</div>', esc_attr( $header_pos ), u7at_get_widget_html() );
		}
	}
} );

/**
 * 8. ADMIN PANEL
 */
add_action( 'admin_menu', function () {
	add_options_page(
		'U7 Automatic Translator',
		'U7 Translator',
		'manage_options',
		'uplink7-automatic-translator',
		'u7at_render_admin_page'
	);
} );

function u7at_render_admin_page() {
	$pos_opts = [ '' => 'Off', 'left' => 'Left', 'center' => 'Center', 'right' => 'Right' ];
	$u7at_fs = u7at_fs();
	?>
    <div class="wrap">
        <h1 style="background:#2271b1; color:#fff; padding:20px; border-radius:8px 8px 0 0; margin:0;">🌍 Uplink7 Automatic Translator</h1>

        <div class="u7at-box-code" style="background: #e7f7ff; padding: 15px; margin: 20px 0; border: none; border-left: 4px solid #2271b1; color: #0c3d5e;">
            <strong>Shortcode:</strong> <code>[u7at_translate]</code> | <strong>PHP Hook:</strong> <code>&lt;?php echo u7at_get_widget_html(); ?&gt;</code>
        </div>

        <form method="post" action="options.php" style="background:#fff; padding:30px; border:1px solid #ccd0d4; border-radius:0 0 8px 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.05);">
			<?php settings_fields( 'u7at-settings-group' ); ?>
            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 40px;">

                <div>
                    <h3 style="border-bottom: 1px solid #eee; padding-bottom: 10px;">🎨 Design & Position</h3>
                    <table class="form-table">
                        <tr>
                            <th scope="row">Default Language (Code)</th>
                            <td>
                                <input type="text" name="u7at_default_lang" value="<?php echo esc_attr( get_option( 'u7at_default_lang', 'de' ) ); ?>" size="5">
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">Background Color</th>
                            <td><input type="color" name="u7at_bg_color" value="<?php echo esc_attr( get_option( 'u7at_bg_color', '#ffffff' ) ); ?>"></td>
                        </tr>
                        <tr>
                            <th scope="row">Text Color</th>
                            <td><input type="color" name="u7at_text_color" value="<?php echo esc_attr( get_option( 'u7at_text_color', '#333333' ) ); ?>"></td>
                        </tr>
                        <tr>
                            <th scope="row">Text Size (px)</th>
                            <td><input type="number" name="u7at_text_size" value="<?php echo absint( get_option( 'u7at_text_size', '14' ) ); ?>" min="8" max="40"></td>
                        </tr>
                        <tr>
                            <th scope="row">Border Radius (px)</th>
                            <td><input type="number" name="u7at_border_radius" value="<?php echo absint( get_option( 'u7at_border_radius', '0' ) ); ?>" min="0" max="50"></td>
                        </tr>
                        <tr>
                            <th scope="row">Border Width (px)</th>
                            <td><input type="number" name="u7at_border_width" value="<?php echo absint( get_option( 'u7at_border_width', '0' ) ); ?>" min="0" max="10"></td>
                        </tr>
                    </table>

                    <div style="margin-top:20px; background:#f9f9f9; padding:15px; border-radius:8px;">
                        <strong>Automatic Placement:</strong><br><br>
                        <div>Header: <select name="u7at_inj_header_pos"><?php foreach ( $pos_opts as $v => $l ) {
									printf( '<option value="%s" %s>%s</option>', esc_attr( $v ), selected( $v, get_option( 'u7at_inj_header_pos' ), false ), esc_html( $l ) );
								} ?></select></div>
                        <div style="margin-top:10px;">Footer: <select name="u7at_inj_footer_pos"><?php foreach ( $pos_opts as $v => $l ) {
									printf( '<option value="%s" %s>%s</option>', esc_attr( $v ), selected( $v, get_option( 'u7at_inj_footer_pos' ), false ), esc_html( $l ) );
								} ?></select></div>
                        <div style="margin-top:10px;">Sticky: <select name="u7at_inj_float_pos"><?php foreach ( $pos_opts as $v => $l ) {
									printf( '<option value="%s" %s>%s</option>', esc_attr( $v ), selected( $v, get_option( 'u7at_inj_float_pos' ), false ), esc_html( $l ) );
								} ?></select></div>
                    </div>
                </div>

                <div>
                    <h3 style="border-bottom: 1px solid #eee; padding-bottom: 10px;">⚙️ Functions</h3>
                    <div style="margin-bottom:15px;">
                        <label><input type="checkbox" name="u7at_auto_switch" value="1" <?php checked( 1, get_option( 'u7at_auto_switch' ) ); ?>> Enable Browser Language Detection</label>
                    </div>
                    <div style="margin-bottom:15px;">
                        <label><input type="checkbox" name="u7at_native_names" value="1" <?php checked( 1, get_option( 'u7at_native_names' ) ); ?>> Use Native Language Names</label>
                    </div>
                    <div style="margin-bottom:15px;">
                        <label><input type="checkbox" name="u7at_flag_mode" value="1" <?php checked( 1, get_option( 'u7at_flag_mode' ) ); ?>> Display Flags</label>
                    </div>

					<!-- Promo Card -->
					<div class="u7at-promo-card">
						<h3 style="margin-top: 0;">🚀 Get more out of your translations!</h3>
						<p>Unlock professional features like <strong>SEO Hreflang Tags</strong>, <strong>Geo-IP Detection</strong> and <strong>Brand Protection</strong> (remove Google logo).</p>
						<div style="display: flex; gap: 10px; margin-top: 15px;">
							<?php if ( $u7at_fs ) : ?>
								<a href="<?php echo esc_url( $u7at_fs->get_upgrade_url() ); ?>" class="button button-primary" style="background: #fbbf24; border-color: #f59e0b; color: #000; font-weight: bold; padding: 5px 15px; height: auto;">Upgrade Now</a>
							<?php endif; ?>
							<a href="<?php echo esc_url( 'https://uplink7.com/' ); ?>" target="_blank" class="button button-secondary" style="padding: 5px 15px; height: auto;">Visit uplink7.com</a>
						</div>
					</div>
                </div>
            </div>
			<?php submit_button( 'Save Settings' ); ?>
        </form>

        <div style="background: #fff; border: 2px solid #d63638; padding: 25px; margin-top: 20px; border-radius: 12px; display: flex; justify-content: space-between; align-items: center; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1);">
            <div>
                <h3 style="margin:0; color:#d63638;">Support & Documentation</h3>
                <p style="margin:5px 0 0; color:#666;">Need help? Read the documentation.</p>
            </div>
            <a href="<?php echo esc_url( 'https://www.uplink7.com/dokumentation/uplink7-automatic-translator-fuer-google-translate-wordpress-plugin/' ); ?>" target="_blank" class="button button-primary" style="background:#d63638; border-color:#d63638; padding: 8px 20px; height: auto;">View Documentation</a>
        </div>
    </div>
	<?php
}

/**
 * 9. CLEANUP ON UNINSTALL
 */
register_uninstall_hook( __FILE__, 'u7at_uninstall' );
function u7at_uninstall() {
	$options = [
		'u7at_default_lang', 'u7at_auto_switch', 'u7at_native_names',
		'u7at_flag_mode', 'u7at_bg_color', 'u7at_text_color', 'u7at_text_size',
		'u7at_border_radius', 'u7at_border_width', 'u7at_inj_header_pos', 'u7at_inj_footer_pos',
		'u7at_inj_float_pos'
	];
	foreach ( $options as $opt ) {
		delete_option( $opt );
	}
}