#buddy-chat-widget {
  --buddy-primary-color: {
      {
      block.settings.primary-color
    }
  }

  ;

  --buddy-icon-offset: {
      {
      block.settings.offset
    }
  }

  %;
}

#buddy-chat-toggle {
  position: fixed;
  background: var(--buddy-primary-color);
  color: #fff;
  border-radius: 50%;
  width: 50px;
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  z-index: 9999998;
  transition: opacity 0.3s ease;
}

#buddy-chat-toggle.hidden {
  opacity: 0;
  pointer-events: none;
}

#buddy-chat-toggle.buddy__style__field {
  width: auto;
  height: auto;
  padding: 2rem 0.5rem;
  font-weight: bold;
  font-size: 0.95rem;
}

#buddy-chat-toggle.buddy__style__field.buddy__left {
  writing-mode: vertical-rl;
  transform: rotate(180deg) translateY(50%);
  border-radius: 10px 0 0 10px;
}

#buddy-chat-toggle.buddy__style__field.buddy__bottom {
  border-radius: 10px 10px 0 0;
}

#buddy-chat-toggle.buddy__style__field.buddy__right {
  transform: rotate(180deg) translateY(50%);
  writing-mode: vertical-rl;
  border-radius: 0 10px 10px 0;
}

#buddy-chat-toggle.buddy__style__field span {
  white-space: nowrap;
}

#buddy-chat-toggle.buddy__left {
  top: var(--buddy-icon-offset);
  transform: translateY(-50%);
  left: 0;
}

#buddy-chat-toggle.buddy__left.buddy__style__bubble {
  left: 20px;
}

#buddy-chat-toggle.buddy__bottom {
  left: var(--buddy-icon-offset);
  transform: translateX(-50%);
  bottom: 0;
}

#buddy-chat-toggle.buddy__bottom.buddy__style__bubble {
  bottom: 20px;
}

#buddy-chat-toggle.buddy__right {
  top: var(--buddy-icon-offset);
  transform: translateY(-50%);
  right: 0;
}

#buddy-chat-toggle.buddy__right.buddy__style__bubble {
  right: 20px;
}

.buddy-toggle-label {
  font-size: 1.3rem;
}

.buddy__field-bubble {
  position: absolute;
  top: -10px;
  left: 12px;
  background-color: var(--buddy-primary-color);
  padding: .5rem;
  border-radius: 10px;
  transform: rotate(180deg);
}

/* Main chat window - FIXED for mobile keyboard */
#buddy-chat-window {
  background: white;
  flex-direction: column;
  justify-content: space-between;
  overflow: hidden;
  display: flex;
  opacity: 0;
  pointer-events: none;
  position: fixed;
  transition: all 0.3s ease;
  z-index: 99999999;

  /* Mobile-first approach */
  left: 0;
  right: 0;
  bottom: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;

  height: calc(var(--buddy__vh) * 100);
}

/* Desktop styles */
@media screen and (min-width: 990px) {
  #buddy-chat-window {
    border-radius: var(--buddy-border-radius);
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
    width: 30vw;
    height: auto;
    max-height: calc(100vh - 30px);
    bottom: auto;
    left: auto;
    right: auto;
  }

  #buddy-chat-window.buddy__height__fit-content {
    height: fit-content;
    max-height: calc(100vh - 30px);
  }

  #buddy-chat-window.buddy__height__full-height {
    height: calc(100vh - 30px);
  }

  #buddy-chat-window.buddy__left {
    left: 15px;
  }

  #buddy-chat-window.buddy__right {
    right: 15px;
  }

  #buddy-chat-window.buddy__top {
    top: 15px;
  }

  #buddy-chat-window.buddy__center {
    top: 50%;
    bottom: initial;
    max-height: calc(100vh - 40px);
  }

  #buddy-chat-window.buddy__bottom {
    bottom: 20px;
  }

  #buddy-chat-window.buddy__center.open {
    transform: translateY(-50%);
  }

  /* Remove keyboard-specific styles on desktop */
  #buddy-chat-window.keyboard-visible {
    height: auto;
    max-height: calc(100vh - 30px);
  }
}

#buddy-chat-window.open {
  opacity: 1;
  pointer-events: auto;
  /* transform: translateY(0); */
}

.buddy__close {
  background-color: var(--buddy-primary-color);
  border-radius: 50%;
  color: white;
  width: 30px;
  height: 30px;
  padding-top: 5px;
  cursor: pointer;
  transition: height 0.3s ease, width 0.3s ease;
  border: none;
}

.buddy__close:hover {
  border-radius: 50%;
  background-color: rgba(255, 255, 255, .1);
}

.buddy__left .buddy__close {
  transform: rotate(180deg);
}

.buddy__bottom .buddy__close {
  transform: rotate(90deg);
}

#buddy-chat-header {
  padding: 1.5rem;
  background: var(--buddy-primary-color);
  color: white;
  font-weight: bold;
  display: flex;
  justify-content: space-between;
  flex-shrink: 0;
  /* Prevent header from shrinking */
}

#buddy-chat-header span {
  display: flex;
  gap: .2rem;
}

.buddy__ai-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 6px;
}


#buddy-chat-messages-container {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  height: fit-content;
  min-height: 0;
  /* Important for flex child to shrink */
  /* Use webkit for iOS smooth scrolling */
  -webkit-overflow-scrolling: touch;
}

#buddy-chat-messages {
  flex: 1;
  padding: 2rem 1rem;
  display: flex;
  flex-direction: column;
  min-height: min-content;
}

/* Ensure messages stay visible when keyboard appears */
.keyboard-visible #buddy-chat-messages-container {
  /* Maintain scrollability when keyboard is visible */
  overflow-y: auto;
}

.buddy-chat-msg {
  margin-bottom: 0.75rem;
  max-width: 80%;
  padding: 0.6rem 1rem;
  border-radius: 1rem;
  line-height: 1.4;
  display: inline-block;
  clear: both;
}

.buddy-chat-msg.user {
  background-color: var(--buddy-primary-color, #007aff);
  color: white;
  margin-left: auto;
  border-bottom-right-radius: 0.2rem;
}

.buddy-chat-msg.assistant {
  background-color: #f1f1f1;
  color: #333;
  margin-right: auto;
  border-bottom-left-radius: 0.2rem;
  text-align: left;
}

/* FIXED: Input area with better mobile keyboard handling */
#buddy-chat-input {
  display: flex;
  align-items: center;
  border-top: 1px solid #e5e7eb;
  padding: 1.25rem 1rem;
  background: white;
  position: relative;
  flex-shrink: 0;
  /* Prevent input from shrinking */
  /* Ensure input stays at bottom */
  margin-top: auto;
}

/* Better mobile input handling */
@media screen and (max-width: 989px) {
  #buddy-chat-input {
    /* Add safe area for mobile devices */
    padding-bottom: max(1.25rem, env(safe-area-inset-bottom));
  }

  /* When keyboard is visible, adjust padding */
  .keyboard-visible #buddy-chat-input {
    padding-bottom: 1.25rem;
  }
}

#buddy-chat-input input {
  flex: 1;
  border: none;
  box-shadow: none;
  background: transparent;
  font-size: 1.4rem;
  outline: none;
  color: #111827;
  /* Prevent zoom on iOS */
  font-size: max(1.4rem, 16px);
}

#buddy-chat-input input::placeholder {
  font-size: max(1.6rem, 16px);
  color: #9ca3af;
}

#buddy-send-button {
  background: none;
  border: none;
  color: var(--buddy-primary-color);
  padding: 0;
  margin-left: 0.5rem;
  margin-right: 1rem;
  cursor: pointer;
  display: none;
  transition: transform 0.25s ease;
}

#buddy-send-button:hover {
  transform: scale(1.15);
}

#buddy-chat-widget a {
  position: relative;
  color: inherit;
  text-decoration: none;
  background-image: linear-gradient(currentColor, currentColor);
  background-position: 0 100%;
  background-repeat: no-repeat;
  background-size: 100% 1px;
  transition: background-size 0.3s ease;
}

.buddy-proactive {
  position: fixed;
  bottom: 100px;
  font-weight: 600;
  right: 20px;
  background: white;
  border: 1px solid #ccc;
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  padding: 1rem;
  z-index: 9999999;
  max-width: 300px;
  font-size: 1.3rem;
}

.buddy-proactive.hidden {
  display: none;
}

.buddy__popup-message {
  margin-bottom: 0.75rem;
}

.buddy__popup-actions button {
  margin-right: 0.5rem;
  background-color: white;
  border-radius: 5px;
  padding: .75rem 2rem;
  cursor: pointer;
  border-style: none;
}

.buddy__popup-actions button.accept {
  color: white;
  box-shadow: 0 6px 14px rgba(0, 0, 0, 0.25);
  background-color: var(--buddy-primary-color);
}

.buddy__popup-close {
  transform: translateX(25%) translateY(-25%);
  position: absolute;
  right: 0;
  top: 0;
}

.buddy__popup-close:hover {
  opacity: 0.8;
  background: var(--buddy-primary-color);
}

#buddy-chat-widget a:hover {
  background-size: 0% 1px;
}

.buddy__stream-controls {
  margin-left: 2rem;
}

.buddy__stream-btn {
  border: none;
  background: none;
  color: var(--buddy-primary-color);
}

.buddy__stop-btn {
  border-radius: 50%;
}

.buddy-proactive {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
}

/* Prevent body scroll when chat is open on mobile and keyboard-related overflow */
.buddy__overflow-hidden {
  overflow: hidden;
}

/* Additional mobile-specific improvements */
@media screen and (max-width: 989px) {

  /* Ensure proper scrolling behavior on mobile */
  #buddy-chat-messages-container {
    overscroll-behavior: contain;
  }

  /* Better touch targets on mobile */
  .buddy__close {
    width: 35px;
    height: 35px;
    padding-top: 6px;
  }

  #buddy-send-button {
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}

/* Loading spinner positioning fix */
#buddy__loading-spinner {
  margin: 1rem auto;
  flex-shrink: 0;
}

/* Chat end anchor for scrolling */
#buddy-chat-end {
  height: 1px;
  flex-shrink: 0;
}