/* stats.css — the Stats tab only.
 *
 * Leans on base.css for tokens and for .view-head / .view-scroll / .btn /
 * .chip / .empty-state / .section-label, so this tab still looks like the other
 * three. Everything below is either a bar chart drawn in CSS (no library: the
 * CSP forbids external origins, SPEC §10) or the import affordance.
 *
 * Target is a 390x844 iPhone in standalone PWA mode. The numbers here are sized
 * to be readable at arm's length without zooming, which is the entire point of
 * a stats screen you check on a phone.
 */

.stats-body {
  padding: 12px 16px calc(28px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 26px;
}

.stats-accounts {
  padding: 0 16px 4px;
  flex: 0 0 auto;
}

/* ---- Headline totals ---------------------------------------------------- */

/* Two columns on a phone. Four tiles across a 390px screen would put the
 * numbers below 20px, which defeats the "readable at a glance" requirement. */
.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.stat-grid-3 { grid-template-columns: repeat(3, 1fr); }

.stat-tile {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-height: 84px;
  justify-content: center;
}

.stat-value {
  font-size: 1.9rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
}

.stat-label {
  font-size: 0.78rem;
  color: var(--text-dim);
  letter-spacing: 0.01em;
}

.stats-subtotals {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  font-size: 0.82rem;
  margin-top: -16px; /* pull up under the tile grid; they read as one block */
}

/* ---- Sections ----------------------------------------------------------- */

.stats-section {
  display: flex;
  flex-direction: column;
}

.stats-section .section-hint { margin-bottom: 12px; }

/* ---- Top posts ---------------------------------------------------------- */

.top-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.top-item {
  width: 100%;
  min-height: 44px; /* tap target floor */
  display: flex;
  gap: 12px;
  align-items: flex-start;
  text-align: left;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--text);
  transition: transform 0.12s var(--ease-out);
}

.top-item:active {
  transform: scale(0.985);
  background: var(--surface-2);
}

.top-rank {
  flex: 0 0 22px;
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text-dim);
  padding-top: 1px;
}

.top-body {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0; /* without this the flex child refuses to shrink and clamp */
}

.top-text {
  font-size: 0.92rem;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.top-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 3px 10px;
  font-size: 0.76rem;
}

.top-pillar {
  color: var(--accent);
  max-width: 14ch;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---- Bars --------------------------------------------------------------- */

.bars {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.bar-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 5px;
}

.bar-label {
  font-size: 0.9rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.bar-value {
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--text);
  flex: 0 0 auto;
}

.bar-track {
  height: 10px;
  border-radius: 6px;
  background: var(--surface-2);
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  border-radius: 6px;
  background: linear-gradient(90deg, var(--accent), var(--edit));
  /* Grow from the left on first paint. transform only — animating width would
   * lay out every frame, and this list can be a dozen bars long. */
  transform-origin: left center;
  animation: bar-grow 0.42s var(--ease-out) both;
}

@keyframes bar-grow {
  from { transform: scaleX(0); }
  to { transform: scaleX(1); }
}

.bar-sub {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 12px;
  font-size: 0.76rem;
  margin-top: 5px;
}

/* ---- Import progress ---------------------------------------------------- */

.stats-progress {
  padding: 40px 4px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  text-align: center;
}

.progress-track {
  height: 6px;
  border-radius: 4px;
  background: var(--surface-2);
  overflow: hidden;
  margin-bottom: 6px;
}

/* Indeterminate on purpose: fetch() cannot report upload progress on Safari,
 * and a fake percentage that sticks at 90% is worse than an honest shuttle. */
.progress-fill {
  width: 40%;
  height: 100%;
  border-radius: 4px;
  background: var(--accent);
  animation: progress-shuttle 1.15s ease-in-out infinite;
}

@keyframes progress-shuttle {
  0% { transform: translateX(-105%); }
  100% { transform: translateX(255%); }
}

/* ---- Empty state -------------------------------------------------------- */

/* base.css centres .empty-state with `position: absolute; inset: 0`, which is
 * right for a one-line "nothing here" but wrong for this one: the click path is
 * five steps and is taller than a 390x844 screen. Absolutely positioned, it
 * escapes .view-scroll entirely and paints over the account chips. Back to
 * normal flow, with `margin-block: auto` doing the centring only while there is
 * room for it. */
.stats-body .empty-state {
  position: static;
  inset: auto;
  margin-block: auto;
  padding: 24px 4px;
}

.stats-empty { gap: 14px; }
.stats-empty .btn { margin-top: 6px; }

/* The click path. Numbered because it is a sequence Artur follows once a month
 * inside a different app, not a bullet list of options. */
.howto {
  margin: 0;
  padding: 14px 16px 14px 34px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  text-align: left;
  font-size: 0.88rem;
  color: var(--text-mid);
  display: flex;
  flex-direction: column;
  gap: 7px;
  max-width: 40ch;
}

.howto li::marker {
  color: var(--accent);
  font-weight: 700;
}

/* ---- Result / failure sheets -------------------------------------------- */

.stats-sheet {
  padding: 20px 16px calc(20px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.stats-sheet h2 { font-size: 1.15rem; font-weight: 700; }
.stats-sheet .btn { margin-top: 4px; }
.stats-sheet .stat-value { font-size: 1.5rem; }
.stats-sheet .stat-tile { min-height: 68px; padding: 10px; }

.stats-warn {
  font-size: 0.85rem;
  color: var(--warn);
  background: var(--surface-2);
  border-left: 3px solid var(--warn);
  border-radius: 0 8px 8px 0;
  padding: 9px 11px;
}

.stats-pre {
  margin: 0;
  padding: 12px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.76rem;
  line-height: 1.45;
  color: var(--text-mid);
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 40vh;
  max-height: min(40vh, calc(var(--vvh) * 0.4)); /* --vvh: the real viewport (ui.js) */
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

.stats-errors {
  font-size: 0.82rem;
  color: var(--text-dim);
}

.stats-errors summary {
  min-height: 44px;
  display: flex;
  align-items: center;
  cursor: default;
}

.stats-errors ul {
  margin: 0;
  padding-left: 20px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

/* ---- Footer ------------------------------------------------------------- */

.stats-foot {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-top: 4px;
  border-top: 1px solid var(--line);
}

.stats-foot .section-hint { margin: 0; }
.stats-foot .btn { margin-top: 6px; }

/* Wider phones and the odd desktop check: four tiles fit once there is room,
 * and the content stops stretching to a silly line length. */
@media (min-width: 560px) {
  .stat-grid { grid-template-columns: repeat(4, 1fr); }
  .stats-body { max-width: 720px; margin: 0 auto; width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  .bar-fill { animation: none; }
  .progress-fill { animation-duration: 2.4s; }
  .top-item { transition: none; }
}
