/**
 * TableFlow Chat Widget - Premium Glassmorphism Design
 * "Flow" - The TableFlow Assistant
 * Zero API Cost - Rule-Based Responses
 */

/* ============================================
   CSS Variables
   ============================================ */
:root {
  /* Chat Colors */
  --chat-bg: linear-gradient(135deg, rgba(26, 26, 46, 0.98), rgba(22, 33, 62, 0.98));
  --chat-glass: rgba(255, 255, 255, 0.05);
  --chat-border: rgba(255, 255, 255, 0.1);

  /* Message Colors */
  --user-msg-bg: linear-gradient(135deg, #F49609, #FFB347);
  --bot-msg-bg: rgba(255, 255, 255, 0.08);

  /* Accent Colors */
  --chat-accent-orange: #F49609;
  --chat-accent-pink: #D93151;
  --chat-accent-purple: #2D2E88;

  /* Text Colors */
  --chat-text-primary: #FFFFFF;
  --chat-text-secondary: rgba(255, 255, 255, 0.7);
  --chat-text-muted: rgba(255, 255, 255, 0.5);

  /* Sizing */
  --chat-width: 380px;
  --chat-height: 520px;
  --chat-bubble-size: 60px;
  --chat-border-radius: 20px;
}

/* ============================================
   Chat Bubble (Minimized State)
   ============================================ */
.chat-bubble {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: var(--chat-bubble-size);
  height: var(--chat-bubble-size);
  border-radius: 50%;
  background: var(--chat-bg);
  border: 2px solid transparent;
  background-clip: padding-box;
  cursor: pointer;
  z-index: 9998;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  animation: bubble-pulse 2s infinite;
}

.chat-bubble::before {
  content: '';
  position: absolute;
  inset: -2px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--chat-accent-orange), var(--chat-accent-pink));
  z-index: -1;
}

.chat-bubble:hover {
  transform: scale(1.1);
  box-shadow: 0 12px 40px rgba(244, 150, 9, 0.4);
}

.chat-bubble:focus {
  outline: 3px solid var(--chat-accent-orange);
  outline-offset: 3px;
}

.chat-bubble-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.chat-bubble-avatar {
  width: 28px;
  height: 28px;
  background: linear-gradient(135deg, var(--chat-accent-orange), var(--chat-accent-pink));
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: white;
}

.chat-bubble-label {
  font-size: 9px;
  font-weight: 600;
  color: var(--chat-text-primary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.chat-bubble-dots {
  display: flex;
  gap: 3px;
  position: absolute;
  top: 8px;
  right: 8px;
}

.chat-bubble-dots span {
  width: 6px;
  height: 6px;
  background: var(--chat-accent-orange);
  border-radius: 50%;
  animation: dot-bounce 1.4s infinite ease-in-out;
}

.chat-bubble-dots span:nth-child(1) { animation-delay: 0s; }
.chat-bubble-dots span:nth-child(2) { animation-delay: 0.2s; }
.chat-bubble-dots span:nth-child(3) { animation-delay: 0.4s; }

.chat-bubble.has-notification .chat-bubble-dots {
  display: flex;
}

.chat-bubble:not(.has-notification) .chat-bubble-dots {
  display: none;
}

/* Hide bubble when chat is open */
.chat-bubble.hidden {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.8);
}

/* ============================================
   Proactive Tooltip
   ============================================ */
.chat-tooltip {
  position: fixed;
  bottom: 94px;
  right: 24px;
  background: var(--chat-bg);
  border-radius: 16px;
  padding: 14px 18px;
  max-width: 220px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  z-index: 9997;
  opacity: 0;
  transform: translateY(10px) scale(0.95);
  pointer-events: none;
  transition: all 0.3s ease;
}

.chat-tooltip::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 16px;
  padding: 1px;
  background: linear-gradient(135deg, rgba(244, 150, 9, 0.5), rgba(217, 49, 81, 0.3));
  -webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
  mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
  -webkit-mask-composite: xor;
  mask-composite: exclude;
  pointer-events: none;
}

/* Arrow pointing to bubble */
.chat-tooltip::after {
  content: '';
  position: absolute;
  bottom: -8px;
  right: 24px;
  width: 16px;
  height: 16px;
  background: rgba(26, 26, 46, 0.98);
  transform: rotate(45deg);
  border-radius: 0 0 4px 0;
  box-shadow: 4px 4px 8px rgba(0, 0, 0, 0.2);
}

.chat-tooltip.show {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
  animation: tooltip-bounce 0.5s ease;
}

.chat-tooltip-content {
  position: relative;
  z-index: 1;
}

.chat-tooltip-text {
  color: var(--chat-text-primary);
  font-size: 14px;
  font-weight: 500;
  line-height: 1.4;
  margin: 0;
}

.chat-tooltip-text .wave {
  display: inline-block;
  animation: wave-hand 1s ease infinite;
  transform-origin: 70% 70%;
}

.chat-tooltip-close {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 20px;
  height: 20px;
  border: none;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  color: var(--chat-text-secondary);
  font-size: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  padding: 0;
  line-height: 1;
}

.chat-tooltip-close:hover {
  background: rgba(255, 255, 255, 0.2);
  color: var(--chat-text-primary);
}

/* Hide tooltip when chat is open */
.chat-bubble.hidden ~ .chat-tooltip,
.chat-tooltip.dismissed {
  opacity: 0 !important;
  pointer-events: none !important;
  transform: translateY(10px) scale(0.95) !important;
}

@keyframes tooltip-bounce {
  0% { transform: translateY(10px) scale(0.95); }
  50% { transform: translateY(-5px) scale(1.02); }
  100% { transform: translateY(0) scale(1); }
}

@keyframes wave-hand {
  0%, 100% { transform: rotate(0deg); }
  25% { transform: rotate(20deg); }
  75% { transform: rotate(-10deg); }
}

/* ============================================
   Chat Window
   ============================================ */
.chat-window {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: var(--chat-width);
  height: var(--chat-height);
  max-height: calc(100vh - 48px);
  background: var(--chat-bg);
  border-radius: var(--chat-border-radius);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 9999;
  opacity: 0;
  transform: scale(0.9) translateY(20px);
  pointer-events: none;
  transition: opacity 0.3s ease, transform 0.3s ease;
}

.chat-window::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: var(--chat-border-radius);
  padding: 1px;
  background: linear-gradient(135deg, rgba(244, 150, 9, 0.5), rgba(217, 49, 81, 0.3));
  -webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
  mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
  -webkit-mask-composite: xor;
  mask-composite: exclude;
  pointer-events: none;
}

.chat-window.open {
  opacity: 1;
  transform: scale(1) translateY(0);
  pointer-events: auto;
  animation: chat-open 0.3s ease-out;
}

/* ============================================
   Chat Header
   ============================================ */
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  background: rgba(255, 255, 255, 0.03);
  border-bottom: 1px solid var(--chat-border);
}

.chat-header-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.chat-avatar {
  width: 40px;
  height: 40px;
  background: linear-gradient(135deg, var(--chat-accent-orange), var(--chat-accent-pink));
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  position: relative;
}

.chat-avatar::after {
  content: '';
  position: absolute;
  bottom: 2px;
  right: 2px;
  width: 10px;
  height: 10px;
  background: #22C55E;
  border-radius: 50%;
  border: 2px solid rgba(26, 26, 46, 0.98);
}

.chat-header-text h3 {
  font-size: 16px;
  font-weight: 700;
  color: var(--chat-text-primary);
  margin: 0;
}

.chat-header-text p {
  font-size: 12px;
  color: var(--chat-text-secondary);
  margin: 0;
}

.chat-header-actions {
  display: flex;
  gap: 8px;
}

.chat-header-btn {
  width: 32px;
  height: 32px;
  border: none;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  color: var(--chat-text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

.chat-header-btn:hover {
  background: rgba(255, 255, 255, 0.15);
  color: var(--chat-text-primary);
}

.chat-header-btn:focus {
  outline: 2px solid var(--chat-accent-orange);
  outline-offset: 2px;
}

/* ============================================
   Chat Messages
   ============================================ */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
}

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

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

.chat-messages::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 3px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.3);
}

/* Message Bubbles */
.chat-message {
  display: flex;
  gap: 10px;
  animation: message-in 0.3s ease-out;
}

.chat-message.user {
  flex-direction: row-reverse;
}

.chat-message-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
}

.chat-message.bot .chat-message-avatar {
  background: linear-gradient(135deg, var(--chat-accent-orange), var(--chat-accent-pink));
}

.chat-message.user .chat-message-avatar {
  background: rgba(255, 255, 255, 0.2);
  color: var(--chat-text-primary);
}

.chat-message-content {
  max-width: 80%;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.chat-message-bubble {
  padding: 12px 16px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.5;
}

.chat-message.bot .chat-message-bubble {
  background: var(--bot-msg-bg);
  color: var(--chat-text-primary);
  border-bottom-left-radius: 4px;
  backdrop-filter: blur(10px);
}

.chat-message.user .chat-message-bubble {
  background: var(--user-msg-bg);
  color: #1a1a2e;
  border-bottom-right-radius: 4px;
  font-weight: 500;
}

/* Message text formatting */
.chat-message-bubble strong {
  font-weight: 700;
  color: var(--chat-accent-orange);
}

.chat-message.user .chat-message-bubble strong {
  color: #1a1a2e;
}

.chat-message-bubble ul,
.chat-message-bubble ol {
  margin: 8px 0;
  padding-left: 20px;
}

.chat-message-bubble li {
  margin: 4px 0;
}

/* ============================================
   Quick Actions
   ============================================ */
.chat-quick-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 4px;
}

.chat-quick-action {
  padding: 8px 14px;
  background: rgba(244, 150, 9, 0.15);
  border: 1px solid rgba(244, 150, 9, 0.3);
  border-radius: 20px;
  color: var(--chat-accent-orange);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.chat-quick-action:hover {
  background: rgba(244, 150, 9, 0.25);
  border-color: var(--chat-accent-orange);
  transform: translateY(-1px);
}

.chat-quick-action:focus {
  outline: 2px solid var(--chat-accent-orange);
  outline-offset: 2px;
}

/* Initial quick actions (before any message) */
.chat-initial-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 0 20px 16px;
}

.chat-initial-action {
  padding: 10px 16px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--chat-border);
  border-radius: 24px;
  color: var(--chat-text-primary);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  gap: 6px;
}

.chat-initial-action:hover {
  background: rgba(244, 150, 9, 0.15);
  border-color: rgba(244, 150, 9, 0.3);
  color: var(--chat-accent-orange);
}

.chat-initial-action:focus {
  outline: 2px solid var(--chat-accent-orange);
  outline-offset: 2px;
}

/* ============================================
   Thinking Indicator
   ============================================ */
.chat-thinking {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: var(--bot-msg-bg);
  border-radius: 16px;
  border-bottom-left-radius: 4px;
  width: fit-content;
  animation: message-in 0.3s ease-out;
}

.chat-thinking-icon {
  font-size: 16px;
  animation: thinking-pulse 1.5s infinite;
}

.chat-thinking-text {
  font-size: 14px;
  color: var(--chat-text-secondary);
}

.chat-thinking-dots {
  display: flex;
  gap: 4px;
}

.chat-thinking-dots span {
  width: 6px;
  height: 6px;
  background: var(--chat-text-secondary);
  border-radius: 50%;
  animation: thinking-bounce 1.4s infinite ease-in-out;
}

.chat-thinking-dots span:nth-child(1) { animation-delay: 0s; }
.chat-thinking-dots span:nth-child(2) { animation-delay: 0.2s; }
.chat-thinking-dots span:nth-child(3) { animation-delay: 0.4s; }

/* ============================================
   Chat Input
   ============================================ */
.chat-input-container {
  padding: 16px 20px;
  background: rgba(255, 255, 255, 0.03);
  border-top: 1px solid var(--chat-border);
}

.chat-input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--chat-border);
  border-radius: 24px;
  padding: 8px 16px;
  transition: border-color 0.2s ease;
}

.chat-input-wrapper:focus-within {
  border-color: var(--chat-accent-orange);
}

.chat-input {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--chat-text-primary);
  font-size: 14px;
  font-family: inherit;
  resize: none;
  max-height: 100px;
  line-height: 1.5;
}

.chat-input::placeholder {
  color: var(--chat-text-muted);
}

.chat-input:focus {
  outline: none;
}

.chat-send-btn {
  width: 36px;
  height: 36px;
  border: none;
  background: linear-gradient(135deg, var(--chat-accent-orange), var(--chat-accent-pink));
  border-radius: 50%;
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: transform 0.2s ease, opacity 0.2s ease;
}

.chat-send-btn:hover {
  transform: scale(1.05);
}

.chat-send-btn:focus {
  outline: 2px solid var(--chat-accent-orange);
  outline-offset: 2px;
}

.chat-send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

/* ============================================
   Ticket Escalation Form
   ============================================ */
.chat-ticket-form {
  padding: 16px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 12px;
  margin-top: 8px;
  animation: message-in 0.3s ease-out;
}

.chat-ticket-form h4 {
  font-size: 14px;
  font-weight: 600;
  color: var(--chat-text-primary);
  margin: 0 0 12px;
}

.chat-ticket-field {
  margin-bottom: 12px;
}

.chat-ticket-field label {
  display: block;
  font-size: 12px;
  color: var(--chat-text-secondary);
  margin-bottom: 4px;
}

.chat-ticket-field input,
.chat-ticket-field textarea {
  width: 100%;
  padding: 10px 12px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--chat-border);
  border-radius: 8px;
  color: var(--chat-text-primary);
  font-size: 14px;
  font-family: inherit;
  transition: border-color 0.2s ease;
}

.chat-ticket-field input::placeholder,
.chat-ticket-field textarea::placeholder {
  color: var(--chat-text-muted);
}

.chat-ticket-field input:focus,
.chat-ticket-field textarea:focus {
  outline: none;
  border-color: var(--chat-accent-orange);
}

.chat-ticket-field textarea {
  resize: vertical;
  min-height: 60px;
}

.chat-ticket-submit {
  width: 100%;
  padding: 12px;
  background: linear-gradient(135deg, var(--chat-accent-orange), var(--chat-accent-pink));
  border: none;
  border-radius: 8px;
  color: white;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s ease, opacity 0.2s ease;
}

.chat-ticket-submit:hover {
  transform: translateY(-1px);
}

.chat-ticket-submit:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

/* ============================================
   Animations
   ============================================ */
@keyframes bubble-pulse {
  0%, 100% {
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3), 0 0 0 0 rgba(244, 150, 9, 0.4);
  }
  50% {
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3), 0 0 0 12px rgba(244, 150, 9, 0);
  }
}

@keyframes chat-open {
  from {
    opacity: 0;
    transform: scale(0.9) translateY(20px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

@keyframes message-in {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes thinking-bounce {
  0%, 80%, 100% {
    transform: scale(0.8);
    opacity: 0.5;
  }
  40% {
    transform: scale(1.2);
    opacity: 1;
  }
}

@keyframes thinking-pulse {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}

@keyframes dot-bounce {
  0%, 80%, 100% {
    transform: scale(0);
    opacity: 0;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

/* ============================================
   Responsive Design
   ============================================ */
@media (max-width: 480px) {
  .chat-window {
    bottom: 0;
    right: 0;
    left: 0;
    width: 100%;
    height: 100%;
    max-height: 100vh;
    border-radius: 0;
  }

  .chat-window::before {
    border-radius: 0;
  }

  .chat-bubble {
    bottom: 16px;
    right: 16px;
  }

  :root {
    --chat-border-radius: 0;
  }
}

@media (max-width: 380px) {
  .chat-initial-actions {
    flex-direction: column;
  }

  .chat-initial-action {
    width: 100%;
    justify-content: center;
  }
}

/* ============================================
   Reduced Motion
   ============================================ */
@media (prefers-reduced-motion: reduce) {
  .chat-bubble,
  .chat-window,
  .chat-message,
  .chat-thinking,
  .chat-quick-action,
  .chat-send-btn {
    animation: none;
    transition: none;
  }

  .chat-bubble-dots span,
  .chat-thinking-dots span,
  .chat-thinking-icon {
    animation: none;
  }
}

/* ============================================
   High Contrast Mode
   ============================================ */
@media (prefers-contrast: high) {
  .chat-window {
    border: 2px solid white;
  }

  .chat-message.bot .chat-message-bubble {
    border: 1px solid rgba(255, 255, 255, 0.3);
  }

  .chat-quick-action {
    border-width: 2px;
  }
}
