:root {
    --bg: #F3F4F6;
    --surface: #FFFFFF;
    --border: #E5E7EB;
    --text: #111827;
    --text-light: #6B7280;
    --primary: #2563EB;
    --ecc: #DC2626;
    --mask: #D97706;
    --code-font: 'Roboto Mono', monospace;
}

body { margin: 0; font-family: 'Inter', sans-serif; background: var(--bg); color: var(--text); height: 100vh; overflow: hidden; }
.app-container { display: grid; grid-template-rows: 60px 1fr 70px; height: 100%; }

.toolbar, .settings-bar { background: var(--surface); border-color: var(--border); display: flex; align-items: center; padding: 0 24px; }
.toolbar { border-bottom: 1px solid var(--border); justify-content: space-between; z-index: 20; }
.settings-bar { border-top: 1px solid var(--border); justify-content: center; gap: 30px; }
.logo { font-weight: 800; font-size: 1.2rem; letter-spacing: -0.05em; }

.step-controls { flex: 1; display: flex; align-items: center; justify-content: center; gap: 16px; margin: 0 20px; }
.step-indicator { flex: 1; text-align: center; max-width: 200px; }
#stepLabel { display: block; font-size: 0.85rem; font-weight: 600; margin-bottom: 6px; }
.progress-bar { height: 4px; background: #E5E7EB; border-radius: 2px; overflow: hidden; }
#progressFill { height: 100%; background: var(--primary); width: 0%; transition: width 0.3s; }
.actions { display: flex; gap: 10px; }

.btn { padding: 8px 16px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-weight: 600; font-size: 0.9rem; transition: 0.2s; }
.btn:hover:not(:disabled) { background: #F9FAFB; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: var(--primary); color: white; border: none; }
.btn.icon-btn { padding: 8px 12px; }

input[type="text"], select { padding: 8px; border: 1px solid var(--border); border-radius: 6px; font-family: var(--code-font); outline: none; }
input[type="text"]:focus { border-color: var(--primary); }

.stage { display: flex; padding: 20px; gap: 20px; position: relative; overflow: hidden; }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; display: flex; flex-direction: column; padding: 16px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.05); }
.panel-header { display: flex; justify-content: space-between; align-items: center; font-size: 0.75rem; font-weight: 700; color: var(--text-light); text-transform: uppercase; margin-bottom: 12px; height: 32px; }

.source { flex: 1; min-width: 350px; }
.dest { flex: 1; min-width: 350px; background: #F8FAFC; }
.input-wrapper { flex: 1; margin-left: 20px; }

.bit-grid { 
    flex: 1; 
    overflow-y: auto; 
    font-family: var(--code-font); 
    font-size: 13px; 
    display: flex; 
    flex-wrap: wrap; 
    align-content: flex-start; 
    gap: 4px; 
    padding: 4px; 
    position: relative; 
}

.char-block {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 8px;
    margin: 4px;
    background: #FFF;
    transition: all 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    min-width: 40px;
}
.char-block.transforming {
    transform: scale(1.1);
    border-color: var(--primary);
    background: #EFF6FF;
}
.char-label { font-size: 1.2rem; font-weight: bold; margin-bottom: 4px; color: var(--text); }
.char-hex { font-size: 0.7rem; color: var(--text-light); }
.char-bits { 
    display: flex; gap: 1px; margin-top: 4px; 
    opacity: 0; transform: translateY(10px); transition: 0.3s; 
}
.char-block.revealed .char-bits { opacity: 1; transform: translateY(0); }
.char-block.revealed .char-label { font-size: 0.8rem; color: var(--text-light); }

.rs-log { width: 100%; font-family: var(--code-font); font-size: 0.85rem; }
.rs-row { display: flex; align-items: center; padding: 6px; border-bottom: 1px solid #F3F4F6; animation: fadeIn 0.3s ease; }
.rs-label { width: 120px; color: var(--text-light); font-weight: 600; }
.rs-val { flex: 1; display: flex; gap: 4px; flex-wrap: wrap; }
.byte-box {
    width: 24px; height: 24px;
    display: flex; align-items: center; justify-content: center;
    border-radius: 4px;
    background: #E5E7EB;
    font-size: 0.75rem;
    color: #374151;
    position: relative;
    transition: 0.2s;
}
.byte-box.active { background: var(--primary); color: white; transform: scale(1.1); z-index: 10; }
.byte-box.generator { background: #10B981; color: white; }
.byte-box.ecc-temp { background: #F59E0B; color: white; }
.byte-box.result { background: var(--ecc); color: white; font-weight: bold; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: translateY(0); } }

.stream-section {
    width: 100%; 
    box-sizing: border-box;
    background: #FFFFFF;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px;
    margin-bottom: 12px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.section-data { border-left: 4px solid var(--primary); }
.section-ecc { border-left: 4px solid var(--ecc); }

.stream-title {
    margin: 0 0 10px 0;
    font-size: 0.8rem;
    color: var(--text-light);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    border-bottom: 1px solid #F3F4F6;
    padding-bottom: 6px;
}

.stream-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-content: flex-start;
}

.codeword-block {
    display: inline-flex;
    background: #fff;
    border: 1px solid #E5E7EB;
    border-radius: 4px;
    overflow: hidden;
    transition: transform 0.1s;
}
.codeword-block:hover {
    transform: translateY(-1px);
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    border-color: var(--primary);
}

.bit { display: inline-block; width: 14px; text-align: center; color: #D1D5DB; }

.char-bits .bit.data { color: var(--primary); font-weight: bold; }

.codeword-block .bit {
    height: 18px;
    line-height: 18px;
    font-weight: bold;
    font-size: 0.75rem;
    cursor: help;
}

.bit.DATA { color: var(--primary); background: #EFF6FF; }
.bit.ECC { color: var(--ecc); background: #FEF2F2; }
.bit.MODE { color: #7C3AED; background: #F5F3FF; }
.bit.COUNT { color: #059669; background: #ECFDF5; }
.bit.TERM { color: #9CA3AF; background: #F3F4F6; }
.bit.BIT_PAD { color: #D1D5DB; background: #F9FAFB; }
.bit.BYTE_PAD, .bit.PAD { color: #9CA3AF; background: #F3F4F6; border-left: 1px solid #fff; }

.bit.transport-active { opacity: 0; }

.qr-frame { flex: 1; display: flex; align-items: center; justify-content: center; background: #fff; border-radius: 8px; position: relative; }
canvas { image-rendering: pixelated; }
#overlayCanvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; z-index: 10; }

.meta-info { text-align: center; margin-top: 10px; font-family: var(--code-font); font-size: 0.8rem; color: var(--text-light); }
.setting-group { display: flex; flex-direction: column; gap: 4px; }
.setting-group label { font-size: 0.7rem; font-weight: 700; color: var(--text-light); text-transform: uppercase; }

.tooltip { position: fixed; background: rgba(17,24,39,0.95); color: #fff; padding: 8px 12px; border-radius: 6px; font-size: 0.8rem; pointer-events: none; z-index: 100; font-family: 'Inter', sans-serif; white-space: nowrap; }
.tooltip.hidden { opacity: 0; }

.mask-score-item { transition: all 0.2s; }
.mask-score-item:hover { background-color: #F3F4F6; }

.input-stream-container {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    padding: 8px;
    background: #F9FAFB;
    border: 1px solid #E5E7EB;
    border-radius: 6px;
    margin-bottom: 12px;
    max-height: 120px;
    overflow-y: auto;
}

.stream-byte {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 36px;
    background: #FFF;
    border: 1px solid #D1D5DB;
    border-radius: 4px;
    font-size: 0.75rem;
    transition: all 0.2s;
}

.stream-byte .lbl {
    font-size: 0.6rem;
    color: #9CA3AF;
}

.stream-byte .val {
    font-weight: bold;
    color: #374151;
}

.stream-byte.active {
    background: #2563EB;
    border-color: #2563EB;
    transform: scale(1.1);
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.stream-byte.active .lbl,
.stream-byte.active .val {
    color: #FFF;
}

/* --- レスポンシブ対応 (スマホ・タブレット向け) --- */
@media (max-width: 900px) {
    /* 画面の高さを固定せず、スクロール可能にする */
    body {
        height: auto;
        overflow-y: auto;
        overflow-x: hidden;
    }

    .app-container {
        display: flex;
        flex-direction: column;
        height: auto;
        min-height: 100vh;
        grid-template-rows: none;
    }

    /* ヘッダー周り */
    .toolbar {
        position: sticky;
        top: 0;
        z-index: 100;
        flex-wrap: wrap;
        height: auto;
        padding: 10px 16px;
        gap: 10px;
    }

    .logo {
        width: 100%;
        text-align: center;
        margin-bottom: 4px;
    }

    .step-controls {
        order: 2; /* ボタン類を下段へ */
        width: 100%;
        margin: 0;
        justify-content: space-between;
    }

    .actions {
        width: 100%;
        justify-content: center;
        order: 3;
        margin-top: 4px;
    }

    /* メインステージ（縦並びへ変更） */
    .stage {
        flex-direction: column;
        padding: 16px;
        gap: 24px;
        overflow: visible;
        height: auto;
    }

    .panel {
        width: 100%;
        min-width: 0; /* Flexアイテムの縮小を許可 */
        height: auto;
        min-height: auto;
        box-sizing: border-box;
    }

    /* 入力パネル */
    .source {
        order: 1;
        max-height: 50vh; /* 入力欄が長くなりすぎないように制限 */
    }

    /* QR表示パネル */
    .dest {
        order: 2;
        background: #fff;
    }

    /* QRフレームを正方形に保ちつつ画面に収める */
    .qr-frame {
        width: 100%;
        max-width: 350px;
        aspect-ratio: 1 / 1;
        margin: 0 auto;
        height: auto;
    }

    /* フッター（設定エリア） */
    .settings-bar {
        flex-wrap: wrap;
        height: auto;
        padding: 20px 16px;
        gap: 16px;
        border-top: 1px solid var(--border);
    }

    .setting-group {
        flex: 1 1 40%; /* 2列配置 */
        min-width: 120px;
    }

    .speed-group {
        flex: 1 1 100%; /* スライダーは横幅いっぱい */
    }
}