/* Signed-in header: brand, primary menu and account menu. */
.site-header {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 6px var(--gutter);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}

.site-header__brand {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--color-text);
  text-decoration: none;
}

.site-header__logo {
  width: 56px;
  height: 56px;
}

/* The drawn name stands in for the title, which stays for screen readers. */
.site-header__title {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.site-header__brand .wordmark {
  height: 28px;
}

/* The pond takes the header's empty space, so the ducklings sit by the account icon and the first
   one has room to swim without crossing the menu (docs/visual-design.md, Ducklings). */
.primary-nav {
  flex: none;
}

.site-header__pond {
  display: flex;
  flex: 1;
  justify-content: flex-end;
  min-width: 0;
  container-type: inline-size;
}

/* Each time a page appears the first duckling swims once to the pond's far edge and back, then
   stays still: about 3.5 seconds out facing left and 3.5 back, a second after the page appears.
   `translate` and `scale` carry the swim and `transform` the bob, so the two compose. Turbo renders
   the header afresh on every visit, which starts the animation again (docs/visual-design.md). */
.site-header__pond .ducklings__duck--1 {
  animation: duckling-swim 8s ease-in-out 1s, duckling-bob 8s linear 1s;
}

/* A cached preview is followed by the fresh page, so only the fresh page swims. */
html[data-turbo-preview] .site-header__pond .ducklings__duck--1 {
  animation: none;
}

@keyframes duckling-swim {
  0% { translate: 0; scale: 1 1; }
  2% { translate: 0; scale: -1 1; }
  45% { translate: calc(var(--ducklings-width) - 100cqw); scale: -1 1; }
  50% { translate: calc(var(--ducklings-width) - 100cqw); scale: 1 1; }
  95%, 100% { translate: 0; scale: 1 1; }
}

@keyframes duckling-bob {
  0%, 2%, 47.5%, 95%, 100% { transform: none; }
  7%, 17%, 27%, 37%, 55%, 65%, 75%, 85% { transform: translateY(-2px) rotate(-4deg); }
  12%, 22%, 32%, 42%, 60%, 70%, 80%, 90% { transform: translateY(1px) rotate(2deg); }
}

@media (prefers-reduced-motion: reduce) {
  .site-header__pond .ducklings__duck--1 {
    animation: none;
  }
}

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

.primary-nav__item {
  display: block;
  padding: 7px 14px;
  border-radius: 999px;
  color: var(--color-muted);
  font-size: 1rem;
  text-decoration: none;
  white-space: nowrap;
}

a.primary-nav__item:hover {
  background: var(--color-bg);
  color: var(--color-text);
}

/* Icons belong to the phone's bottom bar. */
.primary-nav__icon {
  display: none;
}

.primary-nav__item--disabled {
  cursor: default;
  opacity: 0.6;
}

/* Family submenu in the primary menu. */
.nav-menu {
  position: relative;
}

.nav-menu__toggle {
  list-style: none;
  cursor: pointer;
}

.nav-menu__toggle::-webkit-details-marker {
  display: none;
}

.nav-menu__toggle::after {
  content: "\25BE"; /* small down triangle */
  margin-left: 4px;
  font-size: 0.75em;
}

.nav-menu[open] .nav-menu__toggle,
.nav-menu__toggle:hover {
  background: var(--color-bg);
  color: var(--color-text);
}

/* More specific than the hover and open rules, so the current section stays a lime pill. */
.primary-nav .primary-nav__item--current,
.primary-nav .nav-menu[open] .primary-nav__item--current {
  background: var(--color-brand);
  color: var(--color-bg);
  font-weight: 600;
}

.nav-menu__panel {
  position: absolute;
  left: 0;
  top: calc(100% + 6px);
  z-index: 10;
  min-width: 160px;
  padding: 4px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgb(1 4 9 / 0.6);
}

/* Same hover bridge as the account menu. */
/* The last menu sits near the right edge, so its panel opens leftwards to stay on screen. */
.primary-nav li:last-child .nav-menu__panel {
  left: auto;
  right: 0;
}

.nav-menu__panel::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: -8px;
  height: 8px;
}

.nav-menu__item {
  display: block;
  padding: 6px 10px;
  border-radius: var(--radius);
  color: var(--color-text);
  font-size: 1rem;
  text-decoration: none;
  white-space: nowrap;
}

a.nav-menu__item:hover {
  background: var(--color-brand);
  color: var(--color-bg);
}

.nav-menu__item[aria-current="page"] {
  font-weight: 600;
}

.nav-menu__item--disabled {
  color: var(--color-muted);
  cursor: default;
  opacity: 0.6;
}

.user-menu {
  position: relative;
}

.user-menu__toggle {
  display: block;
  padding: 2px;
  color: var(--color-muted);
  font-size: 32px;
  line-height: 1;
  list-style: none;
  cursor: pointer;
}

.user-menu__toggle::-webkit-details-marker {
  display: none;
}

.user-menu[open] .user-menu__toggle,
.user-menu__toggle:hover {
  color: var(--color-text);
}

.user-menu__panel {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  z-index: 10;
  width: max-content;
  min-width: 140px;
  max-width: calc(100vw - 2 * var(--gutter));
  box-sizing: border-box;
  padding: 4px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgb(1 4 9 / 0.6);
}

/* Bridges the gap below the icon so moving the mouse to the panel keeps a hover-opened menu open. */
.user-menu__panel::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: -8px;
  height: 8px;
}

.user-menu__panel form {
  margin: 0;
}

/* Which account this is: information, not a menu item. */
.user-menu__email {
  margin: 0 0 4px;
  padding: 6px 14px 10px;
  border-bottom: 1px solid var(--color-border);
  color: var(--color-muted);
  font-size: 0.875rem;
  overflow-wrap: anywhere;
}

.user-menu__item {
  width: 100%;
  border: 0;
  background: none;
  text-align: left;
}

a.user-menu__item {
  display: block;
  box-sizing: border-box;
  padding: 6px 14px;
  border-radius: var(--radius);
  color: var(--color-text);
  text-decoration: none;
  white-space: nowrap;
}

.user-menu__item[aria-current="page"] {
  font-weight: 600;
}

.user-menu__item:hover {
  background: var(--color-brand);
  color: var(--color-bg);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* Too tight for the ducklings beside the menu: the header is as it was without them. */
@media (min-width: 680px) and (max-width: 889px) {
  .primary-nav {
    flex: 1;
    min-width: 0;
  }

  .site-header__pond {
    display: none;
  }
}

/* Just wide enough for the menu in the header, so its items sit closer together. */
@media (min-width: 680px) and (max-width: 789px) {
  .site-header {
    gap: 12px;
  }

  .primary-nav ul {
    gap: 2px;
  }

  .primary-nav__item {
    padding-right: 8px;
    padding-left: 8px;
  }
}

/* A phone: the name stays in the header and the menu moves to a bar at the thumb. */
@media (max-width: 679px) {
  .site-header {
    gap: 8px;
  }

  .site-header__brand {
    gap: 8px;
  }

  .site-header__logo {
    width: 52px;
    height: 52px;
  }

  .site-header__brand .wordmark {
    height: 26px;
  }

  .site-header ~ main {
    padding-bottom: calc(80px + env(safe-area-inset-bottom));
  }

  .primary-nav {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 20;
    padding-bottom: env(safe-area-inset-bottom);
    background: var(--color-surface);
    border-top: 1px solid var(--color-border);
  }

  .primary-nav ul {
    display: grid;
    grid-auto-columns: minmax(0, 1fr);
    grid-auto-flow: column;
    gap: 0;
  }

  .primary-nav__item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 8px 0 10px;
    border-radius: 0;
    font-size: 0.875rem;
  }

  .primary-nav__icon {
    display: block;
    font-size: 20px;
  }

  a.primary-nav__item:hover,
  .nav-menu[open] .nav-menu__toggle,
  .nav-menu__toggle:hover {
    background: none;
  }

  .primary-nav .primary-nav__item--current,
  .primary-nav .nav-menu[open] .primary-nav__item--current {
    background: none;
    color: var(--color-brand);
  }

  .nav-menu__toggle::after {
    display: none;
  }

  /* Submenus open upwards from the bar. */
  .nav-menu__panel {
    top: auto;
    bottom: calc(100% + 6px);
  }

  /* With five items the last but one is near the right edge too. */
  .primary-nav li:nth-last-child(2) .nav-menu__panel {
    left: auto;
    right: 0;
  }

  .nav-menu__panel::before {
    top: auto;
    bottom: -8px;
  }
}
