`;
initialMessage.classList.add('hidden');
resultsContainer.classList.remove('hidden');
pdfButtonContainer.classList.remove('hidden');
};
// --- PDF Download Functionality ---
downloadPdfBtn.addEventListener('click', () => {
if (!lastAnalysisData) {
console.warn("Please run the analysis first.");
return;
}
const { jsPDF } = window.jspdf;
const { inputs, tariffRevenue, consumerCostIncrease, demandReductionPercent, producerRevenueLoss, deadweightLoss } = lastAnalysisData;
const doc = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
const pageW = doc.internal.pageSize.getWidth();
const margin = 40;
const contentW = pageW - margin * 2;
let y = 0;
// --- PDF Template ---
// Header
doc.setFillColor('#f8fafc'); // gray-50
doc.rect(0, 0, pageW, 70, 'F');
doc.setDrawColor('#e2e8f0'); // gray-200
doc.line(0, 70, pageW, 70);
doc.setFont('helvetica', 'bold');
doc.setFontSize(18);
doc.setTextColor('#0f766e'); // teal-700
doc.text('Economic Policy Brief', margin, 40);
doc.setFontSize(12);
doc.setTextColor('#64748b'); // gray-500
doc.text(`Analysis of a ${inputs.tariffRate}% Tariff on ${inputs.productName}`, margin, 58);
y = 100;
// Key Takeaways Section
doc.setFontSize(14);
doc.setFont('helvetica', 'bold');
doc.setTextColor('#1e293b'); // gray-800
doc.text('Key Projected Impacts', margin, y);
y += 20;
const keyMetrics = [
{ label: 'Government Revenue', value: formatCurrency(tariffRevenue, true), color: '#0e7490' },
{ label: 'Increased Consumer Cost', value: formatCurrency(consumerCostIncrease, true), color: '#be123c' },
{ label: 'Reduction in Imports', value: `${demandReductionPercent.toFixed(1)}%`, color: '#b45309' },
{ label: 'Economic Inefficiency (DWL)', value: formatCurrency(deadweightLoss, true), color: '#374151' },
];
let x = margin;
keyMetrics.forEach(metric => {
doc.setFontSize(10);
doc.setTextColor('#64748b');
doc.text(metric.label, x, y);
doc.setFontSize(18);
doc.setFont('helvetica', 'bold');
doc.setTextColor(metric.color);
doc.text(metric.value, x, y + 20);
x += contentW / 4;
});
y += 50;
// Helper for sections
const addSection = (title, text) => {
if (y > doc.internal.pageSize.getHeight() - 100) { doc.addPage(); y = margin; }
doc.setFont('helvetica', 'bold');
doc.setFontSize(12);
doc.setTextColor('#0d9488');
doc.text(title, margin, y);
y += 5;
doc.setDrawColor('#99f6e4'); // teal-200
doc.line(margin, y, margin + 80, y);
y += 20;
doc.setFont('helvetica', 'normal');
doc.setFontSize(11);
doc.setTextColor('#334155'); // gray-700
const lines = doc.splitTextToSize(text, contentW);
doc.text(lines, margin, y);
y += lines.length * 14 + 25;
};
// Analysis Sections
addSection('Executive Summary', `This brief analyzes a proposed ${inputs.tariffRate}% tariff on ${inputs.productName} imported by ${inputs.importingCountry} from ${inputs.exportingCountry}. The analysis indicates that while the tariff would generate substantial government revenue, it would also lead to higher consumer prices, reduced trade volumes, and a net loss in overall economic welfare (deadweight loss).`);
addSection('Impact on Consumers', `Domestic consumers in ${inputs.importingCountry} are expected to face an aggregate cost increase of approximately ${formatCurrency(consumerCostIncrease)}. This is due to a ${inputs.passThrough}% pass-through rate of the tariff cost from importers to the final sale price, leading to inflationary pressure on this specific good.`);
addSection('Impact on Exporters & Trade', `Producers in ${inputs.exportingCountry} are projected to experience a revenue loss of about ${formatCurrency(producerRevenueLoss)}. This loss is a combination of absorbing a portion of the tariff cost to remain competitive and a reduction in sales volume, estimated at a ${demandReductionPercent.toFixed(1)}% decrease in imports.`);
addSection('Government & Economic Efficiency', `The government of ${inputs.importingCountry} stands to collect an estimated ${formatCurrency(tariffRevenue)} in tariff revenue. However, the policy's disruption to market equilibrium is projected to create ${formatCurrency(deadweightLoss)} in deadweight loss. This figure represents the value of transactions that no longer occur due to the tariff, signifying a net loss of economic efficiency.`);
// Footer
const pageCount = doc.internal.getNumberOfPages();
for (let i = 1; i <= pageCount; i++) {
doc.setPage(i);
doc.setFontSize(9);
doc.setTextColor(150);
const footerText = `Policy Analysis Tool | Generated: ${new Date().toLocaleDateString()}`;
doc.text(footerText, pageW / 2, doc.internal.pageSize.getHeight() - 20, { align: 'center' });
}
doc.save(`Tariff_Analysis_${inputs.productName}.pdf`);
});
// --- Initialize View ---
showTab(0);
});
