/*
 * The BRIDGE. Every bar floats between the level before it and the level after,
 * so it is absolute in BOTH orientations and the axis it does NOT float on has
 * to be pinned: a horizontal bar given only `left`/`width` collapses to its
 * minimum and draws a hairline where a bar was asked for.
 *
 * The connector and the baseline are hairlines in the SAME ink as a table's
 * rules: they join two bars and mark zero, and neither is a third quantity.
 *
 * Every fraction the geometry produces is DATA and arrives as a property, so a
 * consumer can restyle a step without `!important`.
 */
@layer theme, base, lotics, components, utilities;
@layer lotics.tokens, lotics.reset, lotics.components;

@layer lotics.components {
  .lotics-waterfall-chart {
    --lotics-waterfall-chart-rule: var(--border);
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: center;
  }

  .lotics-waterfall-chart__plot {
    display: flex;
    flex-direction: column;
    align-items: stretch;
  }

  .lotics-waterfall-chart[data-orientation="vertical"] .lotics-waterfall-chart__plot {
    gap: var(--lotics-space-8);
  }

  /* Between two steps, more than between a step's own name, figure and bar:
     those three are one object, and the connector spans exactly this gap. */
  .lotics-waterfall-chart[data-orientation="horizontal"] .lotics-waterfall-chart__plot {
    gap: var(--lotics-space-24);
  }

  .lotics-waterfall-chart__track {
    position: relative;
    display: flex;
    align-items: stretch;
  }

  .lotics-waterfall-chart[data-orientation="vertical"] .lotics-waterfall-chart__track {
    flex-direction: row;
    height: var(--lotics-waterfall-chart-size);
    gap: var(--lotics-space-8);
  }

  .lotics-waterfall-chart[data-orientation="horizontal"] .lotics-waterfall-chart__track {
    flex-direction: column;
    height: var(--lotics-waterfall-chart-size);
  }

  .lotics-waterfall-chart__slot {
    position: relative;
    display: flex;
  }

  .lotics-waterfall-chart[data-orientation="vertical"] .lotics-waterfall-chart__slot {
    flex-direction: column;
    align-items: stretch;
    flex: 1;
    min-width: 0;
  }

  .lotics-waterfall-chart[data-orientation="horizontal"] .lotics-waterfall-chart__slot {
    flex-direction: column;
    align-items: stretch;
    gap: var(--lotics-space-4);
  }

  .lotics-waterfall-chart__bar {
    position: absolute;
    border-radius: calc(var(--lotics-radius-control) - var(--lotics-space-8));
    background-color: var(--lotics-waterfall-chart-color);
  }

  .lotics-waterfall-chart[data-orientation="vertical"] .lotics-waterfall-chart__bar {
    left: 0;
    right: 0;
    bottom: var(--lotics-waterfall-chart-offset);
    height: var(--lotics-waterfall-chart-size-fraction);
    min-height: 2px;
  }

  .lotics-waterfall-chart[data-orientation="horizontal"] .lotics-waterfall-chart__bar {
    top: 0;
    bottom: 0;
    left: var(--lotics-waterfall-chart-offset);
    width: var(--lotics-waterfall-chart-size-fraction);
    min-width: 2px;
  }

  /* Bridges the gap to the NEXT step, drawn from inside this one so the
     connector cannot drift out of step with the bar whose level it leaves. */
  .lotics-waterfall-chart__connector {
    position: absolute;
    background-color: var(--lotics-waterfall-chart-rule);
  }

  /* Hung off the slot's trailing edge by its own negative offset, which is the
     gap it spans — so it is the plot's own gutter that decides its length. */
  .lotics-waterfall-chart[data-orientation="vertical"] .lotics-waterfall-chart__connector {
    right: calc(var(--lotics-space-8) * -1);
    bottom: var(--lotics-waterfall-chart-level);
    width: var(--lotics-space-8);
    height: 1px;
  }

  .lotics-waterfall-chart[data-orientation="horizontal"] .lotics-waterfall-chart__connector {
    bottom: calc(var(--lotics-space-24) * -1);
    left: var(--lotics-waterfall-chart-level);
    width: 1px;
    height: var(--lotics-space-24);
  }

  .lotics-waterfall-chart__baseline {
    position: absolute;
    background-color: var(--lotics-waterfall-chart-rule);
  }

  .lotics-waterfall-chart[data-orientation="vertical"] .lotics-waterfall-chart__baseline {
    left: 0;
    right: 0;
    bottom: var(--lotics-waterfall-chart-level);
    height: 1px;
  }

  .lotics-waterfall-chart[data-orientation="horizontal"] .lotics-waterfall-chart__baseline {
    top: 0;
    bottom: 0;
    left: var(--lotics-waterfall-chart-level);
    width: 1px;
  }

  .lotics-waterfall-chart__captions {
    display: flex;
    flex-direction: row;
    align-items: flex-start;
    gap: var(--lotics-space-8);
  }

  .lotics-waterfall-chart__caption {
    display: flex;
  }

  .lotics-waterfall-chart[data-orientation="vertical"] .lotics-waterfall-chart__caption {
    flex-direction: column;
    align-items: stretch;
    flex: 1;
    min-width: 0;
    gap: var(--lotics-space-2);
  }

  .lotics-waterfall-chart[data-orientation="horizontal"] .lotics-waterfall-chart__caption {
    flex-direction: row;
    align-items: flex-start;
    gap: var(--lotics-space-16);
  }

  .lotics-waterfall-chart__text {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: var(--lotics-space-2);
  }

  .lotics-waterfall-chart[data-orientation="horizontal"] .lotics-waterfall-chart__text {
    flex: 1;
    min-width: 0;
  }
}
