:root {
  --app-bg: #d1d7db;
  --panel: #ffffff;
  --panel-head: #f0f2f5;
  --hover: #f5f6f6;
  --active: #f0f2f5;
  --border: #e9edef;
  --text: #111b21;
  --muted: #667781;
  --accent: #00a884;
  --accent-text: #ffffff;
  --chat-bg: #efeae2;
  --chat-dots: rgb(0 0 0 / .045);
  --bubble-in: #ffffff;
  --bubble-out: #d9fdd3;
  --quote-bg: rgb(0 0 0 / .05);
  --tick-read: #53bdeb;
  --danger: #ea0038;
  --badge: #25d366;
  --badge-text: #ffffff;
  --rail: #f0f2f5;
  --day-chip: #ffffff;
  --shadow: 0 1px .5px rgb(11 20 26 / .13);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --app-bg: #0c1317;
    --panel: #111b21;
    --panel-head: #202c33;
    --hover: #202c33;
    --active: #2a3942;
    --border: #222d34;
    --text: #e9edef;
    --muted: #8696a0;
    --accent: #00a884;
    --accent-text: #111b21;
    --chat-bg: #0b141a;
    --chat-dots: rgb(255 255 255 / .03);
    --bubble-in: #202c33;
    --bubble-out: #005c4b;
    --quote-bg: rgb(0 0 0 / .2);
    --tick-read: #53bdeb;
    --danger: #f15c6d;
    --badge: #00a884;
    --badge-text: #111b21;
    --rail: #202c33;
    --day-chip: #182229;
    --shadow: 0 1px .5px rgb(0 0 0 / .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--app-bg);
  color: var(--text);
  font: 14.2px/1.4 "Segoe UI", system-ui, -apple-system, Roboto, Helvetica, Arial, sans-serif;
}
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea { font: inherit; color: var(--text); }
[hidden] { display: none !important; }

.app {
  display: grid;
  grid-template-columns: 64px minmax(300px, 30%) 1fr;
  grid-template-rows: minmax(0, 1fr); /* long chats scroll inside, composer stays visible */
  height: 100vh;
  height: 100dvh;
  max-width: 1700px;
  margin: 0 auto;
  background: var(--panel);
  box-shadow: 0 6px 18px rgb(11 20 26 / .05);
}

/* ---- rail ---- */
.rail {
  background: var(--rail);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; align-items: center;
  padding: 12px 0; gap: 8px;
}
.rail-accounts {
  display: flex; flex-direction: column; gap: 14px; flex: 1; overflow-y: auto; align-items: center; width: 100%;
  padding: 8px 0; /* room for the outline and unread badge */
}
.rail-acc {
  position: relative; border: 0; padding: 0; background: none; border-radius: 50%;
  outline: 3px solid transparent; outline-offset: 2px;
}
.rail-acc.active { outline-color: var(--accent); }
.rail-acc .dot {
  position: absolute; right: -1px; bottom: -1px; width: 12px; height: 12px; border-radius: 50%;
  border: 2px solid var(--rail); background: var(--danger);
}
.rail-acc .dot.open { background: var(--badge); }
.rail-acc .dot.connecting { background: #f5a623; }
.rail-acc .count {
  position: absolute; top: -5px; right: -7px; min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px; background: var(--badge); color: var(--badge-text); font-size: 11px; font-weight: 700;
  display: grid; place-items: center;
}
.rail-btn { color: var(--muted); padding: 8px; border-radius: 50%; display: grid; place-items: center; }
.rail-btn:hover { background: var(--hover); }

/* ---- shared ---- */
.avatar {
  flex: none; width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center; font-weight: 600; font-size: 15px; color: #fff;
  background: #8696a0; user-select: none;
}
.avatar.lg { width: 49px; height: 49px; font-size: 17px; }
.icon-btn {
  border: 0; background: none; color: var(--muted); padding: 8px; border-radius: 50%;
  display: grid; place-items: center; cursor: pointer;
}
.icon-btn:hover { background: var(--hover); }
.side-title { min-width: 0; flex: 1; }
.title-main { font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.title-sub { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---- side ---- */
.side { display: flex; flex-direction: column; min-width: 0; border-right: 1px solid var(--border); background: var(--panel); }
.side-head, .conv-head {
  height: 59px; flex: none; display: flex; align-items: center; gap: 12px; padding: 0 16px;
  background: var(--panel-head);
}
.banner { padding: 10px 16px; font-size: 13px; background: #fff3c4; color: #54420a; }
@media (prefers-color-scheme: dark) { .banner { background: #3d3415; color: #f3e3a3; } }
.banner a { color: inherit; font-weight: 600; }
.search { padding: 8px 12px; border-bottom: 1px solid var(--border); }
.search input {
  width: 100%; border: 0; border-radius: 8px; padding: 8px 14px; background: var(--panel-head); outline: none;
}
.chat-list { flex: 1; overflow-y: auto; }
.chat-item {
  display: flex; gap: 13px; align-items: center; padding: 0 0 0 13px; cursor: pointer; width: 100%;
  border: 0; background: none; text-align: left;
}
.chat-item:hover { background: var(--hover); }
.chat-item.active { background: var(--active); }
.chat-item .body { flex: 1; min-width: 0; border-bottom: 1px solid var(--border); padding: 12px 15px 12px 0; }
.chat-item .row1, .chat-item .row2 { display: flex; align-items: center; gap: 6px; }
.chat-item .name { flex: 1; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .time { font-size: 12px; color: var(--muted); }
.chat-item .time.unread { color: var(--badge); }
.chat-item .preview { flex: 1; font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; align-items: center; gap: 3px; }
.chat-item .unread-badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: var(--badge); color: var(--badge-text);
  font-size: 12px; font-weight: 600; display: grid; place-items: center;
}
.chat-item .tag { font-size: 11px; color: var(--danger); }
.list-empty { padding: 40px 24px; text-align: center; color: var(--muted); font-size: 14px; }

/* ---- conversation ---- */
.conv { display: flex; flex-direction: column; min-width: 0; position: relative; background: var(--chat-bg); }
.conv-head { border-left: 1px solid var(--border); }
.conv-head .back { display: none; margin-left: -8px; }
.conv-empty {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
  background: var(--panel-head); color: var(--muted); padding: 24px; border-bottom: 6px solid var(--accent);
}
.conv-empty h2 { color: var(--text); font-weight: 300; font-size: 30px; margin: 16px 0 8px; }
.empty-art { font-size: 72px; opacity: .6; }

.messages {
  flex: 1; overflow-y: auto; padding: 12px 6% 8px;
  background-color: var(--chat-bg);
  background-image: radial-gradient(var(--chat-dots) 1.2px, transparent 1.3px);
  background-size: 18px 18px;
}
.day { display: flex; justify-content: center; margin: 10px 0; }
.day span {
  background: var(--day-chip); color: var(--muted); font-size: 12.5px; padding: 5px 12px; border-radius: 7.5px;
  box-shadow: var(--shadow); text-transform: uppercase;
}
.msg { display: flex; flex-direction: column; margin-bottom: 2px; position: relative; }
.msg.out { align-items: flex-end; }
.msg.in { align-items: flex-start; }
.msg.first { margin-top: 10px; }
.msg.has-reactions { margin-bottom: 22px; }
.bubble {
  position: relative; max-width: min(65%, 560px); padding: 6px 7px 8px 9px; border-radius: 7.5px;
  box-shadow: var(--shadow); word-wrap: break-word; overflow-wrap: anywhere;
}
.msg.in .bubble { background: var(--bubble-in); }
.msg.out .bubble { background: var(--bubble-out); }
.msg.in.first .bubble { border-top-left-radius: 0; }
.msg.out.first .bubble { border-top-right-radius: 0; }
.msg.in.first .bubble::before, .msg.out.first .bubble::before {
  content: ""; position: absolute; top: 0; width: 8px; height: 13px;
}
.msg.in.first .bubble::before { left: -8px; background: var(--bubble-in); clip-path: polygon(0 0, 100% 0, 100% 100%); }
.msg.out.first .bubble::before { right: -8px; background: var(--bubble-out); clip-path: polygon(0 0, 100% 0, 0 100%); }
.sender { font-size: 12.8px; font-weight: 500; color: var(--accent); margin-bottom: 2px; }
.text { white-space: pre-wrap; }
.text a { color: #027eb5; }
@media (prefers-color-scheme: dark) { .text a { color: #53bdeb; } }
.meta {
  float: right; margin: 10px 0 -5px 10px; font-size: 11px; color: var(--muted);
  display: inline-flex; align-items: center; gap: 3px; white-space: nowrap; position: relative; top: 3px;
}
.meta .src { font-style: italic; opacity: .8; }
.tick { display: inline-flex; color: var(--muted); }
.tick.read { color: var(--tick-read); }
.tick.failed { color: var(--danger); }
.err { color: var(--danger); font-size: 12px; margin-top: 4px; clear: both; }

.quote {
  display: block; border-left: 4px solid var(--accent); background: var(--quote-bg); border-radius: 6px;
  padding: 5px 8px; margin-bottom: 5px; font-size: 13px; color: var(--muted); max-height: 64px; overflow: hidden; cursor: pointer;
}
.quote .who { color: var(--accent); font-weight: 500; font-size: 12.8px; }
.quote.mine { border-left-color: #53bdeb; }
.quote.mine .who { color: #53bdeb; }

.media { margin: -3px -4px 4px; border-radius: 6px; overflow: hidden; }
.media img, .media video { display: block; max-width: 100%; max-height: 340px; min-width: 120px; border-radius: 6px; cursor: pointer; background: var(--quote-bg); }
.media video { cursor: auto; }
.media.sticker img { max-width: 150px; max-height: 150px; background: none; }
.media audio { display: block; width: 280px; max-width: 100%; margin: 4px; }
.media .placeholder {
  display: grid; place-items: center; width: 240px; max-width: 100%; height: 140px; background: var(--quote-bg);
  color: var(--muted); font-size: 13px; border-radius: 6px;
}
.doc {
  display: flex; align-items: center; gap: 10px; padding: 10px; border-radius: 6px; background: var(--quote-bg);
  color: inherit; text-decoration: none; min-width: 220px; cursor: pointer;
}
.doc .ico { font-size: 28px; }
.doc .doc-name { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 260px; }
.doc .doc-sub { font-size: 12px; color: var(--muted); }

.actions {
  position: absolute; top: 4px; display: none; gap: 2px;
}
.msg.out .actions { right: calc(100% + 6px); }
.msg.in .actions { left: calc(100% + 6px); }
.bubble:hover .actions, .bubble:focus-within .actions { display: flex; }
.actions button {
  border: 0; width: 28px; height: 28px; border-radius: 50%; background: var(--panel); box-shadow: var(--shadow);
  display: grid; place-items: center; font-size: 15px; color: var(--muted);
}
.reactions {
  position: absolute; bottom: -21px; display: flex; gap: 1px; padding: 1px 5px; border-radius: 12px;
  background: var(--panel); box-shadow: var(--shadow); font-size: 14px; border: 1px solid var(--border);
}
.msg.out .reactions { right: 10px; }
.msg.in .reactions { left: 10px; }
.reactions .mine { filter: drop-shadow(0 0 0 transparent); }

.notice {
  text-align: center; font-size: 13px; padding: 8px 16px; background: var(--panel-head); color: var(--muted);
  border-top: 1px solid var(--border);
}
.compose-extra {
  display: flex; align-items: center; gap: 10px; padding: 8px 16px; background: var(--panel-head);
  border-top: 1px solid var(--border);
}
.quote-preview { flex: 1; min-width: 0; border-left: 4px solid var(--accent); background: var(--quote-bg); border-radius: 6px; padding: 6px 10px; }
.quote-who { color: var(--accent); font-size: 13px; font-weight: 500; }
.quote-text { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.attach-preview { flex: 1; display: flex; align-items: center; gap: 12px; min-width: 0; font-size: 13.5px; }
.attach-preview img, .attach-preview video { width: 64px; height: 64px; object-fit: cover; border-radius: 6px; }
.attach-preview .fname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attach-preview .fsize { color: var(--muted); font-size: 12px; }

.composer {
  display: flex; align-items: flex-end; gap: 8px; padding: 8px 16px 10px; background: var(--panel-head);
}
.composer textarea {
  flex: 1; resize: none; border: 0; outline: none; border-radius: 8px; padding: 9px 12px; background: var(--panel);
  max-height: 140px; line-height: 20px;
}
.send-btn { border: 0; background: none; color: var(--muted); padding: 8px; border-radius: 50%; display: grid; place-items: center; }
.send-btn:not(:disabled):hover { color: var(--accent); }
.send-btn:disabled { opacity: .4; cursor: default; }

/* ---- floating ---- */
.react-picker {
  position: fixed; z-index: 20; display: flex; gap: 2px; padding: 6px 8px; border-radius: 26px;
  background: var(--panel); box-shadow: 0 2px 10px rgb(11 20 26 / .2);
}
.react-picker button {
  border: 0; background: none; font-size: 22px; width: 38px; height: 38px; border-radius: 50%;
  transition: transform .1s;
}
.react-picker button:hover { transform: scale(1.2); background: var(--hover); }
.react-picker button.remove { font-size: 15px; color: var(--muted); }
.react-picker button.current { background: var(--active); }

.lightbox {
  position: fixed; inset: 0; z-index: 30; background: rgb(0 0 0 / .85); display: grid; place-items: center; cursor: zoom-out;
}
.lightbox img { max-width: 94vw; max-height: 92vh; }

dialog {
  border: 0; border-radius: 10px; padding: 18px; width: min(440px, calc(100vw - 32px));
  background: var(--panel); color: var(--text); box-shadow: 0 10px 40px rgb(0 0 0 / .3);
}
dialog::backdrop { background: rgb(0 0 0 / .4); }
dialog h3 { margin: 0; font-weight: 500; }
.dlg-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
.dlg-note { color: var(--muted); font-size: 13px; margin: 6px 0 12px; }
dialog input {
  width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 9px 12px; background: var(--panel-head); outline: none;
}
.dlg-actions { display: flex; justify-content: flex-end; margin-top: 12px; }
.primary { border: 0; border-radius: 20px; padding: 8px 22px; background: var(--accent); color: var(--accent-text); font-weight: 600; }
.pick { max-height: 50vh; overflow-y: auto; margin-top: 8px; }
.pick button {
  display: flex; align-items: center; gap: 12px; width: 100%; border: 0; background: none; padding: 9px 6px; border-radius: 8px; text-align: left;
}
.pick button:hover { background: var(--hover); }
.pick .p-sub { color: var(--muted); font-size: 13px; }

.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); z-index: 40;
  background: #323739; color: #fff; padding: 10px 18px; border-radius: 8px; font-size: 14px; max-width: 90vw;
  box-shadow: 0 4px 14px rgb(0 0 0 / .3);
}

/* ---- narrow screens: list OR conversation ---- */
@media (max-width: 780px) {
  .app { grid-template-columns: 56px 1fr; }
  .conv { display: none; }
  .app.show-conv { grid-template-columns: 1fr; }
  .app.show-conv .rail, .app.show-conv .side { display: none; }
  .app.show-conv .conv { display: flex; }
  .conv-head .back { display: grid; }
  .messages { padding: 10px 12px; }
  .bubble { max-width: 85%; }
  /* Tap a message to show react / reply. */
  .bubble:hover .actions, .bubble:focus-within .actions { display: none; }
  .msg.show-actions .actions { display: flex; position: static; margin-top: 4px; clear: both; }
  .msg.out .actions, .msg.in .actions { right: auto; left: auto; }
  .actions button { width: 26px; height: 26px; font-size: 13px; }
}
