/*
 * Pages hors application : connexion, inscription, mot de passe oublié et 404.
 *
 *   <main id="main" tabindex="-1" class="auth">
 *     <div class="auth__brand">…logo, collage décoratif, accroche…</div>
 *     <div class="auth__content">
 *       <div class="auth__body">…titre + formulaire…</div>
 *       <footer class="auth__footer">…</footer>
 *     </div>
 *   </main>
 *
 *   < 48rem   mobile   : plein écran, en-tête compact (logo + nom)
 *   48–64rem  tablette : carte centrée sur le fond dégradé de la page
 *   ≥ 64rem   bureau   : carte à deux colonnes, panneau de marque illustré à gauche
 *
 * .auth--lost : variante à une colonne de la page 404 (illustration « 4 💬 4 »).
 * Les éléments du collage se placent avec des variables en ligne :
 *   style="--x: 4%; --y: 6%; --w: 32%; --r: -6deg; --delay: -2s"
 */

@layer components {
  /* La page entière centre la carte (seul <main> occupe la grille : le reste est positionné) */
  body:has(> .auth) {
    display: grid;

    @media (width >= 48rem) {
      place-items: center;
      padding: var(--space-10) var(--space-6);
    }
  }

  .auth {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-columns: minmax(0, 1fr);
    gap: var(--space-8);
    inline-size: 100%;
    min-block-size: 100dvh;
    padding: var(--space-5) var(--space-4);
    background-color: var(--color-surface);

    &:focus {
      outline: none;
    }

    @media (width >= 48rem) {
      inline-size: min(100%, 31rem);
      min-block-size: auto;
      padding: var(--space-8);
      border-radius: var(--radius-xl);
      box-shadow: var(--shadow-lg);
    }

    @media (width >= 64rem) {
      grid-template-rows: minmax(0, 1fr);
      grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
      gap: var(--space-4);
      inline-size: min(100%, 70rem);
      min-block-size: min(46rem, 100dvh - 2 * var(--space-10));
      padding: var(--space-4);
    }
  }

  /* ---------- Panneau de marque ---------- */
  .auth__brand {
    display: flex;
    align-items: center;

    @media (width >= 64rem) {
      position: relative;
      display: grid;
      grid-template-rows: auto minmax(0, 1fr) auto;
      gap: var(--space-6);
      align-items: stretch;
      padding: var(--space-8);
      overflow: hidden;
      border-radius: var(--radius-lg);
      background:
        radial-gradient(90% 60% at 0% 0%, var(--tone-azure), transparent 70%),
        radial-gradient(70% 55% at 100% 35%, var(--tone-rose), transparent 70%),
        radial-gradient(80% 60% at 30% 100%, var(--tone-mint), transparent 75%), var(--tone-lilac);
    }
  }

  /* Logo + nom : lien vers l'accueil */
  .auth__home {
    display: inline-flex;
    gap: var(--space-3);
    align-items: center;
    justify-self: start;
    font-size: var(--text-xl);
    font-weight: var(--weight-bold);
    text-decoration: none;
    border-radius: var(--radius-md);
  }

  .auth__logo {
    inline-size: 2.5rem;
    block-size: 2.5rem;
    filter: drop-shadow(0 0.25rem 0.5rem var(--shadow-color));

    @media (width >= 64rem) {
      inline-size: 3.5rem;
      block-size: 3.5rem;
    }
  }

  /* Collage décoratif (avatars, photos, bulles) : bureau uniquement */
  .auth__collage {
    display: none;

    @media (width >= 64rem) {
      position: relative;
      display: block;
      place-self: center center;
      inline-size: 100%;
      max-inline-size: 30rem;
      aspect-ratio: 1 / 0.92;
      max-block-size: 100%;
    }
  }

  .auth__float {
    position: absolute;
    inset-block-start: var(--y, 0);
    inset-inline-start: var(--x, 0);
    rotate: var(--r, 0deg);

    @media (prefers-reduced-motion: no-preference) {
      animation: auth-float 6s ease-in-out var(--delay, 0s) infinite alternate;
    }
  }

  .auth__photo {
    inline-size: var(--w, 30%);
    block-size: auto;
    aspect-ratio: 2 / 3;
    object-fit: cover;
    border: 4px solid var(--color-surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
  }

  .auth__avatar {
    --avatar-size: var(--size, 3.25rem);

    border: 3px solid var(--color-surface);
    box-shadow: var(--shadow-md);
  }

  /* Bulle de notification factice (« Grâce aime votre photo ») */
  .auth__bubble {
    display: flex;
    gap: var(--space-2);
    align-items: center;
    padding: var(--space-1) var(--space-3) var(--space-1) var(--space-1);
    font-size: var(--text-xs);
    font-weight: var(--weight-medium);
    white-space: nowrap;
    background-color: var(--color-surface);
    border-radius: var(--radius-full);
    box-shadow: var(--shadow-md);
  }

  .auth__dot {
    inline-size: 0.625rem;
    aspect-ratio: 1;
    background-color: var(--tone, var(--tone-violet));
    border-radius: 50%;
  }

  /* Accroche et preuve sociale : bureau uniquement */
  .auth__pitch {
    display: none;

    @media (width >= 64rem) {
      display: grid;
      gap: var(--space-4);
    }
  }

  .auth__tagline {
    max-inline-size: 22ch;
    font-size: clamp(1.5rem, 1rem + 1vw, 1.875rem);
    font-weight: var(--weight-bold);
    line-height: var(--leading-tight);
    text-wrap: balance;
  }

  .auth__proof {
    display: flex;
    gap: var(--space-3);
    align-items: center;
    font-size: var(--text-sm);
  }

  .auth__stack {
    display: flex;

    & .avatar {
      border: 2px solid var(--color-surface);

      & + .avatar {
        margin-inline-start: calc(var(--space-3) * -1);
      }
    }
  }

  /* ---------- Colonne du formulaire ---------- */
  .auth__content {
    display: grid;
    grid-template-rows: minmax(0, 1fr) auto;
    gap: var(--space-8);
    justify-items: center;

    @media (width >= 64rem) {
      padding: var(--space-8) var(--space-10) var(--space-4);
    }
  }

  .auth__body {
    display: grid;
    gap: var(--space-6);
    align-content: start;
    inline-size: 100%;

    @media (width >= 64rem) {
      align-content: center;
      inline-size: min(100%, 25rem);
    }

    @media (prefers-reduced-motion: no-preference) {
      animation: auth-enter 500ms var(--ease-out) both;
    }
  }

  .auth__header {
    display: grid;
    gap: var(--space-2);
  }

  .auth__title {
    font-size: clamp(1.5rem, 1.25rem + 0.8vw, 1.875rem);
    font-weight: var(--weight-bold);
  }

  .auth__subtitle {
    font-size: var(--text-sm);
    color: var(--color-text-muted);
  }

  /* Pastille d'icône au-dessus du titre (mot de passe oublié, confirmation) */
  .auth__icon {
    display: grid;
    place-items: center;
    inline-size: 3.5rem;
    aspect-ratio: 1;
    margin-block-end: var(--space-2);
    background-color: var(--tone, var(--tone-lilac));
    border-radius: var(--radius-md);

    & .icon {
      inline-size: 1.75rem;
      block-size: 1.75rem;
    }
  }

  /* Prénom + nom côte à côte dès que possible (la colonne du formulaire est étroite) */
  .auth__row {
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 9rem), 1fr));
  }

  /* « Se souvenir de moi » + « Mot de passe oublié ? » */
  .auth__options {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2) var(--space-4);
    align-items: center;
    justify-content: space-between;
  }

  .auth__link {
    font-size: var(--text-sm);
    font-weight: var(--weight-semibold);
    color: var(--color-text);
    text-decoration: underline 1px var(--color-border-strong);
    text-underline-offset: 0.2em;

    &:hover {
      text-decoration-color: currentcolor;
    }
  }

  /* « ← Retour à la connexion » */
  .auth__back {
    display: inline-flex;
    gap: var(--space-2);
    align-items: center;
  }

  .auth__submit {
    min-block-size: 2.75rem;

    &[aria-disabled='true'] {
      cursor: progress;
    }
  }

  /* Séparateur « ou » entre deux traits */
  .auth__separator {
    display: flex;
    gap: var(--space-3);
    align-items: center;
    font-size: var(--text-xs);
    color: var(--color-text-muted);

    &::before,
    &::after {
      flex: 1;
      content: '';
      border-block-start: 1px solid var(--color-border);
    }
  }

  /* « Pas encore de compte ? Créer un compte » */
  .auth__switch {
    font-size: var(--text-sm);
    color: var(--color-text-muted);
    text-align: center;

    & a {
      font-weight: var(--weight-semibold);
      color: var(--color-text);
    }
  }

  .auth__footer {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-1) var(--space-4);
    justify-content: center;
    font-size: var(--text-xs);
    color: var(--color-text-muted);

    & a {
      text-decoration: none;

      &:hover {
        color: var(--color-text);
        text-decoration: underline;
      }
    }
  }

  /* ---------- Force du mot de passe (inscription) ---------- */
  .auth__meter {
    --meter-color: var(--color-border-strong);

    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: var(--space-2) var(--space-1);

    &[data-level='1'] {
      --meter-color: var(--color-danger);
    }

    &[data-level='2'] {
      --meter-color: var(--tone-sun);
    }

    &[data-level='3'] {
      --meter-color: var(--color-focus);
    }

    &[data-level='4'] {
      --meter-color: var(--color-success);
    }
  }

  .auth__meter-bar {
    block-size: 0.375rem;
    background-color: var(--color-border);
    border-radius: var(--radius-full);
    transition: background-color var(--duration) var(--ease-out);
  }

  /* Niveau N : les N premières barres prennent la couleur du niveau */
  .auth__meter[data-level='1'] > .auth__meter-bar:nth-child(-n + 1),
  .auth__meter[data-level='2'] > .auth__meter-bar:nth-child(-n + 2),
  .auth__meter[data-level='3'] > .auth__meter-bar:nth-child(-n + 3),
  .auth__meter[data-level='4'] > .auth__meter-bar {
    background-color: var(--meter-color);
  }

  .auth__meter-label {
    grid-column: 1 / -1;
    font-size: var(--text-xs);
    color: var(--color-text-muted);

    & strong {
      font-weight: var(--weight-semibold);
      color: var(--color-text);
    }
  }

  /* Liste des règles : chaque règle respectée passe au vert (data-met) */
  .auth__rules {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 10rem), 1fr));
    gap: var(--space-1) var(--space-3);
    font-size: var(--text-xs);
    color: var(--color-text-muted);
  }

  .auth__rule {
    display: flex;
    gap: var(--space-1);
    align-items: center;
    transition: color var(--duration) var(--ease-out);

    & .icon {
      inline-size: 1.1em;
      block-size: 1.1em;
      opacity: 0.45;
      transition:
        opacity var(--duration) var(--ease-out),
        scale var(--duration) var(--ease-spring);
    }

    &[data-met] {
      color: var(--color-text);

      & .icon {
        color: var(--color-success);
        opacity: 1;
        scale: 1.1;
      }
    }
  }

  /* ---------- Confirmation d'envoi (mot de passe oublié) ---------- */
  .auth__success {
    display: grid;
    gap: var(--space-4);
    justify-items: center;
    padding: var(--space-6);
    text-align: center;
    background-color: var(--tone-mint);
    border-radius: var(--radius-lg);

    & .auth__icon {
      margin: 0;
      background-color: var(--color-surface);
      border-radius: 50%;
    }

    @media (prefers-reduced-motion: no-preference) {
      animation: auth-enter 400ms var(--ease-out) both;
    }
  }

  .auth__success-title {
    font-size: var(--text-lg);
    font-weight: var(--weight-bold);

    &:focus {
      outline: none;
    }
  }

  .auth__success-text {
    font-size: var(--text-sm);

    & strong {
      overflow-wrap: anywhere;
    }
  }

  .auth__actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    justify-content: center;
  }

  .auth__hint {
    font-size: var(--text-xs);
    color: var(--color-text-muted);
    text-align: center;

    & .auth__link {
      font-size: inherit;
    }
  }

  /* ---------- Variante 404 ---------- */
  .auth--lost {
    @media (width >= 64rem) {
      grid-template-rows: auto minmax(0, 1fr);
      grid-template-columns: minmax(0, 1fr);
      inline-size: min(100%, 44rem);
      min-block-size: auto;
      padding: var(--space-10);
    }

    & .auth__brand {
      justify-content: center;
      padding: 0;
      background: none;
    }

    & .auth__body {
      inline-size: min(100%, 32rem);
      justify-items: center;
      text-align: center;
    }

    & .auth__content {
      padding: 0;
    }
  }

  /* Illustration : « 4 [bulle du logo] 4 » entouré d'avatars flottants */
  .auth__scene {
    position: relative;
    display: grid;
    place-items: center;
    inline-size: 100%;
    padding-block: var(--space-6);
  }

  .auth__digits {
    display: flex;
    gap: 0.04em;
    align-items: center;
    font-size: clamp(5.5rem, 3rem + 14vw, 9.5rem);
    font-weight: var(--weight-bold);
    line-height: 1;
    letter-spacing: -0.04em;
    color: var(--tone-violet);

    @supports (background-clip: text) {
      color: transparent;
      background: linear-gradient(
        135deg,
        var(--tone-azure),
        var(--tone-violet) 45%,
        var(--tone-rose)
      );
      background-clip: text;
    }

    & svg {
      inline-size: 0.95em;
      block-size: 0.95em;
      filter: drop-shadow(0 0.75rem 1.25rem var(--shadow-color));

      @media (prefers-reduced-motion: no-preference) {
        animation: auth-wobble 4s ease-in-out infinite;
      }
    }
  }

  /* Ombre au sol sous la bulle */
  .auth__shadow {
    inline-size: 40%;
    block-size: 0.75rem;
    margin-block-start: var(--space-2);
    background: radial-gradient(closest-side, var(--shadow-color), transparent);
    border-radius: 50%;
  }

  @keyframes auth-float {
    to {
      translate: 0 -0.625rem;
    }
  }

  @keyframes auth-wobble {
    0%,
    100% {
      translate: 0 0;
      rotate: -4deg;
    }

    50% {
      translate: 0 -0.5rem;
      rotate: 4deg;
    }
  }

  @keyframes auth-enter {
    from {
      opacity: 0;
      translate: 0 0.75rem;
    }
  }
}
