:root {
  --bg: #12141a;
  --bg-elevated: #1b1e26;
  --bg-tile-empty: #262a35;
  --fg: #e8e9ee;
  --fg-muted: #8b909f;
  --border: #2c303c;
  --accent: #4c8dff;
  --accent-fg: #ffffff;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.45);

  --c-dead: #8a5a5a;
  --c-downtime: #5c1c1c;
  --c-down: #b3261e;
  --c-healthcheck: #d1453d;
  --c-stopped: #e0777a;
  --c-offline: #e0c341;
  --c-unknown: #9a4fce;
  --c-empty: #22c55e;
  --c-full: #2f5ce0;
  --c-draining: #17a672;
  --c-fill-empty: #2b3040;

  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg: #f4f5f8;
  --bg-elevated: #ffffff;
  --bg-tile-empty: #e7e9ef;
  --fg: #1a1c22;
  --fg-muted: #5b606e;
  --border: #d9dce3;
  --shadow: 0 8px 24px rgba(20, 20, 30, 0.12);
  --c-fill-empty: #dfe2ea;
  color-scheme: light;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f4f5f8;
    --bg-elevated: #ffffff;
    --bg-tile-empty: #e7e9ef;
    --fg: #1a1c22;
    --fg-muted: #5b606e;
    --border: #d9dce3;
    --shadow: 0 8px 24px rgba(20, 20, 30, 0.12);
    --c-fill-empty: #dfe2ea;
    color-scheme: light;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0 1.25rem 3rem;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
}

a {
  color: inherit;
}

.page-header {
  position: sticky;
  top: 0;
  z-index: 6;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.75rem;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 1rem 0 0.75rem;
}

.topbar-title {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
}

.topbar-title h1 {
  font-size: 1.3rem;
  margin: 0;
}

.timestamp {
  color: var(--fg-muted);
  font-size: 0.8rem;
}

.topbar-controls {
  display: flex;
  gap: 0.5rem;
}

#filter-input {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--fg);
  border-radius: 8px;
  padding: 0.45rem 0.7rem;
  font-size: 0.85rem;
  min-width: 220px;
}

#theme-toggle {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--fg);
  border-radius: 8px;
  padding: 0.45rem 0.6rem;
  cursor: pointer;
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  padding: 0.75rem 0;
}

.chip {
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--fg);
  border-radius: 999px;
  padding: 0.25rem 0.7rem;
  font-size: 0.78rem;
  cursor: pointer;
  user-select: none;
}

.chip.active {
  border-color: var(--accent);
  color: var(--accent-fg);
  background: var(--accent);
}

.summary {
  display: flex;
  flex-wrap: wrap;
  gap: 1.25rem;
  margin: 0.5rem 0 1rem;
  font-size: 0.85rem;
  color: var(--fg-muted);
}

.summary strong {
  color: var(--fg);
}

.fairshare-bar {
  display: flex;
  height: 8px;
  width: 100%;
  max-width: 480px;
  border-radius: 999px;
  overflow: hidden;
  margin-top: 0.35rem;
  background: var(--c-fill-empty);
}

.fairshare-bar.real {
  height: 5px;
  margin-top: 0.25rem;
  opacity: 0.7;
}

.fairshare-bar span {
  height: 100%;
}

.fairshare-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin-top: 0.35rem;
  font-size: 0.75rem;
}

.fairshare-legend .dot {
  display: inline-block;
  width: 0.6rem;
  height: 0.6rem;
  border-radius: 50%;
  margin-right: 0.3rem;
  vertical-align: middle;
}

.overall {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.6rem 0.85rem;
  margin-bottom: 0.75rem;
}

.overall .cluster-head {
  cursor: default;
}

.overall .cluster-util {
  height: 8px;
  margin-bottom: 0;
}

section.farm-section {
  margin-top: 1.25rem;
}

.section-header {
  position: sticky;
  top: var(--topbar-h, 64px);
  z-index: 4;
  background: var(--bg);
  padding: 0.3rem 0 0.6rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.75rem;
}

section.farm-section > .section-header > h2 {
  font-size: 1.05rem;
  margin: 0 0 0.15rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.stale-badge {
  background: var(--c-down);
  color: white;
  font-size: 0.7rem;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
}

.clusters {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 0.85rem;
  margin-top: 0.75rem;
}

.cluster-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.75rem 0.85rem 0.9rem;
}

.cluster-card.dimmed {
  opacity: 0.35;
}

.cluster-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.5rem;
  margin-bottom: 0.55rem;
  cursor: pointer;
}

.cluster-head .name {
  font-weight: 600;
}

.cluster-head .stats {
  font-size: 0.75rem;
  color: var(--fg-muted);
  text-align: right;
}

.cluster-util {
  display: flex;
  height: 5px;
  background: var(--c-fill-empty);
  border: 1px solid var(--border);
  border-radius: 999px;
  margin-bottom: 0.6rem;
  overflow: hidden;
}

.cluster-util span {
  display: block;
  height: 100%;
}

.cluster-util .seg-occupied {
  background: var(--accent);
}

.cluster-util .seg-down {
  background: var(--c-down);
}

.cluster-util .seg-offline {
  background: var(--c-offline);
}

.node-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  gap: 0.3rem;
}

.tile {
  position: relative;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  height: 34px;
  border-radius: 6px;
  font-size: 0.64rem;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  overflow: hidden;
  /* Always-visible (not transparent) so each tile's own boundary is
     unambiguous — otherwise a thin occupancy sliver at the bottom of one
     tile, sitting right next to the small grid-gap, reads as if it belongs
     to the tile below it instead. */
  border: 1px solid var(--border);
  background: var(--bg-tile-empty);
  color: var(--fg);
  padding-bottom: 2px;
  transition: transform 0.08s ease;
}

.tile:hover {
  transform: translateY(-1px);
}

.tile.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent) inset;
}

.tile .label {
  position: relative;
  z-index: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
  padding: 0 2px;
  /* Fixed light color, not var(--fg): tile fills are the same colorful set
     regardless of page theme, so the label needs to stay legible against
     those fills specifically, not against --bg like normal page text does
     (in light theme, var(--fg) turns near-black and — combined with the
     dark outline below — reads as an unreadable dark smear). */
  color: #f0f0f0;
  text-shadow:
    -1px -1px 0 #000, 1px -1px 0 #000,
    -1px 1px 0 #000, 1px 1px 0 #000;
}

/* dead/down: a colored rectangle frame on the normal tile background rather
   than a full fill, so they read as distinct markers instead of a solid
   block of alarming color repeated across a whole cluster during an outage */
.tile.state-dead { outline: 3px solid var(--c-dead); outline-offset: -3px; color: var(--fg-muted); }
.tile.state-down { outline: 3px solid var(--c-down); outline-offset: -3px; color: var(--fg); }
/* downtime: a yellow/black caution bar across the status-text row instead of
   a full-tile fill — the node itself isn't necessarily faulty, just under a
   scheduled Icinga downtime, so it reads as a caution flag rather than an
   alarm color covering the whole tile */
.tile.state-downtime::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 11px;
  overflow: hidden;
  z-index: 0;
  background-image: repeating-linear-gradient(
    135deg,
    #17181c 0px, #17181c 4px,
    var(--c-offline) 4px, var(--c-offline) 8px
  );
}
/* healthcheck: like downtime, an additive strip rather than covering the
   whole tile — leaves whatever the node's own background/fill is showing
   underneath (e.g. its occupancy fill, if it has one) visible below the bar */
.tile.state-healthcheck::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 11px;
  overflow: hidden;
  z-index: 0;
  /* same 135deg/4px/8px stripe geometry as .state-downtime::before above, so
     the two bar styles read as the same visual language — red bars in the
     same count/spacing as downtime's black bars, just additive (transparent
     gaps) instead of opaque (yellow) ones */
  background-image: repeating-linear-gradient(
    135deg,
    var(--c-healthcheck) 0px, var(--c-healthcheck) 4px,
    transparent 4px, transparent 8px
  );
}
.tile.state-stopped { background: var(--c-stopped); }
.tile.state-offline { background: var(--c-offline); }
.tile.state-unknown { background: var(--c-unknown); color: #f6e9ff; }

.tile.graded {
  background-image: linear-gradient(to top, var(--fill-color) var(--pct), var(--c-fill-empty) var(--pct));
}

/* legend-only: a fixed "mostly full" look, since the legend swatch has no
   real --pct/occupancy data to derive a fill height from */
.tile.legend-full-demo {
  background-image: linear-gradient(to top, var(--c-full) 80%, var(--c-fill-empty) 80%);
}

.tile.graded.state-empty,
.tile.graded.state-partialy-full,
.tile.graded.state-full,
.tile.graded.state-healthcheck {
  --fill-color: var(--c-full);
}

.tile.graded.state-draining {
  --fill-color: var(--c-draining);
}

.tile.graded.state-stopped-with-jobs {
  --fill-color: var(--c-stopped);
}

.tile.graded.state-offline-with-jobs {
  --fill-color: var(--c-offline);
}

.tile.in-downtime {
  outline: 2px dashed var(--c-downtime);
  outline-offset: -2px;
}

.tile.prop-noff {
  border-color: var(--accent);
}

.tile.prop-rebooter {
  outline: 2px solid #22c55e;
  outline-offset: -2px;
}

/* Fixed position on every tile regardless of state: sitting IN the top strip
   (same space the downtime/healthcheck bar occupies), not pushed below it —
   its own background chip is what keeps it legible over a striped bar (or
   any other fill), rather than dodging around the bar. */
.tile .badges {
  position: absolute;
  top: 1px;
  right: 2px;
  color: #f0f0f0;
  text-shadow:
    -1px -1px 0 #000, 1px -1px 0 #000,
    -1px 1px 0 #000, 1px 1px 0 #000;
  font-size: 0.55rem;
  letter-spacing: 0.05em;
  z-index: 1;
  opacity: 0.85;
}

.tile.dim {
  opacity: 0.2;
}

/* tooltip */
.node-tooltip {
  position: fixed;
  z-index: 20;
  min-width: 160px;
  max-width: 95vw;
  max-height: 80vh;
  overflow: auto;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 0.55rem 0.65rem;
  font-size: 0.75rem;
}

.node-tooltip .tip-title {
  font-weight: 600;
  text-transform: uppercase;
  font-size: 0.7rem;
  color: var(--fg-muted);
  margin-bottom: 0.35rem;
}

.node-tooltip table {
  border-collapse: collapse;
}

.node-tooltip td {
  padding: 0.1rem 0.3rem;
  white-space: nowrap;
}

.node-tooltip tr:nth-child(even) td {
  background: color-mix(in srgb, currentColor 8%, transparent);
}

.node-tooltip .lowcpu {
  color: var(--c-down);
}

.node-tooltip .hc {
  margin-top: 0.35rem;
  color: var(--c-healthcheck);
}

/* context menu */
.context-menu {
  display: none;
  position: absolute;
  z-index: 30;
  flex-direction: column;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  overflow: hidden;
  min-width: 140px;
}

.context-menu.active {
  display: flex;
}

.context-menu a {
  padding: 0.5rem 0.9rem;
  text-decoration: none;
  font-size: 0.82rem;
}

.context-menu a:hover {
  background: var(--accent);
  color: var(--accent-fg);
}

/* selection bar */
.selection-bar[hidden] {
  display: none;
}

.selection-bar {
  position: fixed;
  bottom: 1rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: var(--shadow);
  padding: 0.5rem 0.9rem;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.8rem;
  z-index: 15;
}

.selection-bar button {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  border-radius: 999px;
  padding: 0.3rem 0.7rem;
  cursor: pointer;
  font-size: 0.78rem;
}

.selection-bar button:hover {
  border-color: var(--accent);
}

/* toast */
.toast {
  position: fixed;
  bottom: 1rem;
  right: 1rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 0.55rem 0.9rem;
  font-size: 0.8rem;
  z-index: 25;
}

footer {
  margin-top: 2rem;
  border-top: 1px solid var(--border);
  padding-top: 1rem;
}

footer h2 {
  font-size: 0.95rem;
}

.legend {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 0.35rem 1rem;
  font-size: 0.78rem;
}

.legend li {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.legend .swatch {
  width: 1rem;
  height: 1rem;
  border-radius: 4px;
  flex-shrink: 0;
}
