/* ===========================
   CSS Variables & Reset
   =========================== */
:root {
    --bg-primary: #0a0e1a;
    --bg-secondary: #111827;
    --bg-card: #1a2035;
    --bg-glass: rgba(15, 20, 40, 0.88);
    --text-primary: #f0f2f5;
    --text-secondary: #9ca3af;
    --text-muted: #6b7280;
    --accent-1: #6366f1;
    --accent-2: #8b5cf6;
    --accent-3: #a78bfa;
    --accent-gradient: linear-gradient(135deg, #6366f1, #8b5cf6, #a78bfa);
    --accent-glow: rgba(99, 102, 241, 0.3);
    --autism-blue: #3b82f6;
    --autism-gold: #f59e0b;
    --autism-red: #ef4444;
    --autism-green: #10b981;
    --header-h: 54px;
    --bottom-h: 56px;
    --arrow-w: 56px;
    --shadow-sm: 0 2px 8px rgba(0,0,0,0.35);
    --shadow-md: 0 4px 20px rgba(0,0,0,0.45);
    --shadow-lg: 0 8px 40px rgba(0,0,0,0.55);
    --shadow-glow: 0 0 28px rgba(99,102,241,0.22);
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 18px;
    --transition: all 0.28s cubic-bezier(0.4, 0, 0.2, 1);
    --font-primary: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    --font-display: 'Outfit', 'Inter', sans-serif;
}

/* Mobile overrides */
@media (max-width: 767px) {
    :root {
        --header-h: 48px;
        --bottom-h: 52px;
        --arrow-w: 36px;
    }
}

*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
    width: 100%; height: 100%;
    overflow: hidden;
    font-family: var(--font-primary);
    background: var(--bg-primary);
    color: var(--text-primary);
    -webkit-font-smoothing: antialiased;
    touch-action: manipulation;
}

.hidden { display: none !important; }

/* ===========================
   Loading Screen
   =========================== */
#loading-screen {
    position: fixed; inset: 0; z-index: 9999;
    display: flex; align-items: center; justify-content: center;
    background: var(--bg-primary);
    background-image:
        radial-gradient(ellipse at 20% 50%, rgba(99,102,241,0.09) 0%, transparent 55%),
        radial-gradient(ellipse at 80% 20%, rgba(139,92,246,0.07) 0%, transparent 55%);
}

.loader-container { text-align: center; padding: 32px 20px; }

/* Puzzle animation */
.puzzle-pieces {
    width: 72px; height: 72px;
    margin: 0 auto 24px;
    display: grid; grid-template-columns: 1fr 1fr; gap: 5px;
    animation: puzzleSpin 3s ease-in-out infinite;
}
.puzzle-piece { border-radius: 5px; animation: piecePulse 2s ease-in-out infinite; }
.puzzle-piece.p1 { background: var(--autism-blue);  animation-delay: 0s; }
.puzzle-piece.p2 { background: var(--autism-gold);  animation-delay: .25s; }
.puzzle-piece.p3 { background: var(--autism-green); animation-delay: .5s; }
.puzzle-piece.p4 { background: var(--autism-red);   animation-delay: .75s; }

@keyframes puzzleSpin {
    0%,100% { transform: rotate(0deg) scale(1); }
    25%      { transform: rotate(6deg) scale(1.06); }
    75%      { transform: rotate(-6deg) scale(1.06); }
}
@keyframes piecePulse {
    0%,100% { opacity: .6; }
    50%      { opacity: 1; }
}

.loader-title {
    font-family: var(--font-display);
    font-size: clamp(1.1rem, 3vw, 1.6rem);
    font-weight: 700;
    background: var(--accent-gradient);
    -webkit-background-clip: text; -webkit-text-fill-color: transparent;
    background-clip: text;
    margin-bottom: 6px;
}
.loader-subtitle { color: var(--text-secondary); font-size: .9rem; margin-bottom: 28px; }

.progress-bar-container {
    width: min(280px, 80vw); height: 6px;
    background: var(--bg-card);
    border-radius: 100px; overflow: hidden;
    margin: 0 auto 12px;
}
.progress-bar {
    height: 100%; width: 0%;
    background: var(--accent-gradient);
    border-radius: 100px;
    transition: width .3s ease;
    box-shadow: 0 0 12px var(--accent-glow);
}
.progress-text { color: var(--text-muted); font-size: .8rem; }

/* ===========================
   App Shell
   =========================== */
#app {
    position: fixed; inset: 0;
    display: flex; flex-direction: column;
    overflow: hidden;
}

/* ===========================
   Header
   =========================== */
#header {
    position: relative; z-index: 100;
    height: var(--header-h);
    flex-shrink: 0;
    display: flex; align-items: center;
    padding: 0 12px;
    gap: 8px;
    background: var(--bg-glass);
    backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
    border-bottom: 1px solid rgba(255,255,255,0.07);
}

.brand {
    display: flex; align-items: center; gap: 9px;
    flex-shrink: 0;
}
.brand-icon {
    font-size: 1.3rem;
    background: var(--accent-gradient);
    -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;
}
.brand-text { display: flex; flex-direction: column; }
.brand-title {
    font-family: var(--font-display); font-size: .88rem; font-weight: 600;
    line-height: 1.15; white-space: nowrap;
}
.brand-sub { font-size: .68rem; color: var(--text-muted); white-space: nowrap; }

.header-center {
    flex: 1; display: flex; justify-content: center; min-width: 0;
}
.page-indicator {
    font-size: .82rem; color: var(--text-secondary); font-weight: 500;
    background: rgba(255,255,255,0.05);
    padding: 4px 14px; border-radius: 100px;
    border: 1px solid rgba(255,255,255,0.08);
    white-space: nowrap;
}

.header-right {
    display: flex; align-items: center; gap: 6px; flex-shrink: 0;
}
.header-btn {
    width: 36px; height: 36px;
    border: none;
    background: rgba(255,255,255,0.06);
    color: var(--text-secondary);
    border-radius: 9px; cursor: pointer;
    font-size: .88rem;
    transition: var(--transition);
    display: flex; align-items: center; justify-content: center;
}
.header-btn:hover {
    background: rgba(99,102,241,0.15);
    color: var(--accent-3);
    transform: translateY(-1px);
}
.header-btn:active { transform: scale(.92); }

/* ===========================
   Flipbook Area  (MIDDLE)
   =========================== */
#flipbook-area {
    flex: 1; min-height: 0;           /* fills remaining vertical space */
    position: relative;
    display: flex; align-items: center; justify-content: center;
    overflow: hidden;
    background: var(--bg-primary);
    background-image:
        radial-gradient(ellipse at 30% 50%, rgba(99,102,241,0.05) 0%, transparent 60%),
        radial-gradient(ellipse at 70% 30%, rgba(139,92,246,0.04) 0%, transparent 60%);
}

/* Nav arrows — fixed width, sit at edges */
.nav-arrow {
    position: absolute; top: 50%; transform: translateY(-50%);
    z-index: 50;
    width: 52px; height: 52px;
    border: none; border-radius: 50%;
    background: var(--bg-glass);
    backdrop-filter: blur(10px);
    color: var(--text-secondary);
    font-size: 1rem; cursor: pointer;
    transition: var(--transition);
    display: flex; align-items: center; justify-content: center;
    border: 1px solid rgba(255,255,255,0.08);
    flex-shrink: 0;
}
.nav-prev { left: 6px; }
.nav-next { right: 6px; }

.nav-arrow:hover {
    background: rgba(99,102,241,0.22);
    color: var(--accent-3);
    transform: translateY(-50%) scale(1.1);
    box-shadow: var(--shadow-glow);
    border-color: rgba(99,102,241,.35);
}
.nav-arrow:active { transform: translateY(-50%) scale(.92); }

/* Flipbook wrapper — fills the space BETWEEN the two arrows */
#flipbook-wrapper {
    position: absolute;
    inset: 0;
    /* leave 64px each side for the arrow buttons */
    left: 64px;
    right: 64px;
    display: flex; align-items: center; justify-content: center;
    transform-origin: center center;
    transition: transform .3s ease;
    pointer-events: auto;
}

#flipbook {
    display: flex; align-items: center; justify-content: center;
}

/* Page images */
.page-wrapper {
    background: #fff; overflow: hidden;
    user-select: none; -webkit-user-select: none;
}
.page-wrapper img {
    width: 100%; height: 100%;
    object-fit: fill; display: block;
    pointer-events: none;
}

/* ===========================
   Bottom Controls
   =========================== */
#bottom-controls {
    flex-shrink: 0;
    height: var(--bottom-h);
    display: flex; align-items: center; justify-content: center;
    gap: 6px; padding: 0 12px;
    background: var(--bg-glass);
    backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
    border-top: 1px solid rgba(255,255,255,0.07);
    z-index: 100;
}

.control-btn {
    width: 38px; height: 38px;
    border: none;
    background: rgba(255,255,255,0.06);
    color: var(--text-secondary);
    border-radius: 9px; cursor: pointer;
    font-size: .9rem;
    transition: var(--transition);
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
    -webkit-tap-highlight-color: transparent;
}
.control-btn:hover  { background: rgba(99,102,241,0.15); color: var(--accent-3); }
.control-btn:active { transform: scale(.9); }

/* Slider */
.page-scrubber { flex: 1; max-width: 480px; padding: 0 6px; }

#page-slider {
    -webkit-appearance: none; appearance: none;
    width: 100%; height: 6px;
    border-radius: 100px;
    background: var(--bg-card);
    outline: none; cursor: pointer;
}
#page-slider::-webkit-slider-thumb {
    -webkit-appearance: none; appearance: none;
    width: 22px; height: 22px; border-radius: 50%;
    background: var(--accent-gradient);
    cursor: pointer;
    box-shadow: 0 0 10px var(--accent-glow);
    transition: var(--transition);
}
#page-slider::-webkit-slider-thumb:hover { transform: scale(1.18); }
#page-slider::-moz-range-thumb {
    width: 22px; height: 22px; border-radius: 50%;
    background: var(--accent-gradient);
    border: none; cursor: pointer;
}

/* ===========================
   Share Modal
   =========================== */
#share-modal {
    position: fixed; inset: 0; z-index: 500;
    display: flex; align-items: center; justify-content: center;
    background: rgba(0,0,0,0.72);
    backdrop-filter: blur(8px);
    animation: fadeIn .22s ease;
    padding: 16px;
}
.modal-content {
    background: var(--bg-card);
    border-radius: var(--radius-lg);
    padding: 28px;
    width: 100%; max-width: 420px;
    position: relative;
    border: 1px solid rgba(255,255,255,0.08);
    box-shadow: var(--shadow-lg);
    animation: slideUp .3s ease;
}
.modal-content h3 {
    font-family: var(--font-display); font-size: 1.25rem;
    margin-bottom: 6px;
}
.modal-content h3 i {
    background: var(--accent-gradient);
    -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;
    margin-right: 8px;
}
.modal-content p { color: var(--text-secondary); font-size: .88rem; margin-bottom: 18px; }

.close-btn {
    width: 30px; height: 30px;
    border: none; background: rgba(255,255,255,0.06);
    color: var(--text-secondary);
    border-radius: 7px; cursor: pointer; font-size: .82rem;
    display: flex; align-items: center; justify-content: center;
    transition: var(--transition);
}
.close-btn:hover { background: rgba(239,68,68,.15); color: #ef4444; }
.modal-close { position: absolute; top: 14px; right: 14px; }

.share-link-box { display: flex; gap: 8px; margin-bottom: 18px; }
.share-link-box input {
    flex: 1; padding: 9px 12px;
    background: var(--bg-primary);
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: var(--radius-sm);
    color: var(--text-primary); font-size: .82rem;
    font-family: var(--font-primary); outline: none;
    min-width: 0;
}
.share-link-box input:focus { border-color: var(--accent-1); }
.copy-btn {
    padding: 9px 14px;
    background: var(--accent-gradient);
    border: none; border-radius: var(--radius-sm);
    color: white; font-size: .82rem; font-weight: 500;
    cursor: pointer; white-space: nowrap;
    font-family: var(--font-primary);
    transition: var(--transition); flex-shrink: 0;
}
.copy-btn:hover { transform: translateY(-1px); box-shadow: 0 4px 14px var(--accent-glow); }

.share-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.share-btn {
    display: flex; align-items: center; justify-content: center;
    gap: 8px; padding: 11px 12px;
    border-radius: var(--radius-sm);
    color: white; text-decoration: none;
    font-size: .85rem; font-weight: 500;
    transition: var(--transition);
}
.share-btn:hover { transform: translateY(-2px); box-shadow: var(--shadow-md); }
.share-btn.whatsapp { background: #25D366; }
.share-btn.telegram  { background: #0088cc; }
.share-btn.facebook  { background: #1877F2; }
.share-btn.twitter   { background: #1DA1F2; }

/* ===========================
   Thumbnail Panel
   =========================== */
#thumbnail-overlay {
    position: fixed; inset: 0; z-index: 400;
    background: rgba(0,0,0,0.6);
    backdrop-filter: blur(5px);
    animation: fadeIn .2s ease;
}
#thumbnail-panel {
    position: absolute; right: 0; top: 0; bottom: 0;
    width: min(300px, 85vw);
    background: var(--bg-secondary);
    border-left: 1px solid rgba(255,255,255,0.06);
    box-shadow: var(--shadow-lg);
    animation: slideInRight .28s ease;
    display: flex; flex-direction: column;
    overflow: hidden;
}
.thumb-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid rgba(255,255,255,0.06);
    flex-shrink: 0;
}
.thumb-header h3 { font-family: var(--font-display); font-size: 1rem; font-weight: 600; }
#thumbnail-grid {
    padding: 12px; display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px; overflow-y: auto; flex: 1;
}
.thumb-item {
    cursor: pointer; border-radius: 6px; overflow: hidden;
    border: 2px solid transparent;
    transition: var(--transition);
    position: relative; aspect-ratio: 0.707;
    background: var(--bg-card);
}
.thumb-item:hover { border-color: var(--accent-1); transform: scale(1.04); }
.thumb-item.active { border-color: var(--accent-2); box-shadow: 0 0 14px rgba(139,92,246,.3); }
.thumb-item img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb-label {
    position: absolute; bottom: 0; left: 0; right: 0;
    padding: 3px; background: rgba(0,0,0,.72);
    color: white; font-size: .68rem; text-align: center; font-weight: 500;
}

/* ===========================
   Swipe hint (mobile)
   =========================== */
#swipe-hint {
    position: absolute; bottom: 12px; left: 50%;
    transform: translateX(-50%);
    z-index: 200; pointer-events: none;
    animation: fadeIn .5s ease, fadeOutDelay .4s ease 3.5s forwards;
}
.swipe-animation {
    display: flex; align-items: center; gap: 10px;
    background: var(--bg-glass);
    backdrop-filter: blur(10px);
    padding: 10px 18px; border-radius: 100px;
    border: 1px solid rgba(255,255,255,0.1);
    box-shadow: var(--shadow-md);
}
.swipe-animation i { font-size: 1.1rem; color: var(--accent-3); animation: swipeHand 1.4s ease-in-out infinite; }
.swipe-animation span { font-size: .82rem; color: var(--text-secondary); white-space: nowrap; }

@keyframes swipeHand {
    0%,100% { transform: translateX(0); }
    50%      { transform: translateX(-14px); }
}
@keyframes fadeOutDelay { to { opacity: 0; } }

/* ===========================
   Animations
   =========================== */
@keyframes fadeIn  { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { opacity: 0; transform: translateY(18px); } to { opacity: 1; transform: translateY(0); } }
@keyframes slideInRight { from { transform: translateX(100%); } to { transform: translateX(0); } }

/* ===========================
   Scrollbar
   =========================== */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.12); border-radius: 100px; }
::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.2); }

/* ===========================
   StPageFlip tweaks
   =========================== */
.stf__parent { overflow: visible !important; }

/* ===========================
   RESPONSIVE — Tablet (768–1023px)
   =========================== */
@media (min-width: 768px) and (max-width: 1023px) {
    .brand-sub { display: none; }
    .header-btn#btn-zoom-in,
    .header-btn#btn-zoom-out { display: none; }   /* use pinch zoom on tablet */
    .page-indicator { font-size: .78rem; padding: 3px 11px; }
    .control-btn { width: 36px; height: 36px; }
}

/* ===========================
   RESPONSIVE — Mobile (<768px)
   =========================== */
@media (max-width: 767px) {
    /* Header: compact */
    .brand-sub { display: none; }
    .brand-title { font-size: .82rem; }
    .header-btn#btn-zoom-in,
    .header-btn#btn-zoom-out { display: none; }
    .page-indicator { font-size: .74rem; padding: 3px 10px; }
    .header-btn { width: 32px; height: 32px; font-size: .82rem; border-radius: 8px; }

    /* Arrows: smaller but same 64px margin preserved in wrapper */
    .nav-arrow { width: 40px; height: 40px; font-size: .85rem; }
    .nav-prev { left: 4px; }
    .nav-next { right: 4px; }

    /* Bottom bar: compact */
    .control-btn { width: 34px; height: 34px; font-size: .85rem; }
    #bottom-controls { gap: 4px; padding: 0 8px; }
    .page-scrubber { padding: 0 4px; }

    /* Share modal: full-width */
    .share-buttons { grid-template-columns: 1fr; }
    .modal-content { padding: 20px 18px; }
}

/* ===========================
   RESPONSIVE — Very small phones (<400px)
   =========================== */
@media (max-width: 399px) {
    .brand { display: none; }           /* reclaim header space */
    .header-btn { width: 30px; height: 30px; }
    .nav-arrow { width: 34px; height: 34px; font-size: .78rem; }
    /* Tighten wrapper slightly for very small screens */
    #flipbook-wrapper { left: 38px; right: 38px; }
    .control-btn { width: 30px; height: 30px; font-size: .78rem; }
    #btn-first, #btn-last { display: none; }   /* remove rarely-used buttons */
}

/* ===========================
   Landscape phone fix
   =========================== */
@media (max-width: 767px) and (orientation: landscape) {
    :root { --header-h: 40px; --bottom-h: 44px; }
    .brand-icon { display: none; }
    .brand-title { font-size: .78rem; }
    .control-btn { width: 30px; height: 30px; }
    .nav-arrow { width: 36px; height: 36px; }
}
