<!-- ================= LOGIN MODAL ================= -->
<div class="modal fade login-modal" id="loginModal" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content login-box">

      <!-- CLOSE -->
      <button type="button" class="login-close" data-dismiss="modal" aria-label="Close">
        ✕
      </button>

      <!-- HEADER -->
      <div class="login-header">
        <img data-src="../assets/img/<?php echo $logo ?>" class="login-logo">
        <h3>Selamat datang kembali Bos</h3>
      </div>
      
      <div class="note-info">
        <i class="fas fa-info-circle"></i>
        <span>
          Keamanan akun adalah prioritas. Ganti kata sandi secara berkala.
        </span>
      </div>

      <!-- FORM -->
      <form name="login-form" action="function/cek_login.php" method="POST" autocomplete="off" id="loginForm">

        <!-- USERNAME -->
        <div class="login-input">
          <i class="fas fa-user"></i>
          <input type="text" name="username" minlength="1" maxlength="25" placeholder="Nama Pengguna" required>
        </div>

        <!-- PASSWORD -->
        <div class="login-input" style="position: relative;">
          <i class="fas fa-lock"></i>
          <input type="password" name="password" minlength="5" maxlength="50" id="loginPassword" placeholder="Kata Sandi" required style="padding-right: 40px;">
          
          <div class="toggle-pass-login" style="position: absolute; right: 15px; top: 50%; transform: translateY(-50%); cursor: pointer; color: #888;">
             <i class="fas fa-eye"></i>
          </div>
        </div>

        <!-- CAPTCHA (TIRUAN DARI DAFTAR) -->
        <div class="captcha-bar">
            <div class="captcha-display">
                <div class="captcha-code" id="loginCaptchaCode"><?php echo rand(100000, 999999); ?></div>
                <button type="button" class="captcha-refresh" id="loginRefreshCaptcha" title="Refresh">
                    <i class="fas fa-sync-alt"></i>
                </button>
            </div>
            <input type="number" name="captcha" id="loginCaptchaInput" placeholder="Masukkan kode di atas" required>
            <input type="hidden" name="captcha_asli" id="loginCaptchaAsli" value="">
        </div>

        <!-- OPTIONS -->
        <div class="login-options">
          <label>
            <input type="checkbox" name="remember">
            Ingat akun
          </label>
          <a href="?page=lupa-password">Lupa Kata Sandi</a>
        </div>

        <!-- SUBMIT -->
        <button type="submit" name="submit" class="login-btn">
          Masuk
        </button>

        <!-- FOOTER -->
        <div class="login-footer">
          <a href="https://direct.lc.chat/<?php echo $id_livechat ?>">Layanan Pelanggan</a>
          <p>
            Belum memiliki akun?
            <a href="?page=daftar">Daftar</a>
          </p>
        </div>

      </form>
    </div>
  </div>
</div>

<!-- CSS CAPTCHA (COPY PERSIS DARI DAFTAR) -->
<style>
.captcha-bar {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 12px;
}

.captcha-display {
    display: flex;
    gap: 10px;
    height: 48px;
}

.captcha-code {
    flex: 1;
    height: 100%;
    background: #fff;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: 'Courier New', monospace;
    font-size: 22px;
    font-weight: 700;
    letter-spacing: 8px;
    color: #111;
    user-select: none;
}

.captcha-refresh {
    width: 48px;
    height: 100%;
    border-radius: 10px;
    border: 1px solid rgba(255,255,255,0.1);
    background: rgba(255,255,255,0.06);
    color: #aaa;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}
.captcha-refresh:hover { background: rgba(255,255,255,0.1); color: #fff; }
.captcha-refresh:active { transform: scale(0.92); }
.captcha-refresh.spinning i { animation: spinR 0.4s ease; }
@keyframes spinR { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }

.captcha-bar input[type="number"] {
    width: 100%;
    height: 48px;
    background: rgba(255,255,255,0.04);
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 10px;
    padding: 0 16px;
    font-size: 14px;
    color: #fff;
    outline: none;
    transition: border-color 0.2s;
    box-sizing: border-box;
    -moz-appearance: textfield;
}
.captcha-bar input[type="number"]::-webkit-outer-spin-button,
.captcha-bar input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.captcha-bar input[type="number"]:focus { border-color: rgba(255,255,255,0.25); }
.captcha-bar input[type="number"]::placeholder { color: #555; }
</style>

<!-- SCRIPT LOGIN -->
<script>
    /* Toggle Password */
    const toggleLogin = document.querySelector('.toggle-pass-login');
    if(toggleLogin) {
        toggleLogin.addEventListener('click', function() {
            const input = document.getElementById('loginPassword');
            const icon = this.querySelector('i');
            if (input.type === 'password') {
                input.type = 'text';
                icon.classList.replace('fa-eye', 'fa-eye-slash');
            } else {
                input.type = 'password';
                icon.classList.replace('fa-eye-slash', 'fa-eye');
            }
        });
    }

    /* Captcha Login (Tiruan dari daftar) */
    function generateLoginCaptcha() {
        const code = Math.floor(100000 + Math.random() * 900000).toString();
        document.getElementById('loginCaptchaCode').textContent = code;
        document.getElementById('loginCaptchaAsli').value = code;
        document.getElementById('loginCaptchaInput').value = '';
    }

    const loginRefreshBtn = document.getElementById('loginRefreshCaptcha');
    if(loginRefreshBtn) {
        loginRefreshBtn.addEventListener('click', function() {
            this.classList.add('spinning');
            setTimeout(() => { generateLoginCaptcha(); this.classList.remove('spinning'); }, 200);
        });
    }

    generateLoginCaptcha();

    const loginFormEl = document.getElementById('loginForm');
    if(loginFormEl) {
        loginFormEl.addEventListener('submit', function() {
            document.getElementById('loginCaptchaAsli').value = document.getElementById('loginCaptchaCode').textContent;
        });
    }
</script>