/* ---------------------------------------------------------------- tokens
   Paleta de dataviz validada (adjacent + all-pairs, luz y oscuro).
   El aqua en modo claro queda bajo 3:1 -> por eso todo gráfico lleva
   etiquetas visibles y una vista de tabla (regla de relieve).           */
:root {
  color-scheme: light;
  --surface-1: #fcfcfb;
  --plane: #f9f9f7;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, .10);
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --track: #eceae4;
  --good: #0ca30c;
  --warning: #fab219;
  --critical: #d03b3b;
  --shadow: 0 1px 2px rgba(11,11,11,.05), 0 4px 16px rgba(11,11,11,.04);
  --radius: 10px;
  --sans: system-ui, -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-1: #1a1a19;
    --plane: #0d0d0d;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255,255,255,.10);
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --track: #2c2c2a;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.3);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-1: #1a1a19;
  --plane: #0d0d0d;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255,255,255,.10);
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --track: #2c2c2a;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.3);
}

* { box-sizing: border-box; }
/* .filters y otros contenedores declaran display:flex, que le gana al
   [hidden] de la hoja del navegador. Sin esto los paneles ocultos se ven. */
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; }
body { overflow-x: hidden; }
body {
  font-family: var(--sans);
  background: var(--plane);
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ---------------------------------------------------------------- shell */
.topbar {
  position: sticky; top: 0; z-index: 30;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
  padding: 0 20px;
  display: flex; align-items: center; gap: 24px;
  min-height: 56px; flex-wrap: wrap;
}
.brand { display: flex; align-items: baseline; gap: 10px; font-weight: 650; letter-spacing: -.01em; }
.brand small { font-weight: 400; color: var(--text-muted); font-size: 12px; letter-spacing: 0; }
nav { display: flex; gap: 2px; margin-left: auto; flex-wrap: wrap; }
nav button[hidden] { display: none; }
.session { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.session select { min-width: 0; width: auto; font-size: 13px; padding: 5px 8px; }
.session .btn { padding: 6px 12px; font-size: 13px; }
.whoami { font-size: 12.5px; color: var(--text-muted); white-space: nowrap; }
nav button {
  font: inherit; font-size: 13.5px; font-weight: 500;
  background: none; border: 0; border-radius: 8px;
  padding: 7px 13px; color: var(--text-secondary); cursor: pointer;
}
nav button:hover { background: var(--track); color: var(--text-primary); }
nav button[aria-current="page"] { background: var(--series-1); color: #fff; }
.theme-toggle {
  font: inherit; background: none; border: 1px solid var(--border); border-radius: 8px;
  width: 32px; height: 32px; cursor: pointer; color: var(--text-secondary); line-height: 1;
}
main { padding: 20px; max-width: 1500px; margin: 0 auto; }
.view[hidden] { display: none; }

h2 { font-size: 15px; font-weight: 620; margin: 28px 0 12px; letter-spacing: -.005em; }
h2:first-child { margin-top: 0; }
h2 .hint { font-weight: 400; color: var(--text-muted); font-size: 12.5px; margin-left: 8px; }

/* ---------------------------------------------------------------- filtros */
.filters {
  display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap;
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px 14px; margin-bottom: 18px;
}
.field { display: flex; flex-direction: column; gap: 4px; }
.field > label { font-size: 11.5px; color: var(--text-muted); font-weight: 500; }
input, select, textarea {
  font: inherit; font-size: 13.5px;
  background: var(--surface-1); color: var(--text-primary);
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 9px;
  max-width: 100%;   /* un <select> se estira a su opción más larga */
}
input:focus, select:focus, textarea:focus, button:focus-visible {
  outline: 2px solid var(--series-1); outline-offset: 1px;
}
select { min-width: 140px; width: 100%; }
.field { max-width: 100%; min-width: 0; }
.field > label .hint { color: var(--text-muted); font-weight: 400; }
.btn {
  font: inherit; font-size: 13.5px; font-weight: 500; cursor: pointer;
  background: var(--series-1); color: #fff; border: 0; border-radius: 8px; padding: 7px 14px;
}
.btn:hover { filter: brightness(1.08); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.ghost { background: none; color: var(--text-secondary); border: 1px solid var(--border); }
.btn.ghost:hover { background: var(--track); color: var(--text-primary); }

/* ------------------------------------------------------------ stat tiles */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(165px, 100%), 1fr)); gap: 12px; }
.tile {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow);
}
.tile .label { font-size: 12px; color: var(--text-secondary); }
.tile .value { font-size: 27px; font-weight: 640; letter-spacing: -.02em; margin-top: 3px; }
.tile .sub { font-size: 11.5px; color: var(--text-muted); margin-top: 2px; }
.hero .value { font-size: 48px; line-height: 1.05; }

/* ----------------------------------------------------------------- cards */
/* align-items:start -> cada tarjeta mide lo que su contenido, sin estirarse
   al alto de la más larga de la fila. */
.grid {
  display: grid;
  /* min(380px, 100%) evita que la columna mínima supere el ancho del viewport
     en pantallas angostas y empuje un scroll horizontal en toda la página. */
  grid-template-columns: repeat(auto-fit, minmax(min(380px, 100%), 1fr));
  gap: 14px; align-items: start;
}
.card {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 15px 17px 17px; box-shadow: var(--shadow);
  min-width: 0;
}
.card > header { display: flex; align-items: baseline; gap: 10px; margin-bottom: 4px; }
.card h3 { font-size: 13.5px; font-weight: 600; margin: 0; }
.card .sub { font-size: 12px; color: var(--text-muted); margin: 0 0 12px; }
.card .table-toggle {
  margin-left: auto; font: inherit; font-size: 11.5px; background: none;
  border: 1px solid var(--border); border-radius: 6px; padding: 2px 8px;
  color: var(--text-muted); cursor: pointer; white-space: nowrap;
}
.card .table-toggle:hover { background: var(--track); color: var(--text-primary); }
.card .empty { color: var(--text-muted); font-size: 13px; padding: 24px 0; text-align: center; }
.stale { opacity: .45; transition: opacity .15s; }

/* ----------------------------------------------------- barras (HTML/CSS) */
.bars { display: flex; flex-direction: column; gap: 7px; }   /* > 2px de aire */
.bar-row {
  display: grid; grid-template-columns: minmax(80px, 40%) 1fr auto;
  gap: 10px; align-items: center; min-height: 24px;           /* hit target */
  border-radius: 6px; padding: 1px 2px; cursor: default;
}
.bar-row:hover, .bar-row:focus-visible { background: var(--track); outline: none; }
.bar-label {
  font-size: 12.5px; color: var(--text-secondary);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.bar-track { background: var(--track); border-radius: 0 4px 4px 0; height: 18px; min-width: 2px; }
.bar-fill {
  height: 100%; background: var(--series-1);
  border-radius: 0 4px 4px 0;   /* extremo de dato redondeado, base cuadrada */
  min-width: 3px; transition: width .25s ease;
}
.bar-fill.s2 { background: var(--series-2); }
.bar-fill.s3 { background: var(--series-3); }
.bar-value {
  font-size: 12.5px; font-weight: 560; font-variant-numeric: tabular-nums;
  color: var(--text-primary); min-width: 46px; text-align: right;
}
.bar-value small { color: var(--text-muted); font-weight: 400; margin-left: 4px; }

/* --------------------------------------------------------- leyenda + svg */
.legend { display: flex; gap: 14px; flex-wrap: wrap; margin: 0 0 8px; font-size: 12px; color: var(--text-secondary); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 14px; height: 2px; border-radius: 1px; background: var(--series-1); }
.legend i.rect { width: 11px; height: 11px; border-radius: 3px; }
.legend i.s2 { background: var(--series-2); }
.legend i.s3 { background: var(--series-3); }
.chart-svg { display: block; width: 100%; overflow: visible; }
.chart-wrap { position: relative; }

.tooltip {
  position: fixed; z-index: 100; pointer-events: none;
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; box-shadow: var(--shadow);
  font-size: 12px; max-width: 280px;
}
.tooltip .tt-title { color: var(--text-secondary); font-size: 11.5px; margin-bottom: 5px; }
.tooltip .tt-row { display: flex; align-items: center; gap: 7px; white-space: nowrap; }
.tooltip .tt-row + .tt-row { margin-top: 3px; }
.tooltip .tt-key { width: 12px; height: 2px; border-radius: 1px; flex: none; background: var(--series-1); }
.tooltip .tt-key.s2 { background: var(--series-2); }
.tooltip .tt-val { font-weight: 640; font-variant-numeric: tabular-nums; }
.tooltip .tt-name { color: var(--text-secondary); }

/* ---------------------------------------------------------------- tablas */
/* --------------------------------------------------------- mapa de calor
   Una rampa de un solo tono; el número va escrito en cada celda, así que el
   color es relieve y nunca el único portador del dato. El separador de 2px
   entre celdas lo da el borde del color de superficie.                     */
.heat-wrap { overflow-x: auto; margin-top: 4px; }
table.heat { border-collapse: separate; border-spacing: 2px; width: 100%; }
table.heat th, table.heat td { border-bottom: 0; }
table.heat thead th {
  font-size: 11.5px; font-weight: 600; color: var(--text-secondary);
  text-align: right; padding: 4px 8px; white-space: nowrap;
}
table.heat thead th:first-child { text-align: left; }
table.heat thead th small {
  display: block; font-weight: 400; color: var(--text-muted); font-size: 10.5px;
}
table.heat tbody th {
  position: static; background: none; font-weight: 400; font-size: 12.5px;
  color: var(--text-secondary); text-align: left; padding: 6px 8px;
  max-width: 230px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.heat-cell {
  text-align: right; font-size: 12px; font-variant-numeric: tabular-nums;
  color: var(--text-primary); border-radius: 4px; padding: 7px 9px;
  min-width: 62px; cursor: default;
}
.heat-cell:focus-visible { outline: 2px solid var(--text-primary); outline-offset: -2px; }

.table-view { margin-top: 10px; max-height: 260px; overflow: auto; }
table { border-collapse: collapse; width: 100%; font-size: 12.5px; }
th, td { text-align: left; padding: 6px 9px; border-bottom: 1px solid var(--grid); vertical-align: top; }
th {
  position: sticky; top: 0; background: var(--surface-1); z-index: 1;
  font-weight: 600; font-size: 11.5px; color: var(--text-secondary);
  white-space: nowrap; cursor: pointer; user-select: none;
}
th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr:hover { background: var(--track); }

.table-scroll {
  overflow-x: auto; background: var(--surface-1);
  border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
#results-table td {
  max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
#results-table tbody tr { cursor: pointer; }
.pager { display: flex; align-items: center; gap: 10px; margin-top: 12px; flex-wrap: wrap; font-size: 13px; color: var(--text-secondary); }
.pager .spacer { flex: 1; }

/* ---------------------------------------------------------------- ask */
.ask-wrap { max-width: 860px; margin: 0 auto; }
.ask-box { display: flex; gap: 10px; align-items: flex-end; }
.ask-box textarea { flex: 1; min-height: 78px; resize: vertical; font-size: 15px; padding: 12px 14px; border-radius: var(--radius); }
.answer {
  background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px 20px; margin-top: 16px; box-shadow: var(--shadow);
  font-size: 15px; line-height: 1.65; white-space: pre-wrap;
}
.answer .q { font-size: 13px; color: var(--text-muted); margin-bottom: 10px; padding-bottom: 10px; border-bottom: 1px solid var(--grid); white-space: normal; }
.suggestions { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 12px; }
.suggestions button {
  font: inherit; font-size: 12.5px; background: var(--surface-1); color: var(--text-secondary);
  border: 1px solid var(--border); border-radius: 999px; padding: 5px 12px; cursor: pointer;
}
.suggestions button:hover { background: var(--track); color: var(--text-primary); }

/* ---------------------------------------------------------------- upload */
.upload-wrap { max-width: 780px; }
.drop {
  border: 1.5px dashed var(--axis); border-radius: var(--radius);
  padding: 22px; text-align: center; color: var(--text-secondary); cursor: pointer;
  background: var(--surface-1); transition: border-color .15s, background .15s;
}
.drop:hover, .drop.over { border-color: var(--series-1); background: var(--track); }
.drop strong { color: var(--text-primary); display: block; font-size: 13.5px; }
.drop small { font-size: 12px; color: var(--text-muted); }
.drop.filled { border-style: solid; border-color: var(--series-1); }
.joblog {
  background: var(--plane); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 12px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11.5px; line-height: 1.55; max-height: 300px; overflow: auto;
  white-space: pre-wrap; word-break: break-word; color: var(--text-secondary); margin-top: 12px;
}
.progress { height: 6px; background: var(--track); border-radius: 3px; overflow: hidden; margin: 12px 0 4px; }
.progress > div { height: 100%; background: var(--series-1); border-radius: 3px; transition: width .4s; }
.status { font-size: 13px; color: var(--text-secondary); display: flex; align-items: center; gap: 8px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-muted); flex: none; }
.dot.running { background: var(--warning); animation: pulse 1.2s infinite; }
.dot.done { background: var(--good); }
.dot.error { background: var(--critical); }
@keyframes pulse { 50% { opacity: .3; } }
.notice {
  border-radius: 8px; padding: 10px 13px; font-size: 13px; margin-top: 12px;
  border: 1px solid var(--border); background: var(--surface-1);
}
.notice.error { border-color: var(--critical); color: var(--critical); }
.notice.ok { border-color: var(--good); }
.notice.warn { border-color: var(--warning); }

/* ---------------------------------------------------------- voz del cliente
   Las barras de temas y frases se despliegan en las citas que las sustentan:
   el número sin la cita textual no sirve para decidir nada. */
.bar-row.expandable { cursor: pointer; }
.bar-row.expandable .bar-label::before {
  content: '▸'; display: inline-block; width: 12px; color: var(--text-muted);
  transition: transform .12s;
}
.bar-row.expandable.open .bar-label::before { transform: rotate(90deg); }
.quotes { display: flex; flex-direction: column; gap: 8px; margin: 2px 0 10px 12px; }
.quotes-desc { font-size: 12px; color: var(--text-muted); margin: 0; }
.quotes-hint { font-size: 11.5px; color: var(--text-muted); margin: 10px 0 0; }
.quote {
  margin: 0; padding: 8px 12px; background: var(--plane); border-radius: 8px;
  border-left: 2px solid var(--series-2); font-size: 13px; line-height: 1.55;
}
.quote p { margin: 0; overflow-wrap: anywhere; }
.quote-meta { font-size: 11.5px; color: var(--text-muted); margin-top: 5px; }
.quote.linked { cursor: pointer; }
.quote.linked:hover, .quote.linked:focus-visible { background: var(--track); outline: none; }

/* La cita es el dato: se muestra completa y la tabla scrollea, no la recorta. */
table.extractos td { max-width: none; white-space: normal; }
table.extractos td:nth-child(4) { min-width: 320px; line-height: 1.5; }
table.extractos td:nth-child(1), table.extractos td:nth-child(2) { white-space: nowrap; }
table.extractos tbody tr { cursor: pointer; }

/* --------------------------------------------------------------- detalle */
dialog {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 0;
  background: var(--surface-1); color: var(--text-primary);
  max-width: min(880px, 94vw); width: 100%; box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0,0,0,.45); }
.detail-head {
  display: flex; align-items: center; gap: 12px; padding: 14px 18px;
  border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface-1);
}
.detail-head h3 { margin: 0; font-size: 14px; }
.detail-body { padding: 16px 18px; max-height: 68vh; overflow: auto; }
.detail-body h4 { font-size: 12px; color: var(--text-muted); margin: 18px 0 6px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.detail-body h4:first-child { margin-top: 0; }
.detail-body h4 .hint { text-transform: none; letter-spacing: 0; font-weight: 400; margin-left: 8px; color: var(--text-muted); }
.kv { display: grid; grid-template-columns: minmax(120px, 200px) 1fr; gap: 4px 14px; font-size: 13px; }
.kv dt { color: var(--text-muted); }
.kv dd { margin: 0; }
.transcript { white-space: pre-wrap; font-size: 13px; line-height: 1.65; background: var(--plane); border-radius: 8px; padding: 12px 14px; max-height: 320px; overflow: auto; }

/* ---------------------------------------------------------- transcripción */
.btn.mini { font-size: 11.5px; padding: 2px 9px; border-radius: 6px; }
/* La columna del botón no debe crecer ni recortarse como las de datos. */
th.col-transcript, td.col-transcript { width: 1%; white-space: nowrap; }
#results-table td.col-transcript { max-width: none; overflow: visible; }

/* Turnos de habla. El color distingue quién habla; la etiqueta lo dice
   igual, así que no depende sólo del color. */
.transcript .turn {
  margin: 0 0 7px; display: grid; grid-template-columns: 62px 1fr; gap: 10px;
}
.transcript .turn:last-child { margin-bottom: 0; }
.transcript .who {
  font-size: 10.5px; font-weight: 650; text-transform: uppercase; letter-spacing: .04em;
  padding-top: 3px; color: var(--series-1);
}
.transcript .turn.cliente .who { color: var(--series-2); }
.transcript .said { min-width: 0; overflow-wrap: anywhere; }
.transcript mark {
  background: var(--warning); color: #0b0b0b;
  border-radius: 3px; padding: 0 2px;
}

.spinner {
  width: 14px; height: 14px; border: 2px solid var(--track);
  border-top-color: var(--series-1); border-radius: 50%;
  animation: spin .7s linear infinite; display: inline-block; flex: none;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
