@import url("https://fonts.googleapis.com/css2?family=Amiri:wght@400;700&family=Scheherazade+New:wght@400;700&display=swap");

* {
    box-sizing: border-box;
    margin: 0;
}

body {
    min-height: 100vh;
    display: grid;
    grid-template-rows: auto 1fr auto;
    background: linear-gradient(135deg, #1e5b5c 0%, #2d7a7b 100%);
    font-family: "Scheherazade New", serif;
    color: #f9f6ef;
}

/* Loading and Error States */
.loading, .error {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    text-align: center;
}

.spinner {
    width: 50px;
    height: 50px;
    border: 4px solid #f9f6ef25;
    border-top: 4px solid #f9f6ef;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 1rem;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.error {
    color: #ff6b6b;
}

.error button {
    margin-top: 1rem;
    padding: 0.5rem 1rem;
    background: #f9f6ef;
    color: #1e5b5c;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-family: "Amiri", serif;
}

/* Container */
.container {
    margin: 0;
    padding: 0.5rem;
    font-family: "Scheherazade New", serif;
    font-size: 1.5rem;
    height: fit-content;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
}

/* Desktop book centering */
@media (min-width: 769px) {
    .container {
        width: 100vw;
        height: 90vh;
        padding: 0;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    #bookPages {
        margin: 0 auto;
        display: flex;
        justify-content: center;
        align-items: center;
    }
}

/* Desktop Navigation */
@media (min-width: 769px) {
    nav {
        position: fixed;
        bottom: 20px;
        width: 100vw;
        display: flex;
        justify-content: space-between;
        padding: 0 20px;
        box-sizing: border-box;
        z-index: 1000;
    }

    nav button {
        width: 60px;
        height: 60px;
        min-width: 60px;
        background: linear-gradient(135deg, #c9b18c 0%, #a88e6a 100%);
        color: #1e5b5c;
        font-family: "Amiri", serif;
        font-size: 1.5rem;
        font-weight: 700;
        border: 2px solid #f9f6ef50;
        border-radius: 50%;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        transition: all 0.3s ease;
        box-shadow: 0 2px 4px rgba(0,0,0,0.2);
        display: flex;
        align-items: center;
        justify-content: center;
    }

    nav button:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 8px rgba(0,0,0,0.3);
        background: linear-gradient(135deg, #d4bc9a 0%, #b39b78 100%);
    }

    nav button:active {
        transform: translateY(0);
        box-shadow: 0 2px 4px rgba(0,0,0,0.2);
    }
}

nav button:before {
    content: "";
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: radial-gradient(circle, rgba(249,246,239,0.2) 0%, transparent 70%);
    transform: rotate(45deg);
    transition: all 0.5s;
    opacity: 0;
}

nav button:hover:before {
    opacity: 1;
    top: -20%;
    left: -20%;
}

#notation {
    color: #f9f6ef;
    font-family: "Amiri", serif;
    font-size: 0.8rem;
    text-align: center;
    padding: 0.5rem;
}

/* Book Pages */
#bookPages {
    display: none;
    background-size: cover;
    height: 100%;
}

.book-page {
    background-color: #f8f3e6;
    color: #3a3226;
    border: solid 1px #c9b18c;
    overflow: hidden;
    box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0.5);
}

.book-page.--left {
    border-right: 0;
    box-shadow: inset -7px 0 30px -7px rgba(0, 0, 0, 0.4);
}

.book-page.--right {
    border-left: 0;
    box-shadow: inset 7px 0 30px -7px rgba(0, 0, 0, 0.4);
}

.book-page.front-cover {
    color: #f9f6ef;
    background-color: #1e5b5c;
    border: solid 1px #0d3b3c;
    box-shadow: inset 0px 0 30px 0px rgba(13, 59, 60, 0.7),
        -2px 0 5px 2px rgba(0, 0, 0, 0.4);
}

.book-page.back-cover {
    background-color: #1e5b5c;
    border: solid 1px #0d3b3c;
    box-shadow: inset 0px 0 30px 0px rgba(13, 59, 60, 0.7),
        10px 0 8px 0px rgba(0, 0, 0, 0.4);
}

/* Cover Page Content */
.cover-content {
    position: absolute;
    inset: 0.1rem;
    height: 100%;
    background: linear-gradient(135deg, rgba(30, 91, 92, 0.8), rgba(45, 122, 123, 0.8));
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    padding: 2rem;
}

.cover-content h1 {
    font-size: 3.5rem;
    line-height: 0.9;
    margin-bottom: 2rem;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}

.cover-content p {
    font-size: 1.2rem;
    background: rgba(0, 0, 0, 0.3);
    padding: 0.5rem 1rem;
    border-radius: 4px;
}

.cover-image {
    max-width: 60%;
    max-height: 50%;
    object-fit: contain;
    margin-bottom: 1rem;
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

/* Back Cover Content */
.back-cover-content {
    position: absolute;
    inset: 0.1rem;
    height: 100%;
    background: linear-gradient(135deg, rgba(30, 91, 92, 0.8), rgba(45, 122, 123, 0.8));
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    padding: 2rem;
}

.back-cover-content h2 {
    font-size: 2.5rem;
    line-height: 1.2;
    margin-bottom: 1rem;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}

.back-cover-content p {
    font-size: 1.1rem;
    background: rgba(0, 0, 0, 0.3);
    padding: 0.5rem 1rem;
    border-radius: 4px;
    margin-bottom: 2rem;
}

.close-book-btn {
    background: linear-gradient(135deg, #2d7a7b, #1e5b5c);
    color: white;
    border: none;
    padding: 1rem 2rem;
    font-size: 1.1rem;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

.close-book-btn:hover {
    background: linear-gradient(135deg, #3a9a9b, #2d7a7b);
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.4);
}

/* Desktop Story Page Content */
.page-content {
    padding: 1rem;
    display: flex;
    flex-direction: column;
    height: 100%;
    text-align: center;
}

/* Desktop - Both image and text */
.page-content.has-both {
    display: flex;
    flex-direction: column;
}

.page-content.has-both .story-image {
    flex: 1;
    width: 100%;
    height: 0;
    min-height: 0;
    object-fit: contain;
}

.page-content.has-both .story-text {
    flex: 0 0 auto;
    font-size: 18pt;
    line-height: 1.5;
    padding: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
}

/* Desktop - Image only */
.page-content.image-only {
    display: flex;
    align-items: center;
    justify-content: center;
}

.page-content.image-only .story-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* Desktop - Text only */
.page-content.text-only {
    display: flex;
    align-items: center;
    justify-content: center;
}

.page-content.text-only .story-text {
    font-size: 18pt;
    line-height: 1.5;
    padding: 1rem;
    text-align: center;
}

.empty-page {
    background-color: #f8f3e6;
}

/* Library Page Styles */
.book-item {
    margin: 1rem 0;
    padding: 1rem;
    background: rgba(249, 246, 239, 0.1);
    border: 2px solid #c9b18c;
    border-radius: 8px;
    transition: all 0.3s ease;
}

.book-item:hover {
    background: rgba(249, 246, 239, 0.2);
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.3);
}

.book-item a {
    display: block;
    color: #f9f6ef;
    text-decoration: none;
    font-family: "Amiri", serif;
    font-size: 1.5rem;
    font-weight: 700;
    text-align: center;
    padding: 0.5rem;
}

.book-item a:hover {
    color: #c9b18c;
}

#book-list {
    max-width: 600px;
    margin: 0 auto;
    padding: 1rem;
}

h1 {
    text-align: center;
    font-family: "Amiri", serif;
    font-size: 2.5rem;
    margin-bottom: 2rem;
    color: #f9f6ef;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}

/* Mobile Portrait Mode - Data attribute selector for proper mobile detection */
body[data-device-type="mobile"][data-orientation="portrait"],
body[data-device-type="mobile"][data-orientation="portrait"] html {
    margin: 0;
    padding: 0;
    height: 100vh;
    overflow: hidden;
}

body[data-device-type="mobile"][data-orientation="portrait"] .container {
    width: 100vw;
    height: 100vh;
    padding: 0;
    margin: 0;
}

body[data-device-type="mobile"][data-orientation="portrait"] #bookPages {
    width: 100%;
    height: 100vh;
}

body[data-device-type="mobile"][data-orientation="portrait"] .mobile-landscape-page {
    display: none !important;
}

body[data-device-type="mobile"][data-orientation="portrait"] .page-content.has-both {
    display: flex;
    flex-direction: column;
    padding: 1rem;
}

body[data-device-type="mobile"][data-orientation="portrait"] .page-content.has-both .story-image {
    flex: 1;
    width: 100%;
    height: 0;
    min-height: 0;
    object-fit: contain;
}

body[data-device-type="mobile"][data-orientation="portrait"] .page-content.has-both .story-text {
    flex: 0 0 auto;
    font-size: 18pt;
    line-height: 1.5;
    padding: 1rem;
    text-align: center;
}

body[data-device-type="mobile"][data-orientation="portrait"] .page-content.image-only {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

body[data-device-type="mobile"][data-orientation="portrait"] .page-content.image-only .story-image {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

body[data-device-type="mobile"][data-orientation="portrait"] .page-content.text-only {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

body[data-device-type="mobile"][data-orientation="portrait"] .page-content.text-only .story-text {
    font-size: 18pt;
    line-height: 1.5;
    text-align: center;
}

body[data-device-type="mobile"][data-orientation="portrait"] nav {
    position: fixed;
    bottom: 20px;
    width: 100vw;
    z-index: 1000;
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    box-sizing: border-box;
}

body[data-device-type="mobile"][data-orientation="portrait"] nav button {
    width: 60px;
    height: 60px;
    min-width: 60px;
    font-size: 1.5rem;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.7);
    color: white;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
    transition: all 0.3s ease;
}

body[data-device-type="mobile"][data-orientation="portrait"] nav button:hover {
    background: rgba(0, 0, 0, 0.9);
    transform: scale(1.1);
}

body[data-device-type="mobile"][data-orientation="portrait"] aside {
    display: none !important;
}

/* Mobile Portrait Mode - Fallback for viewport-based detection */
@media (max-width: 768px) and (orientation: portrait) {
    body, html {
        margin: 0;
        padding: 0;
        height: 100vh;
        overflow: hidden;
    }

    .container {
        width: 100vw;
        height: 100vh;
        padding: 0;
        margin: 0;
    }

    #bookPages {
        width: 100%;
        height: 100vh;
    }

    /* Hide mobile landscape pages in portrait mode */
    .mobile-landscape-page {
        display: none !important;
    }

    /* Mobile Portrait - Both image and text */
    .page-content.has-both {
        display: flex;
        flex-direction: column;
        padding: 1rem;
    }

    .page-content.has-both .story-image {
        flex: 1;
        width: 100%;
        height: 0;
        min-height: 0;
        object-fit: contain;
    }

    .page-content.has-both .story-text {
        flex: 0 0 auto;
        font-size: 18pt;
        line-height: 1.5;
        padding: 1rem;
        text-align: center;
    }

    /* Mobile Portrait - Image only */
    .page-content.image-only {
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 0;
    }

    .page-content.image-only .story-image {
        width: 100%;
        height: 100%;
        object-fit: contain;
    }

    /* Mobile Portrait - Text only */
    .page-content.text-only {
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 1rem;
    }

    .page-content.text-only .story-text {
        font-size: 18pt;
        line-height: 1.5;
        text-align: center;
    }

    /* Navigation buttons for mobile portrait */
    nav {
        position: fixed;
        bottom: 20px;
        width: 100vw;
        z-index: 1000;
        display: flex;
        justify-content: space-between;
        padding: 0 20px;
        box-sizing: border-box;
    }

    nav button {
        width: 60px;
        height: 60px;
        min-width: 60px;
        font-size: 1.5rem;
        border-radius: 50%;
        background: rgba(0, 0, 0, 0.7);
        color: white;
        border: none;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        touch-action: manipulation;
        transition: all 0.3s ease;
    }

    nav button:hover {
        background: rgba(0, 0, 0, 0.9);
        transform: scale(1.1);
    }

    /* Hide aside element */
    aside {
        display: none !important;
    }
}

/* Mobile Landscape Mode - Data attribute selector for proper mobile detection */
body[data-device-type="mobile"][data-orientation="landscape"],
body[data-device-type="mobile"][data-orientation="landscape"] html {
    margin: 0;
    padding: 0;
    height: 100vh;
    overflow: hidden;
}

body[data-device-type="mobile"][data-orientation="landscape"] .container {
    width: 100vw;
    height: 100vh;
    padding: 0;
    margin: 0;
}

body[data-device-type="mobile"][data-orientation="landscape"] #bookPages {
    width: 100%;
    height: 100vh;
}

/* Hide regular story pages in landscape mode */
body[data-device-type="mobile"][data-orientation="landscape"] .book-page:not(.mobile-landscape-page):not(.front-cover):not(.back-cover):not(.empty-page):not(.end-of-book-page) {
    display: none !important;
}

/* Mobile landscape page container */
body[data-device-type="mobile"][data-orientation="landscape"] .mobile-landscape-page {
    width: 100vw;
    height: 100vh;
    display: flex !important;
}

/* Landscape page content - Both image and text */
body[data-device-type="mobile"][data-orientation="landscape"] .page-content-landscape.has-both {
    display: flex;
    flex-direction: row;
    width: 100%;
    height: 100%;
    padding: 0;
}

body[data-device-type="mobile"][data-orientation="landscape"] .page-content-landscape.has-both .landscape-image-section {
    flex: 0 0 50%;
    width: 50%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f8f3e6;
    padding: 1rem;
    box-sizing: border-box;
}

body[data-device-type="mobile"][data-orientation="landscape"] .page-content-landscape.has-both .landscape-image-section .story-image {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

body[data-device-type="mobile"][data-orientation="landscape"] .page-content-landscape.has-both .landscape-text-section {
    flex: 0 0 50%;
    width: 50%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f8f3e6;
    padding: 1.5rem;
    box-sizing: border-box;
}

body[data-device-type="mobile"][data-orientation="landscape"] .page-content-landscape.has-both .landscape-text-section .story-text {
    font-size: 18pt;
    line-height: 1.6;
    text-align: center;
}

/* Landscape page content - Image only */
body[data-device-type="mobile"][data-orientation="landscape"] .page-content-landscape.image-only {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    padding: 0;
}

body[data-device-type="mobile"][data-orientation="landscape"] .page-content-landscape.image-only .landscape-image-section {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

body[data-device-type="mobile"][data-orientation="landscape"] .page-content-landscape.image-only .landscape-image-section .story-image {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

/* Landscape page content - Text only */
body[data-device-type="mobile"][data-orientation="landscape"] .page-content-landscape.text-only {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    padding: 1.5rem;
}

body[data-device-type="mobile"][data-orientation="landscape"] .page-content-landscape.text-only .landscape-text-section {
    display: flex;
    align-items: center;
    justify-content: center;
}

body[data-device-type="mobile"][data-orientation="landscape"] .page-content-landscape.text-only .landscape-text-section .story-text {
    font-size: 18pt;
    line-height: 1.6;
    text-align: center;
}

/* Navigation buttons for mobile landscape */
body[data-device-type="mobile"][data-orientation="landscape"] nav {
    position: fixed;
    bottom: 20px;
    width: 100vw;
    z-index: 1000;
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    box-sizing: border-box;
}

body[data-device-type="mobile"][data-orientation="landscape"] nav button {
    width: 60px;
    height: 60px;
    min-width: 60px;
    font-size: 1.5rem;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.7);
    color: white;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
    transition: all 0.3s ease;
}

body[data-device-type="mobile"][data-orientation="landscape"] nav button:hover {
    background: rgba(0, 0, 0, 0.9);
    transform: scale(1.1);
}

/* Hide aside element */
body[data-device-type="mobile"][data-orientation="landscape"] aside {
    display: none !important;
}

/* Mobile Landscape Mode - Fallback for viewport-based detection */
@media (max-width: 768px) and (orientation: landscape) {
    body, html {
        margin: 0;
        padding: 0;
        height: 100vh;
        overflow: hidden;
    }

    .container {
        width: 100vw;
        height: 100vh;
        padding: 0;
        margin: 0;
    }

    #bookPages {
        width: 100%;
        height: 100vh;
    }

    /* Hide regular story pages in landscape mode */
    .book-page:not(.mobile-landscape-page):not(.front-cover):not(.back-cover):not(.empty-page):not(.end-of-book-page) {
        display: none !important;
    }

    /* Mobile landscape page container */
    .mobile-landscape-page {
        width: 100vw;
        height: 100vh;
        display: flex !important;
    }

    /* Landscape page content - Both image and text */
    .page-content-landscape.has-both {
        display: flex;
        flex-direction: row;
        width: 100%;
        height: 100%;
        padding: 0;
    }

    .page-content-landscape.has-both .landscape-image-section {
        flex: 0 0 50%;
        width: 50%;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        background: #f8f3e6;
        padding: 1rem;
        box-sizing: border-box;
    }

    .page-content-landscape.has-both .landscape-image-section .story-image {
        width: 100%;
        height: 100%;
        object-fit: contain;
    }

    .page-content-landscape.has-both .landscape-text-section {
        flex: 0 0 50%;
        width: 50%;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        background: #f8f3e6;
        padding: 1.5rem;
        box-sizing: border-box;
    }

    .page-content-landscape.has-both .landscape-text-section .story-text {
        font-size: 18pt;
        line-height: 1.6;
        text-align: center;
    }

    /* Landscape page content - Image only */
    .page-content-landscape.image-only {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        height: 100%;
        padding: 0;
    }

    .page-content-landscape.image-only .landscape-image-section {
        width: 100%;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .page-content-landscape.image-only .landscape-image-section .story-image {
        width: 100%;
        height: 100%;
        object-fit: contain;
    }

    /* Landscape page content - Text only */
    .page-content-landscape.text-only {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        height: 100%;
        padding: 1.5rem;
    }

    .page-content-landscape.text-only .landscape-text-section {
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .page-content-landscape.text-only .landscape-text-section .story-text {
        font-size: 18pt;
        line-height: 1.6;
        text-align: center;
    }

    /* Navigation buttons for mobile landscape */
    nav {
        position: fixed;
        bottom: 20px;
        width: 100vw;
        z-index: 1000;
        display: flex;
        justify-content: space-between;
        padding: 0 20px;
        box-sizing: border-box;
    }

    nav button {
        width: 60px;
        height: 60px;
        min-width: 60px;
        font-size: 1.5rem;
        border-radius: 50%;
        background: rgba(0, 0, 0, 0.7);
        color: white;
        border: none;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        touch-action: manipulation;
        transition: all 0.3s ease;
    }

    nav button:hover {
        background: rgba(0, 0, 0, 0.9);
        transform: scale(1.1);
    }

    /* Hide aside element */
    aside {
        display: none !important;
    }
}

/* Touch target accessibility */
@media (max-width: 1024px) {
    button, a, .clickable {
        min-width: 44px;
        min-height: 44px;
        touch-action: manipulation;
    }
    
    /* Ensure proper spacing for touch targets */
    nav button {
        margin: 8px;
    }
}