/*
 * Couche responsive commune de FacControl.
 *
 * Chargee apres les feuilles communes et avant les feuilles propres a une
 * page. Elle ne change ni les couleurs ni les composants : elle adapte leur
 * taille et leur disposition au telephone, a la tablette et au PC.
 *
 *   1. Garde-fous contre les debordements
 *   2. En-tete et menu lateral sur telephone et tablette
 *   3. Contenu : marges des petits ecrans, navigation de module
 *   4. Tableaux : densite selon la place disponible
 *   5. Fenetres modales et popups
 *   6. Ecrans tactiles : cibles de 44 px et champs de 16 px
 *
 * Largeurs de reference : 360, 390, 768, 1024, 1440, 1536 et 1707 px. A
 * 1536 px (ecran de 1920 px a 125 %), menu lateral ouvert, un tableau dispose
 * d'environ 1120 px ; a 1707 px (2560 px a 150 %), d'environ 1290 px.
 */

:root {
  /* Plus petite cible confortable au doigt. */
  --fc-touch: 44px;
}

/* ------------------------------------------------------------------------
 * 1. Garde-fous contre les debordements
 * --------------------------------------------------------------------- */
#main {
  min-width: 0;
}

#main img,
#main video,
#main canvas {
  max-width: 100%;
}

/* Un mot ou un code tres long passe a la ligne au lieu de pousser la carte. */
#main .card-body,
#main .alert,
.modal-body,
.dropdown-menu {
  overflow-wrap: break-word;
}

#main pre {
  max-width: 100%;
  overflow-x: auto;
}

/* ------------------------------------------------------------------------
 * 2. En-tete et menu lateral
 * --------------------------------------------------------------------- */
/* Le bouton du menu est une icone : on lui donne une vraie surface d'appui. */
.header .toggle-sidebar-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: var(--fc-touch);
  height: var(--fc-touch);
  margin-left: 4px;
  padding-left: 0;
  border-radius: 10px;
}

.header-nav .nav-profile {
  min-width: var(--fc-touch);
  min-height: var(--fc-touch);
  justify-content: center;
}

/* Theme et langue ranges dans le menu du profil sous 768 px (voir base.html). */
.fc-header-prefs {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 15px;
}

.header-nav .profile .fc-header-link {
  white-space: normal;
}

@media (max-width: 767.98px) {
  .header {
    padding-left: 8px;
  }

  /*
   * Le PNG du logo (1536 x 1024) porte une large marge transparente : a
   * 112 px de haut, le dessin visible mesure 136 x 35 px. Les marges
   * negatives rognent uniquement ce vide, pour laisser la place au bouton du
   * menu, aux alertes et au profil sur un ecran de 360 px.
   */
  .header .logo img {
    max-height: 112px !important;
    margin: 0 -14px 0 -8px;
  }

  .header-nav > ul > .nav-item {
    margin-right: 4px !important;
  }

  .header-nav > ul > .nav-item.dropdown {
    margin-right: 0 !important;
    padding-right: 8px !important;
  }

  /* Le menu du profil ne depasse jamais de l'ecran. */
  .header-nav .profile {
    max-width: calc(100vw - 16px);
  }
}

@media (max-width: 359.98px) {
  .header .logo img {
    max-height: 96px !important;
    margin: 0 -12px 0 -7px;
  }

  #faccontrol-install-app {
    display: none !important;
  }
}

/* Menu lateral en superposition : jamais plus large que l'ecran. */
@media (max-width: 1199.98px) {
  .sidebar {
    max-width: 86vw;
  }

}

/* Voile derriere le menu ouvert : un appui dessus le referme (main.js). */
@media screen and (max-width: 1199.98px) {
  body.toggle-sidebar:not(.pos-compact-sidebar)::before {
    content: "";
    position: fixed;
    inset: 60px 0 0;
    z-index: 995;
    background: rgba(15, 23, 42, 0.38);
  }
}

/* Au point de vente, la rampe d'icones garde son propre voile des 992 px. */
@media screen and (max-width: 991.98px) {
  body.toggle-sidebar.pos-compact-sidebar::before {
    content: "";
    position: fixed;
    inset: 60px 0 0;
    z-index: 995;
    background: rgba(15, 23, 42, 0.38);
  }
}

/* ------------------------------------------------------------------------
 * 3. Contenu : marges des petits ecrans, navigation de module
 * --------------------------------------------------------------------- */
@media (max-width: 575.98px) {
  #main {
    padding: 16px 12px;
  }

  .card-body {
    padding: 0 16px 16px;
  }

  /* Pagination : les numeros passent a la ligne au lieu de deborder. */
  .pagination {
    flex-wrap: wrap;
    row-gap: 6px;
  }
}

/* Navigation d'un module (Achats & Fournisseurs) : sous 1800 px elle passe
   au-dessus du contenu, en rangee d'onglets, comme elle le fait deja sur
   tablette. Le menu lateral presente les memes liens juste a cote ; la page
   recupere ainsi toute la largeur pour ses tableaux (jusqu'a 18 colonnes
   dans les rapports de gestion). A partir de 1800 px la colonne revient. */
@media (min-width: 992px) and (max-width: 1799.98px) {
  .fc-module .fc-settings-layout {
    grid-template-columns: minmax(0, 1fr);
    gap: 16px;
  }

  .fc-module .fc-module-nav {
    position: static;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 2px;
    padding: 6px 8px;
    border-radius: 12px;
  }

  .fc-module .fc-module-nav :is(.fc-settings-nav-title, .fc-settings-nav-group) {
    display: none;
  }

  .fc-module .fc-module-nav .fc-settings-nav-link {
    flex: 0 0 auto;
    padding: 8px 9px;
    white-space: nowrap;
  }
}

/* ------------------------------------------------------------------------
 * 4. Tableaux : densite selon la place disponible
 *
 * Sur PC, un tableau doit tenir en entier dans la fenetre a 100 % de zoom.
 * Le conteneur du tableau mesure la place reelle (menu lateral ouvert ou
 * ferme, colonne de navigation du module) et le tableau se resserre selon
 * son nombre de colonnes : texte de 13 px puis 12,5 px (11,5 px a partir de
 * treize colonnes), cellules plus etroites. Le defilement horizontal ne reste
 * qu'en dernier recours.
 *
 * Les tableaux de saisie (champs, listes deroulantes) sont laisses tels
 * quels : leurs menus flottants ne doivent pas etre enfermes dans le
 * conteneur.
 * --------------------------------------------------------------------- */
/* A l'ecran seulement : l'impression des listes et des documents ne change pas. */
@supports (container-type: inline-size) and selector(:has(a)) {
  @media screen {
    :is(.table-responsive, .fc-admin-table-wrap, .datatable-container, .daily-table-scroll, .ca-table-scroll, .pdv-factures-wrapper, .ventes-jour-table-wrapper, .factures-table-wrapper, .reductions-table-wrapper):not(:has(input:not([type="checkbox"], [type="radio"], [type="hidden"]), select, textarea, .dropdown-menu)) {
      container: fc-table / inline-size;
    }
  }
}

/* --- Niveau compact : 13 px ------------------------------------------- */
@container fc-table (max-width: 759.98px) {
  :is(table, .table, .datatable-table) {
    font-size: 0.8125rem;
  }

  :is(table, .table, .datatable-table) > :not(caption) > * > * {
    padding: 0.5rem 0.6rem;
  }

  :is(table, .table, .datatable-table) > thead > tr > th {
    font-size: 0.8125rem;
    letter-spacing: 0;
  }

  :is(table, .table, .datatable-table) :is(.badge, .small, small) {
    font-size: 0.75rem;
  }
}

@container fc-table (max-width: 1199.98px) {
  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(7)) {
    font-size: 0.8125rem;
  }

  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(7)) > :not(caption) > * > * {
    padding: 0.5rem 0.6rem;
  }

  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(7)) > thead > tr > th {
    font-size: 0.8125rem;
    letter-spacing: 0;
  }

  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(7)) :is(.badge, .small, small) {
    font-size: 0.75rem;
  }
}

@container fc-table (max-width: 1499.98px) {
  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(9)) {
    font-size: 0.8125rem;
  }

  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(9)) > :not(caption) > * > * {
    padding: 0.5rem 0.6rem;
  }

  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(9)) > thead > tr > th {
    font-size: 0.8125rem;
    letter-spacing: 0;
  }

  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(9)) :is(.badge, .small, small) {
    font-size: 0.75rem;
  }
}

@container fc-table (max-width: 1699.98px) {
  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(10)) {
    font-size: 0.8125rem;
  }

  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(10)) > :not(caption) > * > * {
    padding: 0.5rem 0.6rem;
  }

  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(10)) > thead > tr > th {
    font-size: 0.8125rem;
    letter-spacing: 0;
  }

  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(10)) :is(.badge, .small, small) {
    font-size: 0.75rem;
  }
}

/* --- Niveau dense : 12,5 px, en-tetes sur deux lignes si besoin ---------- */
@container fc-table (max-width: 899.98px) {
  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(7)) {
    font-size: 0.78125rem;
  }

  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(7)) > :not(caption) > * > * {
    padding: 0.4rem 0.45rem;
  }

  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(7)) > thead > tr > th {
    font-size: 0.78125rem;
    white-space: normal;
  }
}

@container fc-table (max-width: 1299.98px) {
  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(10)) {
    font-size: 0.78125rem;
  }

  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(10)) > :not(caption) > * > * {
    padding: 0.4rem 0.45rem;
  }

  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(10)) > thead > tr > th {
    font-size: 0.78125rem;
    white-space: normal;
  }
}

/* Treize colonnes et plus (grands rapports) : dense des 1700 px. */
@container fc-table (max-width: 1699.98px) {
  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(13)) {
    font-size: 0.78125rem;
  }

  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(13)) > :not(caption) > * > * {
    padding: 0.4rem 0.45rem;
  }

  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(13)) > thead > tr > th {
    font-size: 0.78125rem;
    white-space: normal;
  }
}

/* --- Niveau serre : cellules etroites -----------------------------------
   Neuf colonnes et plus dans moins de 1200 px (fenetre de 1536 px avec le
   menu ouvert, PC portable) : marges de cellule reduites, texte de 12,5 px
   (12 px a partir de dix colonnes), pour garder une ligne par enregistrement
   sans defiler. Pas en dessous de 760 px : sur telephone le tableau defile de
   toute facon, le texte n'a pas a retrecir. */
@container fc-table (min-width: 760px) and (max-width: 1199.98px) {
  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(9)) {
    font-size: 0.78125rem;
  }

  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(9)) > :not(caption) > * > * {
    padding: 0.35rem 0.3rem;
  }

  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(9)) > thead > tr > th {
    font-size: 0.78125rem;
  }

  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(10)),
  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(10)) > thead > tr > th {
    font-size: 0.75rem;
  }
}

/* Treize colonnes et plus dans moins de 1200 px : 11,5 px, le plus petit
   texte admis sur PC, pour que toutes les colonnes restent a l'ecran. */
@container fc-table (min-width: 760px) and (max-width: 1199.98px) {
  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(13)) {
    font-size: 0.71875rem;
  }

  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(13)) > :not(caption) > * > * {
    padding: 0.35rem 0.25rem;
  }

  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(13)) > thead > tr > th,
  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(13)) :is(.badge, .small, small) {
    font-size: 0.71875rem;
  }
}

@container fc-table (min-width: 760px) and (max-width: 1099.98px) {
  :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(13)) > :not(caption) > * > * {
    padding-inline: 0.2rem;
  }
}

/* Tableaux de saisie (lignes d'achat, conditionnements, reapprovisionnement,
   transfert) sur PC : cellules resserrees et en-tetes sur deux lignes. Les
   champs gardent leur taille de texte ; pas de conteneur mesure ici, leurs
   listes flottantes doivent rester libres. */
@media screen and (min-width: 992px) and (max-width: 1919.98px) and (pointer: fine) {
  .table-responsive:has(> .table :is(input:not([type="checkbox"], [type="radio"], [type="hidden"]), select, textarea)) > .table > :not(caption) > * > * {
    padding: 0.5rem 0.4rem;
  }

  .table-responsive:has(> .table :is(input:not([type="checkbox"], [type="radio"], [type="hidden"]), select, textarea)) > .table > thead > tr > th {
    font-size: 0.8125rem;
    letter-spacing: 0;
    white-space: normal;
  }
}

/* A la souris, les boutons d'action suivent la densite du tableau. */
@media (hover: hover) and (pointer: fine) {
  @container fc-table (max-width: 1699.98px) {
    :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(10)) .btn-sm {
      padding: 0.2rem 0.45rem;
      font-size: 0.8125rem;
    }
  }

  @container fc-table (max-width: 1499.98px) {
    :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(9)) .btn-sm {
      padding: 0.2rem 0.45rem;
      font-size: 0.8125rem;
    }
  }

  @container fc-table (max-width: 1199.98px) {
    :is(table, .table, .datatable-table):has(> thead > tr > :nth-child(7)) .btn-sm {
      padding: 0.2rem 0.45rem;
      font-size: 0.8125rem;
    }
  }
}

/* ------------------------------------------------------------------------
 * 5. Fenetres modales et popups
 * --------------------------------------------------------------------- */
@media (max-width: 575.98px) {
  .modal-header,
  .modal-body,
  .modal-footer {
    padding-inline: 16px;
  }

  /* Boutons du pied : larges, faciles a viser, deux par ligne au plus. */
  .modal-footer {
    gap: 8px;
  }

  .modal-footer > .btn,
  .modal-footer > form {
    flex: 1 1 calc(50% - 8px);
    margin: 0;
  }

  .modal-footer > form > .btn {
    width: 100%;
  }

  /* Un tableau dans une modale defile dans la modale, pas dans la page. */
  .modal-body > .table {
    display: block;
    max-width: 100%;
    overflow-x: auto;
  }

  .swal2-popup {
    padding-inline: 12px;
  }

  .swal2-actions {
    gap: 8px;
  }
}

/* ------------------------------------------------------------------------
 * 6. Ecrans tactiles
 *
 * `pointer: coarse` vise l'appareil dont le doigt est l'entree principale
 * (telephone, tablette) : un PC a ecran tactile pilote a la souris garde
 * l'affichage dense du bureau.
 * --------------------------------------------------------------------- */
@media (pointer: coarse) {
  /* Boutons : 44 px de haut et de large par les marges internes. Pas de
     `min-width` : il remplacerait la largeur minimale automatique d'un bouton
     place dans une ligne flexible, qui serait alors ecrase sous son texte. */
  .btn {
    padding-block: 0.6rem;
    padding-inline: 0.875rem;
  }

  .btn-sm,
  .btn-group-sm > .btn {
    padding-block: 0.6875rem;
    padding-inline: 0.875rem;
  }

  .btn-lg,
  .btn-group-lg > .btn {
    padding-block: 0.5rem;
  }

  .btn-close,
  .modal-header .btn-close,
  .alert-dismissible .btn-close {
    box-sizing: border-box;
    flex: 0 0 auto;
    width: var(--fc-touch);
    height: var(--fc-touch);
    padding: 0;
  }

  /* Champs : 44 px de haut et 16 px de texte (pas de zoom force sur iPhone). */
  .form-control,
  .form-select,
  .form-control-sm,
  .form-select-sm,
  .input-group-text,
  .datatable-selector,
  .datatable-input,
  #main :is(input:not([type="checkbox"], [type="radio"], [type="range"], [type="hidden"]), select),
  .modal :is(input:not([type="checkbox"], [type="radio"], [type="range"], [type="hidden"]), select) {
    min-height: var(--fc-touch);
  }

  /* .pos-qty-step : fleches du compteur de quantite, reglees dans quick_billing.css. */
  #main button:not(.btn-close, .datatable-sorter, .pos-qty-step),
  #main summary {
    min-height: var(--fc-touch);
  }

  #main summary {
    display: flex;
    align-items: center;
  }

  #main :is(input:not([type="checkbox"], [type="radio"], [type="range"], [type="file"]), select, textarea),
  .modal :is(input:not([type="checkbox"], [type="radio"], [type="range"], [type="file"]), select, textarea),
  .sidebar :is(input, select) {
    font-size: 1rem;
  }

  .form-check-input {
    width: 1.375em;
    height: 1.375em;
    margin-top: 0.0625em;
  }

  .form-check:not(.form-switch) {
    padding-left: 1.9em;
  }

  .form-check:not(.form-switch) .form-check-input {
    margin-left: -1.9em;
  }

  .form-switch .form-check-input {
    width: 2.75em;
  }

  .form-switch {
    padding-left: 3.25em;
  }

  .form-switch .form-check-input {
    margin-left: -3.25em;
  }

  /* Navigation. */
  .sidebar-nav .nav-link,
  .sidebar-nav .nav-content a {
    min-height: var(--fc-touch);
  }

  .nav-tabs .nav-link,
  .nav-pills .nav-link {
    padding-block: 0.625rem;
  }

  .dropdown-menu .dropdown-item,
  .header-nav .profile .dropdown-item {
    padding-block: 12px;
  }

  #main .page-link,
  .page-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: var(--fc-touch);
    min-height: var(--fc-touch);
  }

  /* Tri des colonnes (simple-datatables) : toute la cellule d'en-tete repond. */
  .datatable-table > thead > tr > th .datatable-sorter {
    min-height: var(--fc-touch);
    display: inline-flex;
    align-items: center;
  }

  .datatable-pagination a,
  .datatable-pagination button {
    min-width: var(--fc-touch);
    min-height: var(--fc-touch);
  }

  .fc-settings-nav-link {
    min-height: var(--fc-touch);
  }

  /* Commandes de l'en-tete. */
  .faccontrol-theme-option {
    width: var(--fc-touch);
    height: var(--fc-touch);
  }

  .gerreur-cloche,
  .faccontrol-install-btn,
  .back-to-top {
    width: var(--fc-touch);
    height: var(--fc-touch);
    min-width: var(--fc-touch);
  }

  /* Barre Retour / fil d'Ariane (g_retour). */
  .g-retour-barre {
    height: var(--fc-touch);
  }

  .g-retour-btn {
    width: var(--fc-touch);
    height: var(--fc-touch);
    font-size: 1.15rem;
  }

  .g-retour-chemin {
    height: 36px;
  }

  /* Petites commandes des champs de recherche (retirer une puce, effacer). */
  .faccontrol-search-chip-remove,
  .faccontrol-search-clear,
  .product-search-clear {
    min-width: var(--fc-touch);
    min-height: var(--fc-touch);
  }

  .swal2-styled {
    min-height: var(--fc-touch);
  }
}
