:root {
  --bg: #f3f5f7;
  --panel: #ffffff;
  --line: #e2e6ea;
  --text: #1c2430;
  --muted: #6b7684;
  --brand: #128c7e;
  --brand-2: #25d366;
  --danger: #d64545;
  --warn: #d9880f;
  --info: #2f6fed;
  --side: #111b21;
  --side-text: #cfd8dc;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 14px rgba(0,0,0,.05);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--text); background: var(--bg); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
h1 { font-size: 22px; margin: 0; }
h2 { font-size: 15px; margin: 22px 0 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
a { color: var(--brand); }
input, select, textarea { font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--text); min-width: 0; }
input:focus, select:focus { outline: 2px solid #b9e6d9; border-color: var(--brand); }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); margin-bottom: 10px; }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
textarea { resize: vertical; font: inherit; }
fieldset { border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px 4px; margin: 0 0 12px; }
legend { font-size: 12px; color: var(--muted); padding: 0 6px; }
.events { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 2px 10px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.groups-list { max-height: 220px; overflow: auto; border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; margin-bottom: 8px; }
.groups-list:empty { display: none; }
.groups-list label.check { margin: 2px 0; }
.groups-list small { color: var(--muted); margin-left: auto; }
.agent { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 8px; background: #fafbfc; }
.agent .agent-row { display: grid; grid-template-columns: 1.2fr 1fr 1fr auto; gap: 8px; align-items: end; }
.agent .agent-row label { margin: 0; }
.agent .agent-groups { display: flex; flex-wrap: wrap; gap: 4px 12px; margin-top: 8px; }
.agent .agent-groups label.check { margin: 0; font-size: 13px; }
.agent .agent-foot { display: flex; gap: 8px; align-items: center; margin-top: 8px; flex-wrap: wrap; }
.agent code.cmd { font-size: 11px; background: #fff; padding: 4px 8px; border-radius: 6px; border: 1px solid var(--line); word-break: break-all; flex: 1; }
@media (max-width: 700px) { .agent .agent-row { grid-template-columns: 1fr 1fr; } }

.btn { font: inherit; padding: 8px 14px; border-radius: 8px; border: 1px solid var(--line); background: #fff; color: var(--text); cursor: pointer; white-space: nowrap; }
.btn:hover { background: #f5f7f9; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: #0f7a6e; }
.btn.danger { color: var(--danger); border-color: #f0c9c9; }
.btn.danger:hover { background: #fdf3f3; }
.btn.warn { color: var(--warn); border-color: #f2ddb9; }
.btn.small { padding: 5px 10px; font-size: 12px; }
.btn.link { background: none; border: none; color: var(--side-text); padding: 4px; }
.btn:disabled { opacity: .5; cursor: default; }
.row { display: flex; gap: 8px; align-items: center; }
.row.end { justify-content: flex-end; margin-top: 14px; }
.row.wrap { flex-wrap: wrap; margin-bottom: 10px; }
.error { color: var(--danger); font-size: 13px; min-height: 18px; margin-top: 6px; }

/* ---- login ---- */
.login { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; background: var(--side); }
.login-box { background: var(--panel); padding: 28px; border-radius: 14px; width: 340px; box-shadow: var(--shadow); }
.login-box .logo { color: var(--text); margin-bottom: 4px; }
.login-box .btn { width: 100%; margin-top: 6px; }
.logo { font-weight: 700; font-size: 18px; display: flex; align-items: center; gap: 8px; color: #fff; }
.logo .dot { width: 12px; height: 12px; border-radius: 50%; background: var(--brand-2); display: inline-block; }

/* ---- layout ---- */
.app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar { background: var(--side); color: var(--side-text); padding: 18px 14px; display: flex; flex-direction: column; gap: 18px; position: sticky; top: 0; height: 100vh; }
.sidebar nav { display: flex; flex-direction: column; gap: 4px; }
.sidebar nav a { color: var(--side-text); text-decoration: none; padding: 9px 12px; border-radius: 8px; }
.sidebar nav a:hover { background: rgba(255,255,255,.06); }
.sidebar nav a.active { background: var(--brand); color: #fff; }
.sidebar-foot { margin-top: auto; display: flex; align-items: center; justify-content: space-between; }
.conn-dot { width: 10px; height: 10px; border-radius: 50%; background: #666; display: inline-block; }
.conn-dot.on { background: var(--brand-2); }
.content { padding: 22px 26px; max-width: 1400px; }
.view { display: none; }
.view.active { display: block; }
.view-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }

/* ---- sessions ---- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 14px; }
.card { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.card-name { font-weight: 700; font-size: 16px; }
.card-meta { color: var(--muted); font-size: 13px; display: flex; flex-direction: column; gap: 2px; }
.card-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.badge { padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; background: #e6e9ec; color: #444; }
.badge.connected { background: #d9f5e5; color: #157347; }
.badge.qrcode { background: #fff1d6; color: #9a5b00; }
.badge.starting, .badge.pairing { background: #dfe9ff; color: #1f4fc2; }
.badge.disconnected, .badge.error { background: #fbe1e1; color: #a12b2b; }
.badge.logged_out { background: #eee; color: #666; }

/* ---- modal ---- */
.modal { position: fixed; inset: 0; background: rgba(10, 20, 30, .55); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px; }
.modal-box { background: var(--panel); border-radius: 14px; padding: 22px; width: 100%; max-width: 560px; max-height: 92vh; overflow: auto; box-shadow: var(--shadow); }
.modal-box h2 { margin: 0 0 14px; font-size: 18px; color: var(--text); text-transform: none; letter-spacing: 0; }
.modal-box.center { text-align: center; max-width: 440px; }
#qrArea img { width: 300px; height: 300px; image-rendering: pixelated; border: 1px solid var(--line); border-radius: 8px; }
.spinner { width: 34px; height: 34px; border: 4px solid #e4e7ea; border-top-color: var(--brand); border-radius: 50%; margin: 30px auto 10px; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- conversas ---- */
.conv { display: grid; grid-template-columns: 320px 1fr; gap: 14px; height: calc(100vh - 120px); min-height: 420px; }
.conv-list { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); overflow: auto; }
.chat-item { padding: 10px 14px; border-bottom: 1px solid var(--line); cursor: pointer; }
.chat-item:hover { background: #f7f9fa; }
.chat-item.active { background: #e8f6f1; }
.chat-item .name { font-weight: 600; display: flex; justify-content: space-between; gap: 8px; }
.chat-item .name small { color: var(--muted); font-weight: 400; }
.chat-item .last { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-pane { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); display: flex; flex-direction: column; overflow: hidden; }
.conv-title { padding: 12px 16px; border-bottom: 1px solid var(--line); font-weight: 600; }
.conv-messages { flex: 1; overflow: auto; padding: 14px; background: #efe7dd; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 72%; padding: 7px 10px; border-radius: 10px; background: #fff; box-shadow: 0 1px 1px rgba(0,0,0,.08); font-size: 13.5px; white-space: pre-wrap; word-break: break-word; }
.msg.me { align-self: flex-end; background: #d9fdd3; }
.msg .who { font-size: 11px; color: var(--brand); font-weight: 600; margin-bottom: 2px; }
.msg .time { font-size: 10px; color: var(--muted); text-align: right; margin-top: 3px; }
.msg .media { font-size: 12px; color: var(--info); }
.msg img { max-width: 260px; border-radius: 6px; display: block; margin-bottom: 4px; }
.conv-composer { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--line); }
.conv-composer input { flex: 1; }

/* ---- tables ---- */
.table-wrap { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); overflow: auto; max-height: 420px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; white-space: nowrap; }
td.wrap { white-space: normal; max-width: 420px; }
th { position: sticky; top: 0; background: #fafbfc; color: var(--muted); font-weight: 600; font-size: 12px; }
tr.err td { background: #fff6f6; }
.cards-row { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }
.stat { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; }
.stat .k { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.stat .v { font-size: 22px; font-weight: 700; margin-top: 4px; }
.stat .s { color: var(--muted); font-size: 12px; margin-top: 2px; white-space: pre-line; }
pre.log { background: #0f171c; color: #cfe3d6; padding: 14px; border-radius: var(--radius); max-height: 380px; overflow: auto; font-size: 12px; white-space: pre-wrap; }
.doc { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px 24px; line-height: 1.6; }
.doc h3 { margin: 18px 0 6px; font-size: 15px; }
.doc code { background: #f1f3f5; padding: 1px 6px; border-radius: 5px; font-size: 12.5px; }
.doc pre { background: #0f171c; color: #d9e6df; padding: 12px; border-radius: 8px; overflow: auto; font-size: 12.5px; }
.doc table td, .doc table th { white-space: normal; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #1c2430; color: #fff; padding: 10px 18px; border-radius: 10px; z-index: 99; box-shadow: var(--shadow); }
.toast.err { background: var(--danger); }

@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; align-items: center; flex-wrap: wrap; gap: 10px; }
  .sidebar nav { flex-direction: row; flex-wrap: wrap; }
  .sidebar-foot { margin-left: auto; }
  .content { padding: 14px; }
  .conv { grid-template-columns: 1fr; height: auto; }
  .conv-list { max-height: 260px; }
  .conv-messages { min-height: 320px; }
  .two { grid-template-columns: 1fr; }
}
