/*
 * Shared toast / flash notifications.
 *
 * Self-contained: declares its own `--toast-*` tokens (light defaults + a dark
 * override) so it can be linked into any surface bundle (portal, blog, admin)
 * regardless of that surface's own token namespace or dark-mode support.
 *
 * Used by app/views/layouts/shared/_flash.html.erb and driven by
 * app/javascript/controllers/toast_controller.js.
 */

.toast-region {
  /* Palette ----------------------------------------------------------- */
  --toast-bg: #ffffff;
  --toast-text: #1f2330;
  --toast-text-muted: #5b6072;
  --toast-border: rgba(15, 17, 21, 0.10);
  --toast-shadow: 0 1px 3px rgba(15, 17, 21, 0.12), 0 8px 28px rgba(15, 17, 21, 0.14);
  --toast-radius: 12px;
  --toast-max-width: 26rem;

  /* Per-severity accents (overridden by the .toast--* modifiers) ------ */
  --toast-accent: #1d4ed8;

  /* Geometry ---------------------------------------------------------- */
  position: fixed;
  left: 50%;
  bottom: calc(1rem + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  z-index: 9999;

  display: flex;
  flex-direction: column;
  gap: 0.625rem;

  width: min(var(--toast-max-width), calc(100vw - 1.5rem));

  /* The region itself never intercepts clicks; individual toasts do. */
  pointer-events: none;
}

@media (prefers-color-scheme: dark) {
  .toast-region {
    --toast-bg: #23262e;
    --toast-text: #f1f2f5;
    --toast-text-muted: #b3b7c2;
    --toast-border: rgba(255, 255, 255, 0.12);
    --toast-shadow: 0 1px 3px rgba(0, 0, 0, 0.40), 0 10px 32px rgba(0, 0, 0, 0.48);
  }
}

.toast {
  pointer-events: auto;
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: 0.625rem;
  padding: 0.75rem 0.75rem 0.75rem 0.9rem;

  /* Solid fallbacks first for engines without color-mix() support. */
  background: var(--toast-bg);
  background: color-mix(in srgb, var(--toast-accent) 5%, var(--toast-bg));
  color: var(--toast-text);
  border: 1px solid var(--toast-border);
  border: 1px solid color-mix(in srgb, var(--toast-accent) 22%, var(--toast-border));
  border-left: 4px solid var(--toast-accent);
  border-radius: var(--toast-radius);
  box-shadow: var(--toast-shadow);

  font-size: 0.9rem;
  line-height: 1.45;
}

/* Severity treatments ------------------------------------------------- */
.toast--success {
  --toast-accent: #15803d;
}

.toast--info {
  --toast-accent: #1d4ed8;
}

.toast--error {
  --toast-accent: #c0341d;
}

@media (prefers-color-scheme: dark) {
  .toast--success {
    --toast-accent: #4ade80;
  }

  .toast--info {
    --toast-accent: #7aa2ff;
  }

  .toast--error {
    --toast-accent: #ff7a6b;
  }
}

/* Icon ---------------------------------------------------------------- */
.toast__icon {
  flex: 0 0 auto;
  width: 1.25rem;
  height: 1.25rem;
  margin-top: 0.05rem;
  color: var(--toast-accent);
}

/* Message ------------------------------------------------------------- */
.toast__message {
  flex: 1 1 auto;
  min-width: 0;
  /* Non-error messages stay compact: clamp to 3 lines. */
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
  overflow-wrap: anywhere;
}

/* Errors are essential and persistent: never truncate; scroll if huge. */
.toast--error .toast__message {
  display: block;
  -webkit-line-clamp: none;
  max-height: 8rem;
  overflow-y: auto;
}

/* Dismiss button ------------------------------------------------------ */
.toast__dismiss {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.6rem;
  height: 1.6rem;
  margin: -0.15rem -0.1rem 0 0;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--toast-text-muted);
  cursor: pointer;
  transition: background-color 0.12s ease, color 0.12s ease, transform 0.06s ease;
}

/* Extend the hit area to a comfortable >=44px touch target without
   inflating the visual size of the button. */
.toast__dismiss::before {
  content: "";
  position: absolute;
  inset: -0.65rem;
}

.toast__dismiss svg {
  width: 1rem;
  height: 1rem;
}

.toast__dismiss:hover {
  background: color-mix(in srgb, var(--toast-text) 12%, transparent);
  color: var(--toast-text);
}

.toast__dismiss:active {
  background: color-mix(in srgb, var(--toast-text) 20%, transparent);
  transform: scale(0.92);
}

.toast__dismiss:focus-visible {
  outline: 2px solid var(--toast-accent);
  outline-offset: 2px;
  color: var(--toast-text);
}

/* Motion -------------------------------------------------------------- */
/* Enter via a one-shot CSS animation so toasts animate in with no JS and
   remain visible if the controller never loads (progressive enhancement). */
@media (prefers-reduced-motion: no-preference) {
  .toast {
    animation: toast-in 0.24s cubic-bezier(0.34, 1.32, 0.64, 1) both;
  }

  .toast--leaving {
    animation: none;
    transition: transform 0.16s ease-in, opacity 0.16s ease-in;
    transform: translateY(0.5rem);
    opacity: 0;
  }
}

@keyframes toast-in {
  from {
    transform: translateY(calc(100% + 0.75rem));
    opacity: 0;
  }

  to {
    transform: translateY(0);
    opacity: 1;
  }
}

/* Screen-reader-only live region announcer ---------------------------- */
.toast-announcer {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Responsive: near-full-width on small screens ------------------------ */
@media (max-width: 40rem) {
  .toast-region {
    left: 0.75rem;
    right: 0.75rem;
    transform: none;
    width: auto;
  }
}
