/* ===== タブレット対応 (768px以下) ===== */
@media (max-width: 768px) and (min-width: 481px) {
    /* メインレイアウトの最適化 */
    .main-content {
        flex-direction: column;
        padding: 1.5rem;
        gap: 1.5rem;
        max-width: none;
    }
    
    /* ツールバーを上部に配置 */
    .toolbar {
        width: 100%;
        position: static;
        order: 2;
        padding: 1.5rem;
        border-radius: 12px;
    }
    
    .canvas-area {
        order: 1;
    }
    
    /* ヘッダーの最適化 */
    .header-content {
        padding: 1.25rem 1.5rem;
        flex-wrap: wrap;
        gap: 1rem;
    }
    
    .logo {
        font-size: 1.4rem;
    }
    
    .header-actions {
        gap: 0.75rem;
        flex: 1;
        justify-content: flex-end;
        min-width: 200px;
    }
    
    .header-actions .btn {
        min-width: 100px;
    }
    
    .btn {
        padding: 0.75rem 1.25rem;
        font-size: 0.9rem;
    }
    
    .btn-text {
        display: inline;
    }
    
    .btn-icon {
        font-size: 1.1rem;
    }
    
    /* キャンバスエリアの最適化 */
    .canvas-container {
        padding: 1.5rem;
        min-height: 400px;
        border-radius: 12px;
    }
    
    .main-canvas {
        border-radius: 8px;
    }
    
    /* ファイルアップロードエリアの最適化 */
    .file-upload-area {
        padding: 2.5rem 2rem;
        border-radius: 12px;
        max-width: 500px;
        margin: 0 auto;
    }
    
    .upload-icon {
        font-size: 3rem;
    }
    
    .upload-text {
        font-size: 1.1rem;
    }
    
    .upload-info {
        font-size: 0.95rem;
    }
    
    /* ツールバーをホリゾンタルレイアウトに */
    .tool-group {
        margin-bottom: 1.5rem;
    }
    
    .tool-group:first-child {
        display: flex;
        flex-wrap: wrap;
        gap: 0.75rem;
        justify-content: center;
    }
    
    .tool-group:first-child .tool-btn {
        display: inline-flex;
        width: auto;
        min-width: 140px;
        margin: 0;
        padding: 1rem 1.25rem;
        border-radius: 10px;
        flex: 1;
        max-width: 180px;
    }
    
    .tool-group:last-child {
        text-align: center;
    }
    
    .tool-group:last-child .btn {
        width: 100%;
        max-width: 300px;
        justify-content: center;
        margin: 0 auto;
        padding: 1rem 2rem;
        font-size: 1rem;
    }
    
    /* ツール設定パネルの最適化 */
    .tool-settings {
        padding: 1.5rem;
        border-radius: 12px;
    }
    
    .setting-title {
        font-size: 1.2rem;
        text-align: center;
        margin-bottom: 1.25rem;
    }
    
    .setting-description {
        text-align: center;
        font-size: 1rem;
        margin-bottom: 1.5rem;
    }
    
    .setting-actions {
        justify-content: center;
        gap: 1rem;
        flex-wrap: wrap;
    }
    
    .setting-actions .btn {
        min-width: 140px;
        padding: 0.875rem 1.5rem;
    }
    
    /* リサイズ入力の2列レイアウト維持 */
    .resize-inputs {
        grid-template-columns: 1fr 1fr;
        gap: 1.5rem;
        max-width: 400px;
        margin: 0 auto;
    }
    
    /* スライダーの最適化 */
    .slider-container {
        max-width: 400px;
        margin: 1rem auto;
    }
    
    .slider-value {
        font-size: 1.1rem;
        font-weight: 600;
    }
    
    /* 情報表示の最適化 */
    .image-info,
    .resize-info,
    .center-crop-info {
        max-width: 400px;
        margin: 1rem auto;
        border-radius: 10px;
    }
    
    /* プレビューエリアの最適化 */
    .preview-area {
        max-width: 600px;
        margin: 1.5rem auto;
        gap: 1.5rem;
    }
    
    .preview-image {
        max-height: 200px;
        border-radius: 8px;
    }
}

/* ===== モバイル対応 (480px以下) ===== */
@media (max-width: 480px) {
    /* ヘッダーの最適化 */
    .header-content {
        flex-direction: column;
        gap: 0.75rem;
        text-align: center;
        padding: 0.75rem 1rem;
    }
    
    .logo {
        font-size: 1.25rem;
    }
    
    .header-actions {
        justify-content: center;
        gap: 0.5rem;
    }
    
    .header-actions .btn {
        flex: 1;
        max-width: 120px;
        justify-content: center;
    }
    
    /* メインコンテンツの最適化 */
    .main-content {
        padding: 0.5rem;
        gap: 0.75rem;
    }
    
    /* ツールバーの最適化 */
    .toolbar {
        padding: 1rem;
        border-radius: 12px;
    }
    
    .tool-group {
        margin-bottom: 1.5rem;
    }
    
    .tool-group-title {
        font-size: 0.9rem;
        margin-bottom: 0.75rem;
        font-weight: 700;
        text-align: center;
    }
    
    .tool-btn {
        padding: 1rem;
        font-size: 0.9rem;
        border-radius: 12px;
        margin-bottom: 0.75rem;
    }
    
    .tool-icon {
        font-size: 1.3rem;
    }
    
    .tool-text {
        font-size: 0.9rem;
        font-weight: 600;
    }
    
    /* キャンバスエリアの最適化 */
    .canvas-container {
        padding: 0.75rem;
        min-height: 280px;
        border-radius: 12px;
        position: relative;
        overflow: visible; /* タッチ操作のため */
    }
    
    .main-canvas {
        border-radius: 8px;
    }
    
    /* ファイルアップロードエリアの最適化 */
    .file-upload-area {
        padding: 2rem 1rem;
        border-radius: 12px;
        border-width: 2px;
    }
    
    .upload-icon {
        font-size: 2.5rem;
        margin-bottom: 1rem;
    }
    
    .upload-text {
        font-size: 1rem;
        line-height: 1.5;
    }
    
    .upload-info {
        font-size: 0.85rem;
        margin-top: 0.75rem;
    }
    
    .file-select-btn {
        font-weight: 600;
        text-decoration-thickness: 2px;
    }
    
    /* ツール設定パネルの最適化 */
    .tool-settings {
        padding: 1.25rem;
        border-radius: 12px;
    }
    
    .setting-title {
        font-size: 1.1rem;
        margin-bottom: 1rem;
        text-align: center;
    }
    
    .setting-description {
        text-align: center;
        margin-bottom: 1.25rem;
    }
    
    .setting-row {
        flex-direction: column;
        align-items: stretch;
        gap: 0.75rem;
        margin-bottom: 1rem;
    }
    
    .setting-actions {
        flex-direction: column;
        gap: 0.75rem;
    }
    
    .setting-actions .btn {
        width: 100%;
        justify-content: center;
        min-height: 48px;
    }
    
    /* リサイズ入力の最適化 */
    .resize-inputs {
        grid-template-columns: 1fr;
        gap: 1rem;
    }
    
    .input-group {
        gap: 0.75rem;
    }
    
    .setting-label {
        font-size: 1rem;
        font-weight: 600;
        text-align: center;
    }
    
    /* スライダーコンテナの最適化 */
    .slider-container {
        gap: 0.75rem;
        margin: 0.5rem 0;
    }
    
    .slider-value {
        font-size: 1.1rem;
        font-weight: 700;
        color: var(--primary-color);
    }
    
    /* エラーメッセージとローディングの最適化 */
    .error-message,
    .success-message,
    .info-message,
    .warning-message {
        position: fixed;
        top: 20px;
        left: 10px;
        right: 10px;
        max-width: none;
        border-radius: 12px;
        padding: 1rem 1.25rem;
    }
    
    .error-content,
    .success-content,
    .info-content,
    .warning-content {
        flex-wrap: wrap;
        gap: 0.5rem;
    }
    
    .error-text,
    .success-text,
    .info-text,
    .warning-text {
        min-width: 100%;
        text-align: center;
        font-size: 0.95rem;
    }
    
    .loading-overlay {
        backdrop-filter: blur(8px);
    }
    
    .loading-text {
        font-size: 1.1rem;
        font-weight: 600;
        margin-top: 1.5rem;
    }
    
    .loading-spinner {
        width: 48px;
        height: 48px;
        border-width: 5px;
    }
    
    /* ダウンロード設定の最適化 */
    .download-settings {
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        right: auto;
        margin-top: 0;
        width: 92%;
        max-width: 320px;
        border-radius: 16px;
        max-height: 85vh;
        overflow-y: auto;
    }
    
    /* 情報表示の最適化 */
    .image-info,
    .resize-info,
    .center-crop-info {
        border-radius: 8px;
        padding: 1rem;
        margin-top: 1rem;
    }
    
    .info-row {
        margin-bottom: 0.75rem;
        flex-wrap: wrap;
        gap: 0.25rem;
    }
    
    .info-label {
        font-weight: 600;
        min-width: 100px;
    }
    
    .info-value {
        font-family: monospace;
        font-size: 0.9rem;
    }
    
    /* プレビューエリアの最適化 */
    .preview-area {
        flex-direction: column;
        gap: 1rem;
        margin-top: 1rem;
    }
    
    .preview-before,
    .preview-after {
        flex: none;
    }
    
    .preview-title {
        font-size: 1rem;
        font-weight: 600;
        margin-bottom: 0.75rem;
    }
    
    .preview-image {
        max-height: 150px;
        border-radius: 8px;
    }
}

/* ===== 大画面対応 (1200px以上) ===== */
@media (min-width: 1200px) {
    .main-content {
        max-width: 1400px;
        padding: 3rem;
    }
    
    .toolbar {
        width: 250px;
    }
    
    .canvas-container {
        min-height: 500px;
    }
    
    .tool-btn {
        padding: 1.25rem;
    }
    
    .tool-icon {
        font-size: 1.4rem;
    }
    
    .tool-text {
        font-size: 1rem;
    }
}

/* ===== タッチデバイス対応 ===== */
@media (hover: none) and (pointer: coarse) {
    /* タッチターゲットサイズを大きく（Apple HIG推奨44px以上） */
    .btn {
        min-height: 48px;
        min-width: 48px;
        padding: 0.875rem 1.5rem;
        font-size: 1rem;
    }
    
    .tool-btn {
        min-height: 56px;
        padding: 1.25rem;
        margin-bottom: 0.75rem;
    }
    
    .tool-icon {
        font-size: 1.5rem;
    }
    
    .tool-text {
        font-size: 1rem;
        font-weight: 600;
    }
    
    /* 選択ハンドルを大きく（タッチしやすく） */
    .selection-handle {
        width: 20px;
        height: 20px;
        border-width: 3px;
    }
    
    .handle-nw, .handle-ne, .handle-sw, .handle-se {
        top: -10px;
        left: -10px;
    }
    
    .handle-ne, .handle-se {
        left: auto;
        right: -10px;
    }
    
    .handle-sw, .handle-se {
        top: auto;
        bottom: -10px;
    }
    
    .handle-n, .handle-s {
        top: -10px;
        width: 24px;
        height: 16px;
        border-radius: 8px;
    }
    
    .handle-s {
        top: auto;
        bottom: -10px;
    }
    
    .handle-w, .handle-e {
        left: -10px;
        width: 16px;
        height: 24px;
        border-radius: 8px;
    }
    
    .handle-e {
        left: auto;
        right: -10px;
    }
    
    /* 選択ボックスのタッチ操作改善 */
    .selection-box {
        border-width: 3px;
        min-width: 60px;
        min-height: 60px;
    }
    
    /* ホバー効果を無効化 */
    .btn:hover,
    .tool-btn:hover,
    .file-upload-area:hover,
    .selection-handle:hover {
        transform: none;
        background: inherit;
        box-shadow: inherit;
    }
    
    /* タップ時のフィードバック強化 */
    .btn:active {
        transform: scale(0.96);
        transition: transform 0.1s ease;
    }
    
    .tool-btn:active {
        background: var(--primary-color);
        color: white;
        transform: scale(0.98);
        transition: all 0.1s ease;
    }
    
    .selection-handle:active {
        transform: scale(1.3);
        background: #FF5252;
        transition: all 0.1s ease;
    }
    
    /* ファイルアップロードエリアのタッチ改善 */
    .file-upload-area {
        padding: 2.5rem 1.5rem;
        border-width: 3px;
    }
    
    .file-upload-area:active {
        background: rgba(255, 107, 107, 0.15);
        border-color: var(--primary-color);
        transform: scale(0.99);
    }
    
    /* 入力フィールドのタッチ改善 */
    .setting-input {
        min-height: 48px;
        padding: 0.875rem 1rem;
        font-size: 1rem;
        border-width: 2px;
    }
    
    .setting-input:focus {
        border-width: 3px;
    }
    
    /* スライダーのタッチ改善 */
    .slider {
        height: 8px;
    }
    
    .slider::-webkit-slider-thumb {
        width: 28px;
        height: 28px;
        border-width: 3px;
    }
    
    .slider::-moz-range-thumb {
        width: 28px;
        height: 28px;
        border-width: 3px;
    }
    
    /* チェックボックスのタッチ改善 */
    .setting-checkbox input[type="checkbox"] {
        width: 24px;
        height: 24px;
        margin-right: 0.75rem;
    }
    
    .setting-checkbox {
        padding: 0.5rem;
        min-height: 48px;
        align-items: center;
    }
}

/* ===== 高解像度ディスプレイ対応 ===== */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    .main-canvas {
        image-rendering: -webkit-optimize-contrast;
        image-rendering: crisp-edges;
    }
}

/* ===== iOS Safari対応 ===== */
@supports (-webkit-touch-callout: none) {
    /* iOSでのタッチ操作改善 */
    .selection-box,
    .selection-handle {
        -webkit-touch-callout: none;
        -webkit-user-select: none;
        user-select: none;
    }
    
    /* iOSでのスクロール防止 */
    .canvas-container {
        -webkit-overflow-scrolling: touch;
        overflow: hidden;
    }
    
    /* iOSでのズーム無効化 */
    .setting-input,
    .download-filename {
        font-size: 16px; /* iOS Safariでのズーム防止 */
    }
}

/* ===== Android対応 ===== */
@media screen and (max-width: 768px) {
    /* Androidでのタッチ遅延削除 */
    .btn,
    .tool-btn,
    .selection-handle,
    .file-upload-area {
        touch-action: manipulation;
    }
    
    /* Androidでのスクロール改善 */
    .tool-settings,
    .download-dialog .dialog-body {
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }
}

/* ===== 横向き対応（モバイル） ===== */
@media screen and (max-width: 768px) and (orientation: landscape) {
    .main-content {
        flex-direction: row;
        padding: 0.75rem;
        gap: 1rem;
    }
    
    .toolbar {
        width: 200px;
        order: 1;
        position: static;
        max-height: calc(100vh - 140px);
        overflow-y: auto;
    }
    
    .canvas-area {
        order: 2;
        flex: 1;
    }
    
    .canvas-container {
        min-height: calc(100vh - 200px);
        padding: 1rem;
    }
    
    .tool-group:first-child {
        display: block;
    }
    
    .tool-group:first-child .tool-btn {
        width: 100%;
        margin-bottom: 0.5rem;
        padding: 0.75rem;
    }
    
    .header-content {
        padding: 0.75rem 1rem;
    }
    
    .file-upload-area {
        padding: 1.5rem 1rem;
    }
}

/* ===== 縦向き対応（タブレット） ===== */
@media screen and (min-width: 481px) and (max-width: 768px) and (orientation: portrait) {
    .canvas-container {
        min-height: 350px;
    }
    
    .tool-group:first-child {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
        gap: 0.75rem;
    }
    
    .tool-group:first-child .tool-btn {
        margin: 0;
    }
}

/* ===== タブレット専用最適化 (769px-1024px) ===== */
@media screen and (min-width: 769px) and (max-width: 1024px) {
    /* タブレット向けレイアウト */
    .main-content {
        max-width: 1000px;
        padding: 2rem;
        gap: 2rem;
    }
    
    .toolbar {
        width: 220px;
        padding: 1.75rem;
    }
    
    .tool-btn {
        padding: 1.125rem;
        font-size: 0.95rem;
        margin-bottom: 0.75rem;
    }
    
    .tool-icon {
        font-size: 1.3rem;
    }
    
    .tool-text {
        font-size: 0.95rem;
    }
    
    /* キャンバスエリアの最適化 */
    .canvas-container {
        min-height: 450px;
        padding: 2rem;
    }
    
    /* ツール設定パネルの最適化 */
    .tool-settings {
        padding: 1.75rem;
    }
    
    .setting-title {
        font-size: 1.25rem;
    }
    
    .setting-description {
        font-size: 1rem;
    }
    
    /* リサイズ入力の最適化 */
    .resize-inputs {
        grid-template-columns: 1fr 1fr;
        gap: 1.5rem;
        max-width: 500px;
    }
    
    /* スライダーの最適化 */
    .slider-container {
        max-width: 500px;
    }
    
    /* ボタンサイズの調整 */
    .btn {
        padding: 0.875rem 1.5rem;
        font-size: 0.95rem;
    }
    
    .setting-actions .btn {
        min-width: 150px;
    }
    
    /* ファイルアップロードエリア */
    .file-upload-area {
        max-width: 600px;
        padding: 3rem 2.5rem;
    }
    
    .upload-icon {
        font-size: 3.5rem;
    }
    
    .upload-text {
        font-size: 1.2rem;
    }
    
    .upload-info {
        font-size: 1rem;
    }
}

/* ===== タブレット横向き専用 (1024px以下) ===== */
@media screen and (max-width: 1024px) and (orientation: landscape) and (min-height: 600px) {
    .main-content {
        flex-direction: row;
        height: calc(100vh - 120px);
    }
    
    .toolbar {
        width: 240px;
        height: 100%;
        overflow-y: auto;
        position: sticky;
        top: 0;
    }
    
    .canvas-area {
        flex: 1;
        display: flex;
        flex-direction: column;
        height: 100%;
    }
    
    .canvas-container {
        flex: 1;
        min-height: 0;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .tool-settings {
        max-height: 200px;
        overflow-y: auto;
    }
    
    /* ヘッダーの最適化 */
    .header-content {
        padding: 1rem 2rem;
    }
    
    /* フッターの最適化 */
    .footer {
        padding: 1.5rem 2rem;
    }
}

/* ===== 大型タブレット対応 (1024px-1199px) ===== */
@media screen and (min-width: 1025px) and (max-width: 1199px) {
    .main-content {
        max-width: 1150px;
        padding: 2.5rem;
    }
    
    .toolbar {
        width: 240px;
    }
    
    .canvas-container {
        min-height: 500px;
    }
    
    /* より大きなタッチターゲット */
    .btn {
        min-height: 44px;
        padding: 0.875rem 1.75rem;
    }
    
    .tool-btn {
        min-height: 52px;
        padding: 1.25rem;
    }
    
    .selection-handle {
        width: 16px;
        height: 16px;
    }
}

/* ===== 小さな画面での追加最適化 ===== */
@media screen and (max-width: 360px) {
    .header-content {
        padding: 0.5rem;
    }
    
    .logo {
        font-size: 1.1rem;
    }
    
    .header-actions .btn {
        padding: 0.5rem 0.75rem;
        font-size: 0.8rem;
        min-width: 80px;
    }
    
    .main-content {
        padding: 0.25rem;
        gap: 0.5rem;
    }
    
    .toolbar {
        padding: 0.75rem;
    }
    
    .tool-btn {
        padding: 0.875rem;
        font-size: 0.85rem;
    }
    
    .tool-icon {
        font-size: 1.2rem;
    }
    
    .canvas-container {
        padding: 0.5rem;
        min-height: 250px;
    }
    
    .file-upload-area {
        padding: 1.5rem 0.75rem;
    }
    
    .upload-text {
        font-size: 0.9rem;
    }
    
    .upload-info {
        font-size: 0.8rem;
    }
    
    .tool-settings {
        padding: 1rem;
    }
    
    .setting-title {
        font-size: 1rem;
    }
    
    .setting-actions .btn {
        padding: 0.75rem 1rem;
        font-size: 0.9rem;
    }
}

/* ===== ダークモード対応 ===== */
@media (prefers-color-scheme: dark) {
    :root {
        --background-color: #1a1a1a;
        --text-color: #e9ecef;
        --border-color: #343a40;
    }
    
    body {
        background-color: var(--background-color);
        color: var(--text-color);
    }
    
    .header,
    .toolbar,
    .canvas-container,
    .tool-settings,
    .download-settings {
        background: #2d3748;
        border-color: var(--border-color);
    }
    
    .btn-secondary {
        background: #4a5568;
        color: var(--text-color);
        border-color: var(--border-color);
    }
    
    .btn-secondary:hover:not(:disabled) {
        background: #5a6578;
    }
    
    .setting-input {
        background: #4a5568;
        color: var(--text-color);
        border-color: var(--border-color);
    }
    
    .file-upload-area {
        border-color: var(--border-color);
    }
    
    .file-upload-area:hover,
    .file-upload-area.dragover {
        background: rgba(255, 107, 107, 0.1);
    }
}

/* ===== 印刷対応 ===== */
@media print {
    .header,
    .toolbar,
    .footer,
    .loading-overlay,
    .error-message,
    .success-message {
        display: none !important;
    }
    
    .main-content {
        flex-direction: column;
        padding: 0;
        max-width: none;
    }
    
    .canvas-area {
        width: 100%;
    }
    
    .canvas-container {
        box-shadow: none;
        border: 1px solid #000;
        page-break-inside: avoid;
    }
}

/* ===== アクセシビリティ対応 ===== */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    
    .loading-spinner {
        animation: none;
        border: 4px solid var(--primary-color);
    }
}

/* ===== 高コントラスト対応 ===== */
@media (prefers-contrast: high) {
    :root {
        --primary-color: #000;
        --secondary-color: #000;
        --accent-color: #000;
        --text-color: #000;
        --border-color: #000;
    }
    
    .btn-primary {
        background: #000;
        color: #fff;
        border: 2px solid #000;
    }
    
    .btn-secondary {
        background: #fff;
        color: #000;
        border: 2px solid #000;
    }
    
    .selection-box {
        border: 3px solid #000;
        background: rgba(0, 0, 0, 0.1);
    }
    
    .selection-handle {
        background: #000;
        border: 2px solid #fff;
    }
}