/**
 * Teleworld AI Floating Support Widget
 * Shown only to logged-in users on every page.
 * Positioning: fixed bottom-right corner.
 */

/* Scoped box-sizing reset — ensures padding is included in width */
#tw-ai-panel,
#tw-ai-panel *,
#tw-ai-panel *::before,
#tw-ai-panel *::after {
  box-sizing: border-box;
}

/* ─── Toggle Button ─── */
#tw-ai-toggle-btn {
  position: fixed;
  bottom: 28px;
  right: 28px;
  z-index: 99990;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: linear-gradient(135deg, #8b5cf6 0%, #06b6d4 100%);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 20px rgba(139, 92, 246, 0.45);
  transition:
    transform 0.25s ease,
    box-shadow 0.25s ease;
  outline: none;
}

#tw-ai-toggle-btn:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 28px rgba(139, 92, 246, 0.6);
}

#tw-ai-toggle-btn:focus-visible {
  outline: 3px solid rgba(139, 92, 246, 0.5);
  outline-offset: 3px;
}

#tw-ai-toggle-btn svg {
  width: 28px;
  height: 28px;
  fill: #ffffff;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

#tw-ai-toggle-btn .tw-icon-close {
  display: none;
}

#tw-ai-toggle-btn.tw-open .tw-icon-chat {
  display: none;
}

#tw-ai-toggle-btn.tw-open .tw-icon-close {
  display: block;
}

/* Notification badge — shown when widget is closed and chat has activity */
#tw-ai-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  width: 18px;
  height: 18px;
  background: #ef4444;
  border-radius: 50%;
  border: 2px solid #0f172a;
  display: none;
  font-size: 0;
}

/* ─── Chat Panel ─── */
#tw-ai-panel {
  position: fixed;
  bottom: 100px;
  right: 28px;
  z-index: 99989;
  width: 370px;
  max-width: calc(100vw - 32px);
  height: 520px;
  max-height: calc(100vh - 130px);
  background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
  border: 1px solid rgba(139, 92, 246, 0.25);
  border-radius: 16px;
  box-shadow:
    0 20px 60px rgba(0, 0, 0, 0.5),
    0 4px 20px rgba(139, 92, 246, 0.15);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  transform: translateY(20px) scale(0.97);
  pointer-events: none;
  transition:
    opacity 0.28s ease,
    transform 0.28s ease;
}

#tw-ai-panel.tw-visible {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

/* ─── Panel Header ─── */
.tw-ai-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  background: linear-gradient(
    135deg,
    rgba(139, 92, 246, 0.2) 0%,
    rgba(6, 182, 212, 0.1) 100%
  );
  border-bottom: 1px solid rgba(139, 92, 246, 0.18);
  flex-shrink: 0;
}

.tw-ai-avatar {
  width: 36px;
  height: 36px;
  background: linear-gradient(135deg, #8b5cf6 0%, #06b6d4 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.tw-ai-avatar svg {
  width: 20px;
  height: 20px;
  fill: #ffffff;
}

.tw-ai-header-info {
  flex: 1;
  min-width: 0;
}

.tw-ai-header-info .tw-ai-name {
  font-size: 0.9rem;
  font-weight: 600;
  color: #f0f9ff;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tw-ai-header-info .tw-ai-status {
  font-size: 0.72rem;
  color: #34d399;
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 2px;
}

.tw-ai-status-dot {
  width: 7px;
  height: 7px;
  background: #34d399;
  border-radius: 50%;
  flex-shrink: 0;
  animation: tw-pulse-dot 2s infinite;
}

@keyframes tw-pulse-dot {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.4;
  }
}

.tw-ai-header-actions {
  display: flex;
  gap: 6px;
  align-items: center;
}

.tw-ai-header-btn {
  background: none;
  border: none;
  padding: 4px;
  cursor: pointer;
  color: #94a3b8;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition:
    color 0.2s ease,
    background 0.2s ease;
  line-height: 1;
}

.tw-ai-header-btn:hover {
  color: #f0f9ff;
  background: rgba(255, 255, 255, 0.08);
}

.tw-ai-header-btn svg {
  width: 16px;
  height: 16px;
  fill: currentColor;
}

/* ─── Messages Area ─── */
#ai-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#ai-chat-messages::-webkit-scrollbar {
  width: 6px;
}

#ai-chat-messages::-webkit-scrollbar-track {
  background: transparent;
}

#ai-chat-messages::-webkit-scrollbar-thumb {
  background: rgba(139, 92, 246, 0.35);
  border-radius: 3px;
}

/* Individual message rows */
.tw-msg {
  display: flex;
  flex-direction: column;
  animation: tw-msg-in 0.25s ease-out;
}

.tw-msg.tw-ai-msg,
.tw-msg.tw-error-msg {
  flex-direction: row;
  align-items: flex-start;
  gap: 8px;
}

.tw-msg.tw-user-msg {
  flex-direction: row-reverse;
  align-items: flex-start;
  gap: 8px;
}

.tw-msg-avatar {
  width: 28px;
  height: 28px;
  min-width: 28px;
  border-radius: 50%;
  background: linear-gradient(135deg, #8b5cf6 0%, #06b6d4 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  flex-shrink: 0;
  margin-top: 2px;
}

.tw-msg-content {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
  max-width: calc(100% - 36px);
}

@keyframes tw-msg-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.tw-msg.tw-user-msg .tw-msg-content {
  align-items: flex-end;
  flex: unset;
  max-width: calc(100% - 36px);
}

.tw-msg.tw-user-msg .tw-msg-avatar {
  background: linear-gradient(135deg, #7c3aed 0%, #8b5cf6 100%);
}

.tw-msg-bubble {
  max-width: 82%;
  padding: 9px 13px;
  border-radius: 14px;
  font-size: 0.85rem;
  line-height: 1.5;
  word-break: break-word;
}

.tw-msg.tw-ai-msg .tw-msg-bubble {
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid rgba(139, 92, 246, 0.2);
  color: #e2e8f0;
  border-bottom-left-radius: 4px;
}

.tw-msg.tw-user-msg .tw-msg-bubble {
  background: linear-gradient(135deg, #8b5cf6 0%, #06b6d4 100%);
  color: #ffffff;
  border-bottom-right-radius: 4px;
}

.tw-msg.tw-error-msg .tw-msg-bubble {
  background: rgba(239, 68, 68, 0.12);
  border: 1px solid rgba(239, 68, 68, 0.3);
  color: #fca5a5;
  border-bottom-left-radius: 4px;
}

.tw-msg.tw-system-msg {
  justify-content: center;
}

.tw-msg.tw-system-msg .tw-msg-bubble {
  max-width: 90%;
  background: rgba(59, 130, 246, 0.1);
  border: 1px solid rgba(59, 130, 246, 0.2);
  color: #93c5fd;
  font-size: 0.8rem;
  text-align: center;
  border-radius: 10px;
}

.tw-msg-meta {
  font-size: 0.58rem;
  color: #475569;
  margin-top: 1px;
  padding: 0 4px;
  text-align: left;
  line-height: 1;
}

.tw-msg.tw-user-msg .tw-msg-meta {
  text-align: right;
}

/* Typing indicator */
.tw-typing-indicator {
  flex-direction: row !important;
  align-items: flex-start !important;
  gap: 8px !important;
}

.tw-typing-indicator .tw-msg-content {
  flex-direction: row;
  align-items: center;
  gap: 5px;
  padding: 4px 14px;
}

.tw-typing-indicator .tw-msg-bubble {
  display: none;
}

.tw-typing-dot {
  width: 8px;
  height: 8px;
  background: #a78bfa;
  border-radius: 50%;
  animation: tw-typing-bounce 1.2s infinite;
}

.tw-typing-dot:nth-child(2) {
  animation-delay: 0.2s;
}
.tw-typing-dot:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes tw-typing-bounce {
  0%,
  80%,
  100% {
    transform: translateY(0);
    opacity: 0.5;
  }
  40% {
    transform: translateY(-5px);
    opacity: 1;
  }
}

/* ─── Quick Actions ─── */
.tw-quick-actions {
  padding: 6px 14px;
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  flex-shrink: 0;
}

.tw-quick-btn {
  background: rgba(139, 92, 246, 0.1);
  border: 1px solid rgba(139, 92, 246, 0.25);
  color: #a78bfa;
  border-radius: 20px;
  padding: 4px 11px;
  font-size: 0.73rem;
  cursor: pointer;
  transition:
    background 0.2s ease,
    color 0.2s ease;
  white-space: nowrap;
  line-height: 1.5;
}

.tw-quick-btn:hover {
  background: rgba(139, 92, 246, 0.22);
  color: #c4b5fd;
}

/* ─── Input Area ─── */
.tw-ai-footer {
  padding: 10px 12px 12px;
  border-top: 1px solid rgba(139, 92, 246, 0.15);
  flex-shrink: 0;
  width: 100%;
}

.tw-ai-input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(139, 92, 246, 0.2);
  border-radius: 12px;
  padding: 12px 12px;
  transition: border-color 0.2s ease;
  width: 100%;
  min-height: 50px;
}

.tw-ai-input-row:focus-within {
  border-color: rgba(139, 92, 246, 0.5);
}

#ai-chat-input {
  flex: 1;
  background: none;
  border: none;
  outline: none;
  color: #e2e8f0;
  font-size: 0.88rem;
  line-height: 1.5;
  resize: none;
  min-height: 34px;
  max-height: 100px;
  padding: 4px 0;
  font-family: inherit;
}

#ai-chat-input::placeholder {
  color: #475569;
}

.tw-input-action-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px;
  border-radius: 8px;
  color: #64748b;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition:
    color 0.2s ease,
    background 0.2s ease;
  line-height: 1;
}

.tw-input-action-btn:hover {
  color: #a78bfa;
  background: rgba(139, 92, 246, 0.12);
}

.tw-input-action-btn.recording,
#ai-voice-btn.recording {
  color: #ef4444;
  animation: tw-pulse-recording 1s infinite;
}

@keyframes tw-pulse-recording {
  0%,
  100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.15);
    opacity: 0.8;
  }
}

.tw-input-action-btn svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
}

#ai-send-btn {
  background: linear-gradient(135deg, #8b5cf6 0%, #06b6d4 100%);
  border: none;
  border-radius: 8px;
  width: 34px;
  height: 34px;
  padding: 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition:
    transform 0.15s ease,
    opacity 0.15s ease;
  line-height: 1;
}

#ai-send-btn:hover {
  transform: scale(1.08);
}

#ai-send-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

#ai-send-btn svg {
  width: 18px;
  height: 18px;
  fill: #ffffff;
  pointer-events: none;
}

#ai-voice-btn.tw-voice-error {
  color: #f59e0b;
  animation: tw-pulse-recording 1s infinite;
}

.tw-ai-voice-status {
  font-size: 0.7rem;
  color: #a78bfa;
  text-align: center;
  margin-top: 5px;
  min-height: 14px;
}

/* ─── Responsive ─── */

/* RES-14 FIX: Tablet breakpoint */
@media (max-width: 768px) {
  #tw-ai-panel {
    width: 340px;
    max-width: 90vw;
    height: 55vh;
    max-height: 55vh;
  }

  .tw-quick-actions {
    flex-wrap: wrap;
  }

  .tw-quick-btn {
    font-size: 0.8rem;
    padding: 0.35rem 0.65rem;
  }
}

@media (max-width: 480px) {
  #tw-ai-panel {
    right: 0;
    left: 0;
    bottom: 0;
    width: 100%;
    max-width: 100%;
    height: 80vh;
    max-height: 80vh;
    border-radius: 16px 16px 0 0;
  }

  #tw-ai-toggle-btn {
    right: 16px;
    bottom: 16px;
  }
}
