/* ===== Thème ===== */
:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #f0ede5;
  --border: #e2ddd1;
  --text: #1d2723;
  --muted: #66736d;
  --accent: #1f5c4d;
  --accent-soft: #e3efe9;
  --sidebar-bg: #17352d;
  --sidebar-text: #dfe9e4;
  --sidebar-muted: #8fb0a3;
  --sidebar-hover: #21463c;
  --overdue: #b8382f;
  --overdue-soft: #fbe9e6;
  --soon: #a86400;
  --soon-soft: #fdf1dc;
  --later: #2c7a4b;
  --later-soft: #e5f3ea;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(20, 30, 25, 0.06), 0 4px 14px rgba(20, 30, 25, 0.05);
  --sidebar-width: 264px;
  font-size: 15px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121816;
    --surface: #1a2220;
    --surface-2: #222c29;
    --border: #2e3a36;
    --text: #e6ece9;
    --muted: #9aaba4;
    --accent: #5fbf9f;
    --accent-soft: #1f3a32;
    --sidebar-bg: #0e1513;
    --sidebar-hover: #1b2925;
    --overdue: #ef7a6f;
    --overdue-soft: #3a1f1c;
    --soon: #f0b35a;
    --soon-soft: #372a15;
    --later: #6cc792;
    --later-soft: #183324;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, sans-serif;
  line-height: 1.45;
}
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.2; margin: 0; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.num { font-variant-numeric: tabular-nums; }

/* ===== Layout + volet latéral ===== */
.layout { display: flex; min-height: 100vh; }

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  width: var(--sidebar-width);
  flex-shrink: 0;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  padding: 14px 10px;
  gap: 14px;
  overflow-y: auto;
  transition: width 0.18s ease;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 0 6px; }
.brand { display: flex; align-items: center; gap: 10px; color: #fff; text-decoration: none; font-weight: 700; font-size: 1.1rem; }
.brand-mark {
  display: inline-grid; place-items: center; width: 32px; height: 32px; border-radius: 9px;
  background: #f4efe4; color: #17352d; font-weight: 800;
}
.main-nav { display: flex; flex-direction: column; gap: 2px; }
.main-nav a, .logout-link {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px;
  color: var(--sidebar-text); text-decoration: none; white-space: nowrap;
}
.main-nav a:hover, .logout-link:hover { background: var(--sidebar-hover); }
.main-nav a.active { background: var(--sidebar-hover); color: #fff; font-weight: 600; }
.nav-icon { width: 20px; text-align: center; flex-shrink: 0; }
.logout-link { margin-top: auto; color: var(--sidebar-muted); }

.sidebar-section { display: flex; flex-direction: column; gap: 4px; min-height: 0; }
.sidebar-section-title {
  display: flex; align-items: center; justify-content: space-between; padding: 0 6px 0 10px;
  font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--sidebar-muted);
}
.sidebar .icon-btn { color: var(--sidebar-text); }
.sidebar .icon-btn:hover { background: var(--sidebar-hover); }

.property-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 1px; }
.property-item {
  display: flex; align-items: center; gap: 8px; padding: 6px 6px 6px 10px; border-radius: 8px; cursor: pointer;
}
.property-item:hover { background: var(--sidebar-hover); }
.property-item.active { background: var(--sidebar-hover); }
.property-item.active .property-name { color: #fff; font-weight: 600; }
.property-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; background: var(--sidebar-muted); }
.property-dot.paid { background: #6cc792; }
.property-dot.partial, .property-dot.pending { background: #f0b35a; }
.property-dot.unpaid { background: #ef7a6f; }
.property-dot.vacant { background: transparent; border: 1.5px solid var(--sidebar-muted); }
.property-text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.property-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.property-sub { font-size: 0.75rem; color: var(--sidebar-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rename-btn { opacity: 0; font-size: 0.85rem; }
.property-item:hover .rename-btn, .rename-btn:focus-visible { opacity: 1; }
.rename-input {
  width: 100%; padding: 4px 6px; border-radius: 6px; border: 1px solid var(--sidebar-muted);
  background: #0d1f1a; color: #fff; font: inherit;
}
.sidebar-empty { padding: 6px 10px; color: var(--sidebar-muted); font-size: 0.85rem; }

/* Volet replié (bureau) */
.layout.collapsed .sidebar { width: 64px; }
.layout.collapsed .brand-name,
.layout.collapsed .nav-text,
.layout.collapsed .sidebar-section,
.layout.collapsed .collapse-btn { display: none; }
.layout.collapsed .sidebar-head { justify-content: center; }
.layout.collapsed .main-nav a, .layout.collapsed .logout-link { justify-content: center; }
.expand-btn { display: none; }

.content { flex: 1; min-width: 0; padding: 28px clamp(16px, 3vw, 40px) 60px; }
.topbar { display: none; }
.sidebar-backdrop { display: none; }

@media (max-width: 860px) {
  .sidebar {
    position: fixed; z-index: 30; left: 0; top: 0; transform: translateX(-100%);
    transition: transform 0.2s ease; box-shadow: 0 0 30px rgba(0, 0, 0, 0.3);
  }
  .layout.drawer-open .sidebar { transform: translateX(0); }
  .layout.drawer-open .sidebar-backdrop { display: block; position: fixed; inset: 0; background: rgba(0,0,0,0.35); z-index: 20; }
  .collapse-btn { display: none; }
  .layout.collapsed .sidebar { width: var(--sidebar-width); }
  .layout.collapsed .brand-name, .layout.collapsed .nav-text { display: inline; }
  .layout.collapsed .sidebar-section { display: flex; }
  .rename-btn { opacity: 1; }
  .topbar { display: flex; align-items: center; gap: 10px; margin: -12px 0 16px; }
  .topbar-title { font-weight: 700; }
  .content { padding-top: 24px; }
}

/* ===== Composants ===== */
.icon-btn {
  display: inline-grid; place-items: center; min-width: 28px; height: 28px; padding: 0 6px;
  border: none; border-radius: 7px; background: transparent; color: var(--text);
  cursor: pointer; font-size: 1rem; text-decoration: none;
}
.icon-btn:hover { background: var(--surface-2); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: 9px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 600; font-size: 0.9rem;
  cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-danger { color: var(--overdue); }
.btn-small { padding: 4px 10px; font-size: 0.8rem; }
.btn:disabled { opacity: 0.5; cursor: default; }

.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 22px; }
.page-head p { margin: 4px 0 0; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px;
}
.card + .card { margin-top: 16px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }

.grid { display: grid; gap: 16px; }
.tiles { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); margin-bottom: 24px; }
.tile .tile-label { font-size: 0.8rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.tile .tile-value { font-size: 1.6rem; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.tile .tile-sub { font-size: 0.85rem; color: var(--muted); }
.tile-value.negative { color: var(--overdue); }

.property-cards { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.property-card { cursor: pointer; display: flex; flex-direction: column; gap: 10px; transition: border-color 0.12s; }
.property-card:hover { border-color: var(--accent); }
.property-card h3 { font-size: 1.05rem; }
.property-card .rent { font-size: 1.25rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.property-card .row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }

.badge {
  display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px; border-radius: 999px;
  font-size: 0.78rem; font-weight: 600; white-space: nowrap;
}
.badge.paid, .badge.later { background: var(--later-soft); color: var(--later); }
.badge.partial, .badge.pending, .badge.soon { background: var(--soon-soft); color: var(--soon); }
.badge.unpaid, .badge.overdue { background: var(--overdue-soft); color: var(--overdue); }
.badge.vacant { background: var(--surface-2); color: var(--muted); }

.deadline-chip { border-left: 3px solid var(--muted); padding: 6px 10px; border-radius: 0 8px 8px 0; background: var(--surface-2); font-size: 0.85rem; }
.deadline-chip.overdue { border-color: var(--overdue); background: var(--overdue-soft); }
.deadline-chip.soon { border-color: var(--soon); background: var(--soon-soft); }
.deadline-chip.later { border-color: var(--later); background: var(--later-soft); }

/* Échéancier */
.filters { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 16px; }
.filter-btn { border-radius: 999px; }
.filter-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.timeline { display: flex; flex-direction: column; gap: 10px; }
.deadline-row {
  display: grid; grid-template-columns: 110px 1fr auto; gap: 14px; align-items: start;
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--muted);
  border-radius: 10px; padding: 12px 14px;
}
.deadline-row.overdue { border-left-color: var(--overdue); }
.deadline-row.soon { border-left-color: var(--soon); }
.deadline-row.later { border-left-color: var(--later); }
.deadline-date { font-weight: 700; font-variant-numeric: tabular-nums; }
.deadline-title { font-weight: 600; }
.deadline-detail { font-size: 0.88rem; color: var(--muted); margin-top: 2px; }
.deadline-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
@media (max-width: 640px) {
  .deadline-row { grid-template-columns: 1fr; gap: 6px; }
  .deadline-actions { justify-content: flex-start; }
}

/* Fiche */
.info-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px 20px; }
.info-grid dt { font-size: 0.78rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }
.info-grid dd { margin: 2px 0 0; font-weight: 500; white-space: pre-line; }
.two-cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 16px; align-items: start; }
.two-cols > .card + .card { margin-top: 0; }

table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th { text-align: left; font-weight: 600; color: var(--muted); font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.03em; }
th, td { padding: 8px 6px; border-bottom: 1px solid var(--border); }
td.right, th.right { text-align: right; }
.table-wrap { overflow-x: auto; }

.list-plain { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.list-plain li { display: flex; align-items: center; justify-content: space-between; gap: 10px; }

/* Formulaires */
form .form-section { margin-bottom: 18px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 14px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field.wide { grid-column: 1 / -1; }
.field label { font-size: 0.82rem; font-weight: 600; color: var(--muted); }
.field .hint { font-size: 0.75rem; color: var(--muted); }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
textarea { resize: vertical; min-height: 70px; }
.check { flex-direction: row; align-items: center; gap: 8px; padding-top: 22px; }
.check input { width: auto; }
.check label { font-size: 0.9rem; color: var(--text); font-weight: 500; }
.tenant-row { display: grid; grid-template-columns: 1.2fr 1.2fr 1fr auto; gap: 8px; margin-bottom: 8px; }
@media (max-width: 640px) { .tenant-row { grid-template-columns: 1fr; } }
.inline-form { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-end; }
.inline-form .field { min-width: 120px; flex: 1; }
.form-actions { display: flex; gap: 10px; justify-content: flex-end; position: sticky; bottom: 0; padding: 12px 0; background: linear-gradient(transparent, var(--bg) 30%); }

.letter-text { min-height: 420px; font-family: "SF Mono", Menlo, monospace; font-size: 0.85rem; line-height: 1.55; }
.callout { padding: 10px 14px; border-radius: 10px; background: var(--accent-soft); font-size: 0.88rem; }
.callout.warn { background: var(--soon-soft); }
.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty h2 { color: var(--text); margin-bottom: 8px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(120%);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 10px;
  font-size: 0.9rem; transition: transform 0.2s ease; z-index: 50; max-width: calc(100vw - 32px);
}
.toast.show { transform: translateX(-50%) translateY(0); }
.toast.error { background: var(--overdue); color: #fff; }

/* Connexion */
.login-body { min-height: 100vh; display: grid; place-items: center; background: var(--sidebar-bg); padding: 16px; }
.login-card {
  background: var(--surface); border-radius: 14px; padding: 28px; width: min(320px, 100%);
  display: flex; flex-direction: column; gap: 6px; box-shadow: 0 10px 30px rgba(0,0,0,0.3);
}
.login-card h1 { display: flex; align-items: center; justify-content: center; gap: 10px; font-size: 1.3rem; margin-bottom: 12px; }
.login-card label { font-size: 0.85rem; color: var(--muted); margin-top: 6px; }
.login-card button { margin-top: 14px; }
.login-error { color: var(--overdue); text-align: center; font-size: 0.9rem; margin: 8px 0 0; }

/* Impression : uniquement le courrier */
#print-area { display: none; }
@media print {
  body > *:not(#print-area) { display: none !important; }
  #print-area {
    display: block; white-space: pre-wrap; font-family: Georgia, "Times New Roman", serif;
    font-size: 11.5pt; line-height: 1.55; color: #000;
  }
  @page { margin: 22mm 20mm; }
}
