// ==UserScript==
// @name         Boards.ie Forestry - Forest Preset Test Extended
// @namespace    corkiejp-boardscleaner-tests
// @version      0.2
// @description  Forest preset prototype for Boards.ie Forestry using broader selector coverage based on an existing Boardscleaner theme file
// @match        https://www.boards.ie/categories/forestry*
// @match        https://www.boards.ie/discussion/*
// @grant        GM_addStyle
// @run-at       document-end
// ==/UserScript==

(function () {
  'use strict';

  const isForestryPage =
    location.pathname.startsWith('/categories/forestry') ||
    document.querySelector('a[href="/categories/forestry"]') ||
    /forestry/i.test(document.body.innerText.slice(0, 2500));

  if (!isForestryPage) return;

  document.documentElement.classList.add('tm-forest-forestry');

  GM_addStyle(`
/* ============================================================
FOREST PRESET — broad coverage prototype
Based on the structure of current-affairs-imho.css
============================================================ */

html.tm-forest-forestry {
/* Backgrounds */
--t-bg: #162018;
--t-bg-2: #1c281e;
--t-surface: #1f2c22;
--t-surface-2: #27352a;
--t-surface-3: #314237;
--t-panel: #1a251d;

/* Borders */
--t-border: #425a47;
--t-border-soft: #33473a;

/* Text */
--t-text: #e3eee3;
--t-muted: #a8bca9;
--t-faint: #798d79;

/* Links */
--t-link: #91c98b;
--t-link-hover: #b8e1b3;

/* Header / accent strip */
--t-header-top: #567858;
--t-header-bottom: #3f5d42;
--t-header-border: #709272;

/* Buttons */
--t-button-top: #628b65;
--t-button-bottom: #446448;
--t-button-hover-top: #759f78;
--t-button-hover-bottom: #537557;
--t-button-border: #355039;

/* Legacy aliases */
--forum-row-bg: #1f2c22;
--forum-row-hover: #2a392d;
--forum-row-unread: #243327;
--forum-border: #425a47;
--forum-text: #e3eee3;
--forum-link: #91c98b;
--forum-link-hover: #b8e1b3;
--forum-muted: #a8bca9;
--forum-btn-bg: #628b65;
--forum-btn-text: #ffffff;
--forum-btn-border: #355039;
--forum-btn-hover: #759f78;
--forum-dropdown-bg: #1f2c22;
--forum-dropdown-hover: #2a392d;

--t-row-bg: #1f2c22;
--t-row-hover: #2a392d;
--t-row-unread: #243327;
--t-btn-bg: #628b65;
--t-btn-text: #ffffff;
--t-btn-border: #355039;
--t-btn-hover: #759f78;
--t-dropdown-bg: #1f2c22;
--t-dropdown-hover: #2a392d;
}

/* ============================================================
Page base
============================================================ */
html.tm-forest-forestry,
html.tm-forest-forestry body,
html.tm-forest-forestry #Body,
html.tm-forest-forestry .Container,
html.tm-forest-forestry .Content.MainContent,
html.tm-forest-forestry .page-content,
html.tm-forest-forestry .pageBox,
html.tm-forest-forestry .MessageList.Discussion,
html.tm-forest-forestry .Comments.pageBox,
html.tm-forest-forestry .Frame,
html.tm-forest-forestry .Frame-contentWrap {
  background: var(--t-bg) !important;
  color: var(--t-text) !important;
  border-color: var(--t-border-soft) !important;
}

/* ============================================================
Top navigation bar
============================================================ */
html.tm-forest-forestry #nav,
html.tm-forest-forestry #themeHeader,
html.tm-forest-forestry .titleBar,
html.tm-forest-forestry [class*="TitleBar-classes-bg"],
html.tm-forest-forestry [class*="TitleBar-classes-container"] {
  background: var(--t-bg) !important;
  border-bottom: 1px solid var(--t-border) !important;
}

html.tm-forest-forestry #nav a,
html.tm-forest-forestry [class*="TitleBar"] a,
html.tm-forest-forestry [class*="TitleBar"] button {
  color: var(--t-link) !important;
  background: transparent !important;
}

/* ============================================================
Breadcrumbs / heading
============================================================ */
html.tm-forest-forestry .BreadcrumbsBox,
html.tm-forest-forestry .Breadcrumbs,
html.tm-forest-forestry .PageTitle,
html.tm-forest-forestry .pageHeadingBox {
  background: var(--t-surface-2) !important;
  color: var(--t-text) !important;
  border-color: var(--t-border) !important;
}

html.tm-forest-forestry .Breadcrumbs a,
html.tm-forest-forestry .CrumbLabel a {
  color: var(--t-link) !important;
}

/* ============================================================
Links
============================================================ */
html.tm-forest-forestry a {
  color: var(--t-link) !important;
}

html.tm-forest-forestry a:hover {
  color: var(--t-link-hover) !important;
}

/* ============================================================
Forum thread list header
============================================================ */
html.tm-forest-forestry .forum-threadlist-header,
html.tm-forest-forestry .forum-threadlist-header .latest-title {
  background: linear-gradient(180deg, var(--t-header-top) 0%, var(--t-header-bottom) 100%) !important;
  color: #ffffff !important;
  border-bottom: 1px solid var(--t-header-border) !important;
}

/* ============================================================
Thread list table
============================================================ */
html.tm-forest-forestry .forum-threadlist-table th {
  background: var(--t-surface-2) !important;
  color: var(--t-text) !important;
  border-color: var(--t-border-soft) !important;
}

html.tm-forest-forestry .forum-threadlist-table tbody td {
  background: var(--t-surface) !important;
  color: var(--t-text) !important;
  border-color: var(--t-border-soft) !important;
}

html.tm-forest-forestry .forum-threadlist-table tbody tr:nth-child(even) td {
  background: var(--t-surface-2) !important;
}

html.tm-forest-forestry td.forum-threadlist-lastpost,
html.tm-forest-forestry td.forum-threadlist-replies,
html.tm-forest-forestry td.forum-threadlist-views,
html.tm-forest-forestry .forum-threadlist-thread-lastposter,
html.tm-forest-forestry .threadbit-lastposter {
  background: var(--t-surface) !important;
  color: var(--t-muted) !important;
  border-color: var(--t-border-soft) !important;
}

/* ============================================================
Pager
============================================================ */
html.tm-forest-forestry .PageControls,
html.tm-forest-forestry .Pager,
html.tm-forest-forestry #PagerBefore,
html.tm-forest-forestry #PagerAfter,
html.tm-forest-forestry .Pager a,
html.tm-forest-forestry .Pager span {
  background: var(--t-surface) !important;
  color: var(--t-link) !important;
  border-color: var(--t-border-soft) !important;
}

html.tm-forest-forestry .Pager a.Highlight {
  background: var(--t-header-bottom) !important;
  color: #ffffff !important;
}

/* ============================================================
Buttons
============================================================ */
html.tm-forest-forestry div.ButtonGroup[data-is-kludged="true"],
html.tm-forest-forestry .ButtonGroup.NewDiscussion,
html.tm-forest-forestry .Button,
html.tm-forest-forestry button,
html.tm-forest-forestry input[type="submit"],
html.tm-forest-forestry input[type="button"] {
  background: linear-gradient(180deg, var(--t-button-top) 0%, var(--t-button-bottom) 100%) !important;
  border: 1px solid var(--t-button-border) !important;
  color: #ffffff !important;
}

html.tm-forest-forestry div.ButtonGroup[data-is-kludged="true"] a.Button {
  background: transparent !important;
  color: #ffffff !important;
  border: none !important;
}

html.tm-forest-forestry .Button:hover,
html.tm-forest-forestry button:hover,
html.tm-forest-forestry input[type="submit"]:hover,
html.tm-forest-forestry input[type="button"]:hover {
  background: linear-gradient(180deg, var(--t-button-hover-top) 0%, var(--t-button-hover-bottom) 100%) !important;
}

/* ============================================================
Post header
============================================================ */
html.tm-forest-forestry .postbit-header {
  background: var(--t-surface-2) !important;
  color: var(--t-text) !important;
  border-bottom: 1px solid var(--t-border-soft) !important;
}

html.tm-forest-forestry .postbit-header a,
html.tm-forest-forestry .postbit-header .MItem {
  color: var(--t-text) !important;
  background: transparent !important;
}

/* ============================================================
User info column
============================================================ */
html.tm-forest-forestry .postbit-userinfo,
html.tm-forest-forestry .postbit-userinfo .userinfo-row,
html.tm-forest-forestry .postbit-userinfo .userinfo-joindate,
html.tm-forest-forestry .postbit-userinfo .userinfo-posts,
html.tm-forest-forestry .postbit-userinfo .RoleTitle,
html.tm-forest-forestry .postbit-userinfo .MItem {
  background: var(--t-panel) !important;
  color: var(--t-muted) !important;
  border-color: var(--t-border-soft) !important;
}

html.tm-forest-forestry .postbit-userinfo a,
html.tm-forest-forestry .postbit-userinfo .userinfo-username-title {
  color: var(--t-link) !important;
}

html.tm-forest-forestry .postbit-wrapper,
html.tm-forest-forestry .postbit-postdetails,
html.tm-forest-forestry .PhotoWrap,
html.tm-forest-forestry .ProfilePhotoMedium,
html.tm-forest-forestry .postbit-userinfo .PhotoWrapLarge {
  background: var(--t-panel) !important;
  border-color: var(--t-border-soft) !important;
}

/* ============================================================
Post body
============================================================ */
html.tm-forest-forestry .postbit-postbody,
html.tm-forest-forestry .postbit-message,
html.tm-forest-forestry .message-wrapper,
html.tm-forest-forestry .Item-BodyWrap,
html.tm-forest-forestry .Item-Body,
html.tm-forest-forestry .Message.userContent,
html.tm-forest-forestry .userContent,
html.tm-forest-forestry .UserSignature {
  background: var(--t-surface) !important;
  color: var(--t-text) !important;
  border-color: var(--t-border-soft) !important;
}

html.tm-forest-forestry .Message.userContent p,
html.tm-forest-forestry .Message.userContent div,
html.tm-forest-forestry .Message.userContent span,
html.tm-forest-forestry .Item-Body p,
html.tm-forest-forestry .Item-Body div,
html.tm-forest-forestry .userContent p,
html.tm-forest-forestry .userContent li {
  background: transparent !important;
  color: var(--t-text) !important;
}

/* ============================================================
Reactions
============================================================ */
html.tm-forest-forestry .Reactions,
html.tm-forest-forestry .Reactions > div,
html.tm-forest-forestry .Reactions .ReactButton,
html.tm-forest-forestry .Reactions .ReactLabel,
html.tm-forest-forestry .clearleft {
  background: var(--t-surface) !important;
  color: var(--t-link) !important;
  border-color: var(--t-border-soft) !important;
}

/* ============================================================
Quotes / code / embeds
============================================================ */
html.tm-forest-forestry blockquote,
html.tm-forest-forestry pre,
html.tm-forest-forestry code,
html.tm-forest-forestry [class*="quoteEmbed"],
html.tm-forest-forestry [class*="embedContent"] {
  background: var(--t-surface-2) !important;
  color: var(--t-text) !important;
  border: 1px solid var(--t-border-soft) !important;
}

/* ============================================================
Reply form
============================================================ */
html.tm-forest-forestry .MessageForm,
html.tm-forest-forestry .CommentForm,
html.tm-forest-forestry .Form-BodyWrap,
html.tm-forest-forestry textarea,
html.tm-forest-forestry input,
html.tm-forest-forestry select {
  background: var(--t-surface) !important;
  color: var(--t-text) !important;
  border-color: var(--t-border-soft) !important;
}

/* ============================================================
Extension UI bits
============================================================ */
html.tm-forest-forestry #profileToggleBtn,
html.tm-forest-forestry #bcEditModalBtn {
  background-color: var(--t-button-top) !important;
  color: #ffffff !important;
  border: 1px solid var(--t-button-border) !important;
}

html.tm-forest-forestry #profileToggleBtn:hover {
  background-color: var(--t-button-hover-top) !important;
}

html.tm-forest-forestry .infoIcon,
html.tm-forest-forestry .navIcon,
html.tm-forest-forestry dialog#shortcutDialog {
  background: var(--t-surface-2) !important;
  color: var(--t-text) !important;
  border-color: var(--t-border-soft) !important;
}

/* ============================================================
Mobile thread list
============================================================ */
@media (max-width: 768px) {
  html.tm-forest-forestry .forum-threadlist-wrapper,
  html.tm-forest-forestry .forum-threadlist-table td,
  html.tm-forest-forestry .threadrow-container,
  html.tm-forest-forestry .forum-threadlist-thread,
  html.tm-forest-forestry .forum-threadlist-thread-title,
  html.tm-forest-forestry .forum-threadlist-thread-meta {
    background: var(--t-surface) !important;
    color: var(--t-text) !important;
    border-color: var(--t-border-soft) !important;
  }

  html.tm-forest-forestry .forum-threadlist-thread-threadlink {
    color: var(--t-link) !important;
  }
}

/* ============================================================
Vanilla legacy structures
============================================================ */
html.tm-forest-forestry .DataTableWrap {
  background: transparent !important;
}

html.tm-forest-forestry table.DataTable.DiscussionsTable {
  background: transparent !important;
  border-collapse: collapse;
  width: 100%;
}

html.tm-forest-forestry table.DataTable.DiscussionsTable tr.Item,
html.tm-forest-forestry table.DataTable.DiscussionsTable tr.Item.Alt {
  background: var(--t-row-bg, #1f2c22) !important;
  border-bottom: 1px solid var(--t-border, #425a47) !important;
}

html.tm-forest-forestry table.DataTable.DiscussionsTable tr.Item:hover,
html.tm-forest-forestry table.DataTable.DiscussionsTable tr.Item.Alt:hover {
  background: var(--t-row-hover, #2a392d) !important;
}

html.tm-forest-forestry table.DataTable.DiscussionsTable tr.Item.Unread,
html.tm-forest-forestry table.DataTable.DiscussionsTable tr.Item.New {
  background: var(--t-row-unread, #243327) !important;
}

html.tm-forest-forestry table.DataTable.DiscussionsTable td.DiscussionName {
  background: transparent !important;
  color: var(--t-text, #e3eee3) !important;
}

html.tm-forest-forestry table.DataTable.DiscussionsTable td.DiscussionName a {
  color: var(--t-link, #91c98b) !important;
}

html.tm-forest-forestry table.DataTable.DiscussionsTable td.DiscussionName a:hover {
  color: var(--t-link-hover, #b8e1b3) !important;
  text-decoration: underline;
}

html.tm-forest-forestry table.DataTable.DiscussionsTable td.BlockColumn,
html.tm-forest-forestry table.DataTable.DiscussionsTable td.FirstUser,
html.tm-forest-forestry table.DataTable.DiscussionsTable td.LastUser,
html.tm-forest-forestry table.DataTable.DiscussionsTable td.BigCount,
html.tm-forest-forestry table.DataTable.DiscussionsTable td.BigCount.CountViews {
  background: transparent !important;
  color: var(--t-muted, #a8bca9) !important;
}

html.tm-forest-forestry .BoxButtons.BoxNewDiscussion,
html.tm-forest-forestry .BoxButtons.BoxNewDiscussion .ButtonGroup.Multi.Primary.Action.NewDiscussion.Big {
  background: transparent !important;
}

html.tm-forest-forestry .BoxButtons.BoxNewDiscussion .ButtonGroup.Multi.Primary.Action.NewDiscussion.Big .Button.Primary,
html.tm-forest-forestry .BoxButtons.BoxNewDiscussion .ButtonGroup.Multi.Primary.Action.NewDiscussion.Big button {
  background: var(--t-btn-bg, #628b65) !important;
  color: var(--t-btn-text, #fff) !important;
  border: 1px solid var(--t-btn-border, #355039) !important;
}

html.tm-forest-forestry .BoxButtons.BoxNewDiscussion .ButtonGroup.Multi.Primary.Action.NewDiscussion.Big .Button.Primary:hover,
html.tm-forest-forestry .BoxButtons.BoxNewDiscussion .ButtonGroup.Multi.Primary.Action.NewDiscussion.Big button:hover {
  background: var(--t-btn-hover, #759f78) !important;
}

html.tm-forest-forestry .BoxButtons.BoxNewDiscussion .Dropdown.MenuItems,
html.tm-forest-forestry .BoxButtons.BoxNewDiscussion .Dropdown.MenuItems li,
html.tm-forest-forestry .BoxButtons.BoxNewDiscussion .Dropdown.MenuItems a {
  background: var(--t-dropdown-bg, #1f2c22) !important;
  color: var(--t-text, #e3eee3) !important;
  border-color: var(--t-border, #425a47) !important;
}

html.tm-forest-forestry .BoxButtons.BoxNewDiscussion .Dropdown.MenuItems a:hover {
  background: var(--t-dropdown-hover, #2a392d) !important;
}

/* Ad placeholder cleanup */
html.tm-forest-forestry .no-ad-top-spacer {
  display: none !important;
}

html.tm-forest-forestry .Frame-contentWrap.noad-top {
  background: transparent !important;
  padding-top: 0 !important;
  margin-top: 0 !important;
}
/* ============================================================
Patch — Side categories box + top nav text links
============================================================ */

/* Right-hand Categories panel */
html.tm-forest-forestry .Box.BoxCategories.SideNav,
html.tm-forest-forestry .Box.BoxCategories.SideNav .Box-contents,
html.tm-forest-forestry .Box.BoxCategories.SideNav ul,
html.tm-forest-forestry .Box.BoxCategories.SideNav li {
  background: var(--t-bg) !important;
  color: var(--t-text) !important;
  border-color: var(--t-border-soft) !important;
}

html.tm-forest-forestry .Box.BoxCategories.SideNav .SideNav-header {
  background: linear-gradient(180deg, var(--t-header-top) 0%, var(--t-header-bottom) 100%) !important;
  color: #ffffff !important;
  border-bottom: 1px solid var(--t-header-border) !important;
}

html.tm-forest-forestry .Box.BoxCategories.SideNav .SideNav-header h2,
html.tm-forest-forestry .Box.BoxCategories.SideNav .SideNav-header .small-text {
  color: #ffffff !important;
  background: transparent !important;
}

html.tm-forest-forestry .Box.BoxCategories.SideNav a,
html.tm-forest-forestry .Box.BoxCategories.SideNav .NavItem,
html.tm-forest-forestry .Box.BoxCategories.SideNav .Aside,
html.tm-forest-forestry .Box.BoxCategories.SideNav .ChildCategories a {
  color: var(--t-link) !important;
  background: transparent !important;
}

html.tm-forest-forestry .Box.BoxCategories.SideNav a:hover,
html.tm-forest-forestry .Box.BoxCategories.SideNav .NavItem:hover {
  color: var(--t-link-hover) !important;
  background: var(--t-surface-2) !important;
}

/* Top nav text links like "Talk to..." */
html.tm-forest-forestry .nav-area,
html.tm-forest-forestry .nav-area #nav,
html.tm-forest-forestry .nav-area #nav > li {
  background: var(--t-bg) !important;
  border-color: var(--t-border) !important;
}

html.tm-forest-forestry .nav-area #nav .nav-link,
html.tm-forest-forestry .nav-area #nav span.nav-link,
html.tm-forest-forestry .nav-area #nav > li > a,
html.tm-forest-forestry .nav-area #nav > li > span {
  color: var(--t-link) !important;
  background: transparent !important;
}

html.tm-forest-forestry .nav-area #nav .nav-link:hover,
html.tm-forest-forestry .nav-area #nav span.nav-link:hover,
html.tm-forest-forestry .nav-area #nav > li:hover > a,
html.tm-forest-forestry .nav-area #nav > li:hover > span,
html.tm-forest-forestry .nav-area #nav .open > a,
html.tm-forest-forestry .nav-area #nav .open > span {
  color: var(--t-link-hover) !important;
  background: var(--t-surface-2) !important;
}

/* Optional: dropdown panels spawned by top nav */
html.tm-forest-forestry .nav-area .dropdown-menu,
html.tm-forest-forestry .nav-area .MenuItems,
html.tm-forest-forestry .nav-area .FlyoutMenu,
html.tm-forest-forestry .nav-area .Popup {
  background: var(--t-surface) !important;
  color: var(--t-text) !important;
  border: 1px solid var(--t-border-soft) !important;
}

html.tm-forest-forestry .nav-area .dropdown-menu a,
html.tm-forest-forestry .nav-area .MenuItems a,
html.tm-forest-forestry .nav-area .FlyoutMenu a,
html.tm-forest-forestry .nav-area .Popup a {
  color: var(--t-link) !important;
}

html.tm-forest-forestry .nav-area .dropdown-menu a:hover,
html.tm-forest-forestry .nav-area .MenuItems a:hover,
html.tm-forest-forestry .nav-area .FlyoutMenu a:hover,
html.tm-forest-forestry .nav-area .Popup a:hover {
  color: var(--t-link-hover) !important;
  background: var(--t-surface-2) !important;
}
function patchThemeHeaderShadowNav() {
  const host = document.querySelector('#themeHeader');
  if (!host || !host.shadowRoot) return false;

  let style = host.shadowRoot.getElementById('tm-forest-shadow-nav-style');
  if (style) return true;

  style = document.createElement('style');
  style.id = 'tm-forest-shadow-nav-style';
  style.textContent = `
    .nav-area {
      background: #162018 !important;
      border-bottom: 1px solid #425a47 !important;
    }

    #nav,
    ul,
    ul li {
      background: #162018 !important;
      background-image: none !important;
    }

    #nav > li {
      border-left: 1px solid #425a47 !important;
      border-right: 1px solid transparent !important;
    }

    #nav > li > a,
    #nav > li > span,
    .nav-link,
    ul li a,
    ul li span {
      color: #91c98b !important;
      background: transparent !important;
    }

    #nav > li.hover,
    #nav > li.hover > a,
    #nav > li.hover > span,
    li:hover > a,
    li:hover > span,
    ul li a:hover,
    ul li span:hover {
      background: #27352a !important;
      color: #b8e1b3 !important;
    }

    li:hover > ul,
    li ul,
    ul ul,
    ul ul li {
      background: #1f2c22 !important;
      border-color: #33473a !important;
    }

    .footer {
      background: #1c281e !important;
      color: #a8bca9 !important;
    }

    .footer a,
    .footer-consent {
      color: #91c98b !important;
    }
  `;
  host.shadowRoot.appendChild(style);
  return true;
}

patchThemeHeaderShadowNav();
setTimeout(patchThemeHeaderShadowNav, 300);
setTimeout(patchThemeHeaderShadowNav, 1000);
setTimeout(patchThemeHeaderShadowNav, 2500);
`);
/* STEP 19 NOTE — #themeHeader nav is inside an open shadow root.
   Plain preset CSS cannot style it directly.
   Must be patched by JS injecting a <style> into document.querySelector('#themeHeader').shadowRoot */
})();