{t('custom_meals', lang)}
{#if !editing}
{t('new_meal', lang)}
{/if}
{#if loading}
{t('loading', lang)}
{:else if editing}
{editingId ? t('edit', lang) : t('new_meal', lang)}
{t('meal_name', lang)}
{t('ingredients', lang)} ({ingredients.length})
{#if ingredients.length > 0}
{#each ingredients as ing, i} {@const sp = ing.selectedPortion} {@const displayQty = sp ? Math.round((ing.amountGrams / sp.grams) * 10) / 10 : ing.amountGrams} {@const displayUnit = sp ? sp.description : 'g'}
{ing.name}
{#if ing.source !== 'custom'}
{ing.source === 'bls' ? 'BLS' : 'USDA'}
{/if}
{ const qty = Number(e.target.value) || 1; ingredients[i].amountGrams = sp ? Math.round(qty * sp.grams) : qty; ingredients = [...ingredients]; }} /> {#if ing.portions?.length > 0}
p.description === sp.description) : -1} onchange={(e) => { const idx = Number(e.target.value); const oldGrams = ing.amountGrams; if (idx >= 0) { const portion = ing.portions[idx]; ingredients[i].selectedPortion = portion; // Convert current grams to new unit, round to nearest 0.5 const qty = Math.round((oldGrams / portion.grams) * 2) / 2 || 1; ingredients[i].amountGrams = Math.round(qty * portion.grams); } else { ingredients[i].selectedPortion = undefined; } ingredients = [...ingredients]; }}>
g
{#each ing.portions as p, pi}
{p.description} ({Math.round(p.grams)}g)
{/each}
{:else}
{displayUnit}
{/if}
{#if sp}
{ing.amountGrams}g ·
{/if} {fmt((ing.per100g?.calories ?? 0) * ing.amountGrams / 100)} {t('kcal', lang)}
removeIngredient(i)} aria-label="Remove">
{/each}
{/if} {#if ingredients.length > 0}
{t('total', lang)}
{Math.round(formTotals.calories)} {t('kcal', lang)}
{fmt(formTotals.protein)}g P
{fmt(formTotals.fat)}g F
{fmt(formTotals.carbs)}g C
{/if} {#if !showSearch}
{ showSearch = true; }}>
{t('add_ingredient', lang)}
{:else}
{ showSearch = false; }} showDetailLinks={false} confirmLabel={t('add_ingredient', lang)} />
{/if}
{t('cancel', lang)}
{saving ? t('loading', lang) : t('save_meal', lang)}
{:else if meals.length === 0}
{t('no_custom_meals', lang)}
{t('create_meal_hint', lang)}
{:else}
{#each meals as meal, i}
{meal.name}
{meal.ingredients.length} {t('ingredients', lang)} — {Math.round(mealTotalCal(meal))} {t('kcal', lang)}
startEdit(meal)} aria-label={t('edit', lang)}>
deleteMeal(meal)} aria-label={t('delete_', lang)}>
{#each meal.ingredients as ing}
{ing.name} ({ing.amountGrams}g)
{/each}
{/each}
{/if}