/* ===== Base ===== */
:root {
  --navy: #062259;
  --navy-2: #0d3a8f;
  --line: #e2e8f0;
  --line-2: #cbd5e1;
  --bg: #f5f7fa;
  --text: #1c2733;
  --muted: #64748b;
  --blue: #0d6efd;
  --lightblue: #7fbfff;
  --brown: #8a5a2b;
  --yellow: #f2c200;
  --red: #dc2626;
  --green: #16a34a;
  --gray: #94a3b8;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Arial, sans-serif;
  color: var(--text); background: var(--bg); font-size: 14px;
  display: flex; flex-direction: column; min-height: 100vh;
}
h1 { font-size: 1.3rem; margin: 0; }
a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }
.muted { color: var(--muted); font-size: 0.85rem; }

/* ===== Topbar (charte Spekty — identique integrations) ===== */
.app-header {
  display: flex; align-items: center; gap: 1.5rem;
  background: #1f2c54; border-bottom: 3px solid #3d92d1;
  box-shadow: 0 2px 12px rgba(0,0,0,.18);
  padding: .7rem 1.4rem; position: sticky; top: 0; z-index: 100;
}
.app-header nav { align-items: center; }
.app-brand { display: flex; align-items: center; gap: .65rem; font-weight: 700; }
.app-brand-logo-img { height: 34px; width: auto; display: block; }
.app-brand-name {
  color: rgba(255,255,255,.9); font-size: .95rem; font-weight: 600;
  padding-left: .65rem; border-left: 1px solid rgba(255,255,255,.28); white-space: nowrap;
}
.app-version { color: rgba(255,255,255,.5); font-size: .68rem; font-weight: 500; margin-left: .15rem; }
.nav { display: flex; align-items: center; gap: 0.25rem; flex: 1; }
.nav-link { padding: 0.4rem 0.7rem; border-radius: 6px; }
.app-header nav a.nav-link { color: #e7ecf6 !important; opacity: .82; font-weight: 500; }
.app-header nav a.nav-link:hover { color: #fff !important; opacity: 1; text-decoration: none; background: rgba(255,255,255,.1); }
.app-header nav a.nav-link.active {
  color: #fff !important; opacity: 1; font-weight: 700; background: transparent;
  text-decoration: underline; text-decoration-color: #3d92d1;
  text-decoration-thickness: 2px; text-underline-offset: 6px;
}
.app-header .logout-link { color: #ffc2c2 !important; }
.app-header .logout-link:hover { color: #fff !important; }
.nav-dropdown { position: relative; }
.nav-dd-toggle {
  background: transparent; border: 0; color: #e7ecf6; opacity: .82; font: inherit; font-weight: 500;
  cursor: pointer; padding: 0.4rem 0.7rem; border-radius: 6px;
}
.nav-dd-toggle:hover { background: rgba(255,255,255,0.1); color: #fff; opacity: 1; }
.nav-dd-menu {
  position: absolute; top: 100%; left: 0; background: #1f2c54;
  border: 1px solid rgba(255,255,255,.15); border-radius: 8px;
  box-shadow: 0 10px 28px rgba(0,0,0,.35); padding: 0.3rem; min-width: 210px;
  display: none; z-index: 200;
}
.nav-dropdown:hover .nav-dd-menu, .nav-dropdown.open .nav-dd-menu { display: block; }
.nav-dd-menu .nav-link { display: block; }
/* Menu déroulant sombre (charte integrations) : texte clair, item actif/survol en navy plus clair, sans soulignement. */
.app-header .nav-dd-menu a.nav-link {
  color: #e7ecf6 !important; opacity: .9; font-weight: 500;
  border-radius: 6px; text-decoration: none !important;
}
.app-header .nav-dd-menu a.nav-link:hover,
.app-header .nav-dd-menu a.nav-link.active {
  background: #2e4272; color: #fff !important; opacity: 1;
  font-weight: 500; text-decoration: none !important;
}
.topbar-right { display: flex; align-items: center; gap: 0.75rem; margin-left: auto; }
#user-info { display: flex; align-items: center; gap: 0.6rem; font-size: 0.9rem; color: #e7ecf6; }
.link-muted { color: #cdd8ec; font-size: 0.85rem; }

/* ===== Badges & buttons ===== */
.badge { font-size: 0.7rem; padding: 0.1rem 0.4rem; border-radius: 4px; font-weight: 700; }
.badge-admin { background: #fff; color: var(--navy); }
.badge-warn { background: var(--yellow); color: #5a4700; }
.btn {
  font: inherit; cursor: pointer; border: 1px solid var(--line-2); background: #fff;
  color: var(--text); padding: 0.4rem 0.8rem; border-radius: 7px; font-weight: 500;
  transition: background 0.12s, border-color 0.12s;
  white-space: nowrap; display: inline-flex; align-items: center; gap: 0.3rem;
}
.btn[hidden] { display: none !important; }   /* l'attribut hidden doit primer sur inline-flex */
.btn:hover:not(:disabled) { background: var(--bg); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--navy); color: #fff; border-color: var(--navy); }
.btn-primary:hover:not(:disabled) { background: var(--navy-2); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover:not(:disabled) { background: #eef2f7; }
.btn-sm { padding: 0.28rem 0.55rem; font-size: 0.85rem; border-radius: 6px; }
.btn-danger { color: var(--red); border-color: #f3c2c2; }
/* Petits boutons icône supprimer/annuler : discrets (gris), fonction peu utilisée. */
.btn-icon-danger { color: var(--muted); border-color: var(--line-2); padding: 0.28rem 0.5rem; font-size: 0.95rem; }
.btn-icon-danger:hover { background: var(--bg); }

/* ===== Pages ===== */
.page-section { padding: 1rem 1.2rem; }
#page-lots { padding: 0.6rem 0.8rem; box-sizing: border-box; }
/* Quand l'onglet Lots est visible : occupe la hauteur sous l'en-tête et laisse la
   grille prendre l'espace restant (barre de défilement horizontale toujours visible). */
#page-lots:not([hidden]) { display: flex; flex-direction: column; height: calc(100vh - 60px); }
.page-head { display: flex; align-items: center; gap: 1rem; margin-bottom: 0.8rem; }
.page-head-actions { margin-left: auto; display: flex; gap: 0.6rem; align-items: center; }
.link-ext { font-size: 0.85rem; }

/* ===== Lots toolbar ===== */
.lots-search {
  display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.5rem; flex-wrap: wrap;
}
.lots-search input {
  flex: 0 1 420px; min-width: 240px; padding: 0.45rem 0.7rem;
  border: 1px solid var(--line-2); border-radius: 7px; font: inherit;
}
.lots-search input:focus { outline: 2px solid #3d92d1; outline-offset: -1px; }
.lots-toolbar {
  display: flex; align-items: center; gap: 0.6rem; margin-bottom: 0.5rem; flex-wrap: wrap;
}
.toolbar-left, .toolbar-right { display: flex; align-items: center; gap: 0.5rem; }
.toolbar-right { margin-left: auto; }
.toggle { display: inline-flex; align-items: center; gap: 0.3rem; font-size: 0.85rem; cursor: pointer; }
/* Toggle 3 positions (périmètre des lots) */
.seg3 { display: inline-flex; border: 1px solid var(--line-2); border-radius: 8px; overflow: hidden; }
.seg3[hidden] { display: none; }
.seg-btn {
  font: inherit; font-size: 0.85rem; cursor: pointer; border: 0; background: #fff;
  color: var(--muted); padding: 0.35rem 0.75rem; border-right: 1px solid var(--line);
}
.seg-btn:last-child { border-right: 0; }
.seg-btn:hover:not(.active) { background: var(--bg); }
.seg-btn.active { background: var(--navy); color: #fff; font-weight: 600; }
#selection-count { font-size: 0.85rem; }
.facturation-bar {
  display: flex; align-items: center; gap: 0.4rem; flex-wrap: wrap;
  background: #eef3fb; border: 1px solid #d5e0f3; border-radius: 8px;
  padding: 0.4rem 0.6rem; margin-bottom: 0.5rem;
}
.facturation-bar .sep { width: 1px; height: 20px; background: #cbd5e1; margin: 0 0.2rem; }

/* ===== Grille type Excel ===== */
.grid-wrap { overflow: auto; border: 1px solid var(--line); border-radius: 8px; background: #fff; flex: 1 1 auto; min-height: 0; }
table.lots-grid {
  border-collapse: separate; border-spacing: 0; table-layout: fixed;
  width: max-content; min-width: 100%;
}
/* Le texte des cellules reste sélectionnable (double-clic pour copier une réf) ;
   seule la sélection de texte au Maj+clic est neutralisée (cf. mousedown en JS). */
table.lots-grid th, table.lots-grid td {
  border-right: 1px solid var(--line); border-bottom: 1px solid var(--line);
  padding: 0.3rem 0.45rem; text-align: left; vertical-align: top; overflow: hidden;
}
table.lots-grid thead th {
  position: sticky; top: 0; z-index: 3; background: #f1f5f9; font-weight: 600; font-size: 0.82rem;
  white-space: nowrap; user-select: none;
}
.th-inner { display: flex; align-items: center; gap: 0.25rem; }
.th-label { cursor: pointer; flex: 1; overflow: hidden; text-overflow: ellipsis; }
.th-sort { font-size: 0.7rem; color: var(--muted); }
.th-filter { cursor: pointer; color: var(--muted); font-size: 0.75rem; padding: 0 2px; }
.th-filter.active { color: #0d6efd; font-size: 0.9rem; font-weight: 700; }
/* Colonne avec filtre actif : mise en évidence forte de l'en-tête. */
table.lots-grid thead th.th-cell.filtered {
  background: #dbeafe;
  box-shadow: inset 0 -3px 0 #0d6efd;
}
table.lots-grid thead th.th-cell.filtered .th-label { color: #0d3a8f; font-weight: 700; }
.col-resizer {
  position: absolute; right: -3px; top: 0; width: 7px; height: 100%; cursor: col-resize; z-index: 4;
}
table.lots-grid thead th { position: sticky; }
.th-cell { position: relative; }

/* cellule multi-lignes : 4 lignes max + ellipsis */
.cell-clip {
  display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical;
  overflow: hidden; line-height: 1.25; max-height: 5em; word-break: break-word;
}
td.num, th.num { text-align: right; }
td.cell-red { color: var(--red); font-weight: 600; }
td.cell-green { color: var(--green); font-weight: 600; }

/* Statut coloré comme sa pastille (gras pour la lisibilité). */
.statut-txt { font-weight: 600; }
.statut-txt.c-blue { color: var(--blue); }
.statut-txt.c-lightblue { color: #3d92d1; }
.statut-txt.c-brown { color: var(--brown); }
.statut-txt.c-yellow { color: #b8860b; }
.statut-txt.c-red { color: var(--red); }
.statut-txt.c-gray { color: var(--gray); }
.statut-txt.c-black { color: #111827; }

/* colonne fixe : cases + pastille + alertes */
th.col-fixed, td.col-fixed {
  position: sticky; left: 0; z-index: 2; background: #fff; white-space: nowrap;
  border-right: 1px solid var(--line-2);
}
thead th.col-fixed { z-index: 5; background: #f1f5f9; }
.fixed-inner { display: flex; align-items: center; gap: 0.35rem; }
.pastille { width: 11px; height: 11px; border-radius: 50%; flex: none; display: inline-block; border: 1px solid rgba(0,0,0,0.15); }
.pastille.blue { background: var(--blue); }
.pastille.lightblue { background: var(--lightblue); }
.pastille.brown { background: var(--brown); }
.pastille.yellow { background: var(--yellow); }
.pastille.red { background: var(--red); }
.pastille.gray { background: var(--gray); }
.pastille.black { background: #111827; }
.alert-ico { font-size: 0.95rem; line-height: 1; cursor: default; }

tr.lot-row { cursor: pointer; }
tr.lot-row:hover td { background: #f8fafc; }
tr.lot-row.selected td { background: #dbeafe !important; }
tr.lot-row.selected td.col-fixed { background: #dbeafe !important; }
.row-check { cursor: pointer; }
.pencil-btn { background: none; border: 0; cursor: pointer; font-size: 0.85rem; padding: 0 2px; color: var(--muted); }
.pencil-btn:hover { color: var(--navy); }

.empty-state { padding: 2rem; text-align: center; color: var(--muted); }

/* ===== Filtre dropdown ===== */
.filter-pop {
  position: absolute; z-index: 500; background: #fff; border: 1px solid var(--line-2);
  border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,0.16); padding: 0.5rem; width: 240px;
}
.filter-pop input[type=text] { width: 100%; padding: 0.3rem; border: 1px solid var(--line-2); border-radius: 6px; margin-bottom: 0.4rem; }
.filter-list { max-height: 220px; overflow: auto; }
.filter-list label { display: flex; align-items: center; gap: 0.4rem; padding: 0.15rem 0; font-size: 0.85rem; }
.filter-actions { display: flex; justify-content: space-between; margin-top: 0.4rem; }

/* ===== Tables génériques (admin/fiches) ===== */
table.data-table { border-collapse: collapse; width: 100%; background: #fff; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
table.data-table th, table.data-table td { border-bottom: 1px solid var(--line); padding: 0.45rem 0.6rem; text-align: left; font-size: 0.88rem; }
table.data-table thead th { background: #f1f5f9; font-weight: 600; }
table.data-table tr:hover td { background: #f8fafc; }
table.data-table input[type=text], table.data-table input[type=number], table.data-table select {
  padding: 0.25rem; border: 1px solid var(--line-2); border-radius: 5px; font: inherit;
}

/* ===== Settings form ===== */
.settings-form { max-width: 640px; display: flex; flex-direction: column; gap: 1rem; }
.settings-form .field { display: flex; flex-direction: column; gap: 0.25rem; }
.settings-form label { font-weight: 600; font-size: 0.9rem; }
.settings-form input { padding: 0.45rem; border: 1px solid var(--line-2); border-radius: 7px; font: inherit; }
.settings-form .hint { font-size: 0.8rem; color: var(--muted); }
.tag-ok { color: var(--green); font-weight: 600; }
.tag-ko { color: var(--muted); }

/* ===== Modales ===== */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(15,23,42,0.45); z-index: 1000;
  display: grid; place-items: center;
}
.modal-box { background: #fff; border-radius: 12px; padding: 1.2rem 1.4rem; width: min(560px, 92vw); max-height: 90vh; overflow: auto; box-shadow: 0 20px 50px rgba(0,0,0,0.3); }
.modal-footer { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 1rem; }
.modal-error { margin-top: 0.8rem; padding: 0.5rem 0.7rem; border-radius: 7px; background: #f8d7da; color: #842029; font-size: 0.9rem; font-weight: 500; }
.modal-input { width: 100%; padding: 0.5rem; border: 1px solid var(--line-2); border-radius: 7px; font: inherit; margin-top: 0.4rem; }
.modal-box h2 { font-size: 1.1rem; margin: 0 0 0.8rem; }
.infos-tools { display: flex; flex-direction: column; gap: 0.5rem; margin: 0.6rem 0; }
.infos-tools .btn { justify-content: flex-start; text-align: left; }
.cumul-table { width: 100%; border-collapse: collapse; margin-top: 0.4rem; }
.cumul-table td { padding: 0.35rem 0.5rem; border-bottom: 1px solid var(--line-2); }
.cumul-table td:last-child { text-align: right; white-space: nowrap; }
.cumul-table tr:last-child td { border-bottom: none; }
.modal-field { display: flex; flex-direction: column; gap: 0.25rem; margin-bottom: 0.7rem; }
.modal-field label { font-weight: 600; font-size: 0.85rem; }
.modal-field input, .modal-field select, .modal-field textarea { padding: 0.4rem; border: 1px solid var(--line-2); border-radius: 6px; font: inherit; }
.modal-field label.rl { display: inline-flex; align-items: center; gap: 0.35rem; font-weight: 400; margin-right: 1.2rem; }
.modal-field label.rl input { padding: 0; }
.rl-date { font-weight: 600; font-size: 1.05rem; color: var(--navy); padding: 0.2rem 0; }
.pole-check-list { display: flex; flex-direction: column; gap: 0.2rem; max-height: 200px; overflow: auto; border: 1px solid var(--line-2); border-radius: 6px; padding: 0.45rem; }
.pole-check-list label { display: flex; align-items: center; gap: 0.45rem; font-weight: 400; font-size: 0.9rem; }

/* ===== Génération synthèses : bandeau + volet ===== */
.synth-banner {
  position: fixed; right: 0; bottom: 0; z-index: 1500;
  background: #1f2c54; color: #fff; padding: 0.5rem 1rem; font-weight: 600; font-size: 0.9rem;
  border-top-left-radius: 8px; box-shadow: 0 -2px 12px rgba(0,0,0,0.25);
  display: flex; align-items: center; gap: 0.5rem;
}
.synth-banner .spin { display: inline-block; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.synth-panel {
  position: fixed; top: 60px; right: 0; width: 350px; max-width: 92vw; height: calc(100vh - 60px);
  background: #fff; border-left: 1px solid var(--line); box-shadow: -6px 0 18px rgba(0,0,0,0.12);
  z-index: 1400; display: flex; flex-direction: column;
}
.synth-panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.6rem 0.8rem; border-bottom: 1px solid var(--line); background: #f1f5f9;
}
.synth-head-actions { display: flex; align-items: center; gap: 0.4rem; }
.synth-panel-head #synth-panel-close { border: 0; background: none; cursor: pointer; font-size: 1rem; color: var(--muted); }
.synth-panel-head #synth-panel-close:hover { color: var(--text); }
#synth-list { overflow: auto; padding: 0.6rem; display: flex; flex-direction: column; gap: 0.5rem; }
.synth-item { border: 1px solid var(--line); border-left: 4px solid var(--gray); border-radius: 7px; padding: 0.5rem 0.6rem; font-size: 0.85rem; }
.synth-item.ok { border-left-color: var(--green); }
.synth-item.err { border-left-color: var(--red); }
.synth-item .ref { font-weight: 700; font-family: ui-monospace, monospace; word-break: break-all; }
.synth-item .st { color: var(--muted); margin-top: 0.15rem; }
.synth-item .msg-err { color: var(--red); margin-top: 0.25rem; }
.synth-item .msg-warn { color: #856404; margin-top: 0.25rem; }
.synth-item .row-b { display: flex; align-items: center; justify-content: space-between; margin-top: 0.35rem; }
.btn-validate { background: var(--yellow); border-color: #d9ae00; color: #5a4700; font-weight: 700; }
.btn-validate:hover { background: #e6b800; }
.synth-item .tm { color: var(--muted); font-size: 0.75rem; }
.synth-reopen {
  position: fixed; right: 0; top: 68px; z-index: 1400;
  background: #1f2c54; color: #fff; border: 0; border-top-left-radius: 8px; border-bottom-left-radius: 8px;
  padding: 0.4rem 0.7rem; cursor: pointer; font-size: 0.85rem; box-shadow: -3px 0 10px rgba(0,0,0,0.2);
}
/* L'attribut `hidden` doit l'emporter sur les display: flex ci-dessus. */
.synth-panel[hidden], .synth-banner[hidden], .synth-reopen[hidden] { display: none !important; }

/* ===== Loader ===== */
.loader-box {
  background: #fff; border-radius: 12px; padding: 1.4rem 1.8rem;
  display: flex; align-items: center; gap: 1rem; font-weight: 500;
  box-shadow: 0 20px 50px rgba(0,0,0,0.3);
}
.spinner { width: 22px; height: 22px; border: 3px solid var(--line); border-top-color: var(--navy); border-radius: 50%; animation: spin 1s linear infinite; }

/* ===== Modale mail de relance ===== */
.mail-head {
  display: flex; flex-direction: column; gap: 0.4rem;
  background: #f1f5f9; border: 1px solid var(--line); border-radius: 8px;
  padding: 0.5rem 0.7rem; margin-bottom: 0.8rem; font-size: 0.9rem;
}
.mail-head .mh-row { display: flex; align-items: center; gap: 0.4rem; flex-wrap: wrap; }
.mail-head .mh-lbl { color: var(--muted); min-width: 60px; }
.mail-head code { background: #fff; border: 1px solid var(--line-2); padding: 0.15rem 0.4rem; border-radius: 5px; }
.rt-toolbar {
  display: flex; align-items: center; gap: 0.2rem; flex-wrap: wrap;
  border: 1px solid var(--line-2); border-bottom: 0; border-radius: 7px 7px 0 0;
  padding: 0.35rem; background: #fafbfc; margin-top: 0.25rem;
}
.rt-toolbar button { border: 1px solid transparent; background: none; cursor: pointer; padding: 0.2rem 0.5rem; border-radius: 5px; font-size: 0.9rem; color: var(--text); min-width: 30px; }
.rt-toolbar button:hover { background: #eef2f7; }
.rt-sep { width: 1px; height: 20px; background: var(--line-2); margin: 0 0.2rem; }
.rt-editor { border: 1px solid var(--line-2); border-radius: 0 0 7px 7px; min-height: 170px; max-height: 40vh; overflow: auto; padding: 0.6rem; background: #fff; }
.rt-editor:focus { outline: 2px solid #3d92d1; outline-offset: -2px; }

/* ===== Toast ===== */
.flash-toast {
  position: fixed; top: 1rem; right: 1rem; z-index: 3000; padding: 0.7rem 1.1rem;
  border-radius: 8px; font-weight: 500; box-shadow: 0 6px 18px rgba(0,0,0,0.18);
  max-width: 460px; cursor: pointer; transition: opacity 0.3s;
}

/* ===== Login ===== */
.login-card { max-width: 420px; margin: 12vh auto; background: #fff; border: 1px solid var(--line); border-radius: 14px; padding: 2rem; text-align: center; box-shadow: 0 8px 30px rgba(0,0,0,0.06); }
.login-card h1 { margin-bottom: 0.5rem; }

/* ===== Guide ===== */
.guide { max-width: 820px; }
.guide-section { border: 1px solid var(--line); border-radius: 8px; padding: 0.6rem 0.9rem; margin-bottom: 0.7rem; background: #fff; }
.guide-section summary { font-weight: 600; cursor: pointer; }
.guide-section ul { margin: 0.5rem 0; }
.guide-section li { margin: 0.2rem 0; }
