/* Graph-track — tokens issus de la palette data-viz validée.
   Les valeurs sombres sont déclarées sous les deux portées : la media query
   couvre le réglage système, la portée [data-theme] couvre un sélecteur de
   thème (qui doit gagner dans les deux sens). */

:root {
  color-scheme: light;

  --page:           #f9f9f9;
  --surface-1:      #ffffff;
  --text-primary:   #191919;
  --text-secondary: #666666;
  --grid:           #e7e7e7;
  --axis:           #c2c2c2;
  --border:         rgba(11, 11, 11, 0.10);
  --hover:          rgba(11, 11, 11, 0.04);
  --danger:         #dc3c3d;

  --series-1: #257de0;
  --series-2: #f47230;
  --series-3: #00b894;
  --series-4: #f2a600;
  --series-5: #ee7fac;
  --series-6: #008a17;
  --series-7: #6a4ce2;
  --series-8: #ec4647;

  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;

  /* Largeur fixe de la colonne des labels : l offset de la colonne Base figee
     juste a sa droite en depend. */
  --label-w: 248px;
  --base-w: 104px;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;

    --page:           #0b0b0b;
    --surface-1:      #191919;
    --text-primary:   #efefef;
    --text-secondary: #9c9c9c;
    --grid:           #303030;
    --axis:           #474747;
    --border:         rgba(255, 255, 255, 0.10);
    --hover:          rgba(255, 255, 255, 0.06);
    --danger:         #eb6263;

    --series-1: #338ced;
    --series-2: #e76624;
    --series-3: #00a888;
    --series-4: #d28e05;
    --series-5: #e24d8b;
    --series-6: #0aac23;
    --series-7: #9887ef;
    --series-8: #eb6263;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;

  --page:           #0b0b0b;
  --surface-1:      #191919;
  --text-primary:   #efefef;
  --text-secondary: #9c9c9c;
  --grid:           #303030;
  --axis:           #474747;
  --border:         rgba(255, 255, 255, 0.10);
  --hover:          rgba(255, 255, 255, 0.06);
  --danger:         #eb6263;

  --series-1: #338ced;
  --series-2: #e76624;
  --series-3: #00a888;
  --series-4: #d28e05;
  --series-5: #e24d8b;
  --series-6: #0aac23;
  --series-7: #9887ef;
  --series-8: #eb6263;
}

/* Sur un petit ecran, deux colonnes figees larges ne laisseraient presque rien
   aux jours. */
@media (max-width: 640px) {
  :root {
    --label-w: 190px;
    --base-w: 84px;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 0 16px 48px;
  background: var(--page);
  color: var(--text-primary);
  font: 14px/1.5 var(--font);
}

/* ---------- Barre du haut ---------- */

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  padding: 20px 0 14px;
}

.topbar h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.btn {
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--surface-1);
  color: var(--text-primary);
  font: inherit;
  cursor: pointer;
}

.btn:hover { background: var(--hover); }

.btn:focus-visible,
.grid-wrap input:focus-visible,
.icon-btn:focus-visible {
  outline: 2px solid var(--series-1);
  outline-offset: 1px;
}

/* Bascule du type de graphique */

.seg {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 7px;
  overflow: hidden;
}

.seg-btn {
  padding: 7px 12px;
  border: 0;
  background: var(--surface-1);
  color: var(--text-secondary);
  font: inherit;
  cursor: pointer;
}

.seg-btn + .seg-btn { border-left: 1px solid var(--border); }

.seg-btn:hover { background: var(--hover); color: var(--text-primary); }

/* Selection marquee par un trait accentue : le texte garde l encre du systeme,
   donc aucun probleme de contraste sur une teinte de serie. */
.seg-btn.is-on {
  background: var(--hover);
  color: var(--text-primary);
  box-shadow: inset 0 -2px 0 0 var(--series-1);
}

.seg-btn:focus-visible {
  outline: 2px solid var(--series-1);
  outline-offset: -2px;
}

.status {
  min-width: 11ch;
  color: var(--text-secondary);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.status.warn { color: var(--danger); }

/* ---------- Mise en page ---------- */

main {
  display: grid;
  gap: 16px;
  min-width: 0;
}

/* min-width: 0 est indispensable : un element de grille vaut min-width: auto par
   defaut, donc un contenu large (la table, le canvas) pourrait elargir la piste
   au-dela de la fenetre. Les deux cartes partagent cette piste : sans cela, une
   table large emporte aussi le graphique hors de l ecran. overflow: hidden ferme
   la porte a tout depassement residuel. */
/* ---------- Barre de periode ---------- */

/* Une seule rangee, au-dessus du contenu qu elle cadre : tableau, graphique et
   totaux lisent la meme tranche, donc les nombres s accordent toujours. */
.filters {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.range-manual {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

/* Comme pour .empty-hint : le display ci-dessus est plus specifique que la regle
   [hidden] du navigateur, il faut donc le neutraliser explicitement. */
.range-manual[hidden] { display: none; }

.range-field {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text-secondary);
  font-size: 12px;
}

.range-field input[type="date"] {
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-1);
  color: var(--text-primary);
  font: inherit;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.range-field input[type="date"]:focus-visible {
  outline: 2px solid var(--series-1);
  outline-offset: 1px;
}

.range-info {
  color: var(--text-secondary);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

/* ---------- Cartes ---------- */

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  min-width: 0;
  overflow: hidden;
}

.chart-wrap {
  position: relative;
  height: 45vh;
  min-height: 280px;
  min-width: 0;
}

/* Sans max-width, la largeur en pixels que Chart.js pose en style inline
   empeche le conteneur de rapetisser quand la fenetre se reduit. */
#chart {
  display: block;
  max-width: 100%;
}

.empty-hint {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  margin: 0;
  color: var(--text-secondary);
  text-align: center;
  pointer-events: none;
}

/* Indispensable : la declaration display ci-dessus est plus specifique que la
   regle [hidden] du navigateur, donc sans ceci l attribut hidden reste sans
   effet et le message ne disparait jamais. */
.empty-hint[hidden] { display: none; }

.msg {
  margin: 0 0 12px;
  padding: 9px 11px;
  border: 1px solid var(--danger);
  border-radius: 7px;
  color: var(--text-primary);
  background: var(--hover);
}

.hint {
  margin: 12px 2px 0;
  color: var(--text-secondary);
  font-size: 12px;
}

.hint .sep { opacity: 0.5; }
.hint strong { color: var(--text-primary); }

.hint kbd {
  padding: 0 4px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--page);
  font: inherit;
  font-size: 11px;
}

.handle-key { letter-spacing: -1px; }

/* ---------- Tableau ---------- */

.grid-wrap {
  overflow: auto;
  max-width: 100%;
  max-height: 58vh;
  border: 1px solid var(--border);
  border-radius: 8px;
  /* le defilement horizontal du tableau ne declenche pas le retour arriere du
     navigateur sur un pave tactile */
  overscroll-behavior-x: contain;
}

table {
  border-collapse: separate;
  border-spacing: 0;
  width: max-content;
  min-width: 100%;
}

th, td {
  margin: 0;
  padding: 0;
  border-right: 1px solid var(--grid);
  border-bottom: 1px solid var(--grid);
  background: var(--surface-1);
  text-align: left;
  font-weight: 400;
  vertical-align: middle;
}

thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  color: var(--text-secondary);
  font-size: 12px;
}

/* Colonne des labels figée pour rester lisible en défilement horizontal */
tbody th, tfoot th, thead th.corner {
  position: sticky;
  left: 0;
  z-index: 1;
}

thead th.corner {
  z-index: 3;
  height: 36px;
  padding: 0 10px;
}

/* ---------- Colonne Base (figee avec les labels) ---------- */

thead th.corner,
tbody th.rowhead,
tfoot th { width: var(--label-w); }

th.base-head,
td.base {
  position: sticky;
  left: var(--label-w);
  width: var(--base-w);
  z-index: 1;
  /* frontiere entre ce qui decrit le label et les jours */
  border-right: 1px solid var(--axis);
}

thead th.base-head {
  z-index: 3;
  padding: 0 8px;
  color: var(--text-secondary);
}

/* La cellule Base de la ligne des totaux est vide par construction, mais doit
   porter le fond de cette ligne. */
tfoot tr.total-row td.base {
  z-index: 3;
  background: var(--page);
}

/* Seule la ligne des totaux reste collee en bas : deux lignes de pied fixees au
   meme bord se chevaucheraient. */
tfoot tr.total-row th,
tfoot tr.total-row td {
  position: sticky;
  bottom: 0;
  z-index: 2;
}

tfoot tr.total-row th { left: 0; z-index: 3; }

tfoot tr:last-child th, tfoot tr:last-child td { border-bottom: 0; }
th:last-child, td:last-child { border-right: 0; }

/* La ligne « + » separe desormais les lignes ouvertes des lignes verrouillees :
   le fond de la page la fait lire comme une frontiere, pas comme une donnee. */
tr.add-row-tr th,
tr.add-row-tr td { background: var(--page); }

/* ---------- Totaux (calcules, non modifiables) ---------- */

.total-head {
  color: var(--text-secondary);
  font-weight: 600;
  padding: 0 10px;
  background: var(--page);
  white-space: nowrap;
}

td.total {
  padding: 7px 8px;
  background: var(--page);
  color: var(--text-primary);
  text-align: right;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

tfoot tr.total-row th,
tfoot tr.total-row td { border-top: 1px solid var(--axis); }

/* Le flex vit dans un div interne : pose sur le <td> lui-meme, display: flex le
   ferait sortir de la mise en page du tableau. */
.total-cell {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
}

.lock-box {
  flex: none;
  width: 15px;
  height: 15px;
  margin: 0;
  cursor: pointer;
  accent-color: var(--series-1);
}

/* ---------- Ligne verrouillee ---------- */

/* Les nombres restent lisibles et selectionnables, mais visiblement inertes. */
tr.locked td.val input,
tr.locked td.base input {
  color: var(--text-secondary);
  cursor: default;
}

tr.locked td.val input,
tr.locked td.base input,
tr.locked td.val input:hover,
tr.locked td.base input:hover,
tr.locked td.val input:focus,
tr.locked td.base input:focus {
  background: var(--page);
}

tr.locked th.rowhead { color: var(--text-secondary); }

/* Cellules d en-tete de colonne (dates) */

.col-cell, .row-cell {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 3px 4px 3px 6px;
}

.col-cell input[type="date"] {
  width: 124px;
  padding: 4px 2px;
  border: 1px solid transparent;
  border-radius: 5px;
  background: transparent;
  color: var(--text-primary);
  font: inherit;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

/* Cellules d en-tete de ligne (labels) */

.row-cell { min-width: 0; }   /* la largeur vient de la colonne, cf. --label-w */

.row-cell input[type="text"] {
  flex: 1;
  min-width: 0;
  padding: 5px 6px;
  border: 1px solid transparent;
  border-radius: 5px;
  background: transparent;
  color: var(--text-primary);
  font: inherit;
}

.handle {
  flex: none;
  padding: 0 3px;
  color: var(--text-secondary);
  cursor: grab;
  user-select: none;
  letter-spacing: -1px;
}

.handle:active { cursor: grabbing; }

/* Cle de serie : un court trait de la couleur de la courbe (une ligne se
   repere par un trait, pas par un pave). */
.swatch {
  flex: none;
  width: 16px;
  height: 2px;
  border-radius: 1px;
  background: var(--c-series);
}

.swatch.d2 {
  background: repeating-linear-gradient(to right, var(--c-series) 0 6px, transparent 6px 10px);
}

.swatch.d3 {
  background: repeating-linear-gradient(to right, var(--c-series) 0 2px, transparent 2px 5px);
}

/* La pastille est aussi le selecteur de couleur : un input color transparent la
   recouvre, ce qui garde le rendu du trait tout en offrant une cible de 20 px. */
.swatch-pick {
  position: relative;
  flex: none;
  display: grid;
  place-items: center;
  width: 20px;
  height: 20px;
  border-radius: 5px;
}

.swatch-pick:hover { background: var(--hover); }

.swatch-pick:focus-within {
  outline: 2px solid var(--series-1);
  outline-offset: 0;
}

.swatch-field {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  border: 0;
  opacity: 0;
  cursor: pointer;
  /* Safari ignore opacity sur ce controle : on le reduit a la zone utile. */
  -webkit-appearance: none;
  appearance: none;
  background: transparent;
}

/* Cellules de valeurs */

/* La largeur minimale appartient a la cellule : sur l input, la taille
   intrinseque du champ l emportait et donnait des colonnes deux fois trop
   larges (d ou l attribut size cote HTML). */
td.val { min-width: 92px; }

td.val input,
td.base input {
  width: 100%;
  min-width: 0;
  padding: 7px 8px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-primary);
  font: inherit;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.grid-wrap input:hover { background: var(--hover); }

.grid-wrap input:focus {
  outline: none;
  border-color: var(--series-1);
  background: var(--surface-1);
}

td.val input.invalid {
  border-color: var(--danger);
  color: var(--danger);
}

/* Boutons + et x */

.icon-btn {
  display: grid;
  place-content: center;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--text-secondary);
  font: inherit;
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
}

.icon-btn:hover {
  background: var(--hover);
  color: var(--text-primary);
}

.icon-btn.add {
  margin: 4px 8px;
  border-color: var(--border);
}

/* Les suppressions restent discretes : visibles au survol ou au focus */
.icon-btn.del { opacity: 0; transition: opacity 0.12s; }

th:hover .icon-btn.del,
tr:hover .icon-btn.del,
.icon-btn.del:focus-visible { opacity: 1; }

.icon-btn.del:hover { color: var(--danger); }

/* Visible en permanence, contrairement au x : sa presence signale que la ligne
   porte une couleur choisie a la main. */
.icon-btn.reset { font-size: 13px; }

@media (hover: none) {
  .icon-btn.del { opacity: 1; }
}

/* ---------- Glisser-deposer ---------- */

tr.dragging th, tr.dragging td { opacity: 0.45; }

/* Indicateur d insertion : un trait interne, fiable dans un layout de table
   (un ::after positionne ne l est pas). */
tr.drop-above th, tr.drop-above td { box-shadow: inset 0 2px 0 0 var(--series-1); }
tr.drop-below th, tr.drop-below td { box-shadow: inset 0 -2px 0 0 var(--series-1); }

@media (prefers-reduced-motion: reduce) {
  .icon-btn.del { transition: none; }
}

/* ---------- Plein ecran, sans defilement de page (tablette et plus) ----------

   Place en fin de feuille a dessein : une media query n ajoute aucune
   specificite, donc ces regles doivent venir APRES celles qu elles remplacent.

   La hauteur cesse d etre une somme de vh (graphique 45vh + tableau 58vh
   depassaient a eux seuls la fenetre) : la page tient dans la fenetre, le
   graphique prend sa part, et la carte du tableau absorbe le reste. Seul
   .grid-wrap defile.

   Conditionne a une fenetre assez large ET assez haute : en dessous, la page
   defile normalement, ce qui vaut mieux qu un tableau haut de 30 px. */
@media (min-width: 768px) and (min-height: 480px) {
  body {
    height: 100vh;
    height: 100dvh;
    padding-bottom: 16px;
    display: flex;
    flex-direction: column;
  }

  .topbar { flex: none; }

  main {
    flex: 1;
    min-height: 0;
    /* filtres, graphique, puis le tableau qui prend tout le reste */
    grid-template-rows: auto auto minmax(0, 1fr);
  }

  .chart-wrap {
    height: 36vh;
    min-height: 200px;
  }

  .table-card {
    display: flex;
    flex-direction: column;
    min-height: 0;
  }

  .table-card .msg,
  .table-card .hint { flex: none; }

  /* flex: 0 1 auto plutot que flex: 1 — la hauteur suit le contenu mais peut
     retrecir : avec deux lignes le cadre reste court, avec cinquante il se
     limite a la place disponible et defile. */
  .table-card .grid-wrap {
    flex: 0 1 auto;
    min-height: 0;
    max-height: none;
  }
}
