:root {
  --bg: #ffffff;
  --side: #ffffff;
  --pane: #e6ebee;
  --pane-pattern: #d7e0e5;
  --text: #0f1419;
  --muted: #707579;
  --line: #e4e7ea;
  --hover: #f1f3f5;
  --active: #3390ec;
  --active-text: #ffffff;
  --bubble-in: #ffffff;
  --bubble-out: #e3fdd0;
  --bubble-out-meta: #4fae4e;
  --accent: #3390ec;
  --badge: #3390ec;
  --input: #f1f3f5;
  --danger: #e53935;
  --shadow: 0 1px 2px rgba(16, 35, 47, .15);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #17212b;
  --side: #17212b;
  --pane: #0e1621;
  --pane-pattern: #131d29;
  --text: #f5f5f5;
  --muted: #8b9aa7;
  --line: #101921;
  --hover: #202b36;
  --active: #2b5278;
  --active-text: #ffffff;
  --bubble-in: #182533;
  --bubble-out: #2b5278;
  --bubble-out-meta: #7da8d3;
  --accent: #5eb5f7;
  --badge: #4e9ed8;
  --input: #242f3d;
  --danger: #ef5350;
  --shadow: 0 1px 2px rgba(0, 0, 0, .35);
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; }
input, textarea, select { font: inherit; color: var(--text); }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error.ok { color: #2e9d48; }
.menu-btn { position: relative; }
.badge.mini { position: absolute; top: 2px; right: 0; min-width: 18px; height: 18px; font-size: 11px; padding: 0 5px; background: var(--danger); }
#admin-badge { background: var(--danger); margin-left: 6px; pointer-events: none; }
.error { color: var(--danger); min-height: 1.4em; margin-top: 10px; font-size: 14px; }

/* auth */
.auth { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 24px 16px; background: var(--pane); }
.auth-card { width: 100%; max-width: 380px; background: var(--bg); border-radius: 16px; padding: 28px 24px; box-shadow: 0 8px 30px rgba(0,0,0,.12); }
.auth-logo { text-align: center; }
.auth-logo h1 { font-size: 24px; margin: 10px 0 18px; }
.tabs { display: flex; background: var(--input); border-radius: 10px; padding: 3px; margin-bottom: 18px; }
.tab { flex: 1; border: 0; background: transparent; padding: 9px; border-radius: 8px; cursor: pointer; color: var(--muted); font-weight: 600; }
.tab.active { background: var(--bg); color: var(--text); box-shadow: var(--shadow); }
.auth-form label, .modal-body label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
.auth-form input, .modal-body input[type=text], .modal-body input[type=password], .modal-body input[type=number], .modal-body input:not([type]), .modal-body select {
  display: block; width: 100%; margin-top: 4px; padding: 10px 12px; border: 1px solid var(--line);
  border-radius: 10px; background: var(--input); outline: none;
}
.auth-form input:focus, .modal-body input:focus { border-color: var(--accent); }
.btn { border: 0; border-radius: 10px; padding: 10px 16px; cursor: pointer; background: var(--input); font-weight: 600; }
.btn.primary { background: var(--accent); color: #fff; width: 100%; }
.btn.small { padding: 5px 10px; font-size: 13px; border-radius: 8px; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; cursor: default; }

/* layout */
.app { display: flex; height: 100vh; height: 100dvh; overflow: hidden; }
.sidebar { width: 340px; min-width: 260px; max-width: 420px; border-right: 1px solid var(--line); background: var(--side); display: flex; flex-direction: column; position: relative; }
.side-head { display: flex; gap: 8px; align-items: center; padding: 10px 12px; }
#search { flex: 1; min-width: 0; border: 0; border-radius: 20px; padding: 9px 14px; background: var(--input); outline: none; }
.icon-btn { border: 0; background: transparent; cursor: pointer; font-size: 20px; width: 40px; height: 40px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; color: var(--muted); flex: none; }
.icon-btn:hover { background: var(--hover); }
.menu { position: absolute; top: 56px; left: 12px; z-index: 20; background: var(--bg); border-radius: 12px; box-shadow: 0 6px 24px rgba(0,0,0,.2); padding: 6px; min-width: 220px; }
.menu button { display: block; width: 100%; text-align: left; border: 0; background: transparent; padding: 10px 12px; border-radius: 8px; cursor: pointer; }
.menu button:hover { background: var(--hover); }
.menu-user { padding: 8px 12px 10px; border-bottom: 1px solid var(--line); margin-bottom: 4px; font-weight: 600; }
.menu-user .muted { font-weight: 400; font-size: 13px; }
.chat-list { overflow-y: auto; flex: 1 1 auto; }
#user-results { flex: 0 0 auto; max-height: 50%; }
.section-label { padding: 8px 16px 4px; font-size: 13px; color: var(--muted); font-weight: 600; }
.chat-item { display: flex; gap: 12px; align-items: center; padding: 8px 12px; margin: 0 6px; border-radius: 10px; cursor: pointer; }
.chat-item:hover { background: var(--hover); }
.chat-item.active { background: var(--active); color: var(--active-text); }
.chat-item.active .muted, .chat-item.active .chat-time { color: rgba(255,255,255,.8); }
.chat-item.active .badge { background: #fff; color: var(--active); }
.avatar { width: 48px; height: 48px; border-radius: 50%; display: flex; align-items: center; justify-content: center; color: #fff; font-weight: 600; font-size: 18px; flex: none; }
.conv-head .avatar { width: 40px; height: 40px; font-size: 16px; }
.chat-main { flex: 1; min-width: 0; }
.chat-row { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.chat-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-time { font-size: 12px; color: var(--muted); flex: none; }
.chat-preview { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.badge { background: var(--badge); color: #fff; border-radius: 12px; min-width: 22px; height: 22px; padding: 0 7px; font-size: 12px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; flex: none; }

/* conversation */
.pane { flex: 1; min-width: 0; display: flex; background: var(--pane); background-image: radial-gradient(var(--pane-pattern) 1px, transparent 1px); background-size: 22px 22px; }
.empty { margin: auto; }
.empty span { background: rgba(0,0,0,.25); color: #fff; padding: 6px 14px; border-radius: 14px; font-size: 14px; }
.conv { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.conv-head { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--side); border-bottom: 1px solid var(--line); }
.conv-title { flex: 1; min-width: 0; cursor: pointer; }
#conv-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.back { display: none; }
.messages { flex: 1; overflow-y: auto; padding: 12px 4%; }
#msg-list { display: flex; flex-direction: column; gap: 3px; max-width: 760px; margin: 0 auto; }
.load-more { display: block; margin: 0 auto 10px; border: 0; background: rgba(0,0,0,.2); color: #fff; border-radius: 14px; padding: 5px 14px; cursor: pointer; }
.day-sep { align-self: center; background: rgba(0,0,0,.22); color: #fff; font-size: 13px; padding: 3px 12px; border-radius: 12px; margin: 10px 0 6px; }
.sys-msg { align-self: center; background: rgba(0,0,0,.18); color: #fff; font-size: 13px; padding: 3px 12px; border-radius: 12px; margin: 4px 0; text-align: center; max-width: 90%; }
.msg { display: flex; }
.msg.out { justify-content: flex-end; }
.bubble { position: relative; max-width: min(520px, 82%); background: var(--bubble-in); border-radius: 14px 14px 14px 4px; padding: 6px 10px 6px; box-shadow: var(--shadow); overflow-wrap: anywhere; white-space: pre-wrap; }
.msg.out .bubble { background: var(--bubble-out); border-radius: 14px 14px 4px 14px; }
.msg.flash .bubble { outline: 2px solid var(--accent); }
.sender { font-weight: 600; font-size: 13px; margin-bottom: 2px; }
.meta { float: right; font-size: 11.5px; color: var(--muted); margin: 6px 0 -2px 10px; white-space: nowrap; user-select: none; }
.msg.out .meta { color: var(--bubble-out-meta); }
.meta .ticks { margin-left: 3px; }
.deleted-text { font-style: italic; color: var(--muted); }
.reply-quote { border-left: 3px solid var(--accent); padding: 2px 8px; margin: 2px 0 4px; border-radius: 4px; background: rgba(51,144,236,.08); cursor: pointer; font-size: 13.5px; white-space: normal; }
.reply-quote b { display: block; color: var(--accent); font-size: 13px; }
.reply-quote span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }
.img-wrap { display: block; margin: 2px -4px 4px; }
.img-wrap img { display: block; max-width: 100%; max-height: 320px; width: auto; border-radius: 10px; cursor: zoom-in; background: rgba(0,0,0,.05); }
.file-box { display: flex; gap: 10px; align-items: center; text-decoration: none; color: inherit; padding: 4px 0; white-space: normal; }
.file-icon { width: 42px; height: 42px; border-radius: 50%; background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center; font-size: 18px; flex: none; }
.file-name { font-weight: 600; word-break: break-all; }
.reply-bar { display: flex; align-items: center; gap: 8px; padding: 6px 12px; background: var(--side); border-top: 1px solid var(--line); }
.reply-bar-text { flex: 1; min-width: 0; border-left: 3px solid var(--accent); padding-left: 8px; }
.reply-bar-text b { display: block; color: var(--accent); font-size: 13px; }
.reply-bar-text span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.composer { display: flex; align-items: flex-end; gap: 6px; padding: 8px 12px calc(8px + env(safe-area-inset-bottom, 0px)); background: var(--side); }
#input { flex: 1; resize: none; border: 0; border-radius: 20px; padding: 10px 14px; background: var(--input); outline: none; max-height: 160px; line-height: 1.35; }
.send-btn { border: 0; width: 42px; height: 42px; border-radius: 50%; background: var(--accent); color: #fff; font-size: 18px; cursor: pointer; flex: none; }
.attach { cursor: pointer; }

/* context menu */
.ctx { position: fixed; z-index: 50; background: var(--bg); border-radius: 10px; box-shadow: 0 6px 24px rgba(0,0,0,.25); padding: 5px; min-width: 160px; }
.ctx button { display: block; width: 100%; text-align: left; border: 0; background: transparent; padding: 9px 12px; border-radius: 7px; cursor: pointer; }
.ctx button:hover { background: var(--hover); }
.ctx button.danger { color: var(--danger); }

/* modal */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 40; display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal-card { background: var(--bg); border-radius: 14px; width: 100%; max-width: 520px; max-height: 90vh; display: flex; flex-direction: column; box-shadow: 0 10px 40px rgba(0,0,0,.3); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 6px 20px; }
.modal-head h2 { font-size: 18px; margin: 0; }
.modal-body { padding: 6px 20px 20px; overflow-y: auto; }
.modal-body h3 { font-size: 15px; margin: 18px 0 8px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row > input { flex: 1; min-width: 140px; }
.list-item { display: flex; align-items: center; gap: 10px; padding: 7px 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.list-item .avatar { width: 36px; height: 36px; font-size: 14px; }
.list-item .grow { flex: 1; min-width: 120px; }
.tag { font-size: 11px; padding: 1px 7px; border-radius: 8px; background: var(--input); color: var(--muted); margin-left: 6px; }
.tag.on { background: var(--accent); color: #fff; }
.check-item { display: flex; align-items: center; gap: 10px; padding: 6px 0; cursor: pointer; color: var(--text) !important; font-size: 15px !important; margin: 0 !important; }
.code { font-family: ui-monospace, Menlo, Consolas, monospace; background: var(--input); padding: 2px 6px; border-radius: 6px; user-select: all; word-break: break-all; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: rgba(20,20,20,.9); color: #fff; padding: 9px 16px; border-radius: 10px; z-index: 60; max-width: 90vw; }

/* mobile */
@media (max-width: 760px) {
  .sidebar { width: 100%; max-width: none; border-right: 0; }
  .pane { display: none; }
  .app.show-conv .sidebar { display: none; }
  .app.show-conv .pane { display: flex; }
  .back { display: inline-flex; }
  .messages { padding: 10px 8px; }
  .bubble { max-width: 88%; }
}
.name-icon { vertical-align: -2px; margin-right: 5px; opacity: .8; }

/* "AI bot" badge next to bot accounts */
.bot-badge { display: inline-block; margin-left: 6px; padding: 0 6px; border-radius: 6px; font-size: 10.5px;
  font-weight: 700; line-height: 16px; vertical-align: 1px; letter-spacing: .02em; background: #7c4dff; color: #fff;
  white-space: nowrap; }
.meta .bot-badge { margin: 0 6px 0 0; font-size: 9.5px; line-height: 14px; opacity: .85; }
.chat-item.active .bot-badge { background: #fff; color: #7c4dff; }
.has-bot { display: flex; align-items: center; min-width: 0; }
.has-bot > .nm-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.has-bot > .bot-badge { flex: none; }
#conv-sub.typing { color: var(--accent); font-style: italic; }
.mention-bar { display: flex; flex-wrap: wrap; gap: 6px; padding: 6px 12px 0; background: var(--side); }
.mention-chip { border: 1px solid var(--line); background: transparent; color: var(--accent); border-radius: 14px; padding: 3px 10px; font-size: 13px; cursor: pointer; }
.mention-chip.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.mic-btn.rec { color: #e53935; }
.rec-bar { align-items: center; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: #e53935; animation: recblink 1s infinite; flex: none; }
@keyframes recblink { 50% { opacity: .25; } }
audio.voice { display: block; width: 260px; max-width: 100%; height: 40px; margin: 2px 0; }

/* Jarvis live voice panel */
.jarvis-panel { position: fixed; z-index: 45; right: 16px; bottom: 16px; width: min(460px, calc(100vw - 32px)); height: min(720px, calc(100vh - 32px)); background: var(--bg); border-radius: 14px; box-shadow: 0 10px 40px rgba(0,0,0,.35); display: flex; flex-direction: column; overflow: hidden; border: 1px solid var(--line); }
.jarvis-head { display: flex; align-items: center; gap: 10px; padding: 8px 8px 8px 16px; border-bottom: 1px solid var(--line); }
.jarvis-head .muted { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.jarvis-head a { color: var(--accent); }
.jarvis-frame-wrap { flex: 1; min-height: 0; }
.jarvis-frame-wrap iframe { width: 100%; height: 100%; border: 0; display: block; }
@media (max-width: 700px) {
  .jarvis-panel { inset: 0; width: auto; height: auto; border-radius: 0; border: 0; padding-top: env(safe-area-inset-top, 0px); padding-bottom: env(safe-area-inset-bottom, 0px); }
}
