@use '../variables' as v;
@use '../mixins' as m;

// ── Automation funnel builder ─────────────────────────────────────────

.af-main {
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

// ── Top bar ──

.af-topbar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  border-bottom: 1px solid var(--border-color);
  background: var(--bg-surface);
  gap: 12px;
  z-index: 10;
  flex-wrap: wrap;

  @media (max-width: 600px) {
    padding: 8px 12px;
    gap: 8px;
  }
}

.af-topbar-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1;
}

.af-topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
  flex-wrap: wrap;
}

.af-breadcrumb-link {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  text-decoration: none;

  @media (max-width: 480px) {
    display: none;
  }

  &:hover {
    color: var(--info, #{v.$info});
  }
}

.af-breadcrumb-sep {
  color: var(--text-muted);
  font-size: 13px;

  @media (max-width: 480px) {
    display: none;
  }
}

.af-funnel-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
}

.af-draft-badge {
  font-size: 12px;
  font-weight: 700;
  color: #dc2626;
  padding: 1px 0;
}

.af-name-input {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
  border: 1px solid v.$info;
  border-radius: 4px;
  padding: 2px 8px;
  outline: none;
  font-family: inherit;
  background: var(--bg-surface);
}

.af-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background: var(--bg-surface);
  cursor: pointer;
  color: var(--text-secondary);
  transition: background .12s, color .12s;

  svg {
    width: 15px;
    height: 15px;
  }

  &:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
  }
}

.af-flag-btn {
  border-color: transparent;
  background: none;
}

// ── Toggle switches ──

.af-toggle-group,
.af-status-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

.af-toggle-label {
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 500;

  @media (max-width: 480px) {
    display: none;
  }
}

.af-toggle-switch {
  position: relative;
  display: inline-block;
  width: 36px;
  height: 20px;
  cursor: pointer;

  input {
    display: none;
  }
}

.af-toggle-thumb {
  position: absolute;
  inset: 0;
  background: var(--bg-hover-strong, #{v.$gray-200});
  border-radius: 10px;
  transition: background .2s;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, .15), 0 2px 4px rgba(0, 0, 0, .15);

  &::after {
    content: '';
    position: absolute;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--bg-surface, #fff);
    top: 2px;
    left: 2px;
    transition: transform .2s;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, .15), 0 2px 4px rgba(0, 0, 0, .15);
  }
}

.af-toggle-switch input:checked+.af-toggle-thumb {
  background: v.$info;

  &::after {
    transform: translateX(16px);
  }
}

// ── Workspace ──

.af-workspace {
  flex: 1;
  min-height: 0;
  display: flex;
  overflow: hidden;
}

// ── Canvas ──

.af-canvas {
  flex: 1;
  min-width: 0;
  overflow: auto;
  background-color: var(--bg-page);
  background-image: radial-gradient(circle, var(--border-color) 1px, transparent 1px);
  background-size: 22px 22px;
  padding: 40px 60px 80px;
  position: relative;
  @include m.scrollbar-thin;

  &.is-fullscreen {
    position: fixed !important;
    inset: 0 !important;
    z-index: 99999 !important;
    width: 100vw !important;
    height: 100vh !important;
    max-height: 100vh !important;
    border-radius: 0 !important;
  }
}

// Floating Zoom & Fullscreen Controls
.af-canvas-controls {
  position: sticky;
  bottom: -28px;
  left: 100%;

  transform: translateX(-100%);
  margin-top: -48px;
  z-index: 100;
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 4px 6px;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  user-select: none;
}

.af-canvas-control-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 28px;
  min-width: 28px;
  padding: 0 6px;
  border-radius: 6px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.12s ease;

  &:hover:not(:disabled) {
    background: var(--bg-hover);
    color: var(--text-primary);
  }

  &:disabled {
    opacity: 0.4;
    cursor: not-allowed;
  }

  &.is-label {
    font-size: 11px;
    font-weight: 700;
    min-width: 44px;
    padding: 0 4px;
    color: var(--text-primary);
  }
}

.af-canvas-control-divider {
  width: 1px;
  height: 16px;
  background: var(--border-color);
  margin: 0 4px;
}

.af-flow {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 700px;
}

// ── Connector (vertical line + add button) ──

.af-connector {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.af-connector-line {
  width: 2px;
  height: 24px;
  background: var(--border-color);
}

.af-add-circle {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 2px solid var(--text-muted);
  background: var(--bg-surface);
  cursor: pointer;
  font-size: 18px;
  font-weight: 300;
  line-height: 1;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color .15s, color .15s, background .15s;
  position: relative;
  z-index: 2;

  &:hover {
    border-color: var(--info, #{v.$info});
    color: var(--info, #{v.$info});
    background: var(--info-light, #{v.$info-light});
  }
}

// ── Flow nodes (n8n Style Aesthetic) ──

.af-node,
.af-node-card {
  position: relative;
  width: 310px;
  background: var(--bg-surface);
  padding: 14px 16px;
  border-radius: 14px;
  border: 1px solid var(--border-color);
  box-shadow: 0 4px 16px -2px rgba(15, 23, 42, 0.07), 0 2px 4px -1px rgba(15, 23, 42, 0.04);
  cursor: pointer;
  text-align: left;
  transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);

  @media (max-width: 480px) {
    width: min(310px, calc(100vw - 48px));
  }

  &:hover {
    box-shadow: 0 10px 25px -4px rgba(15, 23, 42, 0.12), 0 4px 10px -2px rgba(15, 23, 42, 0.05);
    border-color: var(--border-color);
    transform: translateY(-2px);
  }

  &.is-selected {
    border-color: var(--info, #3b82f6) !important;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.18), 0 10px 25px -4px rgba(15, 23, 42, 0.12) !important;
  }

  &.has-active-tooltip {
    z-index: 99 !important;
    overflow: visible !important;
  }
}

.af-node-hdr {
  display: flex;
  align-items: center;
  gap: 12px;
}

.af-node-icon-badge {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  color: #ffffff;
  flex-shrink: 0;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);

  svg {
    width: 20px;
    height: 20px;
  }

  &.af-icon--trigger {
    background: linear-gradient(135deg, #f97316, #ea580c);
  }

  &.af-icon--email {
    background: linear-gradient(135deg, #3b82f6, #2563eb);
  }

  &.af-icon--campaign_email {
    background: linear-gradient(135deg, #0284c7, #0369a1);
  }

  &.af-icon--webhook {
    background: linear-gradient(135deg, #10b981, #059669);
  }

  &.af-icon--ai_lead_score {
    background: linear-gradient(135deg, #ec4899, #db2777);
  }

  &.af-icon--incoming_webhook {
    background: linear-gradient(135deg, #ec4899, #db2777);
  }

  &.af-icon--wait {
    background: linear-gradient(135deg, #f59e0b, #d97706);
  }

  &.af-icon--condition,
  &.af-icon--ab_split {
    background: linear-gradient(135deg, #8b5cf6, #7c3aed);
  }

  &.af-icon--apply_tag,
  &.af-icon--remove_tag,
  &.af-icon--update_field {
    background: linear-gradient(135deg, #06b6d4, #0e7490);
  }

  &.af-icon--apply_list {
    background: linear-gradient(135deg, #10b981, #059669);
  }

  &.af-icon--remove_list {
    background: linear-gradient(135deg, #ef4444, #dc2626);
  }

  &.af-icon--end {
    background: linear-gradient(135deg, #64748b, #475569);
  }
}

.af-node-title-wrap {
  flex: 1;
  min-width: 0;
}

.af-node-title-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 3px;
}

.af-node-title {
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.af-node-cat-row {
  display: flex;
  align-items: center;
}

.af-node-cat-badge {
  display: inline-block;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 2px 7px;
  border-radius: 4px;

  &.af-cat--trigger {
    background: #fff7ed;
    color: #ea580c;
    border: 1px solid #ffedd5;
  }

  &.af-cat--action {
    background: #eff6ff;
    color: #2563eb;
    border: 1px solid #dbeafe;
  }

  &.af-cat--logic {
    background: #f5f3ff;
    color: #7c3aed;
    border: 1px solid #ede9fe;
  }

  &.af-cat--ai {
    background: #fdf2f8;
    color: #db2777;
    border: 1px solid #fce7f3;
  }

  &.af-cat--end {
    background: #f1f5f9;
    color: #475569;
    border: 1px solid #e2e8f0;
  }
}

.af-node-actions {
  display: flex;
  align-items: center;
}

.af-node-del-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 6px;
  border: 1px solid transparent;
  background: transparent;
  color: #94a3b8;
  font-size: 18px;
  cursor: pointer;
  transition: all 0.15s;

  &:hover {
    background: #fee2e2;
    color: #dc2626;
    border-color: #fca5a5;
  }
}

.af-node-status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #10b981;
  box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.2);
}

.af-node-sub-box {
  background: var(--bg-hover);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 8px 10px;
  margin-top: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.af-node-sub-text {
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 500;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

// ── Branch layout ──

.af-branch-root {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
}

.af-branch-hline {
  width: 60%;
  height: 2px;
  background: var(--border-color);
  position: relative;
  margin-top: 0;

  @media (max-width: 640px) {
    display: none;
  }
}

.af-branch-cols {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 32px;
  width: 100%;
  max-width: 800px;

  @media (max-width: 640px) {
    grid-template-columns: 1fr;
    gap: 16px;
  }
}

.af-branch-col {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.af-branch-vline-top {
  width: 2px;
  height: 20px;
  background: var(--border-color);
}

.af-branch-vline {
  width: 2px;
  height: 16px;
  background: var(--border-color);
}

.af-branch-pill {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  z-index: 2;

  &--no {
    border: 2px solid var(--danger, #f87171);
    color: var(--danger, #ef4444);
    background: var(--bg-surface);
  }

  &--yes {
    border: 2px solid var(--text-secondary);
    color: var(--text-primary);
    background: var(--bg-surface);
  }
}

.af-branch-box {
  width: 100%;
  border-radius: 10px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;

  &--no {
    border: 1.5px solid var(--danger-border, #fca5a5);
    background: var(--danger-light, rgba(255, 241, 242, .3));
  }

  &--yes {
    border: 1.5px solid var(--border-color);
    background: var(--bg-hover);
  }
}

.af-branch-inner-line {
  width: 2px;
  height: 14px;
  background: var(--border-color);
  flex-shrink: 0;
}

// ── Add Action button (in branches) ──

.af-add-action-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  padding: 10px 16px;
  border: 1.5px dashed v.$border-color;
  border-radius: 6px;
  background: rgba(255, 255, 255, .7);
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
  color: v.$info;
  justify-content: center;
  transition: background .12s, border-color .12s;

  svg {
    width: 12px;
    height: 12px;
  }

  &:hover {
    background: v.$info-light;
    border-color: v.$info;
  }
}

// ── Right panel ──

.af-panel {
  display: flex !important;
  width: 400px;
  flex-shrink: 0;
  border-left: 1px solid var(--border-color);
  background: var(--bg-surface);
  flex-direction: column;
  overflow: hidden;

  &.is-email-panel {
    width: 450px;
  }

  &.is-webhook-panel {
    width: 460px;
  }

  @media (max-width: 900px) {
    width: 100% !important;
    border-left: none;
    border-top: 1px solid var(--border-color);
    max-height: 55vh;
  }
}

.af-panel-content {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
  width: 400px;
  flex-shrink: 0;

  .is-email-panel & {
    width: 450px;
  }

  .is-webhook-panel & {
    width: 460px;
  }

  @media (max-width: 900px) {
    width: 100% !important;
  }
}

/* Vue Transition: slide-panel */
.slide-panel-enter-active,
.slide-panel-leave-active {
  transition: width 0.28s cubic-bezier(0.16, 1, 0.3, 1), border-left-color 0.28s ease;
}

.slide-panel-enter-from,
.slide-panel-leave-to {
  width: 0 !important;
  border-left-color: transparent !important;
}

.slide-panel-enter-to,
.slide-panel-leave-from {
  /* Inherited from base rules */
}

.af-panel-hdr {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 18px 12px;
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

.af-panel-title {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  line-height: 1.3;
}

.af-panel-badge {
  display: inline-flex;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;

  &--trigger {
    background: rgba(124, 58, 237, 0.12);
    color: #7c3aed;
  }

  &--action {
    background: var(--info-light, #dbeafe);
    color: var(--info, #{v.$info});
  }

  &--condition {
    background: var(--danger-light, #fee2e2);
    color: var(--danger, #dc2626);
  }
}

.af-panel-desc {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 4px;
  line-height: 1.4;
}

.af-panel-close {
  flex-shrink: 0;
  font-size: 18px;
  line-height: 1;
  color: var(--text-muted);
  background: none;
  border: none;
  cursor: pointer;
  padding: 0 2px;

  &:hover {
    color: var(--text-primary);
  }
}

.af-panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  @include m.scrollbar-thin;
}

.af-panel-footer {
  flex-shrink: 0;
  padding: 12px 18px;
  border-top: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg-hover);
}

// ── Panel form elements ──

.af-form-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.af-form-row-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.af-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
}

.af-input {
  width: 100%;
  padding: 8px 10px;
  height: 36px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  font-size: 13px;
  color: var(--text-primary);
  background: var(--bg-surface);
  font-family: inherit;
  outline: none;

}

.af-textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border-color);
  border-radius: v.$radius;
  font-size: 13px;
  color: var(--text-primary);
  background: var(--bg-surface);
  font-family: inherit;
  outline: none;
  resize: vertical;

}

.af-select {
  width: 100%;
  padding: 8px 30px 8px 10px;
  border: 1px solid var(--border-color);
  border-radius: v.$radius;
  font-size: 13px;
  color: var(--text-primary);
  font-family: inherit;
  outline: none;
  background-color: var(--bg-surface);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke-width='2.5' stroke='%234b5563'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M19.5 8.25l-7.5 7.5-7.5-7.5'%3E%3C/path%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 14px;
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  @include m.focus-ring;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, .12), 0 2px 4px rgba(0, 0, 0, .12);
}

.af-settings-card {
  border: 1px solid var(--border-color);
  border-radius: v.$radius-md;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--bg-surface);
}

.af-settings-card-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 2px;
}

.af-checkbox-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  font-size: 12px;
  color: var(--text-secondary);
  cursor: pointer;

  input {
    margin-top: 2px;
    flex-shrink: 0;
  }
}

.af-radio-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.af-radio {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.af-radio-label {
  font-size: 13px;
  color: var(--text-secondary);

  &--active {
    color: var(--info, #{v.$info});
    font-weight: 600;
  }
}

.af-condition-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;

  .af-select {
    flex: 1;
    min-width: 80px;
  }

  .af-input {
    flex: 0 0 80px;
  }
}

.af-add-condition-btn {
  font-size: 12px;
  color: v.$info;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  font-weight: 600;
  text-align: left;

  &:hover {
    text-decoration: underline;
  }
}

.af-delete-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: 1px solid #fee2e2;
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
  color: #ef4444;
  margin-left: auto;

  &:hover {
    background: #fee2e2;
  }
}

// ── Tag selector ──

.af-tag-selector {
  display: flex;
  gap: 6px;
  align-items: stretch;
}

.af-tag-selected {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 6px 8px;
  border: 1px solid var(--border-color);
  border-radius: v.$radius;
  min-height: 38px;
  align-items: center;
  background: var(--bg-surface);
}

.af-tag-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  background: var(--bg-hover-strong, #{v.$gray-200});
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);

  button {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    font-size: 12px;
    padding: 0;
    line-height: 1;
  }
}

.af-tag-input {
  border: none;
  outline: none;
  font-size: 12px;
  color: var(--text-primary);
  background: transparent;
  flex: 1;
  min-width: 80px;
  font-family: inherit;
}

.af-tag-add-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  border: 1px solid var(--border-color);
  border-radius: v.$radius;
  background: var(--text-secondary, #{v.$gray-600});
  color: var(--bg-surface, #fff);
  font-size: 20px;
  font-weight: 300;
  cursor: pointer;

  &:hover {
    background: var(--text-primary, #{v.$gray-800});
  }
}

// ── Add Action panel (block picker) ──

.af-add-search-wrap {
  padding: 12px 18px;
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

.af-add-search {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border-color);
  border-radius: v.$radius;
  font-size: 13px;
  color: var(--text-primary);
  background: var(--bg-surface);
  font-family: inherit;
  outline: none;
  @include m.focus-ring;
}

.af-add-tabs {
  display: flex;
  border-bottom: 2px solid var(--border-color);
  flex-shrink: 0;
}

.af-add-tab {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 10px 8px;
  border: none;
  background: none;
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: color .12s;

  svg {
    width: 13px;
    height: 13px;
  }

  &:hover {
    color: var(--text-primary);
  }

  &.is-active {
    color: var(--info, #{v.$info});
    border-bottom-color: var(--info, #{v.$info});
    font-weight: 600;
  }
}

.af-add-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 18px;
  @include m.scrollbar-thin;
}

.af-add-section-label {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 6px;
}

.af-add-section-desc {
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.4;
  margin-bottom: 16px;
}

.af-add-group-label {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  color: var(--text-secondary);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 2px 8px;
  margin-bottom: 12px;
}

.af-block-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 20px;
  padding: 14px;
  border: 1px solid var(--border-color);
  border-radius: 8px;

  @media (max-width: 400px) {
    grid-template-columns: 1fr;
  }

  &--dark {
    background: var(--bg-hover);
    border-color: var(--border-color);
  }
}

.af-block-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 16px 12px;
  border: 1.5px solid var(--border-color);
  border-radius: 8px;
  background: var(--bg-surface);
  cursor: pointer;
  transition: border-color .12s, box-shadow .12s;

  &:hover {
    border-color: var(--info, #{v.$info});
    box-shadow: 0 2px 8px rgba(37, 99, 235, .1);
  }

  &--active {
    border-color: var(--info, #{v.$info});
    box-shadow: 0 0 0 3px rgba(37, 99, 235, .12);
  }

  &--goal {
    background: var(--warning-light, #fef9c3);
    border-color: var(--warning-border, #f5e05a);

    &:hover {
      border-color: var(--warning, #d97706);
    }
  }

  &--cond {
    background: var(--danger-light, #fee2e2);
    border-color: var(--danger-border, #fca5a5);

    &:hover {
      border-color: var(--danger, #dc2626);
    }
  }

  &--white {
    background: var(--bg-surface);
  }
}

.af-block-icon {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--bg-hover);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 10px;
  color: var(--text-secondary);

  svg {
    width: 16px;
    height: 16px;
  }
}

.af-block-name {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 5px;
  line-height: 1.2;
}

.af-block-desc {
  font-size: 11px;
  color: var(--text-secondary);
  line-height: 1.4;
}

// ── Add step popup (floating) ──

.af-add-popup {
  position: fixed;
  z-index: 9999;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: v.$radius-md;
  box-shadow: v.$shadow;
  padding: 6px;
  min-width: 280px;
  max-width: calc(100vw - 24px);

  @media (max-width: 480px) {
    left: 12px !important;
    right: 12px;
    width: auto;
    min-width: 0;
  }
}

.af-add-popup-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  width: 100%;
  padding: 12px 14px;
  border: none;
  border-radius: v.$radius;
  background: none;
  cursor: pointer;
  text-align: left;
  transition: background .1s;

  &:hover {
    background: var(--bg-hover);
  }
}

.af-add-popup-icon {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--bg-hover);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);

  svg {
    width: 15px;
    height: 15px;
  }
}

.af-add-popup-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 2px;
}

.af-add-popup-desc {
  font-size: 11px;
  color: var(--text-secondary);
  line-height: 1.4;
}

// ── Automation list page: create funnel modal ──

.af-template-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
  padding: 4px 0;

  @include m.max-md {
    grid-template-columns: repeat(2, 1fr);
  }
}

.af-template-card {
  border: 1.5px solid var(--border-color);
  border-radius: 10px;
  padding: 20px 16px;
  cursor: pointer;
  text-align: center;
  transition: border-color .15s, box-shadow .15s;
  background: var(--bg-surface);

  &:hover {
    border-color: var(--info, #{v.$info});
    box-shadow: 0 4px 12px rgba(37, 99, 235, .1);
  }

  &--scratch {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border-style: dashed;
  }
}

.af-template-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1.5px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 12px;
  color: var(--text-muted);
  font-size: 18px;
  font-weight: 300;

  svg {
    width: 18px;
    height: 18px;
  }
}

.af-template-name {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 6px;
  line-height: 1.3;
}

.af-template-desc {
  font-size: 11px;
  color: var(--text-secondary);
  line-height: 1.4;
}

// ── Draw.io Free-Form Drag & Drop Canvas ─────────────────────────────

.af-view-switcher {
  display: flex;
  align-items: center;
  background: v.$gray-100;
  border-radius: 8px;
  padding: 3px;
  gap: 2px;




  .af-switch-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 5px 12px;
    font-size: 12px;
    font-weight: 600;
    color: v.$gray-600;
    background: transparent;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    transition: all .15s ease;

    svg {
      width: 14px;
      height: 14px;
    }

    &.is-active {
      background: #ffffff;
      color: v.$info;
      box-shadow: 0 1px 3px rgba(0, 0, 0, .08);

    }

    &:hover:not(.is-active) {
      color: v.$gray-900;
    }
  }
}

// Left sidebar block palette
.af-drawio-palette {
  width: 240px;
  background: var(--bg-surface);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  z-index: 20;

  @media (max-width: 900px) {
    width: 100%;
    border-right: none;
    border-bottom: 1px solid var(--border-color);
    max-height: 180px;
  }

  @media (max-width: 640px) {
    display: none;

    &.is-open {
      display: flex;
      position: fixed;
      bottom: 0;
      left: 0;
      right: 0;
      z-index: 200;
      max-height: 60vh;
      border-radius: 16px 16px 0 0;
      box-shadow: 0 -4px 24px rgba(0, 0, 0, .15);
    }
  }

  .af-palette-hdr {
    padding: 14px 16px;
    border-bottom: 1px solid var(--border-color);

    .af-palette-title {
      font-size: 13px;
      font-weight: 700;
      color: var(--text-primary);
    }

    .af-palette-sub {
      font-size: 11px;
      color: var(--text-secondary);
      margin-top: 2px;
    }
  }

  .af-palette-body {
    padding: 12px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 8px;
    flex: 1;
    min-height: 0;
    @include m.scrollbar-thin;

    @media (max-width: 900px) {
      flex-direction: row;
      overflow-x: auto;
      overflow-y: hidden;
      flex-wrap: nowrap;
      flex: none;
    }
  }

  .af-palette-card {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    background: var(--bg-hover);
    border: 1px dashed var(--border-color);
    border-radius: 8px;
    cursor: grab;
    transition: all .15s ease;
    flex-shrink: 0;

    &:hover {
      background: var(--bg-surface);
      border-color: var(--info, #{v.$info});
      box-shadow: 0 2px 6px rgba(0, 0, 0, .06);
      transform: translateY(-1px);
    }

    &:active {
      cursor: grabbing;
    }

    .af-palette-icon {
      width: 32px;
      height: 32px;
      border-radius: 8px;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-shrink: 0;

      svg {
        width: 16px;
        height: 16px;
      }
    }

    .af-palette-info {
      min-width: 0;

      .af-palette-name {
        font-size: 12px;
        font-weight: 600;
        color: var(--text-primary);
      }

      .af-palette-desc {
        font-size: 10px;
        color: var(--text-secondary);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }
}

// 2D Canvas Board
.af-drawio-board {
  position: relative;
  width: 3500px;
  height: 3500px;
}

// Absolute SVG connection line layer
.af-drawio-svg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;

  .af-line-path {
    fill: none;
    stroke: #94a3b8;
    stroke-width: 2.5px;
    transition: stroke .15s ease;
    pointer-events: auto;
    cursor: pointer;

    &:hover {
      stroke: v.$info;
      stroke-width: 3.5px;
    }
  }

  .af-line-preview {
    fill: none;
    stroke: v.$info;
    stroke-width: 2.5px;
    stroke-dasharray: 6, 4;
  }
}

.af-line-del-btn {
  position: absolute;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #ffffff;
  border: 1.5px solid #ef4444;
  color: #ef4444;
  font-size: 13px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 15;
  transform: translate(-50%, -50%);
  box-shadow: 0 2px 5px rgba(0, 0, 0, .15);
  transition: opacity .15s ease, transform .15s ease, background .15s ease;
  opacity: 0;
  pointer-events: none;

  &.is-visible,
  &:hover {
    opacity: 1;
    pointer-events: auto;
  }

  &:hover {
    background: #ef4444;
    color: #ffffff;
    transform: translate(-50%, -50%) scale(1.15);
  }
}

.af-line-path-hover-helper {
  fill: none;
  stroke: transparent;
  stroke-width: 14px;
  cursor: pointer;
}

// Draggable 2D Node Card on Canvas
.af-drawio-node {
  position: absolute;
  width: 290px;
  background: var(--bg-surface);
  border: 1.5px solid var(--border-color);
  border-radius: 14px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, .06);
  z-index: 10;
  transition: border-color .15s, box-shadow .15s;
  user-select: none;

  &:hover {
    border-color: var(--border-color);
    box-shadow: 0 6px 18px rgba(0, 0, 0, .1);
  }

  &.is-selected {
    border-color: var(--info, #{v.$info});
    box-shadow: 0 0 0 3px rgba(37, 99, 235, .18), 0 6px 18px rgba(0, 0, 0, .1);
  }

  // Header handle for dragging node
  .af-drawio-node-hdr {
    padding: 12px 14px;
    display: flex;
    align-items: center;
    gap: 12px;
    cursor: grab;
    border-bottom: 1px solid var(--border-color);

    &:active {
      cursor: grabbing;
    }
  }

  .af-drawio-node-sub {
    padding: 8px 14px;
    background: var(--bg-hover);
    border-bottom-left-radius: 13px;
    border-bottom-right-radius: 13px;
    font-size: 11px;
    color: var(--text-secondary);
  }

  &.has-active-tooltip {
    z-index: 99 !important;
    overflow: visible !important;
  }
}

// Connectable Anchor Ports (Hidden by default, visible on hover or during connection)
.af-port {
  position: absolute;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #ffffff;
  border: 2.5px solid v.$info;
  cursor: crosshair;
  z-index: 12;
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s ease, transform .15s ease, background .15s ease;

  &:hover {
    transform: scale(1.4);
    background: v.$info;
  }

  &.af-port--top {
    top: -7px;
    left: 50%;
    transform: translateX(-50%);
    border-color: #3b82f6;

    &:hover {
      background: #3b82f6;
      transform: translateX(-50%) scale(1.4);
    }
  }

  &.af-port--bottom {
    bottom: -7px;
    left: 50%;
    transform: translateX(-50%);
    border-color: #10b981;

    &:hover {
      background: #10b981;
      transform: translateX(-50%) scale(1.4);
    }
  }

  &.af-port--left {
    left: -7px;
    top: 50%;
    transform: translateY(-50%);
    border-color: #8b5cf6;

    &:hover {
      background: #8b5cf6;
      transform: translateY(-50%) scale(1.4);
    }
  }

  &.af-port--right {
    right: -7px;
    top: 50%;
    transform: translateY(-50%);
    border-color: #f59e0b;

    &:hover {
      background: #f59e0b;
      transform: translateY(-50%) scale(1.4);
    }
  }
}

// Show connected ports always; show unconnected ports only when hovering over step node or actively dragging a line
.af-port.is-connected,
.af-drawio-node:hover .af-port,
.af-drawio-board.is-drawing-line .af-port {
  opacity: 1;
  pointer-events: auto;
}

.af-connecting-banner {
  position: sticky;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 50;
  display: inline-flex;
  align-items: center;
  background: var(--text-primary, #1e293b);
  color: var(--bg-surface, #ffffff);
  padding: 8px 16px;
  border-radius: 9999px;
  font-size: 12px;
  font-weight: 600;
  box-shadow: 0 4px 12px rgba(0, 0, 0, .2);
  pointer-events: none;
}



.af-drawio-node.is-connect-target {
  border-color: #3b82f6 !important;
  box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.25), 0 8px 20px rgba(0, 0, 0, 0.12) !important;
  transform: scale(1.02);
}

// Canvas Panning (Spacebar or Middle Click / Drag)
.af-canvas.is-space-pressed,
.af-drawio-board.is-space-pressed {
  cursor: grab !important;

  * {
    cursor: grab !important;
  }
}

.af-canvas.is-panning,
.af-drawio-board.is-panning {
  cursor: grabbing !important;

  * {
    cursor: grabbing !important;
  }
}

// ── Node Status Footer & Tooltip ──

.af-node-status-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 14px;
  background: transparent;
  border-top: 1px solid var(--border-color);
  border-bottom-left-radius: 13px;
  border-bottom-right-radius: 13px;
}

// Only flatten bottom corners of the sub-box if there is a status footer below it
.af-drawio-node-sub:not(:last-child),
.af-node-sub-box:not(:last-child) {
  border-bottom-left-radius: 0px !important;
  border-bottom-right-radius: 0px !important;
}

.af-status-indicators {
  display: flex;
  align-items: center;
  gap: 8px;
}

.af-status-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 4px;

  &.is-processing {
    background: var(--info-light, #f0f9ff);
    color: var(--info, #0284c7);
    border: 1px solid var(--info-border, #e0f2fe);

    svg {
      animation: spin 1.5s linear infinite;
    }
  }

  &.is-success {
    background: var(--success-light, #f0fdf4);
    color: var(--success, #16a34a);
    border: 1px solid var(--success-border, #dcfce7);
  }

  &.is-error {
    background: var(--danger-light, #fef2f2);
    color: var(--danger, #dc2626);
    border: 1px solid var(--danger-border, #fee2e2);
  }
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }

  to {
    transform: rotate(360deg);
  }
}

.af-tooltip-wrapper {
  position: relative;
  display: inline-block;
}

.af-info-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  color: #64748b;
  cursor: pointer;
  transition: all 0.15s ease;

  &:hover {
    border-color: #3b82f6;
    color: #3b82f6;
    transform: scale(1.05);
  }
}

.af-tooltip-panel {
  position: absolute;
  bottom: 125%;
  right: -5px;
  width: 320px;
  background: #1e293b;
  border: 1px solid #334155;
  color: #ffffff;
  padding: 12px;
  border-radius: 10px;
  box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.3), 0 8px 10px -6px rgba(0, 0, 0, 0.3);
  z-index: 100;
  text-align: left;
  pointer-events: auto;

  &::after {
    content: '';
    position: absolute;
    top: 100%;
    right: 12px;
    border-width: 6px;
    border-style: solid;
    border-color: #1e293b transparent transparent transparent;
  }
}

.af-tooltip-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: #94a3b8;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s ease;

  &:hover {
    background: #334155;
    color: #ffffff;
  }
}

.af-tooltip-title {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #94a3b8;
}

.af-tooltip-stats {
  display: flex;
  gap: 8px;
  font-size: 11px;
  font-weight: 600;
  margin-bottom: 10px;

  .stat-item {
    padding: 1px 6px;
    border-radius: 3px;

    &.is-success {
      background: #064e3b;
      color: #34d399;
    }

    &.is-processing {
      background: #0c4a6e;
      color: #38bdf8;
    }

    &.is-error {
      background: #7f1d1d;
      color: #f87171;
    }
  }
}

.af-tooltip-logs {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 150px;
  overflow-y: auto;
  padding-right: 6px;

  /* Premium Subtle Dark Scrollbar */
  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-track {
    background: #1e293b;
    border-radius: 3px;
  }

  &::-webkit-scrollbar-thumb {
    background: #475569;
    border-radius: 3px;

    &:hover {
      background: #64748b;
    }
  }
}

.af-tooltip-logs {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 150px;
  overflow-y: auto;
  padding-right: 4px;
}

.af-tooltip-log-item {
  font-size: 11px;
  padding: 6px;
  border-radius: 4px;
  background: #334155;
  line-height: 1.3;

  .log-header {
    display: flex;
    justify-content: space-between;
    font-weight: 600;
    margin-bottom: 2px;
    gap: 8px;
  }

  .log-email {
    color: #e2e8f0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
  }

  .log-time {
    color: #94a3b8;
    flex-shrink: 0;
  }

  .log-body {
    color: #cbd5e1;
    word-break: break-word;
  }

  &.is-success {
    border: 1px solid #10b981;
  }

  &.is-processing {
    border: 1px solid #0284c7;
  }

  &.is-error {
    border: 1px solid #ef4444;
  }
}

.af-tooltip-empty {
  font-size: 11px;
  color: #64748b;
  text-align: center;
  padding: 8px 0;
}

// ── Topbar Run & Restart Buttons ──

.af-control-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  background: var(--bg-surface);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s ease;

  &:hover {
    background: var(--bg-hover);
    border-color: var(--text-muted);
  }

  &.is-run {
    color: var(--success, #16a34a);

    &.is-active {
      color: var(--success, #16a34a);
      background: var(--success-light, #f0fdf4);
      border-color: var(--success-border, #86efac);
      animation: runPulse 2s infinite;
    }

    &:hover {
      background: var(--success-light, #f0fdf4);
      border-color: var(--success-border, #86efac);
      transform: scale(1.05);
    }
  }

  &.is-restart {
    color: #ea580c;

    &:hover {
      background: rgba(234, 88, 12, 0.08);
      border-color: #fdba74;
      transform: scale(1.05);
    }
  }

  &.is-report {
    color: #0f766e;

    &:hover {
      background: rgba(15, 118, 110, 0.08);
      border-color: #99f6e4;
      transform: scale(1.05);
    }
  }
}

@keyframes runPulse {
  0% {
    box-shadow: 0 0 0 0 rgba(22, 163, 74, 0.4);
  }

  70% {
    box-shadow: 0 0 0 6px rgba(22, 163, 74, 0);
  }

  100% {
    box-shadow: 0 0 0 0 rgba(22, 163, 74, 0);
  }
}

.af-panel-delete-btn {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 6px;
  border: none;
  background: transparent;
  color: var(--danger, #ef4444);
  cursor: pointer;
  transition: all 0.15s ease;

  svg {
    width: 18px;
    height: 18px;
  }

  &:hover {
    background: var(--danger-light, #fee2e2);
    color: var(--danger, #dc2626);
  }
}

// ── Responsive: Workspace layout ──────────────────────────────────────────────

@media (max-width: 900px) {
  .af-workspace {
    flex-direction: column;
  }

  .af-canvas {
    min-height: 40vh;
  }
}

@media (max-width: 640px) {
  .af-flow {
    min-width: 100%;
    padding: 0 8px;
  }

  .af-canvas {
    padding: 20px 12px 60px;
  }

  .af-form-row-2 {
    grid-template-columns: 1fr;
  }

  .af-topbar {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    padding: 8px 12px;

    &-left,
    &-right {
      width: 100%;
      justify-content: space-between;
      flex-wrap: wrap;
      gap: 6px;
    }
  }

  .af-funnel-name {
    max-width: 160px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .af-view-switcher {
    flex: 1;
    display: flex;
    margin-right: 0 !important;

    .af-switch-btn {
      flex: 1;
      justify-content: center;
      padding: 5px 8px;
      font-size: 11px;
    }
  }
}

// ── Palette mobile toggle button ────────────────────────────────────────────

.af-palette-toggle-btn {
  display: none;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background: var(--bg-surface);
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  cursor: pointer;

  @media (max-width: 640px) {
    display: inline-flex;
  }

  svg {
    width: 14px;
    height: 14px;
  }
}