<?php
require_once __DIR__ . '/../config/constants.php';
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/rbac.php';

startSecureSession();
if (isLoggedIn()) { redirectByRole(); }

$error = '';
$mode  = $_GET['mode'] ?? 'choose';
$path  = $_GET['path'] ?? '';
if (!in_array($path, ['clerk','lawyer','admin'], true)) $path = '';
if ($mode !== 'choose' && $path === '') $mode = 'choose';
// تسجيل الحساب متاح للمحامي فقط
if ($mode === 'register' && $path !== 'lawyer') {
    header('Location: ' . BASE_URL . '/public/index.php?mode=login&path=' . urlencode($path));
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? 'login';
    if ($action === 'login') {
        $result = login($_POST['email'] ?? '', $_POST['password'] ?? '');
        if ($result['success']) {
            redirectByRole();
        } else {
            $error = $result['message'];
            $mode  = 'login';
            $path  = $_POST['path'] ?? $path;
        }
    }
    if ($action === 'register') {
        $required = ['full_name','email','phone','password','password2','bar_number','bar_association_id','specialization'];
        $missing  = array_filter($required, fn($f) => empty($_POST[$f]));
        if ($missing) {
            $error = 'يرجى ملء جميع الحقول المطلوبة';
        } elseif ($_POST['password'] !== $_POST['password2']) {
            $error = 'كلمتا المرور غير متطابقتين';
        } elseif (strlen($_POST['password']) < 8) {
            $error = 'كلمة المرور يجب أن تكون 8 أحرف على الأقل';
        } else {
            $barAssocId = $_POST['bar_association_id'] ?: null;
            $barAssocName = $_POST['bar_association'];
            if ($barAssocId) {
                $b = db()->prepare('SELECT name FROM bar_associations WHERE id=?');
                $b->execute([$barAssocId]);
                $barAssocName = $b->fetchColumn() ?: $barAssocName;
            }
            $result = registerLawyer([
                'full_name'          => $_POST['full_name'],
                'email'              => $_POST['email'],
                'phone'              => $_POST['phone'],
                'password'           => $_POST['password'],
                'bar_number'         => $_POST['bar_number'],
                'bar_association'    => $barAssocName,
                'bar_association_id' => $barAssocId,
                'specialization'     => $_POST['specialization'],
            ]);
            if ($result['success']) {
                $error = '';
                $mode  = 'login';
                $path  = 'lawyer';
                $successMsg = 'تم إنشاء حسابك! سجّل الدخول الآن.';
            } else {
                $error = $result['message'];
                $mode  = 'register';
                $path  = 'lawyer';
            }
        }
    }
}

$homeUrl = BASE_URL . '/public/home.php';
$successMsg = $successMsg ?? '';
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl" data-theme="dark">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>(function(){var d=localStorage.getItem('kadiyati_dark');if(d===null){localStorage.setItem('kadiyati_dark','1');d='1';}document.documentElement.setAttribute('data-theme',d==='1'?'dark':'light');var l=localStorage.getItem('kadiyati_lang')||'ar';document.documentElement.lang=l;document.documentElement.dir=(l==='en'||l==='fr')?'ltr':'rtl';}());</script>
<title>
<?php
if ($mode==='choose') echo 'اختر المسار';
elseif ($mode==='register') echo 'تسجيل محامي';
elseif ($path==='admin') echo 'دخول مدير النظام';
elseif ($path==='clerk') echo 'دخول أمين الضبط';
else echo 'دخول المحامي';
echo ' — ' . APP_NAME;
?>
</title>
<link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700;800&family=Amiri:wght@700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.rtl.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<style>
:root{--navy:#0A1628;--gold:#C9A84C;--gold2:#E8C97A;--border:rgba(255,255,255,.09);--border2:rgba(201,168,76,.25);--surf:rgba(255,255,255,.04);--card:rgba(12,24,44,.97);--txt:rgba(240,237,232,.65);--txt2:rgba(240,237,232,.35)}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
body{font-family:'Cairo',sans-serif;min-height:100vh;background:radial-gradient(ellipse at 20% 60%,rgba(201,168,76,.06) 0%,transparent 55%),#0A1628;direction:rtl;display:flex;flex-direction:column}

/* TOPBAR */
.tb{display:flex;align-items:center;justify-content:space-between;padding:14px 28px;border-bottom:1px solid var(--border)}
.tb-logo{display:flex;align-items:center;gap:10px;text-decoration:none}
.tb-ico{width:38px;height:38px;background:linear-gradient(135deg,var(--gold),var(--gold2));border-radius:10px;display:flex;align-items:center;justify-content:center;font-size:17px;color:#0A1628}
.tb-name{font-family:'Amiri',serif;font-size:19px;font-weight:700;color:#fff;display:block;line-height:1.1}
.tb-sub{font-size:10px;color:var(--txt2);display:block}
.tb-btn{display:flex;align-items:center;gap:6px;font-size:12px;color:var(--txt);text-decoration:none;padding:7px 14px;border-radius:8px;border:1px solid var(--border);background:var(--surf);transition:all .2s}
.tb-btn:hover{color:var(--gold);border-color:var(--border2)}

/* MAIN */
.main{flex:1;display:flex;align-items:center;justify-content:center;padding:50px 20px}

/* CHOOSE */
.choose{width:100%;max-width:520px;text-align:center}
.c-icon{width:60px;height:60px;background:linear-gradient(135deg,var(--gold),var(--gold2));border-radius:15px;display:flex;align-items:center;justify-content:center;font-size:26px;color:#0A1628;margin:0 auto 22px;box-shadow:0 8px 24px rgba(201,168,76,.3)}
.c-title{font-family:'Amiri',serif;font-size:32px;font-weight:700;color:#fff;margin-bottom:8px}
.c-sub{font-size:14px;color:var(--txt2);margin-bottom:32px}
.c-grid{display:grid;grid-template-columns:1fr 1fr;gap:14px;margin-bottom:22px}
.c-card{background:var(--card);border:1px solid var(--border);border-radius:18px;padding:26px 16px 20px;text-decoration:none;transition:all .25s;display:block}
.c-card:hover{border-color:var(--border2);transform:translateY(-4px);box-shadow:0 16px 40px rgba(0,0,0,.3)}
.cc-icon{width:50px;height:50px;border-radius:13px;display:flex;align-items:center;justify-content:center;font-size:22px;margin:0 auto 13px}
.cc-i1{background:rgba(201,168,76,.14);color:var(--gold)}
.cc-i2{background:rgba(26,95,138,.2);color:#60a5fa}
.cc-name{font-family:'Amiri',serif;font-size:17px;font-weight:700;color:#fff;display:block;margin-bottom:5px}
.cc-desc{font-size:11px;color:var(--txt);display:block;line-height:1.55;margin-bottom:14px}
.cc-btns{display:flex;gap:6px;justify-content:center;flex-wrap:wrap}
.cb{font-size:11px;font-weight:700;padding:7px 13px;border-radius:8px;text-decoration:none;border:none;cursor:pointer;font-family:'Cairo',sans-serif;transition:all .2s;display:inline-flex;align-items:center;gap:4px}
.cb-g{background:var(--gold);color:#0A1628}
.cb-g:hover{background:var(--gold2);color:#0A1628}
.cb-gh{background:rgba(255,255,255,.07);color:rgba(255,255,255,.75);border:1px solid rgba(255,255,255,.12)}
.cb-gh:hover{border-color:var(--border2);color:var(--gold)}
.c-note{font-size:12px;color:var(--txt2);padding:12px 16px;background:var(--surf);border-radius:10px;border:1px solid var(--border);text-align:right}

/* LOGIN BOX */
.lbox{width:100%;max-width:420px;background:var(--card);border:1px solid var(--border);border-radius:22px;padding:38px 34px;box-shadow:0 24px 60px rgba(0,0,0,.4)}
.lb-badge{display:inline-flex;align-items:center;gap:6px;border-radius:99px;padding:5px 14px;font-size:11px;font-weight:700;margin-bottom:18px}
.lb-b1{background:rgba(201,168,76,.12);border:1px solid rgba(201,168,76,.25);color:var(--gold)}
.lb-b2{background:rgba(26,95,138,.15);border:1px solid rgba(26,95,138,.3);color:#60a5fa}
.lb-b3{background:rgba(181,38,38,.12);border:1px solid rgba(181,38,38,.25);color:#f87171}
.lb-title{font-family:'Amiri',serif;font-size:25px;font-weight:700;color:#fff;margin-bottom:4px}
.lb-sub{font-size:13px;color:var(--txt);margin-bottom:24px}
.f-label{font-size:12px;font-weight:600;color:rgba(255,255,255,.65);margin-bottom:5px;display:block}
.f-group{display:flex;margin-bottom:14px}
.f-pre,.f-suf{padding:10px 12px;background:rgba(255,255,255,.05);border:1px solid var(--border);color:var(--txt2);font-size:14px;display:flex;align-items:center}
.f-pre{border-left:none;border-radius:10px 0 0 10px}
.f-suf{border-right:none;border-radius:0 10px 10px 0;cursor:pointer;transition:color .2s}
.f-suf:hover{color:var(--gold)}
.f-inp{flex:1;padding:10px 13px;border-radius:0;border:1px solid var(--border);border-right:none;border-left:none;background:rgba(255,255,255,.05);color:#fff;font-size:14px;font-family:'Cairo',sans-serif;outline:none;transition:border-color .2s}
.f-inp:focus{border-color:rgba(201,168,76,.4);background:rgba(255,255,255,.07)}
.f-inp.round{border-radius:10px;border-right:1px solid var(--border);border-left:1px solid var(--border)}
.f-inp.round:focus{border-color:rgba(201,168,76,.4)}
.f-sel{flex:1;padding:10px 13px;border-radius:0;border:1px solid var(--border);border-right:none;border-left:none;background:rgba(255,255,255,.05);color:#fff;font-size:14px;font-family:'Cairo',sans-serif;outline:none}
.f-sel option{background:#0A1628;color:#fff}
.btn-sub{width:100%;padding:12px;border-radius:11px;border:none;background:var(--gold);color:#0A1628;font-size:15px;font-weight:800;font-family:'Cairo',sans-serif;cursor:pointer;transition:all .2s;display:flex;align-items:center;justify-content:center;gap:8px;margin-top:6px}
.btn-sub:hover{background:var(--gold2);transform:translateY(-1px);box-shadow:0 8px 20px rgba(201,168,76,.35)}
.btn-sub.red{background:linear-gradient(135deg,#7B0000,#A52020);color:#fff}
.btn-sub.red:hover{background:linear-gradient(135deg,#8B0000,#B52626)}
.alert-e{display:flex;align-items:center;gap:9px;padding:10px 13px;border-radius:9px;font-size:13px;margin-bottom:16px;background:rgba(220,53,69,.1);border:1px solid rgba(220,53,69,.2);color:#f87171}
.alert-s{display:flex;align-items:center;gap:9px;padding:10px 13px;border-radius:9px;font-size:13px;margin-bottom:16px;background:rgba(74,222,128,.1);border:1px solid rgba(74,222,128,.2);color:#4ade80}
.back-l{text-align:center;margin-top:16px}
.back-l a{font-size:12px;color:var(--txt2);text-decoration:none;display:inline-flex;align-items:center;gap:5px;transition:color .2s}
.back-l a:hover{color:var(--gold)}

/* REGISTER */
.rbox{width:100%;max-width:540px;background:var(--card);border:1px solid var(--border);border-radius:22px;padding:36px 32px;box-shadow:0 24px 60px rgba(0,0,0,.4)}
.sect-hd{font-size:11px;font-weight:700;color:var(--gold);display:flex;align-items:center;gap:6px;margin:16px 0 11px;text-transform:uppercase;letter-spacing:.5px}

@media(max-width:600px){.c-grid{grid-template-columns:1fr}.main{padding:40px 14px}.lbox,.rbox{padding:28px 18px}}

/* Lang/Dark shared */
.lang-btn{font-size:11px;padding:5px 11px;border-radius:7px;border:1px solid var(--border);background:rgba(255,255,255,.05);color:var(--txt2);cursor:pointer;transition:all .2s;font-family:'Cairo',sans-serif;font-weight:600}
.lang-btn.active,.lang-btn:hover{background:rgba(201,168,76,.14);border-color:rgba(201,168,76,.3);color:var(--gold)}
.dark-btn{width:34px;height:34px;border-radius:8px;border:1px solid var(--border);background:rgba(255,255,255,.05);color:var(--txt2);cursor:pointer;display:flex;align-items:center;justify-content:center;font-size:15px;transition:all .2s}
.dark-btn:hover{border-color:rgba(201,168,76,.3);color:var(--gold)}
/* Light theme overrides for index.php */
[data-theme="light"]{--navy:#0A1628;--gold:#C9A84C;--border:rgba(13,27,42,.12);--txt2:rgba(13,27,42,.55)}
[data-theme="light"] body{background:#F7F4EF;color:#0A1628}
[data-theme="light"] .tb{background:#fff;border-color:rgba(13,27,42,.1)}
[data-theme="light"] .tb-name{color:#0A1628}
[data-theme="light"] .c-title{color:#0A1628}
[data-theme="light"] .c-card{background:#fff;border-color:rgba(13,27,42,.1);color:#0A1628}
[data-theme="light"] .cc-name{color:#0A1628}
[data-theme="light"] .cc-desc{color:rgba(13,27,42,.55)}
[data-theme="light"] .lbox,[data-theme="light"] .rbox{background:#fff;border-color:rgba(13,27,42,.1)}
[data-theme="light"] .lb-title{color:#0A1628}
[data-theme="light"] .f-inp{border-color:rgba(13,27,42,.15);background:#F7F4EF;color:#0A1628}
[data-theme="light"] .f-inp:focus{background:#fff;border-color:rgba(201,168,76,.5)}
[data-theme="light"] .f-pre,.f-suf{background:#F0EDE8;border-color:rgba(13,27,42,.15);color:rgba(13,27,42,.5)}
[data-theme="light"] .tb-btn{color:rgba(13,27,42,.6);border-color:rgba(13,27,42,.12);background:transparent}
[data-theme="light"] .tb-btn:hover{color:var(--gold)}
[data-theme="light"] .back-l a{color:rgba(13,27,42,.5)}
</style>
</head>
<body>

<!-- TOPBAR -->
<div class="tb">
  <a href="<?= $homeUrl ?>" class="tb-logo">
    <div class="tb-ico logo-svg-wrap"></div>
    <div><span class="tb-name">قضيتي</span><span class="tb-sub">نظام القضايا القضائية</span></div>
  </a>
  <div class="d-flex align-items-center gap-2">
    <button class="lang-btn active" data-lang="ar" onclick="applyLang('ar')">ع</button>
    <button class="lang-btn" data-lang="fr" onclick="applyLang('fr')">FR</button>
    <button class="lang-btn" data-lang="en" onclick="applyLang('en')">EN</button>
    <button class="dark-btn" onclick="toggleDark()"><i class="bi bi-moon dark-ico"></i></button>
    <?php if ($mode !== 'choose'): ?>
    <a href="<?= BASE_URL ?>/public/index.php" class="tb-btn" data-i18n="change_path">تغيير المسار</a>
    <?php endif; ?>
    <a href="<?= $homeUrl ?>" class="tb-btn" data-i18n="home">الرئيسية</a>
  </div>
</div>

<!-- CONTENT -->
<div class="main">

<?php if ($mode === 'choose'): ?>
<!-- ══ CHOOSE ══ -->
<div class="choose">
  <div class="c-icon">
    <svg width="28" height="28" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
      <line x1="16" y1="3" x2="16" y2="29" stroke="#0A1628" stroke-width="2.5" stroke-linecap="round"/>
      <line x1="6" y1="7" x2="26" y2="7" stroke="#0A1628" stroke-width="2.5" stroke-linecap="round"/>
      <line x1="6" y1="7" x2="2" y2="16" stroke="#0A1628" stroke-width="2" stroke-linecap="round"/>
      <line x1="6" y1="7" x2="10" y2="16" stroke="#0A1628" stroke-width="2" stroke-linecap="round"/>
      <path d="M1.5 16 Q6 19.5 10.5 16" stroke="#0A1628" stroke-width="2" fill="none" stroke-linecap="round"/>
      <line x1="26" y1="7" x2="22" y2="16" stroke="#0A1628" stroke-width="2" stroke-linecap="round"/>
      <line x1="26" y1="7" x2="30" y2="16" stroke="#0A1628" stroke-width="2" stroke-linecap="round"/>
      <path d="M21.5 16 Q26 19.5 30.5 16" stroke="#0A1628" stroke-width="2" fill="none" stroke-linecap="round"/>
      <line x1="10" y1="29" x2="22" y2="29" stroke="#0A1628" stroke-width="2.5" stroke-linecap="round"/>
    </svg>
  </div>
  <h1 class="c-title" data-i18n="choose_title">اختر مسارك</h1>
  <p class="c-sub" data-i18n="choose_sub">سجّل دخولك أو أنشئ حساباً حسب دورك في المنظومة القضائية</p>
  <div class="c-grid">
    <!-- أمين الضبط -->
    <div class="c-card">
      <div class="cc-icon cc-i1"><i class="bi bi-person-badge"></i></div>
      <span class="cc-name" data-i18n="clerk_name">أمين الضبط</span>
      <span class="cc-desc" data-i18n="clerk_desc">إدارة القضايا والجلسات والمحاضر والأحكام</span>
      <div class="cc-btns">
        <a href="?mode=login&path=clerk" class="cb cb-g"><i class="bi bi-box-arrow-in-right"></i> <span data-i18n="btn_login">دخول</span></a>
      </div>
    </div>
    <!-- المحامي -->
    <div class="c-card">
      <div class="cc-icon cc-i2"><i class="bi bi-briefcase"></i></div>
      <span class="cc-name" data-i18n="lawyer_name">المحامي</span>
      <span class="cc-desc" data-i18n="lawyer_desc">متابعة القضايا ورفع الوثائق والاعتماد</span>
      <div class="cc-btns">
        <a href="?mode=login&path=lawyer"    class="cb cb-g"><i class="bi bi-box-arrow-in-right"></i> <span data-i18n="btn_login">دخول</span></a>
        <a href="?mode=register&path=lawyer" class="cb cb-gh"><i class="bi bi-person-plus"></i> <span data-i18n="btn_register">تسجيل</span></a>
      </div>
    </div>
  </div>
  <div class="c-note">
    <i class="bi bi-info-circle" style="color:var(--gold);margin-left:6px"></i>
    <span data-i18n="choose_note">يمكن للمحامين إنشاء حساب جديد. أمين الضبط يحتاج حساباً مُعداً مسبقاً من الإدارة.</span>
  </div>
</div>

<?php elseif ($mode === 'login'): ?>
<!-- ══ LOGIN ══ -->
<?php
$badgeKeys = ['clerk'=>['lb-b1','person-badge','clerk_name'],'admin'=>['lb-b3','shield-lock','r3_name'],'lawyer'=>['lb-b2','briefcase','lawyer_name']];
$descKeys  = ['clerk'=>'desc_clerk','admin'=>'desc_admin','lawyer'=>'desc_lawyer'];
[$bc,$bi,$bnKey] = $badgeKeys[$path] ?? ['lb-b1','person','clerk_name'];
$isAdmin = ($path === 'admin');
$descKey = $descKeys[$path] ?? 'desc_clerk';
$submitKey = $isAdmin ? 'btn_submit_admin' : 'btn_submit_login';
$submitIcon = $isAdmin ? 'shield-lock' : 'box-arrow-in-right';
?>
<div class="lbox">
  <div class="lb-badge <?= $bc ?>"><i class="bi bi-<?= $bi ?>"></i><span data-i18n="<?= $bnKey ?>">مدير النظام</span></div>
  <div class="lb-title" data-i18n="login_welcome">مرحباً بعودتك</div>
  <p class="lb-sub" data-i18n="<?= $descKey ?>"></p>

  <?php if ($error): ?>
  <div class="alert-e"><i class="bi bi-exclamation-triangle-fill"></i><?= htmlspecialchars($error) ?></div>
  <?php endif; ?>
  <?php if ($successMsg): ?>
  <div class="alert-s"><i class="bi bi-check-circle-fill"></i><?= htmlspecialchars($successMsg) ?></div>
  <?php endif; ?>

  <form method="POST">
    <input type="hidden" name="action" value="login">
    <input type="hidden" name="path"   value="<?= htmlspecialchars($path) ?>">

    <label class="f-label" data-i18n="lbl_email">البريد الإلكتروني</label>
    <div class="f-group">
      <span class="f-pre"><i class="bi bi-envelope"></i></span>
      <input type="email" name="email" class="f-inp" placeholder="example@domain.com" value="<?= htmlspecialchars($_POST['email']??'') ?>" required>
    </div>

    <label class="f-label" data-i18n="lbl_pass">كلمة المرور</label>
    <div class="f-group">
      <span class="f-pre"><i class="bi bi-lock"></i></span>
      <input type="password" name="password" id="pInp" class="f-inp" placeholder="••••••••" required>
      <span class="f-suf" onclick="tp()"><i class="bi bi-eye" id="eIco"></i></span>
    </div>

    <button type="submit" class="btn-sub <?= $isAdmin ? 'red' : '' ?>">
      <i class="bi bi-<?= $submitIcon ?>"></i>
      <span data-i18n="<?= $submitKey ?>">تسجيل الدخول</span>
    </button>
  </form>

  <?php if (!$isAdmin && $path === 'lawyer'): ?>
  <div style="text-align:center;margin-top:14px">
    <a href="?mode=register&path=lawyer" style="font-size:13px;color:var(--gold);text-decoration:none">
      <i class="bi bi-person-plus"></i> <span data-i18n="new_lawyer">إنشاء حساب محامي جديد</span>
    </a>
  </div>
  <?php endif; ?>



  <div class="back-l">
    <a href="<?= BASE_URL ?>/public/index.php"><i class="bi bi-arrow-right"></i> <span data-i18n="back_choose">العودة لاختيار المسار</span></a>
  </div>
</div>

<?php elseif ($mode === 'register' && $path === 'lawyer'): ?>
<!-- ══ REGISTER ══ -->
<div class="rbox">
  <div class="lb-badge lb-b2"><i class="bi bi-person-plus"></i> <span data-i18n="lawyer_name">محامي</span></div>
  <div class="lb-title" data-i18n="reg_title">إنشاء حساب محامي</div>
  <p class="lb-sub" data-i18n="reg_sub">أدخل بياناتك المهنية للتسجيل في المنصة</p>

  <?php if ($error): ?>
  <div class="alert-e"><i class="bi bi-exclamation-triangle-fill"></i><?= htmlspecialchars($error) ?></div>
  <?php endif; ?>

  <form method="POST">
    <input type="hidden" name="action" value="register">
    <input type="hidden" name="path"   value="lawyer">

    <div class="sect-hd"><i class="bi bi-person"></i> <span data-i18n="sect_personal">البيانات الشخصية</span></div>
    <div class="row g-2 mb-1">
      <div class="col-md-6">
        <label class="f-label">الاسم الكامل *</label>
        <input type="text" name="full_name" class="f-inp round" style="width:100%" value="<?= htmlspecialchars($_POST['full_name']??'') ?>" required>
      </div>
      <div class="col-md-6">
        <label class="f-label">البريد الإلكتروني *</label>
        <input type="email" name="email" class="f-inp round" style="width:100%" value="<?= htmlspecialchars($_POST['email']??'') ?>" required>
      </div>
      <div class="col-12">
        <label class="f-label">الهاتف *</label>
        <input type="tel" name="phone" class="f-inp round" style="width:100%" value="<?= htmlspecialchars($_POST['phone']??'') ?>" required>
      </div>
    </div>

    <div class="sect-hd"><i class="bi bi-briefcase"></i> البيانات المهنية</div>
    <div class="row g-2 mb-1">
      <div class="col-md-6">
        <label class="f-label">رقم البطاقة المهنية *</label>
        <input type="text" name="bar_number" class="f-inp round" style="width:100%" value="<?= htmlspecialchars($_POST['bar_number']??'') ?>" required>
      </div>
      <div class="col-md-6">
        <label class="f-label">النقابة *</label>
        <?php $regBars = db()->query("SELECT id, name FROM bar_associations WHERE is_active=1 ORDER BY name")->fetchAll(); ?>
        <select name="bar_association_id" id="barAssocSelect" class="f-inp round" style="width:100%" required
                onchange="document.getElementById('barAssocText').value = this.options[this.selectedIndex].text">
          <option value="">— اختر النقابة —</option>
          <?php foreach ($regBars as $rb): ?>
          <option value="<?= $rb['id'] ?>" <?= (($_POST['bar_association_id']??'')==$rb['id'])?'selected':'' ?>><?= htmlspecialchars($rb['name']) ?></option>
          <?php endforeach; ?>
        </select>
        <input type="hidden" name="bar_association" id="barAssocText" value="<?= htmlspecialchars($_POST['bar_association']??'') ?>">
        <?php if (empty($regBars)): ?><small class="text-muted">لم تُضَف أي نقابة بعد من طرف الإدارة.</small><?php endif; ?>
      </div>
      <div class="col-12">
        <label class="f-label">التخصص *</label>
        <div class="f-group"><select name="specialization" class="f-sel" style="border-radius:10px;border:1px solid var(--border);border-right:1px solid var(--border);border-left:1px solid var(--border)" required>
          <option value="">اختر التخصص</option>
          <option value="الكل" <?= ($_POST['specialization']??'')==='الكل'?'selected':'' ?>>الكل (محامٍ عام في جميع القضايا)</option>
          <?php foreach(['مدني','جنائي','تجاري','إداري','أسري','عقاري'] as $sp): ?>
          <option value="<?= $sp ?>" <?= ($_POST['specialization']??'')===$sp?'selected':'' ?>><?= $sp ?></option>
          <?php endforeach; ?>
        </select></div>
      </div>
    </div>

    <div class="sect-hd"><i class="bi bi-lock"></i> بيانات الحساب</div>
    <div class="row g-2 mb-3">
      <div class="col-md-6">
        <label class="f-label">كلمة المرور *</label>
        <input type="password" name="password" class="f-inp round" style="width:100%" placeholder="8 أحرف على الأقل" required>
      </div>
      <div class="col-md-6">
        <label class="f-label">تأكيد كلمة المرور *</label>
        <input type="password" name="password2" class="f-inp round" style="width:100%" required>
      </div>
    </div>

    <button type="submit" class="btn-sub"><i class="bi bi-person-check"></i> إنشاء الحساب</button>
  </form>

  <div class="back-l">
    <a href="?mode=login&path=lawyer"><i class="bi bi-box-arrow-in-right"></i> لديك حساب؟ سجّل الدخول</a>
  </div>
</div>
<?php endif; ?>

</div><!-- /main -->

<script>
function tp(){var i=document.getElementById('pInp'),e=document.getElementById('eIco');
  i.type=i.type==='password'?'text':'password';e.className=i.type==='password'?'bi bi-eye':'bi bi-eye-slash';}
function fd(em){document.querySelector('[name=email]').value=em;document.querySelector('[name=password]').value='password';}
</script>
<script src="assets/js/i18n.js"></script>
</body>
</html>