:root {
  --bg: #0e1412;
  --surface: #161e1b;
  --surface-2: #1e2824;
  --line: #2a3631;
  --text: #f2f0e8;
  --muted: #a3ada7;
  --faint: #6f7a74;
  --accent: #34c38f;
  --accent-ink: #062016;
  --rec: #ef4f4f;
  --warn: #f2b14a;
  --quran: #d9b45a;
  --hadith: #6fb5ff;
  --dua: #c49bff;
  --font-id: 21px;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #f7f5ef;
  --surface: #ffffff;
  --surface-2: #f0ede4;
  --line: #e1ddd1;
  --text: #16201c;
  --muted: #56615b;
  --faint: #8b948f;
  --accent: #128a5e;
  --accent-ink: #ffffff;
  --rec: #d62f2f;
  --quran: #9a7413;
  --hadith: #1d6fc4;
  --dua: #7a45c9;
  color-scheme: light;
}
@media (prefers-color-scheme: light) {
  :root[data-theme="auto"] {
    --bg: #f7f5ef; --surface: #ffffff; --surface-2: #f0ede4; --line: #e1ddd1;
    --text: #16201c; --muted: #56615b; --faint: #8b948f; --accent: #128a5e;
    --accent-ink: #ffffff; --rec: #d62f2f; --quran: #9a7413; --hadith: #1d6fc4; --dua: #7a45c9;
    color-scheme: light;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; letter-spacing: -.01em; white-space: nowrap; flex: none; }
.brand-mark { width: 22px; height: 22px; color: var(--accent); }
.status {
  margin-left: auto;
  display: inline-flex; align-items: center; gap: 7px;
  padding: 5px 11px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted);
  font-size: 13px; font-weight: 600; white-space: nowrap;
  min-width: 0; overflow: hidden; text-overflow: ellipsis;
}
#statusText { overflow: hidden; text-overflow: ellipsis; }
.topbar .icon-btn { flex: none; }
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); }
.status[data-state="listening"] { color: var(--text); }
.status[data-state="listening"] .dot { background: var(--rec); animation: pulse 1.4s infinite; }
.status[data-state="busy"] .dot { background: var(--warn); }
.status[data-state="error"] { color: var(--rec); }
.status[data-state="error"] .dot { background: var(--rec); }
.status[data-state="done"] .dot { background: var(--accent); }
@keyframes pulse { 50% { opacity: .3; } }

.icon-btn {
  width: 40px; height: 40px; display: grid; place-items: center;
  border: 0; border-radius: 12px; background: transparent; color: var(--text);
}
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn:active { background: var(--surface-2); }

/* ---------- Feed ---------- */
.feed {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 16px 16px 24px;
}
.cards { display: flex; flex-direction: column; gap: 12px; max-width: 720px; margin: 0 auto; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px 15px;
  animation: rise .25s ease-out;
}
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }
.card.latest { border-color: color-mix(in srgb, var(--accent) 55%, var(--line)); }
.card .tag {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; font-weight: 700; letter-spacing: .03em; text-transform: uppercase;
  margin-bottom: 6px;
}
.card[data-type="quran"] { border-left: 4px solid var(--quran); }
.card[data-type="quran"] .tag { color: var(--quran); }
.card[data-type="hadith"] { border-left: 4px solid var(--hadith); }
.card[data-type="hadith"] .tag { color: var(--hadith); }
.card[data-type="dua"] { border-left: 4px solid var(--dua); }
.card[data-type="dua"] .tag { color: var(--dua); }

.card .id {
  font-size: var(--font-id);
  line-height: 1.5;
  font-weight: 500;
  margin: 0;
  text-wrap: pretty;
}
.card .ar {
  font-family: "Noto Naskh Arabic", "Geeza Pro", "Times New Roman", serif;
  direction: rtl; text-align: right;
  font-size: calc(var(--font-id) * .82);
  line-height: 1.85;
  color: var(--muted);
  margin: 10px 0 0;
  padding-top: 10px;
  border-top: 1px dashed var(--line);
}
body.hide-ar .card .ar { display: none; }
.card .meta { margin-top: 8px; font-size: 12px; color: var(--faint); display: flex; gap: 10px; }

.card.streaming { border-color: color-mix(in srgb, var(--accent) 55%, var(--line)); }
.card .caret {
  display: inline-block; width: .5em; height: 1em; margin-left: 2px; vertical-align: -0.12em;
  background: var(--accent); border-radius: 2px; animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }
.switch { width: 46px; height: 26px; accent-color: var(--accent); }
.card.pending .id { color: var(--faint); font-size: 16px; display: flex; align-items: center; gap: 10px; }
.card.pending .id::before {
  content: ""; width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid var(--line); border-top-color: var(--accent);
  animation: spin .8s linear infinite; flex: none;
}
@keyframes spin { to { transform: rotate(360deg); } }
.card.failed { border-style: dashed; }
.card.failed .id { color: var(--rec); font-size: 15px; }
.card.failed button { margin-top: 8px; }

/* ---------- Empty state ---------- */
.empty { max-width: 460px; margin: 6vh auto 0; text-align: center; padding: 0 8px; }
.empty-icon { font-size: 44px; }
.empty h1 { font-size: 24px; line-height: 1.25; margin: 10px 0 18px; letter-spacing: -.02em; }
.steps { text-align: left; margin: 0 auto; padding: 0; list-style: none; counter-reset: s; display: grid; gap: 10px; }
.steps li {
  counter-increment: s; display: flex; gap: 12px; align-items: flex-start;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px;
  font-size: 16px;
}
.steps li::before {
  content: counter(s); flex: none; width: 24px; height: 24px; border-radius: 50%;
  display: grid; place-items: center; font-size: 13px; font-weight: 700;
  background: var(--accent); color: var(--accent-ink);
}
.hint { color: var(--muted); font-size: 14px; margin-top: 18px; }

.jump {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(118px + var(--safe-b)); z-index: 6;
  border: 0; border-radius: 999px; padding: 9px 16px;
  background: var(--accent); color: var(--accent-ink); font-weight: 700; font-size: 14px;
  box-shadow: 0 6px 20px rgba(0,0,0,.3);
}

/* ---------- Controls ---------- */
.controls {
  border-top: 1px solid var(--line);
  background: var(--surface);
  padding: 0 16px calc(12px + var(--safe-b));
}
.meter { position: relative; height: 4px; background: var(--surface-2); margin: 0 -16px 10px; overflow: hidden; }
.meter-bar { height: 100%; width: 0; background: var(--accent); transition: width .08s linear; }
.meter-thr { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--warn); left: 0; opacity: .8; }
.controls-row { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 10px; max-width: 720px; margin: 0 auto; }
.side { display: flex; gap: 8px; align-items: center; }
.side.right { justify-content: flex-end; }
.pill-btn {
  min-width: 44px; height: 40px; padding: 0 10px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--surface-2);
  font-weight: 700; font-size: 15px;
}
.pill-btn[aria-pressed="false"] { opacity: .45; }
#arToggle { font-family: "Noto Naskh Arabic", serif; font-size: 15px; }
.timer { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 14px; min-width: 44px; text-align: right; }

.rec {
  width: 84px; height: 84px; border-radius: 50%;
  border: 0; background: var(--accent); color: var(--accent-ink);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  font-weight: 800; font-size: 14px;
  box-shadow: 0 8px 24px color-mix(in srgb, var(--accent) 35%, transparent);
  transition: transform .1s, background .2s;
}
.rec:active { transform: scale(.96); }
.rec-icon { width: 22px; height: 22px; border-radius: 50%; background: currentColor; }
.rec.on { background: var(--rec); color: #fff; box-shadow: 0 8px 24px color-mix(in srgb, var(--rec) 40%, transparent); }
.rec.on .rec-icon { border-radius: 5px; width: 20px; height: 20px; }
.rec:disabled { opacity: .5; }

/* ---------- Sheets ---------- */
.sheet { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.5); }
.sheet-panel {
  position: relative; width: 100%; max-width: 640px; max-height: 88vh;
  background: var(--surface); border-radius: 22px 22px 0 0;
  display: flex; flex-direction: column;
  padding-bottom: var(--safe-b);
  animation: up .22s ease-out;
}
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 12px 6px 20px; }
.sheet-head h2 { margin: 0; font-size: 19px; }
.sheet-body { overflow-y: auto; padding: 4px 20px 24px; }
.group { padding: 14px 0; border-bottom: 1px solid var(--line); }
.group:last-child { border-bottom: 0; }
.group h3 { margin: 0 0 10px; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; font-size: 15px; }
.row select, .row input[type="range"] { flex: 0 1 58%; }
select {
  background: var(--surface-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 10px; padding: 8px 10px; font: inherit; font-size: 14px;
}
input[type="range"] { accent-color: var(--accent); }
.file-row input { max-width: 58%; font-size: 13px; color: var(--muted); }
.small { font-size: 13px; color: var(--muted); margin: 6px 0 0; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }
.btn {
  flex: 1 1 auto; min-height: 46px; padding: 0 16px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface-2); font-weight: 600; font-size: 15px;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.danger { color: var(--rec); }
.btn.sm { min-height: 34px; font-size: 13px; padding: 0 12px; flex: 0 0 auto; }

.history { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; }
.history li {
  display: flex; align-items: center; gap: 10px;
  background: var(--surface-2); border-radius: 12px; padding: 10px 12px;
}
.history .h-main { flex: 1; min-width: 0; }
.history .h-title { font-weight: 600; font-size: 14px; }
.history .h-sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.history .empty-h { color: var(--muted); font-size: 14px; background: none; padding: 0; }

.prose { font-size: 16px; line-height: 1.6; }
.prose h1, .prose h2, .prose h3 { font-size: 18px; margin: 14px 0 6px; }
.prose ul { padding-left: 20px; margin: 6px 0; }
.prose li { margin: 4px 0; }
.prose p { margin: 8px 0; }

/* ---------- PIN ---------- */
.pin { position: fixed; inset: 0; z-index: 30; background: var(--bg); display: grid; place-items: center; padding: 24px; }
.pin-card { width: 100%; max-width: 340px; display: grid; gap: 12px; text-align: center; }
.pin-card h2 { margin: 0; }
.pin-card input {
  font-size: 28px; text-align: center; letter-spacing: .3em; padding: 12px;
  border-radius: 14px; border: 1px solid var(--line); background: var(--surface); color: var(--text);
}
.error { color: var(--rec); font-size: 14px; margin: 0; }
code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; font-size: .9em; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 40;
  bottom: calc(124px + var(--safe-b));
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px;
  font-size: 14px; font-weight: 600; max-width: 90vw; text-align: center;
}

@media (min-width: 720px) {
  .feed { padding-top: 24px; }
}
