/* Layer: feature-specific
   Scope: reusable system bar, account actions and header notifications.
   Used by: REALM/assets/atoms/shell/header.html (workspace-header region).
   Reegl CSS owns the shared palette, fluid scale and theme profiles. */

/* Header defaults stay local; the host can override these semantic roles. */
.rx-workspace-header {
  --ui-ready: color-mix(in srgb, var(--c1) 75%, var(--text));
  --ui-wait: color-mix(in srgb, var(--c-warn) 75%, var(--text));
  --ui-off: color-mix(in srgb, var(--c-danger) 75%, var(--text));
  --ui-muted: var(--text-muted);
  --ui-hover: var(--surface-2);
  --ui-ready-soft: color-mix(in srgb, var(--ui-ready) 12%, var(--bg));
}

/* Workspace header: markup, menu, feedback and view carriers form one unit. */
.rx-workspace-header {
  position: sticky;
  inset-block-start: 0;
  z-index: 5;
  align-self: start;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto auto;
  grid-template-areas: "brand items controls preferences";
  align-items: center;
  gap: var(--uspxs);
  padding-block: calc(var(--usp2xs) / 2);
  padding-inline: max(var(--usps), env(safe-area-inset-left)) max(var(--usps), env(safe-area-inset-right));
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

.rx-header-brand,
.rx-header-user summary {
  display: flex;
  align-items: center;
  gap: var(--uspxs);
}

.rx-header-brand {
  grid-area: brand;
  font-size: var(--ust0);
  font-weight: 600;
  letter-spacing: -0.03em;
}

.rx-header-brand b {
  font-weight: 400;
  color: inherit;
}

.rx-workspace-header .rx-header-brand-name {
  display: inline-flex;
  align-items: center;
  gap: var(--usp2xs);
  min-block-size: 44px;
  padding-inline: var(--usp2xs);
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-muted);
  text-decoration: none;
  transition: background-color 180ms ease, color 180ms ease;
}

.rx-header-brand-name[href]:is(:hover, :focus-visible) {
  background: var(--surface-2);
  color: var(--text);
}

.rx-header-brand-name[href]:focus-visible {
  outline: 2px solid var(--ring);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

.rx-header-mark,
.rx-header-avatar {
  display: grid;
  place-items: center;
  flex: none;
  inline-size: var(--uspl);
  block-size: var(--uspl);
  font-weight: 700;
  line-height: 1;
}

.rx-header-mark {
  display: block;
  inline-size: calc(var(--uspl) * 0.625);
  stroke: none;
}

.rx-header-controls {
  --frame-signal: var(--ui-off);
  grid-area: controls;
  justify-self: end;
  display: flex;
  overflow: hidden;
  border: 1px solid var(--frame-signal);
  border-radius: var(--radius-lg);
  transition: border-color 180ms ease;
}

/* Registered fragments occupy their own compact header region. */
.rx-systembar-items {
  grid-area: items;
  display: flex;
  align-items: center;
  justify-self: end;
  gap: var(--usp2xs);
  min-inline-size: 0;
}

.rx-systembar-items:empty { display: none; }

.rx-systembar-icon {
  inline-size: 44px;
  min-inline-size: 44px;
  min-block-size: 44px;
  padding: 0;
  color: var(--text-muted);
}

.rx-systembar-icon svg {
  inline-size: var(--uspm);
  block-size: var(--uspm);
}

.rx-systembar-icon:disabled {
  background: transparent;
  opacity: 1;
  pointer-events: auto;
}

.rx-workspace-header > .rx-header-preferences { grid-area: preferences; }

.rx-systembar-menu {
  position: relative;
  margin: 0;
  padding: 0;
  border: 0;
}

.rx-systembar-menu > summary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-inline-size: 44px;
  min-block-size: 44px;
  padding: 0 var(--uspxs);
  border-radius: var(--radius);
  color: var(--text-muted);
  cursor: pointer;
  font-size: var(--ust-1);
  font-weight: 600;
  list-style: none;
  transition: background-color 180ms ease, color 180ms ease;
}

.rx-systembar-menu > summary::before,
.rx-systembar-menu > summary::-webkit-details-marker { content: none; display: none; }

.rx-systembar-menu[open] > summary,
.rx-systembar-menu > summary:hover {
  background: var(--surface-2);
  color: var(--text);
}

.rx-systembar-menu > summary:focus-visible,
.rx-systembar-menu-button:focus-visible {
  outline: 2px solid var(--ring);
  outline-offset: 2px;
}

.rx-systembar-menu-panel {
  position: absolute;
  z-index: 2;
  inset-block-start: calc(100% + var(--uspxs));
  inset-inline-end: 0;
  display: grid;
  gap: var(--usp2xs);
  inline-size: min(18rem, calc(100vw - var(--uspm) * 2));
  margin: 0;
  padding: var(--usp2xs);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
}

.rx-systembar-menu-button {
  display: flex;
  align-items: center;
  justify-content: start;
  min-block-size: 44px;
  padding: var(--uspxs) var(--usps);
  border-radius: var(--radius);
  font-size: var(--ust0);
  text-align: start;
}

.rx-systembar-menu-note {
  margin: 0;
  padding: var(--usp2xs) var(--uspxs);
  color: var(--text-muted);
  font-size: var(--ust-1);
}

/* The frame reads only its two existing child states. */
.rx-header-controls:has(> #rx-phantom-control[data-state="unknown"]) {
  --frame-signal: var(--ui-muted);
}

.rx-header-controls:has(> #rx-phantom-control:is([data-state="ready"], [data-state="pending"])) {
  --frame-signal: var(--ui-wait);
}

.rx-header-controls:has(> #rx-phantom-control[data-state="ready"]):has(> #rx-bridge-control[data-state="ready"]) {
  --frame-signal: var(--ui-ready);
}

.rx-header-control {
  --control-signal: var(--ui-off);
  display: flex;
  align-items: center;
  min-inline-size: 0;
}

.rx-header-control + .rx-header-control {
  border-inline-start: 1px solid var(--border);
}

/* All header actions keep the same 44px touch floor; glyphs use the fluid scale. */
.rx-header-status-action,
.rx-header-user summary {
  inline-size: 44px;
  min-inline-size: 44px;
  min-block-size: 44px;
  padding: 0;
  justify-content: center;
  transition: background-color 180ms ease;
}

.rx-header-status-action {
  display: inline-flex;
  align-items: center;
  border: 0;
  border-radius: 0;
  color: var(--control-signal);
}

.rx-header-status-action:disabled {
  opacity: 1;
}

.rx-header-status-action:not(:disabled):hover,
.rx-header-user summary:hover,
.rx-header-user[open] summary,
.rx-header-user-menu .btn:not(:disabled):hover {
  background: var(--ui-hover);
}

.rx-header-status-action:focus-visible {
  outline: 2px solid var(--ring);
  outline-offset: -4px;
}

.rx-header-user summary:focus-visible,
.rx-header-user-menu .btn:focus-visible {
  outline: 2px solid var(--ring);
  outline-offset: 2px;
}

.rx-header-status-action svg {
  flex: none;
  inline-size: var(--uspm);
  block-size: var(--uspm);
}

.rx-header-status-action[aria-busy="true"] svg {
  animation: rx-header-connection-pulse 1s ease-in-out infinite alternate;
}

.rx-header-control[data-state="ready"] {
  --control-signal: var(--ui-ready);
}

.rx-header-control[data-state="unknown"],
#rx-bridge-control[data-state="closed"]:has(> :disabled) {
  --control-signal: var(--ui-muted);
}

.rx-header-control:is([data-state="pending"], [data-state="connecting"], [data-state="closing"]) {
  --control-signal: var(--ui-wait);
}

.rx-header-ghost-awake {
  display: none;
  fill: currentColor;
}

.rx-header-control[data-state="ready"] .rx-header-ghost-body {
  fill: var(--ui-ready-soft);
}

.rx-header-control[data-state="ready"] .rx-header-ghost-awake {
  display: block;
}

.rx-header-control[data-state="ready"] .rx-header-ghost-asleep {
  display: none;
}

.rx-header-link-left {
  transform: translateX(-5%);
}

.rx-header-link-right {
  transform: translateX(5%);
}

.rx-header-link-left,
.rx-header-link-right {
  transition: transform 180ms ease;
}

.rx-header-link-join {
  opacity: 0;
  transition: opacity 180ms ease;
}

.rx-header-control[data-state="ready"] :is(.rx-header-link-left, .rx-header-link-right) {
  transform: none;
}

.rx-header-control[data-state="ready"] .rx-header-link-join {
  opacity: 1;
}

.rx-header-user {
  position: relative;
  min-inline-size: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

.rx-header-user summary {
  border-radius: var(--radius);
  cursor: pointer;
  list-style: none;
  color: var(--text);
  font-weight: 400;
}

.rx-header-user summary::before {
  content: none;
}

.rx-header-user summary::-webkit-details-marker {
  display: none;
}

.rx-header-avatar {
  border-radius: var(--radius-full);
  overflow: hidden;
  background: var(--avatar-bg, var(--surface-2));
  color: var(--avatar-fg, var(--text));
  font-size: var(--ust2);
}

.rx-header-avatar img {
  inline-size: 100%;
  block-size: 100%;
  object-fit: cover;
}

.rx-header-user-menu {
  position: absolute;
  z-index: 2;
  inset-block-start: calc(100% + var(--uspxs));
  inset-inline-end: 0;
  inline-size: max-content;
  max-inline-size: calc(100vw - var(--usps) * 2);
  margin: 0;
  padding: var(--usp2xs);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
}

.rx-header-user-menu .btn {
  display: flex;
  justify-content: start;
  inline-size: 100%;
  min-block-size: 44px;
  padding: var(--uspxs) var(--usps);
  border-radius: var(--radius);
  font-size: var(--ust0);
  text-align: start;
  transition: background-color 180ms ease;
}

/* Browser-local Bridge preference stays readable within the compact user menu. */
.rx-header-user-menu .rx-bridge-auto-connect {
  align-items: center;
  justify-content: space-between;
  gap: var(--usps);
}

.rx-bridge-auto-connect > [data-lang-text] {
  min-inline-size: 0;
  overflow-wrap: anywhere;
}

.rx-bridge-auto-state {
  flex: none;
  padding: var(--usp2xs) var(--uspxs);
  border-radius: var(--radius-full);
  background: var(--surface-2);
  color: var(--ui-muted);
  font-size: var(--ust-1);
  font-weight: 600;
  line-height: 1;
}

.rx-bridge-auto-connect[aria-checked="true"] .rx-bridge-auto-state {
  background: var(--ui-ready-soft);
  color: var(--ui-ready);
}

.rx-header-menu-greeting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--usps);
  margin: 0 0 var(--usp2xs);
  padding: var(--usp2xs) var(--uspxs) var(--usp2xs) var(--usps);
  border-bottom: 1px solid var(--border);
  font-size: var(--ust0);
  overflow-wrap: anywhere;
}

.rx-header-menu-greeting strong { min-inline-size: 0; }
.rx-header-menu-greeting .rx-header-icon { flex-shrink: 0; }

.rx-header-notifications {
  position: absolute;
  inset-block-start: 100%;
  inset-inline: 0;
  z-index: 1;
  display: grid;
  gap: var(--uspxs);
  inline-size: min(100%, 40rem);
  max-block-size: calc(100dvh - 100% - var(--usps));
  margin-inline: auto;
  padding: var(--usps);
  overflow-y: auto;
  overscroll-behavior: contain;
}

.rx-header-notifications:empty {
  display: none;
}

/* The header keeps ownership when the host lends a modal presentation slot. */
.rx-header-notifications[data-popup-mounted] {
  position: static;
  inline-size: 100%;
  max-block-size: 28dvh;
  padding: 0;
}

.rx-header-notice {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: var(--uspxs);
  margin: 0;
  padding: var(--uspxs) var(--usps);
  font-size: var(--ust0);
  box-shadow: var(--shadow);
}

.rx-header-notice-icon {
  display: grid;
  place-items: center;
  inline-size: var(--uspm);
  block-size: var(--uspm);
  border: 1px solid currentColor;
  border-radius: var(--radius-full);
  font-weight: 700;
  line-height: 1;
}

.rx-header-notice-content {
  min-inline-size: 0;
  overflow-wrap: anywhere;
  text-align: center;
}

.rx-header-notice-content > :first-child {
  margin-block-start: 0;
}

.rx-header-notice-content > :last-child {
  margin-block-end: 0;
}

.rx-header-notice-close {
  align-self: start;
  min-inline-size: 44px;
  min-block-size: 44px;
  padding: 0;
  border: 0;
  color: inherit;
  font-size: var(--ust2);
  line-height: 1;
}

.rx-header-notice-close:hover {
  background: color-mix(in srgb, currentColor 10%, transparent);
}

.rx-header-notice-close:focus-visible {
  outline: 2px solid currentColor;
  outline-offset: 2px;
}

@keyframes rx-header-connection-pulse {
  to { opacity: 0.55; }
}

@media (prefers-reduced-motion: reduce) {
  .rx-header-status-action[aria-busy="true"] svg {
    animation: none;
  }

  .rx-header-controls,
  .rx-workspace-header .rx-header-brand-name,
  .rx-header-status-action,
  .rx-header-user summary,
  .rx-header-user-menu .btn,
  .rx-header-link-left,
  .rx-header-link-right,
  .rx-header-link-join {
    transition: none;
  }
}

@media (max-width: 48rem) {
  .rx-header-brand-word {
    display: none;
  }
}

@media (max-width: 30rem) {
  .rx-workspace-header {
    grid-template-areas: "brand . controls preferences" "items items items items";
  }

  /* Keep every extension panel inside the viewport as more items follow it. */
  .rx-systembar-items { position: relative; }
  .rx-systembar-menu { position: static; }

  /* Open below both rows so a panel cannot cover another menu's trigger. */
  .rx-workspace-header:has(.rx-systembar-items details) :is(.rx-header-language-menu, .rx-header-user-menu) {
    inset-block-start: calc(100% + 44px + var(--uspxs) * 2);
  }
}
