/* Stats tab. The page scrolls; no text below 12px. Marks are one hue (the accent) on
   grey context; text never takes a mark's colour. The grey marks (empty days, the
   period before) keep 3:1 against the card in both themes. */

:root {
  --chart-mark: var(--accent);
  --chart-lift: color-mix(in oklab, var(--accent) 82%, var(--text));
  --chart-quiet: color-mix(in srgb, var(--text) 36%, var(--surface));
  --chart-grid: color-mix(in srgb, var(--text) 14%, var(--surface));
  --chart-select: var(--accent-soft);
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme]) {
    --chart-quiet: color-mix(in srgb, var(--text) 50%, var(--surface));
    --chart-grid: color-mix(in srgb, var(--text) 9%, var(--surface));
  }
}

:root[data-theme="light"] {
  --chart-quiet: color-mix(in srgb, var(--text) 50%, var(--surface));
  --chart-grid: color-mix(in srgb, var(--text) 9%, var(--surface));
}

@supports not (color: color-mix(in srgb, red 50%, blue)) {
  :root,
  :root[data-theme="light"] {
    --chart-lift: var(--accent);
    --chart-quiet: #808084;
    --chart-grid: rgb(128 128 128 / 0.22);
  }
}

/* Period ---------------------------------------------------------------------- */

.stats-range {
  margin-bottom: var(--space-2);
}

.stats-range label {
  min-height: var(--tap);
}

.stats-caption {
  margin-bottom: var(--space-3);
}

.stats-body,
.stats-slice {
  transition: opacity var(--duration) var(--ease);
}

.stats-body.is-loading,
.stats-slice.is-loading {
  opacity: 0.55;
}

/* Cards ----------------------------------------------------------------------- */

.stats-card {
  display: grid;
  gap: var(--space-3);
  min-width: 0;
  margin-bottom: var(--space-3);
}

.stats-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2) var(--space-3);
  min-height: 36px;
}

.stats-card-heading {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 0 var(--space-2);
  min-width: 0;
}

.stats-empty {
  padding: var(--space-2) 0;
}

.stats-card .empty {
  padding: var(--space-5) var(--space-3);
}

.stats-views {
  margin-top: calc(-1 * var(--space-1));
}

.stats-more {
  justify-self: start;
}

.stats-library {
  margin-top: var(--space-4);
  text-align: center;
  text-wrap: balance;
}

/* Summary: a hero figure over three tiles, 3 across on a phone. */

.kpis {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: var(--space-4) var(--space-2);
  margin: 0;
}

.kpi {
  display: grid;
  align-content: start;
  gap: 2px;
  min-width: 0;
}

.kpi dd {
  margin: 0;
}

.kpi-hero {
  grid-column: 1 / -1;
  gap: var(--space-1);
}

.kpi-label {
  color: var(--muted);
  font-size: var(--text-sm);
  font-weight: 600;
}

.kpi-value {
  overflow-wrap: anywhere;
  font-size: var(--text-xl);
  font-weight: 600;
  line-height: 1.15;
}

.kpi-hero .kpi-value {
  font-size: 48px;
  line-height: 1;
  letter-spacing: -0.02em;
}

.kpi-change {
  color: var(--muted);
  font-size: var(--text-xs);
}

.kpi-hero .kpi-change {
  font-size: var(--text-sm);
}

.change-arrow .icon {
  margin-right: 2px;
  vertical-align: -2px;
}

.change-arrow[data-dir="down"] .icon {
  transform: rotate(180deg);
}

/* Legends --------------------------------------------------------------------- */

.chart-legend {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  color: var(--muted);
  font-size: var(--text-xs);
}

.key {
  flex: none;
  display: inline-block;
  background: var(--chart-quiet);
}

.key ~ .key {
  margin-left: var(--space-2);
}

.key-bar {
  width: 10px;
  height: 10px;
  border-radius: 2px;
  background: var(--chart-mark);
}

.key-line {
  width: 14px;
  height: 2px;
  border-radius: 1px;
}

.key-tick {
  width: 2px;
  height: 12px;
  border-radius: 1px;
}

/* Column charts ------------------------------------------------------------------
   Columns are buttons, one per bucket, with the bar's share of the plot in --v. A tap
   anywhere over the plot or the axis picks the nearest column (stats.js). */

.chart-readout {
  display: grid;
  grid-template-columns: var(--tap) minmax(0, 1fr) var(--tap);
  align-items: center;
  margin: calc(-1 * var(--space-2)) calc(-1 * var(--space-2)) 0;
}

.chart-readout-text {
  font-size: var(--text-sm);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-align: center;
  overflow-wrap: anywhere;
}

.chart-step:first-child .icon {
  transform: scaleX(-1);
}

.chart-step[aria-disabled="true"] {
  opacity: 0.3;
  cursor: default;
}

.chart-plot {
  --plot: 120px;
  --head: 16px; /* room for the peak's label */
  --axis: 20px;
  --gutter: 0px;
  position: relative;
  display: grid;
  grid-template-rows: var(--plot) var(--axis);
  padding-left: var(--gutter);
  touch-action: pan-y;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

.chart-plot.has-grid {
  --gutter: 28px;
}

.chart-plot-hours {
  --plot: 80px;
}

.chart-plot-days {
  --plot: 64px;
}

.chart-plot + .chart-plot {
  margin-top: var(--space-2);
}

/* As many equal columns as there are buckets, in both rows. */
.chart-columns,
.chart-axis {
  display: grid;
  grid-auto-columns: minmax(0, 1fr);
  grid-auto-flow: column;
}

.chart-columns {
  position: relative;
  z-index: 1;
}

.chart-column {
  position: relative;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  min-width: 0;
  height: 100%;
  padding: 0;
  border: 0;
  border-radius: 4px 4px 0 0;
  background: transparent;
  color: inherit;
  cursor: pointer;
}

.chart-column[aria-pressed="true"] {
  background: var(--chart-select);
}

.chart-column:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.chart-bar {
  width: min(24px, calc(100% - 2px));
  height: max(4px, calc(var(--v) * (100% - var(--head))));
  border-radius: 4px 4px 0 0;
  background: var(--chart-mark);
}

.chart-bar.is-zero {
  height: 2px;
  border-radius: 1px;
  background: var(--chart-quiet);
}

@media (hover: hover) {
  .chart-column.is-hover .chart-bar:not(.is-zero) {
    background: var(--chart-lift);
  }
}

.chart-cap {
  position: absolute;
  bottom: calc(max(4px, var(--v) * (100% - var(--head))) + 2px);
  left: 50%;
  color: var(--muted);
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums;
  line-height: 1;
  white-space: nowrap;
  transform: translateX(-50%);
  pointer-events: none;
}

/* Gridlines and the period-before average: the height bars share. */
.chart-layer {
  position: absolute;
  top: var(--head);
  right: 0;
  left: var(--gutter);
  z-index: 2;
  height: calc(var(--plot) - var(--head));
  pointer-events: none;
}

.chart-grid {
  z-index: 0;
}

.chart-gridline {
  position: absolute;
  right: 0;
  bottom: calc(var(--at) * 100%);
  left: 0;
  height: 1px;
  background: var(--chart-grid);
}

.chart-gridlabel {
  position: absolute;
  right: calc(100% + 6px);
  bottom: 0;
  color: var(--muted);
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums;
  line-height: 1;
  transform: translateY(50%);
}

.chart-average {
  position: absolute;
  right: 0;
  bottom: calc(var(--at) * 100% - 1px);
  left: 0;
  height: 2px;
  border-radius: 1px;
  background: var(--chart-quiet);
}

.chart-axis {
  align-items: end;
}

.chart-axis > span {
  display: flex;
  justify-content: center;
  min-width: 0;
  color: var(--muted);
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums;
  line-height: 1;
  white-space: nowrap;
}

.chart-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums;
}

.chart-table th,
.chart-table td {
  padding: 6px var(--space-2);
  border-bottom: 1px solid var(--border);
  text-align: right;
}

.chart-table th:first-child {
  padding-left: 0;
  font-weight: 500;
  text-align: left;
}

.chart-table thead th {
  color: var(--muted);
  font-size: var(--text-xs);
  font-weight: 600;
}

.stats-tables {
  display: grid;
  gap: var(--space-4);
}

/* Ranked rows ------------------------------------------------------------------- */

.rank-list {
  display: grid;
  gap: 2px;
  margin: 0 calc(-1 * var(--space-2));
  padding: 0;
  list-style: none;
}

.rank-artist {
  display: grid;
  gap: 6px;
  width: 100%;
  min-height: 52px;
  padding: var(--space-2);
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: inherit;
  text-align: left;
  transition: background-color var(--duration) var(--ease);
}

.stats-card button.list-item:active,
.rank-artist:active {
  background: var(--surface-2);
}

@media (hover: hover) {
  .stats-card button.list-item:hover,
  .rank-artist:hover {
    background: var(--surface-2);
  }
}

.rank-line {
  display: flex;
  align-items: baseline;
  gap: var(--space-2);
  min-width: 0;
}

.rank-name {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  font-weight: 600;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.rank-line > .list-item-title {
  flex: 0 1 auto;
  min-width: 0;
}

.rank-count {
  flex: none;
  font-size: var(--text-sm);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* One width per list, so the bars beside the counts share a scale. */
.rank-track > .rank-count {
  align-self: center;
  min-width: 3ch;
  text-align: right;
}

.rank-track.is-quiet > .rank-count {
  min-width: 5.2em;
}

.tag {
  flex: none;
  align-self: center;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--text);
  font-size: var(--text-xs);
  font-weight: 600;
  line-height: 18px;
}

.rank-track-bar {
  position: relative;
  display: block;
  height: 4px;
  border-radius: 2px;
  background: var(--surface-2);
}

.rank-track .rank-track-bar {
  margin-top: 4px;
}

.rank-bar {
  display: block;
  width: calc(var(--v) * 100%);
  height: 100%;
  border-radius: 2px;
  background: var(--chart-mark);
}

.rank-track.is-quiet .rank-bar {
  background: var(--chart-quiet);
}

.rank-mark {
  position: absolute;
  top: -4px;
  left: clamp(0px, calc(var(--at) * 100% - 1px), calc(100% - 2px));
  width: 2px;
  height: 12px;
  border-radius: 1px;
  background: var(--chart-quiet);
}

.rank-track {
  min-height: 56px;
}

.cover-sm {
  --cover-size: 40px;
}

.cover-xs {
  --cover-size: 32px;
  border-radius: 6px;
}

.slice-row {
  min-height: 48px;
  padding-block: 6px;
}

.slice-more {
  padding-top: var(--space-1);
}

/* From ListenBrainz ------------------------------------------------------------ */

.stats-source {
  margin: var(--space-5) 0 var(--space-3);
}

.stats-lb[aria-busy="true"] {
  min-height: 120px;
}

.stats-note {
  font-size: var(--text-sm);
  text-wrap: pretty;
}

.stats-external {
  justify-self: start;
  gap: var(--space-2);
}

.stats-retry {
  justify-self: start;
}

/* A country is a native disclosure: its row is the summary, its artists the body. */
.lb-country > summary {
  display: block;
  list-style: none;
  border-radius: var(--radius);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.lb-country > summary::-webkit-details-marker {
  display: none;
}

.lb-country > summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.lb-country > summary:active .rank-artist {
  background: var(--surface-2);
}

@media (hover: hover) {
  .lb-country > summary:hover .rank-artist {
    background: var(--surface-2);
  }
}

.lb-country[open] > summary .rank-name {
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
}

/* The map: land in the chart grey, each country with listens in the accent at one
   of four opacities, rings for the countries too small to see. */
.lb-map-figure {
  display: grid;
  gap: var(--space-2);
}

.lb-map {
  display: block;
  width: 100%;
  height: auto;
  pointer-events: none;
}

.lb-map use {
  fill-rule: evenodd;
}

.lb-map-figure [data-tone="1"] {
  --tone: 0.3;
}

.lb-map-figure [data-tone="2"] {
  --tone: 0.5;
}

.lb-map-figure [data-tone="3"] {
  --tone: 0.75;
}

.map-land {
  fill: var(--chart-grid);
}

.map-tone {
  fill: var(--chart-mark);
  fill-opacity: var(--tone, 1);
}

.map-ring {
  fill: none;
  stroke: var(--chart-mark);
  stroke-opacity: var(--tone, 1);
  stroke-width: 1.5px;
}

/* The country whose row is open (paths and rings keep 1.5px at any size). */
.lb-map .is-selected {
  stroke: var(--text);
  stroke-opacity: 1;
  stroke-width: 1.5px;
}

.lb-map-legend {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 2px var(--space-3);
  margin: 0;
  color: var(--muted);
  font-size: var(--text-xs);
}

.lb-map-legend > span {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.map-key {
  position: relative;
  width: 10px;
  height: 10px;
  overflow: hidden;
  border-radius: 2px;
  background: var(--chart-grid);
}

.map-key::before {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--chart-mark);
  opacity: var(--tone, 1);
}

.lb-flag {
  flex: none;
  width: 1.25em;
  text-align: center;
}

.lb-disclose {
  display: inline-flex;
  flex: none;
  align-self: center;
  color: var(--muted);
  transform: rotate(90deg);
  transition: transform var(--duration) var(--ease);
}

.lb-country[open] .lb-disclose,
.stats-about[open] > summary .lb-disclose {
  transform: rotate(-90deg);
}

.lb-country .rank-bar {
  min-width: 4px;
}

/* Artists line up under the country's name (past the flag). */
.lb-artists {
  display: grid;
  margin: 0;
  padding: 0 0 var(--space-1) calc(var(--space-2) * 2 + 1.25em);
  list-style: none;
}

.lb-artist {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  width: 100%;
  min-height: var(--tap);
  padding: 0 var(--space-2);
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: inherit;
  font-size: var(--text-sm);
  text-align: left;
}

button.lb-artist:active {
  background: var(--surface-2);
}

@media (hover: hover) {
  button.lb-artist:hover {
    background: var(--surface-2);
  }
}

.lb-artist > .icon {
  flex: none;
  color: var(--muted);
}

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

.lb-artist.is-plain {
  padding-right: calc(var(--space-2) * 2 + 16px); /* where a chevron would be */
}

.lb-artists-more {
  padding: 0 0 var(--space-2) calc(var(--space-2) * 3 + 1.25em);
}

.lb-countries-more {
  padding: var(--space-1) var(--space-2);
}

.lb-missing {
  display: grid;
  gap: var(--space-2);
}

.lb-missing dl {
  display: grid;
  gap: var(--space-2);
  margin: 0;
}

.lb-missing dt {
  color: var(--muted);
  font-size: var(--text-xs);
  font-weight: 600;
}

.lb-missing dd {
  margin: 2px 0 0;
  font-size: var(--text-sm);
  overflow-wrap: anywhere;
}

.lb-release-icon {
  display: inline-flex;
  flex: none;
  color: var(--muted);
}

.stats-about {
  margin-bottom: var(--space-3);
  padding-block: 0;
}

.stats-about > summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  list-style: none;
  min-height: var(--tap);
  font-weight: 600;
  cursor: pointer;
}

.stats-about > summary::-webkit-details-marker {
  display: none;
}

.stats-about > summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

.stats-about-body {
  display: grid;
  gap: var(--space-2);
  padding-bottom: var(--space-4);
  font-size: var(--text-sm);
}

.stats-about-body p {
  margin: 0;
}

@media (prefers-reduced-motion: reduce) {
  .lb-disclose {
    transition: none;
  }
}

/* Sign-out, at the bottom of Stats for want of an app header. */
.account {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2) var(--space-3);
  margin-top: var(--space-5);
  padding-top: var(--space-4);
  border-top: 1px solid var(--border);
}

.account-user {
  overflow-wrap: anywhere;
}

[data-host="telegram"] .account {
  display: none;
}
