/* Système visuel : jetons de couleur (clair / sombre), puis composants. */
:root {
  --accent: #0f766e;
  --accent-soft: color-mix(in srgb, var(--accent) 12%, transparent);
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e3e6eb;
  --text: #16181d;
  --muted: #667085;
  --danger: #d92d20;
  --danger-soft: #fef3f2;
  --warning: #b54708;
  --warning-soft: #fffaeb;
  --success: #067647;
  --success-soft: #ecfdf3;
  --info: #175cd3;
  --info-soft: #eff8ff;
  --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);
  --radius: 10px;
  --sidebar: 232px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115; --surface: #171a21; --surface-2: #1e222b; --border: #2a2f3a; --text: #e8eaee; --muted: #98a2b3;
    --danger-soft: #3a1512; --warning-soft: #3a2a0c; --success-soft: #0c2e1f; --info-soft: #0f2340;
    --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1115; --surface: #171a21; --surface-2: #1e222b; --border: #2a2f3a; --text: #e8eaee; --muted: #98a2b3;
  --danger-soft: #3a1512; --warning-soft: #3a2a0c; --success-soft: #0c2e1f; --info-soft: #0f2340;
  --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.45 "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 0 0 8px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.pos { color: var(--success); } .neg { color: var(--danger); }
.hidden { display: none !important; }

/* ---------- mise en page ---------- */
.layout { display: grid; grid-template-columns: var(--sidebar) 1fr; min-height: 100vh; }
.sidebar { background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; gap: 10px; align-items: center; padding: 18px 16px 12px; }
.brand-logo { width: 34px; height: 34px; border-radius: 9px; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 700; overflow: hidden; }
.brand-logo img { width: 100%; height: 100%; object-fit: cover; }
.brand-name { font-weight: 700; line-height: 1.1; }
.brand-sub { font-size: 11px; color: var(--muted); }
.org-switch { margin: 0 12px 10px; }
.org-switch select { width: 100%; }
.nav { padding: 4px 8px; overflow-y: auto; flex: 1; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; color: var(--text); font-weight: 500; margin-bottom: 2px; }
.nav a:hover { background: var(--surface-2); text-decoration: none; }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.nav svg { width: 18px; height: 18px; flex: none; }
.nav .section { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; padding: 14px 10px 4px; }
.sidebar-foot { border-top: 1px solid var(--border); padding: 12px 14px; display: flex; align-items: center; gap: 8px; }
.avatar { width: 30px; height: 30px; border-radius: 50%; background: var(--surface-2); display: grid; place-items: center; font-weight: 600; font-size: 12px; }
.main { min-width: 0; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 16px 24px; border-bottom: 1px solid var(--border); background: var(--surface); position: sticky; top: 0; z-index: 500; }
.topbar .actions { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.content { padding: 20px 24px 40px; }
.topbar .menu-toggle { display: none; }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: fixed; z-index: 1000; transform: translateX(-100%); transition: transform .2s; width: 260px; box-shadow: var(--shadow-lg); }
  .sidebar.open { transform: none; }
  .topbar .menu-toggle { display: inline-flex; }
  .content { padding: 16px; }
  .topbar { padding: 12px 16px; flex-wrap: wrap; }
  /* Sur téléphone, les actions passent sous le titre et défilent horizontalement. */
  .topbar .actions { margin-left: 0; width: 100%; overflow-x: auto; padding-bottom: 2px; }
  .topbar .actions select { width: auto; max-width: 160px; }
}

/* ---------- composants ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.card + .card { margin-top: 16px; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 1100px) { .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } .grid-3 { grid-template-columns: 1fr 1fr; } }
@media (max-width: 700px) { .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; } }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.stack > * + * { margin-top: 12px; }

.kpi { padding: 14px 16px; }
.kpi .label { color: var(--muted); font-size: 12px; font-weight: 500; }
.kpi .value { font-size: 22px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .hint { font-size: 12px; color: var(--muted); margin-top: 2px; }
.kpi.alert .value { color: var(--danger); }

.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--text); padding: 7px 12px; border-radius: 8px; font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.small { padding: 4px 8px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn svg { width: 16px; height: 16px; }

.badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 500; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.success { background: var(--success-soft); color: var(--success); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.warning { background: var(--warning-soft); color: var(--warning); }
.badge.info { background: var(--info-soft); color: var(--info); }
.badge.accent { background: var(--accent-soft); color: var(--accent); }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; background: currentColor; }

input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
input[type="checkbox"] { width: auto; }
textarea { min-height: 90px; resize: vertical; }
label.field { display: block; }
label.field > span { display: block; font-size: 12px; font-weight: 500; color: var(--muted); margin-bottom: 4px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 14px; }
.form-grid .full { grid-column: 1 / -1; }
@media (max-width: 600px) { .form-grid { grid-template-columns: 1fr; } }
.check { display: flex; gap: 8px; align-items: center; }
.search { max-width: 280px; }

table.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table td { padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tr.clickable { cursor: pointer; }
.table tr.clickable:hover td { background: var(--surface-2); }
.table-wrap { overflow-x: auto; }
.table tfoot td { font-weight: 700; border-bottom: none; }
.empty { text-align: center; color: var(--muted); padding: 32px 12px; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; }
.tabs button { background: none; border: none; padding: 9px 12px; font: inherit; font-weight: 500; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); }

/* ---------- fenêtres ---------- */
.modal-back { position: fixed; inset: 0; background: rgba(15, 17, 21, .5); display: grid; place-items: center; z-index: 2000; padding: 16px; }
.modal { background: var(--surface); border-radius: 14px; box-shadow: var(--shadow-lg); width: min(640px, 100%); max-height: 92vh; display: flex; flex-direction: column; }
.modal.wide { width: min(900px, 100%); }
.modal header { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; align-items: center; }
.modal header h2 { margin: 0; }
.modal .body { padding: 18px 20px; overflow-y: auto; }
.modal footer { padding: 12px 20px; border-top: 1px solid var(--border); display: flex; gap: 8px; justify-content: flex-end; }
.toasts { position: fixed; bottom: 20px; right: 20px; z-index: 3000; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--text); color: var(--surface); padding: 10px 14px; border-radius: 10px; box-shadow: var(--shadow-lg); max-width: 380px; animation: slide .2s ease-out; }
.toast.error { background: var(--danger); color: #fff; }
@keyframes slide { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------- connexion ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; background: radial-gradient(circle at 20% 20%, var(--accent-soft), transparent 55%), var(--bg); padding: 16px; }
.auth .card { width: min(420px, 100%); padding: 28px; }
.auth h1 { margin-bottom: 4px; }

/* ---------- carte ---------- */
.map-layout { display: grid; grid-template-columns: 1fr 360px; gap: 16px; height: calc(100vh - 150px); min-height: 520px; }
.map-box { border-radius: var(--radius); overflow: hidden; border: 1px solid var(--border); box-shadow: var(--shadow); position: relative; }
#map { position: absolute; inset: 0; }
.map-side { overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
.map-legend { position: absolute; left: 12px; bottom: 12px; z-index: 400; background: var(--surface); border-radius: 10px; padding: 8px 10px; box-shadow: var(--shadow); font-size: 12px; display: flex; gap: 10px; flex-wrap: wrap; }
.pin { width: 30px; height: 30px; border-radius: 50% 50% 50% 0; transform: rotate(-45deg); border: 3px solid #fff; box-shadow: 0 2px 6px rgba(0,0,0,.35); display: grid; place-items: center; }
.pin span { transform: rotate(45deg); color: #fff; font-size: 10px; font-weight: 700; }
.pin.alerte::after { content: ""; position: absolute; top: -4px; right: -4px; width: 11px; height: 11px; border-radius: 50%; background: var(--danger); border: 2px solid #fff; }
.bien-item { display: flex; gap: 10px; padding: 10px; border-radius: 10px; cursor: pointer; border: 1px solid transparent; }
.bien-item:hover, .bien-item.active { background: var(--surface-2); border-color: var(--border); }
.state-dot { width: 10px; height: 10px; border-radius: 50%; margin-top: 5px; flex: none; }
@media (max-width: 1000px) { .map-layout { grid-template-columns: 1fr; height: auto; } .map-box { height: 60vh; } }
.leaflet-popup-content { font: 13px/1.4 system-ui, sans-serif; }
.leaflet-container { background: var(--surface-2); }
:root[data-theme="dark"] .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(.95) contrast(.9); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(.95) contrast(.9); } }

/* ---------- planning ---------- */
.cal { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); border-left: 1px solid var(--border); border-top: 1px solid var(--border); }
.cal .dow { font-size: 12px; font-weight: 600; color: var(--muted); padding: 6px 8px; border-right: 1px solid var(--border); border-bottom: 1px solid var(--border); background: var(--surface-2); }
.cal .day { min-height: 110px; border-right: 1px solid var(--border); border-bottom: 1px solid var(--border); padding: 4px; background: var(--surface); overflow: hidden; }
.cal .day.out { background: var(--surface-2); opacity: .6; }
.cal .day.today .n { background: var(--accent); color: #fff; }
.cal .n { font-size: 12px; font-weight: 600; width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; }
.ev { font-size: 11.5px; padding: 2px 6px; border-radius: 5px; margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; cursor: pointer; }
.ev.sejour { background: #dbeafe; color: #1e40af; }
.ev.contrat { background: #dcfce7; color: #166534; }
.ev.intervention { background: #fef3c7; color: #92400e; }
.ev.intervention.urgente, .ev.intervention.haute { background: #fee2e2; color: #991b1b; }
.ev.evenement { background: #ede9fe; color: #5b21b6; }
:root[data-theme="dark"] .ev, .dark .ev { filter: saturate(.8) brightness(.9); }

.timeline { overflow-x: auto; }
.tl-grid { display: grid; min-width: 900px; }
.tl-head, .tl-row { display: grid; grid-template-columns: 170px 1fr; border-bottom: 1px solid var(--border); }
.tl-label { padding: 8px; font-weight: 500; border-right: 1px solid var(--border); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; background: var(--surface); position: sticky; left: 0; z-index: 2; }
.tl-days { display: grid; position: relative; }
.tl-days .d { font-size: 10px; text-align: center; color: var(--muted); border-right: 1px solid var(--border); padding: 4px 0; }
.tl-days .d.we { background: var(--surface-2); }
.tl-track { position: relative; height: 38px; }
.tl-bar { position: absolute; top: 6px; height: 26px; border-radius: 6px; font-size: 11px; padding: 4px 6px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; cursor: pointer; }
.tl-bar.sejour { background: #3b82f6; color: #fff; }
.tl-bar.contrat { background: #22c55e; color: #fff; }
.tl-bar.intervention { background: #f59e0b; color: #fff; top: 22px; height: 12px; padding: 0; }
.tl-today { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--danger); z-index: 1; }

/* ---------- interventions (kanban) ---------- */
.kanban { display: grid; grid-template-columns: repeat(4, minmax(240px, 1fr)); gap: 12px; overflow-x: auto; align-items: start; }
.col { background: var(--surface-2); border-radius: var(--radius); padding: 10px; min-height: 200px; }
.col h3 { display: flex; justify-content: space-between; }
.col.drop { outline: 2px dashed var(--accent); }
.ticket { background: var(--surface); border: 1px solid var(--border); border-radius: 9px; padding: 10px; margin-bottom: 8px; box-shadow: var(--shadow); cursor: grab; }
.ticket .t { font-weight: 600; margin-bottom: 4px; }
.ticket.urgente { border-left: 4px solid var(--danger); }
.ticket.haute { border-left: 4px solid var(--warning); }

/* ---------- notes ---------- */
.notes { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px; }
.note { border-radius: 10px; padding: 12px; min-height: 120px; box-shadow: var(--shadow); cursor: pointer; white-space: pre-wrap; position: relative; color: #1f2937; }
.note.jaune { background: #fef9c3; } .note.vert { background: #dcfce7; } .note.bleu { background: #dbeafe; } .note.rose { background: #fce7f3; } .note.gris { background: #e5e7eb; }
.note .pin-icon { position: absolute; top: 8px; right: 10px; font-size: 13px; }
.note h4 { margin: 0 0 6px; font-size: 14px; }
.note .meta { font-size: 11px; opacity: .7; margin-top: 8px; }

/* ---------- graphiques ---------- */
.chart { width: 100%; height: 220px; }
.chart text { fill: var(--muted); font-size: 11px; }
.chart .grid-line { stroke: var(--border); }
.bar-in { fill: var(--accent); } .bar-out { fill: #f59e0b; } .line-res { stroke: var(--text); fill: none; stroke-width: 2; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 4px; vertical-align: -1px; }

.detail-head { display: flex; gap: 16px; align-items: flex-start; margin-bottom: 16px; }
.detail-head .photo { width: 96px; height: 72px; border-radius: 10px; background: var(--surface-2); object-fit: cover; }
dl.props { display: grid; grid-template-columns: 150px 1fr; gap: 6px 12px; margin: 0; }
dl.props dt { color: var(--muted); }
dl.props dd { margin: 0; }
.files { display: flex; gap: 8px; flex-wrap: wrap; }
.file { border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; display: flex; gap: 6px; align-items: center; }
.drop-zone { border: 2px dashed var(--border); border-radius: 10px; padding: 14px; text-align: center; color: var(--muted); cursor: pointer; }
.drop-zone.over { border-color: var(--accent); color: var(--accent); }
