:root {
  --fg: #0f172a;
  --fg-muted: #64748b;
  --bg: #ffffff;
  --bg-subtle: #f8fafc;
  --border: #e2e8f0;
  --accent: #2563eb;
  --stale: #d97706;

  --stage-landing-zone: #64748b;
  --stage-scope-draft: #3b82f6;
  --stage-scope-ready: #0891b2;
  --stage-investment-ready: #7c3aed;
  --stage-cpo-review: #d97706;
  --stage-approved: #16a34a;
  --stage-parked: #6b7280;
  --stage-rejected: #dc2626;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; overflow: hidden; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 13px;
  line-height: 1.35;
  color: var(--fg);
  background: var(--bg-subtle);
  display: flex;
  flex-direction: column;
}

.mono { font-family: ui-monospace, SFMono-Regular, "SF Mono", Consolas, monospace; }

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
  height: 48px;
}
.topbar h1 { font-size: 14px; font-weight: 600; }
.status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  color: var(--fg-muted);
}
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.dot-live    { background: #22c55e; }
.dot-stale   { background: var(--stale); }
.dot-offline { background: #ef4444; }
.dot-unknown { background: #a1a1aa; }

.version-link {
  color: var(--accent);
  text-decoration: none;
  padding: 2px 8px;
  border: 1px solid var(--border);
  border-radius: 3px;
  font-size: 11px;
}
.version-link:hover { background: var(--bg-subtle); }

/* Banners */
.banner { padding: 6px 16px; font-size: 12px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.banner-stale  { background: #fef3c7; color: #92400e; }
.banner-offline { background: #f4f4f5; color: var(--fg-muted); }

/* Grid */
.grid {
  flex: 1;
  display: grid;
  grid-template-columns: 2fr 1fr;
  grid-template-rows: 3fr 2fr 2fr;
  grid-template-areas:
    "pipeline  tasks"
    "pipeline  questions"
    "activity  activity";
  gap: 8px;
  padding: 8px;
  overflow: hidden;
  min-height: 0;
}

.panel-pipeline  { grid-area: pipeline; }
.panel-tasks     { grid-area: tasks; }
.panel-questions { grid-area: questions; }
.panel-activity  { grid-area: activity; }

/* Panel */
.panel {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  min-height: 0;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-subtle);
  flex-shrink: 0;
}

.panel-header h2 {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--fg-muted);
}

.panel-meta { font-size: 11px; color: var(--fg-muted); }

.panel-body { flex: 1; overflow: auto; min-height: 0; }

.btn-sort {
  background: none;
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 2px 6px;
  font-size: 11px;
  color: var(--fg-muted);
  cursor: pointer;
  font-family: inherit;
}
.btn-sort:hover { background: var(--bg); }

/* Tables */
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  table-layout: fixed;
}

.data-table thead {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--bg);
  box-shadow: 0 1px 0 var(--border);
}

.data-table th {
  text-align: left;
  padding: 6px 12px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--fg-muted);
  border-bottom: 1px solid var(--border);
}

.data-table td {
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.data-table tbody tr { transition: background 0.1s; }
.data-table tbody tr:hover { background: var(--bg-subtle); cursor: pointer; }

/* Pipeline cols */
.col-sbr      { width: 64px; font-family: ui-monospace, monospace; font-weight: 600; color: var(--accent); font-size: 12px; text-align: right; padding-right: 8px !important; }
.col-title    { /* flex — no width, table-layout fixed gives it the remainder */ }
.col-stage    { width: 160px; }
.col-activity { width: 220px; font-size: 11px; color: var(--fg-muted); }
.col-who      { width: 80px; }
.col-upd      { width: 48px; font-family: ui-monospace, monospace; font-size: 11px; color: var(--fg-muted); text-align: right; }

/* Pipeline Last-change cell wraps onto a second line instead of truncating */
.data-table td.col-activity {
  white-space: normal;
  word-break: break-word;
  line-height: 1.4;
}
.activity-actor {
  font-family: ui-monospace, monospace;
  color: var(--fg);
  font-weight: 500;
  display: block;
}
.activity-summary {
  color: var(--fg-muted);
  font-family: ui-monospace, monospace;
  display: block;
}

/* Activity table cols */
.col-device       { width: 240px; font-family: ui-monospace, monospace; font-size: 12px; }

/* Per-device liveness dot — based on last_sync_utc age */
.liveness-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 8px;
  flex-shrink: 0;
  vertical-align: middle;
}
.liveness-live   { background: #22c55e; box-shadow: 0 0 0 2px rgba(34,197,94,0.18); animation: pulse-live 2.4s ease-in-out infinite; }
.liveness-idle   { background: #d97706; }
.liveness-stale  { background: #dc2626; }
.liveness-offline{ background: #6b7280; }
@keyframes pulse-live {
  0%, 100% { box-shadow: 0 0 0 2px rgba(34,197,94,0.18); }
  50%      { box-shadow: 0 0 0 4px rgba(34,197,94,0.28); }
}
.col-policy-wide  { width: 260px; font-size: 12px; }
.col-metric       { width: 180px; font-size: 12px; }
.col-last-action  { color: var(--fg-muted); font-size: 12px; }
.col-expand       { width: 24px; text-align: center; }

/* Activity cells wrap — no ellipses anywhere */
.panel-activity .data-table td {
  white-space: normal;
  word-break: break-word;
  vertical-align: top;
  line-height: 1.45;
}

.metric-primary {
  font-family: ui-monospace, monospace;
  font-size: 12px;
  color: var(--fg);
  font-weight: 500;
}
.metric-totals {
  font-family: ui-monospace, monospace;
  font-size: 11px;
  color: var(--fg-muted);
  display: block;
  margin-top: 2px;
}

/* Item-ID badge for tasks/questions */
.item-id {
  font-family: ui-monospace, monospace;
  font-size: 10px;
  font-weight: 600;
  color: var(--fg-muted);
  background: var(--bg-subtle);
  padding: 1px 4px;
  border-radius: 3px;
  flex-shrink: 0;
  letter-spacing: 0.02em;
}

/* Stage checklist — 6 progression steps as dots + 2-letter abbreviations */
.stage-checklist {
  display: flex;
  align-items: center;
  gap: 5px;
}
.stage-step {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-width: 18px;
}
.stage-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  box-sizing: border-box;
  flex-shrink: 0;
}
.stage-dot-done {
  background: var(--stage-approved);      /* green filled */
}
.stage-dot-wip {
  background: transparent;
  border: 2px solid var(--stage-cpo-review);  /* amber ring */
  width: 12px;
  height: 12px;
}
.stage-dot-pending {
  background: transparent;
  border: 1px solid var(--border);
}
.stage-abbr {
  font-size: 9px;
  font-family: ui-monospace, monospace;
  color: var(--fg-muted);
  letter-spacing: 0.02em;
  text-transform: uppercase;
  line-height: 1;
}
.stage-abbr-wip {
  color: var(--fg);
  font-weight: 700;
}
.stage-abbr-done {
  color: var(--stage-approved);
}
.stage-terminal {
  font-size: 13px;
  margin-left: 6px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 3px;
}
.stage-terminal-parked   { color: var(--stage-parked); }
.stage-terminal-rejected { color: var(--stage-rejected); }
.stage-terminal-label {
  font-family: ui-monospace, monospace;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
/* In terminal state, dots are greyed to de-emphasize prior progress */
.stage-checklist-terminal .stage-dot {
  background: transparent;
  border: 1px solid var(--border);
  opacity: 0.6;
  width: 10px;
  height: 10px;
}
.stage-checklist-terminal .stage-abbr {
  opacity: 0.6;
}

/* Avatars */
.avatar-stack { display: inline-flex; align-items: center; }
.avatar-stack img {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 1px solid var(--bg);
  margin-left: -4px;
}
.avatar-stack img:first-child { margin-left: 0; }
.avatar-stack .avatar-more {
  margin-left: 4px;
  font-size: 10px;
  color: var(--fg-muted);
  font-family: ui-monospace, monospace;
}

.policy-pill {
  font-size: 11px;
  padding: 1px 4px;
  border-radius: 3px;
  background: var(--bg-subtle);
  white-space: nowrap;
}

/* Compact lists */
.compact-list { list-style: none; }
.compact-list li {
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: flex-start;
  gap: 6px;
  font-size: 13px;
  line-height: 1.35;
}
.compact-list li:last-child { border-bottom: none; }

.priority-tag {
  display: inline-block;
  padding: 1px 4px;
  border-radius: 3px;
  font-size: 10px;
  font-weight: 600;
  font-family: ui-monospace, monospace;
  flex-shrink: 0;
  color: white;
}
.priority-P0 { background: #dc2626; }
.priority-P1 { background: #d97706; }
.priority-P2 { background: #64748b; }

.question-bullet { color: var(--fg-muted); flex-shrink: 0; }

/* Activity drill-down (per-device commit observations, today + yesterday) */
.activity-details td {
  background: var(--bg-subtle);
  padding: 10px 14px;
  white-space: normal;
}
.activity-details-inner {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
.activity-day h4 {
  font-size: 10px;
  text-transform: uppercase;
  color: var(--fg-muted);
  margin-bottom: 6px;
  font-weight: 600;
  letter-spacing: 0.05em;
}
.activity-day ul { list-style: none; }
.activity-day li {
  padding: 3px 0;
  font-size: 12px;
  line-height: 1.4;
  display: flex;
  gap: 8px;
  align-items: baseline;
}
.activity-day li .commit-time {
  font-family: ui-monospace, monospace;
  font-size: 11px;
  color: var(--fg-muted);
  flex-shrink: 0;
  width: 52px;
}
.activity-day li .commit-subject {
  color: var(--fg);
  word-break: break-word;
}
.activity-day .empty {
  font-size: 11px;
  color: var(--fg-muted);
  font-style: italic;
}

.expand-chevron {
  display: inline-block;
  transition: transform 0.1s;
  color: var(--fg-muted);
}
.expand-chevron.open { transform: rotate(90deg); }

/* Responsive */
@media (max-width: 1279px) {
  .grid { grid-template-columns: 1.6fr 1fr; }
  .col-stage { width: 130px; }
  .stage-step { min-width: 14px; }
  .stage-checklist { gap: 3px; }
  .stage-abbr { font-size: 8px; }
  .col-policy-wide { width: 220px; }
  .col-metric { width: 150px; }
}

@media (max-width: 899px) {
  html, body { overflow: auto; height: auto; }
  .grid {
    grid-template-columns: 1fr;
    grid-template-areas:
      "pipeline"
      "tasks"
      "questions"
      "activity";
    grid-template-rows: none;
    gap: 6px;
    padding: 6px;
  }
  .panel { max-height: 50vh; }
  /* Hide Who + Last-change columns on narrow panels */
  .col-who { display: none; }
  .col-activity { display: none; }
  /* Shrink activity table cols */
  .col-device { width: 160px; }
  .col-policy-wide { width: 160px; }
  .col-metric { width: 120px; }
}

@media (max-width: 599px) {
  .topbar { padding: 6px 10px; height: 44px; }
  .panel-header { padding: 6px 10px; }
  .data-table th, .data-table td { padding: 4px 8px; }
  .panel { max-height: 40vh; }
  /* On narrow mobile, hide abbreviations and shrink to dots only */
  .col-stage { width: 90px; }
  .stage-abbr { display: none; }
  .stage-step { min-width: 12px; }
  .stage-checklist { gap: 2px; }
  .col-upd { display: none; }
  /* Hide Policy on mobile to keep Device + metrics visible */
  .col-policy-wide { display: none; }
  .col-metric { width: 90px; font-size: 11px; }
  .activity-details-inner { grid-template-columns: 1fr; }
}
