<div class="modal-overlay" id="employeeModal">
    <div class="modal-content modal-xl">
        <div class="modal-header">
            <h2 id="modalTitle">Nouvel Employé</h2>
            <button class="modal-close" onclick="closeModal()">&times;</button>
        </div>
        <div class="modal-body">
            <!-- Tabs -->
            <div class="tabs">
                <button class="tab-btn active" onclick="switchTab('personal')" data-tab="personal">
                    👤 Personnel
                </button>
                <button class="tab-btn" onclick="switchTab('professional')" data-tab="professional">
                    💼 Professionnel
                </button>
                <button class="tab-btn" onclick="switchTab('password')" data-tab="password">
                    🔒 Connexion
                </button>
            </div>

            <!-- Tab 1: Personnel -->
            <div id="tab-personal" class="tab-content active">
                <form id="employeeForm">
                    <input type="hidden" id="employeeId">

                    <div class="form-row">
                        <div class="form-group">
                            <label for="nom">Nom <span class="required">*</span></label>
                            <input type="text" id="nom" required placeholder="Ex: ZOUANI">
                        </div>
                        <div class="form-group">
                            <label for="nom_arab">Nom (Arabe)</label>
                            <input type="text" id="nom_arab" placeholder="الاسم العائلي">
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="prenom">Prénom <span class="required">*</span></label>
                            <input type="text" id="prenom" required placeholder="Ex: YOUNES">
                        </div>
                        <div class="form-group">
                            <label for="prenom_arab">Prénom (Arabe)</label>
                            <input type="text" id="prenom_arab" placeholder="الاسم الشخصي">
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="cin">CIN <span class="required">*</span></label>
                            <input type="text" id="cin" required placeholder="Ex: EE123456">
                        </div>
                        <div class="form-group">
                            <label for="email">Email <span class="required">*</span></label>
                            <input type="email" id="email" required placeholder="Ex: nom@domaine.com">
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="phone">Téléphone</label>
                            <input type="text" id="phone" placeholder="Ex: 0612345678">
                        </div>
                        <div class="form-group">
                            <label for="genre">Genre</label>
                            <select id="genre">
                                <option value="">Sélectionner</option>
                                <option value="M">Masculin</option>
                                <option value="F">Féminin</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="lieu_naissance">Lieu de naissance</label>
                            <input type="text" id="lieu_naissance" placeholder="Ex: Casablanca">
                        </div>
                        <div class="form-group">
                            <label for="date_naissance">Date de naissance</label>
                            <input type="date" id="date_naissance">
                        </div>
                    </div>
                </form>
            </div>

            <!-- Tab 2: Professionnel -->
            <div id="tab-professional" class="tab-content">
                <form id="employeeForm2">
                    <div class="form-row">
                       <div class="form-group">
                           <label for="code">Code <span class="required">*</span></label>
                           <input type="text" id="code" required placeholder="Ex: EMP001">
                           <span class="help-text">Entrez un code unique pour l'employé</span>
                       </div>
                        <div class="form-group">
                            <label for="login">Login <span class="required">*</span></label>
                            <input type="text" id="login" required placeholder="Ex: zouani.younes">
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="specialite">Spécialité</label>
                            <input type="text" id="specialite" placeholder="Ex: Informatique">
                        </div>
                        <div class="form-group">
                            <label for="diplome">Diplôme</label>
                            <input type="text" id="diplome" placeholder="Ex: Doctorat">
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="etablissement_diplome">Établissement diplôme</label>
                            <input type="text" id="etablissement_diplome" placeholder="Ex: Université Hassan II">
                        </div>
                        <div class="form-group">
                            <label for="status">Statut</label>
                            <select id="status">
                                <option value="active">Actif</option>
                                <option value="inactive">Inactif</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="fonctionnaire">Fonctionnaire</label>
                            <select id="fonctionnaire">
                                <option value="0">Non</option>
                                <option value="1">Oui</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="nombre_annee_fonctionnaire">Années fonctionnaire</label>
                            <input type="number" id="nombre_annee_fonctionnaire" min="0" placeholder="0">
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="date_recrutement">Date recrutement</label>
                            <input type="date" id="date_recrutement">
                        </div>
                        <div class="form-group">
                            <label for="date_affectation_enseignement">Date affectation</label>
                            <input type="date" id="date_affectation_enseignement">
                        </div>
                    </div>

                    <!-- NOUVEL ORDRE : Type de grade en premier, puis Grade -->
                    <div class="form-row">
                        <div class="form-group">
                            <label for="type_grade_id">Type de grade</label>
                            <select id="type_grade_id" onchange="filterGradesByType(this.value)">
                                <option value="">Sélectionner un type</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="grade_id">Grade</label>
                            <select id="grade_id" onchange="filterEchellesByGrade(this.value)">
                                <option value="">Sélectionner un grade</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="echelle_select">Échelle</label>
                            <select id="echelle_select" onchange="loadEchellons(this.value)">
                                <option value="">Sélectionner une échelle</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="echellon_id">Échelon</label>
                            <select id="echellon_id" disabled>
                                <option value="">Sélectionner un échelon</option>
                            </select>
                        </div>
                    </div>
                </form>
            </div>
           <!-- Tab 3: Connexion -->
           <div id="tab-password" class="tab-content">
               <div class="password-section">
                   <h3 style="margin-top: 0; color: #2c3e50;">
                       <i class="fas fa-key" style="color: #e74c3c;"></i> Gestion du mot de passe
                   </h3>

                   <p style="color: #666; font-size: 14px; margin-bottom: 20px;" id="passwordMessage">
                       Pour un nouvel employé, veuillez définir un mot de passe.
                   </p>

                   <div class="form-row">
                       <div class="form-group">
                           <label for="new_password">
                               Nouveau mot de passe
                               <span class="required" id="passwordRequired">*</span>
                           </label>
                           <div class="password-input-wrapper">
                               <input type="password" id="new_password" placeholder="Au moins 6 caractères" minlength="6">
                               <button type="button" class="password-toggle" onclick="togglePasswordVisibility('new_password', this)" title="Afficher/Masquer le mot de passe">
                                   <i class="fas fa-eye"></i>
                               </button>
                           </div>
                           <div class="password-strength">
                               <div class="bar" id="passwordStrengthBar"></div>
                           </div>
                           <span class="password-hint" id="passwordStrengthText">Entrez un mot de passe</span>
                       </div>
                       <div class="form-group">
                           <label for="confirm_password">
                               Confirmer le mot de passe
                               <span class="required" id="confirmRequired">*</span>
                           </label>
                           <div class="password-input-wrapper">
                               <input type="password" id="confirm_password" placeholder="Confirmer le mot de passe">
                               <button type="button" class="password-toggle" onclick="togglePasswordVisibility('confirm_password', this)" title="Afficher/Masquer le mot de passe">
                                   <i class="fas fa-eye"></i>
                               </button>
                           </div>
                       </div>
                   </div>

                   <!-- Bouton pour générer un mot de passe (visible en création) -->
                   <div style="margin-top: 15px; text-align: center;" id="generatePasswordContainer">
                       <button type="button" class="btn btn-warning" onclick="generatePassword()" id="generatePasswordBtn">
                           <i class="fas fa-sync-alt"></i> <span id="generatePasswordText">Générer un mot de passe</span>
                       </button>
                       <p style="font-size: 12px; color: #888; margin-top: 8px;" id="generatePasswordHint">
                           Cliquez pour générer un nouveau mot de passe automatiquement
                       </p>
                   </div>

                   <!-- Bouton pour changer le mot de passe (visible en édition) -->
                   <div style="margin-top: 15px; text-align: center; display: none;" id="changePasswordContainer">
                       <button type="button" class="btn btn-warning" onclick="changeExistingPassword()" id="changePasswordBtn">
                           <i class="fas fa-key"></i> Changer le mot de passe
                       </button>
                       <p style="font-size: 12px; color: #888; margin-top: 8px;">
                           Saisissez un mot de passe ci-dessus puis cliquez pour l'enregistrer directement
                       </p>
                   </div>

                   <div style="background: #fff3cd; border-radius: 8px; padding: 12px 16px; margin-top: 15px; border-left: 4px solid #ffc107;">
                       <p style="margin: 0; font-size: 13px; color: #856404;">
                           <i class="fas fa-info-circle"></i>
                           Le mot de passe doit contenir au moins 6 caractères.
                           <span id="passwordEditHint" style="display: none;">Laissez vide pour garder le mot de passe actuel.</span>
                       </p>
                   </div>
               </div>
           </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" onclick="closeModal()">Annuler</button>
            <button type="button" class="btn btn-primary" onclick="saveEmployee()">💾 Enregistrer</button>
        </div>
    </div>
</div>

<style>
    /* ============================================ */
    /* MODAL OVERLAY */
    /* ============================================ */

    .modal-overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.5);
        z-index: 9999;
        justify-content: center;
        align-items: center;
        backdrop-filter: blur(4px);
    }

    .modal-overlay.active {
        display: flex;
    }

    /* ============================================ */
    /* MODAL CONTENT */
    /* ============================================ */

    .modal-content {
        background: #ffffff;
        border-radius: 16px;
        max-width: 900px;
        width: 95%;
        max-height: 95vh;
        display: flex;
        flex-direction: column;
        box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
        animation: modalFadeIn 0.3s ease;
    }

    .modal-xl {
        max-width: 1000px;
    }

    @keyframes modalFadeIn {
        from {
            opacity: 0;
            transform: translateY(-30px) scale(0.95);
        }
        to {
            opacity: 1;
            transform: translateY(0) scale(1);
        }
    }

    /* ============================================ */
    /* MODAL HEADER */
    /* ============================================ */

    .modal-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 20px 30px;
        border-bottom: 2px solid #e9ecef;
        flex-shrink: 0;
    }

    .modal-header h2 {
        margin: 0;
        font-size: 22px;
        color: #2c3e50;
        font-weight: 700;
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .modal-close {
        background: none;
        border: none;
        font-size: 32px;
        cursor: pointer;
        color: #adb5bd;
        transition: color 0.3s ease;
        padding: 0 5px;
        line-height: 1;
    }

    .modal-close:hover {
        color: #343a40;
    }

    /* ============================================ */
    /* MODAL BODY */
    /* ============================================ */

    .modal-body {
        padding: 25px 30px;
        overflow-y: auto;
        flex: 1;
        max-height: calc(95vh - 140px);
    }

    .modal-body::-webkit-scrollbar {
        width: 6px;
    }

    .modal-body::-webkit-scrollbar-track {
        background: #f1f1f1;
        border-radius: 3px;
    }

    .modal-body::-webkit-scrollbar-thumb {
        background: #c1c1c1;
        border-radius: 3px;
    }

    .modal-body::-webkit-scrollbar-thumb:hover {
        background: #a8a8a8;
    }

    /* ============================================ */
    /* TABS */
    /* ============================================ */

    .tabs {
        display: flex;
        border-bottom: 3px solid #e9ecef;
        gap: 0;
        background: #f8f9fa;
        border-radius: 10px 10px 0 0;
        overflow: hidden;
        margin-bottom: 25px;
        flex-shrink: 0;
    }

    .tab-btn {
        padding: 14px 28px;
        background: none;
        border: none;
        border-bottom: 3px solid transparent;
        cursor: pointer;
        font-size: 15px;
        font-weight: 600;
        color: #6c757d;
        transition: all 0.3s ease;
        display: flex;
        align-items: center;
        gap: 10px;
        flex: 1;
        justify-content: center;
        background: transparent;
    }

    .tab-btn:hover {
        color: #2c3e50;
        background: rgba(0, 0, 0, 0.03);
    }

    .tab-btn.active {
        color: #2c3e50;
        border-bottom-color: #3498db;
        background: #ffffff;
    }

    .tab-btn i {
        font-size: 18px;
    }

    .tab-content {
        display: none;
        padding: 5px 0 20px 0;
        animation: fadeIn 0.3s ease;
    }

    .tab-content.active {
        display: block;
    }

    @keyframes fadeIn {
        from {
            opacity: 0;
            transform: translateY(10px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    /* ============================================ */
    /* FORM ROWS (2 COLUMNS) */
    /* ============================================ */

    .form-row {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 18px 25px;
        margin-bottom: 5px;
    }

    .form-row .form-group {
        margin-bottom: 15px;
    }

    .form-row .form-group:last-child {
        margin-bottom: 0;
    }

    /* ============================================ */
    /* FORM GROUP */
    /* ============================================ */

    .form-group label {
        display: block;
        font-weight: 600;
        font-size: 14px;
        color: #343a40;
        margin-bottom: 6px;
    }

    .form-group input,
    .form-group select,
    .form-group textarea {
        width: 100%;
        padding: 10px 14px;
        border: 1.5px solid #dee2e6;
        border-radius: 8px;
        font-size: 14px;
        transition: all 0.3s ease;
        background: #ffffff;
        box-sizing: border-box;
        font-family: inherit;
    }

    .form-group input:focus,
    .form-group select:focus,
    .form-group textarea:focus {
        border-color: #3498db;
        outline: none;
        box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.15);
    }

    .form-group input:disabled,
    .form-group select:disabled {
        background: #e9ecef;
        cursor: not-allowed;
        opacity: 0.7;
    }

    .form-group textarea {
        resize: vertical;
        min-height: 80px;
    }

    .required {
        color: #dc3545;
        font-weight: bold;
    }

    .help-text {
        font-size: 12px;
        color: #6c757d;
        margin-top: 4px;
        display: block;
    }

    /* ============================================ */
    /* PASSWORD SECTION */
    /* ============================================ */

    .password-section {
        background: #f8f9fa;
        border-radius: 12px;
        padding: 25px 30px;
        border: 1px solid #e9ecef;
    }

    .password-section h3 {
        margin-top: 0;
        color: #2c3e50;
        font-size: 18px;
        font-weight: 700;
    }

    .password-section h3 i {
        margin-right: 8px;
    }

    .password-strength {
        height: 4px;
        background: #e9ecef;
        border-radius: 2px;
        margin-top: 8px;
        overflow: hidden;
    }

    .password-strength .bar {
        height: 100%;
        width: 0%;
        transition: width 0.3s ease;
        border-radius: 2px;
    }

    .password-strength .bar.weak {
        background: #dc3545;
        width: 25%;
    }

    .password-strength .bar.medium {
        background: #ffc107;
        width: 50%;
    }

    .password-strength .bar.strong {
        background: #28a745;
        width: 75%;
    }

    .password-strength .bar.very-strong {
        background: #28a745;
        width: 100%;
    }

    .password-hint {
        font-size: 12px;
        color: #6c757d;
        margin-top: 5px;
        display: block;
    }

    /* ============================================ */
    /* MODAL FOOTER */
    /* ============================================ */

    .modal-footer {
        display: flex;
        justify-content: flex-end;
        gap: 12px;
        padding: 18px 30px 20px;
        border-top: 2px solid #e9ecef;
        flex-shrink: 0;
        background: #fafbfc;
        border-radius: 0 0 16px 16px;
    }

    /* ============================================ */
    /* BUTTONS */
    /* ============================================ */

    .btn {
        padding: 10px 28px;
        border: none;
        border-radius: 8px;
        font-size: 14px;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s ease;
        display: inline-flex;
        align-items: center;
        gap: 8px;
        font-family: inherit;
    }

    .btn-primary {
        background: #3498db;
        color: #ffffff;
    }

    .btn-primary:hover {
        background: #2980b9;
        transform: translateY(-1px);
        box-shadow: 0 4px 15px rgba(52, 152, 219, 0.35);
    }

    .btn-secondary {
        background: #e9ecef;
        color: #495057;
    }

    .btn-secondary:hover {
        background: #dee2e6;
        transform: translateY(-1px);
    }

    .btn-warning {
        background: #f39c12;
        color: #ffffff;
    }

    .btn-warning:hover {
        background: #e67e22;
        transform: translateY(-1px);
        box-shadow: 0 4px 15px rgba(243, 156, 18, 0.35);
    }

    .btn-success {
        background: #28a745;
        color: #ffffff;
    }

    .btn-success:hover {
        background: #218838;
        transform: translateY(-1px);
        box-shadow: 0 4px 15px rgba(40, 167, 69, 0.35);
    }

    /* ============================================ */
    /* RESPONSIVE */
    /* ============================================ */

    @media (max-width: 768px) {
        .modal-content {
            max-width: 98%;
            max-height: 98vh;
        }

        .modal-header {
            padding: 15px 20px;
        }

        .modal-header h2 {
            font-size: 18px;
        }

        .modal-body {
            padding: 15px 20px;
            max-height: calc(98vh - 120px);
        }

        .form-row {
            grid-template-columns: 1fr;
            gap: 0;
        }

        .form-row .form-group {
            margin-bottom: 12px;
        }

        .tabs {
            flex-wrap: wrap;
            border-radius: 8px;
        }

        .tab-btn {
            flex: 1;
            justify-content: center;
            font-size: 12px;
            padding: 10px 8px;
            min-width: 60px;
            gap: 6px;
        }

        .modal-footer {
            flex-direction: column;
            padding: 12px 20px 16px;
        }

        .modal-footer .btn {
            width: 100%;
            justify-content: center;
        }

        .password-section {
            padding: 15px 18px;
        }
    }

    @media (max-width: 480px) {
        .tab-btn {
            font-size: 11px;
            padding: 8px 6px;
            min-width: 50px;
        }

        .modal-header h2 {
            font-size: 16px;
        }

        .modal-close {
            font-size: 28px;
        }

        .btn {
            padding: 8px 18px;
            font-size: 13px;
        }
    }
    /* ============================================ */
    /* PASSWORD INPUT WITH TOGGLE EYE */
    /* ============================================ */

    .password-input-wrapper {
        position: relative;
        width: 100%;
    }

    .password-input-wrapper input {
        width: 100%;
        padding: 10px 45px 10px 14px;
        border: 1.5px solid #dee2e6;
        border-radius: 8px;
        font-size: 14px;
        transition: all 0.3s ease;
        background: #ffffff;
        box-sizing: border-box;
        font-family: inherit;
    }

    .password-input-wrapper input:focus {
        border-color: #3498db;
        outline: none;
        box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.15);
    }

    .password-input-wrapper input:disabled {
        background: #e9ecef;
        cursor: not-allowed;
        opacity: 0.7;
    }

    .password-toggle {
        position: absolute;
        right: 12px;
        top: 50%;
        transform: translateY(-50%);
        background: none;
        border: none;
        cursor: pointer;
        color: #6c757d;
        font-size: 18px;
        padding: 5px;
        transition: color 0.3s ease;
        display: flex;
        align-items: center;
        justify-content: center;
        z-index: 2;
    }

    .password-toggle:hover {
        color: #2c3e50;
    }

    .password-toggle:focus {
        outline: none;
    }

    .password-toggle .fa-eye-slash {
        color: #3498db;
    }
</style>