:root {
  --bg: #090a0f;
  --card-bg: #141620;
  --card-border: #232738;
  --accent: #6366f1;
  --accent-hover: #4f46e5;
  --cached: #10b981;
  --cached-bg: rgba(16, 185, 129, 0.15);
  --uncached: #f59e0b;
  --uncached-bg: rgba(245, 158, 11, 0.15);
  --text: #f3f4f6;
  --text-muted: #9ca3af;
  --radius: 14px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

body {
  background-color: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding-bottom: max(env(safe-area-inset-bottom, 0px) + 40px, 60px);
}

header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: rgba(9, 10, 15, 0.88);
  backdrop-filter: blur(25px);
  -webkit-backdrop-filter: blur(25px);
  border-bottom: 1px solid var(--card-border);
  padding-top: max(env(safe-area-inset-top, 0px) + 8px, 52px);
  padding-left: max(env(safe-area-inset-left, 0px), 16px);
  padding-right: max(env(safe-area-inset-right, 0px), 16px);
  padding-bottom: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 1.2rem;
  letter-spacing: -0.02em;
}

.logo-icon {
  background: linear-gradient(135deg, #6366f1, #a855f7);
  color: white;
  width: 32px;
  height: 32px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  font-size: 16px;
  box-shadow: 0 2px 8px rgba(99, 102, 241, 0.35);
}

.settings-btn {
  background: #1c1f2e;
  border: 1px solid var(--card-border);
  color: var(--text);
  width: 38px;
  height: 38px;
  border-radius: 50%;
  cursor: pointer;
  display: grid;
  place-items: center;
  font-size: 18px;
  transition: background 0.15s;
}

.settings-btn:active {
  background: #2a2f45;
}

.container {
  max-width: 600px;
  margin: 0 auto;
  padding: 16px;
}

.setup-banner {
  background: #1c1f2e;
  border: 1px solid #3b82f640;
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.setup-banner-text {
  font-size: 13px;
  line-height: 1.4;
}

.setup-banner-btn {
  background: var(--accent);
  color: white;
  border: none;
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
}

.search-box {
  position: relative;
  margin-bottom: 14px;
}

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

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

.clear-search {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 18px;
  cursor: pointer;
  display: none;
  padding: 4px;
}

.search-history-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  background: #141724;
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.7);
  z-index: 100;
  overflow: hidden;
  display: none;
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 9px 14px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  background: rgba(0, 0, 0, 0.25);
}

.history-clear-all {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 11px;
  cursor: pointer;
  padding: 2px 4px;
  font-family: inherit;
  transition: color 0.15s;
}

.history-clear-all:hover {
  color: #ef4444;
}

.history-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 11px 14px;
  cursor: pointer;
  transition: background 0.15s;
  border-bottom: 1px solid rgba(255, 255, 255, 0.03);
}

.history-item:last-child {
  border-bottom: none;
}

.history-item:hover {
  background: #1e2336;
}

.history-item-left {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
}

.history-icon {
  font-size: 12px;
  color: var(--text-muted);
  opacity: 0.7;
}

.history-text {
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-delete-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 13px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  transition: all 0.15s;
  line-height: 1;
}

.history-delete-btn:hover {
  color: #ef4444;
  background: rgba(239, 68, 68, 0.15);
}

.type-tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.type-tab {
  flex: 1;
  padding: 10px 12px;
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  color: var(--text-muted);
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
  transition: all 0.15s;
}

.type-tab.active {
  background: #252a3d;
  color: var(--text);
  border-color: var(--accent);
}

.results-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.media-card {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.15s, border-color 0.15s;
  position: relative;
}

.media-card:active {
  transform: scale(0.97);
}

.media-poster {
  width: 100%;
  aspect-ratio: 2 / 3;
  object-fit: cover;
  background: #111;
  display: block;
}

.media-info {
  padding: 8px;
}

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

.media-year {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

/* Detail View */
.detail-view {
  display: none;
}

.back-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 10px;
  font-size: 14px;
  cursor: pointer;
  margin-bottom: 16px;
}

.detail-header {
  display: flex;
  gap: 16px;
  margin-bottom: 20px;
}

.detail-poster {
  width: 90px;
  aspect-ratio: 2/3;
  border-radius: 10px;
  object-fit: cover;
  flex-shrink: 0;
}

.detail-text h2 {
  font-size: 1.25rem;
  font-weight: 700;
  line-height: 1.2;
}

.detail-text p {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 6px;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.ep-picker {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.select-box {
  flex: 1;
}

.select-box label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

select {
  width: 100%;
  padding: 12px;
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  color: var(--text);
  border-radius: 10px;
  font-size: 15px;
  outline: none;
}

/* Streams */
.streams-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}

.streams-title {
  font-size: 15px;
  font-weight: 700;
}

.stream-count {
  font-size: 12px;
  color: var(--text-muted);
}

.stream-card {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.stream-card.is-cached {
  border-color: rgba(16, 185, 129, 0.45);
  border-left: 3px solid #10b981;
  background: linear-gradient(180deg, rgba(16, 185, 129, 0.05) 0%, var(--card-bg) 100%);
}

.stream-card.is-downloading {
  border-color: rgba(14, 165, 233, 0.45);
  border-left: 3px solid #0ea5e9;
  background: linear-gradient(180deg, rgba(14, 165, 233, 0.05) 0%, var(--card-bg) 100%);
}

.stream-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.stream-filters {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 8px;
  margin-bottom: 12px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.stream-filters::-webkit-scrollbar {
  display: none;
}

.filter-chip {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  color: var(--text-muted);
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  transition: all 0.2s;
}

.filter-chip.active {
  background: #252a3d;
  color: var(--text);
  border-color: var(--accent);
}

.stream-card-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  -ms-overflow-style: none;
  padding-bottom: 2px;
}

.stream-card-scroll::-webkit-scrollbar {
  display: none;
}

.stream-card-inner {
  min-width: 100%;
  width: max-content;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.stream-card-top {
  display: flex;
  align-items: center;
  gap: 10px;
}

.stream-title {
  font-size: 14.5px;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
}

.spec-list {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 6px 0 2px 0;
  border-top: 1px solid rgba(255, 255, 255, 0.06);
}

.spec-row {
  display: flex;
  align-items: baseline;
  gap: 6px;
  font-size: 12px;
  line-height: 1.45;
  white-space: nowrap;
}

.spec-label {
  color: var(--text-muted);
  font-weight: 600;
  min-width: 56px;
  flex-shrink: 0;
}

.spec-value {
  color: #d1d5db;
  white-space: nowrap;
}

.spec-filename {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 11px;
  color: #94a3b8;
  margin-top: 2px;
  white-space: nowrap;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid rgba(255, 255, 255, 0.06);
  padding: 3px 8px;
  border-radius: 6px;
  transition: background 0.15s, color 0.15s;
}

.spec-filename:active {
  color: #cbd5e1;
  background: rgba(99, 102, 241, 0.2);
}

/* Slots Pill */
.torbox-slots-pill {
  font-size: 11.5px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 12px;
  background: #1e2233;
  color: var(--text-muted);
  border: 1px solid var(--card-border);
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.torbox-slots-pill.slots-full {
  background: rgba(239, 68, 68, 0.15);
  color: #f87171;
  border-color: rgba(239, 68, 68, 0.35);
}

.btn-downloading {
  background: #0284c7;
  color: white;
  cursor: not-allowed;
}

.stream-actions {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}

.action-btn {
  flex: 1;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: none;
  transition: opacity 0.2s, background 0.2s;
}

.btn-cache {
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  color: white;
}

.btn-cache:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-cached-done {
  background: #064e3b;
  color: #6ee7b7;
  border: 1px solid rgba(16, 185, 129, 0.3);
}

.btn-magnet {
  background: #1e2233;
  color: var(--text);
  border: 1px solid var(--card-border);
  flex: 0 0 auto;
  padding: 10px 14px;
}

.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: white;
  animation: spin 0.8s ease-in-out infinite;
}

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

.progress-bar-wrap {
  width: 100%;
  height: 6px;
  background: #1f2333;
  border-radius: 3px;
  overflow: hidden;
  margin-top: 6px;
  display: none;
}

.progress-bar {
  width: 0%;
  height: 100%;
  background: linear-gradient(90deg, #6366f1, #10b981);
  transition: width 0.3s;
}

/* Settings Modal */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(8px);
  z-index: 200;
  display: none;
  place-items: center;
  padding: 16px;
}

.modal-content {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  width: 100%;
  max-width: 480px;
  padding: 20px;
}

.modal-title {
  font-size: 1.15rem;
  font-weight: 700;
  margin-bottom: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.close-modal {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 20px;
  cursor: pointer;
}

.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.form-control {
  width: 100%;
  padding: 12px;
  background: #0d0f17;
  border: 1px solid var(--card-border);
  color: var(--text);
  border-radius: 10px;
  font-size: 14px;
  outline: none;
}

.form-control:focus {
  border-color: var(--accent);
}

.btn-save {
  width: 100%;
  padding: 14px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: #1f2937;
  color: white;
  padding: 12px 20px;
  border-radius: 30px;
  font-size: 14px;
  font-weight: 500;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.5);
  border: 1px solid #374151;
  z-index: 999;
  opacity: 0;
  transition: transform 0.25s cubic-bezier(0.175, 0.885, 0.32, 1.275), opacity 0.2s;
  pointer-events: none;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

.empty-state {
  text-align: center;
  padding: 40px 16px;
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1.6;
}

/* Home Dashboard */
.home-dashboard {
  margin-top: 14px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.quick-links-bar {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

@media (max-width: 380px) {
  .quick-links-bar {
    grid-template-columns: 1fr;
  }
}

.quick-link-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 14px;
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  transition: all 0.2s;
  font-family: inherit;
}

.quick-link-btn:hover {
  border-color: var(--accent);
  background: #1e2230;
  color: white;
}

.quick-link-icon {
  font-size: 15px;
}

.external-icon {
  font-size: 11px;
  opacity: 0.5;
}

/* Active Downloads on Home */
.home-active-section {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.home-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.home-section-title {
  font-size: 14px;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
}

.active-badge {
  font-size: 11px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 12px;
  background: rgba(99, 102, 241, 0.2);
  color: #818cf8;
  border: 1px solid rgba(99, 102, 241, 0.4);
}

.icon-refresh-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 16px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  transition: color 0.2s;
}

.icon-refresh-btn:hover {
  color: var(--text);
}

.home-downloads-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.home-dl-card {
  background: #111420;
  border: 1px solid #232838;
  border-radius: 10px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.home-dl-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}

.home-dl-title {
  font-size: 13px;
  font-weight: 600;
  line-height: 1.4;
  color: var(--text);
  word-break: break-word;
}

.home-dl-status {
  font-size: 11px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 6px;
  white-space: nowrap;
  background: rgba(99, 102, 241, 0.2);
  color: #818cf8;
}

.home-dl-status.stalled {
  background: rgba(239, 68, 68, 0.2);
  color: #f87171;
}

.home-dl-bottom {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.home-dl-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.home-dl-delete-btn {
  background: none;
  border: 1px solid #374151;
  color: #9ca3af;
  padding: 3px 8px;
  font-size: 11px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
  font-family: inherit;
}

.home-dl-delete-btn:hover {
  border-color: #ef4444;
  color: #ef4444;
  background: rgba(239, 68, 68, 0.1);
}
