:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --border: #e3e6ea;
  --text: #16191d;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --in-bubble: #eceef1;
  --out-bubble: #2563eb;
  --out-text: #ffffff;
  --hover: #f0f2f5;
  --active: #e7eefc;
  --danger: #b91c1c;
  --danger-bg: #fdecec;
  --warn-bg: #fff6e0;
  --warn-text: #7a4b00;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --panel: #171a20;
    --border: #2a2f37;
    --text: #e8eaed;
    --muted: #9aa1ab;
    --accent: #3b82f6;
    --in-bubble: #252a32;
    --out-bubble: #2f6fe0;
    --hover: #1e222a;
    --active: #1d2a42;
    --danger: #f87171;
    --danger-bg: #3a1d1d;
    --warn-bg: #3a2e14;
    --warn-text: #f5d38a;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}
.hidden { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
input, textarea {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  outline: none;
}
input:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent); }
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
label > input, label > textarea { margin-top: 4px; color: var(--text); font-size: 15px; }

.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.icon-btn {
  width: 34px; height: 34px; border-radius: 8px; border: 0; background: transparent;
  display: inline-grid; place-items: center; cursor: pointer; color: var(--text);
}
.icon-btn:hover { background: var(--hover); }
.icon-btn svg { width: 20px; height: 20px; fill: currentColor; }
.error { color: var(--danger); font-size: 13px; min-height: 1em; margin: 4px 0 10px; }
.muted { color: var(--muted); }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 28px; width: 100%; max-width: 360px; }
.card h1 { margin: 0 0 20px; font-size: 22px; }
.card .btn { width: 100%; padding: 10px; }

/* Layout */
.app { display: grid; grid-template-columns: 340px 1fr; height: 100vh; height: 100dvh; }
.sidebar { background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 14px 8px 16px; }
.brand { font-weight: 650; font-size: 17px; }
.sub { font-size: 12px; color: var(--muted); }
.side-tools { display: flex; gap: 8px; padding: 6px 14px 12px; border-bottom: 1px solid var(--border); }
.convos { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.convos li { padding: 11px 16px; border-bottom: 1px solid var(--border); cursor: pointer; display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; }
.convos li:hover { background: var(--hover); }
.convos li.active { background: var(--active); }
.c-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.c-time { font-size: 12px; color: var(--muted); }
.c-last { grid-column: 1 / -1; font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tag { display: inline-block; font-size: 11px; font-weight: 600; padding: 1px 6px; border-radius: 4px; background: var(--danger-bg); color: var(--danger); margin-left: 6px; vertical-align: 1px; }
.convos .none { color: var(--muted); text-align: center; cursor: default; display: block; padding: 30px 16px; }

/* Menu */
.menu-wrap { position: relative; }
.menu { position: absolute; right: 0; top: 38px; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.15); min-width: 200px; z-index: 10; padding: 6px; }
.menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 6px; cursor: pointer; }
.menu button:hover { background: var(--hover); }
.menu-who { padding: 8px 10px; font-size: 13px; color: var(--muted); border-bottom: 1px solid var(--border); margin-bottom: 4px; }

/* Thread */
.thread { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.empty { flex: 1; display: grid; place-items: center; color: var(--muted); padding: 16px; text-align: center; }
.thread-inner { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.thread-head { display: flex; align-items: center; gap: 10px; padding: 12px 18px; background: var(--panel); border-bottom: 1px solid var(--border); }
.who { flex: 1; min-width: 0; }
.who-name { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.who-phone { font-size: 13px; color: var(--muted); }
.back { display: none; }
.optout-banner { background: var(--danger-bg); color: var(--danger); padding: 9px 18px; font-size: 13px; border-bottom: 1px solid var(--border); }
.messages { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 4px; }
.day { text-align: center; font-size: 12px; color: var(--muted); margin: 14px 0 6px; }
.msg { max-width: min(560px, 78%); display: flex; flex-direction: column; }
.msg.in { align-self: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.bubble { padding: 8px 12px; border-radius: 16px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.in .bubble { background: var(--in-bubble); border-bottom-left-radius: 5px; }
.msg.out .bubble { background: var(--out-bubble); color: var(--out-text); border-bottom-right-radius: 5px; }
.msg.failed .bubble { background: var(--danger-bg); color: var(--danger); }
.meta { font-size: 11px; color: var(--muted); margin: 2px 6px 6px; }
.meta .bad { color: var(--danger); font-weight: 600; }
.media-note { font-style: italic; opacity: .85; }
.composer { display: flex; gap: 10px; padding: 12px 18px; border-top: 1px solid var(--border); background: var(--panel); align-items: flex-end; }
.composer textarea { resize: none; max-height: 160px; min-height: 40px; }
.composer-side { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.counter { font-size: 11px; color: var(--muted); }

/* Dialogs */
dialog { border: 1px solid var(--border); border-radius: 14px; padding: 0; background: var(--panel); color: var(--text); width: min(440px, calc(100vw - 32px)); }
dialog.wide { width: min(680px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0,0,0,.4); }
dialog form, .dlg-body { padding: 22px; }
dialog h2 { margin: 0 0 14px; font-size: 18px; }
dialog h3 { margin: 18px 0 10px; font-size: 15px; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.opt-box { background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; font-size: 13px; margin-bottom: 8px; display: flex; gap: 10px; align-items: center; justify-content: space-between; }
.users { width: 100%; border-collapse: collapse; font-size: 14px; }
.users th { text-align: left; font-weight: 600; color: var(--muted); font-size: 12px; border-bottom: 1px solid var(--border); padding: 6px; }
.users td { padding: 8px 6px; border-bottom: 1px solid var(--border); }
.users td:last-child { text-align: right; white-space: nowrap; }
.add-user .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.check { display: flex; align-items: center; gap: 8px; margin: 0; color: var(--text); font-size: 14px; }
.check input { width: auto; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; font-size: 14px; z-index: 50; max-width: calc(100vw - 32px); }

/* Mobile */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .app.show-thread .sidebar { display: none; }
  .app:not(.show-thread) .thread { display: none; }
  .back { display: inline-grid; }
  .thread-head, .messages, .composer { padding-left: 12px; padding-right: 12px; }
  .msg { max-width: 88%; }
  .add-user .grid { grid-template-columns: 1fr; }
  .users th:nth-child(2), .users td:nth-child(2) { display: none; }
}

/* Notifications dialog */
.radios { border: 0; padding: 0; margin: 0 0 10px; }
.radios legend { font-size: 13px; color: var(--muted); margin-bottom: 6px; padding: 0; }
.radios .check { margin-bottom: 8px; }
.small { font-size: 12px; }
.notice { background: var(--warn-bg); color: var(--warn-text); border-radius: 8px; padding: 9px 12px; font-size: 13px; margin: 0 0 12px; }
.dlg-actions .spacer { flex: 1; }

/* Unread */
.filter-tabs { display: flex; gap: 4px; padding: 8px 14px; border-bottom: 1px solid var(--border); }
.filter-tabs .tab { border: 0; background: none; padding: 5px 12px; border-radius: 999px; cursor: pointer; color: var(--muted); font-size: 13px; font-weight: 600; }
.filter-tabs .tab:hover { background: var(--hover); }
.filter-tabs .tab.active { background: var(--active); color: var(--accent); }
.tab-count { display: inline-block; min-width: 18px; padding: 0 5px; margin-left: 2px; border-radius: 9px; background: var(--accent); color: var(--accent-text); font-size: 11px; line-height: 18px; text-align: center; }
.convos li { position: relative; }
.convos li.unread .c-name { font-weight: 750; }
.convos li.unread .c-last { color: var(--text); font-weight: 550; padding-right: 30px; }
.convos li.unread .c-time { color: var(--accent); font-weight: 600; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-right: 7px; vertical-align: 1px; }
.badge { position: absolute; right: 16px; bottom: 11px; min-width: 20px; padding: 0 6px; border-radius: 10px; background: var(--accent); color: var(--accent-text); font-size: 12px; font-weight: 700; line-height: 20px; text-align: center; }
.new-divider { display: flex; align-items: center; gap: 10px; margin: 12px 0 8px; color: var(--accent); font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; }
.new-divider::before, .new-divider::after { content: ""; flex: 1; height: 1px; background: var(--accent); opacity: .5; }
@media (max-width: 760px) {
  #unreadBtn { padding: 8px 10px; font-size: 13px; }
}

/* Mine filter */
.filter-tabs { align-items: center; }
.filter-tabs .spacer { flex: 1; }
.chip { border: 1px solid var(--border); background: var(--panel); color: var(--muted); border-radius: 999px; padding: 4px 12px; font-size: 13px; font-weight: 600; cursor: pointer; }
.chip:hover { background: var(--hover); }
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

/* Signed-in person in the header */
.me-line { font-size: 12px; color: var(--muted); }
.me-line #meName { color: var(--text); font-weight: 600; }

/* Delete conversation */
.icon-btn.danger-icon { color: var(--danger); }
.icon-btn.danger-icon:hover { background: var(--danger-bg); }

/* Short button labels on phones so the contact name has room */
.btn .sm { display: none; }
@media (max-width: 760px) {
  .btn .lg { display: none; }
  .btn .sm { display: inline; }
  .thread-head { gap: 6px; }
  .thread-head .btn { padding: 7px 10px; font-size: 13px; }
}
