/* ============================================================
   Realtime Chat — dark theme, desktop-first, mobile responsive
   ============================================================ */

:root {
    --bg-0: #0b0d12;
    --bg-1: #12151c;
    --bg-2: #181c25;
    --bg-3: #1f2430;
    --bg-hover: #232938;
    --border: #272d3a;
    --text: #e6e9ef;
    --text-dim: #9aa3b2;
    --text-faint: #6b7280;
    --accent: #5865f2;
    --accent-hover: #6b78ff;
    --accent-soft: rgba(88, 101, 242, 0.15);
    --green: #3ba55d;
    --red: #ed4245;
    --yellow: #faa61a;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
    --font: "Segoe UI", system-ui, -apple-system, Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
    height: 100%;
    margin: 0;
}

body {
    font-family: var(--font);
    background: radial-gradient(1200px 800px at 80% -10%, #161a25 0%, var(--bg-0) 55%);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
}

button { font-family: inherit; cursor: pointer; }
input { font-family: inherit; }

/* The HTML `hidden` attribute must always win over component display rules
   (.lightbox, .composer, .upload-preview, .badge all set `display`, which
   would otherwise override the low-specificity UA `[hidden]` rule). */
[hidden] { display: none !important; }

/* ---------- Buttons ---------- */
.btn {
    border: none;
    border-radius: var(--radius-sm);
    padding: 10px 16px;
    font-size: 14px;
    font-weight: 600;
    color: #fff;
    background: var(--bg-3);
    transition: background .15s, transform .05s;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { background: transparent; color: var(--text-dim); }
.btn-ghost:hover { background: var(--bg-hover); color: var(--text); }
.btn-block { width: 100%; }
.btn-sm { padding: 6px 12px; font-size: 13px; }

.icon-btn {
    border: none;
    background: var(--bg-3);
    color: var(--text-dim);
    border-radius: var(--radius-sm);
    width: 42px;
    height: 42px;
    font-size: 18px;
    display: grid;
    place-items: center;
    transition: background .15s, color .15s;
    flex: 0 0 auto;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }

/* ============================================================
   LOGIN
   ============================================================ */
.login-wrap {
    min-height: 100%;
    display: grid;
    place-items: center;
    padding: 24px;
}
.login-card {
    width: 100%;
    max-width: 400px;
    background: var(--bg-1);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 36px 32px;
}
.login-brand { text-align: center; margin-bottom: 26px; }
.login-logo { font-size: 44px; }
.login-brand h1 { margin: 8px 0 4px; font-size: 24px; }
.login-sub { margin: 0; color: var(--text-dim); font-size: 14px; }

.field { display: block; margin-bottom: 18px; }
.field-label {
    display: block;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--text-dim);
    margin-bottom: 7px;
    font-weight: 700;
}
.field input {
    width: 100%;
    padding: 12px 14px;
    background: var(--bg-0);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 15px;
    outline: none;
    transition: border-color .15s, box-shadow .15s;
}
.field input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}
.field textarea {
    width: 100%;
    padding: 12px 14px;
    background: var(--bg-0);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 15px;
    font-family: inherit;
    line-height: 1.5;
    resize: vertical;
    outline: none;
    transition: border-color .15s, box-shadow .15s;
}
.field textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}
.captcha-row { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
#captcha-img {
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    display: block;
}
.form-error {
    background: rgba(237, 66, 69, 0.12);
    border: 1px solid rgba(237, 66, 69, 0.4);
    color: #ff9ea0;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    margin-bottom: 16px;
}

/* ============================================================
   APP SHELL
   ============================================================ */
.app {
    height: 100vh;
    display: flex;
    flex-direction: column;
}

.topbar {
    height: 60px;
    flex: 0 0 60px;
    display: flex;
    align-items: center;
    gap: 18px;
    padding: 0 18px;
    background: var(--bg-1);
    border-bottom: 1px solid var(--border);
}
.topbar-brand { font-weight: 700; font-size: 16px; white-space: nowrap; }
.topbar-brand span { display: inline-block; }

.tabs { display: flex; gap: 6px; flex: 1; }
.tab {
    position: relative;
    border: none;
    background: transparent;
    color: var(--text-dim);
    padding: 9px 16px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 600;
    transition: background .15s, color .15s;
}
.tab:hover { background: var(--bg-2); color: var(--text); }
.tab.is-active { background: var(--accent-soft); color: #c9cfff; }

.topbar-user { display: flex; align-items: center; gap: 10px; }
.me-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--green); box-shadow: 0 0 8px var(--green); }
.me-name { font-weight: 600; font-size: 14px; }

.lang-switch { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; }
.lang-opt {
    padding: 5px 9px;
    font-size: 12px;
    font-weight: 700;
    color: var(--text-dim);
    text-decoration: none;
    transition: background .15s, color .15s;
}
.lang-opt:hover { background: var(--bg-2); color: var(--text); }
.lang-opt.is-active { background: var(--accent-soft); color: #c9cfff; }

.badge {
    display: inline-grid;
    place-items: center;
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    border-radius: 9px;
    background: var(--red);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    margin-left: 6px;
}

/* ---------- Views ---------- */
.view { flex: 1; min-height: 0; display: none; }
.view.is-active { display: grid; }
.view-public { grid-template-columns: 1fr 260px; }
.view-private { grid-template-columns: 300px 1fr; }

/* ---------- Chat main column ---------- */
.chat-main {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    background: var(--bg-0);
}
.chat-header {
    height: 54px;
    flex: 0 0 54px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-1);
}
.chat-header h2 { margin: 0; font-size: 16px; }
.online-count { font-size: 13px; color: var(--text-dim); }
.online-count #online-count { color: var(--green); font-weight: 700; }

.messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 18px 20px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

/* ---------- Message bubbles ---------- */
.msg {
    display: flex;
    gap: 12px;
    padding: 6px 8px;
    border-radius: var(--radius-sm);
}
.msg:hover { background: var(--bg-1); }
.msg.grouped { padding-top: 0; }
.msg.grouped .msg-avatar { visibility: hidden; height: 0; }
.msg-avatar {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    display: grid;
    place-items: center;
    font-weight: 700;
    color: #fff;
    flex: 0 0 38px;
    text-transform: uppercase;
}
.msg-body { min-width: 0; flex: 1; }
.msg-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 2px; }
.msg-author { font-weight: 700; font-size: 14px; }
.msg-author.clickable { cursor: pointer; }
.msg-author.clickable:hover { text-decoration: underline; }
.msg-time { font-size: 11px; color: var(--text-faint); }
.msg-text { font-size: 14.5px; line-height: 1.45; word-wrap: break-word; overflow-wrap: anywhere; white-space: pre-wrap; }
.msg.mine .msg-author { color: #c9cfff; }

.msg-image {
    margin-top: 6px;
    max-width: 320px;
    max-height: 320px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    cursor: pointer;
    display: block;
}

/* system / welcome line */
.msg-system {
    text-align: center;
    color: var(--text-faint);
    font-size: 13px;
    padding: 8px;
}

/* ---------- Private message bubbles ---------- */
.pm { display: flex; margin: 3px 0; }
.pm.mine { justify-content: flex-end; }
.pm-bubble {
    max-width: 72%;
    padding: 9px 12px;
    border-radius: 14px;
    background: var(--bg-2);
    font-size: 14.5px;
    line-height: 1.45;
}
.pm.mine .pm-bubble { background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.pm.theirs .pm-bubble { border-bottom-left-radius: 4px; }
.pm-sender { font-size: 12px; font-weight: 700; color: var(--text-dim); margin-bottom: 3px; }
.pm.mine .pm-sender { display: none; }
.pm-text { white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.pm-time { display: block; font-size: 10.5px; opacity: .65; margin-top: 4px; text-align: right; }

/* ---------- Composer ---------- */
.composer {
    flex: 0 0 auto;
    display: flex;
    gap: 10px;
    padding: 14px 18px;
    border-top: 1px solid var(--border);
    background: var(--bg-1);
}
.composer input[type="text"] {
    flex: 1;
    min-width: 0;
    padding: 12px 14px;
    background: var(--bg-0);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 15px;
    outline: none;
}
.composer input[type="text"]:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

/* upload preview strip */
.upload-preview {
    display: flex;
    gap: 14px;
    align-items: center;
    padding: 12px 18px;
    border-top: 1px solid var(--border);
    background: var(--bg-2);
}
.upload-preview img { width: 64px; height: 64px; object-fit: cover; border-radius: var(--radius-sm); }
.upload-meta { display: flex; flex-direction: column; gap: 8px; font-size: 13px; color: var(--text-dim); }
.upload-actions { display: flex; gap: 8px; }

/* ---------- Sidebars ---------- */
.sidebar {
    background: var(--bg-1);
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow-y: auto;
}
.sidebar-conversations { border-left: none; border-right: 1px solid var(--border); }
.sidebar-title {
    padding: 16px 18px 10px;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: .05em;
    color: var(--text-faint);
    font-weight: 700;
}

.user-list, .conversation-list { list-style: none; margin: 0; padding: 0 8px 12px; }

.user-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 10px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background .12s;
}
.user-item:hover { background: var(--bg-hover); }
.status-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--green); flex: 0 0 9px; box-shadow: 0 0 6px var(--green); }
.user-name { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-item .you-tag { margin-left: auto; font-size: 11px; color: var(--text-faint); }

.conversation-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 11px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background .12s;
}
.conversation-item:hover { background: var(--bg-hover); }
.conversation-item.is-active { background: var(--accent-soft); }
.conv-avatar {
    width: 40px; height: 40px; border-radius: 50%;
    display: grid; place-items: center; font-weight: 700; color: #fff;
    flex: 0 0 40px; text-transform: uppercase;
}
.conv-body { min-width: 0; flex: 1; }
.conv-name { font-size: 14px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-preview { font-size: 12.5px; color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-unread {
    margin-left: auto;
    min-width: 20px; height: 20px; padding: 0 6px;
    border-radius: 10px; background: var(--accent); color: #fff;
    font-size: 11px; font-weight: 700; display: grid; place-items: center;
}

.empty-hint { color: var(--text-faint); font-size: 13px; padding: 14px; line-height: 1.5; }

.empty-state {
    margin: auto;
    text-align: center;
    color: var(--text-faint);
    max-width: 320px;
}
.empty-emoji { font-size: 48px; margin-bottom: 8px; }

/* ---------- Lightbox ---------- */
.lightbox {
    position: fixed; inset: 0;
    background: rgba(0, 0, 0, 0.85);
    display: grid; place-items: center;
    padding: 32px; z-index: 50; cursor: zoom-out;
}
.lightbox img { max-width: 90vw; max-height: 90vh; border-radius: var(--radius-sm); }

/* ---------- Modal (guest login) ---------- */
.modal-overlay {
    position: fixed; inset: 0;
    background: rgba(0, 0, 0, 0.7);
    display: grid; place-items: center;
    padding: 24px; z-index: 60;
}
.modal-overlay .login-card { position: relative; }
.modal-close {
    position: absolute; top: 10px; right: 14px;
    background: none; border: none;
    color: var(--text-dim); font-size: 26px; line-height: 1;
    cursor: pointer;
}
.modal-close:hover { color: var(--text); }

/* ---------- Toast ---------- */
.toast {
    position: fixed;
    bottom: 24px; left: 50%;
    transform: translateX(-50%);
    background: var(--bg-3);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 12px 20px;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow);
    font-size: 14px;
    z-index: 60;
    max-width: 90vw;
}
.toast.error { border-color: rgba(237, 66, 69, .5); color: #ff9ea0; }

/* ---------- Scrollbars ---------- */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #2a3140; border-radius: 6px; }
::-webkit-scrollbar-thumb:hover { background: #36405a; }
::-webkit-scrollbar-track { background: transparent; }

/* ============================================================
   RESPONSIVE
   ============================================================ */
.mobile-only { display: none; }

@media (max-width: 820px) {
    .mobile-only { display: inline-grid !important; }
    .topbar { gap: 10px; padding: 0 12px; }
    .topbar-brand span { display: none; }
    .suggest-btn-label { display: none; }
    .view-private { grid-template-columns: 1fr; }

    /* Public: keep the online-users list always visible in a slim right column. */
    .view-public { grid-template-columns: 1fr 122px; }
    .view-public .sidebar-users { display: flex; }
    .view-public .chat-header .mobile-only { display: none !important; } /* toggle no longer needed */
    .sidebar-users .sidebar-title { padding: 12px 10px 8px; font-size: 11px; }
    .sidebar-users .user-list { padding: 0 5px 10px; }
    .sidebar-users .user-item { padding: 8px 7px; gap: 7px; }
    .sidebar-users .user-name { flex: 1; min-width: 0; font-size: 13px; }

    .view-private .sidebar-conversations { display: flex; }
    .view-private.show-thread .sidebar-conversations { display: none; }
    .view-private .chat-main { display: none; }
    .view-private.show-thread .chat-main { display: flex; }

    .msg-image { max-width: 70vw; }
}
