/* ==========================================================================
   Tree view — folders and files, categories, permissions, navigation.
   Plain nested lists: folders are native <details>, so they open and close without
   JavaScript; leaves are links, buttons or labels.

   <ul class="tree" aria-label="Files">
     <li>
       <details open>
         <summary><i class="mi mi-folder"></i>src <span class="tree-meta">3</span></summary>
         <ul>
           <li><a href="/src/index.html"><i class="mi mi-file-code"></i>index.html</a></li>
         </ul>
       </details>
     </li>
     <li><button><i class="mi mi-file-text"></i>README.md</button></li>
   </ul>

   moey.js adds: tree roles for screen readers, arrow-key navigation (WAI-ARIA tree),
   type-ahead, selection, folder icons that open/close, checkbox trees (a folder checks
   everything inside; partly checked shows a dash), filtering, and lazy loading.

   .tree-lines   guide lines        .tree-sm   compact        theme-<color>   selection color
   ========================================================================== */

.tree {
  --mo-tree-indent: 1.25rem;
  --mo-tree-row: 2rem;
  --mo-tree-chevron: 1rem;
  margin: 0;
  padding: 0;
  font-size: .875rem;
  list-style: none;
}
.tree ul { margin: 0; padding: 0 0 0 var(--mo-tree-indent); list-style: none; }
.tree li { margin: 0; }
.tree li[hidden] { display: none; }
.tree summary { list-style: none; }
.tree summary::-webkit-details-marker { display: none; }

/* A row: the folder's <summary>, or the leaf's link / button / label / .tree-item */
.tree :is(summary, li > a, li > button, li > label, li > .tree-item) {
  display: flex;
  align-items: center;
  gap: .5rem;
  width: 100%;
  min-height: var(--mo-tree-row);
  padding: 0 .5rem 0 .25rem;
  font: inherit;
  color: var(--mo-fg-body);
  text-align: start;
  text-decoration: none;
  cursor: pointer;
  user-select: none;
  background: none;
  border: 0;
  border-radius: var(--mo-radius-control);
}
/* Chevron on folders; the same space on leaves, so labels line up */
.tree :is(summary, li > a, li > button, li > label, li > .tree-item)::before {
  flex-shrink: 0;
  width: var(--mo-tree-chevron);
  height: var(--mo-tree-chevron);
  content: "";
}
.tree summary::before {
  background: var(--mo-fg-3);
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M6 4l4 4-4 4' fill='none' stroke='black' stroke-width='1.75' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / 100% no-repeat;
}
@media (prefers-reduced-motion: no-preference) { .tree summary::before { transition: rotate .15s ease; } }
.tree details[open] > summary::before { rotate: 90deg; }
.tree details[open] > summary::before:dir(rtl) { rotate: -90deg; }

.tree :is(summary, li > a, li > button, li > label, li > .tree-item) > .mi { flex-shrink: 0; font-size: 1rem; color: var(--mo-fg-3); }
.tree .tree-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree .tree-meta { flex-shrink: 0; margin-inline-start: auto; font-size: .75rem; color: var(--mo-fg-3); font-variant-numeric: tabular-nums; }
.tree .form-check-input { flex-shrink: 0; margin: 0; }

.tree :is(summary, li > a, li > button, li > label, li > .tree-item):hover { background-color: color-mix(in oklab, var(--mo-fg-body) 6%, transparent); }
.tree :is(summary, li > a, li > button, li > label, li > .tree-item):focus-visible {
  outline: var(--mo-focus-ring-width) solid var(--mo-theme-focus-ring);
  outline-offset: -2px;
}
/* Selected / current */
.tree :is(summary, li > a, li > button, li > label, li > .tree-item):is([aria-selected="true"], .active, [aria-current]:not([aria-current="false"])) {
  color: var(--mo-theme-subtle-text);
  background-color: var(--mo-theme-subtle);
}
.tree :is(summary, li > a, li > button, li > label, li > .tree-item):is([aria-selected="true"], .active, [aria-current]:not([aria-current="false"])) > .mi { color: var(--mo-theme-fg); }
.tree :is(li > a, li > button):is(:disabled, [aria-disabled="true"]) { cursor: default; opacity: .5; }

/* Loading children (lazy): <details class="is-loading"> */
.tree details.is-loading > summary::before {
  mask: none;
  background: none;
  border: 2px solid color-mix(in oklab, var(--mo-fg-3) 30%, transparent);
  border-top-color: var(--mo-fg-3);
  border-radius: 50%;
  width: .75rem; height: .75rem; margin-inline: .125rem;
  animation: mo-spin .7s linear infinite;
}
@keyframes mo-spin { to { rotate: 360deg; } }

/* Guide lines: a vertical line under each open folder, aligned with its chevron */
.tree-lines ul {
  margin-inline-start: calc(.25rem + var(--mo-tree-chevron) / 2);
  padding-inline-start: calc(var(--mo-tree-indent) - .25rem - var(--mo-tree-chevron) / 2);
  border-inline-start: var(--mo-border-width) solid var(--mo-border-color);
}

/* Compact */
.tree-sm { --mo-tree-row: 1.625rem; --mo-tree-indent: 1rem; --mo-tree-chevron: .875rem; font-size: .8125rem; }
.tree-sm :is(summary, li > a, li > button, li > label, li > .tree-item) { gap: .375rem; }
