:root {
  color-scheme: light;
  --ink: #152033;
  --muted: #607086;
  --line: #d9e2ec;
  --soft: #f5f8fb;
  --panel: #ffffff;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --shadow: 0 18px 45px rgba(17, 31, 54, 0.12);

  /* Chart chrome */
  --grid: #edf2f7;
  --axis: #c9d4e0;

  /* Categorical series slots (validated order — never re-assign on filter) */
  --s1: #2a78d6;
  --s2: #eb6834;
  --s3: #1baf7a;
  --s4: #eda100;
  --s5: #e87ba4;
  --s6: #008300;
  --s7: #4a3aa7;
  --s8: #e34948;

  /* Diverging pair for risers / fallers */
  --rise: #2a78d6;
  --fall: #e34948;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
  background: #eef3f8;
  color: var(--ink);
}

button,
input,
select {
  font: inherit;
}

.app-shell {
  width: min(1240px, calc(100% - 32px));
  margin: 0 auto;
  padding: 40px 0;
  display: grid;
  grid-template-columns: 340px 1fr;
  gap: 20px;
}

.control-panel,
.chart-panel .card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

/* The sidebar scrolls with the page as one flow: it is usually shorter than
   the charts, so it scrolls off first and the content continues. */
.control-panel {
  align-self: start;
  padding: 28px;
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--accent-dark);
  font-size: 0.78rem;
  font-weight: 800;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(2.2rem, 4vw, 3.2rem);
  line-height: 1;
}

.subtitle {
  margin: 14px 0 0;
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1.55;
}

.control-group {
  margin-top: 22px;
  padding-top: 22px;
  border-top: 1px solid var(--line);
}

.control-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  font-weight: 800;
  font-size: 0.9rem;
}

.mini-button {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: white;
  color: var(--accent-dark);
  padding: 3px 10px;
  font-size: 0.8rem;
  font-weight: 800;
  cursor: pointer;
}

.mini-button:hover {
  background: var(--soft);
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.conf-chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 7px 11px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: white;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 800;
  cursor: pointer;
}

.conf-chip .dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--line);
}

.conf-chip.active {
  border-color: #b9cdf3;
  background: #eef4ff;
  color: var(--ink);
}

.conf-chip.active .dot {
  background: var(--chip-color, var(--accent));
}

.range-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.range-row label {
  display: block;
  margin-bottom: 6px;
  font-size: 0.82rem;
  font-weight: 800;
  color: var(--muted);
}

.range-row select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 9px 10px;
  color: var(--ink);
}

.metric-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0;
  margin-top: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
}

.metric-button {
  border: 0;
  padding: 9px 8px;
  background: white;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 800;
  cursor: pointer;
}

.metric-button + .metric-button {
  border-left: 1px solid var(--line);
}

.metric-button.active {
  background: var(--accent);
  color: white;
}

.keyword-search {
  position: relative;
}

.keyword-search input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 11px 12px;
  color: var(--ink);
  outline: none;
}

.keyword-search input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.14);
}

.keyword-suggestions {
  position: absolute;
  z-index: 30;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  max-height: 260px;
  overflow: auto;
  background: white;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 12px 30px rgba(17, 31, 54, 0.16);
  padding: 4px;
}

.keyword-suggestions button {
  display: flex;
  width: 100%;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border: 0;
  border-radius: 6px;
  background: none;
  padding: 8px 10px;
  text-align: left;
  font-size: 0.9rem;
  cursor: pointer;
}

.keyword-suggestions button:hover,
.keyword-suggestions button:focus {
  background: var(--soft);
  outline: none;
}

.keyword-suggestions .suggestion-count {
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
  white-space: nowrap;
}

.keyword-chips {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 12px;
}

.keyword-chip {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--soft);
  font-size: 0.9rem;
  font-weight: 700;
}

.keyword-chip .key {
  flex: 0 0 auto;
  width: 14px;
  height: 3px;
  border-radius: 2px;
}

.keyword-chip .name {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.keyword-chip a,
.keyword-chip button {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border: 0;
  border-radius: 5px;
  background: none;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 800;
  text-decoration: none;
  cursor: pointer;
}

.keyword-chip a:hover,
.keyword-chip button:hover {
  background: #e3ebf5;
  color: var(--ink);
}

.empty-chips {
  color: var(--muted);
  font-size: 0.88rem;
}

.quick-adds {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 7px;
  margin-top: 12px;
}

.quick-adds-label {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

.quick-adds button {
  border: 0;
  border-radius: 6px;
  padding: 6px 9px;
  background: #dfefff;
  color: var(--accent-dark);
  font-size: 0.84rem;
  font-weight: 800;
  cursor: pointer;
}

.quick-adds button:hover {
  background: #cde3ff;
}

.action-grid {
  display: grid;
  gap: 8px;
  margin-top: 22px;
  padding-top: 22px;
  border-top: 1px solid var(--line);
}

.nav-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  border-radius: 6px;
  color: white;
  background: var(--accent);
  font-weight: 800;
  text-decoration: none;
}

.nav-button:hover {
  background: var(--accent-dark);
}

.nav-button-alt {
  background: #15803d;
}

.nav-button-alt:hover {
  background: #166534;
}

.chart-panel {
  display: grid;
  gap: 20px;
  align-content: start;
}

.status {
  color: var(--muted);
  font-weight: 700;
  padding: 0 4px;
}

.card {
  padding: 24px 26px 20px;
}

.card-header h2 {
  margin: 0;
  font-size: 1.2rem;
}

.card-subtitle {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 18px;
  margin-top: 14px;
  font-size: 0.88rem;
  font-weight: 700;
}

.legend:empty {
  display: none;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--ink);
}

.legend-item .key {
  width: 16px;
  height: 3px;
  border-radius: 2px;
}

.trend-chart {
  position: relative;
  margin-top: 10px;
  outline: none;
}

.trend-chart:focus-visible {
  border-radius: 8px;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.25);
}

.trend-chart svg {
  display: block;
  width: 100%;
  height: auto;
}

.axis-text {
  fill: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.end-label {
  fill: var(--ink);
  font-size: 12px;
  font-weight: 700;
}

.empty-note {
  margin-top: 14px;
  padding: 18px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  line-height: 1.5;
}

/* Section chips (hot card scope) */
.section-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.section-chip {
  border: 1px solid var(--line);
  border-radius: 999px;
  background: white;
  color: var(--muted);
  padding: 6px 12px;
  font-size: 0.86rem;
  font-weight: 800;
  cursor: pointer;
}

.section-chip.active {
  border-color: #b9cdf3;
  background: #eef4ff;
  color: var(--ink);
}

/* Ranked bars (hot keywords) */
.bar-list {
  display: grid;
  gap: 2px;
  margin-top: 14px;
}

.bar-row.static {
  cursor: default;
}

.bar-row.static:hover {
  background: none;
}

.bar-row.static:hover .bar-fill {
  background: var(--s1);
}

.bar-row {
  display: grid;
  grid-template-columns: minmax(150px, 220px) 1fr;
  align-items: center;
  gap: 12px;
  width: 100%;
  border: 0;
  border-radius: 6px;
  padding: 4px 6px;
  background: none;
  text-align: left;
  cursor: pointer;
}

.bar-row:hover,
.bar-row:focus-visible {
  background: var(--soft);
  outline: none;
}

.bar-row .bar-label {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--ink);
}

.bar-row .bar-label .name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.bar-row .tracked-mark {
  flex: 0 0 auto;
  color: var(--accent-dark);
  font-weight: 800;
}

.bar-row .bar-track {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.bar-row .bar-fill {
  height: 16px;
  min-width: 2px;
  background: var(--s1);
  border-radius: 0 4px 4px 0;
}

.bar-row:hover .bar-fill {
  background: #1c5cab;
}

.bar-row .bar-value {
  color: var(--ink);
  font-size: 0.85rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.bar-row .bar-delta {
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* Diverging bars (risers & fallers) */
.movers-years {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
}

.movers-years label,
.movers-years-vs {
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 800;
}

.movers-years select {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 7px 9px;
  color: var(--ink);
}

.movers-list {
  display: grid;
  gap: 2px;
  margin-top: 14px;
}

.mover-row {
  display: grid;
  grid-template-columns: minmax(140px, 200px) 1fr;
  align-items: center;
  gap: 12px;
  width: 100%;
  border: 0;
  border-radius: 6px;
  padding: 4px 6px;
  background: none;
  text-align: left;
  cursor: pointer;
}

.mover-row:hover,
.mover-row:focus-visible {
  background: var(--soft);
  outline: none;
}

.mover-row .mover-label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.9rem;
  font-weight: 700;
}

.mover-track {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr;
  align-items: center;
  height: 24px;
}

.mover-track::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 0;
  bottom: 0;
  width: 1px;
  background: var(--axis);
}

.mover-side {
  position: relative;
  display: flex;
  align-items: center;
  min-width: 0;
  height: 100%;
}

.mover-side.left {
  justify-content: flex-end;
}

.mover-fill {
  height: 16px;
  min-width: 2px;
}

.mover-side.right .mover-fill {
  background: var(--rise);
  border-radius: 0 4px 4px 0;
}

.mover-side.left .mover-fill {
  background: var(--fall);
  border-radius: 4px 0 0 4px;
}

.mover-value {
  margin: 0 8px;
  font-size: 0.82rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  white-space: nowrap;
}

.movers-note {
  margin-top: 10px;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.5;
}

.card-footnote {
  margin: 16px 0 0;
  padding-top: 14px;
  border-top: 1px solid var(--grid);
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.6;
}

.card-footnote strong {
  color: var(--ink);
}

.table-view tr.row-tentative td {
  color: var(--muted);
}

/* Table views */
.table-view {
  margin-top: 14px;
}

.table-view summary {
  width: fit-content;
  cursor: pointer;
  color: var(--accent-dark);
  font-weight: 800;
  font-size: 0.9rem;
}

.table-scroll {
  overflow-x: auto;
  margin-top: 10px;
}

.table-view table {
  border-collapse: collapse;
  width: 100%;
  font-size: 0.88rem;
}

.table-view th,
.table-view td {
  border-bottom: 1px solid var(--grid);
  padding: 7px 10px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.table-view th:first-child,
.table-view td:first-child {
  text-align: left;
}

.table-view th {
  color: var(--muted);
  font-weight: 800;
}

/* Tooltip */
.tooltip {
  position: fixed;
  z-index: 60;
  max-width: 320px;
  padding: 10px 12px;
  background: white;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 12px 30px rgba(17, 31, 54, 0.18);
  pointer-events: none;
  font-size: 0.86rem;
}

.tooltip .tooltip-title {
  font-weight: 800;
  margin-bottom: 6px;
}

.tooltip .tooltip-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 3px;
}

.tooltip .tooltip-row .key {
  flex: 0 0 auto;
  width: 12px;
  height: 3px;
  border-radius: 2px;
}

.tooltip .tooltip-row .value {
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.tooltip .tooltip-row .name {
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

@media (max-width: 960px) {
  .app-shell {
    grid-template-columns: 1fr;
    padding: 18px 0;
  }

  .control-panel {
    position: static;
  }
}

@media (max-width: 520px) {
  .app-shell {
    width: min(100% - 20px, 1240px);
  }

  .control-panel,
  .card {
    padding: 18px;
  }

  .bar-row,
  .mover-row {
    grid-template-columns: 1fr;
    gap: 4px;
  }
}
