/*
 * What sticks above the tab bar: the cart bar (the number of items, "Bekijk mandje", the subtotal; a link to the cart)
 * and the messages above it ("Chicken Korma toegevoegd", "Chicken Korma verwijderd" with the button that undoes it).
 * The dock is sticky and not fixed: the page is a container (layout.css), which makes a fixed child scroll with the page.
 * It sits in the flow just above the tab bar, so the end of the page has the room of the bar, and sticks to the bottom of
 * the screen, clear of the tab bar, while the page scrolls. The tablet in landscape and the desktop have the cart panel
 * and no bar.
 */

@layer components {
  .dock {
    position: sticky;
    bottom: var(--bottom-bars);
    z-index: 22;
  }

  /* The messages float above the dock (above the cart bar, or above the foot of the cart page). */
  .toasts {
    position: absolute;
    right: 0;
    bottom: 100%;
    left: 0;
    display: flex;
    justify-content: center;
    padding: 0 var(--s-3) var(--s-2);
    pointer-events: none;
  }

  .app:has(.cart-foot) .toasts {
    bottom: calc(100% + var(--cart-foot-h));
  }

  /*
   * Where dishes are listed (the menu, the search) the messages have a place of their own in the dock, above the cart bar:
   * the dock ends the page, so the end of the page has the room of a message (the data attribute `room`, which dock_controller.js
   * sets where the page is taller than the screen without it), and one never lies over the last row, which cannot be scrolled
   * out from under it. A page that does not scroll has no room kept (it would scroll for nothing): a message makes it as long as
   * it needs to be. A message that is taller than the room (a long name that wraps) makes the page longer while it is shown
   * (toast_controller.js keeps the end of the page in view). The dock lets pointer events through (the room is empty most of
   * the time); the bar and the message take them.
   */
  .dock:has(.cartbar-slot--messages) {
    pointer-events: none;
  }

  .dock:has(.cartbar-slot--messages) .toasts {
    position: static;
    align-items: flex-end;
    padding: 0 var(--s-3);
  }

  .dock[data-room]:has(.cartbar-slot--messages) .toasts {
    min-height: var(--toast-room);
  }

  .dock:has(.cartbar-slot--messages) .toast {
    margin-bottom: var(--s-2);
  }

  .toast {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--s-3);
    max-width: 28rem;
    min-height: 48px;
    padding: var(--s-2) var(--s-4);
    border-radius: var(--r-md);
    background: var(--text);
    color: var(--surface);
    box-shadow: var(--shadow-2);
    font-size: 0.9375rem;
    font-weight: 600;
    pointer-events: auto;
  }

  /* The text takes at least 8rem: at large text on a narrow screen the button goes under it instead of squeezing the text into a column. */
  .toast__text {
    flex: 1 1 8rem;
    min-width: 0;
    overflow-wrap: anywhere;
  }

  .toast__form {
    flex: none;
  }

  .toast__action {
    min-height: var(--tap);
    padding: 0 var(--s-2);
    border: 0;
    background: transparent;
    color: inherit;
    font-weight: 700;
    text-decoration: underline;
    text-underline-offset: 0.2em;
  }

  .toast__action:focus-visible {
    outline-color: var(--surface);
  }

  .toast__action:active {
    opacity: 0.6;
  }

  .cartbar {
    display: flex;
    align-items: center;
    gap: var(--s-3);
    min-height: 56px;
    margin: 0 var(--s-3) var(--s-3);
    padding: 0 var(--s-4);
    border-radius: var(--r-md);
    background: var(--primary);
    color: var(--on-primary);
    box-shadow: var(--shadow-2);
    font-weight: 700;
    text-decoration: none;
    pointer-events: auto;
  }

  .cartbar:focus-visible {
    outline-offset: 3px;
  }

  .cartbar:active {
    opacity: 0.85;
  }

  .cartbar__count {
    display: grid;
    flex: none;
    place-items: center;
    min-width: 26px;
    height: 26px;
    padding: 0 6px;
    border-radius: 13px;
    background: var(--on-primary);
    color: var(--primary);
    font-size: min(0.875rem, 16px);
  }

  .cartbar__label {
    flex: 1;
    min-width: 0;
    overflow-wrap: anywhere;
  }

  .cartbar__total {
    flex: none;
    font-variant-numeric: tabular-nums;
  }

  /* From 600px wide the content has gutters of 16px (the cards, the search field, the dish grid): the bar is as wide as that column. */
  @media (min-width: 600px) {
    .cartbar {
      margin-inline: var(--s-4);
    }
  }

  /* The page ends clear of the cart bar: the bar is in the flow above the tab bar, so nothing is under it at the end. */

  /* Expanded: the cart panel has the cart; the messages come in the middle of the screen's bottom. */
  @media (min-width: 1024px) {
    .cartbar {
      display: none;
    }
  }

  @media (prefers-reduced-motion: no-preference) {
    .toast {
      animation: toast-in var(--dur) ease-out;
    }

    .toast[data-leaving] {
      animation: toast-out var(--dur-fast) ease-in forwards;
    }

    @keyframes toast-in {
      from {
        opacity: 0;
        transform: translateY(8px);
      }
    }

    @keyframes toast-out {
      to {
        opacity: 0;
        transform: translateY(8px);
      }
    }
  }
}
