/**
 * Toast notification system — Layer 1 (system feedback)
 */

.toast-container {
  position: fixed;
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  pointer-events: none;
  bottom: calc(var(--space-6) + env(safe-area-inset-bottom));
  right: var(--space-6);
  width: min(380px, calc(100vw - var(--space-8)));
  align-items: flex-end;
}

@media (max-width: 640px) {
  .toast-container {
    top: calc(var(--space-4) + env(safe-area-inset-top));
    bottom: auto;
    right: var(--space-4);
    left: var(--space-4);
    width: auto;
    align-items: stretch;
  }
}

.toast {
  pointer-events: all;
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  width: 100%;
  padding: 0.875rem var(--space-4) 1.125rem 0;
  background: var(--color-surface);
  border: 0.5px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  position: relative;
  overflow: hidden;
  animation: toastIn 220ms cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
  will-change: transform, opacity;
}

.toast.is-leaving {
  animation: toastOut 180ms ease forwards;
}

@keyframes toastIn {
  from {
    opacity: 0;
    transform: translateX(120%) scale(0.9);
  }

  to {
    opacity: 1;
    transform: translateX(0) scale(1);
  }
}

@keyframes toastOut {
  from {
    opacity: 1;
    transform: translateX(0) scale(1);
    max-height: 200px;
    margin-bottom: 0;
  }

  to {
    opacity: 0;
    transform: translateX(120%) scale(0.9);
    max-height: 0;
    margin-bottom: calc(var(--space-2) * -1);
  }
}

@media (max-width: 640px) {
  @keyframes toastIn {
    from {
      opacity: 0;
      transform: translateY(-120%) scale(0.95);
    }

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

  @keyframes toastOut {
    from {
      opacity: 1;
      transform: translateY(0);
      max-height: 200px;
    }

    to {
      opacity: 0;
      transform: translateY(-2.5rem);
      max-height: 0;
    }
  }
}

.toast__border {
  width: 4px;
  align-self: stretch;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  flex-shrink: 0;
}

.toast--success .toast__border { background: var(--color-success); }
.toast--error .toast__border { background: var(--color-danger); }
.toast--warning .toast__border { background: var(--color-warning); }
.toast--info .toast__border { background: var(--color-info); }
.toast--loading .toast__border { background: var(--color-brand-primary); }

.toast__icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 1.25rem;
  height: 1.25rem;
  flex-shrink: 0;
  margin-top: 1px;
}

.toast__icon svg {
  width: 1.25rem;
  height: 1.25rem;
}

.toast--success .toast__icon { color: var(--color-success); }
.toast--error .toast__icon { color: var(--color-danger); }
.toast--warning .toast__icon { color: var(--color-warning); }
.toast--info .toast__icon { color: var(--color-info); }
.toast--loading .toast__icon { color: var(--color-brand-primary); }

.toast--loading .toast__icon svg {
  animation: toastSpin 1s linear infinite;
}

@keyframes toastSpin {
  to { transform: rotate(360deg); }
}

.toast__body {
  flex: 1;
  min-width: 0;
  padding-right: var(--space-1);
}

.toast__title {
  margin: 0;
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  color: var(--color-text-primary);
  line-height: var(--line-height-snug);
}

.toast__message {
  margin: 2px 0 0;
  font-size: var(--text-xs);
  color: var(--color-text-secondary);
  line-height: var(--line-height-base);
}

.toast__progress {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: var(--color-border);
}

.toast__progress-bar {
  height: 100%;
  width: 100%;
  transform-origin: left;
  animation: toastProgress var(--duration, 5000ms) linear forwards;
}

.toast--success .toast__progress-bar { background: var(--color-success); }
.toast--error .toast__progress-bar { background: var(--color-danger); }
.toast--warning .toast__progress-bar { background: var(--color-warning); }
.toast--info .toast__progress-bar { background: var(--color-info); }

@keyframes toastProgress {
  from { transform: scaleX(1); }
  to { transform: scaleX(0); }
}

.toast:hover .toast__progress-bar {
  animation-play-state: paused;
}

.toast__close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--color-text-muted);
  padding: var(--space-1);
  border-radius: var(--radius-sm);
  line-height: 1;
  flex-shrink: 0;
  transition: color var(--transition-fast), background var(--transition-fast);
}

.toast__close:hover {
  color: var(--color-text-primary);
  background: var(--color-surface-secondary);
}

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

@media (prefers-reduced-motion: reduce) {
  .toast,
  .toast.is-leaving {
    animation: none;
    opacity: 1;
    transform: none;
  }

  .toast__progress-bar {
    animation: none;
    display: none;
  }

  .toast--loading .toast__icon svg {
    animation: none;
  }
}
