:root {
  --bg: #f4f5f7; --panel: #fff; --line: #e3e5e8; --text: #1b1d21; --muted: #6b7280;
  --accent: #2563eb; --in: #fff; --out: #dbeafe; --err: #b91c1c;
  --wa: #16a34a; --fb: #2563eb; --ig: #c026d3;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0f1115; --panel: #171a20; --line: #2a2e36; --text: #e6e8eb; --muted: #9aa1ab; --in: #222731; --out: #1e3a8a; }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 15px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif; background: var(--bg); color: var(--text); }
[hidden] { display: none !important; }
button { font: inherit; cursor: pointer; border: 0; border-radius: 8px; padding: .55em 1em; background: var(--accent); color: #fff; }
button.icon { background: none; color: var(--text); font-size: 1.3em; padding: .2em .5em; }
button.link { background: none; color: var(--muted); text-decoration: underline; padding: 0; margin-top: 1em; }
input, textarea, select { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: .55em .7em; width: 100%; }
.muted { color: var(--muted); }
.error { color: var(--err); }

.login { display: grid; place-items: center; min-height: 100%; padding: 1em; }
.login form { background: var(--panel); padding: 2em; border-radius: 14px; width: min(360px, 100%); display: grid; gap: .8em; }
.login h1 { margin: 0; }

.app { display: grid; grid-template-columns: 340px 1fr; height: 100dvh; }
.bar { display: flex; align-items: center; justify-content: space-between; gap: .5em; padding: .7em 1em; border-bottom: 1px solid var(--line); background: var(--panel); min-height: 56px; }
.bar h1 { font-size: 1.2em; margin: 0; }
.list-pane { display: flex; flex-direction: column; border-right: 1px solid var(--line); background: var(--panel); min-height: 0; }
.tabs { display: flex; gap: .3em; padding: .5em; overflow-x: auto; border-bottom: 1px solid var(--line); }
.tabs button { background: none; color: var(--muted); padding: .35em .75em; white-space: nowrap; }
.tabs button.active { background: var(--bg); color: var(--text); font-weight: 600; }
.conversations { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conversations li { display: grid; grid-template-columns: auto 1fr auto; gap: .2em .6em; padding: .75em 1em; border-bottom: 1px solid var(--line); cursor: pointer; }
.conversations li.active { background: var(--bg); }
.conversations .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conversations .snippet { grid-column: 2 / 4; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conversations .time { color: var(--muted); font-size: .8em; }
.unread { background: var(--accent); color: #fff; border-radius: 999px; font-size: .75em; padding: 0 .5em; margin-left: .4em; }
.empty { padding: 1.5em 1em; }

.badge { display: inline-block; width: 1.6em; height: 1.6em; border-radius: 50%; color: #fff; font-size: .75em; font-weight: 700; text-align: center; line-height: 1.6em; grid-row: 1 / 3; align-self: center; }
.badge.whatsapp { background: var(--wa); } .badge.messenger { background: var(--fb); } .badge.instagram { background: var(--ig); }

.thread-pane { display: flex; flex-direction: column; min-height: 0; }
.thread-pane .who { display: flex; align-items: center; gap: .5em; flex: 1; }
.back { display: none; }
.messages { flex: 1; overflow-y: auto; padding: 1em; display: flex; flex-direction: column; gap: .4em; }
.msg { max-width: min(75%, 560px); padding: .5em .8em; border-radius: 12px; background: var(--in); white-space: pre-wrap; word-wrap: break-word; box-shadow: 0 1px 1px rgb(0 0 0 / .06); }
.msg.out { align-self: flex-end; background: var(--out); }
.msg .meta { display: block; font-size: .72em; color: var(--muted); margin-top: .2em; text-align: right; }
.msg.failed { outline: 1px solid var(--err); }
.msg .err { color: var(--err); }
.window-note { margin: 0; padding: .5em 1em; font-size: .85em; background: #fef3c7; color: #78350f; }
.composer { display: flex; gap: .5em; padding: .6em; border-top: 1px solid var(--line); background: var(--panel); padding-bottom: max(.6em, env(safe-area-inset-bottom)); }
.composer textarea { resize: none; max-height: 8em; }
.placeholder { margin: auto; }
.thread-pane:not(.open) .messages, .thread-pane:not(.open) .composer, .thread-pane:not(.open) .bar, .thread-pane:not(.open) .window-note { display: none; }
.thread-pane.open .placeholder { display: none; }

dialog { border: 0; border-radius: 14px; padding: 1.2em 1.4em; width: min(560px, 96vw); max-height: 92dvh; background: var(--panel); color: var(--text); }
dialog::backdrop { background: rgb(0 0 0 / .4); }
dialog h2 { margin-top: 0; }
dialog section { border-top: 1px solid var(--line); padding: .6em 0; display: grid; gap: .5em; }
dialog label { display: grid; gap: .2em; font-size: .9em; }
.close-row { position: absolute; right: .6em; top: .6em; }
.sim { display: grid; gap: .5em; }
.status-on { color: var(--wa); } .status-off { color: var(--muted); }

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .thread-pane { display: none; }
  .app.thread .list-pane { display: none; }
  .app.thread .thread-pane { display: flex; }
  .back { display: inline-block; }
}
