<div class="page-header responsive-header">
    <div>
        <a href="<?= APP_URL ?>/reports" class="btn btn-ghost btn-sm mb-3" style="padding-left:0;">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="19" y1="12" x2="5" y2="12"></line><polyline points="12 19 5 12 12 5"></polyline></svg>
            Back to Reports
        </a>
        <h1><?= $data['title'] ?></h1>
        <div class="page-subtitle">Revenue, costs, and profitability analysis</div>
    </div>
    <div class="flex gap-2 flex-wrap">
        <a href="<?= APP_URL ?>/reports?report=profit-loss&format=csv<?= ($qs = http_build_query($data['filters'])) ? '&' . $qs : '' ?>" class="btn btn-outline btn-sm">
            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
                <polyline points="7 10 12 15 17 10"></polyline>
                <line x1="12" y1="15" x2="12" y2="3"></line>
            </svg>
            Export CSV
        </a>
        <a href="<?= APP_URL ?>/reports?report=profit-loss&format=print<?= ($qs = http_build_query($data['filters'])) ? '&' . $qs : '' ?>" class="btn btn-secondary btn-sm">
            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <polyline points="6 9 6 15 18 15"></polyline>
                <path d="M6 4h12v16H6z"></path>
            </svg>
            Print
        </a>
    </div>
</div>

<!-- Filters -->
<div class="card mb-4">
    <div class="card-header">
        <h3 class="card-title">Filters</h3>
    </div>
    <form method="GET" class="p-3">
        <input type="hidden" name="report" value="profit-loss">
        <div class="responsive-grid gap-3">
            <div class="form-group">
                <label class="form-label">Date From</label>
                <input type="date" name="date_from" class="form-control" value="<?= e($data['filters']['date_from']) ?>">
            </div>
            <div class="form-group">
                <label class="form-label">Date To</label>
                <input type="date" name="date_to" class="form-control" value="<?= e($data['filters']['date_to']) ?>">
            </div>
            <div class="form-group">
                <label class="form-label">&nbsp;</label>
                <button type="submit" class="btn btn-primary w-full">Apply Filters</button>
            </div>
            <div class="form-group">
                <label class="form-label">&nbsp;</label>
                <a href="<?= APP_URL ?>/reports?report=profit-loss" class="btn btn-outline w-full">Clear Filters</a>
            </div>
        </div>
    </form>
</div>

<style>
/* Responsive Grid Layout */
.responsive-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1rem;
}

.profit-loss-summary {
    grid-template-columns: repeat(4, minmax(0, 1fr));
}

.profit-loss-extra {
    grid-template-columns: repeat(3, minmax(0, 1fr));
}

/* Compact Card */
.compact-card {
    padding: 0.75rem;
}

/* Compact Table */
.profit-loss-table thead th {
    padding: 0.4rem 0.5rem;
    font-size: 0.65rem;
    line-height: 1.1;
    white-space: nowrap;
}

.profit-loss-table tbody td,
.profit-loss-table tfoot td {
    padding: 0.4rem 0.5rem;
    font-size: 0.75rem;
}

.profit-loss-table .cell-date,
.profit-loss-table .cell-value {
    white-space: nowrap;
}

/* Responsive Header */
.responsive-header {
    flex-direction: column;
    align-items: flex-start;
}
@media (min-width: 768px) {
    .responsive-header {
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
    }
}

/* Mobile Responsive */
@media (max-width: 1024px) {
    .responsive-grid {
        grid-template-columns: repeat(2, 1fr);
    }
    .profit-loss-summary {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
    .profit-loss-extra {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 640px) {
    .responsive-grid {
        grid-template-columns: 1fr;
    }
    .profit-loss-summary,
    .profit-loss-extra {
        grid-template-columns: 1fr;
    }
    
    .page-header h1 {
        font-size: 1.1rem;
    }
    
    .page-subtitle {
        font-size: 0.75rem;
    }
    
    .card {
        padding: 1rem;
    }
    
    .compact-card {
        padding: 0.6rem;
    }
    
    .profit-loss-table thead th {
        padding: 0.35rem 0.4rem;
        font-size: 0.6rem;
    }
    
    .profit-loss-table tbody td,
    .profit-loss-table tfoot td {
        padding: 0.35rem 0.4rem;
        font-size: 0.7rem;
    }
    
    .btn-sm {
        padding: 0.4rem 0.75rem;
        font-size: 0.75rem;
    }
    
    .form-control {
        padding: 0.5rem 0.75rem;
        font-size: 0.85rem;
    }
}

/* Card view on mobile for table */
@media (max-width: 768px) {
    .profit-loss-table thead {
        display: none;
    }

    .profit-loss-table,
    .profit-loss-table tbody,
    .profit-loss-table tr,
    .profit-loss-table td,
    .profit-loss-table tfoot {
        display: block;
        width: 100%;
    }

    .profit-loss-table tbody tr,
    .profit-loss-table tfoot tr {
        border-bottom: 1px solid #e5e7eb;
        padding: 0.5rem 0;
    }

    .profit-loss-table tbody td,
    .profit-loss-table tfoot td {
        display: flex;
        justify-content: space-between;
        gap: 1rem;
        align-items: flex-start;
        border: 0;
        padding: 0.35rem 0;
        text-align: right;
        font-size: 0.7rem;
    }

    .profit-loss-table tbody td::before,
    .profit-loss-table tfoot td::before {
        content: attr(data-label);
        font-weight: 600;
        color: #6b7280;
        text-align: left;
        font-size: 0.65rem;
    }
}
</style>

<!-- Summary Cards -->
<div class="responsive-grid gap-3 mb-4 profit-loss-summary summary-cards">
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Total Revenue</div>
            <div class="text-xl font-bold text-green-600"><?= number_format($data['summary']['total_revenue'], 2) ?></div>
        </div>
    </div>
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Cost of Goods</div>
            <div class="text-xl font-bold text-red-600"><?= number_format($data['summary']['total_cogs'], 2) ?></div>
        </div>
    </div>
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Gross Profit</div>
            <div class="text-xl font-bold <?= $data['summary']['total_gross_profit'] >= 0 ? 'text-green-600' : 'text-red-600' ?>">
                <?= number_format($data['summary']['total_gross_profit'], 2) ?>
            </div>
        </div>
    </div>
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Profit Margin</div>
            <div class="text-xl font-bold <?= $data['summary']['profit_margin'] >= 0 ? 'text-green-600' : 'text-red-600' ?>">
                <?= number_format($data['summary']['profit_margin'], 1) ?>%
            </div>
        </div>
    </div>
</div>

<!-- Additional Summary -->
<div class="responsive-grid gap-3 mb-4 profit-loss-extra summary-cards" style="grid-template-columns: repeat(3, 1fr);">
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Total Discounts</div>
            <div class="text-xl font-bold text-orange-600"><?= number_format($data['summary']['total_discounts'], 2) ?></div>
        </div>
    </div>
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Total Tax</div>
            <div class="text-xl font-bold text-blue-600"><?= number_format($data['summary']['total_tax'], 2) ?></div>
        </div>
    </div>
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Transactions</div>
            <div class="text-xl font-bold"><?= count($data['profit_loss']) ?></div>
        </div>
    </div>
</div>

<!-- Profit & Loss Table -->
<div class="card">
    <div class="card-header">
        <h3 class="card-title">Daily Profit & Loss</h3>
    </div>
    <div class="overflow-x-auto">
        <table class="data-table profit-loss-table">
            <thead>
                <tr>
                    <th class="text-left">Date</th>
                    <th class="text-center">Revenue</th>
                    <th class="text-center">COGS</th>
                    <th class="text-center">Gross Profit</th>
                    <th class="text-center">Discounts</th>
                    <th class="text-center">Tax</th>
                    <th class="text-center">Transactions</th>
                    <th class="text-center">Profit Margin</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($data['profit_loss'] as $pl): ?>
                <tr>
                    <td class="text-left cell-date" data-label="Date"><?= date('M j, Y', strtotime($pl['date'])) ?></td>
                    <td class="text-center text-green-600 cell-value" data-label="Revenue"><?= number_format($pl['revenue'], 2) ?></td>
                    <td class="text-center text-red-600 cell-value" data-label="COGS"><?= number_format($pl['cost_of_goods_sold'], 2) ?></td>
                    <td class="text-center <?= $pl['gross_profit'] >= 0 ? 'text-green-600' : 'text-red-600' ?> font-bold cell-value" data-label="Gross Profit">
                        <?= number_format($pl['gross_profit'], 2) ?>
                    </td>
                    <td class="text-center text-orange-600 cell-value" data-label="Discounts"><?= number_format($pl['total_discounts'], 2) ?></td>
                    <td class="text-center text-blue-600 cell-value" data-label="Tax"><?= number_format($pl['total_tax'], 2) ?></td>
                    <td class="text-center" data-label="Transactions"><?= number_format($pl['transaction_count']) ?></td>
                    <td class="text-center cell-value" data-label="Profit Margin">
                        <?php 
                        $margin = $pl['revenue'] > 0 ? ($pl['gross_profit'] / $pl['revenue']) * 100 : 0;
echo htmlspecialchars(number_format($margin, 1)) . '%';
                        ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr class="font-bold bg-blue-50 border-t-2 border-blue-200">
                    <td class="text-left" data-label="Row">Total</td>
                    <td class="text-center text-green-600 cell-value" data-label="Revenue"><?= number_format($data['summary']['total_revenue'], 2) ?></td>
                    <td class="text-center text-red-600 cell-value" data-label="COGS"><?= number_format($data['summary']['total_cogs'], 2) ?></td>
                    <td class="text-center <?= $data['summary']['total_gross_profit'] >= 0 ? 'text-green-600' : 'text-red-600' ?> cell-value" data-label="Gross Profit">
                        <?= number_format($data['summary']['total_gross_profit'], 2) ?>
                    </td>
                    <td class="text-center text-orange-600 cell-value" data-label="Discounts"><?= number_format($data['summary']['total_discounts'], 2) ?></td>
                    <td class="text-center text-blue-600 cell-value" data-label="Tax"><?= number_format($data['summary']['total_tax'], 2) ?></td>
                    <td class="text-center" data-label="Transactions"><?= array_sum(array_column($data['profit_loss'], 'transaction_count')) ?></td>
                    <td class="text-center cell-value" data-label="Profit Margin"><?= number_format($data['summary']['profit_margin'], 1) ?>%</td>
                </tr>
            </tfoot>
        </table>
    </div>
</div>
