:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --border: #e2e5ea;
  --text: #1b1f24;
  --muted: #687080;
  --track: #e9ecf1;
  --accent: #2f6fde;
  --ok: #1d8f4e;
  --warn: #b26a00;
  --bad: #cc3550;
  --idle: #8a93a1;
  --ok-soft: #e6f4ec;
  --warn-soft: #fbf0dc;
  --bad-soft: #fbe7eb;
  --idle-soft: #eef0f3;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --radius: 10px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a20;
    --border: #272c35;
    --text: #e5e8ec;
    --muted: #98a0ac;
    --track: #252a33;
    --accent: #5b8ff0;
    --ok: #42c07a;
    --warn: #e3a440;
    --bad: #f0687f;
    --idle: #8a93a1;
    --ok-soft: #15291e;
    --warn-soft: #2e2413;
    --bad-soft: #321820;
    --idle-soft: #1f232a;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 0.9375rem/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

main, .top, .footer {
  max-width: 1920px;
  margin: 0 auto;
  padding: 0 16px;
}

.top {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--bg);
  padding-top: max(12px, env(safe-area-inset-top));
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}

.top-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; }
h1 { font-size: 1.25rem; margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 0.8125rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 28px 0 10px; }
.count { color: var(--text); margin-left: 4px; }

button {
  font: inherit;
  font-size: 0.875rem;
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
}
button.ghost { background: transparent; color: var(--muted); border-color: var(--border); }
button:disabled { opacity: 0.6; cursor: default; }

#refresh {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-width: 9.5em;
  transition: color 0.2s, border-color 0.2s, background-color 0.2s;
}
#refresh:active { transform: scale(0.97); }
#refresh .spin {
  display: none;
  width: 12px;
  height: 12px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
#refresh.busy { color: var(--accent); border-color: var(--accent); cursor: progress; }
#refresh.busy .spin { display: inline-block; }
#refresh.ok { color: var(--ok); border-color: var(--ok); background: var(--ok-soft); }
#refresh.warn { color: var(--warn); border-color: var(--warn); background: var(--warn-soft); }
#refresh.bad { color: var(--bad); border-color: var(--bad); background: var(--bad-soft); }
@keyframes spin { to { transform: rotate(360deg); } }

/* Indeterminate bar along the bottom of the sticky header while refreshing. */
.topbar { position: absolute; left: 0; right: 0; bottom: -1px; height: 2px; overflow: hidden; opacity: 0; transition: opacity 0.3s; }
.topbar.active { opacity: 1; }
.topbar::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  width: 35%;
  background: var(--accent);
  animation: slide 1.1s ease-in-out infinite;
}
@keyframes slide { from { left: -35%; } to { left: 100%; } }

.status.flash .status-text { animation: flash-text 1.6s ease-out; }
@keyframes flash-text { 0%, 35% { color: var(--ok); } 100% { color: var(--muted); } }
.card.flash { animation: flash-card 1.6s ease-out; }
@keyframes flash-card { 0%, 30% { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); } 100% { border-color: var(--border); box-shadow: 0 0 0 0 transparent; } }

@media (prefers-reduced-motion: reduce) {
  .topbar::before, #refresh .spin { animation-duration: 3s; }
  .card.flash, .status.flash .status-text { animation: none; }
}

.status { display: flex; align-items: center; gap: 8px; margin-top: 6px; font-size: 0.875rem; color: var(--muted); }
.status-text { flex: 1; min-width: 0; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--idle); flex: none; }
.status.ok .dot { background: var(--ok); }
.status.warn .dot { background: var(--warn); }
.status.bad .dot { background: var(--bad); }
.status.wait .dot { background: var(--warn); animation: pulse 1.2s ease-in-out infinite; }
.status.bad .status-text { color: var(--bad); font-weight: 600; }
@keyframes pulse { 50% { opacity: 0.25; } }

.banner {
  margin-top: 14px;
  padding: 10px 12px;
  border-radius: var(--radius);
  background: var(--warn-soft);
  color: var(--text);
  font-size: 0.875rem;
  overflow-wrap: anywhere;
}
.banner.bad { background: var(--bad-soft); }

.summary { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }
.chip {
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 0.8125rem;
  color: var(--muted);
}
.chip b { color: var(--text); font-weight: 600; }
.chip.bad b { color: var(--bad); }

.cards { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 26.25rem), 1fr)); }
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  min-width: 0;
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.name { font-weight: 600; overflow-wrap: anywhere; }
.id { font-family: var(--mono); font-size: 0.75rem; color: var(--muted); white-space: nowrap; }
.meta { font-size: 0.8125rem; color: var(--muted); margin-top: 2px; overflow-wrap: anywhere; }

/* A <progress> restyled from scratch: the element is the track, the pseudo-element
   the fill. The WebKit and Firefox fill rules must stay separate or both are dropped. */
.bar {
  --fill: var(--accent);
  appearance: none;
  display: block;
  width: 100%;
  height: 8px;
  border: 0 solid;
  border-radius: 4px;
  background: var(--track);
  margin: 10px 0 6px;
  overflow: clip;
}
.bar::-webkit-progress-bar { background: transparent; }
.bar::-webkit-progress-value { background: var(--fill); border-radius: 4px; transition: inline-size 0.4s; }
.bar::-moz-progress-bar { background: var(--fill); border-radius: 4px; transition: inline-size 0.4s; }
.bar.late { --fill: var(--warn); }
.bar.time { --fill: var(--idle); }
.bar.time.late { --fill: var(--warn); }
.progress-line { display: flex; justify-content: space-between; gap: 10px; font-size: 0.8125rem; flex-wrap: wrap; }
.progress-line .pct { font-weight: 600; font-variant-numeric: tabular-nums; }
.progress-line .counter { color: var(--muted); font-variant-numeric: tabular-nums; }
.progress-line .eta { font-variant-numeric: tabular-nums; }

.badges { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.badge {
  font-size: 0.75rem;
  padding: 2px 8px;
  border-radius: 6px;
  background: var(--idle-soft);
  color: var(--muted);
}
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.ok { background: var(--ok-soft); color: var(--ok); }

.metrics { display: flex; flex-wrap: wrap; gap: 4px 12px; margin-top: 8px; font-size: 0.78125rem; color: var(--muted); }
.metrics b { color: var(--text); font-weight: 500; font-family: var(--mono); font-size: 0.75rem; }

.lastline {
  margin-top: 8px;
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--muted);
  overflow-wrap: anywhere;
}

details { margin-top: 8px; }
summary { font-size: 0.78125rem; color: var(--muted); cursor: pointer; user-select: none; }
pre {
  margin: 6px 0 0;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  font: 0.71875rem/1.5 var(--mono);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  max-height: 240px;
  overflow-y: auto;
}

/* On wide screens rows flow into columns. Each row draws its own top and left
   hairline, pulled 1px outward so the box clips the ones along its edges. */
.rows {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 26.25rem), 1fr));
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: clip;
}
.row { padding: 10px 14px; border: solid var(--border); border-width: 1px 0 0 1px; margin: -1px 0 0 -1px; min-width: 0; }
.row-head { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.row-head .id { margin-left: auto; }
.row .meta { margin-top: 3px; }

.state {
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.03em;
  padding: 1px 7px;
  border-radius: 5px;
  background: var(--idle-soft);
  color: var(--muted);
}
.state.ok { background: var(--ok-soft); color: var(--ok); }
.state.bad { background: var(--bad-soft); color: var(--bad); }
.state.warn { background: var(--warn-soft); color: var(--warn); }

.empty { padding: 14px; color: var(--muted); font-size: 0.875rem; }

/* OpenAI spend: one full-width card per project, sized by its own width. Its
   regions sit on two columns: a narrow card spans both and stacks them, a wide
   one puts the chart beside the totals and keys. The keys row is the 1fr one,
   so a tall chart stretches it rather than pushing the keys down. */
.stack { display: grid; gap: 10px; }
.spend {
  container: spend / inline-size;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  grid-template-rows: auto auto 1fr;
  column-gap: 2rem;
}
.spend > * { grid-column: 1 / -1; }
@container spend (inline-size >= 47.5rem) {
  .spend > .stats, .spend > .spend-keys { grid-column: 1; }
  .spend > .chart { --plot-h: 128px; grid-column: 2; grid-row: 2 / span 2; align-self: start; }
}
.stats { display: flex; flex-wrap: wrap; row-gap: 8px; margin-top: 10px; }
.stat { padding: 0 20px; border-left: 1px solid var(--border); min-width: 0; }
.stat:first-child { padding-left: 0; border-left: 0; }
.stat-label { font-size: 0.78125rem; color: var(--muted); }
.stat-value { font-size: 1.125rem; font-weight: 600; line-height: 1.3; font-variant-numeric: tabular-nums; }
.spend-keys { min-width: 0; }

/* The top margin is room for the tooltip, which sits above the plot. */
.chart { --plot-h: 96px; display: grid; grid-template-columns: auto 1fr; column-gap: 8px; margin-top: 2.25rem; }
.yaxis { display: flex; flex-direction: column; justify-content: space-between; height: var(--plot-h); font-size: 0.6875rem; color: var(--muted); text-align: right; line-height: 1; }
.plot {
  position: relative;
  height: var(--plot-h);
  display: flex;
  align-items: flex-end;
  gap: 2px;
  border-top: 1px solid var(--track);
  border-bottom: 1px solid var(--border);
}
.col { flex: 1; height: 100%; display: flex; align-items: flex-end; justify-content: center; outline: none; cursor: default; }
.col-bar { width: 100%; max-width: 24px; background: var(--accent); border-radius: 4px 4px 0 0; transition: opacity 0.15s; }
.col:hover .col-bar, .col:focus-visible .col-bar { opacity: 0.75; }
.col:focus-visible { box-shadow: inset 0 0 0 2px var(--accent); border-radius: 4px; }
.xaxis { grid-column: 2; display: flex; justify-content: space-between; font-size: 0.6875rem; color: var(--muted); margin-top: 4px; }
.tip {
  position: absolute;
  top: -6px;
  transform: translate(-50%, -100%);
  padding: 4px 8px;
  border-radius: 6px;
  background: var(--text);
  color: var(--surface);
  font-size: 0.75rem;
  white-space: nowrap;
  pointer-events: none;
  z-index: 1;
}
.tip b { font-weight: 600; }

table.keys { width: 100%; border-collapse: collapse; margin-top: 12px; font-size: 0.8125rem; }
table.keys th { text-align: right; font-weight: 500; font-size: 0.75rem; color: var(--muted); padding: 4px 0 4px 8px; border-bottom: 1px solid var(--border); }
table.keys th:first-child { text-align: left; padding-left: 0; }
table.keys td { text-align: right; padding: 6px 0 6px 8px; border-bottom: 1px solid var(--border); font-variant-numeric: tabular-nums; white-space: nowrap; vertical-align: top; }
table.keys td:first-child { text-align: left; padding-left: 0; white-space: normal; overflow-wrap: anywhere; }
table.keys tr:last-child td { border-bottom: 0; }
table.keys tr.muted td { color: var(--muted); }
table.keys.daily { margin-top: 6px; }
.key-name { font-weight: 500; }
.key-sub { font-size: 0.71875rem; color: var(--muted); }
.note { margin-top: 10px; font-size: 0.75rem; }
.more { grid-column: 1 / -1; display: block; width: 100%; border: 0; border-top: 1px solid var(--border); border-radius: 0 0 var(--radius) var(--radius); background: transparent; color: var(--accent); padding: 10px; }

.footer { color: var(--muted); font-size: 0.75rem; padding-top: 28px; padding-bottom: max(24px, env(safe-area-inset-bottom)); }
.footer a { color: inherit; }

.login-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login {
  width: 100%;
  max-width: 340px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px 20px;
  display: grid;
  gap: 10px;
}
.login h1 { margin-bottom: 8px; }
.login label { font-size: 0.8125rem; color: var(--muted); }
.login input {
  font: inherit;
  font-size: 1rem;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}
.login input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.login button { padding: 10px; font-size: 0.9375rem; }
.login-error { margin: 0; color: var(--bad); font-size: 0.875rem; }
.login-note { margin: 4px 0 0; color: var(--muted); font-size: 0.78125rem; text-align: center; }
