<?php

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

class AIEO_DMM_Button_Styles extends AIEO_DMM_Module_Base {

	const OPTION_KEY = 'aieo_dmm_button_styles';

	public function get_id()       { return 'button_styles'; }
	public function get_label()    { return __( 'Button Styles', 'ai-eshop-optimizer' ); }
	public function get_priority() { return 15; } 
	public function get_group()    { return 'tools'; }

	
	public static function defaults() {
		return array(
			
			'bg'              => '#212121',
			'fg'              => '#ffffff',
			'border'          => '#212121',
			'border_w'        => 0,
			'radius'          => 4,
			'bg_hover'        => '#404040',
			'fg_hover'        => '#ffffff',
			'border_hover'    => '#404040',
			
			
			'pad_t'           => 12,
			'pad_r'           => 24,
			'pad_b'           => 12,
			'pad_l'           => 24,
			'min_w'           => 0,
			'max_w'           => 0,
			'min_h'           => 0,
			'max_h'           => 0,
			
			'glass'           => 0,
			'glass_opacity'   => 70,
			'glass_blur'      => 10,
			
			'sec_bg'          => '#e9e6e2',
			'sec_fg'          => '#1a1a1a',
			'sec_bg_hover'    => '#dfdbd4',
			
			
			
			'sec_border'        => '#e9e6e2',
			'sec_border_w'      => 0,
			'sec_fg_hover'      => '#1a1a1a',
			'sec_border_hover'  => '#dfdbd4',
			'sec_pad_t'       => 10,
			'sec_pad_r'       => 18,
			'sec_pad_b'       => 10,
			'sec_pad_l'       => 18,
			'sec_min_w'       => 0,
			'sec_max_w'       => 0,
			'sec_min_h'       => 0,
			'sec_max_h'       => 0,
			'sec_glass'         => 0,
			'sec_glass_opacity' => 70,
			'sec_glass_blur'    => 10,
			
			
			'arch_bg'           => '#212121',
			'arch_fg'           => '#ffffff',
			'arch_border'       => '#212121',
			'arch_border_w'     => 0,
			'arch_radius'       => 4,
			'arch_bg_hover'     => '#404040',
			'arch_fg_hover'     => '#ffffff',
			'arch_border_hover' => '#404040',
			'arch_pad_t'        => 8,
			'arch_pad_r'        => 16,
			'arch_pad_b'        => 8,
			'arch_pad_l'        => 16,
			'arch_min_w'        => 0,
			'arch_max_w'        => 0,
			'arch_min_h'        => 0,
			'arch_max_h'        => 0,
			'arch_glass'         => 0,
			'arch_glass_opacity' => 70,
			'arch_glass_blur'    => 10,
			'style_archive'     => 1,
			
			'style_main_atc'  => 1,
		);
	}

	public function get_settings() {
		$stored = get_option( self::OPTION_KEY, array() );
		if ( ! is_array( $stored ) ) { $stored = array(); }
		return array_merge( self::defaults(), $stored );
	}

	public function register_hooks() {
		add_action( 'wp_ajax_aieo_dmm_save_button_styles', array( $this, 'ajax_save' ) );
		
		
		
		
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend' ), 5 );
		
		add_filter( 'body_class', array( $this, 'maybe_atc_body_class' ) );
	}

	
	
	

	public function enqueue_admin_assets() {
		if ( ! class_exists( 'AIEO_Admin_Color' ) && defined( 'AIEO_DIR' ) && file_exists( AIEO_DIR . 'includes/class-aieo-admin-color.php' ) ) {
			require_once AIEO_DIR . 'includes/class-aieo-admin-color.php';
		}
		if ( class_exists( 'AIEO_Admin_Color' ) ) {
			AIEO_Admin_Color::enqueue();
		} else {
			wp_enqueue_style( 'wp-color-picker' );
			wp_enqueue_script( 'wp-color-picker' );
			wp_add_inline_script( 'wp-color-picker', 'jQuery(function($){$(".aieo-dmm-btn-color").wpColorPicker();});' );
		}
	}

	public function ajax_save() {
		AIEO_DMM::verify_ajax_request();
		if ( ! current_user_can( 'install_plugins' ) ) {
			wp_send_json_error( array( 'message' => __( 'Insufficient permissions.', 'ai-eshop-optimizer' ) ) );
		}
		$d   = self::defaults();
		$hex = function ( $key, $fallback ) {
			
			
			$raw = isset( $_POST[ $key ] ) ? wp_unslash( $_POST[ $key ] ) : ''; 
			$v   = class_exists( 'AIEO_Admin_Color' ) ? AIEO_Admin_Color::sanitize( $raw ) : (string) sanitize_hex_color( $raw );
			return $v !== '' ? $v : $fallback;
		};
		$out = array(
			'bg'           => $hex( 'bg', $d['bg'] ),
			'fg'           => $hex( 'fg', $d['fg'] ),
			'border'       => $hex( 'border', $d['border'] ),
			'border_w'     => isset( $_POST['border_w'] ) ? max( 0, min( 10, (int) $_POST['border_w'] ) ) : $d['border_w'],
			'radius'       => isset( $_POST['radius'] ) ? max( 0, min( 48, (int) $_POST['radius'] ) ) : $d['radius'],
			'pad_t'        => isset( $_POST['pad_t'] ) ? max( 0, min( 80,   (int) $_POST['pad_t'] ) ) : $d['pad_t'],
			'pad_r'        => isset( $_POST['pad_r'] ) ? max( 0, min( 120,  (int) $_POST['pad_r'] ) ) : $d['pad_r'],
			'pad_b'        => isset( $_POST['pad_b'] ) ? max( 0, min( 80,   (int) $_POST['pad_b'] ) ) : $d['pad_b'],
			'pad_l'        => isset( $_POST['pad_l'] ) ? max( 0, min( 120,  (int) $_POST['pad_l'] ) ) : $d['pad_l'],
			'min_w'        => isset( $_POST['min_w'] ) ? max( 0, min( 1000, (int) $_POST['min_w'] ) ) : $d['min_w'],
			'max_w'        => isset( $_POST['max_w'] ) ? max( 0, min( 1000, (int) $_POST['max_w'] ) ) : $d['max_w'],
			'min_h'        => isset( $_POST['min_h'] ) ? max( 0, min( 400,  (int) $_POST['min_h'] ) ) : $d['min_h'],
			'max_h'        => isset( $_POST['max_h'] ) ? max( 0, min( 400,  (int) $_POST['max_h'] ) ) : $d['max_h'],
			'glass'         => empty( $_POST['glass'] ) ? 0 : 1,
			'glass_opacity' => isset( $_POST['glass_opacity'] ) ? max( 0, min( 100, (int) $_POST['glass_opacity'] ) ) : $d['glass_opacity'],
			'glass_blur'    => isset( $_POST['glass_blur'] ) ? max( 0, min( 40, (int) $_POST['glass_blur'] ) ) : $d['glass_blur'],
			'bg_hover'     => $hex( 'bg_hover', $d['bg_hover'] ),
			'fg_hover'     => $hex( 'fg_hover', $d['fg_hover'] ),
			'border_hover' => $hex( 'border_hover', $d['border_hover'] ),
			'sec_bg'       => $hex( 'sec_bg', $d['sec_bg'] ),
			'sec_fg'       => $hex( 'sec_fg', $d['sec_fg'] ),
			'sec_bg_hover' => $hex( 'sec_bg_hover', $d['sec_bg_hover'] ),
			'sec_border'       => $hex( 'sec_border', $d['sec_border'] ),
			'sec_border_w'     => isset( $_POST['sec_border_w'] ) ? max( 0, min( 10, (int) $_POST['sec_border_w'] ) ) : $d['sec_border_w'],
			'sec_fg_hover'     => $hex( 'sec_fg_hover', $d['sec_fg_hover'] ),
			'sec_border_hover' => $hex( 'sec_border_hover', $d['sec_border_hover'] ),
			'sec_pad_t'    => isset( $_POST['sec_pad_t'] ) ? max( 0, min( 80,   (int) $_POST['sec_pad_t'] ) ) : $d['sec_pad_t'],
			'sec_pad_r'    => isset( $_POST['sec_pad_r'] ) ? max( 0, min( 120,  (int) $_POST['sec_pad_r'] ) ) : $d['sec_pad_r'],
			'sec_pad_b'    => isset( $_POST['sec_pad_b'] ) ? max( 0, min( 80,   (int) $_POST['sec_pad_b'] ) ) : $d['sec_pad_b'],
			'sec_pad_l'    => isset( $_POST['sec_pad_l'] ) ? max( 0, min( 120,  (int) $_POST['sec_pad_l'] ) ) : $d['sec_pad_l'],
			'sec_min_w'    => isset( $_POST['sec_min_w'] ) ? max( 0, min( 1000, (int) $_POST['sec_min_w'] ) ) : $d['sec_min_w'],
			'sec_max_w'    => isset( $_POST['sec_max_w'] ) ? max( 0, min( 1000, (int) $_POST['sec_max_w'] ) ) : $d['sec_max_w'],
			'sec_min_h'    => isset( $_POST['sec_min_h'] ) ? max( 0, min( 400,  (int) $_POST['sec_min_h'] ) ) : $d['sec_min_h'],
			'sec_max_h'    => isset( $_POST['sec_max_h'] ) ? max( 0, min( 400,  (int) $_POST['sec_max_h'] ) ) : $d['sec_max_h'],
			'sec_glass'         => empty( $_POST['sec_glass'] ) ? 0 : 1,
			'sec_glass_opacity' => isset( $_POST['sec_glass_opacity'] ) ? max( 0, min( 100, (int) $_POST['sec_glass_opacity'] ) ) : $d['sec_glass_opacity'],
			'sec_glass_blur'    => isset( $_POST['sec_glass_blur'] ) ? max( 0, min( 40, (int) $_POST['sec_glass_blur'] ) ) : $d['sec_glass_blur'],
			'arch_bg'           => $hex( 'arch_bg', $d['arch_bg'] ),
			'arch_fg'           => $hex( 'arch_fg', $d['arch_fg'] ),
			'arch_border'       => $hex( 'arch_border', $d['arch_border'] ),
			'arch_border_w'     => isset( $_POST['arch_border_w'] ) ? max( 0, min( 10, (int) $_POST['arch_border_w'] ) ) : $d['arch_border_w'],
			'arch_radius'       => isset( $_POST['arch_radius'] ) ? max( 0, min( 48, (int) $_POST['arch_radius'] ) ) : $d['arch_radius'],
			'arch_pad_t'        => isset( $_POST['arch_pad_t'] ) ? max( 0, min( 80,   (int) $_POST['arch_pad_t'] ) ) : $d['arch_pad_t'],
			'arch_pad_r'        => isset( $_POST['arch_pad_r'] ) ? max( 0, min( 120,  (int) $_POST['arch_pad_r'] ) ) : $d['arch_pad_r'],
			'arch_pad_b'        => isset( $_POST['arch_pad_b'] ) ? max( 0, min( 80,   (int) $_POST['arch_pad_b'] ) ) : $d['arch_pad_b'],
			'arch_pad_l'        => isset( $_POST['arch_pad_l'] ) ? max( 0, min( 120,  (int) $_POST['arch_pad_l'] ) ) : $d['arch_pad_l'],
			'arch_min_w'        => isset( $_POST['arch_min_w'] ) ? max( 0, min( 1000, (int) $_POST['arch_min_w'] ) ) : $d['arch_min_w'],
			'arch_max_w'        => isset( $_POST['arch_max_w'] ) ? max( 0, min( 1000, (int) $_POST['arch_max_w'] ) ) : $d['arch_max_w'],
			'arch_min_h'        => isset( $_POST['arch_min_h'] ) ? max( 0, min( 400,  (int) $_POST['arch_min_h'] ) ) : $d['arch_min_h'],
			'arch_max_h'        => isset( $_POST['arch_max_h'] ) ? max( 0, min( 400,  (int) $_POST['arch_max_h'] ) ) : $d['arch_max_h'],
			'arch_glass'         => empty( $_POST['arch_glass'] ) ? 0 : 1,
			'arch_glass_opacity' => isset( $_POST['arch_glass_opacity'] ) ? max( 0, min( 100, (int) $_POST['arch_glass_opacity'] ) ) : $d['arch_glass_opacity'],
			'arch_glass_blur'    => isset( $_POST['arch_glass_blur'] ) ? max( 0, min( 40, (int) $_POST['arch_glass_blur'] ) ) : $d['arch_glass_blur'],
			'arch_bg_hover'     => $hex( 'arch_bg_hover', $d['arch_bg_hover'] ),
			'arch_fg_hover'     => $hex( 'arch_fg_hover', $d['arch_fg_hover'] ),
			'arch_border_hover' => $hex( 'arch_border_hover', $d['arch_border_hover'] ),
			'style_archive'     => empty( $_POST['style_archive'] ) ? 0 : 1,
			'style_main_atc' => empty( $_POST['style_main_atc'] ) ? 0 : 1,
		);
		update_option( self::OPTION_KEY, $out, false );
		wp_send_json_success( array( 'message' => __( 'Button styles saved.', 'ai-eshop-optimizer' ) ) );
	}

	public function render_subtab() {
		$s     = $this->get_settings();
		$nonce = wp_create_nonce( AIEO_DMM::NONCE_ACTION );
		$ajax  = admin_url( 'admin-ajax.php' );

		$color_row = function ( $label, $name, $val ) {
			echo '<tr><th scope="row">' . esc_html( $label ) . '</th><td>';
			echo '<input type="text" name="' . esc_attr( $name ) . '" value="' . esc_attr( $val ) . '" class="aieo-dmm-btn-color" data-default-color="' . esc_attr( $val ) . '">';
			echo '</td></tr>';
		};
		$num_row = function ( $label, $name, $val, $max, $suffix ) {
			echo '<tr><th scope="row">' . esc_html( $label ) . '</th><td>';
			echo '<input type="number" name="' . esc_attr( $name ) . '" value="' . esc_attr( (int) $val ) . '" min="0" max="' . (int) $max . '" step="1" style="width:90px;"> ' . esc_html( $suffix );
			echo '</td></tr>';
		};
		$glass_block = function ( $prefix, $on, $opacity, $blur ) {
			echo '<p style="margin:8px 0 0;font-size:13px;">';
			echo '<label><input type="checkbox" name="' . esc_attr( $prefix . 'glass' ) . '" value="1" ' . checked( ! empty( $on ), true, false ) . '> ';
			echo esc_html__( 'Glass effect (transparent background + backdrop blur)', 'ai-eshop-optimizer' ) . '</label> &nbsp;&nbsp; ';
			echo esc_html__( 'Opacity', 'ai-eshop-optimizer' ) . ' <input type="number" name="' . esc_attr( $prefix . 'glass_opacity' ) . '" value="' . (int) $opacity . '" min="0" max="100" step="1" style="width:64px;">% &nbsp; ';
			echo esc_html__( 'Blur', 'ai-eshop-optimizer' ) . ' <input type="number" name="' . esc_attr( $prefix . 'glass_blur' ) . '" value="' . (int) $blur . '" min="0" max="40" step="1" style="width:64px;">px';
			echo '</p>';
		};
		?>
		<div class="aieo-dmm-section">
			<div class="aieo-dmm-card" id="aieo-card-button-styles">
				<h3 style="margin-top:0;"><?php esc_html_e( 'Button styles', 'ai-eshop-optimizer' ); ?></h3>
				<p class="description" style="max-width:760px;">
					<?php esc_html_e( 'One master look for every AIEO storefront button — applied as the default to all of them at once. Each button that has its own colour control (Waitlist, Complementary, Sticky Add-to-Cart, Side-Cart) still overrides this master when you set it; leave those untouched and they follow the master here.', 'ai-eshop-optimizer' ); ?>
				</p>

				<h4 style="margin:18px 0 4px;"><?php esc_html_e( 'Primary buttons (Add-to-Cart, Checkout, Notify Me, Add selected…)', 'ai-eshop-optimizer' ); ?></h4>
				<table class="form-table" role="presentation"><tbody>
					<?php
					$color_row( __( 'Background', 'ai-eshop-optimizer' ),        'bg',           $s['bg'] );
					$color_row( __( 'Text colour', 'ai-eshop-optimizer' ),       'fg',           $s['fg'] );
					$color_row( __( 'Border colour', 'ai-eshop-optimizer' ),     'border',       $s['border'] );
					$num_row(   __( 'Border width', 'ai-eshop-optimizer' ),      'border_w',     $s['border_w'], 10, 'px' );
					$num_row(   __( 'Corner radius', 'ai-eshop-optimizer' ),     'radius',       $s['radius'],   48, 'px' );
					$num_row(   __( 'Padding top', 'ai-eshop-optimizer' ),      'pad_t',        $s['pad_t'],    80,   'px' );
					$num_row(   __( 'Padding right', 'ai-eshop-optimizer' ),    'pad_r',        $s['pad_r'],    120,  'px' );
					$num_row(   __( 'Padding bottom', 'ai-eshop-optimizer' ),   'pad_b',        $s['pad_b'],    80,   'px' );
					$num_row(   __( 'Padding left', 'ai-eshop-optimizer' ),     'pad_l',        $s['pad_l'],    120,  'px' );
					$num_row(   __( 'Min width', 'ai-eshop-optimizer' ),        'min_w',        $s['min_w'],    1000, 'px (0 = auto)' );
					$num_row(   __( 'Max width', 'ai-eshop-optimizer' ),        'max_w',        $s['max_w'],    1000, 'px (0 = none)' );
					$num_row(   __( 'Min height', 'ai-eshop-optimizer' ),       'min_h',        $s['min_h'],    400,  'px (0 = auto)' );
					$num_row(   __( 'Max height', 'ai-eshop-optimizer' ),       'max_h',        $s['max_h'],    400,  'px (0 = none)' );
					$color_row( __( 'Hover background', 'ai-eshop-optimizer' ),  'bg_hover',     $s['bg_hover'] );
					$color_row( __( 'Hover text colour', 'ai-eshop-optimizer' ), 'fg_hover',     $s['fg_hover'] );
					$color_row( __( 'Hover border colour', 'ai-eshop-optimizer' ),'border_hover',$s['border_hover'] );
					?>
				</tbody></table>
				<?php $glass_block( '', $s['glass'], $s['glass_opacity'], $s['glass_blur'] ); ?>

				<h4 style="margin:18px 0 4px;"><?php esc_html_e( 'Secondary buttons (View Cart, Save / share cart, Apply coupon…)', 'ai-eshop-optimizer' ); ?></h4>
				<table class="form-table" role="presentation"><tbody>
					<?php
					$color_row( __( 'Background', 'ai-eshop-optimizer' ),         'sec_bg',           $s['sec_bg'] );
					$color_row( __( 'Text colour', 'ai-eshop-optimizer' ),        'sec_fg',           $s['sec_fg'] );
					$color_row( __( 'Border colour', 'ai-eshop-optimizer' ),      'sec_border',       $s['sec_border'] );
					$num_row(   __( 'Border width', 'ai-eshop-optimizer' ),       'sec_border_w',     $s['sec_border_w'], 10, 'px' );
					$color_row( __( 'Hover background', 'ai-eshop-optimizer' ),    'sec_bg_hover',     $s['sec_bg_hover'] );
					$color_row( __( 'Hover text colour', 'ai-eshop-optimizer' ),   'sec_fg_hover',     $s['sec_fg_hover'] );
					$color_row( __( 'Hover border colour', 'ai-eshop-optimizer' ), 'sec_border_hover', $s['sec_border_hover'] );
					$num_row(   __( 'Padding top', 'ai-eshop-optimizer' ),      'sec_pad_t',    $s['sec_pad_t'], 80,   'px' );
					$num_row(   __( 'Padding right', 'ai-eshop-optimizer' ),    'sec_pad_r',    $s['sec_pad_r'], 120,  'px' );
					$num_row(   __( 'Padding bottom', 'ai-eshop-optimizer' ),   'sec_pad_b',    $s['sec_pad_b'], 80,   'px' );
					$num_row(   __( 'Padding left', 'ai-eshop-optimizer' ),     'sec_pad_l',    $s['sec_pad_l'], 120,  'px' );
					$num_row(   __( 'Min width', 'ai-eshop-optimizer' ),        'sec_min_w',    $s['sec_min_w'], 1000, 'px (0 = auto)' );
					$num_row(   __( 'Max width', 'ai-eshop-optimizer' ),        'sec_max_w',    $s['sec_max_w'], 1000, 'px (0 = none)' );
					$num_row(   __( 'Min height', 'ai-eshop-optimizer' ),       'sec_min_h',    $s['sec_min_h'], 400,  'px (0 = auto)' );
					$num_row(   __( 'Max height', 'ai-eshop-optimizer' ),       'sec_max_h',    $s['sec_max_h'], 400,  'px (0 = none)' );
					?>
				</tbody></table>
				<?php $glass_block( 'sec_', $s['sec_glass'], $s['sec_glass_opacity'], $s['sec_glass_blur'] ); ?>

				<h4 style="margin:18px 0 4px;"><?php esc_html_e( 'Archive / product-card buttons (shop & category pages)', 'ai-eshop-optimizer' ); ?></h4>
				<table class="form-table" role="presentation"><tbody>
					<?php
					$color_row( __( 'Background', 'ai-eshop-optimizer' ),          'arch_bg',           $s['arch_bg'] );
					$color_row( __( 'Text colour', 'ai-eshop-optimizer' ),         'arch_fg',           $s['arch_fg'] );
					$color_row( __( 'Border colour', 'ai-eshop-optimizer' ),       'arch_border',       $s['arch_border'] );
					$num_row(   __( 'Border width', 'ai-eshop-optimizer' ),        'arch_border_w',     $s['arch_border_w'], 10, 'px' );
					$num_row(   __( 'Corner radius', 'ai-eshop-optimizer' ),       'arch_radius',       $s['arch_radius'],   48, 'px' );
					$num_row(   __( 'Padding top', 'ai-eshop-optimizer' ),        'arch_pad_t',        $s['arch_pad_t'],    80,   'px' );
					$num_row(   __( 'Padding right', 'ai-eshop-optimizer' ),      'arch_pad_r',        $s['arch_pad_r'],    120,  'px' );
					$num_row(   __( 'Padding bottom', 'ai-eshop-optimizer' ),     'arch_pad_b',        $s['arch_pad_b'],    80,   'px' );
					$num_row(   __( 'Padding left', 'ai-eshop-optimizer' ),       'arch_pad_l',        $s['arch_pad_l'],    120,  'px' );
					$num_row(   __( 'Min width', 'ai-eshop-optimizer' ),          'arch_min_w',        $s['arch_min_w'],    1000, 'px (0 = auto)' );
					$num_row(   __( 'Max width', 'ai-eshop-optimizer' ),          'arch_max_w',        $s['arch_max_w'],    1000, 'px (0 = none)' );
					$num_row(   __( 'Min height', 'ai-eshop-optimizer' ),         'arch_min_h',        $s['arch_min_h'],    400,  'px (0 = auto)' );
					$num_row(   __( 'Max height', 'ai-eshop-optimizer' ),         'arch_max_h',        $s['arch_max_h'],    400,  'px (0 = none)' );
					$color_row( __( 'Hover background', 'ai-eshop-optimizer' ),    'arch_bg_hover',     $s['arch_bg_hover'] );
					$color_row( __( 'Hover text colour', 'ai-eshop-optimizer' ),   'arch_fg_hover',     $s['arch_fg_hover'] );
					$color_row( __( 'Hover border colour', 'ai-eshop-optimizer' ), 'arch_border_hover', $s['arch_border_hover'] );
					?>
				</tbody></table>
				<?php $glass_block( 'arch_', $s['arch_glass'], $s['arch_glass_opacity'], $s['arch_glass_blur'] ); ?>
				<p style="margin:8px 0 0;">
					<label><input type="checkbox" name="style_archive" value="1" <?php checked( ! empty( $s['style_archive'] ) ); ?>>
						<?php esc_html_e( 'Apply this archive master to product-card add-to-cart buttons (shop / category loops)', 'ai-eshop-optimizer' ); ?></label>
				</p>

				<p style="margin:10px 0 0;">
					<label><input type="checkbox" name="style_main_atc" value="1" <?php checked( ! empty( $s['style_main_atc'] ) ); ?>>
						<?php esc_html_e( 'Also style the theme’s main Add-to-Cart button (single product page)', 'ai-eshop-optimizer' ); ?></label>
					<span class="description" style="display:block;margin-top:2px;"><?php esc_html_e( 'Off = leave the main Add-to-Cart to your theme. On = the master look applies to it too.', 'ai-eshop-optimizer' ); ?></span>
				</p>

				<p class="submit" style="margin-top:14px;">
					<button type="button" class="button button-primary" id="aieo-btn-styles-save"><?php esc_html_e( 'Save button styles', 'ai-eshop-optimizer' ); ?></button>
					<span id="aieo-btn-styles-status" style="margin-left:10px;color:#646970;font-size:13px;"></span>
				</p>
				<p class="description" style="margin-top:6px;"><?php esc_html_e( 'After saving, purge any page cache to see the change on cached storefront pages.', 'ai-eshop-optimizer' ); ?></p>
			</div>
		</div>
		<script>
		(function () {
			var btn = document.getElementById('aieo-btn-styles-save');
			if (!btn) { return; }
			btn.addEventListener('click', function () {
				var card = document.getElementById('aieo-card-button-styles');
				var status = document.getElementById('aieo-btn-styles-status');
				var fd = new FormData();
				fd.set('action', 'aieo_dmm_save_button_styles');
				fd.set('nonce', <?php echo wp_json_encode( $nonce ); ?>);
				card.querySelectorAll('input').forEach(function (el) {
					if (el.type === 'checkbox') { if (el.checked) { fd.set(el.name, '1'); } }
					else { fd.set(el.name, el.value); }
				});
				status.textContent = <?php echo wp_json_encode( __( 'Saving…', 'ai-eshop-optimizer' ) ); ?>;
				fetch(<?php echo wp_json_encode( $ajax ); ?>, { method: 'POST', credentials: 'same-origin', body: fd })
					.then(function (r) { return r.json(); })
					.then(function (j) {
						status.textContent = (j && j.data && j.data.message) ? j.data.message : 'Saved.';
						status.style.color = (j && j.success) ? '#15803d' : '#b91c1c';
					})
					.catch(function () { status.textContent = 'Save failed.'; status.style.color = '#b91c1c'; });
			});
		})();
		</script>
		<?php
	}

	
	
	

	
	public function maybe_atc_body_class( $classes ) {
		$s = $this->get_settings();
		if ( ! empty( $s['style_main_atc'] ) ) { $classes[] = 'aieo-btn-master-atc'; }
		if ( ! empty( $s['style_archive'] ) )  { $classes[] = 'aieo-btn-master-archive'; }
		return is_array( $classes ) ? $classes : (array) $classes;
	}

	
	public function enqueue_frontend() {
		if ( is_admin() ) { return; }
		$s = $this->get_settings();

		
		
		$wh = function ( $v, $none ) { $v = (int) $v; return $v > 0 ? $v . 'px' : $none; };
		
		
		$mix = function ( $color, $on, $opacity ) {
			return ! empty( $on ) ? sprintf( 'color-mix(in srgb, %s %d%%, transparent)', $color, max( 0, min( 100, (int) $opacity ) ) ) : $color;
		};
		$bd  = function ( $on, $blur ) {
			return ! empty( $on ) ? sprintf( 'blur(%dpx)', max( 0, min( 40, (int) $blur ) ) ) : 'none';
		};

		$vars = sprintf(
			':root{'
			. '--aieo-btn-bg:%s;--aieo-btn-fg:%s;--aieo-btn-border:%s;--aieo-btn-border-w:%dpx;--aieo-btn-radius:%dpx;'
			. '--aieo-btn-bg-hover:%s;--aieo-btn-fg-hover:%s;--aieo-btn-border-hover:%s;'
			. '--aieo-btn-pad-t:%dpx;--aieo-btn-pad-r:%dpx;--aieo-btn-pad-b:%dpx;--aieo-btn-pad-l:%dpx;'
			. '--aieo-btn-min-w:%s;--aieo-btn-max-w:%s;--aieo-btn-min-h:%s;--aieo-btn-max-h:%s;'
			. '--aieo-btn-backdrop:%s;'
			. '--aieo-btn-secondary-bg:%s;--aieo-btn-secondary-fg:%s;--aieo-btn-secondary-bg-hover:%s;'
			. '--aieo-btn-secondary-border:%s;--aieo-btn-secondary-border-w:%dpx;--aieo-btn-secondary-fg-hover:%s;--aieo-btn-secondary-border-hover:%s;'
			. '--aieo-btn-secondary-pad-t:%dpx;--aieo-btn-secondary-pad-r:%dpx;--aieo-btn-secondary-pad-b:%dpx;--aieo-btn-secondary-pad-l:%dpx;'
			. '--aieo-btn-secondary-min-w:%s;--aieo-btn-secondary-max-w:%s;--aieo-btn-secondary-min-h:%s;--aieo-btn-secondary-max-h:%s;'
			. '--aieo-btn-secondary-backdrop:%s;'
			. '--aieo-btn-archive-bg:%s;--aieo-btn-archive-fg:%s;--aieo-btn-archive-border:%s;--aieo-btn-archive-border-w:%dpx;--aieo-btn-archive-radius:%dpx;'
			. '--aieo-btn-archive-bg-hover:%s;--aieo-btn-archive-fg-hover:%s;--aieo-btn-archive-border-hover:%s;'
			. '--aieo-btn-archive-pad-t:%dpx;--aieo-btn-archive-pad-r:%dpx;--aieo-btn-archive-pad-b:%dpx;--aieo-btn-archive-pad-l:%dpx;'
			. '--aieo-btn-archive-min-w:%s;--aieo-btn-archive-max-w:%s;--aieo-btn-archive-min-h:%s;--aieo-btn-archive-max-h:%s;'
			. '--aieo-btn-archive-backdrop:%s;'
			. '}',
			$mix( $s['bg'], $s['glass'], $s['glass_opacity'] ), $s['fg'], $s['border'], (int) $s['border_w'], (int) $s['radius'],
			$mix( $s['bg_hover'], $s['glass'], $s['glass_opacity'] ), $s['fg_hover'], $s['border_hover'],
			(int) $s['pad_t'], (int) $s['pad_r'], (int) $s['pad_b'], (int) $s['pad_l'],
			$wh( $s['min_w'], 'auto' ), $wh( $s['max_w'], 'none' ), $wh( $s['min_h'], 'auto' ), $wh( $s['max_h'], 'none' ),
			$bd( $s['glass'], $s['glass_blur'] ),
			$mix( $s['sec_bg'], $s['sec_glass'], $s['sec_glass_opacity'] ), $s['sec_fg'], $mix( $s['sec_bg_hover'], $s['sec_glass'], $s['sec_glass_opacity'] ),
			$s['sec_border'], (int) $s['sec_border_w'], $s['sec_fg_hover'], $s['sec_border_hover'],
			(int) $s['sec_pad_t'], (int) $s['sec_pad_r'], (int) $s['sec_pad_b'], (int) $s['sec_pad_l'],
			$wh( $s['sec_min_w'], 'auto' ), $wh( $s['sec_max_w'], 'none' ), $wh( $s['sec_min_h'], 'auto' ), $wh( $s['sec_max_h'], 'none' ),
			$bd( $s['sec_glass'], $s['sec_glass_blur'] ),
			$mix( $s['arch_bg'], $s['arch_glass'], $s['arch_glass_opacity'] ), $s['arch_fg'], $s['arch_border'], (int) $s['arch_border_w'], (int) $s['arch_radius'],
			$mix( $s['arch_bg_hover'], $s['arch_glass'], $s['arch_glass_opacity'] ), $s['arch_fg_hover'], $s['arch_border_hover'],
			(int) $s['arch_pad_t'], (int) $s['arch_pad_r'], (int) $s['arch_pad_b'], (int) $s['arch_pad_l'],
			$wh( $s['arch_min_w'], 'auto' ), $wh( $s['arch_max_w'], 'none' ), $wh( $s['arch_min_h'], 'auto' ), $wh( $s['arch_max_h'], 'none' ),
			$bd( $s['arch_glass'], $s['arch_glass_blur'] )
		);

		
		
		
		$css_path = AIEO_DIR . 'assets/css/aieo-button-styles.css';
		$ver      = file_exists( $css_path ) ? (string) filemtime( $css_path ) : ( defined( 'AIEO_VERSION' ) ? AIEO_VERSION : '1.0' );
		wp_enqueue_style( 'aieo-button-styles', AIEO_URI . 'assets/css/aieo-button-styles.css', array(), $ver );

		
		
		wp_register_style( 'aieo-button-styles-vars', false, array(), defined( 'AIEO_VERSION' ) ? AIEO_VERSION : '1.0' );
		wp_enqueue_style( 'aieo-button-styles-vars' );
		wp_add_inline_style( 'aieo-button-styles-vars', $vars );
	}
}
