/* ── Lens Companion ─────────────────────────────────────────────────── */

/* Floating bubble */
.companion-bubble {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 1000;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: #8566eb;
  border: none;
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
  transition: background 0.15s, transform 0.15s;
}

.companion-bubble:hover {
  background: #765ad0;
  transform: scale(1.05);
}

.companion-bubble:focus-visible {
  outline: 3px solid #c4b5fd;
  outline-offset: 3px;
}

.companion-bubble--hidden {
  display: none;
}

/* Panel */
.companion-panel {
  display: none;
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 1000;
  width: 380px;
  height: 60vh;
  min-height: 320px;
  max-height: 640px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.companion-panel--open {
  display: flex;
}

/* Hide panel when closed (override the flex from above) */
.companion-panel:not(.companion-panel--open) {
  display: none;
}

.companion-panel--sidebar-open {
  width: 560px;
}

/* Header */
.companion-panel__header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  flex-shrink: 0;
}

.companion-panel__history-toggle {
  background: none;
  border: none;
  color: rgba(255, 255, 255, 0.75);
  font-size: 1rem;
  cursor: pointer;
  line-height: 1;
  padding: 0 2px;
}

.companion-panel__history-toggle:hover {
  color: #fff;
}

/* Body: sidebar + main chat column */
.companion-panel__body {
  display: flex;
  flex: 1;
  min-height: 0;
}

.companion-main {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
}

/* Sidebar (collapsed by default) */
.companion-sidebar {
  width: 0;
  overflow: hidden;
  flex-shrink: 0;
  border-right: 1px solid #e2e8f0;
  display: flex;
  flex-direction: column;
  transition: width 0.15s;
}

.companion-sidebar--open {
  width: 180px;
}

.companion-new-chat-btn {
  margin: 10px;
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  color: #334155;
  font-size: 0.78rem;
  padding: 8px 10px;
  cursor: pointer;
  font-family: inherit;
  white-space: nowrap;
}

.companion-new-chat-btn:hover {
  border-color: var(--color-companion);
}

.companion-history-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 6px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.companion-history-empty {
  color: #475569;
  font-size: 0.72rem;
  text-align: center;
  padding: 12px 6px;
}

.companion-history-item-row {
  display: flex;
  align-items: center;
  gap: 2px;
  border-radius: 6px;
}

.companion-history-item-row:hover {
  background: #f8fafc;
}

.companion-history-item--active {
  background: #f5f3ff;
  border: 1px solid #ede9fe;
}

.companion-history-item {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  flex: 1;
  min-width: 0;
  background: none;
  border: none;
  border-radius: 6px;
  padding: 6px 8px;
  cursor: pointer;
  font-family: inherit;
  text-align: left;
}

.companion-history-item__title {
  color: #1e293b;
  font-size: 0.75rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.companion-history-item__time {
  color: #64748b;
  font-size: 0.65rem;
}

/* Inline rename */
.companion-history-rename-form {
  flex: 1;
  min-width: 0;
  padding: 6px 8px;
}

.companion-history-rename-input {
  width: 100%;
  background: #fff;
  border: 1px solid var(--color-companion);
  border-radius: 4px;
  color: #1e293b;
  font-size: 0.75rem;
  font-family: inherit;
  padding: 3px 6px;
}

.companion-history-rename-input:focus {
  outline: none;
}

/* Three-dot menu */
.companion-history-menu {
  position: relative;
  flex-shrink: 0;
}

.companion-history-menu__trigger {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  color: #64748b;
  font-size: 0.85rem;
  cursor: pointer;
  border-radius: 4px;
  opacity: 0;
  list-style: none;
}

.companion-history-menu__trigger::-webkit-details-marker {
  display: none;
}

.companion-history-item-row:hover .companion-history-menu__trigger,
.companion-history-menu[open] .companion-history-menu__trigger {
  opacity: 1;
}

.companion-history-menu__trigger:hover {
  color: #1e293b;
  background: #f1f5f9;
}

.companion-history-menu__dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  z-index: 10;
  margin-top: 2px;
  min-width: 110px;
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
  overflow: hidden;
}

.companion-history-menu__item {
  display: block;
  width: 100%;
  background: none;
  border: none;
  color: #334155;
  font-size: 0.75rem;
  font-family: inherit;
  text-align: left;
  padding: 7px 10px;
  cursor: pointer;
}

.companion-history-menu__item:hover {
  background: #f1f5f9;
}

.companion-history-menu__item--danger {
  color: #f87171;
}

.companion-panel__title {
  font-size: 0.85rem;
  font-weight: 600;
  color: #fff;
}

.companion-scope-chip {
  font-size: 0.72rem;
  font-weight: 400;
  background: rgba(255, 255, 255, 0.15);
  color: rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 20px;
  padding: 2px 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 160px;
}

.companion-panel__close {
  margin-left: auto;
  background: none;
  border: none;
  color: rgba(255, 255, 255, 0.75);
  font-size: 1.2rem;
  cursor: pointer;
  line-height: 1;
  padding: 0 2px;
}

.companion-panel__close:hover {
  color: #fff;
}

/* Thread */
.companion-panel__thread {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.companion-empty {
  color: #475569;
  font-size: 0.85rem;
  text-align: center;
  margin-top: 32px;
}

/* Messages */
.companion-message {
  display: flex;
  flex-direction: column;
}

.companion-message--user {
  align-items: flex-end;
}

.companion-message--agent {
  align-items: flex-start;
}

.companion-message__bubble {
  max-width: 88%;
  font-size: 0.83rem;
  line-height: 1.5;
}

.companion-message__bubble p {
  margin: 0 0 6px;
}

.companion-message__bubble p:last-child {
  margin-bottom: 0;
}

/* Scope marker */
.companion-scope-marker {
  text-align: center;
  color: #475569;
  font-size: 0.72rem;
  display: flex;
  align-items: center;
  gap: 8px;
}

.companion-scope-marker::before,
.companion-scope-marker::after {
  content: "";
  flex: 1;
  height: 1px;
  background: #334155;
}

/* Input row */
.companion-panel__input-row {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  background: #fff;
  border-top: 1px solid #e2e8f0;
  flex-shrink: 0;
}

.companion-panel__input-row textarea {
  flex: 1;
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  color: #1e293b;
  font-size: 0.83rem;
  padding: 8px 10px;
  resize: none;
  font-family: inherit;
  line-height: 1.4;
}

.companion-panel__input-row textarea:focus {
  outline: none;
  border-color: var(--color-companion);
}

.companion-panel__input-row textarea::placeholder {
  color: #94a3b8;
}

.companion-panel__input-row input[type="submit"] {
  border: none;
  border-radius: 8px;
  font-size: 0.83rem;
  padding: 0 14px;
  cursor: pointer;
  font-family: inherit;
  white-space: nowrap;
  align-self: flex-end;
}

.companion-panel__input-row input[type="submit"]:disabled,
.companion-panel__input-row textarea:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ── Pinned insights — pin control overlays the top-right corner of an
   agent message bubble ─────────────────────────────────────────────── */
.companion-message--agent {
  position: relative;
}

.pin-control {
  position: absolute;
  top: -6px;
  right: 0;
  z-index: 1;
}

.pin-btn {
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 50%;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.72rem;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  /* Faintly visible at rest rather than opacity: 0 — fully invisible until
     hover made the pin action impossible to discover in practice. */
  opacity: 0.45;
  transition: opacity 0.15s, border-color 0.15s;
}

.companion-message--agent:hover .pin-btn,
.pin-btn--pinned {
  opacity: 1;
}

.pin-btn--pinned {
  border-color: var(--color-companion);
  color: var(--color-companion);
}

.pin-note {
  margin-top: 2px;
}

.pin-note-input {
  font-size: 0.7rem;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  padding: 3px 6px;
  width: 140px;
  font-family: inherit;
}

.pin-note-input:focus {
  outline: none;
  border-color: var(--color-companion);
}

/* Companion streaming: a blinking cursor after the text while tokens arrive
   (removed when the final event replaces the message). */
.companion-message--streaming [data-markdown] {
  white-space: pre-wrap;
}
.companion-message--streaming [data-markdown]::after {
  content: "▍";
  margin-left: 1px;
  animation: companion-cursor-blink 1s steps(1) infinite;
  color: var(--color-lens-muted, #94a3b8);
}
@keyframes companion-cursor-blink {
  50% { opacity: 0; }
}

/* reporting_v2: the collection a pin just went into, shown briefly. */
.pin-collection-flash {
  margin-left: 6px;
  animation: pin-collection-flash 3s ease-out forwards;
}

@keyframes pin-collection-flash {
  0%, 70% { opacity: 1; }
  100% { opacity: 0; visibility: hidden; }
}

/* ── Theme: panel shell, header, Send, user bubble ────────────────────
   Values are explicit (not shared with application.css). Tailwind utilities
   on these elements sit in a layer, so these unlayered rules win. */
.companion-panel {
  border: 1px solid #e6e3ed;
  border-radius: 16px;
  background: #fff;
  box-shadow: 0 7px 22px rgba(27, 23, 46, 0.12), 0 1px 3px rgba(27, 23, 46, 0.06);
}

/* White on #765ad0 is 5.07:1; on #8566eb it is 4.12:1 (under 4.5:1 for small text). */
.companion-panel__header { background: #765ad0; }
.companion-scope-chip { color: #fff; }

.companion-panel__input-row input[type="submit"] { background: #765ad0; color: #fff; }
.companion-panel__input-row input[type="submit"]:hover:not(:disabled) { background: #6d52c4; }

.companion-message--user .companion-message__bubble { background: #f5f2ff; border-color: #e8defb; }

/* ── Resize handle (top-left corner; hidden at 1024px and below) ───── */
.companion-resize-handle {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 3;
  width: 22px;
  height: 22px;
  cursor: nwse-resize;
  touch-action: none;
}

.companion-resize-handle::after {
  content: "";
  position: absolute;
  top: 5px;
  left: 5px;
  width: 9px;
  height: 9px;
  border-top: 2px solid rgba(255, 255, 255, 0.75);
  border-left: 2px solid rgba(255, 255, 255, 0.75);
  border-top-left-radius: 4px;
}

.companion-resize-handle:hover::after { border-color: #fff; }
.companion-panel--resizing { user-select: none; }

/* ── Narrow panel (< 560px wide): history sidebar overlays the chat ─── */
.companion-panel--narrow .companion-panel__body { position: relative; }

.companion-panel--narrow .companion-sidebar {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  z-index: 2;
  background: #fff;
}

.companion-panel--narrow .companion-sidebar--open {
  width: min(200px, 80%);
  box-shadow: 4px 0 18px rgba(27, 23, 46, 0.14);
}

/* ── Phones and tablets (≤ 1024px): full screen when open ─────────────
   !important beats any inline size left over from desktop resizing. */
@media (max-width: 1024px) {
  .companion-bubble { bottom: max(24px, env(safe-area-inset-bottom)); right: max(24px, env(safe-area-inset-right)); }

  .companion-panel {
    inset: 0;
    width: 100% !important;
    height: 100vh !important;
    height: 100dvh !important;
    min-height: 0 !important;
    max-height: none !important;
    border: 0;
    border-radius: 0;
    box-shadow: none;
  }

  .companion-panel__header { padding-top: calc(12px + env(safe-area-inset-top)); }
  .companion-panel__input-row { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  .companion-panel__input-row textarea { font-size: 16px; } /* stops iOS zooming on focus */
  .companion-panel__thread { overscroll-behavior: contain; }

  .companion-panel__close,
  .companion-panel__history-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
  }

  .companion-resize-handle { display: none; }
  body:has(.companion-panel--open) { overflow: hidden; }
}

/* Sidebar polish. Higher specificity than the global button/link rules, which
   otherwise gave "New chat" a red underlined look; and no stray 1px edge while
   the sidebar is collapsed. */
.companion-sidebar .companion-new-chat-btn,
.companion-sidebar form.button_to .companion-new-chat-btn {
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  color: #334155;
  font-size: 0.78rem;
  font-weight: 600;
  text-decoration: none;
}

.companion-sidebar .companion-new-chat-btn:hover { background: #f5f2ff; border-color: #765ad0; color: #5b43b8; }
.companion-sidebar:not(.companion-sidebar--open) { border-right: 0; }
