/* =========================================================
   VORTEX READER — مشغّل موسيقى الفصل (OST)
   خفيف ولا يغطي القراءة: بطاقة عائمة على الكمبيوتر،
   وشريط سفلي منزلق على الجوال. يعتمد على متغيّرات القارئ.
   ========================================================= */

/* الزر لا يظهر إلا إذا كان للعمل موسيقى */
#toggle-music { display: none; position: relative; }
body.has-music #toggle-music { display: grid; }
#toggle-music.music-on { color: var(--accent-2); }
#toggle-music.music-on::after {
    content: ""; position: absolute; top: 7px; inset-inline-end: 7px;
    width: 6px; height: 6px; border-radius: 50%; background: var(--accent-2);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent-2) 22%, transparent);
}

/* ---------- الحاوية ---------- */
.music-panel {
    position: fixed; z-index: 880;
    inset-inline-end: 16px; bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    width: min(372px, calc(100vw - 32px));
    max-height: min(740px, calc(100dvh - 100px));
    display: flex; flex-direction: column; overflow: hidden;
    background: color-mix(in srgb, var(--panel) 96%, transparent);
    border: 1px solid var(--line-2); border-radius: 18px;
    box-shadow: 0 28px 70px rgba(0, 0, 0, 0.42), inset 0 1px 0 rgba(255, 255, 255, 0.04);
    backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
    opacity: 0; visibility: hidden; transform: translateY(14px) scale(0.98);
    transition: opacity 220ms ease, transform 260ms cubic-bezier(0.2, 0.7, 0.2, 1), visibility 0s linear 260ms;
}
.music-panel.open { opacity: 1; visibility: visible; transform: none; transition-delay: 0s; }

/* ---------- الرأس ---------- */
.mp-head { display: flex; align-items: center; gap: 10px; padding: 16px 16px 12px 18px; }
.mp-title-wrap { flex: 1; min-width: 0; }
.mp-title-wrap small { display: block; font-family: var(--mono); font-size: 10px; letter-spacing: 0.2em; color: var(--accent-2); }
.mp-title-wrap h2 { margin: 2px 0 0; font-size: 16px; font-weight: 700; line-height: 1.3; }
.mp-count { font-family: var(--mono); font-size: 11.5px; color: var(--mute); padding: 4px 9px; border: 1px solid var(--line); border-radius: 999px; white-space: nowrap; }
.mp-close { width: 36px; height: 36px; display: grid; place-items: center; border-radius: 50%; color: var(--mute); flex-shrink: 0; transition: background 150ms, color 150ms; }
.mp-close:hover { background: var(--line); color: var(--text); }

/* ---------- المقطع الحالي ---------- */
.mp-now { display: flex; align-items: center; gap: 14px; padding: 4px 18px 10px; }
.mp-disc {
    flex-shrink: 0; width: 52px; height: 52px; border-radius: 50%; display: grid; place-items: center;
    background: radial-gradient(circle at 50% 50%, var(--raised) 0 30%, var(--panel) 31% 33%, var(--raised) 34%);
    border: 1px solid var(--line-2); color: var(--accent-2);
}
.mp-disc svg { width: 18px; height: 18px; }
.mp-panel-playing .mp-disc, .music-panel.is-playing .mp-disc { animation: mpSpin 6s linear infinite; }
@keyframes mpSpin { to { transform: rotate(360deg); } }
.mp-track { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.mp-track b { font-size: 14.5px; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mp-track small { font-size: 12.5px; color: var(--mute); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- شريط الموضع ---------- */
.mp-seek { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 10px; padding: 0 18px; font-family: var(--mono); font-size: 11.5px; color: var(--mute); font-variant-numeric: tabular-nums; }

/* ---------- أزرار التحكم ---------- */
.mp-ctrl { display: flex; align-items: center; justify-content: center; gap: 18px; padding: 12px 18px 14px; }
.mp-side { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 50%; color: var(--text); transition: background 150ms; }
.mp-side:hover { background: var(--line); }
.mp-play { width: 58px; height: 58px; display: grid; place-items: center; border-radius: 50%; background: var(--accent); color: var(--on-accent); box-shadow: 0 10px 26px color-mix(in srgb, var(--accent) 35%, transparent); transition: transform 150ms ease, filter 150ms; }
.mp-play:hover { filter: brightness(1.06); }
.mp-play:active { transform: scale(0.96); }
.mp-play svg { width: 22px; height: 22px; }

/* ---------- الإعدادات (تجميعة أزرار) ---------- */
.mp-set { display: grid; gap: 10px; padding: 12px 18px; border-top: 1px solid var(--line); }
.mp-seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; padding: 3px; gap: 3px; background: var(--raised); border: 1px solid var(--line); border-radius: 11px; }
.mp-seg button { min-height: 34px; padding: 0 8px; border-radius: 8px; font-size: 12.5px; font-weight: 600; color: var(--mute); transition: background 150ms, color 150ms; }
.mp-seg button:hover { color: var(--text); }
.mp-seg button.active { background: var(--panel); color: var(--accent-2); box-shadow: 0 1px 0 rgba(0, 0, 0, 0.25), inset 0 0 0 1px var(--line-2); }
.mp-vol { display: grid; grid-template-columns: auto 1fr; align-items: center; gap: 12px; font-size: 12.5px; color: var(--mute); }

/* ---------- قائمة المقاطع ---------- */
.mp-bulk { display: flex; gap: 8px; padding: 10px 18px 0; }
.mp-bulk button { min-height: 34px; padding: 0 12px; border-radius: 9px; font-size: 12.5px; font-weight: 600; border: 1px solid var(--line-2); color: var(--text); transition: background 150ms; }
.mp-bulk button:hover { background: var(--line); }
.mp-empty { margin: 10px 18px 0; font-size: 12.5px; line-height: 1.8; color: var(--mute); }
.mp-empty:empty, .mp-empty.hidden { display: none; }
.mp-list { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 0 8px 16px; margin-top: 8px; border-top: 1px solid var(--line); -webkit-overflow-scrolling: touch; }
.mp-cat { position: sticky; top: -8px; z-index: 1; display: flex; align-items: center; gap: 8px; margin: 0; padding: 12px 10px 8px; background: var(--panel); font-size: 12px; font-family: var(--mono); letter-spacing: 0.08em; color: var(--mute); font-weight: 600; }
.mp-cat svg { width: 13px; height: 13px; color: var(--accent-2); }
.mp-cat span { flex: 1; }
.mp-cat small { font-size: 11px; }
.mp-row { display: grid; grid-template-columns: 30px minmax(0, 1fr) auto; align-items: center; gap: 8px; min-height: 60px; padding: 8px 12px 8px 10px; border-radius: 13px; transition: background 150ms; }
.mp-row:hover { background: var(--raised); }
.mp-row.playing { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.mp-row.muted .mp-play-row { opacity: 0.45; }
.mp-row input[type="checkbox"] { width: 22px; height: 22px; margin: 0 auto; accent-color: var(--accent); cursor: pointer; }
.mp-play-row { min-height: 48px; min-width: 0; display: flex; flex-direction: column; justify-content: center; align-items: flex-start; gap: 3px; text-align: start; padding: 4px 6px; }
.mp-play-row b { font-size: 15px; font-weight: 600; line-height: 1.4; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mp-play-row small { font-size: 12.5px; color: var(--mute); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mp-row.playing .mp-play-row b { color: var(--accent-2); }
.mp-dur { font-family: var(--mono); font-size: 12px; color: var(--mute); padding-inline: 4px; font-variant-numeric: tabular-nums; }

/* ---------- عنصر تمرير ومنزلقات ---------- */
.music-panel input[type="range"] { -webkit-appearance: none; appearance: none; width: 100%; height: 22px; background: transparent; border: 0; border-radius: 0; box-shadow: none; padding: 0; cursor: pointer; margin: 0; }
.music-panel input[type="range"]::-webkit-slider-runnable-track { height: 4px; border-radius: 999px; background: linear-gradient(to left, var(--line-2), var(--line-2)); }
.music-panel input[type="range"]::-moz-range-track { height: 4px; border-radius: 999px; background: var(--line-2); }
.music-panel input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; width: 14px; height: 14px; margin-top: -5px; border-radius: 50%; background: var(--accent); border: 0; box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 22%, transparent); }
.music-panel input[type="range"]::-moz-range-thumb { width: 14px; height: 14px; border-radius: 50%; background: var(--accent); border: 0; }
.music-panel input[type="range"]:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 4px; border-radius: 6px; }
.music-panel ::-webkit-scrollbar { width: 6px; }
.music-panel ::-webkit-scrollbar-thumb { background: var(--line-2); border-radius: 999px; }

/* ---------- الجوال: شريط سفلي بعرض الشاشة ---------- */
@media (max-width: 640px) {
    .music-panel {
        inset-inline: 8px;
        bottom: calc(8px + env(safe-area-inset-bottom, 0px));
        width: auto;
        max-height: min(88dvh, 760px);
        border-radius: 20px;
        transform: translateY(24px);
    }
    .mp-ctrl { gap: 14px; padding: 8px 18px 10px; }
    .mp-now { padding: 2px 18px 8px; }
    .mp-disc { width: 44px; height: 44px; }
    .mp-set { padding: 10px 18px; gap: 8px; }
    .mp-play { width: 52px; height: 52px; }
    .mp-row { min-height: 64px; }
    .mp-seg button { min-height: 40px; }
}

@media (prefers-reduced-motion: reduce) {
    .music-panel, .mp-play, .mp-row, .mp-seg button { transition: none !important; }
    .mp-panel-playing .mp-disc, .music-panel.is-playing .mp-disc { animation: none; }
}

/* ---------- إصلاحات v2: الموضع فوق الهيدر، وأشرطة التقدم والصوت من اليسار لليمين ---------- */
.music-panel { max-height: min(740px, calc(100dvh - 150px)); }
.mp-seek { direction: ltr; }
.music-panel input[type="range"] { direction: ltr; }

/* ---------- v4: النافذة ثابتة، والتمرير داخل قائمة المقاطع فقط ----------
   الرأس + المقطع الحالي + أزرار التحكم + الإعدادات + (تفعيل/إيقاف الكل) تبقى ثابتة،
   وقائمة المقاطع وحدها هي التي تُمرَّر. */
.music-panel { overflow: hidden; }
.music-panel > .mp-head,
.music-panel > .mp-now,
.music-panel > .mp-seek,
.music-panel > .mp-ctrl,
.music-panel > .mp-set,
.music-panel > .mp-bulk,
.music-panel > .mp-empty { flex-shrink: 0; }
.music-panel .mp-list {
    flex: 1 1 auto; min-height: 150px; max-height: none;
    overflow-y: auto; overflow-x: hidden;
    overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
}
.music-panel .mp-cat { top: 0; }

/* شاشات قصيرة: ضغط الجزء الثابت ليبقى للقائمة مساحة كافية */
@media (max-height: 760px) {
    .mp-head { padding-top: 10px; padding-bottom: 6px; }
    .mp-now { padding-bottom: 4px; }
    .mp-disc { width: 40px; height: 40px; }
    .mp-ctrl { padding: 4px 18px 6px; gap: 12px; }
    .mp-side { width: 38px; height: 38px; }
    .mp-play { width: 46px; height: 46px; }
    .mp-set { padding: 8px 18px; gap: 6px; }
    .mp-seg button { min-height: 32px; }
    .mp-bulk { padding-top: 6px; }
    .mp-row { min-height: 56px; }
}

/* جوال بالعرض (ارتفاع صغير جدًا): عمودان — التحكم ثابت على جانب، والقائمة تُمرَّر على الجانب الآخر */
@media (max-height: 520px) {
    .music-panel {
        inset-inline: 8px; width: auto; max-height: calc(100dvh - 16px);
        bottom: calc(8px + env(safe-area-inset-bottom, 0px));
        display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
        grid-auto-rows: min-content; align-content: start;
    }
    .music-panel > .mp-list { grid-column: 2; grid-row: 1 / span 8; min-height: 0; max-height: calc(100dvh - 16px); border-top: 0; margin-top: 0; }
    .music-panel > :not(.mp-list) { grid-column: 1; }
}

/* ---------- v5: نافذة عائمة قابلة للسحب وتغيير الحجم والتكبير ---------- */
.music-panel.mp-floating { max-height: none; }
.music-panel.mp-moving { transition: none !important; user-select: none; }
.mp-head { cursor: grab; touch-action: none; }
.mp-head button, .mp-head .mp-count { cursor: pointer; }
.mp-head.mp-dragging, .music-panel.mp-moving .mp-head { cursor: grabbing; }
.mp-max { width: 36px; height: 36px; display: grid; place-items: center; border-radius: 50%; color: var(--mute); flex-shrink: 0; transition: background 150ms, color 150ms; }
.mp-max:hover { background: var(--line); color: var(--text); }
.mp-max svg { width: 16px; height: 16px; }
.mp-resize {
    position: absolute; inset-inline-start: 0; bottom: 0; width: 18px; height: 18px;
    cursor: nwse-resize; touch-action: none; z-index: 2; display: none;
    background:
        linear-gradient(to top right, transparent 0 46%, var(--line-2) 47% 53%, transparent 54% 100%),
        linear-gradient(to top right, transparent 0 66%, var(--line-2) 67% 73%, transparent 74% 100%);
    opacity: .9;
}
.music-panel.mp-floating .mp-resize { display: block; }
.music-panel.mp-floating { overflow: hidden; }
.music-panel.mp-floating .mp-list { min-height: 0; }
/* الوضع المكبّر: يملأ الشاشة تحت الهيدر */
.music-panel.mp-maxed {
    inset: calc(env(safe-area-inset-top, 0px) + 8px) 8px calc(env(safe-area-inset-bottom, 0px) + 8px) 8px !important;
    width: auto !important; height: auto !important; max-height: none !important;
    transform: none; left: 8px; top: 8px; right: 8px; bottom: 8px;
}
.music-panel.mp-maxed .mp-resize { display: none; }
.music-panel.mp-maxed .mp-list { min-height: 0; }
@media (max-width: 640px) {
    .mp-resize { display: none !important; }
    .mp-max { display: none; }
    .mp-head { cursor: default; }
}
@media (prefers-reduced-motion: reduce) { .music-panel.mp-moving { transition: none !important; } }
