:root {
  color-scheme: dark;
  --bg: #0c0d0f;
  --surface: #15161a;
  --surface-2: #1c1e23;
  --surface-3: #24262c;
  --border: #2a2c32;
  --border-strong: #383a41;
  --text: #f3f3f1;
  --text-2: #b4b5ae;
  --muted: #7d7e78;
  --accent: #3987e5;
  --accent-soft: rgba(57, 135, 229, 0.14);
  --up: #2fbf86;
  --down: #ef6b6b;
  --hot: #ff7a45;
  --hot-soft: rgba(255, 122, 69, 0.14);
  --bump: #a593ff;
  --bump-soft: rgba(165, 147, 255, 0.15);
  --cool: #7fb2e8;
  --cool-soft: rgba(127, 178, 232, 0.12);
  --grid: rgba(255, 255, 255, 0.06);
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --other: #5c5d58;
  --shadow: 0 1px 0 rgba(255, 255, 255, 0.03) inset, 0 8px 24px rgba(0, 0, 0, 0.25);
  --radius: 14px;
}
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f4f4f1;
  --surface: #ffffff;
  --surface-2: #f7f7f5;
  --surface-3: #eeeeea;
  --border: #e4e3de;
  --border-strong: #d3d2cc;
  --text: #121211;
  --text-2: #52514e;
  --muted: #85847e;
  --accent: #2a78d6;
  --accent-soft: rgba(42, 120, 214, 0.1);
  --up: #0f8a5a;
  --down: #d23c3c;
  --hot: #d9541e;
  --hot-soft: rgba(235, 104, 52, 0.12);
  --bump: #5a47c7;
  --bump-soft: rgba(74, 58, 167, 0.1);
  --cool: #2f6fb3;
  --cool-soft: rgba(42, 120, 214, 0.08);
  --grid: rgba(0, 0, 0, 0.06);
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
  --other: #b9b8b2;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 6px 20px rgba(0, 0, 0, 0.04);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  font-feature-settings: "tnum" 1, "cv11" 1;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}
a { color: inherit; text-decoration: none; }
button, input, select { font: inherit; color: inherit; }
img { display: block; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 1360px; margin: 0 auto; padding: 0 24px; height: 60px;
  display: flex; align-items: center; gap: 28px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 15px; letter-spacing: -0.01em; white-space: nowrap; }
.brand-mark {
  width: 28px; height: 28px; border-radius: 8px; display: grid; place-items: center;
  background: linear-gradient(135deg, #ff4d4d, #ff7a45); color: #fff;
}
.nav { display: flex; gap: 4px; }
.nav a { padding: 7px 12px; border-radius: 8px; color: var(--text-2); font-weight: 500; }
.nav a:hover { color: var(--text); background: var(--surface-2); }
.nav a.active { color: var(--text); background: var(--surface-3); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.status-pill {
  display: flex; align-items: center; gap: 8px; padding: 6px 12px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--border); color: var(--text-2); font-size: 12.5px; white-space: nowrap;
}
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--up); box-shadow: 0 0 0 3px color-mix(in srgb, var(--up) 22%, transparent); }
.status-dot.running { background: var(--accent); animation: pulse 1.2s infinite; }
.status-dot.error { background: var(--down); }
@keyframes pulse { 50% { opacity: .35; } }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  height: 34px; padding: 0 14px; border-radius: 9px; cursor: pointer; white-space: nowrap;
  background: var(--surface); border: 1px solid var(--border); font-weight: 500; font-size: 13px;
  transition: background .15s, border-color .15s;
}
.btn:hover { background: var(--surface-2); border-color: var(--border-strong); }
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--accent); border-color: transparent; color: #fff; }
.btn-primary:hover { background: color-mix(in srgb, var(--accent) 88%, #fff); border-color: transparent; }
.btn-danger { color: var(--down); }
.btn-danger:hover { background: color-mix(in srgb, var(--down) 10%, var(--surface)); border-color: color-mix(in srgb, var(--down) 40%, var(--border)); }
.btn-icon { width: 34px; padding: 0; }
.btn svg { width: 16px; height: 16px; }
.spin { animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- layout ---------- */
main { max-width: 1360px; margin: 0 auto; padding: 28px 24px 80px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 22px; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: 24px; letter-spacing: -0.02em; font-weight: 700; }
.page-head p { margin: 4px 0 0; color: var(--text-2); }
.grid { display: grid; gap: 16px; }
.kpis { grid-template-columns: repeat(5, minmax(0, 1fr)); margin-bottom: 16px; }
.two { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
.two-wide { grid-template-columns: minmax(0, 1.55fr) minmax(0, 1fr); }
.mt { margin-top: 16px; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); min-width: 0;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 18px 0; flex-wrap: wrap; }
.card-title { font-weight: 600; font-size: 14.5px; letter-spacing: -0.01em; }
.card-sub { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.card-body { padding: 14px 18px 18px; }
.chart-box { position: relative; height: 300px; }
.chart-box.tall { height: 340px; }
.chart-box.short { height: 240px; }

/* KPI tiles */
.kpi { padding: 16px 18px; }
.kpi-label { color: var(--text-2); font-size: 12.5px; font-weight: 500; display: flex; align-items: center; gap: 6px; }
.kpi-value { font-size: 27px; font-weight: 700; letter-spacing: -0.025em; margin-top: 6px; }
.kpi-foot { margin-top: 4px; font-size: 12.5px; color: var(--muted); display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.delta { font-weight: 600; }
.delta.up { color: var(--up); }
.delta.down { color: var(--down); }
.delta.flat { color: var(--muted); }

/* segmented control */
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 9px; padding: 2px; }
.seg button { border: 0; background: none; padding: 5px 11px; border-radius: 7px; cursor: pointer; color: var(--text-2); font-size: 12.5px; font-weight: 500; }
.seg button.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.2); }
:root[data-theme="light"] .seg button.active { box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.controls { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* legend */
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; padding: 0 18px; margin-top: 12px; font-size: 12.5px; color: var(--text-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

/* badges */
.badge {
  display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px; border-radius: 999px;
  font-size: 11.5px; font-weight: 600; white-space: nowrap; line-height: 18px;
  background: var(--surface-3); color: var(--text-2);
}
.badge.hot { background: var(--hot-soft); color: var(--hot); }
.badge.bump, .badge.spike { background: var(--bump-soft); color: var(--bump); }
.badge.revived { background: var(--hot-soft); color: var(--hot); }
.badge.cooling { background: var(--cool-soft); color: var(--cool); }
.badge.new { background: var(--accent-soft); color: var(--accent); }
.badge.short { background: color-mix(in srgb, var(--s5) 14%, transparent); color: var(--s5); }

/* lists */
.list { display: flex; flex-direction: column; }
.list-item { display: flex; align-items: center; gap: 12px; padding: 10px 18px; border-top: 1px solid var(--border); }
.list-item:first-child { border-top: 0; }
a.list-item:hover { background: var(--surface-2); }
.avatar { width: 34px; height: 34px; border-radius: 50%; object-fit: cover; background: var(--surface-3); flex: none; }
.avatar.lg { width: 72px; height: 72px; }
.avatar.sm { width: 22px; height: 22px; }
.grow { flex: 1; min-width: 0; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.title-line { font-weight: 600; }
.meta { color: var(--muted); font-size: 12.5px; }
.right { text-align: right; }
.empty { padding: 28px 18px; text-align: center; color: var(--muted); }

/* table */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 14px; text-align: left; white-space: nowrap; }
th {
  font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600;
  border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); user-select: none;
}
th.sortable { cursor: pointer; }
th.sortable:hover { color: var(--text-2); }
th.sorted { color: var(--text); }
th.num, td.num { text-align: right; }
tbody tr { border-top: 1px solid var(--border); }
tbody tr:first-child { border-top: 0; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: var(--surface-2); }
td .cell-channel { display: flex; align-items: center; gap: 10px; min-width: 200px; max-width: 320px; }
td.video-cell { min-width: 300px; max-width: 460px; white-space: normal; }
.video-row { display: flex; gap: 12px; align-items: center; }
.thumb { width: 96px; aspect-ratio: 16/9; border-radius: 7px; object-fit: cover; background: var(--surface-3); flex: none; }
.thumb.short { aspect-ratio: 9/16; width: 40px; }
.clamp2 { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; font-weight: 500; }
.spark { display: block; }
.color-chip { width: 8px; height: 8px; border-radius: 2px; display: inline-block; flex: none; }

/* video cards */
.video-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 14px; padding: 4px 18px 18px; }
.vcard { border: 1px solid var(--border); border-radius: 12px; overflow: hidden; background: var(--surface-2); cursor: pointer; transition: transform .15s, border-color .15s; }
.vcard:hover { transform: translateY(-2px); border-color: var(--border-strong); }
.vcard-thumb { position: relative; aspect-ratio: 16/9; background: var(--surface-3); }
.vcard-thumb img { width: 100%; height: 100%; object-fit: cover; }
.vcard-thumb .badge { position: absolute; top: 8px; left: 8px; backdrop-filter: blur(6px); background: rgba(0,0,0,.6); color: #fff; }
.vcard-thumb .rank { position: absolute; bottom: 8px; left: 8px; background: rgba(0,0,0,.7); color: #fff; font-weight: 700; font-size: 12px; border-radius: 6px; padding: 1px 7px; }
.vcard-body { padding: 10px 12px 12px; }
.vcard-gain { font-size: 18px; font-weight: 700; letter-spacing: -0.02em; margin-top: 6px; }
.vcard-gain small { font-size: 12px; color: var(--muted); font-weight: 500; }

/* channel header */
.ch-header { display: flex; gap: 18px; align-items: center; padding: 20px; flex-wrap: wrap; }
.ch-header h1 { margin: 0; font-size: 24px; letter-spacing: -0.02em; }
.ch-stats { display: flex; gap: 22px; margin-top: 8px; color: var(--text-2); flex-wrap: wrap; }
.ch-stats b { color: var(--text); }
.back { color: var(--text-2); display: inline-flex; gap: 6px; align-items: center; margin-bottom: 14px; font-weight: 500; }
.back:hover { color: var(--text); }

/* forms */
.add-form { display: flex; gap: 10px; padding: 18px; flex-wrap: wrap; }
.input {
  height: 40px; padding: 0 14px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2);
  outline: none; min-width: 0; transition: border-color .15s, box-shadow .15s;
}
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.add-form .input { flex: 1; min-width: 220px; }
.add-form .btn { height: 40px; }
select.input { padding-right: 30px; }
.form-msg { padding: 0 18px 16px; font-size: 13px; }
.form-msg.error { color: var(--down); }
.form-msg.ok { color: var(--up); }
.hint { color: var(--muted); font-size: 12.5px; }

.channel-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
.ccard { padding: 16px; display: flex; flex-direction: column; gap: 14px; }
.ccard-top { display: flex; gap: 12px; align-items: center; }
.ccard-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.ccard-stats div { background: var(--surface-2); border-radius: 9px; padding: 8px 10px; }
.ccard-stats .label { white-space: nowrap; font-size: 10.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.ccard-stats .val { font-weight: 700; font-size: 15px; margin-top: 2px; }
.ccard-actions { display: flex; gap: 8px; justify-content: space-between; align-items: center; }
.err-text { color: var(--down); font-size: 12px; }

/* modal */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 50; display: grid; place-items: center; padding: 16px; animation: fade .15s; }
.modal { width: min(820px, 100%); max-height: calc(100vh - 32px); overflow: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; box-shadow: 0 24px 60px rgba(0,0,0,.4); }
.modal-head { display: flex; gap: 14px; padding: 18px; align-items: flex-start; border-bottom: 1px solid var(--border); }
.modal-head .thumb { width: 160px; }
.modal-head .thumb.short { width: 60px; }
@keyframes fade { from { opacity: 0; } }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 60;
  background: var(--surface-3); border: 1px solid var(--border-strong); padding: 10px 16px; border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0,0,0,.3); font-weight: 500; max-width: calc(100% - 32px);
}
.toast.error { border-color: color-mix(in srgb, var(--down) 50%, var(--border)); }

.skeleton { background: linear-gradient(90deg, var(--surface-2), var(--surface-3), var(--surface-2)); background-size: 200% 100%; animation: shimmer 1.3s infinite; border-radius: 10px; }
@keyframes shimmer { to { background-position: -200% 0; } }

.usage-bar { height: 6px; border-radius: 3px; background: var(--surface-3); overflow: hidden; margin-top: 8px; }
.usage-bar > div { height: 100%; background: var(--accent); border-radius: 3px; }

.welcome { padding: 48px 24px; text-align: center; }
.welcome h2 { margin: 12px 0 6px; font-size: 22px; letter-spacing: -0.02em; }
.welcome p { color: var(--text-2); margin: 0 auto 20px; max-width: 460px; }
.welcome .add-form { max-width: 560px; margin: 0 auto; padding: 0; }

@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .two, .two-wide { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 760px) {
  .topbar-inner { padding: 0 16px; gap: 12px; height: auto; min-height: 56px; flex-wrap: wrap; padding-top: 8px; padding-bottom: 8px; }
  .nav { order: 3; width: 100%; overflow-x: auto; }
  .status-pill .status-text-long { display: none; }
  main { padding: 18px 16px 60px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi-value { font-size: 22px; }
  .chart-box { height: 240px; }
  .brand-name { display: none; }
}
@media (max-width: 760px) {
  #refresh-btn span { display: none; }
  #refresh-btn { width: 34px; padding: 0; }
  .topbar-right { gap: 6px; }
}
