/*
 * The dashboard's own styles, after the design system's (crates/ui/assets/css/app.css, the
 * pikumo-ui crate: tokens, reset, base, components, utilities): the page shell (the frame, the
 * sidebar, the canvas, the auth screens) and the components that belong to the product (theme
 * option previews, opening hours, the section list, the draft preview, the forms of stored
 * documents, the media library). The page editor's look is css/editor.css.
 * Reference: docs/design-system.md.
 *
 * Same cascade layers as app.css, declared again in the same order (the first declaration wins,
 * so this changes nothing): a rule here sits in its layer after the design system's.
 * Every value is a token: var(--pk-…), never a raw colour or size.
 */

@layer reset, tokens, base, components, shell, utilities;

/* -------------------------------------------------------------- Components */

@layer components {
  /* Theme option previews: what the visitor will see, drawn with CSS only. */
  .swatches {
    display: flex;
    gap: var(--pk-space-100);
  }

  .swatch {
    inline-size: var(--pk-size-swatch);
    block-size: var(--pk-size-swatch);
    border-radius: var(--pk-radius-full);
    box-shadow: inset 0 0 0 var(--pk-border-width) rgb(17 17 21 / 12%);
  }

  /* A palette's colours, drawn by the server in SVG from the theme's tokens
     (presentation attributes: no inline style). The stroke is its hairline. */
  .swatch--drawn {
    box-shadow: none;
    color: var(--pk-color-text-default);
  }

  .swatch--custom-bg { background: var(--pk-preview-custom-bg); }
  .swatch--custom-accent { background: var(--pk-preview-custom-accent); }
  .swatch--custom-ink { background: var(--pk-preview-custom-ink); }

  .font-sample {
    font-size: var(--pk-space-600);
    line-height: 1;
    color: var(--pk-color-text-default);
  }

  .font-sample--serif { font-family: var(--pk-font-serif); }
  .font-sample--sans { font-family: var(--pk-font-sans); }
  .font-sample--rounded { font-family: var(--pk-font-rounded); }

  /* Density: three bars whose gaps grow. Corners: a square whose corner rounds. */
  .density-sample {
    display: flex;
    flex-direction: column;
    gap: var(--pk-space-100);
    inline-size: var(--pk-space-1000);

    & > span {
      block-size: var(--pk-space-100);
      border-radius: var(--pk-radius-full);
      background: var(--pk-color-icon-secondary);
    }
  }

  .density-sample--compact { gap: var(--pk-space-050); }
  .density-sample--spacious { gap: var(--pk-space-200); }

  .corner-sample {
    inline-size: var(--pk-space-800);
    block-size: var(--pk-space-800);
    border: var(--pk-size-icon-stroke) solid var(--pk-color-icon-secondary);
    border-inline-start: 0;
    border-block-end: 0;
  }

  .corner-sample--square { border-start-end-radius: 0; }
  .corner-sample--soft { border-start-end-radius: var(--pk-radius-300); }
  .corner-sample--round { border-start-end-radius: var(--pk-radius-full); }

  /* Opening hours: one row per day; the times hide while the day is closed. */
  .hours {
    display: flex;
    flex-direction: column;
    gap: var(--pk-space-200);
  }

  /* Phones: the day and its "Closed" box on a line, the times under them.
     From 48rem: day, times, box on one line. */
  .hours__day {
    display: grid;
    grid-template-areas: "name closed" "times times";
    grid-template-columns: 1fr auto;
    align-items: center;
    gap: var(--pk-space-200) var(--pk-space-300);
    padding-block: var(--pk-space-200);
    border-block-start: var(--pk-border-width) solid var(--pk-color-border-default);

    &:first-child {
      border-block-start: 0;
    }

    & > .choice {
      grid-area: closed;
    }

    @media (width >= 48rem) {
      grid-template-areas: "name times closed";
      grid-template-columns: var(--pk-size-hours-day) 1fr auto;
      padding-block: var(--pk-space-100);
      border: 0;
    }
  }

  .hours__name {
    grid-area: name;
    font: var(--pk-text-body-md-medium);
  }

  .hours__times {
    display: flex;
    grid-area: times;
    align-items: center;
    gap: var(--pk-space-150);

    & > .field {
      flex: 1 1 0;
      min-inline-size: 0;
    }

    & .input {
      min-inline-size: 0;
      font-variant-numeric: tabular-nums;
    }
  }

  /* ------------------------------------------------------ Section list */

  /* The ordered sections of a page. With the script, the grip drags (pointer, touch) or moves
     with the arrow keys; without it, the up and down buttons post and redirect. */
  .section-list {
    display: flex;
    flex-direction: column;
  }

  .section-row {
    position: relative;
    display: flex;
    align-items: center;
    gap: var(--pk-space-200);
    min-block-size: calc(var(--pk-size-row) + var(--pk-space-200));
    padding: var(--pk-space-150) var(--pk-space-300) var(--pk-space-150) var(--pk-space-100);
    border-block-start: var(--pk-border-width) solid var(--pk-color-border-default);
    background: var(--pk-color-bg-surface);
    transition:
      box-shadow var(--pk-duration-fast) var(--pk-ease),
      opacity var(--pk-duration-fast) var(--pk-ease);

    &:first-child {
      border-block-start-color: transparent;
    }

    &[data-hidden] .list-row__title {
      color: var(--pk-color-text-tertiary);
    }

    &[data-dragging] {
      z-index: var(--pk-z-drag);
      border-radius: var(--pk-radius-200);
      box-shadow: 0 0 0 var(--pk-size-focus-ring) var(--pk-color-border-focus), var(--pk-shadow-lift);
      transition: none;
      cursor: grabbing;
    }

    &:target {
      box-shadow: inset 0 0 0 var(--pk-size-focus-ring) var(--pk-color-border-focus);
    }

    @media (width >= 48rem) {
      padding-inline-end: var(--pk-space-400);
    }
  }

  .section-list[data-reordering] .section-row:not([data-dragging]) {
    transition: none;
  }

  .section-row__handle {
    display: grid;
    flex: none;
    place-items: center;
    inline-size: var(--pk-size-control-md);
    block-size: var(--pk-size-control-lg);
    border-radius: var(--pk-radius-200);
    color: var(--pk-color-icon-secondary);
    cursor: grab;
    /* The page must not scroll when a drag starts on the grip (touch). */
    touch-action: none;

    &:hover,
    &[aria-pressed="true"] {
      color: var(--pk-color-icon-default);
      background: var(--pk-color-bg-fill);
    }
  }

  .section-row__main {
    display: flex;
    flex: 1 1 0;
    align-items: center;
    gap: var(--pk-space-300);
    min-inline-size: 0;
    padding-block: var(--pk-space-100);
    border-radius: var(--pk-radius-200);
  }

  .section-row__main:is(a):hover .list-row__title {
    text-decoration: underline;
  }

  .section-row__move {
    display: flex;
    flex-direction: column;
    gap: var(--pk-space-050);

    & .icon-button {
      inline-size: var(--pk-size-control-md);
      block-size: calc(var(--pk-size-control-md) - var(--pk-space-100));
    }
  }

  .section-row__actions {
    display: flex;
    flex: none;
    align-items: center;
    gap: var(--pk-space-100);
  }

  /* The catalogue of section types (add-section dialog and page). */
  .catalogue__icon {
    color: var(--pk-color-icon-brand);
  }

  /* ----------------------------------------------------------- Preview */

  /* The draft in the renderer: a tinted panel, a toolbar, the page in a phone frame. */
  .preview-panel {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border-radius: var(--pk-radius-300);
    background: var(--pk-color-bg-surface-tinted);
    box-shadow: var(--pk-shadow-100);
  }

  .preview-panel__toolbar {
    display: flex;
    align-items: center;
    gap: var(--pk-space-200);
    min-block-size: var(--pk-size-preview-toolbar);
    padding-inline: var(--pk-space-400) var(--pk-space-100);
    border-block-end: var(--pk-border-width) solid var(--pk-color-border-default);
  }

  .preview-panel__title {
    flex: 1 1 0;
    min-inline-size: 0;
    font: var(--pk-text-body-sm-medium);
    color: var(--pk-color-text-secondary);
  }

  .preview-panel__stage {
    display: flex;
    justify-content: center;
    padding: var(--pk-space-500) var(--pk-space-400);
  }

  .preview-phone {
    inline-size: 100%;
    max-inline-size: calc(var(--pk-size-preview) + 2 * var(--pk-size-preview-bezel));
    padding: var(--pk-size-preview-bezel);
    border-radius: var(--pk-radius-600);
    background: var(--pk-gray-950);
    box-shadow: var(--pk-shadow-showcase);

    :root[data-theme="dark"] & {
      background: var(--pk-gray-800);
    }

    @media (prefers-color-scheme: dark) {
      :root:not([data-theme="light"]) & {
        background: var(--pk-gray-800);
      }
    }
  }

  .preview-frame {
    display: block;
    inline-size: 100%;
    block-size: min(72vh, 52rem);
    border: 0;
    border-radius: calc(var(--pk-radius-600) - var(--pk-size-preview-bezel));
    background: var(--pk-color-bg-surface);
  }

  /* ------------------------------------------- Fields on the dashboard's pages */

  /* A counter without a hint sits on the label's line, at the end, rather than on a line of
     its own under the field (the dashboard's pages; the page editor keeps its layout). */
  .app--frame .field:has(> .field__label:not(.visually-hidden) + :is(.input, .input-group)) {
    position: relative;

    & > .field__footer:not(:has(.field__hint)) {
      position: absolute;
      inset-block-start: 0;
      inset-inline-end: 0;
      align-items: center;
      block-size: 1.25rem; /* 20: the label's line */
    }

    &:has(> .field__footer:not(:has(.field__hint)) > .field__counter) > .field__label {
      padding-inline-end: var(--pk-space-1600);
    }
  }

  /* The same for a translated field: the shown language's counter on the legend's line. The
     fieldset's content box starts under its legend (20px and its 8px margin). */
  .app--frame .field-group--i18n {
    position: relative;

    & > .lang-pane > .field > .field__footer:not(:has(.field__hint)) {
      position: absolute;
      inset-block-start: -1.75rem; /* 28: the legend's line and its margin */
      inset-inline-end: 0;
      align-items: center;
      block-size: 1.25rem; /* 20: the legend's line */
    }

    & > .field-group__label {
      padding-inline-end: var(--pk-space-1600);
    }
  }

  /* ------------------------------------------------- Forms of documents */

  /* A row of a list field (buttons, cards, dishes): its title, then its fields. */
  .repeat-row__body {
    display: flex;
    flex-direction: column;
    gap: var(--pk-space-300);
    min-inline-size: 0;
  }

  .repeat-row__title {
    font: var(--pk-text-body-sm-medium);
    color: var(--pk-color-text-secondary);
    text-transform: capitalize;
  }

  .repeat-row:has(> .repeat-row__body) {
    align-items: start;
  }

  /* A row of a long list on the Info pages (a dish, a category, a special day): folded into
     one line, its name and price, until opened. */
  .repeat-row:has(> .repeat-row__fold) {
    align-items: center;
    padding-block: var(--pk-space-150);

    &:has(> .repeat-row__fold[open]) {
      align-items: start;
      padding-block: var(--pk-space-300);
    }
  }

  .repeat-row__summary {
    display: flex;
    align-items: center;
    gap: var(--pk-space-200);
    min-block-size: var(--pk-size-control-md);
    min-inline-size: 0;
    list-style: none;
    cursor: pointer;

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

    & > .icon {
      flex: none;
      color: var(--pk-color-icon-secondary);
      transition: rotate var(--pk-duration-fast) var(--pk-ease);
    }

    & .repeat-row__title {
      flex: none;
    }
  }

  .repeat-row__preview {
    display: flex;
    gap: var(--pk-space-200);
    min-inline-size: 0;
    overflow: hidden;
    font: var(--pk-text-body-md-medium);
    white-space: nowrap;
    text-overflow: ellipsis;

    & > span:not([hidden]) + span:not([hidden])::before {
      content: "·";
      margin-inline-end: var(--pk-space-200);
      color: var(--pk-color-text-tertiary);
    }

    & > .repeat-row__quiet {
      font: var(--pk-text-body-md);
      color: var(--pk-color-text-secondary);
      font-variant-numeric: tabular-nums;
    }
  }

  .repeat-row__fold[open] > .repeat-row__summary > .icon {
    rotate: 90deg;
  }

  /* On phones a folded row shows what it holds, not "Slot 1". */
  @media (width < 30rem) {
    .repeat-row__summary:has(> .repeat-row__preview > span) > .repeat-row__title {
      display: none;
    }
  }

  /* The blank row that adds one without the script: "Add: dish", folded. */
  .repeat-row__summary--add {
    color: var(--pk-color-text-secondary);
  }

  /* A closed <details> keeps its hidden content as a flex item: block, so no gap is left. */
  .repeat-row__fold {
    display: block;
  }

  .repeat-row__fold[open] > .form {
    margin-block-start: var(--pk-space-300);
  }

  /* A group's short fields two to a line (postal code and city, latitude and longitude):
     `.form--pairs`, each field in a `.form__cell`, the long ones `--wide`. */
  .form--pairs {
    @media (width >= 30rem) {
      display: grid;
      grid-template-columns: 1fr 1fr;
      gap: var(--pk-space-500) var(--pk-space-400);

      & > .form__cell--wide {
        grid-column: 1 / -1;
      }
    }
  }

  /* Two times in a row (opens, closes) sit side by side from 30rem. */
  .repeat-row .form:has(> .field > input[type="time"]) {
    @media (width >= 30rem) {
      display: grid;
      grid-template-columns: 1fr 1fr;
      gap: var(--pk-space-400);

      & > :not(.field:has(> input[type="time"])) {
        grid-column: 1 / -1;
      }
    }
  }

  /* Checkboxes in a row: days of the week, allergens, languages. */
  .choice-row {
    display: flex;
    flex-wrap: wrap;
    gap: var(--pk-space-200) var(--pk-space-400);
  }

  .choice--compact {
    min-block-size: auto;
  }

  /* Errors of a form that no field shows. */
  .error-list {
    margin: 0;
    padding-inline-start: var(--pk-space-600);
    font: var(--pk-text-body-sm);
    color: var(--pk-color-text-critical);
  }

  /* A list row with its own controls: a link, then actions. */
  .list-row--actions {
    padding-block: var(--pk-space-200);
  }

  .list-row__link {
    display: flex;
    flex: 1 1 auto;
    align-items: center;
    gap: var(--pk-space-300);
    min-inline-size: 0;
    color: inherit;
    text-decoration: none;

    &:hover .list-row__title {
      text-decoration: underline;
    }
  }

  /* The languages of one page, together. */
  .entry-group {
    border-block-start: var(--pk-border-width) solid var(--pk-color-border-default);

    .card > &:first-of-type {
      border-block-start: 0;
    }

    & > .list-row {
      border-block-start: 0;
    }
  }

  .entry-group__translate {
    padding: 0 var(--pk-space-500) var(--pk-space-300);
  }

  /* « Ajouter une langue » (Settings, Languages): the choice in its
     selection modal, a control of the footer's width, then Add; the hint
     under them. */
  .language-add {
    display: grid;
    flex: 1 1 100%;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: var(--pk-space-200) var(--pk-space-300);

    /* Phones: the choice on its own line, Add under it. */
    @media (width < 30rem) {
      grid-template-columns: minmax(0, 1fr);
    }
  }

  /* A language chosen in a form (the new site's main language, « Ajouter
     une langue »): the select modal's row drawn as a control. */
  :is(.language-main, .language-add) .select-field__row {
    border: var(--pk-border-width) solid var(--pk-color-border-default);
    background: var(--pk-color-bg-surface);
  }

  /* The new site's other languages, folded until asked for. */
  .language-choices {
    & > summary {
      list-style: none;
      inline-size: fit-content;

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

    &[open] > summary {
      margin-block-end: var(--pk-space-300);
    }
  }

  .language-add__hint {
    grid-column: 1 / -1;
    margin: 0;
    font: var(--pk-text-body-sm);
    color: var(--pk-color-text-secondary);
  }

  /* Rename in place (a passkey). */
  .rename {
    padding: 0 var(--pk-space-500) var(--pk-space-300);

    &[open] > .rename__summary {
      display: none;
    }
  }

  .rename__summary {
    list-style: none;
    inline-size: fit-content;

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

  .rename__form {
    display: flex;
    flex-wrap: wrap;
    align-items: end;
    gap: var(--pk-space-300);
  }

  .auth-card__switch {
    text-align: center;
  }

  /* ------------------------------------------------------------- Files */
  /* The library page and a file's page; the library, the picker and the file fields are
     pikumo-ui's (app.css, "File picker"). */

  /* A file's page: its icon, its name, its address. */
  .media-file {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--pk-space-300);
    margin-block-end: var(--pk-space-400);
    padding: var(--pk-space-800) var(--pk-space-400);
    border-radius: var(--pk-radius-300);
    background: var(--pk-color-bg-surface-secondary);
    text-align: center;
  }

  .media-file__icon {
    display: grid;
    place-items: center;
    inline-size: var(--pk-space-1600);
    block-size: var(--pk-space-1600);
    border-radius: var(--pk-radius-300);
    color: var(--pk-color-border-focus);
    background: var(--pk-color-bg-brand-subtle);

    & .icon {
      inline-size: var(--pk-space-1000);
      block-size: var(--pk-space-1000);
      stroke-width: 1.5;
    }
  }

  .media-file__name {
    font: var(--pk-text-body-md-medium);
    overflow-wrap: anywhere;
  }

  .media-file__link {
    margin-block-start: var(--pk-space-400);
  }

  .media-preview {
    inline-size: 100%;
    block-size: auto;
    margin-block-end: var(--pk-space-400);
    border-radius: var(--pk-radius-300);
    background: var(--pk-color-bg-surface-secondary);
  }

  /* Billing: the two plans side by side from 48rem, stacked below. */
  .plan-grid {
    display: grid;
    gap: var(--pk-space-400);
    align-items: stretch;
  }

  @media (min-width: 48rem) {
    .plan-grid {
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    /* The new password and its button on one line, the button level with
       the field (below its label). */
    .billing-password {
      display: grid;
      grid-template-columns: minmax(0, 24rem) auto;
      gap: var(--pk-space-300);
      align-items: start;
    }

    .billing-password__action {
      padding-block-start: var(--pk-space-600);
    }
  }

  .plan-card--current {
    box-shadow: var(--pk-shadow-100), 0 0 0 1px var(--pk-color-border-brand);
  }

  .plan-card__body {
    display: flex;
    flex-direction: column;
    gap: var(--pk-space-300);
    block-size: 100%;
  }

  .plan-card__head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--pk-space-200);
  }

  .plan-card__features {
    display: flex;
    flex-direction: column;
    gap: var(--pk-space-200);
    padding: 0;
    margin: 0;
    font: var(--pk-text-body-md);
  }

  .plan-card__features li {
    display: flex;
    align-items: flex-start;
    gap: var(--pk-space-200);
  }

  .plan-card__features .icon {
    flex: none;
    margin-block-start: 2px;
    color: var(--pk-color-icon-success);
  }

  /* The price and the actions sit at the foot, aligned across the cards. */
  .plan-card__foot {
    display: flex;
    flex-direction: column;
    gap: var(--pk-space-300);
    margin-block-start: auto;
    padding-block-start: var(--pk-space-300);
    border-block-start: 1px solid var(--pk-color-border-secondary);
  }

  .plan-card__form {
    display: flex;
    flex-direction: column;
    gap: var(--pk-space-300);
  }

  .plan-card__choices {
    gap: var(--pk-space-200);
  }

  .plan-card__price {
    font: var(--pk-text-heading-md);
  }

  .plan-card__prices {
    display: flex;
    flex-direction: column;
    gap: var(--pk-space-100);
    padding: 0;
    margin: 0;
  }
}

/* ------------------------------------------------------------------ Shell */

@layer shell {
  .skip-link {
    position: fixed;
    inset-block-start: var(--pk-space-200);
    inset-inline-start: var(--pk-space-200);
    z-index: var(--pk-z-toast);
    padding: var(--pk-space-200) var(--pk-space-300);
    border-radius: var(--pk-radius-200);
    font: var(--pk-text-body-md-medium);
    color: var(--pk-color-text-on-primary);
    background: var(--pk-color-bg-fill-primary);
    translate: 0 -200%;

    &:focus-visible {
      translate: 0;
    }
  }

  /* The mark (brand constants as SVG attributes) and the wordmark. */
  .logo {
    display: inline-flex;
    flex: none;
    align-items: center;
    gap: 0.625rem;
    font: var(--pk-text-wordmark);
    letter-spacing: var(--pk-tracking-tight);
    white-space: nowrap;
  }

  .logo__mark {
    flex: none;
    inline-size: var(--pk-size-mark);
    block-size: var(--pk-size-mark);
  }

  .app {
    display: grid;
    min-block-size: 100dvh;
  }

  /*
   * The frame (MyMCPs): dark, holding the sidebar on the left and the page on a rounded canvas
   * with 8px of frame around it. The canvas scrolls; the frame stays put. Under 48rem: a dark
   * top bar, the canvas under it, and the sidebar as a drawer (a native popover).
   */
  .app--frame {
    grid-template-columns: var(--pk-size-sidebar) minmax(0, 1fr);
    block-size: 100dvh;
    padding: var(--pk-size-frame-gap) var(--pk-size-frame-gap) var(--pk-size-frame-gap) 0;
    overflow: hidden;
    background: var(--pk-color-bg-frame);
    transition: grid-template-columns var(--pk-duration-base) var(--pk-ease);

    @media (width < 48rem) {
      display: block;
      block-size: auto;
      min-block-size: 100dvh;
      padding: 0;
      overflow: visible;
    }
  }

  :root:has(.app--frame) {
    background: var(--pk-color-bg-frame);
  }

  /* Under 48rem only: the menu button, the logo (or a back link and the title), the avatar. */
  .topbar {
    display: none;

    @media (width < 48rem) {
      position: sticky;
      inset-block-start: 0;
      z-index: var(--pk-z-bar);
      display: flex;
      align-items: center;
      gap: var(--pk-space-100);
      block-size: calc(var(--pk-size-topbar) + env(safe-area-inset-top));
      padding: env(safe-area-inset-top) var(--pk-space-150) 0;
      color: var(--pk-color-text-on-frame-active);
      background: var(--pk-color-bg-frame);

      & :focus-visible {
        outline-color: var(--pk-color-border-focus-inverse);
      }
    }
  }

  .topbar__brand {
    display: inline-flex;
    margin-inline: auto;
    padding: var(--pk-space-100);
    border-radius: var(--pk-radius-200);
    color: var(--pk-color-text-on-frame-active);
  }

  .topbar__page {
    display: flex;
    flex: 1 1 0;
    align-items: center;
    gap: var(--pk-space-100);
    min-inline-size: 0;
  }

  .topbar__title {
    flex: 1 1 0;
    min-inline-size: 0;
    font: var(--pk-text-body-lg-medium);
  }

  .topbar__account {
    display: grid;
    flex: none;
    place-items: center;
    inline-size: var(--pk-size-control-lg);
    block-size: var(--pk-size-control-lg);
    border-radius: var(--pk-radius-full);
  }

  /* The sidebar: a column of the frame from 48rem, a drawer under it. */
  .sidebar {
    position: static;
    display: flex;
    flex-direction: column;
    inline-size: auto;
    block-size: 100%;
    min-block-size: 0;
    margin: 0;
    padding: var(--pk-space-400) var(--pk-space-200) var(--pk-space-200);
    overflow: hidden auto;
    border: 0;
    color: var(--pk-color-text-on-frame);
    background: var(--pk-color-bg-frame);
    scrollbar-width: none;

    & :focus-visible {
      outline-color: var(--pk-color-border-focus-inverse);
    }

    @media (width < 48rem) {
      position: fixed;
      inset: 0 auto 0 0;
      inline-size: min(var(--pk-size-sidebar-drawer), calc(100vw - var(--pk-space-1200)));
      block-size: 100dvh;
      max-block-size: none;
      padding-block-start: calc(var(--pk-space-300) + env(safe-area-inset-top));
      padding-block-end: calc(var(--pk-space-200) + env(safe-area-inset-bottom));
      border-inline-end: var(--pk-border-width) solid var(--pk-color-border-frame);
      box-shadow: var(--pk-shadow-600);
      translate: -100% 0;
      transition:
        translate var(--pk-duration-base) var(--pk-ease),
        display var(--pk-duration-base) allow-discrete,
        overlay var(--pk-duration-base) allow-discrete;

      &:not(:popover-open) {
        display: none;
      }

      &:popover-open {
        translate: 0;

        @starting-style {
          translate: -100% 0;
        }
      }

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

  .sidebar__header {
    display: flex;
    flex: none;
    align-items: center;
    justify-content: space-between;
    gap: var(--pk-space-200);
    padding: 0 0 var(--pk-space-300) var(--pk-space-200);
  }

  .sidebar__brand {
    display: inline-flex;
    min-inline-size: 0;
    border-radius: var(--pk-radius-200);
    color: var(--pk-color-text-on-frame-active);
  }

  .sidebar__close {
    display: none;

    @media (width < 48rem) {
      display: inline-grid;
    }
  }

  .sidebar__collapse {
    @media (width < 48rem) {
      display: none;
    }
  }

  .sidebar__nav {
    display: flex;
    flex: 1 0 auto;
    flex-direction: column;
    gap: var(--pk-space-050);
    padding-block-end: var(--pk-space-400);
  }

  /* A destination (selected: aria-current="page"), on the frame. */
  .nav-item {
    display: flex;
    flex: none;
    align-items: center;
    gap: var(--pk-space-200);
    inline-size: 100%;
    block-size: var(--pk-size-control-md);
    padding-inline: var(--pk-space-200);
    border-radius: var(--pk-radius-200);
    font: var(--pk-text-body-md-medium);
    letter-spacing: var(--pk-tracking-normal);
    text-align: start;
    color: var(--pk-color-text-on-frame);
    transition:
      background-color var(--pk-duration-fast) var(--pk-ease),
      color var(--pk-duration-fast) var(--pk-ease);

    & > .icon {
      color: var(--pk-color-icon-on-frame);
    }

    &:hover,
    &[aria-expanded="true"] {
      color: var(--pk-color-text-on-frame-active);
      background: var(--pk-color-bg-frame-hover);

      & > .icon {
        color: var(--pk-color-icon-on-frame-active);
      }
    }

    &[aria-current="page"] {
      color: var(--pk-color-text-on-frame-active);
      background: var(--pk-color-bg-frame-selected);
      box-shadow: inset 0 0 0 var(--pk-border-width) var(--pk-color-border-frame);

      & > .icon {
        color: var(--pk-color-icon-on-frame-active);
      }
    }

    @media (width < 48rem) {
      block-size: var(--pk-size-control-lg);
      font: var(--pk-text-body-lg-medium);
    }
  }

  .nav-item__label {
    flex: 1 1 0;
    min-inline-size: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* The site switcher: the first entry, the site's icon in a small tile. */
  .nav-item--switcher {
    margin-block-end: var(--pk-space-100);
    color: var(--pk-color-text-on-frame-active);
  }

  .nav-item__tile {
    display: inline-grid;
    flex: none;
    place-items: center;
    inline-size: 1.25rem;
    block-size: 1.25rem;
    margin-inline-start: calc(-1 * var(--pk-space-050));
    border-radius: var(--pk-radius-150);
    color: var(--pk-color-text-on-inverse-brand);
    background: var(--pk-color-bg-frame-selected);
    box-shadow: inset 0 0 0 var(--pk-border-width) var(--pk-color-border-frame);

    & > .icon {
      inline-size: var(--pk-size-icon-sm);
      block-size: var(--pk-size-icon-sm);
    }
  }

  /* A group's label. */
  .nav-section {
    display: flex;
    align-items: flex-start;
    inline-size: 100%;
    block-size: var(--pk-size-control-md);
    padding: var(--pk-space-300) var(--pk-space-200) var(--pk-space-100);
    font: var(--pk-text-body-sm);
    white-space: nowrap;
    color: var(--pk-color-text-on-frame-secondary);
  }

  /* The site's card: online or not, its address. */
  .site-card {
    display: flex;
    flex: none;
    flex-direction: column;
    gap: var(--pk-space-200);
    margin-block-end: var(--pk-space-200);
    padding: var(--pk-space-300);
    border: var(--pk-border-width) solid var(--pk-color-border-frame);
    border-radius: var(--pk-radius-300);
    background: var(--pk-color-bg-frame-hover);
  }

  .site-card__status {
    display: flex;
    align-items: center;
    gap: var(--pk-space-200);
    font: var(--pk-text-body-sm-medium);
    color: var(--pk-color-text-on-frame-active);
  }

  /* Closed behind its password: the pill leads to Billing. */
  .site-card__closed {
    transition: filter var(--pk-duration-fast) var(--pk-ease);

    &:hover {
      filter: brightness(1.08);
    }
  }

  .site-card__address {
    display: flex;
    align-items: center;
    gap: var(--pk-space-200);
    min-inline-size: 0;
    font: var(--pk-text-code-sm);
    color: var(--pk-color-text-on-frame-secondary);

    & > .site-card__link {
      display: flex;
      flex: 1 1 0;
      align-items: center;
      gap: var(--pk-space-100);
      min-inline-size: 0;
      border-radius: var(--pk-radius-100);

      & > span {
        min-inline-size: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      & > .icon {
        inline-size: var(--pk-size-icon-sm);
        block-size: var(--pk-size-icon-sm);
      }

      &:hover {
        color: var(--pk-color-text-on-frame-active);
      }
    }
  }

  .site-card__copy {
    display: grid;
    flex: none;
    place-items: center;
    border-radius: var(--pk-radius-100);
    color: var(--pk-color-icon-on-frame);
    cursor: pointer;

    & > .icon {
      inline-size: var(--pk-size-icon-sm);
      block-size: var(--pk-size-icon-sm);
    }

    &:hover {
      color: var(--pk-color-icon-on-frame-active);
    }
  }

  /* The account, at the foot of the sidebar: opens the user menu. */
  .nav-user {
    display: flex;
    flex: none;
    align-items: center;
    gap: var(--pk-space-200);
    inline-size: 100%;
    padding: var(--pk-space-200);
    border-radius: var(--pk-radius-200);
    text-align: start;
    color: var(--pk-color-icon-on-frame);
    cursor: pointer;
    transition: background-color var(--pk-duration-fast) var(--pk-ease);

    &:hover,
    &[aria-expanded="true"] {
      background: var(--pk-color-bg-frame-hover);
    }
  }

  .nav-user__text {
    display: flex;
    flex: 1 1 0;
    flex-direction: column;
    min-inline-size: 0;
  }

  .nav-user__name,
  .nav-user__email {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .nav-user__name {
    font: var(--pk-text-body-md-medium);
    color: var(--pk-color-text-on-frame-active);
  }

  .nav-user__email {
    font: var(--pk-text-body-xs);
    color: var(--pk-color-text-on-frame-secondary);
  }

  /* The folded rail (from 48rem): <html data-sidebar="collapsed">, set by app.js or the server. */
  @media (width >= 48rem) {
    :root[data-sidebar="collapsed"] {
      & .app--frame {
        grid-template-columns: var(--pk-size-sidebar-rail) minmax(0, 1fr);
      }

      & .sidebar__header {
        flex-direction: column;
        gap: var(--pk-space-200);
        padding-inline-start: 0;
      }

      & .logo__word,
      & .nav-item__label,
      & .nav-item--switcher > .icon:last-child,
      & .nav-user__text,
      & .nav-user > .icon,
      & .site-card {
        display: none;
      }

      & .nav-item,
      & .nav-user {
        justify-content: center;
        padding-inline: 0;
      }

      & .nav-item__tile {
        margin: 0;
      }

      /* Group labels become hairlines. */
      & .nav-section {
        block-size: var(--pk-space-300);
        margin-block-start: var(--pk-space-300);
        padding: 0;
        border-block-start: var(--pk-border-width) solid var(--pk-color-border-frame);
        font-size: 0;
      }

      & .save-bar {
        inset-inline-start: calc(var(--pk-size-sidebar-rail) + var(--pk-space-200));
      }
    }
  }

  /* The page lives on the canvas, which scrolls; the frame stays put. */
  .panel {
    position: relative;
    min-inline-size: 0;
    padding: var(--pk-space-800);
    overflow: hidden auto;
    border-radius: var(--pk-radius-500);
    color: var(--pk-color-text-default);
    background: var(--pk-color-bg-canvas);
    box-shadow: inset 0 0 0 var(--pk-border-width) var(--pk-color-border-panel);
    scrollbar-width: thin;
    scrollbar-color: var(--pk-color-border-strong) transparent;

    @media (width < 48rem) {
      min-block-size: calc(100dvh - var(--pk-size-topbar) - var(--pk-space-200));
      margin: 0 var(--pk-space-200) var(--pk-space-200);
      padding: var(--pk-space-600) var(--pk-space-400);
      overflow: clip;
    }
  }

  :is(.app--focus, .app:has(.action-bar), .app:has(.save-bar)) .panel {
    @media (width < 48rem) {
      padding-block-end: calc(var(--pk-size-action-bar) + env(safe-area-inset-bottom) + var(--pk-space-400));
    }
  }

  .page {
    container: page / inline-size;
    display: flex;
    flex-direction: column;
    gap: var(--pk-space-600);
    inline-size: 100%;
    max-inline-size: var(--pk-size-page-max);
    margin-inline: auto;

    @media (width < 48rem) {
      gap: var(--pk-space-500);
    }
  }

  /* A single column starts where the wide pages start: on their left edge. */
  .page--narrow {
    max-inline-size: var(--pk-size-page-narrow);
    margin-inline: max(0px, (100% - var(--pk-size-page-max)) / 2) auto;
  }

  .page--wide {
    max-inline-size: var(--pk-size-page-wide);
  }

  /* Before sign-in and error pages: one card on the dark frame, controls Large. */
  .auth {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-block-size: 100dvh;
    padding: var(--pk-space-600) var(--pk-space-400);
    background: var(--pk-color-bg-frame);
  }

  :root:has(.auth) {
    background: var(--pk-color-bg-frame);
  }

  .auth__main {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--pk-space-600);
    inline-size: 100%;

    @media (width < 48rem) {
      gap: var(--pk-space-500);
    }
  }

  .auth-card {
    display: flex;
    flex-direction: column;
    gap: var(--pk-space-600);
    inline-size: 100%;
    max-inline-size: var(--pk-size-auth-card);
    padding: var(--pk-space-800);
    border-radius: var(--pk-radius-500);
    color: var(--pk-color-text-default);
    background: var(--pk-color-bg-surface);
    box-shadow: var(--pk-shadow-600);

    & > .logo {
      align-self: flex-start;
    }

    @media (width < 48rem) {
      gap: var(--pk-space-500);
      padding: var(--pk-space-600);
    }
  }

  .auth-card__heading {
    display: flex;
    flex-direction: column;
    gap: var(--pk-space-100);
  }

  .auth-card__title {
    font: var(--pk-text-heading-2xl);
    letter-spacing: var(--pk-tracking-display);
  }

  .auth-card__subtitle {
    font: var(--pk-text-body-lg);
    color: var(--pk-color-text-secondary);
    text-wrap: pretty;
  }

  .auth__footnote {
    font: var(--pk-text-body-sm);
    text-align: center;
    color: var(--pk-color-text-on-frame-secondary);
  }

  .divider {
    display: flex;
    align-items: center;
    gap: var(--pk-space-300);
    font: var(--pk-text-body-sm);
    color: var(--pk-color-text-tertiary);

    &::before,
    &::after {
      content: "";
      flex: 1 1 0;
      block-size: var(--pk-border-width);
      background: var(--pk-color-border-default);
    }
  }

  /*
   * Page transitions (the @view-transition rule at the top). From 48rem the sidebar and the
   * canvas are captured on their own: the sidebar swaps at once (no fade, no move), only the
   * canvas cross-fades. Under 48rem the page scrolls as a whole, so the top bar swaps at once
   * and the rest (the drawer included) cross-fades. The old and new images share one duration
   * and easing so their opacities always add up to 1: the dark frame never shows through.
   */
  @media (width >= 48rem) {
    .sidebar {
      view-transition-name: pk-sidebar;
    }

    .panel {
      view-transition-name: pk-canvas;
    }
  }

  .topbar {
    view-transition-name: pk-topbar;
  }

  ::view-transition-group(*),
  ::view-transition-old(*),
  ::view-transition-new(*) {
    animation-duration: var(--pk-duration-base);
    animation-timing-function: var(--pk-ease);
  }

  ::view-transition-group(pk-sidebar),
  ::view-transition-group(pk-topbar),
  ::view-transition-new(pk-sidebar),
  ::view-transition-new(pk-topbar) {
    animation: none;
  }

  ::view-transition-old(pk-sidebar),
  ::view-transition-old(pk-topbar) {
    display: none;
  }
}
