<script>
    import DashboardNav from "../dashboard/DashboardNav.svelte";
    import Game from "./Game.svelte";

    let { data } = $props();
    let games = $derived(data?.data ?? data ?? []);
</script>

<div class="page">
    <DashboardNav />

    <main>
        <div class="heading">
            <a href="/dashboard" class="back">
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
                    <path d="M19 12H5M12 5l-7 7 7 7"/>
                </svg>
                Dashboard
            </a>
            <div class="heading-row">
                <h1>My Games</h1>
                <a href="/game/new" class="new-btn">
                    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
                        <path d="M12 5v14M5 12h14"/>
                    </svg>
                    New Game
                </a>
            </div>
            <p>{games.length} {games.length === 1 ? "adventure" : "adventures"}</p>
        </div>

        {#if games.length > 0}
            <div class="game-list">
                {#each games as game (game.id)}
                    <Game {game} />
                {/each}
            </div>
        {:else}
            <div class="empty">
                <div class="empty-icon">
                    <svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">
                        <path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"/>
                        <path d="M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z"/>
                    </svg>
                </div>
                <p>No adventures yet. Start your first one.</p>
            </div>
        {/if}
    </main>
</div>

<style>
    .page {
        min-height: 100vh;
        display: flex;
        flex-direction: column;
        background: #0b0b10;
    }

    main {
        flex: 1;
        max-width: 720px;
        width: 100%;
        margin: 0 auto;
        padding: 3rem 2rem 6rem;
    }

    .heading {
        margin-bottom: 2rem;
    }

    .back {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        font-size: 0.85rem;
        font-weight: 500;
        color: #6b6b7a;
        text-decoration: none;
        margin-bottom: 1.25rem;
        transition: color 0.15s;
    }

    .back:hover { color: #a1a1aa; }

    h1 {
        font-size: clamp(1.8rem, 4vw, 2.4rem);
        font-weight: 800;
        letter-spacing: -1.5px;
        color: #e5e5ea;
        margin-bottom: 0.25rem;
    }

    .heading-row {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 1rem;
        margin-bottom: 0.25rem;
    }

    .heading p {
        font-size: 0.9rem;
        color: #6b6b7a;
    }

    .new-btn {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        padding: 0.5rem 1rem;
        background: #c084fc;
        color: #0b0b10;
        font-size: 0.875rem;
        font-weight: 600;
        border-radius: 8px;
        text-decoration: none;
        white-space: nowrap;
        transition: background 0.15s;
        flex-shrink: 0;
    }

    .new-btn:hover { background: #d8b4fe; }

    .game-list {
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
    }

    .empty {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 1rem;
        padding: 5rem 2rem;
        color: #6b6b7a;
        text-align: center;
    }

    .empty-icon {
        width: 64px;
        height: 64px;
        border-radius: 16px;
        background: #0f0f18;
        border: 1px solid #1e1e28;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #3a3a50;
    }

    .empty p {
        font-size: 0.95rem;
    }

    @media (max-width: 600px) {
        main { padding: 2rem 1.25rem 4rem; }
    }
</style>
