:root {
  --bg: #0b0f14;
  --bg-elevated: #121820;
  --bg-card: #161d27;
  --border: #243041;
  --text: #e8eef7;
  --text-muted: #8b9bb4;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --success: #22c55e;
  --danger: #ef4444;
  --radius: 12px;
  --font: 'Inter', system-ui, sans-serif;
  --mono: 'JetBrains Mono', monospace;
}

[data-theme="light"] {
  --bg: #f4f6f9;
  --bg-elevated: #ffffff;
  --bg-card: #ffffff;
  --border: #d8dee8;
  --text: #0f172a;
  --text-muted: #64748b;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
}

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

html, body {
  height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  z-index: 20;
  flex-shrink: 0;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 180px;
}

.logo-icon {
  font-size: 1.6rem;
}

.logo strong {
  display: block;
  font-size: 1.05rem;
  letter-spacing: -0.02em;
}

.tagline {
  font-size: 0.7rem;
  color: var(--text-muted);
}

.search-wrap {
  flex: 1;
  max-width: 520px;
  position: relative;
}

.search-wrap input {
  width: 100%;
  padding: 10px 36px 10px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
  outline: none;
  transition: border-color 0.15s;
}

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

#clearSearch {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.2rem;
  cursor: pointer;
  display: none;
}

.top-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.top-actions button,
.btn-agent {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: background 0.15s, border-color 0.15s;
}

.btn-agent {
  background: linear-gradient(135deg, #1e3a8a, #3b82f6);
  border: none;
  color: white;
  font-weight: 600;
}

.btn-agent:hover {
  filter: brightness(1.1);
}

.btn-update {
  background: linear-gradient(135deg, #065f46, #10b981);
  border: none !important;
  color: white !important;
  font-weight: 600;
}

.btn-update:hover {
  filter: brightness(1.1);
}

.btn-update:disabled {
  opacity: 0.65;
  cursor: wait;
  filter: none;
}

.btn-update.loading .btn-update-icon {
  display: inline-block;
  animation: spin 0.8s linear infinite;
}

@media (max-width: 600px) {
  .btn-update-label {
    display: none;
  }
}

.top-actions button:hover {
  background: var(--border);
}

/* Main layout */
.main {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.sidebar {
  width: 240px;
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  flex-shrink: 0;
  padding: 12px 0;
}

.sidebar-section {
  padding: 8px 12px 16px;
}

.sidebar-section h3 {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 8px;
  padding: 0 4px;
}

.filter-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.filter-btn {
  text-align: left;
  background: transparent;
  border: none;
  color: var(--text);
  padding: 7px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.88rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: background 0.12s;
}

.filter-btn:hover {
  background: var(--bg-card);
}

.filter-btn.active {
  background: var(--accent);
  color: white;
}

.filter-btn .count {
  font-size: 0.75rem;
  opacity: 0.7;
  font-family: var(--mono);
}

.sidebar-footer {
  padding: 16px 14px;
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 0.75rem;
  line-height: 1.4;
}

.sidebar-footer a {
  color: var(--accent);
}

/* Content */
.content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
}

.player-section {
  background: #000;
  flex-shrink: 0;
}

.player-container {
  position: relative;
  width: 100%;
  max-height: 42vh;
  background: #000;
}

#videoPlayer {
  width: 100%;
  max-height: 42vh;
  display: block;
  background: #000;
  aspect-ratio: 16/9;
  object-fit: contain;
}

.player-overlay {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 10px 14px;
  background: linear-gradient(transparent, rgba(0,0,0,0.75));
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 12px;
  pointer-events: none;
}

.now-playing {
  min-width: 0;
  flex: 1;
}

.now-playing span {
  display: block;
}

#nowPlayingName {
  font-weight: 600;
  font-size: 1rem;
}

#nowPlayingMeta {
  font-size: 0.8rem;
  opacity: 0.85;
}

.player-controls-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
  pointer-events: auto;
}

.player-status {
  font-size: 0.75rem;
  font-family: var(--mono);
  opacity: 0.8;
  white-space: nowrap;
}

/* Quality / ABR selector */
.quality-wrap {
  position: relative;
}

.quality-btn {
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.25);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 600;
  font-family: var(--mono);
  padding: 4px 10px;
  border-radius: 6px;
  cursor: pointer;
  backdrop-filter: blur(6px);
  transition: background 0.15s, border-color 0.15s;
}

.quality-btn:hover {
  background: rgba(255, 255, 255, 0.22);
  border-color: rgba(255, 255, 255, 0.4);
}

.quality-menu {
  position: absolute;
  bottom: calc(100% + 6px);
  right: 0;
  min-width: 120px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  z-index: 30;
}

.quality-menu button {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  color: var(--text);
  font-size: 0.82rem;
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-family: var(--font);
}

.quality-menu button:hover {
  background: var(--bg-card);
}

.quality-menu button.active {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

.quality-menu .quality-label {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  padding: 6px 12px 2px;
}

/* Network offline banner (over player) */
.network-banner {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 10px 14px;
  background: rgba(127, 29, 29, 0.92);
  color: #fff;
  font-size: 0.88rem;
  font-weight: 500;
  z-index: 25;
  transform: translateY(-100%);
  opacity: 0;
  transition: transform 0.25s ease, opacity 0.25s ease;
  pointer-events: none;
}

.network-banner.visible {
  transform: translateY(0);
  opacity: 1;
  pointer-events: auto;
}

.network-banner-icon {
  font-size: 1.1rem;
}

.network-retry-btn {
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.4);
  color: #fff;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 4px 12px;
  border-radius: 6px;
  cursor: pointer;
  margin-left: 4px;
}

.network-retry-btn:hover {
  background: rgba(255, 255, 255, 0.35);
}

.channels-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px 6px;
  flex-shrink: 0;
}

.channels-header h2 {
  font-size: 1.05rem;
  font-weight: 600;
}

#channelCount {
  background: var(--bg-card);
  border: 1px solid var(--border);
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-family: var(--mono);
  color: var(--text-muted);
}

.channel-grid {
  flex: 1;
  overflow-y: auto;
  padding: 8px 12px 24px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 10px;
  align-content: start;
}

.channel-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  cursor: pointer;
  transition: transform 0.12s, border-color 0.12s, box-shadow 0.12s;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 110px;
}

.channel-card:hover {
  transform: translateY(-2px);
  border-color: var(--accent);
  box-shadow: 0 6px 20px rgba(0,0,0,0.25);
}

.channel-card.playing {
  border-color: var(--success);
  box-shadow: 0 0 0 1px var(--success);
}

.channel-logo {
  width: 100%;
  height: 48px;
  object-fit: contain;
  border-radius: 6px;
  background: rgba(255,255,255,0.04);
}

.channel-logo.placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  color: var(--text-muted);
}

.channel-name {
  font-size: 0.85rem;
  font-weight: 500;
  line-height: 1.25;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.channel-meta {
  font-size: 0.7rem;
  color: var(--text-muted);
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.channel-meta span {
  background: var(--bg);
  padding: 1px 6px;
  border-radius: 4px;
}

/* Loading */
.loading {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: var(--text-muted);
  gap: 16px;
}

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

.spinner-sm {
  width: 18px;
  height: 18px;
  border-width: 2px;
  display: inline-block;
  vertical-align: middle;
  margin-right: 8px;
}

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

.scan-progress {
  max-width: 360px;
  margin: 0 auto;
  text-align: center;
}

.scan-progress .scan-label {
  font-size: 0.9rem;
}

.scan-progress .scan-meta {
  font-family: var(--mono);
  font-size: 0.8rem;
}

.progress-bar {
  width: 100%;
  height: 8px;
  background: var(--border);
  border-radius: 999px;
  overflow: hidden;
  margin-top: 4px;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #10b981, #3b82f6);
  border-radius: 999px;
  transition: width 0.2s ease;
}

.health-badge {
  font-weight: 600 !important;
}

.health-online {
  background: rgba(16, 185, 129, 0.2) !important;
  color: #34d399 !important;
}

.health-slow {
  background: rgba(245, 158, 11, 0.2) !important;
  color: #fbbf24 !important;
}

.health-offline {
  background: rgba(239, 68, 68, 0.2) !important;
  color: #f87171 !important;
}

.channel-card.is-offline {
  opacity: 0.45;
}

.agent-scan {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* AI Agent Panel */
.agent-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: 380px;
  max-width: 100%;
  height: 100%;
  background: var(--bg-elevated);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.25s ease;
  z-index: 50;
  box-shadow: -8px 0 30px rgba(0,0,0,0.35);
}

.agent-panel.open {
  transform: translateX(0);
}

.agent-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.agent-title {
  display: flex;
  gap: 10px;
  align-items: center;
}

.agent-title strong {
  display: block;
  font-size: 0.95rem;
}

.agent-title small {
  color: var(--text-muted);
  font-size: 0.75rem;
}

#closeAgent {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.4rem;
  cursor: pointer;
  line-height: 1;
}

.agent-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg {
  max-width: 92%;
  padding: 10px 12px;
  border-radius: 12px;
  font-size: 0.9rem;
  line-height: 1.45;
}

.msg.agent {
  background: var(--bg-card);
  border: 1px solid var(--border);
  align-self: flex-start;
}

.msg.user {
  background: var(--accent);
  color: white;
  align-self: flex-end;
}

.msg ul {
  margin: 8px 0 0 16px;
}

.msg .suggestion {
  display: block;
  margin-top: 8px;
  padding: 8px 10px;
  background: var(--bg);
  border-radius: 8px;
  border: 1px solid var(--border);
  cursor: pointer;
  transition: border-color 0.12s;
}

.msg .suggestion:hover {
  border-color: var(--accent);
}

.msg .suggestion strong {
  display: block;
  font-size: 0.88rem;
}

.msg .suggestion span {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.agent-input-wrap {
  display: flex;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#agentInput {
  flex: 1;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 0.9rem;
  outline: none;
}

#agentInput:focus {
  border-color: var(--accent);
}

#agentSend {
  background: var(--accent);
  color: white;
  border: none;
  padding: 0 16px;
  border-radius: 10px;
  font-weight: 600;
  cursor: pointer;
}

#agentSend:hover {
  background: var(--accent-hover);
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 0.9rem;
  z-index: 100;
  opacity: 0;
  transition: transform 0.25s, opacity 0.25s;
  pointer-events: none;
  box-shadow: 0 8px 24px rgba(0,0,0,0.3);
}

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

/* Responsive */
@media (max-width: 900px) {
  .sidebar {
    position: absolute;
    left: 0;
    top: 57px;
    bottom: 0;
    z-index: 30;
    transform: translateX(-100%);
    transition: transform 0.2s;
  }
  .sidebar.open {
    transform: translateX(0);
  }
  .player-container {
    max-height: 36vh;
  }
  #videoPlayer {
    max-height: 36vh;
  }
  .channel-grid {
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  }
}

@media (max-width: 600px) {
  .logo .tagline {
    display: none;
  }
  .btn-agent span:last-child {
    display: none;
  }
  .agent-panel {
    width: 100%;
  }
}
