/* ==========================================================
   Telegram Live Chat Widget
   Toàn bộ selector scope trong #tcb-bubble / #tcb-panel để
   tránh xung đột với CSS sẵn có của theme website.
   ========================================================== */

#tcb-bubble,
#tcb-bubble *,
#tcb-panel,
#tcb-panel * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
    -webkit-tap-highlight-color: transparent;
}

#tcb-bubble {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: #229ED9; /* đổi theo brand nếu muốn */
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 999998;
    transition: transform 0.2s ease;
    border: none;
    /* an toàn cho iPhone có notch/thanh gesture */
    margin-bottom: env(safe-area-inset-bottom, 0px);
    margin-right: env(safe-area-inset-right, 0px);
}
#tcb-bubble:hover { transform: scale(1.06); }
#tcb-bubble svg {
    width: 28px;
    height: 28px;
    fill: #fff;
    pointer-events: none;
}

#tcb-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    background: #ff4d4f;
    color: #fff;
    font-size: 11px;
    line-height: 1;
    font-weight: 700;
    min-width: 18px;
    height: 18px;
    border-radius: 9px;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
}

#tcb-panel {
    position: fixed;
    bottom: 96px;
    right: 20px;
    width: 340px;
    max-width: calc(100vw - 24px);
    height: 460px;
    /* dvh xử lý đúng chiều cao trên mobile (thanh địa chỉ Safari co giãn),
       fallback vh cho trình duyệt cũ không hỗ trợ dvh */
    max-height: min(560px, calc(100vh - 140px));
    max-height: min(560px, calc(100dvh - 140px));
    background: #fff;
    border-radius: 14px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
    display: none;
    flex-direction: column;
    overflow: hidden;
    z-index: 999999;
    line-height: 1.4;
}
#tcb-panel.tcb-open { display: flex; }

#tcb-header {
    background: #229ED9;
    color: #fff;
    padding: 14px 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
    gap: 8px;
}
#tcb-header h4 {
    font-size: 15px;
    font-weight: 600;
    color: #fff;
    line-height: 1.3;
}
#tcb-header span {
    font-size: 12px;
    opacity: 0.85;
    color: #fff;
    display: block;
}
#tcb-close {
    cursor: pointer;
    font-size: 22px;
    line-height: 1;
    background: none;
    border: none;
    color: #fff;
    width: 28px;
    height: 28px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
}
#tcb-close:hover { background: rgba(255, 255, 255, 0.15); }

#tcb-messages {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 12px;
    background: #f5f7fa;
    display: flex;
    flex-direction: column;
}

.tcb-msg {
    max-width: 80%;
    margin-bottom: 8px;
    padding: 8px 12px;
    border-radius: 12px;
    font-size: 13.5px;
    line-height: 1.4;
    word-wrap: break-word;
    overflow-wrap: break-word;
    white-space: pre-wrap;
    color: #222;
}
#tcb-panel .tcb-msg-user {
    background: #229ED9;
    color: #fff;
    align-self: flex-end;
    border-bottom-right-radius: 4px;
    padding: 3px 5px;
    margin: 5px 0px;
}
#tcb-panel .tcb-msg-admin {
    background: #fff;
    color: #222;
    border: 1px solid #e3e6ea;
    align-self: flex-start;
    border-bottom-left-radius: 4px;
    padding: 3px 5px;
    margin: 5px 0px;

}
.tcb-msg-time {
    font-size: 10px;
    opacity: 0.6;
    margin-top: 3px;
    display: block;
    color: inherit;
}

#tcb-name-gate {
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    flex-shrink: 0;
}
#tcb-name-gate input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid #ddd;
    border-radius: 8px;
    font-size: 14px;
    line-height: 1.3;
    color: #222;
    background: #fff;
    -webkit-appearance: none;
    appearance: none;
    outline: none;
}
#tcb-name-gate input:focus { border-color: #229ED9; }
#tcb-name-gate button {
    width: 100%;
    padding: 10px 12px;
    border: none;
    border-radius: 8px;
    background: #229ED9;
    color: #fff;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    -webkit-appearance: none;
    appearance: none;
}
#tcb-name-gate button:hover { background: #1c8bc0; }

#tcb-input-bar {
    display: flex;
    align-items: flex-end;
    border-top: 1px solid #eee;
    padding: 8px;
    flex-shrink: 0;
    background: #fff;
    gap: 8px;
    /* tránh bị thanh home iPhone che khi bàn phím đóng */
    padding-bottom: max(8px, env(safe-area-inset-bottom, 8px));
}
#tcb-input-bar textarea {
    flex: 1;
    min-width: 0;
    resize: none;
    border: 1px solid #ddd;
    border-radius: 8px;
    padding: 8px 10px;
    font-size: 13.5px;
    line-height: 1.4;
    color: #222;
    background: #fff;
    max-height: 70px;
    -webkit-appearance: none;
    appearance: none;
    outline: none;
}
#tcb-input-bar textarea:focus { border-color: #229ED9; }
#tcb-send-btn {
    flex-shrink: 0;
    background: #229ED9;
    color: #fff;
    border: none;
    border-radius: 8px;
    padding: 10px 14px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 600;
    -webkit-appearance: none;
    appearance: none;
}
#tcb-send-btn:hover { background: #1c8bc0; }
#tcb-send-btn:disabled { opacity: 0.5; cursor: default; }

/* ===== Responsive ===== */

/* Tablet nhỏ / màn hẹp: panel co lại vừa khung nhìn */
@media (max-width: 600px) {
    #tcb-panel {
        right: 12px;
        left: 12px;
        width: auto;
        max-width: none;
        bottom: 88px;
    }
}

/* Điện thoại: full-width gần hết màn hình, chiều cao ưu tiên khung nhìn thực tế */
@media (max-width: 480px) {
    #tcb-bubble {
        width: 54px;
        height: 54px;
        bottom: 16px;
        right: 16px;
    }
    #tcb-bubble svg { width: 24px; height: 24px; }

    #tcb-panel {
        right: 10px;
        left: 10px;
        bottom: 80px;
        max-height: min(70vh, calc(100dvh - 110px));
        border-radius: 12px;
    }

    #tcb-header { padding: 12px 14px; }
    #tcb-header h4 { font-size: 14px; }
    #tcb-header span { font-size: 11px; }

    .tcb-msg { max-width: 86%; font-size: 13px; }
}

/* Màn hình rất nhỏ (< 360px) */
@media (max-width: 360px) {
    #tcb-panel {
        right: 6px;
        left: 6px;
        bottom: 76px;
    }
}

/* Chế độ ngang (landscape) trên điện thoại: chiều cao khung nhìn thấp */
@media (max-height: 480px) and (orientation: landscape) {
    #tcb-panel {
        bottom: 8px;
        max-height: calc(100dvh - 16px);
        height: calc(100dvh - 16px);
    }
    #tcb-bubble { bottom: 8px; }
}
