:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --surface-2: #f0f2f1;
  --border: #e2e6e4;
  --text: #17201c;
  --muted: #64716b;
  --accent: #128c7e;
  --accent-strong: #0b6b60;
  --accent-soft: #e2f4f0;
  --bubble-out: #d9fdd3;
  --bubble-in: #ffffff;
  --chat-bg: #efeae2;
  --danger: #d03b3b;
  --danger-soft: #fde8e8;
  --warn: #b7791f;
  --warn-soft: #fdf3dc;
  --ok: #15803d;
  --ok-soft: #dcf5e4;
  --shadow: 0 1px 2px rgba(16, 24, 20, .06), 0 4px 16px rgba(16, 24, 20, .06);
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1412; --surface: #151d1a; --surface-2: #1c2622; --border: #26322d; --text: #e6ece9; --muted: #8fa097;
    --accent: #25b39f; --accent-strong: #3fd0bb; --accent-soft: #143430; --bubble-out: #144d37; --bubble-in: #1c2622;
    --chat-bg: #0b100e; --danger: #f26d6d; --danger-soft: #3a1c1c; --warn: #e5b45a; --warn-soft: #3a2f17;
    --ok: #4ade80; --ok-soft: #173525; --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e1412; --surface: #151d1a; --surface-2: #1c2622; --border: #26322d; --text: #e6ece9; --muted: #8fa097;
  --accent: #25b39f; --accent-strong: #3fd0bb; --accent-soft: #143430; --bubble-out: #144d37; --bubble-in: #1c2622;
  --chat-bg: #0b100e; --danger: #f26d6d; --danger-soft: #3a1c1c; --warn: #e5b45a; --warn-soft: #3a2f17;
  --ok: #4ade80; --ok-soft: #173525; --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 22px; font-weight: 650; }
h2 { font-size: 16px; font-weight: 600; }
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; }
pre { background: var(--surface-2); border: 1px solid var(--border); padding: 12px; border-radius: 8px; overflow-x: auto; white-space: pre; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.hidden { display: none !important; }

/* ---------- botões & inputs ---------- */
button, .btn {
  font: inherit; cursor: pointer; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 8px 14px; border-radius: 8px; display: inline-flex; align-items: center; gap: 6px; text-decoration: none;
  transition: background .15s, border-color .15s;
}
button:hover { background: var(--surface-2); }
button:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 550; }
.btn-primary:hover { background: var(--accent-strong); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); }
.btn-sm { padding: 5px 10px; font-size: 13px; }
.btn-ghost { border-color: transparent; background: transparent; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  padding: 9px 11px; border-radius: 8px; width: 100%; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
label { display: block; font-weight: 550; margin-bottom: 4px; font-size: 13px; }
.field { margin-bottom: 14px; }
.checks { display: flex; flex-wrap: wrap; gap: 6px 16px; }
.checks label { display: flex; gap: 6px; align-items: center; font-weight: 400; }
.checks input { width: auto; }

/* ---------- layout ---------- */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.sidebar { background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 16px 12px; gap: 4px; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 15px; padding: 4px 8px 16px; }
.brand-logo { width: 32px; height: 32px; border-radius: 9px; background: var(--accent); display: grid; place-items: center; color: #fff; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px; color: var(--text); text-decoration: none; font-weight: 500; }
.nav a:hover { background: var(--surface-2); }
.nav a.active { background: var(--accent-soft); color: var(--accent-strong); }
.nav svg { width: 18px; height: 18px; flex: none; }
.side-foot { margin-top: auto; border-top: 1px solid var(--border); padding-top: 12px; display: flex; flex-direction: column; gap: 6px; }
.user-chip { padding: 6px 8px; }
.user-chip b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.main { padding: 24px 28px; min-width: 0; }
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; }
.topbar-mobile { display: none; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.card + .card { margin-top: 16px; }
.card-head { display: flex; align-items: center; gap: 8px; margin-bottom: 14px; }
.grid { display: grid; gap: 16px; }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-cards { grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); }

.stat .label { color: var(--muted); font-size: 12.5px; font-weight: 500; }
.stat .value { font-size: 28px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.stat .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
tr:last-child td { border-bottom: none; }
.table-wrap { overflow-x: auto; }

.badge { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.badge.connected, .badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.qr, .badge.connecting, .badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.disconnected, .badge.off { background: var(--danger-soft); color: var(--danger); }
.badge.plain::before { display: none; }

.instance-card .phone { font-size: 13px; color: var(--muted); margin: 4px 0 14px; }
.instance-card .actions { display: flex; gap: 6px; flex-wrap: wrap; }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; }
.login-card .brand { justify-content: center; padding-bottom: 6px; font-size: 18px; }
.login-card p { text-align: center; margin: 0 0 20px; }

/* ---------- modal ---------- */
.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal { background: var(--surface); border-radius: 14px; width: 100%; max-width: 460px; max-height: 92vh; overflow-y: auto; padding: 20px; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal-head { display: flex; align-items: center; margin-bottom: 16px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.qr-box { display: grid; place-items: center; gap: 12px; text-align: center; padding: 8px 0; }
.qr-box img { width: 280px; max-width: 100%; aspect-ratio: 1; background: #fff; padding: 8px; border-radius: 10px; border: 1px solid var(--border); }
.qr-steps { text-align: left; margin: 0; padding-left: 18px; color: var(--muted); font-size: 13px; }
.secret { display: flex; gap: 6px; }
.secret input { font-family: ui-monospace, Menlo, monospace; font-size: 12.5px; }

/* ---------- toasts ---------- */
#toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 100; max-width: calc(100vw - 32px); }
.toast { background: var(--text); color: var(--surface); padding: 10px 14px; border-radius: 10px; box-shadow: var(--shadow); max-width: 360px; animation: in .2s ease-out; }
.toast.error { background: var(--danger); color: #fff; }
@keyframes in { from { opacity: 0; transform: translateY(6px); } }

/* ---------- conversas ---------- */
.chat-layout { display: grid; grid-template-columns: 340px 1fr; height: calc(100vh - 48px); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.chat-side { border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.chat-side-head { padding: 12px; border-bottom: 1px solid var(--border); display: flex; flex-direction: column; gap: 8px; }
.chat-side-head .row { flex-wrap: nowrap; }
.chat-side-head .row input { flex: 1; min-width: 0; }
.chat-side-head .row button { flex: none; }
.filters { display: flex; gap: 4px; flex-wrap: wrap; }
.filters button { padding: 4px 10px; font-size: 12.5px; border-radius: 999px; }
.filters button.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-strong); }
.chat-list { overflow-y: auto; flex: 1; }
.chat-item { display: flex; gap: 10px; padding: 11px 12px; cursor: pointer; border-bottom: 1px solid var(--border); align-items: center; }
.chat-item:hover { background: var(--surface-2); }
.chat-item.active { background: var(--accent-soft); }
.avatar { width: 42px; height: 42px; border-radius: 50%; background: var(--accent-soft); color: var(--accent-strong); display: grid; place-items: center; font-weight: 700; flex: none; overflow: hidden; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.chat-item .meta { flex: 1; min-width: 0; }
.chat-item .top { display: flex; gap: 6px; align-items: baseline; }
.chat-item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.chat-item .time { font-size: 11.5px; color: var(--muted); flex: none; }
.chat-item .last { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.chat-item .bottom { display: flex; gap: 6px; align-items: center; margin-top: 2px; }
.unread { background: var(--accent); color: #fff; font-size: 11px; font-weight: 700; min-width: 20px; height: 20px; border-radius: 10px; display: grid; place-items: center; padding: 0 6px; }

.chat-main { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--chat-bg); }
.chat-main-head { background: var(--surface); padding: 10px 14px; border-bottom: 1px solid var(--border); display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.chat-main-head .title { flex: 1; min-width: 120px; }
.chat-main-head select { width: auto; padding: 5px 8px; font-size: 13px; }
.messages { flex: 1; overflow-y: auto; padding: 16px 6%; display: flex; flex-direction: column; gap: 4px; }
.msg { max-width: min(560px, 80%); padding: 6px 9px 4px; border-radius: 9px; background: var(--bubble-in); box-shadow: 0 1px 1px rgba(0,0,0,.08); align-self: flex-start; word-wrap: break-word; white-space: pre-wrap; }
.msg.out { background: var(--bubble-out); align-self: flex-end; }
.msg .sender { font-size: 12px; font-weight: 600; color: var(--accent-strong); margin-bottom: 2px; }
.msg .foot { font-size: 11px; color: var(--muted); text-align: right; margin-top: 2px; display: flex; gap: 4px; justify-content: flex-end; align-items: center; }
.msg .tick.read { color: #34b7f1; }
.msg img.media, .msg video.media { max-width: 100%; max-height: 320px; border-radius: 6px; display: block; margin-bottom: 4px; }
.msg audio { max-width: 100%; display: block; }
.msg .doc { display: flex; gap: 8px; align-items: center; padding: 8px; background: rgba(0,0,0,.05); border-radius: 6px; margin-bottom: 4px; text-decoration: none; color: inherit; }
.day-sep { align-self: center; background: var(--surface); color: var(--muted); font-size: 12px; padding: 3px 10px; border-radius: 8px; margin: 8px 0; box-shadow: 0 1px 1px rgba(0,0,0,.06); }
.composer { background: var(--surface); padding: 10px 12px; border-top: 1px solid var(--border); display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { resize: none; max-height: 140px; min-height: 40px; }
.composer .file-chip { font-size: 12px; background: var(--accent-soft); color: var(--accent-strong); padding: 4px 8px; border-radius: 6px; }
.empty { display: grid; place-items: center; text-align: center; color: var(--muted); padding: 40px 16px; flex: 1; }
.empty svg { width: 56px; height: 56px; opacity: .5; margin-bottom: 8px; }
.load-more { align-self: center; }
.back-btn { display: none; }

/* ---------- responsivo ---------- */
@media (max-width: 1000px) { .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 820px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 260px; z-index: 40; transform: translateX(-100%); transition: transform .2s; }
  .shell.menu-open .sidebar { transform: none; box-shadow: var(--shadow); }
  .topbar-mobile { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 30; }
  .main { padding: 16px; }
  .chat-layout { grid-template-columns: 1fr; height: calc(100vh - 90px); }
  .chat-layout.has-chat .chat-side { display: none; }
  .chat-layout:not(.has-chat) .chat-main { display: none; }
  .back-btn { display: inline-flex; }
  .messages { padding: 12px; }
  .msg { max-width: 88%; }
}
@media (max-width: 520px) { .grid-4 { grid-template-columns: 1fr 1fr; } .stat .value { font-size: 22px; } }
.grid > .card + .card { margin-top: 0; }

/* lista de grupos (nova conversa) */
.group-list { margin-top: 8px; max-height: 300px; overflow-y: auto; border: 1px solid var(--border); border-radius: 8px; }
.group-item { display: flex; gap: 10px; align-items: center; padding: 9px 10px; cursor: pointer; border-bottom: 1px solid var(--border); }
.group-item:last-child { border-bottom: none; }
.group-item:hover { background: var(--surface-2); }
.group-item.active { background: var(--accent-soft); }
.group-item.disabled { opacity: .5; cursor: not-allowed; }
.group-item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
