/* ChatTreeWidget — config-driven vanilla JS chat component */
/* Class prefix: ctw- (ChatTreeWidget) */

.ctw-wrapper {
  --ctw-brand: #2C5F7C;
  --ctw-accent: #4CAF50;
  --ctw-bubble-bg: #FFFFFF;
  --ctw-bubble-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
  --ctw-text-primary: #1A2B3C;
  --ctw-text-secondary: #5A6B7C;
  --ctw-menu-bg: #F8FAFC;
  --ctw-menu-hover: #EAF1F8;
  --ctw-divider: #E1E8EF;

  max-width: 480px;
  margin: 0 auto;
  border: 1px solid var(--ctw-divider);
  border-radius: 16px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: #fff;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.08);
  font-family: inherit;
  font-size: 14px;
  line-height: 1.5;
  color: var(--ctw-text-primary);
}

/* ── Launcher mode — reset wrapper card styles ── */
.ctw-wrapper--launcher {
  max-width: none;
  margin: 0;
  border: none;
  border-radius: 0;
  overflow: visible;
  display: block;
  background: none;
  box-shadow: none;
}

/* ── Header ── */
.ctw-header {
  background: var(--ctw-brand);
  padding: 14px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.ctw-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid rgba(255, 255, 255, 0.3);
  flex-shrink: 0;
}

.ctw-avatar-fallback {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  border: 2px solid rgba(255, 255, 255, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
  font-size: 16px;
  letter-spacing: 0.04em;
  flex-shrink: 0;
}

.ctw-agent-info { flex: 1; min-width: 0; }

.ctw-agent-name {
  font-size: 16px;
  font-weight: 700;
  color: #fff;
  line-height: 1.2;
}

.ctw-agent-subtitle {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.75);
  margin-top: 2px;
  line-height: 1.35;
}

/* ── Close button (launcher mode header) ── */
.ctw-close-btn {
  flex-shrink: 0;
  margin-left: auto;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
  transition: background 0.12s ease;
}

.ctw-close-btn:hover {
  background: rgba(255, 255, 255, 0.28);
}

/* ── Topic Menu ── */
.ctw-menu {
  background: var(--ctw-menu-bg);
  border-bottom: 1px solid var(--ctw-divider);
  flex-shrink: 0;
}

.ctw-menu-items {
  list-style: none;
  margin: 0;
  padding: 0;
}

.ctw-topic-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 16px;
  cursor: pointer;
  border-bottom: 1px solid var(--ctw-divider);
  transition: background 0.12s ease;
  user-select: none;
}

.ctw-topic-item:last-child { border-bottom: none; }
.ctw-topic-item:hover { background: var(--ctw-menu-hover); }

.ctw-topic-item.ctw-disabled {
  pointer-events: none;
  opacity: 0.55;
}

.ctw-check-slot {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ctw-accent);
}

.ctw-topic-icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  color: var(--ctw-text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
}

.ctw-topic-label {
  font-size: 14px;
  color: var(--ctw-text-primary);
  flex: 1;
  min-width: 0;
  line-height: 1.35;
}

.ctw-topic-item.ctw-checked .ctw-topic-label {
  font-weight: 600;
  color: var(--ctw-brand);
}

.ctw-reset-row {
  padding: 8px 16px 10px;
  text-align: center;
  border-top: 1px solid var(--ctw-divider);
}

.ctw-reset-btn {
  font-size: 12px;
  color: var(--ctw-text-secondary);
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
  background: none;
  border: none;
  font-family: inherit;
  padding: 0;
  transition: color 0.12s;
}

.ctw-reset-btn:hover { color: var(--ctw-text-primary); }

/* ── Conversation Pane ── */
.ctw-convo {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 140px;
  max-height: 360px;
  scroll-behavior: smooth;
}

/* In launcher panel, convo fills available height */
.ctw-panel .ctw-convo {
  max-height: none;
}

.ctw-convo-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 110px;
  color: var(--ctw-text-secondary);
  font-size: 13px;
  text-align: center;
  opacity: 0.7;
  padding: 0 24px;
  line-height: 1.5;
}

/* ── Bubble Row ── */
.ctw-bubble-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  animation: ctw-fade-up 0.18s ease;
}

.ctw-bubble-av-sm {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  object-fit: cover;
}

.ctw-bubble-av-fallback {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--ctw-brand);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
  font-size: 11px;
  letter-spacing: 0.04em;
}

.ctw-bubble {
  background: var(--ctw-bubble-bg);
  border-radius: 16px 16px 16px 4px;
  padding: 10px 14px;
  font-size: 14px;
  line-height: 1.55;
  color: var(--ctw-text-primary);
  box-shadow: var(--ctw-bubble-shadow);
  max-width: calc(100% - 44px);
  word-wrap: break-word;
}

.ctw-bubble ul {
  margin: 6px 0 0;
  padding-left: 18px;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.ctw-bubble ul li { color: var(--ctw-text-primary); }

.ctw-bubble a {
  color: var(--ctw-brand);
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* ── Typing Indicator ── */
.ctw-typing-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.ctw-typing-dots {
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--ctw-bubble-bg);
  border-radius: 16px 16px 16px 4px;
  padding: 13px 14px;
  box-shadow: var(--ctw-bubble-shadow);
}

.ctw-typing-dots span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--ctw-text-secondary);
  display: block;
  animation: ctw-bounce 1.1s infinite;
}

.ctw-typing-dots span:nth-child(2) { animation-delay: 0.17s; }
.ctw-typing-dots span:nth-child(3) { animation-delay: 0.34s; }

@keyframes ctw-bounce {
  0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

/* ── Appointment CTA ── */
.ctw-cta-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  padding-left: 40px;
  animation: ctw-fade-up 0.18s ease;
}

.ctw-btn-yes {
  background: var(--ctw-accent);
  color: #fff;
  border: none;
  border-radius: 20px;
  padding: 9px 18px;
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: opacity 0.12s;
  line-height: 1;
}

.ctw-btn-yes:hover { opacity: 0.88; }

.ctw-btn-no {
  background: #fff;
  color: var(--ctw-text-primary);
  border: 1px solid var(--ctw-divider);
  border-radius: 20px;
  padding: 9px 18px;
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.12s;
  line-height: 1;
}

.ctw-btn-no:hover { background: var(--ctw-menu-hover); }

/* ── Animations ── */
@keyframes ctw-fade-up {
  from { opacity: 0; transform: translateY(5px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── Launcher button ── */
.ctw-launcher {
  position: fixed;
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: 3px solid var(--ctw-brand);
  background: var(--ctw-brand);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.22), 0 1px 4px rgba(0, 0, 0, 0.12);
  padding: 0;
  z-index: 9999;
  overflow: hidden;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.ctw-launcher:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.26), 0 2px 6px rgba(0, 0, 0, 0.14);
}

.ctw-launcher--bottom-right {
  bottom: 20px;
  right: 20px;
}

.ctw-launcher--bottom-left {
  bottom: 20px;
  left: 20px;
}

.ctw-launcher-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
  display: block;
}

.ctw-launcher-fallback {
  color: #fff;
  font-weight: 700;
  font-size: 20px;
  letter-spacing: 0.04em;
}

/* ── Floating chat panel ── */
.ctw-panel {
  position: fixed;
  width: 380px;
  height: 600px;
  border-radius: 16px;
  border: 1px solid var(--ctw-divider);
  background: #fff;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.18), 0 2px 8px rgba(0, 0, 0, 0.10);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 9999;
  opacity: 0;
  transform: translateY(12px);
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.ctw-panel--bottom-right {
  bottom: 96px; /* 64px launcher + 20px margin + 12px gap */
  right: 20px;
}

.ctw-panel--bottom-left {
  bottom: 96px;
  left: 20px;
}

.ctw-panel--open {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

/* ── Mobile ── */
@media (max-width: 639px) {
  .ctw-wrapper { max-width: 100%; border-radius: 14px; }
  .ctw-avatar, .ctw-avatar-fallback { width: 36px; height: 36px; font-size: 13px; }
  .ctw-agent-name { font-size: 15px; }
  .ctw-agent-subtitle { font-size: 11px; }
  .ctw-bubble-av-sm, .ctw-bubble-av-fallback { width: 28px; height: 28px; font-size: 10px; }
  .ctw-bubble { font-size: 13px; padding: 9px 12px; max-width: calc(100% - 38px); }
  .ctw-cta-row { padding-left: 36px; }
  .ctw-convo { padding: 12px; max-height: 300px; }
  .ctw-header { padding: 12px 14px; gap: 10px; }
  .ctw-topic-item { padding: 10px 14px; }
  .ctw-topic-label { font-size: 13px; }

  /* Launcher panel: full-screen takeover on mobile */
  .ctw-panel {
    width: 100vw;
    height: 100dvh;
    bottom: 0 !important;
    right: 0 !important;
    left: 0 !important;
    border-radius: 0;
  }

  .ctw-panel .ctw-convo {
    max-height: none;
    padding: 12px;
  }
}

@media (max-width: 360px) {
  .ctw-header { padding: 10px 12px; }
  .ctw-convo { padding: 10px; }
  .ctw-topic-item { padding: 9px 12px; }
}
