:root {
  --bg: #f6f6f3; --panel: #ffffff; --panel-2: #f0f0ec; --line: #e2e2dc; --text: #1c1c1a; --muted: #6b6b66;
  --accent: #0f766e; --accent-ink: #ffffff; --accent-soft: #d8efec;
  --ok: #15803d; --warn: #b45309; --err: #b91c1c; --info: #1d4ed8;
  --bubble-in: #ffffff; --bubble-out: #dcf3e6; --note: #fff6d6; --chat-bg: #efeae2;
  --radius: 10px; --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.05);
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121412; --panel: #1b1e1c; --panel-2: #232725; --line: #2f3431; --text: #ecefec; --muted: #9aa29d;
    --accent: #2dd4bf; --accent-ink: #062b27; --accent-soft: #123a35;
    --bubble-in: #232725; --bubble-out: #12372f; --note: #3a3417; --chat-bg: #0f1311; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121412; --panel: #1b1e1c; --panel-2: #232725; --line: #2f3431; --text: #ecefec; --muted: #9aa29d;
  --accent: #2dd4bf; --accent-ink: #062b27; --accent-soft: #123a35;
  --bubble-in: #232725; --bubble-out: #12372f; --note: #3a3417; --chat-bg: #0f1311; color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 14px/1.45 var(--font); -webkit-font-smoothing: antialiased; }
a { color: var(--accent); }
h1 { font-size: 20px; margin: 0; }
h2 { font-size: 16px; margin: 0 0 8px; }
h3 { font-size: 14px; margin: 16px 0 6px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
small, .muted { color: var(--muted); }
code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }
button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; width: 100%; }
textarea { resize: vertical; min-height: 70px; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label { display: block; font-size: 12.5px; color: var(--muted); margin: 10px 0 4px; }
label.inline { display: inline-flex; gap: 6px; align-items: center; margin: 0; color: var(--text); font-size: 14px; }
label.inline input { width: auto; }
.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 7px 12px; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn.danger { color: var(--err); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.sm { padding: 4px 8px; font-size: 12.5px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.stack > * + * { margin-top: 12px; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 99px; font-size: 12px; background: var(--panel-2); border: 1px solid var(--line); }
.chip.ok { color: var(--ok); } .chip.warn { color: var(--warn); } .chip.err { color: var(--err); } .chip.info { color: var(--info); }
.chip.accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 600; }
tr.click { cursor: pointer; } tr.click:hover td { background: var(--panel-2); }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; }
.kpi b { display: block; font-size: 22px; }
.kpi span { color: var(--muted); font-size: 12px; }
.empty { padding: 40px; text-align: center; color: var(--muted); }

/* Estructura */
.shell { display: grid; grid-template-columns: 220px 1fr; height: 100vh; height: 100dvh; }
.side { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 12px; gap: 4px; overflow: auto; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; padding: 4px 6px 12px; }
.brand img { width: 26px; height: 26px; }
.side a.nav { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 8px; color: var(--text); text-decoration: none; }
.side a.nav:hover { background: var(--panel-2); }
.side a.nav.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.side a.nav .badge { margin-left: auto; background: var(--accent); color: var(--accent-ink); border-radius: 99px; font-size: 11px; padding: 0 7px; }
.side .foot { margin-top: auto; padding-top: 12px; border-top: 1px solid var(--line); }
.main { overflow: auto; min-width: 0; }
.page { padding: 20px 24px 60px; max-width: 1200px; }
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; }
.tabs button { background: none; border: 0; padding: 8px 12px; cursor: pointer; color: var(--muted); border-bottom: 2px solid transparent; }
.tabs button.on { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.mobile-bar { display: none; }

/* Bandeja */
.inbox { display: grid; grid-template-columns: 320px 1fr 280px; height: 100vh; height: 100dvh; }
.inbox .list { border-right: 1px solid var(--line); overflow: auto; background: var(--panel); }
.inbox .list .filters { position: sticky; top: 0; background: var(--panel); padding: 10px; border-bottom: 1px solid var(--line); z-index: 1; }
.conv { padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; }
.conv:hover { background: var(--panel-2); }
.conv.on { background: var(--accent-soft); }
.conv .top { display: flex; gap: 6px; align-items: baseline; }
.conv .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .time { margin-left: auto; font-size: 11.5px; color: var(--muted); white-space: nowrap; }
.conv .preview { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .unread { background: var(--accent); color: var(--accent-ink); border-radius: 99px; font-size: 11px; padding: 0 6px; }
.thread { display: flex; flex-direction: column; min-width: 0; background: var(--chat-bg); }
.thread .head { background: var(--panel); border-bottom: 1px solid var(--line); padding: 10px 14px; display: flex; align-items: center; gap: 10px; }
.msgs { flex: 1; overflow: auto; padding: 16px; display: flex; flex-direction: column; gap: 6px; }
.bubble { max-width: min(78%, 560px); padding: 7px 10px 5px; border-radius: 10px; background: var(--bubble-in); box-shadow: 0 1px 1px rgba(0,0,0,.06); white-space: pre-wrap; word-wrap: break-word; }
.bubble.out { align-self: flex-end; background: var(--bubble-out); }
.bubble.note { align-self: center; background: var(--note); font-size: 13px; max-width: 90%; }
.bubble .meta { font-size: 11px; color: var(--muted); text-align: right; margin-top: 2px; }
.bubble img, .bubble video { max-width: 260px; border-radius: 6px; display: block; margin-bottom: 4px; }
.bubble .tpl { font-size: 11px; color: var(--accent); font-weight: 600; margin-bottom: 2px; }
.bubble.failed { outline: 1px solid var(--err); }
.daysep { align-self: center; font-size: 11.5px; background: var(--panel); border-radius: 6px; padding: 2px 8px; color: var(--muted); margin: 6px 0; }
.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 10px; }
.composer textarea { min-height: 44px; max-height: 180px; }
.window { font-size: 12px; padding: 6px 10px; border-radius: 8px; margin-bottom: 8px; }
.window.open { background: var(--accent-soft); color: var(--accent); }
.window.closed { background: var(--note); }
.ctx { border-left: 1px solid var(--line); background: var(--panel); overflow: auto; padding: 14px; }
.ai-box { background: var(--panel-2); border-radius: 8px; padding: 10px; font-size: 13px; }

/* Campañas */
.steps { display: flex; gap: 6px; margin-bottom: 14px; flex-wrap: wrap; }
.steps span { padding: 4px 10px; border-radius: 99px; background: var(--panel-2); font-size: 12.5px; color: var(--muted); }
.steps span.on { background: var(--accent); color: var(--accent-ink); }
.bar { height: 8px; background: var(--panel-2); border-radius: 99px; overflow: hidden; display: flex; }
.bar i { display: block; height: 100%; }

/* Templates: vista de teléfono */
.phone { background: var(--chat-bg); border-radius: 18px; padding: 16px; min-height: 280px; border: 1px solid var(--line); }
.phone .bubble { max-width: 100%; }
.phone .btns { display: grid; gap: 4px; margin-top: 4px; }
.phone .btns div { background: var(--bubble-in); text-align: center; color: var(--info); border-radius: 8px; padding: 6px; font-size: 13px; }
.media-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.media-grid .card { padding: 8px; }
.media-grid img, .media-grid video { width: 100%; height: 120px; object-fit: cover; border-radius: 6px; background: var(--panel-2); }
.drop { border: 2px dashed var(--line); border-radius: var(--radius); padding: 24px; text-align: center; color: var(--muted); }
.drop.over { border-color: var(--accent); color: var(--accent); }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login .card { width: 100%; max-width: 360px; }

dialog { border: 1px solid var(--line); border-radius: 14px; padding: 0; background: var(--panel); color: var(--text); width: min(640px, calc(100vw - 32px)); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(0,0,0,.35); }
#modal-body { padding: 18px; max-height: 85vh; overflow: auto; }
#toasts { position: fixed; bottom: 16px; right: 16px; display: grid; gap: 8px; z-index: 50; }
.toast { background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); max-width: 360px; }
.toast.err { background: var(--err); color: #fff; }
pre.box { background: var(--panel-2); padding: 10px; border-radius: 8px; overflow: auto; white-space: pre-wrap; word-break: break-all; }

@media (max-width: 1100px) { .inbox { grid-template-columns: 300px 1fr; } .inbox .ctx { display: none; } .inbox.show-ctx .ctx { display: block; position: fixed; right: 0; top: 0; bottom: 0; width: 300px; z-index: 5; box-shadow: var(--shadow); } }
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: fixed; inset: 0 30% 0 0; z-index: 20; transform: translateX(-102%); transition: transform .2s; }
  .shell.menu .side { transform: none; box-shadow: var(--shadow); }
  .mobile-bar { display: flex; align-items: center; gap: 8px; padding: 8px 16px; background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 4; }
  .page { padding: 16px; }
  .grid2, .grid3 { grid-template-columns: 1fr; }
  .inbox { grid-template-columns: 1fr; height: calc(100dvh - 49px); }
  .inbox .thread { display: none; }
  .inbox.open .list { display: none; }
  .inbox.open .thread { display: flex; }
  table { font-size: 13px; } th, td { padding: 6px; }
  .hide-sm { display: none; }
}
