/* Shared ElevenLabs v4 agent surfaces. Loaded by the shell so opening Studio
   directly has the same preview/workflow layout as opening it from the list.
   Scope these rules to agents; keep the existing orb palettes and call states. */
/* Tokens que usan los módulos de Agentes de IA (06-11-2026). En pruebas también los definen las hojas globales
   elevenlabs-v4-theme/controls/modules con los mismos valores; aquí van acotados a los dos módulos para que funcionen
   sin esas hojas (producción) y sin cambiar nada del resto del CRM. */
:root body :is(.aag, .ai-agent-editor) {
  --dt-bg: var(--dt-app);
  --el-stage-bg: var(--dt-surface);
  --el-card-stone: var(--dt-surface-2);
  --el-card-stone-hover: var(--dt-surface-3, var(--dt-hover));
  --el-ink: var(--dt-text);
  --el-ink-secondary: var(--dt-muted);
  --el-ink-muted: var(--dt-faint);
  --el-cta-bg: var(--dt-cta);
  --el-cta-fg: var(--dt-cta-fg);
  --el-cta-hover: var(--dt-cta-hover);
  --el-font-sans: var(--font-sans);
  --el-shadow-card: var(--dt-card-shadow);
}
/* Modo oscuro de los dos módulos: en pruebas lo aplica elevenlabs-v4-modules.css a todo el CRM; aquí solo a agentes */
:root[data-bs-theme="dark"] body :is(.aag, .ai-agent-editor) {
  --input-color: var(--dt-field-bg) !important;
  --card-bg: var(--dt-surface) !important;
  --bg-2: var(--dt-surface) !important;
  --bg-3: var(--dt-surface-2) !important;
  --aag-surface: var(--dt-surface) !important;
  --aag-surface2: var(--dt-surface-2) !important;
}
:root[data-bs-theme="dark"] body :is(
  .aag-btn-use-tpl, .aag-big-conic-callbtn, .aag-prv-send-btn, .aag-btn-dark-pill, .aag-panel__btn--primary,
  .el-tpl-use-btn, .el-aset-btn-primary, .el-aset-apply-btn, .el-prv-ctrl-btn.primary, .el-dock-pill-btn.call,
  .el-studio-orb-call-btn, .el-studio-inline-send, .el-tpl-cat-btn.active
) {
  background: var(--dt-cta) !important;
  border-color: transparent !important;
  color: var(--dt-cta-fg) !important;
  box-shadow: none !important;
}
:root body :is(.aag, .ai-agent-editor) .aag-browse-right {
  display: flex;
  flex-direction: column;
  position: relative;
  min-width: 0;
  min-height: 560px;
  background-color: var(--dt-surface-2);
  background-image: radial-gradient(var(--dt-border-strong) 1px, transparent 1px);
  background-size: 18px 18px;
  color: var(--dt-text);
}
:root body .ai-agent-editor #elStudioPreviewDock { border-left: 1px solid transparent; }
:root body .ai-agent-editor .dock-collapsed #elStudioPreviewDock { display: none; }
:root body .ai-agent-editor .ai-header-actions {
  flex-wrap: wrap;
  min-width: 0;
  max-width: 100%;
}
:root body .ai-agent-editor :is(.ai-btn, .el-studio-btn-ghost, .el-studio-btn-autopilot, .el-aset-btn-ghost) {
  border-radius: 999px;
}
:root body .ai-agent-editor .ai-main-container { border-radius: 24px; }
:root body :is(.aag, .ai-agent-editor) .aag-browse-right__bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid transparent;
  background: var(--dt-surface);
  z-index: 2;
}
:root body :is(.aag, .ai-agent-editor) .aag-canvas-seg {
  display: inline-flex;
  padding: 3px;
  border: 1px solid transparent;
  border-radius: 999px;
  background: var(--dt-surface-2);
}
:root body :is(.aag, .ai-agent-editor) .aag-canvas-seg button {
  min-height: 30px;
  padding: 0 13px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--dt-muted);
  font: 500 12.5px/1.3 var(--font-sans, 'Inter', sans-serif);
  cursor: pointer;
}
:root body :is(.aag, .ai-agent-editor) .aag-canvas-seg button.is-active {
  background: var(--dt-surface);
  color: var(--dt-text);
  box-shadow: none;
}
:root body :is(.aag, .ai-agent-editor) .aag-canvas-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}
:root body :is(.aag, .ai-agent-editor) .aag-prv-color-bar { flex-wrap: wrap; }
:root body :is(.aag, .ai-agent-editor) .aag-prv-chat-input { min-width: 0; }
:root body :is(.aag, .ai-agent-editor) .aag-chat-bubble { overflow-wrap: anywhere; }

/* Workflow preview is also shared by both entry points. Size the layout itself
   instead of scaling it visually and leaving overflowing, invisible boxes. */
:root body :is(.aag, .ai-agent-editor) .aag-wf-stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  min-width: 0;
  padding: 16px;
  transform: none !important;
}
:root body :is(.aag, .ai-agent-editor) :is(.aag-wf-card-node, .aag-wf-pill-node) {
  max-width: 100%;
  box-sizing: border-box;
  padding: 12px 14px;
  border: 0;
  border-radius: 12px;
  background: var(--dt-surface);
  color: var(--dt-text);
  box-shadow: none;
  text-align: left;
  overflow-wrap: anywhere;
}
:root body :is(.aag, .ai-agent-editor) .aag-wf-pill-node {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border-radius: 999px;
  font-size: 12.5px;
}
:root body :is(.aag, .ai-agent-editor) .aag-wf-card-node__head {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-bottom: 5px;
  color: var(--dt-text);
  font-size: 12.5px;
  font-weight: 600;
}
:root body :is(.aag, .ai-agent-editor) .aag-wf-card-node__body {
  color: var(--dt-muted);
  font-size: 12px;
  line-height: 1.5;
}
:root body :is(.aag, .ai-agent-editor) .aag-wf-card-node__chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-top: 8px;
  padding: 2px 7px;
  border-radius: 6px;
  background: var(--dt-surface-2);
  color: var(--dt-muted);
  font-size: 11px;
}
:root body :is(.aag, .ai-agent-editor) .aag-wf-edge-label {
  max-width: 46%;
  padding: 4px 8px;
  border-radius: 999px;
  background: var(--dt-cta);
  color: var(--dt-cta-fg);
  font-size: 11px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  box-shadow: none;
}
:root body :is(.aag, .ai-agent-editor) .aag-wf-branches {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  align-items: start;
  width: 100%;
  max-width: 568px;
  gap: 16px !important;
}
:root body :is(.aag, .ai-agent-editor) .aag-wf-branches > .aag-wf-card-node { width: 100% !important; }
:root body :is(.aag, .ai-agent-editor) .aag-wf-stage svg path { stroke: var(--dt-muted); }

/* Command search must follow the selected theme and fit short viewports. */
:root body .aag-cmd-modal {
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 40px);
  max-height: calc(100dvh - 40px);
  background: var(--dt-overlay);
  color: var(--dt-text);
  border-color: transparent;
  border-radius: 24px;
  box-shadow: var(--dt-pop-shadow);
}
:root body .aag-cmd-search { flex-shrink: 0; border-color: transparent; background: transparent; }
:root body .aag-cmd-search:focus-within { background: var(--dt-field-hover); }
:root body .aag-cmd-search input,
:root body .aag-cmd-search input:is(:hover, :focus, :focus-visible) { min-width: 0; color: var(--dt-text); background: transparent !important; border-color: transparent !important; box-shadow: none !important; }
:root body .aag-cmd-body { min-height: 0; overscroll-behavior: contain; }
:root body :is(.aag-cmd-item, .aag-cmd-item__txt b) { color: var(--dt-text); }
:root body :is(.aag-cmd-search i, .aag-cmd-sec, .aag-cmd-item__txt small, .aag-cmd-item__kbd) { color: var(--dt-muted); }
:root body .aag-cmd-item__ico { color: var(--dt-muted); background: var(--dt-surface-2); border-color: transparent; }
:root body .aag-cmd-item:is(:hover, :focus-visible) { background: var(--dt-hover); }
@media (forced-colors: active) {
  :root body .aag-cmd-search:focus-within { outline: 2px solid Highlight !important; outline-offset: -2px; }
}

/* Keep the template actions (including close) above the list on small screens.
   Only rearrange layout: JS remains responsible for opening/closing the modal. */
:root body .ai-agent-editor .el-tpl-modal-dialog {
  max-width: 100%;
  max-height: calc(100vh - 40px);
  max-height: calc(100dvh - 40px);
}
:root body .ai-agent-editor :is(.el-tpl-modal-left, .el-tpl-modal-right) { min-width: 0; min-height: 0; }
:root body .ai-agent-editor :is(.el-tpl-search-box input, .ai-tab-panels) { min-width: 0; }
:root body .ai-agent-editor .el-tpl-cat-pills { flex-wrap: wrap; }
:root body .ai-agent-editor .el-tpl-right-topbar { gap: 8px; flex-wrap: wrap; }
@media (max-width: 1180px) {
  :root body .ai-agent-editor #elStudioPreviewDock { border-left: 0; border-top: 1px solid transparent; }
  :root body .ai-agent-editor .el-tpl-modal-dialog { display: flex; flex-direction: column; overflow-y: auto; }
  :root body .ai-agent-editor .el-tpl-modal-left { flex: 0 0 auto; overflow: visible; border-right: 0; }
  :root body .ai-agent-editor .el-tpl-modal-right { display: contents; }
  :root body .ai-agent-editor .el-tpl-right-topbar {
    order: -1;
    position: sticky;
    top: 0;
    z-index: 3;
    flex: 0 0 auto;
    margin: 0;
    padding: 12px;
    background: var(--dt-surface);
    border-bottom: 1px solid transparent;
  }
  :root body .ai-agent-editor .el-tpl-cards-grid { flex: none; overflow: visible; }
  :root body .ai-agent-editor .el-tpl-right-canvas { flex-shrink: 0; min-height: 360px; }
}
@media (max-width: 560px) {
  :root body .ai-agent-editor .ai-header-actions { width: 100%; }
  :root body .ai-agent-editor .el-tpl-cards-grid { grid-template-columns: minmax(0, 1fr); }
  :root body :is(.aag, .ai-agent-editor) .aag-browse-right__bar { padding: 12px; }
  :root body :is(.aag, .ai-agent-editor) .aag-prv-stage { padding: 16px 12px; }
  :root body :is(.aag, .ai-agent-editor) .aag-wf-stage { padding: 12px 8px; }
  :root body :is(.aag, .ai-agent-editor) .aag-wf-card-node { padding: 10px; }
}
