@font-face {
  font-family: 'Hafs';
  src: url('/fonts/hafs.woff2') format('woff2');
  font-display: block;
}

:root {
  --bg: #eef1ee;
  --surface: #ffffff;
  --sheet: #fffdf6;
  --sheet-edge: #d9cfae;
  --text: #1b1f1d;
  --muted: #5f6b66;
  --line: #d8ddd9;
  --accent: #1f5f4a;
  --accent-text: #ffffff;
  --danger: #b3261e;
  --shadow: 0 6px 24px rgb(0 0 0 / 0.12);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #121514;
    --surface: #1c201e;
    --sheet: #1a1c19;
    --sheet-edge: #4a4433;
    --text: #e9ece9;
    --muted: #9aa5a0;
    --line: #2f3532;
    --accent: #5fbf98;
    --accent-text: #0d1a14;
    --danger: #f2b8b5;
    --shadow: 0 6px 24px rgb(0 0 0 / 0.5);
    color-scheme: dark;
  }
}
:root[data-theme='dark'] {
  --bg: #121514;
  --surface: #1c201e;
  --sheet: #1a1c19;
  --sheet-edge: #4a4433;
  --text: #e9ece9;
  --muted: #9aa5a0;
  --line: #2f3532;
  --accent: #5fbf98;
  --accent-text: #0d1a14;
  --danger: #f2b8b5;
  --shadow: 0 6px 24px rgb(0 0 0 / 0.5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, 'Segoe UI', 'Noto Sans Arabic', Tahoma, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  cursor: pointer;
}
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.primary { background: var(--accent); color: var(--accent-text); border-radius: 10px; padding: 10px 18px; font-weight: 600; }
.link { color: var(--accent); padding: 6px 4px; }
.pill { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 6px 12px; white-space: nowrap; }
.pill.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.icon { font-size: 20px; padding: 6px 10px; }
.error { color: var(--danger); min-height: 1.4em; margin: 0; }

/* ---- login ---- */
.login {
  max-width: 340px; margin: 12vh auto 0; padding: 28px 24px; display: grid; gap: 14px; justify-items: center;
  background: var(--surface); border-radius: 16px; box-shadow: var(--shadow);
}
.login h1 { margin: 0; font-size: 22px; }
.login label { display: grid; gap: 6px; width: 100%; }
.login input { font: inherit; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text); }
.login .primary { width: 100%; }

/* ---- app frame ---- */
#app { height: 100dvh; display: flex; flex-direction: column; }
.top {
  display: flex; align-items: center; gap: 8px; padding: 6px 10px;
  padding-top: max(6px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.where { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.2; }
.where span { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.where small { color: var(--muted); }
.presence { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; white-space: nowrap; }
.presence::before { content: ''; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.presence.on::before { background: #2e9d6a; }

.session-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 6px 12px; font-size: 14px; color: var(--muted); }
.session-bar:empty { display: none; }
.session-bar .running { color: var(--text); font-weight: 600; }
.banner { background: #f9e7b0; color: #3d2f00; text-align: center; padding: 4px; font-size: 13px; }

main { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; gap: 4px; padding: 8px 4px; }
.nav { font-size: 28px; color: var(--muted); padding: 8px 6px; align-self: center; }

/* ---- the mushaf sheet ---- */
.sheet {
  position: relative; height: 100%; width: 100%; max-width: 760px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.page {
  width: 100%; background: var(--sheet); border: 1px solid var(--sheet-edge); border-radius: 6px;
  box-shadow: var(--shadow); padding: 2.5% 3%;
}
.lines { font-family: 'Hafs', serif; direction: rtl; line-height: 1.85; color: var(--text); margin: 0 auto; }
.line { display: flex; justify-content: space-between; align-items: center; white-space: nowrap; height: 1.85em; }
.line.short, .line.header, .line.bismillah { justify-content: center; gap: 0.3em; }
.lines.measuring .line.words { display: inline-flex; width: max-content; gap: 0.25em; justify-content: flex-start; }
.line.header .frame {
  font-size: 0.8em; padding: 0 1.4em; border: 3px double var(--sheet-edge); border-radius: 0.4em;
  line-height: 1.6; background: color-mix(in srgb, var(--sheet-edge) 18%, transparent);
}
.line.bismillah { font-size: 0.95em; }

.w { position: relative; border-radius: 0.18em; padding: 0 0.04em; transition: background-color 0.2s; }
.w.marked { cursor: pointer; }
.w.ayah-marked { text-decoration: underline 0.08em; text-decoration-color: var(--ac); text-underline-offset: 0.35em; }
.w.cleared { outline: 0.04em dashed var(--cc); outline-offset: -0.02em; }
.teacher .w { cursor: pointer; }
@media (hover: hover) {
  .teacher .w:hover { box-shadow: inset 0 -0.08em 0 var(--brush); }
}
.w.flash { animation: flash 1.2s ease-out; }
@keyframes flash { from { box-shadow: 0 0 0 0.15em var(--accent); } to { box-shadow: 0 0 0 0 transparent; } }

.page-no { margin-top: 6px; color: var(--muted); font-size: 14px; padding: 2px 10px; border-radius: 8px; }
.page-no input { width: 5em; font: inherit; text-align: center; }

/* ---- toolbar ---- */
.toolbar {
  display: flex; align-items: center; gap: 6px; padding: 8px 10px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  background: var(--surface); border-top: 1px solid var(--line); overflow-x: auto;
}
.chip {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); border-radius: 999px;
  padding: 6px 10px; white-space: nowrap; background: var(--surface);
}
.chip i { width: 14px; height: 14px; border-radius: 50%; background: var(--c); flex: none; }
.chip.on { border-color: var(--c); box-shadow: inset 0 0 0 2px var(--c); font-weight: 600; }
.legend .chip { cursor: default; }
.toolbar .spacer { flex: 1; }
.toolbar .read-only { color: var(--muted); font-size: 13px; white-space: nowrap; }

/* ---- popover ---- */
.popover {
  position: fixed; z-index: 30; width: min(320px, calc(100vw - 16px)); background: var(--surface);
  border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); padding: 10px; display: grid; gap: 10px;
}
.popover .word { font-family: 'Hafs', serif; font-size: 28px; text-align: center; direction: rtl; }
.mark-card { border-inline-start: 4px solid var(--c); padding: 4px 8px; display: grid; gap: 6px; }
.mark-card .meta { color: var(--muted); font-size: 13px; }
.mark-card .note { white-space: pre-wrap; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; }
.actions button { border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; }
.actions .danger { color: var(--danger); }
.swatches { display: flex; flex-wrap: wrap; gap: 6px; }
.swatches button { width: 28px; height: 28px; border-radius: 50%; background: var(--c); border: 2px solid transparent; }
.swatches button.on { border-color: var(--text); }

/* ---- drawer ---- */
.drawer-bg { position: fixed; inset: 0; background: rgb(0 0 0 / 0.3); z-index: 40; }
.drawer {
  position: fixed; inset-block: 0; inset-inline-start: 0; z-index: 41; width: min(400px, 92vw);
  background: var(--surface); box-shadow: var(--shadow); display: grid; grid-template-rows: auto 1fr auto;
}
.tabs { display: flex; border-bottom: 1px solid var(--line); }
.tabs button { flex: 1; padding: 12px 6px; border-bottom: 3px solid transparent; }
.tabs button.on { border-color: var(--accent); font-weight: 600; }
.drawer-body { overflow-y: auto; padding: 10px 12px; display: grid; gap: 10px; align-content: start; }
.drawer-foot { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-top: 1px solid var(--line); }
.drawer-foot span { flex: 1; color: var(--muted); }
.filters { display: flex; flex-wrap: wrap; gap: 6px; }
.filters .chip { padding: 4px 8px; font-size: 13px; }
.group h3 { margin: 6px 0; font-size: 14px; color: var(--muted); }
.item {
  display: flex; width: 100%; align-items: center; gap: 10px; text-align: start; padding: 8px;
  border-radius: 8px; border: 1px solid var(--line);
}
.item + .item { margin-top: 6px; }
.item .dot { width: 12px; height: 12px; border-radius: 50%; background: var(--c); flex: none; }
.ar { font-family: 'Hafs', serif; font-size: 22px; direction: rtl; }
.item .ar { flex: 1; text-align: start; }
.item small { color: var(--muted); }
.timeline { margin: 4px 0 0; padding-inline-start: 18px; color: var(--muted); font-size: 13px; }
.check { display: flex; gap: 8px; align-items: center; }
.empty { color: var(--muted); text-align: center; padding: 20px; }
.stats { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 13px; color: var(--muted); }

/* ---- call ---- */
.call-bar {
  position: fixed; z-index: 35; inset-inline: 0; margin-inline: auto; bottom: 76px; width: max-content; max-width: calc(100vw - 16px);
  display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-radius: 999px;
  background: var(--accent); color: var(--accent-text); box-shadow: var(--shadow);
}
.call-bar button { background: rgb(255 255 255 / 0.2); border-radius: 999px; padding: 6px 12px; }
.call-bar .hang { background: #c62828; color: #fff; }
.pill.in-call { background: #2e9d6a; border-color: #2e9d6a; color: #fff; }

.toast {
  position: fixed; z-index: 50; left: 50%; bottom: 90px; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 10px; opacity: 0; pointer-events: none;
  transition: opacity 0.2s; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; }

@media (max-width: 600px) {
  .nav { display: none; }
  main { padding: 6px 4px; }
  .presence span, .presence { font-size: 0; gap: 0; }
  .toolbar:not(.legend) .chip span { display: none; }
  .toolbar:not(.legend) .chip { padding: 8px; }
  .toolbar.legend { flex-wrap: wrap; gap: 4px; font-size: 12px; }
  .toolbar.legend .chip { padding: 3px 7px; }
}
.toolbar .chip.on span { display: inline; }
