:root {
  --bg: #0f1115; --panel: #171a21; --panel-2: #1e222b; --line: #2a2f3a;
  --text: #e6e9ef; --muted: #8b93a3; --accent: #25d366; --accent-2: #3b82f6;
  --danger: #ef4444; --warn: #f59e0b;
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
header {
  display: flex; align-items: center; gap: 20px; padding: 14px 22px; flex-wrap: wrap;
  background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 10;
}
h1 { font-size: 16px; margin: 0; font-weight: 600; white-space: nowrap; }
nav { display: flex; gap: 6px; flex-wrap: wrap; }
.tab {
  background: transparent; border: 1px solid transparent; color: var(--muted);
  padding: 6px 14px; border-radius: 8px; cursor: pointer; font-size: 13px;
}
.tab:hover { color: var(--text); }
.tab.active { background: var(--panel-2); color: var(--text); border-color: var(--line); }
.health { margin-left: auto; font-size: 12px; color: var(--muted); white-space: nowrap; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; }
.dot.on { background: var(--accent); } .dot.off { background: var(--danger); }

.panel { display: none; padding: 22px; }
.panel.active { display: block; }

.toolbar { display: flex; gap: 10px; margin-bottom: 18px; align-items: center; }
input, textarea, select {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--text);
  padding: 9px 12px; border-radius: 8px; font: inherit; outline: none;
}
input:focus, textarea:focus { border-color: var(--accent-2); }
#new-instance { width: 280px; }
button {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--text);
  padding: 9px 14px; border-radius: 8px; cursor: pointer; font: inherit;
}
button:hover { border-color: var(--muted); }
button.primary { background: var(--accent); border-color: var(--accent); color: #06210f; font-weight: 600; }
button.danger { color: var(--danger); }
button.small { padding: 5px 10px; font-size: 12px; }

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px; }
.card h3 { margin: 0 0 4px; font-size: 15px; }
.card .sub { color: var(--muted); font-size: 12px; margin-bottom: 12px; }
.card .row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.badge { font-size: 11px; padding: 3px 9px; border-radius: 999px; border: 1px solid var(--line); }
.badge.open { color: var(--accent); border-color: #1c4a30; background: #10291b; }
.badge.closed { color: var(--danger); border-color: #4a1c1c; background: #291010; }
.badge.wait { color: var(--warn); border-color: #4a3a1c; background: #292010; }
.badge.manual { color: var(--warn); border-color: #4a3a1c; background: #292010; }
.chat-item:has(.badge.manual) { border-left: 3px solid var(--warn); }

.chat-layout { display: grid; grid-template-columns: 300px 1fr; gap: 16px; height: calc(100vh - 120px); }
.chat-side { display: flex; flex-direction: column; gap: 10px; min-height: 0; }
.chat-list { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; overflow-y: auto; flex: 1; }

/* filtro por número */
.inst-filter { display: flex; gap: 6px; flex-wrap: wrap; }
.inst-filter:empty { display: none; }
.inst-chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--panel); border: 1px solid var(--line); color: var(--muted);
  padding: 6px 11px; border-radius: 999px; font-size: 12px; cursor: pointer;
}
.inst-chip:hover { color: var(--text); }
.inst-chip.active { background: var(--panel-2); color: var(--text); border-color: var(--accent); }
.chip-count {
  background: var(--danger); color: #fff; border-radius: 999px;
  padding: 0 6px; font-size: 10px; font-weight: 700; line-height: 16px; min-width: 16px; text-align: center;
}

/* avisos de no leído */
.dot-unread {
  display: inline-block; width: 8px; height: 8px; border-radius: 50%;
  background: var(--danger); margin-right: 7px; vertical-align: middle;
}
.chat-item.unread .name { color: var(--text); }
.chat-item.unread .prev { color: var(--text); }
.tab-dot {
  display: inline-block; width: 7px; height: 7px; border-radius: 50%;
  background: var(--danger); margin-left: 6px; vertical-align: super;
}

.chat-item { padding: 12px 14px; border-bottom: 1px solid var(--line); cursor: pointer; }
.chat-item:hover { background: var(--panel-2); }
.chat-item.active { background: var(--panel-2); border-left: 3px solid var(--accent); }
.chat-item .name { font-weight: 600; font-size: 13px; display: flex; justify-content: space-between; }
.chat-item .prev { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pill { background: var(--accent); color: #06210f; border-radius: 999px; padding: 0 6px; font-size: 10px; font-weight: 700; }

.chat-view { display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.chat-header { padding: 12px 16px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 12px; }
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: 68%; padding: 8px 12px; border-radius: 12px; font-size: 13px; word-wrap: break-word; }
.msg.in { background: var(--panel-2); align-self: flex-start; border-bottom-left-radius: 3px; }
.msg.out { background: #124d2b; align-self: flex-end; border-bottom-right-radius: 3px; }
.msg.out.humano { background: #1e3a8a; }
.msg .meta { font-size: 10px; color: var(--muted); margin-top: 3px; }
.composer { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--line); }
.composer input { flex: 1; }

table { width: 100%; border-collapse: collapse; background: var(--panel); border-radius: 12px; overflow: hidden; }
th, td { text-align: left; padding: 11px 14px; border-bottom: 1px solid var(--line); font-size: 13px; }
th { color: var(--muted); font-weight: 500; font-size: 12px; }
tr.cancelado td { opacity: .45; text-decoration: line-through; }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,.7); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal[hidden], [hidden] { display: none !important; }
.modal-box { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 24px; max-width: 560px; width: 92%; max-height: 88vh; overflow-y: auto; position: relative; }
.close { position: absolute; top: 10px; right: 12px; background: none; border: none; font-size: 22px; color: var(--muted); padding: 0 6px; }
.modal-box img { width: 260px; display: block; margin: 16px auto; border-radius: 10px; background: #fff; padding: 8px; }
.modal-box textarea { width: 100%; min-height: 240px; font-family: ui-monospace, Menlo, monospace; font-size: 12px; }
.muted { color: var(--muted); font-size: 12px; }
.empty { color: var(--muted); text-align: center; padding: 40px; font-size: 13px; }

.section-title { font-size: 14px; margin: 28px 0 12px; color: var(--text); }
#turnos .section-title:first-of-type { margin-top: 34px; }
.slots { display: flex; flex-direction: column; gap: 8px; margin-bottom: 18px; }
.slot-row { display: flex; align-items: flex-start; gap: 10px; }
.slot-row strong { min-width: 130px; flex-shrink: 0; font-size: 13px; padding-top: 3px; }
.slot-wrap { display: flex; flex-wrap: wrap; gap: 6px; }
.slot { background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px;
        padding: 3px 9px; font-size: 12px; color: var(--accent); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 14px; }
.form-grid label { display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--muted); }
.form-grid input, .form-grid select { width: 100%; }
.msg.out.sistema { background: #4a3a10; }
.msg.out.no-enviado { background: #4a1010; border: 1px dashed var(--danger); }
.msg.out.no-enviado .meta::after { content: " · NO SE ENVIÓ"; color: var(--danger); }
.msg.in.admin { background: #2a2440; }

.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-box { background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
             padding: 32px; width: 320px; display: flex; flex-direction: column; gap: 12px; }
.login-box h1 { text-align: center; margin-bottom: 8px; }
.login-error { color: var(--danger); font-size: 12px; min-height: 16px; text-align: center; }


/* ---------- CELULAR ---------- */
@media (max-width: 760px) {
  header { gap: 12px; padding: 10px 14px; }
  h1 { font-size: 15px; }
  .tab { padding: 5px 10px; font-size: 12px; }
  .health { font-size: 11px; width: 100%; margin-left: 0; order: 3; }
  .panel { padding: 14px; }

  /* Una sola columna: o la lista, o la conversación */
  .chat-layout { grid-template-columns: 1fr; height: calc(100dvh - 150px); }
  .chat-view { display: none; }
  .msg { max-width: 85%; font-size: 14px; }

  /* Con un chat abierto: se oculta la lista y se ve la conversación entera */
  body.chat-abierto .chat-side { display: none; }
  body.chat-abierto .chat-view { display: flex; }
  body.chat-abierto .chat-layout { height: calc(100dvh - 120px); }

  .chat-header { flex-wrap: wrap; gap: 8px; padding: 10px 12px; }
  .btn-volver { display: inline-flex !important; }

  .cards { grid-template-columns: 1fr; }
  table { font-size: 12px; }
  th, td { padding: 8px 10px; }
  .toolbar { flex-wrap: wrap; }
  .toolbar input, .toolbar select { flex: 1; min-width: 140px; }
  #new-instance { width: 100%; }
}
.btn-volver { display: none; }
