/* ============================================================
   AI Assistant — floating panel, chat, preview modal
   Uses design tokens from css/themes.css:
     --bg-primary, --bg-secondary, --bg-tertiary, --bg-hover
     --text-primary, --text-secondary, --text-muted
     --border-color, --border-hover
     --accent-color, --accent-hover, --accent-color-alpha
     --success-color, --warning-color, --danger-color
     --font-sans, --font-mono
     --shadow-color
   ============================================================ */

/* ── Floating panel container ──────────────────────────────── */

.ai-panel {
  position: fixed;
  z-index: 900;
  width: 360px;
  height: 500px;
  bottom: 72px;
  right: 16px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  box-shadow: 0 8px 40px var(--shadow-color);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  font-family: var(--font-sans);
  transition: height 0.15s ease;
  resize: both;
  min-width: 280px;
  min-height: 200px;
  max-width: 600px;
  max-height: 80vh;
}

.ai-panel.hidden { display: none; }
.ai-panel.minimized { height: 42px !important; overflow: hidden; resize: none; }

/* ── Title bar (drag handle) ────────────────────────────────── */

.ai-panel-titlebar {
  display: flex;
  align-items: center;
  height: 42px;
  padding: 0 8px 0 12px;
  background: var(--bg-tertiary);
  border-bottom: 1px solid var(--border-color);
  cursor: move;
  flex-shrink: 0;
  gap: 6px;
  user-select: none;
}

.ai-panel-title {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--text-primary);
  flex-shrink: 0;
}

.ai-panel-context {
  font-size: 11px;
  color: var(--text-muted);
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ai-panel-actions {
  display: flex;
  gap: 2px;
  flex-shrink: 0;
}

.ai-panel-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  padding: 0 6px;
  height: 26px;
  border-radius: 4px;
  font-size: 14px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.ai-panel-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

/* SVG icons inside titlebar buttons */
.ai-panel-btn svg {
  display: block;
  pointer-events: none;
}

/* ── Chat area ──────────────────────────────────────────────── */

.ai-chat-area {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  scroll-behavior: smooth;
}

/* ── Message bubbles ────────────────────────────────────────── */

.ai-message {
  max-width: 88%;
  padding: 8px 12px;
  border-radius: 12px;
  font-size: 13px;
  line-height: 1.55;
  word-break: break-word;
}

.ai-message.ai-role {
  align-self: flex-start;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-bottom-left-radius: 3px;
  color: var(--text-primary);
}

.ai-message.user-role {
  align-self: flex-end;
  background: var(--accent-color);
  color: #fff;
  border-bottom-right-radius: 3px;
}

.ai-message.tool-status {
  align-self: flex-start;
  font-size: 11px;
  color: var(--text-muted);
  background: none;
  border: none;
  padding: 0 2px;
  font-style: italic;
  max-width: 100%;
}

.ai-message.error-role {
  align-self: flex-start;
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid rgba(239, 68, 68, 0.3);
  color: var(--danger-color);
  border-bottom-left-radius: 3px;
}

/* ── Markdown rendering (assistant bubbles only) ────────────── */

.ai-message.ai-role p {
  margin: 0 0 6px;
}
.ai-message.ai-role p:last-child { margin-bottom: 0; }

.ai-message.ai-role h1,
.ai-message.ai-role h2,
.ai-message.ai-role h3,
.ai-message.ai-role h4 {
  margin: 10px 0 4px;
  font-weight: 600;
  line-height: 1.3;
  color: var(--text-primary);
}
.ai-message.ai-role h1 { font-size: 15px; }
.ai-message.ai-role h2 { font-size: 14px; }
.ai-message.ai-role h3,
.ai-message.ai-role h4 { font-size: 13px; }
.ai-message.ai-role h1:first-child,
.ai-message.ai-role h2:first-child,
.ai-message.ai-role h3:first-child { margin-top: 0; }

.ai-message.ai-role code {
  font-family: var(--font-mono, monospace);
  font-size: 11.5px;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 3px;
  padding: 1px 4px;
}

.ai-message.ai-role pre {
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 10px 12px;
  overflow-x: auto;
  margin: 6px 0;
}
.ai-message.ai-role pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 11.5px;
  line-height: 1.55;
}

.ai-message.ai-role ul,
.ai-message.ai-role ol {
  margin: 4px 0 6px;
  padding-left: 20px;
}
.ai-message.ai-role li {
  margin: 2px 0;
}

.ai-message.ai-role blockquote {
  border-left: 3px solid var(--accent-color);
  margin: 6px 0;
  padding: 4px 10px;
  color: var(--text-muted);
  font-style: italic;
}

.ai-message.ai-role table {
  border-collapse: collapse;
  width: 100%;
  margin: 6px 0;
  font-size: 12px;
}
.ai-message.ai-role th,
.ai-message.ai-role td {
  border: 1px solid var(--border-color);
  padding: 4px 8px;
  text-align: left;
}
.ai-message.ai-role th {
  background: var(--bg-primary);
  font-weight: 600;
}

.ai-message.ai-role hr {
  border: none;
  border-top: 1px solid var(--border-color);
  margin: 8px 0;
}

.ai-message.ai-role a {
  color: var(--accent-color);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.ai-message.ai-role strong { font-weight: 600; }
.ai-message.ai-role em     { font-style: italic; }
.ai-message.ai-role del    { text-decoration: line-through; color: var(--text-muted); }

/* Streaming cursor */
.ai-streaming::after {
  content: '\25AE'; /* ▮ block cursor */
  animation: ai-cursor-blink 0.75s step-end infinite;
  margin-left: 1px;
}

@keyframes ai-cursor-blink {
  50% { opacity: 0; }
}

/* ── Attachment chips ───────────────────────────────────────── */

.ai-attachment-chips {
  /* Sits between chat area and input row — visually tied to the input */
  padding: 4px 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  flex-shrink: 0;
  border-top: 1px solid var(--border-color);
}

.ai-attachment-chips:empty { display: none; }

.ai-chip {
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 2px 6px 2px 10px;
  font-size: 11px;
  color: var(--text-secondary);
  font-family: var(--font-mono);
}

.ai-chip-remove {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  padding: 0 2px;
  font-size: 14px;
  line-height: 1;
  border-radius: 3px;
}

.ai-chip-remove:hover { color: var(--danger-color); }

/* ── Input row ──────────────────────────────────────────────── */

.ai-input-row {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 8px;
  border-top: 1px solid var(--border-color);
  flex-shrink: 0;
}

/* Attach button — paperclip icon via inline SVG mask */
.ai-attach-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  width: 30px;
  height: 30px;
  border-radius: 6px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.ai-attach-btn::before {
  content: '';
  display: block;
  width: 16px;
  height: 16px;
  background: currentColor;
  /* paperclip outline SVG */
  mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M21.44 11.05l-9.19 9.19a6 6 0 01-8.49-8.49l9.19-9.19a4 4 0 015.66 5.66l-9.2 9.19a2 2 0 01-2.83-2.83l8.49-8.48'/%3E%3C/svg%3E");
  -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M21.44 11.05l-9.19 9.19a6 6 0 01-8.49-8.49l9.19-9.19a4 4 0 015.66 5.66l-9.2 9.19a2 2 0 01-2.83-2.83l8.49-8.48'/%3E%3C/svg%3E");
  mask-size: contain;
  -webkit-mask-size: contain;
  mask-repeat: no-repeat;
  -webkit-mask-repeat: no-repeat;
}

.ai-attach-btn:hover { background: var(--bg-hover); color: var(--text-primary); }

/* Chat textarea */
.ai-input {
  flex: 1;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 7px 10px;
  font-size: 13px;
  font-family: var(--font-sans);
  color: var(--text-primary);
  resize: none;
  min-height: 34px;
  max-height: 120px;
  overflow-y: auto;
  line-height: 1.4;
  field-sizing: content; /* auto-grow on modern browsers */
}

.ai-input::placeholder { color: var(--text-muted); }
.ai-input:focus { outline: none; border-color: var(--accent-color); }

/* Send button — arrow-up icon via SVG mask */
.ai-send-btn {
  background: var(--accent-color);
  border: none;
  cursor: pointer;
  color: #fff;
  width: 30px;
  height: 30px;
  border-radius: 6px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.1s;
}

.ai-send-btn:hover { background: var(--accent-hover); }
.ai-send-btn:disabled { background: var(--bg-hover); cursor: not-allowed; }

.ai-send-btn::before {
  content: '';
  display: block;
  width: 14px;
  height: 14px;
  background: currentColor;
  mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cline x1='12' y1='19' x2='12' y2='5'/%3E%3Cpolyline points='5 12 12 5 19 12'/%3E%3C/svg%3E");
  -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cline x1='12' y1='19' x2='12' y2='5'/%3E%3Cpolyline points='5 12 12 5 19 12'/%3E%3C/svg%3E");
  mask-size: contain;
  -webkit-mask-size: contain;
  mask-repeat: no-repeat;
  -webkit-mask-repeat: no-repeat;
}

/* ── AI Preview Modal — diff view ───────────────────────────── */

#ai-preview-modal .modal-body { max-height: 55vh; overflow-y: auto; }

.ai-preview-section { margin-bottom: 16px; }

.ai-preview-section-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.ai-preview-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 7px 10px;
  border-radius: 6px;
  font-size: 13px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  margin-bottom: 4px;
}

.ai-preview-add::before {
  content: '+';
  color: var(--success-color);
  font-weight: 700;
  font-family: var(--font-mono);
  flex-shrink: 0;
}

.ai-preview-item-name { font-weight: 600; color: var(--text-primary); }

.ai-preview-item-detail {
  color: var(--text-muted);
  font-size: 12px;
  font-family: var(--font-mono);
}

.ai-preview-summary {
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 12px;
  padding: 8px 10px;
  background: var(--accent-color-alpha);
  border-radius: 6px;
  border: 1px solid var(--accent-color-alpha);
}

/* ── Multi-service settings ─────────────────────────────────── */

.ai-services-list {
  border: 1px solid var(--border-color);
  border-radius: 6px;
  overflow: hidden;
}

.ai-svc-empty {
  padding: 12px;
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
  margin: 0;
}

.ai-service-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 8px;
  border-bottom: 1px solid var(--border-color);
  background: var(--bg-tertiary);
  transition: background 0.1s;
}
.ai-service-row:last-child { border-bottom: none; }
.ai-service-row:hover      { background: var(--bg-hover); }

.ai-service-row-label {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  min-width: 0;
  cursor: pointer;
}

.ai-service-row-label input[type="radio"] { flex-shrink: 0; }

.ai-service-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.ai-service-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ai-service-url {
  font-size: 11px;
  color: var(--text-muted);
  font-family: var(--font-mono);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ai-service-row-actions {
  display: flex;
  gap: 2px;
  flex-shrink: 0;
}

/* Model chips inside the service editor */
.ai-svc-models-list {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  min-height: 26px;
  padding: 2px 0;
}

.ai-svc-model-chip {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 2px 4px 2px 10px;
  font-size: 11px;
  font-family: var(--font-mono);
  color: var(--text-secondary);
  white-space: nowrap;
}

.ai-svc-model-chip button {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  padding: 0 3px;
  font-size: 14px;
  line-height: 1;
  border-radius: 3px;
}
.ai-svc-model-chip button:hover { color: var(--danger-color); }

/* ── Service editor — proxy row ─────────────────────────────── */

.ai-svc-proxy-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 8px 10px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  cursor: pointer;
}

.ai-svc-proxy-row input[type="checkbox"] {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
  accent-color: var(--accent-color);
  cursor: pointer;
}

.ai-svc-proxy-label {
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 12px;
  color: var(--text-primary);
  cursor: pointer;
  user-select: none;
  line-height: 1.4;
}

.ai-svc-proxy-note {
  font-size: 11px;
  color: var(--accent-color);
  font-style: italic;
}

/* ── Settings section label ─────────────────────────────────── */

.settings-section-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  padding-top: 8px;
  border-top: 1px solid var(--border-color);
  display: block;
  margin-top: 4px;
}
