---
---

<div class="toast-container"></div>

<script>
  class ToastManager {
    static instance: ToastManager;
    container: HTMLElement | null = null;

    static getInstance() {
      if (!ToastManager.instance) {
        ToastManager.instance = new ToastManager();
      }
      return ToastManager.instance;
    }

    init() {
      if (!this.container) {
        this.container = document.querySelector('.toast-container');
        if (!this.container) {
          this.container = document.createElement('div');
          this.container.className = 'toast-container';
          document.body.insertBefore(this.container, document.body.firstChild);
        }
      }
    }

    show(message: string, type: string = 'info', duration: number = 3000) {
      this.init();
      
      const toast = document.createElement('div');
      toast.className = `toast-item toast-${type}`;
      toast.innerHTML = `
        <div class="toast-icon">
          ${type === 'success' ? '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path><polyline points="22 4 12 14.01 9 11.01"></polyline></svg>' : ''}
          ${type === 'error' ? '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"></circle><line x1="15" y1="9" x2="9" y2="15"></line><line x1="9" y1="9" x2="15" y2="15"></line></svg>' : ''}
          ${type === 'warning' ? '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 512 512"><path fill="currentColor" d="M240 176h32v176h-32zm0 208h32v32h-32z"/><path fill="currentColor" d="M274.014 16h-36.028L16 445.174V496h480v-50.826ZM464 464H48v-11.041L256 50.826l208 402.133Z"/></svg>' : ''}
          ${type === 'info' ? '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"></circle><line x1="12" y1="16" x2="12" y2="12"></line><line x1="12" y1="8" x2="12.01" y2="8"></line></svg>' : ''}
        </div>
        <span class="toast-message">${message}</span>
        <button class="toast-close" aria-label="Закрыть">
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>
        </button>
      `;

      const closeBtn = toast.querySelector('.toast-close');
      closeBtn?.addEventListener('click', () => this.remove(toast));

      this.container?.appendChild(toast);

      requestAnimationFrame(() => {
        toast.classList.add('show');
      });

      if (duration > 0) {
        setTimeout(() => this.remove(toast), duration);
      }
    }

    remove(toast: Element) {
      toast.classList.remove('show');
      toast.classList.add('hide');
      setTimeout(() => {
        toast.remove();
      }, 300);
    }
  }

  (window as any).toast = ToastManager.getInstance();
</script>

<style>
  .toast-container {
    position: fixed;
    top: 1.5rem;
    right: 1.5rem;
    z-index: 10000;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    max-width: 400px;
  }

  :global(.toast-item) {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 1rem 1.25rem;
    background: #1e293b;
    border-radius: 0.75rem;
    box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.3), 0 8px 10px -6px rgba(0, 0, 0, 0.2);
    color: #fff;
    font-size: 0.9375rem;
    font-weight: 500;
    transform: translateX(120%);
    opacity: 0;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    border-left: 4px solid;
  }

  :global(.toast-item.show) {
    transform: translateX(0);
    opacity: 1;
  }

  :global(.toast-item.hide) {
    transform: translateX(120%);
    opacity: 0;
  }

  :global(.toast-success) {
    border-left-color: #22c55e;
  }

  :global(.toast-error) {
    border-left-color: #ef4444;
  }

  :global(.toast-warning) {
    border-left-color: #f59e0b;
  }

  :global(.toast-info) {
    border-left-color: #3b82f6;
  }

  :global(.toast-item .toast-icon) {
    flex-shrink: 0;
    width: 1.25rem;
    height: 1.25rem;
  }

  :global(.toast-item .toast-icon svg) {
    width: 100%;
    height: 100%;
  }

  :global(.toast-success .toast-icon) { color: #22c55e; }
  :global(.toast-error .toast-icon) { color: #ef4444; }
  :global(.toast-warning .toast-icon) { color: #f59e0b; }
  :global(.toast-info .toast-icon) { color: #3b82f6; }

  :global(.toast-item .toast-message) {
    flex: 1;
    line-height: 1.4;
  }

  :global(.toast-item .toast-close) {
    flex-shrink: 0;
    background: none;
    border: none;
    color: #94a3b8;
    cursor: pointer;
    padding: 0.25rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 0.25rem;
    transition: all 0.2s ease;
  }

  :global(.toast-item .toast-close:hover) {
    color: #fff;
    background: rgba(255, 255, 255, 0.1);
  }

  :global(.toast-item .toast-close svg) {
    width: 1rem;
    height: 1rem;
  }

  @media (max-width: 640px) {
    .toast-container {
      left: 1rem;
      right: 1rem;
      max-width: none;
    }
  }
</style>