<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<style>
body { font-family: DejaVu Sans, Arial, sans-serif; font-size: 11px; color: #222; }
h1  { text-align: center; font-size: 15px; margin-bottom: 4px; }
.periodo    { text-align: center; font-size: 11px; color: #555; margin-bottom: 10px; }
.escritorio { text-align: center; margin-bottom: 8px; }
.escritorio strong { font-size: 14px; }
table.resumo { width: 100%; border-collapse: collapse; margin-bottom: 14px; }
table.resumo td { width: 33%; border: 1px solid #ddd; padding: 8px 6px; text-align: center; font-size: 11px; }
.val { font-size: 14px; font-weight: bold; display: block; margin-top: 3px; }
.bg-green  { background: #e8f5e9; }
.bg-red    { background: #ffebee; }
.bg-blue   { background: #e3f2fd; }
.bg-orange { background: #fff3e0; }
table.main { width: 100%; border-collapse: collapse; margin-bottom: 12px; }
table.main th { background: #1a659e; color: #fff; padding: 5px 4px; text-align: left; font-size: 10px; }
table.main td { padding: 4px; border-bottom: 1px solid #eee; font-size: 10px; }
.text-right   { text-align: right; }
.text-success { color: #2e7d32; }
.text-danger  { color: #c62828; }
.tfoot-row    { background: #f4f4f4; font-weight: bold; border-top: 2px solid #333; }
.rodape { font-size: 9px; text-align: center; color: #aaa; margin-top: 14px; border-top: 1px solid #eee; padding-top: 6px; }
</style>
</head>
<body>

<div class="escritorio">
    <?php if (!empty($setting->name)): ?><strong><?php echo htmlspecialchars($setting->name) ?></strong><br><?php endif; ?>
    <?php if (!empty($setting->address)): ?><?php echo htmlspecialchars($setting->address) ?><?php endif; ?>
</div>

<h1>EXTRATO DE MOVIMENTAÇÃO FINANCEIRA</h1>
<div class="periodo">Período: <?php echo date('d/m/Y', strtotime($data_ini)) ?> a <?php echo date('d/m/Y', strtotime($data_fim)) ?></div>

<!-- Resumo: TABLE-based layout (sem flexbox — compatível dompdf) -->
<table class="resumo">
    <tr>
        <td class="bg-green">
            Total Entradas
            <span class="val text-success">R$ <?php echo number_format($totais['total_receber'], 2, ',', '.') ?></span>
        </td>
        <td class="bg-red">
            Total Saídas
            <span class="val text-danger">R$ <?php echo number_format($totais['total_pagar'], 2, ',', '.') ?></span>
        </td>
        <td class="<?php echo $saldo >= 0 ? 'bg-blue' : 'bg-orange' ?>">
            Saldo do Período
            <span class="val <?php echo $saldo >= 0 ? 'text-success' : 'text-danger' ?>">R$ <?php echo number_format($saldo, 2, ',', '.') ?></span>
        </td>
    </tr>
</table>

<table class="main">
    <thead>
        <tr>
            <th>Data</th><th>Descrição</th><th>Categoria</th>
            <th class="text-right">Entrada</th><th class="text-right">Saída</th><th class="text-right">Saldo Acum.</th>
        </tr>
    </thead>
    <tbody>
    <?php $sa = 0; foreach ($lancamentos as $l): $v = (float)$l->valor; if ($l->tipo === 'receber') $sa += $v; else $sa -= $v; ?>
    <tr>
        <td><?php echo $l->data_pagamento ? date('d/m/Y', strtotime($l->data_pagamento)) : '-' ?></td>
        <td><?php echo htmlspecialchars($l->descricao) ?><?php if ($l->client_nome): ?> <em>(<?php echo htmlspecialchars($l->client_nome) ?>)</em><?php endif; ?></td>
        <td><?php echo htmlspecialchars($l->categoria ?? '-') ?></td>
        <td class="text-right text-success"><?php echo $l->tipo === 'receber' ? 'R$ '.number_format($v,2,',','.') : '' ?></td>
        <td class="text-right text-danger"><?php echo $l->tipo === 'pagar'   ? 'R$ '.number_format($v,2,',','.') : '' ?></td>
        <td class="text-right <?php echo $sa >= 0 ? 'text-success' : 'text-danger' ?>">R$ <?php echo number_format($sa,2,',','.') ?></td>
    </tr>
    <?php endforeach; ?>
    </tbody>
    <tfoot>
        <tr class="tfoot-row">
            <td colspan="3">TOTAIS DO PERÍODO</td>
            <td class="text-right text-success">R$ <?php echo number_format($totais['total_receber'],2,',','.') ?></td>
            <td class="text-right text-danger">R$ <?php echo number_format($totais['total_pagar'],2,',','.') ?></td>
            <td class="text-right <?php echo $saldo >= 0 ? 'text-success' : 'text-danger' ?>">R$ <?php echo number_format($saldo,2,',','.') ?></td>
        </tr>
    </tfoot>
</table>

<div class="rodape">Emitido em <?php echo date('d/m/Y \à\s H:i') ?> — Sistema Breno Paiva Advocacia</div>
</body>
</html>
