/* ================= Couche mobile (< 761px) =================
   Reprend la structure desktop sur téléphone :
   - les tableaux deviennent des cartes (une ligne = une carte, intitulés issus
     des en-têtes via assets/mobile.js) au lieu d'un défilement horizontal ;
   - les graphiques ne réservent plus de hauteur vide ;
   - les infobulles et barres d'onglets restent dans l'écran.
   Chargée en dernier, aucune règle ne s'applique au-dessus de 760px. */

@media (max-width: 760px) {
  /* ---------- Tableaux → cartes ---------- */
  .table-wrap:has(> table.m-card-table) {
    overflow: visible;
    margin-inline: 0;
    padding: 0;
    border: 0;
    background: none;
    box-shadow: none;
  }

  table.m-card-table,
  table.m-card-table tbody {
    display: block;
    width: 100%;
    min-width: 0 !important;
  }

  table.m-card-table thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }

  table.m-card-table tbody tr {
    display: block;
    margin: 0 0 10px;
    padding: 12px 14px 4px;
    background: var(--panel-inset, var(--panel));
    border: 1px solid var(--line);
    border-radius: var(--radius, 14px);
  }

  table.m-card-table tbody tr:last-child { margin-bottom: 0; }

  /* Chaque cellule : intitulé à gauche (pseudo-élément), valeur à droite. */
  table.m-card-table tbody td[data-label] {
    display: block;
    position: relative;
    min-height: 34px;
    padding: 8px 0 8px 46%;
    border-top: 1px solid var(--line-soft, var(--line));
    color: var(--ink);
    font-size: 0.875rem;
    text-align: right !important;
    white-space: normal;
    overflow-wrap: anywhere;
  }

  table.m-card-table tbody td[data-label]::before {
    content: attr(data-label);
    position: absolute;
    left: 0;
    top: 50%;
    max-width: 44%;
    transform: translateY(-50%);
    color: var(--faint);
    font-size: 0.75rem;
    font-weight: 600;
    line-height: 1.25;
    text-align: left;
  }

  /* Première cellule = titre de la carte (nom du modèle, de la personne…). */
  table.m-card-table tbody td[data-label]:first-child {
    min-height: 0;
    padding: 0 0 10px;
    border-top: 0;
    font-size: 0.9375rem;
    font-weight: 680;
    text-align: left !important;
  }

  table.m-card-table tbody td[data-label]:first-child::before { display: none; }

  /* Annule la colonne collante du tableau desktop. */
  table.m-card-table tbody td,
  table.m-card-table tbody th {
    position: relative;
    left: auto;
    background: transparent !important;
    box-shadow: none !important;
  }

  /* Contenu interne des cellules : alignement à droite propre. */
  table.m-card-table tbody td[data-label]:not(:first-child) > * {
    margin-left: auto;
  }

  table.m-card-table tbody td[data-label]:not(:first-child) :is(.badge, .pill, .panel-badge, .chip, .status, [class*="badge"], [class*="pill"]) {
    display: inline-flex;
  }

  table.m-card-table tbody td input,
  table.m-card-table tbody td select {
    max-width: 100%;
  }

  /* Cellules sans intitulé (actions, icônes) : alignées en pied de carte. */
  table.m-card-table tbody td:not([data-label]) {
    display: flex;
    justify-content: flex-end;
    gap: 6px;
    padding-top: 8px;
  }

  table.m-card-table tbody td:not([data-label]):empty,
  table.m-card-table tbody td[data-label]:empty { display: none; }

  /* Ligne d'état vide (« Aucun versement… ») */
  table.m-card-table tbody tr.m-empty-row {
    display: block;
    padding: 18px 14px;
    text-align: center;
    color: var(--muted);
    border-style: dashed;
  }

  table.m-card-table tbody tr.m-empty-row td {
    display: block;
    padding: 0;
    border: 0;
    text-align: center !important;
  }

  table.m-card-table tbody tr:hover { background: var(--panel-inset, var(--panel)); }

  /* ---------- Graphiques : pas de hauteur vide réservée ---------- */
  .chart { min-height: 0; }
  .chart:empty { min-height: 120px; }

  /* ---------- Infobulle de synchronisation dans l'écran ---------- */
  .sync-status-tooltip {
    right: 0;
    left: auto;
    width: min(205px, calc(100vw - 32px));
    transform-origin: right top;
  }

  .top-actions .sync-status-tooltip {
    left: auto;
    right: 0;
    transform-origin: right top;
  }

  .top-actions .sync-status-tooltip::after {
    left: auto;
    right: 14px;
  }

  /* ---------- Barres d'onglets : défilement net, sans coupure brute ---------- */
  .segmented,
  .model-tabs,
  .view-tabs,
  [role="tablist"] {
    max-width: 100%;
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x proximity;
  }

  .segmented::-webkit-scrollbar,
  .model-tabs::-webkit-scrollbar,
  .view-tabs::-webkit-scrollbar,
  [role="tablist"]::-webkit-scrollbar { display: none; }

  [role="tablist"] > * {
    flex-shrink: 0;
    scroll-snap-align: start;
  }

  /* ---------- Formule de management : passe à la ligne ---------- */
  .management-formula {
    flex-wrap: wrap;
    overflow-x: visible;
    row-gap: 8px;
  }

  /* ---------- Zones tactiles ---------- */
  .icon-btn,
  .icon-mini {
    min-width: 36px;
    min-height: 36px;
  }
}

/* ================= Passe 2 : finitions mobile ================= */
@media (max-width: 760px) {
  /* ---------- Header : bouton Période sur une ligne ---------- */
  #rangeCustomBtn {
    min-width: 0;
    max-width: 100%;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  .range-custom-wrap { min-width: 0; flex: 1 1 auto; }

  /* ---------- Ligne de contexte : une seule ligne compacte ----------
     L'état de synchro est déjà visible dans le badge du header. */
  .workspace-context { gap: 0; padding-block: 8px 2px; }
  .workspace-scope { flex-wrap: nowrap; gap: 6px; }
  #rangeSummary {
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
  .workspace-scope .pill { flex: none; }
  #storageStatus { display: none; }

  /* ---------- Sous-onglets : fondu en bord pour signaler le défilement ---------- */
  .model-context-actions,
  .context-nav {
    overflow-x: auto;
    scrollbar-width: none;
    scroll-snap-type: x proximity;
    -webkit-mask-image: linear-gradient(90deg, #000 calc(100% - 28px), transparent);
    mask-image: linear-gradient(90deg, #000 calc(100% - 28px), transparent);
  }
  .model-context-actions.m-scrolled-end,
  .context-nav.m-scrolled-end,
  .model-context-actions.m-no-overflow,
  .context-nav.m-no-overflow {
    -webkit-mask-image: none;
    mask-image: none;
  }
  .model-context-actions::-webkit-scrollbar,
  .context-nav::-webkit-scrollbar { display: none; }
  .model-context-actions > *,
  .context-nav > * { flex-shrink: 0; scroll-snap-align: start; }

  /* ---------- Tâches : panneau Catégories plus collant (chevauchement) ---------- */
  .task-layout > .task-categories,
  .task-categories {
    position: static !important;
    top: auto !important;
  }

  /* Barre de filtres en grille lisible */
  .task-toolbar-main {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto;
    gap: 8px;
    align-items: center;
  }
  .task-toolbar-main > .task-search { grid-column: 1 / 3; grid-row: 1; min-width: 0; }
  .task-toolbar-main > #newTaskToggle { grid-column: 3; grid-row: 1; }
  .task-toolbar-main > #taskCategoryFilter { grid-column: 1; grid-row: 2; min-width: 0; width: 100%; max-width: none; }
  .task-toolbar-main > .task-sort-control { grid-column: 2 / 4; grid-row: 2; min-width: 0; width: 100%; }
  .task-toolbar-main > .task-sort-control select { width: 100%; min-width: 0; max-width: none; }
  .task-toolbar-main > .task-assignee-filter,
  .task-toolbar-main > .task-priority-filter {
    grid-column: 1 / -1;
    display: flex;
    justify-self: stretch;
    width: 100%;
  }
  .task-toolbar-main > .task-assignee-filter { grid-row: 3; }
  .task-toolbar-main > .task-priority-filter { grid-row: 4; }
  .task-toolbar-main > .task-assignee-filter > button,
  .task-toolbar-main > .task-priority-filter > button {
    flex: 1 1 0;
    display: grid;
    place-items: center;
    min-height: 36px;
  }
  .task-toolbar-main > #taskCategoryFilter,
  .task-toolbar-main > .task-sort-control select {
    font-size: 0.8125rem;
    padding-inline: 10px 26px;
    text-overflow: ellipsis;
  }

  /* Lignes de tâche : actions à droite, pas sur une ligne à part */
  .task-row { grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; min-height: 0; }
  .task-row .task-row-extras {
    grid-column: 3;
    grid-row: 1 / span 2;
    align-self: center;
    justify-content: flex-end;
  }
  .task-row .task-row-arrow { display: none; }
  /* La priorité est déjà donnée par l'en-tête du groupe. */
  .task-group-heading ~ * .task-priority,
  .task-list .task-row .task-priority { display: none; }

  .task-group-heading {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    justify-content: flex-start;
    gap: 4px 8px;
    text-align: left;
  }
  .task-group-heading small { flex-basis: 100%; }

  /* ---------- Acquisition : l'étiquette « Cible » du repère chevauchait le nom ---------- */
  .bullet-marker::after { display: none; }
}

/* ================= Passe 3 : KPI d'accueil et boutons compacts ================= */
@media (max-width: 760px) {
  /* KPI secondaires de l'accueil : une ligne par indicateur au lieu d'un 2 + 1 */
  #view-overview .overview-kpi-strip { grid-template-columns: minmax(0, 1fr) !important; }
  #view-overview .overview-kpi-strip .overview-stats {
    display: flex !important;
    flex-direction: column;
    grid-column: 1 / -1;
    width: 100%;
  }
  #view-overview .overview-kpi-strip .overview-stats .hero-stat {
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 2px 12px;
    min-height: 0 !important;
    height: auto !important;
    padding: 13px 20px;
    text-align: left;
    border-left: 0 !important;
    border-right: 0 !important;
    border-bottom: 0 !important;
    border-top: 1px solid var(--line) !important;
    grid-column: auto !important;
  }
  #view-overview .overview-kpi-strip .overview-stats .hero-stat > span {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    flex: 1 1 auto;
    min-width: 0;
    margin: 0;
    font-size: 0.8125rem;
  }
  #view-overview .overview-kpi-strip .overview-stats .hero-stat > strong {
    flex: none;
    margin: 0;
    font-size: 1.125rem;
    text-align: right;
  }
  #view-overview .overview-kpi-strip .overview-stats .hero-stat > small {
    flex-basis: 100%;
    margin: 0;
  }

  /* Bouton « ajouter un modèle » : carré compact comme sur desktop */
  .panel-head > .model-add-button.compact-create {
    flex: 0 0 40px;
    width: 40px !important;
    min-width: 40px;
    height: 40px;
    min-height: 40px;
    align-self: center;
    justify-self: end;
    margin-left: auto;
    padding: 0;
  }
}
