/* -- Property Tree -------------------------------------------------------------
   One value as a tree of key / value rows - only the open branches have rows, only the rows on screen exist. A row:
   its fold arrow, its key, its value (a reference to an earlier appearance is a link), its type, an actions button.
   A value or a key edits in place. Every color, radius and font is the theme's; the value colours are its chart
   colours. */

@layer components {
  :where(.property-tree, .property-tree *, .property-tree *::before, .property-tree *::after) {
    box-sizing: border-box;
  }

  .property-tree {
    --property-tree-row-height: 28px;
    --property-tree-indent: 1rem;
    --property-tree-key-width: 38%;
    --property-tree-font: 0.8125rem;

    position: relative;
    display: flex;
    flex-direction: column;
    min-width: 0;
    block-size: 20rem;
    background: var(--card);
    color: var(--card-foreground);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    font-size: var(--property-tree-font);
    line-height: 1.35;

    &[data-variant="comfortable"] {
      --property-tree-row-height: 34px;
      --property-tree-font: 0.875rem;
    }
    &[data-frame="none"] {
      border: 0;
      border-radius: 0;
      background: transparent;
    }
    & > script {
      display: none;
    }
  }

  .property-tree-viewport {
    flex: 1;
    min-block-size: 0;
    overflow: auto;
    overscroll-behavior: contain;
    border-radius: inherit;

    &:focus-visible {
      outline: 2px solid var(--ring);
      outline-offset: -2px;
    }
  }
  .property-tree-sizer {
    position: relative;
  }
  .property-tree-rows {
    position: absolute;
    inset-inline: 0;
    inset-block-start: 0;
  }

  .property-tree-row {
    display: grid;
    grid-template-columns:
      calc(0.5rem + var(--depth, 0) * var(--property-tree-indent) + 1rem)
      minmax(4rem, var(--property-tree-key-width)) minmax(0, 1fr) auto auto;
    align-items: center;
    gap: 0.5rem;
    block-size: var(--property-tree-row-height);
    padding-inline-end: 0.25rem;
    white-space: nowrap;
    cursor: default;

    &:hover {
      background: color-mix(in oklab, var(--accent) 60%, transparent);
    }
    &[aria-selected="true"] {
      background: var(--accent);
      color: var(--accent-foreground);
    }
    .property-tree-viewport:focus-visible &[data-active] {
      box-shadow: inset 0 0 0 2px var(--ring);
    }
    &[data-editing] {
      position: relative;
      z-index: 2;
      background: var(--background);
    }
  }

  .property-tree-toggle {
    justify-self: end;
    inline-size: 1rem;
    block-size: 1rem;
    cursor: pointer;

    &::before {
      content: "";
      display: block;
      inline-size: 100%;
      block-size: 100%;
      background: currentColor;
      opacity: 0.6;
      mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m9 18 6-6-6-6'/%3E%3C/svg%3E")
        center / 0.875rem no-repeat;
      transition: rotate 120ms ease;
    }
    [aria-expanded="true"] > &::before {
      rotate: 90deg;
    }
    &[data-leaf] {
      cursor: default;

      &::before {
        opacity: 0;
      }
    }
  }

  .property-tree-key {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    font-weight: 500;

    &[data-key-kind="index"] {
      color: var(--muted-foreground);
      font-family: var(--font-mono);
      font-weight: 400;
    }
    &[data-key-kind="root"] {
      color: var(--muted-foreground);
    }
  }

  .property-tree-value {
    display: flex;
    align-items: center;
    gap: 0.375rem;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    font-family: var(--font-mono);
    font-size: 0.95em;

    &[data-kind="string"] {
      color: var(--chart-2);
    }
    &:is([data-kind="number"], [data-kind="bigint"]) {
      color: var(--chart-1);
    }
    &[data-kind="boolean"] {
      color: var(--chart-4);
    }
    &:is([data-kind="null"], [data-kind="undefined"]) {
      color: var(--muted-foreground);
      font-style: italic;
    }
    &:is([data-kind="date"], [data-kind="regexp"]) {
      color: var(--chart-5);
    }
    &:is([data-kind="object"], [data-kind="array"], [data-kind="map"], [data-kind="set"], [data-kind="typed"]) {
      color: var(--muted-foreground);
    }
    [aria-selected="true"] > & {
      color: inherit;
    }
  }
  .property-tree-swatch {
    flex: none;
    inline-size: 0.75rem;
    block-size: 0.75rem;
    border-radius: var(--radius-sm);
    box-shadow: inset 0 0 0 1px color-mix(in oklab, var(--foreground) 20%, transparent);
  }
  .property-tree-ref {
    padding: 0 0.375rem;
    border: 1px dashed var(--border);
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--primary);
    font: inherit;
    cursor: pointer;

    &:hover {
      border-style: solid;
    }
  }

  .property-tree-type {
    padding: 0 0.375rem;
    border-radius: var(--radius-sm);
    background: var(--muted);
    color: var(--muted-foreground);
    font-size: 0.6875rem;
    line-height: 1.5;
  }

  .property-tree-actions {
    display: grid;
    place-items: center;
    inline-size: 1.5rem;
    block-size: 1.5rem;
    padding: 0;
    border: 0;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--muted-foreground);
    cursor: pointer;
    opacity: 0;

    &::before {
      content: "";
      inline-size: 1rem;
      block-size: 1rem;
      background: currentColor;
      mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='black'%3E%3Ccircle cx='5' cy='12' r='1.8'/%3E%3Ccircle cx='12' cy='12' r='1.8'/%3E%3Ccircle cx='19' cy='12' r='1.8'/%3E%3C/svg%3E")
        center / contain no-repeat;
    }
    :is(.property-tree-row:hover, .property-tree-row[data-active], .property-tree-row[aria-selected="true"]) > &,
    &[aria-expanded="true"] {
      opacity: 1;
    }
    &:hover {
      background: var(--muted);
      color: var(--foreground);
    }
  }
  .property-tree[data-readonly] .property-tree-actions {
    display: none;
  }

  /* -- editing: a value's (or a key's) editor fills its cell; a refused value shows its message below -- */
  .property-tree-editor {
    position: relative;
    display: flex;
    flex: 1;
    align-items: center;
    min-width: 0;
    font-family: var(--font-sans);

    & > :is(.input, .select) {
      flex: 1;
      min-width: 0;
    }
    & > .textarea {
      position: absolute;
      inset-block-start: -0.25rem;
      inset-inline: 0;
      z-index: 3;
      min-block-size: 8rem;
      font-family: var(--font-mono);
      box-shadow: var(--shadow-md);
    }
  }
  .property-tree-row[data-editing] :is(.property-tree-key, .property-tree-value) {
    overflow: visible;
  }
  .property-tree-color-input {
    inline-size: 2.5rem;
    block-size: calc(var(--property-tree-row-height) - 6px);
    padding: 0;
    border: 1px solid var(--input);
    border-radius: var(--radius-sm);
    background: var(--background);
    cursor: pointer;
  }
  .property-tree-error {
    position: absolute;
    inset-block-start: calc(100% + 0.25rem);
    inset-inline-start: 0;
    z-index: 4;
    padding: 0.25rem 0.5rem;
    border-radius: var(--radius-sm);
    background: var(--destructive);
    color: var(--destructive-foreground);
    font-size: 0.75rem;
    font-family: var(--font-sans);
    white-space: nowrap;
    box-shadow: var(--shadow-md);
  }

  /* -- the actions menu: one per tree, anchored to the row's button -- */
  .property-tree-menu {
    position: fixed;
    inset: auto;
    margin: 0;
    position-area: block-end span-inline-start;
    position-try-fallbacks: flip-block, flip-inline;
    min-inline-size: 12rem;
    max-block-size: min(24rem, 70vh);
    overflow-y: auto;
    padding: 0.25rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--popover);
    color: var(--popover-foreground);
    box-shadow: var(--shadow-md);
    font-size: 0.8125rem;
  }
  .property-tree-menu-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    inline-size: 100%;
    padding: 0.3125rem 0.5rem;
    border: 0;
    border-radius: var(--radius-sm);
    background: transparent;
    color: inherit;
    font: inherit;
    text-align: start;
    cursor: pointer;

    &:is(:hover, :focus-visible) {
      background: var(--accent);
      color: var(--accent-foreground);
      outline: none;
    }
    &:disabled {
      opacity: 0.5;
      cursor: default;
    }
    &[role="menuitemradio"]::before {
      content: "";
      inline-size: 0.5rem;
      block-size: 0.5rem;
      border-radius: 50%;
    }
    &[aria-checked="true"]::before {
      background: currentColor;
    }
  }
  .property-tree-menu-separator {
    block-size: 1px;
    margin: 0.25rem -0.25rem;
    background: var(--border);
  }
}

/* -- Accessibility ----------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  @layer components {
    .property-tree-toggle::before {
      transition: none;
    }
  }
}

@media (prefers-contrast: more) {
  @layer components {
    .property-tree {
      border-color: var(--foreground);
    }
    .property-tree-type,
    .property-tree-value {
      color: var(--foreground);
    }
  }
}

@media (forced-colors: active) {
  @layer components {
    .property-tree {
      border-color: CanvasText;
    }
    .property-tree-row[aria-selected="true"] {
      background: Highlight;
      color: HighlightText;
    }
    .property-tree-toggle::before,
    .property-tree-actions::before {
      background: CanvasText;
    }
    .property-tree-viewport:focus-visible .property-tree-row[data-active] {
      outline: 2px solid Highlight;
    }
  }
}
