:root {
  color-scheme: dark;
  --bg: #13161a;
  --panel: rgba(16, 20, 26, 0.72);
  --panel-2: rgba(26, 31, 38, 0.86);
  --ink: #eef3f8;
  --muted: #8b95a6;
  --line: rgba(120, 140, 170, 0.13);
  --line-2: rgba(120, 140, 170, 0.26);
  --accent: #5d9cec;
  --accent-2: #89b9ff;
  --good: #68d391;
  --warn: #f6c85a;
  --danger: #ff6b6b;
  --radius: 8px;
}

* { box-sizing: border-box; }
html, body { min-height: 100%; background: var(--bg); }
body {
  margin: 0;
  color: var(--ink);
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-feature-settings: "cv02", "cv03", "cv04", "cv11";
  -webkit-font-smoothing: antialiased;
}
body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(ellipse 60% 50% at 15% 0%, rgba(93, 156, 236, 0.12), transparent 58%),
    linear-gradient(180deg, #171c22, #11151a 50%, #0e1116);
}
button, input, select, textarea { font: inherit; }
button, input, select, textarea {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #11161c;
  color: var(--ink);
}
button {
  min-height: 32px;
  padding: 0 12px;
  cursor: pointer;
  font-weight: 700;
}
button:hover { border-color: rgba(106, 167, 255, 0.45); }
input, select, textarea { min-height: 34px; padding: 0 10px; outline: none; }
textarea { min-height: 80px; padding-top: 8px; resize: vertical; }
h1, h2, h3, p { margin: 0; }
svg {
  width: 17px;
  height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: 0 0 auto;
}
.hidden { display: none !important; }
.primary { background: #1d5fa8; border-color: #2d7ed2; }
.danger { border-color: rgba(255, 107, 107, 0.35); color: #ffd3d3; }

.appShell {
  position: relative;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  min-height: 100vh;
  background: rgba(12, 16, 20, 0.65);
}
.sideNav {
  position: sticky;
  top: 0;
  z-index: 20;
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 2px;
  padding: 12px 10px 10px;
  min-width: 64px;
  border-right: 1px solid var(--line);
  background: rgba(16, 20, 26, 0.88);
  backdrop-filter: blur(24px);
}
.brandIcon,
.brandMark {
  position: relative;
  flex: 0 0 auto;
  border-radius: var(--radius);
  background: linear-gradient(145deg, rgba(93, 156, 236, 0.2), rgba(30, 38, 48, 0.9));
  border: 1px solid rgba(93, 156, 236, 0.25);
  box-shadow: 0 4px 20px rgba(93, 156, 236, 0.1);
}
.brandIcon {
  width: 36px;
  height: 36px;
  margin: 0 auto 10px;
}
.brandMark {
  width: 34px;
  height: 34px;
}
.brandIcon::before,
.brandMark::before {
  content: "";
  position: absolute;
  inset: 8px;
  border: 2px solid var(--accent-2);
  border-radius: 50%;
  box-shadow: 0 0 18px rgba(93, 156, 236, 0.35), inset 0 0 18px rgba(93, 156, 236, 0.06);
}
.brandIcon::after,
.brandMark::after {
  content: "";
  position: absolute;
  width: 5px;
  height: 5px;
  top: 14px;
  left: 14px;
  border-radius: 50%;
  background: var(--accent-2);
  box-shadow: 0 0 10px var(--accent-2);
}
.navItem {
  display: grid;
  place-items: center;
  min-height: 46px;
  padding: 6px 8px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: 0.02em;
  white-space: nowrap;
  transition: background 0.2s ease, color 0.2s ease;
}
.navItem:hover {
  background: rgba(93, 156, 236, 0.08);
  color: var(--ink);
}
.navItem span {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  margin-bottom: 2px;
  border-radius: var(--radius);
  background: rgba(120, 140, 170, 0.06);
  color: currentColor;
}
.navItem.active {
  color: var(--accent-2);
  background: rgba(93, 156, 236, 0.15);
}
.navItem.active span {
  background: rgba(93, 156, 236, 0.16);
  box-shadow: inset 0 0 0 1px rgba(93, 156, 236, 0.18);
}

.mainPane {
  min-width: 0;
  padding: 16px 24px 32px;
}
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 14px;
}
.brandBlock {
  display: flex;
  align-items: center;
  gap: 10px;
}
.topbar h1 { font-size: 18px; letter-spacing: 0; }
.topbar p { margin-top: 3px; color: var(--muted); font-size: 12px; }
.topActions { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 32px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #11161c;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}
.toggle input { min-height: auto; accent-color: var(--accent); }

.metricsGrid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}
.metricsGrid div {
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: linear-gradient(145deg, rgba(93, 156, 236, 0.06), transparent 70%), var(--panel);
}
.metricsGrid span { display: block; color: var(--muted); font-size: 10px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.04em; }
.metricsGrid strong { display: block; margin-top: 6px; font-size: 22px; }

.actionButton {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 32px;
  padding: 0 12px;
  border-color: rgba(93, 156, 236, 0.5);
  background: linear-gradient(145deg, #5d9cec, #3d7bc8);
  color: #fff;
  font-size: 13px;
}
.squareButton {
  width: 32px;
  min-width: 32px;
  height: 32px;
  display: inline-grid;
  place-items: center;
  padding: 0;
}
.dangerSquare:hover {
  color: var(--danger);
  border-color: rgba(255, 107, 107, 0.35);
  background: rgba(255, 107, 107, 0.08);
}

.view { display: none; }
.view.active { display: block; }
.tools, .sectionHead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}
.tools input { flex: 1; min-width: 160px; }
.tools select { min-width: 110px; }
.sectionHead h2 { font-size: 16px; }
.sectionHead p { margin-top: 3px; color: var(--muted); font-size: 12px; }

.videoGrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 10px;
}
.videoCard {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  transition: border-color 0.18s ease, transform 0.18s ease;
}
.videoCard:hover {
  border-color: rgba(93, 156, 236, 0.28);
  transform: translateY(-1px);
}
.thumb {
  position: relative;
  aspect-ratio: 16 / 9;
  background: #0d1116;
}
.thumb img { width: 100%; height: 100%; display: block; object-fit: cover; }
.viewsBadge {
  position: absolute;
  right: 6px;
  bottom: 6px;
  padding: 3px 6px;
  border-radius: 5px;
  background: rgba(0, 0, 0, 0.82);
  font-size: 11px;
  font-weight: 800;
}
.videoBody { padding: 10px; }
.videoBody h3 {
  min-height: 36px;
  font-size: 13px;
  line-height: 1.35;
  cursor: pointer;
}
.meta {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-top: 6px;
  color: var(--muted);
  font-size: 11px;
}
.cardActions { display: flex; gap: 6px; margin-top: 10px; }
.cardActions a, .cardActions button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 28px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--ink);
  background: #11161c;
  text-decoration: none;
  font-size: 11px;
  font-weight: 800;
}

.ruleForm {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
}
.ruleForm label { display: grid; gap: 4px; color: var(--muted); font-size: 11px; font-weight: 800; }
.ruleForm .wide, .formActions { grid-column: 1 / -1; }
.formActions { display: flex; justify-content: flex-end; gap: 8px; }
.rulesList, .tableList {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--panel);
}
.ruleRow, .tableRow {
  display: grid;
  grid-template-columns: minmax(120px, 1fr) minmax(160px, 2fr) 100px 120px;
  gap: 10px;
  align-items: center;
  min-height: 46px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
}
.ruleRow:last-child, .tableRow:last-child { border-bottom: 0; }
.ruleRow span, .tableRow span { min-width: 0; overflow: hidden; color: var(--muted); text-overflow: ellipsis; white-space: nowrap; }
.rowActions { display: flex; justify-content: flex-end; gap: 4px; }
.rowActions button { min-height: 26px; padding: 0 8px; font-size: 11px; }

.empty {
  display: grid;
  place-items: center;
  min-height: 180px;
  color: var(--muted);
  border: 1px dashed var(--line);
  border-radius: var(--radius);
}

.durationBadge {
  position: absolute;
  left: 6px;
  bottom: 6px;
  padding: 2px 5px;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.82);
  font-size: 10px;
  font-weight: 800;
  line-height: 1.2;
}

.statusBadge {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 800;
  line-height: 1.2;
  z-index: 2;
}
.statusBadge.new {
  background: rgba(93, 156, 236, 0.85);
  color: #fff;
}
.statusBadge.downloaded {
  background: rgba(104, 211, 145, 0.85);
  color: #fff;
}

.downloadBtn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-height: 28px;
  padding: 0 8px;
  border: 1px solid rgba(104, 211, 145, 0.4);
  border-radius: var(--radius);
  color: #68d391;
  background: rgba(104, 211, 145, 0.08);
  text-decoration: none;
  font-size: 11px;
  font-weight: 800;
  cursor: pointer;
}
.downloadBtn:hover {
  background: rgba(104, 211, 145, 0.18);
  border-color: rgba(104, 211, 145, 0.6);
}

.dangerBtn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-height: 28px;
  padding: 0 8px;
  border: 1px solid rgba(255, 107, 107, 0.3);
  border-radius: var(--radius);
  color: #ff6b6b;
  background: rgba(255, 107, 107, 0.06);
  font-size: 11px;
  font-weight: 800;
  cursor: pointer;
}
.dangerBtn:hover {
  background: rgba(255, 107, 107, 0.16);
  border-color: rgba(255, 107, 107, 0.5);
}

.loadMoreWrap {
  display: flex;
  justify-content: center;
  margin-top: 12px;
}
.loadMoreWrap.hidden { display: none; }

.toastContainer {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 999;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  max-width: 420px;
  width: 90vw;
  pointer-events: none;
}
.toast {
  pointer-events: auto;
  padding: 10px 18px;
  border-radius: var(--radius);
  background: #1a1f26;
  border: 1px solid var(--line);
  color: var(--ink);
  font-size: 13px;
  font-weight: 600;
  text-align: center;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.5);
  transform: translateY(-16px);
  opacity: 0;
  transition: transform 0.3s ease, opacity 0.3s ease;
}
.toast.show {
  transform: translateY(0);
  opacity: 1;
}

.errorText { color: var(--danger); }
.okText { color: var(--good); }

.logLevel {
  display: inline-block;
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
}
.logLevel_info { background: rgba(93, 156, 236, 0.15); color: var(--accent-2); }
.logLevel_warn { background: rgba(246, 200, 90, 0.15); color: var(--warn); }
.logLevel_error { background: rgba(255, 107, 107, 0.15); color: var(--danger); }

.loadingDot {
  display: none;
  width: 14px;
  height: 14px;
  flex: 0 0 14px;
  border: 2px solid var(--accent-2);
  border-top-color: transparent;
  border-radius: 50%;
  animation: vp-spin 0.7s linear infinite;
}
body.loading .loadingDot {
  display: block;
}
@keyframes vp-spin { to { transform: rotate(360deg); } }

.ruleRow {
  grid-template-columns: minmax(120px, 1fr) minmax(120px, 2fr) 100px 70px 100px;
}
.ruleKeywords { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ruleMeta { color: var(--muted); font-size: 11px; }
.ruleCount { font-size: 12px; font-weight: 700; }

.modal {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
}
.backdrop {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
  border-radius: 0;
  background: rgba(0, 0, 0, 0.72);
}
.dialog {
  position: relative;
  width: min(780px, calc(100vw - 24px));
  max-height: calc(100vh - 24px);
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #12171d;
}
.dialog header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 44px;
  padding: 0 12px;
  border-bottom: 1px solid var(--line);
}
.dialog h3 { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.modalBody { max-height: calc(100vh - 80px); overflow: auto; padding: 14px; }
.modalBody img { width: min(380px, 100%); border-radius: var(--radius); }
.detailGrid { display: grid; grid-template-columns: 110px 1fr; gap: 8px; margin-top: 12px; font-size: 13px; }
.detailGrid dt { color: var(--muted); font-size: 11px; font-weight: 800; }
.detailGrid dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.description {
  margin-top: 12px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #0d1116;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  color: #d6dee8;
  line-height: 1.5;
}

/* ── Downloads Tab ── */

.downloadsGrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 10px;
}
.downloadCard {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  transition: border-color 0.18s ease, transform 0.18s ease;
}
.downloadCard:hover {
  border-color: rgba(93, 156, 236, 0.28);
  transform: translateY(-1px);
}
.downloadCard .thumb {
  position: relative;
  aspect-ratio: 16 / 9;
  background: #0d1116;
}
.downloadCard .thumb img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}

.dlBadge {
  position: absolute;
  top: 6px;
  right: 6px;
  padding: 3px 6px;
  border-radius: 5px;
  font-size: 10px;
  font-weight: 800;
  line-height: 1.2;
  z-index: 2;
  max-width: 70%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.dlBadge.downloading {
  background: rgba(93, 156, 236, 0.85);
  color: #fff;
  animation: pulse-bg 1.5s ease-in-out infinite;
}
.dlBadge.completed {
  background: rgba(104, 211, 145, 0.85);
  color: #fff;
}
.dlBadge.error {
  background: rgba(255, 107, 107, 0.85);
  color: #fff;
}
.dlBadge.idle {
  background: rgba(139, 149, 166, 0.85);
  color: #fff;
}

@keyframes pulse-bg {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

.dlErrorText {
  margin-top: 8px;
  padding: 6px 8px;
  border-radius: 4px;
  background: rgba(255, 107, 107, 0.1);
  color: #ff6b6b;
  font-size: 11px;
  line-height: 1.3;
  word-break: break-all;
}

.playBtn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-height: 28px;
  padding: 0 8px;
  border: 1px solid rgba(104, 211, 145, 0.5);
  border-radius: var(--radius);
  color: #68d391;
  background: rgba(104, 211, 145, 0.12);
  text-decoration: none;
  font-size: 11px;
  font-weight: 800;
  cursor: pointer;
}
.playBtn:hover {
  background: rgba(104, 211, 145, 0.22);
  border-color: rgba(104, 211, 145, 0.7);
}

.statusBadge.completed {
  background: rgba(104, 211, 145, 0.85);
  color: #fff;
}
.statusBadge.downloading {
  background: rgba(93, 156, 236, 0.85);
  color: #fff;
  animation: pulse-bg 1.5s ease-in-out infinite;
}

.downloadsGrid.hasActive .dlBadge.downloading {
  /* subtle extra attention for active downloads */
  box-shadow: 0 0 8px rgba(93, 156, 236, 0.4);
}

@media (max-width: 720px) {
  .appShell { grid-template-columns: 1fr; }
  .sideNav {
    position: static;
    height: auto;
    min-height: 0;
    flex-direction: row;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
  .brandIcon { display: none; }
  .mainPane { padding: 12px; }
  .topbar, .tools, .sectionHead { align-items: stretch; flex-direction: column; }
  .brandBlock { align-items: flex-start; }
  .metricsGrid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .ruleForm { grid-template-columns: 1fr; }
  .ruleRow { grid-template-columns: 1fr 1fr; grid-template-rows: auto auto; gap: 6px; }
  .ruleRow > strong { grid-column: 1 / -1; }
  .ruleRow > .ruleCount { text-align: right; }
  .ruleRow > .rowActions { grid-column: 1 / -1; justify-content: flex-start; }
  .tableRow { grid-template-columns: 1fr; }
  .rowActions { justify-content: flex-start; }
}
