:root {
  --bg: #0f1216;
  --panel: #161b22;
  --panel2: #1c232d;
  --line: #262e3a;
  --text: #e8edf2;
  --muted: #8b97a5;
  --accent: #25d366;
  --accent-dim: #128c4b;
  --warn: #ffd166;
  --danger: #f25c5c;
  --blue: #4da3ff;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--text);
  overflow: hidden;
}
button { font: inherit; cursor: pointer; }
input, textarea { font: inherit; }

.hidden { display: none !important; }
.empty { color: var(--muted); text-align: center; padding: 48px 16px; line-height: 1.8; }

/* ---------- flag banner ---------- */
.flag { position: fixed; top: 0; left: 0; right: 0; z-index: 100; background: #3a1216; border-bottom: 1px solid var(--danger); }
.flag-inner { max-width: 1100px; margin: 0 auto; padding: 10px 20px; display: flex; gap: 14px; align-items: center; flex-wrap: wrap; }
.flag strong { color: var(--danger); margin-right: 6px; }
.flag span { color: var(--text); flex: 1; min-width: 260px; }
.flag-actions { display: flex; gap: 8px; }
.flag button { background: var(--danger); color: #fff; border: 0; padding: 6px 14px; border-radius: 8px; }
.flag #flag-ack { background: transparent; border: 1px solid var(--danger); color: var(--danger); }

/* ---------- login ---------- */
.login { height: 100%; display: flex; align-items: center; justify-content: center; }
.login form { background: var(--panel); border: 1px solid var(--line); padding: 36px; border-radius: 16px; display: flex; flex-direction: column; gap: 12px; width: 320px; }
.login h1 { font-size: 20px; margin-bottom: 6px; }
.login input { background: var(--bg); border: 1px solid var(--line); color: var(--text); padding: 10px 12px; border-radius: 8px; }
.login button { background: var(--accent-dim); color: #fff; border: 0; padding: 10px; border-radius: 8px; font-weight: 600; }

/* ---------- layout ---------- */
.app { height: 100%; display: grid; grid-template-columns: 220px 300px 1fr; }

.accounts, .chats { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; }
.pane-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 14px 10px; }
.pane-title { font-weight: 700; font-size: 13px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.pane-head button { background: var(--panel2); border: 1px solid var(--line); color: var(--accent); border-radius: 8px; width: 28px; height: 28px; font-size: 15px; }
.filter { margin: 0 12px 10px; background: var(--bg); border: 1px solid var(--line); color: var(--text); border-radius: 8px; padding: 7px 10px; }

/* ---------- accounts ---------- */
.account-list { flex: 1; overflow-y: auto; }
.account {
  display: flex; align-items: center; gap: 10px; padding: 10px 14px; cursor: pointer;
  border-left: 3px solid transparent; user-select: none;
}
.account:hover { background: var(--panel2); }
.account.active { background: var(--panel2); border-left-color: var(--accent); }
.avatar {
  width: 36px; height: 36px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 15px;
  background: var(--accent-dim); color: #fff;
}
.account .meta { flex: 1; min-width: 0; }
.account .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.account .sub { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dot { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; }
.dot.open { background: var(--accent); box-shadow: 0 0 6px var(--accent); }
.dot.qr { background: var(--warn); animation: blink 1.2s infinite; }
.dot.connecting { background: var(--blue); animation: blink 1.2s infinite; }
.dot.paused, .dot.idle { background: var(--muted); }
.dot.flagged, .dot.error, .dot.relink { background: var(--danger); }
@keyframes blink { 50% { opacity: .35; } }
.account .dot { margin-left: 2px; }
.account .qr-badge { color: var(--warn); font-size: 15px; }

.conn { padding: 10px 14px; border-top: 1px solid var(--line); display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; }
#conn-dot { background: var(--muted); }

/* ---------- chats ---------- */
.chat-list { flex: 1; overflow-y: auto; }
.chat {
  display: flex; align-items: center; gap: 10px; padding: 10px 14px; cursor: pointer; border-left: 3px solid transparent;
}
.chat:hover { background: var(--panel2); }
.chat.active { background: var(--panel2); border-left-color: var(--accent); }
.chat .meta { flex: 1; min-width: 0; }
.chat .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat .last { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat .time { color: var(--muted); font-size: 11px; flex-shrink: 0; align-self: flex-start; margin-top: 3px; }
.badge { background: var(--accent-dim); color: #fff; border-radius: 10px; min-width: 20px; height: 20px; padding: 0 6px; font-size: 11px; display: flex; align-items: center; justify-content: center; }

/* ---------- thread ---------- */
.thread { display: flex; flex-direction: column; background: var(--bg); }
.thread-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 18px; border-bottom: 1px solid var(--line); background: var(--panel); }
.thread-name { font-weight: 700; font-size: 16px; }
.thread-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.thread-actions button.ghost { background: transparent; border: 1px solid var(--line); color: var(--muted); border-radius: 8px; width: 32px; height: 32px; }

.messages { flex: 1; overflow-y: auto; padding: 18px 20px; display: flex; flex-direction: column; gap: 4px; }
.msg { max-width: 68%; padding: 8px 12px; border-radius: 14px; font-size: 14px; position: relative; margin-bottom: 6px; word-wrap: break-word; }
.msg.in { background: var(--panel2); align-self: flex-start; border-bottom-left-radius: 4px; }
.msg.out { background: var(--accent-dim); align-self: flex-end; border-bottom-right-radius: 4px; }
.msg.media img { max-width: 260px; max-height: 220px; border-radius: 8px; display: block; margin-bottom: 4px; }
.msg .meta { display: block; font-size: 11px; color: var(--muted); margin-top: 4px; text-align: right; }
.msg.out .meta { color: #b9e6ca; }
.msg.failed { outline: 1px solid var(--danger); }

.composer { display: flex; gap: 10px; padding: 12px 16px; border-top: 1px solid var(--line); background: var(--panel); }
.composer textarea {
  flex: 1; background: var(--bg); border: 1px solid var(--line); color: var(--text);
  border-radius: 20px; padding: 10px 16px; resize: none; max-height: 120px; outline: none;
}
.composer textarea:focus { border-color: var(--accent-dim); }
.composer .send { background: var(--accent-dim); color: #fff; border: 0; border-radius: 50%; width: 40px; height: 40px; font-size: 16px; }

/* ---------- modals ---------- */
.qr-modal { position: fixed; inset: 0; background: rgba(0,0,0,.7); display: flex; align-items: center; justify-content: center; z-index: 50; }
.qr-card { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 26px; width: 360px; text-align: center; }
.qr-card h2 { font-size: 18px; margin-bottom: 8px; }
.qr-hint { color: var(--muted); font-size: 13px; margin-bottom: 14px; }
.qr-box { background: #fff; border-radius: 12px; padding: 12px; margin-bottom: 14px; min-height: 280px; display: flex; align-items: center; justify-content: center; }
.qr-box img { width: 260px; height: 260px; image-rendering: pixelated; }
.qr-actions { display: flex; gap: 10px; justify-content: center; }
.qr-actions button { background: var(--accent-dim); color: #fff; border: 0; padding: 8px 18px; border-radius: 8px; }
.qr-actions button.ghost { background: var(--panel2); border: 1px solid var(--line); color: var(--text); }
.qr-card input { width: 100%; background: var(--bg); border: 1px solid var(--line); color: var(--text); padding: 10px 12px; border-radius: 8px; margin-bottom: 10px; }