/* Central Cockpit console.
 *
 * One stylesheet, no framework. The interface is dense tables and short forms;
 * a framework would ship far more than that costs to write.
 *
 * Colours are defined once as tokens and redefined for a dark preference, so
 * the console follows whatever the operator's machine already does rather than
 * asking them to choose again.
 */

:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --border: #dfe3e8;
  --text: #16191d;
  --muted: #656d76;
  --accent: #1a56b8;
  --accent-text: #ffffff;
  --ok: #146c2e;
  --ok-bg: #e6f4ea;
  --warn: #8a5300;
  --warn-bg: #fdf3e2;
  --bad: #a3161d;
  --bad-bg: #fceceb;
  --shadow: 0 1px 2px rgba(16, 22, 26, 0.07);
  --radius: 6px;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171a;
    --panel: #1c2024;
    --border: #2e343a;
    --text: #e6e9ec;
    --muted: #9aa4ae;
    --accent: #4c8dff;
    --accent-text: #0b1016;
    --ok: #63d089;
    --ok-bg: #16281c;
    --warn: #e0a94b;
    --warn-bg: #2b2317;
    --bad: #f28b82;
    --bad-bg: #2c1a1a;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------------------------------------------------------------- layout */

header.top {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 0 1.25rem;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

header.top .brand {
  font-weight: 650;
  letter-spacing: -0.01em;
  color: var(--text);
  padding: 0.9rem 0;
  white-space: nowrap;
}

header.top nav {
  display: flex;
  gap: 0.25rem;
  flex: 1;
  overflow-x: auto;
}

header.top nav a {
  padding: 0.9rem 0.75rem;
  color: var(--muted);
  border-bottom: 2px solid transparent;
  white-space: nowrap;
}

header.top nav a:hover { color: var(--text); text-decoration: none; }

header.top nav a.on {
  color: var(--text);
  border-bottom-color: var(--accent);
  font-weight: 550;
}

header.top .who {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  color: var(--muted);
  font-size: 0.875rem;
  white-space: nowrap;
}

main {
  max-width: 1180px;
  margin: 0 auto;
  padding: 1.5rem 1.25rem 4rem;
}

h1 {
  font-size: 1.4rem;
  margin: 0 0 1.25rem;
  letter-spacing: -0.015em;
}

h2 {
  font-size: 1.05rem;
  margin: 0 0 0.75rem;
  letter-spacing: -0.01em;
}

.head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 1.25rem;
}

.head h1 { margin: 0; }

/* ---------------------------------------------------------------- panels */

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 1.25rem;
  overflow: hidden;
}

.panel > h2 {
  padding: 0.75rem 1rem;
  margin: 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.panel .body { padding: 1rem; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 1rem;
  margin-bottom: 1.25rem;
}

.stat {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.9rem 1rem;
  box-shadow: var(--shadow);
}

.stat .n {
  font-size: 1.9rem;
  font-weight: 600;
  letter-spacing: -0.03em;
  line-height: 1.1;
}

.stat .k {
  color: var(--muted);
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-top: 0.2rem;
}

.stat.bad .n { color: var(--bad); }
.stat.warn .n { color: var(--warn); }

/* ---------------------------------------------------------------- tables */

table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }

/* A wide table scrolls inside its own panel rather than pushing the page
   sideways. The fleet listing has grown to ten columns; without this the last
   of them is cut off the right of the window and the whole page scrolls to
   reach it, taking the navigation with it. */
.panel:has(> table),
.panel .body:has(> table) { overflow-x: auto; }

/* Values that mean nothing broken across lines. "Windows Server 2016 Standard
   1607" wrapped to three lines makes a row three times as tall and no easier
   to read; letting it size itself and scrolling the table is better. */
td.nowrap, th { white-space: nowrap; }

/* Columns that earn their place at a glance but not at every width. Hidden
   rather than shrunk: a column squeezed to four characters is worse than one
   that is not there, and everything hidden here is on the machine's own
   page. */
@media (max-width: 1400px) {
  .fleet .col-arch { display: none; }
}
@media (max-width: 1200px) {
  .fleet .col-runs-on { display: none; }
}
@media (max-width: 1000px) {
  .fleet .col-groups { display: none; }
}

th {
  text-align: left;
  padding: 0.55rem 1rem;
  color: var(--muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

td {
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

tr:last-child td { border-bottom: none; }
tbody tr:hover { background: color-mix(in srgb, var(--accent) 5%, transparent); }

td.num { text-align: right; font-variant-numeric: tabular-nums; }
td.mono, .mono { font-family: var(--mono); font-size: 0.85em; }

.empty {
  padding: 2rem 1rem;
  text-align: center;
  color: var(--muted);
}

/* ---------------------------------------------------------------- pills */

.pill {
  display: inline-block;
  padding: 0.12rem 0.5rem;
  border-radius: 100px;
  font-size: 0.75rem;
  font-weight: 600;
  white-space: nowrap;
}

.pill.ok { background: var(--ok-bg); color: var(--ok); }
.pill.warn { background: var(--warn-bg); color: var(--warn); }
.pill.bad { background: var(--bad-bg); color: var(--bad); }
.pill.flat { background: color-mix(in srgb, var(--muted) 15%, transparent); color: var(--muted); }

/* ---------------------------------------------------------------- forms */

form.inline { display: inline; }

label {
  display: block;
  margin-bottom: 0.25rem;
  font-size: 0.85rem;
  font-weight: 550;
}

label .hint {
  display: block;
  font-weight: 400;
  color: var(--muted);
  font-size: 0.8rem;
  margin-top: 0.1rem;
}

input[type="text"], input[type="email"], input[type="password"], input[type="url"], select {
  width: 100%;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
}

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

.field { margin-bottom: 0.9rem; }

.row {
  display: flex;
  gap: 0.9rem;
  flex-wrap: wrap;
}

.row > .field { flex: 1; min-width: 190px; }

.check {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  margin-bottom: 0.9rem;
}

.check input { margin-top: 0.25rem; }
.check label { margin: 0; }

button, .button {
  display: inline-block;
  padding: 0.45rem 0.85rem;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-text);
  font: inherit;
  font-size: 0.875rem;
  font-weight: 550;
  cursor: pointer;
}

button:hover, .button:hover { filter: brightness(1.08); text-decoration: none; }

button.quiet, .button.quiet {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}

button.danger {
  background: transparent;
  color: var(--bad);
  border-color: color-mix(in srgb, var(--bad) 40%, transparent);
}

button.link {
  background: none;
  border: none;
  color: var(--accent);
  padding: 0;
  font-size: inherit;
}

button.link:hover { text-decoration: underline; }

/* ---------------------------------------------------------------- notices */

.notice {
  padding: 0.7rem 1rem;
  border-radius: var(--radius);
  margin-bottom: 1.25rem;
  border: 1px solid;
  font-size: 0.9rem;
}

.notice.ok { background: var(--ok-bg); color: var(--ok); border-color: color-mix(in srgb, var(--ok) 30%, transparent); }
.notice.bad { background: var(--bad-bg); color: var(--bad); border-color: color-mix(in srgb, var(--bad) 30%, transparent); }
.notice.warn { background: var(--warn-bg); color: var(--warn); border-color: color-mix(in srgb, var(--warn) 30%, transparent); }

/* A notice that carries the action it is asking for, rather than describing a
   button somewhere else on the page. */
.notice { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.notice form { margin: 0; }

/* ---------------------------------------------------------------- login */

.login {
  max-width: 380px;
  margin: 8vh auto;
  padding: 0 1.25rem;
}

.login .brand {
  text-align: center;
  font-size: 1.25rem;
  font-weight: 650;
  margin-bottom: 1.5rem;
  letter-spacing: -0.015em;
}

.login .panel .body { padding: 1.25rem; }
.login button { width: 100%; margin-top: 0.4rem; }

/* ---------------------------------------------------------------- misc */

pre.command {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.8rem;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 0.85rem;
  margin: 0.5rem 0;
  white-space: pre-wrap;
  word-break: break-all;
}

pre.log {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.8rem;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 0.82rem;
  line-height: 1.45;
  margin: 0;
  max-height: 32rem;
  overflow-y: auto;
}

dl.facts {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.4rem 1.25rem;
  margin: 0;
  font-size: 0.9rem;
}

dl.facts dt { color: var(--muted); }
dl.facts dd { margin: 0; }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.actions { display: flex; gap: 0.5rem; flex-wrap: wrap; }

/* ---------------------------------------------------------- tiled consoles */

/* The tiled view fills the screen and never scrolls as a page: every console
 * has to be visible at once, which is the whole point of watching several
 * machines together. Each log scrolls inside its own tile instead. */

body.tiled {
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

body.tiled main {
  flex: 1;
  min-height: 0;
  max-width: none;
  display: flex;
  flex-direction: column;
  padding: 0.75rem 1rem 1rem;
}

.run-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 0.75rem;
}

.run-head h1 { margin: 0; font-size: 1.15rem; }
.run-head p { margin: 0.15rem 0 0; }

.tiles {
  flex: 1;
  min-height: 0;
  display: grid;
  gap: 0.75rem;
  /* Columns and rows are set from the tile count, so one machine gets the
   * whole screen, two split it, and so on. */
  grid-template-columns: repeat(var(--cols, 1), minmax(0, 1fr));
  grid-template-rows: repeat(var(--rows, 1), minmax(0, 1fr));
}

.tile {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.tile header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
  flex: none;
}

.tile header .name {
  font-weight: 600;
  font-size: 0.9rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tile .log {
  flex: 1;
  min-height: 0;
  margin: 0;
  border: none;
  border-radius: 0;
  background: transparent;
  overflow-y: auto;
  font-size: 0.78rem;
  line-height: 1.4;
  padding: 0.6rem 0.75rem;
}

.tile-note {
  padding: 0.6rem 0.75rem;
  font-size: 0.82rem;
  flex: none;
}

.tile-note.bad { color: var(--bad); }

.tile footer {
  padding: 0.4rem 0.75rem;
  border-top: 1px solid var(--border);
  flex: none;
}

/* A tile finished badly is worth spotting without reading its log. */
.tile.failed { border-color: color-mix(in srgb, var(--bad) 55%, var(--border)); }
.tile.succeeded header { background: color-mix(in srgb, var(--ok) 8%, transparent); }
.tile.failed header { background: color-mix(in srgb, var(--bad) 10%, transparent); }

/* On a narrow screen the tiles cannot all be readable at once, so they become
 * one column and the page scrolls — a legible list beats an unreadable grid. */
@media (max-width: 820px) {
  body.tiled { height: auto; overflow: auto; }
  body.tiled main { display: block; }
  .tiles {
    display: block;
  }
  .tile {
    height: 60vh;
    margin-bottom: 0.75rem;
  }
}

/* ------------------------------------------------------------- selection */

.select-bar {
  position: sticky;
  bottom: 0;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  padding: 0.75rem 1rem;
  margin-top: 1rem;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 -2px 10px rgba(16, 22, 26, 0.12);
}

.select-bar[hidden] { display: none; }
.select-bar .count { font-weight: 600; }
.select-bar form { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; }

td.pick, th.pick { width: 1%; padding-right: 0; }

/* Each action says what it does on this machine. The same button means
 * different things on Debian and Windows, and a row of bare labels hides
 * that. */
.action-list { display: flex; flex-direction: column; gap: 0.6rem; }

.action {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.action button { flex: none; min-width: 12rem; text-align: left; }
.action .small { flex: 1; min-width: 16rem; }

/* The filter row wraps rather than scrolling: a fleet is filtered by several
 * things at once, and a control that has scrolled out of sight is a control
 * nobody uses. */
.filters {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
  align-items: flex-end;
}

.filters .field { margin: 0; min-width: 9rem; flex: 1 1 9rem; }
.filters .field.actions-field { flex: 0 0 auto; display: flex; gap: 0.5rem; align-items: center; }
.filters .check-inline {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding-bottom: 0.5rem;
}
.filters .check-inline label { margin: 0; }

.select-bar .group-move { display: flex; gap: 0.4rem; align-items: center; }
.select-bar .group-move input { width: 11rem; }

a.button.link {
  background: none;
  border: none;
  color: var(--accent);
  padding: 0.45rem 0.2rem;
}

/* ------------------------------------------------------------------ sheets */

/* A dialog for work that is too big for a table cell. Sixteen permissions
   across several groups is more than a row can hold, and a row that grows to
   forty lines makes the list of people unreadable to answer a question nobody
   asked. */
dialog.sheet {
  width: min(46rem, 92vw);
  max-height: 88vh;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--fg);
  overflow: hidden;
}

dialog.sheet::backdrop { background: rgba(0, 0, 0, 0.55); }

/* Shown as an anchor target when script has not run. The link's href points at
   the dialog, so the forms inside stay reachable rather than sealed behind a
   button that does nothing. */
dialog.sheet:target {
  display: block;
  position: fixed;
  inset: 4vh auto auto 50%;
  transform: translateX(-50%);
  z-index: 50;
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.9rem 1.1rem;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--panel);
}

.sheet-head h2 { margin: 0; font-size: 1rem; }

/* The body scrolls, not the page behind it. */
.sheet-body {
  padding: 1.1rem;
  overflow-y: auto;
  max-height: calc(88vh - 3.5rem);
  display: grid;
  gap: 1.25rem;
}

.sheet-body form {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.9rem;
}

.sheet-body h3 {
  margin: 0 0 0.2rem;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.sheet-body p { margin: 0 0 0.6rem; }
.sheet-body button { margin-top: 0.7rem; }
.sheet-body input[type="text"] { margin-bottom: 0.6rem; }

/* One permission per line with its explanation beside it, rather than a dense
   grid. A permission somebody cannot read the meaning of is granted by ticking
   everything, which is how fine-grained permission ends up worse than none. */
.perm {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  padding: 0.2rem 0;
  cursor: pointer;
}

.perm input { margin: 0; flex: none; }
