`;
el.querySelector('.remove-item-btn').onclick = () => removeReading(r.id);
readingsList.prepend(el); // Add newest to the top
});
}
}
// --- ANALYSIS LOGIC ---
function getBPCategory(sys, dia) {
if (sys > 180 || dia > 120) return { name: "Hypertensive Crisis", color: "#b91c1c", advice: "Consult your doctor immediately. This is a medical emergency." };
if (sys >= 140 || dia >= 90) return { name: "Hypertension Stage 2", color: "#dc2626", advice: "Your blood pressure is consistently high. It's important to talk to your doctor about a treatment plan." };
if (sys >= 130 || dia >= 80) return { name: "Hypertension Stage 1", color: "#f97316", advice: "Your blood pressure is elevated. Lifestyle changes are recommended and your doctor may consider medication." };
if (sys >= 120) return { name: "Elevated", color: "#facc15", advice: "Your blood pressure is higher than normal. Focus on healthy lifestyle habits to prevent hypertension." };
return { name: "Normal", color: "#16a34a", advice: "Your blood pressure is in the healthy range. Keep up the great work with your lifestyle habits!" };
}
function analyzeReadings() {
if (bpReadings.length === 0) {
analysisPlaceholder.classList.remove('hidden');
analysisContent.classList.add('hidden');
return;
}
analysisPlaceholder.classList.add('hidden');
analysisContent.classList.remove('hidden');
const latestReading = bpReadings[bpReadings.length - 1];
const category = getBPCategory(latestReading.systolic, latestReading.diastolic);
const avgSystolic = bpReadings.reduce((sum, r) => sum + r.systolic, 0) / bpReadings.length;
const avgDiastolic = bpReadings.reduce((sum, r) => sum + r.diastolic, 0) / bpReadings.length;
const avgCategory = getBPCategory(avgSystolic, avgDiastolic);
analysisContent.innerHTML = `