:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --line: #dfe4ea;
  --line-soft: #eef1f5;
  --text: #1c2530;
  --muted: #5b6773;
  --accent: #2563eb;
  --running: #2563eb;
  --success: #15803d;
  --failed: #c2410c;
  --aborted: #a16207;
  --unknown: #64748b;
  --radius: 10px;
  --pad-x: 20px;
  --indent-machine: 18px;
  --indent-job: 36px;
  --topbar-h: 56px;
  --z-topbar: 10;
  --z-detail: 20;
  --z-login: 30;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, "Segoe UI", "Microsoft YaHei", sans-serif;
}

button, select, input { font: inherit; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: var(--z-topbar);
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  justify-content: flex-start;
  padding: 10px max(var(--pad-x), env(safe-area-inset-right)) 10px max(var(--pad-x), env(safe-area-inset-left));
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.topbar h1 { margin: 0; font-size: 17px; text-wrap: balance; }

.controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-left: auto; }
.row-tools { display: flex; gap: 8px; align-items: center; }

/* 账号信息独立成组：桌面端排在控件之后靠最右，手机端提到标题同行 */
.account { display: flex; gap: 8px; align-items: center; order: 5; }
.current-user { color: var(--muted); font-size: 13px; }

.controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

.controls select,
.controls input[type="search"] {
  padding: 5px 9px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

/* 两个下拉等宽（桌面端）：手机端在媒体查询里改用 grid 控制列宽。
   7rem 足够放下最长的"按提交时间"。 */
#statusFilter,
#defaultSort { flex: 0 0 7rem; min-width: 0; }

.check { display: flex; gap: 6px; align-items: center; color: var(--muted); }

.freshness { color: var(--muted); font-size: 12px; min-width: 92px; font-variant-numeric: tabular-nums; }

.picker { position: relative; }
.picker > summary {
  list-style: none;
  cursor: pointer;
  padding: 5px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}
.picker > summary::-webkit-details-marker { display: none; }
.picker[open] > summary { border-color: var(--accent); }
.picker-list {
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  z-index: var(--z-topbar);
  min-width: 11rem;
  padding: 8px 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.1);
  display: grid;
  gap: 4px;
}
.picker-item { display: flex; gap: 6px; align-items: center; font-size: 13px; white-space: nowrap; }
.picker-item input:disabled + span { color: var(--muted); }

/* 状态多选选择器：summary 会出现"状态：运行中、失败"这种摘要，
   固定宽度 + 省略号，避免选项变化时把搜索框挤得忽宽忽窄。 */
.status-picker > summary {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 13px;
}
.status-picker .picker-list { left: 0; right: auto; }

button { cursor: pointer; border-radius: 6px; border: 1px solid transparent; padding: 5px 10px; }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button:disabled:hover { filter: none; }
button.ghost { background: #fff; border-color: var(--line); color: var(--text); }
button.danger { background: #fff; border-color: #f0b4a4; color: #b42318; }
button:hover { filter: brightness(0.97); }

.banner {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  margin: 12px max(var(--pad-x), env(safe-area-inset-right)) 0 max(var(--pad-x), env(safe-area-inset-left));
  padding: 8px 12px;
  background: #fdf3f1;
  border: 1px solid #f0b4a4;
  border-radius: var(--radius);
  color: #8a1c12;
}

.board {
  padding: 12px max(var(--pad-x), env(safe-area-inset-right)) 32px max(var(--pad-x), env(safe-area-inset-left));
}

/* ---------------------------------------------------------------- 作业表格 */

table.jobs {
  width: 100%;
  table-layout: fixed;
  /* separate + border-spacing 0：collapse 下 Edge 仍会按默认 border-spacing 留出 2px，
     表格白底从单元格左边露出来，在分组行的深色背景旁显成一条白竖条。 */
  border-collapse: separate;
  border-spacing: 0;
  background: var(--panel);
  /* 只留上下边框：左右边框紧贴分组行的深色背景，同样会显成一条突兀的浅色竖条 */
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  font-size: 13px;
}

table.jobs th,
table.jobs td {
  padding: 5px 10px;
  text-align: left;
  border-bottom: 1px solid var(--line-soft);
  vertical-align: middle;
  white-space: nowrap;
}

table.jobs thead th {
  background: #f8fafb;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-weight: 600;
  font-size: 12px;
}

button.sort {
  appearance: none;
  background: none;
  border: none;
  padding: 0;
  color: inherit;
  font: inherit;
  cursor: pointer;
}

.col-status { width: 7rem; }
.col-model { width: 14rem; }
.col-job { width: 7.5rem; }
.col-started { width: 8.5rem; }
.col-progress { width: 12.5rem; }
.col-step { width: 7.5rem; }
.col-elapsed { width: 7.5rem; }
.col-eta { width: 9rem; }
.col-submitter { width: 6.5rem; }

.col-step,
.col-elapsed,
.col-eta,
.col-started,
.col-progress .pct,
table.jobs td.col-status {
  font-variant-numeric: tabular-nums;
}

td.col-model,
td.col-job,
td.col-started,
td.col-submitter { overflow: hidden; text-overflow: ellipsis; }

/* 模型名过长时省略号截断（悬停看全名）；"已归档"标签不允许被挤掉 */
.model-cell { display: flex; align-items: center; gap: 6px; min-width: 0; }

button.row-open {
  appearance: none;
  background: none;
  border: none;
  padding: 0;
  flex: 0 1 auto;
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font: inherit;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  text-align: left;
}
button.row-open:hover { color: var(--accent); }

.tag {
  flex: none;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--line-soft);
  color: var(--muted);
  font-size: 11px;
  font-weight: 500;
}

.dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-right: 6px;
  border-radius: 50%;
  background: var(--unknown);
}
.status-running .dot { background: var(--running); }
.status-success .dot { background: var(--success); }
.status-failed .dot { background: var(--failed); }
.status-aborted .dot { background: var(--aborted); }

td.col-status {
  border-left: 3px solid var(--unknown);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.status-running td.col-status { border-left-color: var(--running); color: var(--running); }
.status-success td.col-status { border-left-color: var(--success); color: var(--success); }
.status-failed td.col-status { border-left-color: var(--failed); color: var(--failed); }
.status-aborted td.col-status { border-left-color: var(--aborted); color: var(--aborted); }
.status-unknown td.col-status { color: var(--unknown); }

/* 已完成的作业：次要列弱化，让运行中和失败一眼可见；模型名保持主色，不发灰 */
tr.status-success td:not(.col-status):not(.col-model) { color: var(--muted); }

td.col-progress { white-space: nowrap; }
.bar {
  display: inline-block;
  width: 6.5rem;
  height: 6px;
  margin-right: 8px;
  border-radius: 999px;
  background: var(--line-soft);
  overflow: hidden;
  vertical-align: middle;
}
.bar > span { display: block; height: 100%; background: var(--running); }
.status-success .bar > span { background: var(--success); }
.status-failed .bar > span { background: var(--failed); }
.status-aborted .bar > span { background: var(--aborted); }

tr.job-row { cursor: pointer; }
tr.job-row:hover { background: #eef4fb; }
tr.job-row.alt { background: #fafbfc; }
tr.job-row.archived { color: var(--muted); }
tr.job-row.archived button.row-open { color: var(--muted); font-weight: 500; }

/* 层级缩进：项目顶格 → 机器一级 → 作业两级。
   选择器带 table.jobs 前缀，避免被媒体查询里的 padding 简写覆盖。 */
table.jobs tr.group-row th { padding-left: 12px; }
table.jobs tr.machine-row th { padding-left: calc(12px + var(--indent-machine)); }
table.jobs tr.job-row td:first-child { padding-left: calc(10px + var(--indent-job)); }
table.jobs thead th:first-child { padding-left: calc(10px + var(--indent-job)); }

td.col-submitter { overflow: hidden; text-overflow: ellipsis; color: var(--muted); }

tr.spacer td { height: 24px; padding: 0; border: 0; background: transparent; }

tr.group-row th {
  background: #dfe6ee;
  border-top: 3px solid #b4c1d0;
  border-bottom: 1px solid #c9d3de;
  padding-top: 7px;
  padding-bottom: 7px;
}
.group-name { font-size: 14px; font-weight: 700; }

/* 项目名可收缩并省略号截断，计数标签固定不缩——项目名再长也不会把计数挤到第二行 */
.group-head { display: flex; align-items: center; gap: 8px; min-width: 0; }
.group-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.group-chips { display: flex; flex-shrink: 0; }

/* 滚动时贴在顶栏下方的"当前项目"条。
   不用 CSS sticky：浏览器的粘性约束算在整张表上，多个项目行会同时吸顶并互相重叠。
   不加 box-shadow：阴影会在下方拖出一条灰带，看起来像多余的边线。
   左右铺满屏幕、内容用 padding 对齐表格：条本身比页面背景亮，
   只铺到表格宽度的话左右两端会显成两条竖线。 */
.group-bar {
  position: fixed;
  top: var(--topbar-h);
  left: 0;
  right: 0;
  z-index: 5;
  padding: 7px max(var(--pad-x), env(safe-area-inset-right)) 7px max(var(--pad-x), env(safe-area-inset-left));
  background: #dfe6ee;
  border-top: 3px solid #b4c1d0;
  border-bottom: 1px solid #b4c1d0;
}

/* 浮动表头：与项目条同一套 JS 机制，贴在项目条下方。
   背景与表头单元格一致（#f8fafb）：容器若用白色，padding 区会和单元格差出
   7 个色阶，在左右两端显成两条竖线。 */
.header-bar {
  position: fixed;
  left: 0;
  right: 0;
  z-index: 5;
  padding: 0 max(var(--pad-x), env(safe-area-inset-right)) 0 max(var(--pad-x), env(safe-area-inset-left));
  background: #f8fafb;
}
/* 只保留表头单元格自己的那条下边框：再加表格边框和阴影会叠成一条灰带 */
.header-bar table.jobs { border: 0; }

.chip {
  margin-left: 8px;
  padding: 1px 8px;
  border-radius: 999px;
  background: #fff;
  border: 1px solid var(--line);
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.chip.running { border-color: #b7cdf7; color: var(--running); }
.chip.failed { border-color: #f0b4a4; color: var(--failed); }
.chip.success { color: var(--muted); }
.group-chips .chip:first-child { margin-left: 0; }

tr.machine-row th {
  background: #eef2f6;
  border-bottom: 1px solid var(--line);
  padding-top: 6px;
  padding-bottom: 6px;
  font-weight: 500;
}
tr.machine-row.offline .machine-metrics { opacity: 0.75; }
.machine-name { font-weight: 700; color: var(--text); }
.machine-metrics {
  margin-left: 12px;
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}
.machine-metrics > span + span { margin-left: 12px; }

/* 机器行的精简摘要按钮：只在手机端出现，点开才显示全部指标 */
.machine-summary {
  display: none;
  align-items: center;
  gap: 4px;
  margin-left: 8px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}
.machine-summary .chevron { font-size: 10px; }
tr.machine-row.expanded .machine-summary .chevron { transform: rotate(180deg); }

.badge { font-size: 11px; padding: 0 7px; border-radius: 999px; background: var(--line-soft); color: var(--muted); }
.badge.online { background: #e7f6ec; color: var(--success); }
.badge.offline { background: #f1f2f4; color: var(--muted); }
.machine-row .badge { margin-left: 8px; }

.empty { color: var(--muted); padding: 20px 0; display: grid; gap: 10px; justify-items: start; }

.skeleton-line { display: block; height: 9px; border-radius: 3px; background: var(--line-soft); }

/* ---------------------------------------------------------------- 详情面板 */

.detail {
  position: fixed;
  inset: 0 0 0 auto;
  width: min(520px, 100vw);
  background: var(--panel);
  border-left: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  z-index: var(--z-detail);
  box-shadow: -8px 0 24px rgba(15, 23, 42, 0.08);
}

.detail-head {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 10px max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
}
.detail-title { flex: 1; margin: 0; font-size: 15px; font-weight: 600; overflow-wrap: anywhere; text-wrap: balance; }
.detail-actions { display: flex; gap: 8px; }
.detail-body {
  padding: 14px max(16px, env(safe-area-inset-right)) max(40px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  overflow-y: auto;
  overflow-x: hidden;
  display: grid;
  gap: 16px;
}

.section { min-width: 0; }
.section > h3 { margin: 0 0 8px; font-size: 13px; color: var(--muted); text-wrap: balance; }

.table-wrap { overflow-x: auto; }

.kv { display: grid; grid-template-columns: 96px 1fr; gap: 3px 12px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; font-variant-numeric: tabular-nums; text-wrap: pretty; }

.chart { width: 100%; height: 240px; }

table.warnings { width: 100%; border-collapse: collapse; font-size: 12px; }
table.warnings th, table.warnings td { text-align: left; padding: 5px 8px; border-bottom: 1px solid var(--line-soft); vertical-align: top; }
table.warnings th { color: var(--muted); font-weight: 500; white-space: nowrap; }
table.warnings td { text-wrap: pretty; }
table.warnings td.count { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }

ul.errors { margin: 0; padding-left: 18px; display: grid; gap: 6px; }
ul.errors li { color: #b42318; font-size: 13px; overflow-wrap: anywhere; text-wrap: pretty; }

.hint { color: var(--muted); font-size: 12px; text-wrap: pretty; }

/* ---------------------------------------------------------------- 登录与对话框 */

.login {
  position: fixed;
  inset: 0;
  z-index: var(--z-login);
  display: grid;
  place-items: center;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  background: rgba(244, 246, 248, 0.96);
}
.login-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 24px;
  display: grid;
  gap: 10px;
  min-width: 260px;
}
.login-card h2 { margin: 0; font-size: 16px; text-wrap: balance; }
.login-card label { color: var(--muted); font-size: 13px; }
.login-card input { padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; }
.login-card input[aria-invalid="true"] { border-color: #b42318; }
.login-card button { background: var(--accent); color: #fff; }
.error { color: #b42318; margin: 0; font-size: 13px; }

.dialog {
  border: none;
  border-radius: var(--radius);
  padding: 0;
  max-width: min(420px, calc(100vw - 32px));
  color: var(--text);
}
.dialog::backdrop { background: rgba(15, 23, 42, 0.35); }
.dialog-card { padding: 20px; display: grid; gap: 12px; }
.dialog-card h2 { margin: 0; font-size: 16px; text-wrap: balance; }
.dialog-card p { margin: 0; color: var(--muted); text-wrap: pretty; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; }

/* ---------------------------------------------------------------- 手机 */

.current-user { color: var(--muted); font-size: 12px; }

/* ---------------------------------------------------------------- 用户与机器管理 */

.admin {
  padding: 16px max(var(--pad-x), env(safe-area-inset-right)) 40px max(var(--pad-x), env(safe-area-inset-left));
  display: grid;
  gap: 16px;
}
.admin-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.admin-head h2 { margin: 0; font-size: 16px; text-wrap: balance; }
.admin-body { display: grid; gap: 20px; }

table.grid { width: 100%; border-collapse: collapse; background: var(--panel); border: 1px solid var(--line); font-size: 13px; }
table.grid th, table.grid td { padding: 6px 10px; text-align: left; border-bottom: 1px solid var(--line-soft); vertical-align: middle; }
table.grid th { background: #f8fafb; color: var(--muted); font-weight: 600; font-size: 12px; white-space: nowrap; }
table.grid td.num { text-align: right; font-variant-numeric: tabular-nums; }
table.grid td.actions { display: flex; flex-wrap: wrap; gap: 6px; }
table.grid select { padding: 4px 8px; border: 1px solid var(--line); border-radius: 6px; background: #fff; }

.inline-form { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 10px; }
.inline-form input[type="text"],
.inline-form input:not([type]) { padding: 5px 9px; border: 1px solid var(--line); border-radius: 6px; }
.inline-form input { padding: 5px 9px; border: 1px solid var(--line); border-radius: 6px; }
.inline-form button { background: var(--accent); color: #fff; }

code.token {
  display: inline-block;
  margin: 0 6px;
  padding: 2px 6px;
  background: var(--line-soft);
  border-radius: 4px;
  font-size: 12px;
  word-break: break-all;
}

.dialog-card label { color: var(--muted); font-size: 13px; }
.dialog-card input { padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; }

@media (max-width: 900px) {
  :root {
    --pad-x: 12px;
    --indent-machine: 8px;
    --indent-job: 12px;
  }
  .topbar { padding-top: 8px; padding-bottom: 8px; }
  /* 标题与账号同行，控件整体换到下面，省掉原来"更新于"那一行 */
  .topbar h1 { font-size: 15px; flex: 1 1 auto; }
  .account { order: 1; }
  /* 控件区改 grid 三列：状态筛选 | 搜索框 | 排序。
     "显示列 + 用户与机器"单独占第二列，于是和搜索框左右都对齐、宽度也相同。
     列宽由"行3 宽 = 搜索框宽"反推：2W + 16 + 64 + 84 = 366 → W ≈ 101px，
     实测 6.0625rem(97px) 刚好放下最长的"全部状态"，按钮 padding 同步收窄。 */
  .controls {
    width: 100%;
    order: 3;
    margin-left: 0;
    display: grid;
    grid-template-columns: 6.0625rem minmax(0, 1fr) 6.0625rem;
    gap: 8px;
    align-items: center;
  }
  .row-tools { grid-column: 2; min-width: 0; }
  .freshness { display: none; }
  #statusPicker,
  #defaultSort,
  .controls input[type="search"] { width: 100%; min-width: 0; flex: none; }
  /* 状态选择器的 summary 也要顶满这一列，和排序下拉同宽 */
  #statusPicker > summary { display: block; width: 100%; padding-left: 7px; padding-right: 7px; }
  /* 收窄按钮内边距，让"显示列 + 用户与机器"正好填满搜索框那一列。
     不加 nowrap / flex-shrink:0 的话，flex 会把按钮压窄、文字折成两行。 */
  .row-tools > * { padding-left: 7px; padding-right: 7px; flex: 0 0 auto; white-space: nowrap; }
  .board { padding-top: 10px; padding-bottom: 24px; }

  table.jobs { font-size: 12px; }
  table.jobs th, table.jobs td { padding: 6px 6px; }
  /* 最后一列右侧留白：文字贴边会让人误以为表格还能往右滑。
     注意光加 padding-right 没用——"12 小时 41 分"比内容区还宽，会溢出进 padding，
     必须把列本身加宽到能放下文字并留出右边距。 */
  table.jobs th:last-child,
  table.jobs td:last-child { padding-right: 12px; }
  /* 状态列要放得下"缩进 + 圆点 + 三个字"，否则会和模型名撞在一起 */
  .col-status { width: 5.2rem; }
  .col-model { width: auto; }
  .col-job { width: 5rem; }
  /* 进度条缩短，把省下的宽度让给模型列，避免最后一列加宽后模型名被挤 */
  .col-progress { width: 5.4rem; }
  .col-elapsed { width: 5.6rem; }
  .bar { width: 1.9rem; margin-right: 4px; }
  tr.group-row th, tr.machine-row th { white-space: normal; }

  /* 手机上空间紧：机器行只留摘要，点开才看全部指标 */
  .machine-metrics { display: none; }
  tr.machine-row.expanded .machine-metrics { display: block; margin: 6px 0 0; }
  .machine-summary { display: inline-flex; }

  .detail { inset: 0; width: 100vw; border-left: none; }
  .chart { height: 220px; }
}

/* 极窄屏（360px 及以下）：下拉已按最长选项定宽，再挤搜索框就会截断，
   所以"显示归档"只留复选框，文字交给 title 提示。 */
@media (max-width: 370px) {
  .check > span { display: none; }
  .check { gap: 0; }
}
