.wrapper {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 16px;
  min-height: 0;
}

.subtitle {
  margin: 0;
  color: #999;
  font-size: 14px;
}

.grid {
  display: grid;
  flex: 1;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 24px;
  align-items: stretch;
  min-height: 0;
}

.card {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 0;
  padding: 18px 24px 24px;
  overflow: hidden;
  box-sizing: border-box;
  background: #fff;
  border: 2px solid #ececec;
  border-radius: 24px;
}

/* ── อวาตาร์ ─────────────────────────────────────────────────────────────── */

.avatarToolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
}

.avatarToolbar label {
  color: #374151;
  font-weight: 500;
}

.select {
  padding: 7px 10px;
  font-family: "Noto Sans Thai", sans-serif;
  font-size: 13px;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
}

.motionBadge {
  padding: 5px 12px;
  margin-left: auto;
  overflow: hidden;
  color: #6b7280;
  font-size: 12px;
  white-space: nowrap;
  text-overflow: ellipsis;
  background: #f4f4f4;
  border-radius: 999px;
}

.vrmCard {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  background: #f7f7f7;
  border: 1px solid #ececec;
  border-radius: 16px;
}

.vrmViewport {
  width: 100%;
  height: 100%;
  border: 0;
}

.avatarLoading {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: #6b7280;
  font-size: 13px;
  background: rgb(255 255 255 / 85%);
}

.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid #e5e7eb;
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.metaCard {
  padding: 12px 16px;
  background: #fafafa;
  border: 1px solid #ececec;
  border-radius: 16px;
}

.metaRow {
  display: flex;
  gap: 12px;
  padding: 6px 0;
  font-size: 13px;
}

.metaRow.borderTop {
  border-top: 1px solid #ececec;
}

.metaLabel {
  flex-shrink: 0;
  width: 110px;
  color: #9ca3af;
}

.metaVal {
  min-width: 0;
  color: #111827;
  word-break: break-word;
}

.metaVal.highlight {
  color: var(--color-primary);
  font-weight: 500;
}

/* ── ห้องแชท ─────────────────────────────────────────────────────────────── */

.history {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  padding: 4px;
  overflow-y: auto;
}

.bubble {
  max-width: 85%;
  padding: 10px 14px;
  font-size: 14px;
  line-height: 1.6;
  word-break: break-word;
  border-radius: 16px;
}

.botBubble {
  align-self: flex-start;
  color: #111827;
  background: #f4f4f4;
  border-bottom-left-radius: 4px;
}

.userBubble {
  align-self: flex-end;
  color: #fff;
  background: var(--color-primary);
  border-bottom-right-radius: 4px;
}

.errorBubble {
  align-self: flex-start;
  color: #b91c1c;
  background: #fee2e2;
  border-bottom-left-radius: 4px;
}

.warningBubble {
  align-self: flex-start;
  display: flex;
  flex-direction: column;
  gap: 4px;
  color: #b45309;
  background: #fffbeb;
  border-bottom-left-radius: 4px;
}

.warningBubble small {
  opacity: 0.85;
  font-size: 12px;
}

.controls {
  display: flex;
  justify-content: flex-end;
}

.inputArea {
  display: flex;
  gap: 10px;
}

.input {
  flex: 1;
  min-width: 0;
  padding: 10px 14px;
  font-family: "Noto Sans Thai", sans-serif;
  font-size: 14px;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
}

.input:focus {
  border-color: var(--color-primary);
  outline: none;
}

.primaryBtn,
.ghostBtn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 18px;
  font-family: "Noto Sans Thai", sans-serif;
  font-size: 13px;
  white-space: nowrap;
  border-radius: 12px;
  cursor: pointer;
}

.primaryBtn {
  color: #fff;
  background: var(--color-primary);
  border: 0;
}

.primaryBtn:disabled {
  background: var(--color-primary-disabled);
  cursor: not-allowed;
}

.ghostBtn {
  color: #4b5563;
  background: #f4f4f4;
  border: 0;
}

.ghostBtn:disabled {
  color: #c8c8c8;
  cursor: not-allowed;
}

@media (max-width: 1100px) {
  .grid {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) minmax(0, 1fr);
  }
}

/* ── มือถือ ──────────────────────────────────────────────────────────────
   จอ 390×664 เหลือให้เนื้อหา 555px วางแบบวิดีโอคอล อวาตาร์กินพื้นหลังทั้งหมด
   ห้องแชทกับช่องพิมพ์ลอยทับขอบล่าง จึงเห็นอวาตาร์กับช่องพิมพ์พร้อมกันโดยไม่ต้องเลื่อน

   ประวัติยังเลื่อนอ่านย้อนได้ในกล่องของตัวเอง ที่หายไปบนจอเล็กคือกล่อง
   Plain Text / Sign Structure ซึ่งซ้ำกับฟองข้อความของบอทที่บอกโครงสร้างอยู่แล้ว */
@media (max-width: 768px) {
  .wrapper {
    gap: 0;
  }

  .subtitle {
    display: none;
  }

  .grid {
    position: relative;
    display: block;
    gap: 0;
  }

  .card {
    padding: 12px;
    border-radius: 18px;
  }

  .avatarCard {
    position: absolute;
    inset: 0;
    min-height: 0;
  }

  .vrmCard {
    flex: 1;
    min-height: 0;
  }

  .metaCard {
    display: none;
  }

  /* การ์ดแชทเป็นแค่ที่วางของที่ลอยอยู่ ตัวการ์ดเองไม่มีพื้นหลังให้บังอวาตาร์ */
  .chatCard {
    position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
    gap: 8px;
    padding: 0;
    overflow: visible;
    background: none;
    border: 0;
  }

  /* เหลือแค่ฟองล่าสุดสองใบ ฟองอื่นยังอยู่ใน DOM ให้จอใหญ่ใช้ตามเดิม
     และตัดแผ่นพื้นหลังของกล่องออก ให้ฟองลอยเดี่ยว ๆ อวาตาร์จึงโผล่ระหว่างฟอง */
  .history > .bubble:not(.latest) {
    display: none;
  }

  .history {
    flex: none;
    max-height: 30vh;  /* fallback: เบราว์เซอร์ที่ไม่รู้จัก dvh (Chrome <108) */
    max-height: 30dvh;
    padding: 4px;
    background: none;
  }

  .bubble {
    max-width: 92%;
    box-shadow: 0 2px 10px rgb(0 0 0 / 14%);
  }

  .controls {
    justify-content: flex-end;
  }

  .inputArea {
    padding: 8px;
    background: #fff;
    border: 1px solid #ececec;
    border-radius: 18px;
  }

  .primaryBtn,
  .ghostBtn {
    min-height: 44px;
  }

  /* iOS Safari ซูมทั้งหน้าเองถ้าช่องกรอกมี font ต่ำกว่า 16px */
  .input,
  .select {
    min-height: 44px;
    font-size: 16px;
  }
}
