/* Battle AIrena — Monitor (T80 Phase 8.1)
   Read-only health dashboard. Two layouts (compact + spacious)
   switchable via top-right selector or ?layout= URL param.

   Shared widgets live in the DOM; each layout's grid-template-areas
   rearranges them. Widgets that exist only in one layout get
   display:none in the other layout's rules.
*/

@layer defaults, theme, responsive, user;

@layer defaults {

:root {
  --bg: #0e1116;
  --bg-panel: #161b22;
  --bg-panel-2: #1f2630;
  --border: #2d333b;
  --fg: #d0d7de;
  --fg-dim: #8b949e;
  --fg-strong: #f0f6fc;
  --accent: #58a6ff;
  --green: #3fb950;
  --yellow: #d29922;
  --red: #f85149;
  --cyan: #56d4dd;
  --orange: #db6d28;
  --grey: #6e7681;
  --green-bg: #133621;
  --yellow-bg: #3d2f12;
  --red-bg: #3d1418;
  --cyan-bg: #103b3d;
  --orange-bg: #3a1f0c;
  --grey-bg: #21262d;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
  font-size: 14px;
}

/* Author layered rules with explicit `display:` (e.g. .overlay
   below uses flex, #monitor-header uses flex) outrank the user-agent
   `[hidden] { display: none }` rule under the cascade — so the
   HTML hidden attribute alone doesn't hide them. These targeted
   overrides restore the expected behaviour without resorting to
   !important. */
.overlay[hidden],
#monitor-header[hidden],
#dashboard[hidden],
[data-bind][hidden] {
  display: none;
}

/* ── Login overlay ─────────────────────────────────────────────── */

.overlay {
  position: fixed; inset: 0;
  background: rgba(14, 17, 22, 0.95);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.login-card {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 32px;
  width: 320px;
}
.login-card h1 { margin: 0 0 4px; font-size: 22px; color: var(--fg-strong); }
.login-subtitle { margin: 0 0 24px; color: var(--fg-dim); }
.form-group { margin-bottom: 16px; }
.form-group label { display: block; margin-bottom: 4px; color: var(--fg-dim); font-size: 12px; }
.form-group input {
  width: 100%;
  background: var(--bg-panel-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 8px 12px;
  color: var(--fg);
  font-family: var(--font);
  font-size: 14px;
}
.form-group input:focus { outline: none; border-color: var(--accent); }
.login-error {
  background: var(--red-bg);
  color: var(--red);
  padding: 8px 12px;
  border-radius: 4px;
  margin-bottom: 12px;
  font-size: 13px;
}

/* ── Buttons ───────────────────────────────────────────────────── */

.btn {
  display: inline-block;
  padding: 6px 12px;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--bg-panel-2);
  color: var(--fg);
  font-family: var(--font);
  font-size: 13px;
  cursor: pointer;
  text-decoration: none;
}
.btn:hover { background: var(--border); }
.btn-primary { background: var(--accent); color: var(--bg); border-color: var(--accent); }
.btn-primary:hover { filter: brightness(1.1); }
.btn-secondary { background: var(--bg-panel-2); }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-block { width: 100%; padding: 10px; }

/* ── Header ────────────────────────────────────────────────────── */

#monitor-header {
  display: flex;
  flex-direction: column;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
}
.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
}
.controls-row {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 8px 20px;
  flex-wrap: wrap;
}
.toggle-group {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.toggle-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 24px;
  padding: 0 10px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-panel-2);
  color: var(--fg-dim);
  font-family: var(--font);
  font-size: 12px;
  cursor: pointer;
  user-select: none;
}
.toggle-pill:hover { filter: brightness(1.2); }
.toggle-pill .toggle-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--grey);
  flex: 0 0 auto;
}
.toggle-pill.on {
  border-color: var(--green);
  color: var(--fg-strong);
  background: var(--green-bg);
}
.toggle-pill.on .toggle-dot {
  background: var(--green);
  box-shadow: 0 0 4px var(--green);
}
.toggle-pill.pending {
  /* Optimistic-update visual while PATCH is in flight. Slight
     dimming distinguishes "user clicked, awaiting server" from
     the final settled state. */
  opacity: 0.6;
  cursor: progress;
}
.export-group {
  display: inline-flex;
  gap: 6px;
  margin-left: auto;
}
.header-left { display: flex; align-items: baseline; gap: 16px; }
.header-left h1 { margin: 0; font-size: 16px; color: var(--fg-strong); }
.header-clock { color: var(--fg-dim); font-family: var(--font-mono); font-size: 12px; }
.header-right { display: flex; align-items: center; gap: 12px; }
.header-status {
  color: var(--fg-dim);
  font-family: var(--font-mono);
  font-size: 12px;
}
.header-status.error { color: var(--red); }
.layout-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--fg-dim);
  font-size: 12px;
}
.layout-toggle select {
  background: var(--bg-panel-2);
  border: 1px solid var(--border);
  color: var(--fg);
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
}

/* ── Dashboard grid — base ─────────────────────────────────────── */

.dashboard {
  padding: 16px;
  gap: 16px;
}

.widget {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 14px 16px;
  overflow: hidden;
}
.widget h2 {
  margin: 0 0 12px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--fg-dim);
  font-weight: 600;
}
.widget h3 {
  margin: 8px 0 6px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--fg-dim);
  font-weight: 600;
}

/* Key-value row used across widgets */
.kv {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 3px 0;
  font-family: var(--font-mono);
  font-size: 13px;
}
.kv span:first-child { color: var(--fg-dim); }
.kv span:last-child { color: var(--fg-strong); }

/* Threshold-coded cells */
.cell.green { color: var(--green); }
.cell.yellow { color: var(--yellow); background: var(--yellow-bg); padding: 1px 6px; border-radius: 3px; }
.cell.red { color: var(--red); background: var(--red-bg); padding: 1px 6px; border-radius: 3px; }
.cell.grey { color: var(--fg-dim); }

/* ── Layout: Compact (sidebar + matrix) ────────────────────────── */

body.layout-compact .dashboard {
  display: grid;
  grid-template-columns: 280px 1fr;
  grid-template-areas:
    "sidebar tournaments"
    "sidebar recent"
    "sidebar standalone"
    "sidebar latency";
  align-items: start;
}

body.layout-compact .widget-system,
body.layout-compact .widget-memory,
body.layout-compact .widget-platform,
body.layout-compact .widget-activity,
body.layout-compact .widget-sse,
body.layout-compact .widget-logging {
  display: none;
}

body.layout-compact .widget-sidebar { grid-area: sidebar; align-self: stretch; }
body.layout-compact .widget-tournaments { grid-area: tournaments; }
body.layout-compact .widget-recent { grid-area: recent; }
body.layout-compact .widget-standalone { grid-area: standalone; }
body.layout-compact .widget-latency { grid-area: latency; }

.sidebar-group {
  border-bottom: 1px solid var(--border);
  padding-bottom: 10px;
  margin-bottom: 10px;
}
.sidebar-group:last-child { border-bottom: none; margin-bottom: 0; }
.sparkline-mini { width: 100%; height: 28px; margin: 4px 0; }

/* ── Layout: Spacious (counter cards + full-width tournaments) ── */

body.layout-spacious .dashboard {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  grid-template-areas:
    "system   memory      platform"
    "tournaments tournaments tournaments"
    "recent   standalone   standalone"
    "latency  latency      latency"
    "activity sse          logging";
}

body.layout-spacious .widget-sidebar { display: none; }

body.layout-spacious .widget-system { grid-area: system; }
body.layout-spacious .widget-memory { grid-area: memory; }
body.layout-spacious .widget-platform { grid-area: platform; }
body.layout-spacious .widget-tournaments { grid-area: tournaments; }
body.layout-spacious .widget-recent { grid-area: recent; }
body.layout-spacious .widget-standalone { grid-area: standalone; }
body.layout-spacious .widget-latency { grid-area: latency; }
body.layout-spacious .widget-activity { grid-area: activity; }
body.layout-spacious .widget-sse { grid-area: sse; }
body.layout-spacious .widget-logging { grid-area: logging; }

/* ── Tournament matrix view ────────────────────────────────────── */

.tournaments-container {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.tournament-card {
  background: var(--bg-panel-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 12px;
}
.tournament-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
  font-family: var(--font-mono);
  font-size: 13px;
}
.tournament-name {
  color: var(--fg-strong);
  font-weight: 600;
  flex: 1;
}
.tournament-engine-tag {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  color: var(--cyan);
  padding: 1px 8px;
  border-radius: 3px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.5px;
}
.tournament-progress {
  color: var(--fg-dim);
  font-family: var(--font-mono);
  font-size: 12px;
}

/* Matrix: rows of tiles (top = matches, below = games per match) */
.matrix-row {
  display: flex;
  gap: 3px;
  margin-bottom: 3px;
}
.matrix-row-label {
  width: 14px;
  color: var(--fg-dim);
  font-family: var(--font-mono);
  font-size: 10px;
  text-align: center;
  flex: 0 0 auto;
  line-height: 22px;
}
.matrix-tile {
  width: 22px;
  height: 22px;
  border-radius: 3px;
  background: var(--grey-bg);
  border: 1px solid var(--border);
  cursor: pointer;
  flex: 0 0 auto;
  position: relative;
}
.matrix-tile.placeholder {
  background: transparent;
  border-style: dashed;
}
.matrix-tile.status-pending { background: var(--grey-bg); border-color: var(--grey); }
.matrix-tile.status-ready { background: var(--cyan-bg); border-color: var(--cyan); }
.matrix-tile.status-running { background: var(--yellow-bg); border-color: var(--yellow); animation: pulse 1.5s ease-in-out infinite; }
.matrix-tile.status-paused { background: var(--orange-bg); border-color: var(--orange); }
.matrix-tile.status-ended { background: var(--green-bg); border-color: var(--green); }
.matrix-tile.status-aborted,
.matrix-tile.status-voided,
.matrix-tile.status-cancelled { background: var(--red-bg); border-color: var(--red); }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

.matrix-tile:hover { filter: brightness(1.4); }
.matrix-tile.non-clickable { cursor: default; }
.matrix-tile.non-clickable:hover { filter: none; }

/* The match-row tiles are slightly wider — they often carry [N/M] text */
.matrix-row.match-row .matrix-tile {
  width: auto;
  min-width: 40px;
  padding: 0 4px;
  font-size: 10px;
  font-family: var(--font-mono);
  color: var(--fg);
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

/* ── Tile strips (recent tournaments + standalone games) ───────── */

.tile-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
}
.strip-tile {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 24px;
  padding: 0 6px;
  border-radius: 3px;
  background: var(--bg-panel-2);
  border: 1px solid var(--border);
  font-family: var(--font-mono);
  font-size: 11px;
  cursor: pointer;
  color: var(--fg);
}
.strip-tile.status-pending { background: var(--grey-bg); border-color: var(--grey); }
.strip-tile.status-ready { background: var(--cyan-bg); border-color: var(--cyan); }
.strip-tile.status-running { background: var(--yellow-bg); border-color: var(--yellow); }
.strip-tile.status-paused { background: var(--orange-bg); border-color: var(--orange); }
.strip-tile.status-ended { background: var(--green-bg); border-color: var(--green); }
.strip-tile.status-aborted,
.strip-tile.status-voided,
.strip-tile.status-cancelled { background: var(--red-bg); border-color: var(--red); }
.strip-tile:hover { filter: brightness(1.4); }
.strip-tile.selected {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.strip-tile .engine-letter {
  color: var(--cyan);
  font-weight: 600;
}

/* Expanded past-tournament slot inside the Recent Tournaments widget.
   Sits below the tile strip; visible only when a past tile is selected.
   Re-uses .tournament-card via shared markup from buildTournamentCard. */
#recent-expanded-container {
  margin-top: 12px;
  border-top: 1px solid var(--border);
  padding-top: 12px;
}

/* Status badge on the tournament card header — distinguishes past
   (ended/aborted) tournaments rendered in the expanded slot from
   active ones in the Active Tournaments section. Active tournaments
   omit the badge entirely (running state is implicit by section). */
.tournament-status-badge {
  font-family: var(--font-mono);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 1px 6px;
  border-radius: 3px;
  font-weight: 600;
}
.tournament-status-badge.status-ended { color: var(--green); background: var(--green-bg); }
.tournament-status-badge.status-aborted { color: var(--red); background: var(--red-bg); }
.tournament-status-badge.status-cancelled { color: var(--red); background: var(--red-bg); }
.tournament-status-badge.status-ready { color: var(--cyan); background: var(--cyan-bg); }
.tournament-status-badge.status-created { color: var(--fg-dim); background: var(--grey-bg); }

.standalone-rows { display: flex; flex-direction: column; gap: 8px; }
.standalone-row { display: flex; align-items: center; gap: 10px; }
.row-label {
  color: var(--fg-dim);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  width: 70px;
  flex: 0 0 auto;
}

/* ── Per-team latency strip ────────────────────────────────────── */

.latency-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
.latency-cell {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 80px;
  padding: 6px 8px;
  border-radius: 4px;
  background: var(--bg-panel-2);
  border: 1px solid var(--border);
  font-family: var(--font-mono);
  font-size: 11px;
}
.latency-cell.green { background: var(--green-bg); border-color: var(--green); color: var(--green); }
.latency-cell.yellow { background: var(--yellow-bg); border-color: var(--yellow); color: var(--yellow); }
.latency-cell.red { background: var(--red-bg); border-color: var(--red); color: var(--red); }
.latency-cell .team-name { font-weight: 600; }
.latency-cell .p95-value { font-size: 13px; margin-top: 2px; }

.empty-state {
  color: var(--fg-dim);
  font-style: italic;
  font-size: 12px;
  margin: 8px 0;
}

/* ── Sparkline (inline SVG) ────────────────────────────────────── */

.sparkline {
  display: block;
  width: 100%;
  height: 80px;
  margin: 8px 0;
}
.sparkline path {
  fill: none;
  stroke-width: 1.5;
  stroke-linejoin: round;
  vector-effect: non-scaling-stroke;
}
.sparkline path.rss { stroke: var(--accent); }
.sparkline path.persisted { stroke: var(--cyan); }

/* ── Responsive — narrower screens collapse the spacious grid ── */

@media (max-width: 1100px) {
  body.layout-spacious .dashboard {
    grid-template-columns: 1fr 1fr;
    grid-template-areas:
      "system memory"
      "platform platform"
      "tournaments tournaments"
      "recent standalone"
      "latency latency"
      "activity sse"
      "logging logging";
  }
}

@media (max-width: 800px) {
  body.layout-compact .dashboard,
  body.layout-spacious .dashboard {
    grid-template-columns: 1fr;
    grid-template-areas: none;
  }
  body.layout-compact .widget-sidebar { display: block; }
  body.layout-spacious .widget-sidebar { display: none; }
}

} /* @layer defaults */
