/* ==========================================================================
   Media Home viewer page — dark + purple, RTL-first, matches the admin panel palette.
   All styling lives here (CSP: style-src 'self', no inline styles).
   ========================================================================== */

:root {
  color-scheme: dark;

  --bg: #07060d;
  --bg-2: #0c0a16;
  --bg-3: #120f1f;
  --surface: #171327;
  --surface-2: #1e1933;
  --border: #2a2340;

  --primary: #8b5cf6;
  --primary-2: #7c3aed;
  --primary-3: #6d28d9;
  --primary-light: #c4a7ff;

  --text: #e9e6f5;
  --muted: #a69fc0;
  --faint: #6f6890;

  --ok: #34d399;
  --warn: #fbbf24;
  --bad: #fb7185;

  --glow: rgba(139, 92, 246, .45);
  --grad: linear-gradient(135deg, #7c3aed 0%, #a855f7 50%, #c4a7ff 100%);
  --surface-grad: linear-gradient(160deg, rgba(30, 25, 51, .9) 0%, rgba(18, 15, 31, .9) 100%);
  --shadow-glow: 0 0 0 1px rgba(139, 92, 246, .25), 0 0 24px -4px rgba(139, 92, 246, .45);
  --shadow-glow-lg: 0 0 0 1px rgba(139, 92, 246, .30), 0 0 48px -6px rgba(139, 92, 246, .55);
  --shadow-soft: 0 8px 40px -12px rgba(124, 58, 237, .35);

  --font: Tajawal, Cairo, "Segoe UI", Tahoma, "Noto Sans Arabic", system-ui, sans-serif;
  --radius: 14px;
  --radius-sm: 10px;
  --topbar-h: 58px;
  --sidebar-w: 320px;
}

/* ---------- base ---------- */
*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  background:
    radial-gradient(1100px 600px at 85% -10%, rgba(124, 58, 237, .18), transparent 60%),
    radial-gradient(900px 500px at 0% 100%, rgba(168, 85, 247, .12), transparent 55%),
    var(--bg);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}

[hidden] { display: none !important; }

h1, h2, h3, p { margin: 0; }

button { font: inherit; color: inherit; }

:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* Purple-tinted scrollbars (same as the panel) */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: var(--bg-2); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 6px; }
::-webkit-scrollbar-thumb:hover { background: var(--primary-3); }
* { scrollbar-width: thin; scrollbar-color: var(--border) var(--bg-2); }

/* ---------- controls ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border-radius: 12px;
  padding: 9px 16px;
  font-size: 14px;
  font-weight: 600;
  line-height: 1.2;
  cursor: pointer;
  border: 1px solid transparent;
  background: transparent;
  white-space: nowrap;
  text-decoration: none;
  transition: box-shadow .15s ease, filter .15s ease, border-color .15s ease, background-color .15s ease;
}
.btn svg { width: 16px; height: 16px; flex: none; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-primary {
  background: var(--grad);
  color: #fff;
  box-shadow: var(--shadow-glow);
}
.btn-primary:hover:not(:disabled) { box-shadow: var(--shadow-glow-lg); filter: brightness(1.1); }

.btn-ghost {
  background: rgba(23, 19, 39, .6);
  border-color: var(--border);
  color: var(--text);
}
.btn-ghost:hover:not(:disabled) { border-color: rgba(139, 92, 246, .6); color: #fff; }

.btn-sm { padding: 6px 12px; font-size: 13px; border-radius: 10px; }
.btn-block { width: 100%; }

.icon-btn {
  width: 38px;
  height: 38px;
  flex: none;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(23, 19, 39, .6);
  color: var(--text);
  display: inline-grid;
  place-items: center;
  cursor: pointer;
}
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn:hover { border-color: rgba(139, 92, 246, .6); }

.input {
  width: 100%;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(12, 10, 22, .75);
  color: var(--text);
  padding: 10px 12px;
  font: inherit;
  font-size: 14px;
  line-height: 1.4;
  transition: border-color .15s ease, box-shadow .15s ease;
}
.input::placeholder { color: var(--faint); }
.input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(139, 92, 246, .35);
}
.input:disabled { opacity: .6; }
.input-ltr { direction: ltr; text-align: left; unicode-bidi: plaintext; }
input[type="search"]::-webkit-search-cancel-button { cursor: pointer; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  border-radius: 999px;
  padding: 1px 10px;
  font-size: 12px;
  font-weight: 600;
  line-height: 1.6;
}
.chip-bad { background: rgba(251, 113, 133, .12); color: var(--bad); }

.form-error {
  color: var(--bad);
  font-size: 13px;
  background: rgba(251, 113, 133, .08);
  border: 1px solid rgba(251, 113, 133, .3);
  border-radius: 10px;
  padding: 8px 12px;
}

/* ---------- brand ---------- */
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 46px;
  height: 46px;
  flex: none;
  border-radius: 13px;
  background: var(--grad);
  color: #fff;
  display: grid;
  place-items: center;
  box-shadow: var(--shadow-glow);
}
.brand-mark svg { width: 24px; height: 24px; }
.brand-mark-sm { width: 34px; height: 34px; border-radius: 10px; }
.brand-mark-sm svg { width: 18px; height: 18px; }
.brand-name {
  font-size: 24px;
  font-weight: 800;
  letter-spacing: .02em;
  line-height: 1.1;
  background: var(--grad);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 0 18px rgba(139, 92, 246, .35));
}
.brand-inline .brand-name { font-size: 18px; }
.brand-sub { color: var(--muted); font-size: 13px; margin-block-start: 2px; }

/* ---------- login view ---------- */
.view-login {
  min-height: 100%;
  overflow: auto;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}

.card {
  background: var(--surface-grad);
  border: 1px solid rgba(42, 35, 64, .8);
  border-radius: 20px;
  box-shadow: var(--shadow-soft);
  backdrop-filter: blur(6px);
}

.login-card {
  width: min(100%, 400px);
  padding: 32px 28px;
  display: grid;
  gap: 18px;
}
.login-card .brand { margin-block-end: 4px; }

.field { display: grid; gap: 6px; }
.field-label { font-size: 12px; font-weight: 600; color: var(--muted); }

/* ---------- player view ---------- */
.view-player {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.topbar {
  height: var(--topbar-h);
  flex: none;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  background: rgba(12, 10, 22, .85);
  backdrop-filter: blur(8px);
}

.account {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-inline-start: auto;
  min-width: 0;
  font-size: 13px;
  color: var(--muted);
}
.acct-user {
  color: var(--text);
  font-weight: 700;
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  unicode-bidi: isolate;
}
.acct-exp, .acct-conns { white-space: nowrap; }
.acct-conns:empty { display: none; }

.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
}

/* ---------- sidebar ---------- */
.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  border-inline-end: 1px solid var(--border);
  background: rgba(18, 15, 31, .75);
}

.search-wrap {
  flex: none;
  padding: 12px;
  border-bottom: 1px solid var(--border);
}

.history-panel {
  flex: none;
  max-height: 190px;
  overflow-y: auto;
  padding: 9px 8px 8px;
  border-bottom: 1px solid var(--border);
  background: rgba(12, 10, 22, .32);
}
.history-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 0 5px 6px;
}
.history-head h2 {
  font-size: 12px;
  color: var(--text);
  font-weight: 700;
}
.history-hint { color: var(--faint); font-size: 11px; }
.history-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 3px; }
.history-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-rows: auto auto;
  align-items: center;
  gap: 0 8px;
  width: 100%;
  padding: 6px 8px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  text-align: start;
  cursor: pointer;
}
.history-row:hover { background: rgba(139, 92, 246, .10); border-color: rgba(139, 92, 246, .28); }
.history-row.is-playing { background: rgba(139, 92, 246, .18); border-color: rgba(139, 92, 246, .48); }
.history-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12px;
  font-weight: 600;
}
.history-meta {
  grid-column: 1 / -1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: 11px;
}
.history-progress {
  width: 42px;
  height: 4px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--border);
  border: 0;
  appearance: none;
}
.history-progress::-webkit-progress-bar { background: var(--border); }
.history-progress::-webkit-progress-value { background: var(--primary); }
.history-progress::-moz-progress-bar { background: var(--primary); }
.history-sync { padding: 5px; font-size: 11px; color: var(--warn); }

.groups {
  flex: 0 1 auto;
  min-height: 0;
  max-height: 34%;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 6px;
  border-bottom: 1px solid var(--border);
}

.group-list, .channel-list { list-style: none; margin: 0; padding: 0; }

.group {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border: 0;
  border-inline-start: 3px solid transparent;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  border-radius: 8px;
  cursor: pointer;
  text-align: start;
  transition: background-color .12s ease, color .12s ease;
}
.group:hover { background: rgba(139, 92, 246, .10); color: var(--text); }
.group.is-active {
  background: rgba(139, 92, 246, .18);
  border-inline-start-color: var(--primary);
  color: #fff;
}
.group-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.count {
  margin-inline-start: auto;
  flex: none;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  color: var(--faint);
  background: rgba(42, 35, 64, .6);
  border-radius: 999px;
  padding: 1px 8px;
  min-width: 28px;
  text-align: center;
}
.group.is-active .count { color: var(--primary-light); background: rgba(124, 58, 237, .3); }

.channels-head {
  flex: none;
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 14px 4px;
  font-size: 12px;
  color: var(--faint);
}
.ch-loading { color: var(--primary-light); }

.channel-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 4px 6px 14px;
}

.ch {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 8px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  font-size: 14px;
  border-radius: 10px;
  cursor: pointer;
  text-align: start;
  transition: background-color .12s ease, border-color .12s ease;
}
.ch:hover { background: rgba(139, 92, 246, .10); }
/* Keyboard cursor while browsing results from the search box (↑/↓, Enter plays) */
.ch.is-cursor {
  background: rgba(139, 92, 246, .14);
  border-color: rgba(139, 92, 246, .55);
}
.ch.is-playing {
  background: linear-gradient(to left, rgba(139, 92, 246, .24), rgba(139, 92, 246, .06));
  border-color: rgba(139, 92, 246, .45);
  box-shadow: var(--shadow-glow);
}

.ch-logo {
  width: 36px;
  height: 36px;
  flex: none;
  border-radius: 8px;
  object-fit: contain;
  background: rgba(12, 10, 22, .8);
  border: 1px solid var(--border);
}
.ch-logo.ph {
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 14px;
  color: var(--primary-light);
  background: linear-gradient(160deg, rgba(124, 58, 237, .35), rgba(23, 19, 39, .9));
  user-select: none;
}
.ch-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.ch-num {
  flex: none;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  color: var(--faint);
}
.is-playing .ch-num { color: var(--primary-light); }

.list-empty {
  padding: 28px 12px;
  text-align: center;
  color: var(--faint);
  font-size: 13px;
}
.more { margin-block-start: 10px; }

/* ---------- main / stage ---------- */
.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  gap: 12px;
  padding: 16px;
}

.stage {
  position: relative;
  flex: 1;
  min-height: 0;
  background: #000;
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-soft);
}
.stage video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
}
.stage:fullscreen { border-radius: 0; border: 0; box-shadow: none; }
.stage:-webkit-full-screen { border-radius: 0; border: 0; box-shadow: none; }

.stage-empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 24px;
  overflow-y: auto;
  text-align: center;
  color: var(--muted);
  background:
    radial-gradient(600px 320px at 50% 60%, rgba(124, 58, 237, .20), transparent 70%),
    var(--bg-2);
}
.stage-empty-icon { color: var(--primary); opacity: .9; filter: drop-shadow(0 0 22px rgba(139, 92, 246, .45)); }
.stage-empty-icon svg { width: 72px; height: 72px; }
.stage-empty-title { font-size: 17px; font-weight: 600; color: var(--text); }
.stage-empty-copy { font-size: 14px; max-width: 440px; }
.stage-empty-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; }
.stage-empty-actions .btn { white-space: normal; max-width: 100%; overflow-wrap: anywhere; }
.hint { font-size: 12px; color: var(--faint); }
.mobile-only { display: none; }

.spinner {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: rgba(7, 6, 13, .35);
  pointer-events: none;
  color: var(--muted);
  font-size: 13px;
}
.spinner-ring {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 3px solid rgba(139, 92, 246, .25);
  border-top-color: var(--primary);
  box-shadow: 0 0 24px -4px var(--glow);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.unmute {
  position: absolute;
  inset-block-start: 14px;
  inset-inline-start: 14px;
  z-index: 2;
}

.infobar {
  flex: none;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 14px;
}
.now {
  font-weight: 600;
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.now span { color: var(--primary-light); }
.status {
  flex: 1;
  min-width: 140px;
  font-size: 13px;
  color: var(--muted);
  overflow-wrap: anywhere;
}
.status.is-error { color: var(--bad); font-weight: 600; }
.status.is-ok { color: var(--ok); }
.status.is-ok::before {
  content: "";
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--ok);
  box-shadow: 0 0 10px var(--ok);
  margin-inline-end: 6px;
  vertical-align: middle;
}
.infobar .btn:last-child { margin-inline-start: auto; }

/* ---------- mobile drawer ---------- */
.menu-btn { display: none; }
.backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(7, 6, 13, .65);
  display: none;
}

@media (max-width: 800px) {
  :root { --topbar-h: 54px; }

  .menu-btn { display: inline-grid; }
  .mobile-only { display: inline-flex; }
  .backdrop { display: block; }
  .hint { display: none; }

  .topbar { padding: 0 12px; gap: 10px; }
  .acct-exp, .acct-conns { display: none; }
  .acct-user { max-width: 120px; }

  .layout { grid-template-columns: minmax(0, 1fr); }

  .sidebar {
    position: fixed;
    inset-block: var(--topbar-h) 0;
    inset-inline-start: 0;
    width: min(86vw, 340px);
    z-index: 50;
    background: var(--bg-3);
    border-inline-end: 0;
    border-inline-start: 1px solid var(--border);
    box-shadow: -8px 0 40px rgba(0, 0, 0, .55);
    transform: translateX(100%);
    transition: transform .22s ease;
  }
  .sidebar.is-open { transform: none; }

  .main { padding: 10px; gap: 10px; }
  .stage { border-radius: 12px; }
}

@media (max-width: 420px) {
  #fs-label { display: none; }
  .login-card { padding: 26px 20px; }
}

/* Short viewports (phones in landscape): keep the video usable */
@media (max-height: 460px) {
  .stage-empty-icon { display: none; }
  .main { padding: 6px; gap: 6px; }
}

/* ---------- reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
  .btn, .input, .group, .ch, .sidebar, .mode-tab, .episode-row { transition: none; }
  .spinner-ring {
    animation: none;
    border-color: var(--primary);
    border-top-color: var(--primary-light);
  }
}

/* ---------- content-type tabs (Live / Movies / Series) ---------- */
.mode-tabs {
  display: flex;
  flex: none;
  gap: 4px;
  padding: 10px 12px 0;
  border-bottom: 1px solid var(--border);
}
.mode-tab {
  flex: 1;
  min-width: 0;
  padding: 8px 10px;
  border: 0;
  border-radius: 10px 10px 0 0;
  background: transparent;
  color: var(--muted);
  font-weight: 600;
  cursor: pointer;
  transition: background .12s, color .12s;
}
.mode-tab:hover { color: var(--text); background: rgba(139, 92, 246, .10); }
.mode-tab.is-active {
  color: #fff;
  background: linear-gradient(180deg, rgba(124,58,237,.25), rgba(124,58,237,.05));
  box-shadow: inset 0 -2px 0 var(--primary);
}
.mode-tab:focus-visible { outline-offset: -3px; }

/* ---------- series detail view ---------- */
.series-detail {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  min-height: 0;
  min-width: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--bg-2);
}
.series-detail-head {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
.series-detail-title {
  font-size: 18px;
  color: var(--text);
  margin: 0;
  min-width: 0;
  overflow-wrap: anywhere;
}
.series-detail-body { display: grid; gap: 12px; }
.series-detail-body > .btn { justify-self: center; }
.series-seasons {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.label-sm { color: var(--muted); font-size: 13px; }
.series-seasons .input { max-width: 220px; }
.episode-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.episode-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  text-align: start;
  transition: background .12s, border-color .12s;
  width: 100%;
}
.episode-row:hover { background: rgba(139, 92, 246, .08); border-color: var(--primary-3); }
.episode-row.is-playing { border-color: var(--primary); background: rgba(139, 92, 246, .18); }
.episode-num { flex: none; color: var(--primary-light); font-variant-numeric: tabular-nums; min-width: 40px; }
.episode-title { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.episode-ext { flex: none; color: var(--muted); font-size: 12px; }

/* ---------- external-player panel (MKV/AVI) ---------- */
.ext-player {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(7, 6, 13, .92);
  padding: 24px;
  overflow-y: auto;
}
.ext-player-inner {
  max-width: 480px;
  width: 100%;
  min-width: 0;
  margin-block: auto;
  flex: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: var(--shadow-glow);
}
.ext-player-title { font-size: 16px; font-weight: 700; color: var(--text); }
.ext-player-hint { color: var(--muted); font-size: 13px; }
.ext-player-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.ext-player-actions .btn { flex: 1; }

@media (max-width: 800px) {
  .series-detail { padding: 12px; border-radius: 12px; }
  .ext-player { padding: 16px; }
}

@media (max-height: 460px) {
  .stage-empty { justify-content: flex-start; gap: 8px; padding: 12px; }
  .ext-player { padding: 12px; align-items: flex-start; }
}
