/* =============================================================================
   CHAT WIDGET - Floating Chat Interface (2-tilainen: FAB + Mini)
   ============================================================================= */

/* FAB (Floating Action Button) */
.chat-fab {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 56px;
  height: 56px;
  background: var(--primary-strong);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: var(--shadow-lg);
  z-index: var(--z-toast);
  transition: all var(--transition-fast);
  color: white;
  border: none;
}

.chat-fab:hover {
  transform: scale(1.1);
  box-shadow: var(--shadow-xl);
  background: var(--primary-hover);
}

.chat-fab:active {
  transform: scale(0.95);
}

.chat-fab__badge {
  position: absolute;
  top: -4px;
  right: -4px;
  background: var(--error);
  color: white;
  border-radius: 50%;
  min-width: 20px;
  height: 20px;
  padding: 2px 6px;
  font-size: var(--text-xs);
  font-weight: var(--font-bold);
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid white;
  animation: badgePulse 2s ease-in-out infinite;
}

@keyframes badgePulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

/* Mini Chat Container */
.chat-mini {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 400px;
  height: 600px;
  background: var(--bg-primary);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-xl);
  display: flex;
  flex-direction: column;
  z-index: var(--z-toast);
  transform: scale(0.8);
  opacity: 0;
  transform-origin: bottom right;
  transition: all var(--transition-base);
  border: 1px solid var(--border);
  overflow: hidden;
}

.chat-mini--open {
  transform: scale(1);
  opacity: 1;
}

/* Header */
.chat-mini__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--spacing-4);
  border-bottom: 1px solid var(--border);
  background: var(--primary-strong);
  color: white;
  flex-shrink: 0;
}

.chat-mini__title {
  font-weight: var(--font-semibold);
  font-size: var(--text-base);
}

.chat-mini__controls {
  display: flex;
  gap: var(--spacing-1);
}

.chat-control-btn {
  background: none;
  border: none;
  color: white;
  cursor: pointer;
  padding: var(--spacing-2);
  border-radius: var(--radius-md);
  transition: background var(--transition-fast);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
}

.chat-control-btn:hover {
  background: rgba(255, 255, 255, 0.15);
}

.chat-control-btn:active {
  background: rgba(255, 255, 255, 0.25);
}

/* Body */
.chat-mini__body {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--bg-secondary);
}

/* Conversations List */
.chat-conversations {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

/* 🤖 UUSI: Aava AI Coach -nappi */
.chat-aava-button {
  display: flex;
  align-items: center;
  gap: var(--spacing-3);
  padding: var(--spacing-4);
  cursor: pointer;
  transition: all var(--transition-fast);
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  margin: var(--spacing-3);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
}

.chat-aava-button:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
  background: linear-gradient(135deg, #5a67d8 0%, #6b46c1 100%);
}

.chat-aava-button:active {
  transform: translateY(0);
  box-shadow: var(--shadow-sm);
}

.chat-aava-button__icon {
  font-size: 2rem;
  flex-shrink: 0;
  animation: float 3s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-5px); }
}

.chat-aava-button__content {
  flex: 1;
  min-width: 0;
}

.chat-aava-button__title {
  font-weight: var(--font-bold);
  font-size: var(--text-base);
  margin-bottom: var(--spacing-1);
}

.chat-aava-button__subtitle {
  font-size: var(--text-xs);
  opacity: 0.9;
  line-height: 1.3;
}

.chat-aava-button__arrow {
  flex-shrink: 0;
  opacity: 0.7;
  transition: transform var(--transition-fast);
}

.chat-aava-button:hover .chat-aava-button__arrow {
  transform: translateX(4px);
}

/* 🤖 UUSI: Erotin Aavan ja viestien välillä */
.chat-conversations__divider {
  padding: var(--spacing-2) var(--spacing-4);
  font-size: var(--text-xs);
  font-weight: var(--font-semibold);
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}

.chat-conversations__loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--spacing-3);
  padding: var(--spacing-6);
  color: var(--text-muted);
  font-size: var(--text-sm);
}

.chat-conversations__empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--spacing-3);
  padding: var(--spacing-6);
  text-align: center;
  color: var(--text-muted);
  flex: 1;
}

.chat-conversations__empty p {
  font-size: var(--text-sm);
  color: var(--text-secondary);
  margin: 0;
}

.chat-conversations__empty svg {
  color: var(--text-muted);
}

/* Conversation Item */
.chat-conversation-item {
  display: flex;
  gap: var(--spacing-3);
  padding: var(--spacing-3) var(--spacing-4);
  cursor: pointer;
  transition: background var(--transition-fast);
  border-bottom: 1px solid var(--border);
  background: var(--bg-primary);
}

.chat-conversation-item:hover {
  background: var(--bg-hover);
}

.chat-conversation-item:active {
  background: var(--bg-secondary);
}

.chat-conversation-item.unread {
  background: rgba(249, 115, 22, 0.05);
}

.chat-conversation-item.unread:hover {
  background: rgba(249, 115, 22, 0.08);
}

.chat-conversation-item__avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--primary-strong);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: var(--font-bold);
  font-size: var(--text-sm);
  flex-shrink: 0;
}

.chat-conversation-item__content {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: var(--spacing-1);
}

.chat-conversation-item__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--spacing-2);
}

.chat-conversation-item__name {
  font-weight: var(--font-semibold);
  font-size: var(--text-sm);
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-conversation-item__time {
  font-size: var(--text-xs);
  color: var(--text-muted);
  flex-shrink: 0;
}

.chat-conversation-item__footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--spacing-2);
}

.chat-conversation-item__preview {
  font-size: var(--text-xs);
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.chat-conversation-item.unread .chat-conversation-item__preview {
  font-weight: var(--font-semibold);
  color: var(--text-primary);
}

.chat-conversation-item__badge {
  background: var(--primary-strong);
  color: white;
  border-radius: 10px;
  padding: 2px 6px;
  font-size: 0.625rem;
  font-weight: var(--font-bold);
  min-width: 18px;
  text-align: center;
  flex-shrink: 0;
}

/* Messages View */
.chat-messages {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-messages__header {
  display: flex;
  align-items: center;
  gap: var(--spacing-3);
  padding: var(--spacing-3) var(--spacing-4);
  border-bottom: 1px solid var(--border);
  background: var(--bg-primary);
  flex-shrink: 0;
}

/* 🆕 UUSI: Varmista että controls eivät kutista */
.chat-messages__header .chat-mini__controls {
  flex-shrink: 0;
  margin-left: auto;
}

/* 🆕 UUSI: Tumma väri message-headerin napeille (valkoinen tausta) */
.chat-messages__header .chat-control-btn {
  color: var(--text-secondary);
}

.chat-messages__header .chat-control-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.chat-back-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: var(--spacing-1);
  border-radius: var(--radius-md);
  transition: all var(--transition-fast);
  display: flex;
  align-items: center;
  gap: var(--spacing-1);
  font-size: var(--text-sm);
}

.chat-back-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.chat-messages__recipient {
  display: flex;
  align-items: center;
  gap: var(--spacing-2);
  flex: 1;
}

.chat-recipient__avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--primary-strong);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: var(--font-bold);
  font-size: 0.75rem;
  flex-shrink: 0;
}

.chat-recipient__name {
  font-weight: var(--font-semibold);
  font-size: var(--text-sm);
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Messages List */
.chat-messages__list {
  flex: 1;
  overflow-y: auto;
  padding: var(--spacing-4);
  display: flex;
  flex-direction: column;
  gap: var(--spacing-3);
  background: var(--bg-secondary);
}

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

.chat-messages__list::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

.chat-messages__list::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

.chat-messages__empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--spacing-2);
  text-align: center;
  color: var(--text-muted);
  flex: 1;
  padding: var(--spacing-6);
}

.chat-messages__empty p {
  font-size: var(--text-sm);
  color: var(--text-secondary);
  margin: 0;
}

.chat-messages__empty small {
  font-size: var(--text-xs);
  color: var(--text-muted);
}

/* Message Bubble */
.chat-message {
  display: flex;
  animation: messageSlideIn 0.3s ease-out;
}

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

.chat-message--own {
  justify-content: flex-end;
}

.chat-message__bubble {
  max-width: 70%;
  padding: var(--spacing-3);
  border-radius: var(--radius-lg);
  word-wrap: break-word;
  box-shadow: var(--shadow-sm);
}

.chat-message--own .chat-message__bubble {
  background: var(--primary-strong);
  color: white;
  border-bottom-right-radius: var(--radius-sm);
}

.chat-message--other .chat-message__bubble {
  background: var(--bg-primary);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-bottom-left-radius: var(--radius-sm);
}

.chat-message__content {
  font-size: var(--text-sm);
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}

.chat-message__time {
  font-size: 0.6875rem;
  margin-top: var(--spacing-1);
  opacity: 0.7;
}

/* Footer (Input Area) */
.chat-mini__footer {
  padding: var(--spacing-3);
  border-top: 1px solid var(--border);
  display: flex;
  gap: var(--spacing-2);
  align-items: flex-end;
  flex-shrink: 0;
  background: var(--bg-primary);
}

/* Input */
.chat-input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--spacing-3);
  font-size: var(--text-sm);
  font-family: inherit;
  background: var(--bg-secondary);
  color: var(--text-primary);
  resize: none;
  max-height: 100px;
  min-height: 40px;
  transition: all var(--transition-fast);
  line-height: 1.5;
}

.chat-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(249, 115, 22, 0.1);
  background: var(--bg-primary);
}

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

.chat-input:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* Send Button */
.chat-send-btn {
  background: var(--primary-strong);
  border: none;
  color: white;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-lg);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition-fast);
  flex-shrink: 0;
}

.chat-send-btn:hover {
  background: var(--primary-hover);
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

.chat-send-btn:active {
  transform: translateY(0);
}

.chat-send-btn:disabled {
  background: var(--bg-tertiary);
  color: var(--text-muted);
  cursor: not-allowed;
  transform: none;
  opacity: 0.6;
}

/* Loading Spinner */
.spinner {
  width: 24px;
  height: 24px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Responsive */
@media (max-width: 768px) {
  .chat-fab {
    width: 52px;
    height: 52px;
    bottom: 20px;
    right: 20px;
  }

  /* Alapalkin yläpuolelle, ettei painike peitä välilehtiä */
  body.has-bottom-nav .chat-fab {
    bottom: calc(var(--tabbar-height) + 12px + env(safe-area-inset-bottom, 0px));
    right: 16px;
  }

  .chat-mini {
    width: 100%;
    height: 100%;
    bottom: 0;
    right: 0;
    border-radius: 0;
    max-height: 100vh;
  }

  .chat-mini--open {
    transform: scale(1);
  }

  .chat-message__bubble {
    max-width: 80%;
  }

  .chat-conversation-item {
    padding: var(--spacing-3);
  }
}

@media (max-width: 480px) {
  .chat-fab {
    width: 48px;
    height: 48px;
    bottom: 16px;
    right: 16px;
  }

  .chat-fab__badge {
    top: -2px;
    right: -2px;
    min-width: 18px;
    height: 18px;
    font-size: 0.625rem;
  }
}
