:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1d2330;
  --muted: #6b7280;
  --border: #e3e5ea;
  --accent: #2563eb;
  --ok: #16a34a;
  --warn: #ea8a00;
  --bad: #dc2626;
  --lost: #9333ea;
  --row-hover: #f1f4fb;
  --row-sel: #e6edff;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11141a;
    --surface: #1a1e26;
    --text: #e6e8ec;
    --muted: #949aa6;
    --border: #2b303b;
    --accent: #6b9bff;
    --row-hover: #222835;
    --row-sel: #253150;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 14px/1.4 system-ui, "Segoe UI", sans-serif; background: var(--bg); color: var(--text); display: flex; flex-direction: column; }
button { font: inherit; cursor: pointer; border-radius: 6px; border: 1px solid var(--accent); background: var(--accent); color: #fff; padding: 6px 12px; }
button.ghost { background: transparent; color: var(--text); border-color: var(--border); }
button.danger { background: var(--lost); border-color: var(--lost); }
input, textarea, select { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; }
.muted { color: var(--muted); }
.error { color: var(--bad); min-height: 1.2em; }

/* Login */
.login-page { align-items: center; justify-content: center; }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 28px; width: min(380px, calc(100vw - 32px)); display: flex; flex-direction: column; gap: 12px; }
.login-card h1 { margin: 0; font-size: 20px; }
.login-card label { display: flex; flex-direction: column; gap: 4px; }

/* Header */
.top { display: flex; align-items: center; gap: 16px; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.top h1 { font-size: 17px; margin: 0; white-space: nowrap; }
.kpis { display: flex; gap: 8px; flex-wrap: wrap; flex: 1; }
.kpi { border: 1px solid var(--border); border-radius: 8px; padding: 4px 10px; min-width: 84px; }
.kpi b { display: block; font-size: 18px; font-variant-numeric: tabular-nums; }
.kpi span { font-size: 12px; color: var(--muted); }
.kpi.ok b { color: var(--ok); } .kpi.warn b { color: var(--warn); } .kpi.bad b { color: var(--bad); } .kpi.lost b { color: var(--lost); }
.top-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* Layout */
.layout { flex: 1; display: grid; grid-template-columns: minmax(380px, 38%) 1fr; min-height: 0; }
#map { min-height: 300px; }
.panel { display: flex; flex-direction: column; min-height: 0; background: var(--surface); border-right: 1px solid var(--border); }
.filters { padding: 10px; display: flex; flex-direction: column; gap: 8px; border-bottom: 1px solid var(--border); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chips button { background: transparent; color: var(--text); border-color: var(--border); padding: 3px 10px; border-radius: 999px; }
.chips button.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.table-wrap { overflow: auto; flex: 1; min-height: 120px; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 6px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { position: sticky; top: 0; background: var(--surface); cursor: pointer; user-select: none; font-weight: 600; font-size: 12px; color: var(--muted); }
th.sorted::after { content: " ▲"; } th.sorted.desc::after { content: " ▼"; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--row-hover); }
tbody tr.sel { background: var(--row-sel); }
.empty { padding: 16px; }

.badge { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.s-en_ligne { background: var(--ok); } .s-recent { background: var(--warn); } .s-hors_ligne { background: var(--bad); } .s-inconnu { background: var(--muted); }
.os { font-size: 15px; }
.batt-low { color: var(--bad); font-weight: 600; }
.tag-lost { background: var(--lost); color: #fff; border-radius: 4px; padding: 0 5px; font-size: 11px; margin-left: 4px; }

/* Fiche appareil */
.detail { border-top: 2px solid var(--border); padding: 12px; overflow: auto; max-height: 55%; }
.detail h2 { margin: 0 0 6px; font-size: 16px; display: flex; align-items: center; gap: 8px; }
.detail dl { display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; margin: 8px 0; font-size: 13px; }
.detail dt { color: var(--muted); }
.detail dd { margin: 0; overflow-wrap: anywhere; }
.detail .row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin: 8px 0; }
.lost-box { border: 1px solid var(--lost); border-radius: 8px; padding: 8px; margin: 8px 0; }
.timeline { list-style: none; padding: 0; margin: 8px 0 0; font-size: 13px; }
.timeline li { padding: 4px 6px; border-left: 2px solid var(--border); margin-left: 4px; cursor: pointer; }
.timeline li:hover { background: var(--row-hover); }
.timeline li .src { font-size: 11px; color: var(--muted); }

/* Marqueurs */
.pin { width: 16px; height: 16px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.35); }
.pin.android { border-radius: 4px; transform: rotate(45deg); }
.pin.lost { width: 22px; height: 22px; background: var(--lost) !important; animation: pulse 1.4s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(147,51,234,.7); } 100% { box-shadow: 0 0 0 14px rgba(147,51,234,0); } }
.leaflet-popup-content dl { display: grid; grid-template-columns: auto 1fr; gap: 1px 10px; margin: 4px 0 0; }
.leaflet-popup-content dt { color: #6b7280; }
.leaflet-popup-content dd { margin: 0; }

dialog { border: 1px solid var(--border); border-radius: 10px; background: var(--surface); color: var(--text); width: min(820px, calc(100vw - 32px)); max-height: 80vh; padding: 0; }
dialog::backdrop { background: rgba(0,0,0,.4); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); }
.dlg-head h2 { margin: 0; font-size: 16px; }
#dlg-body { padding: 8px 14px 14px; overflow: auto; }

@media (max-width: 860px) {
  .layout { grid-template-columns: minmax(0, 1fr); grid-template-rows: 45vh auto; }
  .top { gap: 8px; padding: 8px 12px; }
  .top h1 { width: 100%; }
  .kpis { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; flex-basis: 100%; }
  .kpi { min-width: 0; padding: 3px 8px; }
  .kpi b { font-size: 16px; }
  .kpi span { font-size: 11px; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  #map { order: -1; }
  .panel { border-right: 0; }
  .detail { max-height: none; }
}
