/* หน้าสนทนาแบบแอปแชท ข้อความของเราอยู่ขวา ข้อความคนอื่นอยู่ซ้าย */
.chat-open .page { max-width: none; padding: 0 !important; gap: 0; }
.chat-open .tabbar { display: none; }

.chat {
    gap: 0;
    height: 100vh;
    height: 100dvh;
    background: var(--chat-bg);
}

.chat-head {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 58px;
    padding: 6px 10px;
    padding-top: calc(6px + env(safe-area-inset-top));
    background: var(--surface);
    border-bottom: 1px solid var(--line);
}
.chat-title { flex: 1; min-width: 0; }
.chat-title h1 { overflow: hidden; font-size: 17px; line-height: 1.3; text-overflow: ellipsis; white-space: nowrap; }
.chat-title p { overflow: hidden; font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }

.people {
    display: flex;
    gap: 10px;
    padding: 10px 12px;
    overflow-x: auto;
    background: var(--surface);
    border-bottom: 1px solid var(--line);
}
.person { display: flex; flex-direction: column; align-items: center; width: 62px; color: var(--text); font-size: 12px; }
.person span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.chat-body { position: relative; display: flex; flex: 1; flex-direction: column; min-height: 0; }

.messages {
    /* พื้นแชทเป็นสีขาวทุกธีม จึงใช้ชุดสีสว่างเฉพาะพื้นที่นี้ แม้หน้าอื่นจะเป็นโหมดมืด */
    --surface: #eef2f8;
    --surface-2: #e9f0fa;
    --line: #e1e8f2;
    --text: #12233f;
    --muted: #5b6d88;
    --brand: #0a6fc2;
    --bubble-mine: #0a6fc2;
    color-scheme: light;
    color: var(--text);
    display: flex;
    flex: 1;
    flex-direction: column;
    gap: 1px;
    width: 100%;
    min-height: 0;
    padding: 12px;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}
/* ดันข้อความไปชิดด้านล่างเมื่อมีน้อย เหมือนแอปแชททั่วไป */
.messages::before { content: ""; flex: 1 0 0; }

.older-state { min-height: 20px; font-size: 12px; text-align: center; }

.day-line {
    align-self: center;
    margin: 12px 0 8px;
    padding: 2px 12px;
    border-radius: 11px;
    background: var(--surface-2);
    color: var(--muted);
    font-size: 12px;
}
.system-line { align-self: center; margin: 6px 0; color: var(--muted); font-size: 12px; text-align: center; }

.message { display: flex; align-items: flex-end; gap: 6px; max-width: 82%; }
.message.run-start { margin-top: 10px; }
.message.mine { align-self: flex-end; }
.message:not(.mine) { align-self: flex-start; }

.message .avatar { width: 32px; height: 32px; visibility: hidden; }
.message.run-end .avatar { visibility: visible; }

.bubble {
    position: relative;
    min-width: 0;
    padding: 6px 11px 6px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 6px 16px 16px 6px;
    overflow-wrap: anywhere;
}
.bubble p { white-space: pre-wrap; }
.message.run-start .bubble { border-top-left-radius: 16px; }
.message.run-end .bubble { border-bottom-left-radius: 4px; }

.message.mine .bubble {
    background: var(--bubble-mine);
    border-color: var(--bubble-mine);
    border-radius: 16px 6px 6px 16px;
    color: var(--on-mine);
}
.message.mine.run-start .bubble { border-top-right-radius: 16px; }
.message.mine.run-end .bubble { border-bottom-right-radius: 4px; }

.bubble-name { display: none; align-items: center; gap: 4px; font-size: 13px; font-weight: 600; }
.message.run-start .bubble-name { display: flex; }
.bubble-name a { color: var(--brand); }
.bubble-name .role { padding: 0 6px; border-radius: 8px; background: var(--surface-2); color: var(--muted); font-size: 11px; font-weight: 500; }

.bubble-image { display: block; max-width: 200px; max-height: 240px; min-height: 40px; margin-top: 8px; border-radius: 10px; }
.bubble-image:first-child { margin-top: 2px; }
.bubble time { display: block; margin-top: 2px; color: var(--muted); font-size: 11px; line-height: 1.5; text-align: right; }
.bubble-image + time { margin-top: 8px; }
.message.mine time { color: inherit; opacity: 0.75; }

.jump {
    position: absolute;
    right: 16px;
    bottom: 12px;
    display: inline-flex;
    align-items: center;
    gap: 2px;
    min-width: 42px;
    height: 42px;
    padding: 0 10px;
    border: 1px solid var(--line);
    border-radius: 21px;
    background: var(--surface);
    color: var(--text);
    font: inherit;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.composer {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 8px 10px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
    background: var(--surface);
    border-top: 1px solid var(--line);
}
.composer input { flex: 1; min-width: 0; min-height: 42px; border-radius: 21px; }

.composer-notice {
    padding: 14px 16px;
    padding-bottom: calc(14px + env(safe-area-inset-bottom));
    background: var(--surface);
    border-top: 1px solid var(--line);
    color: var(--muted);
    font-size: 14px;
    text-align: center;
}

.emo-panel {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(52px, 1fr));
    gap: 4px;
    max-height: 240px;
    padding: 8px;
    overflow-y: auto;
    background: var(--surface);
    border-top: 1px solid var(--line);
}
.emo-panel button { min-height: 48px; padding: 4px; border: 0; border-radius: 8px; background: transparent; cursor: pointer; }
.emo-panel button:hover { background: var(--surface-2); }
.emo-panel img { max-width: 44px; max-height: 44px; }
.image-panel { display: flex; gap: 8px; padding: 8px 10px; background: var(--surface); border-top: 1px solid var(--line); }

.group-mark {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: none;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--surface-2);
    color: var(--brand);
}

@media (min-width: 900px) {
    .chat { border-left: 0; }
    .messages { padding: 16px 24px; }
    /* จอกว้างมากก็ไม่ให้บรรทัดยาวจนอ่านยาก */
    .message { max-width: min(68%, 680px); }
}

@media (prefers-reduced-motion: no-preference) {
    .message { animation: rise 0.14s ease-out; }
}
@keyframes rise {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: none; }
}
