:root{
  --bg:#f5f7fb;
  --card:#ffffff;
  --text:#0f172a;
  --muted:#64748b;
  --border:#e2e8f0;
  --shadow:0 8px 24px rgba(15,23,42,.06);
  --radius:14px;
  --primary:#2563eb;
  --primary2:#1d4ed8;
  --danger:#dc2626;
  --ok:#16a34a;
  --warn:#f59e0b;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  --sans: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji","Segoe UI Emoji";
}
*{box-sizing:border-box}
html,body{height:100%}
body{
  margin:0;
  font-family:var(--sans);
  color:var(--text);
  background:linear-gradient(180deg, #ffffff 0%, var(--bg) 100%);
}
a{color:var(--primary); text-decoration:none}
a:hover{text-decoration:underline}
.container{max-width:98vw; margin:0 auto; padding:18px 16px 28px}
.topbar{
  position:sticky; top:0; z-index:50;
  backdrop-filter:saturate(180%) blur(8px);
  background:rgba(255,255,255,.75);
  border-bottom:1px solid var(--border);
}
.header{
  display:flex; gap:14px; align-items:center; justify-content:space-between;
  padding:14px 16px;
}
.brand{display:flex; flex-direction:column; gap:2px}
.h1{font-size:20px; font-weight:800; letter-spacing:.2px}
.sub{font-size:12px; color:var(--muted)}
.pills{display:flex; gap:10px; flex-wrap:wrap; align-items:center; justify-content:flex-end}
.pill{
  background:var(--card);
  border:1px solid var(--border);
  border-radius:12px;
  padding:10px 12px;
  min-width:120px;
  box-shadow:var(--shadow);
}
.pill .k{font-size:11px; color:var(--muted); margin-bottom:2px}
.pill .v{font-size:16px; font-weight:800}
.grid{
  display:grid;
  grid-template-columns:repeat(12,1fr);
  gap:12px;
}
.card{
  background:var(--card);
  border:1px solid var(--border);
  border-radius:var(--radius);
  box-shadow:var(--shadow);
}
.card.pad{padding:14px}
.hero{
  display:flex; flex-direction:column; gap:10px;
  padding:18px;
}
.hero h2{margin:0; font-size:18px}
.hero p{margin:0; color:var(--muted)}
.cards3{
  display:grid;
  grid-template-columns:repeat(3,minmax(0,1fr));
  gap:12px;
}
.homecard{
  padding:16px;
  display:flex; flex-direction:column; gap:10px;
}
.homecard .title{font-size:16px; font-weight:800}
.homecard .desc{color:var(--muted); font-size:13px; line-height:1.35}
.btn{
  display:inline-flex; align-items:center; justify-content:center;
  gap:8px;
  border-radius:12px;
  border:1px solid var(--border);
  padding:10px 12px;
  font-weight:700;
  cursor:pointer;
  background:#fff;
}
.btn.primary{
  background:var(--primary);
  color:#fff;
  border-color:transparent;
}
.btn.primary:hover{background:var(--primary2)}
.btn.ghost:hover{background:#f1f5f9}
.btn.danger{color:var(--danger)}
.toolbar{
  padding:12px;
  display:grid;
  /* 6 filtres + 3 actions (Réinitialiser / Exporter Excel / Vue compacte) */
  grid-template-columns: 1.6fr 1fr 1fr 1fr 1fr 1fr auto auto auto;
  gap:10px;
  align-items:end;
}
.field{display:flex; flex-direction:column; gap:6px}
.label{font-size:11px; color:var(--muted)}
.input, select{
  width:100%;
  border:1px solid var(--border);
  border-radius:12px;
  padding:10px 12px;
  background:#fff;
  outline:none;
}
.input:focus, select:focus{border-color:#93c5fd; box-shadow:0 0 0 4px rgba(59,130,246,.15)}
.tablewrap{
  overflow:auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-x pan-y;
  border-top:1px solid var(--border);
  border-bottom-left-radius:var(--radius);
  border-bottom-right-radius:var(--radius);
}
table{width:100%; border-collapse:separate; border-spacing:0}
thead th{
  position:sticky; top:0;
  background:#ffffff;
  z-index:2;
  border-bottom:1px solid var(--border);
  text-align:left;
  font-size:12px;
  padding:10px 12px;
  white-space:nowrap;
}
tbody td{
  font-size:13px;
  padding:10px 12px;
  border-bottom:1px solid #f1f5f9;
  white-space:nowrap;
}
tbody tr:nth-child(even){background:#fbfdff}
.num{text-align:right; font-variant-numeric: tabular-nums}
.badge{
  display:inline-flex; align-items:center;
  border:1px solid var(--border);
  border-radius:999px;
  padding:4px 8px;
  font-size:11px;
  color:var(--muted);
  background:#fff;
}
.small{font-size:12px; color:var(--muted)}
.footer{
  padding:14px 16px;
  color:var(--muted);
  font-size:12px;
  display:flex; justify-content:space-between; gap:10px; flex-wrap:wrap;
}
@media (max-width: 980px){
  .toolbar{
    grid-template-columns: 1fr 1fr;
    gap:10px;
  }
  .pills{justify-content:flex-start}
  .cards3{grid-template-columns:1fr}
}

/* Desktop large : on densifie un peu pour faire rentrer toutes les colonnes */
@media (min-width: 1200px){
  /* Desktop : on prend (presque) toute la largeur écran pour limiter le scroll horizontal */
  .container{ max-width: calc(100vw - 24px); }

  /* Sur desktop, on évite la barre de scroll : le tableau doit rentrer */
  .tablewrap{ overflow-x: visible; }

  /* Densification des cellules pour faire rentrer plus de colonnes */
  #table-data thead th,
  #table-data tbody td{
    padding: 6px 8px;
    font-size: 11px;
  }

  /* Autorise le retour à la ligne sur les 2 colonnes les plus larges */
  #table-data th:nth-child(5),
  #table-data td:nth-child(5),
  #table-data th:nth-child(6),
  #table-data td:nth-child(6){
    white-space: normal;
    max-width: 260px;
  }
}

/* Sécurité : si l'écran est petit, on conserve le scroll horizontal (mobile/tablette) */
@media (max-width: 1199px){
  .tablewrap{ overflow-x: auto; }
}

/* ===== Tri colonnes (clic sur en-têtes) ===== */
th.th-sort{
  cursor:pointer;
  user-select:none;
}
th.th-sort:hover{
  text-decoration: underline;
}
th.th-sort[data-sort="asc"]::after{
  content:" ▲";
  color: var(--muted);
  font-size: 11px;
}
th.th-sort[data-sort="desc"]::after{
  content:" ▼";
  color: var(--muted);
  font-size: 11px;
}

/* Masquer les hints "Lien : /xxx" sur la Home */
.homecard .small{ display:none; }

/* Masquer la ligne de description sous le titre (optionnel) */
.topbar .sub{ display:none; }

.pill.kpi-soc{ min-width: 220px; }
.pill.kpi-soc .small{ margin-top:6px; }

/* ===== KPI sociétés : même ligne que les KPI globaux ===== */
/* On garde le bandeau intact (header) : ne pas toucher à .header */

/* .pills d'origine : flex + wrap + align center + justify end */
.pills{
  display:flex;
  gap:10px;
  flex-wrap:wrap;
  align-items:center;
  justify-content:flex-end;
}

/* Slot KPI sociétés : on "efface" le conteneur du layout
   => les .pill injectées deviennent des enfants directs de .pills */
#kpi_societes{
  display:contents;
}

/* Largeur mini cohérente pour les cartes sociétés */
#kpi_societes .pill{
  min-width:260px;
}

/* En écran plus petit : on aligne à gauche (comme tu avais déjà) */
@media (max-width: 980px){
  .pills{ justify-content:flex-start; }
}

/* ===== KPI : tout sur UNE ligne (scroll horizontal si besoin) ===== */
.pills{
  display:flex;
  gap:10px;
  flex-wrap:nowrap;          /* <-- plus de passage à la ligne */
  align-items:center;
  justify-content:flex-start;
  overflow-x:auto;           /* <-- si ça dépasse, on scroll */
  -webkit-overflow-scrolling: touch;
  padding-bottom: 4px;       /* évite que la scrollbar “mange” */
}

.pill{
  flex: 0 0 auto;            /* <-- chaque carte garde sa largeur */
}

/* Les KPI sociétés un peu plus compacts pour rentrer plus souvent */
#kpi_societes .pill{
  min-width:220px;           /* au lieu de 260 */
}

.kpi-header{ align-items:flex-start; }

.kpi-right{
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.kpi-right .pills{
  display:flex;
  gap:10px;
  flex-wrap:nowrap;    /* reste sur 1 ligne */
  align-items:flex-start;
  justify-content:flex-end;
}

.kpi-right .pill{ flex:0 0 auto; }

#kpi_societes{ display:flex; gap:10px; }
#kpi_societes .pill{ min-width:220px; }



/* ===== Vue compacte (AS400) — tables denses + lignes totaux ===== */
body.is-compact #table-data td,
body.is-compact #table-data th{
  padding: 6px 8px;
  font-size: 12px;
}

#table-data tr.compact-group td{
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: .4px;
  border-top: 2px solid #ddd;
}

#table-data tr.compact-subgroup td{
  font-weight: 700;
  border-top: 1px solid #eee;
}

/* Sous-groupe interne (ex: RAL vs FACTURABLE/FACTURÉ) */
#table-data tr.compact-subsubgroup td{
  font-weight: 700;
  border-top: 1px solid #f1f1f1;
}

/* Petit retrait visuel pour les sous-groupes */
#table-data tr.compact-subsubgroup td:first-child{
  padding-left: 18px;
}

#table-data tr.row-total-rep td{
  font-weight: 700;
  border-top: 1px dashed #ddd;
}

#table-data tr.row-total-nature td{
  font-weight: 800;
  border-top: 2px solid #ccc;
}

#table-data tr.row-total-global td{
  font-weight: 900;
  border-top: 3px solid #bbb;
}

.toggle{
  display:flex;
  align-items:center;
  gap:8px;
  user-select:none;
}
.toggle input{ transform: translateY(1px); }


/* ===== Bouton Vue compacte (état actif) ===== */
#compactToggle[aria-pressed="true"]{
  background:#111;
  color:#fff;
  border-color:#111;
}

/* ===== Filtres repliables (mobile) ===== */
.mobile-only{ display:none; }

@media (max-width: 600px){
  .mobile-only{
    display:block;
    width:100%;
    margin-bottom:10px;
  }

  /*
    Mobile : on garde toujours visibles les actions (Accueil / Vue compacte / Reset / Export)
    et on replie uniquement les champs de filtres.
  */
  #filters{
    display:grid;
    grid-template-columns:1fr;
    gap:10px;
  }

  /* champs filtres cachés par défaut */
  #filters .filter-field{ display:none; }

  /* ouverture : on affiche tout */
  #filters.is-open .filter-field{ display:flex; }

  /* Export Excel : desktop prioritaire (mobile = optionnel) */
  #exportBtn{ display:none; }
}

@media (max-width: 600px){
  tbody tr{
    cursor:pointer;
  }
  tbody tr:active{
    background:#eef3ff;
  }
}

.to-top{
  position:fixed;
  bottom:16px;
  right:16px;
  z-index: 2000;
  border-radius:50%;
  padding:10px 14px;
  display:none;
}

@media (max-width:600px){
  .to-top{ display:block; }
}

tbody tr.is-active{
  outline: 2px solid rgba(0,0,0,.18);
  background: rgba(0,0,0,.04);
}

/* Cache KPIs sur mobile */
@media (max-width: 700px){
  .kpis, .kpi-bar, .kpi-grid, #kpis {
    display: none !important;
  }
}

/* Hide KPI topbar on mobile */
@media (max-width: 700px){
  .topbar{
    display: none !important;
  }
}
