:root {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1e222b;
  --border: #272c37;
  --text: #e6e9ef;
  --muted: #9aa3b2;
  --accent: #ff4d4d;
  --accent-dim: #3a1f24;
  --ok: #3ecf8e;
  --warn: #f5a524;
  --radius: 12px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f7f9; --surface: #fff; --surface-2: #f1f3f6; --border: #e2e5ea;
    --text: #14171c; --muted: #626b7a; --accent-dim: #ffe9e9;
  }
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
h1 { font-size: 20px; }
h2 { font-size: 15px; }
code { font-family: var(--mono); font-size: 0.88em; background: var(--surface-2);
       padding: 1px 5px; border-radius: 4px; }

/* ── header ──
   The bar spans the window so its rule and background do; its contents do not.
   `.header-inner` repeats <main>'s column exactly — same max-width, same 16px
   gutter — so the brand sits over the first card and the account over the last.
   Without it the three groups are pushed to the window's edges and, on a wide
   screen, the stats end up marooned in the middle of an empty bar. */
header { border-bottom: 1px solid var(--border); background: var(--surface); }
.header-inner {
  display: flex; justify-content: space-between; align-items: center; gap: 20px;
  flex-wrap: wrap;
  max-width: 980px; margin: 0 auto; padding: 14px 16px;
}
.brand { display: flex; align-items: center; gap: 11px; }
.logo {
  display: grid; place-items: center; width: 32px; height: 32px; flex: none;
  background: var(--accent); color: #fff; border-radius: 9px; font-size: 13px;
}
/* Stats and account are both "how is this instance doing"; the brand is identity.
   Grouping the two means one boundary in the bar rather than two. */
.header-status { display: flex; align-items: center; gap: 20px; flex-wrap: wrap; }
.header-stats { display: flex; gap: 22px; }
.stat { display: flex; flex-direction: column; align-items: flex-end; line-height: 1.2; }
.stat-value { font-size: 17px; font-weight: 650; font-variant-numeric: tabular-nums; }
.stat-label { font-size: 10.5px; color: var(--muted); text-transform: uppercase;
              letter-spacing: 0.06em; margin-top: 2px; }

/* ── banner ── */
.banner {
  margin: 0; padding: 11px 24px; font-size: 14px;
  background: var(--accent-dim); border-bottom: 1px solid var(--border);
}
.banner.ok { background: color-mix(in srgb, var(--ok) 16%, transparent); }
.banner.busy { background: color-mix(in srgb, var(--warn) 16%, transparent); }

/* ── tabs ── */
.tabs { border-bottom: 1px solid var(--border); background: var(--surface); }
/* Same column as the header and <main>: the active tab's underline starts where the
   brand does. `role="tablist"` moved to this element with the buttons. */
.tabs-inner { display: flex; gap: 2px; max-width: 980px; margin: 0 auto;
              padding: 0 16px; overflow-x: auto; }
.tab {
  /* border-radius: 0 matters — the global button radius curves the 2px underline
     into a hook at each end. */
  padding: 12px 15px; background: none; border: none; border-bottom: 2px solid transparent;
  border-radius: 0;
  color: var(--muted); font: inherit; font-size: 14px; cursor: pointer; white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }

/* ── sub-sections ──
   A second level inside one panel: a segmented control rather than a second row of
   underlines, so it never reads as the top-level tab bar repeated. The bar's right
   edge carries the panel's one whole-flow action. */
.subnav { display: flex; justify-content: space-between; align-items: center;
          gap: 12px; margin: 0 0 20px; flex-wrap: wrap; }
.subtabs { display: inline-flex; gap: 2px; padding: 3px; background: var(--surface-2);
           border: 1px solid var(--border); border-radius: 10px; }
.subtab {
  padding: 7px 13px; border: 1px solid transparent; border-radius: 7px; background: none;
  color: var(--muted); font: inherit; font-size: 13.5px; cursor: pointer; white-space: nowrap;
}
.subtab:hover { color: var(--text); }
.subtab.active { background: var(--surface); border-color: var(--border); color: var(--text); }
.subpanel { display: none; }
.subpanel.active { display: block; }
/* Last card in a sub-section: nothing follows it, so its bottom margin is dead space. */
.subpanel > .card:last-child { margin-bottom: 0; }

main { max-width: 980px; margin: 0 auto; padding: 24px 16px 64px; }
.panel { display: none; }
.panel.active { display: block; }

/* ── cards & lists ── */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 18px; margin-bottom: 22px;
}
.hint { color: var(--muted); font-size: 13px; margin: 6px 0 12px; }
.card > h2, .card > .card-head { margin-bottom: 14px; }
.card > h2 + .hint, .card > .card-head + .hint { margin-top: -8px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.row > input[type=text], .row > input[type=search] { flex: 1 1 260px; }
/* The one search bar carries the panel: give it the width the two boxes had. */
.search-row > input[type=search] { flex: 1 1 420px; font-size: 15px; }
/* `[hidden]` is a UA rule, so any author `display:` beats it. Every element we hide
   from JS needs its own hidden case. */
.toolbar[hidden] { display: none; }
.toolbar { display: flex; justify-content: space-between; align-items: center;
           gap: 12px; margin: 0 0 12px; flex-wrap: wrap; }
.count { color: var(--muted); font-weight: 400; }

input, textarea, select {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  border-radius: 9px; padding: 10px 12px; font: inherit; width: 100%;
}
textarea { resize: vertical; font-size: 14px; }
input:focus-visible, textarea:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent;
}
/* A mouse click should not leave a ring behind; keyboard navigation still shows one. */
button:focus { outline: none; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
label { font-size: 13px; color: var(--muted); display: flex; flex-direction: column; gap: 5px; }

button {
  border-radius: 9px; padding: 10px 15px; font: inherit; font-size: 14px;
  cursor: pointer; border: 1px solid transparent; white-space: nowrap;
}
button.primary { background: var(--accent); color: #fff; font-weight: 550; }
button.ghost { background: var(--surface); border-color: var(--border); color: var(--text); }
button.tiny { padding: 5px 10px; font-size: 12.5px; background: var(--surface-2);
              border-color: var(--border); color: var(--muted); }
button.tiny:hover { color: var(--text); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.danger:hover { border-color: var(--accent); color: var(--accent); }

.sub-hint { margin: 12px 0 0; }
.sub-hint a { color: var(--accent); text-decoration: none; }
.sub-hint a:hover { text-decoration: underline; }
.tab-count { font-size: 11px; padding: 1px 7px; margin-left: 4px; border-radius: 20px;
  background: var(--surface-2); color: var(--muted); font-variant-numeric: tabular-nums; }
.tab-count:empty { display: none; }
.prompt-actions { margin-top: 12px; }
.fixed-rules { margin: 0; padding-left: 18px; color: var(--muted); font-size: 13.5px; }
.fixed-rules li + li { margin-top: 7px; }
.fixed-rules b { color: var(--text); font-weight: 550; }
.opt.edited { color: var(--warn); }
.opt { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em;
       color: var(--muted); border: 1px solid var(--border); padding: 1px 6px;
       border-radius: 20px; margin-left: 6px; }

.list { display: flex; flex-direction: column; gap: 10px; }
.item {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px;
}
.item-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }
.item-title { font-weight: 570; }
.item-meta { color: var(--muted); font-size: 12.5px; margin-top: 3px; }
.item-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.empty { color: var(--muted); font-size: 14px; text-align: center; padding: 34px 16px;
         border: 1px dashed var(--border); border-radius: var(--radius); }

/* A channel's own uploads, revealed under it: a thumbnail grid in a box that scrolls
   rather than pushing the next channel off the screen. */
.videos { margin-top: 12px; padding-top: 13px; border-top: 1px solid var(--border);
  max-height: 428px; overflow-y: auto; overscroll-behavior: contain; }
.video-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
@media (max-width: 880px) { .video-grid { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 560px) { .video-grid { grid-template-columns: 1fr; } }

.tile { display: flex; flex-direction: column; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.thumb { position: relative; display: block; aspect-ratio: 16 / 9;
  background: var(--surface); }
.thumb img { display: block; width: 100%; height: 100%; object-fit: cover; }
/* A private, deleted or still-processing video has no thumbnail; show a quiet tile
   rather than a broken image. */
.thumb.no-thumb img { display: none; }
.thumb.no-thumb::after { content: "no thumbnail"; position: absolute; inset: 0;
  display: grid; place-items: center; color: var(--muted); font-size: 12px; }
.thumb-time { position: absolute; right: 6px; bottom: 6px; padding: 1px 6px;
  border-radius: 4px; background: rgba(0,0,0,.78); color: #fff; font-size: 11px;
  font-variant-numeric: tabular-nums; }
.tile-body { display: flex; flex-direction: column; gap: 7px; padding: 9px 10px 10px; }
.tile-body > span { display: block; }
label.tile .tile-title { color: var(--text); }
.tile-title { font-size: 13.5px; font-weight: 550; line-height: 1.35; color: var(--text);
  text-decoration: none; display: -webkit-box; -webkit-line-clamp: 2;
  -webkit-box-orient: vertical; overflow: hidden; }
.tile-title:hover { color: var(--accent); }
.tile .item-meta { margin-top: 0; }
.tile .item-actions { margin-top: auto; }

/* The queue keeps the one-line form: it is a work list, not a gallery. */
.video-main { min-width: 0; }
.video-title { font-weight: 520; color: var(--text); text-decoration: none;
  display: block; overflow-wrap: anywhere; }
.video-title:hover { color: var(--accent); }
.video-loading, .video-empty { color: var(--muted); font-size: 13px; padding: 14px 2px; }
button.reveal.open { color: var(--text); border-color: var(--accent); }

/* The manual flow's search form and pickable results. */
.field { margin-bottom: 14px; }
.field-label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.pills { display: flex; gap: 7px; flex-wrap: wrap; }
.pill { font-size: 12.5px; padding: 5px 12px; border-radius: 20px; background: var(--surface-2);
  border: 1px solid var(--border); color: var(--muted); }
.pill.on { border-color: var(--accent); color: var(--text); background: var(--accent-dim); }
.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text);
  font-size: 14px; cursor: pointer; margin-bottom: 14px; }
.check input { width: auto; }
.check.inline { margin: 0; color: var(--muted); font-size: 13px; }
.hint.inline { margin: 0; }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.inline-field { flex-direction: row; align-items: center; gap: 8px;
  color: var(--text); font-size: 14px; }
.inline-field input { width: auto; }
.inline-field input[type=number] { width: 78px; text-align: center; }
.row.actions { margin-top: 2px; }
#auto-interval-field[hidden], #auto-time-field[hidden] { display: none; }
.open-grid { margin-bottom: 26px; }

.tile.pickable { cursor: pointer; }
.tile.pickable:hover { border-color: var(--muted); }
.tile.picked { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.tile.dimmed { opacity: 0.55; }
.tile.dimmed:hover { opacity: 1; }
.pick { position: absolute; top: 8px; left: 8px; width: 18px; height: 18px; margin: 0;
  accent-color: var(--accent); cursor: pointer; }
.reason.match { color: var(--ok); font-style: normal; }

.grid-footer { margin-top: 12px; }
button.wide { width: 100%; justify-content: center; }

.chips { display: flex; gap: 7px; flex-wrap: wrap; margin-bottom: 14px; }
.chip { font-size: 12.5px; padding: 4px 11px; border-radius: 20px;
        background: var(--surface-2); border: 1px solid var(--border); color: var(--muted); }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 20px; border: 1px solid var(--border);
         color: var(--muted); text-transform: lowercase; }
.badge.completed { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, transparent); }
.badge.failed { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.badge.skipped, .badge.filtered { color: var(--muted); }
.badge.manual { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.badge.auto { color: var(--muted); }
.reason { color: var(--muted); font-size: 12.5px; margin-top: 2px; font-style: italic; }
.badge.pending, .badge.processing { color: var(--warn);
         border-color: color-mix(in srgb, var(--warn) 40%, transparent); }
.badge.custom { color: var(--warn); }
.err { color: var(--accent); font-size: 12.5px; margin-top: 6px; }

.modes { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.mode { flex: 1 1 150px; flex-direction: row; align-items: center; gap: 9px; cursor: pointer;
        padding: 10px 13px; border: 1px solid var(--border); border-radius: 9px;
        background: var(--surface-2); color: var(--text); font-size: 14px; }
.mode:has(input:checked) { border-color: var(--accent); background: var(--accent-dim); }
.mode input { width: auto; margin: 0; accent-color: var(--accent); }
.digest-time { margin-bottom: 16px; align-items: flex-end; }
.digest-time label { flex: 1 1 150px; }
.digest-time[hidden] { display: none; }

dl { display: grid; grid-template-columns: auto 1fr; gap: 7px 18px; margin: 0; font-size: 14px; }
dt { color: var(--muted); }
dd { margin: 0; font-family: var(--mono); font-size: 13px; word-break: break-all; }

/* ── modal ── */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: grid;
         place-items: center; padding: 20px; z-index: 50; }
.modal[hidden] { display: none; }
.modal-box { background: var(--surface); border: 1px solid var(--border);
             border-radius: var(--radius); padding: 22px; max-width: 680px; width: 100%;
             max-height: 84vh; overflow: auto; position: relative; }
.modal-close { position: absolute; top: 12px; right: 12px; background: none;
               border: none; color: var(--muted); font-size: 16px; }
/* Telegram HTML separates blocks with newlines, which HTML would collapse into one
   run-on paragraph. pre-wrap makes the preview match what actually arrives in chat. */
.preview { background: var(--surface-2); border: 1px solid var(--border);
           border-radius: 10px; padding: 15px; margin-top: 14px; font-size: 14px;
           line-height: 1.6; overflow-wrap: anywhere; white-space: pre-wrap; }
.preview a { color: var(--accent); }
.telegram-note { color: var(--muted); font-size: 12.5px; margin-top: 10px; }

/* The header stacks well before the rest of the page does: brand, stats and account
   measure ~810px side by side, so below that the row wraps of its own accord and
   lands somewhere nobody chose. Stack it deliberately instead — stats on their own
   row, then the account under a rule of its own. Its own breakpoint, because the
   cards below have no reason to reflow at the same width. */
@media (max-width: 840px) {
  .header-inner { padding: 12px 16px; }
  .header-status { width: 100%; gap: 12px; }
  /* Left-aligned, not spread: `space-between` across the full width would pull three
     short figures into three corners of an empty row. */
  .header-stats { flex: 1 1 100%; gap: 26px; }
  .stat { align-items: flex-start; }
  .account { flex: 1 1 100%; flex-wrap: wrap; padding-left: 0; padding-top: 12px;
             border-left: none; border-top: 1px solid var(--border); }
}

@media (max-width: 620px) {
  .tabs-inner { padding: 0 10px; }
  .item-head { flex-direction: column; }
}


/* ── account and the Telegram connection ─────────────────────────────────── */

/* The divider does the separating, so the account needs no pills of its own: a name,
   a dot for the connection, and the actions. Only the one that asks for something
   — Connect Telegram, shown exactly when there is no chat — keeps a button's
   border; Disconnect and Sign out are rare and recede until hovered. Three identical
   pills side by side gave a settings link the same weight as the call to action. */
.account {
  display: flex; align-items: center; gap: 10px;
  padding-left: 20px; border-left: 1px solid var(--border);
}
.account-name { font-weight: 600; font-size: 13.5px; }

/* Scoped: the generic `.chip` above is the pill used in the channel and account
   lists, and an unscoped redefinition here silently restyled all of them. */
.account .chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 0; border: none; background: none;
  font-size: 12.5px; color: var(--muted);
}
.account .chip::before {
  content: ''; width: 6px; height: 6px; border-radius: 50%; flex: none;
  background: var(--muted);
}
.account .chip.linked::before { background: var(--ok); }
.account .chip.unlinked::before { background: var(--warn); }

button.small { padding: 5px 10px; font-size: 12.5px; }
button.quiet { background: none; border-color: transparent; color: var(--muted);
               padding: 5px 4px; font-size: 13px; }
button.quiet:hover { color: var(--text); }

.connect-card {
  position: relative;
  margin: 1rem auto;
  max-width: 44rem;
  padding: 1.25rem 1.5rem;
  border: 1px solid var(--line, #d8dee4);
  border-radius: .6rem;
  background: var(--card, #fff);
}
.connect-card h2 { margin-top: 0; }
.connect-card ol { line-height: 1.9; }
.connect-card code {
  padding: .15rem .45rem;
  border-radius: .3rem;
  background: rgba(127, 127, 127, .14);
  user-select: all;
}
.connect-close {
  position: absolute;
  top: .5rem;
  right: .75rem;
  border: 0;
  background: none;
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
  opacity: .55;
}
.connect-close:hover { opacity: 1; }
.muted { opacity: .7; font-size: .85rem; }

.note {
  margin: .75rem 0 0;
  padding-top: .75rem;
  border-top: 1px solid var(--line, #d8dee4);
  line-height: 1.55;
  max-width: 62ch;
}
.note code {
  padding: .05rem .3rem;
  border-radius: .25rem;
  background: rgba(127, 127, 127, .14);
}

/* ── importing subscriptions ─────────────────────────────────────────────── */

.import-row { margin-top: .75rem; align-items: center; gap: .6rem; }
.sub-list {
  max-height: 24rem;
  overflow-y: auto;
  margin: .5rem 0 1rem;
  border: 1px solid var(--line, #d8dee4);
  border-radius: .4rem;
}
.sub-row {
  display: flex;
  align-items: center;
  gap: .6rem;
  padding: .45rem .75rem;
  border-bottom: 1px solid var(--line, #eceff2);
}
.sub-row:last-child { border-bottom: 0; }
.sub-row label { flex: 1; cursor: pointer; }
.sub-row.watched { opacity: .55; }
.sub-row .watched-tag { font-size: .72rem; opacity: .8; }

/* ── the login gate (public mode only) ─────────────────────────────────────
   A full-screen cover rather than a separate page: the console is one document,
   and `boot()` simply does not fetch anything while this is showing. */
.login {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 1.5rem;
  background: var(--bg, #f6f7f9);
}
/* See the note at the top: `display: grid` above beats the UA's `[hidden]` rule, and
   without this the gate covers the console in every mode, including single-owner. */
.login[hidden] { display: none; }
.login-box {
  max-width: 26rem;
  padding: 2rem;
  border: 1px solid var(--line, #d8dee4);
  border-radius: .6rem;
  background: var(--card, #fff);
  text-align: center;
}
.login-box h1 { margin: .4rem 0 .2rem; }
.login-box .logo { font-size: 1.8rem; }
.login-box p { line-height: 1.5; }
#login-widget { margin: 1.4rem 0 .4rem; min-height: 2.5rem; }
.login-error {
  padding: .6rem .8rem;
  border-radius: .4rem;
  background: rgba(200, 50, 50, .12);
  font-size: .9rem;
}

/* Two utilities that replace the last inline `style=` attributes, so the console needs
   no `style-src 'unsafe-inline'` in the CSP the reverse proxy sets (ops/Caddyfile). */
.sent { color: var(--ok); }
.row-spaced { margin-top: 12px; }

/* The approval queue (migration 017). One row per account, the buttons pushed to the
   right so a long display name cannot shove them off the card. */
.account-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.account-row:last-child { border-bottom: none; }
.account-row .muted { font-size: 12.5px; margin-top: 3px; }
.badge.suspended, .badge.blocked_bot { color: var(--accent);
         border-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.badge.active { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, transparent); }
.account-actions { gap: 8px; align-items: center; }
/* `flex-direction: row` on purpose: the global `label` rule stacks its children, which
   puts the $ on its own line above the box. */
.cap { display: flex; flex-direction: row; align-items: center; gap: 4px;
       color: var(--muted); font-size: 12.5px; }
.cap-input { width: 5.5rem; }
