/* ============================================================
   Brand & Store — Fulfillment Performance  (bs-ffm-*)
   Scoped to .bs-ffm-page.  Uses design tokens from variables.css.
   Dual-platform layout: SLA Overview → Section A (1A/1B) → Section B (2A/2B).
   ============================================================ */

/* STICKY-HEADER ENABLER (route override — mirrors #aging + #atp, 2026-05-31):
   the router outlet .page-content is `overflow-y: auto`, which makes IT the
   sticky scroll container — but it never scrolls internally (the document
   scrolls), so the sticky BsSharedHeader filter bar never pins. Dropping it to
   `visible` (route-scoped) lets the header stick to the document viewport.
   .content-area gets min-width:0 so the wide capture/heatmap tables don't
   stretch the page past the viewport. */
body[data-page="fulfillment"] .page-content { overflow: visible; }
body[data-page="fulfillment"] .content-area { min-width: 0; }

/* ---- page shell ---------------------------------------------------------- */
.bs-ffm-page {
  display: flex;
  flex-direction: column;
  gap: var(--space-5, 20px);
  min-width: 0;
}
.bs-ffm-page > * {
  min-width: 0;
}

/* ---- section label (A / B headers) --------------------------------------- */
.bs-ffm-section-label {
  display: flex;
  align-items: center;
  padding: var(--space-2, 8px) 0 0;
}

/* ---- dual-row layout: platform cards STACKED full-width (1A then 1B; 2A then 2B).
   Dense matrices (5-6 metric cols) + 13-period heatmaps need the full width to stay
   readable without horizontal scroll — side-by-side at ~410px each forced overflow. */
.bs-ffm-dual-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--space-5, 20px);
  min-width: 0;
}

/* ---- section card wrapper ------------------------------------------------ */
.bs-ffm-section {
  padding: var(--space-5, 20px);
  min-width: 0;
}

.bs-ffm-platform-card {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

/* ---- card actions cluster (right-side of card header) -------------------- */
.bs-ffm-card-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2, 8px);
  flex-shrink: 0;
  flex-wrap: wrap;
}

/* ---- state / empty / error ----------------------------------------------- */
.bs-ffm-state {
  padding: var(--space-6, 24px);
  color: var(--text-tertiary, #94A3B8);
  text-align: center;
  font-size: 13px;
}

.bs-ffm-state--error {
  color: var(--color-danger);
  background: color-mix(in srgb, var(--color-danger) 10%, var(--bg-surface));
  border: 1px solid color-mix(in srgb, var(--color-danger) 30%, transparent);
  border-radius: var(--radius-md, 8px);
  padding: var(--space-4, 16px);
  text-align: left;
}

/* ---- SLA Overview — platform groups stacked vertically in a single card -- */
.bs-ffm-overview-columns {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.bs-ffm-overview-group {
  padding-top: var(--space-4, 16px);
}

.bs-ffm-overview-group + .bs-ffm-overview-group {
  margin-top: var(--space-5, 20px);
  padding-top: var(--space-5, 20px);
  border-top: 1px solid var(--border-subtle, rgba(0,0,0,0.05));
}

/* ---- card title with inline platform pill --------------------------------- */
.bs-ffm-card-title-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-2, 8px);
}

/* ---- platform sub-label (dot + name) ------------------------------------- */
.bs-ffm-platform-label {
  display: flex;
  align-items: center;
  gap: var(--space-2, 8px);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-secondary, #5F6775);
  margin-bottom: var(--space-3, 12px);
}

.bs-ffm-platform-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.bs-ffm-platform-dot--shopee {
  background: var(--color-shopee, #EE4D2D);
}
.bs-ffm-platform-dot--tiktok {
  /* Use a guaranteed non-transparent token — --text-primary adapts dark/light */
  background: var(--text-primary, #111318);
}

/* ---- metric cards grid --------------------------------------------------- */
.bs-ffm-cards-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--space-4, 16px);
}

.bs-ffm-card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: var(--space-4, 16px);
  border-radius: var(--radius-md, 8px);
  border: 1px solid var(--border-subtle, rgba(0,0,0,0.05));
  background: var(--bg-surface);
  transition: border-color 0.15s;
}

.bs-ffm-card--pass {
  border-left: 3px solid var(--color-success, #059669);
  background: color-mix(in srgb, var(--color-success, #059669) 6%, var(--bg-surface));
}

.bs-ffm-card--fail {
  border-left: 3px solid var(--color-danger, #DC2626);
  background: color-mix(in srgb, var(--color-danger, #DC2626) 6%, var(--bg-surface));
}

.bs-ffm-card__metric {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: inline-flex;
  align-items: center;
}

.bs-ffm-card__icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  margin-right: 6px;
  color: var(--color-action);
}
.bs-ffm-card__icon svg { width: 14px; height: 14px; }
.bs-ffm-card--fail .bs-ffm-card__icon { color: var(--color-brand); }

.bs-ffm-card__value {
  font-size: 20px;
  font-weight: 700;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.2;
}

.bs-ffm-card__target {
  font-size: 11px;
  color: var(--text-secondary);
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.bs-ffm-card__pass-rate {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  margin-top: 4px;
}

.bs-ffm-card__pass-rate--pass {
  color: var(--color-success, #059669);
  font-weight: 600;
}

.bs-ffm-card__pass-rate--fail {
  color: var(--color-danger, #DC2626);
  font-weight: 600;
}

/* ---- capture table wrapper (rounded bordered, self-contained scroll) ------ */
.bs-ffm-capture-wrap {
  width: 100%;
  max-height: calc(100vh - 240px);
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
}

/* ---- capture table ------------------------------------------------------- */
.bs-ffm-capture-table {
  border-collapse: collapse;
  width: 100%;
  font-size: 12px;
}

/* Sticky thead (the element, not individual th cells — per page-conventions.md) */
.bs-ffm-capture-table thead {
  position: sticky;
  top: 0;
  z-index: 2;
}

.bs-ffm-capture-table th {
  /* Gold-standard header tokens: --bg-inset, --text-secondary, bold/UPPERCASE/centered */
  background: var(--bg-inset);
  color: var(--text-secondary);
  font-size: var(--font-xs, 11px);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  text-align: center;
  padding: var(--space-1, 4px) var(--space-3, 12px);
  border-bottom: 1px solid var(--border-default);
  white-space: nowrap;
}

.bs-ffm-capture-table td {
  padding: var(--space-1, 4px) var(--space-3, 12px);
  border-bottom: 1px solid var(--border-subtle);
  white-space: nowrap;
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

/* Frozen first (Store) column — opaque bg to stay solid on horizontal scroll */
.bs-ffm-capture-table th:first-child,
.bs-ffm-capture-table td:first-child {
  position: sticky;
  left: 0;
  /* Opaque: mix with --bg-surface, not transparent (page-conventions.md) */
  background: color-mix(in srgb, var(--bg-inset) 100%, var(--bg-surface));
  z-index: 1;
  min-width: 120px;
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 500;
  text-align: left;
  font-family: var(--font-sans, system-ui, sans-serif);
  font-variant-numeric: normal;
}
.bs-ffm-capture-table td:first-child {
  background: color-mix(in srgb, var(--bg-surface) 100%, transparent);
}
.bs-ffm-capture-table th:first-child {
  z-index: 3;
  /* Header sticky corner: bg-inset to match the rest of the header row */
  background: var(--bg-inset);
  text-align: left;
}

/* Zebra rows */
.bs-ffm-row-zebra td {
  background: color-mix(in srgb, var(--text-primary) 3.5%, transparent);
}
.bs-ffm-row-zebra td:first-child {
  background: color-mix(in srgb, var(--text-primary) 3.5%, var(--bg-surface));
}

/* Hover */
.bs-ffm-capture-table tbody tr:hover td {
  background: var(--bg-hover);
}
.bs-ffm-capture-table tbody tr:hover td:first-child {
  background: var(--bg-hover);
}

/* Store column in body: non-mono, left-aligned */
.bs-ffm-td-store {
  text-align: left;
  font-weight: 500;
  font-family: var(--font-sans, system-ui, sans-serif);
  font-variant-numeric: normal;
}

/* Metric + numeric columns right-aligned */
.bs-ffm-th-metric {
  text-align: right;
}

/* Stacked capture header: metric name + Target pill + data range (20260609) */
.bs-ffm-cap-th-name {
  display: block;
}
.bs-ffm-cap-th-target {
  display: block;
  font-size: 11px;
  font-weight: 400;
  color: var(--text-secondary);
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.bs-ffm-cap-th-range {
  display: block;
  font-size: 10px;
  font-weight: 400;
  color: var(--text-tertiary);
  font-style: italic;
  white-space: nowrap;
}

.bs-ffm-td-right {
  text-align: right;
}

/* ---- pass/fail cell tints ------------------------------------------------ */
.bs-ffm-cell-pass {
  color: var(--color-success);
  font-weight: 600;
  background: color-mix(in srgb, var(--color-success) 10%, transparent);
}

.bs-ffm-cell-fail {
  color: var(--color-danger);
  font-weight: 600;
  background: color-mix(in srgb, var(--color-danger) 10%, transparent);
}

.bs-ffm-cell-neutral {
  color: var(--text-secondary);
}

/* ---- capture value text pass/fail coloring (FFM-4) ----------------------- */
/* Applied to the inner <span> wrapping the numeric value in capture cells.   */
/* Inherits font-weight from the parent .bs-ffm-cell-pass/fail td.            */
.bs-ffm-cell-value--pass {
  color: var(--color-success);
}

.bs-ffm-cell-value--fail {
  color: var(--color-danger);
}

/* ---- violation cells (capture + heatmap) --------------------------------- */
.bs-ffm-vcell--none {
  color: var(--text-tertiary);
}
.bs-ffm-vcell--high {
  color: var(--color-danger);
  font-weight: 600;
  background: color-mix(in srgb, var(--color-danger) 12%, transparent);
}

/* ---- violation sub-row (heatmap TikTok, S2) ------------------------------ */
.bs-ffm-viol-row td {
  padding: var(--space-1, 4px) var(--space-2, 8px);
  border-bottom: 1px solid var(--border-subtle);
  font-size: 10px;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  text-align: center;
  background: var(--bg-inset);
}
.bs-ffm-viol-label {
  text-align: left;
  font-style: italic;
  color: var(--text-tertiary);
  font-family: var(--font-sans, system-ui, sans-serif);
  font-variant-numeric: normal;
}

/* ---- capture table legend ------------------------------------------------ */
.bs-ffm-capture-legend {
  display: flex;
  align-items: center;
  gap: var(--space-4, 16px);
  margin-bottom: var(--space-3, 12px);
  flex-wrap: wrap;
}

/* ---- metric selector pills strip ----------------------------------------- */
.bs-ffm-metric-pills {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2, 8px);
  margin-bottom: var(--space-3, 12px);
}

/* ---- heatmap ------------------------------------------------------------- */

.bs-ffm-heatmap-meta {
  display: flex;
  align-items: center;
  gap: var(--space-4, 16px);
  flex-wrap: wrap;
  margin-bottom: var(--space-2, 8px);
}

.bs-ffm-heatmap-target {
  font-size: 12px;
  color: var(--text-secondary);
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.bs-ffm-heatmap-range {
  font-size: 11px;
  color: var(--text-tertiary);
  font-style: italic;
}

/* Caption centered above heatmap grid (FFM-5) */
.bs-ffm-heatmap-range--centered {
  display: block;
  text-align: center;
  width: 100%;
}

.bs-ffm-heatmap-legend {
  display: flex;
  align-items: center;
  gap: var(--space-4, 16px);
  margin-bottom: var(--space-3, 12px);
  flex-wrap: wrap;
}

/* Heatmap table container (rounded bordered, self-contained scroll) */
.bs-ffm-heatmap-wrap {
  width: 100%;
  max-height: calc(100vh - 240px);
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
}

.bs-ffm-heatmap-table {
  border-collapse: collapse;
  font-size: 11px;
  width: 100%;
}

/* Sticky thead element (not th cells) */
.bs-ffm-heatmap-table thead {
  position: sticky;
  top: 0;
  z-index: 2;
}

.bs-ffm-heatmap-table th {
  padding: var(--space-1, 4px) var(--space-2, 8px);
  border: 1px solid var(--border-subtle);
  white-space: nowrap;
  text-align: center;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  /* Gold-standard: --bg-inset + --text-secondary */
  background: var(--bg-inset);
  color: var(--text-secondary);
}

.bs-ffm-heatmap-table td {
  padding: var(--space-1, 4px) var(--space-2, 8px);
  border: 1px solid var(--border-subtle);
  white-space: nowrap;
  text-align: center;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

/* Frozen store column in heatmap — opaque bg per page-conventions.md */
.bs-ffm-heatmap-table th:first-child,
.bs-ffm-heatmap-table td:first-child {
  position: sticky;
  left: 0;
  z-index: 1;
  text-align: left;
  min-width: 110px;
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 500;
  color: var(--text-primary);
}
.bs-ffm-heatmap-table th:first-child {
  z-index: 3;
  background: var(--bg-inset);
}
.bs-ffm-heatmap-table td:first-child {
  background: var(--bg-surface);
  font-family: var(--font-sans, system-ui, sans-serif);
  font-variant-numeric: normal;
}
/* Zebra: frozen store column must use opaque mix, not transparent */
.bs-ffm-row-zebra .bs-ffm-heatmap-td-store {
  background: color-mix(in srgb, var(--text-primary) 3.5%, var(--bg-surface));
}

/* Store column class helpers */
.bs-ffm-heatmap-th-store {
  /* Handled by th:first-child above */
}
.bs-ffm-heatmap-td-store {
  /* Handled by td:first-child above */
}

/* Trend sparkline cell */
.bs-ffm-heatmap-spark {
  text-align: center;
  background: var(--bg-surface);
  vertical-align: middle;
}

/* Heatmap cell pass/fail states — uniform token-based pair (FFM-6) */
.bs-ffm-heatmap-cell--pass {
  background: color-mix(in srgb, var(--color-success) 10%, transparent);
  color: var(--color-success);
  font-weight: 600;
}

.bs-ffm-heatmap-cell--fail {
  background: color-mix(in srgb, var(--color-danger) 10%, transparent);
  color: var(--color-danger);
  font-weight: 600;
}

.bs-ffm-heatmap-cell--empty {
  color: var(--text-tertiary);
}

/* Scoped hover pill for heatmap cells (canonical [data-tooltip]::after pattern) */
.bs-ffm-heatmap-table td[data-tooltip] { position: relative; }
.bs-ffm-heatmap-table td[data-tooltip]:hover::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(8, 10, 15, 0.95);
  color: #FFFFFF;
  padding: 4px 8px;
  border-radius: var(--radius-sm, 6px);
  font-size: 11px;
  font-family: var(--font-sans, system-ui, sans-serif);
  font-variant-numeric: normal;
  letter-spacing: 0;
  text-transform: none;
  white-space: nowrap;
  z-index: 50;
  pointer-events: none;
  box-shadow: var(--shadow-md, 0 4px 12px rgba(0,0,0,0.15));
}

/* Anti-jitter overlay on metric change (stale table stays visible) */
.bs-ffm-heatmap-wrap--loading {
  opacity: 0.5;
  pointer-events: none;
  transition: opacity 0.15s;
}

/* ---- legend chips -------------------------------------------------------- */
.bs-ffm-legend__item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-secondary);
}

.bs-ffm-legend__swatch {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 2px;
  flex-shrink: 0;
}

.bs-ffm-legend__swatch--pass {
  background: color-mix(in srgb, var(--color-success, #059669) 60%, transparent);
}

.bs-ffm-legend__swatch--fail {
  background: color-mix(in srgb, var(--color-danger, #DC2626) 50%, transparent);
}

/* ---- export dropdown ----------------------------------------------------- */
.pnl-export-menu {
  position: relative;
  display: inline-flex;
}

.pnl-export-menu__caret {
  margin-left: 4px;
  font-size: 11px;
}

.pnl-export-menu__dropdown {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 30;
  background: var(--bg-elevated, #FFFFFF);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md, 8px);
  box-shadow: var(--shadow-lg, 0 8px 24px rgba(0,0,0,0.10));
  min-width: 140px;
  overflow: hidden;
}

.pnl-export-menu__item {
  display: block;
  width: 100%;
  padding: 9px 14px;
  text-align: left;
  background: none;
  border: none;
  font-size: 13px;
  color: var(--text-primary);
  cursor: pointer;
  transition: background 0.12s;
}

.pnl-export-menu__item:hover {
  background: var(--bg-hover);
}

/* ---- responsive ---------------------------------------------------------- */
@media (max-width: 1100px) {
  .bs-ffm-dual-row {
    grid-template-columns: 1fr;
  }
  .bs-ffm-cards-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (max-width: 900px) {
  .bs-ffm-cards-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 560px) {
  .bs-ffm-cards-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .bs-ffm-card__value {
    font-size: 18px;
  }
}
