/* ===== Global touch/iPad base ===== */
*, *::before, *::after {
    -webkit-tap-highlight-color: transparent;
    box-sizing: border-box;
}

html {
    -webkit-text-size-adjust: 100%;
    scroll-behavior: smooth;
}

/* ===== Global font — children's reading font =====
   Andika (SIL, self-hosted) is the everywhere-fallback; a locally
   installed Sassoon Primary is preferred when the device has it. */
@font-face {
    font-family: 'Andika';
    src: url('../fonts/andika-regular.ttf') format('truetype');
    font-weight: 400;
    font-style: normal;
    font-display: swap;
}
@font-face {
    font-family: 'Andika';
    src: url('../fonts/andika-bold.ttf') format('truetype');
    font-weight: 700;
    font-style: normal;
    font-display: swap;
}

body, button, input, select, textarea {
    font-family: 'Sassoon Primary', 'Andika', 'Comic Sans MS', 'Segoe UI', system-ui, sans-serif;
}

button, a, label, [role="button"] {
    touch-action: manipulation; /* removes 300ms tap delay */
    cursor: pointer;
}

/* ===== Reader body — use dvh so Safari's address bar collapse is handled ===== */
.reader-body {
    height: 100dvh;
}

/* ===== Any story word — tappable for TTS ===== */
.word {
    cursor: pointer;
    border-radius: 3px;
    padding: 2px 3px;
    transition: background-color 0.15s, color 0.15s;
    position: relative;
}

.word::after {
    content: '';
    position: absolute;
    inset: -4px -2px;
}

.word:hover {
    background-color: #f1f5f9;
}

.word:active {
    background-color: #e2e8f0;
}

/* ===== Phonics word — amber underline on top of .word ===== */
.phonics-word {
    font-weight: 700;
    text-decoration: underline;
    text-decoration-color: #f59e0b;
    text-decoration-thickness: 3px;
    text-underline-offset: 5px;
}

.phonics-word::after {
    inset: -6px -4px; /* slightly larger tap target */
}

.phonics-word:hover {
    background-color: #fef3c7;
    color: #92400e;
}

.phonics-word:active {
    background-color: #fde68a;
}

/* ===== Reader text — large, Georgia serif for readability ===== */
.page-text {
    font-size: clamp(1.5rem, 5vw, 2.6rem);
    line-height: 1.7;
    font-family: 'Sassoon Primary', 'Andika', Georgia, 'Times New Roman', serif;
    color: #1e293b;
    letter-spacing: 0.01em;
}

/* ===== Navigation buttons — 52px min touch target ===== */
.nav-btn {
    min-height: 52px;
    padding: 0.7rem 1.6rem;
    border-radius: 14px;
    font-weight: 700;
    font-size: 1rem;
    transition: background-color 0.15s, opacity 0.15s, transform 0.1s;
    border: 2px solid #e2e8f0;
    background: white;
    color: #475569;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

.nav-btn:hover:not(:disabled) {
    background-color: #f1f5f9;
}

.nav-btn:active:not(:disabled) {
    transform: scale(0.97);
}

.nav-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

.nav-btn-primary {
    background-color: #9333ea;
    color: white;
    border-color: #9333ea;
}

.nav-btn-primary:hover:not(:disabled) {
    background-color: #7e22ce;
    border-color: #7e22ce;
}

/* ===== Page dots — larger tap area ===== */
.page-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    transition: background-color 0.2s, transform 0.2s;
    position: relative;
}

/* Invisible enlarged tap target */
.page-dot::after {
    content: '';
    position: absolute;
    inset: -8px;
}

.dot-active {
    background-color: #9333ea;
    transform: scale(1.3);
}

.dot-inactive {
    background-color: #cbd5e1;
}

.dot-inactive:hover {
    background-color: #94a3b8;
}

/* ===== Reader panels (portrait default) ===== */
#image-panel {
    flex: 1;
    min-height: 0;
}

#text-panel {
    min-height: 32vh;
    max-height: 44vh;
}

/* ===== iPad landscape: side-by-side reader layout ===== */
@media (orientation: landscape) and (min-width: 768px) {
    #reader-main {
        flex-direction: row;
    }

    #image-panel {
        flex: 3;
        height: 100%;
        max-height: 100%;
    }

    #text-panel {
        flex: 2;
        min-height: 0;
        max-height: 100%;
        height: 100%;
        border-left: 1px solid #e2e8f0;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
    }

    /* More breathing room in landscape text panel */
    #text-panel .flex-1 {
        padding: 2rem 2.5rem 1rem;
    }

    #text-panel .shrink-0 {
        padding: 1rem 2rem 1.5rem;
    }

    /* Slightly smaller text in landscape (more width available) */
    .page-text {
        font-size: clamp(1.4rem, 2.8vw, 2.4rem);
    }
}

/* ===== iPad portrait: bigger text area ===== */
@media (orientation: portrait) and (min-width: 768px) {
    #text-panel {
        min-height: 28vh;
        max-height: 36vh;
    }

    .page-text {
        font-size: clamp(1.8rem, 4vw, 2.8rem);
    }

    .nav-btn {
        min-height: 60px;
        padding: 0.85rem 2rem;
        font-size: 1.1rem;
    }
}

/* ===== Safe area padding (home bar on newer iPads) ===== */
.pb-safe {
    padding-bottom: max(1.25rem, env(safe-area-inset-bottom));
}

/* ===== Magical drifting library background ===== */
.magic-bg {
    position: fixed;
    inset: 0;
    z-index: 0;
    pointer-events: none;
    overflow: hidden;
}

.magic-blob {
    position: absolute;
    border-radius: 50%;
    filter: blur(60px);
    opacity: 0.5;
    animation: blob-drift 18s ease-in-out infinite alternate;
}

.magic-blob-1 { width: 340px; height: 340px; background: #e9d5ff; top: -80px;  left: -60px; }
.magic-blob-2 { width: 300px; height: 300px; background: #fde68a; top: 30%;    right: -80px; animation-duration: 23s; animation-delay: -6s; }
.magic-blob-3 { width: 380px; height: 380px; background: #fbcfe8; bottom: -120px; left: 25%; animation-duration: 27s; animation-delay: -12s; }
.magic-blob-4 { width: 240px; height: 240px; background: #c7d2fe; top: 12%;    left: 42%;  animation-duration: 21s; animation-delay: -3s; }

@keyframes blob-drift {
    0%   { transform: translate(0, 0) scale(1); }
    50%  { transform: translate(45px, -35px) scale(1.12); }
    100% { transform: translate(-35px, 28px) scale(0.94); }
}

.magic-sparkle {
    position: absolute;
    color: #f59e0b;
    animation: sparkle-twinkle 4s ease-in-out infinite;
    text-shadow: 0 0 12px rgba(251, 191, 36, 0.8);
}

@keyframes sparkle-twinkle {
    0%, 100% { opacity: 0.15; transform: scale(0.7) rotate(0deg); }
    50%      { opacity: 0.9;  transform: scale(1.15) rotate(20deg); }
}

/* ===== Loud variant — big vivid blobs on dark purple ===== */
.magic-bg-loud .magic-blob {
    filter: blur(70px);
    animation-name: blob-drift-loud;
}

.magic-bg-loud .magic-blob-l1 { width: 560px; height: 560px; background: #7c3aed; top: -160px;   left: -140px; opacity: 0.55; animation-duration: 9s; }
.magic-bg-loud .magic-blob-l2 { width: 480px; height: 480px; background: #fbbf24; top: 18%;      right: -160px; opacity: 0.32; animation-duration: 12s; animation-delay: -3s; }
.magic-bg-loud .magic-blob-l3 { width: 620px; height: 620px; background: #ec4899; bottom: -220px; left: 8%;    opacity: 0.38; animation-duration: 14s; animation-delay: -6s; }
.magic-bg-loud .magic-blob-l4 { width: 380px; height: 380px; background: #38bdf8; top: 32%;      left: 40%;   opacity: 0.28; animation-duration: 10s; animation-delay: -2s; }

@keyframes blob-drift-loud {
    0%   { transform: translate(0, 0) scale(1); }
    50%  { transform: translate(90px, -70px) scale(1.25); }
    100% { transform: translate(-70px, 60px) scale(0.9); }
}

.magic-bg-loud .magic-sparkle {
    color: #fde68a;
    animation-duration: 2.6s;
    text-shadow: 0 0 18px rgba(251, 191, 36, 1);
}

/* ===== Start Reading party button — wiggle + pulse + shimmer ===== */
.pi-start-party {
    background: linear-gradient(100deg, #fbbf24, #f59e0b 40%, #fb923c 70%, #fbbf24);
    background-size: 220% 100%;
    animation: pi-start-party 1.7s ease-in-out infinite,
               pi-shimmer 3.5s linear infinite;
    cursor: pointer;
}

@keyframes pi-start-party {
    0%, 100% { transform: scale(1) rotate(0deg);       box-shadow: 0 0 0 0 rgba(251, 191, 36, 0.7), 0 10px 30px rgba(251, 191, 36, 0.3); }
    15%      { transform: scale(1.05) rotate(-1.6deg); }
    30%      { transform: scale(1.02) rotate(1.3deg); }
    45%      { transform: scale(1.07) rotate(-1deg);   box-shadow: 0 0 0 18px rgba(251, 191, 36, 0), 0 14px 44px rgba(251, 191, 36, 0.55); }
    60%      { transform: scale(1.02) rotate(0.8deg); }
    75%      { transform: scale(1.04) rotate(-0.5deg); }
}

@keyframes pi-shimmer {
    0%   { background-position: 0% 50%; }
    100% { background-position: 220% 50%; }
}

/* ===== Book card ===== */
.book-card {
    background: white;
    border-radius: 1rem;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    overflow: hidden;
    transition: box-shadow 0.2s, transform 0.2s;
}

.book-card:hover {
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.14);
    transform: translateY(-2px);
}

/* Larger touch-friendly delete button */
.delete-btn {
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ===== Form elements ===== */
.form-group {
    display: flex;
    flex-direction: column;
}

.form-label {
    font-weight: 600;
    font-size: 0.95rem;
    color: #374151;
    margin-bottom: 0.45rem;
}

.form-input {
    width: 100%;
    padding: 0.75rem 1rem;
    border: 2px solid #e5e7eb;
    border-radius: 0.75rem;
    /* 16px minimum prevents iOS auto-zoom on focus */
    font-size: max(1rem, 16px);
    transition: border-color 0.15s, box-shadow 0.15s;
    background: white;
    color: #1f2937;
    appearance: auto;
    min-height: 52px;
}

.form-input:focus {
    outline: none;
    border-color: #9333ea;
    box-shadow: 0 0 0 3px rgba(147, 51, 234, 0.15);
}

.form-input:disabled {
    background-color: #f9fafb;
    color: #9ca3af;
    cursor: not-allowed;
}

/* ===== Moral radio options — 44px touch targets ===== */
.moral-option {
    display: flex;
    align-items: center;
    cursor: pointer;
}

.moral-option input[type="radio"] {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
}

.moral-label {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    padding: 0.65rem 0.75rem;
    border: 2px solid #e5e7eb;
    border-radius: 0.75rem;
    font-size: 0.9rem;
    font-weight: 500;
    color: #4b5563;
    cursor: pointer;
    transition: border-color 0.15s, background-color 0.15s, color 0.15s;
    text-align: center;
    min-height: 52px;
    background: white;
}

.moral-option input[type="radio"]:checked + .moral-label {
    border-color: #9333ea;
    background-color: #f3e8ff;
    color: #7e22ce;
}

.moral-label:hover {
    border-color: #c084fc;
    background-color: #faf5ff;
}

/* ===== Example setting buttons ===== */
.example-btn {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
}

/* ===== Progress overlay ===== */
#progress-overlay {
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
}

/* ===== Phonics focus chips ===== */
.phonics-chip-on,
.phonics-chip-off {
    padding: 0.25rem 0.6rem;
    border-radius: 0.4rem;
    font-size: 0.85rem;
    font-family: ui-monospace, monospace;
    font-weight: 600;
    cursor: pointer;
    border: none;
    transition: background-color 0.15s, color 0.15s, opacity 0.15s;
    line-height: 1.4;
}

.phonics-chip-on {
    background-color: #e9d5ff;
    color: #6b21a8;
}

.phonics-chip-on:hover {
    background-color: #d8b4fe;
}

.phonics-chip-off {
    background-color: #e5e7eb;
    color: #9ca3af;
    text-decoration: line-through;
    opacity: 0.7;
}

.phonics-chip-off:hover {
    background-color: #d1d5db;
    opacity: 0.9;
}

/* ===== Start Reading flash animation ===== */
@keyframes start-reading-pulse {
    0%, 100% { transform: scale(1);    box-shadow: 0 0 0  0   rgba(251,191,36,0.7), 0 4px 15px rgba(0,0,0,0.25); }
    50%       { transform: scale(1.04); box-shadow: 0 0 0 12px rgba(251,191,36,0),   0 4px 30px rgba(251,191,36,0.35); }
}
.start-reading-ready {
    animation: start-reading-pulse 1.3s ease-in-out infinite !important;
}

/* ===== Magic captured counter pop ===== */
@keyframes magic-count-pop {
    0%   { transform: scale(1); }
    40%  { transform: scale(1.35); }
    100% { transform: scale(1); }
}
.magic-count-pop {
    animation: magic-count-pop 0.3s ease-out;
}

/* ===== Start Reading warp transition ===== */
.warp-burst {
    position: absolute; left: 50%; top: 50%;
    width: 40px; height: 40px; border-radius: 50%;
    transform: translate(-50%, -50%) scale(0);
    background: radial-gradient(circle, #fff7d6 0%, #fbbf24 30%, #a855f7 65%, rgba(76, 29, 158, 0.98) 100%);
    animation: warp-burst 1.4s ease-in forwards;
}
@keyframes warp-burst {
    0%   { transform: translate(-50%, -50%) scale(0);  opacity: 0.9; }
    60%  { opacity: 1; }
    100% { transform: translate(-50%, -50%) scale(60); opacity: 1; }
}

/* Library variant — rose/violet burst instead of gold/purple */
.warp-burst-library {
    background: radial-gradient(circle, #ffe4f1 0%, #f472b6 28%, #8b5cf6 62%, rgba(30, 10, 70, 0.98) 100%);
}

.warp-core {
    position: absolute; left: 50%; top: 50%;
    font-size: 72px;
    filter: drop-shadow(0 0 30px #fbbf24);
    animation: warp-core 1.4s ease-out forwards;
}
@keyframes warp-core {
    0%   { transform: translate(-50%, -50%) scale(0.2) rotate(-20deg); opacity: 0; }
    25%  { opacity: 1; }
    100% { transform: translate(-50%, -50%) scale(2.6) rotate(12deg);  opacity: 0; }
}

.warp-star {
    position: absolute; left: 50%; top: 50%;
    transform: translate(-50%, -50%);
    animation: warp-star 1.25s ease-out forwards;
}
@keyframes warp-star {
    0%   { transform: translate(-50%, -50%) translate(0, 0) scale(0.4) rotate(0deg);                    opacity: 0; }
    15%  { opacity: 1; }
    100% { transform: translate(-50%, -50%) translate(var(--tx), var(--ty)) scale(1.6) rotate(200deg);  opacity: 0; }
}

.warp-text {
    position: absolute; left: 50%; top: 62%;
    transform: translateX(-50%);
    color: #fff;
    font-family: 'Sassoon Primary', 'Andika', Georgia, serif;
    font-weight: 800;
    font-size: 1.15rem;
    text-shadow: 0 0 20px rgba(251, 191, 36, 0.8);
    opacity: 0;
    animation: warp-text 1.4s ease-out 0.35s forwards;
    white-space: nowrap;
}
@keyframes warp-text {
    0%        { opacity: 0; transform: translateX(-50%) translateY(14px); }
    30%, 100% { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ===== Sparks celebration — end of book ===== */
.sparks-celebrate {
    position: fixed; inset: 0; z-index: 55;
    display: flex; align-items: center; justify-content: center;
    pointer-events: none;
    animation: sparks-fade-in 0.35s ease-out;
}
.sparks-celebrate::before {
    content: '';
    position: absolute; inset: 0;
    background: radial-gradient(circle at 50% 50%, rgba(26,10,60,0.8) 0%, rgba(26,10,60,0.4) 45%, transparent 75%);
}
.sparks-celebrate-inner { position: relative; text-align: center; }
.sparks-celebrate-burst {
    font-size: 72px;
    animation: sparks-burst-spin 2.2s ease-in-out;
}
.sparks-celebrate-amount {
    font-size: clamp(3.5rem, 12vw, 6rem);
    font-weight: 900;
    color: #fbbf24;
    line-height: 1;
    text-shadow: 0 0 40px rgba(251,191,36,0.9), 0 4px 0 rgba(0,0,0,0.2);
    animation: sparks-amount-pop 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.sparks-celebrate-label {
    font-size: 1.3rem; font-weight: 800; color: #fff;
    text-shadow: 0 0 20px rgba(168,85,247,1), 0 2px 8px rgba(0,0,0,0.5);
    margin-top: 0.4rem;
}
.sparks-fly {
    position: absolute; left: 50%; top: 50%;
    animation: sparks-fly-out 1.7s ease-out forwards;
}
@keyframes sparks-fly-out {
    0%   { transform: translate(-50%,-50%) scale(0.4) rotate(0deg);   opacity: 0; }
    15%  { opacity: 1; }
    100% { transform: translate(calc(-50% + var(--fx)), calc(-50% + var(--fy))) scale(1.3) rotate(160deg); opacity: 0; }
}
@keyframes sparks-amount-pop { 0% { transform: scale(0); } 70% { transform: scale(1.15); } 100% { transform: scale(1); } }
@keyframes sparks-burst-spin { 0% { transform: scale(0) rotate(-40deg); } 30% { transform: scale(1.4) rotate(10deg); } 60% { transform: scale(1) rotate(0deg); } }
@keyframes sparks-fade-in { from { opacity: 0; } }
.sparks-celebrate-out { opacity: 0; transition: opacity 0.5s ease; }

/* ===== Costume unlock — over-the-top ===== */
.unlock-overlay {
    position: fixed; inset: 0; z-index: 70;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    background: rgba(15, 2, 32, 0.88);
    animation: sparks-fade-in 0.25s ease-out;
    overflow: hidden;
}
.unlock-rays {
    position: absolute; left: 50%; top: 50%;
    width: 160vmax; height: 160vmax;
    transform: translate(-50%, -50%);
    background: repeating-conic-gradient(rgba(251,191,36,0.16) 0deg 12deg, transparent 12deg 24deg);
    border-radius: 50%;
    animation: unlock-rays-spin 7s linear infinite;
}
@keyframes unlock-rays-spin {
    from { transform: translate(-50%,-50%) rotate(0deg); }
    to   { transform: translate(-50%,-50%) rotate(360deg); }
}
.unlock-emoji {
    position: relative;
    font-size: clamp(6rem, 25vw, 10rem);
    line-height: 1.1;
    filter: drop-shadow(0 0 50px rgba(251,191,36,0.85));
    animation: unlock-emoji-pop 0.7s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes unlock-emoji-pop {
    0%   { transform: scale(0) rotate(-30deg); }
    60%  { transform: scale(1.25) rotate(8deg); }
    100% { transform: scale(1) rotate(0deg); }
}
.unlock-title {
    position: relative;
    font-size: clamp(2.2rem, 9vw, 4rem);
    font-weight: 900;
    color: #fbbf24;
    letter-spacing: 0.06em;
    text-shadow: 0 0 40px rgba(251,191,36,0.9);
    animation: unlock-title-pulse 0.9s ease-in-out infinite alternate;
}
@keyframes unlock-title-pulse { from { transform: scale(1); } to { transform: scale(1.07); } }
.unlock-sub {
    position: relative;
    color: #e9d5ff; font-weight: 700; font-size: 1.2rem; margin-top: 0.25rem;
}
.unlock-confetti {
    position: absolute; left: 50%; top: 50%;
    width: 12px; height: 12px; border-radius: 3px;
    animation: unlock-confetti-fly 1.8s ease-out forwards;
}
@keyframes unlock-confetti-fly {
    0%   { transform: translate(-50%,-50%) scale(0.5) rotate(0deg);   opacity: 1; }
    100% { transform: translate(calc(-50% + var(--cx)), calc(-50% + var(--cy))) scale(1.1) rotate(540deg); opacity: 0; }
}
.unlock-out { opacity: 0; transition: opacity 0.5s ease; }

/* Costume slide emoji pop on change */
@keyframes costume-pop { 0% { transform: scale(0.6); } 60% { transform: scale(1.12); } 100% { transform: scale(1); } }
.costume-pop { animation: costume-pop 0.3s ease-out; }

/* ===== Line clamp utility ===== */
.line-clamp-2 {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
