:root {
  --bg: #f3f5f4;
  --surface: #ffffff;
  --surface-2: #f7f9f8;
  --line: #e2e7e5;
  --text: #13201b;
  --muted: #64736d;
  --accent: #1fa463;
  --accent-ink: #ffffff;
  --accent-soft: #e3f5eb;
  --warn: #c98a12;
  --warn-soft: #fbf1db;
  --danger: #d0453a;
  --bubble-in: #ffffff;
  --bubble-out: #d9f7e3;
  --chat-bg: #eef1ef;
  --shadow: 0 1px 2px rgba(16, 32, 26, .06), 0 4px 14px rgba(16, 32, 26, .05);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1315;
    --surface: #151d20;
    --surface-2: #1a2427;
    --line: #26323a;
    --text: #e5ece9;
    --muted: #8a9a95;
    --accent: #2fbf78;
    --accent-ink: #06210f;
    --accent-soft: #163327;
    --warn: #e6b04a;
    --warn-soft: #33290f;
    --danger: #ef6a5f;
    --bubble-in: #1d282c;
    --bubble-out: #144233;
    --chat-bg: #10181b;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 18px rgba(0, 0, 0, .22);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  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;
}
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 20px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; letter-spacing: -.01em; }
.logo { display: grid; grid-template-columns: repeat(2, 9px); gap: 3px; }
.logo i { width: 9px; height: 9px; border-radius: 3px; background: var(--accent); }
.logo i:last-child { opacity: .45; }
.brand-name { font-size: 16px; }
.top-stats { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.top-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }

.pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px; border-radius: 999px;
  font-size: 12px; font-weight: 550;
  background: var(--surface-2); border: 1px solid var(--line);
}
.pill .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }
.pill-on { background: var(--accent-soft); border-color: transparent; color: var(--accent); }
.pill-on .dot { background: var(--accent); }
.pill-off .dot { background: var(--danger); }
.pill-demo { background: var(--warn-soft); border-color: transparent; color: var(--warn); }
.pill-demo .dot { background: var(--warn); }
.stat { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.stat-unread { color: var(--accent); font-weight: 600; }

.search {
  height: 34px; padding: 0 12px; min-width: 0; width: 200px;
  border-radius: 10px; border: 1px solid var(--line); background: var(--surface-2);
  outline: none;
}
.search:focus { border-color: var(--accent); }
.search-full { width: auto; margin: 0 12px 8px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 14px; border-radius: 10px;
  border: 1px solid transparent; cursor: pointer; font-weight: 550; white-space: nowrap;
}
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { filter: brightness(1.06); }
.btn-ghost { background: transparent; border-color: var(--line); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-sm { height: 28px; padding: 0 10px; font-size: 12px; border-radius: 8px; }
.icon-btn {
  display: inline-grid; place-items: center; width: 34px; height: 34px; flex: none;
  border: 0; border-radius: 10px; background: transparent; cursor: pointer; color: var(--muted);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- banner ---------- */
.banner {
  display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
  margin: 16px 20px 0; padding: 14px 16px;
  border-radius: var(--radius); background: var(--warn-soft); color: var(--text);
}
.banner strong { margin-right: 6px; }
.banner-actions { display: flex; gap: 8px; }

/* ---------- grid of accounts ---------- */
.grid {
  display: grid; gap: 14px; padding: 16px 20px 28px;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
}
.card {
  display: flex; flex-direction: column; min-height: 300px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden;
}
.card.is-open { cursor: pointer; }
.card.is-open:hover { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.card-head {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 10px 10px 12px; border-bottom: 1px solid var(--line);
}
.num {
  display: grid; place-items: center; flex: none;
  width: 28px; height: 28px; border-radius: 8px;
  background: var(--surface-2); color: var(--muted);
  font-weight: 700; font-size: 12px; font-variant-numeric: tabular-nums;
}
.is-open .num { background: var(--accent); color: var(--accent-ink); }
.card-title { min-width: 0; flex: 1; }
.card-title b { display: block; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-title span { display: block; font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 999px;
  display: inline-grid; place-items: center;
  background: var(--accent); color: var(--accent-ink); font-size: 11px; font-weight: 700;
}
.status-tag { font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.status-tag.qr { background: var(--warn-soft); color: var(--warn); }
.status-tag.connecting { background: var(--surface-2); color: var(--muted); }
.status-tag.error { color: var(--danger); }

.menu-wrap { position: relative; }
.menu {
  position: absolute; right: 0; top: 38px; z-index: 3; min-width: 160px;
  padding: 6px; border-radius: 12px; background: var(--surface);
  border: 1px solid var(--line); box-shadow: var(--shadow);
}
.menu button {
  display: block; width: 100%; text-align: left; padding: 8px 10px;
  border: 0; border-radius: 8px; background: transparent; cursor: pointer;
}
.menu button:hover { background: var(--surface-2); }
.menu .danger { color: var(--danger); }

.card-body { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.center {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 10px; padding: 18px; text-align: center;
}
.center p { margin: 0; color: var(--muted); font-size: 13px; }
.center .note { color: var(--warn); font-size: 12px; }
.empty-ico {
  width: 46px; height: 46px; border-radius: 14px; display: grid; place-items: center;
  background: var(--surface-2); color: var(--muted);
}
.qr-img {
  width: 176px; height: 176px; border-radius: 10px; background: #fff; padding: 6px;
  image-rendering: pixelated;
}
.qr-help { font-size: 12px; color: var(--muted); line-height: 1.4; }
.qr-help b { color: var(--text); font-weight: 600; }
.spinner {
  width: 28px; height: 28px; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.mini-chats { list-style: none; margin: 0; padding: 4px 0; }
.mini-chats li { display: flex; align-items: center; gap: 10px; padding: 7px 12px; }
.mini-chats .mc-main { min-width: 0; flex: 1; }
.mini-chats .mc-top { display: flex; justify-content: space-between; gap: 8px; }
.mini-chats .mc-name { font-weight: 600; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mini-chats .mc-time { font-size: 11px; color: var(--muted); flex: none; }
.mini-chats .mc-last { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mini-chats .unread .mc-time { color: var(--accent); font-weight: 600; }
.card-foot {
  margin-top: auto; padding: 8px 12px; border-top: 1px solid var(--line);
  display: flex; justify-content: space-between; align-items: center; font-size: 12px; color: var(--muted);
}
.card-foot .open-link { color: var(--accent); font-weight: 600; }

.avatar {
  flex: none; width: 34px; height: 34px; border-radius: 50%;
  display: grid; place-items: center; font-size: 13px; font-weight: 650;
  background: var(--accent-soft); color: var(--accent);
}
.avatar.sm { width: 28px; height: 28px; font-size: 11px; }

/* ---------- full panel ---------- */
.panel {
  position: fixed; inset: 0; z-index: 10;
  display: grid; grid-template-columns: minmax(280px, 360px) 1fr;
  background: var(--bg);
}
.panel-side { display: flex; flex-direction: column; min-height: 0; background: var(--surface); border-right: 1px solid var(--line); }
.panel-head { display: flex; align-items: center; gap: 8px; padding: 10px 12px; }
.panel-title { min-width: 0; flex: 1; }
.panel-title > div:first-child { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.switch {
  height: 32px; max-width: 130px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--surface-2); padding: 0 6px; font-size: 12px;
}
.chat-list { flex: 1; overflow-y: auto; }
.chat-row {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 10px 12px; border: 0; background: transparent; text-align: left; cursor: pointer;
  border-top: 1px solid color-mix(in srgb, var(--line) 60%, transparent);
}
.chat-row:hover { background: var(--surface-2); }
.chat-row.active { background: var(--accent-soft); }
.chat-row .cr-main { min-width: 0; flex: 1; }
.chat-row .cr-top { display: flex; justify-content: space-between; gap: 8px; }
.chat-row .cr-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-row .cr-time { font-size: 11px; color: var(--muted); flex: none; }
.chat-row .cr-bottom { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.chat-row .cr-last { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-row.unread .cr-time { color: var(--accent); font-weight: 600; }
.list-empty { padding: 30px 16px; text-align: center; color: var(--muted); }

.panel-main { display: flex; min-height: 0; background: var(--chat-bg); }
.conv-empty { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--muted); }
.conv-empty-art {
  width: 72px; height: 72px; border-radius: 20px; margin-bottom: 8px;
  background:
    radial-gradient(circle at 30% 35%, var(--accent) 0 9px, transparent 10px),
    radial-gradient(circle at 70% 65%, color-mix(in srgb, var(--accent) 50%, transparent) 0 9px, transparent 10px),
    var(--surface);
  border: 1px solid var(--line);
}
.conv { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.conv-head {
  display: flex; align-items: center; gap: 10px; padding: 10px 16px;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.conv-name { font-weight: 650; }
.messages { flex: 1; overflow-y: auto; padding: 16px 6%; display: flex; flex-direction: column; gap: 4px; }
.day-sep { align-self: center; margin: 10px 0 6px; font-size: 11px; padding: 3px 10px; border-radius: 8px; background: var(--surface); color: var(--muted); box-shadow: var(--shadow); }
.bubble {
  max-width: min(560px, 78%); padding: 6px 10px 4px; border-radius: 10px;
  background: var(--bubble-in); box-shadow: 0 1px 1px rgba(0,0,0,.06);
  white-space: pre-wrap; overflow-wrap: anywhere; align-self: flex-start;
}
.bubble.out { background: var(--bubble-out); align-self: flex-end; }
.bubble .sender { display: block; font-size: 12px; font-weight: 650; color: var(--accent); margin-bottom: 1px; }
.bubble .meta { display: block; text-align: right; font-size: 10.5px; color: var(--muted); margin-top: 1px; }
.bubble .tick { margin-left: 3px; }
.bubble .tick.read { color: #3aa0e8; }
.composer {
  display: flex; align-items: flex-end; gap: 8px; padding: 10px 14px;
  background: var(--surface); border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1; resize: none; max-height: 140px; padding: 9px 12px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface-2); outline: none;
}
.composer textarea:focus { border-color: var(--accent); }
.send-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 0; cursor: pointer;
  display: grid; place-items: center; background: var(--accent); color: var(--accent-ink);
}
.send-btn:disabled { opacity: .5; cursor: default; }
.only-mobile { display: none; }

/* ---------- dialogs, toast ---------- */
.modal {
  width: min(440px, calc(100vw - 32px)); padding: 22px; border-radius: 16px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
.modal::backdrop { background: rgba(5, 12, 10, .45); }
.modal h2 { margin: 0 0 6px; font-size: 18px; }
.modal p { margin: 0 0 14px; font-size: 13px; }
.modal label { display: block; margin-bottom: 12px; font-size: 12px; font-weight: 600; color: var(--muted); }
.modal input {
  display: block; width: 100%; margin-top: 5px; height: 38px; padding: 0 12px;
  border-radius: 10px; border: 1px solid var(--line); background: var(--surface-2); color: var(--text); outline: none;
  font-weight: 400; font-size: 14px;
}
.modal input:focus { border-color: var(--accent); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.toast {
  position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%); z-index: 50;
  padding: 10px 16px; border-radius: 12px; background: var(--text); color: var(--bg);
  font-size: 13px; box-shadow: var(--shadow); max-width: calc(100vw - 32px);
}

/* ---------- small screens ---------- */
@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; padding: 10px 16px; gap: 10px; }
  .top-actions { width: 100%; margin-left: 0; }
  .top-actions .search { flex: 1; width: auto; }
  .btn-ghost span { display: none; }
  .grid { padding: 12px 16px 24px; grid-template-columns: 1fr; }
  .banner { margin: 12px 16px 0; }
  .panel { grid-template-columns: 1fr; }
  .panel.show-conv .panel-side { display: none; }
  .panel:not(.show-conv) .panel-main { display: none; }
  .only-mobile { display: inline-grid; }
  .messages { padding: 12px; }
}
