@layer components {
  /* Base
  /* -------------------------------------------------------------------------- */

  .card {
    --card-padding-inline: var(--inline-space-double);
    --card-padding-block: var(--block-space);
    --border-color: transparent;
    --border-radius: 0.2em;
    --border-size: 0;

    aspect-ratio: var(--card-aspect-ratio, auto);
    background-color: color-mix(in srgb, var(--card-color) 4%, var(--color-canvas));
    border-radius: var(--border-radius);
    box-shadow: var(--shadow);
    color: color-mix(in srgb, var(--card-color) 40%, var(--color-ink));
    display: flex;
    flex-direction: column;
    inline-size: 100%;
    padding: var(--card-padding-block) var(--card-padding-inline);
    position: relative;
    text-align: start;
    z-index: 1;

    .avatar {
      --avatar-size: 2.75em;
    }

    @media (prefers-color-scheme: dark) {
      box-shadow: 0 0 0 1px var(--color-ink-lighter);
    }
  }

  /* Children
  /* ------------------------------------------------------------------------ */

  .card__background {
    inset: 0;
    position: absolute;
    z-index: -1;

    img {
      block-size: 100%;
      border-radius: var(--border-radius);
      inline-size: 100%;
      object-fit: cover;
      object-position: center;
      opacity: 1;
      transition: filter 0.2s ease-in-out, opacity 0.2s ease-in-out;
    }
  }

  .card__body {
    display: flex;
    flex-grow: 1;
    gap: var(--card-padding-inline);
    inline-size: 100%;
    padding-block: var(--card-padding-block);
  }

  .card__collection {
    background-color: var(--card-color);
    border-radius: var(--border-radius) 0 var(--border-radius) 0;
    color: var(--color-ink-inverted);
    display: inline-flex;
    font-weight: 800;
    max-inline-size: 100%;
    min-inline-size: 0;
    padding: var(--block-space-half) var(--inline-space) var(--block-space-half) var(--card-padding-inline);
  }

  .card__collection-name {
    border-inline-start: 1px solid color-mix(in srgb, var(--color-ink-inverted) 33%, var(--card-color));
    color: currentcolor;
    display: flex;
    margin-inline-start: var(--inline-space-half);
    max-inline-size: 100%;
    min-inline-size: 0;
    padding-inline-start: calc(var(--inline-space) * 0.75);
    text-transform: uppercase;

    .cards--considering & {
      border-inline-start: 1px solid color-mix(in srgb, var(--color-ink) 33%, var(--color-canvas));
      color: color-mix(in srgb, var(--card-color) 40%, var(--color-ink));
    }
  }

  .card__content {
    contain: inline-size;
    flex: 2 1 auto;
    max-inline-size: 100%;
  }

  .card__description {
    house-md-toolbar,
    .house-md-content {
      color: color-mix(in srgb, var(--card-color) 40%, var(--color-ink));
    }

    .house-md-content {
      min-block-size: 3lh;
      padding: var(--block-space-half) var(--inline-space) 0 0;
    }

    & ~ .btn {
      --btn-background: var(--card-color);
      --btn-color: var(--color-ink-inverted);
    }
  }

  .card-field__description {
    lexical-toolbar {
      border-block-start: 1px solid var(--color-ink-light);
      margin-block-start: var(--block-margin) !important;
    }
  }

  .card__header {
    align-items: center;
    border-radius: var(--border-radius) 0 0 0;
    display: flex;
    flex-wrap: nowrap;
    gap: var(--inline-space);
    margin-block-start: calc(-1 * var(--card-padding-block));
    margin-inline-start: calc(-1 * var(--card-padding-inline));
    max-inline-size: 100%;
    min-inline-size: 0;

    .workflow-stage {
      display: none;
    }
  }

  .card__id {
    flex-shrink: 0;
    margin-inline-start: calc(var(--inline-space) * -1);

    .card-perma &:before {
      content: "No.";
      opacity: 0.5;
    }
  }

  .card__link {
    --hover-size: 0;

    content: "";
    inset: 0;
    position: absolute;
    z-index: -1;
  }

  .card__stages {
    display: flex;
    flex: 0 1 auto;
    flex-direction: column;
    justify-self: end;
    max-inline-size: 20ch;
    padding-block: var(--block-space-half);
  }

  .card__tag {
    color: var(--card-color);
    font-weight: 700;
  }

  .card__tags {
    align-items: center;
    color: var(--card-color);
    display: flex;
  }

  .card__title {
    --hover-size: 0;
    --input-border-radius: 0;
    --input-color: color-mix(in srgb, var(--card-color) 40%, var(--color-ink));
    --lines: 3;

    color: inherit;
    font-size: var(--text-xx-large);
    font-weight: 900;
    line-height: 1.2;
    text-wrap: balance;

    @supports not (field-sizing: content) {
      &:has(textarea) {
        text-wrap: unset; /* Safari is annoying if you have text-wrap: balance in textareas */
      }
    }

    .card-field__title {
      &:is(textarea)::placeholder {
        color: inherit;
        opacity: 0.66;
      }
    }

    .card__title-link {
      color: inherit;
    }
  }

  .card--new-card-button:is(.btn) {
    --btn-padding: var(--block-space) var(--inline-space-double);
    --btn-border-size: 0;
    --btn-color: var(--color-link);
    --btn-icon-size: 1em;
    --card-padding-block: calc(var(--block-space) * 1.25);

    box-shadow: 0 0 0 1px color-mix(in srgb, var(--color-link) 20%, var(--color-canvas));
    color: var(--btn-color);
    font-weight: 700;
    transition: none;
  }

  .card__move-button {
    --btn-background: color-mix(in srgb, inherit 40%, var(--color-ink));

    margin-block-start: calc(var(--block-space) * 1.2);
    margin-inline-end: calc(var(--inline-space) * -4);
    padding-inline-end: var(--inline-space);
  }

  .card__meta {
     --border: 1px solid var(--card-color);

    align-items: center;
    display: flex;
    max-inline-size: 100%;
    min-inline-size: 0;
  }

  .card__meta-grid {
    display: grid;
    grid-template-columns: auto auto;
    margin-inline-start: var(--inline-space);
  }

  .card__meta-item {
    text-transform: uppercase;

    strong,
    .local-time-value {
      font-weight: 900;
    }

    &:nth-child(odd) {
      border-inline-end: var(--border);
      padding-inline-end: var(--inline-space);
    }

    &:nth-child(even) {
      padding-inline-start: var(--inline-space);
    }

    /* First two */
    &:nth-child(-n+2) {
      border-block-end: var(--border);
      padding-block-end: 0.25ch;
    }

    /* Last two */
    &:nth-child(n+3) {
      padding-block-start: 0.25ch;
    }
  }

  .card__timestamp {
    opacity: 0.66;
  }

  .card__closed {
    align-items: center;
    aspect-ratio: 8/5;
    border: 0.75ch solid var(--card-color);
    border-radius: 0.2em;
    color: var(--card-color);
    display: flex;
    flex-direction: column;
    font-weight: bold;
    inset: auto var(--block-space) var(--block-space-double) auto;
    justify-content: center;
    max-inline-size: 25ch;
    min-inline-size: 16ch;
    mix-blend-mode: multiply;
    padding: 1ch;
    position: absolute;
    transform: rotate(-10deg);

    @media (prefers-color-scheme: dark) {
      mix-blend-mode: screen;
    }

    .card:has(&),
    .card-perma:has(&) {
      --card-color: var(--color-card-complete) !important;
    }
  }

  .card__closed-title {
    font-size: 1.3em;
    font-weight: 900;
    line-height: 1.2;
    position: relative;
    text-align: center;
    text-transform: uppercase;
  }

  .card__closed-date {
    font-family: var(--font-mono);
    text-transform: uppercase;
  }

  .card__closed-by {
    border-block-end: 1px dashed currentcolor;
  }

  .card:nth-child(2n+1) .bubble { --bubble-rotate: -90deg; }
  .card:nth-child(3n+1) .bubble { --bubble-rotate: 45deg; }

  /* Variants
  /* ------------------------------------------------------------------------ */

  .card--golden.card--doing {
    --lch-golden: 89.1% 0.178 95.7;

    background-color: var(--color-canvas);
    background-image: linear-gradient(60deg,
      oklch(var(--lch-golden) / 0.5) 0%,
      oklch(var(--lch-golden) / 0.15) 33%,
      oklch(var(--lch-golden) / 0.05) 66%,
      oklch(var(--lch-golden) / 0.5) 100%
    );
    box-shadow:
      0 0 0 1px oklch(var(--lch-golden) / 75%),
      0 0 0.5em 0.2em oklch(var(--lch-golden) / 25%),
      0 0 1em 0.8em oklch(var(--lch-golden) / 25%) !important;

    @media (prefers-color-scheme: dark) {
      --lch-golden: var(--lch-blue-medium);
    }
  }

  .card--notification {
    --card-color: var(--color-card-default);
    --card-padding-inline: 1ch;
    --card-padding-block: 1ch;

    background-color: var(--color-canvas);

    &.card--closed {
      --card-color: var(--color-card-complete) !important;
    }

    .card__body {
      padding-block-end: 0;
    }

    .card__collection {
      font-size: var(--text-xx-small);
      padding-block: 0.5ch;
    }

    .card__header {
      margin-block-start: calc(-1.1 * var(--card-padding-block));
      margin-inline: calc(-1 * var(--card-padding-inline));
      max-inline-size: unset;
    }

    .card__notification-body {
      font-size: var(--text-x-small);
    }

    .card__notification-meta {
      font-size: var(--text-xx-small);
      font-weight: 600;
      text-transform: uppercase;
    }

    .card__title {
      font-size: var(--text-small);
      font-weight: bold;
      min-block-size: 0;
    }
  }

  .card__notification-unread-indicator {
    --btn-background: var(--color-canvas);
    --btn-border-color: var(--color-canvas);
    --btn-icon-size: 0.75em;
    --btn-padding: 0;
    --btn-size: 1.25em;
    --hover-size: 0;

    position: relative;
    z-index: 1;

    &:before,
    .icon {
      transition: opacity 150ms ease;
    }

    /* The red dot */
    &:before {
      aspect-ratio: 1;
      background-color: var(--color-marker);
      border-radius: 50%;
      content: "";
      inline-size: 50%;
      inset: 50% auto auto 50%;
      position: absolute;
      translate: -50% -50%;
    }

    /* The X */
    .icon {
      opacity: 0;
    }

    @media (hover: hover) {
      &:hover {
        --btn-background: transparent;

        &:before { opacity: 0; }
           .icon { opacity: 1; }
      }
    }
  }

  .card__collection-public-description {
    max-inline-size: 66ch;

    p:first-child {
      margin-block-start: 0;
    }

    p:last-child {
      margin-block-end: 0;
    }
  }

  .card--search {
    --card-color: var(--color-ink);
    --card-padding-block: 0.75rem;
    --card-padding-inline: 0.75rem;

    background-color: var(--color-canvas);

    &:not(&:hover) {
      box-shadow: 0 0 0 1px var(--color-ink-lighter);
    }

    .card__collection {
      background-color: var(--color-canvas);
      color: var(--color-ink-medium);
      font-size: var(--text-x-small);
      font-weight: 600;
      padding-block-end: 1ch;
    }

    .card__id {
      margin: 0;
    }

    .card__body {
      padding: 0;
    }

    .card__title {
      font-size: var(--text-medium);
    }
  }
}
