/* The app's places. On phones a bar along the bottom, within reach of a thumb, with the less used places in a menu
   that opens upwards from it. On wider screens a column down the left */
.app-nav {
  z-index: 1000;

  .brand {
    color: var(--text-color);
    font-weight: 700;
    white-space: nowrap;
    text-decoration: none;
  }
}

.nav-link {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 0;

  background: none;
  color: var(--text-color);
  font-size: var(--fs-body);
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;

  &[aria-current="page"] {
    color: var(--link-color);
  }

  @media (hover: hover) {
    &:hover {
      color: var(--link-hover-color);
      text-decoration: none;
    }
  }

  /* How many items wait there, at the end of the row. Its holder is only there to be updated */
  .nav-count {
    display: contents;
  }

  .count-badge {
    margin-left: auto;
  }
}

/* Offers the theme that isn't showing */
.theme-toggle {
  .to-dark {
    display: contents;
  }

  .to-light {
    display: none;
  }

  :root[data-theme="dark"] & {
    .to-dark {
      display: none;
    }

    .to-light {
      display: contents;
    }
  }
}

/* Who's signed in, set apart from the places above. Its menu opens upwards over them, as wide as it is */
.user-menu {
  margin-top: var(--space-2);
  padding-top: var(--space-2);
  border-top: 1px solid var(--border-color);

  .user-name {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .menu-items {
    right: 0;
    bottom: 100%;
    left: 0;
    min-width: 0;
    margin: 0 0 var(--space-1);
  }
}

@media (max-width: 799px) {
  :root:has(.app-nav) {
    --bottom-inset: calc(var(--tab-bar-height) + env(safe-area-inset-bottom));
  }

  /* Selecting puts the bar of actions where the nav was */
  :root:has(.selection-area.selecting) {
    --bottom-inset: env(safe-area-inset-bottom);
  }

  body:has(> .app-nav) {
    padding-bottom: calc(var(--tab-bar-height) + env(safe-area-inset-bottom));
  }

  .app-nav {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;

    display: flex;
    height: calc(var(--tab-bar-height) + env(safe-area-inset-bottom));
    padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);

    background-color: var(--bar-bg-color);
    backdrop-filter: var(--bar-backdrop-filter);
    border-top: 1px solid var(--border-color);

    /* Slid away, its links can't be reached */
    transition: translate 0.2s ease-in-out, visibility 0s;

    :root:has(.selection-area.selecting) & {
      translate: 0 100%;
      visibility: hidden;
      transition: translate 0.2s ease-in-out, visibility 0s linear 0.2s;
    }

    .brand {
      display: none;
    }

    > .nav-link, .nav-menu {
      flex: 1;
    }

    /* Tabs: an icon above a small label */
    > .nav-link, .nav-menu-toggle {
      display: grid;
      place-content: center;
      justify-items: center;
      gap: 0.125rem;
      width: 100%;
      height: 100%;

      color: var(--meta-text-color);
      font-size: 0.75rem;

      &[aria-current="page"] {
        color: var(--link-color);
      }
    }

    /* Over the icon's top right corner, growing to the right. It's placed from the icon's grid cell, whose end lines
       are given since an auto one would be the link's edge */
    > .nav-link {
      position: relative;

      .count-badge {
        position: absolute;
        grid-area: 1 / 1 / 2 / 2;
        top: calc(-1 * var(--space-1));
        left: calc(50% + var(--space-1));
      }
    }

    @media (prefers-reduced-motion: reduce) {
      &, :root:has(.selection-area.selecting) & {
        transition: none;
      }
    }
  }

  /* The links in the menu slide up from behind the bar, then fade in one after another from the bottom. The menu icon
     turns into a close icon while it's open */
  .nav-menu {
    .nav-menu-icons {
      display: grid;

      .icon {
        grid-area: 1 / 1;
        transition: opacity 0.2s ease-out, rotate 0.2s ease-out;
      }

      .icon-close {
        opacity: 0;
        rotate: -90deg;
      }
    }

    /* The menu tab shows the page is one of the menu's */
    &:has(.nav-menu-panel [aria-current="page"]) .nav-menu-toggle {
      color: var(--link-color);
    }

    /* Hides the panel while it waits behind the bar. The padding leaves room for its shadow */
    .nav-menu-items {
      position: absolute;
      right: 0;
      bottom: 100%;
      overflow: hidden;
      padding: var(--space-5) 0 0 var(--space-5);
      pointer-events: none;

      /* Hidden once it has slid away, so its links can't be reached */
      visibility: hidden;
      transition: visibility 0s linear 0.2s;
    }

    .nav-menu-panel {
      display: flex;
      flex-direction: column;
      min-width: 12rem;
      padding: var(--space-2) calc(var(--space-5) + env(safe-area-inset-right)) var(--space-2) var(--space-5);
      border-top-left-radius: var(--radius-m);

      background-color: var(--card-bg-color);
      box-shadow: -4px -4px 12px var(--drop-shadow-color);
      pointer-events: auto;

      transform: translateY(100%);
      transition: transform 0.2s ease-in-out;

      .nav-link {
        padding: var(--space-3) 0;
        font-size: 1.125rem;
      }

      > * {
        opacity: 0;
        transition: opacity 0s linear 0.2s;

        &:nth-last-child(2) { --i: 1; }
        &:nth-last-child(3) { --i: 2; }
        &:nth-last-child(4) { --i: 3; }
        &:nth-last-child(5) { --i: 4; }
        &:nth-last-child(6) { --i: 5; }
        &:nth-last-child(7) { --i: 6; }
      }
    }

    &.open {
      .icon-menu {
        opacity: 0;
        rotate: 90deg;
      }

      .icon-close {
        opacity: 1;
        rotate: 0deg;
      }

      .nav-menu-items {
        visibility: visible;
        transition: none;
      }

      .nav-menu-panel {
        transform: none;

        > * {
          opacity: 1;
          transition: opacity 0.2s ease-out calc(0.1s + var(--i, 0) * 0.05s);
        }
      }
    }

    @media (prefers-reduced-motion: reduce) {
      &, &.open {
        .icon, .nav-menu-items, .nav-menu-panel, .nav-menu-panel > * {
          transition: none;
        }
      }
    }
  }
}

@media (min-width: 800px) {
  body:has(> .app-nav) {
    display: grid;
    grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
    align-items: start;
  }

  .app-nav {
    position: sticky;
    top: 0;

    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    height: 100svh;
    overflow-y: auto;
    padding: var(--space-4) var(--space-3) calc(var(--space-4) + env(safe-area-inset-bottom)) calc(var(--space-3) + env(safe-area-inset-left));

    background-color: var(--bg-color);
    border-right: 1px solid var(--border-color);

    .brand {
      padding: var(--space-2) var(--space-3) var(--space-4);
    }

    /* Everything in the menu is shown in the column, so it needs no toggle */
    .nav-menu, .nav-menu-items, .nav-menu-panel {
      display: contents;
    }

    .nav-menu-toggle {
      display: none;
    }

    .nav-link {
      padding: var(--space-2) var(--space-3);
      border-radius: var(--radius-s);

      &[aria-current="page"] {
        background-color: var(--card-bg-color);
        font-weight: 600;
      }

      @media (hover: hover) {
        &:hover {
          background-color: var(--card-bg-color);
        }
      }
    }

    .settings-link {
      margin-top: auto;
    }
  }
}
