/* ==========================================================
   Portal Capta — /login y /app
   ========================================================== */
:root {
  --font-primary: "Manrope", system-ui, sans-serif;
  --font-secondary: "IBM Plex Sans", system-ui, sans-serif;
  --orange: #e8751a;
  --orange-dark: #c95f0c;
  --orange-50: #fdf2e8;
  --black: #0a0a0a;
  --white: #ffffff;
  --bg: #f6f5f3;
  --border: #e6e3df;
  --muted: #5c5955;
  --ok: #1a7f43;
  --ok-bg: #e7f6ed;
  --error: #b42318;
  --error-bg: #fdecea;
  --radius: 14px;
}

*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; }
body {
  min-height: 100vh;
  background: var(--bg);
  color: var(--black);
  font-family: var(--font-secondary);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
a { color: var(--orange); }
h1, h2, h3 { font-family: var(--font-primary); margin: 0; }

/* ---------- Marca ---------- */
.brand { display: inline-flex; align-items: center; gap: 10px; text-decoration: none; }
.brand img { display: block; }

/* ---------- Botones y campos ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 20px;
  border: 1px solid transparent;
  border-radius: 10px;
  font: 600 15px/1 var(--font-primary);
  cursor: pointer;
  text-decoration: none;
  transition: background .15s, border-color .15s, opacity .15s;
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn--primary { background: var(--orange); color: var(--white); }
.btn--primary:hover:not(:disabled) { background: var(--orange-dark); }
.btn--dark { background: var(--black); color: var(--white); }
.btn--dark:hover:not(:disabled) { background: #2a2a2a; }
.btn--ghost { background: transparent; border-color: var(--border); color: var(--black); }
.btn--ghost:hover:not(:disabled) { border-color: var(--black); }
.btn--sm { min-height: 36px; padding: 0 14px; font-size: 14px; }
.btn--block { width: 100%; }

.field { display: grid; gap: 6px; margin-bottom: 16px; }
.field label { font-weight: 500; font-size: 14px; }
.field small { color: var(--muted); font-size: 13px; }
.input {
  width: 100%;
  min-height: 44px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--white);
  color: var(--black);
  font: inherit;
}
.input:focus { outline: 2px solid var(--orange); outline-offset: 1px; border-color: transparent; }
textarea.input { min-height: 96px; resize: vertical; }

.alert { margin: 0 0 16px; padding: 12px 14px; border-radius: 10px; font-size: 14px; }
.alert--error { background: var(--error-bg); color: var(--error); }
.alert--ok { background: var(--ok-bg); color: var(--ok); }
.alert--info { background: var(--orange-50); color: var(--orange-dark); }

/* ---------- Login ---------- */
.auth {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 24px 16px;
  background:
    radial-gradient(1200px 600px at 100% 0%, rgba(232, 117, 26, .14), transparent 60%),
    var(--bg);
}
.auth__card {
  width: 100%;
  max-width: 400px;
  padding: 36px 32px;
  background: var(--white);
  border: 1px solid var(--border);
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(10, 10, 10, .06);
}
.auth__card .brand { margin-bottom: 28px; }
.auth__card h1 { font-size: 26px; margin-bottom: 6px; }
.auth__card > p { margin: 0 0 24px; color: var(--muted); }
.auth__foot { margin-top: 20px; font-size: 13px; color: var(--muted); text-align: center; }

/* ---------- App ---------- */
.topbar { background: var(--white); border-bottom: 1px solid var(--border); box-shadow: inset 0 3px 0 var(--orange); }
.topbar__inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  max-width: 1080px;
  margin: 0 auto;
  padding: 14px 24px;
}
.topbar__user { display: flex; align-items: center; gap: 14px; font-size: 14px; color: var(--muted); min-width: 0; }
.topbar__user span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.page { max-width: 1080px; margin: 0 auto; padding: 36px 24px 72px; }
.page__head { margin-bottom: 24px; }
.page__head h1 { font-size: 28px; }
.page__head p { margin: 4px 0 0; color: var(--muted); }

.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.card {
  padding: 24px;
  background: var(--white);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.card--wide { grid-column: 1 / -1; }
.card__head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.card__head h2 { font-size: 18px; }
.card > p { margin: 0 0 16px; color: var(--muted); }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.badge--off { background: #efedea; color: var(--muted); }
.badge--ok { background: var(--ok-bg); color: var(--ok); }
.badge--warn { background: var(--orange-50); color: var(--orange-dark); }

.details { display: grid; grid-template-columns: max-content 1fr; gap: 8px 16px; margin: 0 0 20px; font-size: 14px; }
.details dt { color: var(--muted); }
.details dd { margin: 0; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; word-break: break-all; }

.segmented { display: flex; gap: 6px; padding: 4px; background: var(--bg); border-radius: 10px; margin-bottom: 16px; }
.segmented label {
  flex: 1;
  padding: 8px 10px;
  border-radius: 8px;
  text-align: center;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label:has(input:checked) { background: var(--white); box-shadow: 0 1px 3px rgba(0, 0, 0, .1); }
.segmented label:has(input:focus-visible) { outline: 2px solid var(--orange); }

.messages { list-style: none; margin: 0; padding: 0; }
.messages li {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: start;
  gap: 12px;
  padding: 12px 0;
  border-top: 1px solid var(--border);
}
.messages li:first-child { border-top: 0; }
.messages__dir {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  font-size: 14px;
  font-weight: 700;
}
.messages__dir--in { background: var(--orange-50); color: var(--orange-dark); }
.messages__dir--out { background: var(--black); color: var(--white); }
.messages__who { font-weight: 600; font-size: 14px; }
.messages__body { margin: 2px 0 0; color: var(--black); word-break: break-word; }
.messages__meta { color: var(--muted); font-size: 12px; text-align: right; white-space: nowrap; }
.empty { margin: 0; padding: 20px 0; color: var(--muted); text-align: center; }

.card__actions { display: flex; gap: 8px; }
.card__actions .btn { text-decoration: none; }
select.input { appearance: auto; }

.templates { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 12px; }
.templates li { display: flex; flex-direction: column; gap: 10px; padding: 16px; border: 1px solid var(--border); border-radius: 12px; }
.templates__head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.templates__name { font-weight: 600; font-size: 14px; word-break: break-all; }
.templates__meta { color: var(--muted); font-size: 12px; }
.templates__preview { margin: 0; padding: 10px 12px; background: var(--bg); border-radius: 10px; font-size: 14px; white-space: pre-wrap; word-break: break-word; }
.templates__preview b { display: block; margin-bottom: 4px; }
.templates__preview small { display: block; margin-top: 6px; color: var(--muted); }
.templates__note { margin: 0; color: var(--muted); font-size: 12px; }
.templates__chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { padding: 4px 10px; border: 1px solid var(--border); border-radius: 999px; font-size: 12px; color: var(--muted); }
.templates__actions { display: flex; gap: 8px; margin-top: auto; }
.badge--error { background: var(--error-bg); color: var(--error); }
.btn--danger { color: var(--error); }
.btn--danger:hover:not(:disabled) { border-color: var(--error); }

/* ---------- Navegación del panel ---------- */
.topnav { display: flex; gap: 4px; margin-right: auto; margin-left: 12px; }
.topnav a { padding: 8px 12px; border-radius: 8px; color: var(--muted); font-weight: 500; font-size: 14px; text-decoration: none; }
.topnav a:hover { color: var(--black); background: var(--bg); }
.topnav a[aria-current="page"] { color: var(--black); background: var(--orange-50); }

/* ---------- Página de plantillas ---------- */
.page__head--row { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; }
.creator { margin-bottom: 20px; }
.creator__grid { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 28px; align-items: start; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field__label-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.optional { color: var(--muted); font-weight: 400; font-size: 13px; }
.link-btn { padding: 0; border: 0; background: none; color: var(--orange); font: 500 14px var(--font-secondary); cursor: pointer; }
.link-btn:hover { text-decoration: underline; }
.link-btn--danger { color: var(--error); }
textarea.input { resize: vertical; }
.examples__title { margin: 0 0 10px; font-size: 13px; color: var(--muted); }
#examples:not(:empty) { margin-bottom: 16px; padding: 14px; background: var(--orange-50); border-radius: 10px; }
.field--inline { grid-template-columns: 150px 1fr; align-items: center; margin-bottom: 8px; }
.field--inline:last-child { margin-bottom: 0; }
#buttons { display: grid; gap: 8px; }
.button-row { display: grid; grid-template-columns: 170px 1fr auto; gap: 8px; align-items: center; }
.button-row:has([data-b="extra"]:not([hidden])) { grid-template-columns: 170px 1fr 1fr auto; }
.form-note { margin: 10px 0 0; color: var(--muted); font-size: 13px; }

/* Vista previa estilo WhatsApp */
.wa-preview { position: sticky; top: 20px; }
.wa-preview__label { margin-bottom: 8px; font-size: 13px; font-weight: 500; color: var(--muted); }
.wa-preview__chat { min-height: 320px; padding: 20px 16px; border-radius: 14px; background: #efe7dd; }
.wa-bubble { position: relative; max-width: 92%; padding: 8px 10px 20px; background: var(--white); border-radius: 0 10px 10px 10px; box-shadow: 0 1px 1px rgba(0, 0, 0, .08); font-size: 14px; }
.wa-bubble__header { display: block; margin-bottom: 4px; }
.wa-bubble__body { margin: 0; white-space: pre-wrap; word-break: break-word; }
.wa-bubble__body.is-placeholder { color: var(--muted); }
.wa-bubble__footer { display: block; margin-top: 6px; color: var(--muted); font-size: 12px; }
.wa-bubble__time { position: absolute; right: 8px; bottom: 4px; color: var(--muted); font-size: 11px; }
.wa-buttons { display: grid; gap: 4px; max-width: 92%; margin-top: 4px; }
.wa-button { padding: 9px; background: var(--white); border-radius: 10px; color: #027eb5; font-size: 14px; font-weight: 500; text-align: center; box-shadow: 0 1px 1px rgba(0, 0, 0, .08); }
.segmented--inline { display: inline-flex; flex-wrap: wrap; }
.segmented--inline label { flex: none; }

@media (max-width: 900px) {
  .creator__grid { grid-template-columns: 1fr; }
  .wa-preview { position: static; }
}

@media (max-width: 800px) {
  .grid { grid-template-columns: 1fr; }
  .page { padding: 28px 16px 56px; }
  .topbar__inner { padding: 12px 16px; }
}
@media (max-width: 480px) {
  .auth__card { padding: 28px 20px; }
  .topbar__user span { display: none; }
  .messages li { grid-template-columns: auto 1fr; }
  .messages__meta { grid-column: 2; text-align: left; }
  .topnav { margin-left: 0; }
  .topbar__inner { gap: 8px; }
  .topbar .brand img + img { display: none; }
  .topbar .btn { white-space: nowrap; padding: 0 10px; font-size: 13px; }
  .topnav { gap: 0; }
  .topnav a { padding: 8px 6px; font-size: 13px; }
  .messages__meta { white-space: normal; }
  .page__head--row > .badge { align-self: flex-start; }
  .wa-preview__chat { min-height: 0; }
  .topnav a { padding: 8px; }
  .page__head--row { flex-direction: column; align-items: stretch; }
  .field-row, .field--inline { grid-template-columns: 1fr; }
  .button-row, .button-row:has([data-b="extra"]:not([hidden])) { grid-template-columns: 1fr auto; }
  .button-row [data-b="type"] { grid-column: 1 / -1; }
}
@media (max-width: 380px) {
  .topbar__inner { padding: 12px; gap: 4px; }
  .topnav a { padding: 8px 4px; }
}

/* ---------- Mensajes: bandeja tipo chat ---------- */
/* La bandeja ocupa el ancho del encabezado */
.inbox {
  display: grid;
  grid-template-columns: 340px minmax(0, 1fr);
  height: calc(100vh - 230px);
  min-height: 520px;
  background: var(--white);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.inbox__list { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--border); }
.inbox__search { padding: 12px; border-bottom: 1px solid var(--border); }
.convs { flex: 1; overflow-y: auto; list-style: none; margin: 0; padding: 0; }
.inbox__list .empty { padding: 24px 20px; }
.conv { display: flex; gap: 12px; align-items: center; width: 100%; padding: 12px 14px; border: 0; border-bottom: 1px solid var(--border); background: none; text-align: left; font: inherit; color: inherit; cursor: pointer; }
.conv:hover { background: var(--bg); }
.conv.is-active { background: var(--orange-50); }
.conv__avatar, .chat__avatar { flex-shrink: 0; display: grid; place-items: center; width: 42px; height: 42px; border-radius: 50%; background: #efedea; color: var(--muted); font: 600 14px var(--font-primary); }
.conv.is-active .conv__avatar { background: var(--orange); color: var(--white); }
.conv__text { flex: 1; min-width: 0; display: grid; gap: 2px; }
.conv__top { display: flex; justify-content: space-between; gap: 8px; }
.conv__name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.conv__time { flex-shrink: 0; color: var(--muted); font-size: 12px; }
.conv__preview { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 13px; }

.inbox__chat { position: relative; display: flex; flex-direction: column; min-height: 0; background: #efe7dd; }
.chat__placeholder { flex: 1; display: grid; place-items: center; color: var(--muted); }
.chat { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chat__head { display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--white); border-bottom: 1px solid var(--border); }
.chat__back { display: none; padding: 4px 8px; border: 0; background: none; font-size: 20px; cursor: pointer; }
.chat__avatar { width: 38px; height: 38px; }
.chat__who { flex: 1; min-width: 0; display: grid; }
.chat__who strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat__who small { color: var(--muted); font-size: 12px; }
.chat__to { flex: 1; display: flex; align-items: center; gap: 8px; }
.chat__to label { font-weight: 500; font-size: 14px; }
.chat__to .input { min-height: 38px; }

.thread { flex: 1; overflow-y: auto; list-style: none; margin: 0; padding: 16px 6%; display: flex; flex-direction: column; gap: 4px; }
.thread__day { align-self: center; margin: 10px 0 6px; padding: 4px 10px; background: rgba(255, 255, 255, .85); border-radius: 8px; color: var(--muted); font-size: 12px; }
.thread__empty { margin: auto; padding: 16px; max-width: 320px; text-align: center; color: var(--muted); font-size: 14px; }
.bubble { position: relative; max-width: min(75%, 520px); padding: 6px 10px 6px; border-radius: 10px; font-size: 14px; box-shadow: 0 1px 1px rgba(0, 0, 0, .08); }
.bubble--in { align-self: flex-start; background: var(--white); border-top-left-radius: 2px; }
.bubble--out { align-self: flex-end; background: #d9fdd3; border-top-right-radius: 2px; }
.bubble__body { display: inline; margin: 0; white-space: pre-wrap; word-break: break-word; }
.bubble__meta { float: right; display: inline-flex; gap: 4px; align-items: center; margin: 6px 0 -2px 10px; color: var(--muted); font-size: 11px; }
.bubble__status { letter-spacing: -2px; }
.bubble__status.is-read { color: #34b7f1; }
.bubble__status.is-failed { color: var(--error); letter-spacing: 0; }
.bubble__error { clear: both; display: block; padding-top: 4px; color: var(--error); font-size: 12px; }

.composer { display: grid; gap: 8px; padding: 10px 14px 12px; background: var(--white); border-top: 1px solid var(--border); }
.composer .alert { margin: 0; }
.composer__notice { margin: 0; padding: 8px 10px; background: var(--orange-50); border-radius: 8px; color: var(--orange-dark); font-size: 13px; }
.segmented--sm { justify-self: start; margin: 0; padding: 3px; }
.segmented--sm label { flex: none; padding: 4px 12px; font-size: 13px; }
.segmented label.is-disabled { opacity: .45; cursor: not-allowed; }
.composer__template { display: grid; gap: 8px; }
.composer__params { display: flex; flex-wrap: wrap; gap: 8px; }
.composer__params:empty { display: none; }
.composer__params .input { flex: 1 1 160px; min-height: 38px; }
.composer__preview { margin: 0; padding: 8px 10px; background: var(--bg); border-radius: 8px; color: var(--muted); font-size: 13px; white-space: pre-wrap; }
.composer__preview b { display: block; color: var(--black); }
.composer__row { display: flex; gap: 8px; align-items: flex-end; }
textarea.composer__text { flex: 1; height: 44px; min-height: 44px; max-height: 160px; padding-top: 10px; padding-bottom: 10px; resize: none; }
.composer__send { margin-left: auto; }

@media (max-width: 800px) {
  .page--inbox { padding-bottom: 16px; }
  .inbox { grid-template-columns: 1fr; height: calc(100vh - 210px); min-height: 460px; }
  .inbox__list { border-right: 0; }
  .inbox__chat { display: none; }
  .inbox.is-open .inbox__list { display: none; }
  .inbox.is-open .inbox__chat { display: flex; }
  .chat__back { display: block; }
  .chat__head { padding: 8px 10px; gap: 8px; }
  .chat__head .badge { display: none; }
  .thread { padding: 12px; }
  .bubble { max-width: 85%; }
}
@media (max-width: 800px) {
  /* Con un chat abierto, la conversación usa toda la pantalla */
  .page--inbox:has(.inbox.is-open) { padding-top: 12px; }
  .page--inbox:has(.inbox.is-open) .page__head { display: none; }
  .inbox.is-open { height: calc(100dvh - 90px); min-height: 420px; }
}
