/* Media resize: 1px selection border + eight square handles (Google Docs pattern). */

/* Tokens follow the ancestor `color-scheme` via light-dark(); see media-toolbar.css. */
:root {
  --hm-resize-border: light-dark(#1a73e8, #60a5fa);
  --hm-resize-handle-bg: light-dark(#fff, #1f2937);
}

.hypermultimedia__resize-gripper {
  position: absolute;
  /* Overlay widget: never inherit the host editor's content-flow margins
     (e.g. `.ProseMirror > *` block spacing) — they shift it off the media box
     and desync offsetTop↔style.top, breaking drag. Must win the host cascade. */
  margin: 0 !important;
  display: none;
  pointer-events: none;
  box-sizing: border-box;
  z-index: 1;
}

.hypermultimedia__resize-gripper::before {
  content: '';
  position: absolute;
  inset: 0;
  border: 1px solid var(--hm-resize-border, #1a73e8);
  pointer-events: none;
}

.hypermultimedia__resize-gripper--active {
  display: block;
}

.hypermultimedia__resize-gripper .media-resize-clamp {
  position: absolute;
  width: 12px;
  height: 12px;
  display: none;
  pointer-events: auto;
  touch-action: none;
}

.hypermultimedia__resize-gripper--active .media-resize-clamp {
  display: block;
}

.hypermultimedia__resize-gripper .media-resize-clamp::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 8px;
  height: 8px;
  transform: translate(-50%, -50%);
  box-sizing: border-box;
  background: var(--hm-resize-handle-bg, #fff);
  border: 1px solid var(--hm-resize-border, #1a73e8);
}

.hypermultimedia__resize-gripper .media-resize-clamp--left {
  left: 0;
  top: 50%;
  transform: translate(-50%, -50%);
  cursor: ew-resize;
}

.hypermultimedia__resize-gripper .media-resize-clamp--right {
  right: 0;
  top: 50%;
  transform: translate(50%, -50%);
  cursor: ew-resize;
}

.hypermultimedia__resize-gripper .media-resize-clamp--top {
  top: 0;
  left: 50%;
  transform: translate(-50%, -50%);
  cursor: ns-resize;
}

.hypermultimedia__resize-gripper .media-resize-clamp--bottom {
  bottom: 0;
  left: 50%;
  transform: translate(-50%, 50%);
  cursor: ns-resize;
}

.hypermultimedia__resize-gripper .media-resize-clamp--top-left {
  top: 0;
  left: 0;
  transform: translate(-50%, -50%);
  cursor: nwse-resize;
}

.hypermultimedia__resize-gripper .media-resize-clamp--top-right {
  top: 0;
  right: 0;
  transform: translate(50%, -50%);
  cursor: nesw-resize;
}

.hypermultimedia__resize-gripper .media-resize-clamp--bottom-left {
  bottom: 0;
  left: 0;
  transform: translate(-50%, 50%);
  cursor: nesw-resize;
}

.hypermultimedia__resize-gripper .media-resize-clamp--bottom-right {
  bottom: 0;
  right: 0;
  transform: translate(50%, 50%);
  cursor: nwse-resize;
}

/* Drag: gripper decoration captures pointer — never inject into node-view contentDOM. */
.hypermultimedia__resize-gripper--dragging {
  pointer-events: auto;
}

.hypermultimedia__resize-gripper--dragging::after {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 2;
  background: transparent;
  touch-action: none;
  pointer-events: auto;
  cursor: inherit;
}

.hypermultimedia__resize-gripper--dragging .media-resize-clamp {
  z-index: 3;
}

html.hypermultimedia--resize-dragging,
html.hypermultimedia--resize-dragging * {
  user-select: none !important;
  cursor: inherit;
}

/* Loading shell tokens follow ancestor color-scheme via light-dark(). */
:root {
  --hm-loading-bg: light-dark(#f3f4f6, #111827);
  --hm-loading-shimmer: light-dark(rgba(255, 255, 255, 0.45), rgba(255, 255, 255, 0.08));
  --hm-loading-provider: light-dark(#374151, #e5e7eb);
  --hm-loading-message: light-dark(#6b7280, #9ca3af);
  --hm-loading-error: light-dark(#b91c1c, #f87171);
  --hm-loading-spinner-track: light-dark(#e5e7eb, #374151);
  --hm-loading-spinner-active: light-dark(#2563eb, #60a5fa);
}

.hm-media-host {
  position: relative;
  box-sizing: border-box;
  max-width: 100%;
  overflow: hidden;
  border-radius: 6px;
  background: var(--hm-loading-bg, #f3f4f6);
}

.hm-media-host--plain {
  background: transparent;
}

.hm-media-slot {
  width: 100%;
  height: 100%;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.hm-media-slot > * {
  display: block;
  width: 100%;
  height: 100%;
}

.hm-media-host[data-hm-loading='ready'] {
  background: transparent;
}

.hm-media-host[data-hm-loading='ready'] .hm-media-slot {
  opacity: 1;
}

.hm-media-host[data-hm-loading='ready'] iframe {
  background: transparent;
}

.hm-media-host[data-hm-loading='error'] .hm-media-slot {
  opacity: 1;
}

.hm-loading-shell,
.hm-loading-shell__overlay {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  border-radius: inherit;
}

.hm-media-host[data-hm-loading='ready'] .hm-loading-shell {
  opacity: 0;
  visibility: hidden;
  transition:
    opacity 0.2s ease,
    visibility 0.2s ease;
}

.hm-media-host[data-hm-loading='error'] .hm-loading-shell__shimmer,
.hm-media-host[data-hm-loading='error'] .hm-loading-shell__spinner {
  display: none;
}

.hm-loading-shell__shimmer {
  position: absolute;
  inset: 0;
  background: linear-gradient(
    90deg,
    transparent 0%,
    var(--hm-loading-shimmer, rgba(255, 255, 255, 0.45)) 50%,
    transparent 100%
  );
  background-size: 200% 100%;
  animation: hm-loading-shimmer 1.4s ease-in-out infinite;
}

@keyframes hm-loading-shimmer {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

.hm-loading-shell__body {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.35rem;
  padding: 0.75rem 1rem;
  text-align: center;
}

.hm-loading-shell__provider {
  font-size: 0.8125rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--hm-loading-provider, #374151);
}

.hm-loading-shell__message {
  font-size: 0.75rem;
  color: var(--hm-loading-message, #6b7280);
}

.hm-media-host[data-hm-loading='error'] .hm-loading-shell__message {
  color: var(--hm-loading-error, #b91c1c);
}

.hm-loading-shell__spinner {
  width: 1.25rem;
  height: 1.25rem;
  border: 2px solid var(--hm-loading-spinner-track, #e5e7eb);
  border-top-color: var(--hm-loading-spinner-active, #2563eb);
  border-radius: 50%;
  animation: hm-loading-spin 0.7s linear infinite;
}

@keyframes hm-loading-spin {
  to {
    transform: rotate(360deg);
  }
}

.hm-media-slot iframe,
.hm-media-slot video,
.hm-media-slot audio,
.hm-media-slot img {
  display: block;
  max-width: 100%;
}

@media (prefers-reduced-motion: reduce) {
  .hm-loading-shell__shimmer,
  .hm-loading-shell__spinner {
    animation: none;
  }
}

.hm-media-host--fluid {
  overflow: visible;
}

.hm-media-host--fluid[data-hm-loading='ready'] {
  height: auto !important;
}

.hypermultimedia--x__content {
  border-radius: 12px;
  overflow: visible;
}

.hypermultimedia--x__content:not(:has(.hm-media-host[data-hm-loading='ready'])) {
  overflow: hidden;
}

.hypermultimedia--x__content[data-x-theme='dark'] {
  background: rgb(21, 32, 43);
}

.hypermultimedia--x__content[data-x-theme='light'] {
  background: #fff;
}

.hypermultimedia--x__content .hm-media-host[data-hm-loading='ready'] iframe {
  border-radius: inherit;
}

.hm-media-host--fluid .hm-media-slot {
  height: auto;
}

.hypermultimedia--loom__content .hm-media-slot {
  overflow: hidden;
}

.hypermultimedia--loom__content iframe {
  display: block;
  border: 0;
}

/* Spotify's signature rounded player — mirrors the 12px radius in its oEmbed iframe. */
.hypermultimedia--spotify__content iframe,
.spotify-media iframe {
  display: block;
  border: 0;
  border-radius: 12px;
}

/*
 * Theme tokens use `light-dark()` so the toolbar follows the nearest ancestor's
 * `color-scheme` — a host toggling `color-scheme: light | dark` on <html> flips
 * it automatically; with the default `normal` it falls back to the OS
 * `prefers-color-scheme`. Same approach as extension-hyperlink's styles.css.
 */
:root {
  --hm-toolbar-bg: light-dark(#ffffff, #1f2937);
  --hm-toolbar-fg: light-dark(#374151, #e5e7eb);
  --hm-toolbar-border: light-dark(rgba(0, 0, 0, 0.08), rgba(255, 255, 255, 0.12));
  --hm-toolbar-hover: light-dark(#f3f4f6, #374151);
  --hm-toolbar-active: light-dark(#e5edff, #1e3a8a);
  --hm-toolbar-active-fg: light-dark(#1d4ed8, #bfdbfe);
  /* Elevation matches the docs.plus floating-surface language: 10px shells with a
     two-layer overlay shadow (anchored panels), 8px inner controls — lockstep with
     extension-hyperlink's --hl-shadow/--hl-radius. */
  --hm-toolbar-shadow:
    0 20px 25px -5px light-dark(rgba(0, 0, 0, 0.08), rgba(0, 0, 0, 0.5)),
    0 8px 10px -6px light-dark(rgba(0, 0, 0, 0.08), rgba(0, 0, 0, 0.4));
  --hm-caption-fg: light-dark(#6b7280, #9ca3af);
  --hm-caption-placeholder: light-dark(#9ca3af, #6b7280);
}

[class*='hypermultimedia--'][class*='__content'] {
  position: relative;
  box-sizing: border-box;
  max-width: 100%;
  min-width: 0;
}

.media-toolbar {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 6;
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 3px;
  border: 1px solid var(--hm-toolbar-border);
  border-radius: 10px;
  background: var(--hm-toolbar-bg);
  box-shadow: var(--hm-toolbar-shadow);
  opacity: 0;
  /* exit: 80ms ease-in fade once .hm-has-toolbar is removed */
  transition: opacity 80ms ease-in;
}
.hm-has-toolbar > .media-toolbar:not([data-hm-closing]) {
  opacity: 1;
  /* Keyframe, not transition: the class lands before DOM insertion, so a transition never fires. */
  animation: hm-toolbar-in 120ms ease-out;
}
@keyframes hm-toolbar-in {
  from {
    opacity: 0;
    transform: translateY(-2px);
  }
}

.media-toolbar__button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--hm-toolbar-fg);
  cursor: pointer;
  transition: background-color 120ms ease;
}
.media-toolbar__button svg,
.media-toolbar__menu-item svg,
.media-toolbar__submenu-item svg {
  display: block;
  flex-shrink: 0;
}
.media-toolbar__button:hover {
  background: var(--hm-toolbar-hover);
}
.media-toolbar__button--active {
  background: var(--hm-toolbar-active);
  color: var(--hm-toolbar-active-fg);
}

.media-toolbar__button--text {
  width: auto;
  min-width: 28px;
  padding: 0 6px;
  font-size: 12px;
}
.media-toolbar__divider {
  box-sizing: border-box;
  width: 1px;
  align-self: stretch;
  flex-shrink: 0;
  margin-block: 5px;
  margin-inline: 0;
  background: var(--hm-toolbar-border);
}

.media-toolbar__menu,
.media-toolbar__submenu {
  display: flex;
  flex-direction: column;
  min-width: 160px;
  padding: 4px;
  border: 1px solid var(--hm-toolbar-border);
  border-radius: 10px;
  background: var(--hm-toolbar-bg);
  box-shadow: var(--hm-toolbar-shadow);
}
.media-toolbar__menu-item,
.media-toolbar__submenu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 5px 8px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--hm-toolbar-fg);
  font: inherit;
  /* Match the bar's compact scale — `font: inherit` alone resolves to the page's editor size. */
  font-size: 12px;
  text-align: left;
  cursor: pointer;
  transition: background-color 120ms ease;
}
.media-toolbar__menu-item:hover,
.media-toolbar__submenu-item:hover {
  background: var(--hm-toolbar-hover);
}
.media-toolbar__menu-item--active,
.media-toolbar__submenu-item--active {
  background: var(--hm-toolbar-active);
  color: var(--hm-toolbar-active-fg);
}
.media-toolbar__menu-heading,
.media-toolbar__submenu-heading {
  margin: 6px 8px 2px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--hm-caption-fg);
}

.media-toolbar__input {
  display: block;
  box-sizing: border-box;
  width: 100%;
  margin-bottom: 2px;
  padding: 6px 8px;
  border: 1px solid var(--hm-toolbar-border);
  border-radius: 8px;
  background: transparent;
  color: var(--hm-toolbar-fg);
  font: inherit;
  font-size: 12px;
  outline: none;
  transition: border-color 120ms ease;
}
.media-toolbar__input:focus {
  border-color: var(--hm-toolbar-active-fg);
}
.media-toolbar__error {
  margin: 2px 8px 4px;
  font-size: 12px;
  color: var(--hm-loading-error);
}

.hm-caption {
  display: block;
  margin-top: 6px;
  font-size: 13px;
  line-height: 1.4;
  color: var(--hm-caption-fg);
  outline: none;
}
.hm-caption:empty::before {
  content: attr(data-placeholder);
  color: var(--hm-caption-placeholder);
}
.hm-caption--empty:not(:focus) {
  display: none;
}

/* Captioned media serializes inside <figure>; reset UA figure margins so alignment attrs win. */
.hypermultimedia--figure {
  margin: 0;
  display: inline-block;
  max-width: 100%;
}

/* Floating-popover shell (overflow/submenu) — kept in lockstep with extension-hyperlink styles.css. */
.floating-popover {
  opacity: 0;
  transform: scale(0.96);
  /* exit: 80ms ease-in fade (plays when .visible is removed; engine defers removal) */
  transition:
    opacity 80ms ease-in,
    transform 80ms ease-in;
  pointer-events: none;
  -webkit-tap-highlight-color: transparent;
  will-change: transform, opacity;
}

.floating-popover.visible {
  opacity: 1;
  transform: scale(1);
  pointer-events: auto;
  /* enter: 120ms ease-out scale from the anchored side (engine sets transform-origin) */
  transition:
    opacity 120ms ease-out,
    transform 120ms ease-out;
}

@media (prefers-reduced-motion: reduce) {
  .floating-popover,
  .floating-popover.visible {
    transition: none;
  }
}

.floating-popover-content {
  position: relative;
  z-index: 2;
}

/* Floating-tooltip skin (icon-button labels) — lockstep with extension-hyperlink styles.css.
   Both extensions style this one global class, so the block uses identical literals, never
   per-package tokens: with both bundles loaded, cascade order would pick one package's
   tokens for every bubble. */
.floating-tooltip {
  position: fixed;
  z-index: 10000;
  width: max-content;
  padding: 3px 8px;
  border-radius: 6px;
  background: light-dark(#374151, #e5e7eb);
  color: light-dark(#ffffff, #1f2937);
  font-size: 11px;
  box-shadow: 0 4px 12px light-dark(rgba(0, 0, 0, 0.15), rgba(0, 0, 0, 0.5));
  opacity: 0;
  pointer-events: none;
  transform: translateY(2px);
  transition:
    opacity 100ms ease,
    transform 100ms ease;
}

.floating-tooltip.visible {
  opacity: 1;
  transform: translateY(0);
}

/* Matches the media-loading-shell.css precedent: zero movement, keep instant states. */
@media (prefers-reduced-motion: reduce) {
  .hm-has-toolbar > .media-toolbar:not([data-hm-closing]) {
    animation: none;
  }
  .media-toolbar,
  .media-toolbar__button,
  .media-toolbar__menu-item,
  .media-toolbar__submenu-item,
  .media-toolbar__input,
  .floating-popover,
  .floating-tooltip {
    transition: none;
  }
}
