/* static/css/insights.css — the History dashboard (static/js/insights.js).
 *
 * Its own file: app.css is at its size cap and batch.css holds the old strips this replaces.
 * The two old strips are hidden ONLY under #p-history[data-insights], which insights.js sets
 * (pending, then on) and removes on an empty or failed load. Delete the script tag and no rule
 * below matches anything, so History is exactly the pre-dashboard page (C3).
 *
 * Layout is the 2026-10-16 design handoff: a centred 1760px container, five top-aligned KPI
 * tiles, then a 12-column grid with named areas (2 columns below 1440px, 1 below 900px). The
 * bar chart is HTML with a fixed pixel plot height, so its text never scales with the width,
 * and the heatmap is a fluid 53-column grid of square cells.
 *
 * Tokens are scoped to #bx-insights, not :root. Light uses the handoff's palette verbatim; the
 * handoff had no dark palette, so dark maps each token onto the app's own (app.css :root), with
 * the deeper mark gold the first version validated for filled marks on --s2.
 */
#p-history[data-insights] #bx-loyalty,
#p-history[data-insights] #bx-pulse { display: none; }

/* ---------- Tokens ---------- */
#bx-insights, #bx-history {
  --pk-bg: var(--bg);
  --pk-card: var(--s2);
  --pk-border: var(--border);
  --pk-border-strong: var(--border2);
  --pk-text: var(--text);
  --pk-muted: var(--dim);
  --pk-faint: var(--muted);
  --pk-track: var(--s3);
  --pk-subtle: var(--s1);
  --pk-gold: #b0892a;
  --pk-gold-text: var(--gold);
  --pk-gold-tint: var(--gold-bg);
  --pk-gold-ink: var(--gold-light);
  --pk-video: #6f78ee;
  --pk-heat-0: var(--s3);
  --pk-heat-1: rgba(var(--gold-rgb), .28);
  --pk-heat-2: rgba(var(--gold-rgb), .5);
  --pk-heat-3: rgba(var(--gold-rgb), .75);
  --pk-heat-4: var(--gold);
  --pk-empty-bar: var(--border);
  --pk-risk-high: #f97066;
  --pk-risk-medium: #fdb022;
  --pk-risk-passed: #47cd89;
  --pk-radius: 14px;
  --pk-gap: 16px;
  --pk-chart-h: clamp(170px, 20vh, 220px);
  --pk-font: 'Segoe UI', system-ui, sans-serif;   /* the app's own stack (app.css body) */
}
[data-theme="light"] #bx-insights, [data-theme="light"] #bx-history {
  --pk-bg: #F4F5FB;
  --pk-card: #FFFFFF;
  --pk-border: #E1E4F2;
  --pk-border-strong: #D5D9EC;
  --pk-text: #161830;
  --pk-muted: #5E6382;      /* 6:1 on white: secondary text */
  --pk-faint: #6B7090;      /* 5:1 on white: axis labels */
  --pk-track: #ECEEF7;      /* meter tracks, empty heat cells, dividers */
  --pk-subtle: #F7F8FC;
  --pk-gold: #B07D00;       /* images, primary data color */
  --pk-gold-text: #8A6200;  /* gold used AS text (contrast-safe) */
  --pk-gold-tint: #FBF3DC;
  --pk-gold-ink: #6B4C00;
  --pk-video: #5046E5;      /* videos */
  --pk-heat-0: #ECEEF7;
  --pk-heat-1: #F0DFA9;
  --pk-heat-2: #DDBA57;
  --pk-heat-3: #C29423;
  --pk-heat-4: #8F6600;
  --pk-empty-bar: #DDE0EE;
  --pk-risk-high: #B42318;
  --pk-risk-medium: #C27803;
  --pk-risk-passed: #1F7A4D;
}

/* ---------- Page shell ---------- */
/* The tab panel pads 20px 22px; the shell bleeds over that so .pk-wrap is the only gutter. */
.pk-dash {
  margin: -20px -22px 0;
  font-family: var(--pk-font);
  color: var(--pk-text);
  background: var(--pk-bg);
  font-variant-numeric: tabular-nums;
}
.pk-dash *, .pk-dash *::before, .pk-dash *::after { box-sizing: border-box; }
.pk-wrap {
  max-width: 1760px;
  margin: 0 auto;
  padding: 24px clamp(16px, 2.5vw, 40px) 40px;
  display: flex;
  flex-direction: column;
  gap: var(--pk-gap);
}

/* ---------- Header ---------- */
.pk-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 12px; }
.pk-eyebrow { font-size: 12px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--pk-gold-text); }
.pk-title { margin: 2px 0 0; font-size: 26px; font-weight: 600; letter-spacing: -.02em; color: var(--pk-text); }
.pk-head__actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.pk-head__meta { font-size: 13px; color: var(--pk-muted); }
.pk-btn {
  height: 40px; padding: 0 16px; border-radius: 10px;
  border: 1px solid var(--pk-border-strong); background: var(--pk-card);
  font: inherit; font-size: 14px; font-weight: 600; color: var(--pk-text);
  display: inline-flex; align-items: center; gap: 8px; cursor: pointer;
}
.pk-btn:hover { border-color: var(--pk-gold); }
.pk-btn:focus-visible, .pk-link:focus-visible { outline: 2px solid var(--pk-video); outline-offset: 2px; }

/* ---------- Cards ---------- */
.pk-card {
  background: var(--pk-card);
  border: 1px solid var(--pk-border);
  border-radius: var(--pk-radius);
  padding: 18px 22px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-width: 0;            /* critical: lets grid children shrink */
}
.pk-card__head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: baseline; gap: 8px; }
.pk-card__title { margin: 0; font-size: 15px; font-weight: 600; line-height: 1.3; color: var(--pk-text); }
.pk-card__meta { font-size: 12.5px; color: var(--pk-muted); }
.pk-card__empty { font-size: 13.5px; color: var(--pk-muted); }
.pk-pill { font-size: 12px; font-weight: 600; color: var(--pk-gold-text); background: var(--pk-gold-tint); border-radius: 999px; padding: 3px 10px; }
.pk-link { font-size: 13px; font-weight: 600; color: var(--pk-gold-text); text-decoration: none; }
.pk-link:hover { color: var(--pk-gold-ink); text-decoration: underline; }
/* The two in-app actions (top-up modal, Images tab) are buttons that look like the link. */
button.pk-link { background: none; border: 0; padding: 0; font-family: inherit; cursor: pointer; text-align: left; }
.pk-card__foot { margin-top: auto; align-self: flex-start; }

/* ---------- KPI row ---------- */
/* padding: 0 beats app.css's landing-page `section { padding: 60px 28px }`. */
.pk-kpis { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: var(--pk-gap); padding: 0; }
.pk-kpi {
  background: var(--pk-card); border: 1px solid var(--pk-border); border-radius: var(--pk-radius);
  padding: 18px 20px; min-width: 0;
  display: flex; flex-direction: column; justify-content: flex-start; gap: 4px;   /* TOP-aligned: every value on one baseline */
}
.pk-kpi__label { font-size: 13px; font-weight: 500; color: var(--pk-muted); }
.pk-kpi__value { font-size: 32px; font-weight: 600; letter-spacing: -.02em; line-height: 1.15; white-space: nowrap; color: var(--pk-text); }
.pk-kpi__sub { font-size: 12.5px; color: var(--pk-muted); }
.pk-kpi__foot { margin-top: auto; padding-top: 10px; height: 38px; display: flex; align-items: center; }
.pk-spark { width: 100%; height: 28px; display: block; }
.pk-spark polyline { fill: none; stroke: var(--pk-gold); stroke-width: 2; stroke-linejoin: round; vector-effect: non-scaling-stroke; }

/* ---------- Meter ---------- */
.pk-meter { flex: 1; height: 6px; border-radius: 3px; background: var(--pk-track); overflow: hidden; }
.pk-meter__fill { height: 100%; width: var(--pct, 0%); background: var(--pk-gold); border-radius: 3px; }

/* ---------- Main grid ---------- */
.pk-grid {
  display: grid;
  gap: var(--pk-gap);
  grid-template-columns: repeat(12, minmax(0, 1fr));
  grid-template-areas:
    "act act act act act act act act mon mon mon mon"
    "bar bar bar bar bar bar rej rej rej rec rec rec"
    "kw  kw  kw  kw  kw  kw  kw  kw  kw  kw  kw  kw";
}
.pk-area-act { grid-area: act; }
.pk-area-mon { grid-area: mon; }
.pk-area-bar { grid-area: bar; }
.pk-area-rej { grid-area: rej; }
.pk-area-rec { grid-area: rec; }
.pk-area-kw  { grid-area: kw; }

@media (max-width: 1439px) {
  .pk-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-areas: "act act" "mon rec" "bar bar" "rej kw";
  }
}
@media (max-width: 1279px) {
  .pk-kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (max-width: 899px) {
  .pk-grid { grid-template-columns: minmax(0, 1fr); grid-template-areas: "act" "mon" "rej" "bar" "rec" "kw"; }
  .pk-kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  /* An odd tile count leaves the last tile alone on its row (Credits, with all five). */
  .pk-kpi:nth-child(odd):last-child { grid-column: 1 / -1; }
  .pk-kpi__value { font-size: 26px; }
  .pk-hm-scroll { overflow-x: auto; }
  .pk-hm { min-width: 640px; }
}

/* ---------- Heatmap ---------- */
.pk-hm {
  display: grid;
  grid-template-columns: 30px repeat(53, minmax(0, 1fr));
  gap: 3px;
}
.pk-hm__month { grid-row: 1; font-size: 11px; color: var(--pk-faint); white-space: nowrap; padding-bottom: 2px; }
/* line-height 0: a label must not make its row taller than a small (9px) cell. */
.pk-hm__day { grid-column: 1; font-size: 11px; line-height: 0; color: var(--pk-faint); align-self: center; }
.pk-hm__cell { aspect-ratio: 1; border-radius: 3px; background: var(--pk-heat-0); }
.pk-hm__cell[data-level="1"] { background: var(--pk-heat-1); }
.pk-hm__cell[data-level="2"] { background: var(--pk-heat-2); }
.pk-hm__cell[data-level="3"] { background: var(--pk-heat-3); }
.pk-hm__cell[data-level="4"] { background: var(--pk-heat-4); }
.pk-hm__cell:hover { outline: 2px solid var(--pk-text); outline-offset: 1px; }
.pk-hm-legend { display: flex; justify-content: flex-end; align-items: center; gap: 4px; font-size: 11px; color: var(--pk-faint); }
.pk-hm-legend i { width: 11px; height: 11px; border-radius: 2px; display: inline-block; }

/* ---------- Bar chart (HTML, not SVG) ---------- */
.pk-legend { display: flex; gap: 14px; font-size: 12.5px; color: var(--pk-muted); }
.pk-legend span { display: inline-flex; align-items: center; gap: 6px; }
.pk-swatch { width: 10px; height: 10px; border-radius: 2px; display: inline-block; flex: none; }
.pk-swatch--img { background: var(--pk-gold); }
.pk-swatch--vid { background: var(--pk-video); }

.pk-bars {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr);
  grid-template-rows: var(--pk-chart-h) auto;
  column-gap: 8px;
  row-gap: 8px;
  padding-top: 18px;               /* room for the value label of the tallest bar */
}
.pk-bars__axis { position: relative; font-size: 11px; color: var(--pk-faint); }
.pk-bars__axis span { position: absolute; right: 0; transform: translateY(50%); }
.pk-bars__plot { position: relative; }
.pk-bars__grid { position: absolute; left: 0; right: 0; border-top: 1px dashed var(--pk-border); }
.pk-bars__grid--base { border-top: 1px solid var(--pk-border-strong); }
.pk-bars__cols { position: relative; display: flex; gap: 6px; height: 100%; }
.pk-bar { position: relative; flex: 1; min-width: 0; height: 100%; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; }
.pk-bar__stack {
  width: 64%; max-width: 46px; height: var(--h);
  display: flex; flex-direction: column;
  border-radius: 4px 4px 0 0; overflow: hidden;
}
.pk-bar__vid { background: var(--pk-video); flex: 0 0 var(--vid); }
.pk-bar__img { background: var(--pk-gold); flex: 1 1 auto; }
.pk-bar--empty .pk-bar__stack { height: 2px; background: var(--pk-empty-bar); border-radius: 1px; }
.pk-bar__value {
  position: absolute; bottom: calc(var(--h) + 4px); left: 0; right: 0;
  text-align: center; font-size: 11.5px; font-weight: 600; color: var(--pk-text);
}
.pk-bars__labels { grid-column: 2; display: flex; gap: 6px; }
.pk-bars__labels span { flex: 1; min-width: 0; text-align: center; font-size: 12px; color: var(--pk-faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pk-bars__labels span.is-current { color: var(--pk-text); font-weight: 600; }
/* Beside the sidebar a column is ~55px, a hair short of "Oct · so far": let it run into the
   gap. Not on phones, where every label is a few pixels wide and it would spill off the card. */
@media (min-width: 900px) { .pk-bars__labels span.is-current { overflow: visible; text-overflow: clip; } }

/* ---------- This month ---------- */
.pk-month { display: flex; align-items: center; gap: 22px; flex-wrap: wrap; }
.pk-donut {
  --video-pct: 0%;
  position: relative; width: 132px; height: 132px; flex: none; border-radius: 50%;
  background: conic-gradient(var(--pk-video) 0 var(--video-pct), var(--pk-gold) var(--video-pct) 100%);
}
.pk-donut__hole {
  position: absolute; inset: 15px; border-radius: 50%; background: var(--pk-card);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.pk-donut__num { font-size: 30px; font-weight: 600; letter-spacing: -.02em; line-height: 1; }
.pk-donut__unit { font-size: 12px; color: var(--pk-muted); margin-top: 4px; }
.pk-month__stats { flex: 1; min-width: 170px; display: flex; flex-direction: column; gap: 10px; }
.pk-month__stats .pk-meter { flex: none; }
.pk-stat { display: flex; align-items: center; gap: 8px; font-size: 13.5px; }
.pk-stat__label { color: var(--pk-muted); }
.pk-stat__value { margin-left: auto; font-weight: 600; }
.pk-stat__value small { color: var(--pk-muted); font-weight: 500; font-size: inherit; }
.pk-divider { height: 1px; background: var(--pk-track); border: 0; margin: 0; }

/* ---------- Lists (Records, Reject Radar) ---------- */
.pk-rows { display: flex; flex-direction: column; margin: 0; padding: 0; list-style: none; }
.pk-row { display: flex; align-items: center; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--pk-track); font-size: 13.5px; }
.pk-row:last-child { border-bottom: 0; }
.pk-row__sub { display: block; font-size: 12px; color: var(--pk-muted); }
.pk-row__value { margin-left: auto; font-size: 15px; font-weight: 600; white-space: nowrap; }
.pk-dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.pk-dot--high { background: var(--pk-risk-high); }
.pk-dot--medium { background: var(--pk-risk-medium); }
.pk-dot--passed { background: var(--pk-risk-passed); }

/* ---------- Milestones ---------- */
.pk-milestone { margin-top: auto; background: var(--pk-subtle); border-radius: 10px; padding: 12px 14px; display: flex; flex-direction: column; gap: 8px; }
.pk-milestone__head { display: flex; justify-content: space-between; gap: 8px; font-size: 12.5px; }
.pk-milestone__track { display: flex; gap: 3px; }
.pk-milestone__seg { flex: 1; height: 6px; border-radius: 3px; background: var(--pk-track); overflow: hidden; }
.pk-milestone__seg > i { display: block; height: 100%; width: var(--pct, 0%); background: var(--pk-gold); }
.pk-milestone__ticks { display: flex; font-size: 11px; color: var(--pk-faint); }
.pk-milestone__ticks span { flex: 1; text-align: right; }
/* A milestone crossed since the last visit: one quiet glow, no confetti. */
.pk-milestone--new { animation: pkGlow 2.4s ease-out 1; }
@keyframes pkGlow {
  0%, 100% { box-shadow: 0 0 0 0 rgba(var(--gold-rgb), 0); }
  30% { box-shadow: 0 0 0 3px rgba(var(--gold-rgb), .35); }
}

/* ---------- Keyword chips ---------- */
.pk-chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 0; padding: 0; list-style: none; }
.pk-chip { display: inline-flex; align-items: center; height: 32px; padding: 0 12px; border-radius: 999px; font-size: 13.5px; font-weight: 500; background: var(--pk-bg); color: var(--pk-text); }
.pk-chip--top { background: var(--pk-gold-tint); color: var(--pk-gold-ink); font-weight: 600; }

/* ---------- Contributor tier (below the grid, only when the loyalty payload has one) ---------- */
.pk-tier__track { display: flex; align-items: center; gap: 12px; }
.pk-tier__name { font-size: 13px; color: var(--pk-muted); white-space: nowrap; }
.pk-tier__name.is-current { color: var(--pk-gold-text); font-weight: 600; }
.pk-tier__line { font-size: 13.5px; color: var(--pk-muted); }
.pk-tier__line b { color: var(--pk-text); font-weight: 600; }
@media (max-width: 899px) { .pk-tier__track { flex-wrap: wrap; } }

@media (prefers-reduced-motion: reduce) {
  .pk-milestone--new { animation: none; }
}

/* ---------- Motion layer (insights_motion.js, insights_story.js) ---------- */
/* .pk-dash already sets tabular digits, so the odometer's columns and the plain text it hands
   back to are the same width: no jump when a roll finishes. */
.pk-odo { display: inline-block; }
.pk-odo__col { display: inline-block; height: 1.15em; overflow: hidden; vertical-align: top; }
.pk-odo__strip { display: flex; flex-direction: column; will-change: transform; }
.pk-odo__c { display: block; height: 1.15em; line-height: 1.15; }
.pk-odo__s { display: inline-block; height: 1.15em; vertical-align: top; }
/* The true text, for screen readers; the strips above are aria-hidden. */
.pk-odo-sr {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* "+37 since you last looked", leading the tile's sub line. */
.pk-kpi__delta {
  display: inline-flex; align-items: center; margin-right: 6px; padding: 1px 8px;
  border-radius: 999px; font-size: 12px; font-weight: 600; line-height: 1.5; vertical-align: 1px;
}
.pk-kpi__delta--up { color: var(--pk-gold-text); background: var(--pk-gold-tint); }
.pk-kpi__delta--down { color: var(--pk-muted); background: var(--pk-track); }

/* Highlights: up to three short sentences between the header and the tiles. */
.pk-stories {
  list-style: none; margin: 0; padding: 0; display: grid; gap: var(--pk-gap);
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
}
.pk-story {
  display: flex; align-items: flex-start; gap: 12px; min-width: 0;
  background: var(--pk-card); border: 1px solid var(--pk-border); border-radius: var(--pk-radius);
  padding: 14px 16px;
}
.pk-story__icon {
  flex: none; width: 32px; height: 32px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--pk-gold-text); background: var(--pk-gold-tint);
}
.pk-story--calm .pk-story__icon { color: var(--pk-muted); background: var(--pk-track); }
.pk-story__text { margin: 0; font-size: 14px; line-height: 1.45; color: var(--pk-text); text-wrap: pretty; }
