/* ===========================================================================
   The Cosmos of Letters — 3D timeline styling
   Fullscreen WebGL stage + HUD. Dark design tokens are declared here because
   this page intentionally does NOT load main.css (the overlay from authors.css
   still needs its variables).
   =========================================================================== */

/* Platform default modern Kufi for Arabic. The cosmos does NOT load main.css,
   so the webfont must be declared here (paths relative to /assets/css/). */
@font-face {
    font-family: "Noto Kufi Arabic";
    src: url("../fonts/NotoKufiArabic-500.ttf") format("truetype");
    font-weight: 400 600;
    font-display: swap;
}
@font-face {
    font-family: "Noto Kufi Arabic";
    src: url("../fonts/NotoKufiArabic-700.ttf") format("truetype");
    font-weight: 700;
    font-display: swap;
}

:root {
    --bg-primary: #0a0d18;
    --bg-secondary: #121728;
    --bg-tertiary: #1b2236;
    --text-primary: #f2f4fb;
    --text-secondary: #a9b0c7;
    --text-muted: #6b7290;
    --border-color: rgba(255, 255, 255, 0.10);
    --color-primary: #8aa2ff;
}

/* Arabic: force the platform's modern Kufi across the ENTIRE cosmos DOM —
   splash/loader (title, tagline, button), HUD, era nav, filters and the era
   story card. Latin (en/fr) keeps the elegant serif. Uses !important to beat
   the per-element Georgia/serif declarations below.
   (Only affects DOM text; the WebGL era labels are handled in timeline-3d.js.) */
[dir="rtl"] .cosmos-body,
[dir="rtl"] .cosmos-body *,
[dir="rtl"] .cosmos-body button,
[dir="rtl"] .cosmos-body input,
[dir="rtl"] .cosmos-body textarea,
[dir="rtl"] .cosmos-body select {
    font-family: "Noto Kufi Arabic", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif !important;
}

* { box-sizing: border-box; }
html, body {
    margin: 0; padding: 0; height: 100%; width: 100%;
    overflow: hidden; background: #05060d; color: var(--text-primary);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    -webkit-font-smoothing: antialiased;
}
.cosmos-body { position: fixed; inset: 0; }

/* ---- Stage ---- */
.cosmos-stage { position: fixed; inset: 0; z-index: 1; }
.cosmos-stage canvas { display: block; touch-action: none; }

/* Fade overlay for transitions */
.cosmos-fade {
    position: fixed; inset: 0; z-index: 40; pointer-events: none;
    background: #05060d; opacity: 0; transition: opacity 0.6s ease;
}

/* ---- Loader / entrance ---- */
.cosmos-loader {
    position: fixed; inset: 0; z-index: 100;
    display: flex; align-items: center; justify-content: center;
    background: radial-gradient(1200px 700px at 50% 40%, #12183a 0%, #080a16 55%, #04050b 100%);
    transition: opacity 0.85s ease, visibility 0.85s ease;
    overflow: hidden;
}
.cosmos-loader.hide { opacity: 0; visibility: hidden; }
.cosmos-loader-stars {
    position: absolute; inset: 0;
    background-image:
        radial-gradient(1.5px 1.5px at 20% 30%, rgba(255,255,255,.9), transparent),
        radial-gradient(1.5px 1.5px at 70% 60%, rgba(255,255,255,.7), transparent),
        radial-gradient(2px 2px at 40% 80%, rgba(190,210,255,.8), transparent),
        radial-gradient(1px 1px at 85% 25%, rgba(255,255,255,.8), transparent),
        radial-gradient(1.5px 1.5px at 55% 15%, rgba(255,240,220,.8), transparent),
        radial-gradient(1px 1px at 10% 70%, rgba(255,255,255,.7), transparent);
    background-repeat: no-repeat; opacity: 0.9;
    animation: cosmosDrift 60s linear infinite alternate;
}
@keyframes cosmosDrift { from { transform: scale(1) translateY(0); } to { transform: scale(1.15) translateY(-20px); } }
.cosmos-loader-inner { position: relative; z-index: 2; text-align: center; padding: 2rem; max-width: 560px; }
.cosmos-loader-title {
    font-family: Georgia, "Times New Roman", serif;
    font-size: clamp(2rem, 6vw, 3.4rem); font-weight: 700; margin: 0 0 0.6rem;
    letter-spacing: 0.01em;
    background: linear-gradient(120deg, #cfe0ff, #b8a6ff 50%, #ffd9b0);
    -webkit-background-clip: text; background-clip: text; color: transparent;
    text-shadow: 0 0 40px rgba(140, 160, 255, 0.25);
}
.cosmos-loader-tagline { color: var(--text-secondary); font-size: clamp(1rem, 2.4vw, 1.2rem); margin: 0 0 2rem; line-height: 1.6; }
.cosmos-progress { width: 260px; max-width: 80vw; height: 3px; margin: 0 auto 0.9rem; background: rgba(255,255,255,0.12); border-radius: 999px; overflow: hidden; }
.cosmos-progress-bar { height: 100%; width: 0; background: linear-gradient(90deg, #8aa2ff, #c8a6ff); border-radius: 999px; transition: width 0.4s ease; }
.cosmos-status { color: var(--text-muted); font-size: 0.85rem; min-height: 1.2em; margin: 0 0 1.6rem; }
.cosmos-enter-btn {
    appearance: none; border: 1px solid rgba(160,180,255,0.4); cursor: pointer;
    background: linear-gradient(135deg, rgba(138,162,255,0.22), rgba(200,166,255,0.16));
    color: #eef2ff; font-size: 1.05rem; font-weight: 600; letter-spacing: 0.04em;
    padding: 0.85rem 2.4rem; border-radius: 999px; opacity: 0.35; pointer-events: none;
    transition: opacity 0.4s ease, transform 0.2s ease, box-shadow 0.3s ease;
    backdrop-filter: blur(6px);
}
.cosmos-enter-btn.ready { opacity: 1; pointer-events: auto; box-shadow: 0 0 30px rgba(138,162,255,0.35); }
.cosmos-enter-btn.ready:hover { transform: translateY(-2px); box-shadow: 0 0 44px rgba(138,162,255,0.55); }
.cosmos-enter-btn:disabled { cursor: default; }

/* ---- HUD (top-right) ---- */
.cosmos-topright {
    position: fixed; top: 16px; right: 16px; z-index: 50;
    display: flex; gap: 8px; align-items: center;
}
.cosmos-hud-btn {
    display: inline-flex; align-items: center; gap: 7px;
    height: 42px; padding: 0 14px; border-radius: 999px;
    background: rgba(18, 22, 40, 0.6); border: 1px solid var(--border-color);
    color: var(--text-primary); font-size: 0.85rem; font-weight: 600;
    cursor: pointer; text-decoration: none; white-space: nowrap;
    backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
    transition: background 0.2s, border-color 0.2s, transform 0.15s;
}
.cosmos-hud-btn:hover { background: rgba(30, 38, 66, 0.8); border-color: rgba(160,180,255,0.5); transform: translateY(-1px); }
.cosmos-hud-btn.on { border-color: #8aa2ff; color: #cfe0ff; box-shadow: 0 0 18px rgba(138,162,255,0.4); }
.cosmos-hud-btn .ico { font-size: 1rem; line-height: 1; }
#cosmos-audio { width: 42px; padding: 0; justify-content: center; font-size: 1.1rem; }
@media (max-width: 640px) { .cosmos-hud-btn .lbl { display: none; } .cosmos-hud-btn { padding: 0 12px; width: 42px; justify-content: center; } .cosmos-hud-btn.cosmos-switch { width: auto; } }

/* ---- Top-left cluster (eras + filters) ---- */
.cosmos-topleft { position: fixed; top: 16px; left: 16px; z-index: 50; display: flex; gap: 8px; }
.cosmos-tl-btn {
    height: 42px; padding: 0 16px; border-radius: 999px;
    background: rgba(18, 22, 40, 0.6); border: 1px solid var(--border-color);
    color: var(--text-primary); font-size: 0.85rem; font-weight: 600; cursor: pointer;
    white-space: nowrap;
    backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
    transition: background 0.2s, border-color 0.2s;
}
.cosmos-tl-btn:hover { background: rgba(30, 38, 66, 0.8); }
.cosmos-tl-btn.on { border-color: #8aa2ff; color: #cfe0ff; }

/* ---- Era navigation ---- */
.cosmos-eras {
    position: fixed; top: 68px; left: 16px; z-index: 49;
    display: flex; flex-direction: column; gap: 4px;
    width: 240px; max-height: calc(100vh - 90px); overflow-y: auto;
    padding: 8px; border-radius: 16px;
    background: rgba(12, 16, 30, 0.72); border: 1px solid var(--border-color);
    backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
    opacity: 0; visibility: hidden; transform: translateX(-12px);
    transition: opacity 0.25s ease, transform 0.25s ease, visibility 0.25s ease;
}
.cosmos-eras.open { opacity: 1; visibility: visible; transform: none; }
.cosmos-era-item {
    display: flex; align-items: center; gap: 10px; width: 100%;
    padding: 10px 12px; border: none; border-radius: 10px; cursor: pointer;
    background: transparent; color: var(--text-secondary); text-align: start;
    font-size: 0.9rem; transition: background 0.15s, color 0.15s;
}
.cosmos-era-item:hover { background: rgba(255,255,255,0.06); color: var(--text-primary); }
.cosmos-era-item.active { background: color-mix(in srgb, var(--era) 22%, transparent); color: #fff; }
.cosmos-era-item .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--era); box-shadow: 0 0 10px var(--era); flex-shrink: 0; }
.cosmos-era-item .nm { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cosmos-era-item .ct { font-size: 0.75rem; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.cosmos-era-item.is-dim { opacity: 0.4; }

/* ---- Filters panel ---- */
.cosmos-filters {
    position: fixed; top: 68px; left: 16px; z-index: 49;
    display: flex; flex-direction: column; gap: 10px;
    width: 268px; max-width: calc(100vw - 32px);
    overflow: visible; /* let the Origin dropdown expand without clipping */
    padding: 14px; border-radius: 16px;
    background: rgba(12, 16, 30, 0.78); border: 1px solid var(--border-color);
    backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
    opacity: 0; visibility: hidden; transform: translateX(-12px);
    transition: opacity 0.25s ease, transform 0.25s ease, visibility 0.25s ease;
}
.cosmos-filters.open { opacity: 1; visibility: visible; transform: none; }
.cosmos-search { position: relative; display: flex; align-items: center; }
.cosmos-search svg { position: absolute; inset-inline-start: 12px; color: var(--text-muted); pointer-events: none; }
.cosmos-search input {
    width: 100%; height: 40px; padding: 0 14px; padding-inline-start: 38px;
    border-radius: 10px; border: 1px solid var(--border-color);
    background: rgba(0,0,0,0.25); color: var(--text-primary); font-size: 0.9rem;
}
.cosmos-search input:focus { outline: none; border-color: #8aa2ff; box-shadow: 0 0 0 3px rgba(138,162,255,0.2); }
.cosmos-search-results { display: none; flex-direction: column; gap: 2px; max-height: 220px; overflow-y: auto; }
.cosmos-search-results.show { display: flex; }
.cosmos-sr-item {
    display: flex; align-items: baseline; gap: 8px; width: 100%; text-align: start;
    padding: 8px 10px; border: none; background: transparent; border-radius: 8px;
    color: var(--text-primary); cursor: pointer; font-size: 0.85rem;
}
.cosmos-sr-item:hover { background: rgba(255,255,255,0.07); }
.cosmos-sr-item .yr { color: var(--text-muted); font-size: 0.75rem; font-variant-numeric: tabular-nums; margin-inline-start: auto; }
.cosmos-sr-empty { padding: 8px 10px; color: var(--text-muted); font-size: 0.82rem; font-style: italic; }

.cosmos-fdrop { position: relative; }
.cosmos-fdrop-btn {
    display: flex; align-items: center; justify-content: space-between; gap: 8px; width: 100%;
    height: 40px; padding: 0 14px; border-radius: 10px;
    background: rgba(0,0,0,0.25); border: 1px solid var(--border-color);
    color: var(--text-primary); font-size: 0.88rem; font-weight: 600; cursor: pointer;
}
.cosmos-fdrop-btn svg { opacity: 0.7; transition: transform 0.2s; }
.cosmos-fdrop.open .cosmos-fdrop-btn svg { transform: rotate(180deg); }
.cosmos-fdrop.has-value .cosmos-fdrop-btn { border-color: #8aa2ff; color: #cfe0ff; }
.cosmos-fdrop-menu {
    position: absolute; top: calc(100% + 6px); left: 0; right: 0; z-index: 60;
    max-height: 260px; overflow-y: auto; padding: 6px;
    background: rgba(16, 20, 36, 0.98); border: 1px solid var(--border-color);
    border-radius: 12px; box-shadow: 0 14px 40px rgba(0,0,0,0.5);
    opacity: 0; visibility: hidden; transform: translateY(-8px);
    transition: opacity 0.18s, transform 0.18s, visibility 0.18s;
}
.cosmos-fdrop.open .cosmos-fdrop-menu { opacity: 1; visibility: visible; transform: none; }
.cosmos-fdrop-item {
    display: block; width: 100%; text-align: start; padding: 9px 12px;
    border: none; background: transparent; border-radius: 8px;
    color: var(--text-primary); font-size: 0.86rem; cursor: pointer;
}
.cosmos-fdrop-item:hover { background: rgba(255,255,255,0.07); }
.cosmos-fdrop-item.active { background: rgba(138,162,255,0.18); color: #cfe0ff; font-weight: 600; }
.cosmos-count { text-align: center; font-size: 0.82rem; color: var(--text-secondary); font-variant-numeric: tabular-nums; }

/* Subtle scrollbars for cosmos scroll areas (replaces the default white bar) */
.cosmos-fdrop-menu, .cosmos-eras, .cosmos-search-results {
    scrollbar-width: thin;
    scrollbar-color: rgba(255, 255, 255, 0.22) transparent;
}
.cosmos-fdrop-menu::-webkit-scrollbar,
.cosmos-eras::-webkit-scrollbar,
.cosmos-search-results::-webkit-scrollbar { width: 8px; }
.cosmos-fdrop-menu::-webkit-scrollbar-track,
.cosmos-eras::-webkit-scrollbar-track,
.cosmos-search-results::-webkit-scrollbar-track { background: transparent; }
.cosmos-fdrop-menu::-webkit-scrollbar-thumb,
.cosmos-eras::-webkit-scrollbar-thumb,
.cosmos-search-results::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.2); border-radius: 999px; }
.cosmos-fdrop-menu::-webkit-scrollbar-thumb:hover,
.cosmos-eras::-webkit-scrollbar-thumb:hover,
.cosmos-search-results::-webkit-scrollbar-thumb:hover { background: rgba(255, 255, 255, 0.35); }

/* ---- Hover card ---- */
.cosmos-hover {
    --acc: #8aa2ff;
    position: fixed; z-index: 45; pointer-events: none;
    transform: translate(-50%, 22px) scale(0.94);
    transform-origin: top center;
    background: linear-gradient(160deg, rgba(16,20,36,0.96), rgba(10,13,24,0.96));
    border: 1px solid var(--border-color);
    border-top: 2px solid var(--acc);
    border-radius: 14px; padding: 12px 14px; white-space: nowrap;
    box-shadow: 0 14px 40px rgba(0,0,0,0.55), 0 0 0 1px rgba(255,255,255,0.03) inset, 0 0 24px color-mix(in srgb, var(--acc) 30%, transparent);
    opacity: 0; transition: opacity 0.18s ease, transform 0.18s cubic-bezier(.2,.9,.3,1.2);
    display: flex; align-items: center; gap: 12px;
}
.cosmos-hover.show { opacity: 1; transform: translate(-50%, 22px) scale(1); }
/* The pinned (selected) card is tappable — reliable way to open on touch */
.cosmos-hover.pinned { pointer-events: auto; cursor: pointer; }
.cosmos-hover::after {
    content: ''; position: absolute; top: -7px; left: 50%; transform: translateX(-50%) rotate(45deg);
    width: 12px; height: 12px; background: rgba(16,20,36,0.96);
    border-left: 1px solid var(--border-color); border-top: 1px solid var(--border-color);
}
.hv-portrait {
    width: 52px; height: 52px; border-radius: 50%; object-fit: cover; flex-shrink: 0;
    border: 2px solid var(--acc); box-shadow: 0 0 16px color-mix(in srgb, var(--acc) 45%, transparent);
    background: #0c0f1a;
}
.hv-portrait.hv-ph { display: flex; align-items: center; justify-content: center; color: var(--acc); font-weight: 700; font-size: 1.4rem; font-family: Georgia, serif; }
.hv-info { display: flex; flex-direction: column; gap: 3px; align-items: flex-start; }
.cosmos-hover .hn { font-weight: 700; font-size: 0.98rem; color: #fff; letter-spacing: 0.01em; }
.cosmos-hover .hl { font-size: 0.8rem; color: #c3cbe6; font-variant-numeric: tabular-nums; }
.cosmos-hover .hnat { font-size: 0.74rem; color: var(--text-muted); }
.cosmos-hover .hbooks {
    margin-top: 3px; font-size: 0.72rem; font-weight: 600; color: #fff;
    background: color-mix(in srgb, var(--acc) 30%, transparent);
    border: 1px solid color-mix(in srgb, var(--acc) 45%, transparent);
    padding: 2px 9px; border-radius: 999px;
}
/* "Click/tap again to open" hint — only on the pinned (selected) card */
.cosmos-hover .hopen { display: none; margin-top: 5px; font-size: 0.72rem; font-weight: 600; color: var(--acc); }
.cosmos-hover.pinned .hopen { display: inline-block; animation: cosmosOpenPulse 1.8s ease-in-out infinite; }
@keyframes cosmosOpenPulse { 0%,100% { opacity: 0.6; } 50% { opacity: 1; } }
[dir="rtl"] .hv-info { align-items: flex-end; }

/* ---- Hint ---- */
.cosmos-hint {
    position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); z-index: 45;
    color: var(--text-secondary); font-size: 0.8rem; text-align: center;
    background: rgba(10, 13, 24, 0.5); padding: 8px 16px; border-radius: 999px;
    border: 1px solid var(--border-color); backdrop-filter: blur(8px);
    pointer-events: none; max-width: 92vw;
}
.cosmos-hint .t { display: none; }
@media (pointer: coarse) { .cosmos-hint .d { display: none; } .cosmos-hint .t { display: inline; } }

/* Overlay sits above everything */
.author-overlay { z-index: 200; }

/* ---- Current era chip ---- */
.cosmos-now {
    position: fixed; bottom: 56px; left: 50%; transform: translateX(-50%); z-index: 44;
    padding: 6px 16px; border-radius: 999px;
    font-size: 0.8rem; font-weight: 700; letter-spacing: 0.02em; color: #fff;
    background: rgba(12, 16, 30, 0.55); border: 1px solid var(--border-color);
    backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
    opacity: 0; transition: opacity 0.3s ease; pointer-events: none; white-space: nowrap;
}
.cosmos-now.show { opacity: 1; }

/* ---- Era story card ---- */
.cosmos-era-card {
    --acc: #8aa2ff;
    position: fixed; top: 50%; inset-inline-end: 20px; transform: translateY(-50%) translateX(18px);
    width: 360px; max-width: calc(100vw - 32px); max-height: 82vh; overflow-y: auto;
    z-index: 120; padding: 22px;
    border-radius: 18px;
    background: linear-gradient(165deg, rgba(16,20,36,0.97), rgba(9,12,22,0.97));
    border: 1px solid var(--border-color); border-top: 3px solid var(--acc);
    box-shadow: 0 24px 70px rgba(0,0,0,0.6), 0 0 40px color-mix(in srgb, var(--acc) 22%, transparent);
    opacity: 0; visibility: hidden;
    transition: opacity 0.3s ease, transform 0.3s cubic-bezier(.2,.9,.3,1.1), visibility 0.3s;
    scrollbar-width: thin; scrollbar-color: rgba(255,255,255,0.22) transparent;
}
.cosmos-era-card.show { opacity: 1; visibility: visible; transform: translateY(-50%) translateX(0); }
.cosmos-era-card::-webkit-scrollbar { width: 8px; }
.cosmos-era-card::-webkit-scrollbar-track { background: transparent; }
.cosmos-era-card::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.2); border-radius: 999px; }
.cosmos-era-card-close {
    position: absolute; top: 12px; inset-inline-end: 12px; width: 34px; height: 34px; border-radius: 50%;
    background: rgba(255,255,255,0.06); border: 1px solid var(--border-color); color: var(--text-secondary);
    cursor: pointer; display: flex; align-items: center; justify-content: center;
}
.cosmos-era-card-close:hover { background: rgba(255,255,255,0.12); color: #fff; }
.cosmos-ec-kicker { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.12em; color: var(--acc); font-weight: 700; }
.cosmos-ec-title { font-family: Georgia, "Times New Roman", serif; font-size: 1.6rem; margin: 4px 0 2px; color: #fff; }
.cosmos-ec-range { font-size: 0.82rem; color: var(--text-secondary); margin: 0 0 14px; font-variant-numeric: tabular-nums; }
.cosmos-ec-desc { font-size: 0.92rem; line-height: 1.6; color: #d7ddf0; margin: 0 0 18px; }
.cosmos-ec-subtitle { font-size: 0.74rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text-muted); margin: 0 0 10px; }
.cosmos-ec-authors { display: flex; flex-direction: column; gap: 3px; }
.cosmos-ec-author {
    display: flex; align-items: center; gap: 10px; width: 100%; text-align: start;
    padding: 7px 8px; border: none; background: transparent; border-radius: 10px;
    color: var(--text-primary); cursor: pointer; transition: background 0.15s;
}
.cosmos-ec-author:hover { background: rgba(255,255,255,0.06); }
.cosmos-ec-author img, .cosmos-ec-author .ph {
    width: 36px; height: 36px; border-radius: 50%; object-fit: cover; flex-shrink: 0;
    background: #0c0f1a; border: 1px solid var(--border-color);
    display: flex; align-items: center; justify-content: center; color: var(--acc); font-weight: 700; font-family: Georgia, serif;
}
.cosmos-ec-author .ec-nm { flex: 1; font-weight: 600; font-size: 0.9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cosmos-ec-author .ec-yr { font-size: 0.75rem; color: var(--text-muted); font-variant-numeric: tabular-nums; }
@media (max-width: 560px) {
    .cosmos-era-card {
        top: auto; bottom: 0; inset-inline-end: 0; inset-inline-start: 0;
        width: 100%; max-width: 100%; max-height: 72vh;
        border-radius: 18px 18px 0 0; transform: translateY(24px);
    }
    .cosmos-era-card.show { transform: translateY(0); }
}

/* ---- Small screens ---- */
@media (max-width: 480px) {
    .cosmos-topright { gap: 6px; top: 12px; right: 12px; }
    .cosmos-topleft { gap: 6px; top: 12px; left: 12px; }
    .cosmos-hud-btn, .cosmos-tl-btn { height: 38px; font-size: 0.8rem; }
    .cosmos-hud-btn { padding: 0 11px; }
    #cosmos-audio { width: 38px; }
    .cosmos-eras, .cosmos-filters { top: 60px; width: min(280px, calc(100vw - 24px)); }
    .cosmos-tl-btn { padding: 0 12px; }
    .cosmos-hint { bottom: 12px; font-size: 0.72rem; padding: 6px 12px; }
}

@media (prefers-reduced-motion: reduce) {
    .cosmos-loader-stars { animation: none; }
    .cosmos-hover.pinned .hopen { animation: none; }
}
