:root {
    --bg: #f6f7fb;
    --card-bg: #ffffff;
    --border: #e5e7eb;
    --border-soft: #eef0f3;
    --text: #1a1f2b;
    --muted: #6b7280;
    --muted-soft: #9ca3af;
    --primary: #4f46e5;
    --primary-dark: #4338ca;
    --primary-soft: #eef2ff;
    --danger: #dc2626;
    --success-bg: #ecfdf5;
    --success-text: #065f46;
    --error-bg: #fef2f2;
    --error-text: #991b1b;
    --radius: 10px;
    --radius-lg: 14px;
    --sidebar-w: 244px;
    --shadow-sm: 0 1px 2px rgba(16,24,40,.04);
    --shadow-md: 0 4px 14px rgba(16,24,40,.06);
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

/* ---------- App shell: sidebar + topbar ---------- */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
    width: var(--sidebar-w);
    flex-shrink: 0;
    background: #14152b;
    color: #cbd0e6;
    padding: 18px 12px 20px;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
}
.sidebar-brand { display: flex; align-items: center; gap: 10px; padding: 8px 10px 18px; color: #fff; text-decoration: none; }
.sidebar-brand .logo-mark { width: 28px; height: 28px; border-radius: 8px; background: linear-gradient(135deg,var(--primary),#818cf8); display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 13px; color: #fff; flex-shrink: 0; }
.sidebar-brand .name { font-weight: 700; font-size: 15px; letter-spacing: -.01em; }
.sidebar-section { margin-top: 16px; }
.sidebar-section-title { font-size: 10.5px; text-transform: uppercase; letter-spacing: .08em; color: #6b7099; padding: 0 12px 6px; font-weight: 700; }
.sidebar-link { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-radius: 8px; color: #b6bcdd; text-decoration: none; font-size: 13.5px; font-weight: 500; margin-bottom: 1px; }
.sidebar-link .ic { width: 16px; text-align: center; opacity: .85; font-size: 13px; }
.sidebar-link:hover { background: #1e2040; color: #fff; }
.sidebar-link.active { background: var(--primary); color: #fff; }
.sidebar-link .count { margin-left: auto; background: rgba(255,255,255,.12); padding: 1px 7px; border-radius: 10px; font-size: 10.5px; font-weight: 700; }
.sidebar-link.active .count { background: rgba(255,255,255,.25); }

.app-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.app-topbar { background: #fff; border-bottom: 1px solid var(--border); padding: 12px 24px; display: flex; align-items: center; gap: 16px; position: sticky; top: 0; z-index: 20; }
.topbar-search { flex: 1; max-width: 420px; position: relative; }
.topbar-search input { width: 100%; padding: 8px 12px 8px 32px; border: 1px solid var(--border); border-radius: 8px; font-size: 13px; background: var(--bg); }
.topbar-search input:focus { outline: none; border-color: var(--primary); background: #fff; }
.topbar-search .ic { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--muted-soft); font-size: 13px; }
.topbar-spacer { flex: 1; }
.topbar-right { display: flex; align-items: center; gap: 14px; margin-left: auto; }
.topbar-notif { position: relative; font-size: 17px; color: var(--muted); text-decoration: none; }
.topbar-notif .dot { position: absolute; top: -3px; right: -5px; background: var(--danger); color: #fff; font-size: 9.5px; font-weight: 700; border-radius: 8px; padding: 0 4px; line-height: 14px; min-width: 14px; text-align: center; }
.topbar-user { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text); text-decoration: none; }
.topbar-user .avatar { width: 28px; height: 28px; border-radius: 50%; background: var(--primary-soft); color: var(--primary); display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 12px; }
.topbar-user .logout { color: var(--muted); font-size: 12px; margin-left: 4px; }

.container { max-width: 1320px; margin: 0 auto; padding: 24px; width: 100%; }

.page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 18px; gap: 12px; flex-wrap: wrap; }
.page-header h1 { font-size: 20px; margin: 0; display: flex; gap: 10px; align-items: center; }
.actions { display: flex; gap: 8px; }

.card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 20px;
}

.form-stack label { display: block; margin-bottom: 14px; font-weight: 500; font-size: 13px; color: var(--text); }
.form-stack input[type=text],
.form-stack input[type=number],
.form-stack input[type=url],
.form-stack input[type=password],
.form-stack input[type=email],
.form-stack input[type=tel],
.form-stack input[type=date],
.form-stack input[type=datetime-local],
.form-stack textarea,
.form-stack select {
    display: block;
    width: 100%;
    margin-top: 6px;
    padding: 9px 11px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-family: inherit;
    font-size: 14px;
    line-height: 1.4;
    min-height: 38px;
    box-sizing: border-box;
}
.form-stack input[type=checkbox] {
    width: auto;
    margin-top: 8px;
    font-size: 14px;
}
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; }
legend { font-weight: 600; padding: 0 6px; }

.chip-group { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 10px;
    border: 1px solid var(--border);
    border-radius: 20px;
    font-weight: 400;
    font-size: 13px;
    cursor: pointer;
    background: #f9fafb;
}
.chip input { margin: 0; }

.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }

.btn {
    display: inline-block;
    padding: 8px 16px;
    border-radius: 6px;
    border: 1px solid transparent;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    color: #fff;
    background: var(--primary);
}
.btn:hover { background: var(--primary-dark); }
.btn-secondary { background: #fff; color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: #f3f4f6; }
.btn-link { background: transparent; color: var(--primary); padding: 4px 6px; border: none; }
.btn-danger { color: var(--danger); }
.inline-form { display: inline-block; margin-right: 6px; }
.inline-form input[type=text],
.inline-form input[type=number],
.inline-form input[type=email],
.inline-form input[type=date],
.inline-form input[type=datetime-local],
.inline-form select {
    padding: 7px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-family: inherit;
    font-size: 13px;
    min-height: 34px;
    box-sizing: border-box;
}

.login-wrap { display: flex; justify-content: center; padding-top: 60px; }
.login-card { width: 340px; }
.login-card h1 { font-size: 18px; margin-top: 0; }

.flash { padding: 10px 14px; border-radius: 6px; margin-bottom: 14px; font-size: 13px; }
.flash-success { background: var(--success-bg); color: var(--success-text); }
.flash-error { background: var(--error-bg); color: var(--error-text); }

.table-wrap { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th, .data-table td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.data-table th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
.empty-cell { text-align: center; color: var(--muted); padding: 30px !important; }
.truncate { max-width: 260px; overflow: hidden; text-overflow: ellipsis; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 12px; font-size: 11px; font-weight: 600; background: #e5e7eb; color: #374151; text-transform: capitalize; }
.badge-running, .badge-queued { background: #dbeafe; color: #1e40af; }
.badge-completed, .badge-qualified_for_phase_2 { background: #d1fae5; color: #065f46; }
.badge-failed, .badge-invalid { background: #fee2e2; color: #991b1b; }
.badge-paused { background: #fef3c7; color: #92400e; }
.badge-duplicate { background: #e5e7eb; color: #4b5563; }
.badge-review { background: #fef3c7; color: #92400e; margin-left: 6px; }
.badge-new { background: #e0e7ff; color: #3730a3; }

.stat-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 12px; margin-bottom: 20px; }
.stat-card { background: var(--card-bg); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; text-align: center; }
.stat-value { display: block; font-size: 24px; font-weight: 700; }
.stat-label { display: block; font-size: 11px; color: var(--muted); margin-top: 4px; text-transform: uppercase; letter-spacing: .03em; }

.progress-bar-wrap p { margin: 8px 0 0; font-size: 12px; color: var(--muted); }
.progress-bar { background: #e5e7eb; border-radius: 6px; height: 10px; overflow: hidden; }
.progress-bar-fill { background: var(--primary); height: 100%; }

.def-list { display: grid; grid-template-columns: 200px 1fr; gap: 8px 16px; margin: 0; }
.def-list dt { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
.def-list dd { margin: 0; }

.filter-bar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.filter-bar input, .filter-bar select { padding: 7px 10px; border: 1px solid var(--border); border-radius: 6px; font-size: 13px; }

.pagination { display: flex; gap: 6px; }
.pagination a { padding: 6px 10px; border: 1px solid var(--border); border-radius: 6px; text-decoration: none; color: var(--text); background: #fff; }
.pagination a.active { background: var(--primary); color: #fff; border-color: var(--primary); }

.empty-state { text-align: center; padding: 40px; }
.muted { color: var(--muted); }

.actions-bar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }

/* Single shared pill-badge primitive used everywhere in the app (evidence
   status, classification, pipeline stage, priority, ...) - always
   `.badge` + exactly one `.badge-*` color modifier. Never reuse one
   subsystem's modifier for another subsystem's meaning. */
.badge { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 12px; white-space: nowrap; }

/* Evidence status (verified/likely/unknown/not detected) - Phase 2/3/4 findings. */
.badge-verified { background: #d1fae5; color: #065f46; }
.badge-likely { background: #fef3c7; color: #92400e; }
.badge-unknown { background: #e5e7eb; color: #4b5563; }
.badge-not-detected { background: #fee2e2; color: #991b1b; }

/* Opportunity classification (HOT/WARM/MODERATE/LOW) - its own palette,
   distinct from the evidence badges above so the two concepts never look
   identical by accident. */
.badge-hot { background: #fee2e2; color: #b91c1c; }
.badge-warm { background: #ffedd5; color: #9a3412; }
.badge-moderate { background: #fef9c3; color: #854d0e; }
.badge-low { background: #f3f4f6; color: #4b5563; }

.evidence-drawer { border: 1px solid var(--border); border-radius: 6px; margin-bottom: 8px; padding: 8px 12px; }
.evidence-drawer summary { cursor: pointer; font-weight: 600; display: flex; gap: 8px; align-items: center; }
.evidence-body { margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--border); font-size: 13px; }
.evidence-body p { margin: 4px 0; }

.top-opportunity-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.top-opportunity-list li { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.top-opportunity-list li:last-child { border-bottom: none; }

.score-hero { display: flex; align-items: baseline; gap: 12px; margin-bottom: 8px; }
.score-hero .score-number { font-size: 40px; font-weight: 700; }
.score-hero .score-max { color: var(--muted); font-size: 16px; }

.dimension-row { display: grid; grid-template-columns: 220px 1fr 90px; gap: 12px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--border); }
.dimension-row:last-child { border-bottom: none; }
.dimension-bar { background: #e5e7eb; border-radius: 6px; height: 8px; overflow: hidden; }
.dimension-bar-fill { background: var(--primary); height: 100%; }
.dimension-points { text-align: right; font-weight: 600; font-size: 13px; }
.dimension-evidence { grid-column: 1 / -1; font-size: 12px; color: var(--muted); margin-top: -4px; }

.opportunity-pill { display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 20px; background: #eef2ff; color: #3730a3; font-size: 13px; font-weight: 600; margin: 4px 6px 4px 0; }
.opportunity-pill.secondary { background: #f3f4f6; color: #374151; font-weight: 500; }

.ai-analysis-card { border-left: 4px solid var(--primary); }

.section-nav { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-bottom: 18px; padding: 10px 14px; background: #fff; border: 1px solid var(--border); border-radius: var(--radius); position: sticky; top: 65px; z-index: 5; }
.section-nav a { font-size: 12px; color: var(--primary); text-decoration: none; font-weight: 600; }

.timeline-list { list-style: none; margin: 0; padding: 0; }
.timeline-list li { padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.timeline-list li:last-child { border-bottom: none; }
.timeline-time { color: var(--muted); font-size: 11px; margin-right: 10px; display: inline-block; min-width: 90px; }

.funnel-row { display: grid; grid-template-columns: 180px 1fr 60px; align-items: center; gap: 12px; padding: 8px 0; }
.funnel-bar { background: #e5e7eb; border-radius: 6px; height: 10px; overflow: hidden; }
.funnel-bar-fill { background: var(--primary); height: 100%; }

/* ---------- Dashboard: greeting, metrics, grid layout ---------- */
.dash-greeting { font-size: 22px; font-weight: 700; margin: 0 0 2px; letter-spacing: -.01em; }
.dash-subtext { color: var(--muted); font-size: 13.5px; margin: 0 0 22px; }

.dash-grid { display: grid; grid-template-columns: repeat(12, 1fr); gap: 18px; margin-bottom: 18px; }
.dash-col-4 { grid-column: span 4; }
.dash-col-6 { grid-column: span 6; }
.dash-col-8 { grid-column: span 8; }
.dash-col-12 { grid-column: span 12; }
@media (max-width: 1100px) {
    .dash-col-4, .dash-col-6, .dash-col-8 { grid-column: span 12; }
}

.metric-card { background: var(--card-bg); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 18px 20px; box-shadow: var(--shadow-sm); }
.metric-card .metric-label { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; display: flex; align-items: center; gap: 6px; }
.metric-card .metric-value { font-size: 26px; font-weight: 800; letter-spacing: -.02em; margin: 6px 0 2px; }
.metric-card .metric-trend { font-size: 12px; font-weight: 600; }
.metric-trend.up { color: #059669; }
.metric-trend.down { color: var(--danger); }
.metric-trend.flat { color: var(--muted); }
.metric-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 20px; }
@media (max-width: 1100px) { .metric-row { grid-template-columns: repeat(2, 1fr); } }

.panel { background: var(--card-bg); border: 1px solid var(--border); border-radius: var(--radius-lg); box-shadow: var(--shadow-sm); padding: 20px 22px; margin-bottom: 18px; }
.panel-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.panel-header h2 { font-size: 14.5px; font-weight: 700; margin: 0; }
.panel-header .panel-action { font-size: 12px; color: var(--primary); text-decoration: none; font-weight: 600; }

/* Attention required */
.attention-list { list-style: none; margin: 0; padding: 0; }
.attention-item { display: flex; align-items: center; gap: 10px; padding: 10px 4px; border-bottom: 1px solid var(--border-soft); text-decoration: none; color: var(--text); }
.attention-item:last-child { border-bottom: none; }
.attention-item:hover { background: #fafafa; }
.attention-item .warn-ic { color: #d97706; font-size: 15px; }
.attention-item .count { font-weight: 700; }
.attention-item .arrow { margin-left: auto; color: var(--muted-soft); }
.attention-empty { color: var(--muted); font-size: 13px; padding: 6px 4px; }

/* Activity feed */
.activity-feed { list-style: none; margin: 0; padding: 0; }
.activity-feed li { display: flex; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border-soft); }
.activity-feed li:last-child { border-bottom: none; }
.activity-ic { width: 30px; height: 30px; border-radius: 50%; background: var(--primary-soft); color: var(--primary); display: flex; align-items: center; justify-content: center; font-size: 13px; flex-shrink: 0; }
.activity-body { flex: 1; }
.activity-title { font-size: 13px; font-weight: 600; }
.activity-sub { font-size: 12.5px; color: var(--muted); }
.activity-time { font-size: 11px; color: var(--muted-soft); white-space: nowrap; }

/* Task list */
.task-list { list-style: none; margin: 0; padding: 0; }
.task-list li { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border-soft); font-size: 13px; }
.task-list li:last-child { border-bottom: none; }
.task-list .task-due { margin-left: auto; font-size: 11px; color: var(--muted); white-space: nowrap; }

/* Charts (server-rendered inline SVG - no JS chart library) */
.chart-wrap { width: 100%; overflow-x: auto; }
.chart-wrap svg { display: block; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 12px 18px; margin-top: 10px; font-size: 12px; color: var(--muted); }
.chart-legend span { display: inline-flex; align-items: center; gap: 6px; }
.chart-legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.chart-empty { color: var(--muted); font-size: 13px; padding: 30px 0; text-align: center; }

/* Client portfolio tree */
.portfolio-tree { font-size: 13px; }
.portfolio-tree ul { list-style: none; margin: 4px 0 4px 18px; padding-left: 16px; border-left: 1px dashed var(--border); }
.portfolio-tree > ul { margin-left: 0; padding-left: 0; border-left: none; }
.portfolio-tree li { margin: 6px 0; }
.portfolio-node { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; background: var(--primary-soft); color: #3730a3; border-radius: 6px; font-weight: 600; }
.portfolio-node.leaf { background: #f3f4f6; color: #374151; font-weight: 500; }

/* Priority + status badges (CRM-wide) */
.badge-priority-low { background: #f3f4f6; color: #4b5563; }
.badge-priority-normal { background: #e0e7ff; color: #3730a3; }
.badge-priority-high { background: #fef3c7; color: #92400e; }
.badge-priority-urgent { background: #fee2e2; color: #991b1b; }

/* Search results */
.search-group { margin-bottom: 18px; }
.search-group h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 8px; }
.search-result { display: block; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; margin-bottom: 6px; text-decoration: none; color: var(--text); }
.search-result:hover { border-color: var(--primary); background: var(--primary-soft); }
.search-result .r-title { font-weight: 600; font-size: 13px; }
.search-result .r-sub { font-size: 12px; color: var(--muted); }

/* Progress / at-risk indicator */
.progress-track { background: #e5e7eb; border-radius: 6px; height: 8px; overflow: hidden; }
.progress-fill { height: 100%; border-radius: 6px; }
.progress-fill.ontrack { background: #059669; }
.progress-fill.atrisk { background: #d97706; }
.progress-fill.delayed { background: var(--danger); }
.track-badge { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 10px; }
.track-badge.ontrack { background: #d1fae5; color: #065f46; }
.track-badge.atrisk { background: #fef3c7; color: #92400e; }
.track-badge.delayed { background: #fee2e2; color: #991b1b; }

/* Toast (minimal, CSS-driven fade via app.js) */
.toast { position: fixed; bottom: 20px; right: 20px; background: #111827; color: #fff; padding: 12px 18px; border-radius: 8px; font-size: 13px; box-shadow: var(--shadow-md); z-index: 100; opacity: 1; transition: opacity .4s ease; }
.toast.fade-out { opacity: 0; }

/* Sortable table header links */
.sortable-th a { color: inherit; text-decoration: none; }
.sortable-th a:hover { color: var(--primary); }

/* Kanban board (Projects) */
.kanban-board { display: flex; gap: 14px; overflow-x: auto; padding-bottom: 8px; align-items: flex-start; }
.kanban-column { flex: 0 0 260px; background: #eef0f4; border-radius: var(--radius-lg); padding: 10px; }
.kanban-column-header { display: flex; align-items: center; justify-content: space-between; padding: 4px 6px 10px; font-size: 12.5px; font-weight: 700; color: var(--text); text-transform: uppercase; letter-spacing: .03em; }
.kanban-column-header .count { background: #dfe2e8; color: var(--muted); font-weight: 700; border-radius: 10px; padding: 1px 8px; font-size: 11px; }
.kanban-cards { display: flex; flex-direction: column; gap: 8px; min-height: 30px; }
.kanban-card { display: block; background: #fff; border-radius: 8px; padding: 10px 12px; box-shadow: var(--shadow-sm); text-decoration: none; color: var(--text); border-left: 3px solid var(--border); }
.kanban-card:hover { box-shadow: var(--shadow-md); }
.kanban-card.ontrack { border-left-color: #059669; }
.kanban-card.atrisk { border-left-color: #d97706; }
.kanban-card.delayed { border-left-color: var(--danger); }
.kanban-card .kc-title { font-size: 13px; font-weight: 700; margin-bottom: 3px; }
.kanban-card .kc-client { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.kanban-card .kc-meta { display: flex; justify-content: space-between; align-items: center; font-size: 11px; color: var(--muted); }
.kanban-empty { color: var(--muted-soft); font-size: 12px; padding: 10px 6px; text-align: center; }
.kanban-card { cursor: grab; }
.kanban-card.dragging { opacity: .4; }
.kanban-cards { border-radius: 8px; transition: background-color .1s ease; min-height: 40px; }
.kanban-cards.drag-over { background: #dde3f0; outline: 2px dashed var(--primary); outline-offset: -2px; }

@media (max-width: 900px) {
    .stat-grid { grid-template-columns: repeat(2, 1fr); }
    .grid-3 { grid-template-columns: 1fr; }
    .metric-row { grid-template-columns: 1fr 1fr; }
}
