/* --- 1. Structure Overrides --- */

/* Remove default padding from main-content to allow full-screen chat layout */
.main-content.chat-interface-wrapper {
    padding: 0 !important;
    height: 100vh;
    overflow: hidden; /* Prevent double scrollbar */
    display: flex;
    flex-direction: column;
}

/* On mobile, adjust for top/bottom bars */
@media (max-width: 992px) {
    .main-content.chat-interface-wrapper {
        height: calc(100vh - 72px) !important; /* Subtract bottom nav */
        padding-top: 1px !important; /* Space for top header */
    }
}

/* --- 2. Inner Chat Layout (The 3rd Pane) --- */

.chat-layout {
    display: flex;
    height: 100%;
    width: 100%;
    background: var(--bg-white);
    border-radius: 0; /* Flat on full screen */
}

/* Left Panel: Chat List */
.chat-list-panel {
    width: 350px;
    border-right: 1px solid var(--border-light);
    display: flex;
    flex-direction: column;
    background: var(--bg-white);
}

.panel-header {
    padding: 20px;
    border-bottom: 1px solid var(--border-light);
}

.search-wrapper {
    position: relative;
    margin-top: 15px;
}

.search-wrapper input {
    width: 100%;
    padding: 10px 10px 10px 35px;
    border-radius: 20px;
    background: var(--bg-light);
    border: 1px solid var(--border-medium);
    color: var(--text-dark);
}

.search-wrapper i {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-medium);
}

.search-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    width: 100%;
    background: var(--bg-white);
    border: 1px solid var(--border-light);
    box-shadow: 0 4px 10px rgba(0,0,0,0.1);
    z-index: 50;
    max-height: 200px;
    overflow-y: auto;
    border-radius: 8px;
    margin-top: 5px;
}

.search-result {
    padding: 10px;
    display: flex;
    align-items: center;
    cursor: pointer;
}
.search-result:hover { background: var(--bg-light); }
.search-result img { width: 30px; height: 30px; border-radius: 50%; margin-right: 10px; }

/* Chat List Items */
.chat-items-container {
    flex: 1;
    overflow-y: auto;
}

.chat-card {
    display: flex;
    align-items: center;
    padding: 15px 20px;
    cursor: pointer;
    border-bottom: 1px solid var(--border-light);
    transition: background 0.2s;
}

.chat-card:hover, .chat-card.active {
    background: var(--bg-light);
}

.chat-card.pinned {
    background: linear-gradient(to right, rgba(16, 185, 129, 0.05), transparent);
    border-left: 3px solid var(--primary-color);
}

.chat-avatar-wrapper { position: relative; }
.chat-avatar-wrapper img {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    object-fit: cover;
    margin-right: 15px;
}

.chat-info {
    flex: 1;
    overflow: hidden;
}

.chat-top {
    display: flex;
    justify-content: space-between;
    margin-bottom: 4px;
}

.chat-top .name { font-weight: 600; color: var(--text-dark); }
.chat-top .time { font-size: 0.75rem; color: var(--text-medium); }
.last-msg {
    font-size: 0.85rem;
    color: var(--text-medium);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Right Panel: Conversation */
.conversation-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg-light); /* Slight contrast from list */
    position: relative;
}

.empty-state {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--text-medium);
    text-align: center;
}
.empty-state i { font-size: 3rem; margin-bottom: 15px; opacity: 0.3; }

/* Active Chat UI */
#active-chat-container {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.chat-header {
    height: 65px;
    background: var(--bg-white);
    border-bottom: 1px solid var(--border-light);
    display: flex;
    align-items: center;
    padding: 0 20px;
    justify-content: space-between;
}

.header-info {
    display: flex;
    align-items: center;
    cursor: pointer; /* Click to view profile */
}

.header-info img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-right: 12px;
}

.status-indicator {
    font-size: 0.8rem;
    color: var(--text-medium);
}
.status-indicator.online { color: #10B981; }

.messages-scroll-area {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

/* --- Message Bubbles with DP (Groups) --- */
.msg-row {
    display: flex;
    align-items: flex-end;
    margin-bottom: 12px;
    max-width: 80%;
}

.msg-row.incoming { align-self: flex-start; }
.msg-row.outgoing { align-self: flex-end; flex-direction: row-reverse; }

.msg-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    margin: 0 8px;
    cursor: pointer;
    flex-shrink: 0;
}

.msg-bubble {
    padding: 10px 14px;
    border-radius: 18px;
    position: relative;
    font-size: 0.95rem;
    line-height: 1.4;
    word-wrap: break-word;
}

.incoming .msg-bubble {
    background: var(--bg-white);
    border: 1px solid var(--border-medium);
    border-bottom-left-radius: 4px;
    color: var(--text-dark);
}

.outgoing .msg-bubble {
    background: var(--primary-color);
    color: white;
    border-bottom-right-radius: 4px;
}

/* Sender Name in Group Chat */
.msg-sender-name {
    font-size: 0.75rem;
    color: var(--text-medium);
    margin-bottom: 2px;
    margin-left: 14px; /* Align with bubble */
    display: block;
}

/* Input Area */
.input-area {
    padding: 15px 20px;
    background: var(--bg-white);
    border-top: 1px solid var(--border-light);
    display: flex;
    gap: 10px;
}

.input-area input {
    flex: 1;
    padding: 12px 15px;
    border-radius: 24px;
    border: 1px solid var(--border-medium);
    background: var(--bg-light);
    color: var(--text-dark);
}

/* --- Mobile Logic --- */
@media (max-width: 768px) {
    .chat-layout {
        position: relative;
    }

    .chat-list-panel {
        width: 100%;
        height: 100%;
        position: absolute;
        z-index: 10;
        transition: transform 0.3s ease;
    }

    .conversation-panel {
        width: 100%;
        height: 100%;
        position: absolute;
        z-index: 20;
        transform: translateX(100%); /* Hidden by default */
        transition: transform 0.3s ease;
    }

    /* Active State: Show conversation */
    .chat-layout.mobile-view-chat .chat-list-panel { transform: translateX(-100%); }
    .chat-layout.mobile-view-chat .conversation-panel { transform: translateX(0); }

    .mobile-nav-link.active {
        color: var(--primary-color);
    }
}

/* --- 1. Structure Overrides --- */

.main-content.chat-interface-wrapper {
    padding: 0 !important;
    height: 100vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

/* Mobile Adjustments */
@media (max-width: 992px) {
    .main-content.chat-interface-wrapper {
        /* Account for Top Bar (approx 60px) and Bottom Nav (72px) */
        height: calc(100vh - 72px) !important; 
        padding-top: -1px !important; /* Matches mobile-top-bar height */
        margin-top: 0 !important; /* Removes extra gap */
    }
    
    .chat-layout {
        height: 100%;
        border-top: 1px solid var(--border-light); /* Separation from top bar */
    }
}

/* Mobile Back Button Styling */
.mobile-only-back {
    display: none;
    margin-right: 15px;
    background: none;
    border: none;
    font-size: 1.2rem;
    color: var(--text-dark);
    padding: 5px;
}

@media (max-width: 768px) {
    .mobile-only-back {
        display: block; /* Only show inside the chat on mobile */
    }
}

/* --- FIXED HEADER SPACING --- */
.chat-header {
    height: 60px; /* Reduced height */
    padding: 0 15px;
    /* Fix line-height/gap issues */
    line-height: 1.2; 
}

.header-info h3 {
    margin: 0;
    font-size: 1rem;
    line-height: 1.2;
    margin-bottom: 2px; /* Small gap between name and status */
}

.header-info .status-indicator {
    font-size: 0.75rem;
    line-height: 1;
    display: block;
}

/* --- CUSTOM CONTEXT MENU (Right Click) --- */
.custom-context-menu {
    position: fixed; /* Changed from absolute to fixed */
    background: var(--bg-white);
    border: 1px solid var(--border-light);
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    border-radius: 8px;
    z-index: 9999; /* Increased Z-index to be on top of everything */
    min-width: 160px;
    overflow: hidden;
    display: none;
}

.context-menu-item {
    padding: 10px 15px;
    cursor: pointer;
    font-size: 0.9rem;
    color: var(--text-dark);
    display: flex;
    align-items: center;
    gap: 10px;
    transition: background 0.2s;
}

.context-menu-item:hover {
    background: var(--bg-light);
}

.context-menu-item.danger {
    color: #ef4444;
}

.context-menu-item.disabled {
    opacity: 0.5;
    pointer-events: none;
    cursor: not-allowed;
}

/* --- CREATE GROUP MODAL --- */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.5);
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-card {
    background: var(--bg-white);
    padding: 20px;
    border-radius: 12px;
    width: 90%;
    max-width: 400px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.2);
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 20px;
}

/* --- Add to chat.css --- */

/* Message Bubble Layout for Timestamp */
.message-bubble {
    position: relative;
    padding-bottom: 20px; /* Make space for time */
    min-width: 120px; /* Ensure bubble doesn't squash too small */
}

.msg-time {
    position: absolute;
    bottom: 4px;
    right: 8px;
    font-size: 0.65rem;
    opacity: 0.7;
    white-space: nowrap;
}

/* Adjust text color based on bubble type */
.message.sent .msg-time {
    color: rgba(255, 255, 255, 0.9);
}

.message.received .msg-time {
    color: #666;
}

/* Ensure avatars don't flicker (Issue 5 CSS Fix) */
.chat-item-avatar img,
.msg-avatar {
    transition: opacity 0.2s ease-in-out;
    background-color: #f0f0f0; /* Placeholder color */
}

/* --- Message Timestamp & Pin Fixes --- */

/* Make bubble relative so absolute timestamp sits inside */
.msg-bubble {
    position: relative;
    padding-bottom: 20px !important; /* Space for time */
    min-width: 80px;
}

/* Time styling */
.msg-time {
    position: absolute;
    bottom: 3px;
    right: 10px;
    font-size: 0.6rem;
    pointer-events: none;
    opacity: 0.8;
}

/* Adjust colors for visibility */
.outgoing .msg-time { color: rgba(255,255,255, 0.9); }
.incoming .msg-time { color: #888; }

/* Pin Icon in Chat List */
.pin-icon-indicator {
    font-size: 0.75rem;
    color: var(--primary-color);
    margin-left: 6px;
    transform: rotate(45deg);
}

/* Fix for broken images */
img {
    text-indent: -10000px; /* Hides "alt" text overlap */
}

/* Context Menu Icons Alignment */
.context-menu-item i {
    width: 25px;
    text-align: center;
    margin-right: 5px;
}

/* Edit Indicator in Bubble */
.msg-bubble span {
    font-style: italic;
}

/* --- FIX: Constrain Images within Bubbles --- */

/* Ensure images don't exceed the bubble width */
.msg-bubble img {
    max-width: 100%;
    height: auto;
    border-radius: 8px; /* Nice rounded corners for images inside bubbles */
    display: block; /* Prevents tiny gaps below images */
    margin-top: 5px;
}

/* If using the grid system from before, ensure it fits too */
.media-grid {
    display: grid;
    gap: 4px;
    margin-top: 5px;
    border-radius: 12px;
    overflow: hidden;
    width: 100%; /* Take available space within bubble */
}

/* Ensure grid images also conform */
.media-grid img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    aspect-ratio: 1 / 1; /* thumbnails */
    max-width: unset; /* Reset max-width for grid items specifically */
}

/* --- Context Menu Theme Fix --- */
.custom-context-menu {
    background: var(--bg-white);
    border: 1px solid var(--border-light);
    color: var(--text-dark);
    box-shadow: var(--shadow-lg);
}

.context-menu-item {
    color: var(--text-dark);
    transition: background 0.2s;
}

.context-menu-item:hover {
    background: var(--bg-light);
}

/* Explicit Dark Mode Overrides to prevent "White Box" issue */
[data-theme="dark"] .custom-context-menu {
    background: #1f2937; /* Dark Grey */
    border: 1px solid #374151;
    color: #f3f4f6;
}

[data-theme="dark"] .context-menu-item {
    color: #f3f4f6; /* Light Text */
}

[data-theme="dark"] .context-menu-item:hover {
    background: #374151; /* Lighter Grey on Hover */
}

[data-theme="dark"] .context-menu-item i {
    color: var(--primary-color); /* Keep icons vibrant */
}

/* --- TELEGRAM-STYLE GRID LAYOUT --- */
.media-grid {
    display: grid;
    gap: 2px;
    border-radius: 12px;
    overflow: hidden;
    margin-top: 5px;
    width: 100%;
    max-width: 320px; /* Limits grid width */
}

.media-grid img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    cursor: pointer;
    transition: filter 0.2s;
}
.media-grid img:hover { filter: brightness(0.9); }

/* Grid Variations */
.grid-1 { grid-template-columns: 1fr; }
.grid-1 img { max-height: 300px; object-fit: contain; background: rgba(0,0,0,0.05); }

.grid-2 { grid-template-columns: 1fr 1fr; aspect-ratio: 2/1; }
.grid-3 { grid-template-columns: 1fr 1fr 1fr; aspect-ratio: 3/1; }
.grid-4 { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; aspect-ratio: 1/1; }

/* If more than 4, we just show 4 in a square (you can expand logic if needed) */
.grid-more { grid-template-columns: repeat(auto-fit, minmax(80px, 1fr)); }

/* --- FULL SCREEN IMAGE VIEWER (LIGHTBOX) --- */
.image-viewer-modal {
    display: none;
    position: fixed;
    z-index: 10000;
    top: 0; left: 0;
    width: 100%; height: 100%;
    background-color: rgba(0, 0, 0, 0.9);
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.image-viewer-modal.active { opacity: 1; }

.image-viewer-content {
    max-width: 95%;
    max-height: 95vh;
    border-radius: 4px;
    box-shadow: 0 0 20px rgba(0,0,0,0.5);
    object-fit: contain;
}

.image-viewer-close {
    position: absolute;
    top: 20px; right: 30px;
    color: white;
    font-size: 40px;
    font-weight: bold;
    cursor: pointer;
    background: none;
    border: none;
    z-index: 10001;
}

.msg-bubble span {
    font-style: normal; /* Force regular text */
}

/* --- Fix for Edit Group Modal Styles --- */
#edit-group-modal textarea, 
#edit-group-modal input[type="text"] {
    width: 100%;
    padding: 12px 15px;
    border-radius: 8px; /* Matches standard inputs */
    border: 1px solid var(--border-medium);
    background: var(--bg-light);
    color: var(--text-dark);
    font-family: inherit;
    font-size: 0.95rem;
    resize: none; /* Prevents dragging */
    box-sizing: border-box;
}

#edit-group-modal textarea:focus, 
#edit-group-modal input[type="text"]:focus {
    outline: none;
    border-color: var(--primary-color);
    background: var(--bg-white);
}

/* Dark Mode Fixed Preview Bar */
.file-preview-bar {
    display: none; 
    align-items: center;
    justify-content: space-between;
    padding: 12px 20px;
    background-color: var(--bg-light); /* Uses your existing dark theme vars */
    border-top: 1px solid var(--border-light);
    border-bottom: 1px solid var(--border-light);
    color: var(--text-dark);
}

/* Telegram Style Grid */
.media-grid {
    display: grid;
    gap: 4px;
    border-radius: 12px;
    overflow: hidden;
    margin: 5px 0;
    max-width: 400px;
}

/* Grid logic based on count */
.grid-1 img { max-height: 300px; width: auto; border-radius: 12px; }
.grid-2 { grid-template-columns: 1fr 1fr; aspect-ratio: 16/9; }
.grid-3 { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; aspect-ratio: 1/1; }
.grid-3 :first-child { grid-row: span 2; }
.grid-4 { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; aspect-ratio: 1/1; }

.grid-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    cursor: pointer;
    transition: opacity 0.2s;
}
.grid-img:hover { opacity: 0.9; }

/* "More" photos overlay */
.more-photos {
    position: relative;
}
.more-photos::after {
    content: attr(data-count);
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.5);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    font-weight: bold;
}

/* Search Results Theme Support */
#search-results {
    background: var(--bg-white);
    border: 1px solid var(--border-light);
    border-radius: 12px;
    box-shadow: 0 8px 24px rgba(0,0,0,0.15);
    overflow: hidden;
}

.search-header-label {
    padding: 10px 15px;
    font-size: 0.7rem;
    letter-spacing: 1px;
    font-weight: 700;
    color: var(--text-medium);
    background: var(--bg-light);
    border-bottom: 1px solid var(--border-light);
}

.search-result {
    display: flex;
    align-items: center;
    padding: 12px 15px;
    cursor: pointer;
    transition: background 0.2s;
    border-bottom: 1px solid var(--border-light);
    color: var(--text-dark);
}

.search-result:hover {
    background: var(--bg-light);
}

.search-result img {
    width: 42px;
    height: 42px;
    object-fit: cover;
    margin-right: 12px;
}

.search-result .res-name {
    font-weight: 600;
    color: var(--text-dark);
}

.search-result .res-sub {
    font-size: 0.8rem;
    color: var(--text-medium);
}

/* --- TELEGRAM-STYLE PINNED BAR --- */
.pinned-bar {
    background: var(--bg-white);
    padding: 8px 15px;
    border-bottom: 1px solid var(--border-light);
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: relative;
    z-index: 10;
    cursor: pointer;
    animation: fadeIn 0.2s ease;
}

.pinned-bar-content {
    display: flex;
    align-items: center;
    gap: 12px;
    flex: 1;
    overflow: hidden;
}

.pinned-line {
    width: 3px;
    height: 34px;
    background: var(--primary-color);
    border-radius: 2px;
    flex-shrink: 0;
}

.pinned-info {
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.pinned-label {
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--primary-color);
}

.pinned-text {
    font-size: 0.85rem;
    color: var(--text-dark);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 90%;
}

.pinned-actions .icon-btn-small {
    color: var(--text-medium);
    opacity: 0.7;
    padding: 5px;
    background: none;
    border: none;
    cursor: pointer;
}
.pinned-actions .icon-btn-small:hover { opacity: 1; color: var(--error); }

/* --- ADMIN TAGS --- */
.admin-badge {
    font-size: 0.65rem;
    padding: 2px 6px;
    border-radius: 4px;
    margin-left: 6px;
    font-weight: 600;
    text-transform: uppercase;
    display: inline-block;
    vertical-align: middle;
}

.badge-owner {
    color: #fff;
    background-color: var(--primary-color); /* Greenish for Owner */
}

.badge-admin {
    color: var(--primary-color);
    background-color: rgba(16, 185, 129, 0.1);
    border: 1px solid var(--primary-color);
}

/* In Member List */
.member-item-info { display: flex; align-items: center; flex-wrap: wrap; }
.member-name { display: flex; align-items: center; }

/* --- Add to the bottom of chat.css --- */

/* In-Chat Search Bar */
.chat-search-bar {
    background: var(--bg-white);
    padding: 8px 15px;
    border-bottom: 1px solid var(--border-light);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    animation: slideDown 0.2s ease-out;
}

@keyframes slideDown {
    from { transform: translateY(-10px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

.search-input-wrapper {
    flex: 1;
    position: relative;
    display: flex;
    align-items: center;
}

.search-input-wrapper i {
    position: absolute;
    left: 10px;
    color: var(--text-medium);
    font-size: 0.8rem;
}

.search-input-wrapper input {
    width: 100%;
    padding: 8px 10px 8px 30px;
    border-radius: 18px;
    border: 1px solid var(--border-medium);
    background: var(--bg-light);
    color: var(--text-dark);
    font-size: 0.9rem;
}
.search-input-wrapper input:focus {
    outline: none;
    border-color: var(--primary-color);
}

.search-controls {
    display: flex;
    align-items: center;
    gap: 5px;
}

/* Highlighted Text in Messages */
.highlight-text {
    background-color: #fde047; /* Yellow highlight */
    color: black;
    border-radius: 2px;
    font-weight: bold;
}

.highlight-text.active-match {
    background-color: #fb923c; /* Orange for current match */
    box-shadow: 0 0 5px rgba(0,0,0,0.2);
}

/* Fix Header Actions alignment */
.header-actions .icon-btn {
    background: transparent;
    border: none;
    font-size: 1.1rem;
    color: var(--text-medium);
    cursor: pointer;
    padding: 8px;
    border-radius: 50%;
    transition: background 0.2s;
}
.header-actions .icon-btn:hover {
    background: var(--bg-light);
    color: var(--primary-color);
}

/* --- Add to chat.css --- */

/* 1. Animation for jumping to a message */
@keyframes highlight-pulse {
    0% { background-color: rgba(255, 215, 0, 0.1); transform: scale(1); }
    50% { background-color: rgba(255, 215, 0, 0.5); transform: scale(1.02); }
    100% { background-color: transparent; transform: scale(1); }
}

.highlight-msg {
    animation: highlight-pulse 2s ease-out;
    border-radius: 8px;
    position: relative;
    z-index: 5;
}

/* 2. Styles for Search Highlights */
.highlight-text {
    background-color: #fde047; /* Soft Yellow */
    color: black;
    border-radius: 2px;
    font-weight: bold;
    padding: 0 2px;
}

.highlight-text.active-match {
    background-color: #fb923c; /* Orange for the 'current' find */
    box-shadow: 0 0 0 2px rgba(251, 146, 60, 0.5); /* Glow ring */
}

/* 3. Search Bar Animation */
.chat-search-bar {
    background: var(--bg-white);
    padding: 8px 15px;
    border-bottom: 1px solid var(--border-light);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    animation: slideDown 0.2s ease-out;
}

@keyframes slideDown {
    from { transform: translateY(-10px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}