:root {
  --bg-primary: #0a0a0a;
  --bg-secondary: #141414;
  --bg-card: #1a1a1a;
  --bg-elevated: #222;
  --text-primary: #fff;
  --text-secondary: #aaa;
  --text-muted: #666;
  --accent: #e50914;
  --accent-hover: #f40612;
  --success: #46d369;
  --warning: #f5c518;
  --border: #2a2a2a;
  --radius: 8px;
  --radius-lg: 12px;
  --nav-height: 60px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

body {
  padding-bottom: calc(var(--nav-height) + var(--safe-bottom) + 8px);
}

a { color: inherit; text-decoration: none; }
img { display: block; max-width: 100%; }
button { font-family: inherit; cursor: pointer; }

/* Scrollbar */
::-webkit-scrollbar { width: 4px; height: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--text-muted); border-radius: 2px; }

/* ── Bottom Navigation ── */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(var(--nav-height) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 1000;
}

.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  color: var(--text-muted);
  transition: color 0.2s;
}

.nav-item svg {
  width: 22px;
  height: 22px;
}

.nav-item span {
  font-size: 10px;
  font-weight: 500;
}

.nav-item.active {
  color: var(--accent);
}

/* ── App Container ── */
#app {
  min-height: 100vh;
}

.page {
  padding: 16px;
  padding-top: calc(12px + var(--safe-top));
  padding-left: calc(16px + var(--safe-left));
  padding-right: calc(16px + var(--safe-right));
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ── Page Header ── */
.page-header {
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 20px;
  padding-top: 8px;
}

/* ── Section ── */
.section {
  margin-bottom: 28px;
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 12px;
}

/* ── Card Grid ── */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 12px;
}

.card {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--bg-card);
  cursor: pointer;
  transition: transform 0.2s;
}

.card:active {
  transform: scale(0.96);
}

.card-poster {
  width: 100%;
  aspect-ratio: 2/3;
  object-fit: cover;
  background: var(--bg-elevated);
}

.card-info {
  padding: 8px;
}

.card-title {
  font-size: 12px;
  font-weight: 500;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card-meta {
  font-size: 10px;
  color: var(--text-secondary);
  margin-top: 4px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.card-rating {
  color: var(--warning);
  font-weight: 600;
}

.card-type-badge {
  position: absolute;
  top: 6px;
  left: 6px;
  background: var(--accent);
  color: #fff;
  font-size: 9px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 4px;
  text-transform: uppercase;
}

/* ── Horizontal Scroll Row ── */
.scroll-row {
  display: flex;
  gap: 12px;
  overflow-x: auto;
  padding-bottom: 8px;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
}

.scroll-row::-webkit-scrollbar { display: none; }

.scroll-row .card {
  min-width: 130px;
  max-width: 130px;
  scroll-snap-align: start;
}

/* ── Search ── */
.search-container {
  position: sticky;
  top: 0;
  z-index: 10;
  padding: 12px 0;
  padding-top: calc(12px + var(--safe-top));
  margin-top: calc(-1 * var(--safe-top));
  background: var(--bg-primary);
}

.search-input {
  width: 100%;
  padding: 14px 16px;
  padding-left: 44px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  color: var(--text-primary);
  font-size: 16px;
  outline: none;
  transition: border-color 0.2s;
}

.search-input:focus {
  border-color: var(--accent);
}

.search-input::placeholder {
  color: var(--text-muted);
}

.search-icon {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  width: 20px;
  height: 20px;
  color: var(--text-muted);
}

.search-wrapper {
  position: relative;
}

/* ── Details Page ── */
.details-backdrop {
  width: 100%;
  height: 240px;
  object-fit: cover;
  border-radius: 0 0 var(--radius-lg) var(--radius-lg);
}

.details-backdrop-gradient {
  position: relative;
}

.details-backdrop-gradient::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 120px;
  background: linear-gradient(transparent, var(--bg-primary));
}

.details-header {
  display: flex;
  gap: 16px;
  margin-top: -60px;
  position: relative;
  z-index: 2;
  padding: 0 16px;
}

.details-poster {
  width: 120px;
  border-radius: var(--radius);
  flex-shrink: 0;
  box-shadow: 0 4px 20px rgba(0,0,0,0.5);
}

.details-meta {
  flex: 1;
  padding-top: 64px;
}

.details-title {
  font-size: 22px;
  font-weight: 700;
  line-height: 1.2;
}

.details-subtitle {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 4px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.details-rating {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: var(--warning);
  font-weight: 600;
}

.details-body {
  padding: 20px 16px;
}

.details-overview {
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-secondary);
  margin-bottom: 20px;
}

.details-genres {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.genre-tag {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 12px;
  color: var(--text-secondary);
}

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 24px;
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 600;
  border: none;
  transition: all 0.2s;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  width: 100%;
}

.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
  background: var(--bg-elevated);
  color: var(--text-primary);
  border: 1px solid var(--border);
}

.btn-sm {
  padding: 8px 16px;
  font-size: 13px;
}

.btn-icon {
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 50%;
  background: rgba(255,255,255,0.1);
  border: none;
  color: #fff;
}

.btn-icon svg {
  width: 20px;
  height: 20px;
}

.btn-group {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

/* ── Cast ── */
.cast-list {
  display: flex;
  gap: 12px;
  overflow-x: auto;
  padding-bottom: 8px;
}

.cast-list::-webkit-scrollbar { display: none; }

.cast-item {
  text-align: center;
  min-width: 70px;
}

.cast-photo {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--bg-elevated);
  margin: 0 auto 6px;
}

.cast-name {
  font-size: 11px;
  font-weight: 500;
  line-height: 1.2;
}

.cast-role {
  font-size: 10px;
  color: var(--text-muted);
}

/* ── Season/Episode ── */
.season-select {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 14px;
  margin-bottom: 12px;
  outline: none;
}

.episode-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.episode-item {
  display: flex;
  gap: 12px;
  padding: 10px;
  background: var(--bg-card);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.2s;
}

.episode-item:active { background: var(--bg-elevated); }

.episode-still {
  width: 120px;
  height: 68px;
  border-radius: 6px;
  object-fit: cover;
  background: var(--bg-elevated);
  flex-shrink: 0;
}

.episode-info {
  flex: 1;
  min-width: 0;
}

.episode-number {
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 600;
}

.episode-name {
  font-size: 14px;
  font-weight: 500;
  margin: 2px 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.episode-overview {
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.episode-play-icon {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-secondary);
  align-self: center;
  transition: all 0.2s;
}

.episode-item:active .episode-play-icon {
  background: var(--accent);
  color: #fff;
}

.season-picker {
  margin-bottom: 8px;
}

/* ── Modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.8);
  z-index: 2000;
  display: flex;
  align-items: flex-end;
  animation: fadeIn 0.15s ease;
}

.modal-content {
  background: var(--bg-secondary);
  width: 100%;
  max-height: 85vh;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  overflow-y: auto;
  animation: slideUp 0.25s ease;
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

.modal-header {
  position: sticky;
  top: 0;
  background: var(--bg-secondary);
  padding: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--border);
  z-index: 1;
}

.modal-title {
  font-size: 18px;
  font-weight: 600;
}

.modal-close {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 24px;
  padding: 4px;
  cursor: pointer;
}

.modal-body {
  padding: 16px;
}

/* ── Torrent Picker ── */
.torrent-result {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px;
  background: var(--bg-card);
  border-radius: var(--radius);
  margin-bottom: 8px;
  cursor: pointer;
  transition: background 0.2s;
}

.torrent-result:active { background: var(--bg-elevated); }

.torrent-title {
  font-size: 13px;
  font-weight: 500;
  margin-bottom: 4px;
  line-height: 1.3;
}

.torrent-meta {
  font-size: 11px;
  color: var(--text-muted);
  display: flex;
  gap: 10px;
}

.torrent-seeds {
  color: var(--success);
  font-weight: 600;
}

.magnet-input-group {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}

.magnet-input {
  flex: 1;
  padding: 10px 12px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 13px;
  outline: none;
}

/* ── Progress ── */
.progress-bar-container {
  background: var(--bg-elevated);
  border-radius: 4px;
  height: 6px;
  overflow: hidden;
  margin: 8px 0;
}

.progress-bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 4px;
  transition: width 0.5s ease;
}

.progress-info {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--text-muted);
}

/* ── Toast ── */
#toast-container {
  position: fixed;
  top: calc(16px + var(--safe-top));
  left: calc(16px + var(--safe-left));
  right: calc(16px + var(--safe-right));
  z-index: 3000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  background: rgba(30, 30, 30, 0.95);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: var(--radius-lg);
  padding: 14px 18px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-primary);
  animation: slideDown 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 10px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
}

.toast-icon {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
}

.toast.error { border-left: 3px solid var(--accent); }
.toast.error .toast-icon { color: var(--accent); }
.toast.success { border-left: 3px solid var(--success); }
.toast.success .toast-icon { color: var(--success); }
.toast.info { border-left: 3px solid #4a9eff; }
.toast.info .toast-icon { color: #4a9eff; }

@keyframes slideDown {
  from { opacity: 0; transform: translateY(-20px) scale(0.95); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

/* ── Settings ── */
.settings-section {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  padding: 16px;
  margin-bottom: 16px;
}

.settings-label {
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.settings-value {
  font-size: 20px;
  font-weight: 700;
}

.disk-bar {
  width: 100%;
  height: 10px;
  background: var(--bg-elevated);
  border-radius: 5px;
  overflow: hidden;
  margin: 10px 0;
}

.disk-bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 5px;
  transition: width 0.5s ease;
}

.disk-bar-fill.warning { background: var(--warning); }
.disk-bar-fill.danger { background: #ff4444; }

.active-torrent {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 12px;
  background: var(--bg-elevated);
  border-radius: var(--radius);
  margin-bottom: 8px;
}

.active-torrent-info {
  flex: 1;
  min-width: 0;
}

.active-torrent-name {
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.btn-danger {
  flex-shrink: 0;
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 6px 14px;
  border-radius: var(--radius);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

/* ── Subtitle Picker ── */
.subtitle-result {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  background: var(--bg-card);
  border-radius: var(--radius);
  margin-bottom: 6px;
  cursor: pointer;
  transition: background 0.2s;
}

.subtitle-result:active { background: var(--bg-elevated); }

.subtitle-lang {
  font-weight: 600;
  font-size: 13px;
  text-transform: uppercase;
  min-width: 30px;
}

.subtitle-release {
  font-size: 12px;
  color: var(--text-secondary);
  flex: 1;
  margin: 0 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

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

/* ── Loading ── */
.loader {
  display: flex;
  justify-content: center;
  padding: 40px;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
}

.empty-state svg {
  width: 48px;
  height: 48px;
  margin-bottom: 12px;
  opacity: 0.3;
}

/* ── Downloads Page ── */
.download-item {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 10px;
}

.download-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 8px;
}

.download-name {
  font-size: 14px;
  font-weight: 500;
  flex: 1;
  margin-right: 8px;
}

.download-stats {
  display: flex;
  gap: 12px;
  font-size: 11px;
  color: var(--text-muted);
}

/* ── Library Tabs ── */
.library-tabs {
  display: flex;
  gap: 4px;
  background: var(--bg-secondary);
  border-radius: var(--radius-lg);
  padding: 4px;
  margin-bottom: 20px;
}

.library-tab {
  flex: 1;
  padding: 10px 16px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 600;
  border-radius: var(--radius);
  transition: all 0.2s;
}

.library-tab.active {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

/* ── Watchlist (Continue Watching) ── */
.watchlist {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.watchlist-item {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 10px;
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  cursor: pointer;
  transition: background 0.2s;
  position: relative;
}

.watchlist-item:active {
  background: var(--bg-elevated);
}

.watchlist-poster-wrap {
  position: relative;
  flex-shrink: 0;
  width: 80px;
  border-radius: var(--radius);
  overflow: hidden;
}

.watchlist-poster {
  width: 80px;
  aspect-ratio: 2/3;
  object-fit: cover;
  background: var(--bg-elevated);
}

.watchlist-progress-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: rgba(255, 255, 255, 0.15);
}

.watchlist-progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 0 2px 2px 0;
  transition: width 0.3s;
}

.watchlist-play-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.4);
  color: #fff;
  opacity: 0;
  transition: opacity 0.2s;
}

.watchlist-item:active .watchlist-play-overlay {
  opacity: 1;
}

.watchlist-info {
  flex: 1;
  min-width: 0;
}

.watchlist-title {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.watchlist-ep {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.watchlist-meta {
  display: flex;
  gap: 8px;
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 4px;
}

.watchlist-remove {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border: none;
  background: rgba(255, 255, 255, 0.06);
  border-radius: 50%;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
}

.watchlist-remove:active {
  background: var(--accent);
  color: #fff;
}

/* ── Favorites (My List) ── */
.fav-card {
  position: relative;
}

.fav-seen-badge {
  position: absolute;
  top: 6px;
  left: 6px;
  background: var(--success);
  color: #000;
  font-size: 9px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 4px;
  z-index: 2;
}

.fav-actions-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 28px;
  height: 28px;
  border: none;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  border-radius: 50%;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 2;
  opacity: 0.7;
  transition: opacity 0.2s;
}

.fav-actions-btn:active {
  opacity: 1;
}

/* ── Favorites Context Menu ── */
.fav-context-menu {
  position: fixed;
  z-index: 3000;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  min-width: 160px;
  overflow: hidden;
  animation: fadeIn 0.15s ease;
}

.fav-menu-item {
  padding: 12px 16px;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.15s;
}

.fav-menu-item:active {
  background: rgba(255, 255, 255, 0.08);
}

.fav-menu-danger {
  color: var(--accent);
}

/* ── Favorite Button Active State ── */
.btn-icon-active {
  background: rgba(229, 9, 20, 0.2);
  color: var(--accent);
}

/* ── Continue Watching Card Progress ── */
.continue-card {
  position: relative;
}

.continue-progress {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: rgba(255, 255, 255, 0.15);
}

.continue-progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 0 2px 2px 0;
}

/* ── Responsive ── */
@media (min-width: 480px) {
  .card-grid {
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  }
}

@media (min-width: 768px) {
  .card-grid {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  }

  .page {
    max-width: 900px;
    margin: 0 auto;
  }

  .details-backdrop { height: 350px; }
  .details-poster { width: 160px; }
  .details-title { font-size: 28px; }
}

@media (min-width: 1024px) {
  .card-grid {
    grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  }

  .page {
    max-width: 1200px;
  }
}
