Aichatbot12321 commited on
Commit
c339e05
·
verified ·
1 Parent(s): 6971c54

Update youtube.html

Browse files
Files changed (1) hide show
  1. youtube.html +262 -129
youtube.html CHANGED
@@ -12,28 +12,36 @@
12
  * { box-sizing: border-box; }
13
  body { font-family: 'Quicksand', sans-serif; margin: 0; padding: 0; background-color: #f0f8ff; color: #2c3e50; min-height: 100vh; overflow-x: hidden; }
14
 
15
- /* SLEEK TOP BAR (100% WIDTH) */
16
- .top-bar {
17
- position: sticky; top: 0; z-index: 100; background: #fff;
18
- padding: 10px 15px; box-shadow: 0 2px 10px rgba(0,0,0,0.08);
19
- display: flex; gap: 10px; align-items: center; width: 100%;
20
- }
21
  .hamburger { background: none; border: none; font-size: 26px; cursor: pointer; color: #0277bd; padding: 0 5px; }
22
 
23
- input[type="text"] { flex-grow: 1; padding: 10px 15px; border: 2px solid #b3e5fc; border-radius: 20px; font-size: 16px; outline: none; font-family: 'Quicksand'; min-width: 0; }
24
- input[type="text"]:focus { border-color: #039be5; }
 
 
 
 
 
25
 
 
 
 
 
 
 
 
26
  .btn-action { padding: 10px 20px; font-size: 15px; font-weight: 700; background: #039be5; color: white; border: none; border-radius: 20px; cursor: pointer; transition: 0.2s; white-space: nowrap; text-align: center; }
27
  .btn-action:hover { background: #0277bd; transform: translateY(-2px); }
28
 
29
- /* FLOATING DRAWER (HIDDEN BY DEFAULT) */
 
 
 
 
 
30
  .drawer-overlay { display: none; position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; background: rgba(0,0,0,0.5); z-index: 1000; backdrop-filter: blur(2px); }
31
- .drawer {
32
- position: fixed; top: 0; left: -280px; width: 260px; height: 100vh;
33
- background: #0277bd; color: white; padding: 20px 10px;
34
- box-shadow: 4px 0 15px rgba(0,0,0,0.2); transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
35
- z-index: 1001; overflow-y: auto; display: flex; flex-direction: column; gap: 8px;
36
- }
37
  .drawer.active { left: 0; }
38
 
39
  .drawer-header { display: flex; justify-content: space-between; align-items: center; padding: 0 10px 15px 10px; }
@@ -46,9 +54,8 @@
46
  .menu-link:hover { background: rgba(255,255,255,0.1); color: white; padding-left: 20px; }
47
  .menu-divider { border-top: 1px solid rgba(255,255,255,0.2); margin: 10px 5px; }
48
 
49
- /* MAIN WORKSPACE (FULL WIDTH) */
50
  .workspace { padding: 15px; width: 100%; max-width: 1000px; margin: 0 auto; }
51
-
52
  #selection-bar { display: none; background: #ab47bc; color: white; padding: 12px 20px; border-radius: 20px; margin-bottom: 15px; text-align: center; font-weight: bold; position: sticky; top: 65px; z-index: 99; box-shadow: 0 4px 10px rgba(0,0,0,0.1); flex-wrap: wrap; gap: 10px; justify-content: center; align-items: center; }
53
 
54
  /* LIST RENDER ANIMATIONS & LAYOUT */
@@ -65,19 +72,22 @@
65
  /* GUI OVERLAYS (MODALS) */
66
  .modal-overlay { display: none; position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; background: rgba(0,0,0,0.6); z-index: 500; backdrop-filter: blur(4px); }
67
  .modal-content { background: white; width: 96%; max-width: 800px; max-height: 90vh; margin: 5vh auto; border-radius: 20px; padding: 20px; overflow-y: auto; overflow-x: hidden; position: relative; box-shadow: 0 10px 30px rgba(0,0,0,0.3); }
68
-
69
  .close-modal { position: absolute; top: 10px; right: 10px; font-size: 22px; cursor: pointer; color: #fff; font-weight: bold; width: 32px; height: 32px; line-height: 32px; text-align: center; background: #e53935; border-radius: 50%; box-shadow: 0 2px 5px rgba(0,0,0,0.2); z-index: 10; border: 2px solid white; transition: 0.2s; }
70
 
71
- .modal-header-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 15px; padding-right: 35px; }
72
  .modal-header-actions .btn-action { flex: 1 1 auto; }
73
 
 
 
 
 
 
74
  .channel-banner { width: 100%; height: 140px; object-fit: cover; border-radius: 12px; margin-bottom: 15px; background: #b3e5fc; }
75
 
76
  #player-overlay { display: none; position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; background: #000; z-index: 2000; }
77
  #close-player { position: absolute; top: 20px; right: 20px; color: white; font-size: 35px; cursor: pointer; z-index: 2100; background: rgba(255,255,255,0.2); width: 50px; height: 50px; line-height: 45px; text-align: center; border-radius: 50%; }
78
  #player-target { width: 100%; height: 100%; display: block; border: none; }
79
 
80
- /* MOBILE OPTIMIZATIONS */
81
  @media (max-width: 600px) {
82
  .result-item { flex-direction: column; align-items: flex-start; padding: 12px; }
83
  .thumb-wrapper { width: 100%; }
@@ -88,31 +98,35 @@
88
  </head>
89
  <body>
90
 
91
- <!-- SLEEK TOP BAR -->
92
  <div class="top-bar">
93
  <button class="hamburger" onclick="toggleDrawer()">☰</button>
94
- <input type="text" id="searchInput" placeholder="Search YouTube...">
95
- <button class="btn-action" onclick="searchYT(false)">Search</button>
 
 
 
 
96
  </div>
97
 
98
- <!-- FLOATING DRAWER SYSTEM -->
 
 
 
 
 
 
 
99
  <div id="drawer-overlay" class="drawer-overlay" onclick="toggleDrawer()"></div>
100
  <div class="drawer" id="app-drawer">
101
  <div class="drawer-header">
102
  <h3>✦ STUDIO</h3>
103
  <button class="close-drawer" onclick="toggleDrawer()">✖</button>
104
  </div>
105
-
106
- <button class="menu-btn" onclick="openModal('playlist-modal'); loadCustomPlaylists(); toggleDrawer();">
107
- <span>📁</span> My Playlists
108
- </button>
109
- <button class="menu-btn" onclick="openModal('history-modal'); loadHistory(); toggleDrawer();" style="background: rgba(38, 166, 154, 0.4);">
110
- <span>🕒</span> Watch History
111
- </button>
112
-
113
  <div class="menu-divider"></div>
114
  <h4 style="margin: 5px 15px; color: #81d4fa;">Apps & Tools</h4>
115
-
116
  <a href="index.html" class="menu-link">🏠 Main Index</a>
117
  <a href="search.html" class="menu-link">🔍 Web Search</a>
118
  <a href="chess.html" class="menu-link">♟️ Chess Engine</a>
@@ -127,16 +141,26 @@
127
  <a href="translator.html" class="menu-link">🌐 Translator</a>
128
  </div>
129
 
130
- <!-- MAIN WORKSPACE -->
131
  <div class="workspace">
132
  <div id="selection-bar">
133
  <span id="sel-count">0</span> Items Marked
134
  <button style="background:white; color:#ab47bc; border:none; padding:8px 16px; border-radius:20px; font-weight:bold; cursor:pointer;" onclick="initiateAddToPlaylist()">Transfer Selected</button>
135
  </div>
136
-
137
  <div id="results"></div>
138
  <div id="load-more-container" style="text-align:center; padding: 20px; display:none;">
139
- <button class="btn-action" style="background:#26a69a;" onclick="searchYT(true)">Load Next Page</button>
 
 
 
 
 
 
 
 
 
 
 
140
  </div>
141
  </div>
142
 
@@ -152,7 +176,7 @@
152
  <div id="playlist-modal" class="modal-overlay">
153
  <div class="modal-content">
154
  <span class="close-modal" onclick="closeModal('playlist-modal')">&#10006;</span>
155
- <div class="modal-header-actions">
156
  <button class="btn-action" style="background:#ab47bc;" onclick="openModal('create-playlist-modal')">+ Create New</button>
157
  </div>
158
  <h3 style="color:#ab47bc; margin: 0 0 15px 0;">Playlist Registry</h3>
@@ -186,6 +210,8 @@
186
  <span class="close-modal" onclick="closeModal('yt-playlist-modal')">&#10006;</span>
187
  <div class="modal-header-actions">
188
  <button class="btn-action" style="background:#26a69a;" onclick="playYTPlaylistMode(true)">Play All</button>
 
 
189
  <button class="btn-action" style="background:#26a69a;" onclick="playYTPlaylistMode(false)">Play Selected</button>
190
  <button class="btn-action" style="background:#ab47bc;" onclick="initiateAddToPlaylist()">Transfer Selected</button>
191
  </div>
@@ -216,29 +242,147 @@
216
  let selectedVideos = [];
217
  let currentSearchQuery = "";
218
  let nextPageToken = "";
 
219
 
220
  // UI Helpers
221
- function toggleDrawer() {
222
- const drawer = document.getElementById('app-drawer');
223
- const overlay = document.getElementById('drawer-overlay');
224
- if(drawer.classList.contains('active')) {
225
- drawer.classList.remove('active');
226
- overlay.style.display = 'none';
227
- } else {
228
- drawer.classList.add('active');
229
- overlay.style.display = 'block';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
  }
232
 
233
- function openModal(id) { document.getElementById(id).style.display = 'block'; }
234
- function closeModal(id) { document.getElementById(id).style.display = 'none'; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
235
 
236
- // CRITICAL FIX: Removed hardcoded localhost. Using relative paths for HuggingFace.
237
- async function searchYT(isLoadMore = false) {
238
- const query = document.getElementById('searchInput').value;
239
  if(!query) return;
240
- if(!isLoadMore) { selectedVideos = []; updateSelectionBar(); currentSearchQuery = query; nextPageToken = ""; document.getElementById('results').innerHTML = "<h3>Querying Server...</h3>"; }
 
 
 
 
 
 
241
  document.getElementById('load-more-container').style.display = 'none';
 
242
  try {
243
  const res = await fetch('/api/youtube-search', {
244
  method: 'POST',
@@ -260,7 +404,7 @@
260
  const clickAction = isVid ? `playVid('${safeData}')` : `openYoutubePlaylist('${item.id}', '${item.title.replace(/'/g, "\\'")}')`;
261
 
262
  resultsDiv.innerHTML += `
263
- <div class="result-item" style="border-left-color: ${isVid ? '#29b6f6' : '#ab47bc'};">
264
  <input type="checkbox" class="vid-checkbox" onchange="toggleSelect(this, '${safeData}')">
265
  <div class="thumb-wrapper" onclick="${clickAction}">
266
  <img class="thumb" src="${item.thumb}">
@@ -274,11 +418,14 @@
274
  </div>
275
  </div>`;
276
  });
 
 
 
277
  nextPageToken = data.nextPageToken;
278
  document.getElementById('load-more-container').style.display = nextPageToken ? 'block' : 'none';
279
  } catch(e) {
280
  console.error(e);
281
- document.getElementById('results').innerHTML = `<h3 style="color:red;">Error connecting to API.</h3>`;
282
  }
283
  }
284
 
@@ -297,32 +444,29 @@
297
  } else { bar.style.display = 'none'; }
298
  }
299
 
 
300
  let currentYTPlaylistVids = [];
 
 
301
  async function openYoutubePlaylist(pid, title) {
302
  if(!pid) return;
303
  openModal('yt-playlist-modal');
304
  document.getElementById('yt-playlist-title').innerText = title;
305
  const contentDiv = document.getElementById('yt-playlist-content');
306
  contentDiv.innerHTML = "<p>Loading playlist items...</p>";
307
- currentYTPlaylistVids = [];
308
 
309
  try {
310
- // CRITICAL FIX: Relative Path
311
  const res = await fetch(`/api/youtube-playlist-items?id=${pid}`);
312
  const data = await res.json();
313
  contentDiv.innerHTML = "";
314
-
315
- if(!data.videos || data.videos.length === 0) {
316
- contentDiv.innerHTML = "<p>No videos found or playlist is private.</p>";
317
- return;
318
- }
319
 
320
  currentYTPlaylistVids = data.videos;
321
-
322
  data.videos.forEach((item, index) => {
323
  const safeData = encodeURIComponent(JSON.stringify(item));
324
  contentDiv.innerHTML += `
325
- <div class="result-item">
326
  <input type="checkbox" class="vid-checkbox yt-pl-checkbox" data-index="${index}" onchange="toggleSelect(this, '${safeData}')">
327
  <div class="thumb-wrapper" onclick="playVid('${safeData}')">
328
  <img class="thumb" src="${item.thumb}">
@@ -333,63 +477,37 @@
333
  </div>
334
  </div>`;
335
  });
336
- } catch(e) {
337
- contentDiv.innerHTML = "<p>Failed to load playlist contents.</p>";
338
- }
 
 
 
 
 
 
 
 
 
339
  }
340
 
341
  function playYTPlaylistMode(playAll) {
342
  if(currentYTPlaylistVids.length === 0) return;
343
  let targetArray = [];
344
- if(playAll) {
345
- targetArray = currentYTPlaylistVids.map(v => v.id);
346
- } else {
347
- document.querySelectorAll('.yt-pl-checkbox').forEach(cb => {
348
- if(cb.checked) targetArray.push(currentYTPlaylistVids[cb.getAttribute('data-index')].id);
349
- });
350
- }
351
- if(targetArray.length === 0) { alert("No videos selected!"); return; }
352
 
 
353
  document.getElementById('player-overlay').style.display = 'block';
354
  rebuildPlayerDOM();
355
 
356
  ytPlayer = new YT.Player('player-target', {
357
- playerVars: { playlist: targetArray.join(',') },
358
- events: { 'onReady': (e) => e.target.playVideo() }
359
  });
360
  }
361
 
362
- async function loadChannel(id) {
363
- if(!id) return;
364
- openModal('channel-modal');
365
- const headerDiv = document.getElementById('channel-header-view');
366
- const contentDiv = document.getElementById('channel-content');
367
- contentDiv.innerHTML = "<p>Calling Channel API Nodes...</p>";
368
- try {
369
- // CRITICAL FIX: Relative Path
370
- const res = await fetch(`/api/youtube-channel?id=${id}`);
371
- const data = await res.json();
372
- let bannerHtml = data.info.banner ? `<img class="channel-banner" src="${data.info.banner}">` : `<div class="channel-banner"></div>`;
373
- headerDiv.innerHTML = `
374
- <div style="text-align:center; padding-bottom:15px; border-bottom:1px solid #ddd;">
375
- ${bannerHtml}
376
- <h2 style="margin:5px 0 0 0;">${data.info.title}</h2>
377
- <p style="color:#666; margin:5px 0;">${Number(data.info.subs).toLocaleString()} Subscribers</p>
378
- </div>`;
379
- contentDiv.innerHTML = "";
380
- data.videos.forEach(item => {
381
- const safeData = encodeURIComponent(JSON.stringify(item));
382
- contentDiv.innerHTML += `
383
- <div class="result-item">
384
- <div class="thumb-wrapper" onclick="playVid('${safeData}')">
385
- <img class="thumb" src="${item.thumb}">
386
- </div>
387
- <div class="info-col"><div class="title" onclick="playVid('${safeData}')">${item.title}</div></div>
388
- </div>`;
389
- });
390
- } catch(e) { contentDiv.innerHTML = "<p>Channel data fetch failure.</p>"; }
391
- }
392
-
393
  async function getPlaylists() { return await localforage.getItem('custom_playlists') || {}; }
394
 
395
  async function createNewPlaylist() {
@@ -399,8 +517,8 @@
399
  if(!p[name]) { p[name] = []; await localforage.setItem('custom_playlists', p); }
400
  document.getElementById('newPlaylistName').value = "";
401
  closeModal('create-playlist-modal');
402
- if(document.getElementById('add-to-gui-modal').style.display === 'block') { initiateAddToPlaylist(); }
403
- else { loadCustomPlaylists(); }
404
  }
405
 
406
  async function initiateAddToPlaylist() {
@@ -409,7 +527,7 @@
409
  openModal('add-to-gui-modal');
410
  if(keys.length === 0) { content.innerHTML = "<p>No entries. Please create a playlist first.</p>"; }
411
  else {
412
- let html = "<p style='margin-bottom:15px;'>Choose local target array:</p>";
413
  keys.forEach(k => { html += `<div style="background:#f3e5f5; padding:15px; margin-bottom:10px; border-radius:10px; cursor:pointer;" onclick="saveSelectedTo('${k.replace(/'/g, "\\'")}')">📁 <b>${k}</b></div>`; });
414
  content.innerHTML = html;
415
  }
@@ -417,7 +535,7 @@
417
 
418
  async function saveSelectedTo(playlistName) {
419
  let p = await getPlaylists();
420
- selectedVideos.forEach(vid => { if(!p[playlistName].find(v => v.id === vid.id)) { p[playlistName].push(vid); } });
421
  await localforage.setItem('custom_playlists', p);
422
  selectedVideos = [];
423
  document.querySelectorAll('.vid-checkbox').forEach(cb => cb.checked = false);
@@ -442,11 +560,13 @@
442
  });
443
  }
444
 
 
445
  async function viewPlaylistContents(name) {
446
  let p = await getPlaylists(); let vids = p[name];
447
  document.getElementById('playlist-list').style.display = 'none';
448
  const viewDiv = document.getElementById('playlist-view');
449
  viewDiv.style.display = 'block';
 
450
 
451
  let html = `
452
  <div style="margin-bottom: 20px;">
@@ -456,6 +576,7 @@
456
  </div>
457
  <div class="modal-header-actions" style="padding-right:0;">
458
  <button class="btn-action" style="background:#26a69a;" onclick="playMultipleFromPlaylist('${name.replace(/'/g, "\\'")}', true)">Play Sequential</button>
 
459
  <button class="btn-action" style="background:#26a69a;" onclick="playMultipleFromPlaylist('${name.replace(/'/g, "\\'")}', false)">Play Flagged Only</button>
460
  <button class="btn-action" style="background:#e53935;" onclick="deleteEntirePlaylist('${name.replace(/'/g, "\\'")}')">Delete Stack</button>
461
  </div>
@@ -467,7 +588,7 @@
467
  const safeData = encodeURIComponent(JSON.stringify(item));
468
  html += `
469
  <div class="result-item" style="padding:10px;">
470
- <input type="checkbox" class="vid-checkbox pl-checkbox" data-index="${index}">
471
  <div class="thumb-wrapper" onclick="playVid('${safeData}')">
472
  <img class="thumb" src="${item.thumb}" style="width:100px;height:56px;"><div class="dur-badge">${item.duration || ''}</div>
473
  </div>
@@ -478,40 +599,48 @@
478
  viewDiv.innerHTML = html;
479
  }
480
 
481
- async function deleteEntirePlaylist(name) {
482
- let p = await getPlaylists(); delete p[name];
483
- await localforage.setItem('custom_playlists', p); loadCustomPlaylists();
484
- }
485
-
486
- async function removeFromPlaylist(playlistName, index) {
487
- let p = await getPlaylists(); p[playlistName].splice(index, 1);
488
- await localforage.setItem('custom_playlists', p); viewPlaylistContents(playlistName);
489
  }
490
 
491
  async function playMultipleFromPlaylist(playlistName, playAll) {
492
  let p = await getPlaylists(); let vids = p[playlistName]; if(vids.length === 0) return;
493
  let targetArray = [];
494
  if(playAll) { targetArray = vids.map(v => v.id); }
495
- else {
496
- document.querySelectorAll('.pl-checkbox').forEach(cb => { if(cb.checked) targetArray.push(vids[cb.getAttribute('data-index')].id); });
497
- }
498
  if(targetArray.length === 0) return;
499
 
500
  document.getElementById('player-overlay').style.display = 'block';
501
  rebuildPlayerDOM();
502
 
503
  ytPlayer = new YT.Player('player-target', {
504
- playerVars: { playlist: targetArray.join(',') },
505
- events: { 'onReady': (e) => e.target.playVideo() }
506
  });
507
  }
508
 
 
509
  function rebuildPlayerDOM() {
510
  if (ytPlayer) { try { ytPlayer.destroy(); } catch(e){} ytPlayer = null; }
511
  const container = document.getElementById('player-overlay');
512
  container.innerHTML = '<div id="close-player" onclick="closePlayer()">&#10006;</div><div id="player-target"></div>';
513
  }
514
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
515
  function playVid(safeData) {
516
  const data = JSON.parse(decodeURIComponent(safeData));
517
  currentVideoData = data;
@@ -522,15 +651,19 @@
522
  if (document.documentElement.requestFullscreen) { document.documentElement.requestFullscreen().catch(e=>{}); }
523
  if (screen.orientation && screen.orientation.lock) { screen.orientation.lock('landscape').catch(e=>{}); }
524
 
 
 
 
525
  if (data.type === 'playlist' && data.id) {
526
  ytPlayer = new YT.Player('player-target', {
527
- playerVars: { listType: 'playlist', list: data.id },
528
- events: { 'onReady': (e) => { e.target.playVideo(); } }
529
  });
530
  } else if (data.id) {
531
  ytPlayer = new YT.Player('player-target', {
532
  videoId: data.id,
533
- events: { 'onReady': (e) => { e.target.playVideo(); } }
 
534
  });
535
  }
536
  startSavingHistory();
@@ -569,7 +702,7 @@
569
  </div>
570
  <div class="info-col">
571
  <div class="title" onclick="playVid('${safeData}')">${val.title}</div>
572
- <div style="font-size:0.8em; color:#666;">Offset: ${Math.floor(val.timeWatched)}s | ${val.lastWatched}</div>
573
  </div>
574
  </div>`;
575
  });
 
12
  * { box-sizing: border-box; }
13
  body { font-family: 'Quicksand', sans-serif; margin: 0; padding: 0; background-color: #f0f8ff; color: #2c3e50; min-height: 100vh; overflow-x: hidden; }
14
 
15
+ /* SLEEK TOP BAR */
16
+ .top-bar { position: sticky; top: 0; z-index: 100; background: #fff; padding: 10px 15px; box-shadow: 0 2px 10px rgba(0,0,0,0.08); display: flex; gap: 10px; align-items: center; width: 100%; }
 
 
 
 
17
  .hamburger { background: none; border: none; font-size: 26px; cursor: pointer; color: #0277bd; padding: 0 5px; }
18
 
19
+ /* SEARCH & SUGGESTIONS */
20
+ .search-wrapper { flex-grow: 1; position: relative; display: flex; align-items: center; background: #fff; border: 2px solid #b3e5fc; border-radius: 20px; padding-right: 10px; }
21
+ .search-wrapper:focus-within { border-color: #039be5; }
22
+ input[type="text"] { flex-grow: 1; padding: 10px 15px; border: none; background: transparent; font-size: 16px; outline: none; font-family: 'Quicksand'; min-width: 0; }
23
+
24
+ .mic-btn { background: none; border: none; cursor: pointer; font-size: 18px; color: #0277bd; display: flex; align-items: center; justify-content: center; padding: 5px; }
25
+ .mic-btn:hover { color: #e53935; }
26
 
27
+ .search-dropdown { display: none; position: absolute; top: 100%; left: 0; right: 0; background: white; border-radius: 15px; box-shadow: 0 5px 15px rgba(0,0,0,0.1); margin-top: 5px; max-height: 250px; overflow-y: auto; z-index: 101; border: 1px solid #e1f5fe; }
28
+ .suggestion-item { display: flex; justify-content: space-between; align-items: center; padding: 10px 15px; cursor: pointer; border-bottom: 1px solid #f0f8ff; transition: 0.2s; }
29
+ .suggestion-item:hover { background: #e1f5fe; }
30
+ .suggestion-text { flex-grow: 1; font-weight: 600; }
31
+ .suggestion-del { color: #90a4ae; background: none; border: none; font-size: 16px; padding: 5px; cursor: pointer; }
32
+ .suggestion-del:hover { color: #e53935; }
33
+
34
  .btn-action { padding: 10px 20px; font-size: 15px; font-weight: 700; background: #039be5; color: white; border: none; border-radius: 20px; cursor: pointer; transition: 0.2s; white-space: nowrap; text-align: center; }
35
  .btn-action:hover { background: #0277bd; transform: translateY(-2px); }
36
 
37
+ /* FILTER CHIPS */
38
+ .filter-container { display: flex; gap: 10px; padding: 10px 15px; background: #e1f5fe; justify-content: center; }
39
+ .filter-chip { background: white; border: 2px solid #81d4fa; color: #0277bd; padding: 6px 15px; border-radius: 20px; font-weight: 600; cursor: pointer; transition: 0.2s; font-family: 'Quicksand'; }
40
+ .filter-chip.active { background: #0277bd; color: white; border-color: #0277bd; }
41
+
42
+ /* FLOATING DRAWER */
43
  .drawer-overlay { display: none; position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; background: rgba(0,0,0,0.5); z-index: 1000; backdrop-filter: blur(2px); }
44
+ .drawer { position: fixed; top: 0; left: -280px; width: 260px; height: 100vh; background: #0277bd; color: white; padding: 20px 10px; box-shadow: 4px 0 15px rgba(0,0,0,0.2); transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1); z-index: 1001; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; }
 
 
 
 
 
45
  .drawer.active { left: 0; }
46
 
47
  .drawer-header { display: flex; justify-content: space-between; align-items: center; padding: 0 10px 15px 10px; }
 
54
  .menu-link:hover { background: rgba(255,255,255,0.1); color: white; padding-left: 20px; }
55
  .menu-divider { border-top: 1px solid rgba(255,255,255,0.2); margin: 10px 5px; }
56
 
57
+ /* MAIN WORKSPACE */
58
  .workspace { padding: 15px; width: 100%; max-width: 1000px; margin: 0 auto; }
 
59
  #selection-bar { display: none; background: #ab47bc; color: white; padding: 12px 20px; border-radius: 20px; margin-bottom: 15px; text-align: center; font-weight: bold; position: sticky; top: 65px; z-index: 99; box-shadow: 0 4px 10px rgba(0,0,0,0.1); flex-wrap: wrap; gap: 10px; justify-content: center; align-items: center; }
60
 
61
  /* LIST RENDER ANIMATIONS & LAYOUT */
 
72
  /* GUI OVERLAYS (MODALS) */
73
  .modal-overlay { display: none; position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; background: rgba(0,0,0,0.6); z-index: 500; backdrop-filter: blur(4px); }
74
  .modal-content { background: white; width: 96%; max-width: 800px; max-height: 90vh; margin: 5vh auto; border-radius: 20px; padding: 20px; overflow-y: auto; overflow-x: hidden; position: relative; box-shadow: 0 10px 30px rgba(0,0,0,0.3); }
 
75
  .close-modal { position: absolute; top: 10px; right: 10px; font-size: 22px; cursor: pointer; color: #fff; font-weight: bold; width: 32px; height: 32px; line-height: 32px; text-align: center; background: #e53935; border-radius: 50%; box-shadow: 0 2px 5px rgba(0,0,0,0.2); z-index: 10; border: 2px solid white; transition: 0.2s; }
76
 
77
+ .modal-header-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 15px; padding-right: 35px; align-items: center; }
78
  .modal-header-actions .btn-action { flex: 1 1 auto; }
79
 
80
+ /* VOICE MODAL */
81
+ .voice-circle { width: 100px; height: 100px; border-radius: 50%; background: #e53935; margin: 30px auto; cursor: pointer; display: flex; align-items: center; justify-content: center; font-size: 40px; color: white; box-shadow: 0 4px 15px rgba(229, 57, 53, 0.4); transition: 0.2s; }
82
+ .voice-circle.recording { animation: pulse 1.5s infinite; }
83
+ @keyframes pulse { 0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(229, 57, 53, 0.7); } 70% { transform: scale(1.1); box-shadow: 0 0 0 20px rgba(229, 57, 53, 0); } 100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(229, 57, 53, 0); } }
84
+
85
  .channel-banner { width: 100%; height: 140px; object-fit: cover; border-radius: 12px; margin-bottom: 15px; background: #b3e5fc; }
86
 
87
  #player-overlay { display: none; position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; background: #000; z-index: 2000; }
88
  #close-player { position: absolute; top: 20px; right: 20px; color: white; font-size: 35px; cursor: pointer; z-index: 2100; background: rgba(255,255,255,0.2); width: 50px; height: 50px; line-height: 45px; text-align: center; border-radius: 50%; }
89
  #player-target { width: 100%; height: 100%; display: block; border: none; }
90
 
 
91
  @media (max-width: 600px) {
92
  .result-item { flex-direction: column; align-items: flex-start; padding: 12px; }
93
  .thumb-wrapper { width: 100%; }
 
98
  </head>
99
  <body>
100
 
101
+ <!-- TOP BAR -->
102
  <div class="top-bar">
103
  <button class="hamburger" onclick="toggleDrawer()">☰</button>
104
+ <div class="search-wrapper">
105
+ <input type="text" id="searchInput" placeholder="Search YouTube..." autocomplete="off">
106
+ <button class="mic-btn" onclick="openVoiceModal()" title="Voice Search">🎤</button>
107
+ <div class="search-dropdown" id="searchDropdown"></div>
108
+ </div>
109
+ <button class="btn-action" onclick="executeSearch(false)">Search</button>
110
  </div>
111
 
112
+ <!-- FILTER CHIPS -->
113
+ <div class="filter-container">
114
+ <button class="filter-chip active" onclick="setSearchFilter('all', this)">All</button>
115
+ <button class="filter-chip" onclick="setSearchFilter('video', this)">Videos</button>
116
+ <button class="filter-chip" onclick="setSearchFilter('playlist', this)">Playlists</button>
117
+ </div>
118
+
119
+ <!-- DRAWER -->
120
  <div id="drawer-overlay" class="drawer-overlay" onclick="toggleDrawer()"></div>
121
  <div class="drawer" id="app-drawer">
122
  <div class="drawer-header">
123
  <h3>✦ STUDIO</h3>
124
  <button class="close-drawer" onclick="toggleDrawer()">✖</button>
125
  </div>
126
+ <button class="menu-btn" onclick="openModal('playlist-modal'); loadCustomPlaylists(); toggleDrawer();"><span>📁</span> My Playlists</button>
127
+ <button class="menu-btn" onclick="openModal('history-modal'); loadHistory(); toggleDrawer();" style="background: rgba(38, 166, 154, 0.4);"><span>🕒</span> Watch History</button>
 
 
 
 
 
 
128
  <div class="menu-divider"></div>
129
  <h4 style="margin: 5px 15px; color: #81d4fa;">Apps & Tools</h4>
 
130
  <a href="index.html" class="menu-link">🏠 Main Index</a>
131
  <a href="search.html" class="menu-link">🔍 Web Search</a>
132
  <a href="chess.html" class="menu-link">♟️ Chess Engine</a>
 
141
  <a href="translator.html" class="menu-link">🌐 Translator</a>
142
  </div>
143
 
144
+ <!-- WORKSPACE -->
145
  <div class="workspace">
146
  <div id="selection-bar">
147
  <span id="sel-count">0</span> Items Marked
148
  <button style="background:white; color:#ab47bc; border:none; padding:8px 16px; border-radius:20px; font-weight:bold; cursor:pointer;" onclick="initiateAddToPlaylist()">Transfer Selected</button>
149
  </div>
 
150
  <div id="results"></div>
151
  <div id="load-more-container" style="text-align:center; padding: 20px; display:none;">
152
+ <button class="btn-action" style="background:#26a69a;" onclick="executeSearch(true)">Load Next Page</button>
153
+ </div>
154
+ </div>
155
+
156
+ <!-- VOICE MODAL -->
157
+ <div id="voice-modal" class="modal-overlay" style="z-index: 3000;">
158
+ <div class="modal-content" style="max-width: 400px; text-align: center;">
159
+ <span class="close-modal" onclick="closeVoiceModal()">&#10006;</span>
160
+ <h3 style="color:#e53935; margin-top:0;">Voice Search</h3>
161
+ <p id="voice-status">Tap the mic and speak...</p>
162
+ <div class="voice-circle" id="voice-btn" onclick="toggleVoiceRecording()">🎤</div>
163
+ <h4 id="voice-result" style="min-height: 25px; color: #0277bd;"></h4>
164
  </div>
165
  </div>
166
 
 
176
  <div id="playlist-modal" class="modal-overlay">
177
  <div class="modal-content">
178
  <span class="close-modal" onclick="closeModal('playlist-modal')">&#10006;</span>
179
+ <div class="modal-header-actions" style="margin-bottom: 20px;">
180
  <button class="btn-action" style="background:#ab47bc;" onclick="openModal('create-playlist-modal')">+ Create New</button>
181
  </div>
182
  <h3 style="color:#ab47bc; margin: 0 0 15px 0;">Playlist Registry</h3>
 
210
  <span class="close-modal" onclick="closeModal('yt-playlist-modal')">&#10006;</span>
211
  <div class="modal-header-actions">
212
  <button class="btn-action" style="background:#26a69a;" onclick="playYTPlaylistMode(true)">Play All</button>
213
+ <!-- FIXED SELECT ALL PLACEMENT -->
214
+ <button class="btn-action" style="background:#ffca28; color:#333;" onclick="toggleSelectAllYTPlaylist()">☑ Select All</button>
215
  <button class="btn-action" style="background:#26a69a;" onclick="playYTPlaylistMode(false)">Play Selected</button>
216
  <button class="btn-action" style="background:#ab47bc;" onclick="initiateAddToPlaylist()">Transfer Selected</button>
217
  </div>
 
242
  let selectedVideos = [];
243
  let currentSearchQuery = "";
244
  let nextPageToken = "";
245
+ let currentFilter = 'all'; // all | video | playlist
246
 
247
  // UI Helpers
248
+ function toggleDrawer() { document.getElementById('app-drawer').classList.toggle('active'); document.getElementById('drawer-overlay').style.display = document.getElementById('app-drawer').classList.contains('active') ? 'block' : 'none'; }
249
+ function openModal(id) { document.getElementById(id).style.display = 'block'; }
250
+ function closeModal(id) { document.getElementById(id).style.display = 'none'; }
251
+ window.onclick = function(event) { if (event.target.classList.contains('modal-overlay')) event.target.style.display = "none"; }
252
+
253
+ // --- SEARCH SUGGESTIONS LOGIC ---
254
+ const searchInput = document.getElementById('searchInput');
255
+ const searchDropdown = document.getElementById('searchDropdown');
256
+
257
+ searchInput.addEventListener('focus', renderSearchHistory);
258
+ searchInput.addEventListener('keyup', renderSearchHistory);
259
+ document.addEventListener('click', (e) => { if(!e.target.closest('.search-wrapper')) searchDropdown.style.display = 'none'; });
260
+
261
+ function saveSearchToHistory(query) {
262
+ let hist = JSON.parse(localStorage.getItem('yt_search_history') || '[]');
263
+ hist = hist.filter(q => q.toLowerCase() !== query.toLowerCase()); // Remove dupes
264
+ hist.unshift(query);
265
+ if(hist.length > 10) hist.pop(); // Keep last 10
266
+ localStorage.setItem('yt_search_history', JSON.stringify(hist));
267
+ }
268
+
269
+ function renderSearchHistory() {
270
+ const val = searchInput.value.toLowerCase();
271
+ let hist = JSON.parse(localStorage.getItem('yt_search_history') || '[]');
272
+ if(val) hist = hist.filter(q => q.toLowerCase().includes(val));
273
+
274
+ if(hist.length === 0) { searchDropdown.style.display = 'none'; return; }
275
+
276
+ searchDropdown.innerHTML = '';
277
+ hist.forEach(q => {
278
+ searchDropdown.innerHTML += `
279
+ <div class="suggestion-item">
280
+ <div class="suggestion-text" onclick="searchInput.value='${q}'; executeSearch(false);">🕒 ${q}</div>
281
+ <button class="suggestion-del" onclick="deleteSearchItem('${q}')">&#10006;</button>
282
+ </div>`;
283
+ });
284
+ searchDropdown.style.display = 'block';
285
+ }
286
+
287
+ function deleteSearchItem(query) {
288
+ let hist = JSON.parse(localStorage.getItem('yt_search_history') || '[]');
289
+ hist = hist.filter(q => q !== query);
290
+ localStorage.setItem('yt_search_history', JSON.stringify(hist));
291
+ renderSearchHistory();
292
+ }
293
+
294
+ // --- VOICE SEARCH LOGIC ---
295
+ let recognition = null;
296
+ let isRecording = false;
297
+
298
+ function openVoiceModal() {
299
+ openModal('voice-modal');
300
+ document.getElementById('voice-result').innerText = "";
301
+ document.getElementById('voice-btn').classList.remove('recording');
302
+ document.getElementById('voice-status').innerText = "Tap the mic and speak in English...";
303
+ isRecording = false;
304
+ }
305
+
306
+ function closeVoiceModal() {
307
+ closeModal('voice-modal');
308
+ if(recognition) { recognition.stop(); isRecording = false; }
309
+ }
310
+
311
+ function toggleVoiceRecording() {
312
+ if (!('webkitSpeechRecognition' in window) && !('SpeechRecognition' in window)) {
313
+ alert("Voice search is not supported in this browser."); return;
314
+ }
315
+ if(isRecording) {
316
+ recognition.stop();
317
+ return;
318
  }
319
+
320
+ const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
321
+ recognition = new SpeechRecognition();
322
+ recognition.lang = 'en-US';
323
+ recognition.interimResults = true;
324
+
325
+ recognition.onstart = function() {
326
+ isRecording = true;
327
+ document.getElementById('voice-btn').classList.add('recording');
328
+ document.getElementById('voice-status').innerText = "Listening...";
329
+ };
330
+
331
+ recognition.onresult = function(event) {
332
+ let text = event.results[0][0].transcript;
333
+ document.getElementById('voice-result').innerText = text;
334
+ if(event.results[0].isFinal) {
335
+ searchInput.value = text;
336
+ setTimeout(() => { closeVoiceModal(); executeSearch(false); }, 1000);
337
+ }
338
+ };
339
+
340
+ recognition.onerror = function(e) {
341
+ document.getElementById('voice-status').innerText = "Error: " + e.error;
342
+ document.getElementById('voice-btn').classList.remove('recording');
343
+ isRecording = false;
344
+ };
345
+
346
+ recognition.onend = function() {
347
+ document.getElementById('voice-btn').classList.remove('recording');
348
+ isRecording = false;
349
+ };
350
+
351
+ recognition.start();
352
  }
353
 
354
+ // --- SEARCH & FILTER LOGIC ---
355
+ function setSearchFilter(type, btnElement) {
356
+ currentFilter = type;
357
+ document.querySelectorAll('.filter-chip').forEach(b => b.classList.remove('active'));
358
+ btnElement.classList.add('active');
359
+ applyFilterToDOM();
360
+ }
361
+
362
+ function applyFilterToDOM() {
363
+ const items = document.querySelectorAll('#results .result-item');
364
+ items.forEach(item => {
365
+ const itemType = item.getAttribute('data-type');
366
+ if(currentFilter === 'all' || currentFilter === itemType) {
367
+ item.style.display = 'flex';
368
+ } else {
369
+ item.style.display = 'none';
370
+ }
371
+ });
372
+ }
373
 
374
+ async function executeSearch(isLoadMore = false) {
375
+ const query = searchInput.value.trim();
 
376
  if(!query) return;
377
+
378
+ searchDropdown.style.display = 'none';
379
+ if(!isLoadMore) {
380
+ saveSearchToHistory(query);
381
+ selectedVideos = []; updateSelectionBar(); currentSearchQuery = query; nextPageToken = "";
382
+ document.getElementById('results').innerHTML = "<h3>Querying Server...</h3>";
383
+ }
384
  document.getElementById('load-more-container').style.display = 'none';
385
+
386
  try {
387
  const res = await fetch('/api/youtube-search', {
388
  method: 'POST',
 
404
  const clickAction = isVid ? `playVid('${safeData}')` : `openYoutubePlaylist('${item.id}', '${item.title.replace(/'/g, "\\'")}')`;
405
 
406
  resultsDiv.innerHTML += `
407
+ <div class="result-item" data-type="${item.type}" style="border-left-color: ${isVid ? '#29b6f6' : '#ab47bc'};">
408
  <input type="checkbox" class="vid-checkbox" onchange="toggleSelect(this, '${safeData}')">
409
  <div class="thumb-wrapper" onclick="${clickAction}">
410
  <img class="thumb" src="${item.thumb}">
 
418
  </div>
419
  </div>`;
420
  });
421
+
422
+ applyFilterToDOM(); // Apply current filter immediately
423
+
424
  nextPageToken = data.nextPageToken;
425
  document.getElementById('load-more-container').style.display = nextPageToken ? 'block' : 'none';
426
  } catch(e) {
427
  console.error(e);
428
+ document.getElementById('results').innerHTML = `<h3 style="color:red;">Error connecting to API. Check if server.js is running.</h3>`;
429
  }
430
  }
431
 
 
444
  } else { bar.style.display = 'none'; }
445
  }
446
 
447
+ // --- YOUTUBE PLAYLIST VIEWER ---
448
  let currentYTPlaylistVids = [];
449
+ let ytSelectAllState = false;
450
+
451
  async function openYoutubePlaylist(pid, title) {
452
  if(!pid) return;
453
  openModal('yt-playlist-modal');
454
  document.getElementById('yt-playlist-title').innerText = title;
455
  const contentDiv = document.getElementById('yt-playlist-content');
456
  contentDiv.innerHTML = "<p>Loading playlist items...</p>";
457
+ currentYTPlaylistVids = []; ytSelectAllState = false;
458
 
459
  try {
 
460
  const res = await fetch(`/api/youtube-playlist-items?id=${pid}`);
461
  const data = await res.json();
462
  contentDiv.innerHTML = "";
463
+ if(!data.videos || data.videos.length === 0) { contentDiv.innerHTML = "<p>No videos found or private.</p>"; return; }
 
 
 
 
464
 
465
  currentYTPlaylistVids = data.videos;
 
466
  data.videos.forEach((item, index) => {
467
  const safeData = encodeURIComponent(JSON.stringify(item));
468
  contentDiv.innerHTML += `
469
+ <div class="result-item" style="border-left-color: #29b6f6;">
470
  <input type="checkbox" class="vid-checkbox yt-pl-checkbox" data-index="${index}" onchange="toggleSelect(this, '${safeData}')">
471
  <div class="thumb-wrapper" onclick="playVid('${safeData}')">
472
  <img class="thumb" src="${item.thumb}">
 
477
  </div>
478
  </div>`;
479
  });
480
+ } catch(e) { contentDiv.innerHTML = "<p>Failed to load playlist contents.</p>"; }
481
+ }
482
+
483
+ function toggleSelectAllYTPlaylist() {
484
+ ytSelectAllState = !ytSelectAllState;
485
+ const checkboxes = document.querySelectorAll('.yt-pl-checkbox');
486
+ checkboxes.forEach(cb => {
487
+ if(cb.checked !== ytSelectAllState) {
488
+ cb.checked = ytSelectAllState;
489
+ cb.dispatchEvent(new Event('change')); // Trigger toggleSelect
490
+ }
491
+ });
492
  }
493
 
494
  function playYTPlaylistMode(playAll) {
495
  if(currentYTPlaylistVids.length === 0) return;
496
  let targetArray = [];
497
+ if(playAll) { targetArray = currentYTPlaylistVids.map(v => v.id); }
498
+ else { document.querySelectorAll('.yt-pl-checkbox').forEach(cb => { if(cb.checked) targetArray.push(currentYTPlaylistVids[cb.getAttribute('data-index')].id); }); }
 
 
 
 
 
 
499
 
500
+ if(targetArray.length === 0) { alert("No videos selected!"); return; }
501
  document.getElementById('player-overlay').style.display = 'block';
502
  rebuildPlayerDOM();
503
 
504
  ytPlayer = new YT.Player('player-target', {
505
+ playerVars: { playlist: targetArray.join(','), rel: 0 },
506
+ events: { 'onReady': (e) => e.target.playVideo(), 'onStateChange': handleIframeStateChange }
507
  });
508
  }
509
 
510
+ // --- CUSTOM PLAYLISTS ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
511
  async function getPlaylists() { return await localforage.getItem('custom_playlists') || {}; }
512
 
513
  async function createNewPlaylist() {
 
517
  if(!p[name]) { p[name] = []; await localforage.setItem('custom_playlists', p); }
518
  document.getElementById('newPlaylistName').value = "";
519
  closeModal('create-playlist-modal');
520
+ if(document.getElementById('add-to-gui-modal').style.display === 'block') initiateAddToPlaylist();
521
+ else loadCustomPlaylists();
522
  }
523
 
524
  async function initiateAddToPlaylist() {
 
527
  openModal('add-to-gui-modal');
528
  if(keys.length === 0) { content.innerHTML = "<p>No entries. Please create a playlist first.</p>"; }
529
  else {
530
+ let html = "<p style='margin-bottom:15px;'>Choose target playlist:</p>";
531
  keys.forEach(k => { html += `<div style="background:#f3e5f5; padding:15px; margin-bottom:10px; border-radius:10px; cursor:pointer;" onclick="saveSelectedTo('${k.replace(/'/g, "\\'")}')">📁 <b>${k}</b></div>`; });
532
  content.innerHTML = html;
533
  }
 
535
 
536
  async function saveSelectedTo(playlistName) {
537
  let p = await getPlaylists();
538
+ selectedVideos.forEach(vid => { if(!p[playlistName].find(v => v.id === vid.id)) p[playlistName].push(vid); });
539
  await localforage.setItem('custom_playlists', p);
540
  selectedVideos = [];
541
  document.querySelectorAll('.vid-checkbox').forEach(cb => cb.checked = false);
 
560
  });
561
  }
562
 
563
+ let customSelectAllState = false;
564
  async function viewPlaylistContents(name) {
565
  let p = await getPlaylists(); let vids = p[name];
566
  document.getElementById('playlist-list').style.display = 'none';
567
  const viewDiv = document.getElementById('playlist-view');
568
  viewDiv.style.display = 'block';
569
+ customSelectAllState = false;
570
 
571
  let html = `
572
  <div style="margin-bottom: 20px;">
 
576
  </div>
577
  <div class="modal-header-actions" style="padding-right:0;">
578
  <button class="btn-action" style="background:#26a69a;" onclick="playMultipleFromPlaylist('${name.replace(/'/g, "\\'")}', true)">Play Sequential</button>
579
+ <button class="btn-action" style="background:#ffca28; color:#333;" onclick="toggleSelectAllCustomPlaylist()">☑ Select All</button>
580
  <button class="btn-action" style="background:#26a69a;" onclick="playMultipleFromPlaylist('${name.replace(/'/g, "\\'")}', false)">Play Flagged Only</button>
581
  <button class="btn-action" style="background:#e53935;" onclick="deleteEntirePlaylist('${name.replace(/'/g, "\\'")}')">Delete Stack</button>
582
  </div>
 
588
  const safeData = encodeURIComponent(JSON.stringify(item));
589
  html += `
590
  <div class="result-item" style="padding:10px;">
591
+ <input type="checkbox" class="vid-checkbox custom-pl-checkbox" data-index="${index}">
592
  <div class="thumb-wrapper" onclick="playVid('${safeData}')">
593
  <img class="thumb" src="${item.thumb}" style="width:100px;height:56px;"><div class="dur-badge">${item.duration || ''}</div>
594
  </div>
 
599
  viewDiv.innerHTML = html;
600
  }
601
 
602
+ function toggleSelectAllCustomPlaylist() {
603
+ customSelectAllState = !customSelectAllState;
604
+ document.querySelectorAll('.custom-pl-checkbox').forEach(cb => cb.checked = customSelectAllState);
 
 
 
 
 
605
  }
606
 
607
  async function playMultipleFromPlaylist(playlistName, playAll) {
608
  let p = await getPlaylists(); let vids = p[playlistName]; if(vids.length === 0) return;
609
  let targetArray = [];
610
  if(playAll) { targetArray = vids.map(v => v.id); }
611
+ else { document.querySelectorAll('.custom-pl-checkbox').forEach(cb => { if(cb.checked) targetArray.push(vids[cb.getAttribute('data-index')].id); }); }
 
 
612
  if(targetArray.length === 0) return;
613
 
614
  document.getElementById('player-overlay').style.display = 'block';
615
  rebuildPlayerDOM();
616
 
617
  ytPlayer = new YT.Player('player-target', {
618
+ playerVars: { playlist: targetArray.join(','), rel: 0 },
619
+ events: { 'onReady': (e) => e.target.playVideo(), 'onStateChange': handleIframeStateChange }
620
  });
621
  }
622
 
623
+ // --- PLAYER & HISTORY CORE ---
624
  function rebuildPlayerDOM() {
625
  if (ytPlayer) { try { ytPlayer.destroy(); } catch(e){} ytPlayer = null; }
626
  const container = document.getElementById('player-overlay');
627
  container.innerHTML = '<div id="close-player" onclick="closePlayer()">&#10006;</div><div id="player-target"></div>';
628
  }
629
 
630
+ // END SCREEN JAIL: If player navigates to a new video via related videos
631
+ function handleIframeStateChange(event) {
632
+ if (event.data === 1 && ytPlayer) { // Playing
633
+ const vidData = ytPlayer.getVideoData();
634
+ if (vidData && vidData.video_id && vidData.video_id !== currentVideoData.id) {
635
+ currentVideoData = {
636
+ id: vidData.video_id, title: vidData.title || "Watched from Suggestion",
637
+ thumb: `https://i.ytimg.com/vi/${vidData.video_id}/hqdefault.jpg`,
638
+ author: vidData.author || "Unknown", type: 'video'
639
+ };
640
+ }
641
+ }
642
+ }
643
+
644
  function playVid(safeData) {
645
  const data = JSON.parse(decodeURIComponent(safeData));
646
  currentVideoData = data;
 
651
  if (document.documentElement.requestFullscreen) { document.documentElement.requestFullscreen().catch(e=>{}); }
652
  if (screen.orientation && screen.orientation.lock) { screen.orientation.lock('landscape').catch(e=>{}); }
653
 
654
+ // HISTORY RESUME LOGIC
655
+ const startSec = data.timeWatched ? Math.floor(data.timeWatched) : 0;
656
+
657
  if (data.type === 'playlist' && data.id) {
658
  ytPlayer = new YT.Player('player-target', {
659
+ playerVars: { listType: 'playlist', list: data.id, rel: 0 },
660
+ events: { 'onReady': (e) => { e.target.playVideo(); }, 'onStateChange': handleIframeStateChange }
661
  });
662
  } else if (data.id) {
663
  ytPlayer = new YT.Player('player-target', {
664
  videoId: data.id,
665
+ playerVars: { start: startSec, autoplay: 1, rel: 0 }, // Jumps to saved time & disables random related videos
666
+ events: { 'onReady': (e) => { e.target.playVideo(); }, 'onStateChange': handleIframeStateChange }
667
  });
668
  }
669
  startSavingHistory();
 
702
  </div>
703
  <div class="info-col">
704
  <div class="title" onclick="playVid('${safeData}')">${val.title}</div>
705
+ <div style="font-size:0.8em; color:#666;">Resumes at: ${Math.floor(val.timeWatched)}s | ${val.lastWatched}</div>
706
  </div>
707
  </div>`;
708
  });