:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e3e8ef;
  --text: #1b2433;
  --muted: #667085;
  --primary: #15803d;
  --primary-ink: #ffffff;
  --primary-soft: #e3f6ea;
  --danger: #dc2626;
  --sidebar: #0f2a1d;
  --sidebar-text: #c9d3e6;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 16px rgba(16, 24, 40, .06);

  --b-gray-bg: #eef1f5; --b-gray: #475467;
  --b-blue-bg: #e6efff; --b-blue: #1d4ed8;
  --b-green-bg: #e3f6ea; --b-green: #15803d;
  --b-amber-bg: #fdf3dc; --b-amber: #a15c07;
  --b-orange-bg: #ffeadb; --b-orange: #c2410c;
  --b-red-bg: #fde4e4; --b-red: #b91c1c;
  --b-purple-bg: #f0e8fd; --b-purple: #7e22ce;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1420; --surface: #171d2b; --surface-2: #1c2333; --border: #2a3346;
    --text: #e6eaf2; --muted: #98a2b3; --primary: #4ade80; --primary-ink: #052e16; --primary-soft: #163a26;
    --sidebar: #0a1610; --shadow: 0 1px 2px rgba(0,0,0,.4);
    --b-gray-bg: #273041; --b-gray: #c2cad6;
    --b-blue-bg: #1c2d52; --b-blue: #93b4ff;
    --b-green-bg: #163a26; --b-green: #7ddc9d;
    --b-amber-bg: #3d2f12; --b-amber: #f3c46b;
    --b-orange-bg: #43260f; --b-orange: #fdac74;
    --b-red-bg: #451a1a; --b-red: #fca5a5;
    --b-purple-bg: #33204d; --b-purple: #d3b1fb;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--text);
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }

/* ---------- Layout ---------- */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 240px; flex-shrink: 0; background: var(--sidebar); color: var(--sidebar-text);
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 18px 18px 14px; color: #fff; font-weight: 700; font-size: 15px; }
.brand svg { color: #4ade80; }
#nav { flex: 1; overflow-y: auto; padding: 4px 10px; }
#nav a {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px;
  color: var(--sidebar-text); font-size: 13.5px;
}
#nav a:hover { background: rgba(255,255,255,.06); text-decoration: none; }
#nav a.active { background: rgba(74,222,128,.16); color: #fff; }
#nav .ico { width: 20px; text-align: center; }
#nav .sep { margin: 12px 10px 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; opacity: .55; }
.sidebar-foot { padding: 12px; border-top: 1px solid rgba(255,255,255,.08); display: flex; flex-wrap: wrap; gap: 6px; }
.me { width: 100%; font-size: 12.5px; margin-bottom: 4px; }
.me strong { color: #fff; display: block; }
.sidebar .btn.ghost { color: var(--sidebar-text); border-color: rgba(255,255,255,.15); }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 14px 24px; background: var(--surface);
  border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20;
}
.topbar h1 { font-size: 18px; margin: 0; flex: 1; }
.topbar-actions { position: relative; }
.view { padding: 22px 24px 60px; max-width: 1400px; width: 100%; }
.menu-btn { display: none; }

@media (max-width: 860px) {
  .sidebar { position: fixed; left: -260px; z-index: 50; transition: left .2s; }
  .sidebar.open { left: 0; box-shadow: 0 0 40px rgba(0,0,0,.4); }
  .menu-btn { display: inline-flex; }
  .view { padding: 16px; }
  .topbar { padding: 12px 16px; }
}

/* ---------- Controls ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; }
.btn.small { padding: 4px 10px; font-size: 12.5px; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { background: none; border: 0; color: var(--text); cursor: pointer; padding: 6px; border-radius: 8px; font-size: 18px; position: relative; display: inline-flex; }
.icon-btn:hover { background: var(--surface-2); }

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(--primary-soft); border-color: var(--primary); }
textarea { min-height: 80px; resize: vertical; }
label { display: block; font-size: 12.5px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }

.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 16px; }
.toolbar input[type=search] { max-width: 280px; }
.toolbar select, .toolbar input[type=month] { width: auto; }
.toolbar .spacer { flex: 1; }

/* ---------- Cards & tables ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: 18px; }
.card h2, .card h3 { margin: 0 0 12px; font-size: 15px; }
.grid { display: grid; gap: 16px; }
.grid.stats { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); margin-bottom: 18px; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); }
.stat { padding: 16px 18px; }
.stat .label { color: var(--muted); font-size: 12.5px; }
.stat .value { font-size: 24px; font-weight: 700; margin-top: 2px; }
.stat .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.progress { height: 6px; background: var(--surface-2); border-radius: 4px; overflow: hidden; margin-top: 8px; border: 1px solid var(--border); }
.progress > div { height: 100%; background: var(--primary); }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; background: var(--surface-2); white-space: nowrap; }
tbody tr:hover { background: var(--surface-2); }
tbody tr.clickable { cursor: pointer; }
tbody tr.highlight { background: var(--primary-soft); }
td.actions { white-space: nowrap; text-align: right; }
td.actions .btn { margin-left: 4px; }
.empty { padding: 36px; text-align: center; color: var(--muted); }

.list-compact { list-style: none; margin: 0; padding: 0; }
.list-compact li { padding: 9px 0; border-bottom: 1px solid var(--border); display: flex; gap: 10px; align-items: center; justify-content: space-between; }
.list-compact li:last-child { border-bottom: 0; }

.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; background: var(--b-gray-bg); color: var(--b-gray); }
.badge.blue { background: var(--b-blue-bg); color: var(--b-blue); }
.badge.green { background: var(--b-green-bg); color: var(--b-green); }
.badge.amber { background: var(--b-amber-bg); color: var(--b-amber); }
.badge.orange { background: var(--b-orange-bg); color: var(--b-orange); }
.badge.red { background: var(--b-red-bg); color: var(--b-red); }
.badge.purple { background: var(--b-purple-bg); color: var(--b-purple); }

/* ---------- Detail (immeuble) ---------- */
.detail-head { display: flex; flex-wrap: wrap; align-items: flex-start; gap: 12px; margin-bottom: 16px; }
.detail-head h2 { margin: 0; font-size: 22px; }
.detail-head .spacer { flex: 1; }
.dl { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 12px 20px; }
.dl div span { display: block; font-size: 12px; color: var(--muted); }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin: 20px 0 16px; overflow-x: auto; }
.tabs button { background: none; border: 0; padding: 10px 14px; font: inherit; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs button.active { color: var(--primary); border-color: var(--primary); font-weight: 600; }

.dropzone {
  border: 2px dashed var(--border); border-radius: var(--radius); padding: 22px; text-align: center;
  color: var(--muted); margin-bottom: 14px; background: var(--surface);
}
.dropzone.over { border-color: var(--primary); background: var(--primary-soft); color: var(--primary); }
.dropzone .row { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; align-items: center; margin-top: 10px; }
.dropzone select { width: auto; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(10, 15, 30, .5); display: flex;
  align-items: flex-start; justify-content: center; padding: 40px 16px; overflow-y: auto; z-index: 100;
}
.modal { width: 100%; max-width: 720px; background: var(--surface); border-radius: 12px; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal.narrow { max-width: 440px; }
.modal-head { display: flex; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; font-size: 17px; flex: 1; }
.modal-body { padding: 20px; }
.modal-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 14px 20px; border-top: 1px solid var(--border); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px 16px; }
.form-grid .full { grid-column: 1 / -1; }
.form-section { grid-column: 1 / -1; font-weight: 700; font-size: 13px; margin-top: 6px; padding-top: 10px; border-top: 1px solid var(--border); }
.form-section:first-child { border-top: 0; padding-top: 0; margin-top: 0; }
.hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
.error-msg { color: var(--danger); font-size: 13px; margin-top: 10px; }
@media (max-width: 600px) { .form-grid { grid-template-columns: 1fr; } }


/* ---------- Auth ---------- */
.auth { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.auth .card { width: 100%; max-width: 400px; padding: 28px; }
.auth h1 { margin: 0 0 4px; font-size: 22px; }
.auth form > div { margin-bottom: 14px; }
.auth .btn { width: 100%; justify-content: center; padding: 10px; }

.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); background: #1b2433; color: #fff;
  padding: 10px 18px; border-radius: 8px; box-shadow: 0 8px 30px rgba(0,0,0,.25); z-index: 200; font-size: 13.5px;
}
.toast.error { background: var(--danger); }
a.card { color: inherit; text-decoration: none; }
a.card:hover { border-color: var(--primary); text-decoration: none; }
.toolbar input[type=date] { width: auto; }
details.card > summary { list-style-position: inside; }

/* ---------- Budget familial ---------- */
:root { --pos: #15803d; --neg: #dc2626; --c-reer: #2563eb; --c-celi: #16a34a; --c-nr: #f59e0b; --c-rrq: #0891b2; --c-psv: #7c3aed; --c-pension: #db2777; --c-emploi: #64748b; --c-imp: #dc2626; }
@media (prefers-color-scheme: dark) { :root { --pos: #4ade80; --neg: #f87171; --c-reer: #60a5fa; --c-celi: #4ade80; --c-nr: #fbbf24; --c-rrq: #22d3ee; --c-psv: #a78bfa; --c-pension: #f472b6; --c-emploi: #94a3b8; --c-imp: #f87171; } }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
th.num { text-align: right; }
tfoot td { font-weight: 700; background: var(--surface-2); }
.periode { display: flex; align-items: center; gap: 4px; }
.periode input, .periode select { width: auto; padding: 6px 8px; }
.periode .icon-btn { font-size: 16px; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: middle; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; border: 1px solid var(--border); }
.cat-ico { width: 22px; display: inline-block; text-align: center; }
.bar-line { display: grid; grid-template-columns: minmax(120px, 1.3fr) 2fr auto; gap: 10px; align-items: center; padding: 7px 0; border-bottom: 1px solid var(--border); }
.bar-line:last-child { border-bottom: 0; }
.bar-track { height: 10px; border-radius: 6px; background: var(--surface-2); border: 1px solid var(--border); overflow: hidden; position: relative; }
.bar-track > div { height: 100%; border-radius: 6px; }
.bar-track.over > div { background: var(--neg) !important; }
.banner { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 12px 16px; border-radius: var(--radius); background: var(--b-amber-bg); color: var(--b-amber); margin-bottom: 16px; }
.banner .spacer { flex: 1; }
.banner.info { background: var(--b-blue-bg); color: var(--b-blue); }
.banner.ok { background: var(--b-green-bg); color: var(--b-green); }
.banner.bad { background: var(--b-red-bg); color: var(--b-red); }
.chart { width: 100%; height: auto; display: block; }
.chart text { fill: var(--muted); font-size: 11px; font-family: inherit; }
.chart .grid-l { stroke: var(--border); stroke-width: 1; }
.chart .hit { fill: transparent; }
.chart .hit:hover { fill: var(--text); fill-opacity: .05; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 12.5px; color: var(--muted); margin-top: 8px; }
.tip { position: fixed; z-index: 300; pointer-events: none; background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 8px 30px rgba(0,0,0,.18); padding: 8px 10px; font-size: 12.5px; max-width: 300px; }
.tip b { display: block; margin-bottom: 4px; }
.tip .r { display: flex; justify-content: space-between; gap: 14px; }
.inline-num { width: 110px; text-align: right; padding: 5px 8px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { background: var(--surface); border: 0; padding: 6px 12px; font: inherit; cursor: pointer; color: var(--muted); }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 500; color: var(--text); font-size: 13.5px; }
.check input { width: auto; }
.lock { font-size: 12.5px; color: var(--muted); }
.matrix td, .matrix th { padding: 7px 8px; font-size: 12.5px; }
.matrix td:first-child { white-space: nowrap; }

/* Retraite */
.ret-layout { display: grid; grid-template-columns: minmax(330px, 400px) minmax(0, 1fr); gap: 18px; align-items: start; }
@media (max-width: 1100px) { .ret-layout { grid-template-columns: minmax(0, 1fr); } }
.ret-layout > *, .grid > * { min-width: 0; }
.chart { max-height: 340px; }
.ret-form { position: sticky; top: 72px; max-height: calc(100vh - 90px); overflow-y: auto; }
@media (max-width: 1100px) { .ret-form { position: static; max-height: none; } }
.ret-form details { border-bottom: 1px solid var(--border); }
.ret-form details:last-child { border-bottom: 0; }
.ret-form summary { cursor: pointer; padding: 12px 16px; font-weight: 700; font-size: 13.5px; list-style: none; display: flex; align-items: center; gap: 8px; }
.ret-form summary::before { content: '▸'; color: var(--muted); transition: transform .15s; }
.ret-form details[open] summary::before { transform: rotate(90deg); }
.ret-form .fields { padding: 0 16px 14px; display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; }
.ret-form .fields .full { grid-column: 1 / -1; }
.ret-form label { font-size: 11.5px; margin-bottom: 3px; }
.ret-form input, .ret-form select { padding: 6px 8px; }
.ret-form .p-head { grid-column: 1 / -1; font-weight: 700; font-size: 12.5px; color: var(--primary); margin-top: 6px; display: flex; justify-content: space-between; align-items: center; }
.ret-kpis { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
.ret-kpis .value { font-size: 21px; }
.mini-table td, .mini-table th { padding: 5px 8px; font-size: 12.5px; }
.ev-row { display: grid; grid-template-columns: 60px 1fr 90px auto; gap: 6px; grid-column: 1 / -1; }
.year-table td, .year-table th { padding: 6px 8px; font-size: 12px; }
.year-table tr.retraite-debut td { border-top: 2px solid var(--primary); }
.year-table tr.manque td { background: var(--b-red-bg); }
@media print { .sidebar, .topbar, .toolbar, .btn { display: none !important; } .view { padding: 0; } .card { box-shadow: none; } }
.btn.danger-bg { background: var(--danger); border-color: var(--danger); color: #fff; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(min(380px, 100%), 1fr)); }
@media (max-width: 600px) {
  .topbar { flex-wrap: wrap; row-gap: 8px; }
  .periode { width: 100%; justify-content: center; }
  .periode #pm-now { display: none; }
  .bar-line { grid-template-columns: 1fr auto; }
  .bar-line .bar-track { grid-column: 1 / -1; grid-row: 2; }
}
