<?php
session_start();
if(!isset($_SESSION['username'])) {
    header("Location: ../login.php");
    exit();
}

include('../includes/db.php');

$supplier_id = isset($_GET['supplier_id']) ? (int)$_GET['supplier_id'] : 0;
$start_date = isset($_GET['start_date']) ? $_GET['start_date'] : '';
$end_date = isset($_GET['end_date']) ? $_GET['end_date'] : '';
$filter_applied = false;

// Get all suppliers for dropdown
$all_suppliers_sql = "SELECT id, name, phone FROM suppliers ORDER BY name";
$all_suppliers_result = mysqli_query($conn, $all_suppliers_sql);

// Get supplier details if selected
$supplier = null;
$transactions = [];
$total_purchases = 0;
$total_payments = 0;
$final_balance = 0;

if($supplier_id > 0) {
    $supplier_sql = "SELECT * FROM suppliers WHERE id = '$supplier_id'";
    $supplier_result = mysqli_query($conn, $supplier_sql);
    $supplier = mysqli_fetch_assoc($supplier_result);
    
    if($supplier) {
        $filter_applied = (!empty($start_date) || !empty($end_date));
        
        // Build date filter condition
        $date_condition = "";
        if(!empty($start_date) && !empty($end_date)) {
            $date_condition = "AND DATE(date_time) BETWEEN '$start_date' AND '$end_date'";
        } elseif(!empty($start_date)) {
            $date_condition = "AND DATE(date_time) >= '$start_date'";
        } elseif(!empty($end_date)) {
            $date_condition = "AND DATE(date_time) <= '$end_date'";
        }
        
        // Get purchases with date filter
        $purchases_sql = "SELECT p.*, f.name as fuel_name, t.tank_name 
                          FROM purchases p 
                          LEFT JOIN fuel_types f ON p.fuel_type_id = f.id 
                          LEFT JOIN tanks t ON p.tank_id = t.id 
                          WHERE p.supplier_id = '$supplier_id' $date_condition
                          ORDER BY p.date_time ASC";
        $purchases_result = mysqli_query($conn, $purchases_sql);
        
        // Get payments with date filter
        $payments_sql = "SELECT * FROM supplier_payments 
                         WHERE supplier_id = '$supplier_id' $date_condition 
                         ORDER BY date_time ASC";
        $payments_result = mysqli_query($conn, $payments_sql);
        
        // Get purchases before filter date for opening balance calculation
        $opening_balance_condition = "";
        if(!empty($start_date)) {
            $opening_balance_condition = "AND DATE(date_time) < '$start_date'";
        }
        
        $prev_purchases_sql = "SELECT SUM(total_amount) as total FROM purchases 
                               WHERE supplier_id = '$supplier_id' $opening_balance_condition";
        $prev_purchases_res = mysqli_query($conn, $prev_purchases_sql);
        $prev_purchases = mysqli_fetch_assoc($prev_purchases_res);
        
        $prev_payments_sql = "SELECT SUM(amount) as total FROM supplier_payments 
                              WHERE supplier_id = '$supplier_id' $opening_balance_condition";
        $prev_payments_res = mysqli_query($conn, $prev_payments_sql);
        $prev_payments = mysqli_fetch_assoc($prev_payments_res);
        
        $opening_balance = $supplier['opening_balance'] + ($prev_purchases['total'] ?? 0) - ($prev_payments['total'] ?? 0);
        
        // Build transactions
        $balance = $opening_balance;
        
        // Add opening balance transaction if has value or filter applied
        if($opening_balance != 0 || $filter_applied) {
            $transactions[] = [
                'date' => $start_date ? $start_date . ' (Before)' : $supplier['date_time'],
                'type' => 'Opening Balance',
                'purchase' => 0,
                'payment' => 0,
                'balance' => $opening_balance,
                'reference' => 'Opening balance as of ' . ($start_date ? date('d-m-Y', strtotime($start_date)) : 'registration')
            ];
        }
        
        mysqli_data_seek($purchases_result, 0);
        while($purchase = mysqli_fetch_assoc($purchases_result)) {
            $balance += $purchase['total_amount'];
            $total_purchases += $purchase['total_amount'];
            $transactions[] = [
                'date' => $purchase['date_time'],
                'type' => 'Purchase',
                'purchase' => $purchase['total_amount'],
                'payment' => 0,
                'balance' => $balance,
                'reference' => 'DO: ' . $purchase['do_number'] . ' | ' . $purchase['fuel_name'] . ' | Qty: ' . $purchase['quantity'] . 'L'
            ];
        }
        
        mysqli_data_seek($payments_result, 0);
        while($payment = mysqli_fetch_assoc($payments_result)) {
            $balance -= $payment['amount'];
            $total_payments += $payment['amount'];
            $transactions[] = [
                'date' => $payment['date_time'],
                'type' => 'Payment',
                'purchase' => 0,
                'payment' => $payment['amount'],
                'balance' => $balance,
                'reference' => $payment['notes'] ?: 'Payment received'
            ];
        }
        
        usort($transactions, function($a, $b) {
            return strtotime($a['date']) - strtotime($b['date']);
        });
        
        $final_balance = $supplier['current_balance'];
        if($filter_applied) {
            $final_balance = $balance;
        }
    }
}

// Get all suppliers closing data for summary
$all_suppliers_summary = [];
$all_suppliers_query = "SELECT id, name, phone, opening_balance, current_balance FROM suppliers ORDER BY name";
$all_suppliers_summary_result = mysqli_query($conn, $all_suppliers_query);
$total_outstanding_all = 0;

while($sup = mysqli_fetch_assoc($all_suppliers_summary_result)) {
    $total_outstanding_all += $sup['current_balance'];
    $all_suppliers_summary[] = $sup;
}
?>

<?php include('../includes/log.php'); ?>
<?php include('../includes/sd.php'); ?>

<div class="closing-container">
    <div class="page-header">
        <div class="header-left">
            <h2><i class="fas fa-file-invoice"></i> Supplier Closing Report</h2>
            <p>View detailed supplier account statement with transactions history</p>
        </div>
        <div class="header-right">
            <a href="suppliers.php" class="btn-back"><i class="fas fa-arrow-left"></i> Back to Suppliers</a>
        </div>
    </div>

    <!-- All Suppliers Summary Section -->
    <div class="all-suppliers-card">
        <div class="all-suppliers-header">
            <i class="fas fa-chart-pie"></i>
            <h4>All Suppliers Summary</h4>
            <span class="total-outstanding" style="color:#fff;">Total Outstanding: Rs <?php echo number_format($total_outstanding_all, 2); ?></span>
        </div>
        <div class="all-suppliers-body">
            <div class="suppliers-summary-grid">
                <?php foreach($all_suppliers_summary as $sup): ?>
                    <div class="supplier-summary-item <?php echo ($supplier_id == $sup['id']) ? 'active' : ''; ?>">
                        <div class="supplier-summary-icon"><i class="fas fa-truck"></i></div>
                        <div class="supplier-summary-info">
                            <h5><?php echo htmlspecialchars($sup['name']); ?></h5>
                            <small><?php echo htmlspecialchars($sup['phone']); ?></small>
                            <div class="balance-info <?php echo $sup['current_balance'] > 0 ? 'due' : 'clear'; ?>">
                                Rs <?php echo number_format($sup['current_balance'], 2); ?>
                            </div>
                        </div>
                        <a href="?supplier_id=<?php echo $sup['id']; ?><?php echo $start_date ? '&start_date=' . $start_date : ''; ?><?php echo $end_date ? '&end_date=' . $end_date : ''; ?>" class="view-link">
                            <i class="fas fa-chevron-right"></i>
                        </a>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>

    <!-- Filter Section -->
    <div class="selection-card">
        <div class="selection-header">
            <i class="fas fa-filter"></i>
            <h4>Filter & Select Supplier</h4>
        </div>
        <div class="selection-body">
            <form method="GET" action="" class="filter-form">
                <div class="filter-row">
                    <div class="filter-group">
                        <label><i class="fas fa-calendar-alt"></i> Start Date</label>
                        <input type="date" name="start_date" class="date-input" value="<?php echo $start_date; ?>">
                    </div>
                    <div class="filter-group">
                        <label><i class="fas fa-calendar-alt"></i> End Date</label>
                        <input type="date" name="end_date" class="date-input" value="<?php echo $end_date; ?>">
                    </div>
                    <div class="filter-group select-group">
                        <label><i class="fas fa-user"></i> Select Supplier</label>
                        <div class="select-wrapper">
                            <select name="supplier_id" class="supplier-select">
                                <option value="">-- All Suppliers --</option>
                                <?php 
                                mysqli_data_seek($all_suppliers_result, 0);
                                while($sup = mysqli_fetch_assoc($all_suppliers_result)): 
                                ?>
                                    <option value="<?php echo $sup['id']; ?>" <?php echo ($supplier_id == $sup['id']) ? 'selected' : ''; ?>>
                                        <?php echo htmlspecialchars($sup['name']); ?> - <?php echo htmlspecialchars($sup['phone']); ?>
                                    </option>
                                <?php endwhile; ?>
                            </select>
                            <i class="fas fa-chevron-down select-arrow"></i>
                        </div>
                    </div>
                    <div class="filter-actions">
                        <button type="submit" class="btn-filter"><i class="fas fa-search"></i> Apply Filter</button>
                        <?php if($filter_applied || $supplier_id > 0): ?>
                            <a href="supplier_closing.php" class="btn-reset"><i class="fas fa-undo-alt"></i> Reset</a>
                        <?php endif; ?>
                        <?php if($supplier_id > 0 && count($transactions) > 0): ?>
                            <button type="button" class="btn-print" onclick="window.print()"><i class="fas fa-print"></i> Print</button>
                        <?php endif; ?>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <?php if($supplier_id > 0 && $supplier): ?>
        <div class="supplier-info-card">
            <div class="supplier-avatar"><i class="fas fa-building"></i></div>
            <div class="supplier-details">
                <h3><?php echo htmlspecialchars($supplier['name']); ?></h3>
                <div class="info-grid">
                    <div class="info-row"><span><i class="fas fa-user"></i> Contact:</span><strong><?php echo htmlspecialchars($supplier['contact_person'] ?: 'N/A'); ?></strong></div>
                    <div class="info-row"><span><i class="fas fa-phone"></i> Phone:</span><strong><?php echo htmlspecialchars($supplier['phone']); ?></strong></div>
                    <div class="info-row"><span><i class="fas fa-envelope"></i> Email:</span><strong><?php echo htmlspecialchars($supplier['email'] ?: 'N/A'); ?></strong></div>
                    <div class="info-row"><span><i class="fas fa-calendar-alt"></i> Registered:</span><strong><?php echo date('d-m-Y', strtotime($supplier['date_time'])); ?></strong></div>
                </div>
            </div>
        </div>

        <div class="summary-row">
            <div class="summary-card opening">
                <div class="summary-icon"><i class="fas fa-play-circle"></i></div>
                <div class="summary-content">
                    <p>Opening Balance</p>
                    <h4>Rs <?php echo number_format($opening_balance, 2); ?></h4>
                </div>
            </div>
            <div class="summary-card purchase">
                <div class="summary-icon"><i class="fas fa-shopping-cart"></i></div>
                <div class="summary-content">
                    <p>Total Purchases</p>
                    <h4>Rs <?php echo number_format($total_purchases, 2); ?></h4>
                </div>
            </div>
            <div class="summary-card payment">
                <div class="summary-icon"><i class="fas fa-hand-holding-usd"></i></div>
                <div class="summary-content">
                    <p>Total Payments</p>
                    <h4>Rs <?php echo number_format($total_payments, 2); ?></h4>
                </div>
            </div>
            <div class="summary-card final <?php echo $final_balance > 0 ? 'due' : 'clear'; ?>">
                <div class="summary-icon"><i class="fas fa-chart-line"></i></div>
                <div class="summary-content">
                    <p>Closing Balance</p>
                    <h4>Rs <?php echo number_format($final_balance, 2); ?></h4>
                    <span class="status-label"><?php echo $final_balance > 0 ? 'Due to Supplier' : ($final_balance < 0 ? 'Supplier Owes You' : 'Settled'); ?></span>
                </div>
            </div>
        </div>
        <div class="table-card">
            <div class="table-card-header">
                <div class="header-info"><i class="fas fa-list-ul"></i><h4>Transaction History</h4></div>
                <div class="table-stats"><i class="fas fa-exchange-alt"></i> Total Transactions: <?php echo count($transactions); ?></div>
            </div>
            <div class="table-card-body">
                <div class="table-responsive">
                    <table class="transactions-table">
                        <thead>
                            <tr>
                                <th style="width: 50px;">#</th>
                                <th>Date & Time</th>
                                <th>Transaction Type</th>
                                <th>Purchase (Rs)</th>
                                <th>Payment (Rs)</th>
                                <th>Running Balance (Rs)</th>
                                <th>Reference</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if(count($transactions) > 0): $sn = 1; foreach($transactions as $trans): ?>
                                <tr class="<?php echo $trans['type'] == 'Purchase' ? 'purchase-row' : ($trans['type'] == 'Payment' ? 'payment-row' : 'opening-row'); ?>">
                                    <td class="text-center"><?php echo $sn++; ?> </div>
                                     </div>
                                      </div>
                                    </div>
                                    </div>
                                    </td>
                                    <td><?php echo date('d-m-Y h:i A', strtotime($trans['date'])); ?> </div>
                                      </div>
                                    </div>
                                    </div>
                                    </div>
                                    </td>
                                    <td>
                                        <span class="transaction-badge <?php echo strtolower(str_replace(' ', '-', $trans['type'])); ?>">
                                            <?php if($trans['type'] == 'Purchase'): ?>
                                                <i class="fas fa-arrow-up"></i>
                                            <?php elseif($trans['type'] == 'Payment'): ?>
                                                <i class="fas fa-arrow-down"></i>
                                            <?php else: ?>
                                                <i class="fas fa-hourglass-start"></i>
                                            <?php endif; ?>
                                            <?php echo $trans['type']; ?>
                                        </span>
                                     </div>
                                      </div>
                                    </div>
                                    </div>
                                    </div>
                                    </td>
                                    <td class="purchase-amount"><?php echo $trans['purchase'] > 0 ? 'Rs ' . number_format($trans['purchase'], 2) : '-'; ?> </div>
                                      </div>
                                    </div>
                                    </div>
                                    </div>
                                    </td>
                                    <td class="payment-amount"><?php echo $trans['payment'] > 0 ? 'Rs ' . number_format($trans['payment'], 2) : '-'; ?> </div>
                                      </div>
                                    </div>
                                    </div>
                                    </div>
                                    </td>
                                    <td class="balance-amount">Rs <?php echo number_format($trans['balance'], 2); ?> </div>
                                      </div>
                                    </div>
                                    </div>
                                    </div>
                                    </td>
                                    <td class="reference-cell"><?php echo htmlspecialchars($trans['reference']); ?> </div>
                                      </div>
                                    </div>
                                    </div>
                                    </div>
                                    </tr>
                            <?php endforeach; else: ?>
                                <tr>
                                    <td colspan="7" class="empty-state">
                                        <i class="fas fa-receipt"></i>
                                        <p>No transactions found for this period</p>
                                        <small>Try changing the date range</small>
                                     </div>
                                      </div>
                                    </div>
                                    </div>
                                    </tr>
                            <?php endif; ?>
                        </tbody>
                        <?php if(count($transactions) > 0): ?>
                            <tfoot>
                                <tr class="footer-row">
                                    <td colspan="3"><strong>Totals</strong></td>
                                    <td class="purchase-amount"><strong>Rs <?php echo number_format($total_purchases, 2); ?></strong></td>
                                    <td class="payment-amount"><strong>Rs <?php echo number_format($total_payments, 2); ?></strong></td>
                                    <td class="balance-amount"><strong>Rs <?php echo number_format($final_balance, 2); ?></strong></td>
                                    <td></td>
                                </table>
                            </tfoot>
                        <?php endif; ?>
                    </div>
                      </div>
                    </div>
                </div>
            </div>
        </div>
        
    <?php elseif($supplier_id > 0): ?>
        <div class="error-card">
            <i class="fas fa-exclamation-triangle"></i>
            <h4>Supplier Not Found</h4>
            <p>The selected supplier does not exist.</p>
            <a href="suppliers.php" class="btn-primary">Go to Suppliers</a>
        </div>
    <?php else: ?>
        <div class="welcome-card">
            <i class="fas fa-truck"></i>
            <h4>Select a Supplier</h4>
            <p>Please select a supplier from the dropdown above or click on any supplier card to view their closing report.</p>
        </div>
    <?php endif; ?>
</div>

<style>
/* Google Fonts - Poppins */
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700;800&display=swap');

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Poppins', sans-serif;
}

body {
    background: #f5f7fb;
}

/* Headings - Times New Roman */
h1, h2, h3, h4, h5, h6,
.page-header h2,
.all-suppliers-header h4,
.selection-header h4,
.supplier-details h3,
.header-info h4,
.transactions-table th {
    font-family: 'Times New Roman', Times, serif;
}

.closing-container {
    max-width: 1600px;
    margin: 0 auto;
    padding: 0 24px;
}

/* Page Header */
.page-header {
    background: linear-gradient(135deg, #A04657 0%, #6b2a36 100%);
    border-radius: 24px;
    padding: 28px 36px;
    margin-bottom: 28px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    box-shadow: 0 6px 20px rgba(160,70,87,0.25);
}

.page-header h2 {
    font-size: 26px;
    margin-bottom: 8px;
}

.page-header h2 i {
    margin-right: 12px;
}

.page-header p {
    font-size: 14px;
    opacity: 0.9;
}

.btn-back {
    background: rgba(255,255,255,0.2);
    color: #fff;
    padding: 10px 22px;
    border-radius: 40px;
    text-decoration: none;
    font-weight: 500;
    font-size: 14px;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    transition: all 0.3s;
}

.btn-back:hover {
    background: rgba(255,255,255,0.3);
    transform: translateX(-3px);
}

/* All Suppliers Summary Card */
.all-suppliers-card {
    background: #fff;
    border-radius: 24px;
    margin-bottom: 28px;
    border: 1px solid #eef2f6;
    overflow: hidden;
}

.all-suppliers-header {
    background: linear-gradient(135deg, #A04657 0%, #6b2a36 100%);
    padding: 18px 24px;
    display: flex;
    align-items: center;
    gap: 12px;
    color: #fff;
}

.all-suppliers-header i {
    font-size: 22px;
}

.all-suppliers-header h4 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
}

.total-outstanding {
    margin-left: auto;
    background: rgba(255,255,255,0.2);
    padding: 6px 16px;
    border-radius: 40px;
    font-size: 13px;
    font-weight: 500;
}

.all-suppliers-body {
    padding: 20px;
}

.suppliers-summary-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 16px;
}

.supplier-summary-item {
    background: #f8fafc;
    border-radius: 16px;
    padding: 16px;
    display: flex;
    align-items: center;
    gap: 14px;
    transition: all 0.3s;
    border: 1px solid #eef2f6;
    text-decoration: none;
}

.supplier-summary-item:hover {
    background: #fefaf8;
    border-color: #A04657;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(160,70,87,0.1);
}

.supplier-summary-item.active {
    background: #fef5f3;
    border-left: 4px solid #A04657;
}

.supplier-summary-icon i {
    font-size: 36px;
    color: #A04657;
}

.supplier-summary-info {
    flex: 1;
}

.supplier-summary-info h5 {
    font-size: 16px;
    font-weight: 700;
    color: #1e293b;
    margin-bottom: 4px;
}

.supplier-summary-info small {
    font-size: 12px;
    color: #64748b;
    display: block;
    margin-bottom: 6px;
}

.balance-info {
    font-size: 14px;
    font-weight: 700;
}

.balance-info.due {
    color: #dc3545;
}

.balance-info.clear {
    color: #28a745;
}

.view-link {
    color: #A04657;
    font-size: 18px;
    text-decoration: none;
    padding: 8px;
}

/* Filter Section */
.selection-card {
    background: #fff;
    border-radius: 24px;
    margin-bottom: 28px;
    border: 1px solid #eef2f6;
    overflow: hidden;
}

.selection-header {
    background: #f8fafc;
    padding: 16px 24px;
    border-bottom: 1px solid #eef2f6;
    display: flex;
    align-items: center;
    gap: 10px;
}

.selection-header i {
    color: #A04657;
    font-size: 18px;
}

.selection-header h4 {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
}

.selection-body {
    padding: 20px 24px;
}

.filter-form {
    width: 100%;
}

.filter-row {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
    align-items: flex-end;
}

.filter-group {
    flex: 1;
    min-width: 180px;
}

.filter-group label {
    display: block;
    margin-bottom: 8px;
    font-weight: 600;
    font-size: 13px;
    color: #475569;
}

.filter-group label i {
    margin-right: 6px;
    color: #A04657;
}

.date-input {
    width: 100%;
    padding: 12px 14px;
    border: 2px solid #e2e8f0;
    border-radius: 14px;
    font-size: 14px;
    font-weight: 500;
    transition: all 0.3s;
}

.date-input:focus {
    border-color: #A04657;
    outline: none;
    box-shadow: 0 0 0 3px rgba(160,70,87,0.1);
}

.select-group {
    flex: 1.5;
}

.select-wrapper {
    position: relative;
}

.supplier-select {
    width: 100%;
    padding: 12px 40px 12px 14px;
    border: 2px solid #e2e8f0;
    border-radius: 14px;
    font-size: 14px;
    font-weight: 500;
    background: #fff;
    cursor: pointer;
    appearance: none;
}

.supplier-select:focus {
    border-color: #A04657;
    outline: none;
}

.select-arrow {
    position: absolute;
    right: 16px;
    top: 50%;
    transform: translateY(-50%);
    color: #94a3b8;
    pointer-events: none;
}

.filter-actions {
    display: flex;
    gap: 12px;
}

.btn-filter, .btn-reset, .btn-print {
    padding: 12px 22px;
    border-radius: 40px;
    font-weight: 600;
    font-size: 13px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    transition: all 0.3s;
    border: none;
}

.btn-filter {
    background: #A04657;
    color: #fff;
}

.btn-filter:hover {
    background: #6b2a36;
    transform: translateY(-2px);
}

.btn-reset {
    background: #f1f5f9;
    color: #475569;
    text-decoration: none;
}

.btn-reset:hover {
    background: #e2e8f0;
}

.btn-print {
    background: #f1f5f9;
    color: #475569;
}

.btn-print:hover {
    background: #A04657;
    color: #fff;
}

/* Supplier Info Card */
.supplier-info-card {
    background: #fff;
    border-radius: 24px;
    padding: 24px;
    margin-bottom: 28px;
    display: flex;
    gap: 24px;
    align-items: center;
    border: 1px solid #eef2f6;
}

.supplier-avatar i {
    font-size: 64px;
    color: #A04657;
}

.supplier-details h3 {
    margin: 0 0 12px 0;
    font-size: 22px;
    font-weight: 700;
}

.info-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 24px;
}

.info-row {
    display: flex;
    gap: 8px;
    font-size: 14px;
}

.info-row span {
    color: #64748b;
    font-weight: 500;
}

.info-row strong {
    color: #1e293b;
    font-weight: 600;
}

/* Summary Cards */
.summary-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 20px;
    margin-bottom: 28px;
}

.summary-card {
    background: #fff;
    border-radius: 24px;
    padding: 20px;
    display: flex;
    align-items: center;
    gap: 18px;
    border: 1px solid #eef2f6;
    transition: all 0.3s;
}

.summary-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 8px 20px rgba(0,0,0,0.06);
}

.summary-icon {
    width: 60px;
    height: 60px;
    background: rgba(160,70,87,0.1);
    border-radius: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.summary-icon i {
    font-size: 28px;
    color: #A04657;
}

.summary-card.opening .summary-icon i { color: #6c5ce7; }
.summary-card.purchase .summary-icon i { color: #dc3545; }
.summary-card.payment .summary-icon i { color: #28a745; }

.summary-content p {
    margin: 0 0 4px 0;
    font-size: 12px;
    color: #64748b;
    font-weight: 500;
}

.summary-content h4 {
    margin: 0;
    font-size: 22px;
    font-weight: 700;
    color: #1e293b;
}

.summary-card.final.due .summary-content h4 { color: #dc3545; }
.summary-card.final.clear .summary-content h4 { color: #28a745; }

.status-label {
    font-size: 11px;
    padding: 3px 10px;
    border-radius: 20px;
    background: #f1f5f9;
    display: inline-block;
    margin-top: 6px;
    font-weight: 500;
}

/* Formula Card */
.formula-card {
    background: #f8fafc;
    border-radius: 20px;
    padding: 18px 24px;
    margin-bottom: 28px;
    border: 1px solid #eef2f6;
}

.formula-header {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 12px;
}

.formula-header i {
    color: #A04657;
    font-size: 18px;
}

.formula-header span {
    font-weight: 700;
    font-size: 14px;
    color: #1e293b;
}

.formula-body code {
    font-family: monospace;
    background: #fff;
    padding: 8px 14px;
    border-radius: 12px;
    font-size: 13px;
    display: inline-block;
    margin-bottom: 10px;
    border: 1px solid #e2e8f0;
}

.formula-example {
    font-size: 13px;
    color: #475569;
    margin-top: 8px;
}

.filter-note {
    margin-top: 10px;
    padding: 8px 12px;
    background: #e3f2fd;
    border-radius: 10px;
    font-size: 12px;
    color: #1565c0;
}

.filter-note i {
    margin-right: 6px;
}

/* Table Card */
.table-card {
    background: #fff;
    border-radius: 24px;
    margin-bottom: 28px;
    border: 1px solid #eef2f6;
    overflow: hidden;
}

.table-card-header {
    padding: 18px 24px;
    border-bottom: 1px solid #eef2f6;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 12px;
}

.header-info {
    display: flex;
    align-items: center;
    gap: 10px;
}

.header-info i {
    font-size: 22px;
    color: #A04657;
}

.header-info h4 {
    margin: 0;
    font-size: 18px;
    font-weight: 700;
}

.table-stats {
    font-size: 13px;
    color: #64748b;
    font-weight: 500;
}

.table-stats i {
    margin-right: 6px;
    color: #A04657;
}

.table-responsive {
    overflow-x: auto;
}

/* Transactions Table */
.transactions-table {
    width: 100%;
    border-collapse: collapse;
    min-width: 900px;
}

.transactions-table th {
    padding: 16px 16px;
    background: #fafbfc;
    font-weight: 700;
    color: #475569;
    font-size: 13px;
    border-bottom: 2px solid #eef2f6;
    text-align: left;
}

.transactions-table td {
    padding: 14px 16px;
    border-bottom: 1px solid #f0f4f9;
    font-size: 13px;
    font-weight: 500;
    color: #334155;
    vertical-align: middle;
}

.transactions-table tr:hover td {
    background: #fefaf8;
}

/* Transaction Badge */
.transaction-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 5px 14px;
    border-radius: 40px;
    font-size: 12px;
    font-weight: 600;
}

.transaction-badge.opening-balance {
    background: #e8e8ff;
    color: #6c5ce7;
}

.transaction-badge.purchase {
    background: #ffebee;
    color: #dc3545;
}

.transaction-badge.payment {
    background: #e8f5e9;
    color: #28a745;
}

/* Amount Columns */
.purchase-amount {
    color: #dc3545;
    font-weight: 700;
}

.payment-amount {
    color: #28a745;
    font-weight: 700;
}

.balance-amount {
    font-weight: 800;
    color: #1e293b;
}

.text-center {
    text-align: center;
}

.reference-cell {
    font-size: 12px;
    color: #64748b;
    max-width: 250px;
    word-break: break-word;
}

/* Footer Row */
.footer-row {
    background: #f8fafc;
    border-top: 2px solid #eef2f6;
}

.footer-row td {
    font-weight: 800;
    font-size: 14px;
}

/* Empty & Error States */
.empty-state {
    text-align: center;
    padding: 50px 20px !important;
}

.empty-state i {
    font-size: 56px;
    color: #A04657;
    opacity: 0.35;
    margin-bottom: 14px;
    display: block;
}

.empty-state p {
    font-size: 15px;
    font-weight: 600;
    color: #556b82;
    margin-bottom: 6px;
}

.empty-state small {
    font-size: 13px;
    color: #94a3b8;
}

.error-card, .welcome-card {
    background: #fff;
    border-radius: 24px;
    padding: 50px 30px;
    text-align: center;
    border: 1px solid #eef2f6;
}

.error-card i, .welcome-card i {
    font-size: 64px;
    color: #A04657;
    opacity: 0.5;
    margin-bottom: 20px;
    display: block;
}

.error-card h4, .welcome-card h4 {
    font-size: 20px;
    margin-bottom: 10px;
    color: #1e293b;
}

.error-card p, .welcome-card p {
    font-size: 14px;
    color: #64748b;
    margin-bottom: 20px;
}

.btn-primary {
    background: #A04657;
    color: #fff;
    padding: 12px 28px;
    border-radius: 40px;
    text-decoration: none;
    font-weight: 600;
    display: inline-block;
}

/* Responsive */
@media (max-width: 1200px) {
    .suppliers-summary-grid {
        grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    }
}

@media (max-width: 768px) {
    .closing-container {
        padding: 0 16px;
    }
    
    .page-header {
        flex-direction: column;
        text-align: center;
        gap: 16px;
        padding: 24px;
    }
    
    .supplier-info-card {
        flex-direction: column;
        text-align: center;
    }
    
    .info-grid {
        justify-content: center;
    }
    
    .summary-row {
        grid-template-columns: 1fr;
    }
    
    .filter-row {
        flex-direction: column;
    }
    
    .filter-actions {
        width: 100%;
        justify-content: stretch;
    }
    
    .btn-filter, .btn-reset, .btn-print {
        flex: 1;
        justify-content: center;
    }
    
    .all-suppliers-header {
        flex-wrap: wrap;
    }
    
    .total-outstanding {
        margin-left: 0;
        width: 100%;
        text-align: center;
    }
}

/* Print Styles */
@media print {
    .page-header, .selection-card, .all-suppliers-card, .btn-print, .btn-back, .formula-card, .filter-actions {
        display: none;
    }
    
    body {
        background: #fff;
    }
    
    .summary-card, .table-card, .supplier-info-card {
        break-inside: avoid;
        box-shadow: none;
        border: 1px solid #ddd;
    }
    
    .closing-container {
        padding: 0;
    }
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 10px;
}

::-webkit-scrollbar-thumb {
    background: #A04657;
    border-radius: 10px;
}

::-webkit-scrollbar-thumb:hover {
    background: #6b2a36;
}
</style>

<script>
// Auto-submit form when supplier is selected
document.querySelector('.supplier-select')?.addEventListener('change', function() {
    this.form.submit();
});

// Print functionality
function printReport() {
    window.print();
}
</script>

<?php include('../includes/ft.php'); ?>