/* モバイルアプリ風レイアウトのオーバーライド */
body.mobile-app-layout {
    min-height: 100vh;
    min-height: 100dvh;
    /* ブラウザのアドレスバーの伸縮に追従 */
    background: #ffffff;
    /* 背景色をエディタと統一して伸びる現象を解消 */
    color: #0f172a;
    display: flex;
    flex-direction: column;
    margin: 0;
    padding: 0;
}

/* トップバー */
.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1rem;
    height: 60px;
    background: #ffffff;
    border-bottom: 1px solid #e2e8f0;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
    z-index: 100;
    /* モーダルより下、エディタより上 */
    flex-shrink: 0;
    position: sticky;
    top: 0;
}

.header-back-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    color: #475569;
    text-decoration: none;
    border-radius: 50%;
    margin-left: -0.5rem;
    transition: background 0.2s;
}

.header-back-btn:active {
    background: #f1f5f9;
}

.header-title {
    font-size: 1.1rem !important;
    font-weight: 600;
    color: #0f172a;
    margin: 0;
    padding: 0;
    flex: 1;
    text-align: left;
    background: none !important;
    -webkit-text-fill-color: initial !important;
}

.header-icon-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    background: transparent;
    color: #475569;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    transition: background 0.2s;
}

.header-icon-btn:active {
    background: #f1f5f9;
}

.header-print-btn {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    background: var(--primary-color);
    color: white;
    border: none;
    padding: 0.5rem 0.75rem;
    border-radius: 2rem;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 2px 4px rgba(99, 102, 241, 0.2);
}

.header-print-btn:active {
    background: var(--primary-hover);
    transform: translateY(1px);
}

/* メインコンテンツ（エディタ領域） */
.app-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 0;
    margin: 0;
    max-width: 100%;
    width: 100%;
    background: #e2e8f0; /* 背景をグレーにして用紙を際立たせる */
    align-items: center; /* 80mmの用紙を中央に配置 */
}

.editor-wrapper {
    width: 80mm; /* 印刷時と完全に一致させる */
    min-height: 100%;
    margin: 1rem auto 120px;
    padding: 4mm; /* 印刷時と同じ余白 */
    background: #ffffff;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
}

.rich-editor {
    flex: 1;
    outline: none;
    font-size: 7pt; /* 印刷時(通常)と同じサイズ */
    line-height: 1.5; /* レシートらしく少し詰める */
    white-space: pre-wrap;
    word-wrap: break-word;
    color: #000000;
}

/* エディタ内に入力された最初の要素（pやdiv）の余白を消す */
.rich-editor > *:first-child {
    margin-top: 0 !important;
}

.rich-editor:empty::before {
    content: attr(data-placeholder);
    color: #94a3b8;
    pointer-events: none;
    display: block;
}

.rich-editor img {
    max-width: 100%;
    height: auto;
    display: block;
    margin: 1rem 0;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* ===== フィロソフィモード (画面) ===== */
@media screen {
    body.philosophy-mode .philosophy-fold-line {
        display: none !important; /* 画面上では右ボーダーを代用するため非表示 */
    }
    body.philosophy-mode .philosophy-bottom-line {
        display: block !important;
        position: absolute;
        top: 126.2mm; /* 上部余白4mm分を足して、印刷時のテキスト許容量(122.2mm)と完全に一致させる */
        left: 0;
        right: 0;
        border-top: 2px solid #000; /* 印刷時と同じ */
        z-index: 10;
        pointer-events: none;
    }
    body.philosophy-mode .editor-wrapper {
        position: relative;
        width: 67.8mm; /* 右側の不要な余白をカット（80mm - 12.2mm） */
        padding-left: 4mm; /* 印刷時と同じ */
        padding-right: 4.4mm; /* 文字と右線の間の隙間のみ残す（16.6mm - 12.2mm） */
        min-height: 126.2mm; /* 上部余白4mm分を足す */
        border-right: 2px solid #000; /* これを右側の縦線として表示 */
    }
    body.philosophy-mode .rich-editor {
        font-size: 9pt; /* 印刷時と同じ */
    }
}

/* ボトムツールバー */
.app-bottom-bar {
    background: #ffffff;
    border-top: 1px solid #e2e8f0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    padding-bottom: max(24px, env(safe-area-inset-bottom));
    /* 通常の余白 */
    box-sizing: border-box;
    box-shadow: 0 -1px 3px rgba(0, 0, 0, 0.05);
    z-index: 100;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    transition: transform 0.1s ease-out;
    /* キーボード追従時のアニメーション */
}

.bottom-action-group {
    display: flex;
    gap: 1rem;
    width: 100%;
    max-width: 600px;
    margin: 0 auto;
}

.action-btn {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    background: #f1f5f9;
    color: #334155;
    border: none;
    padding: 0.75rem 1rem;
    border-radius: 2rem;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
}

.action-btn:active {
    background: #e2e8f0;
}

/* 切り取り線（画面上は非表示、印刷時のみ表示） */
.cut-line-container {
    display: none;
}

/* QRコード表示エリア（画面上は非表示） */
#qr-container {
    display: none;
}

.cut-line {
    border-top: 2px dashed #94a3b8;
    position: relative;
    width: 100%;
}

.cut-line::before {
    content: "✂";
    position: absolute;
    top: -14px;
    left: 20px;
    font-size: 20px;
    color: #64748b;
    background: #ffffff;
    padding: 0 10px;
}

/* 履歴モーダル */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    display: flex;
    align-items: flex-end;
}

body.modal-open {
    overflow: hidden !important;
    position: fixed;
    width: 100%;
}

.modal-content {
    background: white;
    width: 100%;
    max-width: 500px;
    max-height: 90vh; /* 画面からはみ出さないように制限 */
    overflow-y: auto; /* 中身が長い場合は内部スクロール */
    border-radius: 20px 20px 0 0;
    padding: 20px;
    animation: slideUp 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

/* アクションシート（画像選択用） */
.action-sheet {
    border-radius: 20px 20px 0 0 !important;
    padding-bottom: max(32px, env(safe-area-inset-bottom)) !important;
}

.action-sheet-body {
    display: flex;
    gap: 20px;
    padding: 20px 0;
    justify-content: center;
}

.action-sheet-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    background: none;
    border: none;
    padding: 10px;
    cursor: pointer;
    flex: 1;
    max-width: 120px;
}

.action-icon {
    width: 60px;
    height: 60px;
    border-radius: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    transition: transform 0.2s;
}

.action-sheet-item:active .action-icon {
    transform: scale(0.9);
}

.camera-bg {
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
}

.gallery-bg {
    background: linear-gradient(135deg, #10b981, #3b82f6);
}

.action-sheet-item span {
    font-size: 0.85rem;
    color: #475569;
    font-weight: 500;
}

/* QRモーダル専用スタイル */
.qr-input-group {
    display: flex;
    gap: 10px;
    margin-bottom: 15px;
}

#qr-text {
    flex: 1;
    padding: 12px;
    border: 1px solid #e2e8e0;
    border-radius: 10px;
    font-size: 1rem;
    outline: none;
}

.qr-scan-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 0 15px;
    background: #f1f5f9;
    border: none;
    border-radius: 10px;
    color: #475569;
    font-weight: 500;
    cursor: pointer;
}

#qr-reader {
    width: 100%;
    background: #000;
    margin-bottom: 20px;
}

.qr-action-footer {
    margin-top: 10px;
}

/* トースト通知 */
.toast {
    position: fixed;
    bottom: 100px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.8);
    color: white;
    padding: 12px 24px;
    border-radius: 50px;
    font-size: 0.9rem;
    z-index: 9999;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

.toast.show {
    opacity: 1;
    visibility: visible;
    bottom: 110px;
}

@keyframes slideUp {
    from {
        transform: translateY(100%);
    }

    to {
        transform: translateY(0);
    }
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid #e2e8f0;
}

.modal-header h2 {
    font-size: 1.1rem;
    margin: 0;
}

.modal-close {
    background: none;
    border: none;
    font-size: 2rem;
    line-height: 1;
    color: #64748b;
    cursor: pointer;
    padding: 0;
}

.modal-body {
    padding: 1rem;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    flex: 1;
}

.history-item {
    padding: 1rem;
    border: 1px solid #e2e8f0;
    border-radius: 0.5rem;
    margin-bottom: 1rem;
    cursor: pointer;
    transition: background 0.2s;
}

.history-item:active {
    background: #f8fafc;
}

.history-time {
    font-size: 0.85rem;
    color: #64748b;
    margin-bottom: 0.5rem;
}

.history-preview {
    font-size: 0.95rem;
    color: #334155;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.empty-history {
    text-align: center;
    color: #94a3b8;
    padding: 2rem 0;
}

/* 通常画面ではレシート上のQRを非表示（モーダルのプレビューがあるため） */
#qr-container {
    display: none !important;
}

/* 印刷日時は通常画面では非表示 */
.print-timestamp {
    display: none;
}

.bottom-action-group {
    display: flex;
    gap: 5px;
    justify-content: space-around;
    width: 100%;
}

.action-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
    background: none;
    border: none;
    color: #64748b;
    cursor: pointer;
    padding: 8px 4px;
    border-radius: 12px;
    transition: all 0.2s;
    flex: 1;
    min-width: 0;
}

.action-btn span {
    font-size: 0.75rem;
    font-weight: 600;
    white-space: nowrap;
}

/* 印刷時のスタイル */
@media print {
    .print-timestamp:not(:empty) {
        display: block !important;
        text-align: left !important;
        font-size: 7pt !important;
        color: #000 !important;
        margin-bottom: 2mm !important;
        border: none !important;
        background: none !important;
        padding: 0 !important;
    }
    .print-timestamp:empty {
        display: none !important;
        margin: 0 !important;
        padding: 0 !important;
        height: 0 !important;
    }
    /* ===== ページサイズ ===== */
    @page {
        margin: 0;
        /* sizeは script.js でコンテンツ量に合わせて動的に設定されます */
    }

    /* ===== 全要素のリセット ===== */
    * {
        color: #000000 !important;
        -webkit-text-fill-color: #000000 !important;
        /* グローバルCSSの transparent 対策 */
        background: transparent !important;
        box-shadow: none !important;
        text-shadow: none !important;
        border-color: #000000 !important;
    }

    html {
        width: 80mm !important;
        max-width: 80mm !important;
        margin: 0 !important;
        padding: 0 !important;
        overflow: visible !important;
    }

    body,
    body.mobile-app-layout {
        width: 80mm !important;
        max-width: 80mm !important;
        margin: 0 !important;
        padding: 0 !important;
        overflow: visible !important;
        overflow-x: visible !important;
        background: #ffffff !important;
        display: block !important;
        min-height: 0 !important;
        height: auto !important;
        /* flexレイアウト解除 */
        flex-direction: unset !important;
    }

    /* ===== 不要な要素を非表示 ===== */
    .no-print,
    .app-header,
    .app-bottom-bar,
    .modal-overlay,
    #history-modal,
    header,
    footer {
        display: none !important;
        height: 0 !important;
        width: 0 !important;
        overflow: hidden !important;
        margin: 0 !important;
        padding: 0 !important;
        border: none !important;
    }

    /* ===== メインレイアウト ===== */
    main,
    .app-main {
        display: block !important;
        width: 80mm !important;
        margin: 0 !important;
        padding: 0 !important;
        max-width: 80mm !important;
        overflow: visible !important;
        flex: unset !important;
        flex-direction: unset !important;
    }

    .editor-wrapper {
        display: block !important;
        width: 80mm !important;
        margin: 0 !important;
        padding: 4mm !important;
        /* 左右4mmの余白で72mmの印字可能領域に収める */
        min-height: 0 !important;
        overflow: visible !important;
        flex: unset !important;
        flex-direction: unset !important;
    }

    /* ===== テキスト ===== */
    .rich-editor {
        display: block !important;
        font-size: 7pt !important;
        line-height: 1.5 !important;
        color: #000000 !important;
        -webkit-text-fill-color: #000000 !important;
        white-space: pre-wrap !important;
        word-wrap: break-word !important;
        overflow-wrap: break-word !important;
        word-break: break-word !important;
        overflow: visible !important;
        flex: unset !important;
        /* 改ページ抑制 */
        page-break-inside: avoid;
        break-inside: avoid;
    }

    /* ===== 画像 ===== */
    .rich-editor img {
        display: block !important;
        max-width: 100% !important;
        height: auto !important;
        margin: 2mm 0 !important;
        border-radius: 0 !important;
        page-break-inside: avoid;
        break-inside: avoid;
    }

    /* ===== 印刷時のみQRを表示（内容がある場合のみ） ===== */
    #qr-container:not(.hidden-qr) {
        display: block !important;
        margin: 4mm 0 !important;
        text-align: center;
    }

    #qr-canvas {
        display: block !important;
        width: 100% !important;
        max-width: 100% !important;
        height: auto !important;
    }

    /* プレースホルダー（入力ヒント）を印刷しない */
    .rich-editor:empty:before {
        content: none !important;
        display: none !important;
    }

    /* ===== 切り取り線 ===== */
    .cut-line-container {
        display: block !important;
        margin-top: 4mm !important;/* 上部の余白を4mmに調整 */
        padding-bottom: 15mm !important;
    }

    .cut-line {
        border-top: 2px dashed #000 !important;
        position: relative;
        width: 100% !important;
    }

    .cut-line::before {
        content: "✂";
        position: absolute;
        top: 1px; /* 2pxの線の中心 */
        transform: translateY(-50%); /* 自身の高さの半分だけ上にずらして中央へ */
        left: 10px;
        font-size: 28px;
        line-height: 1;
        color: #000;
        -webkit-text-fill-color: #000 !important;
        background: #fff !important;
        padding: 0 5px;
    }

    /* ===== 改ページ完全抑制 ===== */
    html,
    body,
    .app-main,
    .editor-wrapper,
    .rich-editor {
        page-break-before: auto !important;
        page-break-after: auto !important;
        page-break-inside: avoid !important;
    }
}