/* ==============================================
   Anima WebUI - Mobile Responsive
   小画面（768px以下）でスマホ用UIに切替
   ※ orientation は使わない（キーボード表示で landscape 判定になる問題回避）
   ============================================== */

/* ── 判定: 画面幅768px以下 ── */
@media (max-width: 768px) {

    /* iOS Safari: font-size < 16px の入力欄はフォーカス時に自動ズームされる。
       全入力要素を16px以上にして防止 */
    input,
    textarea,
    select {
        font-size: 16px !important;
    }

    /* ========== body & container ========== */
    body {
        overflow: auto;
        overflow-x: hidden;
    }

    .app-container {
        height: auto;
        min-height: 100vh;
        min-height: 100dvh;
        /* iOS Safari対応 */
        overflow: visible;
        padding-bottom: 52px;
        /* 画面最下部の固定パレットバー分 */
    }

    /* ========== ヘッダー ========== */
    .app-header {
        padding: 6px 10px;
        gap: 8px;
        flex-wrap: wrap;
        position: sticky;
        top: 0;
        z-index: 100;
    }

    .header-brand {
        gap: 6px;
    }

    .brand-icon svg {
        width: 22px;
        height: 22px;
    }

    .brand-name {
        font-size: 0.95rem;
    }

    .brand-sub {
        display: none;
    }

    /* タブバー: コンパクト化 */
    .header-tabs {
        order: 10;
        width: 100%;
        justify-content: center;
        padding: 2px;
        gap: 1px;
    }

    .header-tab {
        padding: 5px 12px;
        font-size: 0.78rem;
        flex: 1;
        text-align: center;
    }

    /* ヘッダー右側: 最小限に */
    .header-right {
        gap: 6px;
    }

    .header-status .status-text {
        display: none;
    }

    .queue-badge span,
    .clients-badge span {
        font-size: 0.68rem;
    }

    #tutorialBtn {
        display: none;
    }

    /* ========== メインコンテンツ ========== */
    .main-content {
        overflow: visible;
    }

    .tab-panel {
        overflow: visible;
    }

    .tab-panel.active {
        display: flex;
    }

    /* ========== txt2img: 3カラム → 1カラム縦積み ========== */
    .txt2img-layout {
        display: flex;
        flex-direction: column;
        overflow: visible;
        min-height: 0;
    }

    /* ── 左カラム: プロンプト ── */
    .layout-prompts {
        border-right: none;
        border-bottom: 1px solid var(--border-subtle);
        overflow-y: visible;
        padding: 10px 12px;
        max-height: none;
        font-size: 1rem;
    }

    .prompt-sections {
        gap: 1px;
    }

    /* テキストエリア */
    .control-textarea {
        font-size: 1.1rem;
        padding: 8px 10px;
        resize: none;
    }

    .control-textarea--compact {
        font-size: 1.05rem;
        padding: 5px 8px;
        resize: none;
    }

    /* テキストエリア リサイズハンドル (下辺全体) */
    .textarea-resize-handle {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 18px;
        cursor: ns-resize;
        background: var(--bg-elevated);
        border: 1px solid var(--border-subtle);
        border-top: none;
        border-radius: 0 0 var(--radius-sm) var(--radius-sm);
        margin-top: -2px;
        margin-bottom: 4px;
        touch-action: none;
        -webkit-tap-highlight-color: transparent;
    }

    .textarea-resize-handle:active {
        background: var(--bg-hover);
    }

    .resize-handle-bar {
        display: block;
        width: 40px;
        height: 3px;
        border-radius: 2px;
        background: var(--text-muted);
        opacity: 0.35;
    }

    .textarea-resize-handle:active .resize-handle-bar {
        opacity: 0.6;
    }

    /* セクションラベル */
    .section-label-text {
        font-size: 0.95rem;
    }

    /* プロンプトモード切替 */
    .prompt-mode-switcher {
        padding: 4px 6px;
    }

    .prompt-mode-btn {
        padding: 4px 10px;
        font-size: 0.78rem;
    }

    /* タグパレットボタン: やや小さく */
    .quickpreset-btn {
        padding: 4px 8px;
        font-size: 0.72rem;
    }

    .quickpreset-btn .qp-desc {
        font-size: 0.6rem;
    }

    .quickpreset-category-label {
        font-size: 0.78rem;
    }

    /* キャラスロット */
    .char-slot-label {
        font-size: 0.75rem;
    }

    .char-add-btn {
        font-size: 0.72rem;
    }

    .char-preset-btn {
        font-size: 0.68rem;
    }

    /* ── 中央カラム: 設定 + プレビュー ── */
    .layout-center {
        overflow: visible;
    }

    /* 設定バー: グリッドで2列配置 */
    .settings-bar {
        padding: 8px 10px;
        gap: 8px;
        flex-wrap: wrap;
    }

    .settings-bar-controls {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
        width: 100%;
        position: relative;
    }

    .sbar-item {
        min-width: 0;
    }

    .sbar-select {
        min-width: 0;
        width: 100%;
        font-size: 0.78rem;
    }

    .sbar-slider {
        width: 60px;
    }

    .sbar-num {
        width: 42px;
        font-size: 0.75rem;
    }

    .sbar-seed-input {
        width: 80px;
        font-size: 0.78rem;
    }

    /* Generateボタン: フル幅、最下段に配置 */
    .sbar-gen-btn {
        width: 100%;
        grid-column: 1 / -1;
        padding: 10px 16px;
        font-size: 0.9rem;
        order: 2;
    }

    /* キャンセルボタン: Sizeの右横に配置 */
    .sbar-skip-btn {
        order: 1;
        grid-column: 2;
        align-self: end;
        padding: 7px 14px;
        font-size: 0.75rem;
        border-radius: var(--radius-sm);
        background: rgba(239, 68, 68, 0.85);
        color: #fff;
        border: none;
    }

    /* プレビューエリア: 固定高さではなく可変に */
    .preview-area {
        min-height: 50vw;
        max-height: 70vh;
        padding: 12px;
    }

    .result-image {
        max-height: 65vh;
    }

    .live-preview-image {
        max-height: 65vh;
    }

    /* ヒストリーストリップ */
    .history-strip {
        padding: 4px 8px;
        min-height: 44px;
    }

    /* 画面最下部の固定パレットバー分、コンテンツに下部余白を確保 */
    .layout-prompts {
        padding-bottom: 52px;
    }

    .history-strip-inner .history-thumb {
        width: 36px;
        height: 36px;
    }

    /* ── 右カラム: ギャラリーストリップ → 横スクロール化 ── */
    .layout-gallery-strip {
        border-left: none;
        border-top: 1px solid var(--border-subtle);
        overflow-x: auto;
        overflow-y: hidden;
        padding: 6px 8px;
        flex-direction: row;
        max-height: none;
    }

    .blur-toggle-header {
        position: static;
        padding: 4px;
        flex-shrink: 0;
    }

    .blur-toggle-btn {
        font-size: 0.65rem;
        padding: 2px 6px;
        width: auto;
    }

    .gallery-strip-inner {
        flex-direction: row;
        gap: 4px;
        overflow-x: auto;
    }

    .gallery-strip-inner .gallery-thumb {
        width: 48px;
        height: 48px;
        aspect-ratio: 1;
        flex-shrink: 0;
    }

    /* ========== タグパレットサイドバー → 下半分ボトムシート ========== */
    .tag-palette-sidebar {
        position: fixed;
        top: auto !important;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100% !important;
        max-width: 100vw;
        height: 50vh !important;
        z-index: 200;
        border-radius: 16px 16px 0 0;
        transform: translateY(100%);
        transition: transform 0.3s cubic-bezier(0.22, 1, 0.36, 1);
        box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.3);
    }

    .tag-palette-sidebar.open {
        transform: translateY(0);
    }

    /* タグパレットボタン: 画面最下部にぴったり配置 → ボトムバー化 */
    .mobile-bottom-bar {
        display: flex !important;
        align-items: center;
        gap: 8px;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        padding: 8px 12px;
        padding-bottom: max(8px, env(safe-area-inset-bottom));
        margin: 0;
        border-top: 1px solid var(--border-default);
        background: var(--bg-surface);
        z-index: 79;
        box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.15);
    }

    /* Generate ボタン（メイン） */
    .mobile-generate-btn {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        padding: 12px 16px;
        border: none;
        border-radius: var(--radius-md);
        background: linear-gradient(135deg, #6366f1, #8b5cf6);
        color: #fff;
        font-size: 0.95rem;
        font-weight: 700;
        letter-spacing: 0.03em;
        cursor: pointer;
        transition: all 0.2s ease;
        box-shadow: 0 2px 8px rgba(99, 102, 241, 0.3);
    }

    .mobile-generate-btn:active {
        transform: scale(0.97);
        box-shadow: 0 1px 4px rgba(99, 102, 241, 0.2);
    }

    .mobile-generate-btn .generate-icon {
        width: 16px;
        height: 16px;
    }

    /* 生成中のスタイル */
    .mobile-generate-btn.generating {
        background: linear-gradient(135deg, #4b5563, #6b7280);
        box-shadow: none;
        pointer-events: none;
    }

    /* パレットボタン（サブ） */
    .mobile-palette-btn {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 6px;
        padding: 12px 14px;
        border: 1px solid var(--border-default);
        border-radius: var(--radius-md);
        background: var(--bg-elevated);
        color: var(--text-primary);
        font-size: 0.88rem;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.2s ease;
    }

    .mobile-palette-btn:active {
        background: var(--bg-hover);
    }

    .mobile-palette-btn svg {
        width: 16px;
        height: 16px;
        opacity: 0.7;
    }

    /* タグパレット開いてるときはボトムバー非表示 */
    .tag-palette-sidebar.open~.main-content .mobile-bottom-bar,
    .mobile-bottom-bar.palette-open {
        display: none !important;
    }

    /* ドラッグハンドル (モバイル時のみ表示) */
    .palette-drag-handle {
        display: flex !important;
        align-items: center;
        justify-content: center;
        width: 100%;
        padding: 10px 0 4px;
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
    }

    .palette-drag-bar {
        display: block;
        width: 40px;
        height: 4px;
        border-radius: 2px;
        background: var(--text-muted);
        opacity: 0.4;
        transition: opacity 0.2s;
    }

    .palette-drag-handle:active .palette-drag-bar {
        opacity: 0.7;
    }

    /* モバイル: サイドバーヘッダーをタップ可能バーに変身 */
    .tag-palette-sidebar-header {
        flex-direction: column;
        padding: 0 16px 8px;
        cursor: pointer;
    }

    /* モバイル: ×ボタン非表示 (バータップで閉じるので不要) */
    .tag-palette-close-btn {
        display: none !important;
    }

    /* ========== img2img: 2カラム → 1カラム ========== */
    .img2img-layout {
        grid-template-columns: 1fr;
        overflow-y: auto;
        padding: 10px;
        gap: 12px;
    }

    .img2img-left {
        overflow-y: visible;
    }

    .img2img-input-image {
        max-height: 250px;
    }

    .img2img-settings-bar {
        flex-wrap: wrap;
    }

    /* ========== ギャラリー ========== */
    .gallery-grid {
        grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
        gap: 8px;
    }

    .gallery-toolbar {
        flex-direction: column;
        align-items: stretch;
        gap: 8px;
    }

    .gallery-sort-group {
        flex-wrap: wrap;
    }

    .gallery-sort-btn {
        flex: 1;
        min-width: 0;
        justify-content: center;
        padding: 5px 8px;
        font-size: 0.72rem;
    }

    /* ========== ライトボックス ========== */
    .simple-lightbox-image {
        max-width: 96vw;
        max-height: 80vh;
    }

    /* ========== プリセットポップアップ ========== */
    .preset-popup {
        position: fixed;
        left: 4px;
        right: 4px;
        bottom: 4px;
        top: auto;
        max-height: 70vh;
        width: auto;
        border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    }

    /* ========== ネガティブプロンプト ========== */
    .neg-label {
        font-size: 1rem;
    }

    /* ========== スクロールバー (モバイルでは薄く) ========== */
    ::-webkit-scrollbar {
        width: 3px;
        height: 3px;
    }
}

/* ── 追加: 横向き + 小画面 (スマホ横持ち) ── */
@media (orientation: landscape) and (max-height: 500px) {
    .app-header {
        padding: 4px 12px;
    }

    .header-tabs {
        padding: 2px;
    }

    .header-tab {
        padding: 4px 12px;
        font-size: 0.75rem;
    }

    .brand-name {
        font-size: 0.9rem;
    }

    .brand-sub {
        display: none;
    }

    /* ヘッダーステータス縮小 */
    .header-status .status-text {
        display: none;
    }

    /* プレビュー: 横持ち時は高さ最大化 */
    .preview-area {
        padding: 8px;
    }

    .result-image,
    .live-preview-image {
        max-height: calc(100vh - 120px);
    }

    /* 設定バー: 高さ節約 */
    .settings-bar {
        padding: 4px 8px;
    }

    .sbar-label {
        font-size: 0.6rem;
    }
}

/* ── Generateボタン: モバイルstickyバー ── */
@media (max-width: 768px) {
    .mobile-generate-sticky {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 80;
        padding: 8px 12px;
        background: var(--bg-surface);
        border-top: 1px solid var(--border-subtle);
        box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.3);
        display: flex;
        gap: 8px;
    }

    .mobile-generate-sticky .generate-btn {
        flex: 1;
    }
}

/* ── モバイルボトムバーはデスクトップで非表示 ── */
.mobile-bottom-bar {
    display: none;
}

/* ── img2img 工事中オーバーレイ ── */
.img2img-construction {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    min-height: 60vh;
    gap: 20px;
    text-align: center;
    padding: 40px 20px;
}

.img2img-construction-icon {
    font-size: 3rem;
    animation: construction-bounce 1.5s ease-in-out infinite;
}

@keyframes construction-bounce {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(-10px);
    }
}

.img2img-construction-title {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-primary);
    letter-spacing: 0.05em;
}

.img2img-construction-sub {
    font-size: 0.9rem;
    color: var(--text-secondary);
}

.img2img-construction-image {
    max-width: 280px;
    max-height: 280px;
    border-radius: var(--radius-lg);
    object-fit: contain;
    box-shadow: var(--shadow-md);
}