:root {
  --bg: #0b141a;
  --panel: #202c33;
  --panel-alt: #2a3942;
  --panel-hover: #313f48;
  --border: #2f3b43;
  --text: #e9edef;
  --text-dim: #8696a0;
  --accent: #00a884;
  --accent-dim: #06231d;
  --bubble-out: #005c4b;
  --bubble-in: #202c33;
  --danger: #f15c6d;
  --warning: #e2b93b;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  --radius: 12px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
}

body {
  margin: 0;
  font-family: 'Inter', -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #3a4750; border-radius: 8px; }
::-webkit-scrollbar-thumb:hover { background: #47555f; }

#app {
  display: grid;
  grid-template-columns: 78px 340px 1fr;
  height: 100vh;
}

/* ---- Accounts rail ---- */
#accounts-rail {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 18px 0;
  gap: 12px;
  overflow-y: auto;
}

#brand {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), #027a63);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1.1rem;
  color: #04241d;
  margin-bottom: 6px;
  flex-shrink: 0;
}

#accounts-list {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  width: 100%;
}

.account-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 1rem;
  cursor: pointer;
  position: relative;
  border: 2px solid transparent;
  flex-shrink: 0;
  transition: transform 0.15s ease, border-color 0.15s ease;
}
.account-avatar:hover { transform: scale(1.06); }

.account-avatar.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-dim);
}

.account-avatar .status-dot {
  position: absolute;
  bottom: -1px;
  right: -1px;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  border: 2px solid var(--panel);
  background: var(--text-dim);
  transition: background 0.2s ease;
}

.account-avatar .status-dot.connected { background: var(--accent); }
.account-avatar .status-dot.connecting { background: var(--warning); animation: pulse 1.4s infinite; }
.account-avatar .status-dot.disconnected { background: var(--danger); }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

.account-avatar .remove-btn {
  position: absolute;
  top: -4px;
  right: -4px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--danger);
  color: #fff;
  font-size: 12px;
  line-height: 18px;
  text-align: center;
  display: none;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
}
.account-avatar:hover .remove-btn { display: block; }

#add-account-btn {
  margin-top: auto;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: 1px dashed #45535c;
  background: transparent;
  color: var(--text-dim);
  font-size: 1.4rem;
  cursor: pointer;
  flex-shrink: 0;
  transition: all 0.15s ease;
}
#add-account-btn:hover { background: var(--panel-alt); color: var(--text); border-style: solid; }

/* ---- Chat list panel ---- */
#chat-list-panel {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.panel-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

#active-account-label {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  min-width: 0;
}
#active-account-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status-pill {
  font-size: 0.68rem;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  flex-shrink: 0;
}
.status-pill.connected { background: var(--accent-dim); color: var(--accent); }
.status-pill.connecting { background: #3a341a; color: var(--warning); }
.status-pill.disconnected { background: #3a1f22; color: var(--danger); }

.icon-btn {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: none;
  background: var(--panel-alt);
  color: var(--text);
  font-size: 1rem;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s ease;
}
.icon-btn:hover { background: var(--panel-hover); }

.search-box { padding: 10px 16px; border-bottom: 1px solid var(--border); }
.search-box input {
  width: 100%;
  padding: 9px 14px;
  border-radius: 20px;
  border: none;
  background: var(--panel-alt);
  color: var(--text);
  font-size: 0.88rem;
  outline: none;
  transition: box-shadow 0.15s ease;
}
.search-box input:focus { box-shadow: 0 0 0 2px var(--accent-dim); }

#chat-list { overflow-y: auto; flex: 1; }

.chat-row {
  display: flex;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  align-items: center;
  transition: background 0.12s ease;
}
.chat-row:hover { background: var(--panel-alt); }
.chat-row.active { background: var(--panel-hover); }

.chat-avatar, .conversation-avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  flex-shrink: 0;
  font-size: 0.95rem;
}

.chat-meta { flex: 1; min-width: 0; }
.chat-name { font-size: 0.95rem; margin-bottom: 2px; }
.chat-preview {
  color: var(--text-dim);
  font-size: 0.82rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-side { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; flex-shrink: 0; }
.chat-time { font-size: 0.7rem; color: var(--text-dim); }

.unread-badge {
  background: var(--accent);
  color: #062f27;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 2px 7px;
  min-width: 20px;
  text-align: center;
}

.empty-hint {
  color: var(--text-dim);
  padding: 32px 16px;
  font-size: 0.88rem;
  text-align: center;
  line-height: 1.5;
}

/* ---- Conversation panel ---- */
#conversation-panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
  background-image: radial-gradient(circle at 25% 15%, rgba(0, 168, 132, 0.05), transparent 45%);
}

#empty-state {
  margin: auto;
  color: var(--text-dim);
  text-align: center;
}
.empty-icon { font-size: 3rem; margin-bottom: 10px; opacity: 0.6; }

#conversation {
  display: flex;
  flex-direction: column;
  height: 100%;
}
#conversation.hidden, #empty-state.hidden { display: none; }

.conversation-header {
  padding: 12px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: var(--shadow);
  z-index: 1;
}

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.date-divider {
  align-self: center;
  background: var(--panel);
  color: var(--text-dim);
  font-size: 0.72rem;
  font-weight: 600;
  padding: 4px 12px;
  border-radius: 8px;
  margin: 14px 0 10px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.bubble {
  max-width: 60%;
  padding: 8px 12px;
  border-radius: var(--radius);
  font-size: 0.92rem;
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-word;
  margin: 3px 0;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
  animation: pop-in 0.12s ease;
}

@keyframes pop-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.bubble.in {
  background: var(--bubble-in);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
}
.bubble.out {
  background: var(--bubble-out);
  align-self: flex-end;
  border-bottom-right-radius: 4px;
}

.bubble .time {
  display: block;
  margin-top: 4px;
  font-size: 0.68rem;
  color: var(--text-dim);
  text-align: right;
}

#send-form {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  background: var(--panel);
  border-top: 1px solid var(--border);
}

#message-input {
  flex: 1;
  padding: 11px 16px;
  border-radius: 22px;
  border: none;
  background: var(--panel-alt);
  color: var(--text);
  font-size: 0.95rem;
  outline: none;
  transition: box-shadow 0.15s ease;
}
#message-input:focus { box-shadow: 0 0 0 2px var(--accent-dim); }

.send-btn {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #062f27;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
  font-size: 1.1rem;
  transition: filter 0.15s ease, transform 0.1s ease;
}
.send-btn:hover { filter: brightness(1.08); }
.send-btn:active { transform: scale(0.94); }

/* ---- Modals ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  animation: fade-in 0.15s ease;
}
.modal-overlay.hidden { display: none; }

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

.modal-box {
  background: var(--panel);
  border-radius: 16px;
  padding: 28px;
  width: 340px;
  text-align: center;
  position: relative;
  box-shadow: var(--shadow);
  animation: scale-in 0.15s ease;
}

@keyframes scale-in {
  from { opacity: 0; transform: scale(0.96); }
  to { opacity: 1; transform: scale(1); }
}

.modal-box h3 { margin-top: 0; }

.modal-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: none;
  background: var(--panel-alt);
  color: var(--text-dim);
  font-size: 1rem;
  cursor: pointer;
}
.modal-close:hover { background: var(--panel-hover); color: var(--text); }

.modal-box input,
.modal-box textarea {
  width: 100%;
  padding: 11px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--panel-alt);
  color: var(--text);
  font-size: 0.95rem;
  margin-bottom: 12px;
  font-family: inherit;
  outline: none;
  resize: vertical;
}
.modal-box input:focus, .modal-box textarea:focus { box-shadow: 0 0 0 2px var(--accent-dim); }

.modal-box img {
  width: 220px;
  height: 220px;
  border-radius: 10px;
  background: #fff;
  padding: 10px;
}

.modal-box button[type="submit"], .modal-box > button:not(.modal-close) {
  padding: 10px 20px;
  border-radius: 10px;
  border: none;
  background: var(--accent);
  color: #062f27;
  font-weight: 600;
  cursor: pointer;
  font-size: 0.95rem;
  transition: filter 0.15s ease;
}
.modal-box button[type="submit"]:hover, .modal-box > button:not(.modal-close):hover { filter: brightness(1.08); }

.modal-status {
  color: var(--text-dim);
  font-size: 0.85rem;
  margin-top: 12px;
  line-height: 1.4;
}

.spinner {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 3px solid var(--panel-alt);
  border-top-color: var(--accent);
  margin: 20px auto;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- Toasts ---- */
#toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
}

.toast {
  background: var(--panel);
  color: var(--text);
  padding: 12px 18px;
  border-radius: 10px;
  box-shadow: var(--shadow);
  font-size: 0.88rem;
  border-left: 3px solid var(--accent);
  animation: toast-in 0.2s ease;
  max-width: 320px;
}
.toast.error { border-left-color: var(--danger); }

@keyframes toast-in {
  from { opacity: 0; transform: translateX(20px); }
  to { opacity: 1; transform: translateX(0); }
}
