*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface-2: #20242f;
  --border: #2a2d3a;
  --text: #e8eaf0;
  --muted: #6b7080;
  --accent: #4f7fff;
  --danger: #e05555;
  --green: #31d49b;
  --orange: #f0a030;
  --radius: 12px;
  --header-h: 56px;
  --nav-h: 60px;
}

html, body { height: 100%; background: var(--bg); color: var(--text); font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; font-size: 15px; -webkit-font-smoothing: antialiased; overflow: hidden; }

#app { display: flex; flex-direction: column; height: 100%; max-width: 600px; margin: 0 auto; }

/* Header */
header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px; height: var(--header-h); flex-shrink: 0;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
  padding-top: env(safe-area-inset-top);
}
.header-left { display: flex; align-items: center; gap: 10px; min-width: 0; }
.header-right { display: flex; align-items: center; gap: 10px; }

h1, #header-title { font-size: 18px; font-weight: 700; letter-spacing: -.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.conn-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); display: inline-block; transition: background .3s; }
.conn-dot.conn-ok  { background: var(--green); }
.conn-dot.conn-err { background: var(--danger); cursor: pointer; }

.badge {
  background: var(--accent); color: #fff; border-radius: 10px;
  font-size: 11px; font-weight: 700; min-width: 20px; height: 20px;
  display: flex; align-items: center; justify-content: center; padding: 0 5px;
  flex-shrink: 0;
}
.badge.hidden { display: none; }

button { background: none; border: none; color: var(--text); cursor: pointer; font-size: 15px; }
.icon-btn { font-size: 28px; font-weight: 300; line-height: 1; padding: 0 6px 0 0; opacity: .85; }
.icon-btn:hover { opacity: 1; }
.icon-btn.hidden { display: none; }
#install-btn { font-size: 13px; font-weight: 600; background: rgba(79,127,255,.18); border: 1px solid var(--accent); color: var(--accent); border-radius: 999px; padding: 4px 10px; }

/* Content */
main, #content {
  flex: 1; overflow-y: auto; overflow-x: hidden; display: flex; flex-direction: column;
  padding-bottom: env(safe-area-inset-bottom);
  min-height: 0;
}
.empty-state { color: var(--muted); text-align: center; margin: 60px 16px 0; }

/* Bottom nav */
.bottom-nav {
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: var(--nav-h); flex-shrink: 0;
  border-top: 1px solid var(--border); background: var(--surface);
  padding-bottom: env(safe-area-inset-bottom);
}
.bottom-nav.hidden { display: none; }
.nav-item {
  display: grid; place-items: center; align-content: center; gap: 2px;
  color: var(--muted); font-size: 11px;
}
.nav-item b { font-size: 18px; line-height: 1; font-weight: 500; }
.nav-item.active { color: var(--accent); }
.nav-label { display: inline-flex; align-items: center; gap: 3px; }
.nav-badge {
  background: var(--accent); color: #fff; border-radius: 999px;
  font-size: 10px; font-weight: 700; min-width: 15px; height: 15px;
  display: inline-flex; align-items: center; justify-content: center; padding: 0 4px;
}
.nav-badge.hidden { display: none; }

/* Search bar */
.search {
  margin: 14px; display: flex; align-items: center; gap: 9px;
}
.search input {
  width: 100%; background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; color: var(--text); font-size: 14px; padding: 11px 12px;
}

/* List items (chats / registry / channels) */
.list { padding: 8px 12px 16px; display: flex; flex-direction: column; gap: 8px; }
.list-item {
  display: grid; grid-template-columns: 38px 1fr auto; gap: 12px;
  align-items: center; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px; cursor: pointer;
}
.list-item:active { opacity: .85; }
.avatar {
  width: 38px; height: 38px; border-radius: 50%; flex-shrink: 0;
  display: grid; place-items: center; font-weight: 800; font-size: 14px;
  background: linear-gradient(135deg, var(--accent), #8a6dff); color: #fff;
}
.avatar.agent { background: linear-gradient(135deg, var(--green), #35bfd3); color: #042; }
.item-main { min-width: 0; }
.item-main b { display: block; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-main span { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.time { color: var(--muted); font-size: 11px; }

.sub-btn {
  border: 1px solid var(--accent); color: var(--accent); background: rgba(79,127,255,.12);
  border-radius: 999px; padding: 6px 10px; font-size: 11px; font-weight: 700;
}
.sub-btn.subscribed { border-color: var(--green); color: var(--green); background: rgba(49,212,155,.10); }

/* Tabs (channel detail: Feed / My sent) */
.tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; padding: 12px 12px 0; flex-shrink: 0; }
.tab { padding: 10px; text-align: center; font-size: 13px; color: var(--muted); border-radius: 10px; border: 1px solid transparent; }
.tab.active { color: var(--text); background: rgba(79,127,255,.14); border-color: var(--accent); }
.tab-body { flex: 1; display: flex; flex-direction: column; min-height: 0; }

/* Feed posts */
.feed { padding: 12px; display: flex; flex-direction: column; gap: 12px; flex: 1; }
.post { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
.post-head { display: flex; align-items: center; gap: 9px; margin-bottom: 10px; }
.post-head .avatar { width: 28px; height: 28px; font-size: 11px; }
.post-head b { font-size: 13px; }
.post-head span { color: var(--muted); font-size: 11px; margin-left: auto; }
.post-title { font-weight: 700; margin-bottom: 4px; }
.post p { line-height: 1.45; font-size: 14px; }
.cta-row { display: flex; gap: 8px; margin-top: 12px; }
.small-cta { padding: 7px 10px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); color: var(--muted); font-size: 12px; }

/* Chat thread (human chats + channel "My sent") */
.chat-wrapper { display: flex; flex-direction: column; flex: 1; min-height: 0; overflow: hidden; }
.chat { flex: 1; display: flex; flex-direction: column; gap: 10px; padding: 14px; overflow-y: auto; min-height: 0; }
.bubble-wrap { display: flex; }
.bubble {
  max-width: 82%; border-radius: 16px; padding: 10px 12px; font-size: 14px;
  line-height: 1.4; border: 1px solid var(--border); position: relative; touch-action: pan-y;
  word-break: break-word; overflow-wrap: break-word;
}
.bubble small { display: block; margin-top: 6px; color: rgba(255,255,255,.45); text-align: right; font-size: 10px; }
.incoming { align-self: flex-start; background: var(--surface); margin-right: auto; }
.outgoing { align-self: flex-end; margin-left: auto; background: linear-gradient(135deg, rgba(79,127,255,.55), rgba(79,127,255,.28)); border-color: var(--accent); }
.agent-reply { align-self: flex-start; margin-right: auto; background: linear-gradient(135deg, rgba(49,212,155,.16), rgba(53,191,211,.08)); border-color: rgba(49,212,155,.35); }
.bubble-wrap.justify-end { justify-content: flex-end; }

.bubble-delete {
  display: none; position: absolute; top: -7px; right: -7px;
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--danger); color: #fff; font-size: 13px; font-weight: 700; line-height: 1;
  align-items: center; justify-content: center; cursor: pointer; z-index: 1;
}
@media (hover: hover) {
  .bubble-wrap:hover .bubble-delete { display: flex; }
}

.composer {
  display: grid; grid-template-columns: 1fr 42px; gap: 8px;
  padding: 10px 14px; background: var(--bg); flex-shrink: 0;
  border-top: 1px solid var(--border);
}
.composer-input {
  min-height: 42px; border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface); color: var(--text); padding: 0 14px; font-size: 14px;
}
.send {
  width: 42px; height: 42px; border-radius: 50%; background: var(--accent);
  color: #fff; font-size: 18px; display: flex; align-items: center; justify-content: center;
}

/* Me tab */
.me-panel { padding: 16px; display: flex; flex-direction: column; }
.field-label { display: block; font-size: 11px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .6px; margin-top: 16px; margin-bottom: 6px; }
.me-panel input[type="text"], .me-panel input, .me-panel textarea {
  width: 100%; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text); font-size: 14px; padding: 10px;
}
.me-panel textarea { resize: vertical; font-family: inherit; }
#me-save-btn { width: 100%; margin-top: 20px; background: var(--accent); color: #fff; border-radius: 8px; padding: 10px 18px; font-weight: 600; }
.hint { color: var(--muted); font-size: 12px; margin-top: 12px; line-height: 1.5; }
.hint code { background: var(--border); padding: 2px 5px; border-radius: 4px; font-size: 11px; overflow-wrap: anywhere; }
button.secondary { background: var(--surface-2); border: 1px solid var(--border); color: var(--text); border-radius: 8px; padding: 10px 18px; margin-top: 18px; font-weight: 600; }
button.danger { background: var(--danger); color: #fff; border-radius: 8px; padding: 10px 18px; margin-top: 18px; font-weight: 600; }
.error { color: var(--danger); font-size: 13px; margin-top: 10px; }
.error.hidden { display: none; }

/* Toast */
.toast {
  position: fixed; bottom: calc(20px + var(--nav-h) + env(safe-area-inset-bottom)); left: 50%;
  transform: translateX(-50%); background: #333; color: #fff;
  padding: 10px 18px; border-radius: 20px; font-size: 13px;
  z-index: 100; white-space: nowrap;
}
.toast.hidden { display: none; }

/* Key import modal */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.7); z-index: 200;
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.modal.hidden { display: none; }
.modal-box {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 24px; width: 100%; max-width: 420px;
}
.modal-box h2 { margin-bottom: 12px; }
.modal-box p { color: var(--muted); font-size: 13px; margin-bottom: 16px; line-height: 1.5; }
.modal-box code { background: var(--border); padding: 2px 5px; border-radius: 4px; font-size: 12px; }
textarea {
  width: 100%; background: var(--bg); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text); font-family: monospace; font-size: 12px;
  padding: 10px; resize: vertical; margin-bottom: 14px;
}
button:hover { opacity: .85; }
#key-import-btn { background: var(--accent); color: #fff; border-radius: 8px; padding: 10px 18px; font-weight: 600; }
