:root {
  --bg: #f6f7f9; --surface: #ffffff; --surface-2: #f0f2f5; --border: #e2e5ea;
  --text: #1a1d23; --muted: #626a77; --accent: #2563eb; --accent-ink: #fff;
  --verde: #16a34a; --naranja: #f97316; --rojo: #dc2626; --gris: #9ca3af;
  --t-mensaje: #3b82f6; --t-oferta: #a855f7; --t-visita: #16a34a; --t-otro: #94a3b8;
  --radius: 10px; --shadow: 0 1px 2px rgba(0,0,0,.05), 0 1px 8px rgba(0,0,0,.04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115; --surface: #171a21; --surface-2: #1f232c; --border: #2a2f3a;
    --text: #e7e9ee; --muted: #9aa3b2; --accent: #3b82f6;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1115; --surface: #171a21; --surface-2: #1f232c; --border: #2a2f3a;
  --text: #e7e9ee; --muted: #9aa3b2; --accent: #3b82f6;
}
* { box-sizing: border-box; }
body { margin: 0; font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 16px; margin: 0 0 12px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* layout */
.topbar { display: flex; align-items: center; gap: 16px; padding: 0 20px; height: 54px; background: var(--surface);
  border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 1000; }
.brand { font-weight: 700; color: var(--text); white-space: nowrap; }
.brand span { color: var(--accent); }
.nav { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
.nav a { padding: 7px 11px; border-radius: 8px; color: var(--muted); white-space: nowrap; }
.nav a.active, .nav a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.userbox { display: flex; align-items: center; gap: 10px; white-space: nowrap; }
.avatar { width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 600; font-size: 13px; }
.container { max-width: 1280px; margin: 0 auto; padding: 22px 20px 60px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-5 { grid-template-columns: repeat(5, minmax(0, 1fr)); }
.span-2 { grid-column: span 2; }
@media (max-width: 900px) { .grid-2, .grid-3, .grid-5 { grid-template-columns: 1fr 1fr; } .span-2 { grid-column: span 2; } }
@media (max-width: 560px) { .grid-2, .grid-3, .grid-5 { grid-template-columns: 1fr; } .span-2 { grid-column: auto; }
  .topbar { padding: 0 12px; } .container { padding: 16px 16px 60px; } .userbox .name { display: none; } }

/* kpis */
.kpi { border-left: 4px solid var(--c, var(--border)); }
.kpi .n { font-size: 26px; font-weight: 700; }

/* forms */
label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
input, select, textarea { width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); font: inherit; }
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 35%, transparent); border-color: var(--accent); }
.field { margin-bottom: 12px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.row > .field { flex: 1 1 160px; }
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--rojo); }
.btn.sm { padding: 4px 9px; font-size: 12px; }
.inline { display: inline; }

/* tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; color: var(--muted); font-weight: 600; cursor: default; white-space: nowrap; }
th[data-sort] { cursor: pointer; }
tr.dim td { opacity: .55; }

/* chips */
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 2px 9px; border-radius: 999px; font-size: 12px;
  background: var(--surface-2); border: 1px solid var(--border); white-space: nowrap; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex: none; }
.sem { color: #fff; border: 0; font-weight: 600; }
.sem.gris { color: #1f2937; }
.imp { font-weight: 700; }

/* flash */
.flash { background: color-mix(in srgb, var(--accent) 12%, var(--surface)); border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
  padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; }
.error { background: color-mix(in srgb, var(--rojo) 10%, var(--surface)); border: 1px solid color-mix(in srgb, var(--rojo) 35%, transparent);
  padding: 10px 14px; border-radius: 8px; margin-bottom: 12px; }

/* franja de contactos 12 meses */
.strip { display: grid; grid-template-columns: repeat(12, 1fr); gap: 4px; }
.strip .m { background: var(--surface-2); border-radius: 6px; padding: 6px 4px; min-height: 64px; display: flex; flex-direction: column; align-items: center; gap: 3px; }
.strip .m.empty { outline: 1px dashed var(--border); background: transparent; }
.strip .lbl { font-size: 11px; color: var(--muted); }
.flags { display: flex; flex-wrap: wrap; gap: 2px; justify-content: center; }
.flag { width: 10px; height: 10px; border-radius: 2px; }
.t-mensaje { background: var(--t-mensaje); } .t-oferta { background: var(--t-oferta); }
.t-visita { background: var(--t-visita); } .t-otro { background: var(--t-otro); }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 5px; }

.progress { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.progress > div { height: 100%; background: var(--verde); }
.progress.warn > div { background: var(--naranja); }

.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li { display: flex; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.timeline li:last-child { border-bottom: 0; }
.timeline .ic { width: 28px; height: 28px; border-radius: 8px; display: grid; place-items: center; color: #fff; font-size: 13px; flex: none; }

/* mapa */
#map { height: calc(100vh - 230px); min-height: 420px; border-radius: var(--radius); border: 1px solid var(--border); }
.filters { display: grid; grid-template-columns: repeat(6, minmax(0,1fr)); gap: 10px; align-items: end; margin-bottom: 14px; }
@media (max-width: 900px) { .filters { grid-template-columns: 1fr 1fr; } }
.tabs { display: inline-flex; background: var(--surface-2); border-radius: 9px; padding: 3px; }
.tabs button { border: 0; background: transparent; padding: 6px 14px; border-radius: 7px; cursor: pointer; color: var(--muted); font: inherit; }
.tabs button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.leaflet-popup-content { font: 13px/1.4 system-ui, sans-serif; }
.pin { border-radius: 50%; border: 3px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.35); }
.pin.other { border-style: dashed; opacity: .8; }
.pin.free { opacity: .45; }
.mine-ring { box-shadow: 0 0 0 3px var(--accent); }

.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; }
.hidden { display: none !important; }
.li-row { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; padding: 6px 0; border-bottom: 1px solid var(--border); }
.li-row > div { min-width: 0; }

/* mapa con los tonos de la página */
:root[data-theme="dark"] .base-tiles { filter: invert(1) hue-rotate(180deg) brightness(.85) contrast(.9) saturate(.6); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .base-tiles { filter: invert(1) hue-rotate(180deg) brightness(.85) contrast(.9) saturate(.6); } }
div.leaflet-container { background: var(--surface-2); }
.map-popup { color: #1a1d23; font: 13px/1.45 system-ui, sans-serif; min-width: 200px; }
.map-popup a { color: #2563eb; }
.map-popup form { margin-top: 6px; }
.map-popup .btn { background: #2563eb; color: #fff; border: 0; }
.leaflet-container .leaflet-popup-content-wrapper, .leaflet-container .leaflet-popup-tip { background: var(--surface); color: var(--text); box-shadow: 0 4px 18px rgba(0,0,0,.35); }
.leaflet-container .leaflet-popup .map-popup { color: var(--text); }
.leaflet-container .leaflet-popup .map-popup a { color: var(--accent); }
.leaflet-container a.leaflet-popup-close-button { color: var(--muted); }
.nav { scrollbar-width: none; }
.nav::-webkit-scrollbar { display: none; }

/* tareas */
.task { padding: 10px 0; border-bottom: 1px solid var(--border); }
.task:last-of-type { border-bottom: 0; }
.task.done { opacity: .65; }
.bell { position: relative; }
.bell .badge { position: absolute; top: -6px; right: -6px; background: var(--rojo); color: #fff; border-radius: 99px;
  font-size: 10px; padding: 1px 5px; font-weight: 700; }
.notif { display: flex; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.notif.unread strong { color: var(--accent); }

.nav { overflow: visible; flex-wrap: nowrap; }
.navmenu { position: relative; }
.navmenu summary { list-style: none; padding: 7px 11px; border-radius: 8px; color: var(--muted); cursor: pointer; white-space: nowrap; }
.navmenu summary::-webkit-details-marker { display: none; }
.navmenu[open] summary, .navmenu.active summary, .navmenu summary:hover { background: var(--surface-2); color: var(--text); }
.navmenu-list { position: absolute; top: 38px; left: 0; background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0,0,0,.25); padding: 6px; min-width: 220px; z-index: 1100; display: flex; flex-direction: column; }
.navmenu-list a { padding: 8px 10px; border-radius: 6px; color: var(--text); }
@media (max-width: 1100px) { .nav { overflow-x: auto; } .navmenu-list { position: fixed; top: 54px; left: auto; } }
.liq-bar { margin-bottom: 16px; border-color: color-mix(in srgb, var(--verde) 45%, var(--border)); position: sticky; top: 62px; z-index: 50; }
.btn:disabled { opacity: .45; cursor: not-allowed; filter: none; }

/* pronóstico */
.fc-row { display: grid; grid-template-columns: 76px 1fr 130px; gap: 10px; align-items: center; padding: 7px 0; border-bottom: 1px solid var(--border); }
.fc-label { font-size: 13px; }
.fc-bars { position: relative; height: 18px; background: var(--surface-2); border-radius: 5px; overflow: hidden; }
.fc-bar { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 5px; }
.fc-bar.light { background: color-mix(in srgb, var(--accent) 30%, transparent); }
.fc-bar.strong { background: var(--accent); top: 4px; bottom: 4px; }
.fc-bar.cash { background: var(--verde); }
.fc-bar.late { background: var(--rojo); }
.fc-val { text-align: right; font-size: 13px; }
@media (max-width: 1500px) { .userbox .name { display: none; } .nav a, .navmenu summary { padding: 7px 9px; } }
.mine-row td { background: color-mix(in srgb, var(--accent) 8%, transparent); }
.stops { padding-left: 20px; margin: 10px 0 0; }
.stops li { padding: 6px 0; border-bottom: 1px solid var(--border); }
.stops li > span:first-child { display: inline-block; max-width: 70%; vertical-align: top; }
.stops li > span.row { float: right; }

/* ================================================================ celular y tablet */
.menu-btn, .nav-head, .nav-logout, .nav-backdrop { display: none; }
.grid > * { min-width: 0; }
.card { min-width: 0; }
@media (max-width: 900px) {
  .grid-2, .grid-3, .grid-5 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .span-2 { grid-column: 1 / -1; }
}
@media (max-width: 1100px) {
  .menu-btn { display: inline-grid; place-items: center; width: 38px; height: 38px; border-radius: 9px; border: 1px solid var(--border);
    background: var(--surface); color: var(--text); font-size: 18px; cursor: pointer; flex: none; }
  .topbar { gap: 10px; }
  .brand { flex: 1; }
  .nav { position: fixed; top: 0; left: 0; bottom: 0; width: min(300px, 84vw); z-index: 1300; background: var(--surface);
    border-right: 1px solid var(--border); flex-direction: column; align-items: stretch; gap: 2px; padding: 12px;
    overflow-y: auto; transform: translateX(-105%); transition: transform .2s ease; box-shadow: 4px 0 24px rgba(0,0,0,.3); }
  body.nav-open .nav { transform: none; }
  .nav-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 1250; }
  body.nav-open .nav-backdrop { display: block; }
  .nav a, .navmenu summary { padding: 12px 12px; font-size: 15px; border-radius: 8px; }
  .nav-head { display: flex; gap: 10px; align-items: center; padding: 6px 6px 14px; border-bottom: 1px solid var(--border); margin-bottom: 8px; }
  .nav-logout { display: block; margin-top: auto; color: var(--rojo) !important; }
  .navmenu { position: static; }
  .navmenu summary { color: var(--muted); }
  .navmenu[open] summary, .navmenu summary:hover { background: transparent; }
  .navmenu-list { position: static; box-shadow: none; border: 0; padding: 0 0 0 10px; min-width: 0; background: transparent; }
  .userbox .name, .userbox .logout, .userbox .user-avatar { display: none; }
}
@media (max-width: 700px) {
  body { font-size: 15px; }
  .container { padding: 14px 12px 70px; }
  h1 { font-size: 20px; }
  .page-head { margin-bottom: 12px; }
  .page-head > .row, .page-head > form { width: 100%; }
  .page-head .btn { flex: 1; justify-content: center; }
  .card { padding: 14px; }
  .grid { gap: 12px; }
  .grid-3, .grid-2 { grid-template-columns: minmax(0, 1fr); }
  .grid-5 { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }
  .kpi { padding: 10px 12px; }
  .kpi .n { font-size: 19px; }
  .kpi .small:last-child { display: none; }
  .filters { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) !important; }
  input, select, textarea { font-size: 16px; }              /* evita el zoom automático en iPhone */
  .btn { padding: 10px 14px; }
  .btn.sm { padding: 7px 11px; font-size: 13px; }
  .row > .field { flex: 1 1 100%; }
  .li-row { flex-wrap: wrap; }
  .strip { grid-template-columns: repeat(6, 1fr); }
  #map { height: 62vh !important; min-height: 320px; }
  .liq-bar { position: static; }
  .fc-row { grid-template-columns: 62px 1fr 96px; }

  /* tablas como tarjetas */
  .table-wrap { overflow: visible; }
  table.rtable, table.rtable tbody, table.rtable tr, table.rtable td { display: block; width: 100%; }
  table.rtable thead { display: none; }
  table.rtable tfoot { display: block; }
  table.rtable tfoot tr { display: flex; justify-content: space-between; gap: 8px; padding: 6px 0; }
  table.rtable tfoot th { padding: 2px 0; border: 0; text-align: left !important; }
  table.rtable tbody tr { border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px; margin-bottom: 10px; background: var(--surface); }
  table.rtable tbody tr.dim { opacity: .7; }
  table.rtable td { border: 0; padding: 5px 0; text-align: left !important; display: flex; gap: 10px; align-items: flex-start;
    justify-content: space-between; min-height: 0; }
  table.rtable td::before { content: attr(data-label); color: var(--muted); font-size: 12px; flex: 0 0 38%; padding-top: 2px; }
  table.rtable td:not([data-label])::before, table.rtable td[data-label=""]::before { display: none; }
  table.rtable td > * { max-width: 62%; }
  table.rtable td:empty { display: none; }
  table.rtable td input, table.rtable td select { max-width: 100%; }
  table.rtable td form.row { flex-wrap: wrap; }
  .progress { min-width: 90px; }
}
@media (max-width: 380px) {
  .brand span { display: none; }
  .filters { grid-template-columns: minmax(0, 1fr) !important; }
}
@media (max-width: 700px) {
  table.rtable td { display: grid; grid-template-columns: 36% minmax(0, 1fr); column-gap: 10px; align-items: start; }
  table.rtable td::before { grid-column: 1; grid-row: 1 / span 6; flex: none; }
  table.rtable td > * { grid-column: 2; max-width: none; }
  table.rtable td:not([data-label]), table.rtable td[data-label=""] { grid-template-columns: minmax(0, 1fr); }
  table.rtable td:not([data-label]) > *, table.rtable td[data-label=""] > * { grid-column: 1; }
  table.rtable td[data-label="#"] { display: none; }
}

.quick-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: -6px 0 16px; }
@media (max-width: 700px) {
  .quick-actions { display: grid; grid-template-columns: 1fr 1fr; }
  .quick-actions .btn { justify-content: center; }
}
@media (max-width: 700px) {
  table.rtable td:has(> input, > select, > textarea) { grid-template-columns: minmax(0, 1fr); row-gap: 4px; }
  table.rtable td:has(> input, > select, > textarea)::before { grid-row: 1; padding: 0; }
  table.rtable td:has(> input, > select, > textarea) > * { grid-column: 1; }
  table.rtable td:has(> button:only-child) { justify-items: end; grid-template-columns: minmax(0, 1fr); }
  table.rtable td:has(> button:only-child)::before { display: none; }
}

.filters-toggle { display: none; }
@media (max-width: 700px) {
  .filters-toggle { display: flex; width: 100%; justify-content: center; margin-bottom: 10px; }
  .filters.collapsible { display: none !important; }
  .filters.collapsible.open { display: grid !important; }
}
@media (max-width: 700px) {
  table.rtable td .row, table.rtable td form.row { flex-wrap: wrap !important; }
  table.rtable td .row > input { min-width: 0 !important; flex: 1 1 100%; }
  .li-row .row { flex-wrap: wrap !important; }
}
