:root {
  --bg: #ffffff;
  --bg-side: #f6f7f9;
  --bg-raise: #ffffff;
  --bg-hover: #eef0f4;
  --bg-active: #e3e8f1;
  --bg-code: #f3f4f7;
  --line: #e4e6eb;
  --text: #16181d;
  --text-2: #5d6370;
  --text-3: #8b909b;
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --accent-soft: #e5edfd;
  --mention: #fff4d6;
  --danger: #d92d20;
  --warn: #c2620a;
  --ok: #1f9d55;
  --shadow: 0 8px 30px rgba(15, 20, 30, .12), 0 1px 3px rgba(15, 20, 30, .08);
  --radius: 14px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171b;
    --bg-side: #101215;
    --bg-raise: #1d2025;
    --bg-hover: #1f2329;
    --bg-active: #262c36;
    --bg-code: #1e2127;
    --line: #2a2e35;
    --text: #e8eaee;
    --text-2: #a2a8b3;
    --text-3: #737985;
    --accent: #4d86f7;
    --accent-soft: #1d2a44;
    --mention: #3a3220;
    --danger: #f2655a;
    --warn: #f0a24b;
    --ok: #3ccf7c;
    --shadow: 0 8px 30px rgba(0, 0, 0, .5), 0 1px 3px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
code, pre { font-family: var(--mono); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 36px; height: 36px; border: 0; border-radius: 10px;
  background: transparent; color: var(--text-2); flex: none;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
button.primary {
  border: 0; border-radius: 10px; padding: 10px 16px;
  background: var(--accent); color: var(--accent-text); font-weight: 600;
}
button.primary:disabled { opacity: .5; cursor: default; }
button.ghost {
  border: 1px solid var(--line); border-radius: 10px; padding: 8px 14px; background: var(--bg-raise);
}
button.ghost:hover { background: var(--bg-hover); }
button.danger { color: var(--danger); }
.error { color: var(--danger); min-height: 1.2em; margin: 0; font-size: 14px; }

/* --- pairing -------------------------------------------------------------------------------- */
.pair { height: 100%; display: grid; place-items: center; padding: 16px; background: var(--bg-side); }
.pair-card {
  width: min(420px, 100%); background: var(--bg-raise); border: 1px solid var(--line);
  border-radius: 20px; padding: 32px 28px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 10px; text-align: center;
}
.pair-card img { align-self: center; }
.pair-card h1 { margin: 4px 0 0; font-size: 26px; }
.pair-card p { margin: 0; color: var(--text-2); }
.pair-card pre { margin: 0; padding: 8px; border-radius: 8px; background: var(--bg-code); }
.pair-card input {
  margin-top: 8px; padding: 12px 14px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg); text-align: center; letter-spacing: .04em;
}

/* --- layout ----------------------------------------------------------------------------------- */
.app { display: flex; height: 100%; height: 100dvh; }
.sidebar {
  width: 320px; flex: none; display: flex; flex-direction: column;
  background: var(--bg-side); border-right: 1px solid var(--line);
}
.room { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
.thread {
  width: 400px; flex: none; display: flex; flex-direction: column;
  border-left: 1px solid var(--line); background: var(--bg);
}

.side-head { display: flex; align-items: center; gap: 10px; padding: 14px 12px 8px 14px; }
.side-head h1 { font-size: 20px; margin: 0; flex: 1; }
.avatar-btn { border: 0; padding: 0; background: none; border-radius: 50%; }
.conn { font-size: 12px; color: var(--text-3); }
.conn.bad { color: var(--warn); }
.search {
  margin: 4px 12px 8px; padding: 8px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg);
}
.rooms { overflow-y: auto; flex: 1; padding: 0 8px 12px; }
.room-item {
  display: flex; gap: 10px; align-items: center; width: 100%; text-align: left;
  padding: 9px 10px; border: 0; border-radius: 12px; background: none;
}
.room-item:hover { background: var(--bg-hover); }
.room-item[aria-current="page"] { background: var(--bg-active); }
.room-item .meta { flex: 1; min-width: 0; }
.room-item .line1 { display: flex; align-items: baseline; gap: 6px; }
.room-item .name { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-item .time { font-size: 12px; color: var(--text-3); flex: none; }
.room-item .line2 { display: flex; align-items: center; gap: 6px; }
.room-item .preview {
  flex: 1; min-width: 0; color: var(--text-2); font-size: 13.5px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.room-item .preview.live { color: var(--accent); }
.room-item.unread .name { font-weight: 700; }
.room-item.unread .preview { color: var(--text); }
.badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; flex: none;
  background: var(--accent); color: #fff; font-size: 12px; font-weight: 700;
  display: inline-grid; place-items: center;
}
.badge.muted { background: var(--text-3); }
.badge.paused { background: var(--warn); min-width: 10px; width: 10px; height: 10px; padding: 0; }
.empty-list { color: var(--text-3); text-align: center; padding: 24px 12px; font-size: 14px; }

/* --- avatars ---------------------------------------------------------------------------------- */
.av {
  position: relative; flex: none; display: inline-grid; place-items: center;
  width: 36px; height: 36px; border-radius: 50%; color: #fff; font-weight: 600; font-size: 14px;
  background: var(--c, #8b909b); user-select: none;
}
.av.agent { border-radius: 28%; }
.av.sm { width: 22px; height: 22px; font-size: 10px; }
.av.lg { width: 40px; height: 40px; font-size: 15px; }
.av.group { border-radius: 28%; background: linear-gradient(135deg, #3b82f6, #2563eb); }
.av .dot {
  position: absolute; right: -2px; bottom: -2px; width: 11px; height: 11px; border-radius: 50%;
  background: var(--ok); border: 2px solid var(--bg-side);
}
.c-red { --c: #e5484d; } .c-orange { --c: #f76b15; } .c-yellow { --c: #d6a40a; } .c-green { --c: #30a46c; }
.c-mint { --c: #12a594; } .c-teal { --c: #0d9488; } .c-cyan { --c: #0891b2; } .c-blue { --c: #2f6fed; }
.c-indigo { --c: #4f46e5; } .c-purple { --c: #8e4ec6; } .c-pink { --c: #d6409f; } .c-brown { --c: #a1734e; }
.c-gray { --c: #8b909b; }

/* --- room ------------------------------------------------------------------------------------- */
.room-empty { margin: auto; color: var(--text-3); }
.room-head {
  display: flex; align-items: center; gap: 10px; padding: 10px 14px;
  border-bottom: 1px solid var(--line); min-height: 60px;
}
.room-head .back { display: none; }
.room-title { flex: 1; min-width: 0; }
.room-title h2 { font-size: 16px; margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-title p { font-size: 12.5px; margin: 0; color: var(--text-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.transcript { flex: 1; overflow-y: auto; padding: 12px 0 8px; overscroll-behavior: contain; }
.transcript .older { text-align: center; color: var(--text-3); font-size: 12px; padding: 8px; }
.day {
  display: flex; justify-content: center; margin: 14px 0 6px;
}
.day span {
  font-size: 12px; font-weight: 600; color: var(--text-2); background: var(--bg-raise);
  border: 1px solid var(--line); border-radius: 20px; padding: 2px 10px;
}

.msg { position: relative; display: flex; gap: 10px; padding: 2px 18px; }
.msg:hover { background: var(--bg-hover); }
.msg.start { margin-top: 10px; padding-top: 4px; }
.msg .gutter { width: 36px; flex: none; }
.msg .gutter .t { display: none; font-size: 10.5px; color: var(--text-3); text-align: right; padding-top: 4px; }
.msg:hover .gutter .t { display: block; }
.msg .main { flex: 1; min-width: 0; }
.msg .who { display: flex; align-items: baseline; gap: 6px; margin-bottom: 1px; }
.msg .who .n { font-weight: 650; color: var(--c, var(--text)); }
.msg .who .tag {
  font-size: 10.5px; font-weight: 600; color: var(--text-3); border: 1px solid var(--line);
  border-radius: 6px; padding: 0 5px; text-transform: lowercase;
}
.msg .who .t { font-size: 12px; color: var(--text-3); }
.msg.mine .who .n { color: var(--text); }
.msg.mentioned { background: var(--mention); }
.msg.urgent { box-shadow: inset 3px 0 0 var(--danger); }
.msg.pending .body { opacity: .55; }
.msg.failed .body { color: var(--danger); }
.msg .note { font-size: 12px; color: var(--text-3); display: flex; gap: 10px; align-items: center; }
.msg .note button { border: 0; background: none; padding: 0; color: var(--accent); font-weight: 600; font-size: 12px; }
.msg .note .warn { color: var(--warn); }
.msg.system { justify-content: center; }
.msg.system .sys {
  font-size: 13px; color: var(--text-2); background: var(--bg-code); border-radius: 10px;
  padding: 5px 12px; text-align: center; max-width: 80%;
}
.msg.system .sys p { margin: 0; }

.actions {
  position: absolute; right: 14px; top: -14px; display: none; gap: 2px; padding: 2px;
  background: var(--bg-raise); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow);
  z-index: 2;
}
.msg:hover .actions, .actions:focus-within { display: flex; }
.actions button { border: 0; background: none; border-radius: 8px; padding: 3px 7px; font-size: 14px; color: var(--text-2); }
.actions button:hover { background: var(--bg-hover); color: var(--text); }
.actions svg { width: 16px; height: 16px; vertical-align: -3px; }

.reactions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.reactions button {
  border: 1px solid var(--line); background: var(--bg-raise); border-radius: 12px;
  padding: 1px 8px; font-size: 13px;
}
.reactions button.mine { border-color: var(--accent); background: var(--accent-soft); }
.thread-link {
  display: inline-flex; align-items: center; gap: 6px; margin-top: 4px;
  border: 0; background: none; padding: 2px 4px 2px 0; color: var(--accent); font-size: 13px; font-weight: 600;
}
.thread-link .faces { display: inline-flex; }
.thread-link .faces .av { margin-right: -5px; border: 2px solid var(--bg); }
.thread-link .ago { color: var(--text-3); font-weight: 400; margin-left: 4px; }

/* --- markdown --------------------------------------------------------------------------------- */
.body { overflow-wrap: anywhere; }
.body > :first-child { margin-top: 0; }
.body > :last-child { margin-bottom: 0; }
.body p { margin: 0 0 .5em; }
.body h1, .body h2, .body h3, .body h4 { margin: .6em 0 .3em; line-height: 1.25; }
.body h1 { font-size: 1.3em; } .body h2 { font-size: 1.18em; } .body h3, .body h4 { font-size: 1.05em; }
.body ul, .body ol { margin: .2em 0 .5em; padding-left: 1.4em; }
.body li { margin: .1em 0; }
.body li > p { margin: 0; }
.body blockquote { margin: .3em 0 .5em; padding: 0 0 0 .8em; border-left: 3px solid var(--line); color: var(--text-2); }
.body a { color: var(--accent); }
.body code { font-size: .88em; background: var(--bg-code); border-radius: 5px; padding: .1em .35em; }
.body pre {
  position: relative; background: var(--bg-code); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px; overflow-x: auto; margin: .3em 0 .6em; font-size: 13px; line-height: 1.5;
}
.body pre code { background: none; padding: 0; font-size: inherit; }
.body pre .copy {
  position: absolute; top: 6px; right: 6px; border: 1px solid var(--line); background: var(--bg-raise);
  border-radius: 7px; font-size: 11.5px; padding: 1px 8px; opacity: 0; color: var(--text-2);
}
.body pre:hover .copy, .body pre .copy:focus { opacity: 1; }
.body table { border-collapse: collapse; margin: .3em 0 .6em; font-size: 14px; display: block; overflow-x: auto; }
.body th, .body td { border: 1px solid var(--line); padding: 4px 10px; text-align: left; }
.body th { background: var(--bg-code); }
.body hr { border: 0; border-top: 1px solid var(--line); margin: .8em 0; }
.body img { max-width: 100%; }
.body input[type=checkbox] { margin: 0 .4em 0 0; vertical-align: -1px; }
.body .mention { color: var(--accent); background: var(--accent-soft); border-radius: 5px; padding: 0 .2em; font-weight: 600; }
.caret {
  display: inline-block; width: 18px; height: 4px; border-radius: 2px; background: var(--accent);
  margin: 6px 0 2px; animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse { 0%, 100% { opacity: .25; } 50% { opacity: 1; } }
.deleted { color: var(--text-3); font-style: italic; }

/* --- composer --------------------------------------------------------------------------------- */
.foot { padding: 0 14px 14px; padding-bottom: max(14px, env(safe-area-inset-bottom)); }
.banner {
  display: flex; align-items: center; gap: 12px; margin-bottom: 8px; padding: 10px 12px;
  border-radius: 12px; background: var(--mention); border: 1px solid var(--line);
}
.banner .txt { flex: 1; font-size: 13.5px; }
.banner strong { display: block; }
.composer { position: relative; }
.typing { font-size: 12.5px; color: var(--text-3); min-height: 18px; padding: 0 4px 2px; }
.composer-row {
  display: flex; align-items: flex-end; gap: 8px; padding: 6px 6px 6px 14px;
  border: 1px solid var(--line); border-radius: 18px; background: var(--bg-raise);
}
.composer-row:focus-within { border-color: var(--accent); }
.composer textarea {
  flex: 1; border: 0; outline: 0; resize: none; background: none; padding: 6px 0;
  max-height: 40vh; line-height: 1.45;
}
.composer .send {
  width: 34px; height: 34px; border-radius: 50%; border: 0; flex: none;
  background: var(--accent); color: #fff; display: grid; place-items: center;
}
.composer .send:disabled { background: var(--bg-active); color: var(--text-3); cursor: default; }
.composer .send svg { width: 18px; height: 18px; stroke-width: 2.4; }
.mentions-pop {
  position: absolute; left: 0; bottom: 100%; margin: 0 0 6px; padding: 4px; list-style: none;
  min-width: 240px; max-height: 260px; overflow-y: auto; z-index: 5;
  background: var(--bg-raise); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
}
.mentions-pop li { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 8px; cursor: pointer; }
.mentions-pop li[aria-selected="true"], .mentions-pop li:hover { background: var(--bg-hover); }
.mentions-pop .h { color: var(--text-3); font-size: 13px; }

.jump {
  position: absolute; right: 22px; bottom: 120px; width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--bg-raise); box-shadow: var(--shadow); display: grid; place-items: center;
  color: var(--text-2); z-index: 3;
}

.thread-head { display: flex; align-items: center; padding: 10px 10px 10px 18px; border-bottom: 1px solid var(--line); min-height: 60px; }
.thread-head h2 { flex: 1; font-size: 16px; margin: 0; }
.thread .divider { margin: 8px 18px; border-top: 1px solid var(--line); font-size: 12px; color: var(--text-3); padding-top: 6px; }

/* --- dialogs & menus -------------------------------------------------------------------------- */
dialog {
  border: 1px solid var(--line); border-radius: 18px; padding: 0; background: var(--bg-raise); color: var(--text);
  box-shadow: var(--shadow); width: min(440px, calc(100% - 32px)); max-height: calc(100% - 64px);
}
dialog::backdrop { background: rgba(0, 0, 0, .35); }
.dlg { display: flex; flex-direction: column; gap: 12px; padding: 20px; }
.dlg h3 { margin: 0; font-size: 18px; }
.dlg label.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-2); }
.dlg input[type=text], .dlg select {
  padding: 9px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text);
}
.dlg .row { display: flex; gap: 8px; justify-content: flex-end; }
.dlg .seg { display: flex; gap: 4px; padding: 3px; background: var(--bg-code); border-radius: 10px; }
.dlg .seg button { flex: 1; border: 0; border-radius: 8px; padding: 6px; background: none; }
.dlg .seg button[aria-pressed="true"] { background: var(--bg-raise); box-shadow: 0 1px 2px rgba(0,0,0,.12); font-weight: 600; }
.picklist { list-style: none; margin: 0; padding: 0; max-height: 300px; overflow-y: auto; }
.picklist li label, .picklist li button {
  display: flex; align-items: center; gap: 10px; padding: 7px 6px; border-radius: 10px; width: 100%;
  border: 0; background: none; text-align: left; cursor: pointer;
}
.picklist li label:hover, .picklist li button:hover { background: var(--bg-hover); }
.picklist .sub { color: var(--text-3); font-size: 12.5px; }
.picklist .grow { flex: 1; min-width: 0; }
.picklist input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--accent); }
.emoji-pop {
  position: fixed; z-index: 20; display: flex; gap: 2px; padding: 4px;
  background: var(--bg-raise); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
}
.emoji-pop button { border: 0; background: none; font-size: 20px; padding: 4px 6px; border-radius: 8px; }
.emoji-pop button:hover { background: var(--bg-hover); }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 30;
  background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 12px; font-size: 14px;
  box-shadow: var(--shadow); max-width: calc(100% - 32px);
}

/* --- narrow screens --------------------------------------------------------------------------- */
@media (max-width: 1100px) {
  .thread { position: fixed; inset: 0 0 0 auto; width: min(420px, 100%); z-index: 10; box-shadow: var(--shadow); }
}
@media (max-width: 760px) {
  .sidebar { width: 100%; border-right: 0; }
  .app[data-view="room"] .sidebar { display: none; }
  .app[data-view="list"] .room { display: none; }
  .room-head .back { display: inline-grid; }
  .thread { width: 100%; }
  .msg { padding: 2px 12px; }
  .msg .gutter { width: 30px; }
  .msg .gutter .av { width: 30px; height: 30px; font-size: 12px; }
  .foot { padding: 0 8px 8px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .actions { right: 8px; }
}
@media (hover: none) {
  .msg:hover { background: none; }
  .msg:hover .actions { display: none; }
  .msg.touch-open .actions { display: flex; }
  .body pre .copy { opacity: 1; }
}
