/* NimbleResolve embeddable chat — compact bottom-right pattern (helpdesk-style: small launcher + capped panel). */

#nr-chat-fab,
#nr-chat-window {
  --cw-primary: #2563eb;
  --cw-primary-hover: #1d4ed8;
  --cw-surface: #ffffff;
  --cw-header-bg: #2563eb;
  --cw-header-fg: #ffffff;
  --cw-body-fg: #1e293b;
  --cw-agent-bg: #f1f5f9;
  --cw-agent-fg: #1e293b;
  --cw-border: #e2e8f0;
  --cw-meta: #94a3b8;
  --cw-label: #334155;
  --cw-error: #dc2626;
  --cw-shadow: rgba(15, 23, 42, 0.12);
  --cw-fab-glow: rgba(37, 99, 235, 0.22);
  --cw-focus-ring: rgba(37, 99, 235, 0.2);
  /* Layout tokens (aligned with typical Freshdesk / Intercom–class widgets) */
  --cw-fab-size: 52px;
  --cw-fab-icon: 22px;
  --cw-edge: 16px;
  --cw-panel-w: 336px;
  --cw-panel-max-h: 420px;
  --cw-radius: 12px;
}

#nr-chat-fab.nr-chat--dark,
#nr-chat-window.nr-chat--dark {
  --cw-primary: #3b82f6;
  --cw-primary-hover: #60a5fa;
  --cw-surface: #0f172a;
  --cw-header-bg: #1e3a5f;
  --cw-header-fg: #f1f5f9;
  --cw-body-fg: #e2e8f0;
  --cw-agent-bg: #334155;
  --cw-agent-fg: #e2e8f0;
  --cw-border: #475569;
  --cw-meta: #94a3b8;
  --cw-label: #cbd5e1;
  --cw-error: #f87171;
  --cw-shadow: rgba(0, 0, 0, 0.4);
  --cw-fab-glow: rgba(59, 130, 246, 0.3);
  --cw-focus-ring: rgba(59, 130, 246, 0.25);
}

#nr-chat-fab {
  position: fixed;
  bottom: calc(var(--cw-edge) + env(safe-area-inset-bottom, 0px));
  right: calc(var(--cw-edge) + env(safe-area-inset-right, 0px));
  width: var(--cw-fab-size);
  height: var(--cw-fab-size);
  border-radius: 50%;
  background: var(--cw-primary);
  border: none;
  color: #fff;
  cursor: pointer;
  box-shadow: 0 2px 12px var(--cw-fab-glow), 0 1px 3px var(--cw-shadow);
  z-index: 99999;
  transition:
    transform 0.18s ease,
    background 0.18s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

#nr-chat-fab:hover {
  transform: scale(1.05);
  background: var(--cw-primary-hover);
}

@media (prefers-reduced-motion: reduce) {
  #nr-chat-fab:hover {
    transform: none;
  }
}

#nr-chat-fab svg {
  width: var(--cw-fab-icon);
  height: var(--cw-fab-icon);
}

#nr-chat-window {
  position: fixed;
  bottom: calc(var(--cw-fab-size) + var(--cw-edge) + 10px + env(safe-area-inset-bottom, 0px));
  right: calc(var(--cw-edge) + env(safe-area-inset-right, 0px));
  width: min(var(--cw-panel-w), calc(100vw - var(--cw-edge) * 2));
  max-height: min(var(--cw-panel-max-h), calc(100dvh - var(--cw-fab-size) - var(--cw-edge) * 3));
  background: var(--cw-surface);
  color: var(--cw-body-fg);
  border-radius: var(--cw-radius);
  box-shadow: 0 4px 24px var(--cw-shadow), 0 0 0 1px var(--cw-border);
  z-index: 99999;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  transition:
    background 0.2s ease,
    color 0.2s ease;
}

/* Pre-chat: panel sizes to content — avoid a tall empty message area (previous bug). */
#nr-chat-window:has(#nr-chat-start) {
  max-height: min(520px, calc(100dvh - var(--cw-fab-size) - var(--cw-edge) * 3));
}

#nr-chat-window.nr-hidden {
  display: none;
}

.nr-chat-header {
  flex-shrink: 0;
  padding: 10px 12px;
  min-height: 44px;
  box-sizing: border-box;
  background: var(--cw-header-bg);
  color: var(--cw-header-fg);
  font-weight: 600;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  transition: background 0.2s ease;
}

.nr-chat-close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  margin: -6px -8px -6px 0;
  padding: 0;
  background: transparent;
  border: none;
  border-radius: 8px;
  color: inherit;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  opacity: 0.9;
}

.nr-chat-close:hover {
  opacity: 1;
  background: rgba(255, 255, 255, 0.12);
}

.nr-chat-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 12px;
  -webkit-overflow-scrolling: touch;
}

/* Pre-chat form: no forced “message list” min-height */
.nr-chat-body:has(#nr-chat-start) {
  flex: 0 1 auto;
  overflow-y: visible;
  padding: 0;
}

/* Active conversation: scrollable transcript, capped like vendor widgets */
.nr-chat-body:not(:has(#nr-chat-start)) {
  min-height: 120px;
  max-height: min(240px, 38dvh);
}

.nr-chat-msg {
  margin-bottom: 10px;
  max-width: 88%;
}

.nr-chat-msg-customer {
  margin-left: auto;
  text-align: right;
}

.nr-chat-msg-agent {
  margin-right: auto;
}

.nr-chat-bubble {
  display: inline-block;
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.45;
}

.nr-chat-msg-customer .nr-chat-bubble {
  background: var(--cw-primary);
  color: #fff;
  border-bottom-right-radius: 3px;
}

.nr-chat-msg-agent .nr-chat-bubble {
  background: var(--cw-agent-bg);
  color: var(--cw-agent-fg);
  border-bottom-left-radius: 3px;
}

.nr-chat-meta {
  font-size: 11px;
  color: var(--cw-meta);
  margin-top: 2px;
}

.nr-chat-footer {
  flex-shrink: 0;
  padding: 10px 12px;
  border-top: 1px solid var(--cw-border);
  display: flex;
  gap: 8px;
  align-items: center;
}

.nr-chat-footer input {
  flex: 1;
  min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--cw-border);
  border-radius: 8px;
  font-size: 15px;
  outline: none;
  background: var(--cw-surface);
  color: var(--cw-body-fg);
}

.nr-chat-footer input:focus {
  border-color: var(--cw-primary);
  box-shadow: 0 0 0 2px var(--cw-focus-ring);
}

.nr-chat-footer input::placeholder {
  color: var(--cw-meta);
}

.nr-chat-footer button {
  flex-shrink: 0;
  padding: 8px 12px;
  background: var(--cw-primary);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  min-height: 40px;
  transition: background 0.15s ease;
}

.nr-chat-footer button:hover {
  background: var(--cw-primary-hover);
}

.nr-chat-turnstile-slot {
  margin-top: 6px;
  display: flex;
  justify-content: flex-start;
  max-width: 100%;
  overflow: hidden;
}

.nr-chat-start {
  padding: 12px;
}

.nr-chat-start label {
  display: block;
  margin-bottom: 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--cw-label);
}

.nr-chat-start input,
.nr-chat-start textarea {
  display: block;
  width: 100%;
  box-sizing: border-box;
  margin-top: 4px;
  padding: 8px 10px;
  border: 1px solid var(--cw-border);
  border-radius: 8px;
  font-size: 16px;
  background: var(--cw-surface);
  color: var(--cw-body-fg);
}

.nr-chat-start textarea {
  resize: vertical;
  min-height: 52px;
  max-height: 120px;
}

.nr-chat-start button {
  margin-top: 10px;
  width: 100%;
  padding: 10px;
  background: var(--cw-primary);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  min-height: 44px;
  transition: background 0.15s ease;
}

.nr-chat-start button:hover {
  background: var(--cw-primary-hover);
}

.nr-chat-start button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.nr-chat-error {
  color: var(--cw-error);
  font-size: 12px;
  margin-top: 6px;
}

.nr-chat-start .nr-field-error {
  color: var(--cw-error);
  font-size: 12px;
  margin-top: 2px;
  display: none;
}

@media (min-width: 641px) {
  .nr-chat-start input,
  .nr-chat-start textarea {
    font-size: 14px;
  }
}

/* Small phones: edge-to-edge sheet feel but still below status bar */
@media (max-width: 480px) {
  #nr-chat-window {
    right: max(10px, env(safe-area-inset-right, 0px));
    left: max(10px, env(safe-area-inset-left, 0px));
    width: auto;
    max-height: min(72dvh, 480px);
    bottom: calc(var(--cw-fab-size) + 12px + env(safe-area-inset-bottom, 0px));
  }

  #nr-chat-window:has(#nr-chat-start) {
    max-height: min(78dvh, 520px);
  }

  .nr-chat-body:not(:has(#nr-chat-start)) {
    max-height: min(200px, 32dvh);
  }
}

html.nr-scroll-locked,
body.nr-scroll-locked {
  overflow: hidden !important;
  touch-action: none;
}
