/* Every bar floats between two levels, absolute in both orientations, so the other axis is pinned or a
   horizontal bar collapses to a hairline. Connector and baseline take a table rule's ink: neither is a
   third quantity. Fractions are properties, so a step restyles 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);
  }

  /* More between steps than within one; 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;
  }

  /* Drawn from inside this step so it cannot drift from the bar whose level it leaves. */
  .lotics-waterfall-chart__connector {
    position: absolute;
    background-color: var(--lotics-waterfall-chart-rule);
  }

  /* Its negative offset is the gap it spans, so the plot's gutter 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;
  }
}
