#earthrise-chat-widget {
  --er-primary: #1a5276;
  --er-bg: #fff;
  --er-text: #1a1a1a;
  --er-border: #e0e0e0;
  --er-user-bg: #e8f0fe;
  --er-error-bg: #fdecea;

  position: fixed;
  bottom: 0;
  right: 0;
  z-index: 1050;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

[data-bs-theme="dark"] #earthrise-chat-widget {
  --er-primary: #2980b9;
  --er-bg: #1e1e1e;
  --er-text: #e0e0e0;
  --er-border: #444;
  --er-user-bg: #1a3a5c;
  --er-error-bg: #4a1a1a;
}

#earthrise-chat-toggle {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: var(--er-primary);
  color: #fff;
  cursor: pointer;
  box-shadow: 0 4px 12px rgb(0 0 0 / 25%);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  z-index: 1051;
}

#earthrise-chat-toggle:hover {
  transform: scale(1.05);
  box-shadow: 0 6px 16px rgb(0 0 0 / 30%);
}

#earthrise-chat-toggle svg {
  width: 28px;
  height: 28px;
  fill: currentcolor;
}

#earthrise-chat-panel {
  position: fixed;
  bottom: 92px;
  right: 24px;
  width: 380px;
  height: 520px;
  background: var(--er-bg);
  border: 1px solid var(--er-border);
  border-radius: 12px;
  box-shadow: 0 8px 30px rgb(0 0 0 / 15%);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 1051;
  transform: scale(0.9) translateY(20px);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.25s ease, opacity 0.25s ease;
}

#earthrise-chat-panel.open {
  transform: scale(1) translateY(0);
  opacity: 1;
  pointer-events: auto;
}

#earthrise-chat-panel header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--er-primary);
  color: #fff;
  flex-shrink: 0;
}

#earthrise-chat-panel header h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

#earthrise-chat-suggestions button {
  background: var(--er-bg);
  border: 1px solid var(--er-border);
  border-radius: 16px;
  padding: 8px 14px;
  color: var(--er-primary);
  cursor: pointer;
  font-size: 0.825rem;
  text-align: left;
  transition: background 0.15s;
}

#earthrise-chat-panel header button {
  background: none;
  border: none;
  color: #fff;
  cursor: pointer;
  font-size: 1.25rem;
  padding: 0 4px;
  line-height: 1;
}

.earthrise-chat-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

#earthrise-chat-new {
  font-size: 0.75rem;
  opacity: 0.85;
  padding: 2px 8px;
  border-radius: 4px;
}

#earthrise-chat-new:hover {
  opacity: 1;
  background: rgb(255 255 255 / 15%);
}

#earthrise-chat-suggestions button:hover {
  background: var(--er-user-bg);
}

#earthrise-chat-suggestions {
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}

#earthrise-chat-suggestions.hidden {
  display: none;
}

#earthrise-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#earthrise-chat-widget .msg {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 0.875rem;
  line-height: 1.5;
  color: var(--er-text);
  overflow-wrap: break-word;
}

#earthrise-chat-widget .msg-user {
  align-self: flex-end;
  background: var(--er-user-bg);
  border-bottom-right-radius: 4px;
}

#earthrise-chat-widget .msg-assistant {
  align-self: flex-start;
  background: var(--er-bg);
  border: 1px solid var(--er-border);
  border-bottom-left-radius: 4px;
}

#earthrise-chat-widget .msg-error {
  align-self: flex-start;
  background: var(--er-error-bg);
  border: 1px solid var(--er-border);
  border-bottom-left-radius: 4px;
  color: #c0392b;
}

#earthrise-chat-widget .msg-assistant p {
  margin: 0 0 8px;
}

#earthrise-chat-widget .msg-assistant p:last-child {
  margin-bottom: 0;
}

#earthrise-chat-widget .msg-assistant code {
  background: #f0f0f0;
  padding: 1px 4px;
  border-radius: 3px;
  font-size: 0.8rem;
}

#earthrise-chat-widget .msg-assistant pre code {
  background: none;
  padding: 0;
}

#earthrise-chat-widget .msg-assistant pre {
  background: #f5f5f5;
  padding: 8px;
  border-radius: 6px;
  overflow-x: auto;
  font-size: 0.8rem;
}

[data-bs-theme="dark"] #earthrise-chat-widget .msg-assistant pre {
  background: #2a2a2a;
}

[data-bs-theme="dark"] #earthrise-chat-widget .msg-assistant code {
  background: #333;
}

#earthrise-chat-widget .msg-assistant ul,
#earthrise-chat-widget .msg-assistant ol {
  margin: 4px 0;
  padding-left: 20px;
}

#earthrise-chat-widget .msg-assistant h3 {
  font-size: 1rem;
  font-weight: 600;
  margin: 10px 0 4px;
  line-height: 1.3;
}

#earthrise-chat-widget .msg-assistant h3:first-child {
  margin-top: 0;
}

#earthrise-chat-widget .msg-assistant h4 {
  font-size: 0.925rem;
  font-weight: 600;
  margin: 8px 0 4px;
  line-height: 1.3;
}

#earthrise-chat-widget .msg-assistant h4:first-child {
  margin-top: 0;
}

#earthrise-chat-widget .msg-assistant h5 {
  font-size: 0.875rem;
  font-weight: 600;
  margin: 6px 0 2px;
  line-height: 1.3;
}

#earthrise-chat-widget .msg-assistant h5:first-child {
  margin-top: 0;
}

#earthrise-chat-widget .msg-assistant .table-wrap {
  overflow-x: auto;
  margin: 8px 0;
}

#earthrise-chat-widget .msg-assistant table {
  border-collapse: collapse;
  font-size: 0.85rem;
  min-width: 100%;
}

#earthrise-chat-widget .msg-assistant th,
#earthrise-chat-widget .msg-assistant td {
  border: 1px solid var(--er-border);
  padding: 5px 8px;
  text-align: left;
}

#earthrise-chat-widget .msg-assistant th {
  background: var(--er-user-bg);
  font-weight: 600;
}

#earthrise-chat-widget .msg-assistant hr {
  border: none;
  border-top: 1px solid var(--er-border);
  margin: 8px 0;
}

#earthrise-chat-widget .msg-assistant .md-link {
  color: var(--er-primary);
  text-decoration: underline;
}

#earthrise-chat-widget .msg-assistant .md-link:hover {
  text-decoration: none;
}

#earthrise-chat-widget .citation-ref {
  font-size: 0.75rem;
  vertical-align: super;
  color: var(--er-primary);
  text-decoration: none;
  font-weight: 600;
  cursor: pointer;
}

#earthrise-chat-widget .citation-ref-plain {
  font-size: 0.75rem;
  vertical-align: super;
  color: var(--er-primary);
  font-weight: 600;
}

#earthrise-chat-widget details.sources {
  margin-top: 8px;
  font-size: 0.8rem;
  border-top: 1px solid var(--er-border);
  padding-top: 6px;
}

#earthrise-chat-widget details.sources summary {
  cursor: pointer;
  color: var(--er-primary);
  font-weight: 600;
}

#earthrise-chat-widget details.sources ul {
  margin: 4px 0 0;
  padding-left: 16px;
  list-style: none;
}

#earthrise-chat-widget details.sources li {
  margin: 2px 0;
}

#earthrise-chat-widget details.sources a {
  color: var(--er-primary);
  text-decoration: none;
}

#earthrise-chat-widget details.sources a:hover {
  text-decoration: underline;
}

#earthrise-chat-widget details.sources .source-ref {
  color: var(--er-primary);
  text-decoration: none;
  font-weight: 600;
}

#earthrise-chat-widget details.sources .source-ref:hover {
  text-decoration: underline;
}

#earthrise-chat-widget details.sources .source-ref-plain {
  color: var(--er-primary);
  font-weight: 600;
}

#earthrise-chat-widget .loading-dots {
  display: flex;
  gap: 4px;
  padding: 4px 0;
}

#earthrise-chat-widget .loading-dots span {
  width: 8px;
  height: 8px;
  background: var(--er-primary);
  border-radius: 50%;
  animation: er-pulse 1.2s ease-in-out infinite;
}

#earthrise-chat-widget .loading-dots span:nth-child(2) {
  animation-delay: 0.2s;
}

#earthrise-chat-widget .loading-dots span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes er-pulse {
  0%,
  80%,
  100% {
    opacity: 0.3;
    transform: scale(0.8);
  }

  40% {
    opacity: 1;
    transform: scale(1);
  }
}

#earthrise-chat-form {
  display: flex;
  padding: 10px 12px;
  gap: 8px;
  border-top: 1px solid var(--er-border);
  background: var(--er-bg);
  flex-shrink: 0;
}

#earthrise-chat-form textarea {
  flex: 1;
  border: 1px solid var(--er-border);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 0.875rem;
  resize: none;
  min-height: 38px;
  max-height: 120px;
  font-family: inherit;
  line-height: 1.4;
  background: var(--er-bg);
  color: var(--er-text);
}

#earthrise-chat-form textarea:focus {
  outline: 2px solid var(--er-primary);
  outline-offset: -1px;
}

#earthrise-chat-form button[type="submit"] {
  align-self: flex-end;
  background: var(--er-primary);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 600;
  transition: opacity 0.15s;
}

#earthrise-chat-form button[type="submit"]:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

#earthrise-chat-status {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
}

@media (width <= 640px) {
  #earthrise-chat-panel {
    width: calc(100vw - 16px);
    height: 80vh;
    right: 8px;
    bottom: 84px;
  }
}
