/* "Ask CoderFlow" assistant — launcher button + chat drawer (Phase 2).
   Uses the app's theme custom properties so it follows light/dark automatically. */

.ask-cf-launcher {
  position: fixed;
  right: var(--ask-cf-edge-inline, 20px);
  bottom: var(--ask-cf-launcher-bottom, 20px);
  /* Sit above the per-app QA float button/panel (999998/999999) so the
     launcher and its drawer are never covered by that widget. */
  z-index: 1000000;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border: none;
  border-radius: 999px;
  background: var(--color-accent, #3f72ff);
  color: #fff;
  font: inherit;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  box-shadow: var(--shadow-card, 0 10px 30px rgba(15, 23, 42, 0.2));
  transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
  touch-action: none;
  user-select: none;
}
.ask-cf-launcher.ask-cf-position-left {
  right: auto;
  left: var(--ask-cf-edge-inline, 20px);
}
.ask-cf-launcher.ask-cf-dragging {
  cursor: grabbing;
  transition: none;
}
.ask-cf-launcher:hover {
  transform: translateY(-1px);
  background: var(--color-accent-dark, #2f5ae0);
}
.ask-cf-launcher:active { transform: translateY(0); }
.ask-cf-launcher:focus-visible {
  outline: 2px solid var(--color-accent, #3f72ff);
  outline-offset: 2px;
}
.ask-cf-launcher-label { line-height: 1; }
.ask-cf-launcher svg { flex: none; }

/* When the per-app feedback / QA widget is also on the page (e.g. previewing
   CoderFlow itself), lift the launcher only when both floating controls are on
   the same side. */
body:not(.ask-cf-position-left):has(.profound-feedback-float-btn):not(:has(.profound-feedback-panel.left)),
body.ask-cf-position-left:has(.profound-feedback-float-btn):has(.profound-feedback-panel.left) {
  --ask-cf-launcher-bottom: 88px;
  --ask-cf-panel-bottom: 88px;
  --ask-cf-panel-height: min(620px, calc(100vh - 108px));
}

/* While the QA panel is open we're in "feedback mode" — get the Ask
   launcher out of the way so it doesn't crowd or overlap that panel.
   It reappears automatically when the panel closes (.open is removed). */
body:has(.profound-feedback-panel.open) .ask-cf-launcher {
  opacity: 0;
  pointer-events: none;
  visibility: hidden;
}

/* Panel / drawer */
.ask-cf-panel {
  position: fixed;
  right: var(--ask-cf-edge-inline, 20px);
  bottom: var(--ask-cf-panel-bottom, 20px);
  z-index: 1000001;
  width: min(420px, calc(100vw - 40px));
  height: var(--ask-cf-panel-height, min(620px, calc(100vh - 40px)));
  display: flex;
  flex-direction: column;
  background: var(--color-surface, #fff);
  color: var(--color-text, #1f2a44);
  border: 1px solid var(--color-border, rgba(15, 23, 42, 0.1));
  border-radius: var(--radius-large, 16px);
  box-shadow: var(--shadow-card, 0 20px 60px rgba(15, 23, 42, 0.18));
  overflow: hidden;
  opacity: 0;
  transform: translateY(12px) scale(0.98);
  transform-origin: bottom right;
  transition: opacity 0.18s ease, transform 0.18s ease;
}
.ask-cf-panel.ask-cf-position-left {
  right: auto;
  left: var(--ask-cf-edge-inline, 20px);
  transform-origin: bottom left;
}
.ask-cf-panel.ask-cf-open {
  opacity: 1;
  transform: translateY(0) scale(1);
}
.ask-cf-panel[hidden] { display: none; }

.ask-cf-drop-target {
  position: fixed;
  right: var(--ask-cf-edge-inline, 20px);
  bottom: var(--ask-cf-launcher-bottom, 20px);
  box-sizing: border-box;
  width: 56px;
  height: 56px;
  border: 2px dashed var(--color-accent, #3f72ff);
  border-radius: 999px;
  background: rgba(var(--color-accent-rgb, 63, 114, 255), 0.1);
  display: none;
  pointer-events: none;
  z-index: 999999;
}
.ask-cf-drop-target.ask-cf-drop-target-left {
  right: auto;
  left: var(--ask-cf-edge-inline, 20px);
}

.ask-cf-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--color-border, rgba(15, 23, 42, 0.08));
  background: var(--color-surface-muted, rgba(15, 23, 42, 0.02));
}
.ask-cf-title {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 15px;
}
.ask-cf-title svg { color: var(--color-accent, #3f72ff); }
.ask-cf-header-actions { display: inline-flex; gap: 4px; }
.ask-cf-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  width: 32px;
  height: 32px;
  padding: 0;
  border: none;
  border-radius: var(--radius-small, 8px);
  background: transparent;
  color: var(--color-text-secondary, #5b6b8c);
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
}
.ask-cf-icon-btn:hover {
  background: var(--color-border, rgba(15, 23, 42, 0.08));
  color: var(--color-text, #1f2a44);
}

/* Messages */
.ask-cf-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.ask-cf-empty {
  margin: auto;
  text-align: center;
  color: var(--color-text-secondary, #5b6b8c);
  padding: 24px 12px;
}
.ask-cf-empty p { margin: 0 0 6px; }
.ask-cf-empty-hint { font-size: 13px; color: var(--color-text-muted, #8a97b1); }

.ask-cf-unavailable {
  font-size: 13px;
  padding: 10px 12px;
  border-radius: var(--radius-small, 8px);
  background: rgba(var(--color-accent-rgb, 63, 114, 255), 0.08);
  border: 1px solid rgba(var(--color-accent-rgb, 63, 114, 255), 0.25);
  color: var(--color-text-secondary, #5b6b8c);
}

.ask-cf-msg { display: flex; }
.ask-cf-msg-user { justify-content: flex-end; }
.ask-cf-msg-assistant { justify-content: flex-start; }
.ask-cf-msg-body {
  max-width: 88%;
  padding: 10px 12px;
  border-radius: var(--radius-medium, 12px);
  font-size: 14px;
  line-height: 1.5;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.ask-cf-msg-user .ask-cf-msg-body {
  background: var(--color-accent, #3f72ff);
  color: #fff;
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;
}
.ask-cf-msg-assistant .ask-cf-msg-body {
  background: var(--color-surface-muted, rgba(15, 23, 42, 0.04));
  border: 1px solid var(--color-border, rgba(15, 23, 42, 0.06));
  border-bottom-left-radius: 4px;
}

/* Answer markdown */
.ask-cf-answer > *:first-child { margin-top: 0; }
.ask-cf-answer > *:last-child { margin-bottom: 0; }
.ask-cf-answer p { margin: 0 0 8px; }
.ask-cf-answer ul { margin: 0 0 8px; padding-left: 20px; }
.ask-cf-answer li { margin: 2px 0; }
.ask-cf-answer h3, .ask-cf-answer h4, .ask-cf-answer h5, .ask-cf-answer h6 {
  margin: 10px 0 6px;
  font-size: 14px;
  font-weight: 700;
}
.ask-cf-answer code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.88em;
  background: var(--color-border, rgba(15, 23, 42, 0.1));
  padding: 1px 5px;
  border-radius: 4px;
}
.ask-cf-answer pre {
  background: var(--color-background, #0b1220);
  color: var(--color-text, #e2e8f0);
  padding: 10px 12px;
  border-radius: var(--radius-small, 8px);
  overflow-x: auto;
  margin: 0 0 8px;
}
.ask-cf-answer pre code { background: none; padding: 0; }
.ask-cf-answer a { color: var(--color-accent, #3f72ff); }
.ask-cf-answer-error { color: var(--color-danger, #dc2626); }

/* Status / spinner */
.ask-cf-status {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--color-text-secondary, #5b6b8c);
  font-size: 13px;
}
.ask-cf-spinner {
  width: 14px;
  height: 14px;
  border: 2px solid var(--color-border, rgba(15, 23, 42, 0.2));
  border-top-color: var(--color-accent, #3f72ff);
  border-radius: 50%;
  animation: ask-cf-spin 0.7s linear infinite;
  flex: none;
}
@keyframes ask-cf-spin { to { transform: rotate(360deg); } }

/* Sources */
.ask-cf-sources {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px dashed var(--color-border, rgba(15, 23, 42, 0.15));
}
.ask-cf-sources-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-muted, #8a97b1);
  margin-bottom: 4px;
}
.ask-cf-source {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  margin: 3px 0;
}
.ask-cf-source a { color: var(--color-accent, #3f72ff); text-decoration: none; }
.ask-cf-source a:hover { text-decoration: underline; }
.ask-cf-source-badge {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 1px 6px;
  border-radius: 999px;
  background: var(--color-border, rgba(15, 23, 42, 0.1));
  color: var(--color-text-muted, #8a97b1);
}

/* Input */
.ask-cf-input {
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid var(--color-border, rgba(15, 23, 42, 0.08));
  background: var(--color-surface, #fff);
}
.ask-cf-input textarea {
  flex: 1;
  min-width: 0;
  resize: none;
  border: 1px solid var(--color-border, rgba(15, 23, 42, 0.15));
  border-radius: var(--radius-medium, 12px);
  padding: 9px 12px;
  font: inherit;
  font-size: 14px;
  line-height: 1.4;
  background: var(--color-background, #fff);
  color: var(--color-text, #1f2a44);
  max-height: 160px;
  /* Hidden by default so a single, non-wrapping line never shows a
     scrollbar; autoGrow() flips this to auto only once we hit max-height. */
  overflow-y: hidden;
}
.ask-cf-input textarea:focus {
  outline: none;
  border-color: var(--color-accent, #3f72ff);
  box-shadow: 0 0 0 3px rgba(var(--color-accent-rgb, 63, 114, 255), 0.18);
}
.ask-cf-send {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  padding: 0;
  flex: none;
  border: none;
  border-radius: var(--radius-medium, 12px);
  background: var(--color-accent, #3f72ff);
  color: #fff;
  cursor: pointer;
  transition: background 0.12s ease, opacity 0.12s ease;
}
.ask-cf-send:hover { background: var(--color-accent-dark, #2f5ae0); }
.ask-cf-send:disabled { opacity: 0.5; cursor: default; }

/* Send/stop icon swap: the send button doubles as a stop control while a
   response is streaming (panel gets .ask-cf-streaming). */
.ask-cf-send .ask-cf-ic-stop { display: none; }
.ask-cf-streaming .ask-cf-send { background: var(--color-danger, #dc2626); }
.ask-cf-streaming .ask-cf-send:hover { background: var(--color-danger, #dc2626); filter: brightness(0.93); }
.ask-cf-streaming .ask-cf-send .ask-cf-ic-send { display: none; }
.ask-cf-streaming .ask-cf-send .ask-cf-ic-stop { display: inline; }

@media (max-width: 720px) {
  .ask-cf-launcher {
    right: max(16px, env(safe-area-inset-right));
    bottom: var(--ask-cf-launcher-bottom, max(16px, env(safe-area-inset-bottom)));
    width: 48px;
    height: 48px;
    justify-content: center;
    padding: 0;
  }

  .ask-cf-launcher.ask-cf-position-left {
    right: auto;
    left: max(16px, env(safe-area-inset-left));
  }

  /* The dashboard and task-detail pages expose their own mobile bottom docks.
     Keep the Ask button above those bars instead of competing for the same
     bottom-right tap target. */
  body:has(.mobile-task-dock),
  body:has(.mobile-task-detail-dock) {
    --ask-cf-launcher-bottom: calc(94px + env(safe-area-inset-bottom));
  }

  .ask-cf-panel {
    inset: 0;
    bottom: 0;
    right: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    max-height: 100dvh;
    transform-origin: bottom center;
    border-radius: 0;
    border: none;
  }

  .ask-cf-panel.ask-cf-position-left {
    inset: 0;
    right: 0;
    left: 0;
    transform-origin: bottom center;
  }

  .ask-cf-drop-target {
    right: max(16px, env(safe-area-inset-right));
    bottom: var(--ask-cf-launcher-bottom, max(16px, env(safe-area-inset-bottom)));
    width: 48px;
    height: 48px;
  }

  .ask-cf-drop-target.ask-cf-drop-target-left {
    right: auto;
    left: max(16px, env(safe-area-inset-left));
  }

  .ask-cf-header {
    padding-top: max(12px, env(safe-area-inset-top));
  }

  .ask-cf-input {
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }

  .ask-cf-launcher-label { display: none; }
}
