/* ============================================ */
/* 0. ROOT + VARIABLES                          */
/* ============================================ */

.b2b-cw {
  /* Local sizing — everything scales from here */
  --cw-width-closed: 380px;
  --cw-width-chat: 780px;
  --cw-height: 560px;
  --cw-radius: 12px;
  --cw-radius-sm: 8px;
  --cw-shadow: 0 12px 40px rgba(0, 0, 0, 0.22);
  --cw-shadow-soft: 0 4px 14px rgba(0, 0, 0, 0.12);
  --cw-z: 9998;
  /* below scroll-top (9999), above content */

  /* Colors are pulled from existing theme variables.
     If a variable is missing, we provide a safe fallback. */
  --cw-bg: var(--bg-card, #ffffff);
  --cw-bg-alt: var(--bg-secondary, #f0f0f0);
  --cw-bg-hover: var(--bg-hover, rgba(0, 0, 0, 0.04));
  --cw-border: var(--border-color, #dee2e6);
  --cw-border-soft: var(--border-color-light, #e9ecef);
  --cw-text: var(--text-primary, #212529);
  --cw-text-dim: var(--text-secondary, #6c757d);
  --cw-text-mute: var(--text-muted, #868e96);
  --cw-accent: var(--accent-color, #10b981);
  --cw-accent-hover: var(--accent-hover, #059669);
  --cw-accent-rgb: var(--accent-rgb, 16, 185, 129);
  --cw-input-bg: var(--input-bg, #f0f5ff);
  --cw-danger: var(--danger, #dc3545);
  --cw-success: var(--success, #198754);

  /* Positioning — sits to the right of the scroll-top button */
  position: fixed;
  right: 80px;
  bottom: 24px;
  z-index: var(--cw-z);

  /* Typography inherits from body */
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--cw-text);
}

/* ============================================ */
/* 1. TRIGGER BUTTON (FAB)                      */
/* ============================================ */

.b2b-cw__trigger {
  position: relative;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: none;
  background: linear-gradient(
    135deg,
    var(--cw-accent, #10b981) 0%,
    var(--cw-accent-hover, #059669) 100%
  );
  color: #fff;
  font-size: 1.35rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(var(--cw-accent-rgb), 0.45);
  transition:
    transform 0.2s ease,
    box-shadow 0.2s ease,
    background 0.2s ease;
  outline: none;
}

.b2b-cw__trigger:hover {
  transform: translateY(-3px) scale(1.04);
  box-shadow: 0 10px 28px rgba(var(--cw-accent-rgb), 0.6);
  background: linear-gradient(
    135deg,
    var(--cw-accent-hover, #059669) 0%,
    #047857 100%
  );
}

.b2b-cw__trigger:active {
  transform: translateY(-1px) scale(0.98);
}

.b2b-cw__trigger:focus-visible {
  outline: 3px solid rgba(var(--cw-accent-rgb), 0.5);
  outline-offset: 2px;
}

/* When widget is open, rotate icon slightly for feedback */
.b2b-cw[data-state="open"] .b2b-cw__trigger i {
  transform: scale(0.92);
  transition: transform 0.2s ease;
}

/* Unread badge (sits on top-right of trigger) */
.b2b-cw__badge {
  position: absolute;
  top: -3px;
  right: -3px;
  font-size: 0.65rem;
  font-weight: 700;
  padding: 0.2rem 0.4rem;
  min-width: 20px;
  border: 2px solid var(--cw-bg);
  border-radius: 999px;
  line-height: 1;
  pointer-events: none;
}

/* ============================================ */
/* 2. PANEL (mini window)                       */
/* ============================================ */

.b2b-cw__panel {
  position: absolute;
  right: 0;
  bottom: 72px;
  /* above the trigger */
  width: var(--cw-width-closed);
  height: var(--cw-height);
  display: flex;
  flex-direction: column;
  background: var(--cw-bg);
  color: var(--cw-text);
  border: 1px solid var(--cw-border);
  border-radius: var(--cw-radius);
  box-shadow: var(--cw-shadow);
  overflow: hidden;
  transition:
    width 0.28s ease,
    opacity 0.2s ease,
    transform 0.28s ease;
  transform-origin: bottom right;
  animation: b2bCwPop 0.22s ease-out;
}

.b2b-cw__panel[hidden] {
  display: none;
}

@keyframes b2bCwPop {
  from {
    opacity: 0;
    transform: translateY(12px) scale(0.98);
  }

  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* Expanded (chat mode) — wider panel */
.b2b-cw__panel--expanded {
  width: var(--cw-width-chat);
}

/* ============================================ */
/* 3. PANEL HEADER                              */
/* ============================================ */

.b2b-cw__header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.7rem 0.9rem;
  background: var(--cw-bg-alt);
  border-bottom: 1px solid var(--cw-border);
  flex-shrink: 0;
}

.b2b-cw__title {
  flex: 1;
  display: flex;
  align-items: center;
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--cw-text);
  min-width: 0;
}

.b2b-cw__title-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.b2b-cw__header-actions {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}

/* Icon buttons (minimize / close / back / open-full) */
.b2b-cw__icon-btn {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--cw-text-dim);
  border-radius: var(--cw-radius-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition:
    background 0.15s ease,
    color 0.15s ease;
  font-size: 0.9rem;
  text-decoration: none;
  outline: none;
}

.b2b-cw__icon-btn:hover {
  background: rgba(var(--cw-accent-rgb), 0.12);
  color: var(--cw-accent);
}

.b2b-cw__icon-btn:focus-visible {
  outline: 2px solid rgba(var(--cw-accent-rgb), 0.5);
  outline-offset: 1px;
}

.b2b-cw__icon-btn--danger:hover {
  background: rgba(220, 53, 69, 0.12);
  color: var(--cw-danger);
}

.b2b-cw__icon-btn[hidden] {
  display: none;
}

/* Back button only visible in chat mode */
.b2b-cw__back {
  margin-right: 2px;
}

/* ============================================ */
/* 4. PANEL BODY (two columns)                  */
/* ============================================ */

.b2b-cw__body {
  flex: 1;
  display: flex;
  min-height: 0;
  overflow: hidden;
}

/* ============================================ */
/* 5. LEFT COLUMN — ACTIVE CHAT                 */
/* ============================================ */

.b2b-cw__chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  border-right: 1px solid var(--cw-border);
}

.b2b-cw__chat[hidden] {
  display: none;
}

/* Chat head: avatar + name + sub + open-full */
.b2b-cw__chat-head {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.55rem 0.85rem;
  border-bottom: 1px solid var(--cw-border);
  background: var(--cw-bg);
  flex-shrink: 0;
}

.b2b-cw__chat-avatar {
  width: 34px;
  height: 34px;
  object-fit: cover;
  flex-shrink: 0;
}

.b2b-cw__chat-meta {
  flex: 1;
  min-width: 0;
}

.b2b-cw__chat-name {
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--cw-text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.b2b-cw__chat-sub {
  font-size: 0.72rem;
  color: var(--cw-text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.b2b-cw__chat-sub .b2b-cw__dot-online {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--cw-success);
  margin-right: 4px;
  vertical-align: middle;
}

/* Messages area — extends .chat-container */
.b2b-cw__messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0.75rem 0.85rem;
  min-height: 0;
  /* Override .chat-container's min-height:400px and border to fit widget */
  min-height: 0 !important;
  border: none !important;
  border-radius: 0 !important;
  background: var(--cw-bg-alt) !important;
}

/* Empty chat state (inside messages area) */
.b2b-cw__chat-empty {
  color: var(--cw-text-mute);
}

/* Message bubbles — reuse existing .message-bubble from company.css,
   but tune max-width inside the widget */
.b2b-cw__messages .message-bubble {
  max-width: 82%;
  padding: 0.5rem 0.75rem;
  font-size: 0.83rem;
  line-height: 1.45;
}

.b2b-cw__messages .message-text {
  word-wrap: break-word;
  overflow-wrap: break-word;
  white-space: normal;
}

.b2b-cw__messages .message-time {
  font-size: 0.68rem;
  opacity: 0.75;
  margin-top: 2px;
}

/* Slightly smaller check icons inside widget */
.b2b-cw__messages .message-bubble .bi-check-lg,
.b2b-cw__messages .message-bubble .bi-check2-circle {
  font-size: 0.9rem !important;
}

/* Day separator (e.g. — HEUTE —) */
.b2b-cw__day-sep {
  text-align: center;
  margin: 0.6rem 0 0.4rem;
  font-size: 0.68rem;
  color: var(--cw-text-mute);
  letter-spacing: 0.4px;
  text-transform: uppercase;
  position: relative;
}

.b2b-cw__day-sep::before,
.b2b-cw__day-sep::after {
  content: "";
  display: inline-block;
  width: 30%;
  height: 1px;
  background: var(--cw-border-soft);
  vertical-align: middle;
  margin: 0 0.5rem;
}

/* ============================================ */
/* 6. COMPOSER (textarea + emoji + send)        */
/* ============================================ */

.b2b-cw__composer {
  position: relative;
  border-top: 1px solid var(--cw-border);
  background: var(--cw-bg);
  padding: 0.5rem 0.7rem 0.4rem;
  flex-shrink: 0;
}

.b2b-cw__composer-row {
  display: flex;
  align-items: flex-end;
  gap: 0.4rem;
}

.b2b-cw__emoji-btn,
.b2b-cw__send-btn {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.b2b-cw__textarea {
  flex: 1;
  min-height: 40px;
  max-height: 120px;
  resize: none;
  font-size: 0.85rem;
  line-height: 1.4;
  padding: 0.5rem 0.7rem;
}

.b2b-cw__composer-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 0.25rem;
  padding: 0 0.15rem;
}

/* ============================================ */
/* 7. EMOJI PICKER (tabbed style)               */
/* ============================================ */

.b2b-cw__emoji-panel {
  /* extends .emoji-panel but shrinks for widget */
  width: 320px !important;
  max-width: calc(100vw - 60px);
  max-height: 280px !important;
  padding: 0 !important;
  border-radius: var(--cw-radius) !important;
  overflow: hidden !important;
  background: var(--cw-bg) !important;
  border: 1px solid var(--cw-border) !important;
  box-shadow: var(--cw-shadow-soft) !important;
}

/* Category tabs */
.b2b-cw__emoji-tabs {
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--cw-border);
  background: var(--cw-bg-alt);
  padding: 0 4px;
  overflow-x: auto;
  scrollbar-width: none;
}

.b2b-cw__emoji-tabs::-webkit-scrollbar {
  display: none;
}

.b2b-cw__emoji-tab {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  border: none;
  background: transparent;
  font-size: 1.05rem;
  cursor: pointer;
  color: var(--cw-text-dim);
  border-bottom: 2px solid transparent;
  transition:
    border-color 0.15s ease,
    background 0.15s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.b2b-cw__emoji-tab:hover {
  background: rgba(var(--cw-accent-rgb), 0.08);
}

.b2b-cw__emoji-tab.active {
  border-bottom-color: var(--cw-accent);
  background: rgba(var(--cw-accent-rgb), 0.06);
}

/* Emoji body — scrollable, grid inside */
.b2b-cw__emoji-body {
  max-height: 240px;
  overflow-y: auto;
  padding: 8px;
}

.b2b-cw__emoji-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 4px;
}

.b2b-cw__emoji-grid[hidden] {
  display: none;
}

/* Override main emoji-panel's desktop width when inside widget */
.b2b-cw .emoji-item {
  font-size: 1.15rem;
  padding: 4px;
  border-radius: 6px;
  cursor: pointer;
  text-align: center;
  transition:
    transform 0.12s ease,
    background 0.12s ease;
}

.b2b-cw .emoji-item:hover {
  transform: scale(1.15);
  background: var(--cw-bg-alt);
}

/* ============================================ */
/* 8. RIGHT COLUMN — THREAD LIST               */
/* ============================================ */

.b2b-cw__list {
  width: 100%;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

/* In chat mode, list shrinks to a fixed sidebar */
.b2b-cw__panel--expanded .b2b-cw__list {
  width: 280px;
  border-left: 1px solid var(--cw-border);
  flex-shrink: 0;
}

/* Search bar */
.b2b-cw__search {
  position: relative;
  padding: 0.55rem 0.75rem;
  border-bottom: 1px solid var(--cw-border);
  flex-shrink: 0;
  background: var(--cw-bg);
}

.b2b-cw__search-icon {
  position: absolute;
  left: 1.1rem;
  top: 50%;
  transform: translateY(-50%);
  color: var(--cw-text-mute);
  font-size: 0.85rem;
  pointer-events: none;
}

.b2b-cw__search-input {
  padding-left: 2rem !important;
  font-size: 0.82rem !important;
  height: 34px;
  border-radius: var(--cw-radius-sm) !important;
}

/* Thread list container */
.b2b-cw__thread-list {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  min-height: 0;
  padding: 0;
  background: var(--cw-bg);
}

/* Individual thread row */
.b2b-cw__thread {
  display: flex;
  align-items: flex-start;
  gap: 0.6rem;
  padding: 0.6rem 0.75rem;
  cursor: pointer;
  border: none;
  border-bottom: 1px solid var(--cw-border-soft);
  background: var(--cw-bg);
  color: inherit;
  text-align: left;
  width: 100%;
  transition: background 0.15s ease;
}

.b2b-cw__thread:hover {
  background: var(--cw-bg-hover);
}

.b2b-cw__thread.active {
  background: rgba(var(--cw-accent-rgb), 0.1);
  border-left: 3px solid var(--cw-accent);
  padding-left: calc(0.75rem - 3px);
}

.b2b-cw__thread.unread .b2b-cw__thread-name {
  font-weight: 700;
}

/* Thread avatar */
.b2b-cw__thread-avatar-wrap {
  position: relative;
  flex-shrink: 0;
  width: 40px;
  height: 40px;
}

.b2b-cw__thread-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
}

/* Online dot — same pattern as messages.php */
.b2b-cw__thread-online {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: var(--cw-success);
  border: 2px solid var(--cw-bg);
}

/* Thread content */
.b2b-cw__thread-body {
  flex: 1;
  min-width: 0;
}

.b2b-cw__thread-name {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--cw-text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.b2b-cw__thread-contact {
  font-size: 0.72rem;
  color: var(--cw-text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-top: 1px;
}

.b2b-cw__thread-preview {
  font-size: 0.76rem;
  color: var(--cw-text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-top: 2px;
}

.b2b-cw__thread-preview .b2b-cw__you {
  color: var(--cw-text-mute);
  font-style: italic;
}

/* Right side of thread: time + unread badge */
.b2b-cw__thread-side {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 3px;
  margin-left: 0.4rem;
}

.b2b-cw__thread-time {
  font-size: 0.68rem;
  color: var(--cw-text-mute);
  white-space: nowrap;
}

.b2b-cw__thread-badge {
  background: var(--cw-danger);
  color: #fff;
  font-size: 0.65rem;
  font-weight: 700;
  padding: 0.1rem 0.4rem;
  border-radius: 999px;
  min-width: 18px;
  text-align: center;
  line-height: 1.3;
}

/* Loading / empty states */
.b2b-cw__list-loading,
.b2b-cw__list-empty {
  padding: 1rem;
}

.b2b-cw__list-empty[hidden],
.b2b-cw__list-loading[hidden] {
  display: none;
}

.b2b-cw__list-empty.empty-state {
  /* Override the desktop empty-state that assumes wide layout */
  max-width: none !important;
  border: none !important;
  background: transparent !important;
  box-shadow: none !important;
  padding: 1rem !important;
}

.b2b-cw__list-empty .empty-state-icon {
  font-size: 2rem !important;
  color: var(--cw-text-mute) !important;
  animation: none !important;
}

.b2b-cw__list-empty .empty-state-text {
  border: none !important;
  background: transparent !important;
  padding: 0 !important;
  color: var(--cw-text-dim) !important;
  font-size: 0.82rem !important;
}

/* ============================================ */
/* 9. SCROLLBARS (subtle)                       */
/* ============================================ */

.b2b-cw__messages::-webkit-scrollbar,
.b2b-cw__thread-list::-webkit-scrollbar,
.b2b-cw__emoji-body::-webkit-scrollbar {
  width: 6px;
}

.b2b-cw__messages::-webkit-scrollbar-track,
.b2b-cw__thread-list::-webkit-scrollbar-track,
.b2b-cw__emoji-body::-webkit-scrollbar-track {
  background: transparent;
}

.b2b-cw__messages::-webkit-scrollbar-thumb,
.b2b-cw__thread-list::-webkit-scrollbar-thumb,
.b2b-cw__emoji-body::-webkit-scrollbar-thumb {
  background: var(--cw-border);
  border-radius: 3px;
}

.b2b-cw__messages::-webkit-scrollbar-thumb:hover,
.b2b-cw__thread-list::-webkit-scrollbar-thumb:hover,
.b2b-cw__emoji-body::-webkit-scrollbar-thumb:hover {
  background: var(--cw-text-mute);
}

/* ============================================ */
/* 10. RESPONSIVE                               */
/* ============================================ */

/* Tablet — smaller expanded panel */
@media (max-width: 992px) {
  .b2b-cw__panel--expanded {
    width: min(680px, calc(100vw - 40px));
  }
}

/* Small tablet — collapse expanded panel back to closed width,
   hide thread list, use full width for chat */
@media (max-width: 768px) {
  .b2b-cw {
    right: 70px;
    bottom: 14px;
  }

  .b2b-cw__panel {
    width: min(360px, calc(100vw - 28px));
    height: min(520px, calc(100vh - 120px));
  }

  .b2b-cw__panel--expanded {
    width: min(420px, calc(100vw - 28px));
  }

  /* In expanded mode on tablet, hide thread list to give room to chat */
  .b2b-cw__panel--expanded .b2b-cw__list {
    display: none;
  }
}

/* Mobile — full screen bottom sheet */
@media (max-width: 576px) {
  .b2b-cw {
    right: 68px;
    bottom: 12px;
  }

  .b2b-cw__trigger {
    width: 44px;
    height: 44px;
    font-size: 1.2rem;
  }

  .b2b-cw__panel,
  .b2b-cw__panel--expanded {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100vw;
    height: 100dvh;
    max-width: 100vw;
    max-height: 100dvh;
    border-radius: 0;
    border: none;
  }

  /* Thread list is fully hidden in expanded mobile view */
  .b2b-cw__panel--expanded .b2b-cw__list {
    display: none;
  }

  /* Slightly larger emojis on mobile for tap */
  .b2b-cw .emoji-item {
    font-size: 1.35rem;
    padding: 6px;
  }

  .b2b-cw__emoji-panel {
    width: calc(100vw - 24px) !important;
    max-width: calc(100vw - 24px) !important;
  }
}

/* Landscape phone — cap height */
@media (max-height: 480px) {
  .b2b-cw__panel,
  .b2b-cw__panel--expanded {
    height: calc(100dvh - 20px);
  }
}

/* ============================================ */
/* 11. ACCESSIBILITY                            */
/* ============================================ */

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  .b2b-cw__panel,
  .b2b-cw__trigger,
  .b2b-cw__thread,
  .b2b-cw .emoji-item {
    transition: none !important;
    animation: none !important;
  }
}

/* High contrast / forced-colors */
@media (forced-colors: active) {
  .b2b-cw__panel,
  .b2b-cw__trigger {
    border: 1px solid CanvasText;
  }

  .b2b-cw__thread.active {
    outline: 2px solid Highlight;
  }
}

/* Focus visible ring for the whole panel via keyboard */
.b2b-cw__panel:focus-visible {
  outline: 2px solid rgba(var(--cw-accent-rgb), 0.6);
  outline-offset: 2px;
}

/* ============================================ */
/* 12. DARK THEME                               */
/* only the bits that need extra attention      */
/* ============================================ */

/* Most colors come from theme variables and update automatically.
   The handful of hard-coded rgba values below get a small tweak
   so shadows and hover states look right on dark surfaces. */

[data-theme="dark"] .b2b-cw__trigger {
  /* Keep brand gradient, only deepen the shadow ring */
  box-shadow:
    0 6px 20px rgba(0, 0, 0, 0.5),
    0 0 0 1px rgba(255, 255, 255, 0.06);
}

[data-theme="dark"] .b2b-cw__trigger:hover {
  box-shadow:
    0 10px 28px rgba(0, 0, 0, 0.65),
    0 0 0 1px rgba(255, 255, 255, 0.09);
}

[data-theme="dark"] .b2b-cw__panel {
  box-shadow:
    0 12px 40px rgba(0, 0, 0, 0.65),
    0 0 0 1px rgba(255, 255, 255, 0.04);
}

[data-theme="dark"] .b2b-cw__badge {
  border-color: var(--cw-bg);
}

[data-theme="dark"] .b2b-cw__thread.active {
  background: rgba(var(--cw-accent-rgb), 0.22);
}

[data-theme="dark"] .b2b-cw__thread:hover {
  background: rgba(255, 255, 255, 0.04);
}

[data-theme="dark"] .b2b-cw__messages {
  background: var(--cw-bg-alt) !important;
}

[data-theme="dark"] .b2b-cw__emoji-panel {
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.7) !important;
}
