/* --------------------------------------------------------------
   หน้าแชท peopleChat (body.chat-page)

   เลย์เอาต์ของจอ (ทุกขนาด):
   แถบหัวเรื่องพร้อมโลโก้ -> การ์ดแชท (พื้นที่ข้อความ) -> ช่องพิมพ์

   โทนสีอิงแบรนด์ OAG: ส้ม (#c14a05) สำหรับปุ่ม/เครื่องหมาย,
   กรมท่า (#16233f) สำหรับข้อความ, ครีมอ่อนสำหรับฟองข้อความบอท

   หมายเหตุ: ไฟล์นี้โหลดหลัง styles.css และครอบคลุมสไตล์ของหน้าแชททั้งหมด
   เพื่อไม่ให้สไตล์หน้าแชทปะปนกับ styles.css ที่ใช้ร่วมกับหน้า home
   -------------------------------------------------------------- */

body.chat-page {
    --chat-ink: #17233d;
    --chat-ink-soft: #5b6577;
    --chat-brand: #e8750c;
    --chat-brand-deep: #c14a05;
    --chat-navy: #16233f;
    --chat-line: #efe4d6;

    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: clamp(8px, 1.4vh, 14px);
    box-sizing: border-box;
    width: 100%;
    height: 100vh;
    padding: clamp(10px, 2vh, 20px);
    padding-top: max(clamp(10px, 2vh, 20px), env(safe-area-inset-top));
    padding-bottom: max(clamp(10px, 2vh, 20px), env(safe-area-inset-bottom));
    overflow: hidden;
    color: var(--chat-ink);
    font-family: "Sarabun", "Noto Sans Thai", "Leelawadee UI", "Segoe UI", Tahoma, sans-serif;
    background-color: #f6f1e8;
    background-image:
        radial-gradient(900px 520px at 92% -12%, rgba(232, 117, 12, 0.20), transparent 62%),
        radial-gradient(760px 560px at -8% 112%, rgba(22, 35, 63, 0.16), transparent 60%),
        linear-gradient(155deg, #fdfcf9 0%, #f7f2ea 55%, #f2ede4 100%);
}

/* --------------------------------------------------------------
   แถบหัวเรื่อง: โลโก้สำนักงาน กึ่งกลางกับการ์ดด้านล่าง
   -------------------------------------------------------------- */
body.chat-page .chat-header {
    flex: 0 0 auto;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    width: 100%;
    max-width: 840px;
    padding-left: 6px;
}

body.chat-page .chat-header-link {
    display: inline-flex;
    align-items: center;
    line-height: 0;
    border-radius: 12px;
}

body.chat-page .chat-header-logo {
    width: auto;
    height: clamp(38px, 5.5vh, 52px);
}

body.chat-page .chat-header-link:focus-visible {
    outline: 3px solid rgba(232, 117, 12, 0.45);
    outline-offset: 4px;
}

/* --------------------------------------------------------------
   การ์ดแชท: ครอบพื้นที่ข้อความทั้งหมด
   -------------------------------------------------------------- */
body.chat-page #chat-container {
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
    max-width: 840px;
    display: flex;
    flex-direction: column;
    position: relative;
    margin: 0;
    padding-bottom: 0;
    overflow: hidden;
    background-color: #ffffff;
    border: 1px solid rgba(255, 255, 255, 0.9);
    border-radius: 24px;
    box-shadow:
        0 30px 60px -30px rgba(15, 24, 45, 0.45),
        0 10px 24px -16px rgba(15, 24, 45, 0.22);
}

/* --------------------------------------------------------------
   พื้นที่ข้อความ
   -------------------------------------------------------------- */
body.chat-page #messages {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 14px;
    padding: clamp(14px, 2.4vh, 24px);
    overflow-y: auto;
    font-size: clamp(15px, 2.1vh, 17px);
    line-height: 1.6;
    background-image: radial-gradient(rgba(22, 35, 63, 0.05) 1px, transparent 1px);
    background-size: 22px 22px;
    scrollbar-width: thin;
    scrollbar-color: #e6dccb transparent;
}

body.chat-page #messages::-webkit-scrollbar {
    width: 10px;
}

body.chat-page #messages::-webkit-scrollbar-track {
    background: transparent;
}

body.chat-page #messages::-webkit-scrollbar-thumb {
    background-color: #e6dccb;
    background-clip: padding-box;
    border: 3px solid transparent;
    border-radius: 999px;
}

/* --------------------------------------------------------------
   แถวข้อความ
   -------------------------------------------------------------- */
body.chat-page .bot-message-container,
body.chat-page .user-message-container {
    display: flex;
    margin: 0;
    animation: chat-message-in 0.28s ease both;
}

body.chat-page .bot-message-container {
    align-items: flex-start;
    justify-content: flex-start;
    gap: 10px;
}

body.chat-page .user-message-container {
    align-items: center;
    justify-content: flex-end;
}

@keyframes chat-message-in {
    from {
        opacity: 0;
        transform: translateY(8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* รูปโปรไฟล์บอท */
body.chat-page .bot-icon {
    flex: 0 0 auto;
    display: block;
    width: 40px;
    height: 40px;
    margin: 0;
    object-fit: cover;
    object-position: center;
    background-color: #fff;
    border: 3px solid #f89718;
    border-radius: 50%;
    box-shadow: 0 6px 14px -8px rgba(232, 117, 12, 0.85);
}

/* --------------------------------------------------------------
   ฟองข้อความ
   -------------------------------------------------------------- */
body.chat-page .user-message,
body.chat-page .bot-message {
    max-width: 76%;
    padding: 11px 15px;
    border-radius: 18px;
    display: inline-block;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}

body.chat-page .user-message {
    background-image: linear-gradient(135deg, #c9500a 0%, #a53d04 100%);
    color: #ffffff;
    align-self: flex-end;
    text-align: left;
    border-bottom-right-radius: 6px;
    box-shadow: 0 12px 24px -16px rgba(165, 61, 4, 0.95);
}

body.chat-page .bot-message {
    background-color: #fffaf4;
    color: #2b3446;
    border: 1px solid #f2e6d8;
    border-bottom-left-radius: 6px;
    max-width: 76%;
    margin-top: 0;
    box-shadow: 0 10px 22px -18px rgba(15, 24, 45, 0.55);
}

/* ใช้ padding ของกล่องคุมช่องไฟรอบคำตอบ ไม่บวกระยะขอบย่อหน้าซ้ำ */
body.chat-page .bot-message p {
    margin: 0 0 8px;
}

body.chat-page .bot-message a {
    color: var(--chat-brand-deep);
    text-decoration: underline;
    overflow-wrap: anywhere;
}

body.chat-page .bot-message > :first-child {
    margin-top: 0;
}

body.chat-page .bot-message > :last-child {
    margin-bottom: 0;
}

/* องค์ประกอบจากคำตอบที่แปลงจาก Markdown */
body.chat-page .bot-message ul,
body.chat-page .bot-message ol {
    margin: 6px 0;
    padding-left: 22px;
}

body.chat-page .bot-message li {
    margin: 2px 0;
}

body.chat-page .bot-message blockquote {
    margin: 8px 0;
    padding: 4px 12px;
    color: var(--chat-ink-soft);
    background: #fff4e8;
    border-left: 3px solid var(--chat-brand);
    border-radius: 0 10px 10px 0;
}

body.chat-page .bot-message code {
    padding: 1px 6px;
    font-size: 0.92em;
    background: rgba(22, 35, 63, 0.07);
    border-radius: 6px;
}

body.chat-page .bot-message pre {
    margin: 8px 0;
    padding: 10px 12px;
    overflow-x: auto;
    color: #f4efe7;
    background: var(--chat-navy);
    border-radius: 10px;
}

body.chat-page .bot-message pre code {
    padding: 0;
    background: transparent;
}

body.chat-page .bot-message table {
    margin: 8px 0;
    border-collapse: collapse;
}

body.chat-page .bot-message th,
body.chat-page .bot-message td {
    padding: 6px 10px;
    border: 1px solid var(--chat-line);
}

body.chat-page .bot-message th {
    background: #fff4e8;
    color: var(--chat-brand-deep);
}

/* ก้อนรวมคำตอบ/ปุ่มประเมินใต้ฟองข้อความ */
body.chat-page .bot-reply {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    max-width: 76%;
    min-width: 0;
}

body.chat-page .bot-reply .bot-message {
    box-sizing: border-box;
    max-width: 100%;
}

/* --------------------------------------------------------------
   สถานะ: จุดไข่ปลายัง กับแถบสถานะระหว่างรอคำตอบ
   -------------------------------------------------------------- */
body.chat-page .typing-indicator {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 9px 13px;
    height: auto;
    background: #fff4e8;
    border: 1px solid #f7dcc2;
    border-radius: 16px;
    border-bottom-left-radius: 6px;
}

body.chat-page .typing-indicator .dot {
    width: 7px;
    height: 7px;
    background-color: var(--chat-brand);
    border-radius: 50%;
    animation: typingBounce 1.4s infinite ease-in-out both;
}

body.chat-page .typing-indicator .dot:nth-child(1) {
    animation-delay: -0.32s;
}

body.chat-page .typing-indicator .dot:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes typingBounce {
    0%, 80%, 100% {
        transform: scale(0);
    }
    40% {
        transform: scale(1.2);
    }
}

body.chat-page .chat-status {
    display: inline-block;
    margin-top: 6px;
    padding: 6px 12px;
    font-size: 13px;
    color: #8a5a1f;
    background-color: #fff4e8;
    border: 1px solid #f7dcc2;
    border-radius: 999px;
    animation: chat-status-pulse 1.4s ease-in-out infinite;
}

@keyframes chat-status-pulse {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.45;
    }
}

/* --------------------------------------------------------------
   ปุ่มประเมินคำตอบ
   -------------------------------------------------------------- */
body.chat-page .chat-feedback {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
    margin-top: 8px;
    color: var(--chat-ink-soft);
    font-size: 13px;
}

body.chat-page .chat-feedback-button {
    padding: 6px 12px;
    font-size: 13px;
    color: #33404f;
    background: #ffffff;
    border: 1px solid var(--chat-line);
    border-radius: 999px;
    cursor: pointer;
    transition: border-color 0.15s ease, background-color 0.15s ease, color 0.15s ease;
}

body.chat-page .chat-feedback-button:hover,
body.chat-page .chat-feedback-button:focus-visible {
    color: var(--chat-brand-deep);
    background: #fff4e8;
    border-color: var(--chat-brand);
}

body.chat-page .chat-feedback-button.selected {
    color: var(--chat-brand-deep);
    background: #ffe9d4;
    border-color: var(--chat-brand);
}

body.chat-page .chat-feedback-button:disabled {
    cursor: default;
    opacity: 0.65;
}

body.chat-page .chat-feedback-status {
    flex-basis: 100%;
    min-height: 1em;
    color: var(--chat-brand-deep);
}

body.chat-page .chat-feedback-form {
    display: flex;
    flex-direction: column;
    gap: 10px;
    flex-basis: 100%;
    min-width: 0;
    padding: 12px;
    background: #fffdf9;
    border: 1px solid var(--chat-line);
    border-radius: 14px;
}

body.chat-page .chat-feedback-form[hidden] {
    display: none;
}

body.chat-page .chat-feedback-form fieldset {
    min-width: 0;
    margin: 0;
    padding: 0;
    border: 0;
}

body.chat-page .chat-feedback-form legend {
    margin-bottom: 8px;
    color: var(--chat-ink);
}

body.chat-page .chat-feedback-reasons,
body.chat-page .chat-feedback-reasons label {
    display: flex;
    gap: 8px;
}

body.chat-page .chat-feedback-reasons {
    flex-direction: column;
}

body.chat-page .chat-feedback-reasons label {
    align-items: flex-start;
    cursor: pointer;
}

body.chat-page .chat-feedback-form textarea {
    box-sizing: border-box;
    width: 100%;
    min-height: 80px;
    padding: 8px;
    font: inherit;
    color: var(--chat-ink);
    background: #ffffff;
    border: 1px solid var(--chat-line);
    border-radius: 10px;
    resize: vertical;
}

body.chat-page .chat-feedback-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

body.chat-page .office-page-button {
    margin-top: 10px;
}

/* --------------------------------------------------------------
   แถบปุ่มล้างแชท — ซ่อนไว้จนกว่าระบบจะแจ้งว่าโมเดลตอบไม่ไหวแล้ว
   -------------------------------------------------------------- */
body.chat-page #clear-chat-bar {
    display: none;
    justify-content: flex-end;
    padding: 8px 12px;
    background-color: #fff8e6;
    border-bottom: 1px solid #f0d28a;
    flex-shrink: 0;
}

body.chat-page #clear-chat-bar.show {
    display: flex;
}

body.chat-page #clear-chat-button {
    padding: 5px 12px;
    font-size: 13px;
    font-family: inherit;
    color: #7a4a12;
    background-color: #ffffff;
    border: 1px solid #f0d28a;
    border-radius: 999px;
    cursor: pointer;
    white-space: nowrap;
    transition: border-color 0.15s ease, color 0.15s ease;
}

body.chat-page #clear-chat-button:hover {
    color: var(--chat-brand-deep);
    border-color: var(--chat-brand);
}

body.chat-page #clear-chat-button:disabled {
    cursor: default;
    opacity: 0.6;
}

body.chat-page #clear-chat-button.highlight {
    color: #dc3545;
    border-color: #dc3545;
    animation: clear-chat-pulse 1s ease-in-out infinite;
}

@keyframes clear-chat-pulse {
    0%, 100% {
        box-shadow: 0 0 0 0 rgba(220, 53, 69, 0.4);
    }
    50% {
        box-shadow: 0 0 0 5px rgba(220, 53, 69, 0);
    }
}

/* --------------------------------------------------------------
   ช่องพิมพ์กับปุ่มส่ง
   -------------------------------------------------------------- */
body.chat-page #input-container {
    flex: 0 0 auto;
    display: flex;
    align-items: flex-end;
    gap: 10px;
    box-sizing: border-box;
    width: 100%;
    max-width: 840px;
    padding: clamp(8px, 1.4vh, 12px);
    background-color: #ffffff;
    border: 1px solid rgba(255, 255, 255, 0.9);
    border-radius: 20px;
    box-shadow: 0 24px 48px -32px rgba(15, 24, 45, 0.55);
}

body.chat-page #message {
    flex: 1 1 auto;
    min-width: 0;
    box-sizing: border-box;
    width: 100%;
    margin: 0;
    padding: 12px 16px;
    font-family: inherit;
    font-size: clamp(15px, 2.1vh, 17px);
    line-height: 1.5;
    color: var(--chat-ink);
    background-color: #fffdf9;
    border: 1px solid #e9dfd1;
    border-radius: 14px;
    resize: none;
    min-height: 44px;
    max-height: 120px;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

body.chat-page #message::placeholder {
    color: #9aa0a8;
}

body.chat-page #message:focus {
    outline: none;
    background-color: #ffffff;
    border-color: #f3b26a;
    box-shadow: 0 0 0 4px rgba(232, 117, 12, 0.16);
}

body.chat-page #message:disabled {
    background-color: #f4f1ec;
    cursor: not-allowed;
    opacity: 0.8;
}

body.chat-page #send-button {
    flex: 0 0 auto;
    min-width: 76px;
    padding: 12px 22px;
    font-family: inherit;
    font-size: clamp(15px, 2.1vh, 17px);
    font-weight: 600;
    color: #ffffff;
    background-image: linear-gradient(135deg, #c9500a 0%, #a53d04 100%);
    border: none;
    border-radius: 14px;
    box-shadow: 0 14px 26px -16px rgba(165, 61, 4, 0.95);
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.15s ease, filter 0.15s ease;
}

body.chat-page #send-button:hover {
    filter: brightness(1.06);
    transform: translateY(-1px);
}

body.chat-page #send-button:active {
    transform: translateY(0);
}

body.chat-page #send-button:disabled {
    color: #7a736a;
    background-image: none;
    background-color: #ddd6cc;
    box-shadow: none;
    cursor: not-allowed;
    transform: none;
    filter: none;
}

body.chat-page #send-button:focus-visible {
    outline: 3px solid rgba(232, 117, 12, 0.45);
    outline-offset: 3px;
}

/* --------------------------------------------------------------
   จอเตี้ย (มือถือแนวนอน / แล็ปท็อปความสูงน้อย): ย่อระยะห่างและโลโก้
   -------------------------------------------------------------- */
@media only screen and (max-height: 560px) {
    body.chat-page {
        gap: 8px;
        padding: 8px;
    }

    body.chat-page .chat-header-logo {
        height: 30px;
    }

    body.chat-page #messages {
        gap: 10px;
        padding: 12px;
    }

    body.chat-page #input-container {
        padding: 8px;
    }

    body.chat-page #message {
        min-height: 40px;
        padding: 9px 12px;
    }

    body.chat-page #send-button {
        padding: 9px 18px;
    }
}

@media (prefers-reduced-motion: reduce) {
    body.chat-page .bot-message-container,
    body.chat-page .user-message-container,
    body.chat-page .chat-status,
    body.chat-page #clear-chat-button,
    body.chat-page .typing-indicator .dot {
        animation: none;
    }

    body.chat-page #send-button,
    body.chat-page .chat-feedback-button {
        transition: none;
    }
}

/* --------------------------------------------------------------
   มือถือ (≤480px): เต็มจอ ไม่มีระยะขอบรอบการ์ด
   จัดเป็นคอลัมน์: หัวเรื่อง -> แชท -> ช่องพิมพ์
   ช่องพิมพ์อยู่ล่างสุดและหดตามคีย์บอร์ด ไม่ต้องสไลด์หา
   -------------------------------------------------------------- */
@media only screen and (max-width: 480px) {
    body.chat-page {
        justify-content: flex-start;
        align-items: stretch;
        gap: 0;
        height: 100dvh; /* ปรับตามหน้าจอจริงบนเบราว์เซอร์รุ่นใหม่ (กันแถบ URL ของ Safari) */
        height: var(--app-height, 100vh); /* JS ใส่ค่า visual viewport จริง กันคีย์บอร์ดทับช่องพิมพ์ */
        overflow: hidden;
        padding: 0;
        padding-top: env(safe-area-inset-top);
        background-color: #ffffff;
        background-image: none;
    }

    body.chat-page .chat-header {
        max-width: none;
        padding: 8px 14px;
        padding-top: calc(8px + env(safe-area-inset-top));
        background-color: #fffaf4;
        border-bottom: 1px solid var(--chat-line);
    }

    body.chat-page .chat-header-logo {
        height: 34px;
    }

    body.chat-page #chat-container {
        flex: 1 1 auto;
        min-height: 0;
        height: auto;
        max-width: none;
        margin: 0;
        padding-bottom: 0;
        border: 0;
        border-radius: 0;
        box-shadow: none;
    }

    body.chat-page #input-container {
        flex: 0 0 auto;
        max-width: none;
        margin: 0;
        padding: 8px 10px;
        padding-bottom: calc(8px + env(safe-area-inset-bottom)); /* เว้นพื้นที่แถบ home ของ iPhone */
        border: 0;
        border-top: 1px solid var(--chat-line);
        border-radius: 0;
        box-shadow: none;
        background-color: #ffffff;
    }

    body.chat-page #message {
        max-height: 30vh; /* กันพิมพ์ข้อความยาวมากๆ แล้วกลืนพื้นที่แสดงผลแชท */
    }

    body.chat-page #send-button {
        width: auto;
        flex-shrink: 0; /* ปุ่มห้ามถูกบีบจนหายเมื่อช่องพิมพ์ยาว */
    }

    body.chat-page .bot-reply {
        max-width: 85%;
    }

    body.chat-page .user-message,
    body.chat-page .bot-message {
        max-width: 82%;
    }

    body.chat-page #clear-chat-bar {
        padding: 6px 8px;
    }

    body.chat-page #clear-chat-button {
        padding: 4px 10px;
        font-size: 12px;
    }
}
