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

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
  background: #04080f;
  color: #F2EFE9;
  overflow: hidden;
  overscroll-behavior: none;
  font-family: "IBM Plex Sans Arabic", "Tajawal", system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

body { user-select: none; -webkit-user-select: none; }

:root {
  --ink: #F2EFE9;
  --muted: rgba(242, 239, 233, 0.52);
  --faint: rgba(242, 239, 233, 0.24);
  --accent: #E5321F;
  --bar-h: clamp(22px, 4.4vh, 46px);
  --pad: clamp(12px, 3.6vw, 26px);
}

#stage {
  position: fixed;
  inset: 0;
  overflow: hidden;
  background: #04080f;
}

#gl {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

/* ---------- طبقات جوية ---------- */
.scrim-top {
  position: absolute;
  inset: 0 0 auto 0;
  height: calc(var(--bar-h) + 72px);
  background: linear-gradient(to bottom, rgba(2, 5, 9, 0.88), rgba(2, 5, 9, 0));
  pointer-events: none;
  z-index: 3;
}

.vignette {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 2;
  background:
    radial-gradient(130% 90% at 50% 42%, rgba(0, 0, 0, 0) 42%, rgba(0, 0, 0, 0.42) 100%);
}

/* ---------- الشريط العلوي ---------- */
.hud-top {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: var(--bar-h);
  min-height: 22px;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 0 var(--pad);
  z-index: 6;
  font-size: clamp(9.5px, 2.5vw, 11.5px);
  line-height: 1;
  letter-spacing: 0.01em;
  white-space: nowrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--ink);
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--accent);
  flex: 0 0 auto;
}

.chapter-ind {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  min-width: 0;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.94em;
}

.chapter-ind .dim { opacity: 0.55; }

.chapter-ind .slash { opacity: 0.4; }

.chap-name {
  font-family: "IBM Plex Sans Arabic", sans-serif;
  color: var(--ink);
  font-size: 1em;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 46vw;
  margin-inline-start: 4px;
  padding-inline-start: 8px;
  border-inline-start: 1px solid rgba(255, 255, 255, 0.18);
}

/* ---------- لوحة التحكم السفلية ---------- */
.hud-bottom {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 6;
  padding: 34px var(--pad) calc(14px + env(safe-area-inset-bottom, 0px) * 0);
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: linear-gradient(to top, rgba(3, 6, 11, 0.96) 0%, rgba(3, 6, 11, 0.78) 42%, rgba(3, 6, 11, 0) 100%);
}

/* شريط التقدم */
.track {
  position: relative;
  height: 30px;
  direction: ltr;
  display: flex;
  align-items: center;
  cursor: pointer;
  touch-action: none;
  outline: none;
}
.track:focus-visible .track-base { box-shadow: 0 0 0 2px rgba(229, 50, 31, 0.55); }

.track-base {
  position: absolute;
  left: 0;
  right: 0;
  height: 1px;
  background: rgba(255, 255, 255, 0.34);
}

.track-fill {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  height: 2px;
  width: 0%;
  background: var(--accent);
  box-shadow: 0 0 10px rgba(229, 50, 31, 0.65);
}

.track-ticks { position: absolute; inset: 0; }

.tick {
  position: absolute;
  top: 50%;
  width: 26px;
  height: 26px;
  margin-left: -13px;
  margin-top: -13px;
  border: 0;
  background: none;
  display: grid;
  place-items: center;
  padding: 0;
  cursor: pointer;
}
.tick i {
  display: block;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.42);
  transition: transform 0.18s ease, background 0.18s ease;
}
.tick.done i { background: rgba(229, 50, 31, 0.85); }
.tick.current i { background: #fff; transform: scale(1.55); }
@media (hover: hover) { .tick:hover i { background: #fff; transform: scale(1.7); } }

/* أزرار */
.controls {
  display: flex;
  align-items: center;
  gap: clamp(7px, 2.2vw, 14px);
}

button { font-family: inherit; color: inherit; }

.btn-play {
  flex: 0 0 auto;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: 0;
  background: var(--ink);
  color: #06090e;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: transform 0.14s ease, background 0.18s ease;
}
.btn-play:active { transform: scale(0.93); background: var(--accent); color: #fff; }
.btn-play:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.btn-play svg { width: 19px; height: 19px; fill: currentColor; }
.btn-play .ico-play { display: none; }
.btn-play.paused .ico-play { display: block; }
.btn-play.paused .ico-pause { display: none; }

.btn-ghost {
  flex: 0 1 auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 38px;
  padding: 0 13px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.26);
  background: rgba(255, 255, 255, 0.03);
  color: var(--ink);
  font-size: 12px;
  font-weight: 400;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.18s ease, border-color 0.18s ease, transform 0.12s ease;
}
.btn-ghost svg { width: 13px; height: 13px; fill: currentColor; opacity: 0.85; }
.btn-ghost:active { transform: scale(0.96); }
.btn-ghost:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
@media (hover: hover) {
  .btn-ghost:hover { background: rgba(255, 255, 255, 0.1); border-color: rgba(255, 255, 255, 0.5); }
}

.tc {
  margin-inline-start: auto;
  direction: ltr;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 11px;
  color: var(--ink);
  letter-spacing: 0.02em;
  white-space: nowrap;
}
.tc-sep { margin: 0 5px; opacity: 0.4; }
.tc-dim { opacity: 0.45; }

.switch {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 38px;
  padding: 0 11px 0 8px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.26);
  background: rgba(255, 255, 255, 0.03);
  font-size: 11.5px;
  color: var(--muted);
  cursor: pointer;
  transition: color 0.18s ease, border-color 0.18s ease, background 0.18s ease;
}
.switch:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.sw-track {
  width: 26px;
  height: 15px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.16);
  position: relative;
  transition: background 0.2s ease;
  flex: 0 0 auto;
}
.sw-dot {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.75);
  transition: transform 0.22s cubic-bezier(0.34, 1.4, 0.64, 1), background 0.2s ease;
}
.switch[aria-pressed="true"] { color: var(--ink); border-color: rgba(255, 255, 255, 0.5); }
.switch[aria-pressed="true"] .sw-track { background: var(--accent); }
.switch[aria-pressed="true"] .sw-dot { transform: translateX(11px); background: #fff; }

/* ---------- الخاتمة ---------- */
.finale {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 2px;
  padding: 0 calc(var(--pad) + 8px);
  text-align: center;
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  transition: opacity 1.1s ease, visibility 0s linear 1.1s;
  background: radial-gradient(70% 45% at 50% 52%, rgba(2, 5, 10, 0.62) 0%, rgba(2, 5, 10, 0) 72%);
}
.finale.show {
  opacity: 1;
  visibility: visible;
  transition: opacity 1.1s ease, visibility 0s linear 0s;
}

.finale-line {
  margin: 0;
  font-weight: 300;
  font-size: clamp(23px, 6.6vw, 54px);
  line-height: 1.42;
  color: #F7F3EA;
  text-shadow: 0 2px 34px rgba(0, 0, 0, 0.75), 0 0 60px rgba(200, 107, 255, 0.28);
  opacity: 0;
  transform: translateY(16px);
  transition: opacity 1s cubic-bezier(0.22, 0.8, 0.3, 1), transform 1.1s cubic-bezier(0.22, 0.8, 0.3, 1);
  will-change: transform, opacity;
}
.finale.show .finale-line[data-i="0"] { opacity: 1; transform: translateY(0); transition-delay: 0.15s; }
.finale.show .finale-line[data-i="1"] { opacity: 1; transform: translateY(0); transition-delay: 1.05s; }

/* ---------- التحميل / الخطأ ---------- */
.loader {
  position: absolute;
  inset: 0;
  z-index: 20;
  background: #04080f;
  display: grid;
  place-items: center;
  transition: opacity 0.9s ease, visibility 0s linear 0.9s;
}
.loader.hide { opacity: 0; visibility: hidden; }
.loader-inner { display: grid; justify-items: center; gap: 16px; }
.loader-inner p {
  margin: 0;
  font-size: 12px;
  letter-spacing: 0.04em;
  color: var(--muted);
}
.loader-line {
  width: 92px;
  height: 1px;
  background: rgba(255, 255, 255, 0.16);
  overflow: hidden;
  position: relative;
}
.loader-line::after {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--accent);
  transform-origin: left center;
  animation: sweep 1.5s cubic-bezier(0.6, 0, 0.3, 1) infinite;
}
@keyframes sweep {
  0%   { transform: scaleX(0) translateX(0); }
  55%  { transform: scaleX(1) translateX(0); }
  100% { transform: scaleX(1) translateX(100%); }
}

.fatal {
  position: absolute;
  inset: 0;
  z-index: 30;
  background: #04080f;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 18px;
  text-align: center;
  padding: 24px;
  font-size: 14px;
  color: var(--ink);
}
.fatal.hidden { display: none; }
.fatal button {
  border: 1px solid rgba(255, 255, 255, 0.35);
  background: none;
  color: var(--ink);
  padding: 10px 20px;
  border-radius: 999px;
  font-size: 13px;
  cursor: pointer;
}
.fatal button:active { background: rgba(255, 255, 255, 0.12); }

/* ---------- استجابة ---------- */
@media (max-width: 400px) {
  .btn-ghost span { display: none; }
  .btn-ghost { padding: 0 12px; }
  .sw-label { display: none; }
  .switch { padding: 0 9px; }
  .chap-name { max-width: 40vw; }
}

@media (max-height: 460px) {
  .hud-bottom { padding-top: 18px; gap: 4px; }
  .track { height: 24px; }
  .btn-play { width: 40px; height: 40px; }
  .btn-ghost, .switch { height: 34px; }
  .finale-line { font-size: clamp(19px, 4.6vw, 34px); }
}

@media (prefers-reduced-motion: reduce) {
  .finale-line, .btn-play, .btn-ghost, .switch, .sw-dot { transition-duration: 0.12s; }
  .loader-line::after { animation-duration: 2.4s; }
}