/* billing.js: the API / Subscription switch and the Subscriptions card. A
 * two-way segmented switch in the top bar, a limits readout that reuses the
 * context ring, and a card in the meta-pop style for linking accounts.
 * Pool usage keeps names and compact usage summaries in two columns. */

.billing { position: relative; display: flex; align-items: center; gap: var(--sv-space-1); }
.billing[hidden] { display: none; }
.billing-switch { display: inline-flex; padding: 2px; border-radius: var(--sv-radius); background: var(--sv-surface-sunken); }
.billing-switch button {
  min-height: 26px;
  padding: 0 var(--sv-space-2);
  border: 0;
  border-radius: calc(var(--sv-radius) * 0.75);
  background: none;
  color: var(--sv-ink-tertiary);
  font: inherit;
  font-size: var(--ui-meta);
  cursor: pointer;
  transition: background var(--sv-dur-fast) var(--sv-ease-out), color var(--sv-dur-fast) var(--sv-ease-out);
}
.billing-switch button:hover { color: var(--sv-ink); }
.billing-switch button[aria-checked="true"] { background: var(--sv-surface); color: var(--sv-ink); box-shadow: 0 1px 2px rgb(0 0 0 / 0.08); }
.billing.is-sub .billing-switch button[aria-checked="true"] { color: var(--ws-working-ink); }
.billing-switch button:focus-visible { outline: var(--sv-hairline-w) solid var(--sv-ink-tertiary); outline-offset: 1px; }
button.billing-limits { min-height: 24px; gap: var(--sv-space-1); padding: 0 var(--sv-space-2); color: var(--sv-ink-tertiary); font-size: var(--ui-meta); font-weight: 400; }
.billing-limits .meta-num { color: var(--sv-ink-secondary); }
.billing.is-sub .billing-limits > .model-logo { color: var(--ws-working-ink); }
.billing-limits.warn .meta-num { color: var(--sv-attention); }
.billing-limits.full .meta-num { color: var(--sv-danger); }

.billing-card {
  position: absolute;
  top: calc(100% + var(--sv-space-2));
  right: 0;
  z-index: var(--sv-z-overlay);
  width: min(26rem, calc(100vw - 2 * var(--sv-space-4)));
  max-height: min(70vh, 40rem);
  overflow-y: auto;
  padding: var(--sv-space-3);
  font-size: var(--ui-meta);
}
.billing-card[hidden] { display: none; }
.billing-card:focus { outline: none; }
.bl-lead { margin-bottom: var(--sv-space-2); }
.bl-kind { padding: var(--sv-space-3) 0; border-top: var(--sv-hairline-w) solid var(--sv-hairline); }
.bl-khead { display: flex; align-items: center; justify-content: space-between; gap: var(--sv-space-2); margin-bottom: var(--sv-space-2); }
.bl-khead b { font-weight: 500; font-size: var(--ui-text); }
.bl-khead .ghost, .bl-tools .ghost, .bl-link a.ghost { min-height: 26px; padding: 0 var(--sv-space-2); font-size: var(--ui-meta); }
.bl-account { display: grid; gap: var(--sv-space-1); padding: var(--sv-space-2); margin-bottom: var(--sv-space-2); border-radius: var(--sv-radius); background: var(--sv-surface); border: var(--sv-hairline-w) solid var(--sv-hairline); }
.bl-account.needs_login { border-color: var(--sv-danger); }
.bl-ahead { display: flex; align-items: baseline; gap: var(--sv-space-2); min-width: 0; }
.bl-ahead b { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bl-sub { flex: 1; min-width: 0; color: var(--sv-ink-tertiary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bl-badge { flex: none; color: var(--sv-attention); }
.needs_login .bl-badge { color: var(--sv-danger); }
.bl-window { display: grid; grid-template-columns: 6rem 1fr 4.25rem 5.5rem; align-items: center; gap: var(--sv-space-2); font-variant-numeric: tabular-nums; }
.bl-wname { color: var(--sv-ink-secondary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bl-bar { height: 4px; border-radius: 2px; background: var(--sv-hairline); overflow: hidden; }
.bl-bar > span { display: block; height: 100%; background: var(--sv-ink-secondary); transition: width var(--sv-dur-base) var(--sv-ease-out); }
.bl-window.warn .bl-bar > span { background: var(--sv-attention); }
.bl-window.full .bl-bar > span { background: var(--sv-danger); }
.bl-num { text-align: right; }
.bl-reset { color: var(--sv-ink-tertiary); text-align: right; white-space: nowrap; }
.bl-note { color: var(--sv-ink-tertiary); white-space: normal; }
.bl-error { color: var(--sv-danger); white-space: normal; overflow-wrap: anywhere; }
.bl-tools { display: flex; flex-wrap: wrap; align-items: center; gap: var(--sv-space-2); margin-top: var(--sv-space-1); }
.bl-tools .bl-check { margin-right: auto; }
.bl-remove:hover { color: var(--sv-danger); }
.bl-check { display: inline-flex; align-items: center; gap: var(--sv-space-1); color: var(--sv-ink-secondary); cursor: pointer; }
.bl-switch { display: flex; margin-bottom: var(--sv-space-2); }
.bl-tabs { flex: none; margin-bottom: var(--sv-space-2); border-bottom: var(--sv-hairline-w) solid var(--sv-hairline); }
.bl-tabs .pn-tab { min-height: 34px; }
.bl-ksum { flex: 1; text-align: right; }
/* Whom an account served: one grid, so the numbers line up under their heads. */
.bl-users { display: grid; grid-template-columns: minmax(0, 1fr) auto; column-gap: var(--sv-space-2); row-gap: 2px; margin-top: var(--sv-space-1); font-variant-numeric: tabular-nums; }
.bl-users.has-tokens { grid-template-columns: minmax(0, 1fr) repeat(4, auto); }
.bl-user { display: contents; }
.bl-uhead > .bl-n { color: var(--sv-ink-tertiary); font-size: 0.85em; }
.bl-who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bl-n { text-align: right; white-space: nowrap; }
.bl-user:not(.bl-uhead) > .bl-cached { color: var(--sv-ink-tertiary); }
.bl-ufoot { grid-column: 1 / -1; }
.bl-switch button { flex: 1; }
/* The chip's words read like the other top-bar chips (app.css): secondary ink. */
.billing-mode { color: var(--sv-ink-secondary); }
/* A plan with nothing usable linked: runs would fail, so the chip asks for attention. */
.billing-limits.unlinked::after { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--sv-attention); }
.bl-link { display: grid; gap: var(--sv-space-2); }
.bl-link:empty { display: none; }
/* A kind's head: logo, name and plan, and its Link button. */
.bl-kname { display: flex; align-items: center; gap: var(--sv-space-2); min-width: 0; }
.bl-kname > .model-logo { flex: none; width: 18px; height: 18px; color: var(--sv-ink); }
.bl-kwords { display: grid; min-width: 0; }
.bl-kwords .bl-sub { font-size: var(--ui-meta); }
.bl-khead .primary { min-height: 30px; padding: 0 var(--sv-space-3); font-size: var(--ui-meta); }
.bl-head { align-items: flex-start; }
.bl-titles { display: grid; gap: 2px; min-width: 0; }
.bl-close { flex: none; display: inline-flex; align-items: center; justify-content: center; width: 30px; height: 30px; padding: 0; margin: -4px -4px 0 0; }
.bl-close .icon { width: 16px; height: 16px; }
/* The steps of a sign-in: numbered, the current one marked, done ones ticked. */
.bl-steps { display: grid; gap: var(--sv-space-3); margin: 0; padding: 0; list-style: none; }
.bl-step { display: grid; grid-template-columns: 24px minmax(0, 1fr); gap: var(--sv-space-2); color: var(--sv-ink-secondary); white-space: normal; }
.bl-step.now { color: var(--sv-ink); }
.bl-step.done { color: var(--sv-ink-tertiary); }
.bl-num-dot { display: inline-flex; align-items: center; justify-content: center; width: 22px; height: 22px; border-radius: 50%; border: var(--sv-hairline-w) solid var(--sv-hairline-strong); font-size: 0.75rem; font-variant-numeric: tabular-nums; }
.bl-step.now .bl-num-dot { border-color: var(--sv-ink); background: var(--sv-ink); color: var(--sv-surface); }
.bl-step.done .bl-num-dot { border-color: transparent; background: var(--sv-surface-sunken); }
.bl-num-dot .icon { width: 13px; height: 13px; }
.bl-step-body { display: grid; gap: var(--sv-space-2); min-width: 0; }
.bl-step-body > :is(a, button) { justify-self: start; }
.bl-flow { display: grid; gap: var(--sv-space-3); white-space: normal; }
/* The provider's page, as a link that looks like button.primary (tokens.css). */
a.bl-go {
  display: inline-flex;
  align-items: center;
  gap: var(--sv-space-1);
  min-height: 32px;
  padding: 0 var(--sv-space-3);
  background: var(--sv-ink);
  color: var(--sv-surface);
  border-radius: var(--sv-radius);
  font: 500 var(--ui-text) / 1 var(--sv-font-sans);
  text-decoration: none;
  transition: opacity var(--sv-dur-instant) var(--sv-ease-out);
}
a.bl-go:hover { opacity: 0.85; }
a.bl-go .icon { width: 14px; height: 14px; opacity: 0.85; }
a.bl-go.again { background: none; color: var(--sv-ink); border: var(--sv-hairline-w) solid var(--sv-hairline-strong); }
.bl-link:focus { outline: none; }
/* Read by screen readers, not shown. */
.bl-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.bl-field { display: flex; gap: var(--sv-space-2); }
.bl-field > .bl-input { flex: 1; min-width: 0; }
.bl-codebox { display: flex; flex-wrap: wrap; align-items: center; gap: var(--sv-space-2) var(--sv-space-3); padding: var(--sv-space-2) var(--sv-space-3); border-radius: var(--sv-radius); background: var(--sv-surface-sunken); }
.bl-codebox .bl-wname { flex-basis: 100%; }
.bl-code { flex: 1; font: 600 1.25rem/1.2 var(--sv-font-mono, ui-monospace, monospace); letter-spacing: 0.08em; user-select: all; overflow-wrap: anywhere; }
.bl-address { display: block; padding: var(--sv-space-2); border-radius: var(--sv-radius); background: var(--sv-surface-sunken); font: 0.8125rem/1.4 var(--sv-font-mono, ui-monospace, monospace); overflow-wrap: anywhere; user-select: all; }
.bl-status { color: var(--sv-ink-secondary); white-space: normal; }
.bl-status:empty { display: none; }
.bl-status.good { color: var(--ws-working-ink); }
.bl-status.bad { color: var(--sv-danger); }
.bl-actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--sv-space-2); }
.bl-actions > button.primary, .bl-step-body > button.primary { min-height: 30px; padding: 0 var(--sv-space-3); }
.bl-more { padding-top: var(--sv-space-2); border-top: var(--sv-hairline-w) dashed var(--sv-hairline); }
.bl-more .ghost { min-height: 26px; padding: 0 var(--sv-space-2); font-size: var(--ui-meta); color: var(--sv-ink-tertiary); }
.bl-input {
  width: 100%;
  padding: var(--sv-space-2);
  border: var(--sv-hairline-w) solid var(--sv-hairline-strong);
  border-radius: var(--sv-radius);
  background: var(--sv-surface);
  color: var(--sv-ink);
  font: 0.75rem/1.4 var(--sv-font-mono, ui-monospace, monospace);
  resize: vertical;
}
/* usage card: runs a subscription paid for are not billed */
.meta-pop .u-sub td:last-child { color: var(--ws-working-ink); }
@media (max-width: 760px) {
  /* The chip is the vendor's mark; the words return only when a limit is close. */
  .billing-limits:has(> .model-logo) > .billing-mode, .billing-limits:not(.warn, .full) > .meta-num { display: none; }
}
/* A phone, upright or on its side: the card is a sheet with thumb-sized controls. */
@media (max-width: 760px), (pointer: coarse) and (max-height: 540px) {
  /* A full-screen sheet, one scroller: the visible viewport's height while
   * the keyboard is up (billing.js sets --bl-vh/--bl-top), else 100dvh. */
  .billing-card {
    position: fixed;
    top: var(--bl-top, 0px);
    right: 0;
    left: 0;
    z-index: calc(var(--sv-z-overlay) + 1);
    width: auto;
    height: 100vh;
    height: 100dvh;
    height: var(--bl-vh, 100dvh);
    max-height: none;
    padding: 0 max(var(--sv-space-4), env(safe-area-inset-right)) max(var(--sv-space-6, 1.5rem), env(safe-area-inset-bottom)) max(var(--sv-space-4), env(safe-area-inset-left));
    border: 0;
    border-radius: 0;
    overscroll-behavior: contain;
    font-size: var(--ui-text);
  }
  .billing-card > .bl-head {
    position: sticky;
    top: 0;
    z-index: 1;
    margin: 0 calc(-1 * var(--sv-space-4)) var(--sv-space-2);
    padding: max(var(--sv-space-3), env(safe-area-inset-top)) var(--sv-space-4) var(--sv-space-3);
    background: var(--sv-surface-raised);
    border-bottom: var(--sv-hairline-w) solid var(--sv-hairline);
  }
  .bl-head b { font-size: 1.0625rem; }
  /* A phone on its side with the keyboard up: every line goes to the field. */
  .billing-card.bl-short > .bl-head { position: static; padding-top: var(--sv-space-2); padding-bottom: var(--sv-space-2); }
  .billing-card.bl-short .bl-titles > span { display: none; }
  /* Thumb-sized controls and a 16px field (iOS zooms into anything smaller). */
  .billing-card :is(button, a.bl-go, .bl-check), .bl-switch button, .bl-tabs .pn-tab,
  .bl-khead :is(.ghost, .primary), .bl-tools .ghost, .bl-more .ghost, .bl-actions > button.primary, .bl-step-body > button.primary { min-height: 44px; }
  .bl-close { width: 44px; height: 44px; margin: 0 -8px 0 0; }
  .bl-check input { width: 20px; height: 20px; }
  .bl-input { font-size: 16px; padding: var(--sv-space-2) var(--sv-space-3); }
  .bl-field > .bl-input { min-height: 44px; }
  .bl-step-body > :is(a.bl-go, button.primary) { justify-self: stretch; justify-content: center; }
  .bl-num-dot { width: 24px; height: 24px; }
  .bl-more .ghost { font-size: var(--ui-text); }
  /* Limits stack on a narrow screen: name and numbers over the bar. */
  .bl-window { grid-template-columns: minmax(0, 1fr) auto auto; row-gap: 2px; }
  .bl-window .bl-bar { grid-column: 1 / -1; grid-row: 2; }
}
