:root {
  --rubis: #9b1b30;
  --rubis-dark: #6e1222;
  --rubis-light: #fbeef1;
  --bg: #f5f4f7;
  --card: #ffffff;
  --text: #1d1b22;
  --muted: #6b6775;
  --border: #e3e0e8;
  --green: #1a7f4b;
  --green-bg: #e3f5eb;
  --red: #c0262d;
  --red-bg: #fde8e8;
  --orange: #b25e00;
  --orange-bg: #fff1dc;
  --blue: #1f5fbf;
  --blue-bg: #e6efff;
  --radius: 10px;
  --nav-w: 230px;
  --top-h: 58px;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font-family: 'Segoe UI', system-ui, -apple-system, Roboto, Arial, sans-serif;
  background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.45;
}
a { color: var(--rubis); }
h1 { font-size: 24px; margin: 0; }
h2 { font-size: 17px; margin: 0 0 12px; }
h3 { font-size: 15px; margin: 0 0 8px; }
.muted { color: var(--muted); }
.red { color: var(--red); } .green { color: var(--green); }
.right { text-align: right; } .nowrap { white-space: nowrap; }

/* ---- Barre du haut + menu ---- */
.topbar {
  position: fixed; inset: 0 0 auto 0; height: var(--top-h); z-index: 30;
  background: linear-gradient(90deg, var(--rubis-dark), var(--rubis)); color: #fff;
  display: flex; align-items: center; gap: 12px; padding: 0 16px;
  box-shadow: 0 2px 8px rgba(0,0,0,.15);
}
.brand { display: flex; align-items: center; gap: 10px; }
.logo { width: 34px; height: 34px; display: grid; place-items: center; }
.brand-name { font-weight: 700; letter-spacing: .04em; }
.brand-ncc { font-size: 12px; opacity: .85; }
#menu-btn { display: none; color: #fff; }
.nav {
  position: fixed; top: var(--top-h); bottom: 0; left: 0; width: var(--nav-w); z-index: 20;
  background: #fff; border-right: 1px solid var(--border); padding: 14px 10px; overflow-y: auto;
  display: flex; flex-direction: column; gap: 2px;
}
.nav a {
  display: flex; align-items: center; gap: 6px; padding: 10px 12px; border-radius: 8px;
  color: var(--text); text-decoration: none; font-weight: 500;
}
.nav a:hover { background: var(--bg); }
.nav a.active { background: var(--rubis-light); color: var(--rubis); font-weight: 700; }
.nav-badge { margin-left: auto; background: var(--red); color: #fff; border-radius: 10px; font-size: 12px; padding: 0 7px; font-weight: 700; }
.nav-foot { margin-top: auto; font-size: 12px; color: var(--muted); padding: 10px; }
main { margin-left: var(--nav-w); padding: calc(var(--top-h) + 22px) 28px 40px; max-width: calc(1200px + var(--nav-w)); }

/* ---- Blocs ---- */
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.actions.left { justify-content: flex-start; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; margin-bottom: 18px; box-shadow: 0 1px 2px rgba(0,0,0,.03); }
.card-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; flex-wrap: wrap; margin-bottom: 10px; }
.card-head h2 { margin-bottom: 4px; }
.card-head p { margin: 2px 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.grid2 > .card { margin-bottom: 0; }
.grid2 + .card, .grid2 + .alert { margin-top: 18px; }
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 18px; }
.kpi { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; border-top: 4px solid var(--rubis); }
.kpi.good { border-top-color: var(--green); } .kpi.bad { border-top-color: var(--red); }
.kpi-label { font-size: 13px; color: var(--muted); }
.kpi-value { font-size: 21px; font-weight: 700; margin: 4px 0; }
.kpi-sub { font-size: 12px; color: var(--muted); }
.bar { height: 6px; background: var(--bg); border-radius: 3px; overflow: hidden; margin: 4px 0; }
.bar span { display: block; height: 100%; background: var(--green); }
.alert { background: var(--blue-bg); border: 1px solid #c9dafb; color: #173d78; padding: 12px 16px; border-radius: var(--radius); margin-bottom: 18px; }
.alert.warn { background: var(--orange-bg); border-color: #f3d3a3; color: #6b3a00; }
.alert .btn { margin-left: 8px; }
.ok-msg { color: var(--green); font-weight: 600; }
.empty { text-align: center; padding: 30px 10px; color: var(--muted); }
.solde { margin-top: 12px; padding: 10px 14px; border-radius: 8px; }
.solde.bad { background: var(--red-bg); color: var(--red); } .solde.good { background: var(--green-bg); color: var(--green); }
.guide ol.steps { padding-left: 20px; margin: 0; }
.guide ol.steps li { margin-bottom: 14px; }
.guide ol.steps li > div { margin-top: 6px; }

/* ---- Tableaux ---- */
.tbl-wrap { overflow-x: auto; }
.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
.tbl th, .tbl td { padding: 9px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
.tbl thead th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); background: #faf9fb; }
.tbl tfoot th { background: #faf9fb; }
.tbl .num { text-align: right; white-space: nowrap; }
.tbl tr.clickable { cursor: pointer; }
.tbl tbody tr:hover { background: #fcfbfd; }
.tbl td .btn + .btn { margin-left: 4px; }
.filtres { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 6px; }
.filtres .search { margin-bottom: 0; flex: 1 1 240px; }
.filtres select { font: inherit; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: #fff; }
.compteur { font-size: 13px; margin: 6px 0 10px; }
.import-list { margin: 8px 0 0; padding-left: 20px; max-height: 180px; overflow-y: auto; font-size: 13px; }
.import-list.err li { color: var(--red); }
.search { width: 100%; max-width: 360px; padding: 9px 12px; border: 1px solid var(--border); border-radius: 8px; margin-bottom: 12px; font: inherit; }

/* ---- Badges ---- */
.badge { display: inline-block; padding: 2px 9px; border-radius: 20px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge.green { background: var(--green-bg); color: var(--green); }
.badge.red { background: var(--red-bg); color: var(--red); }
.badge.orange { background: var(--orange-bg); color: var(--orange); }
.badge.blue { background: var(--blue-bg); color: var(--blue); }
.badge.grey { background: #eeedf1; color: #55515e; }
.badge.dark { background: #3b3742; color: #fff; }

/* ---- Boutons ---- */
.btn {
  font: inherit; font-weight: 600; font-size: 14px; border: 1px solid var(--border); background: #fff; color: var(--text);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; white-space: nowrap; transition: background .15s, border-color .15s;
}
.btn:hover { border-color: #c9c4d2; background: #faf9fb; }
.btn.primary { background: var(--rubis); border-color: var(--rubis); color: #fff; }
.btn.primary:hover { background: var(--rubis-dark); }
.btn.danger { color: var(--red); }
.btn.danger:hover { background: var(--red-bg); border-color: #f2b8b8; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.icon-btn { background: none; border: 0; font-size: 20px; cursor: pointer; padding: 4px 8px; border-radius: 6px; color: inherit; }

/* ---- Formulaires ---- */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 16px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field.full { grid-column: 1 / -1; }
.field label { font-size: 13px; font-weight: 600; color: #3d3946; }
.field input, .field select, .field textarea {
  font: inherit; padding: 9px 11px; border: 1px solid #d5d1dc; border-radius: 8px; background: #fff; width: 100%;
}
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--rubis-light); border-color: var(--rubis); }
.field.invalid input, .field.invalid select { border-color: var(--red); background: var(--red-bg); }
.field.checkbox label { font-weight: 500; display: flex; gap: 8px; align-items: center; cursor: pointer; }
.field.checkbox input { width: auto; }
.help { color: var(--muted); font-size: 12px; }
.req { color: var(--red); }
.section-title { font-weight: 700; color: var(--rubis); border-bottom: 1px solid var(--border); padding-top: 6px; }
.form-actions { margin-top: 16px; }
label.inline { display: inline-flex; gap: 8px; align-items: center; font-weight: 600; }
label.inline select { font: inherit; padding: 6px 10px; border-radius: 8px; border: 1px solid var(--border); }

/* ---- Fenêtre modale ---- */
.modal-overlay { position: fixed; inset: 0; background: rgba(20,15,30,.45); z-index: 100; display: flex; align-items: flex-start; justify-content: center; padding: 40px 16px; overflow-y: auto; }
.modal { background: #fff; border-radius: 12px; width: 100%; max-width: 560px; box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.modal.wide { max-width: 780px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; }
.modal-body { padding: 18px 20px; }
.modal-body > p { margin-top: 0; }
.modal-body .field.full.checkbox, .modal-body > .field { margin-top: 12px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; padding-top: 14px; border-top: 1px solid var(--border); }

/* ---- Bail : détails & signatures ---- */
.dl { display: grid; grid-template-columns: 130px 1fr; gap: 6px 12px; margin: 0; }
.dl dt { color: var(--muted); font-size: 13px; }
.dl dd { margin: 0; }
.sig-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; margin-top: 12px; }
.sig-box { border: 1px dashed #c9c4d2; border-radius: var(--radius); padding: 12px; background: #fcfbfd; }
.sig-pad { width: 100%; max-width: 600px; aspect-ratio: 3 / 1; background: #fff; border: 1px solid var(--border); border-radius: 8px; touch-action: none; cursor: crosshair; display: block; }
.sig-img { max-width: 100%; max-height: 140px; background: #fff; border: 1px solid var(--border); border-radius: 8px; display: block; margin-bottom: 8px; }
.sig-actions { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.sig-foot { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--border); }

/* ---- Loyers : navigation par mois ---- */
.month-nav { display: flex; align-items: center; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; }
.month-nav input[type=month] { font: inherit; padding: 6px 10px; border: 1px solid var(--border); border-radius: 8px; }
.month-total { margin-left: auto; color: var(--muted); }
.month-total b { color: var(--text); }

/* ---- Graphique ---- */
.chart { display: flex; gap: 6px; align-items: flex-end; height: 170px; padding: 10px 0; border-bottom: 1px solid var(--border); margin-bottom: 6px; }
.chart .col { flex: 1; display: flex; flex-direction: column; align-items: center; height: 100%; }
.chart .bars { flex: 1; width: 100%; display: flex; align-items: flex-end; justify-content: center; gap: 2px; }
.chart .bars span { width: 40%; max-width: 18px; border-radius: 3px 3px 0 0; min-height: 1px; }
.chart small { font-size: 11px; color: var(--muted); margin-top: 4px; }
.b-att, .sw.att { background: #d9d4e0; }
.b-enc, .sw.enc { background: var(--rubis); }
.legend { font-size: 13px; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-left: 8px; }

/* ---- Notifications ---- */
.toast { position: fixed; bottom: 24px; left: 50%; transform: translate(-50%, 20px); opacity: 0; transition: .25s; z-index: 200;
  background: #1f1b26; color: #fff; padding: 12px 20px; border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.25); max-width: 90vw; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--red); }

/* ---- Mode en ligne : utilisateur, connexion ---- */
.user-box { margin-left: auto; display: flex; align-items: center; gap: 12px; font-size: 13px; }
.user-box .logout { background: rgba(255,255,255,.15); color: #fff; border: 1px solid rgba(255,255,255,.35); border-radius: 6px; padding: 5px 10px; cursor: pointer; font: inherit; }
.user-box .logout:hover { background: rgba(255,255,255,.28); }
.save-status { opacity: .9; }
.save-status.err { background: #fff; color: var(--red); padding: 2px 8px; border-radius: 6px; font-weight: 600; }
body.login-mode .nav, body.login-mode #menu-btn { display: none; }
body.login-mode main { margin-left: 0; max-width: none; }
.login-box { max-width: 400px; margin: 40px auto; text-align: center; padding: 30px; }
.login-box form { text-align: left; display: flex; flex-direction: column; gap: 12px; margin-top: 16px; }
.login-logo { width: 64px; height: 64px; margin: 0 auto 12px; display: grid; place-items: center; }
.login-btn { width: 100%; padding: 11px; margin-top: 6px; }
body.lecture-seule [data-action^="add"], body.lecture-seule [data-action^="edit"], body.lecture-seule [data-action^="del"],
body.lecture-seule [data-action="encaisser"], body.lecture-seule [data-action="encaisserChoix"], body.lecture-seule [data-action="resilier"],
body.lecture-seule [data-action="saveSig"], body.lecture-seule [data-action="signPaper"] { display: none; }

/* ---- Espace locataire ---- */
body.espace-locataire main { margin: 0 auto; max-width: 980px; padding-top: 20px; }
body.espace-locataire .topbar { position: sticky; }
.tabs { display: flex; gap: 4px; justify-content: center; background: #fff; border-bottom: 1px solid var(--border); padding: 0 12px; position: sticky; top: var(--top-h); z-index: 10; overflow-x: auto; }
.tabs a { padding: 13px 16px; text-decoration: none; color: var(--text); font-weight: 600; border-bottom: 3px solid transparent; white-space: nowrap; }
.tabs a.active { color: var(--rubis); border-bottom-color: var(--rubis); }
.apercu-banner { background: #fff6d6; border-bottom: 1px solid #f0dc94; color: #5b4500; padding: 10px 16px; text-align: center; font-size: 14px; }
.kpis.kpis-3 { grid-template-columns: repeat(3, 1fr); }
.kpi-value.small-v { font-size: 18px; text-transform: capitalize; }
.sig-zone { margin-top: 18px; padding: 16px; border: 2px dashed var(--rubis); border-radius: var(--radius); background: var(--rubis-light); }
.sig-zone .sig-pad { margin-top: 10px; }
.accept { display: flex; gap: 10px; align-items: flex-start; font-weight: 600; cursor: pointer; margin: 10px 0; }
.accept input { margin-top: 4px; width: 18px; height: 18px; }
.small-steps { margin: 6px 0; padding-left: 20px; font-size: 14px; }
p.small { font-size: 13px; margin-top: 14px; }
.acces-cell .btn { margin: 2px 0; }
.cred { background: var(--bg); border-radius: 8px; padding: 12px 14px; margin: 10px 0; line-height: 1.9; }
.cred .mdp { font-family: Consolas, monospace; font-size: 17px; letter-spacing: .05em; color: var(--rubis); }
.modal-body label.field > span { font-size: 13px; font-weight: 600; }
.modal-body .actions.left { margin-top: 10px; }
a.btn { text-decoration: none; display: inline-block; }

/* ---- Visionneuse de documents (contrat, quittance, relevé) ---- */
.doc-viewer { position: fixed; inset: 0; z-index: 300; background: #3b3742; display: flex; flex-direction: column; }
.doc-bar { display: flex; align-items: center; gap: 8px; padding: 10px 14px; background: var(--rubis-dark); color: #fff; flex-wrap: wrap; }
.doc-bar .doc-titre { margin-right: auto; font-size: 15px; }
.doc-bar .doc-info { font-size: 13px; opacity: .9; }
.doc-bar .btn:not(.primary) { background: rgba(255,255,255,.15); color: #fff; border-color: rgba(255,255,255,.35); }
.doc-bar .btn.primary { background: #fff; color: var(--rubis); border-color: #fff; }
.doc-astuce { margin: 0; padding: 6px 14px; font-size: 12px; color: #e9e4ee; background: #4a4552; }
.doc-frame { flex: 1; width: 100%; border: 0; background: #e9e9ee; }
body.doc-ouvert { overflow: hidden; }

/* ---- Compte Agence : menus et actions limités ---- */
body.role-agence .nav a[data-page="loyers"], body.role-agence .nav a[data-page="demandes"],
body.role-agence .nav a[data-page="depenses"], body.role-agence .nav a[data-page="rapports"],
body.role-agence .col-sci,
body.role-agence [data-action="addImmeuble"], body.role-agence [data-action="editImmeuble"], body.role-agence [data-action="delImmeuble"],
body.role-agence [data-action="addLot"], body.role-agence [data-action="editLot"], body.role-agence [data-action="delLot"],
body.role-agence [data-action="importExcel"], body.role-agence [data-action="encaisserChoix"] { display: none !important; }
body.role-agence .nav a[data-page="parametres"]::after { content: ""; }

/* ---- États des lieux ---- */
.edl-pieces { display: flex; flex-direction: column; gap: 12px; }
.edl-piece { border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; background: #fcfbfd; }
.edl-piece-head { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 6px; }
.edl-piece-head .edl-nom { font: inherit; font-weight: 700; padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; flex: 1 1 180px; }
.edl-piece-head select { font: inherit; padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; }
.edl-piece textarea { font: inherit; width: 100%; padding: 8px; border: 1px solid #d5d1dc; border-radius: 6px; }
.edl-piece .photos { margin: 6px 0 0; }
.edl-piece .ph { position: relative; display: inline-block; }
.edl-piece .ph img { width: 90px; height: 90px; }
.ph-x { position: absolute; top: 2px; right: 2px; background: rgba(0,0,0,.6); color: #fff; border: 0; border-radius: 50%; width: 22px; height: 22px; cursor: pointer; font-size: 12px; }
.ph-add { width: 90px; height: 90px; border: 2px dashed #c9c4d2; border-radius: 8px; background: #fff; cursor: pointer; color: var(--muted); font: inherit; font-size: 13px; }
.ph-add:hover { border-color: var(--rubis); color: var(--rubis); }
.edl-form input:disabled, .edl-piece :disabled, textarea:disabled { background: #f3f2f6; color: #444; }

/* ---- Demandes (pannes, questions) ---- */
.fil { display: flex; flex-direction: column; gap: 10px; margin: 6px 0 12px; }
.msg { max-width: 85%; padding: 10px 14px; border-radius: 12px; }
.msg.moi { align-self: flex-end; background: var(--rubis-light); border: 1px solid #f0cfd6; }
.msg.autre { align-self: flex-start; background: var(--bg); border: 1px solid var(--border); }
.msg-qui { font-size: 12px; color: var(--muted); margin-bottom: 3px; }
.msg-txt { white-space: normal; word-wrap: break-word; }
.photos { display: flex; gap: 8px; flex-wrap: wrap; margin: 8px 0; }
.photos img { width: 110px; height: 110px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); }
.repondre { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.repondre textarea, .note-interne { font: inherit; width: 100%; padding: 10px; border: 1px solid #d5d1dc; border-radius: 8px; }
.repondre-bas { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.repondre .btn { align-self: flex-end; }
.liste-demandes { display: flex; flex-direction: column; }
.demande-ligne { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 4px; border-bottom: 1px solid var(--border); color: inherit; text-decoration: none; }
.demande-ligne:hover { background: #fcfbfd; }
.tbl tr.non-lu td { background: #fff8f9; }

/* ---- Mobile / tablette ---- */
@media (max-width: 1000px) {
  .kpis { grid-template-columns: 1fr 1fr; }
  .grid2, .sig-grid { grid-template-columns: 1fr; }
  .grid2 > .card { margin-bottom: 0; }
}
@media (max-width: 800px) {
  #menu-btn { display: block; }
  .nav { transform: translateX(-100%); transition: transform .2s; box-shadow: 4px 0 20px rgba(0,0,0,.15); }
  body.nav-open .nav { transform: none; }
  main { margin-left: 0; padding: calc(var(--top-h) + 16px) 16px 30px; }
  .form-grid { grid-template-columns: 1fr; }
  .page-head .actions { justify-content: flex-start; }
  h1 { font-size: 21px; }
  .dl { grid-template-columns: 100px 1fr; }
  .user-box .user-name { display: none; }
}
@media (max-width: 800px) {
  .kpis.kpis-3 { grid-template-columns: 1fr; }
  body.espace-locataire main { padding: 16px; }
  .tabs { justify-content: flex-start; }
}
@media (max-width: 480px) {
  .kpis { grid-template-columns: 1fr; }
  .month-total { margin-left: 0; width: 100%; }
}
