@layer components {
  .invoices-page {
    padding-block-end: 3rem;
  }

  .invoices-header__btn-icon {
    font-size: 1.125rem;
    font-weight: 300;
    line-height: 1;
  }

  .invoices-body {
    padding-block-start: 2.5rem;
  }

  .invoices-toolbar {
    align-items: center;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    margin-block-end: 2rem;
    row-gap: 0.75rem;
  }

  /* The filter grows into the free space (`flex: 1`), but never below its
     widest chip: at zero width its chips, aligned to its end, would spill
     back over the tabs. */
  .invoices-toolbar .inv-filter {
    min-inline-size: min-content;
  }

  .invoices-toolbar__manage {
    background: none;
    border: none;
    color: var(--color-brand);
    cursor: pointer;
    flex-shrink: 0;
    font-size: 0.9375rem;
    font-weight: 600;
    margin-inline-start: auto;
    padding: 0;
    white-space: nowrap;
  }

  .invoices-manage {
    align-items: center;
    display: none;
    gap: 0.75rem;
    margin-inline-start: 1rem;
  }

  /* The group of actions (Copy / Export / Delete), on a row of its own,
     aligned right (under "Done"), so it forms one coherent block instead of
     buttons scattered across rows. */
  .invoices-manage__actions {
    align-items: center;
    display: flex;
    flex-basis: 100%;
    flex-wrap: wrap;
    gap: 0.75rem 0.6rem;
    justify-content: flex-end;
    order: 2;
  }

  /* "Done" lands exactly where "Manage" was, at the end of the first row, so
     switching modes doesn't move the control elsewhere. */
  .invoices-manage__btn[data-action="manage#exit"] {
    margin-inline-start: auto;
    order: 1;
  }

  .invoices-body.is-managing .invoices-toolbar__manage {
    display: none;
  }

  /* The manage container is "dissolved", its buttons flowing straight in the
     toolbar: row 1 = tabs, filter … Done, row 2 = the actions. */
  .invoices-body.is-managing .invoices-manage {
    display: contents;
  }

  /* Only the group of actions slides in — "Done" merely takes the place of
     "Manage" in the same spot, so it deliberately doesn't animate and doesn't
     jump on the switch. The fade is on the wrapper (not the buttons), so it
     doesn't override the greying of disabled buttons (`:disabled` → 0.45). */
  .invoices-body.is-managing .invoices-manage__actions {
    animation: invoices-manage-in-stack 0.3s cubic-bezier(0.16, 1, 0.3, 1) both;
  }

  /* With room to spare — a wide display and no chip of the filter in use —
     the actions join the first row ahead of "Done", sliding in from the side.
     They may shrink and wrap: should a longer label ("Preparing export…") or
     a longer translation outgrow the row, they fold under themselves and
     "Done" keeps its place. */
  @media (width >= 1280px) {
    .invoices-toolbar:not(:has(.inv-chip:not([hidden]))) {
      flex-wrap: nowrap;

      .invoices-manage__actions {
        flex-basis: auto;
        gap: 0.75rem;
        order: 0;
      }
    }

    .invoices-body.is-managing .invoices-toolbar:not(:has(.inv-chip:not([hidden]))) {
      .invoices-manage {
        display: flex;
      }

      .invoices-manage__actions {
        animation-name: invoices-manage-in;
      }
    }
  }

  @keyframes invoices-manage-in {
    from {
      opacity: 0;
      transform: translateX(8px);
    }
    to {
      opacity: 1;
      transform: translateX(0);
    }
  }

  /* The actions arrive on a row of their own — a plain fade with no slide from
     the side, which only makes sense in the one-row layout. */
  @keyframes invoices-manage-in-stack {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }

  .invoices-manage__btn {
    background: transparent;
    color: var(--color-ink-secondary);
    padding: 0.5rem 1.5rem;
    transition: background 0.15s, border-color 0.15s, color 0.15s, opacity 0.15s;
  }

  .invoices-manage__btn--danger {
    border-color: var(--color-negative-border);
    color: var(--color-negative-strong);
  }

  .invoices-manage__btn--danger:hover:not(:disabled) {
    background: var(--color-negative-tint);
    border-color: var(--color-negative-border);
  }

  /* While the print run is put together in the background: the disabled
     "Print" with a brand sheen sweeping across it, left to right, at full
     strength rather than faded like any disabled button. The gradient is
     three times the button's width with the sheen in its middle third, so
     both ends of the sweep sit off the button and the loop has no seam. */
  .invoices-manage__print[aria-busy="true"] {
    &, &:hover {
      animation: invoices-manage-print-busy 1.8s linear infinite;
      background: linear-gradient(90deg,
        var(--color-canvas) 35%, color-mix(in oklch, var(--color-brand) 22%, var(--color-canvas)) 50%, var(--color-canvas) 65%);
      background-size: 300% 100%;
      color: var(--color-ink-secondary);
      cursor: progress;
      opacity: 1;
    }
  }

  @keyframes invoices-manage-print-busy {
    from { background-position: 100% 0; }
    to   { background-position: 0 0; }
  }

  /* Split control: language select + export confirm rendered as one button */
  .invoices-manage__export {
    display: flex;
  }

  .invoices-manage__export-select {
    -webkit-appearance: none;
    appearance: none;
    background-color: transparent;
    background-image: var(--icon-chevron);
    background-position: right 0.8rem center;
    background-repeat: no-repeat;
    border: 1.5px solid var(--color-border);
    border-inline-end: none;
    border-radius: var(--radius-pill) 0 0 var(--radius-pill);
    color: var(--color-ink-secondary);
    cursor: pointer;
    font-size: 0.9375rem;
    font-weight: 600;
    padding: 0.5rem 1.9rem 0.5rem 1.25rem;
    transition: background-color 0.15s, border-color 0.15s;
  }

  .invoices-manage__export-select:hover {
    background-color: var(--color-surface-tint);
    border-color: var(--color-border-strong);
  }

  .invoices-manage__export-btn {
    border-radius: 0 var(--radius-pill) var(--radius-pill) 0;
  }

  .doc-check {
    align-items: center;
    display: flex;
    justify-content: center;
    opacity: 0;
    overflow: hidden;
    transition: opacity 0.25s ease;
  }

  .invoices-body.is-managing .doc-check {
    opacity: 1;
  }

  .invoices-body.is-managing .doc-headers,
  .invoices-body.is-managing .doc-row {
    --doc-check-size: 44px;
  }

  .doc-checkbox {
    accent-color: var(--color-brand);
    block-size: 1.125rem;
    cursor: pointer;
    inline-size: 1.125rem;
    margin: 0;
  }

  /* A turbo-frame is inline by default. */
  #documents {
    display: block;
  }

  /* The grid is the checkbox column, closed until the list is being managed,
     then the side's own columns: variants redefine the two variables, never
     the declaration, so a list with a column of its own (`bills.css`) keeps it
     in every mode and at every width. */
  .doc-headers {
    display: grid;
    grid-template-columns: var(--doc-check-size, 0px) var(--doc-columns, repeat(5, 1fr));
    margin-block-end: 0.75rem;
    padding: 0 1.5rem;
    transition: grid-template-columns 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  }

  .doc-header {
    text-align: center;
    white-space: nowrap;
  }

  .doc-header:nth-child(2) {
    text-align: start;
  }

  .doc-table {
    background: var(--color-surface);
    border-radius: var(--radius-md);
    overflow: hidden;
  }

  .doc-row {
    align-items: center;
    border-block-end: 1px solid var(--color-border-subtle);
    display: grid;
    grid-template-columns: var(--doc-check-size, 0px) var(--doc-columns, repeat(5, 1fr));
    padding: 1.125rem 1.5rem;
    transition: grid-template-columns 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  }

  .doc-row:last-child {
    border-block-end: none;
  }

  .doc-cell {
    color: var(--color-ink-secondary);
    font-size: 1.125rem;
    text-align: center;
  }

  .doc-cell--number {
    text-align: start;
  }

  .doc-cell--number a {
    color: var(--color-brand);
    font-family: var(--font-mono);
    font-size: 1.125rem;
    font-weight: 600;
    text-decoration: none;
  }

  .doc-cell--number a:hover {
    text-decoration: underline;
  }

  /* What a row is when it is not a plain invoice — an advance invoice, a
     credit note — named after its number. */
  .doc-kind {
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    color: var(--color-ink-secondary);
    display: inline-block;
    font-size: 0.6875rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    margin-inline-start: 0.5rem;
    padding: 0.125rem 0.375rem;
    text-transform: uppercase;
    vertical-align: middle;
    white-space: nowrap;
  }

  /* The office's code inside the number: a tinted chip the digits sit in,
     its full name the tooltip. */
  .doc-number__series {
    background: var(--color-brand-tint);
    border-radius: var(--radius-xs);
    margin-inline: 0.125em;
    padding: 0.1em 0.2em;
  }

  .doc-cell--client {
    color: var(--color-ink);
    font-size: 1.125rem;
    font-weight: 700;
    text-align: center;
  }

  /* Truncated client name — a click expands it in full, and it may then cover
     the amount */
  .doc-cell--client.is-truncatable {
    cursor: pointer;
  }

  .doc-cell--client.is-expanded {
    overflow: visible;
    position: relative;
    text-align: start;
    z-index: 3;
  }

  .doc-cell--client.is-expanded .doc-cell--client__text {
    background: var(--color-surface);
    white-space: nowrap;
  }

  .doc-col--amount {
    text-align: end;
  }

  /* The sum below the table. Aligned right, not tied to the rows' grid — that
     changes its column count per breakpoint, while the right edge always
     holds. */
  .doc-total {
    align-items: baseline;
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem 0.75rem;
    justify-content: flex-end;
    padding: 1.125rem 1.5rem 0;
  }

  .doc-total__amount {
    color: var(--color-brand);
    font-family: var(--font-mono);
    font-size: 1.125rem;
    font-weight: 700;
  }

  .doc-cell.doc-col--amount {
    color: var(--color-ink);
    font-family: var(--font-mono);
    font-size: 1.125rem;
    font-weight: 700;
  }

  .doc-header__short {
    display: none;
  }

  .doc-empty {
    color: var(--color-ink-muted);
    font-size: 0.9375rem;
    padding: 3rem 1.5rem;
    text-align: center;
  }

  @media (width < 768px) {
    .doc-col--date {
      display: none;
    }

    /* Search is hidden — keep the toolbar's height as in managing mode so the
       list doesn't shift on the switch and the text stays on one line.
       flex-wrap + row-gap: on narrow displays the filter and the manage group
       wrap onto rows of their own instead of overflowing to the right. */
    .invoices-toolbar {
      /* `center`: "Manage" is plain text, "Done" a pill — centring the row
         puts them in the same spot, so switching modes doesn't move them.
         `align-content: flex-start`, so the single row (outside managing
         mode) doesn't stretch to `min-height` and shift them by 2px. */
      align-content: flex-start;
      align-items: center;
      column-gap: 0.6rem;
      justify-content: flex-start;
      min-block-size: 2.5rem;
    }

    /* Order within the group: the two narrow pills side by side, the wider
       export after them — on a phone it wraps under them, on a wider display
       it stays on the same row. */
    .invoices-manage__btn:is([data-action="manage#copy"], [data-action="manage#print"]) {
      order: 1;
    }

    .invoices-manage__btn--danger {
      order: 2;
    }

    .invoices-manage__export {
      order: 3;
    }

    .doc-headers,
    .doc-row {
      --doc-columns: repeat(3, 1fr);
    }

    .invoices-body.is-managing .doc-headers,
    .invoices-body.is-managing .doc-row {
      --doc-check-size: 56px;
    }

    .doc-header:nth-child(2) {
      text-align: start;
    }

    .doc-header.doc-col--amount {
      text-align: end;
    }

    .doc-cell--number {
      text-align: start;
    }

    .doc-cell.doc-col--amount {
      text-align: end;
    }
  }

  @media (width < 480px) {
    /* Toolbar — search lives in the navbar, only the manage buttons remain */
    .invoices-toolbar {
      margin-block-end: 1.25rem;
    }

    .invoices-manage {
      flex-wrap: wrap;
      gap: 0.5rem;
      margin-inline-start: 0;
    }

    .invoices-manage__btn {
      font-size: 0.875rem;
      padding: 0.4375rem 1rem;
    }

    .invoices-manage__export-select {
      font-size: 0.875rem;
      padding: 0.4375rem 1.75rem 0.4375rem 1rem;
    }

    /* Table */
    .doc-headers,
    .doc-row {
      --doc-columns: repeat(3, 1fr);

      padding-inline-end: 0.875rem;
      padding-inline-start: 0.875rem;
    }

    .invoices-body.is-managing .doc-headers,
    .invoices-body.is-managing .doc-row {
      --doc-check-size: 44px;
    }

    /* Same side padding as the rows, so the sum lines up with the amounts
       above it. */
    .doc-total {
      padding-inline-end: 0.875rem;
      padding-inline-start: 0.875rem;
    }

    .doc-cell,
    .doc-cell--number a,
    .doc-cell--client,
    .doc-cell.doc-col--amount,
    .doc-total__amount {
      font-size: 0.9375rem;
    }

    /* Keep long client names from overflowing */
    .doc-cell--client {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .doc-header {
      text-align: center;
    }

    .doc-header:nth-child(2) {
      text-align: start;
    }

    .doc-header.doc-col--amount {
      text-align: end;
    }

    .doc-cell--number {
      text-align: start;
    }

    .doc-cell.doc-col--amount {
      text-align: end;
    }

    .doc-header__full {
      display: none;
    }

    .doc-header__short {
      display: inline;
    }
  }


  /* ─── A bill received over Peppol ───────── */

  .doc-row__badge {
    background: var(--color-brand-tint);
    border-radius: var(--radius-pill);
    color: var(--color-brand);
    font-size: 0.6875rem;
    font-weight: 600;
    margin-inline-start: 0.375rem;
    padding: 0.0625rem 0.4375rem;
    vertical-align: middle;
  }

}

@layer components {

  /* ─── The issued documents' columns ──────────── */

  /* Invoices, advance invoices and credit notes: the number with its kind,
     the client over how far the document got to them, the two dates, and
     the amount over where its payment stands. Read from the left like a
     table, the amount held to the right. */
  .doc-headers.doc-headers--issued,
  .doc-row.doc-row--issued {
    --doc-columns: minmax(0, 1.4fr) minmax(0, 1.7fr) minmax(0, 0.9fr) minmax(0, 1fr) minmax(0, 1.3fr);
  }

  /* A document listed under the one heading its group — a credit note
     under its invoice, an advance invoice's tax documents and final
     invoice under it — set in and quieter. */
  .doc-row--nested {
    background: var(--color-surface-tint);

    .doc-cell--number {
      padding-inline-start: 1.25rem;
    }

    .doc-cell--number a {
      font-size: 1rem;
    }

    @media (width < 768px) {
      .doc-cell--number {
        padding-inline-start: 0.75rem;
      }
    }
  }

  .doc-headers--issued .doc-header,
  .doc-row--issued .doc-cell {
    text-align: start;
  }

  .doc-headers--issued .doc-header.doc-col--amount,
  .doc-row--issued .doc-cell.doc-col--amount {
    text-align: end;
  }

  /* A document whose client is not filled in yet, named by what it is. */
  .doc-cell--client__text--blank {
    color: var(--color-ink-muted);
  }

  .doc-delivery {
    --doc-delivery-ink: var(--color-ink-muted);

    color: var(--doc-delivery-ink);
    display: block;
    font-size: 0.875rem;
    font-weight: 500;
    margin-block-start: 0.125rem;
  }

  .doc-delivery--positive {
    --doc-delivery-ink: var(--color-positive);
  }

  .doc-delivery--brand {
    --doc-delivery-ink: var(--color-brand);
  }

  .doc-delivery--negative {
    --doc-delivery-ink: var(--color-negative);
  }

  .doc-due {
    display: flex;
    flex-direction: column;
  }

  .doc-due__note {
    color: var(--color-ink-muted);
    font-size: 0.8125rem;
  }

  .doc-due--overdue {
    color: var(--color-negative);

    .doc-due__note {
      color: var(--color-negative);
      font-weight: 700;
    }
  }

  .doc-amount {
    display: block;
  }

  .doc-amount--voided {
    color: var(--color-ink-muted);
    text-decoration: line-through;
  }

  /* The state in words beside a bar of what has come in. */
  .doc-payment {
    --doc-payment-ink: var(--color-ink-muted);

    align-items: center;
    color: var(--doc-payment-ink);
    display: flex;
    font-family: var(--font-sans);
    font-size: 0.875rem;
    font-weight: 500;
    gap: 0.75rem;
    justify-content: flex-end;
    margin-block-start: 0.25rem;
    white-space: nowrap;
  }

  .doc-payment--positive {
    --doc-payment-ink: var(--color-positive);
  }

  .doc-payment--negative {
    --doc-payment-ink: var(--color-negative);
  }

  .doc-payment__bar {
    background: linear-gradient(to right, var(--color-positive) var(--doc-payment-share, 0%), var(--color-border-subtle) 0);
    block-size: 0.3125rem;
    border-radius: var(--radius-pill);
    flex: none;
    inline-size: 4.5rem;
  }

  @media (width < 768px) {
    .doc-headers.doc-headers--issued,
    .doc-row.doc-row--issued {
      --doc-columns: minmax(0, 1fr) minmax(0, 1.2fr) minmax(0, 1fr);
    }

    .doc-payment {
      text-align: end;
      white-space: normal;
    }

    .doc-payment__bar {
      display: none;
    }
  }
}
