<?php
// দ্বিতীয় কোডের সকল লজিক এখানে ইনসার্ট করা হয়েছে
$cookie_email = !empty(get_cookie("c_cookie_email")) ? encrypt_decode(get_cookie("c_cookie_email")) : "";
$cookie_pass = !empty(get_cookie("c_cookie_pass")) ? encrypt_decode(get_cookie("c_cookie_pass")) : "";
$redirect = session('ref_url') ?? user_url();
?>

<!-- স্টাইল এবং ডিজাইন (প্রথম কোড অনুযায়ী) -->
<style>
    body {
        background: #ffffff;
        min-height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
        margin: 0;
        padding: 20px;
        box-sizing: border-box;
        position: relative;
        overflow-x: hidden;
    }

    body::before {
        content: '';
        position: absolute;
        width: 300px;
        height: 300px;
        background: linear-gradient(135deg, #007aff, #00fbff);
        filter: blur(100px);
        border-radius: 50%;
        top: 5%;
        left: 5%;
        z-index: -1;
        opacity: 0.2;
    }

    .glow-wrap {
        position: relative;
        padding: 3px;
        border-radius: 28px;
        background: #f0f0f0;
        overflow: hidden;
        animation: floating 6s ease-in-out infinite;
        box-shadow: 0 20px 50px rgba(0,0,0,0.1);
        width: 100%;
        max-width: 380px;
    }

    .glow-wrap::before {
        content: '';
        position: absolute;
        width: 150%;
        height: 150%;
        background: conic-gradient(transparent, transparent, transparent, #007aff);
        animation: rotate 4s linear infinite;
        top: -25%;
        left: -25%;
    }

    @keyframes rotate {
        100% { transform: rotate(360deg); }
    }

    @keyframes floating {
        0%, 100% { transform: translateY(0px); }
        50% { transform: translateY(-15px); }
    }

    .auth-card {
        background: rgba(255, 255, 255, 0.95);
        backdrop-filter: blur(20px);
        border-radius: 25px;
        padding: 30px;
        width: 100%;
        text-align: center;
        position: relative;
        z-index: 1;
        box-sizing: border-box;
    }

    .site-logo {
        width: 55px;
        margin-bottom: 10px;
    }

    .form-control {
        border-radius: 12px;
        padding: 10px 14px;
        border: 1px solid #f0f0f0;
        width: 100%;
        margin-top: 5px;
        box-sizing: border-box;
        background: #f9f9f9;
        font-size: 13px;
        transition: 0.2s ease;
    }

    .form-control:focus {
        outline: none;
        border-color: #007aff;
        background: #fff;
    }

    /* Validation messages styling */
    .invalid-feedback {
        display: none;
        color: #ff3b30;
        font-size: 10px;
        text-align: left;
        margin-top: 4px;
        font-weight: 500;
    }
    
    .was-validated .form-control:invalid ~ .invalid-feedback {
        display: block;
    }

    .btn-auth {
        background: #000;
        color: #fff;
        border-radius: 12px;
        font-weight: 600;
        padding: 13px;
        width: 100%;
        border: none;
        cursor: pointer;
        margin-top: 15px;
        font-size: 15px;
        transition: 0.2s;
    }

    .btn-auth:hover {
        opacity: 0.85;
    }

    .google-btn {
        border: 1px solid #eee;
        border-radius: 12px;
        padding: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        text-decoration: none;
        color: #1d1d1f;
        font-size: 14px;
        font-weight: 600;
        background: #fff;
        margin-top: 10px;
    }
</style>

<div class="glow-wrap">
    <div class="auth-card">
        <!-- লোগো -->
        <img src="<?= base_url('logo.png') ?>" alt="Logo" class="site-logo">
        <h3 style="font-weight: 700; margin: 0; font-size: 20px; letter-spacing: -0.5px;">Welcome Back</h3>
        <p style="color: #86868b; font-size: 12px; margin-top: 4px; margin-bottom: 15px;">Sign in to continue</p>

        <!-- Form Open (লজিক অনুযায়ী ক্লাস এবং ডাটা রিডাইরেক্ট অ্যাড করা হয়েছে) -->
        <?= form_open(base_url('sign-in'), 'class="actionForm needs-validation" novalidate="" data-redirect="' . $redirect . '"') ?>

        <div style="text-align: left; margin-top: 12px;">
            <label for="email" style="font-size: 10px; font-weight: 700; color: #86868b; text-transform: uppercase;">Email Address</label>
            <input id="email" type="email" class="form-control" name="email" tabindex="1" required autofocus value="<?= !empty($cookie_email) ? $cookie_email : set_value('email') ?>">
            <!-- ভ্যালিডেশন মেসেজ -->
            <div class="invalid-feedback">
                Please fill in your email
            </div>
        </div>

        <div style="text-align: left; margin-top: 12px;">
            <div style="display: flex; justify-content: space-between; align-items: center;">
                <label for="password" style="font-size: 10px; font-weight: 700; color: #86868b; text-transform: uppercase;">Password</label>
                <a href="<?= base_url('password-reset') ?>" tabindex="5" style="font-size: 11px; color: #007aff; text-decoration: none; font-weight: 600;">Forgot Password?</a>
            </div>
            <input id="password" type="password" class="form-control" name="password" tabindex="2" required value="<?= !empty($cookie_pass) ? $cookie_pass : set_value('password') ?>">
            <!-- ভ্যালিডেশন মেসেজ -->
            <div class="invalid-feedback">
                Please fill in your password
            </div>
        </div>

        <div style="display: flex; align-items: center; gap: 8px; margin-top: 15px; text-align: left;">
            <input type="checkbox" name="remember" id="remember-me" tabindex="3" style="accent-color:#000; width:16px; height:16px; cursor: pointer;" <?= !empty($cookie_email) ? 'checked' : '' ?>>
            <label for="remember-me" style="color: #555; cursor: pointer; font-size: 12px; font-weight: 500;">Remember Me</label>
        </div>

        <button type="submit" class="btn-auth" tabindex="4">Sign In</button>

        <?php if (get_option('google_login')) : ?>
            <div style="margin: 15px 0 5px 0; font-size: 10px; color: #bbb; font-weight: 700;">OR</div>

            <a href="<?= base_url('auth/google_process') ?>" class="google-btn">
                <svg width="18" height="18" viewBox="0 0 48 48">
                    <path fill="#EA4335" d="M24 9.5c3.54 0 6.71 1.22 9.21 3.6l6.85-6.85C35.9 2.38 30.47 0 24 0 14.62 0 6.51 5.38 2.56 13.22l7.98 6.19C12.43 13.72 17.74 9.5 24 9.5z" />
                    <path fill="#4285F4" d="M46.98 24.55c0-1.57-.15-3.09-.38-4.55H24v9.02h12.94c-.58 2.96-2.26 5.48-4.78 7.18l7.73 6c4.51-4.18 7.09-10.36 7.09-17.65z" />
                    <path fill="#FBBC05" d="M10.53 28.59c-.48-1.45-.76-2.99-.76-4.59s.27-3.14.76-4.59l-7.98-6.19C.92 16.46 0 20.12 0 24s.92 7.54 2.56 10.78l7.97-6.19z" />
                    <path fill="#34A853" d="M24 48c6.48 0 11.93-2.13 15.89-5.81l-7.73-6-2.15 1.45-4.92 2.3-8.16 2.3-6.26 0-11.57-4.22-13.47-9.91l-7.98 6.19C6.51 42.62 14.62 48 24 48z" />
                    <path fill="none" d="M0 0h48v48H0z" />
                </svg>
                Google Login
            </a>
        <?php endif; ?>

        <?= form_close(); ?> <!-- CodeIgniter form close -->

        <p style="margin-top: 18px; font-size: 13px; color: #86868b;">Don't have an account? <a href="<?= base_url('sign-up') ?>" style="color: #007aff; font-weight: 600; text-decoration: none;">Register</a></p>
    </div>
</div>