/* studia-video — CSS scoped (.sv-* prefix) */

studia-video {
  --sv-bg: #000;
  --sv-controls-bg: rgba(15, 15, 18, 0.92);
  --sv-text: #fff;
  --sv-muted: #9ca3af;
  --sv-accent: #3b82f6;
  --sv-track: rgba(255, 255, 255, 0.18);
  --sv-track-fill: var(--sv-accent);
  --sv-radius: 10px;
  --sv-controls-height: 56px;

  display: block;
  position: relative;
  width: 100%;
  background: var(--sv-bg);
  border-radius: var(--sv-radius);
  overflow: hidden;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--sv-text);
  user-select: none;
  -webkit-user-select: none;

  /* Container query: i controlli decidono il layout in base alla larghezza
     del componente, non del viewport. Funziona uguale in classic, IA video,
     IA audio sticky bottom, ecc. */
  container-type: inline-size;
}

studia-video.sv-css-fullscreen {
  position: fixed !important;
  inset: 0 !important;
  width: 100vw !important;
  height: 100vh !important;
  z-index: 99999 !important;
  border-radius: 0 !important;
  max-width: none !important;
  max-height: none !important;
}

html.sv-css-fullscreen-lock,
html.sv-css-fullscreen-lock body {
  overflow: hidden !important;
}

studia-video .sv-stage {
  position: relative;
  width: 100%;
  background: #000;
  aspect-ratio: 16 / 9;
}

studia-video.sv-css-fullscreen .sv-stage,
studia-video:fullscreen .sv-stage {
  height: 100%;
  aspect-ratio: auto;
}

studia-video video {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
  background: #000;
  cursor: pointer;
}

studia-video .sv-overlay-play {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.35);
  cursor: pointer;
  transition: opacity 0.2s ease;
  pointer-events: none;
}
studia-video[data-state="playing"] .sv-overlay-play {
  opacity: 0;
}
studia-video .sv-overlay-play svg {
  width: 80px;
  height: 80px;
  fill: rgba(255, 255, 255, 0.9);
  filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.5));
}

studia-video .sv-controls {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 10px 14px;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.85) 0%, rgba(0, 0, 0, 0) 100%);
  display: flex;
  flex-direction: column;
  gap: 6px;
  transition: opacity 0.25s ease, transform 0.25s ease;
  z-index: 5;
}

studia-video[data-state="playing"][data-controls="hidden"] .sv-controls {
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
}

studia-video .sv-progress {
  position: relative;
  height: 6px;
  background: var(--sv-track);
  border-radius: 999px;
  cursor: pointer;
  overflow: hidden;
}
studia-video .sv-progress-buffered {
  position: absolute;
  left: 0; top: 0; bottom: 0;
  background: rgba(255, 255, 255, 0.25);
  width: 0;
}
studia-video .sv-progress-played {
  position: absolute;
  left: 0; top: 0; bottom: 0;
  background: var(--sv-track-fill);
  width: 0;
}
studia-video .sv-progress:hover {
  height: 8px;
}

studia-video .sv-controls-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

studia-video .sv-btn {
  appearance: none;
  background: transparent;
  border: 0;
  color: var(--sv-text);
  cursor: pointer;
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 13px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: background 0.15s ease;
}
studia-video .sv-btn:hover {
  background: rgba(255, 255, 255, 0.12);
}
studia-video .sv-btn:focus-visible {
  outline: 2px solid var(--sv-accent);
  outline-offset: 2px;
}
studia-video .sv-btn svg {
  width: 20px;
  height: 20px;
  fill: currentColor;
}

studia-video .sv-time {
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  color: var(--sv-muted);
  min-width: 90px;
  text-align: center;
}

studia-video .sv-spacer {
  flex: 1;
}

studia-video .sv-btn-toggle.active,
studia-video .sv-btn[aria-pressed="true"] {
  background: rgba(59, 130, 246, 0.25);
  color: #fff;
}

studia-video .sv-popover {
  position: absolute;
  bottom: calc(100% + 8px);
  right: 0;
  min-width: 180px;
  background: var(--sv-controls-bg);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  padding: 8px;
  display: none;
  z-index: 10;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}
studia-video .sv-popover.open {
  display: block;
}
studia-video .sv-popover-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--sv-muted);
  padding: 4px 8px 6px;
}
studia-video .sv-popover-item {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  color: var(--sv-text);
  padding: 7px 10px;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
}
studia-video .sv-popover-item:hover,
studia-video .sv-popover-item.active {
  background: rgba(255, 255, 255, 0.12);
}
studia-video .sv-popover-item.active {
  color: var(--sv-accent);
  font-weight: 600;
}

studia-video .sv-control-wrapper {
  position: relative;
}

studia-video .sv-volume {
  display: flex;
  align-items: center;
  gap: 6px;
}
studia-video .sv-volume input[type="range"] {
  width: 90px;
  accent-color: var(--sv-accent);
}
studia-video .sv-boost-badge {
  font-size: 10px;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(135deg, #f59e0b, #ef4444);
  padding: 2px 5px;
  border-radius: 4px;
  margin-left: 4px;
  letter-spacing: 0.04em;
}

studia-video .sv-error,
studia-video .sv-loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 20px;
  background: rgba(0, 0, 0, 0.7);
  font-size: 14px;
  color: var(--sv-muted);
  z-index: 8;
}

/* Resume overlay */
studia-video .sv-resume-overlay {
  position: absolute;
  left: 50%;
  bottom: calc(var(--sv-controls-height) + 16px);
  transform: translateX(-50%);
  z-index: 20;
  animation: sv-resume-in 0.25s ease;
  max-width: calc(100% - 24px);
}

studia-video .sv-resume-overlay.sv-resume-out {
  animation: sv-resume-out 0.2s ease forwards;
}

studia-video .sv-resume-card {
  background: var(--sv-controls-bg);
  border: 1px solid rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 10px;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 280px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}

studia-video .sv-resume-text {
  font-size: 13px;
  color: var(--sv-text);
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}
studia-video .sv-resume-time {
  font-variant-numeric: tabular-nums;
  color: var(--sv-accent);
}

studia-video .sv-resume-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

studia-video .sv-btn-primary {
  background: var(--sv-accent);
  color: #fff;
  padding: 7px 14px;
  font-weight: 600;
}
studia-video .sv-btn-primary:hover {
  background: #2563eb;
}
studia-video .sv-btn-secondary {
  background: rgba(255, 255, 255, 0.1);
  padding: 7px 14px;
}

studia-video .sv-resume-progress {
  height: 2px;
  background: rgba(255, 255, 255, 0.12);
  border-radius: 2px;
  overflow: hidden;
}
studia-video .sv-resume-progress-bar {
  height: 100%;
  background: var(--sv-accent);
  transform-origin: left;
  transform: scaleX(0);
}

@keyframes sv-resume-in {
  from { opacity: 0; transform: translate(-50%, 8px); }
  to   { opacity: 1; transform: translate(-50%, 0); }
}
@keyframes sv-resume-out {
  to { opacity: 0; transform: translate(-50%, 8px); }
}

@media (max-width: 640px) {
  studia-video .sv-time {
    min-width: 70px;
    font-size: 11px;
  }
  studia-video .sv-volume input[type="range"] {
    width: 60px;
  }
  studia-video .sv-btn {
    padding: 6px;
  }
}

/* ===== Offline button ===== */

studia-video .sv-offline-btn {
  position: relative;
  min-width: 36px;
  font-size: 12px;
  white-space: nowrap;
}
studia-video .sv-offline-btn[data-state="cached"] {
  color: #4ade80;
}
studia-video .sv-offline-btn[data-state="cached"]:hover {
  background: rgba(74, 222, 128, 0.15);
}
studia-video .sv-offline-btn[data-state="downloading"] {
  background: rgba(59, 130, 246, 0.2);
  color: #60a5fa;
  cursor: progress;
}
studia-video .sv-offline-btn[data-state="error"] {
  color: #f87171;
}
studia-video .sv-offline-btn .sv-offline-fill {
  position: absolute;
  left: 0; top: 0; bottom: 0;
  background: rgba(59, 130, 246, 0.35);
  width: 0;
  border-radius: 6px;
  pointer-events: none;
  z-index: -1;
}
studia-video .sv-offline-btn .sv-offline-label {
  position: relative;
  z-index: 1;
}

/* ===== Dialog ===== */

studia-video .sv-dialog-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  animation: sv-dialog-fade 0.18s ease;
  padding: 20px;
}
studia-video .sv-dialog-backdrop.sv-dialog-out {
  animation: sv-dialog-fade 0.18s ease reverse forwards;
}

studia-video .sv-dialog {
  background: #1f2937;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  padding: 20px 22px;
  max-width: 420px;
  width: 100%;
  color: #f1f5f9;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.6);
}
studia-video .sv-dialog-icon {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  margin-bottom: 12px;
}
studia-video .sv-dialog-icon svg {
  width: 24px;
  height: 24px;
}
studia-video .sv-dialog-icon-info {
  background: rgba(59, 130, 246, 0.15);
  color: #60a5fa;
}
studia-video .sv-dialog-icon-warn {
  background: rgba(245, 158, 11, 0.15);
  color: #fbbf24;
}
studia-video .sv-dialog-title {
  font-size: 16px;
  font-weight: 700;
  margin-bottom: 8px;
  color: #fff;
}
studia-video .sv-dialog-body {
  font-size: 14px;
  line-height: 1.5;
  color: #cbd5e0;
  margin-bottom: 12px;
}
studia-video .sv-dialog-body p { margin: 0 0 8px; }
studia-video .sv-dialog-body strong { color: #f1f5f9; }
studia-video .sv-dialog-body .sv-dim {
  color: #94a3b8;
  font-size: 12px;
}
studia-video .sv-dialog-extra {
  margin-bottom: 14px;
}
studia-video .sv-dialog-extra:empty { display: none; }
studia-video .sv-dialog-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}
studia-video .sv-dialog .sv-btn-danger {
  background: #dc2626;
  color: #fff;
  padding: 7px 14px;
  font-weight: 600;
}
studia-video .sv-dialog .sv-btn-danger:hover {
  background: #b91c1c;
}

/* Progress dialog bar */
studia-video .sv-progress-bar {
  height: 6px;
  background: rgba(255, 255, 255, 0.12);
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 6px;
}
studia-video .sv-progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #3b82f6, #60a5fa);
  width: 0;
  transition: width 0.18s ease;
}
studia-video .sv-progress-pct {
  font-size: 12px;
  color: #94a3b8;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

@keyframes sv-dialog-fade {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* ===== Download banner ===== */

studia-video .sv-download-banner {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  background: linear-gradient(180deg, rgba(15, 23, 42, 0.96), rgba(15, 23, 42, 0.85));
  backdrop-filter: blur(8px);
  color: #fff;
  padding: 10px 14px 0;
  display: none;
  flex-direction: column;
  gap: 6px;
  z-index: 15;
  border-radius: 10px 10px 0 0;
  animation: sv-banner-in 0.25s ease;
}
studia-video .sv-download-banner.visible {
  display: flex;
}
studia-video .sv-download-banner-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 12px;
}
studia-video .sv-download-banner-title {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 600;
}
studia-video .sv-download-banner-title svg {
  width: 14px;
  height: 14px;
  fill: #60a5fa;
  animation: sv-pulse 1.4s ease-in-out infinite;
}
studia-video .sv-download-banner-info {
  color: #cbd5e0;
  font-variant-numeric: tabular-nums;
  font-size: 11px;
}
studia-video .sv-download-banner-cancel {
  background: transparent;
  border: 0;
  color: #cbd5e0;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 11px;
  cursor: pointer;
  text-decoration: underline;
}
studia-video .sv-download-banner-cancel:hover {
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
}
studia-video .sv-download-banner-bar {
  height: 3px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 0;
  overflow: hidden;
  margin: 0 -14px;
}
studia-video .sv-download-banner-fill {
  height: 100%;
  background: linear-gradient(90deg, #3b82f6, #60a5fa, #a78bfa);
  width: 0%;
  transition: width 0.25s ease;
}

@keyframes sv-banner-in {
  from { transform: translateY(-100%); opacity: 0; }
  to   { transform: translateY(0); opacity: 1; }
}

@keyframes sv-pulse {
  0%, 100% { opacity: 1; transform: translateY(0); }
  50%      { opacity: 0.5; transform: translateY(2px); }
}

studia-video .sv-offline-btn[data-state="loading"] {
  pointer-events: none;
  cursor: wait;
  opacity: 0.7;
}
studia-video .sv-offline-btn[data-state="loading"] .sv-offline-label {
  display: inline-block;
  animation: sv-spin 0.9s linear infinite;
}
@keyframes sv-spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* ===== Audio mode: card compatta, niente video frame ===== */
studia-video[data-media-type="audio"] .sv-stage {
  aspect-ratio: auto;
  background: linear-gradient(135deg, #1f2937, #0f172a);
  min-height: 0;
  display: block;
  padding-bottom: 0;
}
studia-video[data-media-type="audio"] audio.sv-video {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}
studia-video[data-media-type="audio"] .sv-controls {
  position: relative;
  background: transparent;
  opacity: 1 !important;
  transform: none !important;
  pointer-events: auto !important;
  padding: 12px 14px;
}

studia-video .sv-quality-btn {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  min-width: 36px;
}
studia-video .sv-quality-btn.active {
  background: rgba(59, 130, 246, 0.25);
  color: #fff;
}

/* Download banner: variante "loading" (preparing). Niente progress bar visibile,
   niente bottone Annulla, animazione pulse sull'icona per ribadire l'attesa. */
studia-video .sv-download-banner[data-phase="loading"] .sv-download-banner-bar {
  display: none;
}
studia-video .sv-download-banner[data-phase="loading"] .sv-download-banner-cancel {
  display: none;
}
studia-video .sv-download-banner[data-phase="loading"] .sv-download-banner-title {
  animation: sv-pulse 1.4s ease-in-out infinite;
}

/* ============================================================================
   COMPACT MODE — strategie diverse per AUDIO e VIDEO sotto 480px del componente
   ----------------------------------------------------------------------------
   AUDIO compact: two-row stabile (player non e' overlay, puo' permettersi altezza)
     [timeline ──────────────────────────]
     [◀10] [▶] [▶10]      02:45 / 47:12   ← riga nav
     [🔊]  [Boost] [1×] [↓] [ⓘ]            ← riga secondari

   VIDEO compact: single-row + overflow menu (l'overlay non deve coprire il video)
     [timeline ──────────────────────────]
     [◀10] [▶] [▶10]  02:45/47:12  [🔊][⋯][⛶]
                                       └─ click ⋯: secondari su seconda riga, ⋯->✕

   Container query (Safari iOS 16+, Chrome 105+, Firefox 110+):
   il layout reagisce alla larghezza della *component*, non del viewport.
   ============================================================================ */

/* Default: bottone "more" nascosto su tutti i media-type (e' video-compact-only) */
studia-video .sv-more-btn {
  display: none;
}

@container (max-width: 480px) {
  /* ===== AUDIO compact: two-row (tutti visibili, gap stretto) ===== */
  studia-video[data-media-type="audio"] .sv-controls-row {
    flex-wrap: wrap;
    gap: 6px;
    row-gap: 8px;
  }
  studia-video[data-media-type="audio"] .sv-controls-row > .sv-spacer {
    flex: 0 0 100%;
    height: 0;
    margin: 0;
  }
  studia-video[data-media-type="audio"] .sv-volume input[type="range"] {
    width: 64px;
  }
  studia-video[data-media-type="audio"] .sv-time {
    min-width: 70px;
    font-size: 11px;
  }

  /* ===== VIDEO compact: single-row, overflow menu, overlay piu' raccolto ===== */
  studia-video[data-media-type="video"] .sv-controls,
  studia-video:not([data-media-type]) .sv-controls {
    padding: 8px 10px;
  }
  studia-video[data-media-type="video"] .sv-controls-row,
  studia-video:not([data-media-type]) .sv-controls-row {
    gap: 4px;
  }
  studia-video[data-media-type="video"] .sv-time,
  studia-video:not([data-media-type]) .sv-time {
    min-width: 64px;
    font-size: 10.5px;
  }
  studia-video[data-media-type="video"] .sv-volume input[type="range"],
  studia-video:not([data-media-type]) .sv-volume input[type="range"] {
    display: none;
  }
  /* Bottone "more" visibile solo in compact video, non audio */
  studia-video[data-media-type="video"] .sv-more-btn,
  studia-video:not([data-media-type]) .sv-more-btn {
    display: inline-flex;
  }
  /* Default closed: nascondi i secondari, mostra solo essenziali */
  studia-video[data-media-type="video"]:not([data-more="open"]) .sv-quality-btn,
  studia-video[data-media-type="video"]:not([data-more="open"]) .sv-control-wrapper:has(> .sv-boost-btn),
  studia-video[data-media-type="video"]:not([data-more="open"]) .sv-control-wrapper:has(> .sv-speed-btn),
  studia-video[data-media-type="video"]:not([data-more="open"]) .sv-offline-btn,
  studia-video[data-media-type="video"]:not([data-more="open"]) .sv-info-btn,
  studia-video:not([data-media-type]):not([data-more="open"]) .sv-quality-btn,
  studia-video:not([data-media-type]):not([data-more="open"]) .sv-control-wrapper:has(> .sv-boost-btn),
  studia-video:not([data-media-type]):not([data-more="open"]) .sv-control-wrapper:has(> .sv-speed-btn),
  studia-video:not([data-media-type]):not([data-more="open"]) .sv-offline-btn,
  studia-video:not([data-media-type]):not([data-more="open"]) .sv-info-btn {
    display: none;
  }
  /* Open: secondari visibili, controls-row va a capo */
  studia-video[data-media-type="video"][data-more="open"] .sv-controls-row,
  studia-video:not([data-media-type])[data-more="open"] .sv-controls-row {
    flex-wrap: wrap;
    row-gap: 8px;
  }
  studia-video[data-media-type="video"][data-more="open"] .sv-controls-row > .sv-spacer,
  studia-video:not([data-media-type])[data-more="open"] .sv-controls-row > .sv-spacer {
    flex: 0 0 100%;
    height: 0;
    margin: 0;
  }
}

/* Extra-narrow audio (<360px): nascondi anche lo slider volume */
@container (max-width: 360px) {
  studia-video[data-media-type="audio"] .sv-volume input[type="range"] {
    display: none;
  }
}

/* ============================================================================
   FALLBACK per browser senza container query (Safari iOS 15-)
   Stessa logica, agganciata al viewport invece che al componente.
   ============================================================================ */
@supports not (container-type: inline-size) {
  @media (max-width: 480px) {
    studia-video[data-media-type="audio"] .sv-controls-row {
      flex-wrap: wrap;
      gap: 6px;
      row-gap: 8px;
    }
    studia-video[data-media-type="audio"] .sv-controls-row > .sv-spacer {
      flex: 0 0 100%;
      height: 0;
      margin: 0;
    }
    studia-video[data-media-type="audio"] .sv-volume input[type="range"] {
      width: 64px;
    }
    studia-video[data-media-type="audio"] .sv-time {
      min-width: 70px;
      font-size: 11px;
    }
    studia-video[data-media-type="video"] .sv-controls,
    studia-video:not([data-media-type]) .sv-controls {
      padding: 8px 10px;
    }
    studia-video[data-media-type="video"] .sv-time,
    studia-video:not([data-media-type]) .sv-time {
      min-width: 64px;
      font-size: 10.5px;
    }
    studia-video[data-media-type="video"] .sv-volume input[type="range"],
    studia-video:not([data-media-type]) .sv-volume input[type="range"] {
      display: none;
    }
    studia-video[data-media-type="video"] .sv-more-btn,
    studia-video:not([data-media-type]) .sv-more-btn {
      display: inline-flex;
    }
    studia-video[data-media-type="video"]:not([data-more="open"]) .sv-quality-btn,
    studia-video[data-media-type="video"]:not([data-more="open"]) .sv-offline-btn,
    studia-video[data-media-type="video"]:not([data-more="open"]) .sv-info-btn,
    studia-video:not([data-media-type]):not([data-more="open"]) .sv-quality-btn,
    studia-video:not([data-media-type]):not([data-more="open"]) .sv-offline-btn,
    studia-video:not([data-media-type]):not([data-more="open"]) .sv-info-btn {
      display: none;
    }
    studia-video[data-media-type="video"][data-more="open"] .sv-controls-row,
    studia-video:not([data-media-type])[data-more="open"] .sv-controls-row {
      flex-wrap: wrap;
      row-gap: 8px;
    }
    studia-video[data-media-type="video"][data-more="open"] .sv-controls-row > .sv-spacer,
    studia-video:not([data-media-type])[data-more="open"] .sv-controls-row > .sv-spacer {
      flex: 0 0 100%;
      height: 0;
      margin: 0;
    }
  }
  @media (max-width: 360px) {
    studia-video[data-media-type="audio"] .sv-volume input[type="range"] {
      display: none;
    }
  }
}
