.ape-playlist {
  --ape-accent: #2563eb;
  --ape-card-bg: #fff;
  --ape-list-bg: #f8fafc;
  --ape-item-bg: #fff;
  --ape-active-bg: #eaf2ff;
  --ape-text: #0f172a;
  --ape-muted: #64748b;
  --ape-border: #e2e8f0;
  --ape-radius: 18px;
  --ape-gap: 20px;
  --ape-item-gap: 8px;
  --ape-list-width: 34%;
  --ape-list-max-height: 620px;
  --ape-player-ratio: 16 / 9;
  --ape-hover-lift: -1px;
  --ape-press-scale: .985;
  color: var(--ape-text);
  font-family: inherit;
  container-type: inline-size;
}

.ape-playlist,
.ape-playlist *,
.ape-playlist *::before,
.ape-playlist *::after { box-sizing: border-box; }

.ape-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--ape-list-width);
  grid-template-areas: "player sidebar";
  gap: var(--ape-gap);
  align-items: start;
  direction: ltr;
}

.ape-player-column, .ape-sidebar { direction: rtl; }

.ape-player-column { grid-area: player; min-width: 0; }
.ape-sidebar { grid-area: sidebar; min-width: 0; }

.ape-player-column {
  background: var(--ape-card-bg);
  border-radius: var(--ape-radius);
}

.ape-player-stage {
  position: relative;
  overflow: hidden;
  border-radius: var(--ape-radius);
  background: #020617;
  aspect-ratio: var(--ape-player-ratio);
  box-shadow: 0 12px 40px rgba(15, 23, 42, .08);
  isolation: isolate;
}

.ape-player {
  width: 100%;
  height: 100%;
  border: 0;
  display: block;
  background: #020617;
}

.ape-player-feedback {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: grid;
  place-items: center;
  padding: 20px;
  color: #fff;
  background: rgba(2, 6, 23, .32);
  opacity: 0;
  pointer-events: none;
  transition: opacity 140ms ease-out;
}

.ape-player-feedback span {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(2, 6, 23, .76);
  font-size: 12px;
  line-height: 1.5;
}

.ape-player-stage[aria-busy="true"] .ape-player-feedback { opacity: 1; }
.ape-player-stage[aria-busy="true"] .ape-player { opacity: .94; }

.ape-video-meta { padding-block: 18px 4px; padding-inline: 4px; }
.ape-current-title {
  margin: 0;
  color: var(--ape-text);
  font-size: clamp(20px, 2vw, 30px);
  line-height: 1.4;
  font-weight: 700;
}
.ape-current-description {
  margin-block-start: 9px;
  color: var(--ape-muted);
  line-height: 1.9;
  font-size: 14px;
  white-space: pre-line;
}

.ape-sidebar {
  background: var(--ape-list-bg);
  border: 1px solid var(--ape-border);
  border-radius: var(--ape-radius);
  padding: 14px;
  overflow: hidden;
  box-shadow: 0 12px 40px rgba(15, 23, 42, .05);
}

.ape-sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-block: 4px 12px;
  padding-inline: 6px;
}
.ape-sidebar-head h3 { margin: 0; font-size: 18px; line-height: 1.45; color: var(--ape-text); }
.ape-sidebar-head span { color: var(--ape-muted); font-size: 12px; white-space: nowrap; }

.ape-items {
  list-style: none;
  margin: 0;
  padding: 0 0 0 2px;
  display: flex;
  flex-direction: column;
  gap: var(--ape-item-gap);
  max-height: var(--ape-list-max-height);
  overflow: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: color-mix(in srgb, var(--ape-muted), transparent 35%) transparent;
}

.ape-item-shell { margin: 0; padding: 0; min-width: 0; }

.ape-item {
  position: relative;
  width: 100%;
  min-height: 56px;
  border: 1px solid transparent;
  background: var(--ape-item-bg);
  color: var(--ape-text);
  border-radius: calc(var(--ape-radius) * .65);
  padding: 8px;
  display: grid;
  grid-template-columns: auto auto minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  text-align: start;
  cursor: pointer;
  font: inherit;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition: transform 170ms cubic-bezier(.2, .8, .2, 1), border-color 170ms ease-out, background-color 170ms ease-out, box-shadow 170ms ease-out;
}

.ape-item::before {
  content: "";
  position: absolute;
  inset-block: 12px;
  inset-inline-start: 0;
  width: 3px;
  border-radius: 999px;
  background: var(--ape-accent);
  opacity: 0;
  transform: scaleY(.4);
  transform-origin: center;
  transition: opacity 150ms ease-out, transform 180ms cubic-bezier(.2, .8, .2, 1);
}

@media (hover: hover) and (pointer: fine) {
  .ape-motion-on .ape-item:hover {
    transform: translateY(var(--ape-hover-lift));
    border-color: #bfdbfe;
    border-color: color-mix(in srgb, var(--ape-accent), transparent 80%);
    box-shadow: 0 8px 24px rgba(15, 23, 42, .06);
  }
}

.ape-motion-on .ape-item:active {
  transform: scale(var(--ape-press-scale));
  transition-duration: 0ms;
}

.ape-item:focus-visible {
  outline: 3px solid #93c5fd;
  outline-color: color-mix(in srgb, var(--ape-accent), transparent 62%);
  outline-offset: 2px;
  border-color: var(--ape-accent);
}

.ape-item.is-active {
  background: var(--ape-active-bg);
  border-color: #93c5fd;
  border-color: color-mix(in srgb, var(--ape-accent), transparent 58%);
}
.ape-item.is-active::before { opacity: 1; transform: scaleY(1); }

.ape-index { color: var(--ape-muted); font-size: 12px; min-width: 18px; text-align: center; }
.ape-item.is-active .ape-index { color: var(--ape-accent); font-weight: 700; }

.ape-thumb {
  position: relative;
  width: 104px;
  aspect-ratio: 16 / 9;
  overflow: hidden;
  border-radius: max(8px, calc(var(--ape-radius) * .5));
  background: #e2e8f0;
  display: block;
}
.ape-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.ape-thumb-placeholder {
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, #dbeafe, #e2e8f0);
  color: var(--ape-accent);
  font-size: 20px;
}
.ape-duration {
  position: absolute;
  inset-inline-end: 5px;
  inset-block-end: 5px;
  background: rgba(2, 6, 23, .86);
  color: #fff;
  border-radius: 6px;
  padding: 2px 5px;
  font-size: 10px;
  direction: ltr;
  line-height: 1.3;
}

.ape-item-copy { min-width: 0; display: flex; flex-direction: column; gap: 5px; }
.ape-item-copy strong {
  font-size: 14px;
  line-height: 1.55;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.ape-item-copy small {
  color: var(--ape-muted);
  line-height: 1.55;
  font-size: 11px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ape-empty {
  padding: 18px;
  border: 1px dashed #cbd5e1;
  border-radius: 12px;
  background: #f8fafc;
  color: #475569;
  text-align: center;
}

.ape-sr-only {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

.ape-motion-off .ape-item,
.ape-motion-off .ape-item::before,
.ape-motion-off .ape-player-feedback { transition: none; }
.ape-motion-off .ape-item:hover,
.ape-motion-off .ape-item:active { transform: none; }

@container (max-width: 820px) {
  .ape-layout {
    grid-template-columns: 1fr;
    grid-template-areas: "player" "sidebar";
  }
  .ape-sidebar { padding: 12px; }
  .ape-items {
    /*
     * Mobile stays horizontal, but uses Flex instead of a one-row Grid.
     * A Grid row is sized by the tallest title in the entire playlist; after
     * long Persian titles were allowed to wrap fully, that shared row height
     * could reserve a large blank block below shorter cards. Flex lets every
     * card keep its own intrinsic block-size while preserving horizontal swipe.
     */
    display: flex;
    flex-flow: row nowrap;
    align-items: flex-start;
    overflow-x: auto;
    overflow-y: hidden;
    max-height: none;
    padding-block-end: 4px;
    scroll-snap-type: inline proximity;
    overscroll-behavior-inline: contain;
  }
  .ape-item-shell {
    flex: 0 0 78%;
    width: 78%;
    min-width: 250px;
    max-width: 100%;
    scroll-snap-align: start;
    height: auto;
    min-height: 0;
    align-self: flex-start;
  }

  .ape-playlist,
  .ape-layout,
  .ape-player-column,
  .ape-sidebar,
  .ape-items {
    height: auto;
    min-height: 0;
  }

  .ape-layout {
    align-content: start;
    grid-auto-rows: max-content;
  }

  .ape-sidebar {
    align-self: start;
  }

  .ape-item {
    height: auto;
    min-height: 56px;
    align-self: auto;
  }
}

@container (max-width: 520px) {
  .ape-sidebar { padding: 10px; }
  .ape-sidebar-head { padding-inline: 4px; }

  /*
   * Keep the touch-friendly horizontal playlist, but give the title priority.
   * A card uses almost the full container width, the index becomes an overlay,
   * and titles are never line-clamped on phones.
   */
  .ape-items {
    gap: max(8px, var(--ape-item-gap));
  }

  .ape-item-shell {
    flex-basis: min(92%, 390px);
    width: min(92%, 390px);
    min-width: 0;
    max-width: min(92%, 390px);
  }

  .ape-item {
    grid-template-columns: 76px minmax(0, 1fr);
    min-height: 94px;
    padding: 9px;
    gap: 10px;
    align-items: center;
  }

  .ape-index {
    position: absolute;
    z-index: 2;
    inset-block-start: 6px;
    inset-inline-start: 6px;
    min-width: 24px;
    min-height: 24px;
    padding-inline: 6px;
    display: inline-grid;
    place-items: center;
    border-radius: 999px;
    background: rgba(255, 255, 255, .92);
    box-shadow: 0 2px 8px rgba(15, 23, 42, .10);
    color: var(--ape-muted);
    font-size: 11px;
    line-height: 1;
    pointer-events: none;
  }

  .ape-item.is-active .ape-index {
    background: var(--ape-accent);
    color: #fff;
  }

  .ape-thumb { width: 76px; }

  .ape-item-copy {
    gap: 3px;
    justify-content: center;
  }

  .ape-item-copy strong {
    display: block;
    overflow: visible;
    text-overflow: clip;
    -webkit-line-clamp: unset;
    -webkit-box-orient: initial;
    font-size: 13.5px;
    line-height: 1.65;
    white-space: normal;
    overflow-wrap: anywhere;
    word-break: normal;
    text-wrap: pretty;
  }

  /* Secondary copy stays compact so the complete title remains dominant. */
  .ape-item-copy small {
    display: -webkit-box;
    overflow: hidden;
    white-space: normal;
    text-overflow: ellipsis;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1;
    line-clamp: 1;
  }

  .ape-current-title { font-size: 20px; }
  .ape-video-meta { padding-inline: 2px; padding-block-start: 14px; }
}

@media (max-width: 820px) {
  .ape-layout {
    grid-template-columns: 1fr;
    grid-template-areas: "player" "sidebar";
  }

  /* Prevent inherited/flex stretching from reserving space below the widget. */
  .ape-playlist {
    block-size: auto;
    min-block-size: 0;
    margin-block-end: 0;
    padding-block-end: 0;
  }

  .ape-playlist > .ape-layout {
    block-size: auto;
    min-block-size: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .ape-playlist *,
  .ape-playlist *::before,
  .ape-playlist *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
  .ape-playlist .ape-item:hover,
  .ape-playlist .ape-item:active { transform: none !important; }
}

@media (prefers-reduced-transparency: reduce) {
  .ape-player-feedback { background: #111827; }
  .ape-player-feedback span { background: #020617; }
}
