<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    <?php 
        // Helper to safely retrieve settings data from controller variables ($company or $setting)
        $companyData = $company ?? $setting ?? $company_info ?? [];
        $companyName = !empty($companyData['company_name']) ? $companyData['company_name'] : (!empty($companyData['name']) ? $companyData['name'] : 'CNC ERP System');
        
        // Multi-key fallback for logo field (checks logo, company_logo, logo_url, app_logo, site_logo)
        $logoFile = !empty($companyData['logo']) ? $companyData['logo'] : 
                    (!empty($companyData['company_logo']) ? $companyData['company_logo'] : 
                    (!empty($companyData['logo_url']) ? $companyData['logo_url'] : 
                    (!empty($companyData['app_logo']) ? $companyData['app_logo'] : 
                    (!empty($companyData['site_logo']) ? $companyData['site_logo'] : null))));
    ?>

    <title>Login Portal - <?= esc($companyName) ?></title>
    
    <!-- Bootstrap 5, Fonts & Bootstrap Icons -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&family=JetBrains+Mono:wght@500;700&display=swap" rel="stylesheet">
    
    <style>
        /* ===== GAHAR CYBER-INDUSTRIAL DESIGN TOKENS ===== */
        :root {
            /* LIGHT MODE PRESET */
            --bg-body: #0b0f19;
            --bg-card: rgba(15, 23, 42, 0.75);
            --bg-input: rgba(30, 41, 59, 0.6);
            --border-color: rgba(255, 255, 255, 0.12);
            --border-focus: #3b82f6;
            --text-main: #f8fafc;
            --text-sub: #94a3b8;
            --primary: #2563eb;
            --primary-glow: #3b82f6;
            --primary-hover: #1d4ed8;
            --accent-cyan: #06b6d4;
            --ft-bg: rgba(15, 23, 42, 0.8);
            --ft-border: rgba(255, 255, 255, 0.08);
            --ft-text: #94a3b8;
            --ft-link-hover: #60a5fa;
            --shadow-glow: 0 0 35px -5px rgba(37, 99, 235, 0.45);
            --radius-lg: 1.5rem;
            --radius-md: 0.9rem;
            --transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
        }

        /* AUTO DARK MODE - SYSTEM PREFERENCE */
        @media (prefers-color-scheme: dark) {
            :root {
                --bg-body: #070a12;
                --bg-card: rgba(15, 23, 42, 0.85);
                --bg-input: rgba(15, 23, 42, 0.8);
                --border-color: rgba(255, 255, 255, 0.14);
            }
        }

        body {
            background-color: var(--bg-body);
            color: var(--text-main);
            transition: var(--transition);
            font-family: 'Plus Jakarta Sans', system-ui, -apple-system, sans-serif;
            position: relative;
            overflow-x: hidden;
        }

        /* Ambient Cyber Grid & Glow Blobs Background */
        .ambient-bg {
            position: fixed;
            inset: 0;
            z-index: -1;
            overflow: hidden;
            background-image: 
                radial-gradient(circle at 15% 20%, rgba(37, 99, 235, 0.25) 0%, transparent 45%),
                radial-gradient(circle at 85% 80%, rgba(6, 182, 212, 0.2) 0%, transparent 50%),
                radial-gradient(circle at 50% 50%, rgba(99, 102, 241, 0.15) 0%, transparent 65%);
        }

        .ambient-grid {
            position: absolute;
            inset: 0;
            background-size: 40px 40px;
            background-image: 
                linear-gradient(to right, rgba(255, 255, 255, 0.03) 1px, transparent 1px),
                linear-gradient(to bottom, rgba(255, 255, 255, 0.03) 1px, transparent 1px);
            mask-image: radial-gradient(ellipse at center, black 40%, transparent 80%);
        }

        /* Login Container Card - Ultra Glassmorphism */
        .login-card-gahar {
            background: var(--bg-card);
            backdrop-filter: blur(20px);
            -webkit-backdrop-filter: blur(20px);
            border: 1px solid var(--border-color);
            border-radius: var(--radius-lg);
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5), var(--shadow-glow);
            transition: var(--transition);
            overflow: hidden;
            position: relative;
        }

        .login-card-gahar::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 2px;
            background: linear-gradient(90deg, transparent, var(--primary-glow), var(--accent-cyan), transparent);
        }

        /* Brand Icon Box */
        .brand-icon-wrapper {
            width: 76px;
            height: 76px;
            background: linear-gradient(135deg, rgba(37, 99, 235, 0.2), rgba(6, 182, 212, 0.15));
            color: #60a5fa;
            border-radius: var(--radius-md);
            display: inline-flex;
            align-items: center;
            justify-content: center;
            font-size: 2.2rem;
            margin-bottom: 1.25rem;
            padding: 10px;
            overflow: hidden;
            border: 1px solid rgba(96, 165, 250, 0.3);
            box-shadow: 0 0 20px rgba(37, 99, 235, 0.3);
        }

        .brand-icon-wrapper img {
            width: 100%;
            height: 100%;
            object-fit: contain;
            border-radius: calc(var(--radius-md) - 6px);
        }

        /* System Operational Badge */
        .sys-badge {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 4px 12px;
            border-radius: 20px;
            background: rgba(37, 99, 235, 0.15);
            border: 1px solid rgba(59, 130, 246, 0.3);
            color: #93c5fd;
            font-size: 0.72rem;
            font-weight: 700;
            letter-spacing: 0.05em;
            text-transform: uppercase;
        }

        .sys-badge .pulse-dot {
            width: 7px;
            height: 7px;
            background-color: #10b981;
            border-radius: 50%;
            box-shadow: 0 0 8px #10b981;
            animation: pulse-ring 1.8s infinite;
        }

        @keyframes pulse-ring {
            0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7); }
            70% { transform: scale(1); box-shadow: 0 0 0 6px rgba(16, 185, 129, 0); }
            100% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(16, 185, 129, 0); }
        }

        /* Form Custom Input Groups */
        .premium-input-group {
            background: var(--bg-input);
            border: 1px solid var(--border-color);
            border-radius: var(--radius-md);
            transition: var(--transition);
            display: flex;
            align-items: center;
            padding: 0.4rem 0.9rem;
        }

        .premium-input-group:focus-within {
            border-color: var(--border-focus);
            box-shadow: 0 0 20px rgba(59, 130, 246, 0.35);
            background: rgba(15, 23, 42, 0.95);
        }

        .premium-input-group .form-control {
            border: none !important;
            background: transparent !important;
            box-shadow: none !important;
            font-size: 0.92rem;
            padding: 0.55rem 0.6rem;
            color: var(--text-main) !important;
        }

        .premium-input-group .form-control::placeholder {
            color: var(--text-sub) !important;
            opacity: 0.6;
        }

        .input-icon-lead {
            color: #60a5fa;
            font-size: 1.15rem;
        }

        .btn-toggle-password {
            background: transparent;
            border: none;
            color: var(--text-sub);
            padding: 0 0.25rem;
            cursor: pointer;
            transition: var(--transition);
        }

        .btn-toggle-password:hover {
            color: #60a5fa;
        }

        /* Primary High-Tech Button */
        .btn-gahar-glow {
            background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%);
            border: 1px solid rgba(96, 165, 250, 0.4);
            color: #ffffff;
            font-weight: 700;
            padding: 0.8rem 1.5rem;
            border-radius: var(--radius-md);
            transition: var(--transition);
            box-shadow: 0 4px 20px rgba(37, 99, 235, 0.5);
            letter-spacing: 0.02em;
        }

        .btn-gahar-glow:hover {
            background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
            transform: translateY(-2px);
            box-shadow: 0 8px 28px rgba(37, 99, 235, 0.7);
            border-color: rgba(147, 197, 253, 0.6);
            color: #ffffff;
        }

        .btn-gahar-glow:active {
            transform: translateY(0);
        }

        /* Modern Alert Styling */
        .alert-soft-danger {
            background-color: rgba(239, 68, 68, 0.15);
            border: 1px solid rgba(239, 68, 68, 0.3);
            color: #fca5a5;
            border-radius: var(--radius-md);
            font-size: 0.85rem;
        }

        .alert-soft-success {
            background-color: rgba(16, 185, 129, 0.15);
            border: 1px solid rgba(16, 185, 129, 0.3);
            color: #6ee7b7;
            border-radius: var(--radius-md);
            font-size: 0.85rem;
        }

        /* Adaptive Cyber Footer */
        footer.footer-adaptive {
            background-color: var(--ft-bg) !important;
            border-top: 1px solid var(--ft-border) !important;
            color: var(--ft-text) !important;
            backdrop-filter: blur(10px);
            transition: var(--transition);
        }

        footer.footer-adaptive span {
            color: var(--ft-text) !important;
        }

        footer.footer-adaptive a.footer-link {
            color: #93c5fd !important;
            text-decoration: none;
            transition: var(--transition);
            border-bottom: 1px dashed transparent;
        }

        footer.footer-adaptive a.footer-link:hover {
            color: #38bdf8 !important;
            border-bottom-color: #38bdf8;
        }

        .security-footer-tag {
            font-family: 'JetBrains Mono', monospace;
            font-size: 0.73rem;
            color: #64748b;
        }
    </style>
</head>
<body class="d-flex flex-column min-vh-100 justify-content-between">

    <!-- Ambient Glowing Cyber Grid Overlay -->
    <div class="ambient-bg">
        <div class="ambient-grid"></div>
    </div>

    <!-- Main Login Workspace -->
    <main class="container my-auto py-5">
        <div class="row justify-content-center">
            <div class="col-12 col-sm-10 col-md-7 col-lg-5 col-xl-4">
                <div class="card login-card-gahar p-3 p-sm-4">
                    <div class="card-body">
                        
                        <!-- Dynamic Header Branding with Multi-location Logo Finder -->
                        <div class="text-center mb-4">
                            <div class="sys-badge mb-3">
                                <span class="pulse-dot"></span>
                                SYSTEM ONLINE • ERP V2.0
                            </div>

                            <div class="d-block mb-3">
                                <div class="brand-icon-wrapper mx-auto">
                                    <?php 
                                        $logoPath = null;
                                        if (!empty($logoFile)) {
                                            if (filter_var($logoFile, FILTER_VALIDATE_URL) || strpos($logoFile, 'http://') === 0 || strpos($logoFile, 'https://') === 0) {
                                                $logoPath = $logoFile;
                                            } else {
                                                $cleanFile = ltrim($logoFile, '/');
                                                $baseName  = basename($cleanFile);

                                                if (file_exists(FCPATH . $cleanFile)) {
                                                    $logoPath = base_url($cleanFile);
                                                } elseif (file_exists(FCPATH . 'uploads/' . $cleanFile)) {
                                                    $logoPath = base_url('uploads/' . $cleanFile);
                                                } elseif (file_exists(FCPATH . 'uploads/company/' . $baseName)) {
                                                    $logoPath = base_url('uploads/company/' . $baseName);
                                                } elseif (file_exists(FCPATH . 'uploads/logo/' . $baseName)) {
                                                    $logoPath = base_url('uploads/logo/' . $baseName);
                                                } elseif (file_exists(FCPATH . 'assets/img/' . $baseName)) {
                                                    $logoPath = base_url('assets/img/' . $baseName);
                                                } else {
                                                    $logoPath = base_url($cleanFile);
                                                }
                                            }
                                        }
                                    ?>

                                    <?php if ($logoPath): ?>
                                        <img src="<?= $logoPath ?>" 
                                             alt="<?= esc($companyName) ?> Logo" 
                                             onerror="this.style.display='none'; document.getElementById('logoFallbackIcon').style.display='inline-block';"
                                             loading="lazy">
                                        <i class="bi bi-cpu-fill" id="logoFallbackIcon" style="display: none;"></i>
                                    <?php else: ?>
                                        <i class="bi bi-cpu-fill"></i>
                                    <?php endif; ?>
                                </div>
                            </div>

                            <h4 class="fw-extrabold mb-1 tracking-tight" style="color: var(--text-main); font-weight: 800;"><?= esc($companyName) ?></h4>
                            <p class="small mb-0" style="color: var(--text-sub);">Masukkan kredensial untuk mengakses portal operasional</p>
                        </div>

                        <!-- Flash Messages Notification -->
                        <?php if (session()->getFlashdata('error')): ?>
                            <div class="alert alert-soft-danger d-flex align-items-center gap-2 mb-4" role="alert">
                                <i class="bi bi-exclamation-triangle-fill fs-6 flex-shrink-0"></i>
                                <div><?= session()->getFlashdata('error') ?></div>
                            </div>
                        <?php endif; ?>

                        <?php if (session()->getFlashdata('success')): ?>
                            <div class="alert alert-soft-success d-flex align-items-center gap-2 mb-4" role="alert">
                                <i class="bi bi-check-circle-fill fs-6 flex-shrink-0"></i>
                                <div><?= session()->getFlashdata('success') ?></div>
                            </div>
                        <?php endif; ?>

                        <!-- Form Authentication -->
                        <form action="<?= base_url('login/auth') ?>" method="post">
                            <?= csrf_field() ?>

                            <!-- Username Field -->
                            <div class="mb-3">
                                <label class="form-label small fw-semibold" style="color: var(--text-sub);">Username</label>
                                <div class="premium-input-group">
                                    <i class="bi bi-person-fill input-icon-lead ps-1"></i>
                                    <input type="text" name="username" class="form-control" placeholder="Masukkan ID / Username" required autocomplete="username">
                                </div>
                            </div>

                            <!-- Password Field -->
                            <div class="mb-4">
                                <div class="d-flex justify-content-between align-items-center mb-1">
                                    <label class="form-label small fw-semibold mb-0" style="color: var(--text-sub);">Password</label>
                                    <a href="<?= base_url('forgot-password') ?>" class="small text-decoration-none" style="color: #60a5fa; font-size: 0.8rem;">Lupa password?</a>
                                </div>
                                <div class="premium-input-group">
                                    <i class="bi bi-shield-lock-fill input-icon-lead ps-1"></i>
                                    <input type="password" id="passwordInput" name="password" class="form-control" placeholder="••••••••••••" required autocomplete="current-password">
                                    <button type="button" id="togglePassword" class="btn-toggle-password pe-1" title="Tampilkan/Sembunyikan Password">
                                        <i class="bi bi-eye"></i>
                                    </button>
                                </div>
                            </div>

                            <!-- Submit Button -->
                            <button type="submit" class="btn btn-gahar-glow w-100 d-flex align-items-center justify-content-center gap-2">
                                <span>MASUK KE SISTEM</span>
                                <i class="bi bi-arrow-right-short fs-4"></i>
                            </button>
                        </form>

                        <!-- Security SSL Encrypted Tag -->
                        <div class="text-center mt-4 pt-2 border-top border-white border-opacity-10">
                            <span class="security-footer-tag d-inline-flex align-items-center gap-1">
                                <i class="bi bi-lock-fill text-emerald-400"></i> Encrypted 256-Bit SSL Session
                            </span>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </main>

    <!-- Synchronized Adaptive Footer -->
    <footer class="footer-adaptive px-3 px-md-4 py-3 small mt-auto">
        <div class="d-flex flex-column flex-sm-row align-items-center justify-content-between gap-2 text-center text-sm-start">
            <span>&copy; <?= date('Y') ?> <?= esc($companyName) ?>. All rights reserved.</span>
            <div class="d-flex align-items-center gap-2 flex-wrap justify-content-center justify-content-sm-end">
                <span>Enterprise Portal</span>
                <i class="bi bi-circle-fill opacity-20" style="font-size: 4px;"></i>
                <span>Dev by 
                    <a href="https://github.com/mahardikadev" target="_blank" rel="noopener noreferrer" class="fw-semibold footer-link">mahardikadev</a> & 
                    <a href="https://rhn.web.id" target="_blank" rel="noopener noreferrer" class="fw-semibold footer-link">rhn.web.id</a>
                </span>
            </div>
        </div>
    </footer>

    <!-- Interactive Password Visibility Toggle Script -->
    <script>
        document.addEventListener("DOMContentLoaded", function () {
            const togglePassword = document.querySelector('#togglePassword');
            const passwordInput = document.querySelector('#passwordInput');

            if (togglePassword && passwordInput) {
                togglePassword.addEventListener('click', function () {
                    const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
                    passwordInput.setAttribute('type', type);
                    
                    const icon = this.querySelector('i');
                    icon.classList.toggle('bi-eye');
                    icon.classList.toggle('bi-eye-slash');
                });
            }
        });
    </script>
</body>
</html>