/*
 * Header — extractable sticky product bar (Tutor player).
 * Tokens expected from host shell.css :root (--header-*, --touch, --ink, …).
 *
 * Mount host: shell uses <div id="header-host">. Sticky only works if the
 * sticky box is a child of the scrolling page — not trapped in a short wrapper.
 * `display: contents` makes header.top a flex child of .page for real sticky.
 */
#header-host {
  display: contents;
}

    /*
     * Sticky product bar — L | C | R as ONE grid row.
     * Same vertical band for breadcrumb, dots, and actions (no absolute
     * vertical 50% that drifts when safe-area top ≠ bottom pad).
     * Inline pad = --page-pad-x so lead left edge = content left edge.
     */
    header.top {
      --chrome-inline: max(var(--page-pad-x), var(--safe-left));
      --chrome-inline-end: max(var(--page-pad-x), var(--safe-right));
      position: sticky;
      top: 0;
      z-index: var(--header-z);
      display: grid;
      grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
      grid-template-areas: "lead dots actions";
      align-items: center;
      column-gap: 0.35rem;
      flex-shrink: 0;
      min-height: calc(var(--header-h) + var(--safe-top));
      /* Bleed glass to page edges; pad back so L/R match .page content */
      margin-inline: calc(-1 * var(--chrome-inline))
        calc(-1 * var(--chrome-inline-end));
      width: auto;
      max-width: none;
      padding:
        max(var(--header-pad-y), var(--safe-top))
        var(--chrome-inline-end)
        var(--header-pad-y)
        var(--chrome-inline);
      background: var(--glass-bg, rgb(255 253 248 / 0.4));
      backdrop-filter: var(--glass-filter, blur(18px) saturate(1.15));
      -webkit-backdrop-filter: var(--glass-filter, blur(18px) saturate(1.15));
      border-bottom: 1px solid var(--glass-border, rgb(26 26 26 / 0.1));
      box-shadow: var(--glass-inset, 0 1px 0 rgb(255 255 255 / 0.35) inset);
    }

    .top-leading {
      grid-area: lead;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: flex-start;
      /* Tight lead: fixed icon slot + badge (badge never jumps) */
      gap: 0.3rem;
      flex-wrap: nowrap;
      min-width: 0;
      max-width: 100%;
      justify-self: stretch;
      /* No extra pad — first pixel = content left edge */
      padding: 0;
      margin: 0;
      overflow: hidden;
      position: relative;
      z-index: 2;
      pointer-events: none;
    }
    .top-leading > * {
      pointer-events: auto;
    }
    /*
     * Permanent lead slot (Library icon OR back arrow).
     * Always same width so “Tutor · Title” stays put across modes.
     */
    .top-lead-slot {
      position: relative;
      flex: 0 0 var(--header-icon);
      width: var(--header-icon);
      min-width: var(--header-icon);
      height: var(--header-touch);
      min-height: var(--header-touch);
    }
    .top-actions {
      grid-area: actions;
      display: flex;
      align-items: center;
      justify-content: flex-end;
      gap: 0.15rem;
      min-width: 0;
      width: 100%;
      justify-self: stretch;
      margin: 0;
      padding: 0;
      position: relative;
      z-index: 2;
      pointer-events: none;
    }
    .top-actions > * {
      pointer-events: auto;
    }
    /*
     * ONE action chrome for library + lesson (and loading).
     * Shared language: bare muted icons, ink on hover, no bordered pills.
     * Lesson vs library only toggles which controls exist (dots / Library /
     * back) — never a different button style.
     */
    #btn-reload {
      flex-shrink: 0;
      margin-left: 0;
      order: 10;
    }
    #link-library {
      order: 1;
      margin-left: 0;
    }

    /* Shared chrome: lead icons + right actions (no circles / borders) */
    .top-back,
    .top-lib-lead,
    .top-lead-icon,
    .top-link {
      box-sizing: border-box;
      flex-shrink: 0;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      gap: 0.35rem;
      min-width: var(--header-touch);
      min-height: var(--header-touch);
      height: var(--header-touch);
      margin: 0;
      padding: 0;
      border: 0;
      border-radius: var(--radius-sm);
      background: transparent;
      color: var(--muted);
      text-decoration: none;
      font-weight: 700;
      font-size: var(--text-sm);
      font-family: inherit;
      cursor: pointer;
      white-space: nowrap;
      line-height: 0;
      transition: color 0.15s ease, opacity 0.12s ease;
    }
    .top-back:hover,
    .top-lib-lead:hover,
    .top-lead-icon:hover,
    .top-link:hover {
      color: var(--ink);
      background: transparent;
    }
    .top-back:active,
    .top-lib-lead:active,
    .top-lead-icon:active,
    .top-link:active {
      color: var(--ink);
      opacity: 0.75;
    }
    .top-back:focus-visible,
    .top-lib-lead:focus-visible,
    .top-lead-icon:focus-visible,
    .top-link:focus-visible {
      outline: 2px solid var(--ink);
      outline-offset: 2px;
      background: transparent;
    }
    .top-back[hidden],
    .top-lib-lead[hidden],
    .top-lead-icon[hidden],
    .top-link[hidden] {
      display: none !important;
    }
    /*
     * Focus control — icon only, same bare chrome as library/reload (no outline).
     */
    .top-link--why {
      line-height: 0;
    }
    .top-link--why svg {
      display: block;
      width: var(--header-icon-lg, 18px);
      height: var(--header-icon-lg, 18px);
    }
    header.top[data-mode="library"] .top-link--why,
    header.top[data-mode="loading"] .top-link--why {
      display: none !important;
    }

    /*
     * Lead icons (back OR library): glyph flush left of the slot; 44px hit via
     * ::before. No circle / border — same language on desktop + mobile.
     */
    .top-lead-icon,
    .top-back,
    .top-lib-lead {
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      justify-content: flex-start;
      margin: 0;
      padding: 0;
      width: var(--header-icon);
      min-width: var(--header-icon);
      max-width: var(--header-icon);
      height: var(--header-touch);
      min-height: var(--header-touch);
    }
    .top-lead-icon::before,
    .top-back::before,
    .top-lib-lead::before {
      content: "";
      position: absolute;
      top: 50%;
      left: 50%;
      width: var(--header-touch);
      height: var(--header-touch);
      transform: translate(-50%, -50%);
      pointer-events: auto;
    }
    .top-lead-icon svg,
    .top-back svg,
    .top-lib-lead svg {
      position: relative;
      z-index: 1;
      display: block;
      width: var(--header-icon);
      height: var(--header-icon);
      transform: translateY(0.5px);
      pointer-events: none;
    }
    .top-lead-icon[hidden],
    .top-back[hidden],
    .top-lib-lead[hidden] {
      display: none !important;
    }

    /* Icon-only controls (reload always; library icon always) */
    .top-link--icon {
      width: var(--header-touch);
      padding: 0;
    }
    .top-link--icon svg {
      display: block;
      flex-shrink: 0;
      width: var(--header-icon-lg);
      height: var(--header-icon-lg);
    }

    /* Library: same icon tile; label only on wide screens (still no border) */
    #link-library,
    .top-link--library {
      width: auto;
      min-width: var(--header-touch);
      padding: 0 0.55rem 0 0.35rem;
      gap: 0.35rem;
      line-height: 1;
    }
    #link-library .top-link-icon,
    .top-link--library .top-link-icon {
      display: block;
      flex-shrink: 0;
      width: var(--header-icon-lg, 18px);
      height: var(--header-icon-lg, 18px);
      transform: translateY(-0.5px);
    }
    #link-library .top-link-label,
    .top-link--library .top-link-label {
      display: inline;
      line-height: 1;
      letter-spacing: 0.02em;
    }

    .top-leading .badge {
      align-self: center;
      margin: 0;
      min-width: 0;
      flex: 1 1 auto;
      max-width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      /* Same row height as back/reload so caps line up with icon centers */
      min-height: var(--header-touch);
      display: inline-flex;
      align-items: center;
    }

    .badge {
      font-size: var(--text-xs);
      letter-spacing: 0.08em;
      text-transform: uppercase;
      font-weight: 700;
      color: var(--muted);
      text-align: left;
      line-height: 1;
    }

    /* ═══ Slide dots — grid center column (same vertical band as lead/actions) ═══
     * Non-overlapping tiles: each button’s width = disc + gap (pitch).
     * Tall hit (44px) via tile height. Visual disc is ::after only.
     */
    .dots {
      --dot-vis: 10px;
      --dot-vis-edge: 7px;
      --dot-gap: 0.5rem;
      --dot-pitch: calc(var(--dot-vis) + var(--dot-gap));
      grid-area: dots;
      position: relative;
      left: auto;
      top: auto;
      transform: none;
      z-index: 3;
      display: flex;
      gap: 0;
      align-items: center;
      justify-content: center;
      justify-self: center;
      min-width: 0;
      max-width: min(46vw, 14rem);
      height: var(--header-touch);
      overflow: visible;
      pointer-events: auto;
    }
    .dot {
      box-sizing: border-box;
      position: relative;
      flex: 0 0 auto;
      /* Hit tile: full pitch wide, full touch tall — neighbors meet, no overlap */
      width: var(--dot-pitch);
      height: var(--header-touch);
      margin: 0;
      padding: 0;
      border: 0;
      border-radius: 0;
      background: transparent;
      cursor: pointer;
      /* Don’t scale the button (would re-overlap hit areas) */
      transition: none;
    }
    .dot[hidden] {
      display: none !important;
    }
    /* Visual disc — centered in the tile; only this scales/colors */
    .dot::after {
      content: "";
      position: absolute;
      top: 50%;
      left: 50%;
      width: var(--dot-vis);
      height: var(--dot-vis);
      border-radius: var(--radius-pill);
      background: var(--dot-idle);
      transform: translate(-50%, -50%);
      /* transform/opacity only — avoid layout thrash on width/height */
      transition: transform 0.2s cubic-bezier(0.25, 1, 0.5, 1),
        background 0.2s ease, opacity 0.15s ease;
      pointer-events: none;
    }
    .dot[aria-current="true"]::after {
      background: var(--ink);
      /* Active grows ~10% — still a filled disc, not a pill; tile stays put */
      transform: translate(-50%, -50%) scale(1.12);
    }
    /* Edge of window when more slides exist beyond — smaller “more” cue */
    .dot[data-window-edge="1"]:not([aria-current="true"])::after {
      width: var(--dot-vis-edge);
      height: var(--dot-vis-edge);
      opacity: 0.55;
    }
    .dot:focus-visible {
      outline: 2px solid var(--ink);
      outline-offset: 2px;
      border-radius: var(--radius-sm);
    }
    .dot:focus-visible::after {
      outline: none;
    }

    /*
     * Phone: same icon language as desktop — hide Library text only so the
     * lead + dots keep width. Applies to ALL modes (not lesson-only).
     */
    @media (max-width: 520px) {
      header.top {
        column-gap: 0.2rem;
        /* Keep L | C | R so dots stay in the same vertical band as icons */
        grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
      }
      .top-leading {
        padding: 0;
      }
      .top-actions {
        width: 100%;
        max-width: none;
        justify-self: stretch;
        margin: 0;
        padding: 0;
        gap: 0;
      }
      .top .top-link--icon,
      .top #link-library,
      .top .top-link--library {
        min-width: 2.25rem;
        width: 2.25rem;
        height: var(--header-touch);
        padding: 0;
        gap: 0;
      }
      .top #link-library .top-link-label,
      .top .top-link--library .top-link-label {
        position: absolute;
        width: 1px;
        height: 1px;
        padding: 0;
        margin: -1px;
        overflow: hidden;
        clip: rect(0, 0, 0, 0);
        white-space: nowrap;
        border: 0;
      }
      .dots {
        max-width: min(48vw, 12rem);
      }
    }

/* Library / loading: hide dots; keep center column so L|R rails stay balanced. */
.top[data-mode="library"] .dots,
.top[data-mode="loading"] .dots {
  visibility: hidden;
  pointer-events: none;
  /* Reserve a minimal center track so lead/actions don’t jump width */
  width: 0;
  min-width: 0;
  max-width: 0;
  height: var(--header-touch);
  overflow: hidden;
  margin: 0;
  padding: 0;
}
