:root {
  --c-bg: #f4f6f8;
  --c-panel: #ffffff;
  --c-sidebar: #ffffff;
  --c-border: #e3e7ea;
  --c-text: #1a2530;
  --c-text-muted: #6b7680;
  --c-brand: #0f6e5e;
  --c-brand-dark: #0b5347;
  --c-brand-soft: #e3f6ef;
  --c-danger: #b3261e;
  --c-warn: #8a5a00;
  --c-bubble-in: #f0f2f4;
  --c-bubble-out: #d9f2e8;
  --c-hover: #f0f3f4;
  --radius: 10px;
  --radius-sm: 6px;
}

:root[data-theme="dark"] {
  --c-bg: #0f1417;
  --c-panel: #171d21;
  --c-sidebar: #131820;
  --c-border: #2a3238;
  --c-text: #e7edf0;
  --c-text-muted: #8b98a1;
  --c-brand: #17a589;
  --c-brand-dark: #12876f;
  --c-brand-soft: #113830;
  --c-danger: #ff6b60;
  --c-warn: #e0a941;
  --c-bubble-in: #202a30;
  --c-bubble-out: #123d33;
  --c-hover: #1c242a;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --c-bg: #0f1417; --c-panel: #171d21; --c-sidebar: #131820; --c-border: #2a3238;
    --c-text: #e7edf0; --c-text-muted: #8b98a1; --c-brand: #17a589; --c-brand-dark: #12876f;
    --c-brand-soft: #113830; --c-danger: #ff6b60; --c-warn: #e0a941;
    --c-bubble-in: #202a30; --c-bubble-out: #123d33; --c-hover: #1c242a;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--c-bg);
  color: var(--c-text);
  font-size: 13px;
}
a { color: var(--c-brand); }
svg.icon { width: 15px; height: 15px; stroke: currentColor; fill: none; stroke-width: 2; vertical-align: -3px; }

/* Topbar */
.topbar {
  display: flex; align-items: center; gap: 24px;
  background: var(--c-panel); border-bottom: 1px solid var(--c-border);
  padding: 9px 20px;
}
.topbar-brand { font-weight: 700; font-size: 15px; }
.topbar-brand span { color: var(--c-brand); }
.topbar-nav { display: flex; gap: 4px; flex: 1; }
.topbar-nav a {
  text-decoration: none; color: var(--c-text-muted); padding: 6px 12px; border-radius: var(--radius-sm);
  font-weight: 600; font-size: 12.5px;
}
.topbar-nav a.active, .topbar-nav a:hover { background: var(--c-hover); color: var(--c-text); }
.topbar-logout button {
  background: none; border: 1px solid var(--c-border); border-radius: var(--radius-sm);
  padding: 6px 12px; cursor: pointer; font-size: 12.5px; color: var(--c-text-muted);
}
.theme-toggle {
  background: none; border: 1px solid var(--c-border); border-radius: 999px;
  width: 28px; height: 28px; display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer; color: var(--c-text-muted); padding: 0;
}
.theme-toggle:hover { background: var(--c-hover); }

.page { max-width: 1300px; margin: 0 auto; padding: 16px 20px 40px; }
.page.page--flush { max-width: none; padding: 0; }

.messages { position: fixed; top: 54px; right: 16px; z-index: 500; width: 320px; }
.msg { padding: 9px 13px; border-radius: var(--radius-sm); margin-bottom: 6px; font-size: 12.5px; box-shadow: 0 4px 14px rgba(0,0,0,0.12); }
.msg-success { background: var(--c-brand-soft); color: var(--c-brand-dark); }
.msg-error { background: #fbe9e7; color: var(--c-danger); }
.msg-warning { background: #fff4e0; color: var(--c-warn); }
.msg-info { background: #e8f0fe; color: #1a3d8f; }
:root[data-theme="dark"] .msg-error { background: #3a1f1c; }
:root[data-theme="dark"] .msg-warning { background: #3a2e12; }
:root[data-theme="dark"] .msg-info { background: #142238; }

/* Botones */
.btn-primary, .btn-secondary, .btn-link {
  font-family: inherit; font-size: 12.5px; font-weight: 600; cursor: pointer;
  border-radius: var(--radius-sm); border: none; padding: 8px 14px;
}
.btn-primary { background: var(--c-brand); color: #fff; text-decoration: none; display: inline-block; }
.btn-primary:hover { background: var(--c-brand-dark); }
.btn-secondary { background: var(--c-bg); border: 1px solid var(--c-border); color: var(--c-text); }
.btn-link { background: none; border: none; color: var(--c-brand); padding: 2px 4px; text-decoration: underline; }
.btn-icon-send {
  background: var(--c-brand); color: #fff; border: none; border-radius: 50%;
  width: 36px; height: 36px; min-width: 36px; display: flex; align-items: center; justify-content: center; cursor: pointer;
}
.btn-icon-send:hover { background: var(--c-brand-dark); }

/* Auth */
.auth-wrap { display: flex; justify-content: center; padding-top: 80px; }
.auth-card { background: var(--c-panel); border: 1px solid var(--c-border); border-radius: 12px; padding: 32px; width: 320px; box-shadow: 0 8px 24px rgba(0,0,0,0.06); }
.auth-card h1 { font-size: 19px; margin: 0 0 18px; }
.auth-card h1 span { color: var(--c-brand); }
.auth-card label { display: block; font-size: 11.5px; font-weight: 600; margin: 12px 0 4px; color: var(--c-text-muted); }
.auth-card input { width: 100%; padding: 9px 10px; border: 1px solid var(--c-border); border-radius: var(--radius-sm); font-size: 13px; background: var(--c-panel); color: var(--c-text); }
.auth-card .btn-primary { width: 100%; margin-top: 18px; }

/* Forms genéricos */
.form-card { background: var(--c-panel); border: 1px solid var(--c-border); border-radius: 12px; padding: 24px; max-width: 480px; }
.form-card h1 { font-size: 17px; margin: 8px 0 4px; }
.form-card label { display: block; font-size: 11.5px; font-weight: 600; margin: 14px 0 4px; color: var(--c-text-muted); }
.form-card input, .form-card select, .form-card textarea {
  width: 100%; padding: 9px 10px; border: 1px solid var(--c-border); border-radius: var(--radius-sm); font-size: 13px; font-family: inherit;
  background: var(--c-panel); color: var(--c-text);
}
.form-card .btn-primary { margin-top: 18px; }
.form-card .ayuda { font-size: 11.5px; color: var(--c-text-muted); line-height: 1.5; }
.form-card code { background: var(--c-bg); padding: 1px 5px; border-radius: 4px; }
.volver { display: inline-block; font-size: 11.5px; margin-bottom: 12px; text-decoration: none; color: var(--c-text-muted); }

.key-box { background: var(--c-bg); border: 1px dashed var(--c-border); border-radius: var(--radius-sm); padding: 10px 12px; font-family: monospace; font-size: 12px; word-break: break-all; margin: 6px 0 16px; }
.copy-row { display: flex; gap: 8px; align-items: center; }
.copy-row .key-box { flex: 1; margin: 0; }

/* ── Shell tipo WhatsApp (sidebar + panel) ─────────────────────────────── */
.app-shell { display: grid; grid-template-columns: 320px 1fr; height: calc(100vh - 49px); }
.app-shell.app-shell--3col { grid-template-columns: 300px 1fr 300px; }

.sidebar-contactos { background: var(--c-sidebar); border-right: 1px solid var(--c-border); display: flex; flex-direction: column; min-width: 0; }
.sidebar-search { padding: 10px; border-bottom: 1px solid var(--c-border); display: flex; gap: 6px; }
.sidebar-search input { flex: 1; padding: 7px 10px; border: 1px solid var(--c-border); border-radius: 999px; font-size: 12.5px; background: var(--c-bg); color: var(--c-text); }
.sidebar-search button { border: none; background: none; color: var(--c-text-muted); cursor: pointer; padding: 0 4px; }
.sidebar-nuevo { padding: 8px 10px; border-bottom: 1px solid var(--c-border); }
.sidebar-nuevo a { font-size: 12px; font-weight: 600; text-decoration: none; }
.sidebar-lista { flex: 1; overflow-y: auto; }

.contacto-row {
  display: flex; gap: 10px; align-items: center; padding: 10px 12px; text-decoration: none; color: inherit;
  border-bottom: 1px solid var(--c-border);
}
.contacto-row:hover { background: var(--c-hover); }
.contacto-row.activo { background: var(--c-brand-soft); }
.contacto-row.pendiente .contacto-row-preview { color: var(--c-text); font-weight: 600; }
.avatar {
  width: 36px; height: 36px; border-radius: 50%; background: var(--c-brand); color: #fff;
  display: flex; align-items: center; justify-content: center; font-size: 13px; font-weight: 700; flex-shrink: 0;
}
.contacto-row-body { min-width: 0; flex: 1; }
.contacto-row-top { display: flex; justify-content: space-between; gap: 6px; }
.contacto-row-nombre { font-weight: 600; font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contacto-row-fecha { font-size: 10.5px; color: var(--c-text-muted); flex-shrink: 0; }
.contacto-row-preview { font-size: 11.5px; color: var(--c-text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contacto-row-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--c-brand); flex-shrink: 0; }

.panel-vacio { display: flex; align-items: center; justify-content: center; height: 100%; color: var(--c-text-muted); font-size: 13px; flex-direction: column; gap: 8px; }

.empty { padding: 24px; text-align: center; color: var(--c-text-muted); }

/* ── Panel de chat (compartido: interno / embed / preview) ────────────── */
.chat-panel { display: flex; flex-direction: column; height: 100%; min-width: 0; background: var(--c-bg); }
.chat-header {
  display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--c-panel);
  border-bottom: 1px solid var(--c-border);
}
.chat-header-info { min-width: 0; flex: 1; }
.chat-header-nombre { font-weight: 700; font-size: 13.5px; }
.chat-header-telefono { font-size: 11px; color: var(--c-text-muted); }
.chat-header-extra { display: flex; gap: 6px; }

.chat-hilo { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 6px; }
.burbuja { max-width: 65%; padding: 7px 11px; border-radius: 12px; font-size: 12.5px; box-shadow: 0 1px 1px rgba(0,0,0,0.04); }
.burbuja-entrante { align-self: flex-start; background: var(--c-bubble-in); border-bottom-left-radius: 3px; }
.burbuja-saliente { align-self: flex-end; background: var(--c-bubble-out); border-bottom-right-radius: 3px; }
.burbuja-texto { white-space: pre-wrap; word-break: break-word; }
.burbuja-img { max-width: 220px; border-radius: 8px; display: block; margin-bottom: 4px; }
.burbuja-meta { font-size: 9.5px; color: var(--c-text-muted); margin-top: 3px; text-align: right; display: flex; gap: 3px; justify-content: flex-end; align-items: center; }
.burbuja-nota { align-self: center; background: transparent; color: var(--c-text-muted); font-size: 11px; font-style: italic; }

.chat-input { display: flex; gap: 8px; padding: 10px 14px; border-top: 1px solid var(--c-border); background: var(--c-panel); align-items: flex-end; }
.chat-input textarea {
  flex: 1; padding: 9px 14px; border: 1px solid var(--c-border); border-radius: 20px; font-family: inherit;
  font-size: 13px; resize: none; max-height: 90px; background: var(--c-bg); color: var(--c-text);
}
.chat-cerrada { padding: 12px 14px; border-top: 1px solid var(--c-border); font-size: 11.5px; color: var(--c-text-muted); background: var(--c-panel); }
.chat-plantilla { display: flex; gap: 8px; margin-top: 8px; align-items: flex-start; }
.chat-plantilla select { padding: 8px 10px; border: 1px solid var(--c-border); border-radius: var(--radius-sm); background: var(--c-panel); color: var(--c-text); }
#plantilla-variables { display: flex; flex-direction: column; gap: 6px; flex: 1; }
#plantilla-variables input { padding: 8px 10px; border: 1px solid var(--c-border); border-radius: var(--radius-sm); font-size: 12px; background: var(--c-panel); color: var(--c-text); }

/* ── Ficha + HCC (columna interna, no aparece en embed) ─────────────────── */
.ficha-hcc { background: var(--c-panel); border-left: 1px solid var(--c-border); padding: 16px; overflow-y: auto; }
.ficha-hcc label { display: block; font-size: 11px; font-weight: 600; margin: 10px 0 4px; color: var(--c-text-muted); }
.ficha-hcc input, .ficha-hcc textarea { width: 100%; padding: 7px 9px; border: 1px solid var(--c-border); border-radius: var(--radius-sm); font-size: 12.5px; font-family: inherit; background: var(--c-panel); color: var(--c-text); }
.ficha-hcc .btn-secondary { margin-top: 10px; width: 100%; }
.ficha-preview-btn { width: 100%; margin-top: 8px; text-align: center; }

.hcc { margin-top: 18px; border-top: 1px solid var(--c-border); padding-top: 14px; }
.hcc h3 { font-size: 12px; margin: 0 0 10px; text-transform: uppercase; letter-spacing: .03em; color: var(--c-text-muted); }
.hcc-nueva textarea { margin-bottom: 6px; }
.hcc-lista { max-height: 280px; overflow-y: auto; margin-top: 10px; display: flex; flex-direction: column; gap: 8px; }
.hcc-item { background: var(--c-bg); border-radius: var(--radius-sm); padding: 8px 10px; font-size: 11.5px; }
.hcc-item-meta { color: var(--c-text-muted); margin-top: 4px; font-size: 10.5px; }
.hcc-sistema { background: #fff4e0; }
:root[data-theme="dark"] .hcc-sistema { background: #3a2e12; }

/* ── Embed / preview de un solo contacto (sin sidebar, standalone) ──────── */
.embed-page { height: 100vh; display: flex; flex-direction: column; background: var(--c-bg); }
.embed-page .chat-panel { flex: 1 1 auto; min-height: 0; height: auto; }
.embed-banner { background: var(--c-warn); color: #1a1100; font-size: 11px; text-align: center; padding: 4px; font-weight: 600; }
.embed-topbar { display: flex; justify-content: flex-end; padding: 6px 10px; background: var(--c-panel); border-bottom: 1px solid var(--c-border); }
.embed-expirado { display: flex; align-items: center; justify-content: center; height: 100vh; flex-direction: column; gap: 8px; color: var(--c-text-muted); }

/* Formularios genéricos: nuevo contacto / gestión */
.inbox-header { display: flex; gap: 12px; align-items: center; margin-bottom: 16px; padding: 0 20px; }

/* Administrador */
.admin-section { background: var(--c-panel); border: 1px solid var(--c-border); border-radius: 12px; padding: 18px 20px; margin-bottom: 18px; }
.admin-section-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.admin-section-head h2 { font-size: 13.5px; margin: 0; }
.admin-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.admin-table th { text-align: left; font-size: 10.5px; text-transform: uppercase; color: var(--c-text-muted); padding: 6px 8px; border-bottom: 1px solid var(--c-border); }
.admin-table td { padding: 8px; border-bottom: 1px solid var(--c-border); vertical-align: middle; }
.admin-actions { display: flex; gap: 10px; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 10.5px; font-weight: 600; }
.badge-ok { background: var(--c-brand-soft); color: var(--c-brand-dark); }
.badge-off { background: var(--c-hover); color: var(--c-text-muted); }
.badge-warn { background: #fff4e0; color: var(--c-warn); }
:root[data-theme="dark"] .badge-warn { background: #3a2e12; }

@media (max-width: 900px) {
  .app-shell, .app-shell.app-shell--3col { grid-template-columns: 1fr; }
  .sidebar-contactos { display: none; }
}
