/* Mobile-first. Single column by default, wider layout only when there is room.
   No external fonts or CDNs: this must render on a VPS with no outbound access
   and on a phone over a flaky connection.

   Design intent: flat surfaces, one accent colour, no gradients or shadows. The
   numbers are the content, so typography and spacing do the work rather than
   decoration. */

:root {
  --bg: #0b0e14;
  --surface: #131820;
  --surface-2: #1a2130;
  --surface-3: #212a3b;
  --border: #232d3f;
  --border-strong: #30405a;
  --text: #e8eef7;
  --text-dim: #96a3b8;
  --text-faint: #6b7a91;
  --accent: #4c8dff;
  --accent-dim: #2c5aa8;
  --pos: #3fb950;
  --neg: #f85149;
  --warn: #d29922;
  --radius: 10px;
  --radius-sm: 7px;
  --space: 16px;
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  /* Horizontal overflow is always a bug on a dashboard: it hides content off-screen
     with no way to reach it, and on a phone it makes every scroll feel broken. Any
     child that genuinely needs to be wider (a table) gets its own scroll container. */
  overflow-x: hidden;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
  /* Long unbroken strings - error messages, URLs, rule names - must wrap rather than
     push the layout wide. This single rule prevents a whole class of phone bug. */
  overflow-wrap: break-word;
}

/* Images and canvases must never exceed their container. */
img, svg, video, canvas { max-width: 100%; height: auto; }

/* ---- top bar ----
   On a phone the seven nav links do not fit beside the brand. The bar used to wrap
   them onto a second and third row, which made the sticky header eat a fifth of the
   screen before any content appeared. Below 760px the links become one horizontally
   scrollable row instead: every destination stays reachable with a thumb swipe, the
   header stays one line tall, and nothing is ever hidden. */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  gap: 0;
  padding: 0;
  background: rgba(11, 14, 20, 0.96);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(12px);
}

.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  height: 52px;
  padding: 0 var(--space);
  min-width: 0;
}

.brand {
  font-weight: 700;
  font-size: 16px;
  color: var(--text);
  text-decoration: none;
  letter-spacing: -0.3px;
  flex-shrink: 0;
  white-space: nowrap;
}
.brand span { color: var(--accent); }

.topbar nav {
  display: flex;
  gap: 2px;
  align-items: center;
  /* One row that scrolls sideways rather than wrapping to multiple lines. The
     negative margin lets the row bleed to the screen edge so the last item is not
     visually clipped by the page padding. */
  flex-wrap: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
  margin: 0 calc(-1 * var(--space));
  padding: 0 var(--space) 8px;
}
.topbar nav::-webkit-scrollbar { display: none; }

.topbar nav a {
  color: var(--text-dim);
  text-decoration: none;
  font-size: 13px;
  font-weight: 500;
  padding: 7px 11px;
  border-radius: var(--radius-sm);
  transition: background 0.12s, color 0.12s;
  white-space: nowrap;
  flex-shrink: 0;
}
.topbar nav a:hover { color: var(--text); background: var(--surface-2); }
.topbar nav a.active { color: var(--text); background: var(--surface-3); }
.topbar nav a.logout { color: var(--neg); }
.topbar nav a.logout:hover { background: rgba(248, 81, 73, 0.12); }

main { padding: var(--space); max-width: 1180px; margin: 0 auto; }

/* ---- headings ---- */
.page-head { margin: 0 0 20px; }
.page-head h1 {
  font-size: 21px;
  margin: 0 0 3px;
  letter-spacing: -0.4px;
  font-weight: 650;
}
.page-head p { margin: 0; color: var(--text-dim); font-size: 13.5px; }

h2 {
  font-size: 11.5px;
  margin: 26px 0 11px;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 1.1px;
  font-weight: 600;
}
h2:first-of-type { margin-top: 0; }

h3 { font-size: 15px; margin: 0; font-weight: 600; letter-spacing: -0.2px; }

.muted { color: var(--text-dim); }
.faint { color: var(--text-faint); }
.small { font-size: 12.5px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.warn { color: var(--warn); }

/* ---- stat strip ---- */
.stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 9px;
  margin-bottom: 6px;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 13px 14px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
}
.stat .label {
  font-size: 10.5px;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.8px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.stat .value {
  font-size: 19px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.5px;
  line-height: 1.15;
}
.stat .value.sm { font-size: 15px; }
.stat .sub { font-size: 11.5px; color: var(--text-faint); }

/* A tinted stat draws the eye to the one figure that matters most. */
.stat.highlight {
  background: var(--surface-2);
  border-color: var(--border-strong);
}

/* ---- cards ---- */
.cards { display: grid; grid-template-columns: 1fr; gap: 11px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 15px;
}
.card header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 9px;
}
.card p { margin: 0 0 11px; font-size: 13.5px; color: var(--text-dim); line-height: 1.55; }
.card dl { margin: 0; display: grid; grid-template-columns: 92px 1fr; gap: 5px 12px; font-size: 12.5px; }
.card dt { color: var(--text-faint); }
.card dd { margin: 0; color: var(--text-dim); }

/* ---- panels (bordered sections with a title bar) ---- */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 11px;
}
.panel > header {
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}
.panel > header strong {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-dim);
  font-weight: 600;
}
.panel > .body { padding: 13px 14px; }

/* ---- banners ---- */
.banner {
  padding: 11px 13px;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  margin: 0 0 14px;
  border: 1px solid var(--border);
  background: var(--surface);
  line-height: 1.5;
}
.banner.error {
  border-color: rgba(248, 81, 73, 0.45);
  background: rgba(248, 81, 73, 0.09);
  color: #ffb8b3;
}
.banner.warn {
  border-color: rgba(210, 153, 34, 0.45);
  background: rgba(210, 153, 34, 0.09);
  color: #f0c674;
}
.banner.ok {
  border-color: rgba(63, 185, 80, 0.4);
  background: rgba(63, 185, 80, 0.08);
  color: #9be3a5;
}
.banner strong { display: block; margin-bottom: 2px; }

/* ---- forms ---- */
.actions { margin: 0 0 4px; }
.actions p { margin: 0 0 11px; color: var(--text-dim); font-size: 13px; }

form.inline {
  display: flex;
  gap: 9px;
  flex-wrap: wrap;
  align-items: flex-end;
  margin-bottom: 10px;
}

label { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 5px; }

input, select, button {
  font-size: 14.5px;
  font-family: inherit;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface-2);
  color: var(--text);
  padding: 9px 11px;
  /* 40px min height keeps tap targets comfortable on a phone. */
  min-height: 40px;
  transition: border-color 0.12s, background 0.12s;
}

input:hover, select:hover { border-color: var(--accent-dim); }

input:focus, select:focus, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  border-color: var(--accent);
}

input::placeholder { color: var(--text-faint); }

button {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
button:hover { filter: brightness(1.1); }
button:active { filter: brightness(0.95); }
button:disabled { opacity: 0.45; cursor: not-allowed; filter: none; }

button.secondary {
  background: var(--surface-3);
  border-color: var(--border-strong);
  color: var(--text);
}
button.secondary:hover { background: var(--surface-2); filter: none; }

button.danger-btn {
  background: var(--neg);
  border-color: var(--neg);
  color: #fff;
}
button.danger-btn:hover { filter: brightness(1.1); }

form.inline label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin: 0;
  font-size: 11px;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  font-weight: 600;
}
form.inline label input,
form.inline label select {
  text-transform: none;
  letter-spacing: 0;
  font-size: 14.5px;
  min-width: 118px;
  max-width: 100%;
}

/* Below 560px a horizontal control row wastes half the width on gaps and forces
   labels into a cramped column. Stacking gives every field the full width, which is
   what makes a form usable with a thumb. */
@media (max-width: 559px) {
  .controls,
  form.inline {
    flex-direction: column;
    align-items: stretch;
  }
  .controls label,
  form.inline label {
    width: 100%;
  }
  .controls label input,
  .controls label select,
  form.inline label input,
  form.inline label select {
    width: 100%;
    min-width: 0;
  }
  .controls button,
  form.inline button {
    width: 100%;
  }
}

/* ---- list rows ---- */
.list {
  display: flex;
  flex-direction: column;
  gap: 1px;
  background: var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  padding: 11px 13px;
}
.row:hover { background: var(--surface-2); }
.row-main { display: flex; flex-direction: column; min-width: 0; gap: 1px; }
.row-main strong { font-size: 13.5px; font-weight: 600; }
.row-side { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; flex-shrink: 0; }

/* ---- pills ---- */
.pill {
  font-size: 10.5px;
  font-weight: 700;
  padding: 2.5px 8px;
  border-radius: 999px;
  background: var(--surface-3);
  border: 1px solid var(--border-strong);
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  white-space: nowrap;
}
.pill.pos { color: var(--pos); border-color: rgba(63, 185, 80, 0.4); background: rgba(63, 185, 80, 0.1); }
.pill.neg { color: var(--neg); border-color: rgba(248, 81, 73, 0.4); background: rgba(248, 81, 73, 0.1); }
.pill.warn { color: var(--warn); border-color: rgba(210, 153, 34, 0.4); background: rgba(210, 153, 34, 0.1); }

/* ---- health checks ---- */
.checks {
  display: flex;
  flex-direction: column;
  gap: 1px;
  background: var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.check {
  display: flex;
  align-items: center;
  gap: 11px;
  background: var(--surface);
  padding: 12px 14px;
}
.check div { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.check strong { font-size: 13.5px; font-weight: 600; }
.check .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-faint);
  flex-shrink: 0;
}
.check.ok .dot { background: var(--pos); box-shadow: 0 0 0 3px rgba(63, 185, 80, 0.14); }
.check.bad { background: rgba(248, 81, 73, 0.05); }
.check.bad .dot { background: var(--neg); box-shadow: 0 0 0 3px rgba(248, 81, 73, 0.16); }

/* ---- log ---- */
.log {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 9px 11px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11.5px;
  line-height: 1.65;
  max-height: 320px;
  overflow-y: auto;
}
.log-line { display: flex; gap: 8px; padding: 2px 0; }
.log-line + .log-line { border-top: 1px solid rgba(35, 45, 63, 0.55); padding-top: 4px; margin-top: 2px; }
.lvl { flex-shrink: 0; width: 42px; font-weight: 700; color: var(--text-faint); }
.lvl.info { color: var(--accent); }
.lvl.warn { color: var(--warn); }
.lvl.error { color: var(--neg); }
.src { flex-shrink: 0; width: 76px; color: var(--text-faint); overflow: hidden; text-overflow: ellipsis; }
.msg { flex: 1; word-break: break-word; color: var(--text-dim); }
.ts { flex-shrink: 0; color: var(--text-faint); font-size: 10.5px; }

/* ---- tables ---- */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  /* A table wider than its container must scroll inside this box. Without
     max-width the wrap itself grows and takes the whole page with it. */
  max-width: 100%;
}
table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th {
  text-align: left;
  padding: 9px 12px;
  background: var(--surface-2);
  color: var(--text-faint);
  font-weight: 600;
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  white-space: nowrap;
  border-bottom: 1px solid var(--border);
}
tbody td {
  padding: 9px 12px;
  border-top: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
  color: var(--text-dim);
}
tbody td:first-child { color: var(--text); }
tbody tr:hover td { background: var(--surface-2); }

/* ---- empty states ---- */
.empty {
  padding: 26px 18px;
  text-align: center;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  color: var(--text-faint);
  font-size: 13px;
  background: rgba(19, 24, 32, 0.5);
}
.empty strong { display: block; color: var(--text-dim); margin-bottom: 3px; font-weight: 600; }

/* ---- login ---- */
body.centered { display: grid; place-items: center; min-height: 100vh; padding: 20px; }
.login-card {
  width: 100%;
  max-width: 348px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 13px;
  padding: 28px 22px;
}
.login-card h1 { margin: 0 0 4px; font-size: 22px; letter-spacing: -0.6px; font-weight: 700; }
.login-card h1 span { color: var(--accent); }
.login-card p { margin: 0 0 18px; font-size: 13px; color: var(--text-dim); }
.login-card form { display: flex; flex-direction: column; gap: 11px; }
.login-card button { width: 100%; min-height: 42px; }

/* ---- controls row ---- */
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 9px;
  align-items: center;
}
.controls form { display: contents; }

/* ---- footer ---- */
footer {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 22px var(--space) 34px;
  max-width: 1180px;
  margin: 0 auto;
  font-size: 11.5px;
  color: var(--text-faint);
  border-top: 1px solid var(--border);
  line-height: 1.6;
}

.foot-note { margin-top: 22px; }

/* ---- wider screens ---- */
@media (min-width: 560px) {
  .stats { grid-template-columns: repeat(3, 1fr); }
}

@media (min-width: 760px) {
  main { padding: 26px; }
  .page-head h1 { font-size: 25px; }
  .stats { grid-template-columns: repeat(6, 1fr); }
  .cards { grid-template-columns: repeat(2, 1fr); }
  footer { flex-direction: row; justify-content: space-between; }

  /* Restore the single-row header with the nav beside the brand once there is
     room for it. Below this width the nav stays a scrollable row. */
  .topbar {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    height: 56px;
    padding: 0 var(--space);
  }
  .topbar-inner {
    height: auto;
    padding: 0;
    flex: 1;
    justify-content: space-between;
  }
  .topbar nav {
    margin: 0;
    padding: 0;
    overflow: visible;
    flex-wrap: wrap;
  }
}

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

/* ---- background job card ---- */
.job-card {
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 13px 14px;
}

.job-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 11px;
}

.job-title { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.job-title strong { font-size: 13.5px; font-weight: 600; }

.job-timing {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 1px;
  flex-shrink: 0;
}
.job-clock {
  font-size: 15px;
  font-weight: 650;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.3px;
}

.progress-track {
  height: 6px;
  background: var(--bg);
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 10px;
}
.progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 999px;
  transition: width 0.4s ease;
  min-width: 2px;
}

.job-foot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

@media (min-width: 600px) {
  .job-head { flex-wrap: nowrap; }
}

/* ===========================================================================
   Condition builder (Custom page)
   ===========================================================================
   The rule table is four controls wide: a field select, an operator select, a value
   input and a remove button. That needs roughly 480px at its narrowest, so on any
   phone it either overflowed the page or squeezed the selects until the option text
   was unreadable.

   Below 620px the table reflows into one card per condition. Each card shows the
   column name above its control, which is information the table conveyed with a
   header row that no longer exists at this width - so nothing is lost, it moves.
   The alternative would be a horizontally scrolling form, which is genuinely awkward
   to fill in with a thumb. */

.cond-cards { display: none; }

@media (max-width: 619px) {
  /* Hide the table, show the card list. */
  #condTable { display: none; }
  .cond-cards { display: flex; flex-direction: column; gap: 9px; }

  .cond-card {
    background: var(--surface-2);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    padding: 11px 12px;
    display: grid;
    gap: 9px;
  }
  .cond-card .cond-num {
    font-size: 10.5px;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--text-faint);
    font-weight: 600;
  }
  .cond-card label {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 10.5px;
    text-transform: uppercase;
    letter-spacing: 0.7px;
    color: var(--text-faint);
    font-weight: 600;
    margin: 0;
  }
  .cond-card select,
  .cond-card input {
    width: 100%;
    min-width: 0;
    font-size: 14px;
    text-transform: none;
    letter-spacing: 0;
  }
  .cond-card .remove {
    width: 100%;
    min-height: 36px;
    padding: 6px 10px;
    font-size: 13px;
  }
}

/* Very narrow phones: shrink the stat strip rather than let a value clip. */
@media (max-width: 359px) {
  :root { --space: 12px; }
  .stat .value { font-size: 17px; }
  .stat { padding: 11px 12px; }
  .topbar nav a { font-size: 12.5px; padding: 6px 9px; }
}

/* The job card stacks its timing under the title on narrow screens so the clock
   never pushes the stage text off the edge. */
@media (max-width: 460px) {
  .job-head {
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
  }
  .job-timing {
    flex-direction: row;
    align-items: baseline;
    justify-content: space-between;
    gap: 8px;
  }
  .job-foot {
    flex-direction: column;
    align-items: stretch;
    gap: 9px;
  }
  .job-foot button { width: 100%; }
}

/* Log lines: the fixed 42px level and 76px source columns plus a timestamp leave
   almost nothing for the message on a phone. Stack them instead. */
@media (max-width: 559px) {
  .log { font-size: 11px; padding: 8px 9px; }
  .log-line {
    flex-wrap: wrap;
    gap: 4px 8px;
    padding: 6px 0;
  }
  .lvl { width: auto; }
  .src { width: auto; max-width: 45%; }
  .ts { margin-left: auto; }
  .msg { flex: 1 0 100%; }
}

/* Long single-token strings in a table cell (option symbols, error codes) must
   wrap rather than widen the column indefinitely. */
tbody td, .row-main strong, .row-side span { overflow-wrap: anywhere; }

/* Panels and cards never exceed the viewport. */
.panel, .card, .stat, .banner, .job-card, .table-wrap { max-width: 100%; }

/* Code inside a banner (a CLI command) must wrap on a phone or it stretches the
   banner past the screen. */
.banner code, .panel code, p code {
  overflow-wrap: anywhere;
  word-break: break-word;
  font-size: 12px;
}

/* Error text in a run row. The old inline max-width:220px assumed a wide screen; at
   375px that leaves the message narrower than the pill beside it and clips mid-word.
   Let it take the available width and wrap instead. */
.row-side .error-text {
  display: block;
  max-width: 100%;
  overflow-wrap: anywhere;
  text-align: right;
}
@media (max-width: 559px) {
  /* Row side content stacks under the main content on a phone, so right alignment
     no longer makes sense - everything reads left to right in one column. */
  .row {
    flex-direction: column;
    align-items: flex-start;
    gap: 5px;
  }
  .row-side {
    align-items: flex-start;
    width: 100%;
  }
  .row-side .error-text { text-align: left; }
}

/* A horizontally scrollable nav is only usable if the user can tell there is more
   to the right. This fade sits at the edge and hints at it without stealing a tap
   target; it disappears on wide screens where the nav no longer scrolls. */
.topbar { position: relative; }
.topbar::after {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 26px;
  pointer-events: none;
  background: linear-gradient(to right, rgba(11, 14, 20, 0), rgba(11, 14, 20, 0.95));
}
@media (min-width: 760px) {
  .topbar::after { display: none; }
}

/* An anchor wrapping a button was used in a couple of places. The anchor is inline
   and the button is inline-block, so on a narrow screen the two stack unevenly and
   the tap target of the anchor does not match the visible button. Making the anchor
   itself the flex item fixes the alignment and the tap area together. */
.controls > a {
  display: inline-flex;
  text-decoration: none;
  flex: 1 1 auto;
}
.controls > a > button { width: 100%; }

@media (max-width: 559px) {
  .controls > a { flex: 1 1 100%; }
}
