<div class="max-w-3xl">
    <div class="mb-6">
        <h1 class="text-2xl font-semibold text-t1">Settings</h1>
        <nav class="flex gap-1 mt-4">
            <a href="{{ route('w.settings.index', request()->route('workspace')) }}"
               class="px-3 py-1.5 text-sm rounded-lg text-t2 hover:text-t1 hover:bg-s2">Workspace</a>
            <a href="{{ route('w.settings.api-tokens', request()->route('workspace')) }}"
               class="px-3 py-1.5 text-sm rounded-lg bg-blue/10 text-blue font-medium">API Tokens</a>
            <a href="{{ route('w.settings.webhooks', request()->route('workspace')) }}"
               class="px-3 py-1.5 text-sm rounded-lg text-t2 hover:text-t1 hover:bg-s2">Webhooks</a>
        </nav>
    </div>

    {{-- New token one-time display --}}
    @if($newToken)
    <div class="mb-4 p-4 rounded-xl bg-green/10 border border-green/20">
        <div class="flex items-start justify-between gap-3">
            <div class="flex-1">
                <p class="text-sm font-medium text-green mb-2">Token created — copy it now, it won't be shown again</p>
                <code class="block font-mono text-xs text-t1 bg-s3 px-3 py-2 rounded-lg break-all">{{ $newToken }}</code>
                <div class="mt-2">
                    <x-ui.copy-button :value="$newToken" label="Copy Token" />
                </div>
            </div>
            <button wire:click="dismissNewToken" class="text-t3 hover:text-t1 flex-shrink-0"><x-heroicon-o-x-mark class="w-4 h-4" /></button>
        </div>
    </div>
    @endif

    {{-- Create token --}}
    <div class="bg-s1 border border-white/[.06] rounded-xl p-6 mb-4">
        <h3 class="text-sm font-medium text-t1 mb-4">Create New Token</h3>
        <div class="flex gap-3 max-w-sm">
            <input wire:model="tokenName" type="text" placeholder="Token name"
                class="flex-1 px-3 py-2.5 bg-s2 border border-white/[.06] rounded-lg text-sm text-t1 placeholder-t3 focus:outline-none focus:ring-1 focus:ring-blue/50">
            <button wire:click="createToken"
                class="px-4 py-2.5 bg-accent-gradient text-white text-sm font-medium rounded-lg hover:opacity-90 flex-shrink-0">
                Create
            </button>
        </div>
        @error('tokenName') <p class="mt-2 text-xs text-red">{{ $message }}</p> @enderror
    </div>

    {{-- Token list --}}
    <div class="bg-s1 border border-white/[.06] rounded-xl overflow-hidden">
        <div class="px-5 py-3 border-b border-white/[.06]">
            <span class="text-xs font-medium text-t2 uppercase tracking-wider">Active Tokens</span>
        </div>
        <div class="divide-y divide-white/[.04]">
            @forelse($tokens as $token)
            <div class="flex items-center gap-4 px-5 py-4">
                <div class="flex-1 min-w-0">
                    <div class="text-sm font-medium text-t1">{{ $token->name }}</div>
                    <div class="text-xs text-t2 mt-0.5">
                        Created {{ $token->created_at->diffForHumans() }}
                        @if($token->last_used_at) · Last used {{ $token->last_used_at->diffForHumans() }} @endif
                    </div>
                </div>
                <button wire:click="revokeToken({{ $token->id }})"
                        wire:confirm="Revoke this token?"
                        class="text-red text-xs hover:opacity-80">Revoke</button>
            </div>
            @empty
            <div class="px-5 py-10 text-center text-t3 text-sm">No API tokens yet</div>
            @endforelse
        </div>
    </div>
</div>
