/* ==========================================================================
   Hikayati — public site & story reader
   Mobile first. Logical properties throughout so RTL (Arabic) and LTR share one stylesheet.
   ========================================================================== */

/* ---------- fonts (self-hosted, SIL OFL) ---------- */
@font-face {
    font-family: "Hk Read";
    src: url("/fonts/naskh-arabic.woff2") format("woff2");
    font-weight: 400 700;
    font-display: swap;
    unicode-range: U+0600-06FF, U+0750-077F, U+0870-08FF, U+FB50-FDFF, U+FE70-FEFF, U+200C-200E, U+2010-2011, U+204F, U+2E41;
}
@font-face {
    font-family: "Hk UI";
    src: url("/fonts/kufi-arabic.woff2") format("woff2");
    font-weight: 100 900;
    font-display: swap;
    unicode-range: U+0600-06FF, U+0750-077F, U+0870-08FF, U+FB50-FDFF, U+FE70-FEFF, U+200C-200E, U+2010-2011, U+204F, U+2E41;
}
@font-face {
    font-family: "Hk Read";
    src: url("/fonts/nunito-latin.woff2") format("woff2");
    font-weight: 200 1000;
    font-display: swap;
    unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
    font-family: "Hk Read";
    src: url("/fonts/nunito-latin-ext.woff2") format("woff2");
    font-weight: 200 1000;
    font-display: swap;
    unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
@font-face {
    font-family: "Hk UI";
    src: url("/fonts/nunito-latin.woff2") format("woff2");
    font-weight: 200 1000;
    font-display: swap;
    unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
    font-family: "Hk UI";
    src: url("/fonts/nunito-latin-ext.woff2") format("woff2");
    font-weight: 200 1000;
    font-display: swap;
    unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF;
}

/* ---------- tokens ---------- */
:root {
    color-scheme: light;
    --bg: #fff8ef;
    --paper: #fffaf3;
    --surface: #ffffff;
    --surface-2: #f6f0ff;
    --ink: #2b2240;
    --muted: #6a6180;
    --line: #ebe2f5;
    --primary: #5b3cc4;
    --primary-ink: #ffffff;
    --primary-soft: #ece6ff;
    --accent: #ff9f43;
    --accent-soft: #fff0df;
    --ok: #1f8a5b;
    --danger: #c62f45;
    --whatsapp: #128c4a;
    --shadow: 0 1px 2px rgb(43 34 64 / 6%), 0 8px 24px rgb(43 34 64 / 8%);
    --shadow-lg: 0 12px 40px rgb(43 34 64 / 18%);
    --radius: 18px;
    --font-ui: "Hk UI", system-ui, -apple-system, "Segoe UI", Roboto, "Geeza Pro", Tahoma, sans-serif;
    --font-read: "Hk Read", "Geeza Pro", Georgia, "Times New Roman", serif;
    --read-size: 1.25rem;
    --read-leading: 1.8;
}
:root[data-theme="dark"] {
    color-scheme: dark;
    --bg: #15111f;
    --paper: #17131f;
    --surface: #221b31;
    --surface-2: #2b2340;
    --ink: #f1ecfb;
    --muted: #b6abcd;
    --line: #372d4f;
    --primary: #a993ff;
    --primary-ink: #1b1530;
    --primary-soft: #33295a;
    --accent: #ffb36b;
    --accent-soft: #3a2a1d;
    --ok: #5fd39e;
    --danger: #ff7d8f;
    --whatsapp: #3ccf7d;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 8px 24px rgb(0 0 0 / 35%);
    --shadow-lg: 0 12px 40px rgb(0 0 0 / 55%);
}
/* Reader text size steps, chosen in the reader settings (stored on <html data-size>). */
:root[data-size="0"] { --read-size: 1rem; }
:root[data-size="1"] { --read-size: 1.125rem; }
:root[data-size="2"] { --read-size: 1.25rem; }
:root[data-size="3"] { --read-size: 1.4rem; }
:root[data-size="4"] { --read-size: 1.6rem; }
:root[data-size="5"] { --read-size: 1.85rem; }
:lang(ar) { --read-leading: 2; }

/* ---------- base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
    margin: 0;
    min-height: 100vh;
    min-height: 100dvh;
    background: var(--bg);
    color: var(--ink);
    font-family: var(--font-ui);
    font-size: 1rem;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
img { max-width: 100%; }
a { color: var(--primary); }
h1, h2, h3 { line-height: 1.3; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 8px; }
.needs-js { display: none !important; }
html.js .needs-js { display: revert !important; }
html.js .needs-js[hidden] { display: none !important; }
.muted { color: var(--muted); }
.icon { inline-size: 1.4em; block-size: 1.4em; flex: none; }
[dir="rtl"] .flip-rtl { transform: scaleX(-1); }
.skip-link { position: absolute; inset-inline-start: -999px; top: 8px; z-index: 100; background: var(--surface); padding: 8px 14px; border-radius: 10px; }
.skip-link:focus { inset-inline-start: 8px; }

/* ---------- buttons ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    min-height: 44px; padding: 0 18px; border-radius: 999px; border: 0;
    font: inherit; font-weight: 700; text-decoration: none; cursor: pointer;
    background: var(--surface-2); color: var(--ink);
    white-space: nowrap;
    transition: transform .1s ease, background-color .15s ease;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--primary); color: var(--primary-ink); }
.btn-soft { background: var(--primary-soft); color: var(--primary); }
.btn-ghost { background: transparent; color: var(--ink); box-shadow: inset 0 0 0 1.5px var(--line); }
.btn-whatsapp { background: var(--whatsapp); color: #fff; }
.btn-block { inline-size: 100%; }
.icon-btn {
    display: inline-grid; place-items: center; inline-size: 44px; block-size: 44px; flex: none;
    border-radius: 50%; border: 0; background: transparent; color: var(--ink); cursor: pointer;
    list-style: none; text-decoration: none;
}
.icon-btn:hover, .icon-btn:focus-visible { background: var(--surface-2); }
summary.icon-btn::-webkit-details-marker { display: none; }

/* ---------- header / footer ---------- */
.site-header {
    position: sticky; top: 0; z-index: 30;
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    padding: calc(10px + env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    -webkit-backdrop-filter: saturate(1.4) blur(10px);
    backdrop-filter: saturate(1.4) blur(10px);
    border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--ink); min-width: 0; }
.brand img { border-radius: 12px; }
.brand-text { display: grid; line-height: 1.15; min-width: 0; }
.brand-name { font-weight: 800; font-size: 1.2rem; }
.brand-tag { font-size: .8rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.header-actions { display: flex; align-items: center; gap: 8px; }
.site-main { padding: 8px max(16px, env(safe-area-inset-right)) 32px max(16px, env(safe-area-inset-left)); max-width: 1180px; margin: 0 auto; }
.site-footer { text-align: center; color: var(--muted); font-size: .85rem; padding: 24px 16px calc(24px + env(safe-area-inset-bottom)); }

/* language menu (works without JS thanks to <details>) */
.lang-menu { position: relative; }
.lang-menu > summary {
    display: inline-flex; align-items: center; gap: 6px; min-height: 44px; padding: 0 14px;
    border-radius: 999px; background: var(--surface); box-shadow: inset 0 0 0 1.5px var(--line);
    cursor: pointer; list-style: none; font-weight: 700; font-size: .95rem;
}
.lang-menu > summary::-webkit-details-marker { display: none; }
.lang-list {
    position: absolute; inset-inline-end: 0; top: calc(100% + 6px); z-index: 40;
    min-inline-size: 180px; margin: 0; padding: 6px; list-style: none;
    background: var(--surface); border-radius: 14px; box-shadow: var(--shadow-lg);
}
.lang-list form { margin: 0; }
.lang-list button { inline-size: 100%; text-align: start; border: 0; background: none; font: inherit; cursor: pointer; }
.lang-list a, .lang-list button { display: block; padding: 10px 14px; border-radius: 10px; text-decoration: none; color: var(--ink); font-weight: 600; }
.lang-list a:hover, .lang-list a:focus-visible, .lang-list button:hover, .lang-list button:focus-visible { background: var(--surface-2); }
.lang-list [aria-current="true"] { background: var(--primary-soft); color: var(--primary); }

.offline-banner { background: var(--accent-soft); color: var(--ink); text-align: center; padding: 8px 16px; font-size: .9rem; }
.ios-hint {
    position: fixed; inset-inline: 12px; bottom: calc(12px + env(safe-area-inset-bottom)); z-index: 60;
    display: flex; gap: 12px; align-items: center; justify-content: space-between;
    padding: 14px 16px; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow-lg);
}
.ios-hint p { margin: 0; }

/* ---------- library ---------- */
.hero { padding: 20px 0 8px; }
.hero h1 { margin: 0 0 4px; font-size: clamp(1.6rem, 5vw, 2.4rem); }
.hero p { margin: 0; color: var(--muted); font-size: 1.05rem; }
.empty { padding: 40px 0; text-align: center; color: var(--muted); }
.story-grid {
    list-style: none; margin: 20px 0 0; padding: 0;
    display: grid; gap: 20px; align-items: start;
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 300px), 1fr));
}
.story-card {
    display: flex; flex-direction: column; overflow: hidden;
    background: var(--surface); color: var(--ink); text-decoration: none;
    border-radius: var(--radius); box-shadow: var(--shadow);
    transition: transform .15s ease, box-shadow .15s ease;
}
.story-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }
/* Whole illustration, never cropped: the box takes the image's own aspect ratio. */
.card-media { background: var(--surface-2); }
.card-media img { display: block; margin-inline: auto; inline-size: auto; max-inline-size: 100%; block-size: auto; max-block-size: min(85vw, 380px); }
.card-body { padding: 14px 18px 0; }
.card-body h2 { margin: 0 0 6px; font-size: 1.25rem; }
.card-body p { margin: 0; color: var(--muted); font-family: var(--font-read); font-size: 1.02rem; }
.card-meta { padding: 12px 18px 18px; display: grid; gap: 10px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 0; padding: 0; list-style: none; }
.chips li { font-size: .82rem; padding: 3px 10px; border-radius: 999px; background: var(--surface-2); color: var(--muted); font-weight: 600; }
.chips .chip-saved { background: color-mix(in srgb, var(--ok) 15%, transparent); color: var(--ok); }
.card-note { margin: 0; font-size: .85rem; color: var(--muted); }
.card-cta { display: inline-flex; align-items: center; gap: 4px; color: var(--primary); font-weight: 800; }

/* ---------- message pages / auth ---------- */
.message-page { text-align: center; max-width: 520px; margin: 48px auto; }
.message-icon { font-size: 3rem; }
.auth-card { max-width: 420px; margin: 32px auto; padding: 24px; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); }
.auth-card h1 { margin-top: 0; font-size: 1.5rem; }
.form-stack { display: grid; gap: 14px; }
.field { display: grid; gap: 6px; }
.field > span { font-weight: 700; font-size: .92rem; }
.field input, .field select, .field textarea {
    font: inherit; color: var(--ink); background: var(--paper);
    border: 1.5px solid var(--line); border-radius: 12px; padding: 10px 12px; min-height: 44px; inline-size: 100%;
}
.field textarea { font-family: var(--font-read); line-height: 1.7; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.check, .radio { display: flex; align-items: center; gap: 10px; min-height: 40px; cursor: pointer; }
.check input, .radio input { inline-size: 20px; block-size: 20px; accent-color: var(--primary); flex: none; }
.alert { padding: 12px 16px; border-radius: 12px; }
.alert ul { margin: 0; padding-inline-start: 18px; }
.alert-error { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); }
.alert-ok { background: color-mix(in srgb, var(--ok) 12%, transparent); color: var(--ok); }
.help { color: var(--muted); font-size: .85rem; }

/* ==========================================================================
   Story reader
   The reader is a fixed, full-viewport column: top bar · stage · ONE nav bar.
   Only the text panel scrolls; the illustration stays in view.
   ========================================================================== */
.reader {
    position: fixed; inset: 0;
    display: flex; flex-direction: column;
    background: var(--paper);
    padding-top: env(safe-area-inset-top);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    overflow: hidden;
}
.reader-bar { flex: none; display: flex; align-items: center; gap: 2px; padding: 6px 8px; }
.reader-title { flex: 1; min-width: 0; margin: 0 6px; font-size: 1rem; font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.notice { flex: none; margin: 0 12px 6px; padding: 8px 12px; border-radius: 12px; background: var(--accent-soft); font-size: .9rem; }
.notice-warn { background: color-mix(in srgb, var(--danger) 14%, transparent); }
.notice-inline { margin: 0 0 1em; }

.popover { position: relative; }
.popover > summary { list-style: none; }
.popover-panel {
    position: absolute; inset-inline-end: 0; top: calc(100% + 6px); z-index: 50;
    inline-size: min(330px, calc(100vw - 24px));
    max-block-size: min(70vh, 560px); overflow: auto; overscroll-behavior: contain;
    display: grid; gap: 12px; padding: 16px;
    background: var(--surface); color: var(--ink);
    border-radius: var(--radius); box-shadow: var(--shadow-lg);
}
.popover-title { margin: 0; font-weight: 800; }
.share-url { inline-size: 100%; font: inherit; font-size: .85rem; direction: ltr; padding: 8px 10px; border-radius: 10px; border: 1.5px solid var(--line); background: var(--paper); color: var(--muted); }
.setting { display: grid; gap: 8px; border: 0; margin: 0; padding: 0; }
.setting-label { font-weight: 700; font-size: .9rem; color: var(--muted); padding: 0; }
.setting-status { font-size: .88rem; color: var(--ok); }
.segmented { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 6px; }
.segmented button {
    min-height: 44px; border-radius: 12px; border: 0; cursor: pointer;
    font: inherit; font-weight: 800; background: var(--surface-2); color: var(--ink);
}
.segmented button[aria-checked="true"] { background: var(--primary); color: var(--primary-ink); }
.voices .radio span small { color: var(--muted); }

.reader-stage { position: relative; flex: 1 1 auto; min-height: 0; }
.scene { position: absolute; inset: 0; display: flex; flex-direction: column; }

/* Portrait: illustration on top, sized to its own aspect ratio (no letterbox bands), text scrolls below. */
.scene-figure {
    flex: none; margin: 0; padding: 4px 12px 6px;
    display: flex; justify-content: center; align-items: center;
}
.scene-figure img {
    display: block;
    inline-size: auto; block-size: auto;
    max-inline-size: 100%;
    max-block-size: 42vh;
    max-block-size: 42dvh;
    object-fit: contain;
    border-radius: 16px;
    box-shadow: var(--shadow);
    background: var(--surface-2);
    cursor: zoom-in;
    touch-action: pan-y;
}
.scene-text {
    flex: 1 1 auto; min-height: 0;
    overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
    padding: 6px 20px 28px;
    font-family: var(--font-read); font-size: var(--read-size); line-height: var(--read-leading);
    scrollbar-gutter: stable;
}
.scene-text > * { max-inline-size: 42em; margin-inline: auto; }
.scene-text:focus-visible { outline-offset: -3px; }
.scene-title { font-family: var(--font-ui); font-size: 1.15em; margin: .3em auto .6em; color: var(--primary); outline: none; }
.scene-text p { margin-block: 0 .85em; }
.scene-question {
    margin-block: 1.4em .6em; padding: 14px 16px; border-radius: 16px;
    background: var(--primary-soft); font-size: .95em;
}
.scene-question strong { display: block; font-family: var(--font-ui); font-size: .8em; color: var(--primary); margin-bottom: 4px; }
.scene-question p { margin: 0; }
.the-end { text-align: center; padding-top: .6em; font-family: var(--font-ui); font-size: 1rem; }

/* Tap the illustration to see it large; tap again to return to the text. */
.scene.zoomed .scene-text { display: none; }
.scene.zoomed .scene-figure { flex: 1 1 auto; min-height: 0; }
.scene.zoomed .scene-figure img { max-block-size: 100%; cursor: zoom-out; }

.scene.no-image .scene-text { padding-top: 16px; }

/* Short portrait screens (small phones, iPhone SE): give the text more room. */
@media (orientation: portrait) and (max-height: 700px) {
    .scene-figure img { max-block-size: 36vh; max-block-size: 36dvh; }
}

/* Landscape phones, tablets and desktops: illustration and text side by side. */
@media (min-aspect-ratio: 5/4) {
    .scene { flex-direction: row; }
    .scene-figure { flex: 0 0 52%; block-size: 100%; padding: 8px 12px 12px; }
    .scene-figure img { max-block-size: 100%; }
    .scene-text { flex: 1 1 0; block-size: 100%; padding: 12px 24px 28px; }
    .scene.zoomed .scene-figure { flex-basis: 100%; }
}
@media (min-aspect-ratio: 5/4) and (min-width: 1200px) {
    .scene-figure { flex-basis: 56%; padding: 12px 24px 20px; }
}

/* The single navigation bar. */
.reader-nav {
    position: relative; flex: none;
    display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px;
    padding: 8px max(12px, env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
    background: var(--surface);
    box-shadow: 0 -1px 0 var(--line);
}
.progress { position: absolute; inset-inline: 0; top: 0; block-size: 3px; background: var(--line); }
.progress span { display: block; block-size: 100%; background: var(--accent); transition: width .25s ease; }
.nav-btn {
    display: inline-flex; align-items: center; gap: 4px;
    min-block-size: 52px; min-inline-size: 52px; padding: 0 16px;
    border-radius: 999px; text-decoration: none; font-weight: 800; font-size: 1.02rem;
    color: var(--ink); background: var(--surface-2); justify-self: start;
    user-select: none; -webkit-user-select: none;
}
.nav-btn.nav-next { justify-self: end; background: var(--primary); color: var(--primary-ink); }
.nav-btn[aria-disabled="true"] { opacity: .35; pointer-events: none; }
.nav-center { display: flex; align-items: center; gap: 10px; }
.counter { font-weight: 800; color: var(--muted); font-size: .95rem; white-space: nowrap; font-variant-numeric: tabular-nums; }
.listen-btn {
    display: inline-grid; place-items: center; inline-size: 52px; block-size: 52px; border-radius: 50%;
    border: 0; cursor: pointer; background: var(--accent); color: #2b1a05;
}
.listen-btn .pause { display: none; }
.listen-btn[aria-pressed="true"] .play { display: none; }
.listen-btn[aria-pressed="true"] .pause { display: block; }

@media (max-width: 520px) {
    .brand-tag { display: none; }
    .site-header .btn { padding: 0 12px; min-height: 40px; font-size: .9rem; }
    .lang-menu > summary { padding: 0 10px; min-height: 40px; }
}
@media (max-width: 380px) {
    .nav-btn { padding: 0 12px; font-size: .95rem; }
    .scene-text { padding-inline: 16px; }
}

.toast {
    position: fixed; inset-inline: 0; margin-inline: auto; inline-size: fit-content; max-inline-size: calc(100vw - 32px);
    bottom: calc(90px + env(safe-area-inset-bottom)); z-index: 70;
    padding: 10px 16px; border-radius: 999px; background: var(--ink); color: var(--bg); font-weight: 700;
    box-shadow: var(--shadow-lg);
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { transition: none !important; animation: none !important; }
}
