/* ==========================================================
 * でじらぼ 無限シリーズ「マス計算」共通の見た目（style.css）
 *   mugen/pick（選べる！マス計算）と mugen/hnd（無限！百マス計算）は
 *   この同じファイルを使う。直すときは 2本そろえて直すこと。
 * ========================================================== */
:root {
    --primary-color: #5D9CEC;
    --primary-dark: #4A89DC;
    --accent-color: #FFCE54;
    --bg-color: #F5F7FA;
    --text-color: #434A54;
    --error-color: #FFD2D2;
    --success-color: #A0D468;
    --cell-size: 44px;
    --print-cell: 11mm;
    --font-main: 'M PLUS Rounded 1c', 'Gowun Dodum', 'Noto Sans JP', 'Hiragino Maru Gothic ProN', 'Hiragino Sans', 'BIZ UDPGothic', 'Meiryo', 'Apple SD Gothic Neo', 'Malgun Gothic', sans-serif;
    --font-digit: 'Klee One', 'M PLUS Rounded 1c', 'UD Digi Kyokasho N-R', 'Hiragino Mincho ProN', 'Hiragino Maru Gothic ProN', 'Hiragino Sans', 'BIZ UDPGothic', 'Meiryo', 'Apple SD Gothic Neo', 'Malgun Gothic', serif;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --home-space: calc(140px + var(--safe-left));
}

/* 韓国語：ハングルは Gowun Dodum（ハングルがあるときだけ読みこまれる） */
html[lang="ko"] body { word-break: keep-all; }

* { box-sizing: border-box; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
button, a, select, label, .input-cell { touch-action: manipulation; }

body {
    font-family: var(--font-main);
    background-color: var(--bg-color);
    color: var(--text-color);
    margin: 0;
    padding: 0 0 calc(20px + var(--safe-bottom));
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vh;
    min-height: 100dvh;
    overflow-x: hidden;
}

/* --- UI要塞化：かくす --- */
.hidden {
    opacity: 0 !important;
    pointer-events: none !important;
    visibility: hidden !important;
    z-index: -1 !important;
    position: absolute !important;
}

button:focus-visible, select:focus-visible, a:focus-visible {
    outline: 3px solid var(--primary-dark);
    outline-offset: 2px;
}

/* --- ヘッダー（左上の 🏠 もどる は deji_nav.js が自動で付ける） --- */
.header-nav {
    width: 100%;
    background: #fff;
    min-height: calc(68px + var(--safe-top));
    padding: calc(10px + var(--safe-top)) calc(12px + var(--safe-right)) 10px var(--home-space);
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 12px;
    position: sticky;
    top: 0;
    z-index: 50;
}

.title {
    font-size: 1.4rem;
    color: var(--primary-color);
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* 言語の切りかえ（にほんご・English・한국어）。中身は deji_nav.js が入れる */
.lang-slot { display: flex; justify-content: flex-end; min-width: 0; }

@media (max-width: 1024px) {
    .title { font-size: 1.2rem; }
}
@media (max-width: 760px) {
    .header-nav { grid-template-columns: 1fr; }
    .lang-slot { justify-content: flex-start; }
}

/* --- 操作パネル --- */
.control-panel {
    margin: 10px;
    padding: 10px;
    background: #fff;
    border-radius: 10px;
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    box-shadow: 0 2px 5px rgba(0,0,0,0.05);
    width: 95%;
    max-width: 1200px;
    position: relative;
}

.mode-switch {
    display: flex;
    background: #E6E9ED;
    border-radius: 22px;
    padding: 4px;
    gap: 4px;
    position: relative;
}
.mode-switch input {
    position: absolute;
    opacity: 0;
    width: 1px;
    height: 1px;
    pointer-events: none;
}
.mode-switch label {
    min-height: 44px;
    display: flex;
    align-items: center;
    padding: 0 16px;
    cursor: pointer;
    border-radius: 18px;
    font-weight: bold;
    color: #656D78;
    transition: all 0.2s;
    font-size: 0.95rem;
    white-space: nowrap;
}
.mode-switch input:checked + label {
    background: white;
    color: var(--primary-color);
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.mode-switch input:focus-visible + label {
    outline: 3px solid var(--primary-dark);
    outline-offset: 1px;
}

.size-select {
    min-height: 44px;
    padding: 0 12px;
    border-radius: 10px;
    border: 2px solid var(--primary-color);
    font-family: inherit;
    font-weight: bold;
    color: var(--text-color);
    cursor: pointer;
    background: #fff;
    font-size: 0.95rem;
}

.btn {
    border: none;
    min-height: 44px;
    padding: 0 15px;
    border-radius: 10px;
    font-family: inherit;
    font-weight: bold;
    font-size: 0.95rem;
    cursor: pointer;
    color: white;
    box-shadow: 0 4px 0 rgba(0,0,0,0.2);
    transition: transform 0.1s;
    display: flex;
    align-items: center;
    gap: 5px;
    white-space: nowrap;
}
.btn:active { transform: translateY(4px); box-shadow: none; }

.btn-gen { background-color: var(--primary-color); }
.btn-timer { background-color: #656D78; min-width: 86px; justify-content: center; }
.btn-timer.running { background-color: #DA4453; }
.btn-check { background-color: #FB6E52; }
.btn-print { background-color: #48CFAD; }
.btn-print-left { background-color: #37BC9B; }
.btn-gray { background-color: #AAB2BD; }
.btn-hand { background-color: #fff; color: #656D78; border: 2px solid #AAB2BD; box-shadow: 0 4px 0 #AAB2BD; padding: 0 13px; }
.btn-hand[aria-pressed="true"] { background-color: #AC92EC; color: #fff; border-color: #967ADC; box-shadow: 0 4px 0 #967ADC; }
.btn-cert {
    background: linear-gradient(180deg, #f1c40f, #e67e22);
    border: 2px solid #f39c12;
    box-shadow: 0 4px 0 #d35400;
    width: 100%;
    justify-content: center;
    margin-top: 8px;
    font-size: 1rem;
}

/* 左利きモード：数字キーを左に置き、左手でタップしても腕がマス目を隠さないようにする */
body.left-mode .keyboard-section { order: 1; }
body.left-mode .grid-section { order: 2; }
body.left-mode .guide-section { order: 3; }
@media (max-width: 950px) {
    body.left-mode .guide-section { order: 0; }
}

.timer-box {
    font-size: 1.2rem;
    font-weight: bold;
    background: #333;
    color: #0f0;
    padding: 8px 12px;
    border-radius: 5px;
    font-family: monospace;
    min-width: 70px;
    text-align: center;
}

/* --- 3列のならび（左：まなびかた／まん中：マス／右：数字キー） --- */
.flex-layout-container {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    gap: 15px;
    margin-top: 10px;
    width: 100%;
    max-width: 1200px;
    padding: 0 10px;
    margin-bottom: 30px;
}

/* 1. まなびかた */
.guide-section {
    position: relative;
    background: #FFF8E1;
    border: 2px dashed #FFCE54;
    border-radius: 15px;
    padding: 15px;
    width: 240px;
    flex-shrink: 0;
}

#hanamaru {
    position: absolute;
    top: 0; left: 0; width: 100%; height: 100%;
    background: url('../../assets/hanamaru.webp') no-repeat center center;
    background-size: contain;
    z-index: 10;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}
#hanamaru.show {
    visibility: visible;
    animation: stamp 0.3s ease-out forwards, stampFade 0.6s ease-in 2.4s forwards;
}
@keyframes stamp {
    0% { transform: scale(2) rotate(-10deg); opacity: 0; }
    100% { transform: scale(1) rotate(0deg); opacity: 1; }
}
/* はなまるは少しして消える（にんていしょうボタンを押せるように） */
@keyframes stampFade {
    to { opacity: 0; visibility: hidden; }
}

.guide-title {
    font-weight: bold;
    color: #F6BB42;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 5px;
    font-size: 1.1rem;
}
.guide-list {
    margin: 0;
    padding-left: 0;
    list-style: none;
    font-size: 0.85rem;
    line-height: 1.5;
    color: #555;
}
.guide-list li { margin-bottom: 12px; }
.guide-step { font-weight: bold; color: var(--primary-color); display: block; margin-bottom: 2px; }
.guide-note { color: #FB6E52; font-weight: bold; font-size: 0.85rem; display: block; margin-top: 4px; }
.guide-medal { background: #fff; border-radius: 10px; padding: 6px 8px; }
.guide-medal .medal-text { white-space: pre-line; display: block; }

.result-box {
    background: #fff;
    border: 3px solid #FB6E52;
    border-radius: 12px;
    padding: 8px 10px;
    margin-bottom: 12px;
    text-align: center;
    font-weight: bold;
    line-height: 1.5;
}
.result-box.all-correct { border-color: var(--success-color); color: #5E9C2E; }
.result-main { font-size: 1.05rem; }
.result-sub { font-size: 0.85rem; color: #E9573F; }
.result-box.all-correct .result-sub { color: #8a5a2b; }

@media (max-width: 950px) {
    .flex-layout-container { flex-wrap: wrap; }
    .guide-section { width: 100%; margin-bottom: 0; }
    .guide-list { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
    .guide-list li { margin-bottom: 0; }
}
@media (max-width: 600px) {
    .guide-list { grid-template-columns: 1fr; }
}

/* 2. マス */
.grid-section {
    position: relative;
    background: white;
    padding: 20px;
    border-radius: 15px;
    box-shadow: 0 5px 15px rgba(0,0,0,0.1);
    flex-shrink: 0;
    display: flex;
    justify-content: center;
    max-width: 100%;
    overflow-x: auto;
}
@media (max-width: 600px) {
    .grid-section { padding: 10px; }
}

.grid-table {
    border-collapse: collapse;
    table-layout: fixed;
    margin: 0 auto;
    font-family: var(--font-digit);
}

.grid-table td, .grid-table th {
    border: 2px solid #aaa;
    width: var(--cell-size);
    height: var(--cell-size);
    text-align: center;
    font-size: calc(var(--cell-size) * 0.5);
    padding: 0;
    overflow: hidden;
}

.grid-table th {
    background-color: #E6E9ED;
    color: #333;
    font-family: var(--font-main);
    font-size: calc(var(--cell-size) * 0.45);
}
.grid-table th.operator-cell, .operator-cell { color: var(--primary-color); font-weight: 900; }

.input-cell { cursor: pointer; background: #fff; color: #333; font-weight: bold; }
.input-cell.error { background-color: var(--error-color); }
.input-cell.active {
    background-color: #E0F7FA;
    box-shadow: inset 0 0 0 3px var(--primary-color);
}
.input-cell.active.error { background-color: #FFC0C0; }

/* 3. 数字キー */
.keyboard-section {
    background: #fff;
    padding: 12px;
    border-radius: 15px;
    box-shadow: 0 5px 15px rgba(0,0,0,0.1);
    display: flex;
    flex-direction: column;
    gap: 10px;
    align-items: center;
    flex-shrink: 0;
    width: 220px;
}

.numpad, .arrow-pad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
    width: 100%;
}

.key {
    width: 100%;
    height: 48px;
    font-size: 1.3rem;
    font-family: var(--font-main);
    background: #fff;
    border: 2px solid #ddd;
    border-radius: 8px;
    box-shadow: 0 4px 0 #ccc;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    color: #555;
    padding: 0 4px;
    line-height: 1.1;
}
.key:active { transform: translateY(4px); box-shadow: none; }
.key-wide { grid-column: span 2; }

.key-bs { background: #ED5565; color: white; border-color: #DA4453; box-shadow: 0 4px 0 #DA4453; font-size: 1rem; }
.key-enter { background: #A0D468; color: white; border-color: #8CC152; box-shadow: 0 4px 0 #8CC152; font-size: 1.1rem; }
.key-clear-all { background: #FFCE54; color: white; border-color: #F6BB42; box-shadow: 0 4px 0 #F6BB42; font-size: 1rem; }
.key-arrow { height: 44px; font-size: 1.2rem; }

/* --- 小窓（alert/confirm の代わり）。🏠 もどる（9000）より下 --- */
.overlay {
    position: fixed;
    inset: 0;
    z-index: 8000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background: rgba(0,0,0,0.45);
}
.dialog-box {
    background: #fff;
    border: 4px solid var(--primary-color);
    border-radius: 20px;
    box-shadow: 4px 4px 0 var(--primary-dark);
    padding: 24px 20px 14px;
    width: 100%;
    max-width: 440px;
    text-align: center;
}
.dialog-box p {
    margin: 0 0 10px;
    font-size: 1.1rem;
    line-height: 1.6;
    color: #555;
    white-space: pre-wrap;
}
.dialog-actions { display: flex; gap: 12px; justify-content: center; margin-top: 16px; }
.dialog-actions .btn { flex: 1; justify-content: center; max-width: 180px; }

@media (prefers-reduced-motion: reduce) {
    #hanamaru.show { animation: none; opacity: 1; }
    .btn, .key, .mode-switch label { transition: none; }
}

/* --- 印刷 --- */
#print-area { display: none; }

@media print {
    @page { size: A4 landscape; margin: 0; }
    body {
        background: white;
        display: block;
        padding: 0;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
        overflow: visible !important;
    }
    .header-nav, .control-panel, .flex-layout-container, .overlay { display: none !important; }
    #print-area { display: block !important; width: 100%; }

    .print-page {
        display: flex;
        width: 297mm;
        height: 210mm;
        page-break-after: always;
        break-after: page;
        padding: 10mm;
    }
    .print-page:last-child { page-break-after: auto; break-after: auto; }
    .print-half {
        width: 50%;
        height: 100%;
        padding: 0 10mm;
        border-right: 1px dashed #ccc;
        position: relative;
    }
    .print-half:last-child { border-right: none; }

    .print-header {
        display: flex;
        flex-direction: column;
        border-bottom: 2px solid #333;
        margin-bottom: 10mm;
        padding-bottom: 5px;
        font-family: var(--font-main);
    }
    .print-title { font-size: 1.3rem; font-weight: bold; line-height: 1.4; }
    .print-title .print-sub { font-size: 0.9rem; font-weight: normal; margin-left: 4px; }
    .print-title .print-left { font-size: 0.9rem; color: #37BC9B; margin-left: 4px; }
    .print-meta { display: flex; align-items: flex-end; font-size: 0.9rem; color: #333; margin-top: 6px; white-space: nowrap; }
    .print-sep { margin: 0 8px; }
    .print-blank { display: inline-block; min-width: 2.2em; border-bottom: 1px solid #999; margin: 0 3px; height: 1.1em; }
    .print-meta-name { flex-grow: 1; border-bottom: 1px solid #333; margin-left: 5px; height: 1.2rem; }

    .grid-table-print { border-collapse: collapse; table-layout: fixed; margin: 0 auto; font-family: var(--font-digit); }
    .grid-table-print tr { page-break-inside: avoid !important; break-inside: avoid; }
    .grid-table-print td, .grid-table-print th {
        border: 1px solid #333;
        width: var(--print-cell);
        height: var(--print-cell);
        text-align: center;
        font-size: calc(var(--print-cell) * 0.55);
        color: #000;
        padding: 0;
    }
    .grid-table-print th { background-color: #eee !important; font-weight: bold; font-family: var(--font-main); }
    .grid-table-print th.operator-cell { color: var(--primary-color); }
    .answer-text { color: #f00 !important; font-weight: bold; }

    .copyright-text { position: absolute; bottom: 10mm; right: 10mm; font-size: 0.7rem; color: #888; font-family: var(--font-main); }
}
