:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #16181d;
  --muted: #646b78;
  --border: #e2e5ea;
  --accent: #d6281f;
  --accent-text: #ffffff;
  --accent-soft: #fdecea;
  --info-bg: #eef4ff;
  --info-text: #1d3f8a;
  --warn-bg: #fff6e0;
  --warn-text: #7a5300;
  --error-bg: #fdecea;
  --error-text: #9b1c14;
  --radius: 12px;
  --chart-bar: #d6281f;
  --chart-grid: #eceef1;
  --chart-axis: #c9ced6;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --surface: #1b1e24;
    --text: #eceef2;
    --muted: #9aa1ad;
    --border: #2d313a;
    --accent: #ff4e45;
    --accent-soft: #3a1d1b;
    --info-bg: #1a2640;
    --info-text: #b8cdf7;
    --warn-bg: #3a2e12;
    --warn-text: #f3d48b;
    --error-bg: #3e1b18;
    --error-text: #ffb4ad;
    --chart-bar: #ec5a52;
    --chart-grid: #262a31;
    --chart-axis: #3a3f49;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  padding: 0 16px 32px;
}

.page-header, .layout, .page-footer, body > .card, body > .channel-card, body > .leader-card {
  max-width: 1120px;
  margin-left: auto;
  margin-right: auto;
}

.page-header { padding: 28px 0 16px; }
.page-header h1 { margin: 0; font-size: 26px; }
.subtitle { margin: 4px 0 0; color: var(--muted); }

.layout {
  display: grid;
  grid-template-columns: minmax(0, 5fr) minmax(0, 7fr);
  gap: 20px;
  align-items: start;
}
@media (max-width: 860px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}
.card h2 { margin: 0 0 14px; font-size: 18px; }

.page-footer { margin-top: 24px; font-size: 13px; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: 13px; margin: 8px 0; }
code { font-size: 0.92em; background: var(--bg); padding: 1px 4px; border-radius: 4px; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

input[type="text"], input[type="password"], input[type="date"], select {
  font: inherit;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  min-height: 38px;
}
input:focus-visible, select:focus-visible, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
input[aria-invalid="true"] { border-color: var(--accent); }

.btn {
  font: inherit;
  border-radius: 8px;
  border: 1px solid var(--border);
  padding: 8px 16px;
  min-height: 38px;
  cursor: pointer;
  background: var(--surface);
  color: var(--text);
  white-space: nowrap;
}
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-link {
  font: inherit; font-size: 13px; background: none; border: 0; padding: 2px 4px;
  color: var(--accent); cursor: pointer;
}
.btn-link:disabled { color: var(--muted); cursor: default; }
.btn-icon {
  background: none; border: 0; color: var(--muted); cursor: pointer;
  width: 32px; height: 32px; border-radius: 8px; font-size: 14px; flex: none;
}
.btn-icon:hover { background: var(--accent-soft); color: var(--accent); }

.add-form { display: flex; gap: 8px; }
.add-form input { flex: 1; min-width: 0; }

.msg { min-height: 1.2em; margin: 6px 0; font-size: 14px; }
.msg.error { color: var(--error-text); }
.msg.ok { color: var(--muted); }

.list-toolbar {
  display: flex; justify-content: space-between; align-items: center;
  font-size: 13px; padding: 6px 0; border-bottom: 1px solid var(--border);
}

.account-list { list-style: none; margin: 0; padding: 0; }
.account {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 0; border-bottom: 1px solid var(--border);
}
.account:last-child { border-bottom: 0; }
.account.flash { animation: flash 1.2s ease-out; }
@keyframes flash { from { background: var(--accent-soft); } to { background: transparent; } }
.account-check { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; cursor: pointer; }
.account-check input { width: 18px; height: 18px; flex: none; accent-color: var(--accent); }
.avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--bg); flex: none; object-fit: cover; }
.account-text { display: flex; flex-direction: column; min-width: 0; }
.account-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account-name-row { display: flex; align-items: center; gap: 6px; min-width: 0; }
.account-note { font-size: 12px; color: var(--muted); }
.account-note.warn { color: var(--warn-text); }
.account-note a { color: inherit; }
.back-link { color: var(--accent); }
body > .import-card { max-width: 760px; margin: 0 auto 16px; }
.steps { padding-left: 20px; line-height: 1.6; }
.steps li { margin: 6px 0; }
.bookmarklet {
  display: inline-block; margin: 4px 6px; padding: 6px 12px; border-radius: 8px;
  background: var(--accent); color: var(--accent-text); font-weight: 600; text-decoration: none; cursor: grab;
}
.manual-form { display: flex; flex-direction: column; gap: 10px; align-items: stretch; }
.manual-form button { align-self: flex-start; }
.manual-form textarea {
  font: inherit; font-size: 13px; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; resize: vertical;
}
.account-link { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account-link:hover { color: var(--accent); }

.empty { color: var(--muted); padding: 16px 0; font-size: 14px; }

.period-row { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.custom-dates { display: flex; gap: 10px; flex-wrap: wrap; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.field > select, .field > input { color: var(--text); }

.loading { display: flex; align-items: center; gap: 10px; padding: 16px 0; color: var(--muted); }
.spinner {
  width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid var(--border); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.banner { border-radius: 8px; padding: 10px 14px; font-size: 14px; margin: 12px 0; }
.banner-warn { background: var(--warn-bg); color: var(--warn-text); }
.banner-error { background: var(--error-bg); color: var(--error-text); }
.banner-info { background: var(--info-bg); color: var(--info-text); }
.key-card { margin-bottom: 20px; }
.header-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
body > .login-card { max-width: 380px; margin: 12vh auto 0; }
.login-card h1 { font-size: 20px; margin: 0 0 4px; }
.login-form { display: flex; flex-direction: column; gap: 12px; margin-top: 12px; }
.login-form input { width: 100%; }
.login-form button { align-self: flex-start; }
.page-banner { max-width: 1120px; margin: 0 auto 16px; }
.key-card .msg:empty { display: none; }
.key-view {
  display: flex; flex-wrap: wrap; gap: 8px; justify-content: space-between; align-items: center;
  font-size: 14px;
}
.key-view b { font-variant-numeric: tabular-nums; letter-spacing: 0.04em; }

.total { padding: 16px 0 8px; }
.total-label { font-size: 14px; color: var(--muted); }
.total-details { margin-top: 10px; }
.total-value { font-size: 44px; font-weight: 700; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.note {
  font-size: 13px; background: var(--bg); border-radius: 8px;
  padding: 10px 12px; margin: 4px 0 14px;
}

.breakdown { width: 100%; border-collapse: collapse; font-size: 14px; }
.breakdown th, .breakdown td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--border); }
.breakdown th { font-weight: 600; color: var(--muted); font-size: 13px; }
.breakdown .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.breakdown td a { color: inherit; }
.breakdown .row-error td { color: var(--error-text); }

.videos { margin-top: 14px; font-size: 14px; }
.videos summary { cursor: pointer; color: var(--accent); }
.video-list { padding-left: 22px; margin: 8px 0 0; max-height: 360px; overflow: auto; }
.video-list li { padding: 3px 0; }
.video-list a { color: inherit; }
.video-meta { color: var(--muted); font-size: 12px; }

/* ---------- Per-channel details ---------- */
.inline-field { flex-direction: row; align-items: center; gap: 8px; margin: 8px 0 0; }
.name-link { font-size: 14px; padding: 0; color: var(--text); text-align: left; text-decoration: underline; text-decoration-color: var(--border); text-underline-offset: 3px; }
.name-link:hover { color: var(--accent); }
.account .details { flex: none; }

.channel-card { margin-top: 20px; scroll-margin-top: 16px; }
.ch-head { display: flex; gap: 14px; align-items: flex-start; flex-wrap: wrap; }
.ch-head-text { flex: 1; min-width: 200px; display: flex; flex-direction: column; }
.ch-head-text h2 { margin: 0 0 2px; }
.ch-meta { font-size: 13px; }
.ch-head-actions { display: flex; gap: 8px; align-items: flex-end; }
.ch-head-actions select { max-width: 240px; }
.avatar-lg { width: 56px; height: 56px; }

.section-title { font-size: 15px; margin: 20px 0 10px; }
.section-title .muted { font-weight: 400; font-size: 13px; }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.tile { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; min-width: 0; }
.tile-label { font-size: 12px; color: var(--muted); }
.tile-value { font-size: 22px; font-weight: 600; margin-top: 2px; }
.tile-sub { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.chart { margin: 16px 0 0; }
.chart-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 8px; }
.chart-caption { font-size: 14px; font-weight: 600; }
.chart-note { margin-top: 10px; font-size: 13px; color: var(--muted); background: var(--bg); border-radius: 8px; padding: 10px 12px; }
.chart-note summary { cursor: pointer; color: var(--text); font-weight: 600; }
.chart-note ul { margin: 8px 0 0; padding-left: 18px; }
.chart-note li { margin: 3px 0; }
.channel-card.single .ch-col, .channel-card.single .ch-col-opt { display: none; }
.channel-card.group .avatar-lg { display: none; }
.ch-cell { color: var(--muted); font-size: 13px; white-space: nowrap; max-width: 160px; overflow: hidden; text-overflow: ellipsis; }
.chart-box { position: relative; }
.chart-area { width: 100%; overflow: hidden; }
.chart-svg { display: block; max-width: 100%; height: auto; }
.chart-grid { stroke: var(--chart-grid); stroke-width: 1; }
.chart-axis { stroke: var(--chart-axis); stroke-width: 1; }
.chart-tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart-line { fill: none; stroke: var(--chart-bar); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart-area-fill { fill: var(--chart-bar); fill-opacity: 0.1; }
.chart-dot { fill: var(--chart-bar); stroke: var(--surface); stroke-width: 2; }
.chart-crosshair { stroke: var(--muted); stroke-width: 1; }
.chart-overlay { fill: transparent; cursor: crosshair; outline: none; }
.chart-overlay:focus-visible { stroke: var(--accent); stroke-width: 1; }
.chart-badge { fill: var(--chart-grid); stroke: var(--chart-axis); stroke-width: 1; }
.chart-badge-text { fill: var(--text); font-size: 10px; font-weight: 600; font-variant-numeric: tabular-nums; }
.chart-tooltip {
  position: absolute; pointer-events: none; z-index: 2;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 12px; white-space: nowrap;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.18);
}
.tt-when { font-size: 12px; font-weight: 600; }
.tt-value { font-size: 20px; font-weight: 700; display: flex; align-items: center; gap: 8px; font-variant-numeric: tabular-nums; }
.tt-key { display: inline-block; width: 14px; height: 2px; background: var(--chart-bar); border-radius: 1px; }
.tt-sub { font-size: 12px; color: var(--muted); }
.sparkline { display: block; }
.sparkline-line { stroke-width: 1.5; }

.table-toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; margin: 20px 0 8px; }
.table-toolbar .grow { flex: 1; min-width: 180px; }
.table-toolbar input[type="search"] {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; min-height: 38px;
}
.table-scroll { overflow-x: auto; }
.shorts-table { min-width: 640px; }
.shorts-table td { vertical-align: middle; }
.th-sort {
  font: inherit; font-weight: 600; color: inherit; background: none; border: 0; padding: 0; cursor: pointer;
}
.th-sort:hover { color: var(--text); }
th[aria-sort] .th-sort { color: var(--text); }
th[aria-sort="descending"] .th-sort::after { content: " ↓"; }
th[aria-sort="ascending"] .th-sort::after { content: " ↑"; }
.video-cell { display: flex; align-items: center; gap: 10px; min-width: 240px; }
.video-cell a { color: inherit; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.video-thumb { width: 48px; height: 36px; object-fit: cover; border-radius: 4px; flex: none; background: var(--bg); }

/* ---------- Leaderboard ---------- */
.leader-card { margin-top: 20px; transition: opacity 0.2s; }
.leader-card.is-stale { opacity: 0.55; }
.lb-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; }
.lb-head h2 { margin-bottom: 2px; }
.lb-head .hint { margin: 0; }

.lb-controls { display: flex; gap: 8px; flex-wrap: wrap; }
.seg-count { color: var(--muted); font-weight: 400; font-variant-numeric: tabular-nums; }
.lb-name-row { display: flex; align-items: center; gap: 6px; min-width: 0; }
.pf-badge {
  flex: none; font-size: 10px; font-weight: 700; letter-spacing: 0.03em; line-height: 1;
  padding: 3px 4px; border-radius: 4px; border: 1px solid var(--border); color: var(--muted);
}
.pf-yt { color: var(--accent); border-color: var(--accent); }
.pf-tt { color: var(--text); border-color: var(--text); }
.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 9px; padding: 2px; gap: 2px; }
.seg {
  font: inherit; font-size: 13px; border: 0; background: none; color: var(--muted);
  padding: 6px 12px; border-radius: 7px; cursor: pointer; white-space: nowrap;
}
.seg[aria-pressed="true"] { background: var(--bg); color: var(--text); font-weight: 600; }

.podium {
  display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px;
  margin: 16px 0 6px; align-items: end;
}
.podium-card {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 16px;
  display: flex; flex-direction: column; gap: 10px; min-width: 0; background: var(--surface);
}
.podium-card.place-1 { order: 2; border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent), 0 10px 30px rgba(0, 0, 0, 0.18); padding-top: 22px; }
.podium-card.place-2 { order: 1; }
.podium-card.place-3 { order: 3; }
@media (max-width: 860px) {
  .podium { grid-template-columns: minmax(0, 1fr); }
  .podium-card.place-1, .podium-card.place-2, .podium-card.place-3 { order: 0; }
}
.podium-top { display: flex; align-items: center; gap: 6px; }
.podium-rank { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.place-1 .podium-rank { font-size: 24px; }
.podium-trophy { font-size: 18px; }
.podium-who { display: flex; align-items: center; gap: 10px; min-width: 0; }
.podium-names { display: flex; flex-direction: column; min-width: 0; }
.lb-name {
  font-size: 15px; font-weight: 600; color: var(--text); padding: 0; text-align: left;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%;
}
.lb-name:hover { color: var(--accent); text-decoration: underline; }
.lb-handle { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-avatar { border-radius: 50%; object-fit: cover; flex: none; background: var(--bg); }
.lb-avatar-md { width: 40px; height: 40px; }
.lb-avatar-sm { width: 32px; height: 32px; }
.lb-initial {
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: 600; color: var(--muted); border: 1px solid var(--border);
}
.podium-value { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.podium-number { font-size: 34px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.1; }
.place-1 .podium-number { font-size: 40px; }
.podium-unit { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.podium-facts { font-size: 12px; color: var(--muted); }
.podium-trend { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; }
.podium-trend .lb-share { text-align: left; }
.lb-share { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; text-align: right; cursor: help; }
.lb-trend { display: block; flex: none; }
@media (max-width: 560px) { .lb-row .lb-trend { display: none; } }
.podium-best {
  display: flex; flex-direction: column; gap: 2px; border-top: 1px solid var(--border);
  padding-top: 10px; color: inherit; text-decoration: none; min-width: 0;
}
.podium-best:hover .podium-best-title { color: var(--accent); }
.podium-best-title { font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.podium-best-meta { font-size: 12px; color: var(--muted); }
.podium-best-empty { font-size: 13px; color: var(--muted); }

.lb-list { list-style: none; margin: 10px 0 0; padding: 0; }
.lb-row {
  display: grid; grid-template-columns: 44px 32px minmax(0, 1fr) 96px auto; align-items: center; gap: 12px;
  padding: 10px 4px; border-bottom: 1px solid var(--border);
}
.lb-row:last-child { border-bottom: 0; }
.lb-rank { font-weight: 700; color: var(--muted); font-variant-numeric: tabular-nums; }
.lb-names { display: flex; flex-direction: column; min-width: 0; }
.lb-value { display: flex; flex-direction: column; align-items: flex-end; }
.lb-number { font-weight: 700; font-variant-numeric: tabular-nums; }
.lb-more { margin-top: 8px; }
@media (max-width: 560px) {
  .lb-row { grid-template-columns: 36px 32px minmax(0, 1fr) auto; }
  .lb-row .sparkline { display: none; }
}
