:root {
  --cyan: #00b5e6;
  --cyan-soft: #e3f6fc;
  --purple: #5530ad;
  --purple-soft: #efeafb;
  --yellow: #ffd54f;
  --yellow-soft: #fff7d9;
  --ink: #1d1736;
  --muted: #6c6889;
  --line: #e6e4f0;
  --bg: #f5f5fa;
  --card: #ffffff;
  --danger: #e5484d;
  --success: #1fa971;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(29, 23, 54, .06), 0 4px 16px rgba(29, 23, 54, .05);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); font-size: 14px; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error-text { color: var(--danger); min-height: 1.2em; margin: 4px 0 8px; font-size: 13px; }

/* ---------- login ---------- */
.login {
  min-height: 100%; display: grid; place-items: center; padding: 16px;
  background:
    radial-gradient(circle at 15% 20%, rgba(0, 181, 230, .15), transparent 40%),
    radial-gradient(circle at 85% 80%, rgba(85, 48, 173, .15), transparent 40%),
    radial-gradient(circle at 70% 15%, rgba(255, 213, 79, .2), transparent 35%), var(--bg);
}
.login-card { width: min(380px, 100%); background: var(--card); padding: 32px; border-radius: 18px; box-shadow: var(--shadow); text-align: center; }
.login-card label { text-align: left; }
.login-logo { width: 96px; height: 96px; object-fit: contain; }
.login-card h1 { margin: 4px 0; font-size: 26px; }
.login-card h1 span, .brand b { color: var(--purple); }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: 220px 1fr; height: 100vh; }
.sidebar { background: var(--card); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 16px 12px; gap: 8px; }
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; padding: 4px 8px 16px; font-size: 19px; font-weight: 600; }
.brand img { width: 40px; height: 40px; }
#nav { display: flex; flex-direction: column; gap: 2px; }
#nav a { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 10px; text-decoration: none; color: var(--muted); font-weight: 500; position: relative; }
#nav a svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; stroke-linecap: round; }
#nav a:hover { background: var(--bg); color: var(--ink); }
#nav a.active { background: var(--cyan-soft); color: var(--ink); }
#nav a.active svg { stroke: var(--cyan); }
#nav a.active::before { content: ""; position: absolute; left: -12px; top: 8px; bottom: 8px; width: 4px; border-radius: 0 4px 4px 0; background: linear-gradient(var(--cyan), var(--purple)); }
.badge { margin-left: auto; background: var(--purple); color: #fff; font-style: normal; font-size: 11px; font-weight: 700; border-radius: 99px; padding: 2px 7px; }
.nav-logout { margin-top: auto; background: none; border: 0; color: var(--muted); padding: 10px 12px; text-align: left; cursor: pointer; border-radius: 10px; }
.nav-logout:hover { background: var(--bg); color: var(--ink); }
.view { overflow: auto; padding: 24px 28px; min-width: 0; }
.view.full { padding: 0; overflow: hidden; display: flex; flex-direction: column; }

.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head h1 { margin: 0; font-size: 22px; }
.page-head .spacer { flex: 1; }
.view.full .page-head { padding: 20px 24px 0; margin-bottom: 16px; }

/* ---------- controls ---------- */
.btn { border: 1px solid var(--line); background: var(--card); padding: 8px 14px; border-radius: 10px; cursor: pointer; font-weight: 500; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.btn:hover { border-color: #cfcbe2; }
.btn.primary { background: var(--purple); border-color: var(--purple); color: #fff; }
.btn.primary:hover { background: #48279a; }
.btn.accent { background: var(--cyan); border-color: var(--cyan); color: #fff; }
.btn.danger { color: var(--danger); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.block { width: 100%; justify-content: center; }
.btn:disabled { opacity: .6; cursor: default; }
.icon-btn { border: 0; background: none; cursor: pointer; padding: 4px 6px; border-radius: 6px; color: var(--muted); }
.icon-btn:hover { background: var(--bg); color: var(--ink); }

label { display: flex; flex-direction: column; gap: 5px; font-weight: 500; font-size: 13px; margin-bottom: 12px; }
label.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 400; font-size: 14px; }
input:not([type=checkbox]):not([type=color]), select, textarea {
  border: 1px solid var(--line); border-radius: 10px; padding: 9px 11px; background: #fff; outline: none; width: 100%;
}
input:focus, select:focus, textarea:focus { border-color: var(--cyan); box-shadow: 0 0 0 3px rgba(0, 181, 230, .15); }
textarea { resize: vertical; min-height: 80px; }
input[type=color] { width: 40px; height: 36px; border: 1px solid var(--line); border-radius: 8px; padding: 2px; background: #fff; }
.search { max-width: 260px; }
.hint { font-size: 12px; color: var(--muted); font-weight: 400; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }

.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; margin-bottom: 20px; }
.card h2 { font-size: 16px; margin: 0 0 14px; }

.tag { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 99px; background: var(--yellow-soft); color: #7a5c00; margin: 2px 4px 0 0; }
.pill { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; padding: 3px 10px; border-radius: 99px; background: var(--bg); }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex: none; }

/* ---------- panel ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 16px; margin-bottom: 20px; }
.stat { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; border-top: 4px solid var(--c, var(--cyan)); }
.stat small { color: var(--muted); font-weight: 500; }
.stat strong { display: block; font-size: 26px; margin: 6px 0 2px; }
.funnel-row { display: grid; grid-template-columns: 160px 1fr 120px; align-items: center; gap: 12px; padding: 6px 0; }
.funnel-bar { height: 12px; border-radius: 6px; background: var(--bg); overflow: hidden; }
.funnel-bar span { display: block; height: 100%; border-radius: 6px; }
.funnel-row b { text-align: right; font-weight: 600; }
.notice { display: flex; gap: 12px; align-items: center; background: var(--yellow-soft); border: 1px solid #f4dc85; border-radius: var(--radius); padding: 12px 16px; margin-bottom: 20px; }

table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--line); }
td { padding: 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
td.actions { text-align: right; white-space: nowrap; }
.empty { padding: 32px; text-align: center; color: var(--muted); }

/* ---------- kanban ---------- */
.board { flex: 1; display: flex; gap: 14px; overflow-x: auto; padding: 0 24px 24px; align-items: flex-start; }
.column { width: 272px; flex: none; background: #ecebf4; border-radius: var(--radius); display: flex; flex-direction: column; max-height: 100%; }
.column-head { padding: 12px 14px 10px; border-top: 4px solid var(--c); border-radius: var(--radius) var(--radius) 0 0; }
.column-head h3 { margin: 0; font-size: 13px; text-transform: uppercase; letter-spacing: .03em; display: flex; justify-content: space-between; }
.column-head small { color: var(--muted); }
.column-body { padding: 4px 10px 10px; overflow-y: auto; flex: 1; min-height: 60px; display: flex; flex-direction: column; gap: 8px; }
.column.drag-over { background: #e1f3fa; }
.lead-card { background: var(--card); border-radius: 10px; padding: 11px 12px; box-shadow: 0 1px 2px rgba(29, 23, 54, .08); cursor: grab; border-left: 3px solid transparent; }
.lead-card:hover { border-left-color: var(--cyan); }
.lead-card.dragging { opacity: .4; }
.lead-card h4 { margin: 0 0 4px; font-size: 14px; display: flex; justify-content: space-between; gap: 6px; }
.lead-card .meta { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); margin-top: 6px; }
.lead-card .value { font-weight: 700; color: var(--ink); }
.add-lead { margin: 0 10px 10px; border: 1px dashed #c9c5dc; background: transparent; border-radius: 10px; padding: 8px; color: var(--muted); cursor: pointer; }
.add-lead:hover { background: var(--card); color: var(--ink); }

/* ---------- chats ---------- */
.chat-layout { flex: 1; display: grid; grid-template-columns: 300px 1fr 280px; min-height: 0; border-top: 1px solid var(--line); }
.chat-list { border-right: 1px solid var(--line); background: var(--card); display: flex; flex-direction: column; min-height: 0; }
.chat-list .search-box { padding: 12px; border-bottom: 1px solid var(--line); }
.chat-items { overflow-y: auto; flex: 1; }
.chat-item { display: flex; gap: 10px; padding: 12px 14px; cursor: pointer; border-bottom: 1px solid #f1f0f6; text-decoration: none; }
.chat-item:hover { background: #faf9fd; }
.chat-item.active { background: var(--cyan-soft); }
.chat-item .body { min-width: 0; flex: 1; }
.chat-item .top { display: flex; justify-content: space-between; gap: 8px; }
.chat-item .top b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .top small { color: var(--muted); flex: none; }
.chat-item .preview { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: flex; justify-content: space-between; gap: 6px; }
.avatar { width: 40px; height: 40px; border-radius: 50%; flex: none; display: grid; place-items: center; color: #fff; font-weight: 700; background: linear-gradient(135deg, var(--cyan), var(--purple)); }
.chat-main { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: #f0eff7; }
.chat-header { display: flex; align-items: center; gap: 12px; padding: 12px 18px; background: var(--card); border-bottom: 1px solid var(--line); }
.chat-header .who { flex: 1; min-width: 0; }
.chat-header .back { display: none; text-decoration: none; font-size: 18px; }
.chat-header .who small { display: block; color: var(--muted); }
.thread { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 6px; }
.bubble { max-width: min(520px, 75%); padding: 9px 12px 6px; border-radius: 12px; white-space: pre-wrap; word-wrap: break-word; box-shadow: 0 1px 1px rgba(0, 0, 0, .05); line-height: 1.4; }
.bubble.in { background: var(--card); align-self: flex-start; border-top-left-radius: 4px; }
.bubble.out { background: #d9f3fb; align-self: flex-end; border-top-right-radius: 4px; }
.bubble.out.bot { background: var(--purple-soft); }
.bubble footer { font-size: 11px; color: var(--muted); text-align: right; margin-top: 3px; display: flex; gap: 6px; justify-content: flex-end; }
.bubble .who-tag { font-size: 11px; font-weight: 700; color: var(--purple); margin-bottom: 2px; }
.bubble .failed { color: var(--danger); }
.day-sep { align-self: center; font-size: 12px; color: var(--muted); background: #e6e4f0; border-radius: 99px; padding: 3px 10px; margin: 8px 0; }
.composer { display: flex; gap: 10px; padding: 12px 16px; background: var(--card); border-top: 1px solid var(--line); align-items: flex-end; }
.composer textarea { min-height: 42px; max-height: 160px; resize: none; }
.chat-side { border-left: 1px solid var(--line); background: var(--card); overflow-y: auto; padding: 18px; }
.chat-side h3 { font-size: 13px; text-transform: uppercase; color: var(--muted); letter-spacing: .03em; margin: 18px 0 8px; }
.chat-side h3:first-child { margin-top: 0; }
.side-lead { border: 1px solid var(--line); border-radius: 10px; padding: 10px; margin-bottom: 10px; }
.side-lead b { display: block; margin-bottom: 6px; }
.side-lead select { margin-top: 6px; }
.switch { position: relative; display: inline-flex; align-items: center; gap: 8px; cursor: pointer; font-weight: 500; font-size: 13px; margin: 0; flex-direction: row; }
.switch input { appearance: none; width: 36px; height: 20px; border-radius: 99px; background: #d4d1e3; position: relative; cursor: pointer; transition: background .15s; margin: 0; }
.switch input::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s; }
.switch input:checked { background: var(--cyan); }
.switch input:checked::after { transform: translateX(16px); }
.placeholder { flex: 1; display: grid; place-items: center; color: var(--muted); text-align: center; padding: 24px; }
.placeholder img { width: 90px; opacity: .9; }

/* ---------- chatbot ---------- */
.rules { display: flex; flex-direction: column; gap: 10px; }
.rule { display: grid; grid-template-columns: auto 1fr auto; gap: 14px; align-items: start; border: 1px solid var(--line); border-radius: 10px; padding: 14px; }
.rule.off { opacity: .55; }
.rule h4 { margin: 0 0 4px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.rule p { margin: 6px 0 0; white-space: pre-wrap; color: var(--muted); }
.rule .order { display: flex; flex-direction: column; }
.trigger { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 99px; background: var(--cyan-soft); color: #00759a; }
.action { font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 99px; background: var(--purple-soft); color: var(--purple); }
.bot-grid { display: grid; grid-template-columns: 1fr 340px; gap: 20px; align-items: start; }
.test-result { margin-top: 12px; padding: 12px; border-radius: 10px; background: var(--purple-soft); white-space: pre-wrap; }

/* ---------- ajustes ---------- */
.stage-row { display: flex; gap: 10px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line); }
.stage-row:last-child { border-bottom: 0; }
.stage-row input[type=text] { flex: 1; }
.stage-row select { width: 130px; }
.code { font-family: ui-monospace, Consolas, monospace; background: var(--bg); padding: 8px 10px; border-radius: 8px; word-break: break-all; display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.status-ok { color: var(--success); font-weight: 600; }
.status-off { color: var(--muted); font-weight: 600; }

/* ---------- canales ---------- */
.ch { display: inline-flex; align-items: center; gap: 4px; font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 99px; color: #fff; white-space: nowrap; }
.ch-whatsapp { background: #25d366; }
.ch-messenger { background: #0084ff; }
.ch-instagram { background: linear-gradient(45deg, #f58529, #dd2a7b, #8134af); }
.ch-tiktok { background: #111; box-shadow: inset 2px 0 0 #25f4ee, inset -2px 0 0 #fe2c55; }
.avatar .ch { position: absolute; bottom: -3px; right: -5px; padding: 0; width: 16px; height: 16px; font-size: 9px; justify-content: center; border: 2px solid #fff; }
.avatar { position: relative; }
.channel-tabs { display: flex; gap: 4px; padding: 8px 12px 0; flex-wrap: wrap; }
.channel-tabs button { border: 1px solid var(--line); background: #fff; border-radius: 99px; padding: 3px 10px; font-size: 12px; cursor: pointer; color: var(--muted); }
.channel-tabs button.active { background: var(--ink); border-color: var(--ink); color: #fff; }
.channel-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }
.channel-tile { border: 1px solid var(--line); border-radius: 10px; padding: 12px; display: flex; flex-direction: column; gap: 6px; }
.channel-tile strong { font-size: 20px; }
.readonly-bar { padding: 14px 18px; background: var(--yellow-soft); border-top: 1px solid #f4dc85; color: #7a5c00; font-weight: 500; text-align: center; }
.bubble .author { font-size: 11px; font-weight: 700; color: #00759a; margin-bottom: 2px; }

/* ---------- usuarios ---------- */
.me { padding: 10px 12px 0; border-top: 1px solid var(--line); margin-top: auto; font-size: 13px; }
.me b { display: block; }
.me + .nav-logout { margin-top: 0; }
.role { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 99px; }
.role-admin { background: var(--purple-soft); color: var(--purple); }
.role-agent { background: var(--cyan-soft); color: #00759a; }
.role-supervisor { background: var(--yellow-soft); color: #7a5c00; }
.role-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; margin-bottom: 20px; }
.role-card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; }
.role-card p { margin: 8px 0 0; color: var(--muted); font-size: 13px; }
tr.inactive td { opacity: .5; }

/* ---------- modal & toast ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(29, 23, 54, .45); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal { background: var(--card); border-radius: 16px; width: min(520px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0, 0, 0, .25); }
.modal header { display: flex; justify-content: space-between; align-items: center; padding: 18px 20px 6px; }
.modal header h2 { margin: 0; font-size: 18px; }
.modal .modal-body { padding: 12px 20px; overflow-y: auto; }
.modal footer { display: flex; gap: 8px; padding: 12px 20px 18px; justify-content: flex-end; }
.modal footer .left { margin-right: auto; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(20px); background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 10px; opacity: 0; transition: .2s; pointer-events: none; z-index: 60; max-width: calc(100% - 32px); }
.toast.show { opacity: 1; transform: translateX(-50%); }
.toast.error { background: var(--danger); }

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  .chat-layout { grid-template-columns: 260px 1fr; }
  .chat-side { display: none; }
  .bot-grid { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .sidebar { order: 2; flex-direction: row; padding: 6px; border-right: 0; border-top: 1px solid var(--line); }
  .brand, .nav-logout, .me { display: none; }
  #nav { flex-direction: row; flex: 1; justify-content: space-around; }
  #nav a { flex-direction: column; gap: 2px; padding: 6px; font-size: 11px; }
  #nav a.active::before { display: none; }
  #nav .badge { position: absolute; top: 0; right: 0; }
  .view { padding: 16px; }
  .view.full .page-head { padding: 16px 16px 0; }
  .board { padding: 0 16px 16px; }
  .chat-layout { grid-template-columns: 1fr; }
  .chat-layout.has-active .chat-list { display: none; }
  .chat-layout:not(.has-active) .chat-main { display: none; }
  .chat-header .back { display: inline-block; }
  .funnel-row { grid-template-columns: 100px 1fr 80px; }
  .grid-2 { grid-template-columns: 1fr; }
}

[hidden] { display: none !important; }
