:root {
  color-scheme: light;
  --page: #f7f7f5;
  --surface: #fcfcfb;
  --card: #ffffff;
  --ink: #0f1115;
  --ink-2: #4b4e57;
  --muted: #7e818a;
  --grid: #e4e5e0;
  --axis: #c5c6be;
  --line: rgba(15, 17, 21, 0.09);
  --accent: #2563eb;
  --accent-ink: #1d4ed8;
  --accent-soft: #eaf1ff;
  --hover: rgba(15, 17, 21, 0.035);
  --good: #047857;
  --good-mark: #10b981;
  --warn-mark: #f59e0b;
  --crit: #b91c1c;
  --crit-mark: #dc2626;
  --crit-soft: #fef2f2;
  --warn-soft: #fffbeb;
  --tok-uncached: #2563eb;
  --tok-output: #ea580c;
  --tok-cached: #14a37f;
  --s1: #2563eb; --s2: #ea580c; --s3: #14a37f; --s4: #d97706; --s5: #db2777; --s-other: #a8a7a0;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(15, 17, 21, 0.04), 0 4px 16px rgba(15, 17, 21, 0.035);
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #0c0d10;
    --surface: #17181d;
    --card: #131418;
    --ink: #f3f4f6;
    --ink-2: #b6b9c2;
    --muted: #828691;
    --grid: #25272e;
    --axis: #353842;
    --line: rgba(255, 255, 255, 0.09);
    --accent: #4f8ff7;
    --accent-ink: #8cb8ff;
    --accent-soft: #15233d;
    --hover: rgba(255, 255, 255, 0.045);
    --good: #34d399;
    --crit: #f87171;
    --crit-soft: #2c1517;
    --warn-soft: #2b2110;
    --tok-uncached: #4f8ff7;
    --tok-output: #f37944;
    --tok-cached: #26ba92;
    --s1: #4f8ff7; --s2: #f37944; --s3: #26ba92; --s4: #e5a01d; --s5: #e65d94; --s-other: #5e616b;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 14px/1.55 system-ui, -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

a { color: var(--accent-ink); text-decoration: none; }
a:hover { text-decoration: underline; }
code, .mono {
  font: 12.5px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
code { background: var(--hover); padding: 0.1em 0.35em; border-radius: 5px; }
h1 { font-size: 22px; margin: 0; letter-spacing: -0.015em; }
h2 { font-size: 15px; margin: 0 0 12px; font-weight: 620; }
h3 { font-size: 13px; margin: 0 0 8px; color: var(--ink-2); }
p { margin: 0 0 10px; }
.muted { color: var(--muted); }
.ink2 { color: var(--ink-2); }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.small { font-size: 12.5px; }
[hidden] { display: none !important; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 18px;
  padding: 0 24px; min-height: 56px;
  background: var(--card); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 15px; letter-spacing: -0.02em; white-space: nowrap; display: flex; align-items: center; gap: 6px; }
.brand span { color: var(--muted); font-weight: 500; font-size: 13px; }
.nav { display: flex; gap: 3px; overflow-x: auto; flex: 1; scrollbar-width: none; }
.nav::-webkit-scrollbar { display: none; }
.nav a {
  padding: 7px 12px; border-radius: 8px; color: var(--ink-2); white-space: nowrap;
  font-size: 13.5px; transition: background 0.15s, color 0.15s;
}
.nav a:hover { background: var(--hover); text-decoration: none; color: var(--ink); }
.nav a.active { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
.who { display: flex; align-items: center; gap: 10px; white-space: nowrap; font-size: 13px; }
.who-name { max-width: 140px; overflow: hidden; text-overflow: ellipsis; }

main { max-width: 1280px; margin: 0 auto; padding: 24px 24px 72px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 14px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head .sub { color: var(--muted); margin-top: 4px; font-size: 13.5px; }

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px 20px; margin-bottom: 18px; box-shadow: var(--shadow);
}
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.card-head h2 { margin: 0; }
.card-head-tools { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); gap: 18px; margin-bottom: 18px; }
.grid2 > .card { margin-bottom: 0; }
.stack > * + * { margin-top: 14px; }

/* stat tiles */
.tiles { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-bottom: 18px; }
.tile {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 15px 17px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; justify-content: space-between; min-width: 0;
}
.tile .label { color: var(--ink-2); font-size: 12.5px; font-weight: 500; }
.tile .value { font-size: 25px; font-weight: 660; margin-top: 4px; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; line-height: 1.2; }
.tile .foot { color: var(--muted); font-size: 12px; margin-top: 6px; line-height: 1.45; }
.tile.hero .value { font-size: 29px; }

/* token split bar & breakdown */
.split-bar {
  display: flex; height: 5px; border-radius: 999px; overflow: hidden;
  background: var(--hover); margin-top: 8px; gap: 1px;
}
.split-bar .seg-uncached { background: var(--tok-uncached); min-width: 2px; }
.split-bar .seg-cached { background: var(--tok-cached); min-width: 2px; }
.tile-split {
  display: flex; flex-wrap: wrap; gap: 4px 10px;
  font-size: 12px; color: var(--ink-2); margin-top: 6px;
  font-variant-numeric: tabular-nums;
}
.tile-split .dot {
  display: inline-block; width: 7px; height: 7px; border-radius: 50%;
  margin-right: 4px; vertical-align: 0;
}
.dot.uncached { background: var(--tok-uncached); }
.dot.cached { background: var(--tok-cached); }
.dot.output { background: var(--tok-output); }

.metric-stack { display: flex; flex-direction: column; align-items: flex-end; line-height: 1.35; }
.metric-stack .main-v { font-weight: 560; font-variant-numeric: tabular-nums; }
.metric-stack .sub-v { font-size: 11.5px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* filters */
.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--surface); }
.seg button { border: 0; border-radius: 0; background: transparent; padding: 6px 12px; font-size: 13px; color: var(--ink-2); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
.seg.compact button { padding: 4px 10px; font-size: 12.5px; }

/* controls */
button, .btn {
  font: inherit; cursor: pointer; color: var(--ink);
  background: var(--card); border: 1px solid var(--line); border-radius: 8px;
  padding: 6px 12px; line-height: 1.4; white-space: nowrap;
  display: inline-flex; align-items: center; justify-content: center; gap: 5px;
}
button:hover, .btn:hover { background: var(--hover); text-decoration: none; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 500; }
button.primary:hover { filter: brightness(1.06); background: var(--accent); }
button.danger { color: var(--crit); }
button.danger.solid { background: var(--crit-mark); border-color: var(--crit-mark); color: #fff; }
button.link { border: 0; background: none; padding: 2px 4px; color: var(--accent-ink); }
button.link:hover { text-decoration: underline; background: none; }
input, select, textarea {
  font: inherit; color: var(--ink); background: var(--card);
  border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; min-width: 0;
}
input:focus, select:focus, textarea:focus, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
textarea { width: 100%; min-height: 64px; resize: vertical; }
label.field { display: grid; gap: 4px; font-size: 13px; color: var(--ink-2); }
label.field input, label.field select, label.field textarea { width: 100%; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 14px; }
.form-grid .full { grid-column: 1 / -1; }
.checks { display: flex; flex-wrap: wrap; gap: 6px 16px; }
.checks label { display: inline-flex; gap: 6px; align-items: center; color: var(--ink); }
.hint { color: var(--muted); font-size: 12px; line-height: 1.5; }

/* tables */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -2px; padding: 0 2px; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { color: var(--muted); font-weight: 600; font-size: 12px; white-space: nowrap; position: sticky; top: 0; background: var(--card); }
tbody tr:hover td { background: var(--hover); }
tr.clickable { cursor: pointer; }
td.empty { text-align: center; color: var(--muted); padding: 28px; }
.more { display: flex; justify-content: center; margin-top: 12px; }

/* badges */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 2px 9px; border-radius: 999px; font-size: 12px; white-space: nowrap;
  border: 1px solid var(--line); color: var(--ink-2); background: var(--surface);
}
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }
.badge.active::before, .badge.ok::before { background: var(--good-mark); }
.badge.disabled::before, .badge.warn::before { background: var(--warn-mark); }
.badge.revoked::before, .badge.err::before { background: var(--crit-mark); }
.badge.plain::before { display: none; }
.tag { display: inline-block; padding: 1px 7px; border-radius: 6px; background: var(--hover); font-size: 12px; margin: 1px 4px 1px 0; color: var(--ink-2); }
.err-text { color: var(--crit); }
.good-text { color: var(--good); }

/* meters */
.meter { height: 8px; border-radius: 4px; background: var(--accent-soft); overflow: hidden; margin: 6px 0 2px; }
.meter > div { height: 100%; background: var(--accent); border-radius: 4px; }
.meter.warn > div { background: var(--warn-mark); }
.meter.crit > div { background: var(--crit-mark); }

/* charts */
.chart { position: relative; width: 100%; touch-action: pan-y; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart .grid-line { stroke: var(--grid); stroke-width: 1; }
.chart .base-line { stroke: var(--axis); stroke-width: 1; }
.chart text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .col.dim { opacity: 0.42; }
.chart .hit { fill: transparent; cursor: pointer; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin: 10px 0 0; font-size: 12.5px; color: var(--ink-2); }
.legend .sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.tooltip {
  position: fixed; z-index: 50; pointer-events: none;
  background: var(--card); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.16); padding: 9px 12px; font-size: 12.5px;
  min-width: 160px; max-width: min(300px, calc(100vw - 20px));
}
.tooltip .t-head { color: var(--ink-2); font-weight: 600; margin-bottom: 5px; }
.tooltip .t-foot { color: var(--ink); font-weight: 600; margin-top: 6px; padding-top: 5px; border-top: 1px solid var(--line); }
.tooltip .t-row { display: flex; align-items: center; gap: 8px; line-height: 1.55; }
.tooltip .t-row .key { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.tooltip .t-row .v { font-weight: 650; font-variant-numeric: tabular-nums; }
.tooltip .t-row .n { color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* modal */
.backdrop {
  position: fixed; inset: 0; z-index: 40; background: rgba(0, 0, 0, 0.42);
  display: flex; align-items: flex-start; justify-content: center; padding: 7vh 16px; overflow-y: auto;
}
.modal {
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  width: min(640px, 100%); padding: 20px 22px; box-shadow: 0 16px 48px rgba(0, 0, 0, 0.24);
}
.modal h2 { font-size: 17px; margin-bottom: 14px; }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; flex-wrap: wrap; }
.modal .error { margin-top: 12px; }
.secret {
  display: flex; gap: 8px; align-items: center; padding: 10px 12px; border-radius: 8px;
  background: var(--warn-soft); border: 1px solid var(--line); word-break: break-all;
}
.secret code { background: none; padding: 0; font-size: 13px; flex: 1; }
pre.snippet {
  background: var(--hover); border-radius: 8px; padding: 10px 12px; overflow-x: auto; margin: 8px 0 0;
  font: 12.5px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

/* notices */
.notice { padding: 11px 14px; border-radius: 10px; margin-bottom: 14px; border: 1px solid var(--line); font-size: 13px; line-height: 1.6; background: var(--card); }
.notice.err { background: var(--crit-soft); color: var(--crit); }
.notice.warn { background: var(--warn-soft); }
.toast {
  position: fixed; right: 20px; bottom: 20px; z-index: 60;
  background: var(--ink); color: var(--page); padding: 10px 15px; border-radius: 9px; font-size: 13px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.22);
}

/* login */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login { width: min(410px, 100%); padding: 24px 22px; }
.login h1 { margin-bottom: 4px; }
.login .sub { color: var(--muted); margin-bottom: 18px; }
.login form { display: grid; gap: 12px; }
.login .seg { width: 100%; margin-bottom: 16px; }
.login .seg button { flex: 1; }

.kv { display: grid; grid-template-columns: max-content 1fr; gap: 8px 18px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }
.head-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* mobile layout */
@media (max-width: 768px) {
  .topbar {
    flex-wrap: wrap;
    padding: 0;
    gap: 0;
    min-height: 0;
  }
  .topbar .brand {
    padding: 11px 14px;
    flex: 1 1 auto;
  }
  .topbar .brand span { display: none; }
  .topbar .who {
    padding: 10px 14px;
    gap: 8px;
    font-size: 12.5px;
  }
  .topbar .who .badge { display: none; }
  .topbar .who-name { max-width: 92px; }
  .topbar .nav {
    order: 3;
    flex: 1 0 100%;
    padding: 6px 10px;
    gap: 4px;
    border-top: 1px solid var(--line);
    background: var(--surface);
  }
  .topbar .nav a {
    padding: 7px 11px;
    font-size: 13px;
  }

  main { padding: 16px 12px 56px; }
  h1 { font-size: 20px; }
  .page-head { gap: 10px; margin-bottom: 14px; align-items: stretch; }
  .page-head > div:first-child { flex: 1 1 100%; }
  .page-head .seg { width: 100%; }
  .page-head .seg button { flex: 1; padding: 8px 10px; }
  .head-actions { width: 100%; }
  .head-actions .seg { width: 100%; }
  .head-actions > button { flex: 1 1 auto; min-height: 36px; }

  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-bottom: 14px; }
  .tile { padding: 12px 13px; }
  .tile.hero { grid-column: 1 / -1; }
  .tiles > .tile:last-child:nth-child(even) { grid-column: 1 / -1; }
  .tile.hero .value { font-size: 28px; }
  .tile .value { font-size: 20px; }
  .tile-split { gap: 3px 8px; font-size: 11.5px; }

  .card { padding: 14px 14px; margin-bottom: 14px; border-radius: 10px; }
  .card-head { gap: 8px; margin-bottom: 10px; }
  .card-head-tools { width: 100%; justify-content: space-between; }

  .grid2 { grid-template-columns: 1fr; gap: 14px; margin-bottom: 14px; }
  .form-grid { grid-template-columns: 1fr; }

  .filters { gap: 8px; }
  .filters input, .filters select { flex: 1 1 140px; min-height: 38px; font-size: 14px; }
  .filters button, .filters .btn { min-height: 38px; }

  table { font-size: 12.5px; }
  th, td { padding: 8px 8px; }

  /* Stacked card rows on mobile for primary usage tables */
  .table-wrap.cards-mobile { overflow-x: visible; margin: 0; padding: 0; }
  .table-wrap.cards-mobile table,
  .table-wrap.cards-mobile thead,
  .table-wrap.cards-mobile tbody,
  .table-wrap.cards-mobile tr,
  .table-wrap.cards-mobile td { display: block; width: 100%; }
  .table-wrap.cards-mobile thead { display: none; }
  .table-wrap.cards-mobile tbody tr {
    padding: 11px 0;
    border-bottom: 1px solid var(--line);
  }
  .table-wrap.cards-mobile tbody tr:first-child { padding-top: 2px; }
  .table-wrap.cards-mobile tbody tr:last-child { border-bottom: 0; padding-bottom: 2px; }
  .table-wrap.cards-mobile tbody tr:hover td { background: transparent; }
  .table-wrap.cards-mobile td {
    border: 0;
    padding: 3px 0;
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
    text-align: right;
    white-space: normal;
  }
  .table-wrap.cards-mobile td::before {
    content: attr(data-label);
    font-size: 12px;
    color: var(--muted);
    font-weight: 500;
    text-align: left;
    flex: none;
  }
  .table-wrap.cards-mobile td:first-child {
    display: block;
    text-align: left;
    font-weight: 600;
    padding-bottom: 5px;
    word-break: break-all;
  }
  .table-wrap.cards-mobile td:first-child::before { display: none; }
  .table-wrap.cards-mobile td.empty {
    display: block;
    text-align: center;
    padding: 20px 0;
  }
  .table-wrap.cards-mobile td.empty::before { display: none; }

  .backdrop { padding: 16px 10px; }
  .modal { padding: 16px 16px; border-radius: 12px; }
  .modal .actions button { flex: 1; min-height: 40px; }

  .kv { grid-template-columns: 1fr; gap: 3px; }
  .kv dt { margin-top: 8px; font-size: 12px; }
  .kv dt:first-child { margin-top: 0; }

  .toast { left: 12px; right: 12px; bottom: 14px; text-align: center; }
}

@media (max-width: 420px) {
  .tiles { gap: 8px; }
  .tile { padding: 11px 11px; }
  .tile .value { font-size: 18px; }
  .tile.hero .value { font-size: 25px; }
  .seg.compact button { padding: 5px 8px; font-size: 12px; }
}
