/* Phones and small tablets.

   Desktop layouts keep their 1280px page (layout.css applies it from 1024px up); under that the
   page is fluid and these rules fold what doesn't fit. They live together here so the phone layout
   can be read in one place. Many override rules nested in layout.css under
   `body:has(:is(#main.books, #main.book))`, so they repeat that prefix to outrank them; this file
   loads after layout.css, books.css and pages.css (stylesheet_link_tag :all is alphabetical). */

/* The import sources: three across on a desktop. */
.import-cards {
  display: grid;
  gap: var(--block-space);
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

/* The book tree as a drawer: hidden wherever the tree is docked (book_tree.css) or absent. */
.sidebar-toggle,
.sidebar-backdrop {
  display: none;
}

@media (max-width: 70ch) {
  body:has(#sidebar .book-tree) {
    .sidebar-toggle {
      --hover-size: 0;

      align-items: center;
      background-color: #FFFFFF;
      block-size: 52px;
      border: 1px solid #E7E7E7;
      border-radius: 50%;
      box-shadow: 0 6px 18px rgba(0, 0, 0, 0.18);
      cursor: pointer;
      display: flex;
      inline-size: 52px;
      inset-block-end: calc(16px + env(safe-area-inset-bottom));
      inset-inline-start: 16px;
      justify-content: center;
      padding: 0;
      position: fixed;
      z-index: 30;

      img {
        block-size: 24px;
        inline-size: 24px;
      }
    }

    #sidebar {
      background-color: #F6F6F7;
      box-shadow: 0 0 32px rgba(0, 0, 0, 0.2);
      display: block;
      inline-size: min(85vw, 320px);
      inset-block: 0;
      overscroll-behavior: contain;
      padding-block-end: env(safe-area-inset-bottom);
      position: fixed;
      transform: translateX(-100%);
      transition: transform 0.2s ease-out;
      visibility: hidden;
      z-index: 40;
    }

    &.sidebar-open {
      overflow: hidden;

      #sidebar {
        transform: none;
        visibility: visible;
      }

      .sidebar-backdrop {
        background-color: rgba(0, 0, 0, 0.35);
        display: block;
        inset: 0;
        position: fixed;
        z-index: 35;
      }
    }

    /* The tree hides its title in the editor, where the docked tree sits under the book's name. */
    &:has(.leaf-edit-bar) .book-tree__title {
      display: block;
    }
  }

  @media (prefers-color-scheme: dark) {
    body:has(#sidebar .book-tree) {
      .sidebar-toggle {
        background-color: #2A2A2A;
        border-color: #3A3A3A;

        img { filter: invert(0.9); }
      }

      #sidebar { background-color: #1E1E1E; }
    }
  }
}

@media (max-width: 1023px) {
  body:has(:is(#main.books, #main.book)) {
    #main {
      padding-inline: 16px;
    }

    .content {
      padding-block: 32px;
      padding-inline: 20px;
    }

    .copyright {
      padding-inline: 16px;
    }

    /* The page's title and its buttons, and the search above a list. */
    .content__header {
      flex-wrap: wrap;
      gap: 12px;
    }

    .content__actions {
      flex-wrap: wrap;

      .btn {
        white-space: nowrap;
      }
    }

    .content__toolbar {
      flex-wrap: wrap;
      gap: 12px;
    }

    .content__search {
      flex: 1 1 100%;
      max-inline-size: none;
    }

    .pagination__size {
      white-space: nowrap;
    }

    /* A sticky footer of buttons (the book's settings): they wrap rather than squeeze. */
    #footer {
      block-size: auto;
      min-block-size: var(--nav-block-size);
      padding-block-end: env(safe-area-inset-bottom);
    }

    #footer > * {
      padding-block: 12px;
      padding-inline: 16px;
    }

    #footer > * > .flex {
      flex-wrap: wrap;
      justify-content: flex-end;
    }

    #footer .btn {
      white-space: nowrap;
    }
  }

  /* A book's tabs scroll sideways in one row; its buttons follow them. */
  .book__tabs-header > .flex {
    flex-wrap: wrap;
  }

  .book__tabs-header .tabs__nav {
    flex: 1 1 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;

    &::-webkit-scrollbar { display: none; }
  }

  .book__tabs-header .tabs__tab {
    flex-shrink: 0;
    white-space: nowrap;
  }

  /* Tables of rows (users) become a card per row. */
  body:has(:is(#main.books, #main.book)) .library-table {
    table,
    tbody,
    tr,
    td {
      display: block;
      inline-size: 100%;
    }

    table {
      border-spacing: 0;
      margin: 0;
    }

    thead {
      display: none;
    }

    tbody tr {
      border-block-end: 1px solid #E7E7E7;
      padding-block: 12px;
    }

    tbody tr::after {
      display: none;
    }

    th,
    td {
      padding: 6px 4px;
    }
  }

  /* The editor's one line: the book's name gives way to the ←, the document's to the save. */
  .leaf-edit-bar {
    gap: 0.75em;
    padding-inline: 12px;
  }

  .leaf-edit-bar .breadcrumbs {
    flex: 1;
    min-inline-size: 0;
  }

  .leaf-edit-bar__leaf {
    min-inline-size: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* The editor, then its pages and image layout below it, each scrolling on its own. */
  .leaf-edit-layout,
  .leaf-edit-layout:has(lexxy-editor) {
    block-size: auto;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto;
    min-block-size: 0;
    padding-block: 16px;
  }

  .leaf-edit-layout:has(lexxy-editor) lexxy-editor {
    block-size: 70dvh;
    flex: none;
  }

  lexxy-editor .lexxy-editor__content {
    padding: 1rem 1.1rem;
  }

  .leaf-edit-layout__panel,
  .leaf-edit-layout:has(lexxy-editor) .leaf-edit-layout__panel {
    block-size: auto;
    max-block-size: 70dvh;
    position: static;
  }

}

@media (max-width: 640px) {
  .import-cards {
    grid-template-columns: minmax(0, 1fr);
  }

  /* Two covers across. */
  body:has(:is(#main.books, #main.book)) .book-grid {
    gap: 28px 16px;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .leaf-edit-bar__book,
  .leaf-edit-bar__mode {
    display: none;
  }

  .leaf-edit-bar .mode-switch--back {
    padding-inline: 0.75em;
  }
}

/* Sign-in: the version line sits above the language picker rather than under it. */
@media (max-width: 640px) {
  body:has(#main.centered-panel) #footer > * {
    margin-block-end: calc(var(--block-space) + 64px);
  }

  /* The footer is laid over the page's bottom (position: absolute), so the card ends above it. */
  body:has(#main.centered-panel) #main {
    padding-block-end: calc(var(--block-space) * 10);
  }
}
