/* ═══════════════════════════════════════════════════════════════════
   Workflow Builder — all canvas, step, connector, and drag-drop styles
   ═══════════════════════════════════════════════════════════════════ */

/* Let .canvas-scroll take all the scrolling — the min-width:0 chain stops
   the canvas's intrinsic width from pushing through to .wf-main, so we
   don't need to clip horizontal overflow on .wf-main (which on some
   browsers silently disables scrollLeft on inner scroll containers). */
.wf-main:has(.canvas-main-area) > div:first-child {
    min-width: 0;
}

/* The canvas page lays out as a flex column inside .wf-main so the
   scroll area is bounded by the viewport. min-width: 0 is the critical
   pair to min-height: 0 — without it, flex children default to
   min-width: auto and refuse to shrink below their content's intrinsic
   width. */
.canvas-main-area {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    min-width: 0;
}

.canvas-scroll {
    flex: 1;
    min-height: 0;
    min-width: 0;
    /* Always-visible scrollbars — the canvas is the only scroll surface on
       this page, and a permanent gutter avoids the "scrollbar hides below
       the viewport" problem when content is tall AND wide. */
    overflow: scroll;
    padding-bottom: 1rem;
    /* `safe center` centers the canvas while it fits, but snaps to the
       start edge (left/top) the moment content overflows — so scrollLeft
       can engage normally without the centering math fighting it. */
    display: flex;
    justify-content: safe center;
    align-items: safe start;
}

/* Temporary debug HUD — corner panel that prints live scroll metrics so we
   can diagnose why horizontal scrolling isn't engaging. Remove once fixed. */
.wf-zoom-debug {
    position: fixed;
    bottom: 8px;
    right: 8px;
    z-index: 99999;
    padding: 6px 10px;
    background: rgba(0, 0, 0, 0.78);
    color: #fff;
    font-family: ui-monospace, Menlo, Consolas, monospace;
    font-size: 11px;
    line-height: 1.4;
    border-radius: 4px;
    pointer-events: none;
    white-space: pre;
}

.wf-canvas {
    padding: 1rem 2rem 2rem;
    /* No `margin: 0 auto` — auto-centering changes the canvas's left
       position implicitly when crossing the fits-container threshold,
       which fights our scroll-based zoom-to-cursor math. Canvas anchored
       at the scroll origin; smaller canvases just look left-justified. */
    margin: 0;
    /* zoom is layout-affecting (unlike transform: scale), so the wrapper's
       scrollbars track the scaled canvas naturally. Wired to the toolbar
       slider and to shift+wheel via JSCanvasZoomService. */
    transform-origin: top left;
}

/* Toolbar zoom control */
.wf-zoom {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.8rem;
    color: var(--theme-text-secondary);
    user-select: none;
}

.wf-zoom input[type="range"] {
    width: 120px;
    accent-color: var(--theme-primary);
}

.wf-zoom-value {
    min-width: 3em;
    text-align: right;
    font-variant-numeric: tabular-nums;
}

/* Step cell wrapper — centres the card within its grid column */
.wf-step-cell {
    display: flex;
    justify-content: center;
    padding: 0 4px;
}

/* ── Step-type family palette ────────────────────────────────────────
   Categorical colors for the canvas step cards. These are INTENTIONALLY
   theme-independent: a Split is always amber, a Loop always pink, etc.,
   regardless of the tenant's brand color — categorical encoding must stay
   stable so a builder learns "amber = fork" once. Defined once here as
   named tokens (not scattered hex) and read via var(--wf-fam) on the rail
   + chip. Mid-ramp values so they read on light AND dark surfaces. */
.wf-canvas {
    --wf-fam-info: #378ADD;
    --wf-fam-connected: #639922;
    --wf-fam-plain: #8A8A82;
    --wf-fam-split: #C98A1E;
    --wf-fam-multisplit: #7C74D6;
    --wf-fam-context: #1D9E75;
    --wf-fam-value: #D2693E;
    --wf-fam-loop: #CF5C84;
    --wf-fam-start: #57902A;
    --wf-fam-end: #8A8A82;
    --wf-fam-branch: #6E76B3;
    --wf-fam-exit: #2A9D8F;
    --wf-bdg-internal: #B8860B;
}

/* Step card */
.wf-step-card {
    position: relative;
    width: 220px;
    border: 1px solid var(--theme-border);
    border-radius: var(--theme-radius);
    background: var(--theme-background-subtle);
    padding: 0.5rem 0.75rem;
    cursor: pointer;
    transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
    text-align: center;
    user-select: none;
    overflow: hidden;
}

/* Real (non-pseudo) cards carry a .wf-step-body; they go flush-left so the
   color rail can hug the card edge. Pseudo cards (start/end/option) keep the
   centered padding above. */
.wf-step-card:has(.wf-step-body) {
    padding: 0;
    text-align: left;
}

/* The redesigned real-step card: color rail + type-icon chip + text column. */
.wf-step-body {
    display: flex;
    align-items: flex-start;
    gap: 0;
    padding: 0.45rem 0.6rem 0.45rem 0;
}

.wf-step-rail {
    width: 4px;
    align-self: stretch;
    flex: none;
    background: var(--wf-fam, var(--theme-border));
}

.wf-step-chip {
    width: 30px;
    height: 30px;
    margin: 0 0.5rem;
    border-radius: 8px;
    flex: none;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.92rem;
    color: var(--wf-fam, var(--theme-text-secondary));
    background: color-mix(in oklch, var(--wf-fam, var(--theme-text-secondary)) 14%, var(--theme-background));
}

.wf-step-chip.wf-chip-round {
    border-radius: 50%;
}

.wf-step-main {
    min-width: 0;
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 1px;
    padding-top: 1px;
}

.wf-step-title {
    font-size: 0.82rem;
    font-weight: 600;
    line-height: 1.2;
    color: var(--theme-text);
    text-align: left;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    text-overflow: ellipsis;
    word-break: break-word;
}

.wf-step-sub {
    font-size: 0.67rem;
    line-height: 1.2;
    color: var(--theme-text-secondary);
    text-transform: capitalize;
}

.wf-step-badges {
    display: flex;
    flex-wrap: wrap;
    gap: 3px;
    margin-top: 4px;
}

.wf-bdg {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    font-size: 0.62rem;
    font-weight: 500;
    line-height: 1;
    padding: 2px 5px;
    border-radius: 4px;
    white-space: nowrap;
}

.wf-bdg i {
    font-size: 0.72rem;
}

.wf-bdg-connected {
    background: color-mix(in oklch, var(--theme-success) 16%, var(--theme-background));
    color: var(--theme-success);
}

.wf-bdg-required {
    background: color-mix(in oklch, var(--theme-danger) 14%, var(--theme-background));
    color: var(--theme-danger);
}

.wf-bdg-internal {
    background: color-mix(in oklch, var(--wf-bdg-internal) 18%, var(--theme-background));
    color: var(--wf-bdg-internal);
}

.wf-bdg-sensitive {
    background: var(--theme-background-hover);
    color: var(--theme-text-secondary);
}

.wf-bdg-photo {
    background: color-mix(in oklch, var(--theme-primary) 12%, var(--theme-background));
    color: var(--theme-primary);
}

/* Family classes — set --wf-fam (rail + chip) for the card. */
.wf-fam-info { --wf-fam: var(--wf-fam-info); }
.wf-fam-connected { --wf-fam: var(--wf-fam-connected); }
.wf-fam-plain { --wf-fam: var(--wf-fam-plain); }
.wf-fam-split { --wf-fam: var(--wf-fam-split); }
.wf-fam-multisplit { --wf-fam: var(--wf-fam-multisplit); }
.wf-fam-context { --wf-fam: var(--wf-fam-context); }
.wf-fam-value { --wf-fam: var(--wf-fam-value); }
.wf-fam-loop { --wf-fam: var(--wf-fam-loop); }
.wf-fam-start { --wf-fam: var(--wf-fam-start); }
.wf-fam-end { --wf-fam: var(--wf-fam-end); }
.wf-fam-branch { --wf-fam: var(--wf-fam-branch); }
.wf-fam-exit { --wf-fam: var(--wf-fam-exit); }

/* Connected capture: a whisper of green on the card body reinforces the rail. */
.wf-fam-connected {
    background: color-mix(in oklch, var(--wf-fam-connected) 6%, var(--theme-background-subtle));
    border-color: color-mix(in oklch, var(--wf-fam-connected) 30%, var(--theme-border));
}

/* Pseudo blocks — workflow start/end (bookends) and split option / exit
   labels (branches). They read as NOT-a-capture: a fuller card tint plus a
   SOLID family chip (white glyph) sets them apart from the white, rail-only
   step cards. Same rail + chip + title structure for one consistent canvas. */
.wf-fam-start,
.wf-fam-end,
.wf-fam-branch,
.wf-fam-exit {
    background: color-mix(in oklch, var(--wf-fam) 9%, var(--theme-background-subtle));
    border-color: color-mix(in oklch, var(--wf-fam) 32%, var(--theme-border));
}

.wf-fam-start .wf-step-chip,
.wf-fam-end .wf-step-chip,
.wf-fam-branch .wf-step-chip,
.wf-fam-exit .wf-step-chip {
    background: var(--wf-fam);
    color: #fff;
}

.wf-step-delete {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 20px;
    height: 20px;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--theme-text-muted);
    font-size: 0.75rem;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    opacity: 0;
    transition: opacity 150ms ease, color 150ms ease, background 150ms ease;
    padding: 0;
    line-height: 1;
}

/* MultiSplit loop-back indicator — branches return to this step until exit.
   Bottom-right so it clears the left rail + chip and the top-right delete. */
.wf-step-loopback {
    position: absolute;
    bottom: 3px;
    right: 5px;
    width: 16px;
    height: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--wf-fam-multisplit, var(--theme-primary));
    font-size: 0.8rem;
    line-height: 1;
    pointer-events: auto;
}

.wf-step-card:hover .wf-step-delete {
    opacity: 1;
}

.wf-step-delete:hover {
    background: var(--theme-danger, #dc3545);
    color: #fff;
}

.wf-step-card:hover {
    border-color: color-mix(in oklch, var(--wf-fam, var(--theme-primary)) 55%, var(--theme-border));
    box-shadow: 0 1px 5px rgba(0, 0, 0, 0.10);
}

/* CONNECTED step (binds an entity the bound context writes back to via the
   WorkflowConsumer sink) is now signaled by the .wf-fam-connected family
   (green rail + chip + a whisper of card tint) plus the "connected" badge —
   see the family palette block above. */

.wf-step-type {
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--theme-text-secondary);
    margin-bottom: 0.2rem;
}

.wf-step-name {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
    font-size: 0.78rem;
    line-height: 1.2;
    color: var(--theme-text);
    font-weight: 500;
    overflow: hidden;
    /* 2-line clamp: allows two-line wrap, then ellipsis on the third */
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    text-overflow: ellipsis;
    word-break: break-word;
}

.wf-step-name i {
    flex-shrink: 0;
    margin-right: 0.25rem;
}

/* Workflow pseudo step (start/end) */
.wf-step-workflow {
    border-color: var(--theme-primary);
    background: var(--theme-primary-muted);
}

.wf-step-workflow .wf-step-type {
    color: var(--theme-primary);
}

/* Option pseudo step */
.wf-step-option {
    border-color: var(--theme-offset);
    background: var(--theme-offset-muted);
}

.wf-step-option .wf-step-type {
    color: var(--theme-offset);
}

/* Exit option pseudo step */
.wf-step-exit {
    border-color: var(--theme-accent);
    background: var(--theme-accent-muted);
}

.wf-step-exit .wf-step-type {
    color: var(--theme-accent);
}

/* Vertical gap cell */
.wf-vgap-cell {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 0;
    height: 56px;
}

/* Dead-zone filler — stretch to match row, no add button */
.wf-deadzone-cell {
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 100%;
    padding: 0;
}

/* Loop-back terminator — drawn at the bottom of each non-exit branch of a
   MultiSplit step. Caps the trail line from the "+" above. Same color and
   weight as the rest of the connector lines so the canvas reads as one
   consistent line style. */
.wf-loopback-terminator {
    width: 60px;
    height: 2px;
    background: var(--theme-border-subtle);
    margin: 0 auto;
}

.wf-vgap-line {
    width: 2px;
    flex: 1;
    background: var(--theme-border-subtle);
}

.wf-add-btn {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: 1px solid var(--theme-border-subtle);
    background: var(--theme-background-subtle);
    color: var(--theme-text-secondary);
    font-size: 1rem;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    transition: border-color 0.15s, color 0.15s, background 0.15s;
    flex-shrink: 0;
}

.wf-add-btn:hover {
    border-color: var(--theme-primary);
    color: var(--theme-primary);
    background: var(--theme-primary-muted);
}

/* Horizontal branch/merge connector cells — same height as VGap so all G rows are 56px */
.wf-hconn {
    height: 56px;
    padding: 0;
    overflow: visible;
}

/* SVG bracket connectors */
.wf-connector-svg {
    display: block;
    width: 100%;
    height: 100%;
}

.wf-connector-svg path {
    stroke: var(--theme-border-subtle);
    stroke-width: 2;
    stroke-linecap: round;
    fill: none;
}

/* Multi-split connectors inherit the base stroke — same color and weight
   as every other connector so switch type doesn't change the visual style. */

/* Loop-back connector */
.wf-loopback {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    width: 28px;
}

.wf-loopback-top {
    flex-shrink: 0;
    height: 24px;
}

.wf-loopback-bottom {
    flex-shrink: 0;
    height: 32px;
}

.wf-loopback-mid {
    flex: 1;
    width: 2px;
    margin-left: 13px;
    background: repeating-linear-gradient(
        to bottom,
        var(--theme-border-subtle) 0px,
        var(--theme-border-subtle) 5px,
        transparent 5px,
        transparent 9px
    );
}

/* ── Drag-and-drop ─────────────────────────────────────────────── */

/* Draggable steps */
.wf-step-name[draggable="true"] {
    cursor: grab;
}
.wf-step-name[draggable="true"]:active {
    cursor: grabbing;
    opacity: 0.5;
}

/* Drop target: highlight via @ondragenter/@ondragleave toggling .wf-drag-over
   (CSS :hover doesn't fire during HTML5 drag operations) */
.wf-drag-over .wf-add-btn {
    border-color: var(--theme-accent, #0d6efd);
    color: var(--theme-accent, #0d6efd);
    background: var(--theme-accent-muted, rgba(13,110,253,0.15));
    transform: scale(1.4);
    transition: transform 0.12s ease, background 0.12s ease, border-color 0.12s ease;
    box-shadow: 0 0 8px rgba(13,110,253,0.3);
}
.wf-drop-target:hover .wf-vgap-line {
    background: var(--theme-accent, #0d6efd);
    transition: background 0.12s ease;
}

/* ── Drag-mode drop indicator — modest pill, parent cell catches drops ──── */
/* The parent .wf-vgap-cell remains the full 220×56 hit area during drag —
   only the visual indicator changes from a tiny (+) to a more obvious pill,
   so users see exactly where they can drop without the canvas turning into
   a wall of giant boxes. */
.wf-vgap-cell.wf-dragging {
    height: 56px;
    display: flex;
    align-items: center;
    justify-content: center;
}
.wf-drop-zone {
    min-width: 110px;
    height: 32px;
    padding: 0 0.65rem;
    border: 2px dashed var(--theme-border-subtle, #b0b7bf);
    border-radius: 16px;
    background: var(--theme-background-subtle, #f8f9fa);
    color: var(--theme-text-muted, #6c757d);
    font-size: 0.72rem;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.3rem;
    transition: background 0.12s ease, border-color 0.12s ease, color 0.12s ease, transform 0.12s ease;
    user-select: none;
    pointer-events: none; /* parent cell handles drop events */
}
.wf-vgap-cell.wf-dragging.wf-drag-over .wf-drop-zone {
    border-color: var(--theme-accent, #0d6efd);
    border-style: solid;
    background: var(--theme-accent-muted, rgba(13,110,253,0.15));
    color: var(--theme-accent, #0d6efd);
    transform: scale(1.08);
    box-shadow: 0 0 10px rgba(13,110,253,0.25);
}

/* Ensure canvas scrolls during drag */
.canvas-scroll {
    scroll-behavior: smooth;
}
