:root {
  --accent: #4f46c8;
  --accent-2: #6d64e8;
  --accent-soft: #e9e7ff;
  --bg: #f4f3fb;
  --panel: #ffffff;
  --panel-2: #f8f7fd;
  --line: #e4e2f0;
  --text: #1d1b2e;
  --muted: #6b6884;
  --mine: #dcd8ff;
  --theirs: #ffffff;
  --chat-bg: #efedf8;
  --read: #2f8cff;
  --danger: #d6455d;
  --ok: #1d9a6c;
  --warn: #c98a00;
  --shadow: 0 1px 2px rgba(30, 20, 80, .08), 0 4px 16px rgba(30, 20, 80, .06);
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --accent: #8a83ff;
    --accent-2: #a39dff;
    --accent-soft: #2c2a4a;
    --bg: #121120;
    --panel: #1b1a2c;
    --panel-2: #222137;
    --line: #2e2c45;
    --text: #ecebf7;
    --muted: #9a97b5;
    --mine: #3b3680;
    --theirs: #24233a;
    --chat-bg: #15142a;
    --read: #6fb4ff;
  }
}
* { box-sizing: border-box; }
html, body, #app { margin: 0; height: 100%; }
body {
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
  background: var(--bg); color: var(--text);
  -webkit-font-smoothing: antialiased;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
a { color: var(--accent); }

/* ---------- auth ---------- */
.auth-wrap { min-height: 100%; display: grid; place-items: center; padding: 24px;
  background: radial-gradient(1200px 600px at 10% -10%, #8a83ff33, transparent), radial-gradient(800px 500px at 110% 110%, #ffc43d33, transparent), var(--bg); }
.auth-card { width: 100%; max-width: 380px; background: var(--panel); border-radius: 20px; padding: 28px; box-shadow: var(--shadow); }
.brand { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; }
.brand img { width: 44px; height: 44px; }
.brand h1 { font-size: 22px; margin: 0; letter-spacing: -.3px; }
.brand p { margin: 0; color: var(--muted); font-size: 13px; }
.tabs { display: flex; background: var(--panel-2); border-radius: 10px; padding: 4px; margin-bottom: 16px; }
.tabs button { flex: 1; border: 0; background: none; padding: 8px; border-radius: 8px; color: var(--muted); font-weight: 600; }
.tabs button.active { background: var(--panel); color: var(--text); box-shadow: var(--shadow); }
label.field { display: block; margin-bottom: 12px; font-size: 13px; color: var(--muted); }
label.field input, label.field textarea, .input {
  display: block; width: 100%; margin-top: 4px; padding: 10px 12px; border: 1px solid var(--line);
  border-radius: 10px; background: var(--panel-2); outline: none; }
label.field input:focus, label.field textarea:focus, .input:focus { border-color: var(--accent); }
.btn { border: 0; border-radius: 10px; padding: 10px 16px; font-weight: 600; background: var(--accent); color: #fff; }
.btn:hover { background: var(--accent-2); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.block { width: 100%; }
.btn.ghost { background: transparent; color: var(--accent); }
.btn.ghost:hover { background: var(--accent-soft); }
.btn.danger { background: var(--danger); }
.btn.small { padding: 6px 10px; font-size: 13px; }
.msg-box { padding: 10px 12px; border-radius: 10px; font-size: 14px; margin-bottom: 12px; }
.msg-box.err { background: #d6455d1a; color: var(--danger); }
.msg-box.ok { background: #1d9a6c1a; color: var(--ok); }

/* ---------- layout ---------- */
.shell { display: grid; grid-template-columns: 380px 1fr; height: 100vh; height: 100dvh; max-width: 1600px; margin: 0 auto; background: var(--panel); box-shadow: var(--shadow); }
.side { display: flex; flex-direction: column; border-right: 1px solid var(--line); min-width: 0; min-height: 0; }
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--chat-bg); }
.topbar { display: flex; align-items: center; gap: 10px; padding: 10px 14px; background: var(--panel-2); border-bottom: 1px solid var(--line); min-height: 60px; }
.topbar .title { flex: 1; min-width: 0; }
.topbar .title b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .title small { color: var(--muted); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { border: 0; background: none; width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center; color: var(--muted); flex: none; position: relative; }
.icon-btn:hover { background: var(--accent-soft); color: var(--accent); }
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn .dot { position: absolute; top: 4px; right: 4px; min-width: 16px; height: 16px; border-radius: 8px; background: var(--danger); color: #fff; font-size: 10px; font-weight: 700; display: grid; place-items: center; padding: 0 4px; }
.search { padding: 8px 12px; border-bottom: 1px solid var(--line); }
.search input { width: 100%; border: 0; background: var(--panel-2); border-radius: 10px; padding: 9px 12px; outline: none; }
.banner { margin: 8px 12px 0; padding: 10px 12px; border-radius: 10px; background: #ffc43d2e; font-size: 13px; }
.banner button { margin-left: 6px; }
.chat-list { flex: 1; overflow-y: auto; }
.chat-item { display: flex; gap: 12px; align-items: center; padding: 10px 14px; cursor: pointer; border-bottom: 1px solid var(--line); }
.chat-item:hover { background: var(--panel-2); }
.chat-item.active { background: var(--accent-soft); }
.chat-item .meta { flex: 1; min-width: 0; }
.chat-item .row { display: flex; align-items: baseline; gap: 8px; }
.chat-item .name { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .time { font-size: 12px; color: var(--muted); flex: none; }
.chat-item .preview { flex: 1; color: var(--muted); font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .preview.typing { color: var(--accent); font-style: italic; }
.chat-item.unread .time { color: var(--accent); font-weight: 600; }
.badge { background: var(--accent); color: #fff; font-size: 11.5px; font-weight: 700; border-radius: 10px; min-width: 20px; height: 20px; padding: 0 6px; display: grid; place-items: center; flex: none; }
.empty { color: var(--muted); text-align: center; padding: 40px 20px; }

.avatar { width: 46px; height: 46px; border-radius: 50%; flex: none; position: relative; background: var(--accent-soft); display: grid; place-items: center; font-weight: 700; color: var(--accent); }
.avatar img { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; display: block; }
.avatar.sm { width: 36px; height: 36px; font-size: 13px; }
.avatar.lg { width: 110px; height: 110px; font-size: 36px; }
.avatar .presence { position: absolute; right: 0; bottom: 0; width: 12px; height: 12px; border-radius: 50%; background: #2fc46b; border: 2px solid var(--panel); }

/* ---------- conversation ---------- */
.welcome { flex: 1; display: grid; place-items: center; text-align: center; color: var(--muted); padding: 20px; }
.welcome img { width: 96px; opacity: .9; }
.welcome h2 { color: var(--text); margin: 12px 0 4px; }
.messages { flex: 1; overflow-y: auto; padding: 16px 6% 8px; display: flex; flex-direction: column; gap: 2px;
  background-image: radial-gradient(var(--line) 1px, transparent 1px); background-size: 22px 22px; }
.day { align-self: center; background: var(--panel); color: var(--muted); font-size: 12px; padding: 4px 12px; border-radius: 8px; margin: 10px 0; box-shadow: var(--shadow); }
.sys { align-self: center; background: var(--accent-soft); color: var(--muted); font-size: 12.5px; padding: 5px 12px; border-radius: 8px; margin: 6px 0; text-align: center; max-width: 80%; }
.bubble-row { display: flex; margin-top: 2px; }
.bubble-row.mine { justify-content: flex-end; }
.bubble-row.first { margin-top: 8px; }
.bubble { max-width: min(72%, 560px); background: var(--theirs); border-radius: var(--radius); padding: 6px 9px 4px; box-shadow: 0 1px 1px rgba(0,0,0,.06); position: relative; word-wrap: break-word; overflow-wrap: anywhere; }
.mine .bubble { background: var(--mine); }
.bubble-row.first:not(.mine) .bubble { border-top-left-radius: 4px; }
.bubble-row.first.mine .bubble { border-top-right-radius: 4px; }
.bubble .sender { font-size: 12.5px; font-weight: 700; margin-bottom: 2px; }
.bubble .text { white-space: pre-wrap; }
.bubble .text a { word-break: break-all; }
.bubble .foot { float: right; margin: 6px 0 -2px 10px; font-size: 11px; color: var(--muted); display: inline-flex; align-items: center; gap: 3px; white-space: nowrap; }
.bubble .foot svg { width: 16px; height: 16px; }
.bubble .foot .read { color: var(--read); }
.bubble.deleted .text { font-style: italic; color: var(--muted); }
.bubble img.media, .bubble video.media { display: block; max-width: 100%; max-height: 340px; border-radius: 10px; margin: 2px 0 4px; cursor: pointer; background: #0001; }
.bubble audio { display: block; max-width: 260px; margin: 4px 0; }
.file-card { display: flex; align-items: center; gap: 10px; padding: 8px; border-radius: 10px; background: #0000000d; text-decoration: none; color: inherit; margin: 2px 0 4px; min-width: 200px; }
.file-card svg { width: 30px; height: 30px; color: var(--accent); flex: none; }
.file-card small { color: var(--muted); display: block; }
.quote { border-left: 3px solid var(--accent); background: #0000000a; border-radius: 6px; padding: 4px 8px; margin-bottom: 4px; font-size: 13px; cursor: pointer; }
.quote b { color: var(--accent); display: block; font-size: 12px; }
.quote span { color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.bubble .menu-btn { position: absolute; top: 2px; right: 2px; border: 0; background: var(--theirs); border-radius: 50%; width: 24px; height: 24px; display: none; place-items: center; color: var(--muted); }
.mine .bubble .menu-btn { background: var(--mine); }
.bubble:hover .menu-btn { display: grid; }
.bubble .menu-btn svg { width: 16px; height: 16px; }
.flash { animation: flash 1.2s; }
@keyframes flash { 0%, 60% { box-shadow: 0 0 0 3px var(--accent); } 100% { box-shadow: none; } }

.typing-line { min-height: 0; padding: 0 6%; color: var(--accent); font-size: 13px; font-style: italic; }
.composer-wrap { background: var(--panel-2); border-top: 1px solid var(--line); }
.reply-bar { display: flex; align-items: center; gap: 8px; padding: 8px 12px 0; }
.reply-bar .quote { flex: 1; margin: 0; cursor: default; }
.composer { display: flex; align-items: flex-end; gap: 6px; padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); }
.composer textarea { flex: 1; resize: none; border: 0; border-radius: 22px; padding: 10px 16px; background: var(--panel); outline: none; max-height: 140px; min-height: 42px; line-height: 1.35; }
.send-btn { width: 44px; height: 44px; border-radius: 50%; border: 0; background: var(--accent); color: #fff; display: grid; place-items: center; flex: none; }
.send-btn svg { width: 22px; height: 22px; }
.send-btn.recording { background: var(--danger); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .7; } }
.emoji-pop { position: absolute; bottom: 64px; left: 10px; background: var(--panel); border-radius: 14px; box-shadow: var(--shadow); padding: 8px; display: grid; grid-template-columns: repeat(8, 36px); gap: 2px; z-index: 20; border: 1px solid var(--line); }
.emoji-pop button { border: 0; background: none; font-size: 22px; border-radius: 8px; height: 36px; }
.emoji-pop button:hover { background: var(--accent-soft); }
.drop-hint { position: absolute; inset: 0; background: #4f46c833; border: 3px dashed var(--accent); display: grid; place-items: center; font-size: 20px; font-weight: 700; color: var(--accent); z-index: 30; pointer-events: none; }

/* ---------- modal ---------- */
.overlay { position: fixed; inset: 0; background: rgba(12, 10, 30, .5); display: grid; place-items: center; z-index: 100; padding: 16px; }
.modal { background: var(--panel); border-radius: 18px; width: 100%; max-width: 460px; max-height: 90vh; max-height: 90dvh; display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal.wide { max-width: 760px; }
.modal header { display: flex; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--line); gap: 8px; }
.modal header h3 { margin: 0; flex: 1; font-size: 17px; }
.modal .body { padding: 16px; overflow-y: auto; }
.modal footer { padding: 12px 16px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; }
.user-row { display: flex; align-items: center; gap: 12px; padding: 8px; border-radius: 10px; cursor: pointer; }
.user-row:hover { background: var(--panel-2); }
.user-row .meta { flex: 1; min-width: 0; }
.user-row .meta small { color: var(--muted); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-row input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--accent); }
.pill { display: inline-block; font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 10px; background: var(--accent-soft); color: var(--accent); text-transform: uppercase; letter-spacing: .3px; }
.pill.pending { background: #ffc43d33; color: var(--warn); }
.pill.disabled, .pill.rejected { background: #d6455d1f; color: var(--danger); }
.pill.active { background: #1d9a6c1f; color: var(--ok); }
.avatar-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; margin: 10px 0 16px; }
.avatar-grid button { border: 3px solid transparent; border-radius: 50%; padding: 0; background: none; aspect-ratio: 1; }
.avatar-grid button img { width: 100%; height: 100%; display: block; border-radius: 50%; }
.avatar-grid button.sel { border-color: var(--accent); }
.avatar-grid button:hover { transform: scale(1.06); }
.profile-head { display: flex; flex-direction: column; align-items: center; gap: 8px; margin-bottom: 12px; }
.section-title { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); margin: 18px 0 8px; }
.admin-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.admin-table td { padding: 8px 6px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.admin-table .actions { text-align: right; white-space: nowrap; }
.admin-table .actions .btn { margin-left: 4px; }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.9); z-index: 200; display: grid; place-items: center; }
.lightbox img, .lightbox video { max-width: 95vw; max-height: 88vh; }
.lightbox .close { position: absolute; top: 12px; right: 12px; color: #fff; }
.lightbox a { position: absolute; top: 18px; right: 64px; color: #fff; }
.ctx-menu { position: fixed; background: var(--panel); border-radius: 10px; box-shadow: 0 8px 30px rgba(0,0,0,.2); padding: 6px 0; z-index: 150; min-width: 170px; border: 1px solid var(--line); }
.ctx-menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 9px 16px; }
.ctx-menu button:hover { background: var(--panel-2); }
.ctx-menu button.danger { color: var(--danger); }
.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: #1d1b2e; color: #fff; padding: 10px 18px; border-radius: 10px; z-index: 300; box-shadow: var(--shadow); max-width: 90vw; }

/* ---------- mobile ---------- */
.back-btn { display: none; }
@media (max-width: 800px) {
  .shell { grid-template-columns: 1fr; }
  .shell.in-chat .side { display: none; }
  .shell:not(.in-chat) .main { display: none; }
  .back-btn { display: grid; }
  .messages { padding: 12px 10px 6px; }
  .typing-line { padding: 0 12px; }
  .bubble { max-width: 85%; }
  .bubble .menu-btn { display: grid; opacity: .6; }
  .avatar-grid { grid-template-columns: repeat(4, 1fr); }
}

/* ---------- word-by-word reveal for new messages ---------- */
.bubble.reveal { animation: bubbleIn .28s ease-out both; }
.w { display: inline-block; opacity: 0; animation: wordUp .38s cubic-bezier(.2, .7, .3, 1) forwards; white-space: pre-wrap; }
img.w, video.w, a.file-card.w, .quote.w, .sender.w { display: block; }
a.file-card.w { display: flex; }
@keyframes wordUp {
  from { opacity: 0; transform: translateY(.45em); }
  to { opacity: 1; transform: none; }
}
@keyframes bubbleIn {
  from { opacity: 0; transform: translateY(6px) scale(.98); }
  to { opacity: 1; transform: none; }
}
@media (prefers-reduced-motion: reduce) { .w, .bubble.reveal { animation: none; opacity: 1; } }
