/* =========================================
   基本設定・フォント・変数
   ========================================= */
@import url('https://fonts.googleapis.com/css2?family=M+PLUS+Rounded+1c:wght@400;700&display=swap');

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

:root {
    --primary-gradient: linear-gradient(135deg, #a777e3, #6e8efb);
    --secondary-gradient: linear-gradient(45deg, #ff9a9e, #fecfef);
    --accent-color: #ff6b6b;
    --text-color: #444;
    --glass-bg: rgba(255, 255, 255, 0.9);
    --glass-border: rgba(255, 255, 255, 0.6);
    --shadow-soft: 0 10px 30px rgba(100, 100, 255, 0.15);
    --shadow-strong: 0 25px 50px rgba(0,0,0,0.3);
}

html, body {
    width: 100%;
    margin: 0;
    padding: 0;
    /* overflow-x: hidden; ←これを消してJS制御に任せる */
}

body {
    font-family: 'M PLUS Rounded 1c', 'Segoe UI', sans-serif;
    background: linear-gradient(-45deg, #a777e3, #6e8efb, #ff9a9e, #a18cd1);
    background-size: 400% 400%;
    animation: gradientBG 15s ease infinite;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    color: var(--text-color);
}

@keyframes gradientBG {
    0% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
}

/* =========================================
   ゲームコンテナ
   ========================================= */
#game-container {
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    border: 2px solid var(--glass-border);
    border-radius: 30px;
    box-shadow: var(--shadow-soft);
    padding: 40px;
    max-width: 1000px;
    width: 95%;
    position: relative;
    min-height: 600px; 
    margin: 20px auto;
}

h1 {
    text-align: center;
    font-size: 3.5em;
    margin-bottom: 30px;
    background: var(--primary-gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    filter: drop-shadow(2px 2px 0px rgba(0,0,0,0.1));
    letter-spacing: 2px;
}

h2, h3 { color: #555; margin-top: 25px; text-align: center; }
#setup-screen { text-align: center; padding: 20px; }

/* 入力エリア */
#player-name {
    padding: 15px 25px; font-size: 1.2rem; border: 2px solid #e0e0e0;
    border-radius: 50px; margin: 10px auto; width: 60%; max-width: 400px;
    display: block; outline: none; transition: all 0.3s ease;
    background: rgba(255,255,255,0.9); font-family: inherit; text-align: center;
}
#player-name:focus { border-color: #6e8efb; box-shadow: 0 0 15px rgba(110, 142, 251, 0.3); transform: scale(1.02); }

.input-group { margin: 20px auto; text-align: center; max-width: 450px; }
.input-group label { display: block; color: #666; margin-bottom: 8px; font-weight: bold; font-size: 0.95rem; }

#player-image {
    font-size: 0.95rem; color: #666; background: white; padding: 8px;
    border-radius: 50px; border: 2px dashed #ccc; width: 100%; cursor: pointer; transition: all 0.3s;
}
#player-image:hover { border-color: #a777e3; background: #fbf5ff; }
#player-image::file-selector-button {
    padding: 8px 20px; margin-right: 15px; border-radius: 50px; border: none;
    background: linear-gradient(45deg, #a777e3, #6e8efb); color: white;
    font-weight: bold; cursor: pointer; font-family: 'M PLUS Rounded 1c', sans-serif; transition: transform 0.2s;
}
#player-image::file-selector-button:hover { transform: translateY(-2px); box-shadow: 0 4px 10px rgba(167, 119, 227, 0.4); }

/* ボタン */
button {
    font-family: 'M PLUS Rounded 1c', sans-serif; padding: 12px 30px; font-size: 1.1rem;
    border: none; border-radius: 50px; cursor: pointer; margin: 10px;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    font-weight: bold; box-shadow: 0 5px 15px rgba(0,0,0,0.1); letter-spacing: 1px;
}
button:hover { transform: translateY(-5px); box-shadow: 0 8px 20px rgba(0,0,0,0.2); }
button:active { transform: translateY(-2px); }

#spectator-mode-btn { background: linear-gradient(45deg, #ff9a9e 0%, #fecfef 99%, #fecfef 100%); color: white; text-shadow: 1px 1px 2px rgba(0,0,0,0.1); min-width: 200px; }
#spectator-mode-btn:hover { box-shadow: 0 8px 20px rgba(255, 154, 158, 0.4); }
#start-game, #confirm-characters, #confirm-selection, #next-round-button { background: linear-gradient(45deg, #4facfe 0%, #00f2fe 100%); color: white; min-width: 200px; }
#play-button { background: linear-gradient(45deg, #43e97b 0%, #38f9d7 100%); color: white; min-width: 140px; }
#pass-button, #title-button { background: linear-gradient(45deg, #ff9a9e 0%, #fecfef 99%, #fecfef 100%); color: white; min-width: 140px; }
#show-rules, #change-characters, #change-characters-btn, #select-characters-btn { background: white; color: #6e8efb; border: 2px solid #6e8efb; }
#show-rules:hover, #change-characters:hover, #change-characters-btn:hover, #select-characters-btn:hover { background: #f0f7ff; }

/* フィールド・カード */
#field {
    height: 320px; border: 4px dashed rgba(167, 119, 227, 0.3); border-radius: 30px;
    margin: 30px 0; display: flex; justify-content: center; align-items: center;
    background: rgba(255, 255, 255, 0.4); position: relative;
}
.card {
    width: 110px; height: 160px; border-radius: 12px; background-color: white;
    background-size: 100% 100%; background-repeat: no-repeat; display: inline-block;
    margin-right: -40px; position: relative; box-shadow: 0 5px 15px rgba(0,0,0,0.15);
    transition: all 0.3s ease; cursor: pointer; border: 1px solid rgba(0,0,0,0.1); flex-shrink: 0;
}
.card:last-child { margin-right: 20px; }
.card:hover { transform: translateY(-20px) scale(1.1); z-index: 100; box-shadow: 0 15px 30px rgba(0,0,0,0.25); }
.card.selected { transform: translateY(-30px); border: 3px solid var(--accent-color); box-shadow: 0 0 20px rgba(255, 107, 107, 0.6); z-index: 50; }

#player-hand {
    height: 230px; margin: 20px 0; padding: 30px 40px; background: rgba(255, 255, 255, 0.5);
    border-radius: 30px; display: flex; justify-content: flex-start; align-items: center;
    overflow-x: auto; overflow-y: hidden; border: 2px solid white;
    scrollbar-width: thin; white-space: nowrap;
}

/* 通知 */
#game-notification {
    position: fixed; top: 20px; left: 50%; transform: translate(-50%, -100px);
    background: rgba(255, 80, 80, 0.95); color: white; padding: 15px 40px;
    border-radius: 50px; font-weight: bold; font-size: 1.2rem;
    box-shadow: 0 10px 20px rgba(0,0,0,0.2); z-index: 10000;
    transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    pointer-events: none; white-space: nowrap;
    z-index: 2147483647 !important;
}
#game-notification.show { transform: translate(-50%, 0); }

/* リザルト */
.result-content { text-align: center; }
.result-row {
    display: flex; align-items: center; justify-content: space-between;
    background: white; padding: 15px; margin: 10px 0; border-radius: 15px;
    box-shadow: 0 5px 15px rgba(0,0,0,0.05); border-left: 5px solid #ccc;
}
.result-row.rank-1 { border-left-color: #ffd700; background: #fffdf0; }
.result-row.rank-2 { border-left-color: #c0c0c0; }
.result-row.rank-3 { border-left-color: #cd7f32; }
.result-row.rank-4 { border-left-color: #6e8efb; }
.result-row.rank-5 { border-left-color: #444; color: #666; }
.result-rank { font-size: 1.5rem; font-weight: bold; width: 50px; }
.result-name { font-size: 1.2rem; flex-grow: 1; text-align: left; padding-left: 20px; }
.result-title { font-weight: bold; }

/* BGMボタン（固定：右上） */
.bgm-btn {
    position: fixed; top: 20px; right: 20px; z-index: 10001;
    width: 50px; height: 50px; border-radius: 50%; background: white; color: #888;
    border: 2px solid #ddd; font-size: 1.2rem; display: flex;
    justify-content: center; align-items: center; box-shadow: 0 4px 10px rgba(0,0,0,0.1);
    transition: all 0.3s; padding: 0; margin: 0;
}
.bgm-btn:hover { transform: scale(1.1); background: #f0f7ff; color: #6e8efb; border-color: #6e8efb; }
.bgm-btn.playing { color: #ff6b6b; border-color: #ff6b6b; background: #fff0f0; animation: pulse 2s infinite; }
@keyframes pulse {
    0% { box-shadow: 0 0 0 0 rgba(255, 107, 107, 0.4); }
    70% { box-shadow: 0 0 0 10px rgba(255, 107, 107, 0); }
    100% { box-shadow: 0 0 0 0 rgba(255, 107, 107, 0); }
}

/* =========================================
   モーダル共通（PC版の基本）
   ========================================= */
.modal {
    display: none; /* ここを none に戻す！ */
    position: fixed; z-index: 3000; left: 0; top: 0;
    width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(8px);
    justify-content: center; align-items: center;
    overflow: hidden; 
}
/* JSで表示する時だけflexにする */
.modal[style*="display: block"] { display: flex !important; }

/* モーダルの箱（PC版） */
.modal-content, .character-modal-content {
    background: rgba(255, 255, 255, 0.98); 
    margin: 0; padding: 20px; border-radius: 20px;
    width: 95%; max-width: 1000px; height: 90vh; 
    box-shadow: var(--shadow-strong); position: relative;
    border: 1px solid white; animation: slideDown 0.4s ease;
    
    /* 中身のスクロール許可 */
    overflow-y: auto; display: block; 
    scrollbar-width: thin; scrollbar-color: #cbaaca #f1f1f1;
}

@keyframes slideDown { from { opacity: 0; transform: translateY(-20px); } to { opacity: 1; transform: translateY(0); } }

/* =========================================
   ★修正3: スクロールロックの強化
   ========================================= */
/* モーダルが開いている時は、裏側のスクロールを完全に禁止 */
html.modal-open, body.modal-open {
    overflow: hidden !important;
    height: 100vh !important;
    position: fixed; /* これを入れるとiPhoneでもガチガチに固まる */
    width: 100%;
}

/* 閉じるボタン */
.close {
    color: #aaa; font-size: 32px; font-weight: bold; cursor: pointer;
    transition: color 0.3s; 
    position: absolute; left: 20px; top: 15px; z-index: 20000;
}
.close:hover { color: #ff6b6b; }

/* =========================================
   キャラ選択画面
   ========================================= */
.character-modal-content > h2 { margin-top: 10px; margin-bottom: 10px; text-align: center; }

/* プロフエリア */
.character-details {
    margin: 10px 0; display: flex; align-items: center; gap: 15px;
    background: #f8f9fa; border-radius: 20px; padding: 10px; border: 2px solid #eee;
}
.character-portrait img { width: 100px; height: 100px; border-radius: 50%; border: 4px solid #a777e3; box-shadow: 0 5px 15px rgba(0,0,0,0.2); }
#character-description { white-space: pre-wrap; line-height: 1.5; font-size: 0.95rem; }

/* キャラ一覧 */
#character-grid { overflow: visible; height: auto; padding: 0; }
.character-section-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 15px; }

.character-card {
    background: white; border-radius: 20px; padding: 10px; text-align: center;
    cursor: pointer; transition: all 0.3s ease; border: 3px solid transparent; 
    box-shadow: 0 5px 10px rgba(0,0,0,0.05); width: 100%;
}
.character-card:hover { transform: translateY(-5px); box-shadow: 0 10px 20px rgba(0,0,0,0.15); border-color: #e0e0e0; }
.character-card.selected { border-color: #a777e3; background: #fbf5ff; transform: scale(1.02); box-shadow: 0 5px 15px rgba(167, 119, 227, 0.4); }
.character-card img { width: 80px; height: 80px; border-radius: 50%; object-fit: cover; margin-bottom: 5px; border: 3px solid #eee; box-shadow: 0 4px 8px rgba(0,0,0,0.1); }

/* 決定ボタンエリア */
.selected-characters {
    position: static; 
    background: transparent; border: none; box-shadow: none;
    padding: 20px 0 100px 0; 
    margin-top: 10px; text-align: center; border-top: 2px dashed #ddd;
}

#selected-characters-list { display: flex; flex-wrap: wrap; gap: 10px; margin: 15px 0; justify-content: center; }
.selected-character {
    background: var(--primary-gradient); color: white; padding: 8px 15px; border-radius: 20px;
    font-size: 0.9rem; display: flex; align-items: center; gap: 10px;
    box-shadow: 0 3px 6px rgba(0,0,0,0.2);
}
.remove-character {
    cursor: pointer; background: white; color: #a777e3; border-radius: 50%; width: 20px; height: 20px;
    display: flex; justify-content: center; align-items: center; font-weight: bold; font-size: 14px; line-height: 1;
}

.player {
    background: white; padding: 10px 20px; border-radius: 50px;
    box-shadow: 0 5px 15px rgba(0,0,0,0.05); border: 2px solid #eee;
    margin: 5px; display: inline-block; min-width: 140px; text-align: center;
    transition: all 0.3s;
}

/* =========================================
   ★修正1: 矢印ボタン（PC・スマホ共通）
   左下(bottom)の設定を消して、右上(top/right)にするよ！
   ========================================= */
/* =========================================
   ★修正: 矢印ボタン（位置をもっと下に）
   ========================================= */
#modal-jump-btn {
    position: fixed !important;
    top: 130px !important;    /* ★80px → 130px に変更（BGMボタンから離す） */
    right: 20px !important;
    z-index: 2147483647 !important; /* 最強のz-index */
    
    width: 50px; height: 50px;
    border-radius: 50%;
    background: var(--accent-color);
    color: white;
    font-size: 1.2rem;
    box-shadow: 0 4px 10px rgba(0,0,0,0.2);
    border: 3px solid white;
    cursor: pointer;
    
    /* display: none; ←これを消してJS制御だけにする */
    display: none; 
    align-items: center;
    justify-content: center;
    animation: bounce 2s infinite;
}

/* =========================================
   ルールモーダル専用の復活スタイル
   ========================================= */
#rules-modal .modal-content {
    /* 昔のポップアップ風スタイルに戻す */
    width: 90% !important;
    max-width: 800px !important;
    height: auto !important;
    max-height: 85vh !important;
    
    /* 画面中央寄せ */
    position: relative !important;
    margin: 5vh auto !important; 
    top: auto !important;
    left: auto !important;
    border-radius: 25px !important;
    
    /* 余白と装飾 */
    padding: 30px !important;
    border: 4px solid #a777e3 !important;
    box-shadow: 0 10px 40px rgba(0,0,0,0.3) !important;
}

#rules-modal h2 {
    color: #a777e3;
    border-bottom: 2px solid #ddd;
    padding-bottom: 10px;
}

#rule-explanation h3 {
    color: #6e8efb;
    border-left: 5px solid #6e8efb;
    padding-left: 10px;
    margin-top: 25px;
    text-align: left;
    background: #f0f7ff;
    padding: 5px 10px;
    border-radius: 0 10px 10px 0;
}

#rule-explanation p {
    text-align: left;
    line-height: 1.8;
    margin-bottom: 8px;
    padding-left: 5px;
}

/* 下向きに弾むアニメーション */
@keyframes bounce {
    0%, 20%, 50%, 80%, 100% {transform: translateY(0);}
    40% {transform: translateY(5px);}
    60% {transform: translateY(3px);}
}

/* セリフ吹き出し */
#dialogue {
    position: fixed; bottom: 120px; left: 50%; background: transparent;
    border: none; box-shadow: none; width: auto; max-width: 90%;
    pointer-events: none; display: none; z-index: 9999;
}
@keyframes popInStable { 0% { transform: translate(-50%, 0) scale(0.8); opacity: 0; } 100% { transform: translate(-50%, 0) scale(1); opacity: 1; } }
#dialogue.pop-in { animation: popInStable 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards; }
.dialogue-container {
    display: flex; align-items: center; background: rgba(255, 255, 255, 0.95);
    padding: 15px 25px; border-radius: 50px; border: 3px solid #a777e3;
    box-shadow: 0 10px 25px rgba(0,0,0,0.2); min-width: 300px; justify-content: center;
}
.dialogue-icon { flex-shrink: 0; margin-right: 15px; }
.dialogue-icon img, .dialogue-icon .fallback-icon { width: 60px; height: 60px; border-radius: 50%; border: 2px solid #a777e3; object-fit: cover; background: #fff; display: block; }
.dialogue-icon .fallback-icon { display: flex; align-items: center; justify-content: center; background: #eee; color: #a777e3; font-weight: bold; font-size: 24px; }
.dialogue-content { text-align: left; flex-grow: 1; }
.dialogue-name { font-size: 0.85rem; color: #666; display: block; margin-bottom: 2px; }
.dialogue-text { font-size: 1.1rem; font-weight: bold; color: #333; }

/* =========================================
   ★スマホ対応（シンプルイズベスト修正版）
   ========================================= */
@media (max-width: 768px) {
    /* === ①全体レイアウト（flexをやめてblockに戻す） === */
    html, body {
        height: 100%;
        /* overflow: hidden; は削除済み */
    }
    
    #game-container { 
        /* ★上を130px空ける */
        padding-top: 170px !important;
        padding-left: 20px !important;
        padding-right: 20px !important;
        padding-bottom: 50px !important;
        
        width: 100%;
        min-height: 100dvh; /* 画面の高さいっぱいに伸ばす */
        
        border-radius: 0;
        border: none;
        margin: 0;
        
        /* ★重要：ここを「block」に戻す！これで変な位置ズレが直る */
        display: block !important; 
        overflow-y: auto !important; /* スクロール許可 */
    }

    h1 { 
        text-align: center;
        /* ★修正: タイトル自体にも少し余白を持たせておく */
        margin-top: 10px !important;
        margin-bottom: 30px !important;
        font-size: 1.8em; 
        line-height: 1.2;
    }
    
    /* === ②ゲーム画面の修正（バグの原因を除去！） === */
    #game-screen {
        width: 100% !important;
        /* ★ここにあった display: block !important; を削除したよ！！ */
        /* これでJSの制御（非表示）が効くようになる */
        
        margin-top: 0;
    }

    #field {
        height: 280px !important; 
        margin: 10px 0 !important;
    }

    #player-hand { 
        height: 160px !important; 
        padding: 10px 10px !important; 
        justify-content: flex-start !important;
        
        /* 横並び・スクロール設定 */
        display: flex !important;
        flex-direction: row !important; /* 横並び明示 */
        flex-wrap: nowrap !important;   /* 折り返し禁止 */
        overflow-x: auto !important;    /* 横スクロール許可 */
        -webkit-overflow-scrolling: touch;
        
        width: 100%;
        background: rgba(255, 255, 255, 0.4);
    }
    
    .card { 
        width: 70px !important; 
        height: 105px !important; 
        margin-right: -25px !important; 
        flex-shrink: 0 !important; 
    }

    /* === ③モーダル（キャラ選択など） === */
    .modal {
        padding: 0 !important;
        align-items: flex-start !important;
        background-color: rgba(255, 255, 255, 1) !important;
    }

    .modal-content, .character-modal-content {
        width: 100% !important;
        height: 100dvh !important;
        max-height: none !important;
        margin: 0 !important;
        border-radius: 0 !important;
        top: 0 !important;
        left: 0 !important;
        border: none !important;
        
        padding-top: 100px !important;
        padding-bottom: 150px !important; 
        overflow-y: auto !important;
    }
    
    /* === ④ボタン類 === */
    .close {
        position: fixed !important;
        top: 20px !important;
        left: 20px !important;
        z-index: 2147483647 !important;
        background: rgba(255, 255, 255, 0.9);
        width: 44px; height: 44px;
        border-radius: 50%;
        text-align: center;
        line-height: 44px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.2);
        color: #555 !important;
        display: block !important;
    }

    #modal-jump-btn { 
        top: 100px !important; 
        right: 20px !important;
    }
    #rules-modal .modal-content {
        width: 95% !important;
        margin: 10vh auto !important;
        padding: 20px !important;
        height: 70vh !important; /* 程よい高さ */
    }
    #dialogue { width: 95%; bottom: 80px; }
    .character-section-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
}
