:root {
  --bg: #f4f6f8; --surface: #ffffff; --border: #dde2e8; --text: #1b2430; --muted: #6a7686;
  --primary: #1f5fbf; --primary-ink: #ffffff;
  --ok: #1d8a4e; --warn: #c77700; --danger: #c62f2f; --idle: #8a96a6;
  --radius: 8px; --shadow: 0 1px 3px rgba(16,24,40,.08), 0 1px 2px rgba(16,24,40,.04);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11161d; --surface: #1a212b; --border: #2b3542; --text: #e4e9f0; --muted: #93a0b0;
    --primary: #4d8ae8; --ok: #3cb371; --warn: #e39b2d; --danger: #e05656; --idle: #6b7787;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font-size: 14px; display: flex; flex-direction: row; }
a { color: var(--primary); }
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 15px; margin: 0; }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }

/* --- menú lateral */
.sidebar { width: 216px; flex: none; display: flex; flex-direction: column; gap: 18px; padding: 16px 12px;
  background: var(--surface); border-right: 1px solid var(--border); overflow-y: auto; }
.shell { flex: 1; min-width: 0; display: flex; flex-direction: column; overflow-y: auto; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--text); text-decoration: none; font-size: 16px; padding: 4px 8px; }
.brand-mark { width: 14px; height: 14px; border-radius: 50%; background: var(--primary);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--primary) 25%, transparent); display: inline-block; }
.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 6px; text-decoration: none; color: var(--muted); font-weight: 500; }
.nav a svg { width: 18px; height: 18px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.nav a:hover { background: var(--bg); color: var(--text); }
.nav a.on { background: color-mix(in srgb, var(--primary) 12%, transparent); color: var(--primary); }
.who { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 8px 0; border-top: 1px solid var(--border); }
.who-id { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; min-width: 0; }
.who-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 120px; }
.who form { margin: 0; }
.role { font-size: 11px; padding: 2px 8px; border-radius: 999px; background: var(--bg); border: 1px solid var(--border); }
.role-full { color: var(--danger); } .role-escritura { color: var(--warn); } .role-lectura { color: var(--muted); }

/* --- controles */
.btn { font: inherit; padding: 8px 14px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); cursor: pointer; font-weight: 500; }
.btn:hover { border-color: var(--primary); }
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn.small { padding: 4px 10px; font-size: 12.5px; }
.btn-link { background: none; border: none; color: var(--primary); cursor: pointer; font: inherit; padding: 0; }
input, select { font: inherit; padding: 7px 9px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); }
input[type=checkbox] { width: 16px; height: 16px; }
input:focus, select:focus, .btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }

.flash { padding: 10px 20px; font-weight: 500; }
.flash.ok { background: color-mix(in srgb, var(--ok) 14%, var(--surface)); color: var(--ok); }
.flash.err { background: color-mix(in srgb, var(--danger) 14%, var(--surface)); color: var(--danger); }
.badge { font-size: 12px; min-width: 22px; text-align: center; padding: 1px 7px; border-radius: 999px; background: var(--bg); border: 1px solid var(--border); }
.badge.warn:not(:empty):not(.zero) { background: var(--danger); color: #fff; border-color: var(--danger); }

/* --- páginas con tabla */
main { flex: 1; min-height: 0; }
.page { padding: 24px; max-width: 1280px; margin: 0 auto; width: 100%; }
.page-head { margin-bottom: 18px; }
.page-head p { margin: 4px 0 0; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 18px; }
.form-row { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px; }
.form-row h2 { width: 100%; }
.form-row label { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); }
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; background: var(--bg); }
.table th, .table td { padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table tr:last-child td { border-bottom: none; }
.table .empty { text-align: center; color: var(--muted); padding: 32px; white-space: normal; }
.status { font-size: 12px; padding: 2px 8px; border-radius: 999px; font-weight: 600; }
.status-activo { color: var(--ok); background: color-mix(in srgb, var(--ok) 14%, transparent); }
.status-pendiente { color: var(--warn); background: color-mix(in srgb, var(--warn) 14%, transparent); }
.status-inactivo { color: var(--idle); background: color-mix(in srgb, var(--idle) 14%, transparent); }

/* --- login */
.login-body { align-items: center; justify-content: center; }
.login-card { width: min(360px, calc(100% - 32px)); background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 28px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 14px; }
.login-card h1 { display: flex; align-items: center; gap: 10px; }
.login-card p { margin: -10px 0 4px; }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.login-card .flash { border-radius: 6px; padding: 8px 12px; }

/* --- mapa */
.page-mapa, .page-geocercas { display: flex; }
.mapa { position: relative; display: grid; grid-template-columns: 340px 1fr; flex: 1; min-height: 0; }
.panel { display: flex; flex-direction: column; min-height: 0; background: var(--surface); border-right: 1px solid var(--border); }
.panel-sec { display: flex; flex-direction: column; min-height: 0; flex: 1 1 55%; padding: 14px 14px 0; }
.alerts-sec { flex-basis: 45%; border-top: 1px solid var(--border); }
.panel-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.panel-head .btn { margin-left: auto; }
.search { width: 100%; margin-bottom: 8px; }
.dev-list, .alert-list { list-style: none; margin: 0 -14px; padding: 0; overflow-y: auto; flex: 1; }
.dev-list li { display: grid; grid-template-columns: 12px 1fr auto; gap: 4px 10px; align-items: center;
  padding: 9px 14px; cursor: pointer; border-bottom: 1px solid var(--border); }
.dev-list li:hover, .dev-list li.sel { background: color-mix(in srgb, var(--primary) 8%, transparent); }
.dev-list .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dev-list .meta { grid-column: 2 / 4; font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dev-list .batt { font-size: 12px; font-variant-numeric: tabular-nums; }
.dev-list .batt.low { color: var(--danger); font-weight: 600; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.dot-online, .pin-online { background: var(--ok); }
.dot-stale, .pin-stale { background: var(--idle); }
.dot-pendiente, .pin-pendiente { background: var(--warn); }
.dot-inactivo, .pin-inactivo { background: transparent; border: 2px solid var(--idle); }
.alert-list li { padding: 10px 14px; border-bottom: 1px solid var(--border); border-left: 3px solid var(--danger); cursor: pointer; }
.alert-list li.bateria_baja { border-left-color: var(--warn); }
.alert-list .a-top { display: flex; justify-content: space-between; gap: 8px; }
.alert-list .a-msg { margin: 2px 0 6px; }
.alert-list .a-time { font-size: 12px; color: var(--muted); }
.empty-li { color: var(--muted); padding: 14px !important; border: none !important; cursor: default !important; }

.map { min-height: 0; background: var(--bg); }
.pin { display: block; width: 18px; height: 18px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.45); }
.pin.pin-inactivo { background: #fff; }
.leaflet-popup-content { font-size: 13px; line-height: 1.5; }
.leaflet-popup-content b { display: block; font-size: 14px; margin-bottom: 2px; }

.conn { position: absolute; right: 12px; bottom: 26px; z-index: 500; font-size: 12px; padding: 4px 10px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow); }
.conn::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; background: var(--ok); }
.conn.off::before { background: var(--danger); }

.toasts { position: fixed; top: 16px; right: 16px; z-index: 1000; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--danger); border-radius: 8px;
  padding: 10px 14px; box-shadow: 0 6px 20px rgba(0,0,0,.18); max-width: 320px; animation: slide .2s ease-out; }
@keyframes slide { from { transform: translateX(20px); opacity: 0; } }

@media (max-width: 800px) {
  body { flex-direction: column; }
  .sidebar { width: auto; flex-direction: row; align-items: center; gap: 8px; padding: 6px 10px; overflow: visible;
    border-right: none; border-bottom: 1px solid var(--border); }
  .brand { font-size: 0; gap: 0; padding: 4px; }
  .nav { flex-direction: row; overflow-x: auto; scrollbar-width: none; min-width: 0; }
  .nav a { padding: 8px; gap: 6px; white-space: nowrap; }
  .nav a:not(.on) span { display: none; }
  .who { border-top: none; padding: 0; }
  .who-name { display: none; }
  .mapa { grid-template-columns: 1fr; grid-template-rows: 55% 45%; }
  .panel { order: 2; border-right: none; border-top: 1px solid var(--border); }
  .page { padding: 16px; }
}

/* --- expedientes */
code { font-family: ui-monospace, Menlo, monospace; font-size: 12.5px; background: var(--bg); padding: 1px 5px; border-radius: 4px; }
.small { font-size: 12.5px; }
.grow { flex: 1 1 260px; }
.inline { display: inline; margin: 0; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.stack label, .form-row label { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); }
.form-row label.check { flex-direction: row; align-items: center; gap: 6px; color: var(--text); padding-bottom: 8px; }
textarea { font: inherit; padding: 8px 9px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--text); resize: vertical; }
details.card > summary { cursor: pointer; }
details.card[open] > summary { margin-bottom: 14px; }
.searchbar { display: flex; gap: 8px; margin-bottom: 14px; }
.searchbar input { flex: 1; }
a.btn { text-decoration: none; display: inline-block; }
.btn.danger { color: var(--danger); }
.btn.danger:hover { border-color: var(--danger); }
.btn.on { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 50%, var(--border)); }
.rowlink { cursor: pointer; }
.rowlink:hover td { background: color-mix(in srgb, var(--primary) 5%, transparent); }
.avatar-cell { width: 44px; }
.avatar { width: 32px; height: 32px; border-radius: 50%; object-fit: cover; display: inline-grid; place-items: center; vertical-align: middle; }
.avatar.ph { background: color-mix(in srgb, var(--primary) 15%, var(--surface)); color: var(--primary); font-weight: 700; }
.exp-grid { display: grid; grid-template-columns: 220px 1fr 300px; gap: 18px; align-items: start; }
.exp-grid .card { margin-bottom: 0; }
.exp-grid + .card { margin-top: 18px; }
.exp-photo img, .photo-ph { width: 100%; aspect-ratio: 3 / 4; object-fit: cover; border-radius: 6px; margin-bottom: 10px; display: block; }
.photo-ph { display: grid; place-items: center; background: var(--bg); color: var(--muted); }
.card h2 { margin-bottom: 12px; display: flex; align-items: center; gap: 8px; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 14px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.pre { white-space: pre-wrap; }
.upload { padding-bottom: 14px; margin-bottom: 14px; border-bottom: 1px solid var(--border); }
.table-wrap.flat { box-shadow: none; }
.tag { font-size: 11.5px; padding: 2px 7px; border-radius: 4px; background: var(--bg); border: 1px solid var(--border); }
.tag-pdf { color: var(--danger); }
.tag-imagen { color: var(--primary); }
.actions { text-align: right; }
.actions > * { margin-left: 4px; }
.danger-zone summary { color: var(--danger); font-weight: 600; }
.danger-zone p { margin: 0; }

/* --- mensajería */
.page-mensajes { display: flex; }
.chat { display: grid; grid-template-columns: 320px 1fr; flex: 1; min-height: 0; }
.chat-list { display: flex; flex-direction: column; min-height: 0; background: var(--surface); border-right: 1px solid var(--border); padding: 14px 14px 0; }
.chat-list ul { list-style: none; margin: 0 -14px; padding: 0; overflow-y: auto; flex: 1; }
.chat-list li { padding: 10px 14px; border-bottom: 1px solid var(--border); cursor: pointer; display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; }
.chat-list li:hover, .chat-list li.sel { background: color-mix(in srgb, var(--primary) 8%, transparent); }
.chat-list .c-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-list .c-time { font-size: 11.5px; color: var(--muted); }
.chat-list .c-last { font-size: 12.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-list .c-unread { background: var(--primary); color: #fff; border-radius: 999px; font-size: 11px; padding: 0 7px; font-weight: 700; justify-self: end; }
.chat-main { display: flex; flex-direction: column; min-height: 0; }
.chat-head { padding: 12px 18px; background: var(--surface); border-bottom: 1px solid var(--border); }
.chat-head b { color: var(--text); }
.chat-msgs { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: min(560px, 80%); padding: 8px 12px; border-radius: 12px; background: var(--surface); border: 1px solid var(--border); align-self: flex-start; }
.msg.portal { align-self: flex-end; background: color-mix(in srgb, var(--primary) 14%, var(--surface)); border-color: color-mix(in srgb, var(--primary) 30%, var(--border)); }
.msg.aviso { border-left: 4px solid var(--warn); }
.msg .m-text { white-space: pre-wrap; word-wrap: break-word; }
.msg .m-meta { font-size: 11px; color: var(--muted); margin-top: 4px; text-align: right; }
.msg .m-kind { font-size: 10.5px; font-weight: 700; color: var(--warn); text-transform: uppercase; letter-spacing: .05em; }
.chat-form { display: flex; gap: 8px; padding: 12px 18px; background: var(--surface); border-top: 1px solid var(--border); align-items: flex-end; }
.chat-form textarea { flex: 1; }
.chat-empty { margin: auto; color: var(--muted); }

@media (max-width: 1000px) { .exp-grid { grid-template-columns: 1fr 1fr; } .exp-photo { grid-row: span 2; } }
@media (max-width: 800px) {
  .exp-grid { grid-template-columns: 1fr; }
  .chat { grid-template-columns: 1fr; grid-template-rows: 40% 60%; }
  .chat-list { border-right: none; border-bottom: 1px solid var(--border); }
}

/* --- geocercas */
.hint { margin: -4px 0 8px; }
.geo-list { list-style: none; margin: 0 -14px; padding: 0; overflow-y: auto; flex: 1; }
.geo-list li { display: grid; grid-template-columns: 14px 1fr; gap: 2px 10px; align-items: center; padding: 9px 14px; border-bottom: 1px solid var(--border); cursor: pointer; }
.geo-list li:hover, .geo-list li.sel { background: color-mix(in srgb, var(--primary) 8%, transparent); }
.geo-list .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.geo-list .meta { grid-column: 2; font-size: 12px; color: var(--muted); }
.sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; border: 2px solid; }
.sw-ok { border-color: #1d8a4e; background: rgba(29,138,78,.25); }
.sw-bad { border-color: #c62f2f; background: rgba(198,47,47,.25); }
.sw-off { border-color: #8a96a6; border-style: dashed; }
.geo-editor { flex: 0 1 auto; max-height: 65%; overflow-y: auto; border-top: 1px solid var(--border); padding-bottom: 14px; }
.check-row { display: flex !important; flex-direction: row !important; align-items: center; gap: 6px; color: var(--text) !important; font-size: 13px !important; }
.dev-pick { border: 1px solid var(--border); border-radius: 6px; padding: 8px 10px; margin: 0; }
.dev-pick legend { font-size: 12.5px; color: var(--muted); padding: 0 4px; }
.dev-pick #ed-devs { max-height: 170px; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }
.ed-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.legend { position: absolute; left: 352px; bottom: 26px; z-index: 500; display: flex; gap: 12px; font-size: 12px; padding: 5px 10px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 999px; box-shadow: var(--shadow); }
.legend span { display: flex; align-items: center; gap: 5px; }
.toast.ok { border-left-color: var(--ok); }
@media (max-width: 800px) { .legend { left: 12px; bottom: auto; top: 12px; } }
.alert-list li.sin_reporte { border-left-color: var(--idle); }

/* --- historial */
.page-historial { display: flex; }
.hist-panel { overflow-y: auto; }
.hist-panel .panel-sec { flex: none; padding-bottom: 14px; }
.hist-form { gap: 10px; border-bottom: 1px solid var(--border); }
.hist-form label { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); }
.hist-form .two { display: grid; grid-template-columns: 1fr; gap: 8px; }
.hist-form .two input { min-width: 0; font-size: 13px; }
.quick { display: flex; gap: 6px; flex-wrap: wrap; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.stat { background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 6px 10px; display: flex; flex-direction: column; }
.stat b { font-size: 15px; font-variant-numeric: tabular-nums; }
.stats .full { grid-column: 1 / -1; margin: 0; }
.player { display: flex; align-items: center; gap: 8px; }
.player input[type=range] { flex: 1; padding: 0; }
.player select { padding: 4px 6px; font-size: 12.5px; }
.p-time { margin: 6px 0 10px; min-height: 1.2em; }
.export { display: flex; align-items: center; gap: 6px; }
.hist-result h3 { font-size: 14px; margin: 4px 0 8px; display: flex; align-items: center; gap: 8px; }
.h-list { list-style: none; margin: 0 -14px 12px; padding: 0; }
.h-list li { padding: 7px 14px; border-bottom: 1px solid var(--border); cursor: pointer; font-size: 13px; }
.h-list li:hover { background: color-mix(in srgb, var(--primary) 8%, transparent); }
.h-list li[class^="al-"] { border-left: 3px solid var(--danger); }
.h-list li.al-bateria_baja { border-left-color: var(--warn); }
.h-list li.al-sin_reporte { border-left-color: var(--idle); }
.hpin { display: grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; color: #fff; font-size: 12px; font-weight: 700; border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.4); }
.hpin.start { background: #1d8a4e; }
.hpin.end { background: #1b2430; }
.hpin.stop { background: #c77700; }
.popup-links { margin-top: 6px; padding-top: 6px; border-top: 1px solid #ddd; }

/* --- auditoría */
.page.wide { max-width: 1500px; }
.head-row { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; flex-wrap: wrap; }
.filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.filters label { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); }
.filters label.narrow input { width: 80px; }
.filter-actions { display: flex; gap: 6px; }
.table.audit td { vertical-align: top; }
.table td.wrap { white-space: normal; word-break: break-word; min-width: 260px; }
.act { font-family: ui-monospace, Menlo, monospace; font-size: 11.5px; padding: 2px 6px; border-radius: 4px; background: var(--bg); border: 1px solid var(--border); }
.act-danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); }
.act-read { color: var(--muted); }
.act-sec { color: var(--warn); }
.act-write { color: var(--primary); }
.act-sys { color: var(--ok); }
.actor-dispositivo { font-family: ui-monospace, Menlo, monospace; font-size: 12px; }
.actor-dispositivo::before { content: "📱 "; }
.actor-sistema { color: var(--ok); }
.pager { text-align: center; margin-top: 14px; }
