/* ============================================================
   Sweep Design System
   Custom CSS with CSS custom properties. No frameworks.
   GitHub-dark inspired. Built for developer tools.
   ============================================================ */

/* ── 1. Tokens ─────────────────────────────────────────────── */

:root {
  --bg-0: #0d1117;
  --bg-1: #161b22;
  --bg-2: #1c2333;
  --bg-3: #21262d;
  --border: #30363d;
  --border-active: #484f58;
  --text-1: #e6edf3;
  --text-2: #8b949e;
  --text-3: #6e7681;
  --accent: #2f81f7;
  --accent-hover: #388bfd;
  --green: #3fb950;
  --red: #f85149;
  --orange: #d29922;
  --yellow: #e3b341;
  --purple: #8957e5;
  --teal: #3fb9a5;
  --cyan: #58a6ff;
  --sp-1: 4px; --sp-2: 8px; --sp-3: 12px; --sp-4: 16px;
  --sp-5: 20px; --sp-6: 24px; --sp-8: 32px;
  --btn-h: 28px; --btn-h-sm: 24px;
  --input-h: 32px; --input-h-sm: 28px;
  --nav-h: 48px;
  --radius: 4px; --radius-md: 6px; --radius-lg: 8px;
  --font: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'SFMono-Regular', 'Menlo', 'Monaco', 'Consolas', monospace;
  --transition: 150ms ease;
}

/* ── 2. Reset ──────────────────────────────────────────────── */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { scroll-behavior: smooth; }
body {
  background: var(--bg-1);
  color: var(--text-1);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ── 3. Base Elements ──────────────────────────────────────── */

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

h1 { font-size: 24px; font-weight: 600; }
h2 { font-size: 20px; font-weight: 600; }
h3 { font-size: 16px; font-weight: 600; }

code {
  font-family: var(--font-mono);
  font-size: 0.9em;
  background: var(--bg-0);
  padding: 2px 6px;
  border-radius: 3px;
}
pre { font-family: var(--font-mono); font-size: 12px; }
hr { border: none; border-top: 1px solid var(--border); margin: var(--sp-4) 0; }
img { max-width: 100%; display: block; }

/* ── 4. Forms ──────────────────────────────────────────────── */

button, input, select, textarea {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}

input[type="text"], input[type="email"], input[type="password"],
input[type="number"], input[type="url"], input[type="search"],
select, textarea {
  height: var(--input-h);
  width: 100%;
  background: var(--bg-0);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0 var(--sp-3);
  color: var(--text-1);
  transition: border-color var(--transition);
}
input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  outline: 2px solid rgba(47,129,247,0.3);
  outline-offset: -1px;
}
textarea { height: auto; min-height: 64px; padding: var(--sp-2) var(--sp-3); resize: vertical; }
select { cursor: pointer; }

input[type="checkbox"] { accent-color: var(--accent); width: 14px; height: 14px; cursor: pointer; }

.input-sm {
  height: var(--input-h-sm);
  font-size: 12px;
  padding: 0 var(--sp-2);
}
.select-sm {
  height: var(--input-h-sm);
  font-size: 12px;
  padding: 0 var(--sp-2);
  width: auto !important;
  min-width: 100px;
  max-width: 200px;
}

.form-label { display: block; font-size: 12px; color: var(--text-2); margin-bottom: var(--sp-1); }
.form-group { display: flex; flex-direction: column; gap: var(--sp-1); }

*:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
/* Override for inputs which have their own focus style */
input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline-offset: -1px;
}

dialog {
  background: var(--bg-2);
  color: var(--text-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 0;
  max-width: 480px;
  width: 90vw;
  box-shadow: 0 16px 48px rgba(0,0,0,0.4);
}
dialog::backdrop { background: rgba(0,0,0,0.6); }
.modal-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: var(--sp-4) var(--sp-5);
  border-bottom: 1px solid var(--border);
}
.modal-header h3 { margin: 0; font-size: 15px; }
.modal-body { padding: var(--sp-5); }
.modal-body > * + * { margin-top: var(--sp-3); }
.modal-footer {
  display: flex; justify-content: flex-end; gap: var(--sp-2);
  padding: var(--sp-4) var(--sp-5);
  border-top: 1px solid var(--border);
}

details { border: 1px solid var(--border); border-radius: var(--radius-md); }
summary {
  padding: var(--sp-2) var(--sp-3);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-2);
  user-select: none;
}
summary:hover { color: var(--text-1); }
details[open] summary { border-bottom: 1px solid var(--border); }
details > :not(summary) { padding: var(--sp-3); }

/* ── 5. Nav ────────────────────────────────────────────────── */

.nav {
  height: var(--nav-h);
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 var(--sp-6);
  gap: var(--sp-6);
  position: sticky;
  top: 0;
  z-index: 100;
}
.nav .logo { font-size: 15px; font-weight: 700; color: var(--accent); }
.nav a { color: var(--text-2); font-size: 13px; transition: color var(--transition); }
.nav a:hover { color: var(--text-1); }
.nav a.active { color: var(--text-1); box-shadow: inset 0 -2px 0 var(--accent); }

/* ── 6. Buttons ────────────────────────────────────────────── */

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  height: var(--btn-h); padding: 0 var(--sp-3);
  font-size: 12px; font-weight: 500; white-space: nowrap;
  border-radius: var(--radius); border: 1px solid transparent;
  cursor: pointer; transition: all var(--transition);
  background: var(--bg-3); color: var(--text-1);
  text-decoration: none;
  line-height: 1;
}
.btn:hover { background: var(--border); }
.btn-primary { background: var(--accent); color: #fff; border-color: transparent; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-success { background: var(--green); color: var(--bg-0); }
.btn-success:hover { opacity: 0.9; }
.btn-danger { background: var(--red); color: #fff; }
.btn-danger:hover { opacity: 0.9; }
.btn-ghost { background: transparent; color: var(--text-2); border-color: var(--border); }
.btn-ghost:hover { background: var(--bg-3); color: var(--text-1); }
.btn-sm { height: var(--btn-h-sm); padding: 0 var(--sp-2); font-size: 11px; }
.btn-icon { width: var(--btn-h); padding: 0; }
.btn-icon.btn-sm { width: var(--btn-h-sm); }

/* ── 7. Badges ─────────────────────────────────────────────── */

.badge {
  display: inline-flex; align-items: center;
  height: 20px; padding: 0 6px;
  font-size: 11px; font-weight: 600;
  border-radius: 3px;
  text-transform: uppercase; letter-spacing: 0.02em;
  white-space: nowrap;
}
.badge-critical { background: var(--red); color: #fff; }
.badge-high { background: var(--orange); color: #fff; }
.badge-medium { background: var(--yellow); color: var(--bg-0); }
.badge-low { background: var(--text-3); color: var(--bg-0); }
.badge-info { background: var(--cyan); color: var(--bg-0); }
.badge-scanned, .badge-created { background: var(--text-3); color: #fff; }
.badge-evaluated { background: var(--accent); color: #fff; }
.badge-approved { background: var(--green); color: var(--bg-0); }
.badge-implementing { background: var(--orange); color: #fff; }
.badge-completed { background: var(--teal); color: var(--bg-0); }
.badge-rejected { background: var(--red); color: #fff; }
.badge-dismissed { background: var(--bg-3); color: var(--text-2); }
.badge-scanner { background: var(--purple); color: #fff; }
.badge-manual { background: var(--teal); color: var(--bg-0); }

/* ── 8. Cards ──────────────────────────────────────────────── */

.card {
  background: var(--bg-0);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--sp-4);
}
.card-header {
  display: flex; justify-content: space-between; align-items: center;
  padding-bottom: var(--sp-3);
  border-bottom: 1px solid var(--border);
  margin-bottom: var(--sp-3);
  font-weight: 600; font-size: 13px;
}

/* ── 9. Tables ─────────────────────────────────────────────── */

.table { width: 100%; border-collapse: collapse; }
.table th {
  font-size: 11px; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--text-3);
  padding: var(--sp-2) var(--sp-3);
  border-bottom: 1px solid var(--border);
  text-align: left;
}
.table td {
  font-size: 13px;
  padding: var(--sp-2) var(--sp-3);
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.table tbody tr:nth-child(even) { background: rgba(255,255,255,0.015); }
.table tbody tr:hover { background: rgba(255,255,255,0.05); }
.table a { color: var(--text-1); }
.table a:hover { color: var(--accent); }

/* ── 10. Pipeline Bar ──────────────────────────────────────── */

.pipeline-bar {
  display: flex; height: 38px;
  border-radius: var(--radius-lg); overflow: hidden;
  background: var(--bg-3);
}
.pipeline-seg {
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 600; color: #fff;
  min-width: 2px; transition: all var(--transition);
  cursor: pointer; position: relative;
}
.pipeline-seg:hover { filter: brightness(1.15); }
.pipeline-seg.active { filter: brightness(1.3); box-shadow: inset 0 -2px 0 #fff; }
.pipeline-seg.scanned, .pipeline-seg.created { background: var(--text-3); }
.pipeline-seg.evaluated { background: var(--accent); }
.pipeline-seg.approved { background: var(--green); }
.pipeline-seg.implementing { background: var(--orange); }
.pipeline-seg.completed { background: var(--teal); }
.pipeline-seg.rejected { background: var(--red); }
.pipeline-seg.dismissed { background: var(--bg-3); }

/* ── 11. Code Blocks ───────────────────────────────────────── */

.code-block {
  background: var(--bg-0);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--sp-3);
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.6;
}
.code-block .line { white-space: pre; }
.code-block .line.matched {
  background: rgba(248,81,73,0.12);
  border-left: 3px solid var(--red);
  padding-left: var(--sp-2);
  margin-left: calc(var(--sp-2) * -1);
}
.code-block .line-num {
  color: var(--text-3); user-select: none;
  display: inline-block; width: 3rem;
  text-align: right; margin-right: var(--sp-3);
}

/* ── 12. Stat Cards ────────────────────────────────────────── */

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: var(--sp-3);
}
.stat-card {
  background: var(--bg-0);
  border: 1px solid var(--border);
  border-top: 3px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--sp-4);
  text-align: center;
  transition: box-shadow var(--transition), border-color var(--transition);
}
.stat-card:hover { box-shadow: 0 4px 12px rgba(0,0,0,0.3); border-color: var(--border-active); }
.stat-accent-blue { border-top-color: var(--accent); }
.stat-accent-orange { border-top-color: var(--orange); }
.stat-accent-green { border-top-color: var(--green); }
.stat-accent-teal { border-top-color: var(--teal); }
.stat-card .number { font-size: 24px; font-weight: 700; line-height: 1; }
.stat-card .label {
  font-size: 11px; color: var(--text-3); margin-top: var(--sp-1);
  text-transform: uppercase; letter-spacing: 0.04em;
}

/* ── 13. Bar Charts ────────────────────────────────────────── */

.bar-chart .bar-row { display: flex; align-items: center; gap: var(--sp-2); margin-bottom: var(--sp-1); }
.bar-chart .bar-label { width: 120px; font-size: 12px; text-align: right; color: var(--text-2); flex-shrink: 0; }
.bar-chart .bar-fill { height: 18px; background: var(--accent); border-radius: 3px; transition: width 300ms; }
.bar-chart .bar-value { font-size: 12px; color: var(--text-2); min-width: 2rem; }

/* ── 14. Misc Components ───────────────────────────────────── */

.filter-bar { display: flex; gap: var(--sp-3); flex-wrap: wrap; align-items: end; }
.filter-bar select, .select-sm { height: var(--input-h-sm); font-size: 12px; padding: 0 var(--sp-2); width: auto !important; min-width: 120px; max-width: 200px; }
.filter-bar .form-group { min-width: 0; }

.source-tabs { display: flex; gap: var(--sp-1); }
.source-tabs button {
  padding: var(--sp-1) var(--sp-3);
  border-radius: 12px; font-size: 12px;
  color: var(--text-2); border: 1px solid var(--border);
  background: transparent; cursor: pointer;
  transition: all var(--transition);
}
.source-tabs button:hover { background: var(--bg-3); color: var(--text-1); }
.source-tabs button.active { background: var(--bg-3); color: var(--text-1); border-color: var(--border-active); }

.prompt-box {
  background: var(--bg-0); border: 1px solid var(--border);
  border-radius: var(--radius-md); padding: var(--sp-3);
  white-space: pre-wrap; font-family: var(--font-mono);
  font-size: 12px; max-height: 400px; overflow-y: auto;
  position: relative;
}
.copy-btn {
  position: absolute; top: var(--sp-2); right: var(--sp-2);
}

.section-title {
  font-size: 15px; font-weight: 600;
  padding-bottom: var(--sp-2);
  border-bottom: 1px solid var(--border);
  margin-bottom: var(--sp-4);
  margin-top: var(--sp-8);
}
.section-title:first-child { margin-top: 0; }

/* Section spacing */
.section + .section { margin-top: 32px; }

/* Pipeline legend */
.pipeline-legend {
  display: flex; gap: var(--sp-4); margin-top: var(--sp-2);
  font-size: 12px; color: var(--text-2);
}
.pipeline-legend span { display: flex; align-items: center; gap: var(--sp-1); }
.pipeline-legend i {
  display: inline-block; width: 10px; height: 10px; border-radius: 2px;
}

/* Welcome state */
.welcome {
  text-align: center; padding: 64px 24px;
  max-width: 480px; margin: 0 auto;
}
.welcome h1 { margin-bottom: var(--sp-4); }
.welcome p { color: var(--text-2); margin-bottom: var(--sp-4); line-height: 1.7; }
.welcome .steps {
  text-align: left; margin: var(--sp-6) auto;
  max-width: 320px; color: var(--text-2);
}
.welcome .steps li { margin-bottom: var(--sp-2); }
.welcome .btn { margin-top: var(--sp-4); padding: 0 var(--sp-6); height: 36px; font-size: 14px; }

.flash { padding: var(--sp-3) var(--sp-4); border-radius: var(--radius-md); font-size: 13px; }
.flash-success { background: rgba(63,185,80,0.1); border: 1px solid var(--green); color: var(--green); }
.flash-error { background: rgba(248,81,73,0.1); border: 1px solid var(--red); color: var(--red); }

.empty-state { text-align: center; padding: var(--sp-8); color: var(--text-3); }

/* Inline row actions */
.row-actions { display: flex; gap: 2px; }
.row-actions .btn-icon { width: 24px; height: 24px; font-size: 13px; }

/* ── 15. Layout Utilities ──────────────────────────────────── */

.container { max-width: 1280px; margin: 0 auto; padding: var(--sp-6); }

/* Toolbar container for filter bars */
.toolbar {
  background: var(--bg-0);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--sp-2) var(--sp-3);
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  flex-wrap: wrap;
}
.toolbar .btn { flex-shrink: 0; }

/* Scout popover */
.scout-popover {
  position: absolute; top: calc(100% + 4px); right: 0; z-index: 50;
  background: var(--bg-2); border: 1px solid var(--border);
  border-radius: var(--radius-md); padding: var(--sp-3);
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  min-width: 220px;
}
.flex { display: flex; }
.flex-col { flex-direction: column; }
.flex-wrap { flex-wrap: wrap; }
.items-center { align-items: center; }
.items-start { align-items: flex-start; }
.justify-between { justify-content: space-between; }
.justify-end { justify-content: flex-end; }
.justify-center { justify-content: center; }
.gap-1 { gap: var(--sp-1); }
.gap-2 { gap: var(--sp-2); }
.gap-3 { gap: var(--sp-3); }
.gap-4 { gap: var(--sp-4); }
.gap-6 { gap: var(--sp-6); }
.grid { display: grid; }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.w-full { width: 100%; }
.p-2 { padding: var(--sp-2); }
.p-3 { padding: var(--sp-3); }
.p-4 { padding: var(--sp-4); }
.px-3 { padding-left: var(--sp-3); padding-right: var(--sp-3); }
.py-2 { padding-top: var(--sp-2); padding-bottom: var(--sp-2); }
.py-4 { padding-top: var(--sp-4); padding-bottom: var(--sp-4); }
.mt-2 { margin-top: var(--sp-2); }
.mt-3 { margin-top: var(--sp-3); }
.mt-4 { margin-top: var(--sp-4); }
.mb-2 { margin-bottom: var(--sp-2); }
.mb-3 { margin-bottom: var(--sp-3); }
.mb-4 { margin-bottom: var(--sp-4); }
.text-xs { font-size: 11px; }
.text-sm { font-size: 12px; }
.text-base { font-size: 13px; }
.text-md { font-size: 14px; }
.text-lg { font-size: 16px; }
.text-xl { font-size: 20px; }
.text-muted { color: var(--text-3); }
.text-secondary { color: var(--text-2); }
.text-right { text-align: right; }
.text-center { text-align: center; }
.font-semibold { font-weight: 600; }
.font-bold { font-weight: 700; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hidden { display: none; }
.cursor-pointer { cursor: pointer; }

/* ── 16. Loading & Button States ────────────────────────────── */

.loading { animation: pulse 1.5s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: 0.4; } 50% { opacity: 1; } }

.btn.is-loading {
  opacity: 0.7; pointer-events: none; position: relative;
}
.btn.is-loading::after {
  content: ""; position: absolute; right: 8px;
  width: 12px; height: 12px;
  border: 2px solid transparent; border-top-color: currentColor;
  border-radius: 50%; animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* HTMX loading indicator */
.htmx-request { opacity: 0.6; transition: opacity 200ms; }
.htmx-request .btn { pointer-events: none; }

/* ── 17. Responsive ────────────────────────────────────────── */

@media (max-width: 768px) {
  .container { padding: var(--sp-3); }
  .nav { padding: 0 var(--sp-3); gap: var(--sp-3); overflow-x: auto; }
  .nav .logo { font-size: 14px; }
  .nav a { font-size: 12px; white-space: nowrap; }
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
  .filter-bar { flex-direction: column; align-items: stretch; }
  .filter-bar select { width: 100%; }
  .source-tabs { flex-wrap: wrap; }
  .pipeline-legend { flex-wrap: wrap; gap: var(--sp-2); }
  .table { font-size: 12px; }
  .table th, .table td { padding: var(--sp-1) var(--sp-2); }
  h1 { font-size: 20px; }
  h2 { font-size: 18px; }
  .flex.justify-between { flex-wrap: wrap; gap: var(--sp-2); }
  .code-block { font-size: 11px; }
  .modal-body .grid-2 { grid-template-columns: 1fr; }
}

@media (max-width: 480px) {
  .stat-grid { grid-template-columns: 1fr 1fr; gap: var(--sp-2); }
  .stat-card { padding: var(--sp-3); }
  .stat-card .number { font-size: 20px; }
  .badge { font-size: 10px; padding: 0 4px; height: 18px; }
  .table th:nth-child(n+4), .table td:nth-child(n+4) { display: none; }
}

/* ── 18. Better Empty States ───────────────────────────────── */

.empty-state {
  text-align: center; padding: 48px 24px;
  color: var(--text-3);
}
.empty-state .empty-icon {
  font-size: 48px; margin-bottom: var(--sp-4); opacity: 0.3;
}
.empty-state p { margin-bottom: var(--sp-2); }
.empty-state .btn { margin-top: var(--sp-4); }

/* ── 19. Visual Fixes ──────────────────────────────────────── */

/* Badge contrast fix */
.badge-dismissed { background: var(--bg-3); color: var(--text-1); border: 1px solid var(--border); }

/* Consistent table action buttons */
.table .btn { font-size: 12px; }

/* Card hover */
.card { transition: border-color var(--transition), box-shadow var(--transition); }
.card:hover { border-color: var(--border-active); box-shadow: 0 2px 8px rgba(0,0,0,0.2); }

/* Smooth page transitions */
main { animation: fadeIn 150ms ease; }
@keyframes fadeIn { from { opacity: 0.8; } to { opacity: 1; } }

/* Better scrollbar for dark theme */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--bg-0); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--border-active); }

/* Toast notifications */
.toast {
  position: fixed; bottom: 24px; right: 24px;
  padding: var(--sp-3) var(--sp-4);
  border-radius: var(--radius-md);
  font-size: 13px; z-index: 1000;
  animation: slideUp 300ms ease;
  max-width: 400px;
}
.toast-success { background: var(--green); color: var(--bg-0); }
.toast-error { background: var(--red); color: #fff; }
@keyframes slideUp { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

/* ── Agent Terminal Viewer ────────────────────────────────── */
.agent-terminal {
  background: var(--bg-0);
  color: var(--text-2);
  font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: 13px;
  line-height: 1.5;
  padding: 12px;
  border-radius: var(--radius-md);
  max-height: 400px;
  overflow-y: auto;
  white-space: pre-wrap;
  border: 1px solid var(--border);
}
.agent-terminal .tool-call {
  color: var(--accent);
  opacity: 0.7;
  font-style: italic;
}
