:root {
  --accent: #63ba7d;
  --accent-dark: #4a9a62;
  --accent-soft: #eef8f1;
  --bg: #f5f7fa;
  --panel: #ffffff;
  --line: #e6e9ef;
  --text: #1f2430;
  --muted: #6b7385;
  --in: #ffffff;
  --out: #e3f4e8;
  --chat-bg: #eef3f7;
  --danger: #c0392b;
  --warn-bg: #fff6e0;
  --warn-line: #f1c36b;
  --radius: 10px;
  --side: 215px;
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
body { font-size: 14px; line-height: 1.4; -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.hidden { display: none !important; }

/* ---------- Botones y campos ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); background: #fff; border-radius: 8px;
  padding: 8px 12px; cursor: pointer; text-decoration: none; color: var(--text);
  white-space: nowrap; min-height: 36px; font-weight: 500;
}
.btn:hover { background: #f6f7f9; }
.btn-sm { min-height: 32px; padding: 6px 12px; font-size: 13.5px; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); border-color: var(--accent-dark); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: rgba(0,0,0,.05); }
.btn-icon { padding: 0; width: 38px; height: 38px; min-height: 38px; border-radius: 50%; }
.btn:disabled { opacity: .55; cursor: default; }
input[type=text], input[type=email], input[type=password], input[type=search], input[type=tel], select, textarea {
  width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: #fff;
  outline: none; min-height: 36px;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 15%, transparent); }
label { display: block; font-size: 13px; color: var(--muted); }
label > input, label > select, label > textarea { margin-top: 4px; color: var(--text); }

/* ---------- Login ---------- */
.login-body { display: grid; place-items: center; min-height: 100%; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: #fff; border: 1px solid var(--line); border-radius: 14px;
  padding: 28px 24px; display: grid; gap: 14px; box-shadow: 0 8px 30px rgba(0,0,0,.06);
}
.login-card h1 { font-size: 18px; margin: 0; text-align: center; }
.login-logo { width: 52px; height: 52px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; margin: 0 auto; }
.login-brand { display: block; max-width: 230px; width: 100%; height: auto; margin: 0 auto 4px; }
.login-error { background: #fdecea; color: var(--danger); padding: 8px 10px; border-radius: 8px; margin: 0; }
.login-ok { background: #e7f6ef; color: #146c43; padding: 8px 10px; border-radius: 8px; margin: 0; }

/* ---------- Estructura principal ---------- */
.app { display: grid; grid-template-columns: var(--side) 1fr; height: 100vh; height: 100dvh; }
.rail {
  background: #fff; border-right: 1px solid var(--line); display: flex; flex-direction: column; gap: 2px; padding: 18px 12px 12px;
  min-width: 0;
}
.rail .brand { padding: 4px 8px 22px; }
.rail .brand img { display: block; width: 100%; max-width: 180px; height: auto; }
.rail button, .rail .me {
  border: 0; background: transparent; color: #3b4252; border-radius: 8px; padding: 10px 10px;
  cursor: pointer; display: flex; align-items: center; gap: 12px; font-size: 14.5px; position: relative; text-align: left; width: 100%;
}
.rail svg { width: 21px; height: 21px; flex-shrink: 0; color: #6b7385; }
.rail button:hover { background: #f5f6f8; }
.rail button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.rail button.active svg { color: var(--accent); }
.rail .spacer { flex: 1; }
.rail .me { cursor: default; border-top: 1px solid var(--line); border-radius: 0; padding-top: 14px; font-size: 13px; color: var(--muted); }
.rail .me span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rail .badge { margin-left: auto; flex-shrink: 0; }
.only-mobile { display: none !important; }
.badge {
  background: var(--accent); color: #fff; border-radius: 999px; font-size: 11px; font-weight: 700;
  min-width: 20px; height: 20px; line-height: 20px; padding: 0 6px; text-align: center; display: inline-block;
}
.main { min-width: 0; min-height: 0; display: grid; grid-template-rows: auto 1fr; }
.topbar {
  height: 56px; background: #fff; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 12px; padding: 0 16px;
}
.topbar .spacer { flex: 1; }
.chip-ws {
  display: inline-flex; align-items: center; gap: 6px; background: var(--accent-soft); color: var(--accent); border: 1px solid color-mix(in srgb, var(--accent) 25%, transparent);
  padding: 4px 10px; border-radius: 999px; font-size: 13px; font-weight: 600;
}
.who-top { color: var(--muted); font-size: 13px; }
.view { height: 100%; min-height: 0; }

/* ---------- Vista conversaciones ---------- */
.inbox { display: grid; grid-template-columns: 335px 1fr 320px; height: 100%; min-height: 0; }
.list-pane { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.list-head { padding: 12px 12px 8px; display: grid; gap: 10px; }
.list-title { display: flex; align-items: center; justify-content: flex-end; gap: 8px; }
.brand-mobile { display: none; height: 18px; width: auto; margin-right: auto; }
.search { position: relative; }
.search svg { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: #9aa1b0; }
.search input { background: #f1f3f6; border-color: transparent; padding-left: 36px; border-radius: 10px; }
.search input:focus { background: #fff; }
.filters { display: grid; grid-template-columns: 1fr 96px; gap: 8px; }
.filters select { font-size: 13.5px; padding: 7px 8px; min-height: 38px; }
.tabs { display: flex; gap: 0; border-bottom: 1px solid var(--line); padding: 0 6px; overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab {
  border: 0; background: none; padding: 10px 9px 9px; cursor: pointer; font-size: 14px; color: var(--muted);
  white-space: nowrap; display: inline-flex; gap: 6px; align-items: center; border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.tab .badge { min-width: 18px; height: 18px; line-height: 18px; font-size: 10.5px; }
.conv-list { overflow-y: auto; flex: 1; min-height: 0; }
.conv {
  display: grid; grid-template-columns: 44px 1fr auto; gap: 12px; padding: 12px 12px; cursor: pointer; align-items: center;
  width: 100%; text-align: left; background: #fff; border: 0; border-bottom: 1px solid #eef0f4; border-left: 3px solid transparent;
  font: inherit; color: inherit;
}
.conv:hover { background: #f8f9fb; }
.conv.active { background: var(--accent-soft); border-left-color: var(--accent); }
.avatar {
  width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; font-weight: 600; font-size: 14.5px;
  color: #fff; flex-shrink: 0; letter-spacing: .3px;
}
.avatar.xl { width: 72px; height: 72px; font-size: 24px; }
.avatar.placeholder { background: #6b7a90; }
.conv-main { min-width: 0; display: grid; gap: 4px; }
.conv-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14.5px; }
.conv-preview { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 13.5px; }
.conv.unread .conv-preview { color: var(--text); }
.conv-meta { display: grid; justify-items: end; align-self: stretch; align-content: space-between; gap: 6px; font-size: 11px; }
.conv-meta .time { color: var(--accent); font-weight: 600; white-space: nowrap; }
.conv:not(.unread) .conv-meta .time { color: #8a91a1; font-weight: 500; }
.chip { display: inline-block; font-size: 11px; padding: 1px 7px; border-radius: 999px; background: #eef0f3; color: #475467; white-space: nowrap; max-width: 110px; overflow: hidden; text-overflow: ellipsis; }
.chip.ad { background: #e8f0fe; color: #1a56db; }
.chip.closed { background: #f2f4f7; color: #98a2b3; }
.empty { padding: 40px 20px; text-align: center; color: var(--muted); }

/* ---------- Chat ---------- */
.chat-pane { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: var(--chat-bg); }
.chat-empty { flex: 1; display: grid; place-items: center; text-align: center; color: #5b6475; padding: 20px; }
.chat-empty svg { color: var(--accent); opacity: .7; margin-bottom: 18px; }
.chat-empty p { margin: 0 0 12px; font-size: 15px; }
.chat-head {
  background: var(--panel); border-bottom: 1px solid var(--line); padding: 10px 16px; display: flex; align-items: center; gap: 10px;
  flex-wrap: wrap; min-height: 64px;
}
.chat-head .who { min-width: 0; flex: 1; display: flex; align-items: center; gap: 10px; }
.chat-head .who > div { min-width: 0; }
.chat-head .avatar { width: 40px; height: 40px; font-size: 14px; }
.chat-head .name { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; cursor: pointer; }
.chat-head .sub { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head .actions { display: flex; gap: 6px; align-items: center; }
.chat-head .actions select { width: auto; max-width: 190px; font-size: 13px; padding: 6px 8px; min-height: 34px; }
.back-btn { display: none; }
.messages { flex: 1; overflow-y: auto; padding: 16px 5%; display: flex; flex-direction: column; gap: 4px; min-height: 0; }
.day-sep { align-self: center; background: #fff; color: #5b6475; font-size: 12px; padding: 4px 12px; border-radius: 999px; margin: 10px 0 6px; box-shadow: 0 1px 1px rgba(0,0,0,.06); }
.msg {
  max-width: min(72%, 560px); padding: 7px 10px 5px; border-radius: 10px; box-shadow: 0 1px 1px rgba(0,0,0,.08);
  word-wrap: break-word; overflow-wrap: anywhere; white-space: pre-wrap; position: relative;
}
.msg.in { align-self: flex-start; background: var(--in); border-top-left-radius: 3px; }
.msg.out { align-self: flex-end; background: var(--out); border-top-right-radius: 3px; }
.msg.failed { background: #fdecea; }
.msg .meta { font-size: 11px; color: #7b8496; text-align: right; margin-top: 2px; white-space: nowrap; display: flex; gap: 4px; justify-content: flex-end; align-items: center; }
.msg .by { font-size: 11.5px; font-weight: 700; color: var(--accent); margin-bottom: 2px; white-space: normal; }
.msg .err { color: var(--danger); font-size: 12px; margin-top: 4px; white-space: normal; }
.msg img.media { display: block; max-width: 100%; max-height: 320px; border-radius: 8px; margin-bottom: 4px; cursor: zoom-in; background: #ddd; min-height: 60px; }
.msg img.sticker { max-width: 140px; background: transparent; }
.msg video { max-width: 100%; max-height: 320px; border-radius: 8px; display: block; margin-bottom: 4px; }
.msg audio { width: 260px; max-width: 100%; display: block; margin-bottom: 2px; }
.msg .doc { display: flex; align-items: center; gap: 8px; padding: 8px; background: rgba(0,0,0,.04); border-radius: 6px; margin-bottom: 4px; text-decoration: none; color: var(--text); white-space: normal; }
.msg.tpl { border: 1px dashed color-mix(in srgb, var(--accent) 45%, transparent); }
.msg .tpl-tag { font-size: 11px; color: var(--muted); margin-bottom: 2px; }
.ticks { font-size: 12px; letter-spacing: -3px; padding-right: 3px; }
.ticks.read { color: #34b7f1; }
.ticks.failed { color: var(--danger); letter-spacing: 0; }

.composer { background: #fff; padding: 10px 14px; display: flex; gap: 8px; align-items: flex-end; border-top: 1px solid var(--line); }
.composer textarea { resize: none; max-height: 140px; min-height: 42px; border-radius: 21px; padding: 10px 16px; line-height: 1.35; background: #f1f3f6; border-color: transparent; }
.composer textarea:focus { background: #fff; }
.composer .btn-icon { flex-shrink: 0; width: 42px; height: 42px; }
.window-closed {
  background: var(--warn-bg); border-top: 1px solid var(--warn-line); padding: 12px 14px; display: flex; gap: 12px;
  align-items: center; justify-content: space-between; flex-wrap: wrap;
}
.window-closed p { margin: 0; font-size: 13px; flex: 1; min-width: 200px; }
.window-info { font-size: 11.5px; color: var(--muted); padding: 0 14px 8px; background: #fff; }

/* ---------- Ficha del contacto (derecha) ---------- */
.info-pane { background: #fff; border-left: 1px solid var(--line); min-height: 0; overflow-y: auto; }
.info-top { display: grid; place-items: center; padding: 24px 16px 20px; border-bottom: 1px solid var(--line); }
.info-body { padding: 16px 18px; display: grid; gap: 14px; }
.info-body h3 { margin: 0; font-size: 16px; text-align: center; }
.info-body .phone { text-align: center; color: var(--muted); margin-top: -8px; }
.info-row { display: grid; gap: 2px; }
.info-row .k { font-size: 11.5px; text-transform: uppercase; letter-spacing: .4px; color: #8a91a1; font-weight: 600; }
.info-row .v { font-size: 14px; }
.info-actions { display: grid; gap: 8px; margin-top: 4px; }
.pill { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; padding: 3px 10px; border-radius: 999px; background: #e7f6ef; color: #146c43; width: fit-content; }
.pill.off { background: var(--warn-bg); color: #8a5a00; }

/* ---------- Modales ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(15,23,42,.45); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal {
  background: #fff; border-radius: 14px; width: 100%; max-width: 520px; max-height: calc(100dvh - 32px); overflow: auto;
  padding: 20px; display: grid; gap: 14px; box-shadow: 0 20px 60px rgba(0,0,0,.2);
}
.modal h3 { margin: 0; font-size: 17px; }
.modal .row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.modal .foot { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.tpl-preview { background: var(--out); border-radius: 8px; padding: 10px; white-space: pre-wrap; font-size: 13.5px; }
.check { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: 14px; }
.check input { width: 18px; height: 18px; margin: 0; accent-color: var(--accent); }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: grid; place-items: center; z-index: 60; cursor: zoom-out; padding: 16px; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 6px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #1f2430; color: #fff; padding: 10px 16px;
  border-radius: 10px; z-index: 70; max-width: calc(100vw - 32px); box-shadow: 0 8px 24px rgba(0,0,0,.2); font-size: 13.5px;
}
.toast.error { background: var(--danger); }

/* ---------- Páginas (dashboard / usuarios) ---------- */
.page { height: 100%; overflow-y: auto; padding: 22px clamp(14px, 3vw, 32px) 40px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head h2 { margin: 0; font-size: 20px; }
.page-head select { width: auto; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 18px; }
.card { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.card .k { font-size: 12.5px; color: var(--muted); }
.card .v { font-size: 26px; font-weight: 700; margin-top: 2px; }
.card .h { font-size: 12px; color: var(--muted); margin-top: 2px; }
.panel { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 18px; overflow-x: auto; }
.panel h3 { margin: 0 0 12px; font-size: 15px; }
.bars { display: flex; align-items: flex-end; gap: 3px; height: 160px; min-width: 100%; }
.bars .b { flex: 1; min-width: 6px; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; height: 100%; }
.bars .b span { width: 100%; background: var(--accent); border-radius: 3px 3px 0 0; min-height: 1px; opacity: .85; }
.bars .b:hover span { opacity: 1; }
.bars-labels { display: flex; gap: 3px; margin-top: 4px; font-size: 10.5px; color: var(--muted); }
.bars-labels span { flex: 1; min-width: 6px; text-align: center; overflow: visible; white-space: nowrap; display: flex; justify-content: center; }
table.grid { border-collapse: collapse; width: 100%; font-size: 13.5px; }
table.grid th, table.grid td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; }
table.grid th { font-size: 12px; color: var(--muted); font-weight: 600; }
table.heat { border-collapse: separate; border-spacing: 2px; font-size: 11px; }
table.heat td { width: 26px; height: 24px; text-align: center; border-radius: 4px; color: #0f3d1f; }
table.heat th { font-weight: 500; color: var(--muted); padding: 0 4px; text-align: right; }
table.heat thead th { text-align: center; padding: 0; }
.legend { font-size: 12px; color: var(--muted); margin-top: 8px; }

/* ---------- Pantallas medianas: sin ficha derecha ---------- */
@media (max-width: 1250px) {
  .inbox { grid-template-columns: 320px 1fr; }
  .info-pane { display: none; }
}
@media (max-width: 1020px) {
  :root { --side: 64px; }
  .rail { padding: 14px 8px; align-items: center; }
  .rail .brand { display: none; }
  .rail button, .rail .me { justify-content: center; padding: 10px; }
  .rail button > span:not(.badge), .rail .me span { display: none; }
  .rail .badge { position: absolute; top: 2px; right: 2px; margin: 0; }
  .brand-mobile { display: block; }
}

/* ---------- Celular ---------- */
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .rail {
    grid-row: 2; flex-direction: row; justify-content: space-around; align-items: stretch; padding: 4px 6px calc(4px + env(safe-area-inset-bottom));
    gap: 0; border-right: 0; border-top: 1px solid var(--line);
  }
  .rail .spacer, .rail .me { display: none; }
  .only-mobile { display: flex !important; }
  .rail button { flex-direction: column; gap: 2px; font-size: 10.5px; flex: 1; max-width: 100px; padding: 6px 0; }
  .rail button > span:not(.badge) { display: block; }
  .rail button.active { background: transparent; }
  .rail .badge { top: 0; right: calc(50% - 22px); }
  .main { grid-row: 1; grid-template-rows: 1fr; }
  .topbar { display: none; }
  .inbox { grid-template-columns: 1fr; }
  .inbox .chat-pane { display: none; }
  .inbox.show-chat .list-pane { display: none; }
  .inbox.show-chat .chat-pane { display: flex; }
  .app.chat-open .rail { display: none; }
  .back-btn { display: inline-flex; }
  .chat-head { padding: 8px 10px; gap: 6px; }
  .chat-head .actions { width: 100%; }
  .chat-head .actions select { flex: 1; max-width: none; }
  .messages { padding: 12px 10px; }
  .msg { max-width: 86%; }
  .modal .row { grid-template-columns: 1fr; }
  .composer textarea { font-size: 16px; } /* evita zoom automático en iPhone */
  input, select, textarea { font-size: 16px; }
  .filters { grid-template-columns: 1fr 100px; }
  /* Tabla de usuarios como tarjetas */
  table.users thead { display: none; }
  table.users tr { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
  table.users td { border: 0; padding: 0; }
  table.users td:nth-child(1) { font-weight: 600; }
  table.users td:nth-child(2) { grid-column: 1; grid-row: 2; color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }
  table.users td:nth-child(3) { grid-column: 1; grid-row: 3; font-size: 13px; }
  table.users td:nth-child(4) { grid-column: 2; grid-row: 1; text-align: right; }
  table.users td:nth-child(5) { grid-column: 2; grid-row: 2 / span 2; align-self: center; }
  .panel:has(table.users) { padding: 6px 16px; }
}