/* ============================================================================
 * OAW Workbench — Unified 46px Topbar, Inline Nav & Viewport-Aware Popovers
 * Layer: @layer shell (tokens only; no specificity hacks; >= 11px font floor)
 * ============================================================================ */

@layer shell {
  .oaw-topbar {
    position: sticky; inset-block-start: 0; z-index: 95;
    block-size: var(--masthead-h, 46px); min-block-size: var(--masthead-h, 46px); max-block-size: var(--masthead-h, 46px);
    display: flex; align-items: center; justify-content: space-between; gap: 8px;
    padding-inline: clamp(8px, 1.5vw, 16px);
    background: var(--bg-elevated); color: var(--text-primary); border-block-end: 1px solid var(--border-hairline);
  }
  .oaw-topbar-left { display: flex; align-items: center; gap: 8px; min-inline-size: 0; flex: 1 1 auto; }
  .oaw-topbar-right { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
  .oaw-context-switcher,
  .oaw-pillar-launcher,
  .oaw-user-launcher { position: relative; display: inline-flex; align-items: center; min-inline-size: 0; }
  .oaw-context-switcher { flex: 0 0 auto; }
  .oaw-topbar .va-icon-host { display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; }

  .oaw-wordmark {
    font-family: var(--font-sans); font-size: 14.5px; font-weight: 800; letter-spacing: 0.02em;
    color: var(--text-primary); text-decoration: none; block-size: 32px; padding: 0 6px;
    border-radius: var(--radius-ui); white-space: nowrap; flex-shrink: 0;
    display: inline-flex; align-items: center; gap: 5px;
  }
  .oaw-wordmark:hover { background: var(--bg-sunken); text-decoration: none; }

  .oaw-pill-trigger,
  .oaw-view-menu-btn,
  .oaw-user-btn {
    block-size: 32px; min-block-size: 32px; padding: 0 11px; border-radius: var(--radius-pill);
    border: 1px solid var(--border-subtle); background: var(--bg-canvas); color: var(--text-primary);
    font-family: var(--font-sans); font-size: var(--ts-sm); font-weight: 600;
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    cursor: pointer; white-space: nowrap; min-inline-size: 0;
  }
  .oaw-pill-trigger, .oaw-door-pill { flex: 0 0 auto; max-inline-size: none; }
  #entry-menu-btn, .oaw-view-menu-btn { max-inline-size: 180px; flex: 0 1 auto; }
  .oaw-user-btn { flex-shrink: 0; }
  .oaw-pill-label { white-space: nowrap; overflow: visible; text-overflow: clip; flex-shrink: 0; }
  #entry-menu-label,
  .oaw-view-menu-btn > span:not(.va-icon-host):not(.oaw-pill-chevron) {
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-inline-size: 0;
  }
  .oaw-pill-chevron, .oaw-user-chevron, .oaw-btn-chev { flex-shrink: 0; }
  :is(.oaw-pill-trigger, .oaw-view-menu-btn, .oaw-user-btn)[aria-expanded="true"] :is(.oaw-pill-chevron, .oaw-user-chevron, .oaw-btn-chev) {
    transform: rotate(180deg);
  }

  .oaw-pillar-btn {
    inline-size: 32px; block-size: 32px; min-block-size: 32px; padding: 0;
    border-radius: var(--radius-ui); border: 1px solid var(--border-subtle);
    background: var(--bg-canvas); color: var(--text-secondary);
    display: inline-flex; align-items: center; justify-content: center; cursor: pointer; flex-shrink: 0;
  }
  :is(.oaw-pill-trigger, .oaw-view-menu-btn, .oaw-pillar-btn, .oaw-user-btn):hover {
    border-color: var(--border-ink); color: var(--text-primary); background: var(--bg-sunken);
  }
  :is(.oaw-pill-trigger, .oaw-view-menu-btn, .oaw-pillar-btn, .oaw-user-btn)[aria-expanded="true"] {
    border-color: var(--accent-primary); background: var(--sig-bg); color: var(--text-primary);
  }

  /* Inline View Navigation (>= 980px) & Collapsible Popover (< 980px) */
  .masthead-nav.oaw-nav-links { display: inline-flex; align-items: center; min-inline-size: 0; flex: 0 1 auto; }
  #entry-menu-btn, .oaw-view-menu-btn { display: none; }
  #entry-switcher {
    position: static; inset: auto; margin: 0; inline-size: auto; block-size: auto; overflow: visible;
    display: inline-flex; align-items: center; gap: 4px; flex: none;
    background: transparent; border: none; border-radius: 0; padding: 0; box-shadow: none;
  }
  .oaw-nav-links :is(.entry-btn, .oaw-nav-tab) {
    position: relative; block-size: var(--masthead-h, 46px); padding: 0 9px;
    background: transparent; border: none; border-block-end: none; border-radius: 0;
    font-size: var(--ts-nav); font-weight: 500; color: var(--text-secondary);
    white-space: nowrap; flex: none; display: inline-flex; align-items: center; gap: 4px; text-decoration: none;
  }
  .oaw-nav-links :is(.entry-btn, .oaw-nav-tab):hover { color: var(--text-primary); background: var(--bg-sunken); text-decoration: none; }
  #entry-switcher :is(.entry-btn, .oaw-nav-tab):is([aria-current="page"], [aria-selected="true"], .is-active) {
    color: var(--text-primary); font-weight: 600; background: transparent;
    box-shadow: inset 0 -2px 0 var(--accent-primary);
  }

  @media (min-width: 980px) { #entry-switcher { display: inline-flex; opacity: 1; transform: none; } }
  @media (min-width: 980px) and (max-width: 1199.98px) {
    .oaw-topbar-left { gap: 6px; }
    #entry-switcher { gap: 2px; }
    #entry-switcher :is(.entry-btn, .oaw-nav-tab) { padding-inline: 6px; font-size: var(--ts-sm); }
  }
  @media (max-width: 979.98px) {
    #entry-menu-btn, .oaw-view-menu-btn { display: inline-flex; }
    #entry-switcher:not(:is(:popover-open, .\:popover-open, .is-open, .is-popover-open)) { display: none; }
    #entry-switcher:is(:popover-open, .\:popover-open, .is-open, .is-popover-open) {
      position: fixed; inset-block-start: calc(var(--masthead-h, 46px) + 6px); inset-inline-start: 12px;
      inset-inline-end: auto; inset-block-end: auto; margin: 0; z-index: 160;
      display: flex; flex-direction: column; align-items: stretch; gap: 3px;
      inline-size: min(240px, calc(100dvw - 24px)); max-block-size: calc(100dvh - var(--masthead-h, 46px) - 16px);
      overflow-y: auto; overscroll-behavior: contain; padding: 6px;
      background: var(--bg-elevated); border: 1px solid var(--border-subtle);
      border-radius: var(--radius-card); box-shadow: var(--shadow-popover); color: var(--text-primary);
    }
    #entry-switcher:is(:popover-open, .\:popover-open, .is-open, .is-popover-open) :is(.entry-btn, .oaw-nav-tab) {
      inline-size: 100%; block-size: auto; min-block-size: 38px; padding: 8px 12px;
      justify-content: flex-start; text-align: start; border: 1px solid transparent;
      border-radius: var(--radius-ui); box-shadow: none; font-size: var(--ts-sm);
    }
    #entry-switcher:is(:popover-open, .\:popover-open, .is-open, .is-popover-open) :is(.entry-btn, .oaw-nav-tab):hover { background: var(--bg-sunken); color: var(--text-primary); }
    #entry-switcher:is(:popover-open, .\:popover-open, .is-open, .is-popover-open) :is(.entry-btn, .oaw-nav-tab):is([aria-current="page"], [aria-selected="true"], .is-active) {
      background: var(--sig-bg); border-color: color-mix(in oklab, var(--sig-border) 35%, transparent);
      color: var(--text-primary); font-weight: 600; box-shadow: none;
    }
    #entry-switcher:is(:popover-open, .\:popover-open, .is-open, .is-popover-open) .entry-desk { display: inline; }
    #entry-switcher:is(:popover-open, .\:popover-open, .is-open, .is-popover-open) .entry-tab-compact { display: none; }
  }

  /* Opaque Viewport-Aware Popovers & Anchor Positioning */
  [popover].oaw-popover {
    display: none; z-index: 160; padding: 8px;
    max-inline-size: calc(100dvw - 16px); max-block-size: calc(100dvh - 64px);
    overflow-y: auto; overscroll-behavior: contain;
    background: var(--bg-elevated); color: var(--text-primary);
    border: 1px solid var(--border-subtle); border-radius: var(--radius-card); box-shadow: var(--shadow-popover);
  }
  @supports not selector(:popover-open) {
    [popover].oaw-popover:not(.\:popover-open):not(.is-popover-open) { display: none; }
  }
  [popover].oaw-popover:is(:popover-open, .\:popover-open, .is-open, .is-popover-open) { display: flex; flex-direction: column; gap: 6px; }
  #door-switcher-popover, .oaw-context-popover { inline-size: min(21rem, calc(100dvw - 16px)); }
  #oaw-pillar-popover, .oaw-pillar-popover { inline-size: min(20rem, calc(100dvw - 16px)); }
  #oaw-user-popover, .oaw-user-popover { inline-size: min(17.5rem, calc(100dvw - 16px)); }

  @media (min-width: 768px) {
    @supports (position-area: block-end) {
      #door-switcher-btn, .oaw-pill-trigger { anchor-name: --oaw-door-anchor; }
      .oaw-pillar-btn { anchor-name: --oaw-pillar-anchor; }
      .oaw-user-btn { anchor-name: --oaw-user-anchor; }
      #door-switcher-popover, #oaw-pillar-popover, #oaw-user-popover {
        inset: auto; margin: 0; margin-block-start: 6px;
        position-area: block-end span-inline-end;
        position-try-fallbacks: flip-block, flip-inline, flip-block flip-inline;
      }
      #door-switcher-popover { position-anchor: --oaw-door-anchor; }
      #oaw-pillar-popover { position-anchor: --oaw-pillar-anchor; position-area: block-end span-inline-start; }
      #oaw-user-popover { position-anchor: --oaw-user-anchor; position-area: block-end span-inline-start; }
    }
  }
  @supports not (position-area: block-end) {
    [popover].oaw-popover {
      position: fixed; inset-block-start: calc(var(--masthead-h, 46px) + 6px);
      inset-inline-end: 8px; inset-inline-start: auto; inset-block-end: auto; margin: 0;
    }
    #door-switcher-popover { inset-inline-start: 8px; inset-inline-end: auto; }
  }
  @media (max-width: 767.98px) {
    #door-switcher-popover, #oaw-pillar-popover, #oaw-user-popover {
      position: fixed; position-anchor: none; position-area: none;
      inset-block-start: calc(var(--masthead-h, 46px) + 6px);
      inset-inline-end: 8px; inset-inline-start: auto; inset-block-end: auto; margin: 0;
    }
    #door-switcher-popover { inset-inline-start: 8px; inset-inline-end: auto; }
  }

  /* Popover Contents: Back Row, Kicker, Rows, Theme Grid & Text Size */
  .oaw-pop-stage1-back {
    display: flex; align-items: center; gap: 8px; inline-size: 100%; min-block-size: 36px;
    padding: 6px 10px; border-radius: var(--radius-ui); border: 1px solid var(--border-hairline);
    background: var(--bg-surface); color: var(--text-primary); font-size: var(--ts-sm); font-weight: 600; text-decoration: none;
  }
  .oaw-pop-stage1-back:hover { background: var(--bg-sunken); border-color: var(--accent-primary); text-decoration: none; }
  .oaw-pop-kicker {
    font-family: var(--font-mono); font-size: var(--ts-xs); font-weight: 700;
    text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-muted); padding: 2px 6px;
  }
  .oaw-pop-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 3px; }
  .oaw-pop-sep { border: 0; border-block-start: 1px solid var(--border-hairline); margin: 2px 0; }

  .oaw-pop-row {
    display: flex; align-items: center; gap: 10px; inline-size: 100%; min-block-size: 38px;
    padding: 8px 10px; border-radius: var(--radius-ui); border: 1px solid transparent;
    background: transparent; text-decoration: none; color: var(--text-primary); text-align: start; cursor: pointer;
  }
  .oaw-pop-row:hover { background: var(--bg-sunken); text-decoration: none; }
  .oaw-pop-row:is(.is-active, [aria-current="page"], [aria-selected="true"]) {
    background: var(--sig-bg); border-color: color-mix(in oklab, var(--sig-border) 35%, transparent);
  }
  .oaw-pop-row.is-soon { opacity: 0.72; cursor: default; }
  .oaw-pop-row.is-soon:hover { background: transparent; }

  .oaw-pop-icon-well {
    inline-size: 28px; block-size: 28px; min-inline-size: 28px; flex: 0 0 auto;
    border-radius: var(--radius-ui); background: var(--bg-surface); border: 1px solid var(--border-hairline);
    color: var(--accent-primary); display: inline-flex; align-items: center; justify-content: center;
  }
  .oaw-pop-row:is(.is-active, [aria-current="page"]) .oaw-pop-icon-well {
    border-color: color-mix(in oklab, var(--sig-border) 45%, var(--border-subtle));
  }
  .oaw-pop-row-body { display: flex; flex-direction: column; gap: 2px; min-inline-size: 0; flex: 1 1 auto; }
  .oaw-pop-row-title { font-size: var(--ts-sm); font-weight: 600; color: var(--text-primary); line-height: 1.25; }
  .oaw-pop-row-sub { font-size: var(--ts-xs); color: var(--text-muted); line-height: 1.3; }
  .oaw-pop-badge {
    font-family: var(--font-mono); font-size: var(--ts-xs); font-weight: 600;
    padding: 2px 7px; border-radius: var(--radius-pill); background: var(--bg-sunken);
    color: var(--text-secondary); border: 1px solid var(--border-hairline); white-space: nowrap; flex-shrink: 0;
  }
  .oaw-pop-row:is(.is-active, [aria-current="page"]) .oaw-pop-badge {
    background: var(--bg-surface); color: var(--accent-primary);
    border-color: color-mix(in oklab, var(--sig-border) 45%, var(--border-subtle));
  }
  .oaw-pop-check, .oaw-theme-check { margin-inline-start: auto; flex-shrink: 0; color: var(--accent-primary); }
  .oaw-pop-ext { color: var(--text-muted); font-size: var(--ts-xs); flex-shrink: 0; }

  .oaw-user-section { display: flex; flex-direction: column; gap: 5px; padding: 2px 0; }
  .oaw-theme-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 5px; padding: 2px 4px 4px; }
  .oaw-theme-opt {
    display: inline-flex; align-items: center; gap: 7px; min-block-size: 36px; padding: 6px 9px;
    border-radius: var(--radius-ui); border: 1px solid var(--border-subtle);
    background: var(--bg-surface); color: var(--text-primary); font-size: var(--ts-sm); font-weight: 600; text-align: start; cursor: pointer;
  }
  .oaw-theme-opt:hover { border-color: var(--border-ink); background: var(--bg-sunken); }
  .oaw-theme-opt:is(.is-active, [aria-pressed="true"]) { background: var(--sig-bg); border-color: var(--accent-primary); }
  .oaw-theme-opt:not([aria-pressed="true"]):not(.is-active) .oaw-theme-check { visibility: hidden; }
  .oaw-swatch-dot {
    inline-size: 14px; block-size: 14px; border-radius: var(--radius-pill);
    border: 1.5px solid var(--border-ink); flex-shrink: 0; display: inline-block;
  }
  .oaw-swatch-dot[data-swatch="white"] { background: var(--swatch-white); }
  .oaw-swatch-dot[data-swatch="paper"] { background: var(--swatch-paper); }
  .oaw-swatch-dot[data-swatch="dark-paper"] { background: var(--swatch-dark-paper); }
  .oaw-swatch-dot[data-swatch="dark"] { background: var(--swatch-dark); }

  .oaw-size-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 5px; padding: 2px 4px 4px; }
  :is(.oaw-size-opt, .oaw-size-grid > button) {
    display: inline-flex; align-items: center; justify-content: center; min-block-size: 34px; padding: 5px 6px;
    border-radius: var(--radius-ui); border: 1px solid var(--border-subtle); background: var(--bg-surface);
    color: var(--text-secondary); font-size: var(--ts-xs); font-weight: 600; cursor: pointer; white-space: nowrap;
  }
  :is(.oaw-size-opt, .oaw-size-grid > button):hover { border-color: var(--border-ink); background: var(--bg-sunken); color: var(--text-primary); }
  :is(.oaw-size-opt, .oaw-size-grid > button):is(.is-active, [aria-pressed="true"]) {
    background: var(--sig-bg); border-color: var(--accent-primary); color: var(--accent-primary); font-weight: 700;
  }

  @media (prefers-reduced-motion: no-preference) {
    .oaw-wordmark, .oaw-pill-trigger, .oaw-view-menu-btn, .oaw-pillar-btn, .oaw-user-btn,
    .oaw-theme-opt, .oaw-size-grid > button, .oaw-pop-row {
      transition: background-color 140ms ease, border-color 140ms ease, color 140ms ease;
    }
    .oaw-pill-chevron, .oaw-user-chevron, .oaw-btn-chev { transition: transform 150ms ease; }
    [popover].oaw-popover {
      opacity: 0; transform: translateY(-4px);
      transition: opacity 150ms ease, transform 150ms ease, display 150ms, overlay 150ms;
      transition-behavior: allow-discrete;
    }
    [popover].oaw-popover:is(:popover-open, .\:popover-open, .is-open, .is-popover-open) { opacity: 1; transform: translateY(0); }
    @starting-style {
      [popover].oaw-popover:is(:popover-open, .\:popover-open, .is-open, .is-popover-open) { opacity: 0; transform: translateY(-4px); }
    }
  }

  @media (max-width: 640px) {
    .oaw-user-label, .oaw-user-chevron, .oaw-user-btn .oaw-pill-chevron,
    .oaw-user-btn .oaw-btn-chev, .oaw-user-label + .va-icon-host { display: none; }
    .oaw-user-btn { inline-size: 32px; padding: 0; justify-content: center; }
    .oaw-pop-row, .oaw-theme-opt { min-block-size: 40px; }
  }
  @media (max-width: 520px) {
    .oaw-topbar { padding-inline: 8px; gap: 4px; }
    .oaw-topbar-left, .oaw-topbar-right { gap: 4px; }
    .oaw-wordmark { padding-inline: 4px; }
    .oaw-wordmark-sub, .oaw-pill-trigger > .va-icon-host:first-child { display: none; }
    .oaw-pill-trigger { padding-inline: 8px; font-size: var(--ts-xs); gap: 4px; }
    #entry-menu-btn, .oaw-view-menu-btn { max-width: 108px; padding-inline: 8px; font-size: var(--ts-xs); gap: 4px; }
  }
  @media (max-width: 380px) {
    .oaw-topbar { padding-inline: 6px; }
    #entry-menu-btn, .oaw-view-menu-btn { max-width: 102px; padding-inline: 7px; }
  }
  @media (forced-colors: active) {
    #entry-switcher .entry-btn:is([aria-current="page"], [aria-selected="true"], .is-active) {
      color: Highlight; box-shadow: inset 0 -2px 0 Highlight;
    }
  }
  @media print {
    .oaw-topbar, .oaw-popover { display: none; }
  }
}
