/* Bandeja estilo Kommo: lista de chats · ficha del paciente · conversación */

.main:has(.kb) { padding: 0; max-width: none; }
.main:has(.kb) > .alert { margin: 10px 14px 0; }

.kb {
  display: grid;
  grid-template-columns: 340px 320px minmax(0, 1fr);
  height: 100vh;
  background: var(--surface);
}

/* ── Lista ─────────────────────────────── */
.kb-lista { border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; background: var(--surface); }
.kb-buscar { padding: 12px 12px 8px; }
.kb-buscar input { background: var(--n-50); border-color: transparent; padding-left: 12px; }
.kb-filtros { display: flex; gap: 6px; padding: 0 12px 8px; flex-wrap: wrap; align-items: center; }
.kb-filtros a {
  font-size: 12px; padding: 3px 10px; border-radius: 20px; color: var(--text-muted);
  background: var(--n-100); font-weight: 560;
}
.kb-filtros a.on { background: var(--accent); color: #fff; }
.kb-filtros a:hover { text-decoration: none; }
.kb-filtros select { width: auto; font-size: 12px; padding: 2px 6px; border-radius: 20px; }
.kb-total {
  display: flex; justify-content: space-between; align-items: center; font-size: 11.5px; color: var(--text-faint);
  padding: 6px 14px; border-top: 1px solid var(--border-soft); border-bottom: 1px solid var(--border-soft);
  text-transform: uppercase; letter-spacing: .05em; font-weight: 600;
}
.kb-scroll { overflow-y: auto; flex: 1; }

.conv-item {
  display: flex; gap: 11px; padding: 11px 14px; border-bottom: 1px solid var(--border-soft);
  color: var(--text); position: relative;
}
.conv-item:hover { background: var(--n-50); text-decoration: none; }
.conv-item.on { background: var(--accent-soft); }
.conv-item.on::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: var(--accent); }
.av {
  width: 40px; height: 40px; border-radius: 50%; flex: 0 0 40px; color: #fff; font-weight: 650; font-size: 14px;
  display: flex; align-items: center; justify-content: center; position: relative; letter-spacing: .02em;
}
.av-wa {
  position: absolute; right: -2px; bottom: -2px; width: 14px; height: 14px; border-radius: 50%;
  background: #25D366; border: 2px solid var(--surface);
}
.av-g { width: 48px; height: 48px; flex-basis: 48px; font-size: 16px; }
.ci-cuerpo { display: flex; flex-direction: column; gap: 2px; min-width: 0; flex: 1; }
.ci-top { display: flex; justify-content: space-between; gap: 8px; }
.ci-nombre { font-weight: 620; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ci-hora { font-size: 11px; color: var(--text-faint); white-space: nowrap; }
.no-leido .ci-nombre { font-weight: 720; }
.no-leido .ci-hora { color: var(--accent); font-weight: 600; }
.conv-preview {
  font-size: 12.5px; color: var(--text-muted); line-height: 1.4;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.conv-preview b { font-weight: 600; color: var(--text); }
.conv-meta { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 3px; align-items: center; }
.dot-unread {
  min-width: 18px; height: 18px; border-radius: 9px; background: #25D366; color: #fff; font-size: 10.5px;
  font-weight: 700; display: inline-flex; align-items: center; justify-content: center; padding: 0 5px;
}
.dot-unread:empty { min-width: 9px; width: 9px; height: 9px; padding: 0; }
.etq {
  font-size: 10.5px; font-weight: 620; padding: 1px 7px; border-radius: 4px; white-space: nowrap;
  color: var(--c); background: color-mix(in srgb, var(--c) 12%, transparent);
}

/* ── Ficha ─────────────────────────────── */
.kb-ficha { border-right: 1px solid var(--border); overflow-y: auto; background: var(--surface-alt); min-height: 0; }
.kb-volver { display: none; padding: 10px 16px; font-size: 13px; }
.kf-cab { display: flex; gap: 12px; align-items: center; padding: 18px 18px 12px; }
.kf-nombre { font-size: 17px; font-weight: 680; letter-spacing: -.01em; line-height: 1.25; }
.kf-sub { font-size: 12px; color: var(--text-muted); margin-top: 3px; }
.wa-pill { background: #25D366; color: #fff; font-size: 10.5px; font-weight: 650; border-radius: 10px; padding: 1px 7px; }
.kf-etapa { padding: 0 18px 12px; }
.kf-etapa select { font-weight: 620; background: var(--surface); }
.kf-progreso { display: flex; gap: 3px; margin-top: 8px; }
.kf-progreso i { flex: 1; height: 4px; border-radius: 2px; background: var(--n-150); }
.kf-progreso i.on { background: var(--c); }
.kf-tabs { display: flex; gap: 18px; padding: 0 18px; border-bottom: 1px solid var(--border); }
.kf-tabs span { font-size: 13px; padding: 8px 0; font-weight: 600; }
.kf-tabs span.on { border-bottom: 2px solid var(--accent); }
.kf-datos { display: grid; grid-template-columns: 96px 1fr; gap: 9px 10px; padding: 14px 18px; margin: 0; font-size: 13px; }
.kf-datos dt { color: var(--text-faint); }
.kf-datos dd { margin: 0; word-break: break-word; }
.kf-bloque { padding: 12px 18px; border-top: 1px solid var(--border-soft); }
.kf-tit { font-size: 10.5px; text-transform: uppercase; letter-spacing: .07em; color: var(--text-faint); font-weight: 650; margin-bottom: 6px; display: block; }
.kf-cita {
  display: flex; flex-direction: column; gap: 2px; background: var(--surface); border: 1px solid var(--border);
  border-left: 3px solid var(--accent); border-radius: 8px; padding: 8px 10px; color: var(--text); font-size: 13px;
}
.kf-cita:hover { text-decoration: none; border-color: var(--accent); }
.kf-cita span { color: var(--text-muted); font-size: 12px; }
.kf-paq { font-size: 13px; display: flex; justify-content: space-between; padding: 3px 0; }
.kf-paq span { color: var(--text-faint); font-variant-numeric: tabular-nums; }
.kf-bloque textarea { background: var(--surface); font-size: 13px; min-height: 60px; }
.kf-links { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 10px; font-size: 12.5px; align-items: center; }
.link-btn { background: none; border: none; padding: 0; font: inherit; font-size: 12.5px; color: var(--accent); cursor: pointer; }
.link-btn.rojo { color: var(--danger); }

/* ── Chat ──────────────────────────────── */
.kb-chat { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: var(--chat-bg); }
.kc-cab {
  display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 12px 18px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.kc-volver { display: none; font-size: 22px; padding: 0 6px; color: var(--text); }
.kc-cab strong { font-size: 14.5px; display: block; }
.kc-sub { font-size: 12px; color: var(--text-muted); }
.kc-ventana { font-size: 11.5px; padding: 3px 9px; border-radius: 12px; background: var(--n-100); color: var(--text-muted); white-space: nowrap; }
.kc-ventana.ok { background: var(--ok-soft); color: var(--ok); }
.kc-noleido { margin: 0; }
.kc-noleido .btn { white-space: nowrap; }

.thread-body {
  flex: 1; overflow-y: auto; padding: 16px 6%; display: flex; flex-direction: column; gap: 4px;
  background-image: radial-gradient(var(--border-soft) 1px, transparent 1px); background-size: 16px 16px;
}
.msg-day { text-align: center; margin: 12px 0 8px; }
.msg-day span {
  background: var(--surface); color: var(--text-muted); font-size: 11.5px; padding: 3px 12px;
  border-radius: 8px; box-shadow: 0 1px 1px rgba(0,0,0,.06);
}
.msg {
  max-width: min(68%, 560px); padding: 6px 10px 5px; border-radius: 10px; font-size: 14px; line-height: 1.45;
  box-shadow: 0 1px 1px rgba(0,0,0,.08); display: flex; flex-direction: column; word-wrap: break-word;
}
.msg-in { align-self: flex-start; background: var(--burbuja-in); border-top-left-radius: 3px; }
.msg-out { align-self: flex-end; background: var(--burbuja-out); border-top-right-radius: 3px; }
.msg-tpl { align-self: flex-end; background: var(--burbuja-tpl); border-top-right-radius: 3px; border: 1px solid var(--border); }
.msg-new { animation: aparece .25s ease; }
@keyframes aparece { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.msg-autor { font-size: 11px; font-weight: 700; color: var(--accent); margin-bottom: 1px; }
.msg-body { white-space: pre-wrap; }
.msg-error { font-size: 11.5px; color: var(--danger); margin-top: 4px; }
.msg-foot { display: flex; gap: 6px; justify-content: flex-end; align-items: center; margin-top: 2px; }
.msg-tag { font-size: 10px; background: rgba(0,0,0,.06); border-radius: 4px; padding: 0 5px; color: var(--text-muted); }
.msg-time { font-size: 10.5px; color: var(--text-faint); }
.msg-status { font-size: 10.5px; color: var(--text-faint); }
.msg-status.is-read { color: #3B82C4; }
.msg-status.is-failed { color: var(--danger); font-weight: 600; }

.composer { background: var(--surface); border-top: 1px solid var(--border); padding: 10px 14px 8px; }
.composer form { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { resize: none; min-height: 40px; max-height: 180px; border-radius: 20px; padding: 9px 14px; background: var(--n-50); }
.composer .btn { border-radius: 20px; padding: 9px 18px; }
.composer .hint { margin: 4px 4px 0; }
.composer-locked { background: var(--warn-soft); color: var(--warn); padding: 12px 18px; font-size: 13px; border-top: 1px solid var(--border); }
.composer-locked a { font-weight: 650; margin-left: 6px; }

.kb-vacio { grid-column: 2 / 4; display: flex; align-items: center; justify-content: center; background: var(--chat-bg); }

/* ── Pantallas medianas: la ficha se oculta ── */
@media (max-width: 1250px) {
  .kb { grid-template-columns: 300px minmax(0, 1fr); }
  .kb-ficha { display: none; }
  .kb-vacio { grid-column: 2; }
}
/* ── Celular: lista o chat, uno a la vez ── */
@media (max-width: 900px) {
  .kb { grid-template-columns: 1fr; height: calc(100vh - 110px); }
  .kb.con-chat .kb-lista { display: none; }
  .kb:not(.con-chat) .kb-chat, .kb-vacio { display: none; }
  .kb.con-chat .kb-ficha { display: none; }
  .thread-body { padding: 12px; }
  .kc-volver { display: block; }
  .msg { max-width: 85%; }
}

/* Botón "Ficha" dentro del chat (solo celular y pantallas medianas) */
.kc-ficha { display: none; }
@media (max-width: 1250px) {
  .kc-ficha { display: inline-flex; }
  .kb.ver-ficha .kb-ficha {
    display: block; position: fixed; inset: 0; z-index: 80; background: var(--surface);
    padding-top: env(safe-area-inset-top); overflow-y: auto;
  }
  .kb.ver-ficha .kb-volver { display: block; }
}

/* Valor del negocio en la ficha */
.kf-negocio { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 10px; padding: 0 18px 14px; align-items: end; }
.kf-negocio .btn { grid-column: 1 / 3; }
.kf-valor label { font-size: 10.5px; text-transform: uppercase; letter-spacing: .07em; color: var(--text-faint); font-weight: 650; margin: 0 0 4px; }
.kf-valor-input { display: flex; align-items: center; background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius-sm); }
.kf-valor-input:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-ring); }
.kf-valor-input span { padding-left: 10px; color: var(--text-faint); font-weight: 600; }
.kf-valor-input input { border: none; box-shadow: none !important; font-weight: 650; font-size: 15px; padding-left: 4px; }
.kf-negocio select { font-size: 13px; }
.etq-valor { font-size: 11px; font-weight: 650; color: var(--text); }

/* Archivos dentro del chat */
.msg-media { display: block; margin: 2px -4px 4px; }
.msg-media img, video.msg-media { max-width: min(320px, 100%); max-height: 360px; border-radius: 8px; display: block; background: var(--n-100); }
.msg-media img.sticker { max-width: 140px; background: transparent; }
.msg-audio { width: 260px; max-width: 100%; height: 40px; margin: 2px 0; }
.msg-doc { display: inline-flex; gap: 6px; align-items: center; background: var(--velo); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-weight: 600; color: var(--text); margin-bottom: 4px; }
.msg-sin { font-size: 12px; color: var(--text-faint); font-style: italic; }
.msg-transcript { font-size: 12.5px; color: var(--text-muted); font-style: italic; border-left: 2px solid var(--border-strong); padding-left: 8px; margin: 4px 0 2px; white-space: pre-wrap; }
.kc-clip { border: none; background: var(--n-100); width: 42px; height: 42px; border-radius: 50%; font-size: 18px; cursor: pointer; flex: 0 0 42px; }
.kc-clip:hover { background: var(--n-150); }

/* Emojis y respuestas rápidas */
.composer { position: relative; }
.kc-pop { display: none; position: absolute; bottom: calc(100% + 8px); left: 0; z-index: 40; background: var(--surface);
  border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow-lg); }
.kc-pop.on { display: block; }
#kc-emojis.on { display: block; padding: 0; overflow: hidden; width: min(400px, 94vw); }
#kc-emojis emoji-picker { width: 100%; height: 380px; --border-size: 0; --num-columns: 9; --emoji-size: 1.45rem;
  --background: var(--surface); --input-border-color: var(--border-strong); --input-font-color: var(--text);
  --indicator-color: var(--accent); --outline-color: var(--accent); --button-hover-background: var(--n-100); }
@media (max-width: 900px) { #kc-emojis emoji-picker { height: 320px; --num-columns: 8; } }
#kc-emojis button { border: none; background: none; font-size: 21px; padding: 4px; border-radius: 6px; cursor: pointer; }
#kc-emojis button:hover { background: var(--n-100); }
.kc-rr { width: min(520px, 94vw); }
.kc-rr-lista { max-height: 300px; overflow-y: auto; padding: 6px; }
.kc-rr-item { display: grid; grid-template-columns: 110px 1fr; gap: 2px 10px; padding: 8px 10px; border-radius: 8px; cursor: pointer; font-size: 13px; }
.kc-rr-item:hover, .kc-rr-item.sel { background: var(--accent-soft); }
.kc-rr-item b { color: var(--accent); font-weight: 650; }
.kc-rr-item span { color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kc-rr-acc { grid-column: 2; font-size: 11px; color: var(--text-faint) !important; }
.kc-rr-acc a { cursor: pointer; color: var(--text-faint); }
.kc-rr-acc a:hover { color: var(--accent); }
.kc-rr-pie { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 10px; border-top: 1px solid var(--border-soft); flex-wrap: wrap; }
.kc-rr-form { padding: 8px 10px; border-top: 1px solid var(--border-soft); display: flex; flex-direction: column; gap: 6px; }
.kc-saludo { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.kc-saludo select, .kc-saludo input { flex: 1; min-width: 180px; background: var(--surface); }
.kc-previa { margin-top: 8px; font-size: 13px; background: var(--burbuja-tpl); border-radius: 8px; padding: 8px 10px; color: var(--text); white-space: pre-wrap; }
@media (max-width: 900px) { .kc-rr-item { grid-template-columns: 1fr; } .kc-rr-acc { grid-column: 1; } }

/* Origen publicitario */
.etq-ad { max-width: 190px; overflow: hidden; text-overflow: ellipsis; }
.kf-anuncio a { font-size: 12.5px; display: inline-block; margin-top: 6px; }
/* Compacto: una línea con miniatura; el detalle se despliega al tocar */
.ad-card { background: var(--surface); border: 1px solid var(--border); border-left: 3px solid #B0654A; border-radius: 8px; font-size: 12.5px; }
.ad-card summary { display: flex; gap: 8px; align-items: center; padding: 6px 8px; cursor: pointer; list-style: none; }
.ad-card summary::-webkit-details-marker { display: none; }
.ad-card summary::after { content: "›"; margin-left: auto; color: var(--text-muted); font-size: 16px; transition: transform .15s; }
.ad-card[open] summary::after { transform: rotate(90deg); }
.ad-card img { width: 36px !important; height: 36px !important; max-width: 36px; object-fit: cover; border-radius: 6px; flex: 0 0 36px; }
.ad-card summary span { display: flex; flex-direction: column; min-width: 0; }
.ad-card summary small { color: #B0654A; font-weight: 650; font-size: 10.5px; }
.ad-card summary strong { font-size: 13px; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ad-det { display: flex; flex-direction: column; gap: 3px; padding: 0 10px 8px; color: var(--text-muted); }
.ad-det b { color: var(--text); font-weight: 600; }
.ad-body { white-space: pre-wrap; margin-top: 4px; color: var(--text); }
.msg-ad img { max-width: 48px; }
.msg-ad { display: flex; gap: 8px; background: var(--velo); border: 1px solid var(--border); border-left: 3px solid #B0654A;
  border-radius: 8px; padding: 6px 8px; margin: 0 0 6px; color: var(--text); max-width: 340px; }
.msg-ad:hover { text-decoration: none; }
.msg-ad img { width: 48px; height: 48px; object-fit: cover; border-radius: 6px; flex: 0 0 48px; }
.msg-ad span { display: flex; flex-direction: column; min-width: 0; font-size: 12px; }
.msg-ad small { color: #B0654A; font-weight: 650; font-size: 10.5px; }
.msg-ad strong { font-size: 13px; }
.msg-ad em { font-style: normal; color: var(--text-muted); font-size: 11.5px; }

/* ✨ Sugerir respuesta */
.kc-ia { position: relative; }
.kc-ia.cargando { animation: kc-ia-pulso 1s ease-in-out infinite; }
@keyframes kc-ia-pulso { 50% { transform: scale(.88); opacity: .55; } }

/* Botones de las plantillas, como los ve el paciente en WhatsApp */
.msg-btns { display: flex; flex-direction: column; gap: 4px; margin: 8px -4px 2px; }
.msg-btn { display: block; text-align: center; padding: 7px 10px; border-radius: 10px; background: var(--velo); border: 1px solid var(--border-soft);
  color: #1A73E8; font-weight: 600; font-size: 13px; text-decoration: none; }
a.msg-btn:hover { text-decoration: underline; }

/* Tareas del contacto en la ficha del chat */
.kf-tarea { display: flex; gap: 8px; align-items: baseline; margin: 0 0 6px; font-size: 13px; }
.kf-tarea button { border: none; background: none; color: var(--accent); font-size: 15px; cursor: pointer; padding: 0; line-height: 1; }
.kf-tarea small { color: var(--text-faint); }
.kf-tarea-nueva { display: grid; grid-template-columns: minmax(0, 1fr) 118px; gap: 6px; margin: 0; }
.kf-tarea-nueva input { font-size: 12.5px; padding: 6px 8px; }
