:root {
  --bg: #f0f2f5;
  --panel: #ffffff;
  --text: #1f2328;
  --muted: #667085;
  --border: #e3e6ea;
  --accent: #128c7e;
  --accent-text: #ffffff;
  --mine: #d9fdd3;
  --theirs: #ffffff;
  --chat-bg: #efeae2;
  --error: #c62828;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b141a;
    --panel: #111b21;
    --text: #e9edef;
    --muted: #8696a0;
    --border: #222d34;
    --accent: #00a884;
    --mine: #005c4b;
    --theirs: #202c33;
    --chat-bg: #0b141a;
    --error: #ef5350;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 14px/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(--error); min-height: 1.2em; margin: 0; }

button {
  font: inherit;
  border: 0;
  border-radius: 8px;
  padding: 8px 14px;
  background: var(--accent);
  color: var(--accent-text);
  cursor: pointer;
}
button.ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }
input {
  font: inherit;
  width: 100%;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
}
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 24px; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(360px, 100%); display: grid; gap: 12px; }
.login-card h1 { margin: 0; color: var(--accent); }
.login-card label { display: grid; gap: 4px; }

/* Barra superior */
.topbar {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.topbar nav { display: flex; gap: 4px; }
.tab { background: transparent; color: var(--muted); }
.tab.active { background: var(--accent); color: var(--accent-text); }
.spacer { flex: 1; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; padding: 16px; }
.stat { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 12px 16px; }
.stat .value { font-size: 22px; font-weight: 600; }
.stat .label { color: var(--muted); font-size: 12px; }

main { padding: 0 16px 16px; }
.toolbar { padding: 8px 0; }

/* Tabla de teléfonos */
.table-wrap { overflow-x: auto; background: var(--panel); border: 1px solid var(--border); border-radius: 12px; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--muted); font-weight: 500; font-size: 12px; }
tr:last-child td { border-bottom: 0; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; background: var(--border); }
.dot.on { background: #25d366; }
td button { padding: 4px 10px; font-size: 12px; }

/* Conversaciones */
.split {
  display: grid; grid-template-columns: 340px 1fr; gap: 12px;
  height: calc(100vh - 200px); min-height: 420px;
}
.conv-list, .conv-detail {
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  display: flex; flex-direction: column; overflow: hidden;
}
.conv-list .toolbar { padding: 8px; }
.chip { margin: 0 8px 8px; padding: 4px 10px; border-radius: 999px; background: var(--bg); cursor: pointer; font-size: 12px; }
#conv-items { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
#conv-items li { padding: 10px 12px; border-top: 1px solid var(--border); cursor: pointer; }
#conv-items li.active, #conv-items li:hover { background: var(--bg); }
#conv-items .who { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#conv-items .preview { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-header { padding: 12px 16px; border-bottom: 1px solid var(--border); }
.messages { flex: 1; overflow-y: auto; padding: 16px; background: var(--chat-bg); display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: min(70%, 520px); padding: 6px 10px 4px; border-radius: 8px; background: var(--theirs); align-self: flex-start; box-shadow: 0 1px 1px rgb(0 0 0 / 8%); }
.msg.mine { background: var(--mine); align-self: flex-end; }
.msg .from { font-size: 11px; font-weight: 600; color: var(--accent); }
.msg .text { white-space: pre-wrap; word-break: break-word; }
.msg .meta { font-size: 11px; color: var(--muted); text-align: right; }
.msg img, .msg video { display: block; max-width: 100%; max-height: 320px; border-radius: 6px; margin: 4px 0; }
.msg audio { width: 260px; max-width: 100%; margin: 4px 0; }
.load-more { align-self: center; }

@media (max-width: 760px) {
  .split { grid-template-columns: 1fr; height: auto; }
  .conv-list { max-height: 40vh; }
  .conv-detail { height: 70vh; }
}

/* Administradores */
.admins-grid { display: grid; grid-template-columns: 1fr 340px; gap: 16px; align-items: start; padding-top: 8px; }
.form-card { display: grid; gap: 10px; }
.form-card h3 { margin: 0 0 4px; }
.form-card label { display: grid; gap: 4px; }
.actions { display: flex; justify-content: flex-end; gap: 8px; }
.danger { background: var(--error); }
td .row-actions { display: flex; gap: 6px; justify-content: flex-end; }
dialog { border: 1px solid var(--border); border-radius: 12px; padding: 20px; background: var(--panel); color: var(--text); width: min(380px, calc(100vw - 32px)); }
dialog::backdrop { background: rgb(0 0 0 / 45%); }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--accent); color: var(--accent-text); padding: 10px 18px; border-radius: 999px; box-shadow: 0 4px 16px rgb(0 0 0 / 25%); }
.avatar { width: 32px; height: 32px; border-radius: 50%; object-fit: cover; display: block; background: var(--border); }
.avatar.ph { display: grid; place-items: center; font-weight: 600; color: var(--muted); font-size: 13px; }
@media (max-width: 760px) { .admins-grid { grid-template-columns: 1fr; } }

/* Borrar mensajes */
.msg .from { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.msg-del { background: transparent; color: var(--muted); padding: 0 4px; font-size: 13px; opacity: 0; transition: opacity .15s; }
.msg:hover .msg-del, .msg-del:focus-visible { opacity: 1; }
.msg-del:hover { color: var(--error); }
@media (hover: none) { .msg-del { opacity: 1; } }
