/*
 * Champ de saisie Material 3 Expressive — variantes « filled » et « outlined ».
 *
 * @material/web ne vendorise aucun CSS pour le champ de saisie : tout ce qui
 * suit est écrit à partir des tokens, dans la forme du CSS amont (tokens
 * locaux en tête de règle, puis propriétés qui ne lisent que ces tokens).
 *
 * ── Encoche du label (outlined) ──────────────────────────────────────────
 * Le contour n'est pas une simple `border` : il est découpé en trois segments
 * posés côte à côte (`-outline-start`, `-outline-notch`, `-outline-end`). Le
 * segment central contient une copie invisible du label à sa taille flottante ;
 * c'est elle qui donne sa largeur à l'encoche, sans mesure JS et sans dépendre
 * de la langue. Ouvrir l'encoche revient alors à rendre transparent le trait
 * supérieur de ce seul segment.
 *
 * Deux techniques ont été écartées :
 *  · `<fieldset>`/`<legend>`, où la géométrie du découpage du trait dépend du
 *    moteur de rendu et impose des décalages magiques (les `top: -5px` de MUI)
 *    — invérifiables à partir de tokens ;
 *  · le dégradé de bordure, qui exige de connaître la largeur du label en JS.
 *
 * ── Spécificité ──────────────────────────────────────────────────────────
 * Toutes les règles d'état sont écrites en `:where()` : elles pèsent donc
 * toutes 0-1-0 et c'est l'ordre du fichier qui tranche — variante, survol,
 * focus, erreur, désactivé. Même discipline que le CSS amont.
 */

@layer md.sys, md.comp.ripple, md.comp.focus-ring;

@layer md.comp.text-field {
  .text-field {
    /* Conteneur */
    --container-color: transparent;
    --container-height: var(--md-sys-space-700);
    --container-shape: var(--md-sys-shape-corner-xs);

    /* Contour — épaisseurs sans token amont, exposées ici pour rester
       surchargeables par instance. */
    --outline-color: transparent;
    --outline-width: 0px;
    --outline-width-focus: 0px;
    --outline-label-space: var(--md-sys-space-50);

    /* Trait actif du bas (variante « filled ») */
    --active-indicator-color: transparent;
    --active-indicator-height: 1px;
    /* 2 dp : `FilledTextFieldTokens.FocusActiveIndicatorHeight`. */
    --active-indicator-height-focus: 2px;

    /* Label flottant */
    --label-text: var(--md-sys-typescale-body-lg);
    --label-text-tracking: var(--md-sys-typescale-body-lg-tracking);
    --label-text-color: var(--md-sys-color-on-surface-variant);
    --label-text-populated: var(--md-sys-typescale-body-sm);
    --label-text-populated-tracking: var(--md-sys-typescale-body-sm-tracking);
    --label-text-populated-color: var(--md-sys-color-on-surface-variant);

    /* Saisie */
    --input-text: var(--md-sys-typescale-body-lg);
    --input-text-tracking: var(--md-sys-typescale-body-lg-tracking);
    --input-text-color: var(--md-sys-color-on-surface);
    --input-line-height: var(--md-sys-space-300);
    --placeholder-text-color: var(--md-sys-color-on-surface-variant);
    --caret-color: var(--md-sys-color-primary);
    --affix-text-color: var(--md-sys-color-on-surface-variant);

    /* Texte d'aide et compteur */
    --supporting-text: var(--md-sys-typescale-body-sm);
    --supporting-text-tracking: var(--md-sys-typescale-body-sm-tracking);
    --supporting-text-color: var(--md-sys-color-on-surface-variant);

    /* Icônes */
    --icon-color: var(--md-sys-color-on-surface-variant);
    --trailing-icon-color: var(--md-sys-color-on-surface-variant);
    --icon-size: var(--md-sys-space-300);
    --icon-label-space: var(--md-sys-space-200);

    /* Gouttières */
    --leading-space: var(--md-sys-space-200);
    --trailing-space: var(--md-sys-space-200);
    --supporting-space: var(--md-sys-space-200);
    --input-space-top: var(--md-sys-space-200);
    --input-space-bottom: var(--md-sys-space-200);

    /* Distance entre le bord de départ et le label. Décalée par
       `text-field-leading-icon`, pour que label et encoche suivent l'icône. */
    --label-inset: var(--leading-space);
    --label-rest-top: 50%;
    --label-float-top: 0%;

    /* Multiligne */
    --textarea-rows: 3;
    --textarea-max-block-size: none;

    --motion-duration: var(--md-sys-motion-duration-short4);
    --motion-easing: var(--md-sys-motion-easing-emphasized);

    display: flex;
    flex-direction: column;
    min-inline-size: 0;
  }

  .text-field:where(.text-field-leading-icon) {
    --label-inset: calc(var(--leading-space) + var(--icon-size) + var(--icon-label-space));
  }

  /* ── Variante « filled » ─────────────────────────────────────────────── */

  .text-field:where(.text-field-filled) {
    --container-color: var(--md-sys-color-surface-container-highest);
    --container-shape: var(--md-sys-shape-corner-xs) var(--md-sys-shape-corner-xs)
      var(--md-sys-shape-corner-none) var(--md-sys-shape-corner-none);
    --active-indicator-color: var(--md-sys-color-on-surface-variant);
    --input-space-top: var(--md-sys-space-300);
    --input-space-bottom: var(--md-sys-space-100);
    /* 0.5em suit la taille du label pendant qu'elle s'anime : le label reste
       calé sur sa demi-hauteur quelle que soit l'échelle typographique. */
    --label-float-top: calc(var(--md-sys-space-100) + 0.5em);
  }

  /* ── Variante « outlined » ───────────────────────────────────────────── */

  .text-field:where(.text-field-outlined) {
    --outline-color: var(--md-sys-color-outline);
    --outline-width: 1px;
    --outline-width-focus: 2px;
    --active-indicator-height: 0px;
    --active-indicator-height-focus: 0px;
  }

  /* ── Multiligne ──────────────────────────────────────────────────────── */

  .text-field:where(.text-field-textarea) {
    --container-height: auto;
    /* Le label au repos se cale sur la première ligne, pas sur le centre. */
    --label-rest-top: calc(var(--input-space-top) + var(--input-line-height) / 2);
  }

  /* ── Survol ──────────────────────────────────────────────────────────── */

  .text-field:where(:has(.text-field-box:is(:hover, .hover))):where(
      :not([data-disabled], [data-focused])
    ) {
    --active-indicator-color: var(--md-sys-color-on-surface);
    --outline-color: var(--md-sys-color-on-surface);
    --label-text-color: var(--md-sys-color-on-surface-variant);
  }

  /* ── Focus ───────────────────────────────────────────────────────────── */

  .text-field:where([data-focused]) {
    --active-indicator-color: var(--md-sys-color-primary);
    --outline-color: var(--md-sys-color-primary);
    --outline-width: var(--outline-width-focus);
    --label-text-color: var(--md-sys-color-primary);
    --label-text-populated-color: var(--md-sys-color-primary);
  }

  /* ── Erreur ──────────────────────────────────────────────────────────── */

  .text-field:where([data-invalid]) {
    --active-indicator-color: var(--md-sys-color-error);
    --outline-color: var(--md-sys-color-error);
    --label-text-color: var(--md-sys-color-error);
    --label-text-populated-color: var(--md-sys-color-error);
    --supporting-text-color: var(--md-sys-color-error);
    --trailing-icon-color: var(--md-sys-color-error);
    --caret-color: var(--md-sys-color-error);
  }

  .text-field:where([data-invalid]):where(:has(.text-field-box:is(:hover, .hover))):where(
      :not([data-disabled], [data-focused])
    ) {
    --active-indicator-color: var(--md-sys-color-on-error-container);
    --outline-color: var(--md-sys-color-on-error-container);
    --label-text-color: var(--md-sys-color-on-error-container);
    --trailing-icon-color: var(--md-sys-color-on-error-container);
  }

  /* ── Désactivé ───────────────────────────────────────────────────────── */

  .text-field:where([data-disabled]) {
    --container-color: hsl(from var(--md-sys-color-on-surface) h s l / 4%);
    --outline-color: hsl(from var(--md-sys-color-outline) h s l / 12%);
    --active-indicator-color: hsl(from var(--md-sys-color-on-surface) h s l / 38%);
    --label-text-color: hsl(from var(--md-sys-color-on-surface) h s l / 38%);
    --label-text-populated-color: hsl(from var(--md-sys-color-on-surface) h s l / 38%);
    --input-text-color: hsl(from var(--md-sys-color-on-surface) h s l / 38%);
    --placeholder-text-color: hsl(from var(--md-sys-color-on-surface) h s l / 38%);
    --affix-text-color: hsl(from var(--md-sys-color-on-surface) h s l / 38%);
    --supporting-text-color: hsl(from var(--md-sys-color-on-surface) h s l / 38%);
    --icon-color: hsl(from var(--md-sys-color-on-surface) h s l / 38%);
    --trailing-icon-color: hsl(from var(--md-sys-color-on-surface) h s l / 38%);
  }

  .text-field:where(.text-field-outlined):where([data-disabled]) {
    --container-color: transparent;
  }

  /* ── Boîte ───────────────────────────────────────────────────────────── */

  .text-field-box {
    position: relative;
    display: flex;
    align-items: center;
    gap: var(--icon-label-space);
    min-block-size: var(--container-height);
    padding-inline: var(--leading-space) var(--trailing-space);
    border-radius: var(--container-shape);
    background-color: var(--container-color);
    color: var(--input-text-color);
    --md-icon-color: var(--icon-color);
    --md-icon-size: var(--icon-size);
  }

  /* La couche d'état de survol vient de `.ripple`, qui lit `currentColor` : la
     boîte porte donc déjà la bonne couleur. Sa transition doit être réinjectée
     ici, sinon la déclaration ci-dessous l'écrase. */
  .text-field-filled .text-field-box {
    transition:
      background-color var(--motion-duration) var(--motion-easing),
      var(--ripple-transition);
  }

  .text-field-outlined .text-field-box {
    transition: background-color var(--motion-duration) var(--motion-easing);
  }

  .text-field-textarea .text-field-box {
    align-items: start;
  }

  /* ── Trait actif (filled) ────────────────────────────────────────────── */

  .text-field-filled .text-field-box::before,
  .text-field-filled .text-field-box::after {
    content: "";
    position: absolute;
    inset: auto 0 0;
    pointer-events: none;
  }

  .text-field-filled .text-field-box::before {
    block-size: var(--active-indicator-height);
    background-color: var(--active-indicator-color);
    transition: background-color var(--motion-duration) var(--motion-easing);
  }

  /* Le trait de focus s'ouvre depuis le centre, par-dessus le trait au repos. */
  .text-field-filled .text-field-box::after {
    block-size: var(--active-indicator-height-focus);
    background-color: var(--active-indicator-color);
    transform: scaleX(0);
    transition: transform var(--motion-duration) var(--motion-easing);
  }

  .text-field-filled:where([data-focused]) .text-field-box::after {
    transform: scaleX(1);
  }

  /* ── Contour en trois segments (outlined) ────────────────────────────── */

  .text-field-outline {
    position: absolute;
    inset: 0;
    display: flex;
    pointer-events: none;
  }

  .text-field-outline-start,
  .text-field-outline-notch,
  .text-field-outline-end {
    border-block: var(--outline-width) solid var(--outline-color);
    transition:
      border-color var(--motion-duration) var(--motion-easing),
      border-width var(--motion-duration) var(--motion-easing);
  }

  .text-field-outline-start {
    flex: 0 0 auto;
    inline-size: calc(var(--label-inset) - var(--outline-label-space));
    border-inline-start: var(--outline-width) solid var(--outline-color);
    border-start-start-radius: var(--container-shape);
    border-end-start-radius: var(--container-shape);
  }

  .text-field-outline-notch {
    flex: 0 1 auto;
    overflow: hidden;
    padding-inline: var(--outline-label-space);
  }

  .text-field-outline-end {
    flex: 1 1 auto;
    min-inline-size: var(--outline-label-space);
    border-inline-end: var(--outline-width) solid var(--outline-color);
    border-start-end-radius: var(--container-shape);
    border-end-end-radius: var(--container-shape);
  }

  /* Copie invisible du label : elle ne sert qu'à donner sa largeur à
     l'encoche. `visibility: hidden` conserve la place, `display: none` non. */
  .text-field-outline-label {
    display: block;
    visibility: hidden;
    white-space: nowrap;
    font: var(--label-text-populated);
    letter-spacing: var(--label-text-populated-tracking);
    line-height: 1;
  }

  /* ── Label ───────────────────────────────────────────────────────────── */

  .text-field-label {
    position: absolute;
    inset-block-start: var(--label-rest-top);
    inset-inline-start: var(--label-inset);
    max-inline-size: calc(100% - var(--label-inset) - var(--trailing-space));
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    /* La saisie occupe toute la boîte : les clics doivent la traverser. */
    pointer-events: none;
    transform: translateY(-50%);
    color: var(--label-text-color);
    font: var(--label-text);
    letter-spacing: var(--label-text-tracking);
    /* Interlignage neutralisé pour que seule la taille de police pilote le
       déplacement — sinon le label saute au changement d'échelle. */
    line-height: 1;
    transition:
      inset-block-start var(--motion-duration) var(--motion-easing),
      font-size var(--motion-duration) var(--motion-easing),
      color var(--motion-duration) var(--motion-easing);
  }

  /*
   * Le label remonte dès que le champ est rempli, focalisé, ou forcé par
   * `.text-field-float` (présence d'un placeholder, d'un préfixe…).
   * `:autofill` et `:not(:placeholder-shown)` rattrapent les remplissages que
   * React ne voit pas passer (autocomplétion du navigateur).
   */
  .text-field:where([data-filled], [data-focused], .text-field-float) .text-field-label,
  .text-field:where(:has(.text-field-input:is(:autofill, :not(:placeholder-shown))))
    .text-field-label {
    inset-block-start: var(--label-float-top);
    color: var(--label-text-populated-color);
    font: var(--label-text-populated);
    letter-spacing: var(--label-text-populated-tracking);
    line-height: 1;
  }

  /* Encoche ouverte : le trait supérieur disparaît sous le label flottant. */
  .text-field:where([data-filled], [data-focused], .text-field-float) .text-field-outline-notch,
  .text-field:where(:has(.text-field-input:is(:autofill, :not(:placeholder-shown))))
    .text-field-outline-notch {
    border-block-start-color: transparent;
  }

  /* ── Zone de saisie ──────────────────────────────────────────────────── */

  .text-field-main {
    flex: 1 1 auto;
    min-inline-size: 0;
    align-self: stretch;
    display: flex;
    align-items: center;
    gap: var(--md-sys-space-50);
    padding-block: var(--input-space-top) var(--input-space-bottom);
  }

  .text-field-textarea .text-field-main {
    align-items: start;
  }

  .text-field-input {
    flex: 1 1 auto;
    inline-size: 100%;
    min-inline-size: 0;
    appearance: none;
    border: none;
    outline: none;
    background: none;
    padding: 0;
    margin: 0;
    color: var(--input-text-color);
    caret-color: var(--caret-color);
    font: var(--input-text);
    letter-spacing: var(--input-text-tracking);
    line-height: var(--input-line-height);
  }

  .text-field-input::placeholder {
    color: var(--placeholder-text-color);
    opacity: 1;
  }

  /* Safari décolore les champs désactivés hors de toute règle CSS. */
  .text-field-input:disabled {
    -webkit-text-fill-color: var(--input-text-color);
    opacity: 1;
  }

  /* Multiligne. `field-sizing: content` fait grandir la zone avec le texte ;
     là où il n'est pas implémenté, l'attribut `rows` fixe la hauteur et
     l'ascenseur natif prend le relais. `field-sizing` ignorant `rows`, la
     hauteur de départ est reconstruite depuis `--textarea-rows`, que le
     composant pose sur l'élément. */
  textarea.text-field-input {
    field-sizing: content;
    resize: none;
    min-block-size: calc(var(--input-line-height) * var(--textarea-rows));
    max-block-size: var(--textarea-max-block-size);
  }

  /* Préfixe et suffixe : masqués tant que le label occupe la ligne de saisie,
     sinon ils se chevauchent. Leur largeur reste réservée pour éviter tout
     décalage à l'apparition. */
  .text-field-affix {
    flex: 0 0 auto;
    color: var(--affix-text-color);
    font: var(--input-text);
    letter-spacing: var(--input-text-tracking);
    line-height: var(--input-line-height);
    opacity: 0;
    transition: opacity var(--motion-duration) var(--motion-easing);
  }

  .text-field:where([data-filled], [data-focused], .text-field-float) .text-field-affix {
    opacity: 1;
  }

  /* ── Icônes ──────────────────────────────────────────────────────────── */

  .text-field-icon {
    flex: 0 0 auto;
  }

  .text-field-icon-trailing {
    --md-icon-color: var(--trailing-icon-color);
  }

  /* Alignées sur la première ligne de texte, pas sur le centre de la zone. */
  .text-field-textarea .text-field-icon {
    margin-block-start: calc(
      var(--input-space-top) + (var(--input-line-height) - var(--icon-size)) / 2
    );
  }

  /* ── Texte d'aide et compteur ────────────────────────────────────────── */

  .text-field-support {
    display: flex;
    align-items: start;
    justify-content: space-between;
    gap: var(--md-sys-space-200);
    padding-inline: var(--supporting-space);
    padding-block-start: var(--md-sys-space-50);
    color: var(--supporting-text-color);
    font: var(--supporting-text);
    letter-spacing: var(--supporting-text-tracking);
  }

  .text-field-support-text {
    margin: 0;
    min-inline-size: 0;
  }

  .text-field-counter {
    flex: 0 0 auto;
    margin-inline-start: auto;
    font-variant-numeric: tabular-nums;
  }

  @media (prefers-reduced-motion: reduce) {
    .text-field-label,
    .text-field-affix,
    .text-field-outline-start,
    .text-field-outline-notch,
    .text-field-outline-end,
    .text-field-filled .text-field-box::after {
      transition: none;
    }
  }
}
