/* ==========================================================================
   Component kit — the only widgets used anywhere in the app.
   If a page needs something new, add it here first, then use it 1:1.
   Colours come from tokens.css only (enforced by tests/tokens.test.mjs).
   ========================================================================== */

/* --- Buttons ------------------------------------------------------------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 36px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--ink);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  line-height: 1;
  white-space: nowrap;
  text-decoration: none;
  box-shadow: var(--shadow-1);
  transition:
    background var(--dur) var(--ease),
    border-color var(--dur) var(--ease),
    color var(--dur) var(--ease);
}

.btn:hover {
  background: var(--surface-raised);
  border-color: var(--border-strong);
  color: var(--ink);
  text-decoration: none;
}

.btn:active {
  background: var(--surface-sunken);
}

.btn svg {
  width: 16px;
  height: 16px;
  flex: none;
}

.btn--primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}

.btn--primary:hover {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
  color: var(--on-accent);
}

.btn--primary:active {
  background: var(--accent-strong);
}

.btn--ghost {
  background: transparent;
  border-color: transparent;
  color: var(--ink-muted);
  box-shadow: none;
}

.btn--ghost:hover {
  background: var(--surface-sunken);
  border-color: transparent;
  color: var(--ink);
}

.btn--danger {
  background: transparent;
  border-color: var(--danger-soft-border);
  color: var(--danger-ink);
  box-shadow: none;
}

.btn--danger:hover {
  background: var(--danger-soft);
  border-color: var(--danger-soft-border);
  color: var(--danger-ink);
}

.btn--sm {
  height: 30px;
  padding: 0 10px;
  font-size: var(--text-xs);
}

.btn--icon {
  width: 36px;
  padding: 0;
}

.btn--block {
  width: 100%;
}

.btn:disabled,
.btn[aria-disabled='true'] {
  opacity: 0.55;
  cursor: not-allowed;
  box-shadow: none;
}

.btn:disabled:hover,
.btn[aria-disabled='true']:hover {
  background: var(--surface);
  border-color: var(--border);
}

.btn--primary:disabled:hover,
.btn--primary[aria-disabled='true']:hover {
  background: var(--accent);
  border-color: var(--accent);
}

.btn.is-loading {
  cursor: progress;
}

.btn.is-loading .btn__label {
  opacity: 0.7;
}

.btn .spinner {
  display: none;
}

.btn.is-loading .spinner {
  display: inline-block;
}

/* --- Spinner -------------------------------------------------------------- */
.spinner {
  width: 1em;
  height: 1em;
  flex: none;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: var(--radius-full);
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(1turn);
  }
}

/* --- Fields --------------------------------------------------------------- */
.field {
  display: grid;
  gap: 6px;
}

.field__label {
  color: var(--ink-muted);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
}

.field__hint {
  color: var(--ink-muted);
  font-size: var(--text-xs);
}

.field__error {
  display: flex;
  align-items: center;
  gap: 5px;
  color: var(--danger-ink);
  font-size: var(--text-xs);
}

.field__error svg {
  width: 14px;
  height: 14px;
  flex: none;
}

.input,
.select,
.textarea {
  width: 100%;
  height: 36px;
  padding: 0 12px;
  border: 1px solid var(--border-input);
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--ink);
  font-size: var(--text-sm);
  transition:
    border-color var(--dur) var(--ease),
    box-shadow var(--dur) var(--ease);
}

.textarea {
  height: auto;
  min-height: 88px;
  padding: 10px 12px;
  resize: vertical;
}

.input:hover,
.select:hover,
.textarea:hover {
  border-color: var(--border-strong);
}

.input:focus-visible,
.select:focus-visible,
.textarea:focus-visible {
  outline: 2px solid var(--ring);
  outline-offset: 1px;
  border-color: var(--ring);
}

.input[aria-invalid='true'],
.textarea[aria-invalid='true'] {
  border-color: var(--danger);
}

.input:disabled,
.select:disabled,
.textarea:disabled {
  background: var(--surface-sunken);
  color: var(--ink-muted);
  cursor: not-allowed;
}

.checkbox {
  width: 16px;
  height: 16px;
  margin: 0;
  accent-color: var(--accent);
}

/* --- Cards ---------------------------------------------------------------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-1);
}

.card--flat {
  box-shadow: none;
}

.card__head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-5) var(--space-5) var(--space-3);
}.card__title {
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
}

.card__sub {
  margin-top: 2px;
  color: var(--ink-muted);
  font-size: var(--text-xs);
}

.card__actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex: none;
}

.card__body {
  padding: 0 var(--space-5) var(--space-5);
}

.card__body--flush {
  padding: 0;
}

.card__foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-5);
  border-top: 1px solid var(--border-subtle);
  color: var(--ink-muted);
  font-size: var(--text-xs);
}

/* --- Stat tile ------------------------------------------------------------ */
.stat {
  display: grid;
  gap: 2px;
  align-content: start;
  padding: var(--space-4) var(--space-5);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-1);
}

.stat__label {
  color: var(--ink-muted);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  letter-spacing: var(--tracking-wide);
  text-transform: uppercase;
}

.stat__value {
  font-family: var(--font-num);
  font-size: var(--text-2xl);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-tight);
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
}

.stat__value--credit {
  color: var(--ok);
}

.stat__value--danger {
  color: var(--danger);
}

.stat__delta {
  color: var(--ink-muted);
  font-size: var(--text-xs);
}

/* --- Pills and stage ------------------------------------------------------ */
.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 24px;
  padding: 0 9px;
  border: 1px solid transparent;
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  line-height: 1;
  white-space: nowrap;
}

.pill--neutral {
  background: var(--surface-sunken);
  border-color: var(--border-subtle);
  color: var(--ink-muted);
}

.pill--accent {
  background: var(--accent-soft);
  border-color: var(--accent-soft-border);
  color: var(--accent-ink);
}

.pill--ok {
  background: var(--ok-soft);
  border-color: var(--ok-soft-border);
  color: var(--ok-ink);
}

.pill--warn {
  background: var(--warn-soft);
  border-color: var(--warn-soft-border);
  color: var(--warn-ink);
}

.pill--danger {
  background: var(--danger-soft);
  border-color: var(--danger-soft-border);
  color: var(--danger-ink);
}

.pill__dot {
  width: 6px;
  height: 6px;
  flex: none;
  border-radius: var(--radius-full);
  background: currentColor;
}

/* Stage: neutral pill + one coloured dot; the label always carries the word. */
.stage {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  justify-self: start;
  height: 24px;
  padding: 0 9px 0 8px;
  background: var(--surface-sunken);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-full);
  color: var(--ink-muted);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  line-height: 1;
  white-space: nowrap;
}

.stage__dot {
  width: 8px;
  height: 8px;
  flex: none;
  border-radius: var(--radius-full);
  border: 1px solid color-mix(in oklab, var(--ink) 18%, transparent);
  background: var(--stage-lead);
}

.stage[data-stage='lead'] .stage__dot {
  background: var(--stage-lead);
}

.stage[data-stage='intake_booked'] .stage__dot {
  background: var(--stage-intake-booked);
}

.stage[data-stage='documents_collected'] .stage__dot {
  background: var(--stage-documents-collected);
}

.stage[data-stage='prep'] .stage__dot {
  background: var(--stage-prep);
}

.stage[data-stage='client_review'] .stage__dot {
  background: var(--stage-client-review);
}

.stage[data-stage='invoice_sent'] .stage__dot {
  background: var(--stage-invoice-sent);
}

.stage[data-stage='invoice_paid'] .stage__dot {
  background: var(--stage-invoice-paid);
}

.stage[data-stage='filed'] .stage__dot {
  background: var(--stage-filed);
}

/* --- Person chip ---------------------------------------------------------- */
.chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.chip__avatar {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  flex: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  background: var(--surface-sunken);
  color: var(--ink-muted);
  font-size: 11px;
  font-weight: var(--weight-semibold);
  letter-spacing: 0;
}

.chip__name {
  display: block;
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  line-height: 1.3;
}

.chip__role {
  display: block;
  color: var(--ink-muted);
  font-size: 11px;
  line-height: 1.3;
}

.chip__state {
  width: 7px;
  height: 7px;
  flex: none;
  border-radius: var(--radius-full);
  background: var(--ink-faint);
}

.chip__state[data-state='signed'] {
  background: var(--ok);
}

.chip__state[data-state='awaiting'] {
  background: var(--warn);
}

.chip__state[data-state='not-sent'] {
  background: var(--ink-faint);
}

/* --- Money ---------------------------------------------------------------- */
.money {
  font-weight: var(--weight-medium);
  white-space: nowrap;
}

.money--credit {
  color: var(--ok);
}

.money--overdue {
  color: var(--danger);
}

.money--muted {
  color: var(--ink-muted);
}

.money--lg {
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
}

.money--xl {
  font-size: var(--text-2xl);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-tight);
}

.cell-money {
  text-align: right;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.cell-actions {
  text-align: right;
  white-space: nowrap;
}

/* --- Table ---------------------------------------------------------------- */
.table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: var(--text-sm);
}

.table thead th {
  padding: 10px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  color: var(--ink-muted);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  letter-spacing: var(--tracking-wide);
  text-align: left;
  text-transform: uppercase;
  white-space: nowrap;
}

.table tbody td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border-subtle);
  vertical-align: middle;
}

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

.table tbody tr:hover {
  background: color-mix(in oklab, var(--ink) 3%, var(--surface));
}

.table .num,
.table .cell-money {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.table--dense tbody td {
  padding: 9px 14px;
}

/* --- Empty state ---------------------------------------------------------- */
.empty {
  display: grid;
  place-items: center;
  padding: var(--space-8) var(--space-5);
  text-align: center;
}

.empty__icon {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  margin-bottom: var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  background: var(--surface-sunken);
  color: var(--ink-muted);
}

.empty__icon svg {
  width: 20px;
  height: 20px;
}

.empty__title {
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
}

.empty__body {
  max-width: 42ch;
  margin-top: 2px;
  color: var(--ink-muted);
  font-size: var(--text-sm);
}

.empty__action {
  margin-top: var(--space-4);
}

/* --- Dialog --------------------------------------------------------------- */
.dialog {
  width: min(480px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  background: var(--surface-raised);
  color: var(--ink);
  box-shadow: var(--shadow-dialog);
}

.dialog::backdrop {
  background: var(--overlay);
}

.dialog__head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-5) var(--space-5) var(--space-2);
}

.dialog__title {
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
}

.dialog__sub {
  margin-top: 2px;
  color: var(--ink-muted);
  font-size: var(--text-sm);
}

.dialog__body {
  display: grid;
  gap: var(--space-4);
  padding: var(--space-4) var(--space-5);
}

.dialog__foot {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  padding: var(--space-4) var(--space-5) var(--space-5);
}

/* --- Toast ---------------------------------------------------------------- */
.toast-region {
  position: fixed;
  right: var(--space-6);
  bottom: var(--space-6);
  z-index: var(--z-toast);
  display: grid;
  gap: var(--space-2);
  max-width: 380px;
}

.toast {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-md);
  background: var(--surface-raised);
  box-shadow: var(--shadow-toast);
  font-size: var(--text-sm);
}

.toast--ok {
  border-left-color: var(--ok);
}

.toast--warn {
  border-left-color: var(--warn);
}

.toast--danger {
  border-left-color: var(--danger);
}

.toast__body {
  flex: 1;
}

.toast__title {
  font-weight: var(--weight-medium);
}

.toast__text {
  color: var(--ink-muted);
  font-size: var(--text-xs);
}

/* --- Progress, kbd, banner, toolbar -------------------------------------- */
.progress {
  height: 6px;
  overflow: hidden;
  border-radius: var(--radius-full);
  background: var(--surface-sunken);
}

.progress__bar {
  height: 100%;
  width: var(--progress, 0%);
  border-radius: inherit;
  background: var(--accent);
}

.progress__bar--ok {
  background: var(--ok);
}

.kbd {
  padding: 1px 5px;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--ink-muted);
  font-family: var(--font-ui);
  font-size: 11px;
  line-height: 1.5;
}

.banner {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--accent-soft-border);
  border-radius: var(--radius-md);
  background: var(--accent-soft);
  color: var(--accent-ink);
  font-size: var(--text-sm);
}

.banner svg {
  width: 16px;
  height: 16px;
  flex: none;
  margin-top: 2px;
}

.banner--warn {
  border-color: var(--warn-soft-border);
  background: var(--warn-soft);
  color: var(--warn-ink);
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
}

.segmented {
  display: inline-flex;
  gap: 2px;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-sunken);
}

.segmented__btn {
  height: 28px;
  padding: 0 10px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--ink-muted);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
}

.segmented__btn:hover {
  color: var(--ink);
}

.segmented__btn[aria-pressed='true'] {
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow-1);
}

/* --- Stage rail: counts per stage, used on Today and Pipeline ------------ */
.stage-rail {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 1px;
  background: var(--border-subtle);
  border-top: 1px solid var(--border-subtle);
}

.stage-rail__item {
  display: grid;
  gap: var(--space-2);
  padding: var(--space-4) var(--space-5);
  background: var(--surface);
  color: var(--ink);
  text-decoration: none;
}

.stage-rail__item:hover {
  background: color-mix(in oklab, var(--ink) 3%, var(--surface));
  color: var(--ink);
  text-decoration: none;
}

.stage-rail__count {
  font-size: var(--text-xl);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-tight);
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

@media (max-width: 1180px) {
  .stage-rail {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

@media (max-width: 640px) {
  .stage-rail {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

.divider {
  height: 1px;
  border: 0;
  margin: var(--space-4) 0;
  background: var(--border-subtle);
}
