:root {
    /* --- Hacker/Terminal Theme --- */
    --bg-color: #000000;
    --primary-text-color: #00ff00;
    --primary-accent-color: #00ff00;
    --container-bg-color: rgba(0, 10, 0, 0.5); /* Semi-transparent dark green */
    --panel-bg-color: rgba(0, 255, 0, 0.05);
    --panel-border-color: rgba(0, 255, 0, 0.4); /* Grid line color */
    --panel-header-color: #00cc00;
    --dpad-button-bg: rgba(0, 255, 0, 0.1);
    --dpad-button-active-bg: rgba(0, 255, 0, 0.25);
    --center-button-bg: rgba(0, 255, 0, 0.15);
    --center-button-active-bg: rgba(0, 255, 0, 0.3);
    --a-button-bg: rgba(0, 255, 0, 0.15);
    --a-button-active-bg: rgba(0, 255, 0, 0.3);
    --b-button-bg: rgba(0, 255, 0, 0.15);
    --b-button-active-bg: rgba(0, 255, 0, 0.3);
}

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

body {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 100vh; /* ビューポートの高さに合わせて中央寄せ */
    margin: 0;
    /* --- 背景をグリッド線に変更 --- */
    background-color: var(--bg-color);
    background-image: 
        linear-gradient(rgba(0, 255, 0, 0.2) 1px, transparent 1px),
        linear-gradient(90deg, rgba(0, 255, 0, 0.2) 1px, transparent 1px);
    background-size: 25px 25px; /* グリッドのサイズを調整 */
    /* --- ここまで --- */
    color: var(--primary-text-color);
    text-shadow: 0 0 3px rgba(0, 255, 0, 0.7); /* 全てのテキストに基本的なグロー効果 */
    font-family: 'Press Start 2P', cursive;
    padding: 10px;
    overflow: hidden; /* スクロールを禁止して、キー操作による画面揺れを防ぐ */
}

h1 {
    color: var(--primary-accent-color);
    margin-bottom: 15px;
    font-size: 1.6em;
    text-align: center;
    line-height: 1.2; /* 行の高さを調整 */
    text-shadow: 0 0 8px var(--primary-accent-color); /* 見出しのグローを強調 */
}

/* ボタン要素はデフォルトでフォントを継承しない場合があるため明示的に指定 */
button {
    font-family: inherit;
}

/* ゲーム全体を囲むコンテナ */
.game-container {
    display: flex;
    flex-direction: column; /* 縦方向に要素を並べる */
    align-items: center; /* 子要素を中央に配置 */
    width: 100%;
    max-width: 500px; /* PCでの最大幅を制限 */
    box-shadow: 0 0 15px rgba(0, 255, 0, 0.3); /* 全体に影をつける */
    border-radius: 10px;
    /* --- 背景画像の追加 (ウォッシュアウト効果) --- */
    /* 背景は2層で構成されています:
       1. 上層: 半透明の黒いグラデーションを重ねて「ウォッシュアウト」効果（画像を暗くする）を実現します。
       2. 下層: 実際の背景画像。
       この方法により、`background-blend-mode` に起因する緑色の着色を防ぎ、黒い背景に画像が浮かび上がるような見た目になります。
    */
    background: 
        /* この行の最後の数値を 1 に近づけるほど、背景が黒くなります (例: 0.85) */
        linear-gradient(rgba(0, 0, 0, 0.85), rgba(0, 0, 0, 0.85)),
        url('jpg/background.png');
    background-size: cover;
    background-position: center;
    /* --- ここまで --- */
    padding: 15px;
    border: 1px solid var(--panel-border-color);
}

/* ゲーム本体（HOLD、メインキャンバス、NEXT） */
.game-area {
    display: flex;
    align-items: flex-start; /* 上端を揃える */
    gap: 10px;
    width: 100%;
    justify-content: center; /* 中央寄せ */
    margin-bottom: 20px; /* 操作ボタンとの間隔 */
}

canvas#tetrisCanvas {
    border: 2px solid #ffffff; /* 白色に変更 */
    background-color: rgba(0, 0, 0, 0.4); /* 背景を半透明の黒に */
    box-shadow: inset 0 0 10px rgba(0, 255, 0, 0.5), 0 0 10px rgba(255, 255, 255, 0.5); /* 外側のグローを白に変更 */
    display: block;
    width: 200px; /* 基準サイズを固定 */
    height: 400px; /* 基準サイズを固定 */
    flex-shrink: 0;
}

/* サイドバー（HOLD, NEXT） */
.sidebar-left,
.sidebar-right {
    display: flex;
    flex-direction: column; /* 縦並び */
    gap: 10px;
    width: 90px; /* サイドバーの幅を固定 */
    flex-shrink: 0; /* 縮まないようにする */
    height: 404px; /* tetrisCanvasの高さ(400px) + 上下ボーダー(2px*2)に合わせる */
}

.panel#holdPanel {
    margin-top: auto; /* SCORE/LEVELとHOLD/NEXTの間にスペースを空ける */
}

.panel {
    background-color: var(--panel-bg-color);
    border: 1px solid var(--panel-border-color);
    border-radius: 8px;
    padding: 8px;
    text-align: center;
    box-shadow: inset 0 0 8px rgba(0, 255, 0, 0.3);
    color: var(--primary-text-color); /* パネル内の文字色 */
}

.panel.info-only {
    background: none;
    border: none;
    box-shadow: none;
    padding: 0;
}

.panel.info-only h2 {
    font-size: 0.6em;
    margin-bottom: 2px;
}

.panel.info-only p {
    font-size: 0.9em;
    font-weight: normal;
}

.panel h2 {
    margin-top: 0;
    margin-bottom: 5px;
    color: #ffffff; /* 白色に変更 */
    font-size: 0.8em; /* 小さな表示用 */
    text-shadow: 0 0 5px rgba(255, 255, 255, 0.7); /* 白色のグロー効果を追加 */
}

.panel p {
    font-size: 1.2em; /* スコア・レベルのフォントサイズ */
    font-weight: bold;
    color: var(--primary-text-color);
    margin: 0;
}

.small-panel {
    padding: 5px;
}

canvas#nextCanvas,
canvas#holdCanvas {
    background-color: var(--container-bg-color);
    border: 1px solid var(--panel-border-color);
    display: block;
    margin: 0 auto;
    width: 80px; /* NEXT/HOLDキャンバスの幅を固定 */
    height: 80px; /* NEXT/HOLDキャンバスの高さを固定 */
}

.controller-container {
    display: flex; /* D-Padとアクションボタンを横並びにする */
    gap: 25px; /* D-Padとアクションボタン間のスペース */
    background-color: rgba(0, 0, 0, 0.3);
    padding: 25px;
    border-radius: 45px;
    box-shadow: 0 5px 15px rgba(0, 255, 0, 0.2);
    border: 1px solid var(--panel-border-color);
    align-items: center; /* 縦方向の中央揃え */
}

.dpad-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: 3px; /* ボタン間の隙間をさらに小さく */
    width: 135px; /* D-Pad全体のサイズを小さく */
    height: 135px; /* D-Pad全体のサイズを小さく */
    position: relative;
}

.dpad-button {
    /* width/heightはグリッドによって自動計算される */
    background-color: var(--dpad-button-bg);
    color: var(--primary-text-color);
    font-size: 1.2em; /* フォントサイズも調整 */
    font-weight: bold;
    border: none;
    border-radius: 10px; /* 角丸も調整 */
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;    
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.5), inset 0 0 5px rgba(0, 255, 0, 0.2);
    transition: background-color 0.1s ease, transform 0.1s ease, box-shadow 0.1s ease;
    -webkit-tap-highlight-color: transparent;
}

.dpad-button:active,
.dpad-button.active {
    background-color: var(--dpad-button-active-bg);
    transform: translateY(1px); /* 沈み込みも小さく */
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5), inset 0 0 10px rgba(0, 255, 0, 0.4);
}

/* グリッド内でのボタン配置 (D-Pad) */
.dpad-button.up {
    grid-column: 2;
    grid-row: 1;
    border-radius: 10px 10px 3px 3px; /* 上だけ丸く */
    color: #ffffff; /* 白色に変更 */
    text-shadow: 0 0 5px rgba(255, 255, 255, 0.7); /* 白色のグロー効果を追加 */
}

.dpad-button.left {
    grid-column: 1;
    grid-row: 2;
    border-radius: 10px 3px 3px 10px; /* 左だけ丸く */
    color: #ffffff; /* 白色に変更 */
    text-shadow: 0 0 5px rgba(255, 255, 255, 0.7); /* 白色のグロー効果を追加 */
}

.dpad-button.center {
    grid-column: 2;
    grid-row: 2;
    background-color: var(--center-button-bg);
    border-radius: 50%;
    font-size: 1.2em;
}

.dpad-button.center:active,
.dpad-button.center.active {
    background-color: var(--center-button-active-bg);
}

.dpad-button.right {
    grid-column: 3;
    grid-row: 2;
    border-radius: 3px 10px 10px 3px; /* 右だけ丸く */
    color: #ffffff; /* 白色に変更 */
    text-shadow: 0 0 5px rgba(255, 255, 255, 0.7); /* 白色のグロー効果を追加 */
}

.dpad-button.down {
    grid-column: 2;
    grid-row: 3;
    border-radius: 3px 3px 10px 10px; /* 下だけ丸く */
    color: #ffffff; /* 白色に変更 */
    text-shadow: 0 0 5px rgba(255, 255, 255, 0.7); /* 白色のグロー効果を追加 */
}

/* --- アクションボタンのスタイル --- */
.action-buttons-section {
    display: flex; /* ボタンを横並びにする */
    flex-direction: row; /* 横並びにするために explicit に指定 (デフォルトですが念のため) */
    gap: 18px; /* ボタン間のスペース */
    align-items: center; /* 縦方向の中央揃え */
    margin-right: 10px; /* 全体的な右端の余白 */
    /* BボタンとAボタンの表示順を入れ替える場合は、HTML側で記述順をA, Bにすればよい */
    /* もしくは CSS で order プロパティを使うことも可能 */
}

.action-button {
    width: 55px; /* アクションボタンのサイズ */
    height: 55px;
    border-radius: 50%; /* 完全な円形 */
    font-size: 1.5em;
    font-weight: bold;
    color: #fff;
    border: none;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;    
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.5), inset 0 0 8px rgba(0, 255, 0, 0.3);
    transition: background-color 0.1s ease, transform 0.1s ease, box-shadow 0.1s ease;
    -webkit-tap-highlight-color: transparent;
}

.action-button:active,
.action-button.active {
    transform: translateY(2px);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5), inset 0 0 12px rgba(0, 255, 0, 0.5);
}

.b-button {
    background-color: var(--b-button-bg); /* オレンジ系の色 */
}

.b-button:active,
.b-button.active {
    background-color: var(--b-button-active-bg);
}

.a-button {
    background-color: var(--a-button-bg); /* 緑系の色 */
}

.a-button:active,
.a-button.active {
    background-color: var(--a-button-active-bg);
}

/* ==========================================================================
   スマートフォン向けレイアウト調整 (特に縦が短いデバイス)
   ========================================================================== */
@media (max-width: 500px), (max-height: 740px) {
    body {
        padding: 5px;
        justify-content: flex-start; /* 上寄せにして、上部が見切れるのを防ぐ */
    }

    h1 {
        font-size: 1.3em;
        margin-top: 10px;
        margin-bottom: 10px;
    }

    .game-container {
        padding: 10px;
        width: 100%; /* 画面幅いっぱいに広げる */
    }

    .game-area {
        gap: 5px;
        margin-bottom: 15px;
    }

    /* メインキャンバスを縮小 */
    canvas#tetrisCanvas {
        width: 150px;  /* 200px -> 150px */
        height: 300px; /* 400px -> 300px */
    }

    /* サイドバーとパネルを縮小 */
    .sidebar-right {
        width: 70px; /* 90px -> 70px */
        height: 304px; /* canvas(300px) + border(4px) に合わせる */
    }

    canvas#nextCanvas,
    canvas#holdCanvas {
        width: 60px;  /* 80px -> 60px */
        height: 60px; /* 80px -> 60px */
    }

    .panel h2 {
        font-size: 0.6em;
    }
    .panel p {
        font-size: 1.1em;
    }

    /* コントローラーを縮小 */
    .controller-container {
        padding: 15px;
        gap: 20px;
        border-radius: 35px;
    }

    .dpad-grid {
        width: 120px;
        height: 120px;
    }

    .dpad-button {
        width: 38px;
        height: 38px;
        font-size: 1.1em;
        border-radius: 10px;
    }
    
    /* D-Padの角丸を再調整 */
    .dpad-button.up { border-radius: 10px 10px 3px 3px; }
    .dpad-button.left { border-radius: 10px 3px 3px 10px; }
    .dpad-button.right { border-radius: 3px 10px 10px 3px; }
    .dpad-button.down { border-radius: 3px 3px 10px 10px; }


    .action-buttons-section {
        gap: 15px;
    }

    .action-button {
        width: 50px;
        height: 50px;
        font-size: 1.3em;
    }
}

/* Game Over Overlay */
.overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.75);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100; /* Make sure it's on top */
}

.overlay.hidden {
    display: none;
}

.overlay-content {
    background-color: var(--container-bg-color);
    padding: 30px 40px;
    border-radius: 15px;
    text-align: center;    
    box-shadow: 0 0 25px rgba(0, 255, 0, 0.5), inset 0 0 15px rgba(0, 255, 0, 0.3);
    border: 2px solid var(--primary-accent-color);
}

.overlay-content h2 {
    color: var(--primary-accent-color);
    font-size: 2.2em;
    margin-top: 0;
    margin-bottom: 15px;
    text-shadow: 0 0 10px var(--primary-accent-color), 0 0 20px var(--primary-accent-color);
}

#pauseScreen h2 {
    color: var(--primary-accent-color);
}

.overlay-content p {
    font-size: 0.9em;
    line-height: 1.5; /* 行間を調整 */
    margin-bottom: 25px;
}

#retryButton,
#startButton,
#backButton,
#rankingBackButton {
    background-color: var(--a-button-bg);
    color: white;
    border: none;
    padding: 12px 25px;
    font-size: 1em;
    border-radius: 8px;
    cursor: pointer;
    box-shadow: 0 0 10px rgba(0, 255, 0, 0.5);
    transition: background-color 0.2s ease;
}

#retryButton:hover,
#startButton:hover,
#backButton:hover,
#rankingBackButton:hover {
    background-color: var(--a-button-active-bg);
}

/* --- Hamburger Menu & Side Panel --- */

.hamburger-button {
    position: fixed;
    top: 20px;
    right: 20px;
    width: 40px;
    height: 30px;
    background: transparent;
    border: none;
    cursor: pointer;
    z-index: 1002; /* Overlay is 100, menu is 1001 */
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

.hamburger-button span {
    width: 100%;
    height: 4px;
    background-color: var(--primary-accent-color);
    border-radius: 2px;
    transition: all 0.3s ease-in-out;
    box-shadow: 0 0 5px var(--primary-accent-color);
}

/* Animate hamburger to an "X" when active */
.hamburger-button.active span:nth-child(1) {
    transform: rotate(45deg) translate(8px, 8px);
}

.hamburger-button.active span:nth-child(2) {
    opacity: 0;
}

.hamburger-button.active span:nth-child(3) {
    transform: rotate(-45deg) translate(8px, -8px);
}


.side-menu {
    position: fixed;
    top: 0;
    right: 0;
    width: 250px;
    height: 100%;
    background-color: rgba(0, 15, 0, 0.9);
    border-left: 2px solid var(--panel-border-color);
    box-shadow: -5px 0 15px rgba(0, 255, 0, 0.3);
    z-index: 1001; /* Below hamburger, above overlay */
    
    /* Initially hidden off-screen */
    transform: translateX(100%);
    transition: transform 0.4s cubic-bezier(0.23, 1, 0.32, 1); /* Smooth animation */
    
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.side-menu.active {
    transform: translateX(0);
}

.menu-nav {
    display: flex;
    flex-direction: column;
    gap: 25px;
    width: 80%;
}

.menu-item {
    background: none;
    border: 1px solid var(--primary-accent-color);
    color: var(--primary-accent-color);
    padding: 15px 0;
    font-family: 'Press Start 2P', cursive;
    font-size: 1em;
    text-align: center;
    cursor: pointer;
    transition: background-color 0.2s, color 0.2s;
    text-decoration: none; /* Remove underline from links */
    text-shadow: 0 0 5px var(--primary-accent-color);
}

.menu-item:hover {
    background-color: var(--primary-accent-color);
    color: var(--bg-color);
    text-shadow: none;
}

/* --- Options Screen --- */
.options-container {
    display: flex;
    flex-direction: column;
    gap: 20px;
    margin: 30px 0;
    align-items: stretch; /* Stretch items to fill width */
    width: 250px; /* Give it a fixed width */
    font-size: 1em; /* Adjust font size for options */
}

.option-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.toggle-switch {
    position: relative;
    display: inline-block;
    width: 60px;
    height: 34px;
    flex-shrink: 0; /* Prevent shrinking */
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #333;
    border: 1px solid var(--panel-border-color);
    transition: .4s;
    border-radius: 34px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 26px;
    width: 26px;
    left: 4px;
    bottom: 3px;
    background-color: var(--primary-text-color);
    box-shadow: 0 0 5px var(--primary-text-color);
    transition: .4s;
    border-radius: 50%;
}

input:checked + .slider {
    background-color: var(--primary-accent-color);
}

input:checked + .slider:before {
    transform: translateX(26px);
    background-color: var(--bg-color);
    box-shadow: none;
}

/* --- Ranking Screen --- */
.ranking-container {
    width: 300px;
    max-height: 60vh;
    overflow-y: auto;
    margin: 20px 0;
    border: 1px solid var(--panel-border-color);
    padding: 10px;
    background: rgba(0,0,0,0.2);
}

#highScoreList {
    list-style-type: none;
    padding: 0;
    margin: 0;
    font-size: 0.9em;
}

#highScoreList li {
    display: flex;
    justify-content: space-between;
    padding: 8px 5px;
    border-bottom: 1px dashed var(--panel-border-color);
}

#highScoreList li:last-child {
    border-bottom: none;
}

#highScoreList .rank {
    flex-basis: 15%;
    flex-shrink: 0;
    text-align: right;
    padding-right: 10px;
}

#highScoreList .name {
    flex-basis: 50%;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

#highScoreList .score {
    flex-basis: 35%;
    text-align: right;
}