<link rel="stylesheet" href="/public/assets/css/cda-v4-5-adherents.css">
<link rel="stylesheet" href="/public/assets/css/cda-family-messages-972.css?v=9.7.3">
<?php $familyPortal = \App\Core\Permission::isFamilyPortalIdentity(); ?>
<?php if(\App\Core\Permission::can('clients.archive')):?><form method="post" action="/public/administration/corbeille/archiver" onsubmit="return confirm('Archiver cet adhérent ?');" style="display:flex;justify-content:flex-end;margin-bottom:1rem"><?= \App\Core\Csrf::field() ?><input type="hidden" name="entity_type" value="client"><input type="hidden" name="entity_id" value="<?= (int)$client['id'] ?>"><input type="hidden" name="entity_label" value="Adhérent <?= htmlspecialchars(($client['prenom']??'').' '.($client['nom']??'')) ?>"><input type="hidden" name="return_to" value="/public/clients"><button class="button button-secondary">Archiver l’adhérent</button></form><?php endif?>

<nav class="cda-profile-tabs cda-family-profile-tabs" aria-label="Sections du dossier familial">
    <?php if($familyPortal): ?>
    <a href="/public/mon-espace/dossier/identite" data-family-tab="identite">Identité</a>
    <a href="/public/mon-espace/dossier/famille" data-family-tab="famille">Famille</a>
    <a href="/public/mon-espace/dossier/annees" data-family-tab="annees">Années d’action</a>
    <a href="/public/mon-espace/dossier/documents" data-family-tab="documents">Documents</a>
    <a href="/public/mon-espace/dossier/factures" data-family-tab="facturation">Factures</a>
    <a href="/public/mon-espace/dossier/historique" data-family-tab="historique">Historique</a>
    <?php else: ?>
    <?php $familyTabBase='/public'.\App\Models\Client::publicUrl($client); ?>
    <a href="<?=$familyTabBase?>&amp;onglet=identite" data-family-tab="identite">Identité</a>
    <a href="<?=$familyTabBase?>&amp;onglet=famille" data-family-tab="famille">Famille</a>
    <a href="<?=$familyTabBase?>&amp;onglet=annees" data-family-tab="annees">Années d’action</a>
    <a href="<?=$familyTabBase?>&amp;onglet=documents" data-family-tab="documents">Documents</a>
    <a href="<?=$familyTabBase?>&amp;onglet=facturation" data-family-tab="facturation">Factures</a>
    <a href="<?=$familyTabBase?>&amp;onglet=historique" data-family-tab="historique">Historique</a>
    <?php endif; ?>
</nav>

<section class="profile-hero adherent-profile" id="identite">
    <div class="profile-main">
        <div class="profile-avatar-large adherent-avatar">
            <?= htmlspecialchars(
                mb_substr((string)($client['prenom'] ?? ''), 0, 1) .
                mb_substr((string)($client['nom'] ?? ''), 0, 1)
            ) ?>
        </div>

        <div>
            <span class="profile-code">
                ADHÉRENT TITULAIRE ·
                <?= htmlspecialchars((string)($client['code_client'] ?? '')) ?>
            </span>

            <h2>
                <?= htmlspecialchars(
                    trim(
                        (string)($client['nom'] ?? '') . ' ' .
                        (string)($client['prenom'] ?? '')
                    )
                ) ?>
            </h2>

            <p>
                <?= htmlspecialchars(
                    (string)($client['nom_affichage'] ?? 'Famille')
                ) ?>
                ·
                <?= htmlspecialchars(
                    (string)($client['reference_famille'] ?? '')
                ) ?>
            </p>

            <div class="profile-tags">
                <span class="tag green">Adhérent actif</span>
                <span class="tag blue">Titulaire du dossier</span>

                <?php if (!empty($client['rgpd_accepte'])): ?>
                <span class="tag pink">RGPD validé</span>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <div class="profile-actions">
        <?php if (!$familyPortal): ?><a class="button button-secondary" href="/public/clients">Retour</a><?php endif; ?>

        <?php if (!$familyPortal): ?>
        <a class="button button-secondary"
           href="/public/clients/modifier?id=<?= (int)$client['id'] ?>">
            Modifier l’adhérent
        </a>

        <a class="button button-primary"
           href="/public/adherents/membres/nouveau?dossier=<?= rawurlencode((string)$client['public_token']) ?>">
            + Ajouter un membre de la famille
        </a>
        <?php else: ?>
        <a class="button button-primary" href="/public/mon-espace/demandes/nouvelle">
            Faire une demande à la Direction
        </a>
        <?php endif; ?>
    </div>
</section>

<?php if ($familyPortal): ?>
<section class="card cda-family-section" id="identite-details">
    <header><div><span class="cda-eyebrow">Identité</span><h3>Mes données enregistrées</h3></div></header>
    <div class="cda-family-identity-grid">
        <div><span>Nom</span><strong><?=htmlspecialchars((string)($client['nom']??'—'))?></strong></div>
        <div><span>Prénom</span><strong><?=htmlspecialchars((string)($client['prenom']??'—'))?></strong></div>
        <div><span>Date de naissance</span><strong><?=!empty($client['date_naissance'])?date('d/m/Y',strtotime((string)$client['date_naissance'])):'—'?></strong></div>
        <div><span>Adresse e-mail</span><strong><?=htmlspecialchars((string)($client['email']??'—'))?></strong></div>
        <div><span>Téléphone</span><strong><?=htmlspecialchars((string)($client['telephone']??$client['telephone_mobile']??'—'))?></strong></div>
        <div><span>Adresse</span><strong><?=htmlspecialchars(trim((string)($client['adresse_ligne1']??'').' '.(string)($client['code_postal']??'').' '.(string)($client['ville']??''))?:'—')?></strong></div>
        <div><span>Consentement RGPD</span><strong><?=!empty($client['rgpd_accepte'])?'Validé':'Non validé'?></strong></div>
        <div><span>Référence famille</span><strong><?=htmlspecialchars((string)($client['reference_famille']??'—'))?></strong></div>
    </div>
    <p class="cda-readonly-note"><strong>Lecture seule</strong><span>Pour corriger une information, utilisez la messagerie avec la Direction.</span></p>
</section>
<?php endif; ?>

<?php if (!empty($_SESSION['success'])): ?>
<div class="alert alert-success">
    <?= htmlspecialchars((string)$_SESSION['success']) ?>
</div>
<?php unset($_SESSION['success']); endif; ?>

<section class="family-structure" id="famille">
    <article class="family-owner-card">
        <div class="family-owner-icon">A</div>
        <div>
            <span>Adhérent titulaire</span>
            <strong>
                <?= htmlspecialchars(
                    trim(
                        (string)($client['nom'] ?? '') . ' ' .
                        (string)($client['prenom'] ?? '')
                    )
                ) ?>
            </strong>
            <small>Responsable du compte et de la facturation.</small>
        </div>
    </article>

    <div class="family-connector">↓</div>

    <article class="family-members-card">
        <header>
            <div>
                <span>Famille rattachée</span>
                <strong><?= count($members) ?> membre(s)</strong>
            </div>

            <?php if (!$familyPortal): ?><a href="/public/adherents/membres/nouveau?dossier=<?= rawurlencode((string)$client['public_token']) ?>">
                Ajouter
            </a><?php endif; ?>
        </header>

        <?php if (!$members): ?>
        <div class="empty-v4">
            <div>👨‍👩‍👧</div>
            <strong>Aucun membre enregistré</strong>
            <span>Ajoutez les personnes rattachées à cet adhérent.</span>
        </div>
        <?php else: ?>

        <div class="family-member-list">
            <?php foreach ($members as $member): ?>
            <article class="<?= !empty($member['est_client_titulaire']) ? 'is-holder' : '' ?>">
                <div class="member-avatar">
                    <?= htmlspecialchars(
                        mb_substr((string)$member['prenom'], 0, 1) .
                        mb_substr((string)$member['nom'], 0, 1)
                    ) ?>
                </div>

                <div class="member-identity">
                    <span>
                        <?= !empty($member['est_client_titulaire'])
                            ? 'ADHÉRENT TITULAIRE'
                            : 'MEMBRE DE LA FAMILLE' ?>
                    </span>

                    <strong>
                        <?= htmlspecialchars(
                            (string)$member['nom'] . ' ' .
                            (string)$member['prenom']
                        ) ?>
                    </strong>

                    <small>
                        <?= htmlspecialchars(ucfirst((string)$member['lien_familial'])) ?>
                        ·
                        <?= date('d/m/Y', strtotime((string)$member['date_naissance'])) ?>
                    </small>
                </div>

                <div class="member-role-tags">
                    <?php if (!empty($member['est_client_titulaire'])): ?>
                    <span class="tag blue">Titulaire</span>
                    <?php endif; ?>

                    <?php if (!empty($member['responsable_legal'])): ?>
                    <span class="tag pink">Responsable légal</span>
                    <?php endif; ?>

                    <?php if (!empty($member['participant_ateliers'])): ?>
                    <span class="tag green">Participant atelier</span>
                    <?php else: ?>
                    <span class="tag neutral">Non participant</span>
                    <?php endif; ?>
                </div>
            </article>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
    </article>
</section>

<?php
$editableMembers = $members
    ?? $membres
    ?? $familyMembers
    ?? [];
?>
<?php if (!empty($editableMembers)): ?>
<section class="card" id="membres-demandes" style="margin-top:16px">
<h3>Membres et demandes d’inscription</h3>
<div style="display:grid;gap:9px">
<?php foreach ($editableMembers as $editableMember): ?>
<?php
$editableMemberId=(int)(
    $editableMember['id']
    ?? $editableMember->id
    ?? 0
);
$editableMemberName=trim(
    (string)($editableMember['prenom'] ?? $editableMember->prenom ?? '')
    .' '
    .(string)($editableMember['nom'] ?? $editableMember->nom ?? '')
);
?>
<?php if($editableMemberId): ?>
<div style="display:flex;justify-content:space-between;align-items:center;gap:10px;padding:10px;border-radius:12px;background:#f8faff">
<strong><?= htmlspecialchars($editableMemberName) ?></strong>
<div style="display:flex;gap:8px;flex-wrap:wrap">
<a class="button button-secondary" href="/public/membres/voir?id=<?= $editableMemberId ?>"><?= $familyPortal ? 'Voir' : 'Voir / modifier' ?></a>
<?php if (!$familyPortal && !empty($editableMember['participant_ateliers'])): ?>
<a class="button button-primary" href="/public/demandes-inscription/nouvelle?membre_id=<?= $editableMemberId ?>">Demande d’inscription</a>
<?php else: ?>
<span class="tag neutral">Participation aux ateliers non activée</span>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
</section>
<?php endif; ?>

<section class="cda-annual-shell" id="dossiers-annuels">
    <?php
        $currentAnnualFile = null;
        if (!empty($activeYear)) {
            foreach ((array)$annualFiles as $candidateAnnualFile) {
                if ((int)($candidateAnnualFile['annee_scolaire_id'] ?? 0) === (int)$activeYear['id']
                    && (string)($candidateAnnualFile['statut'] ?? '') === 'actif') {
                    $currentAnnualFile = $candidateAnnualFile;
                    break;
                }
            }
        }
    ?>
    <header class="cda-annual-head">
        <div>
            <span class="cda-eyebrow">Dossier adhérent 360°</span>
            <h3>Années d’action et adhésions</h3>
            <p>La fiche identité reste permanente. Chaque participation est historisée dans un dossier annuel distinct.</p>
        </div>

        <?php if (!empty($activeYear) && $currentAnnualFile): ?>
        <span class="cda-status cda-status-active">
            <?= htmlspecialchars((string)$activeYear['libelle']) ?> déjà activée
        </span>
        <?php elseif (!empty($activeYear)): ?>
        <span class="cda-status cda-status-warning">
            <?= htmlspecialchars((string)$activeYear['libelle']) ?> non activée
        </span>
        <?php else: ?>
        <span class="cda-status cda-status-warning">Aucune année active</span>
        <?php endif; ?>
    </header>

    <?php if (!$familyPortal && !empty($activeYear) && !$currentAnnualFile): ?>
    <form method="post" action="/public/clients/dossier-annuel/activer"
          class="cda-annual-decision">
            <?= \App\Core\Csrf::field() ?>
            <input type="hidden" name="client_id" value="<?= (int)$client['id'] ?>">
            <fieldset>
                <legend>Faut-il activer l’année <?= htmlspecialchars((string)$activeYear['libelle']) ?> ?</legend>
                <p>Une demande d’instrument ou d’activité peut rester en réflexion sans créer d’adhésion à facturer.</p>
                <label>
                    <input type="radio" name="annual_decision" value="pending" required>
                    <span>
                        <strong>Non, demande uniquement</strong>
                        <small>Aucune adhésion et aucune facturation ne seront créées.</small>
                    </span>
                </label>
                <label>
                    <input type="radio" name="annual_decision" value="activate" required>
                    <span>
                        <strong>Oui, activer l’année</strong>
                        <small>Crée une seule adhésion annuelle dans « À facturer ».</small>
                    </span>
                </label>
            </fieldset>
            <button class="button button-primary" type="submit">Enregistrer ce choix</button>
        </form>
    <?php elseif ($currentAnnualFile): ?>
    <div class="cda-readonly-note">
        <strong>Année déjà active</strong>
        <span>L’activation est verrouillée : aucun second dossier, aucune seconde adhésion et aucune nouvelle ligne d’historique ne peuvent être créés.</span>
    </div>
    <?php endif; ?>

    <?php if (!empty($_SESSION['form_errors'])): ?>
    <div class="alert alert-danger">
        <?php foreach ((array)$_SESSION['form_errors'] as $annualError): ?>
        <div><?= htmlspecialchars((string)$annualError) ?></div>
        <?php endforeach; ?>
    </div>
    <?php unset($_SESSION['form_errors']); endif; ?>

    <?php if (empty($annualFiles)): ?>
    <div class="cda-empty-annual">
        <strong>Aucun dossier annuel ouvert</strong>
        <span>Activez l’année en cours pour créer automatiquement l’adhésion à facturer.</span>
    </div>
    <?php else: ?>
    <div class="cda-annual-grid">
        <?php foreach ($annualFiles as $annualFile): ?>
        <?php
            $billingStatus = (string)($annualFile['facturation_statut'] ?? 'non_generee');
            $billingLabel = [
                'a_facturer' => 'À facturer',
                'facturee' => 'Facturée',
                'payee' => 'Payée',
                'exoneree' => 'Exonérée',
                'annulee' => 'Annulée',
            ][$billingStatus] ?? 'Non générée';
        ?>
        <article class="cda-annual-card">
            <div class="cda-annual-card-top">
                <div>
                    <span>ANNÉE D’ACTION</span>
                    <h4><?= htmlspecialchars((string)$annualFile['annee_libelle']) ?></h4>
                </div>
                <span class="cda-status cda-status-active"><?= htmlspecialchars(ucfirst((string)$annualFile['statut'])) ?></span>
            </div>

            <div class="cda-annual-metrics">
                <div>
                    <span>Adhésion</span>
                    <strong><?= htmlspecialchars($billingLabel) ?></strong>
                </div>
                <div>
                    <span>Montant</span>
                    <strong><?= $annualFile['montant_ttc'] !== null ? number_format((float)$annualFile['montant_ttc'], 2, ',', ' ') . ' €' : 'Défini en facturation' ?></strong>
                </div>
                <div>
                    <span>Activation</span>
                    <strong><?= !empty($annualFile['activated_at']) ? date('d/m/Y', strtotime((string)$annualFile['activated_at'])) : '—' ?></strong>
                </div>
            </div>

            <div class="cda-readonly-note">
                <strong>Lecture seule</strong>
                <span>Le tarif, les réductions et le statut de paiement sont gérés exclusivement dans le module Facturation.</span>
            </div>
        </article>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>
</section>

<section class="card" id="demandes-inscription" style="margin-top:16px">
    <header style="display:flex;justify-content:space-between;align-items:center;gap:12px;flex-wrap:wrap">
        <div>
            <span class="cda-eyebrow">Inscriptions</span>
            <h3>Demandes et files d’attente</h3>
        </div>
        <?php if (\App\Core\Permission::can('demandes_inscription.review')): ?>
        <a class="button button-secondary" href="/public/demandes-inscription">Voir toutes les files d’attente</a>
        <?php endif; ?>
    </header>

    <?php if (empty($enrollmentRequests)): ?>
    <p>Aucune demande d’inscription enregistrée pour cette famille.</p>
    <?php else: ?>
    <div style="display:grid;gap:10px">
        <?php foreach ($enrollmentRequests as $request): ?>
        <?php
            $requestStatus=(string)($request['statut'] ?? 'a_etudier');
            $requestLabel=[
                'a_etudier'=>'À étudier',
                'file_attente'=>'File d’attente',
                'creneau_propose'=>'Créneau proposé',
                'validee'=>'Inscription validée',
                'refusee'=>'Refusée',
                'abandonnee'=>'Abandonnée',
            ][$requestStatus] ?? ucfirst(str_replace('_',' ',$requestStatus));
        ?>
        <article style="display:flex;justify-content:space-between;gap:16px;align-items:center;padding:13px;border:1px solid #dbe4f0;border-radius:13px;background:#fff;flex-wrap:wrap">
            <div>
                <strong><?= htmlspecialchars((string)$request['atelier_libelle']) ?></strong>
                <div style="color:#64748b;margin-top:3px">
                    <?= htmlspecialchars(trim((string)$request['membre_prenom'].' '.(string)$request['membre_nom'])) ?>
                    · demande du <?= date('d/m/Y',strtotime((string)$request['created_at'])) ?>
                </div>
            </div>
            <span class="cda-status cda-status-neutral"><?= htmlspecialchars($requestLabel) ?></span>
        </article>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>
</section>

<section class="card cda-family-section" id="documents">
    <header>
        <div>
            <span class="cda-eyebrow">Documents</span>
            <h3>Documents du dossier familial</h3>
        </div>
    </header>
    <p>Retrouvez ici uniquement les documents déposés ou validés par le Carrefour des Arts, notamment les consentements RGPD et le droit à l’image. Les échanges et compléments restent centralisés dans « Mes demandes ».</p>
    <?php if($familyPortal): ?>
    <?php if(empty($familyDocuments)): ?><p>Aucun document disponible.</p><?php else: ?>
    <div class="cda-family-invoices">
    <?php foreach($familyDocuments as $document):
        $signed=(string)$document['statut']==='signe';
        $label=(string)$document['type_document']==='droit_image'?'Droit à l’image':((string)$document['type_document']==='rgpd'?'Consentement RGPD':'Document');
    ?>
    <article>
        <div><strong><?=htmlspecialchars($label)?></strong><span><?=htmlspecialchars((string)$document['membre_prenom'].' '.(string)$document['membre_nom'])?> · <?=htmlspecialchars((string)$document['annee_scolaire'])?></span></div>
        <div><span>Statut</span><strong><?=$signed?'Signé et verrouillé':'À signer'?></strong></div>
        <?php if($signed): ?>
        <a class="button button-secondary" href="/public/mon-espace/documents/telecharger?id=<?=(int)$document['id']?>">Télécharger le document signé</a>
        <?php else: ?>
        <a class="button button-primary" href="/public/mon-espace/documents/signature?id=<?=(int)$document['id']?>">Lire et signer</a>
        <?php endif; ?>
    </article>
    <?php endforeach; ?>
    </div>
    <?php endif; ?>
    <p><small>Si vous ne pouvez pas signer en ligne, la Direction peut vous faire signer sur une tablette à l’accueil. Le document signé apparaîtra ensuite ici. Aucun document n’est envoyé en pièce jointe par courriel.</small></p>
    <?php endif; ?>
</section>

<section class="card cda-family-section" id="facturation">
    <header>
        <div>
            <span class="cda-eyebrow">Factures</span>
            <h3>Mes factures et règlements</h3>
        </div>
    </header>
    <p>Consultez, imprimez ou enregistrez uniquement vos factures officielles.</p>
    <?php if ($familyPortal): ?>
    <?php $familyStatusLabels=['issued'=>'Émise','part_paid'=>'Partiellement payée','paid'=>'Payée']; ?>
    <?php if (empty($familyInvoices)): ?>
    <p>Aucune facture disponible.</p>
    <?php else: ?>
    <div class="cda-family-invoices">
        <?php foreach($familyInvoices as $familyInvoice):
            $invoiceTotal=(float)($familyInvoice['total_amount']??0);
            $invoicePaid=(float)($familyInvoice['paid_amount']??0);
        ?>
        <article>
            <div>
                <strong><?=htmlspecialchars((string)$familyInvoice['document_number'])?></strong>
                <span><?=htmlspecialchars($familyStatusLabels[(string)$familyInvoice['status']]??(string)$familyInvoice['status'])?></span>
            </div>
            <div><span>Total</span><strong><?=number_format($invoiceTotal,2,',',' ')?> €</strong></div>
            <div><span>Réglé</span><strong><?=number_format($invoicePaid,2,',',' ')?> €</strong></div>
            <div><span>Reste</span><strong><?=number_format(max(0,$invoiceTotal-$invoicePaid),2,',',' ')?> €</strong></div>
            <a class="button button-primary" target="_blank" rel="noopener" href="/public/mon-espace/factures/pdf?facture=<?=htmlspecialchars((string)$familyInvoice['public_token'])?>">Télécharger / imprimer le PDF</a>
        </article>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>
    <?php else: ?>
    <p>Les outils de gestion comptable sont disponibles uniquement dans l’espace administratif.</p>
    <?php endif; ?>
</section>

<section class="cda-timeline-shell" id="historique">
    <header>
        <div>
            <span class="cda-eyebrow">Traçabilité</span>
            <h3>Historique du parcours</h3>
        </div>
    </header>

    <?php if (empty($timeline) && empty($enrollmentRequests)): ?>
    <div class="cda-empty-timeline">Aucun événement ni demande enregistré pour le moment.</div>
    <?php else: ?>
    <ol class="cda-timeline">
        <?php foreach ($timeline as $event): ?>
        <li>
            <span class="cda-timeline-dot"></span>
            <div>
                <time><?= date('d/m/Y H:i', strtotime((string)$event['created_at'])) ?></time>
                <strong><?= htmlspecialchars((string)$event['title']) ?></strong>
                <?php if (!empty($event['annee_libelle'])): ?>
                <span class="cda-status cda-status-neutral"><?= htmlspecialchars((string)$event['annee_libelle']) ?></span>
                <?php endif; ?>
                <p><?= htmlspecialchars((string)($event['description'] ?? '')) ?></p>
            </div>
        </li>
        <?php endforeach; ?>
        <?php foreach ((array)$enrollmentRequests as $request): ?>
        <?php
            $historyStatus=(string)($request['statut'] ?? 'a_etudier');
            $historyLabel=[
                'a_etudier'=>'À étudier',
                'file_attente'=>'File d’attente',
                'creneau_propose'=>'Créneau proposé',
                'validee'=>'Inscription validée',
                'refusee'=>'Refusée',
                'abandonnee'=>'Abandonnée',
            ][$historyStatus] ?? ucfirst(str_replace('_',' ',$historyStatus));
        ?>
        <li>
            <span class="cda-timeline-dot"></span>
            <div>
                <time><?= date('d/m/Y H:i',strtotime((string)$request['created_at'])) ?></time>
                <strong>Demande d’inscription — <?= htmlspecialchars((string)$request['atelier_libelle']) ?></strong>
                <span class="cda-status cda-status-neutral"><?= htmlspecialchars($historyLabel) ?></span>
                <p>
                    Participant :
                    <?= htmlspecialchars(trim((string)$request['membre_prenom'].' '.(string)$request['membre_nom'])) ?>
                </p>
            </div>
        </li>
        <?php endforeach; ?>
    </ol>
    <?php endif; ?>
</section>

<?php if ($familyPortal): ?>
<section class="card family-help-card">
    <div>
        <span class="cda-eyebrow">Messagerie avec la Direction</span>
        <h3>Une information doit être modifiée ?</h3>
        <p>Votre dossier est consultable uniquement. Envoyez une demande pour ajouter un membre, corriger une information ou poser une question de facturation.</p>
    </div>
    <div class="family-help-actions">
        <a class="button button-primary" href="/public/mon-espace/demandes/nouvelle">Nouvelle demande</a>
        <a class="button button-secondary" href="/public/mon-espace/demandes">Voir mes échanges</a>
        <a class="button button-secondary" href="/public/mon-espace/factures">Ma facturation</a>
    </div>
</section>
<?php endif; ?>

<div class="cda-page-return">
    <a class="button button-secondary" href="<?= $familyPortal ? '#identite' : '/public/clients' ?>">← Retour</a>
</div>

<link rel="stylesheet" href="/public/assets/css/cda-adherents-pro-953.css">
<style>
.cda-annual-decision{display:flex;align-items:flex-end;gap:16px;margin:16px 0 22px;padding:18px;border:1px solid #dbe4f0;border-radius:16px;background:#f8faff}.cda-annual-decision fieldset{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:12px;flex:1;border:0;margin:0;padding:0}.cda-annual-decision legend{grid-column:1/-1;font-weight:800;margin-bottom:4px}.cda-annual-decision p{grid-column:1/-1;margin:0 0 4px;color:#52627a}.cda-annual-decision label{display:flex;gap:10px;align-items:flex-start;padding:12px;border:1px solid #dbe4f0;border-radius:12px;background:#fff;cursor:pointer}.cda-annual-decision label:has(input:checked){border-color:#3b63ff;box-shadow:0 0 0 2px rgba(59,99,255,.12)}.cda-annual-decision span{display:flex;flex-direction:column;gap:3px}.cda-annual-decision small{color:#64748b}@media(max-width:800px){.cda-annual-decision{align-items:stretch;flex-direction:column}.cda-annual-decision fieldset{grid-template-columns:1fr}.cda-annual-decision .button{width:100%}}
.cda-family-profile-tabs{position:sticky;top:0;z-index:4;margin-bottom:16px;background:#fff}.cda-family-profile-tabs a:target,.cda-family-profile-tabs a:hover{color:#315bea}.cda-family-section{margin-top:16px;scroll-margin-top:80px}#identite,#famille,#dossiers-annuels,#documents,#facturation,#historique{scroll-margin-top:80px}
.cda-family-profile-tabs a.is-active{background:#315bea;color:#fff}.cda-family-panel-hidden{display:none!important}
.cda-family-identity-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:10px}.cda-family-identity-grid>div{display:flex;flex-direction:column;gap:4px;padding:12px;border-radius:12px;background:#f6f8fc}.cda-family-identity-grid span,.cda-family-invoices span{color:#64748b;font-size:.88rem}
.cda-family-invoices{display:grid;gap:10px}.cda-family-invoices article{display:grid;grid-template-columns:minmax(210px,1fr) repeat(3,minmax(90px,auto)) auto;align-items:center;gap:16px;padding:14px;border:1px solid #dbe4f0;border-radius:14px}.cda-family-invoices article>div{display:flex;flex-direction:column;gap:3px}
@media(max-width:900px){.cda-family-identity-grid{grid-template-columns:1fr}.cda-family-invoices article{grid-template-columns:1fr}.cda-family-invoices .button{width:100%;text-align:center}}
</style>
<?php if ($familyPortal): ?>
<script>
document.addEventListener('DOMContentLoaded',function(){
    const allowed=['identite','famille','annees','documents','facturation','historique'];
    const requested=<?= json_encode(
        (string)($activeFamilyTab ?? 'identite'),
        JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
    ) ?>;
    const active=allowed.includes(requested)?requested:'identite';
    const panels={
        identite:['#identite','#identite-details'],
        famille:['#famille','#membres-demandes'],
        annees:['#dossiers-annuels','#demandes-inscription'],
        documents:['#documents'],
        facturation:['#facturation'],
        historique:['#historique']
    };
    const managed=['#identite','#identite-details','#famille','#membres-demandes','#dossiers-annuels','#demandes-inscription','#documents','#facturation','#historique'];
    managed.forEach(function(selector){document.querySelectorAll(selector).forEach(function(node){node.classList.add('cda-family-panel-hidden')})});
    (panels[active]||panels.identite).forEach(function(selector){document.querySelectorAll(selector).forEach(function(node){node.classList.remove('cda-family-panel-hidden')})});
    document.querySelectorAll('[data-family-tab]').forEach(function(link){
        link.classList.toggle('is-active',link.dataset.familyTab===active);
        link.setAttribute('aria-current',link.dataset.familyTab===active?'page':'false');
    });
});
</script>
<?php endif; ?>
