/* EduCore Chat — WhatsApp-style UI */
.chat-wrap * { box-sizing: border-box; }
.chat-layout { display: flex; height: calc(100vh - 70px); height: calc(100dvh - 70px); background: #f0f2f5; overflow: hidden; }
@media (max-width: 768px) { .chat-layout { height: calc(100vh - 60px); height: calc(100dvh - 60px); } }

/* ==== SIDEBAR (Chat List) ==== */
.chat-sidebar { width: 340px; background: #fff; border-right: 1px solid #e5e5e5; display: flex; flex-direction: column; flex-shrink: 0; }
@media (max-width: 768px) { .chat-sidebar { width: 100%; } }
.chat-sidebar-header { padding: 14px 18px; background: #9E3039; color: #fff; display: flex; align-items: center; justify-content: space-between; }
.chat-sidebar-header h2 { font-size: 17px; font-weight: 700; margin: 0; }
.chat-sidebar-header .badge { background: #FFD700; color: #9E3039; font-size: 11px; font-weight: 700; padding: 3px 9px; border-radius: 20px; }
.chat-search { padding: 10px 14px; background: #fff; border-bottom: 1px solid #f0f0f0; }
.chat-search input { width: 100%; padding: 9px 14px; border: 1px solid #e0e0e0; border-radius: 20px; font-size: 16px; background: #f7f7f7; }
.chat-search input:focus { outline: none; border-color: #9E3039; background: #fff; }
.chat-list { flex: 1; overflow-y: auto; }
.chat-item { display: flex; align-items: center; gap: 12px; padding: 12px 16px; border-bottom: 1px solid #f5f5f5; cursor: pointer; text-decoration: none; color: inherit; transition: background .15s; }
.chat-item:hover { background: #f9f9f9; }
.chat-item.active { background: #fff5f5; border-left: 3px solid #9E3039; padding-left: 13px; }
.chat-avatar { width: 48px; height: 48px; border-radius: 50%; background: linear-gradient(135deg, #9E3039, #c94854); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 18px; flex-shrink: 0; }
.chat-avatar.gold { background: linear-gradient(135deg, #FFD700, #d4a800); color: #333; }
.chat-avatar.green { background: linear-gradient(135deg, #2C4A3A, #4a7a5c); }
.chat-item-body { flex: 1; min-width: 0; }
.chat-item-name { font-weight: 600; font-size: 14px; color: #1a1a1a; margin-bottom: 2px; }
.chat-item-role { font-size: 11px; color: #9E3039; text-transform: uppercase; letter-spacing: .3px; font-weight: 600; }
.chat-item-preview { font-size: 12.5px; color: #888; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.chat-item-meta { text-align: right; flex-shrink: 0; }
.chat-item-time { font-size: 11px; color: #999; }
.chat-unread-badge { display: inline-block; background: #e53935; color: #fff; font-size: 10px; font-weight: 700; min-width: 18px; height: 18px; line-height: 18px; text-align: center; padding: 0 5px; border-radius: 10px; margin-top: 3px; }

/* ==== MAIN CHAT ==== */
.chat-main { flex: 1; display: flex; flex-direction: column; background: #efe7dd; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='40' height='40'%3E%3Ccircle cx='2' cy='2' r='0.5' fill='%23e5d5c0'/%3E%3Ccircle cx='20' cy='20' r='0.5' fill='%23e5d5c0'/%3E%3C/svg%3E"); }
@media (max-width: 768px) { .chat-main { display: none; } .chat-main.mobile-active { display: flex; } .chat-sidebar.mobile-hidden { display: none; } }
.chat-header { padding: 12px 16px; background: #9E3039; color: #fff; display: flex; align-items: center; gap: 12px; flex-shrink: 0; }
.chat-header-back { background: none; border: none; color: #fff; font-size: 22px; cursor: pointer; padding: 4px 8px; display: none; }
@media (max-width: 768px) { .chat-header-back { display: block; } }
.chat-header-info { flex: 1; min-width: 0; }
.chat-header-name { font-weight: 600; font-size: 15px; }
.chat-header-status { font-size: 12px; opacity: .85; }
.chat-messages { flex: 1; overflow-y: auto; padding: 20px 16px; }
.chat-msg { max-width: 70%; padding: 8px 12px; border-radius: 12px; margin-bottom: 8px; font-size: 14px; line-height: 1.4; word-wrap: break-word; position: relative; box-shadow: 0 1px 2px rgba(0,0,0,.08); }
@media (max-width: 480px) { .chat-msg { max-width: 85%; } }
.chat-msg.mine { background: #d9f7c4; margin-left: auto; border-top-right-radius: 2px; }
.chat-msg.other { background: #fff; margin-right: auto; border-top-left-radius: 2px; }
.chat-msg-time { font-size: 10px; color: #888; text-align: right; margin-top: 3px; display: block; }
.chat-msg-sender { font-size: 11px; font-weight: 700; color: #9E3039; margin-bottom: 3px; }
.chat-input-bar { padding: 10px 12px; background: #fff; border-top: 1px solid #e0e0e0; display: flex; gap: 8px; align-items: flex-end; flex-shrink: 0; }
.chat-input-bar textarea { flex: 1; padding: 10px 14px; border: 1px solid #e0e0e0; border-radius: 20px; font-size: 16px; font-family: inherit; resize: none; max-height: 100px; min-height: 40px; }
.chat-input-bar textarea:focus { outline: none; border-color: #9E3039; }
.chat-send-btn { width: 44px; height: 44px; border-radius: 50%; background: #9E3039; color: #fff; border: none; cursor: pointer; display: flex; align-items: center; justify-content: center; font-size: 18px; flex-shrink: 0; transition: transform .15s; }
.chat-send-btn:active { transform: scale(.95); }
.chat-send-btn:disabled { background: #ccc; cursor: not-allowed; }

/* ==== EMPTY STATES ==== */
.chat-empty { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; color: #999; padding: 40px; text-align: center; }
.chat-empty-icon { font-size: 64px; margin-bottom: 12px; opacity: .3; }
.chat-empty-title { font-size: 16px; font-weight: 600; color: #666; margin-bottom: 6px; }
.chat-empty-text { font-size: 13px; }

/* ==== NEW CHAT BUTTON ==== */
.new-chat-btn { position: fixed; bottom: 24px; right: 24px; width: 54px; height: 54px; border-radius: 50%; background: #9E3039; color: #fff; border: none; cursor: pointer; font-size: 24px; display: flex; align-items: center; justify-content: center; box-shadow: 0 6px 20px rgba(158,48,57,.4); z-index: 50; }
.new-chat-btn:active { transform: scale(.95); }

/* ==== MODAL (New Chat) ==== */
.chat-modal-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 200; align-items: center; justify-content: center; padding: 20px; }
.chat-modal-overlay.active { display: flex; }
.chat-modal { background: #fff; border-radius: 16px; width: 100%; max-width: 480px; max-height: 80vh; display: flex; flex-direction: column; overflow: hidden; }
.chat-modal-header { padding: 16px 20px; border-bottom: 1px solid #eee; display: flex; justify-content: space-between; align-items: center; }
.chat-modal-header h3 { margin: 0; font-size: 16px; font-weight: 700; color: #9E3039; }
.chat-modal-close { background: none; border: none; font-size: 22px; cursor: pointer; color: #999; padding: 4px 8px; }
.chat-modal-body { flex: 1; overflow-y: auto; padding: 8px 0; }
.contact-item { display: flex; align-items: center; gap: 12px; padding: 11px 20px; cursor: pointer; transition: background .15s; text-decoration: none; color: inherit; }
.contact-item:hover { background: #f9f9f9; }
.contact-avatar { width: 42px; height: 42px; border-radius: 50%; background: linear-gradient(135deg, #9E3039, #c94854); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 16px; flex-shrink: 0; }
.contact-body { flex: 1; min-width: 0; }
.contact-name { font-weight: 600; font-size: 14px; }
.contact-role { font-size: 11px; color: #9E3039; text-transform: uppercase; font-weight: 600; }
.contact-empty { padding: 40px 20px; text-align: center; color: #999; font-size: 13px; }

/* ==== TYPING INDICATOR ==== */
.typing-indicator { font-size: 12px; color: #666; font-style: italic; padding: 4px 16px; height: 20px; }
.typing-dots { display: inline-block; }
.typing-dots span { display: inline-block; width: 5px; height: 5px; border-radius: 50%; background: #9E3039; animation: typingBounce 1.2s infinite; margin-right: 2px; }
.typing-dots span:nth-child(2) { animation-delay: .2s; }
.typing-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes typingBounce { 0%, 60%, 100% { transform: translateY(0); opacity: .4; } 30% { transform: translateY(-5px); opacity: 1; } }

/* ==== TOAST ==== */
.chat-toast { position: fixed; top: 80px; right: 20px; background: #333; color: #fff; padding: 12px 18px; border-radius: 10px; font-size: 13px; z-index: 300; opacity: 0; transform: translateX(30px); transition: opacity .3s, transform .3s; }
.chat-toast.show { opacity: 1; transform: translateX(0); }

/* ==== SCROLLBAR (thin) ==== */
.chat-list::-webkit-scrollbar, .chat-messages::-webkit-scrollbar, .chat-modal-body::-webkit-scrollbar { width: 6px; }
.chat-list::-webkit-scrollbar-thumb, .chat-messages::-webkit-scrollbar-thumb, .chat-modal-body::-webkit-scrollbar-thumb { background: #ccc; border-radius: 3px; }

/* ========== MOBILE HEIGHT FIX ========== */
/* Lock body scroll on chat pages */
body.chat-page {
    overflow: hidden !important;
    height: 100vh;
    height: 100dvh;
}

/* Chat layout uses dynamic viewport height */
.chat-layout {
    height: 100vh;   /* fallback */
    height: 100dvh;  /* modern browsers - handles keyboard */
}

/* Messages area with proper flex */
.chat-main {
    height: 100%;
    overflow: hidden;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
    padding-bottom: 12px;
}

.chat-input-bar {
    position: sticky;
    bottom: 0;
    z-index: 5;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
}

/* Mobile specific */
@media (max-width: 768px) {
    .chat-layout {
        height: 100vh;
        height: 100dvh;
    }
    
    .chat-main {
        display: flex !important;
        flex-direction: column;
        height: 100dvh;
    }
    
    .chat-main.mobile-active {
        display: flex !important;
    }
    
    .chat-sidebar.mobile-hidden {
        display: none !important;
    }
    
    .chat-header {
        flex-shrink: 0;
        position: sticky;
        top: 0;
        z-index: 10;
    }
    
    .chat-msg {
        font-size: 13.5px;
        padding: 7px 11px;
    }
    
    /* Compress sidebar header on mobile */
    .chat-sidebar-header {
        padding: 10px 14px;
    }
    
    .chat-sidebar-header h2 {
        font-size: 15px;
    }
}

/* Landscape mobile */
@media (max-height: 500px) and (orientation: landscape) {
    .chat-header {
        padding: 6px 12px;
    }
    
    .chat-input-bar {
        padding: 6px 10px;
    }
    
    .chat-input-bar textarea {
        min-height: 34px;
        padding: 7px 12px;
    }
    
    .chat-send-btn {
        width: 36px;
        height: 36px;
    }
}

/* ========== FIX: Body scroll lock ========== */
body.chat-page {
    overflow: hidden !important;
    height: 100vh !important;
    height: 100dvh !important;
}

/* Chat layout ko topbar ke baad ka space do */
body.chat-page .chat-wrap {
    height: calc(100vh - 70px) !important;
    height: calc(100dvh - 70px) !important;
    overflow: hidden;
}

@media (max-width: 768px) {
    body.chat-page .chat-wrap {
        height: calc(100vh - 60px) !important;
        height: calc(100dvh - 60px) !important;
    }
}

body.chat-page .chat-layout {
    height: 100% !important;
    max-height: 100% !important;
}

body.chat-page .chat-main {
    height: 100% !important;
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden;
}

body.chat-page .chat-messages {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    overflow-y: auto !important;
}

body.chat-page .chat-input-bar {
    flex: 0 0 auto !important;
    display: flex !important;
    visibility: visible !important;
    opacity: 1 !important;
    min-height: 56px;
    position: relative !important;
    z-index: 100 !important;
    background: #fff !important;
    border-top: 1px solid #e0e0e0 !important;
}

/* Ensure textarea always visible */
body.chat-page .chat-input-bar textarea {
    display: block !important;
    visibility: visible !important;
    min-height: 40px;
    background: #f7f7f7 !important;
    border: 1px solid #e0e0e0 !important;
}

body.chat-page .chat-send-btn {
    display: flex !important;
    visibility: visible !important;
}

/* ========== SEARCH BAR (Contacts Modal) ========== */
.chat-modal-search {
    padding: 10px 16px;
    border-bottom: 1px solid #f0f0f0;
    background: #fafafa;
}
.chat-modal-search input {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid #e0e0e0;
    border-radius: 20px;
    font-size: 16px;
    background: #fff;
    font-family: inherit;
    box-sizing: border-box;
}
.chat-modal-search input:focus {
    outline: none;
    border-color: #9E3039;
}

/* ========== SIDEBAR SCROLL FIX ========== */
.sidebar {
    height: 100vh !important;
    height: 100dvh !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 30px !important;
}

.sidebar .menu-item:last-child,
.sidebar > a:last-child {
    margin-bottom: 30px !important;
}

/* ========== TOPBAR MOBILE COMPRESSION ========== */
@media (max-width: 768px) {
    .topbar {
        padding: 6px 8px !important;
        gap: 4px !important;
    }
    
    .topbar-left { gap: 6px !important; }
    
    .brand {
        font-size: 10px !important;
        max-width: 34vw !important;
        line-height: 1.1 !important;
    }
    
    .brand small {
        font-size: 8px !important;
        line-height: 1.1 !important;
    }
    
    .brand-logo {
        width: 28px !important;
        height: 28px !important;
        flex-shrink: 0 !important;
    }
    
    .topbar-right {
        gap: 4px !important;
        flex-shrink: 0 !important;
    }
    
    .menu-toggle {
        width: 36px !important;
        height: 36px !important;
        font-size: 18px !important;
        flex-shrink: 0 !important;
    }
    
    /* Login As — smaller */
    .topbar a[href*="login-as"] {
        padding: 5px 8px !important;
        font-size: 10px !important;
        flex-shrink: 0 !important;
    }
    
    /* View toggle */
    .view-toggle {
        padding: 5px 8px !important;
        font-size: 10px !important;
        flex-shrink: 0 !important;
    }
    
    /* User info */
    .user-info {
        font-size: 10px !important;
        flex-shrink: 0 !important;
    }
    
    .user-info .name {
        font-size: 11px !important;
        line-height: 1.05 !important;
        white-space: nowrap;
    }
    
    .user-info .role {
        font-size: 9px !important;
        line-height: 1.05 !important;
        white-space: nowrap;
    }
    
    .user-avatar {
        width: 30px !important;
        height: 30px !important;
        font-size: 12px !important;
        flex-shrink: 0 !important;
    }
}

/* Very small phones */
@media (max-width: 400px) {
    .user-info .role { display: none !important; }
    .view-toggle { font-size: 0 !important; padding: 6px 7px !important; }
    .view-toggle::before { content: '📱'; font-size: 12px; }
}

/* ========== TOAST NOTIFICATIONS ========== */
.edu-toast-container {
    position: fixed;
    top: 80px;
    right: 16px;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: 10px;
    max-width: 340px;
    pointer-events: none;
}
@media (max-width: 480px) {
    .edu-toast-container {
        top: 70px;
        right: 8px;
        left: 8px;
        max-width: none;
    }
}
.edu-toast {
    background: #fff;
    border-radius: 12px;
    padding: 14px 16px;
    box-shadow: 0 12px 32px rgba(0,0,0,.18);
    display: flex;
    align-items: center;
    gap: 12px;
    border-left: 4px solid #9E3039;
    cursor: pointer;
    pointer-events: auto;
    transform: translateX(120%);
    opacity: 0;
    transition: transform .35s cubic-bezier(.2,.9,.3,1), opacity .35s;
    text-decoration: none;
    color: inherit;
}
.edu-toast.show {
    transform: translateX(0);
    opacity: 1;
}
.edu-toast-icon {
    width: 40px; height: 40px;
    border-radius: 50%;
    background: linear-gradient(135deg, #9E3039, #c94854);
    color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-size: 18px;
    flex-shrink: 0;
    font-weight: 700;
}
.edu-toast.broadcast .edu-toast-icon {
    background: linear-gradient(135deg, #FFD700, #d4a800);
    color: #333;
}
.edu-toast-body { flex: 1; min-width: 0; }
.edu-toast-title {
    font-size: 13px;
    font-weight: 700;
    color: #1a1a1a;
    margin-bottom: 2px;
}
.edu-toast-msg {
    font-size: 12px;
    color: #666;
    line-height: 1.35;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}
.edu-toast-close {
    background: none;
    border: none;
    color: #ccc;
    font-size: 18px;
    cursor: pointer;
    padding: 2px 4px;
    flex-shrink: 0;
}


/* ============================================
   MESSAGE OPTIONS MENU (WhatsApp-style)
   ============================================ */
.chat-msg { position: relative; }

.msg-options-menu {
    background: #fff;
    border-radius: 10px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.18);
    padding: 6px;
    min-width: 160px;
    z-index: 10000;
    animation: menuFadeIn 0.15s ease-out;
}
@keyframes menuFadeIn {
    from { opacity: 0; transform: scale(0.92); }
    to   { opacity: 1; transform: scale(1); }
}
.msg-opt {
    display: block;
    width: 100%;
    padding: 10px 14px;
    border: none;
    background: transparent;
    text-align: left;
    font-size: 14px;
    cursor: pointer;
    border-radius: 6px;
    color: #333;
    font-family: inherit;
}
.msg-opt:hover { background: #f0f0f0; }
.msg-opt-danger { color: #e53935; }
.msg-opt-danger:hover { background: #ffebee; }

.msg-deleted {
    color: #999;
    font-style: italic;
    font-size: 0.92em;
}
.msg-edited {
    font-size: 0.78em;
    color: #999;
    margin-left: 5px;
    font-style: italic;
}

@media (max-width: 600px) {
    .msg-options-menu { min-width: 140px; }
    .msg-opt { padding: 12px 16px; }
    .chat-msg {
        -webkit-touch-callout: none;
        -webkit-user-select: none;
        user-select: none;
    }
    .chat-msg-body { -webkit-user-select: text; user-select: text; }
}

/* ============================================
   CHAT ATTACHMENTS
   ============================================ */
.chat-attach-btn {
    background: transparent;
    border: none;
    font-size: 22px;
    cursor: pointer;
    padding: 6px 8px;
    color: #555;
    flex-shrink: 0;
}
.chat-attach-btn:hover { color: #9E3039; }

.chat-attach-img {
    max-width: 240px;
    max-height: 240px;
    border-radius: 10px;
    margin-bottom: 6px;
    cursor: pointer;
    display: block;
    transition: transform 0.15s;
}
.chat-attach-img:hover { transform: scale(1.02); }

.chat-attach-file {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 10px 14px;
    background: rgba(0,0,0,0.05);
    border-radius: 10px;
    text-decoration: none;
    color: #333;
    font-size: 13px;
    margin-bottom: 6px;
    border: 1px solid rgba(0,0,0,0.08);
    transition: background 0.15s;
}
.chat-attach-file:hover { background: rgba(0,0,0,0.1); }
.chat-attach-file small { color: #888; font-size: 11px; }

.chat-input-bar { display: flex; align-items: center; gap: 4px; }

/* CHAT ATTACHMENT STYLES */
.chat-attach-btn {
    background: transparent;
    border: none;
    font-size: 22px;
    cursor: pointer;
    padding: 6px 8px;
    color: #555;
    flex-shrink: 0;
    transition: color 0.15s;
}
.chat-attach-btn:hover { color: #9E3039; }

.chat-attach-img {
    max-width: 240px;
    max-height: 240px;
    border-radius: 10px;
    margin-bottom: 6px;
    display: block;
    cursor: pointer;
}

.chat-attach-file {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 10px 14px;
    background: rgba(0,0,0,0.06);
    border-radius: 10px;
    text-decoration: none;
    color: #333;
    font-size: 13px;
    margin-bottom: 6px;
    border: 1px solid rgba(0,0,0,0.08);
}
.chat-attach-file:hover { background: rgba(0,0,0,0.12); }
.chat-attach-file small { color: #888; font-size: 11px; }





/* ============================================
   CHAT MOBILE INPUT — Font size 16px (prevent zoom)
   ============================================ */
@media (max-width: 768px) {
    #msgInput,
    .chat-input-bar textarea,
    .chat-input-bar input[type="text"],
    input[type="text"],
    input[type="email"],
    input[type="password"],
    select,
    textarea {
        font-size: 16px !important;
    }
}

/* ═══ P6.14 body-wrap — long text + emoji handling ═══ */
.chat-msg-body {
  word-wrap: break-word !important;
  overflow-wrap: anywhere !important;
  word-break: break-word !important;
  white-space: pre-wrap !important;
  max-width: 100%;
}
.chat-msg { word-wrap: break-word !important; overflow-wrap: anywhere !important; }
.chat-attach-file { word-break: break-all; max-width: 100%; }
/* ═══ end P6.14 ═══ */

/* ═══ P6.15 emoji button ═══ */
.chat-emoji-btn {
  background: transparent;
  border: none;
  font-size: 22px;
  cursor: pointer;
  padding: 6px 8px;
  color: #555;
  flex-shrink: 0;
  transition: transform 0.15s;
}
.chat-emoji-btn:active { transform: scale(1.15); }
.chat-emoji-btn:hover { color: #9E3039; }
#emojiPanel button:hover { background: #f0f2f5; }
/* ═══ end P6.15 ═══ */

/* P6.16c — emoji btn SVG mode */
.chat-emoji-btn {
  background: transparent !important;
  border: none !important;
  color: #555 !important;
  cursor: pointer !important;
  padding: 6px 8px !important;
  flex-shrink: 0 !important;
  display: inline-flex !important;
  align-items: center !important;
  justify-content: center !important;
  min-width: 34px !important;
  min-height: 34px !important;
  visibility: visible !important;
  opacity: 1 !important;
}
.chat-emoji-btn:hover { color: #9E3039 !important; }
.chat-emoji-btn:active { transform: scale(1.15); }
/* end P6.16c */
