/* แชท Codex ต่อโปรเจ็ก (static/chat.js) */
.chat-card { display: flex; flex-direction: column; contain: inline-size; min-width: 0; }  /* ปุ่มคำสั่งด่วนเลื่อนข้างได้ ห้ามดันหน้ามือถือให้กว้างเกินจอ */
.chat-head h2 { flex: 0 0 auto; }
.chat-head .chat-model { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-thread { max-height: 460px; min-height: 120px; overflow-y: auto; padding: 12px 12px 4px; display: flex; flex-direction: column; gap: 10px; background: var(--surface-2); overscroll-behavior: contain; }
.chat-thread .empty { padding: 18px 8px; }
.chat-older { align-self: center; }
.chat-row { display: flex; flex-direction: column; max-width: 92%; flex-shrink: 0; }
.chat-thread > * { flex-shrink: 0; }
.chat-row.me { align-self: flex-end; align-items: flex-end; }
.chat-row.bot { align-self: flex-start; }
.chat-row.sys { align-self: stretch; max-width: none; }
.chat-bubble { padding: 8px 12px; border-radius: 14px; font-size: 14.5px; line-height: 1.55; overflow-wrap: anywhere; }
.chat-row.me .chat-bubble { background: var(--brand); color: #fff; border-bottom-right-radius: 4px; }
.chat-row.bot .chat-bubble { background: var(--surface); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.chat-when { font-size: 12px; color: var(--ink-3); margin: 3px 4px 0; }
.chat-tag { font-size: 12px; background: rgba(255,255,255,.18); border-radius: 6px; padding: 1px 6px; }
.chat-note { text-align: center; font-size: 12.5px; color: var(--ink-3); padding: 2px 8px; }
.chat-err { font-size: 12.5px; color: var(--danger); margin-top: 4px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; overflow-wrap: anywhere; }
.chat-job { background: var(--surface); border: 1px dashed #cfc8b8; border-radius: 12px; padding: 8px 12px; font-size: 14px; overflow-wrap: anywhere; }
.chat-job .row { gap: 6px; margin-bottom: 2px; }
.chat-job-links { font-size: 13px; margin-top: 4px; }
.chat-log { font-size: 12px; color: var(--ink-3); margin-top: 2px; overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

/* เนื้อคำตอบ (markdown แบบย่อ) */
.md p { margin: 0 0 6px; }
.md p:last-child, .md ul:last-child, .md ol:last-child { margin-bottom: 0; }
.md h4 { font-size: 14.5px; margin: 8px 0 4px; }
.md h4:first-child { margin-top: 0; }
.md ul, .md ol { margin: 0 0 6px; padding-left: 20px; }
.md li { margin: 2px 0; }
.md code { background: #f1efe9; border-radius: 4px; padding: 0 4px; font-size: 13px; overflow-wrap: anywhere; }
.md pre { background: #f1efe9; border-radius: 8px; padding: 8px 10px; overflow-x: auto; margin: 4px 0 8px; font-size: 12.5px; }
.md pre code { background: none; padding: 0; }
.md a { color: var(--st-in_progress); }
.chat-table { overflow-x: auto; margin: 4px 0 8px; }
.chat-table table { border-collapse: collapse; font-size: 13px; }
.chat-table th, .chat-table td { border: 1px solid var(--line); padding: 3px 8px; text-align: left; vertical-align: top; }
.chat-table th { background: var(--surface-2); }

/* รายการค้างที่ Codex เสนอ */
.chat-tasks { margin-top: 8px; border-top: 1px solid var(--line); padding-top: 8px; display: flex; flex-direction: column; gap: 6px; }
.chat-tasks.done { color: #1f6b3a; }
.chat-task { display: grid; grid-template-columns: auto 1fr; gap: 4px 8px; align-items: start; font-size: 13.5px; }
.chat-task input { margin-top: 4px; }
.chat-task select { grid-column: 2; min-height: 30px; padding: 3px 8px; font-size: 12.5px; }
.chat-tasks .btn { align-self: flex-start; }

/* กำลังทำงาน */
.chat-pending:empty { display: none; }
.chat-working { display: flex; gap: 10px; align-items: flex-start; padding: 8px 12px; border-top: 1px solid var(--line); background: #f3f7fb; font-size: 13.5px; }
.chat-working .grow { flex: 1; min-width: 0; }
.chat-dots { display: inline-flex; gap: 3px; padding-top: 8px; }
.chat-dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--st-in_progress); animation: chatDot 1.2s infinite ease-in-out; }
.chat-dots i:nth-child(2) { animation-delay: .15s; }
.chat-dots i:nth-child(3) { animation-delay: .3s; }
@keyframes chatDot { 0%, 80%, 100% { opacity: .25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-3px); } }
@media (prefers-reduced-motion: reduce) { .chat-dots i { animation: none; opacity: .7; } }

/* ช่องพิมพ์ */
.chat-compose { border-top: 1px solid var(--line); padding: 8px 10px 10px; display: flex; flex-direction: column; gap: 8px; }
.chat-quick { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
.chat-quick::-webkit-scrollbar { display: none; }
.chat-input { display: flex; gap: 8px; align-items: flex-end; }
.chat-input textarea.input { min-height: 40px; max-height: 180px; resize: none; flex: 1; font-size: 16px; line-height: 1.4; }
.chat-input .btn { flex: 0 0 auto; }

/* เต็มจอ (ปุ่มขยาย / มือถือ) */
.chat-fab { display: none; }
.chat-card.chat-full { position: fixed; inset: 0; z-index: 60; border-radius: 0; border: 0; margin: 0 !important;
  padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom); }
.chat-card.chat-full .chat-thread { flex: 1; max-height: none; }
.chat-card.chat-full .chat-fab { display: none !important; }
.chat-card.chat-full .chat-thread, .chat-card.chat-full .chat-compose, .chat-card.chat-full .chat-pending { width: 100%; max-width: 820px; margin: 0 auto; }
body.chat-open { overflow: hidden; }

@media (max-width: 860px) {
  /* มือถือ: การ์ดอยู่ท้ายหน้า มีปุ่มลอยเปิดแชทเต็มจอได้จากทุกตำแหน่ง */
  .chat-fab { display: inline-flex; align-items: center; gap: 6px; position: fixed; right: 14px; bottom: calc(16px + env(safe-area-inset-bottom)); z-index: 40;
    background: var(--brand); color: #fff; border: 0; border-radius: 999px; padding: 11px 18px; font-weight: 600; font-size: 14px;
    box-shadow: 0 4px 14px rgba(29,43,54,.28); cursor: pointer; }
  .chat-fab-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
  .chat-row { max-width: 96%; }
  .chat-thread { max-height: 380px; }
}

/* ประวัติการทำงานของ Codex (กล่องใต้คำตอบ + หน้ารวม) */
.cx-box { margin-top: 8px; border-top: 1px solid var(--line); padding-top: 6px; font-size: 13px; }
.chat-row.me .cx-box { border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px; background: var(--surface); }
.chat-job .cx-box { margin-top: 6px; }
.cx-box > summary, .cx-out > summary { cursor: pointer; color: var(--ink-2); font-weight: 600; list-style-position: inside; }
.cx-box > summary:hover { color: var(--ink); }
.cx-body { margin-top: 6px; }
.cx-run + .cx-run { margin-top: 10px; border-top: 1px dashed var(--line); padding-top: 8px; }
.cx-run-head { display: flex; flex-wrap: wrap; gap: 4px 8px; align-items: center; margin-bottom: 6px; }
.cx-run-head a { color: var(--st-in_progress); }
.cx-steps { list-style: none; margin: 0; padding: 0 0 0 10px; border-left: 2px solid var(--line); display: flex; flex-direction: column; gap: 6px; }
.cx-step { position: relative; padding-left: 8px; min-width: 0; }
.cx-step::before { content: ""; position: absolute; left: -15px; top: 7px; width: 8px; height: 8px; border-radius: 50%; background: var(--ink-3); }
.cx-step.t-command_execution::before { background: var(--st-in_progress); }
.cx-step.t-file_change::before { background: var(--st-review); }
.cx-step.t-agent_message::before, .cx-step.t-reasoning::before { background: #c9c4b8; }
.cx-step.bad::before { background: var(--danger); }
.cx-head { display: flex; gap: 6px; align-items: center; font-size: 12px; }
.cx-label { font-weight: 700; color: var(--ink-2); }
.cx-time { color: var(--ink-3); }
.cx-ec { font-size: 12px; border-radius: 6px; padding: 0 6px; font-weight: 600; }
.cx-ec.ok { background: color-mix(in srgb, var(--st-done) 14%, white); color: #1f6b3a; }  /* เขียวเข้มให้อ่านได้ ≥4.5:1 */
.cx-ec.bad { background: #fbeceb; color: var(--danger); }
.cx-ec.run { background: color-mix(in srgb, var(--st-in_progress) 14%, white); color: #245a92; }
.cx-cmd { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; background: #f1efe9; border-radius: 6px; padding: 4px 8px; margin-top: 2px; white-space: pre-wrap; overflow-wrap: anywhere; max-height: 8.5em; overflow: auto; }
.cx-text { font-size: 13px; color: var(--ink-2); white-space: pre-wrap; overflow-wrap: anywhere; max-height: 10em; overflow: auto; }
.cx-step.t-reasoning .cx-text { color: var(--ink-3); font-style: italic; }
.cx-files { font-size: 12px; margin-top: 2px; overflow-wrap: anywhere; }
.cx-files code { background: #f1efe9; border-radius: 4px; padding: 0 4px; font-size: 12px; }
.cx-out { margin-top: 2px; font-size: 12px; }
.cx-out pre { background: #1d2b36; color: #e8edf1; border-radius: 6px; padding: 6px 8px; margin: 4px 0 0; max-height: 280px; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 12px; }
.chat-log.cx-livebox { display: block; -webkit-line-clamp: unset; }
.cx-live { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cx-live.bad { color: var(--danger); }

.cx-overlay { position: fixed; inset: 0; z-index: 70; background: var(--bg); display: flex; flex-direction: column; padding-top: env(safe-area-inset-top); }
.cx-overlay-head { display: flex; align-items: center; gap: 8px; padding: 12px 16px; background: var(--surface); border-bottom: 1px solid var(--line); }
.cx-overlay-body { flex: 1; overflow-y: auto; padding: 14px 16px calc(24px + env(safe-area-inset-bottom)); width: 100%; max-width: 900px; margin: 0 auto; overscroll-behavior: contain; }
.cx-filter { display: flex; gap: 6px; margin-bottom: 10px; }
.cx-filter .btn.on { background: var(--brand); color: #fff; border-color: var(--brand); }
.cx-list { display: flex; flex-direction: column; gap: 8px; }
.cx-item { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; }
.cx-item > summary { cursor: pointer; padding: 10px 12px; list-style: none; display: flex; flex-direction: column; gap: 2px; }
.cx-item > summary::-webkit-details-marker { display: none; }
.cx-item[open] > summary { border-bottom: 1px solid var(--line); }
.cx-item-top { display: flex; gap: 8px; align-items: center; }
.cx-item-title { font-size: 14px; overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.cx-item-body { padding: 10px 12px; }
.cx-legacy-h { margin: 16px 0 6px; font-size: 14px; color: var(--ink-2); }
.cx-legacy { margin: 0; padding-left: 18px; font-size: 13.5px; }
.cx-legacy li { margin: 4px 0; }

/* แชทเต็มคอลัมน์ขวาของหน้าโปรเจ็ก (#projectChat วางโดย app.js กำหนดขนาดเอง แชทเติมเต็มกล่อง) */
#projectChat > .chat-dock { height: 100%; width: 100%; margin: 0; }
.chat-card.chat-dock { min-height: 0; overflow: hidden; }
.chat-dock .chat-thread { flex: 1 1 auto; min-height: 0; max-height: none; }
.chat-dock .chat-pending { flex: 0 0 auto; max-height: 30%; overflow-y: auto; }
.chat-dock .chat-compose { flex: 0 0 auto; }
.chat-dock .chat-head { flex: 0 0 auto; }
body:has(#projectChat) .chat-fab { display: none !important; }
@media (max-width: 860px) { .chat-dock .chat-thread { max-height: none; } }
