.flow-chart {
  --bg: #ffffff;
  --fg: #222;
  --muted: #9aa0a6;
  --active: #2ecc71;
  --inactive: #bbb;
  --grid: #e5e7eb;
}

.flow-chart .layout {
    display: grid;
    grid-template-columns: 1fr 400px;
    gap: 12px;
    padding: 12px 20px 20px;
}

.flow-chart .stage {
    /*min-height: 560px;
    border: 1px solid var(--grid);
    border-radius: 12px;
    background: #fafafa;*/
    display: grid;
    place-items: center;
    position: relative;
    /*overflow-x: hidden;*/
}

.flow-chart .controls {
    border: 1px solid var(--grid);
    border-radius: 12px;
    padding: 12px;
    max-width: 100%;
}

.flow-chart .control-row {
    display: grid;
    grid-template-columns: 140px minmax(0, 1fr);
    gap: 8px 12px;
    align-items: start;
    padding: 8px 0;
    border-bottom: 1px dashed var(--grid);
}
.flow-chart .control-row:last-child {
    border-bottom: none;
}
.flow-chart .control-row .label {
    font-size: 14px;
    font-weight: 600;
    min-width: 0;
    align-self: center;
}
.flow-chart .control-row .inputs {
    display: flex;
    flex-wrap: wrap;
    gap: 8px 12px;
    min-width: 0;
}
.flow-chart label {
    display: inline-flex;
    gap: 6px;
    align-items: center;
    font-size: 13px;
}
.flow-chart select {
    border: 1px solid var(--grid);
    border-radius: 8px;
    padding: 6px 8px;
    width: 100%;
    max-width: 160px;
}
.flow-chart .control-row .inputs label {
    flex: 1 1 150px;
}

@media (max-width: 900px) {
    .flow-chart .layout {
        grid-template-columns: 1fr;
    }
    .flow-chart .controls {
        order: 2;
    }
}


/* SVG styles */
.flow-chart svg {
    width: min(720px, 100%);
    height: auto;
}
/* Prospettiva 3D dello schema */
.flow-chart .stage {
    perspective: 1500px;
    /* Variabili responsive per icone */
    --iconMin: 96px;
    --iconMax: 320px;
    --fluidBase: min(720px, 100%);
    --iconFactor: 0.22; /* default */
    --padMin: 6px;
    --padMax: 18px;
    --padFactor: 0.010; /* default */
    /* Variabili bubble */
    --bubbleMin: 60px;
    --bubbleMax: 80px;
    --bubbleFactor: 0.18;
    --bubbleOffsetX: 0px;
    --bubbleOffsetY: 0px;
    --bubbleAnchorX: -50%;
    --bubbleAnchorY: -100%;
    --bubbleBlur: 8px; /* intensità blur */
    --bubbleBgAlpha: 0.55; /* opacità sfondo della bubble */
    --bubbleFadeMs: 200ms; /* durata transizione di visibilità delle bubble */
    /* Bagliore esterno per flussi attivi */
    --flowGlowColor: rgba(25, 228, 197, 0.75);
    --flowGlowSoft: 3px;
}
.flow-chart .stage svg {
    --rx: 56deg;
    --ry: 0deg;
    --rz: 130deg;
    --s: 1;
    transform: rotateX(var(--rx)) rotateY(var(--ry)) rotateZ(var(--rz)) scale(var(--s));
    transform-origin: 50% 50%;
    transform-style: preserve-3d;
    will-change: transform;
    transition: transform 200ms ease;
}

/* Safari-only: disabilita la transition su transform dello stage SVG */
.flow-chart html.safari .stage svg {
    transition: none !important;
}

/* Overlay HTML per immagini 2D, indipendente dalla rotazione dell'SVG */
.flow-chart .stage .overlay {
    position: absolute;
    inset: 0;
    pointer-events: auto;
    z-index: 2;
}
.flow-chart .stage .overlay img.node-image {
    position: absolute;
    display: block;
    opacity: 0.98;
    pointer-events: auto;
    user-select: none;
    touch-action: none;
    transform: translate(var(--anchorX, -50%), var(--anchorY, -100%));
    /* cursor: grab; */
    /* clamp con fattore fluido per icona */
    width: clamp(var(--iconMin), calc(var(--fluidBase) * var(--iconFactor)), var(--iconMax));
    height: auto;
}

/* Bubble */
.flow-chart .stage .overlay .node-bubble {
    position: absolute;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0px;
    width: clamp(var(--bubbleMin), calc(var(--fluidBase) * var(--bubbleFactor)), var(--bubbleMax));
    aspect-ratio: 1 / 1;
    border-radius: 999px;
    background: rgba(255, 255, 255, var(--bubbleBgAlpha, 0.6));
    -webkit-backdrop-filter: blur(var(--bubbleBlur, 8px));
    backdrop-filter: blur(var(--bubbleBlur, 8px));
    box-shadow: 0 2px 10px rgba(0,0,0,0.08), inset 0 0 0 1px rgba(0,0,0,0.06);
    filter: drop-shadow(0 0 8px rgba(46, 204, 113, 0.12));
    pointer-events: none;
    transform: translate( calc(var(--bubbleAnchorX, -50%) + var(--bubbleOffsetX, 0px)), calc(var(--bubbleAnchorY, -100%) + var(--bubbleOffsetY, 0px)) );
    /* Bordo del rettangolo da usare come riferimento per la bubble */
    --bubbleBoxEdge: right;
    opacity: 1;
    visibility: visible;
}


.flow-chart .stage .overlay .node-bubble .icon {
    width: 26px;
    height: 26px;
}
.flow-chart .stage .overlay .node-bubble .value {
    font-weight: 700;
    color: #333;
    font-size: clamp(16px, calc(var(--fluidBase) * 0.05), 34px);
    line-height: 1;
}
.flow-chart .stage .overlay .node-bubble .unit {
    color: var(--muted);
    font-size: clamp(9px, calc(var(--fluidBase) * 0.025), 14px);
    line-height: 1;
    padding-top: 4px;
}

/* Fattori fluidi specifici per nodo (override) */
.flow-chart .stage .overlay img.node-image.solar {
    --iconFactor: 0.28;
    --iconMin: 40px;
    --iconMax: 360px;
    --anchorX: -45%;
    --anchorY: -90%;
    --padMin: 0px;
    --padMax: 40px;
    --padFactor: 0.05
}
.flow-chart .stage .overlay img.node-image.grid {
    --iconFactor: 0.19;
    --iconMin: 35px;
    --iconMax: 380px;
    --anchorX: -50%;
    --anchorY: -100%;
    --padMin: 0px;
    --padMax: 20px;
    --padFactor: 0.05;
}
.flow-chart .stage .overlay img.node-image.house {
    --iconFactor: 0.40;
    --iconMin: 70px;
    --iconMax: 300px;
    --anchorX: -60%;
    --anchorY: -90%;
    --padMin: 0px;
    --padMax: 50px;
    --padFactor: 0.05;
}
.flow-chart .stage .overlay img.node-image.battery {
    --iconFactor: 0.08;
    --iconMin: 10px;
    --iconMax: 60px;
    --anchorX: -50%;
    --anchorY: -100%;
    --padMin: 0px;
    --padMax: 60px;
    --padFactor: 0.05;
}

/* Posizione bubble specifica per nodo */
.flow-chart .stage .overlay .node-bubble.solar {
    --bubbleOffsetX: 0px;
    --bubbleOffsetY: 0px;
    --bubbleAnchorX: -100%;
    --bubbleAnchorY: 0%;
}
.flow-chart .stage .overlay .node-bubble.grid {
    --bubbleOffsetX: 0px;
    --bubbleOffsetY: 0px;
    --bubbleAnchorX: -100%;
    --bubbleAnchorY: 0%;
}
.flow-chart .stage .overlay .node-bubble.house {
    --bubbleBoxEdge: left;
    --bubbleOffsetX: 0px;
    --bubbleOffsetY: 0px;
    --bubbleAnchorX: 50%;
    --bubbleAnchorY: -50%;
}
.flow-chart .stage .overlay .node-bubble.battery {
    --bubbleBoxEdge: left;
    --bubbleOffsetX: 0px;
    --bubbleOffsetY: 0px;
    --bubbleAnchorX: 0%;
    --bubbleAnchorY: -30%;
}


.flow-chart .node rect {
    fill: none;
    stroke: var(--inactive);
    stroke-width: 2;
}
.flow-chart .node.node-active rect {
    stroke: url(#flowGradient);
}

.flow-chart .arrow {
    fill: none;
    stroke: #333;
    stroke-width: 2;
}

.flow-chart .arrow.flow-active {
    stroke: url(#flowGradient);
    stroke-dasharray: 8 6;
    animation: flow 1.2s linear infinite;
    filter: drop-shadow(0 0 var(--flowGlowSoft, 6px) var(--flowGlowColor, rgba(46, 204, 113, 0.55)))
}
.flow-chart .arrow.flow-inactive {
    stroke: var(--inactive);
}
.flow-chart .arrow.flow-hidden {
    display: none;
}

.flow-chart .arrow {
    stroke-linecap: round;
}

@keyframes flow {
  from { stroke-dashoffset: 0; }
  to   { stroke-dashoffset: -28; }
}