/* The header bar on every public page: logo, main menu with its Features panel,
   language picker, Discord link, account link and the phone menu. Below 760px it
   shares one layout with the top bars of the dashboard (dashboard/css/shell.css)
   and the operator panel (operator/css/base/small-screens.css). */

@layer layout {
  .site-header {
    height: 92px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 24px;
    position: relative;
    width: 100%;
    padding-inline: max(24px, calc((100% - 1200px) / 2));
    border-bottom: 1px solid var(--border);
    background: var(--bg-secondary);
  }

  /* Pages with the main menu keep the header on screen. site.js marks it
     data-compact once the page has scrolled, and it shrinks to 64px on
     screens wider than 800px. The shared tool pages under /r/, /s/ and /comp/
     have only the logo, and toolbars of their own that stick, so their header
     scrolls away. */
  .site-header:has(.desktop-nav) {
    position: sticky;
    top: 0;
    z-index: 30;
    transition:
      height 200ms ease,
      box-shadow 200ms ease;
  }

  /* Section links land below the sticky header, not under it. */
  html:has(.site-header .desktop-nav) {
    scroll-padding-top: 88px;
  }

  .brand {
    display: inline-flex;
    flex: none;
    align-items: center;
    font-size: 23px;
    font-weight: 700;
    letter-spacing: -0.6px;
    white-space: nowrap;
    line-height: 1;
  }

  /* Logos are sized by one dimension only. The ratio, no max-width and
     object-fit keep a narrow box (Safari's flex sizing) from squashing them. */
  .brand-logo {
    display: block;
    width: auto;
    max-width: none;
    height: 48px;
    aspect-ratio: 720 / 156;
    object-fit: contain;
    transition: height 200ms ease;
  }

  .brand > span {
    font-weight: 500;
    color: var(--accent);
  }

  .desktop-nav,
  .header-actions {
    display: flex;
    align-items: center;
    gap: 10px;
  }

  .desktop-nav {
    gap: 4px;
  }

  .menu-toggle {
    display: none;
    place-items: center;
    width: 42px;
    height: 42px;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg-elevated);
    color: var(--text-primary);
  }

  .menu-toggle:hover {
    border-color: var(--border-hover);
  }

  .menu-toggle .icon-close,
  .menu-toggle[aria-expanded="true"] .icon-menu {
    display: none;
  }

  .menu-toggle[aria-expanded="true"] .icon-close {
    display: block;
  }

  .header-actions > .text-link {
    display: inline-flex;
    align-items: center;
    min-height: 42px;
    padding-inline: 6px;
  }
}

@layer components {
  /* The arrow sits close to its label, as the dashboard's back links do. */
  .site-header .back-to-site {
    gap: 6px;
  }

  .desktop-nav {
    margin-right: auto;
    padding-left: 20px;
    border-left: 1px solid var(--border-hover);
  }

  /* Links and the Features button: near-white text, a grey fill on hover and
     a dark copper fill for the current page or the open panel. Copper stays
     out of the text and is kept to the icons, like the Features chevron. */
  .desktop-nav > a,
  .features-toggle {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 40px;
    padding: 0 14px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--text-primary);
    font: inherit;
    font-size: 15px;
    font-weight: 500;
    white-space: nowrap;
    cursor: pointer;
  }

  .desktop-nav > a:hover,
  .features-toggle:hover {
    background: var(--bg-elevated);
  }

  .desktop-nav > a[aria-current],
  .features-toggle[aria-expanded="true"] {
    background: var(--nav-selected);
  }

  .desktop-nav > a[aria-current] {
    font-weight: 600;
  }

  .desktop-nav .nav-icon {
    flex: none;
    color: var(--accent);
  }

  .features-chevron {
    width: 16px;
    height: 16px;
    color: var(--accent);
    transition: transform 150ms ease;
  }

  .features-toggle[aria-expanded="true"] .features-chevron {
    transform: rotate(180deg);
  }

  /* The Features panel hangs from the header, lined up with the logo. */
  .features-menu {
    position: absolute;
    top: calc(100% + 8px);
    left: max(24px, calc((100% - 1200px) / 2));
    width: min(880px, calc(100% - 2 * max(24px, calc((100% - 1200px) / 2))));
    display: flex;
    gap: 18px;
    padding: 18px;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--bg-secondary);
    box-shadow: 0 18px 40px rgb(0 0 0 / 50%);
  }

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

  .features-menu-main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
  }

  .features-menu-list {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 4px;
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .features-menu-list a {
    display: flex;
    gap: 12px;
    align-items: flex-start;
    padding: 12px;
    border-radius: 10px;
  }

  .features-menu-icon {
    display: grid;
    place-items: center;
    flex: none;
    width: 40px;
    height: 40px;
    border-radius: 9px;
    background: var(--bg-elevated);
    color: var(--accent);
    transition:
      background 150ms ease,
      color 150ms ease;
  }

  .features-menu-list a:is(:hover, :focus-visible) {
    background: var(--bg-elevated);
  }

  .features-menu-list a:is(:hover, :focus-visible) .features-menu-icon {
    background: var(--accent);
    color: var(--accent-ink);
  }

  .features-menu-text {
    display: flex;
    flex-direction: column;
    gap: 3px;
  }

  .features-menu-title {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
  }

  .features-menu-text small {
    font-size: 13px;
    line-height: 1.4;
    color: var(--text-secondary);
  }

  .features-menu-all {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    min-height: 40px;
    margin-top: 8px;
    padding: 8px 12px 0;
    border-top: 1px solid var(--border);
    font-size: 13px;
    font-weight: 600;
    color: var(--accent);
  }

  .features-menu-all::before {
    content: "";
    width: 6px;
    height: 6px;
    background: var(--accent);
    transform: rotate(45deg);
  }

  .features-menu-all:hover {
    color: var(--accent-hover);
  }

  .features-menu-demo {
    display: flex;
    flex: none;
    flex-direction: column;
    width: 230px;
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--bg-card);
    transition: border-color 150ms ease;
  }

  .features-menu-demo:hover {
    border-color: var(--border-warm);
  }

  .features-menu-demo img {
    display: block;
    width: 100%;
    height: 110px;
    object-fit: cover;
  }

  .features-menu-demo .features-menu-text {
    gap: 6px;
    padding: 14px;
  }

  .features-menu-cta {
    font-size: 13px;
    font-weight: 600;
    color: var(--accent);
  }

  /* The language picker: a ghost button like Features, with a small panel of the
     three languages under it. The phone menu lists them instead (see below). */
  .language-picker {
    position: relative;
  }

  .language-toggle {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 40px;
    padding: 0 10px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--text-primary);
    font: inherit;
    font-size: 14px;
    font-weight: 500;
    white-space: nowrap;
    cursor: pointer;
  }

  .language-toggle:hover {
    background: var(--bg-elevated);
  }

  .language-toggle[aria-expanded="true"] {
    background: var(--nav-selected);
  }

  .language-toggle .ui-icon {
    color: var(--accent);
  }

  .language-menu {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    z-index: 5;
    min-width: 160px;
    margin: 0;
    padding: 6px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--bg-secondary);
    box-shadow: 0 18px 40px rgb(0 0 0 / 50%);
    list-style: none;
  }

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

  .language-menu a {
    display: flex;
    align-items: center;
    min-height: 40px;
    padding: 0 12px;
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 14px;
    font-weight: 500;
    white-space: nowrap;
  }

  .language-menu a:hover {
    background: var(--bg-elevated);
  }

  .language-menu a[aria-current] {
    background: var(--nav-selected);
    font-weight: 600;
  }

  /* The one solid button on the bar. */
  .account-link {
    display: inline-flex;
    align-items: center;
    min-height: 40px;
    padding: 0 18px;
    border: 1px solid var(--accent);
    border-radius: 8px;
    background: var(--accent);
    color: var(--accent-ink);
    font-size: 14px;
    font-weight: 600;
    white-space: nowrap;
    transition:
      background 150ms ease,
      border-color 150ms ease,
      color 150ms ease;
  }

  .account-link:hover {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
  }

  /* The front page's Add to Discord joins the bar once the hero's own button
     has scrolled away (see the breakpoints), and Your account steps back to
     an outline beside it. */
  .site-header .header-cta {
    display: none;
    min-height: 40px;
    padding: 0 18px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    white-space: nowrap;
  }

  .header-socials {
    display: flex;
    align-items: center;
  }

  .social-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg-elevated);
    color: var(--text-secondary);
    transition:
      color 150ms ease,
      border-color 150ms ease;
  }

  .social-link .ui-icon {
    width: 20px;
    height: 20px;
  }

  .social-link:is(:hover, :focus-visible) {
    color: var(--text-primary);
    border-color: var(--border-hover);
  }

  /* Phone menu: big rows with the current page in solid copper, a few
     popular features as chips, and Add to Discord at the end. */
  .mobile-menu {
    position: absolute;
    top: calc(100% + 8px);
    inset-inline: 20px;
    z-index: 5;
    display: flex;
    flex-direction: column;
    max-height: calc(100dvh - 100px);
    overflow-y: auto;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--bg-secondary);
    box-shadow: 0 18px 40px rgb(0 0 0 / 50%);
  }

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

  .mobile-menu-links,
  .mobile-menu-chips {
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .mobile-menu-links {
    display: flex;
    flex-direction: column;
    gap: 4px;
  }

  .mobile-menu-links a {
    display: flex;
    align-items: center;
    gap: 14px;
    min-height: 52px;
    padding: 0 14px;
    border-radius: 10px;
    color: var(--text-primary);
    font-size: 16px;
    font-weight: 500;
  }

  .mobile-menu-links .ui-icon {
    color: var(--text-muted);
  }

  /* A chevron drawn from two borders, so the rows need no extra markup. */
  .mobile-menu-links a::after {
    content: "";
    width: 8px;
    height: 8px;
    margin-left: auto;
    margin-right: 4px;
    border-top: 2px solid currentColor;
    border-right: 2px solid currentColor;
    color: var(--text-muted);
    transform: rotate(45deg);
  }

  .mobile-menu-links a.mobile-menu-external::after {
    content: none;
  }

  .mobile-menu-links a:hover {
    background: var(--bg-elevated);
  }

  .mobile-menu-links a[aria-current] {
    background: var(--accent);
    color: var(--accent-ink);
    font-weight: 600;
  }

  .mobile-menu-links a[aria-current] .ui-icon,
  .mobile-menu-links a[aria-current]::after {
    color: inherit;
  }

  /* The three languages as one row of chips under the page links. */
  .mobile-menu-languages {
    display: flex;
    gap: 8px;
    margin: 12px 0 0;
    padding: 12px 0 0;
    border-top: 1px solid var(--border);
    list-style: none;
  }

  /* On the 404 page the languages are all the phone menu holds. */
  .mobile-menu-languages:first-child {
    margin-top: 0;
    padding-top: 0;
    border-top: 0;
  }

  .mobile-menu-languages li {
    flex: 1;
  }

  .mobile-menu-languages a {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 44px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg-card);
    color: var(--text-primary);
    font-size: 14px;
    font-weight: 500;
  }

  .mobile-menu-languages a:hover {
    border-color: var(--border-warm);
  }

  .mobile-menu-languages a[aria-current] {
    border-color: var(--accent);
    background: var(--accent);
    color: var(--accent-ink);
    font-weight: 600;
  }

  .mobile-menu-label {
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 16px 4px 8px;
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--text-muted);
  }

  .mobile-menu-label::before {
    content: "";
    width: 6px;
    height: 6px;
    background: var(--accent);
    transform: rotate(45deg);
  }

  .mobile-menu-label::after {
    content: "";
    flex: 1;
    height: 1px;
    background: var(--accent-border);
  }

  .mobile-menu-chips {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  .mobile-menu-chips a {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 44px;
    padding: 0 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg-card);
    color: var(--text-primary);
    font-size: 14px;
    font-weight: 500;
  }

  .mobile-menu-chips .ui-icon {
    width: 16px;
    height: 16px;
    color: var(--accent);
  }

  .mobile-menu-chips a:hover {
    border-color: var(--border-warm);
  }

  .site-header .mobile-menu-cta {
    width: 100%;
    min-height: 48px;
    margin-top: 14px;
    font-size: 15px;
    font-weight: 600;
  }
}

@layer responsive {
  /* The compact bar after scrolling, on screens with the full menu. */
  @media (min-width: 801px) {
    .site-header[data-compact] {
      height: 64px;
      box-shadow: 0 6px 18px rgb(0 0 0 / 35%);
    }

    .site-header[data-compact] .brand-logo {
      height: 36px;
    }
  }

  @media (min-width: 1101px) {
    .site-header[data-compact] .header-cta {
      display: inline-flex;
    }

    .site-header[data-compact] .header-cta + .account-link {
      border-color: var(--border-warm);
      background: transparent;
      color: var(--text-primary);
      font-weight: 500;
    }

    .site-header[data-compact] .header-cta + .account-link:hover {
      background: var(--bg-elevated);
    }

    /* Add to Discord takes the room of the language name, which German and
       French menus need. */
    .site-header[data-compact]:has(.header-cta) .language-toggle [data-language-current] {
      display: none;
    }
  }

  /* Below 1261px the compact bar also needs the room of the menu icons. */
  @media (min-width: 1101px) and (max-width: 1260px) {
    .site-header[data-compact]:has(.header-cta) .desktop-nav .nav-icon {
      display: none;
    }
  }

  /* Narrower desktops: tighter links, and the language picker keeps its globe
     but drops the name, which German and French menus need the room for. */
  @media (max-width: 1180px) {
    .site-header {
      gap: 16px;
    }

    .desktop-nav {
      gap: 2px;
      padding-left: 14px;
    }

    .desktop-nav > a,
    .features-toggle {
      padding-inline: 10px;
    }

    .language-toggle [data-language-current] {
      display: none;
    }
  }

  @media (max-width: 1100px) {
    .desktop-nav .nav-icon {
      display: none;
    }
  }

  /* Narrow desktops: the demo card makes room for the feature list. */
  @media (max-width: 1000px) {
    .features-menu-demo {
      display: none;
    }
  }

  /* iPad portrait: smaller links so the menu fits beside the account button. */
  @media (max-width: 960px) {
    .desktop-nav > a,
    .features-toggle {
      padding-inline: 8px;
      font-size: 14px;
    }
  }

  /* Down to the phone layout the header has no room for the Discord link; the
     footer and the phone menu keep it. */
  @media (min-width: 801px) and (max-width: 960px) {
    .header-socials {
      display: none;
    }
  }

  /* The narrowest desktops before the phone menu: the least spacing that still reads. */
  @media (min-width: 801px) and (max-width: 900px) {
    .site-header {
      gap: 12px;
    }

    .desktop-nav {
      padding-left: 8px;
    }

    .desktop-nav > a,
    .features-toggle {
      padding-inline: 6px;
    }

    .account-link {
      padding-inline: 12px;
    }
  }

  @media (max-width: 860px) {
    .brand-logo {
      height: 40px;
    }
  }

  @media (max-width: 800px) {
    .site-header {
      height: 76px;
      padding-inline: 20px;
    }

    .brand-logo {
      height: 40px;
    }

    .desktop-nav,
    .language-picker {
      display: none;
    }

    .menu-toggle {
      display: grid;
    }

    .header-actions {
      gap: 15px;
    }
  }

  @media (max-width: 520px) {
    .header-socials {
      display: none;
    }

    .brand-logo {
      height: 36px;
    }

    .account-link {
      padding-inline: 12px;
      font-size: 13px;
    }
  }

  /* Phones: one header layout on every page (64px bar, 44px logo, 40px
     controls on the logo's centre line), shared with the dashboard and the
     operator panel's top rows. */
  @media (max-width: 760px) {
    html:has(.site-header .desktop-nav) {
      scroll-padding-top: 80px;
    }

    .site-header {
      height: 64px;
      padding-inline: 16px;
    }

    .site-header .brand-logo {
      height: 36px;
    }

    .site-header .header-actions {
      gap: 10px;
    }

    .site-header :is(.account-link, .menu-toggle, .text-link, .social-link) {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      height: 40px;
      min-height: 40px;
      padding-block: 0;
      white-space: nowrap;
    }

    .site-header .menu-toggle {
      width: 40px;
    }

    .site-header .mobile-menu {
      inset-inline: 16px;
      max-height: calc(100dvh - 80px);
    }
  }

  /* The narrowest phones (320px): the smaller logo and tighter gaps keep the
     account and menu buttons on screen. */
  @media (max-width: 340px) {
    .site-header {
      gap: 12px;
    }

    .site-header .brand-logo {
      height: 30px;
    }

    .site-header .header-actions {
      gap: 8px;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .site-header,
    .brand-logo,
    .features-chevron {
      transition: none;
    }
  }
}
