// screen-profile.jsx — Profil v2 · design liste + sous-écrans

const BLOOD_TYPES_P = ['A+', 'A−', 'B+', 'B−', 'AB+', 'AB−', 'O+', 'O−'];
const GENDERS_P     = ['Masculin', 'Féminin', 'Autre'];
const AVATAR_COLORS = ['#1565C0', '#2E6B4F', '#C77A2B', '#6B4F8C', '#E53935'];

// ── Style input édition ──────────────────────────────────────────────────────
const PINP = {
  border: '1.5px solid var(--sm-blue)', outline: 'none',
  background: '#EEF4FF', fontSize: 15, fontFamily: 'inherit',
  color: 'var(--sm-ink)', width: '100%', borderRadius: 8, padding: '9px 11px',
  boxSizing: 'border-box',
};

// ── Calcul complétion profil (/ 12 points) ───────────────────────────────────
function calcCompletion(user, med) {
  const name = user.name || '';
  const parts = name.trim().split(/\s+/);
  const checks = [
    !!parts[0],                                // prénom
    !!(parts[1]),                              // nom
    !!user.birthdate,                          // date naissance
    !!user.gender,                             // sexe
    !!user.email,                              // email
    !!user.phone,                              // téléphone
    !!med.bloodType,                           // groupe sanguin
    !!med.height,                              // taille
    !!med.weight,                              // poids
    (med.allergies   || []).length > 0,        // allergies
    (med.conditions  || []).length > 0,        // antécédents
    (med.emergencyContacts || []).length > 0,  // contact d'urgence
  ];
  return Math.round(checks.filter(Boolean).length / 12 * 100);
}

// ── Composant : ligne de liste ───────────────────────────────────────────────
// hideChevron : pour une ligne d'interrupteur (Mode sombre) — le chevron
// suggère une navigation vers un sous-écran, trompeur pour un simple bouton
// bascule dont l'état se voit déjà dans `right` (ThemeSwitch).
function ListRow({ icon, iconBg, iconColor, label, right, badge, onClick, hideChevron }) {
  return (
    <button
      onClick={onClick}
      className="sm-row-divider"
      style={{
        width: '100%', display: 'flex', alignItems: 'center', gap: 14,
        padding: '14px 16px', background: 'none', border: 'none', textAlign: 'left',
        cursor: 'pointer',
      }}
    >
      <div style={{ position: 'relative', flexShrink: 0 }}>
        <div style={{ width: 36, height: 36, borderRadius: 10, background: iconBg, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
          <Icon name={icon} size={18} color={iconColor} strokeWidth={1.9} />
        </div>
        {badge && (
          <div style={{ position: 'absolute', top: -3, right: -3, width: 9, height: 9, borderRadius: '50%', background: 'var(--sm-red)', border: '1.5px solid white' }} />
        )}
      </div>
      <span style={{ flex: 1, fontSize: 15, fontWeight: 500, color: 'var(--sm-ink)', fontFamily: 'var(--font-ui)' }}>{label}</span>
      {right && <span style={{ fontSize: 13, color: 'var(--sm-ink-400)', fontFamily: 'var(--font-ui)', marginRight: 4 }}>{right}</span>}
      {!hideChevron && <Icon name="chevron-right" size={18} color="var(--sm-ink-400)" />}
    </button>
  );
}

// ── Composant : interrupteur (Mode sombre) ───────────────────────────────────
function ThemeSwitch({ checked }) {
  return (
    <div
      role="switch"
      aria-checked={checked}
      style={{
        width: 44, height: 26, borderRadius: 999, flexShrink: 0, position: 'relative',
        background: checked ? 'var(--sm-blue)' : 'var(--sm-line)',
        transition: 'background 0.2s ease',
      }}
    >
      {/* #FFFFFF (pas 'white') : le galet reste clair dans les deux thèmes —
          l'override CSS global de mode sombre cible spécifiquement le
          littéral 'white' (minuscules), pas cette valeur. */}
      <div style={{
        position: 'absolute', top: 2, left: checked ? 20 : 2,
        width: 22, height: 22, borderRadius: '50%', background: '#FFFFFF',
        boxShadow: '0 1px 3px rgba(0,0,0,0.35)', transition: 'left 0.2s ease',
      }} />
    </div>
  );
}

// ── Composant : sélecteur de langue (deux options, indication de la langue
// active) — au clic, bascule immédiatement toute l'app via window.SM_I18N.
function LangSwitch({ lang }) {
  // div[role=button], pas <button> : ce composant est utilisé comme `right`
  // d'un ListRow qui est LUI-MÊME un <button> — deux boutons imbriqués
  // seraient invalides en HTML (et le clic sur l'un déclencherait aussi
  // celui de l'autre).
  const pill = (code, native, active) => (
    <div
      key={code}
      role="button"
      tabIndex={0}
      onClick={(e) => { e.stopPropagation(); window.SM_I18N.set(code); }}
      style={{
        padding: '5px 12px', borderRadius: 999,
        cursor: 'pointer',
        fontSize: 12, fontWeight: 700, fontFamily: 'var(--font-ui)',
        background: active ? 'var(--sm-blue)' : 'var(--sm-paper-2)',
        color: active ? 'white' : 'var(--sm-ink-500)',
        transition: 'background 0.15s ease, color 0.15s ease',
      }}
    >
      {native}
    </div>
  );
  return (
    <div style={{ display: 'flex', gap: 6 }}>
      {pill('fr', t('common.lang_fr_native'), lang === 'fr')}
      {pill('en', t('common.lang_en_native'), lang === 'en')}
    </div>
  );
}

// ── En-tête sous-écran ───────────────────────────────────────────────────────
function SubHeader({ title, nav, right }) {
  return (
    <div style={{
      padding: '16px 20px 14px', display: 'flex', alignItems: 'center', gap: 12,
      borderBottom: '1px solid var(--sm-line)',
      background: 'linear-gradient(180deg, #f8f9fa, white)',
      flexShrink: 0,
    }}>
      <button onClick={() => goBack(nav)} style={{ background: 'none', border: 'none', padding: '4px', margin: '-4px', cursor: 'pointer' }}>
        <Icon name="arrow-left" size={22} color="var(--sm-ink)" />
      </button>
      <h1 className="sm-serif" style={{ fontSize: 20, flex: 1 }}>{title}</h1>
      {right}
    </div>
  );
}

// ── Barre sticky sauvegarde ──────────────────────────────────────────────────
function SaveBar({ onCancel, onSave, saving }) {
  const t = useTranslation();
  return (
    <div style={{ flexShrink: 0, background: 'white', borderTop: '1px solid var(--sm-line)', padding: '12px 16px 28px', display: 'flex', gap: 10 }}>
      <button onClick={onCancel} style={{ flex: 1, padding: '13px', borderRadius: 12, background: '#F1F2F4', color: 'var(--sm-ink)', border: 'none', fontWeight: 600, fontSize: 15, fontFamily: 'var(--font-ui)', cursor: 'pointer' }}>
        {t('common.cancel')}
      </button>
      <button onClick={onSave} disabled={saving} style={{ flex: 2, padding: '13px', borderRadius: 12, background: saving ? 'var(--sm-line)' : 'var(--sm-blue)', color: 'white', border: 'none', fontWeight: 700, fontSize: 15, fontFamily: 'var(--font-ui)', cursor: saving ? 'default' : 'pointer' }}>
        {saving ? t('common.saving') : t('common.save')}
      </button>
    </div>
  );
}

// ── Toast ────────────────────────────────────────────────────────────────────
// variant="danger" réutilisé pour les échecs d'enregistrement (voir les
// save() de ProfilePersonal/ProfileMedical/ProfileContacts ci-dessous) — même
// composant, jamais un toast de succès affiché à la place d'une erreur.
function ProfileToast({ msg, variant = 'success' }) {
  if (!msg) return null;
  return (
    <Banner
      variant={variant}
      icon={variant === 'danger' ? 'alert-circle' : 'check-circle-2'}
      text={msg}
      style={{ position: 'absolute', top: 70, left: 16, right: 16, zIndex: 300, boxShadow: '0 6px 24px rgba(0,0,0,0.2)' }}
    />
  );
}

// ── Logique notification partagée ────────────────────────────────────────────
function useNotifications() {
  const [notifs, setNotifs]       = useState([]);
  const [showNotifs, setShowNotifs] = useState(false);
  const unread = notifs.filter(n => !n.read).length;

  useEffect(() => {
    window.API.notifications().then(setNotifs).catch(() => {});
  }, []);

  const openNotifs = () => {
    setShowNotifs(true);
    notifs.filter(n => !n.read).forEach(n => window.API.markNotifRead(n.id).catch(() => {}));
    setNotifs(prev => prev.map(n => ({ ...n, read: true })));
  };

  return { notifs, showNotifs, setShowNotifs, unread, openNotifs };
}

// ── Bottom sheet : notifications ─────────────────────────────────────────────
function NotifSheet({ notifs, onClose }) {
  const t = useTranslation();
  return (
    <div style={{ position: 'fixed', inset: 0, zIndex: 200, display: 'flex', flexDirection: 'column', justifyContent: 'flex-end' }}>
      <div onClick={onClose} style={{ flex: 1, background: 'rgba(10,22,40,0.4)' }} />
      <div style={{ background: 'white', borderRadius: '20px 20px 0 0', maxHeight: '78%', display: 'flex', flexDirection: 'column', boxShadow: '0 -4px 30px rgba(0,0,0,0.12)' }}>
        <div style={{ width: 40, height: 4, borderRadius: 2, background: '#E0E0E0', margin: '12px auto 0', flexShrink: 0 }} />
        <div style={{ padding: '14px 20px 12px', display: 'flex', alignItems: 'center', justifyContent: 'space-between', flexShrink: 0, borderBottom: '1px solid var(--sm-line)' }}>
          <h2 className="sm-serif" style={{ fontSize: 18 }}>{t('home.notifications_title')}</h2>
          <button onClick={onClose} style={{ background: 'none', border: 'none', padding: '4px', cursor: 'pointer' }}>
            <Icon name="x" size={20} color="var(--sm-ink-400)" />
          </button>
        </div>
        <div style={{ flex: 1, overflowY: 'auto', padding: '12px 16px 32px' }}>
          {notifs.length === 0 ? (
            <div style={{ textAlign: 'center', padding: '36px 20px', color: 'var(--sm-ink-500)', fontSize: 14, fontFamily: 'var(--font-ui)' }}>{t('home.no_notifications')}</div>
          ) : notifs.map(n => (
            <div key={n.id} style={{ padding: '14px 16px', borderRadius: 'var(--sm-radius)', background: 'white', boxShadow: 'var(--sm-shadow)', marginBottom: 10, display: 'flex', gap: 12, alignItems: 'flex-start', opacity: n.read ? 0.65 : 1 }}>
              <div style={{ width: 42, height: 42, borderRadius: 12, background: 'var(--sm-red-soft)', display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}>
                <Icon name="siren" size={20} color="var(--sm-red)" />
              </div>
              <div style={{ flex: 1, minWidth: 0 }}>
                <div style={{ fontSize: 14, fontWeight: 600, color: 'var(--sm-ink)', lineHeight: 1.4, fontFamily: 'var(--font-ui)' }}>{n.message}</div>
                <div style={{ fontSize: 12, color: 'var(--sm-ink-500)', marginTop: 4 }}>
                  {new Date(n.createdAt).toLocaleString('fr-FR', { hour: '2-digit', minute: '2-digit', day: '2-digit', month: 'short' })}
                </div>
              </div>
              {!n.read && <div style={{ width: 8, height: 8, borderRadius: '50%', background: 'var(--sm-red)', flexShrink: 0, marginTop: 5 }} />}
            </div>
          ))}
        </div>
      </div>
    </div>
  );
}

// ════════════════════════════════════════════════════════════════════════════
// ÉCRAN PRINCIPAL — PROFIL (liste)
// ════════════════════════════════════════════════════════════════════════════
function ProfileScreen({ nav }) {
  useLucide();
  const SM = window.useSM();
  const theme = useTheme();
  const lang = useLang();
  const t = useTranslation();

  const { notifs, showNotifs, setShowNotifs, unread, openNotifs } = useNotifications();
  const [showLogout, setShowLogout] = useState(false);
  const [showPw, setShowPw]         = useState(false);
  const [pwForm, setPwForm]         = useState({ old: '', next: '', confirm: '' });
  const [pwLoading, setPwLoading]   = useState(false);
  const [pwMsg, setPwMsg]           = useState('');
  const [toast, setToast]           = useState('');
  const [error, setError]           = useState('');
  const photoRef = useRef(null);

  const user = SM.user || {};
  const med  = user.medicalRecord || {};
  const completion   = calcCompletion(user, med);
  const contactCount = (med.emergencyContacts || []).length;
  const initials     = (user.name || 'SM').split(' ').map(w => w[0] || '').join('').toUpperCase().slice(0, 2);
  const avatarBg     = AVATAR_COLORS[(initials.charCodeAt(0) || 0) % AVATAR_COLORS.length];
  const medIncomplete = !med.bloodType || !(med.allergies || []).length;

  // ── Photo upload ──────────────────────────────────────────────────────────
  // Même règle que ProfilePersonal/ProfileMedical/ProfileContacts.save() (voir
  // LOT 5) : état local / localStorage mis à jour uniquement APRÈS confirmation
  // serveur, avec la réponse du serveur elle-même — jamais une valeur optimiste
  // calculée en local avant même l'appel réseau. C'était exactement le même
  // découplage (ici pire : la mutation locale précédait même le lancement de
  // l'appel réseau, dont l'échec était en plus avalé par .catch(() => {})) qui
  // avait rendu invisible le bug d'enregistrement du carnet médical.
  function handlePhoto(e) {
    const file = e.target.files[0];
    if (!file) return;
    const img = new Image();
    img.onload = async () => {
      const canvas = document.createElement('canvas');
      const max = 200, scale = Math.min(max / img.width, max / img.height, 1);
      canvas.width  = Math.round(img.width  * scale);
      canvas.height = Math.round(img.height * scale);
      canvas.getContext('2d').drawImage(img, 0, 0, canvas.width, canvas.height);
      const b64 = canvas.toDataURL('image/jpeg', 0.82);
      setError('');
      try {
        const res = await window.API.updateMe({ photo: b64 });
        window.SM.user = res;
        localStorage.setItem('sm_user', JSON.stringify(res));
        window.SM.emit();
      } catch (err) {
        setError(t('profile.save_failed'));
        setTimeout(() => setError(''), 4000);
      }
    };
    img.src = URL.createObjectURL(file);
    e.target.value = '';
  }

  // ── Déconnexion ───────────────────────────────────────────────────────────
  function logout() {
    localStorage.removeItem('sm_token');
    localStorage.removeItem('sm_refresh_token');
    localStorage.removeItem('sm_expires_at');
    localStorage.removeItem('sm_user');
    window.SM.token = null; window.SM.user = null; window.SM.home = null;
    window.SM.emit(); nav.reset('auth');
  }

  // ── Changement de mot de passe ────────────────────────────────────────────
  async function changePassword() {
    if (!pwForm.next || pwForm.next.length < 6) return setPwMsg(t('profile.password_min_error'));
    if (pwForm.next !== pwForm.confirm)        return setPwMsg(t('profile.password_mismatch'));
    setPwLoading(true); setPwMsg('');
    try {
      const base = window.API?.base || '';
      const token = window.SM?.token;
      const res  = await fetch(base + '/api/auth/change-password', {
        method: 'POST',
        headers: {
          'content-type': 'application/json',
          ...(token ? { authorization: 'Bearer ' + token } : {}),
        },
        body: JSON.stringify({ oldPassword: pwForm.old, newPassword: pwForm.next }),
      });
      const data = await res.json();
      setPwMsg(res.ok ? t('profile.password_changed') : (data.error || t('common.error')));
      if (res.ok) { setPwForm({ old: '', next: '', confirm: '' }); setTimeout(() => setShowPw(false), 1200); }
    } catch { setPwMsg(t('common.network_error')); }
    finally { setPwLoading(false); }
  }

  const showToast = msg => { setToast(msg); setTimeout(() => setToast(''), 2500); };

  return (
    <div style={{ position: 'absolute', inset: 0, background: 'var(--sm-paper)', display: 'flex', flexDirection: 'column', overflow: 'hidden' }}>

      {/* ── En-tête ────────────────────────────────────────────────────── */}
      <div style={{ padding: '16px 20px 14px', display: 'flex', alignItems: 'center', gap: 12, background: 'linear-gradient(180deg, #f8f9fa, white)', borderBottom: '1px solid var(--sm-line)', flexShrink: 0 }}>
        <img src="logo_80.png" alt="Sauv'Moi" style={{ width: 32, height: 32, objectFit: 'contain', borderRadius: 8 }} />
        <h1 className="sm-serif" style={{ fontSize: 20, flex: 1 }}>{t('profile.title')}</h1>
        {/* Cloche notifications */}
        <button onClick={openNotifs} style={{ position: 'relative', background: 'none', border: 'none', padding: '6px', cursor: 'pointer' }}>
          <Icon name="bell" size={22} color="var(--sm-ink)" />
          {unread > 0 && (
            <div style={{ position: 'absolute', top: 2, right: 2, width: 16, height: 16, borderRadius: '50%', background: 'var(--sm-red)', color: 'white', fontSize: 9, fontWeight: 700, display: 'flex', alignItems: 'center', justifyContent: 'center', border: '1.5px solid white', fontFamily: 'var(--font-ui)' }}>
              {unread > 9 ? '9+' : unread}
            </div>
          )}
        </button>
      </div>

      {/* ── Corps scrollable ────────────────────────────────────────────── */}
      <div style={{ flex: 1, overflowY: 'auto', padding: '16px 16px 32px', display: 'flex', flexDirection: 'column', gap: 12 }}>

        {/* Erreur photo — EN LIGNE en tête du corps (jamais un overlay
            position:absolute à décalage fixe deviné, voir le correctif du
            lot 8) : c'est le tout premier élément visible, juste au-dessus
            de l'avatar que l'utilisateur vient de toucher, sans dépendre
            d'un défilement ni d'une hauteur de header à deviner. */}
        {error && <Banner variant="danger" icon="alert-circle" text={error} />}

        {/* ── Carte profil ─────────────────────────────────────────────── */}
        <div style={{ background: 'white', borderRadius: 'var(--sm-radius)', boxShadow: 'var(--sm-shadow)', padding: '18px 18px 20px' }}>
          <input ref={photoRef} type="file" accept="image/*" style={{ display: 'none' }} onChange={handlePhoto} />
          <div style={{ display: 'flex', alignItems: 'center', gap: 16 }}>
            {/* Avatar cliquable → upload photo */}
            <button onClick={() => photoRef.current?.click()} style={{ position: 'relative', background: 'none', border: 'none', cursor: 'pointer', padding: 0, flexShrink: 0 }}>
              {user.photo ? (
                <img src={user.photo} alt="avatar" style={{ width: 70, height: 70, borderRadius: '50%', objectFit: 'cover', border: '3px solid white', boxShadow: '0 2px 12px rgba(0,0,0,0.15)' }} />
              ) : (
                <div style={{ width: 70, height: 70, borderRadius: '50%', background: `linear-gradient(135deg, #1565C0, #0D47A1)`, display: 'flex', alignItems: 'center', justifyContent: 'center', border: '3px solid white', boxShadow: '0 2px 12px rgba(21,101,192,0.3)' }}>
                  <span style={{ fontSize: 24, fontWeight: 700, color: 'white', fontFamily: 'var(--font-ui)' }}>{initials}</span>
                </div>
              )}
              <div style={{ position: 'absolute', bottom: 1, right: 1, width: 22, height: 22, borderRadius: '50%', background: 'var(--sm-ink)', display: 'flex', alignItems: 'center', justifyContent: 'center', border: '2px solid white' }}>
                <Icon name="camera" size={11} color="white" />
              </div>
            </button>
            {/* Nom + téléphone */}
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ fontSize: 17, fontWeight: 700, color: 'var(--sm-ink)', fontFamily: 'var(--font-ui)', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>
                {user.name || t('profile.default_user_name')}
              </div>
              <div style={{ fontSize: 13, color: 'var(--sm-ink-500)', marginTop: 3, fontFamily: 'var(--font-ui)' }}>
                {user.phone || user.email || '—'}
              </div>
            </div>
          </div>

          {/* Barre de progression / badge complet — mutuellement exclusifs :
              en dessous de 100%, libellé + pourcentage + barre ; à 100%,
              seul le badge "Profil complet" (plus de libellé redondant). */}
          <div style={{ marginTop: 18 }}>
            <div style={{ display: 'flex', alignItems: 'center', justifyContent: completion < 100 ? 'space-between' : 'flex-end', marginBottom: completion < 100 ? 8 : 0 }}>
              {completion < 100 ? (
                <>
                  <span style={{ fontSize: 13, color: 'var(--sm-ink-500)', fontFamily: 'var(--font-ui)' }}>{t('profile.completion_label')}</span>
                  <span style={{ fontSize: 13, fontWeight: 700, color: '#27500A', fontFamily: 'var(--font-ui)' }}>{completion}%</span>
                </>
              ) : (
                <div style={{ background: '#EAF3DE', borderRadius: 999, padding: '4px 12px', display: 'flex', alignItems: 'center', gap: 5 }}>
                  <Icon name="check" size={13} color="#27500A" strokeWidth={2.5} />
                  <span style={{ fontSize: 12, fontWeight: 700, color: '#27500A', fontFamily: 'var(--font-ui)' }}>{t('profile.profile_complete')}</span>
                </div>
              )}
            </div>
            {completion < 100 && (
              <div style={{ height: 7, borderRadius: 999, background: '#F1F2F4', overflow: 'hidden' }}>
                <div style={{ height: '100%', width: completion + '%', borderRadius: 999, background: 'linear-gradient(90deg, #639922, #27500A)', transition: 'width 0.6s ease' }} />
              </div>
            )}
          </div>
        </div>

        {/* ── Carte sections ───────────────────────────────────────────── */}
        <div style={{ background: 'white', borderRadius: 'var(--sm-radius)', boxShadow: 'var(--sm-shadow)', overflow: 'hidden' }}>
          <ListRow
            icon="user" iconBg="#EBF5FB" iconColor="#1565C0"
            label={t('profile.personal_info')}
            onClick={() => nav.go('profile_personal')}
          />
          <ListRow
            icon="briefcase-medical" iconBg="#FDEDEC" iconColor="#C0392B"
            label={t('profile.medical_profile')}
            badge={medIncomplete}
            right={medIncomplete ? <span style={{ fontSize: 12, fontWeight: 600, color: '#D85A30', fontFamily: 'var(--font-ui)' }}>{t('profile.to_complete')}</span> : null}
            onClick={() => nav.go('profile_medical')}
          />
          <ListRow
            icon="phone" iconBg="#F1F2F4" iconColor="var(--sm-ink)"
            label={t('profile.emergency_contacts')}
            right={contactCount + '/5'}
            onClick={() => nav.go('profile_contacts')}
          />
          <ListRow
            icon="qr-code" iconBg="#F1F2F4" iconColor="var(--sm-ink)"
            label={t('profile.qr_medical_code')}
            right={
              <div style={{ width: 22, height: 22, borderRadius: 5, background: 'var(--sm-ink)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
                <Icon name="qr-code" size={13} color="white" />
              </div>
            }
            onClick={() => nav.go('qr_code')}
          />
          <ListRow
            icon="key" iconBg="#F1F2F4" iconColor="var(--sm-ink)"
            label={t('profile.change_password')}
            onClick={() => { setShowPw(true); setPwMsg(''); }}
          />
          <ListRow
            icon="file-text" iconBg="#F1F2F4" iconColor="var(--sm-ink)"
            label={t('terms.header_title')}
            onClick={() => nav.go('terms')}
          />
          <ListRow
            icon="globe" iconBg="#F1F2F4" iconColor="var(--sm-ink)"
            label={t('profile.language')}
            right={<LangSwitch lang={lang} />}
            hideChevron
          />
          <ListRow
            icon={theme === 'dark' ? 'moon' : 'sun'} iconBg="#F1F2F4" iconColor="var(--sm-ink)"
            label={t('profile.dark_mode')}
            right={<ThemeSwitch checked={theme === 'dark'} />}
            onClick={() => window.SM_THEME.toggle()}
            hideChevron
          />
        </div>

        {/* ── Carte déconnexion ────────────────────────────────────────── */}
        <div style={{ background: 'white', borderRadius: 'var(--sm-radius)', boxShadow: 'var(--sm-shadow)', overflow: 'hidden' }}>
          <button onClick={() => setShowLogout(true)} style={{ width: '100%', display: 'flex', alignItems: 'center', gap: 14, padding: '14px 16px', background: 'none', border: 'none', cursor: 'pointer', textAlign: 'left' }}>
            <div style={{ width: 36, height: 36, borderRadius: 10, background: '#FDEDEC', display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}>
              <Icon name="log-out" size={18} color="#C0392B" strokeWidth={1.9} />
            </div>
            <span style={{ flex: 1, fontSize: 15, fontWeight: 700, color: '#C0392B', fontFamily: 'var(--font-ui)' }}>{t('profile.logout')}</span>
          </button>
        </div>
      </div>

      <FloatingChatButton nav={nav} />

      {/* ── Barre de navigation ──────────────────────────────────────────── */}
      <HomeTabBar active="profile" nav={nav} />

      {/* ── Bottom sheet : mot de passe ──────────────────────────────────── */}
      {showPw && (
        <div style={{ position: 'fixed', inset: 0, zIndex: 150, display: 'flex', flexDirection: 'column', justifyContent: 'flex-end' }}>
          <div onClick={() => setShowPw(false)} style={{ flex: 1, background: 'rgba(10,22,40,0.4)' }} />
          <div style={{ background: 'white', borderRadius: '20px 20px 0 0', padding: '20px 20px 36px', boxShadow: '0 -4px 30px rgba(0,0,0,0.12)' }}>
            <div style={{ width: 40, height: 4, borderRadius: 2, background: '#E0E0E0', margin: '0 auto 18px' }} />
            <h3 className="sm-serif" style={{ fontSize: 19, marginBottom: 16 }}>{t('profile.change_password')}</h3>
            <div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
              {[{ label: t('profile.current_password'), key: 'old', ph: '••••••••' }, { label: t('profile.new_password'), key: 'next', ph: t('profile.new_password_hint') }, { label: t('profile.confirm_password'), key: 'confirm', ph: '••••••••' }].map(f => (
                <div key={f.key}>
                  <label style={{ fontSize: 12, color: 'var(--sm-ink-500)', fontWeight: 600, fontFamily: 'var(--font-ui)', display: 'block', marginBottom: 5 }}>{f.label}</label>
                  <input type="password" value={pwForm[f.key]} onChange={e => setPwForm(p => ({ ...p, [f.key]: e.target.value }))} placeholder={f.ph} style={{ ...PINP, border: '1.5px solid var(--sm-line)', background: 'white' }} />
                </div>
              ))}
              {pwMsg && <Banner variant={pwMsg.includes('✓') ? 'success' : 'danger'} icon={pwMsg.includes('✓') ? 'check-circle-2' : 'alert-circle'} text={pwMsg} />}
              <button onClick={changePassword} disabled={pwLoading} style={{ padding: '13px', borderRadius: 12, background: 'var(--sm-blue)', color: 'white', border: 'none', fontWeight: 700, fontSize: 15, fontFamily: 'var(--font-ui)', cursor: pwLoading ? 'default' : 'pointer' }}>
                {pwLoading ? t('profile.password_changing') : t('profile.password_change_button')}
              </button>
            </div>
          </div>
        </div>
      )}

      {/* ── Bottom sheet : notifications ─────────────────────────────────── */}
      {showNotifs && <NotifSheet notifs={notifs} onClose={() => setShowNotifs(false)} />}

      {/* ── Modal déconnexion ─────────────────────────────────────────────── */}
      {showLogout && (
        <div style={{ position: 'fixed', inset: 0, background: 'rgba(10,22,40,0.5)', display: 'flex', alignItems: 'flex-end', zIndex: 200 }}>
          <div style={{ width: '100%', background: 'white', borderRadius: '20px 20px 0 0', padding: '28px 20px 40px' }}>
            <div style={{ width: 40, height: 4, borderRadius: 2, background: 'var(--sm-line)', margin: '0 auto 22px' }} />
            <div style={{ width: 56, height: 56, borderRadius: '50%', background: '#FDEDEC', display: 'flex', alignItems: 'center', justifyContent: 'center', margin: '0 auto 14px' }}>
              <Icon name="log-out" size={26} color="#C0392B" />
            </div>
            <h3 className="sm-serif" style={{ fontSize: 20, textAlign: 'center', marginBottom: 8 }}>{t('profile.logout_confirm_title')}</h3>
            <p style={{ fontSize: 14, color: 'var(--sm-ink-500)', textAlign: 'center', marginBottom: 24, fontFamily: 'var(--font-ui)', lineHeight: 1.5 }}>
              {t('profile.logout_confirm_body')}
            </p>
            <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
              <button onClick={logout} style={{ padding: '14px', borderRadius: 12, background: '#C0392B', color: 'white', border: 'none', fontWeight: 700, fontSize: 16, fontFamily: 'var(--font-ui)', cursor: 'pointer' }}>
                {t('profile.logout_confirm_button')}
              </button>
              <button onClick={() => setShowLogout(false)} style={{ padding: '14px', borderRadius: 12, background: '#F1F2F4', color: 'var(--sm-ink)', border: 'none', fontWeight: 600, fontSize: 16, fontFamily: 'var(--font-ui)', cursor: 'pointer' }}>
                {t('common.cancel')}
              </button>
            </div>
          </div>
        </div>
      )}

      <ProfileToast msg={toast} />
    </div>
  );
}

// ════════════════════════════════════════════════════════════════════════════
// SOUS-ÉCRAN : INFORMATIONS PERSONNELLES
// ════════════════════════════════════════════════════════════════════════════
function ProfilePersonal({ nav }) {
  useLucide();
  const t = useTranslation();
  const lang = useLang();
  const SM = window.useSM();
  const user = SM.user || {};

  const [editing, setEditing] = useState(false);
  const [form, setForm]       = useState(null);
  const [saving, setSaving]   = useState(false);
  const [toast, setToast]     = useState('');
  const [error, setError]     = useState('');

  const nameParts = (user.name || '').trim().split(/\s+/);
  const prenom = nameParts[0] || '';
  const nom    = nameParts.slice(1).join(' ');

  function startEdit() {
    setForm({ prenom, nom, birthdate: user.birthdate || '', gender: user.gender || '', email: user.email || '', phone: user.phone || '' });
    setEditing(true);
  }
  function cancelEdit() { setEditing(false); setForm(null); }
  const setF = k => v => setForm(f => ({ ...f, [k]: v }));

  async function save() {
    if (!form) return;
    setError('');
    // Même règle que l'inscription (screen-auth.jsx) et le serveur
    // (routes/auth.js, isNotFutureDate) : une date de naissance dans le
    // futur est toujours une erreur de saisie. Vérifiée ici AVANT l'appel
    // réseau pour un message clair et immédiat plutôt que le "Échec de
    // l'enregistrement" générique du catch plus bas.
    if (form.birthdate && form.birthdate > new Date().toISOString().slice(0, 10)) {
      setError(t('profile.error_birthdate_future'));
      setTimeout(() => setError(''), 4000);
      return;
    }
    setSaving(true);
    const name = [form.prenom.trim(), form.nom.trim()].filter(Boolean).join(' ');
    const payload = { name, birthdate: form.birthdate, gender: form.gender, phone: form.phone.trim() };
    // L'état local / localStorage ne sont mis à jour qu'APRÈS confirmation du
    // serveur, avec la réponse du serveur elle-même (source de vérité) — pas
    // une valeur calculée en local qui pourrait diverger de ce qui a
    // réellement été écrit. Un échec (réseau ou réponse d'erreur) ne touche
    // jamais l'état local ni le localStorage et affiche un Banner d'erreur au
    // lieu du toast de succès.
    try {
      const res = await window.API.updateMe(payload);
      window.SM.user = res;
      localStorage.setItem('sm_user', JSON.stringify(res));
      window.SM.emit();
      setSaving(false); setEditing(false); setForm(null);
      setToast(t('profile.updated_personal'));
      setTimeout(() => setToast(''), 2500);
    } catch (e) {
      setSaving(false);
      setError(t('profile.save_failed'));
      setTimeout(() => setError(''), 4000);
    }
  }

  const Val = ({ v }) => (
    <span style={{ fontSize: 15, color: v ? 'var(--sm-ink)' : 'var(--sm-ink-400)', fontFamily: 'var(--font-ui)' }}>
      {v || '—'}
    </span>
  );

  const rows = editing ? null : [
    { label: t('profile.first_name'),         val: prenom           },
    { label: t('profile.last_name'),          val: nom               },
    { label: t('common.birthdate_label'),     val: formatDate(user.birthdate, lang) },
    { label: t('profile.gender'),             val: user.gender ? genderLabel(user.gender) : '' },
    { label: t('profile.email'),              val: user.email        },
    { label: t('profile.phone'),              val: user.phone        },
  ];

  return (
    <div style={{ position: 'absolute', inset: 0, background: 'var(--sm-paper)', display: 'flex', flexDirection: 'column', overflow: 'hidden' }}>
      <SubHeader
        title={t('profile.personal_info')}
        nav={nav}
        right={!editing && (
          <button onClick={startEdit} style={{ padding: '7px 16px', borderRadius: 8, background: 'var(--sm-blue)', color: 'white', border: 'none', fontSize: 14, fontWeight: 600, fontFamily: 'var(--font-ui)', cursor: 'pointer' }}>
            {t('common.edit')}
          </button>
        )}
      />

      <div style={{ flex: 1, overflowY: 'auto', padding: '16px' }}>
        <div style={{ background: 'white', borderRadius: 'var(--sm-radius)', boxShadow: 'var(--sm-shadow)', overflow: 'hidden' }}>

          {/* ── Mode lecture ── */}
          {!editing && rows.map((r, i) => (
            <div key={r.label} className="sm-row-divider" style={{ display: 'flex', alignItems: 'center', gap: 12, padding: '13px 16px' }}>
              <span style={{ fontSize: 13, color: 'var(--sm-ink-500)', width: 130, flexShrink: 0, fontFamily: 'var(--font-ui)' }}>{r.label}</span>
              <Val v={r.val} />
            </div>
          ))}

          {/* ── Mode édition ── */}
          {editing && (
            <>
              <div className="sm-row-divider" style={{ display: 'grid', gridTemplateColumns: '1fr 1fr' }}>
                {[{ label: t('profile.first_name'), key: 'prenom', ph: 'Aïcha' }, { label: t('profile.last_name'), key: 'nom', ph: 'Kouassi' }].map((f, i) => (
                  <div key={f.key} style={{ padding: '12px 14px', borderRight: i === 0 ? '0.5px solid var(--sm-line)' : 'none', background: '#F4F8FF' }}>
                    <div style={{ fontSize: 12, color: 'var(--sm-blue)', fontWeight: 600, fontFamily: 'var(--font-ui)', marginBottom: 6 }}>{f.label}</div>
                    <input value={form[f.key]} onChange={e => setF(f.key)(e.target.value)} placeholder={f.ph} style={{ ...PINP }} />
                  </div>
                ))}
              </div>
              <div className="sm-row-divider" style={{ padding: '12px 16px', background: '#F4F8FF' }}>
                <BirthdateField
                  value={form.birthdate}
                  onChange={v => setF('birthdate')(v)}
                  labelStyle={{ fontSize: 12, color: 'var(--sm-blue)', fontWeight: 600, fontFamily: 'var(--font-ui)' }}
                  inputStyle={{ ...PINP }}
                />
              </div>
              <div className="sm-row-divider" style={{ padding: '12px 16px', background: '#F4F8FF' }}>
                <div style={{ fontSize: 12, color: 'var(--sm-blue)', fontWeight: 600, fontFamily: 'var(--font-ui)', marginBottom: 8 }}>{t('profile.gender')}</div>
                <div style={{ display: 'flex', gap: 8 }}>
                  {GENDERS_P.map(g => (
                    <button key={g} type="button" onClick={() => setF('gender')(g)} style={{ flex: 1, padding: '9px 4px', borderRadius: 8, fontSize: 13, fontFamily: 'var(--font-ui)', cursor: 'pointer', background: form.gender === g ? 'var(--sm-blue)' : 'white', color: form.gender === g ? 'white' : 'var(--sm-ink)', border: `1.5px solid ${form.gender === g ? 'var(--sm-blue)' : 'var(--sm-line)'}` }}>
                      {genderLabel(g)}
                    </button>
                  ))}
                </div>
              </div>
              <div className="sm-row-divider" style={{ padding: '12px 16px', background: '#F4F8FF' }}>
                <div style={{ fontSize: 12, color: 'var(--sm-blue)', fontWeight: 600, fontFamily: 'var(--font-ui)', marginBottom: 6 }}>{t('profile.email')}</div>
                <input type="email" value={form.email} onChange={e => setF('email')(e.target.value)} placeholder={t('auth.email_placeholder')} style={{ ...PINP }} />
              </div>
              <div style={{ padding: '12px 16px', background: '#F4F8FF' }}>
                <div style={{ fontSize: 12, color: 'var(--sm-blue)', fontWeight: 600, fontFamily: 'var(--font-ui)', marginBottom: 6 }}>{t('profile.phone')}</div>
                <input type="tel" value={form.phone} onChange={e => setF('phone')(e.target.value)} placeholder={t('profile.phone_placeholder')} style={{ ...PINP }} />
              </div>
            </>
          )}
        </div>
      </div>

      {/* Erreur EN LIGNE juste au-dessus de la barre Annuler/Sauvegarder —
          jamais un overlay position:absolute à décalage deviné (voir le
          correctif du lot 8) : toujours visible sans défiler, exactement là
          où l'œil se trouve au moment de cliquer sur Sauvegarder. */}
      {error && (
        <div style={{ padding: '0 16px 12px', flexShrink: 0 }}>
          <Banner variant="danger" icon="alert-circle" text={error} />
        </div>
      )}
      {editing && <SaveBar onCancel={cancelEdit} onSave={save} saving={saving} />}
      <ProfileToast msg={toast} />
    </div>
  );
}

// ════════════════════════════════════════════════════════════════════════════
// SOUS-ÉCRAN : PROFIL MÉDICAL
// ════════════════════════════════════════════════════════════════════════════
function ProfileMedical({ nav }) {
  useLucide();
  const t = useTranslation();
  const SM = window.useSM();
  const user = SM.user || {};
  const med  = user.medicalRecord || {};

  const [editing, setEditing] = useState(false);
  const [form, setForm]       = useState(null);
  const [saving, setSaving]   = useState(false);
  const [toast, setToast]     = useState('');
  const [error, setError]     = useState('');

  // ── Justificatif de groupe sanguin (lot 8) — indépendant du mode édition
  // ci-dessus : joindre/remplacer/retirer un justificatif est une action à
  // part, pas un champ du formulaire. Même découplage que save() plus bas :
  // window.SM.user n'est mis à jour qu'avec la réponse du serveur, jamais
  // avant ; erreur affichée EN LIGNE (proofError), jamais un overlay perdu.
  const [proofUploading, setProofUploading] = useState(false);
  const [proofError, setProofError] = useState('');
  const proofRef = useRef(null);

  function applyProofResult(data) {
    const updatedUser = {
      ...window.SM.user,
      medicalRecord: { ...window.SM.user.medicalRecord, bloodTypeStatus: data.bloodTypeStatus, hasBloodTypeProof: data.hasBloodTypeProof },
    };
    window.SM.user = updatedUser;
    localStorage.setItem('sm_user', JSON.stringify(updatedUser));
    window.SM.emit();
  }

  async function handleProofFile(e) {
    const file = e.target.files[0];
    e.target.value = '';
    if (!file) return;
    setProofError('');
    setProofUploading(true);
    try {
      const dataUrl = await new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.onload = () => resolve(reader.result);
        reader.onerror = () => reject(new Error('lecture du fichier échouée'));
        reader.readAsDataURL(file);
      });
      const base = window.API?.base || '';
      const token = window.SM?.token;
      const res = await fetch(base + '/api/medical-record/blood-type-proof', {
        method: 'POST',
        headers: { 'content-type': 'application/json', ...(token ? { authorization: 'Bearer ' + token } : {}) },
        body: JSON.stringify({ file: dataUrl }),
      });
      const data = await res.json();
      if (!res.ok) throw new Error(data.error || t('profile.blood_type_proof_upload_failed'));
      applyProofResult(data);
    } catch (err) {
      setProofError(err.message || t('profile.blood_type_proof_upload_failed'));
      setTimeout(() => setProofError(''), 5000);
    }
    setProofUploading(false);
  }

  async function removeProof() {
    setProofError('');
    setProofUploading(true);
    try {
      const base = window.API?.base || '';
      const token = window.SM?.token;
      const res = await fetch(base + '/api/medical-record/blood-type-proof', {
        method: 'DELETE',
        headers: { ...(token ? { authorization: 'Bearer ' + token } : {}) },
      });
      const data = await res.json();
      if (!res.ok) throw new Error(data.error || t('profile.blood_type_proof_remove_failed'));
      applyProofResult(data);
    } catch (err) {
      setProofError(err.message || t('profile.blood_type_proof_remove_failed'));
      setTimeout(() => setProofError(''), 5000);
    }
    setProofUploading(false);
  }

  function startEdit() {
    setForm({
      bloodType:  med.bloodType  || '',
      height:     med.height     ? String(med.height) : '',
      weight:     med.weight     ? String(med.weight) : '',
      allergies:  (med.allergies  || []).join(', '),
      conditions: (med.conditions || []).join(', '),
    });
    setEditing(true);
  }
  function cancelEdit() { setEditing(false); setForm(null); }
  const setF = k => v => setForm(f => ({ ...f, [k]: v }));

  async function save() {
    if (!form) return;
    setSaving(true);
    setError('');
    const medPayload = {
      bloodType:  form.bloodType,
      height:     form.height     ? Number(form.height)  : null,
      weight:     form.weight     ? Number(form.weight)  : null,
      allergies:  form.allergies.split(',').map(a => a.trim()).filter(Boolean),
      conditions: form.conditions.split(',').map(c => c.trim()).filter(Boolean),
    };
    // Même règle que ProfilePersonal.save() : état local / localStorage
    // uniquement après confirmation serveur, avec la réponse du serveur
    // elle-même — jamais une valeur optimiste calculée en local. C'est ce
    // découplage (état local mis à jour même si l'écriture échouait) qui
    // rendait invisible l'échec réel de l'enregistrement du carnet médical.
    try {
      const res = await window.API.updateMe({ medicalRecord: medPayload });
      window.SM.user = res;
      localStorage.setItem('sm_user', JSON.stringify(res));
      window.SM.emit();
      setSaving(false); setEditing(false); setForm(null);
      setToast(t('profile.updated_medical'));
      setTimeout(() => setToast(''), 2500);
    } catch (e) {
      setSaving(false);
      setError(t('profile.save_failed'));
      setTimeout(() => setError(''), 4000);
    }
  }

  const rows = [
    { label: t('profile.height_weight'), val: (med.height || med.weight) ? `${med.height || '?'} cm · ${med.weight || '?'} kg` : null },
    { label: t('profile.allergies'),     val: (med.allergies  || []).join(', ') || null },
    { label: t('profile.conditions'),    val: (med.conditions || []).join(', ') || null },
  ];

  return (
    <div style={{ position: 'absolute', inset: 0, background: 'var(--sm-paper)', display: 'flex', flexDirection: 'column', overflow: 'hidden' }}>
      <SubHeader
        title={t('profile.medical_profile')}
        nav={nav}
        right={!editing && (
          <button onClick={startEdit} style={{ padding: '7px 16px', borderRadius: 8, background: 'var(--sm-blue)', color: 'white', border: 'none', fontSize: 14, fontWeight: 600, fontFamily: 'var(--font-ui)', cursor: 'pointer' }}>
            {t('common.edit')}
          </button>
        )}
      />

      <div style={{ flex: 1, overflowY: 'auto', padding: '16px' }}>
        <div style={{ background: 'white', borderRadius: 'var(--sm-radius)', boxShadow: 'var(--sm-shadow)', overflow: 'hidden' }}>

          {/* ── Lecture ── */}
          {/* Groupe sanguin — bloc dédié plutôt qu'une ligne de `rows` : c'est
              la seule donnée du carnet avec un justificatif attachable,
              indépendant du mode édition (joindre/remplacer/retirer reste
              possible même hors "Modifier"). */}
          {!editing && (
            <div className="sm-row-divider" style={{ padding: '13px 16px' }}>
              <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
                <span style={{ fontSize: 13, color: 'var(--sm-ink-500)', width: 130, flexShrink: 0, fontFamily: 'var(--font-ui)' }}>{t('profile.blood_type')}</span>
                {med.bloodType ? (
                  <span style={{ padding: '3px 12px', borderRadius: 999, background: 'var(--sm-red-soft)', color: 'var(--sm-red)', fontWeight: 700, fontSize: 14, fontFamily: 'var(--font-ui)' }}>{med.bloodType}</span>
                ) : <span style={{ color: 'var(--sm-ink-400)', fontFamily: 'var(--font-ui)' }}>—</span>}
              </div>
              {med.bloodType && (
                <>
                  <BloodTypeStatusNote t={t} status={med.bloodTypeStatus} />
                  <input
                    ref={proofRef} type="file" accept="image/jpeg,image/png,image/webp,application/pdf"
                    style={{ display: 'none' }} onChange={handleProofFile}
                  />
                  <div style={{ display: 'flex', gap: 8, marginTop: 10, flexWrap: 'wrap' }}>
                    <button
                      type="button" onClick={() => proofRef.current?.click()} disabled={proofUploading}
                      style={{ padding: '7px 14px', borderRadius: 8, background: '#F1F2F4', color: 'var(--sm-ink)', border: 'none', fontSize: 13, fontWeight: 600, fontFamily: 'var(--font-ui)', cursor: proofUploading ? 'default' : 'pointer' }}
                    >
                      {proofUploading ? t('common.saving') : (med.hasBloodTypeProof ? t('profile.blood_type_proof_replace') : t('profile.blood_type_proof_attach'))}
                    </button>
                    {med.hasBloodTypeProof && (
                      <button
                        type="button" onClick={removeProof} disabled={proofUploading}
                        style={{ padding: '7px 14px', borderRadius: 8, background: '#FDEDEC', color: '#C0392B', border: 'none', fontSize: 13, fontWeight: 600, fontFamily: 'var(--font-ui)', cursor: proofUploading ? 'default' : 'pointer' }}
                      >
                        {t('profile.blood_type_proof_remove')}
                      </button>
                    )}
                  </div>
                  {proofError && (
                    <div style={{ marginTop: 10 }}>
                      <Banner variant="danger" icon="alert-circle" text={proofError} />
                    </div>
                  )}
                </>
              )}
            </div>
          )}
          {!editing && rows.map((r, i) => (
            <div key={r.label} className="sm-row-divider" style={{ display: 'flex', alignItems: 'center', gap: 12, padding: '13px 16px' }}>
              <span style={{ fontSize: 13, color: 'var(--sm-ink-500)', width: 130, flexShrink: 0, fontFamily: 'var(--font-ui)' }}>{r.label}</span>
              {r.render ? r.render() : (
                <span style={{ fontSize: 15, color: r.val ? 'var(--sm-ink)' : 'var(--sm-ink-400)', fontFamily: 'var(--font-ui)' }}>{r.val || '—'}</span>
              )}
            </div>
          ))}

          {/* ── Édition ── */}
          {editing && (
            <>
              <div className="sm-row-divider" style={{ padding: '14px 16px', background: '#F4F8FF' }}>
                <div style={{ fontSize: 12, color: 'var(--sm-blue)', fontWeight: 600, fontFamily: 'var(--font-ui)', marginBottom: 10 }}>{t('profile.blood_type')}</div>
                <div style={{ display: 'flex', flexWrap: 'wrap', gap: 8 }}>
                  {BLOOD_TYPES_P.map(bt => (
                    <button key={bt} type="button" onClick={() => setF('bloodType')(bt)} style={{ padding: '7px 14px', borderRadius: 999, fontSize: 14, fontWeight: 600, fontFamily: 'var(--font-ui)', cursor: 'pointer', background: form.bloodType === bt ? 'var(--sm-red)' : 'white', color: form.bloodType === bt ? 'white' : 'var(--sm-ink)', border: `1.5px solid ${form.bloodType === bt ? 'var(--sm-red)' : 'var(--sm-line)'}` }}>
                      {bt}
                    </button>
                  ))}
                </div>
              </div>
              <div className="sm-row-divider" style={{ display: 'grid', gridTemplateColumns: '1fr 1fr' }}>
                {[{ label: t('auth.height_label'), key: 'height', ph: '170', min: 50, max: 250 }, { label: t('auth.weight_label'), key: 'weight', ph: '65', min: 1, max: 300 }].map((f, i) => (
                  <div key={f.key} style={{ padding: '12px 14px', borderRight: i === 0 ? '0.5px solid var(--sm-line)' : 'none', background: '#F4F8FF' }}>
                    <div style={{ fontSize: 12, color: 'var(--sm-blue)', fontWeight: 600, fontFamily: 'var(--font-ui)', marginBottom: 6 }}>{f.label}</div>
                    <input type="number" value={form[f.key]} onChange={e => setF(f.key)(e.target.value)} placeholder={f.ph} min={f.min} max={f.max} style={{ ...PINP }} />
                  </div>
                ))}
              </div>
              <div className="sm-row-divider" style={{ padding: '12px 16px', background: '#F4F8FF' }}>
                <div style={{ fontSize: 12, color: 'var(--sm-blue)', fontWeight: 600, fontFamily: 'var(--font-ui)', marginBottom: 6 }}>{t('profile.allergies_hint')}</div>
                <input value={form.allergies} onChange={e => setF('allergies')(e.target.value)} placeholder="Pénicilline, Arachides…" style={{ ...PINP }} />
              </div>
              <div style={{ padding: '12px 16px', background: '#F4F8FF' }}>
                <div style={{ fontSize: 12, color: 'var(--sm-blue)', fontWeight: 600, fontFamily: 'var(--font-ui)', marginBottom: 6 }}>{t('profile.conditions_hint')}</div>
                <textarea value={form.conditions} onChange={e => setF('conditions')(e.target.value)} placeholder="Diabète, hypertension…" rows={3} style={{ ...PINP, resize: 'none', lineHeight: 1.55 }} />
              </div>
            </>
          )}
        </div>
      </div>

      {/* Erreur EN LIGNE juste au-dessus de la barre Annuler/Sauvegarder —
          jamais un overlay position:absolute à décalage deviné (voir le
          correctif du lot 8) : toujours visible sans défiler, exactement là
          où l'œil se trouve au moment de cliquer sur Sauvegarder. */}
      {error && (
        <div style={{ padding: '0 16px 12px', flexShrink: 0 }}>
          <Banner variant="danger" icon="alert-circle" text={error} />
        </div>
      )}
      {editing && <SaveBar onCancel={cancelEdit} onSave={save} saving={saving} />}
      <ProfileToast msg={toast} />
    </div>
  );
}

// ════════════════════════════════════════════════════════════════════════════
// SOUS-ÉCRAN : CONTACTS D'URGENCE
// ════════════════════════════════════════════════════════════════════════════
function ProfileContacts({ nav }) {
  useLucide();
  const t = useTranslation();
  const SM = window.useSM();
  const user = SM.user || {};
  const med  = user.medicalRecord || {};

  const [editing, setEditing] = useState(false);
  const [contacts, setContacts] = useState([]);
  const [saving, setSaving]    = useState(false);
  const [toast, setToast]      = useState('');
  const [error, setError]      = useState('');

  function startEdit() {
    setContacts((med.emergencyContacts || []).map(c => ({ ...c })));
    setEditing(true);
  }
  function cancelEdit() { setEditing(false); setContacts([]); }

  function addContact()       { if (contacts.length < 5) setContacts(cs => [...cs, { name: '', phone: '', relation: '' }]); }
  function removeContact(i)   { setContacts(cs => cs.filter((_, idx) => idx !== i)); }
  function updateContact(i, k, v) { setContacts(cs => cs.map((c, idx) => idx === i ? { ...c, [k]: v } : c)); }

  async function save() {
    setSaving(true);
    setError('');
    const clean = contacts.filter(c => c.name && c.name.trim());
    // Même règle que ProfilePersonal/ProfileMedical.save() : état local /
    // localStorage uniquement après confirmation serveur, avec la réponse du
    // serveur elle-même.
    try {
      const res = await window.API.updateMe({ medicalRecord: { emergencyContacts: clean } });
      window.SM.user = res;
      localStorage.setItem('sm_user', JSON.stringify(res));
      window.SM.emit();
      setSaving(false); setEditing(false); setContacts([]);
      setToast(t('profile.updated_contacts'));
      setTimeout(() => setToast(''), 2500);
    } catch (e) {
      setSaving(false);
      setError(t('profile.save_failed'));
      setTimeout(() => setError(''), 4000);
    }
  }

  const savedContacts = med.emergencyContacts || [];

  return (
    <div style={{ position: 'absolute', inset: 0, background: 'var(--sm-paper)', display: 'flex', flexDirection: 'column', overflow: 'hidden' }}>
      <SubHeader
        title={t('profile.emergency_contacts')}
        nav={nav}
        right={!editing && (
          <button onClick={startEdit} style={{ padding: '7px 16px', borderRadius: 8, background: 'var(--sm-blue)', color: 'white', border: 'none', fontSize: 14, fontWeight: 600, fontFamily: 'var(--font-ui)', cursor: 'pointer' }}>
            {t('common.edit')}
          </button>
        )}
      />

      <div style={{ flex: 1, overflowY: 'auto', padding: '16px', display: 'flex', flexDirection: 'column', gap: 10 }}>

        {/* ── Lecture ── */}
        {!editing && (
          savedContacts.length === 0 ? (
            <div style={{ background: 'white', borderRadius: 'var(--sm-radius)', boxShadow: 'var(--sm-shadow)', padding: '32px 20px', textAlign: 'center' }}>
              <Icon name="users" size={36} color="var(--sm-ink-400)" strokeWidth={1.5} />
              <p style={{ fontSize: 14, color: 'var(--sm-ink-500)', marginTop: 12, fontFamily: 'var(--font-ui)' }}>
                {t('profile.no_contacts')}
              </p>
            </div>
          ) : savedContacts.map((c, i) => (
            <div key={i} style={{ background: 'white', borderRadius: 'var(--sm-radius)', boxShadow: 'var(--sm-shadow)', padding: '14px 16px', display: 'flex', alignItems: 'center', gap: 12 }}>
              <div style={{ width: 44, height: 44, borderRadius: '50%', background: 'var(--sm-blue-soft)', display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}>
                <span style={{ fontSize: 17, fontWeight: 700, color: 'var(--sm-blue)', fontFamily: 'var(--font-ui)' }}>{(c.name || '?')[0]}</span>
              </div>
              <div style={{ flex: 1, minWidth: 0 }}>
                <div style={{ fontSize: 15, fontWeight: 600, color: 'var(--sm-ink)', fontFamily: 'var(--font-ui)' }}>{c.name}</div>
                <div style={{ fontSize: 12, color: 'var(--sm-ink-500)', marginTop: 2, fontFamily: 'var(--font-ui)' }}>
                  {c.relation ? c.relation + ' · ' : ''}{c.phone}
                </div>
              </div>
              <a href={'tel:' + c.phone} style={{ width: 38, height: 38, borderRadius: '50%', background: '#EAFAF1', display: 'flex', alignItems: 'center', justifyContent: 'center', textDecoration: 'none', flexShrink: 0 }}>
                <Icon name="phone" size={17} color="#27AE60" />
              </a>
            </div>
          ))
        )}

        {/* ── Édition ── */}
        {editing && (
          <>
            {contacts.map((c, i) => (
              <div key={i} style={{ background: 'white', borderRadius: 'var(--sm-radius)', boxShadow: 'var(--sm-shadow)', padding: '14px 16px', display: 'flex', flexDirection: 'column', gap: 10 }}>
                <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
                  <span style={{ fontSize: 13, fontWeight: 700, color: 'var(--sm-blue)', fontFamily: 'var(--font-ui)' }}>{t('profile.contact_n').replace('{n}', i + 1)}</span>
                  <button onClick={() => removeContact(i)} style={{ background: 'none', border: 'none', color: 'var(--sm-red)', cursor: 'pointer', fontSize: 13, fontWeight: 600, fontFamily: 'var(--font-ui)' }}>
                    {t('profile.remove')}
                  </button>
                </div>
                <input value={c.name} onChange={e => updateContact(i, 'name', e.target.value)} placeholder={t('profile.contact_name_placeholder')} style={{ ...PINP }} />
                <input value={c.phone} onChange={e => updateContact(i, 'phone', e.target.value)} placeholder="+225 07 00 00 00" type="tel" style={{ ...PINP }} />
                <input value={c.relation || ''} onChange={e => updateContact(i, 'relation', e.target.value)} placeholder={t('profile.relation_placeholder')} style={{ ...PINP }} />
              </div>
            ))}

            {contacts.length < 5 && (
              <button onClick={addContact} style={{ background: 'white', border: '2px dashed var(--sm-line)', borderRadius: 'var(--sm-radius)', padding: '14px', color: 'var(--sm-blue)', fontWeight: 600, fontSize: 14, fontFamily: 'var(--font-ui)', cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8 }}>
                <Icon name="plus" size={18} color="var(--sm-blue)" />
                {t('profile.add_contact').replace('{n}', contacts.length)}
              </button>
            )}
          </>
        )}
      </div>

      {/* Erreur EN LIGNE juste au-dessus de la barre Annuler/Sauvegarder —
          jamais un overlay position:absolute à décalage deviné (voir le
          correctif du lot 8) : toujours visible sans défiler, exactement là
          où l'œil se trouve au moment de cliquer sur Sauvegarder. */}
      {error && (
        <div style={{ padding: '0 16px 12px', flexShrink: 0 }}>
          <Banner variant="danger" icon="alert-circle" text={error} />
        </div>
      )}
      {editing && <SaveBar onCancel={cancelEdit} onSave={save} saving={saving} />}
      <ProfileToast msg={toast} />
    </div>
  );
}

Object.assign(window, { ProfileScreen, ProfilePersonal, ProfileMedical, ProfileContacts });
