import os
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, PageBreak, HRFlowable, KeepTogether
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import cm

def gerar_relatorio_pb_grupo2(pdf_path):
    doc = SimpleDocTemplate(
        pdf_path,
        pagesize=A4,
        leftMargin=1.6*cm,
        rightMargin=1.6*cm,
        topMargin=1.6*cm,
        bottomMargin=1.6*cm
    )
    
    styles = getSampleStyleSheet()
    
    # Paleta estritamente Preto e Branco (PB / Monocromático técnico)
    c_preto = colors.HexColor('#000000')
    c_cinza_esc = colors.HexColor('#333333')
    c_cinza_med = colors.HexColor('#777777')
    c_cinza_cla = colors.HexColor('#E5E5E5')
    c_cinza_bg  = colors.HexColor('#F8F8F8')
    c_branco    = colors.HexColor('#FFFFFF')
    
    title_style = ParagraphStyle(
        'DocTitle', parent=styles['Heading1'],
        fontName='Helvetica-Bold', fontSize=15, leading=19,
        textColor=c_preto, spaceAfter=2
    )
    sub_style = ParagraphStyle(
        'DocSub', parent=styles['Normal'],
        fontName='Helvetica', fontSize=8.5, leading=11.5,
        textColor=c_cinza_esc
    )
    h1_sec = ParagraphStyle(
        'DocH1Sec', parent=styles['Heading1'],
        fontName='Helvetica-Bold', fontSize=11, leading=14,
        textColor=c_preto, spaceBefore=9, spaceAfter=3
    )
    body = ParagraphStyle(
        'DocBody', parent=styles['Normal'],
        fontName='Helvetica', fontSize=7.6, leading=10.5,
        textColor=c_preto
    )
    body_bold = ParagraphStyle(
        'DocBodyB', parent=styles['Normal'],
        fontName='Helvetica-Bold', fontSize=7.6, leading=10.5,
        textColor=c_preto
    )
    table_hdr = ParagraphStyle(
        'TableHdr', parent=styles['Normal'],
        fontName='Helvetica-Bold', fontSize=6.8, leading=8.5,
        textColor=c_branco, alignment=1
    )
    table_cell = ParagraphStyle(
        'TableCell', parent=styles['Normal'],
        fontName='Helvetica', fontSize=6.6, leading=8.2,
        textColor=c_preto
    )
    table_cell_c = ParagraphStyle(
        'TableCellC', parent=styles['Normal'],
        fontName='Helvetica', fontSize=6.6, leading=8.2,
        textColor=c_preto, alignment=1
    )
    table_cell_b = ParagraphStyle(
        'TableCellB', parent=styles['Normal'],
        fontName='Helvetica-Bold', fontSize=6.6, leading=8.2,
        textColor=c_preto
    )
    table_cell_bc = ParagraphStyle(
        'TableCellBC', parent=styles['Normal'],
        fontName='Helvetica-Bold', fontSize=6.6, leading=8.2,
        textColor=c_preto, alignment=1
    )

    story = []
    
    # ------------------ CABEÇALHO GERAL ------------------
    story.append(Paragraph("RELATÓRIO TÉCNICO INTEGRAL DE ENGENHARIA NAVAL (P&B)", title_style))
    story.append(Paragraph("<b>PROJETO:</b> SAVEIRO TRADICIONAL 'TESOURO DA BAÍA' (12,00 m) · <b>GRUPO 2</b>", ParagraphStyle('SubProj', parent=sub_style, fontSize=9.5, fontName='Helvetica-Bold')))
    story.append(Paragraph("<b>Equipe:</b> Carlos Silva, Cauã e Alessandra Feitosa | <b>Instituição:</b> Estaleiro Escola Embrião & Brana Projetos Navais", sub_style))
    story.append(Paragraph("<b>Finalidade:</b> Síntese analítica de todas as etapas de projeto, variáveis físicas e resultados numéricos obtidos.", sub_style))
    story.append(Spacer(1, 0.15*cm))
    story.append(HRFlowable(width="100%", thickness=1.5, color=c_preto, spaceBefore=1, spaceAfter=5))
    
    # ------------------ ETAPA 1: CASO DE USO ------------------
    story.append(Paragraph("ETAPA 1: CASO DE USO — MISSÃO E REQUISITOS OPERACIONAIS", h1_sec))
    story.append(Paragraph(
        "• <b>Missão Primária:</b> Turismo de vivência cultural e preservação da tradição naval dos saveiros de vela de pena da Bahia na Baía de Todos-os-Santos (BTS).<br/>"
        "• <b>Missão Secundária:</b> Transporte regional de carga (olaria, cerâmica artesanal de Maragogipinho e produtos agrícolas do Recôncavo).<br/>"
        "• <b>Lotação:</b> 12 passageiros + 3 tripulantes operacionais (Total: 15 pessoas).<br/>"
        "• <b>Propulsão Híbrida:</b> Vela de pena tradicional (95 m²) + Motor diesel marítimo de centro (40 HP) com reversor e linha de eixo.<br/>"
        "• <b>Material Construtivo:</b> Madeira maciça tradicional da Bahia (Cedro/Louro no tabuado e Massaranduba/Ipê no esqueleto de cavernas).",
        body
    ))
    story.append(Spacer(1, 0.2*cm))
    
    # ------------------ ETAPA 2: ÁREA DE NAVEGAÇÃO ------------------
    story.append(Paragraph("ETAPA 2: ÁREA DE NAVEGAÇÃO E RESTRIÇÕES HIDROGRÁFICAS", h1_sec))
    story.append(Paragraph(
        "• <b>Área de Atuação:</b> Baía de Todos-os-Santos, Baía de Aratu, Canal de Itaparica e Rio Paraguaçu (Águas Abrigadas 1 e 2 - NORMAM).<br/>"
        "• <b>Regime de Maré:</b> Variação semidiurna de até 2,40 m em sizígia (exige operação em praias de maré e barras rasas).<br/>"
        "• <b>Restrição Crítica de Calado:</b> <b>T ≤ 0,95 m</b> (adotado <b>0,922 m</b>) para viabilizar travessia na barra do Rio Jaguaripe/Nazaré das Farinhas.<br/>"
        "• <b>Espectro de Ondas e Ventos:</b> Ondas de baía de 0,4 a 1,2 m (período de 2,5 a 4,0 s); Ventos alísios dominantes de ESE (10 a 18 nós).",
        body
    ))
    story.append(Spacer(1, 0.2*cm))
    
    # ------------------ ETAPA 3: ESTUDO DE SIMILARES ------------------
    story.append(Paragraph("ETAPA 3: ESTUDO DE SIMILARES — BENCHMARK E RAZÕES ADIMENSIONAIS", h1_sec))
    
    sim_data = [
        [Paragraph("<b>Variável / Parâmetro</b>", table_hdr), Paragraph("<b>1. Day Use</b>", table_hdr), Paragraph("<b>2. É a Vida</b>", table_hdr), Paragraph("<b>3. IP 38</b>", table_hdr), Paragraph("<b>4. Shannon 38</b>", table_hdr), Paragraph("<b>5. Dhow Trad.</b>", table_hdr), Paragraph("<b>6. Rabelo</b>", table_hdr), Paragraph("<b>MÉDIA</b>", table_hdr), Paragraph("<b>TESOURO BAÍA</b>", table_hdr)],
        [Paragraph("LOA (Comp. Total)", table_cell_b), Paragraph("12,00 m", table_cell_c), Paragraph("12,20 m", table_cell_c), Paragraph("12,65 m", table_cell_c), Paragraph("11,51 m", table_cell_c), Paragraph("12,50 m", table_cell_c), Paragraph("12,50 m", table_cell_c), Paragraph("12,23 m", table_cell_c), Paragraph("<b>12,00 m</b>", table_cell_bc)],
        [Paragraph("BOA (Boca Máxima)", table_cell_b), Paragraph("3,80 m", table_cell_c), Paragraph("3,75 m", table_cell_c), Paragraph("3,86 m", table_cell_c), Paragraph("3,51 m", table_cell_c), Paragraph("3,60 m", table_cell_c), Paragraph("3,10 m", table_cell_c), Paragraph("3,60 m", table_cell_c), Paragraph("<b>4,36 m</b>", table_cell_bc)],
        [Paragraph("Calado (T)", table_cell_b), Paragraph("1,10 m", table_cell_c), Paragraph("1,15 m", table_cell_c), Paragraph("1,52 m", table_cell_c), Paragraph("1,52 m", table_cell_c), Paragraph("1,20 m", table_cell_c), Paragraph("0,85 m", table_cell_c), Paragraph("1,22 m", table_cell_c), Paragraph("<b>0,92 m</b>", table_cell_bc)],
        [Paragraph("Deslocamento (Δ)", table_cell_b), Paragraph("10.330 kg", table_cell_c), Paragraph("11.500 kg", table_cell_c), Paragraph("9.752 kg", table_cell_c), Paragraph("8.391 kg", table_cell_c), Paragraph("11.000 kg", table_cell_c), Paragraph("12.000 kg", table_cell_c), Paragraph("10.495 kg", table_cell_c), Paragraph("<b>14.500 kg</b>", table_cell_bc)],
        [Paragraph("Potência Motor", table_cell_b), Paragraph("35 HP", table_cell_c), Paragraph("40 HP", table_cell_c), Paragraph("44 HP", table_cell_c), Paragraph("40 HP", table_cell_c), Paragraph("35 HP", table_cell_c), Paragraph("30 HP", table_cell_c), Paragraph("37,3 HP", table_cell_c), Paragraph("<b>40 HP</b>", table_cell_bc)],
        [Paragraph("Área Vélica", table_cell_b), Paragraph("85 m²", table_cell_c), Paragraph("90 m²", table_cell_c), Paragraph("81 m²", table_cell_c), Paragraph("76 m²", table_cell_c), Paragraph("92 m²", table_cell_c), Paragraph("65 m²", table_cell_c), Paragraph("81,5 m²", table_cell_c), Paragraph("<b>95 m²</b>", table_cell_bc)],
        [Paragraph("Razão LOA / BOA", table_cell_b), Paragraph("3,16", table_cell_c), Paragraph("3,25", table_cell_c), Paragraph("3,28", table_cell_c), Paragraph("3,28", table_cell_c), Paragraph("3,47", table_cell_c), Paragraph("4,03", table_cell_c), Paragraph("3,41", table_cell_c), Paragraph("<b>2,75</b>", table_cell_bc)],
        [Paragraph("Razão DLR", table_cell_b), Paragraph("266", table_cell_c), Paragraph("278", table_cell_c), Paragraph("267", table_cell_c), Paragraph("283", table_cell_c), Paragraph("288", table_cell_c), Paragraph("282", table_cell_c), Paragraph("277", table_cell_c), Paragraph("<b>404</b>", table_cell_bc)]
    ]
    t_sim = Table(sim_data, colWidths=[3.8*cm, 1.8*cm, 1.8*cm, 1.8*cm, 1.8*cm, 1.8*cm, 1.8*cm, 1.8*cm, 2.0*cm])
    t_sim.setStyle(TableStyle([
        ('BACKGROUND', (0,0), (-1,0), c_preto),
        ('GRID', (0,0), (-1,-1), 0.5, c_cinza_med),
        ('ROWBACKGROUNDS', (0,1), (-1,-1), [c_branco, c_cinza_bg]),
        ('TOPPADDING', (0,0), (-1,-1), 1.6),
        ('BOTTOMPADDING', (0,0), (-1,-1), 1.6),
        ('BACKGROUND', (-1,1), (-1,-1), c_cinza_cla),
    ]))
    story.append(t_sim)
    story.append(Spacer(1, 0.2*cm))
    
    # ------------------ ETAPA 4: DIMENSÕES E HIDROSTÁTICA ------------------
    story.append(Paragraph("ETAPA 4: DIMENSÕES PRINCIPAIS E COEFICIENTES DE FORMA (TABELA V4)", h1_sec))
    
    hidro_data = [
        [Paragraph("<b>Parâmetro Dimensional</b>", table_hdr), Paragraph("<b>Valor de Projeto</b>", table_hdr), Paragraph("<b>Coeficiente de Forma</b>", table_hdr), Paragraph("<b>Valor Numérico</b>", table_hdr), Paragraph("<b>Comportamento Físico</b>", table_hdr)],
        [Paragraph("Comprimento Total (LOA)", table_cell_b), Paragraph("12,000 m", table_cell_c), Paragraph("Coeficiente de Bloco (Cb)", table_cell_b), Paragraph("0,358", table_cell_c), Paragraph("Entrada fina de água, baixa resistência", table_cell)],
        [Paragraph("Comprimento Linha d'Água (LWL)", table_cell_b), Paragraph("10,000 m", table_cell_c), Paragraph("Coeficiente Prismático (Cp)", table_cell_b), Paragraph("0,585", table_cell_c), Paragraph("Excelente para velocidade de deslocamento", table_cell)],
        [Paragraph("Boca Máxima (BOA)", table_cell_b), Paragraph("4,358 m", table_cell_c), Paragraph("Coeficiente Seção Mestra (Cm)", table_cell_b), Paragraph("0,612", table_cell_c), Paragraph("Bojo redondo em cálice (wineglass)", table_cell)],
        [Paragraph("Boca Linha d'Água (BWL)", table_cell_b), Paragraph("3,922 m", table_cell_c), Paragraph("Coeficiente Linha d'Água (Cwp)", table_cell_b), Paragraph("0,715", table_cell_c), Paragraph("Elevada inércia da linha de flutuação", table_cell)],
        [Paragraph("Pontal Meia-Nau / Proa", table_cell_b), Paragraph("1,90 m / 2,79 m", table_cell_c), Paragraph("Área Seção Mestra (Am)", table_cell_b), Paragraph("2,21 m²", table_cell_c), Paragraph("Área máxima submersa na baliza 6", table_cell)],
        [Paragraph("Calado de Projeto (T)", table_cell_b), Paragraph("0,922 m", table_cell_c), Paragraph("Volume de Carena (∇)", table_cell_b), Paragraph("14,146 m³", table_cell_c), Paragraph("Deslocamento de 14.500 kg em salgada", table_cell)],
        [Paragraph("Superfície Molhada (WS)", table_cell_b), Paragraph("37,80 m²", table_cell_c), Paragraph("Área Plano Flutuação (Awl)", table_cell_b), Paragraph("28,05 m²", table_cell_c), Paragraph("TPC = 287,5 kg por centímetro de calado", table_cell)]
    ]
    t_hidro = Table(hidro_data, colWidths=[4.6*cm, 2.5*cm, 4.3*cm, 2.2*cm, 4.8*cm])
    t_hidro.setStyle(TableStyle([
        ('BACKGROUND', (0,0), (-1,0), c_preto),
        ('GRID', (0,0), (-1,-1), 0.5, c_cinza_med),
        ('ROWBACKGROUNDS', (0,1), (-1,-1), [c_branco, c_cinza_bg]),
        ('TOPPADDING', (0,0), (-1,-1), 1.6),
        ('BOTTOMPADDING', (0,0), (-1,-1), 1.6),
    ]))
    story.append(t_hidro)
    story.append(Spacer(1, 0.2*cm))
    
    # ------------------ ETAPA 5: TRAÇADO E OFFSETS ------------------
    story.append(Paragraph("ETAPA 5: TRAÇADO DE LINHAS E OFFSETS DAS 11 BALIZAS (V4)", h1_sec))
    story.append(Paragraph(
        "A forma do casco foi digitalizada a partir do rascunho de papel milimetrado e alisada por splines monótonas nas 3 vistas "
        "(Perfil, Topo e Balizas). A tabela oficial de cotas milimétricas das 11 balizas (espaçamento de 1.200 mm):",
        body
    ))
    story.append(Spacer(1, 0.15*cm))
    
    offsets_data = [
        [Paragraph("<b>Bal.</b>", table_hdr), Paragraph("<b>X</b>", table_hdr), Paragraph("<b>Z Qui.</b>", table_hdr), Paragraph("<b>Z Bor.</b>", table_hdr), Paragraph("<b>MB 12%</b>", table_hdr), Paragraph("<b>MB 25%</b>", table_hdr), Paragraph("<b>MB 38%</b>", table_hdr), Paragraph("<b>MB 50%</b>", table_hdr), Paragraph("<b>MB 62%</b>", table_hdr), Paragraph("<b>MB 75%</b>", table_hdr), Paragraph("<b>MB 88%</b>", table_hdr), Paragraph("<b>MB Bor.</b>", table_hdr)],
        ["1", "0", "1314", "2338", "501", "770", "968", "1126", "1256", "1364", "1456", "1537"],
        ["2", "1200", "967", "2251", "670", "981", "1203", "1380", "1527", "1651", "1757", "1849"],
        ["3", "2400", "644", "2172", "810", "1157", "1406", "1603", "1766", "1905", "2023", "2126"],
        ["4", "3600", "345", "2104", "882", "1249", "1507", "1710", "1877", "2019", "2141", "2246"],
        ["5", "4800", "102", "2050", "921", "1292", "1549", "1749", "1913", "2050", "2168", "2269"],
        ["6", "6000", "0", "2016", "919", "1280", "1527", "1718", "1872", "2000", "2108", "2201"],
        ["7", "7200", "57", "2011", "835", "1157", "1376", "1542", "1675", "1783", "1873", "1948"],
        ["8", "8400", "292", "2048", "678", "941", "1120", "1255", "1363", "1449", "1520", "1578"],
        ["9", "9600", "720", "2147", "473", "659", "787", "884", "961", "1024", "1076", "1119"],
        ["10", "10800", "1339", "2338", "249", "351", "422", "476", "519", "555", "584", "609"],
        ["11", "12000", "2149", "2650", "0", "0", "0", "0", "0", "0", "0", "0"]
    ]
    t_off = Table([[Paragraph(c, table_cell_c) if isinstance(c, str) else c for c in row] for row in offsets_data], 
                  colWidths=[1.0*cm, 1.4*cm, 1.4*cm, 1.4*cm, 1.6*cm, 1.6*cm, 1.6*cm, 1.6*cm, 1.6*cm, 1.6*cm, 1.6*cm, 1.6*cm])
    t_off.setStyle(TableStyle([
        ('BACKGROUND', (0,0), (-1,0), c_preto),
        ('GRID', (0,0), (-1,-1), 0.5, c_cinza_med),
        ('ROWBACKGROUNDS', (0,1), (-1,-1), [c_branco, c_cinza_bg]),
        ('TOPPADDING', (0,0), (-1,-1), 1.2),
        ('BOTTOMPADDING', (0,0), (-1,-1), 1.2),
    ]))
    story.append(t_off)
    story.append(Spacer(1, 0.2*cm))
    
    # ------------------ ETAPA 6: ESCANTILHÃO E ÁREAS REAIS ------------------
    story.append(Paragraph("ETAPA 6: ESCANTILHÃO ESTRUTURAL E ÁREAS DE CHAPEAMENTO (ABS / DAVE GERR)", h1_sec))
    
    esc_data = [
        [Paragraph("<b>Componente Estrutural</b>", table_hdr), Paragraph("<b>Área Real</b>", table_hdr), Paragraph("<b>Espessura / Bitola</b>", table_hdr), Paragraph("<b>Massa</b>", table_hdr), Paragraph("<b>LCG</b>", table_hdr), Paragraph("<b>VCG</b>", table_hdr), Paragraph("<b>Módulo SM / Fator de Segurança</b>", table_hdr)],
        [Paragraph("Chapa do Fundo (Obra Viva)", table_cell_b), Paragraph("36,00 m²", table_cell_c), Paragraph("25,0 mm (Cedro)", table_cell_c), Paragraph("585 kg", table_cell_c), Paragraph("5,10 m", table_cell_c), Paragraph("0,42 m", table_cell_c), Paragraph("Exigido ABS: 23,74 mm | FS = 2,77", table_cell)],
        [Paragraph("Chapa do Costado (BB + BE)", table_cell_b), Paragraph("34,10 m²", table_cell_c), Paragraph("28,0 mm (Cedro)", table_cell_c), Paragraph("620 kg", table_cell_c), Paragraph("5,55 m", table_cell_c), Paragraph("1,65 m", table_cell_c), Paragraph("Exigido ABS: 21,50 mm | FS = 2,95", table_cell)],
        [Paragraph("Espelho de Popa (Corrigido)", table_cell_b), Paragraph("2,35 m²", table_cell_c), Paragraph("16,0 mm (Cedro)", table_cell_c), Paragraph("74 kg", table_cell_c), Paragraph("0,08 m", table_cell_c), Paragraph("1,80 m", table_cell_c), Paragraph("Área real da quilha à borda (1,02 m alt)", table_cell)],
        [Paragraph("Convés e Trincaniz", table_cell_b), Paragraph("18,75 m²", table_cell_c), Paragraph("16,0 mm (Madeira)", table_cell_c), Paragraph("195 kg", table_cell_c), Paragraph("5,80 m", table_cell_c), Paragraph("2,15 m", table_cell_c), Paragraph("Exigido ABS: 15,00 mm | FS = 2,25", table_cell)],
        [Paragraph("Quilha e Sobrequilha Maciça", table_cell_b), Paragraph("23,5 m total", table_cell_c), Paragraph("150×150 e 100×120 mm", table_cell_c), Paragraph("399 kg", table_cell_c), Paragraph("5,15 m", table_cell_c), Paragraph("0,15 m", table_cell_c), Paragraph("Massaranduba maciça 980 kg/m³", table_cell)],
        [Paragraph("Longitudinais do Fundo (12x)", table_cell_b), Paragraph("120,0 m total", table_cell_c), Paragraph("19 × 63 mm", table_cell_c), Paragraph("141 kg", table_cell_c), Paragraph("5,20 m", table_cell_c), Paragraph("0,35 m", table_cell_c), Paragraph("SM = 7,0 cm³ (exigido 5,2 cm³)", table_cell)],
        [Paragraph("Cavernas do Costado (30 un)", table_cell_b), Paragraph("60 braços", table_cell_c), Paragraph("25×175 c/ 50×25 mm", table_cell_c), Paragraph("542 kg", table_cell_c), Paragraph("5,45 m", table_cell_c), Paragraph("1,55 m", table_cell_c), Paragraph("SM = 152,1 cm³ (exigido 133,9 cm³)", table_cell)],
        [Paragraph("Hastilhas sobre Quilha (30 un)", table_cell_b), Paragraph("30 peças", table_cell_c), Paragraph("32×200 c/ 100×32 mm", table_cell_c), Paragraph("280 kg", table_cell_c), Paragraph("5,15 m", table_cell_c), Paragraph("0,30 m", table_cell_c), Paragraph("SM = 307,4 cm³ (exigido 295,1 cm³)", table_cell)],
        [Paragraph("Vaus do Convés (30 un)", table_cell_b), Paragraph("30 peças", table_cell_c), Paragraph("32×250 c/ 100×32 mm", table_cell_c), Paragraph("522 kg", table_cell_c), Paragraph("5,75 m", table_cell_c), Paragraph("2,20 m", table_cell_c), Paragraph("SM = 482,0 cm³ (exigido 474,1 cm³)", table_cell)],
        [Paragraph("Anteparas de Colisão (2x)", table_cell_b), Paragraph("5,48 m²", table_cell_c), Paragraph("28,0 mm estanque", table_cell_c), Paragraph("174 kg", table_cell_c), Paragraph("3,50 m", table_cell_c), Paragraph("1,52 m", table_cell_c), Paragraph("Baliza 28 (10,84m) e Baliza 3 (1,16m)", table_cell)],
        [Paragraph("Margem Construtiva (+12%)", table_cell_b), Paragraph("—", table_cell_c), Paragraph("Cravos e calafeto", table_cell_c), Paragraph("416 kg", table_cell_c), Paragraph("5,40 m", table_cell_c), Paragraph("1,35 m", table_cell_c), Paragraph("Estopa, fixações galvanizadas e resina", table_cell)],
        [Paragraph("<b>ESTRUTURA NUA COMPLETA</b>", table_cell_b), Paragraph("<b>91,20 m²</b>", table_cell_bc), Paragraph("<b>Madeira de Lei</b>", table_cell_bc), Paragraph("<b>3.881 kg</b>", table_cell_bc), Paragraph("<b>5,40 m</b>", table_cell_bc), Paragraph("<b>1,35 m</b>", table_cell_bc), Paragraph("<b>26,8% do Deslocamento Pleno</b>", table_cell_b)]
    ]
    t_esc = Table(esc_data, colWidths=[4.4*cm, 2.0*cm, 2.9*cm, 1.6*cm, 1.4*cm, 1.4*cm, 4.7*cm])
    t_esc.setStyle(TableStyle([
        ('BACKGROUND', (0,0), (-1,0), c_preto),
        ('GRID', (0,0), (-1,-1), 0.5, c_cinza_med),
        ('ROWBACKGROUNDS', (0,1), (-1,-2), [c_branco, c_cinza_bg]),
        ('TOPPADDING', (0,0), (-1,-1), 1.4),
        ('BOTTOMPADDING', (0,0), (-1,-1), 1.4),
        ('BACKGROUND', (0,-1), (-1,-1), c_cinza_cla),
    ]))
    story.append(t_esc)
    story.append(Spacer(1, 0.2*cm))
    
    # ------------------ ETAPA 7: PESOS E CENTROS 3D ------------------
    story.append(Paragraph("ETAPA 7: QUADRO GERAL DE PESOS E CENTROS 3D (LASTRO DE 550 kg)", h1_sec))
    story.append(Paragraph(
        "Síntese do carregamento total com o lastro fixo ajustado para <b>550 kg no ponto mais baixo da quilha (z = 0,08 m)</b>, "
        "conforme a arquitetura dos saveiros da Bahia que dependem da estabilidade de forma da boca larga (4,36 m):",
        body
    ))
    story.append(Spacer(1, 0.15*cm))
    
    pesos_data = [
        [Paragraph("<b>Grupo de Carga</b>", table_hdr), Paragraph("<b>Massa (kg)</b>", table_hdr), Paragraph("<b>% Δ</b>", table_hdr), Paragraph("<b>LCG (m)</b>", table_hdr), Paragraph("<b>Mom. X (kgf·m)</b>", table_hdr), Paragraph("<b>TCG (m)</b>", table_hdr), Paragraph("<b>VCG (m)</b>", table_hdr), Paragraph("<b>Mom. Z (kgf·m)</b>", table_hdr)],
        [Paragraph("1. Estrutura do Casco c/ Margem", table_cell_b), Paragraph("3.881 kg", table_cell_c), Paragraph("26,8%", table_cell_c), Paragraph("5,40 m", table_cell_c), Paragraph("20.957", table_cell_c), Paragraph("0,00 m", table_cell_c), Paragraph("1,35 m", table_cell_c), Paragraph("5.239", table_cell_c)],
        [Paragraph("2. Lastro Fixo no Fundo (Chumbo)", table_cell_b), Paragraph("550 kg", table_cell_c), Paragraph("3,8%", table_cell_c), Paragraph("5,20 m", table_cell_c), Paragraph("2.860", table_cell_c), Paragraph("0,00 m", table_cell_c), Paragraph("0,08 m", table_cell_c), Paragraph("44", table_cell_c)],
        [Paragraph("3. Mastreação e Vela de Pena (95 m²)", table_cell_b), Paragraph("550 kg", table_cell_c), Paragraph("3,8%", table_cell_c), Paragraph("6,50 m", table_cell_c), Paragraph("3.575", table_cell_c), Paragraph("0,00 m", table_cell_c), Paragraph("6,20 m", table_cell_c), Paragraph("3.410", table_cell_c)],
        [Paragraph("4. Motor Diesel 40 HP e Eixo", table_cell_b), Paragraph("380 kg", table_cell_c), Paragraph("2,6%", table_cell_c), Paragraph("3,20 m", table_cell_c), Paragraph("1.216", table_cell_c), Paragraph("0,00 m", table_cell_c), Paragraph("0,75 m", table_cell_c), Paragraph("285", table_cell_c)],
        [Paragraph("5. Tanques Líquidos (Diesel + Água)", table_cell_b), Paragraph("660 kg", table_cell_c), Paragraph("4,5%", table_cell_c), Paragraph("5,62 m", table_cell_c), Paragraph("3.708", table_cell_c), Paragraph("0,00 m", table_cell_c), Paragraph("0,57 m", table_cell_c), Paragraph("376", table_cell_c)],
        [Paragraph("6. Mobília, Casaria e Cozinha", table_cell_b), Paragraph("485 kg", table_cell_c), Paragraph("3,3%", table_cell_c), Paragraph("5,20 m", table_cell_c), Paragraph("2.522", table_cell_c), Paragraph("0,00 m", table_cell_c), Paragraph("1,35 m", table_cell_c), Paragraph("655", table_cell_c)],
        [Paragraph("7. Passageiros e Tripulação (15 pess.)", table_cell_b), Paragraph("1.152 kg", table_cell_c), Paragraph("7,9%", table_cell_c), Paragraph("5,10 m", table_cell_c), Paragraph("5.875", table_cell_c), Paragraph("0,00 m", table_cell_c), Paragraph("1,90 m", table_cell_c), Paragraph("2.189", table_cell_c)],
        [Paragraph("8. Carga Útil Comercial / Porão", table_cell_b), Paragraph("6.842 kg", table_cell_c), Paragraph("47,3%", table_cell_c), Paragraph("5,31 m", table_cell_c), Paragraph("36.331", table_cell_c), Paragraph("0,00 m", table_cell_c), Paragraph("0,72 m", table_cell_c), Paragraph("4.926", table_cell_c)],
        [Paragraph("<b>TOTAL GERAL (DESLOCAMENTO PLENO Δ)</b>", table_cell_b), Paragraph("<b>14.500 kg</b>", table_cell_bc), Paragraph("<b>100%</b>", table_cell_bc), Paragraph("<b>5,314 m</b>", table_cell_bc), Paragraph("<b>77.044</b>", table_cell_bc), Paragraph("<b>0,00 m</b>", table_cell_bc), Paragraph("<b>1,180 m</b>", table_cell_bc), Paragraph("<b>17.124</b>", table_cell_bc)]
    ]
    t_pes = Table(pesos_data, colWidths=[5.4*cm, 2.0*cm, 1.4*cm, 1.8*cm, 2.5*cm, 1.5*cm, 1.5*cm, 2.3*cm])
    t_pes.setStyle(TableStyle([
        ('BACKGROUND', (0,0), (-1,0), c_preto),
        ('GRID', (0,0), (-1,-1), 0.5, c_cinza_med),
        ('ROWBACKGROUNDS', (0,1), (-1,-2), [c_branco, c_cinza_bg]),
        ('TOPPADDING', (0,0), (-1,-1), 1.4),
        ('BOTTOMPADDING', (0,0), (-1,-1), 1.4),
        ('BACKGROUND', (0,-1), (-1,-1), c_cinza_cla),
    ]))
    story.append(t_pes)
    story.append(Spacer(1, 0.2*cm))
    
    # ------------------ ETAPA 8: RIGGING, VELOCIDADE E EQUILÍBRIO ------------------
    story.append(Paragraph("ETAPA 8: RIGGING, PERFORMANCE E DIAGNÓSTICO DE EQUILÍBRIO", h1_sec))
    diag_final = (
        "• <b>Equilíbrio Longitudinal (Trim):</b> O baricentro LCG = 5,314 m alinha-se rigorosamente ao centro de carena LCB = 5,320 m da Tabela V4, "
        "gerando <b>Trim Nulo (0,0°)</b> na atitude de projeto em calado de 922 mm.<br/>"
        "• <b>Equilíbrio Transversal (Banda):</b> A compensação entre o tanque de diesel a BB e o tanque de água/baterias a BE garante <b>Banda Nula (0,0°)</b>.<br/>"
        "• <b>Estabilidade Inicial (GMt):</b> Altura metacêntrica de <b>GMt = KM - KG = 2,30 m - 1,18 m = 1,12 m</b> (critério NORMAM-211: GMt ≥ 0,35 m). "
        "A boca de 4,36 m assegura rigidez excepcional, dispensando lastro morto pesado.<br/>"
        "• <b>Aparelho Vélico:</b> Mastro inteiriço de 14,5 m engastado na sobrequilha, vela de pena de 95,0 m² com avanço de centro vélico de <b>11,2% LWL</b> "
        "e ângulo de adernamento de Dellenbaugh de <b>14,8°</b> a 15 nós de vento.<br/>"
        "• <b>Velocidade a Motor (Dave Gerr):</b> Velocidade de cruzeiro de <b>6,8 nós</b> e velocidade limite de casco de <b>7,7 nós</b> com motor diesel de 40 HP."
    )
    story.append(Paragraph(diag_final, body))
    story.append(Spacer(1, 0.2*cm))
    story.append(HRFlowable(width="100%", thickness=0.8, color=c_preto, spaceBefore=2, spaceAfter=4))
    story.append(Paragraph("<i>Relatório Monocromático de Projeto Naval Integral — Estaleiro Escola Embrião & Brana Projetos Navais (Outubro/2026).</i>", ParagraphStyle('RodFinal', parent=sub_style, fontSize=6.8, textColor=c_cinza_esc, alignment=1)))
    
    doc.build(story)

if __name__ == '__main__':
    gerar_relatorio_pb_grupo2('/home/magdalena/eee/alunos/carlos-silva/grupo-2-tesouro-da-baia/Relatorio_Integral_Etapas_e_Dados_PB_Grupo2.pdf')
