:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --ink: #17252a;
  --muted: #5d6b70;
  --line: #e2ded4;
  --brand: #0f5257;
  --brand-ink: #ffffff;
  --brand-soft: #dcecea;
  --rec: #d6342c;
  --ok: #1f7a4d;
  --ok-soft: #dff2e7;
  --warn: #9a6200;
  --warn-soft: #fbefd5;
  --bad: #b3261e;
  --bad-soft: #fbe1df;
  --radius: 18px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 6px 20px rgba(0,0,0,.06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1719;
    --surface: #172225;
    --ink: #eaf1f1;
    --muted: #9aaeb2;
    --line: #26363a;
    --brand: #4fb3a9;
    --brand-ink: #062022;
    --brand-soft: #1b3638;
    --rec: #ff5a4f;
    --ok: #6fd39d;
    --ok-soft: #173326;
    --warn: #f0b85a;
    --warn-soft: #3a2d12;
    --bad: #ff8a80;
    --bad-soft: #3d1b19;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: "IBM Plex Sans Arabic", system-ui, -apple-system, "Segoe UI", Tahoma, sans-serif;
  font-size: 17px;
  line-height: 1.6;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
button:disabled { opacity: .45; cursor: default; }
h1 { font-size: 1.6rem; line-height: 1.3; margin: .2em 0 .4em; }
h2 { font-size: 1.1rem; margin: 0 0 .2em; }
.muted { color: var(--muted); }
.lead { color: var(--muted); margin: 0 0 1.2em; }

.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--brand); white-space: nowrap; font-size: .95rem; }
.top-actions { margin-inline-start: auto; display: flex; gap: 6px; }
.queue {
  border: 0; border-radius: 999px; padding: 4px 12px; font-size: .82rem;
  background: var(--brand-soft); color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 48vw;
}
.queue.ok { background: var(--ok-soft); color: var(--ok); }
.queue.warn { background: var(--warn-soft); color: var(--warn); }
.queue.bad { background: var(--bad-soft); color: var(--bad); }

main { flex: 1; width: 100%; max-width: 640px; margin: 0 auto; padding: 16px 16px calc(24px + env(safe-area-inset-bottom)); }
.view { animation: fade .18s ease-out; }
.center { text-align: center; padding-top: 10vh; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.primary, .secondary, .ghost {
  border-radius: 14px; min-height: 48px; padding: 0 18px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.primary { background: var(--brand); color: var(--brand-ink); border: 0; }
.secondary { background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow); }
.ghost { background: transparent; border: 1px solid transparent; min-height: 40px; padding: 0 10px; color: var(--brand); }
.ghost:active, .secondary:active { background: var(--brand-soft); }
.primary:active { filter: brightness(.92); }
.wide { width: 100%; margin-top: 8px; }
.lang { font-weight: 700; min-width: 44px; }
svg { width: 20px; height: 20px; fill: currentColor; flex: none; }
.chev { fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
[dir="ltr"] .chev { transform: scaleX(-1); }

.field { display: block; margin-bottom: 16px; }
.field span { display: block; font-weight: 600; margin-bottom: 6px; }
.field input {
  width: 100%; min-height: 50px; padding: 0 14px; font: inherit; color: inherit;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
}
.field input:focus { outline: 2px solid var(--brand); outline-offset: 1px; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.mic .row { display: flex; gap: 8px; flex-wrap: wrap; }
.msg { min-height: 1.6em; margin: 8px 0; font-weight: 600; }
.msg.ok { color: var(--ok); }
.msg.bad { color: var(--bad); }
.tips { color: var(--muted); padding-inline-start: 20px; margin: 16px 0; font-size: .95rem; }
.tips li { margin-bottom: 4px; }

.meter { height: 8px; border-radius: 99px; background: var(--line); overflow: hidden; margin: 10px 0 4px; }
.meter i { display: block; height: 100%; width: 0; background: var(--brand); border-radius: inherit; transition: width .06s linear; }
.meter.big { height: 10px; width: min(260px, 70%); margin: 0 auto; }
.recording .meter i { background: var(--rec); }

.progress-row { display: flex; justify-content: space-between; font-weight: 600; font-size: .95rem; }
.bar { height: 6px; background: var(--line); border-radius: 99px; overflow: hidden; margin-top: 6px; }
.bar i { display: block; height: 100%; width: 0; background: var(--ok); transition: width .3s; }

.sentence-card {
  position: relative; margin: 16px 0 8px; padding: 24px 18px 30px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
  display: flex; align-items: stretch; justify-content: flex-start; padding: 10px;
  /* keep the record button on screen: the card scrolls, the current part is scrolled into view */
  max-height: 44dvh; overflow-y: auto; overscroll-behavior: contain;
}
.sentence { margin: 0; width: 100%; display: flex; flex-direction: column; gap: 6px; }
.part {
  display: block; padding: 8px 12px; border-radius: 12px; cursor: pointer;
  font-size: clamp(1.02rem, 4.3vw, 1.25rem); line-height: 2.05; font-weight: 500; color: var(--muted);
  /* dictations are Arabic paragraphs even when a part starts with an English term */
  direction: rtl; unicode-bidi: isolate; text-align: right; overflow-wrap: anywhere; opacity: .7;
  border: 1px solid transparent; position: relative;
}
.part.done { padding-inline-end: 26px; }
.part.done::after { content: "✓"; position: absolute; top: 4px; inset-inline-end: 8px; font-size: .8rem; color: var(--ok); }
.part.cur {
  cursor: default; opacity: 1; color: var(--ink); font-weight: 600; font-size: clamp(1.15rem, 5vw, 1.45rem);
  background: var(--brand-soft); border-color: color-mix(in srgb, var(--brand) 35%, transparent);
}
.card-tools { display: flex; flex-wrap: wrap; justify-content: center; gap: 0 14px; }
.whole { display: block; margin: 0; font-size: .92rem; min-height: 36px; text-decoration: underline; text-underline-offset: 3px; }
.chip {
  position: absolute; bottom: 8px; inset-inline-start: 12px; font-size: .8rem; font-weight: 600;
  padding: 1px 10px; border-radius: 99px; background: var(--brand-soft);
}
.chip.ok { background: var(--ok-soft); color: var(--ok); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.bad { background: var(--bad-soft); color: var(--bad); }
button.chip { border: 0; }

.recorder { text-align: center; padding: 8px 0 4px; }
.timer { font-variant-numeric: tabular-nums; font-size: 1.05rem; color: var(--muted); margin: 6px 0; direction: ltr; }
.rec {
  width: 92px; height: 92px; border-radius: 50%; border: 6px solid var(--surface);
  background: var(--rec); box-shadow: 0 0 0 2px var(--rec), var(--shadow);
  display: inline-flex; align-items: center; justify-content: center; transition: transform .1s;
}
.rec:active { transform: scale(.96); }
.rec .dot { width: 30px; height: 30px; border-radius: 50%; background: #fff; transition: border-radius .15s, width .15s, height .15s; }
.recording .rec { animation: pulse 1.4s ease-out infinite; }
.recording .rec .dot { border-radius: 6px; width: 28px; height: 28px; }
@keyframes pulse { 0% { box-shadow: 0 0 0 2px var(--rec), 0 0 0 0 color-mix(in srgb, var(--rec) 45%, transparent); } 100% { box-shadow: 0 0 0 2px var(--rec), 0 0 0 22px transparent; } }
.hint { margin: 8px 0 0; color: var(--muted); min-height: 1.6em; }

.actions { display: flex; gap: 10px; justify-content: center; margin: 10px 0; min-height: 48px; }
.actions .secondary { flex: 1; max-width: 200px; }
.nav { display: flex; gap: 10px; margin-top: 14px; }
.nav .ghost { border-color: var(--line); }
.navbtn { flex: 1; min-height: 54px; }
.nav .primary { flex: 1.6; }

.list-head { display: flex; align-items: center; justify-content: space-between; }
.takes { list-style: none; padding: 0; margin: 12px 0; display: grid; gap: 10px; }
.takes li {
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px;
  display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; align-items: start;
}
.takes .num { font-weight: 700; color: var(--muted); min-width: 1.8em; text-align: center; }
.takes .txt { direction: rtl; unicode-bidi: isolate; text-align: right; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.takes .row { grid-column: 2; display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.takes .row .chip { position: static; }
.takes .row button.small { min-height: 38px; padding: 0 12px; border-radius: 10px; font-size: .9rem; }
.takes li.missing { opacity: .65; }
.takes .dur { font-size: .85rem; color: var(--muted); direction: ltr; }

.big-emoji { font-size: 3.4rem; }
.toast {
  position: fixed; inset-inline: 16px; bottom: calc(16px + env(safe-area-inset-bottom)); margin: 0 auto; max-width: 520px;
  background: var(--ink); color: var(--bg); padding: 12px 16px; border-radius: 14px; text-align: center; z-index: 20;
  box-shadow: 0 8px 30px rgba(0,0,0,.25);
}
@media (min-width: 700px) {
  main { padding-top: 28px; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

.takes .case-head { grid-column: 2; display: flex; gap: 8px; align-items: start; justify-content: space-between; }
.takes .case-head .chip { position: static; flex: none; }
.takes .units { grid-column: 1 / -1; display: grid; gap: 6px; margin-top: 6px; }
.takes .units .row { grid-column: auto; padding-inline-start: 2.4em; }
.takes .units .row.missing { opacity: .65; }
.recorder .chip { position: static; display: inline-block; margin-top: 6px; }
.takes .unit { font-size: .85rem; font-weight: 600; color: var(--muted); min-width: 4.2em; }
.loading { text-align: center; color: var(--muted); padding-top: 20vh; }
bdi.lat { unicode-bidi: isolate; direction: ltr; }
.nb { white-space: nowrap; }
.part.ltr { direction: ltr; text-align: left; }
.split .part .nb { white-space: normal; }
.split .part bdi.lat {
  display: block; direction: ltr; text-align: left; margin: 2px 0; padding: 0 6px;
  font-size: .92em; color: var(--brand); border-inline-start: 3px solid var(--brand-soft);
}
