:root {
  --bg: #f5f6fa;
  --panel: #ffffff;
  --line: #e6e8ef;
  --text: #1c1f2a;
  --muted: #6b7183;
  --primary: #4f46e5;
  --primary-soft: #eef0ff;
  --side: #14162a;
  --side-text: #b9bdd6;
  --green: #12a150;
  --green-soft: #e3f6ea;
  --amber: #c27803;
  --amber-soft: #fdf3dc;
  --red: #d33a3a;
  --red-soft: #fde8e8;
  --blue: #2563eb;
  --blue-soft: #e5eeff;
  --gray-soft: #eef0f4;
  --bubble-in: #ffffff;
  --bubble-ai: #e9e7ff;
  --bubble-agent: #dcf5e4;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 22, 42, .06), 0 4px 16px rgba(20, 22, 42, .05);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: Inter, system-ui, -apple-system, sans-serif; font-size: 14px; color: var(--text); background: var(--bg); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--red); min-height: 18px; font-size: 13px; }
h1 { font-size: 22px; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 13px; margin: 0 0 8px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }

input, select, textarea {
  font: inherit; color: inherit; width: 100%; padding: 8px 10px; border: 1px solid var(--line);
  border-radius: 8px; background: #fff; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
textarea { resize: vertical; min-height: 70px; }
label { display: grid; gap: 5px; font-size: 12px; font-weight: 500; color: var(--muted); }

.btn {
  font: inherit; font-weight: 500; border: 1px solid var(--line); background: #fff; color: var(--text);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--gray-soft); }
.btn.primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--red); }
.btn.sm { padding: 5px 10px; font-size: 12px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.link { background: none; border: 0; color: inherit; cursor: pointer; text-decoration: underline; font: inherit; padding: 0; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; background: linear-gradient(135deg, #14162a, #2d2a6e); padding: 16px; }
.login-card { width: 100%; max-width: 380px; background: #fff; border-radius: 16px; padding: 32px; display: grid; gap: 14px; box-shadow: 0 20px 60px rgba(0,0,0,.3); }

/* Layout */
.app { display: grid; grid-template-columns: 230px 1fr; height: 100%; }
.sidebar { background: var(--side); color: var(--side-text); display: flex; flex-direction: column; padding: 18px 12px; }
.brand { display: flex; align-items: center; justify-content: center; padding: 4px 8px 22px; }
.brand-full { width: 150px; height: auto; display: block; }
.brand-icon { display: none; width: 30px; height: auto; }
.login-logo { display: grid; justify-items: center; gap: 10px; padding: 6px 0 4px; }
.login-logo img { width: 210px; height: auto; }
.login-logo span { font-size: 11px; font-weight: 600; letter-spacing: .35em; color: var(--muted); padding-left: .35em; }
.login-card > .muted { text-align: center; }
.brand.big { color: var(--text); font-size: 20px; padding: 0; }
.logo { width: 30px; height: 30px; border-radius: 8px; background: var(--primary); color: #fff; display: grid; place-items: center; font-weight: 700; }
nav { display: grid; gap: 2px; }
nav a { display: flex; align-items: center; gap: 10px; color: var(--side-text); text-decoration: none; padding: 9px 10px; border-radius: 8px; font-weight: 500; }
nav a:hover { background: rgba(255,255,255,.06); color: #fff; }
nav a.active { background: rgba(255,255,255,.1); color: #fff; }
.ico { width: 18px; text-align: center; opacity: .8; }
.badge { margin-left: auto; background: var(--primary); color: #fff; font-size: 11px; border-radius: 10px; padding: 1px 7px; }
.sidebar-foot { margin-top: auto; display: grid; gap: 12px; font-size: 12px; }
.integrations { display: grid; gap: 4px; padding: 10px; border-radius: 8px; background: rgba(255,255,255,.04); }
.integrations div { display: flex; align-items: center; gap: 6px; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: #666; display: inline-block; }
.dot.on { background: #34d399; }
.dot.off { background: #f87171; }
.me { display: flex; justify-content: space-between; padding: 0 8px; }

main { overflow: auto; min-width: 0; }
.page { padding: 24px 28px; max-width: 1400px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.grid { display: grid; gap: 16px; }
.kpis { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
.kpi .label { color: var(--muted); font-size: 12px; font-weight: 500; }
.kpi .value { font-size: 28px; font-weight: 700; margin-top: 6px; letter-spacing: -.02em; }
.kpi .hint { font-size: 12px; color: var(--muted); margin-top: 2px; }
.two { grid-template-columns: 1.4fr 1fr; }

.pill { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 20px; background: var(--gray-soft); color: var(--muted); white-space: nowrap; }
.st-nuevo { background: var(--blue-soft); color: var(--blue); }
.st-calificando { background: var(--amber-soft); color: var(--amber); }
.st-calificado { background: var(--green-soft); color: var(--green); }
.st-agendado { background: var(--primary-soft); color: var(--primary); }
.st-cliente { background: #d1fae5; color: #047857; }
.st-no_calificado { background: var(--red-soft); color: var(--red); }
.st-agendada { background: var(--primary-soft); color: var(--primary); }
.st-completada { background: var(--green-soft); color: var(--green); }
.st-cancelada { background: var(--red-soft); color: var(--red); }
.st-no_asistio { background: var(--amber-soft); color: var(--amber); }

table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 500; padding: 8px 10px; border-bottom: 1px solid var(--line); }
td { padding: 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
tr:last-child td { border-bottom: 0; }
.table-wrap { overflow-x: auto; }

.bars { display: flex; align-items: flex-end; gap: 3px; height: 120px; padding-top: 8px; }
.bars div { flex: 1; background: var(--primary); border-radius: 3px 3px 0 0; min-height: 2px; opacity: .85; }
.funnel { display: grid; gap: 8px; }
.funnel-row { display: grid; grid-template-columns: 110px 1fr 40px; align-items: center; gap: 10px; font-size: 13px; }
.funnel-bar { height: 10px; border-radius: 6px; background: var(--gray-soft); overflow: hidden; }
.funnel-bar span { display: block; height: 100%; background: var(--primary); }

.list-item { display: flex; justify-content: space-between; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.list-item:last-child { border-bottom: 0; }

/* Conversaciones */
.chats { display: grid; grid-template-columns: 320px 1fr 320px; height: 100vh; }
.chat-list { border-right: 1px solid var(--line); background: #fff; display: flex; flex-direction: column; min-height: 0; }
.chat-list-head { padding: 16px; border-bottom: 1px solid var(--line); display: grid; gap: 10px; }
.filters { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { font-size: 12px; padding: 4px 10px; border-radius: 20px; border: 1px solid var(--line); background: #fff; cursor: pointer; }
.chip.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.chat-items { overflow-y: auto; flex: 1; }
.chat-item { padding: 12px 16px; border-bottom: 1px solid var(--line); cursor: pointer; display: grid; gap: 4px; }
.chat-item:hover { background: #fafbff; }
.chat-item.active { background: var(--primary-soft); }
.chat-item .row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.chat-item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .preview { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.unread-dot { background: var(--green); color: #fff; font-size: 11px; border-radius: 10px; padding: 0 7px; font-weight: 600; }
.flag { color: var(--red); font-weight: 600; font-size: 11px; }

.chat-main { display: flex; flex-direction: column; min-height: 0; background: #efeae2; }
.chat-head { background: #fff; padding: 12px 18px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.chat-head .title { font-weight: 600; font-size: 15px; }
.messages { flex: 1; overflow-y: auto; padding: 18px 8%; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 72%; padding: 8px 11px 6px; border-radius: 10px; box-shadow: 0 1px 1px rgba(0,0,0,.08); word-wrap: break-word; line-height: 1.4; }
.msg.in { background: var(--bubble-in); align-self: flex-start; border-top-left-radius: 2px; }
.msg.ai { background: var(--bubble-ai); align-self: flex-end; border-top-right-radius: 2px; }
.msg.agent { background: var(--bubble-agent); align-self: flex-end; border-top-right-radius: 2px; }
.msg .meta { font-size: 11px; color: var(--muted); text-align: right; margin-top: 3px; display: flex; gap: 6px; justify-content: flex-end; }
.msg .who { font-size: 11px; font-weight: 600; color: var(--primary); margin-bottom: 2px; }
.msg.agent .who { color: var(--green); }
.msg.failed { outline: 1px solid var(--red); }
.note { align-self: center; font-size: 12px; background: #fff8dc; color: #6b5800; padding: 4px 10px; border-radius: 8px; }
.day-sep { align-self: center; font-size: 11px; background: rgba(255,255,255,.8); padding: 3px 10px; border-radius: 8px; color: var(--muted); margin: 6px 0; }
.typing { align-self: flex-end; font-size: 12px; color: var(--primary); font-style: italic; }
.composer { background: #f0f2f5; padding: 10px 14px; display: grid; gap: 6px; }
.composer form { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { min-height: 42px; max-height: 160px; border-radius: 20px; padding: 10px 16px; }
.composer .warn { font-size: 12px; color: var(--amber); }
.empty { display: grid; place-items: center; height: 100%; color: var(--muted); text-align: center; padding: 20px; }

.lead-panel { border-left: 1px solid var(--line); background: #fff; overflow-y: auto; padding: 16px; display: grid; gap: 16px; align-content: start; }
.toggle { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 12px; border-radius: 8px; background: var(--gray-soft); }
.switch { position: relative; width: 38px; height: 22px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: #c5c9d6; border-radius: 22px; cursor: pointer; transition: .2s; }
.switch span::before { content: ''; position: absolute; width: 16px; height: 16px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: .2s; }
.switch input:checked + span { background: var(--green); }
.switch input:checked + span::before { transform: translateX(16px); }
.form-grid { display: grid; gap: 10px; }
.form-grid.cols { grid-template-columns: 1fr 1fr; }
.score { display: flex; align-items: center; gap: 8px; }
.score-bar { flex: 1; height: 6px; background: var(--gray-soft); border-radius: 4px; overflow: hidden; }
.score-bar span { display: block; height: 100%; background: linear-gradient(90deg, var(--amber), var(--green)); }

/* Kanban */
.kanban { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(230px, 1fr); gap: 12px; overflow-x: auto; padding-bottom: 8px; }
.column { background: #eceef4; border-radius: var(--radius); padding: 10px; min-height: 300px; display: flex; flex-direction: column; gap: 8px; }
.column.drag-over { outline: 2px dashed var(--primary); }
.column-head { display: flex; justify-content: space-between; align-items: center; padding: 2px 4px 6px; font-weight: 600; font-size: 13px; }
.lead-card { background: #fff; border-radius: 8px; padding: 10px 12px; box-shadow: 0 1px 2px rgba(0,0,0,.06); cursor: grab; display: grid; gap: 4px; }
.lead-card:hover { box-shadow: 0 2px 8px rgba(0,0,0,.1); }
.lead-card .name { font-weight: 600; }
.lead-card .sub { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.view-switch { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.view-switch button { border: 0; background: #fff; padding: 7px 12px; cursor: pointer; font: inherit; }
.view-switch button.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }

/* Calendario */
.cal { display: grid; grid-template-columns: repeat(7, 1fr); border-top: 1px solid var(--line); border-left: 1px solid var(--line); }
.cal .dow { padding: 8px; font-size: 12px; color: var(--muted); font-weight: 600; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); background: #fafbfd; }
.cal .day { min-height: 110px; padding: 6px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); background: #fff; display: flex; flex-direction: column; gap: 3px; cursor: pointer; }
.cal .day:hover { background: #fafbff; }
.cal .day.other { background: #fafafa; color: #b0b4c0; }
.cal .day.today .num { background: var(--primary); color: #fff; }
.cal .num { font-size: 12px; font-weight: 600; width: 22px; height: 22px; display: grid; place-items: center; border-radius: 50%; }
.ev { font-size: 11px; padding: 3px 6px; border-radius: 4px; background: var(--primary-soft); color: var(--primary); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; cursor: pointer; }
.ev.cancelada { background: var(--red-soft); color: var(--red); text-decoration: line-through; }
.ev.completada { background: var(--green-soft); color: var(--green); }
.cal-nav { display: flex; align-items: center; gap: 8px; }

/* Modal y toasts */
.modal { position: fixed; inset: 0; background: rgba(20,22,42,.45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal-card { background: #fff; border-radius: 14px; width: 100%; max-width: 520px; max-height: 90vh; overflow: auto; padding: 22px; display: grid; gap: 14px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }
.toasts { position: fixed; right: 16px; bottom: 16px; display: grid; gap: 8px; z-index: 60; }
.toast { background: var(--side); color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); max-width: 360px; font-size: 13px; cursor: pointer; }
.toast.err { background: var(--red); }

.settings { grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); align-items: start; }
.kv { display: grid; grid-template-columns: 140px 1fr; gap: 6px 10px; font-size: 13px; }
.kv code { background: var(--gray-soft); padding: 2px 6px; border-radius: 4px; word-break: break-all; }
.slots { display: flex; flex-wrap: wrap; gap: 6px; max-height: 200px; overflow: auto; }
.slots button.active { background: var(--primary); color: #fff; border-color: var(--primary); }

.back-btn { display: none; }

@media (max-width: 1200px) {
  .chats { grid-template-columns: 280px 1fr; }
  .lead-panel { display: none; }
  .chats.show-lead .lead-panel { display: grid; position: fixed; right: 0; top: 0; bottom: 0; width: 340px; z-index: 30; box-shadow: -8px 0 24px rgba(0,0,0,.12); }
}
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .sidebar { flex-direction: row; align-items: center; padding: 8px 12px; overflow-x: auto; gap: 8px; }
  .brand { padding: 0; }
  .sidebar-foot, nav a .ico, .brand-full { display: none; }
  .brand-icon { display: block; }
  nav { display: flex; }
  nav a { padding: 7px 9px; font-size: 13px; }
  .page { padding: 16px; }
  .two { grid-template-columns: 1fr; }
  .chats { grid-template-columns: 1fr; height: calc(100vh - 52px); }
  .chats.has-chat .chat-list { display: none; }
  .chats:not(.has-chat) .chat-main { display: none; }
  .back-btn { display: inline-block; }
  .messages { padding: 12px; }
  .msg { max-width: 88%; }
  .settings { grid-template-columns: 1fr; }
  .cal .day { min-height: 70px; }
  .chats.show-lead .lead-panel { width: 100%; }
}
.msg .body { white-space: pre-wrap; }
