@font-face {
    font-family: 'Inter';
    font-style: normal;
    font-weight: 400;
    font-display: swap;
    src: url('fonts/Inter-Regular.ttf') format('truetype');
}

@font-face {
    font-family: 'Inter';
    font-style: normal;
    font-weight: 500;
    font-display: swap;
    src: url('fonts/Inter-Medium.ttf') format('truetype');
}

@font-face {
    font-family: 'Inter';
    font-style: normal;
    font-weight: 600;
    font-display: swap;
    src: url('fonts/Inter-SemiBold.ttf') format('truetype');
}

@font-face {
    font-family: 'Vazirmatn';
    font-style: normal;
    font-weight: 400;
    font-display: swap;
    src: url('fonts/Vazirmatn-Regular.ttf') format('truetype');
}

@font-face {
    font-family: 'Vazirmatn';
    font-style: normal;
    font-weight: 500;
    font-display: swap;
    src: url('fonts/Vazirmatn-Medium.ttf') format('truetype');
}

@font-face {
    font-family: 'Vazirmatn';
    font-style: normal;
    font-weight: 600;
    font-display: swap;
    src: url('fonts/Vazirmatn-SemiBold.ttf') format('truetype');
}

:root {
    --primary-color: #3390ec;
    --bg-color: #ffffff;
    --secondary-bg: #f1f1f1;
    --chat-bg: #8e8e93;
    /* detailed background pattern usually used in telegram, using solid color for simplicity or a nice gradient */
    --my-msg-bg: #eeffde;
    --other-msg-bg: #ffffff;
    --text-color: #000000;
    --meta-color: #888888;
    --border-color: #dfe1e5;
    --danger-color: #ff3b30;
}

.dark-mode {
    --primary-color: #5288c1;
    --bg-color: #17212b;
    --secondary-bg: #0e1621;
    --chat-bg: #0e1621;
    --my-msg-bg: #2b5278;
    --other-msg-bg: #182533;
    --text-color: #f5f5f5;
    --meta-color: #6c7883;
    --border-color: #2b3846;
    /* darker dividers */
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--secondary-bg);
    color: var(--text-color);
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}



/* Auth Screens */
.auth-container {
    background: var(--bg-color);
    padding: 2rem;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    width: 300px;
    text-align: center;
}

.auth-container h2 {
    margin-top: 0;
    color: var(--primary-color);
}

.auth-container input {
    width: 100%;
    padding: 10px;
    margin: 10px 0;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-sizing: border-box;
    font-size: 16px;
}

.btn {
    width: 100%;
    padding: 12px;
    background-color: var(--primary-color);
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 16px;
    font-weight: 500;
    transition: background-color 0.2s;
}

.btn:hover {
    background-color: #2885d8;
}

.link {
    display: block;
    margin-top: 10px;
    color: var(--primary-color);
    cursor: pointer;
    font-size: 14px;
}

/* Main App Layout */
#app-view {
    display: flex;
    width: 100%;
    height: 100%;
    max-width: 1200px;
    background: var(--bg-color);
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.05);
}

/* Sidebar */
.sidebar {
    width: 350px;
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    background: var(--bg-color);
}

.sidebar-header {
    padding: 15px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.contact-list {
    flex: 1;
    overflow-y: auto;
}

.contact-item {
    padding: 12px 15px;
    display: flex;
    align-items: center;
    cursor: pointer;
    transition: background-color 0.1s;
}

.contact-item:hover,
.contact-item.active {
    background-color: var(--secondary-bg);
}

.avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: linear-gradient(135deg, #66d, #49b);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    font-weight: bold;
    margin-right: 12px;
}

.contact-info {
    flex: 1;
}

.contact-name {
    font-weight: 600;
    margin-bottom: 4px;
}

/* Chat Area */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background-image: url("data:image/svg+xml,%3Csvg width='100' height='100' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M11 18c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm48 25c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm-43-7c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm63 31c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM34 90c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm56-76c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM12 86c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm28-65c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm23-11c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-6 60c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 1.79 4 4 4zm29 22c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zM32 63c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm57-13c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-9-21c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM60 91c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM35 41c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM12 60c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2z' fill='%239e9e9e' fill-opacity='0.1' fill-rule='evenodd'/%3E%3C/svg%3E"), linear-gradient(#f0f0f0, #eaeaea);
    /* Pattern similar to common chat apps */
    display: none;
    /* Hidden by default on mobile until selected */
}

.chat-area.active {
    display: flex;
}

.chat-header {
    background: var(--bg-color);
    padding: 10px 15px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 60px;
    box-sizing: border-box;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    /* Prevent horizontal scroll */
    padding: 10px 25px;
    /* Safely padded for scrollbars */
    display: flex;
    flex-direction: column;
}

.message {
    max-width: 70%;
    margin-bottom: 8px;
    padding: 8px 12px 20px 12px;
    border-radius: 12px;
    position: relative;
    /* white-space removed to prevent reply quote expansion */
    word-wrap: break-word;
    /* Legacy support */
    overflow-wrap: anywhere;
    /* Handle long URLs/words better */
    font-size: 15px;
    line-height: 1.4;
    animation: fadeIn 0.1s ease;
    min-width: 80px;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(5px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message.sent {
    align-self: flex-end;
    background-color: var(--my-msg-bg);
    border-bottom-right-radius: 2px;
    max-width: 700px;
}

.message.received {
    align-self: flex-start;
    background-color: var(--other-msg-bg);
    border-bottom-left-radius: 2px;
}

.message img,
.message audio {
    max-width: 100%;
    max-height: 400px;
    /* Prevent huge images */
    width: auto;
    height: auto;
    border-radius: 8px;
    margin-top: 5px;
    cursor: pointer;
}

.message-meta {
    position: absolute;
    bottom: 2px;
    right: 8px;
    font-size: 11px;
    color: var(--meta-color);
    display: flex;
    align-items: center;
    direction: ltr;
    /* Ensure time is always left of tick */
}

.ticks {
    margin-left: 4px;
    color: #42a5f5;
    /* Telegram-like blue */
    font-size: 10px;
}

.chat-input-area {
    background: var(--bg-color);
    padding: 10px;
    display: flex;
    align-items: center;
    border-top: 1px solid var(--border-color);
}

.chat-input-area textarea {
    flex: 1;
    padding: 10px;
    border: none;
    resize: none;
    border-radius: 20px;
    background: var(--secondary-bg);
    font-family: inherit;
    outline: none;
    height: 40px;
    /* Initial height */
    max-height: 120px;
    /* Max grow height */
    overflow-y: auto;
    /* Scroll if exceeds max-height */
    box-sizing: border-box;
    margin: 0 10px;
    line-height: 20px;
}

/* Hide scrollbar for cleaner look if needed, but 'auto' is safer */
.chat-input-area textarea::-webkit-scrollbar {
    width: 4px;
}

.chat-input-area textarea::-webkit-scrollbar-thumb {
    background: #ccc;
    border-radius: 4px;
}

.icon-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--meta-color);
    font-size: 20px;
    padding: 8px;
    border-radius: 50%;
    transition: background 0.1s;
}

.icon-btn:hover {
    background: rgba(0, 0, 0, 0.05);
    color: var(--primary-color);
}

.send-btn {
    color: var(--primary-color);
}

.recording-pulse {
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0% {
        transform: scale(1);
        opacity: 1;
    }

    50% {
        transform: scale(1.2);
        opacity: 0.8;
    }

    100% {
        transform: scale(1);
        opacity: 1;
    }
}

/* Modals */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(5px);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    animation: fadeIn 0.2s ease;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: var(--bg-color);
    padding: 24px;
    border-radius: 16px;
    width: 320px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
    text-align: center;
}

.modal-content h3 {
    margin-top: 0;
    margin-bottom: 20px;
    color: var(--text-color);
    font-size: 20px;
    font-weight: 600;
}

.modal-content input[type="text"] {
    width: 100%;
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    box-sizing: border-box;
    font-size: 15px;
    margin-bottom: 15px;
    background: var(--secondary-bg);
    transition: 0.2s;
}

.modal-content input[type="text"]:focus {
    border-color: var(--primary-color);
    background: white;
    outline: none;
}

#group-members-select {
    max-height: 200px;
    overflow-y: auto;
    text-align: left;
    margin: 10px 0 20px;
    border: 1px solid var(--border-color);
    border-radius: 10px;
}

.member-option {
    padding: 10px 12px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid var(--secondary-bg);
    cursor: pointer;
    transition: background-color 0.1s;
}

.member-option:last-child {
    border-bottom: none;
}

.member-option:hover {
    background-color: var(--secondary-bg);
}

.member-option input {
    margin-right: 12px;
    transform: scale(1.2);
    accent-color: var(--primary-color);
}

.modal-actions {
    display: flex;
    gap: 10px;
}

.modal-actions .btn {
    flex: 1;
}

.btn-secondary {
    background-color: transparent;
    color: var(--primary-color);
    border: 1px solid transparent;
}

.btn-secondary:hover {
    background-color: rgba(51, 144, 236, 0.1);
}

/* RTL Support via class */
.rtl {
    direction: rtl;
    text-align: right;
    font-family: 'Vazirmatn', sans-serif;
}

.rtl .avatar {
    margin-left: 12px;
    margin-right: 0;
}

/* Utils */
.hidden {
    display: none !important;
}

/* Responsive Design */
.chat-area.active {
    display: flex;
}

/* When chat is active, we might want to hide sidebar to avoid scroll issues or interactions behind */
/* But simpler method is just covering it with z-index */

@media (max-width: 768px) {
    .sidebar {
        width: 100%;
        border-right: none;
    }

    .chat-area {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: 100%;
        background-color: var(--bg-color);
        z-index: 50;
    }

    .chat-area.active {
        display: flex;
    }

    .chat-header .icon-btn[onclick*="closeChat"] {
        display: inline-block;
        /* Show back button */
    }

    /* Mobile Reply Fix */
    .reply-text {
        max-width: 180px;
    }
}

/* Desktop overrides */
@media (min-width: 769px) {
    .chat-header .icon-btn[onclick*="closeChat"] {
        display: none;
        /* Hide back button on desktop */
    }

    .chat-area {
        display: flex;
        /* Always visible on desktop */
    }

    #no-chat-selected {
        display: flex;
        /* Show placeholder */
    }

    .chat-area.active #no-chat-selected {
        display: none;
    }
}

@keyframes slideUp {
    from {
        transform: translateY(10px);
        opacity: 0;
    }

    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.context-menu {
    position: fixed;
    background: var(--secondary-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    z-index: 1000;
    overflow: hidden;
    min-width: 150px;
}

.context-menu div {
    padding: 10px 16px;
    cursor: pointer;
    font-size: 14px;
    color: var(--text-color);
}

.context-menu div:hover {
    background: var(--hover-bg);
}

.reply-quote {
    border-inline-start: 3px solid var(--primary-color);
    background: rgba(51, 144, 236, 0.05);
    /* Lighter/Subtle */
    padding: 4px 8px;
    margin-bottom: 6px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 13px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    max-width: 100%;
    overflow: hidden;
    text-align: start;
    /* Follows LTR/RTL */
    white-space: nowrap;
    /* Prevent quotes from expanding with newlines */
}

/* Ensure standard width limit so it doesn't stretch bubble excessively */
.reply-text {
    max-width: 300px;
    min-width: 0;
}

/* Specific style for sent message replies to blend better */
.message.sent .reply-quote {
    border-inline-color: #004d40;
    /* Darker green */
    background: rgba(0, 0, 0, 0.05);
    /* Darken slightly on green bg */
}

.message.sent .reply-quote-sender {
    color: #004d40;
}

.reply-quote-sender {
    color: var(--primary-color);
    font-weight: 600;
    font-size: 12px;
    margin-bottom: 2px;
}

/* Dark Mode Component Overrides */
.dark-mode .chat-area {
    /* Dark pattern: use dark background color */
    background-image: url("data:image/svg+xml,%3Csvg width='100' height='100' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M11 18c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm48 25c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm-43-7c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm63 31c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM34 90c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm56-76c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM12 86c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm28-65c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm23-11c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-6 60c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 1.79 4 4 4zm29 22c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zM32 63c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm57-13c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-9-21c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM60 91c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM35 41c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM12 60c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2z' fill='%23ffffff' fill-opacity='0.05' fill-rule='evenodd'/%3E%3C/svg%3E"), linear-gradient(#0e1621, #0e1621);
}

.dark-mode .message.sent .reply-sender {
    color: #64b5f6;
    /* Lighter blue for visibility on dark */
}

.dark-mode .message.sent .reply-quote {
    background: rgba(0, 0, 0, 0.2);
    border-left-color: #64b5f6;
}

.dark-mode input,
.dark-mode textarea,
.dark-mode .modal-content input[type="text"],
.dark-mode .modal-content input[type="password"] {
    background-color: var(--secondary-bg);
    color: var(--text-color);
    border-color: var(--border-color);
}

.dark-mode .modal-content input[type="text"]:focus,
.dark-mode .modal-content input[type="password"]:focus {
    background-color: var(--bg-color);
    border-color: var(--primary-color);
    color: var(--text-color);
}

.dark-mode input::placeholder,
.dark-mode textarea::placeholder {
    color: #888;
}

.reply-quote:hover {
    opacity: 0.9;
}

.message.sent .reply-quote {
    /* For the light green background (#eeffde), use a darker tint for contrast */
    background: rgba(0, 100, 0, 0.05);
    border-left-color: #4cae4f;
    /* Greenish border to match sent theme */
}

.reply-sender {
    font-weight: 600;
    color: var(--primary-color);
    font-size: 12.5px;
    margin-bottom: 2px;
}

.message.sent .reply-sender {
    /* Changed from white to a dark green/teal for readability on light green */
    color: #2e7d32;
}

.reply-text {
    color: var(--text-color);
    opacity: 0.85;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 13px;
}

/* Group & Sender Info */
.message-wrapper {
    display: flex;
    margin-bottom: 2px;
}

.message-wrapper.received {
    align-self: flex-start;
}

.group-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    margin-right: 8px;
    background: #ccc;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 13px;
    font-weight: bold;
    align-self: flex-end;
    /* Align avatar to bottom of message group */
    background-size: cover;
}

.group-sender-name {
    font-size: 12px;
    font-weight: 600;
    color: #e65100;
    /* Distinct orange/color */
    margin-bottom: 2px;
    cursor: pointer;
}

/* Ensure message bubble in wrapper behaves */
.message-wrapper .message {
    margin-bottom: 0;
    /* handled by wrapper margin */
}

/* Modal Fixes */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

.modal.active {
    display: flex;
}

/* Modal Content Scroll Fix */
#image-preview-modal .modal-content {
    max-height: 90vh;
    width: 500px;
    max-width: 90%;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

/* Message Bubble Fit Content Fix */
.message {
    width: fit-content;
    min-width: 80px;
}

.message.sent,
.message.received {
    max-width: 85%;
}

/* Fix Image Preview Image sizing */
#image-preview-container {
    width: 100%;
    display: flex;
    justify-content: center;
    padding: 10px;
    background: #000;
}

#image-preview-img {
    max-width: 100%;
    max-height: 50vh;
    object-fit: contain;
    display: block;
}

.message-text {
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}

/* Audio Player Fix */
/* Audio Player Fix */
.message audio {
    display: block;
    width: 250px;
    max-width: 100%;
    margin: 5px 0;
    height: 40px;
    /* Force standard height */
    min-height: 40px;
}

/* Date Divider (Inline - Static) */
.chat-date-separator {
    text-align: center;
    margin: 15px 0;
    pointer-events: none;
}

.chat-date-separator span,
#floating-date-badge span {
    background-color: rgba(0, 0, 0, 0.4);
    color: #fff;
    padding: 5px 12px;
    border-radius: 14px;
    font-size: 12px;
    font-weight: 600;
    display: inline-block;
    backdrop-filter: blur(2px);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.dark-mode .chat-date-separator span,
.dark-mode #floating-date-badge span {
    background-color: rgba(255, 255, 255, 0.15);
}

/* Floating Date Badge (Hidden by default) */
#floating-date-badge {
    position: absolute;
    top: 10px;
    /* Below header */
    left: 0;
    width: 100%;
    text-align: center;
    z-index: 10;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.3s ease;
}