#apchat {
  --apchat-main: #49225b;
  --apchat-accent: #c5ff88;
  --apchat-bg: #000000;
  --apchat-soft: #1f1f1f;
  --apchat-text: #f2f2f2;
  --apchat-muted: #a8a8a8;
  --apchat-line: #333333;
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 9999;
  font-family: "Open Sans", Arial, sans-serif;
  font-size: 13px;
  line-height: 1.4;
  color: var(--apchat-text);
}

#apchat *,
#apchat *::before,
#apchat *::after {
  box-sizing: border-box;
}

#apchat [hidden] {
  display: none !important;
}

#apchat button {
  font-family: inherit;
  cursor: pointer;
}

#apchat-toggle {
  position: relative;
  width: 56px;
  height: 56px;
  border: 0;
  border-radius: 50%;
  background: var(--apchat-main);
  color: #ffffff;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Kui aken on lahti, ei ole ümmargust nuppu vaja: aken suletakse päise ristist. */
#apchat-toggle[aria-expanded="true"] {
  display: none;
}

/* Teatemull suletud akna nupu kõrval: tervitus ja lugemata vastused. */
#apchat-teaser {
  position: absolute;
  right: 68px;
  bottom: 6px;
  width: max-content;
  max-width: min(280px, calc(100vw - 100px));
  display: flex;
  align-items: flex-start;
  background: #ffffff;
  color: #222222;
  border-radius: 12px 12px 4px 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
  animation: apchat-teaser-in 0.35s ease-out;
}

#apchat-teaser-open {
  flex: 1;
  min-width: 0;
  border: 0;
  background: none;
  color: inherit;
  font-size: 13px;
  line-height: 1.35;
  text-align: left;
  padding: 10px 4px 10px 12px;
}

#apchat-teaser-text {
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
  overflow-wrap: anywhere;
}

#apchat-teaser-text.apchat-swap {
  animation: apchat-teaser-in 0.35s ease-out;
}

#apchat-teaser-close {
  flex: none;
  border: 0;
  background: none;
  color: #6b6b6b;
  font-size: 18px;
  line-height: 1;
  padding: 6px 8px;
}

@keyframes apchat-teaser-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

#apchat-toggle:focus-visible,
#apchat-panel button:focus-visible {
  outline: 2px solid var(--apchat-accent);
  outline-offset: 2px;
}

#apchat-input:focus,
#apchat-input:focus-visible {
  outline: none;
  border-color: #ffffff;
}

#apchat-dot {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--apchat-accent);
  border: 2px solid var(--apchat-main);
}

#apchat-panel {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 468px;
  max-width: calc(100vw - 32px);
  height: 676px;
  max-height: calc(100vh - 32px);
  display: flex;
  flex-direction: column;
  background: var(--apchat-bg);
  border: 1px solid var(--apchat-line);
  border-radius: 12px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
  overflow: hidden;
}

#apchat-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  background: var(--apchat-main);
  color: #ffffff;
  font-weight: 600;
}

#apchat-title {
  display: flex;
  align-items: center;
  gap: 8px;
}

#apchat-live {
  flex: none;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--apchat-accent);
  box-shadow: 0 0 4px 1px rgba(197, 255, 136, 0.7);
  animation: apchat-pulse 2s ease-out infinite;
}

@keyframes apchat-pulse {
  0% { box-shadow: 0 0 4px 1px rgba(197, 255, 136, 0.7), 0 0 0 0 rgba(197, 255, 136, 0.6); }
  70% { box-shadow: 0 0 4px 1px rgba(197, 255, 136, 0.7), 0 0 0 5px rgba(197, 255, 136, 0); }
  100% { box-shadow: 0 0 4px 1px rgba(197, 255, 136, 0.7), 0 0 0 0 rgba(197, 255, 136, 0); }
}

#apchat-head-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

#apchat-new {
  border: 1px solid rgba(255, 255, 255, 0.5);
  border-radius: 6px;
  background: none;
  color: #ffffff;
  font-size: 12px;
  font-weight: 400;
  padding: 3px 8px;
}

#apchat-new:hover {
  background: rgba(255, 255, 255, 0.15);
}

#apchat-close {
  border: 0;
  background: none;
  color: #ffffff;
  font-size: 24px;
  line-height: 1;
  padding: 0 4px;
}

#apchat-log {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.apchat-msg {
  max-width: 85%;
  padding: 8px 12px;
  border-radius: 12px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.apchat-bot,
.apchat-agent {
  align-self: flex-start;
  background: var(--apchat-soft);
  border-bottom-left-radius: 4px;
}

.apchat-agent {
  border-left: 3px solid var(--apchat-accent);
}

.apchat-user {
  align-self: flex-end;
  background: var(--apchat-main);
  color: #ffffff;
  border-bottom-right-radius: 4px;
}

.apchat-system {
  align-self: center;
  max-width: 100%;
  padding: 4px 8px;
  font-size: 12px;
  color: var(--apchat-muted);
  text-align: center;
}

.apchat-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--apchat-accent);
  margin-bottom: 2px;
}

.apchat-msg a {
  color: inherit;
  text-decoration: underline;
}

.apchat-typing {
  display: flex;
  gap: 4px;
  padding: 12px;
}

.apchat-typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--apchat-muted);
  animation: apchat-blink 1.2s infinite both;
}

.apchat-typing span:nth-child(2) { animation-delay: 0.2s; }
.apchat-typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes apchat-blink {
  0%, 80%, 100% { opacity: 0.25; }
  40% { opacity: 1; }
}

#apchat-form {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--apchat-line);
}

#apchat-input {
  flex: 1;
  min-width: 0;
  resize: none;
  border: 1px solid #4a4a4a;
  border-radius: 8px;
  padding: 8px 10px;
  font: inherit;
  color: var(--apchat-text);
  background: #141414;
}

#apchat-input::placeholder {
  color: var(--apchat-muted);
}

#apchat-send {
  border: 0;
  flex: none;
  width: 42px;
  border-radius: 8px;
  padding: 0;
  background: var(--apchat-main);
  color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
}

#apchat-send:disabled {
  opacity: 0.5;
  cursor: default;
}

#apchat-foot {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px 10px;
  font-size: 11px;
  color: var(--apchat-muted);
}

#apchat-cta {
  flex: none;
  border: 0;
  border-radius: 8px;
  padding: 7px 12px;
  background: var(--apchat-accent);
  color: #1d1d1d;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}

@media (max-width: 480px) {
  #apchat {
    right: 12px;
    bottom: 12px;
  }

  #apchat-panel {
    position: fixed;
    left: 8px;
    right: 8px;
    bottom: 8px;
    width: auto;
    max-width: none;
    height: auto;
    top: 8px;
    max-height: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .apchat-typing span {
    animation: none;
    opacity: 0.6;
  }

  #apchat-live {
    animation: none;
  }

  #apchat-teaser,
  #apchat-teaser-text.apchat-swap {
    animation: none;
  }
}
