:root {
  color-scheme: light dark;
  --background: #f5f6f8;
  --surface: #ffffff;
  --surface-muted: #f0f2f5;
  --surface-hover: #f7f8fb;
  --border: #e3e6eb;
  --border-strong: #cfd4dc;
  --text: #1b1f27;
  --text-muted: #626b7a;
  --text-faint: #8c94a1;
  --accent: #5b4fd6;
  --accent-soft: #ecebfb;
  --positive: #1a7f4b;
  --negative: #c2372e;
  --warning: #a35a00;
  --danger-soft: #fdecea;
  --warning-soft: #fdf3e2;
  --positive-soft: #e6f4ec;
  --bar: #c7c2f3;
  --bar-highlight: #5b4fd6;
  --bar-partial: #e4e1fa;
  --kind-marketplace: #5b4fd6;
  --kind-shop: #0f7b8a;
  --kind-integrator: #8a5a0f;
  --kind-other: #6b7280;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.05);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --background: #111318;
    --surface: #181b22;
    --surface-muted: #1f232c;
    --surface-hover: #1d2129;
    --border: #2a2f3a;
    --border-strong: #3a404d;
    --text: #e8eaef;
    --text-muted: #a0a7b4;
    --text-faint: #737b88;
    --accent: #9d94ff;
    --accent-soft: #2a2747;
    --positive: #4cc38a;
    --negative: #ff7b72;
    --warning: #f0a849;
    --danger-soft: #3a1f1f;
    --warning-soft: #3a2c17;
    --positive-soft: #183326;
    --bar: #4b4590;
    --bar-highlight: #9d94ff;
    --bar-partial: #2e2b52;
    --kind-marketplace: #9d94ff;
    --kind-shop: #4fc1cf;
    --kind-integrator: #e0b064;
    --kind-other: #9ca3af;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--background);
  color: var(--text);
  font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

.number { font-variant-numeric: tabular-nums; }
.muted { color: var(--text-muted); }
.faint { color: var(--text-faint); }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 10px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 15px; }
.brand-mark {
  width: 18px; height: 18px; border-radius: 6px;
  background: linear-gradient(135deg, #5b4fd6, #2fb5c9);
}
.refresh { display: flex; align-items: center; gap: 12px; }
.refresh-status { color: var(--text-muted); font-size: 13px; }

.button {
  font: inherit; font-weight: 600; font-size: 13px;
  padding: 7px 14px; border-radius: 8px; cursor: pointer;
  color: #fff; background: var(--accent); border: 1px solid transparent;
}
.button:hover:not(:disabled) { filter: brightness(1.08); }
.button:disabled { cursor: default; opacity: 0.5; }

.page { padding: 20px 24px 48px; max-width: 1680px; margin: 0 auto; }

/* KPI tiles */
.kpis {
  display: grid; gap: 12px; margin-bottom: 18px;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
}
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 16px; box-shadow: var(--shadow);
}
.kpi-label { color: var(--text-muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.kpi-value { font-size: 26px; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; }
.kpi-detail { font-size: 12.5px; color: var(--text-muted); margin-top: 2px; }

/* Toolbar */
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; margin-bottom: 12px; }
.search { position: relative; flex: 1 1 320px; max-width: 460px; }
.search input {
  width: 100%; font: inherit; padding: 8px 36px 8px 12px; border-radius: 8px;
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text);
}
.search input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.search-hint {
  position: absolute; right: 9px; top: 50%; transform: translateY(-50%);
  font: 11px/1 var(--font); color: var(--text-faint);
  border: 1px solid var(--border-strong); border-radius: 4px; padding: 3px 6px;
}
.filters { display: flex; flex-wrap: wrap; gap: 6px; }
.filter {
  font: inherit; font-size: 13px; font-weight: 550; cursor: pointer;
  padding: 6px 11px; border-radius: 999px;
  color: var(--text-muted); background: var(--surface); border: 1px solid var(--border);
}
.filter:hover { border-color: var(--border-strong); color: var(--text); }
.filter[aria-pressed="true"] { color: var(--accent); background: var(--accent-soft); border-color: transparent; }
.filter-count { margin-left: 6px; font-variant-numeric: tabular-nums; opacity: 0.8; }
.filter[data-tone="danger"] .filter-count { color: var(--negative); opacity: 1; }
.filter[data-tone="warning"] .filter-count { color: var(--warning); opacity: 1; }

.notice {
  margin-bottom: 12px; padding: 10px 14px; border-radius: var(--radius);
  background: var(--warning-soft); color: var(--warning); font-size: 13px;
}

/* Table */
.table-frame {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: auto; max-height: calc(100vh - 220px);
}
.instances { width: 100%; border-collapse: separate; border-spacing: 0; }
.instances th {
  position: sticky; top: 0; z-index: 5;
  background: var(--surface-muted); color: var(--text-muted);
  font-size: 12px; font-weight: 600; text-align: left; white-space: nowrap;
  padding: 0; border-bottom: 1px solid var(--border);
}
.instances th.align-right { text-align: right; }
.sort-button {
  all: unset; box-sizing: border-box; display: block; width: 100%; cursor: pointer;
  padding: 9px 12px;
}
.sort-button:hover { color: var(--text); }
.sort-button:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.sort-indicator { display: inline-block; width: 1em; margin-left: 2px; color: var(--accent); }
.instances td {
  padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: middle;
}
.instances td.align-right { text-align: right; }
.instance-row { cursor: pointer; }
.instance-row:hover td { background: var(--surface-hover); }
.instance-row:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
/* The client column stays in view when the table scrolls sideways. */
.instances th:first-child, .instances td:first-child { position: sticky; left: 0; z-index: 2; background: var(--surface); }
.instances th:first-child { z-index: 6; background: var(--surface-muted); }
.instance-row td:first-child { border-left: 3px solid transparent; }
.instance-row[data-attention="danger"] td:first-child { border-left-color: var(--negative); }
.instance-row[data-attention="warning"] td:first-child { border-left-color: var(--warning); }
.instance-row[aria-expanded="true"] td { background: var(--surface-hover); border-bottom-color: transparent; }
.instance-row:hover td:first-child, .instance-row[aria-expanded="true"] td:first-child { background: var(--surface-hover); }
.details-row td:first-child { position: static; background: var(--surface-hover); }

.client { display: flex; align-items: center; gap: 10px; min-width: 150px; }
.client-name { font-weight: 650; }
.client-meta { font-size: 12px; color: var(--text-faint); }
.chevron { color: var(--text-faint); transition: transform 0.15s; flex: none; }
.instance-row[aria-expanded="true"] .chevron { transform: rotate(90deg); }

.orders-value { font-size: 15px; font-weight: 650; font-variant-numeric: tabular-nums; }
.change { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.change.positive { color: var(--positive); }
.change.negative { color: var(--negative); }

.sparkline { display: block; }
.sparkline rect { fill: var(--bar); }
.sparkline rect.highlight { fill: var(--bar-highlight); }
.sparkline rect.partial { fill: var(--bar-partial); }
.trend { display: flex; align-items: center; gap: 10px; }
.trend-total { font-size: 12px; color: var(--text-muted); font-variant-numeric: tabular-nums; white-space: nowrap; }

.chips { display: flex; flex-wrap: nowrap; gap: 4px; }
.chip {
  display: inline-flex; align-items: center; gap: 5px; white-space: nowrap;
  font-size: 12px; padding: 2px 8px; border-radius: 999px;
  background: var(--surface-muted); color: var(--text);
}
.chip-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--kind-other); flex: none; }
.chip-dot[data-kind="marketplace"] { background: var(--kind-marketplace); }
.chip-dot[data-kind="shop"] { background: var(--kind-shop); }
.chip-dot[data-kind="integrator"] { background: var(--kind-integrator); }
.chip-count { color: var(--text-muted); font-variant-numeric: tabular-nums; }
.chip-more { color: var(--text-muted); background: transparent; border: 1px dashed var(--border-strong); }

.badge {
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
  font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 6px;
}
.badge.danger { background: var(--danger-soft); color: var(--negative); }
.badge.warning { background: var(--warning-soft); color: var(--warning); }
.badge.positive { background: var(--positive-soft); color: var(--positive); }

.status { display: flex; align-items: center; gap: 7px; white-space: nowrap; font-size: 12.5px; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-faint); flex: none; }
.status-dot[data-status="ok"] { background: var(--positive); }
.status-dot[data-status="stale"] { background: var(--warning); }
.status-dot[data-status="failed"] { background: var(--negative); }

/* Expanded details */
.details-row td { background: var(--surface-hover); padding: 4px 16px 18px 40px; }
/* The details row spans the whole (possibly scrolled) table; keep its content in view. */
.details {
  display: grid; gap: 18px; grid-template-columns: minmax(320px, 1.2fr) minmax(260px, 1fr) minmax(240px, 1fr);
  position: sticky; left: 16px; max-width: min(1400px, calc(100vw - 112px));
}
.details h3 { margin: 10px 0 8px; font-size: 12px; font-weight: 650; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); }
.details-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.details-table th, .details-table td { padding: 4px 8px; border-bottom: 1px solid var(--border); text-align: right; font-variant-numeric: tabular-nums; }
.details-table th { color: var(--text-faint); font-weight: 600; font-size: 12px; }
.details-table th:first-child, .details-table td:first-child { text-align: left; }
.details-table tr.partial td { color: var(--text-muted); font-style: italic; }
.details-list { list-style: none; margin: 0; padding: 0; font-size: 13px; }
.details-list li { display: flex; justify-content: space-between; gap: 12px; padding: 5px 0; border-bottom: 1px solid var(--border); }
.details-list .item-name { display: flex; align-items: center; gap: 6px; min-width: 0; }
.details-list .item-meta { color: var(--text-faint); font-size: 12px; white-space: nowrap; text-align: right; }
.details-footer { grid-column: 1 / -1; display: flex; flex-wrap: wrap; align-items: center; gap: 16px; font-size: 12.5px; color: var(--text-muted); }
.details-footer a { color: var(--accent); font-weight: 600; text-decoration: none; }
.details-footer a:hover { text-decoration: underline; }
.error-box { grid-column: 1 / -1; padding: 8px 12px; border-radius: 8px; background: var(--danger-soft); color: var(--negative); font-size: 13px; }
.empty-hint { color: var(--text-faint); font-size: 13px; padding: 5px 0; }

.empty { margin: 0; padding: 32px; text-align: center; color: var(--text-muted); }

@media (max-width: 1100px) {
  .details { grid-template-columns: 1fr; }
  .details-row td { padding-left: 16px; }
}
@media (max-width: 720px) {
  .topbar, .page { padding-left: 16px; padding-right: 16px; }
  .refresh-status { display: none; }
  .table-frame { max-height: none; }
}

/* Usage score */
.usage { display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.usage-pill {
  min-width: 36px; padding: 2px 8px; border-radius: 6px; text-align: center;
  font-weight: 700; font-variant-numeric: tabular-nums;
  background: var(--surface-muted); color: var(--text-faint);
}
.usage-pill[data-level="ok"] { background: var(--positive-soft); color: var(--positive); }
.usage-pill[data-level="warning"] { background: var(--warning-soft); color: var(--warning); }
.usage-pill[data-level="risk"] { background: var(--danger-soft); color: var(--negative); }
.usage-trend { display: block; }
.usage-trend polyline { fill: none; stroke: var(--text-faint); stroke-width: 1.6; stroke-linejoin: round; stroke-linecap: round; }
.usage-trend[data-level="ok"] polyline { stroke: var(--positive); }
.usage-trend[data-level="warning"] polyline { stroke: var(--warning); }
.usage-trend[data-level="risk"] polyline { stroke: var(--negative); }
.usage-change { font-size: 12px; font-weight: 650; font-variant-numeric: tabular-nums; }
.usage-change.up { color: var(--positive); }
.usage-change.down { color: var(--negative); }

.usage-details { grid-column: 1 / -1; }
.usage-summary { font-size: 13px; margin-bottom: 6px; color: var(--text-muted); }
.usage-summary[data-level="risk"] { color: var(--negative); font-weight: 600; }
.usage-summary[data-level="warning"] { color: var(--warning); font-weight: 600; }
.share { display: inline-flex; align-items: center; gap: 8px; justify-content: flex-end; }
.share-bar { display: inline-block; height: 6px; border-radius: 3px; background: var(--positive); }
.share-bar[data-tone="warning"] { background: var(--warning); }
.share-bar[data-tone="risk"] { background: var(--negative); }
.usage-details .details-table { max-width: 720px; }

/* Read problem on the client cell, automations in the details */
.client-read-problem { color: var(--warning); font-weight: 600; }
.automations-details { grid-column: 1 / -1; }
.automation-list .item-name { gap: 8px; }
.automation-list .badge { font-weight: 600; background: var(--surface-muted); color: var(--text-muted); }
.automation-list .badge.positive { background: var(--positive-soft); color: var(--positive); }
.automation-flow { white-space: normal; max-width: 60%; }

/* Provisional score and signal statuses */
.usage-preliminary {
  display: inline-grid; place-items: center; width: 16px; height: 16px; margin-left: -4px;
  border-radius: 50%; font-size: 11px; font-weight: 800;
  background: var(--warning-soft); color: var(--warning); cursor: help;
}
.usage-preliminary-note { font-size: 12.5px; color: var(--warning); margin-bottom: 6px; }
.signal-status { color: var(--text-faint); }
.signal-status.new { color: var(--positive); }
.signal-status.missing { color: var(--negative); font-weight: 600; }

/* Health: a pulsing dot next to the client */
.health-dot {
  position: relative; width: 10px; height: 10px; border-radius: 50%; flex: none;
  background: var(--text-faint);
}
.health-dot[data-tone="up"] { background: var(--positive); }
.health-dot[data-tone="down"] { background: var(--negative); }
.health-dot[data-tone="up"]::after,
.health-dot[data-tone="down"]::after {
  content: ""; position: absolute; inset: 0; border-radius: 50%;
  background: inherit; animation: health-pulse 2s ease-out infinite;
}
.health-dot[data-tone="down"]::after { animation-duration: 1s; }
@keyframes health-pulse {
  from { transform: scale(1); opacity: 0.6; }
  to { transform: scale(2.6); opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
  .health-dot::after { animation: none !important; }
}
.health-details { grid-column: 1 / -1; }
.health-details .details-list { max-width: 720px; }
.item-meta.health-up { color: var(--positive); }
.item-meta.health-down { color: var(--negative); font-weight: 600; white-space: normal; }
.kpi-detail { overflow-wrap: anywhere; }

/* Login */
.login-body { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; padding: 28px; border-radius: 14px;
  background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 16px;
}
.login-brand { justify-content: center; font-size: 17px; }
.login-form { display: flex; flex-direction: column; gap: 12px; }
.login-form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-muted); font-weight: 600; }
.login-form input {
  font: inherit; font-size: 14px; padding: 9px 11px; border-radius: 8px;
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text);
}
.login-form input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.login-form .button, .login-github { padding: 10px 14px; text-align: center; text-decoration: none; }
.login-error { margin: 0; padding: 9px 12px; border-radius: 8px; background: var(--danger-soft); color: var(--negative); font-size: 13px; }
.login-divider { display: flex; align-items: center; gap: 10px; color: var(--text-faint); font-size: 12px; }
.login-divider::before, .login-divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }
.login-hint { margin: 0; font-size: 12.5px; color: var(--text-muted); text-align: center; }
.login-hint code { font-size: 12px; }

/* Signed-in user in the top bar */
.account { display: flex; align-items: center; gap: 8px; margin: 0; padding-left: 12px; border-left: 1px solid var(--border); }
.account-name { font-size: 13px; color: var(--text-muted); font-weight: 600; }
.button-quiet { background: transparent; color: var(--text-muted); border-color: var(--border-strong); }
.button-quiet:hover:not(:disabled) { color: var(--text); filter: none; }

/* View switch and breakdowns */
.views { display: inline-flex; gap: 2px; padding: 3px; margin-bottom: 12px; border-radius: 10px; background: var(--surface-muted); border: 1px solid var(--border); }
.view {
  font: inherit; font-size: 13px; font-weight: 600; cursor: pointer;
  padding: 6px 14px; border-radius: 7px; border: 0; background: transparent; color: var(--text-muted);
}
.view:hover { color: var(--text); }
.view[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.view-count { margin-left: 6px; color: var(--text-faint); font-variant-numeric: tabular-nums; }
.header-label { display: block; padding: 9px 12px; }
.breakdown-count { display: flex; align-items: center; gap: 12px; min-width: 200px; }
.bar { flex: 1; height: 8px; border-radius: 4px; background: var(--surface-muted); overflow: hidden; max-width: 260px; }
.bar-fill { display: block; height: 100%; border-radius: 4px; background: var(--accent); }
.chip-button { border: 0; font: inherit; font-size: 12px; cursor: pointer; }
.chip-button:hover { background: var(--accent-soft); color: var(--accent); }
.link-button { all: unset; cursor: pointer; font-weight: 650; color: var(--accent); }
.link-button:hover { text-decoration: underline; }
.breakdown-clients { max-width: 900px; }
.breakdown-clients .item-name { gap: 10px; }
.breakdown-clients .item-name .faint { white-space: normal; }
