<?php
/**
 * Initial Setup Wizard — guided first-run configuration for admins.
 *
 * Self-registering: instantiate once and all hooks are set up automatically.
 * Shows on first activation (or after a full data reset) via admin notice.
 * Does NOT modify any existing functionality — purely additive.
 *
 * Flow:
 *   1. Welcome    — intro + auto-detected mode (Dokan / Solo)
 *   2. Categories — enable/disable the 6 platform categories
 *   3. Settings   — essential booking + payment defaults
 *   4. Ready      — confirmation + quick links
 *
 * Option flags:
 *   wahrb_setup_wizard_completed  (bool)  — true once wizard is finished or skipped
 *   wahrb_setup_wizard_dismissed  (bool)  — true if the admin notice was dismissed
 *
 * @package WahraRentalsBookings
 * @since   2.1.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * Class WAHRB_Setup_Wizard
 */
class WAHRB_Setup_Wizard {

    /**
     * Admin page slug (hidden — no menu entry).
     *
     * @var string
     */
    private string $page_slug = 'wahrb-setup';

    /**
     * Total number of wizard steps.
     *
     * @var int
     */
    private int $total_steps = 4;

    /**
     * Step labels for the stepper.
     *
     * @var array<int,string>
     */
    private array $step_labels = [];

    /**
     * Constructor — register hooks.
     */
    public function __construct() {
        // Admin notice for first-run.
        add_action( 'admin_notices', [ $this, 'maybe_show_setup_notice' ] );

        // AJAX dismiss notice.
        add_action( 'wp_ajax_wahrb_dismiss_setup_wizard', [ $this, 'ajax_dismiss_notice' ] );

        // Register hidden admin page.
        add_action( 'admin_menu', [ $this, 'register_page' ] );

        // Handle form submission.
        add_action( 'admin_init', [ $this, 'handle_save' ] );

        // Enqueue styles early enough (before wp_head prints them).
        add_action( 'admin_enqueue_scripts', [ $this, 'maybe_enqueue_assets' ] );
    }

    /**
     * Conditionally enqueue wizard assets on the wizard page only.
     *
     * Must run on admin_enqueue_scripts so styles are printed during wp_head(),
     * NOT inside the page callback (which runs after wp_head — CSS would be lost).
     *
     * @param string $hook_suffix The admin page hook suffix.
     */
    public function maybe_enqueue_assets( string $hook_suffix ): void {
        // The hidden submenu page has hook suffix like 'admin_page_wahrb-setup'.
        if ( strpos( $hook_suffix, $this->page_slug ) === false ) {
            return;
        }

        wp_enqueue_style(
            'wahrb-setup-wizard',
            WAHRB_PLUGIN_URL . 'assets/css/wahrb-setup-wizard.css',
            [],
            WAHRB_VERSION
        );
    }

    // ------------------------------------------------------------------
    //  Admin notice
    // ------------------------------------------------------------------

    /**
     * Show setup wizard notice if not completed and not dismissed.
     */
    public function maybe_show_setup_notice(): void {
        // Already completed or dismissed.
        if ( get_option( 'wahrb_setup_wizard_completed' ) || get_option( 'wahrb_setup_wizard_dismissed' ) ) {
            return;
        }

        // Only admins.
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }

        // Don't show on the wizard page itself.
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Navigational parameter
        $current_page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
        if ( $current_page === $this->page_slug ) {
            return;
        }

        $wizard_url = admin_url( 'admin.php?page=' . $this->page_slug );
        ?>
        <div class="notice notice-info wahrb-setup-notice is-dismissible" id="wahrb-setup-notice">
            <p>
                <strong>🎉 <?php esc_html_e( 'Rentals & Bookings is activated!', 'wahra-rentals-bookings' ); ?></strong>
                <?php esc_html_e( 'Run the setup wizard to configure your rental platform in under 2 minutes.', 'wahra-rentals-bookings' ); ?>
            </p>
            <p>
                <a href="<?php echo esc_url( $wizard_url ); ?>" class="button button-primary">
                    <?php esc_html_e( 'Launch Setup Wizard', 'wahra-rentals-bookings' ); ?>
                </a>
                <a href="#" class="button wahrb-dismiss-setup" style="margin-left:8px;">
                    <?php esc_html_e( 'Skip', 'wahra-rentals-bookings' ); ?>
                </a>
            </p>
        </div>
        <script>
        (function(){
            var btn = document.querySelector('.wahrb-dismiss-setup');
            if(!btn) return;
            btn.addEventListener('click', function(e){
                e.preventDefault();
                var notice = document.getElementById('wahrb-setup-notice');
                if(notice) notice.style.display = 'none';
                var xhr = new XMLHttpRequest();
                xhr.open('POST', ajaxurl);
                xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                xhr.send('action=wahrb_dismiss_setup_wizard&_wpnonce=<?php echo esc_js( wp_create_nonce( 'wahrb_dismiss_setup_wizard' ) ); ?>');
            });
            // Also handle WP's built-in dismiss button
            var wpDismiss = document.querySelector('#wahrb-setup-notice .notice-dismiss');
            if(wpDismiss){
                wpDismiss.addEventListener('click', function(){
                    var xhr = new XMLHttpRequest();
                    xhr.open('POST', ajaxurl);
                    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                    xhr.send('action=wahrb_dismiss_setup_wizard&_wpnonce=<?php echo esc_js( wp_create_nonce( 'wahrb_dismiss_setup_wizard' ) ); ?>');
                });
            }
        })();
        </script>
        <?php
    }

    /**
     * AJAX handler to dismiss the setup notice.
     */
    public function ajax_dismiss_notice(): void {
        check_ajax_referer( 'wahrb_dismiss_setup_wizard' );

        if ( ! current_user_can( 'manage_options' ) ) {
            wp_die( -1 );
        }

        update_option( 'wahrb_setup_wizard_dismissed', true );
        wp_die( 'ok' );
    }

    // ------------------------------------------------------------------
    //  Hidden admin page
    // ------------------------------------------------------------------

    /**
     * Register a hidden admin page (no menu item).
     */
    public function register_page(): void {
        add_submenu_page(
            '', // No parent — hidden page
            __( 'Setup Wizard', 'wahra-rentals-bookings' ),
            __( 'Setup Wizard', 'wahra-rentals-bookings' ),
            'manage_options',
            $this->page_slug,
            [ $this, 'render_page' ]
        );
    }

    // ------------------------------------------------------------------
    //  Render
    // ------------------------------------------------------------------

    /**
     * Render the wizard page.
     */
    public function render_page(): void {
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Navigational parameter
        $step = isset( $_GET['step'] ) ? absint( wp_unslash( $_GET['step'] ) ) : 1;
        $step = max( 1, min( $step, $this->total_steps ) );

        // Handle skip action inside render (styles already enqueued via admin_enqueue_scripts).
        $this->handle_skip_action();

        $step_labels = [
            1 => __( 'Welcome', 'wahra-rentals-bookings' ),
            2 => __( 'Categories', 'wahra-rentals-bookings' ),
            3 => __( 'Settings', 'wahra-rentals-bookings' ),
            4 => __( 'Ready', 'wahra-rentals-bookings' ),
        ];
        ?>
        <div class="wahrb-setup-wrap">
            <div class="wahrb-setup-header">
                <div class="wahrb-setup-logo">
                    <span class="wahrb-logo-icon">📦</span>
                    <span class="wahrb-logo-text"><?php esc_html_e( 'Rentals & Bookings', 'wahra-rentals-bookings' ); ?></span>
                </div>
                <div class="wahrb-setup-stepper">
                    <?php for ( $i = 1; $i <= $this->total_steps; $i++ ) :
                        $state = 'upcoming';
                        if ( $i < $step ) $state = 'done';
                        if ( $i === $step ) $state = 'active';
                    ?>
                        <div class="wahrb-step-item <?php echo esc_attr( $state ); ?>">
                            <div class="wahrb-step-dot">
                                <?php if ( $state === 'done' ) : ?>
                                    <svg width="14" height="14" viewBox="0 0 16 16" fill="none"><path d="M3 8.5L6.5 12L13 4" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"/></svg>
                                <?php else : ?>
                                    <?php echo esc_html( $i ); ?>
                                <?php endif; ?>
                            </div>
                            <span class="wahrb-step-label"><?php echo esc_html( $step_labels[ $i ] ); ?></span>
                        </div>
                        <?php if ( $i < $this->total_steps ) : ?>
                            <div class="wahrb-step-connector <?php echo $i < $step ? 'done' : ''; ?>"></div>
                        <?php endif; ?>
                    <?php endfor; ?>
                </div>
            </div>

            <div class="wahrb-setup-body">
                <?php
                switch ( $step ) {
                    case 1:
                        $this->render_step_welcome();
                        break;
                    case 2:
                        $this->render_step_categories();
                        break;
                    case 3:
                        $this->render_step_settings();
                        break;
                    case 4:
                        $this->render_step_ready();
                        break;
                }
                ?>
            </div>

            <div class="wahrb-setup-footer">
                <span><?php esc_html_e( 'Rentals & Bookings', 'wahra-rentals-bookings' ); ?> &mdash; <?php esc_html_e( 'Setup Wizard', 'wahra-rentals-bookings' ); ?></span>
            </div>
        </div>
        <?php
    }

    // ------------------------------------------------------------------
    //  Step 1 — Welcome
    // ------------------------------------------------------------------

    /**
     * Render step 1: Welcome.
     */
    private function render_step_welcome(): void {
        $is_dokan = wahrb_is_dokan_active();
        ?>
        <div class="wahrb-setup-card wahrb-setup-card-welcome">
            <div class="wahrb-setup-card-header-gradient">
                <div class="wahrb-setup-card-icon">🚀</div>
                <h2><?php esc_html_e( 'Welcome to Rentals & Bookings', 'wahra-rentals-bookings' ); ?></h2>
                <p class="wahrb-setup-subtitle">
                    <?php esc_html_e( 'Set up your rental platform in under 2 minutes.', 'wahra-rentals-bookings' ); ?>
                </p>
            </div>

            <div class="wahrb-setup-card-body">
                <div class="wahrb-setup-mode-badge <?php echo $is_dokan ? 'dokan' : 'solo'; ?>">
                    <span class="wahrb-mode-icon"><?php echo $is_dokan ? '🏪' : '🏠'; ?></span>
                    <div class="wahrb-mode-text">
                        <strong>
                            <?php
                            echo $is_dokan
                                ? esc_html__( 'Multi-Vendor Mode (Dokan)', 'wahra-rentals-bookings' )
                                : esc_html__( 'Solo Vendor Mode', 'wahra-rentals-bookings' );
                            ?>
                        </strong>
                        <span>
                            <?php
                            echo $is_dokan
                                ? esc_html__( 'Dokan detected — each vendor manages their own rentals from the Dokan dashboard.', 'wahra-rentals-bookings' )
                                : esc_html__( 'WooCommerce only — you manage all rentals from the WordPress admin.', 'wahra-rentals-bookings' );
                            ?>
                        </span>
                    </div>
                </div>

                <div class="wahrb-setup-timeline">
                    <h3><?php esc_html_e( 'What we will configure:', 'wahra-rentals-bookings' ); ?></h3>
                    <div class="wahrb-timeline-items">
                        <div class="wahrb-timeline-item">
                            <div class="wahrb-timeline-marker"><span>1</span></div>
                            <div class="wahrb-timeline-content">
                                <strong><?php esc_html_e( 'Platform Categories', 'wahra-rentals-bookings' ); ?></strong>
                                <span><?php esc_html_e( 'Accommodation, vehicles, equipment, and more', 'wahra-rentals-bookings' ); ?></span>
                            </div>
                        </div>
                        <div class="wahrb-timeline-item">
                            <div class="wahrb-timeline-marker"><span>2</span></div>
                            <div class="wahrb-timeline-content">
                                <strong><?php esc_html_e( 'Booking Defaults', 'wahra-rentals-bookings' ); ?></strong>
                                <span><?php esc_html_e( 'Hold duration, buffer times, booking windows', 'wahra-rentals-bookings' ); ?></span>
                            </div>
                        </div>
                        <div class="wahrb-timeline-item">
                            <div class="wahrb-timeline-marker"><span>3</span></div>
                            <div class="wahrb-timeline-content">
                                <strong><?php esc_html_e( 'Payment Settings', 'wahra-rentals-bookings' ); ?></strong>
                                <span><?php esc_html_e( 'Deposits, security deposits, balance payments', 'wahra-rentals-bookings' ); ?></span>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="wahrb-setup-actions">
                    <a href="<?php echo esc_url( $this->step_url( 2 ) ); ?>" class="wahrb-btn wahrb-btn-primary wahrb-btn-lg">
                        <?php esc_html_e( "Let's Get Started", 'wahra-rentals-bookings' ); ?>
                        <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M3 8h10M9 4l4 4-4 4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
                    </a>
                    <a href="<?php echo esc_url( $this->skip_url() ); ?>" class="wahrb-btn wahrb-btn-ghost">
                        <?php esc_html_e( 'Skip — I will configure manually', 'wahra-rentals-bookings' ); ?>
                    </a>
                </div>
            </div>
        </div>
        <?php
    }

    // ------------------------------------------------------------------
    //  Step 2 — Categories
    // ------------------------------------------------------------------

    /**
     * Render step 2: Categories.
     */
    private function render_step_categories(): void {
        $registry = WAHRB_Platform_Categories::get_registry();
        $settings = WAHRB_Platform_Categories::get_settings();
        ?>
        <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
            <?php wp_nonce_field( 'wahrb_setup_save', 'wahrb_setup_nonce' ); ?>
            <input type="hidden" name="action" value="wahrb_setup_save">
            <input type="hidden" name="wahrb_setup_step" value="2">

            <div class="wahrb-setup-card">
                <div class="wahrb-setup-card-icon">📂</div>
                <h2><?php esc_html_e( 'Platform Categories', 'wahra-rentals-bookings' ); ?></h2>
                <p class="wahrb-setup-desc">
                    <?php esc_html_e( 'Choose which types of rentals your platform supports. You can always change this later in Settings.', 'wahra-rentals-bookings' ); ?>
                </p>

                <div class="wahrb-setup-categories">
                    <?php foreach ( $registry as $slug => $cat ) :
                        $enabled = $settings[ $slug ]['enabled'] ?? true;
                    ?>
                        <label class="wahrb-setup-cat-card <?php echo $enabled ? 'checked' : ''; ?>">
                            <input type="checkbox" name="wahrb_cats[<?php echo esc_attr( $slug ); ?>]" value="1" <?php checked( $enabled ); ?>>
                            <div class="wahrb-cat-check-circle">
                                <svg width="12" height="12" viewBox="0 0 16 16" fill="none"><path d="M3 8.5L6.5 12L13 4" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"/></svg>
                            </div>
                            <span class="wahrb-cat-emoji"><?php echo esc_html( $cat['emoji'] ); ?></span>
                            <strong><?php echo esc_html( $cat['label'] ); ?></strong>
                            <span class="wahrb-cat-desc"><?php echo esc_html( $cat['description'] ); ?></span>
                            <span class="wahrb-cat-modes">
                                <?php
                                $mode_labels = [
                                    'day'     => esc_html__( 'Day', 'wahra-rentals-bookings' ),
                                    'slot'    => esc_html__( 'Slot', 'wahra-rentals-bookings' ),
                                    'service' => esc_html__( 'Service', 'wahra-rentals-bookings' ),
                                ];
                                foreach ( $cat['booking_modes'] as $mode ) {
                                    echo '<span class="wahrb-cat-mode-tag">' . esc_html( $mode_labels[ $mode ] ?? $mode ) . '</span>';
                                }
                                ?>
                            </span>
                        </label>
                    <?php endforeach; ?>
                </div>

                <div class="wahrb-setup-actions">
                    <a href="<?php echo esc_url( $this->step_url( 1 ) ); ?>" class="wahrb-btn wahrb-btn-outline">
                        <svg width="14" height="14" viewBox="0 0 16 16" fill="none"><path d="M13 8H3M7 4L3 8l4 4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
                        <?php esc_html_e( 'Back', 'wahra-rentals-bookings' ); ?>
                    </a>
                    <button type="submit" class="wahrb-btn wahrb-btn-primary wahrb-btn-lg">
                        <?php esc_html_e( 'Save & Continue', 'wahra-rentals-bookings' ); ?>
                        <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M3 8h10M9 4l4 4-4 4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
                    </button>
                </div>
            </div>
        </form>
        <script>
        (function(){
            document.querySelectorAll('.wahrb-setup-cat-card input[type=checkbox]').forEach(function(cb){
                cb.addEventListener('change', function(){
                    this.closest('.wahrb-setup-cat-card').classList.toggle('checked', this.checked);
                });
            });
        })();
        </script>
        <?php
    }

    // ------------------------------------------------------------------
    //  Step 3 — Settings
    // ------------------------------------------------------------------

    /**
     * Render step 3: Essential settings.
     */
    private function render_step_settings(): void {
        $booking = get_option( 'wahrb_settings_booking', [] );
        $payment = get_option( 'wahrb_settings_payment', [ 'categories' => [] ] );

        // Flatten payment defaults from first enabled category (or global defaults).
        $pay_defaults = [
            'deposit_enabled' => true,
            'deposit_type'    => 'percent',
            'deposit_value'   => 30,
            'security_deposit_enabled' => true,
            'balance_enabled' => true,
            'balance_due_days_before' => 3,
        ];
        if ( ! empty( $payment['categories'] ) ) {
            $first = reset( $payment['categories'] );
            $pay_defaults = wp_parse_args( $first, $pay_defaults );
        }
        ?>
        <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
            <?php wp_nonce_field( 'wahrb_setup_save', 'wahrb_setup_nonce' ); ?>
            <input type="hidden" name="action" value="wahrb_setup_save">
            <input type="hidden" name="wahrb_setup_step" value="3">

            <div class="wahrb-setup-card">
                <div class="wahrb-setup-card-icon">⚙️</div>
                <h2><?php esc_html_e( 'Essential Settings', 'wahra-rentals-bookings' ); ?></h2>
                <p class="wahrb-setup-desc">
                    <?php esc_html_e( 'Set the core defaults for your platform. Fine-tune everything later in the full settings page.', 'wahra-rentals-bookings' ); ?>
                </p>

                <!-- Booking section -->
                <fieldset class="wahrb-setup-fieldset">
                    <legend>📅 <?php esc_html_e( 'Booking', 'wahra-rentals-bookings' ); ?></legend>

                    <div class="wahrb-setup-field-row">
                        <div class="wahrb-setup-field">
                            <label for="wahrb_hold_duration"><?php esc_html_e( 'Hold duration (minutes)', 'wahra-rentals-bookings' ); ?></label>
                            <input type="number" id="wahrb_hold_duration" name="wahrb_hold_duration" min="5" max="120" step="5"
                                   value="<?php echo esc_attr( $booking['hold_duration_minutes'] ?? 30 ); ?>">
                            <span class="wahrb-field-hint"><?php esc_html_e( 'How long a slot is reserved while customer checks out.', 'wahra-rentals-bookings' ); ?></span>
                        </div>
                        <div class="wahrb-setup-field">
                            <label for="wahrb_buffer_before"><?php esc_html_e( 'Preparation buffer (hours)', 'wahra-rentals-bookings' ); ?></label>
                            <input type="number" id="wahrb_buffer_before" name="wahrb_buffer_before" min="0" max="72" step="1"
                                   value="<?php echo esc_attr( $booking['buffer_before_hours'] ?? 2 ); ?>">
                            <span class="wahrb-field-hint"><?php esc_html_e( 'Time blocked before each booking for preparation.', 'wahra-rentals-bookings' ); ?></span>
                        </div>
                        <div class="wahrb-setup-field">
                            <label for="wahrb_buffer_after"><?php esc_html_e( 'Cleaning buffer (hours)', 'wahra-rentals-bookings' ); ?></label>
                            <input type="number" id="wahrb_buffer_after" name="wahrb_buffer_after" min="0" max="72" step="1"
                                   value="<?php echo esc_attr( $booking['buffer_after_hours'] ?? 1 ); ?>">
                            <span class="wahrb-field-hint"><?php esc_html_e( 'Time blocked after each booking for cleanup.', 'wahra-rentals-bookings' ); ?></span>
                        </div>
                    </div>

                    <div class="wahrb-setup-field-row">
                        <div class="wahrb-setup-field">
                            <label for="wahrb_min_window"><?php esc_html_e( 'Minimum booking window (hours)', 'wahra-rentals-bookings' ); ?></label>
                            <input type="number" id="wahrb_min_window" name="wahrb_min_window" min="0" max="720" step="1"
                                   value="<?php echo esc_attr( $booking['min_booking_window_hours'] ?? 24 ); ?>">
                            <span class="wahrb-field-hint"><?php esc_html_e( 'How far in advance a booking must be made.', 'wahra-rentals-bookings' ); ?></span>
                        </div>
                        <div class="wahrb-setup-field">
                            <label for="wahrb_max_window"><?php esc_html_e( 'Maximum booking window (days)', 'wahra-rentals-bookings' ); ?></label>
                            <input type="number" id="wahrb_max_window" name="wahrb_max_window" min="7" max="730" step="1"
                                   value="<?php echo esc_attr( $booking['max_booking_window_days'] ?? 365 ); ?>">
                            <span class="wahrb-field-hint"><?php esc_html_e( 'How far in the future bookings can be made.', 'wahra-rentals-bookings' ); ?></span>
                        </div>
                    </div>
                </fieldset>

                <!-- Payment section -->
                <fieldset class="wahrb-setup-fieldset">
                    <legend>💳 <?php esc_html_e( 'Payment Defaults', 'wahra-rentals-bookings' ); ?></legend>
                    <p class="wahrb-setup-fieldset-hint">
                        <?php esc_html_e( 'These defaults apply to all categories. Override per-category later in Settings → Payment.', 'wahra-rentals-bookings' ); ?>
                    </p>

                    <div class="wahrb-setup-field-row">
                        <div class="wahrb-setup-field wahrb-setup-toggle">
                            <label>
                                <input type="checkbox" name="wahrb_deposit_enabled" value="1" <?php checked( $pay_defaults['deposit_enabled'] ); ?>>
                                <?php esc_html_e( 'Require deposit at booking', 'wahra-rentals-bookings' ); ?>
                            </label>
                        </div>
                        <div class="wahrb-setup-field">
                            <label for="wahrb_deposit_value"><?php esc_html_e( 'Deposit (%)', 'wahra-rentals-bookings' ); ?></label>
                            <input type="number" id="wahrb_deposit_value" name="wahrb_deposit_value" min="1" max="100" step="1"
                                   value="<?php echo esc_attr( $pay_defaults['deposit_value'] ?? 30 ); ?>">
                        </div>
                    </div>

                    <div class="wahrb-setup-field-row">
                        <div class="wahrb-setup-field wahrb-setup-toggle">
                            <label>
                                <input type="checkbox" name="wahrb_security_enabled" value="1" <?php checked( $pay_defaults['security_deposit_enabled'] ); ?>>
                                <?php esc_html_e( 'Require security deposit', 'wahra-rentals-bookings' ); ?>
                            </label>
                        </div>
                        <div class="wahrb-setup-field wahrb-setup-toggle">
                            <label>
                                <input type="checkbox" name="wahrb_balance_enabled" value="1" <?php checked( $pay_defaults['balance_enabled'] ); ?>>
                                <?php esc_html_e( 'Enable balance payment', 'wahra-rentals-bookings' ); ?>
                            </label>
                        </div>
                        <div class="wahrb-setup-field">
                            <label for="wahrb_balance_due"><?php esc_html_e( 'Balance due (days before start)', 'wahra-rentals-bookings' ); ?></label>
                            <input type="number" id="wahrb_balance_due" name="wahrb_balance_due" min="0" max="30" step="1"
                                   value="<?php echo esc_attr( $pay_defaults['balance_due_days_before'] ?? 3 ); ?>">
                        </div>
                    </div>
                </fieldset>

                <div class="wahrb-setup-actions">
                    <a href="<?php echo esc_url( $this->step_url( 2 ) ); ?>" class="wahrb-btn wahrb-btn-outline">
                        <svg width="14" height="14" viewBox="0 0 16 16" fill="none"><path d="M13 8H3M7 4L3 8l4 4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
                        <?php esc_html_e( 'Back', 'wahra-rentals-bookings' ); ?>
                    </a>
                    <button type="submit" class="wahrb-btn wahrb-btn-primary wahrb-btn-lg">
                        <?php esc_html_e( 'Save & Finish', 'wahra-rentals-bookings' ); ?>
                        <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M3 8h10M9 4l4 4-4 4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
                    </button>
                </div>
            </div>
        </form>
        <?php
    }

    // ------------------------------------------------------------------
    //  Step 4 — Ready
    // ------------------------------------------------------------------

    /**
     * Render step 4: All done.
     */
    private function render_step_ready(): void {
        $is_dokan     = wahrb_is_dokan_active();
        $settings_url = $is_dokan
            ? wahrb_get_admin_dashboard_url( 'settings' )
            : wahrb_get_dashboard_url( 'settings' );
        $wizard_url   = wahrb_get_dashboard_url( 'wizard' );
        $bookings_url = $is_dokan
            ? wahrb_get_admin_dashboard_url( 'bookings' )
            : wahrb_get_dashboard_url( 'bookings' );
        $permissions_url = wahrb_get_admin_dashboard_url( 'settings', [ 'tab' => 'permissions' ] );
        ?>
        <div class="wahrb-setup-card wahrb-setup-card-success">
            <div class="wahrb-setup-success-anim">
                <div class="wahrb-success-circle">
                    <svg width="48" height="48" viewBox="0 0 48 48" fill="none">
                        <path d="M12 25L20 33L36 15" stroke="#059669" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/>
                    </svg>
                </div>
            </div>
            <h2><?php esc_html_e( 'Your rental platform is ready!', 'wahra-rentals-bookings' ); ?></h2>
            <p class="wahrb-setup-desc">
                <?php esc_html_e( 'Settings saved successfully. Here is what you can do next:', 'wahra-rentals-bookings' ); ?>
            </p>

            <div class="wahrb-setup-next-steps">
                <?php if ( $is_dokan ) : ?>
                    <a href="<?php echo esc_url( $permissions_url ); ?>" class="wahrb-setup-next-card wahrb-next-primary">
                        <span class="wahrb-next-icon">🏪</span>
                        <strong><?php esc_html_e( 'Configure vendor permissions', 'wahra-rentals-bookings' ); ?></strong>
                        <span><?php esc_html_e( 'Vendors create rental offers from their own Dokan dashboard. Configure what they can manage.', 'wahra-rentals-bookings' ); ?></span>
                        <span class="wahrb-next-arrow">
                            <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M3 8h10M9 4l4 4-4 4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
                        </span>
                    </a>
                <?php else : ?>
                    <a href="<?php echo esc_url( $wizard_url ); ?>" class="wahrb-setup-next-card wahrb-next-primary">
                        <span class="wahrb-next-icon">🧙</span>
                        <strong><?php esc_html_e( 'Create your first rental offer', 'wahra-rentals-bookings' ); ?></strong>
                        <span><?php esc_html_e( 'Use the rental wizard to set up a product for booking.', 'wahra-rentals-bookings' ); ?></span>
                        <span class="wahrb-next-arrow">
                            <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M3 8h10M9 4l4 4-4 4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
                        </span>
                    </a>
                <?php endif; ?>
                <a href="<?php echo esc_url( $settings_url ); ?>" class="wahrb-setup-next-card">
                    <span class="wahrb-next-icon">⚙️</span>
                    <strong><?php esc_html_e( 'Fine-tune settings', 'wahra-rentals-bookings' ); ?></strong>
                    <span><?php esc_html_e( 'Configure pricing rules, email templates, and platform configuration.', 'wahra-rentals-bookings' ); ?></span>
                    <span class="wahrb-next-arrow">
                        <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M3 8h10M9 4l4 4-4 4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
                    </span>
                </a>
                <a href="<?php echo esc_url( $bookings_url ); ?>" class="wahrb-setup-next-card">
                    <span class="wahrb-next-icon">📋</span>
                    <strong><?php esc_html_e( 'View admin bookings dashboard', 'wahra-rentals-bookings' ); ?></strong>
                    <span><?php esc_html_e( 'Monitor bookings from the admin dashboard and supervise marketplace activity.', 'wahra-rentals-bookings' ); ?></span>
                    <span class="wahrb-next-arrow">
                        <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M3 8h10M9 4l4 4-4 4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
                    </span>
                </a>
            </div>
        </div>
        <?php
    }

    // ------------------------------------------------------------------
    //  Save handler
    // ------------------------------------------------------------------

    /**
     * Handle wizard form submissions via admin-post.php.
     */
    public function handle_save(): void {
        // phpcs:ignore WordPress.Security.NonceVerification.Missing -- We verify below
        if ( ! isset( $_POST['action'] ) || sanitize_text_field( wp_unslash( $_POST['action'] ) ) !== 'wahrb_setup_save' ) {
            return;
        }

        if ( ! isset( $_POST['wahrb_setup_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wahrb_setup_nonce'] ) ), 'wahrb_setup_save' ) ) {
            wp_die( esc_html__( 'Security check failed.', 'wahra-rentals-bookings' ) );
        }

        if ( ! current_user_can( 'manage_options' ) ) {
            wp_die( esc_html__( 'Unauthorized.', 'wahra-rentals-bookings' ) );
        }

        $step = isset( $_POST['wahrb_setup_step'] ) ? absint( wp_unslash( $_POST['wahrb_setup_step'] ) ) : 0;

        switch ( $step ) {
            case 2:
                $this->save_categories();
                wp_safe_redirect( $this->step_url( 3 ) );
                exit;

            case 3:
                $this->save_settings();
                // Mark wizard as completed.
                update_option( 'wahrb_setup_wizard_completed', true );
                update_option( 'wahrb_setup_wizard_dismissed', true );
                wp_safe_redirect( $this->step_url( 4 ) );
                exit;
        }
    }

    /**
     * Save step 2: Categories.
     */
    private function save_categories(): void {
        $registry = WAHRB_Platform_Categories::get_registry();
        // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Already verified in handle_save()
        $posted_cats = isset( $_POST['wahrb_cats'] ) && is_array( $_POST['wahrb_cats'] )
            ? array_map( 'sanitize_text_field', wp_unslash( $_POST['wahrb_cats'] ) )
            : [];

        $settings = [];
        foreach ( $registry as $slug => $cat ) {
            $settings[ $slug ] = [
                'enabled' => isset( $posted_cats[ $slug ] ),
                'subcategories' => [],
            ];
            // Enable all subcategories for enabled categories.
            foreach ( $cat['subcategories'] as $sub_slug => $sub_label ) {
                $settings[ $slug ]['subcategories'][ $sub_slug ] = isset( $posted_cats[ $slug ] );
            }
        }

        update_option( WAHRB_Platform_Categories::OPTION_KEY, $settings );
        // Clear the platform categories cache.
        WAHRB_Platform_Categories::clear_cache();
    }

    /**
     * Save step 3: Settings.
     */
    private function save_settings(): void {
        // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Already verified in handle_save()

        // Booking settings.
        $booking = get_option( 'wahrb_settings_booking', [] );
        $booking['hold_duration_minutes']     = isset( $_POST['wahrb_hold_duration'] ) ? absint( wp_unslash( $_POST['wahrb_hold_duration'] ) ) : 30;
        $booking['buffer_before_hours']       = isset( $_POST['wahrb_buffer_before'] ) ? absint( wp_unslash( $_POST['wahrb_buffer_before'] ) ) : 2;
        $booking['buffer_after_hours']        = isset( $_POST['wahrb_buffer_after'] ) ? absint( wp_unslash( $_POST['wahrb_buffer_after'] ) ) : 1;
        $booking['min_booking_window_hours']  = isset( $_POST['wahrb_min_window'] ) ? absint( wp_unslash( $_POST['wahrb_min_window'] ) ) : 24;
        $booking['max_booking_window_days']   = isset( $_POST['wahrb_max_window'] ) ? absint( wp_unslash( $_POST['wahrb_max_window'] ) ) : 365;
        update_option( 'wahrb_settings_booking', $booking );

        // Payment settings — apply to all enabled categories.
        $deposit_enabled  = ! empty( $_POST['wahrb_deposit_enabled'] );
        $deposit_value    = isset( $_POST['wahrb_deposit_value'] ) ? absint( wp_unslash( $_POST['wahrb_deposit_value'] ) ) : 30;
        $security_enabled = ! empty( $_POST['wahrb_security_enabled'] );
        $balance_enabled  = ! empty( $_POST['wahrb_balance_enabled'] );
        $balance_due      = isset( $_POST['wahrb_balance_due'] ) ? absint( wp_unslash( $_POST['wahrb_balance_due'] ) ) : 3;

        $payment  = get_option( 'wahrb_settings_payment', [ 'categories' => [] ] );
        $registry = WAHRB_Platform_Categories::get_registry();

        foreach ( $registry as $slug => $cat ) {
            $existing = $payment['categories'][ $slug ] ?? [];
            $payment['categories'][ $slug ] = wp_parse_args( [
                'deposit_enabled'          => $deposit_enabled,
                'deposit_type'             => 'percent',
                'deposit_value'            => $deposit_value,
                'security_deposit_enabled' => $security_enabled,
                'balance_enabled'          => $balance_enabled,
                'balance_due_days_before'  => $balance_due,
            ], $existing );
        }

        update_option( 'wahrb_settings_payment', $payment );
    }

    // ------------------------------------------------------------------
    //  Helpers
    // ------------------------------------------------------------------

    /**
     * Get URL for a wizard step.
     *
     * @param int $step Step number.
     * @return string URL.
     */
    private function step_url( int $step ): string {
        return admin_url( 'admin.php?page=' . $this->page_slug . '&step=' . $step );
    }

    /**
     * Get URL to skip the wizard (mark as completed).
     *
     * @return string URL.
     */
    private function skip_url(): string {
        return wp_nonce_url(
            admin_url( 'admin.php?page=' . $this->page_slug . '&wahrb_skip_wizard=1' ),
            'wahrb_skip_wizard'
        );
    }

    // ------------------------------------------------------------------
    //  Assets
    // ------------------------------------------------------------------

    /**
     * Enqueue wizard-specific styles.
     *
     * @deprecated Use maybe_enqueue_assets() on admin_enqueue_scripts instead.
     */
    private function enqueue_assets(): void {
        // Styles are now enqueued via maybe_enqueue_assets() on admin_enqueue_scripts hook.
        // This method only handles the skip action now.
        $this->handle_skip_action();
    }

    /**
     * Handle the skip wizard action and the relaunch action.
     */
    private function handle_skip_action(): void {
        // Handle skip.
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Checked below with wp_verify_nonce
        if ( isset( $_GET['wahrb_skip_wizard'] ) ) {
            if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'wahrb_skip_wizard' ) ) {
                update_option( 'wahrb_setup_wizard_completed', true );
                update_option( 'wahrb_setup_wizard_dismissed', true );
                $settings_redirect = wahrb_is_dokan_active()
                    ? wahrb_get_admin_dashboard_url( 'settings' )
                    : wahrb_get_dashboard_url( 'settings' );
                wp_safe_redirect( $settings_redirect );
                exit;
            }
        }

        // Handle relaunch (from Settings > Danger Zone > Tools).
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Checked below with wp_verify_nonce
        if ( isset( $_GET['wahrb_relaunch_wizard'] ) ) {
            if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'wahrb_relaunch_wizard' ) ) {
                // Reset wizard flags so it runs fresh.
                update_option( 'wahrb_setup_wizard_completed', false );
                update_option( 'wahrb_setup_wizard_dismissed', false );
                wp_safe_redirect( admin_url( 'admin.php?page=' . $this->page_slug . '&step=1' ) );
                exit;
            }
        }
    }

    /**
     * Get CSS string for the wizard.
     *
     * @return string CSS.
     */
    private function get_css(): string {
        return '
/* ══════════════════════════════════════════════════════════
   Setup Wizard — Full-Screen Experience
   Hide WordPress admin chrome for a clean, distraction-free wizard.
   ══════════════════════════════════════════════════════════ */

/* ── Hide admin chrome ── */
#adminmenuwrap,
#adminmenuback,
#wpadminbar,
#wpfooter,
.update-nag,
.notice:not(#wahrb-setup-notice),
.updated,
.error:not(.wahrb-setup-wrap .error),
#screen-meta,
#screen-meta-links { display: none !important }
html.wp-toolbar { padding-top: 0 !important }
#wpcontent { margin-left: 0 !important }
#wpbody { padding-top: 0 !important }
#wpbody-content { padding-bottom: 0 !important; float: none !important }

/* ── Full-screen background ── */
#wpbody-content {
    background: linear-gradient(160deg, #f8fafc 0%, #eef2f7 50%, #f1f5f9 100%);
    min-height: 100vh;
}

/* ── Global reset inside wizard ── */
.wahrb-setup-wrap *,
.wahrb-setup-wrap *::before,
.wahrb-setup-wrap *::after { box-sizing: border-box }
.wahrb-setup-wrap a { text-decoration: none; color: inherit; outline: none }
.wahrb-setup-wrap a:focus,
.wahrb-setup-wrap a:hover { text-decoration: none; color: inherit }
.wahrb-setup-wrap h2 { padding: 0; margin: 0 0 8px; line-height: 1.3; font-size: 24px }
.wahrb-setup-wrap h3 { padding: 0; margin: 0; line-height: 1.4 }
.wahrb-setup-wrap p { margin: 0 0 16px; padding: 0 }
.wahrb-setup-wrap fieldset { margin: 0; padding: 0 }
.wahrb-setup-wrap legend { margin: 0; padding: 0; border: none; width: auto }
.wahrb-setup-wrap input[type=number],
.wahrb-setup-wrap input[type=text] {
    box-shadow: none !important;
    -webkit-appearance: none;
    appearance: none;
    height: auto;
}
.wahrb-setup-wrap input[type=checkbox] {
    margin: 0;
    vertical-align: middle;
}
.wahrb-setup-wrap label { margin: 0; cursor: pointer }
.wahrb-setup-wrap svg { vertical-align: middle }

/* ── Main container ── */
.wahrb-setup-wrap {
    max-width: 780px;
    margin: 0 auto;
    padding: 48px 24px 24px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    color: #1e293b;
    font-size: 14px;
    line-height: 1.5;
}

/* ── Logo ── */
.wahrb-setup-logo {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    margin-bottom: 36px;
}
.wahrb-logo-icon { font-size: 28px; line-height: 1 }
.wahrb-logo-text {
    font-size: 18px;
    font-weight: 700;
    color: #1e293b;
    letter-spacing: -.3px;
}

/* ── Stepper ── */
.wahrb-setup-stepper {
    display: flex;
    align-items: flex-start;
    justify-content: center;
    gap: 0;
    margin-bottom: 44px;
}
.wahrb-step-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    position: relative;
    z-index: 1;
}
.wahrb-step-dot {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 15px;
    font-weight: 700;
    transition: all .3s ease;
    position: relative;
}
.wahrb-step-item.upcoming .wahrb-step-dot {
    background: #f1f5f9;
    color: #94a3b8;
    border: 2px solid #e2e8f0;
}
.wahrb-step-item.active .wahrb-step-dot {
    background: #f05025;
    color: #fff;
    border: 2px solid #f05025;
    box-shadow: 0 0 0 5px rgba(240,80,37,.12), 0 4px 12px rgba(240,80,37,.25);
}
.wahrb-step-item.done .wahrb-step-dot {
    background: #059669;
    color: #fff;
    border: 2px solid #059669;
}
.wahrb-step-label {
    font-size: 11px;
    font-weight: 600;
    color: #94a3b8;
    text-transform: uppercase;
    letter-spacing: .6px;
    white-space: nowrap;
}
.wahrb-step-item.active .wahrb-step-label { color: #f05025; font-weight: 700 }
.wahrb-step-item.done .wahrb-step-label { color: #059669 }

/* Connector line */
.wahrb-step-connector {
    width: 64px;
    height: 2px;
    background: #e2e8f0;
    margin: 20px 10px 0;
    border-radius: 2px;
    transition: background .3s ease;
}
.wahrb-step-connector.done { background: #059669 }

/* ── Card — Base ── */
.wahrb-setup-card {
    background: #fff;
    border: 1px solid #e2e8f0;
    border-radius: 20px;
    overflow: hidden;
    box-shadow: 0 1px 3px rgba(0,0,0,.06), 0 8px 32px rgba(0,0,0,.04);
}
.wahrb-setup-card-icon {
    font-size: 48px;
    text-align: center;
    margin-bottom: 8px;
    line-height: 1;
}
.wahrb-setup-card h2 {
    text-align: center;
    font-size: 24px;
    font-weight: 800;
    color: #1e293b;
    margin: 0 0 8px;
    letter-spacing: -.4px;
}
.wahrb-setup-desc {
    text-align: center;
    color: #64748b;
    font-size: 15px;
    margin: 0 0 28px;
    line-height: 1.6;
}
.wahrb-setup-subtitle {
    text-align: center;
    color: rgba(255,255,255,.9);
    font-size: 15px;
    margin: 0;
    line-height: 1.5;
}

/* Card body padding */
.wahrb-setup-card-body { padding: 36px 40px 40px }

/* ── Welcome — gradient header ── */
.wahrb-setup-card-welcome { overflow: hidden }
.wahrb-setup-card-header-gradient {
    background: linear-gradient(135deg, #f05025 0%, #e8401b 40%, #d63315 100%);
    padding: 52px 40px 40px;
    text-align: center;
    position: relative;
}
.wahrb-setup-card-header-gradient::after {
    content: "";
    position: absolute;
    bottom: -1px;
    left: 0;
    right: 0;
    height: 40px;
    background: #fff;
    border-radius: 24px 24px 0 0;
}
.wahrb-setup-card-header-gradient .wahrb-setup-card-icon {
    margin-bottom: 14px;
    filter: grayscale(1) brightness(10);
    font-size: 56px;
}
.wahrb-setup-card-header-gradient h2 {
    color: #fff;
    font-size: 28px;
    letter-spacing: -.3px;
}

/* ── Mode badge ── */
.wahrb-setup-mode-badge {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 20px 24px;
    border-radius: 14px;
    margin-bottom: 28px;
    transition: transform .2s;
}
.wahrb-setup-mode-badge.dokan {
    background: linear-gradient(135deg, #ede9fe 0%, #ddd6fe 100%);
    border: 1px solid #c4b5fd;
}
.wahrb-setup-mode-badge.solo {
    background: linear-gradient(135deg, #ecfdf5 0%, #d1fae5 100%);
    border: 1px solid #6ee7b7;
}
.wahrb-mode-icon { font-size: 36px; flex-shrink: 0 }
.wahrb-mode-text { display: flex; flex-direction: column; gap: 3px }
.wahrb-setup-mode-badge strong {
    display: block;
    font-size: 15px;
    color: #1e293b;
    margin-bottom: 2px;
}
.wahrb-setup-mode-badge span {
    font-size: 13px;
    color: #475569;
    line-height: 1.4;
}

/* ── Timeline (checklist) ── */
.wahrb-setup-timeline { margin-bottom: 32px }
.wahrb-setup-timeline h3 {
    font-size: 12px;
    font-weight: 700;
    color: #94a3b8;
    text-transform: uppercase;
    letter-spacing: .8px;
    margin: 0 0 18px;
}
.wahrb-timeline-items {
    display: flex;
    flex-direction: column;
    gap: 0;
    position: relative;
}
.wahrb-timeline-items::before {
    content: "";
    position: absolute;
    left: 17px;
    top: 16px;
    bottom: 16px;
    width: 2px;
    background: #e2e8f0;
    border-radius: 2px;
}
.wahrb-timeline-item {
    display: flex;
    align-items: flex-start;
    gap: 14px;
    padding: 12px 0;
    position: relative;
}
.wahrb-timeline-marker {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: #fff7ed;
    border: 2px solid #fed7aa;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    position: relative;
    z-index: 1;
}
.wahrb-timeline-marker span {
    font-size: 13px;
    font-weight: 800;
    color: #f05025;
}
.wahrb-timeline-content { padding-top: 5px }
.wahrb-timeline-content strong {
    display: block;
    font-size: 14px;
    color: #1e293b;
    margin-bottom: 3px;
}
.wahrb-timeline-content span {
    font-size: 13px;
    color: #64748b;
    line-height: 1.4;
}

/* ── Buttons ── */
.wahrb-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    font-weight: 600;
    padding: 11px 22px;
    border-radius: 10px;
    border: none;
    cursor: pointer;
    text-decoration: none !important;
    transition: all .2s ease;
    line-height: 1.4;
    white-space: nowrap;
    font-family: inherit;
    letter-spacing: -.1px;
}
.wahrb-btn:hover { text-decoration: none !important }
.wahrb-btn:focus { outline: none; box-shadow: 0 0 0 3px rgba(240,80,37,.2) }

.wahrb-btn-primary {
    background: #f05025;
    color: #fff !important;
    box-shadow: 0 2px 6px rgba(240,80,37,.3);
}
.wahrb-btn-primary:hover {
    background: #d93e15;
    color: #fff !important;
    transform: translateY(-1px);
    box-shadow: 0 6px 20px rgba(240,80,37,.3);
}
.wahrb-btn-primary:active { transform: translateY(0) }

.wahrb-btn-outline {
    background: #fff;
    color: #475569 !important;
    border: 1px solid #d1d5db;
    box-shadow: 0 1px 2px rgba(0,0,0,.04);
}
.wahrb-btn-outline:hover {
    background: #f8fafc;
    border-color: #94a3b8;
    color: #1e293b !important;
}

.wahrb-btn-ghost {
    color: #94a3b8 !important;
    background: transparent;
    font-size: 13px;
    padding: 8px 16px;
}
.wahrb-btn-ghost:hover { color: #64748b !important }

.wahrb-btn-lg {
    font-size: 15px;
    padding: 13px 30px;
    border-radius: 12px;
}

.wahrb-btn svg { flex-shrink: 0 }

/* ── Actions bar ── */
.wahrb-setup-actions {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 32px;
    gap: 12px;
}
.wahrb-setup-actions:has(.wahrb-btn-ghost) {
    flex-direction: column;
    align-items: center;
    gap: 16px;
}

/* ── Categories grid ── */
.wahrb-setup-categories {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 14px;
    margin-bottom: 8px;
    padding: 0 4px;
}
.wahrb-setup-cat-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    padding: 24px 14px 18px;
    border: 2px solid #e2e8f0;
    border-radius: 16px;
    cursor: pointer;
    transition: all .2s ease;
    text-align: center;
    background: #fff;
    position: relative;
    user-select: none;
}
.wahrb-setup-cat-card:hover {
    border-color: #fda68a;
    background: #fffbf9;
    transform: translateY(-2px);
    box-shadow: 0 4px 16px rgba(0,0,0,.05);
}
.wahrb-setup-cat-card.checked {
    border-color: #f05025;
    background: #fff7ed;
    box-shadow: 0 0 0 3px rgba(240,80,37,.08);
}
.wahrb-setup-cat-card input[type=checkbox] {
    position: absolute;
    opacity: 0;
    pointer-events: none;
    width: 0;
    height: 0;
}

/* Custom check circle */
.wahrb-cat-check-circle {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: 2px solid #d1d5db;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all .2s;
    color: transparent;
    background: #fff;
}
.wahrb-setup-cat-card.checked .wahrb-cat-check-circle {
    border-color: #f05025;
    background: #f05025;
    color: #fff;
}
.wahrb-cat-emoji { font-size: 36px; margin-bottom: 2px }
.wahrb-setup-cat-card strong {
    font-size: 13px;
    color: #1e293b;
    font-weight: 700;
}
.wahrb-cat-desc {
    font-size: 11px;
    color: #64748b;
    line-height: 1.35;
    min-height: 30px;
}
.wahrb-cat-modes {
    display: flex;
    gap: 4px;
    margin-top: 4px;
    flex-wrap: wrap;
    justify-content: center;
}
.wahrb-cat-mode-tag {
    font-size: 10px;
    padding: 2px 8px;
    border-radius: 8px;
    background: #f1f5f9;
    color: #475569;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .3px;
}

/* ── Settings fieldsets ── */
.wahrb-setup-fieldset {
    border: 1px solid #e2e8f0 !important;
    border-radius: 14px;
    padding: 24px 28px !important;
    margin: 0 0 24px !important;
    background: #fafbfc;
}
.wahrb-setup-fieldset legend {
    font-size: 15px !important;
    font-weight: 700 !important;
    color: #1e293b !important;
    padding: 0 10px !important;
    background: #fafbfc;
    border-radius: 6px;
    width: auto !important;
    float: none !important;
}
.wahrb-setup-fieldset-hint {
    font-size: 13px;
    color: #94a3b8;
    margin: -4px 0 18px;
    line-height: 1.5;
}
.wahrb-setup-field-row {
    display: flex;
    gap: 18px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}
.wahrb-setup-field { flex: 1; min-width: 180px }
.wahrb-setup-field label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    color: #334155;
    margin-bottom: 6px;
}
.wahrb-setup-field input[type=number] {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid #d1d5db !important;
    border-radius: 10px !important;
    font-size: 14px;
    background: #fff;
    transition: all .2s;
    font-family: inherit;
    line-height: 1.4;
    height: auto !important;
    min-height: 0 !important;
}
.wahrb-setup-field input[type=number]:focus {
    border-color: #f05025 !important;
    box-shadow: 0 0 0 3px rgba(240,80,37,.1) !important;
    outline: none !important;
}
.wahrb-field-hint {
    display: block;
    font-size: 11px;
    color: #94a3b8;
    margin-top: 4px;
    line-height: 1.4;
}
.wahrb-setup-toggle label {
    display: flex !important;
    align-items: center;
    gap: 10px;
    font-size: 14px;
    font-weight: 500;
    color: #334155;
    cursor: pointer;
    padding: 8px 0;
}
.wahrb-setup-toggle input[type=checkbox] {
    width: 18px;
    height: 18px;
    accent-color: #f05025;
    flex-shrink: 0;
}

/* Inner card padding for non-welcome steps */
.wahrb-setup-card > .wahrb-setup-card-icon,
.wahrb-setup-card > h2,
.wahrb-setup-card > .wahrb-setup-desc,
.wahrb-setup-card > .wahrb-setup-categories,
.wahrb-setup-card > .wahrb-setup-fieldset,
.wahrb-setup-card > .wahrb-setup-actions { margin-left: 40px; margin-right: 40px }
.wahrb-setup-card > .wahrb-setup-card-icon { padding-top: 40px }
.wahrb-setup-card > .wahrb-setup-actions { padding-bottom: 40px }

/* ── Success card ── */
.wahrb-setup-card-success {
    border-color: #bbf7d0;
    background: linear-gradient(180deg, #ecfdf5 0%, #fff 50%);
}
.wahrb-setup-card-success h2,
.wahrb-setup-card-success .wahrb-setup-desc { margin-left: 40px; margin-right: 40px }
.wahrb-setup-card-success h2 { padding-top: 0 }

/* Success animation */
.wahrb-setup-success-anim { text-align: center; padding-top: 44px; margin-bottom: 16px }
.wahrb-success-circle {
    width: 88px;
    height: 88px;
    border-radius: 50%;
    background: linear-gradient(135deg, #d1fae5, #a7f3d0);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    animation: wahrb-pop .5s ease-out;
    box-shadow: 0 4px 20px rgba(5,150,105,.15);
}
@keyframes wahrb-pop {
    0% { transform: scale(0); opacity: 0 }
    60% { transform: scale(1.15) }
    100% { transform: scale(1); opacity: 1 }
}

/* ── Next steps ── */
.wahrb-setup-next-steps {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 0 40px 40px;
}
.wahrb-setup-next-card {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 18px 22px;
    border: 1px solid #e2e8f0;
    border-radius: 14px;
    background: #fff;
    text-decoration: none !important;
    color: #1e293b !important;
    transition: all .2s ease;
    position: relative;
}
.wahrb-setup-next-card:hover {
    border-color: #f05025;
    transform: translateX(4px);
    box-shadow: 0 4px 16px rgba(0,0,0,.05);
    color: #1e293b !important;
    text-decoration: none !important;
}
.wahrb-setup-next-card.wahrb-next-primary {
    border-color: #f05025;
    background: #fff7ed;
}
.wahrb-setup-next-card.wahrb-next-primary:hover {
    background: #fff2e8;
    box-shadow: 0 4px 16px rgba(240,80,37,.12);
}
.wahrb-next-icon { font-size: 28px; flex-shrink: 0 }
.wahrb-setup-next-card strong {
    font-size: 14px;
    display: block;
    margin-bottom: 2px;
    color: #1e293b;
}
.wahrb-setup-next-card > span:not(.wahrb-next-icon):not(.wahrb-next-arrow) {
    font-size: 12px;
    color: #64748b;
    line-height: 1.3;
    flex: 1;
}
.wahrb-next-arrow {
    color: #cbd5e1;
    margin-left: auto;
    flex-shrink: 0;
    transition: color .2s;
}
.wahrb-setup-next-card:hover .wahrb-next-arrow { color: #f05025 }

/* ── Footer ── */
.wahrb-setup-footer {
    text-align: center;
    margin-top: 32px;
    padding: 16px 0;
    color: #cbd5e1;
    font-size: 12px;
}

/* ── Responsive ── */
@media (max-width: 700px) {
    .wahrb-setup-wrap { margin: 0; padding: 24px 12px 16px }
    .wahrb-setup-card { border-radius: 16px }
    .wahrb-setup-card-header-gradient { padding: 32px 24px 28px }
    .wahrb-setup-card-body { padding: 24px 20px 28px }
    .wahrb-setup-card > .wahrb-setup-card-icon,
    .wahrb-setup-card > h2,
    .wahrb-setup-card > .wahrb-setup-desc,
    .wahrb-setup-card > .wahrb-setup-categories,
    .wahrb-setup-card > .wahrb-setup-fieldset,
    .wahrb-setup-card > .wahrb-setup-actions { margin-left: 20px; margin-right: 20px }
    .wahrb-setup-categories { grid-template-columns: repeat(2, 1fr); gap: 10px }
    .wahrb-setup-next-steps { padding: 0 20px 28px }
    .wahrb-setup-field-row { flex-direction: column; gap: 12px }
    .wahrb-setup-stepper { gap: 0 }
    .wahrb-step-connector { width: 32px }
    .wahrb-step-label { font-size: 10px }
}
@media (max-width: 480px) {
    .wahrb-setup-categories { grid-template-columns: 1fr }
    .wahrb-setup-actions { flex-direction: column; align-items: stretch }
    .wahrb-setup-actions .wahrb-btn { justify-content: center }
    .wahrb-step-connector { width: 20px }
    .wahrb-setup-next-card { flex-direction: column; text-align: center; gap: 8px }
}
';
    }
}
