/* Shared dialog layout. Explicit selector specificity bridges legacy ID styles;
   feature controllers do not own responsive positioning or animation timing. */
.dialog-header { position: sticky; top: 0; z-index: 10; flex: 0 0 auto; padding-bottom: 12px; background: var(--page, #f8f6f2); }
.dialog-header :is(.dialog-head,.roadmap-detail-head) { align-items: flex-start; }
.dialog-header :is(.close,.roadmap-detail-close,[data-close]) { align-self: flex-start; }
:is(dialog, #foodDialog, #shoppingDialog, #quickTargetDialog).bottom-sheet:not(.food-page) {
  position: fixed;
  inset: auto 0 0;
  margin: 0 auto;
  width: min(720px, 100%);
  max-width: 100%;
  height: auto;
  max-height: calc(100dvh - 24px);
  box-sizing: border-box;
  border-radius: 24px 24px 0 0;
  padding-top: 48px;
  padding-bottom: max(24px, env(safe-area-inset-bottom));
  overflow: auto;
  overscroll-behavior: contain;
  transform: translateY(var(--sheet-drag, 0px));
  transition: transform 180ms ease-out;
}
dialog.bottom-sheet:not(.food-page).sheet-dragging { transition: none; user-select: none; }
dialog.bottom-sheet:not(.food-page) .sheet-drag-handle {
  position: absolute; inset: 0 0 auto; margin: 0; width: 100%; height: 40px;
  min-height: 40px; padding: 0; border: 0; border-radius: 24px 24px 0 0;
  display: grid; place-items: center; background: transparent; color: inherit;
  cursor: grab; touch-action: none; flex-shrink: 0;
}
dialog.bottom-sheet:not(.food-page) .sheet-drag-handle:active { cursor: grabbing; }
dialog.bottom-sheet:not(.food-page) .sheet-drag-handle > span {
  display: block; width: 40px; height: 5px; border-radius: 5px;
  background: currentColor; opacity: .4; pointer-events: none;
}
dialog.bottom-sheet:not(.food-page) .sheet-drag-handle:focus-visible { outline: 2px solid currentColor; outline-offset: -5px; }
@media(prefers-reduced-motion:reduce) { dialog.bottom-sheet:not(.food-page) { transition: none; } }

dialog.bottom-sheet:not(.food-page) .sheet-drag-region { touch-action: none; cursor: grab; user-select: none; }
dialog.bottom-sheet:not(.food-page) .sheet-drag-region:active { cursor: grabbing; }
dialog.bottom-sheet:not(.food-page) .sheet-drag-region :is(button, a, input, select, textarea, summary) { cursor: pointer; }

/* Keep heading, close control and drag handle visible while sheet content scrolls. */
dialog.bottom-sheet:not(.food-page) > .sheet-header {
  position: sticky; top: -48px; z-index: 10;
  margin-top: -48px; padding-top: 48px; padding-bottom: 12px;
  background: var(--page, #f8f6f2);
  flex-shrink: 0;
}
dialog.bottom-sheet:not(.food-page) > .sheet-header > .sheet-drag-region { margin-top: 0; margin-bottom: 0; }

/* Explicit exit motion works without CSS overlay/display transition support. */
dialog.bottom-sheet[data-sheet-closing]:not(.food-page) { transition: none; transform: translateY(100%); pointer-events: none; }
dialog.bottom-sheet[data-sheet-closing]:not(.food-page)::backdrop { opacity: 0; }

/* Bounded flex sheets reserve space for headers and actions before scrolling lists. */
#shoppingDialog:modal[open] {
  display: flex; flex-direction: column; overflow: hidden;
  height: min(1000px, calc(100dvh - 24px));
}
#shoppingDialog > .sheet-header { flex: 0 0 auto; }
#shoppingDialog .shopping-list,
#shoppingDialog .shopping-preview {
  flex: 1 1 0; height: auto; min-height: 0; overflow: hidden;
}
#shoppingDialog .shopping-list[hidden] { display: none; }
#shoppingDialog .shopping-options { min-height: 0; overflow-y: auto; }
#shoppingDialog :is(.shopping-modal-footer, .shopping-preview-footer) { flex: 0 0 auto; }
/* Review and search sheets use the same bounded header/body/footer layout. */
#foodDialog.bottom-sheet[open] {
  height: calc(100dvh - 24px);
}
#foodDialog.bottom-sheet > .sheet-header { flex: 0 0 auto; }

/* Close controls align with the first heading line, above subtitles/back links. */
dialog.bottom-sheet:not(.food-page) > .sheet-header :is(.dialog-head, .roadmap-detail-head) { align-items: flex-start; }
dialog.bottom-sheet:not(.food-page) > .sheet-header :is(button.close, .roadmap-detail-close, button[data-close]) { align-self: flex-start; margin-top: 0; flex-shrink: 0; }

/* Desktop presentation and the larger food-logging workspace. */
@media(min-width:701px) {
  dialog:modal { position: fixed; inset: 0; margin: auto; max-width: calc(100vw - 48px); max-height: calc(100dvh - 64px); border-radius: 20px; }
  #foodDialog:modal { width: min(760px, calc(100vw - 48px)); height: min(850px, calc(100dvh - 64px)); max-height: calc(100dvh - 64px); }
  #foodDialog > .dialog-head { position: sticky; top: 0; z-index: 4; background: var(--page); }
}

/* Stable header slots shared by food logging and shopping preview. */
:is(#foodDialog, #shoppingDialog) .dialog-header > .dialog-head {
  position: relative; min-height: 44px; padding-right: 56px; display: block;
}
:is(#foodDialog, #shoppingDialog) .dialog-header > .dialog-head:has(> :is(.food-search-back,.dialog-back):not([hidden])) { padding-left: 56px; }
:is(#foodDialog, #shoppingDialog) .dialog-header > .dialog-head > .close { position: absolute; top: 0; right: 0; margin: 0; }
:is(#foodDialog, #shoppingDialog) .dialog-header > .dialog-head > :is(.food-search-back,.dialog-back) { position: absolute; top: 0; left: 0; margin: 0; }
#foodDialog .dialog-head > div { min-width: 0; }
#foodDialog .dialog-head > .food-search-back { align-self: start; }
#foodDialog .dialog-head > .food-search-back .back-arrow { font-size: 24px; line-height: 1; }

/* One reusable back control for every dialog, including shopping and search. */
dialog button:has(> .back-arrow) {
  display: grid; place-items: center; width: var(--dialog-icon-size); height: var(--dialog-icon-size); min-height: var(--dialog-icon-size);
  padding: 0; border: 0; border-radius: 50%; margin: 0;
  background: var(--card); color: var(--text); text-decoration: none;
}
dialog button:has(> .back-arrow):hover { background: var(--line); text-decoration: none; }
dialog button:has(> .back-arrow) .back-arrow { display: grid; place-items: center; }
dialog button:has(> .back-arrow) .back-label { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
dialog button:has(> .back-arrow):focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
/* Match the established 35px close-button circles across all dialog controls. */
:root { --dialog-icon-size: 35px; }
:is(dialog, #foodDialog, #shoppingDialog, #quickTargetDialog) button:is(.close, .roadmap-detail-close, [data-close], :has(> .back-arrow)),
:is(dialog, #foodDialog, #shoppingDialog, #quickTargetDialog) .dialog-header :is(.dialog-head, .roadmap-detail-head) > button:is(.close, .roadmap-detail-close, [data-close], :has(> .back-arrow)) {
  box-sizing: border-box; width: var(--dialog-icon-size); height: var(--dialog-icon-size);
  min-width: var(--dialog-icon-size); min-height: var(--dialog-icon-size); max-width: var(--dialog-icon-size);
  padding: 0; display: grid; place-items: center; flex-shrink: 0;
}
/* Keep the touch target at least 44px while retaining the smaller visible circle. */
dialog button:is(.close, .roadmap-detail-close, [data-close], :has(> .back-arrow))::after { content: ""; position: absolute; inset: -4.5px; border-radius: 50%; }
:where(dialog) button:where(.close, .roadmap-detail-close, [data-close], :has(> .back-arrow)) { position: relative; }

/* Hidden navigation must not occupy a header slot or override the native hidden state. */
:is(#foodDialog, #shoppingDialog) .dialog-header .dialog-head > button[hidden] { display: none; }
