/* 基本リセット */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
}

/* ページボディのスタイル */
.page_body {
    width: 595px;
    height: 842px;
    position: relative;
    overflow: visible;
}

html {
    overflow-x: hidden;
    width: 100%;
    max-width: 100vw;
}

/* モバイル・タブレット対応 */

body {
    font-family: "Noto Sans JP", sans-serif;
    background: #ffffff;
    overflow-x: hidden;
    overflow-y: hidden;
    height: 100vh;
    width: 100%;
    max-width: 100vw;
}

/* タブレット・モバイル: 縦スクロール許可 */
@media (max-width: 1024px) {
    body {
        overflow-y: auto;
    }
}

.book-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    width: 100%;
    overflow-x: hidden;
    max-width: 100vw;
    box-sizing: border-box;
}

/* ツールバー */
.toolbar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 20px;
    padding: 15px;
    background: #fff;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    z-index: 100;
}

.nav-btn {
    background: #4a90e2;
    color: white;
    border: none;
    padding: 10px 20px;
    font-size: 24px;
    cursor: pointer;
    border-radius: 4px;
    transition: background 0.3s;
    min-width: 50px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.nav-btn:hover {
    background: #357abd;
}

.nav-btn:disabled {
    background: #ccc;
    cursor: not-allowed;
}

.nav-btn.playing {
    background: #e74c3c;
    animation: pulse 1.5s ease-in-out infinite;
}

/* 音声ボタンの無効状態 */
#audioBtn:disabled {
    background: #b0b0b0;
    cursor: not-allowed;
    opacity: 0.5;
}

#audioBtn:disabled:hover {
    background: #b0b0b0;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.7; }
}

.page-info {
    font-size: 16px;
    font-weight: 500;
    min-width: 80px;
    text-align: center;
}

/* ズームコントロール */
.zoom-controls {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-left: 20px;
}

.zoom-btn {
    min-width: 40px;
    padding: 6px 10px;
    font-size: 18px;
}

.zoom-info {
    font-size: 14px;
    font-weight: 500;
    min-width: 50px;
    text-align: center;
}

/* 書籍ビューア */
.book-viewer {
    flex: 1;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    overflow-x: hidden;
    overflow-y: auto;
    padding: 20px;
    background: #e8e8e8;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y; /* 縦スクロールは許可、横スクロールは制御 */
}

/* ページコンテナ */
.page-container {
    position: relative;
    background: white;
    box-shadow: 0 4px 8px rgba(0,0,0,0.2);
    overflow-x: hidden;
    overflow-y: visible;
    max-width: 100%;
    width: 100%;
    box-sizing: border-box;
    touch-action: pan-y; /* 縦スクロールは許可、横スクロールは制御 */
    transition: opacity 0.2s ease-in-out; /* スムーズなフェードイン */
}

/* モバイル・タブレット: ポートレート（縦向き）= 単ページ表示 */
@media (max-width: 1024px) and (orientation: portrait) {
    .book-viewer {
        padding: 5px !important;
        align-items: flex-start;
        justify-content: center;
        overflow-x: auto; /* ピンチ時にJSで中央にスナップするため */
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .page-container {
        width: 595px !important;
        max-width: none !important;
        margin: 0 auto !important;
        transform-origin: top center !important;
        flex-shrink: 0;
    }
    
    .page-container.single-page,
    .page-container.spread {
        display: block;
    }
    
    .page-container.spread .page:nth-child(2) {
        display: none !important; /* 縦向きは左ページのみ表示 */
    }
    
    .page {
        width: 595px !important;
        min-width: 595px !important;
        max-width: 595px !important;
        display: flex;
        flex-direction: column;
        border-right: none !important;
        overflow: visible !important;
    }
    
    .page iframe {
        width: 595px !important;
        min-width: 595px !important;
        height: 842px !important;
        overflow: hidden;
    }
    
    .toolbar {
        padding: 8px 4px;
        gap: 12px;
        flex-wrap: nowrap;
        justify-content: center;
        min-height: auto;
        overflow-x: hidden;
    }
    
    .nav-btn {
        padding: 8px 12px;
        font-size: 18px;
        min-width: 40px;
        height: 40px;
        flex-shrink: 0;
    }
    
    .nav-btn i {
        font-size: 16px;
    }
    
    .page-info {
        font-size: 12px;
        min-width: 50px;
        padding: 0 4px;
        flex-shrink: 0;
    }
    
    .zoom-controls {
        display: none; /* ズームは自動調整なので非表示 */
    }
}

/* モバイル・タブレット: ランドスケープ（横向き）= 見開き表示 */
@media (max-width: 1024px) and (orientation: landscape) {
    .book-viewer {
        padding: 5px !important;
        align-items: flex-start;
        justify-content: center;
        overflow-x: auto;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .page-container {
        max-width: none !important;
        margin: 0 auto !important;
        display: flex !important;
        flex-wrap: nowrap;
        transform-origin: top center !important;
        flex-shrink: 0;
    }
    
    /* 単ページ表示の場合は幅を595pxに */
    .page-container.single-page {
        width: 595px !important;
    }
    
    /* 見開き表示の場合は幅を1190pxに */
    .page-container.spread {
        width: 1190px !important;
    }
    
    .page-container.single-page .page,
    .page-container.spread .page {
        display: flex !important;
    }
    
    .page {
        width: 595px !important;
        min-width: 595px !important;
        max-width: 595px !important;
        flex-shrink: 0;
        display: flex;
        flex-direction: column;
        overflow: visible !important;
    }
    
    .page iframe {
        width: 595px !important;
        min-width: 595px !important;
        height: 842px !important;
        overflow: hidden;
    }
    
    .toolbar {
        padding: 6px 4px;
        gap: 12px;
        flex-wrap: nowrap;
        justify-content: center;
        min-height: auto;
    }
    
    .nav-btn {
        padding: 6px 10px;
        font-size: 16px;
        min-width: 36px;
        height: 36px;
        flex-shrink: 0;
    }
    
    .nav-btn i {
        font-size: 14px;
    }
    
    .page-info {
        font-size: 11px;
        min-width: 45px;
        padding: 0 2px;
        flex-shrink: 0;
    }
    
    .zoom-controls {
        display: none; /* ズームは自動調整なので非表示 */
    }
}

/* 共通モバイル: ビューアは横スクロール可（ピンチ時に中央スナップ用） */
@media (max-width: 1024px) {
    html, body, .book-container {
        overflow-x: hidden !important;
        max-width: 100vw !important;
        width: 100vw !important;
    }
    
    .book-viewer {
        overflow-x: auto !important;
        overflow-y: auto !important;
        max-width: 100vw !important;
        width: 100% !important;
    }
}

/* タブレット・PC: 見開きA3表示 */
@media (min-width: 769px) {
    .book-viewer {
        padding: 20px;
        align-items: flex-start;
    }
    
    .page-container {
        display: flex;
        gap: 0;
        width: 1190px; /* A3幅 = 2 × A4幅 (595px × 2) */
        min-height: 842px; /* A3高さ = A4高さ */
        max-width: calc(100vw - 40px);
    }
    
    .page-container.single-page {
        width: 595px;
    }
    
    .page {
        width: 50%;
        min-height: 842px;
        display: flex;
        flex-direction: column;
        border-right: 1px solid #ddd;
    }
    
    .page-container.single-page .page {
        width: 100%;
        border-right: none;
    }
    
    .page:last-child {
        border-right: none;
    }
    
    /* 見開き時のページ間の区切り線 */
    .page-container.spread .page:first-child {
        border-right: 2px solid #ccc;
    }
}

/* iPad向けの最適化 */
@media (min-width: 769px) and (max-width: 1024px) {
    .page-container {
        width: 100%;
        max-width: 1190px;
        min-height: 842px;
    }
    
    .page {
        width: 50%;
        min-height: 842px;
    }
}

/* 大きなPC画面向け */
@media (min-width: 1440px) {
    .page-container {
        width: 1190px;
        min-height: 842px;
    }
}

/* ページ読み込み中の表示 */
.page-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    background: #f0f0f0;
    color: #666;
    font-size: 18px;
}

/* ページ内のコンテンツ調整 */
.page {
    overflow-x: hidden;
    overflow-y: visible;
    max-width: 100%;
    box-sizing: border-box;
}

.page iframe {
    width: 100%;
    max-width: 100%;
    min-height: 842px;
    height: 100%;
    border: none;
    display: block;
    flex: 1;
    box-sizing: border-box;
    scrollbar-width: none; /* Firefox用 */
    -ms-overflow-style: none; /* IE/Edge用 */
    touch-action: pan-y; /* 縦スクロールは許可、横スクロールは制御 */
}

.page iframe::-webkit-scrollbar {
    display: none; /* Chrome/Safari/Opera用 */
}

.page img {
    width: 100%;
    max-width: 100%;
    height: auto;
    display: block;
    box-sizing: border-box;
}

/* スムーズなスクロール */
.book-viewer {
    scroll-behavior: smooth;
}

/* タッチデバイス向けの最適化 */
@media (hover: none) and (pointer: coarse) {
    .nav-btn {
        min-width: 44px;
        min-height: 44px;
        padding: 10px 15px;
    }
    
    @media (max-width: 768px) {
        .nav-btn {
            min-width: 36px;
            min-height: 36px;
            padding: 6px 10px;
        }
    }
}

/* 非常に小さな画面向けの最適化 */
@media (max-width: 480px) {
    .toolbar {
        padding: 6px 3px;
        gap: 10px;
    }
    
    .nav-btn {
        padding: 5px 6px;
        font-size: 14px;
        min-width: 28px;
        height: 32px;
    }
    
    .nav-btn i {
        font-size: 12px;
    }
    
    .page-info {
        font-size: 10px;
        min-width: 40px;
    }
    
    .zoom-controls {
        gap: 8px;
    }
    
    .zoom-btn {
        min-width: 26px;
        padding: 3px 5px;
        font-size: 11px;
        height: 28px;
    }
    
    .zoom-btn i {
        font-size: 11px;
    }
    
    .zoom-info {
        font-size: 9px;
        min-width: 28px;
    }
}

/* 目次モーダル */
.toc-modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    overflow: auto;
    font-size: larger;
    font-weight: bold;
}

.toc-modal.active {
    display: flex;
    align-items: center;
    justify-content: center;
}

.toc-modal-content {
    background-color: #fff;
    margin: 5% auto;
    padding: 0;
    border-radius: 8px;
    width: 90%;
    max-width: 600px;
    max-height: 90vh;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
    display: flex;
    flex-direction: column;
}

.toc-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 20px;
    border-bottom: 1px solid #eee;
}

.toc-modal-header h2 {
    margin: 0;
    font-size: 24px;
    font-weight: 600;
}

.close-btn {
    background: none;
    border: none;
    font-size: 32px;
    color: #666;
    cursor: pointer;
    padding: 0;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: background 0.3s;
}

.close-btn:hover {
    background: #f0f0f0;
    color: #333;
}

.toc-modal-body {
    padding: 10px 20px;
    overflow-y: auto;
    flex: 1;
}

.toc-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

.toc-list li {
    margin: 0;
    padding: 0;
}

.toc-list a {
    display: block;
    padding: 12px 16px;
    text-decoration: none;
    color: #333;
    border-bottom: 1px solid #f0f0f0;
    transition: background 0.2s;
    cursor: pointer;
}

.toc-list a:hover {
    background: #ffffff;
}

.toc-list a:active {
    background: #e8e8e8;
}

.toc-item-number {
    font-weight: 600;
    margin-right: 8px;
    color: #4a90e2;
}

.toc-item-title {
    font-size: 18px;
    color: #333;
}

/* スマートフォン向けの目次モーダル */
@media (max-width: 768px) {
    .toc-modal-content {
        width: 95%;
        max-width: none;
        margin: 10% auto;
        max-height: 85vh;
    }
    
    .toc-modal-header {
        padding: 15px;
    }
    
    .toc-modal-header h2 {
        font-size: 20px;
    }
    
    .toc-modal-body {
        padding: 15px;
    }
    
    .toc-list a {
        padding: 10px 12px;
        font-size: 14px;
    }
}

