:root {
  --brand: #0f766e;
  --brand-dark: #115e59;
  --brand-soft: #ccfbf1;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --danger: #dc2626;
  --warn: #d97706;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 4px 16px rgba(15, 23, 42, .06);

  --s-unassigned: #d97706;
  --s-assigned: #2563eb;
  --s-started: #7c3aed;
  --s-picked_up: #0891b2;
  --s-delivered: #16a34a;
  --s-failed: #dc2626;
  --s-cancelled: #94a3b8;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font-family: Inter, system-ui, sans-serif; font-size: 14px; color: var(--text); background: var(--bg); }
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; margin: 4px 0; }

/* Boutons */
.btn { border: 1px solid transparent; border-radius: 8px; padding: 8px 14px; cursor: pointer; font-weight: 600; background: var(--surface); }
.btn.primary { background: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.outline { border-color: var(--brand); color: var(--brand); }
.btn.ghost { background: transparent; color: var(--muted); }
.btn.danger { color: var(--danger); border-color: #fecaca; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.full { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }

/* Connexion */
.login { min-height: 100%; display: grid; place-items: center; background: linear-gradient(135deg, #0f766e, #134e4a); padding: 16px; }
.login-card { background: var(--surface); width: 100%; max-width: 360px; padding: 28px; border-radius: 14px; box-shadow: var(--shadow); display: grid; gap: 12px; }
.login-card label, .form label { display: grid; gap: 4px; font-weight: 500; font-size: 13px; }
input, select, textarea { border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px; background: #fff; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand-soft); border-color: var(--brand); }

.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; white-space: nowrap; }
.brand.big { font-size: 22px; }
.logo { color: var(--brand); font-size: 1.2em; }
.brand .tag { font-size: 11px; font-weight: 600; background: var(--brand-soft); color: var(--brand-dark); padding: 2px 8px; border-radius: 99px; }

/* Structure */
.app { height: 100%; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 16px; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line); }
.stats { display: flex; gap: 8px; flex: 1; overflow-x: auto; }
.stat { background: var(--bg); border-radius: 8px; padding: 4px 10px; font-size: 12px; white-space: nowrap; }
.stat b { font-size: 14px; margin-right: 4px; }
.top-actions { display: flex; align-items: center; gap: 8px; }
.live { font-size: 12px; font-weight: 600; padding: 4px 10px; border-radius: 99px; background: #dcfce7; color: #166534; }
.live::before { content: '● '; }
.live.off { background: #fee2e2; color: #991b1b; }

.layout { flex: 1; display: flex; min-height: 0; position: relative; }
.panel { width: 380px; background: var(--surface); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.tabs { display: flex; border-bottom: 1px solid var(--line); }
.tab { flex: 1; background: none; border: 0; padding: 12px; cursor: pointer; font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; }
.tab.active { color: var(--brand); border-color: var(--brand); }
.tab-body { padding: 12px; display: flex; flex-direction: column; gap: 10px; min-height: 0; flex: 1; }
.filters { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--line); background: #fff; border-radius: 99px; padding: 4px 10px; font-size: 12px; cursor: pointer; }
.chip.active { background: var(--text); color: #fff; border-color: var(--text); }
.search { padding: 8px 10px; }
.list { overflow-y: auto; display: flex; flex-direction: column; gap: 8px; flex: 1; padding-bottom: 12px; }
.empty { text-align: center; color: var(--muted); padding: 30px 10px; }

/* Cartes */
.card { border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; background: #fff; cursor: pointer; transition: border-color .15s, box-shadow .15s; }
.card:hover { border-color: #cbd5e1; box-shadow: var(--shadow); }
.card.selected { border-color: var(--brand); box-shadow: 0 0 0 2px var(--brand-soft); }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 6px; }
.ref { font-weight: 700; }
.badge { font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 99px; color: #fff; white-space: nowrap; }
.stops { display: grid; gap: 3px; font-size: 13px; }
.stop { display: flex; gap: 6px; align-items: baseline; }
.stop .dot { width: 8px; height: 8px; border-radius: 50%; flex: none; transform: translateY(1px); }
.dot.p { background: #0ea5e9; }
.dot.d { background: var(--brand); }
.stop span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 8px; font-size: 12px; color: var(--muted); }
.card-foot select { width: auto; max-width: 170px; padding: 4px 6px; font-size: 12px; }
.warn-flag { color: var(--warn); font-weight: 600; }

.courier { display: flex; align-items: center; gap: 10px; }
.avatar { width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; color: #fff; background: #94a3b8; flex: none; }
.avatar.on { background: var(--brand); }
.courier-info { flex: 1; min-width: 0; }
.courier-info .name { font-weight: 600; }
.courier-info .sub { font-size: 12px; color: var(--muted); }
.pill { font-size: 11px; padding: 2px 8px; border-radius: 99px; background: var(--bg); color: var(--muted); }
.pill.on { background: #dcfce7; color: #166534; }

/* Carte */
.map-wrap { flex: 1; position: relative; }
#map { position: absolute; inset: 0; }
.pick-hint { position: absolute; top: 12px; left: 50%; transform: translateX(-50%); z-index: 500; background: var(--text); color: #fff; padding: 8px 14px; border-radius: 99px; font-weight: 600; box-shadow: var(--shadow); }
.marker-courier { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 700; font-size: 12px; border: 3px solid #fff; box-shadow: 0 2px 6px rgba(0,0,0,.35); background: #94a3b8; }
.marker-courier.on { background: var(--brand); }
.marker-stop { width: 26px; height: 26px; border-radius: 50% 50% 50% 0; transform: rotate(-45deg); border: 2px solid #fff; box-shadow: 0 2px 6px rgba(0,0,0,.3); display: grid; place-items: center; }
.marker-stop b { transform: rotate(45deg); color: #fff; font-size: 11px; }
.marker-stop.p { background: #0ea5e9; }
.marker-stop.d { background: var(--brand); }

/* Panneau droit */
.drawer { position: absolute; top: 0; right: 0; bottom: 0; width: 420px; max-width: 100%; background: var(--surface); border-left: 1px solid var(--line); box-shadow: -8px 0 24px rgba(15,23,42,.08); z-index: 800; overflow-y: auto; padding: 18px; }
.drawer-close { position: absolute; top: 10px; right: 12px; border: 0; background: none; font-size: 26px; cursor: pointer; color: var(--muted); }
.drawer h2 { margin: 0 0 4px; font-size: 18px; }
.drawer h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 18px 0 8px; }
.form { display: grid; gap: 10px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; display: grid; gap: 8px; }
.fieldset legend { font-weight: 700; padding: 0 6px; }
.addr { position: relative; }
.addr-row { display: flex; gap: 6px; }
.addr-row .btn { white-space: nowrap; }
.suggest { position: absolute; left: 0; right: 0; top: 100%; background: #fff; border: 1px solid var(--line); border-radius: 8px; box-shadow: var(--shadow); z-index: 10; max-height: 220px; overflow-y: auto; }
.suggest div { padding: 8px 10px; cursor: pointer; font-size: 13px; }
.suggest div:hover { background: var(--bg); }
.geo-ok { font-size: 12px; color: #166534; }
.geo-no { font-size: 12px; color: var(--warn); }
.detail-block { background: var(--bg); border-radius: var(--radius); padding: 10px 12px; display: grid; gap: 2px; margin-bottom: 8px; }
.detail-block .label { font-size: 11px; font-weight: 700; text-transform: uppercase; color: var(--muted); }
.timeline { list-style: none; margin: 0; padding: 0; border-left: 2px solid var(--line); }
.timeline li { position: relative; padding: 0 0 12px 14px; }
.timeline li::before { content: ''; position: absolute; left: -6px; top: 4px; width: 10px; height: 10px; border-radius: 50%; background: var(--brand); }
.timeline li.incident::before { background: var(--warn); }
.timeline .when { font-size: 12px; color: var(--muted); }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }

/* Notifications */
.toasts { position: fixed; bottom: 16px; right: 16px; display: grid; gap: 8px; z-index: 2000; }
.toast { background: var(--text); color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); max-width: 340px; animation: in .2s ease-out; }
.toast.warn { background: var(--warn); }
.toast.err { background: var(--danger); }
@keyframes in { from { transform: translateY(8px); opacity: 0; } }

/* Mobile / petits écrans */
@media (max-width: 900px) {
  .layout { flex-direction: column; }
  .panel { width: 100%; height: 45%; border-right: 0; border-bottom: 1px solid var(--line); }
  .map-wrap { min-height: 55%; }
  .stats { display: none; }
  .topbar { flex-wrap: wrap; }
  .drawer { width: 100%; }
}
