/* Shared treatment for the blue header and the primary heading of each section.
   View-specific rules still control when their commands are available. */
@layer sfera-section-chrome {
  #signed-in {
    --section-bar-title-size: 15px;
    --section-bar-gap: clamp(8px, 1vw, 14px);
    --section-control-size: 36px;
    --section-control-border: rgba(192, 216, 255, .23);
    --section-control-background: rgba(255, 255, 255, .075);
    --section-create-background: #e4f0ff;
    --section-search-background: rgba(255, 255, 255, .09);
    --section-search-focus-background: rgba(255, 255, 255, .15);
    --section-search-focus-border: rgba(219, 235, 255, .66);
    --section-search-focus-shadow: 0 0 0 2px rgba(175, 212, 255, .12);
  }

  #signed-in .app-topbar :where(.opp-desk-topbar__actions, .sfera-section-bar__actions) {
    display: flex;
    align-items: center;
  }

  #signed-in .app-topbar .portal-topbar__actions {
    order: 10 !important;
    margin-left: auto !important;
  }

  @media (min-width: 901px) {
    #signed-in .app-topbar #today-studio-search-toggle { display: none !important; }
  }

  /* Browsers with the previous toolbar module can still build this label. */
  #signed-in .app-topbar #today-studio-topbar-search > .sr-only {
    display: none !important;
  }

  #signed-in .app-topbar :where(
    .sfera-dashboard-topbar-label, .sfera-companies-topbar__title,
    .lead-directory-title, .today-studio-topbar__title, #opportunity-desk-title,
    .sfera-projects-topbar__title, .task-studio-topbar__name, .task-sfera-topbar__title,
    .sfera-strategies-topbar__title, .sfera-notes-topbar__title, .sfera-stats-topbar__title,
    .sfera-support-topbar__title, .sfera-changelog-topbar__title, .sfera-ads-topbar__title,
    .sfera-section-bar__title
  ) {
    margin: 0 !important;
    color: #f6f9ff !important;
    font-size: var(--section-bar-title-size) !important;
    font-weight: 600 !important;
    line-height: 1.25 !important;
    letter-spacing: 0 !important;
  }

  #signed-in .app-topbar :where(
    .sfera-companies-topbar, .sfera-leads-topbar, .lead-directory-header__identity,
    .today-studio-topbar, .opp-desk-topbar, .sfera-projects-topbar, .task-studio-topbar,
    .sfera-strategies-topbar, .sfera-notes-topbar, .sfera-changelog-topbar, .sfera-section-bar
  ) {
    gap: var(--section-bar-gap) !important;
    justify-content: flex-start !important;
  }

  #signed-in .app-topbar :where(
    .lead-directory-header__actions, .today-studio-topbar__actions,
    .sfera-companies-topbar__tools, .sfera-projects-topbar__actions,
    .task-studio-topbar__actions, .sfera-notes-topbar__tools, .sfera-ads-topbar__tools,
    .opp-desk-topbar__actions, .sfera-section-bar__actions
  ) {
    flex: 0 0 auto !important;
    gap: 7px !important;
    margin: 0 !important;
    justify-content: flex-start !important;
  }

  #signed-in .app-topbar :where(.lead-directory-header__identity, .sas-topbar-search) {
    flex: 0 1 auto !important;
    margin-left: 0 !important;
  }

  #signed-in .app-topbar :where(.sfera-projects-topbar__modes, .sfera-strategies-topbar__create) {
    margin-inline: 0 !important;
  }

  #signed-in .app-topbar :where(
    .lead-directory-search, .sfera-companies-topbar__search, .today-studio-topbar__search,
    .opp-desk-search, .sfera-projects-topbar__search, .sfera-projects-topbar__detail-search,
    .sas-topbar-search__field, .sfera-notes-topbar__search, .sfera-changelog-topbar__search,
    .sfera-ads-topbar__search, .sfera-section-bar__search
  ) {
    box-sizing: border-box !important;
    flex: 0 1 auto !important;
    width: min(27vw, 315px) !important;
    min-width: 155px !important;
    max-width: 315px !important;
    height: 38px !important;
    min-height: 38px !important;
    align-items: center !important;
    gap: 9px !important;
    margin: 0 !important;
    padding: 0 10px !important;
    border: 1px solid rgba(192, 216, 255, .21) !important;
    border-radius: 11px !important;
    background: var(--section-search-background) !important;
    color: #d8e9ff !important;
    outline: none !important;
    box-shadow: none !important;

    &:hover {
      background: var(--section-search-focus-background) !important;
    }

    &:focus-within {
      border-color: var(--section-search-focus-border) !important;
      background: var(--section-search-focus-background) !important;
      box-shadow: var(--section-search-focus-shadow) !important;
    }

    & > :where(svg, .task-v3-search-icon) {
      position: static !important;
      display: block !important;
      flex: 0 0 18px !important;
      width: 18px !important;
      height: 18px !important;
      margin: 0 !important;
      color: #e4f1ff !important;
      transform: none !important;
    }

    & svg {
      width: 18px !important;
      height: 18px !important;
      fill: none !important;
      stroke: #e4f1ff !important;
      stroke-width: 1.8 !important;
    }

    & input {
      display: block !important;
      flex: 1 1 auto !important;
      width: 100% !important;
      min-width: 0 !important;
      height: 36px !important;
      min-height: 36px !important;
      margin: 0 !important;
      padding: 0 !important;
      border: 0 !important;
      border-radius: 0 !important;
      background: transparent !important;
      color: #fff !important;
      outline: none !important;
      box-shadow: none !important;
      font-size: 12px !important;
      font-weight: 500 !important;
      line-height: 1.4 !important;
    }

    & input::placeholder {
      color: #b9cbea !important;
      opacity: 1 !important;
    }

    & > button:not([hidden]) {
      flex: 0 0 26px !important;
      width: 26px !important;
      min-width: 26px !important;
      height: 26px !important;
      min-height: 26px !important;
      margin: 0 !important;
    }
  }

  #signed-in .app-topbar :where(
    .sfera-companies-topbar__action, .leads-create--desktop, .leads-quick-delete--admin,
    .lead-directory-search-trigger, .today-studio-topbar__action, #opportunity-new,
    .s4-refresh, .s8-mobile-search-toggle, .sfera-projects-topbar__action,
    #task-v2-refresh, .task-v5-search-toggle, #sas-topbar-search-toggle,
    .sfera-stats-topbar__refresh, .sfera-changelog-topbar__refresh,
    .sfera-changelog-topbar__report, .sfera-dashboard-topbar-tools > #dashboard-refresh,
    .sfera-ads-topbar__new, .sfera-ads-topbar__search-trigger, [data-studio-refresh],
    .sfera-section-bar__button
  ) {
    box-sizing: border-box !important;
    flex: 0 0 var(--section-control-size) !important;
    width: var(--section-control-size) !important;
    min-width: var(--section-control-size) !important;
    max-width: var(--section-control-size) !important;
    height: var(--section-control-size) !important;
    min-height: var(--section-control-size) !important;
    max-height: var(--section-control-size) !important;
    align-items: center !important;
    justify-content: center !important;
    place-items: center !important;
    margin: 0 !important;
    padding: 8px !important;
    border: 1px solid var(--section-control-border) !important;
    border-radius: 10px !important;
    background: var(--section-control-background) !important;
    color: #f4f9ff !important;
    box-shadow: none !important;
    transform: none !important;
    scale: none !important;

    &:is(:hover, :active, [aria-expanded="true"]):not(:disabled) {
      background: rgba(255, 255, 255, .15) !important;
      border-color: rgba(196, 224, 255, .48) !important;
    }

    &:focus-visible {
      outline: 2px solid #b9cbea !important;
      outline-offset: 2px !important;
    }

    & > svg {
      display: block !important;
      flex: none !important;
      width: 19px !important;
      height: 19px !important;
      margin: 0 !important;
      fill: none !important;
      color: currentColor !important;
      stroke: currentColor !important;
      stroke-width: 1.8 !important;
      stroke-linecap: round !important;
      stroke-linejoin: round !important;
    }
  }

  #signed-in .app-topbar :where(
    .sfera-companies-topbar__create, .leads-create--desktop, #today-activities-new,
    #opportunity-new, .sfera-projects-topbar__action--new, .sfera-ads-topbar__new,
    .sfera-section-bar__create
  ) {
    background: var(--section-create-background) !important;
    border-color: #c8dfff !important;
    color: #0e2166 !important;
    font-size: 0 !important;

    &:is(:hover, :active):not(:disabled) {
      background: #f2f7ff !important;
      border-color: #c8dfff !important;
    }
  }

  #signed-in .app-topbar :where(#opportunity-new, .opp-desk-topbar .s4-refresh, #task-v2-refresh):not([hidden]) {
    display: inline-grid !important;
  }

  #signed-in .app-topbar #collaboration-bell {
    color: #e8f3ff !important;

    &:is(:hover, :focus-visible, :active, [aria-expanded="true"]) {
      border-color: rgba(196, 224, 255, .48) !important;
      background: rgba(255, 255, 255, .15) !important;
      transform: none !important;
    }
  }

  /* Same type scale as “Il tuo portafoglio” in Opportunities. */
  #signed-in .app-content :where(
    .opp-desk-heading > h2, #companies-title, .lead-directory-portfolio-title,
    .today-studio-intro h2, .projects-landing-heading h1,
    .task-studio-intro h1, .task-sfera-intro h1, #notes-page-title,
    #strategies-page-title, #recurring-page-title, .agenda__header h2,
    #dashboard-title, .dashboard-welcome h1, .dashboard-workspace > .dashboard-section-heading h2,
    #activity-statistics-title,
    .sfera-hub-heading h1, .support-hero h1, .changelog-studio__intro h1,
    #guide-title, #guide-dashboard-title, #user-management-view h1,
    #sqlite-read-model-view h1, [data-app-view="usage"] h1
  ) {
    font-size: clamp(26px, 2.55vw, 38px) !important;
    font-weight: 650 !important;
    line-height: 1.16 !important;
    letter-spacing: -.055em !important;
  }

  #signed-in .app-topbar :where(
    .sfera-section-bar, .sfera-section-bar__title--guides, .sfera-section-bar__title--users,
    .sfera-section-bar__title--usage, .sfera-section-bar__title--sqlite, .sfera-section-bar__search-toggle
  ) {
    display: none !important;
  }

  #signed-in:has(#agenda-view:not([hidden])) .app-topbar .sfera-section-bar--agenda,
  #signed-in:has(#recurring-task-definition-view:not([hidden])) .app-topbar .sfera-section-bar--recurrences {
    display: flex !important;
    align-items: center;
    flex: 1 1 auto;
    min-width: 0;
  }

  #signed-in:has(:is(#guide-view, #guide-dashboard-view):not([hidden])) .app-topbar .sfera-section-bar__title--guides,
  #signed-in:has(#user-management-view:not([hidden])) .app-topbar .sfera-section-bar__title--users,
  #signed-in:has([data-app-view="usage"]:not([hidden])) .app-topbar .sfera-section-bar__title--usage,
  #signed-in:has(#sqlite-read-model-view:not([hidden])) .app-topbar .sfera-section-bar__title--sqlite,
  #signed-in .sfera-section-bar :where(.sfera-section-bar__search, .sfera-section-bar__button:not(.sfera-section-bar__search-toggle)) {
    display: flex !important;
  }

  @media (max-width: 1100px) {
    #signed-in { --section-bar-title-size: 13px; }
  }

  @media (max-width: 900px) {
    #signed-in { --section-bar-gap: 7px; }

    #signed-in .app-topbar :where(
      .sfera-companies-topbar.is-search-open .sfera-companies-topbar__search,
      .sfera-leads-topbar.is-search-open .lead-directory-search,
      .today-studio-topbar.is-search-open .today-studio-topbar__search,
      .opp-desk-topbar.s8-search-open .opp-desk-search,
      .sfera-projects-topbar.search-open .sfera-projects-topbar__search,
      .task-studio-topbar.task-v5-search-expanded .task-studio-topbar__search,
      .sas-topbar-search.is-search-open .sas-topbar-search__field,
      .sfera-ads-topbar.is-search-open .sfera-ads-topbar__search,
      .sfera-section-bar.is-search-open .sfera-section-bar__search,
      .sfera-notes-topbar.is-search-open .sfera-notes-topbar__search,
      .sfera-changelog-topbar.is-search-open .sfera-changelog-topbar__search
    ) {
      position: fixed !important;
      inset: calc(var(--sfera-topbar-height, 56px) + 7px) 12px auto !important;
      z-index: 13000 !important;
      width: auto !important;
      min-width: 0 !important;
      max-width: none !important;
      height: 43px !important;
      background: #0e2166 !important;
    }

    #signed-in .app-topbar .sfera-section-bar__search-toggle {
      display: inline-grid !important;
    }

    #signed-in .app-topbar :where(.sfera-section-bar, .sfera-notes-topbar, .sfera-changelog-topbar):not(.is-search-open)
      :where(.sfera-section-bar__search, .sfera-notes-topbar__search, .sfera-changelog-topbar__search) {
      display: none !important;
    }
  }

  @media (max-width: 600px) {
    #signed-in.app-shell:has(#directory:not([hidden])) {
      padding-top: var(--sfera-topbar-height, 56px) !important;
    }

    #signed-in:has(#directory:not([hidden])) .app-topbar {
      flex-wrap: nowrap !important;
      height: var(--sfera-topbar-height, 56px) !important;
      min-height: var(--sfera-topbar-height, 56px) !important;
      padding-bottom: 0 !important;
    }

    #signed-in:has(#directory:not([hidden])) .app-topbar .sfera-leads-topbar {
      order: 2 !important;
      flex: 1 1 auto !important;
      width: auto !important;
    }

    #signed-in:has(#directory:not([hidden])) #app-navigation-drawer.app-drawer {
      top: var(--sfera-topbar-height, 56px) !important;
    }
  }

  @media (max-width: 420px) {
    #signed-in .app-topbar :where(
      .today-studio-topbar__title, .sfera-section-bar--recurrences .sfera-section-bar__title,
      .sfera-projects-topbar__title, .lead-directory-title
    ) {
      display: none !important;
    }
  }
}
