<?php
/**
 * FirstRunWizard — three-screen onboarding for brand-new installs.
 *
 * @package CartCue\Onboarding
 */

namespace CartCue\Onboarding;

use CartCue\CampaignRepository;
use CartCue\Context;
use CartCue\DB;
use CartCue\Migrations;
use CartCue\PlayValidator;
use CartCue\Plugin;
use CartCue\RecipeNormalizer;
use CartCue\Recipes;
use CartCue\Triggers\GhostTrigger;

defined( 'ABSPATH' ) || exit;

final class FirstRunWizard {

	private const MIGRATION_KEY = 'first_run_wizard_complete';
	private const NONCE_ACTION  = 'cartcue_first_run_wizard';
	private const PAGE_SLUG     = 'cartcue';
	private const LOCK_TTL      = 30;

	/**
	 * @return array<string, array{icon: string, recipe: string, title: string, desc: string, sentence: string}>
	 */
	private static function goals(): array {
		return [
			'cart_recovery'  => [
				'icon'     => 'ti-shopping-cart-off',
				'recipe'   => 'cart_recovery',
				'title'    => __( 'Recover abandoned carts', 'cartcue-sales-automation' ),
				'desc'     => __( 'Send a friendly reminder to shoppers who leave items behind.', 'cartcue-sales-automation' ),
				'sentence' => __( 'When someone leaves items in their cart, wait 1 hour, then send a recovery email.', 'cartcue-sales-automation' ),
			],
			'welcome'        => [
				'icon'     => 'ti-confetti',
				'recipe'   => 'first_order_welcome',
				'title'    => __( 'Welcome new customers', 'cartcue-sales-automation' ),
				'desc'     => __( 'Thank shoppers for their first order and set the tone for the next one.', 'cartcue-sales-automation' ),
				'sentence' => __( 'When a customer places their first order, send a personalised welcome email.', 'cartcue-sales-automation' ),
			],
			'win_back'       => [
				'icon'     => 'ti-mail-heart',
				'recipe'   => 'win_back',
				'title'    => __( 'Win back lapsed buyers', 'cartcue-sales-automation' ),
				'desc'     => __( 'Re-engage customers who have not ordered in a while.', 'cartcue-sales-automation' ),
				'sentence' => __( 'When a customer has not ordered in 60 days, send a "we miss you" email.', 'cartcue-sales-automation' ),
			],
			'review_request' => [
				'icon'     => 'ti-star',
				'recipe'   => 'review_request',
				'title'    => __( 'Ask for product reviews', 'cartcue-sales-automation' ),
				'desc'     => __( 'Request a review a few days after an order is completed.', 'cartcue-sales-automation' ),
				'sentence' => __( 'A few days after an order is completed, ask the customer for a review.', 'cartcue-sales-automation' ),
			],
		];
	}

	public static function init(): void {
		add_action( 'admin_post_cartcue_wizard_submit', [ __CLASS__, 'handle_submit' ] );
		add_action( 'admin_post_cartcue_wizard_skip', [ __CLASS__, 'handle_skip' ] );
	}

	public static function should_show(): bool {
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
			return false;
		}
		if ( Migrations::has_run( self::MIGRATION_KEY ) ) {
			return false;
		}

		global $wpdb;
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery
		$existing = (int) $wpdb->get_var(
			$wpdb->prepare( 'SELECT COUNT(*) FROM %i', DB::workflows() )
		);
		if ( $existing > 0 ) {
			Migrations::mark_run( self::MIGRATION_KEY );
			return false;
		}

		return self::available_goals_count() > 0;
	}

	public static function mark_complete(): void {
		Migrations::mark_run( self::MIGRATION_KEY );
	}

	public static function render(): void {
		// phpcs:disable WordPress.Security.NonceVerification.Recommended -- read-only step routing.
		$step        = self::clamp_step( isset( $_GET['wizard_step'] ) ? (int) $_GET['wizard_step'] : 1 );
		$goal_slug   = isset( $_GET['goal'] ) ? sanitize_key( wp_unslash( $_GET['goal'] ) ) : '';
		$test_to_raw = isset( $_GET['test_to'] ) ? sanitize_email( wp_unslash( $_GET['test_to'] ) ) : '';
		$send_test   = isset( $_GET['send_test'] ) ? (bool) (int) $_GET['send_test'] : true;
		// phpcs:enable WordPress.Security.NonceVerification.Recommended

		$goals = self::goals();
		if ( ! isset( $goals[ $goal_slug ] ) ) {
			$goal_slug = '';
		}

		if ( $step > 1 && '' === $goal_slug ) {
			$step = 1;
		}

		$admin_email = sanitize_email( (string) get_option( 'admin_email', '' ) );
		$test_to     = $test_to_raw ?: $admin_email;
		?>
		<div class="ap-wrap cc-wizard" role="region" aria-label="<?php esc_attr_e( 'CartCue setup wizard', 'cartcue-sales-automation' ); ?>">
			<div class="cc-wizard__shell">
				<?php self::render_progress( $step ); ?>
				<?php
				switch ( $step ) {
					case 2:
						self::render_step_test( $goal_slug, $test_to );
						break;
					case 3:
						self::render_step_activate( $goal_slug, $test_to, $send_test );
						break;
					case 1:
					default:
						self::render_step_goal();
				}
				?>
				<?php self::render_skip_link(); ?>
			</div>
		</div>
		<?php
		self::render_inline_styles();
	}

	public static function handle_submit(): void {
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
			wp_die( esc_html__( 'Permission denied.', 'cartcue-sales-automation' ) );
		}
		check_admin_referer( self::NONCE_ACTION );

		$lock_key = 'cartcue_wizard_lock_' . get_current_user_id();
		if ( get_transient( $lock_key ) ) {
			wp_safe_redirect( self::base_url() );
			exit;
		}
		set_transient( $lock_key, 1, self::LOCK_TTL );

		if ( Migrations::has_run( self::MIGRATION_KEY ) ) {
			delete_transient( $lock_key );
			wp_safe_redirect( self::base_url() );
			exit;
		}

		$goal_slug = isset( $_POST['goal'] ) ? sanitize_key( wp_unslash( $_POST['goal'] ) ) : '';
		$test_to   = isset( $_POST['test_to'] ) ? sanitize_email( wp_unslash( $_POST['test_to'] ) ) : '';
		$send_test = ! empty( $_POST['send_test'] );
		$action    = isset( $_POST['next_action'] ) ? sanitize_key( wp_unslash( $_POST['next_action'] ) ) : '';

		$goals = self::goals();
		if ( ! isset( $goals[ $goal_slug ] ) ) {
			delete_transient( $lock_key );
			wp_safe_redirect( self::step_url( 1 ) );
			exit;
		}
		$goal = $goals[ $goal_slug ];

		if ( 'continue' === $action ) {
			delete_transient( $lock_key );
			wp_safe_redirect(
				self::step_url(
					3,
					[
						'goal'      => $goal_slug,
						'test_to'   => $test_to,
						'send_test' => $send_test ? '1' : '0',
					]
				)
			);
			exit;
		}

		if ( ! in_array( $action, [ 'activate', 'draft' ], true ) ) {
			delete_transient( $lock_key );
			wp_safe_redirect( self::step_url( 1 ) );
			exit;
		}

		if ( 'activate' === $action && ! Plugin::instance()->can_activate_more_plays( 0 ) ) {
			delete_transient( $lock_key );
			wp_safe_redirect(
				add_query_arg(
					[ 'page' => self::PAGE_SLUG, 'active_limit' => '1' ],
					admin_url( 'admin.php' )
				)
			);
			exit;
		}

		$status  = 'activate' === $action ? 'active' : 'paused';
		$play_id = self::create_play_from_recipe( $goal['recipe'], $status );

		if ( ! $play_id ) {
			self::mark_complete();
			delete_transient( $lock_key );
			wp_safe_redirect(
				add_query_arg(
					[ 'page' => self::PAGE_SLUG, 'action' => 'new', 'wizard_error' => '1' ],
					admin_url( 'admin.php' )
				)
			);
			exit;
		}

		if ( $send_test && is_email( $test_to ) ) {
			self::send_test_email( $play_id, $test_to );
		}

		self::mark_complete();
		delete_transient( $lock_key );

		if ( 'active' === $status ) {
			wp_safe_redirect(
				add_query_arg(
					[
						'page'              => self::PAGE_SLUG,
						'ap_activated'      => '1',
						'cartcue_highlight' => $play_id,
						'wizard_done'       => $play_id,
					],
					admin_url( 'admin.php' )
				)
			);
			exit;
		}

		wp_safe_redirect(
			add_query_arg(
				[
					'page'        => self::PAGE_SLUG,
					'action'      => 'edit',
					'id'          => $play_id,
					'wizard_done' => $play_id,
				],
				admin_url( 'admin.php' )
			)
		);
		exit;
	}

	public static function handle_skip(): void {
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
			wp_die( esc_html__( 'Permission denied.', 'cartcue-sales-automation' ) );
		}
		check_admin_referer( self::NONCE_ACTION );

		self::mark_complete();

		wp_safe_redirect(
			add_query_arg(
				[ 'page' => self::PAGE_SLUG, 'wizard_skipped' => '1' ],
				admin_url( 'admin.php' )
			)
		);
		exit;
	}

	private static function render_progress( int $step ): void {
		?>
		<div class="cc-wizard__progress" aria-label="<?php esc_attr_e( 'Setup progress', 'cartcue-sales-automation' ); ?>">
			<?php for ( $i = 1; $i <= 3; $i++ ) : ?>
			<span class="cc-wizard__dot <?php echo $i <= $step ? 'is-active' : ''; ?>"
				  aria-current="<?php echo $i === $step ? 'step' : 'false'; ?>"></span>
			<?php endfor; ?>
		</div>
		<?php
	}

	private static function render_step_goal(): void {
		$goals = self::goals();
		?>
		<div class="cc-wizard__step">
			<span class="cc-wizard__greeting">
				<i class="ti ti-sparkles" aria-hidden="true"></i>
				<?php esc_html_e( 'Welcome to CartCue', 'cartcue-sales-automation' ); ?>
			</span>

			<h1 class="cc-wizard__headline">
				<?php esc_html_e( 'What do you want to do first?', 'cartcue-sales-automation' ); ?>
			</h1>
			<p class="cc-wizard__sub">
				<?php esc_html_e( 'Pick a goal and CartCue will draft an automation for you. You can change everything later.', 'cartcue-sales-automation' ); ?>
			</p>

			<div class="cc-wizard__goals" role="list">
				<?php foreach ( $goals as $slug => $goal ) :
					if ( ! Recipes::get( $goal['recipe'] ) ) {
						continue;
					}
					$href = self::step_url( 2, [ 'goal' => $slug ] );
					?>
				<a class="cc-wizard__goal" href="<?php echo esc_url( $href ); ?>" role="listitem">
					<span class="cc-wizard__goal-icon" aria-hidden="true">
						<i class="ti <?php echo esc_attr( $goal['icon'] ); ?>"></i>
					</span>
					<span class="cc-wizard__goal-text">
						<strong><?php echo esc_html( $goal['title'] ); ?></strong>
						<span class="cc-wizard__goal-desc"><?php echo esc_html( $goal['desc'] ); ?></span>
					</span>
					<span class="cc-wizard__goal-arrow" aria-hidden="true">→</span>
				</a>
				<?php endforeach; ?>
			</div>
		</div>
		<?php
	}

	private static function render_step_test( string $goal_slug, string $test_to ): void {
		$goal = self::goals()[ $goal_slug ];
		?>
		<form class="cc-wizard__step"
			  method="post"
			  action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
			<?php wp_nonce_field( self::NONCE_ACTION ); ?>
			<input type="hidden" name="action" value="cartcue_wizard_submit">
			<input type="hidden" name="next_action" value="continue">
			<input type="hidden" name="goal" value="<?php echo esc_attr( $goal_slug ); ?>">

			<h1 class="cc-wizard__headline">
				<?php
				printf(
					/* translators: %s: goal title (e.g. "Recover abandoned carts") */
					esc_html__( 'Great — CartCue will set up %s.', 'cartcue-sales-automation' ),
					'<em>' . esc_html( $goal['title'] ) . '</em>'
				);
				?>
			</h1>
			<p class="cc-wizard__sub">
				<?php esc_html_e( "Here's what this automation will do:", 'cartcue-sales-automation' ); ?>
			</p>

			<div class="cc-wizard__sentence">
				<?php echo esc_html( $goal['sentence'] ); ?>
			</div>

			<div class="cc-wizard__field">
				<label for="cc-wizard-test-to" class="cc-wizard__label">
					<?php esc_html_e( 'Send a test email so you can see how it looks:', 'cartcue-sales-automation' ); ?>
				</label>
				<input type="email"
					   id="cc-wizard-test-to"
					   name="test_to"
					   class="cc-wizard__input"
					   value="<?php echo esc_attr( $test_to ); ?>"
					   autocomplete="email"
					   required>
				<label class="cc-wizard__checkbox">
					<input type="checkbox" name="send_test" value="1" checked>
					<span><?php esc_html_e( 'Send the test when I activate.', 'cartcue-sales-automation' ); ?></span>
				</label>
			</div>

			<div class="cc-wizard__nav">
				<a class="cc-wizard__back" href="<?php echo esc_url( self::step_url( 1 ) ); ?>">
					← <?php esc_html_e( 'Back', 'cartcue-sales-automation' ); ?>
				</a>
				<button type="submit" class="ap-btn ap-btn-primary ap-btn-lg cc-wizard__cta">
					<?php esc_html_e( 'Continue', 'cartcue-sales-automation' ); ?> →
				</button>
			</div>
		</form>
		<?php
	}

	private static function render_step_activate( string $goal_slug, string $test_to, bool $send_test ): void {
		$goal = self::goals()[ $goal_slug ];
		?>
		<form class="cc-wizard__step"
			  method="post"
			  action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
			<?php wp_nonce_field( self::NONCE_ACTION ); ?>
			<input type="hidden" name="action" value="cartcue_wizard_submit">
			<input type="hidden" name="goal" value="<?php echo esc_attr( $goal_slug ); ?>">
			<input type="hidden" name="test_to" value="<?php echo esc_attr( $test_to ); ?>">
			<input type="hidden" name="send_test" value="<?php echo $send_test ? '1' : '0'; ?>">

			<h1 class="cc-wizard__headline">
				<?php esc_html_e( 'Ready to go live?', 'cartcue-sales-automation' ); ?>
			</h1>
			<p class="cc-wizard__sub">
				<?php
				printf(
					/* translators: %s: goal title */
					esc_html__( '%s is ready. Switch it on now, or save it as a draft to fine-tune the message first.', 'cartcue-sales-automation' ),
					'<strong>' . esc_html( $goal['title'] ) . '</strong>'
				);
				?>
			</p>

			<div class="cc-wizard__choice-grid">
				<button type="submit"
						name="next_action"
						value="activate"
						class="cc-wizard__choice cc-wizard__choice--primary">
					<span class="cc-wizard__choice-icon" aria-hidden="true">
						<i class="ti ti-bolt"></i>
					</span>
					<span class="cc-wizard__choice-title">
						<?php esc_html_e( 'Activate now', 'cartcue-sales-automation' ); ?>
					</span>
					<span class="cc-wizard__choice-desc">
						<?php esc_html_e( 'Start running this automation immediately.', 'cartcue-sales-automation' ); ?>
					</span>
				</button>

				<button type="submit"
						name="next_action"
						value="draft"
						class="cc-wizard__choice">
					<span class="cc-wizard__choice-icon" aria-hidden="true">
						<i class="ti ti-edit"></i>
					</span>
					<span class="cc-wizard__choice-title">
						<?php esc_html_e( 'Save as draft', 'cartcue-sales-automation' ); ?>
					</span>
					<span class="cc-wizard__choice-desc">
						<?php esc_html_e( 'Open the editor and review the message before launching.', 'cartcue-sales-automation' ); ?>
					</span>
				</button>
			</div>

			<?php if ( $send_test && is_email( $test_to ) ) : ?>
			<p class="cc-wizard__test-hint">
				<i class="ti ti-mail-fast" aria-hidden="true"></i>
				<?php
				printf(
					/* translators: %s: email address */
					esc_html__( 'A test email will be sent to %s.', 'cartcue-sales-automation' ),
					'<strong>' . esc_html( $test_to ) . '</strong>'
				);
				?>
			</p>
			<?php endif; ?>

			<div class="cc-wizard__nav cc-wizard__nav--centered">
				<a class="cc-wizard__back" href="<?php echo esc_url( self::step_url( 2, [ 'goal' => $goal_slug, 'test_to' => $test_to ] ) ); ?>">
					← <?php esc_html_e( 'Back', 'cartcue-sales-automation' ); ?>
				</a>
			</div>
		</form>
		<?php
	}

	private static function render_skip_link(): void {
		$skip_url = wp_nonce_url(
			add_query_arg( [ 'action' => 'cartcue_wizard_skip' ], admin_url( 'admin-post.php' ) ),
			self::NONCE_ACTION
		);
		?>
		<p class="cc-wizard__skip">
			<a href="<?php echo esc_url( $skip_url ); ?>">
				<?php esc_html_e( "I'll set something up myself", 'cartcue-sales-automation' ); ?> →
			</a>
		</p>
		<?php
	}

	private static function clamp_step( int $step ): int {
		return max( 1, min( 3, $step ) );
	}

	private static function base_url(): string {
		return add_query_arg( [ 'page' => self::PAGE_SLUG ], admin_url( 'admin.php' ) );
	}

	/**
	 * @param int                  $step  Target step (1..3).
	 * @param array<string, mixed> $extra Additional query args to merge.
	 */
	private static function step_url( int $step, array $extra = [] ): string {
		$args = array_merge(
			[ 'page' => self::PAGE_SLUG, 'wizard_step' => self::clamp_step( $step ) ],
			$extra
		);
		return add_query_arg( $args, admin_url( 'admin.php' ) );
	}

	private static function available_goals_count(): int {
		$count = 0;
		foreach ( self::goals() as $goal ) {
			if ( Recipes::get( $goal['recipe'] ) ) {
				$count++;
			}
		}
		return $count;
	}

	/**
	 * @return int New workflow ID, or 0 on failure.
	 */
	private static function create_play_from_recipe( string $recipe_key, string $status ): int {
		$recipe = Recipes::get( $recipe_key );
		if ( ! is_array( $recipe ) || empty( $recipe ) ) {
			return 0;
		}

		$trigger_slug = sanitize_key( (string) ( $recipe['trigger_slug'] ?? $recipe['trigger'] ?? '' ) );
		$plugin       = Plugin::instance();
		$trigger      = $plugin->get_trigger( $trigger_slug );
		if ( $trigger instanceof GhostTrigger ) {
			return 0;
		}

		$validation = PlayValidator::validate(
			[
				'name'         => (string) ( $recipe['name'] ?? '' ),
				'trigger_slug' => $trigger_slug,
				'actions'      => is_array( $recipe['actions'] ?? null ) ? $recipe['actions'] : [],
				'sequence'     => is_array( $recipe['sequence'] ?? null ) ? $recipe['sequence'] : [],
			]
		);
		if ( is_wp_error( $validation ) ) {
			return 0;
		}

		global $wpdb;
		$payload = RecipeNormalizer::to_workflow_payload( $recipe, $status );
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery
		$inserted = $wpdb->insert( DB::workflows(), $payload );
		if ( false === $inserted ) {
			return 0;
		}
		$play_id = (int) $wpdb->insert_id;
		if ( $play_id < 1 ) {
			return 0;
		}

		$plugin->runner->bust_workflow_cache( $play_id );
		$plugin->runner->bust_active_workflows_cache( $trigger_slug );
		Plugin::bust_play_count_cache();
		CampaignRepository::assign_play( $play_id, 0 );

		return $play_id;
	}

	private static function send_test_email( int $play_id, string $to ): bool {
		if ( $play_id < 1 || ! is_email( $to ) ) {
			return false;
		}

		$plugin = Plugin::instance();
		$play   = $plugin->runner->get_workflow( $play_id );
		if ( ! $play ) {
			return false;
		}

		$trigger = $plugin->get_trigger( (string) ( $play['trigger_slug'] ?? '' ) );
		if ( ! $trigger ) {
			return false;
		}

		$data                   = $trigger->get_test_data();
		$data['customer_email'] = $to;
		$data['workflow_id']    = $play_id;
		$data['_is_test']       = true;

		$ctx  = new Context( $data );
		$sent = false;

		foreach ( (array) ( $play['actions'] ?? [] ) as $action_config ) {
			$slug = (string) ( $action_config['type'] ?? '' );
			if ( 'send_email' !== $slug ) {
				continue;
			}
			$action = $plugin->get_action( $slug );
			if ( ! $action ) {
				continue;
			}
			try {
				$action->run( (array) ( $action_config['settings'] ?? [] ), $ctx );
				$sent = true;
				break;
			} catch ( \Throwable $e ) {
				$plugin->logger->log(
					$play_id,
					(string) ( $play['name'] ?? '' ),
					(string) ( $play['trigger_slug'] ?? '' ),
					$data,
					'error',
					sprintf( 'First-run wizard test send failed: %s', $e->getMessage() )
				);
			}
		}

		return $sent;
	}

	private static function render_inline_styles(): void {
		?>
		<style>
		.cc-wizard{background:linear-gradient(180deg,#f8f7ff 0%,#fff 280px);min-height:80vh;padding:48px 20px;margin:0 -20px;}
		.cc-wizard__shell{max-width:640px;margin:0 auto;}
		.cc-wizard__progress{display:flex;justify-content:center;gap:8px;margin-bottom:48px;}
		.cc-wizard__dot{width:8px;height:8px;border-radius:50%;background:#d8d4f0;transition:background .2s,transform .2s;}
		.cc-wizard__dot.is-active{background:#534AB7;transform:scale(1.15);}
		.cc-wizard__step{animation:cc-wizard-in .35s cubic-bezier(.2,.7,.2,1) both;}
		@keyframes cc-wizard-in{from{opacity:0;transform:translateY(8px);}to{opacity:1;transform:none;}}
		.cc-wizard__greeting{display:inline-flex;align-items:center;gap:8px;padding:6px 14px;background:#EEEDFE;color:#534AB7;border-radius:99px;font-size:13px;font-weight:500;margin-bottom:20px;}
		.cc-wizard__greeting i{font-size:14px;}
		.cc-wizard__headline{font-size:32px;line-height:1.25;font-weight:600;color:#1a1830;margin:0 0 12px;letter-spacing:-0.02em;}
		.cc-wizard__headline em{font-style:normal;color:#534AB7;}
		.cc-wizard__sub{font-size:16px;color:#4a4760;line-height:1.55;margin:0 0 32px;}
		.cc-wizard__goals{display:flex;flex-direction:column;gap:12px;}
		.cc-wizard__goal{display:flex;align-items:center;gap:16px;padding:20px 24px;background:#fff;border:1.5px solid #e5e4f0;border-radius:12px;text-decoration:none;color:#1a1830;transition:border-color .15s,box-shadow .15s,transform .15s;}
		.cc-wizard__goal:hover,.cc-wizard__goal:focus-visible{border-color:#534AB7;box-shadow:0 4px 14px rgba(83,74,183,.1);transform:translateY(-1px);outline:none;}
		.cc-wizard__goal-icon{flex-shrink:0;width:48px;height:48px;background:#EEEDFE;color:#534AB7;border-radius:10px;display:grid;place-items:center;font-size:22px;}
		.cc-wizard__goal-text{flex:1;min-width:0;}
		.cc-wizard__goal-text strong{display:block;font-size:16px;font-weight:600;color:#1a1830;margin-bottom:3px;}
		.cc-wizard__goal-desc{display:block;color:#6b7280;font-size:13.5px;line-height:1.5;}
		.cc-wizard__goal-arrow{color:#534AB7;font-size:18px;font-weight:500;flex-shrink:0;}
		.cc-wizard__sentence{background:#fff;border:1.5px solid #e5e4f0;border-left:3px solid #534AB7;padding:20px 24px;border-radius:10px;font-size:17px;line-height:1.55;color:#1a1830;margin-bottom:32px;}
		.cc-wizard__field{margin-bottom:28px;}
		.cc-wizard__label{display:block;font-size:14px;color:#4a4760;margin-bottom:10px;font-weight:500;}
		.cc-wizard__input{width:100%;padding:14px 16px;font-size:16px;border:1.5px solid #d8d4f0;border-radius:8px;background:#fff;box-sizing:border-box;font-family:inherit;transition:border-color .15s,box-shadow .15s;}
		.cc-wizard__input:focus{outline:none;border-color:#534AB7;box-shadow:0 0 0 3px rgba(83,74,183,.12);}
		.cc-wizard__checkbox{display:flex;align-items:center;gap:8px;margin-top:14px;font-size:14px;color:#4a4760;cursor:pointer;}
		.cc-wizard__checkbox input{accent-color:#534AB7;}
		.cc-wizard__choice-grid{display:grid;grid-template-columns:1fr 1fr;gap:16px;margin-top:8px;}
		.cc-wizard__choice{background:#fff;border:1.5px solid #e5e4f0;border-radius:12px;padding:28px 24px;text-align:center;cursor:pointer;transition:border-color .15s,box-shadow .15s,transform .15s,background .15s;font-family:inherit;color:#1a1830;display:flex;flex-direction:column;align-items:center;gap:4px;}
		.cc-wizard__choice:hover,.cc-wizard__choice:focus-visible{border-color:#534AB7;box-shadow:0 4px 14px rgba(83,74,183,.1);transform:translateY(-1px);outline:none;}
		.cc-wizard__choice--primary{background:#534AB7;color:#fff;border-color:#534AB7;}
		.cc-wizard__choice--primary:hover,.cc-wizard__choice--primary:focus-visible{background:#4339a8;border-color:#4339a8;box-shadow:0 6px 22px rgba(83,74,183,.28);}
		.cc-wizard__choice-icon{font-size:28px;margin-bottom:8px;display:block;}
		.cc-wizard__choice-title{font-size:17px;font-weight:600;margin-bottom:4px;}
		.cc-wizard__choice-desc{font-size:13.5px;opacity:.85;line-height:1.45;}
		.cc-wizard__test-hint{margin:24px 0 0;padding:14px 18px;background:#f5f4fa;border-radius:8px;font-size:13.5px;color:#4a4760;display:flex;align-items:center;gap:10px;}
		.cc-wizard__test-hint i{color:#534AB7;font-size:16px;}
		.cc-wizard__nav{display:flex;align-items:center;justify-content:space-between;margin-top:32px;}
		.cc-wizard__nav--centered{justify-content:center;}
		.cc-wizard__back{color:#6b7280;text-decoration:none;font-size:14px;}
		.cc-wizard__back:hover{color:#534AB7;}
		.cc-wizard__skip{text-align:center;margin-top:56px;padding-top:32px;border-top:1px solid #e5e4f0;}
		.cc-wizard__skip a{color:#6b7280;font-size:13.5px;text-decoration:none;}
		.cc-wizard__skip a:hover{color:#534AB7;text-decoration:underline;}
		@media (max-width:640px){
			.cc-wizard{padding:32px 16px;}
			.cc-wizard__headline{font-size:24px;}
			.cc-wizard__goal{padding:16px 18px;gap:12px;}
			.cc-wizard__goal-icon{width:40px;height:40px;font-size:18px;}
			.cc-wizard__choice-grid{grid-template-columns:1fr;}
		}
		</style>
		<?php
	}
}
