/* Layout: one screen at a time. Phones get a single mushaf page; wide landscape
   screens get an open-book spread (odd page on the right, as in a printed mushaf). */
:root {
  --bg: #f1f4ec;
  --surface: #ffffff;
  --ink: #17231d;
  --muted: #5a6a61;
  --line: #dbe2d5;
  --brand: #0b5d3b;
  --brand-deep: #08452c;
  --brand-ink: #ffffff;
  --brand-soft: #e2efe5;
  --brand-text: #0b5d3b;
  --gold: #c99a2e;
  --rec: #d6362a;
  --desk: #dfe4d4;
  --hl: rgba(255, 190, 0, 0.45);
  --tap: rgba(11, 93, 59, 0.2);
  --toast-bg: rgba(18, 28, 22, 0.92);
  --toast-ink: #ffffff;
  --page-filter: none;

  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  --font-display: "Amiri", "Noto Naskh Arabic", "Times New Roman", serif;
  --font-arabic: "Amiri", "Noto Naskh Arabic", "Geeza Pro", "Traditional Arabic", serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1612;
    --surface: #16211b;
    --ink: #e5eee8;
    --muted: #9eb0a5;
    --line: #26352d;
    --brand: #0d4a31;
    --brand-deep: #093824;
    --brand-ink: #f1f6f2;
    --brand-soft: #1d3328;
    --brand-text: #8fd1a8;
    --gold: #d9b052;
    --desk: #0a110d;
    --tap: rgba(120, 200, 150, 0.25);
    --toast-bg: rgba(230, 240, 234, 0.95);
    --toast-ink: #0e1612;
    --page-filter: brightness(0.92);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e1612;
  --surface: #16211b;
  --ink: #e5eee8;
  --muted: #9eb0a5;
  --line: #26352d;
  --brand: #0d4a31;
  --brand-deep: #093824;
  --brand-ink: #f1f6f2;
  --brand-soft: #1d3328;
  --brand-text: #8fd1a8;
  --gold: #d9b052;
  --desk: #0a110d;
  --tap: rgba(120, 200, 150, 0.25);
  --toast-bg: rgba(230, 240, 234, 0.95);
  --toast-ink: #0e1612;
  --page-filter: brightness(0.92);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
}
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 15px;
  -webkit-user-select: none;
  user-select: none;
}

button {
  font: inherit;
  color: inherit;
  border: 0;
  background: none;
  padding: 0;
  cursor: pointer;
}
button:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }

svg { width: 24px; height: 24px; fill: currentColor; flex: none; }

.hidden { display: none !important; }

.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
}

/* top bar: spans the width, so it carries the side gutter and the top inset */
.bar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 60px;
  padding-top: calc(8px + env(safe-area-inset-top, 0px));
  padding-bottom: 8px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  background: var(--brand);
  color: var(--brand-ink);
}
.bar-text { min-width: 0; flex: 1; }
.bar h1 {
  margin: 0;
  font-size: 19px;
  font-weight: 600;
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.bar p { margin: 1px 0 0; font-size: 13px; opacity: 0.8; }
#list .bar { justify-content: center; }
#list .bar-text { max-width: 960px; }
#list .bar h1 { font-family: var(--font-display); font-size: 24px; font-weight: 700; }
.bar .icon:first-child { margin-left: -10px; }
.bar .icon:last-child { margin-right: -10px; }
.icon {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  flex: none;
}
.icon:active, .icon:hover { background: rgba(255, 255, 255, 0.14); }

/* surah list */
.list-body {
  flex: 1;
  overflow-y: auto;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  padding-block: 16px calc(24px + env(safe-area-inset-bottom, 0px));
}
.list-inner { max-width: 960px; margin: 0 auto; display: grid; gap: 16px; }
.actions { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.action {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  padding: 12px 16px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  font-size: 16px;
  font-weight: 500;
  text-align: left;
}
.action svg { color: var(--brand-text); }
.action.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.action.primary svg { color: var(--brand-ink); }
.action:hover { filter: brightness(1.04); }
.action:active { filter: brightness(0.95); }

.section-label {
  margin: 4px 2px 0;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.search {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 48px;
  padding: 0 14px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--muted);
}
.search:focus-within { border-color: var(--brand-text); }
.search input {
  flex: 1;
  min-width: 0;
  height: 46px;
  border: 0;
  outline: 0;
  background: none;
  color: var(--ink);
  font: inherit;
  font-size: 16px;
}
#surahGroups { display: grid; gap: 16px; }
.surah-group { display: grid; gap: 10px; }
.surah-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 300px), 1fr));
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
}
.surah {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  padding: 10px 6px 10px 14px;
  background: var(--surface);
  box-shadow: inset 0 -1px var(--line), inset -1px 0 var(--line);
  cursor: pointer;
}
.surah:hover { background: var(--brand-soft); }
.num {
  width: 38px;
  height: 38px;
  flex: none;
  display: grid;
  place-items: center;
  font-size: 14px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--brand-text);
  background: var(--brand-soft);
  border-radius: 50%;
}
.surah:hover .num { background: var(--surface); }
.meta { flex: 1; min-width: 0; }
.meta b { display: block; font-size: 16px; font-weight: 600; }
.meta small { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.ar {
  font-family: var(--font-arabic);
  font-size: 23px;
  line-height: 1.2;
  color: var(--brand);
  direction: rtl;
  white-space: nowrap;
}
:root[data-theme="dark"] .ar { color: var(--gold); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .ar { color: var(--gold); } }
.listen {
  width: 44px;
  height: 44px;
  flex: none;
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: var(--brand-text);
}
.listen:hover { background: var(--surface); }
.dl {
  width: 40px;
  height: 44px;
  flex: none;
  display: grid;
  place-items: center;
  border-radius: 22px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.dl svg { width: 22px; height: 22px; }
.dl:hover { background: var(--surface); }
.dl.done { color: var(--brand-text); }
.dl.busy { color: var(--brand-text); }
.dl.armed { width: auto; padding: 0 10px; color: var(--rec); font-size: 13px; }
.list-note { margin: 0; color: var(--muted); font-size: 13px; text-align: center; }

/* reader */
#reader { background: var(--desk); }
.stage { flex: 1; min-height: 0; position: relative; }
#pager {
  position: absolute;
  inset: 0;
  display: flex;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x mandatory;
  scrollbar-width: none;
  overscroll-behavior-x: contain;
}
#pager::-webkit-scrollbar { display: none; }
.page {
  flex: 0 0 100%;
  height: 100%;
  scroll-snap-align: center;
  scroll-snap-stop: always;
  overflow-y: auto;
  display: flex;
  justify-content: center;
  padding: 8px max(8px, env(safe-area-inset-right, 0px)) 8px max(8px, env(safe-area-inset-left, 0px));
  container-type: size;
}
.page.blank { display: none; }
.sheet {
  position: relative;
  width: 100%;
  max-width: 100%;
  margin: auto 0;
  direction: ltr;
  aspect-ratio: var(--ratio);
  background: #ffffd7;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 6px 20px rgba(0, 0, 0, 0.08);
}
@media (orientation: portrait) {
  .sheet { width: min(100cqw, 100cqh * var(--ratio)); }
}
.sheet img { display: block; width: 100%; height: 100%; pointer-events: none; filter: var(--page-filter); }
.rg {
  position: absolute;
  border-radius: 6px;
  cursor: pointer;
  transition: background-color 0.2s;
}
.rg.on { background: var(--hl); mix-blend-mode: multiply; }
.rg.tap { background: var(--tap); }

/* open-book spread on wide landscape screens */
.spread .page {
  flex-basis: 50%;
  scroll-snap-align: none;
  padding-inline: 0;
}
/* the pager is right-to-left, so flex-end is the spine side of a right-hand page */
.spread .page.right {
  scroll-snap-align: start;
  justify-content: flex-end;
  padding-right: max(72px, env(safe-area-inset-right, 0px));
}
.spread .page.left {
  justify-content: flex-start;
  padding-left: max(72px, env(safe-area-inset-left, 0px));
}
.spread .page.blank { display: flex; }
.spread .sheet { width: min(100cqw, 100cqh * var(--ratio)); }
.spread .page.right .sheet { box-shadow: inset 12px 0 18px -14px rgba(0, 0, 0, 0.35), 0 6px 20px rgba(0, 0, 0, 0.1); }
.spread .page.left .sheet { box-shadow: inset -12px 0 18px -14px rgba(0, 0, 0, 0.35), 0 6px 20px rgba(0, 0, 0, 0.1); }

.turn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 44px;
  height: 64px;
  border-radius: 10px;
  display: none;
  place-items: center;
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--line);
  opacity: 0.85;
  z-index: 2;
}
.turn:hover { opacity: 1; }
.turn[disabled] { opacity: 0.25; cursor: default; }
#turnNext { left: max(10px, env(safe-area-inset-left, 0px)); }
#turnPrev { right: max(10px, env(safe-area-inset-right, 0px)); }
@media (hover: hover) and (pointer: fine) {
  .turn { display: grid; }
}

/* player */
#player {
  flex: none;
  background: var(--brand);
  color: var(--brand-ink);
  padding-top: 8px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.player-inner { max-width: 560px; margin: 0 auto; }
#nowLabel {
  text-align: center;
  font-size: 14px;
  opacity: 0.9;
  min-height: 20px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
  margin-top: 6px;
}
.play {
  width: 58px;
  height: 58px;
  flex: none;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--brand-ink);
  color: var(--brand);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}
.play svg { width: 32px; height: 32px; }
.play:active { transform: scale(0.96); }
.play .i-pause { display: none; }
.playing .play .i-play { display: none; }
.playing .play .i-pause { display: block; }
.chip {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 100px;
  height: 36px;
  padding: 0 10px;
  border-radius: 18px;
  border: 1px solid rgba(255, 255, 255, 0.45);
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
}
.chip svg { width: 18px; height: 18px; }
.chip:hover { background: rgba(255, 255, 255, 0.1); }
.chip.on { background: var(--gold); border-color: var(--gold); color: #fff; }
@media (max-width: 360px) {
  .chip { min-width: 0; }
  .chip span { display: none; }
}

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(150px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--toast-bg);
  color: var(--toast-ink);
  font-size: 14px;
  text-align: center;
  z-index: 10;
  pointer-events: none;
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; scroll-behavior: auto !important; }
}

/* practice: recite and record yourself */
.mic[aria-pressed="true"] { background: var(--brand-ink); color: var(--brand); }
.practice-inner { display: none; max-width: 560px; margin: 0 auto; }
#reader.practice .player-inner { display: none; }
#reader.practice .practice-inner { display: block; }
.prac-top, .prac-rec {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 32px;
}
.prac-rec { display: none; justify-content: center; font-size: 14px; font-variant-numeric: tabular-nums; }
#reader.recording .prac-top { display: none; }
#reader.recording .prac-rec { display: flex; }
.prac-label {
  flex: 1;
  min-width: 0;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.seg { display: flex; border: 1px solid rgba(255, 255, 255, 0.45); border-radius: 16px; overflow: hidden; flex: none; }
.seg button { height: 30px; padding: 0 12px; font-size: 13px; font-weight: 500; }
.seg button[aria-pressed="true"] { background: var(--brand-ink); color: var(--brand); }
.seg button[disabled] { opacity: 0.4; cursor: default; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--rec); animation: blink 1s steps(2, start) infinite; }
.rec-time { min-width: 3.5em; font-weight: 600; }
.meter { width: 80px; height: 6px; border-radius: 3px; background: rgba(255, 255, 255, 0.25); overflow: hidden; }
.meter i { display: block; height: 100%; width: 0; background: var(--brand-ink); transition: width 0.08s linear; }

.chip .i-pause { display: none; }
.chip.playing .i-play { display: none; }
.chip.playing .i-pause { display: block; }
.chip[disabled] { opacity: 0.4; cursor: default; }
.practice-inner .chip { min-width: 92px; }

.rec-btn {
  width: 58px;
  height: 58px;
  flex: none;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--brand-ink);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}
.rec-shape { width: 24px; height: 24px; border-radius: 50%; background: var(--rec); transition: border-radius 0.15s, transform 0.15s; }
#reader.recording .rec-btn { animation: pulse 1.6s ease-out infinite; }
#reader.recording .rec-shape { border-radius: 5px; transform: scale(0.85); }
.rec-btn:active { transform: scale(0.96); }

#recListBtn { position: relative; }
.badge {
  position: absolute;
  top: 4px;
  right: 2px;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--gold);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  line-height: 18px;
  text-align: center;
}

.rec-panel {
  position: absolute;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  width: min(560px, 100%);
  max-height: 60%;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: 16px 16px 0 0;
  box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.18);
  z-index: 3;
}
.rec-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 8px 4px 16px;
  border-bottom: 1px solid var(--line);
}
.rec-panel-head h2 { margin: 0; font-size: 16px; font-weight: 600; }
.rec-panel-head .icon:hover { background: var(--brand-soft); }
#recList { list-style: none; margin: 0; padding: 4px 0; overflow-y: auto; }
.rec-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px 6px 12px;
}
.rec-item + .rec-item { border-top: 1px solid var(--line); }
.rec-item .icon { color: var(--brand-text); }
.rec-item .icon:hover { background: var(--brand-soft); }
.rec-item .icon.playing { background: var(--brand-soft); }
.rec-meta { flex: 1; min-width: 0; }
.rec-meta b { display: block; font-size: 15px; font-weight: 600; }
.rec-meta small { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.rec-del { color: var(--muted) !important; }
.rec-del.armed { color: var(--rec) !important; width: auto; padding: 0 10px; border-radius: 18px; font-size: 13px; font-weight: 600; }
.rec-empty { margin: 0; padding: 16px; color: var(--muted); font-size: 14px; }

@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(214, 54, 42, 0.55); }
  100% { box-shadow: 0 0 0 16px rgba(214, 54, 42, 0); }
}
@keyframes blink { to { visibility: hidden; } }
