:root {
  --bg: #f1f5f9;
  --panel: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --brand: #2563eb;
  --brand-ink: #1d4ed8;
  --ok: #16a34a;
  --warn: #d97706;
  --danger: #dc2626;
  --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 1px 2px rgba(15, 23, 42, .04);
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--ink); font-size: 14px;
}
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Layout */
.app { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.sidebar {
  background: #0b1220; color: #cbd5e1; padding: 18px 14px;
  display: flex; flex-direction: column; gap: 4px;
}
.sidebar .brand { color: #fff; font-weight: 700; font-size: 16px; padding: 6px 10px 16px; display: flex; align-items: center; gap: 8px; }
.sidebar a.nav {
  color: #cbd5e1; padding: 9px 12px; border-radius: 8px; display: flex; gap: 10px; align-items: center;
}
.sidebar a.nav:hover { background: #1e293b; text-decoration: none; }
.sidebar a.nav.active { background: var(--brand); color: #fff; }
.sidebar .spacer { flex: 1; }
.sidebar .who { font-size: 12px; color: #94a3b8; padding: 8px 12px; border-top: 1px solid #1e293b; margin-top: 8px; }

.main { display: flex; flex-direction: column; min-width: 0; }
.topbar {
  background: var(--panel); border-bottom: 1px solid var(--border);
  padding: 12px 20px; display: flex; align-items: center; justify-content: space-between;
}
.topbar h1 { font-size: 17px; margin: 0; }
.content { padding: 20px; }

/* Dashboard metrics */
.metric-cards {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}
.metric-card {
  --metric: var(--brand);
  --metric-soft: #eff6ff;
  position: relative;
  min-height: 132px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  padding: 14px;
  border: 1px solid color-mix(in srgb, var(--metric) 18%, var(--border));
  border-radius: 14px;
  background: linear-gradient(135deg, var(--metric-soft) 0%, #fff 56%, #fff 100%);
  box-shadow: 0 10px 24px rgba(15, 23, 42, .06), 0 2px 5px rgba(15, 23, 42, .03);
  transition: transform .18s ease, box-shadow .18s ease, border-color .18s ease;
}
.metric-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 14px;
  width: 42px;
  height: 3px;
  border-radius: 0 0 999px 999px;
  background: var(--metric);
}
.metric-card::after {
  content: '';
  position: absolute;
  right: -30px;
  bottom: -42px;
  width: 112px;
  height: 112px;
  border-radius: 50%;
  background: color-mix(in srgb, var(--metric) 9%, transparent);
  pointer-events: none;
}
.metric-card:hover {
  transform: translateY(-3px);
  border-color: color-mix(in srgb, var(--metric) 34%, var(--border));
  box-shadow: 0 16px 32px rgba(15, 23, 42, .10), 0 3px 8px rgba(15, 23, 42, .04);
}
.metric-card__top,
.metric-card__content,
.metric-card__footer { position: relative; z-index: 1; }
.metric-card__top { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.metric-card__icon {
  width: 36px;
  height: 36px;
  flex: 0 0 36px;
  overflow: hidden;
  display: inline-grid;
  place-items: center;
  border-radius: 11px;
  color: var(--metric);
  background: color-mix(in srgb, var(--metric) 14%, #fff);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--metric) 14%, transparent);
}
.metric-card__icon svg { width: 21px; height: 21px; max-width: 100%; max-height: 100%; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.metric-card__state {
  padding: 4px 8px;
  border-radius: 999px;
  color: color-mix(in srgb, var(--metric) 78%, #0f172a);
  background: color-mix(in srgb, var(--metric) 10%, #fff);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .055em;
  text-transform: uppercase;
}
.metric-card__content { margin-top: 10px; }
.metric-card__label { margin: 0; color: #475569; font-size: 11px; font-weight: 650; line-height: 1.25; letter-spacing: .025em; }
.metric-card__value { display: block; margin-top: 4px; color: var(--ink); font-size: clamp(27px, 1.65vw, 34px); font-weight: 760; letter-spacing: -.045em; line-height: 1; }
.metric-card__footer { display: flex; align-items: end; justify-content: space-between; gap: 10px; margin-top: auto; }
.metric-card__detail { margin: 0; color: #64748b; font-size: 11px; line-height: 1.25; }
.metric-card__link { flex: 0 0 auto; font-size: 11px; font-weight: 750; color: var(--metric); white-space: nowrap; }
.metric-card__link:hover { text-decoration: none; filter: brightness(.82); }
.metric-card--success { --metric: #16a34a; --metric-soft: #f0fdf4; }
.metric-card--danger { --metric: #dc2626; --metric-soft: #fff1f2; }
.metric-card--warning { --metric: #d97706; --metric-soft: #fffbeb; }
.metric-card--primary { --metric: #2563eb; --metric-soft: #eff6ff; }
.metric-card--violet { --metric: #7c3aed; --metric-soft: #f5f3ff; }
.metric-card--teal { --metric: #0f9488; --metric-soft: #f0fdfa; }
.metric-card--indigo { --metric: #4f46e5; --metric-soft: #eef2ff; }
.metric-card--sky { --metric: #0284c7; --metric-soft: #f0f9ff; }

.panel { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); margin-bottom: 18px; }
.panel .head { padding: 12px 16px; border-bottom: 1px solid var(--border); font-weight: 600; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.panel .body { padding: 16px; }

/* Filters */
.filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field label { font-size: 12px; color: var(--muted); }
input, select, textarea {
  font: inherit; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: #fff; color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: 2px solid #bfdbfe; border-color: var(--brand); }

.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border-radius: 8px; border: 1px solid var(--brand); background: var(--brand); color: #fff; cursor: pointer; font: inherit; }
.btn:hover { background: var(--brand-ink); }
.btn.ghost { background: #fff; color: var(--brand); }
.btn.ghost:hover { background: #eff6ff; }
.btn.sm { padding: 5px 10px; font-size: 13px; }
.btn.danger { background: var(--danger); border-color: var(--danger); }

/* Map & calendar */
#map { height: 460px; width: 100%; border-radius: 0 0 12px 12px; }
#calendar { --fc-border-color: var(--border); }

/* Table */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
tbody tr:hover { background: #f8fafc; }
.thumb { width: 44px; height: 44px; object-fit: cover; border-radius: 6px; border: 1px solid var(--border); cursor: pointer; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; background: #e0e7ff; color: #3730a3; }
.badge.venta { background: #dcfce7; color: #166534; }
.badge.no_contacto { background: #fee2e2; color: #991b1b; }
.badge.seguimiento { background: #fef3c7; color: #92400e; }

/* Facturación: estado del plan y del pago */
.badge.plan-active { background: #dcfce7; color: #166534; }
.badge.plan-expiring { background: #fef3c7; color: #92400e; }
.badge.plan-expired { background: #fee2e2; color: #991b1b; }
.badge.plan-none { background: #e2e8f0; color: #475569; }
.badge.plan-exempt { background: #e0e7ff; color: #3730a3; }

/* Login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; background: linear-gradient(135deg, #0b1220, #1e3a8a); }
.login-card { background: #fff; border-radius: 16px; padding: 32px; width: 360px; box-shadow: 0 20px 40px rgba(2,6,23,.35); }
.login-card h1 { margin: 0 0 4px; font-size: 20px; }
.login-card p { margin: 0 0 20px; color: var(--muted); }
.login-card .field { margin-bottom: 14px; }
.login-card input { width: 100%; }
.login-card .btn { width: 100%; justify-content: center; }
.error { background: #fee2e2; color: #991b1b; padding: 10px 12px; border-radius: 8px; margin-bottom: 14px; font-size: 13px; }
.muted { color: var(--muted); }

/* Modal */
.modal-bg { position: fixed; inset: 0; background: rgba(2,6,23,.5); display: none; place-items: center; z-index: 50; padding: 16px; }
.modal-bg.open { display: grid; }
.modal { background: #fff; border-radius: 14px; width: 100%; max-width: 520px; max-height: 90vh; overflow: auto; }
.modal .head { padding: 14px 18px; border-bottom: 1px solid var(--border); font-weight: 600; }
.modal .body { padding: 18px; display: grid; gap: 12px; }
.modal .foot { padding: 14px 18px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.maplibregl-popup-content { font-family: inherit; }
.popup img { width: 100%; border-radius: 6px; margin-top: 6px; }
.detail-row { padding: 4px 0; border-bottom: 1px solid var(--border); }
tbody tr.clickable { cursor: pointer; }

/* Detalle de evento */
.vdetail { display: flex; flex-direction: column; gap: 10px; }
.vdetail-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.vdetail-title { font-size: 18px; font-weight: 700; }
.vdetail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.vdetail-cell { background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; }
.vdetail-k { font-size: 12px; color: var(--muted); }
.vdetail-v { font-weight: 600; margin-top: 2px; word-break: break-word; }
.vdetail-map { height: 220px; width: 100%; border-radius: 10px; overflow: hidden; border: 1px solid var(--border); }
.vdetail-photo { width: 100%; border-radius: 10px; cursor: pointer; margin-top: 4px; }

/* Gráficas del dashboard */
.charts-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; }
.chart-box { border: 1px solid var(--border); border-radius: 10px; padding: 12px; background: var(--panel); }
.chart-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.chart-title { font-weight: 600; font-size: 13px; color: var(--ink); }
.chart-box canvas { max-height: 260px; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.modal.wide { max-width: 880px; }
#expandedWrap { height: 62vh; }

/* Dashboard de clientes impactados */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
.impact-hero {
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 28px;
  margin-bottom: 18px;
  padding: 24px;
  border: 1px solid #ddd6fe;
  border-radius: 16px;
  color: #fff;
  background: linear-gradient(122deg, #1e1b4b 0%, #4c1d95 54%, #6d28d9 100%);
  box-shadow: 0 16px 35px rgba(76, 29, 149, .17);
}
.impact-hero::after {
  content: '';
  position: absolute;
  width: 270px;
  height: 270px;
  right: -80px;
  top: -145px;
  border-radius: 50%;
  background: rgba(255, 255, 255, .09);
}
.impact-hero > * { position: relative; z-index: 1; }
.impact-eyebrow { margin-bottom: 7px; color: #ddd6fe; font-size: 11px; font-weight: 800; letter-spacing: .13em; text-transform: uppercase; }
.impact-hero h2 { max-width: 720px; margin: 0; font-size: clamp(21px, 2.25vw, 31px); line-height: 1.13; letter-spacing: -.025em; }
.impact-hero p { max-width: 720px; margin: 9px 0 0; color: #ede9fe; line-height: 1.5; }
.impact-filter-summary { max-width: 390px; display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 7px; }
.impact-filter-chip { padding: 6px 10px; border: 1px solid rgba(255,255,255,.23); border-radius: 999px; color: #f5f3ff; background: rgba(255,255,255,.11); font-size: 11px; font-weight: 650; backdrop-filter: blur(8px); }
.impact-filter-panel { border-color: #ddd6fe; }
.impact-status { min-height: 0; margin: -4px 0 10px; color: var(--muted); font-size: 12px; }
.impact-status:not(:empty) { min-height: 18px; }
.impact-metrics { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.impact-metric-icon { font-size: 21px; font-weight: 800; }
.impact-charts-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.impact-chart-box { min-width: 0; }
.impact-chart-canvas { position: relative; height: 290px; }
.impact-chart-canvas canvas { width: 100% !important; height: 100% !important; max-height: none; }
.chart-empty { display: none; place-items: center; height: 100%; border-radius: 8px; color: var(--muted); background: #f8fafc; font-size: 13px; }
.table-wrap { width: 100%; overflow: auto; }
.analysis-table { min-width: 960px; }
.analysis-table thead th { position: sticky; top: 0; z-index: 2; background: #f8fafc; white-space: nowrap; }
.analysis-table tbody tr:last-child td { border-bottom: 0; }
.analysis-primary { color: var(--ink); font-weight: 700; }
.analysis-meta { max-width: 240px; margin-top: 3px; color: var(--muted); font-size: 11px; line-height: 1.3; }
.analysis-empty-cell { padding: 34px 18px !important; color: var(--muted); text-align: center; }
.number-cell { font-variant-numeric: tabular-nums; }
.number-cell > strong { font-size: 16px; }
.coverage-label { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; min-width: 150px; font-variant-numeric: tabular-nums; }
.coverage-track { overflow: hidden; width: 100%; height: 6px; margin-top: 7px; border-radius: 999px; background: #ede9fe; }
.coverage-fill { display: block; height: 100%; border-radius: inherit; background: linear-gradient(90deg, #8b5cf6, #6d28d9); }
.impact-table-head { align-items: center !important; }
.impact-table-head > div:first-child { display: flex; align-items: baseline; gap: 5px; }
.impact-table-tools { display: flex; align-items: center; gap: 8px; }
.impact-table-tools input { width: min(320px, 40vw); }
.clients-analysis-table { min-width: 1320px; }
.contact-cell { max-width: 220px; }
.contact-cell a { display: block; overflow: hidden; max-width: 210px; text-overflow: ellipsis; white-space: nowrap; }
.analysis-tags { display: flex; align-items: center; flex-wrap: wrap; gap: 4px; }
.analysis-tag { display: inline-flex; max-width: 180px; padding: 3px 7px; overflow: hidden; border-radius: 999px; color: #5b21b6; background: #ede9fe; font-size: 11px; font-weight: 650; line-height: 1.2; text-overflow: ellipsis; white-space: nowrap; }
.analysis-tag--muted { color: #475569; background: #e2e8f0; }
.client-history { display: flex; flex-direction: column; gap: 10px; }
.client-history-summary { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; margin-bottom: 4px; }
.client-history-summary > div { display: flex; flex-direction: column; gap: 4px; padding: 11px; border: 1px solid var(--border); border-radius: 9px; background: #f8fafc; }
.client-history-item { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.client-history-item:last-child { border-bottom: 0; }
.analysis-limit-note { padding: 9px 11px; border-radius: 8px; color: #92400e; background: #fffbeb; font-size: 12px; }

/* Aviso del plan del propio usuario del panel */
.plan-banner {
  display: flex; align-items: center; flex-wrap: wrap; gap: 10px;
  margin-bottom: 16px; padding: 11px 14px;
  border: 1px solid #fcd34d; border-radius: 10px;
  color: #92400e; background: #fffbeb; font-size: 13px;
}
.plan-banner a { color: #92400e; font-weight: 700; text-decoration: underline; }

/* Facturación: barra de selección y formularios de parametrización */
.pay-bar {
  position: sticky; bottom: 0; z-index: 3;
  display: flex; align-items: center; justify-content: space-between;
  flex-wrap: wrap; gap: 12px;
  padding: 12px 16px; border-top: 1px solid var(--border);
  border-radius: 0 0 12px 12px; background: #f8fafc;
}
.pay-bar .pay-total { display: flex; align-items: baseline; gap: 8px; font-variant-numeric: tabular-nums; }
.pay-bar .pay-total strong { font-size: 19px; }
.pay-bar .pay-actions { display: flex; align-items: end; gap: 10px; flex-wrap: wrap; }
.quote-lines { display: flex; flex-direction: column; gap: 6px; max-height: 40vh; overflow: auto; }
.quote-line { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; padding: 6px 0; border-bottom: 1px solid var(--border); }
.quote-total { display: grid; gap: 4px; padding: 11px; border: 1px solid var(--border); border-radius: 9px; background: #f8fafc; font-variant-numeric: tabular-nums; }
.quote-total > div { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.quote-total .grand { padding-top: 6px; border-top: 1px solid var(--border); font-weight: 700; font-size: 16px; }
.param-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; }
.param-grid .field input, .param-grid .field select { width: 100%; }
.param-hint { margin: 4px 0 0; color: var(--muted); font-size: 11px; line-height: 1.35; }
.param-section + .param-section { margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--border); }
.param-section h3 { margin: 0 0 10px; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.copy-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.copy-row code { overflow: auto; max-width: 100%; padding: 6px 9px; border: 1px solid var(--border); border-radius: 7px; background: #f8fafc; font-size: 12px; }
.sel-count { color: var(--muted); font-size: 12px; }
.check-cell { width: 34px; text-align: center; }

@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { flex-direction: row; overflow-x: auto; }
  .sidebar .spacer, .sidebar .who { display: none; }
}

@media (max-width: 1240px) {
  .metric-cards { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (max-width: 900px) {
  .metric-cards { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .impact-hero { align-items: flex-start; flex-direction: column; }
  .impact-filter-summary { max-width: none; justify-content: flex-start; }
  .impact-charts-grid { grid-template-columns: 1fr; }
  .impact-table-head { align-items: flex-start !important; flex-direction: column; }
  .impact-table-tools { width: 100%; }
  .impact-table-tools input { width: 100%; }
}

@media (max-width: 560px) {
  .content { padding: 14px; }
  .topbar { align-items: flex-start; flex-wrap: wrap; gap: 10px; }
  .topbar-actions { width: 100%; justify-content: flex-start; flex-wrap: wrap; }
  .metric-cards { grid-template-columns: 1fr; gap: 12px; margin-bottom: 16px; }
  .metric-card { min-height: 128px; padding: 14px; }
  .impact-hero { padding: 19px; }
  .impact-chart-canvas { height: 250px; }
  .impact-table-tools { align-items: stretch; flex-direction: column; }
  .client-history-summary { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  .metric-card { transition: none; }
  .metric-card:hover { transform: none; }
}
