:root {
  --bg: #0b141a;
  --panel: #111b21;
  --panel-2: #182229;
  --line: #222d34;
  --text: #e9edef;
  --muted: #8696a0;
  --accent: #00a884;
  --out: #005c4b;
  --in: #202c33;
  --danger: #f15c6d;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg); color: var(--text); font-size: 15px;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); font-size: 13px; min-height: 18px; }
button { cursor: pointer; font: inherit; }
.brand { font-weight: 600; display: flex; align-items: center; gap: 8px; }
.logo { color: var(--accent); }

/* Login */
.login { display: flex; align-items: center; justify-content: center; height: 100%; }
.login-card {
  background: var(--panel); padding: 28px; border-radius: 14px; width: min(360px, 92vw);
  display: flex; flex-direction: column; gap: 12px; border: 1px solid var(--line);
}
.login-card input {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--text);
  padding: 12px; border-radius: 8px; font-size: 15px;
}
.login-card button, .composer button, #new-btn {
  background: var(--accent); color: #04221b; border: none; border-radius: 8px;
  padding: 12px; font-weight: 600;
}

/* App layout */
.app { display: grid; grid-template-columns: 360px 1fr; height: 100%; }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 14px; border-bottom: 1px solid var(--line); }
.head-actions { display: flex; align-items: center; gap: 8px; }
.icon-btn { background: transparent; color: var(--text); border: none; font-size: 20px; line-height: 1; padding: 6px 8px; border-radius: 8px; }
.icon-btn:hover { background: var(--panel-2); }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.online { background: var(--accent); }
.dot.stale { background: #d9a441; }

.thread-list { overflow-y: auto; flex: 1; min-height: 0; }
.thread {
  display: flex; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line);
  cursor: pointer; align-items: center;
}
.thread:hover { background: var(--panel-2); }
.thread.active { background: var(--panel-2); }
.avatar {
  width: 42px; height: 42px; border-radius: 50%; background: var(--panel-2);
  display: flex; align-items: center; justify-content: center; color: var(--muted);
  font-weight: 600; flex: 0 0 auto; border: 1px solid var(--line);
}
.thread-main { min-width: 0; flex: 1; }
.thread-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-preview { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.account-menu { padding: 12px 14px; border-top: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px; }
.device-status { font-size: 12px; line-height: 1.5; }
.link { background: none; border: none; color: var(--accent); padding: 0; text-align: left; }

/* Conversation */
.conversation { display: flex; flex-direction: column; min-width: 0; min-height: 0;
  background:
    linear-gradient(rgba(11,20,26,.95), rgba(11,20,26,.95)); }
.conv-head { display: flex; align-items: center; gap: 10px; padding: 12px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.conv-name { font-weight: 600; }
.conv-sub { font-size: 12px; }
.back { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 18px 12% 18px 12%; display: flex; flex-direction: column; gap: 6px; }
.placeholder { margin: auto; }
.bubble {
  max-width: 70%; padding: 8px 11px; border-radius: 10px; font-size: 14.5px; line-height: 1.35;
  word-wrap: break-word; white-space: pre-wrap; position: relative;
}
.bubble .meta { font-size: 10.5px; color: var(--muted); margin-top: 3px; text-align: right; }
.bubble.out { align-self: flex-end; background: var(--out); }
.bubble.in { align-self: flex-start; background: var(--in); }
.bubble.failed { border: 1px solid var(--danger); }
.tick.failed { color: var(--danger); }

.composer { display: flex; gap: 10px; padding: 12px 16px; background: var(--panel); border-top: 1px solid var(--line); align-items: flex-end; }
.composer textarea {
  flex: 1; resize: none; background: var(--panel-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 12px; font-size: 15px; max-height: 140px;
}
.composer button { width: 46px; height: 44px; flex: 0 0 auto; font-size: 18px; }

/* Devices overlay */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px; }
.sheet { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; width: min(560px, 96vw); max-height: 90vh; overflow-y: auto; padding: 18px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.devices-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }
.device-row { display: flex; align-items: center; gap: 12px; padding: 12px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; }
.device-row .meta { flex: 1; min-width: 0; }
.device-row .dname { font-weight: 600; display: flex; align-items: center; gap: 8px; }
.device-row .dsub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.badge { font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; background: var(--accent); color: #04221b; padding: 2px 6px; border-radius: 5px; }
.row-actions { display: flex; gap: 6px; flex: 0 0 auto; }
.primary-btn { background: var(--accent); color: #04221b; border: none; border-radius: 9px; padding: 11px 14px; font-weight: 600; width: 100%; }
.ghost-btn, .mini-btn { background: transparent; color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: 13px; }
.mini-btn:hover, .ghost-btn:hover { background: var(--panel); }
.mini-btn.danger { color: var(--danger); border-color: #4a2a30; }
.pair-result { margin-top: 16px; border-top: 1px solid var(--line); padding-top: 16px; }
.pair-result h3 { margin: 0 0 8px; }
.pair-steps { margin: 0 0 12px; padding-left: 20px; color: var(--text); font-size: 14px; line-height: 1.6; }
.pair-label { font-size: 12px; color: var(--muted); display: block; margin-bottom: 4px; }
.pair-code { width: 100%; background: var(--bg); color: var(--accent); border: 1px solid var(--line); border-radius: 8px; padding: 10px; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; word-break: break-all; resize: none; }
.pair-actions { display: flex; gap: 8px; margin-top: 10px; }
.pair-actions .primary-btn { width: auto; flex: 1; }
.small { font-size: 12px; }

.new-search { width: 100%; background: var(--panel-2); border: 1px solid var(--line); color: var(--text); border-radius: 9px; padding: 12px; font-size: 15px; margin-bottom: 12px; }

/* Contacts & groups */
.tabs { display: flex; gap: 6px; margin-bottom: 14px; border-bottom: 1px solid var(--line); }
.tab { background: transparent; border: none; color: var(--muted); padding: 8px 14px; font-weight: 600; border-bottom: 2px solid transparent; }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.pane { display: block; }
.add-row { display: flex; gap: 8px; margin-bottom: 12px; }
.add-row input { flex: 1; min-width: 0; background: var(--panel-2); border: 1px solid var(--line); color: var(--text); border-radius: 8px; padding: 10px; }
.primary-btn.compact { width: auto; padding: 10px 16px; flex: 0 0 auto; }
.import-block { margin-bottom: 12px; border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; }
.import-block summary { cursor: pointer; color: var(--accent); font-size: 13px; }
.import-block textarea { width: 100%; background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 8px; margin: 8px 0; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; resize: vertical; }
.people-list { display: flex; flex-direction: column; gap: 6px; max-height: 50vh; overflow-y: auto; }
.person-row { display: flex; align-items: center; gap: 10px; padding: 10px 12px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px; }
.person-row.selectable { cursor: pointer; }
.person-row.selected { border-color: var(--accent); background: #16302a; }
.person-row .pmeta { flex: 1; min-width: 0; }
.person-row .pname { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.person-row .pnum { font-size: 12px; color: var(--muted); }
.person-row .check { color: var(--accent); font-weight: 700; width: 18px; text-align: center; }
.empty-note { color: var(--muted); padding: 12px; text-align: center; }

/* Mobile */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { display: flex; }
  .conversation { display: none; }
  .app.show-conversation .sidebar { display: none; }
  .app.show-conversation .conversation { display: flex; }
  .messages { padding: 14px 10px; }
  .bubble { max-width: 82%; }
  .back { display: inline-block; }
}
