/* Thème SAVELEC — bleu Leclerc + orange du logo. Clair par défaut, sombre au choix (html[data-theme="dark"]). */

:root {
  color-scheme: light;
  /* Couleurs de marque */
  --primaire: #0f62ba;        /* fonds pleins : header, boutons, lien actif */
  --accent: #0f62ba;          /* textes/liens bleus */
  --orange: #f0663b;          /* accent secondaire SAVELEC */
  --orange-texte: #c24e24;    /* orange lisible en texte sur fond clair */
  /* Neutres */
  --texte: #1a1a1a;
  --gris: #555;
  --bord: #d8dce1;
  --fond: #f5f7fa;
  --surface: #ffffff;
  --surface-2: #fafbfc;
  --survol: #eef3fb;
  --bleu-pale: #e4edfb;
  --desactive: #9aa1a9;
  /* États */
  --vert-fond: #e3f5e8;  --vert-texte: #1e7a34;
  --ambre-fond: #fdf0dd; --ambre-texte: #9a6b15;
  --rouge: #b3261e;      --rouge-fond: #fbe6e5;
  --rouge-solide: #b3261e;
  --neutre-fond: #f0f1f3; --neutre-texte: #6b7280;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --primaire: #1c66b3;
  --accent: #7ab3ef;
  --orange: #f58a5e;
  --orange-texte: #f58a5e;
  --texte: #e8eaed;
  --gris: #a5adb8;
  --bord: #3a4048;
  --fond: #14171b;
  --surface: #1e2329;
  --surface-2: #232930;
  --survol: #2a3442;
  --bleu-pale: #21354e;
  --desactive: #6b7480;
  --vert-fond: #1d3626;  --vert-texte: #7fd196;
  --ambre-fond: #3d2f18; --ambre-texte: #e8bd7a;
  --rouge: #ef8b82;      --rouge-fond: #442022;
  --rouge-solide: #a8352c;
  --neutre-fond: #2b3138; --neutre-texte: #9aa4b1;
}

/* Le logo (couleurs de marque sur fond transparent) garde une plaque claire en sombre */
html[data-theme="dark"] .login-logo,
html[data-theme="dark"] .topnav-logo img {
  background: #fff;
  border-radius: 8px;
  padding: 0.3rem 0.5rem;
}

/* Bouton bascule de thème (icône discrète, sans texte) */
.btn-theme {
  width: auto;
  margin: 0;
  padding: 0.35rem;
  background: transparent;
  color: var(--gris);
  border: 1px solid var(--bord);
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 0;
}
.btn-theme:hover { color: var(--orange-texte); }
.btn-theme svg { margin: 0; }

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  background: var(--fond);
  color: var(--texte);
  margin: 0;
  padding-bottom: 3rem;
}

header {
  background: var(--primaire);
  color: white;
  padding: 0.65rem 1.1rem;
}

header h1 {
  margin: 0;
  font-size: 1.05rem;
}

main {
  max-width: 480px;
  margin: 0.6rem auto;
  padding: 0 0.85rem;
}

fieldset {
  border: 1px solid var(--bord);
  border-radius: 10px;
  margin-bottom: 0.5rem;
  padding: 0.6rem 0.85rem;
  background: var(--surface);
}

legend {
  font-weight: 600;
  color: var(--accent);
  padding: 0 0.4rem;
  font-size: 0.95rem;
}

label {
  display: block;
  font-size: 0.85rem;
  color: var(--gris);
  margin-bottom: 0.55rem;
}

label:last-child { margin-bottom: 0.15rem; }

input, select {
  display: block;
  width: 100%;
  margin-top: 0.25rem;
  padding: 0.5rem 0.6rem;
  font-size: 1rem;
  border: 1px solid var(--bord);
  border-radius: 8px;
  background: var(--surface);
  color: var(--texte);
}

input:focus, select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

input[readonly] {
  background: var(--fond);
  color: var(--gris);
}

.case-config {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  font-size: 0.85rem;
  color: var(--gris);
  line-height: 1.4;
}

.case-config input[type="checkbox"] {
  width: auto;
  margin: 0.15rem 0 0;
  flex-shrink: 0;
}

.titre-page {
  font-size: 1.15rem;
  margin: 0.25rem 0 0.75rem;
}

.aide {
  font-size: 0.82rem;
  color: var(--gris);
  margin: 0 0 0.6rem;
  line-height: 1.5;
}

.aide a { color: var(--accent); font-weight: 600; }

.bloc-test {
  background: var(--surface);
  border: 1px solid var(--bord);
  border-radius: 10px;
  padding: 0.7rem 0.85rem;
  margin-top: 1rem;
}

.bloc-test h2 {
  font-size: 0.95rem;
  color: var(--accent);
  margin: 0 0 0.5rem;
}

details summary {
  font-size: 0.85rem;
  color: var(--orange-texte);
  font-weight: 600;
  cursor: pointer;
  margin-bottom: 0.15rem;
  padding: 0.25rem 0;
}

details[open] summary { margin-bottom: 0.4rem; }

/* --- Mention d'information RGPD (aperçu du reçu) --- */

.mention-rgpd {
  font-size: 0.78rem;
  line-height: 1.45;
  color: var(--gris);
  background: var(--fond);
  border-left: 3px solid var(--bord);
  border-radius: 6px;
  padding: 0.55rem 0.7rem;
  margin-bottom: 1rem;
}

/* --- Signature du client --- */

.signature-bloc {
  background: var(--surface);
  border: 1px solid var(--bord);
  border-radius: 10px;
  padding: 0.75rem 0.85rem;
  margin-bottom: 1rem;
}

.signature-titre {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--gris);
  margin-bottom: 0.5rem;
}

.signature-pad {
  width: 100%;
  height: auto;
  aspect-ratio: 3 / 1;
  border: 1px dashed var(--bord);
  border-radius: 8px;
  background: #fff;
  touch-action: none;
  cursor: crosshair;
  display: block;
}

.signature-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.6rem;
}

.signature-actions button {
  width: auto;
  margin: 0;
  padding: 0.5rem 0.9rem;
  font-size: 0.9rem;
}

#sig-effacer {
  background: var(--surface);
  color: var(--gris);
  border: 1px solid var(--bord);
}

#sig-statut { margin-top: 0.5rem; }

details label { margin-bottom: 0.15rem; }

button {
  display: block;
  width: 100%;
  padding: 0.85rem;
  font-size: 1.05rem;
  font-weight: 600;
  border: none;
  border-radius: 10px;
  background: var(--primaire);
  color: white;
  margin-bottom: 0.6rem;
  cursor: pointer;
}

button:active { opacity: 0.85; }

button:disabled { opacity: 0.5; cursor: not-allowed; }

button svg, a svg {
  vertical-align: -2px;
  margin-right: 0.35rem;
}

#btn-nouveau {
  background: var(--surface);
  color: var(--orange-texte);
  border: 1px solid var(--orange);
}

.erreur {
  color: var(--rouge);
  font-weight: 600;
}

.statut {
  text-align: center;
  font-weight: 600;
  padding: 0.6rem;
  border-radius: 8px;
}

.statut.ok { background: var(--vert-fond); color: var(--vert-texte); }
.statut.err { background: var(--rouge-fond); color: var(--rouge); }
.statut.info { background: var(--ambre-fond); color: var(--ambre-texte); }

.recu {
  background: var(--surface);
  border: 1px solid var(--bord);
  border-radius: 10px;
  padding: 1.25rem;
  margin-bottom: 1rem;
}

.recu h2 {
  margin: 0 0 0.15rem;
  font-size: 1.1rem;
}

.entreprise-apercu {
  font-size: 0.82rem;
  color: var(--gris);
  border-bottom: 1px solid var(--bord);
  padding-bottom: 0.6rem;
  margin-bottom: 0.7rem;
  line-height: 1.5;
}

.recu .numero {
  color: var(--gris);
  font-size: 0.85rem;
  margin-bottom: 0.9rem;
}

.recu p {
  margin: 0.3rem 0;
  font-size: 0.95rem;
}

.recu .montant {
  font-size: 1.7rem;
  font-weight: 700;
  color: var(--accent);
  margin: 0.75rem 0 0.1rem;
}

.recu .tva-apercu {
  color: var(--gris);
  font-size: 0.82rem;
  margin: 0 0 0.6rem;
}

.recu .footer {
  margin-top: 1rem;
  padding-top: 0.75rem;
  border-top: 1px dashed var(--bord);
  font-size: 0.8rem;
  color: var(--gris);
}

/* Aperçu du reçu généré : texte brut, identique au SMS (retours à la ligne conservés) */
#recu-apercu {
  white-space: pre-wrap;
  line-height: 1.55;
  font-size: 0.92rem;
}

/* Message flottant (toast) affiché par-dessus la page après un envoi réussi */
.toast-overlay {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.96);
  background: #1e8e3e;
  color: #fff;
  padding: 1rem 1.6rem;
  border-radius: 12px;
  font-size: 1.05rem;
  font-weight: 600;
  text-align: center;
  max-width: 80%;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.28);
  opacity: 0;
  transition: opacity 0.3s ease, transform 0.3s ease;
  z-index: 2000;
  pointer-events: none;
}

.toast-overlay.visible {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

@media print {
  /* Impression toujours en clair, même si l'écran est en mode sombre */
  :root {
    --texte: #000; --gris: #555; --bord: #ccc;
    --fond: #fff; --surface: #fff;
    --accent: #1d5fbf; --primaire: #1d5fbf;
  }
  header, form, .actions, #sms-statut, .topnav { display: none !important; }
  main { max-width: 100%; margin: 0; }
  .recu { border: none; }
}

/* --- Navigation --- */

.topnav {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--bord);
  padding: 0.45rem 1rem;
}

.topnav-logo { display: block; flex-shrink: 0; }

.topnav-logo img {
  display: block;
  height: 30px;
  width: auto;
}

.topnav-links {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
}

.topnav-links a {
  color: var(--gris);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 600;
  padding: 0.4rem 0.7rem;
  border-radius: 8px;
  white-space: nowrap;
}

.topnav-links a:hover { color: var(--orange-texte); }

.topnav-links a.active { background: var(--primaire); color: white; }

.topnav-user {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  color: var(--gris);
}

.topnav-user button {
  display: inline-block;
  width: auto;
  margin-bottom: 0;
  padding: 0.4rem 0.7rem;
  font-size: 0.85rem;
  background: var(--surface);
  color: var(--orange-texte);
  border: 1px solid var(--orange);
}

@media (max-width: 480px) {
  .topnav { padding: 0.35rem 0.7rem; gap: 0.25rem 0.5rem; }
  .topnav-logo img { height: 24px; }
  .topnav-links a { padding: 0.3rem 0.5rem; font-size: 0.78rem; }
  .topnav-user { font-size: 0.75rem; gap: 0.3rem; }
  .topnav-user button { padding: 0.3rem 0.5rem; font-size: 0.75rem; }
  .topnav-user .role-text { display: none; }
}

/* --- Connexion --- */

.page-login { display: flex; align-items: center; justify-content: center; min-height: 100vh; }

.login-box {
  max-width: 340px;
  width: 100%;
  margin: 0;
  padding: 2rem 1.5rem;
  background: var(--surface);
  border: 1px solid var(--bord);
  border-radius: 12px;
}

.login-logo {
  display: block;
  width: 100%;
  max-width: 260px;
  height: auto;
  margin: 0 auto 0.9rem;
}

.login-box h1 { font-size: 1rem; text-align: center; margin: 0 0 1.25rem; color: var(--gris); font-weight: 600; }

/* --- Pages larges (historique, admin, tableau de bord) --- */

.page-large {
  max-width: 100%;
  width: 100%;
  margin: 1.25rem auto;
  padding: 0 1rem;
  box-sizing: border-box;
}

.page-large h1 { font-size: 1.2rem; }
.page-large h2 { font-size: 1rem; color: var(--accent); margin-top: 2rem; }

.page-large form {
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--bord);
  border-radius: 10px;
  padding: 1rem;
}

/* --- Filtres de l'historique --- */

.filtres {
  background: var(--surface);
  border: 1px solid var(--bord);
  border-radius: 10px;
  padding: 0.75rem 0.85rem;
  margin-bottom: 0.9rem;
}

.filtres .recherche {
  max-width: 100%;
  margin: 0 0 0.6rem;
}

#filtres-avances summary {
  font-size: 0.85rem;
  color: var(--orange-texte);
  font-weight: 600;
  cursor: pointer;
  padding: 0.15rem 0;
}

#filtres-avances[open] summary { margin-bottom: 0.5rem; }

.filtres-ligne {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.5rem 0.75rem;
}

.filtres-ligne label {
  margin-bottom: 0;
  flex: 0 1 auto;
}

.filtres-ligne input, .filtres-ligne select {
  width: auto;
  min-width: 8.5rem;
}

.filtres-ligne button {
  display: inline-block;
  width: auto;
  margin-bottom: 0;
  padding: 0.5rem 0.8rem;
  font-size: 0.85rem;
  background: var(--surface);
  color: var(--orange-texte);
  border: 1px solid var(--orange);
}

.resume {
  margin: 0.6rem 0 0;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--accent);
}

.recherche {
  max-width: 420px;
  margin-bottom: 1rem;
}

.table-scroll { overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  font-size: 0.85rem;
  min-width: 640px;
  border: 1px solid var(--bord);
  border-radius: 10px;
  overflow: hidden;
}

th, td {
  text-align: left;
  padding: 0.5rem 0.6rem;
  border-bottom: 1px solid var(--bord);
  white-space: nowrap;
}

th { color: var(--gris); font-weight: 600; background: var(--fond); }

tbody tr:nth-child(even) { background: var(--surface-2); }
tbody tr:hover { background: var(--survol); }

.th-montant, .cell-montant { text-align: right; }
.cell-montant { font-weight: 700; color: var(--accent); white-space: nowrap; }
.cell-numero { font-weight: 600; }
.cell-client { font-weight: 600; }

.lien-voir {
  color: var(--accent);
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
}
.lien-voir:hover { text-decoration: underline; }

/* --- Badges --- */

.badge {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 99px;
  font-size: 0.78rem;
  font-weight: 600;
  white-space: nowrap;
}

.badge-especes { background: var(--vert-fond); color: var(--vert-texte); }
.badge-cb { background: var(--bleu-pale); color: var(--accent); }
.badge-cheque { background: var(--ambre-fond); color: var(--ambre-texte); }
.badge-ok { background: var(--vert-fond); color: var(--vert-texte); }
.badge-non { background: var(--neutre-fond); color: var(--neutre-texte); }
.badge-annule { background: var(--rouge-fond); color: var(--rouge); }

/* Reçu annulé : ligne grisée, montant barré */
tr.ligne-annulee td { color: var(--desactive); }
tr.ligne-annulee .cell-montant { text-decoration: line-through; color: var(--desactive); }
tr.ligne-annulee .cell-numero { color: var(--desactive); }

td .btn-annuler {
  background: var(--surface);
  color: var(--rouge);
  border: 1px solid var(--rouge);
}

td .btn-sms-ligne {
  background: var(--surface);
  color: var(--accent);
  border: 1px solid var(--accent);
}

/* --- Lignes de regroupement (semaine / mois / année) --- */

tbody tr.ligne-groupe td {
  background: var(--bleu-pale);
  color: var(--accent);
  font-weight: 700;
  white-space: normal;
  border-left: 3px solid var(--orange);
}

tbody tr.ligne-groupe:hover td { background: var(--bleu-pale); }

.titre-danger {
  font-size: 1rem;
  color: var(--rouge);
  margin: 1.4rem 0 0.5rem;
}

/* --- Zone sensible (admin) --- */

.zone-danger {
  background: var(--surface);
  border: 1px solid var(--rouge);
  border-radius: 10px;
  padding: 0.75rem 0.85rem;
}

.zone-danger p {
  font-size: 0.85rem;
  color: var(--gris);
  margin: 0 0 0.6rem;
  line-height: 1.5;
}

.btn-danger {
  background: var(--rouge-solide);
  color: white;
}

/* --- Purge RGPD (historique) --- */

.bloc-purge {
  margin-bottom: 1rem;
}

/* --- Export RGPD d'un client (historique) --- */

.bloc-rgpd {
  background: var(--surface);
  border: 1px solid var(--bord);
  border-radius: 10px;
  padding: 0.75rem 0.85rem;
  margin-bottom: 1rem;
}

.export-client-ligne {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.export-client-ligne input {
  flex: 1 1 220px;
  margin: 0;
}

.export-client-ligne button {
  width: auto;
  flex: 0 0 auto;
}

.liste-purge {
  list-style: none;
  margin: 0.6rem 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.liste-purge li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  background: var(--fond);
  border-radius: 8px;
  padding: 0.45rem 0.6rem;
  font-size: 0.85rem;
}

td button, td select {
  display: inline-block;
  width: auto;
  margin: 0 0.2rem 0 0;
  padding: 0.35rem 0.6rem;
  font-size: 0.8rem;
}

td button.btn-supprimer { background: var(--rouge-solide); color: white; }

/* --- Tableau de bord --- */

.cartes-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 0.7rem;
  margin-bottom: 1rem;
}

.carte-stat {
  flex: 1 1 130px;
  background: var(--surface);
  border: 1px solid var(--bord);
  border-radius: 10px;
  padding: 0.7rem 0.85rem;
}

.carte-stat .valeur {
  font-size: 1.45rem;
  font-weight: 700;
  color: var(--accent);
}

.carte-stat .libelle {
  font-size: 0.8rem;
  color: var(--gris);
  font-weight: 600;
}

.bloc-stats {
  background: var(--surface);
  border: 1px solid var(--bord);
  border-radius: 10px;
  padding: 0.75rem 0.85rem;
  margin-bottom: 0.9rem;
}

.bloc-stats h2 {
  font-size: 0.95rem;
  color: var(--accent);
  margin: 0 0 0.6rem;
}

.ligne-barre {
  display: grid;
  grid-template-columns: minmax(90px, 130px) 1fr auto;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 0.45rem;
  font-size: 0.85rem;
}

.ligne-barre .nom { color: var(--texte); font-weight: 600; overflow: hidden; text-overflow: ellipsis; }

.ligne-barre .piste {
  display: block;
  background: var(--bleu-pale);
  border-radius: 99px;
  height: 14px;
  overflow: hidden;
}

.ligne-barre .remplie {
  display: block;
  background: var(--primaire);
  height: 100%;
  border-radius: 99px;
  min-width: 2px;
}

.ligne-barre.orange .remplie { background: var(--orange); }

.ligne-barre .chiffre { color: var(--gris); font-weight: 600; white-space: nowrap; }

.nav-mois {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 0.9rem;
}

.nav-mois button {
  display: inline-block;
  width: auto;
  margin: 0;
  padding: 0.35rem 0.7rem;
  font-size: 0.9rem;
  background: var(--surface);
  color: var(--accent);
  border: 1px solid var(--accent);
}

.nav-mois .mois-courant {
  font-weight: 700;
  font-size: 1rem;
  min-width: 10rem;
  text-align: center;
}

/* --- Tableaux en cartes empilées sur mobile --- */

@media (max-width: 640px) {
  .table-scroll { overflow-x: visible; }

  table { min-width: 0; width: 100%; border: none; border-radius: 0; overflow: visible; background: none; }
  table thead { display: none; }
  table, table tbody, table tr, table td { display: block; width: 100%; }

  tbody tr:nth-child(even) { background: var(--surface); }
  tbody tr:hover { background: var(--surface); }

  table tr {
    border: 1px solid var(--bord);
    border-radius: 10px;
    margin-bottom: 0.6rem;
    padding: 0.4rem 0.75rem;
    background: var(--surface);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  }

  table td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    border-bottom: none;
    padding: 0.32rem 0;
    white-space: normal;
  }

  table td[data-label]:not([data-label=""])::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--gris);
    flex-shrink: 0;
  }

  table td[data-label=""] {
    justify-content: flex-start;
    flex-wrap: wrap;
    gap: 0.4rem;
  }

  table td button { flex: 1 1 auto; }

  /* En-tête de carte : le n° de reçu ressort */
  td.cell-numero {
    border-bottom: 1px dashed var(--bord);
    padding-bottom: 0.45rem;
    margin-bottom: 0.2rem;
    color: var(--accent);
  }

  td.cell-montant { font-size: 1.05rem; }

  /* Filtres compacts sur mobile : dates côte à côte, le reste pleine largeur */
  .filtres-ligne label, .filtres-ligne input, .filtres-ligne select, .filtres-ligne button {
    width: 100%;
    min-width: 0;
    flex: 1 1 100%;
  }

  .filtres-ligne label.filtre-date { flex: 1 1 45%; }

  /* Lignes de groupe : bandeau au-dessus des cartes */
  tr.ligne-groupe {
    border: none;
    background: none;
    padding: 0;
    box-shadow: none;
    margin-bottom: 0.5rem;
  }

  tr.ligne-groupe td {
    border-radius: 8px;
    padding: 0.45rem 0.75rem;
    justify-content: flex-start;
  }
}
