:root {
  --brand: #f07c2a;
  --brand-dark: #c95f14;
  --bg: #f6f6f4;
  --surface: #ffffff;
  --text: #1c1d1f;
  --muted: #6b6f76;
  --border: #e3e3df;
  --soft: #f1f1ee;
  --danger: #d64545;
  --ok: #1f9d55;
  --radius: 10px;
  --shadow: 0 6px 24px rgba(0,0,0,.12);
  --topbar-h: 56px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
body { overflow: hidden; font-size: 14px; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--brand-dark); }

/* ---------- Top bar ---------- */
.topbar {
  height: var(--topbar-h); display: flex; align-items: center; gap: 12px;
  padding: 0 12px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: relative; z-index: 1001;
}
.brand { display: flex; align-items: center; gap: 8px; min-width: 0; }
.brand svg { width: 26px; height: 26px; color: var(--brand); flex: none; }
.brand strong { display: block; font-size: 15px; line-height: 1.1; }
.brand span { display: block; font-size: 11.5px; color: var(--muted); white-space: nowrap; }
.search { flex: 1; min-width: 0; }
.search input {
  width: 100%; height: 38px; border: 1px solid var(--border); border-radius: 999px;
  padding: 0 14px; background: var(--soft); outline: none;
}
.search input:focus { border-color: var(--brand); background: #fff; }
.top-actions { position: relative; display: flex; gap: 6px; }
.btn {
  height: 38px; padding: 0 14px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--soft); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; font-weight: 600; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.ghost { border-color: transparent; }
.btn.small { height: 32px; padding: 0 10px; font-size: 13px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.link { background: none; border: 0; color: var(--brand-dark); cursor: pointer; padding: 0; font-size: 12.5px; }
.menu {
  position: absolute; right: 0; top: 44px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); min-width: 260px; padding: 6px; display: flex; flex-direction: column;
}
.menu[hidden] { display: none; }
.menu button, .menu a {
  text-align: left; padding: 10px 12px; border: 0; background: none; border-radius: 6px; cursor: pointer;
  color: var(--text); text-decoration: none;
}
.menu button:hover, .menu a:hover { background: var(--soft); }
.only-mobile { display: none; }

/* ---------- Layout ---------- */
.layout { display: grid; grid-template-columns: 360px 1fr; height: calc(100% - var(--topbar-h)); position: relative; overflow: hidden; }
.panel-lista { background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.mapa-wrap { position: relative; min-width: 0; }
#map { position: absolute; inset: 0; }

/* ---------- Stats ---------- */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; padding: 10px; border-bottom: 1px solid var(--border); }
.stat {
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 4px; text-align: center; cursor: pointer;
  background: var(--surface); position: relative; user-select: none;
}
.stat b { display: block; font-size: 17px; font-variant-numeric: tabular-nums; }
.stat span { font-size: 10.5px; color: var(--muted); display: block; line-height: 1.15; }
.stat::before { content: ''; position: absolute; left: 6px; right: 6px; top: 0; height: 3px; border-radius: 0 0 3px 3px; background: var(--c); }
.stat.off { opacity: .4; }
.stat.total { grid-column: span 1; cursor: default; }
.stat.total::before { display: none; }

/* ---------- Filtros ---------- */
.filtros { padding: 8px 10px; border-bottom: 1px solid var(--border); display: grid; gap: 8px; }
.filtro-grupo label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; display: block; margin-bottom: 4px; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip {
  border: 1px solid var(--border); border-radius: 999px; padding: 3px 10px; font-size: 12px; cursor: pointer;
  background: var(--surface); user-select: none;
}
.chip.on { background: #1c1d1f; color: #fff; border-color: #1c1d1f; }
.chip small { opacity: .7; margin-left: 2px; }
.filtro-row { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.filtro-row select, .form select, .form input, .form textarea {
  width: 100%; height: 34px; border: 1px solid var(--border); border-radius: 8px; padding: 0 8px; background: #fff;
}
.lista-head { display: flex; justify-content: space-between; align-items: center; padding: 8px 12px; font-size: 12px; color: var(--muted); }

/* ---------- Lista ---------- */
.lista { list-style: none; margin: 0; padding: 0 6px 12px; overflow-y: auto; flex: 1; }
.item {
  display: grid; grid-template-columns: 12px 1fr auto; gap: 8px; align-items: start; padding: 9px 8px;
  border-radius: 8px; cursor: pointer; border: 1px solid transparent;
}
.item:hover { background: var(--soft); }
.item.sel { border-color: var(--brand); background: #fff7f0; }
.dot { width: 12px; height: 12px; border-radius: 50%; margin-top: 3px; background: var(--c); box-shadow: 0 0 0 2px #fff, 0 0 0 3px var(--c); }
.item .nom { font-weight: 600; font-size: 13.5px; line-height: 1.25; }
.item .dir { color: var(--muted); font-size: 12px; margin-top: 1px; }
.item .tags { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 4px; }
.item .meta { text-align: right; font-size: 11px; color: var(--muted); white-space: nowrap; }
.tag { font-size: 10.5px; padding: 1px 7px; border-radius: 999px; background: var(--soft); color: #444; border: 1px solid var(--border); white-space: nowrap; }
.tag.prev-visitado { background: #fff1e0; border-color: #f5c58e; color: #8a4b0b; }
.tag.prev-sin_gestion { background: #eef4ff; border-color: #c3d4fb; color: #2547a0; }
.tag.prev-agendado { background: #eefaf3; border-color: #b9e4c9; color: #1c6b3c; }
.tag.prev-seguimiento { background: #f4f0ff; border-color: #d8cbfb; color: #5432a8; }
.tag.warn { background: #fff7d6; border-color: #f1d97a; color: #7a5d00; }
.badge { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; color: #fff; background: var(--c); }

/* ---------- Marcadores ---------- */
.pin { width: 22px; height: 22px; border-radius: 50%; background: var(--c); border: 2.5px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.45); position: relative; }
.pin.prev::after { content: ''; position: absolute; inset: 4px; border-radius: 50%; background: #fff; opacity: .95; }
.pin.aprox { border-style: dashed; }
.pin.sel { transform: scale(1.35); box-shadow: 0 0 0 4px rgba(240,124,42,.55), 0 1px 4px rgba(0,0,0,.45); }
.me-dot { width: 16px; height: 16px; border-radius: 50%; background: #2f6fed; border: 3px solid #fff; box-shadow: 0 0 0 6px rgba(47,111,237,.25); }
.marker-cluster-custom { background: rgba(28,29,31,.82); color: #fff; border-radius: 50%; display: grid; place-items: center; font-weight: 700; border: 3px solid rgba(255,255,255,.85); box-shadow: 0 1px 6px rgba(0,0,0,.35); }

.leyenda {
  position: absolute; left: 10px; bottom: 18px; z-index: 500; background: rgba(255,255,255,.95); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 8px 10px; font-size: 11.5px; display: grid; gap: 3px; box-shadow: var(--shadow);
}
.leyenda .row { display: flex; align-items: center; gap: 6px; }
.leyenda .pin { width: 14px; height: 14px; border-width: 2px; }
.leyenda .pin.prev::after { inset: 3px; }

/* ---------- Detalle ---------- */
.detalle {
  position: absolute; top: 0; right: 0; bottom: 0; width: 440px; max-width: 100%; background: var(--surface);
  border-left: 1px solid var(--border); box-shadow: var(--shadow); z-index: 1000; transform: translateX(105%);
  transition: transform .22s ease; display: flex; flex-direction: column;
}
.detalle.open { transform: none; }
.detalle-inner { overflow-y: auto; flex: 1; padding-bottom: 40px; }
.det-head { padding: 14px 16px 10px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); z-index: 2; }
.det-head h2 { margin: 0; font-size: 18px; line-height: 1.2; padding-right: 36px; }
.det-head .sub { color: var(--muted); font-size: 12.5px; margin-top: 3px; }
.det-head .close { position: absolute; right: 10px; top: 10px; width: 34px; height: 34px; border-radius: 50%; border: 0; background: var(--soft); cursor: pointer; font-size: 18px; }
.det-head .badges { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.acciones { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; padding: 10px 16px; border-bottom: 1px solid var(--border); }
.acciones a, .acciones button {
  display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 2px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); text-decoration: none; color: var(--text); font-size: 11.5px; cursor: pointer;
}
.acciones a:hover, .acciones button:hover { background: var(--soft); }
.acciones .ico { font-size: 18px; line-height: 1; }
.cta { padding: 12px 16px; border-bottom: 1px solid var(--border); }
.cta .btn { width: 100%; height: 44px; font-size: 15px; }
.prev-box { margin: 12px 16px 0; padding: 10px 12px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--soft); font-size: 13px; }
.prev-box.visitado { background: #fff4e8; border-color: #f5c58e; }
.prev-box.sin_gestion { background: #eef4ff; border-color: #c3d4fb; }
.prev-box b { display: block; margin-bottom: 2px; }
.prev-box ul { margin: 6px 0 0; padding-left: 18px; }
.sec { padding: 12px 16px 4px; }
.sec h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 8px; display: flex; justify-content: space-between; align-items: center; }
.kv { display: grid; grid-template-columns: 120px 1fr; gap: 5px 10px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }
.copy { cursor: pointer; border-bottom: 1px dashed #bbb; }
.alerta { margin: 8px 0 4px; padding: 8px 10px; border-radius: 8px; background: #fff7d6; border: 1px solid #f1d97a; font-size: 12.5px; }

/* Timeline */
.timeline { list-style: none; margin: 0; padding: 0; position: relative; }
.timeline::before { content: ''; position: absolute; left: 7px; top: 4px; bottom: 4px; width: 2px; background: var(--border); }
.tl { position: relative; padding: 0 0 12px 24px; font-size: 13px; }
.tl::before { content: ''; position: absolute; left: 2px; top: 4px; width: 12px; height: 12px; border-radius: 50%; background: #fff; border: 2px solid #b9bcc2; }
.tl.comment::before { background: var(--brand); border-color: var(--brand); }
.tl.visita::before { background: #e8a400; border-color: #e8a400; }
.tl.campo::before { background: var(--c, #555); border-color: var(--c, #555); }
.tl .who { font-weight: 600; }
.tl .when { color: var(--muted); font-size: 11.5px; }
.tl .msg { margin-top: 2px; white-space: pre-wrap; word-break: break-word; }
.tl.action .msg { color: #555; }
.tl.anulada { opacity: .5; text-decoration: line-through; }
.tl .fotos { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.tl .fotos img { width: 72px; height: 72px; object-fit: cover; border-radius: 6px; border: 1px solid var(--border); }
.tl .fotos a.file { font-size: 12px; }
.tl .datos { margin-top: 4px; font-size: 12.5px; background: var(--soft); border-radius: 6px; padding: 6px 8px; }
.tl .datos div { margin: 1px 0; }
.toggle-actions { font-size: 12px; }

.tabla-mini { width: 100%; border-collapse: collapse; font-size: 12px; }
.tabla-mini td, .tabla-mini th { border-bottom: 1px solid var(--border); padding: 5px 4px; text-align: left; vertical-align: top; }
.tabla-mini th { color: var(--muted); font-weight: 500; }

/* ---------- Dialog / forms ---------- */
.dlg { border: 0; border-radius: 14px; padding: 0; width: min(520px, 96vw); max-height: 92vh; box-shadow: var(--shadow); }
.dlg::backdrop { background: rgba(0,0,0,.45); }
.dlg form { display: flex; flex-direction: column; max-height: 92vh; }
.dlg header { padding: 14px 16px; border-bottom: 1px solid var(--border); }
.dlg header h3 { margin: 0; font-size: 17px; }
.dlg header p { margin: 2px 0 0; color: var(--muted); font-size: 12.5px; }
.dlg .body { padding: 14px 16px; overflow-y: auto; display: grid; gap: 12px; }
.dlg footer { padding: 12px 16px; border-top: 1px solid var(--border); display: flex; gap: 8px; justify-content: flex-end; }
.estado-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.estado-btn {
  text-align: left; padding: 10px 12px; border-radius: 10px; border: 1.5px solid var(--border); background: #fff; cursor: pointer;
  border-left: 6px solid var(--c);
}
.estado-btn:hover { background: var(--soft); }
.estado-btn b { display: block; font-size: 14px; }
.estado-btn span { display: block; font-size: 11.5px; color: var(--muted); margin-top: 2px; }
.form .campo label { display: block; font-size: 12.5px; font-weight: 600; margin-bottom: 4px; }
.form .campo label .req { color: var(--danger); }
.form textarea { height: 84px; padding: 8px; resize: vertical; }
.form input[type=text], .form input[type=date] { height: 38px; }
.form select { height: 38px; }
.multi { display: flex; flex-wrap: wrap; gap: 6px; }
.multi label { font-weight: 400 !important; display: inline-flex !important; align-items: center; gap: 4px; border: 1px solid var(--border); border-radius: 999px; padding: 4px 10px; cursor: pointer; margin: 0 !important; }
.multi input { margin: 0; }
.fotos-input { border: 1.5px dashed var(--border); border-radius: 10px; padding: 12px; text-align: center; }
.fotos-input .prev { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; margin-top: 8px; }
.fotos-input .prev img { width: 64px; height: 64px; object-fit: cover; border-radius: 6px; }
.err { color: var(--danger); font-size: 13px; min-height: 1em; }
.gps { font-size: 12px; color: var(--muted); }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #1c1d1f; color: #fff; padding: 10px 16px;
  border-radius: 10px; z-index: 3000; box-shadow: var(--shadow); font-size: 14px; max-width: 90vw;
}

/* ---------- Mobile ---------- */
@media (max-width: 820px) {
  .brand > div { display: none; }
  .topbar { gap: 8px; padding: 0 8px; }
  .btn.ghost { padding: 0 8px; }
  .only-mobile { display: inline-flex; }
  .layout { grid-template-columns: 1fr; }
  .panel-lista {
    position: absolute; inset: 0; z-index: 900; transform: translateY(100%); transition: transform .22s ease;
  }
  .panel-lista.open { transform: none; }
  .detalle { width: 100%; top: auto; height: 88%; border-radius: 16px 16px 0 0; transform: translateY(105%); border-left: 0; }
  .detalle.open { transform: none; }
  .leyenda { bottom: 10px; font-size: 10.5px; padding: 6px 8px; }
  .stats { grid-template-columns: repeat(4, 1fr); }
  .estado-grid { grid-template-columns: 1fr; }
  .kv { grid-template-columns: 105px 1fr; }
}
