/* DIW Docked Bottom Chat — LiveAgent-style bar + panel */
.diw-chat-restore {
    position: fixed;
    right: 24px;
    bottom: 16px;
    z-index: 1000001;
    width: 44px;
    height: 44px;
    border: 0;
    border-radius: 50%;
    padding: 4px;
    background: #fff;
    box-shadow: 0 8px 24px rgba(15, 42, 74, 0.2);
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
}

.diw-chat-restore.is-visible {
    display: flex;
}

.diw-chat-restore img {
    width: 32px;
    height: 32px;
    max-width: 32px;
    max-height: 32px;
    border-radius: 50%;
    object-fit: cover;
}

#diw-chat-dock {
    --diw-chat-accent: var(--main-ui-color, #3699FF);
    --diw-chat-accent-soft: color-mix(in srgb, var(--diw-chat-accent) 12%, white);
    --diw-chat-shadow: 0 8px 32px rgba(15, 42, 74, 0.18);
    position: fixed;
    right: 24px;
    bottom: 0;
    z-index: 1000001;
    font-family: 'Sarabun', Arial, sans-serif;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

body.law-layout-body #diw-chat-dock {
    --diw-chat-accent: #0d77bd;
}

#diw-chat-dock.is-hidden {
    display: none;
}

/* ย่อแล้วให้คลิกทะลุพื้นที่ panel — รับคลิกเฉพาะแถบล่าง */
#diw-chat-dock:not(.is-open):not(.is-hidden) {
    pointer-events: none;
}

#diw-chat-dock:not(.is-open):not(.is-hidden) .diw-chat-bar,
#diw-chat-dock:not(.is-open):not(.is-hidden) .diw-chat-bar * {
    pointer-events: auto;
}

#diw-chat-dock .diw-chat-panel {
    width: 380px;
    max-width: calc(100vw - 48px);
    height: 0;
    max-height: 0;
    display: flex;
    flex-direction: column;
    background: #fff;
    border-radius: 12px 12px 0 0;
    box-shadow: none;
    border: 0;
    overflow: hidden;
    margin-bottom: 0;
    transform: translateY(8px);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: height 0.22s ease, max-height 0.22s ease, transform 0.22s ease, opacity 0.22s ease, visibility 0.22s ease, box-shadow 0.22s ease, border 0.22s ease;
}

#diw-chat-dock.is-open .diw-chat-panel {
    height: 480px;
    max-height: calc(100vh - 90px);
    box-shadow: var(--diw-chat-shadow);
    border: 1px solid #e8edf3;
    border-bottom: 0;
    margin-bottom: -1px;
    transform: translateY(0);
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

#diw-chat-dock .diw-chat-panel__body {
    flex: 1;
    min-height: 0;
    background: #f8fafc;
    display: flex;
    flex-direction: column;
}

#diw-chat-dock .diw-chat-panel__started {
    text-align: center;
    font-size: 12px;
    color: #94a3b8;
    padding: 10px 0 4px;
}

#diw-chat-dock .diw-chat-panel__messages {
    flex: 1;
    overflow-y: auto;
    padding: 8px 12px 14px;
}

#diw-chat-dock .diw-chat-panel__footer {
    padding: 10px 12px 12px;
    background: #fff;
    border-top: 1px solid #eef2f7;
    flex-shrink: 0;
}

#diw-chat-dock .diw-chat-panel__input-wrap {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    border: 1px solid #e2e8f0;
    border-radius: 10px;
    padding: 6px 8px 6px 12px;
    background: #fff;
}

#diw-chat-dock #txt_msg {
    flex: 1;
    border: 0;
    outline: none;
    resize: none;
    font-family: 'Sarabun', Arial, sans-serif;
    font-size: 14px;
    line-height: 1.4;
    min-height: 22px;
    max-height: 80px;
    background: transparent;
}

#diw-chat-dock .diw-chat-panel__send {
    border: 0;
    background: transparent;
    color: var(--diw-chat-accent);
    width: 32px;
    height: 32px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

#diw-chat-dock .diw-chat-panel__send:hover {
    background: var(--diw-chat-accent-soft);
}

/* แถบล่าง — ย่อ: เล็กสั้น | เปิด: กว้างเท่า panel */
#diw-chat-dock .diw-chat-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 240px;
    min-width: 200px;
    max-width: calc(100vw - 48px);
    min-height: 30px;
    background: #fff;
    border: 1px solid #e2e8f0;
    border-bottom: 0;
    border-radius: 10px 10px 0 0;
    box-shadow: var(--diw-chat-shadow);
    padding: 0 5px 0 7px;
    transition: width 0.22s ease, min-height 0.22s ease, padding 0.22s ease, border-radius 0.22s ease;
}

#diw-chat-dock.is-open .diw-chat-bar {
    width: 380px;
    min-width: 300px;
    min-height: 44px;
    padding: 0 8px 0 10px;
    border-radius: 0;
    box-shadow: none;
    border-top: 1px solid #e8edf3;
}

#diw-chat-dock .diw-chat-bar__main {
    display: flex;
    align-items: center;
    gap: 5px;
    border: 0;
    background: transparent;
    cursor: pointer;
    padding: 3px 2px;
    flex: 1;
    text-align: left;
    min-width: 0;
}

#diw-chat-dock.is-open .diw-chat-bar__main {
    gap: 10px;
    padding: 6px 4px;
}

#diw-chat-dock .diw-chat-bar__avatar {
    width: 20px;
    height: 20px;
    max-width: 20px;
    max-height: 20px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
    display: block;
}

#diw-chat-dock.is-open .diw-chat-bar__avatar {
    width: 28px;
    height: 28px;
    max-width: 28px;
    max-height: 28px;
}

#diw-chat-dock .diw-chat-bar__name {
    font-weight: 700;
    font-size: 11px;
    color: #1e293b;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

#diw-chat-dock.is-open .diw-chat-bar__name {
    font-size: 14px;
}

#diw-chat-dock .diw-chat-bar__controls {
    display: flex;
    align-items: center;
    gap: 2px;
    flex-shrink: 0;
    min-width: 50px;
    margin-left: 2px;
}

#diw-chat-dock.is-open .diw-chat-bar__controls {
    gap: 4px;
    min-width: 72px;
    margin-left: 8px;
}

#diw-chat-dock .diw-chat-bar__btn {
    border: 0;
    background: #f1f5f9;
    color: #475569;
    width: 24px;
    height: 24px;
    border-radius: 6px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    line-height: 1;
    font-weight: 700;
    flex-shrink: 0;
    padding: 0;
}

#diw-chat-dock.is-open .diw-chat-bar__btn {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    font-size: 18px;
}

#diw-chat-dock .diw-chat-bar__btn span {
    display: block;
    pointer-events: none;
}

#diw-chat-dock .diw-chat-bar__btn--toggle .diw-chat-bar__icon--plus {
    display: block;
}

#diw-chat-dock .diw-chat-bar__btn--toggle .diw-chat-bar__icon--minus {
    display: none;
}

#diw-chat-dock.is-open .diw-chat-bar__btn--toggle .diw-chat-bar__icon--plus {
    display: none;
}

#diw-chat-dock.is-open .diw-chat-bar__btn--toggle .diw-chat-bar__icon--minus {
    display: block;
}

#diw-chat-dock .diw-chat-bar__btn:hover {
    background: #e2e8f0;
    color: #1e293b;
}

#diw-chat-dock .diw-chat-bar__btn--close:hover {
    background: #fee2e2;
    color: #b91c1c;
}

#diw-chat-dock .diw-chat-panel__send span {
    display: block;
    font-size: 16px;
    line-height: 1;
    pointer-events: none;
}

/* FAQ hybrid panel */
#diw-chat-dock .diw-faq-loading,
#kt_chat_modal .diw-faq-loading {
    text-align: center;
    color: #94a3b8;
    font-size: 13px;
    padding: 16px 8px;
    font-family: 'Sarabun', Arial, sans-serif;
}

#diw-chat-dock .diw-faq-section-title,
#kt_chat_modal .diw-faq-section-title {
    font-family: 'Sarabun', Arial, sans-serif;
    font-size: 12px;
    font-weight: 700;
    color: #64748b;
    margin: 4px 0 8px;
    text-transform: uppercase;
    letter-spacing: 0.02em;
}

#diw-chat-dock .diw-faq-section-title--history,
#kt_chat_modal .diw-faq-section-title--history {
    margin-top: 14px;
    padding-top: 10px;
    border-top: 1px dashed #e2e8f0;
}

#diw-chat-dock .diw-faq-history,
#kt_chat_modal .diw-faq-history {
    opacity: 0.92;
}

#diw-chat-dock .diw-msg--history,
#kt_chat_modal .diw-msg--history {
    pointer-events: none;
}

#kt_chat_modal .symbol.symbol-40 img {
    width: 40px !important;
    height: 40px !important;
    max-width: 40px !important;
    max-height: 40px !important;
    object-fit: cover;
    border-radius: 50%;
}

/* FAQ quick-replies (docked + home modal) */
#diw-chat-dock .diw-faq-list,
#kt_chat_modal .diw-faq-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 4px 0 8px;
}

#diw-chat-dock .diw-faq-btn,
#kt_chat_modal .diw-faq-btn {
    display: block;
    width: 100%;
    text-align: left;
    padding: 10px 12px;
    border: 1px solid #c7ddf3;
    border-radius: 10px;
    background: #fff;
    color: #1e293b;
    font-family: 'Sarabun', Arial, sans-serif;
    font-size: 13px;
    line-height: 1.45;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}

#diw-chat-dock .diw-faq-btn:hover,
#kt_chat_modal .diw-faq-btn:hover {
    background: var(--diw-chat-accent-soft);
    border-color: var(--diw-chat-accent);
}

#diw-chat-dock .diw-chat-back,
#kt_chat_modal .diw-chat-back {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    margin-bottom: 10px;
    padding: 6px 10px;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    background: #fff;
    color: #475569;
    font-family: 'Sarabun', Arial, sans-serif;
    font-size: 12px;
    cursor: pointer;
}

#diw-chat-dock .diw-chat-back:hover,
#kt_chat_modal .diw-chat-back:hover {
    background: #f8fafc;
    color: #1e293b;
}

/* Message bubbles */
#diw-chat-dock .diw-msg {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    margin-bottom: 12px;
    max-width: 100%;
}

#diw-chat-dock .diw-msg--me {
    flex-direction: row;
    justify-content: flex-end;
}

#diw-chat-dock .diw-msg__avatar {
    width: 28px;
    height: 28px;
    min-width: 28px;
    min-height: 28px;
    max-width: 28px;
    max-height: 28px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
    border: 1px solid #e2e8f0;
    background: #fff;
}

#diw-chat-dock .diw-msg__body {
    display: flex;
    flex-direction: column;
    max-width: calc(100% - 40px);
    min-width: 0;
}

#diw-chat-dock .diw-msg--me .diw-msg__body {
    align-items: flex-end;
}

#diw-chat-dock .diw-msg__meta {
    display: flex;
    align-items: center;
    gap: 5px;
    margin-bottom: 4px;
    font-size: 11px;
    line-height: 1.2;
    color: #64748b;
}

#diw-chat-dock .diw-msg--me .diw-msg__meta {
    flex-direction: row-reverse;
}

#diw-chat-dock .diw-msg__name {
    font-weight: 600;
    color: #334155;
}

#diw-chat-dock .diw-msg__time {
    color: #94a3b8;
    font-size: 10px;
}

#diw-chat-dock .diw-msg__bubble {
    display: inline-block;
    max-width: 100%;
    padding: 10px 12px;
    border-radius: 12px;
    font-size: 14px;
    line-height: 1.5;
    word-break: break-word;
    white-space: pre-wrap;
    border: 1px solid transparent;
    box-shadow: 0 1px 2px rgba(15, 23, 42, 0.06);
}

#diw-chat-dock .diw-msg--bot .diw-msg__bubble {
    background: #fff;
    border-color: #d8e6df;
    color: #1e293b;
    border-top-left-radius: 4px;
}

#diw-chat-dock .diw-msg--me .diw-msg__bubble {
    background: #e8f4fd;
    border-color: #b9daf3;
    color: #0f172a;
    border-top-right-radius: 4px;
    text-align: left;
}

@media (max-width: 767.98px) {
    #diw-chat-dock {
        right: 0;
        left: 0;
        align-items: stretch;
    }

    #diw-chat-dock.is-open .diw-chat-panel,
    #diw-chat-dock.is-open .diw-chat-bar {
        width: 100%;
        max-width: 100%;
        border-radius: 12px 12px 0 0;
    }
}

/* ไม่ให้แชทติดไปตอนพิมพ์รายงาน */
@media print {
    #diw-chat-dock,
    #diw-chat-restore,
    .diw-chat-restore,
    #chatbot,
    #kt_chat_modal {
        display: none !important;
        visibility: hidden !important;
    }
}
