/*
  navbar.css
  The sticky top bar: logo, links, theme toggle, contact button, and the mobile menu.
  js/navbar.js adds these classes:
    .is-scrolled   on .navbar       -> page has scrolled, bar gets a background
    .is-open       on .navbar       -> mobile menu is open
    .is-active     on .navbar__link -> link for the section currently on screen
    .is-hidden     on .navbar       -> scrolling down, bar slides up out of the way
    .has-indicator on .navbar__links -> the sliding underline is showing
*/

.navbar {
  position: fixed;
  inset: 0 0 auto 0;
  z-index: var(--z-navbar);
  height: var(--navbar-height);
  border-bottom: 1px solid transparent;
  transition:
    background-color var(--duration-base) var(--ease-out),
    border-color var(--duration-base) var(--ease-out),
    transform var(--duration-ui) var(--ease-out);
}

/* Slide the bar away while reading down; it comes back as soon as you scroll up.
   Kept visible while a keyboard user is tabbing inside it.
   Tricky part: never leave a transform on the bar when the menu is open, because a
   transform (like backdrop-filter below) would squash the full-screen menu. */
@media (prefers-reduced-motion: no-preference) {
  .navbar.is-hidden:not(.is-open):not(:has(:focus-visible)) {
    transform: translateY(-100%);
  }
}

/* Frosted bar once the page scrolls. Solid colour fallback comes first. */
.navbar.is-scrolled,
.navbar.is-open {
  background-color: var(--color-bg);
  border-bottom-color: var(--color-border);
}

/* Tricky part: backdrop-filter makes the bar the "box" that fixed children are placed in,
   which would squash the full-screen mobile menu to the bar's height.
   So the blur is switched off while the menu is open (the bar is solid then anyway). */
@supports (backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px)) {
  .navbar.is-scrolled:not(.is-open) {
    background-color: color-mix(in srgb, var(--color-bg) 78%, transparent);
    -webkit-backdrop-filter: blur(16px) saturate(140%);
    backdrop-filter: blur(16px) saturate(140%);
  }
}

.navbar__inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-5);
  height: 100%;
}

/* "Ashan." wordmark. overflow: hidden is the mask the word rises out of on load. */
.navbar__logo {
  display: inline-flex;
  align-items: baseline;
  overflow: hidden;
  font-family: var(--font-display);
  font-size: 1.6rem;
  letter-spacing: 0.02em;
  line-height: 1.1;
}

.navbar__logo-word,
.navbar__logo-dot {
  display: inline-block;
}

.navbar__logo-dot {
  color: var(--color-accent);
}

@media (prefers-reduced-motion: no-preference) {
  /* Load-in: the word rises like the hero name, then the dot pops in.
     "backwards" fill only holds the start state, so the hover below still works afterwards. */
  .navbar__logo-word {
    animation: logo-rise var(--duration-slow) var(--ease-out) backwards;
  }

  .navbar__logo-dot {
    animation: logo-dot-in var(--duration-base) var(--ease-out) 380ms backwards;
    transition: transform var(--duration-fast) var(--ease-out);
  }
}

/* Hover: the dot lifts a little, a hint that the logo goes back to the top */
@media (prefers-reduced-motion: no-preference) and (hover: hover) and (pointer: fine) {
  .navbar__logo:hover .navbar__logo-dot {
    transform: translateY(-0.18em);
  }
}

@keyframes logo-rise {
  from {
    transform: translateY(110%);
  }
}

@keyframes logo-dot-in {
  from {
    opacity: 0;
    transform: scale(0.6);
  }
}

.navbar__links {
  position: relative;
  display: flex;
  gap: var(--space-6);
}

.navbar__link {
  position: relative;
  padding-block: var(--space-2);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  transition: color var(--duration-fast) var(--ease-out);
}

.navbar__link:hover,
.navbar__link.is-active {
  color: var(--color-text);
}

/* ---------- Desktop underlines ---------- */
@media (min-width: 900px) {
  /* One shared underline that slides to the active link.
     It's 1px wide; js/navbar.js sets --indicator-x / --indicator-w and scaleX stretches it,
     so only transform moves and nothing re-lays out. */
  .navbar__links::after {
    content: "";
    position: absolute;
    left: 0;
    bottom: 0;
    width: 1px;
    height: 1px;
    background: var(--color-accent);
    transform-origin: left;
    transform: translateX(var(--indicator-x, 0)) scaleX(var(--indicator-w, 0));
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--duration-fast) var(--ease-out);
  }

  .navbar__links.has-indicator::after {
    opacity: 1;
  }

  /* Thin underline that grows in from the left on hover
     (the active link already has the sliding one) */
  .navbar__link::after {
    content: "";
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 1px;
    background: var(--color-accent);
    transform: scaleX(0);
    transform-origin: left;
    transition: transform var(--duration-base) var(--ease-out);
  }
}

@media (min-width: 900px) and (prefers-reduced-motion: no-preference) {
  .navbar__links.has-indicator::after {
    transition:
      opacity var(--duration-fast) var(--ease-out),
      transform var(--duration-ui) var(--ease-out);
  }
}

@media (min-width: 900px) and (hover: hover) and (pointer: fine) {
  .navbar__link:not(.is-active):hover::after {
    transform: scaleX(1);
  }
}

/* Contact already has a button on desktop, so this link only shows in the mobile menu */
.navbar__link--mobile-only {
  display: none;
}

/* GitHub / LinkedIn row: the hero already has these on desktop, so mobile menu only */
.navbar__profiles {
  display: none;
}

.navbar__actions {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

/* Theme toggle shows a sun in dark mode and a moon in light mode */
.theme-icon--moon {
  display: none;
}

[data-theme-current="light"] .theme-icon--sun {
  display: none;
}

[data-theme-current="light"] .theme-icon--moon {
  display: inline;
}

/* Hamburger button: hidden on desktop.
   Both icons sit in the same grid cell; the list icon turns away as the X turns in. */
.navbar__toggle {
  display: none;
}

.navbar__toggle > i {
  grid-area: 1 / 1;
  transition:
    opacity var(--duration-fast) var(--ease-out),
    transform var(--duration-fast) var(--ease-out);
}

.navbar__toggle-close,
.navbar.is-open .navbar__toggle-open {
  opacity: 0;
}

.navbar.is-open .navbar__toggle-close {
  opacity: 1;
}

/* The turn only for visitors who haven't asked for less motion; others get the plain crossfade */
@media (prefers-reduced-motion: no-preference) {
  .navbar__toggle-close {
    transform: rotate(-90deg) scale(0.8);
  }

  .navbar.is-open .navbar__toggle-close {
    transform: none;
  }

  .navbar.is-open .navbar__toggle-open {
    transform: rotate(90deg) scale(0.8);
  }
}

/* ---------- Tablet and phone: links move into a drop-down panel ---------- */
@media (max-width: 899px) {
  .navbar__toggle {
    display: inline-grid;
  }

  .navbar__menu {
    position: fixed;
    inset: var(--navbar-height) 0 0 0;
    z-index: var(--z-menu);
    padding: var(--space-7) var(--gutter);
    background: var(--color-bg);
    opacity: 0;
    visibility: hidden;
    transition:
      opacity var(--duration-ui) var(--ease-out),
      visibility 0s linear var(--duration-ui);
  }

  .navbar.is-open .navbar__menu {
    opacity: 1;
    visibility: visible;
    transition: opacity var(--duration-ui) var(--ease-out);
  }

  .navbar__links {
    flex-direction: column;
    gap: var(--space-2);
  }

  /* Opening: the big links rise in one after another, then the profile row.
     Closing: no stagger, they leave together with the panel's fade. */
  @media (prefers-reduced-motion: no-preference) {
    .navbar__links > li,
    .navbar__profiles {
      opacity: 0;
      transform: translateY(16px);
      transition:
        opacity var(--duration-ui) var(--ease-out),
        transform var(--duration-ui) var(--ease-out);
    }

    .navbar.is-open .navbar__links > li,
    .navbar.is-open .navbar__profiles {
      opacity: 1;
      transform: none;
      /* --reveal-index comes from data-stagger (js/motion.js) and an inline style in index.html */
      transition-delay: calc(60ms + var(--reveal-index, 0) * 50ms);
    }
  }

  .navbar__link {
    display: block;
    font-family: var(--font-display);
    font-size: clamp(2.5rem, 10vw, 4rem);
    line-height: 1.15;
    text-transform: uppercase;
    color: var(--color-text);
  }

  .navbar__link.is-active {
    color: var(--color-accent);
  }

  .navbar__link--mobile-only {
    display: block;
  }

  .navbar__profiles {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3);
    margin-top: var(--space-7);
    padding-top: var(--space-5);
    border-top: 1px solid var(--color-border);
  }

  .navbar__profile {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    min-height: 48px;
    padding: 0 var(--space-5);
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius-control);
    font-size: var(--text-sm);
    font-weight: 500;
  }

  .navbar__profile:hover {
    border-color: var(--color-accent);
    color: var(--color-accent);
  }
}

/* Small phones: hide the contact button (Contact is in the menu instead) */
@media (max-width: 559px) {
  .navbar__cta {
    display: none;
  }
}
