:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f1f3f9;
  --border: #e3e6ef;
  --text: #1c1f2e;
  --muted: #6b7185;
  --accent: #4f46e5;
  --accent-soft: #eceafd;
  --danger: #c2334d;
  --danger-soft: #fdecef;
  --warn: #b26a00;
  --warn-soft: #fff4e0;
  --ok: #1d8a5a;
  --ok-soft: #e4f6ed;
  --info: #2563eb;
  --info-soft: #e6eefe;
  --shadow: 0 1px 2px rgba(20, 24, 40, 0.06), 0 4px 16px rgba(20, 24, 40, 0.04);
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11131b;
    --surface: #1a1d28;
    --surface-2: #232736;
    --border: #2e3346;
    --text: #e8eaf2;
    --muted: #9aa0b5;
    --accent: #8b85ff;
    --accent-soft: #2a2850;
    --danger: #ff7a90;
    --danger-soft: #3a1f27;
    --warn: #ffb74d;
    --warn-soft: #3a2e17;
    --ok: #5fd39c;
    --ok-soft: #173327;
    --info: #7aa7ff;
    --info-soft: #1c2a47;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 Inter, system-ui, -apple-system, 'Segoe UI', sans-serif;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 22px; font-weight: 700; }
h2 { font-size: 16px; font-weight: 600; }
h3 { font-size: 14px; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
[hidden] { display: none !important; }

/* ---------- Layout ---------- */

.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 20px 12px;
  background: var(--surface);
  border-right: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 18px; font-weight: 700; font-size: 16px; }
.brand img { width: 28px; height: 28px; }
.brand small { display: block; font-weight: 500; font-size: 11px; color: var(--muted); }

.nav-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 8px;
  color: var(--text);
  font-weight: 500;
}
.nav-link:hover { background: var(--surface-2); text-decoration: none; }
.nav-link.active { background: var(--accent-soft); color: var(--accent); }
.nav-link .ico { width: 18px; text-align: center; }
.nav-link .badge { margin-left: auto; }
.sidebar .spacer { flex: 1; }
.sidebar .demo-note { font-size: 11px; color: var(--muted); padding: 10px; border-top: 1px solid var(--border); }

.main { padding: 28px 32px 64px; max-width: 1280px; width: 100%; }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head p { margin: 4px 0 0; color: var(--muted); }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.grid { display: grid; gap: 16px; }
.grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.kpis { grid-template-columns: repeat(4, minmax(0, 1fr)); margin-bottom: 16px; }
.grid.side { grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); align-items: start; }

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

.kpi { display: flex; flex-direction: column; gap: 2px; }
.kpi .value { font-size: 28px; font-weight: 700; }
.kpi .label { color: var(--muted); font-size: 13px; }
.kpi.danger .value { color: var(--danger); }
.kpi.warn .value { color: var(--warn); }

.population { display: flex; gap: 20px; flex-wrap: wrap; }
.population div { display: flex; align-items: center; gap: 8px; }
.population strong { font-size: 18px; }

/* ---------- Composants ---------- */

.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(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.danger { color: var(--danger); }
.btn.small { padding: 4px 10px; font-size: 12px; }
.btn:disabled { opacity: 0.6; cursor: wait; }

.icon-btn { border: 0; background: transparent; color: var(--muted); cursor: pointer; font-size: 16px; padding: 4px 8px; border-radius: 6px; }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.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.mission, .badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.bench, .badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.interne, .badge.info { background: var(--info-soft); color: var(--info); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.accent { background: var(--accent-soft); color: var(--accent); }
.badge.count { background: var(--danger); color: #fff; min-width: 20px; justify-content: center; }

.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }

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, .btn:focus-visible, .chip:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
textarea { min-height: 90px; resize: vertical; }
input[type='checkbox'] { width: 18px; height: 18px; accent-color: var(--accent); cursor: pointer; flex: none; margin: 0; }
input.search { width: 240px; }

label.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 500; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.form-grid .full { grid-column: 1 / -1; }
.form-error { color: var(--danger); margin: 0 20px 8px; }
.hint { font-size: 12px; color: var(--muted); font-weight: 400; }
fieldset { border: 1px dashed var(--border); border-radius: 10px; padding: 12px; margin: 0; display: flex; flex-direction: column; gap: 8px; }
legend { font-weight: 600; font-size: 13px; padding: 0 6px; }
.row-inputs { display: grid; grid-template-columns: 1fr 150px auto; gap: 8px; align-items: center; }

/* Tableaux */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th {
  text-align: left;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
td { padding: 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }
td.actions { text-align: right; white-space: nowrap; }

/* Listes d'items */
.list { display: flex; flex-direction: column; }
.item { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.item:last-child { border-bottom: 0; }
.item .body { flex: 1; min-width: 0; }
.item .title { font-weight: 500; }
.item .meta { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; color: var(--muted); font-size: 12px; margin-top: 2px; }
.item.done .title { text-decoration: line-through; color: var(--muted); }
.item .when { white-space: nowrap; font-size: 12px; text-align: right; }
.when.late { color: var(--danger); font-weight: 600; }
.when.soon { color: var(--warn); font-weight: 600; }

.group-title { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 16px 0 4px; }
.group-title:first-child { margin-top: 0; }
.group-title.late { color: var(--danger); }

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

progress { width: 100%; height: 6px; border: 0; border-radius: 99px; overflow: hidden; background: var(--surface-2); appearance: none; }
progress::-webkit-progress-bar { background: var(--surface-2); }
progress::-webkit-progress-value { background: var(--accent); border-radius: 99px; }
progress::-moz-progress-bar { background: var(--accent); }

.journey { padding: 12px 0; border-bottom: 1px solid var(--border); }
.journey:last-child { border-bottom: 0; }
.journey-head { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; margin-bottom: 6px; }
details summary { cursor: pointer; color: var(--accent); font-size: 12px; margin-top: 6px; }

.steps { list-style: none; margin: 8px 0 0; padding: 0; }
.steps li { display: grid; grid-template-columns: 90px 1fr; gap: 8px; padding: 6px 0; border-bottom: 1px dashed var(--border); }
.steps li:last-child { border-bottom: 0; }
.steps .offset { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; color: var(--accent); font-weight: 600; }

.profile-head { display: flex; align-items: center; gap: 16px; }
.avatar {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 700;
  font-size: 18px;
  flex: none;
}
.avatar.sm { width: 32px; height: 32px; font-size: 12px; }
dl.info { display: grid; grid-template-columns: 120px 1fr; gap: 8px 12px; margin: 0; }
dl.info dt { color: var(--muted); }
dl.info dd { margin: 0; }

.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { position: relative; padding: 0 0 16px 22px; border-left: 2px solid var(--border); margin-left: 6px; }
.timeline li:last-child { border-left-color: transparent; padding-bottom: 0; }
.timeline li::before {
  content: '';
  position: absolute;
  left: -7px;
  top: 3px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--surface);
  border: 2px solid var(--accent);
}
.timeline .t-head { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.timeline p { margin: 4px 0 0; white-space: pre-wrap; }

.note-form { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; }
.note-form .toolbar { justify-content: flex-end; }
.note-form select { width: auto; }

.callout { background: var(--accent-soft); border-radius: 10px; padding: 12px 14px; font-size: 13px; margin-bottom: 16px; }

/* ---------- Modale, toast, login ---------- */

dialog {
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 0;
  width: min(640px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
}
dialog::backdrop { background: rgba(15, 18, 30, 0.45); }
dialog form { display: flex; flex-direction: column; max-height: calc(100vh - 48px); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 18px 20px 8px; }
.modal-body { padding: 8px 20px 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 20px; border-top: 1px solid var(--border); }

#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 10px;
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  max-width: calc(100vw - 32px);
  z-index: 10;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login .card { width: min(380px, 100%); padding: 28px; display: flex; flex-direction: column; gap: 14px; }
.login .brand { padding: 0 0 6px; }

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

/* ---------- Mobile ---------- */

@media (max-width: 1000px) {
  .grid.kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid.two, .grid.side { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: sticky;
    height: auto;
    flex-direction: row;
    overflow-x: auto;
    padding: 8px 12px;
    border-right: 0;
    border-bottom: 1px solid var(--border);
    z-index: 5;
  }
  .brand small, .sidebar .spacer, .sidebar .demo-note, .nav-link .label { display: none; }
  .brand { padding: 4px 8px 4px 0; }
  .main { padding: 20px 16px 48px; }
  .form-grid { grid-template-columns: 1fr; }
  input.search { width: 100%; }
  .row-inputs { grid-template-columns: 1fr; }
  dl.info { grid-template-columns: 1fr; gap: 2px; }
  dl.info dd { margin-bottom: 8px; }
  .hide-sm { display: none; }
}

.note-form input[type='date'] { width: auto; }
.card + .grid, .grid + .card, .grid + .grid { margin-top: 16px; }
