/*
 * Mega Menu (Tuotteet) — Sprint 2. Loads after header-footer.css. Desktop
 * only (>=1024px) — .site-nav itself is already hidden below that
 * breakpoint (header-footer.css), and mobile navigation is untouched by
 * this file entirely (the mobile nav loop in site-header.php never calls
 * hitsiaijat_render_mega_menu_trigger()).
 *
 * No-JS / no-desktop baseline: the markup from inc/mega-menu.php has no
 * `hidden` attribute, so it's a normal in-flow list of real category links
 * by default — same progressive-enhancement contract already used for
 * .mobile-nav / .site-header__search in header-footer.css. Only once `.js`
 * is on <html> (added by navigation.js, guaranteed to run first — see
 * inc/enqueue.php's script dependency) does this become an absolutely
 * positioned, hidden-until-opened dropdown.
 *
 * Every value below is an existing token from tokens.css — no new colors,
 * shadows, radii, fonts, or container sizes. `--z-dropdown` was already
 * reserved for exactly this use (DOCX 4.5) and previously unused.
 */

.site-nav__mega-group {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
}

/* Pre-existing bug found during this redesign's visual verification, fixed
   here as a direct blocker (not baseline noise — see the task's own
   allowance to touch header-footer.css "at proven necessity", and this is
   the smaller, safer fix that doesn't even require touching that file):
   header-footer.css's Header v2 dark-bar treatment sets
   `.site-header__bar .site-nav__list a { color: var(--color-text-on-dark) }`
   (specificity 0,2,1) so the three TOP-LEVEL nav links read white against
   the dark bar. That descendant selector has no way to distinguish "a
   direct nav link" from "a link nested inside the mega menu panel, many
   levels down" — .mega-menu is itself inside .site-nav__list in the DOM,
   so every link inside it (column titles, categories, grandchildren)
   silently inherited that same white color against this panel's own
   light (--color-bg-surface) background, making all of it invisible.
   #hitsiaijat-mega-menu is the panel's own existing, real id (see
   inc/mega-menu.php) — an ID selector (specificity 1,0,0) beats the
   offending rule outright, regardless of source order, without needing
   the "duplicate a class to bump specificity" trick used elsewhere in
   this codebase (see catalog.css's own comment on that pattern). */
#hitsiaijat-mega-menu a {
  color: var(--color-text-default);
}

/* Focus-visible fix (component-scoped, Option A): every real <a> inside
   the panel (column titles, sublist links, view-all) is plain-classed or
   unclassed, so Astra's bare a:focus-visible (0,1,1) otherwise wins
   dotted/1px. The ID here already beats Astra's specificity outright, so
   one rule covers all three link types without repeating the fix per
   sub-selector. */
#hitsiaijat-mega-menu a:focus-visible {
  outline: var(--border-width-active) solid var(--color-industrial-900);
  outline-offset: 2px;
}

/* Navigation v2.2: single unified control replacing the old separate
   "$nav_item link + square chevron button" pair (inc/mega-menu.php). Reuses
   the one existing filled-blue-button treatment in this codebase
   (header-footer.css's `.site-header .icon-button.site-header__nav-toggle`,
   --color-brand-blue-600 + --color-text-on-dark) rather than inventing a
   second blue — this is now that token's second, deliberate use, still
   scoped to real button-style controls only, not a general nav color. Base
   sizing/spacing mirrors components.css's `.btn` (min-height
   --touch-target-min, --radius-control, --space-2 gap) without adding that
   class to the markup, so this stays independent of `.btn`'s other
   variants and their focus/disabled states. */
.site-nav__mega-trigger {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  min-height: var(--touch-target-min);
  padding-inline: var(--space-4);
  background-color: var(--color-brand-blue-600);
  color: var(--color-text-on-dark);
  border: none;
  border-radius: var(--radius-control);
  font-family: var(--font-body);
  font-weight: 600;
  font-size: var(--text-button-size);
  cursor: pointer;
}

.site-nav__mega-trigger:hover {
  background-color: var(--color-brand-blue-700); /* one step darker than --color-brand-blue-600, same relationship .btn--brand:hover has to .btn--brand */
}

/* Focus-visible fix (component-scoped, Option A): real <button>, no .btn
   class — Astra's static button:focus (0,1,1) wins outline:0, confirmed
   live (zero visible change from the unfocused blue button — the core
   finding that led to this whole fix). */
.site-nav__mega-trigger:focus-visible {
  outline: var(--border-width-active) solid var(--color-industrial-900);
  outline-offset: 2px;
}

.site-nav__mega-trigger svg {
  transition: transform var(--motion-fast) var(--motion-easing);
}

.site-nav__mega-trigger[aria-expanded='true'] svg {
  transform: rotate(180deg);
}

/* "Näytä kaikki tuotteet" — the compact panel's escape valve to the full
   shop, replacing the direct-link path the old separate <a> trigger used to
   provide (see hitsiaijat_render_mega_menu_trigger()'s own comment).
   Full-width, visually separated from the column grid above it, so it
   reads as the panel's one clear "see everything" action rather than a
   5th column item. */
/* #hitsiaijat-mega-menu prefix — same `.site-nav__list a` collision as
   .mega-menu__column-title / .mega-menu__sublist a above (min-height:44px
   and border-bottom:2px solid transparent otherwise leak through). */
#hitsiaijat-mega-menu .mega-menu__view-all {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 36px;
  margin-block-start: var(--space-3);
  padding-block-start: var(--space-3);
  border-top: var(--border-width-default) solid var(--color-border-default);
  border-bottom: none;
  font-weight: 600;
  font-size: var(--text-small-size);
  text-decoration: none;
  color: var(--color-brand-blue-600);
}

.mega-menu__view-all:hover,
.mega-menu__view-all:focus-visible {
  text-decoration: underline;
}

.mega-menu__columns {
  list-style: none;
  margin: 0;
  padding: 0;
}

/* Compact heading, visually distinct via a short brand-blue rule
   (matches the active-category brand-blue accent already used in the
   catalog filter panel) rather than the generic grey border-bottom this used
   before — a deliberately small heading, not a 44px touch-target-sized
   control (it's a plain link inside a desktop-only, mouse/hover-driven
   panel, not a mobile touch surface).
   #hitsiaijat-mega-menu prefix (not a bare .mega-menu__column-title
   selector): same root cause and same fix already documented above this
   file's #hitsiaijat-mega-menu a rule — .mega-menu lives inside
   .site-nav__list in the DOM, and header-footer.css's `.site-nav__list a`
   (0,1,1) otherwise outranks a bare `.mega-menu__column-title` (0,1,0) and
   silently wins on min-height/font-size/border-bottom. Confirmed live via
   getComputedStyle() during this pass — min-height measured 44px despite
   this rule saying 32px until the ID prefix was added. */
#hitsiaijat-mega-menu .mega-menu__column-title {
  display: flex;
  align-items: center;
  min-height: 32px;
  font-weight: 600;
  font-size: var(--text-small-size);
  text-decoration: none;
  color: var(--color-text-default);
  border-bottom: 2px solid var(--color-brand-blue-600);
  padding-block-end: var(--space-2);
  margin-block-end: var(--space-2);
}

.mega-menu__sublist,
.mega-menu__sublist ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

/* No forced 44px row height here (was --touch-target-min) — this is the
   same desktop-only, non-touch context as .mega-menu__column-title above.
   #hitsiaijat-mega-menu prefix for the same reason as that rule: without
   it, `.site-nav__list a`'s own min-height:44px / font-size:16px simply
   never gets overridden at all — a bare `.mega-menu__sublist a` (0,1,1)
   ties that rule's specificity, and a tie on a PROPERTY THIS RULE DOESN'T
   SET (min-height was omitted here, not zeroed) leaves the other rule's
   value in effect regardless of source order. */
#hitsiaijat-mega-menu .mega-menu__sublist a {
  display: flex;
  align-items: center;
  min-height: 0;
  padding-block: 2px;
  font-size: var(--text-small-size);
  font-weight: 400;
  text-decoration: none;
  color: var(--color-text-default);
}

.mega-menu__sublist a:hover,
.mega-menu__sublist a:focus-visible {
  text-decoration: underline;
}

/* Grandchildren indent under their parent child item. */
.mega-menu__sublist ul {
  padding-inline-start: var(--space-4);
}

/* ---------- Show more / show less (redesign v1 §2.4) ----------
   Rendered by inc/mega-menu.php whenever a single children/grandchildren
   list is longer than HITSIAIJAT_MEGA_MENU_SHOW_MORE_THRESHOLD. No-JS /
   before-JS baseline: a plain <button> (no aria state CSS depends on) and
   a normal in-flow <ul> — both fully visible, exactly like every other
   progressively-enhanced disclosure in this theme. Only once `.js` is on
   <html> AND mega-menu.js hasn't yet marked a given panel `.is-open` does
   CSS collapse it. */
.mega-menu__more-toggle {
  display: inline-flex;
  align-items: center;
  min-height: var(--touch-target-min);
  background: none;
  border: 0;
  padding: 0;
  margin-block-start: var(--space-1);
  color: var(--color-text-muted);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: underline;
}

.mega-menu__more-toggle:hover,
.mega-menu__more-toggle:focus-visible {
  color: var(--color-text-default);
}

/* Focus-visible fix (component-scoped, Option A): a separate rule (not
   folded into the pair above) so the outline applies on keyboard focus
   only, never hover. Real <button>, Astra's static button:focus (0,1,1)
   wins outline:0 otherwise — this had the same "color changes, but width
   stays 0" pattern as .hitsiaijat-scroll-top before this fix. */
.mega-menu__more-toggle:focus-visible {
  outline: var(--border-width-active) solid var(--color-industrial-900);
  outline-offset: 2px;
}

.js .mega-menu__sublist--more {
  display: none;
}

.js .mega-menu__sublist--more.is-open {
  display: flex;
}

/* ---------- Desktop-only activation (>=1024px) ---------- */

@media (min-width: 1024px) {
  /* Positioning anchor for the panel below — .site-header__bar, not the
     narrow <li>, so the dropdown spans the same width as the header bar
     (and its .container max-width) instead of being squeezed to the width
     of the word "Tuotteet". Adds one property to an existing selector;
     header-footer.css itself is not touched. */
  .site-header__bar {
    position: relative;
  }

  /* Viewport-safe height (navigation-redesign-v1.md §2.1 / §2.2, fixing
     the overflow root-caused in navigation-audit-v1.md §3). The panel
     itself (`overflow: hidden`, no scrollbar of its own) never grows
     past this max-height; only .mega-menu__columns below scrolls. Two
     layers of fallback, weakest first, so every browser gets a real
     ceiling even before mega-menu.js runs:

     1. `100vh` — universal baseline. `140px` is a documented, generous
        ESTIMATE for the worst case (topbar still visible + unscrolled
        88px header bar, see header-footer.css:160 and this project's own
        note that the topbar's exact height was never SSH/live-measured —
        see navigation-audit-v1.md §6.2) — being slightly too conservative
        here only ever means a little more internal scrolling than
        strictly necessary, never overflow.
     2. `100dvh` where supported (`@supports` below) — corrects for
        mobile-browser chrome; irrelevant to this desktop-only component
        today, kept only because the spec explicitly asks for it and it
        costs nothing to include.
     3. `.has-measured-max-height` (added by mega-menu.js together with
        `--mega-menu-js-max-height` in the same operation, so this rule
        can never apply with the custom property still unset) — the real,
        live-measured value, using the header's actual current bottom
        edge (getBoundingClientRect()), which automatically already
        accounts for the WordPress admin bar offset, scrolled/unscrolled
        header height, and topbar visibility — see mega-menu.js. */
  .mega-menu {
    position: absolute;
    left: 0;
    right: 0;
    top: 100%;
    z-index: var(--z-dropdown);
    background-color: var(--color-bg-surface);
    border-bottom: var(--border-width-default) solid var(--color-border-200);
    box-shadow: var(--shadow-card-hover);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    max-height: calc(100vh - 140px - var(--space-6));
  }

  @supports (height: 100dvh) {
    .mega-menu {
      max-height: calc(100dvh - 140px - var(--space-6));
    }
  }

  .mega-menu.has-measured-max-height {
    max-height: var(--mega-menu-js-max-height);
  }

  /* Navigation v2.2 visual refinement: was --space-6 (32px) top AND
     bottom, plus .mega-menu__view-all's own margin/padding stacked
     underneath it, so a short 4-group panel ended with roughly 100px of
     compounded empty space below the last real link. Bottom padding is now
     0 — .mega-menu__view-all supplies its own top spacing (margin-block-start
     + padding-block-start, see above) — and top padding is trimmed from
     32px to 16px, so the panel's total height tracks its real content
     instead of a fixed generous allowance. */
  .mega-menu__inner {
    padding-block: var(--space-4) 0;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
  }

  .mega-menu__columns {
    display: grid;
    /* 12rem is a practical column-width floor for this content (short
       category labels), not a design token — same category of implementation
       value as header-footer.css's existing `min(46vw, 160px)` brand-width
       rule; not one of tokens.css's named sizes. */
    grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
    gap: var(--space-5) var(--space-6);
    /* The only element inside the panel that actually scrolls — see the
       .mega-menu comment above. overscroll-behavior:contain stops a
       scroll that reaches the top/bottom edge of this list from
       "leaking" into scrolling the page behind the (non-modal) panel. */
    overflow-y: auto;
    overflow-x: hidden;
    overscroll-behavior: contain;
    min-height: 0;
  }

  @supports (scrollbar-gutter: stable) {
    .mega-menu__columns {
      scrollbar-gutter: stable;
    }
  }

  .js .mega-menu {
    display: none;
  }

  .js .mega-menu.is-open {
    display: flex;
  }
}

/* Defensive backstop: .site-nav (and therefore this whole component) is
   already hidden below 1024px in header-footer.css — this just makes sure
   the panel itself can never appear even if that rule ever changes
   independently of this file. */
@media (max-width: 1023.98px) {
  .mega-menu {
    display: none;
  }
}
