/* ══════════════════════════════════════════════════════
   Analysis page: page-specific styles only.
   Design tokens are provided by tokens.css.
   ══════════════════════════════════════════════════════ */

* { box-sizing: border-box; margin: 0; padding: 0; }

/* ── Global focus ring (WCAG 2.4.7) ─────────────── */
:focus-visible {
  outline: 2px solid var(--teal-light);
  outline-offset: 3px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}


html, body {
  min-height: 100%;
  background:
    radial-gradient(circle at top left, rgba(0, 151, 163, 0.16), transparent 30%),
    radial-gradient(circle at bottom right, rgba(110, 231, 224, 0.08), transparent 40%),
    linear-gradient(135deg, var(--bg), var(--bg-deep));
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.6;
  overflow-x: hidden;
}

.shell {
  width: min(1200px, calc(100vw - 32px));
  margin: 0 auto;
  padding: var(--sp-6) 0 var(--sp-16);
}

/* ── Topbar ── */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--sp-4);
  margin-bottom: var(--sp-8);
  padding-bottom: var(--sp-4);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}

.topbar-left { display: flex; align-items: center; gap: var(--sp-4); }

.brand-lockup { display: flex; align-items: center; gap: var(--sp-3); text-decoration: none; }
.brand-logo { width: 36px; height: 36px; border-radius: var(--r-md); }
.brand-name {
  font-family: var(--font-heading);
  font-size: 1.2rem;
  letter-spacing: -0.03em;
  color: var(--ink);
}

.run-id-badge {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-1);
  /* fine-tune: 6px is not on grid but kept for optical balance */
  gap: 6px;
  padding: var(--sp-1) 10px;
  background: rgba(110, 231, 224, 0.08);
  border: 1px solid rgba(110, 231, 224, 0.15);
  border-radius: var(--r-md);
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--gold);
  letter-spacing: 0.04em;
  font-family: var(--font-mono);
}

.topbar-right { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.rerun-error {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 6px;
  background: rgba(220, 38, 38, 0.12);
  color: #fca5a5;
  font-size: 0.85rem;
  font-weight: 500;
  max-width: 320px;
  white-space: normal;
  line-height: 1.35;
}
.rerun-error[hidden] { display: none; }

button, .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: var(--sp-2) 18px;
  border: none;
  border-radius: var(--r-md);
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
  text-decoration: none;
  font-family: inherit;
  color: inherit;
}
button:hover { opacity: 0.85; transform: translateY(-1px); }
button:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

.btn-secondary {
  background: rgba(141, 162, 181, 0.12);
  color: var(--ink);
  border: 1px solid var(--line);
}
.btn-sm { padding: 5px 12px; font-size: 0.8rem; }

/* ── Loading / Error states ── */
.state-loading, .state-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--sp-4);
  padding: 80px var(--sp-6);
  text-align: center;
}
.state-loading[hidden], .state-error[hidden], #dashboard[hidden] { display: none; }
.state-loading { color: var(--muted); }
.state-loading-progress {
  font-family: var(--font-mono, ui-monospace, monospace);
  font-size: 0.95rem;
  color: var(--ink);
  margin-top: calc(var(--sp-2) * -1);
}
.state-loading-progress[hidden] { display: none; }
.state-error { color: var(--danger); }
.state-error h3 {
  font-family: var(--font-heading);
  font-size: 1.3rem;
  color: var(--ink);
}
.state-error p { font-size: 0.9rem; color: var(--muted); max-width: 400px; }

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--line);
  border-top-color: var(--gold);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── KPIs bar ── */
.kpis {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: var(--sp-3);
  margin-bottom: 28px;
}

.kpi-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  padding: 18px var(--sp-5);
  backdrop-filter: blur(12px);
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.kpi-label {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
}
.kpi-value {
  font-size: 1.45rem;
  font-weight: 700;
  color: var(--ink);
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
}
.kpi-sub {
  font-size: 0.75rem;
  color: var(--muted);
}

/* ── Tabs ── */
.tabs-bar {
  display: flex;
  gap: var(--sp-1);
  margin-bottom: var(--sp-6);
  background: var(--panel-strong);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: var(--sp-1);
  width: fit-content;
}

.tab {
  padding: var(--sp-2) var(--sp-5);
  border-radius: 7px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  background: transparent;
  border: none;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.tab:hover { color: var(--ink); transform: none; opacity: 1; }
.tab.active {
  background: rgba(110, 231, 224, 0.12);
  color: var(--gold);
}

.tab-content { display: none; }
.tab-content.active { display: block; }

/* ── Panel ── */
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  padding: 28px;
  backdrop-filter: blur(12px);
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: var(--sp-4);
  margin-bottom: var(--sp-6);
  flex-wrap: wrap;
}

.eyebrow {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--gold);
  margin-bottom: var(--sp-1);
}

.panel h2 {
  font-family: var(--font-heading);
  font-size: 1.4rem;
  letter-spacing: -0.03em;
  color: var(--ink);
}

/* ── Charts grid ── */
.charts-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--sp-4);
}

.chart-card {
  background: var(--panel-strong);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  padding: var(--sp-5);
}
.chart-title {
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: var(--sp-4);
}
/* Scatter brand filter buttons */
.scatter-filter-btn {
  padding: 3px 10px;
  font-size: 11px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: all 0.15s;
}
.scatter-filter-btn:hover { border-color: var(--teal-light); color: var(--teal-light); }
.scatter-filter-btn.active { background: var(--teal); color: var(--bg); border-color: var(--teal); }

.chart-placeholder {
  border-radius: var(--r-md);
}

/* ── Matrix tab ── */
.matrix-layout {
  display: grid;
  grid-template-columns: 1fr 340px;
  gap: var(--sp-4);
  align-items: start;
}

.matrix-card {
  background: var(--panel-strong);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  padding: var(--sp-5);
}

.matrix-insights {
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
}

.insight-item {
  background: var(--panel-strong);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  padding: var(--sp-4) 18px;
}
.insight-label {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--gold);
  margin-bottom: 6px;
}
.insight-text {
  font-size: 0.85rem;
  color: var(--muted);
  line-height: 1.5;
}

/* ── Tables tab ── */
.tables-layout {
  display: flex;
  flex-direction: column;
  gap: var(--sp-6);
}

.table-search {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: var(--sp-4);
}

.search-input {
  background: rgba(14, 20, 31, 0.8);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  color: var(--ink);
  font-family: inherit;
  font-size: 0.875rem;
  padding: var(--sp-2) 14px;
  outline: none;
  transition: border-color 0.15s;
  width: 280px;
}
.search-input:focus { border-color: var(--teal); }
.search-input::placeholder { color: var(--muted); opacity: 0.6; }

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}
.data-table th {
  text-align: left;
  padding: 10px 14px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
  transition: color 0.15s;
}
.data-table th:hover { color: var(--ink); }
.data-table th.sort-asc::after { content: " ↑"; color: var(--gold); }
.data-table th.sort-desc::after { content: " ↓"; color: var(--gold); }
.data-table td {
  padding: 10px 14px;
  border-bottom: 1px solid rgba(141, 162, 181, 0.06);
  color: var(--ink);
  vertical-align: middle;
}
.data-table tr:last-child td { border-bottom: none; }
.data-table tr:hover td { background: rgba(110, 231, 224, 0.03); }
.data-table td.num {
  font-variant-numeric: tabular-nums;
  color: var(--gold);
  font-weight: 600;
}
.data-table td.score {
  font-weight: 700;
  color: var(--warm);
}
.data-table td a {
  color: var(--teal);
  text-decoration: none;
  font-size: 0.78rem;
}
.data-table td a:hover { text-decoration: underline; }

.empty-state {
  text-align: center;
  padding: var(--sp-10);
  color: var(--muted);
  font-size: 0.9rem;
}

.pagination-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 12px 0;
}
.pagination-info {
  font-size: 0.82rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.skeleton {
  background: linear-gradient(90deg, var(--line) 25%, rgba(141, 162, 181, 0.08) 50%, var(--line) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
  border-radius: var(--r-md);
}
@keyframes shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

/* ── Score badges ── */
.score-badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: var(--r-sm);
  font-weight: 700;
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
}
.score-high { background: var(--status-success-bg); color: var(--gold); }
.score-mid  { background: var(--status-warning-bg); color: var(--warm); }
.score-low  { background: var(--status-error-bg);   color: var(--danger); }

/* ── Filters panel ── */
#filters-toggle[hidden] { display: none; }
.filters-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: var(--r-full);
  background: var(--gold);
  color: #0b1220;
  font-size: 0.7rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.filters-count[hidden] { display: none; }

.filters-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  padding: var(--sp-5);
  margin-bottom: var(--sp-5);
  backdrop-filter: blur(12px);
  animation: filters-slide-in 0.18s ease-out;
}
.filters-panel[hidden] { display: none; }
@keyframes filters-slide-in {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

.filters-grid {
  display: grid;
  grid-template-columns: minmax(220px, 280px) minmax(220px, 1fr);
  gap: var(--sp-4) var(--sp-6);
  align-items: start;
}
.filter-group-full { grid-column: 1 / -1; }
.filter-scroll-list {
  max-height: 160px;
  overflow-y: auto;
  padding-right: var(--sp-2);
}

.filter-group { display: flex; flex-direction: column; gap: var(--sp-2); }

.filter-label {
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}

.filter-price-row { display: flex; align-items: center; gap: var(--sp-2); }

.filter-input {
  flex: 1;
  min-width: 0;
  padding: var(--sp-2) var(--sp-3);
  background: rgba(9, 14, 24, 0.6);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  color: var(--ink);
  font-size: 0.875rem;
  font-family: inherit;
  font-variant-numeric: tabular-nums;
}
.filter-input:focus { outline: 2px solid var(--teal-light); outline-offset: 1px; }
.filter-input::-webkit-outer-spin-button,
.filter-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.filter-input[type="number"] { -moz-appearance: textfield; }

.filter-sep { color: var(--muted); font-size: 0.875rem; }

.filter-conditions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2);
}

.filter-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: rgba(141, 162, 181, 0.08);
  border: 1px solid var(--line);
  border-radius: var(--r-full);
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--ink-soft);
  cursor: pointer;
  user-select: none;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.filter-chip:hover { background: rgba(141, 162, 181, 0.16); }
.filter-chip input { display: none; }
.filter-chip.active {
  background: rgba(110, 231, 224, 0.14);
  border-color: rgba(110, 231, 224, 0.4);
  color: var(--gold);
}
.filter-chip-count {
  font-size: 0.7rem;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}
.filter-chip.active .filter-chip-count { color: var(--gold); opacity: 0.75; }
.filter-chip.filter-chip--empty { opacity: 0.35; }
.filter-chip.filter-chip--hidden { display: none; }

.filter-search-input {
  width: 100%;
  padding: 5px 10px;
  font-size: 0.8rem;
  background: rgba(141, 162, 181, 0.06);
  border: 1px solid var(--line);
  border-radius: var(--r-full);
  color: var(--ink);
  outline: none;
  transition: border-color 0.15s;
}
.filter-search-input::placeholder { color: var(--muted); }
.filter-search-input:focus { border-color: rgba(110, 231, 224, 0.5); }

.filters-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--sp-3);
  margin-top: var(--sp-4);
  padding-top: var(--sp-4);
  border-top: 1px solid var(--line);
}

.filters-status {
  font-size: 0.8rem;
  color: var(--muted);
  margin-right: auto;
}

/* ── Filters backdrop (mobile drawer only) ── */
.filters-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(4, 8, 14, 0.62);
  backdrop-filter: blur(2px);
  z-index: 99;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s ease;
}
.filters-backdrop.is-open {
  opacity: 1;
  pointer-events: auto;
}

/* Mobile-only close button inside the filters drawer */
.filters-sheet-header {
  display: none;
}

/* ── Responsive ── */
@media (max-width: 1024px) {
  .kpis { grid-template-columns: repeat(3, 1fr); }
  .matrix-layout { grid-template-columns: 1fr; }
  .matrix-insights { flex-direction: row; flex-wrap: wrap; }
  .insight-item { flex: 1 1 200px; }
  .filters-grid { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
  /* ── Layout shell ── */
  .shell {
    width: min(1200px, calc(100vw - 20px));
    padding: var(--sp-4) 0 var(--sp-12);
  }

  /* ── Topbar: wrap logo on row 1, buttons on row 2 ── */
  .topbar {
    gap: var(--sp-2);
    margin-bottom: var(--sp-5);
    padding-bottom: var(--sp-3);
  }
  .topbar-left {
    flex-wrap: wrap;
    gap: var(--sp-2);
    min-width: 0;
    flex: 1 1 100%;
  }
  .brand-name { font-size: 1.05rem; }
  .brand-logo { width: 30px; height: 30px; }
  .run-id-badge {
    font-size: 0.7rem;
    padding: 3px 8px;
    max-width: 60vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .topbar-right {
    flex: 1 1 100%;
    justify-content: flex-start;
    gap: 6px;
  }
  .topbar-right .btn-sm {
    padding: 5px 10px;
    font-size: 0.72rem;
    flex: 0 1 auto;
  }
  .rerun-error {
    flex: 1 1 100%;
    max-width: none;
    font-size: 0.78rem;
  }

  /* ── KPIs: 2 cols, tighter ── */
  .kpis {
    grid-template-columns: repeat(2, 1fr);
    gap: var(--sp-2);
    margin-bottom: var(--sp-5);
  }
  .kpi-card {
    padding: var(--sp-3) var(--sp-4);
    gap: 4px;
  }
  .kpi-label { font-size: 0.62rem; letter-spacing: 0.08em; }
  .kpi-value { font-size: 1.15rem; }
  .kpi-value[data-kpi="kpi-top-category"] { font-size: 0.85rem !important; }
  .kpi-sub { font-size: 0.68rem; }

  /* ── Only the "charts" tab is reachable on mobile ── */
  .tabs-bar { display: none; }
  #tab-matrix,
  #tab-tables { display: none !important; }
  #tab-charts { display: block !important; }

  /* ── Charts: single column, tighter ── */
  .charts-grid {
    grid-template-columns: 1fr;
    gap: var(--sp-3);
  }
  .chart-card {
    padding: var(--sp-3) var(--sp-3) var(--sp-2);
    border-radius: var(--r-md);
  }
  .chart-title {
    font-size: 0.7rem;
    letter-spacing: 0.06em;
    margin-bottom: var(--sp-2);
  }

  /* ── Filters: bottom-sheet drawer ── */
  .filters-panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
    margin: 0;
    z-index: 100;
    max-height: 88vh;
    border-radius: var(--r-2xl) var(--r-2xl) 0 0;
    border-left: none;
    border-right: none;
    border-bottom: none;
    padding: var(--sp-4) var(--sp-4) 0;
    box-shadow: var(--shadow-lg);
    display: flex !important;
    flex-direction: column;
    overflow: hidden;
    transform: translateY(100%);
    transition: transform 0.24s ease;
    animation: none;
  }
  .filters-panel[hidden] {
    display: flex !important;
    pointer-events: none;
    transform: translateY(100%);
  }
  .filters-panel:not([hidden]) {
    pointer-events: auto;
    transform: translateY(0);
  }

  /* Drag-handle + title + close */
  .filters-sheet-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--sp-3);
    padding-bottom: var(--sp-3);
    margin: 0 0 var(--sp-3);
    border-bottom: 1px solid var(--line);
    position: relative;
  }
  .filters-sheet-header::before {
    content: "";
    position: absolute;
    top: -8px;
    left: 50%;
    transform: translateX(-50%);
    width: 36px;
    height: 4px;
    border-radius: var(--r-full);
    background: rgba(141, 162, 181, 0.35);
  }
  .filters-sheet-title {
    font-family: var(--font-heading);
    font-size: 1.05rem;
    color: var(--ink);
  }
  .filters-sheet-close {
    background: rgba(141, 162, 181, 0.12);
    border: 1px solid var(--line);
    width: 32px;
    height: 32px;
    padding: 0;
    font-size: 1.1rem;
    line-height: 1;
    border-radius: var(--r-full);
  }

  .filters-grid {
    grid-template-columns: 1fr;
    gap: var(--sp-4);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding-right: var(--sp-1);
    flex: 1 1 auto;
    min-height: 0;
  }
  .filter-scroll-list { max-height: 140px; }
  .filter-chip { padding: 8px 12px; font-size: 0.85rem; }

  .filters-actions {
    flex-wrap: wrap;
    justify-content: space-between;
    gap: var(--sp-2);
    margin: 0 calc(-1 * var(--sp-4));
    padding: var(--sp-3) var(--sp-4);
    border-top: 1px solid var(--line);
    background: var(--panel-strong);
    flex: 0 0 auto;
  }
  .filters-status {
    flex: 1 1 100%;
    margin: 0;
    text-align: center;
    order: -1;
  }
  .filters-actions .btn { flex: 1 1 auto; }

  /* Lock body scroll when drawer is open */
  body.filters-open {
    overflow: hidden;
    touch-action: none;
  }
}
