:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --line: #d9dde5;
  --line-soft: #edf0f4;
  --text: #1f2328;
  --muted: #667085;
  --strong: #111827;
  --accent: #b42318;
  --accent-soft: #fff1f0;
  --ok: #157347;
  --warn: #b7791f;
  --info: #245b8f;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 14px;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  min-height: 34px;
  padding: 7px 10px;
  border-radius: 6px;
  cursor: pointer;
}

button:hover {
  border-color: #b9c0cc;
  background: #fafafa;
}

button.primary {
  background: var(--strong);
  border-color: var(--strong);
  color: #fff;
}

button.primary:hover {
  background: #2c3440;
}

button.danger {
  color: var(--accent);
  border-color: #f0b8b1;
  background: var(--accent-soft);
}

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

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  min-height: 34px;
  padding: 7px 9px;
}

textarea {
  min-height: 84px;
  resize: vertical;
}

.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-panel {
  width: min(420px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 24px;
}

.login-panel h1,
.app-title h1 {
  margin: 0;
  color: var(--strong);
  font-size: 22px;
  letter-spacing: 0;
}

.login-panel p,
.muted {
  color: var(--muted);
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto auto 1fr;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.app-title p {
  margin: 4px 0 0;
  color: var(--muted);
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.stats {
  display: grid;
  grid-template-columns: repeat(5, minmax(120px, 1fr));
  gap: 1px;
  background: var(--line);
  border-bottom: 1px solid var(--line);
}

.stat {
  background: var(--surface);
  padding: 12px 16px;
}

.stat .label {
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
}

.stat .value {
  margin-top: 4px;
  color: var(--strong);
  font-size: 22px;
  font-weight: 700;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(620px, 1fr) minmax(380px, 520px);
  gap: 16px;
  padding: 16px;
  min-height: 0;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  min-width: 0;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
}

.panel-header h2 {
  margin: 0;
  font-size: 16px;
}

.toolbar {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) 170px 150px auto;
  gap: 8px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line-soft);
}

.table-wrap {
  overflow: auto;
  max-height: calc(100vh - 235px);
}

table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
}

th,
td {
  padding: 9px 10px;
  border-bottom: 1px solid var(--line-soft);
  text-align: left;
  vertical-align: top;
}

th {
  position: sticky;
  top: 0;
  background: #fbfcfd;
  color: #475467;
  font-size: 12px;
  text-transform: uppercase;
  z-index: 1;
}

tr {
  cursor: pointer;
}

tr:hover,
tr.selected {
  background: #f7fafc;
}

tr.deleted {
  opacity: 0.55;
}

.clip {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.small {
  color: var(--muted);
  font-size: 12px;
  margin-top: 2px;
}

.badge-row {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 20px;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 12px;
  border: 1px solid transparent;
  background: #eef2f6;
  color: #344054;
}

.badge input[type="checkbox"] {
  width: auto;
  min-height: auto;
  margin: 0 5px 0 0;
}

.badge.ok,
.badge.in_force,
.badge.complete {
  background: #e7f6ee;
  color: var(--ok);
}

.badge.warn,
.badge.partial,
.badge.pending_renewal,
.badge.unknown {
  background: #fff7e6;
  color: var(--warn);
}

.badge.minimal,
.badge.lapsed,
.badge.deleted {
  background: var(--accent-soft);
  color: var(--accent);
}

.editor {
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 160px);
}

.editor-body {
  overflow: auto;
  padding: 14px;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.field.full {
  grid-column: 1 / -1;
}

.field label {
  display: block;
  margin-bottom: 5px;
  color: #475467;
  font-size: 12px;
  font-weight: 600;
}

.editor-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  padding: 12px 14px;
  border-top: 1px solid var(--line);
  background: #fbfcfd;
}

.section-title {
  margin: 20px 0 10px;
  font-size: 13px;
  text-transform: uppercase;
  color: #475467;
}

.building {
  border: 1px solid var(--line-soft);
  border-radius: 6px;
  padding: 10px;
  margin-bottom: 8px;
  background: #fcfcfd;
}

.building-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.empty {
  padding: 24px;
  color: var(--muted);
  text-align: center;
}

.error {
  margin-top: 10px;
  color: var(--accent);
  background: var(--accent-soft);
  border: 1px solid #f0b8b1;
  border-radius: 6px;
  padding: 8px 10px;
}

@media (max-width: 1050px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .editor {
    max-height: none;
  }
}

@media (max-width: 760px) {
  .stats,
  .toolbar,
  .form-grid {
    grid-template-columns: 1fr;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }
}

/* ============================================================================
   Multi-tabs (calque vue MVP statique : Aperçu, Échéances, etc.)
   ============================================================================ */

.app-shell {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.panel-nav {
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  padding: 0 16px;
  display: flex;
  gap: 2px;
  overflow-x: auto;
}

.panel-nav button {
  background: transparent;
  border: none;
  border-bottom: 3px solid transparent;
  border-radius: 0;
  padding: 12px 18px;
  font-size: 14px;
  font-weight: 500;
  color: #555;
  cursor: pointer;
  white-space: nowrap;
  min-height: auto;
}

.panel-nav button:hover {
  background: #f3f5f8;
}

.panel-nav button.active {
  color: var(--strong);
  border-bottom-color: var(--accent);
  font-weight: 700;
}

.panel-main {
  padding: 16px;
  flex: 1;
  min-height: 0;
}

.panel-body {
  padding: 16px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 8px;
  margin-bottom: 16px;
}

.stats-grid .stat {
  background: #fbfcfd;
  border: 1px solid var(--line-soft);
  border-left: 3px solid #555;
  border-radius: 6px;
  padding: 10px 12px;
}

.stats-grid .stat .label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: var(--muted);
}

.stats-grid .stat .value {
  margin-top: 4px;
  font-size: 22px;
  font-weight: 700;
  color: var(--strong);
}

.stats-grid .stat.ok { border-left-color: var(--ok); }
.stats-grid .stat.ok .value { color: var(--ok); }
.stats-grid .stat.warn { border-left-color: var(--warn); }
.stats-grid .stat.warn .value { color: var(--warn); }
.stats-grid .stat.urgent { border-left-color: var(--accent); }
.stats-grid .stat.urgent .value { color: var(--accent); }

.apercu-section {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid var(--line-soft);
  font-size: 14px;
  line-height: 1.7;
}

.apercu-section strong {
  display: inline-block;
  margin-right: 8px;
  color: var(--muted);
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

/* Dense tables (calque vue MVP) */
table.dense {
  font-size: 13px;
}

table.dense th,
table.dense td {
  padding: 8px 10px;
}

table.dense th[data-sort] {
  cursor: pointer;
  user-select: none;
}

table.dense th[data-sort]:hover {
  background: #eef2f6;
}

table.dense th .arrow {
  margin-left: 4px;
  font-size: 10px;
}

table.dense th .arrow.muted {
  opacity: 0.35;
}

table.dense th .arrow.active {
  color: var(--accent);
  opacity: 1;
}

table .num {
  text-align: right;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

table .nowrap {
  white-space: nowrap;
}

/* Badges étendus (calque vue MVP) */
.badge.in_force { background: #e7f6ee; color: var(--ok); }
.badge.complete { background: #e7f6ee; color: var(--ok); }
.badge.partial { background: #fff7e6; color: var(--warn); }
.badge.minimal { background: #fbe2e2; color: var(--accent); font-weight: 600; }
.badge.pending_renewal { background: #fbe2e2; color: var(--accent); font-weight: 600; }
.badge.superseded { background: #ecedef; color: #383d41; }
.badge.lapsed { background: #fff7e6; color: var(--warn); }
.badge.cancelled { background: #ecedef; color: #383d41; }
.badge.unknown { background: #fff7e6; color: var(--warn); }
.badge.urgent { background: #fbe2e2; color: var(--accent); font-weight: 700; }
.badge.proche { background: #ffefb8; color: #7a5400; }
.badge.ok { background: #e7f6ee; color: var(--ok); }

.badge.source_partielle { background: #fff7e6; color: var(--warn); }
.badge.statut_a_confirmer { background: #fff7e6; color: var(--warn); }
.badge.historique_a_clarifier { background: #fff7e6; color: var(--warn); }
.badge.prime_a_sourcer { background: #fbe2e2; color: var(--accent); font-weight: 600; }
.badge.pdf_exact_manquant { background: #fbe2e2; color: var(--accent); font-weight: 600; }
.badge.renouvellement_requis { background: #fbe2e2; color: var(--accent); font-weight: 600; }
.badge.historique { background: #ecedef; color: #383d41; }

.badge.haute { background: #fbe2e2; color: var(--accent); font-weight: 600; }
.badge.moyenne { background: #fff7e6; color: var(--warn); }
.badge.basse { background: #ecedef; color: #383d41; }
.badge.confirme_guillaume { background: #e7f6ee; color: var(--ok); }
.badge.a_verifier { background: #fff7e6; color: var(--warn); }
.badge.brouillon { background: #fff7e6; color: var(--warn); }
.badge.inconnu { background: #ecedef; color: #383d41; }

/* Anomalies / priorités */
.anomaly {
  background: #fff7e6;
  border-left: 3px solid var(--warn);
  padding: 10px 14px;
  margin-bottom: 8px;
  border-radius: 4px;
}

.anomaly.crit {
  background: var(--accent-soft);
  border-left-color: var(--accent);
}

.anomaly-title {
  font-weight: 600;
  color: var(--strong);
}

.anomaly-body {
  margin-top: 4px;
  font-size: 13px;
  color: var(--muted);
}

/* Cellule immeubles (collapsible) */
.immeubles-cell {
  min-width: 240px;
  max-width: 360px;
}

.immeubles-empty {
  border: 1px dashed var(--line);
  border-radius: 6px;
  padding: 6px 9px;
  color: var(--muted);
  background: #fafafa;
  font-size: 12px;
}

.immeubles-detail {
  border: 1px solid var(--line-soft);
  border-radius: 6px;
  background: #fbfcfd;
  font-size: 13px;
}

.immeubles-detail[open] {
  background: #fff;
}

.immeubles-detail summary {
  list-style: none;
  padding: 7px 10px;
  cursor: pointer;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 8px;
}

.immeubles-detail summary::-webkit-details-marker {
  display: none;
}

.immeubles-detail summary::before {
  content: "▸";
  color: var(--accent);
  font-weight: 700;
  align-self: start;
  margin-top: 2px;
}

.immeubles-detail[open] summary::before {
  content: "▾";
}

.immeubles-count {
  font-weight: 700;
  color: var(--strong);
  grid-column: 2;
}

.immeubles-preview {
  grid-column: 2;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.3;
}

.immeubles-list {
  border-top: 1px solid var(--line-soft);
  padding: 4px 10px 8px 30px;
}

.immeuble-item {
  padding: 5px 0;
  border-bottom: 1px solid #f0f0f3;
}

.immeuble-item:last-child {
  border-bottom: none;
}

.immeuble-address {
  font-weight: 600;
  line-height: 1.25;
}

.immeuble-meta {
  color: var(--muted);
  font-size: 12px;
  margin-top: 2px;
}

/* Validation table inputs */
.validation-table input,
.validation-table select,
.validation-table textarea {
  width: 100%;
  min-width: 130px;
}

.validation-table textarea {
  min-height: 48px;
  resize: vertical;
}

.validation-grappe {
  min-width: 160px;
}

.validation-address {
  min-width: 220px;
  font-weight: 600;
}

/* Color helpers */
.accent { color: var(--accent); }
.warn { color: var(--warn); }

tr.deleted {
  opacity: 0.5;
}

/* Editor overlay (panneau latéral droit) */
.editor-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(16, 24, 40, 0.45);
  z-index: 50;
}

.editor-pane {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(560px, 100vw);
  background: var(--surface);
  border-left: 1px solid var(--line);
  box-shadow: -2px 0 12px rgba(16, 24, 40, 0.12);
  z-index: 51;
  display: flex;
  flex-direction: column;
}

.editor-pane .panel-header {
  position: relative;
  padding-right: 48px;
}

.close-btn {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  width: 32px;
  height: 32px;
  min-height: auto;
  padding: 0;
  border-radius: 50%;
  background: transparent;
  border: 1px solid transparent;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}

.close-btn:hover {
  background: #f0f0f4;
  border-color: var(--line);
}

@media (max-width: 760px) {
  .editor-pane {
    width: 100vw;
  }
  .panel-nav {
    padding: 0 8px;
  }
  .panel-nav button {
    padding: 10px 12px;
  }
}

.field.readonly input {
  background: #f5f6fa;
  color: #4a5275;
  cursor: not-allowed;
}

.notes-big textarea {
  min-height: 160px;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 12.5px;
  line-height: 1.5;
}

.avenants-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 8px;
}

.avenant-item {
  padding: 10px 12px;
  background: #f9f9fc;
  border: 1px solid var(--line);
  border-radius: 6px;
}

.avenant-head {
  font-size: 13px;
}

.avenant-meta {
  margin-top: 2px;
}

.avenant-source {
  margin-top: 4px;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 11px;
  word-break: break-all;
}
