:host {
  --maci-brand-red: var(--maci-brand-primary, #b52025);
  --maci-brand-red-dark: #901a1e;
  --maci-ink: #1F2A33;
  --maci-ink-soft: #4A5965;
  --maci-ink-mute: #6E7B86;
  --maci-rule: #E3E6E9;
  --maci-ai-accent: var(--maci-brand-accent, #0B6C8A);
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 101;
  font-family: 'Source Sans 3', system-ui, -apple-system, sans-serif;
}

/* FAB trigger */
#trigger {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: var(--maci-brand-red);
  color: #fff;
  border: 0;
  box-shadow: 0 8px 24px rgba(181, 32, 37, 0.35), 0 2px 6px rgba(0, 0, 0, 0.12);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.18s, box-shadow 0.18s;
  position: relative;
}

#trigger:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 28px rgba(181, 32, 37, 0.45);
}

#trigger.hidden {
  transform: scale(0.3);
  opacity: 0;
  pointer-events: none;
}

.ping {
  position: absolute;
  top: -2px;
  right: -2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #1FA667;
  border: 2px solid #fff;
}

/* Chat panel */
#panel {
  display: flex;
  flex-direction: column;
  width: 380px;
  height: 580px;
  max-height: calc(100vh - 48px);
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.22), 0 4px 12px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  margin-bottom: 12px;
  transform-origin: bottom right;
  transition: transform 0.22s ease, opacity 0.22s ease;
}

#panel.hidden {
  transform: scale(0.2) translateY(40px);
  opacity: 0;
  pointer-events: none;
  display: none;
}

.msg-text h3,
.msg-text h4,
.msg-text h5 {
  margin: 0 0 8px;
  font-weight: 700;
  line-height: 1.35;
}

.msg-text h4 {
  font-size: 15px;
}

.msg-text h5 {
  font-size: 14px;
}

.msg-text p {
  margin: 0 0 8px;
}

.msg-text p:last-child {
  margin-bottom: 0;
}

.msg-text ul,
.msg-text ol {
  margin: 0 0 8px 18px;
  padding: 0;
}

.msg-text li {
  margin-bottom: 4px;
}

.msg-text a {
  color: var(--maci-ai-accent);
  text-decoration: underline;
}

/* Header */
#header {
  background: var(--maci-brand-red);
  color: #fff;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.chat-avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: #fff;
  color: var(--maci-brand-red);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 15px;
  flex-shrink: 0;
}

.head-text {
  flex: 1;
  line-height: 1.2;
}

.head-text .title {
  font-weight: 700;
  font-size: 14px;
}

.head-text .sub {
  font-size: 11px;
  opacity: 0.85;
  display: flex;
  align-items: center;
  gap: 5px;
  margin-top: 2px;
}

.live-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #5BE39D;
  display: inline-block;
}

.head-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.head-actions button {
  background: transparent;
  border: 0;
  color: #fff;
  cursor: pointer;
  width: 30px;
  height: 30px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0.85;
}

.head-actions button:hover {
  background: rgba(255, 255, 255, 0.15);
  opacity: 1;
}

/* Messages */
#messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 14px;
  background: #F7F8FA;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.msg-row {
  display: flex;
  gap: 8px;
  max-width: 88%;
}

.msg-row.user {
  margin-left: auto;
  flex-direction: row-reverse;
}

.msg-avatar {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--maci-brand-red);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  align-self: flex-start;
}

.msg-row.user .msg-avatar {
  background: var(--maci-ink);
}

.msg-bubble {
  background: #fff;
  border: 1px solid #E8EBEF;
  padding: 10px 12px;
  border-radius: 12px;
  border-top-left-radius: 4px;
  font-size: 13.5px;
  line-height: 1.5;
  color: var(--maci-ink);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.02);
  word-break: break-word;
}

.msg-row.user .msg-bubble {
  background: var(--maci-brand-red);
  color: #fff;
  border-color: var(--maci-brand-red);
  border-radius: 12px;
  border-top-right-radius: 4px;
}

.msg-bubble .src {
  display: block;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid #EEF1F4;
  font-size: 11px;
  color: var(--maci-ink-mute);
}

.msg-bubble .src a {
  color: var(--maci-ai-accent);
  font-weight: 600;
  text-decoration: none;
}

.msg-bubble .src a:hover {
  text-decoration: underline;
}

.msg-bubble ul {
  margin: 6px 0 0;
  padding-left: 18px;
}

.msg-bubble ul li {
  margin-bottom: 3px;
}

.msg-bubble.escalate {
  background: #FFF8E6;
  border-color: #F1DEA3;
}

.msg-bubble.escalate .ref {
  display: inline-block;
  margin-top: 6px;
  font-family: ui-monospace, monospace;
  background: #fff;
  padding: 3px 8px;
  border-radius: 3px;
  border: 1px solid #E6D9A8;
  font-size: 11px;
  color: var(--maci-ink);
}

.msg-bubble.escalate .escalate-meta {
  font-size: 11px;
  color: var(--maci-ink-mute);
  margin-top: 6px;
}

/* Feedback */
.feedback-row {
  display: flex;
  gap: 4px;
  margin-top: 8px;
  padding-top: 6px;
  border-top: 1px dashed #EEF1F4;
  align-items: center;
}

.feedback-row .lbl {
  font-size: 10px;
  color: var(--maci-ink-mute);
  margin-right: 4px;
  letter-spacing: 0.3px;
}

.feedback-btn {
  background: transparent;
  border: 1px solid var(--maci-rule);
  color: var(--maci-ink-mute);
  width: 24px;
  height: 24px;
  border-radius: 50%;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
  padding: 0;
}

.feedback-btn:hover {
  border-color: var(--maci-ink-soft);
  color: var(--maci-ink);
}

.feedback-btn.up.active {
  background: #E8F5EE;
  border-color: #5EB37E;
  color: #1E8045;
}

.feedback-btn.down.active {
  background: #FCEBEE;
  border-color: #D87686;
  color: var(--maci-brand-red);
}

.feedback-thanks {
  font-size: 10px;
  color: var(--maci-ink-mute);
  font-style: italic;
  margin-left: 6px;
}

/* Typing dots */
.typing-dots {
  display: inline-flex;
  gap: 4px;
  padding: 4px 0;
}

.typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--maci-ink-mute);
  animation: chatBounce 1.2s infinite;
}

.typing-dots span:nth-child(2) {
  animation-delay: 0.15s;
}

.typing-dots span:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes chatBounce {
  0%,
  60%,
  100% {
    transform: translateY(0);
    opacity: 0.5;
  }

  30% {
    transform: translateY(-3px);
    opacity: 1;
  }
}

/* Suggestion chips */
.suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 4px 4px 8px;
}

.suggestion-chip {
  background: #fff;
  border: 1px solid #D8DDE2;
  color: var(--maci-ink-soft);
  font-family: inherit;
  font-size: 12px;
  padding: 6px 11px;
  border-radius: 999px;
  cursor: pointer;
  text-align: left;
  transition: border-color 0.15s, color 0.15s;
}

.suggestion-chip:hover {
  border-color: var(--maci-brand-red);
  color: var(--maci-brand-red);
}

/* Footer */
#footer {
  border-top: 1px solid #E8EBEF;
  padding: 10px 12px;
  background: #fff;
  flex-shrink: 0;
}

.input-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.input-row textarea {
  flex: 1;
  border: 1px solid #D8DDE2;
  border-radius: 20px;
  padding: 9px 14px;
  font-family: inherit;
  font-size: 13px;
  resize: none;
  max-height: 150px;
  outline: none;
  line-height: 1.4;
}

.input-row textarea:focus {
  border-color: var(--maci-brand-red);
}

#send-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--maci-brand-red);
  color: #fff;
  border: 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s;
}

#send-btn:hover {
  background: var(--maci-brand-red-dark);
}

#send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.disclaimer {
  font-size: 10px;
  color: var(--maci-ink-mute);
  text-align: center;
  margin-top: 6px;
}

.disclaimer a {
  color: var(--maci-ink-mute);
  text-decoration: underline;
}

@media (max-width: 720px) {
  :host {
    left: 8px;
    right: 8px;
    bottom: 8px;
  }

  #panel {
    width: auto;
    height: 70vh;
    max-height: calc(100vh - 16px);
  }
}
