/* Sequencer well and grid: a piano roll of step columns, every step parameter visible at once.
   Layouts: one row of 16 or 32 steps (wide), 32 steps wrapped by 16 (< 960 px), by 8 (< 600 px).
   A row-label column precedes every group of 8 steps in the DOM; each layout shows the ones it needs. */

@layer layout {
  .seq__well {
    padding: var(--sp-3) var(--sp-4) var(--sp-4);
    border-radius: var(--r-3);
    background-color: var(--c-well);
    box-shadow: var(--shadow-well);
    min-inline-size: 0;
  }

  .grid {
    --cols: 16;
    display: grid;
    grid-template-columns: var(--keys-w) repeat(var(--cols), minmax(0, 1fr));
    column-gap: var(--cell-gap);
    row-gap: var(--sp-5);
    touch-action: pan-y; /* a horizontal stroke paints, a vertical one scrolls the page on touch */

    &[data-length="32"] {
      --cols: 32;
    }
  }

  /* everything on one screen: the rows share the height that is left */
  @media (width >= 1200px) and (height >= 700px) {
    :root {
      --cell-h: clamp(14px, calc((100dvh - 420px) / 18), 26px);
    }
  }

  @media (width < 960px) {
    .grid[data-length="32"] {
      --cols: 16;
    }
    :root {
      --cell-h: 21px;
    }
  }

  @media (width < 600px) {
    :root {
      --keys-w: 46px;
      --cell-h: 22px;
    }
    .seq__well {
      padding: var(--sp-2) var(--sp-3) var(--sp-3);
    }
    .grid,
    .grid[data-length="32"] {
      --cols: 8;
    }
  }
}

@layer components {
  .keys,
  .step {
    display: flex;
    flex-direction: column;
    gap: var(--section-gap);
    min-inline-size: 0;
  }

  .keys__notes,
  .keys__octave,
  .keys__flags,
  .step__notes,
  .step__octave,
  .step__flags {
    display: flex;
    flex-direction: column;
    gap: var(--cell-gap);
  }

  /* ── row labels ── */
  .keys__head,
  .step__num {
    block-size: var(--num-h);
    font-family: var(--font-mono);
    font-size: var(--fs-label);
    line-height: var(--num-h);
  }
  .keys__head {
    font-family: var(--font-ui);
    font-weight: 700;
    letter-spacing: var(--track-label);
    color: var(--grey-13);
  }

  .key {
    display: flex;
    align-items: center;
    block-size: var(--cell-h);
    padding-inline: var(--sp-2);
    border-radius: var(--r-1);
    font-size: var(--fs-label);
    font-weight: 700;
    letter-spacing: 0.04em;
    white-space: nowrap;
    overflow: hidden;
    color: var(--grey-14);
    cursor: default;
  }
  .key--white {
    background-color: var(--c-key-white);
    background-image: linear-gradient(90deg, var(--grey-14), var(--grey-16) 30%, var(--grey-15));
    color: var(--c-key-white-text);
    box-shadow: 0 1px 0 var(--grey-12) inset;
  }
  .key--black {
    inline-size: 74%;
    background-color: var(--c-key-black);
    background-image: linear-gradient(90deg, var(--grey-2), var(--grey-6));
    color: var(--c-key-black-text);
    box-shadow: 0 0 0 1px var(--grey-9) inset;
  }
  .keys__octave .key,
  .keys__flags .key {
    font-size: 9px;
    letter-spacing: 0.06em;
    color: var(--grey-15);

    @media (width < 600px) {
      padding-inline: 1px;
      font-size: 8.5px;
      letter-spacing: 0.02em;
    }
  }

  /* ── step columns ── */
  .step {
    position: relative;

    /* beat (every 4 steps) and bar (every 16) lines in the column gap */
    &.is-beat::before {
      content: "";
      position: absolute;
      inset-block: 0;
      inset-inline-start: calc(-1 * var(--cell-gap));
      inline-size: var(--cell-gap);
      background-color: var(--c-beat-line);
      pointer-events: none;
    }
    &.is-bar::before {
      background-color: var(--c-bar-line);
    }

    /* the playing step: one lit column */
    &.is-playing::after {
      content: "";
      position: absolute;
      inset: calc(var(--num-h) + var(--section-gap)) -1px 0;
      border-radius: var(--r-1);
      background-color: rgb(255 255 255 / 0.09);
      box-shadow: 0 0 0 1px rgb(255 255 255 / 0.32);
      pointer-events: none;
    }
  }

  .step__num {
    text-align: center;
    color: var(--grey-13);
    border-radius: var(--r-1);
  }
  .step.is-beat .step__num {
    color: var(--grey-17);
    font-weight: 700;
  }
  .step.is-playing .step__num {
    background-color: var(--grey-19);
    color: var(--grey-0);
    font-weight: 700;
    box-shadow: 0 0 10px rgb(255 255 255 / 0.45);
  }

  /* ── cells ── */
  .cell {
    position: relative;
    display: block;
    inline-size: 100%;
    block-size: var(--cell-h);
    border-radius: var(--r-1);
    background-color: var(--c-cell);
    cursor: pointer;
    touch-action: pan-y;

    &.cell--black {
      background-color: var(--c-cell-black);
    }

    &.is-ghost {
      box-shadow: 0 0 0 1px var(--c-cell-ghost) inset;
    }

    &:focus-visible {
      z-index: 1;
      outline: 2px solid var(--c-focus);
      outline-offset: 1px;
    }

    &[aria-pressed="true"] {
      background-color: var(--c-cell-on);
      background-image: linear-gradient(180deg, var(--grey-19), var(--grey-16));
      box-shadow: 0 0 6px rgb(255 255 255 / 0.22), 0 -2px 0 var(--grey-13) inset;
    }
  }

  .step.is-offbeat .cell:not([aria-pressed="true"]) {
    background-color: var(--c-cell-offbeat);

    &.cell--black {
      background-color: var(--c-cell-black-offbeat);
    }
  }

  /* function rows: their own lit shapes */
  .cell--octave[aria-pressed="true"] {
    background-image: linear-gradient(180deg, var(--grey-16), var(--grey-14));
  }
  .cell--octave[data-value="1"]::after,
  .cell--octave[data-value="-1"]::after {
    content: "";
    position: absolute;
    inset-inline-start: 50%;
    inset-block-start: 50%;
    inline-size: 0;
    block-size: 0;
    border-inline: 4px solid transparent;
    transform: translate(-50%, -50%);
    pointer-events: none;
  }
  .cell--octave[data-value="1"]::after {
    border-block-end: 5px solid var(--grey-11);
  }
  .cell--octave[data-value="-1"]::after {
    border-block-start: 5px solid var(--grey-11);
  }
  .cell--octave[aria-pressed="true"]::after {
    border-block-end-color: var(--grey-2);
    border-block-start-color: var(--grey-2);
  }

  .cell--accent[aria-pressed="true"] {
    background-image: linear-gradient(180deg, var(--grey-19), var(--grey-18));
    box-shadow: 0 0 9px rgb(255 255 255 / 0.4);
  }

  /* slide: a tie reaching into the next step */
  .cell--slide[aria-pressed="true"] {
    z-index: 1;
    background-image: none;
    background-color: var(--grey-10);
    box-shadow: none;

    &::after {
      content: "";
      position: absolute;
      inset-block: 25%;
      inset-inline: 22% calc(-1 * var(--cell-gap) - 22%);
      border-radius: 999px;
      background-color: var(--grey-18);
      pointer-events: none;
    }
  }

  /* where the next step is not to the right (end of the pattern, end of a wrapped row) the tie
     stops at the cell edge with a square end, as if it continued off the row */
  .step.is-last .cell--slide[aria-pressed="true"]::after {
    inset-inline-end: 0;
    border-start-end-radius: 0;
    border-end-end-radius: 0;
  }
  @media (width < 960px) {
    .grid[data-length="32"] .step.is-end-16 .cell--slide[aria-pressed="true"]::after {
      inset-inline-end: 0;
      border-start-end-radius: 0;
      border-end-end-radius: 0;
    }
  }
  @media (width < 600px) {
    .step.is-end-8 .cell--slide[aria-pressed="true"]::after {
      inset-inline-end: 0;
      border-start-end-radius: 0;
      border-end-end-radius: 0;
    }
  }

  .cell--gate[aria-pressed="true"] {
    background-image: linear-gradient(180deg, var(--grey-17), var(--grey-15));
  }

  .step.is-playing .cell[aria-pressed="true"]:not(.cell--slide) {
    background-image: linear-gradient(180deg, var(--grey-19), var(--grey-19));
    box-shadow: 0 0 12px rgb(255 255 255 / 0.55);
  }
}

@layer states {
  @media (hover: hover) and (pointer: fine) {
    .cell:not([aria-pressed="true"]):hover {
      background-color: var(--c-cell-hover);
    }
    .step.is-offbeat .cell:not([aria-pressed="true"]):hover {
      background-color: var(--c-cell-hover);
    }
  }
}

/* which row-label columns a layout shows (after the column styles, so it wins) */
@layer states {
  .keys:not(.keys--at-0) {
    display: none;
  }
  @media (width < 960px) {
    .grid[data-length="32"] .keys--at-16 {
      display: flex;
    }
  }
  @media (width < 600px) {
    .grid .keys--at-8,
    .grid .keys--at-16,
    .grid .keys--at-24 {
      display: flex;
    }
  }
}
