/*
 * A grant's page (grants#show), on the shared detail layout
 * (DetailPageComponent, detail.css): the lock strip, title and archived
 * panel, and the rules for what sits in its main column and sidebar. Its
 * objectives are .objective-card cards.
 */
@layer components {
  /*
   * Whether a won proposal is frozen (green) or unlocked (orange): a
   * full-width strip under the header, the state in bold. The tinted
   * surfaces carry ink text; the left edge carries the full brand colour.
   */
  .grant-lock {
    max-inline-size: none;
    margin-block-start: var(--space-4);
    padding: var(--space-3) var(--space-5);
    border-inline-start: var(--space-1) solid;
    border-radius: var(--radius-s);
    color: var(--color-ink);
  }

  .grant-lock--frozen {
    border-color: var(--color-positive);
    background: var(--color-positive-surface);
  }

  .grant-lock--unlocked {
    border-color: var(--color-attention);
    background: var(--color-attention-surface);
  }

  .grant-detail__title {
    font: var(--text-title);
    color: var(--color-ink);
    max-inline-size: none;
  }

  .grant-detail__introduction {
    max-inline-size: var(--width-reading);
    text-wrap: pretty;
  }

  /* Why the grant was archived, under its title: a quiet grey panel. */
  .grant-detail__archived {
    padding: var(--space-4) var(--space-5);
    border-radius: var(--radius-m);
    background: var(--color-surface-2);

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

  /* Archived: read-only, so the content below the notice fades. */
  .grant-detail--archived .detail__main > :not(.grant-detail__title, .grant-detail__archived) {
    opacity: 0.6;
  }

  .grant-detail__dialog-actions {
    display: flex;
    gap: var(--space-3);
  }

  .grant-detail__total {
    font: var(--text-body-strong);
    color: var(--color-ink);
  }
}

/* One grant objective's card: its plan link, key results, budget, actions. */
@layer components {
  /* Badge, then the plan objective's wording wrapping beside it. Chained,
     so it outranks .objective-card__meta (objective_card.css loads later). */
  .objective-card__meta.grant-objective__plan-link {
    flex-wrap: nowrap;
    align-items: baseline;

    & > .badge {
      flex: none;
    }
  }

  /*
   * The plan objectives a grant objective is linked to (020), one row each:
   * level badge, period, wording, and Unlink at the end.
   */
  .grant-objective__plan-objectives {
    grid-column: 1 / -1;
    display: grid;
    gap: var(--space-1);
    margin: 0;
    padding: 0;
    list-style: none;

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

  .grant-objective__plan-objective-item {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    align-items: baseline;
    font: var(--text-small);
  }

  .grant-objective__plan-period {
    color: var(--color-muted);
  }

  .grant-objective__plan-unlink {
    margin-inline-start: auto;
  }

  /* The proposal's own wording, under the title and across the full card. */
  .grant-objective__original-description {
    grid-column: 1 / -1;
    color: var(--color-text);
  }

  .grant-objective__plan-objective {
    color: var(--color-text);
  }

  .grant-key-result-list {
    display: grid;
    gap: var(--space-2);
    list-style: none;
    padding: 0;

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

  /* Wording left, its link state and actions right. */
  .grant-key-result {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto auto;
    align-items: baseline;
    gap: var(--space-3);
    max-inline-size: none;
  }

  .grant-key-result__link {
    font: var(--text-small);
    color: var(--color-muted);
  }

  a.grant-key-result__link {
    color: var(--color-link);
    font-weight: 600;
    text-decoration-line: underline;
  }

  /* In the red of the sidebar's danger actions (.action-list__item--danger):
     a key result the plan cannot see. */
  .grant-key-result__unlinked {
    padding: 0;
    border: 0;
    background: none;
    font: var(--text-small);
    font-weight: 600;
    color: var(--color-negative);
  }

  button.grant-key-result__unlinked {
    text-decoration-line: underline;
    cursor: pointer;
  }

  .grant-key-result__actions,
  .grant-objective__actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2);

    & > * + * {
      margin: 0;
    }
  }

  .grant-objective__budget-total {
    font: var(--text-body-strong);
  }

  .grant-objective__budget-lines {
    margin-block-start: var(--space-2);
    font: var(--text-small);

    & > summary {
      font: var(--text-small);
      color: var(--color-muted);
      cursor: pointer;
    }

    & > h5 {
      margin-block-start: var(--space-3);
    }
  }
}

/*
 * The plan-link popover: a search field over a list of plan objectives,
 * each a button that links it. Wider than the schedule popover, since an
 * objective's wording is a sentence.
 */
@layer components {
  .popover.popover--wide {
    inline-size: 32rem;
  }

  .plan-link-search input[type="search"] {
    inline-size: 100%;
  }

  .plan-link-results {
    display: grid;
    gap: var(--space-1);
    max-block-size: 20rem;
    overflow-y: auto;
    margin-block-start: var(--space-3);
    padding: 0;
    list-style: none;

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

    & form.button_to {
      display: block;
    }

    /* Each option reads as a row of text, not a button. */
    & :is(form.button_to .plan-link-results__option, a.plan-link-results__option) {
      display: block;
      inline-size: 100%;
      padding: var(--space-2) var(--space-3);
      border: 0;
      border-radius: var(--radius-s);
      background: none;
      color: var(--color-text);
      font: var(--text-small);
      text-align: start;
      white-space: normal;
      cursor: pointer;

      &:hover,
      &:focus-visible {
        background: var(--color-surface);
        color: var(--color-ink);
      }

      & .badge {
        margin-inline-end: var(--space-1);
      }
    }
  }

  /* An offered objective's period, between its level badge and wording. */
  .plan-link-results__period {
    margin-inline-end: var(--space-1);
    color: var(--color-muted);
  }

  .plan-link-results__empty {
    margin-block-start: var(--space-3);
    font: var(--text-small);
    color: var(--color-muted);
  }

  .plan-link-unlink {
    margin-block-start: var(--space-3);
  }

  /* The plan objective a key result sits under, below its wording. */
  .plan-link-results__context {
    display: block;
    overflow: hidden;
    color: var(--color-muted);
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  .plan-link-footer {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2);
    margin-block-start: var(--space-3);
  }

  .plan-link-target {
    font: var(--text-small);
  }

  .plan-key-result-form input:is([type="text"], [type="number"]) {
    inline-size: 100%;
  }
}

/*
 * Deadlines: key result cards (.key-result, key_result.css) coloured by
 * state -- orange while open, red once overdue, green when done -- with a
 * completion check before the date and wording. Chained with .key-result,
 * which loads later and would otherwise reset the border.
 */
@layer components {
  .grant-detail__anchor {
    display: block;
    scroll-margin-block-start: var(--space-6);
  }

  .grant-detail__section-header {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--space-3);
    margin-block-end: var(--space-3);

    & > .eyebrow {
      margin: 0;
    }
  }

  .key-result.deadline-card--open {
    background: var(--color-attention-surface);
    border-color: color-mix(in srgb, var(--color-attention) 40%, transparent);
  }

  .key-result.deadline-card--overdue {
    background: var(--color-negative-surface);
    border-color: color-mix(in srgb, var(--color-negative) 40%, transparent);

    & .deadline-card__status {
      color: var(--color-negative);
    }
  }

  .key-result.deadline-card--done {
    background: var(--color-positive-surface);
    border-color: color-mix(in srgb, var(--color-positive) 30%, transparent);
  }

  .deadline-card__title {
    display: flex;
    align-items: baseline;
    gap: var(--space-3);

    & > .task-row__check,
    & > .task-check {
      align-self: center;
    }

    & > :last-child {
      flex: 1;
      min-inline-size: 0;
    }
  }

  /* As wide as the date it shows, like the goal in a key result's form. */
  .key-result-title-form .deadline-card__date {
    field-sizing: content;
    min-inline-size: 0;
    min-block-size: auto;
    margin-inline-start: calc(-1 * var(--space-2));
    padding: var(--space-1) var(--space-2);
    border: 1px solid transparent;
    background: transparent;
    font: var(--text-body);
    font-weight: 600;
    color: var(--color-ink);

    &:hover {
      border-color: color-mix(in srgb, var(--color-text) 25%, transparent);
    }

    &:focus {
      border-color: var(--color-text);
      background: var(--color-paper);
      outline: 0;
    }
  }

  .deadline-card__meta {
    grid-column: 1 / -1;
    font: var(--text-small);
    color: var(--color-muted);
  }

  /* The inline "Add deadline" card: hidden until #new-deadline is the target. */
  .key-result.deadline-card--new {
    display: none;
    margin-block-end: var(--space-2);
    background: var(--color-paper);
    border-style: dashed;
    border-color: var(--color-text);

    :has(#new-deadline:target) & {
      display: block;
    }
  }

  .deadline-new-form {
    display: grid;
    grid-template-columns: 11rem minmax(0, 1fr);
    gap: var(--space-3) var(--space-4);
    align-items: start;

    & > p {
      margin: 0;
      max-inline-size: none;
    }

    & > p:last-child {
      grid-column: 1 / -1;
    }

    & input:not([type="submit"]) {
      inline-size: 100%;
      min-inline-size: 0;
    }
  }
}

/* Policies: the contract's terms, one white card, one term per row. */
@layer components {
  .grant-policies {
    padding: var(--space-4) var(--space-6);
    border: var(--border);
    border-radius: var(--radius-m);
    background: var(--color-paper);

    & > * {
      margin: 0;
    }
  }

  .grant-policy-list {
    list-style: none;
    padding: 0;
  }

  .grant-policy {
    display: flex;
    align-items: start;
    gap: var(--space-3);
    max-inline-size: none;
    padding-block: var(--space-3);

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

    & > .grant-policy__form {
      flex: 1;
    }

    & > form.button_to {
      margin: 0;
    }
  }

  .grant-policy__body {
    margin: 0;
  }

  .grant-policies__empty {
    color: var(--color-muted);
  }

  /* The inline "Add policy" form: hidden until #new-policy is the target. */
  .grant-policy-new-form {
    display: none;
    padding-block-start: var(--space-3);
    border-block-start: var(--border);

    :has(#new-policy:target) & {
      display: block;
    }

    & textarea {
      inline-size: 100%;
    }
  }
}

/*
 * A grant objective's wording edited in place (draft or unlocked): the
 * title in the card title's size, the proposal's original description
 * beneath it in body text, Save beside the title. Chained with
 * .title-form / .title-form__input, which load later.
 */
@layer components {
  .title-form.grant-objective__form {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 0 var(--space-3);

    & > input[type="submit"] {
      grid-column: 2;
      grid-row: 1;
    }
  }

  .title-form__input.grant-objective__title-input {
    font: var(--text-lead);
  }

  .title-form__input.grant-objective__original-input {
    grid-column: 1;
    font: var(--text-body);
    color: var(--color-text);
  }

  .key-result-title-form.grant-key-result__form {
    margin: 0;
  }

  /* The inline "Add key result" card: shown while its anchor is the target. */
  .grant-detail__anchor:target + .key-result.grant-key-result-new {
    display: block;
    margin-block-start: var(--space-3);
  }

  /* The inline "Add objective" card: hidden until #new-objective is the target. */
  .objective-card.grant-objective-new {
    display: none;
    margin-block-end: var(--space-3);
    border-style: dashed;
    border-color: var(--color-text);

    :has(#new-objective:target) & {
      display: block;
    }
  }

  /* Goal and wording only -- no baseline column, unlike the plan's card. */
  .key-result-new-form.grant-key-result-new__form {
    grid-template-columns: 8rem minmax(0, 1fr);
  }

  .grant-objective-new__form {
    & > p {
      max-inline-size: none;
    }

    & textarea {
      inline-size: 100%;
      max-inline-size: none;
    }
  }
}

/*
 * The grant's name and introduction edited in place (draft or unlocked):
 * the name reads as the page title (.title-form__input), the introduction
 * as body text at reading width. Chained, as .title-form loads later.
 */
@layer components {
  .title-form.grant-detail__introduction-form {
    max-inline-size: var(--width-reading);
  }

  .title-form__input.grant-detail__introduction-input {
    font: var(--text-body);
    color: var(--color-text);
    text-wrap: pretty;
  }
}
