:root {
  --blue: #0A84FF;
  --blue-press: #0071E3;
  --bg: #F2F2F7;
  --panel: #FFFFFF;
  --line: #E5E5EA;
  --text: #111114;
  --muted: #6E6E73;
  --gray-bubble: #E9E9EB;
  --gray-text: #111114;
  --hover: #F5F5F7;
  --active: #E8F1FF;
  --danger: #FF3B30;
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --panel: #1C1C1E;
    --line: #2C2C2E;
    --text: #F5F5F7;
    --muted: #98989D;
    --gray-bubble: #2C2C2E;
    --gray-text: #F5F5F7;
    --hover: #26262A;
    --active: #0B2A4D;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 var(--font);
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { white-space: nowrap; cursor: pointer; border: 0; border-radius: 10px; padding: 8px 14px; background: none; }
button:disabled { opacity: .5; cursor: default; }
.primary { background: var(--blue); color: #fff; font-weight: 600; }
.primary:hover:not(:disabled) { background: var(--blue-press); }
.ghost { color: var(--blue); }
.ghost:hover { background: var(--hover); }
.small { font-size: 13px; padding: 6px 10px; }
.danger { color: var(--danger); }
input, select, textarea {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 9px 12px;
  outline: none;
  width: 100%;
}
input:focus, select:focus, textarea:focus { border-color: var(--blue); }
input[type=color] { padding: 2px; width: 44px; height: 38px; flex: none; }

/* Top bar */
.topbar {
  display: flex; align-items: center; gap: 14px;
  padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.logo { height: 34px; display: block; }
.ws-select { width: auto; max-width: 240px; padding: 6px 10px; font-weight: 600; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 4px; }
.user { color: var(--muted); font-size: 13px; margin-left: 8px; }

/* Layout */
.app { flex: 1; display: grid; grid-template-columns: 340px 1fr; min-height: 0; }
.pane { min-height: 0; display: flex; flex-direction: column; background: var(--panel); }
.list-pane { border-right: 1px solid var(--line); }
.list-tools { display: flex; gap: 8px; padding: 12px; }
.tag-filter { display: flex; gap: 6px; padding: 0 12px 10px; flex-wrap: wrap; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px; border-radius: 999px; font-size: 12.5px; font-weight: 500;
  border: 1px solid var(--line); background: var(--panel); color: var(--text);
}
.chip .dot { width: 8px; height: 8px; border-radius: 50%; }
.chip.on { background: var(--text); color: var(--panel); border-color: var(--text); }
.chat-list { overflow-y: auto; flex: 1; }
.chat {
  display: grid; grid-template-columns: 40px 1fr auto; gap: 10px; align-items: center;
  padding: 10px 12px; margin: 0 6px; border-radius: 12px; cursor: pointer;
}
.chat:hover { background: var(--hover); }
.chat.active { background: var(--active); }
.avatar {
  width: 40px; height: 40px; border-radius: 50%;
  background: linear-gradient(180deg, #A1A1A6, #85858A); color: #fff;
  display: grid; place-items: center; font-weight: 600; font-size: 15px;
}
.chat-main { min-width: 0; }
.chat-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-last { color: var(--muted); font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-meta { text-align: right; font-size: 12px; color: var(--muted); align-self: start; padding-top: 2px; }
.chat-dots { display: flex; gap: 3px; justify-content: flex-end; margin-top: 6px; }
.chat-dots span { width: 7px; height: 7px; border-radius: 50%; }
.list-empty { color: var(--muted); padding: 24px; text-align: center; }

/* Thread */
.thread-pane { background: var(--panel); }
.empty { flex: 1; display: grid; place-content: center; text-align: center; color: var(--muted); gap: 12px; }
.empty-logo { width: 160px; margin: 0 auto; opacity: .9; }
.thread { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.thread[hidden] { display: none; }
.thread-head { display: flex; align-items: center; gap: 8px; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.thread-title { flex: 1; min-width: 0; text-align: center; }
.thread-name { font-weight: 600; }
.thread-sub { color: var(--muted); font-size: 12.5px; }
.back { display: none; font-size: 28px; line-height: 1; padding: 2px 10px; }
.thread-tags { display: flex; gap: 6px; flex-wrap: wrap; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.thread-tags .chip { cursor: pointer; }
.thread-tags .chip.on { background: var(--c); border-color: var(--c); color: #fff; }
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 3px; }
.day { align-self: center; color: var(--muted); font-size: 12px; margin: 12px 0 6px; }
.bubble {
  max-width: min(72%, 520px); padding: 8px 13px; border-radius: 19px;
  white-space: pre-wrap; overflow-wrap: anywhere; font-size: 15.5px; line-height: 1.35;
}
.bubble.me { align-self: flex-end; background: var(--blue); color: #fff; border-bottom-right-radius: 6px; }
.bubble.them { align-self: flex-start; background: var(--gray-bubble); color: var(--gray-text); border-bottom-left-radius: 6px; }
.bubble.pending { opacity: .6; }
.bubble.failed { background: var(--danger); }
.status { align-self: flex-end; font-size: 11px; color: var(--muted); margin: 1px 4px 6px; }
.sender { font-size: 11px; color: var(--muted); margin: 6px 12px 1px; }
.composer { display: flex; gap: 8px; padding: 10px 14px calc(10px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); align-items: flex-end; }
.composer textarea { resize: none; border-radius: 20px; max-height: 160px; padding: 9px 14px; }
.send {
  width: 36px; height: 36px; padding: 0; border-radius: 50%; flex: none;
  background: var(--blue); color: #fff; font-size: 19px; font-weight: 700;
}

/* Dialogs */
.dialog {
  border: 0; border-radius: 18px; padding: 22px; width: min(440px, 92vw);
  background: var(--panel); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.dialog.wide { width: min(640px, 94vw); }
.dialog::backdrop { background: rgba(0,0,0,.35); }
.dialog h2 { margin: 0 0 14px; font-size: 19px; }
.dialog h3 { margin: 20px 0 10px; font-size: 15px; }
.dialog label { display: grid; gap: 6px; margin-bottom: 12px; font-size: 13px; color: var(--muted); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.hint { color: var(--muted); font-size: 13px; margin: -6px 0 12px; }
.row { display: flex; gap: 8px; align-items: center; }
.stack { display: grid; gap: 8px; }
.tags-editor { display: grid; gap: 8px; margin-bottom: 12px; }
.ws-admin-list { display: grid; gap: 10px; }
.ws-card { border: 1px solid var(--line); border-radius: 12px; padding: 12px; display: grid; gap: 8px; }
.ws-card-head { display: flex; align-items: center; gap: 8px; }
.ws-card-head strong { flex: 1; }
.ws-url { color: var(--muted); font-size: 12.5px; overflow-wrap: anywhere; }
.members { display: flex; flex-wrap: wrap; gap: 6px; }

/* Gate + toast */
.gate { position: fixed; inset: 0; background: var(--bg); display: grid; place-content: center; text-align: center; gap: 16px; padding: 24px; }
.gate[hidden] { display: none; }
.gate-logo { width: 200px; margin: 0 auto; }
.gate p { color: var(--muted); max-width: 420px; }
.login { display: flex; gap: 8px; width: min(320px, 86vw); margin: 0 auto; }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px);
  background: #1C1C1E; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: .2s; max-width: 90vw; z-index: 10;
}
.toast.show { opacity: 1; transform: translateX(-50%); }

/* Mobile */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .thread-pane { display: none; }
  body.in-thread .list-pane { display: none; }
  body.in-thread .thread-pane { display: flex; }
  .back { display: block; }
  .user { display: none; }
  .logo { height: 28px; }
  .ws-select { max-width: 140px; }
  .bubble { max-width: 82%; }
}
