/* the index: one line per post on one paper card, the preview unfolding under the row you point
   at. its head flips it between columns (list) and a timeline, where each post is a thread from
   first seen to last edited with a dot at every version, sized by how much changed */

[data-look="index"] {
  --c-blocks: 4.5rem;
  --c-name: 14rem;
  --c-when: 16.5rem;
  --c-count: 2.5rem;
  --c-edits: 6rem;
  --gap: var(--s6);
  /* kind, title, first seen – last edited, versions, edits */
  --cols: var(--c-blocks) minmax(0, 1fr) var(--c-when) var(--c-count) var(--c-edits);
  /* where the title starts, so the preview sits under it */
  --lead: calc(var(--c-blocks) + var(--gap));

  /* one paper card, as wide as the header and legend above it, in both modes */
  .grid {
    display: flex;
    flex-direction: column;
    padding: var(--s2) var(--s4) var(--s4);
    background: var(--paper);
  }

  .grid-item {
    position: static;
    width: auto !important;
    transform: none !important;
  }

  .grid-item + .grid-item .card { border-top: 1px dashed var(--faint); }

  .card {
    background: none;
    padding: var(--s3) 0;
    gap: 0;
  }

  .card .edge { display: none; }

  /* a row: one type scale. the title says it, the hand notes when and how often */

  .index-row {
    display: grid;
    grid-template-columns: var(--cols);
    align-items: center;
    gap: var(--gap);
  }

  .index-row .titled { display: block; min-width: 0; }

  .index-row :is(.when, .count) {
    font-family: var(--hand);
    font-size: var(--h-xs);
    line-height: 1;
    color: var(--dim);
    white-space: nowrap;
  }

  /* first seen and, once it has changed since, last edited, as one span */
  .index-row .when {
    display: flex;
    align-items: baseline;
    gap: var(--s2);
  }

  .index-row .when i { font-style: normal; color: var(--faint); }

  /* versions and edits, as a pair so a phone can give them a line of their own */
  .index-row .tally { display: contents; }

  .index-row .count { justify-self: end; }

  /* edit bars: words added above the line, removed below, one column per version */
  .edits { display: block; flex: none; overflow: visible; justify-self: start; }
  .edits line { stroke: var(--rule); stroke-width: 1; }
  .edits .add { fill: var(--ink); }
  .edits .del { fill: var(--pale); }
  .edits .origin rect { fill: none; stroke: var(--ink); stroke-width: 1; }

  /* pointed at, the title glows; when and how often stay as they were */
  .card:hover .name { text-shadow: var(--glow); }

  /* the head: the switch, and the months when it's a timeline */

  .axis {
    position: sticky;
    top: 0;
    z-index: 1;
    display: grid;
    grid-template-columns: 1fr;
    align-items: end;
    gap: var(--s2) var(--gap);
    padding: var(--s3) 0 var(--s2);
    background: var(--paper);
    border-bottom: 1px dashed var(--rule);
  }

  .index-switch .flip { display: block; color: var(--ink); }

  /* only a phone scrolls the timeline; elsewhere the scroller is no box at all */
  .tl-scroll { display: contents; }

  .tl-track {
    position: relative;
    height: 28px;
    margin-right: var(--s3);
  }

  /* two lines: a year where one starts, over the month */
  .axis .tl-track { height: 30px; }

  .axis .tl-track span {
    position: absolute;
    bottom: 0;
    padding-left: var(--s1);
    border-left: 1px dashed var(--rule);
    font-family: var(--hand);
    font-size: var(--h-xs);
    line-height: 1;
    color: var(--dim);
    white-space: nowrap;
  }

  .axis .tl-track .year { display: block; font-weight: inherit; }

  /* a post's thread and its dots */
  .span {
    position: absolute;
    top: 50%;
    border-top: 1px dashed var(--ink);
  }

  .mark {
    position: absolute;
    top: 50%;
    width: var(--d);
    height: var(--d);
    border-radius: 50%;
    transform: translate(-50%, -50%);
  }

  .card:hover .mark { border-color: var(--ink); }

  /* list: the columns, no track */

  &[data-index="list"] :is(.tl-track, .tl-scroll) { display: none; }

  /* timeline: squares and title, then the track; the dates and columns step aside */

  &[data-index="timeline"] {
    .index-row { grid-template-columns: var(--c-blocks) var(--c-name) 1fr; }

    .index-row :is(.when, .count, .edits) { display: none; }

    /* the title's column is narrow here, so it keeps to one line until the row is pointed at */
    .index-row .name {
      display: block;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }

    .index-row .name .phrase { display: inline; }

    :is(.card:hover, a:focus-visible) .index-row .name { white-space: normal; }
    :is(.card:hover, a:focus-visible) .index-row .name .phrase { display: inline-block; }

    /* with no hovering to open it, or no room to keep it to one line, the title wraps from the start */
    @media (hover: none), (max-width: 700px) {
      .index-row .name { white-space: normal; }
      .index-row .name .phrase { display: inline-block; }
    }

    .axis { grid-template-columns: calc(var(--c-blocks) + var(--gap) + var(--c-name)) 1fr; }
  }

  /* the preview, under the title in both modes: the picture on its own line, then the opening.
     only a pointer unfolds it; a tap on a row is for opening the post */

  .unfold {
    display: grid;
    grid-template-rows: 0fr;
    transition: grid-template-rows 0.35s ease;
  }

  .unfold > div {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    overflow: hidden;
    min-height: 0;
    padding-left: var(--lead);
  }

  @media (hover: hover) {
    .card:hover .unfold { grid-template-rows: 1fr; }
  }

  a:focus-visible .unfold { grid-template-rows: 1fr; }

  .unfold .excerpt {
    width: 100%;
    max-width: 40rem;
    margin: var(--s3) 0 var(--s2);
    max-height: calc(1.6em * 4);
  }

  .unfold img {
    flex: none;
    width: auto;
    max-width: 10rem;
    max-height: 12rem;
    margin: var(--s3) 0 var(--s2);
  }

  .unfold img + .excerpt { margin-top: 0; }

  /* the timeline's rows as tall as the list's, so flipping between them keeps the same padding */
  @media (min-width: 701px) {
    .index-row .tl-track { height: auto; }
  }

  /* narrow desktops: tighter columns so the title keeps its room, last edited under first seen */
  @media (min-width: 701px) and (max-width: 1000px) {
    --c-blocks: 3.5rem;
    --c-when: 9.5rem;
    --c-edits: 3rem;
    --gap: var(--s4);

    .index-row .when { flex-wrap: wrap; row-gap: var(--s1); }
  }

  /* phones: compact rows with no preview, a tap opening the post. the list puts its title over
     when and how often, the timeline its title over the track */
  @media (max-width: 700px) {
    --lead: 0px;

    .index-row .titled { order: -1; grid-column: 1 / -1; }

    /* a line each: the title, the squares, the dates, then versions and edits */
    &[data-index="list"] .index-row {
      grid-template-columns: minmax(0, 1fr);
      justify-items: start;
      gap: var(--s2);

      .tally {
        grid-column: 1 / -1;
        display: flex;
        align-items: center;
        gap: var(--s3);
      }

      .tally:not(:has(.count:not(:empty), .edits:not(:empty))) { display: none; }
    }

    /* the title on its own line, then the squares in a fixed column with the track beside them,
       the switch sitting in that same column over the months */
    &[data-index="timeline"] {
      .index-row {
        grid-template-columns: var(--c-blocks) minmax(0, 1fr);
        gap: var(--s2) 0;
      }

      .index-row .tally { display: none; }

      .axis { grid-template-columns: var(--c-blocks) minmax(0, 1fr); gap: 0; }

      /* the months and tracks are wider than the screen, room for every month's label, and scroll
         sideways together (js/looks/index.js keeps them in step); an edge fades while there's more
         past it */
      .tl-scroll {
        display: flex;
        min-width: 0;
        overflow-x: auto;
        overscroll-behavior-x: contain;
        scrollbar-width: none;
        -webkit-mask-image: linear-gradient(to right, transparent, #000 var(--fade-l, 0px), #000 calc(100% - var(--fade-r, 0px)), transparent);
        mask-image: linear-gradient(to right, transparent, #000 var(--fade-l, 0px), #000 calc(100% - var(--fade-r, 0px)), transparent);
      }
    }

    .tl-scroll::-webkit-scrollbar { display: none; }
    .tl-track { flex: none; width: calc(var(--months, 12) * 4.5rem); margin: 0 var(--s3); }
    .index-row .tl-track { height: 20px; }
    .unfold { display: none; }
  }
}
