/* ============================================
   ai-graph.css — AI Conversation Graph View
   Premium monochrome design, SaaS-grade
   ============================================ */

/* Root layout */
.aig-root {
  display: flex; flex-direction: column;
  height: 100%; min-height: calc(100vh - 50px);
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
}
.aig-header {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  z-index: 10; position: relative;
  flex-shrink: 0;
}
.aig-title {
  font-size: 16px; font-weight: 600;
  border: none; background: transparent;
  color: var(--text); outline: none;
  padding: 4px 8px; border-radius: 6px;
  min-width: 120px;
}
.aig-title:focus { background: var(--hover); }
.aig-header-actions { display: flex; gap: 4px; margin-left: auto; }
.aig-btn {
  border: 1px solid var(--border);
  background: var(--bg-2);
  color: var(--text-2);
  padding: 6px 12px; border-radius: 6px;
  font-size: 12px; font-weight: 500;
  cursor: pointer; transition: all 0.12s;
  display: inline-flex; align-items: center; gap: 5px;
}
.aig-btn:hover { background: var(--hover-strong); color: var(--text); }
.aig-btn.primary { background: var(--text); color: var(--bg); border-color: var(--text); }
.aig-btn.primary:hover { opacity: 0.85; }

/* Body: canvas + panel */
.aig-body {
  display: flex; flex: 1; overflow: hidden;
  position: relative;
}

/* Canvas (SVG + nodes layer) */
.aig-canvas {
  flex: 1; position: relative;
  overflow: hidden; cursor: grab;
  background: var(--bg);
  /* Subtle dot grid */
  background-image: radial-gradient(circle, var(--border) 0.8px, transparent 0.8px);
  background-size: 24px 24px;
}
.aig-canvas:active { cursor: grabbing; }
.aig-canvas svg {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  pointer-events: none;
  overflow: visible;
  z-index: 1;
  transform-origin: 0 0;
  will-change: transform;
}
.aig-canvas svg path, .aig-canvas svg line {
  pointer-events: stroke;
}

/* Nodes container (HTML overlaid on SVG) */
.aig-nodes {
  position: absolute; inset: 0;
  pointer-events: none;
  transform-origin: 0 0;
  z-index: 2;
  will-change: transform;
}
.aig-node {
  position: absolute;
  pointer-events: auto;
  cursor: pointer;
  border-radius: 14px;
  padding: 14px 18px;
  min-width: 180px; max-width: 320px;
  font-size: 13px; line-height: 1.5;
  border: 1.5px solid var(--border);
  background: var(--bg);
  box-shadow: var(--shadow-sm);
  transition: box-shadow 0.2s, border-color 0.2s, transform 0.15s;
  user-select: none;
  word-break: break-word;
}
.aig-node:hover {
  box-shadow: var(--shadow-md);
  border-color: var(--border-strong);
}
.aig-node.selected {
  border-color: var(--text);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--text) 20%, transparent), var(--shadow-md);
}
.aig-node.dragging {
  opacity: 0.85;
  transform: scale(1.03);
  box-shadow: var(--shadow-lg);
  z-index: 100;
  cursor: grabbing;
}

/* Node types */
.aig-node[data-type="user"] {
  border-color: var(--text);
  border-width: 2px;
}
.aig-node[data-type="assistant"] {
  background: var(--bg-2);
  border-color: var(--border);
}
.aig-node[data-type="error"] {
  border-style: dashed;
  border-color: var(--danger);
}

/* Node inner */
.aig-node-header {
  display: flex; align-items: center; gap: 6px;
  margin-bottom: 6px;
  font-size: 11px; font-weight: 600;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.aig-node-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--text);
  flex-shrink: 0;
}
.aig-node[data-type="assistant"] .aig-node-dot { background: var(--text-2); }
.aig-node[data-type="error"] .aig-node-dot { background: var(--danger); }

.aig-node-content {
  color: var(--text);
  overflow: hidden;
  max-height: 140px;
  mask-image: linear-gradient(to bottom, black 80%, transparent 100%);
  -webkit-mask-image: linear-gradient(to bottom, black 80%, transparent 100%);
}
.aig-node.collapsed .aig-node-content {
  max-height: 40px;
}
.aig-node-status {
  margin-top: 6px;
  font-size: 11px; color: var(--text-3);
  display: flex; align-items: center; gap: 6px;
}
.aig-node-status .aig-spinner {
  width: 12px; height: 12px;
  border: 2px solid var(--border);
  border-top-color: var(--text);
  border-radius: 50%;
  animation: aig-spin 0.7s linear infinite;
}
@keyframes aig-spin { to { transform: rotate(360deg); } }

/* Edges (SVG paths) */
.aig-edge {
  fill: none;
  stroke: var(--text-2);
  stroke-width: 2.5;
  stroke-linecap: round;
}
[data-theme="dark"] .aig-edge {
  stroke: var(--text-3);
  stroke-width: 2.5;
}
.aig-edge.animated {
  stroke-dasharray: 200;
  stroke-dashoffset: 200;
  animation: aig-draw 0.6s ease forwards;
}
@keyframes aig-draw { to { stroke-dashoffset: 0; } }

/* Minimap / zoom controls */
.aig-controls {
  position: absolute; bottom: 80px; right: 16px;
  display: flex; flex-direction: column; gap: 4px;
  z-index: 20;
}
.aig-ctrl-btn {
  width: 32px; height: 32px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; font-size: 16px; font-weight: 500;
  box-shadow: var(--shadow-sm);
}
.aig-ctrl-btn:hover { background: var(--hover); }

/* Side panel (node details) */
.aig-panel {
  width: 320px; flex-shrink: 0;
  border-left: 1px solid var(--border);
  background: var(--bg);
  overflow-y: auto; padding: 16px;
  display: flex; flex-direction: column; gap: 12px;
  transition: width 0.25s, opacity 0.2s;
}
.aig-panel.hidden { width: 0; padding: 0; border: none; overflow: hidden; opacity: 0; }
.aig-panel h3 {
  font-size: 13px; font-weight: 600;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin: 0;
}
.aig-panel-content {
  font-size: 13px; line-height: 1.6;
  color: var(--text);
  white-space: pre-wrap;
  background: var(--bg-2);
  border-radius: 8px;
  padding: 12px;
  max-height: 300px;
  overflow-y: auto;
}
.aig-panel-actions {
  display: flex; flex-direction: column; gap: 6px;
}
.aig-panel-actions .aig-btn { justify-content: flex-start; width: 100%; }

/* Bottom chat bar */
.aig-chat {
  position: absolute; bottom: 0; left: 0; right: 0;
  padding: 12px 20px;
  background: var(--bg);
  border-top: 1px solid var(--border);
  display: flex; align-items: flex-end; gap: 10px;
  z-index: 20;
}
.aig-chat-input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 16px;
  font-size: 14px;
  color: var(--text);
  background: var(--bg-2);
  outline: none;
  resize: none;
  min-height: 20px;
  max-height: 120px;
  font-family: inherit;
  line-height: 1.5;
  overflow-y: auto;
}
.aig-chat-input:focus { border-color: var(--text); }
.aig-chat-input::placeholder { color: var(--text-3); }
.aig-send-btn {
  width: 38px; height: 38px;
  border-radius: 10px;
  border: none;
  background: var(--text);
  color: var(--bg);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: opacity 0.12s, transform 0.1s;
  flex-shrink: 0;
}
.aig-send-btn:hover { opacity: 0.8; }
.aig-send-btn:active { transform: scale(0.93); }
.aig-send-btn:disabled { opacity: 0.3; cursor: not-allowed; }
.aig-mode-sel {
  border: 1px solid var(--border);
  background: var(--bg-2);
  color: var(--text-2);
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 11px;
  cursor: pointer;
}

/* Responsive */
@media (max-width: 768px) {
  .aig-panel { position: absolute; right: 0; top: 0; bottom: 0; z-index: 30; width: 280px; }
  .aig-panel.hidden { width: 0; }
  .aig-node { min-width: 140px; max-width: 240px; padding: 10px 14px; }
  .aig-chat { padding: 10px 12px; }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  .aig-edge.animated { animation: none; stroke-dashoffset: 0; }
  .aig-node { transition: none; }
  @keyframes aig-spin { to { transform: none; } }
}


/* Full response modal */
.aig-full-modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 99000;
  display: flex; align-items: center; justify-content: center;
  backdrop-filter: blur(3px);
}
.aig-full-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  width: min(640px, 92vw);
  max-height: 80vh;
  display: flex; flex-direction: column;
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}
.aig-full-head {
  display: flex; align-items: center; gap: 8px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.aig-full-head h3 { margin: 0; font-size: 15px; font-weight: 600; flex: 1; }
.aig-full-close {
  background: transparent; border: none;
  font-size: 22px; cursor: pointer; color: var(--text-2);
  width: 32px; height: 32px; border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
}
.aig-full-close:hover { background: var(--hover); color: var(--text); }
.aig-full-body {
  padding: 20px; overflow-y: auto; flex: 1;
  font-size: 14px; line-height: 1.7; color: var(--text);
  white-space: pre-wrap; word-break: break-word;
}
.aig-full-foot {
  display: flex; gap: 6px; flex-wrap: wrap;
  padding: 12px 20px;
  border-top: 1px solid var(--border);
}

/* Collapsed badge */
.aig-collapsed-badge {
  margin-left: auto;
  font-size: 10px;
  color: var(--text-3);
  font-weight: 400;
}

/* Prevent text selection during canvas interactions */
.aig-canvas { user-select: none; -webkit-user-select: none; }
.aig-canvas * { -webkit-user-drag: none; user-drag: none; }
.aig-node { user-select: text; -webkit-user-select: text; cursor: grab; }
.aig-node.dragging { user-select: none !important; -webkit-user-select: none !important; cursor: grabbing; }
.aig-node .aig-node-content { pointer-events: none; }


/* ============================================
   Multi-graph list
   ============================================ */
.aig-list-toggle { gap: 6px; }
.aig-graph-list {
  position: absolute; top: 52px; left: 12px;
  width: 280px; max-height: 50vh; overflow-y: auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow-lg);
  z-index: 40;
  padding: 8px;
}
.aig-graph-list.hidden { display: none; }
.aig-gl-row {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.12s;
}
.aig-gl-row:hover { background: var(--hover); }
.aig-gl-row.current { background: var(--hover-strong); }
.aig-gl-title { font-size: 13px; font-weight: 500; color: var(--text); }
.aig-gl-meta { font-size: 11px; color: var(--text-3); margin-top: 2px; }

/* Edge endpoint dot */
.aig-edge-dot { fill: var(--border-strong); }

/* Collapsed indicator */
.aig-node.has-collapsed { border-style: dashed; }
.aig-collapsed-badge { margin-left: auto; font-size: 11px; color: var(--text-3); }

/* Canvas touch/pointer */
.aig-canvas { touch-action: none; overscroll-behavior: contain; }


/* ============================================
   V3 — Graph list search, context chip, retry, responsive polish
   ============================================ */
.aig-gl-search {
  width: 100%; padding: 8px 12px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg-2); color: var(--text);
  font-size: 13px; outline: none; margin-bottom: 8px;
}
.aig-gl-search:focus { border-color: var(--text); }
.aig-gl-list { display: flex; flex-direction: column; gap: 2px; }
.aig-gl-acts { display: flex; gap: 4px; opacity: 0; transition: opacity 0.15s; }
.aig-gl-row:hover .aig-gl-acts { opacity: 1; }
.aig-gl-act {
  width: 24px; height: 24px; border-radius: 6px;
  border: 1px solid var(--border); background: var(--bg);
  color: var(--text-2); cursor: pointer; font-size: 14px;
  display: flex; align-items: center; justify-content: center;
}
.aig-gl-act:hover { background: var(--hover); color: var(--text); }
.aig-gl-del:hover { color: var(--danger); }
.aig-gl-empty { padding: 16px; text-align: center; color: var(--text-3); font-size: 13px; }

/* Context chip */
.aig-ctx-chip {
  font-size: 11px; color: var(--text-2);
  background: var(--bg-3); border-radius: 99px;
  padding: 4px 10px; max-width: 200px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.aig-ctx-chip:empty { display: none; }

/* Retry button */
.aig-retry-btn {
  margin-top: 6px; font-size: 11px;
  border: 1px solid var(--border); background: var(--bg-2);
  color: var(--text-2); padding: 4px 10px; border-radius: 6px;
  cursor: pointer;
}
.aig-retry-btn:hover { background: var(--hover); color: var(--text); }

/* Kbd in help */
.aig-full-body kbd {
  background: var(--bg-3); border: 1px solid var(--border);
  padding: 2px 6px; border-radius: 4px; font-size: 12px;
  font-family: var(--font-mono);
}

/* Mobile improvements */
@media (max-width: 768px) {
  .aig-header { flex-wrap: wrap; gap: 6px; padding: 10px 12px; }
  .aig-header-actions { flex-wrap: wrap; gap: 4px; }
  .aig-header-actions .aig-btn { padding: 5px 8px; font-size: 11px; }
  .aig-graph-list { width: calc(100vw - 24px); left: 12px; }
  .aig-panel { width: 100%; position: fixed; bottom: 56px; top: auto; left: 0;
    max-height: 40vh; border-left: none; border-top: 1px solid var(--border);
    border-radius: 14px 14px 0 0; z-index: 35;
  }
  .aig-chat { padding: 8px 10px; }
  .aig-chat-input { font-size: 14px; padding: 8px 12px; }
}


/* ============================================
   Custom confirm modal
   ============================================ */
.aig-confirm-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 99500;
  display: flex; align-items: center; justify-content: center;
  backdrop-filter: blur(3px);
}
.aig-confirm-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  width: min(380px, 90vw);
  box-shadow: var(--shadow-lg);
}
.aig-confirm-title { margin: 0 0 8px; font-size: 16px; font-weight: 600; color: var(--text); }
.aig-confirm-msg { margin: 0 0 20px; font-size: 13px; color: var(--text-2); line-height: 1.6; white-space: pre-line; }
.aig-confirm-btns { display: flex; gap: 8px; justify-content: flex-end; }
.aig-btn-danger { background: var(--danger, #eb5757) !important; color: white !important; border-color: transparent !important; }
.aig-btn-danger:hover { opacity: 0.9; }

/* ============================================
   Search bar (floating)
   ============================================ */
.aig-search-bar {
  position: absolute; top: 52px; left: 50%;
  transform: translateX(-50%);
  display: flex; align-items: center; gap: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 14px;
  box-shadow: var(--shadow-md);
  z-index: 50;
  min-width: 280px;
}
.aig-search-input {
  flex: 1; border: none; background: transparent;
  outline: none; font-size: 13px; color: var(--text);
  min-width: 0;
}
.aig-search-input::placeholder { color: var(--text-3); }
.aig-search-count { font-size: 11px; color: var(--text-3); white-space: nowrap; }
.aig-search-close {
  background: transparent; border: none; color: var(--text-2);
  cursor: pointer; font-size: 16px; padding: 0 4px;
}
.aig-search-close:hover { color: var(--text); }

/* Search match highlight */
.aig-node.search-match {
  outline: 2px solid var(--text);
  outline-offset: 2px;
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--text) 15%, transparent), var(--shadow-md);
}


/* ─── Node entry / hover animations ───────────────────────── */
.aig-node {
  transition: box-shadow 0.18s ease, border-color 0.18s ease, transform 0.15s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.aig-node:hover {
  box-shadow: 0 8px 22px rgba(0,0,0,0.08), 0 2px 6px rgba(0,0,0,0.04);
  transform: translateY(-2px);
  border-color: var(--text-3);
}
.aig-node.dragging {
  cursor: grabbing !important;
  box-shadow: 0 14px 32px rgba(0,0,0,0.18), 0 3px 8px rgba(0,0,0,0.06);
  transform: rotate(-0.5deg);
  z-index: 10;
}
.aig-node.selected {
  box-shadow: 0 0 0 2px var(--text), 0 8px 22px rgba(0,0,0,0.08);
  border-color: var(--text);
}
.aig-node.search-match {
  box-shadow: 0 0 0 2px #f5b800, 0 4px 12px rgba(245, 184, 0, 0.25);
  animation: aig-pulse 1.2s ease-in-out 2;
}
@keyframes aig-pulse {
  0%, 100% { box-shadow: 0 0 0 2px #f5b800, 0 4px 12px rgba(245, 184, 0, 0.25); }
  50% { box-shadow: 0 0 0 4px #f5b800, 0 6px 18px rgba(245, 184, 0, 0.4); }
}

/* Edge subtle drawing animation */
.aig-edge {
  stroke-dasharray: 1000;
  animation: aig-edge-draw 0.45s ease-out forwards;
}
@keyframes aig-edge-draw {
  from { stroke-dashoffset: 1000; opacity: 0; }
  to { stroke-dashoffset: 0; opacity: 1; }
}

/* Graph list redesign with hover actions */
.aig-gl-row {
  transition: background 0.15s ease, transform 0.12s ease;
  position: relative;
}
.aig-gl-row:hover {
  background: var(--bg-2);
  transform: translateX(2px);
}
.aig-gl-row.current {
  background: var(--bg-2);
  border-left: 2px solid var(--text);
}

.aig-gl-acts {
  display: flex; gap: 4px; opacity: 0;
  transition: opacity 0.15s ease;
}
.aig-gl-row:hover .aig-gl-acts,
.aig-gl-row.current .aig-gl-acts { opacity: 1; }

.aig-gl-act {
  width: 26px; height: 26px; border-radius: 6px; border: none;
  background: transparent; cursor: pointer; color: var(--text-3);
  font-size: 14px; display: flex; align-items: center; justify-content: center;
  transition: background 0.15s ease, color 0.15s ease;
}
.aig-gl-act:hover { background: var(--border); color: var(--text); }
.aig-gl-act.aig-gl-del:hover { color: var(--danger, #d92d20); background: rgba(217, 45, 32, 0.08); }

/* Spinner */
.aig-spinner {
  display: inline-block; width: 12px; height: 12px;
  border: 2px solid var(--text-3); border-top-color: var(--text);
  border-radius: 50%; animation: aig-spin 0.7s linear infinite;
  margin-right: 6px;
}
@keyframes aig-spin { to { transform: rotate(360deg); } }

/* Node entry — staggered fade-up */
.aig-node {
  animation: aig-node-in 0.32s cubic-bezier(0.34, 1.56, 0.64, 1) backwards;
}
@keyframes aig-node-in {
  from { opacity: 0; transform: translateY(8px) scale(0.96); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
