/*
 * read-aloud.css — Oplæsnings-tilstand (window.ReadAloud)
 *
 * Styles the portal-wide read-aloud framework defined in public/js/read-aloud.js:
 *   - the speaker toggle (top-bar slot + modal-header mirror)
 *   - the playback control bar (fixed top-bar overlay OR modal-header host)
 *   - the karaoke word highlight (.ra-word / .ra-word--active)
 *   - the "no audio" red blink on the visible toggle
 *   - long-press selection/callout suppression while mode is on
 *
 * All colours via design tokens (var(--hd-*)); no hex literals (hex-zero gate).
 *
 * Cascade layers (docs/architecture/design-system.md §Fase 1 Foundation):
 *   reset -> tokens -> base -> components -> utilities -> overrides
 * These rules live in @layer components.
 *
 * Layout note: the overlay bar mirrors player-top-bar.css exactly (visual-
 * viewport offset vars + --player-top-bar-height) and sits above the portal's
 * fixed slide-out chrome (z-index 10120 > the karakterark panel's 10113), so it
 * occludes the menu/panel while playing. Inside a top-layer <dialog> the top-bar
 * (and this overlay) is painted over, so the controls are instead hosted in the
 * modal header, on their own wrapped row — see .hd-ra-controls there.
 *
 * Compat: Safari 16.6 / iPadOS 16. Active-word highlight uses box-shadow (not
 * padding) so karaoke never reflows the text. Touch targets >=44px (WCAG 2.5.5)
 * via a transparent ::after hit-area so the visible icon stays discreet.
 */

@layer components {

  /* ==================================================================
   * Top-bar slot — pins the toggle immediately left of the view-name.
   * The view-name has margin-left:auto; giving the slot the same auto
   * collapses the view-name's auto to zero, so toggle + view-name hug
   * together on the right, separated only by the top-bar flex gap.
   * ================================================================== */
  .hd-player-top-bar__ra-slot {
    margin-left: auto;
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
  }

  /* ==================================================================
   * Speaker toggle
   * ================================================================== */
  .hd-read-aloud-toggle {
    position: relative;
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    padding: 0;
    border: 0;
    background: transparent;
    color: var(--hd-text-secondary);  /* muted antique gold when off */
    opacity: 0.7;
    cursor: pointer;
    border-radius: var(--hd-radius-full);
    -webkit-appearance: none;
    appearance: none;
    transition: color var(--hd-motion-fast) var(--hd-ease-ui),
                opacity var(--hd-motion-fast) var(--hd-ease-ui);
  }

  /* Transparent hit-area: 32px visible + 6px each side = 44px (WCAG 2.5.5). */
  .hd-read-aloud-toggle::after {
    content: "";
    position: absolute;
    inset: -6px;
  }

  .hd-read-aloud-toggle svg {
    display: block;
    pointer-events: none;
  }

  /* Active (mode ON) — full accent, no dimming. */
  .hd-read-aloud-toggle[aria-pressed="true"] {
    color: var(--hd-accent-primary);
    opacity: 1;
  }

  .hd-read-aloud-toggle:focus-visible {
    outline: 2px solid var(--hd-focus-ring);
    outline-offset: 2px;
  }

  /* Modal-header mirror — same button, slightly tighter against header edge. */
  .hd-read-aloud-toggle--modal {
    margin-left: var(--hd-space-2);
  }

  /* ------------------------------------------------------------------
   * Dyslexia "Aa" toggle — independent of read-aloud (2026-06-24).
   * Same 44px hit-area + accent-when-on treatment as the speaker toggle.
   * ------------------------------------------------------------------ */
  .hd-dyslexic-toggle {
    position: relative;
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 32px;
    height: 32px;
    padding: 0 var(--hd-space-1);
    border: 0;
    background: transparent;
    color: var(--hd-text-secondary);
    opacity: 0.7;
    cursor: pointer;
    border-radius: var(--hd-radius-full);
    font-family: var(--hd-font-display);
    font-size: var(--hd-text-ui);
    font-weight: var(--hd-weight-bold);
    line-height: var(--hd-leading-icon);
    -webkit-appearance: none;
    appearance: none;
    transition: color var(--hd-motion-fast) var(--hd-ease-ui),
                opacity var(--hd-motion-fast) var(--hd-ease-ui);
  }

  .hd-dyslexic-toggle::after {
    content: "";
    position: absolute;
    inset: -6px;
  }

  .hd-dyslexic-toggle[aria-pressed="true"] {
    color: var(--hd-accent-primary);
    opacity: 1;
  }

  .hd-dyslexic-toggle:focus-visible {
    outline: 2px solid var(--hd-focus-ring);
    outline-offset: 2px;
  }

  .hd-dyslexic-toggle--modal {
    margin-left: var(--hd-space-1);
  }

  /* ------------------------------------------------------------------
   * "No audio" feedback — discreet red blink on the icon glyph only
   * (colour, never layout). Drives whichever toggle is currently visible.
   * ------------------------------------------------------------------ */
  .hd-read-aloud-toggle--no-audio {
    animation: hd-ra-no-audio var(--hd-motion-slow) var(--hd-ease-ui) 2;
  }

  @keyframes hd-ra-no-audio {
    50% { color: var(--hd-accent-danger); }
  }

  /* ==================================================================
   * Playback control bar
   * ================================================================== */

  /* Fixed top-bar overlay (Regler inline + karakterark-panel). Mirrors the
   * top-bar's viewport-tracked box and sits one z-layer above it. */
  .hd-ra-bar {
    position: fixed;
    top: var(--hd-visual-viewport-offset-top, 0px);
    left: var(--hd-visual-viewport-offset-left, 0px);
    right: calc(100vw - var(--hd-visual-viewport-offset-left, 0px) - var(--hd-visual-viewport-width, 100vw));
    height: var(--player-top-bar-height);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--hd-space-2);
    padding: 0 var(--hd-space-3);
    background: var(--hd-surface-1);
    border-bottom: 1px solid var(--hd-border-subtle);
    /* Above the portal's fixed slide-out chrome, not just the top-bar (100): the
     * karakterark panel (#char-sheet-panel, z-index 10113) and its backdrop (10112)
     * host read-aloud regions, and at z-index 101 this overlay was painted BEHIND
     * them, hiding the controls while reading inside the panel. Top-layer <dialog>
     * modals paint above any z-index, so they host the controls in their header
     * instead (see showControls()) — this overlay never competes with them. */
    z-index: 10120;
    box-sizing: border-box;
  }

  /* JS toggles the [hidden] attribute; display:flex would otherwise win. */
  .hd-ra-bar[hidden] {
    display: none;
  }

  /* Controls cluster — used both inside .hd-ra-bar and (modal variant) when
   * appended into a <dialog> header. */
  .hd-ra-controls {
    display: inline-flex;
    align-items: center;
    gap: var(--hd-space-2);
  }

  .hd-ra-controls__btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;   /* touch target (WCAG 2.5.5) */
    min-height: 44px;
    padding: 0 var(--hd-space-2);
    border: 0;
    background: transparent;
    color: var(--hd-accent-primary);
    font-size: var(--hd-text-ui);
    line-height: var(--hd-leading-icon, 1);
    cursor: pointer;
    border-radius: var(--hd-radius-sm);
    -webkit-appearance: none;
    appearance: none;
    transition: color var(--hd-motion-fast) var(--hd-ease-ui),
                transform var(--hd-motion-fast) var(--hd-ease-ui);
  }

  /* SVG control icons (play/pause/stop) — fixed size, pick up the button colour. */
  .hd-ra-controls__btn svg {
    display: block;
    width: 20px;
    height: 20px;
    pointer-events: none;
  }

  /* Press feedback — momentary depress (gated under reduced-motion below). */
  .hd-ra-controls__btn:active {
    transform: scale(0.9);
  }

  .hd-ra-controls__btn:focus-visible {
    outline: 2px solid var(--hd-focus-ring);
    outline-offset: 2px;
  }

  /* Speed reads as a tappable label-pill, not a glyph: accent value (tabular) +
   * a dimmed multiplier sign, in a subtle pill so it stands apart from the icons. */
  .hd-ra-controls__speed {
    gap: 0.08em;
    padding: 0 var(--hd-space-2);
    font-size: var(--hd-text-ui);
    font-variant-numeric: tabular-nums;
    border: 1px solid var(--hd-border-subtle);
    border-radius: var(--hd-radius-full);
  }

  .hd-ra-controls__speed-val {
    color: var(--hd-accent-primary);
    font-weight: var(--hd-weight-semibold);
  }

  .hd-ra-controls__speed-x {
    color: var(--hd-text-secondary);
    opacity: 0.85;
  }

  /* Modal-header variant: the controls live INSIDE the modal header (the fixed
   * top-bar overlay is painted under a top-layer <dialog>, so it can't host them
   * there — see showControls()). On a narrow phone the header's title + close
   * button (+ the mirrored speaker toggle + edition badge) fill the title row, so
   * the control bar must drop to its OWN full-width row rather than overflow off
   * the right edge of the viewport (which hid the speed pill, then every button,
   * on iPhone). The header is allowed to wrap only when it actually hosts the bar
   * (:has(), Safari 16.4+ — already required by index.php's 16.6 baseline). */
  .hd-modal__header:has(.hd-ra-controls),
  .hd-ruler-modal__header:has(.hd-ra-controls),
  .hd-ruler-detail__header:has(.hd-ra-controls) {
    flex-wrap: wrap;
  }

  dialog .hd-ra-controls {
    flex: 1 0 100%;            /* own row, full width, below the title + close */
    justify-content: center;   /* centred — mirrors the inline overlay bar */
    margin-top: var(--hd-space-2);
  }

  dialog .hd-ra-controls .hd-ra-controls__btn {
    min-width: 40px;  /* modal headers are tighter than the 48px top-bar */
  }

  /* ==================================================================
   * Karaoke word highlight
   * ================================================================== */
  .ra-word {
    /* No transition — karaoke advances every frame; a transition would trail. */
    border-radius: var(--hd-radius-sm);
  }

  /* Softer karaoke mark: a gentle accent tint keeps the prose colour readable
   * (text stays --hd-text-primary, not inverted) while a solid accent underline
   * tracks the current word. Both the tint (color-mix, Safari 16.2+) and the
   * underline (inset box-shadow) avoid padding, so highlighting never reflows the
   * text. Transition-free — karaoke advances every frame. */
  .ra-word--active {
    background: color-mix(in srgb, var(--hd-accent-primary) 22%, transparent);
    color: var(--hd-text-primary);
    box-shadow: inset 0 -0.14em 0 var(--hd-accent-primary);
  }

  /* ==================================================================
   * Device-TTS fallback (no cached cloned-voice audio)
   * ================================================================== */

  /* Whole-region "reading now" marker. The device voice always gets this; on
   * platforms that fire word-boundary events the per-word .ra-word--active
   * highlight rides on top, on those that don't (iOS Safari) this is the only
   * cue. Accent left bar via inset shadow (no border/padding → no reflow) + a
   * soft tint, static (no motion). */
  [data-ra-region].ra-region--speaking {
    background: color-mix(in srgb, var(--hd-accent-primary) 14%, transparent);
    box-shadow: inset 4px 0 0 var(--hd-accent-primary);
    border-radius: var(--hd-radius-sm);
  }

  /* ==================================================================
   * Long-press affordance — suppress text selection / iOS callout on
   * readable regions, but ONLY while read-aloud mode is on.
   * ================================================================== */
  body.hd-read-aloud-on [data-ra-region],
  body.hd-read-aloud-on [data-ra-region] * {
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
  }

  /* ==================================================================
   * Reduced motion — no pulse; a short static red tone instead. The JS
   * removes the class after NO_AUDIO_FLASH_MS, so the tone is transient.
   * ================================================================== */
  @media (prefers-reduced-motion: reduce) {
    .hd-read-aloud-toggle {
      transition: none;
    }
    .hd-read-aloud-toggle--no-audio {
      animation: none;
      color: var(--hd-accent-danger);
    }
    .hd-ra-controls__btn {
      transition: none;
    }
    .hd-ra-controls__btn:active {
      transform: none;
    }
  }

} /* end @layer components */

/* ====================================================================
 * Dyslexia-friendly prose face — INDEPENDENT toggle (decoupled from
 * read-aloud 2026-06-24). Driven by body.hd-dyslexic-on (see
 * dyslexia-mode.js), NOT read-aloud mode. Scope = readable prose regions
 * ([data-ra-region], the shared "this is prose" marker). In @layer
 * overrides so it beats component prose-font rules regardless of
 * specificity. Reverts automatically when the class is removed.
 * ==================================================================== */
@layer overrides {
  body.hd-dyslexic-on [data-ra-region],
  body.hd-dyslexic-on [data-ra-region] * {
    font-family: var(--hd-font-dyslexic);
  }
  body.hd-dyslexic-on [data-ra-region] {
    line-height: var(--hd-leading-relaxed);  /* extra leading aids dyslexic tracking */
  }
}

/* ====================================================================
 * Wizard image-zoom overlay (Skive 5 "forstør-ved-klik").
 * Bespoke top-layer <dialog>; tap backdrop / close / Escape to dismiss.
 * ==================================================================== */
.qp-image-zoom-overlay {
  border: 0;
  padding: 0;
  background: transparent;
  max-width: 100vw;
  max-height: 100vh;
  overflow: visible;
}

.qp-image-zoom-overlay::backdrop {
  background: rgba(0, 0, 0, 0.82);
  backdrop-filter: blur(2px);
}

.qp-image-zoom-overlay__img {
  display: block;
  max-width: 92vw;
  max-height: 88vh;
  width: auto;
  height: auto;
  margin: auto;
  border-radius: var(--hd-radius-lg);
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.5);
  background: var(--hd-surface-2);
}

.qp-image-zoom-overlay__close {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: var(--hd-radius-full);
  background: var(--hd-surface-2);
  color: var(--hd-text-primary);
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
}

.qp-image-zoom-overlay__close::before,
.qp-image-zoom-overlay__close::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 18px;
  height: 2px;
  background: currentColor;
}

.qp-image-zoom-overlay__close::before {
  transform: translate(-50%, -50%) rotate(45deg);
}

.qp-image-zoom-overlay__close::after {
  transform: translate(-50%, -50%) rotate(-45deg);
}

.qp-image-zoom-overlay__close:focus-visible {
  outline: 2px solid var(--hd-focus-ring);
  outline-offset: 2px;
}
