* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    display: flex;
    justify-content: center;
    align-items: flex-start; /* 上揃えに変更 */
    min-height: 100vh;
    padding: 10px 0; /* 上下に余白を追加 */
    box-sizing: border-box;
}

.container {
    width: 100%;
    max-width: 1000px;
    margin: 0 auto;
    background: white;
    border-radius: 20px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: column;
    min-height: calc(100vh - 20px); /* 上下のpadding分を引く */
    padding-bottom: 70px; /* 下部広告の基本余白 */
}

.title-container {
    display: flex;
    justify-content: center;
    align-items: center;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 6px;
    flex-shrink: 0;
    position: relative;
}

h1 {
    text-align: center;
    margin: 0;
    font-size: 1.2rem;
    font-weight: 700;
}

.help-button {
    background: none;
    border: 2px solid white;
    color: white;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    font-size: 1.1rem;
    font-weight: bold;
    cursor: pointer;
    position: absolute;
    right: 15px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    justify-content: center;
    align-items: center;
    padding-bottom: 2px; /* 微調整 */
}

.settings-wrapper {
    padding: 0 15px;
    background: #f1f3f5;
    flex-shrink: 0;
}

#settings-toggle-btn {
    width: 100%;
    padding: 8px 10px;
    background: none;
    border: none;
    font-size: 1rem;
    font-weight: 600;
    color: #495057;
    cursor: pointer;
    text-align: left;
}

.settings {
    padding: 8px 0 10px;
    background: #f1f3f5;
    border-top: 1px solid #dee2e6;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

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

.range-inputs {
    display: flex;
    align-items: center;
    gap: 5px;
}

.settings label {
    font-weight: 600;
    color: #495057;
}

.settings select,
.settings input {
    padding: 8px 12px;
    border-radius: 6px;
    border: 1px solid #ced4da;
    font-size: 1rem;
}

.settings input[type="number"] {
    width: 70px;
}

.game-area {
    padding: 10px;
    display: flex;
    flex-direction: column;
    flex-grow: 1; /* .container内で残りの高さを埋める */
    gap: 10px;
}

.display-area {
    flex-shrink: 0;
    min-height: 165px; /* タブの高さ(約40px) + コンテンツの最大高さ(125px) */
}

.display-tabs {
    display: flex;
    margin-bottom: -1px;
}

.tab-btn {
    flex: 1;
    padding: 8px;
    font-size: 1rem;
    font-weight: 600;
    border: 1px solid #dee2e6;
    border-bottom: none;
    background: #f8f9fa;
    cursor: pointer;
    border-radius: 8px 8px 0 0;
    color: #495057;
}

.tab-btn.active {
    background: white;
    border-bottom: 1px solid white;
    color: #000;
}

.tab-btn:disabled {
    background: #e9ecef;
    color: #adb5bd;
    cursor: not-allowed;
}

.tab-content {
    display: none;
    border: 1px solid #dee2e6;
    border-radius: 0 0 8px 8px;
    padding: 4px;
    max-height: 125px;
    overflow-y: auto;
}

.tab-content.active {
    display: block;
}

.tiles-container {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 4px;
    background: #f8f9fa;
    border-radius: 8px;
    padding: 10px;
}

.tile {
    width: clamp(28px, 7vw, 45px);
    height: auto;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    transition: transform 0.2s;
    vertical-align: middle;
}

.tile:hover {
    transform: translateY(-3px);
    box-shadow: 0 5px 10px rgba(0, 0, 0, 0.3);
}

.main-content {
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-items: center;
    flex-grow: 1;
    justify-content: center; /* 要素を上下に配置 */
}

.question-area {
    width: 100%;
}

.question-area h3 {
    color: #333;
    margin-bottom: 10px;
    font-size: 1.1rem;
    text-align: center;
    font-weight: bold;
    padding: 8px;
    border-radius: 8px;
    min-height: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid transparent;
    transition: all 0.3s;
}

#question-text.correct {
    background: #d1e7dd;
    color: #0f5132;
    border-color: #badbcc;
}

#question-text.incorrect {
    background: #f8d7da;
    color: #721c24;
    border-color: #f5c2c7;
}

#question-text.answer {
    background: #fff3cd;
    color: #664d03;
    border-color: #ffecb5;
}

.calculator {
    background: #f8f9fa;
    border-radius: 15px;
    padding: 8px;
    margin: 0 auto;
    max-width: 280px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.display {
    background: #333;
    color: white;
    font-size: 1.4rem;
    font-weight: bold;
    text-align: right;
    padding: 8px 12px;
    border-radius: 10px;
    margin-bottom: 8px;
    min-height: 48px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
}

.button-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
}

.calc-btn {
    background: white;
    border: 1px solid #dee2e6;
    border-radius: 8px;
    padding: 8px;
    font-size: 1rem;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.2s;
    min-height: 40px;
}

.calc-btn:hover {
    background: #f1f3f5;
}

.action-buttons {
    display: flex;
    gap: 8px;
    justify-content: center;
    width: 100%;
    margin-bottom: 0; /* ボタン下の余白を追加 */
}

.action-btn {
    padding: 8px;
    font-size: 1rem;
    font-weight: bold;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s;
    flex: 1;
    max-width: 140px;
}

.action-btn.submit {
    background: #198754;
    color: white;
}

.action-btn.giveup {
    background: #ffc107;
    color: #333;
}

.action-btn.next {
    background: #0d6efd;
    color: white;
}

.ukeire-tile-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin: 4px;
}

.ukeire-count {
    margin-top: 4px;
    font-size: 0.9rem;
    font-weight: bold;
    color: #333;
}

.loader-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(255, 255, 255, 0.8);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

.loader {
    border: 8px solid #f3f3f3;
    border-top: 8px solid #667eea;
    border-radius: 50%;
    width: 60px;
    height: 60px;
    animation: spin 1s linear infinite;
}

.loader-overlay p {
    margin-top: 20px;
    font-size: 1.2rem;
    font-weight: 600;
    color: #333;
}

.loader-overlay .action-btn {
    margin-top: 20px;
    flex: 0 0 auto; /* Do not grow or shrink */
    width: 120px;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* 広告コンテナの基本スタイル */
.ad-container-side, .ad-container-bottom {
    background: #f0f0f0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #888;
    font-size: 14px;
    overflow: hidden;
}

/* PC用サイド広告 */
.ad-container-side {
    position: fixed;
    top: 0;
    width: 160px; /* 広告の幅に合わせて調整 */
    height: 100%;
    z-index: 10;
    display: none; /* デフォルトでは非表示 */
}

.ad-left {
    left: 0;
}

.ad-right {
    right: 0;
}

/* モバイル用下部広告 */
.ad-container-bottom {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 50px; /* 広告の高さに合わせて調整 */
    z-index: 10;
    border-top: 1px solid #ddd;
}

@media (max-width: 600px) {
    body {
        padding: 0; /* モバイルではbodyのpaddingをなくす */
        align-items: flex-start;
    }
    .container {
        border-radius: 0;
        width: 100%;
        min-height: 100vh;
        padding-bottom: 90px; /* 広告領域 + 少し余白 (モバイル用に上書き) */
        box-sizing: border-box;
        overflow-y: auto; /* コンテナ内をスクロール可能に */
    }
    h1 {
        font-size: 1.2rem;
        padding: 6px;
    }
    .game-area {
        padding: 10px;
        justify-content: flex-start; /* Align items to the top */
    }
    .main-content {
        justify-content: flex-start;
        gap: 10px;
        flex-grow: 0; /* Don't grow to fill space */
    }
    .calculator {
        padding: 10px;
    }
    .tile {
        width: clamp(30px, 8vw, 48px); /* Make tiles slightly larger on mobile */
    }
    .ukeire-tile-wrapper .tile {
        width: clamp(36px, 9vw, 52px); /* Make ukeire tiles even larger */
    }
    .ad-container-side {
        display: none !important; /* モバイルではサイド広告を強制的に非表示 */
    }
    .credits {
        text-align: center;
    }

    .credit-item a {
        flex-direction: column;
        gap: 5px;
    }

    .credit-item .icon {
        margin-right: 0;
    }
}

/* PC表示用のメディアクエリ */
@media (min-width: 1300px) {
    .ad-container-side {
        display: flex; /* PCではサイド広告を表示 */
    }
    .ad-container-bottom {
        display: none; /* PCでは下部広告を非表示 */
    }
    .container {
        margin: 0 170px; /* サイド広告の幅分マージンをとる */
        padding-bottom: 0; /* PCでは下部パディングをリセット */
    }
}

/* トグルスイッチ */
.toggle-switch-container {
    display: flex;
    align-items: center;
}

.toggle-switch {
    position: relative;
    display: inline-block;
    width: 50px;
    height: 28px;
}

.toggle-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: 28px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 20px;
    width: 20px;
    left: 4px;
    bottom: 4px;
    background-color: white;
    transition: .4s;
    border-radius: 50%;
}

input:checked + .slider {
    background-color: #667eea;
}

input:checked + .slider:before {
    transform: translateX(22px);
}

/* モーダルスタイル */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
}

.modal-content {
    background: white;
    padding: 25px;
    border-radius: 15px;
    width: 90%;
    max-width: 500px;
    position: relative;
    box-shadow: 0 5px 15px rgba(0,0,0,0.3);
    max-height: 80vh;
    overflow-y: auto;
}

.modal-content h2 {
    margin-top: 0;
    margin-bottom: 15px;
    text-align: center;
}

.modal-content ol {
    padding-left: 20px;
}

.modal-content li {
    margin-bottom: 10px;
}

.modal-content ul {
    margin-top: 5px;
    padding-left: 20px;
}

.close-button {
    position: absolute;
    top: 10px;
    right: 15px;
    background: none;
    border: none;
    font-size: 2rem;
    color: #aaa;
    cursor: pointer;
}

/* クレジット表記 */
.credits {
    margin-top: 15px;
    padding-top: 15px;
    border-top: 1px solid #eee;
}

.credit-item a {
    display: flex;
    align-items: center;
    text-decoration: none;
    color: #333;
    padding: 5px 0;
    border-radius: 5px;
    transition: background-color 0.3s;
}

.credit-item a:hover {
    background-color: #f0f0f0;
}

.credit-item .icon {
    margin-right: 10px;
    width: 20px;
    height: 20px;
    display: inline-block;
}

.x-icon {
    background-color: #000;
    color: #fff;
    font-weight: bold;
    text-align: center;
    line-height: 20px;
    border-radius: 3px;
    font-family: sans-serif;
    font-size: 14px;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 1200 1227' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M714.163 519.284L1160.89 0H1055.03L667.137 450.887L357.328 0H0L468.492 681.821L0 1226.37H105.866L515.491 750.218L842.672 1226.37H1200L714.137 519.284H714.163ZM569.165 687.828L521.697 619.934L144.011 79.6904H308.864L607.439 515.974L655.001 583.959L1048.38 1157.9H883.526L569.165 687.854V687.828Z' fill='white'/%3E%3C/svg%3E");
    background-size: 80%;
    background-repeat: no-repeat;
    background-position: center;
}

.image-icon {
    border: 2px solid #555;
    border-radius: 3px;
    position: relative;
}

.image-icon::before {
    content: '';
    position: absolute;
    top: 3px;
    left: 3px;
    width: 6px;
    height: 6px;
    background-color: #555;
    border-radius: 50%;
}

.image-icon::after {
    content: '';
    position: absolute;
    bottom: 3px;
    right: 3px;
    width: 0;
    height: 0;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-bottom: 8px solid #555;
    transform: rotate(-45deg);
    transform-origin: bottom right;
}
