/* ==========================================================================
   ARフォトフレーム - スタイルシート
   テーマ: 深海・海洋インスパイア (フレーム画像に合わせたダークテーマ)
   ========================================================================== */

/* --- デザイントークン --- */
:root {
    --color-bg: #060b18;
    --color-bg-deep: #030610;
    --color-primary: #7c5ce7;
    --color-primary-light: #a78bfa;
    --color-accent-gold: #f0c040;
    --color-accent-teal: #2dd4bf;
    --color-text: #eef0f6;
    --color-text-muted: #7b8bb2;
    --color-surface: rgba(255, 255, 255, 0.06);
    --color-surface-border: rgba(124, 92, 231, 0.15);
    --color-glass: rgba(22 212 45 / 0.30);
    --font-main: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --radius-lg: 20px;
    --radius-md: 14px;
    --radius-sm: 10px;
    --shadow-glow: 0 0 40px rgba(124, 92, 231, 0.2);
    --shadow-deep: 0 12px 40px rgba(0, 0, 0, 0.5);
    --transition-smooth: cubic-bezier(0.16, 1, 0.3, 1);
}

/* --- リセットとベース --- */
*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-family: var(--font-main);
    background: var(--color-bg-deep);
    color: var(--color-text);
    touch-action: manipulation;
    -webkit-text-size-adjust: 100%;
    -webkit-font-smoothing: antialiased;
}

/* ==========================================================================
   ローディング画面
   ========================================================================== */
#loading-screen {
    position: fixed;
    inset: 0;
    background: radial-gradient(ellipse at 50% 80%, #0d1a3a 0%, var(--color-bg-deep) 70%);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    transition: opacity 0.6s ease, visibility 0.6s ease;
}

#loading-screen.fade-out {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

.loading-content {
    text-align: center;
}

/* 3重リングアニメーション */
.loading-ring {
    display: inline-block;
    position: relative;
    width: 64px;
    height: 64px;
    margin-bottom: 20px;
}

.loading-ring div {
    position: absolute;
    border: 3px solid transparent;
    border-radius: 50%;
    animation: loading-ring-spin 1.4s ease-in-out infinite;
}

.loading-ring div:nth-child(1) {
    width: 64px;
    height: 64px;
    border-top-color: var(--color-primary);
    animation-delay: -0.3s;
}

.loading-ring div:nth-child(2) {
    width: 48px;
    height: 48px;
    top: 8px;
    left: 8px;
    border-right-color: var(--color-accent-teal);
    animation-delay: -0.15s;
    animation-direction: reverse;
}

.loading-ring div:nth-child(3) {
    width: 32px;
    height: 32px;
    top: 16px;
    left: 16px;
    border-bottom-color: var(--color-accent-gold);
}

@keyframes loading-ring-spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.loading-text {
    color: var(--color-text-muted);
    font-size: 13px;
    font-weight: 400;
    letter-spacing: 1px;
    text-transform: uppercase;
}

/* ==========================================================================
   カメラ画面
   ========================================================================== */
#camera-screen {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: var(--color-bg-deep);
    position: relative;
}

#camera-container {
    position: relative;
    width: 100%;
    aspect-ratio: 9 / 16;
    max-height: calc(100dvh - 110px);
    max-height: calc(100vh - 110px);
    overflow: hidden;
    background: #000;
}

/* 横長画面の場合、高さ基準で幅を計算 */
@media (min-aspect-ratio: 9/16) {
    #camera-container {
        width: auto;
        height: calc(100dvh - 110px);
        height: calc(100vh - 110px);
    }
}

#camera-video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    transform: none; /* アウトカメラなのでミラー反転は不要 */
}

#frame-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: fill;
    pointer-events: none;
    z-index: 10;
}

/* ==========================================================================
   フレーム選択コントロール
   ========================================================================== */
#frame-toggle-btn {
    position: fixed;
    top: max(20px, env(safe-area-inset-top));
    right: 20px;
    padding: 10px 16px;
    border-radius: 24px;
    background: var(--color-glass);
    border: 1px solid var(--color-surface-border);
    color: var(--color-text);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    z-index: 30;
    cursor: pointer;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    transition: transform 0.2s, background 0.2s;
    font-family: var(--font-main);
    font-size: 14px;
    font-weight: 500;
}

#frame-toggle-btn:active {
    transform: scale(0.9);
    background: rgba(10, 15, 35, 0.9);
}

#frame-selector {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 90px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    gap: 16px;
    padding: max(80px, calc(80px + env(safe-area-inset-top))) 0 max(20px, env(safe-area-inset-bottom)) 0;
    background: linear-gradient(to left, rgba(3, 6, 16, 0.9) 0%, rgba(3, 6, 16, 0.5) 100%);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    overflow-y: auto;
    z-index: 25;
    transform: translateX(100%);
    transition: transform 0.4s var(--transition-smooth);
    /* スクロールバー非表示 */
    -ms-overflow-style: none;
    scrollbar-width: none;
}

#frame-selector.open {
    transform: translateX(0);
}

#frame-selector::-webkit-scrollbar {
    display: none;
}

.frame-thumb {
    width: 56px;
    height: 74px; /* 約9:16の比率 */
    border-radius: var(--radius-sm);
    border: 2px solid rgba(255, 255, 255, 0.4);
    background: rgba(0, 0, 0, 0.5);
    overflow: hidden;
    cursor: pointer;
    transition: transform 0.2s, border-color 0.2s;
    flex-shrink: 0;
    padding: 0;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.frame-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    pointer-events: none;
}

.frame-thumb.active {
    border-color: var(--color-primary-light);
    transform: scale(1.1) translateY(-4px);
    box-shadow: 0 8px 16px rgba(124, 92, 231, 0.4);
}

/* ==========================================================================
   撮影コントロール
   ========================================================================== */
#controls {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px 0 max(20px, env(safe-area-inset-bottom));
    background: linear-gradient(to top, rgba(3, 6, 16, 0.9) 0%, rgba(3, 6, 16, 0.5) 60%, transparent 100%);
    z-index: 20;
}

#capture-btn {
    position: relative;
    width: 76px;
    height: 76px;
    border: none;
    background: transparent;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    outline: none;
    -webkit-appearance: none;
    transition: transform 0.15s var(--transition-smooth);
}

.capture-ring {
    position: absolute;
    width: 76px;
    height: 76px;
    border-radius: 50%;
    border: 3px solid rgba(255, 255, 255, 0.8);
    box-shadow: 0 0 20px rgba(124, 92, 231, 0.3), inset 0 0 20px rgba(124, 92, 231, 0.1);
    transition: transform 0.2s var(--transition-smooth), border-color 0.2s;
    animation: pulse-ring 3s ease-in-out infinite;
}

.capture-inner {
    position: relative;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(145deg, #ffffff, #e8e8f0);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.15s var(--transition-smooth), background 0.15s;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.2);
}

#capture-btn:active .capture-inner {
    transform: scale(0.82);
    background: linear-gradient(145deg, #d0d0d8, #c0c0c8);
}

#capture-btn:active .capture-ring {
    transform: scale(1.08);
    border-color: var(--color-primary-light);
}

/* キャプチャボタンの脈動アニメーション */
@keyframes pulse-ring {
    0%, 100% { box-shadow: 0 0 0 0 rgba(124, 92, 231, 0.4); }
    50% { box-shadow: 0 0 0 8px rgba(124, 92, 231, 0); }
}

/* ==========================================================================
   フラッシュエフェクト
   ========================================================================== */
#flash-overlay {
    position: fixed;
    inset: 0;
    background: #fff;
    opacity: 0;
    pointer-events: none;
    z-index: 50;
    transition: opacity 0.05s;
}

#flash-overlay.active {
    opacity: 0.85;
    transition: opacity 0.02s;
}

/* ==========================================================================
   モーダル共通
   ========================================================================== */
#result-modal,
#error-modal {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
}

.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(3, 6, 16, 0.88);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
}

.modal-content {
    position: relative;
    width: 100%;
    max-width: 380px;
    max-height: 88vh;
    max-height: 88dvh;
    background: linear-gradient(160deg, #161c33 0%, #0e1225 100%);
    border: 1px solid var(--color-surface-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-deep), var(--shadow-glow);
    overflow: hidden;
    animation: modal-enter 0.4s var(--transition-smooth);
}

@keyframes modal-enter {
    from {
        opacity: 0;
        transform: scale(0.9) translateY(24px);
    }
    to {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

/* --- 結果モーダル --- */
.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 18px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.modal-header h2 {
    font-size: 15px;
    font-weight: 600;
    letter-spacing: 0.3px;
}

#close-modal {
    background: var(--color-surface);
    border: 1px solid rgba(255, 255, 255, 0.06);
    color: var(--color-text-muted);
    width: 34px;
    height: 34px;
    border-radius: 50%;
    font-size: 18px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, color 0.2s, transform 0.2s;
    line-height: 1;
}

#close-modal:active {
    background: rgba(255, 255, 255, 0.12);
    color: var(--color-text);
    transform: scale(0.92);
}

.modal-body {
    padding: 14px 18px 22px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 14px;
    overflow-y: auto;
    max-height: calc(88vh - 56px);
    max-height: calc(88dvh - 56px);
    -webkit-overflow-scrolling: touch;
}

.result-image-wrapper {
    width: 100%;
    border-radius: var(--radius-md);
    overflow: hidden;
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
    border: 1px solid rgba(255, 255, 255, 0.04);
}

#result-image {
    width: 100%;
    display: block;
    /* iOS: 長押しによる画像保存を有効化 */
    -webkit-touch-callout: default !important;
    -webkit-user-select: auto !important;
    user-select: auto !important;
}

#save-instruction {
    font-size: 12.5px;
    color: var(--color-text-muted);
    text-align: center;
    line-height: 1.7;
    padding: 0 4px;
}

#download-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    width: 100%;
    padding: 15px 24px;
    background: linear-gradient(135deg, var(--color-primary) 0%, #9b59f0 100%);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 15px;
    font-weight: 600;
    font-family: var(--font-main);
    cursor: pointer;
    text-decoration: none;
    transition: transform 0.2s var(--transition-smooth), box-shadow 0.2s;
    box-shadow: 0 4px 20px rgba(124, 92, 231, 0.35);
    letter-spacing: 0.5px;
}

#download-btn:active {
    transform: scale(0.97);
    box-shadow: 0 2px 10px rgba(124, 92, 231, 0.2);
}

/* --- エラーモーダル --- */
.error-content {
    padding: 36px 24px 28px;
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
}

.error-icon {
    font-size: 40px;
    margin-bottom: 4px;
}

.error-content h2 {
    font-size: 17px;
    font-weight: 600;
}

#error-message {
    color: var(--color-text-muted);
    font-size: 13px;
    line-height: 1.7;
    white-space: pre-line;
    margin-bottom: 8px;
}

#retry-btn {
    padding: 13px 36px;
    background: var(--color-primary);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 600;
    font-family: var(--font-main);
    cursor: pointer;
    transition: transform 0.2s var(--transition-smooth);
    letter-spacing: 0.5px;
}

#retry-btn:active {
    transform: scale(0.95);
}

/* ==========================================================================
   ユーティリティ
   ========================================================================== */
.hidden {
    display: none !important;
}

#composite-canvas {
    display: none;
    position: absolute;
    pointer-events: none;
}
