/* Chat Widget Styles */
#chat-widget-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 999999;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    display: block;
}

#chat-launcher {
    width: 60px;
    height: 60px;
    background-color: #1976d2;
    border-radius: 50%;
    box-shadow: 0 4px 8px rgba(0,0,0,0.2);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.3s ease;
    padding: 0;
    margin: 0;
    line-height: 0;
    position: relative; /* For badge positioning */
}

/* Unread Message Badge */
#chat-unread-badge {
    position: absolute;
    top: -5px;
    right: -5px;
    background-color: #f44336; /* Red */
    color: white;
    border-radius: 50%;
    min-width: 20px;
    height: 20px;
    padding: 0 4px;
    font-size: 12px;
    display: none; /* Hidden by default */
    align-items: center;
    justify-content: center;
    border: 2px solid white;
    font-weight: bold;
    z-index: 10;
    box-sizing: border-box;
}

#chat-launcher:hover {
    transform: scale(1.1);
}

#chat-launcher svg {
    width: 32px;
    height: 32px;
    fill: white;
    display: block;
    margin: 0 auto;
}

#chat-window {
    display: none;
    width: 300px;
    height: 400px;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 5px 15px rgba(0,0,0,0.2);
    flex-direction: column;
    overflow: hidden;
    position: absolute;
    bottom: 100px;
    right: 0;
}

#chat-header {
    background-color: #1976d2;
    color: white;
    padding: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

#chat-header h3 {
    margin: 0;
    font-size: 16px;
}

#chat-close {
    cursor: pointer;
    font-size: 20px;
}

#chat-messages {
    flex: 1;
    padding: 15px;
    overflow-y: auto;
    background-color: #f5f5f5;
    display: flex;
    flex-direction: column;
    gap: 10px;
    -webkit-overflow-scrolling: touch;
    min-height: 0; /* ✅ 补丁：允许压缩 */
}

.message {
    max-width: 65%;
    padding: 8px 12px;
    border-radius: 15px;
    font-size: 14px;
    line-height: 1.4;
    word-wrap: break-word;
    box-sizing: border-box;
}

.message.guest {
    align-self: flex-end;
    background-color: #1976d2;
    color: white;
    border-bottom-right-radius: 2px;
    margin-right: 45px;
}

.message.admin {
    align-self: flex-start;
    background-color: #e0e0e0;
    color: #333;
    border-bottom-left-radius: 2px;
    margin-left: 15px;
}

.chat-timestamp {
    align-self: center;
    font-size: 12px;
    color: #999;
    margin: 10px 0;
    text-align: center;
    width: 100%;
}

#chat-input-area {
    padding: 15px 10px; /* Increased vertical padding */
    border-top: 1px solid #eee;
    display: flex;
    background: #fff;
    align-items: center;
    flex-shrink: 0;
    
    /* ✅ 补丁：固定底部 */
    position: sticky;
    bottom: 0;
    z-index: 10;
}

#chat-input {
    flex: 1;
    border: 1px solid #ddd;
    border-radius: 20px;
    padding: 8px 15px;
    outline: none;
    font-size: 14px;
}

#chat-send {
    background: none;
    border: none;
    color: #1976d2;
    font-weight: bold;
    cursor: pointer;
    margin-left: 10px;
    font-size: 16px;
    padding: 5px 10px;
}

/* Mobile Responsive Styles */
@media (max-width: 480px) {
    body.chat-open {
        overflow: hidden;
        position: fixed;
        width: 100%;
        height: 80%;
    }

    #chat-widget-container.open {
        bottom: 0;
        right: 0;
        width: 100%;
        height: 100%;
        z-index: 99999;
    }

    #chat-window {
        width: 100%;
        height: var(--vh, 100vh); /* ✅ 关键补丁 */
        bottom: 0;
        right: 0;
        border-radius: 0;
        position: fixed;
        top: 0;
        left: 0;
        display: none;
        flex-direction: column;
        background-color: #f5f5f5;
        z-index: 100000;
    }

    #chat-messages {
        flex: 1;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        background-color: #f5f5f5;
        padding-bottom: 10px;
        min-height: 0; /* ✅ 再保险 */
    }

    #chat-input-area {
        flex-shrink: 0;
        background: #fff;
        padding-bottom: calc(env(safe-area-inset-bottom) + 15px);
        border-top: 1px solid #ddd;
    }

    #chat-widget-container.open #chat-launcher {
        display: none;
    }

    #chat-input {
        font-size: 16px;
        padding: 10px 15px;
    }
    
    .message {
        font-size: 16px;
    }
}

/* =========================================
   Mobile Page Mode (Standalone) - .mobile-page-mode
   Used by chat_mobile.php
   ========================================= */
body.mobile-page-mode {
    height: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden; /* Prevent body scroll completely */
    background-color: #f5f5f5;
    position: fixed; /* Lock body */
    width: 100%;
}

body.mobile-page-mode #chat-widget-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    bottom: auto;
    right: auto;
    z-index: 1000;
}

body.mobile-page-mode #chat-window {
    display: flex; /* Override display: none */
    flex-direction: column;
    width: 100%;
    height: 100%;
    border-radius: 0;
    box-shadow: none;
    position: relative; /* Internal layout is relative to container */
    bottom: auto;
    right: auto;
}

body.mobile-page-mode #chat-header {
    flex-shrink: 0;
    height: 50px;
    z-index: 2;
}

body.mobile-page-mode #chat-messages {
    flex: 1 1 auto; /* Grow and shrink */
    width: 100%;
    position: relative;
    padding-bottom: 10px;
}

body.mobile-page-mode #chat-input-area {
    flex-shrink: 0; /* Never shrink */
    position: relative;
    z-index: 2;
    padding-bottom: calc(env(safe-area-inset-bottom) + 20px); /* Lift up from bottom */
    transition: padding-bottom 0.2s ease;
}

/* When keyboard is open (class added by JS), remove safe area and apply gap */
body.mobile-page-mode.keyboard-open #chat-input-area {
    padding-bottom: 15px; /* Keep some padding when keyboard is open */
    margin-bottom: var(--keyboard-gap, 0px);
}

body.mobile-page-mode #chat-launcher {
    display: none !important;
}

body.mobile-page-mode #chat-close {
    position: absolute;
    top: 10px;
    left: 10px;
    z-index: 10;
    
    /* Style matching notice.php .back-btn */
    padding: 6px 14px;
    background: #1976d2;
    color: #fff;
    border-radius: 4px;
    text-decoration: none;
    display: flex;
    align-items: center;
    height: auto;
    width: auto;
    line-height: normal;
    font-size: 0; /* Hide the X text */
}

body.mobile-page-mode #chat-close:hover {
    filter: brightness(85%);
}

body.mobile-page-mode #chat-header {
    background-color: #fff; /* White header */
    color: #333;
    border-bottom: 1px solid #eee;
    justify-content: center; /* Center title */
    position: relative;
}

body.mobile-page-mode #chat-header h3 {
    font-size: 16px;
    font-weight: bold;
    color: #333;
}

body.mobile-page-mode #chat-close::after {
    content: "返回首页";
    font-size: 13px; /* Restore font size for text */
}

