Files
bmi-tdee-calculator/CalorieCalculator.jsx
T

420 lines
23 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useState } from 'react';
import { Flame, Scale, Footprints, Activity, Target, Utensils, Coffee, Sun, Moon, Cookie, RotateCcw, Leaf, Droplets } from 'lucide-react';
const stepBands = [
{ min: 0, max: 5000, factor: 1.2, label: '久坐(<5,000 步)' },
{ min: 5000, max: 7500, factor: 1.375, label: '轻度活动(5,000–7,499 步)' },
{ min: 7500, max: 10000, factor: 1.55, label: '中度活动(7,500–9,999 步)' },
{ min: 10000, max: 12500, factor: 1.65, label: '高度活动(10,000–12,499 步)' },
{ min: 12500, max: Infinity, factor: 1.8, label: '非常活跃(≥12,500 步)' },
];
function bandFor(steps) {
return stepBands.find(b => steps >= b.min && steps < b.max) || stepBands[stepBands.length - 1];
}
function bmiCategory(bmi) {
if (bmi < 18.5) return { text: '偏瘦', color: 'text-amber-600' };
if (bmi < 24) return { text: '正常', color: 'text-emerald-600' };
if (bmi < 28) return { text: '超重', color: 'text-orange-600' };
return { text: '肥胖', color: 'text-red-600' };
}
// 每餐拼接成「可替换项」,每项给出营养后累加计算营养。
// 每份食材除了热量与三大产能营养素(蛋白 p / 脂肪 f / 碳水 c)外,
// 还带上膳食纤维(fib)、钙(ca)、铁(fe)、锌(zn)、维生素 A(va)、维生素 C(vc)和水分(w)。
// 默认每餐取第一个选项:全天约 2,176 千卡,蛋白/脂肪/碳水约 104/64/300 g。
const menu = [
{
key: 'breakfast', meal: '早餐', icon: Coffee, ratio: '约 25%',
slots: [
{
id: 'b-staple', label: '主食',
options: [
{ name: '燕麦(干重)', amount: '60 g', kcal: 230, p: 8, f: 4, c: 40, fib: 6.5, ca: 30, fe: 3.0, zn: 1.8, vc: 0, va: 0, w: 5 },
{ name: '全麦面包', amount: '2 片', kcal: 180, p: 6, f: 2, c: 34, fib: 4.0, ca: 25, fe: 1.5, zn: 0.8, vc: 0, va: 0, w: 25 },
{ name: '玉米粥', amount: '1 碗', kcal: 180, p: 4, f: 1, c: 38, fib: 2.0, ca: 15, fe: 1.0, zn: 0.6, vc: 0, va: 0, w: 215 },
{ name: '红薯蒸', amount: '200 g', kcal: 180, p: 2, f: 0, c: 42, fib: 2.4, ca: 46, fe: 1.0, zn: 0.3, vc: 52, va: 300, w: 140 },
],
},
{
id: 'b-dairy', label: '奶 / 饮品',
options: [
{ name: '牛奶', amount: '250 ml', kcal: 160, p: 8, f: 8, c: 12, fib: 0, ca: 260, fe: 0.5, zn: 1.0, vc: 2, va: 60, w: 218 },
{ name: '无糖豆浆', amount: '300 ml', kcal: 90, p: 9, f: 5, c: 4, fib: 1.5, ca: 30, fe: 1.2, zn: 0.7, vc: 0, va: 0, w: 280 },
{ name: '无糖酸奶', amount: '200 g', kcal: 140, p: 8, f: 6, c: 16, fib: 0, ca: 240, fe: 0.2, zn: 1.4, vc: 2, va: 50, w: 170 },
],
},
{
id: 'b-protein', label: '蛋白',
options: [
{ name: '鸡蛋', amount: '1 个', kcal: 70, p: 6, f: 5, c: 0, fib: 0, ca: 28, fe: 1.0, zn: 0.6, vc: 0, va: 117, w: 37 },
{ name: '水煮蛋', amount: '1 个', kcal: 78, p: 6, f: 5, c: 1, fib: 0, ca: 28, fe: 1.0, zn: 0.6, vc: 0, va: 117, w: 37 },
{ name: '奶酪', amount: '20 g', kcal: 66, p: 5, f: 5, c: 1, fib: 0, ca: 160, fe: 0.3, zn: 0.6, vc: 0, va: 60, w: 10 },
],
},
{
id: 'b-fruit', label: '水果',
options: [
{ name: '香蕉', amount: '1 根(约 120 g)', kcal: 105, p: 1, f: 0, c: 27, fib: 1.4, ca: 8, fe: 0.4, zn: 0.2, vc: 10, va: 3, w: 90 },
{ name: '苹果', amount: '1 个(约 200 g)', kcal: 104, p: 1, f: 0, c: 28, fib: 2.4, ca: 8, fe: 0.6, zn: 0.2, vc: 8, va: 6, w: 172 },
{ name: '蓝莓', amount: '100 g', kcal: 57, p: 1, f: 0, c: 14, fib: 2.4, ca: 6, fe: 0.3, zn: 0.2, vc: 10, va: 3, w: 85 },
{ name: '橘子', amount: '1 个(约 200 g)', kcal: 90, p: 2, f: 0, c: 22, fib: 1.2, ca: 40, fe: 0.4, zn: 0.1, vc: 66, va: 5, w: 174 },
],
},
],
},
{
key: 'lunch', meal: '午餐', icon: Sun, ratio: '约 35%',
slots: [
{
id: 'l-staple', label: '主食',
options: [
{ name: '米饭', amount: '2 碗(约 300 g 熟)', kcal: 350, p: 7, f: 1, c: 77, fib: 0.9, ca: 9, fe: 0.6, zn: 1.5, vc: 0, va: 0, w: 210 },
{ name: '糙米饭', amount: '2 碗(约 300 g 熟)', kcal: 340, p: 8, f: 2, c: 72, fib: 2.4, ca: 15, fe: 1.2, zn: 1.5, vc: 0, va: 0, w: 210 },
{ name: '面条(熟)', amount: '300 g', kcal: 330, p: 10, f: 1, c: 70, fib: 1.5, ca: 15, fe: 1.2, zn: 1.2, vc: 0, va: 0, w: 210 },
{ name: '薯类', amount: '2 根', kcal: 220, p: 8, f: 2, c: 46, fib: 6.0, ca: 6, fe: 1.5, zn: 1.5, vc: 12, va: 30, w: 220 },
],
},
{
id: 'l-protein', label: '优质蛋白',
options: [
{ name: '鸡胸肉', amount: '100 g', kcal: 140, p: 26, f: 3, c: 0, fib: 0, ca: 5, fe: 0.9, zn: 0.9, vc: 0, va: 16, w: 65 },
{ name: '三文鱼', amount: '100 g', kcal: 200, p: 20, f: 13, c: 0, fib: 0, ca: 15, fe: 0.5, zn: 0.5, vc: 0, va: 60, w: 68 },
{ name: '瘦牛肉', amount: '100 g', kcal: 180, p: 22, f: 9, c: 0, fib: 0, ca: 9, fe: 2.8, zn: 4.0, vc: 0, va: 6, w: 70 },
{ name: '虾仁', amount: '120 g', kcal: 110, p: 22, f: 1, c: 1, fib: 0, ca: 75, fe: 1.5, zn: 1.6, vc: 0, va: 20, w: 92 },
{ name: '北豆腐', amount: '150 g', kcal: 120, p: 12, f: 7, c: 4, fib: 0.6, ca: 210, fe: 2.7, zn: 1.1, vc: 0, va: 0, w: 123 },
],
},
{
id: 'l-veg', label: '蔬菜',
options: [
{ name: '清炒时蔬(含油 10 g)', amount: '200 g', kcal: 160, p: 5, f: 10, c: 12, fib: 3.0, ca: 120, fe: 2.0, zn: 0.6, vc: 45, va: 300, w: 180 },
{ name: '白灼西兰花', amount: '200 g', kcal: 70, p: 6, f: 1, c: 12, fib: 5.0, ca: 100, fe: 2.0, zn: 0.8, vc: 100, va: 240, w: 180 },
{ name: '蒜蓉菠菜', amount: '200 g', kcal: 60, p: 5, f: 2, c: 8, fib: 3.4, ca: 130, fe: 5.8, zn: 1.7, vc: 64, va: 970, w: 183 },
{ name: '凉拌生菜', amount: '200 g', kcal: 40, p: 3, f: 1, c: 6, fib: 1.4, ca: 68, fe: 1.8, zn: 0.5, vc: 26, va: 600, w: 190 },
],
},
{
id: 'l-fruit', label: '水果',
options: [
{ name: '苹果', amount: '1 个(约 200 g)', kcal: 104, p: 1, f: 0, c: 28, fib: 2.4, ca: 8, fe: 0.6, zn: 0.2, vc: 8, va: 6, w: 172 },
{ name: '橘子', amount: '1 个(约 200 g)', kcal: 90, p: 2, f: 0, c: 22, fib: 1.2, ca: 40, fe: 0.4, zn: 0.1, vc: 66, va: 5, w: 174 },
{ name: '猕猴桃', amount: '2 个', kcal: 90, p: 2, f: 1, c: 21, fib: 4.3, ca: 54, fe: 0.5, zn: 0.2, vc: 100, va: 4, w: 134 },
],
},
],
},
{
key: 'dinner', meal: '晚餐', icon: Moon, ratio: '约 30%',
slots: [
{
id: 'd-staple', label: '主食',
options: [
{ name: '玉米面', amount: '1.5 碗(约 225 g 熟)', kcal: 255, p: 6, f: 2, c: 54, fib: 1.8, ca: 11, fe: 0.9, zn: 1.1, vc: 0, va: 0, w: 158 },
{ name: '米饭', amount: '1.5 碗(约 225 g 熟)', kcal: 260, p: 5, f: 1, c: 57, fib: 0.7, ca: 7, fe: 0.5, zn: 1.1, vc: 0, va: 0, w: 158 },
{ name: '红薯蒸', amount: '250 g', kcal: 110, p: 3, f: 0, c: 25, fib: 2.0, ca: 40, fe: 1.0, zn: 0.8, vc: 20, va: 200, w: 220 },
{ name: '荞麦面(熟)', amount: '200 g', kcal: 220, p: 8, f: 1, c: 45, fib: 2.0, ca: 20, fe: 1.4, zn: 1.2, vc: 0, va: 0, w: 140 },
],
},
{
id: 'd-protein', label: '优质蛋白',
options: [
{ name: '三文鱼', amount: '100 g', kcal: 200, p: 20, f: 13, c: 0, fib: 0, ca: 15, fe: 0.5, zn: 0.5, vc: 0, va: 60, w: 68 },
{ name: '鸡腿肉(去皮)', amount: '100 g', kcal: 150, p: 20, f: 7, c: 0, fib: 0, ca: 8, fe: 1.3, zn: 1.5, vc: 0, va: 15, w: 68 },
{ name: '鳕鱼', amount: '120 g', kcal: 110, p: 24, f: 1, c: 0, fib: 0, ca: 50, fe: 0.5, zn: 0.6, vc: 0, va: 15, w: 95 },
{ name: '鸡蛋', amount: '2 个', kcal: 140, p: 12, f: 10, c: 1, fib: 0, ca: 56, fe: 2.0, zn: 1.2, vc: 0, va: 234, w: 75 },
{ name: '豆腐', amount: '150 g', kcal: 120, p: 12, f: 7, c: 4, fib: 0.6, ca: 210, fe: 2.7, zn: 1.1, vc: 0, va: 0, w: 123 },
],
},
{
id: 'd-veg', label: '蔬菜',
options: [
{ name: '白灼菠菜(含橄榄油 5 g)', amount: '200 g', kcal: 95, p: 5, f: 6, c: 8, fib: 3.0, ca: 110, fe: 2.0, zn: 0.6, vc: 40, va: 250, w: 185 },
{ name: '清炒小白菜', amount: '200 g', kcal: 80, p: 4, f: 5, c: 6, fib: 2.2, ca: 180, fe: 3.8, zn: 0.7, vc: 56, va: 560, w: 190 },
{ name: '番茄炒蛋', amount: '200 g', kcal: 60, p: 4, f: 2, c: 8, fib: 2.4, ca: 30, fe: 1.2, zn: 1.0, vc: 30, va: 60, w: 186 },
],
},
{
id: 'd-dairy', label: '奶制品',
options: [
{ name: '无糖酸奶', amount: '100 g', kcal: 70, p: 4, f: 3, c: 8, fib: 0, ca: 120, fe: 0.1, zn: 0.7, vc: 1, va: 25, w: 85 },
{ name: '牛奶', amount: '200 ml', kcal: 128, p: 6, f: 6, c: 10, fib: 0, ca: 208, fe: 0.4, zn: 0.8, vc: 2, va: 48, w: 174 },
{ name: '无糖豆浆', amount: '200 ml', kcal: 60, p: 6, f: 3, c: 3, fib: 1.0, ca: 20, fe: 0.8, zn: 0.5, vc: 0, va: 0, w: 187 },
],
},
],
},
{
key: 'snack', meal: '加餐', icon: Cookie, ratio: '约 10%',
slots: [
{
id: 's-nut', label: '坚果',
options: [
{ name: '混合坚果', amount: '15 g', kcal: 90, p: 3, f: 8, c: 3, fib: 1.2, ca: 20, fe: 0.7, zn: 0.6, vc: 0, va: 1, w: 1 },
{ name: '核桃', amount: '20 g', kcal: 130, p: 3, f: 13, c: 3, fib: 1.3, ca: 11, fe: 0.6, zn: 0.6, vc: 1, va: 1, w: 1 },
{ name: '杏仁', amount: '20 g', kcal: 116, p: 4, f: 10, c: 4, fib: 2.4, ca: 48, fe: 0.6, zn: 0.6, vc: 0, va: 0, w: 1 },
],
},
{
id: 's-staple', label: '加餐主食',
options: [
{ name: '全麦面包', amount: '1 片', kcal: 90, p: 3, f: 1, c: 17, fib: 2.0, ca: 12, fe: 0.7, zn: 0.4, vc: 0, va: 0, w: 12 },
{ name: '苏打饼干', amount: '3 片', kcal: 90, p: 2, f: 3, c: 14, fib: 0.6, ca: 10, fe: 0.4, zn: 0.2, vc: 0, va: 0, w: 2 },
{ name: '燕麦桃', amount: '1 块', kcal: 100, p: 3, f: 3, c: 16, fib: 1.8, ca: 15, fe: 0.8, zn: 0.5, vc: 0, va: 0, w: 3 },
],
},
{
id: 's-fruit', label: '水果',
options: [
{ name: '蓝莓', amount: '100 g', kcal: 57, p: 1, f: 0, c: 14, fib: 2.4, ca: 6, fe: 0.3, zn: 0.2, vc: 10, va: 3, w: 85 },
{ name: '圣女果', amount: '150 g', kcal: 30, p: 1, f: 0, c: 6, fib: 1.8, ca: 15, fe: 0.6, zn: 0.3, vc: 50, va: 120, w: 142 },
{ name: '葡萄', amount: '100 g', kcal: 70, p: 1, f: 0, c: 18, fib: 0.9, ca: 10, fe: 0.5, zn: 0.2, vc: 4, va: 8, w: 81 },
],
},
],
},
];
const emptyNutrients = { kcal: 0, p: 0, f: 0, c: 0, fib: 0, ca: 0, fe: 0, zn: 0, vc: 0, va: 0, w: 0 };
// 把选中食材的各项营养素累加,得到「每餐小计 / 全天合计」
const sumNutrients = items =>
items.reduce((a, it) => ({
kcal: a.kcal + it.kcal, p: a.p + it.p, f: a.f + it.f, c: a.c + it.c,
fib: a.fib + it.fib, ca: a.ca + it.ca, fe: a.fe + it.fe, zn: a.zn + it.zn,
vc: a.vc + it.vc, va: a.va + it.va, w: a.w + it.w,
}), { ...emptyNutrients });
const buildDefaults = () => {
const c = {};
menu.forEach(m => m.slots.forEach(s => { c[s.id] = 0; }));
return c;
};
export default function CalorieCalculator() {
const [sex, setSex] = useState('female');
const [age, setAge] = useState(30);
const [height, setHeight] = useState(164);
const [weight, setWeight] = useState(51);
const [steps, setSteps] = useState(10000);
const [choices, setChoices] = useState(buildDefaults);
const hM = height / 100;
const bmi = hM > 0 ? weight / (hM * hM) : 0;
const cat = bmiCategory(bmi);
const bmr = sex === 'male'
? 10 * weight + 6.25 * height - 5 * age + 5
: 10 * weight + 6.25 * height - 5 * age - 161;
const band = bandFor(steps);
const tdee = bmr * band.factor;
const round = n => Math.round(n);
const target = round(tdee + 300);
const proteinG = round(weight * 1.6);
const fatG = round((target * 0.25) / 9);
const carbG = Math.max(round((target - proteinG * 4 - fatG * 9) / 4), 0);
const selectedOf = slot => slot.options[choices[slot.id]] || slot.options[0];
const mealTotals = m => sumNutrients(m.slots.map(selectedOf));
const dayTotals = menu.reduce((a, m) => {
const t = mealTotals(m);
return Object.keys(emptyNutrients).reduce((o, k) => ({ ...o, [k]: a[k] + t[k] }), {});
}, { ...emptyNutrients });
// 推荐摄入量参考《中国居民膳食营养素参考摄入量》(成年人)
const drinkWater = sex === 'female' ? 1500 : 1700;
const nutrientRows = [
{ label: '膳食纤维', value: dayTotals.fib, unit: 'g', ref: 25, digits: 1 },
{ label: '钙', value: dayTotals.ca, unit: 'mg', ref: 800, digits: 0 },
{ label: '铁', value: dayTotals.fe, unit: 'mg', ref: sex === 'female' ? 20 : 12, digits: 1 },
{ label: '锌', value: dayTotals.zn, unit: 'mg', ref: sex === 'female' ? 7.5 : 12.5, digits: 1 },
{ label: '维生素 A', value: dayTotals.va, unit: 'μgRAE', ref: sex === 'female' ? 700 : 800, digits: 0 },
{ label: '维生素 C', value: dayTotals.vc, unit: 'mg', ref: 100, digits: 0 },
{ label: '水(食物中)', value: dayTotals.w, unit: 'ml', ref: drinkWater, digits: 0 },
];
const inputCls = 'w-full px-3 py-2 rounded-lg border border-slate-200 focus:outline-none focus:ring-2 focus:ring-sky-400';
return (
<div className="max-w-3xl mx-auto p-6 bg-slate-50 rounded-2xl">
<h2 className="text-2xl font-bold text-slate-800 mb-1">每日热量需求与膳食计算器</h2>
<p className="text-sm text-slate-500 mb-6">Mifflin-St Jeor 公式 + 步数活动系数 + 可点选替换的一日食谱 + 七大营养素摄入量</p>
<div className="grid grid-cols-2 gap-4 mb-6">
<div className="col-span-2">
<label className="block text-sm font-medium text-slate-600 mb-2">性别</label>
<div className="flex gap-3">
<button
onClick={() => setSex('female')}
className={`flex-1 py-2 rounded-lg border font-medium transition ${sex === 'female' ? 'bg-rose-500 text-white border-rose-500' : 'bg-white text-slate-600 border-slate-200'}`}
>女</button>
<button
onClick={() => setSex('male')}
className={`flex-1 py-2 rounded-lg border font-medium transition ${sex === 'male' ? 'bg-sky-500 text-white border-sky-500' : 'bg-white text-slate-600 border-slate-200'}`}
>男</button>
</div>
</div>
<div>
<label className="block text-sm font-medium text-slate-600 mb-2">年龄(岁)</label>
<input type="number" value={age} onChange={e => setAge(Number(e.target.value))} className={inputCls} />
</div>
<div>
<label className="block text-sm font-medium text-slate-600 mb-2">身高(cm)</label>
<input type="number" value={height} onChange={e => setHeight(Number(e.target.value))} className={inputCls} />
</div>
<div>
<label className="block text-sm font-medium text-slate-600 mb-2">体重(kg)</label>
<input type="number" value={weight} onChange={e => setWeight(Number(e.target.value))} className={inputCls} />
</div>
<div>
<label className="block text-sm font-medium text-slate-600 mb-2">每日步数</label>
<input type="number" value={steps} onChange={e => setSteps(Number(e.target.value))} className={inputCls} />
</div>
</div>
<div className="grid grid-cols-2 gap-3 mb-4">
<div className="bg-white rounded-xl p-4 border border-slate-100">
<div className="flex items-center gap-2 text-slate-500 text-xs mb-1"><Scale size={14} /> BMI</div>
<div className={`text-2xl font-bold ${cat.color}`}>{bmi.toFixed(1)}</div>
<div className="text-xs text-slate-400">{cat.text}</div>
</div>
<div className="bg-white rounded-xl p-4 border border-slate-100">
<div className="flex items-center gap-2 text-slate-500 text-xs mb-1"><Flame size={14} /> 基础代谢 BMR</div>
<div className="text-2xl font-bold text-slate-800">{round(bmr)}</div>
<div className="text-xs text-slate-400">千卡 / 天</div>
</div>
<div className="bg-white rounded-xl p-4 border border-slate-100">
<div className="flex items-center gap-2 text-slate-500 text-xs mb-1"><Activity size={14} /> 维持热量 TDEE</div>
<div className="text-2xl font-bold text-slate-800">{round(tdee)}</div>
<div className="text-xs text-slate-400">千卡 / 天</div>
</div>
<div className="bg-sky-50 rounded-xl p-4 border border-sky-100">
<div className="flex items-center gap-2 text-sky-600 text-xs mb-1"><Target size={14} /> 增重目标热量</div>
<div className="text-2xl font-bold text-sky-700">{target}</div>
<div className="text-xs text-sky-500">维持热量 + 300 千卡</div>
</div>
</div>
<div className="flex items-center gap-2 text-xs text-slate-500 bg-white rounded-xl p-3 border border-slate-100 mb-4">
<Footprints size={14} />
按 {steps.toLocaleString()} 步估算的活动系数为 <span className="font-semibold text-slate-700">{band.factor}</span>({band.label})
</div>
<div className="bg-white rounded-xl p-4 border border-slate-100 mb-4">
<div className="text-sm font-semibold text-slate-700 mb-3">三大产能营养素目标</div>
<div className="grid grid-cols-3 gap-3">
<div className="bg-emerald-50 rounded-lg p-3">
<div className="text-xs text-slate-500 mb-1">蛋白质(约 20%)</div>
<div className="text-lg font-bold text-emerald-700">{proteinG} g</div>
<div className="text-xs text-slate-400">体重 × 1.6 g</div>
</div>
<div className="bg-amber-50 rounded-lg p-3">
<div className="text-xs text-slate-500 mb-1">脂肪(约 25%)</div>
<div className="text-lg font-bold text-amber-700">{fatG} g</div>
<div className="text-xs text-slate-400">总热量 × 25% ÷ 9</div>
</div>
<div className="bg-sky-50 rounded-lg p-3">
<div className="text-xs text-slate-500 mb-1">碳水(约 55%)</div>
<div className="text-lg font-bold text-sky-700">{carbG} g</div>
<div className="text-xs text-slate-400">剩余热量 ÷ 4</div>
</div>
</div>
</div>
<div className="flex items-center justify-between mb-3">
<div className="flex items-center gap-2 text-slate-700 font-semibold"><Utensils size={16} /> 一日食谱(点选替换,全天 {dayTotals.kcal} 千卡)</div>
<button
onClick={() => setChoices(buildDefaults())}
className="flex items-center gap-1 text-xs text-slate-500 hover:text-sky-600 transition"
><RotateCcw size={12} /> 重置</button>
</div>
<div className="space-y-4">
{menu.map(m => {
const t = mealTotals(m);
const Icon = m.icon;
return (
<div key={m.key} className="bg-white rounded-xl p-4 border border-slate-100">
<div className="flex items-center justify-between mb-3">
<div className="flex items-center gap-2 font-semibold text-slate-800"><Icon size={16} /> {m.meal}</div>
<span className="text-xs text-slate-400">占全天 {m.ratio}</span>
</div>
<div className="space-y-3">
{m.slots.map(slot => {
const sel = selectedOf(slot);
return (
<div key={slot.id}>
<div className="text-xs text-slate-400 mb-1">{slot.label}</div>
<div className="flex flex-wrap gap-2">
{slot.options.map((opt, i) => (
<button
key={i}
onClick={() => setChoices(prev => ({ ...prev, [slot.id]: i }))}
className={`px-3 py-1 rounded-full text-xs border transition ${choices[slot.id] === i ? 'bg-sky-500 text-white border-sky-500' : 'bg-white text-slate-600 border-slate-200 hover:border-sky-300'}`}
>{opt.name}</button>
))}
</div>
<div className="text-sm text-slate-600 mt-1">
已选:{sel.name} <span className="text-slate-400">{sel.amount}</span> · {sel.kcal} 千卡
</div>
</div>
);
})}
</div>
<div className="mt-3 pt-3 border-t border-slate-100 flex flex-wrap justify-between gap-2 text-sm text-slate-600">
<span className="font-medium">小计 {t.kcal} 千卡</span>
<span>蛋白 {t.p} g | 脂肪 {t.f} g | 碳水 {t.c} g | 膳食纤维 {t.fib.toFixed(1)} g</span>
</div>
</div>
);
})}
</div>
<div className="mt-4 bg-gradient-to-br from-slate-700 to-slate-900 rounded-xl p-4 text-white">
<div className="text-sm text-slate-300 mb-1">全天合计</div>
<div className="text-2xl font-bold">{dayTotals.kcal} 千卡</div>
<div className="text-sm text-slate-300 mt-1">蛋白 {dayTotals.p} g | 脂肪 {dayTotals.f} g | 碳水 {dayTotals.c} g</div>
<div className="text-xs text-slate-400 mt-2">对比目标:{target} 千卡 · 蛋白 {proteinG} g · 脂肪 {fatG} g · 碳水 {carbG} g;换食材后合计会同步变化,可按需再增减份量。</div>
</div>
<div className="mt-4 bg-white rounded-xl p-4 border border-slate-100">
<div className="flex items-center gap-2 text-slate-700 font-semibold mb-1"><Leaf size={16} /> 七大营养素 —— 全天摄入量</div>
<p className="text-xs text-slate-400 mb-3">前三大产能营养素(蛋白质 / 脂肪 / 碳水)见上方;下面显示其余各项,参考值取《中国居民膳食营养素参考摄入量》(成年人),达到参考值 80% 以上标绿。</p>
<div className="space-y-3">
{nutrientRows.map(r => {
const pct = r.ref > 0 ? Math.min((r.value / r.ref) * 100, 100) : 0;
const ok = r.value >= r.ref * 0.8;
return (
<div key={r.label}>
<div className="flex justify-between text-sm mb-1">
<span className="text-slate-700">{r.label}</span>
<span className="text-slate-500">
<span className={`font-semibold ${ok ? 'text-emerald-600' : 'text-amber-600'}`}>{r.value.toFixed(r.digits)}</span>
{' / '}{r.ref} {r.unit}
</span>
</div>
<div className="h-1.5 bg-slate-100 rounded-full overflow-hidden">
<div className={`h-full rounded-full ${ok ? 'bg-emerald-500' : 'bg-amber-400'}`} style={{ width: `${pct}%` }} />
</div>
</div>
);
})}
</div>
<div className="mt-4 flex items-start gap-2 text-xs text-sky-700 bg-sky-50 rounded-lg p-3">
<Droplets size={14} className="mt-0.5 shrink-0 text-sky-500" />
表中的「水」只统计食物本身所含的水分;按《中国居民膳食指南》,成年人每天还需额外饮水 {drinkWater} ml 左右。
</div>
</div>
<p className="text-xs text-slate-400 mt-4">* 同类食材可放心互换;数据为常见品种的近似值,仅供参考,保证每餐有主食、优质蛋白和蔬菜。</p>
</div>
);
}