:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #dfe3ea;
  --border-strong: #c5ccd8;
  --text: #1d2733;
  --muted: #667085;
  --primary: #1f5fa8;
  --primary-hover: #184d8a;
  --primary-soft: #e8f0fa;
  --danger: #c92a2a;
  --danger-soft: #fdecec;
  --warning: #b35c00;
  --warning-soft: #fff3e0;
  --success: #2b8a3e;
  --success-soft: #e9f7ec;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, .18);
  --topbar-h: 56px;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  font-size: 14px;
  color: var(--text);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; font-weight: 600; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }

/* ---------- Barre du haut ---------- */
.topbar {
  position: sticky; top: 0; z-index: 1000;
  height: var(--topbar-h); display: flex; align-items: center; gap: 20px;
  padding: 0 16px; background: #13355e; color: #fff;
}
.brand { display: flex; align-items: center; gap: 10px; color: #fff; font-weight: 600; font-size: 16px; white-space: nowrap; }
.brand:hover { text-decoration: none; }
.brand svg { width: 28px; height: 28px; }
.brand svg rect { fill: #3b82d6; }
.brand svg path { fill: #fff; }
.nav { display: flex; gap: 2px; flex: 1; }
.nav a {
  color: #c9d7ea; padding: 8px 12px; border-radius: 6px; white-space: nowrap; display: flex; align-items: center; gap: 6px;
}
.nav a:hover { background: rgba(255, 255, 255, .08); color: #fff; text-decoration: none; }
.nav a.active { background: rgba(255, 255, 255, .16); color: #fff; }
.nav-toggle { display: none; background: none; border: 0; color: #fff; font-size: 22px; cursor: pointer; }
.badge {
  background: var(--danger); color: #fff; border-radius: 10px; padding: 0 7px; font-size: 11px; font-weight: 600; line-height: 18px;
}
.user-menu { position: relative; }
.user-btn {
  background: rgba(255, 255, 255, .1); border: 0; color: #fff; padding: 7px 12px; border-radius: 6px; cursor: pointer; font: inherit;
}
.dropdown {
  position: absolute; right: 0; top: calc(100% + 6px); background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-lg); min-width: 220px; padding: 4px; z-index: 2000;
}
.dropdown button {
  display: block; width: 100%; text-align: left; padding: 9px 12px; border: 0; background: none; font: inherit; color: var(--text);
  border-radius: 6px; cursor: pointer;
}
.dropdown button:hover { background: var(--surface-2); }

@media (max-width: 960px) {
  .nav-toggle { display: block; order: 3; }
  .nav {
    display: none; position: absolute; top: var(--topbar-h); left: 0; right: 0; flex-direction: column;
    background: #13355e; padding: 8px; box-shadow: var(--shadow-lg);
  }
  .nav.open { display: flex; }
  .user-menu { margin-left: auto; }
}

/* ---------- Mise en page ---------- */
#main { min-height: calc(100vh - var(--topbar-h)); }
.page { max-width: 1280px; margin: 0 auto; padding: 24px 16px 48px; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head h1 { flex: 1; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.card-pad { padding: 16px; }
.section { margin-bottom: 20px; }
.section-head { display: flex; align-items: center; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.section-head h2, .section-head h3 { flex: 1; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 20px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
@media (max-width: 900px) { .grid-2, .grid-3 { grid-template-columns: 1fr; } }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.spacer { flex: 1; }
.empty { padding: 28px 16px; text-align: center; color: var(--muted); }

/* ---------- Boutons et champs ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; justify-content: center;
  padding: 7px 14px; border-radius: 6px; border: 1px solid var(--border-strong); background: var(--surface);
  color: var(--text); font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap; line-height: 1.4;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { color: var(--danger); border-color: #f0b4b4; }
.btn-danger:hover { background: var(--danger-soft); }
.btn-sm { padding: 4px 10px; font-size: 13px; }
.btn-icon { padding: 4px 8px; border: 0; background: none; color: var(--muted); }
.btn-icon:hover { color: var(--text); background: var(--surface-2); }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: 6px; padding: 7px 10px; width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible {
  outline: 2px solid rgba(31, 95, 168, .35); outline-offset: 1px; border-color: var(--primary);
}
input[type=checkbox] { width: 16px; height: 16px; accent-color: var(--primary); }
input[type=radio] { width: 14px; height: 14px; padding: 0; vertical-align: -2px; accent-color: var(--primary); }
input[type=color] { padding: 2px; height: 36px; }
textarea { min-height: 72px; resize: vertical; }

.form fieldset { border: 0; padding: 0; margin: 0 0 22px; }
.form legend { font-weight: 600; font-size: 15px; margin-bottom: 10px; padding-bottom: 6px; border-bottom: 1px solid var(--border); width: 100%; }
.fields { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px 16px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { font-size: 13px; font-weight: 500; color: #344054; }
.field small { color: var(--muted); font-size: 12px; }
.field.full { grid-column: 1 / -1; }
.field.check { flex-direction: row; align-items: center; gap: 8px; padding-top: 20px; }
.input-unit { display: flex; align-items: center; gap: 6px; }
.input-unit em { font-style: normal; color: var(--muted); font-size: 13px; }
.form-error { background: var(--danger-soft); color: var(--danger); padding: 8px 12px; border-radius: 6px; margin-bottom: 12px; }

/* ---------- Tableaux ---------- */
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; }
table.data th, table.data td { padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
table.data th { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; background: var(--surface-2); white-space: nowrap; }
table.data th.sortable { cursor: pointer; user-select: none; }
table.data th.sortable:hover { color: var(--text); }
table.data tbody tr:hover { background: #fafbfd; }
table.data tr.clickable { cursor: pointer; }
table.data td.actions { text-align: right; white-space: nowrap; width: 1%; }
table.data td.num, table.data th.num { text-align: right; white-space: nowrap; }
table.data tfoot td { font-weight: 600; background: var(--surface-2); }
.pre { white-space: pre-wrap; }

.tag {
  display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px; border-radius: 10px; font-size: 12px; font-weight: 500;
  background: var(--surface-2); border: 1px solid var(--border); white-space: nowrap;
}
.tag.danger { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.tag.warning { background: var(--warning-soft); color: var(--warning); border-color: transparent; }
.tag.success { background: var(--success-soft); color: var(--success); border-color: transparent; }
.tag.info { background: var(--primary-soft); color: var(--primary); border-color: transparent; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex: none; border: 1px solid rgba(0, 0, 0, .15); }
.dpe { display: inline-block; width: 24px; text-align: center; border-radius: 4px; color: #fff; font-weight: 700; }

/* ---------- Carte ---------- */
.carte { display: grid; grid-template-columns: 340px 1fr; height: calc(100vh - var(--topbar-h)); }
.carte-side { display: flex; flex-direction: column; background: var(--surface); border-right: 1px solid var(--border); min-height: 0; }
.carte-filters { padding: 12px; display: grid; gap: 8px; border-bottom: 1px solid var(--border); }
.carte-filters .row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.carte-count { padding: 8px 12px; font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--border); display: flex; align-items: center; }
.carte-list { overflow-y: auto; flex: 1; }
.carte-item { display: flex; gap: 10px; padding: 10px 12px; border-bottom: 1px solid #eef0f4; cursor: pointer; align-items: flex-start; }
.carte-item:hover, .carte-item.active { background: var(--primary-soft); }
.carte-item .dot { margin-top: 4px; }
.carte-item .t { font-weight: 600; }
.carte-item .s { font-size: 12px; color: var(--muted); }
.carte-item .open { margin-left: auto; align-self: center; }
#map { height: 100%; min-height: 300px; }
.leaflet-container { font: inherit; }
.map-legend { background: #fff; padding: 8px 10px; border-radius: 6px; box-shadow: var(--shadow); font-size: 12px; max-height: 260px; overflow-y: auto; }
.map-legend div { display: flex; align-items: center; gap: 6px; padding: 1px 0; }
.popup { min-width: 220px; }
.popup img { width: 100%; height: 110px; object-fit: cover; border-radius: 6px; margin-bottom: 6px; display: block; }
.popup .t { font-weight: 600; font-size: 14px; }
.popup .s { color: var(--muted); font-size: 12px; margin: 2px 0 8px; }
.popup .btn { width: 100%; color: #fff; }
@media (max-width: 800px) {
  .carte { grid-template-columns: 1fr; grid-template-rows: auto 55vh; height: auto; }
  .carte-list { max-height: 30vh; }
}

/* ---------- Fiche bâtiment ---------- */
.fiche-head { display: flex; gap: 18px; align-items: flex-start; margin-bottom: 16px; flex-wrap: wrap; }
.fiche-photo { width: 140px; height: 100px; border-radius: var(--radius); object-fit: cover; background: var(--surface-2); border: 1px solid var(--border); flex: none; }
.fiche-photo.placeholder { display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 12px; }
.fiche-title { flex: 1; min-width: 240px; }
.fiche-title h1 { margin-bottom: 6px; }
.fiche-title .meta { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; color: var(--muted); }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin-bottom: 18px; overflow-x: auto; }
.tabs a { padding: 10px 14px; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; font-weight: 500; }
.tabs a:hover { color: var(--text); text-decoration: none; }
.tabs a.active { color: var(--primary); border-bottom-color: var(--primary); }
.tabs a .count { font-size: 11px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 0 6px; margin-left: 4px; }
.kv { display: grid; grid-template-columns: minmax(140px, 40%) 1fr; gap: 6px 14px; font-size: 13.5px; margin: 0; }
.card-pad > p:only-child { margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.mini-map { height: 260px; border-radius: var(--radius); overflow: hidden; border: 1px solid var(--border); }
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.stat .v { font-size: 20px; font-weight: 600; }
.stat .l { font-size: 12px; color: var(--muted); }
.geo-results { border: 1px solid var(--border); border-radius: 6px; margin-top: 6px; background: var(--surface); }
.geo-results button { display: block; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--border); background: none; padding: 8px 10px; cursor: pointer; font: inherit; }
.geo-results button:hover { background: var(--primary-soft); }

/* Documents */
.docs { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 14px; padding: 16px; }
.doc { border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; background: var(--surface); display: flex; flex-direction: column; }
.doc-thumb { height: 120px; background: var(--surface-2); display: flex; align-items: center; justify-content: center; cursor: pointer; overflow: hidden; }
.doc-thumb img { width: 100%; height: 100%; object-fit: cover; }
.doc-thumb .ext { font-weight: 700; color: var(--muted); font-size: 18px; border: 2px solid var(--border-strong); border-radius: 6px; padding: 10px 8px; }
.doc-body { padding: 8px 10px; flex: 1; }
.doc-body .t { font-weight: 600; font-size: 13px; overflow-wrap: anywhere; }
.doc-body .s { font-size: 12px; color: var(--muted); }
.doc-actions { display: flex; border-top: 1px solid var(--border); }
.doc-actions .btn-icon { flex: 1; border-radius: 0; font-size: 12px; }
.dropzone {
  border: 2px dashed var(--border-strong); border-radius: var(--radius); padding: 18px; text-align: center; color: var(--muted);
  transition: background .15s, border-color .15s;
}
.dropzone.over { background: var(--primary-soft); border-color: var(--primary); }
.pending { margin-top: 12px; display: grid; gap: 10px; }
.pending-files { display: flex; flex-wrap: wrap; gap: 6px; }
.pending-files .btn-icon { padding: 0 2px; font-size: 11px; }

/* Graphique */
.chart { width: 100%; height: 220px; display: block; }
.chart text { font-size: 11px; fill: var(--muted); }
.chart .bar { fill: var(--primary); }
.chart .bar.prev { fill: #b8cde6; }
.legend-inline { display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.legend-inline span { display: inline-flex; align-items: center; gap: 5px; }
.legend-inline i { width: 12px; height: 12px; border-radius: 2px; display: inline-block; }

/* Échéances */
.ech-group h2 { display: flex; align-items: center; gap: 8px; }
.jours { font-weight: 600; white-space: nowrap; }
.jours.retard { color: var(--danger); }
.jours.proche { color: var(--warning); }
.jours.avenir { color: var(--primary); }

/* Imports */
.import-card .cols { max-height: 0; overflow: hidden; }
.import-card details summary { cursor: pointer; color: var(--primary); padding: 4px 0; }
.report { margin-top: 12px; padding: 12px; border-radius: 6px; background: var(--surface-2); border: 1px solid var(--border); }
.report ul { margin: 6px 0 0; padding-left: 18px; max-height: 200px; overflow-y: auto; }

/* ---------- Modale, toasts, connexion ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(16, 24, 40, .45); z-index: 3000; display: flex; align-items: flex-start; justify-content: center;
  padding: 5vh 16px; overflow-y: auto;
}
.modal { background: var(--surface); border-radius: 10px; box-shadow: var(--shadow-lg); width: 100%; max-width: 720px; display: flex; flex-direction: column; }
.modal.wide { max-width: 1000px; }
.modal-head { display: flex; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.modal-head h2 { flex: 1; }
.modal-body { padding: 18px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--border); background: var(--surface-2); border-radius: 0 0 10px 10px; }
.viewer img { max-width: 100%; display: block; margin: 0 auto; }
.viewer iframe { width: 100%; height: 75vh; border: 0; }

.toasts { position: fixed; right: 16px; bottom: 16px; z-index: 4000; display: flex; flex-direction: column; gap: 8px; }
.toast { background: #1d2733; color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow-lg); max-width: 380px; animation: pop .2s ease-out; }
.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } }

.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; background: linear-gradient(160deg, #13355e, #1f5fa8); }
.login { width: 100%; max-width: 380px; padding: 28px; }
.login h1 { margin-bottom: 4px; }
.login p { margin: 0 0 18px; }
.login .field { margin-bottom: 12px; }
.login .btn { width: 100%; margin-top: 6px; padding: 10px; }
.loading { padding: 40px; text-align: center; color: var(--muted); }
