@layer properties, theme, base, components, utilities;

/* =========================================================================
   Data tables - markup from ui.table / ui.sort_link (app/helpers/ui_builder/data_display.rb)

   .figma-table-wrap            scroll container: border, 16px radius, sideways scroll with
                                edge shadows that appear only while there is more to see
     -wrap-flush                no border/radius (table inside a card with padding: :none)
     .figma-table-sticky-first  first / last column stays put while the rest scrolls (solid
     .figma-table-sticky-last   cells, 1px edge rule, shadow while content hides under it);
                                not below a 28rem container (phones), where it all scrolls
   .figma-table                 the table; -compact tightens the padding
     cells: .is-num (right, tabular figures) .is-center .is-nowrap .is-actions (right, shrink)
     .figma-table-empty         the full-width "no rows" cell (its wrapper then measures itself)
     .figma-table-hide-{n}-{bp} hides column n while the nearest @container (the shell's <main>)
                                is narrower than bp: md 28rem, xl 36rem, 3xl 48rem, 4xl 56rem,
                                5xl 64rem, 6xl 72rem (ui.table hide_below:)
   .figma-table-sort            sortable header link
   Keyboard focus on the wrapper (caption: regions) draws a 2px ring inside it, also over
   pinned cells; a pinned cell holding an open menu paints above the rows below it.

   Everything sits in the components layer, so Tailwind utilities on the cells still win.
   ========================================================================= */
@layer components {
  .figma-table-wrap {
    position: relative;
    max-width: 100%;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    border: 1px solid #E9E9E9;
    border-radius: 16px;
    background-color: #FFFFFF;
    /* Scroll-edge shadows: the white covers travel with the content (local) and hide the
       edge shadows (fixed to the scrollport: scroll) until content is hidden on that side. */
    background-image:
      linear-gradient(to right, #FFFFFF 40%, rgb(255 255 255 / 0)),
      linear-gradient(to left, #FFFFFF 40%, rgb(255 255 255 / 0)),
      linear-gradient(to right, rgb(15 23 42 / 0.14), rgb(15 23 42 / 0)),
      linear-gradient(to left, rgb(15 23 42 / 0.14), rgb(15 23 42 / 0));
    background-position: left center, right center, left center, right center;
    background-repeat: no-repeat;
    background-size: 40px 100%, 40px 100%, 14px 100%, 14px 100%;
    background-attachment: local, local, scroll, scroll;
  }

  .figma-table-wrap-flush {
    border: 0;
    border-radius: 0;
  }

  /* Keyboard focus (caption: makes the wrapper a focusable region; Chrome also focuses
     scrollers without focusable content). The ring is drawn inside the box, so a card's
     overflow: hidden can't clip it. A flush table normally fills a ui.card, so while focused
     it takes the card's inner corner radius and the ring follows the card's rounded corners.
     Pinned cells paint over the outline and draw their own stretch of the ring ("Focus ring
     over pinned cells"), sized by these two properties. */
  .figma-table-wrap {
    --figma-table-ring-width: 1px; /* the outline's other 1px sits on the 1px border */
    --figma-table-ring-radius: 15px; /* the 16px corner inside the border */
  }

  .figma-table-wrap-flush {
    --figma-table-ring-width: 2px;
    --figma-table-ring-radius: calc(var(--figma-radius-card, 20px) - 1px);
  }

  .figma-table-wrap:focus-visible {
    outline: 2px solid var(--figma-primary, #007BFF);
    outline-offset: -2px;
  }

  .figma-table-wrap-flush:focus-visible {
    border-radius: var(--figma-table-ring-radius);
  }

  .figma-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: 14px;
    line-height: 20px;
    color: #212121;
    text-align: left;
  }

  /* Header cells. The tint is translucent (#F9FAFB over white) so the edge shadows show through. */
  .figma-table > thead > tr > th {
    padding: 12px 16px;
    background-color: rgb(105 130 155 / 0.04);
    border-bottom: 1px solid #E9E9E9;
    font-size: 12px;
    font-weight: 500;
    line-height: 16px;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    text-align: left;
    vertical-align: middle;
    white-space: nowrap;
    color: #6B6B6B;
  }

  /* Body cells (th scope="row" reads like a cell with medium weight). */
  .figma-table > tbody > tr > :is(td, th) {
    padding: 14px 16px;
    border-top: 1px solid #F0F0F0;
    text-align: left;
    vertical-align: middle;
  }

  .figma-table > tbody > tr > th {
    font-weight: 500;
  }

  .figma-table > tbody > tr:first-child > :is(td, th) {
    border-top: 0;
  }

  /* Row hover: #FAFBFC over white, translucent for the same reason as the header. */
  .figma-table > tbody > tr:hover > :is(td, th):not(.figma-table-empty) {
    background-color: rgb(5 55 105 / 0.02);
  }

  .figma-table > tfoot > tr > :is(td, th) {
    padding: 12px 16px;
    border-top: 1px solid #E9E9E9;
    font-weight: 500;
    vertical-align: middle;
  }

  /* Compact density */
  .figma-table-compact > thead > tr > th {
    padding: 8px 12px;
  }

  .figma-table-compact > :is(tbody, tfoot) > tr > :is(td, th) {
    padding: 10px 12px;
  }

  /* Cell modifiers (on th or td) */
  .figma-table :is(th, td).is-num {
    text-align: right;
    font-variant-numeric: tabular-nums;
  }

  .figma-table :is(th, td).is-center {
    text-align: center;
  }

  .figma-table :is(th, td).is-nowrap {
    white-space: nowrap;
  }

  .figma-table :is(th, td).is-actions {
    width: 1%;
    text-align: right;
    white-space: nowrap;
  }

  /* Empty state row: the content brings its own padding and stays in view while the
     header row scrolls sideways. An empty table's wrapper measures itself (an inline-size
     container), so the content is exactly as wide as the visible part of the wrapper,
     wherever the table sits (padded card, side column, dialog). Only while it is empty, so
     hide_below: and the pinned columns of a table with rows keep following the nearest
     outer container (and a wrapper with rows never becomes a containing block). */
  .figma-table > tbody > tr > .figma-table-empty {
    padding: 0;
  }

  .figma-table-empty > * {
    position: sticky;
    left: 0;
    max-width: calc(100cqw - 2rem); /* no :has(): the shell's <main> minus the page gutter */
  }

  /* Inline-size containment leaves out the content's width, so wherever the width comes
     from the content (w-fit, inline-block, a content-sized flex item or items-start column,
     an auto grid track, an absolutely positioned panel) an empty table would collapse to
     its border. It gets a width of its own instead: 24rem, but no more than 75% of the
     nearest outer container (cq units on a container resolve against the container around
     it, else the viewport), leaving room for page gutters and card padding on phones and in
     dialogs. Where the wrapper stretches (block flow, ui.card, 1fr tracks) nothing changes. */
  .figma-table-wrap:has(> .figma-table > tbody > tr > .figma-table-empty) {
    container-type: inline-size;
    contain-intrinsic-inline-size: min(24rem, 75cqw);
  }

  .figma-table-wrap:has(> .figma-table > tbody > tr > .figma-table-empty) .figma-table-empty > * {
    max-width: 100cqw;
  }

  .figma-table-empty-text {
    margin: 0;
    padding: 24px 16px;
    text-align: center;
    font-size: 14px;
    color: #6B6B6B;
  }

  /* Sticky first / last column: solid cells (header tint and hover included) so the
     scrolling content never shows through, plus a 1px rule on the inner edge. */
  .figma-table-sticky-first > .figma-table > :is(thead, tbody, tfoot) > tr > :first-child:not(.figma-table-empty),
  .figma-table-sticky-last > .figma-table > :is(thead, tbody, tfoot) > tr > :last-child:not(.figma-table-empty) {
    position: sticky;
    z-index: 1;
  }

  .figma-table-sticky-first > .figma-table > :is(thead, tbody, tfoot) > tr > :first-child:not(.figma-table-empty) {
    left: 0;
    box-shadow: inset -1px 0 0 #E9E9E9;
  }

  .figma-table-sticky-last > .figma-table > :is(thead, tbody, tfoot) > tr > :last-child:not(.figma-table-empty) {
    right: 0;
    box-shadow: inset 1px 0 0 #E9E9E9;
  }

  .figma-table-sticky-first > .figma-table > :is(tbody, tfoot) > tr > :first-child:not(.figma-table-empty),
  .figma-table-sticky-last > .figma-table > :is(tbody, tfoot) > tr > :last-child:not(.figma-table-empty) {
    background-color: #FFFFFF;
  }

  .figma-table-sticky-first > .figma-table > thead > tr > :first-child,
  .figma-table-sticky-last > .figma-table > thead > tr > :last-child {
    background-color: #F9FAFB;
  }

  .figma-table-sticky-first > .figma-table > tbody > tr:hover > :first-child:not(.figma-table-empty),
  .figma-table-sticky-last > .figma-table > tbody > tr:hover > :last-child:not(.figma-table-empty) {
    background-color: #FAFBFC;
  }

  /* Every pinned cell is its own stacking context with the same z-index, so the pinned cells
     of later rows would paint over a menu opened from an earlier row (ui.dropdown_menu row
     actions). The cell holding an open menu, or keyboard focus, is lifted above them.
     :has() because Safari doesn't focus a button on click. */
  .figma-table-sticky-first > .figma-table > :is(thead, tbody, tfoot) > tr > :first-child:not(.figma-table-empty):is(:focus-within, :has([aria-expanded="true"])),
  .figma-table-sticky-last > .figma-table > :is(thead, tbody, tfoot) > tr > :last-child:not(.figma-table-empty):is(:focus-within, :has([aria-expanded="true"])) {
    z-index: 2;
  }

  /* Focus ring over pinned cells: pinned cells paint above the wrapper's inset outline, so
     while the wrapper has keyboard focus each one draws its stretch of the ring - the outer
     edge, the top of the header cell and the bottom of the last row's cell, rounded where
     they meet the wrapper's corners. Only while columns are pinned (the same container
     condition as the un-pinning below). */
  @container (width >= 28rem) {
    .figma-table-wrap:focus-visible.figma-table-sticky-first > .figma-table > :is(thead, tbody, tfoot) > tr > :first-child:not(.figma-table-empty)::after,
    .figma-table-wrap:focus-visible.figma-table-sticky-last > .figma-table > :is(thead, tbody, tfoot) > tr > :last-child:not(.figma-table-empty)::after {
      content: "";
      position: absolute;
      inset: 0;
      border: 0 solid var(--figma-primary, #007BFF);
      pointer-events: none;
    }

    .figma-table-wrap:focus-visible.figma-table-sticky-first > .figma-table > :is(thead, tbody, tfoot) > tr > :first-child:not(.figma-table-empty)::after {
      border-left-width: var(--figma-table-ring-width);
    }

    .figma-table-wrap:focus-visible.figma-table-sticky-last > .figma-table > :is(thead, tbody, tfoot) > tr > :last-child:not(.figma-table-empty)::after {
      border-right-width: var(--figma-table-ring-width);
    }

    .figma-table-wrap:focus-visible.figma-table-sticky-first > .figma-table > thead > tr:first-child > :first-child::after,
    .figma-table-wrap:focus-visible.figma-table-sticky-last > .figma-table > thead > tr:first-child > :last-child::after {
      border-top-width: var(--figma-table-ring-width);
    }

    .figma-table-wrap:focus-visible.figma-table-sticky-first > .figma-table > :is(:is(tbody, tfoot):last-child, thead:has(+ tbody:empty)) > tr:last-child > :first-child:not(.figma-table-empty)::after,
    .figma-table-wrap:focus-visible.figma-table-sticky-last > .figma-table > :is(:is(tbody, tfoot):last-child, thead:has(+ tbody:empty)) > tr:last-child > :last-child:not(.figma-table-empty)::after {
      border-bottom-width: var(--figma-table-ring-width);
    }

    .figma-table-wrap:focus-visible.figma-table-sticky-first > .figma-table > thead > tr:first-child > :first-child::after {
      border-top-left-radius: var(--figma-table-ring-radius);
    }

    .figma-table-wrap:focus-visible.figma-table-sticky-last > .figma-table > thead > tr:first-child > :last-child::after {
      border-top-right-radius: var(--figma-table-ring-radius);
    }

    .figma-table-wrap:focus-visible.figma-table-sticky-first > .figma-table > :is(:is(tbody, tfoot):last-child, thead:has(+ tbody:empty)) > tr:last-child > :first-child:not(.figma-table-empty)::after {
      border-bottom-left-radius: var(--figma-table-ring-radius);
    }

    .figma-table-wrap:focus-visible.figma-table-sticky-last > .figma-table > :is(:is(tbody, tfoot):last-child, thead:has(+ tbody:empty)) > tr:last-child > :last-child:not(.figma-table-empty)::after {
      border-bottom-right-radius: var(--figma-table-ring-radius);
    }
  }

  /* A pinned column covers the wrapper's own edge shadow on its side, so the pinned cells
     cast the shadow themselves: driven by the horizontal scroll where supported (shown only
     while content is hidden underneath), otherwise always on. */
  @supports (animation-timeline: scroll()) {
    .figma-table-wrap {
      scroll-timeline: --figma-table-scroll inline;
    }

    .figma-table-sticky-first > .figma-table > :is(thead, tbody, tfoot) > tr > :first-child:not(.figma-table-empty) {
      animation: figma-table-edge-first linear both;
      animation-timeline: --figma-table-scroll;
    }

    .figma-table-sticky-last > .figma-table > :is(thead, tbody, tfoot) > tr > :last-child:not(.figma-table-empty) {
      animation: figma-table-edge-last linear both;
      animation-timeline: --figma-table-scroll;
    }
  }

  @supports not (animation-timeline: scroll()) {
    .figma-table-sticky-first > .figma-table > :is(thead, tbody, tfoot) > tr > :first-child:not(.figma-table-empty) {
      box-shadow: inset -1px 0 0 #E9E9E9, 12px 0 12px -6px rgb(15 23 42 / 0.12);
    }

    .figma-table-sticky-last > .figma-table > :is(thead, tbody, tfoot) > tr > :last-child:not(.figma-table-empty) {
      box-shadow: inset 1px 0 0 #E9E9E9, -12px 0 12px -6px rgb(15 23 42 / 0.12);
    }
  }

  @keyframes figma-table-edge-first {
    0% { box-shadow: inset -1px 0 0 #E9E9E9, 12px 0 12px -6px rgb(15 23 42 / 0); }
    6%, 100% { box-shadow: inset -1px 0 0 #E9E9E9, 12px 0 12px -6px rgb(15 23 42 / 0.12); }
  }

  @keyframes figma-table-edge-last {
    0%, 94% { box-shadow: inset 1px 0 0 #E9E9E9, -12px 0 12px -6px rgb(15 23 42 / 0.12); }
    100% { box-shadow: inset 1px 0 0 #E9E9E9, -12px 0 12px -6px rgb(15 23 42 / 0); }
  }

  /* Phone-width panels (the nearest @container under 28rem) don't pin columns: two pinned
     columns, or one wide action column, would leave no room for the rest. The table scrolls
     sideways as a whole there, with the wrapper's own edge shadows. */
  @container (width < 28rem) {
    .figma-table-sticky-first > .figma-table > :is(thead, tbody, tfoot) > tr > :first-child:not(.figma-table-empty),
    .figma-table-sticky-last > .figma-table > :is(thead, tbody, tfoot) > tr > :last-child:not(.figma-table-empty) {
      position: static;
      box-shadow: none;
      animation: none;
    }
  }

  /* Sortable header link (ui.sort_link): 24px tall hit area without growing the row. */
  .figma-table-sort {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding-block: 4px;
    margin-block: -4px;
    border-radius: 4px;
    color: inherit;
    text-decoration: none;
    cursor: pointer;
  }

  .figma-table-sort:hover,
  .figma-table-sort.is-active {
    color: #212121;
  }

  .figma-table-sort:focus-visible {
    outline: 2px solid var(--figma-primary, #007BFF);
    outline-offset: 2px;
  }

  /* hide_below: - one rule per column (2-12) and container breakpoint. The header and every
     cell in that position hide together, including rows added later by Turbo Streams.
     Tables using these classes must not use colspan cells (the empty row is fine). */
  @container (width < 28rem) {
    .figma-table-hide-2-md tr > :nth-child(2),
    .figma-table-hide-3-md tr > :nth-child(3),
    .figma-table-hide-4-md tr > :nth-child(4),
    .figma-table-hide-5-md tr > :nth-child(5),
    .figma-table-hide-6-md tr > :nth-child(6),
    .figma-table-hide-7-md tr > :nth-child(7),
    .figma-table-hide-8-md tr > :nth-child(8),
    .figma-table-hide-9-md tr > :nth-child(9),
    .figma-table-hide-10-md tr > :nth-child(10),
    .figma-table-hide-11-md tr > :nth-child(11),
    .figma-table-hide-12-md tr > :nth-child(12) {
      display: none;
    }
  }

  @container (width < 36rem) {
    .figma-table-hide-2-xl tr > :nth-child(2),
    .figma-table-hide-3-xl tr > :nth-child(3),
    .figma-table-hide-4-xl tr > :nth-child(4),
    .figma-table-hide-5-xl tr > :nth-child(5),
    .figma-table-hide-6-xl tr > :nth-child(6),
    .figma-table-hide-7-xl tr > :nth-child(7),
    .figma-table-hide-8-xl tr > :nth-child(8),
    .figma-table-hide-9-xl tr > :nth-child(9),
    .figma-table-hide-10-xl tr > :nth-child(10),
    .figma-table-hide-11-xl tr > :nth-child(11),
    .figma-table-hide-12-xl tr > :nth-child(12) {
      display: none;
    }
  }

  @container (width < 48rem) {
    .figma-table-hide-2-3xl tr > :nth-child(2),
    .figma-table-hide-3-3xl tr > :nth-child(3),
    .figma-table-hide-4-3xl tr > :nth-child(4),
    .figma-table-hide-5-3xl tr > :nth-child(5),
    .figma-table-hide-6-3xl tr > :nth-child(6),
    .figma-table-hide-7-3xl tr > :nth-child(7),
    .figma-table-hide-8-3xl tr > :nth-child(8),
    .figma-table-hide-9-3xl tr > :nth-child(9),
    .figma-table-hide-10-3xl tr > :nth-child(10),
    .figma-table-hide-11-3xl tr > :nth-child(11),
    .figma-table-hide-12-3xl tr > :nth-child(12) {
      display: none;
    }
  }

  @container (width < 56rem) {
    .figma-table-hide-2-4xl tr > :nth-child(2),
    .figma-table-hide-3-4xl tr > :nth-child(3),
    .figma-table-hide-4-4xl tr > :nth-child(4),
    .figma-table-hide-5-4xl tr > :nth-child(5),
    .figma-table-hide-6-4xl tr > :nth-child(6),
    .figma-table-hide-7-4xl tr > :nth-child(7),
    .figma-table-hide-8-4xl tr > :nth-child(8),
    .figma-table-hide-9-4xl tr > :nth-child(9),
    .figma-table-hide-10-4xl tr > :nth-child(10),
    .figma-table-hide-11-4xl tr > :nth-child(11),
    .figma-table-hide-12-4xl tr > :nth-child(12) {
      display: none;
    }
  }

  @container (width < 64rem) {
    .figma-table-hide-2-5xl tr > :nth-child(2),
    .figma-table-hide-3-5xl tr > :nth-child(3),
    .figma-table-hide-4-5xl tr > :nth-child(4),
    .figma-table-hide-5-5xl tr > :nth-child(5),
    .figma-table-hide-6-5xl tr > :nth-child(6),
    .figma-table-hide-7-5xl tr > :nth-child(7),
    .figma-table-hide-8-5xl tr > :nth-child(8),
    .figma-table-hide-9-5xl tr > :nth-child(9),
    .figma-table-hide-10-5xl tr > :nth-child(10),
    .figma-table-hide-11-5xl tr > :nth-child(11),
    .figma-table-hide-12-5xl tr > :nth-child(12) {
      display: none;
    }
  }

  @container (width < 72rem) {
    .figma-table-hide-2-6xl tr > :nth-child(2),
    .figma-table-hide-3-6xl tr > :nth-child(3),
    .figma-table-hide-4-6xl tr > :nth-child(4),
    .figma-table-hide-5-6xl tr > :nth-child(5),
    .figma-table-hide-6-6xl tr > :nth-child(6),
    .figma-table-hide-7-6xl tr > :nth-child(7),
    .figma-table-hide-8-6xl tr > :nth-child(8),
    .figma-table-hide-9-6xl tr > :nth-child(9),
    .figma-table-hide-10-6xl tr > :nth-child(10),
    .figma-table-hide-11-6xl tr > :nth-child(11),
    .figma-table-hide-12-6xl tr > :nth-child(12) {
      display: none;
    }
  }
}
