/* ============================================
   CHAT DRAWER - Always a floating drawer
   ============================================ */

/* Chat sheet uses standard .slide-up-sheet from sheets.css */
#chat-view-sheet-overlay {
  /* Inherits from .slide-up-sheet in sheets.css */
}

/* Content inside sheet */
.chat-view-content {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-height: 100%;
  background: var(--brokerly-secondary-background);
  position: relative;
  overflow: hidden; /* Prevent content from overflowing the sheet */
}

/* ============================================
   HEADER
   ============================================ */

.chat-header-ios {
  background: var(--brokerly-secondary-background);
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  flex-shrink: 0;
  position: relative;
}

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

/* Mobile: simpler header within sheet */
@media (max-width: 768px) {
  .chat-header-content {
    padding: 8px 16px;
  }
}

/* AI Broker Avatar - 40×40px with multicolored gradient border */
.ai-broker-avatar {
  position: relative;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  padding: 2px;
  background: linear-gradient(135deg, 
    #FF6B6B 0%,
    #FFD93D 12.5%,
    #6BCF7F 25%,
    #4D96FF 37.5%,
    #9D4EDD 50%,
    #FF6B9D 62.5%,
    #FFB347 75%,
    #7B68EE 87.5%,
    #FF6B6B 100%
  );
}

.ai-broker-avatar .avatar-circle-40 {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--brokerly-primary);
  display: flex;
  align-items: center;
  justify-content: center;
}

.ai-broker-avatar .avatar-initial {
  font-size: 20px;
  font-weight: 500;
  color: white;
}

/* Broker Name & Status */
.broker-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.broker-name {
  font-size: 17px;
  font-weight: 600;
  color: var(--brokerly-text-primary);
  margin: 0;
}

.online-status {
  display: flex;
  align-items: center;
  gap: 4px;
}

.status-dot-green {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #50C878;
  box-shadow: 0 0 4px rgba(80, 200, 120, 0.5);
}

.status-text {
  font-size: 11px;
  font-weight: 500;
  color: #666666;
}

.header-spacer {
  flex: 1;
}

/* Video Call Button - 24px icon, disabled */
.video-call-button {
  width: 44px;
  height: 44px;
  border: none;
  background: transparent;
  color: var(--brokerly-primary);
  cursor: not-allowed;
  opacity: 0.6;
  display: flex;
  align-items: center;
  justify-content: center;
}

.video-call-button svg {
  width: 24px;
  height: 24px;
}

/* Profile Button - 40×40px */
.profile-button-chat {
  border: none;
  background: transparent;
  padding: 0;
  cursor: pointer;
}

.profile-avatar-40 {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(26, 127, 230, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
}

.profile-avatar-40 .profile-initial {
  font-size: 17px;
  font-weight: 600;
  color: var(--brokerly-primary);
}

/* ============================================
   MESSAGES LIST - Lines 150-182 iOS
   ============================================ */

.chat-messages-ios {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  background: rgba(128, 128, 128, 0.05);
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 0; /* Important for flex child scrolling */
  pointer-events: auto; /* Ensure messages area captures touch events */
  touch-action: pan-y; /* Allow vertical scrolling only */
}

/* Message Container */
.message {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 100%;
}

/* User Message - Right-aligned, Lines 402-424 iOS */
.user-message {
  flex-direction: row-reverse;
  margin-left: 50px;
}

.user-bubble {
  background: var(--modern-card-background); /* White background */
  color: var(--brokerly-text-primary); /* Dark text */
  padding: 12px 16px;
  border-radius: 20px;
  max-width: 100%;
  word-wrap: break-word;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04); /* Subtle shadow like AI bubbles */
}

.user-bubble .bubble-content {
  font-size: 17px;
  font-weight: 400;
  line-height: 1.5;
}

.user-bubble .bubble-content p {
  margin: 0;
}

.user-bubble .bubble-content p + p {
  margin-top: 8px;
}

.user-bubble .bubble-content h1,
.user-bubble .bubble-content h2,
.user-bubble .bubble-content h3 {
  margin: 12px 0 8px 0;
  font-weight: 600;
  color: var(--brokerly-text-primary); /* Ensure headings are dark */
}

.user-bubble .bubble-content code {
  background: rgba(0, 0, 0, 0.05); /* Light gray on white */
  color: var(--brokerly-text-primary);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'SF Mono', 'Monaco', 'Courier New', monospace;
  font-size: 15px;
}

.user-bubble .bubble-content pre {
  background: rgba(0, 0, 0, 0.05); /* Light gray on white */
  color: var(--brokerly-text-primary);
  padding: 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 8px 0;
}

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

.user-bubble .bubble-content li {
  margin: 4px 0;
}

.user-avatar-small {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: rgba(26, 127, 230, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.user-avatar-small span {
  font-size: 11px;
  font-weight: 600;
  color: var(--brokerly-primary);
}

/* AI Message - Left-aligned, Lines 425-468 iOS */
.ai-message {
  flex-direction: column;
  align-items: flex-start;
  margin-right: 50px;
}

.ai-message-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}

.ai-avatar-small {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--brokerly-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.ai-avatar-small span {
  font-size: 12px;
  font-weight: 500;
  color: white;
}

/* AI Avatar Images */
.ai-avatar-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

.ai-message-avatar-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

.speaker-button {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: #666666;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  border-radius: 50%;
}

.speaker-button:hover {
  color: var(--brokerly-primary);
  background: rgba(26, 127, 230, 0.1);
}

/* Speaking state - show pause icon and animate */
.speaker-button.speaking {
  color: var(--brokerly-primary);
  background: rgba(26, 127, 230, 0.15);
  animation: pulse-speaker 1.5s ease-in-out infinite;
}

@keyframes pulse-speaker {
  0%, 100% {
    background: rgba(26, 127, 230, 0.15);
    transform: scale(1);
  }
  50% {
    background: rgba(26, 127, 230, 0.25);
    transform: scale(1.05);
  }
}

/* Default AI bubble - Clive's purple gradient */
.ai-bubble,
.ai-bubble.clive {
  background: var(--clive-gradient);
  color: white;
  padding: 12px 16px;
  border-radius: 20px;
  max-width: 85%; /* Only applies to bubble, not siblings */
  word-wrap: break-word;
  box-shadow: 0 4px 12px var(--clive-glow);
}

/* Clara's teal gradient */
.ai-bubble.clara {
  background: var(--clara-gradient) !important;
  box-shadow: 0 4px 12px var(--clara-glow) !important;
}

/* Ensure chart containers can be full width */
.message.ai-message > .ai-chart-container {
  max-width: 100%;
  width: 100%;
}

.ai-bubble .bubble-content {
  font-size: 17px;
  font-weight: 400;
  line-height: 1.5;
}

.ai-bubble .bubble-content p {
  margin: 0;
}

.ai-bubble .bubble-content p + p {
  margin-top: 8px;
}

.ai-bubble .bubble-content h1,
.ai-bubble .bubble-content h2,
.ai-bubble .bubble-content h3 {
  margin: 12px 0 8px 0;
  font-weight: 600;
  color: white; /* Ensure headings are white */
}

.ai-bubble .bubble-content code {
  background: rgba(255, 255, 255, 0.2); /* Light overlay on blue */
  color: white;
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'SF Mono', 'Monaco', 'Courier New', monospace;
  font-size: 15px;
}

.ai-bubble .bubble-content pre {
  background: rgba(255, 255, 255, 0.15); /* Light overlay on blue */
  color: white;
  padding: 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 8px 0;
}

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

.ai-bubble .bubble-content li {
  margin: 4px 0;
}

.ai-bubble .bubble-content strong,
.ai-bubble .bubble-content b {
  color: white; /* Ensure bold text is white */
}

/* Typing Indicator - 3 Pulsing Dots */
.typing-bubble {
  padding: 16px;
  width: auto;
  display: inline-block;
  /* Background inherited from .ai-bubble gradient */
}

.typing-dots {
  display: flex;
  gap: 6px;
  align-items: center;
}

.typing-dots span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.8); /* White dots on purple gradient background */
  animation: typingDot 1.4s ease-in-out infinite;
}

.typing-dots span:nth-child(1) {
  animation-delay: 0s;
}

.typing-dots span:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-dots span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes typingDot {
  0%, 60%, 100% {
    opacity: 0.3;
    transform: scale(1);
  }
  30% {
    opacity: 1;
    transform: scale(1.2);
  }
}

/* ============================================
   INPUT INTERFACE - Lines 184-261 iOS
   ============================================ */

.chat-input-interface {
  background: var(--brokerly-secondary-background);
  position: relative;
  flex-shrink: 0;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

/* Mobile: Make input bigger with better UX */
@media (max-width: 768px) {
  .chat-input-interface {
    position: sticky;
    bottom: 0;
    z-index: 10;
    padding: 16px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    background: var(--brokerly-secondary-background);
    border-top: 1px solid rgba(0, 0, 0, 0.1);
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.05);
  }
}

.input-divider {
  height: 1px;
  background: rgba(0, 0, 0, 0.05);
}

.input-container {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
}

/* Mobile: Bigger, better input container */
@media (max-width: 768px) {
  .input-container {
    gap: 16px;
    padding: 0; /* Padding now on parent .chat-input-interface */
  }
}

/* Text Input Wrapper - 44px height, pill shape */
.text-input-wrapper {
  flex: 1;
  position: relative;
  display: flex;
  align-items: center;
  height: 44px;
  background: var(--brokerly-input-background);
  border: 1px solid var(--brokerly-border-subtle);
  border-radius: 22px;
  overflow: hidden;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.text-input-wrapper:focus-within {
  border-color: var(--brokerly-primary);
  box-shadow: 0 0 0 3px rgba(26, 127, 230, 0.1);
}

/* Mobile: Bigger input for better touch */
@media (max-width: 768px) {
  .text-input-wrapper {
    height: 52px;
    border-radius: 26px;
    border-width: 1.5px;
  }
}

.chat-text-input {
  flex: 1;
  height: 100%;
  border: none;
  background: transparent;
  padding: 0 16px;
  font-size: 17px;
  font-weight: 400;
  color: var(--brokerly-text-primary);
  outline: none;
  position: relative;
  z-index: 2;
}

/* Mobile: Bigger text for better readability */
@media (max-width: 768px) {
  .chat-text-input {
    font-size: 18px;
    padding: 0 20px;
  }
}

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

/* Waveform Container - 24 bars */
.waveform-container {
  position: absolute;
  left: 16px;
  right: 48px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 2px;
  z-index: 1;
  pointer-events: none; /* Allow clicks to pass through to input */
}

.waveform-bar {
  flex: 1;
  background: var(--brokerly-primary);
  border-radius: 2px;
  min-height: 2px;
  transition: height 0.05s ease-out;
}

/* Clear Button - X icon, 20px */
.clear-button {
  position: absolute;
  right: 8px;
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--brokerly-text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.15s ease;
}

.clear-button:hover {
  opacity: 0.7;
}

/* Voice Button - 44×44px circle */
.voice-button {
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 50%;
  background: rgba(128, 128, 128, 0.1);
  color: var(--brokerly-text-primary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.1s ease-out;
}

.voice-button:hover {
  background: rgba(128, 128, 128, 0.15);
}

.voice-button.recording {
  background: rgba(255, 0, 0, 0.1);
  color: #FF0000;
  transform: scale(1.1);
}

.voice-button svg {
  width: 20px;
  height: 20px;
}

/* Send Button - 44×44px circle, brokerlyPrimary */
.send-button-ios {
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 50%;
  background: var(--brokerly-primary);
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s ease-out;
  flex-shrink: 0;
}

.send-button-ios:hover:not(:disabled) {
  transform: scale(1.05);
}

.send-button-ios:active:not(:disabled) {
  transform: scale(0.95);
}

.send-button-ios:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.send-button-ios svg {
  width: 20px;
  height: 20px;
}

/* Mobile: Bigger send button for better touch */
@media (max-width: 768px) {
  .send-button-ios {
    width: 52px;
    height: 52px;
    min-width: 52px;
    min-height: 52px;
  }
  
  .send-button-ios svg {
    width: 24px;
    height: 24px;
  }
}

/* ============================================
   RESPONSIVE ADJUSTMENTS
   ============================================ */

@media (max-width: 768px) {
  .user-message {
    margin-left: 20px;
  }
  
  .ai-message {
    margin-right: 20px;
  }
  
  .chat-messages-ios {
    padding: 12px;
    gap: 12px;
  }
}


/* ============================================
   TYPING INDICATOR
   ============================================ */

.typing-indicator-ios {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  margin: 4px 0;
  opacity: 0;
  animation: fadeIn 0.3s ease-in forwards;
}

.typing-indicator-ios.hidden {
  display: none;
}

.message-avatar-small {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: linear-gradient(135deg, 
    #FF6B6B 0%,
    #FFD93D 12.5%,
    #6BCF7F 25%,
    #4D96FF 37.5%,
    #9D4EDD 50%,
    #FF6B9D 62.5%,
    #FFB347 75%,
    #7B68EE 87.5%,
    #FF6B6B 100%
  );
  padding: 2px;
  flex-shrink: 0;
}

.message-avatar-small-inner {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--brokerly-primary);
  display: flex;
  align-items: center;
  justify-content: center;
}

.message-avatar-small-letter {
  font-size: 12px;
  font-weight: 600;
  color: white;
}

.typing-dots,
.typing-dots.clive {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 12px 16px;
  background: var(--clive-gradient);
  border-radius: 18px;
  min-width: 50px;
  box-shadow: 0 4px 12px var(--clive-glow);
}

.typing-dots.clara {
  background: var(--clara-gradient) !important;
  box-shadow: 0 4px 12px var(--clara-glow) !important;
}

.typing-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.8); /* White dots on blue background */
  animation: typingPulse 1.4s infinite ease-in-out;
}

.typing-dot:nth-child(1) {
  animation-delay: 0s;
}

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

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

@keyframes typingPulse {
  0%, 60%, 100% {
    opacity: 0.3;
    transform: scale(0.8);
  }
  30% {
    opacity: 1;
    transform: scale(1);
  }
}
