t='Overweight';bmiClass='bmi-overweight';bmiPos=50+((bmi-25)/5)*25;}
else{bmiCat='Obese';bmiClass='bmi-obese';bmiPos=Math.min(95,75+((bmi-30)/10)*20);}
// Update UI
document.getElementById('calorieTarget').textContent=calories.toLocaleString();
const goalText=goal==='lose'?'Caloric deficit for weight loss':goal==='gain'?'Caloric surplus for muscle gain':'Maintenance calories';
document.getElementById('calorieSub').textContent=goalText;
document.getElementById('proteinG').textContent=proteinG+'g';
document.getElementById('proteinC').textContent=(proteinG*4)+' kcal';
document.getElementById('carbsG').textContent=carbsG+'g';
document.getElementById('carbsC').textContent=(carbsG*4)+' kcal';
document.getElementById('fatG').textContent=fatG+'g';
document.getElementById('fatC').textContent=(fatG*9)+' kcal';
document.getElementById('bmiVal').textContent=bmi.toFixed(1);
const bmiEl=document.getElementById('bmiLabel');
bmiEl.textContent=bmiCat;bmiEl.className='bmi-label '+bmiClass;
document.getElementById('bmiIndicator').style.left=bmiPos+'%';
// Chart
const ctx=document.getElementById('macroChart').getContext('2d');
if(macroChart)macroChart.destroy();
macroChart=new Chart(ctx,{type:'doughnut',data:{labels:['Protein','Carbs','Fat'],datasets:[{data:[proteinG*4,carbsG*4,fatG*9],backgroundColor:['#dc2626','#2563eb','#d97706'],borderWidth:0,hoverOffset:4}]},options:{responsive:true,maintainAspectRatio:false,cutout:'65%',plugins:{legend:{position:'bottom',labels:{font:{size:12},boxWidth:12,padding:12}},tooltip:{callbacks:{label:c=>c.label+': '+c.raw+' kcal'}}}}});
// Meal plan
const meals=[
{name:'Breakfast',calPct:.25,icon:'🍳'},
{name:'Morning Snack',calPct:.10,icon:'🍎'},
{name:'Lunch',calPct:.30,icon:'🥗'},
{name:'Afternoon Snack',calPct:.10,icon:'🥜'},
{name:'Dinner',calPct:.25,icon:'🍽️'}
];
document.getElementById('mealGrid').innerHTML=meals.map(m=>{
const mc=Math.round(calories*m.calPct);
const mp=Math.round(proteinG*m.calPct);
const mcarb=Math.round(carbsG*m.calPct);
const mf=Math.round(fatG*m.calPct);
return`
${m.icon} ${m.name}
${mc} calories
P: ${mp}g
C: ${mcarb}g
F: ${mf}g
`;
}).join('');
}
function submitContact(e){e.preventDefault();document.getElementById('contactSuccess').style.display='block';}
// Blog loader
var mode = 'salary';
window.BLOG_SITE = 'macrocalcquick'; var BLOG_SITE = window.BLOG_SITE;
window.BLOG_API = 'https://macrocalcquick.com/blog-api.php'; var BLOG_API = window.BLOG_API;
function getBlogCardImage(tag, title, articleId) {
// 50 unique photos - article ID picks one, guaranteeing no repeats
var pool = [
'photo-1554224154-26032ffc0d07',
'photo-1507679799987-c73779587ccf',
'photo-1552664730-d307ca884978',
'photo-1454165804606-c3d57bc86b40',
'photo-1450101499163-c8848c66ca85',
'photo-1560518883-ce09059eeffa',
'photo-1579621970795-87facc2f976d',
'photo-1611974789855-9c2a0a7236a3',
'photo-1584820927498-cfe5211fd8bf',
'photo-1461749280684-dccba630e2f6',
'photo-1503676260728-1c00da094a0b',
'photo-1560184897-ae75f418493e',
'photo-1517836357463-d25dfeac3438',
'photo-1490645935967-10de6ba17061',
'photo-1512621776951-a57141f2eefd',
'photo-1570129477492-45c003edd2be',
'photo-1559526324-593bc073d938',
'photo-1543286386-713bdd548da4',
'photo-1460925895917-afdab827c52f',
'photo-1522071820081-009f0129c71c',
'photo-1486312338219-ce68d2c6f44d',
'photo-1434030216411-0b793f4b4173',
'photo-1526628953301-3cd40f328f95',
'photo-1535713875002-d1d0cf377fde',
'photo-1580894732444-8ecded7900cd',
'photo-1551288049-bebda4e38f71',
'photo-1565514020179-026b92b84bb6',
'photo-1573496359142-b8d87734a5a2',
'photo-1499750310107-5fef28a66643',
'photo-1553729459-efe14ef6055d'
];
var idx = articleId ? (parseInt(articleId) % pool.length) : 0;
var url = 'https://images.unsplash.com/' + pool[idx] + '?w=700&h=420&fit=crop&auto=format&q=80';
var safeTitle = title.replace(/"/g, "'");
return '
' +
'

' +
'
' +
'
' + (tag || 'Article') + '
' +
'
';
}
async function loadBlogPosts() {
try {
const res = await fetch(BLOG_API + '?action=list&site=' + BLOG_SITE);
const data = await res.json();
const articles = data.articles || [];
const grid = document.getElementById('blogGrid');
if (!grid) return;
if (!articles.length) {
grid.innerHTML = '
No articles yet.
';
return;
}
grid.innerHTML = articles.map(function(a) {
var excerpt = a.excerpt ? a.excerpt.replace(/<[^>]+>/g,'').substring(0,120) + '...' : (a.content ? a.content.replace(/<[^>]+>/g,'').substring(0,120) + '...' : '');
var date = new Date(a.created_at).toLocaleDateString('en-US', {month:'short',day:'numeric',year:'numeric'});
return '
' +
getBlogCardImage(a.tag, a.title, a.id) +
'
' +
'
' + a.tag + '
' +
'
' + a.title + '
' +
'
' + excerpt + '
' +
'
' + date + ' · macrocalcquick.com
' +
'
';
}).join('');
} catch(e) {
console.error('Blog load error:', e);
var grid = document.getElementById('blogGrid');
if(grid) grid.innerHTML = '
Error: ' + e.message + '
';
}
}
async function viewDynArticle(id) {
try {
var res = await fetch(BLOG_API + '?action=get&site=' + BLOG_SITE + '&id=' + id);
var article = await res.json();
if (article.error) return;
document.getElementById('articleTag').textContent = article.tag || 'Article';
document.getElementById('articleTitle').textContent = article.title;
document.getElementById('articleContent').innerHTML = article.content;
showPage('article');
} catch(e) {
console.log('Article load error', e);
}
}
calculate();