:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1f2933;
  --muted: #6b7785;
  --border: #dde1e6;
  --accent: #0f766e;
  --accent-text: #ffffff;
  --in: #ffffff;
  --bot: #e0f2fe;
  --human: #dcfce7;
  --warn-bg: #fff7e6;
  --warn: #9a5b00;
  --danger: #b42318;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161b;
    --surface: #1b2128;
    --text: #e6e9ed;
    --muted: #97a3b0;
    --border: #2c343d;
    --accent: #2bb3a3;
    --accent-text: #0b1214;
    --in: #232b33;
    --bot: #16324a;
    --human: #173a26;
    --warn-bg: #3a2c10;
    --warn: #f3c46b;
    --danger: #ff8a7a;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; }
.mono { font-family: ui-monospace, Consolas, monospace; font-size: 13px; }

button {
  font: inherit;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
}
button[type="submit"], button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.link { border: none; background: none; color: var(--accent); padding: 4px 6px; }
button.danger { color: var(--danger); }
input, select, textarea {
  font: inherit;
  width: 100%;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}
label { display: block; margin: 10px 0; font-weight: 500; }
label > input, label > select, label > textarea { margin-top: 4px; font-weight: 400; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 20px; }
.card.wide { max-width: 980px; margin: 16px auto; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 0 16px; }
.row { display: flex; gap: 12px; align-items: center; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login .card { width: min(360px, 100%); }
.login h1 { margin-top: 0; font-size: 20px; }

/* Barra superior */
.topbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px;
  padding: 8px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { color: var(--accent); }
.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tabs button { border: none; background: none; padding: 6px 10px; }
.tabs button.active { background: var(--bg); font-weight: 600; }
.status { margin-left: auto; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.pill { font-size: 12px; padding: 2px 8px; border-radius: 99px; border: 1px solid var(--border); }
.pill.ok { color: var(--accent); }
.pill.bad { color: var(--warn); background: var(--warn-bg); }
.badge { background: var(--danger); color: #fff; border-radius: 99px; font-size: 11px; padding: 1px 6px; margin-left: 2px; }
.switch { display: flex; align-items: center; gap: 4px; margin: 0; font-weight: 600; }
.switch input { width: auto; margin: 0; }

/* Chats */
#app { height: 100%; display: flex; flex-direction: column; }
.tab { flex: 1; min-height: 0; overflow: auto; padding: 0 16px; }
.chats { display: grid; grid-template-columns: 320px 1fr; padding: 0; overflow: hidden; }
.list { border-right: 1px solid var(--border); background: var(--surface); display: flex; flex-direction: column; min-height: 0; }
.list-tools { display: flex; gap: 8px; padding: 10px; border-bottom: 1px solid var(--border); }
#contact-list { list-style: none; margin: 0; padding: 0; overflow: auto; flex: 1; }
#contact-list li { padding: 10px 12px; border-bottom: 1px solid var(--border); cursor: pointer; }
#contact-list li.active { background: var(--bg); }
#contact-list .top { display: flex; justify-content: space-between; gap: 8px; }
#contact-list .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#contact-list .preview { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tag { font-size: 11px; padding: 1px 6px; border-radius: 6px; border: 1px solid var(--border); white-space: nowrap; }
.tag.human { color: var(--warn); background: var(--warn-bg); border-color: transparent; }
.tag.sim { color: var(--muted); }

.conv { display: flex; flex-direction: column; min-height: 0; }
.empty { margin: auto; color: var(--muted); }
.conv-inner { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.conv-head { display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.conv-head .who { flex: 1; min-width: 160px; display: flex; flex-direction: column; }
.conv-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.back { display: none; }
.handoff { background: var(--warn-bg); color: var(--warn); padding: 8px 16px; font-size: 14px; }
.messages { flex: 1; overflow: auto; padding: 16px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: min(560px, 85%); padding: 7px 10px; border-radius: 10px; border: 1px solid var(--border); white-space: pre-wrap; word-wrap: break-word; }
.msg.in { align-self: flex-start; background: var(--in); }
.msg.bot { align-self: flex-end; background: var(--bot); }
.msg.human { align-self: flex-end; background: var(--human); }
.msg .meta { display: block; font-size: 11px; color: var(--muted); margin-top: 2px; text-align: right; }
.msg .meta .err { color: var(--danger); }
.composer { padding: 10px 16px; background: var(--surface); border-top: 1px solid var(--border); }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { resize: vertical; }
.sim-mode { display: flex; gap: 16px; font-size: 13px; }
.sim-mode label { margin: 0 0 6px; font-weight: 400; display: flex; gap: 4px; align-items: center; }
.sim-mode input { width: auto; }

/* Uso y actividad */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; margin: 12px 0; }
.stat { border: 1px solid var(--border); border-radius: 10px; padding: 12px; }
.stat .v { font-size: 24px; font-weight: 700; }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); }
.events { list-style: none; padding: 0; margin: 0; font-size: 14px; }
.events li { padding: 6px 0; border-bottom: 1px solid var(--border); }
.events .warn { color: var(--warn); }
.events .error { color: var(--danger); }

/* Celular */
@media (max-width: 760px) {
  .chats { grid-template-columns: 1fr; }
  .chats.show-conv .list { display: none; }
  .chats:not(.show-conv) .conv { display: none; }
  .back { display: inline-block; }
  .status { margin-left: 0; }
  .tab { padding: 0 8px; }
}

/* Conexión con Google (pestaña Configuración) */
.google-box { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; margin: 8px 0 12px; background: var(--bg); }
.google-box p { margin: 6px 0 0; }

/* ---------- líneas ---------- */
.line-select { width: auto; max-width: 220px; padding: 4px 8px; }
.inline-select { width: auto; display: inline-block; font-size: 0.8em; padding: 2px 6px; vertical-align: middle; }
.lines-table { width: 100%; border-collapse: collapse; margin: 8px 0; }
.lines-table th, .lines-table td { text-align: left; padding: 4px 6px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.lines-table td input[type="text"] { min-width: 0; }
.lines-table td.center { text-align: center; }
.lines-table input[type="checkbox"] { width: auto; }
.line-add { display: grid; grid-template-columns: 2fr 1.2fr 1.6fr auto; gap: 8px; align-items: center; }
.tag.line { background: var(--bg); border: 1px solid var(--border); color: var(--muted); }
@media (max-width: 760px) {
  .line-add { grid-template-columns: 1fr; }
  .lines-table th:nth-child(2), .lines-table td:nth-child(2) { display: none; }
}

/* ---------- ingreso y usuarios ---------- */
.login-card { display: flex; flex-direction: column; gap: 12px; min-width: 300px; }
a.button { display: inline-block; text-align: center; text-decoration: none; border-radius: 8px; padding: 10px 14px; border: 1px solid var(--accent); }
a.button.primary { background: var(--accent); color: var(--accent-text); }
#emergency summary { cursor: pointer; color: var(--muted); font-size: 0.9em; }
#emergency form { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.me { font-size: 0.85em; }
.users-table .line-checks { display: flex; flex-wrap: wrap; gap: 4px 10px; }
.users-table .line-checks label { display: inline-flex; gap: 4px; align-items: center; font-size: 0.9em; white-space: nowrap; }
.users-table input[type="checkbox"] { width: auto; }
.users-table td small { display: block; }
body.agent .admin-only { display: none !important; }
