/* ===== Tokens (paleta validada dataviz) ===== */
:root, .viz-root {
  --surface-0: #f4f4f1;
  --surface-1: #fcfcfb;      /* card */
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #8a8a85;
  --border: #e6e6e2;
  --border-soft: #efefec;
  --received: #2a78d6;       /* serie 1 azul */
  --sent: #eb6834;           /* serie 2 naranja */
  --grid: #ececE8;
  --good: #1a8f5f;
}
:root[data-theme="dark"], .viz-root[data-theme="dark"] {
  --surface-0: #111110;
  --surface-1: #1a1a19;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #8f8f86;
  --border: #33332f;
  --border-soft: #262622;
  --received: #3987e5;
  --sent: #d95926;
  --grid: #2a2a26;
  --good: #34d399;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]), .viz-root:not([data-theme="light"]) {
    --surface-0: #111110; --surface-1: #1a1a19; --text-primary: #fff; --text-secondary: #c3c2b7;
    --text-muted: #8f8f86; --border: #33332f; --border-soft: #262622; --received: #3987e5; --sent: #d95926; --grid: #2a2a26; --good: #34d399;
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: Inter, system-ui, sans-serif; background: var(--surface-0); color: var(--text-primary); -webkit-font-smoothing: antialiased; }

.top {
  display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
  padding: 16px 24px; border-bottom: 1px solid var(--border); background: var(--surface-1); position: sticky; top: 0; z-index: 10;
}
.top__title { display: flex; align-items: center; gap: 12px; }
.top__logo { width: 46px; height: 46px; border-radius: 12px; background: var(--surface-0); display: grid; place-items: center; font-size: 23px; }
.top h1 { font-size: 22px; font-weight: 800; letter-spacing: -.01em; }
.top__sub { font-size: 12.5px; color: var(--text-muted); margin-top: 1px; }
.top__actions { display: flex; align-items: center; gap: 10px; }
.segmented { display: inline-flex; background: var(--surface-0); border: 1px solid var(--border); border-radius: 10px; padding: 3px; }
.seg { padding: 6px 12px; font-size: 12.5px; font-weight: 600; color: var(--text-secondary); border-radius: 7px; cursor: pointer; border: none; background: none; }
.seg--active { background: var(--surface-1); color: var(--text-primary); box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.icon-btn { width: 34px; height: 34px; border-radius: 9px; border: 1px solid var(--border); background: var(--surface-1); color: var(--text-secondary); cursor: pointer; font-size: 16px; }
.icon-btn:hover { color: var(--text-primary); }

.daterange { display: inline-flex; align-items: center; gap: 6px; }
.date-input { padding: 6px 9px; font-size: 12.5px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-1); color: var(--text-primary); color-scheme: light dark; }
.date-input:focus { outline: none; border-color: var(--sent); }
.date-sep { color: var(--text-muted); font-size: 12px; }
.date-btn { padding: 6px 11px; font-size: 12.5px; font-weight: 600; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-1); color: var(--text-primary); cursor: pointer; }
.date-btn:hover { background: var(--surface-0); }
.date-btn--clear { padding: 6px 9px; color: var(--text-muted); }

.wrap { max-width: 1560px; margin: 0 auto; padding: 30px clamp(20px, 3vw, 48px); display: flex; flex-direction: column; gap: 26px; }

/* ===== KPIs ===== */
.kpis { display: grid; grid-template-columns: repeat(20, 1fr); gap: 18px; }
.kpi { grid-column: span 4; background: var(--surface-1); border: 1px solid var(--border); border-radius: 16px; padding: 22px 22px 20px; text-align: center; }
.kpi:nth-last-child(-n+4) { grid-column: span 5; } /* fila de abajo: 4 tarjetas, alineadas al ancho */
/* 7 KPIs (sin costes, cuando no eres super_admin): 4 arriba / 3 abajo */
.kpis--7 { grid-template-columns: repeat(12, 1fr); }
.kpis--7 .kpi { grid-column: span 3; }
.kpis--7 .kpi:nth-last-child(-n+4) { grid-column: span 3; }
.kpis--7 .kpi:nth-last-child(-n+3) { grid-column: span 4; }
@media (max-width: 1100px) {
  .kpis, .kpis--7 { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
  .kpi, .kpi:nth-last-child(-n+4), .kpis--7 .kpi, .kpis--7 .kpi:nth-last-child(-n+3) { grid-column: auto; }
}
/* ocultar la columna "Coste IA" (9ª) cuando no eres super_admin */
.msgs--nocost th:nth-child(9), .msgs--nocost td:nth-child(9) { display: none; }
.msgs__human { color: var(--sent); font-weight: 600; }
.kpi__label { font-size: 13px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: .03em; display: flex; align-items: center; justify-content: center; gap: 7px; }
.kpi__dot { width: 9px; height: 9px; border-radius: 50%; }
.kpi__value { font-size: 42px; font-weight: 800; letter-spacing: -.02em; margin-top: 12px; line-height: 1; font-variant-numeric: tabular-nums; }
.kpi--sm .kpi__value { font-size: 23px; line-height: 1.2; }
.kpi__sub { font-size: 13.5px; color: var(--text-secondary); margin-top: 9px; }
.kpi--muted .kpi__value { color: var(--text-muted); font-size: 26px; font-weight: 700; }

/* ===== Cards ===== */
.card { background: var(--surface-1); border: 1px solid var(--border); border-radius: 16px; padding: 24px; }
.card__head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.card__head h2 { font-size: 17px; font-weight: 700; }
.card__note { font-size: 12.5px; color: var(--text-muted); margin-top: 10px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 26px; }
@media (max-width: 820px) { .grid2 { grid-template-columns: 1fr; } }

.legend { display: flex; gap: 14px; flex-wrap: wrap; }
.legend span { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; color: var(--text-secondary); }
.legend i { width: 10px; height: 10px; border-radius: 3px; }

.chart { width: 100%; }
.chart svg { display: block; width: 100%; overflow: visible; }
.axis { fill: var(--text-muted); font-size: 10.5px; font-weight: 500; }
.gridline { stroke: var(--grid); stroke-width: 1; }
.axisline { stroke: var(--border); stroke-width: 1; }
.chart .cross { stroke: var(--text-muted); stroke-width: 1; }
.chart .enddot { stroke: var(--surface-1); stroke-width: 2; }
.dlabel { font-size: 11px; font-weight: 700; }

/* barras de tipos */
.typebars { display: flex; flex-direction: column; gap: 12px; margin-top: 6px; }
.tbar { display: grid; grid-template-columns: 90px 1fr 46px; align-items: center; gap: 10px; font-size: 12.5px; }
.tbar__name { color: var(--text-secondary); font-weight: 600; text-transform: capitalize; }
.tbar__track { height: 12px; border-radius: 6px; background: var(--surface-0); overflow: hidden; }
.tbar__fill { height: 100%; border-radius: 6px; background: var(--sent); }
.tbar__val { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }

/* ===== Pestañas ===== */
.tabs { display: flex; gap: 4px; max-width: 1560px; margin: 0 auto; width: 100%; padding: 18px clamp(20px, 3vw, 48px) 0; border-bottom: 1px solid var(--border); }
.tab { padding: 11px 20px; font-size: 14.5px; font-weight: 600; color: var(--text-secondary); background: none; border: none; border-bottom: 2px solid transparent; margin-bottom: -1px; cursor: pointer; border-radius: 8px 8px 0 0; }
.tab:hover { color: var(--text-primary); background: var(--surface-1); }
.tab--active { color: var(--text-primary); border-bottom-color: var(--sent); }
.tabpanel { display: flex; flex-direction: column; gap: 26px; }
.tabpanel[hidden] { display: none; }

/* ===== Tabla de mensajes detallados ===== */
.tablewrap { overflow-x: auto; margin: 0 -4px; }
table.msgs { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.msgs thead th { text-align: left; font-weight: 600; color: var(--text-muted); font-size: 11.5px; text-transform: uppercase; letter-spacing: .03em; padding: 10px 14px; border-bottom: 1px solid var(--border); white-space: nowrap; position: sticky; top: 0; background: var(--surface-1); }
.msgs th.num { text-align: right; }
.msgs td { padding: 11px 14px; border-bottom: 1px solid var(--border-soft); vertical-align: top; }
.msgs td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.msgs td.nowrap { white-space: nowrap; color: var(--text-secondary); }
.msgs td.cap { text-transform: capitalize; }
.msgs td.dim, .msgs .dim { color: var(--text-muted); }
.msgs tbody tr:hover td { background: var(--surface-0); }
.msgs__text { max-width: 520px; color: var(--text-secondary); line-height: 1.45; }
.msgs__empty { text-align: center; color: var(--text-muted); padding: 28px 14px; }

/* vista emparejada: entrante (cliente) + respuesta (bot) en la misma fila */
.msgs--pairs th:nth-child(2), .msgs--pairs th:nth-child(3),
.msgs--pairs td.msgs__in, .msgs--pairs td.msgs__out { width: 34%; }
.msgs--pairs td.msgs__in { border-left: 3px solid var(--received); }
.msgs--pairs td.msgs__out { border-left: 3px solid var(--sent); }
.msgs--pairs td.msgs__in .msgs__text { color: var(--text-primary); }
.msgs--pairs .msgs__text { max-width: 420px; display: block; }

.pill { display: inline-flex; align-items: center; gap: 7px; font-size: 12.5px; font-weight: 600; color: var(--text-primary); white-space: nowrap; }
.pill i { width: 8px; height: 8px; border-radius: 50%; flex: none; }

.pager { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-top: 16px; font-size: 13px; color: var(--text-secondary); }
.pager__btns { display: flex; align-items: center; gap: 10px; }
.pager__n { color: var(--text-muted); font-variant-numeric: tabular-nums; }
.pgbtn { padding: 7px 14px; font-size: 13px; font-weight: 600; color: var(--text-primary); background: var(--surface-1); border: 1px solid var(--border); border-radius: 9px; cursor: pointer; }
.pgbtn:hover:not(:disabled) { background: var(--surface-0); }
.pgbtn:disabled { opacity: .45; cursor: default; }

.search-input { padding: 9px 13px; font-size: 13.5px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface-0); color: var(--text-primary); min-width: 260px; }
.search-input:focus { outline: none; border-color: var(--sent); }
.search-input::placeholder { color: var(--text-muted); }
.msgs__name { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

/* selector de canal */
.chan-select { padding: 9px 12px; font-size: 13.5px; font-weight: 600; border: 1px solid var(--border); border-radius: 10px; background: var(--surface-0); color: var(--text-primary); cursor: pointer; }
.chan-select:focus { outline: none; border-color: var(--sent); }

/* chip de canal en la tabla */
.chip { display: inline-flex; align-items: center; gap: 5px; padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; border: 1px solid transparent; white-space: nowrap; }
.chip--whatsapp   { color: #1f7a4d; background: rgba(37, 211, 102, .13); border-color: rgba(37, 211, 102, .30); }
.chip--instagram  { color: #a12a72; background: rgba(214, 41, 118, .12); border-color: rgba(214, 41, 118, .28); }
.chip--facebook   { color: #1554b8; background: rgba(24, 119, 242, .12); border-color: rgba(24, 119, 242, .28); }
.chip--pagina_web { color: #5c5f66; background: rgba(120, 124, 135, .14); border-color: rgba(120, 124, 135, .30); }
[data-theme="dark"] .chip--whatsapp   { color: #57e08c; }
[data-theme="dark"] .chip--instagram  { color: #f472b6; }
[data-theme="dark"] .chip--facebook   { color: #7ab3ff; }
[data-theme="dark"] .chip--pagina_web { color: #b3b7c0; }

/* tooltip */
.tip {
  position: fixed; z-index: 50; pointer-events: none; background: var(--surface-1);
  border: 1px solid var(--border); border-radius: 10px; padding: 9px 11px; font-size: 12px;
  box-shadow: 0 8px 24px rgba(0,0,0,.14); min-width: 120px;
}
.tip__t { font-weight: 700; margin-bottom: 5px; }
.tip__r { display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.tip__r i { width: 8px; height: 8px; border-radius: 2px; display: inline-block; margin-right: 6px; }
.tip__r b { font-variant-numeric: tabular-nums; }
