<!-- ===================== USERS TAB ===================== -->
<div class="tab-pane <?= (isset($_GET['tab']) && $_GET['tab'] == 'users') ? 'active' : '' ?>" id="tab-users">
  <div class="toolbar">
    <span class="card-title">Team Members</span>
    <?php if ($is_admin): ?>
    <button class="btn btn-primary btn-sm" onclick="openModal('addUserModal')" style="margin-left:auto">+ Add User</button>
    <?php endif; ?>
  </div>
  <div class="card">
    <div class="table-wrapper">
      <table class="crm-table">
        <thead><tr><th>Name</th><th>Email</th><th>Phone</th><th>Role</th><th>Since</th><th>Actions</th></tr></thead>
        <tbody>
          <?php while ($u = $all_users->fetch_assoc()): ?>
          <tr id="user_<?= $u['id'] ?>">
            <td><div class="d-flex align-center gap-8"><div class="avatar avatar-sm"><?= strtoupper(substr($u['name'],0,2)) ?></div><?= htmlspecialchars($u['name']) ?></div></td>
            <td><?= htmlspecialchars($u['email']) ?></td>
            <td><?= htmlspecialchars($u['phone'] ?: '—') ?></td>
            <td>
              <?php if ($is_admin && $u['id'] != $user_id): ?>
              <select class="form-control form-control-sm" onchange="changeUserRole(<?= $u['id'] ?>, this.value)">
                <option value="">No Role</option>
                <?php 
                $roles->data_seek(0);
                while ($ro = $roles->fetch_assoc()): 
                ?>
                <option value="<?= $ro['id'] ?>" <?= $u['role_id'] == $ro['id'] ? 'selected' : '' ?>><?= htmlspecialchars($ro['name']) ?></option>
                <?php endwhile; ?>
              </select>
              <?php else: ?>
              <span class="badge badge-blue"><?= htmlspecialchars($u['role_name'] ?: 'User') ?></span>
              <?php endif; ?>
             </td>
            <td class="text-muted text-xs"><?= formatDate($u['datetime_created']) ?></td>
            <td>
              <?php if ($u['id'] != $user_id): ?>
                <?php if ($is_admin): ?>
                <button class="btn btn-warning btn-sm" onclick="editUser(<?= $u['id'] ?>, '<?= addslashes($u['name']) ?>', '<?= addslashes($u['email']) ?>', '<?= addslashes($u['phone']) ?>', <?= $u['role_id'] ?: 'null' ?>)">Edit</button>
                <button class="btn btn-danger btn-sm" onclick="deleteUser(<?= $u['id'] ?>, '<?= addslashes($u['name']) ?>')">Remove</button>
                <?php endif; ?>
              <?php else: ?>
              <span class="badge badge-teal">You</span>
              <?php endif; ?>
             </td>
           </tr>
          <?php endwhile; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>
