/* ============================================================
   TOKENS
   ============================================================ */
:root {
  --primary:        #4f46e5;
  --primary-dark:   #4338ca;
  --primary-light:  #eef2ff;
  --primary-muted:  #a5b4fc;

  --success:        #059669;
  --success-light:  #ecfdf5;
  --success-dark:   #064e3b;
  --success-muted:  #6ee7b7;

  --warning:        #d97706;
  --warning-light:  #fffbeb;
  --warning-dark:   #78350f;

  --danger:         #dc2626;
  --danger-light:   #fef2f2;
  --danger-dark:    #7f1d1d;

  --info:           #0891b2;
  --info-light:     #ecfeff;

  --gray-50:  #f8fafc;
  --gray-100: #f1f5f9;
  --gray-200: #e2e8f0;
  --gray-300: #cbd5e1;
  --gray-400: #94a3b8;
  --gray-500: #64748b;
  --gray-600: #475569;
  --gray-700: #334155;
  --gray-800: #1e293b;
  --gray-900: #0f172a;

  --surface:  #ffffff;
  --bg:       #f0f4f8;
  --border:   #e2e8f0;

  --sidebar-bg:     #1e293b;
  --sidebar-hover:  rgba(255,255,255,0.07);
  --sidebar-active: #4f46e5;

  --radius:    8px;
  --radius-lg: 12px;
  --radius-xl: 16px;
  --radius-full: 9999px;

  --shadow-sm: 0 1px 2px rgba(0,0,0,.05);
  --shadow:    0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
  --shadow-md: 0 4px 6px rgba(0,0,0,.07), 0 2px 4px rgba(0,0,0,.06);
  --shadow-lg: 0 10px 25px rgba(0,0,0,.12), 0 4px 6px rgba(0,0,0,.05);

  --t: 150ms ease;
  --sidebar-w: 240px;
}

/* ============================================================
   RESET
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 15px; scroll-behavior: smooth; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, Roboto, sans-serif;
  background: var(--bg);
  color: var(--gray-800);
  line-height: 1.6;
  height: 100vh;
  overflow: hidden;
}
a { text-decoration: none; color: inherit; }
button { cursor: pointer; font-family: inherit; }
input, textarea, select { font-family: inherit; }
p { line-height: 1.7; }

/* ============================================================
   LAYOUT SHELL
   ============================================================ */
.shell {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width var(--t);
  z-index: 100;
}
.sidebar.collapsed { width: 60px; }
.sidebar.collapsed .brand-text,
.sidebar.collapsed .nav-link span:not(.nav-badge),
.sidebar.collapsed .status-info { display: none; }
.sidebar.collapsed .nav-link { justify-content: center; }
.sidebar.collapsed .nav-icon { margin: 0; }
.sidebar.collapsed .sidebar-footer { justify-content: center; }
.sidebar.collapsed .api-status { display: none; }

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 18px 16px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  flex-shrink: 0;
}
.brand-icon {
  width: 32px; height: 32px;
  background: var(--primary);
  border-radius: var(--radius);
  display: grid;
  place-items: center;
  font-size: 18px;
  color: white;
  flex-shrink: 0;
}
.brand-name { font-size: .9rem; font-weight: 700; color: #e2e8f0; }
.brand-sub  { font-size: .72rem; color: #64748b; display: block; }

/* Nav */
.nav {
  flex: 1;
  padding: 10px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow-y: auto;
}
.nav-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: var(--radius);
  color: #94a3b8;
  font-size: .88rem;
  font-weight: 500;
  transition: background var(--t), color var(--t);
  position: relative;
  white-space: nowrap;
}
.nav-link:hover { background: var(--sidebar-hover); color: #e2e8f0; }
.nav-link.active { background: var(--primary); color: #fff; }
.nav-link-accent { color: #818cf8; }
.nav-link-accent:hover { color: #a5b4fc; }
.nav-icon { width: 18px; height: 18px; flex-shrink: 0; }
.nav-badge {
  margin-left: auto;
  background: var(--primary);
  color: white;
  font-size: .7rem;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: var(--radius-full);
  min-width: 20px;
  text-align: center;
}
.nav-link.active .nav-badge { background: rgba(255,255,255,0.25); }

/* Sidebar footer */
.sidebar-footer {
  padding: 12px;
  border-top: 1px solid rgba(255,255,255,0.08);
  display: flex;
  align-items: center;
  gap: 8px;
}
.api-status {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}
.status-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--gray-500);
  transition: background var(--t);
}
.status-dot.ok      { background: var(--success-muted); box-shadow: 0 0 0 2px rgba(110,231,183,.2); }
.status-dot.error   { background: #f87171; }
.status-dot.loading { background: #fbbf24; }
.status-info { min-width: 0; }
.status-label { font-size: .68rem; color: var(--gray-500); display: block; text-transform: uppercase; letter-spacing: .05em; }
.status-val   { font-size: .78rem; color: #94a3b8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
.icon-btn {
  background: none;
  border: none;
  color: #64748b;
  padding: 6px;
  border-radius: var(--radius);
  display: grid;
  place-items: center;
  transition: background var(--t), color var(--t);
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--sidebar-hover); color: #94a3b8; }

/* Content wrapper */
.content-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

/* Topbar */
.topbar {
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 20px;
  gap: 14px;
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
}
.menu-btn {
  background: none;
  border: none;
  color: var(--gray-500);
  padding: 6px;
  border-radius: var(--radius);
  display: grid;
  place-items: center;
  transition: background var(--t);
}
.menu-btn:hover { background: var(--gray-100); }
.breadcrumb {
  flex: 1;
  font-size: .88rem;
  color: var(--gray-500);
  display: flex;
  align-items: center;
  gap: 6px;
}
.breadcrumb .crumb      { color: var(--gray-700); font-weight: 500; }
.breadcrumb .sep        { color: var(--gray-300); }
.topbar-end { display: flex; align-items: center; gap: 8px; margin-left: auto; }
.api-chip {
  font-size: .75rem;
  background: var(--gray-100);
  color: var(--gray-500);
  padding: 4px 10px;
  border-radius: var(--radius-full);
  border: 1px solid var(--border);
  cursor: pointer;
  transition: background var(--t);
  font-family: monospace;
}
.api-chip:hover { background: var(--gray-200); }

/* Main content */
#app {
  flex: 1;
  overflow-y: auto;
  padding: 28px;
}

/* ============================================================
   UTILITIES
   ============================================================ */
.hidden { display: none !important; }
.text-muted  { color: var(--gray-500); }
.text-small  { font-size: .82rem; }
.text-center { text-align: center; }
.mt-4  { margin-top: 1rem; }
.mt-6  { margin-top: 1.5rem; }
.mb-4  { margin-bottom: 1rem; }
.gap-3 { gap: .75rem; }

/* Spinner */
.spinner {
  width: 24px; height: 24px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .6s linear infinite;
  display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 80px 20px;
  color: var(--gray-500);
}

/* Empty state */
.empty-state {
  text-align: center;
  padding: 80px 20px;
  color: var(--gray-500);
}
.empty-icon {
  font-size: 3rem;
  margin-bottom: 16px;
  opacity: .4;
}
.empty-state h3 { font-size: 1.1rem; color: var(--gray-700); margin-bottom: 8px; }

/* ============================================================
   TYPOGRAPHY
   ============================================================ */
.page-title {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--gray-900);
  margin-bottom: 4px;
}
.page-subtitle {
  font-size: .88rem;
  color: var(--gray-500);
  margin-bottom: 24px;
}
.section-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--gray-700);
  margin-bottom: 12px;
}

/* ============================================================
   CARDS
   ============================================================ */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}
.card-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.card-title {
  font-size: .95rem;
  font-weight: 600;
  color: var(--gray-800);
  display: flex;
  align-items: center;
  gap: 8px;
}
.card-body { padding: 20px; }

/* ============================================================
   BUTTONS
   ============================================================ */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: var(--radius);
  font-size: .88rem;
  font-weight: 500;
  border: 1.5px solid transparent;
  cursor: pointer;
  transition: all var(--t);
  white-space: nowrap;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); }
.btn-secondary { background: var(--surface); color: var(--gray-700); border-color: var(--border); }
.btn-secondary:hover:not(:disabled) { background: var(--gray-50); border-color: var(--gray-300); }
.btn-danger { background: var(--danger); color: white; }
.btn-danger:hover:not(:disabled) { background: var(--danger-dark); }
.btn-ghost { background: none; color: var(--gray-600); border-color: transparent; }
.btn-ghost:hover:not(:disabled) { background: var(--gray-100); }
.btn-sm { padding: 5px 10px; font-size: .8rem; }
.btn-lg { padding: 10px 22px; font-size: .95rem; }
.btn-icon-only {
  padding: 7px;
  border-radius: var(--radius);
  background: var(--gray-100);
  border: 1px solid var(--border);
  color: var(--gray-600);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: all var(--t);
}
.btn-icon-only:hover { background: var(--gray-200); }

/* ============================================================
   BADGES
   ============================================================ */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: var(--radius-full);
  font-size: .72rem;
  font-weight: 600;
  white-space: nowrap;
}
.badge-primary { background: var(--primary-light); color: var(--primary); }
.badge-success { background: var(--success-light); color: var(--success); }
.badge-warning { background: var(--warning-light); color: var(--warning); }
.badge-danger  { background: var(--danger-light);  color: var(--danger); }
.badge-gray    { background: var(--gray-100);       color: var(--gray-600); }
.badge-info    { background: var(--info-light);     color: var(--info); }

/* Run status badges */
.status-badge { display: inline-flex; align-items: center; gap: 5px; }
.status-badge::before {
  content: '';
  width: 7px; height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
}
.status-pending::before  { background: var(--gray-400); }
.status-running::before  { background: var(--warning); animation: pulse 1.2s ease infinite; }
.status-completed::before{ background: var(--success); }
.status-failed::before   { background: var(--danger); }
.status-cancelled::before{ background: var(--gray-400); }
@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:.4} }

/* ============================================================
   FORMS
   ============================================================ */
.form-group { margin-bottom: 16px; }
.form-label {
  display: block;
  font-size: .82rem;
  font-weight: 600;
  color: var(--gray-700);
  margin-bottom: 5px;
}
.form-hint { font-size: .78rem; color: var(--gray-500); margin-top: 4px; }
.form-input, .form-textarea, .form-select {
  width: 100%;
  padding: 9px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  font-size: .9rem;
  color: var(--gray-800);
  background: var(--surface);
  transition: border-color var(--t), box-shadow var(--t);
  outline: none;
}
.form-input:focus, .form-textarea:focus, .form-select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79,70,229,.1);
}
.form-textarea { resize: vertical; min-height: 100px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

/* Tabs */
.tab-group {
  display: flex;
  border-bottom: 2px solid var(--border);
  gap: 2px;
  margin-bottom: 24px;
}
.tab-btn {
  padding: 10px 18px;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  font-size: .88rem;
  font-weight: 500;
  color: var(--gray-500);
  cursor: pointer;
  margin-bottom: -2px;
  transition: color var(--t), border-color var(--t);
}
.tab-btn:hover   { color: var(--gray-700); }
.tab-btn.active  { color: var(--primary); border-color: var(--primary); }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* File drop zone */
.drop-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius-lg);
  padding: 32px;
  text-align: center;
  cursor: pointer;
  transition: border-color var(--t), background var(--t);
  background: var(--gray-50);
}
.drop-zone:hover, .drop-zone.drag-over {
  border-color: var(--primary);
  background: var(--primary-light);
}
.drop-zone input[type="file"] { display: none; }
.drop-zone-icon { font-size: 2rem; margin-bottom: 10px; }
.drop-zone-text { font-size: .88rem; color: var(--gray-600); }
.drop-zone-hint { font-size: .78rem; color: var(--gray-400); margin-top: 4px; }
.drop-zone.has-file { border-color: var(--success); background: var(--success-light); }
.drop-zone.has-file .drop-zone-text { color: var(--success); font-weight: 600; }

/* ============================================================
   MODAL
   ============================================================ */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15,23,42,.5);
  z-index: 1000;
  display: grid;
  place-items: center;
  padding: 20px;
  backdrop-filter: blur(4px);
  animation: fadeIn .15s ease;
}
.modal-backdrop.hidden { display: none; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.modal-box {
  background: var(--surface);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 520px;
  animation: slideUp .2s ease;
  overflow: hidden;
}
.modal-box.modal-lg { max-width: 720px; }
@keyframes slideUp { from { transform: translateY(12px); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-header {
  padding: 18px 22px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-header h3 { font-size: 1rem; font-weight: 600; }
.modal-close {
  background: none;
  border: none;
  color: var(--gray-400);
  font-size: 1.2rem;
  cursor: pointer;
  line-height: 1;
  padding: 4px;
  border-radius: 4px;
  transition: color var(--t);
}
.modal-close:hover { color: var(--gray-700); }
.modal-body { padding: 22px; }
.modal-footer {
  padding: 14px 22px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

/* ============================================================
   ALERTS / TOASTS
   ============================================================ */
.alert {
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: .88rem;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  border: 1px solid;
}
.alert-success { background: var(--success-light); border-color: #a7f3d0; color: var(--success-dark); }
.alert-danger  { background: var(--danger-light);  border-color: #fecaca; color: var(--danger-dark); }
.alert-warning { background: var(--warning-light); border-color: #fde68a; color: var(--warning-dark); }
.alert-info    { background: var(--info-light);    border-color: #a5f3fc; color: var(--info); }

/* ============================================================
   DASHBOARD
   ============================================================ */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 28px;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-sm);
}
.stat-icon { font-size: 1.8rem; margin-bottom: 10px; }
.stat-val  { font-size: 2rem; font-weight: 700; color: var(--gray-900); line-height: 1; }
.stat-label { font-size: .8rem; color: var(--gray-500); margin-top: 4px; }

/* ============================================================
   REPORTS LIST
   ============================================================ */
.reports-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 18px;
}
.report-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-sm);
  cursor: pointer;
  transition: box-shadow var(--t), border-color var(--t), transform var(--t);
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.report-card:hover {
  box-shadow: var(--shadow-md);
  border-color: var(--primary-muted);
  transform: translateY(-1px);
}
.report-card-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.report-commune { font-size: 1.05rem; font-weight: 700; color: var(--gray-900); }
.report-title-badge { font-size: .72rem; background: var(--primary-light); color: var(--primary); padding: 2px 8px; border-radius: var(--radius-full); font-weight: 600; white-space: nowrap; }
.report-meta { display: flex; gap: 8px; flex-wrap: wrap; }
.report-footer { display: flex; gap: 8px; justify-content: flex-end; padding-top: 10px; border-top: 1px solid var(--border); }

/* ============================================================
   RUN MONITOR
   ============================================================ */
.monitor-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  margin-bottom: 20px;
}
.monitor-header {
  padding: 20px 24px;
  background: linear-gradient(135deg, var(--gray-800), var(--gray-900));
  color: white;
}
.monitor-run-id { font-size: .75rem; font-family: monospace; opacity: .5; }
.monitor-commune { font-size: 1.4rem; font-weight: 700; margin-top: 4px; }
.monitor-meta { font-size: .82rem; opacity: .7; margin-top: 2px; }

.progress-bar-wrap { margin: 20px 24px 0; }
.progress-label { font-size: .78rem; opacity: .7; margin-bottom: 6px; display: flex; justify-content: space-between; }
.progress-track {
  height: 6px;
  background: rgba(255,255,255,.15);
  border-radius: var(--radius-full);
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary-muted), #818cf8);
  border-radius: var(--radius-full);
  transition: width .4s ease;
}

.agents-steps {
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.agent-step {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-radius: var(--radius);
  transition: background var(--t);
  font-size: .88rem;
}
.agent-step.done    { color: var(--gray-700); }
.agent-step.active  { background: var(--primary-light); color: var(--primary-dark); font-weight: 600; }
.agent-step.pending { color: var(--gray-400); }
.step-icon {
  width: 24px; height: 24px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: .8rem;
  flex-shrink: 0;
}
.step-icon.done    { background: var(--success-light); color: var(--success); }
.step-icon.active  { background: var(--primary); color: white; }
.step-icon.pending { background: var(--gray-100); color: var(--gray-400); }
.agent-step.active .step-icon .spinner { width: 14px; height: 14px; border-width: 2px; border-top-color: white; }

.event-log {
  border-top: 1px solid var(--border);
  max-height: 420px;
  overflow-y: auto;
  background: var(--gray-50);
}
.event-log-title {
  padding: 10px 20px;
  font-size: .75rem;
  font-weight: 600;
  color: var(--gray-500);
  text-transform: uppercase;
  letter-spacing: .06em;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--gray-50);
}
.event-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 7px 20px;
  border-bottom: 1px solid var(--border);
  font-size: .8rem;
  animation: slideIn .15s ease;
}
@keyframes slideIn { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }
.event-time { color: var(--gray-400); font-family: monospace; white-space: nowrap; flex-shrink: 0; padding-top: 1px; }

/* icon column */
.ev-icon { flex-shrink: 0; width: 16px; text-align: center; font-size: .85rem; padding-top: 1px; }

/* body column */
.ev-body  { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.ev-main  { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; min-width: 0; }
.ev-label { font-weight: 600; white-space: nowrap; }
.ev-detail { color: var(--gray-500); font-size: .78rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 380px; }

/* expand button */
.ev-expand-btn {
  background: none; border: none; cursor: pointer;
  font-size: .72rem; color: var(--primary); padding: 0 2px;
  text-decoration: underline; flex-shrink: 0;
}
.ev-expand-btn:hover { color: var(--primary-dark); }

/* expanded generated text */
.ev-output {
  font-size: .78rem; color: var(--gray-700); line-height: 1.55;
  background: white; border: 1px solid var(--border);
  border-radius: var(--radius); padding: 8px 12px;
  white-space: pre-wrap; word-break: break-word;
  margin-top: 2px;
}
.ev-output.hidden { display: none; }

/* semantic colours */
.ev-ok   { color: var(--success); }
.ev-err  { color: var(--danger); }
.ev-warn { color: var(--warning); }
.ev-info { color: var(--info); }
.ev-run  { color: var(--primary); }
.ev-llm  { color: #7c3aed; }
.ev-dim  { color: var(--gray-400); }

/* ============================================================
   REPORT DETAIL — HEADER
   ============================================================ */
.report-detail-header {
  background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
  border-radius: var(--radius-xl);
  padding: 28px 32px;
  color: white;
  margin-bottom: 24px;
  box-shadow: var(--shadow-lg);
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  flex-wrap: wrap;
}
.rh-left {}
.rh-tag    { font-size: .72rem; text-transform: uppercase; letter-spacing: .1em; color: var(--primary-muted); font-weight: 600; }
.rh-commune { font-size: 2rem; font-weight: 800; line-height: 1.1; margin: 6px 0; }
.rh-meta   { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 10px; }
.rh-pill   {
  display: inline-flex; align-items: center; gap: 6px;
  background: rgba(255,255,255,.1);
  border: 1px solid rgba(255,255,255,.15);
  padding: 5px 12px; border-radius: var(--radius-full);
  font-size: .8rem;
}
.rh-actions { display: flex; gap: 10px; align-items: flex-start; }
.rh-actions .btn { background: rgba(255,255,255,.1); color: white; border-color: rgba(255,255,255,.2); }
.rh-actions .btn:hover { background: rgba(255,255,255,.2); }

/* Portrait card */
.portrait-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  overflow: hidden;
  margin-bottom: 24px;
  box-shadow: var(--shadow-sm);
}
.portrait-header {
  padding: 16px 24px;
  background: linear-gradient(90deg, var(--primary-light), var(--surface));
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
}
.portrait-header h3 { font-size: .95rem; font-weight: 700; color: var(--primary-dark); }
.portrait-body { padding: 24px; }
.portrait-text {
  font-size: .95rem;
  line-height: 1.8;
  color: var(--gray-700);
  border-left: 3px solid var(--primary);
  padding-left: 16px;
}

/* Validation card */
.validation-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 18px 24px;
  margin-bottom: 24px;
  display: flex;
  align-items: center;
  gap: 20px;
  box-shadow: var(--shadow-sm);
}
.validation-score {
  font-size: 2rem;
  font-weight: 800;
  color: var(--success);
}
.validation-info { flex: 1; }
.validation-label { font-weight: 600; color: var(--gray-700); }
.validation-notes { font-size: .82rem; color: var(--gray-500); margin-top: 2px; }

/* ============================================================
   CATEGORY ACCORDION
   ============================================================ */
.categories-list { display: flex; flex-direction: column; gap: 12px; }
.category-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  transition: box-shadow var(--t);
}
.category-section:has(.category-body.open) { box-shadow: var(--shadow-md); }

.category-toggle {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 18px 22px;
  background: none;
  border: none;
  text-align: left;
  cursor: pointer;
  transition: background var(--t);
}
.category-toggle:hover { background: var(--gray-50); }
.cat-emoji { font-size: 1.4rem; flex-shrink: 0; }
.cat-info  { flex: 1; min-width: 0; }
.cat-name  { font-size: 1rem; font-weight: 700; color: var(--gray-800); }
.cat-count { font-size: .78rem; color: var(--gray-500); margin-top: 1px; }
.cat-arrow {
  color: var(--gray-400);
  transition: transform var(--t);
  flex-shrink: 0;
  font-size: .9rem;
}
.category-body.open ~ .category-toggle .cat-arrow,
.category-toggle[aria-expanded="true"] .cat-arrow { transform: rotate(180deg); }

.category-body { display: none; }
.category-body.open { display: block; }

.synthesis-section {
  padding: 18px 22px 14px;
  background: var(--gray-50);
  border-bottom: 1px solid var(--border);
}
.synthesis-label {
  font-size: .72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--primary);
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.synthesis-text {
  font-size: .9rem;
  line-height: 1.75;
  color: var(--gray-700);
}

/* ============================================================
   INDICATOR CARDS
   ============================================================ */
.indicators-grid {
  padding: 18px 22px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
  gap: 14px;
}

.indicator-card {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  transition: box-shadow var(--t), border-color var(--t);
  background: var(--surface);
}
.indicator-card:hover { box-shadow: var(--shadow-md); border-color: var(--gray-300); }

/* Header row */
.ic-header {
  padding: 12px 14px 10px;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  justify-content: space-between;
  border-bottom: 1px solid var(--gray-100);
}
.ic-header-left { flex: 1; min-width: 0; }
.ic-name {
  font-size: .88rem;
  font-weight: 700;
  color: var(--gray-800);
  line-height: 1.3;
  margin-bottom: 4px;
}
.ic-tags { display: flex; gap: 4px; flex-wrap: wrap; }
.ic-tag { font-size: .68rem; padding: 1px 6px; border-radius: var(--radius-full); font-weight: 600; }
.ic-tag-certain   { background: var(--success-light); color: var(--success); }
.ic-tag-uncertain { background: var(--warning-light); color: var(--warning); }
.ic-tag-year      { background: var(--gray-100); color: var(--gray-600); }

/* Gap badge in header */
.gap-badge {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  border-radius: var(--radius-full);
  font-size: .78rem;
  font-weight: 700;
  flex-shrink: 0;
  white-space: nowrap;
}
.gap-pos  { background: var(--success-light); color: var(--success-dark); }
.gap-neg  { background: var(--danger-light);  color: var(--danger-dark); }
.gap-zero { background: var(--gray-100);      color: var(--gray-600); }
.gap-none { background: var(--info-light);    color: var(--info); }

/* Comparison bars */
.ic-comparison {
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  border-bottom: 1px solid var(--gray-100);
}
.bar-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.bar-label {
  font-size: .72rem;
  color: var(--gray-500);
  width: 70px;
  flex-shrink: 0;
  text-align: right;
  font-weight: 500;
}
.bar-track {
  flex: 1;
  height: 8px;
  background: var(--gray-100);
  border-radius: var(--radius-full);
  overflow: hidden;
}
.bar {
  height: 100%;
  border-radius: var(--radius-full);
  transition: width .5s ease;
  min-width: 2px;
}
.bar-commune   { background: linear-gradient(90deg, var(--primary), var(--primary-muted)); }
.bar-reference { background: linear-gradient(90deg, var(--gray-300), var(--gray-400)); }
.bar-value {
  font-size: .78rem;
  font-weight: 600;
  color: var(--gray-700);
  width: 80px;
  text-align: right;
  flex-shrink: 0;
  font-family: 'SF Mono', 'Cascadia Code', monospace;
  font-size: .75rem;
}

/* No-reference state */
.ic-no-ref {
  padding: 10px 14px;
  font-size: .78rem;
  color: var(--gray-400);
  font-style: italic;
  border-bottom: 1px solid var(--gray-100);
}

/* Significance row */
.ic-significance {
  padding: 8px 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  border-bottom: 1px solid var(--gray-100);
}
.sig-badge {
  font-size: .7rem;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: var(--radius-full);
}
.sig-strong-pos { background: #d1fae5; color: #065f46; }
.sig-strong-neg { background: #fee2e2; color: #7f1d1d; }
.sig-moderate   { background: #fef3c7; color: #78350f; }
.sig-weak       { background: var(--gray-100); color: var(--gray-600); }
.sig-stats { font-size: .7rem; color: var(--gray-400); margin-left: auto; font-family: monospace; }

/* Description */
.ic-description {
  padding: 12px 14px;
  font-size: .84rem;
  line-height: 1.7;
  color: var(--gray-700);
  border-bottom: 1px solid var(--gray-100);
}
.ic-description.no-desc { color: var(--gray-400); font-style: italic; }

/* Definition (details/summary) */
.ic-definition {
  padding: 0;
}
.ic-definition summary {
  padding: 8px 14px;
  font-size: .78rem;
  color: var(--gray-500);
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: color var(--t);
  user-select: none;
}
.ic-definition summary::before {
  content: '▶';
  font-size: .6rem;
  transition: transform var(--t);
}
.ic-definition[open] summary::before { transform: rotate(90deg); }
.ic-definition summary:hover { color: var(--primary); }
.ic-definition .def-text {
  padding: 8px 14px 14px;
  font-size: .8rem;
  line-height: 1.65;
  color: var(--gray-600);
  background: var(--gray-50);
  border-top: 1px solid var(--gray-100);
}

/* ============================================================
   NEW RUN FORM
   ============================================================ */
.new-run-grid {
  display: grid;
  grid-template-columns: 1fr 380px;
  gap: 24px;
  max-width: 1100px;
}
@media (max-width: 860px) {
  .new-run-grid { grid-template-columns: 1fr; }
}

.help-sidebar .card-body { display: flex; flex-direction: column; gap: 14px; }
.help-item   { font-size: .84rem; color: var(--gray-600); line-height: 1.6; }
.help-item strong { color: var(--gray-800); display: block; margin-bottom: 2px; }

/* ============================================================
   ERROR PANEL
   ============================================================ */
.errors-panel {
  background: var(--danger-light);
  border: 1px solid #fecaca;
  border-radius: var(--radius-lg);
  padding: 16px 20px;
  margin-bottom: 20px;
}
.errors-panel h4 { font-size: .88rem; color: var(--danger); margin-bottom: 8px; }
.errors-panel ul { list-style: none; display: flex; flex-direction: column; gap: 4px; }
.errors-panel li { font-size: .8rem; color: var(--danger-dark); font-family: monospace; }

/* ============================================================
   DIAGNOSIS CARD
   ============================================================ */
.diagnosis-card {
  background: var(--warning-light);
  border: 1px solid #fde68a;
  border-radius: var(--radius-lg);
  overflow: hidden;
  margin-bottom: 20px;
}
.diagnosis-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 20px;
  border-bottom: 1px solid #fde68a;
}
.diagnosis-title {
  flex: 1;
  font-size: .88rem;
  font-weight: 700;
  color: var(--warning-dark);
}
.diagnosis-body {
  padding: 12px 20px;
}
.diagnosis-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.diagnosis-list li {
  font-size: .82rem;
  color: var(--warning-dark);
}
.diag-slug {
  font-family: monospace;
  font-weight: 600;
  background: rgba(217, 119, 6, .12);
  padding: 1px 6px;
  border-radius: 4px;
}
.diag-reason {
  font-size: .77rem;
  color: var(--warning);
}
.diagnosis-footer {
  padding: 10px 20px;
  border-top: 1px solid #fde68a;
  display: flex;
  justify-content: flex-end;
}
.btn-warning { background: var(--warning); color: white; border-color: var(--warning); }
.btn-warning:hover:not(:disabled) { background: var(--warning-dark); border-color: var(--warning-dark); }

/* ============================================================
   COMMUNE SEARCH AUTOCOMPLETE
   ============================================================ */
.form-label-badge {
  display: inline-block;
  font-size: .65rem;
  font-weight: 600;
  background: var(--primary-light);
  color: var(--primary);
  padding: 1px 6px;
  border-radius: var(--radius-full);
  margin-left: 6px;
  vertical-align: middle;
  letter-spacing: .02em;
}

/* Search input wrapper */
.csearch-wrap { position: relative; }
.csearch-input-row {
  display: flex;
  align-items: center;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  transition: border-color var(--t), box-shadow var(--t);
  overflow: hidden;
}
.csearch-input-row:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79,70,229,.1);
}
.csearch-icon {
  flex-shrink: 0;
  margin-left: 10px;
  color: var(--gray-400);
  pointer-events: none;
}
.csearch-input {
  flex: 1;
  padding: 9px 8px;
  border: none;
  background: none;
  outline: none;
  font-size: .9rem;
  color: var(--gray-800);
}
.csearch-input::placeholder { color: var(--gray-400); }
.csearch-clear {
  background: none;
  border: none;
  color: var(--gray-400);
  font-size: .85rem;
  padding: 0 10px;
  cursor: pointer;
  transition: color var(--t);
  line-height: 1;
  align-self: stretch;
  display: flex;
  align-items: center;
}
.csearch-clear:hover { color: var(--gray-700); }

/* Dropdown list */
.csearch-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  left: 0; right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  z-index: 200;
  max-height: 360px;
  overflow-y: auto;
  list-style: none;
  padding: 4px;
  animation: fadeIn .1s ease;
}
.csearch-dropdown.hidden { display: none; }

.csearch-option {
  padding: 10px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background var(--t);
}
.csearch-option:hover, .csearch-option.active { background: var(--primary-light); }
.csearch-opt-main {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 3px;
}
.csearch-opt-name {
  font-size: .9rem;
  font-weight: 600;
  color: var(--gray-800);
}
.csearch-opt-name mark {
  background: #fef08a;
  color: inherit;
  border-radius: 2px;
  padding: 0 1px;
}
.csearch-opt-code {
  font-size: .78rem;
  font-family: monospace;
  color: var(--gray-500);
  background: var(--gray-100);
  padding: 1px 6px;
  border-radius: var(--radius-full);
  flex-shrink: 0;
}
.csearch-opt-meta {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  font-size: .75rem;
  color: var(--gray-500);
}
.csearch-loading, .csearch-empty, .csearch-error {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px 16px;
  font-size: .85rem;
  color: var(--gray-500);
}
.csearch-error { color: var(--danger); }

/* Selected commune card */
.commune-card {
  border: 1.5px solid var(--success);
  border-radius: var(--radius-lg);
  background: var(--success-light);
  margin-bottom: 16px;
  overflow: hidden;
}
.commune-card.hidden { display: none; }
.commune-card-inner {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
}
.commune-card-icon { font-size: 1.6rem; flex-shrink: 0; }
.commune-card-info { flex: 1; min-width: 0; }
.commune-card-name {
  font-size: 1rem;
  font-weight: 700;
  color: var(--success-dark);
  margin-bottom: 4px;
}
.commune-card-meta { display: flex; gap: 6px; flex-wrap: wrap; }
.meta-chip {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: .72rem;
  font-weight: 600;
  background: rgba(5,150,105,.12);
  color: var(--success-dark);
  padding: 2px 8px;
  border-radius: var(--radius-full);
}
.meta-chip-warn { background: var(--warning-light); color: var(--warning-dark); }

/* ============================================================
   RESPONSIVE
   ============================================================ */
@media (max-width: 640px) {
  .indicators-grid { grid-template-columns: 1fr; }
  .form-row { grid-template-columns: 1fr; }
  .report-detail-header { flex-direction: column; }
  #app { padding: 16px; }
}

/* ============================================================
   LOGIN GATE
   ============================================================ */
.login-gate {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  background: var(--sidebar-bg);
  padding: 20px;
}
.login-gate.hidden { display: none; }
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  padding: 32px 28px;
}
.login-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 24px;
}
.login-brand .brand-name { color: var(--gray-800); }
.login-brand .brand-sub  { color: var(--gray-500); }
.login-title {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--gray-800);
  margin: 0 0 4px;
}
.login-subtitle {
  font-size: .85rem;
  color: var(--gray-500);
  margin: 0 0 20px;
}
#login-error { margin-top: 4px; margin-bottom: 4px; }
