:root {
  --bg: #ffffff;
  --panel: #f0f2f5;
  --text: #111b21;
  --muted: #667781;
  --line: #e9edef;
  --bar: #075e54;
  --bar-ink: #ffffff;
  --accent: #00a884;
  --accent-ink: #ffffff;
  --chat-bg: #efeae2;
  --in: #ffffff;
  --out: #d9fdd3;
  --tick: #53bdeb;
  --danger: #d93025;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111b21;
    --panel: #202c33;
    --text: #e9edef;
    --muted: #8696a0;
    --line: #222d34;
    --bar: #202c33;
    --bar-ink: #e9edef;
    --accent: #00a884;
    --accent-ink: #111b21;
    --chat-bg: #0b141a;
    --in: #202c33;
    --out: #005c4b;
    --tick: #53bdeb;
    --danger: #f28b82;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--text);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea {
  font: inherit; color: var(--text); background: var(--panel);
  border: 1px solid transparent; border-radius: 10px; padding: 10px 12px; outline: none; width: 100%;
}
input:focus, textarea:focus { border-color: var(--accent); }

.view { min-height: 100dvh; }
.center { display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 24px 16px; text-align: center; }
.logo { width: 88px; height: 88px; }
.center h1 { margin: 12px 0 4px; }
.stack { display: flex; flex-direction: column; gap: 10px; width: 100%; max-width: 300px; margin-top: 16px; }
.stack input { text-align: center; font-size: 20px; letter-spacing: 4px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.err { color: var(--danger); font-size: 14px; min-height: 1em; margin: 6px 0 0; }

.btn { border: 0; border-radius: 999px; padding: 11px 18px; font-weight: 600; background: var(--panel); }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.small { padding: 6px 12px; font-size: 13px; }
.icon { background: none; border: 0; font-size: 20px; padding: 8px 10px; line-height: 1; border-radius: 999px; }
.icon:active, .btn:active { opacity: .7; }

.bar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 8px 8px 16px;
  background: var(--bar); color: var(--bar-ink); min-height: 56px;
}
.bar h1 { margin: 0; font-size: 19px; font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar .icon { color: var(--bar-ink); }
.conn { width: 8px; height: 8px; border-radius: 50%; background: #f5b400; }
.conn.ok { background: #25d366; }
.conn.bad { background: #e53935; }

.pad { padding: 16px; max-width: 520px; margin: 0 auto; }
.card { background: var(--panel); border-radius: 14px; padding: 16px; margin-bottom: 12px; }
.card h2 { font-size: 16px; margin: 0 0 6px; }
.qr { display: flex; justify-content: center; margin: 12px 0; min-height: 60px; }
.qr canvas { background: #fff; padding: 10px; border-radius: 10px; width: 260px; height: 260px; max-width: 100%; image-rendering: pixelated; }
.steps { margin: 0; padding-left: 20px; color: var(--muted); font-size: 14px; }
.row { display: flex; gap: 8px; }
.row input { flex: 1; min-width: 0; }
.code { font: 700 28px/1.2 ui-monospace, Menlo, monospace; letter-spacing: 3px; text-align: center; margin: 14px 0 0; }

.searchbar { padding: 8px 12px; background: var(--bg); position: sticky; top: calc(56px + env(safe-area-inset-top)); z-index: 9; }
.banner { margin: 4px 12px 8px; padding: 10px 12px; border-radius: 12px; background: var(--panel); font-size: 14px; display: flex; gap: 10px; align-items: center; justify-content: space-between; }

.chats { list-style: none; margin: 0; padding: 0 0 96px; }
.chats li { display: flex; gap: 12px; align-items: center; padding: 10px 16px; cursor: pointer; }
.chats li:active { background: var(--panel); }
.chats .body { flex: 1; min-width: 0; border-bottom: 1px solid var(--line); padding-bottom: 10px; }
.chats .top, .chats .bottom { display: flex; gap: 8px; align-items: center; }
.chats .name { flex: 1; font-weight: 600; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chats .time { font-size: 12px; color: var(--muted); }
.chats .unread .time { color: var(--accent); }
.chats .last { flex: 1; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.badge { background: var(--accent); color: var(--accent-ink); border-radius: 999px; min-width: 20px; height: 20px; padding: 0 6px; font-size: 12px; font-weight: 700; display: grid; place-items: center; }
.empty { text-align: center; color: var(--muted); padding: 40px 16px; }

.avatar {
  width: 48px; height: 48px; border-radius: 50%; flex: 0 0 auto;
  display: grid; place-items: center; font-weight: 700; color: #fff; font-size: 17px;
}
.avatar.sm { width: 38px; height: 38px; font-size: 15px; }

.fab {
  position: fixed; right: 16px; bottom: calc(20px + env(safe-area-inset-bottom));
  width: 56px; height: 56px; border-radius: 16px; border: 0;
  background: var(--accent); color: var(--accent-ink); font-size: 22px; box-shadow: 0 4px 12px rgba(0,0,0,.25);
}

/* chat view */
.chatview { display: flex; flex-direction: column; height: 100dvh; min-height: 0; background: var(--chat-bg); }
.chatview .bar { padding-left: 4px; }
.titles { flex: 1; min-width: 0; }
.titles h1 { font-size: 17px; }
.sub { margin: 0; font-size: 12px; opacity: .8; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msgs { flex: 1; overflow-y: auto; padding: 8px 10px 12px; display: flex; flex-direction: column; gap: 3px; overscroll-behavior: contain; }
.day { align-self: center; background: var(--panel); color: var(--muted); font-size: 12px; padding: 4px 10px; border-radius: 8px; margin: 8px 0; }
.m { max-width: 82%; padding: 6px 8px 4px; border-radius: 10px; background: var(--in); align-self: flex-start; position: relative; box-shadow: 0 1px .5px rgba(0,0,0,.13); word-wrap: break-word; }
.m.me { background: var(--out); align-self: flex-end; }
.m .who { font-size: 13px; font-weight: 600; margin-bottom: 2px; }
.m .txt { white-space: pre-wrap; }
.m .txt a { color: var(--tick); }
.m .meta { float: right; font-size: 11px; color: var(--muted); margin: 6px 0 -2px 10px; white-space: nowrap; }
.m .tick.read { color: var(--tick); }
.m.revoked .txt { font-style: italic; color: var(--muted); }
.m .quote { border-left: 4px solid var(--accent); background: rgba(0,0,0,.06); padding: 4px 8px; border-radius: 6px; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.m img.media, .m video { display: block; max-width: 100%; width: 260px; border-radius: 8px; margin-bottom: 4px; background: rgba(0,0,0,.1); }
.m img.sticker { width: 140px; background: none; }
.m.sticker-only { background: none; box-shadow: none; }
.m .doc { width: 100%; border: 0; font: inherit; text-align: left; cursor: pointer; display: flex; gap: 8px; align-items: center; padding: 8px; background: rgba(0,0,0,.06); border-radius: 8px; color: inherit; text-decoration: none; margin-bottom: 4px; }
.m audio { width: 240px; max-width: 100%; height: 40px; }
.m .tap { position: relative; cursor: pointer; }
.m .tap::after { content: attr(data-label); position: absolute; inset: 0; display: grid; place-items: center; color: #fff; font-weight: 600; text-shadow: 0 1px 3px #000; }
.m .tap img { filter: blur(2px) brightness(.85); }
.loadmore { align-self: center; margin: 6px 0; }

.replybar { display: flex; align-items: center; gap: 8px; background: var(--panel); margin: 0 8px; padding: 6px 6px 6px 12px; border-left: 4px solid var(--accent); border-radius: 10px 10px 0 0; font-size: 13px; color: var(--muted); }
.replybar span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.composer { display: flex; align-items: flex-end; gap: 6px; padding: 6px 8px calc(6px + env(safe-area-inset-bottom)); }
.composer textarea { flex: 1; resize: none; max-height: 140px; border-radius: 22px; padding: 11px 16px; background: var(--in); }
.composer .attach { background: var(--in); width: 44px; height: 44px; display: grid; place-items: center; padding: 0; }
.composer { position: relative; }
.attachmenu { position: absolute; left: 8px; bottom: calc(100% + 4px); background: var(--panel); border-radius: 12px; box-shadow: 0 6px 24px rgba(0,0,0,.3); padding: 6px 0; z-index: 20; min-width: 180px; }
.attachmenu label { display: block; padding: 12px 16px; cursor: pointer; }
.attachmenu label:active { background: var(--line); }
.send { width: 46px; height: 46px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink); font-size: 18px; flex: 0 0 auto; }
.send:disabled { opacity: .5; }

.menu {
  position: fixed; top: calc(env(safe-area-inset-top) + 50px); right: 8px; z-index: 50;
  background: var(--panel); border-radius: 12px; box-shadow: 0 6px 24px rgba(0,0,0,.3); padding: 6px 0; min-width: 210px;
}
.menu button { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 12px 16px; }
.menu button:active { background: var(--line); }
.menu .danger { color: var(--danger); }

.viewer { position: fixed; inset: 0; background: rgba(0,0,0,.94); z-index: 60; display: grid; place-items: center; }
.viewer img, .viewer video { max-width: 100vw; max-height: 100dvh; }
.viewer .close { position: absolute; top: calc(env(safe-area-inset-top) + 8px); right: 8px; color: #fff; z-index: 1; }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); z-index: 70;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: #323232; color: #fff; padding: 10px 16px; border-radius: 999px; font-size: 14px;
  transition: opacity .2s, transform .2s; max-width: calc(100% - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
