:host {
  display: block;
  width: 100%;
  height: 100%;
  font-family: inherit;
}

.bcx-chat-list {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  min-height: 0; /* Required for flexbox scrolling to work */
  background: var(--bcx-bg-primary, #ffffff);
  color: var(--bcx-text-primary, #1f2937);
  /* Inherits from <bcx-chat-list> host (floating: Inter from widget; is-component: page font) */
  font-family: inherit;
  overflow: hidden;

  &--dark {
    background: var(--bcx-dark-bg, #1e1e1e);
    color: var(--bcx-dark-text-primary, #f9fafb);
  }
}

// Header
.bcx-chat-list__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--bcx-space-4, 16px);
  border-bottom: 1px solid var(--bcx-border-subtle, rgba(0, 0, 0, 0.1));
  background: var(--bcx-bg-primary, #ffffff);
  position: relative;
  z-index: 10;
  flex-shrink: 0;

  :host-context(.dark) &,
  .bcx-chat-list--dark & {
    background: var(--bcx-dark-bg, #1e1e1e);
    border-bottom-color: rgba(255, 255, 255, 0.12);
  }
}

.bcx-chat-list__header-title {
  font-size: var(--bcx-text-lg, 18px);
  font-weight: 600;
  color: var(--bcx-text-primary, #1f2937);
  margin: 0;
  flex: 1;
  text-align: center;
  letter-spacing: -0.01em;

  :host-context(.dark) &,
  .bcx-chat-list--dark & {
    color: var(--bcx-dark-text-primary, #f9fafb);
  }
}

.bcx-chat-list__back-btn,
.bcx-chat-list__close-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  color: var(--bcx-text-secondary, #6b7280);
  cursor: pointer;
  border-radius: var(--bcx-component-radius-button, var(--bcx-radius-sm, 8px));
  transition: all 0.15s cubic-bezier(0.4, 0, 0.2, 1);
  flex-shrink: 0;
  padding: 0;
  margin: 0;

  svg {
    width: 20px;
    height: 20px;
    stroke-width: 2;
  }

  &:hover {
    background: var(--bcx-light-bg-secondary, #f3f4f6);
    color: var(--bcx-text-primary, #1f2937);
  }

  &:active {
    transform: scale(0.95);
    background: var(--bcx-light-bg-tertiary, #e5e7eb);
  }

  &:focus {
    outline: none;
    box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.2);
  }

  :host-context(.dark) &,
  .bcx-chat-list--dark & {
    color: var(--bcx-dark-text-tertiary, #9ca3af);

    &:hover {
      background: rgba(255, 255, 255, 0.1);
      color: var(--bcx-white, #ffffff);
    }

    &:active {
      background: rgba(255, 255, 255, 0.15);
    }

    &:focus {
      box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.2);
    }
  }
}

// Chat List
.bcx-chat-list__list {
  flex: 1;
  min-height: 0; /* Required for flexbox scrolling to work */
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--bcx-space-2, 8px);
  display: flex;
  flex-direction: column;
  gap: var(--bcx-space-2, 8px);
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */

  // Custom scrollbar
  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-track {
    background: transparent;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 3px;

    :host-context(.dark) &,
    .bcx-chat-list--dark & {
      background: rgba(255, 255, 255, 0.2);
    }

    &:hover {
      background: rgba(0, 0, 0, 0.3);

      :host-context(.dark) &,
      .bcx-chat-list--dark & {
        background: rgba(255, 255, 255, 0.3);
      }
    }
  }
}

.bcx-chat-list__item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--bcx-space-3, 12px);
  padding: var(--bcx-space-4, 16px);
  border: 1px solid var(--bcx-border-subtle, rgba(0, 0, 0, 0.1));
  border-radius: var(--bcx-component-radius-item, var(--bcx-radius-lg, 12px));
  background: var(--bcx-bg-secondary, #f9f9f9);
  cursor: pointer;
  transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1), box-shadow 0.3s cubic-bezier(0.16, 1, 0.3, 1), border-color 0.3s cubic-bezier(0.16, 1, 0.3, 1), background 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  text-align: left;
  font-family: inherit;
  position: relative;
  overflow: hidden;
  width: 100%;
  flex-shrink: 0; /* Prevent items from shrinking */
  min-height: fit-content; /* Ensure items maintain their natural height */
  /* AUDYT: chat list items - usunięto will-change i contain (zbyt wiele elementów) */
  /* AUDYT: usunięto backface-visibility - animacja 2D (slide) */

  &::before {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(
      135deg,
      color-mix(in srgb, var(--bcx-primary, #007bff) 4%, transparent) 0%,
      transparent 50%,
      color-mix(in srgb, var(--bcx-primary, #007bff) 2%, transparent) 100%
    );
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
    border-radius: inherit;
  }

  &:hover {
    background: var(--bcx-bg-tertiary, #f3f4f6);
    border-color: color-mix(in srgb, var(--bcx-primary, #007bff) 30%, transparent);
    transform: translate3d(0, -2px, 0); /* OPTYMALIZACJA: translate3d zamiast translateY */
    box-shadow:
      0 8px 16px color-mix(in srgb, var(--bcx-text-primary, #1f2937) 8%, transparent),
      0 4px 8px color-mix(in srgb, var(--bcx-text-primary, #1f2937) 4%, transparent);

    &::before {
      opacity: 1;
    }

    .bcx-chat-list__item-icon {
      transform: translateX(4px);
      opacity: 1;
    }
  }

  &:active {
    transform: translate3d(0, 0, 0); /* OPTYMALIZACJA: translate3d zamiast translateY */
    box-shadow:
      0 4px 8px color-mix(in srgb, var(--bcx-text-primary, #1f2937) 6%, transparent),
      0 2px 4px color-mix(in srgb, var(--bcx-text-primary, #1f2937) 3%, transparent);
  }

  &:focus {
    outline: none;
    border-color: color-mix(in srgb, var(--bcx-primary, #007bff) 50%, transparent);
    box-shadow:
      0 0 0 3px color-mix(in srgb, var(--bcx-primary, #007bff) 15%, transparent),
      0 4px 12px color-mix(in srgb, var(--bcx-text-primary, #1f2937) 8%, transparent);
  }

  :host-context(.dark) &,
  .bcx-chat-list--dark & {
    background: #2b2a2a;
    border-color: rgba(255, 255, 255, 0.12);

    &::before {
      background: linear-gradient(135deg, rgba(0, 123, 255, 0.08) 0%, transparent 50%, rgba(0, 123, 255, 0.04) 100%);
    }

    &:hover {
      background: rgba(255, 255, 255, 0.08);
      border-color: rgba(255, 255, 255, 0.18);
      box-shadow:
        0 8px 16px rgba(0, 0, 0, 0.3),
        0 4px 8px rgba(0, 0, 0, 0.2);
    }

    &:focus {
      border-color: rgba(255, 255, 255, 0.25);
      box-shadow:
        0 0 0 3px rgba(255, 255, 255, 0.1),
        0 4px 12px rgba(0, 0, 0, 0.3);
    }

    &:active {
      background: rgba(255, 255, 255, 0.12);
      box-shadow:
        0 4px 8px rgba(0, 0, 0, 0.25),
        0 2px 4px rgba(0, 0, 0, 0.15);
    }
  }
}

.bcx-chat-list__item-content {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: var(--bcx-space-1, 4px);
}

.bcx-chat-list__item-message {
  font-size: var(--bcx-text-sm, 14px);
  font-weight: 500;
  line-height: 1.5;
  color: var(--bcx-text-primary, #1f2937);
  transition: color 0.3s ease;
  word-wrap: break-word;
  white-space: pre-wrap;

  :host-context(.dark) &,
  .bcx-chat-list--dark & {
    color: var(--bcx-dark-text-primary, #f9fafb);
  }
}

.bcx-chat-list__item-time {
  font-size: var(--bcx-text-xs, 12px);
  color: var(--bcx-text-tertiary, #9ca3af);
  font-weight: 400;

  :host-context(.dark) &,
  .bcx-chat-list--dark & {
    color: var(--bcx-dark-text-tertiary, #9ca3af);
  }
}

.bcx-chat-list__item-icon {
  flex-shrink: 0;
  width: 16px;
  height: 16px;
  opacity: 0.5;
  transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  stroke-width: 2;
  color: var(--bcx-text-secondary, #6b7280);

  :host-context(.dark) &,
  .bcx-chat-list--dark & {
    color: var(--bcx-dark-text-tertiary, #9ca3af);
  }
}

// Empty state
.bcx-chat-list__empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--bcx-space-8, 32px);
  color: var(--bcx-text-tertiary, #9ca3af);
  text-align: center;
  flex: 1;

  svg {
    width: 48px;
    height: 48px;
    margin-bottom: var(--bcx-space-4, 16px);
    opacity: 0.5;
  }

  p {
    font-size: var(--bcx-text-base, 14px);
    margin: 0;
  }

  :host-context(.dark) &,
  .bcx-chat-list--dark & {
    color: var(--bcx-dark-text-tertiary, #9ca3af);
  }
}

// Messages view
.bcx-chat-list__messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--bcx-space-4, 16px);
  display: flex;
  flex-direction: column;
  gap: var(--bcx-space-3, 12px);

  // Custom scrollbar
  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-track {
    background: transparent;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 3px;

    :host-context(.dark) &,
    .bcx-chat-list--dark & {
      background: rgba(255, 255, 255, 0.2);
    }

    &:hover {
      background: rgba(0, 0, 0, 0.3);

      :host-context(.dark) &,
      .bcx-chat-list--dark & {
        background: rgba(255, 255, 255, 0.3);
      }
    }
  }
}

.bcx-chat-list__load-more-trigger {
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.bcx-chat-list__loading-more {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--bcx-space-2, 8px);
}

.bcx-chat-list__message {
  display: flex;
  flex-direction: column;
  gap: var(--bcx-space-1, 4px);
  max-width: 85%;
  animation: bcx-message-slide-in 0.25s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  /* AUDYT: usunięto will-change, contain, backface-visibility - pojedyncze wiadomości */

  &--user {
    align-self: flex-end;
    align-items: flex-end;
  }

  &--assistant {
    align-self: flex-start;
    align-items: flex-start;
  }
}

.bcx-chat-list__message-content {
  display: flex;
  flex-direction: column;
  gap: var(--bcx-space-1, 4px);
}

.bcx-chat-list__message-text {
  padding: var(--bcx-space-3, 12px) var(--bcx-space-4, 16px);
  border-radius: var(--bcx-component-radius-message, var(--bcx-radius-lg, 12px));
  font-size: var(--bcx-text-sm, 14px);
  line-height: 1.5;
  word-wrap: break-word;
  white-space: pre-wrap;
  font-family: inherit;

  .bcx-chat-list__message--user & {
    background: var(--bcx-primary, #007bff);
    color: var(--bcx-white, #ffffff);
    border-bottom-right-radius: var(--bcx-component-radius-button, var(--bcx-radius-sm, 8px));
  }

  .bcx-chat-list__message--assistant & {
    background: var(--bcx-bg-secondary, #f9f9f9);
    color: var(--bcx-text-primary, #1f2937);
    border-bottom-left-radius: var(--bcx-component-radius-button, var(--bcx-radius-sm, 8px));

    :host-context(.dark) &,
    .bcx-chat-list--dark & {
      background: #2b2a2a;
      color: var(--bcx-dark-text-primary, #f9fafb);
    }
  }
}

.bcx-chat-list__message-time {
  font-size: var(--bcx-text-xs, 12px);
  color: var(--bcx-text-tertiary, #9ca3af);
  padding: 0 var(--bcx-space-2, 8px);
  font-weight: 400;

  :host-context(.dark) &,
  .bcx-chat-list--dark & {
    color: var(--bcx-dark-text-tertiary, #9ca3af);
  }
}

// Loading & Error states
.bcx-chat-list__loading,
.bcx-chat-list__error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--bcx-space-8, 32px);
  color: var(--bcx-text-tertiary, #9ca3af);
  text-align: center;
  flex: 1;
  gap: var(--bcx-space-4, 16px);

  svg {
    width: 48px;
    height: 48px;
    opacity: 0.5;
  }

  p {
    font-size: var(--bcx-text-base, 14px);
    margin: 0;
  }

  :host-context(.dark) &,
  .bcx-chat-list--dark & {
    color: var(--bcx-dark-text-tertiary, #9ca3af);
  }
}

.bcx-chat-list__spinner {
  width: 24px;
  height: 24px;
  border: 3px solid var(--bcx-border-subtle, rgba(0, 0, 0, 0.1));
  border-top: 3px solid var(--bcx-primary, #007bff);
  border-radius: 50%;
  animation: bcx-spin 1s linear infinite;

  :host-context(.dark) &,
  .bcx-chat-list--dark & {
    border-color: rgba(255, 255, 255, 0.1);
    border-top-color: var(--bcx-primary, #007bff);
  }
}

@keyframes bcx-message-slide-in {
  from {
    opacity: 0;
    transform: translate3d(0, 10px, 0); /* OPTYMALIZACJA: translate3d zamiast translateY */
  }
  to {
    opacity: 1;
    transform: translate3d(0, 0, 0); /* OPTYMALIZACJA: translate3d zamiast translateY */
  }
}

@keyframes bcx-spin {
  0% {
    transform: translate3d(0, 0, 0) rotate(0deg); /* AUDYT: translate3d zamiast translateZ(0) dla spójności */
  }
  100% {
    transform: translate3d(0, 0, 0) rotate(360deg); /* AUDYT: translate3d zamiast translateZ(0) dla spójności */
  }
}

/* Mobile/fullscreen styles - identical to bettercx-widget chat view */
@media (max-width: 768px) {
  :host {
    width: 100% !important;
    height: 100% !important;
    display: block !important;
    margin: 0 !important;
    padding: 0 !important;
    border: none !important;
    border-radius: 0 !important;
    box-sizing: border-box !important;
    position: relative !important;
    overflow: hidden !important;
  }

  .bcx-chat-list {
    width: 100% !important;
    height: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
    border: none !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    box-sizing: border-box !important;
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
    position: relative !important;
  }
}
