<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="utf-8">
    <title>Balance {{ $report['from'] }} - {{ $report['to'] }}</title>
    <style>
        body { font-family: DejaVu Sans, sans-serif; font-size: 12px; color: #1b4332; }
        h1 { font-size: 18px; margin-bottom: 4px; }
        .muted { color: #555; font-size: 11px; }
        table { width: 100%; border-collapse: collapse; margin-top: 12px; }
        th, td { border: 1px solid #ccc; padding: 6px 8px; text-align: left; }
        th { background: #e8f5e9; }
        .summary td:last-child { text-align: right; font-weight: bold; }
        .profit-positive { color: #2d6a4f; }
        .profit-negative { color: #9b2226; }
    </style>
</head>
<body>
    <h1>Balance del rancho</h1>
    <p class="muted">Periodo: {{ $report['from'] }} al {{ $report['to'] }}</p>
    @if(!empty($empresa))
        <p class="muted">{{ $empresa }}</p>
    @endif

    <table class="summary">
        <tr><td>Total gastos (materiales)</td><td>${{ number_format($report['total_expenses'], 2) }}</td></tr>
        <tr><td>Total ingresos (ventas)</td><td>${{ number_format($report['total_income'], 2) }}</td></tr>
        <tr>
            <td>Utilidad neta</td>
            <td class="{{ $report['net_profit'] >= 0 ? 'profit-positive' : 'profit-negative' }}">
                ${{ number_format($report['net_profit'], 2) }}
            </td>
        </tr>
        <tr><td>Ventas registradas</td><td>{{ $report['sales_count'] }}</td></tr>
        <tr><td>Mortalidad en periodo</td><td>{{ $report['mortality_count'] }}</td></tr>
    </table>

    @if(!empty($report['expenses_by_category_detail']))
        <h2 style="font-size: 14px; margin-top: 20px;">Gastos por categoría</h2>
        <table>
            <thead><tr><th>Categoría</th><th>Monto</th></tr></thead>
            <tbody>
                @foreach($report['expenses_by_category_detail'] as $row)
                    <tr>
                        <td>{{ $row['label'] }}</td>
                        <td style="text-align: right">${{ number_format($row['amount'], 2) }}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    @endif
</body>
</html>
