:root {
  --bg: #161616;
  --surface: #131313;
  --surface-low: #1c1b1b;
  --surface-container: #262626;
  --surface-high: #2a2a2a;
  --surface-highest: #353534;
  --border-subtle: #393939;
  --border-strong: #525252;
  --outline: #8c909f;
  --outline-variant: #424754;
  --on: #e5e2e1;
  --on-variant: #c2c6d6;
  --helper: #8d8d8d;
  --primary: #6699FF;
  --primary-soft: #b3ccff;
  --on-primary: #002e6b;
  --error: #fa4d56;
  --success: #24a148;
  --warn: #f1c21b;
  --input-border: #8d8d8d;
  --surface-container-high: #2a2a2a;
  --shadow: 0 4px 8px rgba(0, 0, 0, 0.5);
  --btn-bg: #0066FF;
  --btn-fg: #FFFFFF;
}

:root[data-theme="light"] {
  --bg: #ffffff;
  --surface: #f4f4f4;
  --surface-low: #f4f4f4;
  --surface-container: #e8e8e8;
  --surface-high: #e8e8e8;
  --surface-highest: #c6c6c6;
  --border-subtle: #e0e0e0;
  --border-strong: #8d8d8d;
  --outline: #525252;
  --outline-variant: #e0e0e0;
  --on: #161616;
  --on-variant: #525252;
  --helper: #6f6f6f;
  --primary: #0066FF;
  --primary-soft: #cce0ff;
  --on-primary: #001141;
  --error: #da1e28;
  --success: #198038;
  --warn: #f1c21b;
  --input-border: #8d8d8d;
  --surface-container-high: #e8e8e8;
  --shadow: 0 4px 8px rgba(0, 0, 0, 0.12);
  --btn-bg: #0066FF;
  --btn-fg: #FFFFFF;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: "IBM Plex Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--on);
  min-height: 100vh;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}
body.modal-open { overflow: hidden; }

.mat {
  font-family: "Material Symbols Outlined";
  font-weight: normal;
  line-height: 1;
  user-select: none;
}

button { font-family: inherit; }

.hidden { display: none !important; }

/* ---------- layout ---------- */
.app { min-height: 100vh; }
.main { min-width: 0; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--outline-variant);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 24px;
  z-index: 50;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: 0.3px;
  margin: 0;
  cursor: pointer;
}
.topbar-brand .mat { font-size: 20px; color: var(--primary); }
.top-spacer { flex: 1; }

.chip-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 32px;
  padding: 0 12px;
  background: var(--btn-bg);
  border: 1px solid var(--border-subtle);
  border-radius: 999px;
  color: var(--btn-fg);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: filter 0.15s;
}
.chip-btn:hover { filter: brightness(1.08); }
.chip-btn .mat { font-size: 16px; }

.acct-wrap { position: relative; }
.acct-state {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: 280px;
  background: var(--surface-container);
  border: 1px solid var(--border-subtle);
  border-radius: 4px;
  padding: 10px 12px;
  font-size: 12px;
  line-height: 1.5;
  color: var(--on-variant);
  display: none;
  z-index: 60;
}
.acct-state.show { display: block; }

.theme-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  background: none;
  border: none;
  border-radius: 4px;
  color: var(--on-variant);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.theme-btn:hover, .theme-btn:focus-visible { background: var(--surface-container); color: var(--on); }
.theme-btn .mat { font-size: 20px; }

.health { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--helper); white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); }
.dot.ok { background: var(--success); }
.dot.bad { background: var(--error); }

/* ---------- hero ---------- */
.content { padding: 24px; max-width: 900px; margin: 0 auto; }

.hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 9vh 0 8px;
}

.hero-brand {
  margin: 0;
  font-size: 44px;
  font-weight: 600;
  letter-spacing: -0.02em;
}
.hero-sub { margin: 8px 0 32px; color: var(--helper); font-size: 14px; }

.home-actions {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-top: 22px;
  flex-wrap: wrap;
}

.g-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 30px;
  padding: 0 15px;
  background: var(--btn-bg);
  border: none;
  border-radius: 999px;
  color: var(--btn-fg);
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: filter 0.15s, box-shadow 0.15s;
}
.g-btn .mat { font-size: 18px; }
.g-btn:hover { filter: brightness(1.08); box-shadow: var(--shadow); }

.back-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 28px;
  align-self: flex-start;
  background: none;
  border: none;
  color: var(--on-variant);
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: color 0.15s;
}
.back-btn:hover { color: var(--on); }
.back-btn .mat { font-size: 18px; }

.sub-brand { margin: 0; font-size: 34px; font-weight: 600; letter-spacing: -0.02em; }

.error { color: var(--error); margin: 16px 0 0; font-size: 13px; word-break: break-word; }

.query-card {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 48px;
  width: 100%;
  max-width: 640px;
  padding: 0 6px 0 16px;
  background: var(--surface-low);
  border: 1px solid var(--outline-variant);
  border-radius: 24px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.query-card:focus-within, .query-card:hover {
  border-color: var(--border-strong);
  box-shadow: var(--shadow);
}
.query-card > .mat { color: var(--on-variant); font-size: 22px; }
.query-card input {
  flex: 1;
  min-width: 0;
  background: transparent;
  border: none;
  border-radius: 0;
  color: var(--on);
  font-family: inherit;
  font-size: 16px;
  padding: 0;
  outline: none;
}
.query-card input::placeholder { color: var(--on-variant); }

.dl-cta {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 30px;
  padding: 0 13px;
  background: var(--btn-bg);
  color: var(--btn-fg);
  border: none;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: filter 0.15s;
}
.dl-cta .mat { font-size: 18px; }
.dl-cta:hover { filter: brightness(1.12); }
.dl-cta:disabled { opacity: 0.6; cursor: default; }

.field-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  background: transparent;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  color: var(--on-variant);
  transition: background 0.15s, color 0.15s;
  flex-shrink: 0;
}
.field-btn .mat { font-size: 18px; }
.field-btn:hover {
  background: var(--surface-container);
  color: var(--on);
}

/* ---------- platform chips ---------- */
.chips-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  max-width: 640px;
  margin: 16px auto 0;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 26px;
  padding: 0 12px;
  background: var(--btn-bg);
  color: var(--btn-fg);
  border: none;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: filter 0.15s;
}
.chip .logo { width: 15px; height: 15px; display: block; pointer-events: none; }
.chip .mat { font-size: 16px; color: var(--btn-fg); }
.chip:hover { filter: brightness(1.08); }
.chip:not(.active) {
  background: var(--surface-container);
  color: var(--on-variant);
}
.chip.active {
  background: var(--btn-bg);
  color: var(--btn-fg);
  font-weight: 600;
  box-shadow: inset 0 0 0 1.5px var(--primary);
}
.chip.active .mat { color: var(--btn-fg); }

/* ---------- link separator + direct-link field ---------- */
.link-sep {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  max-width: 640px;
  margin: 32px auto 20px;
  color: var(--helper);
  font-size: 12px;
  white-space: nowrap;
}
.link-sep::before, .link-sep::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border-subtle);
}
#extract-form { margin-bottom: 12px; }
#result { width: 100%; max-width: 640px; margin-top: 4px; }

/* ---------- masonry grid ---------- */
.masonry { columns: 1; column-gap: 16px; }
@media (min-width: 640px) { .masonry { columns: 2; } }
@media (min-width: 1024px) { .masonry { columns: 3; } }
@media (min-width: 1280px) { .masonry { columns: 4; } }

.vid-card {
  break-inside: avoid;
  margin-bottom: 16px;
  background: var(--surface-low);
  border: 1px solid var(--outline-variant);
  border-radius: 4px;
  overflow: hidden;
  cursor: pointer;
  transition: box-shadow 0.2s, border-color 0.2s;
}
.vid-card:hover, .vid-card:focus-visible {
  border-color: var(--outline);
  box-shadow: var(--shadow);
  outline: none;
}

.thumb-wrap { position: relative; }
.thumb { display: block; width: 100%; aspect-ratio: 16 / 9; object-fit: cover; background: var(--surface-container); }
.thumb.fallback {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: 600;
  color: var(--on-variant);
}
.thumb.big { width: 180px; flex-shrink: 0; border: 1px solid var(--outline-variant); border-radius: 4px; }

.prov-badge {
  position: absolute;
  top: 8px;
  left: 8px;
  background: var(--surface);
  color: var(--badge-color, var(--primary));
  border: 1px solid var(--outline-variant);
  font-size: 10px;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 2px;
  letter-spacing: 0.02em;
}
.dur-badge {
  position: absolute;
  right: 8px;
  bottom: 8px;
  background: var(--on);
  color: var(--surface);
  font-size: 10px;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 2px;
}
.bookmark {
  position: absolute;
  top: 8px;
  right: 8px;
  background: var(--surface);
  border: 1px solid var(--outline-variant);
  border-radius: 2px;
  color: var(--on);
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s;
}
.vid-card:hover .bookmark { opacity: 1; }
.bookmark .mat { font-size: 15px; }

.v-body { padding: 16px; border-top: 1px solid var(--outline-variant); }
.v-title {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.v-sub { margin: 8px 0 0; font-size: 12px; color: var(--on-variant); display: flex; flex-wrap: wrap; gap: 0 6px; }

/* ---------- result detail ---------- */
#result { margin-top: 20px; }

.meta-card { display: flex; gap: 16px; align-items: flex-start; background: var(--surface-low); border: 1px solid var(--outline-variant); border-radius: 4px; padding: 16px; }
.meta-body { min-width: 0; }
.meta-body h2 { margin: 0 0 8px; font-size: 16px; font-weight: 600; line-height: 1.4; word-break: break-word; }
.meta { display: block; color: var(--on-variant); font-size: 12px; margin: 2px 0; word-break: break-all; }

.opts { margin-top: 16px; display: flex; flex-direction: column; gap: 16px; }
.opt-group h3 {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--helper);
  margin: 0 0 8px;
}
.opt-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--surface-container);
  border: 1px solid var(--border-subtle);
  border-radius: 4px;
  padding: 12px 14px;
  margin-bottom: 8px;
}
.opt-info { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.opt-label { font-weight: 600; color: var(--on); }
.size { color: var(--helper); font-size: 12px; }
.dl-btn {
  background: var(--primary);
  color: #fff;
  text-decoration: none;
  flex-shrink: 0;
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 600;
  text-align: center;
  border-radius: 4px;
  transition: filter 0.15s;
}
.dl-btn:hover { filter: brightness(1.12); }

.opt-row-image {
  flex-wrap: wrap;
}
.img-preview {
  width: 100%;
  max-height: 320px;
  object-fit: contain;
  border-radius: 4px;
  background: var(--surface-container);
  cursor: pointer;
  transition: opacity 0.15s;
}
.img-preview:hover { opacity: 0.85; }

/* ---------- modal ---------- */
.modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; padding: 20px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.6); }
.modal-card {
  position: relative;
  width: min(640px, 100%);
  max-height: 90vh;
  overflow-y: auto;
  background: var(--surface-container);
  border: 1px solid var(--border-strong);
  border-radius: 4px;
  padding: 20px;
}
.modal-close {
  position: absolute;
  top: 10px;
  right: 10px;
  background: var(--surface-high);
  color: var(--on);
  width: 32px;
  height: 32px;
  padding: 0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
}
.modal-close .mat { font-size: 18px; }
.modal-close:hover { background: var(--surface-highest); }

/* ---------- download overlay ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(0, 0, 0, 0.72);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  color: var(--on);
  font-size: 14px;
  padding: 24px;
  text-align: center;
}
.overlay .spinner { width: 32px; height: 32px; border-width: 4px; }

#results-status {
  display: flex;
  justify-content: center;
}

.loading, .empty {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--helper);
  font-size: 14px;
  padding: 16px 0;
  text-align: center;
}

.spinner {
  width: 24px;
  height: 24px;
  border: 3px solid var(--border-subtle);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  display: inline-block;
  flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }

.bar { width: min(320px, 80vw); height: 6px; background: var(--outline-variant); border-radius: 2px; overflow: hidden; }
.bar > i { display: block; height: 100%; width: 0; background: var(--primary); transition: width 0.2s ease; }

.ghost {
  background: none;
  border: 1px solid var(--border-strong);
  color: var(--on);
  padding: 8px 18px;
  font-size: 13px;
  border-radius: 4px;
  cursor: pointer;
}
.ghost:hover { background: rgba(255, 255, 255, 0.08); }

.foot { color: var(--helper); text-align: center; font-size: 12px; margin: 40px 0 8px; }

/* ---------- responsive ---------- */
@media (max-width: 767px) {
  .topbar { padding: 0 16px; }
  .content { padding: 16px; }
  .hero { padding: 6vh 0 8px; }
  .hero-brand { font-size: 36px; }
  .meta-card { flex-direction: column; }
  .thumb.big { width: 100%; }
  .opt-row { flex-direction: column; align-items: stretch; }
  .dl-btn { text-align: center; }
  .acct-state { width: 220px; }
}

.update-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}
.update-dialog {
  background: var(--surface);
  border: 1px solid var(--border-subtle);
  border-radius: 16px;
  padding: 32px;
  max-width: 380px;
  width: 90%;
  text-align: center;
}
.update-dialog h2 {
  margin: 0 0 12px;
  font-size: 20px;
  color: var(--on);
}
.update-dialog p {
  margin: 0 0 24px;
  font-size: 14px;
  color: var(--on-variant);
  line-height: 1.5;
}
.update-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}
.update-confirm {
  background: var(--btn-bg);
  color: var(--btn-fg);
  border: none;
  border-radius: 8px;
  padding: 10px 24px;
  font-weight: 600;
  cursor: pointer;
  font-size: 14px;
}
.update-dismiss {
  background: transparent;
  color: var(--on-variant);
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  padding: 10px 24px;
  cursor: pointer;
  font-size: 14px;
}

html.hide-mode #view-link,
html.hide-mode .home-actions,
html.hide-mode #link-go {
  display: none !important;
}

/* content sections (trending + popular) */
.content-sections {
  margin: 40px auto 0;
  padding: 0 8px;
  max-width: 1200px;
}
.content-block {
  margin-bottom: 40px;
}
.content-header {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
}
.content-header h2 {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 18px;
  font-weight: 600;
  color: var(--on);
}
.content-header h2 .mat {
  font-size: 22px;
  color: var(--primary);
}
.period-tabs {
  display: flex;
  gap: 4px;
  background: var(--surface-container);
  border-radius: 8px;
  padding: 3px;
}
.period-tab {
  background: transparent;
  border: none;
  color: var(--on-variant);
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.15s;
}
.period-tab:hover {
  color: var(--on);
  background: var(--surface-high);
}
.period-tab.active {
  background: var(--primary);
  color: #fff;
}
.popular-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.popular-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--surface-container);
  color: var(--on);
  padding: 8px 14px;
  border-radius: 20px;
  text-decoration: none;
  font-size: 13px;
  border: 1px solid var(--border-subtle);
  transition: all 0.15s;
}
.popular-chip:hover {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.popular-chip:hover .pop-count {
  background: rgba(255,255,255,0.25);
  color: #fff;
}
.pop-count {
  background: var(--surface-high);
  color: var(--on-variant);
  padding: 1px 7px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
}

/* ---------- Stats Dashboard ---------- */
.stats-dashboard {
  padding: 0 24px 40px;
  max-width: 1200px;
  margin: 0 auto;
}
.stats-kpis {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 24px;
}
.kpi-card {
  background: var(--surface-container);
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  padding: 20px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}
.kpi-card .mat {
  font-size: 28px;
  color: var(--primary);
}
.kpi-value {
  font-size: 32px;
  font-weight: 700;
  color: var(--on);
  line-height: 1;
}
.kpi-label {
  font-size: 13px;
  color: var(--on-variant);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.stats-panels {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 16px;
}
.stats-panel {
  background: var(--surface-container);
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  padding: 20px;
}
.stats-panel h3 {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
  font-weight: 600;
  color: var(--on);
  margin: 0 0 16px;
}
.stats-panel h3 .mat {
  font-size: 20px;
  color: var(--primary);
}
.stats-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border-subtle);
}
.stats-row:last-child {
  border-bottom: none;
}
.stats-provider-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.stats-provider-name {
  flex: 1;
  font-size: 14px;
  color: var(--on);
}
.stats-provider-count {
  font-size: 14px;
  font-weight: 600;
  color: var(--on-variant);
  font-variant-numeric: tabular-nums;
}
.stats-table-wrap {
  overflow-x: auto;
}
.stats-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.stats-table th {
  text-align: left;
  padding: 8px 12px;
  color: var(--on-variant);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  border-bottom: 1px solid var(--border-subtle);
}
.stats-table td {
  padding: 10px 12px;
  color: var(--on);
  border-bottom: 1px solid var(--border-subtle);
  vertical-align: middle;
}
.stats-table tr:last-child td {
  border-bottom: none;
}
.stats-title {
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.stats-time {
  color: var(--on-variant);
  font-size: 12px;
  white-space: nowrap;
}
.stats-kind-icon {
  font-size: 16px;
  vertical-align: middle;
  margin-right: 4px;
  color: var(--primary);
}
.stats-loading,
.stats-empty,
.stats-error {
  color: var(--on-variant);
  font-size: 14px;
  text-align: center;
  padding: 24px 0;
}
.stats-error {
  color: var(--error);
}
@media (max-width: 768px) {
  .stats-kpis {
    grid-template-columns: repeat(2, 1fr);
  }
  .stats-panels {
    grid-template-columns: 1fr;
  }
  .stats-dashboard {
    padding: 0 16px 32px;
  }
}
@media (max-width: 480px) {
  .stats-kpis {
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }
  .kpi-value {
    font-size: 24px;
  }
}