/* ======================================== */
/*  Variables & Reset                       */
/* ======================================== */
:root {
    --bg-color: #d9d3cf;
    --text-color: #333;
    --card-bg: #fff;
    --modal-bg: #faf7f2;
    
    /* SP Button Colors (Light Mode) */
    --sp-btn-bg: #c0e98b;
    --sp-btn-text: #000;
    --sp-btn-border: #ccc;
    --sp-btn-disabled-bg: #ccc;
    --sp-btn-disabled-text: #666;

    /* Auth Colors */
    --auth-btn-bg: #425e90;
    --auth-btn-text: #fff;
}

body.dark-mode {
    --bg-color: #a09689;
    --text-color: #f0f0f0;
    --card-bg: #261909;
    --modal-bg: #3a292f;

    /* SP Button Colors (Dark Mode) */
    --sp-btn-bg: #c0da9e;
    --sp-btn-text: #0a0a0a;
    --sp-btn-border: #777;
    
    /* Auth Colors */
    --auth-btn-bg: #5e81ac;
}

body {
    margin: 0;
    padding: 0;
    font-family: 'Helvetica Neue', Arial, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    transition: background-color 0.3s, color 0.3s;
    overflow-x: hidden;
    width: 100vw;
    height: 100vh;
}

/* --- Color Utilities --- */
.update-highlight { color: blue; font-weight: bold; }
.text-attention-red { color: red; font-weight: bold; }
.text-attention-yellow { color: #b8860b; font-weight: bold; }
.warning { color: red; font-weight: bold; margin-top: 20px; }
.warningtwo { color: rgb(64, 123, 233); font-weight: bold; margin-top: 20px; }

/* Dark Mode Overrides */
body.dark-mode .update-highlight { color: #ccc9ff; }
body.dark-mode .text-attention-red { color: #c98a8a; }
body.dark-mode .text-attention-yellow { color: #f9eec2; }
body.dark-mode .warning { color: #c0da9e; }
body.dark-mode .warningtwo { color: #a7819a; }

/* ======================================== */
/*  Animations                              */
/* ======================================== */
/* カード配布: 右からスライドイン */
@keyframes slideInRight {
    0% { 
        opacity: 0; 
        transform: translateX(100px) scale(0.8); /* 右に100px、少し小さく */
    }
    100% { 
        opacity: 1; 
        transform: translateX(0) scale(1); 
    }
}

/* カード戻し: 山札（右上）へ吸い込まれる */
@keyframes flyToDeck {
    0% { 
        opacity: 1; 
        transform: translate(0, 0) scale(1);
    }
    100% { 
        opacity: 0; 
        transform: translate(200px, -300px) scale(0.5); /* 右上へ */
    }
}

/* クラス定義 */
.anim-deal {
    /* 0.4秒かけてスライドイン */
    animation: slideInRight 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

.anim-return {
    /* 0.6秒かけて飛んでいく */
    animation: flyToDeck 0.6s ease-in forwards;
    pointer-events: none;
}

/* ゴーストカード（アニメーション専用の浮遊カード） */
.ghost-card {
    position: fixed; /* 画面全体を基準に配置 */
    z-index: 9999;   /* 最前面 */
    width: 130px; 
    height: 180px;
    background-size: cover; 
    background-position: center;
    background-color: #fff;
    border-radius: 8px;
    border: 1px solid #000;
    box-shadow: 0 10px 20px rgba(0,0,0,0.3);
    transition: all 0.8s cubic-bezier(0.68, -0.55, 0.265, 1.55); /* ビヨーンと動く設定 */
}

/* モバイル用ゴーストカード調整 */
body.layout-mobile .ghost-card {
    width: 65px; 
    height: 90px;
}


/* ======================================== */
/*  Common Components                       */
/* ======================================== */

/* カードの基本設定 */
.card {
    width: 130px; height: 180px;
    border: 1px solid #000;
    display: inline-block; margin: 5px;
    background-size: cover; background-position: center;
    background-color: var(--card-bg);
    border-radius: 8px;
    box-shadow: 2px 4px 8px rgba(0,0,0,0.3);
    position: relative;
    
    /* アニメーションの滑らかさを司る設定 */
    /* transform(移動・回転)とopacity(透明度)を滑らかに変化させる */
    transition: transform 0.6s cubic-bezier(0.25, 0.8, 0.25, 1), opacity 0.3s;
    transform-style: preserve-3d; /* 3D回転用 */
    will-change: transform; /* ブラウザに最適化を指示 */
}

body.dark-mode .card { border-color: #5e524b; }

/* 山札（画面外右上）からの配布アニメーション */
@keyframes dealFromDeck {
    0% {
        opacity: 0;
        transform: translate3d(200px, -300px, 0) scale(0.5) rotate(20deg);
    }
    100% {
        opacity: 1;
        transform: translate3d(0, 0, 0) scale(1) rotate(0deg);
    }
}

/* 山札へ戻るアニメーション */
@keyframes returnToDeck {
    0% {
        opacity: 1;
        transform: translate3d(0, 0, 0) scale(1);
    }
    100% {
        opacity: 0;
        transform: translate3d(200px, -300px, 0) scale(0.5) rotate(-20deg);
    }
}

/* JavaScriptから付与するクラス */
.anim-deal {
    animation: dealFromDeck 0.6s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

.anim-return {
    animation: returnToDeck 0.6s cubic-bezier(0.55, 0.055, 0.675, 0.19) forwards;
    pointer-events: none; /* アニメーション中はクリック不可 */
}

/* 交換時の強調表示（浮き上がり） */
.card.swapping {
    z-index: 100;
    box-shadow: 0 15px 30px rgba(0,0,0,0.5);
    border-color: gold;
}

/* Start Screen */
#start-screen {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: var(--bg-color); z-index: 50;
    display: flex; justify-content: center; align-items: center;
}
.content-box {
    background: var(--modal-bg); padding: 40px; border-radius: 15px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.2); text-align: center; max-width: 600px; width: 90%;
}
.main-btn { background: none; border: none; cursor: pointer; }
.main-btn img { width: 250px; transition: transform 0.1s; }
.main-btn:active img { transform: scale(0.95); }

.rules-container ul { list-style: none; padding-left: 0; margin: 20px 0; }
.rules-container li { margin-bottom: 10px; line-height: 1.6; }

/* Auth Buttons in Start Screen */
.auth-buttons { margin-top: 20px; display: flex; justify-content: center; gap: 15px; }
.auth-btn {
    padding: 10px 20px; border: none; border-radius: 5px;
    background-color: var(--auth-btn-bg); color: var(--auth-btn-text);
    font-size: 1rem; cursor: pointer; transition: opacity 0.2s;
}
.auth-btn:hover { opacity: 0.9; }
.auth-btn.secondary { background-color: transparent; border: 1px solid var(--text-color); color: var(--text-color); }

/* Message Log */
#message-log {
    position: fixed; top: 30px; left: 50%; transform: translateX(-50%);
    width: 80%; max-height: 120px; overflow-y: auto;
    font-size: 1.2rem; background: rgba(255,255,255,0.85);
    color: #333; padding: 10px; border: 1px solid #ccc;
    box-sizing: border-box; z-index: 40; border-radius: 8px;
}
body.dark-mode #message-log {
    background: rgba(61, 47, 36, 0.9); color: #f0f0f0; border-color: #555;
}

/* Buttons (Game) */
#buttons button {
    margin-bottom: 20px; background-color: transparent; 
    border: none; padding: 0; cursor: pointer; transition: transform 0.1s;
}
#buttons button:active { transform: scale(0.95); }
#buttons img { width: 390px; filter: drop-shadow(3px 3px 5px rgba(0,0,0,0.2)); }
#buttons #reset-all-button img { width: 180px; }

/* Next Game Button */
#end-game { position: fixed; bottom: 240px; right: 20px; z-index: 45; }
#end-game button { background: none; border: none; cursor: pointer; }
#end-game img { width: 390px; }

/* SP Dashboard */
.sp-cards-container {
    padding: 10px; border: 1px dashed #ccc; background-color: #f9f9f9;
    border-radius: 8px; font-size: 1rem; min-width: 250px;
}
body.dark-mode .sp-cards-container { background-color: #261909; border-color: #444; color: #f0f0f0; }
.sp-cards-container h3, .sp-cards-container h4 { margin: 0 0 5px 0; border-bottom: 1px solid #eee; }
body.dark-mode .sp-cards-container h3 { border-bottom-color: #444; }
.sp-card-types-wrapper { display: flex; gap: 10px; }
.sp-card-category { flex: 1; }
.sp-list-area { min-height: 30px; }
.empty-msg { color: #999; font-size: 0.8em; margin: 5px 0; }
.sp-card-item { 
    display: flex; justify-content: space-between; align-items: center; 
    margin-bottom: 4px; padding: 5px; background-color: var(--card-bg); 
    border: 1px solid #ddd; border-radius: 4px; font-size: 0.9em;
}
body.dark-mode .sp-card-item { border-color: #666; }

button.use-sp-button {
    margin-left: 5px; padding: 4px 10px; font-size: 0.9em; cursor: pointer;
    border-radius: 3px; white-space: nowrap;
    background-color: var(--sp-btn-bg); color: var(--sp-btn-text); border: 1px solid var(--sp-btn-border);
}
button.use-sp-button:disabled {
    background-color: var(--sp-btn-disabled-bg); color: var(--sp-btn-disabled-text); cursor: not-allowed; border-color: #999;
}

/* ======================================== */
/*  LAYOUT MODE: PC                         */
/* ======================================== */
body.layout-pc #ai-area {
    position: fixed; top: 20px; left: 20px; width: 70%; margin-top: 130px; font-size: 1.5rem;
}
body.layout-pc #player-area {
    position: fixed; bottom: 20px; left: 20px; width: calc(100% - 450px);
    font-size: 1.5rem; display: flex; flex-direction: column; gap: 10px;
}
body.layout-pc #buttons {
    position: fixed; top: 50%; right: 20px; transform: translateY(-50%);
    display: flex; flex-direction: column; align-items: flex-end;
}
body.layout-pc #end-game {
    bottom: auto;
    
    top: 75%; 
    right: 20px;
    
    transform: translateY(-50%);
    
    display: flex;
    flex-direction: column;
    align-items: flex-end; 
    gap: 15px; 
    
    z-index: 100; 
}
body.layout-pc .player-top-row { display: flex; gap: 20px; align-items: flex-end; }
body.layout-pc .hand {
    display: flex; flex-wrap: nowrap; overflow-x: auto; min-height: 190px; align-items: center; padding-left: 10px;
}

/* ======================================== */
/*  LAYOUT MODE: MOBILE                     */
/* ======================================== */
body.layout-mobile #game-container {
    display: flex; 
    flex-direction: column; 
    height: 100%; 
    box-sizing: border-box;
    /* メッセージログ(上)とボタン(下)のスペースは確保しつつ詰める */
    padding-top: 110px; 
    padding-bottom: 130px; 
    overflow-y: auto; 
    gap: 20px; /* 要素間の隙間を20px→10pxに */
}
body.layout-mobile #message-log {
    width: 90%; max-height: 80px; font-size: 0.95rem; padding: 5px 10px; top: 60px;
}
body.layout-mobile #ai-area, body.layout-mobile #player-area {
    position: relative; margin: 0 15px; top: auto; left: auto; width: auto; font-size: 1.1rem;
}
body.layout-mobile .hand {
    min-height: 105px; /* カード高さ90px + パディング分 */
    padding: 8px 5px; 
    background-color: rgba(0, 0, 0, 0.03);
    border-radius: 10px; 
    border: 1px solid rgba(0,0,0,0.05);
    display: flex; 
    align-items: center; 
    overflow-x: auto;
}
body.dark-mode.layout-mobile .hand {
    background-color: rgba(255, 255, 255, 0.05); border-color: rgba(255,255,255,0.1);
}
body.layout-mobile .sp-cards-container {
    width: 100%; 
    box-sizing: border-box; 
    font-size: 0.8rem; /* 少し小さく */
    margin-bottom: 2px; /* 下の余白を詰める */
    padding: 5px; 
}
body.layout-mobile .card {
    width: 65px; 
    height: 90px; 
    margin: 3px; 
    flex-shrink: 0; 
    box-shadow: 1px 2px 3px rgba(0,0,0,0.2);
    border-radius: 5px; /* 小さくなったので角丸も少し小さく */
}

/* Mobile Buttons */
body.layout-mobile #buttons {
    height: 130px; background-color: transparent; border-top: none; box-shadow: none;
    padding-bottom: env(safe-area-inset-bottom);
    position: fixed; bottom: -10px; left: 0; width: 100%;
    background-color: rgba(245, 245, 245, 0.98); border-top: 1px solid #ddd;
    z-index: 1000; display: flex; flex-direction: row; justify-content: center; align-items: center;
    gap: 0px; overflow: visible;
}
body.dark-mode.layout-mobile #buttons { background-color: transparent; border-top: none; }
body.layout-mobile #hit-button img, body.layout-mobile #stand-button img {
    width: 90%; height: 90%; max-width: none; max-height: none; object-fit: contain; transform: scale(1.2);
}
body.layout-mobile:not(.dark-mode) #hit-button img, body.layout-mobile:not(.dark-mode) #stand-button img {
    transform: scale(0.9);
}
body.layout-mobile #hit-button, body.layout-mobile #stand-button {
    width: 50%; height: 120px; margin: 0; display: flex; justify-content: center; align-items: center; position: relative;
}
body.layout-mobile #stand-button { order: 1; }
body.layout-mobile #hit-button { order: 2; }

/* Mobile Menu & Reset */
body.layout-mobile .bottom-nav-container {
    position: fixed; bottom: 95px; left: 15px; z-index: 1200;
}
body.layout-mobile .menu-btn {
    width: 45px; height: 45px; font-size: 1.2rem; opacity: 0.9;
    background-color: var(--modal-bg); color: var(--text-color);
    border: 1px solid #ccc; box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}
body.layout-mobile #reset-all-button {
    position: fixed; top: 15px; right: 15px; width: 35px; height: 35px; z-index: 1100;
    background: transparent; border: none; box-shadow: none; opacity: 0;
}
body.layout-mobile .theme-switch-wrapper { top: 15px; left: 15px; }
body.layout-mobile #end-game {
    /* 位置調整: 下の操作バー(約70-80px)の上に表示させる */
    bottom: 130px; /* 少し上げました
    left: 0;
    width: 100%;
    height: 80px; 
    
    z-index: 1010;
    display: flex;
    
    justify-content: center; /* flex-end (右寄せ) から center (中央) に変更 */
    align-items: center;
    
    padding-right: 0; /* 右の余白を削除 */
    padding-left: 0;  /* 左の余白も念のため0に */
    
    pointer-events: none; 
}

/* 念のため：コンテナのクリック無効化を解除してボタンを押せるようにする */
body.layout-mobile #end-game button,
body.layout-mobile #end-game .melon-btn {
    pointer-events: auto;
}

/* ボタン本体 */
body.layout-mobile #end-game button {
    pointer-events: auto; /* ボタン自体はクリック可能にする */
    height: 100%; /* 親の高さに合わせる */
    display: flex;
    align-items: center;
    justify_content: center;
}

/* 画像サイズ制限 */
body.layout-mobile #end-game img {
    width: auto;      /* 横幅はなりゆき */
    height: 100%;     /* 高さをコンテナ(80px)に合わせる */
    max-width: 80vw;  /* ただし画面幅の80%は超えない */
    object-fit: contain; /* 歪まずに収める */
    
    /* 影をつけて浮き上がらせる */
    filter: drop-shadow(0 4px 6px rgba(0,0,0,0.3)); 
}

/* ======================================== */
/*  Modals & Menus                          */
/* ======================================== */
.modal-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.6); z-index: 200;
    display: flex; justify-content: center; align-items: center;
}
.modal-content {
    background: var(--modal-bg); padding: 30px; border-radius: 10px;
    text-align: center; box-shadow: 0 10px 30px rgba(0,0,0,0.5);
    border: 1px solid #555; min-width: 300px;
}
.modal-buttons { display: flex; flex-direction: column; gap: 10px; margin-top: 20px; }
.modal-btn { padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer; font-size: 1rem; }
.save { background: #42518a; color: white; }
.reset { background: #644253; color: white; }
.cancel { background: #95a5a6; color: white; }

/* Auth Form Styles */
.auth-form-group { margin-bottom: 15px; text-align: left; }
.auth-form-group label { display: block; margin-bottom: 5px; font-size: 0.9rem; }
.auth-form-group input {
    width: 100%; padding: 8px; border-radius: 4px; border: 1px solid #ccc;
    box-sizing: border-box; background-color: var(--bg-color); color: var(--text-color);
}
.auth-error { color: red; font-size: 0.9rem; margin-bottom: 10px; display: none; }

/* Side Menu */
.bottom-nav-container { position: fixed; bottom: 20px; left: 20px; z-index: 100; }
.menu-btn { width: 50px; height: 50px; border-radius: 50%; background: #333; color: white; font-size: 1.5rem; border: none; cursor: pointer; box-shadow: 0 4px 10px rgba(0,0,0,0.3); }
.side-menu {
    position: fixed; top: 0; left: 0; width: 250px; height: 100%;
    background: var(--modal-bg); transform: translateX(-100%); transition: transform 0.3s;
    z-index: 300; padding: 20px; display: flex; flex-direction: column; border-right: 1px solid #555;
}
.side-menu.open { transform: translateX(0); }
.menu-header { display: flex; justify-content: space-between; margin-bottom: 20px; border-bottom: 1px solid #ccc; padding-bottom: 10px; }
.menu-list { list-style: none; padding: 0; }
.menu-list li { margin-bottom: 15px; }
.menu-list a { text-decoration: none; color: var(--text-color); font-weight: bold; display: block; padding: 10px; }
.menu-list a:hover { background: rgba(0,0,0,0.1); border-radius: 5px; }

/* Toggle Switch */
.theme-switch-wrapper { position: fixed; top: 20px; left: 20px; z-index: 100; }
.theme-switch { position: relative; display: inline-block; width: 60px; height: 34px; }
.theme-switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; transition: .4s; border-radius: 34px; }
.slider:before { position: absolute; content: ""; height: 26px; width: 26px; left: 4px; bottom: 4px; background-color: white; transition: .4s; border-radius: 50%; }
input:checked + .slider { background-color: #261909; }
input:checked + .slider:before { transform: translateX(26px); }

/* メロンゲーム遷移ボタン */
.melon-btn {
    background: linear-gradient(135deg, #a8e063 0%, #56ab2f 100%);
    border: 2px solid #fff;
    border-radius: 15px;
    padding: 15px 30px;
    color: white;
    font-weight: bold;
    font-size: 1.2rem;
    cursor: pointer;
    box-shadow: 0 0 15px #56ab2f, 0 5px 10px rgba(0,0,0,0.3);
    animation: pulse 2s infinite;
    text-shadow: 1px 1px 2px rgba(0,0,0,0.5);
}
.melon-btn:hover {
    transform: scale(1.05);
    background: linear-gradient(135deg, #b8f073 0%, #66bb3f 100%);
}
@keyframes pulse {
    0% { box-shadow: 0 0 0 0 rgba(86, 171, 47, 0.7); }
    70% { box-shadow: 0 0 0 15px rgba(86, 171, 47, 0); }
    100% { box-shadow: 0 0 0 0 rgba(86, 171, 47, 0); }
}

/* =========================================
   Mobile Layout Patch (Final Adjustments) スマホ画面専用
   ========================================= */

@media screen and (max-width: 768px) {

    /* 1. ダークモードスイッチ: 左上に配置 */
    .theme-switch-wrapper {
        top: 15px !important; 
        left: 15px !important; /* 左寄せ */
        right: auto !important; /* 右指定を解除 */
        
        /* サイズを70%に縮小 (左上を基準に縮める) */
        transform: scale(0.7);
        transform-origin: top left;
        
        z-index: 2000;
    }

    /* 2. リセットボタン(右上)との干渉を防ぐための調整 */
    /* (script.js等で動的に制御される場合があるため、念のためここで位置を保証) */
    #reset-all-button {
        top: 15px !important;
        right: 15px !important;
    }

    /* 3. メッセージログ: 左右のボタンを避けて中央上部に配置 */
    #message-log {
        position: fixed !important;
        top: 15px !important;
        left: 50% !important;
        transform: translateX(-50%) !important;
        
        /* スイッチとリセットボタンの間のスペースに収める */
        width: calc(100% - 100px) !important; 
        max-height: 80px;
        
        margin: 0 !important;
        padding: 8px !important;
        font-size: 0.85rem !important;
        
        /* 背景を少し濃くして文字を見やすく */
        background: rgba(255, 255, 255, 0.95);
        box-shadow: 0 2px 10px rgba(0,0,0,0.2);
        z-index: 1900;
    }
    
    /* ダークモード時のログ背景 */
    body.dark-mode #message-log {
        background: rgba(40, 40, 40, 0.95);
        border: 1px solid #555;
    }
    
    /* 4. ゲームコンテナ: 上部の余白を広げて、AIエリアがログに被らないようにする */
    #game-container {
        /* ログ(約80px) + 余白分を確保 */
        padding-top: 130px !important; 
        padding-bottom: 140px !important; /* 下のボタンエリア用 */
        
        display: flex;
        flex-direction: column;
        gap: 15px;
    }

    /* 5. AIエリアとプレイヤーエリアの微調整 */
    #ai-area, #player-area {
        width: 94% !important;
        margin: 0 auto !important; /* 中央寄せ */
    }
    
    /* 文字サイズを少し調整 */
    .area-header h2, .player-title-area h2 {
        font-size: 1.2rem;
        margin-bottom: 5px;
    }
}
