/*
 * The planning tree (/planning): ObjectiveTreeItemComponent items in
 * ol.tree-group lists, nested strategic > annual > quarterly. Each item is a
 * header -- a .tree-item__meta row (level, period, progress, assignees) above
 * the wording -- then one
 * section.tree-year per year it spans, each holding the group of its
 * key results (013) and a details.tree-new "Add key result" form. Editors drag
 * items between groups (objective_tree_controller.js toggles
 * .tree-group--over and .tree-item--dragging). The level modifiers
 * .tree-item--strategic|annual|quarterly step the wording down in size.
 * In front of the wording sits .tree-item__toggle, a native checkbox drawn as
 * a disclosure triangle; checked, it shows the item's ol.tree-item__key-results
 * (a .tree-key-result line each) with no JavaScript -- a :has() rule. Grant
 * objectives linked to an item sit under it as .tree-grant-objective lines,
 * always visible and tinted positive (DESIGN.md "Grant tint"); a chip from the
 * sidebar dragged over the item's header outlines it (.tree-item__header--drop).
 */
@layer components {
  .tree {
    max-inline-size: none;
  }

  /* A drop target: a dashed outline shows while something is dragged over it. */
  .tree-group {
    display: grid;
    margin: 0;
    padding: 0;
    border: 1px dashed transparent;
    border-radius: var(--radius-s);
    list-style: none;

    & > li + li {
      margin-block-start: 0;
    }

    /* Empty, it still offers somewhere to drop. */
    &:not(:has(> li)) {
      min-block-size: var(--space-7);
      border-color: var(--color-line);
    }
  }

  /* While an item is dragged, every group shows where it could land. */
  .tree:has(.tree-item--dragging) .tree-group {
    border-color: var(--color-line);
  }

  .tree .tree-group.tree-group--over {
    border-color: var(--color-text);
    background: var(--color-surface);
  }

  .tree-item {
    max-inline-size: none;
    padding-block: var(--space-3);

    & + & {
      border-block-start: var(--border);
    }

    &[draggable="true"] {
      cursor: grab;
    }
  }

  .tree-item--dragging {
    opacity: 0.4;
  }

  /* Archived: faded, the wording muted, still opens its page. */
  .tree-item--archived {
    & > .tree-item__header > :not(.tree-item__title) {
      opacity: 0.6;
    }

    & .tree-item__title {
      color: var(--color-muted);
    }
  }

  /* Completed (015): the header on the positive surface, a positive start rule. */
  .tree-item--completed > .tree-item__header {
    padding-inline-start: var(--space-2);
    border-inline-start: var(--space-1) solid var(--color-positive);
    border-radius: var(--radius-s);
    background: var(--color-positive-surface);
  }

  /*
   * Two rows on a grid: the meta row on top, starting where the wording does,
   * then the toggle and the wording. The meta row comes last in the markup
   * (the toggle stays the header's first child); grid areas place it. In edit
   * mode a selection box joins the toggle and an Open link ends the meta row.
   * The visually hidden labels are absolutely positioned and take no cell.
   */
  .tree-item__header {
    display: grid;
    grid-template-columns: var(--space-5) minmax(0, 1fr);
    grid-template-areas:
      ".      meta"
      "toggle title";
    align-items: center;
    gap: var(--space-1) var(--space-3);
    font: var(--text-small);
    color: var(--color-muted);

    &:has(> .tree-item__select) {
      grid-template-columns: var(--space-5) auto minmax(0, 1fr) auto;
      grid-template-areas:
        ".      .      meta  open"
        "toggle select title title";
    }

    & > .tree-item__toggle { grid-area: toggle; }
    & > .tree-item__select { grid-area: select; }
    & > :is(.tree-item__title, .title-form) { grid-area: title; }
    & > .tree-item__open { grid-area: open; }
  }

  /* Level label, period, progress and assignees: one small row. */
  .tree-item__meta {
    grid-area: meta;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2) var(--space-3);
    min-inline-size: 0;

    & > * {
      margin: 0;
    }
  }

  /*
   * The disclosure toggle: a checkbox with its own look dropped, so only the
   * glyph shows. A fixed square keeps every title starting at the same edge
   * (the key results indent by the same amount). The glyph is text, not an icon.
   */
  .tree-item__toggle {
    appearance: none;
    display: inline-grid;
    place-items: center;
    flex: none;
    /* One line of the title tall, pinned to its first line when it wraps. */
    align-self: start;
    inline-size: var(--space-5);
    block-size: 1lh;
    margin: 0;
    padding: 0;
    border: 0;
    background: transparent;
    color: var(--color-muted);
    font: var(--text-body-strong);
    cursor: pointer;

    &::before {
      content: "▸";
    }

    &:checked::before {
      content: "▾";
    }

    &:hover {
      color: var(--color-text);
    }

    &:focus-visible {
      outline: 2px solid var(--color-focus);
      outline-offset: 2px;
    }
  }

  /* The wording links to the objective's page; it reads as a heading. */
  .tree-item__title {
    justify-self: start;
    min-inline-size: 0;
    font: var(--text-body-strong);
    color: var(--color-ink);
    text-decoration: none;

    &:hover {
      text-decoration: underline;
      text-underline-offset: 0.2em;
    }
  }

  /* The wording steps down with the level; child combinators keep nested items apart. */
  .tree-item--strategic > .tree-item__header .tree-item__title {
    font: var(--text-title);
  }

  .tree-item--annual > .tree-item__header .tree-item__title {
    font: var(--text-lead);
  }

  /* The toggle takes the title's size, so its triangle and line match it. */
  .tree-item--strategic > .tree-item__header > .tree-item__toggle {
    font: var(--text-title);
  }

  .tree-item--annual > .tree-item__header > .tree-item__toggle {
    font: var(--text-lead);
  }

  .tree-item--quarterly > .tree-item__header .tree-item__title {
    font: var(--text-body-strong);
  }

  .tree-item__empty {
    color: var(--color-subtle);
  }

  /*
   * Key results: hidden until the toggle in the item's own header is checked
   * (child combinators, so a checked parent never opens its children's lists).
   * Indented to where the wording starts: toggle width plus the header gap.
   */
  .tree-item__key-results {
    display: none;
    gap: var(--space-2);
    margin-block: var(--space-2) 0;
    margin-inline-start: calc(var(--space-5) + var(--space-3));
    padding: 0;
    list-style: none;
  }

  .tree-item:has(> .tree-item__header > .tree-item__toggle:checked) > .tree-item__key-results {
    display: grid;
  }

  /*
   * Grant objectives linked to this objective: after its key results, before its
   * year sections, indented like the key results, and never tied to the toggle.
   * Tinted positive so a funder's wording is never mistaken for the plan's.
   */
  .tree-item__grant-objectives {
    display: grid;
    gap: var(--space-3);
    /* Set apart from the key results above and the year sections below. */
    margin-block: var(--space-4);
    margin-inline-start: calc(var(--space-5) + var(--space-3));
    padding: 0;
    list-style: none;

    &:not(:has(> li)) {
      display: none;
    }
  }

  .tree-grant-objective {
    max-inline-size: none;
    padding: var(--space-2) var(--space-3);
    border-inline-start: var(--space-1) solid var(--color-positive);
    border-radius: var(--radius-s);
    background: var(--color-positive-surface);
    font: var(--text-small);
    color: var(--color-text);
  }

  /* The grant's name, quiet, on its own line with the state word; the wording below it. */
  .tree-grant-objective__grant {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: var(--space-1) var(--space-2);
    max-inline-size: none;
    margin: 0;
    font: var(--text-caption);
    color: var(--color-positive);
  }

  .tree-grant-objective__grant-name {
    color: inherit;
    text-decoration-line: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 0.2em;
  }

  a.tree-grant-objective__grant-name:hover {
    color: var(--color-ink);
  }

  .tree-grant-objective__state {
    font: var(--text-caption);
    color: var(--color-positive);

    &::before {
      content: "· ";
    }
  }

  .tree-grant-objective__title {
    max-inline-size: none;
    margin: var(--space-1) 0 0;
    font: var(--text-body-strong);
    color: var(--color-ink);
  }

  /* Its key results: the number in positive, one small line each. */
  .tree-grant-objective__key-results {
    display: grid;
    gap: var(--space-1);
    max-inline-size: none;
    margin: var(--space-2) 0 0;
    padding: 0;
    list-style: none;

    & > li {
      max-inline-size: none;
      color: var(--color-text);
    }

    & strong {
      color: var(--color-positive);
    }
  }

  /* Unlink: a quiet text link closing the box, not a button. */
  .tree-grant-objective__unlink {
    margin: var(--space-2) 0 0;

    & button {
      min-block-size: auto;
      padding: 0;
      border: 0;
      background: none;
      color: var(--color-muted);
      font: var(--text-caption);
      text-decoration-line: underline;
      text-decoration-thickness: 1px;
      text-underline-offset: 0.2em;

      &:hover {
        background: none;
        color: var(--color-negative);
      }

      &:focus-visible {
        outline: 2px solid var(--color-focus);
        outline-offset: 2px;
      }
    }
  }

  /* A sidebar chip dragged over a plan item: its header shows it can take it. */
  .tree-item__header--drop {
    outline: 2px solid var(--color-positive);
    outline-offset: 2px;
    border-radius: var(--radius-s);
  }

  /* One key result: its wording taking the room, then the bar and its number. */
  .tree-key-result {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    max-inline-size: none;
    font: var(--text-small);
    color: var(--color-text);

    & > * {
      margin: 0;
    }

    & > .meter {
      flex: none;
      inline-size: 6rem;
    }

    /* The percentage in a fixed slot, so every bar ends at the same edge. */
    & > .meter + span {
      flex: none;
      inline-size: 6ch;
      text-align: end;
    }
  }

  /* No reading-width cap: the wording takes the row, so every bar ends at the same edge. */
  .tree-key-result__title {
    flex: 1 1 0;
    min-inline-size: 0;
    max-inline-size: none;
    margin: 0;
  }

  /*
   * Edit mode (.tree--editing, ?edit=1): the wording and each key result's
   * goal, description and baseline become forms that save in place. They reuse
   * title_form.css and key_result.css; only what the tree needs differently is
   * overridden here, always inside .tree--editing so the objective page keeps
   * its own look. Each Save stays in the markup as the no-JavaScript fallback
   * and shows only while its form has focus.
   */
  .tree--editing {
    /* The title form takes the room the link took, one row, no hanging margin. */
    & .tree-item__header > .title-form {
      align-items: center;
      min-inline-size: 0;
      margin: 0;
    }

    & .title-form__input {
      flex: 1 1 0;
      min-inline-size: 0;
      min-block-size: 0;
      margin: 0;
      field-sizing: content;
    }

    /* The textarea wears the level's wording, as .tree-item__title does. */
    & .tree-item--strategic > .tree-item__header .title-form__input {
      font: var(--text-title);
    }

    & .tree-item--annual > .tree-item__header .title-form__input {
      font: var(--text-lead);
    }

    & .tree-item--quarterly > .tree-item__header .title-form__input {
      font: var(--text-body-strong);
    }

    /* A small quiet link to the objective's page. */
    & .tree-item__open {
      justify-self: end;
      margin: 0;
      font: var(--text-small);
      color: var(--color-muted);
      text-decoration: none;

      &:hover {
        color: var(--color-text);
        text-decoration: underline;
        text-underline-offset: 0.2em;
      }
    }

    /*
     * A key result reads "From: [baseline] To: [goal] — description" on one
     * line. Every box on it -- the words, both figures, the description, Save
     * -- is one small line plus the same padding and border, so aligned to the
     * start they share their first line, also when the description wraps.
     */
    & .tree-key-result {
      align-items: start;
      gap: var(--space-2);

      & > * {
        margin: 0;
      }

      & .key-result-title-form,
      & .baseline-form {
        display: flex;
        flex-wrap: nowrap;
        align-items: start;
        gap: var(--space-2);
      }

      & .key-result-title-form {
        flex: 1 1 0;
        min-inline-size: 0;
      }

      & .baseline-form {
        flex: none;
      }

      & :where(input[type="number"], textarea) {
        min-block-size: 0;
        margin: 0;
        padding: var(--space-1) var(--space-2);
        font: var(--text-small);
      }

      /* Both figures alike: a small paper box, wide enough for "e.g. 0". */
      & input[type="number"] {
        flex: none;
        inline-size: calc(6ch + var(--space-2) * 2 + 2px);
        min-inline-size: 0;
        border: var(--border);
        background: var(--color-paper);
        field-sizing: fixed;
      }

      & .key-result-title-form__description {
        flex: 1 1 0;
        min-inline-size: 0;
      }

      /* Still read by screen readers (aria-describedby), never drawn. */
      & .baseline-form__hint {
        position: absolute;
        flex-basis: auto;
        inline-size: 1px;
        block-size: 1px;
        overflow: hidden;
        clip-path: inset(50%);
        white-space: nowrap;
      }
    }

    /* "From:", "To:" and the dash: text boxes as tall as the inputs beside them. */
    & :is(.tree-key-result__label, .key-result-title-form__dash) {
      flex: none;
      padding-block: var(--space-1);
      border-block: 1px solid transparent;
      font: var(--text-small);
      color: var(--color-muted);
    }

    /*
     * With a selection box in the header the wording starts further in; its key
     * results and grant objectives follow. :where() keeps this weaker than a
     * hidden level's lift (below), which un-indents them.
     */
    & :where(.tree-item:has(> .tree-item__header > .tree-item__select)) > :is(.tree-item__key-results, .tree-item__grant-objectives) {
      margin-inline-start: calc(var(--space-5) + var(--space-3) * 2 + 1.125rem);
    }

    /* Save takes no room until its form has focus, so the field gets the row. */
    & :is(.title-form, .key-result-title-form, .baseline-form) input[type="submit"] {
      display: none;
      flex: none;
      visibility: visible;
    }

    & :is(.title-form, .key-result-title-form, .baseline-form):is(:focus-within, :has([aria-invalid="true"])) input[type="submit"] {
      display: inline-flex;
    }

    /* The selection box sits on the title's first line, as the toggle does. */
    & .tree-item__select {
      align-self: start;
      margin-block-start: calc((1lh - 1.125rem) / 2);
      font: var(--text-body-strong);
    }

    & .tree-item--strategic > .tree-item__header > .tree-item__select {
      font: var(--text-title);
    }

    & .tree-item--annual > .tree-item__header > .tree-item__select {
      font: var(--text-lead);
    }
  }

  /* Nesting: a parent's years sit indented behind a hairline on the start edge. */
  .tree-item > .tree-year {
    margin-block-start: var(--space-2);
    margin-inline-start: var(--space-2);
    padding-inline-start: var(--space-4);
    border-inline-start: var(--border);
  }

  .tree-year {
    & + & {
      margin-block-start: var(--space-4);
    }

    & > .eyebrow {
      margin-block-end: var(--space-1);
    }
  }

  /*
   * Level toggles (.tree--hide-strategic|annual|quarterly on .tree). Hiding a
   * level never hides what hangs beneath it: its children are lifted. The
   * item's own header and key results go, its padding and top hairline go, and
   * its .tree-year sections lose the nesting indent so the children appear
   * where the parent was; its linked grant objectives stay visible too. Nothing in the markup changes, so drag and drop
   * keeps working; the year heading stays (it says which year the lifted
   * children belong to) and so does the "New objective" form.
   */
  .tree--hide-strategic .tree-item--strategic,
  .tree--hide-annual .tree-item--annual,
  .tree--hide-quarterly .tree-item--quarterly {
    padding-block: 0;
    border-block-start: 0;

    /* Collapsed with visibility, not display: none, so a ticked selection
       box in the header still counts in the bulk toolbar (bulk_toolbar.css). */
    & > .tree-item__header {
      visibility: hidden;
      block-size: 0;
      overflow: hidden;
    }

    & > .tree-item__key-results {
      display: none;
    }

    & > .tree-year {
      margin-inline-start: 0;
      padding-inline-start: 0;
      border-inline-start: 0;
    }

    /* Its grant objectives stay, lifted like the children: no indent. */
    & > .tree-item__grant-objectives {
      margin-inline-start: 0;
    }
  }

  /*
   * Completed and archived toggle (015): unless it is on, a completed or
   * archived item goes, its children with it.
   */
  .tree--hide-closed :is(.tree-item--archived, .tree-item--completed) {
    display: none;
  }

  /* A group left with nothing visible is still a place to drop. */
  .tree--hide-closed .tree-group:not(:has(> li:not(.tree-item--archived, .tree-item--completed))) {
    min-block-size: var(--space-7);
    border-color: var(--color-line);
  }

  /* "New objective": a quiet link under its group that opens the form. */
  .tree-new {
    margin-block-start: var(--space-2);
    padding: 0;
    border: 0;
    cursor: auto;

    & > summary {
      inline-size: fit-content;
      list-style: none;
      font: var(--text-small);
      color: var(--color-muted);
      text-decoration-line: underline;
      text-decoration-thickness: 1px;
      text-underline-offset: 0.2em;

      &::-webkit-details-marker {
        display: none;
      }

      &:hover {
        color: var(--color-text);
      }
    }

    &[open] > summary {
      margin-block-end: var(--space-2);
    }

    & > form {
      max-inline-size: none;
      padding: var(--space-4);
      border: var(--border);
      border-radius: var(--radius-m);
      background: var(--color-paper);
    }
  }

  /*
   * The form on one line: wording (taking the room, one row, growing with its
   * text), level, Add, Cancel. The form defaults stack fields as <p> blocks
   * with a rule above the actions; inside the row they are plain flex items.
   */
  .objective-new-form__row {
    display: flex;
    align-items: center;
    gap: var(--space-3);

    /* The :has() form outranks forms.css's actions-row rule, which sets a rule and padding. */
    & p,
    & p:has(> input[type="submit"]) {
      margin: 0;
      padding: 0;
      border: 0;
    }

    & textarea {
      flex: 1;
      min-inline-size: 0;
      max-inline-size: none;
      field-sizing: content;
      min-block-size: auto;
    }

    & select {
      inline-size: auto;
    }

    /* The new key result's own number: short, beside the wording. */
    & .objective-new-form__target {
      inline-size: 7ch;
    }

    & .objective-new-form__due {
      inline-size: auto;
    }
  }

  /* An annual or quarterly objective's target ahead of its wording (2026-11-12). */
  .tree-item__target {
    font-variant-numeric: tabular-nums;
    margin-inline-end: var(--space-1);
  }
}
