/* ─────────────────────────────────────────────────────────────────────────
 * Chatbot — prototype-v2 SoT: user-portal/chatbot/index.html inline <style>
 *   mobile-first: 사이드바 모바일에서 가림 (햄버거 토글) → 1024px+ inline
 *   TODO P2-3: mock 응답 → /api/v1/chat SSE 프록시(FastAPI chat 모듈)로 교체.
 * ──────────────────────────────────────────────────────────────────────── */

.chat-layout {
  display: flex;
  height: calc(100vh - var(--gnb-height));
  /* iOS Safari 주소창 수축/확장 대응 — dvh 미지원 브라우저는 위 100vh 폴백 */
  height: calc(100dvh - var(--gnb-height));
  background: var(--bg-darker);
  overflow: hidden;
}

/* Sidebar */
.chat-sidebar {
  width: 240px;
  background: var(--bg-surface);
  border-right: 1px solid var(--glass-border);
  display: none;
  flex-direction: column;
  padding: var(--spacing-md);
  flex-shrink: 0;
}
@media (min-width: 1024px) {
  .chat-sidebar { display: flex; width: 280px; }
}

.new-chat-btn {
  width: 100%;
  padding: 12px;
  background: var(--bg-surface);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-md);
  font-size: 14px;
  font-weight: 700;
  color: var(--text-main);
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  transition: all 0.2s;
  margin-bottom: var(--spacing-md);
  font-family: inherit;
}
.new-chat-btn:hover {
  background: var(--bg-muted);
  border-color: var(--text-dim);
}

.history-list {
  flex: 1;
  /* flex 자식 기본 min-height:auto 면 내용이 줄지 못해 스크롤 대신 잘림 → 0 으로 풀어 스크롤 활성화 */
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
/* 스크롤바 가시화 (얇게) */
.history-list::-webkit-scrollbar { width: 8px; }
.history-list::-webkit-scrollbar-thumb {
  background: var(--glass-border);
  border-radius: 4px;
}
.history-list:hover::-webkit-scrollbar-thumb { background: var(--text-dim); }
.history-list { scrollbar-width: thin; }
.history-group-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  margin: 16px 0 8px 8px;
}
/* 컨테이너(div): 제목 버튼 + 휴지통 버튼 가로 배치 */
.history-item {
  display: flex;
  align-items: center;
  border-radius: var(--radius-md);
  transition: background 0.2s;
}
.history-item:hover { background: var(--bg-muted); }
.history-item.active { background: var(--bg-accent-sky); }

/* 제목 클릭 영역(button) — 행 대부분 차지, ellipsis */
.history-item-main {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 4px 10px 12px;
  border: none;
  background: transparent;
  font-family: inherit;
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
  text-align: left;
}
.history-item-main i { color: var(--text-dim); font-size: 14px; flex-shrink: 0; }
.history-item-title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.history-item:hover .history-item-main { color: var(--text-main); }
.history-item.active .history-item-main { color: var(--primary); font-weight: 600; }
.history-item.active .history-item-main i { color: var(--primary); }

/* 휴지통 버튼 — 평소 숨김, hover 시 노출 */
.history-item-del {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  margin-right: 4px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  border-radius: var(--radius-sm);
  opacity: 0;
  transition: opacity 0.15s, color 0.15s, background 0.15s;
}
.history-item:hover .history-item-del,
.history-item.active .history-item-del { opacity: 1; }
.history-item-del:hover { color: #ef4444; background: rgba(239, 68, 68, 0.1); }
/* 터치 기기 — hover 없으니 항상 노출 */
@media (hover: none) {
  .history-item-del { opacity: 1; }
}

.chat-sidebar-footer {
  margin-top: auto;
  padding-top: 16px;
  border-top: 1px solid var(--glass-border);
  font-size: 12px;
  color: var(--text-dim);
  text-align: center;
}

/* Main */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  min-width: 0;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--spacing-md);
  display: flex;
  flex-direction: column;
  gap: 20px;
}
@media (min-width: 1024px) {
  .chat-messages { padding: var(--spacing-xl) 12%; }
}

/* Empty state */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100%;
  text-align: center;
  padding: var(--spacing-md);
}
.empty-logo {
  width: 56px;
  height: 56px;
  background: linear-gradient(135deg, var(--primary), var(--secondary));
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  color: white;
  margin-bottom: 18px;
  box-shadow: 0 10px 25px rgba(0, 113, 205, 0.3);
}
.empty-title {
  font-size: 20px;
  font-weight: 800;
  color: var(--text-main);
  margin-bottom: 8px;
}
@media (min-width: 768px) {
  .empty-title { font-size: 24px; }
}
.empty-subtitle {
  font-size: 14px;
  color: var(--text-muted);
  margin-bottom: 24px;
}
@media (min-width: 768px) {
  .empty-subtitle { font-size: 15px; margin-bottom: 32px; }
}

.prompt-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
  width: 100%;
  max-width: 700px;
}
@media (min-width: 768px) {
  .prompt-grid { gap: 16px; }
}
.prompt-card {
  background: var(--bg-surface);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-md);
  padding: 10px 12px;
  text-align: left;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: var(--shadow-sm);
  font-family: inherit;
}
@media (min-width: 768px) {
  .prompt-card { padding: 14px; border-radius: var(--radius-lg); }
}
.prompt-card:hover {
  transform: translateY(-2px);
  border-color: var(--primary);
  box-shadow: 0 4px 12px rgba(0, 113, 205, 0.1);
}
.prompt-card-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-main);
  margin-bottom: 4px;
}
@media (min-width: 768px) {
  .prompt-card-title { font-size: 14px; margin-bottom: 6px; }
}
.prompt-card-desc {
  font-size: 11px;
  color: var(--text-muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.4;
}
@media (min-width: 768px) {
  .prompt-card-desc { font-size: 12px; -webkit-line-clamp: 3; }
}

/* Input */
.chat-input-container {
  padding: var(--spacing-md);
  /* 홈바(safe-area) 기기에서 입력창이 시스템 제스처 영역과 겹치지 않게 */
  padding-bottom: calc(var(--spacing-md) + env(safe-area-inset-bottom, 0px));
  background: linear-gradient(to top, var(--bg-darker) 80%, transparent);
}
@media (min-width: 1024px) {
  .chat-input-container { padding: var(--spacing-lg) 12%; }
}
.chat-input-disclaimer {
  font-size: 11px;
  color: var(--text-muted);
  text-align: center;
  margin-bottom: 8px;
}
.input-wrapper {
  position: relative;
  background: var(--bg-surface);
  border: 1px solid var(--glass-border);
  border-radius: 20px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
  display: flex;
  /* 버튼을 textarea 세로 중앙에 정렬 + gap 으로 textarea 와 간격 확보 (붙음 방지) */
  align-items: center;
  gap: 10px;
  padding: 6px 14px;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.input-wrapper:focus-within {
  border-color: var(--primary);
  box-shadow: 0 4px 20px rgba(0, 113, 205, 0.15);
}
.chat-textarea {
  flex: 1;
  border: none;
  background: transparent;
  resize: none;
  padding: 6px 0;
  font-size: 14px;
  line-height: 1.5;
  color: var(--text-main);
  outline: none;
  max-height: 150px;
  min-height: 24px;
  font-family: inherit;
}
.chat-textarea::placeholder { color: var(--text-muted); }

.btn-send {
  width: 32px;
  height: 32px;
  flex-shrink: 0;   /* textarea 가 늘어나도 버튼 크기 유지 */
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
  flex-shrink: 0;
  margin-bottom: 4px;
}
.btn-send:hover {
  background: var(--primary-variant);
  transform: scale(1.05);
}
.btn-send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

/* Messages */
.message-row {
  display: flex;
  gap: 12px;
  max-width: 800px;
  margin: 0 auto;
  width: 100%;
}
.message-row.user { flex-direction: row-reverse; }

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  flex-shrink: 0;
  font-size: 14px;
}
.avatar-user {
  background: var(--glass-border);
  color: var(--text-secondary);
}
.avatar-ai {
  background: linear-gradient(135deg, var(--primary), var(--secondary));
  color: white;
  font-size: 16px;
}
.message-bubble {
  padding: 14px 16px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.6;
  max-width: 85%;
}
.message-bubble.user {
  background: var(--bg-muted);
  color: var(--text-main);
  border-top-right-radius: 4px;
}
.message-bubble.ai {
  background: var(--bg-surface);
  border: 1px solid var(--glass-border);
  color: var(--text-main);
  border-top-left-radius: 4px;
  box-shadow: var(--shadow-sm);
}
.message-bubble.ai p { margin: 0 0 8px 0; }
.message-bubble.ai p:last-child { margin-bottom: 0; }
.message-bubble.ai ol,
.message-bubble.ai ul {
  margin: 8px 0;
  padding-left: 20px;
}
.message-bubble.ai li { margin-bottom: 6px; }
.message-bubble.ai strong { color: var(--text-main); }
.message-bubble.ai code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.92em;
  background: var(--bg-darker);
  border: 1px solid var(--glass-border);
  padding: 1px 5px;
  border-radius: 4px;
}
/* 마크다운 표 — 지수 비교·수치 정리 등. */
.message-bubble.ai table {
  border-collapse: collapse;
  width: 100%;
  margin: 8px 0;
  font-size: 12.5px;
  display: block;
  overflow-x: auto;
}
.message-bubble.ai th,
.message-bubble.ai td {
  border: 1px solid var(--glass-border);
  padding: 6px 10px;
  text-align: left;
}
.message-bubble.ai th { background: var(--bg-darker); font-weight: 700; }

/* 검토의견서 #45 / 이슈 #94 — 액션칩(.message-chip)은 현재 챗봇 마크업에서 렌더되지 않는다(미사용 규칙).
   출처 표기는 .chat-citation-chip + .chat-citations 박스로 이미 분리돼 있고, 리포트 생성 액션은
   인터널 리포트 화면의 실제 버튼(#irp-generate-label)이 담당한다.
   ⚠️ 향후 대화 내 액션칩을 되살릴 경우 #45 요구대로 출처 칩과 구분되는 "채움 배경 + 버튼 형태"로 올려야 한다
      (현재 규칙은 출처 칩과 동일 계열 pill 이므로 그대로 재사용 금지). */
.message-chips {
  margin-top: 12px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.message-chip {
  font-size: 11px;
  padding: 4px 10px;
  background: var(--bg-muted);
  border: 1px solid var(--glass-border);
  border-radius: 20px;
  color: var(--text-secondary);
  display: inline-flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  font-family: inherit;
}
.message-chip:hover {
  color: var(--primary);
  border-color: var(--primary);
}
.message-chip.report { color: var(--primary); }
.message-chip.done {
  color: #10b981;
  background: #ecfdf5;
  border-color: #10b981;
}

/* Typing indicator */
.typing-indicator {
  display: flex;
  gap: 4px;
  padding: 12px 16px;
  background: var(--bg-surface);
  border: 1px solid var(--glass-border);
  border-radius: 16px;
  border-top-left-radius: 4px;
  width: fit-content;
  box-shadow: var(--shadow-sm);
  align-items: center;
}
.typing-dot {
  width: 8px;
  height: 8px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: chatTyping 1.4s infinite ease-in-out both;
}
.typing-dot:nth-child(1) { animation-delay: -0.32s; }
.typing-dot:nth-child(2) { animation-delay: -0.16s; }
@keyframes chatTyping {
  0%, 80%, 100% { transform: scale(0); }
  40% { transform: scale(1); }
}

/* Report draft container */
.report-draft-container {
  background: var(--bg-surface);
  border: 1px solid var(--primary);
  border-radius: var(--radius-lg);
  padding: 18px;
  margin-top: 12px;
  box-shadow: 0 4px 15px rgba(0, 113, 205, 0.1);
}
.report-draft-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 2px solid var(--bg-muted);
  padding-bottom: 12px;
  margin-bottom: 16px;
  gap: 10px;
  flex-wrap: wrap;
}
.report-draft-title {
  display: flex;
  align-items: center;
  gap: 8px;
}
.report-draft-title h3 {
  margin: 0;
  font-size: 14px;
  font-weight: 800;
  color: var(--text-main);
}
.report-draft-icon {
  width: 28px;
  height: 28px;
  background: var(--primary);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  font-size: 12px;
}
.report-draft-download {
  padding: 6px 12px;
  font-size: 12px;
  border-radius: 4px;
  border: none;
  background: var(--primary);
  color: white;
  cursor: pointer;
  font-family: inherit;
}
.report-draft-section h4 {
  font-size: 13px;
  font-weight: 700;
  margin: 0 0 6px 0;
  color: var(--text-secondary);
}
.report-draft-section p,
.report-draft-section ul {
  font-size: 13px;
  color: var(--text-main);
  margin: 0 0 16px 0;
  line-height: 1.6;
}
.report-draft-section ul { padding-left: 20px; }
.report-draft-section ul li { margin-bottom: 6px; }
.report-draft-meta {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px dashed var(--glass-border);
  text-align: right;
  font-size: 12px;
  color: var(--text-muted);
}

/* 스트리밍 중 cursor blink — Chatbot.tsx inline animation 'blink 1s steps(2) infinite' 정의처 */
@keyframes blink {
  0%, 49% { opacity: 1; }
  50%, 100% { opacity: 0; }
}

/* ─── 출처(sources) — AI 메시지 하단 citation chip ──────────────────── */
.chat-citations {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px 12px;
  background: rgba(0, 113, 205, 0.04);
  border: 1px solid rgba(0, 113, 205, 0.12);
  border-radius: 8px;
  font-size: 12px;
}
.chat-citations-label {
  color: var(--text-muted);
  font-weight: 600;
  font-size: 11px;
}
.chat-citations-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.chat-citation-chip {
  display: inline-flex;
  align-items: center;
  padding: 3px 8px;
  background: var(--bg-surface);
  border: 1px solid var(--glass-border);
  border-radius: 12px;
  font-family: inherit;
  font-size: 11px;
  color: var(--primary);
  text-decoration: none;
  max-width: 240px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
}
.chat-citation-chip:hover {
  background: rgba(0, 113, 205, 0.08);
  border-color: rgba(0, 113, 205, 0.3);
}

/* 웹 출처(외부 검색 fallback) — 내부 뉴스(파랑)와 색으로 구분(틸). 클릭=새 탭 아웃링크. */
.chat-citation-chip.web {
  color: #0b7285;
  border-color: rgba(11, 114, 133, 0.35);
  background: rgba(11, 114, 133, 0.06);
}
.chat-citation-chip.web:hover {
  background: rgba(11, 114, 133, 0.12);
  border-color: rgba(11, 114, 133, 0.5);
}

/* 지수 출처 — 뉴스(파랑)/웹(틸)과 구분(보라). 클릭=우측 패널에 지수 차트. */
.chat-citation-chip.index {
  color: #7048c4;
  border-color: rgba(112, 72, 196, 0.35);
  background: rgba(112, 72, 196, 0.06);
}
.chat-citation-chip.index:hover {
  background: rgba(112, 72, 196, 0.12);
  border-color: rgba(112, 72, 196, 0.5);
}

/* 출처 상세 — 우측 슬라이드 패널 (#119). z-index 는 FAB(z 9000)·토스트(9000) 위로
   올린다(우하단 '원문 링크' 버튼이 FAB에 가려 클릭 차단되던 것 수정, #122) — Dashboard 모달(10001) 선례. */
.src-panel-overlay {
  position: fixed; inset: 0; z-index: 10000;
  background: rgba(0, 0, 0, 0.35);
}
.src-panel {
  position: fixed; top: 0; right: 0; z-index: 10001;
  width: min(420px, 92vw); height: 100%;
  background: var(--bg-surface);
  border-left: 1px solid var(--glass-border);
  box-shadow: -8px 0 24px rgba(0, 0, 0, 0.18);
  display: flex; flex-direction: column;
  padding: 18px;
  animation: src-panel-in 0.22s ease-out;
}
@keyframes src-panel-in { from { transform: translateX(100%); } to { transform: translateX(0); } }
.src-panel-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.src-panel-source { font-size: 12px; font-weight: 700; color: var(--text-muted); }
.src-panel-close {
  background: none; border: none; cursor: pointer;
  color: var(--text-muted); font-size: 16px; padding: 4px; line-height: 1;
}
.src-panel-close:hover { color: var(--text-main); }
.src-panel-title { font-size: 16px; font-weight: 800; color: var(--text-main); margin: 10px 0 14px; line-height: 1.4; }
.src-panel-body { flex: 1; overflow-y: auto; font-size: 14px; line-height: 1.65; color: var(--text-secondary); }
.src-panel-empty { color: var(--text-muted); font-style: italic; }
.src-panel-foot { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; padding-top: 14px; border-top: 1px solid var(--glass-border); }
.src-panel-link {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 9px 14px; border-radius: 8px;
  background: var(--primary); color: #fff; text-decoration: none;
  font-size: 13px; font-weight: 700;
}
.src-panel-link:hover { filter: brightness(0.94); }
.src-panel-link.secondary { background: transparent; color: var(--primary); border: 1px solid var(--primary); }
.src-panel-link.secondary:hover { background: var(--primary); color: #fff; filter: none; }

/* ─────────────────────────────────────────────────────────────
   모바일 채팅 (GPT 모바일 패턴) — 파일 끝: 같은 특이도 기본 규칙보다 우선
   1) 상단 바(대화 목록·새 대화) 2) 대화목록 드로어 3) 입력바 확대
───────────────────────────────────────────────────────────── */
.chat-mobile-bar {
  display: flex;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--glass-border);
  background: var(--bg-surface);
}
.chat-mobile-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  min-height: 40px;
  border-radius: var(--radius-full);
  border: 1px solid var(--glass-border);
  background: var(--bg-muted);
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}
.chat-sidebar-backdrop { display: none; }
@media (min-width: 1024px) {
  .chat-mobile-bar { display: none; }
}
@media (max-width: 1023px) {
  /* 드로어로 승격 — 기본은 숨김(.chat-sidebar display:none), 열림 시 좌측 고정 패널 */
  .chat-layout.sidebar-open .chat-sidebar {
    display: flex;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(80vw, 300px);
    z-index: var(--z-modal);
    box-shadow: var(--shadow-lg);
    padding-top: env(safe-area-inset-top, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  .chat-layout.sidebar-open .chat-sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: calc(var(--z-modal) - 1);
  }
  /* 입력바 — GPT 모바일: 넉넉한 필드 + 하단 고정(푸터 제거로 자연히 바닥에 붙음) */
  .chat-input-container { padding: 10px 12px; padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px)); }
  .input-wrapper { padding: 8px 8px 8px 16px; border-radius: 26px; gap: 8px; align-items: flex-end; }
  .chat-textarea { min-height: 30px; font-size: 16px; padding: 6px 0; } /* 16px = iOS 포커스 줌 방지 */
  .btn-send { width: 38px; height: 38px; margin-bottom: 0; flex-shrink: 0; }
}

/* KRDS 터치 타깃 44px — components.css 허용목록과 같은 기준. 이 파일이 번들에서
   나중에 로드돼 위 규칙들을 이기므로 챗봇 버튼은 여기서 보정해야 한다. */
@media (pointer: coarse) {
  .chat-mobile-btn { min-height: 44px; }
  .btn-send { width: 44px; height: 44px; }
}

/* AI 답변 만족도(👍/👎) — 성과지표 결과지표 계측 */
.chat-rate { display: flex; align-items: center; gap: 8px; margin-top: 2px; padding-left: 2px; }
.chat-rate-label { font-size: 12px; color: var(--text-muted); }
.chat-rate-btn {
  width: 28px; height: 28px; padding: 0;
  border: 1px solid var(--glass-border); border-radius: 6px;
  background: var(--bg-surface); color: var(--text-muted);
  cursor: pointer; font-size: 12px;
  display: inline-flex; align-items: center; justify-content: center;
  transition: color .15s, background .15s, border-color .15s;
}
.chat-rate-btn:hover { color: var(--primary); border-color: var(--primary); }
.chat-rate-btn.on { background: var(--primary); border-color: var(--primary); color: #fff; }
.chat-rate-btn.on.down { background: var(--danger, #d64545); border-color: var(--danger, #d64545); }
.chat-rate-thanks { font-size: 12px; color: var(--success, #2e7d32); font-weight: 600; }
