/* ==========================================================================
   5. LINKS & ANALYTICS VIEW: TOOLBAR, KPI STRIP, BREAKDOWNS, FLAT LINKS TABLE
   Control heights follow --ctl-h (32px desktop, 44px touch). Text on dark
   surfaces uses --text-muted or brighter so small labels pass WCAG AA.
   ========================================================================== */
.analytics-view { display: none; flex-direction: column; gap: 10px; }

/* Toolbar ------------------------------------------------------------------ */
.an-toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.an-toolbar .an-sel,
.an-bd-pick .an-sel {
    height: var(--ctl-h);
    min-height: var(--ctl-h);
    padding: 0 28px 0 10px;
    font-size: 13px;
    border-radius: var(--radius-sm);
}
.an-search {
    height: var(--ctl-h);
    min-height: var(--ctl-h);
    padding: 0 10px;
    min-width: 160px;
    flex: 1;
    border-radius: var(--radius-sm);
}
.an-search::-webkit-search-cancel-button { filter: grayscale(1) brightness(0.8); }
.an-range {
    display: inline-flex;
    gap: 3px;
    /* Track is 6px taller than a control so the pill keeps ctl-h minus 2px of tap area
       (42px on touch, 30px on desktop). */
    height: calc(var(--ctl-h) + 6px);
    padding: 3px;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--surface-2);
}
.an-range button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 40px;
    padding: 0 10px;
    border: 0;
    border-radius: 9px;
    background: transparent;
    color: var(--text-muted);
    font-size: 12.5px;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    cursor: pointer;
}
.an-range button:hover { color: var(--text-main); background: rgba(96, 165, 250, 0.06); }
.an-range button[aria-pressed="true"] {
    color: #f8fafc;
    background: var(--surface-3);
    box-shadow: inset 0 0 0 1px rgba(96, 165, 250, 0.6), 0 2px 10px rgba(0, 0, 0, 0.35), 0 0 14px rgba(96, 165, 250, 0.14);
}
.an-range button:focus-visible,
.an-icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.an-icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: var(--ctl-h);
    height: var(--ctl-h);
    flex-shrink: 0;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface-2);
    color: var(--text-main);
    cursor: pointer;
}
.an-icon-btn:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.an-icon-btn:disabled { color: var(--text-muted); opacity: 0.55; cursor: default; }
.an-refresh[aria-busy="true"] svg { opacity: 0.5; }

/* KPI strip ---------------------------------------------------------------- */
.an-kpis {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 1px;
    background: var(--border);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
}
.an-kpi { display: flex; flex-direction: column; gap: 2px; padding: 8px 12px; background: var(--surface-modal); min-width: 0; }
.an-kpi-lbl { font-size: 11.5px; font-weight: 600; color: var(--text-muted); }
.an-kpi-v {
    font-family: 'Outfit', 'Outfit Fallback', sans-serif;
    font-size: 20px;
    font-weight: 700;
    line-height: 1.1;
    font-variant-numeric: tabular-nums;
    color: var(--text-main);
    white-space: nowrap;
}
.an-kpi-d { font-size: 11.5px; color: var(--text-muted); font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Breakdowns --------------------------------------------------------------- */
.an-bd-pick { display: none; }
.an-bd { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.an-bd[data-scope="multi"] [data-bd="country"],
.an-bd[data-scope="single"] [data-bd="brand"] { display: none; }
.an-bd-card {
    padding: 8px 12px;
    background: var(--surface-modal);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    min-width: 0;
}
.an-bd-h {
    margin: 0 0 4px;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--text-muted);
}
.an-mini { width: 100%; border-collapse: collapse; font-size: 12.5px; font-variant-numeric: tabular-nums; }
.an-mini td { height: 26px; border-top: 1px solid var(--border); color: var(--text-main); white-space: nowrap; }
.an-mini td:first-child { overflow: hidden; text-overflow: ellipsis; max-width: 0; width: 100%; }
.an-mini td.r { text-align: right; padding-left: 12px; color: #cbd5e1; }
.an-mini td.an-mini-empty { color: var(--text-muted); max-width: none; }
.an-mini td.an-mini-err { color: var(--danger-text); max-width: none; }
.an-bar { display: block; height: 3px; margin-top: 2px; border-radius: 2px; background: rgba(148, 163, 184, 0.16); overflow: hidden; }
.an-bar > i { display: block; height: 100%; background: rgba(96, 165, 250, 0.6); }
.an-mini .an-dev { display: inline-block; line-height: 1.15; min-width: 72px; }

/* Shared dots and pills (also used by the link detail and X composer) ------- */
.brand-mini-dot,
.an-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
    display: inline-block;
}
.an-mini .brand-mini-dot { margin-right: 6px; vertical-align: 0; }
.brand-ctr-pill {
    display: inline-block;
    font-family: monospace;
    font-size: 11px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    padding: 2px 6px;
    border-radius: 9999px;
    background: rgba(16, 185, 129, 0.10);
    color: #6ee7b7;
    border: 1px solid rgba(16, 185, 129, 0.30);
    white-space: nowrap;
}

/* Flat links table ----------------------------------------------------------- */
.an-table-card {
    background: var(--surface-modal);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
}
.an-tbl { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; font-size: 13px; }
.an-tbl th {
    height: 28px;
    padding: 0 8px;
    text-align: left;
    font-size: 11.5px;
    font-weight: 600;
    color: var(--text-muted);
    border-bottom: 1px solid rgba(148, 163, 184, 0.24);
    white-space: nowrap;
}
.an-tbl td { height: 32px; padding: 0 8px; border-bottom: 1px solid var(--border); white-space: nowrap; color: var(--text-main); }
.an-tbl .an-num { text-align: right; }
.an-tbl td.an-num { width: 72px; }
.an-tbl td.an-ctr { color: #cbd5e1; }
.an-tbl .an-c-dot { width: 20px; padding-right: 0; }
.an-tbl .an-date { width: 84px; color: var(--text-muted); }
.an-tbl td.an-type { width: 80px; }
.an-link { max-width: 0; width: 100%; overflow: hidden; text-overflow: ellipsis; }
.an-slug { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; color: var(--text-main); margin-right: 8px; }
.an-ttl { font-size: 12.5px; color: var(--text-muted); }
.an-sub { font-size: 11px; color: var(--text-muted); margin-left: 3px; }
.an-tag {
    display: inline-block;
    padding: 1px 6px;
    font-size: 11px;
    font-weight: 600;
    line-height: 1.4;
    color: #cbd5e1;
    border: 1px solid rgba(148, 163, 184, 0.32);
    border-radius: 4px;
}
.an-tag-quick { border-color: rgba(56, 189, 248, 0.45); }
.an-tag-kalshi { border-color: rgba(16, 185, 129, 0.45); }
.an-tag-generic { border-color: rgba(245, 158, 11, 0.45); }
.an-tag-off { color: var(--danger-text); border-color: rgba(244, 114, 128, 0.40); margin-left: 6px; }
.an-row.is-off .an-slug { color: var(--text-muted); }
.link-compact-row { cursor: pointer; }
.link-compact-row:hover td { background: rgba(96, 165, 250, 0.06); }
.link-compact-row:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.an-tbl-empty { text-align: center; color: var(--text-muted); height: 64px !important; white-space: normal !important; }
.an-tbl-empty.is-error { color: var(--danger-text); }
.an-foot { display: flex; align-items: center; gap: 8px; padding: 6px 8px; font-size: 12.5px; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.an-sp { flex: 1; }

/* Reporting API reference ----------------------------------------------------- */
.an-api { padding: 8px 12px; background: var(--surface-modal); border: 1px solid var(--border); border-radius: var(--radius-sm); }
.an-api > summary { cursor: pointer; min-height: var(--ctl-h); display: flex; align-items: center; font-size: 13px; font-weight: 600; color: var(--text-muted); }
.an-api > summary:hover { color: var(--text-main); }
.an-api-note { font-size: 12px; color: var(--text-muted); margin-top: 4px; }
.an-api-note code { color: var(--text-main); }
.api-endpoint-box {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-top: 8px;
    background: var(--bg-deep);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 8px;
}
.api-endpoint-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    font-family: monospace;
    font-size: 12px;
    color: #cbd5e1;
    background: var(--surface-2);
    padding: 4px 4px 4px 10px;
    border-radius: 6px;
    border: 1px solid rgba(148, 163, 184, 0.12);
    word-break: break-all;
}

/* Phones and narrow windows ------------------------------------------------- */
@media (max-width: 768px) {
    .analytics-view { gap: 8px; }
    .an-toolbar .an-sel { flex: 1 1 0; min-width: 0; text-overflow: ellipsis; }
    .an-refresh { order: 2; }
    .an-range { order: 3; width: 100%; }
    .an-range button { flex: 1; min-width: 0; padding: 0 4px; }
    .an-search { order: 4; flex-basis: 100%; }
    .an-kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    .an-kpi:last-child { grid-column: span 2; }
    .an-kpi { padding: 8px 10px; }
    .an-bd-pick { display: flex; }
    .an-bd-pick .an-sel { flex: 1; min-width: 0; }
    .an-bd { grid-template-columns: 1fr; }
    .an-bd .an-bd-card { display: none; }
    .an-bd[data-scope="multi"][data-active="brand"] [data-bd="brand"],
    .an-bd[data-scope="single"][data-active="country"] [data-bd="country"],
    .an-bd[data-active="device"] [data-bd="device"],
    .an-bd[data-active="source"] [data-bd="source"] { display: block; }
    .an-bd .an-bd-h { display: none; }
    .an-mini td { height: 32px; }
    .an-hide-s { display: none; }
    .an-tbl td { height: 44px; padding: 4px 6px; }
    .an-tbl th { padding: 0 6px; }
    .an-tbl td.an-num { width: 52px; }
    .an-tbl td.an-ctr { width: 60px; }
    .an-link { white-space: normal; line-height: 1.25; }
    .an-slug { display: block; margin-right: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .an-ttl { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .an-tag-off { margin-left: 0; }
    .an-foot { padding: 4px 6px 4px 10px; }
}

@media (prefers-reduced-motion: no-preference) {
    .an-range button,
    .an-icon-btn,
    .link-compact-row td { transition: background-color 0.12s ease, color 0.12s ease, border-color 0.12s ease; }
}
