YouTube Layout Fix

Forces 6 videos per row, adjusts text sizes, hides checkmarks, adjusts spacing.

// ==UserScript==
// @name         YouTube Layout Fix
// @namespace    http://tampermonkey.net/
// @version      3.59
// @description  Forces 6 videos per row, adjusts text sizes, hides checkmarks, adjusts spacing.
// @author       Kalakaua
// @match        https://www.youtube.com/*
// @grant        GM_addStyle
// @run-at       document-idle
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // --- FINAL ADJUSTABLE VALUES (User Prefs) ---

    // === Column Layout ===
    const desiredColumnCount      = 6;    // Number of columns for main content grids (Home, Subs, Playlists Feed, Channels)

    // === Grid Items - Text Sizes (Home, Subs, Playlists Feed, Channel Pages*) ===
    const gridTitleScale          = 1.12; // Title text size multiplier (em)
    const gridChannelScale        = 0.85; // Channel name text size multiplier (em)
    const gridMetadataScale       = 0.85; // Views/Date text size multiplier (em)

    // === Grid Items - Internal Spacing ===
    const gridChannelMarginTop    = "-1px"; // Space above channel name
    const gridMetaMarginTop       = "-4px"; // Space above views/date line

    // === Overall Grid Structure ===
    const gridEdgePadding         = "24px"; // Padding on the left/right edges of the main grids
    const gridItemHorizontalMargin= "6px";  // Space between grid items horizontally
    const gridItemBottomMargin    = "24px"; // Space below each grid item

    // === Search Results Page (/results) ===
    const searchThumbnailWidth    = "350px"; // Width of video thumbnails
    const searchResultsMarginTop  = "16px";  // Space above the first search result
    const searchTitleScale        = 1.4;     // Scale factor for video titles (based on 1rem)
    const searchChannelScale      = 1.20;    // Scale factor for channel names (based on ~0.8em)
    const searchMetaSnippetScale  = 1.25;    // Scale factor for views/date/description snippet (based on ~0.8em)
    const searchDescFixedWidth    = "800px"; // Fixed width for description snippet container (helps align 3-dot menu)

    // === Watch Page - Below Video Player ===
    const watchOwnerChannelScale  = 1.5;  // Scale factor for channel name under video
    const watchTopRowMarginTop    = "-4px"; // Space above views/date line under video title
    const watchSubCountMarginTop  = "-3.5px";// Space above subscriber count

    // === Sidebar (Watch Page Right Sidebar - Compact Video List) ===
    const sidebarTitleScale       = 1.05; // Scale factor for video titles
    const sidebarChannelNameScale = 1.0;  // Scale factor for channel names
    const sidebarViewsDateScale   = 1.0; // Scale factor for views/date
    const sidebarTitleMarginBottom= "6px";  // Space below sidebar video titles

    // === Sidebar Badge Styling (Watch Page Right Sidebar) ===
    const sidebarBadgeScale       = 0.85; // Scale factor for badges (e.g., "New")
    const sidebarBadgeMarginTop   = "2px";  // Space above badges
    const sidebarBadgeMarginBottom= "0px";  // Space below badges

    // === Comment Section (Watch Page) ===
    const commentTextScale        = 1.15; // Scale factor for main comment and reply text (applied to 1rem base)
    const commentMetaScale        = 1.1;  // Scale factor for author/timestamp (applied to ~0.8rem base)

    // --- Technical Values ---
    const minimalPxReduction      = "0.01px"; // Minimal reduction for calc() robustness in grid width calculation

    // --- END OF ADJUSTABLE VALUES ---


    const css = `
        :root {
           /* Grid Variables */
           --gm-grid-title-size: ${gridTitleScale}em;
           --gm-grid-channel-size: ${gridChannelScale}em;
           --gm-grid-metadata-size: ${gridMetadataScale}em;

           /* Watch Page Variables */
           --gm-watch-owner-channel-size: ${watchOwnerChannelScale}em;
           --gm-watch-title-size: 1.5rem; /* Fixed size for main video title */

           /* Sidebar Variables */
           --gm-sidebar-title-size: ${sidebarTitleScale}em;
           --gm-sidebar-channel-size: ${sidebarChannelNameScale}em;
           --gm-sidebar-viewsdate-size: ${sidebarViewsDateScale}em;
           --gm-sidebar-badge-size: ${sidebarBadgeScale}em;

           /* Search Results Variables */
           --gm-search-title-scale: ${searchTitleScale};
           --gm-search-channel-scale: ${searchChannelScale};
           --gm-search-metasnippet-scale: ${searchMetaSnippetScale};
           --gm-search-title-size: calc(1.0rem * var(--gm-search-title-scale));
           --gm-search-channel-size: calc(0.8em * var(--gm-search-channel-scale));
           --gm-search-metasnippet-size: calc(0.8em * var(--gm-search-metasnippet-scale));

           /* Comment Section Variables (REM based for consistency) */
           --gm-comment-text-final-size: calc(1rem * ${commentTextScale});
           --gm-comment-meta-final-size: calc(0.8rem * ${commentMetaScale});
        }

        /* ========================================================== */
        /* === VARIABLE ITEMS PER ROW - MAIN GRIDS === */
        /* ========================================================== */

        /* --- Global Container Padding & Width --- */
        #contents.ytd-rich-grid-renderer { padding-left: ${gridEdgePadding} !important; padding-right: ${gridEdgePadding} !important; box-sizing: border-box !important; width: 100% !important; }
        ytd-browse[page-subtype="channels"] #contents.ytd-rich-grid-renderer, ytd-browse[page-subtype="channels"] ytd-item-section-renderer #contents > ytd-rich-grid-renderer { padding-left: ${gridEdgePadding} !important; padding-right: ${gridEdgePadding} !important; box-sizing: border-box !important; width: 100% !important; }

        /* --- Rule Set 2: Playlist Feed Specific Styles (ytd-rich-item-renderer, via :not()) --- */
        #contents.ytd-rich-grid-renderer > ytd-rich-item-renderer:not( ytd-browse[page-subtype="home"] #contents.ytd-rich-grid-renderer > ytd-rich-item-renderer, ytd-browse[page-subtype="subscriptions"] #contents.ytd-rich-grid-renderer > ytd-rich-item-renderer, ytd-browse[page-subtype="channels"] #contents.ytd-rich-grid-renderer > ytd-rich-item-renderer ) { margin-left: ${gridItemHorizontalMargin} !important; margin-right: ${gridItemHorizontalMargin} !important; margin-bottom: ${gridItemBottomMargin} !important; max-width: calc(100% / ${desiredColumnCount} - ${gridItemHorizontalMargin} * 2 - ${minimalPxReduction}) !important; }
        #contents.ytd-rich-grid-renderer ytd-rich-item-renderer:not(...) .yt-lockup-metadata-view-model-wiz__title { font-size: var(--gm-grid-title-size) !important; line-height: 1.2em !important; max-height: 2.4em !important; overflow: hidden !important; text-overflow: ellipsis !important; display: -webkit-box !important; -webkit-line-clamp: 2 !important; -webkit-box-orient: vertical !important; margin-bottom: 1px !important; }
        #contents.ytd-rich-grid-renderer ytd-rich-item-renderer:not(...) .yt-content-metadata-view-model-wiz__metadata-text { font-size: var(--gm-grid-metadata-size) !important; line-height: 1.3em !important; display: inline !important; }
        #contents.ytd-rich-grid-renderer ytd-rich-item-renderer:not(...) .yt-lockup-metadata-view-model-wiz__metadata { margin-top: ${gridMetaMarginTop} !important; line-height: 1.3em; }
        #contents.ytd-rich-grid-renderer ytd-rich-item-renderer:not(...) a.yt-core-attributed-string__link--call-to-action-color { font-size: 0.9em !important; margin-top: 2px !important; }

        /* --- Rule Set 1: Home, Subscriptions, AND Channel Homepage Grids (ytd-rich-item-renderer) --- */
        ytd-browse[page-subtype="home"] #contents.ytd-rich-grid-renderer > ytd-rich-item-renderer, ytd-browse[page-subtype="subscriptions"] #contents.ytd-rich-grid-renderer > ytd-rich-item-renderer, ytd-browse[page-subtype="channels"] #contents.ytd-rich-grid-renderer > ytd-rich-item-renderer { margin-left: ${gridItemHorizontalMargin} !important; margin-right: ${gridItemHorizontalMargin} !important; margin-bottom: ${gridItemBottomMargin} !important; max-width: calc(100% / ${desiredColumnCount} - ${gridItemHorizontalMargin} * 2 - ${minimalPxReduction}) !important; border: none !important; }
        ytd-browse[page-subtype="home"] ytd-rich-item-renderer #video-title.ytd-rich-grid-media, ytd-browse[page-subtype="subscriptions"] ytd-rich-item-renderer #video-title.ytd-rich-grid-media, ytd-browse[page-subtype="channels"] ytd-rich-item-renderer #video-title.ytd-rich-grid-media { font-size: var(--gm-grid-title-size) !important; line-height: 1.2em !important; max-height: 2.4em !important; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; margin-bottom: 1px !important; }
        ytd-browse[page-subtype="home"] ytd-rich-item-renderer #byline-container, ytd-browse[page-subtype="subscriptions"] ytd-rich-item-renderer #byline-container, ytd-browse[page-subtype="channels"] ytd-rich-item-renderer #byline-container { white-space: nowrap !important; overflow: hidden !important; text-overflow: ellipsis !important; display: flex !important; align-items: center !important; margin-top: ${gridChannelMarginTop} !important; line-height: 1.2em !important; gap: 0 !important; }
        ytd-browse[page-subtype="home"] ytd-rich-item-renderer ytd-channel-name, ytd-browse[page-subtype="subscriptions"] ytd-rich-item-renderer ytd-channel-name, ytd-browse[page-subtype="channels"] ytd-rich-item-renderer ytd-channel-name { font-size: var(--gm-grid-channel-size) !important; line-height: 1.25em !important; display: inline-block !important; overflow: hidden !important; text-overflow: ellipsis !important; flex-grow: 1 !important; min-width: 0 !important; margin: 0 !important; padding: 0 !important; vertical-align: baseline !important; }
        ytd-browse[page-subtype="home"] ytd-rich-item-renderer #metadata-line, ytd-browse[page-subtype="subscriptions"] ytd-rich-item-renderer #metadata-line, ytd-browse[page-subtype="channels"] ytd-rich-item-renderer #metadata-line { line-height: 1.3em !important; margin-top: ${gridMetaMarginTop} !important; display: block !important; white-space: nowrap !important; overflow: hidden !important; text-overflow: ellipsis !important; }
        ytd-browse[page-subtype="home"] ytd-rich-item-renderer #metadata-line > span, ytd-browse[page-subtype="subscriptions"] ytd-rich-item-renderer #metadata-line > span, ytd-browse[page-subtype="channels"] ytd-rich-item-renderer #metadata-line > span { font-size: var(--gm-grid-metadata-size) !important; line-height: 1.2em !important; display: inline !important; vertical-align: baseline !important; }
        ytd-browse[page-subtype="home"] ytd-rich-item-renderer #metadata-line > span:first-of-type, ytd-browse[page-subtype="subscriptions"] ytd-rich-item-renderer #metadata-line > span:first-of-type, ytd-browse[page-subtype="channels"] ytd-rich-item-renderer #metadata-line > span:first-of-type { margin-right: 0.5em !important; }
        ytd-browse[page-subtype="home"] ytd-rich-item-renderer #metadata-line > span:first-of-type::after, ytd-browse[page-subtype="subscriptions"] ytd-rich-item-renderer #metadata-line > span:first-of-type::after, ytd-browse[page-subtype="channels"] ytd-rich-item-renderer #metadata-line > span:first-of-type::after { content: none !important; }

        /* --- Rule Set 3: Channel Pages SPECIFIC TABS (e.g., Videos Tab using ytd-grid-video-renderer) --- */
        ytd-browse[page-subtype="channels"] #contents.ytd-rich-grid-renderer ytd-grid-video-renderer, ytd-browse[page-subtype="channels"] ytd-item-section-renderer #contents > ytd-rich-grid-renderer ytd-grid-video-renderer { margin-left: ${gridItemHorizontalMargin} !important; margin-right: ${gridItemHorizontalMargin} !important; margin-bottom: ${gridItemBottomMargin} !important; max-width: calc(100% / ${desiredColumnCount} - ${gridItemHorizontalMargin} * 2 - ${minimalPxReduction}) !important; }
        ytd-browse[page-subtype="channels"] ytd-grid-video-renderer #video-title { font-size: var(--gm-grid-title-size) !important; line-height: 1.2em !important; max-height: 2.4em !important; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; margin-bottom: 1px !important; }
        ytd-browse[page-subtype="channels"] ytd-grid-video-renderer #metadata-line { line-height: 1.3em !important; margin-top: ${gridMetaMarginTop} !important; display: block !important; white-space: nowrap !important; overflow: hidden !important; text-overflow: ellipsis !important; }
        ytd-browse[page-subtype="channels"] ytd-grid-video-renderer #metadata-line > span { font-size: var(--gm-grid-metadata-size) !important; line-height: 1.2em !important; display: inline !important; vertical-align: baseline !important; }
        ytd-browse[page-subtype="channels"] ytd-grid-video-renderer #metadata-line > span:first-of-type { margin-right: 0.5em !important; }
        ytd-browse[page-subtype="channels"] ytd-grid-video-renderer #metadata-line > span:first-of-type::after { content: none !important; }

        /* ========================================================== */
        /* === GLOBAL STYLES & FIXES === */
        /* ========================================================== */
        /* Hide Checkmarks */
        ytd-badge-supported-renderer:has(.badge.badge-style-type-verified) { display: none !important; }
        /* Fullscreen Scrollbar Fix */
        html.fullscreen, html.fullscreen body { overflow-x: hidden !important; }

        /* Apply overflow fix to ytd-app for normal view */
        ytd-app {
             overflow-x: hidden !important;
        }

        /* --- Player Button Hitbox Fixes --- */
        /* Combine all selectors needing 'content: none' */
       .ytp-fullscreen-button::after, /* For fullscreen button */
        #contentContainer::after,      /* For play button area (RE-ADDED) */
        .ytp-play-button::before       /* For play button itself */
        {
        content: none !important;
        }

        /* ========================================================== */
        /* === SEARCH RESULTS PAGE (/results) === */
        /* ========================================================== */
        ytd-search ytd-item-section-renderer:first-of-type { margin-top: ${searchResultsMarginTop} !important; }
        ytd-search ytd-item-section-renderer ytd-video-renderer[is-search] { display: flex !important; margin-bottom: 16px !important; align-items: flex-start !important; }
        ytd-search ytd-item-section-renderer ytd-video-renderer[is-search] #dismissible ytd-thumbnail.ytd-video-renderer { width: ${searchThumbnailWidth} !important; min-width: ${searchThumbnailWidth} !important; max-width: ${searchThumbnailWidth} !important; flex-basis: ${searchThumbnailWidth} !important; flex-shrink: 0 !important; }
        ytd-search ytd-item-section-renderer ytd-video-renderer[is-search] #dismissible div.text-wrapper.ytd-video-renderer { margin-left: 12px !important; flex: 1 !important; min-width: 0; }
        /* Text Sizes */
        ytd-search ytd-item-section-renderer ytd-video-renderer[is-search] a#video-title.ytd-video-renderer yt-formatted-string { font-size: var(--gm-search-title-size) !important; line-height: 1.25em !important; max-height: 2.5em !important; -webkit-line-clamp: 2 !important; display: -webkit-box !important; -webkit-box-orient: vertical !important; overflow: hidden !important; text-overflow: ellipsis !important; margin-bottom: 3px !important; }
        ytd-search ytd-item-section-renderer ytd-video-renderer[is-search] #metadata-line.ytd-video-meta-block span.inline-metadata-item { font-size: var(--gm-search-metasnippet-size) !important; line-height: 1.3em !important; }
        ytd-search ytd-item-section-renderer ytd-video-renderer[is-search] #channel-name.ytd-video-renderer yt-formatted-string > a, ytd-search ytd-item-section-renderer ytd-video-renderer[is-search] #channel-name.ytd-video-renderer yt-formatted-string { font-size: var(--gm-search-channel-size) !important; line-height:1.3em !important; }
        ytd-search ytd-item-section-renderer ytd-video-renderer[is-search] #channel-thumbnail.ytd-video-renderer { width: 20px !important; height: 20px !important; }
        /* Description Snippet */
        ytd-search ytd-item-section-renderer ytd-video-renderer[is-search] .metadata-snippet-container.ytd-video-renderer,
        ytd-search ytd-item-section-renderer ytd-video-renderer[is-search] .metadata-snippet-container-one-line.ytd-video-renderer {
            width: ${searchDescFixedWidth} !important;
            min-width: ${searchDescFixedWidth} !important;
            max-width: ${searchDescFixedWidth} !important;
            display: block !important;
            margin-top: 4px !important;
        }
        /* Hide Chapters Section */
        ytd-search ytd-item-section-renderer ytd-video-renderer[is-search] #expandable-metadata.ytd-video-renderer { display: none !important; }

        /* ========================================================== */
        /* === WATCH PAGE (/watch) === */
        /* ========================================================== */
        /* --- Below Video Player --- */
        .title.ytd-video-primary-info-renderer h1.ytd-video-primary-info-renderer { font-size: var(--gm-watch-title-size) !important; line-height: 1.2em !important; }
        #top-row.ytd-watch-metadata { margin-top: ${watchTopRowMarginTop} !important; }
        ytd-video-owner-renderer ytd-channel-name { font-size: var(--gm-watch-owner-channel-size) !important; line-height: 1.05em !important; margin: 0 !important; padding: 0 !important; display: block !important; }
        #owner-sub-count.ytd-video-owner-renderer { margin-top: ${watchSubCountMarginTop} !important; margin-bottom: 0 !important; }
        #description-inner #description .content.ytd-video-secondary-info-renderer, .ytd-expander.ytd-video-secondary-info-renderer { font-size: 0.8em !important; line-height: 1.35em !important; }
        #info-text.ytd-video-primary-info-renderer { font-size: 0.8em !important; }

        /* --- Sidebar --- */
        ytd-compact-video-renderer h3.ytd-compact-video-renderer { margin-bottom: ${sidebarTitleMarginBottom} !important; margin-top: 0 !important; }
        #video-title.ytd-compact-video-renderer { font-size: var(--gm-sidebar-title-size) !important; line-height: 1.3em !important; max-height: 2.6em !important; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
        ytd-compact-video-renderer ytd-channel-name { font-size: var(--gm-sidebar-channel-size) !important; line-height: 1.35em !important; margin-bottom: 1px !important; }
        ytd-compact-video-renderer #metadata-line span.inline-metadata-item { font-size: var(--gm-sidebar-viewsdate-size) !important; line-height: 1.45em !important; }
        ytd-compact-video-renderer ytd-badge-supported-renderer.badges { margin-top: ${sidebarBadgeMarginTop} !important; margin-bottom: ${sidebarBadgeMarginBottom} !important; font-size: var(--gm-sidebar-badge-size) !important; line-height: 1.2 !important; }

        /* --- Comment Section --- */
        /* Reset Reply Expander font size (Neutralizes default 1.4em) */
        ytd-comment-replies-renderer ytd-comment-view-model[is-reply] > #main > #expander {
             font-size: 1rem !important;
        }
        /* Rule for Comment/Reply Text Content (Uses REM for consistency) */
        ytd-comment-view-model #expander > #content > yt-attributed-string#content-text {
            font-size: var(--gm-comment-text-final-size) !important;
            line-height: 1.5em !important;
        }
        /* Rule for Main Comment Meta (Author/Timestamp - Uses REM) */
         ytd-comments ytd-comment-thread-renderer > ytd-comment-view-model:not([is-reply]) #header-author #author-text,
         ytd-comments ytd-comment-thread-renderer > ytd-comment-view-model:not([is-reply]) #header-author .published-time-text.ytd-comment-view-model a {
            font-size: var(--gm-comment-meta-final-size) !important;
            line-height: 1.4em !important;
        }
        /* Rule for Reply Meta (Author/Timestamp - Uses REM) */
        ytd-comments ytd-comment-replies-renderer ytd-comment-view-model[is-reply] #header-author #author-text,
        ytd-comments ytd-comment-replies-renderer ytd-comment-view-model[is-reply] #header-author .published-time-text.ytd-comment-view-model a {
            font-size: var(--gm-comment-meta-final-size) !important;
            line-height: 1.4em !important;
        }
        /* Rule for Comment Toolbar */
        #toolbar.ytd-comment-action-buttons-renderer {
            font-size: var(--gm-comment-meta-final-size) !important;
        }
        /* Rule for Author Text Color */
        #header-author #author-text.ytd-comment-renderer {
             color: #aaa !important;
        }

    `;

    // Inject the CSS using GM_addStyle
    if (typeof GM_addStyle === 'function') {
        // Read version from metadata block for consistency
        const scriptVersion = (typeof GM_info !== 'undefined' && GM_info.script) ? GM_info.script.version : 'N/A';
        GM_addStyle(css);
        console.log(`YouTube Layout Script: v${scriptVersion} Active (${desiredColumnCount} Cols) - Final v3`); // Updated log message
    } else {
        console.error("YouTube Layout Script: GM_addStyle is not defined.");
        const style = document.createElement('style');
        style.textContent = css;
        document.head.appendChild(style);
    }

})();