<div x-data="accountManager" class="view-container">
    <!-- Compact Header -->
    <div class="flex items-center justify-between gap-4 mb-6">
        <!-- Title with inline subtitle -->
        <div class="flex flex-wrap items-center gap-4">
            <h1 class="text-2xl font-bold text-white tracking-tight" x-text="$store.global.t('accountManagement')">
                Account Management
            </h1>
            <div class="flex items-center h-6 px-3 rounded-full bg-space-800/80 border border-space-border/50 shadow-sm backdrop-blur-sm">
                <span class="text-[10px] font-mono text-gray-400 uppercase tracking-wider"
                      x-text="$store.global.t('manageTokens')">
                    Manage Google Account tokens and authorization states
                </span>
            </div>
        </div>

        <!-- Action Buttons -->
        <div class="flex items-center gap-2">
            <!-- Search -->
            <div class="relative" x-show="$store.data.accounts.length > 0">
                <input type="text"
                       x-model="searchQuery"
                       :placeholder="$store.global.t('searchAccounts')"
                       :aria-label="$store.global.t('searchAccounts')"
                       class="input-search-sm w-48 pl-9 h-8"
                       @keydown.escape="searchQuery = ''">
                <svg class="w-4 h-4 absolute left-3 top-2 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
                </svg>
            </div>
            <button class="btn btn-xs btn-outline border-space-border text-gray-400 hover:text-white transition-all gap-2 h-8"
                @click="reloadAccounts()"
                :disabled="reloading">
                <svg xmlns="http://www.w3.org/2000/svg"
                     class="w-3.5 h-3.5 transition-transform"
                     :class="{ 'animate-spin': reloading }"
                     fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
                </svg>
                <span x-text="$store.global.t('reload')">Reload</span>
            </button>
            <button class="btn bg-neon-purple hover:bg-purple-600 border-none text-white btn-xs gap-2 shadow-lg shadow-neon-purple/20 h-8"
                onclick="document.getElementById('add_account_modal').showModal()">
                <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
                </svg>
                <span x-text="$store.global.t('addAccount')">Add Account</span>
            </button>
        </div>
    </div>

    <!-- Table Card -->
    <div class="view-card !p-0">
        <table class="w-full">
            <thead x-show="$store.data.accounts.length > 0">
                <tr class="bg-space-900/50 border-b border-space-border/50">
                    <th class="pl-6 py-3 text-left text-[10px] font-bold text-gray-500 uppercase tracking-wider w-16" x-text="$store.global.t('enabled')">Enabled</th>
                    <th class="py-3 text-left text-[10px] font-bold text-gray-500 uppercase tracking-wider flex-1 min-w-[200px]" x-text="$store.global.t('accountEmail')">Account (Email)</th>
                    <th class="py-3 text-left text-[10px] font-bold text-gray-500 uppercase tracking-wider w-20" x-text="$store.global.t('source')">Source</th>
                    <th class="py-3 text-left text-[10px] font-bold text-gray-500 uppercase tracking-wider w-16">Tier</th>
                    <th class="py-3 text-left text-[10px] font-bold text-gray-500 uppercase tracking-wider w-32">Quota</th>
                    <th class="py-3 text-left text-[10px] font-bold text-gray-500 uppercase tracking-wider w-24" x-text="$store.global.t('health')">Health</th>
                    <th class="py-3 pr-6 text-right text-[10px] font-bold text-gray-500 uppercase tracking-wider w-32" x-text="$store.global.t('operations')">Operations</th>
                </tr>
            </thead>
            <tbody>
                <!-- Empty State -->
                <template x-if="$store.data.accounts.length === 0">
                    <tr>
                        <td colspan="6" class="py-16 text-center">
                            <div class="flex flex-col items-center gap-4 max-w-lg mx-auto">
                                <svg class="w-20 h-20 text-gray-700" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"
                                          d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z" />
                                </svg>
                                <h3 class="text-xl font-semibold text-gray-400" x-text="$store.global.t('noAccountsYet')">
                                    No Accounts Yet
                                </h3>
                                <p class="text-sm text-gray-600 max-w-md leading-relaxed" x-text="$store.global.t('noAccountsDesc')">
                                    Get started by adding a Google account via OAuth, or use the CLI command to import credentials.
                                </p>
                                <div class="flex items-center gap-4 mt-2">
                                    <button class="btn bg-neon-purple hover:bg-purple-600 border-none text-white btn-sm gap-2 shadow-lg shadow-neon-purple/20"
                                        onclick="document.getElementById('add_account_modal').showModal()">
                                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
                                        </svg>
                                        <span x-text="$store.global.t('addFirstAccount')">Add Your First Account</span>
                                    </button>
                                    <span class="text-xs text-gray-600" x-text="$store.global.t('or')">or</span>
                                    <div class="text-xs font-mono bg-space-800 px-3 py-2 rounded border border-space-border text-gray-400">
                                        npm run accounts:add
                                    </div>
                                </div>
                            </div>
                        </td>
                    </tr>
                </template>

                <!-- Account Rows -->
                <template x-for="acc in filteredAccounts" :key="acc.email">
                    <tr class="border-b border-space-border/30 last:border-0 hover:bg-white/5 transition-colors group">
                        <td class="pl-6 py-4">
                            <label class="relative inline-flex items-center cursor-pointer">
                                <input type="checkbox" class="sr-only peer"
                                    :checked="acc.enabled !== false"
                                    @change="toggleAccount(acc.email, $el.checked)">
                                <div class="w-9 h-5 bg-space-800 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:start-[2px] after:bg-gray-600 after:rounded-full after:h-4 after:w-4 after:transition-all peer-checked:bg-neon-green peer-checked:after:bg-white"></div>
                            </label>
                        </td>
                        <td class="py-4">
                            <div class="tooltip tooltip-right" :data-tip="acc.email">
                                <span class="font-mono text-sm text-gray-300 truncate max-w-[320px] inline-block cursor-help group-hover:text-white transition-colors"
                                      x-text="formatEmail(acc.email)">
                                </span>
                            </div>
                        </td>
                        <td class="py-4">
                            <span :class="acc.source === 'oauth' ? 'status-pill-purple' : 'status-pill-free'"
                                  x-text="acc.source || 'oauth'">
                            </span>
                        </td>
                        <td class="py-4">
                            <span :class="{
                                      'status-pill-ultra': acc.subscription?.tier === 'ultra',
                                      'status-pill-pro': acc.subscription?.tier === 'pro',
                                      'status-pill-free': !acc.subscription || acc.subscription.tier === 'free' || acc.subscription.tier === 'unknown'
                                  }"
                                  x-text="(acc.subscription?.tier || 'free').toUpperCase()">
                            </span>
                        </td>
                        <td class="py-4">
                            <div x-data="{ quota: getMainModelQuota(acc) }">
                                <template x-if="quota.percent !== null">
                                    <div class="flex items-center gap-2">
                                        <div class="w-16 bg-gray-700 rounded-full h-2 overflow-hidden">
                                            <div class="h-full rounded-full transition-all"
                                                 :class="{
                                                     'bg-neon-green': quota.percent > 50,
                                                     'bg-yellow-500': quota.percent > 20 && quota.percent <= 50,
                                                     'bg-red-500': quota.percent <= 20
                                                 }"
                                                 :style="`width: ${quota.percent}%`">
                                            </div>
                                        </div>
                                        <span class="text-xs font-mono text-gray-400" x-text="quota.percent + '%'"></span>
                                    </div>
                                </template>
                                <template x-if="quota.percent === null">
                                    <span class="text-xs text-gray-600">-</span>
                                </template>
                            </div>
                        </td>
                        <td class="py-4">
                            <div class="flex items-center gap-2">
                                <div class="w-2 h-2 rounded-full flex-shrink-0"
                                     :class="acc.status === 'ok' ?
                                             'bg-neon-green shadow-[0_0_8px_rgba(34,197,94,0.6)] animate-pulse' :
                                             'bg-red-500 shadow-[0_0_8px_rgba(239,68,68,0.6)]'">
                                </div>
                                <span class="text-xs font-mono font-semibold"
                                      :class="acc.status === 'ok' ? 'text-neon-green' : 'text-red-400'"
                                      x-text="acc.status.toUpperCase()">
                                </span>
                            </div>
                        </td>
                        <td class="py-4 pr-6">
                            <div class="flex justify-end gap-2">
                                <!-- Fix Button -->
                                <button x-show="acc.status === 'invalid'"
                                    class="px-3 py-1 text-[10px] font-bold font-mono uppercase tracking-wider rounded bg-yellow-500/10 text-yellow-400 hover:bg-yellow-500/20 border border-yellow-500/30 hover:border-yellow-500/50 transition-all"
                                    @click="fixAccount(acc.email)"
                                    x-text="$store.global.t('fix')">
                                    FIX
                                </button>
                                <button class="p-2 rounded hover:bg-white/10 text-gray-500 hover:text-white transition-colors"
                                    @click="refreshAccount(acc.email)"
                                    :disabled="refreshing"
                                    :title="$store.global.t('refreshData')">
                                    <svg class="w-4 h-4 transition-transform"
                                         :class="{ 'animate-spin': refreshing }"
                                         fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                            d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
                                    </svg>
                                </button>
                                <button
                                    class="p-2 rounded transition-colors"
                                    :class="acc.source === 'database' ? 'text-gray-700 cursor-not-allowed' : 'hover:bg-red-500/10 text-gray-500 hover:text-red-400'"
                                    :disabled="acc.source === 'database'"
                                    @click="acc.source !== 'database' && confirmDeleteAccount(acc.email)"
                                    :title="acc.source === 'database' ? $store.global.t('cannotDeleteDatabase') : $store.global.t('delete')">
                                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                            d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
                                    </svg>
                                </button>
                            </div>
                        </td>
                    </tr>
                </template>

                <!-- No Search Results -->
                <template x-if="$store.data.accounts.length > 0 && filteredAccounts.length === 0">
                    <tr>
                        <td colspan="6" class="py-12 text-center">
                            <div class="flex flex-col items-center gap-3">
                                <svg class="w-12 h-12 text-gray-700" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
                                </svg>
                                <p class="text-sm text-gray-600" x-text="$store.global.t('noSearchResults')">No accounts match your search</p>
                                <button class="btn-action-ghost !text-gray-500" @click="searchQuery = ''" x-text="$store.global.t('clearSearch')">Clear Search</button>
                            </div>
                        </td>
                    </tr>
                </template>
            </tbody>
        </table>
    </div>

    <!-- Account Status Info -->
    <div x-show="$store.data.accounts.length > 0" class="mt-4 px-6 py-3 bg-space-900/20 rounded-lg border border-space-border/20">
        <p class="text-xs text-gray-600 flex items-center gap-2">
            <svg class="w-4 h-4 text-gray-500 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
            </svg>
            <span x-html="$store.global.t('disabledAccountsNote')"></span>
        </p>
    </div>

    <!-- Delete Confirmation Modal -->
    <dialog id="delete_account_modal" class="modal">
        <div class="modal-box bg-space-900 border-2 border-red-500/50">
            <h3 class="font-bold text-lg text-red-400 flex items-center gap-2">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
                </svg>
                <span x-text="$store.global.t('dangerousOperation')">Dangerous Operation</span>
            </h3>
            <p class="py-4 text-gray-300">
                <span x-text="$store.global.t('confirmDeletePrompt')">Are you sure you want to delete account</span>
                <strong class="text-white font-mono" x-text="deleteTarget"></strong>?
            </p>
            <div class="bg-red-500/10 border border-red-500/30 rounded p-3 mb-4">
                <p class="text-sm text-red-300 flex items-start gap-2">
                    <svg class="w-5 h-5 flex-shrink-0 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
                    </svg>
                    <span x-text="$store.global.t('deleteWarning')">This action cannot be undone. All configuration and historical records will be permanently deleted.</span>
                </p>
            </div>
            <div class="modal-action">
                <button class="btn btn-ghost text-gray-400" onclick="document.getElementById('delete_account_modal').close()" x-text="$store.global.t('cancel')">
                    Cancel
                </button>
                <button class="btn bg-red-500 hover:bg-red-600 border-none text-white"
                        @click="executeDelete()"
                        :disabled="deleting"
                        :class="{ 'loading': deleting }">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24" x-show="!deleting">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
                    </svg>
                    <span x-text="$store.global.t('confirmDelete')">Confirm Delete</span>
                </button>
            </div>
        </div>
        <form method="dialog" class="modal-backdrop">
            <button>close</button>
        </form>
    </dialog>
</div>