/* The list, like a pinboard: pinned keeps on colored cards with a slight tilt, then every keep by when it was kept,
   each on a sand card with its picture, title, source, a line or two about it, and what kind it is */

@layer components {
  .pinned,
  .period {
    width: min(var(--page-width), 100% - 2 * var(--page-padding));
    margin-inline: auto;
  }

  .pinned {
    padding-top: 56px;

    @media (max-width: 699px) {
      padding-top: 32px;
    }
  }

  /* Three to a row, or a row that scrolls sideways on a phone */
  .pins {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr));
    gap: 32px 22px;
    padding-top: 36px;

    @media (max-width: 699px) {
      display: flex;
      gap: 16px;
      /* Room for the cards' shadows, which the scrolling row would otherwise cut off in a hard line */
      width: calc(100% + 2 * var(--page-padding));
      margin: -20px calc(-1 * var(--page-padding)) -52px;
      padding: 40px var(--page-padding) 80px;
      overflow-x: auto;
      overscroll-behavior-x: contain;
      scroll-snap-type: x mandatory;
      scroll-padding-inline: var(--page-padding);
      scrollbar-width: none;
    }
  }

  .pin {
    position: relative;
    rotate: var(--pin-tilt, 0deg);
    height: 384px;
    overflow: clip;
    border: 1px solid rgb(0 0 0 / 10%);
    border-radius: 32px;
    background: var(--tint);
    box-shadow: var(--shadow-pin);
    color: var(--tint-ink);
    transition: rotate 220ms var(--ease-out-expo);

    &:nth-child(6n + 1) { --pin-tilt: 1.3deg; }
    &:nth-child(6n + 2) { --pin-tilt: -0.7deg; }
    &:nth-child(6n + 3) { --pin-tilt: 1deg; }
    &:nth-child(6n + 4) { --pin-tilt: -1.4deg; }
    &:nth-child(6n + 5) { --pin-tilt: 0.8deg; }
    &:nth-child(6n + 6) { --pin-tilt: -1deg; }

    /* Straightens up under the pointer */
    @media (hover: hover) and (pointer: fine) {
      &:hover {
        rotate: 0deg;
      }
    }

    @media (max-width: 699px) {
      flex: 0 0 300px;
      height: 356px;
      border-radius: 28px;
      scroll-snap-align: start;
    }

    @media (prefers-reduced-motion: reduce) {
      transition: none;
    }

    .source {
      color: color-mix(in srgb, currentColor var(--tint-secondary, 76%), transparent);
    }

    /* Gold, in line with the kind and the time */
    .star {
      position: absolute;
      top: 20px;
      right: 18px;

      @media (max-width: 699px) {
        top: 16px;
        right: 14px;
      }
    }
  }

  .pin__link {
    display: flex;
    flex-direction: column;
    height: 100%;
    padding: 28px 28px 0;

    @media (max-width: 699px) {
      padding: 24px 24px 0;
    }
  }

  .pin__meta {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 28px;
    padding-right: 40px;

    .label-chip {
      background: color-mix(in srgb, currentColor 10%, transparent);
    }

    time {
      margin-left: auto;
      font: 400 var(--text-label) / 1.3 var(--font-mono);
      letter-spacing: 0.02em;
      color: color-mix(in srgb, currentColor var(--tint-secondary, 76%), transparent);
    }
  }

  .pin__title {
    display: -webkit-box;
    overflow: hidden;
    padding: 20px 0 4px;
    font-size: var(--text-pin-title);
    line-height: 1.12;
    font-weight: 600;
    letter-spacing: -0.04em;
    overflow-wrap: anywhere;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;

    @media (max-width: 699px) {
      padding-top: 16px;
    }
  }

  .pin__description {
    display: -webkit-box;
    overflow: hidden;
    padding-top: 12px;
    font-size: var(--text-pin-body);
    line-height: 1.41;
    font-weight: 500;
    letter-spacing: -0.01em;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;

    @media (max-width: 699px) {
      padding-top: 10px;
    }
  }

  /* The picture fills what's left, running off the card's bottom edge */
  .pin__picture {
    flex: 1 0 0;
    min-height: 0;
    padding-top: 24px;

    img {
      width: 100%;
      height: 176px;
      border-radius: 16px 16px 0 0;
      background: color-mix(in srgb, currentColor 10%, transparent);
      object-fit: cover;
    }
  }

  /* Keeps in periods of time, with the days they cover across from the heading */
  .period__header {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    justify-content: space-between;
    gap: 4px 16px;
    padding-top: 96px;

    @media (max-width: 699px) {
      padding-top: 48px;
    }
  }

  .filters + .period .period__header {
    padding-top: 56px;

    @media (max-width: 699px) {
      padding-top: 32px;
    }
  }

  .period__dates {
    color: var(--color-ink-secondary);
    font: 400 var(--text-label) / 1.6 var(--font-mono);
    letter-spacing: 0.06em;
    text-transform: uppercase;
  }

  .period__dates--short {
    display: none;
  }

  @media (max-width: 699px) {
    .period__dates--long {
      display: none;
    }

    .period__dates--short {
      display: inline;
    }
  }

  .keeps {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr));
    gap: 16px;
    padding-top: 32px;

    @media (max-width: 699px) {
      gap: 12px;
      padding-top: 16px;
    }
  }

  /* Sorted by title, the list has no periods to sit in */
  .keeps-screen > turbo-frame > .keeps {
    width: min(var(--page-width), 100% - 2 * var(--page-padding));
    margin-inline: auto;
    padding-top: 56px;
  }

  /* The star sits in the card's top right corner, beside its link rather than in it */
  .keep {
    position: relative;

    .star {
      position: absolute;
      top: 14px;
      right: 14px;

      @media (max-width: 699px) {
        top: 10px;
        right: 10px;
      }
    }
  }

  .keep__link {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 232px;
    padding: 24px;
    border-radius: var(--radius-card);
    background: var(--color-card);
    transition: background-color 150ms ease, scale 160ms var(--ease-out-expo);

    /* The whole card, the star included, so the card doesn't lighten again on the way to its star */
    @media (hover: hover) and (pointer: fine) {
      .keep:hover & {
        background: color-mix(in srgb, var(--color-card), var(--color-ink) 4%);
      }
    }

    &:active {
      scale: 0.985;
    }

    @media (max-width: 699px) {
      min-height: 0;
      padding: 20px;
    }
  }

  .keep__head {
    display: flex;
    align-items: start;
    gap: 16px;
    padding-right: 36px;

    @media (max-width: 699px) {
      gap: 14px;
    }
  }

  /* An auto column would grow to fit a long site name on its one line, pushing the text, and the page,
     wider than the screen */
  .keep__heading {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 4px;
    flex: 1;
    min-width: 0;
  }

  .keep__title {
    display: -webkit-box;
    overflow: hidden;
    font-size: var(--text-card-title);
    line-height: 1.17;
    font-weight: 600;
    letter-spacing: -0.03em;
    overflow-wrap: anywhere;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .keep__description {
    display: -webkit-box;
    overflow: hidden;
    padding-top: 12px;
    font-size: var(--text-card-body);
    line-height: 1.38;
    font-weight: 500;
    letter-spacing: -0.01em;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .keep__footer {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: auto;
    padding-top: 16px;

    time {
      margin-left: auto;
      color: var(--color-ink-secondary);
      font: 400 var(--text-label) / 1.3 var(--font-mono);
      font-variant-numeric: tabular-nums;
      white-space: nowrap;
    }
  }

  /* A keep that just finished reading settles into place */
  .keep--arrived .keep__link {
    animation: arrive 1.4s var(--ease-out-expo);
  }

  .keep--arrived :is(.keep__heading, .keep__description, .thumb) {
    animation: settle 420ms var(--ease-out-expo);
  }

  .thumb {
    display: grid;
    flex-shrink: 0;
    place-items: center;
    width: calc(var(--thumb-size) * 1.5);
    height: var(--thumb-size);
    overflow: hidden;
    border-radius: var(--radius-thumb);
    background: var(--color-thumb);
    color: var(--color-ink);

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }

    .icon {
      width: 24px;
      height: 24px;
      stroke-width: 1.7;
    }

    @media (max-width: 699px) {
      border-radius: 10px;
    }
  }

  .thumb--cover img {
    width: auto;
    object-fit: contain;
    box-shadow: 1px 2px 3px rgb(0 0 0 / 12%);
  }

  /* Reading: the loop draws itself while Keepazoid reads the link */
  .thumb--reading .loop {
    width: 30px;
    height: 18px;
    stroke-width: 3.2;
    stroke-dasharray: 112;
    animation: read 1.6s cubic-bezier(0.45, 0, 0.2, 1) infinite;
  }

  .empty {
    padding: 64px var(--page-padding) 0;
    text-align: center;

    h2 {
      font-size: var(--text-heading);
      letter-spacing: -0.04em;
    }

    p {
      margin: 12px 0 24px;
      color: var(--color-ink-secondary);
      font: 400 var(--text-label) / 1.6 var(--font-mono);
      letter-spacing: 0.02em;
      overflow-wrap: anywhere;
    }
  }

  /* Sign Out, for the app, which has no header */
  .list-end {
    display: grid;
    justify-items: center;
    padding: 48px var(--page-padding) 44px;
  }

  [data-hotwire-native="false"] .list-end {
    display: none;
  }

  .keeps-screen {
    padding-bottom: 96px;
  }
}

@keyframes read {
  0% { stroke-dashoffset: 112; }
  55% { stroke-dashoffset: 0; }
  100% { stroke-dashoffset: -112; }
}

@keyframes arrive {
  0% { background: var(--color-coral-wash); }
}

@keyframes settle {
  from { opacity: 0.2; filter: blur(3px); }
  to { opacity: 1; filter: blur(0); }
}

@media (prefers-reduced-motion: reduce) {
  .thumb--reading .loop, .keep--arrived :is(.keep__link, .keep__heading, .keep__description, .thumb) {
    animation: none;
  }

  .keep__link:active {
    scale: none;
  }
}
