/* Issued CRM design tokens. Dark is the default; :root[data-theme="light"]
   overrides for the light theme (toggled per-agent, see partials/topbar.ejs
   and POST /profile/theme). Keep new rules using var(--token) rather than
   raw hex so both themes stay in sync automatically. */
:root {
  --bg: #0f1115;
  --surface: #1a1d24;
  --surface-alt: #14161c;
  --border: #2a2e37;
  --text: #e6e6e6;
  --text-muted: #9aa0a6;

  --accent: #4F46E5;
  --accent-hover: #4338CA;
  --accent-soft: #6366F1;

  --success: #4ade80;
  --success-fill: #22c55e;
  --success-bg: #1e3a2a;
  --success-text: #4ade80;

  --warning: #fbbf24;
  --warning-bg: #3a2f1e;
  --warning-text: #fbbf24;

  --danger: #f87171;
  --danger-strong: #ef4444;
  --danger-bg: #3a1e1e;
  --danger-text: #f87171;

  --muted-bg: #2a2e37;
  --muted-text: #9aa0a6;

  --shadow: rgba(0, 0, 0, 0.4);
}

:root[data-theme="light"] {
  --bg: #f7f7f8;
  --surface: #ffffff;
  --surface-alt: #f1f1f3;
  --border: #e4e4e7;
  --text: #17181c;
  --text-muted: #6b6d76;

  --accent: #4F46E5;
  --accent-hover: #4338CA;
  --accent-soft: #6366F1;

  --success: #16a34a;
  --success-fill: #16a34a;
  --success-bg: #dcfce7;
  --success-text: #15803d;

  --warning: #d97706;
  --warning-bg: #fef3c7;
  --warning-text: #a16207;

  --danger: #dc2626;
  --danger-strong: #dc2626;
  --danger-bg: #fee2e2;
  --danger-text: #b91c1c;

  --muted-bg: #e4e4e7;
  --muted-text: #52525b;

  --shadow: rgba(15, 17, 21, 0.12);
}

body { font-family: -apple-system, Segoe UI, Roboto, sans-serif; background: var(--bg); color: var(--text); margin: 0; }
.topbar { display: flex; justify-content: space-between; align-items: center; padding: 16px 32px; background: var(--surface); border-bottom: 1px solid var(--border); flex-wrap: wrap; gap: 12px; }
.topbar .brand { font-weight: 700; font-size: 18px; display: flex; align-items: center; gap: 8px; line-height: 1.3; }
.topbar .brand .logo-mark { width: 14px; height: 21px; color: var(--accent); flex-shrink: 0; }
.topbar .brand .brand-org { font-weight: 500; font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; }
.topbar nav { display: flex; align-items: center; gap: 20px; flex-wrap: wrap; }
.topbar nav a { color: var(--text-muted); text-decoration: none; font-size: 14px; }
.topbar nav a:hover { color: var(--text); }
.container { max-width: 900px; margin: 32px auto; padding: 0 24px; }
h1 { font-size: 22px; margin-bottom: 24px; }
.card { background: var(--surface); border-radius: 10px; padding: 24px; margin-bottom: 16px; border: 1px solid var(--border); }
.btn { display: inline-block; background: var(--accent); color: white; padding: 10px 18px; border-radius: 6px; text-decoration: none; font-weight: 600; font-size: 14px; border: none; cursor: pointer; }
.btn:hover { background: var(--accent-hover); }
.btn-secondary { background: var(--muted-bg); color: var(--text); }
.btn-secondary:hover { background: var(--border); }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); font-size: 14px; }
th { color: var(--text-muted); font-weight: 600; font-size: 12px; text-transform: uppercase; }
.badge { padding: 3px 10px; border-radius: 12px; font-size: 12px; font-weight: 600; }
.badge-approved { background: var(--success-bg); color: var(--success-text); }
.badge-pending { background: var(--warning-bg); color: var(--warning-text); }
.badge-declined { background: var(--danger-bg); color: var(--danger-text); }
.badge-lapsed { background: var(--muted-bg); color: var(--muted-text); }
form label { display: block; margin-bottom: 6px; font-size: 13px; color: var(--text-muted); }
form input, form select, form textarea { width: 100%; padding: 10px; border-radius: 6px; border: 1px solid var(--border); background: var(--bg); color: var(--text); margin-bottom: 16px; box-sizing: border-box; font-size: 14px; }
.error { background: var(--danger-bg); color: var(--danger-text); padding: 12px; border-radius: 6px; margin-bottom: 16px; font-size: 14px; }
.tree { list-style: none; padding-left: 20px; }
.tree > li { margin: 8px 0; }
.tree-node { display: flex; align-items: center; gap: 8px; }
.tree-node img { width: 24px; height: 24px; border-radius: 50%; }

.btn-link { background: none; border: none; color: var(--accent-soft); cursor: pointer; padding: 0; font-size: inherit; text-decoration: underline; }
.btn-link:hover { color: var(--accent); }

/* Login page */
.login-page { display: flex; align-items: center; justify-content: center; height: 100vh; }
.login-card { padding: 48px; text-align: center; box-shadow: 0 8px 24px var(--shadow); max-width: 360px; }
.login-card .logo-mark { width: 28px; height: 42px; color: var(--accent); margin-bottom: 16px; }
.login-card h1 { margin: 0 0 8px; font-size: 24px; }
.login-card p { color: var(--text-muted); margin-bottom: 32px; }
.login-btn { padding: 14px 28px; font-size: 16px; }

/* Theme toggle */
.theme-toggle { background: none; border: 1px solid var(--border); border-radius: 6px; width: 32px; height: 32px; cursor: pointer; font-size: 14px; line-height: 1; display: inline-flex; align-items: center; justify-content: center; }
.theme-toggle:hover { border-color: var(--accent); }

/* Progress bars (goals, weekly KPI bar, budget) */
.progress-wrap { margin-bottom: 28px; }
.progress-label { display: flex; justify-content: space-between; margin-bottom: 8px; font-size: 14px; }
.progress-label .pct { font-weight: 700; }
.progress-bar-bg { background: var(--bg); border-radius: 8px; height: 20px; overflow: hidden; border: 1px solid var(--border); }
.progress-bar-fill { height: 100%; background: linear-gradient(90deg, var(--accent), var(--accent-soft)); border-radius: 8px 0 0 8px; transition: width 0.3s ease; }
.progress-bar-fill.complete { background: linear-gradient(90deg, var(--success-fill), var(--success)); }
.month-label { color: var(--text-muted); font-size: 13px; margin-bottom: 24px; }

/* Global weekly goal bar (below the topbar on every page) */
.goal-bar { background: var(--surface-alt); border-bottom: 1px solid var(--border); padding: 12px 32px; }
.goal-bar-inner { max-width: 900px; margin: 0 auto; }
.goal-bar-label { display: flex; justify-content: space-between; align-items: center; gap: 16px; margin-bottom: 6px; font-size: 13px; flex-wrap: wrap; }
.goal-bar-label .goal-bar-kpi { color: var(--accent-soft); text-decoration: none; font-weight: 600; }
.goal-bar-label .goal-bar-kpi:hover { text-decoration: underline; }
.goal-bar .progress-bar-bg { height: 8px; }
.goal-bar-empty { color: var(--text-muted); font-size: 13px; }
.goal-bar-empty a { color: var(--accent-soft); }

/* Pipeline (dashboard / downline drill-down) */
.pipeline-group { margin-bottom: 28px; }
.pipeline-group:last-child { margin-bottom: 0; }
.pipeline-group-title { font-size: 14px; text-transform: uppercase; letter-spacing: 0.03em; color: var(--text-muted); margin-bottom: 10px; }
.pipeline-count { background: var(--muted-bg); color: var(--text); border-radius: 10px; padding: 2px 9px; font-size: 12px; margin-left: 4px; }
.badge-followup { background: var(--warning-bg); color: var(--warning-text); }
.alert-card { border: 1px solid var(--warning); background: var(--warning-bg); }
.followup-list { list-style: none; padding: 0; margin: 12px 0 0; }
.followup-list li { display: flex; justify-content: space-between; align-items: center; padding: 6px 0; border-bottom: 1px solid var(--border); font-size: 14px; }
.followup-list li:last-child { border-bottom: none; }

/* Downline bubble tree */
.bubble-tree { list-style: none; padding-left: 0; display: flex; gap: 24px; }
.bubble-tree .bubble-tree { padding-top: 28px; }
.bubble-tree-node { text-align: center; position: relative; padding-top: 24px; }
.bubble-tree > li { text-align: center; position: relative; padding-top: 0; flex: 1; }
.bubble-tree li { position: relative; padding-top: 24px; }
.bubble-tree > li:first-child { padding-top: 0; }
.bubble-tree li li { flex: 1; }
.bubble-tree ul.bubble-tree { justify-content: center; border-top: 1px solid var(--border); padding-top: 24px; margin-top: 4px; }
.bubble-tree li li::before { content: ''; position: absolute; top: 0; left: 50%; width: 1px; height: 24px; background: var(--border); }
.bubble { display: inline-flex; flex-direction: column; align-items: center; gap: 6px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; min-width: 140px; text-decoration: none; color: var(--text); }
.bubble:hover { border-color: var(--accent); }
.bubble img, .bubble .bubble-avatar-fallback { width: 48px; height: 48px; border-radius: 50%; object-fit: cover; background: var(--muted-bg); display: flex; align-items: center; justify-content: center; font-weight: 700; }
.bubble-name { font-weight: 600; font-size: 14px; }
.bubble-comp { font-size: 11px; color: var(--text-muted); background: var(--bg); border-radius: 8px; padding: 1px 8px; }
.bubble-stats { font-size: 12px; color: var(--text-muted); }
.production-summary { display: flex; gap: 32px; margin-bottom: 24px; }
.production-stat { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 16px 24px; }
.production-stat .value { font-size: 22px; font-weight: 700; }
.production-stat .label { font-size: 12px; color: var(--text-muted); text-transform: uppercase; }

/* Gamified polish: subtle motion + energy without changing the layout */
.card { transition: box-shadow 0.2s ease, transform 0.2s ease; }
.bubble { transition: transform 0.15s ease, border-color 0.15s ease; }
.bubble:hover { transform: translateY(-2px); box-shadow: 0 6px 16px rgba(79, 70, 229, 0.25); }
.btn { transition: transform 0.1s ease, background 0.15s ease; }
.btn:active { transform: scale(0.97); }

.btn-live { background: linear-gradient(90deg, #22c55e, #16a34a); color: #fff; animation: live-pulse 2.4s ease-in-out infinite; }
.btn-live:hover { background: linear-gradient(90deg, #29d16b, #1eb058); }
@keyframes live-pulse { 0%, 100% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.5); } 50% { box-shadow: 0 0 0 6px rgba(34, 197, 94, 0); } }

/* Risk badge on a downline bubble */
.bubble.bubble-risk { border-color: var(--danger); box-shadow: 0 0 0 1px rgba(248, 113, 113, 0.4); }
.bubble-risk-flag { font-size: 10px; font-weight: 700; letter-spacing: 0.04em; color: var(--danger-text); background: var(--danger-bg); border-radius: 8px; padding: 1px 8px; }

/* Stat cards (persistency, achievements) */
.stat-row { display: flex; gap: 16px; flex-wrap: wrap; margin-bottom: 8px; }
.stat-pill { background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 12px 18px; min-width: 120px; }
.stat-pill .value { font-size: 20px; font-weight: 700; }
.stat-pill.stat-danger .value { color: var(--danger-text); }
.stat-pill.stat-good .value { color: var(--success-text); }
.stat-pill .label { font-size: 11px; color: var(--text-muted); text-transform: uppercase; }

.badge-achievement { display: inline-block; background: linear-gradient(90deg, var(--accent), var(--accent-soft)); color: #fff; border-radius: 12px; padding: 3px 12px; font-size: 12px; font-weight: 600; margin: 2px 4px 2px 0; }

/* Leaderboard */
.leaderboard-tabs { display: flex; gap: 8px; margin-bottom: 20px; }
.leaderboard-tabs a { padding: 8px 16px; border-radius: 8px; text-decoration: none; color: var(--text-muted); background: var(--surface); border: 1px solid var(--border); font-size: 13px; font-weight: 600; }
.leaderboard-tabs a.active { color: #fff; background: var(--accent); border-color: var(--accent); }
.leaderboard-row { display: flex; align-items: center; gap: 14px; padding: 12px 8px; border-bottom: 1px solid var(--border); }
.leaderboard-row:last-child { border-bottom: none; }
.leaderboard-rank { width: 32px; font-weight: 700; text-align: center; font-size: 16px; color: var(--text-muted); }
.leaderboard-row.rank-1 .leaderboard-rank { color: var(--warning); font-size: 20px; }
.leaderboard-row.rank-2 .leaderboard-rank { color: #cbd5e1; font-size: 18px; }
.leaderboard-row.rank-3 .leaderboard-rank { color: #d97706; font-size: 18px; }
.leaderboard-row img, .leaderboard-row .bubble-avatar-fallback { width: 36px; height: 36px; border-radius: 50%; object-fit: cover; background: var(--muted-bg); display: flex; align-items: center; justify-content: center; font-weight: 700; }
.leaderboard-name { flex: 1; font-weight: 600; }
.leaderboard-stats { text-align: right; font-size: 13px; color: var(--text-muted); }
.leaderboard-stats .ap { color: var(--success-text); font-weight: 700; font-size: 15px; display: block; }

/* Team activity feed */
.activity-item { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 14px; }
.activity-item:last-child { border-bottom: none; }
.activity-item .ap { color: var(--success-text); font-weight: 700; }

/* Confetti + toasts */
.confetti-piece { position: fixed; top: -10px; width: 8px; height: 14px; z-index: 9999; pointer-events: none; animation: confetti-fall linear forwards; }
@keyframes confetti-fall { to { transform: translateY(105vh) rotate(360deg); opacity: 0.3; } }
.toast-stack { position: fixed; bottom: 20px; right: 20px; z-index: 9998; display: flex; flex-direction: column; gap: 10px; }
.toast { background: var(--surface); border: 1px solid var(--accent); color: var(--text); padding: 14px 18px; border-radius: 10px; font-size: 14px; box-shadow: 0 8px 24px var(--shadow); animation: toast-in 0.3s ease; max-width: 320px; }
.toast-out { animation: toast-out 0.4s ease forwards; }
@keyframes toast-in { from { transform: translateX(40px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
@keyframes toast-out { to { transform: translateX(40px); opacity: 0; } }

/* Guided call-script application form */
.form-section { margin-bottom: 28px; padding-bottom: 24px; border-bottom: 1px solid var(--border); }
.form-section:last-child { border-bottom: none; margin-bottom: 0; padding-bottom: 0; }
.form-section-title { font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--accent-soft); margin-bottom: 14px; font-weight: 700; }
.script-callout { background: var(--surface-alt); border-left: 3px solid var(--accent); padding: 14px 18px; border-radius: 0 8px 8px 0; font-size: 13px; color: var(--text-muted); margin-bottom: 16px; white-space: pre-line; line-height: 1.6; }
.checklist { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; }
.checklist label { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--text); margin-bottom: 0; }
.checklist input[type="checkbox"] { width: auto; margin: 0; }
.radio-row { display: flex; gap: 20px; margin-bottom: 16px; }
.radio-row label { display: flex; align-items: center; gap: 6px; font-size: 14px; color: var(--text); margin-bottom: 0; }
.radio-row input[type="radio"] { width: auto; margin: 0; }
.disposition-bar { background: var(--surface); border-top: 1px solid var(--border); padding: 16px 24px; margin: 24px -24px -24px; border-radius: 0 0 10px 10px; display: flex; gap: 12px; align-items: center; }
.disposition-bar select { margin: 0; width: auto; flex: 1; }

/* Carriers page */
.carrier-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px 16px; }
.carrier-col-header { font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); font-weight: 700; text-align: center; padding-bottom: 4px; }
.carrier-btn { display: flex; align-items: center; justify-content: center; text-align: center; background: var(--surface); border: 1px solid var(--border); color: var(--text); text-decoration: none; border-radius: 8px; padding: 14px 12px; font-size: 13px; font-weight: 600; transition: transform 0.15s ease, border-color 0.15s ease; }
.carrier-btn:hover { border-color: var(--accent); transform: translateY(-1px); }
.carrier-btn-eapp { background: color-mix(in srgb, var(--accent) 12%, var(--surface)); }
.carrier-btn-empty { color: var(--text-muted); cursor: not-allowed; border-style: dashed; }
@media (max-width: 700px) {
  .carrier-grid { grid-template-columns: 1fr; }
  .carrier-col-header:nth-child(2), .carrier-col-header:nth-child(3) { display: none; }
}

/* Mobile-responsive pass. The PWA is the "app" on iOS, so this is what
   makes it actually feel like one on a phone rather than a shrunk desktop
   page. */
.nav-toggle { display: none; background: none; border: 1px solid var(--border); border-radius: 6px; width: 36px; height: 36px; color: var(--text); font-size: 16px; cursor: pointer; align-items: center; justify-content: center; }
.nav-toggle:hover { border-color: var(--accent); }

@media (max-width: 640px) {
  body { font-size: 15px; }
  .container { padding: 0 16px; margin: 20px auto; }
  h1 { font-size: 19px; margin-bottom: 16px; }
  .card { padding: 16px; }

  .topbar { padding: 12px 16px; }
  .nav-toggle { display: inline-flex; }
  .topbar nav { display: none; width: 100%; order: 3; flex-direction: column; align-items: stretch; gap: 2px; margin-top: 8px; }
  .topbar nav.open { display: flex; }
  .topbar nav a { padding: 12px 4px; border-top: 1px solid var(--border); font-size: 15px; }
  .topbar nav .btn-live,
  .topbar nav form { width: 100%; margin: 6px 0 0; }
  .topbar nav .theme-toggle { width: 100%; border-radius: 6px; }

  .goal-bar { padding: 10px 16px; }
  .goal-bar-label { font-size: 12px; }

  /* Classic CSS-only responsive-table trick: scroll the table itself
     instead of breaking the page layout. Applies to every <table> in the
     app (pipeline, expenses, audit log, applications list) without
     needing a wrapper element in each template. */
  table { display: block; overflow-x: auto; white-space: nowrap; -webkit-overflow-scrolling: touch; }

  .production-summary { flex-wrap: wrap; gap: 12px; }
  .production-stat { flex: 1 1 calc(50% - 12px); padding: 12px 16px; }

  .bubble-tree { overflow-x: auto; padding-bottom: 8px; -webkit-overflow-scrolling: touch; }
  .bubble { min-width: 110px; padding: 10px 12px; }

  .carrier-btn { padding: 12px 10px; }

  .radio-row { flex-direction: column; gap: 10px; }

  .disposition-bar { flex-direction: column; align-items: stretch; }
  .disposition-bar select { width: 100%; }

  .toast-stack { left: 12px; right: 12px; bottom: 12px; }
  .toast { max-width: none; }
}
