/* Chat Widget — Dark theme matching Infinite Visions AI portals
   All classes prefixed with cw- to avoid host page conflicts */

/* ==================== VARIABLES ==================== */

.cw-widget {
  --cw-sidebar-bg: #171717;
  --cw-main-bg: #212121;
  --cw-surface: #2A2A2A;
  --cw-surface-hover: #333333;
  --cw-text-primary: #ECECEC;
  --cw-text-secondary: #8E8E8E;
  --cw-text-muted: #666666;
  --cw-accent: #6C63FF;
  --cw-accent-dim: rgba(108, 99, 255, 0.15);
  --cw-accent-hover: #7B73FF;
  --cw-border: #333333;
  --cw-code-bg: #1A1A1A;
  --cw-code-text: #E0E0E0;
  --cw-danger: #F44336;
  --cw-transition: 200ms;
  --cw-radius: 12px;
  --cw-radius-sm: 8px;
  --cw-shadow: 0 8px 32px rgba(0, 0, 0, 0.45);
  --cw-shadow-btn: 0 4px 16px rgba(108, 99, 255, 0.35);
}

/* ==================== TOGGLE BUTTON ==================== */

.cw-toggle-btn {
  position: fixed;
  right: 24px;
  bottom: 24px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--cw-accent);
  border: none;
  cursor: pointer;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--cw-shadow-btn);
  transition: transform var(--cw-transition), background var(--cw-transition), box-shadow var(--cw-transition);
}

.cw-toggle-btn:hover {
  transform: scale(1.08);
  background: var(--cw-accent-hover);
  box-shadow: 0 6px 24px rgba(108, 99, 255, 0.45);
}

.cw-toggle-btn:active {
  transform: scale(0.95);
}

.cw-toggle-btn svg {
  width: 26px;
  height: 26px;
  fill: #fff;
  transition: transform var(--cw-transition);
}

.cw-toggle-btn.cw-open svg {
  transform: rotate(90deg);
}

/* Bounce animation on first load */
@keyframes cw-bounce-in {
  0% { transform: scale(0); opacity: 0; }
  50% { transform: scale(1.15); }
  70% { transform: scale(0.92); }
  100% { transform: scale(1); opacity: 1; }
}

.cw-toggle-btn.cw-bounce {
  animation: cw-bounce-in 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

/* ==================== CHAT PANEL ==================== */

.cw-panel {
  position: fixed;
  right: 24px;
  bottom: 96px;
  width: 420px;
  max-height: 600px;
  height: 600px;
  background: var(--cw-main-bg);
  border: 1px solid var(--cw-border);
  border-radius: var(--cw-radius);
  box-shadow: var(--cw-shadow);
  z-index: 9998;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  transform: translateY(20px) scale(0.95);
  pointer-events: none;
  transition: opacity var(--cw-transition), transform var(--cw-transition);
}

.cw-panel.cw-visible {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

/* ==================== PANEL HEADER ==================== */

.cw-header {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  background: var(--cw-sidebar-bg);
  border-bottom: 1px solid var(--cw-border);
  flex-shrink: 0;
}

.cw-header-title {
  flex: 1;
  font-size: 14px;
  font-weight: 600;
  color: var(--cw-text-primary);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.cw-header-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 6px;
  color: var(--cw-text-secondary);
  transition: background var(--cw-transition), color var(--cw-transition);
  display: flex;
  align-items: center;
  justify-content: center;
}

.cw-header-btn:hover {
  background: var(--cw-surface);
  color: var(--cw-text-primary);
}

.cw-header-btn svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
}

/* ==================== PROVIDER / MODEL BAR ==================== */

.cw-provider-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: var(--cw-sidebar-bg);
  border-bottom: 1px solid var(--cw-border);
  flex-shrink: 0;
}

.cw-select {
  flex: 1;
  padding: 5px 8px;
  background: var(--cw-surface);
  border: 1px solid var(--cw-border);
  border-radius: 6px;
  color: var(--cw-text-primary);
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
  outline: none;
  transition: border-color var(--cw-transition);
  -webkit-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%238E8E8E'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  padding-right: 24px;
}

.cw-select:focus {
  border-color: var(--cw-accent);
}

.cw-select option {
  background: var(--cw-surface);
  color: var(--cw-text-primary);
}

/* ==================== MESSAGES AREA ==================== */

.cw-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
}

.cw-messages::-webkit-scrollbar {
  width: 5px;
}

.cw-messages::-webkit-scrollbar-track {
  background: transparent;
}

.cw-messages::-webkit-scrollbar-thumb {
  background: #444;
  border-radius: 3px;
}

/* ==================== MESSAGE BUBBLES ==================== */

.cw-msg {
  max-width: 80%;
  padding: 10px 14px;
  border-radius: var(--cw-radius-sm);
  font-size: 13px;
  line-height: 1.55;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.cw-msg-user {
  align-self: flex-end;
  background: var(--cw-accent);
  color: #fff;
  border-bottom-right-radius: 3px;
}

.cw-msg-assistant {
  align-self: flex-start;
  background: var(--cw-surface);
  color: var(--cw-text-primary);
  border-bottom-left-radius: 3px;
}

.cw-msg-error {
  align-self: flex-start;
  background: rgba(244, 67, 54, 0.12);
  color: var(--cw-danger);
  border: 1px solid rgba(244, 67, 54, 0.25);
  border-bottom-left-radius: 3px;
  font-size: 12px;
}

/* ==================== MARKDOWN IN MESSAGES ==================== */

.cw-msg-assistant strong {
  color: #fff;
  font-weight: 600;
}

.cw-msg-assistant em {
  color: var(--cw-text-secondary);
  font-style: italic;
}

.cw-msg-assistant code {
  background: var(--cw-code-bg);
  color: var(--cw-code-text);
  padding: 1px 5px;
  border-radius: 3px;
  font-family: 'SF Mono', 'Cascadia Code', Consolas, monospace;
  font-size: 0.88em;
}

.cw-msg-assistant pre {
  background: var(--cw-code-bg);
  border: 1px solid var(--cw-border);
  border-radius: 6px;
  padding: 10px 12px;
  overflow-x: auto;
  margin: 8px 0 4px;
}

.cw-msg-assistant pre code {
  background: transparent;
  padding: 0;
  font-size: 0.84em;
  line-height: 1.5;
}

.cw-msg-assistant ul,
.cw-msg-assistant ol {
  margin: 6px 0;
  padding-left: 20px;
}

.cw-msg-assistant li {
  margin-bottom: 3px;
}

.cw-msg-assistant a {
  color: var(--cw-accent);
  text-decoration: none;
}

.cw-msg-assistant a:hover {
  text-decoration: underline;
}

/* ==================== TYPING INDICATOR ==================== */

.cw-typing {
  align-self: flex-start;
  background: var(--cw-surface);
  padding: 12px 18px;
  border-radius: var(--cw-radius-sm);
  border-bottom-left-radius: 3px;
  display: flex;
  gap: 5px;
  align-items: center;
}

.cw-typing-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--cw-text-secondary);
  animation: cw-typing-bounce 1.4s infinite ease-in-out;
}

.cw-typing-dot:nth-child(2) {
  animation-delay: 0.16s;
}

.cw-typing-dot:nth-child(3) {
  animation-delay: 0.32s;
}

@keyframes cw-typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-6px); opacity: 1; }
}

/* ==================== INPUT AREA ==================== */

.cw-input-area {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--cw-border);
  background: var(--cw-sidebar-bg);
  flex-shrink: 0;
}

.cw-input {
  flex: 1;
  padding: 9px 14px;
  background: var(--cw-surface);
  border: 1px solid var(--cw-border);
  border-radius: 20px;
  color: var(--cw-text-primary);
  font-size: 13px;
  font-family: inherit;
  outline: none;
  resize: none;
  max-height: 100px;
  overflow-y: auto;
  line-height: 1.4;
  transition: border-color var(--cw-transition);
}

.cw-input:focus {
  border-color: var(--cw-accent);
}

.cw-input::placeholder {
  color: var(--cw-text-muted);
}

.cw-send-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--cw-accent);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background var(--cw-transition), transform var(--cw-transition);
}

.cw-send-btn:hover {
  background: var(--cw-accent-hover);
}

.cw-send-btn:active {
  transform: scale(0.92);
}

.cw-send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.cw-send-btn svg {
  width: 18px;
  height: 18px;
  fill: #fff;
}

/* ==================== SETTINGS PANEL ==================== */

.cw-settings {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--cw-main-bg);
  z-index: 10;
  display: flex;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transform: translateX(100%);
  transition: opacity var(--cw-transition), transform var(--cw-transition);
}

.cw-settings.cw-visible {
  opacity: 1;
  pointer-events: auto;
  transform: translateX(0);
}

.cw-settings-header {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  background: var(--cw-sidebar-bg);
  border-bottom: 1px solid var(--cw-border);
  flex-shrink: 0;
}

.cw-settings-title {
  flex: 1;
  font-size: 14px;
  font-weight: 600;
  color: var(--cw-text-primary);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.cw-settings-body {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
}

.cw-field {
  margin-bottom: 18px;
}

.cw-field-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--cw-text-secondary);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.cw-field-input {
  width: 100%;
  padding: 9px 12px;
  background: var(--cw-surface);
  border: 1px solid var(--cw-border);
  border-radius: 8px;
  color: var(--cw-text-primary);
  font-size: 13px;
  font-family: inherit;
  outline: none;
  transition: border-color var(--cw-transition);
}

.cw-field-input:focus {
  border-color: var(--cw-accent);
}

.cw-field-input::placeholder {
  color: var(--cw-text-muted);
}

/* Password field with show/hide toggle */
.cw-password-wrap {
  position: relative;
}

.cw-password-wrap .cw-field-input {
  padding-right: 40px;
}

.cw-password-toggle {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  color: var(--cw-text-muted);
  font-size: 12px;
  padding: 2px 4px;
  border-radius: 4px;
  font-family: inherit;
  transition: color var(--cw-transition);
}

.cw-password-toggle:hover {
  color: var(--cw-text-primary);
}

/* Provider radio buttons */
.cw-provider-group {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.cw-provider-radio {
  display: none;
}

.cw-provider-label {
  padding: 7px 14px;
  background: var(--cw-surface);
  border: 1px solid var(--cw-border);
  border-radius: 20px;
  color: var(--cw-text-secondary);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--cw-transition);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.cw-provider-radio:checked + .cw-provider-label {
  background: var(--cw-accent-dim);
  border-color: var(--cw-accent);
  color: var(--cw-accent);
}

.cw-provider-label:hover {
  background: var(--cw-surface-hover);
}

/* Settings model dropdown */
.cw-settings-select {
  width: 100%;
  padding: 9px 12px;
  background: var(--cw-surface);
  border: 1px solid var(--cw-border);
  border-radius: 8px;
  color: var(--cw-text-primary);
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  outline: none;
  transition: border-color var(--cw-transition);
  -webkit-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%238E8E8E'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 30px;
}

.cw-settings-select:focus {
  border-color: var(--cw-accent);
}

.cw-settings-select option {
  background: var(--cw-surface);
  color: var(--cw-text-primary);
}

/* Buttons in settings */
.cw-settings-footer {
  padding: 16px;
  border-top: 1px solid var(--cw-border);
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}

.cw-btn {
  padding: 10px 16px;
  border-radius: 8px;
  border: none;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  transition: background var(--cw-transition), transform var(--cw-transition);
  text-align: center;
}

.cw-btn:active {
  transform: scale(0.97);
}

.cw-btn-primary {
  background: var(--cw-accent);
  color: #fff;
}

.cw-btn-primary:hover {
  background: var(--cw-accent-hover);
}

.cw-btn-danger {
  background: rgba(244, 67, 54, 0.12);
  color: var(--cw-danger);
  border: 1px solid rgba(244, 67, 54, 0.25);
}

.cw-btn-danger:hover {
  background: rgba(244, 67, 54, 0.2);
}

/* ==================== MOBILE RESPONSIVE ==================== */

@media (max-width: 768px) {
  .cw-panel {
    right: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    max-height: 100%;
    height: calc(100% - 56px);
    border-radius: var(--cw-radius) var(--cw-radius) 0 0;
    top: 56px;
    transform: translateY(100%);
  }

  .cw-panel.cw-visible {
    transform: translateY(0);
  }

  .cw-toggle-btn {
    bottom: 88px;
  }
}

/* Lead portal mobile: above bottom nav + FAB */
body.lead-portal-context .cw-toggle-btn {
  /* default stays the same on desktop */
}

@media (max-width: 768px) {
  body.lead-portal-context .cw-toggle-btn {
    bottom: 140px;
  }
}

/* ==================== PRINT HIDE ==================== */

@media print {
  .cw-widget,
  .cw-toggle-btn,
  .cw-panel {
    display: none !important;
  }
}
