/* =====================================================
   ChQMe FLOATING WIDGET
   — Button, Panel, Iframe, Animations, Responsive
===================================================== */

/* =====================================================
   FLOATING BUTTON
===================================================== */
#chqme-widget-btn {
  position: fixed;
  bottom: var(--chqme-widget-offset-bottom, 20px);
  right: var(--chqme-widget-offset-right, 20px);
  z-index: 2147483001 !important; /* above page/3rd-party overlays, below max int (2147483647) */
  width: var(--chqme-widget-btn-size, 56px);
  height: var(--chqme-widget-btn-size, 56px);
  border-radius: 50%;
  border: none;
  background: var(--chqme-widget-btn-color, #ff7a00);
  color: #fff;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1),
              box-shadow 0.25s ease;
  -webkit-tap-highlight-color: transparent;
}

html[data-chqme-widget-position="bottom-left"] #chqme-widget-btn {
  left: var(--chqme-widget-offset-right, 20px);
  right: auto;
}

#chqme-widget-btn:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}

#chqme-widget-btn:active {
  transform: scale(0.95);
}

#chqme-widget-btn .chqme-widget-icon {
  width: 28px;
  height: 28px;
  transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1),
              opacity 0.2s ease;
}

#chqme-widget-btn .chqme-widget-icon--custom {
  /* Merchant-controlled via the "Custom icon size" setting. The 42px
     fallback matches that setting's own default, so it still looks right
     even if the CSS var somehow never gets set. Uploaded icons are
     typically solid badge-style images that already fill their own canvas,
     unlike the default thin-line SVG (fixed at 28px above) — this setting
     is independent of button_size on purpose, so it never changes the
     default icon's appearance for merchants who haven't uploaded one. */
  width: var(--chqme-widget-custom-icon-size, 42px);
  height: var(--chqme-widget-custom-icon-size, 42px);
  object-fit: contain;
  pointer-events: none;
}

#chqme-widget-btn.chqme-widget-btn--active .chqme-widget-icon--open {
  transform: rotate(90deg);
  opacity: 0;
  position: absolute;
}

#chqme-widget-btn.chqme-widget-btn--active .chqme-widget-icon--close {
  opacity: 1;
  transform: rotate(0deg);
}

#chqme-widget-btn .chqme-widget-icon--close {
  opacity: 0;
  transform: rotate(-90deg);
  position: absolute;
}

/* =====================================================
   HOVER TOOLTIP (configurable: top / bottom / left / right)
===================================================== */
.chqme-widget-tooltip {
  position: absolute;
  z-index: 2147483003 !important; /* above widget button/panel */
  background: var(--chqme-tooltip-bg, #111);
  color: var(--chqme-tooltip-color, #fff);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 13px;
  font-weight: 500;
  line-height: 1.3;
  white-space: nowrap;
  padding: 8px 12px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.18);
  pointer-events: none;
  opacity: 0;
  transform: scale(0.92);
  transition: opacity 0.18s ease, transform 0.18s ease;
}

.chqme-widget-tooltip::after {
  content: "";
  position: absolute;
  width: 0;
  height: 0;
  border: 6px solid transparent;
}

.chqme-widget-tooltip--visible {
  opacity: 1;
  transform: scale(1);
}

/* LEFT: tooltip sits to the left of the button */
.chqme-widget-tooltip--left {
  right: calc(100% + 12px);
  top: 50%;
  transform: translateY(-50%) scale(0.92);
  transform-origin: right center;
}
.chqme-widget-tooltip--left.chqme-widget-tooltip--visible {
  transform: translateY(-50%) scale(1);
}
.chqme-widget-tooltip--left::after {
  left: 100%;
  top: 50%;
  margin-top: -6px;
  border-left-color: var(--chqme-tooltip-bg, #111);
}

/* RIGHT: tooltip sits to the right of the button */
.chqme-widget-tooltip--right {
  left: calc(100% + 12px);
  top: 50%;
  transform: translateY(-50%) scale(0.92);
  transform-origin: left center;
}
.chqme-widget-tooltip--right.chqme-widget-tooltip--visible {
  transform: translateY(-50%) scale(1);
}
.chqme-widget-tooltip--right::after {
  right: 100%;
  top: 50%;
  margin-top: -6px;
  border-right-color: var(--chqme-tooltip-bg, #111);
}

/* TOP: tooltip sits above the button */
.chqme-widget-tooltip--top {
  bottom: calc(100% + 12px);
  left: 50%;
  transform: translateX(-50%) scale(0.92);
  transform-origin: center bottom;
}
.chqme-widget-tooltip--top.chqme-widget-tooltip--visible {
  transform: translateX(-50%) scale(1);
}
.chqme-widget-tooltip--top::after {
  top: 100%;
  left: 50%;
  margin-left: -6px;
  border-top-color: var(--chqme-tooltip-bg, #111);
}

/* BOTTOM: tooltip sits below the button */
.chqme-widget-tooltip--bottom {
  top: calc(100% + 12px);
  left: 50%;
  transform: translateX(-50%) scale(0.92);
  transform-origin: center top;
}
.chqme-widget-tooltip--bottom.chqme-widget-tooltip--visible {
  transform: translateX(-50%) scale(1);
}
.chqme-widget-tooltip--bottom::after {
  bottom: 100%;
  left: 50%;
  margin-left: -6px;
  border-bottom-color: var(--chqme-tooltip-bg, #111);
}

/* Hide tooltip on touch-only devices to avoid sticky-hover */
@media (hover: none) and (pointer: coarse) {
  .chqme-widget-tooltip { display: none; }
}

/* Notification Dot */
.chqme-widget-notification-dot {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 12px;
  height: 12px;
  background-color: #ef4444; /* Subtle red/orange dot */
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 0 0 2px rgba(255, 68, 68, 0.2);
  transition: opacity 0.3s ease, transform 0.3s ease;
  pointer-events: none;
}

/* Safe area for mobile (notch / home indicator) */
@supports (bottom: env(safe-area-inset-bottom)) {
  #chqme-widget-btn {
    bottom: calc(var(--chqme-widget-offset-bottom, 20px) + env(safe-area-inset-bottom, 0px));
  }
}

/* =====================================================
   WIDGET PANEL
===================================================== */
#chqme-widget-panel {
  position: fixed;
  bottom: calc(var(--chqme-widget-offset-bottom, 20px) + var(--chqme-widget-btn-size, 56px) + 12px);
  right: var(--chqme-widget-offset-right, 20px);
  z-index: 2147483002 !important; /* panel above button, below tooltip */
  width: min(400px, calc(100vw - var(--chqme-widget-offset-right, 20px) - 20px));
  height: min(600px, calc(100vh - var(--chqme-widget-offset-bottom, 20px) - var(--chqme-widget-btn-size, 56px) - 40px));
  background: rgba(255, 255, 255, 0.75);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid rgba(255, 255, 255, 0.5);
  border-radius: 16px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.12),
              0 4px 16px rgba(0, 0, 0, 0.04);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  animation: chqme-widget-slide-up 0.3s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

html[data-chqme-widget-position="bottom-left"] #chqme-widget-panel {
  left: var(--chqme-widget-offset-right, 20px);
  right: auto;
}

/*
 * On tablet/desktop widths, use the horizontal space beside the launcher.
 * Keeping the panel bottom-aligned with the button prevents a tall iframe from
 * being pushed into (or beyond) the top of a short browser viewport.
 */
@media (min-width: 500px) {
  #chqme-widget-panel {
    right: calc(var(--chqme-widget-offset-right, 20px) + var(--chqme-widget-btn-size, 56px) + 12px);
    bottom: var(--chqme-widget-offset-bottom, 20px);
    width: min(
      400px,
      calc(100vw - var(--chqme-widget-offset-right, 20px) - var(--chqme-widget-btn-size, 56px) - 52px)
    );
    height: min(600px, calc(100vh - var(--chqme-widget-offset-bottom, 20px) - 20px));
  }

  html[data-chqme-widget-position="bottom-left"] #chqme-widget-panel {
    left: calc(var(--chqme-widget-offset-right, 20px) + var(--chqme-widget-btn-size, 56px) + 12px);
    right: auto;
  }
}

/* Dynamic viewport units follow the actually visible browser area. */
@supports (height: 100dvh) {
  @media (min-width: 500px) {
    #chqme-widget-panel {
      height: min(600px, calc(100dvh - var(--chqme-widget-offset-bottom, 20px) - 20px));
    }
  }
}

#chqme-widget-panel.chqme-widget-panel--closing {
  animation: chqme-widget-slide-down 0.2s ease-in forwards;
}

@supports (bottom: env(safe-area-inset-bottom)) {
  #chqme-widget-panel {
    bottom: calc(var(--chqme-widget-offset-bottom, 20px) + var(--chqme-widget-btn-size, 56px) + 12px + env(safe-area-inset-bottom, 0px));
  }

  @media (min-width: 500px) {
    #chqme-widget-panel {
      bottom: calc(var(--chqme-widget-offset-bottom, 20px) + env(safe-area-inset-bottom, 0px));
      height: min(
        600px,
        calc(100vh - var(--chqme-widget-offset-bottom, 20px) - env(safe-area-inset-bottom, 0px) - 20px)
      );
    }
  }
}

@supports (height: 100dvh) and (bottom: env(safe-area-inset-bottom)) {
  @media (min-width: 500px) {
    #chqme-widget-panel {
      height: min(
        600px,
        calc(100dvh - var(--chqme-widget-offset-bottom, 20px) - env(safe-area-inset-bottom, 0px) - 20px)
      );
    }
  }
}

/* =====================================================
   PANEL HEADER
===================================================== */
.chqme-widget-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  flex-shrink: 0;
  background: transparent;
}

.chqme-widget-header-title {
  font-size: 16px;
  font-weight: 600;
  color: #111;
  margin: 0;
  line-height: 1.2;
}

.chqme-widget-close {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: #f0f0f0;
  color: #555;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  line-height: 1;
  padding: 0;
  transition: background 0.15s ease, color 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}

.chqme-widget-close:hover {
  background: #e4e4e4;
  color: #111;
}

/* =====================================================
   IFRAME
===================================================== */
.chqme-widget-iframe {
  flex: 1 1 auto;
  width: 100%;
  height: 100%;
  border: none;
  display: block;
  background: transparent;
}

/* =====================================================
   LOADING STATE
===================================================== */
.chqme-widget-loader {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
}

.chqme-widget-spinner {
  width: 36px;
  height: 36px;
  border: 3px solid #f0f0f0;
  border-top-color: var(--chqme-widget-btn-color, #ff7a00);
  border-radius: 50%;
  animation: chqme-widget-spin 0.7s linear infinite;
}

/* =====================================================
   DESIGN MODE PREVIEW
===================================================== */
.chqme-widget-preview {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  color: #888;
  font-size: 14px;
  line-height: 1.5;
}

/* =====================================================
   ANIMATIONS
===================================================== */
@keyframes chqme-widget-slide-up {
  from {
    opacity: 0;
    transform: translateY(16px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes chqme-widget-slide-down {
  from {
    opacity: 1;
    transform: translateY(0);
  }
  to {
    opacity: 0;
    transform: translateY(16px);
  }
}

@keyframes chqme-widget-spin {
  to { transform: rotate(360deg); }
}

/* =====================================================
   RESPONSIVE — MOBILE BOTTOM SHEET
===================================================== */
@media (max-width: 480px) {
  #chqme-widget-panel {
    width: 100vw;
    max-width: 100vw;
    height: calc(100vh - 60px);
    max-height: calc(100vh - 60px);
    bottom: 0;
    right: 0;
    border-radius: 16px 16px 0 0;
    animation: chqme-widget-sheet-up 0.3s cubic-bezier(0.16, 1, 0.3, 1) forwards;
  }

  html[data-chqme-widget-position="bottom-left"] #chqme-widget-panel {
    left: 0;
    right: 0;
  }

  #chqme-widget-panel.chqme-widget-panel--closing {
    animation: chqme-widget-sheet-down 0.2s ease-in forwards;
  }

  @keyframes chqme-widget-sheet-up {
    from {
      opacity: 0;
      transform: translateY(100%);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  @keyframes chqme-widget-sheet-down {
    from {
      opacity: 1;
      transform: translateY(0);
    }
    to {
      opacity: 0;
      transform: translateY(100%);
    }
  }
}
