<x-layouts.guest :title="__('Log In')">
    <div>
        <h2 class="text-2xl font-bold text-ink">{{ __('Welcome back') }}</h2>
        <p class="mt-1 text-sm text-muted">{{ __('Log in to your :app account', ['app' => config('app.name')]) }}</p>
    </div>

    {{-- Flash error from SocialLoginController. Without this, OAuth
         failures (missing client_id, Socialite exceptions, redirect URI
         mismatch on callback) silently bounced the user back to /login
         with the error stuffed in the session but never displayed —
         making the Microsoft / Google buttons look like they did
         nothing. Showing the message here makes the failure obvious. --}}
    @if(session('error'))
        <div class="mt-4 p-3 rounded-xl bg-danger/10 border border-danger/20 text-sm text-danger" role="alert">
            <div class="flex items-start gap-2">
                <svg class="w-4 h-4 mt-0.5 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
                </svg>
                <span>{{ session('error') }}</span>
            </div>
        </div>
    @endif

    @if(session('status'))
        <div class="mt-4 p-3 rounded-xl bg-success/10 border border-success/20 text-sm text-success" role="status">
            {{ session('status') }}
        </div>
    @endif

    <form method="POST" action="{{ route('login') }}" class="mt-6 space-y-4"
          x-data="{ submitting: false }" @submit="submitting = true">
        @csrf

        {{-- Email --}}
        <div>
            <label for="email" class="block text-sm font-medium text-muted mb-1">{{ __('Email address') }}</label>
            <input id="email" type="email" name="email" value="{{ old('email') }}" required autofocus
                   autocomplete="email"
                   class="input @error('email') !border-danger !ring-danger/20 @enderror"
                   placeholder="{{ __('you@company.com') }}">
            @error('email') <p class="mt-1 text-xs text-danger">{{ $message }}</p> @enderror
        </div>

        {{-- Password --}}
        <div>
            <div class="flex items-center justify-between mb-1">
                <label for="password" class="block text-sm font-medium text-muted">{{ __('Password') }}</label>
                <a href="{{ route('password.request') }}" class="text-sm text-brand hover:text-brand-strong font-medium">{{ __('Forgot password?') }}</a>
            </div>
            <div x-data="{ show: false }" class="relative">
                <input id="password" :type="show ? 'text' : 'password'" name="password" required
                       autocomplete="current-password"
                       class="input pr-10"
                       placeholder="{{ __('Enter your password') }}">
                <button type="button" @click="show = !show" class="absolute right-3 top-1/2 -translate-y-1/2 text-muted hover:text-ink">
                    <svg x-show="!show" class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/></svg>
                    <svg x-show="show" class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13.875 18.825A10.05 10.05 0 0112 19c-4.478 0-8.268-2.943-9.543-7a9.97 9.97 0 011.563-3.029m5.858.908a3 3 0 114.243 4.243M9.878 9.878l4.242 4.242M9.88 9.88l-3.29-3.29m7.532 7.532l3.29 3.29M3 3l3.59 3.59m0 0A9.953 9.953 0 0112 5c4.478 0 8.268 2.943 9.543 7a10.025 10.025 0 01-4.132 5.411m0 0L21 21"/></svg>
                </button>
            </div>
        </div>

        {{-- Remember me --}}
        <div class="flex items-center">
            <input id="remember" type="checkbox" name="remember" class="w-4 h-4 rounded border-border text-brand focus:ring-brand">
            <label for="remember" class="ml-2 text-sm text-muted">{{ __('Remember me') }}</label>
        </div>

        {{-- reCAPTCHA — rendered ONLY when admin has both captcha_enabled=true
             AND filled in both keys (App\Support\Captcha::isEnabled()).
             The widget posts a `g-recaptcha-response` hidden field which
             FortifyServiceProvider::authenticateUsing() verifies against
             Google's /siteverify before the user lookup runs. --}}
        @if(\App\Support\Captcha::isEnabled())
            <div class="flex justify-center">
                <div class="g-recaptcha" data-sitekey="{{ \App\Support\Captcha::siteKey() }}"></div>
            </div>
            <script src="https://www.google.com/recaptcha/api.js" async defer></script>
            @error('email')
                @if(str_contains($message, 'CAPTCHA') || str_contains($message, 'captcha'))
                    <p class="text-danger text-xs text-center">{{ $message }}</p>
                @endif
            @enderror
        @endif

        {{-- Submit --}}
        <button type="submit" :disabled="submitting"
                class="btn-primary w-full py-2.5"
                :class="submitting && 'opacity-75 cursor-not-allowed'">
            <span x-show="!submitting">{{ __('Log In') }}</span>
            <span x-show="submitting" x-cloak class="inline-flex items-center justify-center gap-2">
                <svg class="w-4 h-4 animate-spin" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"/></svg>
                {{ __('Signing in...') }}
            </span>
        </button>

        @php
            // Master kill-switch — Admin → Settings → Auth → "Allow social login".
            // When OFF, none of the per-provider toggles below render.
            $__socialEnabled = \App\Models\SystemSetting::get('social_login_enabled', 'true') === 'true';
            $__registrationEnabled = \App\Models\SystemSetting::get('registration_enabled', 'true') === 'true';

            // Per-provider toggles — same keys the admin Integrations page
            // writes. Default OFF (`'false'`) so a provider is only shown
            // when the admin has both flipped it ON AND saved OAuth keys.
            // Without per-provider gating customers were seeing the GitHub
            // button on /login even after disabling the GitHub integration
            // in admin — clicking it bounced to "GitHub sign-in is not
            // configured" because the keys were never entered.
            $__googleEnabled    = \App\Models\SystemSetting::get('integration_google_enabled',    'false') === 'true';
            $__microsoftEnabled = \App\Models\SystemSetting::get('integration_microsoft_enabled', 'false') === 'true';
            $__githubEnabled    = \App\Models\SystemSetting::get('integration_github_enabled',    'false') === 'true';
            $__facebookEnabled  = \App\Models\SystemSetting::get('integration_facebook_enabled',  'false') === 'true';

            // Only render the social section if at least ONE provider is
            // actually live. Saves an empty "Or continue with" divider
            // when the admin disabled all of them.
            $__anySocial = $__socialEnabled && ($__googleEnabled || $__microsoftEnabled || $__githubEnabled || $__facebookEnabled);
        @endphp

        @if($__anySocial)
        {{-- Divider --}}
        <div class="relative my-4">
            <div class="absolute inset-0 flex items-center"><div class="w-full border-t border-border"></div></div>
            <div class="relative flex justify-center text-xs"><span class="px-3 bg-surface-2 text-muted">{{ __('Or continue with') }}</span></div>
        </div>

        @if($__googleEnabled || $__microsoftEnabled)
        {{-- Social login buttons — 2 in a row when both Google + MS are on, else full-width --}}
        <div class="grid {{ $__googleEnabled && $__microsoftEnabled ? 'grid-cols-2' : 'grid-cols-1' }} gap-3">
            @if($__googleEnabled)
            <a href="{{ url('/auth/google/redirect') }}" class="flex items-center justify-center gap-2 py-2.5 px-3 bg-surface-2 border border-border rounded-xl text-sm font-medium text-ink hover:bg-surface transition-colors">
                <svg class="w-4 h-4" viewBox="0 0 24 24"><path d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92a5.06 5.06 0 01-2.2 3.32v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.1z" fill="#4285F4"/><path d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z" fill="#34A853"/><path d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z" fill="#FBBC05"/><path d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z" fill="#EA4335"/></svg>
                {{ __('Google') }}
            </a>
            @endif
            @if($__microsoftEnabled)
            <a href="{{ url('/auth/microsoft/redirect') }}" class="flex items-center justify-center gap-2 py-2.5 px-3 bg-surface-2 border border-border rounded-xl text-sm font-medium text-ink hover:bg-surface transition-colors">
                <svg class="w-4 h-4" viewBox="0 0 24 24"><rect x="1" y="1" width="10" height="10" fill="#F25022"/><rect x="13" y="1" width="10" height="10" fill="#7FBA00"/><rect x="1" y="13" width="10" height="10" fill="#00A4EF"/><rect x="13" y="13" width="10" height="10" fill="#FFB900"/></svg>
                {{ __('Microsoft') }}
            </a>
            @endif
        </div>
        @endif
        @if($__githubEnabled)
        <a href="{{ url('/auth/github/redirect') }}" class="flex items-center justify-center gap-2 w-full py-2.5 px-3 bg-surface-2 border border-border rounded-xl text-sm font-medium text-ink hover:bg-surface transition-colors">
            <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 24 24"><path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z"/></svg>
            {{ __('Continue with GitHub') }}
        </a>
        @endif
        @if($__facebookEnabled)
        <a href="{{ url('/auth/facebook/redirect') }}" class="flex items-center justify-center gap-2 w-full py-2.5 px-3 bg-surface-2 border border-border rounded-xl text-sm font-medium text-ink hover:bg-surface transition-colors mt-3">
            <svg class="w-4 h-4" viewBox="0 0 24 24" fill="#1877F2"><path d="M24 12.073c0-6.627-5.373-12-12-12s-12 5.373-12 12c0 5.99 4.388 10.954 10.125 11.854v-8.385H7.078v-3.47h3.047V9.43c0-3.007 1.792-4.669 4.533-4.669 1.312 0 2.686.235 2.686.235v2.953H15.83c-1.491 0-1.956.925-1.956 1.874v2.25h3.328l-.532 3.47h-2.796v8.385C19.612 23.027 24 18.062 24 12.073z"/></svg>
            {{ __('Continue with Facebook') }}
        </a>
        @endif
        @endif

        @if($__registrationEnabled)
        {{-- Register link --}}
        <p class="text-center text-sm text-muted mt-4">
            {{ __("Don't have an account?") }} <a href="{{ route('register') }}" class="text-brand font-semibold hover:text-brand-strong">{{ __('Sign up') }}</a>
        </p>
        @endif
    </form>
</x-layouts.guest>
