/* Site chrome: the top bar, and the slim bar that replaces it once the top
   bar has scrolled out of view. */

.topbar {
  height: var(--topbar-h);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 1rem;
  padding: 0 var(--gutter);
}

.nav a,
.topbar__cn {
  display: inline-block;
  padding: 0.3em 0.6em;
  border-radius: 0.5em;
  text-decoration: none;
  transition: background 0.15s var(--ease), transform 0.15s var(--ease);
}

.topbar__cn {
  justify-self: start;
  font-family: "PingFang TC", "Hiragino Sans CNS", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  font-size: 1.15rem;
  white-space: nowrap;
}

/* Collapsed to nothing rather than hidden, so the two characters take up no
   room until they are asked for and the header doesn't shift when they arrive. */
.topbar__cn-more {
  display: inline-block;
  max-width: 0;
  opacity: 0;
  overflow: hidden;
  vertical-align: top;
  transition: max-width 0.26s var(--ease), opacity 0.26s var(--ease);
}

.topbar__cn:hover .topbar__cn-more,
.topbar__cn:focus-visible .topbar__cn-more {
  max-width: 1.3em;
  opacity: 1;
}

/* One after the other, the pair landing inside 0.5s. */
.topbar__cn:hover .topbar__cn-more:nth-of-type(2),
.topbar__cn:focus-visible .topbar__cn-more:nth-of-type(2) {
  transition-delay: 0.22s;
}

.nav {
  display: flex;
  gap: clamp(0.9rem, 3vw, 2.5rem);
  font-size: 0.83rem;
  letter-spacing: 0.015em;
}

/* A touch of magnification and a grey ground, as annotated. */
.nav a:hover {
  background: rgba(20, 17, 15, 0.055);
  transform: scale(1.04);
}

.topbar__cn:hover { background: rgba(20, 17, 15, 0.055); }

.nav a[aria-current="true"] {
  text-decoration: underline;
  text-decoration-thickness: 1.6px;
  text-underline-offset: 0.36em;
}

.socials {
  justify-self: end;
  display: flex;
  align-items: center;
  gap: 0.85rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.socials a {
  display: grid;
  place-items: center;
  color: var(--ink);
  transition: opacity 0.15s var(--ease), transform 0.15s var(--ease);
}

.socials svg {
  width: 1.05rem;
  height: 1.05rem;
}

.socials a:hover {
  opacity: 0.55;
  transform: scale(1.12);
}

/* ------------------------------------------------------------------ rail */
/* Once the top bar has scrolled away the nav comes back at the top left, as a
   slim bar over the page -- nothing in the layout moves. It spans the full
   width on purpose: a small floating pill leaves body text peeking out beside
   it, which just reads as breakage. */

.rail {
  position: fixed;
  inset: 0 0 auto 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1.5rem;
  height: 3.1rem;
  padding: 0 var(--gutter);
  font-size: 0.83rem;
  background: rgba(244, 244, 242, 0);
  border-bottom: 1px solid transparent;
  backdrop-filter: blur(9px);
  opacity: 0;
  /* visibility, not just opacity: it takes the links out of the tab order
     while the bar is hidden, instead of leaving invisible focus stops. */
  visibility: hidden;
  transition: opacity 0.24s var(--ease), background 0.24s var(--ease),
    border-color 0.24s var(--ease), visibility 0s linear 0.24s;
}

.rail__nav {
  display: flex;
  gap: clamp(0.9rem, 2.2vw, 1.9rem);
}

.rail__socials {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.rail .rail__socials a {
  display: grid;
  place-items: center;
  /* comes in from its own side of the bar */
  translate: 10px 0;
}

.rail__socials svg {
  width: 1rem;
  height: 1rem;
}

.rail a {
  color: var(--ink-soft);
  text-decoration: none;
  opacity: 0;
  translate: -10px 0;
  transition: opacity 0.42s var(--ease), translate 0.42s var(--ease), color 0.15s var(--ease);
}

.rail a:hover { color: var(--ink); }

.rail a[aria-current="true"] {
  color: var(--ink);
  text-decoration: underline;
  text-underline-offset: 0.36em;
}

body.nav-docked .rail {
  opacity: 1;
  visibility: visible;
  background: var(--bar);
  border-bottom-color: var(--bar-line);
  transition-delay: 0s;
}

body.nav-docked .rail a {
  opacity: 1;
  translate: 0 0;
}

/* Staggered only on the way in; leaving should be quick and uniform.
   The words land one at a time, then the icons sweep in behind them. */
body.nav-docked .rail__nav a:nth-child(1) { transition-delay: 0.06s; }
body.nav-docked .rail__nav a:nth-child(2) { transition-delay: 0.17s; }
body.nav-docked .rail__nav a:nth-child(3) { transition-delay: 0.28s; }
body.nav-docked .rail__socials li:nth-child(1) a { transition-delay: 0.36s; }
body.nav-docked .rail__socials li:nth-child(2) a { transition-delay: 0.42s; }
body.nav-docked .rail__socials li:nth-child(3) a { transition-delay: 0.48s; }
body.nav-docked .rail__socials li:nth-child(4) a { transition-delay: 0.54s; }
body.nav-docked .rail__socials li:nth-child(5) a { transition-delay: 0.6s; }


@media (max-width: 62rem) {
  /* Name and socials share the first row; the nav gets its own, because all
     three side by side overflow the viewport at phone widths. */
  .topbar {
    height: auto;
    grid-template-columns: auto 1fr;
    row-gap: 0.9rem;
    padding: 1rem var(--gutter);
  }

  .topbar__cn { grid-column: 1; grid-row: 1; }

  .socials {
    grid-column: 2;
    grid-row: 1;
    justify-self: end;
  }

  .nav {
    grid-column: 1 / -1;
    grid-row: 2;
    justify-self: center;
    gap: clamp(1rem, 7vw, 2.5rem);
  }

  /* No room for a rail over the content at this width. */
  .rail { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .nav a:hover,
  .socials a:hover { transform: none; }
}
