:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1e222b;
  --line: #2a2f3a;
  --text: #e6e8ee;
  --muted: #9aa1b0;
  --accent: #5b8def;
  --accent-2: #3f6fd1;
  --ok: #3fb67a;
  --warn: #d9a23b;
  --err: #e5605a;
  --radius: 8px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
a { color: var(--text); text-decoration: none; }
a:hover { text-decoration: underline; }
h2 { margin: 0; font-size: 20px; }
h3 { margin: 22px 0 8px; font-size: 15px; }
h3 small, legend small, .source-head small { color: var(--muted); font-weight: normal; }

header {
  display: flex; align-items: center; gap: 24px;
  padding: 0 24px; height: 56px;
  background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 700; font-size: 17px; }
nav { display: flex; gap: 4px; }
nav button { background: none; color: var(--muted); border: none; padding: 8px 12px; border-radius: 6px; }
nav button:hover { color: var(--text); background: var(--panel-2); }
nav button.active { color: var(--text); background: var(--panel-2); }

main { max-width: 1200px; margin: 0 auto; padding: 24px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--err); }
.hint { color: var(--muted); margin: 6px 0 16px; }
.empty { color: var(--muted); padding: 40px 0; text-align: center; }
.nowrap { white-space: nowrap; }
.spacer { flex: 1; }

button {
  background: var(--accent); color: #fff; border: 1px solid transparent;
  padding: 7px 14px; border-radius: 6px; font: inherit; cursor: pointer;
  touch-action: manipulation;
}
button:hover { background: var(--accent-2); }
button:disabled { opacity: .5; cursor: default; }
button.ghost { background: transparent; border-color: var(--line); color: var(--text); }
button.ghost:hover { background: var(--panel-2); }
button.danger { color: var(--err); }
button.small { padding: 3px 8px; font-size: 12px; }

input, select, textarea {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 6px;
  padding: 6px 8px; font: inherit; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 12px; }
label.check { flex-direction: row; align-items: center; gap: 6px; color: var(--text); font-size: 14px; }
label.grow { flex: 1; }
label.inline { flex-direction: row; align-items: center; gap: 6px; font-size: 14px; color: var(--text); }
.label { color: var(--muted); font-size: 12px; align-self: center; }
.row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; margin: 8px 0; }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 8px; }

.seg {
  display: inline-flex; align-items: center; gap: 2px;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 2px;
}
.seg button {
  display: inline-flex; align-items: center; gap: 6px;
  background: transparent; border: none; color: var(--muted);
  padding: 5px 11px; border-radius: 6px; font-size: 13px;
}
.seg button:hover { color: var(--text); background: var(--panel-2); }
.seg button.active { color: #fff; background: var(--accent); }
.seg-count {
  font-size: 11px; padding: 0 6px; border-radius: 10px;
  background: var(--panel-2); color: var(--muted);
}
.seg button.active .seg-count { background: rgba(255, 255, 255, .2); color: #fff; }
.mt-row { align-items: center; }

.badge { background: var(--err); color: #fff; border-radius: 10px; padding: 0 7px; font-size: 11px; margin-left: 4px; }
.tag { display: inline-block; background: var(--panel-2); border: 1px solid var(--line); color: var(--muted);
  border-radius: 10px; padding: 0 8px; font-size: 11px; margin-right: 4px; font-weight: normal; }
.tag.movie { color: #8fb4ff; }
.tag.tv { color: #b79cff; }

/* approval cards */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(460px, 1fr)); gap: 14px; }
.card { display: flex; gap: 14px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; }
.card .poster { flex: 0 0 100px; }
.card .poster img, .noposter { width: 100px; height: 150px; object-fit: cover; border-radius: 6px; background: var(--panel-2); }
.noposter { display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 11px; }
.card .body { flex: 1; min-width: 0; }
.card .title { font-weight: 600; font-size: 15px; }
.card .overview { color: var(--muted); font-size: 12px; margin: 6px 0;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.card .choose select { max-width: 190px; }

/* rules */
.rule { display: flex; align-items: center; gap: 16px; background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px 16px; margin-bottom: 10px; }
.rule.off { opacity: .65; }
.rule-main { flex: 1; min-width: 0; }
.rule-name { font-weight: 600; font-size: 15px; margin-bottom: 2px; }
.rule-actions { display: flex; gap: 6px; }

.switch { position: relative; width: 42px; height: 24px; flex: 0 0 42px; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: var(--line); border-radius: 24px; cursor: pointer; transition: .15s; }
.switch span::before { content: ""; position: absolute; width: 18px; height: 18px; left: 3px; top: 3px;
  background: #fff; border-radius: 50%; transition: .15s; }
.switch input:checked + span { background: var(--ok); }
.switch input:checked + span::before { transform: translateX(18px); }

/* table */
.table { width: 100%; border-collapse: collapse; background: var(--panel); border-radius: var(--radius); overflow: hidden; }
.table th, .table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table th { color: var(--muted); font-weight: 500; font-size: 12px; background: var(--panel-2); }
.status { font-size: 12px; padding: 1px 8px; border-radius: 10px; background: var(--panel-2); }
.status.added, .status.requested { color: var(--ok); }
.status.pending, .status.processing { color: var(--warn); }
.status.failed { color: var(--err); }
.status.exists, .status.rejected { color: var(--muted); }

/* settings */
.settings { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 14px; margin-top: 12px; }
.settings fieldset { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px 14px; display: flex; flex-direction: column; gap: 10px; }
.settings fieldset .row { margin: 0; }
.settings fieldset .row label { flex: 1; }
.settings fieldset button { align-self: flex-start; }
.settings legend { padding: 0 6px; font-weight: 600; }
.settings .actions { grid-column: 1 / -1; }

/* dialog */
dialog { background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 12px;
  box-sizing: border-box; width: min(940px, calc(100vw - 16px)); max-width: calc(100vw - 16px);
  max-height: min(86vh, calc(100dvh - 16px)); max-height: min(86vh, calc(100svh - 16px));
  overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  padding: 22px 24px; margin: auto; }
dialog::backdrop { background: rgba(0, 0, 0, .6); }
.source { background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; margin-bottom: 10px; }
.source .row { margin: 6px 0 0; }
.source input:not([type]) { flex: 1; }
.dest { background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; margin-bottom: 10px; }
.meta-error { color: var(--err); font-size: 12px; margin-left: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip { display: inline-flex; align-items: center; gap: 4px; background: var(--accent-2); color: #fff;
  border-radius: 14px; padding: 2px 4px 2px 10px; font-size: 12px; }
.chip button { background: none; border: none; color: #fff; padding: 0 5px; font-size: 14px; line-height: 1; }
.results { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.results button { min-height: 36px; }
#source-results { max-height: 220px; overflow: auto; -webkit-overflow-scrolling: touch; }
.chip.kind-providers { background: #2a8f6f; }
.chip.kind-companies { background: #3f6fd1; }
.chip.kind-networks { background: #6b5b95; }
.chip .chip-meta { opacity: .85; font-size: 11px; }
.scope-box { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; margin: 12px 0; }
.scope-box p { margin: 0 0 6px; }
.scope-box ul { margin: 6px 0 0; padding-left: 18px; }
.scope-box li { margin: 3px 0; }
.source-group { width: 100%; }
.source-group h4 { margin: 10px 0 4px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.preview h4 { margin: 12px 0 8px; font-size: 14px; }
.preview-grid { margin-bottom: 8px; }
details { margin-top: 14px; }
summary { cursor: pointer; font-weight: 600; }
.actions { display: flex; gap: 8px; align-items: center; margin-top: 18px; }
#rule-form .actions { position: sticky; bottom: 0; background: var(--panel); padding: 12px 0 4px; border-top: 1px solid var(--line); z-index: 2; }

.preview { margin-top: 16px; background: var(--panel-2); border-radius: var(--radius); padding: 12px; }
.preview-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 10px; max-height: 360px; overflow: auto; }
.pv img, .pv .noposter { width: 100%; height: auto; aspect-ratio: 2 / 3; border-radius: 6px; }
.pv.exists, .pv.seen { opacity: .45; }

.toast { position: fixed; right: 20px; bottom: 20px; max-width: 460px; padding: 12px 16px; border-radius: 8px;
  background: var(--panel-2); border: 1px solid var(--line); box-shadow: 0 6px 24px rgba(0,0,0,.4); z-index: 20; pointer-events: none; }
.toast.ok { border-left: 4px solid var(--ok); }
.toast.error { border-left: 4px solid var(--err); }

@media (max-width: 640px) {
  header { gap: 8px; padding: 6px 10px; height: auto; min-height: 56px; flex-wrap: wrap; }
  nav button { padding: 6px 8px; min-height: 44px; }
  main { padding: 16px 12px; }
  .toolbar .spacer { display: none; }
  .seg { max-width: 100%; overflow-x: auto; }
  .cards { grid-template-columns: 1fr; }
  .rule { flex-wrap: wrap; }
  .rule-main { flex-basis: calc(100% - 58px); }
  .rule-actions { flex-wrap: wrap; }
  .rule-actions button, #rule-new, #tab-queue button, #tab-history button,
  #tab-history select, .dn-var, #settings-form button[type=submit] { min-height: 44px; }
  .history-table, .history-table tbody, .history-table tr { display: block; width: 100%; }
  .history-table thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .history-table tr { padding: 8px 0; border-bottom: 1px solid var(--line); }
  .history-table td { display: block; border: 0; white-space: normal; overflow-wrap: anywhere; padding: 4px 12px; }
  .history-table td[data-label] { display: grid; grid-template-columns: 4.5rem minmax(0, 1fr); align-items: start; gap: 4px; }
  .history-table td[data-label]::before { content: attr(data-label); color: var(--muted); font-size: 12px; }
  .history-table td[data-label="Actions"] { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }
  /* Pin the editor inside the visual viewport. Native <dialog> max-height is ignored on some WebKit builds. */
  dialog {
    position: fixed; inset: 8px; width: auto; height: auto; max-width: none; max-height: none;
    margin: 0; padding: 16px;
  }
  .dn-var { padding: 8px 10px; font-size: 12px; }
}

/* login + setup wizard */
.auth-view { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; }
.auth-card { width: min(400px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 28px; display: flex; flex-direction: column; gap: 12px; }
.auth-card.wide { width: min(720px, 100%); }
.auth-card h2 { margin-top: 6px; }
.auth-card form, .wstep { display: flex; flex-direction: column; gap: 12px; }
.brand.big { font-size: 22px; font-weight: 700; }
.auth-card .ok { color: var(--ok); }
.error:empty, .auth-card .ok:empty { display: none; }
code { background: var(--panel-2); padding: 1px 5px; border-radius: 4px; font-size: 12px; }
.steps { display: flex; gap: 6px; list-style: none; padding: 0; margin: 4px 0 8px; counter-reset: step; }
.steps li { flex: 1; font-size: 12px; color: var(--muted); padding-top: 8px; border-top: 3px solid var(--line); counter-increment: step; }
.steps li::before { content: counter(step) ". "; }
.steps li.done { border-color: var(--ok); color: var(--text); }
.steps li.current { border-color: var(--accent); color: var(--text); font-weight: 600; }
.svc { background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; }
.svc .row { margin: 6px 0 0; }
.svc-status { font-size: 12px; margin-left: 6px; }
.svc-status.ok { color: var(--ok); }
.svc-status.bad { color: var(--err); }
#password-form { margin-top: 14px; }
#gs-fields { display: flex; flex-direction: column; gap: 10px; padding: 10px; background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius); }
#gs-next-run { margin: 0; }
.auth-card p, .auth-card h2 { margin: 0; }
.source-stats { list-style: none; padding: 0; margin: 8px 0 14px; display: grid; gap: 4px; }
.source-stats li { background: var(--panel); border-left: 3px solid var(--ok); padding: 4px 10px; border-radius: 4px; }
.source-stats li.warn { border-left-color: var(--warn); }
.source-stats li.error { border-left-color: var(--err); color: var(--text); }
.source-stats li.error .small { color: var(--err); }
.section-title { margin-top: 28px; font-size: 18px; }
.section-title small { font-size: 12px; font-weight: normal; display: block; margin-top: 4px; }

/* footer */
.site-footer { text-align: center; color: var(--muted); font-size: 12px; padding: 18px 24px 26px; }
.auth-view { min-height: calc(100vh - 70px); }

/* discord notification customization & live preview */
.discord-notify-fieldset { grid-column: 1 / -1; }
.discord-layout { display: grid; grid-template-columns: minmax(0, 1.15fr) minmax(0, 0.85fr); gap: 18px; align-items: start; }
.discord-controls { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.discord-controls textarea { resize: vertical; width: 100%; }
.dn-color-inputs { display: flex; gap: 8px; align-items: center; }
.dn-color-inputs input[type="color"] { width: 38px; height: 32px; padding: 2px; cursor: pointer; flex: 0 0 38px; }
.dn-color-inputs input[type="text"] { width: 92px; font-family: ui-monospace, monospace; }
.dn-vars { display: flex; flex-wrap: wrap; gap: 5px; align-items: center; }
.dn-var { background: var(--panel-2); border: 1px solid var(--line); color: var(--muted); border-radius: 5px;
  padding: 2px 7px; font-size: 11px; font-family: ui-monospace, monospace; }
.dn-var:hover { color: var(--text); border-color: var(--accent); background: var(--panel-2); }
.dn-toggles { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 6px 12px; padding-top: 2px; }
.dn-actions { display: flex; flex-wrap: wrap; gap: 8px; padding-top: 4px; }

.discord-preview-wrap { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.dp-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.discord-preview { background: #313338; border: 1px solid #3f4147; border-radius: var(--radius); padding: 14px; color: #dbdee1; }
.dp-msg { display: flex; gap: 12px; align-items: flex-start; min-width: 0; }
.dp-avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--accent); color: #fff; font-weight: 700;
  display: flex; align-items: center; justify-content: center; flex: 0 0 36px; overflow: hidden; font-size: 15px; }
.dp-avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.dp-body { flex: 1; min-width: 0; }
.dp-meta { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin-bottom: 4px; }
.dp-username { font-weight: 600; color: #f2f3f5; font-size: 14px; }
.dp-bot { background: #5865f2; color: #fff; font-size: 10px; font-weight: 700; padding: 1px 4px; border-radius: 3px; line-height: 1.2; }
.dp-time { color: #949ba4; font-size: 11px; }
.dp-embed { background: #2b2d31; border-left: 4px solid var(--accent); border-radius: 4px; padding: 10px 12px; margin-top: 4px; }
.dp-embed-main { display: flex; gap: 12px; align-items: flex-start; }
.dp-embed-text { flex: 1; min-width: 0; }
.dp-embed-title { font-weight: 600; color: #00a8fc; font-size: 14px; margin-bottom: 4px; word-break: break-word; }
.dp-embed-desc { color: #dbdee1; font-size: 13px; line-height: 1.4; white-space: pre-wrap; word-break: break-word; }
.dp-embed-fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(95px, 1fr)); gap: 8px; margin-top: 8px; }
.dp-field-name { font-size: 11px; font-weight: 600; color: #f2f3f5; }
.dp-field-val { font-size: 12px; color: #dbdee1; }
.dp-thumb { flex: 0 0 60px; }
.dp-thumb svg { display: block; border-radius: 4px; }
.dp-embed-footer { margin-top: 8px; font-size: 11px; color: #949ba4; }

@media (max-width: 820px) {
  .discord-layout { grid-template-columns: 1fr; }
}
