/* FERC Audit Explorer — periwinkle + Google+ "stream + threaded" (DESIGN.md §15) */

:root {
  /* Brand / surface — periwinkle-tinted neutrals */
  --bg: #f4f4fb;
  --surface: #ffffff;
  --surface-2: #ececf8;
  --border: #dedef1;
  --text: #1b1b2e;
  --text-muted: #63637e;

  /* Accent — periwinkle */
  --accent: #5b5bd6;
  --accent-hover: #4a4ac2;
  --accent-weak: #e7e7fb;
  --accent-line: #c7c7f0;

  /* Semantic — meaning, not brand */
  --status-finding: #b7791f;
  --status-rec: #2f73c4;
  --status-resolved: #2e7d52;

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, "Helvetica Neue", Arial, sans-serif;
  --font-serif: "Charter", "Source Serif 4", "Iowan Old Style", "Palatino", Georgia, "Times New Roman", serif;
  --font-mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;

  --shadow-rest: 0 1px 2px rgba(27, 27, 46, 0.06);
  --shadow-hover: 0 4px 18px rgba(91, 91, 214, 0.14);
  --radius: 12px;

  font-feature-settings: "tnum";
}

[data-theme="dark"] {
  --bg: #131320;
  --surface: #1d1d2c;
  --surface-2: #26263a;
  --border: #32324a;
  --text: #ececf6;
  --text-muted: #9d9dba;
  --accent: #9393f2;
  --accent-hover: #a6a6f6;
  --accent-weak: #2a2a48;
  --accent-line: #3c3c5e;
  --status-finding: #e0a85a;
  --status-rec: #6ba6e8;
  --status-resolved: #6fc795;
  --shadow-rest: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-hover: 0 6px 22px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); }
a:hover { color: var(--accent-hover); }

[hidden] { display: none !important; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  background: var(--accent);
  color: #fff;
  padding: 8px 14px;
  border-radius: 0 0 8px 0;
  z-index: 100;
}
.skip-link:focus { left: 0; }

/* ---------- Header ---------- */
.site-header {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: linear-gradient(180deg, var(--accent-weak), var(--surface));
  border-bottom: 1px solid var(--border);
  backdrop-filter: saturate(1.1);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: var(--accent-weak);
  border: 1px solid var(--accent-line);
  color: var(--accent);            /* the pylon (stroke: currentColor) is periwinkle */
  flex: none;
}
.brand-mark svg { width: 24px; height: 24px; display: block; }
.brand-text { display: flex; flex-direction: column; min-width: 0; }
.brand-text strong { font-family: var(--font-serif); font-size: 18px; letter-spacing: 0.01em; }
.brand-sub { color: var(--text-muted); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.icon-btn {
  width: 44px; height: 44px;
  display: grid; place-items: center;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  font-size: 20px;
  cursor: pointer;
  flex: none;
}
.icon-btn:hover { background: var(--surface-2); }

/* ---------- Collection tabs ---------- */
.tabs {
  display: flex;
  gap: 6px;
  max-width: 1180px;
  margin: 14px auto 0;
  padding: 0 16px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex: none;
  min-height: 44px;
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  background: var(--surface);
  color: var(--text-muted);
  border-radius: 10px 10px 0 0;
  font-family: var(--font-serif);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.tab:hover { background: var(--surface-2); color: var(--text); }
.tab[aria-selected="true"] {
  color: var(--accent);
  border-color: var(--accent-line);
  border-bottom-color: var(--accent);
  background: var(--accent-weak);
}
.tab-count {
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  background: var(--surface-2);
  border-radius: 999px;
  padding: 1px 8px;
  min-width: 20px;
  text-align: center;
}
.tab[aria-selected="true"] .tab-count { color: var(--accent-hover); background: var(--surface); }

/* ---------- KPI strip ---------- */
.kpi-strip {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  max-width: 1180px;
  margin: 16px auto 0;
  padding: 0 16px;
}
.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  box-shadow: var(--shadow-rest);
}
.kpi-num { display: block; font-family: var(--font-serif); font-size: 26px; font-weight: 700; line-height: 1.1; }
.kpi-label { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text-muted); margin-top: 2px; }

/* ---------- Intro + how to use ---------- */
.intro { max-width: 1180px; margin: 14px auto 0; padding: 0 16px; }
.intro-lead {
  margin: 0 0 10px;
  font-family: var(--font-serif);
  font-size: 16.5px;
  line-height: 1.45;
  color: var(--text);
}
.how-to {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius);
  box-shadow: var(--shadow-rest);
}
.how-to summary {
  list-style: none;
  cursor: pointer;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-family: var(--font-serif);
  font-size: 14px;
  font-weight: 600;
  color: var(--text-muted);
}
.how-to summary::-webkit-details-marker { display: none; }
.how-to[open] summary { color: var(--text); }
.how-to[open] summary .chev { transform: rotate(180deg); }
.how-to-body { padding: 2px 16px 14px; font-size: 13.5px; color: var(--text); }
.how-to-body ul { margin: 0; padding-left: 18px; display: flex; flex-direction: column; gap: 6px; }

/* ---------- Top patterns band (the headline trends) ---------- */
.patterns-band { max-width: 1180px; margin: 18px auto 0; padding: 0 16px; }
.band-head { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; margin-bottom: 8px; }
.band-title { font-family: var(--font-serif); font-size: 17px; margin: 0; }
.band-note { color: var(--text-muted); font-size: 12.5px; margin: 0; }

.patterns-rail {
  list-style: none;
  margin: 0;
  padding: 4px 0 6px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(195px, 1fr));
  gap: 10px;
}
.patterns-rail > li { min-width: 0; }
.pattern-card {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 6px;
  text-align: left;
  font: inherit;
  cursor: pointer;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 12px;
  box-shadow: var(--shadow-rest);
  transition: border-color 120ms ease, box-shadow 120ms ease;
}
.pattern-card:hover { box-shadow: var(--shadow-hover); border-color: var(--accent-line); }
.pattern-name {
  font-weight: 600;
  font-size: 13px;
  line-height: 1.3;
  color: var(--text);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.pattern-desc {
  font-size: 11.5px;
  line-height: 1.35;
  color: var(--text-muted);
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.pattern-stat { font-size: 12px; color: var(--text-muted); }
.pattern-count { font-family: var(--font-serif); font-size: 18px; font-weight: 700; color: var(--text); font-variant-numeric: tabular-nums; }
.pattern-track { height: 5px; background: var(--surface-2); border-radius: 3px; overflow: hidden; margin-top: auto; }
.pattern-bar { display: block; height: 100%; background: var(--accent-line); border-radius: 3px; }
.pattern-card[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-weak); }
.pattern-card[aria-pressed="true"] .pattern-bar { background: var(--accent); }

/* ---------- Corpus trends (charts over the baked aggregates) ---------- */
.trends-band { max-width: 1180px; margin: 18px auto 0; padding: 0 16px; }
.trends-grid { display: grid; grid-template-columns: 1.5fr 1fr 1fr; gap: 10px; padding: 4px 0 6px; }
.trend-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  box-shadow: var(--shadow-rest);
  min-width: 0;
}
.trend-card-title {
  margin: 0 0 11px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-muted);
}

/* Vertical columns — the year timeline. */
.trend-cols { display: flex; align-items: flex-end; gap: 5px; height: 104px; }
.trend-col { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; gap: 3px; }
.trend-col-val { font-size: 10.5px; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.trend-col-bar { width: 100%; max-width: 24px; min-height: 0; background: var(--accent-line); border-radius: 3px 3px 0 0; }
.trend-col-yr { font-size: 10px; color: var(--text-muted); white-space: nowrap; }

/* Horizontal bars — the few-category breakdowns. */
.trend-rows { display: flex; flex-direction: column; gap: 9px; }
.trend-row { display: grid; grid-template-columns: 88px minmax(0, 1fr) auto; align-items: center; gap: 8px; }
.trend-row-label { font-size: 12px; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.trend-track { height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.trend-bar { display: block; height: 100%; background: var(--accent-line); border-radius: 4px; }
.trend-row-val { font-size: 12px; color: var(--text-muted); font-variant-numeric: tabular-nums; min-width: 22px; text-align: right; }
.trend-note { margin: 9px 0 0; font-size: 10.5px; line-height: 1.4; color: var(--text-muted); }

@media (max-width: 1023px) {
  .trends-grid { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
}
@media (max-width: 560px) {
  .trends-grid { grid-template-columns: 1fr; }
}

/* ---------- Active filters (shows why the stream is narrowed) ---------- */
.active-filters { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-bottom: 12px; }
.active-chip {
  font: inherit;
  font-size: 12px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px 4px 10px;
  border: 1px solid var(--accent-line);
  background: var(--accent-weak);
  color: var(--accent-hover);
  border-radius: 999px;
  cursor: pointer;
}
.active-chip:hover { border-color: var(--accent); }
.active-chip-x { font-size: 11px; opacity: 0.85; }
.active-clear {
  font: inherit;
  font-size: 12px;
  background: none;
  border: 0;
  cursor: pointer;
  color: var(--text-muted);
  text-decoration: underline;
  padding: 4px 6px;
}
.active-clear:hover { color: var(--accent); }

/* ---------- Layout ---------- */
.layout {
  max-width: 1180px;
  margin: 16px auto 0;
  padding: 0 16px 48px;
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr);
  gap: 20px;
  align-items: start;
}

.filters {
  position: sticky;
  top: 70px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow-rest);
}

/* Sheet header (Done/close) + backdrop — only used when .filters is a bottom
   sheet (mobile/tablet). Hidden on desktop, where the rail is always visible. */
.sheet-header { display: none; }
.filters-backdrop { display: none; }

.field { border: 0; margin: 0 0 16px; padding: 0; }
.field-label {
  display: block;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 7px;
  font-weight: 600;
}
#search {
  width: 100%;
  font-size: 16px; /* prevents iOS zoom */
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
}

.chip-options { display: flex; flex-wrap: wrap; gap: 6px; }
.filter-chip {
  font: inherit;
  font-size: 12.5px;
  padding: 5px 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 999px;
  cursor: pointer;
  text-align: left;
}
.filter-chip:hover { background: var(--surface-2); }
.filter-chip[aria-pressed="true"] {
  background: var(--accent-weak);
  border-color: var(--accent);
  color: var(--accent-hover);
  font-weight: 600;
}
.chip-count { color: var(--text-muted); font-variant-numeric: tabular-nums; }
.filter-chip[aria-pressed="true"] .chip-count { color: var(--accent-hover); }

.btn-secondary {
  font: inherit;
  font-size: 13px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--accent);
  border-radius: 8px;
  cursor: pointer;
  min-height: 44px; /* touch target (DESIGN §7) */
}
.btn-secondary:hover { background: var(--surface-2); }
.link-btn { background: none; border: 0; color: var(--accent); cursor: pointer; font: inherit; text-decoration: underline; padding: 0; }

/* ---------- Stream toolbar ---------- */
/* Tapping a pattern scrolls here; clear the sticky header (filters sit at 70px). */
.stream-wrap { scroll-margin-top: 70px; }
.stream-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}
.result-count { color: var(--text-muted); font-size: 13px; }
.filters-toggle { display: none; }

/* ---------- Stream / cards ---------- */
.stream { display: flex; flex-direction: column; gap: 14px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-rest);
  overflow: hidden;
  transition: box-shadow 120ms ease, border-color 120ms ease;
}
.card[open] { border-color: var(--accent-line); }
.card:hover { box-shadow: var(--shadow-hover); }

.card-summary {
  list-style: none;
  cursor: pointer;
  padding: 14px 16px;
  display: block;
}
.card-summary::-webkit-details-marker { display: none; }

.source-line { display: flex; align-items: center; gap: 10px; }
.avatar {
  width: 36px; height: 36px; flex: none;
  border-radius: 50%;
  background: var(--accent-weak);
  color: var(--accent-hover);
  display: grid; place-items: center;
  font-weight: 700; font-size: 15px;
  border: 1px solid var(--accent-line);
}
.source-meta { min-width: 0; flex: 1; }
.company { font-weight: 600; font-size: 15px; }
.sub-meta { color: var(--text-muted); font-size: 12.5px; font-variant-numeric: tabular-nums; }
.disclosure { color: var(--text-muted); font-size: 13px; flex: none; }
.card[open] .disclosure .chev { transform: rotate(180deg); }
.chev { display: inline-block; transition: transform 200ms ease; }

.headline { font-family: var(--font-serif); font-size: 16px; margin: 10px 0 8px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.pill {
  font-size: 10.5px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  letter-spacing: 0.02em;
  white-space: nowrap;
}
.pill.solid { background: var(--accent); color: #fff; }
.pill.outline { border: 1px solid var(--border); color: var(--text-muted); }
.pill.kind { border: 1px solid var(--accent-line); color: var(--accent-hover); }
.pill.func { background: var(--surface-2); color: var(--text-muted); text-transform: capitalize; }
.pill.muted { border: 1px solid var(--border); color: var(--text-muted); background: var(--surface-2); }

/* ---------- Thread (expanded) ---------- */
.thread { padding: 0 16px 16px; border-top: 1px solid var(--border); }
.thread-root {
  background: var(--surface-2);
  border-radius: 8px;
  padding: 10px 12px;
  margin: 14px 0;
  font-size: 13.5px;
}
.thread-root dl { margin: 0; display: grid; grid-template-columns: 110px 1fr; gap: 4px 10px; }
.thread-root dt { color: var(--text-muted); font-size: 12px; }
.thread-root dd { margin: 0; }
.muted-cell { color: var(--text-muted); font-style: italic; }

.finding {
  position: relative;
  padding: 12px 0 12px 20px;
  border-top: 1px dashed var(--border);
}
.finding::before {
  content: "";
  position: absolute;
  left: 5px; top: 18px; bottom: 8px;
  width: 2px;
  background: var(--accent-line);
  border-radius: 2px;
}
.finding-head { display: flex; align-items: baseline; gap: 8px; }
.dot { width: 9px; height: 9px; border-radius: 50%; flex: none; position: relative; top: 3px; margin-left: -20px; }
.dot.finding-dot { background: var(--status-finding); }
.dot.other-dot { background: var(--text-muted); }
.finding-title { font-weight: 600; font-size: 14.5px; }
.finding-flag { font-size: 10px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-muted); }
.finding-summary { margin: 6px 0 0; font-size: 13.5px; color: var(--text); }

/* Per-finding taxonomy tags + the ratepayer-harm ("cost to customers") badge. */
.finding-tags { display: flex; flex-wrap: wrap; gap: 5px; margin: 8px 0 0; }
.finding-tag {
  font-size: 10.5px;
  line-height: 1.45;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-muted);
  border: 1px solid var(--border);
}
.finding-tag.cost-tag {
  background: transparent;
  color: var(--status-finding);
  border-color: var(--status-finding);
  font-weight: 600;
}

.recs { margin: 10px 0 0; padding: 0; list-style: none; }
.rec {
  position: relative;
  padding: 7px 0 7px 18px;
  font-size: 13px;
  color: var(--text);
}
.rec::before {
  content: "";
  position: absolute;
  left: 0; top: 12px;
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--status-rec);
}
.rec-num { font-weight: 600; color: var(--status-rec); font-variant-numeric: tabular-nums; }
.rec-page { color: var(--text-muted); font-size: 0.85em; font-variant-numeric: tabular-nums; white-space: nowrap; cursor: help; }

.thread-footer { margin-top: 14px; display: flex; flex-wrap: wrap; gap: 8px; }

/* ---------- No-findings state (reports not yet machine-extracted) ---------- */
.no-findings {
  margin: 14px 0 0;
  padding: 12px 14px;
  background: var(--surface-2);
  border: 1px dashed var(--border);
  border-radius: 8px;
  font-size: 13px;
  color: var(--text-muted);
}
.no-findings p { margin: 0; }
.no-findings strong { color: var(--text); }

/* ---------- Empty state ---------- */
.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 40px 16px;
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

/* ---------- Footer ---------- */
.site-footer {
  max-width: 1180px;
  margin: 0 auto;
  padding: 24px 16px 48px;
  color: var(--text-muted);
  font-size: 12.5px;
  border-top: 1px solid var(--border);
}
.site-footer p { margin: 0 0 6px; }

/* ---------- Responsive ---------- */
@media (max-width: 1023px) {
  .layout { grid-template-columns: 1fr; }
  /* Patterns become a horizontal swipe rail so trends stay one compact row, not a tall stack. */
  .patterns-rail {
    grid-template-columns: none;
    grid-auto-flow: column;
    grid-auto-columns: 230px;
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
  }
  .patterns-rail > li { scroll-snap-align: start; }
  .filters {
    position: fixed;
    inset: auto 0 0 0;
    top: auto;
    max-height: 75vh;
    overflow: auto;
    border-radius: 14px 14px 0 0;
    z-index: 40;
    transform: translateY(100%);
    transition: transform 200ms ease;
    box-shadow: 0 -6px 24px rgba(0, 0, 0, 0.18);
    padding-bottom: max(14px, env(safe-area-inset-bottom));
  }
  .filters.open { transform: translateY(0); }
  .filters-toggle { display: inline-block; }

  /* Bottom-sheet close affordances: a Done button + a tap-to-dismiss backdrop. */
  .sheet-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin: 0 0 12px;
  }
  .sheet-title { font-family: var(--font-serif); font-weight: 600; font-size: 15px; }
  .sheet-close {
    font: inherit;
    font-weight: 600;
    font-size: 14px;
    min-height: 44px;
    padding: 8px 18px;
    background: var(--accent);
    color: #fff;
    border: 0;
    border-radius: 8px;
    cursor: pointer;
  }
  .sheet-close:hover { background: var(--accent-hover); }
  .filters-backdrop:not([hidden]) {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(15, 15, 30, 0.45);
    z-index: 39;
  }
}

@media (max-width: 560px) {
  .kpi-strip { grid-auto-flow: column; grid-template-columns: none; grid-auto-columns: 60%; overflow-x: auto; scroll-snap-type: x mandatory; }
  .kpi { scroll-snap-align: start; }
  .brand-sub { display: none; }
  /* Tighten so trends + first cards are reachable with minimal scroll. */
  .intro-lead { font-size: 15.5px; }
  .card-summary { padding: 12px 13px; }
  .thread { padding: 0 13px 14px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
