/* Book pages with the tree: the sidebar is a docked column, not a drawer. */
/* layout.css positions #sidebar on #main.book pages with `body { &:has(#main.book) { #sidebar
   { inset-block-start: …; inset-block-end: …; } } }`, and it loads after this file
   (stylesheet_link_tag :all is alphabetical). The extra :has(#main.book) here outranks it. */
/* Below 70ch the sidebar stays hidden (layout.css :where(#sidebar)). */
@media (min-width: 70ch) {
  body:has(#main.book):has(#sidebar .book-tree),
  body:has(#sidebar .book-tree) {
    grid-template-columns: 240px minmax(0, 1fr);
    grid-template-areas:
      "header header"
      "toolbar toolbar"
      "sidebar main"
      "footer footer";

    #sidebar {
      align-self: start;
      background-color: #F6F6F7;
      block-size: calc(100dvh - var(--book-tree-top, 80px));
      border-inline-end: 1px solid #E7E7E7;
      display: block;
      grid-area: sidebar;
      inline-size: auto;
      inset: auto;
      inset-block-end: auto;
      inset-block-start: var(--book-tree-top, 80px);
      overflow: auto;
      position: sticky;
      transform: none;
    }

    /* 53px: the leaf sub-navbar's height (breadcrumbs only, since its search moved into the tree). */
    &:has(.leaf-sub-navbar) { --book-tree-top: calc(var(--nav-block-size, 80px) + 53px); }
    /* The editor has no navbar, just its one line; the book's name is on that line's button. */
    &:has(.leaf-edit-bar) { --book-tree-top: 56px; }
    &:has(.leaf-edit-bar) .book-tree__title { display: none; }
    /* The sticky footer would cover the sidebar's last rows. */
    &:has(#footer > *) #sidebar { block-size: calc(100dvh - var(--book-tree-top, 80px) - var(--nav-block-size, 80px)); }
    #main { min-inline-size: 0; }
    /* The sticky sidebar stays docked to the bottom of the page, so the business-info footer
       goes in the column beside it, on its own row after #footer, not under it. */
    .business-info { grid-column: 2 / -1; grid-row: -1; }
  }
}

.book-tree {
  /* base.css draws a box-shadow on hovered links and buttons; not in the tree. */
  --hover-size: 0;
  display: flex;
  flex-direction: column;
  font-size: 0.95rem;
  gap: 4px;
  padding: 24px 20px 48px;
}

.book-tree__title {
  color: var(--color-ink);
  font-size: 1.1rem;
  font-weight: 700;
  padding-block-end: 12px;
  text-decoration: none;
}

.book-tree__search { margin-block-end: 8px; }

.book-tree__section {
  border-block-start: 1px solid #E2E2E3;
  padding-block: 12px;
}

.book-tree__row {
  align-items: center;
  display: flex;
  gap: 4px;
  min-block-size: 32px;
  position: relative;
}

.book-tree__toggle {
  align-items: center;
  background: none;
  border: 0;
  color: inherit;
  cursor: pointer;
  display: flex;
  flex: 1;
  font: inherit;
  justify-content: space-between;
  padding: 0;
  text-align: start;
}

.book-tree__toggle--part { flex: 0 0 auto; }

/* A type group (장 ▸ 1장, 2장…): the type and its count, the chevron at the end. */
.book-tree__group-label { color: var(--color-subtle-dark); padding-block: 4px; }

.book-tree__count {
  color: var(--color-subtle-dark);
  font-size: 0.8em;
  margin-inline-start: 4px;
  opacity: 0.7;
}

/* A group row ends in the same slot as a section row, so its chevron lines up. */
.book-tree__row--group > .book-tree__add-slot { flex: 0 0 24px; }

/* A document's type beside its title, readable but lighter than the title. */
.book-tree__badge {
  background-color: #E2E2E5;
  border-radius: 4px;
  color: var(--color-ink);
  flex-shrink: 0;
  font-size: 0.75rem;
  font-weight: 500;
  line-height: 1.5;
  padding: 0 5px;
  white-space: nowrap;
}

.book-tree__section-label { font-weight: 700; }

.book-tree__chevron::before {
  content: "⌄";
  color: var(--color-subtle-dark);
  display: inline-block;
  transition: transform 150ms ease;
}

.book-tree__toggle[aria-expanded="false"] .book-tree__chevron::before { transform: rotate(-90deg); }

.book-tree__row:has(.book-tree__toggle[aria-expanded="false"]) + .book-tree__items { display: none; }
.book-tree__row:has(.book-tree__toggle[aria-expanded="false"]) ~ .button_to:has(.book-tree__append) { display: none; }

/* 본문's last row: add the next chapter after the ones above. */
.book-tree__append {
  background: none;
  border: 0;
  border-radius: 4px;
  color: var(--color-subtle-dark);
  cursor: pointer;
  font: inherit;
  inline-size: 100%;
  margin-block-start: 4px;
  padding: 4px 0;
  text-align: start;

  &:hover { color: #FF5205; }
}

.book-tree__items {
  display: flex;
  flex-direction: column;
  list-style: none;
  margin: 0;
  padding: 0;
}

.book-tree__items--nested {
  border-inline-start: 1px solid #E2E2E3;
  margin-inline-start: 2px;
  padding-inline-start: 14px;
}

.book-tree__link {
  color: var(--color-ink);
  flex: 1;
  min-inline-size: 0;
  overflow: hidden;
  padding-block: 4px;
  text-decoration: none;
  text-overflow: ellipsis;
  white-space: nowrap;

  &:hover { text-decoration: underline; }
  &.is-current { color: #FF5205; font-weight: 600; }
}

/* A document's link holds its type tag too (tree_leaf_link): the title takes the room left and
   shortens, the tag stays at the end; only the title underlines. */
.book-tree__link--leaf {
  align-items: center;
  display: flex;
  gap: 4px;

  &:hover { text-decoration: none; }
  &:hover .book-tree__link-text { text-decoration: underline; }
}

.book-tree__link-text {
  flex: 1;
  min-inline-size: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Where the reader left off (reading-progress controller). */
.book-tree__item.is-last-read > .book-tree__row::before {
  background: var(--color-marker);
  block-size: 6px;
  border-radius: 50%;
  content: "";
  flex-shrink: 0;
  inline-size: 6px;
}

.book-tree__icon-btn {
  border-radius: 4px;
  color: var(--color-subtle-dark);
  cursor: pointer;
  list-style: none;
  padding: 0 6px;

  &::-webkit-details-marker { display: none; }
  &:hover { background-color: #E7E7E9; }
}

/* Shown on row hover where there is hover; always shown on touch screens. */
@media (hover: hover) {
  .book-tree__more { opacity: 0; }
  .book-tree__row:is(:hover, :focus-within) .book-tree__more,
  .book-tree__more[open] { opacity: 1; }
}

/* Every section row ends in the same slot, with or without a + menu, so the
   chevrons line up in one column. */
.book-tree__row--section > :is(.book-tree__add, .book-tree__add-slot) {
  flex: 0 0 24px;
  text-align: center;
}

.book-tree__row--section > .book-tree__add > summary { padding-inline: 0; }

.book-tree__add, .book-tree__more { position: relative; }

.book-tree__menu {
  background: #FFFFFF;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.16);
  display: flex;
  flex-direction: column;
  inset-block-start: 100%;
  inset-inline-end: 0;
  min-inline-size: 140px;
  padding: 6px;
  position: absolute;
  z-index: 5;

  :is(button, input[type=submit]) {
    background: none;
    border: 0;
    border-radius: 4px;
    cursor: pointer;
    font: inherit;
    padding: 6px 10px;
    text-align: start;
    white-space: nowrap;

    &:hover { background-color: #F2EEE9; }
  }

  form { display: contents; }
}

.book-tree__rename {
  flex: 1;
  font: inherit;
  min-inline-size: 0;
}

.book-tree__item.is-dragging { opacity: 0.4; }
.book-tree__item.drop-before > .book-tree__row { box-shadow: inset 0 2px 0 #FF5205; }
.book-tree__item.drop-after > .book-tree__row { box-shadow: inset 0 -2px 0 #FF5205; }

@media (prefers-color-scheme: dark) {
  body:has(#sidebar .book-tree) #sidebar { background-color: #1E1E1E; border-color: #3A3A3A; }
  .book-tree__section { border-color: #3A3A3A; }
  .book-tree__items--nested { border-color: #3A3A3A; }
  .book-tree__badge { background-color: #2F2F2F; }
  .book-tree__icon-btn:hover { background-color: #3A3A3A; }
  .book-tree__menu {
    background: #2A2A2A;
    color: var(--color-ink);

    :is(button, input[type=submit]) { color: inherit; }
    :is(button, input[type=submit]):hover { background-color: #3A3A3A; }
  }
}

/* Book overview (books/show) */
.book-overview {
  --book-overview-border: #E3DED8;
  --book-overview-cover-bg: #F2EEE9;
  display: flex;
  flex-direction: column;
  gap: 24px;
  padding-block: 8px 24px;
}

.book-overview__card {
  align-items: flex-start;
  display: flex;
  gap: 28px;
}

.book-overview__cover {
  aspect-ratio: 210 / 297;
  background-color: var(--book-overview-cover-bg);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.18);
  display: block;
  flex: 0 0 160px;
  inline-size: 160px;
  overflow: hidden;

  img { block-size: 100%; inline-size: 100%; object-fit: contain; }
  .book-grid__cover-empty { align-content: center; background-color: #1E1E1E; block-size: 100%; color: var(--color-always-white); display: grid; font-size: 0.75rem; font-weight: 700; justify-items: center; text-align: center; word-break: keep-all; }
}

.book-overview__meta {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-inline-size: 0;
  text-align: start;

  p { margin: 0; }
  a { color: inherit; font-size: 0.85rem; opacity: 0.7; text-underline-offset: 3px; }
  a:hover { color: #FF5205; opacity: 1; }
}

.book-overview__title { font-size: 1.4rem; line-height: 1.25; margin: 0; }
.book-overview__subtitle { font-size: 1.05rem; }
.book-overview__author { font-weight: 600; }
.book-overview__facts { font-size: 0.85rem; margin-block-end: 6px; }

.book-overview__blank {
  align-items: center;
  border: 1px dashed var(--book-overview-border);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 24px;

  p { margin: 0; }
}

.book-overview__print {
  border: 1px solid var(--book-overview-border);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px 20px;
  text-align: start;

  h2 { font-size: 1rem; margin: 0; }
  p { margin: 0; }
  > .flex { flex-wrap: wrap; } /* its buttons wrap to a second row rather than squeeze */
  .btn { --btn-icon-size: 16px; block-size: 2.75rem; border-radius: 999px; font-size: 0.9em; white-space: nowrap; }
  p a { color: inherit; opacity: 0.7; text-underline-offset: 3px; }
  p a:hover { color: #FF5205; opacity: 1; }
}

@media (max-width: 640px) {
  .book-overview__card { flex-direction: column; }
}

@media (prefers-color-scheme: dark) {
  .book-overview { --book-overview-border: #3A3A3A; --book-overview-cover-bg: #2A2A2A; }
}
