:root {
  --bg: #f0f2f5;
  --panel: #ffffff;
  --border: #e3e6ea;
  --text: #111b21;
  --muted: #667781;
  --green: #00a884;
  --green-dark: #008069;
  --bubble-out: #d9fdd3;
  --bubble-in: #ffffff;
  --chat-bg: #efeae2;
  --danger: #d93025;
  --warn-bg: #fff4ce;
  --hover: #f5f6f6;
  --active: #e9edef;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; font-size: 14px; color: var(--text); background: var(--bg); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.strong { font-weight: 600; }
.error { color: var(--danger); min-height: 1em; margin-top: 8px; }
input, select, textarea { font: inherit; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: #fff; color: var(--text); }
textarea { resize: none; }
label { display: block; margin-bottom: 10px; }
label > input:not([type=checkbox]), label > select, label > textarea { display: block; width: 100%; margin-top: 4px; }
code, pre { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; }
pre { background: #1e2a30; color: #e6edf0; padding: 12px; border-radius: 8px; overflow-x: auto; white-space: pre; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 4px; padding: 8px 14px; border: 1px solid var(--border); background: #fff; color: var(--text); border-radius: 8px; cursor: pointer; font: inherit; white-space: nowrap; }
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn.primary:hover { background: var(--green-dark); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.icon { padding: 8px 10px; font-size: 18px; border: none; background: transparent; }
.btn:disabled { opacity: .5; cursor: default; }

.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; }
.logo { width: 30px; height: 30px; border-radius: 50%; background: var(--green); color: #fff; display: inline-flex; align-items: center; justify-content: center; }

/* Login */
.login { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { background: var(--panel); padding: 28px; border-radius: 12px; width: 100%; max-width: 340px; box-shadow: 0 2px 12px rgba(0,0,0,.08); }
.login-card .brand { margin-bottom: 20px; }
.login-card .btn { width: 100%; }

/* Layout */
.app { display: flex; height: 100vh; height: 100dvh; }
.nav { width: 210px; background: #fff; border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 14px 10px; gap: 4px; flex-shrink: 0; }
.nav .brand { padding: 4px 8px 14px; }
.nav a { padding: 10px; border-radius: 8px; color: var(--text); text-decoration: none; }
.nav a:hover { background: var(--hover); }
.nav a.active { background: var(--active); font-weight: 600; }
.nav-foot { margin-top: auto; display: flex; flex-direction: column; gap: 8px; padding: 8px; }
.view { flex: 1; min-width: 0; }
.page { overflow-y: auto; padding: 20px 24px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head h2 { margin: 0; font-size: 20px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 16px; margin-bottom: 14px; }
.card.info { background: #e7f6f2; border-color: #bfe6dc; }
.card h3 { margin: 0 0 6px; font-size: 16px; }
.kv { display: grid; grid-template-columns: 160px 1fr; gap: 6px 12px; margin: 10px 0; }
.kv > div:nth-child(odd) { color: var(--muted); }
.kv code { word-break: break-all; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.table { width: 100%; border-collapse: collapse; background: #fff; border-radius: 12px; overflow: hidden; }
.table th, .table td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.table th { background: #fafafa; font-weight: 600; }
.pill { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 12px; background: var(--active); }
.pill.ok { background: #d8f5e9; color: #0b6b4c; }
.pill.bad { background: #fde2e1; color: #a1271d; }
.pill.warn { background: var(--warn-bg); color: #7a5a00; }

/* Chats */
.chats { display: flex; height: 100%; }
.chat-list-pane { width: 360px; background: #fff; border-right: 1px solid var(--border); display: flex; flex-direction: column; flex-shrink: 0; }
.pane-head { display: flex; gap: 8px; padding: 10px; border-bottom: 1px solid var(--border); }
.pane-head select { flex: 1; min-width: 0; }
.pane-search { padding: 8px 10px; display: flex; flex-direction: column; gap: 6px; border-bottom: 1px solid var(--border); }
.pane-search label { margin: 0; display: flex; align-items: center; gap: 6px; }
.chat-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.chat-item { display: flex; gap: 10px; padding: 10px 12px; cursor: pointer; border-bottom: 1px solid #f1f2f3; align-items: center; }
.chat-item:hover { background: var(--hover); }
.chat-item.active { background: var(--active); }
.avatar { width: 42px; height: 42px; border-radius: 50%; background: #dfe5e7; color: #54656f; display: flex; align-items: center; justify-content: center; font-weight: 600; flex-shrink: 0; }
.chat-main { flex: 1; min-width: 0; }
.chat-row { display: flex; justify-content: space-between; gap: 8px; }
.chat-name { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-time { font-size: 11px; color: var(--muted); flex-shrink: 0; }
.chat-preview { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.unread { background: var(--green); color: #fff; border-radius: 99px; font-size: 11px; min-width: 20px; height: 20px; display: inline-flex; align-items: center; justify-content: center; padding: 0 6px; flex-shrink: 0; }
.chat-item.has-unread .chat-time { color: var(--green); font-weight: 600; }

.conv-pane { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--chat-bg); }
.empty-conv { margin: auto; text-align: center; font-size: 16px; color: var(--muted); padding: 20px; }
.conv { display: flex; flex-direction: column; height: 100%; }
.conv-head { display: flex; align-items: center; gap: 10px; padding: 8px 14px; background: #f0f2f5; border-bottom: 1px solid var(--border); }
.conv-title { flex: 1; min-width: 0; }
.conv-title > div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.back-btn { display: none; }
.badge { font-size: 12px; padding: 4px 10px; border-radius: 99px; white-space: nowrap; }
.badge.open { background: #d8f5e9; color: #0b6b4c; }
.badge.closed { background: var(--warn-bg); color: #7a5a00; }
.messages { flex: 1; overflow-y: auto; padding: 14px 6%; display: flex; flex-direction: column; gap: 4px; }
.day-sep { align-self: center; background: #fff; color: var(--muted); font-size: 12px; padding: 4px 10px; border-radius: 8px; margin: 8px 0; box-shadow: 0 1px 1px rgba(0,0,0,.06); }
.msg { max-width: min(560px, 80%); padding: 6px 8px 4px; border-radius: 8px; box-shadow: 0 1px 1px rgba(0,0,0,.08); position: relative; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.in { background: var(--bubble-in); align-self: flex-start; }
.msg.out { background: var(--bubble-out); align-self: flex-end; }
.msg .text { white-space: pre-wrap; }
.msg .meta { display: flex; justify-content: flex-end; align-items: center; gap: 4px; font-size: 11px; color: var(--muted); margin-top: 2px; }
.msg .tag { font-size: 11px; color: var(--green-dark); font-weight: 600; margin-bottom: 2px; }
.msg .err { font-size: 12px; color: var(--danger); margin-top: 4px; }
.msg .quote { border-left: 3px solid var(--green); background: rgba(0,0,0,.04); padding: 4px 8px; border-radius: 4px; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.msg img.media, .msg video.media { max-width: 100%; max-height: 320px; border-radius: 6px; display: block; cursor: pointer; }
.msg audio { max-width: 260px; }
.doc { display: flex; align-items: center; gap: 10px; background: rgba(0,0,0,.05); padding: 10px; border-radius: 6px; text-decoration: none; color: var(--text); margin-bottom: 4px; }
.doc .ico { font-size: 26px; }
.msg .reply-btn { position: absolute; top: 2px; right: 4px; opacity: 0; border: none; background: transparent; cursor: pointer; color: var(--muted); }
.msg:hover .reply-btn { opacity: 1; }
.ticks { font-size: 12px; letter-spacing: -3px; }
.ticks.read { color: #53bdeb; }
.ticks.failed { color: var(--danger); letter-spacing: 0; }
.load-more { align-self: center; }

.reply-bar { display: flex; justify-content: space-between; align-items: center; padding: 6px 14px; background: #f0f2f5; border-top: 1px solid var(--border); font-size: 13px; }
.composer { display: flex; align-items: flex-end; gap: 8px; padding: 10px 14px; background: #f0f2f5; border-top: 1px solid var(--border); }
.composer textarea { flex: 1; max-height: 140px; border-radius: 10px; }
.composer.closed { align-items: center; justify-content: space-between; background: var(--warn-bg); }

/* Modal */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 50; }
.modal-card { background: #fff; border-radius: 12px; width: 100%; max-width: 560px; max-height: 92vh; overflow-y: auto; padding: 18px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.modal-head h3 { margin: 0; }
.modal .row { display: flex; gap: 10px; }
.modal .row > * { flex: 1; }
.help { font-size: 12px; color: var(--muted); margin: -6px 0 10px; }
.tpl-preview { background: var(--chat-bg); padding: 12px; border-radius: 8px; margin: 10px 0; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #111b21; color: #fff; padding: 10px 16px; border-radius: 8px; z-index: 60; max-width: 90vw; }
.toast.bad { background: var(--danger); }

/* Movil */
@media (max-width: 800px) {
  .app { flex-direction: column; }
  .nav { width: 100%; flex-direction: row; padding: 6px; border-right: none; border-bottom: 1px solid var(--border); overflow-x: auto; align-items: center; }
  .nav .brand { padding: 0 6px; }
  .nav a span, .hide-sm { display: none; }
  .nav-foot { margin: 0 0 0 auto; flex-direction: row; padding: 0; align-items: center; }
  .nav-foot #whoami { display: none; }
  .view { min-height: 0; }
  .chat-list-pane { width: 100%; border-right: none; }
  .chats.show-conv .chat-list-pane { display: none; }
  .chats:not(.show-conv) .conv-pane { display: none; }
  .back-btn { display: inline-flex; }
  .messages { padding: 10px 12px; }
  .msg { max-width: 88%; }
  .page { padding: 16px; }
  .kv { grid-template-columns: 1fr; gap: 2px; }
  .kv > div:nth-child(even) { margin-bottom: 8px; }
  .conv-head .badge { display: none; }
}
