:root {
    --bg: #f2f5fb;
    --panel: rgba(255,255,255,.88);
    --panel-solid: #ffffff;
    --panel-2: #f6f8fc;
    --text: #111827;
    --muted: #667085;
    --line: rgba(15,23,42,.09);
    --accent: #5b5cf0;
    --accent-2: #20b7a5;
    --accent-soft: rgba(91,92,240,.10);
    --mine: linear-gradient(135deg,#5b5cf0 0%,#7c4dff 100%);
    --theirs: #ffffff;
    --danger: #e5484d;
    --success: #18a67e;
    --shadow: 0 22px 60px rgba(15,23,42,.13);
    --shadow-soft: 0 8px 24px rgba(15,23,42,.08);
    --radius: 24px;
    color-scheme: light;
}

html[data-theme="dark"] {
    --bg: #070b14;
    --panel: rgba(14,20,35,.88);
    --panel-solid: #0e1423;
    --panel-2: #111a2c;
    --text: #edf2ff;
    --muted: #8e9ab1;
    --line: rgba(255,255,255,.075);
    --accent: #7a7cff;
    --accent-2: #35d0bc;
    --accent-soft: rgba(122,124,255,.14);
    --theirs: #151e31;
    --shadow: 0 24px 70px rgba(0,0,0,.4);
    --shadow-soft: 0 10px 30px rgba(0,0,0,.22);
    color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
    font-family: Tahoma, Arial, "Segoe UI", sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
    overflow-x: hidden;
}
button, input, textarea, select { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
button { color: inherit; }
.hidden { display: none !important; }

/* Brand */
.brand-lockup { display: flex; align-items: center; gap: 13px; }
.brand-mark {
    width: 46px; height: 46px; border-radius: 16px; display: grid; place-items: center;
    color: white; font-weight: 900; font-size: 21px; letter-spacing: -1px;
    background: linear-gradient(145deg,var(--accent),#8a54ff 55%,var(--accent-2));
    box-shadow: 0 10px 26px rgba(91,92,240,.30), inset 0 1px 0 rgba(255,255,255,.25);
}
.brand-lockup h1 { margin: 0; font-size: 24px; letter-spacing: -.5px; }
.brand-lockup p { margin: 3px 0 0; color: var(--muted); font-size: 12px; }
.brand-lockup.compact .brand-mark { width: 42px; height: 42px; border-radius: 14px; }
.brand-text { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.brand-text strong { font-size: 16px; }
.brand-text span { color: var(--muted); font-size: 10px; letter-spacing: 1px; }

/* Auth / Installer */
.auth-body {
    min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 28px 16px;
    background:
        radial-gradient(circle at 20% 15%, rgba(91,92,240,.18), transparent 30%),
        radial-gradient(circle at 85% 80%, rgba(32,183,165,.15), transparent 26%), var(--bg);
}
.auth-shell, .install-shell { width: min(100%, 460px); }
.install-shell { width: min(100%, 760px); }
.auth-card {
    position: relative; overflow: hidden; padding: 30px; border: 1px solid var(--line); border-radius: 30px;
    background: var(--panel); backdrop-filter: blur(22px); -webkit-backdrop-filter: blur(22px); box-shadow: var(--shadow);
}
.auth-card::before {
    content: ""; position: absolute; inset: -100px auto auto -110px; width: 230px; height: 230px; border-radius: 50%;
    background: radial-gradient(circle, rgba(91,92,240,.18), transparent 68%); pointer-events: none;
}
.auth-brand { margin-bottom: 34px; }
.auth-copy .eyebrow, .eyebrow { display: block; color: var(--accent); font-size: 10px; font-weight: 800; letter-spacing: 2px; margin-bottom: 8px; }
.auth-copy h2 { margin: 0 0 8px; font-size: 29px; letter-spacing: -.7px; }
.auth-copy p { margin: 0 0 24px; color: var(--muted); font-size: 13px; line-height: 1.9; }
.stack-form { display: grid; gap: 14px; }
.stack-form label, .admin-create-grid label { display: grid; gap: 8px; color: var(--muted); font-size: 12px; font-weight: 700; }
input, select {
    width: 100%; min-height: 48px; border: 1px solid var(--line); border-radius: 14px; padding: 0 14px;
    background: var(--panel-2); color: var(--text); outline: none; transition: .18s ease;
}
input:focus, select:focus, textarea:focus { border-color: color-mix(in srgb, var(--accent) 65%, transparent); box-shadow: 0 0 0 4px var(--accent-soft); }
.primary-btn, .secondary-btn {
    min-height: 48px; border-radius: 14px; border: 0; padding: 0 18px; cursor: pointer; font-weight: 800; text-decoration: none;
    display: inline-flex; align-items: center; justify-content: center; transition: transform .16s ease, filter .16s ease;
}
.primary-btn { color: white; background: linear-gradient(135deg,var(--accent),#8355ff); box-shadow: 0 10px 24px rgba(91,92,240,.25); }
.primary-btn:hover { filter: brightness(1.06); transform: translateY(-1px); }
.primary-btn:disabled { opacity: .45; cursor: not-allowed; transform: none; }
.secondary-btn { color: var(--text); border: 1px solid var(--line); background: var(--panel); }
.full-btn { width: 100%; margin-top: 4px; }
.auth-foot { text-align: center; color: var(--muted); font-size: 10px; margin: 20px 0 0; }
.notice { border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; margin: 0 0 16px; font-size: 12px; line-height: 1.8; }
.notice.error { color: #ff777c; background: rgba(229,72,77,.09); border-color: rgba(229,72,77,.20); }
.notice.success { color: var(--success); background: rgba(24,166,126,.09); border-color: rgba(24,166,126,.20); }
.install-card { padding: 32px; }
.requirements { display: flex; flex-wrap: wrap; gap: 8px; margin: 24px 0; }
.req { border: 1px solid var(--line); border-radius: 999px; padding: 7px 10px; font-size: 10px; background: var(--panel-2); }
.req.ok { color: var(--success); } .req.bad { color: var(--danger); }
.form-grid { display: grid; gap: 12px; } .form-grid.two { grid-template-columns: 1fr 1fr; }
.divider { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 11px; margin: 4px 0; }
.divider::before, .divider::after { content: ""; height: 1px; flex: 1; background: var(--line); }

/* Chat shell */
.chat-body { height: 100vh; height: 100dvh; overflow: hidden; }
.chat-app { height: 100%; width: 100%; display: grid; grid-template-columns: 360px minmax(0,1fr); direction: ltr; background: var(--bg); }
.sidebar, .conversation { direction: rtl; min-width: 0; }
.sidebar {
    display: flex; flex-direction: column; height: 100%; border-right: 1px solid var(--line); background: var(--panel);
    backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px); z-index: 5;
}
.sidebar-head { min-height: 76px; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.head-actions { display: flex; gap: 7px; }
.icon-btn {
    width: 40px; height: 40px; border-radius: 13px; border: 1px solid var(--line); background: var(--panel-2); display: grid;
    place-items: center; cursor: pointer; text-decoration: none; color: var(--text); transition: .15s ease;
}
.icon-btn:hover { background: var(--accent-soft); border-color: rgba(91,92,240,.25); transform: translateY(-1px); }
.link-btn { line-height: 1; }
.profile-strip { display: flex; align-items: center; gap: 10px; padding: 14px 16px 10px; }
.avatar {
    flex: 0 0 auto; width: 46px; height: 46px; border-radius: 50%; display: grid; place-items: center; color: white;
    background: linear-gradient(145deg,#6477ff,#8454ff 58%,#2fc6b1); font-size: 16px; font-weight: 900;
    box-shadow: inset 0 1px 0 rgba(255,255,255,.2);
}
.self-avatar { width: 40px; height: 40px; font-size: 14px; }
.profile-meta { min-width: 0; display: flex; flex: 1; flex-direction: column; gap: 3px; }
.profile-meta strong { font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.profile-meta span { color: var(--muted); font-size: 10px; display: flex; align-items: center; gap: 5px; }
.status-dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: #788398; }
.status-dot.online { background: #23c997; box-shadow: 0 0 0 3px rgba(35,201,151,.11); }
.status-dot.offline { background: #f59e0b; }
.logout-form { margin: 0; }
.ghost-btn { border: 0; background: transparent; color: var(--muted); padding: 8px; cursor: pointer; font-size: 10px; }
.ghost-btn:hover { color: var(--danger); }
.search-box { margin: 8px 14px 10px; height: 44px; display: flex; align-items: center; gap: 8px; padding: 0 12px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 14px; }
.search-box span { color: var(--muted); font-size: 21px; transform: rotate(-20deg); }
.search-box input { min-height: 0; height: 100%; padding: 0; border: 0; background: transparent; box-shadow: none; font-size: 12px; }
.search-box input:focus { box-shadow: none; }
.section-label { display: flex; justify-content: space-between; padding: 4px 18px 8px; color: var(--muted); font-size: 10px; font-weight: 800; letter-spacing: .5px; }
.contact-list { flex: 1; overflow-y: auto; padding: 0 8px 16px; overscroll-behavior: contain; scrollbar-width: thin; scrollbar-color: rgba(127,127,127,.25) transparent; }
.contact {
    width: 100%; min-height: 72px; display: flex; align-items: center; gap: 11px; border: 0; border-radius: 17px; padding: 8px 10px;
    background: transparent; cursor: pointer; text-align: right; transition: background .15s ease, transform .15s ease;
}
.contact:hover { background: rgba(127,127,127,.06); }
.contact.active { background: var(--accent-soft); }
.contact-avatar-wrap { position: relative; flex: 0 0 auto; }
.contact .avatar { width: 48px; height: 48px; }
.presence { position: absolute; left: 1px; bottom: 1px; width: 12px; height: 12px; background: #6f7b90; border: 3px solid var(--panel-solid); border-radius: 50%; }
.presence.online { background: #24c795; }
.contact-content { flex: 1; min-width: 0; display: grid; gap: 7px; }
.contact-top, .contact-bottom { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-width: 0; }
.contact-top strong { font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.contact-top span { color: var(--muted); font-size: 9px; flex: 0 0 auto; }
.last-preview { min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; color: var(--muted); font-size: 10px; }
.unread-badge { min-width: 20px; height: 20px; padding: 0 6px; border-radius: 999px; display: grid; place-items: center; background: var(--accent); color: white; font-size: 9px; }
.no-contacts { margin: 24px 10px; padding: 18px; text-align: center; color: var(--muted); font-size: 11px; border: 1px dashed var(--line); border-radius: 16px; }
.sidebar-skeleton { display: grid; gap: 10px; padding: 10px; }
.sidebar-skeleton span { height: 68px; border-radius: 16px; background: linear-gradient(90deg,var(--panel-2),var(--accent-soft),var(--panel-2)); background-size: 220% 100%; animation: shimmer 1.4s infinite; }
@keyframes shimmer { to { background-position: -220% 0; } }

/* Conversation */
.conversation { height: 100%; min-width: 0; position: relative; overflow: hidden; background:
    radial-gradient(circle at 80% 10%, rgba(91,92,240,.06), transparent 27%),
    radial-gradient(circle at 15% 90%, rgba(32,183,165,.055), transparent 24%), var(--bg); }
.empty-chat { height: 100%; display: grid; place-content: center; justify-items: center; text-align: center; padding: 24px; }
.empty-orb { width: 94px; height: 94px; border-radius: 30px; display: grid; place-items: center; background: linear-gradient(145deg,var(--accent-soft),rgba(32,183,165,.12)); border: 1px solid var(--line); box-shadow: var(--shadow-soft); transform: rotate(8deg); }
.empty-orb span { font-size: 36px; color: var(--accent); transform: rotate(-8deg); }
.empty-chat h2 { margin: 24px 0 7px; font-size: 20px; }
.empty-chat p { margin: 0; max-width: 420px; color: var(--muted); font-size: 12px; line-height: 1.9; }
.chat-panel { height: 100%; display: grid; grid-template-rows: auto minmax(0,1fr) auto auto; }
.chat-head { min-height: 76px; display: flex; align-items: center; gap: 11px; padding: 12px 18px; background: color-mix(in srgb,var(--panel-solid) 88%, transparent); border-bottom: 1px solid var(--line); backdrop-filter: blur(18px); z-index: 4; }
.chat-head .avatar { width: 46px; height: 46px; }
.peer-meta { display: flex; flex-direction: column; min-width: 0; gap: 4px; }
.peer-meta strong { font-size: 13px; white-space: nowrap; text-overflow: ellipsis; overflow: hidden; }
.peer-meta span { font-size: 10px; color: var(--muted); }
.peer-meta span.typing-status { color: var(--accent-2); }
.chat-head-spacer { flex: 1; }
.secure-pill { flex: 0 0 auto; border: 1px solid var(--line); background: var(--panel-2); color: var(--muted); border-radius: 999px; padding: 7px 10px; font-size: 9px; }
.mobile-back { display: none; }
.messages-wrap { position: relative; overflow-y: auto; overscroll-behavior: contain; padding: 18px clamp(14px,3vw,46px) 24px; scroll-behavior: auto; scrollbar-width: thin; scrollbar-color: rgba(127,127,127,.28) transparent; }
.messages { min-height: 100%; display: flex; flex-direction: column; justify-content: flex-end; gap: 5px; }
.messages.loading { opacity: .65; }
.load-older { display: none; width: max-content; max-width: 90%; margin: 0 auto 14px; color: var(--muted); background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 7px 12px; font-size: 9px; cursor: pointer; box-shadow: var(--shadow-soft); }
.load-older.visible { display: block; }
.date-separator { align-self: center; margin: 14px 0 10px; padding: 5px 10px; border-radius: 999px; color: var(--muted); background: color-mix(in srgb,var(--panel-solid) 88%,transparent); border: 1px solid var(--line); font-size: 9px; backdrop-filter: blur(8px); }
.message { display: flex; width: 100%; margin: 1px 0; animation: msgIn .18s ease both; }
@keyframes msgIn { from { opacity: 0; transform: translateY(6px) scale(.99); } to { opacity: 1; transform: none; } }
.message.mine { justify-content: flex-end; }
.message.theirs { justify-content: flex-start; }
.bubble { position: relative; max-width: min(72%,680px); min-width: 74px; padding: 9px 12px 6px; border-radius: 17px; box-shadow: 0 2px 9px rgba(15,23,42,.055); overflow-wrap: anywhere; }
.mine .bubble { color: white; background: var(--mine); border-bottom-right-radius: 6px; box-shadow: 0 7px 18px rgba(91,92,240,.15); }
.theirs .bubble { color: var(--text); background: var(--theirs); border: 1px solid var(--line); border-bottom-left-radius: 6px; }
.message-text { white-space: pre-wrap; font-size: 13px; line-height: 1.8; unicode-bidi: plaintext; }
.message-meta { margin-top: 4px; display: flex; justify-content: flex-end; align-items: center; gap: 5px; font-size: 8px; opacity: .76; direction: ltr; }
.checks { letter-spacing: -2px; font-weight: 900; }
.checks.read { color: #72f0dc; }
.message.pending { opacity: .65; }
.message.failed .bubble { outline: 1px solid var(--danger); }
.new-message-btn { position: sticky; bottom: 10px; margin: 10px auto 0; display: block; border: 0; border-radius: 999px; padding: 9px 14px; color: white; background: var(--accent); box-shadow: 0 10px 28px rgba(91,92,240,.32); cursor: pointer; font-size: 10px; z-index: 4; }
.typing-line { height: 0; overflow: hidden; display: flex; align-items: center; gap: 7px; padding: 0 clamp(18px,3vw,50px); color: var(--accent-2); font-size: 9px; background: var(--bg); transition: height .16s ease; }
.typing-line.visible { height: 24px; }
.typing-dots { display: flex; gap: 2px; direction: ltr; }
.typing-dots i { width: 4px; height: 4px; border-radius: 50%; background: currentColor; animation: dot 1s infinite ease-in-out; }
.typing-dots i:nth-child(2) { animation-delay: .13s; } .typing-dots i:nth-child(3) { animation-delay: .26s; }
@keyframes dot { 0%,70%,100% { transform: translateY(0); opacity: .4; } 35% { transform: translateY(-3px); opacity: 1; } }
.composer { display: flex; align-items: flex-end; gap: 10px; padding: 10px clamp(14px,3vw,36px) calc(10px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: color-mix(in srgb,var(--panel-solid) 92%,transparent); backdrop-filter: blur(20px); }
.composer-box { flex: 1; min-width: 0; border: 1px solid var(--line); background: var(--panel-2); border-radius: 19px; padding: 7px 12px 6px; transition: .16s ease; }
.composer-box:focus-within { border-color: rgba(91,92,240,.45); box-shadow: 0 0 0 4px var(--accent-soft); }
.composer textarea { display: block; width: 100%; min-height: 26px; max-height: 140px; resize: none; overflow-y: auto; border: 0; outline: none; background: transparent; color: var(--text); line-height: 1.7; font-size: 13px; padding: 2px 0; box-shadow: none; }
.composer textarea:focus { box-shadow: none; }
.composer-bottom { min-height: 14px; display: flex; justify-content: space-between; gap: 8px; color: var(--muted); font-size: 8px; opacity: .75; }
.send-btn { flex: 0 0 auto; width: 50px; height: 50px; display: grid; place-items: center; border: 0; border-radius: 17px; color: white; background: linear-gradient(145deg,var(--accent),#7e55ff); box-shadow: 0 10px 26px rgba(91,92,240,.3); cursor: pointer; transition: transform .14s ease, filter .14s ease; }
.send-btn span { font-size: 18px; transform: rotate(180deg) translateX(-1px); }
.send-btn:hover { filter: brightness(1.07); transform: translateY(-1px); }
.send-btn:active { transform: scale(.95); }

/* Admin */
.admin-body { min-height: 100vh; background: radial-gradient(circle at 80% 0,rgba(91,92,240,.10),transparent 28%),var(--bg); }
.admin-shell { width: min(1180px,calc(100% - 28px)); margin: 0 auto; padding: 34px 0 60px; }
.admin-top { display: flex; align-items: flex-end; justify-content: space-between; gap: 20px; margin-bottom: 24px; }
.admin-top h1 { margin: 0 0 8px; font-size: 30px; }
.admin-top p { margin: 0; color: var(--muted); font-size: 12px; }
.admin-card { margin-top: 16px; padding: 20px; border: 1px solid var(--line); border-radius: 22px; background: var(--panel); box-shadow: var(--shadow-soft); }
.admin-card h2 { margin: 0 0 16px; font-size: 16px; }
.admin-create-grid { display: grid; grid-template-columns: 1.1fr 1.2fr 1.2fr .8fr auto; gap: 10px; align-items: end; }
.admin-create-grid .primary-btn { min-height: 48px; }
.section-title-row { display: flex; align-items: center; justify-content: space-between; }
.section-title-row span { color: var(--muted); font-size: 10px; }
.user-table-wrap { overflow-x: auto; }
.user-table { width: 100%; border-collapse: collapse; min-width: 800px; }
.user-table th, .user-table td { text-align: right; border-top: 1px solid var(--line); padding: 12px 10px; font-size: 11px; vertical-align: middle; }
.user-table th { color: var(--muted); font-size: 9px; }
.table-user { display: flex; align-items: center; gap: 9px; }
.small-avatar { width: 34px; height: 34px; font-size: 11px; }
.user-table code { color: var(--muted); }
.role-pill, .state-pill { display: inline-flex; padding: 5px 8px; border-radius: 999px; background: var(--panel-2); border: 1px solid var(--line); font-size: 9px; }
.role-pill.admin { color: var(--accent); background: var(--accent-soft); }
.state-pill.active { color: var(--success); } .state-pill.off { color: var(--danger); }
.table-actions { display: flex; align-items: center; gap: 7px; flex-wrap: wrap; }
.table-actions form { margin: 0; }
.tiny-btn { min-height: 32px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel-2); color: var(--text); padding: 0 10px; font-size: 9px; cursor: pointer; }
.tiny-btn.accent { color: var(--accent); }
.reset-form { display: flex; align-items: center; gap: 5px; }
.reset-form input { width: 115px; min-height: 32px; height: 32px; border-radius: 10px; font-size: 9px; }

/* Toast */
.toast-box { position: fixed; left: 18px; bottom: calc(18px + env(safe-area-inset-bottom)); z-index: 999; display: grid; gap: 8px; pointer-events: none; }
.toast { max-width: min(320px,calc(100vw - 36px)); padding: 11px 14px; border-radius: 13px; background: var(--panel-solid); color: var(--text); border: 1px solid var(--line); box-shadow: var(--shadow); font-size: 11px; opacity: 0; transform: translateY(8px); transition: .2s ease; }
.toast.show { opacity: 1; transform: none; } .toast.error { border-color: rgba(229,72,77,.25); color: #ff777c; }

/* Scrollbars */
::-webkit-scrollbar { width: 7px; height: 7px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(127,127,127,.24); border-radius: 999px; }

@media (max-width: 980px) {
    .chat-app { grid-template-columns: 320px minmax(0,1fr); }
    .secure-pill { display: none; }
    .admin-create-grid { grid-template-columns: 1fr 1fr; }
    .admin-create-grid .primary-btn { grid-column: span 2; }
}

@media (max-width: 820px) {
    .chat-app { display: block; position: relative; }
    .sidebar, .conversation { position: absolute; inset: 0; width: 100%; height: 100%; transition: transform .25s cubic-bezier(.22,.7,.2,1); }
    .sidebar { transform: translateX(0); border-right: 0; }
    .conversation { transform: translateX(-100%); z-index: 10; }
    .chat-app.chat-open .sidebar { transform: translateX(25%); }
    .chat-app.chat-open .conversation { transform: translateX(0); }
    .mobile-back { display: grid; }
    .chat-head { min-height: 68px; padding: 9px 10px; }
    .chat-head .avatar { width: 40px; height: 40px; }
    .messages-wrap { padding: 12px 10px 18px; }
    .bubble { max-width: 86%; }
    .composer { padding: 8px 9px calc(8px + env(safe-area-inset-bottom)); gap: 7px; }
    .send-btn { width: 48px; height: 48px; border-radius: 16px; }
    .shortcut-hint { display: none; }
    .typing-line { padding: 0 14px; }
    .sidebar-head { padding-top: calc(12px + env(safe-area-inset-top)); min-height: calc(72px + env(safe-area-inset-top)); }
    .chat-head { padding-top: calc(8px + env(safe-area-inset-top)); min-height: calc(66px + env(safe-area-inset-top)); }
    .contact { min-height: 76px; }
    .admin-top { align-items: flex-start; flex-direction: column; }
}

@media (max-width: 620px) {
    .auth-card, .install-card { padding: 22px 18px; border-radius: 24px; }
    .form-grid.two { grid-template-columns: 1fr; }
    .admin-shell { width: min(100% - 20px,1180px); padding-top: 20px; }
    .admin-card { padding: 14px; border-radius: 18px; }
    .admin-create-grid { grid-template-columns: 1fr; }
    .admin-create-grid .primary-btn { grid-column: auto; }
    .admin-top h1 { font-size: 24px; }
    .message-text { font-size: 12px; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; scroll-behavior: auto !important; transition-duration: .01ms !important; }
}
