/* Chat widget */
#chat-widget {
    position: fixed;
    bottom: 30px;
    right: 30px;
    z-index: 9999;
    transition: transform 0.28s ease, opacity 0.28s ease;
    will-change: transform;
}

#chat-widget.chat-widget--docked {
    transform: translateX(calc(100% - 3px));
}

#chat-widget.chat-widget--open {
    transform: translateX(0);
}

#chat-toggle {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, #F0B90B 0%, #FCD535 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 4px 20px rgba(240, 185, 11, 0.4);
    transition: all 0.3s ease;
    position: relative;
}

#chat-widget:not(.chat-widget--docked) #chat-toggle:hover {
    transform: scale(1.1);
}

#chat-widget.chat-widget--docked #chat-toggle {
    opacity: 0.92;
    box-shadow: 0 4px 16px rgba(240, 185, 11, 0.28);
}

#chat-toggle svg {
    color: var(--text-primary);
}

#chat-badge {
    position: absolute;
    top: -5px;
    right: -5px;
    background: #f44336;
    color: white;
    font-size: 12px;
    min-width: 20px;
    height: 20px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
}

#chat-window,
#chat-globalThis {
    position: absolute;
    bottom: 80px;
    right: 0;
    width: 380px;
    height: 500px;
    background: var(--bg-secondary, #1E2329);
    border-radius: 16px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid var(--border-color, #2B3139);
}

#chat-header {
    background: linear-gradient(135deg, #F0B90B 0%, #FCD535 100%);
    color: var(--text-primary);
    padding: 16px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: 600;
}

#chat-header-actions {
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

#chat-header svg {
    flex-shrink: 0;
}

#chat-end-btn,
#chat-history-btn {
    border: 1px solid rgba(0, 0, 0, 0.12);
    background: rgba(255, 255, 255, 0.28);
    color: var(--text-primary);
    border-radius: 999px;
    padding: 6px 12px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s ease, transform 0.2s ease;
}

#chat-end-btn:hover,
#chat-history-btn:hover {
    background: rgba(255, 255, 255, 0.4);
    transform: translateY(-1px);
}

#chat-close-btn {
    background: none;
    border: none;
    color: var(--text-primary);
    cursor: pointer;
    padding: 8px;
    margin: -8px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: var(--close-btn-transition, 0.2s ease);
}

#chat-close-btn:hover {
    background: rgba(0, 0, 0, 0.1);
    transform: var(--close-btn-hover-transform, rotate(90deg));
}

#chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    background: var(--bg-primary, #0B0E11);
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

#chat-messages::-webkit-scrollbar {
    width: 6px;
}

#chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

#chat-messages::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.2);
    border-radius: 3px;
}

.chat-welcome {
    text-align: center;
    padding: 20px;
    color: var(--text-secondary, #848E9C);
}

.chat-welcome svg {
    margin-bottom: 16px;
}

.chat-welcome h3 {
    margin: 0 0 8px 0;
    color: var(--text-primary, #EAECEF);
}

#chat-quick-questions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 16px;
    justify-content: center;
}

.quick-btn {
    background: var(--bg-secondary, #1E2329);
    border: 1px solid var(--border-color, #2B3139);
    border-radius: 20px;
    padding: 8px 16px;
    font-size: 13px;
    color: var(--text-primary, #EAECEF);
    cursor: pointer;
    transition: all 0.2s;
    -webkit-tap-highlight-color: transparent;
}

.quick-btn:hover {
    background: #F0B90B;
    color: var(--text-primary);
    border-color: #F0B90B;
}

.quick-btn:active {
    transform: scale(0.95);
}

.chat-message {
    margin-bottom: 12px;
    display: flex;
    flex-direction: column;
    animation: msgFadeIn 0.3s ease;
}

@keyframes msgFadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.chat-message.user {
    align-items: flex-end;
}

.chat-message.staff {
    align-items: flex-start;
}

.chat-message.system {
    align-items: center;
}

.chat-bubble {
    max-width: 80%;
    padding: 12px 16px;
    border-radius: 16px;
    font-size: 14px;
    line-height: 1.5;
    word-break: break-word;
}

.chat-message.user .chat-bubble {
    background: linear-gradient(135deg, #F0B90B 0%, #FCD535 100%);
    color: var(--text-primary);
    border-bottom-right-radius: 4px;
}

.chat-message.staff .chat-bubble {
    background: var(--bg-secondary, #1E2329);
    color: var(--text-primary, #EAECEF);
    border: 1px solid var(--border-color, #2B3139);
    border-bottom-left-radius: 4px;
}

.chat-message.system .chat-bubble {
    background: rgba(240, 185, 11, 0.12);
    color: #d3a008;
    border: 1px solid rgba(240, 185, 11, 0.28);
    border-radius: 999px;
    max-width: 92%;
    padding: 8px 14px;
}

.chat-message.system .chat-time {
    text-align: center;
}

.chat-history-empty {
    min-height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    color: var(--text-secondary, #848E9C);
    gap: 10px;
    padding: 24px 18px;
}

.chat-history-empty h3,
.chat-history-empty p {
    margin: 0;
}

.chat-history-spinner {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: 3px solid rgba(240, 185, 11, 0.18);
    border-top-color: #F0B90B;
    animation: chatSpin 0.8s linear infinite;
}

@keyframes chatSpin {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

.chat-history-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.chat-history-caption {
    font-size: 12px;
    color: var(--text-tertiary, #5E6673);
}

.chat-history-item {
    width: 100%;
    border: 1px solid rgba(15, 23, 42, 0.08);
    background: #FFFFFF;
    border-radius: 14px;
    padding: 12px 14px;
    color: #111827;
    text-align: left;
    cursor: pointer;
    transition: border-color 0.2s ease, transform 0.2s ease, background 0.2s ease;
}

.chat-history-item:hover {
    border-color: rgba(240, 185, 11, 0.45);
    background: #FFF8E1;
    transform: translateY(-1px);
}

.chat-history-item__head,
.chat-history-item__meta,
.chat-history-detail__head,
.chat-history-detail__meta,
.chat-history-detail__toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.chat-history-item__head {
    margin-bottom: 6px;
}

.chat-history-item__title {
    font-size: 14px;
    font-weight: 600;
}

.chat-history-item__time,
.chat-history-item__count,
.chat-history-detail__meta {
    font-size: 12px;
    color: #6B7280;
}

.chat-history-item__meta {
    margin-bottom: 8px;
    justify-content: flex-start;
    flex-wrap: wrap;
}

.chat-history-item__preview {
    font-size: 13px;
    color: #4B5563;
    line-height: 1.5;
    word-break: break-word;
}

.chat-history-status {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 999px;
    padding: 4px 10px;
    font-size: 12px;
    font-weight: 600;
}

.chat-history-status.is-open {
    background: rgba(34, 197, 94, 0.14);
    color: #34C759;
}

.chat-history-status.is-closed {
    background: rgba(148, 163, 184, 0.16);
    color: #94A3B8;
}

.chat-history-status.is-manual {
    background: rgba(240, 185, 11, 0.16);
    color: #F0B90B;
}

.chat-history-detail {
    display: flex;
    flex-direction: column;
    gap: 12px;
    min-height: 100%;
}

.chat-history-back {
    border: 1px solid rgba(15, 23, 42, 0.08);
    background: #FFFFFF;
    color: #374151;
    border-radius: 999px;
    padding: 8px 14px;
    font-size: 12px;
    cursor: pointer;
}

.chat-history-detail__card {
    border: 1px solid rgba(15, 23, 42, 0.08);
    border-radius: 14px;
    background: #FFFFFF;
    color: #111827;
    padding: 12px 14px;
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.06);
}

.chat-history-thread {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding-bottom: 4px;
}

.chat-time {
    font-size: 11px;
    color: var(--text-tertiary, #5E6673);
    margin-top: 4px;
    padding: 0 8px;
}

#chat-input-area {
    display: flex;
    padding: 12px 16px;
    background: var(--bg-secondary, #1E2329);
    border-top: 1px solid var(--border-color, #2B3139);
    gap: 10px;
}

#chat-input {
    flex: 1;
    border: 1px solid var(--border-color, #2B3139);
    border-radius: 24px;
    padding: 12px 20px;
    font-size: 16px;
    outline: none;
    background: var(--bg-primary, #0B0E11);
    color: var(--text-primary, #EAECEF);
    -webkit-appearance: none;
}

#chat-input:focus {
    border-color: #F0B90B;
}

#chat-send-btn {
    width: 44px;
    height: 44px;
    min-width: 44px;
    border-radius: 50%;
    border: none;
    background: linear-gradient(135deg, #F0B90B 0%, #FCD535 100%);
    color: var(--text-primary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s;
    -webkit-tap-highlight-color: transparent;
}

#chat-send-btn:active {
    transform: scale(0.9);
}

#chat-send-btn svg {
    color: var(--text-primary);
}

@media (max-width: 768px) {
    #chat-widget {
        bottom: 20px;
        right: 20px;
        bottom: calc(20px + env(safe-area-inset-bottom, 0px));
    }

    #chat-widget.chat-widget--docked {
        transform: translateX(calc(100% - 14px));
    }

    #chat-toggle {
        width: 54px;
        height: 54px;
    }

    #chat-toggle svg {
        width: 24px;
        height: 24px;
    }

    #chat-window,
    #chat-globalThis {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: 100%;
        border-radius: 0;
        animation: chatSlideUp 0.3s ease-out;
    }

    @keyframes chatSlideUp {
        from {
            transform: translateY(100%);
        }
        to {
            transform: translateY(0);
        }
    }

    #chat-header {
        padding: 16px 20px;
        padding-top: calc(16px + env(safe-area-inset-top, 0px));
    }

    #chat-close-btn {
        padding: 12px;
        margin: -12px;
        min-width: 44px;
        min-height: 44px;
    }

    #chat-end-btn,
    #chat-history-btn {
        padding: 8px 12px;
        font-size: 12px;
    }

    #chat-messages {
        padding: 16px;
        padding-bottom: 16px;
    }

    .chat-bubble {
        max-width: 85%;
    }

    #chat-input-area {
        padding: 12px 16px;
        padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
    }

    #chat-input {
        padding: 14px 20px;
    }

    .quick-btn {
        padding: 10px 16px;
        font-size: 14px;
    }
}

@media (max-width: 480px) {
    #chat-quick-questions {
        flex-direction: column;
        align-items: center;
    }

    .quick-btn {
        width: 100%;
        max-width: 200px;
        text-align: center;
    }

    #chat-end-btn,
    #chat-history-btn {
        padding: 8px 10px;
    }
}
