/* Ecopia webchat widget -- CP4. Self-hosted, zero CDN.
 * Replaces the Coze SDK. Design tokens taken from the site:
 *   #16a34a brand green, #f9fafb panel grey, #e5e7eb borders.
 * Positions match the old Coze block exactly so the page looks unchanged:
 *   launcher right 80px / bottom 100px, panel right 80px / bottom 180px.
 * All selectors are prefixed ecw- to guarantee no collision with the
 * React bundle's own class names.
 */

#ecw-launcher {
  position: fixed;
  right: 80px;
  bottom: 100px;
  width: 60px;
  height: 60px;
  border: none;
  padding: 0;
  border-radius: 50%;
  cursor: pointer;
  z-index: 9999;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  background: transparent;
}

#ecw-launcher img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
  display: block;
}

#ecw-launcher:focus-visible,
#ecw-send:focus-visible,
#ecw-close:focus-visible {
  outline: 3px solid #16a34a;
  outline-offset: 2px;
}

/* Unread dot, shown if a reply lands while the panel is closed. */
#ecw-badge {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #dc2626;
  border: 2px solid #fff;
  display: none;
}

#ecw-panel {
  position: fixed;
  right: 80px;
  bottom: 180px;
  width: 390px;
  height: 600px;
  max-height: calc(100vh - 200px);
  z-index: 9999;
  display: none;
  flex-direction: column;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 16px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.18);
  overflow: hidden;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, "Noto Sans TC", "Microsoft JhengHei", sans-serif;
}

#ecw-panel.ecw-open {
  display: flex;
}

#ecw-header {
  background: #16a34a;
  color: #fff;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}

#ecw-title {
  font-size: 15px;
  font-weight: 600;
  flex: 1 1 auto;
}

#ecw-close {
  background: transparent;
  border: none;
  color: #fff;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
  border-radius: 4px;
}

#ecw-log {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 12px;
  background: #f9fafb;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.ecw-msg {
  max-width: 82%;
  padding: 9px 12px;
  border-radius: 14px;
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.ecw-visitor {
  align-self: flex-end;
  background: #16a34a;
  color: #fff;
  border-bottom-right-radius: 4px;
}

.ecw-agent {
  align-self: flex-start;
  background: #fff;
  color: #111827;
  border: 1px solid #e5e7eb;
  border-bottom-left-radius: 4px;
}

/* System notices: queued, rate limited, offline, error. */
.ecw-system {
  align-self: center;
  background: #fef3c7;
  color: #92400e;
  border: 1px solid #fde68a;
  font-size: 13px;
  max-width: 92%;
  text-align: center;
}

.ecw-system.ecw-error {
  background: #fee2e2;
  color: #991b1b;
  border-color: #fecaca;
}

.ecw-msg a {
  color: #16a34a;
  text-decoration: underline;
}

.ecw-agent strong {
  font-weight: 600;
  color: #111827;
}

.ecw-visitor a {
  color: #fff;
}

/* Typing indicator -- must persist for the full ~30s agent turn. */
#ecw-typing {
  align-self: flex-start;
  display: none;
  align-items: center;
  gap: 6px;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 14px;
  padding: 10px 13px;
}

#ecw-typing.ecw-on {
  display: flex;
}

#ecw-typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #9ca3af;
  animation: ecw-bounce 1.4s infinite ease-in-out both;
}

#ecw-typing span:nth-child(2) {
  animation-delay: 0.2s;
}

#ecw-typing span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes ecw-bounce {
  0%, 80%, 100% { transform: scale(0.7); opacity: 0.5; }
  40% { transform: scale(1); opacity: 1; }
}



/* Composer */
#ecw-form {
  flex: 0 0 auto;
  display: flex;
  gap: 8px;
  padding: 10px;
  background: #fff;
  border-top: 1px solid #e5e7eb;
}

#ecw-input {
  flex: 1 1 auto;
  resize: none;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  padding: 9px 11px;
  font-size: 14px;
  font-family: inherit;
  line-height: 1.4;
  max-height: 96px;
  color: #111827;
}

#ecw-input:focus {
  outline: none;
  border-color: #16a34a;
}

#ecw-send {
  flex: 0 0 auto;
  background: #16a34a;
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 0 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

#ecw-send:disabled {
  background: #9ca3af;
  cursor: not-allowed;
}

/* Footer -- keeps the old #custom-chat-footer contact link. */
#ecw-footer {
  flex: 0 0 auto;
  background: #f9fafb;
  border-top: 1px solid #e5e7eb;
  padding: 8px 10px;
  text-align: center;
  font-size: 13px;
}

#ecw-footer a {
  color: #16a34a;
  text-decoration: underline;
  font-weight: 500;
}

/* The [Privacy Policy] label inside an agent reply. A button, not a link,
 * because it opens a pop-up window rather than navigating anywhere -- but
 * styled to read as a link and sized to match the surrounding text. */
.ecw-policy-link {
  background: none;
  border: none;
  padding: 0;
  margin: 0;
  font: inherit;
  color: #16a34a;
  font-weight: 600;
  text-decoration: underline;
  cursor: pointer;
}

.ecw-policy-link:hover {
  color: #15803d;
}

.ecw-policy-link:focus-visible {
  outline: 2px solid #111827;
  outline-offset: 2px;
  border-radius: 2px;
}

/* Mobile: full screen, per the CP4 brief. */
@media (max-width: 768px) {
  #ecw-panel {
    right: 0;
    bottom: 0;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    max-height: none;
    border-radius: 0;
    border: none;
  }

  #ecw-launcher {
    right: 12px;
    bottom: 12px;
    width: 56px;
    height: 56px;
  }

  .ecw-msg {
    max-width: 88%;
  }
}

/* Very short viewports in landscape. */
@media (max-height: 620px) and (min-width: 769px) {
  #ecw-panel {
    bottom: 100px;
    height: calc(100vh - 130px);
  }
}


@media (prefers-reduced-motion: reduce) {
  #ecw-typing span {
    animation: none;
  }
}
