:root {
  --bg: #f7f5f2;
  --surface: #ffffff;
  --surface-2: #efece7;
  --text: #17161a;
  --muted: #6b6772;
  --line: #e3dfd8;
  --accent: #ff5a36;
  --accent-ink: #ffffff;
  --chip: #ebe7e1;
  --chip-on: #17161a;
  --chip-on-ink: #ffffff;
  --danger: #b42318;
  --danger-bg: #fef3f2;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 8px 24px rgba(0, 0, 0, .06);
  --radius: 10px;
  --gap: 10px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f0e11;
    --surface: #18171b;
    --surface-2: #222026;
    --text: #f2f0f4;
    --muted: #a19ca8;
    --line: #2c2a31;
    --chip: #232127;
    --chip-on: #f2f0f4;
    --chip-on-ink: #111;
    --danger: #ff8a7a;
    --danger-bg: #2a1614;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: inherit; }
button, input, select { font: inherit; color: inherit; }
.wrap { width: 100%; max-width: 1200px; margin: 0 auto; padding: 0 16px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip { position: absolute; left: -9999px; }
.skip:focus { left: 16px; top: 8px; z-index: 10; background: var(--surface); padding: 6px 10px; border-radius: 6px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Header */
.top { position: sticky; top: 0; z-index: 5; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(10px); border-bottom: 1px solid var(--line); }
.top-inner { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding-top: 12px; padding-bottom: 12px; }
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.logo { flex: none; width: 40px; height: 40px; border-radius: 12px; background: var(--accent) url("favicon.svg") center/cover; }
h1 { margin: 0; font-size: 18px; line-height: 1.2; letter-spacing: -.01em; }
.sub { margin: 2px 0 0; color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tiktok-link { flex: none; font-size: 13px; text-decoration: none; padding: 8px 12px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface); }
.tiktok-link:hover { border-color: var(--muted); }

/* Controls */
.controls { display: grid; grid-template-columns: 1fr auto; gap: 10px; margin: 16px 0 8px; }
.search input, .sort select {
  width: 100%; height: 42px; padding: 0 14px; border-radius: var(--radius);
  border: 1px solid var(--line); background: var(--surface);
}
.search input::placeholder { color: var(--muted); }
.sort select { padding-right: 34px; cursor: pointer; }
.tags { grid-column: 1 / -1; display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: thin; }
.tag { flex: none; border: 0; cursor: pointer; padding: 6px 12px; border-radius: 999px; background: var(--chip); font-size: 13px; }
.tag[aria-pressed="true"] { background: var(--chip-on); color: var(--chip-on-ink); }
.tag .n { color: var(--muted); margin-left: 4px; font-variant-numeric: tabular-nums; }
.tag[aria-pressed="true"] .n { color: inherit; opacity: .7; }
.result-line { color: var(--muted); font-size: 13px; margin: 4px 0 10px; min-height: 1.5em; }

/* Grid */
.grid { list-style: none; margin: 0; padding: 0; display: grid; gap: 3px;
  grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (min-width: 640px) { .grid { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: var(--gap); } }
@media (min-width: 960px) { .grid { grid-template-columns: repeat(5, minmax(0, 1fr)); } }
@media (min-width: 1180px) { .grid { grid-template-columns: repeat(6, minmax(0, 1fr)); } }
.tile { position: relative; display: block; width: 100%; padding: 0; border: 0; cursor: pointer; overflow: hidden;
  aspect-ratio: 9 / 16; background: var(--surface-2); }
@media (min-width: 640px) { .tile { border-radius: var(--radius); } }
.tile img { width: 100%; height: 100%; object-fit: cover; display: block; transition: transform .25s ease; }
.tile:hover img { transform: scale(1.03); }
.tile::after { content: ""; position: absolute; inset: auto 0 0 0; height: 42%;
  background: linear-gradient(to top, rgba(0,0,0,.65), rgba(0,0,0,0)); pointer-events: none; }
.tile .views, .tile .dur { position: absolute; bottom: 6px; z-index: 1; color: #fff; font-size: 12px; font-weight: 600;
  font-variant-numeric: tabular-nums; text-shadow: 0 1px 2px rgba(0,0,0,.6); }
.tile .views { left: 8px; }
.tile .views::before { content: "▶ "; font-size: 10px; }
.tile .dur { right: 8px; font-weight: 500; opacity: .9; }
.tile .badge { position: absolute; top: 6px; left: 6px; z-index: 1; font-size: 11px; padding: 2px 6px; border-radius: 6px;
  background: rgba(0,0,0,.65); color: #fff; }
.tile .nocover { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: 12px; }
.more { height: 60px; }

/* Errors: loud and specific, with a copyable report */
.error { margin: 16px 0; padding: 16px; border-radius: var(--radius); background: var(--danger-bg);
  border: 1px solid color-mix(in srgb, var(--danger) 40%, transparent); }
.error h2 { margin: 0 0 6px; font-size: 16px; color: var(--danger); }
.error p { margin: 6px 0; }
.error ol { margin: 6px 0; padding-left: 20px; }
.error pre { white-space: pre-wrap; word-break: break-word; font-size: 12px; background: var(--surface); padding: 10px;
  border-radius: 8px; border: 1px solid var(--line); max-height: 260px; overflow: auto; }
.error button { margin-top: 6px; padding: 8px 14px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--surface); cursor: pointer; }

.foot { color: var(--muted); font-size: 12px; padding-top: 24px; padding-bottom: 32px; }
.foot p { margin: 2px 0; }

/* Player */
dialog#player { padding: 0; border: 0; background: transparent; max-width: none; max-height: none;
  width: 100%; height: 100%; margin: 0; color: var(--text); }
dialog#player::backdrop { background: rgba(8, 8, 10, .82); }
.p-shell { display: flex; flex-direction: column; height: 100%; max-width: 1000px; margin: 0 auto; }
.p-media { flex: 1 1 auto; min-height: 0; background: #000; position: relative; }
/* Pinned to the box so tall 9:16 videos never push past the screen; object-fit letterboxes. */
.p-media video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #000; display: block; }
.p-fallback { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; padding: 24px;
  color: #fff; background: #000 center/cover no-repeat; }
.p-fallback .box { background: rgba(0,0,0,.72); padding: 18px; border-radius: 12px; max-width: 360px; }
.p-fallback a { display: inline-block; margin-top: 10px; padding: 8px 14px; border-radius: 999px; background: var(--accent);
  color: var(--accent-ink); text-decoration: none; font-weight: 600; }
.p-info { flex: none; background: var(--surface); padding: 12px 16px 16px; max-height: 45%; overflow-y: auto; }
.p-bar { display: flex; align-items: center; gap: 6px; margin-bottom: 6px; }
.p-pos { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.icon-btn { width: 36px; height: 36px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface);
  cursor: pointer; font-size: 18px; line-height: 1; }
.icon-btn:disabled { opacity: .35; cursor: default; }
.icon-btn.close { margin-left: auto; font-size: 14px; }
.p-date { margin: 0; color: var(--muted); font-size: 13px; }
.p-caption { margin: 6px 0 10px; white-space: pre-wrap; word-break: break-word; }
.p-caption .ht { color: var(--accent); background: none; border: 0; padding: 0; cursor: pointer; font-weight: 600; }
.p-stats { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; margin: 0; }
.p-stats div { background: var(--surface-2); border-radius: 8px; padding: 8px 6px; text-align: center; }
.p-stats dt { font-size: 11px; color: var(--muted); }
.p-stats dd { margin: 0; font-weight: 700; font-variant-numeric: tabular-nums; }
.p-asof, .p-meta, .p-quality { margin: 8px 0 0; color: var(--muted); font-size: 12px; }
.p-tiktok { display: inline-block; margin-top: 10px; font-size: 13px; }
@media (min-width: 900px) {
  dialog#player { padding: 24px; }
  .p-shell { flex-direction: row; border-radius: 14px; overflow: hidden; box-shadow: var(--shadow); height: calc(100vh - 48px); }
  .p-info { width: 340px; max-height: none; padding: 16px 18px; }
}
@media (prefers-reduced-motion: reduce) { .tile img { transition: none; } }

/* Admin cost page */
.admin { padding-top: 16px; }
.hero { margin: 8px 0 16px; }
.hero .label { margin: 0; color: var(--muted); font-size: 13px; }
.hero .big { margin: 2px 0 4px; font-size: 44px; font-weight: 800; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin: 0 0 14px; overflow-x: auto; }
.card h2 { margin: 0 0 12px; font-size: 15px; }
.meter { margin-bottom: 12px; }
.meter-top { display: flex; justify-content: space-between; font-size: 14px; }
.meter .num { font-variant-numeric: tabular-nums; font-weight: 600; }
.bar { height: 8px; border-radius: 999px; background: var(--surface-2); overflow: hidden; margin: 4px 0; }
.bar span { display: block; height: 100%; background: #2f9e6e; border-radius: inherit; }
.bar.warn span { background: #d99a1b; }
.bar.over span { background: var(--danger); }
.meter p { margin: 0; }
.tbl { width: 100%; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
.tbl th, .tbl td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); }
.tbl td:not(:first-child), .tbl thead th:not(:first-child) { text-align: right; }
.tbl thead th { color: var(--muted); font-weight: 500; font-size: 12px; }
.tbl tfoot th { font-weight: 700; border-bottom: 0; }
.chart { width: 100%; height: auto; }
.chart path { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linejoin: round; }
.chart text { fill: var(--muted); font-size: 11px; }
figure { margin: 0 0 12px; }
@media (max-width: 360px) { .tiktok-link { display: none; } }
