/* ============================================================
   크라우니 인테리어 — AI 상담 챗 UI (모바일 퍼스트)
   네임스페이스: .ai-chat- / .ai-msg / .ai-quick
   ============================================================ */

/* chat.css 전용 토큰 — style.css :root에 없는 변수를 AYS 라이트+골드 팔레트로 정의.
   (미정의 시 background:var(--surface)가 투명으로 풀려 패널이 비치는 버그 방지) */
:root {
  --surface: #fffdf9;      /* 패널/카드 흰 배경 (불투명) */
  --surface2: #f4efe6;     /* 헤더/보조 영역 */
  --bg: #14110d;           /* 골드 위 아이콘 대비색 */
  --bg2: #faf8f4;          /* 메시지 영역 배경 */
  --border: #e4ddd0;
  --border-light: #efe9de;
  --gold-light: #e0c988;
  --gold-dark: #b3914a;
  --gold-glow: rgba(201, 169, 97, 0.12);
  --text2: #5a5040;
  --text3: #9b937f;
  --bg3: #efe9de;
  --radius-sm: 12px;
  --radius-xs: 8px;
  --transition: 0.28s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ---------- 플로팅 런처 (FAB) ---------- */
.ai-chat-launcher {
  position: fixed;
  bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  right: 24px;
  z-index: 200;
  width: 56px;
  height: 56px;
  min-width: 44px;
  min-height: 44px;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: var(--bg);
  background: linear-gradient(135deg, var(--gold-light) 0%, var(--gold) 55%, var(--gold-dark) 100%);
  box-shadow: 0 6px 20px rgba(200, 160, 96, 0.35), 0 2px 6px rgba(0, 0, 0, 0.4);
  transition: transform var(--transition), box-shadow var(--transition), opacity var(--transition);
}
.ai-chat-launcher:hover {
  transform: scale(1.08);
  box-shadow: 0 8px 28px rgba(200, 160, 96, 0.45), 0 2px 8px rgba(0, 0, 0, 0.5);
}
.ai-chat-launcher:active { transform: scale(0.96); }

/* 패널 열리면 런처 숨김 (JS가 body 또는 패널에 .open 토글) */
.ai-chat-panel.open ~ .ai-chat-launcher,
body.ai-chat-open .ai-chat-launcher {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.6);
}

/* ---------- 패널 ---------- */
.ai-chat-panel {
  position: fixed;
  bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  right: 24px;
  z-index: 201;
  width: 400px;
  height: min(600px, 80vh);
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.55), 0 4px 16px rgba(0, 0, 0, 0.4);
  /* 기본 숨김 */
  opacity: 0;
  pointer-events: none;
  transform: translateY(16px) scale(0.98);
  transition: opacity var(--transition), transform var(--transition);
}
.ai-chat-panel.open {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0) scale(1);
}

/* ---------- 헤더 ---------- */
.ai-chat-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 18px;
  background: linear-gradient(180deg, var(--surface2) 0%, var(--surface) 100%);
  border-bottom: 1px solid var(--border);
}
.ai-chat-header::before {
  content: "✦";
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  flex: 0 0 auto;
  border-radius: 8px;
  font-size: 16px;
  color: var(--bg);
  background: var(--gold);
}
.ai-chat-header-title {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-width: 0;
}
.ai-chat-header-title strong {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.2px;
}
.ai-chat-header-title span {
  font-size: 12px;
  color: var(--text3);
}
.ai-chat-close {
  flex: 0 0 auto;
  width: 36px;
  height: 36px;
  min-width: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: var(--radius-xs);
  background: transparent;
  color: var(--text2);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  transition: background var(--transition), color var(--transition);
}
.ai-chat-close:hover { background: var(--surface2); color: var(--text); }

/* ---------- 메시지 영역 ---------- */
.ai-chat-messages {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 18px 16px;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  background: var(--bg2);
}
.ai-chat-messages::-webkit-scrollbar { width: 8px; }
.ai-chat-messages::-webkit-scrollbar-track { background: transparent; }
.ai-chat-messages::-webkit-scrollbar-thumb {
  background: var(--border-light);
  border-radius: 4px;
  border: 2px solid var(--bg2);
}
.ai-chat-messages::-webkit-scrollbar-thumb:hover { background: var(--text3); }
.ai-chat-messages { scrollbar-width: thin; scrollbar-color: var(--border-light) transparent; }

/* ---------- 메시지 버블 ---------- */
.ai-msg {
  display: flex;
  width: 100%;
}
.ai-msg--user { justify-content: flex-end; }
.ai-msg--bot { justify-content: flex-start; }

.ai-msg-bubble {
  max-width: 80%;
  padding: 11px 14px;
  font-size: 14px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
  border-radius: var(--radius-sm);
}
.ai-msg--bot .ai-msg-bubble {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-top-left-radius: var(--radius-xs);
}
.ai-msg--user .ai-msg-bubble {
  color: var(--bg);
  background: linear-gradient(135deg, var(--gold-light) 0%, var(--gold) 100%);
  border-top-right-radius: var(--radius-xs);
}

/* ---------- 타이핑 인디케이터 (점 3개 바운스) ---------- */
.ai-msg--typing .ai-msg-bubble {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 14px 16px;
}
.ai-msg--typing .ai-msg-bubble span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text3);
  animation: aiChatBounce 1.3s infinite ease-in-out both;
}
.ai-msg--typing .ai-msg-bubble span:nth-child(1) { animation-delay: -0.30s; }
.ai-msg--typing .ai-msg-bubble span:nth-child(2) { animation-delay: -0.15s; }
.ai-msg--typing .ai-msg-bubble span:nth-child(3) { animation-delay: 0s; }
@keyframes aiChatBounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.5; }
  40% { transform: translateY(-5px); opacity: 1; }
}

/* ---------- 완료 카드 (매칭 전문가) ---------- */
.ai-msg-card {
  width: 100%;
  max-width: 80%;
  padding: 16px;
  border: 1px solid var(--gold);
  border-radius: var(--radius-sm);
  background: var(--gold-glow);
  box-shadow: 0 2px 12px rgba(200, 160, 96, 0.12);
}
.ai-msg-card-label {
  font-size: 11px;
  letter-spacing: 1.2px;
  text-transform: uppercase;
  color: var(--gold);
  font-weight: 600;
  margin-bottom: 8px;
}
.ai-msg-card-name {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 4px;
}
.ai-msg-card-track {
  font-size: 13px;
  color: var(--text2);
  margin-bottom: 12px;
}
.ai-msg-card-phone {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 18px;
  border-radius: var(--radius-xs);
  background: var(--gold);
  color: var(--bg);
  font-size: 14px;
  font-weight: 600;
  transition: background var(--transition), transform var(--transition);
}
.ai-msg-card-phone:hover {
  background: var(--gold-light);
  color: var(--bg);
  transform: translateY(-1px);
}

/* ---------- 빠른답변 칩 ---------- */
.ai-chat-quick {
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 0 16px 10px;
  background: var(--bg2);
}
.ai-chat-quick:empty { display: none; }
.ai-quick-chip {
  min-height: 34px;
  padding: 7px 14px;
  border: 1px solid var(--border-light);
  border-radius: 18px;
  background: var(--surface);
  color: var(--text2);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: border-color var(--transition), color var(--transition), background var(--transition);
}
.ai-quick-chip:hover {
  border-color: var(--gold);
  color: var(--gold-light);
  background: var(--gold-glow);
}

/* ---------- 입력바 ---------- */
.ai-chat-input-row {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 14px calc(12px + env(safe-area-inset-bottom, 0px));
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.ai-chat-input-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 14px calc(12px + env(safe-area-inset-bottom, 0px));
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.ai-chat-input {
  flex: 1 1 auto;
  min-height: 44px;
  max-height: 120px;
  padding: 11px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg3);
  color: var(--text);
  font-family: inherit;
  font-size: 16px; /* iOS 줌 방지 */
  line-height: 1.45;
  resize: none;
  overflow-y: auto;
  transition: border-color var(--transition);
}
.ai-chat-input::placeholder { color: var(--text3); }
.ai-chat-input:focus { outline: none; border-color: var(--gold); }
.ai-chat-input:disabled { opacity: 0.55; cursor: not-allowed; }

.ai-chat-photo {
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  min-width: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 18px;
  color: var(--text2);
  background: var(--surface2);
  transition: border-color var(--transition), color var(--transition), background var(--transition);
}
.ai-chat-photo:hover {
  border-color: var(--gold);
  color: var(--gold);
  background: var(--gold-glow);
}
.ai-chat-photo:active { transform: scale(0.98); }
.ai-chat-photo:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  border-color: var(--border);
  color: var(--text3);
}

.ai-chat-send {
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  min-width: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 18px;
  color: var(--bg);
  background: var(--gold);
  transition: background var(--transition), transform var(--transition), opacity var(--transition);
}
.ai-chat-send:hover { background: var(--gold-light); transform: translateY(-1px); }
.ai-chat-send:active { transform: translateY(0); }
.ai-chat-send:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
  background: var(--surface2);
  color: var(--text3);
}

/* ============================================================
   모바일 — 전체화면 시트 (≤768px)
   ============================================================ */
@media (max-width: 768px) {
  .ai-chat-panel {
    inset: 0;
    width: 100%;
    height: 100%;
    max-height: none;
    border-radius: 0;
    border: none;
    transform: translateY(100%);
  }
  .ai-chat-panel.open { transform: translateY(0); }

  .ai-chat-launcher {
    bottom: calc(18px + env(safe-area-inset-bottom, 0px));
    right: 18px;
    width: 52px;
    height: 52px;
  }

  .ai-msg-bubble { max-width: 85%; }
  .ai-msg-card { max-width: 100%; }
}

/* 모션 최소화 선호 사용자 배려 */
@media (prefers-reduced-motion: reduce) {
  .ai-chat-launcher,
  .ai-chat-panel,
  .ai-msg--typing .ai-msg-bubble span {
    transition: none;
    animation: none;
  }
}

/* 모노톤 아이콘 색상 보정 (골드 배경 버튼) */
.ai-chat-launcher { color: #14110d; }
.ai-chat-launcher .cic-svg { stroke-width: 2; }
.ai-chat-send { color: #14110d; }
.ai-chat-close .cic-svg { stroke-width: 2; }
.cic-svg { display: block; }
