:root {
  --bg: #0e1116;
  --panel: #161b22;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #58a6ff;
  --border: #30363d;
  --green: #3fb950;
  --red: #f85149;
  --yellow: #d29922;
  --race-T: #4d9bd1;
  --race-Z: #b06bd1;
  --race-P: #d1c34d;
  --race-R: #999;
  --race-X: #555;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  color: var(--text);
  background: var(--bg);
  overflow-x: auto;
}

header {
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: 0.6rem 1.2rem;
}
nav { display: flex; align-items: center; gap: 1rem; }
.brand { font-weight: 600; color: var(--accent); text-decoration: none; font-size: 1.1rem; }
.comp-name { color: var(--muted); }

/* --- Bot search box (header) --- */
.bot-search {
  position: relative;
  margin-left: auto;
  width: min(360px, 40vw);
}
.bot-search-input {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.35rem 0.6rem;
  font: inherit;
  font-size: 0.9rem;
  outline: none;
}
.bot-search-input::placeholder { color: var(--muted); }
.bot-search-input:focus { border-color: var(--accent); }

.bot-search-results {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  z-index: 50;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 4px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.5);
  max-height: 60vh;
  overflow-y: auto;
}
.bot-search-empty {
  padding: 0.5rem 0.7rem;
  color: var(--muted);
  font-size: 0.85rem;
  font-style: italic;
}
.bot-search-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.35rem 0.6rem;
  color: var(--text);
  text-decoration: none;
  font-size: 0.9rem;
  border-bottom: 1px solid var(--border);
}
.bot-search-row:last-child { border-bottom: none; }
.bot-search-row:hover,
.bot-search-row.active {
  background: rgba(88, 166, 255, 0.12);
  text-decoration: none;
}
.bot-search-row .bot-search-race { display: inline-flex; flex: none; }
.bot-search-row .bot-search-name {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--accent);
}
.bot-search-author {
  flex: 0 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: 0.8rem;
}
.bot-search-status {
  flex: none;
  color: var(--red);
  font-size: 0.75rem;
  font-style: italic;
}
.bot-search-elo {
  flex: none;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  font-size: 0.8rem;
  min-width: 3ch;
  text-align: right;
}
.bot-search-elo-old { opacity: 0.6; }

main {
  max-width: 1800px;
  margin: 1.2rem auto;
  padding: 0 1.2rem;
}

h1, h2 { color: var(--text); }
h1 { margin-top: 0; }
h2 { margin-top: 1.6rem; border-bottom: 1px solid var(--border); padding-bottom: 0.3rem; }

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

table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 0.5rem;
  background: var(--panel);
  border: 1px solid var(--border);
}
th, td {
  padding: 0.35rem 0.6rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
}
th {
  background: #1c2128;
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--muted);
}
tbody tr:hover { background: #1c2128; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
td.muted, .muted { color: var(--muted); }
.up { color: var(--green); }
.down { color: var(--red); }
.zero { color: var(--text); }

/* 5-bucket gradient for avg step time: green → red in 10 ms steps. */
.step-1 { color: var(--green); }      /* < 10 ms */
.step-2 { color: #9bd13f; }           /* 10–20 ms */
.step-3 { color: var(--yellow); }     /* 20–30 ms */
.step-4 { color: #d27a22; }           /* 30–40 ms */
.step-5 { color: var(--red); }        /* >= 40 ms */
.crash-warn { color: #d27a22; }
.crash-bad  { color: var(--red); }
.warn-icon {
  vertical-align: -2px;
  margin-right: 0.25rem;
}

.race {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  vertical-align: middle;
  font-weight: 700;
}
.race-icon {
  width: 18px;
  height: 18px;
  display: block;
}
.bot-title-block {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  margin-bottom: 1rem;
}
.bot-title-icon {
  display: flex;
}
.bot-title-icon .race {
  width: 2.8rem;
  height: 2.8rem;
}
.bot-title-icon .race-icon {
  width: 100%;
  height: 100%;
}
.bot-title-text {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 0.1rem;
}
.bot-title {
  margin: 0;
  line-height: 1.1;
}
.bot-subtitle {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
}
.bot-key-stats {
  margin-left: 1.5rem;
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}
.bot-key-stat {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.35rem 0.85rem;
  min-width: 4.5rem;
}
.bot-key-stat-value {
  font-size: 1.4rem;
  font-weight: 600;
  line-height: 1.1;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}
.bot-key-stat-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  margin-top: 0.15rem;
}
.race-T { color: var(--race-T); }
.race-Z { color: var(--race-Z); }
.race-P { color: var(--race-P); }
.race-R { color: var(--race-R); }
.race-X { color: var(--race-X); }

.result-win { color: var(--green); font-weight: 600; }
.result-loss { color: var(--red); }
.result-tie { color: var(--yellow); }
.result-crash { color: var(--red); font-style: italic; }
.result-cancelled { color: var(--muted); font-style: italic; }

dl.bot-meta-col, dl.match-meta {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.2rem 1rem;
  background: var(--panel);
  padding: 0.8rem 1rem;
  border: 1px solid var(--border);
  border-radius: 4px;
}
dl.bot-meta-col {
  column-gap: 2rem;
}

.bot-header {
  display: grid;
  grid-template-columns: minmax(240px, max-content) 1fr;
  gap: 1rem;
  align-items: stretch;
  min-height: 260px;
}
.bot-meta {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  height: 100%;
}
.status-active { color: var(--green); font-weight: 600; }
.status-inactive { color: var(--red); font-weight: 600; }
.bot-header dl.bot-meta-col {
  margin: 0;
  height: 100%;
  align-content: start;
}
dl.bot-meta-col-stats {
  grid-template-columns: max-content 1fr;
  column-gap: 2rem;
}
dl.bot-meta-col-stats dd {
  font-variant-numeric: tabular-nums;
}
dl.bot-meta-col-numeric {
  grid-template-columns: max-content 1fr max-content 1fr;
  column-gap: 1.5rem;
}
dl.bot-meta-col-winrates {
  grid-template-columns: max-content 1fr 1fr;
  column-gap: 0.5rem;
}
.bot-meta-heading {
  grid-column: 1 / -1;
  margin: 0 0 0.2rem;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}
.bot-meta-col-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  text-align: right;
}
.bot-meta-col-span2 {
  grid-column: 2 / -1;
}
.bot-meta-col-divider {
  grid-column: 1 / -1;
  height: 0.6rem;
}
dl.bot-meta-col-numeric dd {
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.bot-header .elo-chart-wrap {
  height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
}
.elo-chart-toolbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 10px 6px;
  flex-wrap: wrap;
  flex-shrink: 0;
}
.bot-header .elo-chart {
  flex: 1;
  min-height: 320px;
  background: transparent;
}
.elo-chart-toggle {
  background: var(--panel);
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 3px 10px;
  font: inherit;
  font-size: 11px;
  line-height: 1.4;
  cursor: pointer;
}
.elo-chart-toggle:hover {
  color: #e6edf3;
}
.elo-chart-toggle[aria-pressed="true"] {
  background: #30363d;
  color: #e6edf3;
  border-color: #58a6ff;
}
.elo-chart-legend {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  font-size: 11px;
  color: var(--muted);
}
.elo-legend-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
.elo-legend-item .race-icon {
  width: 14px;
  height: 14px;
}
.elo-legend-swatch {
  display: inline-block;
  width: 18px;
  height: 2px;
  border-radius: 1px;
}
/* Show items appropriate to the current toggle state. */
.elo-chart-toggle[aria-pressed="false"] ~ .elo-chart-legend .elo-legend-by-race { display: none; }
.elo-chart-toggle[aria-pressed="true"]  ~ .elo-chart-legend .elo-legend-overall { display: none; }
.elo-chart-toggle[hidden] ~ .elo-chart-legend .elo-legend-by-race { display: none; }
dt { color: var(--muted); }
dd { margin: 0; }

footer {
  margin: 2rem auto 1rem;
  max-width: 1800px;
  padding: 0 1.2rem;
  color: var(--muted);
  font-size: 0.85rem;
}

.empty { color: var(--muted); font-style: italic; }
.ext-link { font-size: 0.9rem; }

.actions { margin-top: 1rem; display: flex; align-items: center; gap: 1rem; }
.button {
  display: inline-block;
  padding: 0.45rem 0.9rem;
  background: var(--accent);
  color: #0e1116;
  font-weight: 600;
  border-radius: 4px;
  text-decoration: none;
}
.button:hover { text-decoration: none; opacity: 0.9; }
.button-disabled {
  background: var(--muted);
  color: var(--panel);
  cursor: not-allowed;
  opacity: 0.6;
}
.button-disabled:hover { opacity: 0.6; }

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 56px;
  height: 26px;
  color: var(--accent);
  border-radius: 4px;
  text-decoration: none;
  vertical-align: middle;
  font-size: 1.2rem;
  line-height: 1;
}
.icon-button .glyph {
  display: inline-block;
  transform: scaleX(2.5);
  transform-origin: center;
}
.icon-button:hover {
  text-decoration: none;
  background: rgba(88, 166, 255, 0.15);
  color: var(--accent);
}
/* Mark replays the user has already downloaded — class added on click and
 * persisted in localStorage (downloaded-replays-v1). */
.icon-button.downloaded {
  color: var(--muted) !important;
  opacity: 0.45;
}
.icon-button.downloaded:hover {
  color: var(--accent) !important;
  opacity: 1;
}

.placeholder {
  margin-top: 2rem;
  padding: 1rem;
  border: 1px dashed var(--border);
  border-radius: 4px;
  color: var(--muted);
}

.hint {
  color: var(--muted);
  font-size: 0.85rem;
  margin-top: 0;
}
.hint-inline {
  color: var(--muted);
  font-weight: 400;
  font-size: 0.8em;
  margin-left: 0.4rem;
}

/* Win-rate cell: small horizontal bar + integer percent label. */
.winrate-bar {
  display: inline-flex;
  width: 110px;
  height: 8px;
  background: rgba(255, 255, 255, 0.06);
  border-radius: 3px;
  overflow: hidden;
  vertical-align: middle;
  margin-right: 0.5rem;
}
.winrate-bar > .wr-w { background: var(--green); }
.winrate-bar > .wr-t { background: var(--yellow); margin: 0 1px; }
.winrate-bar > .wr-l { background: var(--red); }
.winrate-label {
  font-variant-numeric: tabular-nums;
  vertical-align: middle;
}

/* --- AG Grid host + dark Quartz theme tweaks --- */
.grid-host {
  width: 100%;
  margin-top: 0.5rem;
}

/* Override quartz-dark variables to match the rest of the site */
.ag-theme-quartz-dark {
  --ag-background-color: var(--panel);
  --ag-foreground-color: var(--text);
  --ag-header-background-color: #1c2128;
  --ag-header-foreground-color: var(--muted);
  --ag-odd-row-background-color: #14181f;
  --ag-row-hover-color: #1c2128;
  --ag-border-color: var(--border);
  --ag-secondary-border-color: var(--border);
  --ag-row-border-color: var(--border);
  --ag-control-panel-background-color: var(--panel);
  --ag-side-bar-panel-width: 220px;
  --ag-font-size: 13px;
  --ag-font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  --ag-selected-row-background-color: rgba(88, 166, 255, 0.15);
  --ag-input-focus-border-color: var(--accent);
}
.ag-theme-quartz-dark .ag-cell a { color: var(--accent); }
/* Remove the vertical separator AG Grid draws between the pinned-left
 * (rank) column and the rest of the grid. Quartz puts the divider on the
 * last pinned-left cell (border-right) plus a shadow on the container. */
.ag-theme-quartz-dark .ag-pinned-left-header,
.ag-theme-quartz-dark .ag-pinned-left-cols-container {
  border-right: none !important;
  box-shadow: none !important;
}
.ag-theme-quartz-dark .ag-cell.ag-cell-last-left-pinned:not(.ag-cell-range-right),
.ag-theme-quartz-dark .ag-header-cell.ag-column-last-left-pinned,
.ag-theme-quartz-dark .ag-header-cell.ag-header-cell-last-left-pinned {
  border-right: none !important;
}
.ag-theme-quartz-dark .ag-row.row-clickable { cursor: pointer; }
/* Hide AG Grid's small sort-priority numbers (1, 2) shown for multi-sort. */
.ag-theme-quartz-dark .ag-sort-order { display: none; }
/* Separator line above the first row of each new division on the ladder. */
.ag-theme-quartz-dark .ag-row.division-boundary {
  border-top: 2px solid var(--accent);
}
.ag-theme-quartz-dark .ag-header-cell.ag-center-header .ag-header-cell-label { justify-content: center; }

/* For right-aligned (numeric) columns AG Grid sets:
 *   .ag-cell-label-container { flex-direction: row }   → menu on the LEFT
 *   .ag-header-cell-label    { flex-direction: row-reverse } → text on the RIGHT
 *   .ag-header-cell-text     { text-align: end }       → glyphs hug the right
 * Override all three so every header reads "label on left, menu on right",
 * regardless of column type. Cell content stays right-aligned via the
 * separate ag-right-aligned-cell class. */
.ag-theme-quartz-dark .ag-header-cell.ag-right-aligned-header .ag-cell-label-container {
  flex-direction: row-reverse !important;
}
.ag-theme-quartz-dark .ag-header-cell.ag-right-aligned-header .ag-header-cell-label {
  flex-direction: row !important;
  justify-content: flex-start !important;
}
.ag-theme-quartz-dark .ag-header-cell.ag-right-aligned-header .ag-header-cell-text {
  text-align: start !important;
}
.ag-theme-quartz-dark .ag-header-cell.ag-center-header .ag-header-cell-label {
  justify-content: center !important;
}

/* --- Custom column-toggle toolbar --- */
.grid-toolbar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.4rem;
  position: relative;
}
.col-toggle-button {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.35rem 0.7rem;
  cursor: pointer;
  font-size: 0.85rem;
}
.col-toggle-button:hover { border-color: var(--accent); color: var(--accent); }
.col-toggle-popover {
  display: none;
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  z-index: 10;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 4px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
  padding: 0.4rem 0.6rem;
  flex-direction: column;
  gap: 0.25rem;
  min-width: 200px;
  max-height: 60vh;
  overflow-y: auto;
}
.col-toggle-popover.open { display: flex; }
.col-toggle-popover label {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  cursor: pointer;
  font-size: 0.85rem;
  user-select: none;
}
.col-toggle-popover label:hover { color: var(--accent); }
