@layer surfaces {
  :root {
    --topbar-action-target: 36px;
    --topbar-floating-chrome-plate-block-size: calc(var(--topbar-action-target) + (2 * var(--floating-chrome-plate-padding)));
    --topbar-h: calc(var(--topbar-floating-chrome-plate-block-size) + (var(--space-related) * 2) + var(--app-safe-top));
    --topbar-document-chip-max: min(42vw, 360px);
  }

  #topbar,
  .topbar {
    align-items: center;
    background: transparent;
    block-size: var(--topbar-h);
    border-block-end: 0;
    display: flex;
    flex: 0 0 0;
    gap: 0;
    inset-block-start: 0;
    inset-inline: 0;
    justify-content: center;
    min-inline-size: 0;
    padding-block: max(var(--space-related), var(--app-safe-top)) var(--space-related);
    padding-inline: max(var(--space-cluster), var(--app-safe-left)) max(var(--space-cluster), var(--app-safe-right));
    pointer-events: none;
    position: fixed;
    z-index: var(--z-topbar);
  }

  .topbar-action-rail {
    align-items: center;
    background: transparent;
    border: 0;
    border-radius: 0;
    box-shadow: none;
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
    display: grid;
    flex: 1 1 auto;
    gap: var(--space-cluster);
    grid-template-columns: auto minmax(0, 1fr) auto;
    inline-size: 100%;
    max-inline-size: 100%;
    min-block-size: var(--topbar-floating-chrome-plate-block-size);
    min-inline-size: 0;
    overflow: visible;
    padding: 0;
    pointer-events: none;
    scroll-padding-inline: var(--space-related);
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  .topbar-action-rail::-webkit-scrollbar,
  .topbar-sidebar-group::-webkit-scrollbar { display: none; }

  .topbar-group,
  .topbar-document-chip,
  .topbar-icon-button {
    align-items: center;
    display: flex;
  }

  .topbar-group {
    min-block-size: var(--topbar-action-target);
    min-inline-size: 0;
    overflow: visible;
  }

  .topbar-title-group {
    flex: 0 0 auto;
    grid-column: 1;
    inline-size: auto;
    justify-content: flex-start;
    justify-self: start;
    min-block-size: var(--topbar-action-target);
    padding-inline: 0;
    pointer-events: auto;
  }

  .topbar-sidebar-group {
    background: var(--floating-chrome-plate-bg);
    border: 1px solid var(--floating-chrome-plate-border);
    border-radius: var(--floating-chrome-plate-radius);
    box-shadow: var(--elevation-floating);
    -webkit-backdrop-filter: var(--surface-filter-floating);
    backdrop-filter: var(--surface-filter-floating);
    flex: 0 0 auto;
    gap: var(--floating-chrome-control-gap);
    grid-column: 3;
    justify-self: end;
    max-inline-size: 100%;
    block-size: var(--topbar-floating-chrome-plate-block-size);
    overflow: visible;
    padding: var(--floating-chrome-plate-padding);
    pointer-events: auto;
  }

  .topbar-document-chip {
    background: var(--floating-chrome-plate-bg);
    border: 1px solid var(--floating-chrome-plate-border);
    border-radius: var(--floating-chrome-plate-radius);
    box-shadow: var(--elevation-floating);
    -webkit-backdrop-filter: var(--surface-filter-floating);
    backdrop-filter: var(--surface-filter-floating);
    color: var(--text-sec);
    cursor: pointer;
    flex: 0 1 auto;
    gap: var(--space-related);
    inline-size: fit-content;
    justify-content: flex-start;
    max-inline-size: var(--topbar-document-chip-max);
    block-size: var(--topbar-floating-chrome-plate-block-size);
    min-inline-size: 0;
    overflow: hidden;
    padding-block: 0;
    padding-inline: var(--space-cluster);
    transition:
      background-color var(--motion-duration-micro) var(--motion-ease-standard),
      border-color var(--motion-duration-micro) var(--motion-ease-standard),
      box-shadow var(--motion-duration-micro) var(--motion-ease-standard),
      color var(--motion-duration-micro) var(--motion-ease-standard),
      transform var(--motion-duration-micro) var(--motion-ease-out);
  }

  .topbar-document-chip:hover,
  .topbar-document-chip:focus-visible,
  .topbar-document-chip[aria-expanded="true"] {
    background: var(--floating-chrome-control-bg-hover);
    border-color: var(--floating-chrome-control-border-active);
    color: var(--floating-chrome-control-color-active);
  }

  .topbar-document-chip:focus-visible {
    outline: 0;
    box-shadow: var(--focus-ring-shadow), var(--elevation-control);
  }

  .topbar-document-chip:active { transform: translateY(1px) scale(var(--motion-scale-press)); }

  .topbar-document-chip-title,
  .topbar-document-chip-stat {
    display: inline-block;
    line-height: 1;
    min-inline-size: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .topbar-document-chip-title {
    color: currentColor;
    flex: 1 1 auto;
    font-size: var(--ui-font-size);
    max-inline-size: 100%;
  }

  .topbar-document-chip-stat {
    display: none;
  }

  .topbar-document-chip-chevron {
    block-size: 16px;
    flex: 0 0 16px;
    inline-size: 16px;
    opacity: .72;
    transition: transform var(--motion-duration-micro) var(--motion-ease-out), opacity var(--motion-duration-micro) var(--motion-ease-standard);
  }

  .topbar-document-chip[aria-expanded="true"] .topbar-document-chip-chevron {
    opacity: 1;
    transform: rotate(180deg);
  }

  .topbar-document-chip.is-save-error {
    border-color: color-mix(in srgb, var(--danger, var(--accent)) 54%, var(--floating-chrome-plate-border));
    color: var(--danger, var(--text-sec));
  }

  .topbar-save-status {
    color: var(--text-muted);
    transition: color var(--motion-duration-state) var(--motion-ease-standard), opacity var(--motion-duration-state) var(--motion-ease-standard), transform var(--motion-duration-state) var(--motion-ease-out);
  }

  .topbar-save-status.is-transitioning {
    opacity: .62;
    transform: translateY(calc(-1 * var(--motion-distance-reveal)));
  }

  .topbar-save-status.is-settling {
    animation: compose-state-settle var(--motion-duration-state) var(--motion-ease-out) both;
  }


  .topbar-save-status.is-unsaved { color: var(--text-sec); }
  .topbar-save-status.is-saving { color: var(--accent); }
  .topbar-save-status.is-error { color: var(--danger, var(--text-sec)); }

  @media (pointer: coarse) {
    :root { --topbar-action-target: var(--touch-target); }
  }

  @media (max-width: 760px) {
    :root { --topbar-document-chip-max: clamp(124px, 42vw, 220px); }

    #topbar,
    .topbar {
      justify-content: flex-start;
      padding-inline: max(var(--space-related), var(--app-safe-left)) max(var(--space-related), var(--app-safe-right));
    }

    .topbar-action-rail {
      background: var(--floating-chrome-plate-bg);
      border: 1px solid var(--floating-chrome-plate-border);
      border-radius: var(--floating-chrome-plate-radius);
      box-shadow: var(--elevation-floating);
      -webkit-backdrop-filter: var(--surface-filter-floating);
      backdrop-filter: var(--surface-filter-floating);
      display: flex;
      flex: 0 1 auto;
      gap: var(--floating-chrome-control-gap);
      inline-size: max-content;
      max-inline-size: 100%;
      overflow-x: auto;
      overflow-y: hidden;
      padding: var(--floating-chrome-plate-padding);
      pointer-events: auto;
      -webkit-overflow-scrolling: touch;
    }

    .topbar-title-group {
      flex: 0 0 auto;
      justify-content: flex-start;
      justify-self: auto;
      pointer-events: auto;
    }

    .topbar-sidebar-group {
      background: transparent;
      border-color: transparent;
      border-radius: 0;
      box-shadow: none;
      -webkit-backdrop-filter: none;
      backdrop-filter: none;
      flex: 0 0 auto;
      grid-column: auto;
      justify-self: auto;
      max-inline-size: none;
      block-size: var(--topbar-action-target);
      min-block-size: var(--topbar-action-target);
      overflow: visible;
      padding: 0;
      pointer-events: auto;
    }

    .topbar-document-chip {
      background: var(--floating-chrome-control-bg);
      border-color: var(--floating-chrome-control-border);
      box-shadow: none;
      -webkit-backdrop-filter: none;
      backdrop-filter: none;
      block-size: var(--topbar-action-target);
      min-block-size: var(--topbar-action-target);
    }
  }

  @media (max-width: 380px) {
    :root { --topbar-document-chip-max: clamp(112px, 40vw, 176px); }

    .topbar-document-chip { padding-inline: var(--space-related); }
  }

  @media (prefers-reduced-motion: reduce) {
    .topbar-document-chip,
    .topbar-save-status,
    .topbar-icon-button,
    .toolbar-button {
      transition-duration: var(--motion-duration-reduced);
    }
  }

  @media (forced-colors: active) {
    .topbar-action-rail,
    .topbar-document-chip,
    .topbar-sidebar-group {
      background: Canvas;
      border-color: ButtonText;
      color: CanvasText;
      forced-color-adjust: auto;
    }

    .topbar-action-rail {
      background: transparent;
      border-color: transparent;
    }

    @media (max-width: 760px) {
      .topbar-action-rail {
        background: Canvas;
        border-color: ButtonText;
      }

      .topbar-sidebar-group {
        background: transparent;
        border-color: transparent;
      }
    }

    .topbar-document-chip:hover,
    .topbar-document-chip:focus-visible,
    .topbar-document-chip[aria-expanded="true"] {
      background: Highlight;
      border-color: Highlight;
      color: HighlightText;
    }

    .topbar-document-chip-chevron { color: currentColor; }

    .topbar-document-chip.is-save-error {
      border-color: Mark;
      color: Mark;
    }
  }
}
