:root {
    --primary-color: #2b5c8f;
    --border-thin: #ccc;
    --border-thick: #222;
    --bg-given: #e6f0fa;
    --bg-selected: #b8e0a8;
    --bg-related: #f2f7f0;
    --bg-same-digit: #ffe599;
    --bg-conflict: #fadbd8;
    --text-conflict: #c0392b;
    --text-color: #333;
}

body {
    font-family: "Helvetica Neue", Arial, "Hiragino Kaku Gothic ProN", "Meiryo", sans-serif;
    margin: 0;
    padding: 20px;
    background-color: #f9f9f9;
    color: var(--text-color);
}

.container {
    max-width: 960px;
    margin: 0 auto;
    background: #fff;
    padding: 24px;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

h1 {
    margin-top: 0;
    color: var(--primary-color);
    font-size: 24px;
}

.app-layout {
    display: flex;
    gap: 30px;
    flex-wrap: wrap;
}

.board-container {
    flex: 0 0 auto;
    width: 409px;
    position: relative;
}

.generator-panel {
    margin-top: 12px;
    box-sizing: border-box;
    width: 100%;
}

.grid {
    display: grid;
    grid-template-columns: repeat(9, 45px);
    grid-template-rows: repeat(9, 45px);
    border: 2px solid var(--border-thick);
    background: #fff;
    touch-action: manipulation;
}

.cell {
    width: 45px;
    height: 45px;
    box-sizing: border-box;
    border-right: 1px solid var(--border-thin);
    border-bottom: 1px solid var(--border-thin);
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.08s ease;
}

.cell[data-col="8"] {
    border-right: none;
}

.cell[data-row="8"] {
    border-bottom: none;
}

.cell[data-col="2"],
.cell[data-col="5"] {
    border-right: 2px solid var(--border-thick);
}

.cell[data-row="2"],
.cell[data-row="5"] {
    border-bottom: 2px solid var(--border-thick);
}

.cell.related {
    background-color: var(--bg-related);
}

.cell.given {
    background-color: var(--bg-given);
    color: #000;
    font-weight: 900;
}

.cell.given.related {
    background-color: #d6e6f5;
}

.cell.same-digit {
    background-color: var(--bg-same-digit);
}

.cell.selected {
    background-color: var(--bg-selected);
    outline: 2px solid var(--primary-color);
    outline-offset: -2px;
    z-index: 1;
}

.cell.conflict {
    background-color: var(--bg-conflict);
}

.cell.conflict .cell-value {
    color: var(--text-conflict);
}

/* 確定数字の表示 */
.cell-value {
    font-size: 22px;
    font-weight: bold;
    line-height: 1;
    text-align: center;
    color: var(--text-color);
}

.cell.given .cell-value {
    color: #000;
    font-weight: bold;
}

/* 候補値メモ（3×3グリッド） */
.cell-notes {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    pointer-events: none;
}

.note-digit {
    font-size: 9px;
    color: #555;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
}

/* スマホ用OSキーボード起動用透明input */
#mobile-num-input {
    position: absolute;
    opacity: 0;
    width: 1px;
    height: 1px;
    border: none;
    padding: 0;
    margin: 0;
    pointer-events: none;
    font-size: 16px; /* iOS Safariの自動ズーム防止 */
}

/* スマホ用クイックアクションバー（PCでは非表示） */
.mobile-quick-actions {
    display: none;
    gap: 10px;
    margin-top: 12px;
}

.mobile-quick-actions .btn {
    flex: 1;
    padding: 12px 10px;
    font-size: 15px;
}

/* クリアお祝いバナー */
.clear-banner {
    margin-top: 12px;
    padding: 12px 16px;
    background: linear-gradient(135deg, #2e8b57, #3cb371);
    color: #fff;
    font-weight: bold;
    font-size: 16px;
    text-align: center;
    border-radius: 6px;
    box-shadow: 0 3px 10px rgba(46, 139, 87, 0.3);
    animation: popIn 0.3s ease-out;
}

@keyframes popIn {
    0% {
        transform: scale(0.9);
        opacity: 0;
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

.controls {
    flex: 1;
    min-width: 280px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.panel {
    background: #f5f7fa;
    padding: 16px;
    border-radius: 6px;
    border: 1px solid #e1e4e8;
}

.panel h2 {
    margin-top: 0;
    font-size: 16px;
    color: #444;
    border-bottom: 2px solid #ddd;
    padding-bottom: 6px;
}

.numpad {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 6px;
    margin-top: 10px;
}

.btn {
    padding: 10px;
    font-size: 14px;
    font-weight: bold;
    border: 1px solid #ccc;
    border-radius: 4px;
    background: #fff;
    cursor: pointer;
    transition: all 0.2s;
    touch-action: manipulation;
}

.btn:hover {
    background: #e9e9e9;
}

.btn-primary {
    background: var(--primary-color);
    color: #fff;
    border: none;
}

.btn-primary:hover {
    background: #1d4067;
}

.btn-danger {
    background: #d9534f;
    color: #fff;
    border: none;
}

.btn-danger:hover {
    background: #c9302c;
}

.gen-btns {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
}

.status-box {
    font-weight: bold;
    padding: 10px;
    border-radius: 4px;
    background: #e9ecef;
    margin-top: 10px;
    line-height: 1.5;
}

.text-input-group {
    display: flex;
    gap: 8px;
    margin-top: 8px;
}

.text-input-group input {
    flex: 1;
    padding: 8px;
    font-family: monospace;
    font-size: 12px;
    border: 1px solid #ccc;
    border-radius: 4px;
}

/* 解法ステップ表示エリア */
.step-container {
    margin-top: 12px;
    background: #fff;
    border: 1px solid #ccc;
    border-radius: 4px;
    padding: 10px;
    max-height: 320px;
    overflow-y: auto;
    font-size: 12px;
}

.step-container h3 {
    margin: 0 0 8px 0;
    font-size: 14px;
    color: var(--primary-color);
    border-bottom: 1px dashed #ddd;
    padding-bottom: 4px;
}

.step-list {
    margin: 0;
    padding-left: 20px;
}

.step-list li {
    margin-bottom: 4px;
    line-height: 1.4;
    font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace;
}

.print-header {
    display: none;
}

/* 最高難易度テクニックの表示エリア */
#hardest-technique-display {
    text-align: center;
    margin-top: 12px;
    font-size: 16px;
    font-weight: bold;
    color: #d9534f;
    min-height: 24px;
    letter-spacing: 0.5px;
}

/* ==========================================
   スマホ・タブレット用レスポンシブ対応 (768px以下)
   ========================================== */
@media screen and (max-width: 768px) {
    body {
        padding: 8px;
    }

    .container {
        padding: 12px;
        border-radius: 6px;
    }

    h1 {
        font-size: 18px;
        text-align: center;
        margin-bottom: 12px;
    }

    .app-layout {
        flex-direction: column;
        align-items: center;
        gap: 16px;
    }

    .board-container {
        width: 100%;
        max-width: 405px;
        margin: 0 auto;
    }

    .grid {
        width: 100%;
        aspect-ratio: 1 / 1;
        grid-template-columns: repeat(9, minmax(0, 1fr));
        grid-template-rows: repeat(9, minmax(0, 1fr));
        box-sizing: border-box;
    }

    .cell {
        width: 100%;
        height: 100%;
    }

    .cell-value {
        font-size: clamp(18px, 5.5vw, 24px);
    }

    .note-digit {
        font-size: clamp(8px, 2.3vw, 11px);
    }

    /* スマホ用クイックアクションバーを表示 */
    .mobile-quick-actions {
        display: flex;
    }

    /* スマホでは81桁テキスト読み込み、画面内1〜9テンキー、印刷用一括生成、A4印刷ボタンを非表示 */
    .desktop-text-load-panel,
    .desktop-numpad-panel,
    .desktop-print-section,
    .desktop-print-btn {
        display: none !important;
    }

    .controls {
        width: 100%;
        max-width: 405px;
        min-width: 0;
        gap: 12px;
    }

    .panel {
        padding: 12px;
    }

    /* iOS Safari等でinputフォーカス時に自動ズームされないよう16px以上を確保 */
    .text-input-group input,
    .text-input-group select {
        font-size: 16px;
    }
}

@media print {
    body {
        background: #fff;
        padding: 0;
    }

    .container {
        box-shadow: none;
        padding: 0;
        max-width: 100%;
    }

    .controls,
    .no-print {
        display: none !important;
    }

    .app-layout {
        display: block;
    }

    .grid {
        box-sizing: border-box;
        width: min(95%, 80vh, 495px);
        aspect-ratio: 1 / 1;
        grid-template-columns: repeat(9, minmax(0, 1fr));
        grid-template-rows: repeat(9, minmax(0, 1fr));
        margin: 20px auto;
        page-break-inside: avoid;
    }

    .cell {
        width: 100%;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: clamp(12px, 4vw, 26px);
        line-height: 1;
        overflow: hidden;
    }

    /* 印刷時はハイライト背景をリセット */
    .cell.selected,
    .cell.related,
    .cell.same-digit,
    .cell.conflict {
        background-color: transparent !important;
        outline: none !important;
    }

    .cell.given {
        background-color: var(--bg-given) !important;
    }

    .cell-value {
        line-height: 1;
        height: auto;
        color: #000 !important;
    }

    .print-header {
        display: block !important;
        text-align: center;
        margin-bottom: 20px;
    }

    .controls,
    .panel,
    .no-print {
        display: none !important;
    }

    #hardest-technique-display {
        margin-top: 10px;
        font-size: 14px;
        color: #333;
    }
}

/* ==========================================
   複数印刷モード用のスタイル
   ========================================== */
#print-container {
    display: none;
    flex-wrap: wrap;
    gap: 20px;
    justify-content: center;
    padding: 20px 0;
}

body.multi-print-mode .app-layout,
body.multi-print-mode .print-header,
body.multi-print-mode h1.no-print {
    display: none !important;
}

body.multi-print-mode #print-container {
    display: flex;
}

.print-puzzle-wrapper {
    page-break-inside: avoid;
    break-inside: avoid;
    text-align: center;
    width: min(100%, 260px);
    margin-bottom: 15px;
    border: 1px solid #eee;
    padding: 10px;
    background: #fff;
    box-sizing: border-box;
}

.print-puzzle-header {
    font-size: 13px;
    font-weight: bold;
    margin-bottom: 8px;
    color: #333;
}

.print-puzzle-wrapper .grid {
    width: 100%;
    margin: 0 auto;
}

@media print {
    body.multi-print-mode {
        padding: 0;
        background: #fff;
    }

    body.multi-print-mode .container {
        padding: 0;
        box-shadow: none;
    }

    .print-puzzle-wrapper {
        border: none;
        padding: 5px;
    }

    .print-puzzle-header {
        font-size: 12px;
    }
}
