:root {
    --bg-primary: #f8fafc;
    --bg-secondary: #ffffff;
    --bg-sidebar: #f1f5f9;
    --accent-color: #4f46e5;
    --accent-hover: #4338ca;
    --accent-soft: rgba(79, 70, 229, 0.12);
    --text-main: #0f172a;
    --text-muted: #64748b;
    --border-color: #e2e8f0;
    --mention-bg: #fef08a;
    --msg-bg: #ffffff;
    --msg-hover: #f1f5f9;
    --danger: #ef4444;
    --swatch: #4f46e5;
    --swatch-color: #4f46e5;

    /* Dynamic Local Zoom Setting */
    --chat-font-size: 14px;
}

/* ---------- THEMES ---------- */
[data-theme="dark"] {
    --bg-primary: #0f172a;
    --bg-secondary: #1e293b;
    --bg-sidebar: #1e293b;
    --accent-color: #6366f1;
    --accent-hover: #4f46e5;
    --accent-soft: rgba(99, 102, 241, 0.18);
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --border-color: #334155;
    --mention-bg: #854d0e;
    --msg-bg: #1e293b;
    --msg-hover: #334155;
    --swatch: #6366f1;
}

[data-theme="ocean"] {
    --bg-primary: #f0f9ff;
    --bg-secondary: #ffffff;
    --bg-sidebar: #e0f2fe;
    --accent-color: #0284c7;
    --accent-hover: #0369a1;
    --accent-soft: rgba(2, 132, 199, 0.12);
    --text-main: #0c2b3a;
    --text-muted: #4b7a94;
    --border-color: #bae6fd;
    --mention-bg: #bae6fd;
    --msg-bg: #ffffff;
    --msg-hover: #e0f2fe;
    --swatch: #0284c7;
}

[data-theme="midnight-ocean"] {
    --bg-primary: #041c2c;
    --bg-secondary: #06283f;
    --bg-sidebar: #062338;
    --accent-color: #22d3ee;
    --accent-hover: #06b6d4;
    --accent-soft: rgba(34, 211, 238, 0.16);
    --text-main: #e0f7fa;
    --text-muted: #7fb3c7;
    --border-color: #103c56;
    --mention-bg: #0e4a5c;
    --msg-bg: #06283f;
    --msg-hover: #0a3550;
    --swatch: #22d3ee;
}

[data-theme="forest"] {
    --bg-primary: #f3f8f2;
    --bg-secondary: #ffffff;
    --bg-sidebar: #e6f1e3;
    --accent-color: #15803d;
    --accent-hover: #166534;
    --accent-soft: rgba(21, 128, 61, 0.12);
    --text-main: #14251a;
    --text-muted: #5c7a63;
    --border-color: #cfe6c9;
    --mention-bg: #fde68a;
    --msg-bg: #ffffff;
    --msg-hover: #e6f1e3;
    --swatch: #15803d;
}

[data-theme="sunset"] {
    --bg-primary: #fff7ed;
    --bg-secondary: #ffffff;
    --bg-sidebar: #ffedd5;
    --accent-color: #ea580c;
    --accent-hover: #c2410c;
    --accent-soft: rgba(234, 88, 12, 0.12);
    --text-main: #431407;
    --text-muted: #9a5b3c;
    --border-color: #fed7aa;
    --mention-bg: #fed7aa;
    --msg-bg: #ffffff;
    --msg-hover: #ffedd5;
    --swatch: #ea580c;
}

[data-theme="rose"] {
    --bg-primary: #fff1f4;
    --bg-secondary: #ffffff;
    --bg-sidebar: #ffe4e9;
    --accent-color: #e11d6f;
    --accent-hover: #be1259;
    --accent-soft: rgba(225, 29, 111, 0.12);
    --text-main: #3f0d1f;
    --text-muted: #9c5b71;
    --border-color: #fbcfdc;
    --mention-bg: #fbcfdc;
    --msg-bg: #ffffff;
    --msg-hover: #ffe4e9;
    --swatch: #e11d6f;
}

[data-theme="grape"] {
    --bg-primary: #1a1025;
    --bg-secondary: #241633;
    --bg-sidebar: #201430;
    --accent-color: #a855f7;
    --accent-hover: #9333ea;
    --accent-soft: rgba(168, 85, 247, 0.18);
    --text-main: #f3e8ff;
    --text-muted: #b399cf;
    --border-color: #3b2350;
    --mention-bg: #4c1d75;
    --msg-bg: #241633;
    --msg-hover: #2e1d41;
    --swatch: #a855f7;
}

[data-theme="slate-mono"] {
    --bg-primary: #f5f5f4;
    --bg-secondary: #ffffff;
    --bg-sidebar: #e7e5e4;
    --accent-color: #44403c;
    --accent-hover: #292524;
    --accent-soft: rgba(68, 64, 60, 0.12);
    --text-main: #1c1917;
    --text-muted: #78716c;
    --border-color: #d6d3d1;
    --mention-bg: #e7e5e4;
    --msg-bg: #ffffff;
    --msg-hover: #e7e5e4;
    --swatch: #44403c;
}

* { box-sizing: border-box; margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
body, html { height: 100%; background: var(--bg-primary); color: var(--text-main); overflow: hidden; transition: background 0.2s, color 0.2s; }

.hidden { display: none !important; }
.error-msg { color: var(--danger); margin-top: 8px; font-size: 13px; text-align: center; }

button { font: inherit; }

/* Scrollbars for a slightly more modern feel */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-color); border-radius: 8px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* AUTH OVERLAY */
.overlay {
    position: fixed; inset: 0; background: rgba(15, 23, 42, 0.6); backdrop-filter: blur(8px);
    display: flex; align-items: center; justify-content: center; z-index: 1000; padding: 15px;
}
.auth-card {
    background: var(--bg-secondary); border: 1px solid var(--border-color); padding: 24px; border-radius: 14px;
    width: 100%; max-width: 360px; box-shadow: 0 20px 45px -10px rgba(0, 0, 0, 0.35);
}
.auth-card h2 { font-size: 18px; font-weight: 600; text-align: center; margin-bottom: 16px; }
.tabs { display: flex; background: var(--bg-sidebar); padding: 3px; border-radius: 8px; margin-bottom: 16px; }
.tabs button { flex: 1; padding: 7px; border: none; background: none; color: var(--text-muted); font-size: 12px; font-weight: 500; border-radius: 6px; cursor: pointer; transition: background 0.15s, color 0.15s; }
.tabs button.active { background: var(--bg-secondary); color: var(--text-main); font-weight: 600; box-shadow: 0 1px 3px rgba(0,0,0,0.1); }

.input-group { margin-bottom: 12px; }
.input-group label { display: block; font-size: 11px; color: var(--text-muted); margin-bottom: 4px; }
.input-group input {
    width: 100%; padding: 9px 11px; background: var(--bg-primary); color: var(--text-main);
    border: 1px solid var(--border-color); border-radius: 8px; font-size: 13px; outline: none;
    transition: border-color 0.15s, box-shadow 0.15s;
}
.input-group input:focus { border-color: var(--accent-color); box-shadow: 0 0 0 3px var(--accent-soft); }
.checkbox-group { display: flex; align-items: center; gap: 6px; margin-bottom: 16px; font-size: 12px; color: var(--text-muted); }
.btn-primary {
    width: 100%; padding: 9px; background: var(--accent-color); color: white; border: none;
    border-radius: 8px; font-size: 13px; font-weight: 600; cursor: pointer; transition: background 0.15s, transform 0.05s;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:active { transform: scale(0.98); }

/* MAIN LAYOUT */
.app-container { display: flex; height: 100vh; width: 100vw; position: relative; }
.chat-main { flex: 1; display: flex; flex-direction: column; background: var(--bg-primary); position: relative; min-width: 0; }

/* RIGHT SIDEBAR ALIGNED WITH MAIN CHAT HEADER */
.sidebar {
    width: 240px; background: var(--bg-sidebar); border-left: 1px solid var(--border-color);
    display: flex; flex-direction: column; z-index: 200; transition: transform 0.3s ease;
}
.sidebar-header {
    height: 44px;
    padding: 0 14px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.sidebar h3 { font-size: 11px; font-weight: 700; text-transform: uppercase; color: var(--text-muted); letter-spacing: 0.04em; }
.online-count {
    font-size: 10px; font-weight: 700; color: var(--accent-color); background: var(--accent-soft);
    padding: 2px 7px; border-radius: 999px;
}
.sidebar ul { list-style: none; padding: 6px; overflow-y: auto; flex: 1; }
.sidebar li {
    padding: 7px 8px; border-radius: 8px; font-size: 13px; margin-bottom: 2px;
    display: flex; align-items: center; justify-content: space-between; gap: 6px;
    transition: background 0.12s;
}
.sidebar li:hover { background: var(--msg-hover); }
.sidebar li.active-user { font-weight: 600; color: var(--accent-color); }
.sidebar-empty {
    padding: 24px 12px; text-align: center; color: var(--text-muted); font-size: 12px; line-height: 1.5;
}

.user-info-group { display: flex; align-items: center; gap: 8px; cursor: pointer; flex: 1; min-width: 0; }
.user-avatar {
    width: 26px; height: 26px; border-radius: 50%; background: var(--accent-soft); display: flex;
    align-items: center; justify-content: center; font-size: 11px; font-weight: 700; color: var(--accent-color);
    flex-shrink: 0;
}
.user-info-group span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ignore-btn {
    background: none; border: 1px solid transparent; color: var(--text-muted); cursor: pointer;
    font-size: 11px; padding: 4px 8px; border-radius: 999px; transition: background 0.15s, color 0.15s; flex-shrink: 0;
}
.ignore-btn:hover { background: rgba(239, 68, 68, 0.12); color: var(--danger); }
.ignore-btn.ignored { background: rgba(239, 68, 68, 0.18); color: var(--danger); font-weight: 600; }

.sidebar-footer { padding: 10px; border-top: 1px solid var(--border-color); }
.btn-logout {
    width: 100%; padding: 8px; background: var(--danger); color: #fff; border: none; border-radius: 8px;
    font-size: 12px; font-weight: 600; cursor: pointer; transition: filter 0.15s;
}
.btn-logout:hover { filter: brightness(0.92); }

/* COMPACT ALIGNED HEADER CONTROL BAR */
.chat-header {
    height: 44px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    padding: 0 8px;
    gap: 6px;
    position: relative;
}
.mobile-menu-btn {
    display: none; background: none; border: none; font-size: 18px; color: var(--text-main);
    cursor: pointer; padding: 4px 6px; border-radius: 8px; flex-shrink: 0;
}
.mobile-menu-btn:hover { background: var(--msg-hover); }
.tab-bar-wrapper { display: flex; align-items: center; flex: 1; overflow: hidden; height: 100%; min-width: 0; }
.nav-arrow {
    background: none; border: none; color: var(--text-muted); padding: 4px 6px; cursor: pointer;
    font-size: 12px; border-radius: 6px; flex-shrink: 0; transition: background 0.15s, color 0.15s;
}
.nav-arrow:hover { background: var(--msg-hover); color: var(--text-main); }
.tab-scroll-container { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; align-items: center; scroll-behavior: smooth; }
.tab-scroll-container::-webkit-scrollbar { display: none; }
.tab-item {
    position: relative;
    padding: 4px 10px;
    background: var(--bg-sidebar);
    border-radius: 999px;
    cursor: pointer;
    white-space: nowrap;
    font-size: 12px;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: 6px;
    height: 26px;
    transition: background 0.15s, color 0.15s;
}
.tab-item:hover { background: var(--msg-hover); }
.tab-item.active { background: var(--accent-color); color: #fff; font-weight: 600; }
.close-tab-btn { font-size: 12px; opacity: 0.7; line-height: 1; }
.close-tab-btn:hover { opacity: 1; }

.tab-unread-badge {
    background: var(--danger);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    min-width: 16px;
    height: 16px;
    border-radius: 999px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
    line-height: 1;
}
.tab-item.active .tab-unread-badge { background: rgba(255,255,255,0.35); }

.header-controls { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }

/* Custom fancy dropdown (used for font/size/color/theme) replacing native <select> */
.fancy-dropdown { position: relative; width: 100%; }
.fancy-dropdown-trigger {
    width: 100%; display: flex; align-items: center; justify-content: space-between; gap: 8px;
    background: var(--bg-sidebar); border: 1px solid var(--border-color); color: var(--text-main);
    padding: 7px 10px; border-radius: 8px; font-size: 12px; cursor: pointer;
    transition: border-color 0.15s, box-shadow 0.15s;
}
.fancy-dropdown-trigger:hover { border-color: var(--accent-color); }
.fancy-dropdown.is-open .fancy-dropdown-trigger { border-color: var(--accent-color); box-shadow: 0 0 0 3px var(--accent-soft); }
.fancy-dropdown-caret { font-size: 10px; color: var(--text-muted); transition: transform 0.15s; }
.fancy-dropdown.is-open .fancy-dropdown-caret { transform: rotate(180deg); }

@keyframes pop-in-up {
    from { opacity: 0; transform: translateY(6px) scale(0.98); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

/* Menus open UPWARD by default: the toolbar sits just above the input box near
   the bottom of the screen, so a downward-opening menu was getting clipped. */
.fancy-dropdown-menu {
    position: absolute; bottom: calc(100% + 6px); left: 0; right: 0; max-height: 220px; overflow-y: auto;
    background: var(--bg-secondary); border: 1px solid var(--border-color); border-radius: 10px;
    box-shadow: 0 -14px 30px -8px rgba(0,0,0,0.35); z-index: 450; padding: 4px;
    animation: pop-in-up 0.14s ease;
}
.fancy-dropdown-item {
    padding: 7px 10px; font-size: 12px; border-radius: 6px; cursor: pointer; color: var(--text-main);
    display: flex; align-items: center; justify-content: space-between; transition: background 0.12s;
}
.fancy-dropdown-item:hover { background: var(--msg-hover); }
.fancy-dropdown-item.selected { background: var(--accent-soft); color: var(--accent-color); font-weight: 600; }

/* Theme swatch grid */
.theme-swatch-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.theme-swatch {
    width: 100%; aspect-ratio: 1; border-radius: 10px; cursor: pointer; position: relative;
    border: 2px solid transparent; transition: transform 0.12s, border-color 0.12s;
    background: var(--swatch-color, var(--accent-color));
    display: flex; align-items: flex-end; justify-content: flex-end;
}
.theme-swatch:hover { transform: translateY(-2px); }
.theme-swatch.selected { border-color: var(--text-main); }
.theme-swatch.selected::after {
    content: '✓'; color: #fff; font-size: 11px; font-weight: 800; background: rgba(0,0,0,0.35);
    border-radius: 50%; width: 16px; height: 16px; display: flex; align-items: center; justify-content: center;
    margin: 3px;
}

/* CHAT MESSAGES VIEW WITH ZOOM VARIABLE */
.chat-views { flex: 1; position: relative; overflow: hidden; }
.chat-pane { position: absolute; inset: 0; overflow-y: auto; padding: 10px 14px; display: none; flex-direction: column; gap: 2px; background: var(--bg-primary); }
.chat-pane.active { display: flex; }

/* BORDERLESS MSG FORMAT WITH DYNAMIC FONT SIZE */
.msg-line {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    padding: 5px 8px;
    border-radius: 6px;
    font-size: var(--chat-font-size);
    background: transparent;
    border: none;
    word-break: break-word;
    transition: background 0.1s;
}
.msg-line:hover { background: var(--msg-hover); }
.msg-content-wrap { display: flex; gap: 6px; flex: 1; padding-right: 12px; min-width: 0; }
.msg-sender { font-weight: 700; color: var(--accent-color); white-space: nowrap; }
.msg-body { color: var(--text-main); flex: 1; overflow-wrap: anywhere; }
.msg-timestamp { font-size: calc(var(--chat-font-size) - 3px); color: var(--text-muted); white-space: nowrap; margin-left: auto; }
.msg-line.mention-highlight { background: var(--mention-bg) !important; border-radius: 6px; }

/* Typing indicator */
.typing-indicator {
    font-size: 11px; color: var(--text-muted); padding: 2px 16px 4px; min-height: 16px;
    font-style: italic;
}

/* INPUT & TOOLBAR LAYOUT */
.editor-container { padding: 8px 12px; background: var(--bg-secondary); border-top: 1px solid var(--border-color); position: relative; }
.toolbar { display: flex; align-items: center; gap: 4px; margin-bottom: 8px; flex-wrap: wrap; }
.tool-btn {
    background: var(--bg-sidebar); border: 1px solid var(--border-color); color: var(--text-main);
    min-width: 28px; height: 28px; padding: 0 8px; border-radius: 8px; cursor: pointer; font-size: 12px;
    display: flex; align-items: center; justify-content: center; transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.tool-btn:hover { background: var(--msg-hover); border-color: var(--accent-color); }
.tool-btn:active { transform: scale(0.94); }
.tool-btn.active {
    background: var(--accent-soft); border-color: var(--accent-color); color: var(--accent-color);
}
.tool-divider { width: 1px; height: 18px; background: var(--border-color); margin: 0 4px; }

/* Toolbar dropdowns (font family / size / color) sit inline with Bold/Italic */
.toolbar-dropdown { width: auto; min-width: 92px; }
.toolbar-dropdown-sm { min-width: 64px; }
.toolbar-trigger { padding: 5px 8px; height: 28px; border-radius: 8px; font-size: 12px; }
.toolbar-dropdown .fancy-dropdown-menu { min-width: 160px; width: max-content; left: 0; right: auto; bottom: calc(100% + 6px); }

.toolbar-color-dot {
    width: 15px; height: 15px; border-radius: 50%; display: inline-block;
    background: var(--text-main); border: 1px solid var(--border-color);
}
.color-menu { padding: 8px; min-width: 168px; }
.color-swatch-option-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; }
.color-swatch-option {
    width: 22px; height: 22px; border-radius: 50%; cursor: pointer; border: 2px solid transparent;
    transition: transform 0.12s, border-color 0.12s;
}
.color-swatch-option:hover { transform: scale(1.12); }
.color-swatch-option.selected { border-color: var(--text-main); }

.input-row-wrapper { display: flex; align-items: center; gap: 8px; }

/* TOOLBAR TOGGLE (+) BUTTON */
.toggle-toolbar-btn {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    border: 1px solid var(--border-color);
    background: var(--bg-sidebar);
    color: var(--text-main);
    font-size: 17px;
    font-weight: bold;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s, border-color 0.15s, transform 0.15s;
}
.toggle-toolbar-btn:hover { background: var(--msg-hover); border-color: var(--accent-color); }
.toggle-toolbar-btn.is-active { transform: rotate(45deg); background: var(--accent-soft); border-color: var(--accent-color); color: var(--accent-color); }

/* EXPANDED INPUT BOX WITH EMOJI BUTTON ON LEFT */
.input-box-wrapper {
    position: relative;
    display: flex;
    align-items: center;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 20px;
    padding: 4px 10px;
    flex: 1;
    min-width: 0;
    transition: border-color 0.15s, box-shadow 0.15s;
}
.input-box-wrapper:focus-within { border-color: var(--accent-color); box-shadow: 0 0 0 3px var(--accent-soft); }

.inline-emoji-btn {
    background: none;
    border: none;
    font-size: 16px;
    cursor: pointer;
    padding: 2px 6px;
    margin-right: 4px;
    border-radius: 50%;
    transition: background 0.15s;
}
.inline-emoji-btn:hover { background: var(--msg-hover); }

#rich-editor {
    flex: 1;
    min-height: 36px;
    max-height: 100px;
    overflow-y: auto;
    outline: none;
    font-size: var(--chat-font-size);
    padding: 6px 30px 6px 2px;
    color: var(--text-main);
    line-height: 1.4;
}
#rich-editor[placeholder]:empty:before { content: attr(placeholder); color: var(--text-muted); }

.inline-send-btn {
    position: absolute;
    right: 6px;
    width: 30px;
    height: 30px;
    background: var(--accent-color);
    border: none;
    border-radius: 50%;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 13px;
    transition: background 0.15s, transform 0.05s;
}
.inline-send-btn:hover { background: var(--accent-hover); }
.inline-send-btn:active { transform: scale(0.92); }

/* AUTOCOMPLETE DROPDOWN (anchored via JS to caret/button position) */
.mention-dropdown {
    position: absolute; background: var(--bg-secondary); border: 1px solid var(--border-color);
    border-radius: 10px; box-shadow: 0 10px 26px -6px rgba(0,0,0,0.3); max-height: 140px; overflow-y: auto; width: 190px; z-index: 300;
}
.mention-item { padding: 7px 10px; font-size: 12px; cursor: pointer; color: var(--text-main); border-radius: 6px; margin: 2px; }
.mention-item:hover, .mention-item.selected { background: var(--accent-color); color: #fff; }

.emoji-popover { position: absolute; z-index: 500; }

/* MOBILE RESPONSIVE */
.sidebar-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 150; }
@media (max-width: 768px) {
    .sidebar { position: fixed; top: 0; bottom: 0; right: 0; transform: translateX(100%); width: 78vw; max-width: 300px; box-shadow: -10px 0 30px rgba(0,0,0,0.25); }
    .sidebar.mobile-open { transform: translateX(0); }
    .sidebar-overlay.mobile-open { display: block; }
    .mobile-menu-btn { display: block; }
    .nav-arrow { display: none; }
    .tab-scroll-container { -webkit-overflow-scrolling: touch; }
    .toolbar { justify-content: flex-start; }
    .toolbar-dropdown .fancy-dropdown-menu { max-width: 60vw; }
}
