@layer components {
  :root {
    --global-search-max-width: 36rem;
    --global-search-top: 4rem;
  }

  .global-search-trigger {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.3rem 0.5rem;
    background: transparent;
    border: 1px solid transparent;
    border-radius: 8px;
    color: inherit;
    font: inherit;
    cursor: pointer;
    line-height: 1;
  }

  .global-search-trigger:hover {
    background: #f3f4f6;
  }

  .global-search-dialog {
    width: min(var(--global-search-max-width), 95vw);
    max-height: min(80vh, calc(100vh - var(--global-search-top) - 2rem));
    inset: 0;
    margin-inline: auto;
    margin-block-start: var(--global-search-top);
    margin-block-end: auto;
    padding: 0;
    border: 1px solid var(--dialog-border-color, lightgray);
    border-radius: 12px;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.18);
    overflow: hidden;
    background: #fff;
  }

  .global-search-dialog::backdrop {
    background-color: rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(2px);
  }

  .global-search-panel {
    display: flex;
    flex-direction: column;
    max-height: inherit;
  }

  .global-search-form {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.65rem 0.9rem;
    border-bottom: 1px solid #e5e7eb;
  }

  .global-search-form-icon {
    --icon-size: 1.25rem;
    color: #6b7280;
    flex-shrink: 0;
  }

  .global-search-input {
    flex: 1 1 auto;
    appearance: none;
    border: 0;
    outline: none;
    background: transparent;
    padding: 0.25rem 0;
    margin: 0;
    font-family: inherit;
    font-size: 1rem;
    color: #111827;
    box-shadow: none;
  }

  .global-search-input::placeholder {
    color: #9ca3af;
    font-style: italic;
  }

  .global-search-close {
    --icon-size: 1.1rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.75rem;
    height: 1.75rem;
    background: transparent;
    border: 0;
    border-radius: 6px;
    color: #6b7280;
    cursor: pointer;
    flex-shrink: 0;
  }

  .global-search-close:hover {
    background: #f3f4f6;
    color: #111827;
  }

  .global-search-results {
    flex: 1 1 auto;
    overflow-y: auto;
    padding: 0 0.9rem 0.9rem;
    min-height: 0;
  }

  /* Collapse the area below the search field until there is something
     to show — i.e. the results div is still empty and no spinner runs. */
  .global-search-results:has(> div:empty):has(> .global-search-spinner.global-search-hidden) {
    display: none;
  }

  .global-search-results.global-search-loading {
    opacity: 0.6;
  }

  .global-search-spinner {
    --icon-size: 1.25rem;
    display: block;
    margin: 0.75rem auto;
    color: #6b7280;
    animation: spin 0.9s linear infinite;
  }

  /* Bumped specificity so .global-search-hidden wins over .icon's display. */
  .global-search-hidden,
  .global-search-spinner.global-search-hidden {
    display: none;
  }

  /* Tabbed result groups — host partial may opt in. */
  .global-search-tabs {
    display: flex;
    gap: 0.1rem;
    border-bottom: 1px solid #e5e7eb;
    margin: 0 -0.9rem 0.6rem;
    padding: 0.5rem 0.9rem 0;
    overflow-x: auto;
    scrollbar-width: none;
    position: sticky;
    top: 0;
    background: #fff;
    z-index: 1;
  }

  .global-search-tabs::-webkit-scrollbar {
    display: none;
  }

  .global-search-tab {
    appearance: none;
    border: 0;
    background: transparent;
    padding: 0.45rem 0.7rem;
    margin-bottom: -1px;
    font-family: inherit;
    font-size: 0.85rem;
    font-weight: 600;
    color: #6b7280;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    white-space: nowrap;
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
  }

  .global-search-tab:hover {
    color: #111827;
  }

  .global-search-tab.active {
    color: #111827;
    border-bottom-color: #f59e0b;
  }

  .global-search-tab-count {
    font-weight: 500;
    color: #9ca3af;
    font-size: 0.75rem;
  }

  .global-search-tabpanel {
    display: none;
  }

  .global-search-tabpanel.active {
    display: block;
  }

  /* Default result item styles — host partial may opt into these classes. */
  .global-search-section {
    margin-top: 0.75rem;
  }

  .global-search-section:first-child {
    margin-top: 0;
  }

  .global-search-section-title {
    position: sticky;
    top: 0;
    background: #fff;
    margin: 0 0 0.4rem;
    padding: 0.3rem 0;
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: #6b7280;
  }

  .global-search-result-list {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
  }

  .global-search-result {
    background: #f3f4f6;
    border-radius: 6px;
    padding: 0.5rem 0.6rem;
  }

  .global-search-result:hover {
    background: #e5e7eb;
  }

  .global-search-result a {
    display: block;
    text-decoration: none;
    color: inherit;
  }

  .global-search-result-title {
    font-size: 0.9rem;
    font-weight: 600;
    color: #111827;
  }

  .global-search-result-url {
    font-size: 0.75rem;
    font-weight: 600;
    color: #4b5563;
    word-break: break-all;
  }

  .global-search-result-date {
    font-size: 0.7rem;
    color: #6b7280;
    margin-top: 0.1rem;
  }

  .global-search-result-content {
    font-size: 0.8rem;
    color: #4b5563;
    margin-top: 0.15rem;
  }

  @media (max-width: 540px) {
    /* Floating card: small inset from the screen edges and the status
       bar / notch, keeping the border + radius from the base rule. */
    .global-search-dialog {
      width: calc(100vw - 1.5rem);
      max-height: calc(100vh - env(safe-area-inset-top, 0px) - 1.5rem);
      margin-block-start: calc(env(safe-area-inset-top, 0px) + 0.75rem);
    }

    .global-search-form {
      padding: 0.55rem 0.7rem;
    }

    .global-search-results {
      padding: 0 0.7rem 0.7rem;
    }

    .global-search-tabs {
      margin: 0 -0.7rem 0.5rem;
      padding: 0.4rem 0.7rem 0;
      gap: 0;
    }

    .global-search-tab {
      padding: 0.4rem 0.3rem;
      font-size: 0.7rem;
      gap: 0.2rem;
    }

    .global-search-tab-count {
      font-size: 0.65rem;
    }
  }
}
