/* Toriteki AI Chat Widget */
.chat-widget {
  position: fixed;
  right: 18px;
  bottom: calc(80px + env(safe-area-inset-bottom));
  width: 380px;
  max-height: 520px;
  display: flex;
  flex-direction: column;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 16px 48px rgba(15, 23, 42, 0.18), 0 0 0 1px rgba(0, 0, 0, 0.04);
  z-index: 1086;
  overflow: hidden;
  opacity: 0;
  transform: translateY(20px) scale(0.95);
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
}
.chat-widget.open {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

/* Header */
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: linear-gradient(135deg, #1e76c7 0%, var(--brand, #1e76c7) 100%);
  color: #fff;
  flex-shrink: 0;
}
.chat-header-title {
  font-weight: 700;
  font-size: 0.95rem;
  display: flex;
  align-items: center;
  gap: 6px;
}
.chat-header-close {
  background: none;
  border: none;
  color: #fff;
  font-size: 1.2rem;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
  line-height: 1;
}
.chat-header-close:hover {
  background: rgba(255, 255, 255, 0.15);
}

/* Messages area */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 280px;
  max-height: 340px;
  scroll-behavior: smooth;
}

/* Message bubbles */
.chat-msg {
  display: flex;
  flex-direction: column;
  max-width: 85%;
  animation: chatFadeIn 0.3s ease;
}
@keyframes chatFadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
.chat-msg.user {
  align-self: flex-end;
}
.chat-msg.assistant {
  align-self: flex-start;
}
.chat-msg-bubble {
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 0.88rem;
  line-height: 1.55;
  word-break: break-word;
}
.chat-msg.user .chat-msg-bubble {
  background: var(--brand, #1e76c7);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.chat-msg.assistant .chat-msg-bubble {
  background: #f1f5f9;
  color: #1e293b;
  border-bottom-left-radius: 4px;
}
/* Markdown content in assistant bubbles */
.chat-msg.assistant .chat-msg-bubble p {
  margin: 0 0 4px 0;
}
.chat-msg.assistant .chat-msg-bubble p:last-child {
  margin-bottom: 0;
}
.chat-msg.assistant .chat-msg-bubble br {
  display: block;
  content: "";
  margin-top: 4px;
}
.chat-msg.assistant .chat-msg-bubble h3,
.chat-msg.assistant .chat-msg-bubble h4,
.chat-msg.assistant .chat-msg-bubble h5,
.chat-msg.assistant .chat-msg-bubble h6 {
  font-size: 0.85rem;
  font-weight: 700;
  margin: 8px 0 4px 0;
  color: #0f172a;
}
.chat-msg.assistant .chat-msg-bubble ul,
.chat-msg.assistant .chat-msg-bubble ol {
  margin: 4px 0;
  padding-left: 20px;
}
.chat-msg.assistant .chat-msg-bubble li {
  margin-bottom: 3px;
}
.chat-msg.assistant .chat-msg-bubble strong {
  font-weight: 700;
  color: #0f172a;
}

.chat-msg-query-number {
  font-size: 0.7rem;
  color: #64748b;
  margin-top: 4px;
  padding-left: 4px;
  font-weight: 600;
  font-family: 'SF Mono', 'Consolas', monospace;
}
.chat-msg-sources {
  font-size: 0.72rem;
  color: #94a3b8;
  margin-top: 4px;
  padding-left: 4px;
}

/* Welcome message */
.chat-welcome {
  text-align: center;
  padding: 20px 16px;
  color: #64748b;
  font-size: 0.85rem;
  line-height: 1.6;
}
.chat-welcome-icon {
  font-size: 2rem;
  margin-bottom: 8px;
}

/* Typing indicator */
.chat-typing {
  display: none;
  align-self: flex-start;
  padding: 10px 16px;
  background: #f1f5f9;
  border-radius: 14px;
  border-bottom-left-radius: 4px;
}
.chat-typing.active {
  display: flex;
  gap: 4px;
  align-items: center;
}
.chat-typing-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #94a3b8;
  animation: chatTyping 1.4s infinite ease-in-out;
}
.chat-typing-dot:nth-child(2) { animation-delay: 0.2s; }
.chat-typing-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes chatTyping {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

/* Input area */
.chat-input-area {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid #e2e8f0;
  flex-shrink: 0;
}
.chat-input {
  flex: 1;
  border: 1px solid #cbd5e1;
  border-radius: 12px;
  padding: 8px 12px;
  font-size: 0.88rem;
  resize: none;
  max-height: 80px;
  outline: none;
  transition: border-color 0.2s;
  font-family: inherit;
}
.chat-input:focus {
  border-color: var(--brand, #1e76c7);
  box-shadow: 0 0 0 2px rgba(30, 118, 199, 0.12);
}
.chat-send-btn {
  background: var(--brand, #1e76c7);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 8px 14px;
  cursor: pointer;
  font-size: 0.88rem;
  font-weight: 600;
  white-space: nowrap;
  transition: filter 0.15s;
}
.chat-send-btn:hover {
  filter: brightness(1.08);
}
.chat-send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Escalation button */
.chat-escalate-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 10px;
  padding: 8px 16px;
  font-size: 0.82rem;
  font-weight: 600;
  color: #fff;
  background: #e67e22;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s, transform 0.1s;
  box-shadow: 0 2px 6px rgba(230, 126, 34, 0.3);
}
.chat-escalate-btn:hover {
  background: #d35400;
  transform: translateY(-1px);
  text-decoration: none;
  color: #fff;
  box-shadow: 0 4px 10px rgba(230, 126, 34, 0.4);
}
.chat-escalate-btn:active {
  transform: translateY(0);
}

/* Error message */
.chat-error {
  text-align: center;
  padding: 8px;
  color: #dc3545;
  font-size: 0.8rem;
  display: none;
}
.chat-error.active {
  display: block;
}

/* Mobile: full screen overlay */
@media (max-width: 576px) {
  .chat-widget {
    right: 0;
    bottom: 0;
    width: 100%;
    max-height: 100dvh;
    height: 100dvh;
    border-radius: 0;
  }
  .chat-messages {
    max-height: none;
    flex: 1;
  }
}

/* Plus button (expand menu trigger) */
.chat-plus-btn {
  background: none;
  border: 1px solid #cbd5e1;
  cursor: pointer;
  font-size: 1.3rem;
  font-weight: 700;
  width: 36px;
  height: 36px;
  color: #6b7280;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.25s ease, background 0.15s, color 0.15s;
  line-height: 1;
}
.chat-plus-btn:hover { background: #f3f4f6; color: #374151; }
.chat-plus-btn.active {
  transform: rotate(45deg);
  color: #dc3545;
  border-color: #dc3545;
}

/* Plus expand menu */
.chat-plus-menu {
  display: none;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 12px;
  border-top: 1px solid #e2e8f0;
  background: #f8fafc;
  animation: chatPlusSlide 0.2s ease;
}
@keyframes chatPlusSlide {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
.chat-plus-item {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 20px;
  padding: 6px 14px;
  font-size: 0.8rem;
  cursor: pointer;
  color: #374151;
  transition: background 0.15s, border-color 0.15s;
}
.chat-plus-item:hover { background: #f1f5f9; border-color: #94a3b8; }
.chat-plus-icon { font-size: 1rem; }

/* Mic recording state */
.chat-plus-item.recording {
  background: #fef2f2;
  border-color: #dc3545;
  color: #dc3545;
  animation: chatMicPulse 1.5s infinite;
}
@keyframes chatMicPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(220, 53, 69, 0.3); }
  50% { box-shadow: 0 0 0 6px rgba(220, 53, 69, 0); }
}

/* TTS speak button on assistant messages */
.chat-speak-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 0.75rem;
  color: #94a3b8;
  padding: 2px 6px;
  border-radius: 6px;
  margin-top: 4px;
  transition: color 0.15s;
}
.chat-speak-btn:hover { color: #1e76c7; }
.chat-speak-btn.speaking { color: #1e76c7; }

/* Attachment preview bar */
.chat-attach-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 6px 12px 0;
  border-top: 1px solid #f0f0f0;
}
.chat-attach-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 4px;
  background: #f3f4f6;
  border-radius: 8px;
  padding: 4px 6px;
  max-width: 120px;
}
.chat-attach-item img {
  width: 48px;
  height: 48px;
  object-fit: cover;
  border-radius: 6px;
}
.chat-attach-icon { font-size: 1.2rem; }
.chat-attach-name {
  font-size: 0.7rem;
  color: #374151;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 70px;
}
.chat-attach-remove {
  position: absolute;
  top: -4px;
  right: -4px;
  background: #6b7280;
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 16px;
  height: 16px;
  font-size: 0.65rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Attached images in user message bubble */
.chat-msg-images {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 4px;
}
.chat-msg-thumb {
  width: 64px;
  height: 64px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid rgba(0,0,0,0.08);
}
.chat-msg-file-tag {
  font-size: 0.75rem;
  background: #f3f4f6;
  border-radius: 6px;
  padding: 4px 8px;
  color: #374151;
}
