/* Thème clair par défaut ; sombre selon le système ou l'attribut data-theme. */
:root {
  --fond: #f4f6f2;
  --carte: #ffffff;
  --texte: #1c2320;
  --discret: #66716b;
  --vert: #1f6f5b;
  --vert-clair: #e2efe9;
  --rouge: #b3261e;
  --ambre: #8a5300;
  --ambre-clair: #fdf1d8;
  --bord: #d8dfd9;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --fond: #121714;
    --carte: #1b221e;
    --texte: #e4ebe6;
    --discret: #9aa69f;
    --vert: #5cc2a1;
    --vert-clair: #203029;
    --rouge: #ff8b82;
    --ambre: #f0b35a;
    --ambre-clair: #3a2e17;
    --bord: #2d3732;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --fond: #121714;
  --carte: #1b221e;
  --texte: #e4ebe6;
  --discret: #9aa69f;
  --vert: #5cc2a1;
  --vert-clair: #203029;
  --rouge: #ff8b82;
  --ambre: #f0b35a;
  --ambre-clair: #3a2e17;
  --bord: #2d3732;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  min-height: 100vh;
  background: var(--fond);
  color: var(--texte);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow-x: hidden;
}

h2 { font-size: 20px; margin: 6px 0 12px; }
h3 { font-size: 16px; margin: 20px 0 8px; }
.discret { color: var(--discret); font-size: 14px; }
.num { text-align: right; white-space: nowrap; }
.pos { color: var(--vert); }

button, input, select { font: inherit; }
button {
  padding: 9px 14px;
  border: 0;
  border-radius: 10px;
  background: var(--vert);
  color: #fff;
  cursor: pointer;
  white-space: nowrap;
}
button.ghost { background: transparent; color: var(--vert); border: 1px solid var(--bord); }
button.danger { color: var(--rouge); }
input, select {
  padding: 8px 10px;
  border: 1px solid var(--bord);
  border-radius: 8px;
  background: var(--carte);
  color: var(--texte);
  max-width: 100%;
}

/* ----- Structure ----- */

.haut {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 10px;
  height: 56px;
  padding: 0 14px;
  background: var(--carte);
  border-bottom: 1px solid var(--bord);
}
.marque { flex: 1; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cycle-select { max-width: 170px; }
@media (max-width: 480px) {
  .marque { display: none; }
}

.menu {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 8px 12px;
  background: var(--carte);
  border-bottom: 1px solid var(--bord);
  scrollbar-width: none;
}
.menu::-webkit-scrollbar { display: none; }
.menu button { background: transparent; color: var(--texte); border: 1px solid var(--bord); }
.menu button.actif { background: var(--vert); color: #fff; border-color: var(--vert); }

.vue { max-width: 1100px; margin: 0 auto; padding: 12px 14px 120px; }

.fab {
  position: fixed;
  right: 18px;
  bottom: 22px;
  z-index: 6;
  width: 58px;
  height: 58px;
  padding: 0;
  border-radius: 50%;
  font-size: 30px;
  line-height: 1;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .25);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  z-index: 9;
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--texte);
  color: var(--fond);
}
.toast.erreur { background: var(--rouge); color: #fff; }
.toast[hidden] { display: none; }

@media (min-width: 900px) {
  .menu {
    position: fixed;
    top: 56px;
    bottom: 0;
    left: 0;
    z-index: 4;
    width: 210px;
    flex-direction: column;
    overflow: visible;
    padding: 14px 12px;
    border-right: 1px solid var(--bord);
    border-bottom: 0;
  }
  .menu button { text-align: left; }
  .vue { margin-left: 210px; padding: 20px 28px 80px; }
  .cartes { grid-template-columns: repeat(4, 1fr); }
  .env { grid-template-columns: 2fr 1fr 1fr 1fr 1fr; }
  .env-nom { grid-column: auto; }
}

/* ----- Cycle : cartes et enveloppes ----- */

.entete-vue {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 6px 0 12px;
}
.actions-vue { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.suivants { display: flex; align-items: center; gap: 6px; font-size: 14px; color: var(--discret); }

.cartes { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.cartes.deux { grid-template-columns: repeat(2, 1fr); }
.carte {
  padding: 12px;
  background: var(--carte);
  border: 1px solid var(--bord);
  border-radius: 12px;
}
.carte span, .carte small { display: block; color: var(--discret); font-size: 13px; }
.carte b { font-size: 20px; }
.carte.neg b { color: var(--rouge); }

.envs { display: grid; gap: 8px; }
.env {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 12px;
  align-items: center;
  padding: 10px 12px;
  background: var(--carte);
  border: 1px solid var(--bord);
  border-radius: 12px;
}
.env.depasse { border-color: var(--rouge); }
.env-nom { grid-column: 1 / -1; font-weight: 600; }
.env-prevu { display: grid; gap: 2px; font-size: 12px; color: var(--discret); }
.env-prevu input { width: 100%; }
.env-val span { display: block; font-size: 12px; color: var(--discret); }
.env .barre { grid-column: 1 / -1; height: 6px; background: var(--bord); border-radius: 4px; overflow: hidden; }
.env .barre span { display: block; height: 100%; background: var(--vert); }
.env.depasse .barre span { background: var(--rouge); }

/* ----- Listes de lignes ----- */

.liste { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.tx {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: var(--carte);
  border: 1px solid var(--bord);
  border-radius: 12px;
  cursor: pointer;
}
.tx-gauche { display: grid; min-width: 0; }
.tx-lib { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-date, .tx-gauche .discret { font-size: 12px; color: var(--discret); }
.tx-droite { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; white-space: nowrap; }
.filtres { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.tx b.neg { color: var(--rouge); }

.badge {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--vert-clair);
  color: var(--vert);
}
.badge.warn { background: var(--ambre-clair); color: var(--ambre); }

/* ----- Tableaux modifiables (ordinateur) ----- */

.table-wrap {
  overflow: visible;
  background: var(--carte);
  border: 1px solid var(--bord);
  border-radius: 12px;
  margin-bottom: 8px;
}
table { width: 100%; min-width: 620px; border-collapse: separate; border-spacing: 0; }
th, td { padding: 6px 8px; border-bottom: 1px solid var(--bord); text-align: left; vertical-align: middle; }
th { font-size: 12px; font-weight: 600; color: var(--discret); white-space: nowrap; }
/* L'en-tête reste visible en défilant (sous la barre du haut). Sur ordinateur, un tableau trop large fait défiler la page entière : un tableau ne peut pas avoir son propre défilement, sinon son en-tête ne resterait pas collé. */
thead th { position: sticky; top: 56px; z-index: 2; background: var(--carte); }
@media (min-width: 720px) {
  body { overflow-x: auto; padding-top: 56px; }
  .haut { position: fixed; top: 0; left: 0; right: 0; }
  /* Le cadre du tableau s'agrandit avec lui : fond et bordure couvrent toute sa largeur. */
  .table-wrap { width: max-content; min-width: 100%; }
}
/* Tri : sur ordinateur, on clique sur l'en-tête de la colonne ; sur téléphone, des boutons le remplacent. */
.tri-entete { background: none; border: 0; padding: 0; border-radius: 0; color: inherit; font: inherit; cursor: pointer; }
.tri-entete:hover, .tri-entete.actif { color: var(--vert); }
.tri-telephone { display: none; }
@media (max-width: 719px) {
  .tri-telephone { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 0 0 12px; }
  .tri-telephone button.actif { background: var(--vert); color: #fff; border-color: var(--vert); }
}
td input, td select { width: 100%; min-width: 90px; }
td input[type="checkbox"] { width: auto; min-width: 0; }
/* Noms, types et réserves lisibles sans être coupés sur ordinateur, sans dépasser la largeur de l'écran. */
td.t-texte input { min-width: 160px; }
td.t-choix select { min-width: 110px; }
td.t-relation select { min-width: 140px; }
td.t-nombre input { min-width: 70px; }
tr.nouvelle td { background: var(--vert-clair); }
.mini { font-size: 12px; color: var(--discret); word-break: break-all; }

/* ----- Formulaires et fenêtres ----- */

.formulaire label {
  display: grid;
  gap: 4px;
  margin-bottom: 10px;
  font-size: 14px;
  color: var(--discret);
}
.grille { display: grid; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 600px) {
  .grille { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); column-gap: 12px; }
}
/* Les champs restent dans la fenêtre, même si un bouton de fichier est plus large que sa colonne. */
.formulaire label { min-width: 0; }
.formulaire .choix { display: flex; align-items: center; gap: 8px; }
.formulaire .choix input { width: 18px; height: 18px; padding: 0; margin: 0; }
.formulaire input, .formulaire select { width: 100%; height: 40px; max-width: 100%; }
.formulaire input[type="file"] { width: 100%; }
.actions { display: flex; align-items: center; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.espace { flex: 1; }

dialog {
  width: min(560px, calc(100% - 24px));
  max-height: calc(100vh - 32px);
  overflow: auto;
  padding: 18px;
  border: 0;
  border-radius: 16px;
  background: var(--carte);
  color: var(--texte);
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }

.vide { text-align: center; padding: 40px 0; }
.connexion { max-width: 380px; margin: 12vh auto 0; padding: 0 16px; }
.connexion h1 { font-size: 22px; }

/* Tableau des récurrents : chaque colonne garde sa largeur, l'écran défile si besoin. */
.vue.large { max-width: none; }
table.large { min-width: 1500px; }
table.large th, table.large td { padding: 6px 5px; }
table.large td input[type="text"] { min-width: 190px; }
table.large td input[type="date"] { min-width: 135px; }
table.large td input[type="number"] { min-width: 80px; }
table.large td select { min-width: 115px; }
table.large td select[data-k="enveloppe"], table.large td select[data-k="compte"], table.large td select[data-k="compte_cible"] { min-width: 145px; }

/* Téléphone (moins de 720 px) : chaque ligne d'un tableau devient une carte, avec le nom de chaque champ au-dessus. */
@media (max-width: 719px) {
  .table-wrap { background: none; border: 0; overflow: visible; }
  table, table.large { min-width: 0; }
  thead { display: none; }
  table, tbody, tr, td { display: block; width: 100%; }
  tr {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px 10px;
    margin-bottom: 10px;
    padding: 10px 12px;
    background: var(--carte);
    border: 1px solid var(--bord);
    border-radius: 12px;
  }
  tr.nouvelle { background: var(--vert-clair); }
  td { min-width: 0; padding: 0; border: 0; }
  td::before {
    content: attr(data-label);
    display: block;
    margin-bottom: 2px;
    font-size: 12px;
    color: var(--discret);
  }
  td.actions-ligne::before { display: none; }
  td.t-texte, td.t-relation, td.t-choix, td.actions-ligne { grid-column: 1 / -1; }
  td input, td select { width: 100%; min-width: 0 !important; }
  td input[type="checkbox"] { width: 20px; height: 20px; }
  td.actions-ligne button { width: 100%; }
  .journal-detail { display: none; }
}
