/* ... Preserved CSS ... */
:root {
    --primary-color: #d50000;
    --primary-gradient: linear-gradient(135deg, #d50000 0%, #ff0000 100%);
    --bg-dark: #0a0a0a;
    --text-color: #ffffff;
    --text-secondary: #aaaaaa;
    --border-color: #333;
    --input-bg: #1f1f1f;
    --transition: all 0.3s ease;
    --vv-top-offset: 0px;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Poppins', sans-serif;
    background-color: var(--bg-dark);
    color: var(--text-color);
    line-height: 1.6;
    height: 100dvh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    touch-action: pan-x pan-y;
    -webkit-text-size-adjust: 100%;
}

body::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: radial-gradient(circle at 10% 20%, rgba(213, 0, 0, 0.1) 0%, transparent 20%), radial-gradient(circle at 90% 80%, rgba(200, 0, 0, 0.1) 0%, transparent 20%);
    z-index: -1;
    pointer-events: none;
}

::-webkit-scrollbar {
    width: 6px;
}

::-webkit-scrollbar-track {
    background: var(--bg-dark);
}

::-webkit-scrollbar-thumb {
    background: #444;
    border-radius: 10px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--primary-color);
}

.login-page {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    padding: 20px;
}

.login-card {
    background: rgba(20, 20, 20, 0.95);
    border: 1px solid var(--border-color);
    padding: 40px;
    border-radius: 20px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    width: 100%;
    max-width: 400px;
    text-align: center;
    backdrop-filter: blur(10px);
    transition: var(--transition);
}

.login-card:hover,
.login-card:focus-within {
    border-color: #d50000;
    box-shadow: 0 0 20px rgba(213, 0, 0, 0.4);
}

.logo-wrapper {
    margin-bottom: 20px;
}

.app-logo-large {
    width: 360px;
    max-width: 100%;
    height: auto;
    display: block;
    margin: 0 auto;
}

.form-group {
    margin-bottom: 25px;
    text-align: left;
}

.input-icon-wrapper {
    position: relative;
    display: flex;
    align-items: center;
}

.input-icon,
.toggle-password {
    position: absolute;
    color: #888;
    font-size: 18px;
    z-index: 3;
    transition: color 0.3s;
}

.input-icon-wrapper:focus-within .input-icon,
.input-icon-wrapper:focus-within .toggle-password {
    color: #d50000;
}

.input-icon {
    left: 15px;
    pointer-events: none;
}

.toggle-password {
    right: 15px;
    cursor: pointer;
    padding: 5px;
}

.toggle-password:hover {
    color: #fff;
}

.form-group input {
    width: 100%;
    padding: 14px 15px 14px 45px;
    background: var(--input-bg);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    color: var(--text-color);
    font-size: 16px;
    transition: var(--transition);
}

#password {
    padding-right: 45px;
}

.form-group input:focus {
    outline: none;
    border-color: #d50000;
    box-shadow: 0 0 0 3px rgba(213, 0, 0, 0.2);
}

.btn-login {
    width: 100%;
    padding: 15px;
    background: linear-gradient(135deg, #d50000 0%, #b71c1c 100%);
    border: none;
    border-radius: 10px;
    color: #fff;
    font-size: 18px;
    font-weight: 700;
    cursor: pointer;
    box-shadow: 0 4px 15px rgba(213, 0, 0, 0.3);
    text-transform: uppercase;
    letter-spacing: 1px;
    transition: var(--transition);
}

.btn-login:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(213, 0, 0, 0.5);
}

.error-msg {
    color: #ff5252;
    margin-top: 15px;
    font-size: 14px;
}

.shake {
    animation: shake 0.5s cubic-bezier(.36, .07, .19, .97) both;
}

@keyframes shake {

    10%,
    90% {
        transform: translate3d(-1px, 0, 0);
    }

    20%,
    80% {
        transform: translate3d(2px, 0, 0);
    }
}

.top-ui-stack {
    position: sticky;
    top: 0;
    z-index: 1000;
    background: #141414;
    transform: translate3d(0, var(--vv-top-offset), 0);
    will-change: transform;
    transition: transform 0.08s linear;
    backface-visibility: hidden;
}

.chat-header {
    background: #141414;
    border-bottom: 1px solid var(--border-color);
    padding: 10px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex: 0 0 60px;
    position: relative;
    z-index: 1;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 0;
}

.header-user-block {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    line-height: 1;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.chat-logo-header {
    width: 44px;
    height: auto;
    object-fit: contain;
}

.chat-title-text {
    font-size: 20px;
    font-weight: 700;
    margin: 0;
    color: #fff;
    letter-spacing: 0.5px;
}

.chat-sub-status {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    font-weight: 600;
    margin-top: 0;
    padding: 7px 12px;
    border-radius: 999px;
    border: 1px solid #363636;
    background: #1d1d1d;
    letter-spacing: 0.2px;
}

.chat-sub-status::before {
    content: '';
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #8b8b8b;
    flex: 0 0 8px;
}

.chat-sub-status.online {
    color: #5ce27a;
}

.chat-sub-status.online::before {
    background: #35d66b;
    animation: statusPulse 1.2s infinite;
}

.chat-sub-status.typing {
    color: #ff9f43;
}

.chat-sub-status.typing::before {
    background: #ff9f43;
    animation: none;
}

.chat-sub-status.offline {
    color: #a3a3a3;
}

.chat-sub-status.offline::before {
    background: #8b8b8b;
    animation: none;
}

@keyframes statusPulse {
    0% {
        box-shadow: 0 0 0 0 rgba(53, 214, 107, 0.7);
    }

    70% {
        box-shadow: 0 0 0 7px rgba(53, 214, 107, 0);
    }

    100% {
        box-shadow: 0 0 0 0 rgba(53, 214, 107, 0);
    }
}

.btn-icon {
    background: transparent;
    border: 1px solid var(--border-color);
    color: #d50000;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    transition: var(--transition);
}

.btn-icon:hover {
    background: rgba(213, 0, 0, 0.1);
}

#searchBtn {
    color: #ffffff;
    border-color: #505050;
}

#searchBtn:hover {
    color: #ffffff;
    border-color: #ffffff;
    background: rgba(255, 255, 255, 0.12);
}

.search-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    background: #121212 !important;
    background-color: #121212 !important;
    background-image: none !important;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
    mix-blend-mode: normal !important;
    opacity: 1 !important;
    width: 100%;
    min-height: 52px;
    border-bottom: 1px solid var(--border-color);
    position: relative;
    z-index: 1;
    transition: background-color 0.08s linear;
}

body.keyboard-open .top-ui-stack {
    background: #141414 !important;
    transition: none !important;
}

body.keyboard-open .search-bar {
    background: #121212 !important;
    background-color: #121212 !important;
    background-image: none !important;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
    opacity: 1 !important;
    transition: none !important;
}

.search-bar:focus-within {
    background: #121212 !important;
    background-color: #121212 !important;
    background-image: none !important;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
    opacity: 1 !important;
}

.search-input {
    flex: 1;
    background: #1e1e1e !important;
    background-color: #1e1e1e !important;
    border: 1px solid #3a3a3a;
    color: #fff;
    border-radius: 10px;
    padding: 9px 12px;
    font-size: 14px;
    text-align: start;
    unicode-bidi: plaintext;
    opacity: 1 !important;
}

.search-input:active,
.search-input:focus,
.search-input:hover {
    background: #1e1e1e !important;
    background-color: #1e1e1e !important;
    opacity: 1 !important;
}

.search-input:focus {
    outline: none;
    border-color: #ffffff;
    box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.15);
}

.search-close-btn {
    width: 34px;
    height: 34px;
    color: #cfcfcf;
}

.search-nav {
    display: inline-flex;
    align-items: center;
    gap: 5px;
}

.search-nav-btn {
    width: 30px;
    height: 30px;
    color: #efefef;
    border-color: #4a4a4a;
    font-size: 13px;
}

.search-nav-btn:hover {
    background: rgba(255, 255, 255, 0.12);
    border-color: #ffffff;
    color: #ffffff;
}

.search-nav-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.search-count {
    min-width: 48px;
    text-align: center;
    color: #ffffff;
    font-size: 12px;
    font-weight: 600;
}

.chat-container {
    flex: 1;
    overflow-y: auto;
    padding: 20px 20px 10px 20px;
    margin: 0;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    gap: 0;
    background-image: radial-gradient(#1f1f1f 1px, transparent 1px);
    background-size: 20px 20px;
    position: relative;
    scroll-behavior: smooth;
}

.date-separator {
    align-self: center;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin: 10px 0 8px;
    padding: 4px 12px;
    border-radius: 999px;
    background: #1f1f1f;
    border: 1px solid #353535;
    color: #d8d8d8;
    font-size: 11px;
    font-weight: 600;
    line-height: 1.2;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
}

.input-area-wrapper {
    position: relative;
    background: #0f0f0f;
    border-top: 1px solid var(--border-color);
    padding: 15px 15px 20px 15px;
    z-index: 50;
    flex: 0 0 auto;
    transition: height 0.3s ease;
}

.message-wrapper {
    width: 100%;
    display: flex;
    margin-bottom: 6px;
    position: relative;
    touch-action: pan-y;
    transition: transform 0.1s;
}

.message-wrapper.snap-back {
    transition: transform 0.3s cubic-bezier(0.25, 1, 0.5, 1);
}

.message-wrapper.sent {
    justify-content: flex-end;
}

.message-wrapper.received {
    justify-content: flex-start;
}

.message {
    display: flex;
    flex-direction: column;
    width: max-content;
    max-width: min(82vw, 390px);
    /* UPDATED: Significantly reduced padding and line-height for a much tighter bubble */
    padding: 6px 10px;
    border-radius: 12px;
    position: relative;
    font-size: 14px;
    line-height: 1.35;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    word-break: normal;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    transition: background-color 0.18s ease, box-shadow 0.18s ease, transform 0.18s ease;
    white-space: pre-wrap;
}

.message.new-message {
    animation: slideIn 0.3s ease;
}

.message.search-match {
    border-color: rgba(255, 255, 255, 0.9) !important;
    box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.9), 0 0 10px rgba(255, 255, 255, 0.25);
}

.message.search-active {
    border-color: #ffffff !important;
    box-shadow: 0 0 0 2px #ffffff, 0 0 14px rgba(255, 255, 255, 0.35);
    transform: translateY(-1px);
}

.message.selected {
    border-color: #ffffff !important;
    box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.95);
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message.sent {
    background: rgba(213, 0, 0, 0.55);
    backdrop-filter: blur(5px);
    color: #fff;
    border-bottom-right-radius: 2px;
    border: 1px solid rgba(255, 50, 50, 0.2);
}

.message.received {
    background: #2a2a2a;
    color: #eee;
    border-bottom-left-radius: 2px;
    border: 1px solid #333;
}

.message.deleted {
    background: transparent !important;
    border: 1px dashed #555 !important;
    color: #777 !important;
    font-style: italic;
    cursor: default;
    pointer-events: none;
}

.message-content {
    display: block;
    width: 100%;
    max-width: 100%;
    white-space: pre-wrap;
    word-break: normal;
    overflow-wrap: anywhere;
}

/* UPDATED: Reduced margin-top for header to keep it close */
.message-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    gap: 8px;
    font-size: 10px;
    margin-top: 2px;
    opacity: 0.8;
}

.message-meta-left {
    display: flex;
    align-items: center;
    gap: 5px;
}

.message-time {
    font-weight: 300;
}

.message-status {
    font-size: 11px;
    font-weight: 600;
    letter-spacing: -1px;
    min-width: 14px;
    text-align: center;
    margin-left: auto;
}

.message-status.one-check {
    color: rgba(255, 255, 255, 0.85);
}

.message-status.double-check {
    color: #9ad9ff;
}

@media (min-width: 992px) {
    .message {
        max-width: min(62vw, 420px);
    }
}

@keyframes highlightPulseRed {
    0% {
        transform: scale(1);
        box-shadow: 0 0 0 0 rgba(213, 0, 0, 0.7);
    }

    50% {
        transform: scale(1.02);
        box-shadow: 0 0 20px 0 rgba(213, 0, 0, 0.4);
    }

    100% {
        transform: scale(1);
        box-shadow: 0 0 0 0 rgba(213, 0, 0, 0);
    }
}

@keyframes highlightPulseWhite {
    0% {
        transform: scale(1);
        box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.7);
    }

    50% {
        transform: scale(1.02);
        box-shadow: 0 0 20px 0 rgba(255, 255, 255, 0.4);
    }

    100% {
        transform: scale(1);
        box-shadow: 0 0 0 0 rgba(255, 255, 255, 0);
    }
}

.message.highlight-red {
    animation: highlightPulseRed 1s ease-out;
    border-color: #d50000;
}

.message.highlight-white {
    animation: highlightPulseWhite 1s ease-out;
    border-color: #ffffff;
}

.reply-preview {
    display: inline-flex;
    flex-direction: column;
    align-items: flex-start;
    align-self: flex-start;
    width: fit-content;
    max-width: 100%;
    background: rgba(0, 0, 0, 0.2);
    border-left: 3px solid rgba(255, 255, 255, 0.5);
    padding: 4px 7px;
    margin-bottom: 3px;
    font-size: 12px;
    line-height: 1.25;
    border-radius: 4px;
    color: rgba(255, 255, 255, 0.9);
    cursor: pointer;
    transition: background 0.2s;
    white-space: normal;
    word-break: break-word;
    overflow-wrap: anywhere;
}

.reply-preview strong {
    display: block;
    margin: 0 0 2px 0;
    line-height: 1.2;
}

.reply-preview-text {
    display: block;
    max-width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    unicode-bidi: plaintext;
}

.reply-preview:hover {
    background: rgba(255, 255, 255, 0.1);
}

.reply-preview:active {
    transform: scale(0.98);
}

.scroll-bottom-btn {
    position: absolute;
    top: -55px;
    right: 20px;
    width: 40px;
    height: 40px;
    background: rgba(30, 30, 30, 0.95);
    border: 1px solid #555;
    border-radius: 50%;
    color: #fff;
    font-size: 18px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
    z-index: 90;
    transition: all 0.2s ease;
    opacity: 0;
    pointer-events: none;
    transform: translateY(10px);
}

.scroll-bottom-btn.visible {
    opacity: 1;
    pointer-events: auto;
    transform: translateY(0);
}

.scroll-bottom-btn:hover {
    background: #444;
    border-color: #d50000;
    color: #d50000;
}

.unread-badge {
    position: absolute;
    top: -5px;
    right: -5px;
    background: #d50000;
    color: #fff;
    font-size: 10px;
    font-weight: bold;
    min-width: 20px;
    height: 20px;
    border-radius: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 2px;
    border: 2px solid #1a1a1a;
}

.reply-bar {
    background: #1a1a1a;
    border-left: 3px solid #d50000;
    padding: 10px;
    margin-bottom: 10px;
    border-radius: 4px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.reply-content {
    flex: 1;
    overflow: hidden;
}

.reply-label {
    font-size: 11px;
    color: #d50000;
    font-weight: 700;
    display: block;
    margin-bottom: 2px;
}

#replyTextPreview {
    font-size: 13px;
    color: #ddd;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin: 0;
}

.reply-cancel-btn {
    background: transparent;
    border: none;
    color: #aaa;
    cursor: pointer;
    padding: 5px;
    font-size: 14px;
}

.message-form {
    width: 100%;
}

.input-row {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    width: 100%;
}

.input-container {
    flex: 1;
    background: var(--input-bg);
    border-radius: 25px;
    padding: 5px 15px;
    border: 1px solid #333;
    transition: var(--transition);
}

.input-container:focus-within {
    border-color: #d50000;
}

.message-input {
    width: 100%;
    background: transparent;
    border: none;
    color: #fff;
    font-size: 15px;
    padding: 8px 0;
    resize: none;
    max-height: 120px;
    font-family: inherit;
    line-height: 1.4;
    display: block;
    white-space: normal;
}

.message-input:focus {
    outline: none;
}

.btn-send {
    flex-shrink: 0;
    background: transparent;
    border: 2px solid #d50000;
    width: 48px;
    height: 48px;
    border-radius: 12px;
    color: #d50000;
    font-size: 22px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    transition: var(--transition);
    margin-bottom: 3px;
    box-shadow: none;
    pointer-events: auto !important;
}

.btn-send:hover {
    background: transparent;
    color: #ff5252;
    border-color: #ff5252;
    box-shadow: 0 0 10px rgba(213, 0, 0, 0.4);
}

.message-modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
    animation: fadeIn 0.1s ease-out;
    backdrop-filter: blur(5px);
}

@keyframes popUp {
    0% {
        opacity: 0;
        transform: scale(0.9) translateY(20px);
    }

    100% {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

.message-modal-content,
.delete-modal-content {
    background: #1a1a1a;
    border: 1px solid #333;
    width: 85%;
    max-width: 300px;
    border-radius: 15px;
    overflow: hidden;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.8);
    animation: popUp 0.15s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.delete-modal-box {
    display: flex;
    justify-content: center;
    width: 100%;
}

.modal-header {
    background: #151515;
    padding: 15px 20px;
    border-bottom: 1px solid #333;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-header h3 {
    margin: 0;
    font-size: 16px;
    color: #fff;
}

.modal-close {
    background: transparent;
    border: none;
    color: #888;
    cursor: pointer;
    font-size: 18px;
    padding: 5px;
}

.modal-options {
    list-style: none;
    padding: 10px 0;
    margin: 0;
}

.modal-options li {
    padding: 15px 25px;
    cursor: pointer;
    color: #eee;
    font-size: 16px;
    display: flex;
    align-items: center;
    gap: 15px;
    transition: background 0.1s;
}

.modal-options li:hover {
    background: #252525;
}

.option-icon {
    width: 30px;
    height: 30px;
    background: #222;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #ddd;
}

.option-icon.delete-icon {
    background: rgba(213, 0, 0, 0.1);
    color: #d50000;
}

.delete-modal-content {
    padding: 20px;
    text-align: center;
}

.delete-modal-content h3 {
    margin-top: 0;
    margin-bottom: 20px;
    font-size: 18px;
}

.delete-options {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.delete-options button {
    padding: 12px;
    border: none;
    border-radius: 8px;
    font-size: 15px;
    cursor: pointer;
    font-weight: 600;
}

.btn-delete-all {
    background: #d32f2f;
    color: #fff;
}

.btn-delete-me {
    background: #333;
    color: #fff;
}

.btn-cancel {
    background: transparent;
    border: 1px solid #444;
    color: #aaa;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

.selection-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    background: #1a1a1a;
    border: 1px solid #3a3a3a;
    border-radius: 10px;
    padding: 8px 10px;
    margin-bottom: 10px;
}

.selection-count {
    font-size: 13px;
    color: #fff;
    font-weight: 600;
}

.selection-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.selection-actions .btn-delete-all,
.selection-actions .btn-cancel {
    padding: 8px 10px;
    font-size: 13px;
}

.selection-actions button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}
