/* Generic toast mechanism (flash). Linked EXPLICITLY in app/views/layouts/_head.html.erb through
   stylesheet_link_tag "toast", so it reaches every layout (application, SPA, admin).
   Its values come from the design system's tokens, declared on .kl__toast-region as well as on .kl — so they resolve in
   the SPA and admin layouts too, where the region lies outside any .kl wrapper. Only the z-index below is this sheet's
   own: a token of the mechanism, not of the style. */

@layer components {
  :root {
    /* z-index above .kl__nav (20) and with headroom over the layers of the React applications. */
    --toast-z: 1200;
  }

  /* Region: the top-right corner, a stack with a gap. pointer-events: none on the region, auto on
     the individual toasts — the region does not block clicks on the background between toasts. */
  .kl__toast-region {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: var(--toast-z);
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: min(380px, calc(100vw - 32px));
    pointer-events: none;
  }

  .kl__toast {
    pointer-events: auto;
    display: flex;
    align-items: flex-start;
    gap: 10px;
    border-radius: var(--radius-field);
    padding: 14px 16px;
    font-family: var(--font-sans);
    font-size: var(--text-base);
    line-height: 1.45;
    border: 1px solid transparent;
    box-shadow: var(--shadow-xl);
    /* VISIBLE by default — no opacity:0 at rest, so the message stays readable also
       without JS / on a JS error (the flash carries important content). The enter/exit animation
       is driven by the --enter/--closing classes added by JS. */
    transition: opacity var(--duration-base) var(--ease-standard), transform var(--duration-base) var(--ease-standard);
  }

  /* The starting state of the enter animation — added ONLY by JS (connect() in toast_controller.js), so
     without JS the toast stays visible. JS removes the class in the next frame => slide-in. */
  .kl__toast--enter { opacity: 0; transform: translateY(-8px); }

  /* The exit animation (auto-dismiss / manual close). */
  .kl__toast--closing { opacity: 0; transform: translateY(-8px); }

  .kl__toast-icon { flex-shrink: 0; margin-top: 1px; display: inline-flex; }
  .kl__toast-icon svg { width: 18px; height: 18px; } /* normalization (inline_check = 22px) */

  .kl__toast-message { margin: 0; flex: 1; word-break: break-word; } /* long URLs do not stretch it */

  .kl__toast-close {
    flex-shrink: 0;
    background: transparent;
    border: 0;
    cursor: pointer;
    font-size: var(--heading-xs);
    line-height: 1;
    padding: 0 2px;
    color: inherit;
    opacity: .7;
  }
  .kl__toast-close:hover,
  .kl__toast-close:focus-visible { opacity: 1; }

  /* Variants: the soft families of the palette. Text = the family's dark accent: success → --success-dark, the rest →
     --error/--warning/--info. */
  .kl__toast--success { background: var(--success-bg); border-color: var(--success-bd); color: var(--success-dark); }
  .kl__toast--success .kl__toast-icon { color: var(--success); }
  .kl__toast--error   { background: var(--error-bg);   border-color: var(--error-bd);   color: var(--error); }
  .kl__toast--warning { background: var(--warning-bg); border-color: var(--warning-bd); color: var(--warning); }
  .kl__toast--info    { background: var(--info-bg);    border-color: var(--info-bd);    color: var(--info); }

  /* Mobile: full width at the top. */
  @media (max-width: 640px) {
    .kl__toast-region { top: 8px; right: 8px; left: 8px; width: auto; }
  }

  /* Reduced-motion: no transitions (JS shows/removes at once). */
  @media (prefers-reduced-motion: reduce) {
    .kl__toast { transition: none; transform: none; }
  }
}
