:root {
  --bg-app: #F7F6F5;
  --bg-surface: #FBFAFA;
  --bg-subtle: #F0EFEE;
  --text-primary: #333132;
  --text-secondary: #5F6367;
  --text-muted: #8A9298;
  --border-default: #E2DEDF;
  --border-subtle: #EEE9EB;
  --brand-magenta: #EE2A7B;
  --brand-magenta-dark: #C91463;
  --brand-magenta-soft: #FDE8F1;
  --brand-black: #333132;
  --brand-gray: #8A9298;
  --indigo-50: #FDE8F1;
  --indigo-100: #F9C9DC;
  --indigo-500: #EE2A7B;
  --indigo-600: #EE2A7B;
  --indigo-700: #C91463;
  --success: #10B981;
  --warning: #F59E0B;
  --error: #EF4444;
  --ai-bg: #FFFAFC;
  --copilot-bg: #F6F4F8;
  --copilot-border: #E6E1EA;
  --shadow-xs: 0 1px 2px rgba(0,0,0,0.05);
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.05), 0 2px 4px rgba(0,0,0,0.04);
  --shadow-lg: 0 10px 15px rgba(0,0,0,0.07), 0 4px 6px rgba(0,0,0,0.04);
  --shadow-ai: 0 0 0 1px #F9C9DC, 0 4px 16px rgba(238,42,123,0.14);
  --font-display: 'Poppins', 'Helvetica Neue', Arial, sans-serif;
  --font-ui: 'Helvetica Neue', Arial, system-ui, sans-serif;
}

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

body {
  font-family: 'Helvetica Neue', Arial, system-ui, sans-serif;
  background: var(--bg-app);
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ── Layout ─────────────────────────────────── */
#header {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 56px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-default);
  box-shadow: var(--shadow-xs);
  z-index: 50;
  display: flex;
  align-items: center;
  padding: 0 20px;
  gap: 16px;
}

#sidebar {
  position: fixed;
  top: 56px; left: 0;
  width: 240px;
  height: calc(100vh - 56px);
  background: var(--bg-surface);
  border-right: 1px solid var(--border-default);
  overflow-y: auto;
  z-index: 40;
  display: flex;
  flex-direction: column;
}

#app {
  margin-left: 240px;
  padding-top: 56px;
  min-height: 100vh;
  background: var(--bg-app);
}

/* ── View transitions ────────────────────────── */
.view-enter {
  animation: viewEnter 200ms ease-out forwards;
}
@keyframes viewEnter {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── Sidebar nav ─────────────────────────────── */
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 12px;
  height: 36px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  text-decoration: none;
  transition: background 150ms, color 150ms;
  margin: 1px 8px;
  position: relative;
}
.nav-item:hover {
  background: var(--bg-subtle);
  color: var(--text-primary);
}
.nav-item.active {
  background: var(--indigo-50);
  color: var(--indigo-600);
}
.nav-item.active::before {
  content: '';
  position: absolute;
  left: -8px;
  top: 6px; bottom: 6px;
  width: 1px;
  background: var(--indigo-600);
  border-radius: 0 2px 2px 0;
}
.nav-section-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 0 20px;
  margin: 16px 0 4px;
}

/* ── Buttons ─────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 0 14px;
  height: 34px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border: none;
  transition: background 150ms, box-shadow 150ms;
  text-decoration: none;
  white-space: nowrap;
}
.btn-primary {
  background: var(--indigo-600);
  color: #FBFAFA;
}
.btn-primary:hover { background: var(--indigo-700); }
.btn-secondary {
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border-default);
}
.btn-secondary:hover { background: var(--bg-subtle); }
.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
}
.btn-ghost:hover {
  background: var(--bg-subtle);
  color: var(--text-primary);
}
.btn-lg {
  height: 42px;
  padding: 0 20px;
  font-size: 14px;
  border-radius: 8px;
}

/* ── Cards ───────────────────────────────────── */
.card {
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: 8px;
  box-shadow: var(--shadow-sm);
  padding: 20px;
}
.card-ai {
  background: var(--ai-bg);
  box-shadow: var(--shadow-ai);
  border: none;
}
.card:hover { box-shadow: var(--shadow-md); }

/* ── Badges ──────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 500;
}
.badge-indigo  { background: var(--indigo-50);  color: var(--indigo-600); }
.badge-violet  { background: #F5F3FF; color: #7C3AED; }
.badge-sky     { background: #F0F9FF; color: #0369A1; }
.badge-green   { background: #ECFDF5; color: #065F46; }
.badge-amber   { background: #FFFBEB; color: #92400E; }
.badge-red     { background: #FEF2F2; color: #991B1B; }
.badge-slate   { background: var(--bg-subtle); color: var(--text-secondary); }

/* ── Tables ──────────────────────────────────── */
.table { width: 100%; border-collapse: collapse; }
.table th {
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  padding: 10px 16px;
  background: var(--bg-subtle);
  border-bottom: 1px solid var(--border-default);
}
.table td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-subtle);
  color: var(--text-primary);
  font-size: 13px;
}
.table tr:hover td { background: var(--bg-subtle); cursor: pointer; }
.table tr:last-child td { border-bottom: none; }

/* ── KPI cards ───────────────────────────────── */
.kpi-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: 8px;
  padding: 20px;
  box-shadow: var(--shadow-sm);
}
.kpi-value {
  font-size: 28px;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1;
  margin-bottom: 6px;
}
.kpi-label {
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 500;
}
.kpi-trend {
  font-size: 11px;
  margin-top: 8px;
}

/* ── AI Thinking ─────────────────────────────── */
.ai-step {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  transition: background 200ms;
  animation: stepIn 300ms ease-out forwards;
  opacity: 0;
}
@keyframes stepIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
.ai-step.active { background: var(--indigo-50); }
.ai-step-icon { width: 18px; height: 18px; flex-shrink: 0; margin-top: 1px; }
.ai-step-content { flex: 1; min-width: 0; }
.ai-step-title {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-primary);
}
.ai-step.active .ai-step-title { color: var(--indigo-600); }
.ai-step.pending .ai-step-title { color: var(--text-muted); }
.ai-step-result {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* ── Spinner ─────────────────────────────────── */
.spinner {
  width: 16px; height: 16px;
  border: 2px solid var(--indigo-100);
  border-top-color: var(--indigo-600);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  display: inline-block;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ── Progress bar ────────────────────────────── */
.progress-bar {
  height: 6px;
  background: var(--bg-subtle);
  border-radius: 99px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--indigo-600), #333132);
  border-radius: 99px;
  transition: width 400ms ease-out;
}

/* ── Tabs ────────────────────────────────────── */
.tab-bar {
  display: flex;
  border-bottom: 1px solid var(--border-default);
  gap: 0;
}
.tab {
  padding: 10px 16px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  transition: color 150ms, border-color 150ms;
}
.tab:hover { color: var(--text-primary); }
.tab.active {
  color: var(--indigo-600);
  border-bottom-color: var(--indigo-600);
}

/* ── Expediente sections ─────────────────────── */
.exp-section {
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.exp-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-subtle);
  cursor: pointer;
}
.exp-section-header:hover { background: var(--bg-subtle); }
.exp-section-title {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
}
.exp-section-num {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  width: 20px;
}
.exp-section-body { padding: 20px; }

/* ── Search input ────────────────────────────── */
.search-input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border-default);
  border-radius: 6px;
  font-size: 13px;
  font-family: inherit;
  background: var(--bg-surface);
  color: var(--text-primary);
  outline: none;
  transition: border-color 150ms, box-shadow 150ms;
}
.search-input:focus {
  border-color: var(--indigo-500);
  box-shadow: 0 0 0 3px rgba(238,42,123,0.12);
}

/* ── Category tree ───────────────────────────── */
details > summary { list-style: none; }
details > summary::-webkit-details-marker { display: none; }
.tree-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-secondary);
  transition: background 150ms, color 150ms;
}
.tree-item:hover { background: var(--bg-subtle); color: var(--text-primary); }
.tree-item.active { background: var(--indigo-50); color: var(--indigo-600); font-weight: 500; }

/* ── Stage stepper (9 etapas del Caso) ───────── */
.stage-stepper {
  display: flex;
  align-items: center;
  gap: 3px;
  margin-top: 10px;
}
.stage-seg {
  flex: 1;
  height: 4px;
  border-radius: 99px;
  background: var(--border-default);
  position: relative;
  cursor: default;
}
.stage-seg.done { background: var(--brand-magenta); opacity: 0.45; }
.stage-seg.active { background: var(--brand-magenta); opacity: 1; }
.stage-stepper-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--brand-magenta);
  margin-top: 6px;
}

/* ── Badges de etapa del Caso ─────────────────── */
.stage-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 10px;
  border-radius: 99px;
  font-size: 11px;
  font-weight: 600;
}
.stage-badge::before { content: '●'; font-size: 8px; }
.stage-badge-necesidad, .stage-badge-analisis { background: var(--bg-subtle); color: var(--text-secondary); }
.stage-badge-rfp, .stage-badge-juridico, .stage-badge-aprobacion { background: var(--brand-magenta-soft); color: var(--brand-magenta-dark); }
.stage-badge-contrato, .stage-badge-administracion { background: #E6F4EE; color: #0E7A53; }
.stage-badge-renovacion { background: #EFF1F5; color: #4B5066; }

/* ── Panel Copiloto (IA persistente) ──────────── */
.copilot-panel {
  background: var(--copilot-bg);
  border-left: 1px solid var(--copilot-border);
  height: 100%;
  display: flex;
  flex-direction: column;
}
.copilot-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--copilot-border);
}
.copilot-pulse {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--brand-magenta);
  animation: pulseDot 2s ease-in-out infinite;
}
@keyframes pulseDot {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(0.8); }
}
.copilot-block {
  padding: 14px 18px;
  border-bottom: 1px solid var(--copilot-border);
}
.copilot-block-label {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 8px;
}
.copilot-block-body {
  font-size: 13px;
  color: var(--text-primary);
  line-height: 1.55;
}
.copilot-chip {
  display: inline-block;
  font-size: 12px;
  color: var(--brand-magenta-dark);
  background: var(--bg-surface);
  border: 1px solid var(--copilot-border);
  border-radius: 8px;
  padding: 6px 10px;
  margin: 3px 4px 3px 0;
  cursor: pointer;
  transition: border-color 150ms;
}
.copilot-chip:hover { border-color: var(--brand-magenta); }
.copilot-input-row {
  margin-top: auto;
  padding: 12px 14px;
  border-top: 1px solid var(--copilot-border);
}
.copilot-input-row input {
  width: 100%;
  border: 1px solid var(--copilot-border);
  background: var(--bg-surface);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 12px;
  font-family: inherit;
  outline: none;
}
.copilot-input-row input:focus { border-color: var(--brand-magenta); }

/* ── Navegación de áreas del Caso (estilo Linear) ── */
.area-nav {
  display: flex;
  gap: 22px;
  padding: 0 24px;
  border-bottom: 1px solid var(--border-default);
}
.area-nav-item {
  padding: 12px 0;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: color 150ms, border-color 150ms;
}
.area-nav-item:hover { color: var(--text-primary); }
.area-nav-item.active {
  color: var(--brand-magenta-dark);
  border-bottom-color: var(--brand-magenta);
  font-weight: 600;
}

/* ── Bloques narrativos de área (Resumen/Doc/Análisis/Colab/Hist) ── */
.area-block { padding: 18px 0; border-bottom: 1px solid var(--border-subtle); }
.area-block:last-child { border-bottom: none; }
.area-block-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 10px;
}
.area-block-body { font-size: 13px; color: var(--text-secondary); line-height: 1.6; }

/* ── Tarjetas de rol (selector de entrada) ────── */
.role-card {
  border: 1.5px solid var(--border-default);
  border-radius: 14px;
  padding: 22px 20px;
  cursor: pointer;
  background: var(--bg-surface);
  transition: border-color 150ms, background 150ms, transform 150ms;
}
.role-card:hover { transform: translateY(-2px); }
.role-card.selected {
  border-color: var(--brand-magenta);
  background: var(--brand-magenta-soft);
}
.role-card-title { font-family: var(--font-display); font-size: 16px; font-weight: 600; color: var(--text-primary); margin-bottom: 8px; }
.role-card-desc { font-size: 13px; color: var(--text-secondary); line-height: 1.5; }

/* ── Utility ─────────────────────────────────── */
.divider { height: 1px; background: var(--border-subtle); margin: 8px 0; }
.text-mono { font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', monospace; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── Scrollbar ───────────────────────────────── */
::-webkit-scrollbar { width: 4px; height: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-default); border-radius: 99px; }

/* ── Toasts ─────────────────────────────────── */
.toast-region {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 80;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}
.toast {
  max-width: 320px;
  padding: 10px 12px;
  border: 1px solid var(--border-default);
  border-radius: 8px;
  background: var(--bg-surface);
  box-shadow: var(--shadow-lg);
  color: var(--text-primary);
  font-size: 13px;
  animation: toastIn 180ms ease-out both;
}
@keyframes toastIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ── Responsive shell ───────────────────────── */
@media (max-width: 900px) {
  #header {
    padding: 0 12px;
    gap: 10px;
  }

  #header .global-search {
    max-width: 260px !important;
  }

  #header .topbar-brand {
    min-width: 0 !important;
  }

  #sidebar {
    width: 72px;
  }

  #app {
    margin-left: 72px;
    padding: 76px 16px 24px !important;
  }

  .brand-label,
  .nav-label,
  .nav-section-label,
  #recent-conversations,
  .sidebar-conversations {
    display: none !important;
  }

  #header .topbar-brand .brand-label {
    display: inline !important;
    font-size: 15px !important;
  }

  .nav-item {
    justify-content: center;
    padding: 0;
    margin: 2px 10px;
  }

  .nav-item.active::before {
    left: -10px;
  }

  [style*="grid-template-columns:repeat(4,1fr)"] {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  [style*="grid-template-columns:1.4fr 1fr"],
  [style*="grid-template-columns:2fr 1fr"],
  [style*="grid-template-columns:1fr 420px"],
  [style*="grid-template-columns:1fr 220px"],
  [style*="grid-template-columns:1fr 320px"],
  [style*="grid-template-columns:340px 1fr"],
  [style*="grid-template-columns:360px 1fr"],
  [style*="grid-template-columns:110px 1fr 120px 140px"] {
    grid-template-columns: 1fr !important;
  }

  [style*="max-width:1100px"],
  [style*="max-width:1200px"],
  [style*="max-width:1000px"],
  [style*="max-width:900px"] {
    max-width: 100% !important;
  }

  .card[style*="position:sticky"],
  [style*="position:sticky"] {
    position: static !important;
  }

  .table {
    min-width: 720px;
  }

  .card:has(.table),
  [style*="overflow:hidden"]:has(.table) {
    overflow-x: auto !important;
  }
}

@media (max-width: 640px) {
  #header .global-search {
    display: none !important;
  }

  #header .topbar-brand .brand-label {
    display: none !important;
  }

  #sidebar {
    width: 60px;
  }

  #app {
    margin-left: 60px;
    padding: 72px 12px 20px !important;
  }

  [style*="grid-template-columns:repeat(4,1fr)"],
  [style*="grid-template-columns:repeat(2,minmax(0,1fr))"],
  [style*="grid-template-columns:1fr 1fr"],
  [style*="grid-template-columns:1fr 1fr 1fr"],
  [style*="grid-template-columns:repeat(3,1fr)"] {
    grid-template-columns: 1fr !important;
  }

  [style*="min-height:calc(100vh - 180px)"] {
    min-height: calc(100vh - 110px) !important;
  }

  .kpi-value {
    font-size: 24px;
  }

  .toast-region {
    left: 72px;
    right: 12px;
    bottom: 12px;
  }

  .toast {
    max-width: none;
  }
}
