:root {
  --bg: var(--tg-theme-secondary-bg-color, #f1f1ef);
  --card: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #fcfcfb));
  --text: var(--tg-theme-text-color, #0b0b0b);
  --muted: var(--tg-theme-hint-color, #6b6a66);
  --accent: var(--tg-theme-button-color, #2a78d6);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --line: color-mix(in srgb, var(--text) 9%, transparent);
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --good: #1a8f4c;
  --bad: #d03b3b;
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: var(--tg-theme-secondary-bg-color, #111110);
    --card: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #1a1a19));
    --text: var(--tg-theme-text-color, #f0efec);
    --muted: var(--tg-theme-hint-color, #a3a29b);
    --series-1: #3987e5;
    --series-2: #d95926;
    --good: #3fbf72;
    --bad: #ef6b6b;
  }
}
:root[data-theme="dark"] {
  --bg: var(--tg-theme-secondary-bg-color, #111110);
  --card: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #1a1a19));
  --text: var(--tg-theme-text-color, #f0efec);
  --muted: var(--tg-theme-hint-color, #a3a29b);
  --series-1: #3987e5;
  --series-2: #d95926;
  --good: #3fbf72;
  --bad: #ef6b6b;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  padding: 0 0 calc(24px + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; }

.top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px 8px; gap: 12px;
}
.ch { display: flex; align-items: center; gap: 10px; min-width: 0; }
.ch img { width: 40px; height: 40px; border-radius: 50%; background: var(--line); flex: none; }
.ch img:not([src]) { visibility: hidden; }
#chTitle { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 50%; border: 0; background: var(--card);
  font-size: 20px; flex: none;
}
.icon-btn.spin { animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.tabs {
  position: sticky; top: 0; z-index: 5; display: flex; gap: 4px;
  margin: 0 16px 12px; padding: 4px; border-radius: 12px; background: var(--card);
}
.tabs button {
  flex: 1; border: 0; background: none; padding: 8px 0; border-radius: 9px; font-weight: 550;
  color: var(--muted);
}
.tabs button.on { background: var(--accent); color: var(--accent-text); }

main, .sheet-body { padding: 0 16px; }
section { display: grid; gap: 12px; }

.chips { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.chip {
  border: 1px solid var(--line); background: var(--card); border-radius: 999px;
  padding: 5px 12px; font-size: 13px; color: var(--muted);
}
.chip.on { border-color: var(--accent); color: var(--text); font-weight: 600; }
select.chip { appearance: none; padding-right: 12px; }
.spacer { flex: 1; }

.kpis { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.kpi { background: var(--card); border-radius: var(--radius); padding: 12px 14px; min-width: 0; }
.kpi .label { color: var(--muted); font-size: 12.5px; }
.kpi .value { font-size: 24px; font-weight: 700; letter-spacing: -0.01em; margin-top: 2px; }
.kpi .delta { font-size: 12.5px; margin-top: 2px; color: var(--muted); }
.up { color: var(--good); }
.down { color: var(--bad); }

.card { background: var(--card); border-radius: var(--radius); padding: 14px; min-width: 0; }
.card h3 { margin: 0; font-size: 15px; font-weight: 650; }
.card .sub { color: var(--muted); font-size: 12.5px; margin: 2px 0 10px; }
.chart { position: relative; height: 190px; }
.chart.tall { height: 220px; }
.empty { color: var(--muted); font-size: 13px; padding: 18px 0; text-align: center; }

/* horizontal bars, labels always visible */
.hbars { display: grid; gap: 9px; }
.hbar .row { display: flex; justify-content: space-between; gap: 8px; font-size: 13.5px; }
.hbar .row span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar .track { height: 8px; border-radius: 4px; background: var(--line); margin-top: 4px; overflow: hidden; }
.hbar .fill { height: 100%; border-radius: 4px; background: var(--series-1); }

.progress { display: grid; gap: 12px; }
.progress .track { height: 8px; border-radius: 4px; background: var(--line); margin-top: 5px; overflow: hidden; }
.progress .fill { height: 100%; border-radius: 4px; background: var(--series-1); }
.progress .row { display: flex; justify-content: space-between; font-size: 13.5px; }

/* video list */
.vlist { display: grid; gap: 8px; }
.vrow {
  display: grid; grid-template-columns: 112px 1fr; gap: 10px; align-items: center;
  background: var(--card); border-radius: var(--radius); padding: 8px; border: 0; text-align: left; width: 100%;
}
.vrow img { width: 112px; aspect-ratio: 16 / 9; object-fit: cover; border-radius: 8px; background: var(--line); }
.vrow .t {
  font-weight: 600; font-size: 14px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden;
}
.vrow .m { color: var(--muted); font-size: 12.5px; margin-top: 3px; display: flex; flex-wrap: wrap; gap: 4px 10px; }
.badge {
  display: inline-block; font-size: 11px; font-weight: 650; padding: 1px 6px; border-radius: 5px;
  background: color-mix(in srgb, var(--series-1) 16%, transparent); color: var(--text);
}
.badge.short { background: color-mix(in srgb, var(--series-2) 18%, transparent); }

.table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.table th { text-align: left; font-weight: 500; color: var(--muted); padding: 4px 0; }
.table td { padding: 5px 0; border-top: 1px solid var(--line); }
.table td:not(:first-child), .table th:not(:first-child) { text-align: right; }

/* video detail sheet */
.sheet { position: fixed; inset: 0; z-index: 20; background: var(--bg); overflow-y: auto; padding-bottom: 32px; }
.sheet .hero { width: 100%; aspect-ratio: 16 / 9; object-fit: cover; display: block; background: var(--line); }
.sheet h2 { font-size: 18px; margin: 12px 0 2px; line-height: 1.3; }
.sheet-body { display: grid; gap: 12px; }
.sheet .close {
  position: fixed; top: 10px; left: 10px; z-index: 21; border: 0; border-radius: 999px;
  background: rgba(0, 0, 0, 0.55); color: #fff; padding: 6px 12px;
}
.btn {
  display: block; width: 100%; border: 0; border-radius: 12px; padding: 11px; font-weight: 600;
  background: var(--accent); color: var(--accent-text);
}

.foot { color: var(--muted); font-size: 12px; text-align: center; padding: 4px 0 8px; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 30;
  background: rgba(20, 20, 20, 0.9); color: #fff; padding: 9px 14px; border-radius: 10px; font-size: 14px;
  max-width: calc(100% - 32px);
}
