/* ============================================================
   Agent-Loop Timeline Renderer
   ============================================================
   Vertical timeline for multi-step governed reasoning loops.
   Each step shows governance verdict, confidence, and optional
   revision when a step was blocked and retried.

   Uses design-system tokens (--space-*, --radius-*, --text-*).
   Dark theme by default; light theme via body[data-theme="light"].
   ============================================================ */

/* ── Timeline container ─────────────────────────────────────── */
.agent-loop-timeline {
  position: relative;
  padding: 0 0 0 40px;
  margin: var(--space-4, 16px) 0;
}

/* Vertical line */
.agent-loop-timeline::before {
  content: '';
  position: absolute;
  left: 18px;
  top: 0;
  bottom: 0;
  width: 2px;
  background: rgba(99, 102, 241, 0.2);
  border-radius: 1px;
}

/* ── Plan header ────────────────────────────────────────────── */
.al-plan-header {
  margin-bottom: var(--space-4, 16px);
  padding: var(--space-3, 12px) var(--space-4, 16px);
  background: rgba(99, 102, 241, 0.08);
  border: 1px solid rgba(99, 102, 241, 0.2);
  border-radius: var(--radius-md, 8px);
  font-size: var(--text-sm, 14px);
  color: var(--text-secondary, #9ca3af);
  line-height: 1.5;
}

.al-plan-header strong {
  color: var(--text-primary, #e5e7eb);
  font-weight: 600;
}

/* ── Progress indicator ─────────────────────────────────────── */
.al-progress {
  display: flex;
  align-items: center;
  gap: var(--space-2, 8px);
  padding: var(--space-2, 8px) var(--space-3, 12px);
  margin-bottom: var(--space-3, 12px);
  font-family: 'IBM Plex Mono', monospace;
  font-size: var(--text-xs, 12px);
  color: rgba(99, 102, 241, 0.8);
  letter-spacing: 0.04em;
}

/* Equalizer-style loader (never use spinners) */
.al-equalizer {
  display: inline-flex;
  align-items: flex-end;
  gap: 2px;
  height: 14px;
}

.al-equalizer span {
  display: block;
  width: 3px;
  background: rgba(99, 102, 241, 0.7);
  border-radius: 1px;
  animation: al-eq 0.8s ease-in-out infinite;
}

.al-equalizer span:nth-child(1) { height: 6px; animation-delay: 0s; }
.al-equalizer span:nth-child(2) { height: 10px; animation-delay: 0.15s; }
.al-equalizer span:nth-child(3) { height: 4px; animation-delay: 0.3s; }
.al-equalizer span:nth-child(4) { height: 8px; animation-delay: 0.1s; }

@keyframes al-eq {
  0%, 100% { transform: scaleY(0.4); }
  50% { transform: scaleY(1); }
}

/* ── Step card ──────────────────────────────────────────────── */
.al-step {
  position: relative;
  margin-bottom: var(--space-4, 16px);
  padding: var(--space-4, 16px);
  background: var(--bg-tertiary, #1f2937);
  border: 1px solid var(--border-primary, #1a2444);
  border-radius: var(--radius-md, 8px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  animation: al-step-in 0.3s ease-out both;
}

@keyframes al-step-in {
  from { opacity: 0; transform: translateX(-12px); }
  to   { opacity: 1; transform: translateX(0); }
}

/* Step number badge (circle on the timeline) */
.al-step-badge {
  position: absolute;
  left: -34px;
  top: var(--space-4, 16px);
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'IBM Plex Mono', monospace;
  font-size: var(--text-xs, 12px);
  font-weight: 600;
  color: #fff;
  background: var(--bg-secondary, #111827);
  border: 2px solid rgba(99, 102, 241, 0.4);
  z-index: 1;
}

/* Active step: pulsing ring */
.al-step-badge.active {
  border-color: rgba(99, 102, 241, 0.8);
  animation: al-pulse-ring 1.5s ease-in-out infinite;
}

@keyframes al-pulse-ring {
  0%, 100% { box-shadow: 0 0 0 0 rgba(99, 102, 241, 0.4); }
  50% { box-shadow: 0 0 0 6px rgba(99, 102, 241, 0); }
}

/* Completed: ALLOWED */
.al-step.verdict-allowed .al-step-badge {
  background: #10b981;
  border-color: #10b981;
}

/* Completed: BLOCKED */
.al-step.verdict-blocked .al-step-badge {
  background: #ef4444;
  border-color: #ef4444;
}

/* Completed: MODIFIED */
.al-step.verdict-modified .al-step-badge {
  background: #f59e0b;
  border-color: #f59e0b;
}

/* Revised step: orange badge */
.al-step.was-revised .al-step-badge {
  background: #f59e0b;
  border-color: #f59e0b;
}

/* ── Step header (title + governance badge) ──────────────────── */
.al-step-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-3, 12px);
  margin-bottom: var(--space-3, 12px);
}

.al-step-title {
  font-size: var(--text-sm, 14px);
  font-weight: 500;
  color: var(--text-primary, #e5e7eb);
  line-height: 1.4;
  flex: 1;
}

/* ── Governance verdict badge ────────────────────────────────── */
.al-gov-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 10px;
  border-radius: 999px;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  white-space: nowrap;
  flex-shrink: 0;
}

.al-gov-badge svg {
  width: 12px;
  height: 12px;
  flex-shrink: 0;
}

.al-gov-badge.allowed {
  background: rgba(16, 185, 129, 0.12);
  color: #10b981;
  border: 1px solid rgba(16, 185, 129, 0.3);
}

.al-gov-badge.blocked {
  background: rgba(239, 68, 68, 0.12);
  color: #ef4444;
  border: 1px solid rgba(239, 68, 68, 0.3);
}

.al-gov-badge.modified {
  background: rgba(245, 158, 11, 0.12);
  color: #f59e0b;
  border: 1px solid rgba(245, 158, 11, 0.3);
}

/* ── Step body (execution output, collapsible) ───────────────── */
.al-step-body {
  font-size: var(--text-sm, 14px);
  color: var(--text-secondary, #9ca3af);
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

.al-step-body.collapsed {
  max-height: 80px;
  overflow: hidden;
  position: relative;
}

.al-step-body.collapsed::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 32px;
  background: linear-gradient(transparent, var(--bg-tertiary, #1f2937));
  pointer-events: none;
}

.al-step-toggle {
  display: inline-block;
  margin-top: var(--space-2, 8px);
  padding: 0;
  background: none;
  border: none;
  color: rgba(99, 102, 241, 0.7);
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  cursor: pointer;
  letter-spacing: 0.04em;
}

.al-step-toggle:hover {
  color: rgba(99, 102, 241, 1);
}

/* ── Revision display ────────────────────────────────────────── */
.al-revision-block {
  margin-top: var(--space-3, 12px);
  padding-top: var(--space-3, 12px);
  border-top: 1px dashed rgba(239, 68, 68, 0.3);
}

.al-original {
  opacity: 0.45;
  text-decoration: line-through;
  font-size: var(--text-sm, 14px);
  color: var(--text-muted, #6b7280);
  line-height: 1.5;
  margin-bottom: var(--space-2, 8px);
}

.al-revision-label {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px;
  margin-bottom: var(--space-2, 8px);
  border-radius: 999px;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  background: rgba(245, 158, 11, 0.12);
  color: #f59e0b;
  border: 1px solid rgba(245, 158, 11, 0.3);
}

.al-revised-output {
  font-size: var(--text-sm, 14px);
  color: var(--text-primary, #e5e7eb);
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ── Light-mode overrides ────────────────────────────────────── */
body[data-theme="light"] .agent-loop-timeline::before {
  background: rgba(99, 102, 241, 0.25);
}

body[data-theme="light"] .al-plan-header {
  background: rgba(99, 102, 241, 0.06);
  border-color: rgba(99, 102, 241, 0.2);
  color: #4a4a5a;
}

body[data-theme="light"] .al-plan-header strong {
  color: #1a1a2e;
}

body[data-theme="light"] .al-step {
  background: #ffffff;
  border-color: #e0d9cf;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
}

body[data-theme="light"] .al-step-badge {
  background: #f4efe5;
  color: #3730a3;
}

body[data-theme="light"] .al-step-title {
  color: #1a1a2e;
}

body[data-theme="light"] .al-step-body {
  color: #4a4a5a;
}

body[data-theme="light"] .al-step-body.collapsed::after {
  background: linear-gradient(transparent, #ffffff);
}

body[data-theme="light"] .al-original {
  color: #8a847d;
}

body[data-theme="light"] .al-revised-output {
  color: #1a1a2e;
}

body[data-theme="light"] .al-progress {
  color: rgba(79, 70, 229, 0.85);
}

.al-revision-reason {
  margin-top: var(--space-2, 8px);
  font-size: 11px;
  color: var(--text-muted, #6b7280);
  font-style: italic;
}

/* ── Confidence bar ──────────────────────────────────────────── */
.al-confidence-bar-wrap {
  margin-top: var(--space-3, 12px);
  display: flex;
  align-items: center;
  gap: var(--space-2, 8px);
}

.al-confidence-label {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  color: var(--text-muted, #6b7280);
  letter-spacing: 0.04em;
  white-space: nowrap;
  min-width: 80px;
}

.al-confidence-track {
  flex: 1;
  height: 4px;
  background: rgba(255, 255, 255, 0.06);
  border-radius: 2px;
  overflow: hidden;
}

.al-confidence-fill {
  height: 100%;
  border-radius: 2px;
  transition: width 0.4s ease-out;
}

.al-confidence-value {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  font-weight: 600;
  min-width: 36px;
  text-align: right;
}

/* ── Final recommendation card ───────────────────────────────── */
.al-final {
  position: relative;
  margin-top: var(--space-5, 20px);
  padding: var(--space-5, 20px);
  background: var(--bg-tertiary, #1f2937);
  border: 1px solid rgba(99, 102, 241, 0.3);
  border-radius: var(--radius-md, 8px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
  animation: al-step-in 0.3s ease-out both;
}

.al-final-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-4, 16px);
}

.al-final-title {
  font-size: var(--text-base, 16px);
  font-weight: 600;
  color: var(--text-primary, #e5e7eb);
}

.al-final-confidence {
  display: flex;
  align-items: baseline;
  gap: 4px;
}

.al-final-conf-num {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 28px;
  font-weight: 700;
  line-height: 1;
}

.al-final-conf-pct {
  font-family: 'IBM Plex Mono', monospace;
  font-size: var(--text-sm, 14px);
  color: var(--text-muted, #6b7280);
}

.al-final-body {
  font-size: var(--text-sm, 14px);
  color: var(--text-secondary, #9ca3af);
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
  margin-bottom: var(--space-4, 16px);
}

/* ── Governance summary stats ────────────────────────────────── */
.al-gov-summary {
  display: flex;
  gap: var(--space-4, 16px);
  padding-top: var(--space-3, 12px);
  border-top: 1px solid var(--border-primary, #1a2444);
  flex-wrap: wrap;
}

.al-gov-stat {
  display: flex;
  align-items: center;
  gap: var(--space-2, 8px);
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  letter-spacing: 0.04em;
}

.al-gov-stat-label {
  color: var(--text-muted, #6b7280);
  text-transform: uppercase;
}

.al-gov-stat-value {
  font-weight: 600;
  color: var(--text-primary, #e5e7eb);
}

.al-gov-stat-value.green  { color: #10b981; }
.al-gov-stat-value.red    { color: #ef4444; }
.al-gov-stat-value.amber  { color: #f59e0b; }

/* ── Metadata row (trace_id, duration, llm calls) ────────────── */
.al-meta-row {
  display: flex;
  gap: var(--space-4, 16px);
  margin-top: var(--space-3, 12px);
  flex-wrap: wrap;
}

.al-meta-item {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  color: var(--text-muted, #6b7280);
  letter-spacing: 0.03em;
}

.al-meta-item span {
  color: var(--text-secondary, #9ca3af);
}

/* ── Left-border coloring by verdict ─────────────────────────── */
.al-step.verdict-allowed { border-left: 3px solid #10b981; }
.al-step.verdict-blocked { border-left: 3px solid #ef4444; }
.al-step.verdict-modified { border-left: 3px solid #f59e0b; }

/* ── Authority / Advisory distinction badges ─────────────────── */
/* These are also defined in cognitive-demo.html's <style> block.
   Duplicated here so the renderer works standalone on other pages. */
.verdict-authority-badge {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #4a90e2;
  background: rgba(74, 144, 226, 0.12);
  border: 1px solid rgba(74, 144, 226, 0.3);
  border-radius: 4px;
  padding: 2px 7px;
  margin-left: 8px;
  vertical-align: middle;
}
.advisory-badge {
  display: inline-block;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: #8a94aa;
  background: rgba(138, 148, 170, 0.10);
  border: 1px solid rgba(138, 148, 170, 0.25);
  border-radius: 4px;
  padding: 2px 7px;
  margin-left: 8px;
  vertical-align: middle;
}

/* ── Mobile responsive (< 720px) ────────────────────────────── */
@media (max-width: 720px) {
  .agent-loop-timeline {
    padding-left: 28px;
  }

  .agent-loop-timeline::before {
    left: 10px;
  }

  .al-step-badge {
    left: -22px;
    width: 22px;
    height: 22px;
    font-size: 10px;
  }

  .al-step {
    padding: var(--space-3, 12px);
  }

  .al-step-header {
    flex-direction: column;
    gap: var(--space-2, 8px);
  }

  .al-gov-summary {
    flex-direction: column;
    gap: var(--space-2, 8px);
  }

  .al-final-header {
    flex-direction: column;
    gap: var(--space-2, 8px);
    align-items: flex-start;
  }

  .al-meta-row {
    flex-direction: column;
    gap: var(--space-1, 4px);
  }
}

/* ── Light theme overrides ───────────────────────────────────── */
body[data-theme="light"] .al-plan-header {
  background: rgba(99, 102, 241, 0.06);
  border-color: rgba(99, 102, 241, 0.15);
  color: #6b7280;
}

body[data-theme="light"] .al-plan-header strong {
  color: #1a1a1a;
}

body[data-theme="light"] .al-step {
  background: #ffffff;
  border-color: #e5e7eb;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
}

body[data-theme="light"] .al-step-title {
  color: #1a1a1a;
}

body[data-theme="light"] .al-step-body {
  color: #4b5563;
}

body[data-theme="light"] .al-step-body.collapsed::after {
  background: linear-gradient(transparent, #ffffff);
}

body[data-theme="light"] .al-confidence-track {
  background: rgba(0, 0, 0, 0.06);
}

body[data-theme="light"] .al-final {
  background: #ffffff;
  border-color: rgba(99, 102, 241, 0.25);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

body[data-theme="light"] .al-final-title {
  color: #1a1a1a;
}

body[data-theme="light"] .al-final-body {
  color: #4b5563;
}

body[data-theme="light"] .al-gov-summary {
  border-top-color: #e5e7eb;
}

body[data-theme="light"] .al-original {
  color: #9ca3af;
}

body[data-theme="light"] .al-revised-output {
  color: #1a1a1a;
}

body[data-theme="light"] .al-step-badge {
  background: #ffffff;
}

body[data-theme="light"] .agent-loop-timeline::before {
  background: rgba(99, 102, 241, 0.15);
}
