:root {
  --bg: #0e1117;
  --bg-elev: #161b22;
  --bg-elev-2: #1d242d;
  --border: #2d3540;
  --fg: #e6edf3;
  --muted: #8b949e;
  --accent: #58a6ff;
  --accent-warm: #f7d063;
  --green: #3fb950;
  --red: #f85149;
  --orange: #d29922;
  --pink: #ff7b72;
  --shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, "JetBrains Mono", Menlo, monospace;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
  line-height: 1.45;
}

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

.topbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem 1rem;
  padding: 0.75rem 1.25rem;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 0.5rem;
  min-width: 0;
  font-weight: 600;
  font-size: 1.05rem;
  word-break: break-word;
}
.brand .bolt {
  font-size: 1.2rem;
  filter: drop-shadow(0 0 4px var(--accent-warm));
}
.brand small { color: var(--muted); font-weight: 400; font-size: 0.85rem; }

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.25rem 0.65rem;
  border-radius: 999px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  font-family: var(--mono);
  max-width: 100%;
  overflow: hidden;
}
.status-pill .label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.status-pill .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
}
.status-pill.state-running_claude .dot,
.status-pill.state-escalating .dot,
.status-pill.state-selected .dot {
  background: var(--green);
  animation: pulse 1.4s infinite;
}
.status-pill.state-shipped .dot { background: var(--green); }
.status-pill.state-completed .dot { background: var(--green); }
.status-pill.state-blocked .dot,
.status-pill.state-exhausted .dot,
.status-pill.state-skipped_dirty .dot,
.status-pill.state-skipped_diverged .dot,
.status-pill.state-errored .dot { background: var(--red); }
.status-pill.state-pending_review .dot { background: var(--orange); }
.status-pill.state-ci_waiting .dot {
  background: var(--accent);
  animation: pulse 1.4s infinite;
}
.status-pill.state-ci_fixing .dot {
  background: var(--orange);
  animation: pulse 1.4s infinite;
}
.status-pill.state-rebuilding .dot {
  background: var(--accent);
  animation: pulse 1.4s infinite;
}
.status-pill.state-limit_sleep .dot,
.status-pill.state-sleeping .dot { background: var(--accent); }

.meta {
  margin-left: auto;
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem 0.75rem;
  align-items: center;
  font-size: 0.8rem;
  color: var(--muted);
  font-family: var(--mono);
}
.conn::before { content: "● "; color: var(--green); }
.conn.disconnected::before { color: var(--red); }

.limit-banner {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 0.75rem 1.25rem 0;
  padding: 0.7rem 1rem;
  background: rgba(88, 166, 255, 0.10);
  border: 1px solid var(--accent);
  border-left: 4px solid var(--accent);
  border-radius: var(--radius);
  color: var(--fg);
  font-size: 0.9rem;
}
.limit-banner.hidden { display: none; }
.limit-banner .limit-icon { font-size: 1.1rem; }
.limit-banner .limit-text { display: flex; flex-wrap: wrap; gap: 0.35rem 0.6rem; min-width: 0; }
.limit-banner .limit-detail { color: var(--muted); font-family: var(--mono); font-size: 0.82rem; }

.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 0.75rem;
  padding: 1rem 1.25rem 0.5rem;
}
.kpi {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
}
.kpi-label {
  font-size: 0.75rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.kpi-value {
  margin-top: 0.25rem;
  font-size: 1.4rem;
  font-weight: 600;
  font-family: var(--mono);
}

.charts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 0.75rem;
  padding: 0.5rem 1.25rem;
}
.chart-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
}
.chart-title {
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 0.5rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.chart svg { width: 100%; display: block; }

.filters {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  padding: 0.75rem 1.25rem 0.25rem;
  flex-wrap: wrap;
  font-size: 0.85rem;
}
.label-filters {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}
.label-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  font-family: var(--mono);
  font-size: 0.78rem;
  cursor: pointer;
  user-select: none;
}
.label-chip.active { background: var(--accent); color: #0b1018; border-color: var(--accent); }
.label-chip[data-special="in"]:not(.active) { color: var(--green); }
.label-chip[data-special="attn"]:not(.active) { color: var(--orange); }
.label-chip[data-special="block"]:not(.active) { color: var(--red); }
.label-chip[data-special="rev"]:not(.active) { color: var(--pink); }

button.ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.2rem 0.6rem;
  cursor: pointer;
  font-family: var(--mono);
  font-size: 0.78rem;
}
button.ghost:hover { color: var(--fg); border-color: var(--accent); }

#repos {
  padding: 0.75rem 1.25rem 2rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.repo-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0;
  overflow: hidden;
  transition: border-color 0.3s ease;
}
.repo-card.working {
  border-color: var(--green);
  box-shadow: 0 0 0 1px var(--green), 0 0 24px -8px var(--green);
  animation: working-glow 2.5s ease-in-out infinite;
}
.repo-card.rebuilding {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent), 0 0 24px -8px var(--accent);
  animation: rebuilding-glow 2.5s ease-in-out infinite;
}

.repo-head {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem 1rem;
  padding: 0.75rem 1rem;
  background: var(--bg-elev-2);
  border-bottom: 1px solid var(--border);
}
.repo-head h2 {
  margin: 0;
  min-width: 0;
  font-size: 1rem;
  font-weight: 600;
  font-family: var(--mono);
  word-break: break-word;
}
.repo-head h2 a { color: inherit; }
.repo-head h2 a:hover { color: var(--accent); }
.repo-head .repo-link {
  color: var(--muted);
  font-size: 0.85rem;
  font-family: var(--mono);
}
.repo-head .repo-link:hover { color: var(--accent); }
.repo-head .branch { color: var(--muted); font-family: var(--mono); font-size: 0.85rem; word-break: break-word; }
.repo-head .stats {
  margin-left: auto;
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem 0.5rem;
  font-size: 0.75rem;
  font-family: var(--mono);
}
.stat-pill {
  padding: 0.15rem 0.55rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
}
.stat-pill.open { color: var(--accent); }
.stat-pill.inprog { color: var(--green); }
.stat-pill.attn { color: var(--orange); }
.stat-pill.err { color: var(--red); }
.stat-pill.health {
  color: var(--bg);
  background: var(--orange);
  border-color: var(--orange);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.kpi-value.alert { color: var(--orange); }

.repo-card.unhealthy {
  border-color: var(--orange);
  box-shadow: 0 0 0 1px var(--orange);
}
.repo-card.unhealthy.working {
  /* Working trumps unhealthy visually — keep the green glow but tint the
     border to the warning colour so the dev still sees something is off. */
  box-shadow: 0 0 0 1px var(--orange), 0 0 24px -8px var(--green);
}
.repo-card.unhealthy.rebuilding {
  box-shadow: 0 0 0 1px var(--orange), 0 0 24px -8px var(--accent);
}

.repo-health {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem 0.75rem;
  padding: 0.55rem 1rem;
  background: rgba(210, 153, 34, 0.12);
  border-bottom: 1px solid var(--border);
  border-left: 4px solid var(--orange);
  font-size: 0.85rem;
}
.repo-health .health-icon { color: var(--orange); }
.repo-health strong { color: var(--fg); }
.repo-health .health-since { color: var(--muted); font-family: var(--mono); font-size: 0.75rem; }
.repo-health .health-hint { color: var(--muted); flex: 1 1 240px; min-width: 0; }
.repo-health .health-detail { width: 100%; }
.repo-health .health-detail summary {
  cursor: pointer;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.75rem;
}
.repo-health .health-detail pre {
  margin: 0.4rem 0 0;
  padding: 0.5rem 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-family: var(--mono);
  font-size: 0.78rem;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 12rem;
  overflow: auto;
}
.repo-health-branch_diverged,
.repo-health-iteration_error,
.repo-health-quarantined {
  background: rgba(248, 81, 73, 0.10);
  border-left-color: var(--red);
}
.repo-health-branch_diverged .health-icon,
.repo-health-iteration_error .health-icon,
.repo-health-quarantined .health-icon { color: var(--red); }

.issue-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
}
.issue-row {
  display: grid;
  grid-template-columns: 60px minmax(0, 1fr) auto auto;
  gap: 0.75rem;
  padding: 0.55rem 1rem;
  border-bottom: 1px solid var(--border);
  align-items: center;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.15s ease;
}
.issue-row .title {
  min-width: 0;
  word-break: break-word;
  overflow-wrap: anywhere;
}
.issue-row:hover { background: var(--bg-elev-2); }
.issue-row:last-child { border-bottom: none; }
.issue-row.is-working {
  background: linear-gradient(90deg, rgba(63,185,80,0.16) 0%, rgba(63,185,80,0) 60%);
  position: relative;
}
.issue-row.is-working::before {
  content: "▶";
  position: absolute;
  left: 0.45rem;
  color: var(--green);
  animation: pulse 1.4s infinite;
}
.issue-row.is-rebuilding {
  background: linear-gradient(90deg, rgba(88,166,255,0.16) 0%, rgba(88,166,255,0) 60%);
  position: relative;
}
.issue-row.is-rebuilding::before {
  content: "▶";
  position: absolute;
  left: 0.45rem;
  color: var(--accent);
  animation: pulse 1.4s infinite;
}
.issue-row .num { color: var(--muted); font-family: var(--mono); font-size: 0.8rem; }
.issue-row .title { color: var(--fg); }
.issue-row .labels {
  display: flex;
  gap: 0.25rem;
  flex-wrap: wrap;
}
.issue-row .labels .label-chip {
  font-size: 0.7rem;
  padding: 0.05rem 0.4rem;
  cursor: default;
}
.issue-row .ext {
  color: var(--muted);
  font-size: 0.75rem;
  font-family: var(--mono);
}
.issue-row .ext a { color: var(--muted); }
.issue-row .ext a:hover { color: var(--accent); }

.empty-row {
  padding: 0.75rem 1rem;
  color: var(--muted);
  font-size: 0.85rem;
  font-style: italic;
}

footer {
  text-align: center;
  padding: 1rem;
  color: var(--muted);
  font-size: 0.8rem;
  border-top: 1px solid var(--border);
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1rem;
  justify-content: center;
}

.drill-down {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 1rem;
}
.drill-down.hidden { display: none; }
.drill-card {
  position: relative;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  max-width: 720px;
  width: 100%;
  max-height: 80vh;
  overflow: auto;
  padding: 1.5rem;
  box-shadow: var(--shadow);
}
.drill-card .close {
  position: absolute;
  top: 0.5rem;
  right: 0.75rem;
  background: transparent;
  color: var(--muted);
  border: none;
  font-size: 1.4rem;
  cursor: pointer;
}
.drill-card h3 { margin-top: 0; }
.drill-card table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
  font-family: var(--mono);
}
.drill-card th, .drill-card td {
  border-bottom: 1px solid var(--border);
  padding: 0.4rem 0.6rem;
  text-align: left;
}
.drill-card th { color: var(--muted); font-weight: 500; }

@keyframes pulse {
  0%   { opacity: 1; transform: scale(1); }
  50%  { opacity: 0.5; transform: scale(0.7); }
  100% { opacity: 1; transform: scale(1); }
}
@keyframes working-glow {
  0%, 100% { box-shadow: 0 0 0 1px var(--green), 0 0 16px -8px var(--green); }
  50%      { box-shadow: 0 0 0 1px var(--green), 0 0 32px -4px var(--green); }
}
@keyframes rebuilding-glow {
  0%, 100% { box-shadow: 0 0 0 1px var(--accent), 0 0 16px -8px var(--accent); }
  50%      { box-shadow: 0 0 0 1px var(--accent), 0 0 32px -4px var(--accent); }
}

@media (max-width: 640px) {
  .topbar { padding: 0.6rem 0.75rem; }
  .meta { margin-left: 0; width: 100%; }

  .kpis { grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); padding: 0.75rem 0.75rem 0.25rem; gap: 0.5rem; }
  .kpi { padding: 0.55rem 0.7rem; }
  .kpi-value { font-size: 1.15rem; }

  .charts { padding: 0.5rem 0.75rem; gap: 0.5rem; }
  .chart-card { padding: 0.6rem 0.75rem; }

  .filters { padding: 0.5rem 0.75rem 0.25rem; }
  #repos { padding: 0.5rem 0.75rem 1.5rem; }

  .repo-head { padding: 0.6rem 0.75rem; }
  .repo-head .stats { margin-left: 0; width: 100%; }

  .issue-row {
    grid-template-columns: auto minmax(0, 1fr);
    row-gap: 0.3rem;
    padding: 0.55rem 0.75rem;
  }
  .issue-row .num   { grid-column: 1; grid-row: 1; }
  .issue-row .title { grid-column: 2; grid-row: 1; }
  .issue-row .labels { grid-column: 1 / -1; grid-row: 2; }
  .issue-row .ext   { grid-column: 1 / -1; grid-row: 3; }
  .issue-row.is-working::before { left: 0.2rem; }
  .issue-row.is-rebuilding::before { left: 0.2rem; }

  .drill-down { padding: 0.5rem; align-items: flex-start; }
  .drill-card { padding: 1rem; max-height: calc(100vh - 1rem); }
  .drill-card table { display: block; overflow-x: auto; white-space: nowrap; }
}
