<!-- Login Modal -->
<div id="loginModal" class="fixed inset-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full z-50 hidden">
    <div class="relative top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white">
        <!-- Modal Header -->
        <div class="flex justify-between items-center pb-3">
            <h3 class="text-lg font-semibold text-gray-900">অ্যাকাউন্টে প্রবেশ করুন</h3>
            <button type="button" class="text-gray-400 hover:text-gray-600" onclick="closeLoginModal()">
                <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="M6 18L18 6M6 6l12 12"></path>
                </svg>
            </button>
        </div>

        <!-- Modal Content -->
        <div class="mt-3">
            <!-- Error Messages -->
            <div id="loginErrors" class="mb-4 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg hidden">
                <ul id="loginErrorsList" class="list-disc list-inside"></ul>
            </div>

            <!-- Success Messages -->
            <div id="loginSuccess" class="mb-4 p-4 bg-green-100 border border-green-400 text-green-700 rounded-lg hidden">
                <span id="loginSuccessMessage"></span>
            </div>

            <!-- Step 1: Phone Input -->
            <div id="loginPhoneStep">
                <form id="loginForm">
                    @csrf
                    
                    <!-- Phone Number -->
                    <div class="mb-4">
                        <label for="login_phone" class="block text-sm font-medium text-gray-700 mb-2">ফোন নম্বর</label>
                        <input type="tel" id="login_phone" name="phone" 
                               class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                               placeholder="01XXXXXXXXX" required inputmode="numeric" maxlength="11">
                    </div>

                    <!-- Submit Button -->
                    <div class="flex items-center justify-between">
                        <button type="submit" id="loginSubmitBtn"
                                class="w-full bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 transition-colors">
                            <span id="loginBtnText">লগইন</span>
                            <span id="loginBtnLoading" class="hidden">
                                <svg class="animate-spin -ml-1 mr-3 h-5 w-5 text-white inline" 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"></circle>
                                    <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                                </svg>
                                প্রক্রিয়াকরণ হচ্ছে...
                            </span>
                        </button>
                    </div>
                </form>
            </div>

            <!-- Step 2: OTP Options -->
            <div id="loginChoiceStep" class="hidden">
                <!-- Instructional Message -->
                <div class="mb-4 p-4 bg-blue-50 border border-blue-200 rounded-lg">
                    <p class="text-sm text-blue-800 leading-relaxed">
                        আপনার সর্বশেষ যাচাইকৃত <strong>OTP</strong> এখন আপনার বর্তমান পাসওয়ার্ড হিসেবে কাজ করবে। 
                        লগইন বা অর্ডার নিশ্চিতকরণের জন্য নতুন কোড অনুরোধ না করে সেটি ব্যবহার করতে পারেন।
                    </p>
                </div>

                <p class="text-sm text-gray-600 mb-4">
                    আপনি নিচের যেকোনো একটি বিকল্প বেছে নিতে পারেন:
                </p>

                <!-- Error Messages -->
                <div id="loginChoiceErrors" class="mb-4 p-3 bg-red-100 border border-red-400 text-red-700 rounded-lg hidden">
                    <ul id="loginChoiceErrorsList" class="list-disc list-inside text-sm"></ul>
                </div>

                <!-- Options -->
                <div class="space-y-3">
                    <!-- Option 1: Verify by Last OTP -->
                    <button type="button" id="verifyLastOtpBtn" onclick="useLastOtp()"
                            class="w-full flex items-center p-4 border-2 border-blue-200 rounded-lg hover:bg-blue-50 hover:border-blue-400 transition-colors text-left">
                        <div class="flex-shrink-0 mr-4">
                            <div class="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center">
                                <svg class="w-5 h-5 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"></path>
                                </svg>
                            </div>
                        </div>
                        <div>
                            <div class="font-medium text-gray-900">শেষ OTP দিয়ে যাচাই করুন</div>
                            <div class="text-xs text-gray-500 mt-0.5">আপনার আগের যাচাইকৃত কোড ব্যবহার করুন</div>
                        </div>
                    </button>

                    <!-- Option 2: Send new OTP -->
                    <button type="button" id="sendNewOtpBtn" onclick="sendNewOtp()"
                            class="w-full flex items-center p-4 border-2 border-green-200 rounded-lg hover:bg-green-50 hover:border-green-400 transition-colors text-left">
                        <div class="flex-shrink-0 mr-4">
                            <div class="w-10 h-10 bg-green-100 rounded-full flex items-center justify-center">
                                <svg class="w-5 h-5 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8"></path>
                                </svg>
                            </div>
                        </div>
                        <div>
                            <div class="font-medium text-gray-900">নতুন OTP পাঠান</div>
                            <div class="text-xs text-gray-500 mt-0.5">মোবাইলে নতুন কোড পাঠান</div>
                        </div>
                        <span id="sendNewOtpLoading" class="hidden ml-auto">
                            <svg class="animate-spin -ml-1 mr-3 h-5 w-5 text-green-600 inline" 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"></circle>
                                <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                            </svg>
                        </span>
                    </button>
                </div>
            </div>

            <!-- Step 3a: Verify by Last OTP -->
            <div id="loginLastOtpStep" class="hidden">
                <!-- Phone Display -->
                <div class="mb-4 p-3 bg-gray-50 border border-gray-200 rounded-lg">
                    <p class="text-sm text-gray-600">ফোন নম্বর:</p>
                    <p id="lastOtpPhoneDisplay" class="text-lg font-semibold text-gray-900"></p>
                </div>

                <!-- Instructional Message -->
                <div class="mb-4 p-4 bg-blue-50 border border-blue-200 rounded-lg">
                    <p class="text-sm text-blue-800 leading-relaxed">
                        আপনার সর্বশেষ যাচাইকৃত <strong>OTP</strong> এখন আপনার পাসওয়ার্ড হিসেবে সংরক্ষিত আছে। 
                        নিচে সেই কোডটি লিখুন এবং লগইন করুন।
                    </p>
                </div>

                <!-- Error Messages -->
                <div id="loginLastOtpErrors" class="mb-4 p-3 bg-red-100 border border-red-400 text-red-700 rounded-lg hidden">
                    <ul id="loginLastOtpErrorsList" class="list-disc list-inside text-sm"></ul>
                </div>

                <!-- OTP Form -->
                <form id="loginLastOtpForm" class="space-y-4">
                    @csrf
                    <input type="hidden" id="lastOtpPhone" name="phone" value="">
                    
                    <div>
                        <label for="lastOtpCode" class="block text-sm font-medium text-gray-700 mb-2">পাসওয়ার্ড / OTP</label>
                        <input type="text" id="lastOtpCode" name="password"
                               inputmode="numeric" autocomplete="one-time-code"
                               class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent text-center text-2xl tracking-widest"
                               placeholder="0000" maxlength="4" required>
                    </div>

                    <div class="flex space-x-3">
                        <button type="button" onclick="backToChoice()"
                                class="flex-1 bg-gray-200 text-gray-700 px-4 py-2 rounded-md hover:bg-gray-300 transition-colors">
                            ফিরে যান
                        </button>
                        <button type="submit" id="lastOtpSubmitBtn"
                                class="flex-1 bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 transition-colors flex items-center justify-center">
                            <span id="lastOtpBtnText">যাচাই করুন</span>
                            <span id="lastOtpBtnLoading" class="hidden ml-2">
                                <svg class="animate-spin -ml-1 mr-3 h-5 w-5 text-white inline" 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"></circle>
                                    <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                                </svg>
                                যাচাই হচ্ছে...
                            </span>
                        </button>
                    </div>
                </form>
            </div>

            <!-- Step 3b: Send New OTP -->
            <div id="loginNewOtpStep" class="hidden">
                <!-- Instructional Message -->
                <div class="mb-4 p-4 bg-green-50 border border-green-200 rounded-lg">
                    <p class="text-sm text-green-800 leading-relaxed">
                        নতুন যাচাইকরণ কোড পাঠানো হয়েছে। নিচে কোডটি লিখুন অথবা SMS থেকে অটো-ফিল করুন।
                    </p>
                </div>

                <!-- Error Messages -->
                <div id="loginNewOtpErrors" class="mb-4 p-3 bg-red-100 border border-red-400 text-red-700 rounded-lg hidden">
                    <ul id="loginNewOtpErrorsList" class="list-disc list-inside text-sm"></ul>
                </div>

                <!-- OTP Form -->
                <form id="loginNewOtpForm" class="space-y-4">
                    @csrf
                    <input type="hidden" id="newOtpPhone" name="phone" value="">
                    <input type="hidden" id="newOtpType" name="type" value="auth_verification">
                    
                    <div>
                        <label for="newOtpCode" class="block text-sm font-medium text-gray-700 mb-2">যাচাইকরণ কোড</label>
                        <input type="text" id="newOtpCode" name="otp"
                               inputmode="numeric" autocomplete="one-time-code"
                               class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent text-center text-2xl tracking-widest"
                               placeholder="0000" maxlength="4" required>
                    </div>

                    <div class="flex space-x-3">
                        <button type="button" onclick="backToChoice()"
                                class="flex-1 bg-gray-200 text-gray-700 px-4 py-2 rounded-md hover:bg-gray-300 transition-colors">
                            ফিরে যান
                        </button>
                        <button type="submit" id="newOtpSubmitBtn"
                                class="flex-1 bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 transition-colors flex items-center justify-center">
                            <span id="newOtpBtnText">কোড যাচাই করুন</span>
                            <span id="newOtpBtnLoading" class="hidden ml-2">
                                <svg class="animate-spin -ml-1 mr-3 h-5 w-5 text-white inline" 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"></circle>
                                    <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                                </svg>
                                যাচাই হচ্ছে...
                            </span>
                        </button>
                    </div>

                    <div class="text-center mt-4">
                        <button type="button" id="resendOtpBtn" onclick="resendOtpFromChoice()"
                                class="text-blue-600 hover:text-blue-500 text-sm font-medium">
                            কোড পুনরায় পাঠান
                        </button>
                        <p id="resendTimer" class="text-sm text-gray-500 mt-1 hidden">
                            <span id="timerCount">60</span> সেকেন্ডের মধ্যে আবার পাঠানো যাবে
                        </p>
                    </div>
                </form>
            </div>

            <!-- Register Link -->
            <div class="mt-4 text-center">
                <p class="text-sm text-gray-600">
                    অ্যাকাউন্ট নেই? 
                    <a href="#" onclick="switchToRegisterModal()" class="text-blue-600 hover:text-blue-500 font-medium">এখনে নিবন্ধন করুন</a>
                </p>
            </div>
        </div>
    </div>
</div>
