@import url("https://fonts.googleapis.com/css2?family=Geist:wght@400;500;600;700;750&display=swap");

:root {
  color-scheme: light;
  --bg: #f8fafc;
  --paper: #fff;
  --ink: #111827;
  --muted: #64748b;
  --line: #e5e7eb;
  --accent: #111827;
  --accent-ink: #fff;
  --warn: #b45309;
  --warn-bg: #fff7ed;
  --ok: #166534;
  --header: #f9fafb;
  --group: #f3f4f6;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Geist", ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

main {
  width: 100%;
  padding: 28px 20px 56px;
}

.auth-shell {
  align-items: center;
  display: flex;
  justify-content: center;
  min-height: 100vh;
}

.auth-card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 10px;
  max-width: 440px;
  padding: 28px;
  width: 100%;
}

.auth-button {
  margin-top: 22px;
  width: 100%;
}

.auth-divider {
  align-items: center;
  color: var(--muted);
  display: flex;
  font-size: 12px;
  gap: 12px;
  margin: 20px 0;
}

.auth-divider::before,
.auth-divider::after {
  background: var(--line);
  content: "";
  height: 1px;
  flex: 1;
}

.auth-error { color: #b91c1c; }

header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
  border-bottom: 1px solid var(--line);
  padding-bottom: 18px;
  margin-bottom: 18px;
}

h1 {
  margin: 0;
  font-size: 28px;
  letter-spacing: 0;
}

h2 {
  margin: 0;
  font-size: 16px;
}

.sub {
  color: var(--muted);
  margin-top: 7px;
  font-size: 14px;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.app-tabs {
  align-items: center;
  border-bottom: 1px solid var(--line);
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
  margin: -4px 0 18px;
  padding: 0 4px;
}

.app-tab {
  border-bottom: 3px solid transparent;
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
  padding: 9px 2px 10px;
  text-decoration: none;
}

.app-tab:hover {
  color: var(--ink);
  text-decoration: none;
}

.app-tab-active {
  border-bottom-color: var(--ink);
  color: var(--ink);
}

.app-subtabs {
  gap: 20px;
  margin-top: -10px;
}

.app-subtabs .app-tab {
  font-size: 13px;
  font-weight: 600;
  padding-top: 6px;
  padding-bottom: 8px;
}

button,
.button {
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-ink);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font: inherit;
  font-weight: 700;
  min-height: 38px;
  padding: 8px 12px;
  text-decoration: none;
}

button:disabled {
  cursor: progress;
  opacity: 0.65;
}

.button.secondary {
  background: var(--paper);
  color: var(--accent);
}

.secondary-button {
  background: var(--paper);
  color: var(--accent);
}

.stats {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 0;
  margin-bottom: 16px;
}

.receivables-stats {
  grid-template-columns: repeat(7, minmax(0, 1fr));
}

.stat.stat-active {
  background: var(--ink);
  color: var(--paper);
}

.stat.stat-active span {
  color: #d1d5db;
}

.receivables-preview {
  display: grid;
  gap: 16px;
}

.pending-workflow {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
}

.pending-workflow-header {
  align-items: flex-start;
  display: flex;
  justify-content: space-between;
  margin-bottom: 14px;
}

.pending-workflow-header h3 {
  font-size: 16px;
  margin: 0;
}

.pending-workflow-header p {
  color: var(--muted);
  font-size: 12px;
  margin: 6px 0 0;
}

.pending-workflow-sections {
  display: grid;
  gap: 14px;
}

.load-error {
  align-items: start;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--muted);
  display: grid;
  gap: 8px;
  grid-column: 1 / -1;
  line-height: 1.4;
  padding: 14px;
}

.load-error strong {
  color: var(--ink);
}

.load-error button {
  justify-self: start;
}

.stat {
  background: var(--paper);
  border: 1px solid var(--line);
  border-left-width: 0;
  border-radius: 0;
  color: inherit;
  cursor: default;
  display: block;
  font: inherit;
  padding: 10px 10px;
  text-align: left;
  min-width: 0;
}

.stat:disabled {
  cursor: default;
  opacity: 1;
}

.stat-button {
  cursor: pointer;
}

.stat-button:hover {
  background: var(--header);
}

.stat-right {
  text-align: right;
}

.stat:first-child {
  border-left-width: 1px;
  border-bottom-left-radius: 8px;
  border-top-left-radius: 8px;
}

.stat:last-child {
  border-bottom-right-radius: 8px;
  border-top-right-radius: 8px;
}

.stat strong {
  display: block;
  font-size: 18px;
}

.stat span {
  color: var(--muted);
  font-size: 11px;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 14px;
}

.controls input,
.controls select {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  font: inherit;
  min-height: 38px;
  padding: 8px 10px;
}

.controls input {
  min-width: min(420px, 100%);
}

section {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 8px;
  margin-top: 14px;
  overflow: hidden;
}

.section-title,
summary {
  align-items: center;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  list-style: none;
  padding: 14px 16px;
}

summary {
  cursor: pointer;
}

summary::-webkit-details-marker {
  display: none;
}

.title {
  font-weight: 750;
}

.badge {
  background: #f3f4f6;
  border-radius: 999px;
  color: var(--ink);
  font-size: 13px;
  font-weight: 750;
  padding: 3px 9px;
}

.admin-layout {
  display: grid;
  gap: 14px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  padding: 16px;
}

.admin-layout-single {
  grid-template-columns: minmax(280px, 420px) minmax(0, 1fr);
}

.admin-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
}

.admin-panel-full {
  grid-column: 1 / -1;
}

.admin-panel h3 {
  font-size: 15px;
  margin: 0 0 12px;
}

.admin-button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 12px;
}

.admin-form {
  display: flex;
  gap: 10px;
  align-items: end;
}

.admin-field {
  display: block;
  flex: 1 1 auto;
}

.admin-field span {
  color: var(--muted);
  display: block;
  font-size: 12px;
  margin-bottom: 6px;
}

.admin-field input {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  font: inherit;
  min-height: 38px;
  padding: 8px 10px;
  width: 100%;
}

.admin-note,
.admin-empty,
.admin-meta {
  color: var(--muted);
  font-size: 12px;
}

.admin-note {
  margin: 12px 0 0;
}

.admin-list {
  display: grid;
  gap: 8px;
}

.admin-row {
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
}

.admin-email {
  font-weight: 600;
}

.admin-remove-button {
  white-space: nowrap;
}

.admin-result {
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
  padding: 10px 12px;
}

.admin-result strong {
  color: var(--ink);
}

.digest-preview {
  display: grid;
  gap: 16px;
}

.writeback-preview {
  display: block;
}

.digest-section {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}

.digest-section-title {
  align-items: center;
  background: var(--header);
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  padding: 10px 12px;
}

.digest-section-actions {
  align-items: center;
  display: flex;
  gap: 8px;
}

.digest-section-title h4 {
  font-size: 14px;
  margin: 0;
}

.digest-table-wrap {
  overflow-x: auto;
}

.digest-table {
  min-width: 1100px;
}

.digest-table th:nth-child(1) { width: 240px; }
.digest-table th:nth-child(2) { width: 220px; }
.digest-table th:nth-child(3) { width: 150px; }
.digest-table th:nth-child(4) { width: 130px; }
.digest-table th:nth-child(5) { width: 170px; }
.digest-table th:nth-child(6) { width: 120px; }
.digest-table th:nth-child(7) { width: 120px; }
.digest-table th:nth-child(8) { width: 260px; }
.digest-table th:last-child { width: 260px; }

.pending-new-customer-table {
  min-width: 1280px;
}

.pending-new-customer-table th:nth-child(1) { width: 220px; }
.pending-new-customer-table th:nth-child(2) { width: 220px; }
.pending-new-customer-table th:nth-child(3) { width: 150px; }
.pending-new-customer-table th:nth-child(4) { width: 170px; }
.pending-new-customer-table th:nth-child(5) { width: 130px; }
.pending-new-customer-table th:nth-child(6) { width: 120px; }
.pending-new-customer-table th:nth-child(7) { width: 120px; }
.pending-new-customer-table th:nth-child(8) { width: 110px; }
.pending-new-customer-table th:nth-child(9) { width: 220px; }

.digest-table tr.digest-group-a td {
  background: #fff;
}

.digest-table tr.digest-group-b td {
  background: #f8fafc;
}

.digest-table tr.digest-customer-header td {
  border-top: 10px solid var(--paper);
  color: var(--ink);
  font-weight: 750;
}

.digest-table tr.digest-customer-header.digest-group-a td {
  background: #e5e7eb;
}

.digest-table tr.digest-customer-header.digest-group-b td {
  background: #f1f5f9;
}

.writeback-table-wrap {
  overflow-x: auto;
}

.writeback-table {
  min-width: 1180px;
}

.writeback-table th:nth-child(1) { width: 260px; }
.writeback-table th:nth-child(2) { width: 140px; }
.writeback-table th:nth-child(3) { width: 170px; }
.writeback-table th:nth-child(4) { width: 120px; }
.writeback-table th:nth-child(5) { width: 120px; }
.writeback-table th:nth-child(6) { width: 120px; }
.writeback-table th:nth-child(7) { width: 210px; }
.writeback-table th:nth-child(8) { width: 170px; }

.writeback-update td {
  background: var(--warn-bg);
}

.writeback-unchanged td {
  background: #fff;
}

.writeback-missing-stripe-customer td,
.writeback-stripe-error td,
.writeback-missing-stripe-live-mrr td {
  background: #f8fafc;
}

.table-wrap {
  overflow-x: auto;
  width: 100%;
}

table {
  border-collapse: collapse;
  font-size: 13px;
  table-layout: fixed;
  min-width: 1380px;
  width: 100%;
}

.col-company { width: 300px; }
.col-hubspot-id { width: 150px; }
.col-stripe-id { width: 165px; }
.col-product { width: 220px; }
.col-unit-price { width: 110px; }
.col-quantity { width: 90px; }
.col-contracted { width: 135px; }
.col-expected { width: 150px; }
.col-stripe-live { width: 125px; }
.col-variance { width: 135px; }
.col-next-invoice { width: 205px; }

th,
td {
  border-bottom: 1px solid #ece7dc;
  padding: 9px 10px;
  text-align: left;
  vertical-align: top;
}

thead th {
  background: var(--header);
  color: var(--muted);
  font-size: 12px;
  position: sticky;
  top: 0;
  user-select: none;
  white-space: nowrap;
  z-index: 2;
}

th.sortable {
  cursor: pointer;
}

tr.group-header {
  background: var(--group);
  border-top: 18px solid var(--paper);
  font-weight: 750;
}

.matrix-error-detail {
  color: #9f1239;
  font-size: 12px;
  font-weight: 500;
  line-height: 1.35;
  margin-top: 5px;
  overflow-wrap: anywhere;
}

.matrix-error-detail span {
  font-weight: 750;
}

.stat-button.stat-active {
  background: #e8eef8;
  color: var(--ink);
  border-color: #6b7fa6;
  box-shadow: inset 0 0 0 1px #6b7fa6;
}

.stat-button.stat-active span {
  color: #475569;
}

tr.discrepancy {
  background: var(--warn-bg);
}

tr.hidden {
  display: none;
}

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

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
}

a {
  color: var(--accent);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

#toast {
  background: var(--ink);
  border-radius: 8px;
  bottom: 16px;
  color: white;
  display: none;
  left: 50%;
  max-width: min(760px, calc(100vw - 32px));
  padding: 10px 12px;
  position: fixed;
  transform: translateX(-50%);
}

#toast.visible {
  display: block;
}

@media (max-width: 760px) {
  header {
    align-items: flex-start;
    display: block;
  }

  .actions {
    margin-top: 14px;
  }

  .stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  .receivables-stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .admin-layout {
    grid-template-columns: 1fr;
  }

  .admin-form {
    align-items: stretch;
    flex-direction: column;
  }

  .stat,
  .stat:first-child,
  .stat:last-child {
    border-left-width: 1px;
    border-radius: 8px;
  }
}
.cleanup-table { overflow-x: auto; margin-top: 12px; }
#mrr-type-table tbody tr { background: var(--warn-bg); }
.cleanup-sort { background: none; border: 0; color: inherit; font: inherit; font-weight: 600; min-height: 28px; padding: 0; text-align: left; }
.cleanup-column-filter { box-sizing: border-box; display: block; font-size: 12px; margin-top: 6px; max-width: 100%; min-width: 0; width: 100%; }
.refresh-banner { position: sticky; top: 10px; z-index: 100; display: flex; align-items: center; gap: 14px; margin-bottom: 18px; padding: 16px 18px; border: 1px solid #6b84a8; border-left: 5px solid #315f9a; border-radius: 6px; background: #edf3fc; color: #18375f; box-shadow: 0 3px 10px #182d4914; }
.refresh-banner[hidden] { display: none; }
.refresh-banner strong, .refresh-banner div > span { display: block; }
.refresh-banner strong { font-size: 16px; margin-bottom: 3px; }
.refresh-banner div > span { font-size: 13px; line-height: 1.4; }
.refresh-elapsed { margin-left: auto; white-space: nowrap; font-size: 13px; font-variant-numeric: tabular-nums; }
.refresh-spinner { flex: 0 0 auto; width: 22px; height: 22px; border: 3px solid currentColor; border-top-color: transparent; border-radius: 50%; animation: refresh-spin 1s linear infinite; }
.refresh-banner:not([data-state="loading"]) .refresh-spinner { display: none; }
.refresh-banner[data-state="success"] { background: #edf7f0; border-color: #568866; color: #245136; }
.refresh-banner[data-state="warning"], .refresh-banner[data-state="error"] { background: #fff3df; border-color: #ad741d; color: #70440a; }
@keyframes refresh-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .refresh-spinner { animation: none; border-top-color: currentColor; } }
@media (max-width: 600px) { .refresh-banner { flex-wrap: wrap; padding: 12px; } .refresh-banner div { flex: 1; } }
.builder-controls { display: grid; grid-template-columns: minmax(140px, 180px) minmax(220px, 1fr) max-content; align-items: end; }
.action-dialog { box-sizing: border-box; width: min(680px, calc(100vw - 32px)); max-height: calc(100vh - 40px); padding: 24px; border: 1px solid #d1d5db; border-radius: 8px; background: #fff; color: #1f2937; box-shadow: 0 20px 60px #11182740; }
.action-dialog::backdrop { background: #11182770; }
.action-dialog h2 { margin: 0 0 16px; }
.action-dialog-message { white-space: pre-wrap; overflow-wrap: anywhere; max-height: 55vh; overflow-y: auto; font-size: 14px; line-height: 1.6; }
.action-dialog-actions { justify-content: flex-end; padding-top: 16px; margin-top: 20px; margin-bottom: 0; border-top: 1px solid #e5e7eb; }
.action-banner { z-index: 110; background: #f8fafc; color: #1f2937; border-color: #94a3b8; }
.action-banner .action-dismiss { min-height: 30px; padding: 4px 10px; font-size: 12px; }
.builder-controls label { display: grid; gap: 6px; }
.builder-controls input, .builder-controls select { width: 100%; max-width: none; }
.builder-controls button { min-height: 40px; }
.builder-option { display: flex; align-items: center; gap: 8px; margin: 12px 0; font-size: 14px; }
.builder-option input { width: auto; margin: 0; }
.onboarding-completion { margin: 20px 0; padding: 18px; border: 1px solid #cbd5e1; border-radius: 8px; background: #f8fafc; }
.onboarding-result { padding: 12px 0; border-top: 1px solid #dbe1e8; }
.onboarding-result p { margin: 6px 0; }
.onboarding-result strong { color: #166534; }
.onboarding-result[data-pending="true"] strong { color: #92400e; }
@media (max-width: 700px) { .builder-controls { grid-template-columns: 1fr; } }
.timezone-controls { display: grid; grid-template-columns: minmax(180px, 240px) minmax(220px, 1fr) max-content max-content; align-items: end; }
.timezone-controls label { display: grid; gap: 6px; min-width: 0; }
.timezone-controls input, .timezone-controls select { width: 100%; min-width: 0; max-width: none; height: 40px; }
.timezone-controls button { height: 40px; white-space: nowrap; }
@media (max-width: 900px) { .timezone-controls { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 560px) { .timezone-controls { grid-template-columns: 1fr; } }
