<script lang="ts">
  import { toast, type Toast } from '$lib/toast';

  function icon(type: Toast['type']) {
    if (type === 'success') return '✓';
    if (type === 'error') return '!';
    if (type === 'loading') return null; // spinner shown separately
    return 'ℹ';
  }
</script>

{#if $toast.length}
  <div class="toast-stack" aria-live="polite" aria-atomic="false">
    {#each $toast as t (t.id)}
      <div class="toast toast--{t.type}" role={t.type === 'error' ? 'alert' : 'status'}>
        <span class="toast-icon" aria-hidden="true">
          {#if t.type === 'loading'}
            <span class="spinner"></span>
          {:else}
            {icon(t.type)}
          {/if}
        </span>
        <span class="toast-message">{t.message}</span>
        {#if t.type !== 'loading'}
          <button
            class="toast-close"
            type="button"
            aria-label="Dismiss"
            onclick={() => toast.dismiss(t.id)}
          >✕</button>
        {/if}
      </div>
    {/each}
  </div>
{/if}

<style>
  .toast-stack {
    position: fixed;
    bottom: 1.5rem;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    z-index: 9999;
    pointer-events: none;
    width: max-content;
    max-width: min(28rem, calc(100vw - 2rem));
  }

  .toast {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    padding: 0.7rem 1rem 0.7rem 0.85rem;
    border-radius: 0.75rem;
    font-size: 0.88rem;
    font-weight: 500;
    pointer-events: all;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.14);
    animation: slide-up 180ms ease both;
    line-height: 1.4;
  }

  @keyframes slide-up {
    from { opacity: 0; transform: translateY(0.5rem); }
    to   { opacity: 1; transform: translateY(0); }
  }

  .toast--info {
    background: #1a1f1c;
    color: #f0f4f1;
  }

  .toast--success {
    background: #1a3326;
    color: #a3f0c5;
    border: 1px solid #2a6645;
  }

  .toast--error {
    background: #3b1212;
    color: #fca5a5;
    border: 1px solid #7f2020;
  }

  .toast--loading {
    background: #1a1f1c;
    color: #d1e8da;
  }

  .toast-icon {
    flex-shrink: 0;
    font-size: 0.82rem;
    display: flex;
    align-items: center;
  }

  .toast--success .toast-icon { color: #4ade80; }
  .toast--error  .toast-icon { color: #f87171; }
  .toast--info   .toast-icon { color: #93c5fd; }

  .toast-message {
    flex: 1;
  }

  .toast-close {
    flex-shrink: 0;
    margin-left: 0.25rem;
    padding: 0;
    background: none;
    border: none;
    color: inherit;
    opacity: 0.5;
    font-size: 0.78rem;
    cursor: pointer;
    line-height: 1;
    transition: opacity 120ms ease;
  }

  .toast-close:hover {
    opacity: 1;
  }

  /* Loading spinner */
  .spinner {
    display: block;
    width: 0.85rem;
    height: 0.85rem;
    border: 2px solid rgba(255, 255, 255, 0.2);
    border-top-color: #d1e8da;
    border-radius: 50%;
    animation: spin 600ms linear infinite;
  }

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

  @media (max-width: 480px) {
    .toast-stack {
      bottom: 5rem;
      width: calc(100vw - 2rem);
      max-width: none;
    }
  }
</style>
