/* ============================================
   BILLING PAGE (/companies/{id}/billing)
   ============================================
   Credit balance, Stripe top-up packs, and a recent-usage table, in the
   two-column settings layout shared with the members page.

   Same convention as the rest of the per-page CSS:
   - @layer components: structural rules only — sizing, positioning,
     grid/flex, padding/margin, transitions, font-size/weight.
   - @layer theme: all color / surface / border-color / background /
     box-shadow / text-color rules. Theme is later in the cascade than
     components, so palette overrides win without specificity wars.
   ============================================ */

/* --- COMPONENTS --- */
@layer components {
  .billing-page {
    padding-block: var(--space-xl);
    position: relative;
    /* clip, not hidden: overflow:hidden makes the page root a scroll
       container, which would silently disable the sticky side panel. */
    overflow: clip;

    .detail-back-link {
      display: inline-flex;
      align-items: center;
      gap: var(--space-xs);
      margin-block-end: var(--space-md);
      font-size: var(--fs-small);
      text-decoration: none;
      transition: color 0.15s ease-out;
      position: relative;
      z-index: 1;

      svg {
        flex-shrink: 0;
      }
    }

    .billing-banner {
      position: relative;
      z-index: 1;
      margin-block-end: var(--space-lg);
      padding: var(--space-sm) var(--space-md);
      border-radius: var(--wire-radius);
      border-width: 1px;
      border-style: solid;
      font-size: var(--fs-small);
    }

    .settings-body {
      position: relative;
      z-index: 1;
      display: grid;
      grid-template-columns: minmax(0, 1fr) 300px;
      align-items: start;
      gap: var(--space-xl);
      max-inline-size: 1280px;
    }

    .settings-main {
      display: flex;
      flex-direction: column;
      gap: var(--space-xl);
      min-inline-size: 0;
    }

    .settings-side {
      position: sticky;
      inset-block-start: var(--space-xl);
      display: flex;
      flex-direction: column;
      gap: var(--space-lg);
    }

    .section-head {
      display: flex;
      align-items: center;
      gap: var(--space-sm);
      margin-block-end: var(--space-md);

      h2 {
        font-size: var(--fs-body);
        margin: 0;
      }

      .section-icon {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        inline-size: 28px;
        block-size: 28px;
        border-radius: 8px;
        flex-shrink: 0;
      }
    }

    .card {
      padding: var(--space-lg);
      border-radius: 12px;
      border-width: 1px;
      border-style: solid;
    }

    .side-card .card {
      padding: var(--space-md);
    }

    .balance-card {
      display: flex;
      flex-direction: column;
      gap: var(--space-2xs);
    }

    .balance-amount {
      margin: 0;
      font-family: var(--font-heading);
      font-size: var(--fs-xxl);
      font-weight: var(--fw-semibold);
      line-height: 1.1;
    }

    .balance-hint {
      margin: 0;
      font-size: var(--fs-xsmall);
    }

    .pack-grid {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
      gap: var(--space-md);
    }

    .pack-button {
      display: flex;
      flex-direction: column;
      align-items: center;
      gap: var(--space-3xs);
      padding: var(--space-md);
      border-radius: 10px;
      border-width: 1px;
      border-style: solid;
      cursor: pointer;
      font-family: var(--font-heading);
      transition:
        border-color 0.15s ease-out,
        background 0.15s ease-out;

      .pack-amount {
        font-size: var(--fs-large);
        font-weight: var(--fw-semibold);
      }

      /* Sits between the amount and the assessment count: the customer read
         "+15% at $5,000" on the pricing page and comes here looking for it. */
      .pack-bonus {
        font-size: var(--fs-xsmall);
        font-weight: var(--fw-semibold);
        padding-block: 1px;
        padding-inline: var(--space-2xs);
        border-radius: 999px;
        white-space: nowrap;

        /* A pack below the first tier still renders this, holding the row's
           space so every assessment count shares one baseline. */
        &.pack-bonus--none {
          visibility: hidden;
        }
      }

      .pack-label {
        font-size: var(--fs-xsmall);
      }

      &:disabled {
        cursor: not-allowed;
      }
    }

    .custom-amount {
      display: flex;
      flex-direction: column;
      gap: var(--space-xs);
      margin-block-start: var(--space-lg);
      padding-block-start: var(--space-lg);
      border-block-start-width: 1px;
      border-block-start-style: solid;
    }

    .custom-amount-label {
      font-size: var(--fs-small);
      font-weight: var(--fw-medium);
    }

    .custom-amount-row {
      display: flex;
      align-items: center;
      gap: var(--space-xs);
    }

    .custom-amount-prefix {
      font-size: var(--fs-small);
    }

    .custom-amount-input {
      inline-size: 10ch;
      padding: var(--space-sm) var(--space-md);
      border-radius: var(--wire-radius);
      font-size: var(--fs-small);
    }

    .custom-amount-hint {
      margin: 0;
      font-size: var(--fs-xsmall);
    }

    .topup-hint {
      margin-block: var(--space-md) 0;
      font-size: var(--fs-xsmall);
    }

    .table-container {
      overflow-x: auto;
      border-radius: 12px;
    }

    .usage-table {
      inline-size: 100%;
      border-collapse: collapse;
      font-size: var(--fs-small);

      th,
      td {
        padding: var(--space-sm) var(--space-md);
        text-align: start;
        vertical-align: middle;
      }

      td {
        overflow-wrap: anywhere;
      }

      /* Classed, never positional — inserting a column must not silently
         re-target these. */
      .amount-cell {
        text-align: end;
        white-space: nowrap;
      }

      .when-cell {
        white-space: nowrap;
      }

      .empty-row td {
        text-align: center;
        padding-block: var(--space-lg);
      }
    }

    .billing-notes {
      list-style: none;
      margin: 0;
      padding: 0;
      display: flex;
      flex-direction: column;
      gap: var(--space-md);
      font-size: var(--fs-xsmall);

      strong {
        display: block;
        font-size: var(--fs-small);
        font-weight: var(--fw-semibold);
        margin-block-end: var(--space-4xs);
      }
    }

    @media (max-width: 1100px) {
      .settings-body {
        grid-template-columns: minmax(0, 1fr);
      }

      .settings-side {
        position: static;
      }
    }

    /* --- Rate table. The recruiter's question on this page is "what will this
       cost me", and the answer is two figures and a caveat, so it is a table
       rather than prose. Row labels are <th scope="row"> so a screen reader
       announces "Short — per candidate — $23-29" rather than a bare number. --- */
    .rate-table {
      inline-size: 100%;
      border-collapse: collapse;
      font-size: var(--fs-small);
      /* The price column is compared down the page, so its digits line up. */
      font-variant-numeric: tabular-nums;

      th,
      td {
        text-align: start;
        padding-block: var(--space-xs);
        padding-inline: 0 var(--space-sm);
      }

      thead th {
        font-size: var(--fs-xsmall);
        font-weight: 600;
        letter-spacing: 0.06em;
        text-transform: uppercase;
      }

      tbody th {
        font-weight: 600;
      }

      /* Classed, never positional — inserting a column must not silently
         re-target the price. The header cell carries it too, so one selector
         reaches both. */
      .rate-table__num {
        text-align: end;
        padding-inline-end: 0;
        white-space: nowrap;
      }
    }

    .rate-note {
      margin-block: var(--space-md) 0;
      font-size: var(--fs-small);
    }

    /* Table caption: the column headers alone do not say what is counted. */
    .sr-only {
      position: absolute;
      inline-size: 1px;
      block-size: 1px;
      padding: 0;
      margin: -1px;
      overflow: hidden;
      clip-path: inset(50%);
      white-space: nowrap;
    }
  }
}

/* --- THEME --- */
@layer theme {
  .billing-page {
    .detail-back-link {
      color: var(--text-secondary);

      &:hover {
        color: var(--accent-primary);
      }
    }

    .billing-banner {
      background: light-dark(
        oklch(from var(--color-info) l c h / 0.08),
        oklch(from var(--color-info) l c h / 0.12)
      );
      border-color: light-dark(
        oklch(from var(--color-info) l c h / 0.2),
        oklch(from var(--color-info) l c h / 0.25)
      );
      color: var(--color-info);

      &[data-kind='warn'] {
        background: light-dark(
          oklch(from var(--color-warning) l c h / 0.08),
          oklch(from var(--color-warning) l c h / 0.12)
        );
        border-color: light-dark(
          oklch(from var(--color-warning) l c h / 0.2),
          oklch(from var(--color-warning) l c h / 0.25)
        );
        color: var(--color-warning);
      }

      &[data-kind='error'] {
        background: light-dark(
          oklch(from var(--color-error) l c h / 0.08),
          oklch(from var(--color-error) l c h / 0.12)
        );
        border-color: light-dark(
          oklch(from var(--color-error) l c h / 0.2),
          oklch(from var(--color-error) l c h / 0.25)
        );
        color: var(--color-error);
      }

      &[data-kind='success'] {
        background: light-dark(
          oklch(from var(--color-success) l c h / 0.08),
          oklch(from var(--color-success) l c h / 0.12)
        );
        border-color: light-dark(
          oklch(from var(--color-success) l c h / 0.2),
          oklch(from var(--color-success) l c h / 0.25)
        );
        color: var(--color-success);
      }
    }

    .section-head .section-icon {
      color: var(--accent-primary);
      background: light-dark(oklch(0% 0 0 / 0.03), oklch(100% 0 0 / 0.05));
    }

    .card {
      background: light-dark(oklch(0% 0 0 / 0.02), oklch(100% 0 0 / 0.03));
      border-color: oklch(from var(--glass-tint) l c h / 0.06);
    }

    .balance-amount {
      color: var(--text-primary);

      /* A negative balance is not decorative — it means work has been done
         that the workspace has not paid for. */
      &[data-negative='true'] {
        color: var(--color-error);
      }
    }

    .balance-hint {
      color: var(--text-secondary);
    }

    .pack-button {
      background: light-dark(oklch(0% 0 0 / 0.02), oklch(100% 0 0 / 0.04));
      border-color: oklch(from var(--glass-tint) l c h / 0.08);
      color: var(--text-primary);

      &:hover:not(:disabled) {
        border-color: var(--accent-primary);
        background: light-dark(
          oklch(from var(--accent-primary) l c h / 0.06),
          oklch(from var(--accent-primary) l c h / 0.1)
        );
      }

      .pack-label {
        color: var(--text-secondary);
      }

      /* Bonus credit is money the customer gains, so it reads as positive
         rather than as another neutral caption. */
      .pack-bonus {
        color: var(--color-success);
        background: light-dark(
          oklch(from var(--color-success) l c h / 0.1),
          oklch(from var(--color-success) l c h / 0.16)
        );
      }

      &:disabled {
        color: var(--text-secondary);

        /* The badge sets its own colour, so it would stay bright on a button
           that is mid-checkout and no longer clickable. */
        .pack-bonus {
          color: var(--text-secondary);
          background: light-dark(oklch(0% 0 0 / 0.04), oklch(100% 0 0 / 0.06));
        }
      }
    }

    .custom-amount {
      border-block-start-color: oklch(from var(--glass-tint) l c h / 0.06);
    }

    .custom-amount-label {
      color: var(--text-primary);
    }

    .custom-amount-prefix,
    .custom-amount-hint {
      color: var(--text-secondary);
    }

    .topup-hint {
      color: var(--text-secondary);
    }

    .table-container {
      background: light-dark(oklch(0% 0 0 / 0.02), oklch(100% 0 0 / 0.03));
      border: 1px solid oklch(from var(--glass-tint) l c h / 0.06);
      backdrop-filter: blur(8px);
    }

    .usage-table {
      th {
        background: light-dark(oklch(0% 0 0 / 0.02), oklch(100% 0 0 / 0.04));
        color: var(--text-secondary);
        font-weight: var(--fw-medium);
        white-space: nowrap;
        border-block-end: 1px solid oklch(from var(--glass-tint) l c h / 0.06);
      }

      td {
        border-block-end: 1px solid oklch(from var(--glass-tint) l c h / 0.04);
      }

      tbody tr:last-child td {
        border-block-end: none;
      }

      .empty-row td {
        color: var(--text-secondary);
      }
    }

    .billing-notes {
      color: var(--text-secondary);

      strong {
        color: var(--text-primary);
      }
    }

    .rate-table {
      color: var(--text-primary);

      thead th {
        color: var(--text-secondary);
        border-block-end: 1px solid var(--border-subtle);
      }

      tbody th,
      tbody td {
        border-block-end: 1px solid var(--border-subtle);
      }

      tbody tr:last-child {
        th,
        td {
          border-block-end: none;
        }
      }
    }

    .rate-note {
      color: var(--text-secondary);
    }
  }
}
