/* Food logging: search, review, meal destination and actions. */
/* Food logging draft and persistent app action */
#addFoodFab {
  position: fixed;
  z-index: 20;
  right: max(20px, env(safe-area-inset-right, 0px));
  bottom: max(20px, env(safe-area-inset-bottom, 0px));
  border-radius: 999px;
  box-shadow: 0 5px 20px #00000026;
}
@media (max-width: 700px) {
  #addFoodFab { bottom: calc(100px + env(safe-area-inset-bottom, 0px)); }
}
.food-draft-search { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 8px; }
.food-draft-search input { grid-column: 1 / -1; grid-row: 1; }
.food-draft-search:has(button:not([hidden])) input { padding-inline-end: 72px; }
.food-draft-search input::-webkit-search-cancel-button { display: none; }
.food-draft-search button { grid-column: 2; grid-row: 1; margin-inline-end: 8px; }
.food-draft-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.food-draft-actions button { padding-inline: 8px; }
#draftAi:empty, #draftAiStatus:empty, [data-ai-status]:empty, .food-draft-footer:empty { display: none; }
.food-draft-results .estimate-option:has(input:checked) { border-color: var(--accent); background: var(--accent-secondary); }
.food-draft-results .row { gap: 8px; }
.food-draft-review .food-search-back, .food-draft-item [data-remove] { justify-self: start; }
.food-draft-item { display: grid; gap: 12px; min-width: 0; margin: 0; }
.food-draft-item legend { font-weight: 700; overflow-wrap: anywhere; }
.food-draft-item .field-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.food-draft-footer { display: grid; gap: 8px; position: sticky; bottom: 0; z-index: 1; background: var(--page); padding-block: 10px; }

/* Consistent logging height and compact meal selection. */
#foodDialog { height: 90vh; max-height: 90vh; box-sizing: border-box; overflow-y: auto; }
.draft-meal-picker { border: 0; padding: 0; margin: 0; min-width: 0; }
.draft-meal-picker legend { font-weight: 700; font-size: 14px; margin-bottom: 10px; }
.draft-meal-options { overflow-y: hidden; }
.draft-meal-options label { flex: 1; min-width: max-content; position: relative; margin: 0; }
.draft-meal-options input { position: absolute; inset: 0; z-index: 2; opacity: 0; width: 100%; height: 100%; min-height: 0; padding: 0; border: 0; margin: 0; cursor: pointer; }
.draft-meal-options label > span { cursor: pointer; font-weight: 400; }
.draft-meal-options input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 3px; }

/* Draft controls retain their intended layout inside the mobile dialog. */
#foodDialog .food-draft-actions { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); gap:8px; }
#foodDialog .food-draft-actions button { width:100%; min-width:0; padding-inline:6px; font-size:14px; }
#foodDialog .food-draft-actions .icon { width:18px; height:18px; flex-shrink:0; }
#foodDialog #draftRecentSort { width:auto; max-width:145px; min-height:36px; padding:4px 24px 4px 8px; border:0; background-color:transparent; color:var(--muted); font-size:12px; }
#foodDialog .portion-fields { grid-template-columns:repeat(auto-fit,minmax(min(100%,180px),1fr)); }
#foodDialog .edit-entry-portion .portion-fields { grid-template-columns:1fr; gap:20px; }
#foodDialog[open]:has(:is(.food-draft-review,.food-draft-search-view)) { display:flex; flex-direction:column; overflow:hidden; }
#foodDialog:has(:is(.food-draft-review,.food-draft-search-view)) > .dialog-head { flex:none; }
#foodDialog:has(:is(.food-draft-review,.food-draft-search-view)) > #modalBody { flex:1; min-height:0; overflow:hidden; }
#foodDialog .food-draft-review { display:flex; flex-direction:column; height:100%; min-height:0; gap:0; margin:0; }
.food-draft-review-fields { display:grid; gap:20px; overflow-y:auto; min-height:0; flex:1; padding:4px 4px 16px; }
@supports (-webkit-touch-callout: none) {
  .food-draft-review-fields { scrollbar-width:none; }
  .food-draft-review-fields::-webkit-scrollbar { display:none; }
}
#foodDialog .food-draft-review > .food-draft-footer { position:static; flex:none; margin:0; padding-block:12px 0; border-top:1px solid var(--line); }


/* Search content scrolls independently of the catalogue/review actions. */



#foodDialog .food-draft-search-view { display:flex; flex-direction:column; height:100%; min-height:0; gap:0; }
.food-draft-search-content { display:grid; gap:20px; min-height:0; flex:1; overflow-y:auto; padding:0 4px 16px; align-content:start; }
#foodDialog .food-draft-search-content .food-draft-results { max-height:none; overflow:visible; padding-top:0; }
.food-draft-picker-footer { display:grid; gap:12px; flex:none; padding-top:12px; border-top:1px solid var(--line); background:var(--page); }
#foodDialog .food-draft-picker-footer .food-draft-footer { position:static; padding:0; }
#foodDialog .food-draft-picker-footer:has(#draftBrowse[hidden]):not(:has(#reviewDraft)) { display:none; }

/* Food review refinements: fixed navigation and uncluttered entry fields. */
#foodDialog .dialog-head.food-draft-header { flex-wrap: wrap; }
#foodDialog .food-draft-header > .food-search-back { flex-basis: 100%; margin: 16px 0 0; text-align: left; }
#foodDialog #draftScanBack { margin-bottom: 20px; }
#foodDialog .food-draft-item { background: transparent; }

#foodDialog > .food-draft-header { position: sticky; top: 0; z-index: 2; background: var(--page); }

#foodDialog .food-draft-item > .field-grid > div:only-child { grid-column: 1 / -1; }
#foodDialog .food-draft-header > .food-search-back { margin-top: 4px; }

#foodDialog .food-draft-item { border:0; padding:0; border-radius:0; margin:0 0 24px; gap:16px; }
#foodDialog .food-draft-item > legend { padding:0; margin-bottom:16px; font-size:20px; }
#foodDialog .food-draft-item .field-grid { grid-template-columns:1fr; gap:20px; }
/* No empty sticky surface when there is no review action. */
#foodDialog .food-draft-picker-footer:not(:has(#reviewDraft)) { display:none; }

/* Keep logging methods together, including on narrow phones. */
#foodDialog .food-draft-methods { grid-template-columns:repeat(2,minmax(0,1fr)); }
#foodDialog .food-draft-methods button { display:flex; align-items:center; justify-content:center; gap:6px; }
#foodDialog .draft-ai-conversation { display:grid; gap:16px; }
#foodDialog .draft-ai-conversation .description-reply { margin-top:8px; }

@media (max-width:540px) {
  #foodDialog .food-draft-methods button { flex-direction:column; gap:6px; padding:10px 3px; font-size:12px; }
  #foodDialog .food-draft-methods button span { white-space:nowrap; }
}

#foodDialog .draft-ai-conversation [data-ai-allowance] { padding-bottom:0; }

/* Match the colour markers in Today’s meal navigation, including theme changes. */
.draft-meal-options input[value="Breakfast"] + span { --meal-color: var(--meal-breakfast); }
.draft-meal-options input[value="Lunch"] + span { --meal-color: var(--meal-lunch); }
.draft-meal-options input[value="Dinner"] + span { --meal-color: var(--meal-dinner); }
.draft-meal-options input[value="Snack"] + span { --meal-color: var(--meal-snacks); }
.draft-meal-options span::before { content: ""; }

/* Native textarea sizing, with the existing JavaScript fallback retained. */
@supports (field-sizing: content) {
  #foodDialog textarea#foodSearch { field-sizing: content; min-block-size: 52px; max-block-size: calc(3lh + 28px); overflow-y: auto; }
}
/* Manual entry asks for a total, with an optional description of the amount. */
#foodDialog .manual-carb-help { margin:8px 0 0; }
#foodDialog .food-draft-review-fields { align-content:start; gap:24px; }
#foodDialog .manual-draft-item { align-content:start; gap:20px; margin-bottom:0; }
#foodDialog .draft-add-another { justify-self:start; }
#foodDialog .food-draft-footer > [hidden] { display:none; }

/* Countable bread and fruit: keep direct entry alongside labelled step controls. */
.quantity-control.is-stepper { display:grid; width:max(50%,176px); max-width:100%; grid-template-columns:44px minmax(0,1fr) 44px; gap:8px; align-items:center; }
.quantity-control input { min-width:0; }
.quantity-control.is-stepper input { text-align:center; padding-inline:8px; }
.quantity-control button { min-width:44px; min-height:44px; padding:0; font-size:24px; border:1px solid var(--secondary-border); border-radius:50%; background:var(--card); color:var(--text); }
.quantity-control button[hidden] { display:none; }
.quantity-control button:disabled { opacity:.4; }

.portion-mode-toggle { justify-self:start; min-height:44px; padding:0; text-align:left; }

.draft-review-guidance { font-size: 12px; }

/* Search and explicit selections stay visible across changing queries. */
.draft-unlisted { display:flex; align-items:center; justify-content:center; gap:8px; width:100%; }
.food-draft-results .estimate-option:has(input:checked) { box-shadow:inset 0 0 0 2px var(--accent); }
#draftSelection > p { margin:0; }
.diary-impact { margin:4px 0; line-height:1.5; }
.idea-component.is-removed > input, .idea-component.is-removed > p { display:none; }
.idea-component.is-removed label { text-decoration:line-through; color:var(--muted); }
.idea-component .row label { flex:1; min-width:0; }
.idea-component .row button { flex:none; min-height:40px; }

/* The AI composer stays at the sheet/modal's bottom; only messages scroll. */
#foodDialog:modal[open]:has(.draft-ai-conversation) { display:flex; flex-direction:column; overflow:hidden; height:min(820px,90dvh,var(--food-ai-height,90dvh)); }
#foodDialog:has(.draft-ai-conversation) > #modalBody { flex:1; min-height:0; overflow:hidden; }
#foodDialog .draft-ai-conversation { display:flex; flex-direction:column; gap:12px; height:100%; min-height:0; }
#foodDialog .draft-ai-conversation .description-conversation { flex:1; min-height:0; overflow-y:auto; align-content:start; margin:0; padding-block:8px 16px; overscroll-behavior:contain; }
#foodDialog .draft-ai-conversation .description-reply { flex:none; margin:0; padding-top:14px; background:var(--page); }
#foodDialog .draft-ai-conversation :is(#draftAiStatus,#foodAiVerification):empty { display:none; }
@media (max-width:700px) {
  #foodDialog.bottom-sheet[open]:has(.draft-ai-conversation) { height:var(--food-ai-height,calc(100dvh - 24px)); max-height:var(--food-ai-height,calc(100dvh - 24px)); bottom:var(--food-ai-bottom,0px); }
}

.estimate-option > .food-carb-value { margin-left:auto; flex:none; text-align:right; }
.food-carb-value small, .meal-idea-carb-label { display:block; font-size:10px; font-weight:400; color:var(--muted); }

#foodDialog .food-draft-search-field { position:sticky; top:0; z-index:2; background:var(--page); }

#foodDialog .food-draft-results > .estimate-group:first-child .estimate-group-title { margin-top:0; }

/* Keep all food-entry scrolling inside the body, below the opaque header. */
#foodDialog:modal[open] { display:flex; flex-direction:column; overflow:hidden; }
#foodDialog:modal:not(:has(.food-draft-review,.food-draft-search-view,.draft-ai-conversation)) > #modalBody { flex:1; min-height:0; overflow-y:auto; overscroll-behavior:contain; }
#foodDialog:modal > .dialog-header { flex:none; background:var(--page); }

/* Match the vertical method spacing to the gap between Scan and AI lookup. */
#foodDialog .food-draft-method-group { display:grid; gap:8px; }

/* Date display and chooser share one fixed-height header row. */
.food-log-date-control { display:flex; flex-wrap:nowrap; align-items:center; gap:12px; margin-top:4px; height:36px; min-width:0; }
.food-log-date-control > button { flex:none; min-width:72px; padding:0; min-height:28px; font-size:12px; font-weight:400; text-align:left; }
.food-log-date-control > .log-for-date { margin:0; min-width:0; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.food-log-date-control > select { position:static; flex:1; width:0; min-width:0; max-width:100%; height:36px; min-height:36px; padding:4px 8px; font-size:14px; background:var(--page); box-shadow:none; }
.food-log-date-control > :is(select,.log-for-date)[hidden] { display:none; }

.draft-item-actions { display:flex; align-items:center; flex-wrap:wrap; gap:4px 16px; }

.draft-item-position {
  display: block;
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

@media (max-width:700px) {
  .food-draft-results .food-swipe-row > .estimate-option {
    border-radius: inherit;
  }
}

.food-thought-line { align-self:start; color:var(--muted); font-size:14px; padding:8px 12px; }
.food-thought-line summary { cursor:pointer; }
.food-thought-line strong { color:var(--ink); }
.food-thought-line .thought-time { margin-inline-start:8px; font-variant-numeric:tabular-nums; }
.food-thought-line ol { margin:8px 0 0 6px; padding-inline-start:18px; border-inline-start:1px solid var(--line); list-style:none; }
.food-thought-line li { position:relative; padding-block:3px; }
.food-thought-line li::before { content:""; position:absolute; inset-inline-start:-22px; top:.8em; width:7px; height:7px; border-radius:50%; background:var(--muted); }
.food-thought-line:not(.is-finished) li:last-child::before { background:var(--accent); }
@media (prefers-reduced-motion:no-preference) {
  .food-thought-line:not(.is-finished) li:last-child::before { animation:thought-pulse 1.5s ease-in-out infinite alternate; }
  @keyframes thought-pulse { to { opacity:.4; } }
}

.description-message p.chat-typing { position:relative; color:transparent; }
.chat-typed-text { position:absolute; inset:0; color:var(--text); pointer-events:none; }
.chat-typed-text::before { content:attr(data-typed); }
.chat-typed-text::after { content:""; border-inline-start:2px solid currentColor; animation:chat-caret .7s steps(1,end) infinite; }
@keyframes chat-caret { 50% { opacity:0; } }
@media (prefers-reduced-motion:reduce) {
  .description-message p.chat-typing { color:inherit; }
  .chat-typed-text { display:none; }
}
