/* ==========================================================================
   Niwa Chatbot – Centerstage (Center Pill → Full Chat)
   ---------------------------------------------------------------------------
   Expands SYMMETRICALLY from center (both left and right).
   Uses left:50% + translateX(-50%) for centering.

   Width, height and radius morph together so the pill stays one continuous
   shape during open and close.

   STRUCTURE:
     .niwa-center
       .niwa-center__chat     (header + body — clipped until tall)
       .niwa-center__pill     (bottom input bar — always visible)
   ========================================================================== */

.niwa-center {
  position: fixed;
  bottom: var(--niwa-flyout-offset-bottom);
  left: 50%;
  transform: translateX(-50%);
  z-index: var(--niwa-layer-floating);

  display: flex;
  flex-direction: column;
  width: 224px;
  height: 48px;
  overflow: visible;

  /* Glass — light */
  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;

  transition:
    width 360ms cubic-bezier(0.2, 0, 0, 1),
    height 360ms cubic-bezier(0.2, 0, 0, 1),
    bottom 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;
}

/* Dark glass */
.niwa-widget[data-niwa-theme="dark"] .niwa-center {
  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 glass — frosted glass visible on light backgrounds */
.niwa-widget[data-niwa-theme="light"] .niwa-center {
  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;
}

.niwa-widget[data-niwa-theme="dark"] .niwa-center:not(.niwa-center--open):not(.niwa-center--closing) {
  box-shadow:
    0 4px 14px rgba(0, 0, 0, 0.16),
    0 1px 0 rgba(255, 255, 255, 0.05) inset;
}

.niwa-widget[data-niwa-theme="neutral"] .niwa-center:not(.niwa-center--open):not(.niwa-center--closing) {
  box-shadow:
    0 4px 14px rgba(0, 0, 0, 0.12),
    0 1px 0 rgba(255, 255, 255, 0.10) inset;
}

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

/* CSS-only diffusion behind the closed launcher; the glass itself stays clean. */
.niwa-widget--frontend-shell
  :is(
    .niwa-morph:not(.niwa-morph--open):not(.niwa-morph--closing),
    .niwa-center:not(.niwa-center--open):not(.niwa-center--closing)
  ) {
  filter:
    drop-shadow(-9px 10px 13px rgba(86, 204, 242, 0.34))
    drop-shadow(9px 10px 13px rgba(234, 81, 255, 0.30));
}

.niwa-widget--frontend-shell
  :is(
    .niwa-morph:not(.niwa-morph--open):not(.niwa-morph--closing),
    .niwa-center:not(.niwa-center--open):not(.niwa-center--closing)
  ):hover {
  filter:
    drop-shadow(-10px 10px 14px rgba(86, 204, 242, 0.48))
    drop-shadow(10px 10px 14px rgba(234, 81, 255, 0.44));
}

.niwa-widget--frontend-shell[data-niwa-theme="dark"]
  :is(
    .niwa-morph:not(.niwa-morph--open):not(.niwa-morph--closing),
    .niwa-center:not(.niwa-center--open):not(.niwa-center--closing)
  ) {
  filter:
    drop-shadow(-7px 8px 11px rgba(86, 204, 242, 0.20))
    drop-shadow(7px 8px 11px rgba(234, 81, 255, 0.18));
}

.niwa-widget--frontend-shell[data-niwa-theme="dark"]
  :is(
    .niwa-morph:not(.niwa-morph--open):not(.niwa-morph--closing),
    .niwa-center:not(.niwa-center--open):not(.niwa-center--closing)
  ):hover {
  filter:
    drop-shadow(-8px 8px 12px rgba(86, 204, 242, 0.30))
    drop-shadow(8px 8px 12px rgba(234, 81, 255, 0.27));
}

/* Fine static texture on light glass, behind all interactive content. */
.niwa-widget--frontend-shell[data-niwa-theme="light"]
  :is(.niwa-morph, .niwa-center)::after {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 0;
  border-radius: inherit;
  background: url('../img/niwa-grain.svg') repeat;
  background-size: 128px 128px;
  mix-blend-mode: overlay;
  opacity: 0.24;
  pointer-events: none;
}

.niwa-widget--frontend-shell:is([data-niwa-theme="light"], [data-niwa-theme="neutral"])
  :is(.niwa-morph__pill, .niwa-center__pill) {
  z-index: 1;
}

.niwa-widget--frontend-shell:is([data-niwa-theme="light"], [data-niwa-theme="neutral"])
  :is(
    .niwa-morph--open .niwa-morph__header,
    .niwa-center--open .niwa-center__header
  ) {
  border-bottom: 1px solid rgba(255, 255, 255, 0.34);
}

.niwa-widget--frontend-shell:is([data-niwa-theme="light"], [data-niwa-theme="neutral"])
  :is(
    .niwa-morph--open > .niwa-morph__pill,
    .niwa-center--open > .niwa-center__pill
  ) {
  border-top: 1px solid rgba(255, 255, 255, 0.28);
}

/* ── Expanded ───────────────────────────────────────────────────────────── */
.niwa-center--open {
  width: 494px;
  height: var(--niwa-flyout-height);
  max-height: var(--niwa-flyout-max-height);
  border-radius: var(--niwa-radius-lg);
  cursor: default;
  overflow: hidden;
}

/* Animated gradient tint — light and neutral themes. */
.niwa-widget:is([data-niwa-theme="light"], [data-niwa-theme="neutral"]) .niwa-center--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;
  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-center--open > .niwa-center__chat,
.niwa-widget:is([data-niwa-theme="light"], [data-niwa-theme="neutral"]) .niwa-center--open > .niwa-center__pill {
  position: relative;
  z-index: 1;
}

/* Closing uses the same continuous morph, in reverse. */
.niwa-center--closing {
  overflow: hidden;
  transition:
    width 360ms cubic-bezier(0.2, 0, 0, 1),
    height 360ms cubic-bezier(0.2, 0, 0, 1),
    bottom 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;
}

/* ==========================================================================
   Chat Area
   ========================================================================== */

.niwa-center__chat {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease-out 100ms;
}

.niwa-center--open .niwa-center__chat {
  opacity: 1;
  pointer-events: auto;
}

.niwa-center--closing .niwa-center__chat {
  opacity: 0;
  transition: opacity 140ms ease-out;
}

/* Header */
.niwa-center__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-center__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-center__close:hover { background: rgba(0, 0, 0, 0.08); }
[data-niwa-theme="dark"] .niwa-center__close:hover { background: rgba(255, 255, 255, 0.1); }
.niwa-center__close svg {
  width: 16px; height: 16px;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round;
}

/* Body (scrollable, acts as message list container) */
.niwa-center__body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-width: thin;
  scrollbar-color: var(--niwa-color-border-default) transparent;

  /* Flex column layout — messages, typing, suggestions, date separators
     are appended here directly by JS (no .niwa-messages wrapper in WP). */
  display: flex;
  flex-direction: column;
  padding: var(--niwa-space-sm) 0;
  gap: var(--niwa-space-xs);
}
.niwa-center__body::-webkit-scrollbar { width: 6px; }
.niwa-center__body::-webkit-scrollbar-track { background: transparent; }
.niwa-center__body::-webkit-scrollbar-thumb {
  background: var(--niwa-color-border-default);
  border-radius: var(--niwa-radius-full);
}

/* ==========================================================================
   Pill Bar
   ========================================================================== */

.niwa-center__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-center--open.niwa-center--multiline .niwa-center__pill {
  min-height: 48px;
  height: auto;
  align-items: flex-end;
  padding-top: 8px;
  padding-bottom: 8px;
}

/* Badge — left side */
.niwa-center__badge {
  position: absolute;
  top: -6px;
  left: -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-center__badge:empty { display: none; }
.niwa-center--open .niwa-center__badge { display: none; }

/* Label */
.niwa-center__label {
  font-size: var(--niwa-font-size-md);
  font-weight: var(--niwa-font-weight-normal);
  color: #6B7280;
  white-space: nowrap;
  line-height: 48px;
}
[data-niwa-theme="dark"] .niwa-center__label {
  color: #9CA3AF;
}
.niwa-center--open .niwa-center__label {
  display: none;
}

/* Input — visible after width finishes */
.niwa-center__input {
  display: flex;
  flex: 1;
  align-items: center;
  gap: 4px;
  min-width: 0;
  min-height: 0;
  opacity: 0;
  pointer-events: none;
  transition: opacity 150ms ease;
}
.niwa-center--multiline .niwa-center__input {
  align-items: flex-end;
}
.niwa-center--open .niwa-center__input {
  opacity: 1;
  pointer-events: auto;
  transition: opacity 160ms ease-out 120ms;
}

/* Textarea — !important on layout-critical props to override WordPress themes */
.niwa-widget .niwa-center__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-center__textarea::placeholder { color: #6B7280; }
.niwa-widget[data-niwa-theme="dark"] .niwa-center__textarea::placeholder { color: #FFFFFF; opacity: 1; }
.niwa-widget[data-niwa-theme="dark"] .niwa-center__textarea { color: #E8E6E1; }

/* Send button */
.niwa-center__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-center__send:hover { opacity: 1; }
.niwa-center__send:active { transform: scale(0.95); }
.niwa-center__send svg { display: block; }
.niwa-center__send--disabled {
  opacity: 0.4;
  cursor: default;
  pointer-events: none;
  background: #9CA3AF;
}
[data-niwa-theme="dark"] .niwa-center__send {
  background: #E8E6E1;
  color: #111318;
}
[data-niwa-theme="dark"] .niwa-center__send--disabled {
  background: #4B5563;
}

/* Responsive */
@media (max-width: 782px) {
  /* Default: keep desktop size on mobile */
  .niwa-center--open {
    width: var(--niwa-flyout-width);
    max-height: var(--niwa-flyout-max-height);
  }

  /* Fullscreen mode (toggled via admin setting) */
  .niwa-center--open.niwa-morph--fullscreen {
    position: fixed !important;
    top: auto !important;
    right: auto !important;
    bottom: 0 !important;
    left: 50% !important;
    width: 100vw !important; min-width: 0 !important; max-width: none !important;
    height: 100vh !important; height: 100dvh !important;
    max-height: none !important; border-radius: 0 !important;
    box-sizing: border-box !important;
    margin: 0 !important;
    transform: translateX(-50%) !important;
    overflow: hidden !important;
  }

  .niwa-center--open.niwa-morph--viewport-resizing {
    transition: none !important;
  }

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

  /* Taller pill with separator lines */
  .niwa-center--open.niwa-morph--fullscreen .niwa-center__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-center--open.niwa-morph--fullscreen .niwa-center__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-center--open.niwa-morph--fullscreen .niwa-center__send,
  [data-niwa-theme="dark"] .niwa-center--open.niwa-morph--fullscreen .niwa-center__send--disabled {
    background: #E8E6E1 !important;
    color: #111318 !important;
    opacity: 1 !important;
    pointer-events: auto !important;
    cursor: pointer !important;
  }
}
