/* ==========================================================================
   Niwa Chatbot – Morph Container
   ---------------------------------------------------------------------------
   Single element: pill → full chat window.

   COLLAPSED: small pill (fit-content × 48px)
   OPEN: 380px × 580px, chat revealed

   STRUCTURE:
     .niwa-morph
       .niwa-morph__chat       (header + body — clipped when collapsed)
       .niwa-morph__pill       (bottom bar — always visible)
   ========================================================================== */

/* ── Container — Collapsed (pill) ───────────────────────────────────────── */
.niwa-morph {
  position: fixed;
  bottom: var(--niwa-flyout-offset-bottom);
  right: var(--niwa-flyout-offset-right);
  z-index: var(--niwa-layer-floating);

  /* Collapsed = just the pill. The width is PHP-calculated from the
     launcher label text and emitted as --niwa-launcher-width on the element
     itself (see class-chat-renderer.php -> estimate_launcher_pill_width).
     Using the variable directly as the fixed width guarantees the pill
     paints at its final size on first frame — no measurement + snap and
     no animation from a default width to a content width. */
  display: flex;
  flex-direction: column;
  width: var(--niwa-launcher-width, 148px);
  max-width: calc(100vw - 48px);
  height: var(--niwa-launcher-height, 48px);
  overflow: visible;

  /* Glass */
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border: 1px solid rgba(255, 255, 255, 0.18);
  box-shadow:
    0 4px 6px -1px rgba(0, 0, 0, 0.1),
    0 2px 4px -1px rgba(0, 0, 0, 0.06);
  border-radius: 24px;
  cursor: pointer;

  /* No transitions in the collapsed state. Width is content-based
     (`max-content`), so any layout re-measure (e.g. web-font load, label
     text change) would otherwise animate the pill width. We only want the
     width to animate during the open/close morph — those transitions are
     defined on `.niwa-morph--open` and `.niwa-morph--closing`. */
  transition: none;
}

/* Dark glass */
.niwa-widget[data-niwa-theme="dark"] .niwa-morph {
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(24px) saturate(1.4);
  -webkit-backdrop-filter: blur(24px) saturate(1.4);
  border-color: rgba(255, 255, 255, 0.08);
  box-shadow:
    0 8px 32px rgba(0, 0, 0, 0.4),
    0 1px 0 rgba(255, 255, 255, 0.05) inset;
}

/* Light launcher and open flyout share one continuous glass surface. */
.niwa-widget[data-niwa-theme="light"] .niwa-morph {
  background: rgba(226, 228, 224, 0.14);
  backdrop-filter: blur(30px) saturate(1.35);
  -webkit-backdrop-filter: blur(30px) saturate(1.35);
  border-color: rgba(255, 255, 255, 0.52);
  box-shadow:
    0 12px 36px rgba(33, 40, 36, 0.08),
    0 1px 0 rgba(255, 255, 255, 0.50) inset;
}

/* Animated gradient tint — visible when the light or neutral flyout is open. */
.niwa-widget:is([data-niwa-theme="light"], [data-niwa-theme="neutral"]) .niwa-morph--open::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 160vmax;
  height: 160vmax;
  z-index: 0;
  opacity: 0.23;
  filter: saturate(1.28);
  border-radius: 50%;
  pointer-events: none;
  transform: translate(-50%, -50%) rotate(0deg);
  transform-origin: center center;
  /* Promote the heavy 7-stop gradient to its own compositor layer so its
     one-time paint at open is isolated and the infinite rotate animates on
     the GPU (no per-frame main-thread repaint). Visual output unchanged. */
  will-change: transform;
  background:
    radial-gradient(ellipse 80% 50% at 20% 8%,  rgba(255, 120,  50, 0.70) 0%, transparent 60%),
    radial-gradient(ellipse 60% 40% at 75% 15%, rgba( 99,  51, 238, 0.60) 0%, transparent 55%),
    radial-gradient(ellipse 90% 50% at 50% 35%, rgba( 59, 200, 246, 0.50) 0%, transparent 55%),
    radial-gradient(ellipse 70% 45% at 30% 55%, rgba(236,  72, 153, 0.60) 0%, transparent 55%),
    radial-gradient(ellipse 80% 50% at 80% 65%, rgba( 45, 200, 120, 0.50) 0%, transparent 55%),
    radial-gradient(ellipse 60% 40% at 15% 80%, rgba(147,  51, 234, 0.60) 0%, transparent 55%),
    radial-gradient(ellipse 90% 50% at 70% 90%, rgba(255, 180,  50, 0.50) 0%, transparent 55%);
  animation: niwa-gradient-drift 30s linear infinite;
}

/* Keep chat + pill above the gradient layer */
.niwa-widget:is([data-niwa-theme="light"], [data-niwa-theme="neutral"]) .niwa-morph--open > .niwa-morph__chat,
.niwa-widget:is([data-niwa-theme="light"], [data-niwa-theme="neutral"]) .niwa-morph--open > .niwa-morph__pill {
  position: relative;
  z-index: 1;
}

/* Hover (collapsed only) */
.niwa-morph:not(.niwa-morph--open):hover {
  background: rgba(255, 255, 255, 0.25);
  border-color: rgba(255, 255, 255, 0.3);
}
.niwa-widget[data-niwa-theme="light"] .niwa-morph:not(.niwa-morph--open):hover {
  background: rgba(255, 255, 255, 0.25);
  border-color: rgba(255, 255, 255, 0.3);
}
.niwa-widget[data-niwa-theme="dark"] .niwa-morph:not(.niwa-morph--open):hover {
  background: rgba(0, 0, 0, 0.7);
  border-color: rgba(255, 255, 255, 0.12);
}

/* ── Expanded ───────────────────────────────────────────────────────────── */
/* Width, height and radius move as one shape. Transitions live on the state
   classes so font-load or launcher-copy changes stay instant while collapsed. */
.niwa-morph--open {
  width: var(--niwa-flyout-width);
  height: var(--niwa-flyout-height);
  max-height: var(--niwa-flyout-max-height);
  border-radius: var(--niwa-radius-lg);
  cursor: default;
  overflow: hidden;
  transition:
    width 360ms cubic-bezier(0.2, 0, 0, 1),
    height 360ms cubic-bezier(0.2, 0, 0, 1),
    top 360ms cubic-bezier(0.2, 0, 0, 1),
    right 360ms cubic-bezier(0.2, 0, 0, 1),
    bottom 360ms cubic-bezier(0.2, 0, 0, 1),
    left 360ms cubic-bezier(0.2, 0, 0, 1),
    border-radius 360ms cubic-bezier(0.2, 0, 0, 1),
    box-shadow 240ms ease-out,
    background 240ms ease-out;
}

/* Closing uses the same continuous morph, in reverse. */
.niwa-morph--closing {
  overflow: hidden;
  transition:
    width 360ms cubic-bezier(0.2, 0, 0, 1),
    height 360ms cubic-bezier(0.2, 0, 0, 1),
    top 360ms cubic-bezier(0.2, 0, 0, 1),
    right 360ms cubic-bezier(0.2, 0, 0, 1),
    bottom 360ms cubic-bezier(0.2, 0, 0, 1),
    left 360ms cubic-bezier(0.2, 0, 0, 1),
    border-radius 360ms cubic-bezier(0.2, 0, 0, 1),
    box-shadow 240ms ease-out,
    background 240ms ease-out;
}

/* Hide chat content immediately on close */
.niwa-morph--closing .niwa-morph__chat {
  opacity: 0;
  transition: opacity 140ms ease-out;
}

/* ==========================================================================
   Chat Area — hidden when collapsed
   ========================================================================== */

.niwa-morph__chat {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease-out 100ms;
  /* Scope the first layout+paint of this subtree (display:none → flex on
     open) so it doesn't invalidate page-level layout/paint. Safe: no
     absolutely-positioned descendant relies on .niwa-morph as its
     containing block, and overflow is already hidden. */
  contain: layout paint;
}

.niwa-morph:not(.niwa-morph--open):not(.niwa-morph--closing) .niwa-morph__chat {
  display: none;
}

.niwa-morph--open .niwa-morph__chat {
  opacity: 1;
  pointer-events: auto;
}

/* ── Header ─────────────────────────────────────────────────────────────── */
.niwa-morph__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 48px;
  padding: 0 var(--niwa-space-lg);
  flex-shrink: 0;
  box-sizing: border-box;
}

.niwa-morph__wordmark {
  height: 10px;
  width: auto;
  opacity: 0.4;
}

[data-niwa-theme="light"] .niwa-morph__wordmark {
  filter: invert(1);
  opacity: 0.35;
}

.niwa-morph__close {
  width: 28px;
  height: 28px;
  border-radius: var(--niwa-radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--niwa-color-text-secondary);
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  transition: background var(--niwa-transition-fast);
}

.niwa-morph__close:hover {
  background: rgba(0, 0, 0, 0.08);
}
[data-niwa-theme="dark"] .niwa-morph__close:hover {
  background: rgba(255, 255, 255, 0.1);
}

.niwa-morph__close svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}

/* ── Body (scrollable, flex column for message layout) ─────────────────── */
.niwa-morph__body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  background: transparent;
  scrollbar-width: thin;
  scrollbar-color: var(--niwa-color-border-default) transparent;
  display: flex;
  flex-direction: column;
  padding: var(--niwa-space-sm) 0;
  gap: var(--niwa-space-xs);
  /* Isolate message layout/scroll work from ancestors (no clipping). */
  contain: layout;
}

.niwa-morph__body::-webkit-scrollbar { width: 6px; }
.niwa-morph__body::-webkit-scrollbar-track { background: transparent; }
.niwa-morph__body::-webkit-scrollbar-thumb {
  background: var(--niwa-color-border-default);
  border-radius: var(--niwa-radius-full);
}

/* ── Welcome (logo + greeting) ──────────────────────────────────────────── */
.niwa-flyout__welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: var(--niwa-space-xl) var(--niwa-space-lg) var(--niwa-space-lg);
}

.niwa-flyout__logo {
  width: 72px;
  height: 72px;
  margin-bottom: var(--niwa-space-md);
}

.niwa-flyout__greeting {
  font-size: var(--niwa-font-size-xl);
  font-weight: var(--niwa-font-weight-semibold);
  color: var(--niwa-color-text-primary);
  text-align: center;
}

/* ==========================================================================
   Pill Bar — bottom input strip
   ========================================================================== */

.niwa-morph__pill {
  display: flex;
  align-items: center;
  gap: var(--niwa-space-sm);
  height: 48px;
  padding: 0 var(--niwa-space-md);
  flex-shrink: 0;
  box-sizing: border-box;
  overflow: visible;
  position: relative;
  background: transparent;
}

.niwa-morph--open.niwa-morph--multiline .niwa-morph__pill {
  min-height: 48px;
  height: auto;
  align-items: flex-end;
  padding-top: 8px;
  padding-bottom: 8px;
}

.niwa-morph__orb {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  flex-shrink: 0;
}

/* Label — collapsed only */
.niwa-morph__label {
  font-size: var(--niwa-font-size-md);
  font-weight: var(--niwa-font-weight-normal);
  color: #6B7280;
  flex: 0 1 auto;
  min-width: 0;
  overflow: clip;
  text-overflow: clip;
  white-space: nowrap;
  padding-right: var(--niwa-space-xs);
  line-height: 48px;
}
[data-niwa-theme="dark"] .niwa-morph__label {
  color: #9CA3AF;
}
.niwa-morph--open .niwa-morph__label {
  display: none;
}

/* Badge — inside pill, positioned top-right */
.niwa-morph__badge {
  position: absolute;
  top: -6px;
  right: -6px;
  z-index: 2;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: var(--niwa-radius-full);
  background: var(--niwa-color-error);
  color: #FFFFFF;
  font-size: var(--niwa-font-size-xs);
  font-weight: var(--niwa-font-weight-bold);
  line-height: 20px;
  text-align: center;
  animation: niwa-pop-in 300ms var(--niwa-animation-bounce) both;
  pointer-events: none;
}

.niwa-morph__badge:empty { display: none; }
.niwa-morph--open .niwa-morph__badge { display: none; }

/* Input — visible only after width animation finishes.
   Collapsed: flex-basis 0 + width 0 + overflow hidden so the input does NOT
   contribute to the pill's `max-content` intrinsic width. Otherwise the
   hidden textarea + send button (~181px) would inflate the collapsed pill.
   Open: the flyout width is explicit, so we restore flex:1 so the input
   fills the remaining space between the orb/label and the send area. */
.niwa-morph__input {
  display: flex;
  flex: 0 0 0;
  width: 0;
  align-items: center;
  gap: 4px;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
  transition: opacity 150ms ease;
}
.niwa-morph--multiline .niwa-morph__input {
  align-items: flex-end;
}
.niwa-morph--open .niwa-morph__input {
  flex: 1;
  width: auto;
  overflow: visible;
  opacity: 1;
  pointer-events: auto;
  transition: opacity 160ms ease-out 120ms;
}

/* WordPress themes inject aggressive textarea styles — !important ensures
   pixel-perfect alignment within the 48px pill bar. */
.niwa-widget .niwa-morph__textarea {
  flex: 1;
  display: block !important;
  width: 100% !important;
  min-width: 0 !important;
  box-sizing: border-box !important;
  height: 22px !important;
  min-height: 22px !important;
  max-height: 96px !important;
  resize: none !important;
  overflow-y: hidden !important;
  background: transparent !important;
  border: none !important;
  outline: none !important;
  padding: 4px 8px !important;
  margin: 0 !important;
  font-family: var(--niwa-font-family) !important;
  font-size: 14px !important;
  font-weight: 400;
  line-height: 22px !important;
  letter-spacing: -0.14px;
  color: var(--niwa-color-text-primary);
  box-shadow: none !important;
  border-radius: 0 !important;
}
.niwa-widget .niwa-morph__textarea::placeholder {
  color: #6B7280;
}
.niwa-widget[data-niwa-theme="dark"] .niwa-morph__textarea::placeholder {
  color: #FFFFFF;
  opacity: 1;
}

/* Send button */
.niwa-morph__send {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  border: none;
  padding: 0;
  cursor: pointer;
  background: #0A1217;
  color: #FFFFFF;
  opacity: 0.9;
  transition: opacity 200ms ease, transform 200ms ease;
}
.niwa-morph__send:hover { opacity: 1; }
.niwa-morph__send:active { transform: scale(0.95); }
.niwa-morph__send svg { display: block; }
.niwa-morph__send--disabled {
  opacity: 0.4;
  cursor: default;
  pointer-events: none;
  background: #9CA3AF;
}
[data-niwa-theme="dark"] .niwa-morph__send {
  background: #E8E6E1;
  color: #111318;
}
[data-niwa-theme="dark"] .niwa-morph__send--disabled {
  background: #4B5563;
}

/* ==========================================================================
   Left-side variant — .niwa-morph--left
   ========================================================================== */

.niwa-morph--left {
  right: auto;
  left: var(--niwa-flyout-offset-right);
}

html.niwa-mobile-morph-open,
body.niwa-mobile-morph-open {
  overflow: hidden !important;
  overscroll-behavior: none !important;
}

/* ==========================================================================
   Responsive
   ========================================================================== */
@media (max-width: 782px) {
  /* Center the configurable non-fullscreen vw/dvh size around the viewport. */
  .niwa-widget .niwa-morph.niwa-morph--open:not(.niwa-morph--fullscreen) {
    width: var(--niwa-mobile-flyout-width, 95vw) !important;
    max-width: var(--niwa-mobile-flyout-width, 95vw) !important;
    height: var(--niwa-mobile-flyout-height-fallback, 95vh) !important;
    height: var(--niwa-mobile-flyout-height, 95dvh) !important;
    max-height: var(--niwa-mobile-flyout-height-fallback, 95vh) !important;
    max-height: var(--niwa-mobile-flyout-height, 95dvh) !important;
    top: auto !important;
    right: calc((100vw - var(--niwa-mobile-flyout-width, 95vw)) / 2) !important;
    bottom: calc((100vh - var(--niwa-mobile-flyout-height-fallback, 95vh)) / 2) !important;
    bottom: calc((100dvh - var(--niwa-mobile-flyout-height, 95dvh)) / 2) !important;
    left: auto !important;
  }

  .niwa-widget .niwa-morph.niwa-morph--open.niwa-morph--left:not(.niwa-morph--fullscreen) {
    right: auto !important;
    left: calc((100vw - var(--niwa-mobile-flyout-width, 95vw)) / 2) !important;
  }

  .niwa-widget .niwa-morph.niwa-morph--open.niwa-morph--top:not(.niwa-morph--fullscreen) {
    top: calc((100vh - var(--niwa-mobile-flyout-height-fallback, 95vh)) / 2) !important;
    top: calc((100dvh - var(--niwa-mobile-flyout-height, 95dvh)) / 2) !important;
    bottom: auto !important;
  }

  /* Fullscreen mode — !important overrides inline position styles from PHP */
  .niwa-morph--open.niwa-morph--fullscreen {
    position: fixed !important;
    inset: auto 0 0 auto !important;
    width: 100vw !important;
    min-width: 0 !important;
    max-width: none !important;
    height: 100vh !important;
    height: 100dvh !important;
    max-height: none !important;
    box-sizing: border-box !important;
    margin: 0 !important;
    border-radius: 0 !important;
    /* Override overflow:clip so keyboard scroll-to-focus works */
    overflow: hidden !important;
  }

  /* Preserve the launcher's side while the pill morphs to fullscreen. */
  .niwa-morph--open.niwa-morph--fullscreen.niwa-morph--left {
    inset: auto auto 0 0 !important;
  }

  /* Keyboard-driven visualViewport updates must track each rendered frame;
     replaying the 360ms open morph for every intermediate height causes lag. */
  .niwa-morph--open.niwa-morph--viewport-resizing {
    transition: none !important;
  }

  /* ── Fullscreen input bar — dark theme visibility boost ───────────────── */

  /* Taller pill with separator lines (open only — collapsed pill stays 48px) */
  .niwa-morph--open.niwa-morph--fullscreen .niwa-morph__pill {
    height: auto !important;
    padding: 10px var(--niwa-space-md) !important;
    border-top: 1px solid rgba(255, 255, 255, 0.12) !important;
    border-bottom: 1px solid rgba(255, 255, 255, 0.12) !important;
  }

  /* Light theme separator lines */
  .niwa-widget[data-niwa-theme="light"] .niwa-morph--open.niwa-morph--fullscreen .niwa-morph__pill {
    border-top-color: rgba(0, 0, 0, 0.08) !important;
    border-bottom-color: rgba(0, 0, 0, 0.08) !important;
  }

  /* Send button always white in dark fullscreen (not dim when disabled) */
  [data-niwa-theme="dark"] .niwa-morph--open.niwa-morph--fullscreen .niwa-morph__send,
  [data-niwa-theme="dark"] .niwa-morph--open.niwa-morph--fullscreen .niwa-morph__send--disabled {
    background: #E8E6E1 !important;
    color: #111318 !important;
    opacity: 1 !important;
    pointer-events: auto !important;
    cursor: pointer !important;
  }
}

/* ==========================================================================
   ColorOrb
   ========================================================================== */

@property --niwa-orb-angle {
  syntax: "<angle>";
  inherits: false;
  initial-value: 0deg;
}

.niwa-orb {
  --niwa-orb-size: 24px;
  --niwa-orb-base: oklch(22.64% 0 0);
  --niwa-orb-accent1: oklch(75% 0.15 350);
  --niwa-orb-accent2: oklch(80% 0.12 200);
  --niwa-orb-accent3: oklch(78% 0.14 280);
  --niwa-orb-spin: 20s;
  display: grid;
  grid-template-areas: "stack";
  width: var(--niwa-orb-size);
  height: var(--niwa-orb-size);
  overflow: hidden;
  border-radius: 50%;
  position: relative;
  transform: scale(1.1);
}

.niwa-orb::before,
.niwa-orb::after {
  content: "";
  display: block;
  grid-area: stack;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  transform: translateZ(0);
}

.niwa-orb::before {
  background:
    conic-gradient(from calc(var(--niwa-orb-angle) * 2) at 25% 70%, var(--niwa-orb-accent3), transparent 20% 80%, var(--niwa-orb-accent3)),
    conic-gradient(from calc(var(--niwa-orb-angle) * 2) at 45% 75%, var(--niwa-orb-accent2), transparent 30% 60%, var(--niwa-orb-accent2)),
    conic-gradient(from calc(var(--niwa-orb-angle) * -3) at 80% 20%, var(--niwa-orb-accent1), transparent 40% 60%, var(--niwa-orb-accent1)),
    conic-gradient(from calc(var(--niwa-orb-angle) * 2) at 15% 5%, var(--niwa-orb-accent2), transparent 10% 90%, var(--niwa-orb-accent2)),
    conic-gradient(from calc(var(--niwa-orb-angle) * 1) at 20% 80%, var(--niwa-orb-accent1), transparent 10% 90%, var(--niwa-orb-accent1)),
    conic-gradient(from calc(var(--niwa-orb-angle) * -2) at 85% 10%, var(--niwa-orb-accent3), transparent 20% 80%, var(--niwa-orb-accent3));
  box-shadow: inset var(--niwa-orb-base) 0 0 0.5px 0.1px;
  filter: blur(0.2px) contrast(1.3);
  animation: niwa-orb-spin var(--niwa-orb-spin) linear infinite;
}

.niwa-orb::after {
  background-image: radial-gradient(circle at center, var(--niwa-orb-base) 0.05px, transparent 0.05px);
  background-size: 0.1px 0.1px;
  backdrop-filter: blur(0.4px) contrast(2.6);
  mix-blend-mode: overlay;
}

@keyframes niwa-orb-spin {
  to { --niwa-orb-angle: 360deg; }
}

@keyframes niwa-gradient-drift {
  from { transform: translate(-50%, -50%) rotate(0deg); }
  to   { transform: translate(-50%, -50%) rotate(360deg); }
}

@media (prefers-reduced-motion: reduce) {
  .niwa-orb::before { animation: none; }
  .niwa-morph--open::before { animation: none; }
  .niwa-center--open::before { animation: none; }
}
