﻿/* ============================================================
   AI ASSISTANT
   ============================================================ */
.ai-chat-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-width: 100%;
}

.ai-welcome {
  text-align: center;
  padding: 1.5rem 0.5rem;
  animation: slideInUp 0.3s ease;
}
.ai-welcome-icon {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--accent-gradient);
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 0.75rem;
  font-size: 1.5rem;
  color: #fff;
  box-shadow: var(--shadow-glow);
}
.ai-welcome h6 {
  font-weight: 600;
  font-family: var(--font-heading);
  margin-bottom: 0.25rem;
}
.ai-welcome p {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

.quick-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  justify-content: center;
}
.quick-actions .btn { transition: all var(--transition-fast); }
.quick-actions .btn:hover { transform: translateY(-2px); box-shadow: 0 4px 12px rgba(0,0,0,0.3); }

/* Chat bubbles */
.ai-message {
  max-width: 90%;
  padding: 0.6rem 0.75rem;
  border-radius: var(--radius-md);
  font-size: 0.82rem;
  line-height: 1.5;
  animation: messageIn 0.25s ease;
  transition: all var(--transition-fast);
  overflow-wrap: break-word;
  word-break: break-word;
  position: relative;
  /* In the flex-column scroll list, a message must never shrink below its
     content. With `flex-shrink: 1` (the default) plus `overflow: hidden`,
     the automatic minimum size becomes 0, so a long transcript collapses
     the card to ~0px and hides its content and Apply button. */
  flex-shrink: 0;
}
.ai-message img,
.ai-message table,
.ai-message pre {
  max-width: 100%;
}
.ai-message.user {
  align-self: flex-end;
  background: linear-gradient(135deg, var(--purple-600), var(--purple-700));
  color: #fff;
  border-bottom-right-radius: 4px;
  box-shadow: 0 4px 12px rgba(119, 44, 232, 0.25);
}
.ai-message.user:hover { box-shadow: 0 6px 18px rgba(119, 44, 232, 0.4); }
.ai-message.assistant {
  align-self: flex-start;
  background: var(--bg-surface);
  border: 1px solid var(--surface-border);
  border-bottom-left-radius: 4px;
  color: var(--text-primary);
}
.ai-message.assistant:hover { border-color: var(--border-default); box-shadow: 0 4px 12px rgba(0,0,0,0.2); }
.ai-message-content {
  max-width: 170px;
  word-break: break-word;
}
.ai-message pre {
  background: var(--bg-deep);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-sm);
  padding: 0.5rem;
  font-size: 0.75rem;
  font-family: var(--font-mono);
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0.35rem 0;
  overflow: auto;
  max-width: 100%;
}
.ai-message.system {
  align-self: center;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.72rem;
  font-style: italic;
}
/* Retry / Regenerate button on assistant messages */
.ai-message-actions {
  display: flex;
  gap: 0.3rem;
  flex-wrap: wrap;
  margin-top: 0.3rem;
  align-items: center;
}

/* Applied badge shown inside a message/section after its change is applied */
.ai-applied-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.62rem;
  color: var(--success);
  background: rgba(34, 197, 94, 0.12);
  border: 1px solid rgba(34, 197, 94, 0.35);
  border-radius: 999px;
  padding: 0.1rem 0.5rem;
  font-weight: 600;
}
.ai-applied-badge .bi { font-size: 0.7rem; }

/* Prev/Next nav in the AI diff modal — keep it clearly readable in dark/light */
#applyNavGroup { margin-inline-start: auto !important; }
#btnApplyPrev, #btnApplyNext {
  color: var(--text-accent);
  border-color: var(--surface-border);
  background: rgba(255, 255, 255, 0.05);
}
#btnApplyPrev:not(:disabled):hover, #btnApplyNext:not(:disabled):hover {
  color: #fff;
  border-color: var(--border-accent);
  background: rgba(145, 71, 255, 0.16);
}
#btnApplyPrev:disabled, #btnApplyNext:disabled { opacity: 0.35; }
#applyNavCounter {
  color: var(--text-primary) !important;
  font-weight: 600;
  font-size: 0.72rem;
  letter-spacing: 0.02em;
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid var(--surface-border);
  border-radius: 999px;
  padding: 0.18rem 0.6rem;
  white-space: nowrap;
}

.ai-chat-messages [data-applied="1"] button.disabled { opacity: 0.5; cursor: not-allowed; }
.ai-message-retry,
.ai-message-reapply {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.65rem;
  color: var(--text-muted);
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
  transition: all var(--transition-fast);
  opacity: 0.5;
}
.ai-message-retry:hover,
.ai-message-reapply:hover {
  color: var(--text-accent);
  background: var(--bg-surface-hover);
  opacity: 1;
}
.ai-message-reapply {
  color: var(--success);
  background: var(--bg-surface);
  border: 1px solid var(--success);
  opacity: 1;
  font-weight: 500;
}
.ai-message-reapply:hover {
  color: #fff;
  background: var(--success);
}
.ai-message.assistant:hover .ai-message-retry {
  opacity: 0.8;
}

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

/* Typing indicator */
.typing-indicator {
  display: flex;
  gap: 4px;
  padding: 0.6rem 0.75rem;
  align-self: flex-start;
}
.typing-indicator span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--purple-500);
  animation: typingBounce 1.2s infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-8px); opacity: 1; }
}

/* ============================================================
   CHAT HISTORY PANEL
   ============================================================ */
.ai-history-panel {
  display: none;
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
  flex-direction: column;
  gap: 0.25rem;
}
.ai-history-panel.open {
  display: flex;
}

.ai-history-title {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 0.5rem 0.4rem 0.25rem;
  border-bottom: 1px solid var(--surface-border);
  margin-bottom: 0.25rem;
}

.ai-history-item {
  display: flex;
  flex-direction: column;
  padding: 0.45rem 0.55rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition-fast);
  border: 1px solid transparent;
  animation: fadeIn 0.2s ease;
}
.ai-history-item:hover {
  background: var(--bg-surface-hover);
  border-color: var(--border-subtle);
}
.ai-history-item.active {
  background: var(--bg-surface);
  border-color: var(--border-accent);
}

.ai-history-item-preview {
  font-size: 0.78rem;
  color: var(--text-primary);
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  word-break: break-word;
}

.ai-history-item-meta {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-top: 0.2rem;
}
.ai-history-item-time {
  font-size: 0.62rem;
  color: var(--text-muted);
}
.ai-history-item-count {
  font-size: 0.6rem;
  color: var(--text-muted);
  background: var(--bg-tertiary);
  padding: 0.05em 0.4em;
  border-radius: 100px;
}
.ai-history-empty {
  text-align: center;
  padding: 2rem 0.5rem;
  color: var(--text-muted);
  font-size: 0.78rem;
}

/* ============================================================
   AI INPUT AREA
   ============================================================ */
.ai-input-area {
  border-top: 1px solid var(--surface-border);
  padding: 0.6rem 0.75rem;
  background: rgba(255, 255, 255, 0.02);
  position: relative;
}
.ai-input-area::before {
  content: '';
  position: absolute;
  top: -1px;
  inset-inline: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--purple-500), var(--magenta-500), transparent);
  opacity: 0.5;
}
.ai-input-row {
  display: flex;
  gap: 0.35rem;
  align-items: flex-end;
}
.ai-input {
  flex: 1;
  background: var(--bg-surface) !important;
  border: 1px solid var(--border-subtle) !important;
  color: var(--text-primary) !important;
  font-size: 0.82rem !important;
  border-radius: var(--radius-md) !important;
  resize: vertical;
  min-height: 60px;
  max-height: 200px;
}
.ai-input:focus {
  border-color: var(--border-accent) !important;
  box-shadow: 0 0 0 0.15rem var(--purple-glow) !important;
}
.ai-input::placeholder { color: var(--text-placeholder); opacity: 1; }

.ai-send-btn {
  width: 38px;
  height: 38px;
  border-radius: 50% !important;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  padding: 0 !important;
}

.ai-input-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 0.4rem;
  gap: 0.5rem;
}
.ai-model-select, .ai-target-select {
  /* Only background-color (not the `background` shorthand): the shorthand
     would reset background-repeat/position/size to their defaults and make
     Bootstrap's dropdown chevron tile into a row of ∨∨∨∨ characters. */
  background-color: var(--bg-surface) !important;
  border: 1px solid var(--surface-border) !important;
  color: var(--text-secondary) !important;
  font-size: 0.72rem !important;
  border-radius: var(--radius-sm) !important;
}
.ai-target-group {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}
.ai-target-label {
  font-size: 0.72rem;
  color: var(--text-muted);
  white-space: nowrap;
}

/* Context usage bar */
.context-bar {
  position: relative;
  height: 16px;
  border-radius: 100px;
  background: var(--bg-surface);
  border: 1px solid var(--surface-border);
  overflow: hidden;
  margin-bottom: 0.4rem;
}
.context-bar-fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: 0%;
  background: linear-gradient(90deg, var(--purple-600), var(--magenta-600));
  transition: width var(--transition-normal), background var(--transition-normal);
}
.context-bar-fill.warn { background: linear-gradient(90deg, #d97706, var(--warning)); }
.context-bar-fill.danger { background: linear-gradient(90deg, #b91c1c, var(--danger)); }
.context-bar-fill.warn ~ .context-bar-label,
.context-bar-fill.danger ~ .context-bar-label {
  color: #fff;
  text-shadow: 0 1px 3px rgba(0,0,0,0.5);
}
.context-bar-label {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.62rem;
  font-family: var(--font-mono);
  color: var(--text-secondary);
  letter-spacing: 0.02em;
}

.ai-send-group {
  display: flex;
  align-items: flex-end;
  gap: 0.25rem;
  flex-shrink: 0;
}

/* ============================================================
   FIELD CHIP SELECTOR
   ============================================================ */
.ai-field-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin-top: 0.4rem;
}
.ai-field-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.18rem 0.5rem;
  font-size: 0.65rem;
  font-weight: 500;
  border-radius: 100px;
  border: 1px solid var(--surface-border);
  background: var(--bg-surface);
  color: var(--text-muted);
  cursor: pointer;
  transition: all var(--transition-fast);
  user-select: none;
  white-space: nowrap;
}
.ai-field-chip:hover {
  border-color: var(--purple-500);
  color: var(--text-accent);
  background: rgba(145, 71, 255, 0.08);
}
.ai-field-chip.active {
  background: var(--purple-600);
  border-color: var(--purple-400);
  color: #fff;
  box-shadow: 0 0 8px var(--purple-glow);
}
.ai-field-chip.all-fields {
  border-color: var(--purple-600);
  font-weight: 600;
}
.ai-field-chip.all-fields.active {
  background: var(--accent-gradient);
}

/* Greeting count control */
.ai-greeting-count {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  margin-top: 0.35rem;
  animation: fadeIn 0.2s ease;
}
.ai-greeting-count-label {
  font-size: 0.68rem;
  color: var(--text-accent);
  font-weight: 500;
  white-space: nowrap;
}
.ai-greeting-count-input {
  width: 52px;
  background: var(--bg-surface) !important;
  border: 1px solid var(--border-accent) !important;
  color: var(--text-primary) !important;
  font-size: 0.72rem !important;
  border-radius: var(--radius-sm) !important;
  padding: 0.15rem 0.35rem !important;
  text-align: center;
}
.ai-greeting-count-input:focus {
  box-shadow: 0 0 0 0.15rem var(--purple-glow) !important;
}

/* ============================================================
   GROUPED MULTI-FIELD RESPONSE CARD
   ============================================================ */
.ai-message.multi-field {
  max-width: 95%;
  padding: 0;
  /* NOT overflow:hidden — see the flex-shrink note on .ai-message. An
     overflow:hidden card inside the flex-column scroll container computes
     its height from the automatic minimum size (0) instead of its content,
     collapsing the response and its Review & Apply button to ~0px. */
  overflow: visible;
}
.multi-field-header {
  padding: 0.45rem 0.7rem;
  font-size: 0.68rem;
  font-weight: 600;
  color: var(--text-accent);
  background: rgba(145, 71, 255, 0.08);
  border-bottom: 1px solid var(--surface-border);
  display: flex;
  align-items: center;
  gap: 0.4rem;
  /* Round the top corners since the parent no longer clips with overflow:hidden */
  border-radius: var(--radius-md) var(--radius-md) 0 0;
}
.multi-field-header .bi { font-size: 0.75rem; }

.multi-field-section {
  padding: 0.5rem 0.7rem;
  border-bottom: 1px solid var(--surface-border);
  transition: background var(--transition-fast);
  overflow-x: hidden;
  max-width: 100%;
}
.multi-field-section:last-child { border-bottom: none; border-radius: 0 0 var(--radius-md) var(--radius-md); }
.multi-field-section.streaming { background: rgba(145, 71, 255, 0.03); }
.multi-field-section.done { background: rgba(34, 197, 94, 0.04); }
.multi-field-section.error { background: rgba(239, 68, 68, 0.05); }

.multi-field-label {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 0.3rem;
  display: flex;
  align-items: center;
  gap: 0.3rem;
}
.multi-field-section.done .multi-field-label { color: var(--success); }
.multi-field-section.error .multi-field-label { color: var(--danger); }

.multi-field-content {
  font-size: 0.78rem;
  line-height: 1.5;
  color: var(--text-primary);
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 200px;
  overflow-y: auto;
  overflow-x: hidden;
  max-width: 100%;
}

/* Collapsed state — truncated preview */
.multi-field-content.collapsed {
  max-height: 5.4em;
  overflow-y: hidden;
  cursor: pointer;
  position: relative;
}
.multi-field-content.collapsed:hover {
  background: rgba(145, 71, 255, 0.03);
  border-radius: var(--radius-sm);
}
.multi-field-content.collapsed::after {
  content: 'Click to expand';
  position: absolute;
  bottom: 2px;
  inset-inline-end: 4px;
  font-size: 0.6rem;
  color: var(--text-accent);
  background: var(--bg-deep);
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
  opacity: 0;
  transition: opacity var(--transition-fast);
  pointer-events: none;
  border: 1px solid var(--surface-border);
  line-height: 1.4;
}
.multi-field-content.collapsed:hover::after {
  opacity: 0.85;
}

/* Expand / View button */
.multi-field-expand-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.65rem;
  color: var(--text-accent);
  background: transparent;
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  padding: 0.15rem 0.5rem;
  transition: all var(--transition-fast);
  margin-top: 0.4rem;
}
.multi-field-expand-btn:hover {
  background: rgba(145, 71, 255, 0.1);
  border-color: var(--border-accent);
}
.multi-field-expand-btn .bi {
  font-size: 0.7rem;
}
/* Result modal body */
.ai-result-body {
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 0.85rem;
  line-height: 1.6;
  max-height: 70vh;
  overflow-y: auto;
  font-family: var(--font-mono);
  background: var(--bg-deep);
  border-radius: var(--radius-sm);
  padding: 1rem;
}

.multi-field-content pre {
  background: var(--bg-deep);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-sm);
  padding: 0.4rem;
  font-size: 0.72rem;
  font-family: var(--font-mono);
  margin: 0.25rem 0;
  white-space: pre-wrap;
}

.multi-field-actions {
  display: flex;
  gap: 0.35rem;
  margin-top: 0.4rem;
  flex-wrap: wrap;
}
.multi-field-actions .btn {
  font-size: 0.65rem;
  padding: 0.15rem 0.5rem;
}

.multi-field-status {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.62rem;
  color: var(--text-muted);
  margin-inline-start: auto;
}
.multi-field-status .spinner-border {
  width: 10px;
  height: 10px;
  border-width: 1.5px;
}

/* Ready-bar: one-click apply-all / review at the bottom of a finished run */
.multi-field-footer {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.7rem;
  border-top: 1px solid var(--surface-border);
  border-radius: 0 0 var(--radius-md) var(--radius-md);
  background: rgba(145, 71, 255, 0.06);
  flex-wrap: wrap;
}
.multi-field-footer-count {
  font-size: 0.68rem;
  font-weight: 600;
  color: var(--text-accent);
  margin-inline-end: auto;
}
.multi-field-footer .btn {
  font-size: 0.68rem;
  padding: 0.2rem 0.6rem;
}
.multi-field-footer .btn.disabled {
  opacity: 0.55;
  pointer-events: none;
}
