/* ==========================================
   全域樣式與變數設定
   ========================================== */
:root {
    --primary-color: #4f46e5;       /* 靛藍色 */
    --primary-hover: #4338ca;
    --excel-color: #107c41;         /* Excel 經典綠 */
    --excel-hover: #0d6233;
    --word-color: #185abd;          /* Word 經典藍 */
    --word-hover: #134896;
    --bg-gradient: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
    --card-bg: rgba(255, 255, 255, 0.75);
    --card-border: rgba(255, 255, 255, 0.4);
    --text-main: #1f2937;           /* 深灰色 */
    --text-muted: #4b5563;          /* 中灰色 */
    --text-light: #9ca3af;          /* 淺灰色 */
    --shadow-sm: 0 4px 6px -1px rgba(0, 0, 0, 0.05), 0 2px 4px -1px rgba(0, 0, 0, 0.03);
    --shadow-md: 0 10px 15px -3px rgba(0, 0, 0, 0.08), 0 4px 6px -2px rgba(0, 0, 0, 0.04);
    --shadow-lg: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
    --radius-sm: 8px;
    --radius-md: 16px;
    --radius-lg: 24px;
    --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Outfit', 'Noto Sans TC', sans-serif;
    background: var(--bg-gradient);
    color: var(--text-main);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    position: relative;
    overflow-x: hidden;
    line-height: 1.6;
}

/* ==========================================
   背景裝飾光暈
   ========================================== */
.background-decor {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
    overflow: hidden;
    pointer-events: none;
}

.circle {
    position: absolute;
    border-radius: 50%;
    filter: blur(80px);
    opacity: 0.4;
}

.circle-1 {
    top: -10%;
    right: -10%;
    width: 50vw;
    height: 50vw;
    background: radial-gradient(circle, rgba(99, 102, 241, 0.6) 0%, rgba(168, 85, 247, 0.2) 100%);
    animation: float-slow 20s infinite alternate;
}

.circle-2 {
    bottom: -10%;
    left: -10%;
    width: 45vw;
    height: 45vw;
    background: radial-gradient(circle, rgba(56, 189, 248, 0.6) 0%, rgba(99, 102, 241, 0.2) 100%);
    animation: float-slow 25s infinite alternate-reverse;
}

@keyframes float-slow {
    0% { transform: translate(0, 0) scale(1); }
    100% { transform: translate(5%, 5%) scale(1.1); }
}

/* ==========================================
   主容器與排版
   ========================================== */
.container {
    width: 100%;
    max-width: 1200px;
    padding: 40px 20px;
    z-index: 1;
    display: flex;
    flex-direction: column;
    gap: 40px;
}

header {
    text-align: center;
    margin-bottom: 10px;
}

header h1 {
    font-size: 2.8rem;
    font-weight: 700;
    background: linear-gradient(to right, #4338ca, #3b82f6);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 12px;
    letter-spacing: -0.5px;
}

.subtitle {
    font-size: 1.1rem;
    color: var(--text-muted);
    font-weight: 400;
}

/* ==========================================
   上傳卡片區塊
   ========================================== */
.upload-section {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 30px;
}

.upload-card {
    background: var(--card-bg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-lg);
    padding: 40px 30px;
    text-align: center;
    box-shadow: var(--shadow-md);
    transition: var(--transition);
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
    cursor: pointer;
}

.upload-card:hover {
    transform: translateY(-8px);
    box-shadow: var(--shadow-lg);
    border-color: rgba(255, 255, 255, 0.6);
}

/* 拖曳檔案在上方的效果 */
.upload-card.dragover {
    border: 2px dashed var(--primary-color);
    background: rgba(79, 70, 229, 0.05);
    transform: scale(1.02);
}

/* 卡片圖示樣式 */
.card-icon {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 24px;
    transition: var(--transition);
}

#excel-card .card-icon {
    background: rgba(16, 124, 65, 0.1);
    color: var(--excel-color);
}

#excel-card:hover .card-icon {
    background: var(--excel-color);
    color: white;
}

#word-card .card-icon {
    background: rgba(24, 90, 189, 0.1);
    color: var(--word-color);
}

#word-card:hover .card-icon {
    background: var(--word-color);
    color: white;
}

.card-icon svg {
    width: 36px;
    height: 36px;
    transition: transform 0.3s ease;
}

.upload-card:hover .card-icon svg {
    transform: scale(1.1);
}

.upload-card h2 {
    font-size: 1.4rem;
    font-weight: 600;
    margin-bottom: 8px;
}

.card-desc {
    font-size: 0.95rem;
    color: var(--text-muted);
    margin-bottom: 24px;
}

/* 隱藏原生 input file */
.file-input {
    display: none;
}

/* 選擇檔案按鈕 */
.upload-btn {
    padding: 12px 28px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.95rem;
    font-weight: 500;
    color: white;
    cursor: pointer;
    transition: var(--transition);
    box-shadow: var(--shadow-sm);
    outline: none;
}

#excel-btn {
    background: var(--excel-color);
}

#excel-btn:hover {
    background: var(--excel-hover);
    box-shadow: 0 4px 12px rgba(16, 124, 65, 0.3);
}

#word-btn {
    background: var(--word-color);
}

#word-btn:hover {
    background: var(--word-hover);
    box-shadow: 0 4px 12px rgba(24, 90, 189, 0.3);
}

.file-info {
    margin-top: 16px;
    font-size: 0.85rem;
    color: var(--text-light);
    font-style: italic;
    max-width: 100%;
    word-break: break-all;
}

/* ==========================================
   預覽區塊樣式
   ========================================== */
.preview-section {
    background: var(--card-bg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
    padding: 30px;
    min-height: 400px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.preview-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid rgba(0, 0, 0, 0.08);
    padding-bottom: 16px;
    flex-wrap: wrap;
    gap: 15px;
}

.preview-title {
    font-size: 1.4rem;
    font-weight: 600;
    color: var(--text-main);
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 16px;
    flex-wrap: wrap;
}

/* 生成合約按鈕樣式 */
.generate-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 20px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.95rem;
    font-weight: 600;
    color: white;
    background: linear-gradient(135deg, #6366f1 0%, #4f46e5 100%);
    box-shadow: 0 4px 15px rgba(79, 70, 229, 0.3);
    cursor: pointer;
    transition: var(--transition);
}

.generate-btn:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(79, 70, 229, 0.5);
    background: linear-gradient(135deg, #4f46e5 0%, #4338ca 100%);
}

.generate-btn:active:not(:disabled) {
    transform: translateY(0);
}

.generate-btn:disabled {
    background: #e5e7eb;
    color: var(--text-light);
    box-shadow: none;
    cursor: not-allowed;
}

.generate-btn svg {
    transition: transform 0.3s ease;
}

.generate-btn:hover:not(:disabled) svg {
    transform: rotate(15deg) scale(1.1);
}

/* 分頁切換鈕（多檔案預覽時使用） */
.preview-tabs {
    display: flex;
    gap: 10px;
    background: rgba(0, 0, 0, 0.04);
    padding: 4px;
    border-radius: var(--radius-sm);
}

.tab-btn {
    padding: 8px 16px;
    border: none;
    background: transparent;
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
    color: var(--text-muted);
}

.tab-btn:hover {
    color: var(--text-main);
}

.tab-btn.active {
    background: white;
    color: var(--primary-color);
    box-shadow: var(--shadow-sm);
}

.preview-container {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
}

/* 未上傳檔案的提示狀態 */
.empty-state {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--text-light);
    gap: 16px;
    padding: 60px 20px;
    text-align: center;
}

.empty-icon {
    width: 64px;
    height: 64px;
    color: var(--text-light);
}

.empty-state p {
    font-size: 1.05rem;
    max-width: 400px;
}

/* 解析內容展示容器 */
.preview-content-wrapper {
    display: none;
    animation: fadeIn 0.4s ease-out forwards;
    width: 100%;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ==========================================
   解析後的資料樣式渲染（Table 與 Word）
   ========================================== */

/* --- Excel 表格樣式 --- */
.excel-table-container {
    width: 100%;
    overflow-x: auto;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-sm);
    border: 1px solid rgba(0, 0, 0, 0.05);
}

.excel-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.95rem;
    text-align: left;
    background: white;
}

.excel-table th {
    background-color: #f3f4f6;
    color: var(--text-main);
    font-weight: 600;
    padding: 12px 16px;
    border-bottom: 2px solid #e5e7eb;
    white-space: nowrap;
}

.excel-table td {
    padding: 12px 16px;
    border-bottom: 1px solid #e5e7eb;
    color: var(--text-muted);
    white-space: nowrap;
}

.excel-table tr:last-child td {
    border-bottom: none;
}

.excel-table tr:nth-child(even) {
    background-color: #f9fafb;
}

.excel-table tr:hover {
    background-color: #f3f4f6;
}

/* --- Word 文件排版樣式 --- */
.word-document-container {
    background: white;
    padding: 40px;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-sm);
    border: 1px solid rgba(0, 0, 0, 0.05);
    color: #2c3e50; /* 經典閱讀字色 */
    max-height: 600px;
    overflow-y: auto;
}

.word-document-container h1,
.word-document-container h2,
.word-document-container h3,
.word-document-container h4 {
    margin-top: 1.5em;
    margin-bottom: 0.6em;
    color: #1a252f;
    font-weight: 600;
}

.word-document-container h1 { font-size: 1.8rem; border-bottom: 1px solid #eee; padding-bottom: 8px; }
.word-document-container h2 { font-size: 1.45rem; }
.word-document-container h3 { font-size: 1.2rem; }

.word-document-container p {
    margin-bottom: 1.2em;
    line-height: 1.8;
    font-size: 1.05rem;
}

.word-document-container ul,
.word-document-container ol {
    margin-bottom: 1.2em;
    padding-left: 24px;
}

.word-document-container li {
    margin-bottom: 0.4em;
}

.word-document-container table {
    width: 100%;
    border-collapse: collapse;
    margin: 20px 0;
}

.word-document-container th,
.word-document-container td {
    border: 1px solid #ddd;
    padding: 8px 12px;
}

/* ==========================================
   載入中動畫（Loading Spinner）
   ========================================== */
.loading-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    padding: 60px 20px;
}

.spinner {
    width: 50px;
    height: 50px;
    border: 5px solid rgba(79, 70, 229, 0.1);
    border-radius: 50%;
    border-top-color: var(--primary-color);
    animation: spin 1s ease-in-out infinite;
}

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

/* ==========================================
   錯誤與訊息通知提示
   ========================================== */
.error-message {
    color: #ef4444;
    background: #fef2f2;
    border: 1px solid #fee2e2;
    padding: 16px;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    gap: 12px;
    margin-top: 10px;
}

.error-message svg {
    width: 24px;
    height: 24px;
    flex-shrink: 0;
}

/* ==========================================
   RWD 響應式佈局
   ========================================== */
@media (max-width: 768px) {
    header h1 {
        font-size: 2.2rem;
    }
    .preview-section {
        padding: 20px;
    }
    .word-document-container {
        padding: 20px;
    }
}

/* ==========================================
   批次生成合約雙欄排版與操作樣式
   ========================================== */
.merged-container {
    display: grid;
    grid-template-columns: 350px 1fr;
    gap: 24px;
    align-items: start;
    width: 100%;
}

/* 左側列表邊欄 */
.merged-sidebar {
    background: rgba(255, 255, 255, 0.5);
    border: 1px solid rgba(0, 0, 0, 0.05);
    border-radius: var(--radius-md);
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    max-height: 600px;
    overflow-y: auto;
}

/* 一鍵打包下載全部 ZIP 按鈕 */
.zip-download-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    padding: 12px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.95rem;
    font-weight: 600;
    color: white;
    background: linear-gradient(135deg, #107c41 0%, #0b592e 100%);
    box-shadow: 0 4px 12px rgba(16, 124, 65, 0.25);
    cursor: pointer;
    transition: var(--transition);
}

.zip-download-btn:hover {
    transform: translateY(-2px);
    background: linear-gradient(135deg, #0d6233 0%, #084322 100%);
    box-shadow: 0 6px 16px rgba(16, 124, 65, 0.4);
}

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

.zip-download-btn svg {
    width: 20px;
    height: 20px;
}

/* 租客合約列表 */
.merged-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.merged-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: white;
    border: 1px solid rgba(0, 0, 0, 0.05);
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    transition: var(--transition);
    cursor: pointer;
}

.merged-item:hover {
    transform: translateX(4px);
    box-shadow: var(--shadow-sm);
    border-color: rgba(79, 70, 229, 0.2);
}

.merged-item.active {
    border-left: 4px solid var(--primary-color);
    background: rgba(79, 70, 229, 0.02);
    font-weight: 500;
}

.tenant-name {
    font-size: 0.95rem;
    color: var(--text-main);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 140px;
}

.item-actions {
    display: flex;
    gap: 8px;
    align-items: center;
}

/* 列表內操作小按鈕 */
.item-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    transition: var(--transition);
    background: #f3f4f6;
    color: var(--text-muted);
}

.item-btn:hover {
    background: var(--primary-color);
    color: white;
}

.item-btn.download:hover {
    background: var(--word-color);
}

.item-btn svg {
    width: 16px;
    height: 16px;
}

/* 右側預覽窗格 */
.merged-preview-pane {
    flex-grow: 1;
    min-width: 0; /* 避免 flex 子元素溢出 */
}

/* 響應式：當螢幕較窄時，改為上下排列 */
@media (max-width: 900px) {
    .merged-container {
        grid-template-columns: 1fr;
    }
    .merged-sidebar {
        max-height: 300px;
    }
}

/* 頁尾樣式 */
footer {
    text-align: center;
    color: var(--text-light);
    font-size: 0.85rem;
    margin-top: 20px;
    padding-top: 20px;
    border-top: 1px solid rgba(0, 0, 0, 0.05);
}
