:host(:not(:is([hydrated], .hydrated))) {
  @apply not-hydrated;
}


mds-button {
  &:not(:is([hydrated], .hydrated)) {
    @apply not-hydrated;
  }
}


/**
 * @prop --mds-tree-item-actions-border-radius: Inherits the border-radius used for tree action containers.
 * @prop --mds-tree-item-actions-gap: Spacing between tree item action elements.
 * @prop --mds-tree-item-branch-border-color: The border color of the branch connector for the item.
 * @prop --mds-tree-item-branch-border-radius: The border-radius applied to the branch connector.
 * @prop --mds-tree-item-branch-border-size: The thickness of the branch connector line.
 * @prop --mds-tree-item-branch-dot-default-color: Default color of the branch dot indicator.
 * @prop --mds-tree-item-branch-dot-expanded-color: Color of the branch dot when expanded.
 * @prop --mds-tree-item-label-default-background: Background color of the label in default state.
 * @prop --mds-tree-item-label-hover-background: Background color of the label in hover state.
 * @prop --mds-tree-item-label-icon-default-color: Default icon color inside the label.
 * @prop --mds-tree-item-label-icon-hover-color: Icon color inside the label when hovering.
 * @prop --mds-tree-item-line-clamp: Defines the number of lines before text is truncated.
 * @prop --mds-tree-item-toggle-gap: Spacing between the label and the toggle control.
 * @prop --mds-tree-item-toggle-icon-async-background: Background color for async-loading toggle icons.
 * @prop --mds-tree-item-toggle-icon-async-color: Icon color for async-loading toggles.
 * @prop --mds-tree-item-toggle-icon-chevron-default-background: Background for the default chevron toggle.
 * @prop --mds-tree-item-toggle-icon-chevron-default-color: Icon color for the default chevron toggle.
 * @prop --mds-tree-item-toggle-icon-chevron-expanded-background: Background for the expanded chevron toggle.
 * @prop --mds-tree-item-toggle-icon-chevron-expanded-color: Icon color for the expanded chevron toggle.
 * @prop --mds-tree-item-toggle-icon-folder-default-background: Background for the default folder toggle.
 * @prop --mds-tree-item-toggle-icon-folder-default-color: Icon color for the default folder toggle.
 * @prop --mds-tree-item-toggle-icon-folder-expanded-background: Background for the expanded folder toggle.
 * @prop --mds-tree-item-toggle-icon-folder-expanded-color: Icon color for the expanded folder toggle.
 * @prop --mds-tree-item-toggle-icon-position-right-default-background: Background for right-positioned toggle in default state.
 * @prop --mds-tree-item-toggle-icon-position-right-default-color: Icon color for right-positioned toggle in default state.
 * @prop --mds-tree-item-toggle-icon-position-right-expanded-background: Background for right-positioned toggle in expanded state.
 * @prop --mds-tree-item-toggle-icon-position-right-expanded-color: Icon color for right-positioned toggle in expanded state.
 * @prop --mds-tree-item-toggle-size: Size of the toggle control.
 * @prop --mds-tree-item-transition-duration: Transition duration for tree item animation.
 * @prop --mds-tree-item-transition-timing-function: Transition easing function for tree item animation.
 */

/*
@property --mds-tree-item-transition-timing-function {
  inherits: false;
  initial-value: var(--mds-tree-transition-timing-function);
  syntax: "<timing-function>";
}
*/

@container style(--mds-tree-branch-use-rounded-border: true) {
  :host {
    --mds-tree-branch-border-radius: var(--private-tree-item-branch-border-radius);
  }
}

@container style(--mds-tree-branch-use-rounded-border: false) {
  :host {

    /* stylelint-disable-next-line length-zero-no-unit */
    --mds-tree-branch-border-radius: 0px;
  }
}

:host {
  --mds-tree-item-actions-border-radius: var(--mds-tree-actions-border-radius);
  --mds-tree-item-actions-gap: var(--mds-tree-actions-gap);
  --mds-tree-item-branch-border-color: var(--mds-tree-branch-border-color);
  --mds-tree-item-branch-border-radius: var(
    --mds-tree-branch-border-radius,
    var(--private-tree-item-branch-border-radius)
  );
  --mds-tree-item-branch-border-size: var(--mds-tree-branch-border-size);
  --mds-tree-item-branch-dot-default-color: var(--mds-tree-branch-dot-default-color);
  --mds-tree-item-branch-dot-expanded-color: var(--mds-tree-branch-dot-expanded-color);
  --mds-tree-item-label-default-background: var(--mds-tree-label-default-background);
  --mds-tree-item-label-hover-background: var(--mds-tree-label-hover-background);
  --mds-tree-item-label-icon-default-color: var(--mds-tree-label-icon-default-color);
  --mds-tree-item-label-icon-hover-color: var(--mds-tree-label-icon-hover-color);
  --mds-tree-item-line-clamp: var(--mds-tree-line-clamp);
  --mds-tree-item-toggle-gap: var(--mds-tree-toggle-gap, --spacing(100));
  --mds-tree-item-toggle-icon-async-background: var(--mds-tree-toggle-icon-async-background);
  --mds-tree-item-toggle-icon-async-color: var(--mds-tree-toggle-icon-async-color);
  --mds-tree-item-toggle-icon-chevron-default-background: var(
    --mds-tree-toggle-icon-chevron-default-background
  );
  --mds-tree-item-toggle-icon-chevron-default-color: var(
    --mds-tree-toggle-icon-chevron-default-color
  );
  --mds-tree-item-toggle-icon-chevron-expanded-background: var(
    --mds-tree-toggle-icon-chevron-expanded-background,
    var(--mds-tree-item-toggle-icon-chevron-default-background)
  );
  --mds-tree-item-toggle-icon-chevron-expanded-color: var(
    --mds-tree-toggle-icon-chevron-expanded-color,
    var(--mds-tree-item-toggle-icon-chevron-default-color)
  );
  --mds-tree-item-toggle-icon-folder-default-background: var(
    --mds-tree-toggle-icon-folder-default-background
  );
  --mds-tree-item-toggle-icon-folder-default-color: var(
    --mds-tree-toggle-icon-folder-default-color
  );
  --mds-tree-item-toggle-icon-folder-expanded-background: var(
    --mds-tree-item-toggle-icon-folder-default-background
  );
  --mds-tree-item-toggle-icon-folder-expanded-color: var(
    --mds-tree-toggle-icon-folder-expanded-color,
    var(--mds-tree-item-toggle-icon-folder-default-color)
  );
  --mds-tree-item-toggle-icon-position-right-default-background: var(
    --mds-tree-toggle-icon-position-right-default-background
  );
  --mds-tree-item-toggle-icon-position-right-default-color: var(
    --mds-tree-toggle-icon-position-right-default-color
  );
  --mds-tree-item-toggle-icon-position-right-expanded-background: var(
    --mds-tree-toggle-icon-position-right-expanded-background
  );
  --mds-tree-item-toggle-icon-position-right-expanded-color: var(
    --mds-tree-toggle-icon-position-right-expanded-color
  );
  --mds-tree-item-toggle-size: var(--mds-tree-toggle-size);
  --mds-tree-item-transition-duration: var(--mds-tree-transition-duration);
  --mds-tree-item-transition-timing-function: var(--mds-tree-transition-timing-function);

  /* internal */
  --mds-tree-item-icon-background-color: var(
    --mds-tree-item-toggle-icon-chevron-default-background
  );
  --mds-tree-item-icon-color: var(--mds-tree-item-toggle-icon-chevron-default-color);
  --header-gap: var(--mds-tree-item-toggle-gap);

  /* the elbow of an item with children is half a toggle wide, so its corner can take at most
     that half: capped as mds-button's corner is (#913), the radius the branch travels by stays
     the radius it draws */
  --private-tree-item-branch-border-radius: min(
    var(--magma-radius-lg, var(--radius-squircle-lg, 28px)),
    var(--magma-corner-cap, 0.5) * var(--mds-tree-item-toggle-size)
  );

  display: grid;
  transition-duration: var(--mds-tree-item-transition-duration);
  transition-timing-function: var(--mds-tree-item-transition-timing-function);
}

.header {
  --offset: calc(var(--mds-tree-item-toggle-size) + var(--header-gap));

  align-items: flex-start;
  display: flex;
  gap: var(--header-gap);
  min-width: 0;
  position: relative;
  z-index: 1;
}

.title {
  align-items: stretch;
  display: flex;
  flex-grow: 1;
  gap: --spacing(200);
  justify-content: space-between;
  min-width: 0;
}

.label-action {
  --mds-button-line-clamp: var(--mds-tree-item-line-clamp);
  --padding: --spacing(100);

  background-color: var(--mds-tree-item-label-default-background);

  /* an mds-button, which draws the shape of the axis on its own host: its radius is capped at
     a part of its 36px height, as its own is (#913) */
  border-radius: min(var(--magma-radius-lg, var(--radius-squircle-lg, 28px)), var(--magma-corner-cap, 0.5) * --spacing(900));
  fill: var(--mds-tree-item-label-icon-default-color);
  flex-grow: 1;
  justify-content: flex-start;
  margin-left: calc(var(--padding) * -1);
  padding-left: var(--padding);
  padding-right: var(--padding);
}

.label-action:hover {
  background-color: var(--mds-tree-item-label-hover-background);
  fill: var(--mds-tree-item-label-icon-hover-color);
}

.actions-container {
  --ease: var(--ease-in-out-expo, cubic-bezier(1, 0, 0, 1));

  align-items: center;
  border-radius: var(--mds-tree-item-actions-border-radius);
  corner-shape: var(--magma-corner-shape, squircle);
  display: none;
  interpolate-size: allow-keywords;
  justify-content: flex-start;
  min-width: 0;
  overflow: hidden;
  transition-behavior: allow-discrete;
  transition-duration: var(--duration-300, 300ms);
  transition-property: min-width, width, overflow;
  transition-timing-function: var(--ease);
  width: 0;
}

.actions-container--has-actions {
  display: flex;
}

.actions {
  align-items: center;
  border-radius: var(--mds-tree-item-actions-border-radius);
  corner-shape: var(--magma-corner-shape, squircle);
  display: inline-flex;
  flex-shrink: 0;
  gap: var(--mds-tree-item-actions-gap);
  pointer-events: none;
  transform: scale(0);
  transition-duration: var(--duration-300, 300ms);
  transition-property: transform;
  transition-timing-function: var(--ease);
}

/* Effective actions mode is resolved in JS (own value wins, else inherited from
 * the nearest ancestor tree/tree-item) and reflected as resolved-actions on the
 * host, so these read the item's own host instead of :host-context. 'auto' keeps
 * the default hidden .actions (scale 0) revealed on hover/focus below. */

:host([resolved-actions='visible']) .actions-container,
.header:focus-within .actions,
.header:hover .actions {
  pointer-events: auto;
}

:host([resolved-actions='visible']) .actions-container,
.header:focus-within .actions-container,
.header:hover .actions-container {
  animation: toggleOverflow var(--duration-300, 300ms) forwards;
  min-width: fit-content;
  width: fit-content;
}

:host([resolved-actions='visible']) .actions,
.header:focus-within .actions,
.header:hover .actions {
  transform: scale(1);
}

.children {
  --mds-tree-item-children-padding-left: calc(
    calc(var(--mds-tree-item-toggle-size) - var(--mds-tree-item-branch-border-size)) / 2
  );

  display: flex;
  flex-direction: column;
  height: 0;
  interpolate-size: allow-keywords;
  overflow: clip;
  padding-left: var(--mds-tree-item-children-padding-left);
  position: relative;
  transform: translateY(calc(var(--mds-tree-item-toggle-size) / 2 * -1));
  transition-duration: var(--mds-tree-item-transition-duration);

  /* a closed branch is only 0px tall: visibility keeps its items out of the tab order and of
     the accessibility tree, from the first render (the display: none below comes only after a
     collapse). As a transition it turns visible at the start of the opening and hidden at the
     end of the closing */
  transition-property: height, transform, visibility;
  transition-timing-function: var(--mds-tree-item-transition-timing-function);
  visibility: hidden;
  z-index: 0;
}

.children--expanded {
  animation: toggleOverflow var(--mds-tree-item-transition-duration) forwards;
  animation-timing-function: var(--mds-tree-item-transition-timing-function);
  display: flex;
  height: auto;
  transform: translateY(0);

  /* inherit, not visible: the open branch of an item inside a closed one stays hidden with it */
  visibility: inherit;
}

.children--hidden,
.children--empty {
  display: none;
}

@keyframes toggleOverflow {
  99.99% {
    overflow: hidden;
  }

  100% {
    overflow: visible;
  }
}

@keyframes toggleDisplay {
  99.99% {
    display: flex;
  }

  100% {
    display: none;
  }
}

.tree-node {
  --middle-pos-x: calc(50% - calc(var(--mds-tree-item-branch-border-size)/2));

  align-items: center;
  box-sizing: border-box;
  display: inline-flex;
  flex-shrink: 0;
  height: var(--mds-tree-item-toggle-size);
  justify-content: center;
  pointer-events: none;
  position: relative;
  width: var(--mds-tree-item-toggle-size);
  z-index: 0;
}

/* .tree-node::before, */

/* The elbow spans two rows, and where its line has to STOP depends on what is above it.
   Under a sibling it is that sibling's elbow, and an elbow leaves the vertical a radius
   before its own line: hence the radius in the travel, which is what joins the two into one
   continuous line down the side of the list. */

.tree-branch {
  --pos-y: calc(
    calc(var(--mds-tree-item-toggle-size) / 2 + var(--mds-tree-item-branch-border-radius, var(--magma-radius-lg, var(--radius-squircle-lg, 28px)))) * -1
  );
  --tree-height: calc(
    var(--mds-tree-item-toggle-size) + var(--mds-tree-item-branch-border-radius, var(--magma-radius-lg, var(--radius-squircle-lg, 28px))) +
      var(--mds-tree-item-branch-border-size) / 2
  );
  --tree-width: calc(var(--mds-tree-item-toggle-size) / 2);

  border-color: var(--mds-tree-item-branch-border-color);
  border-radius: 0 0 0 var(--mds-tree-item-branch-border-radius, var(--magma-radius-lg, var(--radius-squircle-lg, 28px)));
  border-style: solid;
  border-width: 0 0 var(--mds-tree-item-branch-border-size) var(--mds-tree-item-branch-border-size);
  box-sizing: border-box;
  corner-shape: var(--magma-corner-shape, squircle);
  display: flex;
  height: var(--tree-height);
  inset: 0;
  position: absolute;
  transform: translateY(var(--pos-y));
  width: calc(var(--mds-tree-item-toggle-size) / 2);
  z-index: 1;
}

/* Above the first child there is no sibling elbow but the icon of the item it hangs from,
   and the line has to end on the CENTRE of that icon, which covers it. A radius more - the
   travel every other child needs - and the line came out of the top of the icon.
   First and last are counted among the ITEMS (-of-type): the actions of the parent are
   light DOM children of it too, written before or after the items, so the first item is
   seldom its :first-child and the last one not always its :last-child. */

:host(:first-of-type) .tree-branch {
  --pos-y: calc(var(--mds-tree-item-toggle-size) / 2 * -1);
  --tree-height: calc(
    var(--mds-tree-item-toggle-size) + var(--mds-tree-item-branch-border-size) / 2
  );
}

.tree-dot {
  --size: calc(--spacing(300) + var(--mds-tree-item-branch-border-size) * 2);

  background-color: var(--mds-tree-item-branch-border-color);
  border-radius: var(--size);
  display: none;
  height: var(--size);
  position: relative;
  width: var(--size);
}

.header--has-children .tree-dot {
  display: inline-flex;
}

/* .header:not(.header--has-children) .tree-node::before, */

.header:not(.header--has-children) .tree-node .tree-branch {
  width: calc(var(--mds-tree-item-toggle-size) - var(--header-gap));
}

/* border used when component has children */

:host(:not(:last-of-type)) .header--has-children .tree-node::after {
  --size: calc(
    calc(var(--mds-tree-item-toggle-size) / 2) + var(--mds-tree-item-branch-border-radius, var(--magma-radius-lg, var(--radius-squircle-lg, 28px)))
  );
  --pos-y: calc(var(--size) / 2);

  /* border-color: purple; */
  border-color: var(--mds-tree-item-branch-border-color);
  border-radius: 0 0 0 var(--mds-tree-item-branch-border-radius, var(--magma-radius-lg, var(--radius-squircle-lg, 28px)));
  border-style: solid;
  border-width: 0 0 var(--mds-tree-item-branch-border-size) var(--mds-tree-item-branch-border-size);
  box-sizing: border-box;
  content: '';
  corner-shape: var(--magma-corner-shape, squircle);
  display: flex;
  height: var(--size);
  inset: 0;
  position: absolute;
  transform: translateY(var(--pos-y));
  width: var(--mds-tree-item-branch-border-size);
  z-index: 1;
}

:host(:not(:last-of-type)) .children--has-children::before {

  /* border-left-color: green; */
  border-left-color: var(--mds-tree-item-branch-border-color);
  border-left-style: solid;
  border-left-width: var(--mds-tree-item-branch-border-size);
  content: '';
  display: flex;
  height: calc(100% + var(--mds-tree-item-toggle-size));
  left: 0;
  position: absolute;
  top: calc(var(--mds-tree-item-toggle-size) * -1);
}

.tree-dot::after {
  --pos-y: calc(var(--size) / 2);

  background-color: var(--mds-tree-item-branch-border-color);
  box-sizing: border-box;
  content: '';
  display: inline-flex;
  height: calc(var(--size) / 2);
  inset: 0 50% 0 0;
  position: absolute;
  transform: translate(
    calc(var(--pos-y) - var(--mds-tree-item-branch-border-size) / 2),
    var(--pos-y)
  );
  transition-duration: var(--mds-tree-item-transition-duration);
  transition-property: height;
  transition-timing-function: var(--mds-tree-item-transition-timing-function);
  width: var(--mds-tree-item-branch-border-size);
  z-index: 1;
}

:host([expanded]) .tree-dot::after {
  height: calc(var(--size) + var(--mds-tree-item-branch-border-size));
}

/* remove first level elements */

:host([depth='0']) .children--has-children::before,
:host([depth='0']) .children--has-children .tree-branch,
:host([depth='0']) .header--has-children .tree-node::after,
:host([depth='0']) .tree-node::before,
:host([depth='0']) .tree-node .tree-branch {
  display: none;
}

.toggle-icon {
  align-items: center;
  align-self: center;
  display: inline-flex;
  flex-shrink: 0;
  height: var(--mds-tree-item-toggle-size);
  justify-content: center;
  margin-left: calc(var(--offset) * -1);
  position: relative;
  transform: rotate(0deg);
  transition-duration: var(--mds-tree-item-transition-duration);
  transition-property: transform;
  transition-timing-function: var(--mds-tree-item-transition-timing-function);
  width: var(--mds-tree-item-toggle-size);
  z-index: 1;
}

.toggle-icon mds-button {
  --size: --spacing(600);

  align-self: center;
  background-color: var(--mds-tree-item-icon-background-color);
  border: 0;
  border-radius: var(--mds-tree-item-toggle-size);
  color: var(--mds-tree-item-icon-color);
  fill: var(--mds-tree-item-icon-color);
  height: var(--size);
  min-height: var(--size);
  min-width: var(--size);
  overflow: hidden;
  width: var(--size);
}

.toggle-icon mds-button[await] {
  background-color: var(--mds-tree-item-toggle-icon-async-background);
  color: var(--mds-tree-item-toggle-icon-async-color);
}

/* toggle and toggle-position are propagated from mds-tree onto every item
 * (reflected props), so these read the item's own host (no :host-context). */

:host([toggle='chevron']) .toggle-icon {
  --mds-tree-item-icon-background-color: var(
    --mds-tree-item-toggle-icon-chevron-default-background
  );
  --mds-tree-item-icon-color: var(--mds-tree-item-toggle-icon-chevron-default-color);
}

:host([expanded][toggle='chevron']) .toggle-icon {
  --mds-tree-item-icon-background-color: var(
    --mds-tree-item-toggle-icon-chevron-expanded-background
  );
  --mds-tree-item-icon-color: var(--mds-tree-item-toggle-icon-chevron-expanded-color);
}

:host([toggle='folder'][toggle-position='left']) .toggle-icon mds-button {
  box-shadow: 0 --spacing(25) 0 0 var(--mds-tree-item-branch-border-color);
}

:host([toggle='folder']) .toggle-icon mds-button {
  --size: auto;

  /* a tile the folder glyph almost fills, so its corner is the glyph's own plus the padding;
     mds-button draws it in the shape of the axis on its own host (#998) */
  border-radius: var(--magma-radius-3xs, var(--radius-squircle-3xs, 6px));
  padding: --spacing(25) --spacing(50);
}

:host([toggle='folder']) .toggle-icon {
  --mds-tree-item-icon-background-color: var(--mds-tree-item-toggle-icon-folder-default-background);
  --mds-tree-item-icon-color: var(--mds-tree-item-toggle-icon-folder-default-color);
}

:host([toggle='folder'][expanded]) .toggle-icon {
  --mds-tree-item-icon-background-color: var(
    --mds-tree-item-toggle-icon-folder-expanded-background
  );
  --mds-tree-item-icon-color: var(--mds-tree-item-toggle-icon-folder-expanded-color);
}

:host([toggle-position='right']) .toggle-icon {
  --mds-tree-item-icon-background-color: var(
    --mds-tree-item-toggle-icon-position-right-default-background
  );
  --mds-tree-item-icon-color: var(--mds-tree-item-toggle-icon-position-right-default-color);
}

:host([expanded][toggle-position='right']) .toggle-icon {
  --mds-tree-item-icon-background-color: var(
    --mds-tree-item-toggle-icon-position-right-expanded-background
  );
  --mds-tree-item-icon-color: var(--mds-tree-item-toggle-icon-position-right-expanded-color);
}

:host([expanded][toggle-position='left'][toggle='chevron']) .toggle-icon {
  transform: rotate(90deg);
}

.header:not(.header--has-children) .toggle-icon {
  display: none;
}

/* toggle-position is propagated from mds-tree onto every item (reflected prop),
 * so these read the item's own host attribute instead of :host-context. */

:host([toggle-position='left']) .toggle-icon {
  order: 0;
}

:host([toggle-position='right']) .header--has-children .title {
  margin-right: var(--offset);
}

:host([toggle-position='left'][toggle='chevron'][expanded]) .toggle-icon {
  transform: rotate(90deg);
}

:host([toggle-position='left'][toggle='chevron']:not([expanded])) .toggle-icon {
  transform: rotate(0deg);
}

:host([toggle-position='right']) .toggle-icon {
  background-color: transparent;
  order: 1;
}

/* chevron right */

:host([toggle='chevron'][toggle-position='right']) .toggle-icon {
  transform: rotate(90deg);
}

:host([toggle='chevron'][expanded][toggle-position='right']) .toggle-icon {
  transform: rotate(270deg);
}

:host([toggle='chevron'][toggle-position='right']) .toggle-icon--dot,
:host([toggle='folder'][toggle-position='right']) .toggle-icon--dot {
  transform: rotate(0) scale(0.825);
}

:host([toggle='chevron'][toggle-position='right']) .toggle-icon,
:host([toggle='folder'][toggle-position='right']) .toggle-icon {
  background-color: transparent;
  fill: var(--mds-tree-item-branch-dot-default-color);
}

:host([toggle='chevron'][expanded][toggle-position='right']) .toggle-icon,
:host([toggle='folder'][expanded][toggle-position='right']) .toggle-icon {
  background-color: transparent;
  fill: var(--mds-tree-item-branch-dot-expanded-color);
}

/* appearance and toggle-position are propagated from mds-tree onto every item
 * (reflected props), so these read the item's own host (no :host-context). */

:host([appearance='none']) .header {
  --offset: 0;

  gap: 0;
}

:host([appearance='none']:not(:last-of-type)) .children--has-children::before,
:host([appearance='none']) .tree-dot,
:host([appearance='none']) .tree-node {
  display: none;
}

:host([appearance='none']) .label-action {
  --padding: --spacing(200);

  margin-left: 0;
  padding-left: var(--padding);
  padding-right: var(--padding);
}

:host([appearance='none'][toggle-position='left']) .children {
  padding-left: calc(
    calc(var(--header-gap) * 2) + var(--mds-tree-item-branch-border-size) +
      var(--mds-tree-item-children-padding-left) * 2
  );
}

:host([appearance='none'][toggle-position='right']) .children {
  padding-left: var(--mds-tree-item-children-padding-left);
}

