/* ===== 變數定義 ===== */
:root {
    --primary-400: #facc15;
    --primary-500: #eab308;
    --primary-600: #ca8a04;
    --accent-400: #a78bfa;
    --accent-500: #8b5cf6;
    --accent-600: #7c3aed;
    --success-500: #22c55e;
    --error-500: #ef4444;
    --bg-900: #0f0f12;
    --bg-800: #18181b;
    --bg-700: #27272a;
    --bg-600: #3f3f46;
    --text-100: #fafafa;
    --text-200: #e4e4e7;
    --text-300: #a1a1aa;
    --text-400: #71717a;
    --glass-bg: rgba(39, 39, 42, 0.6);
    --glass-border: rgba(255, 255, 255, 0.08);
    --gradient-primary: linear-gradient(135deg, var(--primary-500), var(--accent-500));
    --gradient-dark: linear-gradient(180deg, var(--bg-800), var(--bg-900));
    --shadow-lg: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
    --shadow-glow: 0 0 40px rgba(234, 179, 8, 0.15);
    --radius-md: 12px;
    --radius-lg: 20px;
    --radius-xl: 28px;
    --font-sans: 'Inter', 'Noto Sans TC', -apple-system, BlinkMacSystemFont, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; scroll-behavior: smooth; }
body {
    font-family: var(--font-sans);
    background: var(--gradient-dark);
    color: var(--text-100);
    min-height: 100vh;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
}

/* ===== 容器 ===== */
.app-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 2rem;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ===== 頂部 ===== */
.header { text-align: center; padding: 2rem 0; }
.logo { display: flex; align-items: center; justify-content: center; gap: 0.75rem; margin-bottom: 0.75rem; }
.logo-icon { font-size: 3rem; animation: bounce 2s ease-in-out infinite; }
@keyframes bounce { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-10px); } }
.logo h1 {
    font-size: 2.5rem; font-weight: 700;
    background: var(--gradient-primary);
    -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;
}
.tagline { color: var(--text-300); font-size: 1.1rem; }

.user-bar {
    display: flex; align-items: center; justify-content: center; gap: 1rem;
    margin-top: 1rem; color: var(--text-300); font-size: 0.9rem;
}
.btn-sm { padding: 0.4rem 1rem; font-size: 0.85rem; }
.user-avatar { width: 28px; height: 28px; border-radius: 50%; }

/* ===== 主內容 ===== */
.main-content { flex: 1; display: flex; flex-direction: column; gap: 1.5rem; }

/* ===== 設定區 (雲端版，無 API Key) ===== */
.settings-cloud {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
    background: var(--glass-bg);
    backdrop-filter: blur(16px);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-lg);
    padding: 1.5rem;
}
.setting-group { display: flex; flex-direction: column; gap: 0.5rem; }
.setting-group label { font-weight: 500; color: var(--text-200); display: flex; align-items: center; gap: 0.5rem; }
.setting-icon { font-size: 1.1rem; }
.setting-group select {
    flex: 1; background: var(--bg-700); border: 1px solid var(--glass-border);
    border-radius: var(--radius-md); padding: 0.75rem 1rem;
    color: var(--text-100); font-size: 1rem; transition: all 0.2s ease;
}
.setting-group select:focus { outline: none; border-color: var(--primary-500); box-shadow: 0 0 0 3px rgba(234, 179, 8, 0.15); }

/* ===== 統計區 ===== */
.stats-section { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; }
.stat-card {
    background: var(--glass-bg); backdrop-filter: blur(16px);
    border: 1px solid var(--glass-border); border-radius: var(--radius-md);
    padding: 1rem 1.25rem; display: flex; align-items: center; gap: 1rem;
    transition: all 0.2s ease;
}
.stat-card.clickable { cursor: pointer; }
.stat-card.clickable:hover { border-color: var(--primary-500); background: rgba(234, 179, 8, 0.05); }
.stat-icon { font-size: 1.5rem; }
.stat-content { flex: 1; }
.stat-label { font-size: 0.8rem; color: var(--text-400); margin-bottom: 0.25rem; }
.stat-value { font-size: 1.25rem; font-weight: 600; color: var(--text-100); }

/* ===== 上傳區 ===== */
.upload-section {
    background: var(--glass-bg); backdrop-filter: blur(16px);
    border: 1px solid var(--glass-border); border-radius: var(--radius-xl); padding: 1rem;
}
.upload-zone {
    border: 2px dashed var(--bg-600); border-radius: var(--radius-lg);
    padding: 3rem 2rem; text-align: center; cursor: pointer; transition: all 0.3s ease;
}
.upload-zone:hover, .upload-zone.drag-over { border-color: var(--primary-500); background: rgba(234, 179, 8, 0.05); }
.upload-zone.drag-over { transform: scale(1.01); }
.upload-content { pointer-events: none; }
.upload-icon { font-size: 3.5rem; margin-bottom: 0.75rem; opacity: 0.8; }
.upload-zone h2 { font-size: 1.35rem; font-weight: 600; margin-bottom: 0.5rem; color: var(--text-100); }
.upload-zone p { color: var(--text-300); }
.upload-hint { color: var(--text-400) !important; font-size: 0.875rem; margin-top: 0.75rem !important; }

/* ===== 區段標題 ===== */
.section-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1.5rem; }
.section-header h2 { font-size: 1.25rem; font-weight: 600; }
.action-buttons { display: flex; align-items: center; gap: 0.75rem; }
.cost-display {
    font-size: 0.9rem; color: var(--primary-400);
    padding: 0.5rem 1rem; background: rgba(234, 179, 8, 0.1); border-radius: var(--radius-md);
}

/* ===== 按鈕 ===== */
.btn {
    display: inline-flex; align-items: center; gap: 0.5rem;
    padding: 0.75rem 1.5rem; border-radius: var(--radius-md);
    font-size: 0.95rem; font-weight: 500; border: none; cursor: pointer; transition: all 0.2s ease;
}
.btn-primary { background: var(--gradient-primary); color: var(--bg-900); }
.btn-primary:hover { transform: translateY(-2px); box-shadow: var(--shadow-glow); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn-secondary { background: var(--bg-700); color: var(--text-200); border: 1px solid var(--glass-border); }
.btn-secondary:hover { background: var(--bg-600); }
.btn-success { background: var(--success-500); color: white; }
.btn-success:hover { filter: brightness(1.1); }

/* ===== 預覽/結果區 ===== */
.preview-section, .results-section {
    background: var(--glass-bg); backdrop-filter: blur(16px);
    border: 1px solid var(--glass-border); border-radius: var(--radius-xl); padding: 1.5rem;
}
.image-grid, .results-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 1rem; }

/* ===== 圖片卡片 ===== */
.image-card { position: relative; background: var(--bg-700); border-radius: var(--radius-md); overflow: hidden; aspect-ratio: 1; }
.image-card img { width: 100%; height: 100%; object-fit: cover; }
.image-card .remove-btn {
    position: absolute; top: 0.5rem; right: 0.5rem; width: 28px; height: 28px;
    border-radius: 50%; background: rgba(0,0,0,0.6); border: none; color: white;
    cursor: pointer; display: flex; align-items: center; justify-content: center;
    font-size: 1rem; opacity: 0; transition: opacity 0.2s ease;
}
.image-card:hover .remove-btn { opacity: 1; }
.image-card .remove-btn:hover { background: var(--error-500); }
.image-card .filename {
    position: absolute; bottom: 0; left: 0; right: 0; padding: 0.5rem;
    background: linear-gradient(transparent, rgba(0,0,0,0.8));
    font-size: 0.7rem; color: var(--text-200); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* ===== 結果卡片 ===== */
.result-card { background: var(--bg-700); border-radius: var(--radius-md); overflow: hidden; }
.result-card .comparison { display: grid; grid-template-columns: 1fr 1fr; }
.result-card .comparison img { width: 100%; aspect-ratio: 1; object-fit: cover; }
.result-card .label {
    position: absolute; top: 0.5rem; left: 0.5rem; padding: 0.25rem 0.5rem;
    background: rgba(0,0,0,0.7); border-radius: 4px; font-size: 0.65rem; color: var(--text-200);
}
.result-card .image-container { position: relative; }
.result-card .actions { padding: 0.75rem; display: flex; justify-content: space-between; align-items: center; gap: 0.5rem; }
.result-card .actions button { padding: 0.5rem 1rem; font-size: 0.85rem; }
.cost-info { font-size: 0.8rem; color: var(--text-400); }

/* ===== 進度區 ===== */
.progress-section {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(15,15,18,0.9); backdrop-filter: blur(8px);
    display: flex; align-items: center; justify-content: center; z-index: 1000;
}
.progress-card {
    background: var(--bg-800); border: 1px solid var(--glass-border);
    border-radius: var(--radius-xl); padding: 3rem; text-align: center; min-width: 400px;
}
.progress-icon { font-size: 4rem; margin-bottom: 1.5rem; animation: spin 2s linear infinite; }
@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }
.progress-card h3 { font-size: 1.5rem; margin-bottom: 1.5rem; }
.progress-bar-container { height: 8px; background: var(--bg-600); border-radius: 4px; overflow: hidden; margin-bottom: 1rem; }
.progress-bar { height: 100%; background: var(--gradient-primary); border-radius: 4px; width: 0%; transition: width 0.3s ease; }
.progress-text { color: var(--text-300); }
.cost-estimate { margin-top: 1rem; font-size: 0.9rem; color: var(--primary-400); }

/* ===== Modal ===== */
.modal {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(15,15,18,0.9); backdrop-filter: blur(8px);
    display: flex; align-items: center; justify-content: center; z-index: 2000;
}
.modal-content {
    background: var(--bg-800); border: 1px solid var(--glass-border);
    border-radius: var(--radius-xl); width: 90%; max-width: 600px; max-height: 80vh;
    display: flex; flex-direction: column;
}
.modal-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 1.5rem; border-bottom: 1px solid var(--glass-border);
}
.modal-header h2 { font-size: 1.25rem; font-weight: 600; }
.modal-close {
    width: 32px; height: 32px; border-radius: 50%; background: var(--bg-700);
    border: none; color: var(--text-200); font-size: 1.25rem; cursor: pointer;
    display: flex; align-items: center; justify-content: center; transition: all 0.2s ease;
}
.modal-close:hover { background: var(--error-500); color: white; }
.modal-body { padding: 1.5rem; overflow-y: auto; }
.modal-description { font-size: 0.9rem; color: var(--text-300); margin-bottom: 1.5rem; }
.add-term-form {
    display: flex; gap: 0.75rem; align-items: center;
    margin-bottom: 1.5rem; padding-bottom: 1.5rem; border-bottom: 1px solid var(--glass-border);
}
.add-term-form input {
    flex: 1; background: var(--bg-700); border: 1px solid var(--glass-border);
    border-radius: var(--radius-md); padding: 0.75rem 1rem; color: var(--text-100); font-size: 0.95rem;
}
.add-term-form input:focus { outline: none; border-color: var(--primary-500); }
.add-term-form .arrow { color: var(--text-400); font-size: 1.25rem; }
.term-list { display: flex; flex-direction: column; gap: 0.5rem; }
.term-item { display: flex; align-items: center; gap: 0.75rem; padding: 0.75rem 1rem; background: var(--bg-700); border-radius: var(--radius-md); }
.term-cn { color: var(--text-300); min-width: 100px; }
.term-tw { color: var(--primary-400); flex: 1; }
.term-delete {
    width: 24px; height: 24px; border-radius: 50%; background: transparent;
    border: 1px solid var(--glass-border); color: var(--text-400); cursor: pointer;
    display: flex; align-items: center; justify-content: center; transition: all 0.2s ease;
}
.term-delete:hover { background: var(--error-500); border-color: var(--error-500); color: white; }
.no-terms { text-align: center; color: var(--text-400); padding: 2rem; }

/* ===== 底部 ===== */
.footer { text-align: center; padding: 2rem 0 1rem; color: var(--text-400); font-size: 0.875rem; }

/* ===== Toast ===== */
.toast-container { position: fixed; bottom: 2rem; right: 2rem; display: flex; flex-direction: column; gap: 0.75rem; z-index: 3000; }
.toast {
    background: var(--bg-700); border: 1px solid var(--glass-border); border-radius: var(--radius-md);
    padding: 1rem 1.5rem; display: flex; align-items: center; gap: 0.75rem;
    box-shadow: var(--shadow-lg); animation: slideIn 0.3s ease;
}
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
.toast.success { border-left: 4px solid var(--success-500); }
.toast.error { border-left: 4px solid var(--error-500); }

/* ===== Lightbox 圖片預覽 ===== */
.lightbox {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.92); backdrop-filter: blur(8px);
    display: flex; align-items: center; justify-content: center;
    z-index: 5000; cursor: zoom-out;
}
.lightbox-inner {
    position: relative; max-width: 90vw; max-height: 90vh; cursor: default;
}
.lightbox-inner img {
    max-width: 90vw; max-height: 85vh; object-fit: contain;
    border-radius: var(--radius-md); box-shadow: var(--shadow-lg);
}
.lightbox-close {
    position: absolute; top: -1rem; right: -1rem;
    width: 36px; height: 36px; border-radius: 50%;
    background: var(--bg-700); border: 1px solid var(--glass-border);
    color: var(--text-100); font-size: 1.3rem; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: all 0.2s ease; z-index: 1;
}
.lightbox-close:hover { background: var(--error-500); }
.lightbox-label {
    text-align: center; color: var(--text-300); font-size: 0.9rem;
    margin-top: 0.75rem;
}

/* ===== 文案區 ===== */
.copywriting-content {
    color: var(--text-200); line-height: 1.8;
}
.copy-block {
    background: var(--bg-700); border-radius: var(--radius-md);
    padding: 1.5rem; margin-bottom: 1rem;
}
.copy-block h3 { color: var(--primary-400); margin-bottom: 0.75rem; font-size: 1.1rem; }
.copy-title { font-size: 1.3rem; font-weight: 700; color: var(--text-100); margin-bottom: 0.5rem; }
.copy-list { padding-left: 1.25rem; }
.copy-list li { margin-bottom: 0.3rem; }
.ad-copy-content { font-size: 0.95rem; line-height: 1.7; }
.ad-copy-content h3 { margin-top: 1.5rem; color: var(--primary-400); }
.ad-copy-content h4 { margin-top: 0.75rem; color: var(--text-200); }
.ad-copy-content hr { border-color: var(--glass-border); margin: 1rem 0; }

/* ===== 響應式 ===== */
@media (max-width: 768px) {
    .app-container { padding: 1rem; }
    .settings-cloud { grid-template-columns: 1fr; }
    .stats-section { grid-template-columns: 1fr; }
    .section-header { flex-direction: column; gap: 1rem; align-items: flex-start; }
    .image-grid, .results-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
    .progress-card { min-width: auto; margin: 1rem; }
    .add-term-form { flex-wrap: wrap; }
    .add-term-form input { flex: 1 1 100%; }
    .add-term-form .arrow { display: none; }
}

/* ===================================================================
   首圖 / 詳情圖 / 歷史（整併新增）
   =================================================================== */

/* 分頁列 */
.tab-nav {
    display: flex; gap: 0.5rem; flex-wrap: wrap;
    margin-bottom: 0.5rem;
}
.tab-btn {
    padding: 0.7rem 1.3rem; border-radius: var(--radius-md);
    background: var(--bg-700); color: var(--text-300);
    border: 1px solid var(--glass-border); cursor: pointer;
    font-size: 0.95rem; font-weight: 600; transition: all 0.2s ease;
}
.tab-btn:hover { background: var(--bg-600); color: var(--text-100); }
.tab-btn.active { background: var(--gradient-primary); color: var(--bg-900); border-color: transparent; }

.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* 工具兩欄版面 */
.tool-layout { display: grid; grid-template-columns: 45% 55%; gap: 1.5rem; align-items: start; }
.tool-form, .tool-result {
    background: var(--glass-bg); border: 1px solid var(--glass-border);
    border-radius: var(--radius-lg); padding: 1.5rem;
}
.tool-result { position: sticky; top: 1rem; min-height: 360px; }
.tool-title { font-size: 1.4rem; margin-bottom: 0.25rem; }
.tool-sub { color: var(--text-300); font-size: 0.9rem; margin-bottom: 1.25rem; }

/* 表單欄位 */
.field { margin-bottom: 1.1rem; }
.field > label { display: block; font-weight: 600; font-size: 0.9rem; margin-bottom: 0.4rem; color: var(--text-200); }
.field .req { color: var(--primary-400); }
.field input[type=text], .field textarea, .field select,
.tool-form input[type=text], .tool-form textarea, .tool-form select {
    width: 100%; padding: 0.65rem 0.8rem; border-radius: var(--radius-md);
    background: var(--bg-800); border: 1px solid var(--glass-border);
    color: var(--text-100); font-size: 0.95rem; font-family: inherit;
}
.field textarea { resize: vertical; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.8rem; }
.field-hint { font-size: 0.8rem; color: var(--text-400); margin-top: 0.35rem; }
.checkbox { display: flex; align-items: center; gap: 0.5rem; font-size: 0.88rem; color: var(--text-300); margin-top: 0.5rem; cursor: pointer; font-weight: 400; }
.checkbox input { width: auto; }
.btn-block { width: 100%; justify-content: center; margin-top: 0.5rem; padding: 0.9rem; font-size: 1.05rem; }

.dna-preview {
    margin-top: 0.6rem; padding: 0.7rem 0.9rem; border-radius: var(--radius-md);
    background: rgba(167, 139, 250, 0.1); border: 1px solid rgba(167, 139, 250, 0.25);
    font-size: 0.8rem; color: var(--text-300); line-height: 1.5;
}
.dna-preview strong { color: var(--accent-400); }

.advanced { margin-bottom: 1.1rem; border: 1px solid var(--glass-border); border-radius: var(--radius-md); padding: 0.5rem 0.9rem; }
.advanced summary { cursor: pointer; font-weight: 600; font-size: 0.9rem; color: var(--text-200); }
.advanced > *:not(summary) { margin-top: 0.7rem; }

/* 上傳元件 */
.uploader { }
.uploader-grid { display: flex; flex-wrap: wrap; gap: 0.6rem; margin-bottom: 0.6rem; }
.uploader-grid:empty { display: none; }
.uploader-thumb { position: relative; width: 72px; height: 72px; border-radius: var(--radius-md); overflow: hidden; border: 1px solid var(--glass-border); }
.uploader-thumb img { width: 100%; height: 100%; object-fit: cover; }
.uploader-thumb .remove-btn {
    position: absolute; top: 2px; right: 2px; width: 20px; height: 20px;
    border-radius: 50%; border: none; background: rgba(0,0,0,0.65); color: #fff;
    cursor: pointer; font-size: 0.9rem; line-height: 1; display: flex; align-items: center; justify-content: center;
}
.uploader-add {
    padding: 0.5rem 1rem; border-radius: var(--radius-md); cursor: pointer;
    background: var(--bg-700); color: var(--text-200); border: 1px dashed var(--glass-border); font-size: 0.85rem;
}
.uploader-zone.drag-over { outline: 2px dashed var(--primary-400); outline-offset: 3px; }

/* 結果區 */
.result-empty, .result-loading { text-align: center; color: var(--text-400); padding: 4rem 1rem; }
.result-image { width: 100%; border-radius: var(--radius-md); cursor: pointer; }
.prompt-box {
    margin-top: 1rem; padding: 0.9rem; background: var(--bg-900); border-radius: var(--radius-md);
    font-size: 0.75rem; color: var(--text-300); white-space: pre-wrap; word-break: break-word; max-height: 320px; overflow: auto;
}
.spinner {
    width: 42px; height: 42px; margin: 0 auto 1rem; border-radius: 50%;
    border: 4px solid var(--bg-600); border-top-color: var(--primary-400); animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* 詳情圖 8 格 */
.detail-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.7rem; }
.detail-cell { position: relative; border-radius: var(--radius-md); overflow: hidden; border: 1px solid var(--glass-border); }
.detail-cell img { width: 100%; display: block; cursor: pointer; }
.detail-cell span { position: absolute; top: 4px; left: 4px; background: rgba(0,0,0,0.6); color: #fff; font-size: 0.7rem; padding: 0.1rem 0.45rem; border-radius: 6px; }

/* 歷史 */
.history-list { display: flex; flex-direction: column; gap: 0.7rem; }
.history-card { display: flex; align-items: center; gap: 1rem; padding: 0.8rem; background: var(--glass-bg); border: 1px solid var(--glass-border); border-radius: var(--radius-md); }
.history-thumb { width: 64px; height: 64px; border-radius: var(--radius-md); overflow: hidden; flex-shrink: 0; background: var(--bg-800); }
.history-thumb img { width: 100%; height: 100%; object-fit: cover; }
.history-thumb .no-thumb { width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; color: var(--text-400); }
.history-info { flex: 1; min-width: 0; }
.history-name { font-weight: 600; }
.history-meta { font-size: 0.82rem; color: var(--text-300); margin-top: 0.2rem; }
.history-date { font-size: 0.78rem; color: var(--text-400); margin-top: 0.15rem; }
.history-actions { display: flex; gap: 0.5rem; flex-shrink: 0; }

/* RWD：窄螢幕單欄 */
@media (max-width: 820px) {
    .tool-layout { grid-template-columns: 1fr; }
    .tool-result { position: static; }
}
