:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --text: #17202a;
  --muted: #64707d;
  --line: #dde2e7;
  --accent: #1e7bd8;
  --accent-text: #ffffff;
  --ok: #1d9a5b;
  --warn: #c98a0a;
  --bad: #d23b3b;
  --bar: #e4e8ec;
  --chip: #e8ecf0;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1720;
    --card: #18222d;
    --text: #e6ebf0;
    --muted: #8d99a6;
    --line: #26323f;
    --accent: #4c9ff0;
    --accent-text: #08121c;
    --ok: #3cc47e;
    --warn: #e0a83a;
    --bad: #ef6262;
    --bar: #26323f;
    --chip: #222e3a;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2 { margin: 0; }

.btn {
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 10px;
  padding: 10px 16px;
  cursor: pointer;
  white-space: nowrap;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn {
  border: 0; background: transparent; font-size: 20px; width: 40px; height: 40px; border-radius: 10px; cursor: pointer;
  filter: grayscale(1); opacity: .55;
}
.icon-btn[aria-pressed="true"] { filter: none; opacity: 1; background: var(--chip); }
#menu-btn { filter: none; opacity: 1; font-weight: 700; }
input[type=text], input[type=search], input[type=password], textarea, select {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 10px;
  padding: 10px 12px;
  outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); }
.muted { color: var(--muted); }
.error { color: var(--bad); min-height: 1.4em; margin: 0; }

/* login */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.login-box { width: 100%; max-width: 320px; display: grid; gap: 14px; text-align: center; }
.login-logo { justify-self: center; border-radius: 18px; }

/* layout */
.app { min-height: 100%; display: flex; flex-direction: column; }
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.brand { font-size: 18px; flex: 1; }
.rates { display: flex; flex-direction: column; align-items: flex-end; font-size: 12px; font-variant-numeric: tabular-nums; }
.rate.down { color: var(--ok); }
.rate.up { color: var(--accent); }
.menu {
  position: absolute; right: 12px; top: calc(100% + 4px);
  background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 8px; min-width: 220px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .18);
}
.menu-info { margin: 4px 8px 8px; font-size: 13px; color: var(--muted); white-space: pre-line; }
.menu-item { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 10px 8px; border-radius: 8px; cursor: pointer; }
.menu-item:hover { background: var(--chip); }

main { flex: 1; width: 100%; max-width: 860px; margin: 0 auto; padding: 12px 12px calc(84px + env(safe-area-inset-bottom)); }
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: flex; justify-content: center;
  background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  flex: 1; max-width: 200px; border: 0; background: none; padding: 8px 0 10px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 12px; color: var(--muted);
}
.tab-icon { font-size: 20px; line-height: 1.2; }
.tab.on { color: var(--accent); font-weight: 600; }

.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 10px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  border: 0; background: var(--chip); border-radius: 999px; padding: 6px 12px; white-space: nowrap; cursor: pointer; font-size: 14px;
}
.chip b { font-weight: 600; opacity: .7; margin-left: 2px; }
.chip.on { background: var(--accent); color: var(--accent-text); }

/* torrent list */
.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.item {
  background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; box-shadow: var(--shadow);
}
.item-head { display: flex; gap: 8px; align-items: flex-start; cursor: pointer; }
.item-name { flex: 1; font-weight: 600; overflow-wrap: anywhere; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 999px; background: var(--chip); color: var(--muted); white-space: nowrap; }
.badge.dl { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok); }
.badge.seed { background: color-mix(in srgb, var(--accent) 18%, transparent); color: var(--accent); }
.badge.err { background: color-mix(in srgb, var(--bad) 18%, transparent); color: var(--bad); }
.badge.wait { color: var(--warn); }
.bar { height: 6px; background: var(--bar); border-radius: 3px; overflow: hidden; margin: 8px 0 6px; }
.bar > i { display: block; height: 100%; width: 0; background: var(--ok); border-radius: 3px; transition: width .4s; }
.bar.paused > i { background: var(--muted); }
.bar.seed > i { background: var(--accent); }
.bar.err > i { background: var(--bad); }
.meta { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.item-error { color: var(--bad); font-size: 13px; margin-top: 4px; overflow-wrap: anywhere; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--line); }
.actions .btn { padding: 8px 12px; font-size: 14px; }
.actions .where { width: 100%; font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }
.empty, .status { color: var(--muted); text-align: center; margin: 24px 0; }
.status { margin: 8px 0; }
.free { color: var(--muted); text-align: center; font-size: 13px; margin-top: 16px; }

/* search */
.search-bar { display: flex; gap: 8px; margin-bottom: 10px; }
.search-tools { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; }
.search-tools select { width: auto; flex-shrink: 0; }
.folder-pick { display: flex; align-items: center; gap: 8px; flex: 1; font-size: 14px; color: var(--muted); }
.folder-pick input { flex: 1; min-width: 0; }
.result { display: flex; gap: 10px; align-items: center; }
.result .body { flex: 1; min-width: 0; }
.result .item-name { font-weight: 500; }
.seeds { color: var(--ok); font-weight: 600; }
.add-btn {
  flex-shrink: 0; width: 44px; height: 44px; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-text); font-size: 24px; line-height: 1; cursor: pointer;
}
.add-btn:disabled { background: var(--chip); color: var(--muted); }
.add-btn.done { background: var(--ok); }

/* add */
.add-form { display: grid; gap: 12px; }
.add-form textarea { resize: vertical; font-family: ui-monospace, Menlo, monospace; font-size: 13px; }
.file-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.file-btn { display: inline-block; }
.check { display: flex; gap: 8px; align-items: center; }

/* dialog + toast */
.dialog { border: 0; border-radius: 16px; padding: 20px; background: var(--card); color: var(--text); width: min(92vw, 380px); }
.dialog::backdrop { background: rgba(0, 0, 0, .45); }
.remove-name { overflow-wrap: anywhere; color: var(--muted); }
.dialog-actions { display: grid; gap: 8px; margin-top: 16px; }
.toast {
  position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; max-width: 90vw;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 20; font-size: 14px;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.bad { background: var(--bad); color: #fff; }

@media (min-width: 700px) {
  .top { padding-left: 20px; padding-right: 20px; }
  main { padding-top: 20px; }
}
