:root {
  --bg: #eef1f4;
  --panel: #ffffff;
  --border: #dde2e7;
  --text: #1d2730;
  --muted: #6b7782;
  --primary: #0b6e4f;
  --primary-hover: #095c42;
  --primary-soft: #e3f3ec;
  --danger: #b42318;
  --warn-bg: #fff6e0;
  --warn-text: #7a5300;
  --in-bubble: #ffffff;
  --out-bubble: #d9fdd3;
  --bot-bubble: #e8eefc;
  --note: #fff3c4;
  --chat-bg: #efeae2;
  --radius: 8px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color: var(--text);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--border); background: var(--panel); border-radius: 6px; padding: 6px 12px; }
button:hover:not(:disabled) { background: #f4f6f8; }
button:disabled { opacity: .5; cursor: not-allowed; }
button.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
button.primary:hover:not(:disabled) { background: var(--primary-hover); }
button.danger { color: var(--danger); border-color: #f1c4c0; }
input, select, textarea { border: 1px solid var(--border); border-radius: 6px; padding: 7px 10px; background: #fff; width: 100%; }
textarea { resize: vertical; }
label { display: block; font-weight: 600; margin: 12px 0 4px; font-size: 13px; }
.hint { color: var(--muted); font-size: 12px; font-weight: 400; }
.hidden { display: none !important; }

/* Login */
#login { display: grid; place-items: center; height: 100%; }
#login form { background: var(--panel); padding: 32px; border-radius: 12px; width: min(360px, 92vw); box-shadow: 0 4px 24px rgba(0,0,0,.08); }
#login h1 { margin: 0 0 4px; font-size: 20px; }
#login button { width: 100%; margin-top: 20px; padding: 10px; }
.error { color: var(--danger); margin-top: 10px; min-height: 1em; }

/* Layout */
#app { display: flex; flex-direction: column; height: 100%; }
header.top { display: flex; align-items: center; gap: 16px; padding: 0 16px; height: 52px; background: var(--primary); color: #fff; flex-shrink: 0; }
header.top .brand { font-weight: 700; font-size: 16px; white-space: nowrap; }
header.top nav { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
header.top nav button { background: transparent; border: none; color: #d6efe5; padding: 8px 12px; white-space: nowrap; }
header.top nav button.active, header.top nav button:hover { background: rgba(255,255,255,.15) !important; color: #fff; }
header.top .me { display: flex; align-items: center; gap: 10px; white-space: nowrap; }
header.top .me button { background: transparent; color: #fff; border-color: rgba(255,255,255,.4); }
.conn { width: 8px; height: 8px; border-radius: 50%; background: #f5b700; display: inline-block; }
.conn.ok { background: #6ee7a8; }
main { flex: 1; min-height: 0; display: flex; }
.view { flex: 1; min-height: 0; display: flex; }

/* Conversaciones */
aside.list { width: 340px; background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.filters { display: flex; flex-wrap: wrap; gap: 4px; padding: 10px; border-bottom: 1px solid var(--border); }
.filters button { font-size: 12px; padding: 4px 10px; border-radius: 999px; }
.filters button.active { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); font-weight: 600; }
.badge { display: inline-block; min-width: 18px; padding: 0 6px; border-radius: 999px; background: var(--danger); color: #fff; font-size: 11px; line-height: 18px; text-align: center; font-weight: 700; }
.badge.soft { background: #c9d1d8; color: var(--text); }
.search { padding: 8px 10px; border-bottom: 1px solid var(--border); }
#convList { flex: 1; overflow-y: auto; }
.conv { padding: 10px 12px; border-bottom: 1px solid #eef0f2; cursor: pointer; display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; }
.conv:hover { background: #f7f9fa; }
.conv.selected { background: var(--primary-soft); }
.conv .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .time { color: var(--muted); font-size: 12px; }
.conv .preview { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .meta { display: flex; gap: 6px; align-items: center; justify-content: flex-end; }
.status { font-size: 11px; padding: 1px 8px; border-radius: 999px; font-weight: 600; white-space: nowrap; }
.status.Bot { background: #e8eefc; color: #2a4fa8; }
.status.EnCola { background: #ffe9d6; color: #a14b00; }
.status.ConAsesor { background: var(--primary-soft); color: var(--primary); }
.status.Cerrada { background: #eceff1; color: var(--muted); }
.empty { color: var(--muted); padding: 24px; text-align: center; }

section.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--chat-bg); }
.chat-header { background: var(--panel); border-bottom: 1px solid var(--border); padding: 10px 16px; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.chat-header .who { flex: 1; min-width: 180px; }
.chat-header .who strong { font-size: 15px; }
.chat-header .who div { color: var(--muted); font-size: 12px; }
.chat-header .actions { display: flex; gap: 6px; flex-wrap: wrap; }
.chat-header .actions select { width: auto; }
#messages { flex: 1; overflow-y: auto; padding: 16px 6%; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 70%; padding: 6px 10px 4px; border-radius: 8px; box-shadow: 0 1px 1px rgba(0,0,0,.08); white-space: pre-wrap; word-wrap: break-word; }
.msg.in { align-self: flex-start; background: var(--in-bubble); }
.msg.out { align-self: flex-end; background: var(--out-bubble); }
.msg.bot { align-self: flex-end; background: var(--bot-bubble); }
.msg.note { align-self: center; background: var(--note); font-size: 12px; max-width: 90%; text-align: center; box-shadow: none; }
.msg.sep { align-self: center; background: #dfe5ea; font-size: 12px; color: var(--muted); box-shadow: none; }
.msg .who { font-size: 11px; font-weight: 700; color: var(--primary); margin-bottom: 2px; }
.msg.bot .who { color: #2a4fa8; }
.msg .foot { font-size: 11px; color: var(--muted); text-align: right; margin-top: 2px; }
.msg .foot .failed { color: var(--danger); font-weight: 600; }
.msg .opts { margin-top: 6px; display: flex; flex-direction: column; gap: 3px; }
.msg .opts span { border-top: 1px solid rgba(0,0,0,.08); padding-top: 3px; color: #1f6fb2; font-size: 13px; text-align: center; }
.msg img { max-width: 260px; max-height: 260px; border-radius: 6px; display: block; }
.msg audio { max-width: 260px; }
.composer { background: var(--panel); border-top: 1px solid var(--border); padding: 10px 16px; display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { min-height: 40px; max-height: 160px; }
.notice { background: var(--warn-bg); color: var(--warn-text); padding: 8px 16px; font-size: 13px; border-top: 1px solid #f0dfb2; }
.placeholder { flex: 1; display: grid; place-items: center; color: var(--muted); text-align: center; padding: 24px; }

/* Administración */
.admin { flex: 1; overflow-y: auto; padding: 20px; }
.admin h2 { margin: 0 0 4px; }
.admin > p { margin: 0 0 16px; color: var(--muted); }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.split { display: grid; grid-template-columns: minmax(280px, 1fr) minmax(320px, 1.3fr); gap: 16px; align-items: start; }
.tree .node { display: flex; align-items: center; gap: 8px; padding: 7px 8px; border-radius: 6px; cursor: pointer; }
.tree .node:hover { background: #f4f6f8; }
.tree .node.selected { background: var(--primary-soft); }
.tree .node .ttl { flex: 1; }
.tree .node.inactive .ttl { color: var(--muted); text-decoration: line-through; }
.tag { font-size: 11px; padding: 1px 6px; border-radius: 4px; background: #eceff1; color: var(--muted); white-space: nowrap; }
/* Canal de la conversación: WhatsApp o la burbuja de la app/web ADBOX. */
.channel { font-size: 10.5px; font-weight: 700; padding: 1px 6px; border-radius: 4px; white-space: nowrap; vertical-align: middle; }
.channel.whatsapp { background: #e3f6ea; color: #1b7a3d; }
.channel.adbox { background: #fdece2; color: #b4461a; }
.row { display: flex; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
.row .spacer { flex: 1; }
.check { display: flex; align-items: center; gap: 8px; margin-top: 12px; font-weight: 600; }
.check input { width: auto; }
.ciudades { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 0 12px; }
.ciudades .check { margin-top: 6px; font-weight: 400; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px; border-bottom: 1px solid var(--border); }
th { font-size: 12px; color: var(--muted); }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #1d2730; color: #fff; padding: 10px 18px; border-radius: 8px; z-index: 10; max-width: 90vw; }

/* Métricas */
.metrics {
  --series-1: #2a78d6; /* Resueltas por el bot */
  --series-2: #eb6834; /* Atendidas por asesor */
  --series-3: #1baf7a; /* Sin atender */
  --series-4: #eda100; /* Abiertas */
  --grid: #e6e8eb;
  --text-secondary: #52514e;
}
.filterbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.filterbar h2 { margin: 0 12px 0 0; }
.filterbar input[type=date] { width: auto; }
.filterbar .spacer { flex: 1; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.seg button { border: none; border-radius: 0; border-right: 1px solid var(--border); }
.seg button:last-child { border-right: none; }
.seg button.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
h3.sec { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 20px 0 8px; }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 12px; }
.tile { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.tile .k { color: var(--muted); font-size: 12px; }
.tile .v { font-size: 26px; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; }
.tile .s { color: var(--muted); font-size: 12px; margin-top: 2px; }
.tile.alert { border-color: #f0b37e; background: #fff8f1; }
.tile.alert .v::before { content: "⚠ "; font-size: 18px; }
.chart-card { margin-top: 16px; }
.chart-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.legend { display: flex; flex-wrap: wrap; gap: 14px; margin: 12px 0 4px; font-size: 12px; color: var(--text-secondary); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: 260px; overflow: visible; }
.chart .tip { position: absolute; pointer-events: none; background: #1d2730; color: #fff; padding: 8px 10px; border-radius: 6px; font-size: 12px; white-space: nowrap; z-index: 2; }
.chart .tip i { width: 8px; height: 8px; border-radius: 2px; display: inline-block; margin-right: 6px; }
.table-wrap { overflow-x: auto; margin-top: 8px; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

@media (max-width: 800px) {
  aside.list { width: 100%; }
  .view.has-chat aside.list { display: none; }
  .view:not(.has-chat) section.chat { display: none; }
  .split { grid-template-columns: 1fr; }
  #messages { padding: 12px; }
  .msg { max-width: 88%; }
  header.top .brand { display: none; }
}

/* Crear ticket */
dialog#ticketDialog { border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; width: min(560px, calc(100vw - 32px)); background: var(--panel); color: var(--text); }
dialog#ticketDialog::backdrop { background: rgba(0, 0, 0, .35); }
dialog#ticketDialog .notice { margin-top: 10px; border-radius: 6px; border: 1px solid #f0dfb2; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 560px) { .row2 { grid-template-columns: 1fr; } }

/* Calificación de la atención (encuesta 1-10) */
.nota { display: inline-block; min-width: 22px; padding: 1px 6px; border-radius: 999px; font-size: 11px; font-weight: 700; text-align: center; white-space: nowrap; }
.nota.good { background: #dcf3e6; color: #11663a; }
.nota.warn { background: var(--warn-bg); color: var(--warn-text); }
.nota.bad { background: #fde4e1; color: var(--danger); }
.rating-card { margin-top: 12px; display: grid; grid-template-columns: minmax(220px, 1fr) minmax(260px, 1.4fr); gap: 24px; }
@media (max-width: 760px) { .rating-card { grid-template-columns: 1fr; } }
.rating-bars { margin-top: 10px; display: flex; flex-direction: column; gap: 4px; }
.rbar { display: grid; grid-template-columns: 22px 1fr 32px; align-items: center; gap: 8px; font-size: 12px; font-variant-numeric: tabular-nums; }
.rbar .rn { color: var(--muted); text-align: right; }
.rbar .rv { color: var(--muted); }
.rtrack { height: 12px; background: #eef1f4; border-radius: 4px; overflow: hidden; }
.rfill { display: block; height: 100%; border-radius: 0 4px 4px 0; }
.rfill.good { background: #2e9e63; }
.rfill.warn { background: #e0a526; }
.rfill.bad { background: #d4483a; }
.rcomment { display: flex; gap: 10px; align-items: flex-start; padding: 8px 0; border-bottom: 1px solid var(--border); }
.rcomment:last-child { border-bottom: 0; }
#mRatingComments { margin-top: 6px; max-height: 320px; overflow: auto; }