:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f8fafb;
  --line: #e1e6eb;
  --line-strong: #c9d2da;
  --text: #17212b;
  --muted: #5d6b78;
  --faint: #8a96a2;
  --accent: #1d6fd6;
  --accent-ink: #ffffff;
  --accent-soft: #e7f0fc;
  --good: #1f7a4d;
  --good-soft: #e3f4ea;
  --warn: #9a6200;
  --warn-soft: #fdf1d8;
  --bad: #c0303e;
  --bad-soft: #fbe5e7;
  --info-soft: #eef2f6;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --radius: 10px;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1419;
    --surface: #171d24;
    --surface-2: #1c232b;
    --line: #29323c;
    --line-strong: #3a4552;
    --text: #e6ebf0;
    --muted: #9aa7b4;
    --faint: #6f7c89;
    --accent: #4c94ef;
    --accent-ink: #0b1220;
    --accent-soft: #1b2a3d;
    --good: #4cc38a;
    --good-soft: #15291f;
    --warn: #e2a93b;
    --warn-soft: #2d2412;
    --bad: #f0707b;
    --bad-soft: #321a1d;
    --info-soft: #1e252d;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
}
h1, h2, h3 { margin: 0; line-height: 1.3; font-weight: 650; }
h1 { font-size: 22px; }
h2 { font-size: 16px; }
p { margin: 6px 0; }
small, .hint { color: var(--muted); font-size: 13px; }
b { font-weight: 650; }
[hidden] { display: none !important; }
.num { font-variant-numeric: tabular-nums; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.mono { font-family: var(--mono); font-size: 12px; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 24px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand { font-weight: 700; font-size: 16px; white-space: nowrap; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 2px 10px; border-radius: 999px;
  font-size: 12px; font-weight: 600; background: var(--info-soft); color: var(--muted); white-space: nowrap;
}
.chip.good { background: var(--good-soft); color: var(--good); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.bad { background: var(--bad-soft); color: var(--bad); }
.chip.accent { background: var(--accent-soft); color: var(--accent); }
.conn { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); flex: none; }
.conn.good .dot { background: var(--good); }
.conn.bad .dot { background: var(--bad); }
.conn.bad { color: var(--bad); }
.emergency-link { color: var(--bad); font-weight: 650; font-size: 13px; text-decoration: none; white-space: nowrap; }
.emergency-link:hover { text-decoration: underline; }

main { max-width: 1240px; margin: 0 auto; padding: 20px 24px 64px; }

/* buttons & inputs */
.btn {
  appearance: none; border: 1px solid transparent; border-radius: 8px; padding: 9px 16px;
  font: inherit; font-weight: 600; cursor: pointer; background: var(--surface-2); color: var(--text);
  border-color: var(--line-strong); white-space: nowrap;
}
.btn:hover:not(:disabled) { filter: brightness(.97); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn.ghost { background: transparent; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.wide { width: 100%; }
.btn.huge { width: 100%; padding: 18px; font-size: 18px; margin: 12px 0; }
.btn:focus-visible, input:focus-visible, select:focus-visible, summary:focus-visible, a:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 45%, transparent); outline-offset: 2px;
}
label { display: block; font-size: 13px; color: var(--muted); font-weight: 600; }
input, select {
  display: block; width: 100%; margin-top: 4px; padding: 8px 10px; font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--line-strong); border-radius: 7px;
}
input[aria-invalid="true"] { border-color: var(--bad); }
.field-help { display: block; font-weight: 400; color: var(--faint); font-size: 12px; margin-top: 3px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-error { color: var(--bad); font-size: 13px; font-weight: 600; }

/* login & emergency */
.login, .emergency {
  max-width: 440px; margin: 8vh auto; background: var(--surface); border: 1px solid var(--line);
  border-radius: 14px; padding: 32px; box-shadow: var(--shadow);
}
.emergency { max-width: 560px; }
.login form { margin-top: 20px; display: grid; gap: 10px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; margin: 16px 0; font-size: 14px; }
.kv dt, .kv .k { color: var(--muted); }

/* banner */
.banner {
  display: flex; justify-content: space-between; align-items: flex-start; gap: 16px;
  padding: 18px 20px; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--line);
  border-left: 5px solid var(--faint); box-shadow: var(--shadow); margin-bottom: 16px;
}
.banner.good { border-left-color: var(--good); }
.banner.warn { border-left-color: var(--warn); background: var(--warn-soft); }
.banner.bad { border-left-color: var(--bad); background: var(--bad-soft); }
.banner.accent { border-left-color: var(--accent); }
.banner-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.banner #detail { color: var(--muted); }
.banner ul { margin: 6px 0 0; padding-left: 18px; color: var(--text); }

/* tiles */
.tiles { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); min-width: 0; }
.tile .t-label { font-size: 12px; color: var(--muted); font-weight: 600; }
.tile .t-value { font-size: 22px; font-weight: 650; margin-top: 2px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile .t-value.good { color: var(--good); }
.tile .t-value.bad { color: var(--bad); }
.tile .t-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.meter { height: 6px; border-radius: 999px; background: var(--info-soft); margin-top: 8px; overflow: hidden; }
.meter > span { display: block; height: 100%; width: 0; background: var(--accent); border-radius: inherit; }
.meter.warn > span { background: var(--warn); }
.meter.bad > span { background: var(--bad); }

/* layout */
.columns { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); gap: 16px; margin-bottom: 16px; align-items: start; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; box-shadow: var(--shadow); margin-bottom: 16px; min-width: 0; }
.columns .card { margin-bottom: 0; }
.card > h2, .card-head { margin-bottom: 12px; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.empty { color: var(--faint); font-size: 14px; padding: 6px 0; }

/* strategy control */
.strategy-list { display: grid; gap: 8px; margin-bottom: 14px; }
.strategy-option {
  display: grid; grid-template-columns: auto 1fr; gap: 10px; align-items: start; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 8px; cursor: pointer; background: var(--surface);
  color: var(--text); font-weight: 400;
}
.strategy-option:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.strategy-option input { width: auto; margin: 4px 0 0; }
.strategy-option .s-name { font-weight: 650; }
.strategy-option .s-desc { color: var(--muted); font-size: 13px; }
.params { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin: 12px 0; }
.params .full { grid-column: 1 / -1; }
.budget-field input { font-size: 18px; font-weight: 650; }
.plan { background: var(--surface-2); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; font-size: 13px; color: var(--muted); }
.plan b { color: var(--text); }
.start-row { display: flex; gap: 10px; align-items: center; margin-top: 14px; }
.start-row .btn.primary { flex: 1; padding: 12px; font-size: 16px; }
.blockers { margin: 10px 0 0; padding: 10px 12px 10px 30px; border-radius: 8px; background: var(--warn-soft); color: var(--warn); font-size: 13px; font-weight: 600; }
.run-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.run-head h3 { font-size: 18px; }
.facts { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px 16px; margin: 14px 0; }
.facts .f-label { font-size: 12px; color: var(--muted); font-weight: 600; }
.facts .f-value { font-variant-numeric: tabular-nums; }
.market-list { display: grid; gap: 6px; margin: 8px 0 14px; }
.market-row { display: flex; justify-content: space-between; gap: 10px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; }
.market-row .quote { font-variant-numeric: tabular-nums; color: var(--muted); white-space: nowrap; }
.actions-row { display: flex; gap: 8px; flex-wrap: wrap; }
.last-run { margin-top: 14px; padding: 10px 12px; border-radius: 8px; background: var(--surface-2); border: 1px solid var(--line); font-size: 13px; }
.report { margin-top: 12px; font-size: 13px; }
.report-list { margin: 6px 0; padding-left: 18px; font-variant-numeric: tabular-nums; }
.report-list li { margin: 3px 0; }
.last-run.warn { background: var(--warn-soft); border-color: color-mix(in srgb, var(--warn) 35%, var(--line)); }
details.more { margin-top: 10px; }
details.more summary { cursor: pointer; color: var(--muted); font-size: 13px; font-weight: 600; }

/* checks */
.checks { list-style: none; margin: 0; padding: 0; }
.checks li { display: grid; grid-template-columns: 22px 1fr; gap: 8px; padding: 9px 0; border-bottom: 1px solid var(--line); }
.checks li:last-child { border-bottom: 0; }
.checks .mark { font-weight: 700; text-align: center; color: var(--faint); }
.checks .PASS .mark { color: var(--good); }
.checks .FAIL .mark { color: var(--bad); }
.checks .CHECKING .mark { color: var(--accent); }
.checks .CANCELED .mark { color: var(--faint); }
.checks .c-name { font-weight: 600; }
.checks .NOT_CHECKED .c-name { color: var(--faint); font-weight: 500; }
.checks .c-why { font-size: 13px; color: var(--muted); }
.checks .FAIL .c-why { color: var(--bad); }
.checks ul { margin: 4px 0 0; padding-left: 18px; font-size: 13px; color: var(--muted); }
.spin { display: inline-block; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spin { animation: none; } }

/* lists & tables */
.table-wrap { overflow-x: auto; margin: 0 -4px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 6px 8px; border-bottom: 1px solid var(--line-strong); white-space: nowrap; }
td { padding: 9px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
tr:last-child td { border-bottom: 0; }
td.r, th.r { text-align: right; font-variant-numeric: tabular-nums; }
td .sub { display: block; font-size: 12px; color: var(--muted); }
.side-buy { color: var(--good); font-weight: 650; }
.side-sell { color: var(--bad); font-weight: 650; }
.attention-list { display: grid; gap: 8px; }
.attention-item { padding: 10px 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface-2); }
.attention-item.bad { border-color: color-mix(in srgb, var(--bad) 35%, var(--line)); background: var(--bad-soft); }
.attention-item.warn { border-color: color-mix(in srgb, var(--warn) 35%, var(--line)); background: var(--warn-soft); }
.attention-item .a-title { font-weight: 650; }
.attention-item .a-meta { font-size: 12px; color: var(--muted); }
.attention-item .a-meta.mono { font-family: var(--mono); word-break: break-all; }
.attention-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 10px; }
.transfer-list { display: grid; gap: 4px; font-size: 12px; }
.transfer-list .mono { word-break: break-all; }
.subhead { font-size: 13px; color: var(--muted); font-weight: 650; margin: 14px 0 6px; }
.recon { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
.recon .r-item { padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; }
.recon .r-name { font-size: 12px; color: var(--muted); font-weight: 600; }
.recon .r-status { font-weight: 650; }
.recon .r-status.good { color: var(--good); }
.recon .r-status.warn { color: var(--warn); }
.recon .r-status.bad { color: var(--bad); }
.history-item { border-bottom: 1px solid var(--line); padding: 10px 0; }
.history-item:last-child { border-bottom: 0; }
.history-item summary { cursor: pointer; display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap; list-style: none; }
.history-item summary::-webkit-details-marker { display: none; }
.history-item .h-main { font-weight: 600; }
.history-item .h-meta { font-size: 13px; color: var(--muted); }

/* emergency progress */
.attempts { font-size: 13px; margin-top: 12px; display: grid; gap: 4px; }

/* toast */
.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 50;
  display: flex; align-items: center; gap: 10px; max-width: min(640px, calc(100vw - 32px));
  padding: 10px 12px 10px 16px; border-radius: 10px; background: var(--text); color: var(--bg);
  box-shadow: 0 8px 24px rgba(0,0,0,.18); font-size: 14px;
}
.toast.bad { background: var(--bad); color: #fff; }
.toast.good { background: var(--good); color: #fff; }
.toast.warn { background: var(--warn); color: #fff; }
.toast-close { background: transparent; border: 0; color: inherit; font-size: 18px; cursor: pointer; padding: 0 4px; }

/* dialogs */
dialog {
  border: 1px solid var(--line); border-radius: 14px; padding: 0; width: min(460px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: 0 20px 50px rgba(0,0,0,.25);
}
dialog::backdrop { background: rgba(15, 20, 25, .45); }
dialog form { padding: 22px; display: grid; gap: 12px; }
fieldset { border: 0; margin: 0; padding: 0; display: grid; gap: 8px; }
legend { font-size: 13px; color: var(--muted); font-weight: 600; margin-bottom: 6px; padding: 0; }
.choice { display: grid; grid-template-columns: auto 1fr; gap: 10px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; color: var(--text); font-weight: 400; }
.choice:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.choice input { width: auto; margin: 3px 0 0; }
.choice small { display: block; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
dialog fieldset { border: 0; padding: 0; margin: 0; }

@media (max-width: 1020px) {
  .tiles { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .columns { grid-template-columns: 1fr; }
  .recon { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 640px) {
  main { padding: 12px 16px 56px; }
  .topbar { padding: 8px 16px; flex-wrap: wrap; row-gap: 6px; }
  .topbar-left { width: 100%; justify-content: space-between; }
  .topbar-right { width: 100%; justify-content: space-between; }
  .brand { font-size: 15px; }
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .tile { padding: 10px 12px; }
  .tile .t-value { font-size: 18px; }
  .banner { flex-direction: column; }
  .banner-actions { justify-content: flex-start; }
  .params, .facts, .field-row { grid-template-columns: 1fr; }
  .card { padding: 14px 16px; }
  .login, .emergency { margin: 4vh auto; padding: 22px; }
  .hide-sm { display: none; }
}
