:root {
  --bg: #0e1621; --side: #17212b; --panel: #17212b; --hover: #202b36; --sel: #2b5278; --line: #0b121a;
  --text: #f5f5f5; --dim: #7f91a4; --accent: #5eb5f7; --in: #182533; --out: #2b5278; --red: #e5534b;
  --badge: #3e9fe0; --muted: #3f4c5a; --r: 14px;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text); font: 15px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased; overflow: hidden;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
input, textarea { font: inherit; color: var(--text); }
[hidden] { display: none !important; }
svg { width: 24px; height: 24px; fill: currentColor; }

/* ---------------------------------------------------------------- auth */
#auth { height: 100%; overflow: auto; display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 12px; text-align: center; }
.card .logo svg { width: 88px; height: 88px; margin: 0 auto 4px; display: block; }
.card h1 { font-size: 22px; font-weight: 600; margin: 0 0 8px; }
.card input {
  width: 100%; padding: 13px 14px; border-radius: 10px; border: 1px solid #2a3846; background: var(--side); outline: none;
}
.card input:focus { border-color: var(--accent); }
.row2 { display: flex; gap: 8px; }
.row2 input { flex: 1; min-width: 0; }
.primary { background: var(--accent); color: #fff; font-weight: 600; padding: 13px; border-radius: 10px; }
.primary:disabled { opacity: .6; cursor: default; }
.ghost { border: 1px solid var(--accent); color: var(--accent); border-radius: 10px; padding: 0 12px; white-space: nowrap; }
.link { color: var(--accent); padding: 6px; }
.link.small { font-size: 13px; color: var(--dim); }
.err { color: var(--red); min-height: 1.2em; font-size: 14px; }
.ok { color: #6fcf7c; min-height: 1.2em; font-size: 14px; }
.note { color: var(--dim); font-size: 13px; margin: 4px 0 0; }

/* ---------------------------------------------------------------- layout */
#main { display: flex; height: 100%; }
#side { width: 360px; min-width: 260px; display: flex; flex-direction: column; background: var(--side); border-right: 1px solid var(--line); }
#chat { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative;
  background: var(--bg) radial-gradient(circle at 20% 10%, #13263a 0, transparent 55%), var(--bg); }
#chat.none .bar, #chat.none #compose { display: none; }
#chat.drop::after { content: ""; position: absolute; inset: 8px; border: 2px dashed var(--accent); border-radius: 16px; pointer-events: none; }
.bar { height: 56px; flex: none; display: flex; align-items: center; gap: 10px; padding: 0 10px; background: var(--panel);
  border-bottom: 1px solid var(--line); }
.htitle { font-weight: 600; font-size: 17px; }
.icon { width: 40px; height: 40px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; color: var(--dim); flex: none; }
.icon:hover { background: var(--hover); color: var(--text); }
#backBtn { display: none; }
#chead { display: flex; align-items: center; gap: 12px; min-width: 0; flex: 1; }
.htext { min-width: 0; }
.htext .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status { color: var(--dim); font-size: 13px; }
.accent { color: var(--accent) !important; }

.searchbox { padding: 8px 10px; }
#q { width: 100%; padding: 9px 14px; border-radius: 20px; border: 0; background: #242f3d; outline: none; }
#q:focus { box-shadow: 0 0 0 1px var(--accent); }
#list { flex: 1; overflow-y: auto; padding: 0 6px 8px; }
.item { width: 100%; display: flex; gap: 12px; align-items: center; padding: 8px; border-radius: 10px; text-align: left; }
.item:hover { background: var(--hover); }
.item.on { background: var(--sel); }
.item.on .sub, .item.on .time, .item.on .who { color: #d6e6f5; }
.item .body { flex: 1; min-width: 0; }
.top, .sub { display: flex; align-items: center; gap: 6px; }
.top .name { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.time { color: var(--dim); font-size: 12px; flex: none; }
.tick { color: var(--accent); font-size: 12px; letter-spacing: -3px; padding-right: 3px; }
.sub { color: var(--dim); font-size: 14px; margin-top: 2px; }
.sub > span:first-child { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who { color: var(--text); }
.badge { background: var(--badge); color: #fff; font-size: 12px; font-weight: 600; min-width: 22px; height: 22px; border-radius: 11px;
  padding: 0 7px; display: inline-flex; align-items: center; justify-content: center; flex: none; }
.badge.muted { background: var(--muted); }
.section { color: var(--accent); font-size: 13px; font-weight: 600; padding: 12px 8px 4px; }
.empty { color: var(--dim); text-align: center; padding: 32px 16px; }
.av { width: 52px; height: 52px; border-radius: 50%; flex: none; display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 600; font-size: 18px; background-size: cover; background-position: center; }
.av.sm { width: 40px; height: 40px; font-size: 15px; }

/* ---------------------------------------------------------------- messages */
#msgs { flex: 1; overflow-y: auto; padding: 12px max(12px, calc((100% - 760px) / 2)) 8px; display: flex; flex-direction: column; gap: 3px; }
.pick { margin: auto; }
.pick span, .day span, .sysline span { background: rgba(10, 20, 30, .55); color: #e3ecf5; border-radius: 12px; padding: 4px 11px; font-size: 13px; }
.day, .sysline { text-align: center; margin: 8px 0; }
.more { text-align: center; color: var(--dim); font-size: 13px; padding: 6px; }
.msg { display: flex; }
.msg.mine { justify-content: flex-end; }
.bubble { max-width: min(560px, 82%); background: var(--in); border-radius: var(--r); padding: 6px 10px 6px; position: relative;
  word-wrap: break-word; overflow-wrap: anywhere; box-shadow: 0 1px 1px rgba(0, 0, 0, .25); }
.mine .bubble { background: var(--out); }
.msg.flash .bubble, .msg.editing .bubble { box-shadow: 0 0 0 2px var(--accent); }
.bubble .who { font-weight: 600; font-size: 14px; margin-bottom: 1px; }
.text { white-space: pre-wrap; display: inline; }
.text a { color: #8ccaff; }
.dim { color: var(--dim); font-style: italic; }
.meta { float: right; font-size: 12px; color: rgba(255, 255, 255, .5); margin: 6px 0 -4px 10px; white-space: nowrap; user-select: none; }
.meta .tick { color: rgba(255, 255, 255, .55); letter-spacing: -3px; padding-right: 3px; }
.meta .tick.read { color: #8ccaff; }
.fwd { color: var(--accent); font-size: 13px; margin-bottom: 2px; }
.quote { border-left: 3px solid var(--accent); background: rgba(255, 255, 255, .06); border-radius: 6px; padding: 3px 8px;
  margin: 2px 0 5px; cursor: pointer; display: flex; flex-direction: column; min-width: 0; font-size: 14px; }
.quote b { color: var(--accent); font-weight: 600; }
.quote span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: #d6e0ea; }
.stk { font-size: 46px; line-height: 1.15; }
.sticker .bubble { background: none; box-shadow: none; padding: 0; }
.sticker .meta { background: rgba(10, 20, 30, .55); border-radius: 10px; padding: 1px 7px; }
.photos { display: grid; gap: 2px; margin: -2px -6px 4px; border-radius: 10px; overflow: hidden; width: min(340px, 64vw); }
.photos.n2, .photos.n4 { grid-template-columns: 1fr 1fr; }
.photos.n3 { grid-template-columns: 1fr 1fr 1fr; }
.photo { aspect-ratio: 1 / var(--r); max-height: 420px; background: #0b1420 center / cover no-repeat; position: relative; overflow: hidden; }
.photos:not(.n1) .photo { aspect-ratio: 1; }
.photo:not(.ready)::after { content: ""; position: absolute; inset: 0; backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); }
.photo.broken::before { content: "⚠"; position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; z-index: 1; }
.photo img { width: 100%; height: 100%; object-fit: cover; display: block; cursor: zoom-in; }
.media .bubble { padding: 4px 4px 4px; }
.media .photos { margin: 0; }
.media .meta { position: absolute; right: 10px; bottom: 10px; margin: 0; background: rgba(0, 0, 0, .45); color: #fff; border-radius: 10px; padding: 1px 7px; }
.filerow { display: flex; align-items: center; gap: 10px; text-align: left; padding: 4px 0; max-width: 100%; }
.filerow.busy { opacity: .6; }
.ficon { width: 42px; height: 42px; border-radius: 50%; background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center; flex: none; }
.fname { display: flex; flex-direction: column; min-width: 0; overflow-wrap: anywhere; }
.fname small { color: var(--dim); }
.mine .fname small { color: #a9c6e2; }
.voice { padding: 4px 0; }
.voice audio { width: min(280px, 60vw); height: 36px; }
.reacts { clear: both; display: flex; flex-wrap: wrap; gap: 4px; padding-top: 4px; }
.react { background: rgba(255, 255, 255, .1); border-radius: 12px; padding: 1px 8px; font-size: 13px; }
.react.me { background: var(--accent); color: #fff; }
.bubble[title] { cursor: pointer; outline: 1px solid var(--red); }

/* ---------------------------------------------------------------- compose */
#compose { flex: none; padding: 6px max(10px, calc((100% - 780px) / 2)) max(10px, env(safe-area-inset-bottom)); }
#bar { display: flex; align-items: center; gap: 8px; background: var(--side); border-radius: 12px 12px 0 0; padding: 6px 6px 6px 12px; }
#bar .quote { flex: 1; margin: 0; cursor: default; }
.inputrow { display: flex; align-items: flex-end; gap: 4px; background: var(--side); border-radius: 22px; padding: 4px; }
#bar:not([hidden]) + .inputrow { border-radius: 0 0 22px 22px; }
#text { flex: 1; resize: none; border: 0; outline: none; background: none; padding: 9px 4px; max-height: 180px; line-height: 1.35; }
.send { color: var(--accent); }

/* ---------------------------------------------------------------- overlays */
#viewer { position: fixed; inset: 0; background: rgba(0, 0, 0, .9); display: flex; align-items: center; justify-content: center; z-index: 20; cursor: zoom-out; }
#viewer img { max-width: 96vw; max-height: 96vh; object-fit: contain; }
#toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: #2b3846; color: #fff; padding: 10px 16px;
  border-radius: 10px; z-index: 30; max-width: calc(100vw - 32px); box-shadow: 0 4px 16px rgba(0, 0, 0, .4); }
.popup { position: fixed; z-index: 25; background: #222e3b; border-radius: 12px; padding: 6px 0; min-width: 190px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .45); display: flex; flex-direction: column; }
.popup button { text-align: left; padding: 9px 16px; }
.popup button:hover, .popup button:focus { background: var(--hover); outline: none; }
.popup .danger { color: var(--red); }

/* ---------------------------------------------------------------- phones: one panel at a time */
@media (max-width: 720px) {
  #side { width: 100%; border-right: 0; }
  #chat { display: none; }
  #main.in-chat #side { display: none; }
  #main.in-chat #chat { display: flex; }
  #backBtn { display: inline-flex; }
  .bubble { max-width: 88%; }
}
