/* media-window.css — the one floating media window (PDF, audio, video).
   Written for the phone first (375×812: a full-width sheet from the
   bottom, 44 px controls, one column, no horizontal overflow); the desk
   inherits it and adds the floating, draggable, expandable panel.
   Colour and glass come from the app's own tokens (app.css, popups.css,
   layers.css) — nothing here names a colour of its own except the paper. */

.mw-window {
  --mw-glass: var(--popup-glass-b, color-mix(in srgb, var(--bg-elevated) 65%, transparent));
  --mw-line: var(--border, rgba(127,127,127,.35));
  --mw-paper: #f2f5f3;
  --mw-well: color-mix(in srgb, var(--bg-sunken, var(--bg)) 70%, transparent);
  position: fixed;
  z-index: calc(var(--z-dialog, 1200) + 5);
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  left: 0;
  right: 0;
  bottom: 0;
  top: auto;
  width: 100%;
  height: 92dvh;
  max-height: 100dvh;
  margin: 0;
  padding: 0;
  overflow: hidden;
  color: var(--text);
  background: var(--mw-glass);
  -webkit-backdrop-filter: blur(20px) saturate(1.4);
  backdrop-filter: blur(20px) saturate(1.4);
  border: 1px solid var(--mw-line);
  border-bottom: 0;
  border-radius: var(--radius-lg, 16px) var(--radius-lg, 16px) 0 0;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255,.08),
    var(--shadow-lg, 0 30px 80px rgba(0,0,0,.4));
  touch-action: manipulation;
}
.mw-window[hidden] { display: none; }

/* Expanded: the whole viewport, respecting the notch and the home bar. */
.mw-window.is-expanded {
  inset: 0;
  width: 100%;
  height: 100dvh;
  max-height: none;
  border-radius: 0;
  border: 0;
  transform: none !important;
  padding-top: env(safe-area-inset-top);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

/* ---- title bar -------------------------------------------------------- */
.mw-bar {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 6px 8px;
  border-bottom: 1px solid var(--mw-line);
  background: color-mix(in srgb, var(--bg-elevated) 30%, transparent);
  user-select: none;
  -webkit-user-select: none;
}
.mw-bar-row { display: flex; align-items: center; gap: 4px; min-height: 44px; }
.mw-bar-main { cursor: default; }
.mw-window.is-dragging .mw-bar-main { cursor: grabbing; }
.mw-title {
  flex: 1 1 auto;
  min-width: 0;
  margin: 0;
  padding: 0 6px;
  font-size: 15px;
  font-weight: 600;
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.mw-pages, .mw-zoom, .mw-time {
  flex: 0 0 auto;
  font-family: var(--font-mono, ui-monospace, monospace);
  font-size: 12px;
  color: var(--text-muted);
  padding: 0 6px;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.mw-zoom { min-width: 48px; text-align: center; }
.mw-bar-tools { justify-content: space-between; }
.mw-group { display: inline-flex; align-items: center; gap: 2px; }
.mw-window[data-kind="audio"] .mw-pdf-only,
.mw-window[data-kind="video"] .mw-pdf-only,
.mw-window[data-kind="loading"] .mw-pdf-only { visibility: hidden; }
.mw-window[data-kind="audio"] .mw-pages,
.mw-window[data-kind="video"] .mw-pages { display: none; }

/* Every control is a 44 px hand; only its face is drawn lighter. */
.mw-btn {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  min-width: 44px;
  padding: 0;
  margin: 0;
  border: 0;
  border-radius: var(--radius-sm, 8px);
  background: transparent;
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.mw-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.mw-btn:hover { background: var(--popup-row-hover, rgba(127,127,127,.12)); }
.mw-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.mw-btn[aria-pressed="true"] { color: var(--accent); }
.mw-close { margin-left: 2px; }

/* ---- body ------------------------------------------------------------- */
.mw-body {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  background: var(--mw-well);
  outline: none;
  padding-bottom: env(safe-area-inset-bottom);
}
.mw-body.is-pinching { overflow: hidden; }
.mw-body:focus-visible { box-shadow: inset 0 0 0 2px var(--accent); }
.mw-pages { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 12px; will-change: transform; }
.mw-page {
  position: relative;
  flex: 0 0 auto;
  background: var(--mw-paper);
  box-shadow: 0 1px 2px rgba(0,0,0,.18), 0 8px 24px rgba(0,0,0,.18);
  overflow: hidden;
  color-scheme: only light;
}
.mw-page.is-broken::after {
  content: "This page could not be drawn.";
  position: absolute; inset: 0; display: grid; place-items: center;
  font-size: 13px; color: #555;
}
.mw-canvas { display: block; position: absolute; inset: 0; }
.mw-status {
  position: absolute;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  padding: 10px 16px;
  border-radius: var(--radius-pill, 999px);
  background: var(--mw-glass);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  border: 1px solid var(--mw-line);
  font-size: 13px;
  color: var(--text-muted);
  pointer-events: none;
  max-width: calc(100% - 32px);
  text-align: center;
}
.mw-status[hidden] { display: none; }
.mw-status.is-error { color: var(--danger, #b00020); pointer-events: auto; }
.mw-fallback { margin: 0; padding: 24px 16px; text-align: center; font-size: 14px; color: var(--text-muted); }
.mw-fallback a { color: var(--accent); }

/* ---- pdf.js text layer (the vendor's own rules, scoped to the window) -- */
.mw-window .textLayer {
  color-scheme: only light;
  position: absolute;
  text-align: initial;
  inset: 0;
  overflow: clip;
  opacity: 1;
  line-height: 1;
  -webkit-text-size-adjust: none;
  text-size-adjust: none;
  forced-color-adjust: none;
  transform-origin: 0 0;
  caret-color: CanvasText;
  z-index: 1;
  --min-font-size: 1;
  --text-scale-factor: calc(var(--total-scale-factor) * var(--min-font-size));
  --min-font-size-inv: calc(1 / var(--min-font-size));
}
.mw-window .textLayer :is(span, br) {
  color: transparent;
  position: absolute;
  white-space: pre;
  cursor: text;
  transform-origin: 0% 0%;
}
.mw-window .textLayer > :not(.markedContent),
.mw-window .textLayer .markedContent span:not(.markedContent) {
  z-index: 1;
  --font-height: 0;
  font-size: calc(var(--text-scale-factor) * var(--font-height));
  --scale-x: 1;
  --rotate: 0deg;
  transform: rotate(var(--rotate)) scaleX(var(--scale-x)) scale(var(--min-font-size-inv));
}
.mw-window .textLayer .markedContent { display: contents; }
.mw-window .textLayer span[role="img"] { user-select: none; cursor: default; }
.mw-window .textLayer ::selection { background: color-mix(in srgb, var(--accent, #050605) 30%, transparent); }
.mw-window .textLayer br::selection { background: transparent; }
.mw-window .textLayer .endOfContent {
  display: block; position: absolute; inset: 100% 0 0; z-index: 0; cursor: default; user-select: none;
}
.mw-window .textLayer.selecting .endOfContent { top: 0; }

/* ---- audio / video ---------------------------------------------------- */
.mw-body-media { display: flex; flex-direction: column; align-items: stretch; background: #000; }
.mw-window[data-kind="audio"] .mw-body-media { background: var(--mw-well); }
.mw-media { display: block; width: 100%; max-height: 100%; background: #000; outline: none; }
.mw-window[data-kind="video"] .mw-media { flex: 1 1 auto; min-height: 0; object-fit: contain; }
.mw-window[data-kind="audio"] .mw-media { height: 0; }
.mw-audio-art {
  flex: 1 1 auto;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px;
  padding: 24px 16px; text-align: center; min-height: 160px;
}
.mw-audio-name { font-size: 15px; font-weight: 600; color: var(--text); }
.mw-audio-wave { display: flex; align-items: flex-end; gap: 6px; height: 48px; }
.mw-audio-wave i { display: block; width: 6px; height: 12px; border-radius: 3px; background: var(--accent); opacity: .7; }
.mw-window[data-kind="audio"]:has(.mw-media:not([paused])) .mw-audio-wave i { animation: mw-wave 1s ease-in-out infinite; }
.mw-audio-wave i:nth-child(2) { animation-delay: .15s; }
.mw-audio-wave i:nth-child(3) { animation-delay: .3s; }
.mw-audio-wave i:nth-child(4) { animation-delay: .45s; }
.mw-audio-wave i:nth-child(5) { animation-delay: .6s; }
@keyframes mw-wave { 0%, 100% { height: 12px; } 50% { height: 44px; } }
@media (prefers-reduced-motion: reduce) { .mw-audio-wave i { animation: none !important; } }

.mw-transport {
  flex: 0 0 auto;
  display: flex; align-items: center; gap: 2px; flex-wrap: wrap;
  padding: 4px 8px calc(4px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--mw-line);
  background: color-mix(in srgb, var(--bg-elevated) 30%, transparent);
}
.mw-transport[hidden] { display: none; }
.mw-seek { flex: 1 1 120px; min-width: 80px; height: 44px; margin: 0; accent-color: var(--accent); }
.mw-volume { flex: 0 1 90px; width: 90px; height: 44px; margin: 0; accent-color: var(--accent); }
.mw-speed { font-family: var(--font-mono, ui-monospace, monospace); }

.mw-captions {
  flex: 0 0 auto;
  max-height: 34%;
  overflow: auto;
  padding: 8px 12px;
  border-top: 1px solid var(--mw-line);
  background: var(--mw-glass);
  color: var(--text);
  font-size: 14px;
  line-height: 1.45;
}
.mw-window:not(.has-captions) .mw-captions { display: none; }
.mw-cue { display: flex; gap: 10px; width: 100%; margin: 0; padding: 6px 4px; border: 0; border-radius: var(--radius-sm, 8px); background: transparent; color: inherit; text-align: left; font: inherit; cursor: default; min-height: 44px; align-items: center; }
button.mw-cue { cursor: pointer; }
button.mw-cue:hover { background: var(--popup-row-hover, rgba(127,127,127,.12)); }
.mw-cue-t { flex: 0 0 auto; font-family: var(--font-mono, ui-monospace, monospace); font-size: 12px; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.mw-cue.is-live { background: color-mix(in srgb, var(--accent) 12%, transparent); }

/* While the window is open on a phone, the page behind it holds still. */
@media (max-width: 767.98px) {
  body.mw-open { overflow: hidden; }
}

/* ---- the desk: a floating, draggable panel -------------------------- */
@media (min-width: 768px) {
  .mw-window {
    left: 50%;
    top: 50%;
    right: auto;
    bottom: auto;
    transform: translate(-50%, -50%);
    width: min(920px, calc(100% - 48px));
    height: min(860px, calc(100dvh - 48px));
    border-bottom: 1px solid var(--mw-line);
    border-radius: var(--radius-lg, 16px);
    resize: both;
    min-width: 360px;
    min-height: 240px;
  }
  .mw-window.is-expanded { resize: none; }
  .mw-bar { flex-direction: row; align-items: center; gap: 4px; padding: 4px 8px; cursor: grab; }
  .mw-bar-row { display: contents; }
  .mw-title { order: 1; }
  .mw-pages { order: 2; }
  .mw-pdf-only { order: 3; }
  .mw-group:not(.mw-pdf-only) { order: 4; }
  .mw-close { order: 5; margin-left: 6px; }
  .mw-pages { padding: 24px; gap: 16px; }
  .mw-transport { padding: 6px 12px; flex-wrap: nowrap; }
  .mw-captions { max-height: 30%; }
}
