/**
 * SmartRecycle AI - 全域樣式
 * Mobile-first 響應式設計
 */

/* ===== CSS 變數 ===== */
:root {
    /* 主題色 */
    --primary-color: #10b981;
    --primary-dark: #059669;
    --primary-light: #34d399;
    
    /* 漸層背景 */
    --bg-gradient: linear-gradient(135deg, #0f766e 0%, #10b981 50%, #34d399 100%);
    
    /* 中性色 */
    --bg-color: #f8fafc;
    --card-bg: rgba(255, 255, 255, 0.95);
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --border-color: rgba(0, 0, 0, 0.08);
    
    /* 狀態色 */
    --success-color: #22c55e;
    --warning-color: #f59e0b;
    --error-color: #ef4444;
    
    /* 陰影 */
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.1);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.15);
    --shadow-lg: 0 10px 40px rgba(0, 0, 0, 0.2);
    
    /* 圓角 */
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 20px;
    --radius-full: 50px;
    
    /* 動效 */
    --transition-fast: 0.15s ease;
    --transition-normal: 0.3s ease;
    --transition-slow: 0.5s ease;
    
    /* 安全區域 */
    --safe-area-top: env(safe-area-inset-top, 0px);
    --safe-area-bottom: env(safe-area-inset-bottom, 0px);
}

/* 深色模式 */
@media (prefers-color-scheme: dark) {
    :root {
        --bg-color: #0f172a;
        --card-bg: rgba(30, 41, 59, 0.95);
        --text-primary: #f1f5f9;
        --text-secondary: #94a3b8;
        --border-color: rgba(255, 255, 255, 0.1);
    }
}

/* ===== 基礎重置 ===== */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 16px;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: 'Noto Sans TC', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: var(--bg-gradient);
    min-height: 100vh;
    min-height: 100dvh;
    color: var(--text-primary);
    line-height: 1.5;
    overflow-x: hidden;
}

/* ===== 版面配置 ===== */
.app-container {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    min-height: 100dvh;
    padding-top: var(--safe-area-top);
    padding-bottom: var(--safe-area-bottom);
}

/* ===== Header ===== */
.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    position: sticky;
    top: 0;
    z-index: 100;
}

.app-logo {
    display: flex;
    align-items: center;
    gap: 8px;
}

.app-logo .logo-icon {
    font-size: 28px;
}

.app-logo .logo-text {
    font-size: 18px;
    font-weight: 700;
    color: white;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

/* 選單容器 */
.menu-container {
    position: relative;
}

.menu-toggle {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    padding: 10px 14px;
    border-radius: var(--radius-md);
    color: white;
    font-size: 20px;
    cursor: pointer;
    transition: var(--transition-fast);
}

.menu-toggle:hover {
    background: rgba(255, 255, 255, 0.3);
}

.menu-toggle:active {
    transform: scale(0.95);
}

/* 下拉選單 */
.menu-dropdown {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    background: var(--card-bg);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    min-width: 200px;
    opacity: 0;
    visibility: hidden;
    transform: translateY(-10px);
    transition: var(--transition-normal);
    overflow: hidden;
}

.menu-dropdown.show {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.menu-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    border: none;
    background: none;
    width: 100%;
    text-align: left;
    font-size: 15px;
    color: var(--text-primary);
    cursor: pointer;
    transition: var(--transition-fast);
}

.menu-item:hover {
    background: rgba(0, 0, 0, 0.05);
}

.menu-item .menu-icon {
    font-size: 18px;
}

/* Legal Links in Menu */
.menu-divider {
    height: 1px;
    background: var(--border-color);
    margin: 8px 0;
}

.menu-legal {
    padding: 12px 16px;
    font-size: 12px;
    color: var(--text-secondary);
    background: rgba(0, 0, 0, 0.02);
}

.legal-text {
    display: block;
    margin-bottom: 4px;
}

.legal-links {
    display: flex;
    gap: 8px;
    align-items: center;
}

.legal-links a {
    color: var(--primary-color);
    text-decoration: none;
    font-weight: 500;
}

.legal-links a:hover {
    text-decoration: underline;
}

/* Toggle Switch */
.toggle-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 16px;
}

.toggle-switch {
    position: relative;
    width: 48px;
    height: 26px;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-slider {
    position: absolute;
    inset: 0;
    background: #cbd5e1;
    border-radius: 26px;
    cursor: pointer;
    transition: var(--transition-fast);
}

.toggle-slider::before {
    content: '';
    position: absolute;
    width: 20px;
    height: 20px;
    left: 3px;
    top: 3px;
    background: white;
    border-radius: 50%;
    transition: var(--transition-fast);
    box-shadow: var(--shadow-sm);
}

.toggle-switch input:checked + .toggle-slider {
    background: var(--primary-color);
}

.toggle-switch input:checked + .toggle-slider::before {
    transform: translateX(22px);
}

/* ===== Main Content ===== */
.app-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20px 16px;
    gap: 20px;
}

/* 狀態徽章 */
.status-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 20px;
    border-radius: var(--radius-full);
    font-size: 14px;
    font-weight: 600;
    background: rgba(255, 255, 255, 0.95);
    color: #334155;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    border: 1.5px solid rgba(0, 0, 0, 0.08);
}

.status-badge.status-ready {
    background: #ffffff;
    color: #15803d;
    border-color: #86efac;
    box-shadow: 0 2px 10px rgba(34, 197, 94, 0.3);
}

.status-badge.status-loading {
    background: #eff6ff;
    color: #1d4ed8;
    border-color: #93c5fd;
    box-shadow: 0 2px 10px rgba(59, 130, 246, 0.3);
}

.status-badge.status-success {
    background: #f0fdf4;
    color: #15803d;
    border-color: #86efac;
    box-shadow: 0 2px 10px rgba(34, 197, 94, 0.3);
}

.status-badge.status-warning {
    background: #fffbeb;
    color: #b45309;
    border-color: #fcd34d;
    box-shadow: 0 2px 10px rgba(245, 158, 11, 0.3);
}

.status-badge.status-error {
    background: #fef2f2;
    color: #b91c1c;
    border-color: #fca5a5;
    box-shadow: 0 2px 10px rgba(239, 68, 68, 0.3);
}

/* ===== Camera Container ===== */
.camera-container {
    position: relative;
    width: 100%;
    max-width: 350px;
    aspect-ratio: 1;
    border-radius: var(--radius-lg);
    overflow: hidden;
    background: #000;
    box-shadow: var(--shadow-lg);
    border: 4px solid rgba(255, 255, 255, 0.3);
}

.camera-container video,
.camera-container canvas {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.camera-container .hidden {
    display: none;
}

/* 載入遮罩 */
.loading-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 12px;
    opacity: 0;
    visibility: hidden;
    transition: var(--transition-fast);
}

.loading-overlay.show {
    opacity: 1;
    visibility: visible;
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(255, 255, 255, 0.3);
    border-top-color: var(--primary-light);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.loading-text {
    color: white;
    font-size: 14px;
}

/* ===== 結果卡片 ===== */
.result-card {
    width: 100%;
    max-width: 350px;
    background: var(--card-bg);
    border-radius: var(--radius-lg);
    padding: 20px;
    box-shadow: var(--shadow-md);
    opacity: 0;
    transform: translateY(20px);
    transition: var(--transition-normal);
    pointer-events: none;
}

.result-card.show {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}

.result-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 16px;
}

.result-icon {
    font-size: 48px;
    line-height: 1;
}

.result-info {
    flex: 1;
}

.result-name {
    font-size: 22px;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 4px;
}

.result-confidence {
    font-size: 15px;
    color: var(--text-secondary);
    font-weight: 500;
}

/* 信心度進度條 */
.result-bar-container {
    height: 8px;
    background: var(--border-color);
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: 12px;
}

.result-bar {
    height: 100%;
    width: 0%;
    border-radius: 4px;
    transition: width 0.5s ease-out;
}

.result-description {
    font-size: 14px;
    color: var(--text-secondary);
    line-height: 1.6;
}

.result-description.low-confidence {
    color: var(--warning-color);
}

/* AI 解說區塊 (Phase 3) */
.ai-explanation {
    margin-top: 16px;
    padding-top: 16px;
    border-top: 1px solid var(--border-color);
    display: none;
}

.ai-explanation.show {
    display: block;
}

.ai-explanation-title {
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.ai-explanation-content {
    font-size: 14px;
    line-height: 1.7;
}

/* ===== 按鈕 ===== */
.btn-container {
    display: flex;
    gap: 12px;
    width: 100%;
    max-width: 350px;
}

.btn {
    flex: 1;
    padding: 16px 24px;
    border: none;
    border-radius: var(--radius-full);
    font-size: 17px;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition-normal);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.btn-primary {
    background: white;
    color: var(--primary-dark);
    box-shadow: var(--shadow-md);
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
}

.btn-primary:active {
    transform: translateY(0);
}

.btn-secondary {
    background: rgba(255, 255, 255, 0.2);
    color: white;
    border: 2px solid rgba(255, 255, 255, 0.4);
}

.btn-secondary:hover {
    background: rgba(255, 255, 255, 0.3);
}

.btn.hidden {
    display: none;
}

/* ===== Footer ===== */
.app-footer {
    padding: 16px;
    text-align: center;
    color: rgba(255, 255, 255, 0.7);
    font-size: 13px;
}

.app-footer a {
    color: white;
    text-decoration: none;
}

.app-footer a:hover {
    text-decoration: underline;
}

/* ===== 隱藏的上傳 input ===== */
.hidden-input {
    position: absolute;
    width: 0;
    height: 0;
    opacity: 0;
    pointer-events: none;
}

/* ===== 響應式調整 ===== */
@media (min-width: 768px) {
    .app-main {
        padding: 40px 24px;
    }
    
    .camera-container {
        max-width: 400px;
    }
    
    .result-card {
        max-width: 400px;
    }
    
    .btn-container {
        max-width: 400px;
    }
}

/* 桌面版調整 */
@media (min-width: 1024px) {
    .app-main {
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: center;
        align-items: flex-start;
        gap: 40px;
        padding: 60px 40px;
    }
    
    .camera-section {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 20px;
    }
    
    .result-section {
        display: flex;
        flex-direction: column;
        gap: 20px;
    }
}

/* ===== 動畫 ===== */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes pulse {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.5;
    }
}

/* 載入狀態動畫 */
.status-badge.status-loading::before {
    content: '';
    display: inline-block;
    width: 8px;
    height: 8px;
    background: currentColor;
    border-radius: 50%;
    animation: pulse 1s ease infinite;
}

/* ===== AI Chat Bottom Sheet ===== */
.ai-chat-sheet {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 70vh;
    background: #1a1a1a;
    border-radius: 24px 24px 0 0;
    transform: translateY(calc(100% - 60px));
    transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 200;
    display: flex;
    flex-direction: column;
    box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.5);
}

.ai-chat-sheet.expanded {
    transform: translateY(0);
}

/* 拖曳把手 */
.ai-chat-handle {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 12px 16px 8px;
    cursor: grab;
    user-select: none;
}

.ai-chat-handle:active {
    cursor: grabbing;
}

.handle-bar {
    width: 40px;
    height: 4px;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 2px;
    margin-bottom: 8px;
}

/* 展開按鈕 */
.ai-chat-toggle {
    background: linear-gradient(135deg, #10b981, #059669);
    border: none;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: var(--transition-normal);
    box-shadow: 0 4px 15px rgba(16, 185, 129, 0.4);
}

.ai-chat-toggle:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 20px rgba(16, 185, 129, 0.5);
}

.ai-chat-toggle .toggle-icon {
    font-size: 22px;
}

.ai-chat-sheet.expanded .ai-chat-toggle .toggle-icon::after {
    content: '';
}

/* 聊天內容區 */
.ai-chat-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
}

/* 背景警告文字 */
.ai-chat-disclaimer {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 80%;
    text-align: center;
    font-size: 14px;
    color: rgba(255, 255, 255, 0.15);
    pointer-events: none;
    line-height: 1.6;
    z-index: 0;
}

/* 訊息列表 */
.ai-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    position: relative;
    z-index: 1;
}

/* 訊息樣式 */
.chat-message {
    display: flex;
    gap: 10px;
    max-width: 85%;
    animation: fadeIn 0.3s ease;
}

.chat-message.ai-message {
    align-self: flex-start;
}

.chat-message.user-message {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.message-avatar {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    flex-shrink: 0;
}

.message-bubble {
    padding: 12px 16px;
    border-radius: 18px;
    font-size: 14px;
    line-height: 1.5;
}

.ai-message .message-bubble {
    background: #2d2d2d;
    color: #e5e5e5;
    border-bottom-left-radius: 4px;
}

.user-message .message-bubble {
    background: linear-gradient(135deg, #10b981, #059669);
    color: white;
    border-bottom-right-radius: 4px;
}

/* 載入動畫 */
.message-bubble.loading {
    display: flex;
    gap: 4px;
    padding: 16px 20px;
}

.message-bubble.loading .dot {
    width: 8px;
    height: 8px;
    background: rgba(255, 255, 255, 0.5);
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out;
}

.message-bubble.loading .dot:nth-child(1) { animation-delay: -0.32s; }
.message-bubble.loading .dot:nth-child(2) { animation-delay: -0.16s; }

@keyframes bounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}

/* 輸入區 */
.ai-chat-input-area {
    display: flex;
    gap: 10px;
    padding: 12px 16px;
    padding-bottom: calc(12px + var(--safe-area-bottom));
    background: #242424;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.ai-chat-input {
    flex: 1;
    padding: 12px 16px;
    border: none;
    border-radius: 24px;
    background: #333;
    color: white;
    font-size: 15px;
    outline: none;
    transition: var(--transition-fast);
}

.ai-chat-input::placeholder {
    color: rgba(255, 255, 255, 0.4);
}

.ai-chat-input:focus {
    background: #3d3d3d;
    box-shadow: 0 0 0 2px rgba(16, 185, 129, 0.3);
}

.ai-chat-send {
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 50%;
    background: linear-gradient(135deg, #10b981, #059669);
    color: white;
    font-size: 18px;
    cursor: pointer;
    transition: var(--transition-normal);
    display: flex;
    align-items: center;
    justify-content: center;
}

.ai-chat-send:hover {
    transform: scale(1.05);
    box-shadow: 0 4px 15px rgba(16, 185, 129, 0.4);
}

.ai-chat-send:active {
    transform: scale(0.95);
}

.ai-chat-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* 錯誤訊息樣式 */
.chat-message.error-message .message-bubble {
    background: rgba(239, 68, 68, 0.2);
    color: #fca5a5;
    border: 1px solid rgba(239, 68, 68, 0.3);
}

/* 響應式 - 桌面版 */
@media (min-width: 768px) {
    .ai-chat-sheet {
        left: auto;
        right: 20px;
        width: 400px;
        height: 60vh;
        border-radius: 24px 24px 0 0;
    }
}

@media (min-width: 1024px) {
    .ai-chat-sheet {
        width: 420px;
        height: 65vh;
    }
}


/* ===== Markdown 內容樣式 ===== */
.markdown-body {
    font-size: 14px;
    line-height: 1.6;
    word-wrap: break-word;
}

.markdown-body > *:first-child {
    margin-top: 0;
}

.markdown-body > *:last-child {
    margin-bottom: 0;
}

.markdown-body p {
    margin-bottom: 0.8em;
}

.markdown-body strong {
    font-weight: 700;
    color: #fff;
}

.markdown-body em {
    font-style: italic;
    color: #cbd5e1;
}

.markdown-body ul, 
.markdown-body ol {
    margin-bottom: 0.8em;
    padding-left: 20px;
}

.markdown-body li {
    margin-bottom: 0.4em;
}

.markdown-body h1, 
.markdown-body h2, 
.markdown-body h3 {
    font-weight: 700;
    margin-top: 1.2em;
    margin-bottom: 0.6em;
    color: #fff;
    line-height: 1.4;
}

.markdown-body h1 { font-size: 1.4em; }
.markdown-body h2 { font-size: 1.2em; }
.markdown-body h3 { font-size: 1.1em; }

.markdown-body code {
    background: rgba(255, 255, 255, 0.15);
    padding: 2px 4px;
    border-radius: 4px;
    font-family: monospace;
    font-size: 0.9em;
    color: #e2e8f0;
}

.markdown-body pre {
    background: #1e1e1e;
    padding: 12px;
    border-radius: 8px;
    overflow-x: auto;
    margin-bottom: 0.8em;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.markdown-body pre code {
    background: transparent;
    padding: 0;
    color: #cbd5e1;
    font-size: 0.85em;
}

.markdown-body blockquote {
    border-left: 3px solid #10b981;
    margin: 0 0 0.8em 0;
    padding-left: 12px;
    color: #94a3b8;
}

.markdown-body a {
    color: #34d399;
    text-decoration: underline;
}

.markdown-body table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 0.8em;
    font-size: 0.9em;
}

.markdown-body th, 
.markdown-body td {
    border: 1px solid rgba(255, 255, 255, 0.2);
    padding: 6px 10px;
    text-align: left;
}

.markdown-body th {
    background: rgba(255, 255, 255, 0.1);
    font-weight: 600;
    color: #fff;
}

