/* TFAC Chatbot Widget */

.chatbot-root {
    position: fixed;
    right: calc(20px + env(safe-area-inset-right, 0px));
    bottom: calc(20px + env(safe-area-inset-bottom, 0px));
    z-index: 90;
    font-family: 'Sarabun', 'Noto Sans Thai', system-ui, sans-serif;

    /* Fluid typography custom properties */
    --chatbot-font-msg: clamp(0.8125rem, 0.75rem + 0.227vw, 0.9375rem);
    --chatbot-font-card-title: clamp(0.875rem, 0.8rem + 0.227vw, 1rem);
    --chatbot-font-card-meta: clamp(0.6875rem, 0.625rem + 0.227vw, 0.8125rem);

    /* Safe area inset custom properties */
    --chatbot-safe-top: env(safe-area-inset-top, 0px);
    --chatbot-safe-bottom: env(safe-area-inset-bottom, 0px);
    --chatbot-safe-left: env(safe-area-inset-left, 0px);
    --chatbot-safe-right: env(safe-area-inset-right, 0px);
}

.chatbot-root--expanded,
.chatbot-root--course-modal {
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: 120;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.chatbot-root--expanded::before,
.chatbot-root--course-modal::before {
    content: "";
    position: absolute;
    inset: 0;
    background: rgba(15, 23, 42, 0.48);
    opacity: 1;
    animation: chatbotBackdropIn 0.25s ease;
}

.chatbot-launcher {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 12px 20px;
    min-width: 48px;
    min-height: 48px;
    background: linear-gradient(135deg, #DD030E 0%, #B91C1C 100%);
    color: #fff;
    border: 0;
    border-radius: 999px;
    box-shadow: 0 12px 30px rgba(221, 3, 14, 0.3);
    cursor: pointer;
    font-size: 14px;
    font-weight: 700;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.chatbot-launcher:hover {
    transform: translateY(-2px);
    box-shadow: 0 18px 36px rgba(221, 3, 14, 0.38);
}

.chatbot-launcher__label {
    line-height: 1;
}

.chatbot-panel {
    position: absolute;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 100vh;
    height: 100dvh;
    background: #fff;
    border-radius: 0;
    box-shadow: 0 30px 60px rgba(15, 23, 42, 0.22), 0 6px 16px rgba(15, 23, 42, 0.08);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid #E5E5E5;
    transition: width 0.28s ease, height 0.28s ease, border-radius 0.28s ease, box-shadow 0.28s ease, transform 0.28s ease;
    animation: chatbotSlideUp 0.28s ease;
    padding-left: var(--chatbot-safe-left, 0px);
    padding-right: var(--chatbot-safe-right, 0px);
}

.chatbot-panel.chatbot-animating {
    will-change: transform;
}

.chatbot-root--expanded .chatbot-launcher,
.chatbot-root--course-modal .chatbot-launcher {
    display: none;
}

.chatbot-panel.chatbot-panel--expanded,
.chatbot-panel.chatbot-panel--course-modal {
    position: relative;
    right: auto;
    bottom: auto;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    border-radius: 0;
    box-shadow: 0 34px 90px rgba(15, 23, 42, 0.34), 0 12px 36px rgba(15, 23, 42, 0.14);
    pointer-events: auto;
    transform-origin: center center;
    animation: none;
}

.chatbot-panel.chatbot-panel--expanded .chatbot-msg,
.chatbot-panel.chatbot-panel--course-modal .chatbot-msg {
    max-width: 95%;
}

.chatbot-panel.chatbot-panel--expanded .chatbot-panel__messages,
.chatbot-panel.chatbot-panel--course-modal .chatbot-panel__messages {
    padding: 24px;
    gap: 16px;
    background: linear-gradient(180deg, #FAFAFA 0%, #FFFFFF 100%);
}

/* 2-column grid for expanded mode is now handled via breakpoints (min-width: 901px) */

.chatbot-panel.hidden {
    display: none;
}

.chatbot-panel__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: calc(14px + var(--chatbot-safe-top, 0px)) 16px 14px;
    background: linear-gradient(135deg, #FEF2F2, #FFFFFF);
    border-bottom: 1px solid #FEE2E2;
}

.chatbot-panel__brand {
    display: flex;
    align-items: center;
    gap: 10px;
}

.chatbot-panel__avatar {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: #DD030E;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
}

.chatbot-panel__title {
    margin: 0;
    font-size: 14px;
    font-weight: 700;
    color: #171717;
}

.chatbot-panel__subtitle {
    margin: 0;
    font-size: 11px;
    color: #737373;
}

.chatbot-panel__close,
.chatbot-panel__action {
    background: transparent;
    border: 0;
    color: #737373;
    cursor: pointer;
    padding: 4px;
    border-radius: 8px;
    transition: background 0.2s, color 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
}

.chatbot-panel__close:hover {
    background: #FEE2E2;
    color: #B91C1C;
}

.chatbot-panel__action:hover {
    background: #F3F4F6;
    color: #171717;
}

.chatbot-panel__actions {
    display: flex;
    align-items: center;
    gap: 4px;
}

.chatbot-panel__messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: #FAFAFA;
}

.chatbot-panel.chatbot-panel--course-modal .chatbot-panel__messages {
    padding: 20px;
    gap: 16px;
    background: linear-gradient(180deg, #FAFAFA 0%, #FFFFFF 100%);
}

.chatbot-panel.chatbot-panel--course-modal .chatbot-msg {
    max-width: 95%;
}

.chatbot-msg {
    max-width: 85%;
    padding: 10px 14px;
    border-radius: 14px;
    font-size: 13px;
    font-size: var(--chatbot-font-msg);
    line-height: 1.55;
    white-space: pre-wrap;
    word-break: break-word;
}

.chatbot-msg--bot {
    align-self: flex-start;
    background: #FFFFFF;
    border: 1px solid #E5E5E5;
    color: #262626;
    border-bottom-left-radius: 4px;
}

.chatbot-msg--user {
    align-self: flex-end;
    background: #DD030E;
    color: #fff;
    border-bottom-right-radius: 4px;
}

.chatbot-msg--error {
    background: #FEE2E2;
    border: 1px solid #FCA5A5;
    color: #B91C1C;
}

.chatbot-cards {
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-self: stretch;
}

.chatbot-cards--course {
    display: grid;
    grid-template-columns: 1fr;
    gap: 14px;
}

.chatbot-card {
    background: #fff;
    border: 1px solid #E5E5E5;
    border-radius: 14px;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    box-shadow: 0 4px 12px rgba(15, 23, 42, 0.06);
}

.chatbot-card__title {
    font-weight: 700;
    color: #171717;
    font-size: 13px;
    line-height: 1.4;
}

.chatbot-card__meta {
    font-size: 11px;
    font-size: var(--chatbot-font-card-meta);
    color: #525252;
    line-height: 1.5;
}

.chatbot-card__price {
    font-size: 11px;
    color: #DD030E;
    font-weight: 700;
}

.chatbot-card__actions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 4px;
}

.chatbot-action {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 10px 14px;
    min-height: 44px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 600;
    cursor: pointer;
    border: 1px solid #E5E5E5;
    background: #FFFFFF;
    color: #525252;
    text-decoration: none;
    transition: background 0.2s, color 0.2s, border-color 0.2s;
}

.chatbot-action:hover {
    background: #FEE2E2;
    color: #B91C1C;
    border-color: #FCA5A5;
}

.chatbot-action--primary {
    background: #DD030E;
    color: #fff;
    border-color: #DD030E;
}

.chatbot-action--primary:hover {
    background: #B91C1C;
    color: #fff;
    border-color: #B91C1C;
}

.chatbot-course-card {
    display: grid;
    grid-template-columns: 1fr;
    background: #FFFFFF;
    border: 1px solid rgba(229, 229, 229, 0.9);
    border-radius: 18px;
    overflow: hidden;
    box-shadow: 0 14px 30px rgba(15, 23, 42, 0.08);
}

.chatbot-course-card__media {
    position: relative;
    background: #F5F5F5;
    overflow: hidden;
    aspect-ratio: 16 / 9;
}

.chatbot-course-card__media img {
    width: 100%;
    height: 100%;
    min-height: 0;
    max-height: 180px;
    display: block;
    object-fit: cover;
    object-position: center;
}

.chatbot-course-card__fallback {
    width: 100%;
    height: 100%;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    background: linear-gradient(135deg, #FEE2E2, #FFFFFF);
    color: #B91C1C;
    font-weight: 800;
    text-align: center;
}

.chatbot-course-card__body {
    min-width: 0;
    padding: 14px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.chatbot-course-card__top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.chatbot-course-card__code,
.chatbot-course-card__status {
    display: inline-flex;
    align-items: center;
    min-height: 24px;
    padding: 3px 8px;
    border-radius: 999px;
    font-size: 10px;
    font-weight: 800;
    line-height: 1.2;
}

.chatbot-course-card__code {
    background: #F5F5F5;
    color: #525252;
}

.chatbot-course-card__status {
    background: #DCFCE7;
    color: #166534;
}

.chatbot-course-card__status--full,
.chatbot-course-card__status--stop {
    background: #FEF3C7;
    color: #92400E;
}

.chatbot-course-card__title {
    margin: 0;
    color: #171717;
    font-size: 14px;
    font-size: var(--chatbot-font-card-title);
    font-weight: 800;
    line-height: 1.38;
}

.chatbot-course-card__desc {
    margin: 0;
    color: #525252;
    font-size: 11px;
    font-size: var(--chatbot-font-card-meta);
    line-height: 1.55;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.chatbot-course-card__facts {
    display: grid;
    gap: 6px;
}

.chatbot-course-card__fact {
    display: grid;
    grid-template-columns: 18px minmax(0, 1fr);
    align-items: start;
    gap: 6px;
    color: #525252;
    font-size: 11px;
    font-size: var(--chatbot-font-card-meta);
    line-height: 1.45;
}

.chatbot-course-card__fact iconify-icon {
    color: #DD030E;
    font-size: 16px;
    margin-top: 1px;
}

.chatbot-course-card__price {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: auto;
    color: #525252;
    font-size: 11px;
}

.chatbot-course-card__price span {
    padding: 6px 8px;
    border-radius: 10px;
    background: #FEF2F2;
}

.chatbot-course-card__price strong {
    color: #DD030E;
    font-size: 14px;
}

.chatbot-typing {
    align-self: flex-start;
    display: inline-flex;
    gap: 4px;
    padding: 12px 14px;
    background: #FFFFFF;
    border: 1px solid #E5E5E5;
    border-radius: 14px;
    border-bottom-left-radius: 4px;
}

.chatbot-typing span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #DD030E;
    opacity: 0.4;
    animation: chatbotDot 1.2s infinite ease-in-out;
}

.chatbot-typing span:nth-child(2) { animation-delay: 0.15s; }
.chatbot-typing span:nth-child(3) { animation-delay: 0.3s; }

@keyframes chatbotDot {
    0%, 80%, 100% { opacity: 0.3; transform: scale(0.85); }
    40% { opacity: 1; transform: scale(1); }
}

@keyframes chatbotBackdropIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes chatbotModalIn {
    from { transform: translateY(14px) scale(0.96); opacity: 0.72; }
    to { transform: translateY(0) scale(1); opacity: 1; }
}

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

.chatbot-panel__quick {
    display: flex;
    flex-wrap: nowrap;
    gap: 6px;
    padding: 10px 16px 0;
    background: #FFFFFF;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none; /* Firefox */
}

.chatbot-panel__quick::-webkit-scrollbar {
    display: none; /* Chrome/Safari */
}

.chatbot-quick {
    padding: 6px 10px;
    min-height: 44px;
    border-radius: 999px;
    border: 1px solid #FCA5A5;
    background: #FEF2F2;
    color: #B91C1C;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
    white-space: nowrap;
    flex-shrink: 0;
}

.chatbot-quick:hover {
    background: #DD030E;
    color: #fff;
    border-color: #DD030E;
}

.chatbot-panel__form {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px calc(14px + var(--chatbot-safe-bottom, 0px));
    background: #FFFFFF;
    border-top: 1px solid #F5F5F5;
}

.chatbot-panel__input {
    flex: 1;
    border: 1px solid #E5E5E5;
    border-radius: 12px;
    padding: 10px 12px;
    font-size: 16px;
    color: #171717;
    background: #FAFAFA;
    outline: none;
    transition: border 0.2s;
}

.chatbot-panel__input:focus {
    border-color: #DD030E;
    background: #fff;
}

.chatbot-panel__send {
    width: 44px;
    height: 44px;
    min-width: 44px;
    min-height: 44px;
    border-radius: 12px;
    border: 0;
    background: #DD030E;
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.chatbot-panel__send:hover { background: #B91C1C; }
.chatbot-panel__send:disabled { opacity: 0.5; cursor: not-allowed; }

/* === RESPONSIVE BREAKPOINT SYSTEM (mobile-first) === */

/* Base (≤480px): full-screen panel — already set in .chatbot-panel base styles */

/* Launcher label: hidden at base, shown from 481px */
.chatbot-launcher__label { display: none; }
.chatbot-launcher { padding: 14px; }

@media (min-width: 481px) {
    .chatbot-launcher__label { display: inline; }
    .chatbot-launcher { padding: 12px 20px; }

    .chatbot-panel {
        width: min(420px, calc(100vw - 24px));
        height: min(560px, calc(100vh - 110px));
        border-radius: 18px;
        bottom: 64px;
        animation: none;
    }

    .chatbot-panel__quick {
        flex-wrap: wrap;
        overflow-x: visible;
    }

    .chatbot-quick {
        white-space: normal;
        flex-shrink: 1;
    }

    /* Expanded mode: tablet-sized overlay with backdrop blur */
    .chatbot-root--expanded,
    .chatbot-root--course-modal {
        padding: 12px;
    }

    .chatbot-root--expanded::before,
    .chatbot-root--course-modal::before {
        backdrop-filter: blur(6px);
    }

    .chatbot-panel.chatbot-panel--expanded,
    .chatbot-panel.chatbot-panel--course-modal {
        width: calc(100vw - 24px);
        height: calc(100vh - 24px);
        border-radius: 18px;
        animation: chatbotModalIn 0.28s ease;
    }
}

@media (min-width: 641px) {
    .chatbot-panel {
        width: min(460px, calc(100vw - 32px));
    }

    /* Scale down input font-size for non-mobile viewports */
    .chatbot-panel__input {
        font-size: 13px;
    }

    /* Restore quick action font-size for non-mobile viewports */
    .chatbot-quick {
        font-size: 11px;
    }

    /* Reset touch target overrides for desktop-class viewports */
    .chatbot-panel__close,
    .chatbot-panel__action {
        min-width: auto;
        min-height: auto;
    }

    .chatbot-quick {
        min-height: auto;
    }

    .chatbot-action {
        min-height: auto;
        padding: 6px 10px;
    }

    /* Course card: horizontal layout (image left, content right) */
    .chatbot-course-card {
        grid-template-columns: 140px minmax(0, 1fr);
    }

    .chatbot-course-card__media {
        aspect-ratio: auto;
    }

    .chatbot-course-card__media img {
        min-height: 230px;
        max-height: none;
    }

    .chatbot-course-card__fallback {
        min-height: 230px;
    }

    /* Expanded mode message width at tablet+ */
    .chatbot-panel.chatbot-panel--expanded .chatbot-msg,
    .chatbot-panel.chatbot-panel--course-modal .chatbot-msg {
        max-width: min(720px, 92%);
    }
}

@media (min-width: 901px) {
    .chatbot-panel {
        width: 380px;
    }

    /* Restore 2-column grid in expanded mode */
    .chatbot-panel.chatbot-panel--expanded .chatbot-cards--course,
    .chatbot-panel.chatbot-panel--course-modal .chatbot-cards--course {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    /* Restore full-size card layout */
    .chatbot-course-card {
        grid-template-columns: 190px minmax(0, 1fr);
    }

    .chatbot-course-card__media img {
        min-height: 230px;
    }

    /* Expanded mode: desktop-sized centered modal */
    .chatbot-root--expanded,
    .chatbot-root--course-modal {
        padding: 24px;
    }

    .chatbot-panel.chatbot-panel--expanded,
    .chatbot-panel.chatbot-panel--course-modal {
        width: min(1080px, calc(100vw - 32px));
        height: min(760px, calc(100vh - 32px));
        border-radius: 22px;
    }
}

@media (min-width: 1201px) {
    .chatbot-panel {
        width: 380px;
    }
}

/* === LANDSCAPE ORIENTATION HANDLING === */

@media (orientation: landscape) and (max-height: 500px) {
    .chatbot-panel__header {
        padding: 8px 12px;
    }

    .chatbot-panel__quick {
        display: none;
    }

    .chatbot-panel__form {
        padding: 8px 12px;
    }
}

@media (orientation: landscape) and (max-width: 900px) {
    .chatbot-panel {
        height: 100vh;
        height: 100dvh;
    }
}

@media (max-height: 300px) {
    .chatbot-panel__header {
        display: none;
    }

    .chatbot-panel__quick {
        display: none;
    }
}
