:root {
  --stage: #0e3b3b;
  --stage-2: #134a49;
  --stage-ink: #cfe3df;
  --paper: #f6f7f4;
  --surface: #ffffff;
  --line: #dde3df;
  --line-2: #eaeeeb;
  --ink: #13201f;
  --muted: #5e6e6b;
  --brand: #127369;
  --brand-soft: #e3f1ee;
  --highlight: #fbe38a;
  --bot: #127369;
  --wait: #c27c0e;
  --wait-soft: #fbf0dc;
  --human: #4251c9;
  --human-soft: #e7e9fa;
  --urgent: #c23b2c;
  --urgent-soft: #f9e4e1;
  --wall: #e9e2d6;
  --bubble-in: #ffffff;
  --bubble-out: #d5f2c8;
  --chat-head: #0f5d55;
  --read: #3b8ed8;
  --font: "Onest", "Segoe UI", system-ui, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  color: var(--ink);
  background: var(--stage);
  display: grid;
  grid-template-rows: auto 1fr;
  -webkit-font-smoothing: antialiased;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--highlight); outline-offset: 2px; }
code { font-family: var(--mono); font-size: 0.85em; }

/* ---------- barra superior ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px 24px; background: var(--stage); color: #fff;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand strong { display: block; font-size: 16px; letter-spacing: -0.01em; }
.brand-sub { font-size: 13px; color: var(--stage-ink); }
.brand-mark {
  width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center;
  background: #fff; color: var(--brand); font-weight: 700; font-size: 18px;
}
.top-actions { display: flex; gap: 10px; align-items: center; }
.ai-toggle {
  display: inline-flex; align-items: center; gap: 8px; padding: 8px 14px; border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.22); background: transparent; color: #fff; font-size: 14px;
}
.ai-dot { width: 8px; height: 8px; border-radius: 50%; background: rgba(255, 255, 255, 0.35); }
.ai-toggle[aria-pressed="true"] { background: var(--highlight); color: var(--ink); border-color: var(--highlight); }
.ai-toggle[aria-pressed="true"] .ai-dot { background: var(--ink); }
.btn-ghost {
  padding: 8px 14px; border-radius: 999px; border: 1px solid rgba(255, 255, 255, 0.22);
  background: transparent; color: #fff; font-size: 14px;
}
.btn-primary {
  padding: 11px 18px; border-radius: 10px; border: 0; background: var(--brand); color: #fff;
  font-weight: 600; font-size: 15px;
}
.btn-primary:hover { background: #0e6058; }
.btn-primary:disabled { opacity: 0.55; cursor: progress; }

.mobile-tabs { display: none; }

/* ---------- distribución ---------- */
.layout { display: grid; grid-template-columns: 440px minmax(0, 1fr); min-height: 0; }

/* ---------- escenario y celular ---------- */
.stage {
  background: radial-gradient(120% 70% at 30% 20%, var(--stage-2), var(--stage) 70%);
  display: flex; flex-direction: column; align-items: center; gap: 22px;
  padding: 22px 28px; overflow-y: auto; min-height: 0;
}
.phone {
  width: 360px; flex: 0 0 auto; height: min(700px, calc(100vh - 260px)); min-height: 520px;
  border-radius: 46px; padding: 12px; background: #0a1414;
  box-shadow: 0 30px 60px -20px rgba(0, 0, 0, 0.6), inset 0 0 0 2px #273534;
}
.phone-screen {
  position: relative; height: 100%; border-radius: 36px; overflow: hidden;
  display: grid; grid-template-rows: auto 1fr auto; background: var(--wall);
}
.chat-header {
  display: flex; align-items: center; gap: 10px; padding: 34px 14px 10px;
  background: var(--chat-head); color: #fff;
}
.chat-avatar {
  width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center;
  background: #fff; color: var(--brand); font-weight: 700;
}
.chat-title strong { display: block; font-size: 15px; }
.chat-title span { font-size: 12px; opacity: 0.8; }
.verified {
  display: inline-grid; place-items: center; width: 15px; height: 15px; border-radius: 50%;
  background: #25b35a; color: #fff; font-size: 9px; vertical-align: 1px; margin-left: 2px;
}
.chat-body {
  overflow-y: auto; padding: 12px 10px 16px; display: flex; flex-direction: column; gap: 6px;
  background-color: var(--wall);
  background-image: radial-gradient(rgba(19, 32, 31, 0.05) 1px, transparent 1px);
  background-size: 14px 14px;
}
.chat-empty {
  margin: auto 12px; text-align: center; font-size: 13px; color: var(--muted);
  background: rgba(255, 255, 255, 0.7); padding: 10px 12px; border-radius: 10px;
}
.day-note {
  align-self: center; font-size: 12px; color: var(--muted); background: rgba(255, 255, 255, 0.8);
  padding: 3px 10px; border-radius: 8px; margin: 4px 0;
}
.msg { max-width: 84%; display: flex; flex-direction: column; gap: 4px; }
.msg.in { align-self: flex-start; }
.msg.out { align-self: flex-end; }
.bubble {
  padding: 7px 10px 6px; border-radius: 10px; font-size: 14.5px; line-height: 1.38;
  box-shadow: 0 1px 0.5px rgba(0, 0, 0, 0.12); overflow-wrap: anywhere;
}
.msg.in .bubble { background: var(--bubble-in); border-top-left-radius: 3px; }
.msg.out .bubble { background: var(--bubble-out); border-top-right-radius: 3px; }
.bubble .meta { display: block; text-align: right; font-size: 11px; color: var(--muted); margin-top: 2px; }
.bubble .head { font-weight: 600; display: block; margin-bottom: 2px; }
.bubble .foot { display: block; font-size: 12.5px; color: var(--muted); margin-top: 3px; }
.bubble .tpl-tag { display: block; font-size: 11.5px; color: var(--brand); margin-bottom: 3px; }
.reply-btns { display: flex; flex-direction: column; gap: 3px; }
.reply-btn {
  border: 0; background: var(--bubble-in); color: #0b7fbf; padding: 9px 10px; border-radius: 9px;
  font-size: 14px; font-weight: 500; box-shadow: 0 1px 0.5px rgba(0, 0, 0, 0.12);
}
.reply-btn:hover { background: #f3f8fb; }
.reply-btn:disabled { color: var(--muted); cursor: default; background: #f6f6f6; }
.ai-tag { font-size: 11px; color: var(--brand); font-weight: 600; margin-right: 4px; }

.chat-composer { display: flex; gap: 8px; padding: 8px 10px 18px; background: var(--wall); }
.chat-composer input {
  flex: 1; border: 0; border-radius: 22px; padding: 11px 14px; background: #fff; font-size: 14.5px; min-width: 0;
}
.chat-composer input:focus-visible { outline: 2px solid var(--brand); }
.chat-composer button {
  width: 44px; height: 44px; border-radius: 50%; border: 0; background: var(--chat-head); color: #fff;
  display: grid; place-items: center; flex: 0 0 auto;
}
.chat-composer button:disabled { opacity: 0.6; }

.sheet {
  position: absolute; inset: 0; background: rgba(10, 20, 20, 0.35); display: flex; align-items: flex-end;
}
.sheet[hidden] { display: none; }
.sheet-card {
  width: 100%; max-height: 72%; overflow-y: auto; background: #fff; border-radius: 18px 18px 0 0;
  padding: 10px 0 18px;
}
.sheet-head { display: flex; align-items: center; gap: 10px; padding: 6px 16px 10px; border-bottom: 1px solid var(--line-2); }
.sheet-close { border: 0; background: none; font-size: 16px; color: var(--muted); }
.sheet-section { padding: 10px 16px 2px; font-size: 13px; color: var(--brand); font-weight: 600; }
.sheet-row {
  display: block; width: 100%; text-align: left; border: 0; background: none; padding: 10px 16px;
  border-bottom: 1px solid var(--line-2);
}
.sheet-row:hover { background: var(--brand-soft); }
.sheet-row strong { display: block; font-weight: 500; font-size: 14.5px; }
.sheet-row span { font-size: 12.5px; color: var(--muted); }

.stage-help { width: 360px; color: var(--stage-ink); font-size: 14px; }
.stage-help p { margin: 0 0 12px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 18px; }
.chips button {
  border: 1px solid rgba(255, 255, 255, 0.25); background: rgba(255, 255, 255, 0.06); color: #fff;
  border-radius: 999px; padding: 7px 12px; font-size: 13.5px;
}
.chips button:hover { background: rgba(255, 255, 255, 0.14); }
.credit { font-size: 12.5px; opacity: 0.7; }

/* ---------- panel ---------- */
.panel {
  background: var(--paper); display: grid; grid-template-rows: auto 1fr; min-height: 0; min-width: 0;
  border-top-left-radius: 18px;
}
.tabs { display: flex; gap: 4px; padding: 14px 20px 0; border-bottom: 1px solid var(--line); }
.tabs button {
  border: 0; background: none; padding: 10px 14px 12px; font-size: 15px; color: var(--muted);
  border-bottom: 2px solid transparent; margin-bottom: -1px; font-weight: 500;
}
.tabs button.active { color: var(--ink); border-bottom-color: var(--brand); }
.count {
  display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 999px; background: var(--urgent);
  color: #fff; font-size: 12px; text-align: center; margin-left: 2px;
}
.count:empty { display: none; }
.tab-pane { display: none; min-height: 0; overflow: auto; }
.tab-pane.active { display: block; }
.tab-pane[data-pane="inbox"].active { display: grid; overflow: hidden; }
.pane-head { padding: 22px 28px 6px; max-width: 78ch; }
.pane-head h2 { margin: 0 0 4px; font-size: 21px; letter-spacing: -0.015em; }
.pane-head p { margin: 0; color: var(--muted); }

/* bandeja */
.inbox { display: grid; grid-template-columns: 270px minmax(0, 1fr) 290px; min-height: 0; height: 100%; }
.conv-list { list-style: none; margin: 0; padding: 0; border-right: 1px solid var(--line); overflow-y: auto; background: var(--surface); }
.conv {
  display: grid; grid-template-columns: 40px minmax(0, 1fr) auto; gap: 10px; padding: 12px 14px; cursor: pointer;
  border-bottom: 1px solid var(--line-2); align-items: start;
}
.conv:hover { background: #fafbfa; }
.conv.active { background: var(--brand-soft); }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; font-weight: 600;
  font-size: 14px; background: #e4ebe8; color: var(--ink);
}
.avatar.you { background: var(--highlight); }
.conv-main { min-width: 0; }
.conv-name { font-weight: 600; font-size: 14.5px; display: flex; align-items: center; gap: 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-preview { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 5px; }
.conv-side { display: flex; flex-direction: column; align-items: flex-end; gap: 5px; font-size: 12px; color: var(--muted); }
.urgent-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--urgent); }

.status {
  display: inline-block; font-size: 12px; font-weight: 500; padding: 2px 8px; border-radius: 999px; white-space: nowrap;
}
.status.bot { background: var(--brand-soft); color: var(--bot); }
.status.waiting_human { background: var(--wait-soft); color: var(--wait); }
.status.human { background: var(--human-soft); color: var(--human); }
.status.closed { background: #eef0ef; color: var(--muted); }

.thread { display: grid; grid-template-rows: auto 1fr auto; min-height: 0; min-width: 0; }
.thread-head {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; padding: 14px 20px;
  border-bottom: 1px solid var(--line); background: var(--surface);
}
.thread-head h3 { margin: 0; font-size: 17px; }
.window { font-size: 13px; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; }
.window::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--bot); }
.window.closed::before { background: #b9c1be; }
.thread-actions { margin-left: auto; display: flex; gap: 8px; }
.btn-line {
  border: 1px solid var(--line); background: var(--surface); border-radius: 8px; padding: 6px 11px; font-size: 13.5px;
}
.btn-line:hover { border-color: var(--brand); color: var(--brand); }
.thread-msgs { overflow-y: auto; padding: 16px 20px; display: flex; flex-direction: column; gap: 8px; }
.tmsg { max-width: 72%; }
.tmsg.in { align-self: flex-start; }
.tmsg.out { align-self: flex-end; }
.tmsg .tb {
  padding: 8px 11px; border-radius: 10px; font-size: 14px; background: var(--surface);
  border: 1px solid var(--line-2); overflow-wrap: anywhere;
}
.tmsg.out .tb { background: #e9f4f1; border-color: #d3e7e2; }
.tmsg.out.staff .tb { background: var(--human-soft); border-color: #d6daf6; }
.tmsg.out.automation .tb { background: #fff7dc; border-color: #f1e2a9; }
.tmeta { display: flex; gap: 8px; align-items: center; font-size: 12px; color: var(--muted); margin-top: 3px; }
.tmsg.out .tmeta { justify-content: flex-end; }
.who { font-weight: 600; }
.ticks { letter-spacing: -3px; color: var(--muted); }
.ticks.read { color: var(--read); }
.json-btn { border: 0; background: none; padding: 0 2px; font-size: 12px; color: var(--muted); text-decoration: underline dotted; }
.json {
  margin: 6px 0 0; padding: 10px; border-radius: 8px; background: #102624; color: #d6efe9;
  font-family: var(--mono); font-size: 11.5px; line-height: 1.5; white-space: pre-wrap; overflow-wrap: anywhere;
  max-height: 260px; overflow-y: auto;
}
.thread-compose { border-top: 1px solid var(--line); background: var(--surface); padding: 12px 20px; }
.thread-compose form { display: flex; gap: 10px; }
.thread-compose textarea {
  flex: 1; resize: none; height: 44px; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px;
  font-size: 14px; min-width: 0;
}
.thread-compose .note { margin: 0 0 8px; font-size: 13px; color: var(--muted); }
.thread-compose .note.warn { color: var(--wait); }
.thread-empty { padding: 40px; color: var(--muted); }

.profile { border-left: 1px solid var(--line); background: var(--surface); overflow-y: auto; padding: 18px; }
.profile h3 { margin: 0 0 2px; font-size: 17px; }
.profile .sub { color: var(--muted); font-size: 13px; margin: 0 0 14px; }
.facts { margin: 0 0 16px; display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; font-size: 13.5px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; overflow-wrap: anywhere; }
.facts code { font-size: 12px; }
.stage-chip {
  display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12.5px; font-weight: 600;
  background: #eef0ef; color: var(--ink);
}
.stage-chip.turno_agendado, .stage-chip.turno_confirmado { background: var(--brand-soft); color: var(--brand); }
.stage-chip.urgencia { background: var(--urgent-soft); color: var(--urgent); }
.stage-chip.consulto_precios { background: var(--wait-soft); color: var(--wait); }
.stage-chip.atendido { background: var(--human-soft); color: var(--human); }
.history { list-style: none; margin: 0; padding: 0; border-left: 2px solid var(--line); }
.history li { position: relative; padding: 0 0 12px 14px; font-size: 13px; }
.history li::before {
  content: ""; position: absolute; left: -6px; top: 5px; width: 10px; height: 10px; border-radius: 50%;
  background: var(--surface); border: 2px solid var(--brand);
}
.history time { display: block; color: var(--muted); font-size: 12px; }
.profile h4 { font-size: 14px; margin: 18px 0 8px; }

/* agenda */
.agenda { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 18px; padding: 18px 28px 28px; }
.day { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.day h3 { margin: 0; padding: 12px 16px; font-size: 15px; border-bottom: 1px solid var(--line); }
.slot {
  display: grid; grid-template-columns: 54px 1fr; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--line-2);
  font-size: 14px; align-items: baseline;
}
.slot:last-child { border-bottom: 0; }
.slot time { font-variant-numeric: tabular-nums; color: var(--muted); }
.slot .free { color: #9aa6a3; }
.slot .who-name { font-weight: 600; }
.slot .trat { color: var(--muted); font-size: 13px; }
.slot.mine .who-name { background: linear-gradient(transparent 55%, var(--highlight) 55%); }
.slot .confirmed { color: var(--brand); font-size: 12.5px; margin-left: 6px; }

/* pacientes */
.funnel { display: flex; flex-wrap: wrap; gap: 8px; padding: 14px 28px 6px; }
.funnel span { font-size: 13px; color: var(--muted); }
.funnel strong { color: var(--ink); margin-right: 4px; font-variant-numeric: tabular-nums; }
.table-wrap { padding: 8px 28px 28px; overflow-x: auto; }
table.leads { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.leads th { text-align: left; font-size: 13px; color: var(--muted); font-weight: 500; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.leads td { padding: 12px 14px; border-bottom: 1px solid var(--line-2); font-size: 14px; vertical-align: top; }
.leads tr:last-child td { border-bottom: 0; }
.leads .last { color: var(--muted); font-size: 13px; }

/* automatizaciones */
.auto { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 380px); gap: 28px; padding: 18px 28px 28px; }
.flow { list-style: none; counter-reset: step; margin: 0; padding: 0; position: relative; }
.flow li {
  counter-increment: step; position: relative; padding: 0 0 22px 52px; display: flex; flex-direction: column; gap: 2px;
}
.flow li::before {
  content: counter(step); position: absolute; left: 0; top: 0; width: 34px; height: 34px; border-radius: 50%;
  background: var(--surface); border: 2px solid var(--brand); color: var(--brand); display: grid; place-items: center;
  font-weight: 700;
}
.flow li:not(:last-child)::after {
  content: ""; position: absolute; left: 17px; top: 36px; bottom: 2px; width: 2px; background: var(--line);
}
.flow span { color: var(--muted); font-size: 14px; max-width: 52ch; }
.template-preview {
  background: var(--wall); border-radius: 12px; padding: 14px; margin-bottom: 14px;
}
.template-preview .bubble { background: #fff; }
.auto-side h3 { font-size: 15px; margin: 22px 0 8px; }
.hint { font-size: 13px; color: var(--muted); margin: 10px 0 0; }
.runs { list-style: none; margin: 0; padding: 0; }
.runs li { padding: 9px 0; border-bottom: 1px solid var(--line-2); font-size: 13.5px; }
.runs .run-status { font-weight: 600; margin-right: 6px; }
.runs .ok { color: var(--brand); }
.runs .err { color: var(--urgent); }
.runs time { color: var(--muted); font-size: 12px; display: block; }

/* resaltador: el único efecto animado */
@keyframes mark { from { background-size: 0% 100%; } to { background-size: 100% 100%; } }
.fresh {
  background-image: linear-gradient(var(--highlight), var(--highlight));
  background-repeat: no-repeat; background-size: 100% 100%;
  animation: mark 0.7s ease-out;
}
@media (prefers-reduced-motion: reduce) { .fresh { animation: none; } }

/* modal y aviso */
.modal { border: 0; border-radius: 16px; padding: 0; max-width: 440px; width: calc(100% - 32px); }
.modal::backdrop { background: rgba(10, 20, 20, 0.5); }
.modal form { padding: 22px 24px; }
.modal h2 { margin: 0 0 8px; font-size: 19px; }
.modal p { color: var(--muted); font-size: 14px; margin: 0 0 14px; }
.modal label { display: block; font-size: 14px; font-weight: 500; margin-bottom: 6px; }
.modal input { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px; }
.modal .btn-ghost { color: var(--ink); border-color: var(--line); }
.form-error { color: var(--urgent) !important; margin: 8px 0 0 !important; }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--ink); color: #fff;
  padding: 10px 16px; border-radius: 10px; font-size: 14px; max-width: calc(100% - 32px); z-index: 10;
}

/* ---------- pantallas medianas ---------- */
@media (max-width: 1360px) {
  .inbox { grid-template-columns: 240px minmax(0, 1fr); }
  .profile { display: none; }
}
@media (max-width: 1080px) {
  .layout { grid-template-columns: 400px minmax(0, 1fr); }
  .phone, .stage-help { width: 330px; }
  .agenda { grid-template-columns: 1fr; }
  .auto { grid-template-columns: 1fr; }
}
/* ---------- celular ---------- */
@media (max-width: 860px) {
  body { grid-template-rows: auto auto 1fr; }
  .topbar { padding: 10px 16px; }
  .brand-sub { display: none; }
  .ai-toggle span:last-child { font-size: 13px; }
  .mobile-tabs { display: flex; background: var(--stage); padding: 0 16px 10px; gap: 6px; }
  .mobile-tabs button {
    flex: 1; border: 1px solid rgba(255, 255, 255, 0.22); background: transparent; color: #fff; border-radius: 999px;
    padding: 8px; font-size: 14px;
  }
  .mobile-tabs button.active { background: #fff; color: var(--ink); }
  .layout { grid-template-columns: 1fr; }
  .layout[data-view="stage"] .panel, .layout[data-view="panel"] .stage { display: none; }
  .stage { padding: 12px 16px; }
  .phone { width: 100%; max-width: 380px; height: calc(100vh - 150px); }
  .stage-help { width: 100%; max-width: 380px; }
  .panel { border-radius: 0; }
  .tabs { overflow-x: auto; padding: 8px 12px 0; }
  .tabs button { white-space: nowrap; }
  .inbox { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .conv-list { max-height: 210px; border-right: 0; border-bottom: 1px solid var(--line); }
  .pane-head, .agenda, .table-wrap, .auto, .funnel { padding-left: 16px; padding-right: 16px; }
  .thread-msgs, .thread-head, .thread-compose { padding-left: 14px; padding-right: 14px; }
  .tmsg { max-width: 88%; }
}
