/* tutti factory floor — industrial dark theme per DESIGN.md */

/* ── Self-hosted fonts ── */
@font-face {
  font-family: 'Instrument Serif';
  src: url('/fonts/instrument-serif-regular.woff2') format('woff2');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: 'Instrument Serif';
  src: url('/fonts/instrument-serif-italic.woff2') format('woff2');
  font-weight: 400;
  font-style: italic;
  font-display: swap;
}
@font-face {
  font-family: 'DM Sans';
  src: url('/fonts/dm-sans-latin.woff2') format('woff2');
  font-weight: 400 700;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: 'JetBrains Mono';
  src: url('/fonts/jetbrains-mono-latin.woff2') format('woff2');
  font-weight: 400 600;
  font-style: normal;
  font-display: swap;
}

/* ── Design system tokens (DESIGN.md) ── */
:root {
  --bg: #0a0f1e;
  --surface: #111827;
  --surface-elevated: #1a2236;
  --border: #1f2937;
  --border-active: #374151;
  --text: #f9fafb;
  --text-secondary: #9ca3af;
  --dim: #6b7280;
  --accent: #6366f1;
  --working: #22c55e;
  --idle: #6b7280;
  --stopped: #374151;
  --blocked: #ef4444;
  --auth-fail: #f59e0b;
  --flow-line: #1f2937;
  --flow-active: #22c55e;
  --font-display: 'Instrument Serif', Georgia, 'Times New Roman', serif;
  --font-body: 'DM Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', 'SF Mono', 'Fira Code', 'Cascadia Code', monospace;
}

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

body {
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

/* ── top bar ── */
header#top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.bar-left { display: flex; gap: 12px; align-items: baseline; }
.bar-right { display: flex; gap: 16px; align-items: center; }
.logo {
  font-family: var(--font-display);
  font-weight: 400;
  font-size: 1.25rem;
  letter-spacing: -0.02em;
  color: var(--text);
}
.dim { color: var(--dim); font-family: var(--font-mono); font-size: 0.8125rem; }
.hud-item {
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  color: var(--dim);
  letter-spacing: 0.02em;
}
.hud-item.warn { color: var(--auth-fail); }
.hud-item.alert { color: var(--blocked); }
.conn-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--stopped);
  display: inline-block;
  flex-shrink: 0;
}
.conn-dot.connected { background: var(--working); }
.conn-dot.error { background: var(--blocked); }

/* ── pipeline / factory floor ── */
main#factory {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 48px 20px 24px;
  overflow-x: auto;
}

.pipeline {
  display: flex;
  align-items: center;
  gap: 0;
  min-height: 160px;
}

/* flow connector between stages */
.flow-connector {
  width: 40px;
  height: 2px;
  background: var(--flow-line);
  position: relative;
  flex-shrink: 0;
}
.flow-connector.active {
  background: var(--flow-active);
  box-shadow: 0 0 6px rgba(34,197,94,0.3);
}
.flow-connector::after {
  content: "";
  position: absolute;
  right: -4px;
  top: -3px;
  border: 4px solid transparent;
  border-left-color: inherit;
}
.artifact-label {
  position: absolute;
  top: -18px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 10px;
  color: var(--flow-active);
  background: var(--bg);
  padding: 0 4px;
  white-space: nowrap;
  font-family: var(--font-mono);
  opacity: 0.85;
}

/* stage card — the "machine" on the floor */
.stage {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 20px 16px;
  min-width: 140px;
  text-align: center;
  position: relative;
  transition: border-color 0.25s ease-out, box-shadow 0.25s ease-out;
  flex-shrink: 0;
  cursor: pointer;
}
.stage:hover { border-color: var(--border-active); }
.stage.selected { border-color: var(--accent); box-shadow: 0 0 12px rgba(99,102,241,0.2); }

.stage .stage-name {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 8px;
}

.stage .state-chip {
  display: inline-block;
  font-family: var(--font-mono);
  font-size: 0.625rem;
  font-weight: 500;
  padding: 2px 8px;
  border-radius: 4px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stage .agent-runtime {
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  color: var(--dim);
  margin-top: 6px;
}

/* state treatments */
.stage.working {
  border-color: var(--working);
  box-shadow: 0 0 16px rgba(34,197,94,0.12);
}
.stage.working .state-chip { background: rgba(34,197,94,0.15); color: var(--working); }

.stage.idle { border-color: var(--border); }
.stage.idle .state-chip { background: rgba(107,114,128,0.15); color: var(--idle); }

.stage.stopped { border-color: var(--stopped); opacity: 0.5; }
.stage.stopped .state-chip { background: rgba(55,65,81,0.3); color: var(--dim); }

.stage.blocked {
  border-color: var(--blocked);
  box-shadow: 0 0 16px rgba(239,68,68,0.15);
}
.stage.blocked .state-chip { background: rgba(239,68,68,0.15); color: var(--blocked); }

.stage.auth-fail {
  border-color: var(--auth-fail);
  box-shadow: 0 0 16px rgba(245,158,11,0.15);
}
.stage.auth-fail .state-chip { background: rgba(245,158,11,0.15); color: var(--auth-fail); }

/* pulse animation for working stages */
@keyframes pulse-border {
  0%, 100% { box-shadow: 0 0 12px rgba(34,197,94,0.08); }
  50% { box-shadow: 0 0 24px rgba(34,197,94,0.2); }
}
.stage.working { animation: pulse-border 2s ease-in-out infinite; }

/* ── run dots — work items flowing through the pipeline ── */
.run-dots {
  display: flex;
  gap: 6px;
  justify-content: center;
  margin-top: 8px;
  min-height: 12px;
}
.run-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}
.run-dot-active {
  background: var(--working);
  animation: dot-pulse 1.2s ease-in-out infinite;
}
.run-dot-failed {
  background: var(--blocked);
  box-shadow: 0 0 6px rgba(239,68,68,0.4);
}
@keyframes dot-pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.6; transform: scale(0.85); }
}

/* flow connector with animated dash when carrying a run */
.flow-connector.flowing {
  background: repeating-linear-gradient(
    90deg,
    var(--flow-active) 0px,
    var(--flow-active) 6px,
    transparent 6px,
    transparent 12px
  );
  background-size: 24px 2px;
  animation: flow-march 0.6s linear infinite;
}
@keyframes flow-march {
  from { background-position: 0 0; }
  to { background-position: 24px 0; }
}

/* HUD active-run indicator */
.hud-item.active-run { color: var(--working); }

/* empty/unknown stage */
.stage.empty {
  border-style: dashed;
  border-color: var(--border);
  opacity: 0.35;
  cursor: default;
}
.stage.empty .state-chip { background: transparent; color: var(--dim); border: 1px solid var(--dim); }

/* ── detail drawer ── */
#detail-drawer {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 16px 20px;
  display: none;
  max-height: 240px;
  overflow-y: auto;
}
#detail-drawer.open { display: block; }
#detail-drawer .detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}
#detail-drawer .detail-agent-name {
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
#detail-drawer .detail-close {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--dim);
  cursor: pointer;
  border: none;
  background: none;
  padding: 4px 8px;
}
#detail-drawer .detail-close:hover { color: var(--text); }
#detail-drawer .detail-meta {
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  color: var(--text-secondary);
  margin-bottom: 12px;
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}
#detail-drawer .detail-meta span { color: var(--dim); }
#detail-drawer .detail-events { list-style: none; }
#detail-drawer .detail-events li {
  padding: 3px 0;
  font-size: 0.75rem;
  color: var(--dim);
  border-bottom: 1px solid var(--border);
  font-family: var(--font-mono);
}
#detail-drawer .detail-events li:last-child { border-bottom: none; }
#detail-drawer .detail-events .evt-type { color: var(--text-secondary); font-weight: 500; }
#detail-drawer .detail-events .evt-time { float: right; font-size: 0.625rem; }

/* step timeline rows */
.step-row {
  display: flex;
  align-items: baseline;
  gap: 8px;
  flex-wrap: wrap;
}
.step-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  font-size: 0.5625rem;
  font-weight: 600;
  flex-shrink: 0;
  border: 1px solid var(--border);
  color: var(--dim);
}
.step-badge.step-ok { border-color: var(--working); color: var(--working); }
.step-badge.step-fail { border-color: var(--blocked); color: var(--blocked); background: rgba(239,68,68,0.1); }
.step-badge.step-running { border-color: var(--working); color: var(--working); animation: dot-pulse 1.2s ease-in-out infinite; }
.step-desc { color: var(--text-secondary); flex: 1; min-width: 0; }
.step-dur { color: var(--dim); font-size: 0.625rem; flex-shrink: 0; }
.step-dur.running-text { color: var(--working); }
.step-msg {
  width: 100%;
  color: var(--blocked);
  font-size: 0.625rem;
  padding: 2px 0 2px 28px;
  overflow-wrap: anywhere;
}

/* selected run dot */
.run-dot-selected {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ── dispatch panel ── */
#dispatch-panel {
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding: 8px 20px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.dispatch-toggle {
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--accent);
  background: rgba(99,102,241,0.1);
  border: 1px solid var(--accent);
  border-radius: 4px;
  padding: 4px 12px;
  cursor: pointer;
  flex-shrink: 0;
}
.dispatch-toggle:hover { background: rgba(99,102,241,0.2); }
.dispatch-form {
  display: none;
  align-items: center;
  gap: 8px;
  flex: 1;
}
.dispatch-form.open { display: flex; }
.dispatch-form select,
.dispatch-form input {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 8px;
}
.dispatch-form select { min-width: 140px; }
.dispatch-form input { width: 100px; }
.dispatch-form select:focus,
.dispatch-form input:focus { border-color: var(--accent); outline: none; }
#dispatch-go {
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--bg);
  background: var(--working);
  border: none;
  border-radius: 4px;
  padding: 5px 14px;
  cursor: pointer;
}
#dispatch-go:hover { background: #16a34a; }
#dispatch-go:disabled { opacity: 0.5; cursor: not-allowed; }
.dispatch-status {
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  color: var(--dim);
}
.dispatch-status.ok { color: var(--working); }
.dispatch-status.err { color: var(--blocked); }

/* ── agent focus mode ── */
#focus-view {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 100;
  flex-direction: column;
  animation: focus-enter 250ms ease-out;
}
@keyframes focus-enter {
  from { opacity: 0; }
  to { opacity: 1; }
}
.focus-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  font-family: var(--font-mono);
  font-size: 0.6875rem;
}
.focus-back {
  color: var(--accent);
  background: none;
  border: none;
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  padding: 4px 8px;
}
.focus-back:hover { text-decoration: underline; }
.focus-agent-name {
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-size: 0.8125rem;
}
.focus-status {
  font-size: 0.6875rem;
}
.focus-status.working { color: var(--working); }
.focus-status.stopped { color: var(--dim); }
.focus-status.blocked { color: var(--blocked); }
.focus-meta {
  color: var(--dim);
  margin-left: auto;
}
.focus-grid {
  display: grid;
  grid-template-columns: 1fr 280px;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}
.focus-terminal {
  background: var(--bg);
  border-right: 1px solid var(--border);
  padding: 12px;
  overflow-y: auto;
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.6;
  color: var(--text-secondary);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.focus-terminal .term-tool { color: var(--working); }
.focus-terminal .term-prompt { color: var(--accent); }
.focus-terminal .term-empty {
  color: var(--dim);
  text-align: center;
  padding-top: 48px;
  font-size: 0.8125rem;
}
.focus-terminal .term-empty button {
  display: block;
  margin: 12px auto 0;
  background: var(--working);
  color: var(--bg);
  border: none;
  border-radius: 4px;
  padding: 6px 16px;
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  cursor: pointer;
}
.focus-sidebar {
  background: var(--surface);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
.focus-section {
  padding: 12px;
  border-bottom: 1px solid var(--border);
}
.focus-section h3 {
  font-family: var(--font-mono);
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--dim);
  margin-bottom: 8px;
}
.focus-stat-row {
  display: flex;
  justify-content: space-between;
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  padding: 2px 0;
}
.focus-stat-label { color: var(--dim); }
.focus-stat-value { color: var(--text); font-weight: 500; }
.focus-stat-value.green { color: var(--working); }
.focus-stat-value.amber { color: var(--auth-fail); }
.focus-stat-value.red { color: var(--blocked); }
/* context fill bar */
.focus-ctx-bar {
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  margin-top: 4px;
  overflow: hidden;
}
.focus-ctx-fill {
  height: 100%;
  border-radius: 2px;
  transition: width 0.3s ease-out;
}
/* diff in sidebar */
.focus-diff {
  font-family: var(--font-mono);
  font-size: 10px;
  line-height: 1.5;
  max-height: 200px;
  overflow-y: auto;
}
.focus-diff-empty { color: var(--dim); font-size: 0.6875rem; }
.focus-diff-add { color: var(--working); background: rgba(34,197,94,0.08); padding: 0 4px; }
.focus-diff-del { color: var(--blocked); background: rgba(239,68,68,0.08); padding: 0 4px; }
.focus-diff-hunk { color: var(--accent); font-weight: 500; }
.focus-diff-file { color: var(--text-secondary); font-weight: 600; padding: 4px 0 2px; }
/* prompt bar */
.focus-prompt-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.focus-prompt-bar input {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 10px;
  border-radius: 6px;
  font-family: var(--font-mono);
  font-size: 12px;
}
.focus-prompt-bar input:focus { border-color: var(--accent); outline: none; }
.focus-send {
  background: var(--working);
  color: var(--bg);
  border: none;
  padding: 6px 14px;
  border-radius: 6px;
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  cursor: pointer;
  flex-shrink: 0;
}
.focus-send:hover { background: #16a34a; }
.focus-send:disabled { opacity: 0.5; cursor: not-allowed; }
.focus-send.sending { animation: dot-pulse 1.2s ease-in-out infinite; }

/* ── focus mobile ── */
@media (max-width: 767px) {
  .focus-grid {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr auto;
  }
  .focus-terminal {
    border-right: none;
    border-bottom: 1px solid var(--border);
    min-height: 50vh;
  }
  .focus-sidebar {
    flex-direction: row;
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    border-bottom: 1px solid var(--border);
  }
  .focus-section {
    min-width: 80vw;
    scroll-snap-align: start;
    flex-shrink: 0;
  }
  .focus-prompt-bar { padding: 8px; }
  .focus-prompt-bar input { font-size: 16px; /* prevent iOS zoom */ }
}

/* ── event timeline ── */
footer#timeline {
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding: 12px 20px;
  max-height: 160px;
  overflow-y: auto;
}
footer#timeline h3 {
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--dim);
  margin-bottom: 8px;
}
#event-list {
  list-style: none;
  font-family: var(--font-mono);
  font-size: 0.75rem;
}
#event-list li {
  padding: 3px 0;
  color: var(--dim);
  border-bottom: 1px solid var(--border);
}
#event-list li:last-child { border-bottom: none; }
#event-list .evt-type { color: var(--text-secondary); font-weight: 500; }
#event-list .evt-agent { color: var(--working); }
#event-list .evt-time { float: right; font-size: 0.625rem; }

/* ── mobile ── */
@media (max-width: 640px) {
  header#top-bar { flex-wrap: wrap; gap: 8px; padding: 12px 16px; }
  .bar-right { gap: 12px; }
  main#factory { padding: 16px 12px; }
  .pipeline {
    flex-direction: column;
    align-items: stretch;
    gap: 0;
  }
  .flow-connector {
    width: 2px;
    height: 24px;
    align-self: center;
  }
  .flow-connector::after {
    right: auto;
    bottom: -4px;
    top: auto;
    left: -3px;
    border-color: transparent;
    border-top-color: inherit;
  }
  .stage { min-width: auto; min-height: 44px; }
  .flow-connector.flowing {
    background: repeating-linear-gradient(
      180deg,
      var(--flow-active) 0px,
      var(--flow-active) 6px,
      transparent 6px,
      transparent 12px
    );
    background-size: 2px 24px;
    animation: flow-march-v 0.6s linear infinite;
  }
  @keyframes flow-march-v {
    from { background-position: 0 0; }
    to { background-position: 0 24px; }
  }
  #detail-drawer { max-height: 50vh; }
  footer#timeline { max-height: 120px; }
}
