<?php
/**
 * ManiPay — dedicated admin dashboard listing every order routed through
 * this gateway, so the merchant doesn't have to dig through FluentCart's
 * full order list to find the ones awaiting manual Transaction ID
 * verification.
 *
 * @package ManiPay\FluentCart
 */

namespace ManiPay\FluentCart;

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

use FluentCart\App\Models\Order;
use FluentCart\App\Models\OrderTransaction;

final class AdminOrdersPage {

    const GATEWAY_SLUG = 'pxelcode_pay';
    const PAGE_SLUG     = 'manipay-fc-orders';
    const PER_PAGE       = 20;

    /**
     * The exact labels PaymentPage.php writes into the "Method: X" note
     * (see manipaySetMethod() calls in PaymentPage::render()), grouped the
     * way a merchant thinks about them rather than by individual bank.
     */
    const METHOD_GROUPS = array(
        'bkash'  => array( 'bKash' ),
        'nagad'  => array( 'Nagad' ),
        'rocket' => array( 'Rocket' ),
        'bank'   => array( 'AIBL', 'Pubali', 'DBBL', 'IBBL' ),
        'crypto' => array( 'Binance Pay', 'RedotPay' ),
    );

    const METHOD_ICONS = array(
        'bkash'  => 'dashicons-smartphone',
        'nagad'  => 'dashicons-smartphone',
        'rocket' => 'dashicons-smartphone',
        'bank'   => 'dashicons-bank',
        'crypto' => 'dashicons-money-alt',
    );

    public static function boot(): void {
        add_action( 'admin_menu', array( __CLASS__, 'register_menu' ), 99 );
    }

    /**
     * Added as a submenu of FluentCart's own admin page ('fluent-cart') so it
     * lives alongside Orders/Settings rather than cluttering the main WP
     * admin menu. Falls back to being reachable directly by URL even if
     * FluentCart's menu isn't present yet when this runs.
     */
    public static function register_menu(): void {
        add_submenu_page(
            'fluent-cart',
            __( 'ManiPay Orders', 'manipay-manual-payment-for-fluentcart' ),
            __( 'ManiPay Orders', 'manipay-manual-payment-for-fluentcart' ),
            'manage_options',
            self::PAGE_SLUG,
            array( __CLASS__, 'render' )
        );
    }

    public static function render(): void {
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_die( esc_html__( 'You do not have permission to view this page.', 'manipay-manual-payment-for-fluentcart' ) );
        }

        self::render_styles();

        if ( ! class_exists( OrderTransaction::class ) ) {
            echo '<div class="wrap manipay-orders-wrap"><h1>' . esc_html__( 'ManiPay Orders', 'manipay-manual-payment-for-fluentcart' ) . '</h1>';
            echo '<div class="notice notice-error"><p>' . esc_html__( 'FluentCart is not active — this page needs FluentCart to load order data.', 'manipay-manual-payment-for-fluentcart' ) . '</p></div></div>';
            return;
        }

        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only filter/pagination, no state change.
        $status_filter = isset( $_GET['manipay_status'] ) ? sanitize_key( wp_unslash( $_GET['manipay_status'] ) ) : 'all';
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $method_filter = isset( $_GET['manipay_method'] ) ? sanitize_key( wp_unslash( $_GET['manipay_method'] ) ) : 'all';
        if ( ! isset( self::METHOD_GROUPS[ $method_filter ] ) ) {
            $method_filter = 'all';
        }
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $paged = isset( $_GET['paged'] ) ? max( 1, (int) $_GET['paged'] ) : 1;

        /*
         * payment_note was never a real, fillable column on
         * OrderTransaction — writes to it were silently dropped by
         * Eloquent's mass-assignment protection, so nothing was ever
         * actually saved there. The gateway's own note data now lives in
         * the real, fillable `meta` JSON column instead (manipay_method,
         * manipay_trx_id, etc. — see PaymentPage::handle_submit()), so this
         * filter reads that. `meta` is a real DB column so it could be
         * queried at the SQL level too, but since it's JSON we'd need
         * MySQL JSON_EXTRACT (not guaranteed across all MySQL/MariaDB
         * versions FluentCart supports) — matching in PHP after loading is
         * simpler and safe either way.
         */
        $all = OrderTransaction::query()
            ->with( 'order.customer' )
            ->where( 'payment_method', self::GATEWAY_SLUG )
            ->orderBy( 'created_at', 'desc' )
            ->get();

        $matches_method = function ( $transaction ) use ( $method_filter ) {
            if ( 'all' === $method_filter ) {
                return true;
            }
            $meta   = is_array( $transaction->meta ) ? $transaction->meta : array();
            $method = $meta['manipay_method'] ?? '';
            return in_array( $method, self::METHOD_GROUPS[ $method_filter ], true );
        };

        $is_paid = function ( $transaction ) {
            $order = $transaction->order ?? null;
            return $order && 'paid' === ( $order->payment_status ?? '' );
        };

        $method_filtered = array_values( array_filter( $all->all(), $matches_method ) );

        $total_all     = count( $method_filtered );
        $total_pending = count( array_filter( $method_filtered, function ( $t ) use ( $is_paid ) { return ! $is_paid( $t ); } ) );
        $total_paid    = $total_all - $total_pending;

        if ( 'pending' === $status_filter ) {
            $display = array_filter( $method_filtered, function ( $t ) use ( $is_paid ) { return ! $is_paid( $t ); } );
        } elseif ( 'paid' === $status_filter ) {
            $display = array_filter( $method_filtered, $is_paid );
        } else {
            $display = $method_filtered;
        }
        $display = array_values( $display );

        $total_filtered = count( $display );
        $transactions   = array_slice( $display, ( $paged - 1 ) * self::PER_PAGE, self::PER_PAGE );
        ?>
        <div class="wrap manipay-orders-wrap">
            <div class="manipay-header">
                <div class="manipay-header-icon" aria-hidden="true"><span class="dashicons dashicons-money-alt"></span></div>
                <div>
                    <h1><?php esc_html_e( 'ManiPay Orders', 'manipay-manual-payment-for-fluentcart' ); ?></h1>
                    <p><?php esc_html_e( 'Every order placed through ManiPay (bKash, Nagad, Rocket, Bank Transfer, Crypto), in one place. Verify the Transaction ID against your own account, then mark it paid from FluentCart\'s Order screen.', 'manipay-manual-payment-for-fluentcart' ); ?></p>
                </div>
            </div>

            <div class="manipay-stats">
                <a class="manipay-stat <?php echo 'all' === $status_filter ? 'is-active' : ''; ?>" href="<?php echo esc_url( self::tab_url( 'all', $method_filter ) ); ?>">
                    <span class="manipay-stat-num"><?php echo (int) $total_all; ?></span>
                    <span class="manipay-stat-label"><?php esc_html_e( 'Total orders', 'manipay-manual-payment-for-fluentcart' ); ?></span>
                </a>
                <a class="manipay-stat manipay-stat-pending <?php echo 'pending' === $status_filter ? 'is-active' : ''; ?>" href="<?php echo esc_url( self::tab_url( 'pending', $method_filter ) ); ?>">
                    <span class="manipay-stat-num"><?php echo (int) $total_pending; ?></span>
                    <span class="manipay-stat-label"><?php esc_html_e( 'Awaiting verification', 'manipay-manual-payment-for-fluentcart' ); ?></span>
                </a>
                <a class="manipay-stat manipay-stat-paid <?php echo 'paid' === $status_filter ? 'is-active' : ''; ?>" href="<?php echo esc_url( self::tab_url( 'paid', $method_filter ) ); ?>">
                    <span class="manipay-stat-num"><?php echo (int) $total_paid; ?></span>
                    <span class="manipay-stat-label"><?php esc_html_e( 'Paid', 'manipay-manual-payment-for-fluentcart' ); ?></span>
                </a>
            </div>

            <form method="get" class="manipay-toolbar">
                <input type="hidden" name="page" value="<?php echo esc_attr( self::PAGE_SLUG ); ?>">
                <input type="hidden" name="manipay_status" value="<?php echo esc_attr( $status_filter ); ?>">
                <label for="manipay-method-select">
                    <span class="dashicons dashicons-filter" aria-hidden="true"></span>
                    <?php esc_html_e( 'Payment method', 'manipay-manual-payment-for-fluentcart' ); ?>
                </label>
                <select name="manipay_method" id="manipay-method-select" onchange="this.form.submit()">
                    <option value="all" <?php selected( $method_filter, 'all' ); ?>><?php esc_html_e( 'All methods', 'manipay-manual-payment-for-fluentcart' ); ?></option>
                    <option value="bkash" <?php selected( $method_filter, 'bkash' ); ?>><?php esc_html_e( 'bKash', 'manipay-manual-payment-for-fluentcart' ); ?></option>
                    <option value="nagad" <?php selected( $method_filter, 'nagad' ); ?>><?php esc_html_e( 'Nagad', 'manipay-manual-payment-for-fluentcart' ); ?></option>
                    <option value="rocket" <?php selected( $method_filter, 'rocket' ); ?>><?php esc_html_e( 'Rocket', 'manipay-manual-payment-for-fluentcart' ); ?></option>
                    <option value="bank" <?php selected( $method_filter, 'bank' ); ?>><?php esc_html_e( 'Bank Transfer (any bank)', 'manipay-manual-payment-for-fluentcart' ); ?></option>
                    <option value="crypto" <?php selected( $method_filter, 'crypto' ); ?>><?php esc_html_e( 'Crypto (any provider)', 'manipay-manual-payment-for-fluentcart' ); ?></option>
                </select>
                <noscript><button type="submit" class="button"><?php esc_html_e( 'Filter', 'manipay-manual-payment-for-fluentcart' ); ?></button></noscript>
                <?php if ( 'all' !== $method_filter ) : ?>
                    <a class="manipay-clear-filter" href="<?php echo esc_url( self::tab_url( $status_filter, 'all' ) ); ?>"><?php esc_html_e( 'Clear', 'manipay-manual-payment-for-fluentcart' ); ?></a>
                <?php endif; ?>
                <span class="manipay-result-count">
                    <?php
                    /* translators: %d: number of matching orders */
                    echo esc_html( sprintf( _n( '%d order', '%d orders', $total_filtered, 'manipay-manual-payment-for-fluentcart' ), $total_filtered ) );
                    ?>
                </span>
            </form>

            <div class="manipay-table-card">
                <div class="manipay-table-scroll">
                    <table class="manipay-orders-table">
                        <thead>
                            <tr>
                                <th><?php esc_html_e( 'Order', 'manipay-manual-payment-for-fluentcart' ); ?></th>
                                <th><?php esc_html_e( 'Date', 'manipay-manual-payment-for-fluentcart' ); ?></th>
                                <th><?php esc_html_e( 'Customer', 'manipay-manual-payment-for-fluentcart' ); ?></th>
                                <th><?php esc_html_e( 'Email', 'manipay-manual-payment-for-fluentcart' ); ?></th>
                                <th><?php esc_html_e( 'Amount', 'manipay-manual-payment-for-fluentcart' ); ?></th>
                                <th><?php esc_html_e( 'Method', 'manipay-manual-payment-for-fluentcart' ); ?></th>
                                <th><?php esc_html_e( 'Payment details', 'manipay-manual-payment-for-fluentcart' ); ?></th>
                                <th><?php esc_html_e( 'Status', 'manipay-manual-payment-for-fluentcart' ); ?></th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php if ( empty( $transactions ) ) : ?>
                            <tr class="manipay-empty-row">
                                <td colspan="9">
                                    <div class="manipay-empty-state">
                                        <span class="dashicons dashicons-info-outline" aria-hidden="true"></span>
                                        <p>
                                            <?php if ( 'all' === $status_filter && 'all' === $method_filter ) : ?>
                                                <?php esc_html_e( 'No ManiPay orders yet. They\'ll show up here as soon as a customer checks out with ManiPay.', 'manipay-manual-payment-for-fluentcart' ); ?>
                                            <?php else : ?>
                                                <?php esc_html_e( 'No orders match this filter.', 'manipay-manual-payment-for-fluentcart' ); ?>
                                            <?php endif; ?>
                                        </p>
                                    </div>
                                </td>
                            </tr>
                        <?php else : ?>
                            <?php foreach ( $transactions as $transaction ) : ?>
                                <?php self::render_row( $transaction ); ?>
                            <?php endforeach; ?>
                        <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <?php self::render_pagination( $total_filtered, $paged ); ?>
        </div>
        <?php
    }

    private static function render_row( OrderTransaction $transaction ): void {
        $order = $transaction->order ?? null;
        if ( ! $order && ! empty( $transaction->order_id ) && class_exists( Order::class ) ) {
            $order = Order::find( $transaction->order_id );
        }

        $meta     = is_array( $transaction->meta ) ? $transaction->meta : array();
        $trx_id   = $transaction->vendor_charge_id ?: ( $meta['manipay_trx_id'] ?? '' );
        $amount   = self::format_amount( $order );
        $customer = self::format_customer_name( $order );
        $email    = self::format_customer_email( $order );
        $method   = $meta['manipay_method'] ?? '';
        $status   = self::format_status( $order );
        $order_no = $order && isset( $order->id ) ? '#' . (int) $order->id : '—';
        $initial  = $customer && '—' !== $customer ? mb_strtoupper( mb_substr( $customer, 0, 1 ) ) : '?';
        $date     = ! empty( $transaction->created_at ) ? mysql2date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), (string) $transaction->created_at ) : '';
        ?>
        <tr>
            <td><span class="manipay-order-no"><?php echo esc_html( $order_no ); ?></span></td>
            <td><span class="manipay-date"><?php echo esc_html( $date ); ?></span></td>
            <td>
                <div class="manipay-customer">
                    <span class="manipay-avatar" aria-hidden="true"><?php echo esc_html( $initial ); ?></span>
                    <span><?php echo esc_html( $customer ); ?></span>
                </div>
            </td>
            <td><span class="manipay-muted"><?php echo esc_html( $email ?: '—' ); ?></span></td>
            <td><span class="manipay-amount"><?php echo esc_html( $amount ); ?></span></td>
            <td><?php echo $method ? '<span class="manipay-method-pill">' . esc_html( $method ) . '</span>' : '<span class="manipay-muted">—</span>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- both branches are escaped. ?></td>
            <td class="manipay-details-cell">
                <?php if ( $trx_id ) : ?>
                    <div class="manipay-detail-line"><span class="manipay-detail-label"><?php esc_html_e( 'Trx ID', 'manipay-manual-payment-for-fluentcart' ); ?></span><code><?php echo esc_html( $trx_id ); ?></code></div>
                <?php endif; ?>
                <?php if ( ! empty( $meta['manipay_sender_no'] ) ) : ?>
                    <div class="manipay-detail-line"><span class="manipay-detail-label"><?php esc_html_e( 'Sender', 'manipay-manual-payment-for-fluentcart' ); ?></span><?php echo esc_html( $meta['manipay_sender_no'] ); ?></div>
                <?php endif; ?>
                <?php if ( ! empty( $meta['manipay_sender_email'] ) ) : ?>
                    <div class="manipay-detail-line"><span class="manipay-detail-label"><?php esc_html_e( 'Crypto email', 'manipay-manual-payment-for-fluentcart' ); ?></span><?php echo esc_html( $meta['manipay_sender_email'] ); ?></div>
                <?php endif; ?>
                <?php if ( ! empty( $meta['manipay_slip_url'] ) ) : ?>
                    <div class="manipay-detail-line"><a href="<?php echo esc_url( $meta['manipay_slip_url'] ); ?>" target="_blank" rel="noopener noreferrer"><span class="dashicons dashicons-camera" aria-hidden="true"></span> <?php esc_html_e( 'View slip', 'manipay-manual-payment-for-fluentcart' ); ?></a></div>
                <?php endif; ?>
                <?php if ( ! $trx_id ) : ?>
                    <span class="manipay-muted"><?php esc_html_e( 'Awaiting Transaction ID from customer.', 'manipay-manual-payment-for-fluentcart' ); ?></span>
                <?php endif; ?>
            </td>
            <td><?php echo self::status_badge( $status ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built and escaped in status_badge(). ?></td>
            <td>
                <?php if ( $order && isset( $order->id ) ) : ?>
                    <a class="manipay-view-btn" href="<?php echo esc_url( self::order_admin_url( (int) $order->id ) ); ?>">
                        <?php esc_html_e( 'View', 'manipay-manual-payment-for-fluentcart' ); ?>
                        <span class="dashicons dashicons-arrow-right-alt2" aria-hidden="true"></span>
                    </a>
                <?php endif; ?>
            </td>
        </tr>
        <?php
    }

    private static function format_amount( $order ): string {
        if ( ! $order ) {
            return '—';
        }
        $cents = null;
        if ( isset( $order->total ) ) {
            $cents = (int) $order->total;
        } elseif ( isset( $order->total_amount ) ) {
            $cents = (int) $order->total_amount;
        }
        if ( null === $cents ) {
            return '—';
        }
        $currency = ! empty( $order->currency ) ? $order->currency : '';
        if ( class_exists( '\FluentCart\App\Helpers\Helper' ) && method_exists( '\FluentCart\App\Helpers\Helper', 'formatPrice' ) ) {
            return \FluentCart\App\Helpers\Helper::formatPrice( $cents );
        }
        return trim( number_format( $cents / 100, 2 ) . ' ' . $currency );
    }

    private static function format_customer_name( $order ): string {
        if ( ! $order ) {
            return '—';
        }
        $name = trim( (string) ( $order->billing_name ?? '' ) );
        if ( '' !== $name ) {
            return $name;
        }
        // Fall back to the related Customer record — some checkouts (e.g.
        // minimal/guest checkout without a separate billing name field)
        // leave the order's own billing_name empty even though the
        // customer's name is on file.
        $customer = $order->customer ?? null;
        if ( $customer ) {
            $full = trim( trim( (string) ( $customer->first_name ?? '' ) ) . ' ' . trim( (string) ( $customer->last_name ?? '' ) ) );
            if ( '' !== $full ) {
                return $full;
            }
        }
        return '—';
    }

    private static function format_customer_email( $order ): string {
        if ( ! $order ) {
            return '';
        }
        $email = trim( (string) ( $order->email ?? '' ) );
        if ( '' !== $email ) {
            return $email;
        }
        $customer = $order->customer ?? null;
        if ( $customer && ! empty( $customer->email ) ) {
            return (string) $customer->email;
        }
        return '';
    }

    private static function format_status( $order ): string {
        if ( ! $order ) {
            return 'unknown';
        }
        return (string) ( $order->payment_status ?? $order->status ?? 'unknown' );
    }

    private static function status_badge( string $status ): string {
        $normalized = strtolower( $status );
        $class      = 'manipay-badge manipay-badge-pending';
        if ( in_array( $normalized, array( 'paid', 'completed', 'succeeded' ), true ) ) {
            $class = 'manipay-badge manipay-badge-paid';
        } elseif ( in_array( $normalized, array( 'failed', 'cancelled', 'canceled' ), true ) ) {
            $class = 'manipay-badge manipay-badge-failed';
        }
        return '<span class="' . esc_attr( $class ) . '">' . esc_html( ucfirst( $status ) ) . '</span>';
    }

    /**
     * Link into FluentCart's own order detail screen. Confirmed route:
     * admin.php?page=fluent-cart#/orders/{id}/view
     * Override anytime with:
     * add_filter( 'manipay_fc_order_admin_url', function( $url, $order_id ) { ... }, 10, 2 );
     */
    private static function order_admin_url( int $order_id ): string {
        $url = admin_url( 'admin.php?page=fluent-cart#/orders/' . $order_id . '/view' );
        return apply_filters( 'manipay_fc_order_admin_url', $url, $order_id );
    }

    private static function tab_url( string $status, string $method = 'all' ): string {
        return add_query_arg(
            array(
                'page'           => self::PAGE_SLUG,
                'manipay_status' => $status,
                'manipay_method' => $method,
            ),
            admin_url( 'admin.php' )
        );
    }

    private static function render_pagination( int $total, int $paged ): void {
        $total_pages = (int) ceil( $total / self::PER_PAGE );
        if ( $total_pages <= 1 ) {
            return;
        }
        echo '<div class="manipay-pagination">';
        echo wp_kses_post(
            paginate_links(
                array(
                    'base'      => add_query_arg( 'paged', '%#%' ),
                    'format'    => '',
                    'current'   => $paged,
                    'total'     => $total_pages,
                    'prev_text' => __( '&laquo; Previous', 'manipay-manual-payment-for-fluentcart' ),
                    'next_text' => __( 'Next &raquo;', 'manipay-manual-payment-for-fluentcart' ),
                )
            )
        );
        echo '</div>';
    }

    private static function render_styles(): void {
        ?>
        <style>
            .manipay-orders-wrap { max-width: 1280px; }
            .manipay-header { display: flex; align-items: flex-start; gap: 14px; margin: 18px 0 20px; }
            .manipay-header-icon { width: 42px; height: 42px; border-radius: 12px; background: #0b6e4f; color: #fff; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
            .manipay-header-icon .dashicons { font-size: 22px; width: 22px; height: 22px; }
            .manipay-header h1 { margin: 0 0 4px; padding: 0; font-size: 22px; }
            .manipay-header p { margin: 0; color: #5b6a63; max-width: 720px; line-height: 1.6; }

            .manipay-stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-bottom: 18px; max-width: 720px; }
            .manipay-stat { display: block; background: #fff; border: 1px solid #e4e0d2; border-radius: 12px; padding: 14px 16px; text-decoration: none; transition: border-color .15s ease, box-shadow .15s ease; }
            .manipay-stat:hover { border-color: #0b6e4f; box-shadow: 0 1px 4px rgba(11,110,79,0.12); }
            .manipay-stat.is-active { border-color: #0b6e4f; box-shadow: inset 0 0 0 1px #0b6e4f; }
            .manipay-stat-num { display: block; font-size: 24px; font-weight: 700; color: #15241d; line-height: 1.2; }
            .manipay-stat-label { display: block; font-size: 12.5px; color: #5b6a63; margin-top: 2px; }
            .manipay-stat-pending .manipay-stat-num { color: #b7791f; }
            .manipay-stat-paid .manipay-stat-num { color: #0b6e4f; }

            .manipay-toolbar { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; background: #fbfaf6; border: 1px solid #e4e0d2; border-radius: 12px; padding: 10px 14px; margin-bottom: 16px; }
            .manipay-toolbar label { display: inline-flex; align-items: center; gap: 5px; font-weight: 600; color: #15241d; font-size: 13px; }
            .manipay-toolbar label .dashicons { font-size: 16px; width: 16px; height: 16px; color: #5b6a63; }
            .manipay-toolbar select { border-radius: 8px; border-color: #e4e0d2; }
            .manipay-clear-filter { font-size: 13px; color: #d7263d; text-decoration: none; }
            .manipay-clear-filter:hover { text-decoration: underline; }
            .manipay-result-count { margin-left: auto; font-size: 12.5px; color: #5b6a63; }

            .manipay-table-card { background: #fff; border: 1px solid #e4e0d2; border-radius: 12px; overflow: hidden; }
            .manipay-table-scroll { overflow-x: auto; }
            .manipay-orders-table { width: 100%; border-collapse: collapse; font-size: 13px; }
            .manipay-orders-table thead th { text-align: left; font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.04em; color: #5b6a63; background: #fbfaf6; border-bottom: 1px solid #e4e0d2; padding: 10px 14px; white-space: nowrap; }
            .manipay-orders-table tbody td { padding: 12px 14px; border-bottom: 1px solid #f0eee5; vertical-align: top; }
            .manipay-orders-table tbody tr:last-child td { border-bottom: none; }
            .manipay-orders-table tbody tr:hover { background: #fbfaf6; }

            .manipay-order-no { font-weight: 600; color: #15241d; }
            .manipay-date { color: #5b6a63; white-space: nowrap; }
            .manipay-muted { color: #8a938c; }
            .manipay-amount { font-weight: 600; color: #15241d; white-space: nowrap; }

            .manipay-customer { display: flex; align-items: center; gap: 8px; min-width: 140px; }
            .manipay-avatar { width: 26px; height: 26px; border-radius: 50%; background: #e7f3ee; color: #084f39; font-size: 12px; font-weight: 700; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }

            .manipay-method-pill { display: inline-block; background: #f0eee5; color: #15241d; font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 999px; white-space: nowrap; }

            .manipay-details-cell { min-width: 200px; }
            .manipay-detail-line { font-size: 12.5px; margin-bottom: 4px; color: #15241d; }
            .manipay-detail-line:last-child { margin-bottom: 0; }
            .manipay-detail-label { color: #8a938c; margin-right: 5px; }
            .manipay-detail-line code { background: #f0eee5; padding: 1px 6px; border-radius: 5px; font-size: 12px; }
            .manipay-detail-line a { display: inline-flex; align-items: center; gap: 4px; color: #0b6e4f; text-decoration: none; }
            .manipay-detail-line a:hover { text-decoration: underline; }
            .manipay-detail-line a .dashicons { font-size: 14px; width: 14px; height: 14px; }

            .manipay-badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
            .manipay-badge-pending { background: #fbeed4; color: #8a5a12; }
            .manipay-badge-paid { background: #0b6e4f; color: #fff; }
            .manipay-badge-failed { background: #d7263d; color: #fff; }

            .manipay-view-btn { display: inline-flex; align-items: center; gap: 4px; border: 1px solid #0b6e4f; color: #0b6e4f; background: #fff; font-size: 12.5px; font-weight: 600; padding: 5px 10px; border-radius: 8px; text-decoration: none; white-space: nowrap; }
            .manipay-view-btn:hover { background: #0b6e4f; color: #fff; }
            .manipay-view-btn .dashicons { font-size: 14px; width: 14px; height: 14px; }

            .manipay-empty-state { text-align: center; padding: 46px 20px; color: #5b6a63; }
            .manipay-empty-state .dashicons { font-size: 34px; width: 34px; height: 34px; color: #c9c3af; margin-bottom: 8px; }
            .manipay-empty-state p { margin: 0; font-size: 14px; }

            .manipay-pagination { margin-top: 14px; }

            @media (max-width: 782px) {
                .manipay-stats { grid-template-columns: 1fr; max-width: none; }
                .manipay-result-count { margin-left: 0; width: 100%; }
            }
        </style>
        <?php
    }
}
