/* ==========================================================
 * でじらぼ 無限シリーズ「プリント型ドリル」共通の見た目（style.css）
 *   mugen/amari（あまりのあるわり算道場）と mugen/hissan（筆算マスター）は
 *   この同じファイルを使う。直すときは 2本そろえて直すこと。
 *   画面：左に問題カード・右にそうさパネル（テンキー）。印刷は A4 たて。
 * ========================================================== */
:root {
    --bg-color: #f0f4f8;
    --primary-color: #4A90E2;
    --primary-dark: #2f6fb8;
    --accent-color: #F5A623;
    --side-bg: #2c3e50;
    --cell-size: 40px;           /* hissan：画面の1マス */
    --print-cell-size: 7.2mm;    /* hissan：印刷の1マス */
    --print-font-size: 18pt;     /* amari：印刷の文字 */
    --font-main: 'M PLUS Rounded 1c', 'Gowun Dodum', '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', 'Gowun Dodum', 'UD Digi Kyokasho N-R', '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);
}

html[lang="ko"] body { word-break: keep-all; }

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

body {
    font-family: var(--font-main);
    background: var(--bg-color);
    margin: 0;
    padding: 0;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    color: #333;
}

/* --- UI要塞化：かくす --- */
.hidden {
    opacity: 0 !important;
    pointer-events: none !important;
    visibility: hidden !important;
    z-index: -1 !important;
    position: absolute !important;
    /* P1：かくした ボタン（iPad の 🖨）で ページが 横に のびない */
    max-width: 0 !important;
    max-height: 0 !important;
    overflow: hidden !important;
}

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

/* ==========================================================
 * 画面のならび（左：問題／右：そうさパネル）
 *   左上の「🏠 もどる」は deji_nav.js が自動で付ける（上を 70px あける）
 * ========================================================== */
.app-layout { display: flex; height: 100%; overflow: hidden; }

.main-area {
    flex: 1;
    min-width: 0;
    padding: calc(70px + var(--safe-top)) 20px calc(30px + var(--safe-bottom)) calc(20px + var(--safe-left));
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* ヘッダー：おしらせ（左）と 🏆 けっか（右） */
.main-header {
    display: flex;
    flex-wrap: wrap;
    gap: 10px 16px;
    width: 100%;
    max-width: 1200px;
    margin-bottom: 18px;
    align-items: center;
    justify-content: space-between;
}

.note {
    flex: 1 1 320px;
    background: #fff3cd;
    color: #856404;
    padding: 8px 20px;
    border-radius: 20px;
    font-weight: bold;
    margin: 0;
    font-size: 0.95rem;
    line-height: 1.6;
}
.note-medal { display: block; font-size: 0.85rem; color: #8a5a2b; margin-top: 2px; }

/* 🏆 けっかを見る（答えあわせで ぜんぶの問題に こたえたら出る） */
.show-result-btn {
    background: #FFD700;
    color: #d35400;
    min-height: 52px;
    padding: 0 22px;
    border-radius: 30px;
    font-weight: bold;
    font-family: var(--font-main);
    font-size: 1.1rem;
    border: 3px solid #F5A623;
    box-shadow: 0 4px 8px rgba(0,0,0,0.2);
    cursor: pointer;
    white-space: nowrap;
    animation: popIn 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.show-result-btn:active { transform: scale(0.95); box-shadow: 0 1px 2px rgba(0,0,0,0.2); }

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

#digital-container {
    display: grid;
    gap: 20px;
    width: 100%;
    max-width: 1200px;
}

.card {
    background: white;
    border-radius: 12px;
    box-shadow: 0 3px 6px rgba(0,0,0,0.05);
    display: flex;
    position: relative;
    transition: box-shadow 0.3s;
}

.q-label {
    position: absolute;
    top: 8px;
    left: 12px;
    font-size: 0.85rem;
    color: #888;
    font-weight: bold;
}

/* 💮（正解したカード） */
.hanamaru {
    position: absolute;
    top: 0; left: 0; width: 100%; height: 100%;
    pointer-events: none;
    z-index: 5;
}
.hanamaru::after {
    content: "";
    position: absolute;
    top: 4px;
    right: 6px;
    width: 64px;
    height: 64px;
    background: url('../../assets/hanamaru.webp') no-repeat center / contain;
    opacity: 0.9;
}

/* ゆれる（まちがい） */
@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-8px); }
    50% { transform: translateX(8px); }
    75% { transform: translateX(-8px); }
}
.shake { animation: shake 0.4s ease; }

/* ==========================================================
 * amari：わり算のカード
 * ========================================================== */
.amari-grid { grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); }
.amari-grid .card {
    padding: 25px 15px 38px;
    flex-direction: column;
    align-items: center;
}

.math-row {
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: var(--font-digit);
    font-size: 2.3rem;
    gap: 8px;
    margin-top: 10px;
    width: 100%;
    white-space: nowrap;
}
.math-row span { flex-shrink: 0; }

.ans-input {
    width: 60px;
    height: 56px;
    text-align: center;
    flex-shrink: 0;
    font-size: 1.9rem;
    font-family: var(--font-main);
    font-weight: bold;
    border: 2px solid #ccc;
    border-radius: 8px;
    background: #eef9ff;
    outline: none;
    cursor: pointer;
    padding: 0;
    color: #333;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.ans-input.active {
    background: #d0eaff;
    border-color: var(--primary-color);
    box-shadow: 0 0 8px rgba(74, 144, 226, 0.4);
}
.ans-input.correct { color: #e74c3c; background: transparent; border-color: transparent; pointer-events: none; }
.ans-input.wrong { background: #fadbd8; border-color: #e74c3c; }

.amari-text { font-size: 1.3rem; color: #555; white-space: nowrap; flex-shrink: 0; font-family: var(--font-main); }

.msg-bubble {
    position: absolute;
    bottom: 6px;
    left: 50%;
    transform: translateX(-50%);
    background: #ffeaa7;
    color: #d35400;
    padding: 4px 14px;
    border-radius: 12px;
    font-size: 0.9rem;
    font-weight: bold;
    white-space: nowrap;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    z-index: 10;
}
.msg-bubble::after {
    content: '';
    position: absolute;
    top: -6px;
    left: 50%;
    transform: translateX(-50%);
    border-width: 0 6px 6px 6px;
    border-style: solid;
    border-color: transparent transparent #ffeaa7 transparent;
}

/* ==========================================================
 * hissan：筆算のカード
 * ========================================================== */
.hissan-grid { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.hissan-grid .card { padding: 26px 20px 20px; justify-content: center; }

.hissan-box {
    display: grid;
    gap: 0;
    font-family: var(--font-digit);
    font-size: 1.6rem;
    margin-top: 6px;
    border: 2px solid #aaa;
    background: #fff;
}

.cell {
    width: var(--cell-size);
    height: var(--cell-size);
    display: flex;
    align-items: center;
    justify-content: center;
    border-right: 1px solid #eee;
    border-bottom: 1px solid #eee;
    position: relative;
}
.symbol { font-size: 1.2rem; }
.border-line { border-bottom: 2px solid #333 !important; }

/* 画面の途中式（ふだんは かくす。答えを見るで出る） */
.calc-step-digital { color: transparent; background: #fff; }
.hissan-box.show-answer .calc-step-digital { color: #e74c3c; background: #fff5f5; }

.digit-in {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: inherit;
    font-size: inherit;
    background: #eef9ff;
    color: #333;
    cursor: pointer;
}
.digit-in.active { background: #d0eaff; box-shadow: inset 0 0 0 3px var(--primary-color); }
.digit-in.correct { color: #e74c3c; background: transparent; font-weight: bold; }
.digit-in.wrong { background: #fadbd8; }
.hissan-box.show-answer .digit-in { color: #e74c3c; font-weight: bold; background: transparent; }

/* ==========================================================
 * そうさパネル（右）
 * ========================================================== */
.sidebar {
    width: 320px;
    flex-shrink: 0;
    background: var(--side-bg);
    padding: calc(12px + var(--safe-top)) calc(14px + var(--safe-right)) calc(14px + var(--safe-bottom)) 14px;
    color: white;
    display: flex;
    flex-direction: column;
    gap: 8px;
    box-shadow: -4px 0 10px rgba(0,0,0,0.1);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

/* 言語の切りかえ（にほんご・English・한국어）。中身は deji_nav.js が入れる */
.lang-slot { display: flex; justify-content: center; }
.lang-slot .deji-lang-icon { display: none; }
.lang-slot .deji-lang-btn { padding: 0 10px; font-size: 15px; }

.sidebar h2 {
    font-size: 1.1rem;
    margin: 4px 0 2px;
    border-bottom: 1px solid #556;
    padding-bottom: 6px;
}
.sidebar select {
    width: 100%;
    min-height: 44px;
    padding: 0 10px;
    border-radius: 6px;
    border: none;
    font-family: inherit;
    font-weight: bold;
    font-size: 0.95rem;
    color: #333;
    background: #fff;
}
.select-row { display: flex; gap: 6px; }
.select-row select { flex: 1; min-width: 0; }

.btn-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 4px; }

.ui-btn {
    width: 100%;
    min-height: 48px;
    padding: 4px 6px;
    border: none;
    border-radius: 8px;
    font-weight: bold;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.95rem;
    line-height: 1.25;
    color: #fff;
    box-shadow: 0 3px 0 rgba(0,0,0,0.25);
}
.ui-btn:active { transform: translateY(3px); box-shadow: none; }
.ui-btn.wide { grid-column: span 2; }
.btn-blue { background: var(--primary-color); }
.btn-orange { background: var(--accent-color); }
.btn-pink { background: #E91E63; }
.btn-red { background: #c0392b; }
.btn-gray { background: #7f8c8d; }
.btn-green { background: #27ae60; }

#keypad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
    margin-top: auto;
    padding-top: 8px;
}
.key {
    background: white;
    color: #333;
    font-family: var(--font-main);
    font-size: 1.4rem;
    height: 50px;
    border: none;
    border-radius: 8px;
    font-weight: bold;
    box-shadow: 0 3px 0 #bdc3c7;
    cursor: pointer;
}
.key:active { transform: translateY(3px); box-shadow: none; }
.key-next { background: #3498db; color: white; box-shadow: 0 3px 0 #2471a3; font-size: 1.15rem; }
.key-bs { background: #e74c3c; color: white; box-shadow: 0 3px 0 #a93226; }

/* せまい画面・たて向き：問題を上、そうさパネルを下に */
@media (max-width: 899px) {
    .app-layout { flex-direction: column; }
    .main-area { flex: 1; min-height: 0; padding-bottom: 16px; }
    .sidebar {
        width: 100%;
        max-height: 50dvh;
        display: grid;
        grid-template-columns: 1fr 1fr;
        align-content: start;
        column-gap: 14px;
        padding: 10px calc(12px + var(--safe-right)) calc(10px + var(--safe-bottom)) calc(12px + var(--safe-left));
    }
    .sidebar > *:not(#keypad) { grid-column: 1; }
    #keypad { grid-column: 2; grid-row: 1 / span 8; margin-top: 0; padding-top: 0; align-self: start; }
    .key { height: 46px; }
}
@media (max-height: 640px) {
    .key { height: 42px; font-size: 1.2rem; }
    .ui-btn { min-height: 44px; }
    .main-area { padding-top: calc(64px + var(--safe-top)); }
}

/* ==========================================================
 * 小窓（けっか・かくにん）。🏠 もどる（9000）より下の 8000
 * ========================================================== */
.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: 22px 20px 16px;
    width: 100%;
    max-width: 460px;
    max-height: 100%;
    overflow-y: auto;
    text-align: center;
}
.dialog-box p { margin: 0 0 8px; font-size: 1.1rem; line-height: 1.6; color: #555; white-space: pre-wrap; }
.dialog-actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin-top: 14px; }
.dialog-actions .ui-btn { flex: 1 1 140px; max-width: 200px; }

.result-box { border-color: #F5A623; box-shadow: 4px 4px 0 #d35400; }
.result-title { font-size: 1.5rem; font-weight: 800; color: #d35400; margin-bottom: 6px; }
.result-score { font-size: 1.25rem; font-weight: bold; color: #333; }
.result-medal { font-size: 1.4rem; font-weight: 800; margin: 8px 0 4px; }
.result-hint { font-size: 0.95rem; color: #8a5a2b; }
.result-note { font-size: 0.85rem; color: #777; margin-top: 6px; }
.btn-cert { background: linear-gradient(180deg, #f1c40f, #e67e22); border: 2px solid #f39c12; box-shadow: 0 3px 0 #d35400; }

/* 小さなお知らせ（遊びを止めない） */
.toast {
    position: fixed;
    left: 50%;
    bottom: calc(24px + var(--safe-bottom));
    transform: translateX(-50%);
    z-index: 7000;
    background: rgba(44, 62, 80, 0.95);
    color: #fff;
    padding: 12px 22px;
    border-radius: 30px;
    font-weight: bold;
    font-size: 1rem;
    max-width: calc(100% - 40px);
    text-align: center;
    box-shadow: 0 4px 12px rgba(0,0,0,0.25);
}

@media (prefers-reduced-motion: reduce) {
    .shake, .show-result-btn { animation: none; }
}

/* ==========================================================
 * 印刷（A4 たて）
 * ========================================================== */
#print-area { display: none; }

@media print {
    body { height: auto; overflow: visible; background: #fff; }
    .app-layout, .overlay, .toast { display: none !important; }
    #print-area { display: block; width: 100%; }

    @page { size: A4 portrait; margin: 0; }

    .print-page {
        width: 210mm;
        height: 297mm;
        padding: 15mm;
        page-break-after: always;
        break-after: page;
        position: relative;
        overflow: hidden;
        display: flex;
        flex-direction: column;
    }
    .print-page:last-child { page-break-after: auto; break-after: auto; }

    .print-header {
        height: 18mm;
        border-bottom: 2px solid #333;
        display: flex;
        justify-content: space-between;
        align-items: flex-end;
        padding-bottom: 2mm;
        margin-bottom: 5mm;
        font-family: var(--font-digit);
        white-space: nowrap;
    }
    .ph-title { font-size: 16pt; font-weight: bold; margin-right: 10px; overflow: hidden; text-overflow: ellipsis; }
    .ph-meta { display: flex; gap: 5mm; font-size: 10.5pt; flex-shrink: 0; }
    .ph-blank { display: inline-block; min-width: 10mm; border-bottom: 1px solid #333; }
    .ph-blank.long { min-width: 26mm; }
    .ans-note { font-size: 10pt; color: #555; font-weight: normal; margin-left: 12px; }
    .ans-red { color: red !important; font-weight: bold; }

    /* amari */
    .print-grid-amari {
        display: grid;
        grid-template-columns: 1fr 1fr;
        column-gap: 15mm;
        row-gap: 8mm;
        align-content: start;
        padding-left: 10mm;
    }
    .p-math-row {
        display: flex;
        align-items: baseline;
        justify-content: flex-start;
        font-family: var(--font-digit);
        font-size: var(--print-font-size);
        gap: 2mm;
        padding: 3mm 0;
        border-bottom: 1px dashed #ccc;
        position: relative;
        white-space: nowrap;
    }
    .p-math-row span { flex-shrink: 0; }
    .p-num { position: absolute; left: -10mm; font-size: 12pt; top: 50%; transform: translateY(-50%); }
    .p-box { display: inline-block; width: 15mm; border-bottom: 1px solid #333; text-align: center; }
    .p-amari { font-size: 13pt; margin: 0 2mm; font-family: var(--font-main); }
    .p-box.ans-red { border-bottom: none; }
    .global-calc-space {
        margin-top: 15mm;
        padding-top: 5mm;
        border-top: 2px dashed #ccc;
        font-size: 13pt;
        color: #999;
        font-family: var(--font-main);
        flex-grow: 1;
    }

    /* hissan */
    .print-grid-hissan {
        display: grid;
        grid-template-columns: 1fr 1fr;
        grid-template-rows: repeat(5, 1fr);
        column-gap: 12mm;
        row-gap: 2mm;
        height: 245mm;
    }
    .print-card { display: flex; flex-direction: column; align-items: center; justify-content: flex-start; position: relative; }
    .print-num { position: absolute; top: 0; left: 0; font-size: 10pt; }
    .print-hissan {
        display: grid;
        font-family: var(--font-digit);
        font-size: 13pt;
        line-height: 1;
        margin-top: 3mm;
        border: 2px solid #000;
    }
    .p-cell {
        width: var(--print-cell-size);
        height: var(--print-cell-size);
        display: flex;
        align-items: center;
        justify-content: center;
        border-right: 1px solid #888;
        border-bottom: 1px solid #888;
    }
    .p-cell.last-col { border-right: none; }
    .p-cell.last-row { border-bottom: none; }
    .border-heavy { border-bottom: 2px solid #000 !important; }
}
