:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #586579;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --ok: #15803d;
  --warn: #b45309;
  --danger: #dc2626;
  --off: #94a3b8;
  --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 4px 16px rgba(15, 23, 42, .05);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1120;
    --surface: #111827;
    --surface-2: #1a2333;
    --border: #2f3b52;
    --text: #e5e9f0;
    --muted: #a0aec0;
    --primary: #60a5fa;
    --primary-text: #0b1120;
    --ok: #4ade80;
    --warn: #fbbf24;
    --danger: #f87171;
    --off: #64748b;
    --shadow: 0 1px 3px rgba(0, 0, 0, .5), 0 4px 16px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1120;
  --surface: #111827;
  --surface-2: #1a2333;
  --border: #2f3b52;
  --text: #e5e9f0;
  --muted: #a0aec0;
  --primary: #60a5fa;
  --primary-text: #0b1120;
  --ok: #4ade80;
  --warn: #fbbf24;
  --danger: #f87171;
  --off: #64748b;
  --shadow: 0 1px 3px rgba(0, 0, 0, .5), 0 4px 16px rgba(0, 0, 0, .3);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
h1 { font-size: 22px; margin: 0 0 16px; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 13px; margin: 16px 0 8px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); min-height: 1em; margin: 4px 0; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }

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(--primary); outline-offset: -1px; }
input[type=color] { padding: 2px; height: 36px; }
input[type=checkbox] { width: auto; }
label { display: flex; flex-direction: column; gap: 4px; font-weight: 500; font-size: 13px; }
label.inline { flex-direction: row; align-items: center; gap: 8px; }

.btn {
  font: inherit; font-weight: 500; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 7px 14px; border-radius: 8px; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.small { padding: 4px 10px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* Connexion */
.login-screen { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); padding: 28px; border-radius: 14px;
  box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 14px;
}
.login-card h1 { margin: 0; display: flex; justify-content: center; align-items: center; }
.logo { display: block; height: auto; max-width: 100%; }
.logo-login { width: 240px; margin: 4px auto 6px; }
.logo-top { height: 28px; width: auto; }

/* Structure */
.app { height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 24px; padding: 0 16px; height: 54px; flex-shrink: 0;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 16px; display: flex; align-items: center; gap: 10px; }
#nav { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
#nav a { color: var(--muted); text-decoration: none; padding: 8px 12px; border-radius: 8px; font-weight: 500; white-space: nowrap; }
#nav a.active { color: var(--primary); background: color-mix(in srgb, var(--primary) 10%, transparent); }
.user-box { display: flex; align-items: center; gap: 10px; white-space: nowrap; }
.ws-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--off); }
.ws-dot.on { background: var(--ok); }

.view { flex: 1; min-height: 0; }
.page { overflow-y: auto; padding: 24px; max-width: 1200px; width: 100%; margin: 0 auto; }
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head h1 { margin: 0; }
.page > h2 { margin-top: 28px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; overflow-x: auto; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; }

.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table td { padding: 10px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table td.actions { text-align: right; white-space: nowrap; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }
.swatch { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11.5px; font-weight: 600; background: var(--surface-2); border: 1px solid var(--border); }
.badge.ok { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, transparent); }
.badge.warn { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 40%, transparent); }
.badge.off { color: var(--muted); }
.badge.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }

/* Connexions échouées */
.nav-badge { display: inline-block; min-width: 18px; padding: 0 5px; margin-left: 4px; border-radius: 999px; background: var(--danger); color: #fff; font-size: 11px; line-height: 18px; text-align: center; }
.summary-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 12px; margin-bottom: 16px; }
.summary-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; cursor: pointer; text-align: left; font: inherit; color: var(--text); }
.summary-card:hover { border-color: var(--primary); }
.summary-card.active { border-color: var(--primary); box-shadow: 0 0 0 2px color-mix(in srgb, var(--primary) 25%, transparent); }
.summary-card .n { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.summary-card .l { font-size: 12.5px; font-weight: 600; }
.summary-card .s { font-size: 11.5px; color: var(--muted); }
.failures-table td { vertical-align: top; }
.failures-table .detail { max-width: 420px; }
.failures-table details { margin-top: 4px; }
.failures-table summary { cursor: pointer; color: var(--muted); font-size: 12px; }
.failures-table .hex { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11.5px; word-break: break-all; background: var(--surface-2); padding: 4px 6px; border-radius: 6px; margin-top: 4px; }
.failures-table .hint { font-size: 12px; color: var(--muted); margin-top: 2px; }
tr.flash td { animation: flash 1.5s ease-out; }
@keyframes flash { from { background: color-mix(in srgb, var(--warn) 22%, transparent); } to { background: transparent; } }
.badge.k-unknown_imei, .badge.k-transport_mismatch, .badge.k-disabled { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 40%, transparent); }
.badge.k-crc, .badge.k-decode_error, .badge.k-udp_invalid, .badge.k-tls { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }

/* Carte */
.view-map { display: flex; position: relative; }
.sidebar { width: 290px; flex-shrink: 0; background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 12px; gap: 8px; min-height: 0; }
.device-list { list-style: none; padding: 0; margin: 0; overflow-y: auto; flex: 1; }
.device-list li { padding: 10px; border-radius: 8px; cursor: pointer; display: flex; gap: 10px; align-items: flex-start; }
.device-list li:hover { background: var(--surface-2); }
.device-list li.selected { background: color-mix(in srgb, var(--primary) 12%, transparent); }
.device-list .dot { width: 10px; height: 10px; border-radius: 50%; margin-top: 5px; flex-shrink: 0; background: var(--off); }
.device-list .dot.online { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 25%, transparent); }
.device-list .name { font-weight: 600; }
.device-list .meta { font-size: 12px; color: var(--muted); }
.map { flex: 1; min-width: 0; }

.detail { width: 360px; flex-shrink: 0; background: var(--surface); border-left: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.detail-head { display: flex; justify-content: space-between; gap: 8px; padding: 14px 16px 8px; }
.detail-head h2 { margin: 0; }
.tabs { display: flex; border-bottom: 1px solid var(--border); padding: 0 8px; }
.tabs button { font: inherit; font-weight: 500; background: none; border: 0; padding: 10px 10px; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; }
.tabs button.active { color: var(--primary); border-bottom-color: var(--primary); }
.tab-body { padding: 12px 16px; overflow-y: auto; flex: 1; }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; font-size: 13px; }
.kv dt, .kv .k { color: var(--muted); }
.kv .v { font-variant-numeric: tabular-nums; text-align: right; }
.io-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.io-table td { padding: 5px 0; border-bottom: 1px solid var(--border); }
.io-table td:first-child { color: var(--muted); }
.io-table td:last-child { text-align: right; font-variant-numeric: tabular-nums; }
.io-id { font-size: 11px; color: var(--muted); margin-right: 4px; }

.history-form { display: flex; flex-direction: column; gap: 10px; margin-bottom: 12px; }
.playback { margin-top: 14px; display: flex; flex-direction: column; gap: 10px; }
.playback select { width: auto; }
.playback input[type=range] { padding: 0; border: 0; }

.command-form { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.command-form .row input { flex: 1; width: auto; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { font: inherit; font-size: 12px; padding: 3px 9px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text); cursor: pointer; }
.chip:hover { border-color: var(--primary); }
.repeat-box { border: 1px dashed var(--border); border-radius: 8px; padding: 10px; margin-bottom: 12px; display: flex; flex-direction: column; gap: 8px; }
.repeat-box select { width: auto; flex: 1; }
.repeat-box.running { border-style: solid; border-color: var(--primary); }
.command-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.command-list li { border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; }
.command-list .cmd { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.command-list pre { margin: 6px 0 0; white-space: pre-wrap; word-break: break-word; font-size: 12px; background: var(--surface-2); padding: 6px 8px; border-radius: 6px; }

.marker-pin {
  width: 30px; height: 30px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, .4);
  display: grid; place-items: center; color: #fff; font-size: 13px;
}
.marker-pin.offline { filter: grayscale(.7) opacity(.75); }
.marker-pin .arrow { display: inline-block; line-height: 1; }
.marker-label { background: var(--surface); color: var(--text); border: 1px solid var(--border); font-weight: 600; box-shadow: var(--shadow); }
.leaflet-container { background: var(--bg); font: inherit; }

.modal { border: 0; border-radius: 14px; padding: 0; width: min(520px, calc(100vw - 32px)); background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.modal::backdrop { background: rgba(15, 23, 42, .45); }
.modal .form { padding: 22px; }
.form { display: flex; flex-direction: column; gap: 12px; }
.form .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form .hint { font-size: 12px; font-weight: 400; color: var(--muted); }

.guide ol { padding-left: 20px; }
.guide li { margin-bottom: 6px; }
.guide code, code { background: var(--surface-2); border: 1px solid var(--border); border-radius: 4px; padding: 1px 5px; font-size: 12.5px; }

.toasts { position: fixed; bottom: 16px; right: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 2000; }
.toast { background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); max-width: 360px; }
.toast.error { background: var(--danger); color: #fff; }

@media (max-width: 900px) {
  .topbar { gap: 10px; }
  .logo-top { height: 22px; }
  #user-label { display: none; }
  .view-map { flex-direction: column; }
  .sidebar { width: 100%; max-height: 32vh; border-right: 0; border-bottom: 1px solid var(--border); }
  .map { min-height: 300px; }
  .detail { position: absolute; inset: auto 0 0 0; width: 100%; max-height: 60vh; z-index: 1000; border-left: 0; border-top: 1px solid var(--border); box-shadow: var(--shadow); }
  .page { padding: 16px; }
  .form .grid { grid-template-columns: 1fr; }
}

/* Filtres en ligne dans l'en-tête de page, libellés de type sur une ligne */
.page-head .row select { width: auto; }
.failures-table .badge { white-space: nowrap; }
.failures-table td:first-child { white-space: nowrap; }

/* Utilisateurs : permissions et accès aux trackers */
.badge.role-admin { color: var(--primary); border-color: color-mix(in srgb, var(--primary) 40%, transparent); }
.perm-badges { display: flex; flex-wrap: wrap; gap: 4px; }
.roles-help p { margin: 0 0 6px; }
.roles-help ul { margin: 0 0 8px; padding-left: 20px; }
.modal.wide { width: min(680px, calc(100vw - 32px)); }
.modal .form { max-height: calc(100vh - 48px); overflow-y: auto; }
.user-rights { display: flex; flex-direction: column; gap: 12px; }
.perm-list { border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px 10px; margin: 0; display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.perm-list legend { font-weight: 600; font-size: 13px; padding: 0 4px; }
.perm-list label.perm-item { flex-direction: row; align-items: flex-start; gap: 10px; font-weight: 400; padding: 4px 0; cursor: pointer; }
.perm-list .perm-item input { margin-top: 3px; flex-shrink: 0; }
.perm-list .perm-item > span { display: flex; flex-direction: column; gap: 1px; }
.perm-list .perm-item b { font-weight: 600; }
.device-pick { display: flex; flex-direction: column; gap: 8px; }
.device-pick .row input[type=search] { flex: 1; width: auto; min-width: 160px; }
.device-pick-list { list-style: none; margin: 0; padding: 4px; max-height: 220px; overflow-y: auto; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); }
.device-pick-list li { padding: 2px 6px; border-radius: 6px; }
.device-pick-list li:hover { background: var(--surface); }
.device-pick-list label.inline { font-weight: 400; gap: 8px; padding: 4px 0; cursor: pointer; }
.device-pick-list .mono { margin-left: auto; }
.device-pick .hint { margin: 0; }
.user-rights { margin-top: 4px; }
.modal input:disabled { background: var(--surface-2); color: var(--muted); cursor: not-allowed; }

/* Sélecteur de thème */
.theme-switch { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface); }
.theme-switch button { font: inherit; font-size: 14px; font-weight: 600; line-height: 1; width: 30px; height: 28px; border: 0; background: transparent; color: var(--muted); cursor: pointer; padding: 0; }
.theme-switch button + button { border-left: 1px solid var(--border); }
.theme-switch button:hover { background: var(--surface-2); color: var(--text); }
.theme-switch button[aria-pressed="true"] { background: color-mix(in srgb, var(--primary) 18%, transparent); color: var(--primary); }
.theme-switch button:focus-visible { outline: 2px solid var(--primary); outline-offset: -2px; }
.login-theme { position: fixed; top: 12px; right: 12px; }

/* Logo selon le thème effectif */
.logo-dark { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .logo-light { display: none; }
  :root:not([data-theme="light"]) .logo-dark { display: block; }
}
:root[data-theme="dark"] .logo-light { display: none; }
:root[data-theme="dark"] .logo-dark { display: block; }

/* Thème sombre : composants */
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .modal::backdrop { background: rgba(0, 0, 0, .65); } }
:root[data-theme="dark"] .modal::backdrop { background: rgba(0, 0, 0, .65); }
.toast.error { background: #b91c1c; color: #fff; }
.leaflet-container .leaflet-popup-content-wrapper,
.leaflet-container .leaflet-popup-tip { background: var(--surface); color: var(--text); }
.leaflet-container a { color: var(--primary); }
.leaflet-bar a, .leaflet-control-layers { background-color: var(--surface); color: var(--text); border-color: var(--border); }
.leaflet-bar a:hover { background-color: var(--surface-2); }
.leaflet-control-attribution { background: color-mix(in srgb, var(--surface) 80%, transparent) !important; color: var(--muted); }
.leaflet-control-layers-separator { border-top-color: var(--border); }
input[type=checkbox], input[type=range], input[type=radio] { accent-color: var(--primary); }
::placeholder { color: var(--muted); opacity: .85; }
* { scrollbar-color: var(--border) transparent; }

/* Filtre des tuiles de plan (OSM) en sombre ; satellite inchangé */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .map.basemap-plan .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(.95) contrast(.9); }
}
:root[data-theme="dark"] .map.basemap-plan .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(.95) contrast(.9); }

.page a:not(.btn), .roles-help a { color: var(--primary); }
@media (max-width: 900px) {
  .topbar { flex-wrap: wrap; height: auto; padding: 8px 12px; row-gap: 6px; }
  .brand { order: 1; }
  .user-box { order: 2; margin-left: auto; }
  #nav { order: 3; flex: 1 0 100%; }
}
