/**
 * CMI Chat Widget — shared styles
 * ================================
 * Right-side fixed sidebar + floating toggle. Dark-mode aware (inherits
 * --color-* vars from host app if defined, with fallbacks).
 */

:root {
  --cmi-chat-bg:           var(--color-surface, #ffffff);
  --cmi-chat-border:       var(--color-border, #dee2e6);
  --cmi-chat-text:         var(--color-text, #212529);
  --cmi-chat-muted:        var(--color-text-muted, #6c757d);
  --cmi-chat-primary:      var(--color-primary, #0d6efd);
  --cmi-chat-primary-dk:   var(--color-primary-dk, #0b5ed7);
  --cmi-chat-danger:       var(--color-danger, #dc3545);
  --cmi-chat-success:      var(--color-success, #198754);
  --cmi-chat-bubble-user:  var(--color-primary, #0d6efd);
  --cmi-chat-bubble-ai:    var(--color-bg, #f1f3f5);
  --cmi-chat-shadow:       0 4px 24px rgba(0, 0, 0, 0.15);
  --cmi-chat-radius:       8px;
  --cmi-chat-width:        380px;
}

/* ── Toggle button (collapsed state) ──────────────────────────────────────── */
.cmi-chat-toggle {
  position: fixed;
  right: 18px;
  bottom: 18px;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  border: none;
  background: var(--cmi-chat-primary);
  color: #fff;
  font-size: 22px;
  cursor: pointer;
  box-shadow: var(--cmi-chat-shadow);
  z-index: 9999;
  transition: transform 0.15s ease, background 0.15s ease;
}
.cmi-chat-toggle:hover {
  background: var(--cmi-chat-primary-dk);
  transform: scale(1.05);
}
.cmi-chat-toggle-icon { line-height: 1; }

/* ── Panel (expanded state) ───────────────────────────────────────────────── */
.cmi-chat-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: var(--cmi-chat-width);
  max-width: 95vw;
  background: var(--cmi-chat-bg);
  border-left: 1px solid var(--cmi-chat-border);
  box-shadow: var(--cmi-chat-shadow);
  z-index: 9998;
  display: none;
  flex-direction: column;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--cmi-chat-text);
}
.cmi-chat-panel.open { display: flex; }

/* ── Header ───────────────────────────────────────────────────────────────── */
.cmi-chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--cmi-chat-border);
  background: var(--cmi-chat-bg);
}
.cmi-chat-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 14px;
}
.cmi-chat-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--cmi-chat-success);
  box-shadow: 0 0 8px var(--cmi-chat-success);
}
.cmi-chat-actions { display: flex; gap: 4px; }
.cmi-chat-icon-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--cmi-chat-muted);
  cursor: pointer;
  width: 28px;
  height: 28px;
  border-radius: 4px;
  font-size: 14px;
  line-height: 1;
}
.cmi-chat-icon-btn:hover {
  background: var(--cmi-chat-border);
  color: var(--cmi-chat-text);
}

/* ── Messages list ────────────────────────────────────────────────────────── */
.cmi-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 13.5px;
  line-height: 1.5;
}

.cmi-chat-msg { display: flex; }
.cmi-chat-msg-user { justify-content: flex-end; }
.cmi-chat-msg-assistant, .cmi-chat-msg-tool, .cmi-chat-msg-system, .cmi-chat-msg-error { justify-content: flex-start; }

.cmi-chat-bubble {
  max-width: 90%;
  padding: 8px 12px;
  border-radius: var(--cmi-chat-radius);
  word-wrap: break-word;
  overflow-wrap: break-word;
}
.cmi-chat-msg-user .cmi-chat-bubble {
  background: var(--cmi-chat-bubble-user);
  color: #fff;
  border-bottom-right-radius: 2px;
}
.cmi-chat-msg-assistant .cmi-chat-bubble {
  background: var(--cmi-chat-bubble-ai);
  color: var(--cmi-chat-text);
  border-bottom-left-radius: 2px;
}
.cmi-chat-msg-system .cmi-chat-bubble {
  background: transparent;
  color: var(--cmi-chat-muted);
  font-size: 12px;
  font-style: italic;
}
.cmi-chat-msg-error .cmi-chat-bubble {
  background: rgba(220, 53, 69, 0.1);
  color: var(--cmi-chat-danger);
  border: 1px solid var(--cmi-chat-danger);
  font-size: 12.5px;
}
.cmi-chat-bubble code {
  background: rgba(0,0,0,0.08);
  padding: 1px 4px;
  border-radius: 3px;
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 12px;
}
.cmi-chat-bubble a { color: inherit; text-decoration: underline; }

/* Tool-call card */
.cmi-chat-msg-tool { width: 100%; }
.cmi-chat-tool-card {
  width: 100%;
  background: var(--cmi-chat-bubble-ai);
  border: 1px solid var(--cmi-chat-border);
  border-radius: var(--cmi-chat-radius);
  padding: 6px 10px;
  font-size: 12px;
}
.cmi-chat-tool-card summary {
  cursor: pointer;
  color: var(--cmi-chat-muted);
  user-select: none;
  padding: 2px 0;
}
.cmi-chat-tool-card summary code {
  background: transparent;
  color: var(--cmi-chat-primary);
  font-weight: 600;
}
.cmi-chat-tool-card pre {
  margin: 8px 0 0;
  padding: 8px;
  background: rgba(0,0,0,0.06);
  border-radius: 4px;
  overflow-x: auto;
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 11.5px;
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-word;
}

/* Typing indicator */
.cmi-chat-dots {
  display: inline-flex;
  gap: 4px;
  padding: 2px 0;
}
.cmi-chat-dots span {
  width: 6px;
  height: 6px;
  background: var(--cmi-chat-muted);
  border-radius: 50%;
  animation: cmi-chat-bounce 1.2s infinite ease-in-out;
}
.cmi-chat-dots span:nth-child(2) { animation-delay: 0.15s; }
.cmi-chat-dots span:nth-child(3) { animation-delay: 0.3s; }
@keyframes cmi-chat-bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.5; }
  40% { transform: translateY(-4px); opacity: 1; }
}

/* ── Confirmation modal (inline above input) ──────────────────────────────── */
.cmi-chat-confirm {
  border-top: 1px solid var(--cmi-chat-border);
  padding: 10px;
  background: var(--cmi-chat-bg);
}
.cmi-chat-confirm-card {
  border: 2px solid var(--cmi-chat-primary);
  border-radius: var(--cmi-chat-radius);
  padding: 10px;
  background: var(--cmi-chat-bubble-ai);
}
.cmi-chat-confirm-title {
  font-weight: 600;
  font-size: 13px;
  margin-bottom: 4px;
}
.cmi-chat-confirm-tool {
  font-size: 12px;
  margin-bottom: 6px;
}
.cmi-chat-confirm-tool code {
  background: rgba(0,0,0,0.08);
  padding: 2px 6px;
  border-radius: 3px;
  font-family: "SFMono-Regular", Consolas, monospace;
  font-weight: 600;
  color: var(--cmi-chat-primary);
}
.cmi-chat-confirm-args {
  background: rgba(0,0,0,0.06);
  padding: 6px 8px;
  border-radius: 4px;
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 11px;
  max-height: 120px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-word;
  margin-bottom: 8px;
}
.cmi-chat-confirm-buttons {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.cmi-btn {
  padding: 6px 10px;
  border-radius: 4px;
  border: 1px solid var(--cmi-chat-border);
  font-size: 12px;
  cursor: pointer;
  background: var(--cmi-chat-bg);
  color: var(--cmi-chat-text);
}
.cmi-btn:hover { background: var(--cmi-chat-border); }
.cmi-btn-primary {
  background: var(--cmi-chat-primary);
  color: #fff;
  border-color: var(--cmi-chat-primary);
}
.cmi-btn-primary:hover { background: var(--cmi-chat-primary-dk); }
.cmi-btn-danger {
  background: var(--cmi-chat-danger);
  color: #fff;
  border-color: var(--cmi-chat-danger);
}
.cmi-btn-secondary {
  background: transparent;
  color: var(--cmi-chat-text);
}

/* ── Input form ───────────────────────────────────────────────────────────── */
.cmi-chat-input-form {
  display: flex;
  align-items: stretch;
  gap: 6px;
  padding: 10px;
  border-top: 1px solid var(--cmi-chat-border);
  background: var(--cmi-chat-bg);
}
.cmi-chat-input-form textarea {
  flex: 1;
  border: 1px solid var(--cmi-chat-border);
  border-radius: 6px;
  padding: 8px 10px;
  font-family: inherit;
  font-size: 13px;
  resize: none;
  background: var(--cmi-chat-bg);
  color: var(--cmi-chat-text);
  line-height: 1.4;
}
.cmi-chat-input-form textarea:focus {
  outline: 2px solid var(--cmi-chat-primary);
  outline-offset: -1px;
  border-color: transparent;
}
.cmi-chat-input-form button {
  background: var(--cmi-chat-primary);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 0 14px;
  cursor: pointer;
  font-size: 16px;
}
.cmi-chat-input-form button:disabled {
  background: var(--cmi-chat-muted);
  cursor: not-allowed;
}
.cmi-chat-input-form button:hover:not(:disabled) {
  background: var(--cmi-chat-primary-dk);
}

/* ── Footer ───────────────────────────────────────────────────────────────── */
.cmi-chat-footer {
  display: flex;
  justify-content: space-between;
  padding: 4px 10px 6px;
  background: var(--cmi-chat-bg);
  font-size: 10.5px;
  color: var(--cmi-chat-muted);
  border-top: 1px solid var(--cmi-chat-border);
}
.cmi-chat-footer-right { font-family: "SFMono-Regular", Consolas, monospace; }

/* ── Responsive ───────────────────────────────────────────────────────────── */
@media (max-width: 600px) {
  .cmi-chat-panel { width: 100vw; max-width: 100vw; }
}
