/* ─── Barra superior ─────────────────────────────────────────────────────── */

.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  background: var(--card);
  border-bottom: none;
}

.topbar-inner {
  max-width: 100%;
  margin: 0 auto;
  padding: 14px 18px;
  display: flex;
  align-items: center;
}

/* ─── Botó de menú mòbil ─────────────────────────────────────────────────── */

.mobile-menu-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-1h);
  width: 58px;
  min-height: 58px;
  height: 58px;
  padding: 8px 8px;
  border-radius: var(--radius-lg);
  order: -1;
  border: 0;
  background: transparent;
  box-shadow: none;
  cursor: pointer;
  color: var(--muted);
}

/* Badge agregat global del hamburger (PRODUCT.md §3.1f): amb cerca global activa,
   la suma de tots els badges grocs interns del drawer (que no es veuen amb el
   menú plegat). Buit (`:empty`) → ocult en repòs.
   Presentació alineada amb `.nav-trigger .nav-count-badge` (nav-megamenu.css):
   fons blau del botó com a trigger actiu + badge absolut centrat straddling la
   vora inferior (margin-top --space-1, translate -13px, vora 2px card). */
.mobile-menu-btn:has(.mobile-menu-btn__badge:not(:empty)) {
  position: relative;
  z-index: 51;
  background: var(--color-primary);
  color: var(--card);
  border: 0;
  box-shadow: none;
}

.mobile-menu-btn__badge {
  position: absolute;
  left: 50%;
  top: 100%;
  margin-top: var(--space-1);
  margin-left: 0;
  transform: translate(-50%, -13px);
  box-sizing: border-box;
  border: 2px solid var(--card);
  background: var(--color-primary);
  pointer-events: none;
}

.mobile-menu-btn svg {
  width: 24px;
  height: 24px;
  stroke: currentColor;
  stroke-width: 2;
  fill: none;
}

.mobile-menu-btn:hover {
  background: var(--filter-option-hover-bg);
  border: 1px solid var(--actionable-border);
  color: var(--text-strong);
}

/* Amb badge actiu, manté el fons blau del trigger (no el hover gris genèric). */
.mobile-menu-btn:has(.mobile-menu-btn__badge:not(:empty)):hover {
  background: var(--color-primary);
  color: var(--card);
  border: 0;
}

@media (hover: hover) and (pointer: fine) {
  .mobile-menu-btn:has(.mobile-menu-btn__badge:not(:empty)):hover {
    background: var(--color-primary-solid-70);
  }

  .mobile-menu-btn:has(.mobile-menu-btn__badge:not(:empty)):hover .mobile-menu-btn__badge {
    background: var(--color-primary-solid-70);
  }
}

.mobile-menu-label {
  font-family: var(--font-ui);
  font-size: var(--font-sm);
  font-weight: 800;
  letter-spacing: 0.1px;
  line-height: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

/* ─── Brand / logotip unificat ───────────────────────────────────────────── */

.brand {
  display: flex;
  align-items: center;
  min-width: 220px;
}

/* Header: logo 56×56 centrat verticalment respecte del wordmark */
.topbar .brand {
  align-items: center;
}

.topbar .logo {
  width: 56px;
  height: 56px;
}

.brand--top-static {
  display: none;
}

/* img.logo rep el logotip unificat via viewport.js (SARDAMANIA_DETAILEDICON_DATA_URI) */
.logo {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  position: relative;
  flex: 0 0 auto;
}

.logo::after {
  content: "";
  position: absolute;
  inset: 3px;
  border-radius: var(--radius-md);
  border: none;
  pointer-events: none;
}

.logo svg {
  width: 24px;
  height: 24px;
  fill: currentColor;
  stroke: currentColor;
}

.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.05;
}

.brand-title {
  font-family: var(--font-ui);
  font-weight: 900;
  font-size: var(--font-4xl);
  letter-spacing: 0.2px;
}

.brand-sub {
  margin-top: 2px;
  color: var(--muted);
  font-size: var(--font-base);
  font-weight: 600;
}

.mobile-nav-header .brand {
  min-width: auto;
}

/* Drawer mòbil: wordmark una mica més compacte (topbar/footer sense canvis) */
.mobile-nav-header .brand-title {
  font-size: var(--font-3xl);
}

.mobile-nav-header .brand-sub {
  font-size: var(--font-sm);
}

/* ─── Cerca global (topbar) ──────────────────────────────────────────────── */

.topbar-search-field {
  flex: 0 1 352px;
  max-width: 352px;
  margin-left: auto;
  width: 100%;
  min-width: 0;
}

/* ─── Autenticació / botons ──────────────────────────────────────────────── */

.auth {
  display: flex;
  align-items: center;
  gap: var(--space-2, 8px);
  margin-left: 12px;
}

/* Compte de l'usuari logat: avatar amb inicial + nom, integrat amb la topbar. */
.auth-account {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.auth-avatar {
  flex: 0 0 auto;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--color-primary);
  color: var(--card);
  font-family: var(--font-ui);
  font-weight: 800;
  font-size: var(--font-sm);
  line-height: 1;
  text-transform: uppercase;
  user-select: none;
}

.auth-name {
  font-family: var(--font-ui);
  font-size: var(--font-sm);
  font-weight: 700;
  color: var(--text-strong);
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.auth-logout {
  font-family: var(--font-ui);
  font-size: var(--font-sm);
  font-weight: 700;
  color: var(--muted);
  text-decoration: none;
  padding: var(--ctrl-pad-y) 10px;
  border-radius: var(--ctrl-radius);
  transition: background 0.15s ease, color 0.15s ease;
}

.auth-logout:hover {
  background: var(--ui-overlay-hover);
  color: var(--text-strong);
}

/* ── Distintius de rol (compartits amb el back-office; vegeu partials/role-visual.php) ── */
.role-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-family: var(--font-ui);
  font-weight: 800;
  font-size: 11px;
  letter-spacing: 0.02em;
  color: var(--role, #6b7280);
  background: color-mix(in srgb, var(--role, #6b7280) 12%, #fff);
  border: 1px solid color-mix(in srgb, var(--role, #6b7280) 35%, #fff);
  border-radius: var(--radius-full, 999px);
  padding: 1px 8px;
  white-space: nowrap;
}

.role-glyph {
  font-size: 10px;
}

.role-avatar {
  position: relative;
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: #fff;
  background: var(--role, #6b7280);
  font-family: var(--font-ui);
  font-weight: 900;
  flex: 0 0 auto;
}

.role-avatar__glyph {
  position: absolute;
  right: -3px;
  bottom: -3px;
  width: 16px;
  height: 16px;
  border-radius: 999px;
  background: #fff;
  color: var(--role, #6b7280);
  display: grid;
  place-items: center;
  font-size: 10px;
  border: 1.5px solid var(--role, #6b7280);
}

.expiry-chip {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-family: var(--font-ui);
  font-weight: 800;
  font-size: 11px;
  color: #92400e;
  background: #fef3c7;
  border: 1px solid #fde68a;
  border-radius: var(--radius-full, 999px);
  padding: 1px 7px;
  white-space: nowrap;
}

.expiry-chip.is-expired {
  color: #b91c1c;
  background: #fef2f2;
  border-color: #fecaca;
}

.auth-name-wrap {
  display: inline-flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  min-width: 0;
}

/*
 * Mateixa família visual que .today-btn al sidebar d'agenda (filtres):
 * lavat primari, vora --actionable-line, radi i alçada de control.
 */
.sidebar-pill-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--ctrl-min-height);
  padding: var(--ctrl-pad-y) 14px;
  border-radius: var(--ctrl-radius);
  border: 1px solid var(--actionable-line);
  background: var(--actionable-wash);
  /* color: model secundari a controls.css (alineat amb .programa-col-toggle) */
  font-family: var(--font-ui);
  font-weight: var(--ctrl-font-weight);
  font-size: var(--ctrl-font-size);
  letter-spacing: var(--ctrl-letter-spacing);
  cursor: pointer;
  box-sizing: border-box;
  white-space: nowrap;
  box-shadow: none;
  text-decoration: none;
  transition: var(--ctrl-transition);
}

.sidebar-pill-btn:hover {
  border-color: var(--actionable-border);
}

.sidebar-pill-btn:focus {
  outline: none;
}


.btn {
  border: 1px solid var(--actionable-line);
  background: var(--actionable-wash);
  /* color: model secundari a controls.css (alineat amb .programa-col-toggle) */
  padding: var(--ctrl-pad-y) 14px;
  min-height: var(--ctrl-min-height);
  border-radius: var(--ctrl-radius);
  font-size: var(--ctrl-font-size);
  font-weight: var(--ctrl-font-weight);
  font-family: var(--font-ui);
  letter-spacing: var(--ctrl-letter-spacing);
  cursor: pointer;
  transition:
    transform 0.06s ease,
    border-color 0.12s ease;
  white-space: nowrap;
  box-sizing: border-box;
}

.btn.btn-ghost {
  background: var(--actionable-wash);
}

.btn:hover {
  border-color: var(--actionable-border);
}

.btn:active {
  transform: translateY(1px);
}

.btn:focus {
  outline: none;
}

.btn-primary {
  background: var(--color-primary-strong);
  border-color: var(--ui-overlay-subtle);
  color: var(--card);
}

@media (hover: hover) and (pointer: fine) {
  .btn-primary:hover {
    filter: brightness(1.07);
  }
}

/* ─── Narrow (≤ 980 px) ───────────────────────────────────────────────────── */

@media (max-width: 980px) {
  .brand {
    min-width: unset;
  }
  .topbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    width: 100%;
  }
  /* El títol/subtítol de secció s'ha retirat de la capçalera (el context viu al
     fil d'Ariadna de .results-header). La cerca global manté l'amplada fixa
     d'escriptori (352px), alineada a la dreta de la columna lliure. */
  .topbar-inner {
    position: relative;
    display: grid;
    grid-template-columns: auto 1fr;
    column-gap: 12px;
  }
  /* El botó d'instal·lar reserva columna només quan la instal·lació s'ofereix.
   * La decisió es pren pre-paint (html.pwa-install-ready), sense forat permanent
   * ni salt de layout a la càrrega. */
  html.pwa-install-ready .topbar-inner {
    grid-template-columns: auto auto 1fr;
    column-gap: 6px;
  }
  .mobile-install-btn {
    display: none;
    color: var(--color-detailedicon);
  }
  html.pwa-install-ready .mobile-install-btn {
    display: flex;
    width: auto;
    min-width: 0;
    padding: 6px 3px;
    gap: 2px;
  }
  .mobile-install-btn .mobile-menu-label {
    max-width: none;
    overflow: visible;
    text-overflow: clip;
  }
  html.pwa-install-ready .mobile-install-btn:hover,
  html.pwa-install-ready .mobile-install-btn:focus-visible {
    background: color-mix(in srgb, rgb(var(--color-detailedicon-rgb)) 12%, var(--card));
    border: 1px solid color-mix(in srgb, var(--color-detailedicon) 35%, var(--card));
    color: var(--color-detailedicon);
  }
  .auth {
    display: none;
  }
  .topbar .brand--top-link {
    display: none;
  }
}

/* ─── Molt estret (≤ 520 px): topbar compactada per guanyar zona útil.
 * La cerca global es manté visible (substitueix l'antic títol de secció).
 * L'alçada real es mesura via JS (viewport.js) i s'exposa a --topbar-height;
 * --topbar-chrome-top suma el desbordament del badge agregat del menú (si n'hi ha).
 * Consumeix .page padding-top i els elements sticky (filtres). */

@media (max-width: 520px) {
  /* Alçada topbar suficient per icona + etiqueta «Menú» sense retallats.
   * No es compença contra .main{padding-top}: el separador amb la card roman com està (layout ≤520px). */
  .topbar-inner {
    padding: 11px 8px;
    column-gap: 12px;
    align-items: center;
  }
  html.pwa-install-ready .topbar-inner {
    column-gap: 4px;
  }
  .mobile-menu-btn:not(.mobile-install-btn) {
    width: 44px;
    min-height: 50px;
    height: auto;
    padding: 4px 2px;
    gap: 2px;
    border-radius: var(--radius-md);
  }
  .mobile-menu-btn svg {
    width: 20px;
    height: 20px;
  }
  .mobile-menu-label {
    font-size: var(--font-xs);
  }
  html.pwa-install-ready .mobile-install-btn {
    width: auto;
    min-width: 0;
    min-height: 50px;
    height: auto;
    padding: 4px 1px;
    gap: 2px;
    border-radius: var(--radius-md);
  }
  .logo {
    width: 36px;
    height: 36px;
  }
  .logo::after {
    inset: 2px;
    border-width: 2px;
  }
  .logo svg {
    width: 20px;
    height: 20px;
  }
}

/* ─── Ample (≥ 981 px) ───────────────────────────────────────────────────── */

@media (min-width: 981px) {
  .mobile-menu-btn {
    display: none;
  }
  .brand--top-link {
    display: none;
  }
  .brand--top-static {
    display: flex;
  }
}

/* Escriptori mitjà (981px–1400px): només el logo al topbar per alleugerir la capçalera.
   A partir de 1401px es mostra de nou títol i lema; a ≤980px el comportament és el de sempre (drawer amb marca completa). */
@media (min-width: 981px) and (max-width: 1400px) {
  .topbar .brand--top-static {
    min-width: auto;
  }

  .topbar .brand--top-static .brand-text {
    display: none;
  }
}
