/*
 * Customer Chat Widget
 * Refreshed UI for readability, spacing, and responsive behavior.
 */

.chat-widget-container {
    --chat-font: "Plus Jakarta Sans", "Manrope", "Lato", sans-serif;
    --chat-accent: #0f766e;
    --chat-accent-strong: #115e59;
    --chat-accent-soft: #99f6e4;
    --chat-highlight: #f59e0b;
    --chat-surface: #ffffff;
    --chat-surface-soft: #f7fbfa;
    --chat-line: #d9e8e5;
    --chat-text: #112321;
    --chat-muted: #5f7a75;
    --chat-shadow: 0 24px 56px rgba(10, 35, 31, 0.24);
    --chat-content-gutter: 12px;
    --chat-mobile-window-gap: 16px;
    position: fixed;
    right: 24px;
    bottom: 24px;
    z-index: 1000;
    font-family: var(--chat-font);
}

.chat-toggle-button {
    width: 58px;
    height: 58px;
    border: 0;
    border-radius: 18px;
    color: #ffffff;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(145deg, var(--chat-accent), #14b8a6);
    box-shadow: 0 12px 28px rgba(15, 118, 110, 0.38);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    position: relative;
}

.chat-toggle-button:hover {
    transform: translateY(-2px);
    box-shadow: 0 16px 34px rgba(15, 118, 110, 0.45);
}

.chat-toggle-button svg {
    position: absolute;
    width: 24px;
    height: 24px;
    transition: opacity 0.18s ease, transform 0.18s ease;
}

.chat-icon-close {
    opacity: 0;
    transform: rotate(-90deg) scale(0.8);
}

.chat-toggle-button.open .chat-icon-open {
    opacity: 0;
    transform: rotate(90deg) scale(0.8);
}

.chat-toggle-button.open .chat-icon-close {
    opacity: 1;
    transform: rotate(0deg) scale(1);
}

.notification-dot {
    position: absolute;
    right: -2px;
    top: -2px;
    width: 12px;
    height: 12px;
    border-radius: 999px;
    border: 2px solid #ffffff;
    background: #ef4444;
    transform: scale(0);
    transition: transform 0.16s ease;
}

.notification-dot.visible {
    transform: scale(1);
}

.chat-window {
    position: absolute;
    right: 0;
    bottom: 78px;
    width: 380px;
    max-width: calc(100vw - 28px);
    height: min(680px, calc(100vh - 116px));
    border-radius: 22px;
    border: 1px solid var(--chat-line);
    background:
        radial-gradient(circle at 92% -18%, rgba(20, 184, 166, 0.24), transparent 54%),
        radial-gradient(circle at -18% 108%, rgba(245, 158, 11, 0.15), transparent 56%),
        var(--chat-surface);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-shadow: var(--chat-shadow);
    opacity: 0;
    visibility: hidden;
    transform: translateY(14px) scale(0.98);
    transition: opacity 0.24s ease, transform 0.24s ease, visibility 0.24s ease;
}

.chat-window.open {
    opacity: 1;
    visibility: visible;
    transform: translateY(0) scale(1);
}

.chat-body::before,
.chat-body::after {
    position: absolute;
    inset: 0;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
}

.chat-body::before {
    content: "";
    z-index: 9;
    background: rgba(15, 118, 110, 0.2);
    box-shadow: inset 0 0 0 2px rgba(20, 184, 166, 0.9);
}

.chat-body::after {
    content: "Drop file here to upload";
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #ffffff;
    font-size: 13px;
    font-weight: 700;
    letter-spacing: 0.02em;
    text-align: center;
    text-shadow: 0 1px 2px rgba(6, 78, 59, 0.35);
    padding: 14px;
}

.chat-window.chat-drop-active .chat-body::before,
.chat-window.chat-drop-active .chat-body::after {
    opacity: 1;
}

.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    min-height: 60px;
    padding: 8px var(--chat-content-gutter);
    border-bottom: 1px solid var(--chat-line);
    backdrop-filter: blur(4px);
}

.chat-header-title {
    display: flex;
    align-items: center;
    gap: 6px;
    flex: 1;
    min-width: 0;
}

.chat-header-identity {
    display: flex;
    flex-direction: column;
    gap: 0;
    min-width: 0;
    max-width: 200px;
}

.chat-header-title h3 {
    margin: 0;
    padding: 0;
    font-size: 15px;
    line-height: 1.02;
    color: var(--chat-text);
    font-weight: 700;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-header-subtitle {
    display: block;
    margin: 0;
    padding: 0;
    font-size: 11px;
    line-height: 1;
    font-weight: 600;
    color: var(--chat-muted);
}

.chat-nav-button,
.header-action-btn {
    width: 34px;
    height: 34px;
    border: 0;
    border-radius: 10px;
    background: transparent;
    color: #4a6862;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background-color 0.16s ease, color 0.16s ease;
}

.chat-nav-button:hover,
.header-action-btn:hover {
    background: #e7f4f1;
    color: var(--chat-accent-strong);
}

.header-action-btn {
    border-radius: 999px;
    font-size: 26px;
    line-height: 1;
}

#check-chat-messages-btn {
    border-radius: 10px;
    font-size: 0;
}

#check-chat-messages-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
    background: transparent;
    color: #94a3b8;
}

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

.chat-window.chat-active-session .chat-header {
    min-height: 58px;
    padding: 7px var(--chat-content-gutter);
}

.chat-window.chat-active-session .chat-header-title h3 {
    font-size: 14px;
}

.chat-window.chat-active-session .chat-header-subtitle {
    font-size: 10px;
}

.chat-window.chat-waiting-staff .chat-header-subtitle {
    color: #a16207;
}

.chat-window.chat-waiting-staff .chat-status-msg {
    background: #fef3c7;
    color: #92400e;
}

.chat-body {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 12px var(--chat-content-gutter);
    position: relative;
    overflow: hidden;
}

.chat-body.history-active {
    overflow: hidden;
    touch-action: none;
}

.main-chat-view {
    display: flex;
    flex-direction: column;
    min-height: 0;
    flex: 1;
    overflow: hidden;
}

.main-chat-view.blocked {
    pointer-events: none;
}

.main-chat-view.hidden {
    display: none;
}

.faq-view,
.faq-answer-view,
.chat-room-view {
    display: none;
    flex-direction: column;
    gap: 10px;
    min-height: 0;
    flex: 1;
}

.faq-view.active,
.faq-answer-view.active,
.chat-room-view.active {
    display: flex;
}

.faq-view,
.faq-answer-view {
    overflow-y: auto;
    scrollbar-gutter: stable;
}

.chat-room-view {
    position: relative;
    overflow: hidden;
}

.agent-chat-messages {
    display: flex;
    flex-direction: column;
    gap: 0;
    overflow-y: auto;
    flex: 1;
    min-height: 0;
    scrollbar-gutter: stable;
}

.conversation-history-view {
    position: absolute;
    inset: 0;
    z-index: 4;
    background: linear-gradient(180deg, #f8fcfb 0%, #ffffff 100%);
    border-radius: 14px;
    border: 1px solid var(--chat-line);
    display: flex;
    flex-direction: column;
    transform: translateX(-104%);
    transition: transform 0.24s ease;
    pointer-events: none;
}

.conversation-history-view.active {
    transform: translateX(0);
    pointer-events: auto;
}

.conversation-empty-state {
    margin: auto;
    text-align: center;
    padding: 0 var(--chat-content-gutter);
    color: var(--chat-muted);
}

.conversation-empty-state h4 {
    margin: 0 0 8px;
    color: var(--chat-text);
    font-size: 17px;
}

.conversation-empty-state p {
    margin: 0;
    font-size: 13px;
    line-height: 1.5;
}

.conversation-list {
    list-style: none;
    margin: 0;
    padding: 8px var(--chat-content-gutter);
    overflow-y: auto;
    flex: 1;
}

.conversation-item {
    border: 1px solid transparent;
    border-radius: 12px;
    padding: 10px;
    margin-bottom: 6px;
    cursor: pointer;
    background: #ffffff;
    transition: border-color 0.16s ease, background-color 0.16s ease, transform 0.16s ease;
}

.conversation-item:hover {
    border-color: #cbe5df;
    background: #f6fdfb;
    transform: translateY(-1px);
}

.conversation-item.active {
    border-color: #7fd3c7;
    background: #effcf8;
}

.conversation-item-header {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: center;
}

.conversation-item-header strong {
    color: var(--chat-text);
    font-size: 13px;
}

.conversation-date {
    font-size: 11px;
    color: var(--chat-muted);
    white-space: nowrap;
}

.conversation-item-meta {
    margin-top: 6px;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 11px;
    color: var(--chat-muted);
}

.badge {
    display: inline-flex;
    align-items: center;
    border-radius: 999px;
    padding: 2px 8px;
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}

.badge-success {
    background: #dcfce7;
    color: #166534;
}

.badge-secondary {
    background: #e7edf0;
    color: #455a64;
}

.chat-message {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    margin-top: 12px;
    animation: chatMsgIn 0.2s ease;
}

.chat-message:first-child {
    margin-top: 0;
}

@keyframes chatMsgIn {
    from {
        opacity: 0;
        transform: translateY(6px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.chat-message.user {
    justify-content: flex-end;
}

.chat-message.user .message-content {
    order: 1;
}

.chat-message.user .user-icon {
    order: 2;
}

.chat-message.user .message-content {
    background: linear-gradient(145deg, var(--chat-accent), #0891b2);
    color: #ffffff;
    border-bottom-right-radius: 6px;
}

.chat-message.bot .message-content {
    border-bottom-left-radius: 6px;
}

.chat-message.system {
    justify-content: center;
}

.chat-message.system .message-content {
    background: #edf6f4;
    border: 1px dashed #b9d7d0;
    color: #35554f;
    font-size: 12px;
    text-align: center;
}

.chat-avatar {
    width: 30px;
    height: 30px;
    border-radius: 10px;
    flex: 0 0 30px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.bot-icon {
    color: #ffffff;
    background: linear-gradient(145deg, #f59e0b, #f97316);
    box-shadow: 0 4px 12px rgba(245, 158, 11, 0.35);
}

.user-icon {
    color: #ffffff;
    font-size: 11px;
    font-weight: 800;
    background: linear-gradient(145deg, #0891b2, #0f766e);
    box-shadow: 0 4px 12px rgba(15, 118, 110, 0.35);
    text-transform: uppercase;
}

.message-content {
    max-width: min(82%, 280px);
    border: 1px solid #dbe9e6;
    background: #ffffff;
    border-radius: 14px;
    padding: 9px 11px;
    display: flex;
    flex-direction: column;
    gap: 7px;
    color: var(--chat-text);
    font-size: 13px;
    line-height: 1.45;
    word-break: break-word;
}

.message-body {
    white-space: pre-wrap;
    word-break: break-word;
}

.message-content img {
    display: block;
    max-width: 100%;
    border-radius: 10px;
}

.chat-image-preview {
    width: auto;
    max-width: min(100%, 220px);
    height: auto;
    border-radius: 10px;
}

.chat-message.group-middle,
.chat-message.group-end {
    margin-top: 3px;
}

.chat-message.bot.group-middle,
.chat-message.bot.group-end {
    padding-left: 38px;
}

.chat-message.user.group-middle,
.chat-message.user.group-end {
    padding-right: 38px;
}

.chat-message.bot.group-middle .bot-icon,
.chat-message.bot.group-end .bot-icon,
.chat-message.user.group-middle .user-icon,
.chat-message.user.group-end .user-icon {
    display: none;
}

.chat-message.bot.group-middle .message-content,
.chat-message.bot.group-end .message-content {
    border-top-left-radius: 8px;
    border-bottom-left-radius: 8px;
}

.chat-message.user.group-middle .message-content,
.chat-message.user.group-end .message-content {
    border-top-right-radius: 8px;
    border-bottom-right-radius: 8px;
}

.message-meta {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 0;
    font-size: 10px;
}

.chat-message.bot .message-meta {
    justify-content: flex-start;
}

.message-time {
    opacity: 0.75;
}

.message-status {
    border-radius: 999px;
    padding: 1px 6px;
    font-weight: 700;
    font-size: 10px;
    line-height: 1.5;
    letter-spacing: 0.2px;
}

.message-status.sending {
    background: #fef3c7;
    color: #92400e;
}

.message-status.queued {
    background: #e0f2fe;
    color: #075985;
}

.message-status.sent {
    background: #dcfce7;
    color: #166534;
}

.message-status.failed {
    background: #fee2e2;
    color: #b91c1c;
}

.chat-status-msg {
    margin: 4px auto;
    font-size: 12px;
    color: var(--chat-muted);
    background: #edf4f2;
    border-radius: 999px;
    padding: 4px 10px;
}

.chat-suggestions {
    display: grid;
    gap: 8px;
}

.faq-answer-card {
    border: 1px solid #d5e7e3;
    border-radius: 14px;
    background: #ffffff;
    padding: 12px;
}

.faq-answer-label {
    margin: 0;
    color: var(--chat-muted);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.2px;
    text-transform: uppercase;
}

.faq-answer-question {
    margin: 6px 0 8px;
    font-size: 14px;
    line-height: 1.4;
    color: var(--chat-text);
}

.faq-answer-content {
    margin: 0;
    font-size: 13px;
    line-height: 1.55;
    color: #1f3f39;
}

.chat-faq-reset-btn {
    border: 1px solid #9ad4ca;
    border-radius: 10px;
    background: #ffffff;
    color: var(--chat-accent-strong);
    font-size: 12px;
    font-weight: 700;
    padding: 9px 10px;
    cursor: pointer;
    transition: background-color 0.16s ease;
}

.chat-faq-reset-btn:hover {
    background: #f2faf8;
}

.suggestions-title {
    margin: 0;
    color: var(--chat-muted);
    font-size: 12px;
    font-weight: 700;
}

.suggestion-btn {
    border: 1px solid #d3e5e2;
    border-radius: 10px;
    background: #ffffff;
    color: #14312d;
    padding: 9px 10px;
    text-align: left;
    font-size: 12px;
    line-height: 1.35;
    cursor: pointer;
    transition: border-color 0.16s ease, background-color 0.16s ease;
}

.suggestion-btn:hover {
    border-color: #8ccfc3;
    background: #f3fbf9;
}

.chat-agent-section {
    flex-shrink: 0;
    border-top: 1px solid var(--chat-line);
    padding: 10px var(--chat-content-gutter);
    background: linear-gradient(180deg, #ffffff 0%, #f5fbfa 100%);
}

.chat-agent-btn {
    width: 100%;
    border: 0;
    border-radius: 11px;
    padding: 10px 12px;
    cursor: pointer;
    color: #ffffff;
    font-size: 13px;
    font-weight: 700;
    letter-spacing: 0.2px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    background: linear-gradient(130deg, var(--chat-accent), #0ea5a3);
    box-shadow: 0 9px 20px rgba(15, 118, 110, 0.28);
    transition: transform 0.16s ease, box-shadow 0.16s ease;
}

.chat-agent-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 12px 22px rgba(15, 118, 110, 0.36);
}

.chat-agent-btn:disabled {
    opacity: 0.65;
    pointer-events: none;
}

.chat-footer {
    flex-shrink: 0;
    border-top: 1px solid var(--chat-line);
    background: #ffffff;
    padding: 10px var(--chat-content-gutter);
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
}

.chat-attachment-preview {
    display: none;
    width: 100%;
    border: 1px solid #cfe3df;
    border-radius: 10px;
    background: #f8fcfb;
    padding: 8px;
    gap: 8px;
}

.chat-attachment-preview.active {
    display: flex;
    flex-direction: column;
}

.chat-attachment-preview-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.chat-attachment-count {
    font-size: 11px;
    font-weight: 700;
    color: #3f5f59;
}

.chat-attachment-clear {
    border: 0;
    background: transparent;
    font-size: 11px;
    font-weight: 700;
    color: #0f766e;
    cursor: pointer;
    padding: 0;
}

.chat-attachment-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
    max-height: 140px;
    overflow-y: auto;
}

.chat-attachment-item {
    display: flex;
    align-items: center;
    gap: 8px;
    border: 1px solid #deece9;
    background: #ffffff;
    border-radius: 8px;
    padding: 6px;
}

.chat-attachment-thumb {
    width: 34px;
    height: 34px;
    border-radius: 8px;
    background: #e3f3ef;
    overflow: hidden;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 34px;
}

.chat-attachment-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.chat-attachment-thumb.file {
    font-size: 9px;
    font-weight: 700;
    color: #0f766e;
}

.chat-attachment-meta {
    display: flex;
    flex-direction: column;
    min-width: 0;
    gap: 2px;
    flex: 1;
}

.chat-attachment-name {
    font-size: 11px;
    color: #1f3f39;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-attachment-size {
    font-size: 10px;
    color: #5f7a75;
}

.chat-attachment-remove {
    border: 0;
    width: 24px;
    height: 24px;
    border-radius: 999px;
    background: #f1f5f4;
    color: #5f7a75;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    line-height: 1;
    padding: 0;
    flex: 0 0 24px;
}

.chat-attachment-remove:hover {
    background: #e2ecea;
    color: #1f3f39;
}

.chat-footer input {
    flex: 1;
    min-width: 0;
    border: 1px solid #cfe3df;
    border-radius: 10px;
    padding: 10px 11px;
    font-size: 13px;
    color: #14312d;
    background: #fbfdfd;
}

.chat-footer input:focus {
    outline: 0;
    border-color: #6ec4b7;
    box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.16);
    background: #ffffff;
}

.chat-attach-btn,
.chat-footer>button {
    width: 40px;
    height: 40px;
    border: 0;
    border-radius: 10px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex: 0 0 40px;
    color: #3f5f59;
    background: #edf6f4;
    transition: background-color 0.16s ease, color 0.16s ease;
}

.chat-attach-btn:hover,
.chat-footer>button:hover {
    background: #dff1ed;
    color: var(--chat-accent-strong);
}

.chat-footer>button:disabled,
.chat-attach-btn:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}

.chat-footer-disabled {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 8px;
}

.chat-footer-disabled input {
    background: #f0f5f4;
    color: #7b8e8a;
    cursor: not-allowed;
}

.chat-footer-disabled button {
    background: #e7efed;
    color: #8aa09b;
    cursor: not-allowed;
}

.continue-session-btn {
    margin-top: 8px;
    display: flex;
    justify-content: center;
}

.btn-continue-session {
    border: 0;
    border-radius: 999px;
    padding: 8px 14px;
    font-size: 12px;
    font-weight: 700;
    color: #ffffff;
    background: linear-gradient(135deg, #0284c7, #0ea5e9);
    cursor: pointer;
}

.btn-continue-session:disabled {
    opacity: 0.65;
    cursor: not-allowed;
}

.new-conversation-btn {
    margin: 6px var(--chat-content-gutter) 8px;
    border: 1px solid #9ad4ca;
    background: #ffffff;
    color: var(--chat-accent-strong);
    border-radius: 10px;
    font-size: 12px;
    font-weight: 700;
    padding: 9px;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    gap: 6px;
    cursor: pointer;
}

.new-conversation-btn:hover {
    background: #f2faf8;
}

.chat-window.session-validating .chat-agent-section,
.chat-window.session-validating .new-conversation-btn {
    display: none !important;
}

@media (max-width: 768px) {
    .chat-widget-container {
        --chat-content-gutter: 16px;
        right: var(--chat-mobile-window-gap);
        bottom: 14px;
    }

    .chat-window {
        width: min(calc(100vw - (var(--chat-mobile-window-gap) * 2)), 420px);
        right: 0;
        bottom: 72px;
        height: min(82vh, 640px);
        border-radius: 18px;
    }

    .chat-toggle-button {
        width: 54px;
        height: 54px;
        border-radius: 16px;
    }

    .message-content {
        max-width: 86%;
    }
}

@media (max-width: 460px) {
    .chat-widget-container {
        bottom: 12px;
    }

    .chat-window {
        width: calc(100vw - (var(--chat-mobile-window-gap) * 2));
        height: min(86vh, 640px);
        max-width: none;
    }

    .chat-header {
        min-height: 58px;
        gap: 4px;
    }

    .chat-header-title h3 {
        font-size: 14px;
    }

    .chat-header-identity {
        max-width: 160px;
    }

    .chat-nav-button,
    .header-action-btn {
        width: 32px;
        height: 32px;
    }

    .header-action-btn {
        font-size: 24px;
    }

    .chat-window.chat-active-session .chat-header-identity {
        max-width: 136px;
    }

    .chat-footer,
    .chat-agent-section {
        padding-top: 8px;
        padding-bottom: 8px;
    }

    .chat-footer input {
        font-size: 12px;
        padding: 9px 10px;
    }

    .chat-attach-btn,
    .chat-footer button {
        width: 38px;
        height: 38px;
        flex-basis: 38px;
    }
}

@media (max-width: 380px) {
    .chat-header {
        min-height: 56px;
        padding-top: 6px;
        padding-bottom: 6px;
    }

    .chat-header-title {
        gap: 6px;
    }

    .chat-header-title h3 {
        font-size: 13px;
    }

    .chat-header-subtitle {
        font-size: 10px;
    }

    .chat-header-identity {
        max-width: 140px;
    }

    .chat-window.chat-active-session .chat-header-identity {
        max-width: 118px;
    }

    .chat-nav-button,
    .header-action-btn {
        width: 30px;
        height: 30px;
    }

    .header-action-btn {
        font-size: 22px;
    }
}
