/* ── Layout ── */
.billing-page {
  display: flex;
  flex-direction: column;
  gap: var(--space-6);
  padding: 0 var(--space-6) var(--space-6);
}

.billing-layout {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: var(--space-6);
  align-items: start;
}

/* ── Invoice cards ── */
.billing-invoices {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.invoice-card {
  display: flex;
  align-items: flex-start;
  gap: var(--space-6);
  margin-bottom: var(--space-4);
}

.invoice-card__body {
  flex: 1;
  min-width: 0;
}

.invoice-card__header {
  position: relative;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  column-gap: var(--space-4);
  align-items: center;
  margin-bottom: var(--space-4);
}

.invoice-checkbox {
  width: 1.125rem;
  height: 1.125rem;
  margin: 0;
  cursor: pointer;
  accent-color: var(--color-brand-blue);
  flex-shrink: 0;
}

/* Nudges the checkbox down to optically center against the two-line Invoice # header content,
   since it's now a flex sibling of the whole card body rather than just the header row. */
.invoice-card > .invoice-checkbox {
  margin-top: 0.625rem;
}

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

.invoice-number {
  color: var(--color-text-primary);
}

.invoice-card__header .badge {
  position: absolute;
  right: 0;
  transform: translateY(-50%);
}

.invoice-card__details {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--space-2) var(--space-4);
  padding-bottom: var(--space-4);
  margin-bottom: var(--space-4);
  border-bottom: 1px solid var(--color-divider);
}

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

.invoice-detail-value {
  color: var(--color-text-primary);
}

.invoice-card__footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.invoice-actions {
  display: flex;
  gap: var(--space-2);
}

/* ── Right sidebar ── */
.billing-sidebar {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.billing-summary-card,
.billing-credits-card,
.billing-payment-card {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

/* ── Summary card (matches the reference's .summary-amount-section) ── */
.summary-label {
  color: var(--color-text-secondary);
}

.summary-breakdown {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

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

.summary-row-value {
  font-size: 1.625rem;
  font-weight: 700;
  color: var(--color-text-primary);
}

.summary-row-total {
  padding-top: var(--space-3);
  border-top: 1px solid var(--color-border);
}

.summary-row-remaining.is-overdue {
  color: var(--color-error-text-alt);
}

/* ── Credits card ── */
.credits-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.credits-apply-row {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--space-1);
  margin-top: var(--space-1);
}

.credits-apply-row .form-field {
  flex: 1;
  margin-bottom: 0;
}

.form-field--checkbox {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.form-field--checkbox .form-checkbox {
  width: 1.125rem;
  height: 1.125rem;
  margin: 0;
  cursor: pointer;
  accent-color: var(--color-brand-blue);
  flex-shrink: 0;
}

.form-field--checkbox label {
  margin: 0;
  cursor: pointer;
}

.form-field--checkbox .form-checkbox:disabled {
  cursor: not-allowed;
}

.form-field--checkbox-disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.form-field--checkbox-disabled label {
  cursor: not-allowed;
}

.credits-apply-disabled-note {
  color: var(--color-text-secondary);
  padding-left: calc(1.125rem + var(--space-2));
}

/* ── Payment card ── */
.billing-payment-card .form-field {
  margin-bottom: 0;
}

.payment-method-list {
  display: flex;
  flex-direction: column;
}

.payment-method-list--scroll {
  max-height: 20rem;
  overflow-y: auto;
}

.payment-method {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) 0;
  border-bottom: 1px solid var(--color-divider);
}

.payment-method:last-child {
  border-bottom: none;
}

.payment-method-brand {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-bottom: var(--space-1);
}

.payment-method-name {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--color-text-primary);
}

.payment-method-number {
  font-size: 0.8125rem;
  color: var(--color-text-secondary);
  margin-bottom: var(--space-1);
}

.payment-method-expiry {
  font-size: 0.8125rem;
  color: var(--color-text-secondary);
}

.payment-method-actions {
  display: flex;
  gap: var(--space-2);
  flex-shrink: 0;
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
}

.setting-row--compact {
  padding: var(--space-3) 0;
}

.setting-title {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--color-text-primary);
}

.setting-description {
  font-size: 0.8125rem;
  color: var(--color-text-secondary);
  line-height: 1.4;
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 2.5rem;
  height: 1.375rem;
  cursor: pointer;
  flex-shrink: 0;
}

.toggle-input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-track {
  position: absolute;
  inset: 0;
  background-color: var(--color-border);
  border-radius: 99px;
  transition: background-color 200ms ease-out;
}

.toggle-track::after {
  content: "";
  position: absolute;
  width: 1rem;
  height: 1rem;
  background-color: var(--color-white);
  border-radius: 50%;
  top: 3px;
  left: 3px;
  transition: transform 200ms ease-out;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.toggle-input:checked + .toggle-track {
  background-color: var(--color-success-accent);
}

.toggle-input:checked + .toggle-track::after {
  transform: translateX(1.125rem);
}

.pay-now-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  width: 100%;
  margin-top: var(--space-1);
}

/* ── States ── */
.billing-error {
  padding: var(--space-8);
  text-align: center;
  color: var(--color-error-text-alt);
}

/* ── Responsive ── */
@media (max-width: 900px) {
  .billing-layout {
    grid-template-columns: 1fr;
  }

  .invoice-card__header,
  .invoice-card__details {
    grid-template-columns: repeat(2, 1fr);
  }

  .invoice-card__header .badge {
    position: static;
    transform: none;
    grid-column: 1 / -1;
    justify-self: start;
    order: -1;
    margin: var(--space-1) 0 var(--space-2);
  }
}

@media (max-width: 970px) {
  .billing-history-card .billing-table thead {
    display: none;
  }

  .billing-history-card .billing-table,
  .billing-history-card .billing-table tbody,
  .billing-history-card .billing-table tr {
    display: block;
    width: 100%;
  }

  .billing-history-card .billing-table tbody tr {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: var(--space-3) var(--space-4);
    padding: var(--space-4) var(--space-8);
    border-bottom: 1px solid var(--color-divider);
    box-sizing: border-box;
  }

  .billing-history-card .billing-table tbody tr:last-child {
    border-bottom: none;
  }

  .billing-history-card .billing-table td {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-1);
    padding: 0;
    border-bottom: none;
  }

  .billing-history-card .billing-table td::before {
    content: attr(data-label);
    font-weight: 700;
    font-size: 0.875rem;
    line-height: normal;
    color: var(--color-text-secondary);
  }

  .billing-table__actions {
    grid-column: 1 / -1;
    text-align: left;
  }

  .billing-table__actions::before {
    content: none;
  }

  /* Open Invoices table becomes a fixed 2x2 card grid — switches sooner than Invoice History
     since it only has 4 columns to begin with. */
  .billing-open-invoices-card .billing-table thead {
    display: none;
  }

  .billing-open-invoices-card .billing-table,
  .billing-open-invoices-card .billing-table tbody,
  .billing-open-invoices-card .billing-table tfoot,
  .billing-open-invoices-card .billing-table tr {
    display: block;
    width: 100%;
  }

  .billing-open-invoices-card .billing-table tbody tr {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: var(--space-3) var(--space-4);
    padding: var(--space-4) 0;
    border-bottom: 1px solid var(--color-divider);
    box-sizing: border-box;
  }

  .billing-open-invoices-card .billing-table tbody tr:last-child {
    border-bottom: none;
  }

  .billing-open-invoices-card .billing-table td {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-1);
    padding: 0;
    border-bottom: none;
  }

  .billing-open-invoices-card .billing-table td::before {
    content: attr(data-label);
    font-weight: 700;
    font-size: 0.875rem;
    line-height: normal;
    color: var(--color-text-secondary);
  }

  .billing-open-invoices-card .billing-table tfoot tr {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: var(--space-3);
    padding: var(--space-3) 0 0;
    border-top: 1px solid var(--color-divider);
    box-sizing: border-box;
  }

  .billing-open-invoices-card .billing-table tfoot td {
    border-top: none;
    padding-top: 0;
  }

  .billing-open-invoices-card .billing-table tfoot td:empty {
    display: none;
  }

  .billing-open-invoices-card .billing-table tfoot td::before {
    content: none;
  }
}

/* ── Empty state ── */
.billing-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  padding: var(--space-16) var(--space-8);
  text-align: center;
}

.billing-empty__icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 5rem;
  height: 5rem;
  border-radius: 50%;
  background: var(--color-success-bg-alt);
  color: var(--color-success-icon);
  margin-bottom: var(--space-2);
}

.billing-empty__heading {
  font-size: 1.25rem;
  font-weight: 500;
  color: var(--color-text-primary);
  margin: 0;
}

.billing-empty__subtext {
  font-size: 0.9375rem;
  color: var(--color-text-secondary);
  margin: 0;
}

.billing-invoices__toolbar {
  display: flex;
  padding-bottom: var(--space-6);
}

.billing-invoices__toolbar .btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
}

.billing-invoices__toolbar .form-field {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-bottom: 0;
  cursor: pointer;
}

/* ── Confirmation view ── */
.payment-confirmation {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: var(--space-6);
  gap: var(--space-6);
}

.confirmation-card {
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-5);
  width: 100%;
  max-width: 35rem;
  margin: 0 auto;
  text-align: center;
}

.confirmation-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 4rem;
  height: 4rem;
  border-radius: 50%;
  border: 2.5px solid var(--color-success-icon);
  color: var(--color-success-icon);
}

.confirmation-heading {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--color-text-primary);
  margin: 0;
}

.confirmation-body {
  font-size: 0.9375rem;
  color: var(--color-text-secondary);
  line-height: 1.6;
  margin: 0;
}

.confirmation-number-card {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  background: var(--color-surface);
  border-radius: var(--space-3);
  padding: var(--space-5) var(--space-6);
  width: 100%;
  text-align: left;
}

.confirmation-number-label {
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--color-text-secondary);
}

.confirmation-number {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--color-text-primary);
  letter-spacing: 0.03em;
  word-break: break-all;
}

.confirmation-number-hint {
  font-size: 0.8125rem;
  color: var(--color-text-secondary);
  line-height: 1.5;
}

.confirmation-return-btn {
  width: 100%;
  padding: var(--space-3);
  font-size: 0.9375rem;
}

.invoice-pay-amount.form-field {
  margin-bottom: 0;
}

.invoice-pay-amount .amount-field {
  width: 5.625rem;
}

.invoice-pay-amount input:disabled {
  opacity: 1;
  color: var(--color-text-primary);
  -webkit-text-fill-color: var(--color-text-primary);
}

/* ── Back-to-summary button (MakeAPaymentPanel) ──
   .billing-page is a flex column, so without this the button stretches to
   the full container width and its default border reads as a giant box. */
.billing-back-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  align-self: flex-start;
  width: fit-content;
  padding: var(--space-2) 0;
  margin-bottom: var(--space-2);
  background: none;
  border: none;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--color-text-secondary);
  cursor: pointer;
}

.billing-back-btn:hover {
  color: var(--color-text-primary);
}

/* ══════════════════════════════════════════════════════════════════════
   Summary screen (BillingAndPaymentsPage landing view)
   ══════════════════════════════════════════════════════════════════════ */

.billing-summary-layout {
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: var(--space-6);
  align-items: stretch;
}

/* ── Account balance card ── */
.billing-balance-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: var(--space-3);
  padding: var(--space-8) var(--space-6);
}

.balance-amount {
  font-size: 2.5rem;
  font-weight: 700;
  color: var(--color-text-primary);
  line-height: 1.1;
}

.balance-due-date {
  font-size: 0.9375rem;
  color: var(--color-text-secondary);
}

.autopay-status {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  font-size: 0.875rem;
  color: var(--color-text-secondary);
}

.autopay-status--on {
  color: var(--color-success-icon);
}

.balance-pay-btn {
  width: 100%;
  margin-top: var(--space-2);
}

/* ── Open invoices card ── */
.billing-open-invoices-card {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.open-invoices-heading-row {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.invoice-history-heading {
  padding: 0 var(--space-8);
  margin-bottom: var(--space-4);
}

/* ── Shared table styling (Open Invoices + Invoice History) ── */
.billing-table {
  width: 100%;
  border-collapse: collapse;
}

/* The table itself is taken out of table layout so tbody can scroll independently — nesting
   a display:table thead/tr directly inside a still-display:table <table> renders inconsistently
   across browsers, so the whole element drops to block and each <tr> becomes its own
   fixed-layout mini-table instead; matching column counts + widths keep them aligned. */
.billing-table {
  display: block;
}

.billing-table thead,
.billing-table tbody,
.billing-table tfoot {
  display: block;
  width: 100%;
}

.billing-table tbody {
  max-height: 11.5rem;
  overflow-y: auto;
  box-sizing: border-box;
}

.billing-table tr {
  display: table;
  width: 100%;
  table-layout: fixed;
}

/* Status only ever holds a short badge — every row is its own fixed-layout mini-table (see
   above), so this has to be repeated per row rather than set once on a shared <col>. */
.billing-table th:first-child,
.billing-table td:first-child {
  width: 5rem;
}

.billing-table th {
  text-align: left;
  font-weight: 700;
  font-size: 0.875rem;
  line-height: normal;
  color: var(--color-text-secondary);
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--color-divider);
}

.billing-table td {
  padding: var(--space-3);
  font-size: 0.9375rem;
  color: var(--color-text-primary);
  border-bottom: 1px solid var(--color-divider);
}

.billing-table tbody tr:last-child td {
  border-bottom: none;
}

.billing-table tfoot td {
  border-bottom: none;
  border-top: 1px solid var(--color-divider);
  padding-top: var(--space-3);
}

.billing-table__amount {
  font-weight: 600;
}

.billing-table__total-label,
.billing-table__total-value {
  font-weight: 700;
  color: var(--color-text-primary);
}

/* Fallback in case the shared badge set doesn't already define a warning/unpaid variant. */
.badge-warning {
  background-color: #fef3c7;
  color: #92620a;
}

/* ── Invoice history card ── */
.billing-history-card {
  display: flex;
  flex-direction: column;
  padding: var(--space-8) 0;
}

.billing-history-empty {
  color: var(--color-text-secondary);
  padding: var(--space-4) var(--space-8);
}

.history-year-group {
  border-bottom: 1px solid var(--color-divider);
}

.history-year-group:first-of-type {
  border-top: 1px solid var(--color-divider);
}

.history-year-group:last-of-type {
  border-bottom: none;
}

.history-year-toggle {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  width: 100%;
  padding: var(--space-4) var(--space-8);
  background: none;
  border: none;
  font-size: 1rem;
  font-weight: 700;
  color: var(--color-text-primary);
  cursor: pointer;
  text-align: left;
}

.history-year-toggle--expanded {
  background-color: var(--color-surface-alt);
}

.billing-history-card .billing-table thead th {
  background-color: var(--color-surface-alt);
}

@media (min-width: 971px) {
  .billing-history-card .billing-table th:first-child,
  .billing-history-card .billing-table td:first-child {
    padding-left: var(--space-8);
  }

  .billing-history-card .billing-table th:last-child,
  .billing-history-card .billing-table td:last-child {
    padding-right: var(--space-8);
    text-align: right;
  }

  /* Fixed-layout columns don't shrink their content, so the Download button needs a column
     wide enough to hold it without spilling past the cell boundary. */
  .billing-table th.billing-table__actions,
  .billing-table td.billing-table__actions {
    width: 8rem;
  }

  .billing-table__actions .btn {
    max-width: 100%;
  }
}

.history-pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-8) 0;
}

.history-page-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;
  border: none;
  background: none;
  color: var(--color-text-primary);
  cursor: pointer;
}

.history-page-btn:disabled {
  color: var(--color-text-secondary);
  opacity: 0.4;
  cursor: default;
}

.history-page-label {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--color-text-primary);
}

/* ── Responsive ── */
@media (max-width: 900px) {
  .billing-summary-layout {
    grid-template-columns: 1fr;
  }

  /* Once the cards stack into a single column there's no side-by-side height to match, so the
     scroll cap that keeps the table from stretching the sidebar column is no longer needed. */
  .billing-table tbody {
    max-height: none;
    overflow-y: visible;
  }
}

/* ══════════════════════════════════════════════════════════════════════
   Tabs (My Bill / Payment Settings)
   ══════════════════════════════════════════════════════════════════════ */
.page-tabs {
  border-bottom: 2px solid var(--color-border);
}

.tab-nav {
  display: flex;
}

.tab-btn {
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  font-size: 1rem;
  font-weight: 500;
  color: var(--color-text-secondary);
  padding: var(--space-2) var(--space-6);
  margin-bottom: -2px;
  cursor: pointer;
  transition:
    color 150ms ease-out,
    border-color 150ms ease-out;
}

.tab-btn.active {
  color: var(--color-brand-blue-text);
  border-bottom-color: var(--color-brand-blue);
}

.tab-btn:hover:not(.active) {
  color: var(--color-text-primary);
}

.tab-panel {
  display: none;
}

/* .billing-page's flex gap only applies to its direct children — .page-tabs and the active
   .tab-panel — so the My Bill tab needs its own gap to space .billing-summary-layout from
   .billing-history-card, which used to be direct .billing-page siblings before tabs existed. */
.tab-panel.active {
  display: flex;
  flex-direction: column;
  gap: var(--space-6);
}

/* ── Payment Settings tab ── */
.settings-content {
  max-width: 36.25rem;
  width: 100%;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: var(--space-6);
}

.payment-method-placeholder {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--color-border);
  border-radius: var(--space-2);
  font-size: 0.875rem;
}

.add-payment-btn {
  width: fit-content;
  display: flex;
  margin: var(--space-6) auto 0;
}

@media (max-width: 767px) {
  .billing-page {
    padding: 0 var(--space-2) var(--space-2);
  }

  .settings-content {
    max-width: none;
  }
}

@media (max-width: 480px) {
  .billing-history-card .billing-table tbody tr {
    grid-template-columns: repeat(2, 1fr);
  }

  .invoice-card__details {
    grid-template-columns: repeat(2, 1fr);
  }

  .invoice-card__footer {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-3);
  }
}
