:root {
  --bg: #0e1621;
  --panel: #17212b;
  --panel2: #1e2c3a;
  --line: #232e3c;
  --fg: #e9eef3;
  --dim: #7f91a4;
  --accent: #5eb5f7;
  --in: #182533;
  --out: #2b5278;
  --danger: #ec6666;
  --ok: #6ccb5f;
}
* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  background: var(--bg); color: var(--fg);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}
.hidden { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
input, textarea {
  background: var(--panel2); border: 1px solid transparent; border-radius: 10px;
  padding: 11px 13px; outline: none; width: 100%;
}
input:focus, textarea:focus { border-color: var(--accent); }
button { cursor: pointer; border: 0; background: none; }
.primary { background: var(--accent); color: #07131f; font-weight: 600; padding: 11px 16px; border-radius: 10px; }
.secondary { background: var(--panel2); padding: 11px 16px; border-radius: 10px; width: 100%; }
.danger { background: transparent; color: var(--danger); padding: 11px 16px; border-radius: 10px; width: 100%; border: 1px solid var(--line); }
.icon-btn { width: 38px; height: 38px; border-radius: 50%; color: var(--dim); font-size: 18px; flex: none; }
.icon-btn:hover { background: var(--panel2); color: var(--fg); }
.err { color: var(--danger); font-size: 13px; min-height: 1.3em; margin-top: 8px; }
.hint { color: var(--dim); font-size: 13px; }

/* ── auth ── */
#auth { height: 100%; display: grid; place-items: center; padding: 16px; }
.auth-card { width: min(380px, 100%); display: grid; gap: 12px; }
.auth-card h1 { font-size: 26px; }
.auth-card .sub { color: var(--dim); margin-top: -6px; }
.auth-card form, dialog form:not(.dlg-head) { display: grid; gap: 10px; }
.tabs { display: flex; gap: 4px; background: var(--panel); padding: 4px; border-radius: 10px; }
.tab { flex: 1; padding: 8px; border-radius: 8px; color: var(--dim); }
.tab.on { background: var(--panel2); color: var(--fg); }

/* ── layout ── */
#app { height: 100%; display: grid; grid-template-columns: 340px 1fr; }
#side { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-head { display: flex; gap: 8px; align-items: center; padding: 10px; padding-top: max(10px, env(safe-area-inset-top)); }
.side-head input { border-radius: 20px; padding: 9px 14px; }
#chatList { overflow-y: auto; flex: 1; }
#chatList:empty { flex: 0; }
.empty { color: var(--dim); text-align: center; padding: 40px 20px; margin: auto; }
#main { min-width: 0; min-height: 0; position: relative; background: var(--bg); }
.placeholder { height: 100%; display: grid; place-items: center; color: var(--dim); }
#thread { height: 100%; display: grid; grid-template-rows: auto 1fr auto; position: relative; }

/* avatars */
.avatar, .avatar-btn {
  width: 42px; height: 42px; border-radius: 50%; flex: none; display: grid; place-items: center;
  font-weight: 600; color: #fff; font-size: 16px; user-select: none;
}
.avatar-btn { width: 38px; height: 38px; font-size: 14px; }

/* chat list */
.chat {
  display: flex; gap: 11px; padding: 10px 12px; cursor: pointer; align-items: center;
}
.chat:hover { background: var(--panel2); }
.chat.on { background: #2b5278; }
.chat .mid { flex: 1; min-width: 0; }
.chat .row1, .chat .row2 { display: flex; gap: 8px; align-items: baseline; }
.chat .name { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat .time { font-size: 12px; color: var(--dim); flex: none; }
.chat .prev { flex: 1; color: var(--dim); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat .prev .typing { color: var(--accent); }
.chat.on .prev, .chat.on .time { color: #c5d6e6; }
.badge { background: var(--accent); color: #07131f; font-size: 12px; font-weight: 700; min-width: 20px; height: 20px; border-radius: 10px; padding: 0 6px; display: grid; place-items: center; flex: none; }

/* thread header */
.thread-head {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--panel);
  padding-top: max(8px, env(safe-area-inset-top)); border-bottom: 1px solid var(--line);
}
.head-text { flex: 1; min-width: 0; }
.head-text .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.head-text .subtitle { font-size: 12.5px; color: var(--dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.subtitle.typing { color: var(--accent); }
.back { display: none; font-size: 26px; }

/* messages */
#log { overflow-y: auto; padding: 12px 10px; display: flex; flex-direction: column; gap: 2px; overscroll-behavior: contain; }
.day { align-self: center; background: #0d141c99; color: var(--dim); font-size: 12px; padding: 3px 10px; border-radius: 12px; margin: 10px 0 6px; }
.sys { align-self: center; color: var(--dim); font-size: 12.5px; padding: 4px 10px; margin: 6px 0; text-align: center; }
.loadmore { align-self: center; color: var(--dim); font-size: 12px; padding: 6px; }
.row { display: flex; }
.row.mine { justify-content: flex-end; }
.row.first { margin-top: 8px; }
.bubble {
  position: relative; max-width: min(520px, 82%); background: var(--in); border-radius: 14px;
  padding: 6px 10px 5px; cursor: pointer; overflow-wrap: anywhere;
}
.mine .bubble { background: var(--out); }
.row.first.theirs .bubble { border-top-left-radius: 4px; }
.row.first.mine .bubble { border-top-right-radius: 4px; }
.bubble .who { font-size: 13px; font-weight: 600; margin-bottom: 1px; }
.bubble .text { white-space: pre-wrap; }
.bubble .text a { color: #8fd0ff; }
.bubble .deleted { color: var(--dim); font-style: italic; }
.bubble .meta { float: right; margin: 5px 0 0 12px; font-size: 11.5px; color: #ffffff8f; display: flex; gap: 4px; align-items: center; user-select: none; }
.bubble .meta .ticks.seen { color: #7fd6ff; }
.bubble::after { content: ""; display: block; clear: both; }
.quote {
  border-left: 3px solid var(--accent); background: #ffffff12; border-radius: 4px; padding: 3px 8px;
  margin: 2px 0 5px; font-size: 13px; cursor: pointer;
}
.quote b { display: block; color: var(--accent); font-weight: 600; }
.quote span { display: block; color: #ffffffb0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.media img, .media video { display: block; max-width: 100%; max-height: 340px; border-radius: 10px; margin: 2px 0 4px; background: #0003; }
.media img { cursor: zoom-in; }
.media audio { display: block; width: 260px; max-width: 100%; margin: 4px 0; }
.filecard { display: flex; gap: 10px; align-items: center; padding: 4px 2px; color: inherit; text-decoration: none; }
.filecard .ico { width: 42px; height: 42px; border-radius: 50%; background: var(--accent); color: #07131f; display: grid; place-items: center; font-size: 18px; flex: none; }
.filecard .nm { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }
.filecard .sz { font-size: 12px; color: #ffffff99; }
.reacts { display: flex; gap: 4px; flex-wrap: wrap; margin: 4px 0 2px; clear: both; }
.react { background: #ffffff1a; border-radius: 12px; padding: 1px 8px; font-size: 13px; cursor: pointer; }
.react.me { background: var(--accent); color: #07131f; }
.flash { animation: flash 1.2s ease; }
@keyframes flash { 0%, 40% { box-shadow: 0 0 0 3px var(--accent); } 100% { box-shadow: 0 0 0 3px transparent; } }

/* composer */
#composerWrap { background: var(--panel); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
#composer { display: flex; align-items: flex-end; gap: 6px; padding: 8px 10px; }
#composer textarea { resize: none; max-height: 140px; border-radius: 18px; padding: 10px 14px; line-height: 1.35; overflow-y: auto; scrollbar-width: none; }
#composer textarea::-webkit-scrollbar { display: none; }
.send { width: 40px; height: 40px; border-radius: 50%; background: var(--accent); color: #07131f; font-size: 16px; flex: none; }
.replybar { display: flex; align-items: center; gap: 8px; padding: 6px 12px; border-left: 3px solid var(--accent); margin: 6px 10px 0; background: var(--panel2); border-radius: 6px; }
.rb-text { flex: 1; min-width: 0; font-size: 13px; }
.rb-text b { color: var(--accent); margin-right: 8px; }
.rb-text span { color: var(--dim); }
#uploads { display: grid; gap: 4px; padding: 6px 10px 0; }
.up { background: var(--panel2); border-radius: 8px; padding: 6px 10px; font-size: 13px; position: relative; overflow: hidden; display: flex; gap: 8px; align-items: center; }
.up .bar { position: absolute; inset: 0 auto 0 0; background: #5eb5f733; width: 0; }
.up span { position: relative; }
.up .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.up button { position: relative; color: var(--dim); }
.up.fail { outline: 1px solid var(--danger); }
.drop-hint { position: absolute; inset: 56px 8px 8px; border: 2px dashed var(--accent); border-radius: 14px; background: #0e1621cc; display: grid; place-items: center; color: var(--accent); font-size: 18px; z-index: 5; pointer-events: none; }

/* popover */
.pop {
  position: fixed; z-index: 30; background: var(--panel2); border-radius: 12px; padding: 6px; min-width: 180px;
  box-shadow: 0 8px 30px #0008; display: grid; gap: 2px;
}
.pop .emojis { display: flex; gap: 2px; padding: 2px 2px 6px; border-bottom: 1px solid var(--line); margin-bottom: 2px; }
.pop .emojis button { font-size: 22px; padding: 2px 5px; border-radius: 8px; }
.pop .emojis button.on, .pop .emojis button:hover { background: #ffffff1c; }
.pop .item { text-align: left; padding: 8px 10px; border-radius: 8px; color: var(--fg); text-decoration: none; display: block; }
.pop .item:hover { background: #ffffff14; }
.pop .item.danger { color: var(--danger); border: 0; width: auto; }

/* dialogs */
dialog { margin: auto; background: var(--panel); color: var(--fg); border: 1px solid var(--line); border-radius: 16px; padding: 18px; width: min(400px, calc(100% - 24px)); }
dialog::backdrop { background: #000a; }
dialog > * + * { margin-top: 12px; }
.dlg-head { display: flex !important; justify-content: space-between; align-items: center; }
.dlg-head h2 { font-size: 18px; }
dialog label { display: grid; gap: 6px; font-size: 13px; color: var(--dim); }
dialog hr { border: 0; border-top: 1px solid var(--line); }
.link-out { background: var(--panel2); border-radius: 10px; padding: 12px; text-align: center; }
.code { font: 700 26px/1.2 ui-monospace, Menlo, Consolas, monospace; letter-spacing: .15em; margin-bottom: 6px; user-select: all; }

/* contacts */
#contactsPane { display: grid; gap: 10px; }
#contactsPane .hint a { color: var(--accent); }
.c-head { font-size: 13px; color: var(--dim); margin-top: 4px; }
.c-list { display: grid; max-height: 38dvh; overflow-y: auto; margin: 0 -6px; }
.c-row { display: flex; align-items: center; gap: 10px; padding: 8px 6px; border-radius: 10px; }
.c-row:hover { background: var(--panel2); }
.c-row .avatar { width: 38px; height: 38px; font-size: 14px; }
.c-row .who { flex: 1; min-width: 0; }
.c-row .who b { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.c-row .who span { display: block; font-size: 12.5px; color: var(--dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.c-row button { background: var(--accent); color: #07131f; font-weight: 600; padding: 6px 14px; border-radius: 16px; }
dialog label.check { display: flex; gap: 10px; align-items: flex-start; color: var(--fg); font-size: 14px; }
dialog label.check input { width: auto; margin-top: 3px; flex: none; }
dialog label.check small { display: block; color: var(--dim); font-size: 12.5px; }

/* camera */
dialog.cam { width: min(560px, 100%); max-height: 100dvh; padding: 0; overflow: hidden; background: #000; border-radius: 16px; }
dialog.cam > * + * { margin-top: 0; }
.cam-stage { position: relative; background: #000; aspect-ratio: 3 / 4; max-height: 76dvh; width: 100%; display: grid; place-items: center; }
.cam-stage video, .cam-stage img { width: 100%; height: 100%; object-fit: contain; background: #000; }
#camVideo.mirror { transform: scaleX(-1); }
.cam-msg { position: absolute; inset: 0; display: grid; place-content: center; gap: 12px; text-align: center; padding: 24px; color: #fff; background: #000d; }
.cam-timer { position: absolute; top: 12px; left: 50%; transform: translateX(-50%); background: #d33; color: #fff; padding: 3px 12px; border-radius: 14px; font-variant-numeric: tabular-nums; }
.cam-x { position: absolute; top: 8px; right: 8px; width: 38px; height: 38px; border-radius: 50%; background: #0008; color: #fff; font-size: 16px; }
.cam-bar { background: #000; padding: 10px 16px calc(14px + env(safe-area-inset-bottom)); display: grid; gap: 10px; }
.cam-modes { display: flex; justify-content: center; gap: 4px; }
.cam-modes button { color: #ffffffb0; padding: 4px 14px; border-radius: 14px; }
.cam-modes button.on { color: #ffc400; background: #ffffff1a; }
.cam-row { display: flex; align-items: center; justify-content: space-between; }
.cam-row .cam-side { width: 48px; height: 48px; color: #fff; font-size: 24px; }
.cam-row.review { gap: 10px; }
.cam-row.review button { flex: 1; }
.shutter { width: 68px; height: 68px; border-radius: 50%; border: 4px solid #fff; display: grid; place-items: center; }
.shutter i { width: 50px; height: 50px; border-radius: 50%; background: #fff; transition: all .15s; }
.shutter.video i { background: #e33; }
.shutter.rec i { width: 26px; height: 26px; border-radius: 6px; background: #e33; }

.lightbox { position: fixed; inset: 0; background: #000e; z-index: 40; display: grid; place-items: center; cursor: zoom-out; }
.lightbox img { max-width: 96vw; max-height: 90vh; object-fit: contain; }
.lb-dl { position: absolute; bottom: 14px; right: 18px; color: #fff; background: #ffffff22; padding: 8px 14px; border-radius: 20px; text-decoration: none; }

/* phones: one pane at a time */
@media (max-width: 760px) {
  #app { grid-template-columns: 1fr; }
  #main { position: fixed; inset: 0; transform: translateX(100%); transition: transform .2s ease; z-index: 10; }
  #app.chat-open #main { transform: none; }
  .back { display: block; }
  .bubble { max-width: 88%; }
}
