/* AI chat assistant bubble -- site-wide (rendered from base.html only
   when ANTHROPIC_API_KEY is configured; see chat_enabled context
   processor). Deliberately small and out of the way in its closed state:
   a single round icon button in the corner, not a banner or anything
   that competes with the archive itself for attention. */

.chat-widget {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 40;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 12px;
  /* Android WebView (the app is a Capacitor shell around this same site)
     has a long-standing bug where position:fixed elements visibly drift
     with the page during touch-scroll and only snap back into place once
     scrolling settles, instead of staying pinned throughout. Promoting
     the widget to its own GPU compositing layer is the standard fix. */
  transform: translateZ(0);
  -webkit-transform: translateZ(0);
  will-change: transform;
}

.chat-bubble {
  width: 52px;
  height: 52px;
  border-radius: 999px;
  border: none;
  background: var(--accent);
  color: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: var(--shadow);
  flex: 0 0 auto;
}

.chat-bubble:hover {
  background: var(--accent-hover);
}

/* Closed state: the bubble is Xher's crimson emblem itself. */
.chat-bubble:has(.chat-bubble__emblem) {
  width: 64px;
  height: 64px;
  padding: 0;
  overflow: hidden;
  background: #0b0405;
  box-shadow: 0 0 0 1px rgba(201, 162, 77, 0.55), 0 4px 18px rgba(140, 10, 20, 0.45), var(--shadow);
  transition: transform 0.18s ease, box-shadow 0.18s ease;
}
.chat-bubble:has(.chat-bubble__emblem):hover {
  background: #0b0405;
  transform: scale(1.06);
  box-shadow: 0 0 0 1px rgba(201, 162, 77, 0.8), 0 6px 24px rgba(180, 16, 28, 0.6), var(--shadow);
}
.chat-bubble__emblem {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 999px;
  object-fit: cover;
}
/* Open state: plain close button, same footprint. */
.chat-bubble[aria-expanded="true"]:has(.chat-bubble__emblem) {
  background: var(--accent);
  transform: none;
}
@media (prefers-reduced-motion: reduce) {
  .chat-bubble:has(.chat-bubble__emblem),
  .chat-bubble:has(.chat-bubble__emblem):hover { transition: none; transform: none; }
}

.chat-bubble[aria-expanded="true"] .chat-bubble__icon-open {
  display: none;
}
.chat-bubble[aria-expanded="true"] .chat-bubble__icon-close {
  display: block !important;
}

.chat-panel {
  width: min(360px, calc(100vw - 32px));
  height: min(520px, calc(100vh - 110px));
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-hover);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-panel[hidden] {
  display: none;
}

.chat-panel__header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.chat-panel__title {
  display: block;
  font-family: var(--font-serif);
  color: var(--text);
  font-size: 1.02rem;
}
.chat-panel__subtitle {
  display: block;
  color: var(--text-muted);
  font-size: 0.78rem;
  margin-top: 2px;
}

.chat-panel__log {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 14px 14px 6px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Shown instead of the log+form when the visitor's tier doesn't unlock
   the assistant (see profile.can_use_chat_assistant in base.html) --
   fills the same panel space rather than leaving it half-empty. */
.chat-panel__locked {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 28px 22px;
  text-align: center;
}
.chat-panel__locked-text {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.88rem;
  line-height: 1.55;
}
.chat-panel__upgrade-link {
  display: inline-block;
  padding: 9px 18px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--bg);
  font-size: 0.85rem;
  font-weight: 600;
  text-decoration: none;
}
.chat-panel__upgrade-link:hover {
  background: var(--accent-hover);
  text-decoration: none;
}

.chat-msg {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-width: 88%;
}
.chat-msg--user {
  align-self: flex-end;
  align-items: flex-end;
}
.chat-msg--assistant {
  align-self: flex-start;
  align-items: flex-start;
}

.chat-msg__bubble {
  border-radius: 14px;
  padding: 9px 12px;
  font-size: 0.9rem;
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.chat-msg--user .chat-msg__bubble {
  background: var(--accent-soft);
  color: var(--text);
  border-bottom-right-radius: 4px;
}
.chat-msg--assistant .chat-msg__bubble {
  background: var(--surface-muted);
  color: var(--text);
  border-bottom-left-radius: 4px;
}
.chat-msg--error .chat-msg__bubble {
  background: var(--surface-muted);
  color: var(--text-muted);
  font-style: italic;
}

.chat-msg__suggestions {
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
}
.chat-suggestion {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.82rem;
  text-decoration: none;
}
.chat-suggestion:hover {
  border-color: var(--accent);
  text-decoration: none;
}
.chat-suggestion__cover {
  width: 24px;
  height: 32px;
  object-fit: cover;
  border-radius: 3px;
  flex: 0 0 auto;
  background: var(--surface-muted);
}
.chat-suggestion__text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.chat-suggestion__title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-suggestion__author {
  color: var(--text-muted);
  font-size: 0.75rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-msg__citations {
  display: flex;
  flex-direction: column;
  gap: 5px;
  width: 100%;
}
.chat-citation {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  background: var(--surface);
  color: var(--text);
  font-size: 0.8rem;
  text-decoration: none;
}
.chat-citation:hover {
  border-color: var(--accent);
  text-decoration: none;
}
.chat-citation__icon {
  color: var(--accent);
  line-height: 1.2;
  flex: 0 0 auto;
}
.chat-citation__text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.chat-citation__title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-citation__page {
  color: var(--accent);
  font-size: 0.74rem;
}

.chat-typing {
  display: inline-flex;
  gap: 4px;
  padding: 10px 12px;
}
.chat-typing span {
  width: 6px;
  height: 6px;
  border-radius: 999px;
  background: var(--text-muted);
  animation: chat-typing-bounce 1.1s infinite ease-in-out;
}
.chat-typing span:nth-child(2) { animation-delay: 0.15s; }
.chat-typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes chat-typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-4px); opacity: 1; }
}

.chat-panel__form {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
}
.chat-panel__input {
  flex: 1 1 auto;
  resize: none;
  max-height: 90px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-muted);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 0.88rem;
  padding: 8px 10px;
  line-height: 1.4;
}
.chat-panel__input:focus {
  outline: none;
  border-color: var(--accent);
}
.chat-panel__send {
  flex: 0 0 auto;
  width: 36px;
  height: 36px;
  border-radius: 999px;
  border: none;
  background: var(--accent);
  color: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.chat-panel__send:hover {
  background: var(--accent-hover);
}
.chat-panel__send:disabled {
  opacity: 0.5;
  cursor: default;
}

@media (max-width: 480px) {
  .chat-widget {
    right: 12px;
    bottom: 12px;
  }
  .chat-panel {
    width: calc(100vw - 24px);
    height: min(70vh, calc(100vh - 100px));
  }
}
