/* Toasts. DESIGN.md section 4 (Toasts): bottom centre, at most three stacked,
   a draining hairline that pauses on hover or focus. Section 6 for the
   z-overlay budget and radius-md, section 2 for the status colours. */

@layer components {
  /* A manual popover, so a toast raised from inside a dialog is still visible.
     The host spans the bottom of the viewport but must never intercept a
     pointer; only the toasts themselves do. */
  .toast-host {
    position: fixed;
    inset: auto 0 0 0;
    /* The popover UA stylesheet sets `width: fit-content; height: fit-content;
       margin: auto`. Left unchallenged, the host collapses around its widest
       toast and, being over-constrained against `inset: auto 0 0 0`, resolves
       to the left edge - so `align-items: center` centres the toast inside a
       box that is itself no longer centred. Bottom left is not bottom centre
       (DESIGN.md section 4), and at narrow widths the two look alike, which is
       how it survived a first look. */
    inline-size: 100%;
    block-size: auto;
    margin: 0;
    z-index: var(--z-overlay);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-2);
    padding: 0 var(--space-4) calc(var(--space-6) + env(safe-area-inset-bottom));
    border: 0;
    background: none;
    overflow: visible;
    pointer-events: none;
  }

  .toast-host:popover-open {
    display: flex;
  }

  .toast {
    position: relative;
    pointer-events: auto;
    display: flex;
    align-items: start;
    gap: var(--space-3);
    width: min(420px, 100%);
    padding: var(--space-3) var(--space-3) var(--space-3) var(--space-4);
    border: 1px solid var(--border);
    border-inline-start: 2px solid var(--toast-accent, var(--info));
    border-radius: 0;
    background: var(--surface-3);
    backdrop-filter: var(--glass-filter-strong);
    color: var(--text);
    box-shadow: var(--shadow-2);
    overflow: hidden;
    opacity: 0;
    translate: 0 calc(var(--motion, 1) * var(--space-3));
    transition: opacity var(--dur-3) var(--ease-out),
                translate var(--dur-3) var(--ease-spring);
  }

  .toast[data-state='shown'] {
    opacity: 1;
    translate: 0 0;
  }

  .toast[data-state='leaving'] {
    opacity: 0;
    translate: 0 calc(var(--motion, 1) * var(--space-2));
    transition-duration: var(--dur-2);
  }

  /* Status is never carried by colour alone (DESIGN.md section 2): the title
     text names what happened, and the accent only reinforces it. */
  .toast[data-kind='error']   { --toast-accent: var(--danger); }
  .toast[data-kind='warning'] { --toast-accent: var(--warning); }
  .toast[data-kind='success'] { --toast-accent: var(--success); }
  .toast[data-kind='info']    { --toast-accent: var(--info); }

  .toast__body {
    flex: 1;
    min-width: 0;
    display: grid;
    gap: var(--space-1);
  }

  .toast__title {
    margin: 0;
    font-size: var(--text-sm);
    line-height: var(--text-sm-lh);
    font-weight: 500;
    text-wrap: balance;
    overflow-wrap: anywhere;
  }

  .toast__text {
    margin: 0;
    font-size: var(--text-sm);
    line-height: var(--text-sm-lh);
    color: var(--text-muted);
    text-wrap: pretty;
    /* A caller may pass a server's own message straight through, and its
       length is not ours to predict. Clamped so an unusually long refusal
       cannot grow a toast to the height of the viewport. */
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 4;
    overflow: hidden;
    overflow-wrap: anywhere;
  }

  .toast__controls {
    display: flex;
    align-items: center;
    gap: var(--space-1);
    flex-shrink: 0;
  }

  .toast__close {
    display: grid;
    place-items: center;
    /* 24px is the guideline floor for a pointer target; touch gets 44. */
    inline-size: calc(24px * var(--ui-scale));
    block-size: calc(24px * var(--ui-scale));
    padding: 0;
    border: 0;
    border-radius: var(--radius-pill);
    background: none;
    color: var(--text-muted);
    font-size: var(--text-base);
    line-height: 1;
    cursor: pointer;
    transition: color var(--dur-1) var(--ease-out),
                background var(--dur-1) var(--ease-out);
  }

  .toast__close:hover {
    color: var(--text);
    background: color-mix(in oklab, var(--text) 10%, transparent);
  }

  .toast__close:focus-visible {
    outline: 2px solid var(--focus-ring);
    outline-offset: 2px;
  }

  @media (pointer: coarse) {
    .toast__close { inline-size: calc(44px * var(--ui-scale)); block-size: calc(44px * var(--ui-scale)); }
  }

  /* The draining hairline. Its animation is the toast's clock: dismissal
     happens on animationend, so pausing it here pauses the dismissal. */
  .toast__drain {
    position: absolute;
    inset-block-end: 0;
    inset-inline-start: 0;
    inline-size: 100%;
    block-size: 2px;
    background: var(--toast-accent, var(--info));
    transform-origin: left center;
    scale: 0 1;
  }

  .toast__drain[data-running='true'] {
    animation: toast-drain linear forwards;
  }

  .toast:hover .toast__drain,
  .toast:focus-within .toast__drain {
    animation-play-state: paused;
  }

  @keyframes toast-drain {
    from { scale: 1 1; }
    to   { scale: 0 1; }
  }
}
