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

include('includes/db.php');

// ============================================
// FINANCIAL SUMMARY CALCULATIONS - ACCURATE VERSION
// ============================================

// 1. TOTAL SALES VALUE (Actual calculation from nozzle_readings with fuel prices)
$total_sales_sql = "SELECT 
                    SUM(nr.sale_liters * ft.sale_price) as total_sales_value,
                    SUM(nr.sale_liters) as total_liters
                    FROM nozzle_readings nr
                    LEFT JOIN nozzles n ON nr.nozzle_id = n.id
                    LEFT JOIN tanks t ON n.tank_id = t.id
                    LEFT JOIN fuel_types ft ON t.fuel_type_id = ft.id";
$total_sales_result = mysqli_query($conn, $total_sales_sql);
$total_sales_data = mysqli_fetch_assoc($total_sales_result);
$total_sales = $total_sales_data['total_sales_value'] ? $total_sales_data['total_sales_value'] : 0;
$total_liters_sold = $total_sales_data['total_liters'] ? $total_sales_data['total_liters'] : 0;

// 2. CREDIT SALES
$credit_query = "SELECT SUM(amount) as total_credit FROM credit_sales";
$credit_result = mysqli_query($conn, $credit_query);
$credit_data = mysqli_fetch_assoc($credit_result);
$total_credit = $credit_data['total_credit'] ? $credit_data['total_credit'] : 0;

// 3. CASH SALES (Calculated)
$cash_sales = $total_sales - $total_credit;

// 4. TOTAL EXPENSES
$expenses_query = "SELECT SUM(amount) as total_expenses FROM expenses WHERE amount > 0";
$expenses_result = mysqli_query($conn, $expenses_query);
$expenses_data = mysqli_fetch_assoc($expenses_result);
$total_expenses = $expenses_data['total_expenses'] ? $expenses_data['total_expenses'] : 0;

// If amount column empty, try expense_amount
if($total_expenses == 0) {
    $expenses_query2 = "SELECT SUM(expense_amount) as total_expenses FROM expenses WHERE expense_amount > 0";
    $expenses_result2 = mysqli_query($conn, $expenses_query2);
    $expenses_data2 = mysqli_fetch_assoc($expenses_result2);
    $total_expenses = $expenses_data2['total_expenses'] ? $expenses_data2['total_expenses'] : 0;
}

// 5. SUPPLIER PAYMENTS (Cash Outflow)
$supplier_payments_query = "SELECT SUM(amount) as total_supplier_payments FROM supplier_payments";
$supplier_payments_result = mysqli_query($conn, $supplier_payments_query);
$supplier_payments_data = mysqli_fetch_assoc($supplier_payments_result);
$total_supplier_payments = $supplier_payments_data['total_supplier_payments'] ? $supplier_payments_data['total_supplier_payments'] : 0;

// 6. CUSTOMER RECOVERY (Cash Inflow)
$recovery_query = "SELECT SUM(amount) as total_recovery FROM customer_payments WHERE payment_type != 'opening_balance' OR payment_type IS NULL";
$recovery_result = mysqli_query($conn, $recovery_query);
$recovery_data = mysqli_fetch_assoc($recovery_result);
$total_recovery = $recovery_data['total_recovery'] ? $recovery_data['total_recovery'] : 0;

// 7. NET CASH
$net_cash = $cash_sales - $total_expenses - $total_supplier_payments + $total_recovery;

// Get sales breakdown by fuel type for debugging/info
$sales_breakdown_sql = "SELECT 
                        ft.name as fuel_name,
                        ft.sale_price,
                        SUM(nr.sale_liters) as liters,
                        SUM(nr.sale_liters * ft.sale_price) as amount
                        FROM nozzle_readings nr
                        LEFT JOIN nozzles n ON nr.nozzle_id = n.id
                        LEFT JOIN tanks t ON n.tank_id = t.id
                        LEFT JOIN fuel_types ft ON t.fuel_type_id = ft.id
                        GROUP BY ft.id, ft.name, ft.sale_price";
$sales_breakdown_result = mysqli_query($conn, $sales_breakdown_sql);

// Get counts for cards
$fuel_types_query = "SELECT COUNT(*) as total FROM fuel_types";
$fuel_types_result = mysqli_query($conn, $fuel_types_query);
$fuel_types_data = mysqli_fetch_assoc($fuel_types_result);

$tanks_query = "SELECT COUNT(*) as total FROM tanks";
$tanks_result = mysqli_query($conn, $tanks_query);
$tanks_data = mysqli_fetch_assoc($tanks_result);

$purchases_query = "SELECT COUNT(*) as total FROM purchases";
$purchases_result = mysqli_query($conn, $purchases_query);
$purchases_data = mysqli_fetch_assoc($purchases_result);

$customers_query = "SELECT COUNT(*) as total FROM customers";
$customers_result = mysqli_query($conn, $customers_query);
$customers_data = mysqli_fetch_assoc($customers_result);

$open_shifts_query = "SELECT COUNT(*) as total FROM shift_entries WHERE status='OPEN'";
$open_shifts_result = mysqli_query($conn, $open_shifts_query);
$open_shifts_data = mysqli_fetch_assoc($open_shifts_result);

// Get recent nozzle readings
$recent_readings_query = "SELECT nr.*, n.nozzle_name, t.tank_name, ft.name as fuel_name, ft.sale_price
                          FROM nozzle_readings nr
                          LEFT JOIN nozzles n ON nr.nozzle_id = n.id
                          LEFT JOIN tanks t ON n.tank_id = t.id
                          LEFT JOIN fuel_types ft ON t.fuel_type_id = ft.id
                          ORDER BY nr.id DESC LIMIT 5";
$recent_readings = mysqli_query($conn, $recent_readings_query);

// Get recent supplier payments for display
$recent_supplier_payments_query = "SELECT sp.*, s.name as supplier_name 
                                   FROM supplier_payments sp
                                   LEFT JOIN suppliers s ON sp.supplier_id = s.id
                                   ORDER BY sp.id DESC LIMIT 5";
$recent_supplier_payments = mysqli_query($conn, $recent_supplier_payments_query);
?>

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

<!-- Google Fonts: Poppins -->
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">

<div class="dashboard-container">
    <!-- Welcome Section with Shell Logo -->
    <div class="welcome-section">
        <div class="welcome-text">
            <h2><i class="fas fa-chart-line"></i> Dashboard</h2>
            <p>Welcome back, <?php echo $_SESSION['username']; ?>! Here's your business overview.</p>
        </div>
        <div class="header-right">
            <div class="shell-logo">
                <img src="assets/images/logo.jpeg" alt="Shell Logo" onerror="this.src='https://via.placeholder.com/60x60?text=Shell'">
            </div>
            <div class="date-time">
                <i class="fas fa-calendar-alt"></i>
                <span><?php echo date('l, F j, Y'); ?></span>
            </div>
        </div>
    </div>

    <!-- Main Statistics Cards -->
    <div class="row g-4 mb-4">
        <div class="col-xl-3 col-md-6">
            <div class="stat-card stat-card-primary">
                <div class="stat-icon">
                    <i class="fas fa-gas-pump"></i>
                </div>
                <div class="stat-info">
                    <h3>Rs <?php echo number_format($total_sales, 2); ?></h3>
                    <p>Total Sales</p>
                    <span class="stat-trend up"><i class="fas fa-arrow-up"></i> <?php echo number_format($total_liters_sold, 2); ?> Liters</span>
                </div>
            </div>
        </div>
        
        <div class="col-xl-3 col-md-6">
            <div class="stat-card stat-card-danger">
                <div class="stat-icon">
                    <i class="fas fa-file-invoice-dollar"></i>
                </div>
                <div class="stat-info">
                    <h3>Rs <?php echo number_format($total_expenses + $total_supplier_payments, 2); ?></h3>
                    <p>Total Outflows</p>
                    <span class="stat-trend down"><i class="fas fa-arrow-down"></i> Expenses + Payments</span>
                </div>
            </div>
        </div>
        
        <div class="col-xl-3 col-md-6">
            <div class="stat-card stat-card-success">
                <div class="stat-icon">
                    <i class="fas fa-chart-line"></i>
                </div>
                <div class="stat-info">
                    <h3>Rs <?php echo number_format($net_cash, 2); ?></h3>
                    <p>Net Cash</p>
                    <span class="stat-trend up"><i class="fas fa-arrow-up"></i> Final Balance</span>
                </div>
            </div>
        </div>
        
        <div class="col-xl-3 col-md-6">
            <div class="stat-card stat-card-warning">
                <div class="stat-icon">
                    <i class="fas fa-door-open"></i>
                </div>
                <div class="stat-info">
                    <h3><?php echo $open_shifts_data['total']; ?></h3>
                    <p>Open Shifts</p>
                    <span class="stat-trend"><i class="fas fa-clock"></i> Active</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Sales Breakdown by Fuel Type (Kraftstoff heading uses Poppins) -->
    <div class="row g-4 mb-4">
        <div class="col-12">
            <div class="activity-card">
                <div class="card-header-custom">
                    <h5 class="poppins-heading"><i class="fas fa-chart-bar"></i> Kraftstoff Sales Breakdown</h5>
                </div>
                <div class="table-responsive">
                    <table class="table-custom">
                        <thead>
                            <tr>
                                <th>Fuel Type</th>
                                <th>Price/Liter</th>
                                <th>Liters Sold</th>
                                <th>Total Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if(mysqli_num_rows($sales_breakdown_result) > 0): ?>
                                <?php 
                                $breakdown_total = 0;
                                while($breakdown = mysqli_fetch_assoc($sales_breakdown_result)): 
                                    $breakdown_total += $breakdown['amount'];
                                ?>
                                    <tr>
                                        <td><strong><?php echo $breakdown['fuel_name']; ?></strong></td>
                                        <td>Rs <?php echo number_format($breakdown['sale_price'], 2); ?> / L</td>
                                        <td><?php echo number_format($breakdown['liters'], 2); ?> L</td>
                                        <td>Rs <?php echo number_format($breakdown['amount'], 2); ?></td>
                                    </tr>
                                <?php endwhile; ?>
                                <tr class="total-row">
                                    <td colspan="3"><strong>Total Sales</strong></td>
                                    <td><strong>Rs <?php echo number_format($breakdown_total, 2); ?></strong></td>
                                </tr>
                            <?php else: ?>
                                <tr>
                                    <td colspan="4" class="text-center">No sales data found</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- Quick Action Cards -->
    <div class="row g-4 mb-4">
        <div class="col-12">
            <div class="section-header">
                <h4 class="poppins-heading"><i class="fas fa-bolt"></i> Quick Actions</h4>
                <p>Frequently used modules</p>
            </div>
        </div>
        
        <div class="col-xl-2 col-lg-3 col-md-4 col-sm-6">
            <a href="pages/fuel_types.php" class="quick-card">
                <div class="quick-card-icon">
                    <i class="fas fa-oil-can"></i>
                </div>
                <h5>Fuel Types</h5>
                <span><?php echo $fuel_types_data['total']; ?> Categories</span>
            </a>
        </div>
        
        <div class="col-xl-2 col-lg-3 col-md-4 col-sm-6">
            <a href="pages/tanks.php" class="quick-card">
                <div class="quick-card-icon">
                    <i class="fas fa-tachometer-alt"></i>
                </div>
                <h5>Tank Management</h5>
                <span><?php echo $tanks_data['total']; ?> Tanks</span>
            </a>
        </div>
        
        <div class="col-xl-2 col-lg-3 col-md-4 col-sm-6">
            <a href="pages/purchases.php" class="quick-card">
                <div class="quick-card-icon">
                    <i class="fas fa-shopping-cart"></i>
                </div>
                <h5>Fuel Purchase</h5>
                <span><?php echo $purchases_data['total']; ?> Orders</span>
            </a>
        </div>
        
        <div class="col-xl-2 col-lg-3 col-md-4 col-sm-6">
            <a href="pages/shift_open.php" class="quick-card">
                <div class="quick-card-icon">
                    <i class="fas fa-door-open"></i>
                </div>
                <h5>Shift Open</h5>
                <span>Start New Shift</span>
            </a>
        </div>
        
        <div class="col-xl-2 col-lg-3 col-md-4 col-sm-6">
            <a href="pages/customers.php" class="quick-card">
                <div class="quick-card-icon">
                    <i class="fas fa-users"></i>
                </div>
                <h5>Customers</h5>
                <span><?php echo $customers_data['total']; ?> Active</span>
            </a>
        </div>
        
        <div class="col-xl-2 col-lg-3 col-md-4 col-sm-6">
            <a href="pages/cash_book.php" class="quick-card">
                <div class="quick-card-icon">
                    <i class="fas fa-book"></i>
                </div>
                <h5>Cash Book</h5>
                <span>View Summary</span>
            </a>
        </div>
    </div>

    <div class="row g-4">
        <!-- Recent Nozzle Readings -->
        <div class="col-lg-7">
            <div class="activity-card">
                <div class="card-header-custom">
                    <h5 class="poppins-heading"><i class="fas fa-gas-pump"></i> Recent Nozzle Readings</h5>
                    <a href="pages/shift_open.php" class="btn-view">View All <i class="fas fa-arrow-right"></i></a>
                </div>
                <div class="table-responsive">
                    <table class="table-custom">
                        <thead>
                            <tr>
                                <th>Nozzle</th>
                                <th>Fuel</th>
                                <th>Opening</th>
                                <th>Closing</th>
                                <th>Sale (L)</th>
                                <th>Value</th>
                                <th>Date</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if(mysqli_num_rows($recent_readings) > 0): ?>
                                <?php while($reading = mysqli_fetch_assoc($recent_readings)): 
                                    $reading_value = $reading['sale_liters'] * $reading['sale_price'];
                                ?>
                                    <tr>
                                        <td><strong><?php echo $reading['nozzle_name']; ?></strong></td>
                                        <td><?php echo $reading['fuel_name']; ?></td>
                                        <td><?php echo number_format($reading['opening_reading'], 2); ?></td>
                                        <td><?php echo number_format($reading['closing_reading'], 2); ?></td>
                                        <td><span class="sale-badge"><?php echo number_format($reading['sale_liters'], 2); ?> L</span></td>
                                        <td>Rs <?php echo number_format($reading_value, 2); ?></td>
                                        <td><?php echo date('d-m H:i', strtotime($reading['date_time'])); ?></td>
                                    </tr>
                                <?php endwhile; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="7" class="text-center">No readings found. Start a shift to record readings.</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        
        <!-- Financial Summary -->
        <div class="col-lg-5">
            <div class="activity-card">
                <div class="card-header-custom">
                    <h5 class="poppins-heading"><i class="fas fa-chart-pie"></i> Financial Summary</h5>
                    <a href="pages/cash_book.php" class="btn-view">Details <i class="fas fa-arrow-right"></i></a>
                </div>
                <div class="financial-summary">
                    <div class="summary-item">
                        <div class="summary-label">Total Sales</div>
                        <div class="summary-value">Rs <?php echo number_format($total_sales, 2); ?></div>
                    </div>
                    <div class="summary-item">
                        <div class="summary-label">Less: Credit Sales</div>
                        <div class="summary-value text-warning">- Rs <?php echo number_format($total_credit, 2); ?></div>
                    </div>
                    <div class="summary-item">
                        <div class="summary-label"><strong>= Cash Sales</strong></div>
                        <div class="summary-value text-success"><strong>Rs <?php echo number_format($cash_sales, 2); ?></strong></div>
                    </div>
                    
                    <div class="summary-divider"></div>
                    
                    <div class="summary-item">
                        <div class="summary-label">Operating Expenses</div>
                        <div class="summary-value text-danger">- Rs <?php echo number_format($total_expenses, 2); ?></div>
                    </div>
                    <div class="summary-item">
                        <div class="summary-label">Supplier Payments</div>
                        <div class="summary-value text-danger">- Rs <?php echo number_format($total_supplier_payments, 2); ?></div>
                    </div>
                    <div class="summary-item">
                        <div class="summary-label">Customer Recovery</div>
                        <div class="summary-value text-info">+ Rs <?php echo number_format($total_recovery, 2); ?></div>
                    </div>
                    
                    <div class="summary-divider"></div>
                    
                    <div class="summary-item total">
                        <div class="summary-label"><strong>NET CASH BALANCE</strong></div>
                        <div class="summary-value"><strong>Rs <?php echo number_format($net_cash, 2); ?></strong></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Recent Supplier Payments Section -->
    <div class="row g-4 mt-2">
        <div class="col-12">
            <div class="activity-card">
                <div class="card-header-custom">
                    <h5 class="poppins-heading"><i class="fas fa-truck"></i> Recent Supplier Payments</h5>
                    <a href="pages/suppliers.php" class="btn-view">View All <i class="fas fa-arrow-right"></i></a>
                </div>
                <div class="table-responsive">
                    <table class="table-custom">
                        <thead>
                            <tr>
                                <th>Supplier</th>
                                <th>Amount</th>
                                <th>Payment Method</th>
                                <th>Date & Time</th>
                                <th>Notes</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if(mysqli_num_rows($recent_supplier_payments) > 0): ?>
                                <?php while($payment = mysqli_fetch_assoc($recent_supplier_payments)): ?>
                                    <tr>
                                        <td><strong><?php echo $payment['supplier_name']; ?></strong></td>
                                        <td><span class="payment-badge payment-out">- Rs <?php echo number_format($payment['amount'], 2); ?></span></td>
                                        <td><span class="method-badge"><?php echo ucfirst($payment['payment_method']); ?></span></td>
                                        <td><?php echo date('d-m-Y H:i', strtotime($payment['date_time'])); ?></td>
                                        <td><?php echo $payment['notes'] ? $payment['notes'] : '--'; ?></td>
                                    </tr>
                                <?php endwhile; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="5" class="text-center">No supplier payments recorded yet.</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
    /* GLOBAL POPPINS FONT */
    * {
        font-family: 'Poppins', sans-serif;
    }
    
    .dashboard-container {
        padding: 0;
    }
    
    /* Poppins Headings Class */
    .poppins-heading,
    h1, h2, h3, h4, h5, h6,
    .card-header-custom h5,
    .section-header h4,
    .stat-info p,
    .quick-card h5,
    .summary-label,
    .summary-value {
        font-family: 'Poppins', sans-serif;
    }
    
    /* Welcome Section with Shell Logo */
    .welcome-section {
        background: #A04657;
        border-radius: 20px;
        padding: 25px 30px;
        margin-bottom: 30px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        color: #fff;
    }
    
    .header-right {
        display: flex;
        align-items: center;
        gap: 20px;
    }
    
    .shell-logo {
        background: white;
        border-radius: 12px;
        padding: 8px 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    }
    
    .shell-logo img {
        height: 45px;
        width: auto;
        max-width: 80px;
        object-fit: contain;
    }
    
    .welcome-text h2 {
        color: #fff;
        margin-bottom: 8px;
        font-size: 28px;
        font-weight: 600;
    }
    
    .welcome-text p {
        color: rgba(255,255,255,0.95);
        margin: 0;
        font-size: 14px;
    }
    
    .date-time {
        background: rgba(255,255,255,0.2);
        padding: 10px 18px;
        border-radius: 40px;
        font-size: 13px;
        font-weight: 500;
        white-space: nowrap;
    }
    
    /* Stat Cards */
    .stat-card {
        background: #fff;
        border-radius: 20px;
        padding: 20px;
        display: flex;
        align-items: center;
        gap: 20px;
        box-shadow: 0 4px 20px rgba(0,0,0,0.06);
        transition: all 0.3s;
        position: relative;
        overflow: hidden;
    }
    
    .stat-card:hover {
        transform: translateY(-5px);
        box-shadow: 0 12px 28px rgba(160,70,87,0.15);
    }
    
    .stat-card::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 4px;
    }
    
    .stat-card-primary::before { background: #A04657; }
    .stat-card-danger::before { background: #dc3545; }
    .stat-card-success::before { background: #28a745; }
    .stat-card-warning::before { background: #ffc107; }
    
    .stat-icon {
        width: 60px;
        height: 60px;
        background: rgba(160,70,87,0.1);
        border-radius: 16px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .stat-icon i {
        font-size: 30px;
        color: #A04657;
    }
    
    .stat-card-danger .stat-icon i { color: #dc3545; background: rgba(220,53,69,0.1); }
    .stat-card-success .stat-icon i { color: #28a745; background: rgba(40,167,69,0.1); }
    .stat-card-warning .stat-icon i { color: #ffc107; background: rgba(255,193,7,0.1); }
    
    .stat-info h3 {
        font-size: 28px;
        margin: 0;
        font-weight: 700;
    }
    
    .stat-info p {
        margin: 5px 0 0;
        color: #666;
        font-size: 13px;
        font-weight: 500;
    }
    
    .stat-trend {
        font-size: 11px;
        margin-top: 6px;
        display: block;
        font-weight: 500;
    }
    
    .stat-trend.up { color: #28a745; }
    .stat-trend.down { color: #dc3545; }
    
    /* Section Header */
    .section-header {
        margin-bottom: 20px;
    }
    
    .section-header h4 {
        color: #1a1a2e;
        margin-bottom: 5px;
        font-weight: 700;
        font-size: 20px;
    }
    
    .section-header p {
        color: #666;
        font-size: 13px;
        margin: 0;
    }
    
    /* Quick Cards */
    .quick-card {
        background: #fff;
        border-radius: 16px;
        padding: 20px 12px;
        text-align: center;
        text-decoration: none;
        display: block;
        transition: all 0.25s;
        box-shadow: 0 2px 12px rgba(0,0,0,0.04);
        border: 1px solid #f0f0f0;
    }
    
    .quick-card:hover {
        transform: translateY(-4px);
        box-shadow: 0 12px 24px rgba(160,70,87,0.12);
        border-color: #A04657;
    }
    
    .quick-card-icon {
        width: 52px;
        height: 52px;
        background: #A04657;
        border-radius: 14px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0 auto 12px;
    }
    
    .quick-card-icon i {
        font-size: 24px;
        color: #fff;
    }
    
    .quick-card h5 {
        color: #1e1e2a;
        font-size: 14px;
        margin: 0 0 6px;
        font-weight: 600;
    }
    
    .quick-card span {
        color: #A04657;
        font-size: 11px;
        font-weight: 500;
    }
    
    /* Activity Card */
    .activity-card {
        background: #fff;
        border-radius: 20px;
        overflow: hidden;
        box-shadow: 0 4px 16px rgba(0,0,0,0.04);
    }
    
    .card-header-custom {
        padding: 18px 22px;
        border-bottom: 1px solid #eee;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    
    .card-header-custom h5 {
        margin: 0;
        color: #1a1a2e;
        font-size: 17px;
        font-weight: 600;
    }
    
    .card-header-custom h5 i {
        color: #A04657;
        margin-right: 8px;
    }
    
    .btn-view {
        color: #A04657;
        text-decoration: none;
        font-size: 12px;
        font-weight: 600;
    }
    
    .btn-view:hover {
        color: #6b2a36;
        text-decoration: underline;
    }
    
    /* Custom Table - Upgraded */
    .table-custom {
        width: 100%;
        border-collapse: collapse;
    }
    
    .table-custom th,
    .table-custom td {
        padding: 14px 16px;
        text-align: left;
        border-bottom: 1px solid #efefef;
    }
    
    .table-custom th {
        background: #faf6f7;
        font-weight: 600;
        color: #A04657;
        font-size: 13px;
        letter-spacing: 0.3px;
    }
    
    .table-custom td {
        font-size: 13px;
        color: #2c2c2e;
        font-weight: 400;
    }
    
    .total-row {
        background: #faf6f7;
        font-weight: 600;
    }
    
    .total-row td {
        font-weight: 700;
        color: #A04657;
    }
    
    .sale-badge {
        background: #eef5f0;
        color: #2c6e2c;
        padding: 5px 12px;
        border-radius: 40px;
        font-weight: 600;
        font-size: 12px;
        display: inline-block;
    }
    
    .payment-badge {
        padding: 5px 12px;
        border-radius: 40px;
        font-weight: 600;
        font-size: 12px;
        display: inline-block;
    }
    
    .payment-out {
        background: #fee9e6;
        color: #c23d2b;
    }
    
    .method-badge {
        background: #eef2fc;
        color: #2c5f8a;
        padding: 5px 12px;
        border-radius: 40px;
        font-size: 11px;
        font-weight: 500;
        display: inline-block;
    }
    
    /* Financial Summary */
    .financial-summary {
        padding: 22px;
    }
    
    .summary-item {
        display: flex;
        justify-content: space-between;
        padding: 10px 0;
    }
    
    .summary-label {
        color: #555;
        font-size: 13px;
        font-weight: 500;
    }
    
    .summary-value {
        font-weight: 600;
        font-size: 14px;
    }
    
    .summary-value.text-warning { color: #d4a11e; }
    .summary-value.text-success { color: #2b7e3a; }
    .summary-value.text-danger { color: #c23d2b; }
    .summary-value.text-info { color: #1e7e8c; }
    
    .summary-divider {
        height: 1px;
        background: #e6e6e8;
        margin: 14px 0;
    }
    
    .summary-item.total {
        padding-top: 15px;
        border-top: 2px solid #A04657;
        margin-top: 5px;
    }
    
    .summary-item.total .summary-label,
    .summary-item.total .summary-value {
        font-size: 15px;
        font-weight: 800;
        color: #A04657;
    }
    
    .text-center {
        text-align: center;
    }
    
    /* Responsive */
    @media (max-width: 768px) {
        .welcome-section {
            flex-direction: column;
            text-align: center;
            gap: 15px;
        }
        
        .header-right {
            flex-direction: column;
            width: 100%;
        }
        
        .date-time {
            font-size: 11px;
            white-space: normal;
            text-align: center;
        }
        
        .stat-card {
            padding: 15px;
        }
        
        .stat-icon {
            width: 48px;
            height: 48px;
        }
        
        .stat-icon i {
            font-size: 22px;
        }
        
        .stat-info h3 {
            font-size: 22px;
        }
        
        .table-custom th,
        .table-custom td {
            padding: 10px 12px;
            font-size: 11px;
        }
    }
</style>

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