$api-color-header-bg: var(--md-code-bg-color);
$api-color-keyword: var(--md-code-hl-keyword-color);
$api-color-name: var(--md-code-hl-name-color);
$api-color-pre-name: var(--md-code-hl-name-color);
$api-color-type: var(--md-code-hl-special-color);
$api-color-param-name: var(--md-default-fg-color--light);
$objinfo-icon-size: 16px;

// Wrap in .md-typeset to ensure rules below have higher selectivity
// than those in _typeset.scss.
.md-typeset {
  :is(dl.objdesc, dl.api-field) > dt {
    font-family: var(--md-code-font-family);
    background: $api-color-header-bg;

    // Eliminate the normal padding for inline code blocks
    code {
      padding: 0;
      border-radius: 0;
    }

    .sig-name:not(.sig-name-nonprimary) {
      padding: 0;
      color: $api-color-name;
      font-weight: 700;
    }

    .sig-param {
      font-style: normal;
    }

    .sig-param .n:not(.desctype) {
      color: $api-color-param-name;
    }

    .sig-param a.reference .n:not(.desctype):hover {
      color: var(--md-accent-fg-color);
    }

    // Ensure each parameter starts on a new line and is indented.
    &.sig-wrap {
      .sig-param-decl {
        &::before {
          white-space: pre;
          // 5 spaces below result in 4 spaces of indent.
          // For some reason one space is lost.
          content: "\A     ";
        }
      }

      // Note: It would be nice to have something like
      // `:last-of-class` here, but that doesn't exist.
      .sig-paren ~ .sig-paren {
        // Ensure the final paren starts on a new line
        &::before {
          white-space: pre;
          content: "\A";
        }
      }
    }
  }

  // Rules for coloring cross-references in signatures.
  :is(dl.objdesc, dl.api-field) > dt,
  .sig-inline {
    a.reference > .sig-name,
    a.reference.sig-name,
    a.reference:not(.desctype) > .n {
      color: var(--md-typeset-a-color);

      &:hover {
        color: var(--md-accent-fg-color);
      }
    }

    .desctype {
      &,
      > a.reference {
        color: $api-color-type;
        // Prevent pygments highlight rules from taking precedence.
        .n {
          color: inherit;
        }
      }

      &:is(a.reference):hover,
      > a.reference:hover {
        color: var(--md-accent-fg-color);
      }
    }
  }

  dl.objdesc {
    > dt {
      padding-top: 0.5em;
      padding-right: 0.5em;
      padding-left: 0.5em;
      font-family: var(--md-code-font-family);
      background: $api-color-header-bg;

      &,
      code {
        font-size: 0.75rem;
      }

      // Prefix like "class", "staticmethod".
      .property {
        color: $api-color-keyword;
        font-weight: 700;
        font-style: normal;
      }

      .sig-prename {
        padding: 0;
        color: $api-color-pre-name;
      }

      .viewcode-link,
      .viewcode-back {
        float: right;
        text-align: right;
      }

      &.api-include-path {
        &,
        code {
          font-size: 0.65rem;
        }
      }
    }

    > dt:first-child {
      padding-top: 0.5em;
    }

    > dt:last-of-type {
      padding-bottom: 0.5em;
    }

    > dd {
      // Heading like "Parameters" or "Returns"
      dl.field-list {
        > dt {
          margin-bottom: 1em;
          font-weight: 700;
          font-size: 1em;
        }
      }
      // JSON domain uses noindent class to avoid an extra level of
      // nesting.
      dd.noindent {
        margin-left: 0;
      }
    }
  }

  dl.api-field {
    > dt {

      // Display as table so that background is just the width of the
      // content.
      display: table;

      // Prevent permalink symbol from having a background color of $api-color-header-bg.
      a.headerlink {
        position: relative;
        left: 0.5em;
        width: 0;
        margin-left: 0;
      }

      &,
      code {
        font-size: 0.65rem;
      }

      &.api-parameter-kind {
        float: right;
        font-family: var(--md-text-font-family);

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

        &::after {
          content: "]";
        }
      }
    }
  }

  // Make object summaries more compact
  dl.objdesc.summary > dd {
    &,
    > p:first-child {
      margin-top: 0;
    }
  }

  // C/C++ inline "text" expressions: should have regular, rather than
  // "code", font face.
  .sig-inline:is(.c-texpr, .cpp-texpr) {
    font-family: unset;
    background-color: unset;
  }
}

.md-nav__link {
  white-space: nowrap;
}

:root {
  > * {
    --objinfo-icon-fg-alias: #{$clr-orange-900};
    --objinfo-icon-fg-default: #{$clr-grey-800};
    --objinfo-icon-fg-data: #{$clr-blue-800};
    --objinfo-icon-fg-procedure: #{$clr-purple-800};
    --objinfo-icon-fg-sub-data: #{$clr-green-800};
    --objinfo-icon-bg-default: var(--md-default-bg-color);
  }
}

// Only use dark mode on screens
@media screen {

  // Slate theme, i.e. dark mode
  [data-md-color-scheme="slate"] {
    --objinfo-icon-fg-alias: #{$clr-orange-300};
    --objinfo-icon-fg-default: #{$clr-grey-300};
    --objinfo-icon-fg-data: #{$clr-blue-300};
    --objinfo-icon-fg-procedure: #{$clr-purple-200};
    --objinfo-icon-fg-sub-data: #{$clr-green-300};
  }
}

.objinfo-icon {
  display: inline-block;
  flex-shrink: 0;
  width: $objinfo-icon-size;
  height: $objinfo-icon-size;
  margin-right: 8px;
  color: var(--objinfo-icon-fg-default);
  font-weight: 500;
  font-family: var(--md-text-font-family);
  line-height: $objinfo-icon-size;
  text-align: center;
  vertical-align: middle;
  background-color: var(--objinfo-icon-bg-default);
  border: 1px solid var(--objinfo-icon-fg-default);
  border-radius: 2px;

  @each $objclass in (alias, procedure, data, sub-data) {
    &__#{$objclass} {
      color: var(--objinfo-icon-bg-default);
      background-color: var(--objinfo-icon-fg-#{$objclass});
      border: 1px solid var(--objinfo-icon-fg-#{$objclass});
    }
  }
}

.search-result-objlabel {
  float: right;
  padding: 2px;
  border: 1px solid var(--md-default-fg-color--light);
  border-radius: 2px;
}

// a rule specifically designed for autosummary tables
// this overrides `.md-typeset code { word-break: break-word; }`
table.longtable.docutils.data.align-default {
  tbody > tr > td > p > a.reference.internal {
    > code.xref.py.py-obj.docutils.literal.notranslate {
      > span.pre {
        word-break: normal;
      }
    }
  }
}
