:root {
  --ink: #0f1112;
  --muted: #555;
  --line: #e2e2e2;
  --paper: #fff;
  --well: #f0f0f0;
  --red: #cb130b;
  --red-dark: #9e0f08;
  --black: #0f1112;
  --shadow: 0 16px 40px rgba(70, 50, 30, 0.16);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: #f5f5f5;
  color: var(--ink);
  font-family: Tajawal, "Segoe UI", "Geeza Pro", "Noto Naskh Arabic", sans-serif;
  font-size: 14.5px;
}
button, input, select { font-family: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; }
:focus-visible { outline: 2px solid var(--red); outline-offset: 2px; }

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

#app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px 12px 10px;
}

.panel {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 12px;
}

.row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-top: 1px solid #eee;
}
.session { border-top: 0; min-height: 44px; }
.tools { flex-wrap: nowrap; overflow-x: auto; }
.spacer { flex: 1; }
.label { font-weight: 700; white-space: nowrap; }

.live {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
}
.live i {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--red);
  box-shadow: 0 0 0 4px rgba(203, 19, 11, 0.14);
}

.lang {
  display: inline-flex;
  padding: 3px;
  gap: 3px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  flex: none;
}
.lang[hidden] { display: none; }
.lang button {
  border: 0;
  background: transparent;
  border-radius: 7px;
  min-width: 44px;
  height: 32px;
  padding: 0 10px;
  font-family: Montserrat, Tajawal, sans-serif;
  font-weight: 700;
  letter-spacing: 0.04em;
}
.lang button.on { background: var(--red); color: #fff; }
.icon-btn {
  width: 36px;
  height: 36px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
}
.icon-btn .icon-off { display: none; }
.icon-btn:not(.on) .icon-on { display: none; }
.icon-btn:not(.on) .icon-off { display: block; }

.pill, .segment button, .icon-pair button, .zoom button, .icon-btn {
  border: 1px solid var(--line);
  background: #f5f5f5;
  border-radius: 8px;
}
.pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  font-weight: 700;
  white-space: nowrap;
}
.pill.on, .icon-btn.on {
  background: var(--red);
  border-color: var(--red);
  color: #fff;
}
.pills { display: flex; flex-wrap: nowrap; gap: 8px; }
.ico { width: 16px; height: 16px; flex: none; }

.segment {
  display: inline-flex;
  padding: 3px;
  gap: 3px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
}
.segment button {
  border: 0;
  background: transparent;
  border-radius: 7px;
  padding: 8px 12px;
  font-weight: 700;
  white-space: nowrap;
}
.segment button.on { background: var(--red); color: #fff; }

.icon-pair { display: inline-flex; gap: 6px; flex: none; }
.icon-pair button {
  width: 34px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  padding: 0;
}
.icon-pair button svg { width: 18px; height: 18px; }
.icon-pair button:disabled, .footer button:disabled { opacity: 0.35; }

.zoom {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.zoom-btns { display: inline-flex; gap: 6px; flex: none; }
.zoom button {
  width: 32px;
  height: 32px;
  background: #fff;
  font-size: 18px;
  line-height: 1;
  padding: 0;
}
#zoom-label {
  min-width: 48px;
  text-align: center;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 132px;
  height: 4px;
  border-radius: 99px;
  background: linear-gradient(to right, var(--red) var(--p), #ddd var(--p));
  cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--red);
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px var(--red);
}
input[type="range"]::-moz-range-track {
  height: 4px;
  background: transparent;
  border: 0;
}
input[type="range"]::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--red);
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px var(--red);
}

.well {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: auto;
  background: var(--well);
  border-radius: 12px;
  scrollbar-gutter: stable;
}
.progress {
  position: sticky;
  top: 14px;
  float: left;
  margin-left: 12px;
  color: #8d877e;
  font-weight: 800;
  font-size: 13px;
  z-index: 2;
  pointer-events: none;
}
.spread {
  display: flex;
  align-items: flex-start;
  gap: 18px;
  width: max-content;
  padding: 8px;
}
[data-dir="rtl"] .spread { flex-direction: row-reverse; }
[data-dir="ltr"] .spread { flex-direction: row; }
.spread.enter-next, .spread.enter-prev { animation-duration: 0.34s; animation-timing-function: ease; animation-fill-mode: both; }
[data-dir="rtl"] .spread.enter-next, [data-dir="ltr"] .spread.enter-prev { animation-name: fromLeft; }
[data-dir="rtl"] .spread.enter-prev, [data-dir="ltr"] .spread.enter-next { animation-name: fromRight; }
@keyframes fromLeft {
  from { transform: translateX(-26px); opacity: 0.35; }
  to { transform: none; opacity: 1; }
}
@keyframes fromRight {
  from { transform: translateX(26px); opacity: 0.35; }
  to { transform: none; opacity: 1; }
}

.sheet { flex: none; }
.frame {
  position: relative;
  background: #fff;
  box-shadow: var(--shadow), 0 1px 2px rgba(0, 0, 0, 0.05);
  overflow: hidden;
  direction: ltr;
  -webkit-user-drag: none;
}
.frame canvas { display: block; -webkit-user-drag: none; user-select: none; }
.spread.pair[data-pair="rtl"] .sheet:first-child .frame {
  box-shadow: inset 14px 0 16px rgba(60, 40, 20, 0.05), var(--shadow);
}
.spread.pair[data-pair="rtl"] .sheet:last-child .frame {
  box-shadow: inset -14px 0 16px rgba(60, 40, 20, 0.05), var(--shadow);
}
.spread.pair[data-pair="ltr"] .sheet:first-child .frame {
  box-shadow: inset -14px 0 16px rgba(60, 40, 20, 0.05), var(--shadow);
}
.spread.pair[data-pair="ltr"] .sheet:last-child .frame {
  box-shadow: inset 14px 0 16px rgba(60, 40, 20, 0.05), var(--shadow);
}
.caption {
  margin: 10px 0 0;
  text-align: center;
  color: #8f897f;
  font-size: 13px;
  font-weight: 700;
}
.message {
  margin: 0;
  padding: 48px 20px;
  color: #4c473f;
  font-size: 16px;
  font-weight: 700;
  text-align: center;
}
.message.loading::before {
  content: "";
  display: block;
  width: min(220px, 70vw);
  height: 3px;
  margin: 0 auto 18px;
  border-radius: 99px;
  background: linear-gradient(90deg, #e2e2e2 0%, var(--red) 45%, #e2e2e2 90%);
  background-size: 200% 100%;
  animation: loading-bar 1.1s ease-in-out infinite;
}
@keyframes loading-bar {
  from { background-position: 100% 0; }
  to { background-position: -100% 0; }
}

.textLayer, .linkLayer { position: absolute; inset: 0; overflow: hidden; line-height: 1; }
.textLayer span, .textLayer br {
  color: transparent;
  position: absolute;
  white-space: pre;
  transform-origin: 0 0;
  cursor: text;
}
.textLayer ::selection { background: rgba(226, 59, 50, 0.28); }
.pdf-link { position: absolute; display: block; }
.pdf-link:hover { background: rgba(226, 59, 50, 0.14); }

.footer {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
  padding: 10px;
  direction: rtl;
}
[data-dir="ltr"] .footer { direction: ltr; }
[data-dir="rtl"] .icon-pair { direction: rtl; }
[data-dir="ltr"] .icon-pair { direction: ltr; }
.footer button {
  min-width: 168px;
  height: 46px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-weight: 800;
  font-size: 15px;
  padding: 0 16px;
  background: var(--red);
  color: #fff;
  border: 1px solid var(--red);
  white-space: nowrap;
}
#prev { justify-self: start; }
#next { justify-self: end; }
.primary { background: var(--red); color: #fff; border: 1px solid var(--red); }
.ghost { background: #fff; color: var(--ink); border: 1px solid var(--line); }
.footer svg { width: 18px; height: 18px; flex: none; }
.pager {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 800;
}
.slash, #total { color: #8d877e; font-weight: 700; }
#page-input {
  width: 68px;
  height: 42px;
  border: 1.5px solid var(--red);
  border-radius: 8px;
  text-align: center;
  font-weight: 800;
  font-size: 16px;
  background: #fff;
}
#page-input::-webkit-outer-spin-button,
#page-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

.gate {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(80, 70, 55, 0.28);
}
.gate[hidden] { display: none; }
.dialog {
  width: min(380px, calc(100% - 32px));
  background: #fff;
  border-radius: 12px;
  padding: 20px;
  box-shadow: var(--shadow);
}
.dialog h2 { margin: 0 0 8px; font-size: 18px; }
.dialog p { margin: 0 0 12px; color: var(--muted); }
.dialog input {
  width: 100%;
  height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 12px;
  margin-bottom: 12px;
}
.gate-actions { display: flex; gap: 8px; }
.gate-actions button {
  flex: 1;
  height: 42px;
  border-radius: 8px;
  font-weight: 800;
}

.signup {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(15, 17, 18, 0.46);
  padding: 16px;
}
.signup[hidden] { display: none; }
.signup .dialog label {
  display: block;
  margin-bottom: 10px;
  font-weight: 700;
}
.signup .dialog label span { display: block; margin-bottom: 6px; }
.signup .dialog input { margin-bottom: 0; }
.signup .primary {
  width: 100%;
  height: 44px;
  border-radius: 8px;
  font-weight: 800;
  margin-top: 4px;
}
.signup .fillout-dialog {
  width: min(680px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  padding: 0;
  overflow: auto;
  background: transparent;
  box-shadow: none;
}
.signup .fillout-dialog iframe {
  display: block;
  width: 100%;
  height: min(680px, calc(100dvh - 24px));
  border: 0;
  border-radius: 12px;
  background: #fff;
}
.signup-error {
  margin: 0 0 10px;
  color: var(--red);
  font-weight: 700;
}

body.pseudo-fs { overflow: hidden; }
body.pseudo-fs #app {
  position: fixed;
  inset: 0;
  z-index: 20;
  width: 100%;
  height: 100dvh;
  margin: 0;
  background: #f5f5f5;
  padding:
    max(8px, env(safe-area-inset-top))
    max(12px, env(safe-area-inset-right))
    max(10px, env(safe-area-inset-bottom))
    max(12px, env(safe-area-inset-left));
}

@media print {
  html, body { display: none !important; }
}

@media (max-width: 860px) {
  #app { padding: 8px; gap: 8px; }
  .row { flex-wrap: wrap; }
  .row.tools { flex-wrap: nowrap; }
  .footer { grid-template-columns: 1fr 1fr; }
  .pager { grid-column: 1 / -1; grid-row: 1; justify-content: center; }
  #prev, #next { min-width: 0; width: 100%; }
  #prev { grid-column: 1; grid-row: 2; justify-self: stretch; }
  #next { grid-column: 2; grid-row: 2; justify-self: stretch; }
  input[type="range"] { width: 92px; }
}
