/* =========================================================================
   CP Tables — Feuille de styles
   Version : 3.0.0
   Dépend de : table-shortcode.php (v3.0.0) et tableau.js (v3.0.0)

   Sommaire
   1. Variables
   2. Conteneur & défilement horizontal
   3. Tableau — base
   4. Onglets
   5. Responsive — mode "stack" (cartes empilées)
   6. Responsive — mode "scroll"
   7. Surcharges site (Divi / Mecaspin) — legacy conservé
   ========================================================================= */

/* =========================================================================
   1. Variables
   Redéfinissables par site sans toucher au reste du fichier.
   ========================================================================= */

.cp-table-wrap,
.cp-tabs {
  --cp-t-bg: #fff;
  --cp-t-bg-alt: var(--msp-fp-bg-grey, #f7f7f7);
  --cp-t-border: #f2f2f2;
  --cp-t-text: #222;
  --cp-t-text-muted: #6b6b6b;
  --cp-t-accent: var(--main-color, #c8102e);
  --cp-t-radius: 6px;
  --cp-t-cell-y: 0.65rem;
  --cp-t-cell-x: 1rem;
  --cp-t-font: inherit;
  --cp-t-shadow-size: 14px;
  --cp-t-focus: 2px solid var(--cp-t-accent);
}

/* =========================================================================
   2. Conteneur & défilement horizontal
   Le wrapper est TOUJOURS scrollable : c'est le filet de sécurité qui
   empêche un tableau large de casser la mise en page (cause du débordement
   constaté en mobile avant la v3).
   ========================================================================= */

.cp-table-wrap {
  position: relative;
  overflow-x: auto;
  overflow-y: hidden;
  max-width: 100%;
  /* Évite que le scroll horizontal entraîne un scroll vertical parasite. */
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
}

/* Ombres de défilement en CSS pur (technique background-attachment).
   Elles n'apparaissent que du côté où il reste du contenu à faire défiler. */
.cp-table-wrap--scroll {
  background-image:
    linear-gradient(to right, var(--cp-t-bg) 40%, rgba(255, 255, 255, 0)),
    linear-gradient(to left,  var(--cp-t-bg) 40%, rgba(255, 255, 255, 0)),
    radial-gradient(farthest-side at 0 50%,   rgba(0, 0, 0, 0.16), rgba(0, 0, 0, 0)),
    radial-gradient(farthest-side at 100% 50%, rgba(0, 0, 0, 0.16), rgba(0, 0, 0, 0));
  background-position: 0 0, 100% 0, 0 0, 100% 0;
  background-size: 40px 100%, 40px 100%, var(--cp-t-shadow-size) 100%, var(--cp-t-shadow-size) 100%;
  background-repeat: no-repeat;
  background-attachment: local, local, scroll, scroll;
}

/* Le JS n'ajoute tabindex que si le contenu déborde réellement :
   pas d'arrêt de tabulation parasite quand le tableau tient dans la page. */
.cp-table-wrap[tabindex]:focus-visible {
  outline: var(--cp-t-focus);
  outline-offset: 2px;
}

/* =========================================================================
   3. Tableau — base
   ========================================================================= */

.cp-t {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--cp-t-font);
  color: var(--cp-t-text);
}

.cp-t caption {
  caption-side: top;
  padding-bottom: 0.75rem;
  font-weight: 700;
  text-align: left;
  color: var(--cp-t-text);
}

.cp-t th,
.cp-t td {
  padding: var(--cp-t-cell-y) var(--cp-t-cell-x);
  text-align: left;
  vertical-align: middle;
}

.cp-t thead th {
  font-weight: 800;
  color: var(--cp-t-text);
}

.cp-t tbody tr + tr td {
  border-top: 1.5px solid var(--cp-t-border);
}

.cp-t tbody td {
  color: var(--cp-t-text-muted);
}

.cp-no-data {
  margin: 0;
  padding: 1rem;
  font-style: italic;
  color: var(--cp-t-text-muted);
}

/* =========================================================================
   4. Onglets
   Le PHP génère des <button role="tab">. Les sélecteurs couvrent aussi
   l'ancien balisage <li data-tab> écrit à la main dans des modules Code.
   ========================================================================= */

.cp-tabs-nav {
  display: flex;
  flex-wrap: nowrap;
  gap: clamp(4px, 1vw, 24px);
  justify-content: space-between;
  list-style: none;
  margin: 0 auto 16px;
  padding: 0;
}

.cp-tabs-nav,
.cp-tabs-panels,
.cp-tabs-content {
  max-width: 750px;
  margin-left: auto;
  margin-right: auto;
}

.cp-tabs-nav .cp-tab-button,
.cp-tabs-nav li[data-tab] {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 34px;
  padding: 0 18px;
  border: 0;
  border-bottom: 4px solid var(--cp-t-border);
  border-radius: 0;
  background: var(--cp-t-bg);
  color: var(--cp-t-text);
  font-family: inherit;
  font-size: inherit;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  user-select: none;
  appearance: none;
}

.cp-tabs-nav .cp-tab-button.cp-tab-active,
.cp-tabs-nav li[data-tab].active {
  border: none;
  border-radius: 999px;
  background: var(--cp-t-accent);
  color: #fff;
}

.cp-tabs-nav .cp-tab-button:focus-visible,
.cp-tabs-nav li[data-tab]:focus-visible {
  outline: var(--cp-t-focus);
  outline-offset: 3px;
}

/* Neutralise les pseudo-éléments décoratifs hérités du thème. */
.cp-tabs-nav .cp-tab-button::before,
.cp-tabs-nav li[data-tab]::before {
  display: none !important;
}

.cp-tabs-nav .cp-tab-sign {
  display: inline-block;
  width: 14px;
  margin-left: 10px;
  text-align: center;
}

.cp-tabs-nav .cp-tab-button:not(.cp-tab-active) .cp-tab-sign::before,
.cp-tabs-nav li[data-tab]:not(.active) .cp-tab-sign::before {
  content: "+";
  color: var(--cp-t-accent);
}

.cp-tabs-nav .cp-tab-button.cp-tab-active .cp-tab-sign::before,
.cp-tabs-nav li[data-tab].active .cp-tab-sign::before {
  content: "–";
  color: #fff;
}

/* L'attribut hidden pilote la visibilité des panneaux : il fonctionne
   sans JavaScript. !important neutralise les display imposés par Divi. */
.cp-tab-panel[hidden] {
  display: none !important;
}

.cp-tab-panel:focus-visible {
  outline: var(--cp-t-focus);
  outline-offset: 4px;
}

@media (max-width: 500px) {
  .cp-tabs-nav {
    flex-direction: column;
  }
}

/* =========================================================================
   5. Responsive — mode "stack" (défaut)
   Chaque ligne devient une carte, chaque cellule affiche son en-tête via
   data-label. Le PHP réinjecte les rôles ARIA (role="table"/"row"/"cell")
   car changer le display casse la sémantique native du tableau.
   ========================================================================= */

@media (max-width: 767px) {

  .cp-t--stack {
    display: block;
    border-collapse: separate;
  }

  /* L'en-tête est retiré : son contenu est repris par data-label sur
     chaque cellule, ce qui évite une double lecture par les lecteurs d'écran. */
  .cp-t--stack thead {
    display: none;
  }

  .cp-t--stack tbody,
  .cp-t--stack caption {
    display: block;
  }

  .cp-t--stack tbody tr {
    display: block;
    margin-bottom: 0.85rem;
    border: 1px solid var(--cp-t-border);
    border-radius: var(--cp-t-radius);
    background: var(--cp-t-bg);
    overflow: hidden;
  }

  .cp-t--stack tbody tr:last-child {
    margin-bottom: 0;
  }

  .cp-t--stack tbody td {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 1rem;
    width: auto !important;
    padding: 0.6rem 0.9rem;
    border-top: 0;
    text-align: right;
  }

  .cp-t--stack tbody tr td + td {
    border-top: 1px solid var(--cp-t-border);
  }

  /* Libellé de colonne reconstitué à gauche de la valeur. */
  .cp-t--stack tbody td[data-label]::before {
    content: attr(data-label);
    flex: 0 1 auto;
    max-width: 55%;
    color: var(--cp-t-text);
    font-weight: 700;
    text-align: left;
  }

  /* Cellule sans libellé (ligne fusionnée, sous-titre…) : pleine largeur. */
  .cp-t--stack tbody td:not([data-label]) {
    justify-content: flex-start;
    text-align: left;
    background: var(--cp-t-bg-alt);
    font-weight: 700;
  }

  /* Le wrapper ne doit plus rogner ni scroller en mode carte. */
  .cp-table-wrap--stack {
    overflow: visible;
    background-image: none;
  }
}

/* =========================================================================
   6. Responsive — mode "scroll"
   Le tableau garde sa structure et défile. Utile quand les cellules sont
   fusionnées verticalement (data-merge-v) : l'empilement n'y a pas de sens.
   ========================================================================= */

.cp-t--scroll {
  min-width: max-content;
}

@media (max-width: 767px) {
  .cp-t--scroll th,
  .cp-t--scroll td {
    padding: 0.5rem 0.75rem;
    font-size: 0.9rem;
    white-space: nowrap;
  }
}

/* Impression : jamais d'empilement ni de scroll. */
@media print {
  .cp-table-wrap {
    overflow: visible;
    background-image: none;
  }
  .cp-t--stack {
    display: table;
  }
  .cp-t--stack thead {
    display: table-header-group;
  }
  .cp-t--stack tbody {
    display: table-row-group;
  }
  .cp-t--stack tbody tr {
    display: table-row;
  }
  .cp-t--stack tbody td {
    display: table-cell;
  }
  .cp-t--stack tbody td[data-label]::before {
    content: none;
  }
  .cp-tab-panel[hidden] {
    display: block !important;
  }
}

/* Respecte la préférence système de réduction des animations. */
@media (prefers-reduced-motion: reduce) {
  .cp-table-wrap {
    scroll-behavior: auto;
  }
}

/* =========================================================================
   7. Surcharges site (Divi / Mecaspin) — legacy conservé
   Les sélecteurs de la v2 utilisaient "> div > table". Le tableau étant
   désormais enveloppé dans .cp-table-wrap, ils sont passés en combinateur
   descendant : ils couvrent ainsi l'ancien ET le nouveau balisage.
   ========================================================================= */

#content-area .tab-fp .et_pb_code_inner table.custom-cp-table,
#content-area .tab-fp .et_pb_code_inner table.custom-cp-table tr td {
  border-width: 0;
  text-align: center;
  padding: 0.55em;
}

#content-area .tab-fp .et_pb_code_inner table.custom-cp-table tr td:first-of-type {
  font-weight: 600;
}

.tab-fp thead th,
.entry-content tr th {
  font-weight: 800;
  color: #222;
  font-size: 1.1em;
}

.tab-fp thead tr:nth-child(odd) {
  background-color: #fff;
}

.tab-fp tr:nth-child(even),
.tab-fp table tbody td[rowspan],
.tab-fp table tbody td[colspan] {
  background-color: #fff;
}

/* Variante "complexe" : fond de section et zébrage inversés.
   Sur le module Code, remplacer la classe "classique" par "complexe". */
body.et-db #et-boc .et-l .et_pb_fullwidth_section .et_pb_section:has(.complexe) {
  background-color: var(--msp-white);
}

.tab-fp.complexe tr:nth-child(odd),
.tab-fp.complexe table tbody td[rowspan],
.tab-fp.complexe table tbody td[colspan] {
  background-color: var(--msp-fp-bg-grey);
}

:is(.tab-fp.classique, .tab-fp.complexe) thead th {
  text-align: start;
}

.single-machine .tab-fp.classique thead th:nth-child(2) {
  color: var(--msp-red);
}

:is(.tab-fp.classique, .tab-fp.complexe) tr {
  height: 2.5rem;
}

:is(.tab-fp.classique, .tab-fp.complexe) tr :is(td, th) {
  padding-left: 5rem;
}

.tab-fp.complexe .custom-cp-complex-table {
  width: 100%;
}

.tab-fp.complexe .custom-cp-complex-table tr td:nth-child(odd) {
  font-weight: bold;
}

@media (max-width: 980px) {
  :is(.tab-fp.classique, .tab-fp.complexe) tr :is(td, th) {
    padding-left: 2rem;
  }

  #page-container .tab-fp.classique tr :is(td, th) {
    padding: 0.5rem;
    padding-left: 2rem;
    font-size: 0.9rem;
    line-height: normal;
  }
}

/* En mode carte, les paddings latéraux hérités ci-dessus n'ont plus de sens :
   ils écraseraient la lisibilité sur mobile. */
@media (max-width: 767px) {
  #page-container .cp-t--stack tbody td,
  :is(.tab-fp.classique, .tab-fp.complexe) .cp-t--stack tbody td {
    padding: 0.6rem 0.9rem;
  }

  :is(.tab-fp.classique, .tab-fp.complexe) .cp-t--stack tbody tr {
    height: auto;
  }

  /* Le zébrage par ligne est remplacé par la carte elle-même. */
  .tab-fp .cp-t--stack tbody tr:nth-child(even),
  .tab-fp.complexe .cp-t--stack tbody tr:nth-child(odd) {
    background-color: var(--cp-t-bg);
  }
}