/* frontend/css/styles.css */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --border: #e7e9ee;
  --text: #0f1729;
  --muted: #6b7385;
  --faint: #9aa1b1;
  --accent: #5b5bd6;
  --accent-soft: #eef0fd;
  --high: #e5484d;
  --high-soft: #fdeced;
  --warn: #f0a020;
  --warn-soft: #fdf3e3;
  --good: #11936b;
  --good-soft: #e6f5ef;
  --shadow: 0 1px 2px rgba(16, 23, 41, .04), 0 6px 18px rgba(16, 23, 41, .06);
  --radius: 14px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem; flex-wrap: wrap;
  padding: .85rem 1.6rem;
  background: rgba(255, 255, 255, .82);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: .75rem; }
.logo {
  width: 38px; height: 38px; border-radius: 10px;
  display: grid; place-items: center;
  background: linear-gradient(135deg, #5b5bd6, #8a7bf0);
  color: #fff; font-size: 1.4rem; box-shadow: var(--shadow);
}
.brand h1 { margin: 0; font-size: 1.15rem; letter-spacing: -.01em; }
.brand .sub { color: var(--muted); font-size: .8rem; }
.mode-chip {
  display: inline-block; margin-left: .45rem; padding: .08rem .55rem;
  border-radius: 999px; font-size: .72rem; font-weight: 600;
  vertical-align: 1px;
}
/* display:inline-block above would override the UA's [hidden] rule */
.mode-chip[hidden] { display: none; }
.mode-demo { color: var(--accent); background: var(--accent-soft); }
.mode-live { color: var(--good); background: var(--good-soft); }

.controls { display: flex; gap: .6rem; align-items: center; }
.controls select, .controls button {
  font: inherit; font-size: .88rem;
  border: 1px solid var(--border); border-radius: 9px;
  padding: .5rem .8rem; background: var(--surface); color: var(--text);
}
.controls select { cursor: pointer; }
.controls button {
  cursor: pointer; background: var(--accent); color: #fff;
  border-color: var(--accent); font-weight: 600;
  transition: filter .15s ease;
}
.controls button:hover { filter: brightness(1.07); }

/* ---------- layout ---------- */
main { max-width: 1240px; margin: 0 auto; padding: 1.6rem 1.6rem 2.5rem; }
main.loading { opacity: .45; pointer-events: none; transition: opacity .15s ease; }
section { margin-bottom: 1.9rem; }
.section-head h2 { margin: 0 0 .15rem; font-size: 1.25rem; letter-spacing: -.01em; }
.section-head p { margin: 0 0 1rem; color: var(--muted); font-size: .92rem; }
.block-title {
  margin: 0 0 .8rem; font-size: .78rem; font-weight: 600;
  text-transform: uppercase; letter-spacing: .08em; color: var(--faint);
}

/* ---------- action center ---------- */
.highlight-grid {
  display: grid; gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
}
.highlight {
  /* reset <button> defaults */
  appearance: none; font: inherit; text-align: left; width: 100%;
  position: relative; overflow: hidden;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1.15rem 1.2rem 1.1rem 1.4rem;
  box-shadow: var(--shadow); cursor: pointer;
  transition: transform .15s ease, box-shadow .15s ease;
}
.highlight:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.highlight::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 5px;
}
.highlight:hover { transform: translateY(-2px);
  box-shadow: 0 2px 4px rgba(16,23,41,.05), 0 12px 28px rgba(16,23,41,.10); }
.highlight.high::before { background: var(--high); }
.highlight.medium::before { background: var(--warn); }
.highlight.opportunity::before { background: var(--good); }

.hl-tag {
  display: inline-block; font-size: .68rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: .06em;
  padding: .2rem .5rem; border-radius: 999px; margin-bottom: .55rem;
}
.high .hl-tag { background: var(--high-soft); color: var(--high); }
.medium .hl-tag { background: var(--warn-soft); color: #8a5200; }
.opportunity .hl-tag { background: var(--good-soft); color: var(--good); }

.hl-value { font-size: 1.85rem; font-weight: 750; letter-spacing: -.02em; }
.high .hl-value { color: var(--high); }
.opportunity .hl-value { color: var(--good); }
.medium .hl-value { color: #b97608; }
.hl-title { font-weight: 650; margin: .15rem 0 .35rem; }
.hl-detail { color: var(--muted); font-size: .88rem; }
.hl-action {
  margin-top: .7rem; padding-top: .7rem; border-top: 1px dashed var(--border);
  font-size: .86rem; font-weight: 600; color: var(--accent);
}
.hl-action::before { content: "→ "; }

/* ---------- KPIs ---------- */
.kpi-grid {
  display: grid; gap: .9rem;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
}
.kpi {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: .95rem 1.05rem; box-shadow: var(--shadow);
}
.kpi .value { font-size: 1.5rem; font-weight: 720; letter-spacing: -.01em; }
.kpi .label { color: var(--muted); font-size: .8rem; margin-top: .15rem; }
.kpi .label .accent { color: var(--accent); }
/* Money-first: the headline revenue KPI dominates the snapshot at a glance. */
.kpi-primary {
  background: linear-gradient(180deg, var(--accent-soft), var(--surface));
  border-color: #d4d6fb;
}
.kpi-primary .value { font-size: 1.95rem; color: var(--accent); }

/* ---------- charts ---------- */
.charts {
  display: grid; gap: 1rem;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1.1rem 1.2rem; box-shadow: var(--shadow);
}
.card.span-2 { grid-column: span 2; }
.card-head {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: .75rem; margin-bottom: .8rem;
}
.card-head h3 { margin: 0; font-size: 1rem; }
.card-hint { color: var(--faint); font-size: .78rem; }
.charts canvas { max-height: 300px; }
.chart-clickable { cursor: pointer; }
.empty-note { color: var(--faint); font-size: .88rem; margin: .4rem 0 0; padding: 0; }

/* ---------- segment explorer ---------- */
.detail { display: block; }
.seg-empty { color: var(--faint); padding: .4rem 0; }
/* Contextual note shown when the explorer is opened from the at-risk card,
   clarifying that the card's count is a subset of the full segment. */
.seg-note {
  margin-top: .7rem; padding: .55rem .8rem;
  background: var(--high-soft); border: 1px solid #f5c5c6;
  border-radius: 10px; font-size: .85rem; color: #8a2c2f;
}
.seg-title { font-size: 1.05rem; font-weight: 700; }
.seg-title .badge {
  font-size: .72rem; font-weight: 600; color: var(--accent);
  background: var(--accent-soft); padding: .15rem .55rem;
  border-radius: 999px; margin-left: .5rem; vertical-align: middle;
}
.metric-grid {
  display: grid; gap: .75rem; margin-top: .9rem;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
}
.metric {
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 10px; padding: .7rem .85rem;
}
.metric .m-val { font-size: 1.2rem; font-weight: 700; }
.metric .m-lab { color: var(--muted); font-size: .76rem; margin-top: .1rem; }
.insight {
  margin-top: 1.1rem; padding: .95rem 1.1rem;
  background: var(--accent-soft); border: 1px solid #dfe1fb;
  border-radius: 12px; font-size: .92rem;
}
.insight strong { color: var(--accent); }
.ins-headline { font-weight: 700; font-size: 1rem; margin-bottom: .5rem; }
.ins-detail { color: var(--muted); font-size: .87rem; }
.ins-detail div { margin-bottom: .15rem; }
.ins-action {
  margin-top: .7rem; padding-top: .7rem; border-top: 1px dashed var(--border);
  font-size: .9rem; font-weight: 650; color: var(--accent);
}
.ins-action::before { content: "→ "; }
.ins-impact { margin-top: .45rem; font-size: .82rem; color: var(--muted); font-style: italic; }

/* ---------- footer ---------- */
.foot {
  max-width: 1240px; margin: 0 auto; padding: 0 1.6rem 2.5rem;
  color: var(--faint); font-size: .8rem;
}
.foot code { background: #eef0f4; padding: .05rem .35rem; border-radius: 5px; }

/* ---------- secondary button ---------- */
.controls .btn-secondary {
  background: var(--surface); color: var(--muted);
  border-color: var(--border); font-weight: 500;
}
.controls .btn-secondary:hover { background: var(--bg); filter: none; }

/* ---------- status banner ---------- */
#status { margin-bottom: .75rem; }
.status-banner {
  padding: .65rem 1rem; border-radius: 10px;
  font-size: .9rem; display: flex; align-items: center; gap: .5rem;
}
.status-loading { background: var(--accent-soft); color: var(--accent); }
.status-error { background: var(--high-soft); color: var(--high); border: 1px solid #f5c5c6; }
.status-info { background: var(--good-soft); color: var(--good); }
.spin { display: inline-block; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- decision center (was action center) ---------- */
.highlight .hl-detail { color: var(--muted); font-size: .87rem; margin-top: .2rem; }
.highlight .hl-detail div { margin-bottom: .15rem; }
.hl-impact {
  margin-top: .45rem; font-size: .82rem; color: var(--muted);
  font-style: italic;
}
.dl { font-weight: 600; color: var(--text); }

/* ---------- KPI benchmark badges ---------- */
.kpi-badge {
  display: inline-block; font-size: .65rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: .04em;
  padding: .1rem .42rem; border-radius: 999px; margin-left: .35rem;
  vertical-align: middle;
}
/* Darker foregrounds than the brand --good/--high so small badge text meets
   WCAG AA (4.5:1) contrast on the soft tinted backgrounds. */
.badge-good { background: var(--good-soft); color: #0a6e4e; }
.badge-bad  { background: var(--high-soft); color: #c12026; }
.badge-neutral { background: var(--accent-soft); color: var(--accent); }

/* ---------- estimate labels ---------- */
.est-tag {
  display: inline-block; font-size: .68rem; font-weight: 700;
  color: #5a6273; margin-left: .3rem; vertical-align: middle;  /* AA on --bg tiles */
  text-transform: uppercase; letter-spacing: .03em;
}
.est-label {
  display: inline-block; font-size: .7rem; font-weight: 600;
  color: var(--faint); margin-left: .4rem; vertical-align: middle;
  text-transform: uppercase; letter-spacing: .03em; border: 1px solid var(--border);
  border-radius: 999px; padding: .05rem .4rem;
}

/* ---------- retention cohort table ---------- */
.cohort-wrap { margin-top: 1.2rem; overflow-x: auto; }
.cohort-table {
  width: 100%; border-collapse: collapse; font-size: .86rem;
}
.cohort-table th, .cohort-table td {
  padding: .5rem .75rem; text-align: left;
  border-bottom: 1px solid var(--border);
}
.cohort-table th {
  font-size: .76rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: .05em;
  color: var(--faint); background: var(--bg);
}
.cohort-table tbody tr:hover { background: var(--bg); }
.cohort-low-conf { opacity: .55; font-style: italic; }

/* ---------- revenue ---------- */
.revenue-tiles {
  display: grid; gap: .8rem; margin-bottom: 1.2rem;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
}
.rev-tile {
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 10px; padding: .75rem .9rem;
}
.rev-val { font-size: 1.3rem; font-weight: 720; letter-spacing: -.01em; }
.rev-lab { color: var(--muted); font-size: .78rem; margin-top: .1rem; }
.rev-charts {
  display: grid; gap: 1rem;
  grid-template-columns: repeat(2, 1fr);
}
.chart-sublabel {
  font-size: .78rem; color: var(--faint); margin-bottom: .4rem; font-weight: 600;
}
.rev-charts canvas { max-height: 220px; }

/* ---------- churn drivers ---------- */
#driversChart { max-height: 260px; }
.drivers-note {
  margin-top: .75rem; font-size: .82rem; color: var(--faint);
  font-style: italic; border-top: 1px solid var(--border); padding-top: .6rem;
}

/* ---------- risk colours ---------- */
.risk-high { color: var(--high); }
.risk-med { color: var(--warn); }
.risk-low { color: var(--good); }

/* ---------- segment extras ---------- */
.seg-drivers {
  margin-top: .8rem; font-size: .86rem; color: var(--muted);
}

/* ---------- AI summary ---------- */
.ai-summary {
  margin-bottom: 1.4rem;
  border-left: 3px solid var(--accent);
  background: linear-gradient(180deg, var(--accent-soft) 0%, var(--surface) 60%);
}
.ai-summary > details > summary { list-style: none; cursor: pointer; }
.ai-summary > details > summary::-webkit-details-marker { display: none; }
.ai-head {
  display: flex; align-items: center; gap: .55rem;
}
.ai-spark { color: var(--accent); font-size: 1.05rem; }
.ai-title { font-size: 1rem; font-weight: 700; }
.ai-provider {
  font-size: .68rem; font-weight: 600; text-transform: uppercase;
  letter-spacing: .05em; padding: .12rem .4rem; border-radius: 6px;
}
.ai-provider.ai-llm { color: var(--good); background: var(--good-soft); }
.ai-provider.ai-template { color: var(--muted); background: var(--accent-soft); }
.ai-cached { margin-left: auto; color: var(--faint); font-size: .74rem; }
.ai-body { margin-top: .85rem; }
.ai-pending { color: var(--muted); font-size: .9rem; font-style: italic; }
.ai-text { margin: 0 0 .9rem; font-size: .95rem; line-height: 1.5; color: var(--text); }
.ai-findings { list-style: none; margin: 0; padding: 0; display: grid; gap: .6rem; }
.ai-finding {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: .6rem .75rem;
}
.ai-f-title { font-weight: 600; font-size: .88rem; }
.ai-f-detail { color: var(--muted); font-size: .82rem; margin-top: .15rem; }
.ai-f-action { color: var(--accent); font-size: .82rem; margin-top: .3rem; }
.ai-focus {
  margin-top: .9rem; padding-top: .7rem; border-top: 1px solid var(--border);
  font-size: .88rem; color: var(--text);
}
.ai-focus-lab { font-weight: 600; color: var(--muted); }

/* ---------- responsive ---------- */
@media (max-width: 820px) {
  .charts { grid-template-columns: 1fr; }
  .card.span-2 { grid-column: span 1; }
  .topbar { padding: .75rem 1rem; }
  main { padding: 1.1rem 1rem 2rem; }
  .rev-charts { grid-template-columns: 1fr; }
  .ai-cached { display: none; }
}
