:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --panel-2: #f9fafb;
  --border: #e5e7eb;
  --text: #111827;
  --muted: #6b7280;
  --primary: #4f46e5;
  --primary-ink: #ffffff;
  --primary-soft: #eef2ff;
  --danger: #dc2626;
  --ok: #16a34a;
  --warn-bg: #fef3c7;
  --warn-ink: #92400e;
  --in-bubble: #ffffff;
  --out-bubble: #e0e7ff;
  --bot-bubble: #f3f4f6;
  --chat-bg: #eef0f4;
  --wa: #25d366;
  --ig: #d62976;
  --sf: #2563eb;
  --sf-soft: #dbeafe;
  --mcbo: #ea580c;
  --mcbo-soft: #ffedd5;
  --shadow: 0 10px 30px rgba(17, 24, 39, .12);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --panel: #171a21;
    --panel-2: #1d212a;
    --border: #2a2f3a;
    --text: #e5e7eb;
    --muted: #9ca3af;
    --primary: #818cf8;
    --primary-ink: #0f1115;
    --primary-soft: #262a46;
    --danger: #f87171;
    --ok: #4ade80;
    --warn-bg: #3b2f0b;
    --warn-ink: #fcd34d;
    --in-bubble: #1f232c;
    --out-bubble: #2e3360;
    --bot-bubble: #262a33;
    --chat-bg: #12151b;
    --sf: #60a5fa;
    --sf-soft: #1e2b45;
    --mcbo: #fb923c;
    --mcbo-soft: #3d2615;
    --shadow: 0 10px 30px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; height: 100dvh; margin: 0; }
body {
  font: 14px/1.45 "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}
.hidden { display: none !important; }
button { font: inherit; cursor: pointer; }
input, textarea, select { font: inherit; color: inherit; }

/* ---------- Botones ---------- */
.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  padding: 7px 14px;
  border-radius: 8px;
  font-weight: 500;
  white-space: nowrap;
}
.btn:hover { background: var(--panel-2); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger-outline { color: var(--danger); }
.btn.block { width: 100%; padding: 10px; }
.icon-btn {
  border: none; background: transparent; color: var(--muted);
  width: 32px; height: 32px; border-radius: 8px; font-size: 18px;
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }

/* ---------- Login ---------- */
.login { height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(400px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px;
  box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 14px;
}
.brand { display: flex; gap: 12px; align-items: center; margin-bottom: 6px; }
.brand h1 { font-size: 19px; margin: 0; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.logo { width: 46px; height: 46px; border-radius: 12px; flex: none; }
label { display: flex; flex-direction: column; gap: 5px; font-weight: 500; font-size: 13px; }
input[type=text], input:not([type]), input[type=password], input[type=url], input[type=search], select, textarea {
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 8px;
  padding: 9px 11px;
  outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.server-details { font-size: 12.5px; color: var(--muted); }
.server-details summary { cursor: pointer; margin-bottom: 6px; }
.server-details input { width: 100%; }
.form-error { color: var(--danger); font-size: 13px; min-height: 1em; }

/* ---------- Layout ---------- */
.app { display: grid; grid-template-columns: 340px 1fr; height: 100%; }
.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
.me { display: flex; align-items: center; gap: 10px; padding: 14px 12px 10px 16px; position: relative; }
.me-info { flex: 1; min-width: 0; }
.me-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.me-branch { font-size: 12px; color: var(--muted); }
.menu {
  position: absolute; right: 12px; top: 48px; z-index: 20;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: var(--shadow); padding: 6px; min-width: 190px;
  display: flex; flex-direction: column;
}
.menu button { border: none; background: none; color: var(--text); text-align: left; padding: 8px 10px; border-radius: 6px; }
.menu button:hover { background: var(--panel-2); }

.status-row { display: flex; align-items: center; gap: 8px; padding: 0 16px 10px; font-size: 13px; color: var(--muted); }
.conn { margin-left: auto; width: 9px; height: 9px; border-radius: 50%; background: var(--danger); }
.conn.on { background: var(--ok); }
.switch { position: relative; display: inline-block; width: 34px; height: 20px; flex-direction: row; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; inset: 0; background: var(--border); border-radius: 20px; transition: .2s; cursor: pointer; }
.slider::before { content: ""; position: absolute; width: 14px; height: 14px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: .2s; }
.switch input:checked + .slider { background: var(--ok); }
.switch input:checked + .slider::before { transform: translateX(14px); }

.tabs { display: flex; flex-wrap: wrap; gap: 4px; padding: 0 12px 8px; }
.tab {
  border: none; background: transparent; color: var(--muted);
  padding: 6px 10px; border-radius: 8px; font-weight: 500; font-size: 13px;
  display: flex; align-items: center; gap: 6px; white-space: nowrap;
}
.tab:hover { background: var(--panel-2); }
.tab.active { background: var(--primary-soft); color: var(--primary); }
.tab .count { background: var(--border); color: var(--text); border-radius: 10px; padding: 0 6px; font-size: 11px; font-variant-numeric: tabular-nums; }
.tab.active .count { background: var(--primary); color: var(--primary-ink); }
.tab .count.alert { background: var(--danger); color: #fff; }

.search { padding: 0 12px 8px; }
.search input { width: 100%; }

.conv-list { list-style: none; margin: 0; padding: 0 6px 10px; overflow-y: auto; flex: 1; }
.conv {
  display: flex; gap: 10px; padding: 10px; border-radius: 10px; cursor: pointer;
  align-items: flex-start;
}
.conv:hover { background: var(--panel-2); }
.conv.active { background: var(--primary-soft); }
.conv-body { flex: 1; min-width: 0; }
.conv-top, .conv-bottom { display: flex; align-items: center; gap: 8px; }
.conv-name { font-weight: 600; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-time { font-size: 11.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.conv-preview { flex: 1; min-width: 0; color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv.unread .conv-preview { color: var(--text); font-weight: 500; }
.badge { background: var(--ok); color: #fff; border-radius: 10px; min-width: 20px; padding: 0 6px; font-size: 11.5px; font-weight: 600; text-align: center; }
.conv-tags { display: flex; gap: 4px; margin-top: 4px; flex-wrap: wrap; }
.conv-empty { color: var(--muted); text-align: center; padding: 30px 10px; }

.tag { font-size: 11px; padding: 1px 7px; border-radius: 6px; background: var(--panel-2); color: var(--muted); border: 1px solid var(--border); }
.tag.san_francisco { background: var(--sf-soft); color: var(--sf); border-color: transparent; }
.tag.maracaibo { background: var(--mcbo-soft); color: var(--mcbo); border-color: transparent; }
.tag.queue { background: var(--warn-bg); color: var(--warn-ink); border-color: transparent; }

.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex: none; position: relative;
  display: grid; place-items: center; font-weight: 600; font-size: 14px;
  background: var(--primary-soft); color: var(--primary);
}
.ch-dot {
  position: absolute; right: -2px; bottom: -2px; width: 17px; height: 17px; border-radius: 50%;
  border: 2px solid var(--panel); display: grid; place-items: center; color: #fff; font-size: 8px; font-weight: 700;
}
.ch-dot.whatsapp { background: var(--wa); }
.ch-dot.instagram { background: linear-gradient(45deg, #f58529, #dd2a7b, #8134af); }

/* ---------- Chat ---------- */
.chat { min-width: 0; min-height: 0; display: flex; flex-direction: column; background: var(--chat-bg); }
.empty { margin: auto; text-align: center; color: var(--muted); padding: 20px; }
.empty-icon { font-size: 42px; }
.empty h2 { color: var(--text); margin: 8px 0 4px; font-size: 18px; }
.chat-panel { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.chat-header {
  display: flex; align-items: center; gap: 12px; padding: 10px 16px;
  background: var(--panel); border-bottom: 1px solid var(--border);
}
.chat-title { flex: 1; min-width: 0; }
.chat-name { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-meta { font-size: 12.5px; color: var(--muted); display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.chat-actions { display: flex; gap: 8px; }

.banner { background: var(--warn-bg); color: var(--warn-ink); padding: 8px 16px; font-size: 13px; }

.messages { flex: 1; overflow-y: auto; padding: 16px 6% 10px; display: flex; flex-direction: column; gap: 4px; }
.day { align-self: center; font-size: 11.5px; color: var(--muted); background: var(--panel); border-radius: 8px; padding: 2px 10px; margin: 10px 0 6px; }
.note { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel-2); border: 1px dashed var(--border); border-radius: 8px; padding: 3px 10px; margin: 4px 0; text-align: center; max-width: 80%; }
.msg { display: flex; }
.msg.out { justify-content: flex-end; }
.bubble {
  max-width: min(560px, 75%); padding: 7px 10px 5px; border-radius: 12px;
  background: var(--in-bubble); box-shadow: 0 1px 1px rgba(0, 0, 0, .06);
  overflow-wrap: anywhere;
}
.msg.in .bubble { border-top-left-radius: 4px; }
.msg.out .bubble { background: var(--out-bubble); border-top-right-radius: 4px; }
.msg.bot .bubble { background: var(--bot-bubble); }
.who { font-size: 11.5px; font-weight: 600; color: var(--primary); margin-bottom: 2px; }
.msg.bot .who { color: var(--muted); }
.text { white-space: pre-wrap; }
.text a { color: var(--primary); }
.meta { font-size: 11px; color: var(--muted); text-align: right; margin-top: 2px; display: flex; justify-content: flex-end; gap: 4px; font-variant-numeric: tabular-nums; }
.tick.read { color: #3b82f6; }
.err { color: var(--danger); font-size: 12px; margin-top: 3px; }
.media { display: block; max-width: 100%; max-height: 320px; border-radius: 8px; margin-bottom: 4px; cursor: pointer; }
audio.media { width: 280px; cursor: default; }
.doc-link { display: inline-flex; gap: 6px; align-items: center; color: var(--primary); text-decoration: none; padding: 6px 0; }

.composer { display: flex; gap: 10px; align-items: flex-end; padding: 10px 16px 14px; background: var(--panel); border-top: 1px solid var(--border); }
.composer textarea { flex: 1; resize: none; max-height: 160px; min-height: 40px; line-height: 1.4; }
.composer.disabled textarea { background: var(--panel-2); }

/* ---------- Modal / toast ---------- */
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, .4); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal-card {
  background: var(--panel); border-radius: 14px; box-shadow: var(--shadow);
  width: min(560px, 100%); max-height: 85vh; overflow-y: auto; padding: 22px;
}
.modal-card.wide { width: min(860px, 100%); }
.modal-card h2 { margin: 0 0 14px; font-size: 18px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-grid .full { grid-column: 1 / -1; }
.check { flex-direction: row; align-items: center; gap: 8px; }
.opt { flex-direction: row; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border); margin-bottom: 6px; font-weight: 400; cursor: pointer; }
.opt:hover { background: var(--panel-2); }
.opt small { color: var(--muted); }
.opt-group { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 14px 0 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--border); flex: none; }
.dot.on { background: var(--ok); }
.dot.away { background: #f59e0b; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 600; font-size: 12px; }
td .btn { padding: 4px 10px; font-size: 12.5px; }
tr.inactive td { opacity: .5; }

.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); z-index: 60;
  background: #111827; color: #fff; padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow);
  max-width: 80%;
}
.toast.error { background: var(--danger); }

.lightbox { position: fixed; inset: 0; background: rgba(0, 0, 0, .85); display: grid; place-items: center; z-index: 70; cursor: zoom-out; }
.lightbox img { max-width: 92vw; max-height: 92vh; border-radius: 6px; }

/* ---------- Notificaciones (navegador / tablet) ---------- */
.notif-btn {
  margin: 0 12px 10px; padding: 8px 10px; border-radius: 8px; text-align: left;
  border: 1px dashed var(--primary); background: var(--primary-soft); color: var(--primary); font-weight: 500;
}

/* ---------- Envío de imágenes ---------- */
.chat-panel { position: relative; }
.composer-tools { display: flex; gap: 2px; flex: none; align-self: flex-end; }
.tool-btn { width: 40px; height: 40px; display: grid; place-items: center; color: var(--muted); }
.tool-btn:hover, .tool-btn.active { color: var(--primary); background: var(--primary-soft); }
.tool-btn:disabled { opacity: .4; cursor: default; background: none; color: var(--muted); }
.attach-preview video { width: 72px; height: 72px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); background: #000; }

/* ---------- Paneles flotantes (emojis / respuestas rápidas) ---------- */
.popover {
  position: absolute; left: 12px; z-index: 30;
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; overflow: hidden;
}
.emoji-pop { width: min(360px, calc(100% - 24px)); height: 330px; }
.emoji-tabs { display: flex; gap: 2px; padding: 6px; border-bottom: 1px solid var(--border); }
.emoji-tabs button { flex: 1; border: none; background: none; border-radius: 8px; padding: 6px 0; font-size: 18px; line-height: 1; }
.emoji-tabs button:hover { background: var(--panel-2); }
.emoji-tabs button.active { background: var(--primary-soft); }
.emoji-title { font-size: 12px; font-weight: 600; color: var(--muted); padding: 8px 10px 2px; }
.emoji-grid { flex: 1; overflow-y: auto; padding: 4px 6px 8px; display: grid; grid-template-columns: repeat(auto-fill, minmax(38px, 1fr)); align-content: start; }
.emoji-grid button {
  border: none; background: none; border-radius: 8px; height: 38px; font-size: 24px; line-height: 1;
  font-family: "Segoe UI Emoji", "Apple Color Emoji", "Noto Color Emoji", sans-serif;
}
.emoji-grid button:hover { background: var(--panel-2); transform: scale(1.12); }
.emoji-empty { grid-column: 1 / -1; color: var(--muted); font-size: 13px; padding: 16px 6px; text-align: center; }

.qr-pop { width: min(440px, calc(100% - 24px)); max-height: 360px; }
.qr-pop .qr-search { padding: 8px; border-bottom: 1px solid var(--border); }
.qr-pop .qr-search input { width: 100%; }
.qr-hint { font-size: 12px; color: var(--muted); padding: 8px 12px 4px; }
.qr-list { list-style: none; margin: 0; padding: 4px; overflow-y: auto; flex: 1; }
.qr-item { padding: 8px 10px; border-radius: 8px; cursor: pointer; }
.qr-item:hover, .qr-item.selected { background: var(--primary-soft); }
.qr-item .qr-head { display: flex; align-items: center; gap: 6px; }
.qr-item .qr-shortcut { font-weight: 600; color: var(--primary); }
.qr-item .qr-text { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.qr-empty { color: var(--muted); font-size: 13px; padding: 14px 10px; text-align: center; }
.qr-footer { display: flex; gap: 8px; justify-content: flex-end; padding: 8px; border-top: 1px solid var(--border); }
.qr-footer .btn { padding: 5px 12px; font-size: 13px; }

/* Administrador de respuestas rápidas */
.qr-table td.qr-cell-text { max-width: 380px; white-space: pre-wrap; overflow-wrap: anywhere; color: var(--muted); }
.qr-table td.actions { white-space: nowrap; text-align: right; }
.qr-table td.actions .btn + .btn { margin-left: 4px; }
.var-chips { display: flex; gap: 6px; flex-wrap: wrap; font-size: 12px; color: var(--muted); align-items: center; }
.var-chips button { border: 1px solid var(--border); background: var(--panel-2); color: var(--text); border-radius: 6px; padding: 2px 8px; font-size: 12px; }
.muted { color: var(--muted); }
.shortcut-input { display: flex; align-items: center; border: 1px solid var(--border); border-radius: 8px; background: var(--panel); }
.shortcut-input span { padding-left: 11px; color: var(--muted); font-weight: 600; }
.shortcut-input input { border: none; flex: 1; box-shadow: none !important; padding-left: 2px; }
.attach-preview {
  display: flex; align-items: center; gap: 12px; padding: 10px 16px;
  background: var(--panel); border-top: 1px solid var(--border);
}
.attach-preview img { width: 72px; height: 72px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); }
.attach-info { flex: 1; display: flex; flex-direction: column; font-size: 13px; }
.attach-info span { color: var(--muted); }
.drop-hint {
  position: absolute; inset: 8px; z-index: 5; display: grid; place-items: center;
  border: 3px dashed var(--primary); border-radius: 14px; background: color-mix(in srgb, var(--primary-soft) 85%, transparent);
  color: var(--primary); font-size: 18px; font-weight: 600; pointer-events: none;
}
.msg.sending .media { opacity: .6; }

/* ---------- Botón volver (solo pantallas angostas) ---------- */
.back-btn { display: none; font-size: 20px; }

/* ---------- Tablet / pantallas medianas ---------- */
@media (max-width: 1100px) {
  .app { grid-template-columns: 300px 1fr; }
  .chat-header { flex-wrap: wrap; row-gap: 8px; }
  .chat-title { flex: 1 1 160px; }
  .messages { padding: 12px 3% 8px; }
  .bubble { max-width: 85%; }
}

/* ---------- Pantallas angostas (tablet vertical pequeña / teléfono): un panel a la vez ---------- */
@media (max-width: 700px) {
  .app { grid-template-columns: 1fr; }
  .app.show-chat .sidebar { display: none; }
  .app:not(.show-chat) .chat { display: none; }
  .back-btn { display: grid; place-items: center; }
  .chat-actions { width: 100%; }
  .chat-actions .btn { flex: 1; }
  .composer { padding: 8px 8px 10px; gap: 6px; }
  .tool-btn { width: 36px; }
  .qr-table td.qr-cell-text { max-width: 180px; }
}

/* ---------- Pantallas táctiles: objetivos más grandes ---------- */
@media (pointer: coarse) {
  .btn { padding: 10px 16px; }
  .icon-btn { width: 42px; height: 42px; }
  .tab { padding: 9px 12px; font-size: 14px; }
  .conv { padding: 12px 10px; }
  .menu button { padding: 12px; }
  input, select, textarea { font-size: 16px; }
  .switch { transform: scale(1.15); }
}
