:root {
  --bg: #111b21;
  --panel: #202c33;
  --panel-2: #2a3942;
  --border: #2d3a44;
  --text: #e9edef;
  --muted: #8696a0;
  --accent: #00a884;
  --accent-2: #06cf9c;
  --bubble-out: #005c4b;
  --bubble-in: #202c33;
  --danger: #f15c6d;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}
.hidden { display: none !important; }
button {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
}
button:hover { background: #344048; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { background: var(--accent-2); }
input, textarea, select {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 8px 10px;
  border-radius: 6px;
  outline: none;
  font: inherit;
  width: 100%;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); }
hr { border: 0; border-top: 1px solid var(--border); margin: 12px 0; }

/* Login */
.login {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
}
.login .card {
  background: var(--panel);
  padding: 24px;
  width: min(380px, 90vw);
  border-radius: 8px;
  border: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 12px;
}
.login h1 { margin: 0; }
.login .hint { color: var(--muted); font-size: 12px; margin: 0; }

/* App layout */
/* The two columns scroll independently. Critical: every flex/grid descendant
   that holds a scroll container needs `min-height: 0` so it can shrink below
   its content height — otherwise the whole panel grows and the page itself
   scrolls instead of the inner list. */
.app {
  display: grid;
  grid-template-columns: 360px 1fr;
  height: 100dvh;
  overflow: hidden;
}
.app > .sidebar,
.app > .conversation {
  height: 100%;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}

@media (max-width: 720px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { border-right: 0; }
  .conversation { display: none; }
  body.show-conversation .sidebar { display: none; }
  body.show-conversation .conversation { display: flex; }
}

/* Sidebar */
.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  min-width: 0;
}
.sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.me { display: flex; align-items: center; gap: 10px; min-width: 0; }
.me-meta { display: flex; flex-direction: column; min-width: 0; }
.me-meta strong { font-size: 14px; }
.sidebar-actions { display: flex; gap: 6px; }
.sidebar-actions button { padding: 6px 8px; }

.avatar {
  width: 36px; height: 36px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; color: #00382b;
  flex: none;
  overflow: hidden;
  position: relative;
}
.avatar img {
  width: 100%; height: 100%; object-fit: cover; display: block;
}
.avatar span {
  font-size: 13px;
  text-transform: uppercase;
}

.search { padding: 8px 12px; background: var(--bg); }

.chat-list { flex: 1; min-height: 0; overflow-y: auto; }
.chat-row {
  display: flex; gap: 10px; align-items: center;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  min-width: 0;
}
.chat-row:hover { background: var(--panel-2); }
.chat-row.active { background: var(--panel-2); }
.chat-row .meta { flex: 1; min-width: 0; }
.chat-row .meta .top {
  display: flex; justify-content: space-between; gap: 8px;
}
.chat-row .meta .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-row .meta .time { color: var(--muted); font-size: 11px; flex: none; }
.chat-row .meta .preview {
  color: var(--muted);
  font-size: 12px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chat-row .badge {
  background: var(--accent); color: #fff;
  font-size: 11px; padding: 2px 6px; border-radius: 999px;
}

.new-chat {
  display: flex; gap: 6px; padding: 10px 12px;
  border-top: 1px solid var(--border);
  background: var(--panel);
}
.new-chat input { flex: 1; }

/* Conversation */
.conversation { display: flex; flex-direction: column; min-width: 0; background:
  linear-gradient(rgba(11,20,26,0.92), rgba(11,20,26,0.92)),
  repeating-linear-gradient(45deg, #0b141a 0 8px, #0e1a21 8px 16px);
}
.conv-empty {
  flex: 1; display: flex; align-items: center; justify-content: center;
  color: var(--muted); text-align: center;
}
.conv { flex: 1; display: flex; flex-direction: column; min-height: 0; min-width: 0; height: 100%; }
.conv-header {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.conv-title { display: flex; flex-direction: column; min-width: 0; flex: 1; cursor: pointer; }
.conv-title:hover { opacity: 0.85; }
.conv-title strong { font-size: 14px; }
.conv-title span { color: var(--muted); font-size: 11px; }
.conv-actions { display: flex; gap: 6px; }
.conv-actions button { padding: 6px 8px; }
.back-btn { font-size: 22px; padding: 0 10px; line-height: 1; }
@media (min-width: 721px) { .back-btn { display: none; } }

.messages {
  flex: 1; min-height: 0; overflow-y: auto; padding: 16px;
  display: flex; flex-direction: column; gap: 4px;
  overscroll-behavior: contain;
}
.msg {
  max-width: min(80%, 540px);
  padding: 6px 8px 4px;
  border-radius: 8px;
  word-wrap: break-word;
  position: relative;
  font-size: 14px;
  line-height: 1.35;
}
.msg.in { background: var(--bubble-in); align-self: flex-start; border-top-left-radius: 0; }
.msg.out { background: var(--bubble-out); align-self: flex-end; border-top-right-radius: 0; }
.msg .body { white-space: pre-wrap; }
.msg .sender-name {
  font-size: 12px;
  font-weight: 600;
  margin-bottom: 2px;
  display: block;
}
.msg .ts { color: rgba(255,255,255,0.5); font-size: 10px; margin-left: 8px; float: right; }
.msg img.media { max-width: 100%; border-radius: 6px; display: block; margin: 0 0 4px; }
.msg audio, .msg video { max-width: 100%; display: block; margin: 0 0 4px; }
.msg a.doc {
  display: inline-block; background: rgba(255,255,255,0.08); color: var(--text);
  padding: 6px 8px; border-radius: 6px; text-decoration: none; margin: 4px 0;
}
.day-sep {
  align-self: center;
  background: rgba(0,0,0,0.4);
  color: var(--muted);
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 11px;
  margin: 8px 0;
}

/* Draft tray */
.draft-tray {
  background: var(--panel-2);
  border-top: 1px solid var(--border);
  padding: 8px 10px;
  display: flex; flex-direction: column; gap: 6px;
}
.draft-tray-header {
  display: flex; align-items: center; gap: 8px;
  font-size: 12px; color: var(--accent-2); font-weight: 600;
}
.draft-tray-header > :first-child { flex: 1; }
.draft-tray textarea {
  resize: vertical;
  min-height: 60px;
}
.draft-tray-actions {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.draft-tray-actions small { color: var(--muted); }

/* Send form */
.send-form {
  display: flex; gap: 6px; padding: 8px 10px;
  background: var(--panel);
  border-top: 1px solid var(--border);
}
.send-form input { flex: 1; border-radius: 18px; padding: 10px 14px; }
.send-form button { border-radius: 50%; width: 40px; height: 40px; padding: 0; }

/* Pills */
.pill {
  font-size: 11px; padding: 1px 8px; border-radius: 999px;
  display: inline-block;
}
.pill-connected { background: #056b50; color: #fff; }
.pill-connecting { background: #b8860b; color: #fff; }
.pill-qr_pending { background: #6b3a05; color: #fff; }
.pill-disconnected { background: #6b1c1c; color: #fff; }

/* Account switcher */
.account-switcher {
  position: relative;
  cursor: pointer;
  flex: 1;
  min-width: 0;
}
.account-switcher .me-sub {
  display: flex; align-items: center; gap: 6px; min-width: 0;
}
.account-chev {
  color: var(--muted); font-size: 10px;
}
.account-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: -20px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  z-index: 30;
  max-height: 60vh;
  overflow-y: auto;
  box-shadow: 0 4px 16px rgba(0,0,0,0.4);
}
.account-dropdown .item {
  padding: 8px 10px;
  display: flex; align-items: center; gap: 8px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.account-dropdown .item:last-child { border-bottom: 0; }
.account-dropdown .item:hover { background: var(--panel-2); }
.account-dropdown .item.active { background: var(--panel-2); }
.account-dropdown .item .meta { flex: 1; min-width: 0; }
.account-dropdown .item .meta strong { display: block; font-size: 13px; }
.account-dropdown .item .meta small { color: var(--muted); font-size: 11px; }

/* Settings pane (right-side, replaces conversation/dashboard) */
.settings-pane {
  display: none;
  flex-direction: column;
  background: var(--bg);
  height: 100%;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}
body.show-settings .settings-pane { display: flex; }
body.show-settings .conversation { display: none !important; }
body.show-settings .dashboard { display: none !important; }
@media (max-width: 720px) {
  body.show-settings .sidebar { display: none; }
}

.settings-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.settings-header h2 { margin: 0; font-size: 16px; }
.settings-body {
  flex: 1; min-height: 0; overflow-y: auto;
  padding: 16px;
  display: flex; flex-direction: column; gap: 18px;
}
.settings-section {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
}
.settings-section h3 { margin: 0 0 10px 0; font-size: 14px; color: var(--accent-2); }
.settings-section-head {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 10px;
}
.settings-section-head h3 { margin: 0; }
.settings-row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.settings-row > * { min-width: 0; }
.settings-hint { color: var(--muted); font-size: 12px; margin: 6px 0 0; }
.settings-hint code {
  background: rgba(255,255,255,0.06); padding: 1px 4px; border-radius: 3px;
}

/* Accounts list */
.accounts-list { display: flex; flex-direction: column; gap: 10px; }
.account-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  background: var(--panel-2);
  display: flex; flex-direction: column; gap: 10px;
}
.account-card-head {
  display: flex; align-items: center; gap: 10px;
}
.account-card-head .name { flex: 1; min-width: 0; }
.account-card-head .name strong { display: block; }
.account-card-head .name small { color: var(--muted); font-size: 11px; }
.account-card .row {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
}
.account-card .row label { font-size: 12px; color: var(--muted); }
.account-card .qr-block {
  display: flex; gap: 12px; align-items: flex-start;
  background: var(--panel); padding: 8px; border-radius: 6px;
}
.account-card .qr-block img { max-width: 180px; background: #fff; border-radius: 4px; }
.account-card .qr-block p { color: var(--muted); font-size: 12px; margin: 0; }
.account-card .danger { color: var(--danger); }

/* Toggle switch */
.toggle {
  position: relative; display: inline-block; width: 38px; height: 22px;
}
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle .slider {
  position: absolute; cursor: pointer; inset: 0;
  background: #555; border-radius: 22px; transition: 0.2s;
}
.toggle .slider::before {
  position: absolute; content: '';
  height: 16px; width: 16px;
  left: 3px; top: 3px;
  background: #fff; border-radius: 50%; transition: 0.2s;
}
.toggle input:checked + .slider { background: var(--accent); }
.toggle input:checked + .slider::before { transform: translateX(16px); }

/* API ref */
.api-ref { display: flex; flex-direction: column; gap: 8px; }
.api-card {
  border: 1px solid var(--border); border-radius: 6px;
  background: var(--panel-2);
}
.api-card summary {
  cursor: pointer; padding: 8px 10px;
  font-weight: 600; font-size: 13px;
}
.api-card summary code {
  color: var(--accent-2); background: transparent; padding: 0;
}
.api-card .api-body {
  border-top: 1px solid var(--border);
  padding: 8px 10px; font-size: 12px;
}
.api-card pre {
  background: rgba(0,0,0,0.4); border-radius: 4px;
  padding: 8px; margin: 6px 0 0; overflow-x: auto;
  font-size: 11px; line-height: 1.4;
}

/* Dashboard */
.dashboard {
  display: none;
  flex-direction: column;
  background: var(--bg);
  height: 100%;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}
/* When the dashboard is open it occupies the right pane in place of conversation */
body.show-dashboard .conversation { display: none !important; }
body.show-dashboard .dashboard { display: flex; }
@media (max-width: 720px) {
  body.show-dashboard .sidebar { display: none; }
}

.dashboard-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.dashboard-header h2 { margin: 0; font-size: 16px; }
.dashboard-count {
  color: var(--muted); font-size: 12px; font-weight: 500; margin-left: 6px;
}
.dashboard-actions { display: flex; gap: 6px; }
.dashboard-list {
  flex: 1; min-height: 0; overflow-y: auto;
  padding: 12px;
  display: flex; flex-direction: column; gap: 10px;
}
.dashboard-empty { color: var(--muted); padding: 24px; text-align: center; }
.dash-row {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  display: grid;
  grid-template-columns: 44px 1fr;
  gap: 10px;
  align-items: start;
}
.dash-row .who {
  display: flex; flex-direction: column; gap: 2px;
}
.dash-row .who .name { font-weight: 600; }
.dash-row .who .meta { color: var(--muted); font-size: 11px; }
.dash-row .summary {
  margin-top: 6px;
  padding: 6px 8px;
  background: rgba(0, 168, 132, 0.08);
  border-left: 2px solid var(--accent);
  border-radius: 4px;
  font-size: 13px;
}
.dash-row .preview {
  margin-top: 6px;
  display: flex; flex-direction: column; gap: 2px;
}
.dash-row .preview .line {
  color: var(--muted); font-size: 12px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.dash-row .draft {
  margin-top: 8px;
  display: flex; flex-direction: column; gap: 6px;
}
.dash-row .draft textarea {
  min-height: 56px; resize: vertical;
}
.dash-row .draft-actions {
  display: flex; gap: 6px; flex-wrap: wrap;
}
.dash-row .draft-actions .spacer { flex: 1; }
.dash-row .no-draft {
  margin-top: 8px;
  display: flex; gap: 6px;
}

/* Modal */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 50;
  padding: 16px;
}
.modal-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 18px;
  width: min(420px, 100%);
  display: flex; flex-direction: column; gap: 10px;
  max-height: 90vh; overflow: auto;
}
.modal-card h2 { margin: 0; }
.modal-card img { max-width: 100%; background: #fff; border-radius: 4px; }
