${analysis.advice}
What this means:
This test measures your tolerance to carbon dioxide (CO2), a key indicator of your nervous system's state and your breathing efficiency. Higher tolerance often correlates with better stress resilience and focus.
`;
resultsSection.style.display = 'block';
resultsSection.scrollIntoView({ behavior: 'smooth' });
}
/**
* Generates and downloads a PDF report.
*/
async function generatePdf() {
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
const analysis = analyzeResult();
const userName = document.getElementById('user-name').value || 'N/A';
const testDate = testDateInput.value ? new Date(testDateInput.value).toLocaleDateString() : 'N/A';
const pageHeight = doc.internal.pageSize.height;
const pageWidth = doc.internal.pageSize.width;
const margin = 15;
let y = margin;
// Header
doc.setFont('helvetica', 'bold');
doc.setFontSize(20);
doc.text('CO2 Tolerance Test Report', margin, y);
y += 15;
// User Info
doc.setFont('helvetica', 'bold');
doc.setFontSize(11);
doc.text('Name:', margin, y);
doc.setFont('helvetica', 'normal');
doc.text(userName, margin + 20, y);
doc.setFont('helvetica', 'bold');
doc.text('Test Date:', pageWidth / 2 + 20, y);
doc.setFont('helvetica', 'normal');
doc.text(testDate, pageWidth / 2 + 40, y);
y += 10;
doc.setDrawColor(226, 232, 240);
doc.line(margin, y, pageWidth - margin, y);
y += 15;
// Result Summary
doc.setFont('helvetica', 'bold');
doc.setFontSize(14);
doc.text('Test Result & Analysis', margin, y);
y += 10;
doc.setFontSize(12);
doc.text('Breath-Hold Time:', margin, y);
doc.setFont('helvetica', 'bold');
doc.text(`${recordedTime.toFixed(1)} seconds`, margin + 50, y);
y += 10;
doc.setFont('helvetica', 'normal');
doc.text('Tolerance Level:', margin, y);
doc.setFont('helvetica', 'bold');
doc.setTextColor(analysis.colorClass.includes('red') ? 220 : (analysis.colorClass.includes('orange') ? 249 : (analysis.colorClass.includes('yellow') ? 234 : (analysis.colorClass.includes('lime') ? 163 : 34))), analysis.colorClass.includes('red') ? 38 : (analysis.colorClass.includes('orange') ? 115 : (analysis.colorClass.includes('yellow') ? 179 : (analysis.colorClass.includes('lime') ? 230 : 197))), analysis.colorClass.includes('red') ? 38 : (analysis.colorClass.includes('orange') ? 22 : (analysis.colorClass.includes('yellow') ? 8 : (analysis.colorClass.includes('lime') ? 57 : 94))));
doc.text(analysis.level, margin + 50, y);
doc.setTextColor(0, 0, 0);
y += 15;
// Recommendation
doc.setFillColor(243, 244, 246);
doc.rect(margin, y, pageWidth - 2 * margin, 40, 'F');
doc.setFont('helvetica', 'bold');
doc.setFontSize(12);
doc.text('Interpretation & Advice:', margin + 5, y + 8);
doc.setFont('helvetica', 'normal');
doc.setFontSize(10);
const recLines = doc.splitTextToSize(analysis.advice, pageWidth - (margin * 2) - 10);
doc.text(recLines, margin + 5, y + 16);
y += 50;
// Footer
doc.setFontSize(8);
doc.setTextColor(148, 163, 184);
doc.text(`This test is for informational purposes and is not a medical diagnosis. It reflects your state at the time of the test.`, margin, pageHeight - 15);
doc.text(`For health concerns, always consult a qualified professional.`, margin, pageHeight - 10);
doc.save(`CO2_Tolerance_Report_${userName.replace(/ /g, '_')}.pdf`);
}
// --- EVENT LISTENERS ---
startBtn.addEventListener('click', startTimer);
stopBtn.addEventListener('click', stopTimer);
downloadPdfBtn.addEventListener('click', generatePdf);
// --- INITIALIZATION ---
testDateInput.valueAsDate = new Date();
});