@import url("https://fonts.googleapis.com/css2?family=Raleway:wght@400;500;600;700&family=Roboto:ital,wght@0,300;0,400;0,500;0,700;1,300;1,400&display=swap");

/* ============================================================================
   1. Tokens & resets
   ============================================================================ */

:root {
  /* Colors */
  --chat--color-primary: #363839;
  --chat--color-primary-shade-50: #2a2c2d;
  --chat--color-primary-shade-100: #1e1f20;
  --chat--color-secondary: #515658;
  --chat--color-secondary-shade-50: #363839;
  --chat--color-white: #ffffff;
  --chat--color-light: #f2f1f0;
  --chat--color-light-shade-50: #e8e7e6;
  --chat--color-light-shade-100: #dedcdb;
  --chat--color-medium: #8b8c8d;
  --chat--color-dark: #363839;
  --chat--color-disabled: #94a3b8;
  --chat--color-typing: #515658;

  /* Base Layout */
  --chat--spacing: 1rem;
  --chat--border-radius: 0.25rem;
  --chat--transition-duration: 0.15s;
  --chat--font-family:
    "Roboto", -apple-system, BlinkMacSystemFont, "Segoe UI", Oxygen-Sans,
    Ubuntu, Cantarell, "Helvetica Neue", sans-serif;

  /* Window Dimensions */
  --chat--window--width: 420px;
  --chat--window--height: 640px;

  /* Message Styles */
  --chat--message--font-size: 1rem;
  --chat--message--padding: 0.75rem 1rem;
  --chat--message--border-radius: 1rem;
  --chat--message-line-height: 1.5;
  --chat--message--margin-bottom: 0.5rem;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: var(--chat--font-family) !important;
}

body {
  background: var(--chat--color-light);
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

/* ============================================================================
   2. App shell: chat ↔ iframe split, resizer, drag overlay
   ============================================================================ */

.main-wrapper {
  width: 100%;
  max-width: 800px;
  height: 90vh;
  display: flex;
  gap: 0;
  transition: all 0.4s ease;
}

.main-wrapper.iframe-visible {
  max-width: 100%;
  width: 100%;
  height: 100vh;
}

.chat-container {
  width: 100%;
  height: 100%;
  background: var(--chat--color-white);
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: all 0.4s ease;
  flex-shrink: 0;
}

.main-wrapper.iframe-visible .chat-container {
  width: 25%;
  min-width: 320px;
  border-radius: 0;
  box-shadow: none;
  border-right: 1px solid var(--chat--color-light-shade-100);
}

.iframe-panel {
  width: 0;
  height: 100vh;
  background: var(--chat--color-white);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 0.4s ease;
  flex-shrink: 0;
}

.main-wrapper.iframe-visible .iframe-panel {
  width: 75%;
  min-width: 900px;
}

/* Resizer between chat and iframe */
.resizer {
  width: 5px;
  background: var(--chat--color-light-shade-100);
  cursor: col-resize;
  display: none;
  transition: background-color 0.2s;
  flex-shrink: 0;
}

.resizer:hover {
  background: var(--chat--color-medium);
}

.main-wrapper.iframe-visible .resizer {
  display: block;
}

/* Disable transitions during resize for smoothness */
.main-wrapper.is-resizing .chat-container,
.main-wrapper.is-resizing .iframe-panel {
  transition: none;
}

/* Full-screen overlay used while dragging the resizer so the iframe
   doesn't swallow mouse events mid-drag. */
.drag-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9999;
  display: none;
  cursor: col-resize;
  background: transparent;
}

.drag-overlay.active {
  display: block;
}

/* ============================================================================
   3. Iframe side panel: header bar, content, error state
   ============================================================================ */

.iframe-header {
  padding: 1rem;
  background: var(--chat--color-light);
  border-bottom: 1px solid var(--chat--color-light-shade-100);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.iframe-url {
  flex: 1;
  font-size: 13px;
  color: var(--chat--color-medium);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.iframe-actions {
  display: flex;
  gap: 8px;
}

.iframe-open-new {
  background: none;
  border: none;
  color: var(--chat--color-medium);
  cursor: pointer;
  font-size: 16px;
  padding: 4px 8px;
  line-height: 1;
  transition: color 0.2s;
  text-decoration: none;
}

.iframe-open-new:hover {
  color: var(--chat--color-primary);
}

.iframe-close {
  background: none;
  border: none;
  color: var(--chat--color-medium);
  cursor: pointer;
  font-size: 20px;
  padding: 4px 8px;
  line-height: 1;
  transition: color 0.2s;
}

.iframe-close:hover {
  color: var(--chat--color-dark);
}

.iframe-content {
  flex: 1;
  border: none;
  background: white;
}

.iframe-error {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  text-align: center;
  background: #fff3cd;
  color: #856404;
}

.iframe-error h3 {
  margin-bottom: 1rem;
  font-size: 1.2rem;
}

.iframe-error p {
  margin-bottom: 1rem;
  line-height: 1.6;
}

.iframe-error a {
  display: inline-block;
  padding: 0.75rem 1.5rem;
  background: var(--chat--color-primary);
  color: white;
  text-decoration: none;
  border-radius: 24px;
  transition: transform 0.2s;
}

.iframe-error a:hover {
  transform: translateY(-2px);
}

/* ============================================================================
   4. Chat header (Puccini ONLINE banner)
   ============================================================================ */

.chat-header {
  padding: 1.25rem 1rem;
  background: linear-gradient(
    135deg,
    var(--chat--color-primary) 0%,
    var(--chat--color-secondary) 100%
  );
  color: var(--chat--color-white);
}

/* The visible heading is built from ::before / ::after pseudo-elements
   so it can be retitled without touching the markup. */
.chat-header h1 {
  font-size: 0;
  line-height: 0;
  position: relative;
}

.chat-header h1::before {
  content: "Puccini";
  font-size: 2.2rem;
  line-height: normal;
  font-weight: 800;
  font-family: "Raleway", sans-serif;
}

.chat-header h1::after {
  content: "ONLINE";
  font-size: 1.2rem;
  line-height: normal;
  font-family: "Raleway", sans-serif;
  font-weight: 100;
  font-style: italic;
  vertical-align: baseline;
  margin-left: 0.3rem;
}

.chat-header p {
  font-size: 0;
  line-height: 0;
  margin-top: 0.5rem;
}

.chat-header p::after {
  content: "ArchiBot - Assistente digitale alla ricerca";
  font-size: 1rem;
  line-height: 1.5;
  font-family: "Roboto", sans-serif;
  font-weight: 100;
}

/* ============================================================================
   5. Chat messages: list, message, avatar, content, markdown
   ============================================================================ */

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--chat--spacing);
  display: flex;
  flex-direction: column;
  gap: 16px;
  background: var(--chat--color-light);
}

.message {
  display: flex;
  gap: 12px;
  max-width: 80%;
  animation: slideIn 0.3s ease-out;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.message.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.message.assistant {
  align-self: flex-start;
}

/* Avatar */
.message-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  align-self: flex-start;
  font-size: 18px;
}

.user .message-avatar,
.assistant .message-avatar {
  background: linear-gradient(
    135deg,
    var(--chat--color-primary) 0%,
    var(--chat--color-secondary) 100%
  );
  overflow: hidden;
}

.user .message-avatar img,
.assistant .message-avatar img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  padding: 5px;
  box-sizing: border-box;
}

/* Message bubble */
.message-content {
  background: var(--chat--color-white);
  padding: var(--chat--message--padding);
  border-radius: var(--chat--message--border-radius);
  line-height: var(--chat--message-line-height);
  border: 1px solid var(--chat--color-light-shade-100);
}

.user .message-content {
  background: linear-gradient(
    135deg,
    var(--chat--color-primary) 0%,
    var(--chat--color-secondary) 100%
  );
  color: var(--chat--color-white);
  border: none;
}

.assistant .message-content {
  background: var(--chat--color-white);
  color: var(--chat--color-dark);
}

/* Links inside the bubble */
.message-content a {
  color: inherit;
  text-decoration: underline;
  font-weight: 500;
  cursor: pointer;
}

.user .message-content a {
  color: var(--chat--color-white);
}

.assistant .message-content a {
  color: var(--chat--color-primary);
}

.message-content a:hover {
  opacity: 0.8;
}

/* Markdown rendering inside the bubble */
.message-content h1,
.message-content h2,
.message-content h3 {
  margin: 0.8em 0 0.5em 0;
  font-weight: 600;
}

.message-content h1 { font-size: 1.5em; }
.message-content h2 { font-size: 1.3em; }
.message-content h3 { font-size: 1.1em; }

.message-content strong { font-weight: 600; }
.message-content em { font-style: italic; }

.message-content ul,
.message-content ol {
  margin: 0.5em 0;
  padding-left: 1.5em;
}

.message-content li { margin: 0.3em 0; }
.message-content p { margin: 0.5em 0; }

.message-content blockquote {
  border-left: 3px solid var(--chat--color-medium);
  padding-left: 1em;
  margin: 0.5em 0;
  color: var(--chat--color-medium);
}

/* ============================================================================
   6. Welcome message (greeting card with bullet list)
   ============================================================================ */

.welcome-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.welcome-list li {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 8px;
  line-height: 1.4;
}

.welcome-list li:last-child {
  margin-bottom: 0;
}

.welcome-icon {
  color: #27ae60;
  flex-shrink: 0;
  margin-top: 1px;
  font-weight: bold;
}

/* ============================================================================
   7. Typing + thinking indicators
   ============================================================================ */

/* The bouncing-dots indicator (default chat typing). */
.typing-indicator {
  display: flex;
  gap: 4px;
  padding: 8px;
}

.typing-indicator span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #999;
  animation: typing 1.4s ease-in-out infinite;
}

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

@keyframes typing {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-10px); }
}

/* Multi-step "thinking" trace shown while tools run. */
.thinking-message {
  background: #e3f2fd;
  border-left: 4px solid #2196f3;
  padding: 10px 16px;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 3px;
}

.status-step {
  font-size: 0.9em;
  line-height: 1.4;
  transition: opacity 0.3s ease;
}

.status-step.active {
  color: #1565c0;
  font-style: italic;
  font-weight: 500;
}

.status-step.done {
  color: #90a4ae;
  font-size: 0.82em;
  font-style: normal;
}

/* Italic side-trace shown next to the assistant avatar. */
.thinking-trace {
  align-self: flex-start;
  margin: 4px 8px 4px 56px;
  padding: 6px 12px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.05);
  color: #555;
  font-size: 0.85rem;
  font-style: italic;
  max-width: 80%;
  opacity: 0.9;
  transition: opacity 0.4s ease;
}

/* ============================================================================
   8. Chat input + send button
   ============================================================================ */

/* Overlay modale di verifica Turnstile: blocca tutta la pagina finché
   il widget non è "ready" (success) o "failed" (errore terminale).
   Stati su #captchaStatus: default = loading; `is-failed` = errore;
   `is-ready` nasconde l'overlay. */
.captcha-overlay {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(30, 31, 32, 0.55);
  backdrop-filter: blur(2px);
}

.captcha-overlay.is-ready {
  display: none;
}

.captcha-modal {
  background: var(--chat--color-white);
  border-radius: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
  padding: 24px 28px;
  min-width: 320px;
  max-width: 90vw;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  color: var(--chat--color-dark);
  font-size: 0.95rem;
  line-height: 1.4;
}

.captcha-modal-title {
  font-family: "Raleway", sans-serif;
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--chat--color-primary);
}

.captcha-overlay.is-failed .captcha-modal {
  border: 1px solid #f5576c;
}

.captcha-overlay.is-failed .captcha-modal-title {
  color: #b21f3a;
}

.captcha-status-row {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--chat--color-secondary);
}

.captcha-overlay.is-failed .captcha-status-row {
  color: #b21f3a;
}

.captcha-widget {
  display: flex;
  justify-content: center;
  min-height: 0;
}

.captcha-overlay.is-failed .captcha-widget {
  display: none;
}

.captcha-status-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(81, 86, 88, 0.25);
  border-top-color: var(--chat--color-secondary);
  border-radius: 50%;
  animation: captcha-spin 0.8s linear infinite;
  flex-shrink: 0;
}

.captcha-overlay.is-failed .captcha-status-spinner {
  display: none;
}

@keyframes captcha-spin {
  to { transform: rotate(360deg); }
}

.captcha-status-text {
  flex: 1;
  text-align: center;
}

.captcha-status-reload {
  padding: 8px 20px;
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  background: var(--chat--color-primary);
  color: #fff;
  border: none;
  border-radius: 24px;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
}

.captcha-status-reload:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(54, 56, 57, 0.35);
}

.chat-input-container {
  padding: var(--chat--spacing);
  background: var(--chat--color-white);
  border-top: 1px solid var(--chat--color-light-shade-100);
  display: flex;
  gap: 12px;
}

.chat-input {
  flex: 1;
  padding: 0.75rem 1rem;
  border: none;
  border-radius: 0;
  font-size: 14px;
  outline: none;
  background: var(--chat--color-light);
  transition: background-color 0.2s;
}

.chat-input:focus {
  background: var(--chat--color-light-shade-50);
}

.send-button {
  padding: 12px 24px;
  background: linear-gradient(
    135deg,
    var(--chat--color-primary) 0%,
    var(--chat--color-secondary) 100%
  );
  color: var(--chat--color-white);
  border: none;
  border-radius: 24px;
  cursor: pointer;
  font-weight: 600;
  transition:
    transform 0.2s,
    box-shadow 0.2s;
}

.send-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(54, 56, 57, 0.4);
}

.send-button:active {
  transform: translateY(0);
}

.send-button:disabled {
  background: var(--chat--color-disabled);
  cursor: not-allowed;
  transform: none;
}

/* ============================================================================
   9. Quota / error banners
   ============================================================================ */

.quota-reached {
  margin: 16px;
  padding: 16px 20px;
  border: 1px solid #f5576c;
  background: #fff5f6;
  color: #b21f3a;
  border-radius: 8px;
  font-size: 0.95rem;
  line-height: 1.5;
  text-align: center;
}

/* ============================================================================
   10. OPAC (post-response panels)
   ----------------------------------------------------------------------------
   Three "wrap" boxes appear under an assistant turn:
     - .opac-filters                → authorities CONSULTED (green theme)
     - .opac-filters.opac-filters--mentioned → authorities MENTIONED in text
       (blue theme)
     - .catalog-results             → documents from search_catalog
       (amber theme, "Apri elenco" trigger)

   Each box contains a label, optional inline chips (.opac-chip), and an
   overflow trigger (button) that opens a dropdown panel rendered via JS
   into <body> with position: fixed.
   ============================================================================ */

/* ---- Wrap boxes ---------------------------------------------------------- */

.opac-filters {
  background: #f1f8e9;
  border-left: 4px solid #8bc34a;
  padding: 12px 16px;
  border-radius: 8px;
  margin-top: 8px;
}

.opac-filters strong {
  color: #558b2f;
  display: block;
  margin-bottom: 8px;
}

.opac-filters--mentioned {
  background: #f5f7ff;
  border-left-color: #3949ab;
  margin-top: 6px;
}

.opac-filters--mentioned strong {
  color: #3949ab;
}

.catalog-results {
  background: #fff8e1;
  border-left: 4px solid #ffb300;
  padding: 12px 16px;
  border-radius: 8px;
  margin-top: 8px;
}

.catalog-results strong {
  color: #b26a00;
  display: block;
  margin-bottom: 8px;
}

/* ---- Chips (anchor pills inside the wrap boxes) ------------------------- */

/* Default = consulted authorities: white background, green border;
   hover fills green and flips the text white.
   The `a.` prefix raises specificity to (0,1,1) — the same as
   `.message-content a` — and source order then wins, so the chip's
   `text-decoration: none` and `opacity: 1` override the link styling
   that the chips inherit from being inside the assistant bubble. */
a.opac-chip {
  display: inline-block;
  margin: 4px 6px 4px 0;
  padding: 4px 12px;
  border-radius: 14px;
  background: #fff;
  border: 1px solid #8bc34a;
  color: #558b2f;
  text-decoration: none;
  font-size: 0.85rem;
  font-weight: 500;
  transition: background 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
}

a.opac-chip:hover {
  background: #8bc34a;
  color: #fff;
  opacity: 1;
  text-decoration: none;
  box-shadow: 0 2px 6px rgba(139, 195, 74, 0.35);
}

/* Mentioned-authority variant = compound class so the selector beats
   plain `a.opac-chip` on specificity without anchoring on the wrap.
   Blue border, hover fills blue with white text. */
a.opac-chip.opac-chip--mentioned {
  border-color: #3949ab;
  color: #3949ab;
}

a.opac-chip.opac-chip--mentioned:hover {
  background: #3949ab;
  color: #fff;
  box-shadow: 0 2px 6px rgba(57, 73, 171, 0.35);
}

/* ---- Overflow trigger (button next to the label) ----------------------- */

.opac-overflow-trigger-wrap {
  position: relative;
  display: inline-block;
}

/* Default look (neutral). Used by the authority boxes when there are more
   chips than fit inline. No transform on hover: a translateY would move
   the box edge under the cursor, dropping :hover and producing a
   flicker loop. A box-shadow gives the lift effect without that. */
.opac-overflow-trigger {
  display: inline-block;
  margin: 4px 6px 4px 0;
  padding: 4px 12px;
  border-radius: 14px;
  border: 1px solid rgba(0, 0, 0, 0.15);
  background: rgba(0, 0, 0, 0.06);
  color: #333;
  font: inherit;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s ease, box-shadow 0.15s ease;
}

.opac-overflow-trigger:hover,
.opac-overflow-trigger[aria-expanded="true"] {
  background: rgba(0, 0, 0, 0.12);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.12);
}

/* Amber override for the catalog-results trigger ("Apri elenco"):
   white background with yellow border, hover fills yellow. */
.catalog-results .opac-overflow-trigger {
  background: #fff;
  border-color: #ffb300;
  color: #6a4500;
}

.catalog-results .opac-overflow-trigger:hover,
.catalog-results .opac-overflow-trigger[aria-expanded="true"] {
  background: #ffb300;
  color: #fff;
}

/* ---- Overflow dropdown panel (rendered in <body>) ---------------------- */

/* `position: fixed` so the panel escapes the chat-container's
   `overflow: hidden`. Coordinates are set inline by JS from the trigger's
   getBoundingClientRect(); the panel opens rightward from the trigger. */
.opac-overflow-panel {
  position: fixed;
  min-width: 280px;
  max-width: 480px;
  max-height: 320px;
  overflow-y: auto;
  background: #fff;
  border: 1px solid rgba(0, 0, 0, 0.12);
  border-radius: 8px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.15);
  padding: 6px 0;
  z-index: 1000;
}

/* Sticky header inside the panel: tab toggle + live search input. */
.opac-overflow-header {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px 10px;
  background: #fff;
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
}

.opac-overflow-tabs {
  display: flex;
  gap: 4px;
}

.opac-overflow-tab {
  flex: 1;
  padding: 4px 10px;
  font: inherit;
  font-size: 0.78rem;
  font-weight: 500;
  color: #555;
  background: #f0f0f0;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 12px;
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease, border-color 0.12s ease;
}

.opac-overflow-tab:hover {
  background: #e7e7e7;
}

.opac-overflow-tab.is-active {
  background: #ffe082;
  border-color: #ffb300;
  color: #6a4500;
}

.opac-overflow-search {
  width: 100%;
  box-sizing: border-box;
  padding: 6px 10px;
  font: inherit;
  font-size: 0.85rem;
  color: #222;
  background: #f6f6f6;
  border: 1px solid rgba(0, 0, 0, 0.12);
  border-radius: 6px;
  outline: none;
}

.opac-overflow-search:focus {
  border-color: rgba(255, 179, 0, 0.6);
  background: #fff;
}

/* Rows in the dropdown body. */
.opac-overflow-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 8px 14px;
  text-decoration: none;
  color: #222;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.opac-overflow-item:last-child {
  border-bottom: none;
}

.opac-overflow-item:hover {
  background: rgba(0, 0, 0, 0.05);
}

.opac-overflow-item-title {
  font-size: 0.9rem;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.opac-overflow-item-url {
  font-size: 0.75rem;
  color: #666;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ============================================================================
   11. Responsive overrides
   ============================================================================ */

@media (max-width: 600px) {
  .opac-overflow-panel {
    min-width: 0;
    max-width: calc(100vw - 32px);
  }
}
