:root {
  --bg: #0e1116;
  --bg-2: #151a21;
  --bg-3: #1c232c;
  --line: #262f3a;
  --text: #e6edf3;
  --text-dim: #8b98a8;
  --green: #25d366;
  --green-dim: #128c4a;
  --blue: #4c8dff;
  --danger: #ff6b6b;
  --radius: 12px;
}

* { box-sizing: border-box; }

/*
 * `hidden` is a UA style with low specificity, so any rule that sets `display`
 * silently overrides it — this bit us on .app, .field and .sheet-card. A single
 * high-specificity reset keeps `hidden` authoritative everywhere.
 */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  /* Mobile: keep the layout above the home indicator / keyboard. */
  overscroll-behavior: none;
}

.app {
  height: 100vh;
  /* Set from visualViewport in app.js so the keyboard cannot cover the composer. */
  height: var(--app-height, 100vh);
  height: var(--app-height, 100dvh);
  display: grid;
  grid-template-columns: 72px 300px 320px 1fr;
  overflow: hidden;
}

/*
 * Top inset for the status bar / notch.
 *
 * viewport-fit=cover lets the page paint under the system bars, which is what
 * we want for the background — but the content still has to start below them.
 * Without this the chat header sits under the clock and is unreadable in the
 * Android shell and on iOS.
 */
:root {
  --sai-top: env(safe-area-inset-top, 0px);
  --sai-bottom: env(safe-area-inset-bottom, 0px);
}

/*
 * Push content below the status bar.
 *
 * `env()` is 0 inside an Android WebView, so push.js overwrites --sai-top with
 * the value reported by the native shell. Applied to the scrollable panes'
 * first child rather than the root, so the background still fills the notch.
 */
.panel-head,
.list-pane .search-wrap,
.chat-head {
  padding-top: calc(var(--sai-top) + 16px);
}

/* The rail is a sidebar on desktop and a bottom bar on phones. */
.rail {
  padding-top: calc(var(--sai-top) + 14px);
  padding-bottom: calc(6px + var(--sai-bottom));
}
/* `hidden` loses to `display:grid` — restate it or the inbox flashes pre-login. */
.app[hidden] { display: none; }

/* Grid/flex children default to min-height:auto and will happily grow past the
   viewport when their content is long — pin them to zero so inner panes scroll. */
.rail,
.panel,
.list-pane,
.chat-pane,
.account-list,
.chat-list,
.messages {
  min-height: 0;
  min-width: 0;
}

/* ------------------------------------------------------------------ rail */

.rail {
  background: var(--bg-2);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 14px 0;
  gap: 12px;
}

.rail-head {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.logo {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--green), var(--green-dim));
  color: #04120a;
  font-weight: 800;
  font-size: 13px;
  display: grid;
  place-items: center;
  letter-spacing: .5px;
}

.icon-btn {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  border: 1px dashed var(--line);
  background: transparent;
  color: var(--text-dim);
  font-size: 18px;
  cursor: pointer;
  transition: .15s;
}
.icon-btn:hover { color: var(--green); border-color: var(--green); }

.account-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  align-items: center;
  overflow-y: auto;
  flex: 1;
}

.rail-foot {
  margin-top: auto;
  padding-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
}

.account-chip {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: var(--bg-3);
  border: 1px solid transparent;
  color: var(--text);
  font-weight: 700;
  cursor: pointer;
  position: relative;
  display: grid;
  place-items: center;
  transition: .15s;
}
.account-chip:hover { border-color: var(--line); }
.account-chip.active { border-color: var(--green); background: #14251b; }

.account-chip .dot {
  position: absolute;
  right: -2px;
  bottom: -2px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 2px solid var(--bg-2);
  background: var(--text-dim);
}
.account-chip[data-status="connected"] .dot { background: var(--green); }
.account-chip[data-status="qr"] .dot { background: #f5c451; }
.account-chip[data-status="connecting"] .dot { background: #f5c451; animation: pulse 1s infinite; }
.account-chip[data-status="logged_out"] .dot { background: var(--danger); }
.account-chip[data-status="error"] .dot { background: var(--danger); }

@keyframes pulse { 50% { opacity: .3; } }

/* ----------------------------------------------------------------- panel */

.panel {
  background: var(--bg-2);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  position: relative;
  min-width: 0;
}

.panel-head {
  padding: 16px;
  border-bottom: 1px solid var(--line);
}

.account-title {
  font-size: 15px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* The account panel no longer holds settings; keep its content top-aligned so
   the header does not float in the middle of a tall column. */
.panel {
  justify-content: flex-start;
}

.account-meta {
  margin-top: 4px;
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.head-actions {
  margin-top: 12px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.btn {
  border: 1px solid var(--line);
  background: var(--bg-3);
  color: var(--text);
  padding: 7px 14px;
  border-radius: 9px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  transition: .15s;
}
.btn:hover { border-color: var(--text-dim); }
.btn.primary { background: var(--green); border-color: var(--green); color: #04120a; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.primary:disabled { opacity: .45; cursor: not-allowed; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn.danger:hover { border-color: var(--danger); }

/* ------------------------------------------------------------------- qr */

/* Fixed to the viewport so the code is large and unobstructed while scanning —
   anchoring it to the 300px account panel clipped the card. */
.qr-box {
  position: fixed;
  inset: 0;
  background: rgba(10, 13, 17, .9);
  backdrop-filter: blur(4px);
  display: grid;
  place-items: center;
  padding: 20px;
  z-index: 200;
}
.qr-box[hidden] { display: none; }

.qr-card {
  background: #fff;
  color: #111;
  border-radius: 16px;
  padding: 20px;
  text-align: center;
  max-width: 300px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .5);
  position: relative;
}

/* The overlay covers the whole viewport, so it needs its own way out. */
.qr-close {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 50%;
  background: #f0f0f0;
  color: #666;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: .15s;
}
.qr-close:hover { background: #e0e0e0; color: #111; }

.qr-cancel {
  margin-top: 14px;
  width: 100%;
  color: #666;
  border-color: #ddd;
  font-size: 12.5px;
}
.qr-cancel:hover { color: #c0392b; border-color: #c0392b; }

.qr-title { font-weight: 700; margin-bottom: 12px; font-size: 14px; }
.qr-card img { width: 240px; height: 240px; display: block; margin: 0 auto; image-rendering: pixelated; }
.qr-hint { margin-top: 12px; font-size: 11px; color: #555; line-height: 1.6; }
.qr-expired { margin-top: 8px; font-size: 12px; color: #c0392b; font-weight: 600; }

/* ----------------------------------------------------------------- empty */

.empty {
  flex: 1;
  display: grid;
  place-items: center;
  padding: 24px;
}
.empty[hidden] { display: none; }
.empty-inner { text-align: center; color: var(--text-dim); }
.empty-icon { font-size: 40px; }
.empty-inner h2 { color: var(--text); font-size: 16px; margin: 12px 0 6px; }
.empty-inner p { margin: 0; font-size: 13px; }

/* ------------------------------------------------------------------ list */

.list-pane {
  background: var(--bg);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.search-wrap {
  padding: 12px;
  border-bottom: 1px solid var(--line);
}

#search {
  width: 100%;
  background: var(--bg-3);
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 9px 12px;
  color: var(--text);
  font-size: 13px;
  outline: none;
}
#search:focus { border-color: var(--green-dim); }

.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px;
}

.chat-item {
  display: grid;
  grid-template-columns: 40px 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 10px;
  border-radius: 10px;
  cursor: pointer;
  transition: .12s;
}
.chat-item:hover { background: var(--bg-2); }
.chat-item.active { background: #14251b; }

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--bg-3);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 14px;
  color: var(--text-dim);
  flex: none;
}
.chat-item.active .avatar { background: var(--green-dim); color: #04120a; }

.chat-main { min-width: 0; }
/* List-row name line. The chat header has its own .chat-name-row later, so
   scope this one to the list to avoid the two fighting over layout. */
.chat-name-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: baseline;
}
.chat-item .chat-name-row .name {
  font-weight: 600;
  font-size: 13.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1 1 auto;
  min-width: 0;
}
.chat-item .chat-name-row .time { font-size: 11px; color: var(--text-dim); flex: none; }
.chat-preview {
  font-size: 12.5px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.badge {
  background: var(--green);
  color: #04120a;
  font-size: 11px;
  font-weight: 700;
  border-radius: 999px;
  padding: 1px 7px;
  min-width: 20px;
  text-align: center;
}

/* ------------------------------------------------------------------ chat */

.chat-pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg-2);
}

.chat-head {
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  min-height: 62px;
  display: flex;
  align-items: center;
}

.chat-name { font-weight: 700; font-size: 15px; }
.chat-sub { font-size: 12px; color: var(--text-dim); margin-top: 2px; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.placeholder {
  margin: auto;
  color: var(--text-dim);
  font-size: 13px;
}

.msg {
  max-width: min(70%, 560px);
  padding: 8px 12px;
  border-radius: 12px;
  background: var(--bg-3);
  align-self: flex-start;
  position: relative;
  animation: fade .15s ease-out;
}
@keyframes fade { from { opacity: 0; transform: translateY(3px); } }

.msg.mine {
  align-self: flex-end;
  background: #10361f;
  border: 1px solid #1d5733;
}

.msg .sender {
  font-size: 11px;
  font-weight: 700;
  color: var(--green);
  margin-bottom: 2px;
}

.msg .text {
  font-size: 13.5px;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg .meta {
  font-size: 10px;
  color: var(--text-dim);
  margin-top: 4px;
  text-align: right;
  display: flex;
  gap: 4px;
  justify-content: flex-end;
  align-items: center;
}

.msg .tick { font-size: 11px; }
.msg .tick.read { color: var(--blue); }

.day-divider {
  align-self: center;
  font-size: 11px;
  color: var(--text-dim);
  background: var(--bg-3);
  padding: 3px 12px;
  border-radius: 999px;
  margin: 10px 0;
}

.msg img.thumb {
  max-width: 100%;
  border-radius: 8px;
  display: block;
  margin-bottom: 4px;
  cursor: pointer;
}

/* Stored-but-not-downloaded media: a button instead of a dead placeholder. */
.media-stub {
  display: inline-block;
  margin: 2px 0 4px;
  padding: 9px 14px;
  border: 1px dashed var(--line);
  border-radius: 9px;
  background: transparent;
  color: var(--text-dim);
  font-size: 12.5px;
  cursor: pointer;
  transition: .15s;
}
.media-stub:hover:not(:disabled) { color: var(--green); border-color: var(--green-dim); }
.media-stub:disabled { opacity: .6; cursor: progress; }
.media-stub.failed { color: var(--danger); border-color: #4a2b2b; cursor: default; }

.msg audio { max-width: 100%; margin-bottom: 4px; }
.msg a.file-link { color: var(--blue); }

/* -------------------------------------------------------------- composer */

.composer {
  border-top: 1px solid var(--line);
  padding: 12px;
  background: var(--bg-2);
}
.composer[hidden] { display: none; }

#input {
  flex: 1;
  min-width: 0;
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  color: var(--text);
  font: inherit;
  /* An explicit line-height keeps scrollHeight predictable for autoGrow(). */
  line-height: 1.5;
  resize: none;
  overflow-y: auto;
  /* One line plus padding; grows from here as the user types. */
  min-height: 44px;
  max-height: 160px;
  outline: none;
}
#input:focus { border-color: var(--green-dim); }

.conn-hint {
  padding: 12px;
  text-align: center;
  font-size: 12px;
  color: var(--text-dim);
  border-top: 1px solid var(--line);
}
.conn-hint[hidden] { display: none; }

/* ----------------------------------------------------------------- toast */

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #242c36;
  border: 1px solid var(--line);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 13px;
  z-index: 100;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .4);
}
.toast[hidden] { display: none; }

::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-thumb { background: #2b3541; border-radius: 6px; }
::-webkit-scrollbar-thumb:hover { background: #3a4756; }
::-webkit-scrollbar-track { background: transparent; }

/* The back button only exists in the phone layout; the media query below flips
   it on. Keep this base rule *before* that query — equal specificity means the
   later rule wins, which would otherwise hide it on phones too. */
.back-btn {
  display: none;
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 24px;
  line-height: 1;
  border-radius: 8px;
  cursor: pointer;
  place-items: center;
  flex: none;
  margin-right: 2px;
}
.back-btn:hover { color: var(--text); background: var(--bg-3); }

.attach {
  width: 36px;
  height: 36px;
  flex: none;
  border-style: solid;
  font-size: 16px;
  border-radius: 10px;
}

/* ------------------------------------------------------------ responsive */

/* Tablet: drop the account panel, keep rail + list + chat. */
@media (max-width: 1100px) {
  .app { grid-template-columns: 64px 280px 1fr; }
  .chat-pane { display: none; }
  .app.chat-open .chat-pane { display: flex; grid-column: 3; }
  .app.chat-open .list-pane { display: none; }
}

/* Phone: one pane at a time. The list is the home screen; opening a chat
   slides it over, and the back button returns. */
@media (max-width: 720px) {
  /*
   * Fixed heights are what break this layout on phones: 100dvh changes while
   * the URL bar collapses and while the keyboard opens, and the bottom rail is
   * overlaid rather than reserved. Using a real grid row for the rail means the
   * panes never need a magic padding number and nothing is ever hidden under it.
   */
  .app {
    /* Flex column instead of grid: the desktop rule defines four columns, and
       fighting it with overrides on a narrow screen kept leaking the 75px rail
       column into the panes. A flex column has no track list to inherit. */
    display: flex;
    flex-direction: column;
    grid-template-columns: none;
    grid-template-rows: none;
    height: var(--app-height, 100dvh);
    max-height: var(--app-height, 100dvh);
    overflow: hidden;
  }

  .rail {
    position: static;
    order: 2;
    grid-row: auto;
    grid-column: auto;
    width: 100%;
    flex-direction: row;
    align-items: center;
    /* Top inset does not apply here: on phones the rail is at the bottom. */
    padding: 6px 10px;
    padding-bottom: calc(6px + var(--sai-bottom));
    gap: 8px;
    border-right: none;
    border-top: 1px solid var(--line);
    background: var(--bg-2);
    z-index: 60;
    flex: none;
  }
  .rail-head { flex-direction: row; gap: 8px; flex: none; }
  .rail .logo { width: 30px; height: 30px; font-size: 11px; border-radius: 8px; }

  /*
   * The account strip scrolls; it must be able to shrink below its content
   * width, otherwise the last chip is clipped by the rail edge instead of
   * being reachable by scrolling.
   */
  .account-list {
    flex-direction: row;
    flex: 1 1 0;
    min-width: 0;
    gap: 6px;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 0 2px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    justify-content: flex-start;
  }
  .account-list::-webkit-scrollbar { display: none; }
  .account-chip { width: 38px; height: 38px; font-size: 12px; border-radius: 10px; flex: 0 0 38px; }

  .rail-foot {
    margin-top: 0;
    padding-top: 0;
    flex-direction: row;
    gap: 4px;
    flex: none;
  }
  .rail .icon-btn { width: 32px; height: 32px; font-size: 16px; border-radius: 9px; }

  /* Panes fill the flexible column; the rail sits after them at order 2. */
  .list-pane,
  .chat-pane,
  .panel {
    order: 1;
    grid-column: auto;
    grid-row: auto;
    width: 100%;
    flex: 1 1 auto;
    min-height: 0;
    padding-bottom: 0;
  }

  .panel { display: none; }

  .list-pane {
    display: flex;
    border-right: none;
  }
  .chat-pane { display: none; }

  .app.chat-open .list-pane { display: none; }
  .app.chat-open .chat-pane { display: flex; }

  .back-btn { display: grid; }
  .chat-head {
    /* Keep the status-bar inset here; the desktop rule above is overridden. */
    padding: calc(var(--sai-top) + 8px) 10px 8px;
    min-height: 54px;
    gap: 6px;
    position: sticky;
    top: 0;
    background: var(--bg-2);
    z-index: 5;
    flex: none;
  }
  .chat-name { font-size: 14.5px; }
  .chat-sub { font-size: 11px; }
  .chat-actions { gap: 4px; }
  .btn.small { padding: 6px 9px; font-size: 12px; }

  /*
   * The message area must be the only scrolling region. `min-height: 0` keeps a
   * flex child from refusing to shrink, which is what made long chats push the
   * composer off-screen.
   */
  .messages {
    flex: 1 1 auto;
    min-height: 0;
    padding: 12px 10px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }
  .msg { max-width: 88%; }
  .msg img.thumb { max-height: 46vh; object-fit: cover; }
  .msg audio { width: 100%; }
  .msg video { max-width: 100%; }

  .composer {
    flex: none;
    padding: 8px 10px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
  }
  /* 16px stops iOS from zooming on focus; the shorter placeholder avoids the
     two-line wrap that made the composer look cramped. */
  #input { font-size: 16px; }
  .composer-row { gap: 6px; }
  .attach { width: 34px; height: 34px; font-size: 15px; flex: none; }
  #sendBtn {
    padding: 0;
    width: 56px;
    height: 40px;
    flex: none;
    font-size: 14px;
  }
  .composer-hint { font-size: 11px; margin-top: 5px; }

  .search-wrap { padding: calc(var(--sai-top) + 10px) 10px 10px; flex: none; }
  #search { font-size: 16px; } /* same reason as the composer */

  .list-pane .chat-list { flex: 1 1 auto; min-height: 0; }

  /* Sheets become bottom sheets, which is what phones expect. */
  .sheet {
    align-items: flex-end;
    padding: 0;
  }
  .sheet-card,
  .sheet-card.narrow,
  .sheet-card.wide {
    width: 100%;
    max-height: 88dvh;
    border-radius: 16px 16px 0 0;
  }
  .sheet-body { padding-bottom: calc(16px + env(safe-area-inset-bottom)); }

  /* The two-column group manager cannot fit a phone; stack it. */
  .group-layout {
    grid-template-columns: 1fr;
    max-height: none;
  }
  .group-list-col {
    border-right: none;
    border-bottom: 1px solid var(--line);
    max-height: 34dvh;
  }

  .login { padding: 16px; }
  .login-card { padding: 26px 20px; }
  #loginPassword { font-size: 16px; }

  /* Above the rail, not behind it. */
  .toast {
    bottom: calc(72px + env(safe-area-inset-bottom));
    left: 12px;
    right: 12px;
    transform: none;
    text-align: center;
  }

  .region-chip { display: none; }
  .chat-tags .chat-group-tag { font-size: 9px; }

  .bc-preview-out, .bc-progress-list { max-height: 40dvh; }
  .broadcast-body { max-height: 88dvh; }
}

/* Very short screens (older phones in landscape): shrink chrome further. */
@media (max-width: 720px) and (max-height: 480px) {
  .chat-head { min-height: 44px; padding: 4px 8px; }
  .messages { padding: 8px; }
  .composer { padding: 4px 8px; }
}

/* ---------------------------------------------------------- groups & send */

.panel-groups {
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: none;
}

.btn.block { width: 100%; text-align: center; }

.group-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-height: 132px;
  overflow-y: auto;
}

.group-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  border: 1px solid var(--line);
  background: var(--bg-3);
  color: var(--text-dim);
  font-size: 11.5px;
  padding: 4px 9px;
  border-radius: 999px;
  cursor: pointer;
  max-width: 100%;
  transition: .15s;
}
.group-chip:hover { color: var(--text); }
.group-chip.active { color: #04120a; background: var(--green); border-color: var(--green); }
.group-chip .gcount { opacity: .7; font-size: 10.5px; }
.group-chip .gdot {
  width: 7px; height: 7px; border-radius: 50%; flex: none;
}

/* Chat row badge showing which group a conversation belongs to. */
.chat-tags { display: flex; gap: 4px; margin-top: 4px; flex-wrap: wrap; align-items: center; }
.chat-groups { display: flex; gap: 4px; margin-top: 3px; flex-wrap: wrap; }
.chat-group-tag {
  font-size: 9.5px;
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--bg-3);
  color: var(--text-dim);
  border: 1px solid var(--line);
}

/* ---------------------------------------------------------- region badge */

/* Country + language derived from the calling code. Kept small in list rows so
   it does not crowd the name; the full variant is for the chat header. */
.region-chip {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: 10px;
  line-height: 1.4;
  padding: 0 5px;
  border-radius: 4px;
  background: var(--bg-3);
  color: var(--text-dim);
  border: 1px solid var(--line);
  white-space: nowrap;
  flex: none;
  vertical-align: 1px;
}

.region-chip .rflag { font-size: 11px; line-height: 1; }

.region-chip.full {
  font-size: 11px;
  padding: 1px 7px;
  gap: 4px;
  margin-left: 8px;
}
.region-chip.full .rflag { font-size: 12px; }
.region-chip.full .rcountry { color: var(--text); }
.region-chip.full .rlang {
  color: var(--text-dim);
  padding-left: 5px;
  border-left: 1px solid var(--line);
}

/* The chat header subtitle: number on the left, region badge aligned after it
   without wrapping onto its own line on desktop. */
.chat-sub { display: flex; align-items: center; flex-wrap: wrap; gap: 0 2px; }
.chat-sub .sub-id { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* In a chat row the name must still win the space fight. */
.region-chip.join { margin: 0; }

/* ------------------------------------------------------------- group sheet */

.sheet-card.wide { width: min(880px, 100%); }

.group-layout {
  display: grid;
  grid-template-columns: 220px 1fr;
  min-height: 380px;
  max-height: min(600px, 84vh);
  overflow: hidden;
}

.group-list-col {
  border-right: 1px solid var(--line);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
}

.group-manage-list { display: flex; flex-direction: column; gap: 4px; }

.group-manage-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 9px;
  cursor: pointer;
  font-size: 13px;
  border: 1px solid transparent;
}
.group-manage-item:hover { background: var(--bg-3); }
.group-manage-item.active { background: #14251b; border-color: var(--green-dim); }
.group-manage-item .gname {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.group-manage-item .gcount { font-size: 11px; color: var(--text-dim); flex: none; }

.group-detail-col { padding: 14px 16px; overflow-y: auto; display: flex; flex-direction: column; }

.group-detail-head {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.group-name-input {
  flex: 1;
  min-width: 140px;
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px 11px;
  color: var(--text);
  font-size: 13.5px;
  font-weight: 600;
  outline: none;
}
.group-name-input:focus { border-color: var(--green-dim); }

.group-detail-actions { display: flex; gap: 6px; }

.group-add-row { margin-bottom: 8px; }
#groupChatSearch, #bcChatSearch {
  width: 100%;
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px 11px;
  color: var(--text);
  font-size: 12.5px;
  outline: none;
}

.group-pick-list {
  max-height: 190px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 4px;
  margin-bottom: 14px;
}

.pick-row {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 7px 9px;
  border-radius: 8px;
  font-size: 12.5px;
  cursor: pointer;
}
.pick-row:hover { background: var(--bg-3); }
.pick-row .pname { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pick-row .pphone { color: var(--text-dim); font-size: 11px; flex: none; }
.pick-row input[type="checkbox"] { accent-color: var(--green); flex: none; }
.pick-row.joined { opacity: .5; }

.group-member-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 12.5px;
  color: var(--text-dim);
  margin-bottom: 8px;
}

.group-member-list {
  display: flex;
  flex-direction: column;
  gap: 3px;
  max-height: 200px;
  overflow-y: auto;
}

.member-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 9px;
  border-radius: 8px;
  font-size: 12.5px;
}
.member-row:hover { background: var(--bg-3); }
.member-row .mname { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.member-row .mlang {
  font-size: 10.5px;
  color: var(--text-dim);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 1px 5px;
  flex: none;
}

/* --------------------------------------------------------- broadcast sheet */

.broadcast-body { max-height: min(640px, 86vh); }

.bc-tabs { display: flex; gap: 6px; margin-bottom: 14px; }
.bc-tab {
  flex: 1;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--text-dim);
  font-size: 13px;
  font-weight: 600;
  padding: 9px;
  border-radius: 9px;
  cursor: pointer;
  transition: .15s;
}
.bc-tab:hover { color: var(--text); }
.bc-tab.active { background: #14251b; border-color: var(--green-dim); color: var(--green); }

.bc-group-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 260px;
  overflow-y: auto;
  margin-bottom: 10px;
}

.bc-group-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
  font-size: 13px;
}
.bc-group-row:hover { background: var(--bg-3); }
.bc-group-row.active { border-color: var(--green); background: #14251b; }
.bc-group-row .bgn { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bc-group-row .bgc { font-size: 11.5px; color: var(--text-dim); flex: none; }

.bc-chat-list {
  max-height: 250px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 4px;
  margin: 8px 0;
}

.bc-selected-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 12.5px;
  color: var(--text-dim);
  margin-bottom: 10px;
}

.bc-preview-box { margin-top: 12px; }

.bc-preview-out {
  margin-top: 10px;
  max-height: 200px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px;
}
.bc-preview-out[hidden] { display: none; }

.bc-preview-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 6px;
  font-size: 12px;
  border-bottom: 1px solid var(--line);
}
.bc-preview-row:last-child { border-bottom: none; }
.bc-preview-row .bpname { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bc-preview-row .bplang {
  font-size: 10.5px;
  color: var(--green);
  border: 1px solid var(--green-dim);
  border-radius: 4px;
  padding: 0 5px;
  flex: none;
}

.bc-field { display: block; margin-bottom: 16px; }
.bc-field .field-label { display: block; margin-bottom: 7px; font-size: 12.5px; color: var(--text-dim); }

#bcText {
  width: 100%;
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 11px 13px;
  color: var(--text);
  font: inherit;
  font-size: 13.5px;
  resize: vertical;
  outline: none;
  min-height: 96px;
}
#bcText:focus { border-color: var(--green-dim); }

#bcImage { color: var(--text-dim); font-size: 12.5px; }

.bc-image-preview { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
.bc-image-preview[hidden] { display: none; }
.bc-image-preview img {
  width: 88px; height: 88px;
  object-fit: cover;
  border-radius: 9px;
  border: 1px solid var(--line);
}

.bc-warn {
  font-size: 11.5px;
  line-height: 1.7;
  color: #f0c36d;
  background: #2a2317;
  border: 1px solid #4a3d22;
  border-radius: 9px;
  padding: 9px 12px;
  margin-bottom: 16px;
}

.bc-progress-head { margin-bottom: 10px; }
.bc-progress-title { font-size: 14px; font-weight: 700; margin-bottom: 4px; }
.bc-progress-counts { font-size: 12.5px; color: var(--text-dim); }

.bc-bar {
  height: 8px;
  background: var(--bg-3);
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 14px;
}
.bc-bar-fill {
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, var(--green-dim), var(--green));
  transition: width .25s;
}

.bc-progress-list {
  max-height: 320px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 6px;
}

.bc-progress-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  font-size: 12.5px;
  border-bottom: 1px solid var(--line);
}
.bc-progress-row:last-child { border-bottom: none; }
.bc-progress-row .bprname { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bc-progress-row .bprstate { font-size: 11px; flex: none; }
.bc-progress-row .bprstate.sent { color: var(--green); }
.bc-progress-row .bprstate.failed { color: var(--danger); }
.bc-progress-row .bprstate.skipped { color: var(--text-dim); }
.bc-progress-row .bprstate.pending { color: var(--text-dim); }

/* -------------------------------------------------------------- settings */

/* Settings live in a paged sheet (opened from the gear) so the account
   panel stays short. */

/* ----------------------------------------------------------------- sheet */

.sheet {
  position: fixed;
  inset: 0;
  z-index: 400;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.sheet[hidden] { display: none; }

.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(6, 9, 13, .72);
  backdrop-filter: blur(3px);
}

.sheet-card {
  position: relative;
  width: min(520px, 100%);
  max-height: min(560px, 90vh);
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, .55);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.sheet-card.narrow { width: min(360px, 100%); }

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}
.sheet-title { font-weight: 700; font-size: 14.5px; }

.sheet-close {
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-dim);
  font-size: 15px;
  cursor: pointer;
}
.sheet-close:hover { color: var(--text); background: var(--bg-3); }

.sheet-tabs {
  display: flex;
  gap: 4px;
  padding: 8px 12px 0;
  border-bottom: 1px solid var(--line);
}

.sheet-tab {
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 13px;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: 8px 8px 0 0;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.sheet-tab:hover { color: var(--text); }
.sheet-tab.active { color: var(--green); border-bottom-color: var(--green); }

.sheet-body {
  padding: 16px;
  overflow-y: auto;
}
.sheet-page[hidden] { display: none; }

.field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
}
.field-label { font-size: 13px; color: var(--text); }
.field-value { font-size: 12.5px; color: var(--text-dim); text-align: right; word-break: break-all; }

.field select {
  background: var(--bg-3);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 12.5px;
  outline: none;
  max-width: 55%;
}
.field input[type="checkbox"] {
  accent-color: var(--green);
  width: 16px;
  height: 16px;
  cursor: pointer;
  flex: none;
}

.field-hint {
  margin: -4px 0 12px;
  font-size: 11.5px;
  line-height: 1.6;
  color: var(--text-dim);
}

.field-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding: 9px 0;
  border-bottom: 1px solid var(--line);
}
.field-row:last-of-type { border-bottom: none; }

.sheet-actions { display: flex; gap: 10px; margin-top: 16px; flex-wrap: wrap; }
.sheet-actions .btn { flex: 1; min-width: 130px; }

.test-out {
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--bg-3);
  font-size: 12.5px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}
.test-out[hidden] { display: none; }

.test-out[data-kind="ok"] { color: #b8f0cd; border-left: 3px solid var(--green); }
.test-out[data-kind="warn"] { color: var(--amber); border-left: 3px solid var(--amber); }

/* ------------------------------------------------------- llm + push blocks */

.llm-actions, .push-actions {
  display: flex;
  gap: 8px;
  margin: 12px 0 16px;
  flex-wrap: wrap;
}
.llm-actions .btn, .push-actions .btn { flex: 1; min-width: 120px; }

#llmApiKey, #llmModel, #llmBaseUrl {
  width: 100%;
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 9px 11px;
  color: var(--text);
  font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  outline: none;
}
#llmApiKey:focus, #llmModel:focus, #llmBaseUrl:focus { border-color: var(--green2); }

.push-guide {
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
  margin-top: 14px;
  font-size: 12.5px;
  line-height: 1.7;
}
.push-guide b { color: var(--text); }
.push-guide ol { margin: 8px 0 0; padding-left: 20px; color: var(--text-dim); }
.push-guide li { margin-bottom: 4px; }

/* ------------------------------------------------------------ remark edit */

.chat-name-row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.remark-btn {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--text-dim);
  font-size: 11.5px;
  padding: 2px 8px;
  border-radius: 7px;
  cursor: pointer;
  flex: none;
  transition: .15s;
}
.remark-btn:hover { color: var(--green); border-color: var(--green-dim); }

.chat-actions { margin-left: auto; display: flex; gap: 8px; flex: none; }
.btn.small { padding: 5px 11px; font-size: 12px; }
.btn.ghost.on {
  border-color: var(--green-dim);
  color: var(--green);
  background: #14251b;
}

/* ------------------------------------------------------------- translation */

.translation {
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px dashed var(--line);
  font-size: 12.5px;
  line-height: 1.45;
  color: #a9d5bb;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.mine .translation { color: #9fd3b0; }

.translation .tr-label {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  color: var(--bg);
  background: var(--green);
  border-radius: 4px;
  padding: 0 4px;
  margin-right: 5px;
  vertical-align: 1px;
}

.tr-btn {
  margin-top: 5px;
  border: 1px dashed var(--line);
  background: transparent;
  color: var(--text-dim);
  font-size: 11px;
  line-height: 1;
  padding: 3px 8px;
  border-radius: 999px;
  cursor: pointer;
  transition: .15s;
  display: inline-block;
}
.tr-btn:hover:not(:disabled) { color: var(--green); border-color: var(--green-dim); }
.tr-btn:disabled { opacity: .6; cursor: progress; }

.composer-row { display: flex; gap: 10px; align-items: flex-end; }

.composer-hint {
  margin-top: 7px;
  font-size: 11.5px;
  color: var(--green);
}
.composer-hint[hidden] { display: none; }

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 500;
  background: rgba(0, 0, 0, .92);
  display: grid;
  place-items: center;
  padding: 20px;
  cursor: zoom-out;
}
.lightbox[hidden] { display: none; }
.lightbox img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 8px;
  object-fit: contain;
}

/* ----------------------------------------------------------------- login */

.login {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: radial-gradient(1200px 600px at 50% -10%, #1b2b22, var(--bg) 60%);
  z-index: 500;
  padding: 24px;
}
.login[hidden] { display: none; }

.login-card {
  width: min(360px, 100%);
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 30px 26px;
  box-shadow: 0 30px 70px rgba(0, 0, 0, .5);
  text-align: center;
}

.login-logo {
  width: 52px;
  height: 52px;
  margin: 0 auto 16px;
  border-radius: 14px;
  background: linear-gradient(135deg, var(--green), var(--green-dim));
  color: #04120a;
  font-weight: 800;
  display: grid;
  place-items: center;
  letter-spacing: .5px;
}

.login-card h1 { font-size: 17px; margin: 0 0 6px; }
.login-sub { margin: 0 0 20px; font-size: 13px; color: var(--text-dim); }

#loginPassword {
  width: 100%;
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 11px 13px;
  color: var(--text);
  font: inherit;
  outline: none;
  text-align: center;
  letter-spacing: 1px;
}
#loginPassword:focus { border-color: var(--green-dim); }

.login-btn { width: 100%; margin-top: 12px; padding: 11px; }
.login-btn:disabled { opacity: .5; cursor: progress; }

.login-error {
  margin-top: 12px;
  font-size: 12.5px;
  color: var(--danger);
}
.login-error[hidden] { display: none; }
