:root {
  --brand: #2563eb;
  --brand-dark: #1d4ed8;
  --brand-soft: #e8efff;
  --bg: #f4f6fa;
  --panel: #ffffff;
  --rail: #0f1b33;
  --text: #1b2333;
  --muted: #6b7489;
  --border: #e3e7ef;
  --bubble-me: #dbe7ff;
  --bubble-them: #ffffff;
  --ok: #16a34a;
  --warn: #d97706;
  --danger: #dc2626;
  --radius: 10px;
  --shadow: 0 6px 24px rgba(15, 27, 51, .12);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.45 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  color: var(--text); background: var(--bg);
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--brand); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

.btn {
  border: 1px solid var(--border); background: var(--panel); border-radius: 8px;
  padding: 8px 14px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px;
}
.btn:hover { background: #f0f3f9; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.danger { color: var(--danger); }
.btn.sm { padding: 4px 10px; font-size: 12px; }
.icon-btn {
  border: 0; background: transparent; width: 36px; height: 36px; border-radius: 8px;
  cursor: pointer; display: inline-grid; place-items: center; color: var(--muted); font-size: 18px;
}
.icon-btn:hover { background: #eef1f7; color: var(--text); }
.input {
  width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 9px 12px; background: #fff; outline: none;
}
.input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; font-weight: 600; }

.avatar {
  width: 38px; height: 38px; border-radius: 50%; display: inline-grid; place-items: center; flex: none;
  color: #fff; font-weight: 600; font-size: 14px; position: relative; text-transform: uppercase;
}
.avatar.sm { width: 28px; height: 28px; font-size: 11px; }
.avatar.lg { width: 72px; height: 72px; font-size: 26px; }
.avatar .dot {
  position: absolute; right: -1px; bottom: -1px; width: 11px; height: 11px; border-radius: 50%;
  background: #9aa3b5; border: 2px solid #fff;
}
.avatar .dot.on { background: var(--ok); }

/* ---------- login ---------- */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 16px;
  background: linear-gradient(135deg, #0f1b33 0%, #1e3a8a 100%); }
.login-card { background: #fff; width: 100%; max-width: 380px; padding: 32px; border-radius: 14px; box-shadow: var(--shadow); }
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card .btn { width: 100%; justify-content: center; padding: 10px; }
.error-box { background: #fdecec; color: var(--danger); padding: 8px 12px; border-radius: 8px; margin-bottom: 12px; }

/* ---------- app layout ---------- */
.app { display: flex; height: 100vh; overflow: hidden; }
.rail { width: 64px; background: var(--rail); display: flex; flex-direction: column; align-items: center; padding: 12px 0; gap: 8px; }
.rail .icon-btn { color: #aab4c8; }
.rail .icon-btn:hover, .rail .icon-btn.active { background: rgba(255,255,255,.1); color: #fff; }
.rail .spacer { flex: 1; }

.sidebar { width: 320px; background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-width: 0; }
.sidebar-head { padding: 14px 14px 10px; display: flex; align-items: center; gap: 8px; }
.sidebar-head h2 { margin: 0; font-size: 18px; flex: 1; }
.search-box { padding: 0 14px 10px; position: relative; }
.search-results {
  position: absolute; left: 14px; right: 14px; top: 44px; background: #fff; border: 1px solid var(--border);
  border-radius: 8px; box-shadow: var(--shadow); z-index: 20; max-height: 320px; overflow: auto;
}
.list { flex: 1; overflow-y: auto; }
.list-item { display: flex; gap: 10px; padding: 10px 14px; cursor: pointer; align-items: center; border-left: 3px solid transparent; }
.list-item:hover { background: #f5f7fb; }
.list-item.active { background: var(--brand-soft); border-left-color: var(--brand); }
.list-item .meta { flex: 1; min-width: 0; }
.list-item .top { display: flex; justify-content: space-between; gap: 8px; }
.list-item .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list-item .time { font-size: 11px; color: var(--muted); flex: none; }
.list-item .preview { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; justify-content: space-between; gap: 8px; }
.badge { background: var(--brand); color: #fff; border-radius: 10px; font-size: 11px; padding: 1px 7px; font-weight: 600; flex: none; }
.list-item.unread .name, .list-item.unread .preview span { color: var(--text); font-weight: 600; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--bg); }
.empty-state { flex: 1; display: grid; place-items: center; text-align: center; color: var(--muted); padding: 24px; }
.chat-head { background: var(--panel); border-bottom: 1px solid var(--border); padding: 10px 16px; display: flex; align-items: center; gap: 12px; }
.chat-head .title { flex: 1; min-width: 0; }
.chat-head .title b { display: block; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.messages { flex: 1; overflow-y: auto; padding: 16px 20px; display: flex; flex-direction: column; gap: 2px; }
.day-sep { text-align: center; margin: 14px 0 8px; }
.day-sep span { background: #e6eaf2; color: var(--muted); font-size: 12px; padding: 3px 10px; border-radius: 10px; }
.sys-msg { text-align: center; color: var(--muted); font-size: 12px; margin: 6px 0; }

.msg { display: flex; gap: 8px; max-width: 72%; margin-top: 6px; align-self: flex-start; }
.msg.me { align-self: flex-end; flex-direction: row-reverse; }
.msg.cont { margin-top: 1px; }
.msg.cont .avatar { visibility: hidden; }
.bubble {
  background: var(--bubble-them); border: 1px solid var(--border); border-radius: 12px; padding: 7px 11px;
  position: relative; min-width: 80px; word-wrap: break-word; overflow-wrap: anywhere;
}
.msg.me .bubble { background: var(--bubble-me); border-color: #c9d9fb; }
.bubble .sender { font-size: 12px; font-weight: 600; color: var(--brand); margin-bottom: 2px; }
.bubble .text { white-space: pre-wrap; }
.bubble .foot { display: flex; justify-content: flex-end; align-items: center; gap: 6px; font-size: 11px; color: var(--muted); margin-top: 2px; }
.bubble.deleted .text { font-style: italic; color: var(--muted); }
.bubble.revealed { border-style: dashed; border-color: var(--danger); }
.ticks { cursor: pointer; letter-spacing: -3px; padding-right: 3px; }
.ticks.seen { color: var(--brand); font-weight: 700; }
.seen-label { cursor: pointer; }
.seen-label:hover { text-decoration: underline; }
.msg-actions { display: none; position: absolute; top: -12px; right: 6px; background: #fff; border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow); }
.msg-actions button { border: 0; background: none; padding: 3px 8px; cursor: pointer; font-size: 12px; }
.msg-actions button:hover { background: #f0f3f9; }
.bubble:hover .msg-actions { display: flex; }
.file-card { display: flex; align-items: center; gap: 10px; padding: 8px; border: 1px solid var(--border); border-radius: 8px; background: rgba(255,255,255,.7); text-decoration: none; color: var(--text); margin: 2px 0; }
.file-card .ext { width: 36px; height: 40px; border-radius: 6px; background: var(--brand); color: #fff; display: grid; place-items: center; font-size: 10px; font-weight: 700; text-transform: uppercase; flex: none; }
.file-card .fname { font-weight: 600; word-break: break-all; }
.img-preview { max-width: 280px; max-height: 260px; border-radius: 8px; display: block; cursor: zoom-in; margin: 2px 0; }

.typing { height: 20px; padding: 0 20px; font-size: 12px; color: var(--muted); font-style: italic; }
.composer { background: var(--panel); border-top: 1px solid var(--border); padding: 10px 14px; display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; max-height: 140px; min-height: 40px; border: 1px solid var(--border); border-radius: 10px; padding: 9px 12px; outline: none; }
.composer textarea:focus { border-color: var(--brand); }
.drop-hint { position: absolute; inset: 0; background: rgba(37,99,235,.12); border: 3px dashed var(--brand); display: grid; place-items: center; font-size: 18px; color: var(--brand); font-weight: 600; z-index: 30; pointer-events: none; }
.monitor-banner { background: #fff7e6; color: #8a5a00; border-bottom: 1px solid #f3d9a4; padding: 6px 16px; font-size: 13px; }

.drawer { width: 300px; background: var(--panel); border-left: 1px solid var(--border); display: flex; flex-direction: column; overflow-y: auto; }
.drawer-head { padding: 14px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--border); }
.drawer section { padding: 14px; border-bottom: 1px solid var(--border); }
.member-row { display: flex; align-items: center; gap: 8px; padding: 6px 0; }
.member-row .meta { flex: 1; min-width: 0; }
.tag { font-size: 10px; background: var(--brand-soft); color: var(--brand); border-radius: 6px; padding: 1px 6px; font-weight: 600; }

/* ---------- modal ---------- */
.modal-back { position: fixed; inset: 0; background: rgba(15,27,51,.45); display: grid; place-items: center; z-index: 100; padding: 16px; }
.modal { background: #fff; border-radius: 14px; width: 100%; max-width: 460px; max-height: 90vh; overflow: auto; box-shadow: var(--shadow); }
.modal-head { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
.modal-head h3 { margin: 0; font-size: 17px; }
.modal-body { padding: 16px 20px; }
.modal-foot { padding: 12px 20px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
.chip { background: var(--brand-soft); color: var(--brand); border-radius: 14px; padding: 3px 4px 3px 10px; display: inline-flex; align-items: center; gap: 4px; font-size: 13px; }
.chip button { border: 0; background: none; cursor: pointer; color: inherit; font-size: 14px; }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: grid; place-items: center; z-index: 200; cursor: zoom-out; }
.lightbox img { max-width: 94vw; max-height: 94vh; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #1b2333; color: #fff; padding: 10px 18px; border-radius: 8px; z-index: 300; box-shadow: var(--shadow); }

/* ---------- calls ---------- */
.call-overlay { position: fixed; inset: 0; background: #0b1324; z-index: 150; display: flex; flex-direction: column; color: #fff; }
.call-videos { flex: 1; min-height: 0; position: relative; overflow: hidden; }
.call-videos video#remoteVideo { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #000; }
.call-videos video#localVideo { position: absolute; right: 16px; bottom: 16px; width: 220px; max-width: 35vw; border-radius: 10px; background: #222; border: 2px solid rgba(255,255,255,.2); }
.call-info { position: absolute; top: 20px; left: 0; right: 0; text-align: center; }
.call-info .avatar { margin-bottom: 10px; }
.call-controls { flex: none; display: flex; justify-content: center; gap: 14px; padding: 18px; }
.call-controls button { width: 54px; height: 54px; border-radius: 50%; border: 0; cursor: pointer; font-size: 20px; background: rgba(255,255,255,.15); color: #fff; }
.call-controls button.off { background: #fff; color: #0b1324; }
.call-controls button.hangup { background: var(--danger); }
.call-controls button.accept { background: var(--ok); }

/* ---------- admin ---------- */
.admin-nav { width: 220px; background: var(--rail); color: #cdd5e4; display: flex; flex-direction: column; padding: 16px 10px; gap: 2px; }
.admin-nav h1 { color: #fff; font-size: 17px; margin: 0 8px 16px; }
.admin-nav a { color: inherit; text-decoration: none; padding: 9px 12px; border-radius: 8px; display: block; cursor: pointer; }
.admin-nav a:hover, .admin-nav a.active { background: rgba(255,255,255,.1); color: #fff; }
.admin-main { flex: 1; overflow: auto; padding: 24px 28px; }
.admin-main h2 { margin: 0 0 16px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px; margin-bottom: 20px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.card .num { font-size: 28px; font-weight: 700; }
.panel { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; margin-bottom: 18px; }
.panel h3 { margin: 0 0 12px; font-size: 15px; }
table.grid { width: 100%; border-collapse: collapse; background: var(--panel); }
table.grid th, table.grid td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.grid th { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.table-wrap { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); overflow: auto; }
.pill { font-size: 11px; padding: 2px 8px; border-radius: 10px; font-weight: 600; }
.pill.ok { background: #e3f6ea; color: var(--ok); }
.pill.bad { background: #fdecec; color: var(--danger); }
.pill.warn { background: #fff4e0; color: var(--warn); }
.toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.monitor { display: flex; height: calc(100vh - 110px); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; background: var(--panel); }
.monitor .sidebar { width: 300px; }
.switch { display: flex; align-items: center; gap: 8px; cursor: pointer; margin-bottom: 12px; }

@media (max-width: 800px) {
  .rail { display: none; }
  .sidebar { width: 100%; }
  .app.in-chat .sidebar { display: none; }
  .app:not(.in-chat) .main { display: none; }
  .drawer { position: fixed; inset: 0; width: auto; z-index: 50; }
  .msg { max-width: 88%; }
  .admin-nav { width: 64px; }
  .admin-nav h1, .admin-nav a span { display: none; }
  .row2 { grid-template-columns: 1fr; }
  .admin-main { padding: 16px; }
}
.mobile-only { display: none; }
@media (max-width: 800px) { .mobile-only { display: inline-grid; } }
