:root {
  color-scheme: light;
  --accent: #2563eb;
  --border: #d8dee9;
  --danger: #b91c1c;
  --muted: #667085;
  --text: #111827;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: #f7f9fc;
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
a { color: var(--accent); }
h1, h2, h3, p { margin-top: 0; }
h1 { font-size: 2rem; margin-bottom: 0; }
h2 { font-size: 1.2rem; }
h3 { color: var(--muted); font-size: 0.82rem; margin: 18px 0 8px; }

.topbar, .page-header, .toolbar, .actions, .card-title, .metric, .toggle, .check-row {
  align-items: center;
  display: flex;
}
.topbar {
  background: #fff;
  border-bottom: 1px solid var(--border);
  gap: 24px;
  justify-content: space-between;
  padding: 14px clamp(16px, 4vw, 40px);
}
.brand, label span { font-weight: 700; }
.nav-links, .toolbar, .actions { display: flex; gap: 10px; }
.nav-links a {
  border-radius: 6px;
  color: var(--text);
  padding: 8px 10px;
  text-decoration: none;
}
.nav-links a[aria-current="page"] { background: #eaf1ff; color: var(--accent); }

.page { margin: 0 auto; max-width: 1120px; padding: 28px 18px 48px; }
.page-header { justify-content: space-between; margin-bottom: 22px; }
.eyebrow {
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0;
  margin: 0 0 4px;
  text-transform: uppercase;
}
.card-grid, .scope-grid {
  display: grid;
  gap: 16px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
.card, .section, .modal { background: #fff; border: 1px solid var(--border); border-radius: 8px; }
.card, .section { padding: 18px; }
.card-title, .metric { justify-content: space-between; }
.metric {
  align-items: baseline;
  border-top: 1px solid #edf1f7;
  padding: 10px 0;
}
.metric span, .muted { color: var(--muted); }

.status { border-radius: 999px; font-size: 0.78rem; font-weight: 700; padding: 4px 8px; }
.status-ok { background: #dcfce7; color: #166534; }
.status-partial, .notice { background: #fef3c7; color: #92400e; }
.status-error { background: #fee2e2; }
.status-error, .error-text, .btn-danger { color: var(--danger); }
.status-never { background: #eef2f7; color: #475467; }
.ok-text { color: #166534; }

.toolbar, .actions { margin-top: 16px; }
.toolbar {
  flex-wrap: wrap;
  gap: 10px 12px;
}
.toolbar .muted {
  margin-left: auto;
  padding: 8px 0;
}
.section + .section { margin-top: 24px; }
.btn {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  font: inherit;
  padding: 8px 12px;
}
.btn:disabled { cursor: not-allowed; opacity: 0.55; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.spinner {
  border: 2px solid rgb(255 255 255 / 40%);
  border-left-color: #fff;
  border-radius: 50%;
  display: inline-block;
  height: 14px;
  margin-right: 6px;
  vertical-align: -2px;
  width: 14px;
}
.spinner[hidden] { display: none; }
.spinning .spinner { animation: spin 0.8s linear infinite; }

.table-wrap { overflow-x: auto; }
.data-table-wrap {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-top: 18px;
  overflow-x: auto;
}
table { border-collapse: collapse; min-width: 760px; width: 100%; }
th, td {
  border-bottom: 1px solid #edf1f7;
  padding: 10px;
  text-align: left;
  vertical-align: top;
}
th { color: var(--muted); font-size: 0.78rem; }
.data-table {
  min-width: 1040px;
}
.data-table th {
  background: #f9fbff;
  font-weight: 700;
  text-transform: uppercase;
}
.data-table td {
  background: #fff;
}
.data-table tbody tr:hover td {
  background: #f8fbff;
}

.form, .field-list { display: grid; gap: 16px; }
label span { display: block; margin-bottom: 6px; }
input[type="search"], input[type="text"], select {
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
  padding: 9px 10px;
  width: min(420px, 100%);
}
input[type="search"] { width: min(280px, 100%); }
select { background: #fff; width: auto; }
.wide-select { min-width: 220px; max-width: 320px; }
.toggle, .check-row { gap: 8px; }
.toggle span { margin: 0; }
.scope-card h2 { margin-bottom: 10px; }
.field-list { gap: 7px; margin-top: 8px; }
.field-list.disabled { color: var(--muted); }

.notice, .token-block { border-radius: 6px; padding: 10px 12px; }
.notice { border: 1px solid #f59e0b; }
.modal { max-width: 560px; padding: 22px; width: calc(100% - 32px); }
.modal::backdrop { background: rgb(17 24 39 / 45%); }
.token-block {
  background: #f3f6fb;
  border: 1px solid var(--border);
  display: block;
  overflow-wrap: anywhere;
}
.chip {
  border-radius: 12px;
  display: inline-block;
  font-size: 0.85em;
  font-weight: 700;
  padding: 2px 8px;
  white-space: nowrap;
}
.chip-active { background: #d1fae5; color: #065f46; }
.chip-paused { background: #fef3c7; color: #92400e; }
.chip-churned { background: #fee2e2; color: #991b1b; }
.chip-graduated { background: #ede9fe; color: #5b21b6; }
.chip-winback { background: #dbeafe; color: #1e40af; }
.chip-referred, .chip-default { background: #f3f4f6; color: #374151; }
.chip-created { background: #d1fae5; color: #065f46; }
.chip-updated { background: #dbeafe; color: #1e40af; }
.chip-deleted { background: #fee2e2; color: #991b1b; }
.chip-entity { background: #f3f4f6; color: #374151; }
.pagination {
  align-items: center;
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-top: 18px;
}
.auto-refresh { margin-top: 8px; }
.audit-timeline {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-top: 18px;
  overflow: hidden;
}
.audit-event, .audit-empty {
  border-bottom: 1px solid #edf1f7;
  padding: 16px 18px;
}
.audit-event:last-child, .audit-empty:last-child { border-bottom: 0; }
.audit-time {
  color: var(--muted);
  display: block;
  font-size: 0.82rem;
  font-weight: 700;
  margin-bottom: 8px;
}
.audit-summary {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.audit-actor { margin: 6px 0 10px; }
.btn-small {
  font-size: 0.9rem;
  padding: 6px 9px;
}
.audit-diff {
  background: #f9fbff;
  border-left: 3px solid var(--accent);
  margin-top: 10px;
  padding: 10px 12px;
}
.audit-diff-row {
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  font-size: 0.9rem;
  line-height: 1.55;
  overflow-wrap: anywhere;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@media (max-width: 700px) {
  .card-grid, .scope-grid { grid-template-columns: 1fr; }
  .topbar, .page-header { align-items: flex-start; flex-direction: column; }
  .toolbar .muted { margin-left: 0; width: 100%; }
  input[type="search"], select, .wide-select { width: 100%; }
}
