/* ==========================================================================
   FINICHE — MEASURED VS MODELLED
   One visual grammar for data honesty, built on the styles.css tokens:
     observed facts   solid type, solid borders (price, BSR, reviews, timestamps)
     estimates        lavender "model ink", dashed frame, an "Estimate" legend tag
                      sitting on the frame, and a ≈ before every modelled number
     unknown          a plain dash, no styling at all
   Dashed lines already mean "not measured" elsewhere (hm-untracked), so they are reused here.
   CSP-safe: no inline styles; the popover is positioned through CSSOM only.
   ========================================================================== */

:root,
html[data-theme="dark"],
html.dark {
  --est-ink: #b4b2e6;
  --est-line: rgba(180, 178, 230, 0.42);
  --est-wash: rgba(180, 178, 230, 0.05);
  --est-wash-strong: rgba(180, 178, 230, 0.14);
}

html[data-theme="light"],
html.light {
  --est-ink: #5b57a8;
  --est-line: rgba(91, 87, 168, 0.45);
  --est-wash: rgba(91, 87, 168, 0.04);
  --est-wash-strong: rgba(91, 87, 168, 0.12);
}

/* ---------------------------------------------------------------------------
   The Estimate label (button where it can take focus, span inside clickable cards)
   --------------------------------------------------------------------------- */
.est-tag {
  appearance: none;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin: 0;
  padding: 1px 6px 1px 5px;
  font-family: var(--font-mono);
  font-size: 9.5px;
  font-weight: 700;
  line-height: 1.5;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  white-space: nowrap;
  color: var(--est-ink);
  background: var(--bg-surface);
  border: 1px dashed var(--est-line);
  border-radius: var(--radius-pill);
  cursor: help;
  vertical-align: middle;
  transition: background var(--transition), border-color var(--transition);
}

/* A small dashed ring: the "not measured" mark, echoed at icon size. */
.est-tag::before {
  content: '';
  width: 7px;
  height: 7px;
  border: 1px dashed currentColor;
  border-radius: 50%;
  flex: none;
}

.est-tag:hover {
  background: var(--est-wash-strong);
  border-style: solid;
}

.est-tag:focus-visible {
  outline: 2px solid var(--border-focus);
  outline-offset: 2px;
  border-style: solid;
}

/* Header cells: label text, then the tag. */
.est-col {
  white-space: nowrap;
}

.est-col .est-tag {
  margin-inline-start: 5px;
}

/* ---------------------------------------------------------------------------
   The estimate frame and modelled values
   --------------------------------------------------------------------------- */
.est-zone {
  position: relative;
  padding: 13px 9px 7px;
  margin-top: 4px;
  border: 1px dashed var(--est-line);
  border-radius: var(--radius-sm);
  background: var(--est-wash);
}

/* The legend tag sits on the top edge of the frame, like a fieldset legend. */
.est-zone > .est-tag:first-child {
  position: absolute;
  top: -9px;
  inset-inline-start: 8px;
  background: var(--bg-elevated);
}

.card > .est-zone > .est-tag:first-child,
.arb-card .est-zone > .est-tag:first-child {
  background: var(--bg-surface);
}

.est-zone--inline {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  padding: 2px 4px 2px 8px;
  margin-top: 0;
}

.est-zone--inline > .est-tag:first-child,
.est-zone--inline > .est-tag {
  position: static;
}

.badge.est-badge {
  color: var(--est-ink);
  background: var(--est-wash);
  border: 1px dashed var(--est-line);
}

.est-val::before {
  content: '\2248\2009';
  color: var(--est-ink);
  font-weight: 500;
}

.metric-val.est-val {
  font-variant-numeric: tabular-nums;
}

.arb-card .est-zone {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 12px;
  margin-top: 10px;
  font-size: 12.5px;
  color: var(--text-secondary);
}

.p-metrics--obs,
.p-metrics--est {
  grid-template-columns: repeat(auto-fit, minmax(84px, 1fr));
}

.metric-val.is-hot {
  color: var(--emerald-text);
}

/* ---------------------------------------------------------------------------
   Freshness: "Observed 2 hours ago" and the stale flag (> 3 days)
   --------------------------------------------------------------------------- */
.p-top {
  flex-wrap: wrap;
  row-gap: 6px;
}

.obs-line {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.obs {
  font-size: 11px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.obs--unknown {
  font-style: italic;
}

.badge.stale-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: var(--amber-bg);
  color: var(--amber-text);
  border-color: var(--amber-border);
}

.badge.stale-badge::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}

.row-product .obs-line {
  margin-top: 2px;
}

.drawer-obs-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

/* ---------------------------------------------------------------------------
   Shared explanation popover
   --------------------------------------------------------------------------- */
.est-popover {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1200;
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-width: min(320px, calc(100vw - 16px));
  padding: 10px 12px 10px 13px;
  font-size: 12.5px;
  line-height: 1.45;
  color: var(--text-primary);
  background: var(--bg-elevated);
  border: 1px solid var(--est-line);
  border-inline-start: 3px solid var(--est-ink);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-hover);
  pointer-events: auto;
  animation: est-pop-in 140ms var(--ease-out);
}

.est-popover[hidden] {
  display: none;
}

.est-popover-title {
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--est-ink);
}

.est-popover-method {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
}

@keyframes est-pop-in {
  from {
    opacity: 0;
    transform: translateY(3px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

/* ---------------------------------------------------------------------------
   Data scope strip (Product Spy header) and arbitrage scope line
   --------------------------------------------------------------------------- */
.scope-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 6px 14px;
  padding: 10px 14px;
  margin-bottom: 12px;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  background: var(--bg-surface);
  box-shadow: var(--inner-glow);
}

.scope-title {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.scope-body {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 12px;
  font-size: 12.5px;
  color: var(--text-secondary);
}

.scope-name {
  font-size: 13.5px;
  font-weight: 700;
  color: var(--text-primary);
}

.scope-markets {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 10px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.scope-count {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  color: var(--text-primary);
}

.scope-item::before {
  content: '\00B7';
  margin-inline-end: 10px;
  color: var(--text-muted);
}

.scope-empty {
  font-style: italic;
  color: var(--text-muted);
}

.arb-scope {
  margin: 8px 0 0;
  padding-inline-start: 8px;
  font-size: 12px;
  color: var(--text-secondary);
  border-inline-start: 2px solid var(--border-strong);
}

.arb-scope:empty {
  display: none;
}

/* ---------------------------------------------------------------------------
   Small screens: the strip stacks, the legend tag keeps its place on the frame
   --------------------------------------------------------------------------- */
@media (max-width: 640px) {
  .scope-bar {
    flex-direction: column;
    gap: 6px;
  }

  .scope-item::before {
    content: none;
  }

  .scope-body {
    flex-direction: column;
    gap: 4px;
  }

  /* Two-column KPI grid on phones: the tag drops under the label instead of squeezing it. */
  .kpi-header {
    flex-wrap: wrap;
    row-gap: 4px;
  }
}
