/*
  Bounty Operator: the account panel.

  Loaded after base.css by any page that carries the account dialogs
  (web/site/fragments/account-dialog.mjs). /app/account.mjs adds the link
  itself on a page that does not list it.

  Contents
   1. Header control
   2. Sign-in dialog
   3. Recovery code
   4. Confirm it's you
   5. Account sheet: frame, tabs, panels
   6. Account sheet: pieces (hero, stats, sections, rows, confirmations)
   7. Connection token
   8. Plan table
   9. Checkout bar and pricing note

  Every colour, size and radius is a token from base.css.
*/

/* ==========================================================================
   1. Header control
   ========================================================================== */

/* Signed in, the control carries the plan chip before the word "Account". */
.account-control {
  gap: var(--sp-8);
}

.account-control .chip {
  min-height: 1.25rem;
  padding-inline: 0.375rem;
}

/* The dot of a status chip is left out here: the control is small. */
.account-control .chip::before {
  display: none;
}

.account-control__icon {
  display: none;
}

/* On a phone the header has one row for the brand, the theme toggle and this
   control. The word gives way to a person icon; the plan chip stays. */
@media (max-width: 30em) {
  .account-control:has(.chip) {
    padding-inline: var(--sp-8);
  }

  .account-control:has(.chip) .account-control__icon {
    display: inline-block;
  }

  .account-control:has(.chip) .btn__label {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }
}

.account-control[data-plan="past_due"] {
  border-color: var(--danger);
}

/* ==========================================================================
   2. Sign-in dialog
   ========================================================================== */

.account-dialog {
  --dialog-w: 30rem;
}

/* The head keeps its height on the recovery-code step, where Close is hidden. */
.account-dialog .dialog__head {
  min-height: calc(var(--tap) + 2 * var(--sp-8));
}

.account__body > * + * {
  margin-top: var(--sp-16);
}

/* Why the dialog opened: the one sentence a visitor reads before the button. */
.account__reason {
  font-size: var(--fs-18);
  font-weight: 600;
  line-height: 1.4;
  letter-spacing: -0.01em;
  text-wrap: balance;
}

.account__actions {
  display: grid;
  gap: var(--sp-12);
}

.account__actions .btn--block {
  min-height: var(--tap);
}

.account__actions .btn--lg {
  min-height: 3.25rem;
}

.account__fine {
  display: flex;
  align-items: flex-start;
  gap: var(--sp-8);
  color: var(--muted);
  font-size: var(--fs-13);
  line-height: 1.55;
}

.account__fine .icon {
  margin-top: 0.2em;
}

/* Recovery sits below everything else: it is the way in when the passkey is gone. */
.account__body > .account__recover {
  margin-top: var(--sp-24);
}

.account__recover-form {
  display: grid;
  gap: var(--sp-12);
}

/* ==========================================================================
   3. Recovery code
   ========================================================================== */

.recovery {
  display: grid;
  gap: var(--sp-16);
}

/* In the account sheet the panel keeps the width it has in the sign-in dialog. */
.portal .recovery {
  max-width: 30rem;
}

.recovery__intro {
  font-size: var(--fs-16);
  line-height: 1.5;
}

/* Dashed like an unproven chip: the code exists nowhere else until it is saved. */
.recovery__code {
  display: block;
  padding: var(--sp-16) var(--sp-12);
  border: 1px dashed var(--line-control);
  border-radius: var(--r-ctl);
  background: var(--surface-2);
  color: var(--text);
  /* 43 characters fit one line of the 480px dialog at this size. */
  font: 600 var(--fs-14) / 1.6 var(--font-mono);
  text-align: center;
  word-break: break-all;
  user-select: all;
}

.recovery__actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--sp-12);
}

.btn[data-copied] {
  border-color: var(--ok);
  color: var(--ok);
}

/* ==========================================================================
   4. Confirm it's you
   ========================================================================== */

.reauth-dialog {
  --dialog-w: 25rem;
}

.reauth__text {
  line-height: 1.55;
}

.reauth__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-12);
}

.reauth__actions .btn--primary {
  flex: 1 1 12rem;
}

/* ==========================================================================
   5. Account sheet: frame, tabs, panels
   ========================================================================== */

/* A sheet as tall as the window allows. The head and the tabs stay in place;
   only the panel scrolls. */
.portal {
  --dialog-w: 60rem;
  height: min(100dvh - 3rem, 46rem);
  max-height: none;
  overflow: hidden;
}

.portal[open] {
  display: flex;
  flex-direction: column;
}

.portal__head {
  position: static;
  flex: none;
}

.portal__heading {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--sp-4) var(--sp-12);
  min-width: 0;
}

.portal__plan {
  display: inline-flex;
}

.portal__layout {
  display: grid;
  flex: 1 1 auto;
  grid-template-columns: 13rem minmax(0, 1fr);
  min-height: 0;
}

/* The section list: a rail on the left. */
.portal__tabs {
  flex-direction: column;
  gap: 0.125rem;
  padding: var(--sp-12);
  overflow: hidden auto;
  border-right: 1px solid var(--line);
  border-bottom: 0;
  background: color-mix(in srgb, var(--surface-2) 55%, var(--surface));
}

.portal__tab {
  flex: none;
  justify-content: flex-start;
  gap: 0.625rem;
  margin-bottom: 0;
  padding: 0 var(--sp-12);
  border: 0;
  border-radius: var(--r-ctl);
  transition:
    background-color var(--t-fast),
    color var(--t-fast);
}

.portal__tab .icon {
  width: 1.0625rem;
  height: 1.0625rem;
}

.portal__tab:hover {
  background: color-mix(in srgb, var(--text) 6%, transparent);
}

/* The current section: full ink, a raised fill and the blue mark of the ledger. */
.portal__tab[aria-selected="true"] {
  background: var(--surface-3);
  box-shadow:
    inset 2px 0 0 var(--accent),
    var(--shadow-card);
}

.portal__tab[aria-selected="true"] .icon {
  color: var(--accent);
}

.portal__main {
  min-width: 0;
  padding: var(--sp-24);
  overflow: hidden auto;
  overscroll-behavior: contain;
}

.portal__status:not(:empty) {
  margin-bottom: var(--sp-24);
}

.portal__panel:focus {
  outline: none;
}

.portal__panel > * + * {
  margin-top: var(--sp-24);
}

.portal-loading {
  display: grid;
  gap: var(--sp-8);
}

/* Small screens: the sheet fills the window, and the rail becomes a strip of
   tabs under the head that scrolls sideways. */
@media (max-width: 47.99em) {
  .portal {
    width: 100%;
    max-width: 100%;
    height: 100dvh;
    max-height: 100dvh;
    margin: 0;
    border: 0;
    border-radius: 0;
  }

  .portal__layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr);
  }

  .portal__tabs {
    flex-direction: row;
    gap: var(--sp-4);
    padding: 0 var(--sp-8);
    overflow: auto hidden;
    border-right: 0;
    border-bottom: 1px solid var(--line);
    background: var(--surface);
  }

  .portal__tab {
    padding: 0 var(--sp-8);
    border-radius: 0;
  }

  .portal__tab .icon {
    display: none;
  }

  .portal__tab:hover {
    background: none;
  }

  .portal__tab[aria-selected="true"] {
    background: none;
    box-shadow: inset 0 -2px 0 var(--accent);
  }

  .portal__main {
    padding: var(--sp-16);
    padding-bottom: calc(var(--sp-32) + env(safe-area-inset-bottom, 0px));
  }
}

/* ==========================================================================
   6. Account sheet: pieces
   ========================================================================== */

/* Where the account stands, and the one thing to do about it. */
.portal-hero {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-16);
}

.portal-hero__text {
  flex: 1 1 16rem;
  min-width: 0;
}

.portal-hero__action {
  flex: none;
}

.portal-hero__title {
  font-size: var(--fs-22);
  line-height: 1.25;
  letter-spacing: -0.02em;
}

.portal-hero__line {
  max-width: 52ch;
  margin-top: var(--sp-4);
  color: var(--muted);
  font-size: var(--fs-14);
  line-height: 1.55;
}

/* Three figures: today, running, the plan. */
.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 9.5rem), 1fr));
  gap: var(--sp-12);
}

/* Two figures side by side on a phone; the third takes the full row. */
@media (max-width: 39.99em) {
  .stat:nth-child(3):last-child {
    grid-column: 1 / -1;
  }
}

.stat {
  display: grid;
  align-content: start;
  gap: var(--sp-8);
  padding: var(--sp-16);
  border: 1px solid var(--line);
  border-radius: var(--r-card);
  background: var(--surface);
  box-shadow: var(--shadow-card);
}

.stat__label {
  color: var(--muted);
  font: 600 var(--fs-12) / 1.4 var(--font-mono);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.stat__body {
  display: grid;
  gap: var(--sp-8);
}

.stat__value {
  font: 600 var(--fs-22) / 1.15 var(--font-mono);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}

.stat__note {
  color: var(--muted);
  font-size: var(--fs-13);
  line-height: 1.45;
}

.portal-section {
  display: grid;
  gap: var(--sp-12);
}

.portal-section + .portal-section {
  padding-top: var(--sp-24);
  border-top: 1px solid var(--line);
}

.portal-section__title {
  font-size: var(--fs-16);
  line-height: 1.4;
  letter-spacing: -0.01em;
}

.portal-section__lede {
  max-width: 62ch;
  margin-top: var(--sp-4);
  color: var(--muted);
  font-size: var(--fs-14);
  line-height: 1.55;
}

/* A lone control in a section keeps its own width. */
.portal-section > :is(.btn, .confirm) {
  justify-self: start;
}

:is(.confirm, .secret, .recovery) {
  scroll-margin-block: var(--sp-24);
}

.portal-section__actions {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: var(--sp-12);
}

/* Rows: reviews, connections, passkeys. */
.portal-list {
  margin: 0;
  border: 1px solid var(--line);
  border-radius: var(--r-card);
  background: var(--surface);
}

.portal-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-8) var(--sp-16);
  padding: var(--sp-12) var(--sp-16);
  border-bottom: 1px solid var(--line);
}

.portal-row:last-child {
  border-bottom: 0;
}

.portal-row__main {
  flex: 1 1 14rem;
  min-width: 0;
}

.portal-row__title {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--sp-4) var(--sp-8);
  font-size: var(--fs-14);
  font-weight: 600;
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.portal-row__meta {
  margin-top: 0.125rem;
  color: var(--muted);
  font-size: var(--fs-13);
  line-height: 1.5;
}

.portal-row__actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: var(--sp-8);
}

.portal-empty {
  display: grid;
  justify-items: start;
  gap: var(--sp-12);
  padding: var(--sp-16);
  border: 1px dashed var(--line-strong);
  border-radius: var(--r-card);
  color: var(--muted);
  font-size: var(--fs-14);
}

/* A destructive action, second step: what it does, the action, the way back. */
.confirm {
  display: grid;
  gap: var(--sp-8);
  max-width: 26rem;
  padding: var(--sp-12);
  border: 1px solid color-mix(in srgb, var(--danger) 40%, transparent);
  border-radius: var(--r-ctl);
  background: color-mix(in srgb, var(--danger) 8%, var(--surface));
}

.confirm__text {
  font-size: var(--fs-13);
  line-height: 1.5;
}

.confirm__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-8);
}

.confirm__actions .btn {
  min-height: 2.25rem;
  padding-inline: var(--sp-12);
  font-size: var(--fs-13);
}

.confirm__actions .btn--danger {
  background: var(--surface);
}

@media (pointer: coarse) {
  .confirm__actions .btn {
    min-height: var(--tap);
  }
}

.connection-form__row {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-12);
}

.connection-form__row .input {
  flex: 1 1 14rem;
  width: auto;
}

@media (max-width: 47.99em) {
  .portal-row__actions {
    flex: 1 1 100%;
    justify-content: flex-start;
  }

  .connection-form__row .btn {
    flex: 1 1 100%;
  }
}

/* ==========================================================================
   7. Connection token
   ========================================================================== */

/* Shown once. The blue edge marks it as the thing to act on now. */
.secret {
  display: grid;
  gap: var(--sp-16);
  padding: var(--sp-16);
  border: 1px solid var(--accent);
  border-radius: var(--r-card);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.secret__title {
  font-size: var(--fs-16);
  line-height: 1.4;
  overflow-wrap: anywhere;
}

.secret__lede {
  margin-top: 0.125rem;
  color: var(--muted);
  font-size: var(--fs-14);
}

.secret__token {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--sp-12);
  padding: var(--sp-12);
  border: 1px solid var(--code-line);
  border-radius: var(--r-ctl);
  background: var(--code-bg);
}

.secret__value {
  flex: 1 1 14rem;
  min-width: 0;
  padding: 0;
  border: 0;
  background: none;
  color: var(--code-text);
  font: 500 var(--fs-13) / 1.5 var(--font-mono);
  word-break: break-all;
  user-select: all;
}

/* The copy button sits on the code surface, so it takes the code colours in both themes. */
.secret__token .btn {
  border-color: var(--code-line);
  background: color-mix(in srgb, var(--code-text) 8%, transparent);
  color: var(--code-text);
}

.secret__token .btn:hover {
  border-color: var(--code-muted);
  background: color-mix(in srgb, var(--code-text) 16%, transparent);
}

.secret__token .btn[data-copied] {
  border-color: var(--code-muted);
  color: var(--code-text);
}

.secret__token .btn:focus-visible {
  outline-color: var(--code-hl);
}

.secret__clients {
  gap: var(--sp-16);
}

.secret__panel {
  display: grid;
  gap: var(--sp-8);
  min-width: 0;
}

.secret__foot {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-12);
  font-size: var(--fs-14);
}

/* ==========================================================================
   8. Plan table
   ========================================================================== */

.plan-table {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--r-card);
  border-collapse: separate;
  border-spacing: 0;
  font-size: var(--fs-14);
  line-height: 1.45;
  overflow: hidden;
}

.plan-table th,
.plan-table td {
  padding: var(--sp-12);
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

.plan-table tbody tr:last-child > * {
  border-bottom: 0;
}

.plan-table thead th {
  background: var(--surface-2);
  color: var(--muted);
  font: 600 var(--fs-12) / 1.5 var(--font-mono);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.plan-table tbody th {
  font-weight: 600;
}

.plan-table__feature,
.plan-table__detail {
  display: block;
}

.plan-table__detail {
  margin-top: 0.125rem;
  color: var(--muted);
  font-size: var(--fs-13);
  font-weight: 400;
}

.plan-table td {
  width: 22%;
  min-width: 5.5rem;
  color: var(--muted);
}

/* The Operator column carries the blue of the plan chip. */
.plan-table :is(thead th:last-child, td[data-plan="operator"]) {
  background: color-mix(in srgb, var(--accent) 9%, var(--surface));
  color: var(--text);
}

.plan-table thead th:last-child {
  color: var(--accent);
}

.plan-table td[data-plan="operator"] {
  font-weight: 600;
}

.plan-table td .icon {
  width: 1.125rem;
  height: 1.125rem;
  color: var(--ok);
}

/* ==========================================================================
   9. Checkout bar and pricing note
   ========================================================================== */

/* The payment result. It stays at the top of the window while the page
   scrolls, so it is seen wherever the visitor is put back. */
.account-banner {
  --tone: var(--sev-info);
  position: sticky;
  top: 0;
  z-index: 20;
  border-bottom: 1px solid color-mix(in srgb, var(--tone) 45%, transparent);
  background: color-mix(in srgb, var(--tone) 14%, var(--surface));
  color: var(--text);
  font-size: var(--fs-14);
  line-height: 1.5;
}

.account-banner[data-tone="success"] {
  --tone: var(--ok);
}

.account-banner[data-tone="warn"] {
  --tone: var(--gap);
}

.account-banner[data-tone="error"] {
  --tone: var(--danger);
}

.account-banner__inner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--sp-8) var(--sp-12);
  min-height: 3.25rem;
  padding-block: var(--sp-8);
}

.account-banner__inner > .icon {
  width: 1.25rem;
  height: 1.25rem;
  color: var(--tone);
}

.account-banner__text {
  flex: 1 1 14rem;
  min-width: 0;
}

.account-banner__text strong {
  font-weight: 650;
}

.account-banner__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-8);
}

/* Dismiss keeps the right edge, also when the actions wrap under the text. */
.account-banner__close {
  flex: none;
  margin-right: calc(-1 * var(--sp-8));
  margin-left: auto;
}

.account-banner__spinner {
  flex: none;
  width: 1rem;
  height: 1rem;
  border: 2px solid var(--tone);
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@media (prefers-reduced-motion: reduce) {
  .account-banner__spinner {
    border-right-color: var(--tone);
    opacity: 0.6;
    animation: none;
  }
}

/* A cancelled checkout: one quiet line where the buy button is. */
.account-note {
  display: flex;
  align-items: flex-start;
  gap: var(--sp-8);
  margin-block: var(--sp-12);
  color: var(--muted);
  font-size: var(--fs-14);
  line-height: 1.5;
}

.account-note .icon {
  margin-top: 0.25em;
}

@media print {
  .account-banner,
  .account-note {
    display: none;
  }
}
