.nanpre-app {
    font-family: "Segoe UI", sans-serif;
    background: #f4f4f9;
    color: #333;
    padding: 10px;
    border-radius: 8px;
}

.nanpre-app h1 {
    margin-bottom: 20px;
    font-size: 1.5rem;
    text-align: center;
}

.nanpre-app .container {
    display: flex;
    flex-direction: column;
    gap: 20px;
    align-items: center;
}

.nanpre-app .input-area,
.nanpre-app .display-area {
    width: 100%;
    max-width: 800px;
    background: #fff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
    box-sizing: border-box;
}

.nanpre-app .legend {
    font-size: 0.85em;
    color: #666;
    margin-bottom: 10px;
}

.nanpre-app .canvas-container {
    width: 100%;
    display: flex;
    justify-content: center;
    padding: 0;
    margin: 10px auto;
    background: transparent;
}

.nanpre-app canvas {
    max-width: 100%;
    height: auto !important; /* アスペクト比維持 */
    background: #fff;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.nanpre-app .grid {
    --cell-size: clamp(26px, 4.2vw, 44px);
    display: grid;
    grid-template-columns: repeat(9, var(--cell-size));
    grid-template-rows: repeat(9, var(--cell-size));
    gap: 0;
    justify-content: center;
    margin: 10px auto 15px;
    width: max-content; /* fit-contentだと一部ブラウザで挙動が怪しいためmax-contentに変更 */
    max-width: 100%; /* はみ出し防止 */
    overflow-x: auto; /* 必要ならスクロール */
}

.nanpre-app .cell,
.nanpre-app .cell-input {
    width: var(--cell-size);
    height: var(--cell-size);
    border: 1px solid #cfcfcf;
    box-sizing: border-box;
    text-align: center;
    font-size: 16px;
    padding: 0;
    margin: 0;
    line-height: var(--cell-size); /* 追加: テキストの垂直方向中央揃えと高さ制限を担保 */
    overflow: hidden; /* 追加: はみ出し防止 */
    position: relative; /* 子要素の絶対配置のため */
    display: block; /* Flexboxの中央揃えを解除し、line-heightによる制御にする場合 */
}

/* div要素のセルはFlexboxではなく通常のブロックとして扱い、候補グリッドを絶対配置する */
.nanpre-app div.cell {
    /*display: flex; */ /* 削除 */
    /*align-items: center;*/
    /*justify-content: center;*/
    line-height: var(--cell-size); /* 確定数字を中央に */
}

.nanpre-app .cell-input {
    border-radius: 0;
    outline: none;
    font-family: "BIZ UDゴシック", "Yu Gothic Mono", monospace;
}

.nanpre-app .cell.fixed,
.nanpre-app .cell-input.fixed {
    background: #eef3ff;
    font-weight: bold;
}

.nanpre-app .cell.filled {
    background: #f7f7f7;
}

.nanpre-app .border-right { border-right: 2px solid #666; }
.nanpre-app .border-bottom { border-bottom: 2px solid #666; }


.nanpre-app .keypad {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(5, minmax(44px, 1fr));
    gap: 8px;
    margin: 10px auto 0;
}

.nanpre-app .keypad button {
    padding: 10px 0;
    font-size: 16px;
}

.nanpre-app .keypad button.secondary {
    /* grid-column: span 2; Removed to fit in 5th column */
    background: #fff8e1;
    border-color: #ffecb3;
}

.nanpre-app .cell-input.selected {
    outline: 2px solid #64b5f6;
    outline-offset: -2px;
    background: #eef7ff;
}

.nanpre-app .controls {
    width: 100%;
    margin-top: 10px;
    padding: 10px 0;
    background: #f9f9f9;
    border-radius: 4px;
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    justify-content: center;
}

.nanpre-app .difficulty-label {
    font-size: 13px;
    color: #555;
}

.nanpre-app .difficulty-select {
    padding: 6px 8px;
    border: 1px solid #ccc;
    border-radius: 4px;
    background: #fff;
    font-size: 14px;
}

.nanpre-app button {
    padding: 10px 20px;
    cursor: pointer;
    font-weight: bold;
    border: 1px solid #ccc;
    background: #fff;
    border-radius: 4px;
    transition: 0.2s;
    color: #333;
    font-size: 14px;
}

.nanpre-app button:hover { background: #eee; }
.nanpre-app button.primary { background-color: #e0f7fa; border-color: #00bcd4; color: #006064; }

.nanpre-app #status,
.nanpre-app #stepDisplay,
.nanpre-app .node-info {
    width: 100%;
    margin-top: 8px;
    font-weight: bold;
    color: #444;
    text-align: center;
}

.nanpre-app .playback-controls {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap; /* 折り返しを許可 */
    gap: 8px; /* ギャップを少し広げる */
    margin-bottom: 10px;
}
.nanpre-app .playback-controls button {
    padding: 8px 6px; /* パディングを減らす */
    min-width: 40px;  /* 幅も詰める */
    font-size: 14px;  /* 文字も少し小さく */
    flex: 0 0 auto;
    width: 44px;      /* 固定幅にしてガタつき防止 */
    text-align: center;
    justify-content: center;
}
.nanpre-app #btnPlay {
    padding: 8px 0 !important; /* 左右paddingを0にしてwidthで制御 */
    width: 80px !important;    /* 固定幅にする */
    text-align: center !important;
    font-size: 1.2em;
    background-color: #e3f2fd;
    border-color: #2196f3;
    color: #0d47a1;
    display: inline-flex !important;
    justify-content: center !important;
    align-items: center !important;
}

.nanpre-app #playSpeed {
    margin-left: 0 !important; /* インラインスタイル無効化 */
    padding: 8px;
    font-size: 14px;
    border: 1px solid #ccc;
    border-radius: 4px;
    background: #fff;
}

.nanpre-app .step-explanation {
    background: #e8f5e9;
    border: 1px solid #c8e6c9;
    padding: 12px;
    margin-bottom: 15px;
    border-radius: 8px; /* 角丸を少し大きく */
    min-height: 3em; /* 高さを確保 */
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    font-weight: bold;
    color: #2e7d32;
    font-size: 15px; /* フォントサイズ固定 */
    line-height: 1.5; /* 行間を広げる */
    word-break: break-word; /* 自然な折り返し */
    overflow-wrap: break-word;
    flex-wrap: wrap; /* 内容が多い場合の折り返し */
}

/* 強力な背景色指定: media queryに依存せず、親要素に頼らず適用する */
.nanpre-app .step-explanation {
    background: #e8f5e9 !important;
    background-color: #e8f5e9 !important;
    border: 2px solid #c8e6c9 !important;
    color: #2e7d32 !important;
    font-size: 14px !important;
    padding: 10px !important;
    height: 6em !important; /* 高さを固定してガタつきを防止 */
    min-height: 6em !important; 
    max-height: 6em !important;
    overflow-y: auto !important; /* 長すぎる場合はスクロール */
    box-shadow: none !important;
    display: block !important;
    text-align: center !important;
    line-height: 1.5 !important;
}

@media screen and (max-width: 768px) { 
    .nanpre-app .step-explanation {
        font-size: 13px !important; 
    }
}

/* ダークモード対策：全てのサイズで適用 */
@media (prefers-color-scheme: dark) {
    .nanpre-app .step-explanation {
        background: #e8f5e9 !important; 
        background-color: #e8f5e9 !important;
        color: #2e7d32 !important; 
        border-color: #c8e6c9 !important;
        -webkit-text-fill-color: #2e7d32 !important;
    }
}

@media screen and (max-width: 768px) {
    .nanpre-app .playback-controls button {
        padding: 10px 6px; 
        margin: 2px;
        flex: 0 0 auto;
        width: 44px; /* スマホでも幅固定 */
    }
    
    .nanpre-app .playback-controls {
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: center !important;
        align-items: center !important;
        width: 100%;
    }

    .nanpre-app #playSpeed {
        display: block !important;
        width: 80% !important; 
        max-width: 250px !important;
        margin: 10px auto !important; /* 中央寄せ */
        text-align: center !important;
        text-align-last: center !important;
        padding: 8px !important;
        font-size: 16px !important; 
        flex-basis: 100% !important; 
    }
}

/* 候補数字の表示用 */
.nanpre-app .candidates-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr) !important;
    grid-template-rows: repeat(3, 1fr) !important;
    width: 100% !important;
    height: 100% !important;
    pointer-events: none;
    box-sizing: border-box;
    margin: 0 !important;
    padding: 0 !important;
    position: absolute;
    top: 0;
    left: 0;
    line-height: normal !important; /* 親のline-heightを強制リセット */
}
.nanpre-app .candidate-cell {
    font-size: 8px !important; /* 強制的に小さく */
    line-height: normal !important; /* 行間も強制リセット */
    display: flex;
    align-items: center;
    justify-content: center;
    color: #666;
    overflow: hidden;
    width: auto !important; /* グリッドに従う */
    height: auto !important;
    padding: 0 !important;
    margin: 0 !important;
}
/* コンフリクト（矛盾）ハイライト用 */
.nanpre-app .cell-input.conflict {
    background-color: #ffcdd2 !important; /* 薄い赤背景 */
    color: #c62828 !important; /* 濃い赤文字 */
    font-weight: bold;
    border-color: #e57373 !important;
}

/* ハイライト用 */
.nanpre-app .cell.highlight-target { background-color: #a5d6a7 !important; } /* 確定セル */
.nanpre-app .cell.highlight-source { background-color: #ffe0b2 !important; } /* 根拠セル */
.nanpre-app .cell.highlight-remove { background-color: #ef9a9a !important; } /* 候補削除 */

@media (max-width: 600px) {
    .nanpre-app .input-area,
    .nanpre-app .display-area {
        padding: 12px 2px; /* 左右のpaddingを極限まで減らす */
        width: 100% !important;
        max-width: 100% !important;
        box-sizing: border-box;
    }

    .nanpre-app .grid {
        /*
           スマホでの中央揃えを確実にするための設定
           1. 幅は画面いっぱい (100%)
           2. Flexboxで中身（グリッド）を強制中央揃え
           3. セルサイズは画面幅に合わせて自動縮小 (fit)
        */
        display: flex !important;
        flex-wrap: wrap !important;
        justify-content: center !important;
        width: 100% !important;
        margin: 10px auto !important;
        padding: 0 !important;
        
        /* 9列グリッドの定義 */
        display: grid !important;
        grid-template-columns: repeat(9, 1fr) !important;
        /* 幅は最大でも画面幅 - 余白 */
        max-width: calc(100vw - 10px) !important;
        
        /* セルサイズ計算: 1frで等分するか、固定サイズにするか */
        /* calc((100vw - 16px) / 9) で、左右8pxずつの余白を残して等分 */
        --cell-size: calc((100vw - 16px) / 9);
        
        grid-auto-rows: var(--cell-size) !important;
        
        position: relative;
        z-index: 9999;
    }

    .nanpre-app .cell, .nanpre-app .cell-input {
        width: 100% !important;
        height: 100% !important;
        /* createSquareGrid的にアスペクト比を維持 */
        aspect-ratio: 1 / 1;
        font-size: clamp(16px, 4vw, 24px) !important; /* フォントサイズも動的に */
    }

    .nanpre-app .cell,
    .nanpre-app .cell-input {
        font-size: 20px;
    }

    .nanpre-app .candidate-cell {
        font-size: 8px; /* スマホでは小さめに */
    }

    .nanpre-app .keypad button {
        font-size: 18px;
        padding: 12px 0;
    }
}
