/* Insights › Árvore de Leads — the Desempenho comercial frame: a masthead with
   presets and filters, one joined KPI panel with the trail of where the leads
   stand today, then cards sized by their own container. The tree is the full
   picture; channels, stores and campaigns read the same counts where a tree
   cannot, down to a phone. Each stage keeps the hue Relatórios › Leads gives it,
   set once through [data-stage] and read as --_stage by dots, bars and slices. */
@scope (.leads-tree) {
  :scope {
    --_surface: var(--color-surface);
    --_muted: var(--color-surface-muted);
    --_border: var(--color-border-muted);
    --_ink: var(--color-text-heading);
    display: flex;
    flex-direction: column;
    gap: var(--space-xl);
    container: leads-tree / inline-size;
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  :scope > * {
    min-inline-size: 0;
  }

  [data-stage="lead"] { --_stage: var(--blue-600); }
  [data-stage="prospect"] { --_stage: var(--sky-500); }
  [data-stage="scheduled"] { --_stage: var(--amber-600); }
  [data-stage="visited"] { --_stage: var(--teal-600); }
  [data-stage="closed"] { --_stage: var(--violet-600); }
  [data-stage="not_visited"] { --_stage: var(--rose-500); }
  [data-stage="discarded"] { --_stage: var(--slate-400); }

  /* Masthead */
  .leads-tree__masthead {
    display: flex;
    flex-wrap: wrap;
    align-items: end;
    justify-content: space-between;
    gap: var(--space-lg);
    flex-shrink: 0;
    padding-block: var(--space-lg) var(--space-xl);
    border-bottom: 1px solid var(--_border);
  }

  .leads-tree__eyebrow {
    color: var(--color-text-secondary);
    font-size: var(--text-xs);
    font-weight: var(--font-bold);
    letter-spacing: var(--tracking-widest);
    text-transform: uppercase;
  }

  .leads-tree__title {
    margin-top: var(--space-xs);
    color: var(--_ink);
    font-size: clamp(var(--text-heading-2), 3vw, var(--text-heading-1));
    font-weight: var(--font-semibold);
  }

  .leads-tree__thesis {
    max-inline-size: 46rem;
    margin-top: var(--space-sm);
    color: var(--color-text-secondary);
    font-size: var(--text-body);
  }

  /* KPI tiles: the 1px grid gap over the border color draws the dividers, so any
     column count keeps clean lines. */
  .leads-tree__summary,
  .leads-tree__card {
    flex-shrink: 0;
    border: 1px solid var(--_border);
    border-radius: var(--radius-xl);
    background: var(--_surface);
    box-shadow: var(--shadow-xs);
  }

  .leads-tree__summary {
    overflow: hidden;
    container: leads-tree-kpis / inline-size;
  }

  .leads-tree__metrics {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 1px;
    border-bottom: 1px solid var(--_border);
    background: var(--_border);
  }

  .leads-tree__metric {
    display: grid;
    align-content: start;
    gap: var(--space-xs);
    min-inline-size: 0;
    padding: var(--space-lg);
    background: var(--_surface);
  }

  /* The stage's hue as a dot: the same key the trail and the bars use. */
  .leads-tree__metric[data-stage] .leads-tree__metric-label::before,
  .leads-tree__table thead th[data-stage]::before {
    content: "";
    display: inline-block;
    inline-size: 0.6rem;
    block-size: 0.6rem;
    margin-inline-end: var(--space-xs);
    border-radius: var(--radius-full);
    background: var(--_stage);
    vertical-align: 0.05em;
  }

  .leads-tree__metric--lead {
    background: linear-gradient(var(--color-primary-muted), var(--color-primary-muted)), var(--_surface);
  }

  /* Five tiles: the lead takes a row of its own until all five fit across. */
  @container leads-tree-kpis (width < 64rem) {
    .leads-tree__metric--lead {
      grid-column: 1 / -1;
    }
  }

  @container leads-tree-kpis (width >= 64rem) {
    .leads-tree__metrics {
      grid-template-columns: repeat(5, minmax(0, 1fr));
    }
  }

  @container leads-tree-kpis (width < 22rem) {
    .leads-tree__metrics {
      grid-template-columns: minmax(0, 1fr);
    }
  }

  .leads-tree__metric-label {
    color: var(--color-text-secondary);
    font-size: var(--text-xs);
    font-weight: var(--font-bold);
    letter-spacing: var(--tracking-wide);
    text-transform: uppercase;
  }

  .leads-tree__metric-value {
    overflow: hidden;
    color: var(--_ink);
    font-family: var(--font-mono);
    font-size: clamp(var(--text-heading-5), 2.2cqi, var(--text-heading-3));
    font-variant-numeric: tabular-nums;
    letter-spacing: var(--tracking-tighter);
    line-height: var(--leading-tight);
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* A name in a figure's slot: same size, the body face, cut rather than wrapped. */
  .leads-tree__metric-value--name {
    font-family: inherit;
    font-weight: var(--font-semibold);
    letter-spacing: normal;
  }

  .leads-tree__metric-detail {
    color: var(--color-text-muted);
    font-size: var(--text-caption);
    font-variant-numeric: tabular-nums;
  }

  /* The trail reads the period as one bar: the stage each lead is in right now. */
  .leads-tree__trail {
    display: grid;
    gap: var(--space-sm);
    padding: var(--space-md) var(--space-lg) var(--space-lg);
  }

  .leads-tree__trail-title {
    color: var(--color-text-secondary);
    font-size: var(--text-caption);
    font-weight: var(--font-semibold);
  }

  .leads-tree__trail-track {
    display: flex;
    gap: 2px;
    overflow: hidden;
    height: 0.65rem;
    border-radius: var(--radius-full);
    background: var(--_muted);
  }

  .leads-tree__trail-segment {
    flex: var(--share, 0) 1 0;
    min-inline-size: 2px;
    background: var(--_stage);
  }

  .leads-tree__trail-legend {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-xs) var(--space-lg);
    color: var(--color-text-secondary);
    font-size: var(--text-caption);
    font-variant-numeric: tabular-nums;
    list-style: none;
  }

  .leads-tree__trail-key {
    display: inline-flex;
    align-items: center;
    gap: var(--space-xs);
  }

  .leads-tree__trail-key::before {
    content: "";
    flex-shrink: 0;
    width: 0.6rem;
    height: 0.6rem;
    border-radius: var(--radius-full);
    background: var(--_stage);
  }

  .leads-tree__trail-key span {
    color: var(--color-text-muted);
  }

  .leads-tree__trail-empty,
  .leads-tree__empty {
    color: var(--color-text-muted);
    font-size: var(--text-caption);
  }

  /* Cards */
  .leads-tree__grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: var(--space-lg);
    padding-bottom: var(--space-xl);
  }

  @container leads-tree (width >= 54rem) {
    .leads-tree__grid {
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .leads-tree__card--wide {
      grid-column: 1 / -1;
    }
  }

  .leads-tree__card {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    align-content: start;
    gap: var(--space-md);
    min-inline-size: 0;
    padding: var(--space-lg);
    container: card / inline-size;
  }

  .leads-tree__card .echart {
    min-inline-size: 0;
  }

  .leads-tree__card--empty {
    place-items: center;
    align-content: center;
    gap: var(--space-sm);
    min-block-size: 14rem;
    color: var(--color-text-secondary);
    text-align: center;
  }

  .leads-tree__empty-icon {
    width: 2rem;
    height: 2rem;
    color: var(--color-text-muted);
  }

  .leads-tree__card-header {
    display: flex;
    flex-wrap: wrap;
    align-items: start;
    justify-content: space-between;
    gap: var(--space-sm) var(--space-lg);
  }

  .leads-tree__card-title {
    color: var(--_ink);
    font-size: var(--text-heading-5);
    font-weight: var(--font-semibold);
  }

  .leads-tree__card-hint {
    margin-top: var(--space-2xs, 0.25rem);
    color: var(--color-text-muted);
    font-size: var(--text-caption);
  }

  .leads-tree__num {
    font-family: var(--font-mono);
    font-variant-numeric: tabular-nums;
    text-align: end;
    white-space: nowrap;
  }

  .leads-tree__num small {
    display: block;
    color: var(--color-text-muted);
    font-size: var(--text-xs);
  }

  /* Tree: the levels it unfolds through, then a canvas that pans on its own. */
  .leads-tree__levels {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2xs, 0.25rem);
    color: var(--color-text-muted);
    font-size: var(--text-xs);
    font-weight: var(--font-semibold);
    letter-spacing: var(--tracking-wide);
    list-style: none;
    text-transform: uppercase;
  }

  .leads-tree__levels li + li::before {
    content: "›";
    margin-inline-end: var(--space-2xs, 0.25rem);
    color: var(--color-text-muted);
  }

  .leads-tree__canvas {
    border: 1px solid var(--_border);
    border-radius: var(--radius-lg);
    background:
      radial-gradient(circle, var(--_border) 1px, transparent 1.5px) 0 0 / 1.25rem 1.25rem,
      var(--_surface);
    overflow: hidden;
  }

  /* Channels and stores: name and figures over a bar split by stage. */
  .leads-tree__branches {
    display: grid;
    gap: var(--space-md);
    list-style: none;
  }

  .leads-tree__branch {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: baseline;
    gap: var(--space-xs) var(--space-md);
  }

  .leads-tree__branch-name {
    overflow: hidden;
    color: var(--_ink);
    font-size: var(--text-sm);
    font-weight: var(--font-semibold);
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .leads-tree__branch-figures {
    display: inline-flex;
    align-items: baseline;
    gap: var(--space-sm);
    font-size: var(--text-sm);
  }

  .leads-tree__branch-figures strong {
    color: var(--_ink);
  }

  .leads-tree__branch-figures span {
    min-inline-size: 3.25rem;
    color: var(--color-text-muted);
    font-size: var(--text-caption);
  }

  .leads-tree__branch .leads-tree__stack {
    grid-column: 1 / -1;
  }

  .leads-tree__stack {
    display: flex;
    gap: 2px;
    overflow: hidden;
    inline-size: var(--reach, 100%);
    min-inline-size: 0.5rem;
    block-size: 0.5rem;
    border-radius: var(--radius-full);
  }

  .leads-tree__stack > span {
    flex: var(--share, 0) 1 0;
    min-inline-size: 2px;
    background: var(--_stage);
  }

  /* Tables */
  .leads-tree__table-scroll {
    overflow-x: auto;
  }

  .leads-tree__table {
    width: 100%;
    border-collapse: collapse;
    font-size: var(--text-sm);
  }

  .leads-tree__table th,
  .leads-tree__table td {
    padding: var(--space-sm);
    border-bottom: 1px solid var(--_border);
    text-align: start;
    vertical-align: middle;
  }

  .leads-tree__table thead th {
    color: var(--color-text-secondary);
    font-size: var(--text-xs);
    font-weight: var(--font-bold);
    letter-spacing: var(--tracking-wide);
    text-transform: uppercase;
    white-space: nowrap;
  }

  .leads-tree__table thead .leads-tree__num {
    font-family: inherit;
  }

  .leads-tree__table tbody th {
    font-weight: var(--font-normal, 400);
  }

  .leads-tree__table tbody tr:last-child > * {
    border-bottom: 0;
  }

  .leads-tree__campaign {
    display: block;
    color: var(--_ink);
    font-weight: var(--font-semibold);
  }

  .leads-tree__campaign-path {
    display: block;
    color: var(--color-text-muted);
    font-size: var(--text-caption);
  }

  .leads-tree__campaign-total {
    color: var(--_ink);
    font-weight: var(--font-semibold);
  }

  .leads-tree__campaign-stages {
    min-inline-size: 9rem;
  }

  /* A card too narrow for six columns turns each campaign into a block — name
     and leads on top, the standings labelled below, the stage bar under all. */
  @container card (width < 44rem) {
    .leads-tree__table thead {
      display: none;
    }

    .leads-tree__table tr {
      display: grid;
      grid-template-columns: repeat(3, minmax(0, 1fr));
      align-items: start;
      gap: var(--space-sm) var(--space-md);
      padding-block: var(--space-md);
      border-bottom: 1px solid var(--_border);
    }

    .leads-tree__table tbody tr:last-child {
      border-bottom: 0;
    }

    .leads-tree__table th,
    .leads-tree__table td {
      padding: 0;
      border: 0;
    }

    .leads-tree__table th[scope="row"] {
      grid-column: 1 / span 2;
      grid-row: 1;
    }

    .leads-tree__table .leads-tree__campaign-total {
      grid-column: 3;
      grid-row: 1;
      text-align: end;
    }

    .leads-tree__table .leads-tree__campaign-stages {
      grid-column: 1 / -1;
      min-inline-size: 0;
    }

    .leads-tree__table td[data-label]:not(.leads-tree__campaign-total):not(.leads-tree__campaign-stages) {
      display: grid;
      gap: var(--space-2xs, 0.25rem);
      text-align: start;
    }

    .leads-tree__table td[data-label]:not(.leads-tree__campaign-total):not(.leads-tree__campaign-stages)::before {
      content: attr(data-label);
      overflow: hidden;
      color: var(--color-text-muted);
      font-family: var(--font-sans, inherit);
      font-size: var(--text-xs);
      font-weight: var(--font-semibold);
      letter-spacing: var(--tracking-wide);
      text-overflow: ellipsis;
      text-transform: uppercase;
      white-space: nowrap;
    }
  }
}
