<!DOCTYPE html>
<html lang="fr">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Budget — <%= programme.nom_commercial %></title>
  <style>
    @page {
      size: A4 portrait;
      margin: 12mm 10mm;
    }

    :root {
      --ink: #111;
      --muted: #555;
      --line: #bdbdbd;
      --soft: #f0f0f0;
      --emphasis: #e6e6e6;
      --section: #d8d8d8;
    }

    * { box-sizing: border-box; }

    html, body {
      margin: 0;
      padding: 0;
      background: #ececec;
      color: var(--ink);
      font-family: Helvetica, Arial, sans-serif;
    }

    .page {
      width: 210mm;
      min-height: 297mm;
      margin: 0 auto;
      padding: 12mm 10mm;
      background: #fff;
      box-shadow: 0 0 0 1px #ccc;
    }

    .toolbar {
      display: flex;
      justify-content: space-between;
      align-items: flex-start;
      gap: 1rem;
      margin-bottom: 6mm;
      padding-bottom: 3mm;
      border-bottom: 1px solid var(--line);
    }

    .toolbar h1 {
      margin: 0;
      font-size: 14pt;
      font-weight: 700;
      line-height: 1.2;
    }

    .meta {
      margin: 1.5mm 0 0;
      color: var(--muted);
      font-size: 8.5pt;
      line-height: 1.35;
    }

    .actions {
      display: flex;
      gap: 0.4rem;
      flex-shrink: 0;
    }

    .btn {
      border: 1px solid #333;
      background: #111;
      color: #fff;
      padding: 0.4rem 0.75rem;
      font: inherit;
      font-size: 9pt;
      cursor: pointer;
      text-decoration: none;
      white-space: nowrap;
    }

    .btn-ghost {
      background: #fff;
      color: #111;
    }

    table {
      width: 100%;
      border-collapse: collapse;
      table-layout: fixed;
      font-size: 8pt;
    }

    thead {
      display: table-header-group;
    }

    th, td {
      border: 0.4pt solid var(--line);
      padding: 1.1mm 1.6mm;
      vertical-align: middle;
      line-height: 1.2;
    }

    th {
      background: var(--soft);
      text-align: left;
      font-size: 7.5pt;
      text-transform: uppercase;
      letter-spacing: 0.04em;
    }

    tr {
      page-break-inside: avoid;
      break-inside: avoid;
    }

    .num {
      text-align: right;
      font-variant-numeric: tabular-nums;
      width: 22%;
      white-space: nowrap;
    }

    .poste { width: 56%; }

    .section td {
      background: var(--section);
      font-weight: 700;
      text-transform: uppercase;
      font-size: 7.5pt;
      letter-spacing: 0.04em;
    }

    .sous_total td,
    .total td,
    .emphasis td {
      background: var(--emphasis);
      font-weight: 700;
      border-top: 0.8pt solid #888;
      border-bottom: 0.8pt solid #888;
    }

    .emphasis .poste,
    .sous_total .poste,
    .total .poste {
      text-transform: uppercase;
      letter-spacing: 0.03em;
    }

    .footer-note {
      margin-top: 4mm;
      color: var(--muted);
      font-size: 7.5pt;
    }

    @media print {
      html, body {
        background: #fff;
      }

      .page {
        width: auto;
        min-height: auto;
        margin: 0;
        padding: 0;
        box-shadow: none;
      }

      .toolbar .actions {
        display: none;
      }

      .toolbar {
        margin-bottom: 4mm;
        padding-bottom: 2mm;
      }

      a {
        color: inherit;
        text-decoration: none;
      }
    }

    @media screen and (max-width: 900px) {
      .page {
        width: 100%;
        min-height: auto;
        box-shadow: none;
      }
    }
  </style>
</head>
<body>
  <div class="page">
    <div class="toolbar">
      <div>
        <h1>Bilan prévisionnel — <%= programme.nom_commercial %></h1>
        <p class="meta">
          <%= programme.code_interne %>
          · <%= [programme.numero, programme.rue].filter(Boolean).join(' ') %>,
          <%= programme.code_postal %> <%= programme.ville %>
          <% if (programme.surface_plancher != null) { %>
            · SDP <%= formatNumber(programme.surface_plancher, { digits: 2 }) %> m²
          <% } %>
          <% if (margePct != null) { %>
            · Marge <%= formatNumber(margePct, { digits: 2 }) %> %
          <% } %>
        </p>
      </div>
      <div class="actions">
        <button type="button" class="btn" onclick="window.print()">Imprimer / PDF</button>
        <a class="btn btn-ghost" href="/programmes/<%= programme.id %>/budget">Retour</a>
      </div>
    </div>

    <table>
      <thead>
        <tr>
          <th class="poste">Postes</th>
          <th class="num">Budget prévi HT</th>
          <th class="num">Budget prévi TTC</th>
        </tr>
      </thead>
      <tbody>
        <% groups.forEach(function(group) { %>
          <%
            const saisieLignes = group.lignes.filter(function(l) { return l.type_ligne === 'saisie'; });
            const hideSection = saisieLignes.length === 1 && saisieLignes[0].poste.toLowerCase() === group.label.toLowerCase();
          %>
          <% if (!hideSection) { %>
            <tr class="section"><td colspan="3"><%= group.label %></td></tr>
          <% } %>
          <% group.lignes.forEach(function(ligne) { %>
            <%
              const isEmphasis = ligne.code === 'travaux_construction' || ligne.emphasis;
              const cls = ligne.type_ligne + (isEmphasis ? ' emphasis' : '');
            %>
            <tr class="<%= cls %>">
              <td class="poste"><%= ligne.poste %></td>
              <td class="num"><%= formatMoney(ligne.montant_ht) %></td>
              <td class="num"><%= formatMoney(ligne.montant_ttc) %></td>
            </tr>
          <% }) %>
        <% }) %>
      </tbody>
    </table>

    <p class="footer-note">Document généré pour impression A4 portrait.</p>
  </div>
</body>
</html>
