* {
    touch-action: pan-x pan-y;
    /* すべての要素でピンチズームを無効化、スクロールは有効 */
}

body {
    font-family: sans-serif;
    margin: 0;
    background-color: #f0f0f0;
    padding-top: 80px;
    /* 固定ヘッダー分のスペースを確保 */
    transition: background-color 0.3s ease, background-image 0.3s ease;
    /* グラデーションの変化も滑らかに */
    background-attachment: fixed;
    /* グラデーションを画面に固定 */
}

.hidden {
    display: none !important;
}

#start-screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    /* background-color: #f0f0f0; bodyの背景色を使うため削除 */
    overflow: hidden;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

#start-screen>h1,
#start-screen>button {
    position: relative;
    z-index: 2;
}

.main-logo {
    width: 500px;
    /* ロゴの幅を調整 (500px) */
    height: auto;
    margin-bottom: 25px;
}

#start-screen>button {
    margin-top: 10px;
}


#test-screen {
    width: 95%;
    max-width: 900px;
    background-color: white;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    margin: 20px auto;
}

/* ▼▼▼ ヘッダーのレイアウトを修正 ▼▼▼ */
#header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 20px;
    background-color: white;
    border-bottom: 1px solid #ccc;
    z-index: 1000;
    height: 50px;
    /* ヘッダーの高さを調整 (50px) */
}

.header-logo-button {
    background: none;
    border: none;
    padding: 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    /* 左寄せ */
}

.header-logo {
    height: 60px;
    /* ヘッダー内のロゴの高さ (60px) */
    width: auto;
}

#timer {
    flex-grow: 1;
    /* 中央の要素が柔軟にスペースを埋める */
    text-align: center;
    font-size: 1.5em;
    font-weight: bold;
    color: #333;
}

#difficulty-display {
    text-align: right;
    font-size: 1.2em;
    font-weight: bold;
}

/* ▲▲▲ ここまで修正 ▲▲▲ */

#test-container {
    margin-top: 10px;
}

#test-container table {
    width: 100%;
    border-collapse: collapse;
}

#test-container th,
#test-container td {
    border: 1px solid #ddd;
    padding: 8px;
    text-align: center;
    vertical-align: middle;
}

#test-container th {
    background-color: #a4c2f4;
}

.correct-row {
    background-color: #b7e1cd;
}

.incorrect-row {
    background-color: #efefef;
}

#finish-test-btn-container {
    text-align: center;
    margin-top: 20px;
}

#cancel-test-btn {
    background-color: #f44336;
    /* 赤色 */
    margin-left: 10px;
}

#summary {
    margin-top: 20px;
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 5px;
    background-color: #f9f9f9;
}

#pass-message {
    color: #2a8a2e;
    font-weight: bold;
    font-size: 1.1em;
    background-color: #e9f5e9;
    border: 1px solid #a3d9a5;
    border-radius: 5px;
    padding: 10px;
    margin-top: 0;
    margin-bottom: 15px;
    text-align: center;
}

button {
    padding: 10px 20px;
    font-size: 1em;
    cursor: pointer;
    border: none;
    border-radius: 5px;
    background-color: #4CAF50;
    color: white;
    transition: all 0.2s ease-in-out;
    /* アニメーションを滑らかに */
}

#start-normal-btn,
#start-hard-btn,
#start-very-hard-btn {
    margin-top: 10px;
}

#history-btn {
    background-color: #008CBA;
}

#start-screen>#history-btn {
    margin-top: 20px;
    /* スペースを20pxに調整 */
}



#history-screen {
    width: 95%;
    max-width: 1000px;
    background-color: white;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    margin: 20px auto;
}

#history-container table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 20px;
}

#history-container th,
#history-container td {
    border: 1px solid #ddd;
    padding: 8px;
    text-align: center;
}

#history-container th {
    background-color: #a4c2f4;
}

#back-to-start-from-history-btn {
    margin-top: 20px;
}

#delete-all-history-btn {
    background-color: #f44336;
    margin-left: 10px;
}

.delete-button {
    background: transparent;
    border: none;
    padding: 0;
    margin: 0;
    color: #f44336;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    vertical-align: middle;
}

.delete-button svg {
    width: 20px;
    height: 20px;
}


.detail-btn {
    background-color: #2196F3;
    padding: 5px 10px;
    font-size: 0.9em;
}

.delete-button:hover {
    opacity: 0.7;
}

#retry-btn {
    background-color: #8BC34A;
    margin-left: 10px;
}

#go-to-history-btn {
    background-color: #008CBA;
    margin-left: 10px;
}

button:hover:not(:disabled):not([data-disabled="true"]) {
    opacity: 0.8;
}

/* 挑戦ボタンの幅を統一 */
#start-easy-btn,
#start-normal-btn,
#start-hard-btn,
#start-very-hard-btn {
    width: 220px;
    box-sizing: border-box;
    /* paddingとborderを幅に含める */
}

/* Normal: 青銅の門番 */
#start-normal-btn {
    background: linear-gradient(145deg, #d4a276, #a27b53);
    border: 1px solid #8e6a44;
    font-weight: bold;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2), inset 0 1px 1px rgba(255, 255, 255, 0.2);
    text-shadow: 0 1px 1px rgba(0, 0, 0, 0.3);
}

#start-normal-btn:hover:not(:disabled):not([data-disabled="true"]) {
    background: linear-gradient(145deg, #e0ad81, #b28860);
    box-shadow: 0 6px 8px rgba(0, 0, 0, 0.25), inset 0 1px 1px rgba(255, 255, 255, 0.3);
    transform: translateY(-1px);
    opacity: 1;
    /* 汎用ホバーを上書き */
}

/* Hard: 白銀の騎士 */
#start-hard-btn {
    background: linear-gradient(145deg, #e8e8e8, #b0b0b0);
    border: 2px outset #c0c0c0;
    font-weight: bold;
    color: #222;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.25), inset 0 2px 3px rgba(255, 255, 255, 0.5);
    text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
}

#start-hard-btn:hover:not(:disabled):not([data-disabled="true"]) {
    background: linear-gradient(145deg, #f5f5f5, #bebebe);
    border-color: #d1d1d1;
    box-shadow: 0 6px 10px rgba(0, 0, 0, 0.3), inset 0 2px 3px rgba(255, 255, 255, 0.6);
    transform: translateY(-1px);
    opacity: 1;
    /* 汎用ホバーを上書き */
}

/* Very Hard: 炎獄のラスボス (シンプル版) */
#start-very-hard-btn {
    background: linear-gradient(145deg, #4d0000, #1a0000);
    border: 1px solid #c04000;
    font-weight: bold;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
    /* シンプルな文字の影 */
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
    /* シンプルな影 */
}

#start-very-hard-btn:hover:not(:disabled):not([data-disabled="true"]) {
    background: linear-gradient(145deg, #6b0000, #380000);
    transform: translateY(-1px);
    /* ホバー時の浮き上がりを他のボタンと統一 */
    box-shadow: 0 6px 10px rgba(0, 0, 0, 0.4);
    opacity: 1;
    /* 汎用ホバーを上書き */
}

.tooltip-icon {
    position: relative;
    display: inline-block;
    cursor: help;
    background-color: #ccc;
    color: white;
    border-radius: 50%;
    width: 16px;
    height: 16px;
    font-size: 12px;
    text-align: center;
    line-height: 16px;
    margin-left: 4px;
}

.tooltip-text {
    visibility: hidden;
    width: 220px;
    background-color: #555;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    padding: 5px 0;
    position: absolute;
    z-index: 1;
    bottom: 125%;
    left: 50%;
    margin-left: -110px;
    opacity: 0;
    transition: opacity 0.3s;
}

.tooltip-icon:hover .tooltip-text {
    visibility: visible;
    opacity: 1;
}

#history-container th .tooltip-icon {
    background-color: #888;
}

.medal {
    margin-left: 5px;
    font-size: 1.1em;
}


.bg-animation {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
}

.bg-shape {
    position: absolute;
    will-change: transform, opacity;
}

/* 難易度別 グラデーション背景設定 */
body.bg-normal {
    background-color: #d1c8c0;
    /* フォールバック */
    background-image: radial-gradient(circle, #e3d9d0, #d1c8c0);
}

body.bg-hard {
    background-color: #6a6a6a;
    /* フォールバック */
    background-image: linear-gradient(135deg, #9a9a9a, #5a5a5a);
    /* さらに暗いスチール調 */
}

body.bg-very-hard {
    background-color: #2c2525;
    /* フォールバック */
    background-image: radial-gradient(ellipse at bottom, #4d1a1a, #1a1a1a);
}

/* --- スマートフォン向けスタイル (メディアクエリ) --- */
@media screen and (max-width: 768px) {
    body {
        padding-top: 35px;
        /* スマートフォン用にヘッダーの高さを調整 */
    }

    .main-logo {
        width: 80%;
        /* 画面幅に合わせた相対的なサイズに */
        max-width: 350px;
        /* 最大幅は維持 */
    }

    #start-easy-btn,
    #start-normal-btn,
    #start-hard-btn,
    #start-very-hard-btn {
        width: 200px;
        /* ボタンの幅を少し小さく */
    }

    #test-screen,
    #history-screen {
        width: 100%;
        padding: 5px;
        margin: 0;
        border-radius: 0;
        box-shadow: none;
        box-sizing: border-box;
    }

    #header {
        height: 40px;
        /* ヘッダーの高さを調整 */
        padding: 5px 10px;
    }

    .header-logo {
        width: 120px;
        /* 幅を100pxに変更 */
        height: auto;
        /* 高さは自動 */
    }

    #timer {
        font-size: 1.1em;
    }

    #difficulty-display {
        min-width: 120px;
        /* 難易度表示幅も100pxに */
        width: 120px;
        text-align: right;
        font-size: 1em;
    }

    #test-container,
    #history-container {
        overflow-x: hidden;
        /* 横スクロールバーを非表示に（カードレイアウトでは不要） */
    }

    #test-container table {
        font-size: 0.8em;
    }

    #history-container table {
        font-size: 0.9em;
        line-height: 1.3;
    }

    #summary {
        padding: 15px;
    }

    button {
        padding: 12px 18px;
        /* ボタンのパディングを調整してタップしやすく */
    }

    #finish-test-btn-container button,
    #summary button {
        display: block;
        width: 80%;
        margin: 6px auto;
    }

    #finish-test-btn,
    #cancel-test-btn {
        font-size: 0.9em;
        padding: 6px 10px;
    }

    #back-to-start-btn,
    #retry-btn,
    #go-to-history-btn,
    #back-to-history-from-detail-btn {
        margin-left: auto;
        margin-right: auto;
    }

    #delete-all-history-btn {
        margin-left: auto;
    }

    /* ▼▼▼ スマホ版 テーブルレイアウトと列幅の再定義（px指定） ▼▼▼ */

    /* ▼▼▼ スマホ版 カードレイアウトの定義 ▼▼▼ */

    #test-container table,
    #test-container thead,
    #test-container tbody,
    #test-container th,
    #test-container td,
    #test-container tr,
    #history-container table,
    #history-container thead,
    #history-container tbody,
    #history-container th,
    #history-container td,
    #history-container tr {
        display: block;
    }

    #test-container thead tr,
    #history-container thead tr {
        position: absolute;
        top: -9999px;
        left: -9999px;
    }

    /* テーブルヘッダーを非表示にする（アクセシビリティのため残すが画面外へ） */
    #test-container th,
    #history-container th {
        display: none;
    }

    /* thを含む行（ヘッダー行）も非表示 */
    #test-container tr:has(th),
    #history-container tr:has(th) {
        display: none;
    }

    #test-container tr,
    #history-container tr {
        margin-bottom: 4px;
        border: 1px solid #ccc;
        border-radius: 8px;
        padding: 3px;
        background-color: #fff;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    }

    #test-container td,
    #history-container td {
        border: none;
        border-bottom: 1px solid #eee;
        position: relative;
        padding-left: 35%;
        /* ラベル分のスペース */
        padding-top: 3px;
        padding-bottom: 3px;
        text-align: left;
        min-height: 24px;
        display: flex;
        align-items: center;
        flex-wrap: wrap;
    }

    #test-container td:last-child,
    #history-container td:last-child {
        border-bottom: none;
    }

    /* 削除ボタンを含むセルの上下パディングを削除 */
    #history-container td:has(.delete-button) {
        padding-top: 0;
        padding-bottom: 0;
    }

    #history-container td:before {
        position: absolute;
        top: 10px;
        left: 10px;
        width: 30%;
        padding-right: 10px;
        white-space: nowrap;
        font-weight: bold;
        content: attr(data-label);
        color: #555;
    }

    /* 回答欄の調整 */
    #test-container select {
        width: 100%;
        height: 44px;
        /* タップしやすい高さ */
        font-size: 16px;
    }

    /* 履歴画面のボタン調整 */
    .detail-btn,
    .delete-button {
        width: 100%;
        padding: 10px;
        margin: 5px 0;
        justify-content: center;
    }

    .delete-button svg {
        width: 24px;
        height: 24px;
    }

    /* ▲▲▲ スマホ版 カードレイアウトの定義ここまで ▲▲▲ */
    .speed-col {
        display: none;
    }

    #history-screen {
        margin-top: 0 !important;
        padding-top: 10px !important;
    }

    #history-screen h1 {
        margin-top: 0.5em !important;
        margin-bottom: 0.5em !important;
    }

    #history-container table {
        width: 100%;
        border-collapse: collapse;
        margin-top: 0.5em;
    }

    body:has(#history-screen:not(.hidden)) {
        padding-top: 10px !important;
    }

    /* Single Question Mode Styles */
    .single-question-container {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 100%;
    }

    .question-progress {
        font-size: 1.2em;
        font-weight: bold;
        margin-top: 10px;
        margin-bottom: 8px;
        color: #555;
    }

    .question-card {
        background: white;
        border-radius: 12px;
        padding: 20px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        width: 100%;
        box-sizing: border-box;
    }

    .question-statements {
        background-color: #f0f8ff;
        padding: 10px;
        border-radius: 8px;
        margin-bottom: 12px;
        font-size: 1.5em;
        line-height: 1.4;
        text-align: center;
    }

    .question-statements p {
        margin: 3px 0;
    }

    .question-text {
        font-size: 1.2em;
        font-weight: bold;
        margin-bottom: 15px;
        text-align: center;
        color: #333;
    }

    .answer-options {
        display: flex;
        flex-direction: column;
        gap: 10px;
        margin-bottom: 20px;
    }

    .answer-option-btn {
        padding: 12px;
        font-size: 1.2em;
        background-color: #fff;
        border: 2px solid #2196F3;
        color: #2196F3;
        border-radius: 8px;
        cursor: pointer;
        transition: all 0.2s ease;
        font-weight: bold;
    }

    .answer-option-btn.selected {
        background-color: #2196F3;
        color: white;
    }

    .answer-option-btn:active {
        background-color: #1976D2;
        color: white;
        transform: scale(0.98);
    }

    .navigation-buttons {
        display: flex;
        justify-content: space-between;
        width: 100%;
        margin-top: 10px;
    }

    .nav-btn {
        padding: 10px 20px;
        font-size: 1.2em;
        background-color: #f0f0f0;
        border: 1px solid #ccc;
        border-radius: 8px;
        cursor: pointer;
        color: #333;
        min-width: 80px;
    }

    .nav-btn:disabled {
        opacity: 0.5;
        cursor: not-allowed;
    }

    /* Result Screen Summary Optimization */
    #summary {
        width: 90%;
        margin: 20px auto;
        padding: 25px;
        background: white;
        border-radius: 16px;
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
        box-sizing: border-box;
        text-align: center;
    }

    #summary h2 {
        font-size: 1.8em;
        margin-bottom: 15px;
        color: #333;
    }

    #score-display {
        font-size: 4em;
        font-weight: bold;
        color: #2196F3;
        display: block;
        margin: 15px 0;
    }

    #pass-message {
        font-size: 1.6em;
        margin: 20px 0;
        line-height: 1.6;
        color: #4CAF50;
        font-weight: bold;
    }

    #summary p {
        font-size: 1.4em;
        margin: 12px 0;
        color: #666;
    }

    #summary button {
        width: 100%;
        margin: 8px 0;
        padding: 15px;
        font-size: 1.2em;
        border-radius: 8px;
        font-weight: bold;
    }

    /* Result Table Card Optimization (Refined) */
    #test-container tr {
        display: flex;
        flex-direction: column;
        gap: 3px;
        margin-bottom: 8px;
        background: white;
        border-radius: 10px;
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.12);
        padding: 6px;
        padding-top: 28px;
        /* Space for absolute header */
        border: 1px solid #ddd;
        position: relative;
        width: 100%;
        box-sizing: border-box;
        max-width: 100vw;
    }

    #test-container td {
        display: block;
        border: none;
        padding: 0;
        width: 100%;
        box-sizing: border-box;
        font-size: 1.3em;
        color: #222;
        line-height: 1.4;
    }

    /* --- Header Section (Number & Result) --- */

    /* 1. Number (#) */
    #test-container td[data-label="#"] {
        order: 1;
        font-weight: 900;
        color: #000;
        font-size: 1.2em;
        line-height: 1;
        position: absolute;
        top: 8px;
        left: 8px;
        z-index: 2;
    }

    /* 2. Result (採点結果) */
    #test-container td[data-label="採点結果"] {
        order: 2;
        position: absolute;
        top: 8px;
        right: 8px;
        text-align: right;
        font-size: 1.5em;
        font-weight: bold;
        line-height: 1;
        z-index: 2;
    }

    /* --- Content Section --- */

    /* Combined Statement (比較文) - for mobile */
    #test-container td[data-label="比較文"] {
        order: 3;
        background: #f8f8f8;
        padding: 4px 6px;
        border-radius: 8px;
        margin-bottom: 3px;
        line-height: 1.4;
        font-size: 1.1em;
    }

    /* Separate Statements (比較文1, 比較文2) - for PC fallback */
    #test-container td[data-label="比較文1"] {
        order: 3;
        background: #f8f8f8;
        padding: 4px 6px;
        border-radius: 6px 6px 0 0;
        margin-top: 0;
        line-height: 1.3;
        font-size: 1.1em;
    }

    #test-container td[data-label="比較文2"] {
        order: 4;
        background: #f8f8f8;
        padding: 4px 6px;
        padding-top: 2px;
        border-radius: 0 0 6px 6px;
        margin-bottom: 3px;
        line-height: 1.3;
        font-size: 1.1em;
    }

    /* 4. Question (質問文) - hidden on mobile when combined */
    #test-container td[data-label="質問文"] {
        order: 5;
        font-weight: bold;
        color: #000;
        margin: 0 0 3px 0;
        text-align: center;
        line-height: 1.2;
        font-size: 1.2em;
    }

    /* Hide質問文 when empty (mobile combined mode) */
    #test-container td[data-label="質問文"]:empty,
    #test-container td[data-label="質問文"][style*="display:none"] {
        display: none !important;
        margin: 0;
    }

    /* --- Footer Section (Answers) --- */

    #test-container td[data-label="回答"],
    #test-container td[data-label="正答"] {
        width: 48%;
        box-sizing: border-box;
    }

    #test-container tr {
        flex-flow: row wrap;
        justify-content: space-between;
    }

    #test-container td[data-label="#"],
    #test-container td[data-label="採点結果"],
    #test-container td[data-label="比較文"],
    #test-container td[data-label="比較文1"],
    #test-container td[data-label="比較文2"],
    #test-container td[data-label="質問文"] {
        width: 100%;
    }

    #test-container td[data-label="回答"] {
        order: 6;
        width: 48%;
        background: #e3f2fd;
        border: 1px solid #90caf9;
        padding: 6px;
        padding-left: 90px;
        border-radius: 6px;
        text-align: center;
        margin-top: auto;
        font-size: 1.1em;
    }

    #test-container td[data-label="正答"] {
        order: 7;
        width: 48%;
        background: #e8f5e9;
        border: 1px solid #a5d6a7;
        padding: 6px;
        padding-left: 45px;
        border-radius: 6px;
        text-align: center;
        font-weight: bold;
        color: #2e7d32;
        margin-top: auto;
        font-size: 1.1em;
    }

    /* Labels for Answers */
    #test-container td[data-label="回答"]:before {
        content: "あなたの回答";
        position: absolute;
        left: 6px;
        top: 50%;
        transform: translateY(-50%);
        width: 75px;
        font-size: 0.65em;
        white-space: nowrap;
    }

    #test-container td[data-label="正答"]:before {
        content: "正答";
        position: absolute;
        left: 6px;
        top: 50%;
        transform: translateY(-50%);
        width: 30px;
        font-size: 0.65em;
        white-space: nowrap;
    }

    /* Hide other labels */
    #test-container td[data-label="#"]:before,
    #test-container td[data-label="採点結果"]:before,
    #test-container td[data-label="比較文"]:before,
    #test-container td[data-label="比較文1"]:before,
    #test-container td[data-label="比較文2"]:before,
    #test-container td[data-label="質問文"]:before {
        display: none;
    }

    /* Mobile: Color styling for correct/incorrect cards */
    .correct-row {
        background: #e8f5e9 !important;
        /* Light green background */
        border-left: 4px solid #4CAF50 !important;
    }

    .incorrect-row {
        background: #ffebee !important;
        /* Light red background */
        border-left: 4px solid #f44336 !important;
    }

    .correct-row td[data-label="採点結果"] {
        color: #2e7d32 !important;
        /* Dark green for ○ */
        font-weight: bold;
    }

    .incorrect-row td[data-label="採点結果"] {
        color: #c62828 !important;
        /* Dark red for × */
        font-weight: bold;
    }
}

/* ▼▼▼ 非アクティブな難易度ボタンの表示を強制的に統一するスタイル ▼▼▼ */
#start-normal-btn[data-disabled="true"],
#start-hard-btn[data-disabled="true"],
#start-very-hard-btn[data-disabled="true"] {
    background: #cccccc !important;
    /* 背景を単色グレーで上書き */
    border: 1px solid #b0b0b0 !important;
    /* 枠線を統一 */
    box-shadow: none !important;
    /* 影を完全に削除 */
    text-shadow: none !important;
    /* 文字の影も完全に削除 */
    color: #777 !important;
    /* 文字色もグレー系に統一 */
    cursor: not-allowed;
    opacity: 0.6;
}

/* ▲▲▲ ここまで ▲▲▲ */

/* PC表示用のスタイル */
@media screen and (min-width: 769px) {

    #timer br,
    #difficulty-display br {
        display: none;
    }

    body:has(#history-screen:not(.hidden)) {
        padding-top: 20px !important;
    }
}