:root {
  --bg: #0b141a;
  --panel: #111b21;
  --panel-2: #202c33;
  --panel-3: #2a3942;
  --accent: #00a884;
  --accent-2: #005c4b;
  --text: #e9edef;
  --muted: #8696a0;
  --divider: #222d34;
  --danger: #f15c6d;
  --bubble-in: #202c33;
  --bubble-out: #005c4b;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overflow: hidden;
}

.app {
  display: grid;
  grid-template-columns: minmax(300px, 30%) 1fr;
  height: 100dvh;
}

/* ---------- Sidebar ---------- */
.sidebar {
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--divider);
  min-height: 0;
}

.sidebar-header {
  padding: 10px;
  background: var(--panel-2);
  padding-top: max(10px, env(safe-area-inset-top));
}

.accounts {
  display: flex;
  gap: 8px;
}

.account-pill {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 10px;
  background: var(--panel-3);
  border: 1px solid transparent;
  cursor: pointer;
  color: var(--text);
  text-align: left;
  min-width: 0;
}

.account-pill.active {
  border-color: var(--accent);
  background: #14342f;
}

.account-pill .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--muted);
  flex: 0 0 auto;
}
.account-pill .dot.open {
  background: #25d366;
}
.account-pill .dot.qr {
  background: #f5a623;
}
.account-pill .dot.connecting {
  background: #f5a623;
  animation: blink 1s infinite;
}
.account-pill .dot.close {
  background: var(--danger);
}

@keyframes blink {
  50% {
    opacity: 0.3;
  }
}

.account-pill .meta {
  min-width: 0;
  line-height: 1.2;
}
.account-pill .meta .label {
  font-size: 13px;
  font-weight: 600;
}
.account-pill .meta .sub {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.toolbar {
  display: flex;
  gap: 8px;
  padding: 8px 10px;
  align-items: center;
}

.search {
  flex: 1;
  background: var(--panel-2);
  border: none;
  outline: none;
  color: var(--text);
  padding: 9px 12px;
  border-radius: 8px;
  font-size: 14px;
}

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #06241e;
  font-size: 22px;
  font-weight: 700;
  cursor: pointer;
  flex: 0 0 auto;
}

.chat-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--divider);
}
.chat-item:hover,
.chat-item.active {
  background: var(--panel-2);
}

.avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--panel-3);
  display: grid;
  place-items: center;
  font-weight: 600;
  color: var(--text);
  flex: 0 0 auto;
  text-transform: uppercase;
}
.avatar.group {
  background: #283a45;
}

.chat-main {
  flex: 1;
  min-width: 0;
}
.chat-top {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}
.chat-name {
  font-size: 15px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-time {
  font-size: 11px;
  color: var(--muted);
  flex: 0 0 auto;
}
.chat-bottom {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-top: 2px;
}
.chat-preview {
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.badge {
  background: var(--accent);
  color: #06241e;
  font-size: 11px;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  border-radius: 9px;
  display: grid;
  place-items: center;
  padding: 0 5px;
  flex: 0 0 auto;
}

.sidebar-footer {
  padding: 8px 12px;
  border-top: 1px solid var(--divider);
  background: var(--panel-2);
}

.link-btn {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 13px;
  padding: 4px;
}
.link-btn.danger {
  color: var(--danger);
}

/* ---------- Content / right pane ---------- */
.content {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background-color: var(--bg);
  background-image: linear-gradient(rgba(11, 20, 26, 0.95), rgba(11, 20, 26, 0.95));
}

.placeholder {
  margin: auto;
  text-align: center;
  color: var(--muted);
  padding: 24px;
  max-width: 420px;
}
.placeholder h2 {
  color: var(--text);
  font-weight: 500;
}

/* QR / connect panel */
.connect {
  margin: auto;
  text-align: center;
  padding: 24px;
  max-width: 420px;
}
.connect img {
  width: 260px;
  height: 260px;
  background: #fff;
  border-radius: 12px;
  padding: 10px;
}
.connect h2 {
  font-weight: 500;
}
.connect ol {
  text-align: left;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.6;
  margin-top: 16px;
}
.spinner {
  width: 42px;
  height: 42px;
  border: 4px solid var(--panel-3);
  border-top-color: var(--accent);
  border-radius: 50%;
  margin: 20px auto;
  animation: spin 0.9s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* Conversation */
.conv-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--panel-2);
  border-bottom: 1px solid var(--divider);
  padding-top: max(10px, env(safe-area-inset-top));
}
.back-btn {
  display: none;
  background: none;
  border: none;
  color: var(--text);
  font-size: 22px;
  cursor: pointer;
  padding: 0 4px;
}
.conv-title {
  font-size: 15px;
  font-weight: 500;
}
.conv-sub {
  font-size: 12px;
  color: var(--muted);
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 0;
}

.bubble {
  max-width: 75%;
  padding: 7px 10px 8px;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.35;
  word-wrap: break-word;
  white-space: pre-wrap;
  position: relative;
}
.bubble .b-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 2px;
}
.bubble .b-time {
  font-size: 10px;
  color: var(--muted);
  float: right;
  margin: 6px 0 -2px 10px;
}
.bubble.in {
  align-self: flex-start;
  background: var(--bubble-in);
}
.bubble.out {
  align-self: flex-end;
  background: var(--bubble-out);
}

.composer {
  display: flex;
  gap: 8px;
  padding: 10px 14px;
  background: var(--panel-2);
  padding-bottom: max(10px, env(safe-area-inset-bottom));
}
.composer input {
  flex: 1;
  background: var(--panel-3);
  border: none;
  outline: none;
  color: var(--text);
  padding: 11px 14px;
  border-radius: 22px;
  font-size: 15px;
}
.composer button {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #06241e;
  font-size: 18px;
  cursor: pointer;
  flex: 0 0 auto;
}
.composer button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ---------- Modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: grid;
  place-items: center;
  z-index: 50;
}
.modal.hidden {
  display: none;
}
.modal-card {
  background: var(--panel-2);
  padding: 24px;
  border-radius: 12px;
  width: min(90vw, 340px);
  text-align: center;
}
.modal-card input {
  width: 100%;
  margin: 12px 0;
  padding: 11px;
  border-radius: 8px;
  border: none;
  background: var(--panel-3);
  color: var(--text);
  outline: none;
}
.primary-btn {
  background: var(--accent);
  color: #06241e;
  border: none;
  padding: 11px 18px;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
}
.error-text {
  color: var(--danger);
  font-size: 13px;
  margin-top: 10px;
  min-height: 16px;
}

/* ---------- Responsive: iPhone single pane ---------- */
@media (max-width: 768px) {
  .app {
    grid-template-columns: 1fr;
  }
  .content {
    position: fixed;
    inset: 0;
    transform: translateX(100%);
    transition: transform 0.2s ease;
    z-index: 10;
  }
  .app.show-chat .content {
    transform: translateX(0);
  }
  .back-btn {
    display: block;
  }
}
