:root {
  --bg: #f6f6f4;
  --surface: #ffffff;
  --ink: #151515;
  --ink-2: #44474a;
  --ink-3: #6b6e72;
  --line: #e1e1dd;
  --line-2: #c9c9c4;
  --accent: #d93a14;
  --accent-hover: #bf3110;
  --accent-ink: #ffffff;
  --accent-soft: #fbe7e0;
  --lane-v: #151515;
  --lane-a: #d93a14;
  --lane-off: #e1e1dd;
  --ok: #19713f;
  --warn: #9a5b00;
  --bad: #b8261a;
  --focus: #1b5fd1;
  --r-s: 6px;
  --r-m: 10px;
  --font: 'Onest', system-ui, -apple-system, 'Segoe UI', Roboto, 'Noto Sans', sans-serif;
  --mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111213;
    --surface: #1a1b1d;
    --ink: #ececea;
    --ink-2: #b9bbbd;
    --ink-3: #8e9194;
    --line: #2b2d30;
    --line-2: #3d4044;
    --accent: #ff5f38;
    --accent-hover: #ff7856;
    --accent-ink: #160803;
    --accent-soft: #3a1c13;
    --lane-v: #ececea;
    --lane-a: #ff5f38;
    --lane-off: #2b2d30;
    --ok: #4cc27f;
    --warn: #e0a440;
    --bad: #ff6b5e;
    --focus: #7aa7ff;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 400 15px/1.5 var(--font);
  padding: 0 16px 64px;
}
[hidden] { display: none !important; }
img { max-width: 100%; display: block; }
.mono { font-family: var(--mono); font-size: 0.92em; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ---------- header ---------- */
.bar {
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 18px 0 8px;
}
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--ink);
  text-decoration: none;
  font-weight: 700;
  font-size: 18px;
  letter-spacing: -0.01em;
  margin-right: auto;
}
.brand svg { width: 26px; height: 26px; }
.brand .lane-v { fill: var(--lane-v); }
.brand .lane-a { fill: var(--lane-a); }
.brand .slash { color: var(--ink-3); font-weight: 500; margin: 0 1px; }

.route-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink-2);
  border-radius: 999px;
  padding: 6px 12px;
  font: 500 13px/1.2 var(--font);
  cursor: pointer;
  max-width: 52vw;
}
.route-pill span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ink-3); flex: none; }
.dot.ok { background: var(--ok); }
.dot.warn { background: var(--warn); }
.dot.bad { background: var(--bad); }

/* ---------- buttons ---------- */
button, .primary, .secondary, .ghost { font: inherit; }
.primary, .secondary, .ghost {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border-radius: var(--r-s);
  padding: 10px 16px;
  font-weight: 600;
  font-size: 15px;
  line-height: 1.2;
  cursor: pointer;
  text-decoration: none;
  border: 1px solid transparent;
  transition: background-color .15s, border-color .15s, color .15s;
  white-space: nowrap;
}
.primary { background: var(--accent); color: var(--accent-ink); }
.primary:hover { background: var(--accent-hover); }
.primary:disabled { background: var(--line-2); color: var(--ink-3); cursor: not-allowed; }
.secondary { background: var(--surface); color: var(--ink); border-color: var(--line-2); }
.secondary:hover { border-color: var(--ink-3); }
.ghost { background: transparent; color: var(--ink-2); border-color: var(--line); }
.ghost:hover { color: var(--ink); border-color: var(--line-2); background: var(--surface); }
.ghost.danger { color: var(--bad); }
.small { padding: 6px 10px; font-size: 13px; }
.link { background: none; border: 0; padding: 0; color: var(--ink-3); cursor: pointer; font-size: 13px; text-decoration: underline; text-underline-offset: 3px; }
.link:hover { color: var(--ink); }
.icon-btn {
  width: 36px; height: 36px;
  display: inline-grid; place-items: center;
  border: 0; border-radius: var(--r-s);
  background: transparent; color: var(--ink-3);
  font-size: 22px; line-height: 1; cursor: pointer;
}
.icon-btn:hover { color: var(--ink); background: var(--line); }
button[aria-busy="true"] { opacity: .7; cursor: progress; }

/* ---------- compose form ---------- */
main { max-width: 720px; margin: 0 auto; }
.compose { padding: 20px 0 8px; }
.field-label { display: block; font-weight: 600; font-size: 14px; color: var(--ink-2); margin: 0 0 6px; }
.url-row {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--surface);
  border: 1.5px solid var(--line-2);
  border-radius: var(--r-m);
  padding: 6px 6px 6px 14px;
  transition: border-color .15s;
}
.url-row:focus-within { border-color: var(--ink); }
.url-row input {
  flex: 1;
  min-width: 0;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--ink);
  font: 400 17px/1.4 var(--font);
  padding: 8px 0;
}
.url-row input::placeholder { color: var(--ink-3); }
.hint { margin: 6px 0 0; min-height: 1.4em; font-size: 13px; color: var(--ink-3); }
.hint.bad { color: var(--bad); }
.hint.ok { color: var(--ok); }

.preview {
  display: grid;
  grid-template-columns: 160px 1fr;
  gap: 14px;
  align-items: center;
  margin: 10px 0 4px;
}
.thumb { aspect-ratio: 16 / 9; border-radius: var(--r-s); overflow: hidden; background: var(--line); }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.pv-title { margin: 0; font-weight: 600; font-size: 17px; line-height: 1.3; overflow-wrap: anywhere;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.pv-sub { margin: 4px 0 0; color: var(--ink-3); font-size: 14px; }
.preview.loading .pv-title, .preview.loading .pv-sub {
  background: var(--line); color: transparent; border-radius: 4px; width: 80%;
}
.preview.loading .pv-sub { width: 40%; }

/* modes: the two-lane motif */
.modes { border: 0; padding: 0; margin: 22px 0 0; display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.modes legend { padding: 0; grid-column: 1 / -1; }
.mode {
  position: relative;
  display: grid;
  gap: 2px;
  align-content: start;
  padding: 12px 14px 14px;
  background: var(--surface);
  border: 1.5px solid var(--line);
  border-radius: var(--r-m);
  cursor: pointer;
  transition: border-color .15s, background-color .15s;
}
.mode:hover { border-color: var(--line-2); }
.mode input { position: absolute; opacity: 0; pointer-events: none; }
.mode:has(input:checked) { border-color: var(--ink); }
.mode:has(input:focus-visible) { outline: 2px solid var(--focus); outline-offset: 2px; }
.mode-name { font-weight: 600; font-size: 16px; }
.mode-desc { font-size: 13px; color: var(--ink-3); }
.lanes { display: grid; gap: 5px; margin-top: 10px; }
.lanes i { display: block; height: 7px; border-radius: 2px; background: transparent; border: 1.5px dashed var(--lane-off); }
.lanes i.on { border: 0; height: 7px; }
.lanes i.v.on { background: var(--lane-v); }
.lanes i.a.on { background: var(--lane-a); width: 72%; }
.mode:not(:has(input:checked)) .lanes i.on { opacity: .35; }

.options { display: flex; flex-wrap: wrap; gap: 16px 24px; margin-top: 20px; align-items: flex-end; }
.opt { min-width: 0; }
select, textarea, .login-form input {
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1.5px solid var(--line-2);
  border-radius: var(--r-s);
  padding: 8px 10px;
}
select { padding-right: 28px; min-width: 170px; }
textarea { width: 100%; font-family: var(--mono); font-size: 13px; resize: vertical; }
select:focus, textarea:focus, .login-form input:focus { outline: 0; border-color: var(--ink); }

.seg { display: inline-flex; border: 1.5px solid var(--line-2); border-radius: var(--r-s); overflow: hidden; background: var(--surface); }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.seg span { display: block; padding: 8px 12px; font-weight: 500; font-size: 14px; color: var(--ink-2); cursor: pointer; }
.seg label + label span { border-left: 1px solid var(--line); }
.seg input:checked + span { background: var(--ink); color: var(--bg); }
.seg input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: -2px; }
.seg input:disabled + span { color: var(--line-2); cursor: not-allowed; text-decoration: line-through; }

.submit-row { display: flex; align-items: center; gap: 16px; margin-top: 22px; flex-wrap: wrap; }
.submit-row .primary { padding: 13px 28px; font-size: 16px; }
.estimate { margin: 0; color: var(--ink-3); }
.form-error { margin: 12px 0 0; padding: 10px 12px; border-radius: var(--r-s); background: var(--accent-soft); color: var(--bad); font-size: 14px; }

/* ---------- jobs ---------- */
.jobs { margin-top: 36px; border-top: 1px solid var(--line); padding-top: 18px; }
.jobs-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.jobs h2 { margin: 0; font-size: 17px; }
.empty { color: var(--ink-3); font-size: 14px; margin: 8px 0 0; }
.job-list { list-style: none; margin: 8px 0 0; padding: 0; }
.job {
  display: grid;
  grid-template-columns: 96px 1fr auto;
  gap: 14px;
  padding: 14px 0;
  border-bottom: 1px solid var(--line);
  align-items: start;
}
.job-thumb { aspect-ratio: 16 / 9; border-radius: 4px; overflow: hidden; background: var(--line); }
.job-thumb img { width: 100%; height: 100%; object-fit: cover; }
.job-main { min-width: 0; }
.job-title { margin: 0; font-weight: 600; line-height: 1.3; overflow-wrap: anywhere;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.job-spec { margin: 2px 0 0; color: var(--ink-3); font-size: 12.5px; }
.job-lanes { display: grid; gap: 4px; margin: 8px 0 6px; max-width: 360px; }
.jl { height: 5px; border-radius: 2px; background: var(--lane-off); overflow: hidden; }
.jl span { display: block; height: 100%; width: 0; transition: width .4s ease-out; }
.jl.v span { background: var(--lane-v); }
.jl.a span { background: var(--lane-a); }
.job.merging .jl span { width: 100% !important; }
.job.done .job-lanes, .job.error .job-lanes, .job.cancelled .job-lanes, .job.expired .job-lanes { display: none; }
.job-status { margin: 0; font-size: 13.5px; color: var(--ink-2); overflow-wrap: anywhere; }
.job.done .job-status { color: var(--ok); }
.job.error .job-status { color: var(--bad); }
.job.expired .job-status, .job.cancelled .job-status { color: var(--ink-3); }
.job-actions { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.job-attempts { margin-top: 6px; font-size: 13px; color: var(--ink-3); }
.job-attempts summary { cursor: pointer; width: max-content; }
.job-attempts ol { margin: 4px 0 0; padding-left: 20px; }
.job-attempts li { overflow-wrap: anywhere; }

/* ---------- login ---------- */
.login { display: grid; place-items: center; min-height: 70vh; }
.login-form { width: min(340px, 100%); display: grid; gap: 10px; }
.login-form h1 { margin: 0 0 8px; font-size: 22px; }
.login-form input { width: 100%; font-size: 16px; padding: 10px 12px; }

/* ---------- settings dialog ---------- */
dialog {
  width: min(760px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 1px solid var(--line-2);
  border-radius: var(--r-m);
  background: var(--bg);
  color: var(--ink);
}
dialog::backdrop { background: rgb(0 0 0 / .45); }
.dlg-head {
  position: sticky; top: 0; z-index: 1;
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px 12px 22px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.dlg-head h2 { margin: 0; font-size: 18px; }
.dlg-body { padding: 4px 22px 24px; }
.dlg-body section { padding: 18px 0; border-bottom: 1px solid var(--line); }
.dlg-body section:last-child { border-bottom: 0; }
.dlg-body h3 { margin: 0 0 6px; font-size: 16px; }
.note { margin: 6px 0 10px; color: var(--ink-3); font-size: 13.5px; max-width: 68ch; }
.row-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-top: 12px; }
.check { display: flex; align-items: center; gap: 8px; margin: 6px 0; font-size: 14.5px; cursor: pointer; }
.check input { width: 17px; height: 17px; accent-color: var(--ink); }
.dlg-body .field-label { margin-top: 14px; }
.dlg-body select { width: 100%; max-width: 420px; }
.state-line { margin: 0; font-weight: 500; }
.file-btn { position: relative; overflow: hidden; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

.table-wrap { overflow-x: auto; margin-top: 8px; }
.routes { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.routes th { text-align: left; font-weight: 600; color: var(--ink-3); padding: 6px 10px 6px 0; border-bottom: 1px solid var(--line); }
.routes td { padding: 8px 10px 8px 0; border-bottom: 1px solid var(--line); vertical-align: top; }
.routes td:first-child { font-weight: 500; white-space: nowrap; }
.routes .sub { display: block; color: var(--ink-3); font-size: 12px; font-weight: 400; }
.routes .err { display: block; color: var(--ink-3); font-size: 12px; max-width: 36ch; overflow-wrap: anywhere; }
.st { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.sys { display: grid; grid-template-columns: max-content 1fr; gap: 6px 18px; margin: 4px 0 0; font-size: 14px; }
.sys dt { color: var(--ink-3); }
.sys dd { margin: 0; overflow-wrap: anywhere; }

/* ---------- responsive ---------- */
@media (max-width: 560px) {
  .bar { padding-top: 12px; gap: 8px; }
  .brand { font-size: 16px; }
  .routes thead { display: none; }
  .routes, .routes tbody, .routes tr, .routes td { display: block; width: 100%; }
  .routes tr { padding: 10px 0; border-bottom: 1px solid var(--line); }
  .routes td { border: 0; padding: 2px 0; }
  .routes .err { max-width: none; }
  .bar .ghost { padding: 6px 10px; font-size: 13px; }
  .preview { grid-template-columns: 112px 1fr; gap: 10px; }
  .pv-title { font-size: 15px; }
  .modes { gap: 6px; }
  .mode { padding: 10px 10px 12px; }
  .mode-name { font-size: 15px; }
  .mode-desc { font-size: 12px; }
  .options { gap: 14px; }
  .opt, .opt select { width: 100%; }
  .submit-row .primary { width: 100%; }
  .job { grid-template-columns: 72px 1fr; gap: 10px; }
  .job-actions { grid-column: 2; flex-direction: row; align-items: center; justify-content: flex-start; flex-wrap: wrap; }
  .dlg-body { padding: 4px 16px 20px; }
  .dlg-head { padding-left: 16px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

/* ---------- settings tabs ---------- */
dialog[open] { display: flex; flex-direction: column; }
.dlg-head { position: static; border-bottom: 0; padding-bottom: 4px; }
.tabs {
  display: flex;
  gap: 2px;
  padding: 0 16px 0 22px;
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
  flex: none;
}
.tabs button {
  border: 0;
  background: none;
  padding: 10px 12px;
  margin-bottom: -1px;
  font: 500 14px/1.2 var(--font);
  color: var(--ink-3);
  border-bottom: 2px solid transparent;
  cursor: pointer;
  white-space: nowrap;
}
.tabs button:hover { color: var(--ink); }
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--ink); }
.dlg-body { overflow-y: auto; flex: 1 1 auto; }
.pane-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.api-link { padding: 10px 14px; }

/* ---------- API keys ---------- */
.reveal { margin: 14px 0; padding: 14px; border-radius: var(--r-m); background: var(--accent-soft); }
.reveal-title { margin: 0 0 8px; font-weight: 600; }
.reveal-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.reveal code {
  flex: 1 1 260px;
  min-width: 0;
  padding: 9px 10px;
  background: var(--surface);
  border: 1px solid var(--line-2);
  border-radius: var(--r-s);
  overflow-wrap: anywhere;
  user-select: all;
  font-size: 13px;
}
.reveal .link { margin-top: 10px; }

.key-list { list-style: none; margin: 10px 0 0; padding: 0; }
.key { padding: 12px 0; border-bottom: 1px solid var(--line); }
.key:first-child { border-top: 1px solid var(--line); }
.key-top { display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; }
.key-name { font-weight: 600; font-size: 15px; overflow-wrap: anywhere; }
.key-state { font-size: 13px; font-weight: 500; }
.key-state.ok { color: var(--ok); }
.key-state.bad { color: var(--bad); }
.key-hint { margin: 2px 0 0; color: var(--ink-2); font-size: 12.5px; }
.key-meta, .key-usage { margin: 4px 0 0; font-size: 13px; color: var(--ink-3); }
.key-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.key-confirm {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-top: 10px;
  padding: 12px;
  border-radius: var(--r-s);
  background: var(--surface);
  border: 1px solid var(--line-2);
}
.key-confirm-text { flex-basis: 100%; margin: 0; font-size: 13.5px; }
.key-confirm-err { flex-basis: 100%; margin: 0; color: var(--bad); min-height: 0; }
.key-log ol {
  list-style: none;
  margin: 10px 0 0;
  padding: 8px 10px;
  max-height: 240px;
  overflow: auto;
  font: 12px/1.7 var(--mono);
  color: var(--ink-2);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-s);
  white-space: pre-wrap;
}
.key-log li.bad { color: var(--bad); }

.new-key { margin-top: 16px; }
.new-key summary {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 9px 14px;
  border: 1px solid var(--line-2);
  border-radius: var(--r-s);
  background: var(--surface);
  font-weight: 600;
  cursor: pointer;
  list-style: none;
}
.new-key summary::-webkit-details-marker { display: none; }
.new-key summary::before { content: '+'; font-weight: 500; }
.new-key[open] summary::before { content: '−'; }
.key-form { padding-top: 4px; }
.key-form input:not([type="checkbox"]), .key-confirm input {
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1.5px solid var(--line-2);
  border-radius: var(--r-s);
  padding: 8px 10px;
  width: 100%;
  max-width: 420px;
}
.key-confirm input { width: auto; flex: 1 1 200px; }
.key-form input:focus, .key-confirm input:focus { outline: 0; border-color: var(--ink); }
.key-form select { max-width: none; }
.form-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.form-grid input { max-width: none; }
.scopes { border: 0; padding: 0; margin: 14px 0 0; }
.scopes .check { align-items: flex-start; }
.scopes .check input { margin-top: 3px; flex: none; }
.scopes b { font-weight: 600; }

@media (max-width: 560px) {
  .rt-extra { display: none; }
  .api-link { padding: 6px 10px; font-size: 13px; }
  .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .tabs { padding: 0 8px; gap: 0; }
  .tabs button { padding: 10px 8px; font-size: 13.5px; }
}

.routes .proxy-summary td { color: var(--ink-2); font-size: 13.5px; }
.routes .proxy-summary .link { margin-left: 6px; }

/* ---------- agent ---------- */
.state-line .dot { display: inline-block; vertical-align: middle; margin-right: 2px; }
.agent-steps { padding-left: 20px; margin: 6px 0 4px; }
.agent-steps li { margin: 3px 0; }
a.primary.disabled { pointer-events: none; background: var(--line-2); color: var(--ink-3); }
.bad-text { color: var(--bad); }
