/* レスポンシブ対応 - 全ての画面で三段表示を維持 */

@media (max-width: 1024px) {
    .nav-buttons {
        max-width: 110px;
    }

    .nav-button {
        max-width: 110px;
        padding: 7px 10px;
        font-size: 0.75em;
    }
}

/* レスポンシブ対応 - UI要素用 */
@media (max-width: 768px) {
    /* Safari用の特別なスタイル */
    _::-webkit-full-page-media,
    _:future,
    :root .safari_only {
        #ui-container {
            top: 100px !important;
        }
        .nav-buttons {
            top: 100px !important;
        }
    }

    #ui-container {
        top: max(5px, var(--safe-area-top));
        left: max(5px, var(--safe-area-left));
        padding: 10px 12px;
        max-width: min(280px, calc(100vw - 80px));
        font-size: 16px;
        min-height: 85px;
        border-radius: 15px;
        touch-action: manipulation;
    }

    .ui-item {
        margin: 6px 0;
        font-size: 16px;
        font-weight: bold;
        color: #d32f2f;
        display: flex;
        justify-content: space-between;
        min-width: 140px;
    }

    .ui-value {
        color: #ff6b6b;
        margin-left: 12px;
        font-size: 16px;
    }

    .nav-buttons {
        top: max(5px, var(--safe-area-top));
        right: max(5px, var(--safe-area-right));
        max-width: min(160px, calc(100vw - 20px));
        gap: 6px;
    }

    .nav-button {
        max-width: 120px;
        padding: 8px 10px;
        font-size: 14px;
        min-height: 32px;
        border-radius: 20px;
        min-height: 44px !important;
        min-width: 44px !important;
    }

    .mentaiko.miracle {
        width: 65px;
        height: 45px;
    }

    .mentaiko.miracle::before {
        font-size: 20px;
        top: -20px;
    }
    
    .game-instruction[open] {
        max-height: 80vh !important;
        position: relative;
    }
    
    .instruction-content {
        max-height: calc(80vh - 80px) !important;
        padding: 15px;
    }
    
    .basic-rules {
        width: 95%;
        padding: 15px;
    }
    
    #rules-screen .settings-container {
        max-height: 80vh;
    }
}

/* レスポンシブ対応 - 折りたたみ式説明書用 */
@media (max-width: 768px) {
    .game-instruction {
        width: 95%;
        margin: 15px 0;
    }
    
    .game-instruction[open] {
        max-height: 80vh;
        position: relative;
    }
    
    .instruction-content {
        padding: 15px;
        max-height: calc(80vh - 80px);
    }
}

/* 横長画面（ランドスケープ）対応 - UI要素用 */
@media (max-width: 768px) and (orientation: landscape) {
    .game-instruction[open] {
        max-height: 70vh !important;
    }

    .instruction-content {
        max-height: calc(70vh - 80px) !important;
    }
    
    #ui-container {
        max-width: min(250px, calc(100vw - 100px));
        font-size: 14px;
        min-height: 70px;
        padding: 8px 10px;
    }

    .ui-item {
        font-size: 14px;
        margin: 4px 0;
        min-width: 120px;
    }

    .ui-value {
        font-size: 14px;
    }

    .nav-buttons {
        max-width: min(140px, calc(100vw - 100px));
    }

    .nav-button {
        font-size: 12px;
        padding: 6px 8px;
        min-height: 28px;
    }
    
    #rules-screen .settings-container {
        max-height: 70vh;
    }
}

/* 横長画面（ランドスケープ）対応 - 折りたたみ式説明書用 */
@media (max-width: 768px) and (orientation: landscape) {
    .game-instruction[open] {
        max-height: 65vh;
    }

    .instruction-content {
        max-height: calc(65vh - 80px);
    }
}

/* 高解像度ディスプレイ対応 */
@media (max-width: 768px) and (-webkit-min-device-pixel-ratio: 2) {
    #ui-container {
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }

    .ui-item,
    .ui-value,
    .nav-button {
        text-shadow: 0.5px 0.5px 1px rgba(0, 0, 0, 0.1);
    }
}

@media (max-width: 600px) {
    #ui-container {
        max-width: 50%;
    }

    .nav-buttons {
        max-width: 45%;
        flex-wrap: wrap;
    }
}

@media (max-width: 480px) {
    .game-instruction[open] {
        max-height: 70vh;
    }
    
    .instruction-content {
        max-height: calc(70vh - 80px);
        padding: 15px;
    }
}

@media (max-width: 360px) and (max-height: 640px) {
    .screen {
        padding-top: 10px;
        justify-content: flex-start;
    }

    h1 {
        font-size: 1.6em;
        margin-bottom: 10px;
    }

    .button-group {
        margin-top: 15px;
    }

    button {
        padding: 12px 24px;
        font-size: 1em;
    }

    .nav-buttons {
        max-width: 80px;
        gap: 4px;
    }

    .nav-button {
        max-width: 80px;
        padding: 4px 5px;
        font-size: 0.6em;
        min-height: 22px;
    }

    /* アイコンのみ表示時の調整 */
    #pause-game-button::before,
    #settings-button::before,
    #achievements-button::before {
        font-size: 10px;
    }

    #pause-game-button::before {
        content: "⏸️";
        font-size: 12px;
    }

    #settings-button::before {
        content: "⚙️";
        font-size: 12px;
    }

    #achievements-button::before {
        content: "🏆";
        font-size: 12px;
    }
}

/* 360px以下の小型スマホ */
@media (max-width: 360px) {
    #ui-container {
        top: max(5px, var(--safe-area-top));
        left: max(5px, var(--safe-area-left));
        padding: 10px 12px;
        max-width: min(280px, calc(100vw - 60px));
        font-size: 16px;
        min-height: 90px;
    }

    .ui-item {
        margin: 7px 0;
        font-size: 16px;
        min-width: 150px;
    }

    .ui-value {
        font-size: 16px;
        margin-left: 12px;
    }

    .nav-buttons {
        top: max(5px, var(--safe-area-top));
        right: max(5px, var(--safe-area-right));
        max-width: min(150px, calc(100vw - 20px));
        gap: 6px;
    }

    .nav-button {
        max-width: 130px;
        padding: 8px 10px;
        font-size: 14px;
        min-height: 34px;
    }
}

/* 非常に小さい画面ではボタンテキストを短くする */
@media (max-width: 320px) {
    .nav-button {
        padding: 4px 6px;
        font-size: 0.65em;
    }

    .nav-button span {
        display: none;
    }

    .nav-button::before {
        content: "⚙️";
    }

    #pause-game-button::before {
        content: "⏸️";
    }

    #achievements-button::before {
        content: "🏆";
    }
    
    #pause-game-button {
        font-size: 0;
        padding: 6px 8px;
    }
}

/* 非常に小さい画面用 - 重要な修正 */
@media (max-height: 700px) and (max-width: 480px) {
    .game-instruction[open] {
        max-height: 75vh !important;
    }
    
    .instruction-content {
        max-height: calc(75vh - 80px) !important;
    }
}

/* 超小型デバイス用 */
@media (max-height: 600px) and (max-width: 360px) {
    .game-instruction[open] {
        max-height: 70vh;
    }
    
    .instruction-content {
        max-height: calc(70vh - 80px);
        padding: 12px;
    }
}

/* 折りたたみ説明書の高さ制限を緩和 */
@media (max-height: 700px) {
    .game-instruction[open] {
        max-height: 75vh !important;
    }
    
    .instruction-content {
        max-height: calc(75vh - 80px) !important;
    }
}

@media (max-height: 600px) {
    .game-instruction[open] {
        max-height: 80vh !important;
    }
    
    .instruction-content {
        max-height: calc(80vh - 80px) !important;
        padding: 12px;
    }
    
    /* コンパクト表示 */
    .instruction-section {
        margin: 12px 0;
        padding-bottom: 8px;
    }
    
    .instruction-item {
        margin: 5px 0;
        font-size: 0.9em;
        line-height: 1.4;
    }
    
    .instruction-section h4 {
        font-size: 0.95em;
        margin-bottom: 6px;
    }
}

/* 超小型デバイス用 */
@media (max-height: 500px) {
    .game-instruction[open] {
        max-height: 85vh !important;
    }
    
    .instruction-content {
        max-height: calc(85vh - 80px) !important;
        padding: 10px;
    }
    
    .instruction-section {
        margin: 8px 0;
        padding-bottom: 6px;
    }
    
    .instruction-item {
        margin: 3px 0;
        font-size: 0.85em;
        line-height: 1.3;
    }
    
    .bomb-details div {
        font-size: 0.8em;
        margin: 2px 0;
    }
}

/* 折りたたみ説明書の動的高さ調整 */
.game-instruction[open] {
    max-height: min(600px, 70vh) !important; /* 最大600pxまたは画面の70% */
    min-height: 200px !important;
    display: flex !important;
    flex-direction: column !important;
    height: auto !important;
}

.instruction-content {
    flex: 1 !important;
    height: auto !important; /* 固定高さを解除 */
    min-height: 150px !important;
    max-height: none !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    -webkit-overflow-scrolling: touch !important;
    padding: 15px 20px !important;
}

/* スクロールバーを確実に表示 */
.instruction-content::-webkit-scrollbar {
    display: block !important;
    width: 8px;
}

.instruction-content::-webkit-scrollbar-track {
    background: rgba(255, 107, 107, 0.1) !important;
    border-radius: 4px;
}

.instruction-content::-webkit-scrollbar-thumb {
    background: rgba(255, 107, 107, 0.4) !important;
    border-radius: 4px;
}

.instruction-content::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 107, 107, 0.6) !important;
}

/* コンテンツが確実に表示されるように */
.instruction-section {
    display: block !important;
    min-height: auto !important;
    margin: 15px 0 !important;
}

.instruction-item, .bomb-details div {
    display: block !important;
    min-height: auto !important;
    margin: 8px 0 !important;
}

/* 競合するメディアクエリを上書き */
@media (max-width: 768px) {
    .game-instruction[open] {
        max-height: 70vh !important;
        position: relative !important;
    }
    
    .instruction-content {
        max-height: none !important;
        padding: 12px 15px !important;
    }
}

/* 高さが小さい画面用 */
@media (max-height: 700px) {
    .game-instruction[open] {
        max-height: 80vh !important;
    }
}

@media (max-height: 600px) {
    .game-instruction[open] {
        max-height: 85vh !important;
    }
}

/* メディアクエリで段階的に調整 */
@media (max-height: 800px) {
    .game-instruction[open] {
        max-height: min(500px, 75vh) !important;
    }
}

@media (max-height: 600px) {
    .game-instruction[open] {
        max-height: min(400px, 80vh) !important;
    }
    
    .instruction-content {
        padding: 12px 15px !important;
    }
}

@media (max-height: 500px) {
    .game-instruction[open] {
        max-height: min(350px, 85vh) !important;
    }
    
    .instruction-content {
        padding: 10px 12px !important;
    }
    
    /* コンパクト表示 */
    .instruction-section {
        margin: 10px 0 !important;
        padding-bottom: 8px !important;
    }
    
    .instruction-item {
        margin: 6px 0 !important;
        font-size: 0.9em !important;
    }
}

/* 小さな横画面用 */
@media (max-width: 768px) and (orientation: landscape) {
    .game-instruction[open] {
        max-height: min(300px, 70vh) !important;
    }
    
    .instruction-content {
        max-height: none !important;
    }
}

/* 非常に小さい画面 */
@media (max-height: 400px) {
    .game-instruction[open] {
        max-height: min(280px, 90vh) !important;
    }
    
    .instruction-content {
        padding: 8px 10px !important;
    }
    
    .instruction-section {
        margin: 6px 0 !important;
    }
    
    .instruction-item {
        margin: 4px 0 !important;
        font-size: 0.85em !important;
    }
}