/* =========================================================
   Despair — واجهة القراءة (single-chapter)
   ========================================================= */

/* ---------- الشريط العلوي: زر العودة فقط ---------- */
.ch-topbar {
    position: fixed;
    top: 0;
    inset-inline: 0;
    z-index: 120;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 12px;
    height: 56px;
    padding: 0 14px;
    background: rgba(8, 9, 11, 0.9);
    background: linear-gradient(to bottom, color-mix(in srgb, var(--bg-deep) 94%, transparent), color-mix(in srgb, var(--bg-deep) 60%, transparent) 70%, transparent);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    border-bottom: 0;
    transition: transform 0.3s var(--ease), opacity 0.3s var(--ease);
}

.ch-topbar.ch-hidden { transform: translateY(-100%); opacity: 0; }

.ch-back-btn {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    max-width: 100%;
    padding: 8px 16px 8px 12px;
    border: 1px solid var(--tint-1);
    border-radius: var(--radius-pill);
    background: color-mix(in srgb, var(--surface) 88%, transparent);
    color: var(--text);
    transition: all var(--t-fast);
}

.ch-back-btn:hover {
    border-color: var(--primary-color);
    background: color-mix(in srgb, var(--surface2) 95%, transparent);
    transform: translateX(-2px);
}

.ch-back-btn svg { flex-shrink: 0; color: var(--primary-400); }

.ch-back-meta {
    display: flex;
    flex-direction: column;
    gap: 1px;
    min-width: 0;
}

.ch-back-title {
    max-width: 46vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 14px;
    font-weight: 700;
    line-height: 1.25;
}

.ch-back-sub {
    color: var(--text3);
    font-size: 11.5px;
    font-weight: 600;
    line-height: 1.2;
}

/* ---------- الشريط السفلي ---------- */
.ch-bottombar {
    position: fixed;
    bottom: 0;
    inset-inline: 0;
    z-index: 120;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
    background: rgba(8, 9, 11, 0.92);
    background: linear-gradient(to top, color-mix(in srgb, var(--bg-deep) 96%, transparent), color-mix(in srgb, var(--bg-deep) 75%, transparent) 65%, transparent);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    transition: transform 0.3s var(--ease), opacity 0.3s var(--ease);
}

.ch-bottombar.ch-hidden { transform: translateY(110%); opacity: 0; }

.ch-bar-group {
    display: flex;
    align-items: center;
    gap: 6px;
}

.ch-bar-tools,
.ch-bar-nav {
    padding: 4px;
    border: 1px solid var(--tint-08);
    border-radius: var(--radius-pill);
    background: color-mix(in srgb, var(--surface) 92%, transparent);
}

/* الصفحة RTL: العنصر ذو order الأصغر يظهر على اليمين.
   إذاً: التنقّل (order:1) يميناً، والأدوات (order:2) يساراً.
   لعكس الجهتين أضف الكلاس ch-bottombar--flip على #ch-bottombar. */
.ch-bar-nav   { order: 1; }
.ch-bar-tools { order: 2; }

.ch-bottombar--flip .ch-bar-nav   { order: 2; }
.ch-bottombar--flip .ch-bar-tools { order: 1; }

.ch-icon-btn,
.ch-nav-arrow,
.ch-chapters-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    padding: 9px 14px;
    border: 0;
    border-radius: var(--radius-pill);
    background: transparent;
    color: var(--text2);
    font-family: inherit;
    font-size: 13px;
    font-weight: 700;
    cursor: pointer;
    white-space: nowrap;
    transition: all var(--t-fast);
}

.ch-icon-btn:hover,
.ch-nav-arrow:hover:not(:disabled) {
    color: var(--text);
    background: var(--tint-08);
}

.ch-icon-btn--report:hover { color: var(--color-danger); background: rgba(224, 72, 77, 0.12); }

.ch-chapters-btn {
    background: var(--grad-primary);
    color: var(--text-on-primary);
    box-shadow: 0 4px 14px var(--primary-ring);
}

.ch-chapters-btn:hover { filter: brightness(1.08); }

.ch-nav-arrow:disabled { opacity: 0.28; cursor: not-allowed; }
.ch-nav-arrow svg { flex-shrink: 0; }

/* ---------- شريط التقدّم ---------- */
.ch-progress-container {
    position: fixed;
    bottom: calc(64px + env(safe-area-inset-bottom));
    inset-inline: 0;
    z-index: 115;
    padding: 0 20px;
    transition: opacity 0.3s var(--ease);
}

.ch-bottombar.ch-hidden ~ .ch-progress-container { opacity: 0; pointer-events: none; }

/* ---------- نوافذ القارئ ---------- */
.ch-modal-sheet {
    background: var(--surface);
    border: 1px solid var(--border2);
    border-bottom: 0;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
}

.ch-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 4px 20px 14px;
    border-bottom: 1px solid var(--border);
}

.ch-modal-title { margin: 0; font-size: 17px; font-weight: 800; color: var(--text); }

.ch-modal-close {
    display: grid;
    place-items: center;
    width: 32px;
    height: 32px;
    border: 0;
    border-radius: 50%;
    background: var(--surface3);
    color: var(--text2);
    cursor: pointer;
    transition: all var(--t-fast);
}

.ch-modal-close:hover { color: var(--text); background: var(--border3); }

/* ---------- عناصر الإعدادات ---------- */
.ch-setting-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    padding: 14px 4px;
    border-bottom: 1px solid var(--border);
}

.ch-setting-item:last-child { border-bottom: 0; }

.ch-setting-info { display: flex; flex-direction: column; gap: 3px; min-width: 0; }

.ch-setting-label { color: var(--text); font-size: 14.5px; font-weight: 700; }
.ch-setting-desc { color: var(--text3); font-size: 12px; line-height: 1.5; }

.ch-setting-control { display: flex; align-items: center; gap: 12px; flex: 1; justify-content: flex-end; }

/* تُظهر/تُخفى عبر JS بـ display */
.ch-setting-subgroup {
    display: none;
    padding-inline-start: 12px;
    margin-inline-start: 2px;
    border-inline-start: 2px solid var(--primary-soft);
}

.ch-setting-subgroup .ch-setting-item { border-bottom: 0; padding-block: 10px; }

.ch-slider {
    flex: 1;
    min-width: 100px;
    height: 4px;
    -webkit-appearance: none;
    appearance: none;
    border-radius: 999px;
    background: var(--surface3);
    outline: 0;
    cursor: pointer;
}

.ch-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--primary-color);
    box-shadow: 0 0 0 4px var(--primary-soft);
    cursor: pointer;
}

.ch-slider::-moz-range-thumb {
    width: 16px;
    height: 16px;
    border: 0;
    border-radius: 50%;
    background: var(--primary-color);
    cursor: pointer;
}

.ch-slider-val {
    min-width: 44px;
    color: var(--text2);
    font-size: 12.5px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    text-align: center;
}

/* مفتاح التبديل */
.ch-toggle { position: relative; flex-shrink: 0; cursor: pointer; }
.ch-toggle input { position: absolute; opacity: 0; width: 0; height: 0; }

.ch-toggle-track {
    display: block;
    width: 46px;
    height: 26px;
    border-radius: 999px;
    background: var(--surface3);
    border: 1px solid var(--border2);
    transition: all var(--t-fast);
}

.ch-toggle-track::after {
    content: '';
    position: absolute;
    top: 4px;
    inset-inline-start: 4px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--text3);
    transition: all var(--t-fast);
}

.ch-toggle input:checked + .ch-toggle-track {
    background: var(--primary-soft);
    border-color: var(--primary-color);
}

.ch-toggle input:checked + .ch-toggle-track::after {
    background: var(--primary-400);
    transform: translateX(-20px);
}

html[dir="ltr"] .ch-toggle input:checked + .ch-toggle-track::after { transform: translateX(20px); }

/* ---------- قائمة الفصول داخل القارئ ---------- */
.ch-chapters-list { display: flex; flex-direction: column; gap: 4px; margin: 0; padding: 0; list-style: none; }

.ch-chapters-list li a {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface2);
    color: var(--text2);
    font-size: 14px;
    font-weight: 600;
    transition: all var(--t-fast);
}

.ch-chapters-list li a:hover { color: var(--text); border-color: var(--border3); background: var(--surface3); }

.ch-chapters-list li.is-current a,
.ch-chapters-list li a.current {
    color: var(--text);
    background: var(--primary-soft);
    border-color: var(--primary-color);
}

/* ---------- استجابة ---------- */
@media (max-width: 640px) {
    .ch-btn-label { display: none; }
    .ch-icon-btn { padding: 10px 12px; }
    .ch-nav-text { display: none; }
    .ch-nav-arrow { padding: 10px 12px; }
    .ch-chapters-btn { padding: 9px 14px; }
    .ch-back-title { max-width: 40vw; }
    .ch-bottombar { padding-inline: 10px; }
}

@media (max-width: 380px) {
    .ch-chapters-btn span { display: none; }
}

/* =========================================================
   Despair — إعدادات العرض المقسّمة داخل القارئ
   ========================================================= */

/* ---------- خلفية القراءة ---------- */
.ch-reader-container[data-bg="black"] { --ch-paper: #000000; }
.ch-reader-container[data-bg="dark"]  { --ch-paper: #16120f; }
.ch-reader-container[data-bg="paper"] { --ch-paper: #f2e9d8; }
.ch-reader-container[data-bg="light"] { --ch-paper: #ffffff; }

.ch-reader-container[data-bg] { background: var(--ch-paper, var(--bg-deep)); }
.ch-reader-container[data-bg] .ch-page { background: var(--ch-paper, var(--bg-deep)); }

/* ---------- المسافة بين الصور وعرض الصفحة ---------- */
.ch-pages-track { gap: var(--ch-gap, 0px); }

.ch-page {
    width: var(--ch-width, 100%);
    max-width: 100%;
    margin-inline: auto;
    transition: width 0.2s var(--ease, ease);
}

/* ---------- ملاءمة الصورة ---------- */
.ch-reader-container[data-fit="full"] .ch-page img {
    width: 100%;
    height: auto;
    max-width: 100%;
}

.ch-reader-container[data-fit="original"] .ch-page img {
    width: auto;
    height: auto;
    max-width: 100%;
}

.ch-reader-container[data-fit="screen"] .ch-page img {
    width: auto;
    height: auto;
    max-height: 100vh;
    max-width: 100%;
}
.ch-reader-container[data-fit="screen"] .ch-page { min-height: auto; }

/* ---------- عناصر التحكم المقسّمة ---------- */
.ch-seg-group {
    padding: 14px 0;
    border-bottom: 1px solid var(--tint-07);
}

.ch-seg-title {
    display: flex;
    align-items: center;
    gap: 7px;
    margin-bottom: 10px;
    color: var(--text2);
    font-size: 12.5px;
    font-weight: 700;
}
.ch-seg-title i { font-size: 12px; opacity: .8; }

.ch-seg-sliderrow {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 4px;
}
.ch-seg-sliderrow .ch-seg-title { margin-bottom: 0; }

.ch-seg-val {
    padding: 3px 10px;
    border-radius: var(--radius-pill);
    background: var(--tint-08);
    color: var(--text2);
    font-size: 11.5px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.ch-seg {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: 1fr;
    gap: 7px;
}

.ch-seg-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 12px 6px;
    border: 1px solid var(--tint-12);
    border-radius: var(--radius-sm);
    background: var(--tint-05);
    color: var(--text2);
    font-family: inherit;
    font-size: 12px;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.18s var(--ease, ease);
}
.ch-seg-btn i { font-size: 14px; opacity: .85; }

.ch-seg-dot {
    width: 14px;
    height: 14px;
    border: 1px solid var(--tint-2);
    border-radius: 50%;
    box-shadow: 0 1px 3px rgba(0, 0, 0, .4);
}

.ch-seg-btn:hover {
    color: var(--text);
    border-color: var(--border-primary);
    background: var(--tint-08);
}

.ch-seg-btn.is-active {
    color: var(--text-on-primary);
    border-color: transparent;
    background: var(--grad-primary);
    box-shadow: var(--shadow-primary);
}
.ch-seg-btn.is-active i { opacity: 1; }

@media (max-width: 460px) {
    .ch-seg { grid-auto-flow: row; grid-template-columns: 1fr 1fr; grid-auto-columns: auto; }
    .ch-seg-btn { padding: 10px 6px; font-size: 11.5px; }
}

/* ---------- شريط علوي: مساحة لمبدّل الترجمة ---------- */
.ch-topbar { gap: 12px; }
.ch-topbar .ch-back-btn { min-width: 0; flex-shrink: 1; }
.ch-topbar .ch-back-meta { min-width: 0; display: flex; flex-direction: column; }
.ch-topbar .ch-back-sub { font-size: 11.5px; opacity: .78; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

@media (max-width: 560px) {
    .ch-topbar .ch-back-title { max-width: 92px; }
    .dsp-tswitch-toggle { max-width: 150px; padding: 7px 10px; font-size: 11.5px; }
    .dsp-tswitch-current { max-width: 66px; }
}
