/*
 * The research notebook itself (notebook/shared/_panes), in the drawer and
 * on /notebook: one bar, then three panes — the notebooks, the pages in the
 * one open, the page in the editor — each scrolling on its own, so in the
 * drawer only the text scrolls, never the drawer.
 *
 *   .notebook-panes           the whole; fills its drawer or its page
 *   .notebook-panes__bar      where the reader is, the save state, and in the
 *                             drawer the sizes, Clip from page and close
 *   .notebook-status          the save state, `data-state` new, editing,
 *                             saving, uploading (a clip, with its
 *                             progress), saved (its tick draws in) or failed
 *   .notebook-sizes           Peek, Half, Full; aria-pressed is the size on
 *   .notebook-panes__rail     the notebooks
 *   .notebook-panes__list     the pages, each its title, what it says and
 *                             when; .notebook-sort is the order's pill and
 *                             panel (faktura's), a <details>
 *   .notebook-panes__note     the page open: its own row (.notebook-menu,
 *                             a <details>), then Lexxy, against the left
 *                             and no wider than 69rem;
 *                             __tabs, the pages as tabs, only at Peek
 *   .notebook-toast           the note after a delete, with its Undo
 *   .notebook-export-dialog   the export dialog and its form (.notebook-export)
 *
 * Lexxy's stylesheet is unlayered, so every rule of its wins over these
 * whatever the selector: the editor keeps Lexxy's `display: block`, and its
 * content is sized with properties Lexxy does not set — the editor's height
 * less Lexxy's own toolbar height. Lexxy folds the toolbar buttons that do
 * not fit its width into a menu of its own.
 */
@layer components {
  .notebook-panes {
    position: relative;
    display: flex;
    flex: 1;
    flex-direction: column;
    min-block-size: 0;
    color: var(--color-ink);
    font-family: var(--font-ui);
  }

  .notebook-panes__bar {
    display: flex;
    flex: none;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2xs) var(--space-sm);
    min-block-size: var(--target-min);
    padding: var(--space-2xs) var(--space-md) var(--space-2xs) var(--gutter);
    border-block-end: var(--rule-hairline);
  }

  .notebook-panes__crumb {
    color: var(--color-ink);
  }

  .notebook-panes__crumb-rule {
    color: var(--color-rule-strong);
  }

  .notebook-panes__subject {
    margin: 0;
  }

  .notebook-panes__title {
    overflow: hidden;
    max-inline-size: 24rem;
    color: var(--color-ink-soft);
    font-size: var(--text-ui-sm);
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .notebook-panes__tools {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    margin-inline-start: auto;
  }

  /* The save state: an icon drawn for the state, and its words. */
  .notebook-status {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2xs);
    color: var(--color-ink-subtle);
    font-size: var(--text-meta);

    &[data-state="failed"] {
      color: var(--color-warn);
    }

    &[data-state="uploading"] {
      color: var(--color-ink-muted);
      font-variant-numeric: tabular-nums;
    }
  }

  .notebook-status__icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    inline-size: var(--space-sm);
    block-size: var(--space-sm);

    [data-state="new"] > & {
      display: none;
    }

    [data-state="editing"] > &::before {
      inline-size: var(--space-2xs);
      block-size: var(--space-2xs);
      border-radius: var(--radius-pill);
      background-color: var(--color-ink-subtle);
      content: "";
      animation: notebook-pulse 1.2s ease-in-out infinite;
    }

    [data-state="saving"] > &::before,
    [data-state="uploading"] > &::before {
      inline-size: var(--space-sm);
      block-size: var(--space-sm);
      box-sizing: border-box;
      border: 2px solid var(--color-rule);
      border-block-start-color: var(--color-accent);
      border-radius: var(--radius-pill);
      content: "";
      animation: notebook-spin 600ms linear infinite;
    }

    [data-state="failed"] > &::before {
      inline-size: var(--space-2xs);
      block-size: var(--space-2xs);
      border-radius: var(--radius-pill);
      background-color: var(--color-warn);
      content: "";
    }
  }

  .notebook-status__check {
    display: none;
    fill: none;
    stroke: var(--color-accent);
    stroke-width: 2;

    [data-state="saved"] & {
      display: block;
    }

    & path {
      stroke-dasharray: 14;
      animation: notebook-draw var(--duration-drawer-in) var(--ease-out) both;
    }
  }

  .notebook-status__retry {
    display: none;
    padding: 0;
    border: 0;
    background: none;
    color: var(--color-link);
    font: inherit;
    text-decoration: underline;
    cursor: pointer;

    [data-state="failed"] > & {
      display: inline;
    }
  }

  @keyframes notebook-pulse {
    50% {
      opacity: 0.35;
    }
  }

  @keyframes notebook-spin {
    to {
      rotate: 1turn;
    }
  }

  @keyframes notebook-draw {
    from {
      stroke-dashoffset: 14;
    }

    to {
      stroke-dashoffset: 0;
    }
  }

  .notebook-sizes {
    display: inline-flex;
    border: var(--rule-hairline);
    border-color: var(--color-ink);
  }

  .notebook-sizes__button {
    min-block-size: var(--control-height-sm);
    padding: 0 var(--space-sm);
    border: 0;
    background: transparent;
    color: var(--color-ink-muted);
    font: var(--weight-medium) var(--text-ui-sm) var(--font-ui);
    cursor: pointer;

    &[aria-pressed="true"] {
      background-color: var(--color-ink);
      color: var(--color-ink-inverse);
    }
  }

  .notebook-panes__body {
    display: flex;
    flex: 1;
    min-block-size: 0;
  }

  /* The list's and the tabs' Turbo Frames, reloaded when a new page is
     first saved; they take no box of their own, so the panes lay out as if
     they were not there. */
  .notebook-panes__frame {
    display: contents;
  }

  /* The notebooks. */
  .notebook-panes__rail {
    display: flex;
    flex: none;
    flex-direction: column;
    inline-size: 14rem;
    overflow-y: auto;
    padding-block: var(--space-xs);
    border-inline-end: var(--rule-hairline);
    background-color: var(--color-surface);
  }

  /* The search over the pages, at the head of the notebooks. */
  .notebook-search {
    padding: var(--space-2xs) var(--space-md) var(--space-xs);
  }

  .notebook-panes__rail-label {
    padding: var(--space-sm) var(--space-md) var(--space-2xs);
  }

  .notebook-panes__book {
    display: flex;
    justify-content: space-between;
    gap: var(--space-xs);
    padding: var(--space-xs) var(--space-md);
    color: var(--color-ink);
    font-size: var(--text-ui-sm);
    text-decoration: none;

    &:hover {
      background-color: var(--color-row-hover);
    }

    &[aria-current="page"] {
      background-color: var(--color-surface-sunken);
      font-weight: var(--weight-semibold);
    }
  }

  .notebook-panes__count {
    color: var(--color-ink-subtle);
    font-size: var(--text-meta);
    font-weight: var(--weight-regular);
  }

  /* The pages. */
  .notebook-panes__list {
    display: flex;
    flex: none;
    flex-direction: column;
    inline-size: 20rem;
    min-block-size: 0;
    border-inline-end: var(--rule-hairline);
  }

  .notebook-panes__list-head {
    position: relative;
    display: flex;
    align-items: center;
    gap: var(--space-xs);
    padding: var(--space-xs) var(--space-sm);
  }

  .notebook-panes__new {
    margin-inline-start: auto;
    color: var(--color-link);
    font-size: var(--text-ui-sm);
    font-weight: var(--weight-semibold);
    text-decoration: none;
  }

  .notebook-panes__pages {
    flex: 1;
    min-block-size: 0;
    margin: 0;
    padding: 0;
    overflow-y: auto;
    list-style: none;
  }

  .notebook-panes__empty {
    margin: 0;
    padding: var(--space-sm);
    color: var(--color-ink-muted);
    font-size: var(--text-ui-sm);
  }

  .notebook-panes__item {
    position: relative;
    display: flex;
    border-block-start: var(--rule-hairline);

    &:has(.notebook-panes__page[aria-current="page"]) {
      background-color: var(--color-surface-sunken);
    }
  }

  .notebook-panes__page {
    display: flex;
    flex: 1;
    flex-direction: column;
    gap: var(--space-3xs);
    min-inline-size: 0;
    padding: var(--space-xs) var(--space-xs) var(--space-xs) var(--space-sm);
    color: var(--color-ink);
    text-decoration: none;

    &:hover {
      background-color: var(--color-row-hover);
    }
  }

  .notebook-panes__page-head {
    display: flex;
    align-items: baseline;
    gap: var(--space-2xs);
    min-inline-size: 0;
  }

  .notebook-panes__tag {
    flex: none;
    padding: 0 var(--space-3xs);
    background-color: var(--color-accent-tint);
    color: var(--color-accent-strong);
    font-size: var(--text-caps-xs);
    font-weight: var(--weight-semibold);
    letter-spacing: var(--tracking-caps-tight);
  }

  .notebook-panes__page-title {
    flex: 1;
    overflow: hidden;
    font-family: var(--font-prose);
    font-size: var(--text-ui);
    font-weight: var(--weight-semibold);
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .notebook-panes__when,
  .notebook-panes__clips {
    flex: none;
    color: var(--color-ink-subtle);
    font-size: var(--text-meta);
  }

  .notebook-panes__summary {
    display: -webkit-box;
    overflow: hidden;
    color: var(--color-ink-muted);
    font-size: var(--text-ui-sm);
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;

    &:empty {
      display: none;
    }

    [data-size="full"] & {
      -webkit-line-clamp: 3;
    }
  }

  .notebook-panes__bin-form {
    display: flex;
    align-items: flex-start;
    padding: var(--space-2xs) var(--space-2xs) 0 0;
  }

  .notebook-panes__bin {
    color: var(--color-loss);
    opacity: 0;

    .notebook-panes__item:hover &,
    &:focus-visible {
      opacity: 1;
    }

    @media (pointer: coarse) {
      opacity: 1;
    }
  }

  /* The order: the list's head names it, and opens a panel to change it. */
  .notebook-sort__pill {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2xs);
    padding: var(--space-3xs) var(--space-sm);
    border-radius: var(--radius-pill);
    background-color: var(--color-accent-tint);
    color: var(--color-accent-strong);
    font-size: var(--text-caps);
    font-weight: var(--weight-semibold);
    letter-spacing: var(--tracking-caps);
    text-transform: uppercase;
    list-style: none;
    cursor: pointer;

    &::-webkit-details-marker {
      display: none;
    }
  }

  .notebook-sort__panel {
    position: absolute;
    inset: calc(100% - var(--space-2xs)) auto auto var(--space-xs);
    z-index: 5;
    display: flex;
    flex-direction: column;
    gap: var(--space-xs);
    inline-size: 17rem;
    padding: var(--space-xs);
    border: var(--rule-hairline);
    background-color: var(--color-field);
    box-shadow: var(--shadow-overlay);
  }

  .notebook-sort__group,
  .notebook-sort__directions {
    display: flex;
    flex-direction: column;
    gap: var(--space-3xs);
    margin: 0;
    padding: 0;
    border: 0;

    & legend {
      padding: var(--space-3xs) var(--space-xs);
    }
  }

  .notebook-sort__option {
    display: flex;
    align-items: flex-start;
    gap: var(--space-xs);
    padding: var(--space-2xs) var(--space-xs);
    cursor: pointer;

    &:hover {
      background-color: var(--color-row-hover);
    }

    & input {
      margin: var(--space-3xs) 0 0;
      accent-color: var(--color-accent);
    }
  }

  .notebook-sort__option-text {
    display: flex;
    flex-direction: column;
  }

  .notebook-sort__option-label {
    font-size: var(--text-ui-sm);
    font-weight: var(--weight-semibold);
  }

  .notebook-sort__option-hint {
    color: var(--color-ink-muted);
    font-size: var(--text-meta);
  }

  /* Which way round: two halves of one switch over a well. The well is the
     labels' own border and ground, not the fieldset's, which a fieldset
     paints behind its legend too. */
  .notebook-sort__directions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0;
  }

  .notebook-sort__direction {
    padding: var(--space-2xs) var(--space-xs);
    border: var(--space-3xs) solid var(--color-surface-sunken);
    background-color: var(--color-surface-sunken);
    color: var(--color-ink-muted);
    font-size: var(--text-ui-sm);
    text-align: center;
    cursor: pointer;

    &:has(input:checked) {
      background-color: var(--color-field);
      background-clip: padding-box;
      color: var(--color-ink);
      font-weight: var(--weight-semibold);
    }

    & + & {
      border-inline-start-width: 0;
    }

    &:has(input:focus-visible) {
      outline: var(--focus-ring);
      outline-offset: var(--focus-offset);
    }
  }

  .notebook-sort__apply {
    align-self: flex-start;

    [data-controller~="auto-submit"] > & {
      display: none;
    }
  }

  /* The page open. */
  .notebook-panes__note {
    display: flex;
    flex: 1;
    flex-direction: column;
    min-inline-size: 0;
    background-color: var(--color-field);
  }

  .notebook-panes__tabs {
    display: none;
    flex-wrap: wrap;
    gap: var(--space-3xs);
    padding: var(--space-2xs) var(--gutter) 0;
    border-block-end: var(--rule-hairline);
  }

  .notebook-panes__tab {
    overflow: hidden;
    max-inline-size: 14rem;
    padding: var(--space-2xs) var(--space-sm);
    color: var(--color-ink-muted);
    font-size: var(--text-ui-sm);
    text-decoration: none;
    text-overflow: ellipsis;
    white-space: nowrap;

    &[aria-current="page"] {
      background-color: var(--color-surface-sunken);
      color: var(--color-ink);
      font-weight: var(--weight-semibold);
    }
  }

  .notebook-panes__tab--new {
    color: var(--color-link);
    font-weight: var(--weight-semibold);
  }

  /* At Peek the drawer shows the note alone, its pages as tabs. */
  [data-size="peek"] {
    & .notebook-panes__rail,
    & .notebook-panes__list {
      display: none;
    }

    & .notebook-panes__tabs {
      display: flex;
    }
  }

  /* The page's own row, as wide as the editor under it, so its menu sits
     at the editor's corner rather than at the far edge of a wide screen. */
  .notebook-panes__note-head {
    display: flex;
    flex: none;
    align-items: center;
    gap: var(--space-sm);
    box-sizing: content-box;
    max-inline-size: var(--notebook-editor-width);
    padding: var(--space-2xs) var(--space-md) 0;
  }

  .notebook-panes__edited {
    color: var(--color-ink-subtle);
    font-size: var(--text-meta);
  }

  .notebook-panes__company {
    color: var(--color-link);
    font-size: var(--text-ui-sm);
  }

  .notebook-menu {
    position: relative;
    margin-inline-start: auto;
  }

  .notebook-menu__button {
    list-style: none;

    &::-webkit-details-marker {
      display: none;
    }
  }

  .notebook-menu__panel {
    position: absolute;
    inset: 100% 0 auto auto;
    z-index: 5;
    display: flex;
    flex-direction: column;
    inline-size: 14rem;
    padding: var(--space-2xs);
    border: var(--rule-hairline);
    background-color: var(--color-field);
    box-shadow: var(--shadow-overlay);
  }

  .notebook-menu__form {
    display: flex;
    margin-block-start: var(--space-2xs);
    padding-block-start: var(--space-2xs);
    border-block-start: var(--rule-hairline);
  }

  .notebook-menu__item {
    flex: 1;
    padding: var(--space-xs) var(--space-sm);
    border: 0;
    background: none;
    color: var(--color-ink);
    font: var(--weight-regular) var(--text-ui-sm) var(--font-ui);
    text-align: start;
    text-decoration: none;
    cursor: pointer;

    &:hover {
      background-color: var(--color-row-hover);
    }
  }

  .notebook-menu__item--danger {
    color: var(--color-loss);
  }

  /* The editor: Lexxy, filling what is left, held to the left beside the
     page list and no wider than 69rem on a large screen. */
  .notebook-editor {
    display: flex;
    flex: 1;
    flex-direction: column;
    gap: var(--space-xs);
    min-block-size: 0;
    padding: var(--space-xs) var(--space-md) var(--space-sm);
  }

  .notebook-editor__body {
    flex: 1;
    min-block-size: 0;
    inline-size: 100%;
    max-inline-size: var(--notebook-editor-width);
    font-family: var(--font-prose);
    font-size: var(--text-prose-sm);

    & .lexxy-editor__content {
      box-sizing: border-box;
      block-size: calc(100% - var(--lexxy-toolbar-height));
      overflow-y: auto;
      overscroll-behavior: contain;
    }
  }

  .notebook-editor__save {
    align-self: flex-start;
  }

  /* After a delete: what went, and the Undo. */
  .notebook-toast {
    position: absolute;
    inset: auto auto var(--space-md) 50%;
    z-index: 6;
    display: flex;
    align-items: center;
    gap: var(--space-md);
    padding: var(--space-xs) var(--space-sm) var(--space-xs) var(--space-md);
    background-color: var(--color-ink);
    color: var(--color-ink-inverse);
    font-size: var(--text-ui-sm);
    box-shadow: var(--shadow-overlay);
    translate: -50% 0;
    animation: notebook-rise var(--duration-drawer-in) var(--ease-out);
  }

  .notebook-toast__form {
    display: flex;
  }

  .notebook-toast__undo {
    padding: var(--space-2xs);
    border: 0;
    background: none;
    color: var(--color-accent-tint);
    font: var(--weight-semibold) var(--text-caps) var(--font-ui);
    letter-spacing: var(--tracking-caps);
    text-transform: uppercase;
    cursor: pointer;
  }

  @keyframes notebook-rise {
    from {
      opacity: 0;
      translate: -50% var(--space-sm);
    }
  }

  /* Export. */
  .notebook-export-dialog {
    inline-size: min(44rem, calc(100vw - 2 * var(--gutter)));
    padding: 0;
    border: 0;
    border-block-start: var(--rule-heavy);
    background-color: var(--color-surface-raised);
    color: var(--color-ink);
    box-shadow: var(--shadow-overlay);

    &::backdrop {
      background-color: var(--color-backdrop);
    }
  }

  .notebook-export-dialog__head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-sm) var(--space-md) 0;

    & .heading {
      margin: 0;
    }
  }

  .notebook-export {
    display: flex;
    flex-direction: column;
    gap: var(--space-md);
    padding: var(--space-sm) var(--space-md) var(--space-md);
  }

  .notebook-export__choices {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: var(--space-xs);
    margin: 0;
    padding: 0;
    border: 0;

    & legend {
      padding-block-end: var(--space-xs);
    }

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

  .notebook-export__choice {
    display: flex;
    align-items: center;
    gap: var(--space-xs);
    padding: var(--space-xs) var(--space-sm);
    border: var(--rule-hairline);
    background-color: var(--color-field);
    font-size: var(--text-ui-sm);
    cursor: pointer;

    & input {
      margin: 0;
      accent-color: var(--color-accent);
    }

    &:has(input:checked) {
      border-color: var(--color-accent);
      background-color: var(--color-accent-tint-2);
    }
  }

  .notebook-export__choice--format {
    flex-direction: column;
    align-items: stretch;
  }

  .notebook-export__format {
    display: flex;
    align-items: center;
    gap: var(--space-xs);
  }

  .notebook-export__format-name {
    font-size: var(--text-ui);
    font-weight: var(--weight-semibold);
  }

  .notebook-export__format-ext {
    margin-inline-start: auto;
    color: var(--color-ink-subtle);
    font-size: var(--text-meta);
  }

  .notebook-export__format-hint,
  .notebook-export__note {
    margin: 0;
    color: var(--color-ink-muted);
    font-size: var(--text-meta);
  }

  .notebook-export__actions {
    display: flex;
    justify-content: flex-end;
    gap: var(--space-xs);
  }
}
