/* =====================================================================
   Site header — modelled on cmu.edu
   ---------------------------------------------------------------------
   At the top of the page the gold square logo hangs below the bar.
   Once the page scrolls, `.is-compact` is added to the header and the
   logo turns into the wide two-line plate in four steps (as on cmu.edu):

     to compact:   1 fade out stacked text  2 shrink height (mark slides
                   + scales into place)     3 widen   4 fade in wide text
     back to top:  1 fade out wide text     2 narrow  3 grow height
                   (mark slides back)       4 fade in stacked text

   Each step lasts --logo-step. Tweak the variables below to tune it.
   ===================================================================== */

:root {
  --util-h: 40px;                 /* utility row height                     */
  --bar-h: 64px;                  /* primary row height                     */
  --logo-lg: 200px;               /* edge of the square logo                */
  --logo-sm: var(--bar-h);        /* height of the wide logo (fills bar)    */
  --logo-sm-w: calc(var(--logo-sm) * 840.3 / 180); /* wide logo aspect    */
  --logo-step: .3s;               /* duration of each animation step        */
  --logo-ease: cubic-bezier(.65, 0, .35, 1);
  --logo-hover: .2s;              /* colour fade, as on the .btn buttons    */
  /* border of the hover style, as in logo_square_fullname_whitebg_border.svg */
  --logo-border: max(2px, calc(var(--logo-lg) * 2.81 / 184));
}

.site-header {
  position: fixed;
  inset: 0 0 auto 0;
  z-index: 100;
  background: var(--bg-chrome);
  color: var(--fg);
  transition: transform var(--logo-step) var(--logo-ease),
              box-shadow var(--logo-step) ease;
}

/* On white, a hairline keeps the header apart from the page. */
[data-theme="light"] .site-header { box-shadow: 0 1px 0 rgba(0, 0, 0, .08); }

/* Slide the utility row out of view once scrolled. */
.site-header.is-compact {
  transform: translateY(calc(-1 * var(--util-h)));
  box-shadow: 0 2px 14px var(--shadow);
}

/* Used on first paint so a page reloaded mid-scroll doesn't animate. */
.site-header.no-transition,
.site-header.no-transition * {
  transition: none !important;
}

/* ---------- Utility row ---------- */

.utility {
  height: var(--util-h);
  padding-inline: var(--pad);
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
}

.utility ul {
  display: flex;
  align-items: center;
  gap: 24px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.utility__actions { gap: 10px !important; }

.utility__links a {
  color: rgba(var(--fg-rgb), .72);
  text-decoration: none;
  transition: color .2s;
}

.utility__links a:hover,
.utility__links a:focus-visible { color: var(--fg); }

/* ---------- Primary row ---------- */

.bar {
  position: relative;
  height: var(--bar-h);
  margin-inline: var(--pad);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
}

/* Gold hairline the logo hangs from */
.bar::before {
  content: "";
  position: absolute;
  inset: 0 0 auto 0;
  height: 1px;
  background: var(--gold);
}

/* ---------- Logo ---------- */

.brand {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
  display: block;
  width: var(--logo-lg);
  height: var(--logo-lg);
  overflow: hidden;
  background: var(--gold);
  color: #fff;
  /* returning to the square: narrow first, then grow */
  transition:
    width  var(--logo-step) var(--logo-ease) var(--logo-step),
    height var(--logo-step) var(--logo-ease) calc(var(--logo-step) * 2),
    background-color var(--logo-hover) ease;
}

.is-compact .brand {
  width: var(--logo-sm-w);
  height: var(--logo-sm);
  /* going compact: shrink first, then widen */
  transition:
    height var(--logo-step) var(--logo-ease) var(--logo-step),
    width  var(--logo-step) var(--logo-ease) calc(var(--logo-step) * 2),
    background-color var(--logo-hover) ease;
}

.brand:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: calc(-3px - var(--logo-border));
}

.brand svg {
  position: absolute;
  display: block;
  fill: currentColor;
  overflow: visible;
}

/* ---------- Logo hover ----------
   Pointing at the logo turns the gold plate into the white, gold-bordered
   version (logo_square_fullname_whitebg_border.svg / logo_header_2line_black.svg
   plus a gold border): a plain colour fade, the same as the buttons'. */

.brand::before {
  content: "";
  position: absolute;
  inset: var(--logo-border);
  background: #fff;
  opacity: 0;
  transition: opacity var(--logo-hover) ease;
}

/* Each shape fades to its new colour (fill follows `color`). */
.brand svg > * {
  fill: currentColor;
  transition: color var(--logo-hover) ease;
}

.brand:focus-visible::before { opacity: 1; }
.brand:focus-visible .brand__mark { color: var(--gold); }
.brand:focus-visible .brand__stack,
.brand:focus-visible .brand__line { color: #000; }
.brand:focus-visible .brand__line rect { color: #d4d4d8; }

@media (hover: hover) {
  .brand:hover::before { opacity: 1; }
  .brand:hover .brand__mark { color: var(--gold); }
  .brand:hover .brand__stack,
  .brand:hover .brand__line { color: #000; }
  .brand:hover .brand__line rect { color: #d4d4d8; }
}

/* ---------- Past the hero ----------
   Once the rankings reach the bar (`.is-plain`, from js/header.js; and on
   every page without the hero) the logo takes the bar's own colours, with
   no border: the gold mark and the name in the text colour, on white in
   light mode and on the dark bar in dark mode. Pointing at it brings back
   the gold plate with the white mark and name. */

.is-plain .brand {
  background: var(--bg-chrome);
  box-shadow: inset 0 1px 0 var(--gold);      /* the bar's gold hairline runs on across it */
}
.is-plain .brand::before { opacity: 0; }
.is-plain .brand__mark { color: var(--gold); }
.is-plain .brand__stack,
.is-plain .brand__line { color: var(--fg); }
.is-plain .brand__line rect { color: rgba(var(--fg-rgb), .2); }

.is-plain .brand:focus-visible { background: var(--gold); }
.is-plain .brand:focus-visible::before { opacity: 0; }
.is-plain .brand:focus-visible .brand__mark,
.is-plain .brand:focus-visible .brand__stack,
.is-plain .brand:focus-visible .brand__line,
.is-plain .brand:focus-visible .brand__line rect { color: #fff; }

@media (hover: hover) {
  .is-plain .brand:hover { background: var(--gold); }
  .is-plain .brand:hover::before { opacity: 0; }
  .is-plain .brand:hover .brand__mark,
  .is-plain .brand:hover .brand__stack,
  .is-plain .brand:hover .brand__line,
  .is-plain .brand:hover .brand__line rect { color: #fff; }
}

/* The mark: positions match the two source SVGs exactly.
   Square:  translate(-27.52 -43.45) scale(.53) in a 184-unit box.
   Wide:    translate(80 0) in an 840.3 × 180 box. */
.brand__mark {
  left:   calc(var(--logo-lg) * 22.08  / 184);
  top:    calc(var(--logo-lg) * 22.05  / 184);
  width:  calc(var(--logo-lg) * 84.005 / 184);
  height: calc(var(--logo-lg) * 53     / 184);
  transition-property: left, top, width, height;
  transition-duration: var(--logo-step);
  transition-timing-function: var(--logo-ease);
  transition-delay: calc(var(--logo-step) * 2);   /* moves with the height */
}

.is-compact .brand__mark {
  left:   calc(var(--logo-sm) * 70    / 180);
  top:    calc(var(--logo-sm) * 40    / 180);
  width:  calc(var(--logo-sm) * 158.5 / 180);
  height: calc(var(--logo-sm) * 100   / 180);
  transition-delay: var(--logo-step);
}

.brand__stack {
  left: 0;
  top: 0;
  width: var(--logo-lg);
  height: var(--logo-lg);
  transition:
    opacity var(--logo-step) ease calc(var(--logo-step) * 3),
    visibility 0s linear calc(var(--logo-step) * 3);
}

.is-compact .brand__stack {
  opacity: 0;
  visibility: hidden;
  transition:
    opacity var(--logo-step) ease,
    visibility 0s linear var(--logo-step);
}

.brand__line {
  left: 0;
  top: 0;
  width: var(--logo-sm-w);
  height: var(--logo-sm);
  opacity: 0;
  visibility: hidden;
  transition:
    opacity var(--logo-step) ease,
    visibility 0s linear var(--logo-step);
}

.is-compact .brand__line {
  opacity: 1;
  visibility: visible;
  transition:
    opacity var(--logo-step) ease calc(var(--logo-step) * 3),
    visibility 0s linear calc(var(--logo-step) * 3);
}

/* ---------- Main nav ---------- */

.nav ul {
  display: flex;
  margin: 0;
  padding: 0;
  list-style: none;
}

.nav a {
  position: relative;
  display: block;
  padding: 10px 14px;
  color: var(--fg);
  font-size: 15px;
  font-weight: 600;
  letter-spacing: .01em;
  text-decoration: none;
}

/* Underline that swipes in from the left */
.nav a::after {
  content: "";
  position: absolute;
  left: 14px;
  right: 14px;
  bottom: 4px;
  height: 2px;
  background: var(--gold);
  transform: scaleX(0);
  transform-origin: left;
  transition: transform .25s ease;
}

.nav a:hover::after,
.nav a:focus-visible::after,
.nav a[aria-current="page"]::after { transform: scaleX(1); }   /* the page you are on */

/* ---------- Icon buttons ---------- */

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  background: none;
  color: var(--fg);
  cursor: pointer;
  transition: color .2s;
}

.icon-btn:hover,
.icon-btn:focus-visible { color: var(--accent); }

/* ---------- Light / dark switch: a gold square, like the gold buttons ----------
   Full bar height, so it hangs from the gold hairline like the logo does. */

.theme-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  align-self: stretch;            /* gold hairline to the bottom of the bar */
  width: var(--bar-h);
  margin-left: 10px;              /* right edge sits on the page margin */
  padding: 0;
  border: 0;
  background: var(--gold);
  color: var(--ink);
  cursor: pointer;
  transition: background-color .2s, color .2s;
}

.theme-btn:focus-visible { background: #fff; }
[data-theme="light"] .theme-btn:focus-visible { background: var(--ink); color: #fff; }

@media (hover: hover) {             /* a tap on a phone shouldn't leave it lit */
  .theme-btn:hover { background: #fff; }
  [data-theme="light"] .theme-btn:hover { background: var(--ink); color: #fff; }
}

.theme-btn:focus-visible { outline: 2px solid var(--gold); outline-offset: 3px; }

.theme-btn svg { display: block; width: 24px; height: 24px; }
.theme-btn .theme-btn__moon,
[data-theme="light"] .theme-btn .theme-btn__sun { display: none; }
[data-theme="light"] .theme-btn .theme-btn__moon { display: block; }

.menu-btn { display: none; }

.menu-btn__bars,
.menu-btn__bars::before,
.menu-btn__bars::after {
  display: block;
  width: 22px;
  height: 2px;
  background: currentColor;
  transition: transform .25s ease, background-color .25s ease;
}

.menu-btn__bars { position: relative; }

.menu-btn__bars::before,
.menu-btn__bars::after {
  content: "";
  position: absolute;
  left: 0;
}

.menu-btn__bars::before { transform: translateY(-7px); }
.menu-btn__bars::after  { transform: translateY(7px); }

.menu-btn[aria-expanded="true"] .menu-btn__bars { background-color: transparent; }
.menu-btn[aria-expanded="true"] .menu-btn__bars::before { transform: rotate(45deg); }
.menu-btn[aria-expanded="true"] .menu-btn__bars::after  { transform: rotate(-45deg); }

/* ---------- Small-screen menu ---------- */

.mobile-menu {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 3;                 /* covers the logo while it shrinks */
  max-height: calc(100vh - var(--bar-h));
  max-height: calc(100dvh - var(--bar-h));
  overflow-y: auto;
  padding: 8px var(--pad) 28px;
  background: var(--bg-chrome);
  border-top: 1px solid rgba(var(--fg-rgb), .08);
  box-shadow: 0 12px 24px var(--shadow);
}

.mobile-menu[hidden] { display: none; }

.mobile-menu ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

.mobile-menu__primary a {
  display: block;
  padding: 14px 0;
  border-bottom: 1px solid rgba(var(--fg-rgb), .1);
  color: var(--fg);
  font-size: 18px;
  font-weight: 600;
  text-decoration: none;
}

.mobile-menu__primary a[aria-current="page"] { color: var(--accent); }

.mobile-menu__secondary {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 16px;
  margin-top: 18px !important;
}

.mobile-menu__secondary a {
  display: block;
  padding: 8px 0;
  color: rgba(var(--fg-rgb), .72);
  font-size: 14px;
  text-decoration: none;
}

.mobile-menu__actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 20px;
}

/* ---------- Responsive ---------- */

@media (max-width: 1079px) {
  :root {
    --util-h: 0px;
    --bar-h: 56px;
    --logo-lg: 128px;
  }

  .utility,
  .nav { display: none; }

  .theme-btn { margin-left: 0; }
  .theme-btn svg { width: 22px; height: 22px; }
  .menu-btn { display: inline-flex; margin-right: -10px; }
}

/* Phones: the compact logo is about 261px wide, so the switch narrows to keep
   clear of it (still the full height of the bar). */
@media (max-width: 479px) {
  .theme-btn { width: 40px; }
}

@media (max-width: 379px) {
  :root {
    --bar-h: 48px;
    --logo-lg: 112px;
  }
}

@media (prefers-reduced-motion: reduce) {
  :root { --logo-step: 0s; --logo-hover: 0s; }
}
