:root {
  --bg: #ffffff;
  --surface: #f7f8fa;
  --border: #e3e6ec;
  --border-strong: #c9cfda;
  --text: #1b2333;
  --text-muted: #67708a;
  --accent: #1f6feb;
  --accent-hover: #1a5fd0;
  --accent-soft: #eaf2ff;
  --success: #0f7b46;
  --success-soft: #e6f5ee;
  --danger: #b3261e;
  --danger-soft: #fdecea;
  --warn: #8a5a00;
  --warn-soft: #fff4e0;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(27, 35, 51, 0.06), 0 4px 12px rgba(27, 35, 51, 0.06);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

.wrap { max-width: 720px; margin: 0 auto; padding: 16px; }

/* --- header ------------------------------------------------------------ */
.header {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 12px; padding-bottom: 12px; border-bottom: 1px solid var(--border);
}
.header h1 { margin: 0 0 2px; font-size: 16px; font-weight: 600; }
.payer { color: var(--text-muted); font-size: 13px; line-height: 1.5; }
.payer strong { color: var(--text); font-weight: 600; }

.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 9px; border-radius: 999px; font-size: 11px; font-weight: 600;
  letter-spacing: .02em; white-space: nowrap;
}
.badge-test { background: var(--warn-soft); color: var(--warn); }
.badge-new { background: var(--accent-soft); color: var(--accent); }
.badge-existing { background: var(--success-soft); color: var(--success); }

/* --- tabs -------------------------------------------------------------- */
.tabs { display: flex; gap: 2px; margin: 14px 0 16px; border-bottom: 1px solid var(--border); }
.tab {
  appearance: none; border: 0; background: none; cursor: pointer;
  padding: 9px 14px; font: inherit; font-weight: 500; color: var(--text-muted);
  border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tab:hover { color: var(--text); }
.tab[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); }

.panel[hidden] { display: none; }

/* --- forms ------------------------------------------------------------- */
.field { margin-bottom: 14px; }
.field label { display: block; font-weight: 500; margin-bottom: 5px; font-size: 13px; }
.hint { color: var(--text-muted); font-size: 12px; margin-top: 5px; line-height: 1.45; }

input[type="text"], input[type="number"], select, textarea {
  width: 100%; padding: 9px 10px; font: inherit;
  border: 1px solid var(--border-strong); border-radius: var(--radius);
  background: var(--bg); color: var(--text);
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
input:disabled, select:disabled { background: var(--surface); color: var(--text-muted); }

.row { display: flex; gap: 10px; }
.row > * { flex: 1; }
.row .grow-2 { flex: 2; }

/* --- payment method list ----------------------------------------------- */
.methods { display: flex; flex-direction: column; gap: 8px; margin-bottom: 6px; }
.method {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border: 1px solid var(--border-strong);
  border-radius: var(--radius); cursor: pointer; background: var(--bg);
}
.method:hover { border-color: var(--accent); background: var(--accent-soft); }
.method.selected { border-color: var(--accent); background: var(--accent-soft); box-shadow: 0 0 0 1px var(--accent); }
.method input { margin: 0; accent-color: var(--accent); }
.method-body { flex: 1; min-width: 0; }
.method-label { font-weight: 500; }
.method-meta { color: var(--text-muted); font-size: 12px; margin-top: 1px; }
.method-actions { display: flex; gap: 6px; }

.chip {
  font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em;
  padding: 2px 6px; border-radius: 4px; background: var(--surface); color: var(--text-muted);
}
.chip-default { background: var(--success-soft); color: var(--success); }

/* --- buttons ----------------------------------------------------------- */
.btn {
  appearance: none; font: inherit; font-weight: 500; cursor: pointer;
  padding: 9px 16px; border-radius: var(--radius);
  border: 1px solid var(--border-strong); background: var(--bg); color: var(--text);
}
.btn:hover:not(:disabled) { background: var(--surface); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-link {
  border: 0; background: none; color: var(--accent); padding: 4px 6px; font-size: 13px;
}
.btn-link:hover:not(:disabled) { background: var(--accent-soft); }
.btn-danger-link { color: var(--danger); }
.btn-danger-link:hover:not(:disabled) { background: var(--danger-soft); }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.actions { display: flex; gap: 10px; align-items: center; margin-top: 16px; }

/* --- stripe element ---------------------------------------------------- */
.element-host {
  padding: 12px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); min-height: 48px;
}
.element-host[hidden] { display: none; }

/* --- status ------------------------------------------------------------ */
.alert {
  padding: 11px 13px; border-radius: var(--radius); margin: 14px 0;
  border: 1px solid transparent; line-height: 1.5;
}
.alert-error { background: var(--danger-soft); border-color: #f3c6c2; color: var(--danger); }
.alert-success { background: var(--success-soft); border-color: #b9e2ce; color: var(--success); }
.alert-info { background: var(--accent-soft); border-color: #c3dbff; color: #14448f; }

/* A payment that is still resolving. The spinner is the difference between
   "we are waiting" and "this has hung" — the wording alone cannot say which. */
.alert-busy { display: flex; align-items: flex-start; gap: 10px; }
.alert-busy .alert-text { flex: 1 1 auto; min-width: 0; }
.alert-busy strong { display: block; }

.alert-spinner {
  flex: 0 0 auto; width: 15px; height: 15px; margin-top: 2px;
  border: 2px solid rgba(20, 68, 143, .18);
  border-top-color: #14448f;
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@media (prefers-reduced-motion: reduce) { .alert-spinner { animation: none; } }
.alert-warn { background: var(--warn-soft); border-color: #f0d9a8; color: var(--warn); }
.alert strong { display: block; margin-bottom: 3px; }
.alert code { font-size: 12px; background: rgba(0,0,0,.05); padding: 1px 4px; border-radius: 3px; }
.alert[hidden] { display: none; }

.spinner {
  display: inline-block; width: 14px; height: 14px; vertical-align: -2px;
  border: 2px solid rgba(255,255,255,.4); border-top-color: #fff;
  border-radius: 50%; animation: spin .7s linear infinite; margin-right: 7px;
}
.btn:not(.btn-primary) .spinner { border-color: rgba(0,0,0,.15); border-top-color: var(--text); }
@keyframes spin { to { transform: rotate(360deg); } }

.loading { padding: 40px 16px; text-align: center; color: var(--text-muted); }

/* --- boot loader --------------------------------------------------------
   The widget sits in a CRM iframe, so its loading state is the first thing
   anyone sees and it should read as part of the page around it rather than a
   bare "Loading..." string. A ring in the accent colour, then placeholders
   shaped like the form that follows. */
.boot { padding: 32px 16px 24px; text-align: center; }

.boot-ring {
  width: 28px; height: 28px; margin: 0 auto 12px;
  border: 3px solid var(--accent-soft);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}

.boot-label { color: var(--text-muted); font-size: 13px; }

.skeleton { margin-top: 24px; text-align: left; }

.sk {
  background: linear-gradient(90deg, var(--surface) 25%, #eef0f4 37%, var(--surface) 63%);
  background-size: 400% 100%;
  animation: shimmer 1.4s ease-in-out infinite;
  border-radius: 4px;
  margin-bottom: 10px;
}
.sk-title { height: 14px; width: 45%; }
.sk-line  { height: 11px; width: 100%; }
.sk-short { width: 65%; }
.sk-block { height: 72px; width: 100%; margin-top: 18px; border-radius: var(--radius); }

@keyframes shimmer { 0% { background-position: 100% 50%; } 100% { background-position: 0 50%; } }

/* A spinner is a nice touch; a moving target for someone with vestibular
   issues is not. Hold the shapes still and let the label carry the meaning. */
@media (prefers-reduced-motion: reduce) {
  .boot-ring { animation: none; border-top-color: var(--accent); }
  .sk { animation: none; }
}

/* --- subscription list -------------------------------------------------- */
.sub-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; }
.sub {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface);
}
.sub-title { font-weight: 500; }
.sub-meta { color: var(--text-muted); font-size: 12px; margin-top: 2px; }
.status-dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 6px; }
.status-active { background: var(--success); }
.status-incomplete, .status-past_due, .status-unpaid { background: var(--warn); }
.status-canceled, .status-incomplete_expired { background: var(--text-muted); }
.status-trialing { background: var(--accent); }

.empty { color: var(--text-muted); padding: 14px; text-align: center; background: var(--surface); border-radius: var(--radius); }
.divider { display: flex; align-items: center; gap: 10px; color: var(--text-muted); font-size: 12px; margin: 16px 0; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }

fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; margin: 0 0 14px; }
legend { font-weight: 600; font-size: 13px; padding: 0 5px; }

/* --- checkbox row ------------------------------------------------------- */
label.check {
  display: flex; align-items: flex-start; gap: 9px;
  font-weight: 500; cursor: pointer; margin-bottom: 0;
}
label.check input[type="checkbox"] {
  width: 16px; height: 16px; margin: 1px 0 0; flex: none;
  accent-color: var(--accent); cursor: pointer;
}
#recurringFields {
  border-left: 3px solid var(--accent);
  padding: 12px 0 2px 14px; margin: 12px 0 16px;
  background: linear-gradient(90deg, var(--accent-soft), transparent 70%);
  border-radius: 0 var(--radius) var(--radius) 0;
}
#recurringFields[hidden] { display: none; }
#recSummary { margin: 2px 0 0; font-weight: 500; color: var(--text); }

/* --- payment element load state ---------------------------------------- */
.element-status {
  padding: 14px; text-align: center; color: var(--text-muted);
  font-size: 13px; line-height: 1.5;
}
.element-status-error {
  text-align: left; color: var(--danger);
  background: var(--danger-soft); border: 1px solid #f3c6c2;
  border-radius: var(--radius);
}
.element-status-error strong { display: block; margin-bottom: 3px; }
.element-status-error .hint { color: var(--danger); opacity: .85; }

/* --- payment outcome modal ----------------------------------------------
   Covers the whole payment: taking it, then the answer. The overlay is
   deliberately not dismissible by clicking through while it is still working —
   a click that lands on the Collect button behind it would start a second
   payment, which is the one mistake here that costs real money. */
.modal {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  background: rgba(27, 35, 51, .45);
  animation: modal-fade .16s ease-out;
}
/* MUST come after .modal.
   The browser hides [hidden] elements with a user-agent rule, and ANY author
   rule setting display beats it — so .modal { display: flex } silently defeats
   the hidden attribute and the overlay shows on every load, covering the form.
   Toggling this class needs an explicit hidden rule. */
.modal[hidden] { display: none; }

@keyframes modal-fade { from { opacity: 0; } to { opacity: 1; } }

.modal-card {
  background: var(--bg);
  border-radius: 12px;
  box-shadow: 0 10px 40px rgba(27, 35, 51, .22);
  padding: 28px 24px 24px;
  width: 100%; max-width: 320px;
  text-align: center;
  animation: modal-rise .2s cubic-bezier(.2, .8, .3, 1);
}
@keyframes modal-rise { from { transform: translateY(8px) scale(.97); opacity: 0; } }

.modal-icon { height: 64px; margin-bottom: 14px; display: flex; align-items: center; justify-content: center; }
.modal-title { font-size: 16px; font-weight: 600; margin-bottom: 6px; }
.modal-detail { font-size: 13px; color: var(--text-muted); line-height: 1.5; }
.modal-close { margin-top: 18px; }

/* Working: the same ring as the boot loader, larger. */
.modal-ring {
  width: 44px; height: 44px;
  border: 4px solid var(--accent-soft);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}

/* Success and failure draw themselves: the circle sweeps, then the mark. The
   movement is what makes the outcome register from across a desk — a static
   tick looks the same as one that was already there. */
.modal-mark { width: 60px; height: 60px; }
.modal-mark circle, .modal-mark path {
  fill: none; stroke-width: 3;
  stroke-linecap: round; stroke-linejoin: round;
}
.modal-mark circle { stroke-dasharray: 166; stroke-dashoffset: 0; animation: draw-circle .5s cubic-bezier(.65, 0, .45, 1) backwards; }
.modal-mark path   { stroke-dasharray: 48;  stroke-dashoffset: 0;  animation: draw-mark .3s cubic-bezier(.65, 0, .45, 1) .45s backwards; }

.modal-mark-success circle, .modal-mark-success path { stroke: var(--success); }
.modal-mark-error   circle, .modal-mark-error   path { stroke: var(--danger); }
.modal-mark-info    circle, .modal-mark-info    path { stroke: var(--accent); }

@keyframes draw-circle { from { stroke-dashoffset: 166; } to { stroke-dashoffset: 0; } }
@keyframes draw-mark   { from { stroke-dashoffset: 48; }  to { stroke-dashoffset: 0; } }

/* The outcome must be legible without the animation carrying it. */
@media (prefers-reduced-motion: reduce) {
  .modal, .modal-card { animation: none; }
  .modal-ring { animation: none; }
  .modal-mark circle, .modal-mark path { animation: none; stroke-dashoffset: 0; }
}
