/* TraeWork2API WebUI — design language adapted from chenyme/grok2api
   (sidebar + oklch semantic tokens + light/dark + quota progress bars),
   implemented in plain CSS/JS so the binary stays self-contained. */

:root {
  --radius: 0.5rem;
  --background: oklch(0.995 0 0);
  --foreground: oklch(0.145 0 0);
  --card: oklch(0.975 0.002 90);
  --card-foreground: oklch(0.145 0 0);
  --popover: oklch(1 0 0);
  --popover-foreground: oklch(0.145 0 0);
  --primary: oklch(0.12 0 0);
  --primary-foreground: oklch(0.985 0 0);
  --secondary: oklch(0.965 0.002 90);
  --secondary-foreground: oklch(0.2 0 0);
  --muted: oklch(0.965 0.002 90);
  --muted-foreground: oklch(0.48 0 0);
  --accent: oklch(0.95 0.003 90);
  --accent-foreground: oklch(0.16 0 0);
  --destructive: oklch(0.58 0.22 27);
  --ok: oklch(0.6 0.13 155);
  --warn: oklch(0.66 0.15 80);
  --border: oklch(0.91 0 0);
  --input: oklch(0.9 0 0);
  --ring: var(--border);
  --sidebar: oklch(0.985 0 0);
  --sidebar-foreground: oklch(0.18 0 0);
  --sidebar-accent: oklch(0.94 0 0);
  --sidebar-accent-foreground: oklch(0.14 0 0);
  --sidebar-border: oklch(0.93 0 0);
  --shadow: 0 1px 2px oklch(0 0 0 / 0.04), 0 1px 3px oklch(0 0 0 / 0.06);
}
.dark {
  --background: oklch(0.13 0 0);
  --foreground: oklch(0.95 0 0);
  --card: oklch(0.18 0 0);
  --card-foreground: oklch(0.95 0 0);
  --popover: oklch(0.17 0 0);
  --popover-foreground: oklch(0.95 0 0);
  --primary: oklch(0.96 0 0);
  --primary-foreground: oklch(0.12 0 0);
  --secondary: oklch(0.21 0 0);
  --secondary-foreground: oklch(0.94 0 0);
  --muted: oklch(0.21 0 0);
  --muted-foreground: oklch(0.66 0 0);
  --accent: oklch(0.24 0 0);
  --accent-foreground: oklch(0.96 0 0);
  --destructive: oklch(0.65 0.2 28);
  --ok: oklch(0.72 0.13 155);
  --warn: oklch(0.8 0.13 80);
  --border: oklch(0.27 0 0);
  --input: oklch(0.3 0 0);
  --ring: var(--border);
  --sidebar: oklch(0.145 0 0);
  --sidebar-foreground: oklch(0.92 0 0);
  --sidebar-accent: oklch(0.22 0 0);
  --sidebar-accent-foreground: oklch(0.96 0 0);
  --sidebar-border: oklch(0.25 0 0);
  --shadow: 0 1px 2px oklch(0 0 0 / 0.2), 0 1px 3px oklch(0 0 0 / 0.3);
}

* { box-sizing: border-box; border-color: var(--border); }

/* The hidden attribute is implemented by the UA stylesheet as display:none,
   which ANY author-level display rule silently overrides — .login-wrap sets
   display:flex and .app sets display:grid, so toggling .hidden in JS did
   nothing at all and the login screen sat on top of the console after a
   successful login. Pin it with !important so the attribute always wins. */
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--background);
  color: var(--foreground);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
        "Helvetica Neue", "PingFang SC", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; text-decoration: none; }
button { font: inherit; color: inherit; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.muted { color: var(--muted-foreground); }

/* app shell */
.app { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--sidebar);
  border-right: 1px solid var(--sidebar-border);
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 18px 16px 14px; }
.brand img { width: 26px; height: 26px; border-radius: 6px; }
.brand .name { font-weight: 600; font-size: 14px; letter-spacing: -0.01em; }
.brand .ver { font-size: 11px; color: var(--muted-foreground); }
.nav { padding: 4px 8px; display: flex; flex-direction: column; gap: 1px; }
.nav-group { padding: 12px 8px 4px; font-size: 11px; font-weight: 500;
             color: var(--muted-foreground); letter-spacing: 0.04em; }
.nav-item {
  display: flex; align-items: center; gap: 9px;
  padding: 7px 9px; border-radius: calc(var(--radius) - 2px);
  font-size: 13px; color: var(--sidebar-foreground);
  cursor: pointer; border: 0; background: transparent; width: 100%; text-align: left;
  transition: background-color 0.12s, color 0.12s;
}
.nav-item:hover { background: var(--sidebar-accent); }
.nav-item.active { background: var(--sidebar-accent); color: var(--sidebar-accent-foreground); font-weight: 500; }
.nav-item svg { width: 15px; height: 15px; flex-shrink: 0; opacity: 0.85; }
.nav-item .count { margin-left: auto; font-size: 11px; color: var(--muted-foreground); }
.sidebar-foot { margin-top: auto; padding: 10px 8px; border-top: 1px solid var(--sidebar-border); }

.main { display: flex; flex-direction: column; min-width: 0; }
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 10px;
  padding: 11px 22px;
  background: color-mix(in oklch, var(--background) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 15px; font-weight: 600; margin: 0; letter-spacing: -0.01em; }
.topbar .sub { font-size: 12px; color: var(--muted-foreground); }
.topbar .spacer { flex: 1; }
.content { padding: 20px 22px 56px; display: flex; flex-direction: column; gap: 16px; }

/* primitives */
.card {
  background: var(--card); color: var(--card-foreground);
  border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card-head { display: flex; align-items: center; gap: 10px; padding: 13px 16px;
             border-bottom: 1px solid var(--border); }
.card-head h2 { font-size: 13px; font-weight: 600; margin: 0; }
.card-head .sub { font-size: 12px; color: var(--muted-foreground); }
.card-head .spacer { flex: 1; }
.card-body { padding: 16px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 32px; padding: 0 12px; border-radius: calc(var(--radius) - 2px);
  font-size: 13px; font-weight: 500; cursor: pointer;
  border: 1px solid transparent; background: var(--primary); color: var(--primary-foreground);
  transition: opacity 0.12s, background-color 0.12s;
}
.btn:hover { opacity: 0.9; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-outline { background: transparent; border-color: var(--input); color: var(--foreground); }
.btn-outline:hover { background: var(--accent); }
.btn-ghost { background: transparent; color: var(--foreground); }
.btn-ghost:hover { background: var(--accent); }
.btn-sm { height: 27px; padding: 0 9px; font-size: 12px; }
.btn-icon { width: 32px; padding: 0; }
.btn svg { width: 14px; height: 14px; }

.input, .select {
  height: 32px; padding: 0 10px; border-radius: calc(var(--radius) - 2px);
  background: var(--background); border: 1px solid var(--input);
  color: var(--foreground); font-size: 13px; font-family: inherit; min-width: 0;
}
.input:focus, .select:focus { outline: none; border-color: var(--ring); }
.input::placeholder { color: var(--muted-foreground); }

.badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 1px 7px; border-radius: 999px; font-size: 11px; font-weight: 500;
  background: var(--secondary); color: var(--secondary-foreground);
  border: 1px solid var(--border); white-space: nowrap;
}
.badge-ok { color: var(--ok); border-color: color-mix(in oklch, var(--ok) 35%, transparent); }
.badge-warn { color: var(--warn); border-color: color-mix(in oklch, var(--warn) 35%, transparent); }
.badge-err { color: var(--destructive); border-color: color-mix(in oklch, var(--destructive) 35%, transparent); }
.badge-cn { color: oklch(0.62 0.13 235); border-color: oklch(0.62 0.13 235 / 0.35); }
.badge-solo { color: oklch(0.62 0.15 300); border-color: oklch(0.62 0.15 300 / 0.35); }

/* metric tiles */
.metrics { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
.metric { background: var(--card); border: 1px solid var(--border);
          border-radius: var(--radius); padding: 13px 15px; box-shadow: var(--shadow); }
.metric .top { display: flex; align-items: center; gap: 7px; color: var(--muted-foreground); }
.metric .top svg { width: 14px; height: 14px; }
.metric .label { font-size: 12px; }
.metric .value { font-size: 23px; font-weight: 600; margin-top: 7px; letter-spacing: -0.02em;
                 font-variant-numeric: tabular-nums; }
.metric .detail { font-size: 11px; color: var(--muted-foreground); margin-top: 3px; }

/* quota bar */
.quota { min-width: 132px; }
.quota .qrow { display: flex; align-items: baseline; justify-content: space-between;
              gap: 8px; font-size: 11px; color: var(--muted-foreground); margin-bottom: 4px; }
.quota .qrow .num { font-variant-numeric: tabular-nums; }
.quota .track { height: 5px; border-radius: 999px; background: var(--muted); overflow: hidden; }
.quota .fill { height: 100%; border-radius: 999px; background: var(--ok); transition: width 0.3s; }
.quota .fill.warn { background: var(--warn); }
.quota .fill.full { background: var(--destructive); }

/* table */
.table-wrap { overflow-x: auto; }
/* auto layout lets a long id widen its column and squeeze the action buttons;
   fixed makes the declared widths authoritative so truncation actually bites. */
table { width: 100%; border-collapse: collapse; font-size: 13px;
        table-layout: fixed; }
th { text-align: left; font-weight: 500; font-size: 11px; color: var(--muted-foreground);
     padding: 9px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
td { padding: 11px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr { transition: background-color 0.1s; }
tbody tr:hover { background: color-mix(in oklch, var(--accent) 45%, transparent); }
.cell-main { font-weight: 500; }
.cell-sub { font-size: 11px; color: var(--muted-foreground); margin-top: 1px; }
/* The sub-line is usually an id (16-digit uid, a host:port); it must not force
   the name column open and wrap the display name above it. */
.cell-sub.trunc-sm { overflow: hidden; text-overflow: ellipsis;
                     white-space: nowrap; display: block; }
.cell-main { min-width: 0; }
.cell-actions { display: flex; gap: 6px; justify-content: flex-end; }

.empty { text-align: center; padding: 30px 16px; color: var(--muted-foreground);
          font-size: 13px; display: flex; flex-direction: column;
          align-items: center; gap: 10px; }
.empty .eicon { width: 26px; height: 26px; opacity: 0.4; }
.empty .etitle { color: var(--foreground); font-weight: 500; font-size: 13px; }
.empty .ehint { font-size: 12px; max-width: 46ch; line-height: 1.6; }

/* code / docs */
.code {
  background: var(--background); border: 1px solid var(--border);
  border-radius: calc(var(--radius) - 2px); padding: 13px 15px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px; line-height: 1.65; overflow-x: auto; white-space: pre;
}
.method { display: inline-block; padding: 1px 6px; border-radius: 4px; font-size: 10px;
          font-weight: 600; font-family: ui-monospace, monospace; margin-right: 8px; }
.method-get { background: oklch(0.65 0.13 200 / 0.16); color: oklch(0.5 0.13 200); }
.method-post { background: oklch(0.65 0.14 150 / 0.16); color: oklch(0.46 0.13 150); }
.dark .method-get { color: oklch(0.78 0.12 200); }
.dark .method-post { color: oklch(0.75 0.13 150); }
/* One endpoint per row: a fixed-width method chip on the left, and the path +
   description stacked on the right. The chip's width is reserved by the grid so
   the paths all align — no negative margins. */
.doc-row { display: grid; grid-template-columns: 3.1rem 1fr; gap: 0 10px;
           align-items: start; padding: 8px 0;
           border-bottom: 1px solid var(--border); }
.doc-row:last-child { border-bottom: 0; }
.doc-row .method { margin-right: 0; justify-self: start; margin-top: 1px; }
.doc-path { min-width: 0; }
.doc-path code { font-size: 12px; color: var(--muted-foreground);
                 word-break: break-all; }
.doc-desc { font-size: 11px; color: var(--muted-foreground); margin-top: 2px; }

/* misc */
.row { display: flex; align-items: center; gap: 9px; flex-wrap: wrap; }
.grid-2 { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.stack { display: flex; flex-direction: column; gap: 16px; }
/* Definition rows. The first column is a FIXED fraction (not max-content):
   max-content lets a single long key (MAX_CONCURRENCY) expand the track and
   squeezes the value into a wrapped mess. 9rem fits every key we use and the
   value column keeps the rest. */
.kv { display: grid; grid-template-columns: minmax(7rem, 9rem) 1fr;
      gap: 8px 16px; font-size: 13px; align-items: baseline; }
.kv dt { color: var(--muted-foreground); white-space: nowrap; overflow: hidden;
         text-overflow: ellipsis; }
.kv dd { margin: 0; min-width: 0; word-break: break-word; }
/* A mono value only stays on one line when it genuinely fits; forcing nowrap
   turns a long sentence ("每日 3 点，临到期 24h 或签发超 15 天") into a
   horizontal scroll box that visually collapses the row. Use .nowrap for the
   short ones (HOST / PORT, AUTH_DIR) and let the rest wrap. */
.kv dd.mono { white-space: normal; }
.kv dd.nowrap { white-space: nowrap; }
@media (max-width: 620px) {
  /* Narrow screens: stack, the two-column form is unreadable below this. */
  .kv { grid-template-columns: 1fr; gap: 4px 0; }
  .kv dd { padding-bottom: 8px; }
}
.spinner { width: 14px; height: 14px; border: 2px solid var(--muted);
           border-top-color: var(--foreground); border-radius: 50%;
           animation: spin 0.7s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: 22px; z-index: 100;
         padding: 9px 15px; border-radius: var(--radius); font-size: 13px;
         background: var(--popover); color: var(--popover-foreground);
         border: 1px solid var(--border); box-shadow: var(--shadow);
         display: none; max-width: 80vw; }
.toast.show { display: block; animation: rise 0.16s ease-out; }
.toast.err { border-color: color-mix(in oklch, var(--destructive) 45%, transparent); color: var(--destructive); }
.toast.ok { border-color: color-mix(in oklch, var(--ok) 45%, transparent); color: var(--ok); }
@keyframes rise { from { opacity: 0; transform: translate(-50%, 6px); } }

/* responsive */
.menu-btn { display: none; }
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; z-index: 40; width: 232px;
    transform: translateX(-100%); transition: transform 0.18s;
  }
  .sidebar.open { transform: none; box-shadow: var(--shadow); }
  .menu-btn { display: inline-flex; }
  .content, .topbar { padding-left: 14px; padding-right: 14px; }
}

/* Narrow screens: a 6-column table is unreadable at 390px (names truncate to
   "Bin...", the variant badge collides with the quota text, the note column
   collapses to a dash). Collapse .stack-table rows into key/value cards instead
   of shrinking the columns further. */
@media (max-width: 760px) {
  table.stack-table, table.stack-table tbody, table.stack-table tr,
  table.stack-table td { display: block; width: 100%; }
  table.stack-table thead { display: none; }
  table.stack-table tr {
    border: 1px solid var(--border); border-radius: var(--radius);
    padding: 12px 14px; margin-bottom: 10px;
    background: color-mix(in oklch, var(--accent) 25%, transparent);
  }
  table.stack-table td { border: 0; padding: 4px 0; max-width: none; }
  table.stack-table td:first-child { font-size: 14px; margin-bottom: 6px; }
  /* Each remaining cell gets an inline label so the value stays unambiguous. */
  table.stack-table td:not(:first-child) {
    display: flex; align-items: center; gap: 8px; font-size: 13px;
  }
  table.stack-table td:not(:first-child)::before {
    content: attr(data-label);
    color: var(--muted-foreground); font-size: 11px;
    min-width: 4.2rem; flex-shrink: 0;
  }
  table.stack-table .cell-actions { justify-content: flex-start; margin-top: 8px; }
  table.stack-table .trunc { max-width: none; }
  table.stack-table .cell-sub.trunc-sm { max-width: 16rem; }
  td.col-name, th.col-name, td.col-note, th.col-note { width: auto; }
}

/* Truncation: coordinator to keep table cells from widening the table past the
   viewport (uids are 16 digits, error notes are sentences). */
.trunc { display: block; max-width: 28ch; overflow: hidden;
         text-overflow: ellipsis; white-space: nowrap; }
.trunc-sm { max-width: 18ch; }
.trunc-lg { max-width: 42ch; }
.cell-sub.trunc { max-width: 22ch; } /* monospace uid line */

/* A metric whose detail is pure zeros reads as "unfinished" on a fresh box;
   drop it to muted-weak so the value itself carries the weight. */
.metric .detail.zero { opacity: 0.55; }

/* Column caps for the account table. With table-layout: fixed these widths are
   authoritative, so a 16-digit uid truncates instead of widening the table and
   pushing the action buttons off-screen. */
td.col-name, th.col-name { width: 17%; }
td.col-note, th.col-note { width: 27%; }
td.col-name .cell-main { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- login screen ---------- */
/* Shown until /api/session confirms the credential. It replaces the console
   entirely — the old flow rendered the console with empty data when the user
   dismissed the prompt, which read as a broken login. */
.login-wrap {
  min-height: calc(100vh - 120px);
  display: flex; align-items: center; justify-content: center;
  padding: 24px 16px;
}
.login-card {
  width: 100%; max-width: 380px;
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 26px 24px;
  display: flex; flex-direction: column; gap: 12px;
}
.login-brand { display: flex; align-items: center; gap: 11px; margin-bottom: 4px; }
.login-brand img { width: 30px; height: 30px; border-radius: 7px; }
.login-brand .name { font-weight: 600; font-size: 15px; }
.login-brand .muted { font-size: 12px; }
.login-card label { font-size: 12px; color: var(--muted-foreground); margin-top: 2px; }
.login-card .input { width: 100%; height: 36px; }
.login-card .btn { width: 100%; height: 36px; margin-top: 4px; }
.login-hint { font-size: 11px; line-height: 1.6; margin-top: 2px; }
.login-hint code { background: var(--background); padding: 1px 4px; border-radius: 3px; }
.login-err {
  background: color-mix(in oklch, var(--destructive) 12%, transparent);
  border: 1px solid color-mix(in oklch, var(--destructive) 40%, transparent);
  color: var(--destructive);
  border-radius: calc(var(--radius) - 2px);
  padding: 8px 10px; font-size: 12px;
}

/* ---------- login: two-credential explainer ---------- */
/* The login form takes the MANAGEMENT token only. Client API keys (sk-…) are
   called out explicitly so nobody pastes one here and wonders why it fails. */
.login-note {
  margin-top: 14px; padding-top: 12px;
  border-top: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 7px;
}
.login-note-title { font-size: 11px; font-weight: 500; color: var(--muted-foreground); }
.login-note-row {
  display: grid; grid-template-columns: 5.4rem 1fr; gap: 8px;
  align-items: start; font-size: 11px; line-height: 1.55;
  color: var(--muted-foreground);
}
.login-note-row .badge { justify-self: start; font-size: 10px; padding: 1px 6px; }
.login-note-row code { background: var(--background); padding: 1px 4px; border-radius: 3px; }
.login-note-row b { color: var(--foreground); font-weight: 600; }
