:root {
  --bg: #0a0a0c;
  --surface: #16161a;
  --surface-2: #1f1f26;
  --border: #2a2a32;
  --fg: #f0f0f3;
  --fg-dim: #a0a0aa;
  --fg-muted: #6a6a72;
  --accent: #4f8cff;
  --accent-fg: #ffffff;
  --green: #2ecc71;
  --orange: #ff9f0a;
  --red: #ff3b30;
  --radius: 12px;
  /* iOS safe areas */
  --safe-top: env(safe-area-inset-top);
  --safe-bot: env(safe-area-inset-bottom);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: contain;
}

input, button, select { font: inherit; color: inherit; }

.hidden { display: none !important; }

.topbar {
  padding: calc(var(--safe-top) + 12px) 16px 12px;
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
}

.tabs {
  display: flex;
  gap: 8px;
  padding: 4px 12px 12px;
  background: var(--bg);
  position: sticky;
  top: 0;
  border-bottom: 1px solid var(--border);
}
.tab {
  flex: 1;
  border: 1px solid var(--border);
  background: var(--surface);
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  color: var(--fg-dim);
  cursor: pointer;
  transition: all 0.15s;
}
.tab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-fg);
}
.badge {
  display: inline-block;
  min-width: 20px;
  padding: 2px 7px;
  margin-left: 4px;
  background: rgba(255,255,255,0.18);
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}
.tab:not(.active) .badge { background: var(--surface-2); }

main {
  padding: 16px;
  padding-bottom: calc(var(--safe-bot) + 32px);
}

.view { display: none; }
.view.active { display: block; }

/* Search form */
.search-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 18px;
}
.search-form input[type="search"] {
  width: 100%;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 16px;
  outline: none;
  transition: border-color 0.15s;
}
.search-form input[type="search"]:focus {
  border-color: var(--accent);
}
.search-form .row {
  display: flex;
  gap: 10px;
}
.search-form select {
  flex: 0 0 100px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  outline: none;
  -webkit-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' fill='%23a0a0aa'%3E%3Cpath d='M6 8 0 0h12z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 32px;
}
.search-form button[type="submit"] {
  flex: 1;
  padding: 12px;
  background: var(--accent);
  color: var(--accent-fg);
  border: none;
  border-radius: var(--radius);
  font-weight: 600;
  cursor: pointer;
}
.search-form button[type="submit"]:disabled {
  opacity: 0.5;
  cursor: wait;
}

/* Douban metadata strip */
.movie-meta {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 14px;
  font-size: 14px;
}
.movie-meta .title { font-weight: 600; }
.movie-meta .rating { color: var(--orange); font-weight: 600; }
.movie-meta .muted { color: var(--fg-muted); font-size: 13px; }

.status {
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 14px;
  font-size: 14px;
  color: var(--fg-dim);
}
.status.error {
  border-color: var(--red);
  color: var(--red);
}

/* Result / task cards */
.results {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.card {
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.card .name {
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 6px;
  word-break: break-word;
}
.card .meta {
  font-size: 12px;
  color: var(--fg-muted);
  display: flex;
  flex-wrap: wrap;
  gap: 6px 12px;
  margin-bottom: 10px;
}
.card .meta .pill {
  background: var(--surface-2);
  border-radius: 999px;
  padding: 1px 8px;
}
.card .actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}
.card button.action {
  padding: 8px 14px;
  background: var(--accent);
  color: var(--accent-fg);
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: opacity 0.15s;
}
.card button.action.secondary {
  background: var(--surface-2);
  color: var(--fg);
}
.card button.action:disabled {
  opacity: 0.5;
  cursor: wait;
}
.card.submitted { border-color: rgba(79, 140, 255, 0.4); }
.card.completed, .card.archived { border-color: rgba(46, 204, 113, 0.4); }
.card.failed, .card.archive_failed { border-color: rgba(255, 59, 48, 0.4); }

.card .progress {
  height: 4px;
  background: var(--surface-2);
  border-radius: 2px;
  overflow: hidden;
  margin: 8px 0;
}
.card .progress > div {
  height: 100%;
  background: var(--accent);
  transition: width 0.3s;
}
.card.completed .progress > div, .card.archived .progress > div {
  background: var(--green);
}
.card.failed .progress > div, .card.archive_failed .progress > div {
  background: var(--red);
}

.task-controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}
.link {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 14px;
  padding: 6px 8px;
  cursor: pointer;
}
.muted { color: var(--fg-muted); font-size: 13px; }

/* Feed cards with poster */
.feed-card {
  display: flex;
  gap: 12px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color 0.15s, transform 0.1s;
}
.feed-card:active { transform: scale(0.98); }
.feed-card:hover { border-color: var(--accent); }
.feed-card .poster {
  flex: 0 0 90px;
  width: 90px;
  height: 130px;
  background: var(--surface-2);
  border-radius: 6px;
  object-fit: cover;
  display: block;
}
.feed-card .info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-top: 2px;
}
.feed-card .info .title {
  font-size: 15px;
  font-weight: 600;
  color: var(--fg);
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.feed-card .info .sub {
  font-size: 12px;
  color: var(--fg-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.feed-card .info .pills {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 4px;
}
.feed-card .info .pills .pill {
  font-size: 11px;
  background: var(--surface-2);
  padding: 1px 7px;
  border-radius: 999px;
  color: var(--fg-dim);
}
.feed-card .info .pills .rating {
  background: rgba(255,159,10,0.18);
  color: var(--orange);
  font-weight: 600;
}
.feed-card .info .pills .hasmag {
  background: rgba(46,204,113,0.18);
  color: var(--green);
  font-weight: 600;
}

/* Detail modal */
.modal-bg {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 50;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: fade-in 0.2s;
}
@keyframes fade-in { from { opacity: 0 } to { opacity: 1 } }
.modal {
  width: 100%;
  max-width: 560px;
  max-height: 92vh;
  background: var(--bg);
  border-radius: 16px 16px 0 0;
  overflow-y: auto;
  padding: 16px;
  padding-top: 8px;
  padding-bottom: calc(var(--safe-bot) + 24px);
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border);
  border-bottom: none;
}
.modal .close-handle {
  width: 36px;
  height: 4px;
  background: var(--border);
  border-radius: 999px;
  margin: 6px auto 14px;
}
.modal .hero {
  display: flex;
  gap: 12px;
  margin-bottom: 14px;
}
.modal .hero .poster {
  flex: 0 0 100px;
  width: 100px;
  height: 145px;
  background: var(--surface-2);
  border-radius: 8px;
  object-fit: cover;
}
.modal .hero .info {
  flex: 1;
  min-width: 0;
}
.modal .hero h2 {
  margin: 0 0 4px;
  font-size: 18px;
  font-weight: 700;
  line-height: 1.25;
}
.modal .hero .alt {
  font-size: 13px;
  color: var(--fg-dim);
  margin-bottom: 10px;
}
.modal .hero .ratings {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}
.modal .hero .ratings .pill {
  font-size: 12px;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--fg-dim);
}
.modal .hero .ratings .pill.douban {
  background: rgba(0,180,80,0.18); color: #2ecc71;
}
.modal .hero .ratings .pill.imdb {
  background: rgba(245,197,24,0.18); color: #f5c518;
}
.modal .meta-grid {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 6px 12px;
  font-size: 13px;
  margin-bottom: 14px;
}
.modal .meta-grid dt { color: var(--fg-muted); }
.modal .meta-grid dd { margin: 0; color: var(--fg); }
.modal .synopsis {
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13.5px;
  line-height: 1.55;
  color: var(--fg-dim);
  white-space: pre-wrap;
  margin-bottom: 14px;
}
.modal h3 {
  margin: 14px 0 8px;
  font-size: 14px;
  color: var(--fg-dim);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.modal .magnet-item {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 8px;
}
.modal .magnet-item .label {
  flex: 1;
  font-size: 13px;
  word-break: break-word;
}
.modal .magnet-item button {
  flex: 0 0 auto;
  padding: 8px 14px;
  background: var(--accent);
  color: var(--accent-fg);
  border: none;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.modal .magnet-item button:disabled { opacity: 0.5; cursor: wait; }
.modal .empty-magnets {
  padding: 14px;
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  color: var(--fg-muted);
  text-align: center;
}

.toast {
  position: fixed;
  bottom: calc(var(--safe-bot) + 20px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 18px;
  font-size: 14px;
  z-index: 100;
  animation: toast-in 0.2s ease-out;
}
.toast.success { border-color: var(--green); color: var(--green); }
.toast.error { border-color: var(--red); color: var(--red); }
@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 20px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}
