/* ---------- 9.8 STORY 画面 ---------- */
#story-overlay {
    display: none;
    opacity: 0; /* フェードインさせるため初期値は0にする */
    transition: opacity 0.5s ease;

    position: absolute;
    inset: 0;
    z-index: 140;

    flex-direction: column;
    align-items: center;
    justify-content: flex-start; /* 上寄せ */
}

#story-panel {
    position: relative;
    z-index: 2;
    width: min(92vw, 720px);
    padding: 18px 16px 16px;
    border-radius: 3px;
    border: 1px solid rgba(120,255,255,0.18);
    background: rgba(0,0,0,0.5);
    box-shadow:
        0 0 30px rgba(0,255,255,0.08),
        inset 0 0 24px rgba(120,255,255,0.04);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    
    /* 上下中央寄せ */
    margin-top: auto;
    margin-bottom: auto;

    /* ★修正：高さを自動計算にし、はみ出しを完全に防ぐ最強の設定 */
    display: flex;
    flex-direction: column;
    box-sizing: border-box; /* 内側の余白（padding）を高さに含める */
    flex: 0 1 auto;         /* 画面が狭いときは自動的に縮む */
    min-height: 0;          /* 縮んだ状態でスクロールさせるためのおまじない */
}

/* 本文パネル */
#story-scroll-container {
    width: 100%;
    overflow-y: auto;
    overflow-x: hidden;
    margin: 0;
    padding-right: 12px;
    -webkit-overflow-scrolling: touch;

    scrollbar-width: thin;
    scrollbar-color: rgba(0, 255, 255, 0.6) rgba(0, 255, 255, 0.05);

    /* ★追加：親(story-panel)の空きスペースをすべて埋める */
    flex: 1;
    min-height: 0;
    height: 100%;
}

/* スクロールバー */
#story-scroll-container::-webkit-scrollbar {
    width: 7px;
}

#story-scroll-container::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.04);
    border-radius: 999px;
}

#story-scroll-container::-webkit-scrollbar-thumb {
    background: linear-gradient(
        to bottom,
        rgba(0, 255, 255, 0.35),
        rgba(0, 255, 255, 0.7)
    );
    border-radius: 999px;
    box-shadow: 0 0 10px rgba(0, 255, 255, 0.35);
}

#story-scroll-container::-webkit-scrollbar-thumb:hover {
    background: rgba(0, 255, 255, 0.9);
}

/* 見出し共通 */
#story-scroll-container h1,
#story-scroll-container h2 {
    position: relative;
    color: #bffcff;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    text-shadow:
        0 0 8px rgba(0, 255, 255, 0.35),
        0 0 18px rgba(0, 255, 255, 0.18);
}

/* メインタイトル */
#story-scroll-container h1 {
    margin: 0 0 28px;
    padding: 0 0 14px;
    font-size: 24px;
    text-align: center;
    line-height: 1.4;
    border-bottom: 1px solid rgba(0, 255, 255, 0.45);
}

#story-scroll-container h1::after {
    content: "";
    position: absolute;
    left: 50%;
    bottom: -1px;
    width: 140px;
    height: 2px;
    transform: translateX(-50%);
    background: linear-gradient(
        to right,
        transparent,
        rgba(0, 255, 255, 0.95),
        transparent
    );
    box-shadow: 0 0 12px rgba(0, 255, 255, 0.55);
}

/* セクション見出し */
#story-scroll-container h2 {
    margin: 2.4em 0 1em;
    padding: 0 0 8px;
    font-size: 16px;
    line-height: 1.5;
    border-bottom: 1px solid rgba(0, 255, 255, 0.22);
}

#story-scroll-container h2::after {
    content: "";
    position: absolute;
    left: 0;
    bottom: -1px;
    width: 72px;
    height: 1px;
    background: rgba(0, 255, 255, 0.9);
    box-shadow: 0 0 8px rgba(0, 255, 255, 0.45);
}

/* 英文 */
.en-text {
    color: rgba(220, 235, 240, 0.78);
    font-size: 12.5px;
    line-height: 1.8;
    margin: 0 0 0.45em;
    letter-spacing: 0.04em;
    text-shadow: 0 0 2px rgba(255, 255, 255, 0.08);
}

/* 和文 */
.ja-text {
    color: #d7e3e8;
    font-size: 13px;
    line-height: 1.9;
    font-family: 'Hiragino Kaku Gothic ProN', 'Noto Sans JP', 'Meiryo', sans-serif;
    letter-spacing: 0.04em;
    margin: 0 0 2.2em;
    text-shadow: 0 0 2px rgba(255, 255, 255, 0.06);
}

/* 強調 */
#story-scroll-container strong {
    color: #ffffff;
    font-weight: 700;
    text-shadow:
        0 0 6px rgba(255,255,255,0.35),
        0 0 10px rgba(0,255,255,0.18);
}

/* 段落の読みやすさ */
#story-scroll-container p {
    max-width: 92ch;
}

#btn-story-back {
    margin-top: 20px;
    margin-bottom: 20px;
    flex-shrink: 0; /* 画面が狭くてもボタンが潰れないように保護 */
}

/* =========================================
   レスポンシブ (メディアクエリ)
========================================= */

/* スマホ縦画面 (Portrait) */
@media (orientation: portrait) {
    #story-overlay {
        padding-top: 5vh;
    }

    #story-panel {
        width: 90vw;
        padding: 14px 12px 12px;
    }

    #story-scroll-container {
        width: 100%;
        padding-right: 6px;
    }

    #story-scroll-container h1 {
        font-size: 20px;
        margin-bottom: 22px;
    }

    #story-scroll-container h2 {
        font-size: 15px;
        margin-top: 2em;
    }

    .en-text {
        font-size: 11.5px;
    }

    .ja-text {
        font-size: 12.5px;
        margin-bottom: 2em;
    }
}

/* スマホ横画面 (Landscape) */
@media (orientation: landscape) {
    #story-overlay {
        padding-top: 4vh;
    }

    #story-panel {
        width: min(86vw, 720px);
        padding: 12px;
    }
    
    #btn-story-back {
        margin-top: 12px;
        margin-bottom: 12px;
    }
}