<script lang="ts">
  import { Sparkles } from 'lucide-svelte';
  import type { ChangelogEntry } from '$lib/changelog';

  let { entry, onClose }: { entry: ChangelogEntry; onClose: () => void } = $props();

  const releaseDate = $derived(
    new Date(`${entry.date}T00:00:00`).toLocaleDateString(undefined, {
      year: 'numeric',
      month: 'long',
      day: 'numeric'
    })
  );
</script>

<div class="whats-new-backdrop" role="presentation" onclick={onClose}>
  <div
    class="whats-new"
    role="dialog"
    aria-modal="true"
    aria-labelledby="whats-new-title"
    tabindex="-1"
    onclick={(event) => event.stopPropagation()}
    onkeydown={(event) => {
      if (event.key === 'Escape') {
        onClose();
      }
    }}
  >
    <div class="whats-new-head">
      <span class="whats-new-mark"><Sparkles size={20} strokeWidth={1.75} /></span>
      <div>
        <p class="whats-new-kicker">What's new · v{entry.version}</p>
        <h2 id="whats-new-title">A few updates in this release</h2>
        <p class="whats-new-date">{releaseDate}</p>
      </div>
    </div>

    <ul class="whats-new-list">
      {#each entry.highlights as highlight}
        <li>{highlight}</li>
      {/each}
    </ul>

    <div class="whats-new-actions">
      <button class="whats-new-button" type="button" onclick={onClose}>Got it</button>
    </div>
  </div>
</div>

<style>
  h2,
  p {
    margin: 0;
  }

  .whats-new-backdrop {
    position: fixed;
    inset: 0;
    z-index: 80;
    display: grid;
    place-items: center;
    padding: 1rem;
    background: rgba(11, 18, 14, 0.45);
    backdrop-filter: blur(10px);
  }

  .whats-new {
    width: min(34rem, 100%);
    display: grid;
    gap: 1.15rem;
    padding: 1.5rem;
    border: 1px solid var(--color-border);
    border-radius: 1.1rem;
    background: var(--color-bg-surface);
    color: var(--color-text-primary);
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.22);
  }

  .whats-new-head {
    display: flex;
    align-items: flex-start;
    gap: 0.9rem;
  }

  .whats-new-mark {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 2.6rem;
    height: 2.6rem;
    border-radius: 0.82rem;
    color: var(--color-on-brand);
    background: var(--color-brand);
  }

  .whats-new-kicker {
    color: var(--color-brand);
    font-size: 0.74rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
  }

  .whats-new-head h2 {
    margin-top: 0.22rem;
    font-size: 1.28rem;
    letter-spacing: -0.02em;
  }

  .whats-new-date {
    margin-top: 0.18rem;
    color: var(--color-text-muted);
    font-size: 0.84rem;
  }

  .whats-new-list {
    display: grid;
    gap: 0.7rem;
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .whats-new-list li {
    position: relative;
    padding-left: 1.5rem;
    color: var(--color-text-secondary);
    line-height: 1.5;
  }

  .whats-new-list li::before {
    content: '';
    position: absolute;
    top: 0.5rem;
    left: 0.3rem;
    width: 0.46rem;
    height: 0.46rem;
    border-radius: 999px;
    background: var(--color-brand);
  }

  .whats-new-actions {
    display: flex;
    justify-content: flex-end;
  }

  .whats-new-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 2.6rem;
    padding: 0.72rem 1.3rem;
    border: 1px solid var(--color-brand);
    border-radius: var(--radius-control);
    background: var(--color-brand);
    color: var(--color-on-brand);
    font-weight: 600;
    cursor: pointer;
    transition: background-color 160ms ease, border-color 160ms ease;
  }

  .whats-new-button:hover {
    background: var(--color-brand-hover);
    border-color: var(--color-brand-hover);
  }

  .whats-new-button:focus-visible {
    outline: 3px solid color-mix(in srgb, var(--color-brand) 45%, transparent);
    outline-offset: 2px;
  }
</style>
