:root {
  color-scheme: light dark;
  --bg: #f6f7f9;
  --fg: #1c2330;
  --muted: #5d6677;
  --card: #ffffff;
  --line: #d9dde5;
  --input-line: #868f9f; /* fields and outlined buttons: 3:1 against the card and the page (WCAG 1.4.11) */
  --accent: #2563eb;
  --accent-fg: #ffffff;
  --error: #b42318;
  --movie-fg: #92400e;  /* Movie / Series pills: their own hues, not the accent of the quality pills */
  --movie-bg: #fef3c7;
  --series-fg: #5b21b6;
  --series-bg: #ede9fe;
  --hdr: #a16207; /* HDR / DV pill: 4.9:1 on the card */
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1420;
    --fg: #e6e9f0;
    --muted: #9aa3b5;
    --card: #182030;
    --line: #2a3448;
    --input-line: #66728b;
    --accent: #5b8cff;
    --accent-fg: #0b1020;
    --error: #ff8a80;
    --movie-fg: #fcd34d;
    --movie-bg: #3b2a0c;
    --series-fg: #c4b5fd;
    --series-bg: #2a1f4d;
    --hdr: #fbbf24;
  }
}

* { box-sizing: border-box; }

/* Author rules such as `form { display: grid }` would otherwise override the hidden attribute. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

main {
  max-width: 88rem; /* ~1400px: uses a desktop screen instead of a narrow column */
  margin: 0 auto;
  padding: 1.5rem clamp(16px, 3vw, 40px) 3rem; /* 16px gutter on a phone, wider on a desktop */
}

header { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 1.25rem; }
h1 { font-size: 1.4rem; margin: 0; }
h2 { font-size: 1.05rem; margin: 1.5rem 0 0.6rem; }

form {
  display: grid;
  gap: 0.5rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 1rem;
  margin-bottom: 1.25rem;
}

label { font-size: 0.9rem; color: var(--muted); }

.field { display: grid; gap: 0.5rem; min-width: 0; }


input[type="text"],
input[type="search"],
input[type="number"],
input[type="password"],
input[type="file"] {
  width: 100%;
  padding: 0.5rem 0.6rem;
  color: inherit;
  background: var(--bg);
  border: 1px solid var(--input-line);
  border-radius: 6px;
  font: inherit;
}

button {
  justify-self: start;
  padding: 0.5rem 1rem;
  color: var(--accent-fg);
  background: var(--accent);
  border: 0;
  border-radius: 6px;
  font: inherit;
  cursor: pointer;
  min-height: 2.5rem;
}

button:hover:not(:disabled) { filter: brightness(1.1); }
button:disabled { opacity: 0.5; cursor: default; }

button.secondary {
  color: var(--fg);
  background: transparent;
  border: 1px solid var(--input-line);
}
/* brightness() does nothing on a transparent button: a light tint instead, visible on the card and the page. */
button.secondary:hover:not(:disabled) { background: color-mix(in srgb, var(--fg) 8%, transparent); }

button:focus-visible,
input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.table-wrap {
  position: relative; /* contains the absolutely positioned .sr text, or it widens the whole page */
  overflow-x: auto;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 8px;
}

table { width: 100%; border-collapse: collapse; }

th, td {
  padding: 0.6rem 0.75rem;
  text-align: left;
  white-space: nowrap;
  border-bottom: 1px solid var(--line);
}

th { font-size: 0.85rem; color: var(--muted); font-weight: 600; }
tbody tr:last-child td { border-bottom: 0; }

/* The name takes the room the other columns leave (max-width: 0 + width: 100% is the table-cell
   idiom for "fill the rest and truncate"); min-width keeps it readable and lets a phone scroll. */
td.name {
  width: 100%;
  min-width: 12rem;
  max-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

td { font-variant-numeric: tabular-nums; } /* digits of equal width: speeds and percentages do not jitter */

progress { width: clamp(8rem, 14vw, 16rem); vertical-align: middle; }

td.error { color: var(--error); }

#empty { color: var(--muted); }

/* Toast: fixed, so a message is seen wherever the page is scrolled. */
#msg {
  position: fixed;
  left: 50%;
  bottom: 1rem;
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  margin: 0;
  padding: 0.6rem 1rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgb(0 0 0 / 0.25);
}
#msg:empty { display: none; }
#msg.error { color: var(--error); border-color: var(--error); }

/* Login: a centred, narrow card. */
#login { max-width: 24rem; margin: 4rem auto 0; }

/* Search bar: one row on a desktop (query | destination | button). */

@media (min-width: 900px) {
  #find { grid-template-columns: minmax(0, 1fr) auto; align-items: end; gap: 0.75rem 1rem; }
  #find.with-dest { grid-template-columns: minmax(0, 3fr) minmax(0, 1fr) auto; } /* releases: + the folder */
}

#add { background: none; border: 0; padding: 0 0 1rem; margin: 0; }

caption { text-align: left; padding: 0.6rem 0.75rem 0; font-weight: 600; }

/* Release table (the release dialog). Columns: 1 title, 2 quality, 3 indexer, 4 size, 5 date, 6 seeders,
   7 peers, 8 leechers, 9 action. Size and counts are numbers: they line up on the right. The release name is
   what tells two releases apart: it wraps (after its dots, see breakable) instead of being cut. */
.releases :is(th, td):is(:nth-child(4), :nth-child(n+6):nth-child(-n+8)) { text-align: right; }
.releases td.name { white-space: normal; min-width: 16rem; overflow-wrap: anywhere; }
/* The name opens the release's page on the indexer: text colour, a dotted underline says it is a link. */
.releases td.name a { color: inherit; text-decoration: underline dotted var(--muted); text-underline-offset: 0.2em; }
.releases td.name a:hover { color: var(--accent); text-decoration-style: solid; }
.releases td.name a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.releases td:nth-child(2) { white-space: nowrap; } /* one line: every row the same height, easy to scan */
@media (max-width: 1100px) { /* medium screens: peers and leechers go, so Add stays in view */
  .releases :is(th, td):is(:nth-child(7), :nth-child(8)) { display: none; }
}

/* Phone: one card per release instead of a table wider than the screen. Title, then the quality pills,
   then size, seeders and Add on one line; indexer, date, peers and leechers are left out. */
@media (max-width: 700px) {
  .releases, .releases tbody { display: block; }
  .releases thead { display: none; }
  .releases tr { display: grid; grid-template-columns: auto auto 1fr; align-items: center; gap: 0.35rem 1rem; padding: 0.75rem; border-bottom: 1px solid var(--line); }
  .releases td, .releases th { display: block; padding: 0; border: 0; text-align: left; }
  .releases td.name { grid-column: 1 / -1; width: auto; max-width: none; min-width: 0; }
  .releases td:nth-child(2), .releases tr.group th { grid-column: 1 / -1; white-space: normal; }
  .releases tr.group { padding: 0; }
  .releases tr.group th { padding: 1rem 0.75rem 0.5rem; }
  .releases td:nth-child(6)::after { content: " seeders"; color: var(--muted); }
  .releases td:last-child { justify-self: end; }
  .releases :is(th, td):is(:nth-child(3), :nth-child(5), :nth-child(7), :nth-child(8)) { display: none; }
}

button.danger { color: var(--error); border-color: var(--error); }

/* Status: colour comes on top of the text, never instead of it. */
td[data-s="done"] { color: #1a7f37; }
td[data-s="failed"] { color: var(--error); }
td[data-s="downloading"], td[data-s="grabbed"], td[data-s="moving"] { color: var(--accent); }
td[data-s="missing"] { color: var(--error); }
@media (prefers-color-scheme: dark) { td[data-s="done"] { color: #56d364; } }

/* Phone: keep the columns that matter; the others stay in the API, not on a 375px screen. */
/* Why a torrent failed: under its name, in full (a tooltip cannot be read on a phone nor with the keyboard). */
td.name .why { color: var(--error); font-size: 0.9rem; white-space: normal; overflow-wrap: anywhere; }

/* Downloads, wide screens: each column sized for its longest value ("fetching metadata", "100.0 %",
   "1023.9 KB/s", "99h 59m", "999 / 999", "1023.9 GB"), so a changing value never moves the columns; the
   name takes the rest. Numbers right-aligned: their units line up and only the digits change. */
@media (min-width: 701px) {
  #dl :is(th, td):nth-child(2) { width: calc(17ch + 1.5rem); }
  #dl :is(th, td):nth-child(4) { width: calc(7ch + 1.5rem); }
  #dl :is(th, td):nth-child(5) { width: calc(11ch + 1.5rem); }
  #dl :is(th, td):nth-child(6) { width: calc(7ch + 1.5rem); }
  #dl :is(th, td):nth-child(7) { width: calc(9ch + 1.5rem); }
  #dl :is(th, td):nth-child(8) { width: calc(9ch + 1.5rem); }
  #dl :is(th, td):is(:nth-child(4), :nth-child(5), :nth-child(6), :nth-child(7), :nth-child(8)) { text-align: right; }
}

/* Phone: one card per download, like the releases. Name and Remove, then the bar, then status, % and speed
   (order puts them in that sequence); ETA, peers and size are left out. Flex, not grid: a long line wraps
   instead of widening the card. */
@media (max-width: 700px) {
  #dl, #dl tbody { display: block; }
  #dl thead { display: none; }
  #dl tr { display: flex; flex-wrap: wrap; align-items: center; gap: 0.35rem 1rem; padding: 0.75rem; border-bottom: 1px solid var(--line); }
  #dl tr:last-child { border-bottom: 0; }
  #dl td { display: block; padding: 0; border: 0; order: 3; }
  #dl td.name { flex: 1 1 0; order: 0; width: auto; max-width: none; min-width: 0; white-space: normal; overflow-wrap: anywhere; }
  #dl td:last-child { order: 1; }
  #dl td:nth-child(3) { flex-basis: 100%; order: 2; }
  #dl td:nth-child(3) progress { width: 100%; }
  #dl td:nth-child(5) { color: var(--muted); }
  #dl td:is(:nth-child(6), :nth-child(7), :nth-child(8), :nth-child(9)) { display: none; }
}

/* Touch screens: 44px targets. */
@media (pointer: coarse) {
  button, input, .segmented label { min-height: 2.75rem; }
}

.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* TMDB results: poster, title, one-line synopsis, Add. */
.hits { list-style: none; margin: 0 0 1.25rem; padding: 0; background: var(--card); border: 1px solid var(--line); border-radius: 8px; }
.hits li { display: flex; align-items: center; gap: 0.75rem; padding: 0.6rem 0.75rem; border-bottom: 1px solid var(--line); }
.hits li:last-child { border-bottom: 0; }
.hits img, .hits .nopic { width: 46px; height: 69px; flex: none; object-fit: cover; border-radius: 4px; background: var(--bg); }
.hits .info { flex: 1; min-width: 0; }
.hits .sub { color: var(--muted); font-size: 0.9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar { margin: 0 0 1.25rem; }

/* Tabs: the current one is underlined and bold (not colour alone). */
.tabs { display: flex; gap: 0.25rem; margin-bottom: 1.25rem; border-bottom: 1px solid var(--line); overflow-x: auto; }
.tabs [role="tab"] {
  flex: none;
  min-height: 2.75rem;
  padding: 0.5rem 1rem;
  color: var(--muted);
  background: none;
  border: 0;
  border-bottom: 3px solid transparent;
  border-radius: 0;
  font-weight: 500;
}
.tabs [role="tab"]:hover { color: var(--fg); filter: none; }
.tabs [role="tab"][aria-selected="true"] { color: var(--fg); font-weight: 700; border-bottom-color: var(--accent); }
.badge { display: inline-block; min-width: 1.4em; margin-left: 0.3rem; padding: 0 0.4em; color: var(--accent-fg); background: var(--accent); border-radius: 999px; font-size: 0.8rem; text-align: center; }

/* Movie release picker. */
dialog { width: min(80rem, calc(100vw - 2rem)); max-height: calc(100dvh - 2rem); color: var(--fg); background: var(--bg); border: 1px solid var(--line); border-radius: 8px; }
/* A column: title on top, only the table scrolls, the buttons stay at the bottom. [open] only, or the
   display would show a closed dialog. */
dialog[open] { display: flex; flex-direction: column; overflow: hidden; }
dialog::backdrop { background: rgb(0 0 0 / 0.5); }

/* Library choice of a new series: one group per kind (Séries, Animes…), one full-width button per disk. */
#lib-dlg { width: min(30rem, calc(100vw - 2rem)); }
#lib-dlg form { display: flex; flex-direction: column; min-height: 0; margin: 0; padding: 0; border: 0; background: none; }
#lib-choices { overflow: auto; display: grid; gap: 1.25rem; padding: 4px; margin: -4px; } /* room for the focus ring */
.lib-group h3 { margin: 0 0 0.5rem; font-size: 0.8rem; font-weight: 600; letter-spacing: 0.05em; text-transform: uppercase; color: var(--muted); }
.lib-group button { display: block; width: 100%; margin-top: 0.5rem; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
dialog h2 { margin-top: 0; }
dialog p:empty { display: none; } /* no gap under the title when there is nothing to say */
dialog .table-wrap { flex: 1 1 auto; min-height: 0; overflow: auto; }
dialog thead th { position: sticky; top: 0; z-index: 1; background: var(--card); }
dialog .bar { flex: none; margin: 1rem 0 0; padding: 0; border: 0; background: none; display: flex; gap: 0.5rem; justify-content: flex-end; }

/* Quality pills in the search results: resolution filled, the others outlined (shape, not colour alone). */
.pill { display: inline-block; margin-right: 0.3rem; padding: 0 0.5em; border: 1px solid var(--accent); border-radius: 999px; font-size: 0.8rem; font-weight: 600; }
/* Resolution by tier: 4K filled, 1080p outlined, 720p and SD outlined grey. HDR / DV: a gold outline. */
.pill[data-k="res"][data-v="4K"] { color: var(--accent-fg); background: var(--accent); }
.pill[data-k="res"]:is([data-v="720p"], [data-v="SD"]) { color: var(--muted); border-color: var(--muted); }
.pill[data-k="hdr"] { color: var(--hdr); border-color: var(--hdr); }
/* Source, codec, language: text, a dot between them. Remux is the one source worth a second look. */
.q { font-size: 0.85rem; color: var(--muted); }
.q + .q::before { content: " · "; }
.q[data-v="Remux"] { color: var(--accent); font-weight: 600; }
.q[data-k="lang"] { color: var(--fg); font-weight: 600; }
dialog tr.group th { padding-top: 1rem; color: var(--fg); font-size: 0.95rem; background: var(--bg); }
.hits .pill { vertical-align: 0.1em; }
/* Movie / Series: tinted, no border, so they never read as a quality pill. Contrast 6.4:1 to 9.6:1. */
.pill[data-k="movie"] { color: var(--movie-fg); background: var(--movie-bg); border-color: transparent; }
.pill[data-k="tv"] { color: var(--series-fg); background: var(--series-bg); border-color: transparent; }
#p-following .table-wrap { margin-bottom: 0.75rem; }
#series td:nth-child(4) { white-space: normal; } /* the missing summary wraps rather than being cut */

/* Search mode switch: two native radios shown as one segmented control (arrow keys, screen readers and
   the checked state come from the radios). Full width on a phone. */
.segmented { display: inline-flex; margin-bottom: 0.75rem; padding: 3px; background: var(--card); border: 1px solid var(--line); border-radius: 8px; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label { min-height: 2.5rem; display: flex; align-items: center; padding: 0 1rem; color: var(--muted); font-size: 0.95rem; font-weight: 500; border-radius: 6px; cursor: pointer; }
.segmented input:checked + label { color: var(--accent-fg); background: var(--accent); font-weight: 600; }
.segmented input:focus-visible + label { outline: 2px solid var(--accent); outline-offset: 2px; }
@media (max-width: 700px) { .segmented { display: flex; } .segmented label { flex: 1; justify-content: center; } }
#rel-info:empty { display: none; }

/* Downloads: the "Add torrent…" button on the right, above the list; its dialog is narrow (three fields). */
.bar.end { display: flex; justify-content: flex-end; margin-bottom: 0.75rem; }
#add-dlg { width: min(36rem, calc(100vw - 2rem)); }
#add { padding-bottom: 0; }
