/*
 * The dish over the menu: a modal dialog that covers the page with the scrim and holds the dish (templates/menu/_sheet
 * .html.twig). On a phone it is a sheet that comes up from below, 26px under the top of the screen, with a handle and a
 * close button; from 600px wide it is a centred dialog. The form of the dish (the same on the page of the dish) is a
 * scrolling part and a foot with the quantity and the button that stay where they are while the options scroll.
 *
 * The dialog is the scrim itself (it covers the screen), and a tap on it outside the panel closes the dish. A dialog that
 * is not open keeps the display: none of the browser: only [open] sets a display.
 */

@layer components {
  .sheet-host {
    position: fixed;
    inset: 0;
    width: 100%;
    max-width: none;
    height: 100%;
    max-height: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    overscroll-behavior: contain;
    border: 0;
    background: var(--scrim);
    color: var(--text);
  }

  .sheet-host[open] {
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
  }

  /* The page behind does not scroll while the dish is open; the gutter stays, so that the page does not jump sideways. */
  html:has(.sheet-host[open]) {
    overflow: hidden;
    scrollbar-gutter: stable;
  }

  .sheet-host__frame {
    display: flex;
    flex: 1;
    flex-direction: column;
    min-height: 0;
    margin-top: calc(26px + env(safe-area-inset-top));
  }

  .sheet {
    display: flex;
    flex: 1;
    flex-direction: column;
    min-height: 0;
    border-radius: 22px 22px 0 0;
    background: var(--surface);
    box-shadow: var(--shadow-2);
  }

  /* The handle and the close button: also where a swipe down closes the dish. */
  .sheet__top {
    flex: none;
    touch-action: none;
  }

  .sheet__handle {
    display: block;
    width: 40px;
    height: 5px;
    margin: 8px auto 0;
    border-radius: 3px;
    background: var(--border-strong);
    opacity: 0.6;
  }

  .sheet__bar {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    min-height: 52px;
    padding: var(--s-2) var(--s-4) 0;
  }

  /* From 600px wide: a dialog in the middle of the screen, with the menu behind the scrim. */
  @media (min-width: 600px) {
    .sheet-host[open] {
      align-items: center;
      justify-content: center;
      padding: var(--s-5) var(--s-4);
    }

    .sheet-host__frame {
      flex: none;
      width: min(560px, 100%);
      max-height: 100%;
      margin-top: 0;
    }

    .sheet {
      flex: 0 1 auto;
      border-radius: var(--r-lg);
    }

    .sheet__handle {
      display: none;
    }

    .sheet__bar {
      min-height: 0;
      padding-top: var(--s-2);
    }
  }

  /* ---- The form of the dish: in the sheet and on the page ---- */
  .dish-form {
    display: flex;
    flex: 1;
    flex-direction: column;
    min-height: 0;
  }

  .dish-form__scroll {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .dish-form__photo {
    display: block;
    width: 100%;
    height: auto;
    max-height: 260px;
    object-fit: cover;
  }

  .dish-form__body {
    padding: 0 var(--s-4) var(--s-4);
  }

  .dish-form__title {
    margin: var(--s-2) 0 var(--s-1);
    font-size: 1.5rem;
  }

  .dish-form__lead {
    margin: 0 0 var(--s-3);
    font-size: 0.9375rem;
  }

  .dish-form__alert {
    margin-top: var(--s-4);
  }

  .dish-form__body > .dish-form__field {
    margin: var(--s-4) 0 0;
  }

  .dish-form__note-allergy .note {
    margin: var(--s-4) 0 0;
  }

  /* The foot stays at the bottom of the sheet, clear of the home indicator like the tab bar is. */
  .dish-form__foot {
    display: flex;
    flex: none;
    align-items: center;
    gap: var(--s-3);
    padding: var(--s-3) var(--s-4) calc(var(--s-3) + env(safe-area-inset-bottom));
    border-top: 1px solid var(--border);
    background: var(--surface);
  }

  .dish-form__submit {
    flex: 1;
    min-width: 0;
  }

  /* The dialog of a wide screen has round corners all round, and its foot is the bottom of it. */
  @media (min-width: 600px) {
    .dish-form__foot {
      padding-bottom: var(--s-3);
      border-radius: 0 0 var(--r-lg) var(--r-lg);
    }
  }

  /* The error under a group that needs a choice. */
  .group__error {
    margin-top: var(--s-2);
    color: var(--spice);
    font-size: 0.875rem;
    font-weight: 600;
  }

  .group--invalid .rule {
    background: var(--spice-soft);
    color: var(--spice);
  }

  /* On the page of the dish there is no sheet: the form runs with the page and its foot comes after the note. */
  .dish-form--page .dish-form__scroll {
    overflow: visible;
  }

  .dish-form--page .dish-form__body {
    padding: 0;
  }

  /*
   * Its foot sticks above the tab bar (and above the cart bar, which is there when the cart is not empty): the quantity
   * and the button are never half under a bar, and at the end of the page it is where it would be without sticking.
   */
  .dish-form--page .dish-form__foot {
    position: sticky;
    bottom: var(--bottom-bars);
    z-index: 21;
    margin: var(--s-5) calc(var(--s-4) * -1) calc(var(--s-4) * -1);
    padding: var(--s-3) var(--s-4);
    border-top: 1px solid var(--border);
    background: var(--surface);
  }

  .app:has(.cartbar) .dish-form--page .dish-form__foot {
    bottom: calc(var(--bottom-bars) + var(--cartbar-h));
  }

  @media (min-width: 600px) {
    .dish-form--page .dish-form__foot {
      margin-inline: calc(var(--s-6) * -1);
      margin-bottom: calc(var(--s-6) * -1);
      padding-inline: var(--s-6);
    }
  }

  /* ---- The allergens behind a row: a disclosure ---- */
  .allergens {
    margin-top: var(--s-4);
    border-radius: var(--r-md);
    background: var(--surface-2);
    color: var(--muted);
    font-size: 0.875rem;
  }

  .allergens__summary {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--s-3);
    min-height: var(--tap);
    padding: var(--s-3) var(--s-4);
    border-radius: var(--r-md);
    list-style: none;
  }

  .allergens__summary::-webkit-details-marker {
    display: none;
  }

  .allergens__summary:active {
    opacity: 0.6;
  }

  .allergens[open] > .allergens__summary .icon {
    transform: rotate(90deg);
  }

  .allergens__body {
    padding: 0 var(--s-4) var(--s-3);
  }

  .allergens__body .note {
    margin: 0;
    padding: 0;
    background: transparent;
  }

  @media (hover: hover) and (pointer: fine) {
    .allergens__summary {
      cursor: pointer;
    }
  }

  /* ---- Motion: the sheet slides up in about 250ms and closes faster; none of it with reduced motion ---- */
  @media (prefers-reduced-motion: no-preference) {
    .sheet-host[open] {
      animation: scrim-in var(--dur) ease-out;
    }

    .sheet-host[open] .sheet {
      animation: sheet-in 250ms cubic-bezier(0.2, 0.8, 0.2, 1);
    }

    .sheet-host[data-closing] {
      animation: scrim-out 140ms ease-in forwards;
    }

    .sheet-host[data-closing] .sheet {
      animation: sheet-out 160ms ease-in forwards;
    }

    .allergens .icon {
      transition: transform var(--dur) ease-out;
    }

    @keyframes scrim-in {
      from {
        opacity: 0;
      }
    }

    @keyframes scrim-out {
      to {
        opacity: 0;
      }
    }

    @keyframes sheet-in {
      from {
        transform: translateY(100%);
      }
    }

    @keyframes sheet-out {
      to {
        transform: translateY(100%);
      }
    }

    @keyframes dialog-in {
      from {
        opacity: 0;
        transform: translateY(12px) scale(0.98);
      }
    }

    @keyframes dialog-out {
      to {
        opacity: 0;
        transform: translateY(12px) scale(0.98);
      }
    }

    @media (min-width: 600px) {
      .sheet-host[open] .sheet {
        animation-name: dialog-in;
        animation-duration: var(--dur);
      }

      .sheet-host[data-closing] .sheet {
        animation-name: dialog-out;
        animation-duration: var(--dur-fast);
      }
    }
  }
}
