:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #16191d;
  --muted: #5d6670;
  --border: #dfe3e8;
  --accent: #2f5bd3;
  --accent-text: #ffffff;
  --ok: #1f8a4c;
  --warn: #b7791f;
  --bad: #c53030;
  --idle: #8a949e;
  --bar: #2f5bd3;
  --bar-over: #c53030;
  --focus: #2f5bd3;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1215; --surface: #171b20; --surface-2: #1f242a; --text: #e6e9ec; --muted: #9aa4ae;
    --border: #2b3138; --accent: #7a9cff; --accent-text: #0f1215; --ok: #4cc283; --warn: #e0a84a;
    --bad: #f07171; --idle: #6d7781; --bar: #7a9cff; --bar-over: #f07171; --focus: #7a9cff;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1215; --surface: #171b20; --surface-2: #1f242a; --text: #e6e9ec; --muted: #9aa4ae;
  --border: #2b3138; --accent: #7a9cff; --accent-text: #0f1215; --ok: #4cc283; --warn: #e0a84a;
  --bad: #f07171; --idle: #6d7781; --bar: #7a9cff; --bar-over: #f07171; --focus: #7a9cff;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh; display: flex; flex-direction: column;
}
[hidden] { display: none !important; }
a { color: var(--accent); }
h1 { font-size: 1.35rem; margin: 0 0 .5rem; }
h2 { font-size: 1.05rem; margin: 1.75rem 0 .5rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.num { text-align: right; font-variant-numeric: tabular-nums; }

/* top bar */
.topbar {
  display: flex; align-items: center; gap: .75rem; padding: .6rem 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 2;
}
.brand { display: flex; align-items: center; gap: .45rem; font-weight: 650; color: var(--text); text-decoration: none; }
.brand-mark { width: 22px; height: 22px; fill: none; stroke: var(--accent); stroke-width: 1.8; stroke-linejoin: round; }
.env-tag { font-size: .7rem; font-weight: 600; padding: .05rem .4rem; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.topnav { margin-left: auto; display: flex; align-items: center; gap: .35rem; flex-wrap: nowrap; }
.topnav .who { color: var(--muted); font-size: .85rem; margin-right: .25rem; overflow: hidden; text-overflow: ellipsis; max-width: 40vw; white-space: nowrap; }
.icon-btn { padding: .35rem; line-height: 0; }
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; }
.topnav:not([hidden]) + .icon-btn { margin-left: 0; }
.topnav[hidden] + .icon-btn { margin-left: auto; }

main { flex: 1; width: 100%; max-width: 1280px; margin: 0 auto; padding: 20px 16px 32px; }
.foot { text-align: center; padding: 12px 16px 20px; }

/* controls */
button, input { font: inherit; }
button {
  border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: .5rem .9rem; cursor: pointer; min-height: 40px;
}
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.ghost { background: transparent; }
button:disabled { opacity: .6; cursor: progress; }
button:focus-visible, input:focus-visible, a:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
input {
  width: 100%; padding: .6rem .7rem; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); min-height: 44px;
}
label { display: block; font-weight: 600; margin: .75rem 0 .3rem; }
form button { margin-top: .9rem; width: 100%; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; }
.narrow { max-width: 440px; margin: 6vh auto 0; }
.panel > button { width: 100%; margin-top: .6rem; }
.status { min-height: 1.4em; margin: .8rem 0 0; }
.status.error, .status .error { color: var(--bad); }

/* fleet */
.view-head { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; margin-bottom: .75rem; }
.view-head h1 { margin: 0; }
.view-head button { margin-left: auto; }
.summary { display: flex; gap: .5rem; flex-wrap: wrap; }
.chip { font-size: .8rem; padding: .15rem .55rem; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.chip strong { color: var(--text); }

.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: .6rem .7rem; text-align: left; vertical-align: top; border-bottom: 1px solid var(--border); }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; white-space: nowrap; background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
tr.row-link { cursor: pointer; }
tr.row-link:hover td { background: var(--surface-2); }
.agent-name { font-weight: 600; }
.agent-id { font-size: .8rem; color: var(--muted); font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }
.sub { display: block; font-size: .8rem; color: var(--muted); }

.badge { display: inline-block; font-size: .75rem; font-weight: 600; padding: .1rem .5rem; border-radius: 999px; border: 1px solid currentColor; white-space: nowrap; }
.badge.live { color: var(--ok); }
.badge.test { color: var(--accent); }
.badge.planned, .badge.stub, .badge.parked, .badge.unknown { color: var(--idle); }

.hb { white-space: nowrap; }
.hb .dot, .dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: var(--idle); vertical-align: middle; }
.hb .dot { margin-right: .35rem; }
.dot.ok, .hb.fresh .dot { background: var(--ok); }
.dot.late, .hb.stale .dot { background: var(--warn); }
.dot.missed, .dot.failed, .hb.dead .dot { background: var(--bad); }
.dot.unknown { background: transparent; border: 2px solid var(--idle); }
.dots { display: flex; flex-wrap: wrap; gap: 4px; max-width: 120px; }

.cost.over { color: var(--bad); font-weight: 600; }
.cost .target { color: var(--muted); font-size: .8rem; }

.alerts { list-style: none; padding: 0; margin: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.alerts li { padding: .6rem .8rem; border-bottom: 1px solid var(--border); display: flex; gap: .6rem; flex-wrap: wrap; }
.alerts li:last-child { border-bottom: 0; }
.alerts .kind { font-weight: 600; }
.empty { padding: .9rem; color: var(--muted); }

/* agent detail */
.detail-head { display: flex; align-items: baseline; gap: .75rem; flex-wrap: wrap; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; margin: 1rem 0; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.card h3 { margin: 0 0 .35rem; font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
.card .big { font-size: 1.25rem; font-weight: 650; font-variant-numeric: tabular-nums; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: .2rem .8rem; font-size: .9rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.payload { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: .8rem; color: var(--muted); overflow-wrap: anywhere; }
.chart { width: 100%; height: 180px; display: block; }
.chart .bar { fill: var(--bar); }
.chart .bar.over { fill: var(--bar-over); }
.chart .target-line { stroke: var(--warn); stroke-width: 1.5; stroke-dasharray: 4 3; }
.chart .axis { fill: var(--muted); font-size: 10px; }
.chart .grid-line { stroke: var(--border); stroke-width: 1; }
.legend { display: flex; gap: 1rem; font-size: .8rem; color: var(--muted); margin-top: .3rem; }

/* phones: fleet table becomes cards */
@media (max-width: 760px) {
  table.fleet thead { display: none; }
  table.fleet, table.fleet tbody, table.fleet tr, table.fleet td { display: block; width: 100%; }
  table.fleet tr { padding: .7rem .8rem; border-bottom: 1px solid var(--border); display: grid; grid-template-columns: 1fr 1fr; gap: .35rem .8rem; }
  table.fleet tbody tr:last-child { border-bottom: 0; }
  table.fleet td { border: 0; padding: 0; }
  table.fleet td::before { content: attr(data-label); display: block; font-size: .7rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
  table.fleet td.cell-agent { grid-column: 1 / -1; }
  table.fleet td.cell-agent::before { display: none; }
  .num { text-align: left; }
  .topnav .who { display: none; }
  .topnav button { padding: .3rem .55rem; min-height: 36px; font-size: .85rem; white-space: nowrap; }
  .topbar { gap: .4rem; }
  .narrow { margin-top: 2vh; }
}
