/* MAX IA — estilos. Tokens primero; los componentes solo usan variables. */
:root {
  --bg: #f4f5f7; --surface: #ffffff; --surface-2: #f8f9fb; --border: #e3e6ea; --text: #16191d; --muted: #66707b;
  --accent: #4f46e5; --accent-fg: #ffffff; --accent-soft: #eef0ff;
  --in-bubble: #ffffff; --out-bubble: #e8ecff; --ai-bubble: #f1ecff; --echo-bubble: #eef1f4;
  --wa: #1fa855; --ig: #d6337a; --ms: #2f7cf6;
  --ok: #157f3b; --ok-soft: #e7f6ec; --warn: #9a6200; --warn-soft: #fff4dd; --err: #c02929; --err-soft: #fdeaea;
  --c-gray: #e9ecef; --c-red: #fde2e2; --c-amber: #fff0cc; --c-green: #dcf5e4; --c-blue: #dfeaff; --c-purple: #ece3ff;
  --radius: 10px; --radius-sm: 6px; --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --topbar-h: 52px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115; --surface: #171a20; --surface-2: #1d2128; --border: #2a2f38; --text: #e8eaed; --muted: #9aa3ad;
    --accent: #8b85ff; --accent-fg: #0f1115; --accent-soft: #24243f;
    --in-bubble: #1f232b; --out-bubble: #262a4a; --ai-bubble: #2d2545; --echo-bubble: #222730;
    --ok: #5fd08a; --ok-soft: #13291c; --warn: #f0b44c; --warn-soft: #2e2512; --err: #ff7b7b; --err-soft: #331617;
    --c-gray: #2a2f38; --c-red: #3b1d20; --c-amber: #3a2e12; --c-green: #163222; --c-blue: #182a47; --c-purple: #2b2147;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.45 var(--font); background: var(--bg); color: var(--text); }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 8px 10px; width: 100%; }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea { resize: vertical; }
label { display: block; font-weight: 500; margin-bottom: 10px; }
label > input, label > select, label > textarea { margin-top: 4px; font-weight: 400; }
code, kbd, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); } .small { font-size: 12px; } .center { text-align: center; } .grow { flex: 1; min-width: 0; }
.err-text { color: var(--err); }
.ok, .err { padding: 10px 12px; border-radius: var(--radius-sm); }
.ok { background: var(--ok-soft); color: var(--ok); } .err { background: var(--err-soft); color: var(--err); }

/* botones */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--border); background: var(--surface);
  border-radius: var(--radius-sm); padding: 7px 12px; cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--err); }
.btn.icon { padding: 6px 9px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* chips y pills */
.pill { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--c-gray); white-space: nowrap; }
.pill.red { background: var(--c-red); } .pill.green { background: var(--c-green); } .pill.blue { background: var(--c-blue); } .pill.amber { background: var(--c-amber); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.chip { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--c-gray); }
.chip button { border: 0; background: none; cursor: pointer; padding: 0 2px; color: var(--muted); }
.c-red { background: var(--c-red); } .c-amber { background: var(--c-amber); } .c-green { background: var(--c-green); }
.c-blue { background: var(--c-blue); } .c-purple { background: var(--c-purple); } .c-gray { background: var(--c-gray); }

/* barra superior */
.topbar { height: var(--topbar-h); display: flex; align-items: center; justify-content: space-between; padding: 0 16px;
  background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; text-decoration: none; color: var(--text); }
.logo { width: 28px; height: 28px; border-radius: 8px; background: var(--accent); color: var(--accent-fg); display: grid; place-items: center; font-weight: 800; }
.topbar nav { display: flex; gap: 4px; }
.topbar nav a { text-decoration: none; color: var(--muted); padding: 6px 10px; border-radius: var(--radius-sm); }
.topbar nav a.active, .topbar nav a:hover { color: var(--text); background: var(--surface-2); }

/* login / instalar */
.auth { display: grid; place-items: center; min-height: 100%; padding: 16px; }
.auth-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 28px; box-shadow: var(--shadow); }
.auth-card h1 { font-size: 20px; margin: 12px 0 16px; }
.auth-card .btn { width: 100%; margin-top: 6px; }
.key { white-space: pre-wrap; word-break: break-all; background: var(--surface-2); padding: 10px; border-radius: var(--radius-sm); }

/* bandeja: 3 columnas */
.app { overflow: hidden; }
.inbox { display: grid; grid-template-columns: 360px minmax(0, 1fr) auto; height: calc(100% - var(--topbar-h)); }
.col-list { border-right: 1px solid var(--border); background: var(--surface); display: flex; flex-direction: column; min-height: 0; }
.list-tools { padding: 10px; border-bottom: 1px solid var(--border); }
.folders { display: flex; gap: 4px; overflow-x: auto; padding-bottom: 8px; scrollbar-width: thin; }
.folders button { border: 0; background: none; padding: 6px 10px; border-radius: 999px; cursor: pointer; white-space: nowrap; color: var(--muted); }
.folders button[aria-selected="true"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.count:not(:empty) { background: var(--accent); color: var(--accent-fg); border-radius: 999px; padding: 0 6px; font-size: 11px; margin-left: 2px; }
.filters { display: grid; grid-template-columns: 1fr 120px; gap: 6px; }
.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.list-msg { padding: 24px 16px; color: var(--muted); text-align: center; }
.conv { display: flex; gap: 10px; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--border); background: none; padding: 10px 12px; cursor: pointer; }
.conv:hover { background: var(--surface-2); }
.conv.active { background: var(--accent-soft); }
.conv.unread .name, .conv.unread .preview { font-weight: 700; color: var(--text); }
.conv-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.row { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.name, .preview { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.name { font-weight: 600; } .preview { color: var(--muted); }
.time { color: var(--muted); font-size: 12px; flex-shrink: 0; }
.badge { background: var(--accent); color: var(--accent-fg); border-radius: 999px; font-size: 11px; min-width: 18px; padding: 0 5px; text-align: center; font-weight: 700; }
.tags { display: flex; gap: 4px; flex-wrap: wrap; }
.tags:empty { display: none; }
.avatar { position: relative; flex-shrink: 0; width: 40px; height: 40px; border-radius: 50%; background: var(--c-gray); display: grid; place-items: center; font-weight: 700; color: var(--muted); }
.avatar.big { width: 64px; height: 64px; font-size: 22px; margin: 8px auto 0; }
.ch { position: absolute; right: -2px; bottom: -2px; width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--surface); }
.list .ch { position: static; display: inline-block; width: 12px; height: 12px; border: 0; }
.ch-whatsapp { background: var(--wa); } .ch-instagram { background: var(--ig); } .ch-messenger { background: var(--ms); }

.col-thread { display: flex; flex-direction: column; min-height: 0; background: var(--bg); }
#thread { display: flex; flex-direction: column; height: 100%; min-height: 0; }
#thread[hidden] { display: none; }
.empty-state { margin: auto; text-align: center; color: var(--muted); }
.empty-icon { font-size: 40px; }
.thread-head { display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: var(--surface); border-bottom: 1px solid var(--border); }
.thread-head select { width: auto; }
.back { display: none; }
.switch { display: inline-flex; align-items: center; gap: 6px; margin: 0; cursor: pointer; font-weight: 500; }
.switch input { width: auto; margin: 0; accent-color: var(--accent); }
.switch.small { font-size: 13px; }
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; }
.loading { color: var(--muted); text-align: center; margin: auto; }
.msg { max-width: min(560px, 82%); padding: 8px 11px; border-radius: 12px; box-shadow: var(--shadow); word-wrap: break-word; }
.msg.in { align-self: flex-start; background: var(--in-bubble); border-bottom-left-radius: 4px; }
.msg.out { align-self: flex-end; background: var(--out-bubble); border-bottom-right-radius: 4px; }
.msg.out.ai { background: var(--ai-bubble); } .msg.out.echo { background: var(--echo-bubble); }
.msg.reaction { box-shadow: none; background: none; padding: 0 4px; }
.msg .who { font-size: 11px; font-weight: 600; color: var(--muted); margin-bottom: 2px; }
.msg img, .msg video { max-width: 100%; max-height: 320px; border-radius: 8px; display: block; margin-bottom: 4px; }
.msg audio { max-width: 100%; }
.msg .meta { font-size: 11px; color: var(--muted); text-align: right; margin-top: 2px; }
.st.read { color: var(--accent); } .st.failed { color: var(--err); font-weight: 600; }
.window-bar { font-size: 12.5px; padding: 6px 14px; border-top: 1px solid var(--border); }
.window-bar.ok { background: var(--ok-soft); color: var(--ok); } .window-bar.warn { background: var(--warn-soft); color: var(--warn); } .window-bar.err { background: var(--err-soft); color: var(--err); }
.composer { position: relative; display: flex; gap: 8px; align-items: flex-end; padding: 10px 12px; background: var(--surface); border-top: 1px solid var(--border); }
.composer textarea { resize: none; max-height: 160px; }
.pop { position: absolute; left: 12px; right: 12px; bottom: 100%; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.pop button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 8px 12px; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pop button:hover, .pop button.sel { background: var(--accent-soft); }

.col-info { width: 300px; border-left: 1px solid var(--border); background: var(--surface); overflow-y: auto; padding: 12px 16px; position: relative; }
.col-info[hidden] { display: none; }
.col-info h2 { font-size: 17px; margin: 8px 0 2px; }
.close-info { position: absolute; right: 8px; top: 8px; }
.field { margin-top: 16px; }
.field > label, .field-label { display: block; font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); margin-bottom: 6px; }
.notes { list-style: none; padding: 0; margin: 0 0 8px; display: flex; flex-direction: column; gap: 6px; }
.notes li { background: var(--warn-soft); border-radius: var(--radius-sm); padding: 6px 8px; }
#note-form .btn { margin-top: 6px; width: 100%; }

dialog { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); width: min(480px, calc(100% - 32px)); padding: 20px; }
dialog::backdrop { background: rgba(0, 0, 0, .4); }
dialog h2 { font-size: 18px; margin: 0 0 6px; }
.tpl-preview { white-space: pre-wrap; background: var(--surface-2); border-radius: var(--radius-sm); padding: 10px; }
.tpl-preview:empty { display: none; }
.actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: var(--radius); box-shadow: var(--shadow); z-index: 20; max-width: calc(100% - 32px); }
.toast.err { background: var(--err); color: #fff; }

/* configuración */
.page { overflow: auto; }
.settings { max-width: 900px; margin: 0 auto; padding: 20px 16px 60px; }
.settings-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.settings-head select { width: auto; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; margin-top: 16px; }
.card h2 { font-size: 16px; margin: 0 0 10px; }
.list { list-style: none; padding: 0; margin: 0 0 10px; }
.list li { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.list li form { margin: 0; }
details summary { cursor: pointer; color: var(--accent); margin: 8px 0; font-weight: 500; }
.grid-form { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; align-items: end; }
.grid-form .full { grid-column: 1 / -1; }
.grid-form .btn { justify-self: start; margin-bottom: 10px; }
.inline { display: flex; gap: 8px; align-items: center; }
.inline input { width: auto; flex: 1; }
.inline select { width: auto; }
.chip-form { display: inline; margin: 0; }
.kv { display: grid; grid-template-columns: 200px 1fr; gap: 6px 12px; margin: 0; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; word-break: break-all; }

/* celular: una columna a la vez */
@media (max-width: 860px) {
  .inbox { grid-template-columns: 1fr; }
  .app[data-view="list"] .col-thread { display: none; }
  .app[data-view="thread"] .col-list { display: none; }
  .back { display: inline-flex; }
  .col-info { position: fixed; inset: var(--topbar-h) 0 0 auto; width: min(340px, 100%); z-index: 10; box-shadow: var(--shadow); }
  .thread-head { flex-wrap: wrap; }
  .grid-form, .kv { grid-template-columns: 1fr; }
}
