:root {
  color-scheme: dark;
  --bg: #121212;
  --surface: #1c1c1c;
  --surface-2: #262626;
  --text: #f2f2f2;
  --text-muted: #9a9a9a;
  --accent: #3987e5;
  --watched: #2fa84f;
  --progress: #d98c1f;
  --border: rgba(255,255,255,0.09);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: 44px;
}

.topbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px 20px;
  padding: 14px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.search-input {
  flex: 1 1 160px;
  max-width: 240px;
  padding: 6px 14px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 13.5px;
}
.search-input::placeholder { color: var(--text-muted); }

.topbar h1 { font-size: 18px; margin: 0 auto 0 0; font-weight: 600; }

.tabs { display: flex; gap: 8px; }

.tab-btn, .pill-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  padding: 6px 16px;
  border-radius: 20px;
  cursor: pointer;
  font-size: 13.5px;
}

.tab-btn.active { background: var(--accent); color: white; border-color: var(--accent); }
.pill-btn:hover { color: var(--text); }
.pill-btn.primary { background: var(--accent); color: white; border-color: var(--accent); }
.pill-btn.syncing { opacity: 0.6; pointer-events: none; }

main { padding: 20px; max-width: 1400px; margin: 0 auto; }

.grid-view {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 18px;
}

.card { display: flex; flex-direction: column; gap: 6px; position: relative; }
.card.clickable { cursor: pointer; }

.poster-wrap {
  position: relative;
  aspect-ratio: 2 / 3;
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
}

.poster-wrap img { width: 100%; height: 100%; object-fit: cover; display: block; }

.poster-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: 12px;
  text-align: center;
  padding: 8px;
}

.badge {
  position: absolute;
  bottom: 6px;
  right: 6px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: white;
  box-shadow: 0 1px 4px rgba(0,0,0,0.55);
}

.badge.watched { background: var(--watched); }
.badge.progress { border: 2px solid rgba(0,0,0,0.3); }

.manual-tag {
  position: absolute;
  top: 6px;
  left: 6px;
  background: rgba(0,0,0,0.6);
  color: var(--text-muted);
  font-size: 9.5px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 2px 6px;
  border-radius: 4px;
}

.remove-btn {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: none;
  background: rgba(0,0,0,0.65);
  color: white;
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
}

.card:hover .remove-btn { display: flex; }

.card-title {
  font-size: 13px;
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.card-meta { font-size: 11px; color: var(--text-muted); }

.status-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 6px 20px;
  font-size: 11px;
  color: var(--text-muted);
}

.fab {
  position: fixed;
  right: 24px;
  bottom: 54px;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  border: none;
  font-size: 26px;
  cursor: pointer;
  box-shadow: 0 3px 10px rgba(0,0,0,0.4);
  z-index: 20;
}

/* Detail view */
.back-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 14px;
  border-radius: 20px;
  cursor: pointer;
  margin-bottom: 16px;
}

.detail-header { display: flex; gap: 20px; margin-bottom: 24px; align-items: flex-start; }

.detail-poster {
  width: 160px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
  flex-shrink: 0;
  aspect-ratio: 2 / 3;
  background: var(--surface);
}
.detail-poster img { width: 100%; height: 100%; object-fit: cover; display: block; }

.detail-info h2 { margin: 0 0 8px; }
.detail-note { color: var(--text-muted); font-size: 13px; }

.season-group { margin-bottom: 24px; }
.season-title { font-size: 15px; font-weight: 600; margin-bottom: 10px; color: var(--text-muted); }

.episode-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}

.ep-status {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: white;
}
.ep-status.watched { background: var(--watched); }
.ep-status.unwatched { border: 2px solid var(--border); }

.ep-num { color: var(--text-muted); font-size: 12px; width: 42px; flex-shrink: 0; }
.ep-title { flex: 1; font-size: 13px; }
.ep-date { color: var(--text-muted); font-size: 11px; flex-shrink: 0; }

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
  padding: 16px;
}

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 22px;
  width: 100%;
  max-width: 380px;
}

.modal h3 { margin: 0 0 4px; }
.modal-hint { color: var(--text-muted); font-size: 12px; margin: 0 0 16px; }

.seg-control { display: flex; gap: 8px; margin-bottom: 16px; }
.seg-btn {
  flex: 1;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-muted);
  padding: 8px;
  border-radius: 8px;
  cursor: pointer;
}
.seg-btn.active { background: var(--accent); color: white; border-color: var(--accent); }

.modal label {
  display: block;
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 12px;
}

.modal .optional { font-weight: normal; color: var(--text-muted); }

.modal input {
  width: 100%;
  margin-top: 5px;
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 13px;
}

.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

.title-field { position: relative; margin-bottom: 12px; }
.title-field label { margin-bottom: 0; }

.suggestions {
  position: absolute;
  left: 0;
  right: 0;
  top: 100%;
  margin-top: 2px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  max-height: 260px;
  overflow-y: auto;
  z-index: 40;
  box-shadow: 0 4px 14px rgba(0,0,0,0.4);
}

.suggestion-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 10px;
  cursor: pointer;
}
.suggestion-item:hover { background: rgba(255,255,255,0.06); }

.suggestion-poster {
  width: 30px;
  height: 45px;
  border-radius: 4px;
  object-fit: cover;
  background: var(--surface);
  flex-shrink: 0;
}

.suggestion-text { font-size: 13px; }
.suggestion-year { color: var(--text-muted); font-size: 11.5px; }
