/* Medical-Grade AI Chat Interface */
.ai-chat-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 420px;
    height: 600px;
    background: #ffffff;
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
    z-index: 9999;
    display: none;
    flex-direction: column;
    border: 1px solid #e1e5e9;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    overflow: hidden;
  }
  
  .ai-chat-container.show {
    display: flex;
    animation: slideUp 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  }
  
  @keyframes slideUp {
    from {
      transform: translateY(100%);
      opacity: 0;
    }
    to {
      transform: translateY(0);
      opacity: 1;
    }
  }
  
  /* Header - Medical Professional Style */
  .chat-header {
    background: linear-gradient(135deg, #1e40af 0%, #3b82f6 100%);
    color: white;
    padding: 16px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-radius: 12px 12px 0 0;
    flex-shrink: 0;
  }
  
  .chat-header h6 {
    color: white;
    margin: 0;
    font-weight: 600;
    font-size: 16px;
    display: flex;
    align-items: center;
  }
  
  .chat-header .ai-icon {
    background: rgba(255, 255, 255, 0.15);
    padding: 8px;
    border-radius: 8px;
    margin-right: 12px;
    font-size: 18px;
  }
  
  .chat-header .header-controls {
    display: flex;
    gap: 8px;
  }
  
  .chat-header .btn {
    padding: 6px 10px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    background: transparent;
    color: white;
    border-radius: 6px;
    font-size: 12px;
    transition: all 0.2s ease;
    cursor: pointer;
  }
  
  .chat-header .btn:hover {
    background: rgba(255, 255, 255, 0.1);
  }

    /* Sessions Sidebar - Horizontal Layout (Only for floating widget) */
    .ai-chat-container .chat-sessions-sidebar {
      width: 100%;
      background: #f8fafc;
      border-bottom: 1px solid #e1e5e9;
      border-right: none;
      display: flex;
      flex-direction: column;
      flex-shrink: 0;
      max-height: 150px;
      overflow: hidden;  /* ADD THIS LINE - prevents content from overflowing */
    }
    
    .ai-chat-container .sessions-header {
      padding: 0px 16px;
      border-bottom: 1px solid #e1e5e9;
      background: white;
      display: flex;
      align-items: center;
      gap: 12px;
      flex-shrink: 0;
    }
    
    .ai-chat-container .sessions-header h6 {
      margin: 0;
      font-size: 14px;
      color: #374151;
      font-weight: 600;
      white-space: nowrap;
    }
    
    .ai-chat-container .new-session-btn {
      padding: 8px 12px;
      background: #3b82f6;
      color: white;
      border: none;
      border-radius: 8px;
      cursor: pointer;
      font-size: 13px;
      font-weight: 500;
      transition: background 0.2s ease;
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 6px;
      white-space: nowrap;
      flex-shrink: 0;
      width: auto;
      max-width: 120px;
    }
    
    .ai-chat-container .new-session-btn:hover {
      background: #2563eb;
    }
    
    .ai-chat-container .sessions-list {
      flex: 1;
      overflow-x: auto;
      overflow-y: hidden;
      padding: 8px;
      display: flex;
      flex-direction: row;
      gap: 8px;
      align-items: center;  /* ADD THIS - vertically centers items */
      height: 100%;         /* ADD THIS - ensure proper height */
    }
  
    /* Horizontal scrollbar styling - only for floating widget */
    .ai-chat-container .sessions-list::-webkit-scrollbar {
      height: 6px;
    }
  
    .ai-chat-container .sessions-list::-webkit-scrollbar-track {
      background: #f1f5f9;
    }
  
    .ai-chat-container .sessions-list::-webkit-scrollbar-thumb {
      background: #cbd5e1;
      border-radius: 3px;
    }
  
    .ai-chat-container .sessions-list::-webkit-scrollbar-thumb:hover {
      background: #94a3b8;
    }
    
    .ai-chat-container .session-item {
      padding: 12px 14px;
      margin-bottom: 0;
      background: white;
      border-radius: 8px;
      cursor: pointer;
      transition: all 0.2s ease;
      border: 1px solid transparent;
      position: relative;
      min-width: 140px;
      max-width: 250px;
      flex-shrink: 0;
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
    
    .ai-chat-container .session-item:hover {
      background: #f1f5f9;
      border-color: #cbd5e1;
      transform: translateY(-2px);
      box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    }
    
    .ai-chat-container .session-item.active {
      background: #dbeafe;
      border-color: #3b82f6;
    }
    
    .ai-chat-container .session-item .session-title {
      font-size: 13px;
      font-weight: 500;
      margin-bottom: 4px;
      color: #374151;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      line-height: 1.5;      /* ADD THIS - proper line height */
      display: block;        /* ADD THIS - ensures proper rendering */
    }
    
    .ai-chat-container .session-item .session-date {
      font-size: 11px;
      color: #6b7280;
      white-space: nowrap;
    }
    
    .ai-chat-container .session-actions {
      position: absolute;
      top: 8px;
      right: 8px;
      opacity: 0;
      transition: opacity 0.2s ease;
    }
    
    .ai-chat-container .session-item:hover .session-actions {
      opacity: 1;
    }
  
  /* Sessions Sidebar - Clean Design */
  .chat-sessions-sidebar {
    width: 180px;
    background: #f8fafc;
    border-right: 1px solid #e1e5e9;
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
  }
  
  .sessions-header {
    padding: 16px;
    border-bottom: 1px solid #e1e5e9;
    background: white;
  }
  
  .sessions-header h6 {
    margin: 0;
    font-size: 14px;
    color: #374151;
    font-weight: 600;
  }
  
  .new-session-btn {
    width: 100%;
    margin: 12px 16px;
    padding: 10px 12px;
    background: #3b82f6;
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    transition: background 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
  }
  
  .new-session-btn:hover {
    background: #2563eb;
  }
  
  .sessions-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
  }
  
  .session-item {
    padding: 12px;
    margin-bottom: 4px;
    background: white;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
    border: 1px solid transparent;
    position: relative;
  }
  
  .session-item:hover {
    background: #f1f5f9;
    border-color: #cbd5e1;
  }
  
  .session-item.active {
    background: #dbeafe;
    border-color: #3b82f6;
  }
  
  .session-item .session-title {
    font-size: 13px;
    font-weight: 500;
    margin-bottom: 4px;
    color: #374151;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  
  .session-item .session-date {
    font-size: 11px;
    color: #6b7280;
  }
  
  .session-actions {
    position: absolute;
    top: 8px;
    right: 8px;
    opacity: 0;
    transition: opacity 0.2s ease;
  }
  
  .session-item:hover .session-actions {
    opacity: 1;
  }
  
  .session-actions .btn {
    padding: 4px 6px;
    font-size: 10px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s ease;
  }
  
  .session-actions .btn-delete {
    background: #ef4444;
    color: white;
  }
  
  .session-actions .btn-delete:hover {
    background: #dc2626;
  }
  
  /* Chat Body - Professional Layout */
  .chat-body-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0; /* Critical for flex layout */
  }
  
  .chat-body {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
    background: #fafafa;
    min-height: 0; /* Critical for flex layout */
  }
  
  /* Messages - Clean Design */
  .message-container {
    margin-bottom: 16px;
  }
  
  .message-content {
    display: flex;
    align-items: flex-start;
    gap: 12px;
  }
  
  .message-content.user {
    flex-direction: row-reverse;
  }
  
  .message-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    flex-shrink: 0;
  }
  
  .message-avatar.ai {
    background: linear-gradient(135deg, #1e40af 0%, #3b82f6 100%);
    color: white;
  }
  
  .message-avatar.user {
    background: #6b7280;
    color: white;
  }
  
  .message-bubble {
    max-width: 75%;
    padding: 12px 16px;
    border-radius: 18px;
    word-wrap: break-word;
    position: relative;
    line-height: 1.4;
  }
  
  .message-bubble.ai {
    background: white;
    color: #374151;
    border: 1px solid #e5e7eb;
    border-bottom-left-radius: 4px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  }
  
  .message-bubble.user {
    background: #3b82f6;
    color: white;
    border-bottom-right-radius: 4px;
  }
  
  .message-time {
    font-size: 11px;
    color: #9ca3af;
    margin-top: 4px;
    text-align: right;
  }
  
  .message-time.user {
    text-align: left;
  }

  /* Headings inside AI message bubbles */
  .message-bubble :is(h1, h2, h3, h4, h5, h6) {
    font-weight: 700;
    color: #1e293b;
    margin-top: 0.85rem;
    margin-bottom: 0.3rem;
    line-height: 1.3;
  }

  .message-bubble :is(h1, h2, h3, h4, h5, h6):first-child {
    margin-top: 0;
  }

  /* Top-level headings get a separator line */
  .message-bubble :is(h1, h2) {
    padding-bottom: 0.2rem;
    border-bottom: 1.5px solid #e5e7eb;
  }

  /* Scaled font sizes per heading level */
  .message-bubble h1 { font-size: 1.3rem; border-bottom-width: 2px; }
  .message-bubble h2 { font-size: 1.15rem; }
  .message-bubble h3 { font-size: 1.05rem; }
  .message-bubble h4 { font-size: 0.975rem; }
  .message-bubble h5 { font-size: 0.925rem; }
  .message-bubble h6 { font-size: 0.875rem; color: #6b7280; }

  /* Typing Indicator */
  .typing-indicator {
    display: none;
    padding: 12px 16px;
    background: white;
    border-radius: 18px;
    border-bottom-left-radius: 4px;
    border: 1px solid #e5e7eb;
    max-width: 80px;
  }
  
  .typing-indicator.show {
    display: block;
  }
  
  .typing-dots {
    display: flex;
    gap: 4px;
  }
  
  .typing-dots span {
    width: 6px;
    height: 6px;
    background: #9ca3af;
    border-radius: 50%;
    animation: typing 1.4s infinite ease-in-out;
  }
  
  .typing-dots span:nth-child(1) { animation-delay: -0.32s; }
  .typing-dots span:nth-child(2) { animation-delay: -0.16s; }
  
  @keyframes typing {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
  }
  
  /* Chat Footer - Fixed Position */
  .chat-footer {
    padding: 16px 20px;
    background: white;
    border-top: 1px solid #e1e5e9;
    flex-shrink: 0; /* Critical - prevents footer from shrinking */
  }
  
  .chat-input-container {
    display: flex;
    gap: 12px;
    align-items: flex-end;
  }
  
  .chat-input {
    flex: 1;
    border: 1px solid #d1d5db;
    border-radius: 24px;
    padding: 12px 16px;
    font-size: 14px;
    resize: none;
    outline: none;
    transition: border-color 0.2s ease;
    max-height: 120px;
    min-height: 44px;
    font-family: inherit;
    line-height: 1.4;
  }
  
  .chat-input:focus {
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
  }
  
  .send-btn {
    width: 44px;
    height: 44px;
    background: #3b82f6;
    color: white;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    flex-shrink: 0;
  }
  
  .send-btn:hover {
    background: #2563eb;
    transform: scale(1.05);
  }
  
  .send-btn:disabled {
    background: #9ca3af;
    cursor: not-allowed;
    transform: none;
  }
  
  /* Patient Context */
  .patient-context {
    background: #eff6ff;
    border: 1px solid #bfdbfe;
    border-radius: 8px;
    padding: 12px;
    margin-bottom: 16px;
    font-size: 13px;
  }
  
  .patient-context .patient-info {
    display: flex;
    align-items: center;
    gap: 10px;
  }
  
  .patient-context .patient-avatar {
    width: 24px;
    height: 24px;
    background: #3b82f6;
    color: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
  }
  
  /* Chat Toggle Button */
  .ai-chat-toggle {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: #3b82f6;
    color: white;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    box-shadow: 0 4px 16px rgba(59, 130, 246, 0.4);
    transition: all 0.3s ease;
    z-index: 9998;
  }
  
  .ai-chat-toggle:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 20px rgba(59, 130, 246, 0.6);
  }
  
  .ai-chat-toggle .badge {
    position: absolute;
    top: -4px;
    right: -4px;
    background: #ef4444;
    color: white;
    border-radius: 50%;
    width: 18px;
    height: 18px;
    font-size: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
  }
  
  /* Welcome Message */
  .welcome-message {
    text-align: center;
    padding: 40px 20px;
    color: #6b7280;
  }
  
  .welcome-message .welcome-icon {
    font-size: 48px;
    color: #3b82f6;
    margin-bottom: 16px;
  }
  
  .welcome-message h5 {
    color: #374151;
    margin-bottom: 16px;
    font-weight: 600;
  }
  
  .welcome-message p {
    font-size: 14px;
    line-height: 1.5;
    margin-bottom: 12px;
  }
  
  .welcome-message ul {
    text-align: left;
    display: inline-block;
    margin: 0;
    padding-left: 20px;
  }
  
  .welcome-message li {
    font-size: 13px;
    color: #6b7280;
    margin-bottom: 4px;
  }
  
  /* Mobile Responsiveness */
  @media (max-width: 768px) {
    .ai-chat-container {
      width: calc(100vw - 20px);
      height: calc(100vh - 100px);
      bottom: 10px;
      right: 10px;
      left: 10px;
    }
    
    .chat-sessions-sidebar {
      display: none;
    }
    
    .ai-chat-toggle {
      bottom: 10px;
      right: 10px;
    }
    
    .chat-header {
      padding: 12px 16px;
    }
    
    .chat-footer {
      padding: 12px 16px;
    }
    
    .chat-body {
      padding: 16px;
    }
  }
  
  /* Accessibility */
  .chat-input:focus,
  .send-btn:focus,
  .new-session-btn:focus,
  .session-item:focus {
    outline: 2px solid #3b82f6;
    outline-offset: 2px;
  }
  
  /* Loading States */
  .loading {
    opacity: 0.6;
    pointer-events: none;
  }
  
  /* Error States */
  .error-message {
    background: #fef2f2;
    border: 1px solid #fecaca;
    color: #dc2626;
    padding: 12px;
    border-radius: 8px;
    margin: 8px 0;
    font-size: 13px;
  }

  /* Deep Learning Mode Toggle */
.deep-mode-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
}

.toggle-label {
  font-size: 12px;
  font-weight: 500;
  transition: color 0.2s ease;
}

.toggle-label.text-success {
  color: #10b981 !important;
}

/* Document Attachment */
.attached-files {
  margin-bottom: 10px;
}

.attached-files .badge {
  font-size: 11px;
  padding: 4px 8px;
}

/* Floating Chat Messages */
.floating-message {
  margin-bottom: 10px;
}

.floating-message.user {
  text-align: right;
}

.floating-message.ai {
  text-align: left;
}

.floating-message-content {
  display: inline-block;
  max-width: 80%;
  padding: 8px 12px;
  border-radius: 12px;
  font-size: 13px;
  line-height: 1.4;
}

.floating-message.user .floating-message-content {
  background: #3b82f6;
  color: white;
}

.floating-message.ai .floating-message-content {
  background: #f1f5f9;
  color: #374151;
}

/* Enhanced Session Items */
.session-item {
  transition: all 0.2s ease;
}

.session-item:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

/* Chat Input Enhancements */
#messageInput {
  border: 2px solid #e1e5e9;
  transition: border-color 0.2s ease;
}

#messageInput:focus {
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

/* Responsive Enhancements */
@media (max-width: 992px) {
  .chat-interface-container .row {
      height: 100%;
  }
  
  .chat-sessions-panel {
      overflow-y: auto;
  }
}

@media (max-width: 768px) {
  .page-header {
      padding: 15px;
  }
  
  .page-title {
      font-size: 1.5rem;
  }
  
  .chat-header {
      padding: 15px;
  }
  
  .chat-messages {
      padding: 15px;
  }
  
  .chat-input-area {
      padding: 15px;
  }
}

/* Hide timestamp placeholder while waiting for response */
.message-time-placeholder {
  display: none;
}