<script lang="ts">
  import { RefreshCw, X } from 'lucide-svelte';
  import { pwaStore } from '$lib/client/pwa.svelte';
</script>

{#if pwaStore.updateAvailable}
  <div class="toast" role="status" aria-live="polite">
    <span class="msg">Neue Kochwas-Version verfügbar</span>
    <button class="reload" onclick={() => pwaStore.reload()}>
      <RefreshCw size={16} strokeWidth={2.2} />
      <span>Neu laden</span>
    </button>
    <button class="dismiss" aria-label="Später" onclick={() => pwaStore.dismiss()}>
      <X size={16} strokeWidth={2} />
    </button>
  </div>
{/if}

<style>
  .toast {
    position: fixed;
    bottom: 1rem;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.6rem 0.85rem 0.6rem 1.1rem;
    background: #1a1a1a;
    color: white;
    border-radius: var(--pill-radius);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    z-index: 500;
    max-width: calc(100% - 2rem);
    animation: slide-up 0.3s ease-out;
    font-size: 0.92rem;
  }
  @keyframes slide-up {
    from {
      transform: translate(-50%, 130%);
      opacity: 0;
    }
    to {
      transform: translate(-50%, 0);
      opacity: 1;
    }
  }
  .msg {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .reload {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.4rem 0.85rem;
    background: #2b6a3d;
    color: white;
    border: 0;
    border-radius: var(--pill-radius);
    font-size: 0.88rem;
    cursor: pointer;
    font-weight: 600;
    flex-shrink: 0;
  }
  .reload:hover {
    background: #235532;
  }
  .dismiss {
    background: transparent;
    color: #aaa;
    border: 0;
    cursor: pointer;
    padding: 4px;
    display: inline-flex;
    align-items: center;
    border-radius: var(--pill-radius);
    flex-shrink: 0;
  }
  .dismiss:hover {
    color: white;
    background: rgba(255, 255, 255, 0.1);
  }
  @media (max-width: 420px) {
    .toast {
      left: 0.5rem;
      right: 0.5rem;
      transform: none;
      max-width: none;
      border-radius: 14px;
    }
    .msg {
      flex: 1;
      white-space: normal;
      font-size: 0.85rem;
      line-height: 1.25;
    }
    @keyframes slide-up {
      from {
        transform: translateY(130%);
        opacity: 0;
      }
      to {
        transform: translateY(0);
        opacity: 1;
      }
    }
  }
</style>
