/* =========================================
   1. 核心引入與變數設定
   ========================================= */
@import url('https://cdn.jsdelivr.net/npm/lxgw-wenkai-screen-web/style.css');

:root {
    /* 五行顏色 */
    --c-wood: #27ae60; --c-fire: #e74c3c; --c-earth: #d35400; 
    --c-metal: #7f8c8d; --c-water: #2980b9;
    
    /* 陰陽背景 */
    --bg-yang: #ffffff; --bg-yin: #f2f2f2; 
    
    /* UI 系統變數 */
    --border-radius: 16px; 
    --shadow: 0 4px 20px rgba(0,0,0,0.06);
    --font-stack: "LXGW WenKai Screen", "Microsoft JhengHei", "Microsoft YaHei", sans-serif;
    
    /* 字體大小系統 */
    --fs-xs: 0.75rem;  /* 藏干/標籤用 */
    --fs-sm: 0.85rem;  /* 說明文字 */
    --fs-md: 1rem;     /* 內文 */
    
    /* CJK 字體權重微調 */
    --letter-spacing-cjk: 0;
    --font-weight-cjk: bold; 
}

/* 針對 CJK 語系優化 */
:root:lang(zh-TW), :root:lang(zh-CN), :root:lang(ja-JP), :root:lang(ko-KR) {
    --fs-xs: 0.85rem; 
    --fs-sm: 0.95rem; 
    --letter-spacing-cjk: 0.05em; 
    --font-weight-cjk: 500; 
}

/* 基礎 Reset */
body { 
    margin: 0; 
    background: var(--bg-app) no-repeat fixed; 
    font-family: var(--font-stack); 
    color: var(--text-main); 
    min-height: 100vh; 
    line-height: 1.5;
}

.bazi-app { max-width: 960px; margin: 0 auto; padding: 15px; }

/* =========================================
   2. 主題定義 (Themes)
   ========================================= */

/* (A) 雅致宣紙 (預設) */
body.theme-zen {
    --bg-app: #fdfbf7; --bg-card: #ffffff;
    --text-main: #2c3e50; --text-sub: #7f8c8d;
    --theme-primary: #8e44ad; --theme-light: #f4ecf7;
    --border-color: #e5e0d8;
    --btn-bg: linear-gradient(135deg, #c0392b, #a93226);
    --btn-text: #ffffff; --title-color: #2c3e50;
}

/* (B) 雲月暗夜 (Dark Mode) */
body.theme-moon {
    --bg-app: radial-gradient(circle at 80% 10%, rgba(255,255,255,0.15) 0%, rgba(20,30,48,0) 40%),
              linear-gradient(to bottom, #0f2027, #203a43, #2c5364);
    --bg-card: #1e2a38;
    --text-main: #e0e0e0; --text-sub: #aab7c4;
    --theme-primary: #f1c40f; --theme-light: rgba(241, 196, 15, 0.1); 
    --border-color: #2c3e50;
    --btn-bg: linear-gradient(135deg, #f1c40f, #f39c12);
    --btn-text: #2c3e50; --title-color: #f1c40f;
    --bg-yang: #2c3e50; --bg-yin: #17212b; 
    -webkit-font-smoothing: antialiased;
}

/* 暗夜模式 - 元件修正 */
body.theme-moon .bazi-input, 
body.theme-moon .bazi-select {
    background-color: #2c3e50 !important; color: #fff !important; border: 1px solid #455a64 !important;
}
body.theme-moon .app-title { text-shadow: 0 0 10px rgba(241, 196, 15, 0.4); }
body.theme-moon .current-year-card {
    background-color: rgba(231, 76, 60, 0.2) !important; 
    border-color: #e74c3c !important;
    box-shadow: 0 0 15px rgba(231, 76, 60, 0.4);
}
body.theme-moon .shensha-badge {
    background: transparent !important; 
    border-color: #e67e22 !important; color: #e67e22 !important; 
}
body.theme-moon .shensha-badge.badge-ji {
    background: linear-gradient(135deg, #d35400, #e67e22) !important;
    color: #fff !important; border: none !important;
}
body.theme-moon .liunian-card.kw-card {
    background-image: repeating-linear-gradient(45deg, transparent, transparent 10px, rgba(255, 255, 255, 0.03) 10px, rgba(255, 255, 255, 0.03) 20px);
    border-color: #546e7a;
}

/* (C) 現代簡約 */
body.theme-simple {
    --bg-app: #f0f2f5; --bg-card: #ffffff;
    --text-main: #2c3e50; --text-sub: #7f8c8d;
    --theme-primary: #2c3e50; --theme-light: #e9ecef;
    --border-color: #dee2e6; --shadow: 0 2px 6px rgba(0,0,0,0.03);
    --btn-bg: linear-gradient(135deg, #2c3e50, #34495e);
    --btn-text: #ffffff; --title-color: #34495e;
}

/* =========================================
   3. 介面元件 (Header, Input, Menu)
   ========================================= */
.header-row { position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 80px; padding-bottom: 15px; gap: 15px; }
.app-title { margin: 0; font-size: 2.2rem; font-weight: bold; color: var(--text-main); text-shadow: 0 2px 4px rgba(0,0,0,0.1); line-height: 1.2; text-align: center; }
.app-title small { font-size: 1.1rem; font-weight: 500; opacity: 0.8; display: block; margin-top: 5px; }

/* 選單與按鈕 */
.right-actions { position: relative; width: 100%; display: flex; justify-content: center; gap: 15px; z-index: 10; }
.settings-container { position: relative; }
.btn-settings { background: transparent; border: 1px solid var(--border-color); color: var(--text-main); padding: 6px 14px; border-radius: 20px; cursor: pointer; font-size: 0.95rem; transition: all 0.2s; backdrop-filter: blur(5px); display: flex; align-items: center; }
.btn-settings:hover { background: rgba(255,255,255,0.2); }
.settings-menu { display: none; position: absolute; left: 50%; transform: translateX(-50%); top: 45px; width: 160px; background: var(--bg-card); border: 1px solid var(--border-color); border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,0.2); overflow: hidden; z-index: 100; backdrop-filter: blur(10px); }
.settings-menu.show { display: block; animation: fadeIn 0.2s; }
@keyframes fadeIn { from {opacity:0; transform:translate(-50%, -5px);} to {opacity:1; transform:translate(-50%, 0);} }
.menu-item { padding: 12px 15px; cursor: pointer; font-size: 0.95rem; color: var(--text-main); border-bottom: 1px solid var(--border-color); text-align: center; }
.menu-item:hover { background: var(--theme-light); }

/* 輸入區塊 */
.input-section { background: var(--bg-card); padding: 20px; border-radius: var(--border-radius); box-shadow: var(--shadow); margin-bottom: 20px; border-top: 5px solid var(--theme-primary); }
.input-row { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 15px; }
.input-group { display: flex; flex-direction: column; flex: 1; min-width: 80px; }
.input-group label { font-size: 0.85rem; color: var(--text-sub); margin-bottom: 4px; font-weight: bold; }
.bazi-input, .bazi-select { padding: 10px; border: 1px solid #ddd; border-radius: 8px; font-size: 1rem; font-family: var(--font-stack); box-sizing: border-box; }
.btn-calc { background: var(--btn-bg); color: var(--btn-text); border: none; padding: 12px; border-radius: 8px; cursor: pointer; width: 100%; font-size: 1.1rem; font-weight: bold; box-shadow: 0 4px 12px rgba(0,0,0,0.15); transition: transform 0.1s; font-family: var(--font-stack); }
.btn-calc:active { transform: scale(0.98); }

/* =========================================
   4. 排盤結果核心 (Dashboard, Pillars)
   ========================================= */
.dashboard-top { display: flex; gap: 15px; margin-bottom: 20px; }
.identity-card { flex: 1; background: #fff; border-radius: var(--border-radius); box-shadow: var(--shadow); display: flex; flex-direction: column; min-width: 200px; }
.id-header { background: var(--theme-light); color: var(--theme-primary); padding: 12px; font-size: 1.1rem; font-weight: bold; text-align: center; border-radius: 12px 12px 0 0; }
.id-body { padding: 15px; display: flex; flex-direction: column; gap: 10px; justify-content: center; flex: 1; }
.id-row { display: flex; justify-content: space-between; align-items: center; border-bottom: 1px dashed #eee; padding-bottom: 8px; }
.id-label { color: #95a5a6; font-size: 0.9rem; }
.id-value { font-weight: bold; color: #2c3e50; font-size: 1rem; }

.charts-container { flex: 2; background: #fff; padding: 15px; border-radius: var(--border-radius); box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 10px; }
.chart-box { position: relative; height: 180px; width: 100%; }
.balance-bar-container { display: flex; height: 16px; border-radius: 8px; overflow: hidden; background: #eee; margin: 5px 0; }
.bar-segment { display: flex; align-items: center; justify-content: center; font-size: 0.7rem; color: #fff; font-weight: bold; transition: width 0.6s ease; }
.bar-segment.support { background: linear-gradient(90deg, #27ae60, #2ecc71); }
.bar-segment.drain { background: linear-gradient(90deg, #c0392b, #e74c3c); }

/* 四柱卡片 */
.section-title { font-size: 1.1rem; border-left: 4px solid var(--theme-primary); padding-left: 10px; margin: 25px 0 15px 0; font-weight: bold; color: var(--title-color); }
.pillars-container { display: flex; gap: 10px; overflow-x: auto; padding-bottom: 10px; -webkit-overflow-scrolling: touch; }
.pillar-card { flex: 1; min-width: 95px; background: #fff; border-radius: 12px; box-shadow: var(--shadow); display: flex; flex-direction: column; overflow: hidden; flex-shrink: 0; }
.pillar-header { text-align: center; background: var(--theme-primary); color: #fff; padding: 8px 0; font-size: 1rem; font-weight: bold; letter-spacing: 2px; }
.pillar-body { padding: 10px 8px; display: flex; flex-direction: column; gap: 6px; height: auto; min-height: 300px; background: #fff; }

/* 干支方塊 */
.char-block { flex: 0 0 auto; min-height: 110px; padding-bottom: 10px; display: flex; flex-direction: column; align-items: center; justify-content: flex-start; padding-top: 12px; position: relative; border: 1px solid rgba(0,0,0,0.05); border-radius: 8px; margin-bottom: 5px; }
.char-block.bg-yang { background-color: var(--bg-yang); }
.char-block.bg-yin  { background-color: var(--bg-yin); border-color: rgba(0,0,0,0.1); }
.big-char { font-size: 2.6rem; font-weight: bold; line-height: 1; margin: 2px 0; width: 100%; text-align: center; margin-top: -2px; font-family: "LXGW WenKai Screen", serif; }
.god-label { font-size: var(--fs-xs); font-weight: var(--font-weight-cjk); color: var(--text-sub); margin-bottom: 2px; }

/* =========================================
   5. 地支藏干 (整合多語言/長度適配)
   ========================================= */
/* 容器：整合本命 (.hidden-container) 與流年 (.ln-god-bottom) */
.hidden-container, .ln-god-bottom {
    display: flex;
    flex-wrap: wrap;       /* 核心：字太長會自動換行，避免破版 */
    justify-content: center;
    gap: 4px;
    width: 100%;
    margin-top: 8px;
    padding: 0 4px;
    box-sizing: border-box;
}

/* 物件樣式：整合本命 (.hidden-item) 與流年 (.sub-god-item, .main-god) */
.hidden-item, .sub-god-item, .main-god {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.04);
    border-radius: 6px;
    padding: 3px 8px;
    min-width: fit-content;
    font-size: var(--fs-xs);
    font-weight: var(--font-weight-cjk);
    color: #555;
    border: 1px solid transparent;
    line-height: 1.2;
    transition: all 0.2s;
}

/* 本命第一個藏干 (主氣) 強調 */
.hidden-item:first-child {
    background: rgba(142, 68, 173, 0.1); 
    border-color: rgba(142, 68, 173, 0.2);
    font-weight: bold;
}

/* 流年主氣獨立樣式 */
.main-god {
    background: transparent;
    border: none;
    font-weight: bold;
    color: var(--text-main);
    width: 100%; /* 讓流年主氣置頂 */
    margin-bottom: 2px;
}

/* 內部的干支文字 */
.hidden-char {
    font-size: 1rem;
    font-weight: bold;
    margin-right: 4px;
    flex-shrink: 0;
}

.hidden-god {
    font-size: 0.75rem;
    font-weight: 500;
}

/* --- 雲月暗夜模式 藏干優化 --- */
body.theme-moon .hidden-item, 
body.theme-moon .sub-god-item {
    background: rgba(255, 255, 255, 0.08);
    color: rgba(255, 255, 255, 0.7);
}
body.theme-moon .hidden-item:first-child {
    background: rgba(241, 196, 15, 0.15);
    border-color: rgba(241, 196, 15, 0.4);
    color: #f1c40f;
}
body.theme-moon .main-god {
    color: #f1c40f !important;
    text-shadow: 0 0 5px rgba(241, 196, 15, 0.3);
}
body.theme-moon .ln-god-bottom {
    border-top: 1px solid rgba(255, 255, 255, 0.1);
}
body.theme-moon .liunian-card .ln-head {
    color: #aab7c4 !important;
}

/* =========================================
   6. 神煞與大運流年
   ========================================= */
.shensha-container { display: flex; flex-direction: column; gap: 3px; align-items: center; margin-top: 5px; min-height: 20px; }
.shensha-badge { font-size: var(--fs-xs); border-radius: 4px; width: 100%; text-align: center; white-space: nowrap; font-weight: bold; letter-spacing: 1px; padding: 2px 0; margin-top: 2px; background: transparent !important; border: 1px solid #d35400 !important; color: #d35400 !important; }
.shensha-badge.badge-ji { background: linear-gradient(135deg, #f39c12, #d35400) !important; color: #ffffff !important; text-shadow: 0 1px 1px rgba(0,0,0,0.2); border: none !important; }
.shensha-badge.badge-kw, .shensha-badge.badge-xiong { border-color: #7f8c8d !important; color: #7f8c8d !important; background: transparent !important; }

/* 標籤 */
.dm-badge { display: inline-block; width: 28px; height: 28px; line-height: 26px; text-align: center; border-radius: 50%; font-size: 1.1rem; font-weight: bold; border: 1px solid #e0e0e0; vertical-align: middle; margin-right: 6px; box-shadow: 0 1px 3px rgba(0,0,0,0.05); }
.dm-badge.bg-yang { background: #fff; } .dm-badge.bg-yin { background: #f0f0f0; }
.combo-tag { font-size: 0.8rem; color: #fff; background: linear-gradient(135deg, #667eea, #764ba2); padding: 2px 6px; border-radius: 4px; display: inline-block; margin-left: 2px; }
.life-stage-badge, .ln-stage { font-size: var(--fs-xs); font-weight: var(--font-weight-cjk); padding: 2px 10px; border-radius: 12px; margin: 4px 0; color: #fff; text-align: center; display: inline-block; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
.stage-high { background: linear-gradient(135deg, #27ae60, #2ecc71); }
.stage-mid { background: linear-gradient(135deg, #f39c12, #e67e22); }
.stage-low { background: linear-gradient(135deg, #95a5a6, #7f8c8d); }
.pillar-badge { display: inline-block !important; width: auto !important; align-self: center; font-size: 0.75rem; padding: 2px 10px; border-radius: 12px; margin-top: 4px !important; margin-bottom: 12px !important; font-weight: bold; color: #fff; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
.kw-badge-common { display: inline-block; background-color: var(--bg-yin); border: 1px dashed var(--text-sub); color: var(--text-sub); font-size: var(--fs-xs); padding: 1px 6px; border-radius: 4px; font-weight: bold; font-family: sans-serif; margin-left: 6px; vertical-align: middle; }

/* 五行顏色工具類 */
.c-wood { color: var(--c-wood); } .c-fire { color: var(--c-fire); } .c-earth { color: var(--c-earth); } 
.c-metal { color: var(--c-metal); } .c-water { color: var(--c-water); }

/* 大運與流年 */
.dayun-info { color: var(--text-main); font-weight: bold; margin-bottom: 10px; }
.dayun-scroll-container { overflow-x: auto; padding: 5px 0 10px 0; }
.dayun-grid { display: flex; gap: 8px; }
.dayun-item { min-width: 60px; padding: 8px 4px; border: 1px solid #eee; border-radius: 10px; text-align: center; cursor: pointer; transition: all 0.2s; background: #fff; display: flex; flex-direction: column; justify-content: center; position: relative; }
.dayun-item:hover { transform: translateY(-2px); border-color: var(--theme-primary); }
.dayun-item.active { background: var(--theme-light); border: 2px solid var(--theme-primary); box-shadow: 0 4px 10px rgba(0,0,0,0.1); }
.dayun-item.active .dy-age { color: var(--theme-primary); font-weight: bold; }
.dayun-item .dy-gz { display: flex; justify-content: center; gap: 1px; font-size: 1.1rem; font-weight: bold; }
.dy-age { font-size: 0.75rem; color: #999; margin-bottom: 2px; }

.liunian-section { margin-top: 15px; border-top: 1px dashed #eee; padding-top: 15px; animation: fadeIn 0.3s; }
.liunian-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.liunian-card { display: flex; flex-direction: column; align-items: center; padding: 10px 4px; min-height: 180px; height: auto; justify-content: flex-start; gap: 4px; background: var(--bg-card); border: 1px solid var(--border-color); border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.02); transition: transform 0.2s; position: relative; overflow: hidden; }
.liunian-card:hover { transform: translateY(-3px); box-shadow: 0 5px 15px rgba(0,0,0,0.1); }
.liunian-card.kw-card { background-color: var(--bg-yin); border: 1px dashed var(--text-sub); opacity: 0.95; background-image: repeating-linear-gradient(45deg, transparent, transparent 10px, rgba(0, 0, 0, 0.03) 10px, rgba(0, 0, 0, 0.03) 20px); }
.ln-head { font-size: var(--fs-sm); color: #7f8c8d; margin-bottom: 2px; }
.ln-god-top { font-weight: bold; color: var(--theme-primary); font-size: var(--fs-sm); min-height: 1.2em; text-align: center; word-break: break-word; line-height: 1.1; }
.ln-gz-box { font-size: 1.5rem; font-weight: bold; line-height: 1.1; font-family: "LXGW WenKai Screen", serif; margin: 2px 0; }
.current-year-card { border: 2px solid #e74c3c !important; background-color: #fff5f5; transform: scale(1.02); z-index: 5; box-shadow: 0 4px 12px rgba(231, 76, 60, 0.3); }

/* =========================================
   7. AI 介面樣式
   ========================================= */
.ai-guide-text { font-size: 0.95rem; color: var(--text-sub); margin-bottom: 12px; display: flex; justify-content: space-between; align-items: center; }
.category-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.btn-category { padding: 16px 12px; border: 1px solid var(--border-color); border-radius: 12px; background: var(--bg-card); color: var(--text-main); font-size: 1rem; cursor: pointer; transition: all 0.2s; text-align: left; display: flex; align-items: center; gap: 10px; box-shadow: 0 2px 6px rgba(0,0,0,0.02); }
.btn-category:hover { transform: translateY(-3px); border-color: var(--theme-primary); box-shadow: 0 5px 15px rgba(0,0,0,0.08); }
.question-list { display: flex; flex-direction: column; gap: 8px; max-height: 320px; overflow-y: auto; }
.question-group-title { font-size: 0.85rem; color: #999; margin: 15px 0 5px 2px; font-weight: bold; letter-spacing: 1px; }
.question-group-title:first-child { margin-top: 0; }
.btn-question { padding: 12px 15px; border: 1px solid transparent; border-radius: 10px; background: rgba(0,0,0,0.02); color: var(--text-main); font-size: 0.95rem; text-align: left; cursor: pointer; transition: all 0.2s; line-height: 1.4; }
.btn-question:hover { background: #fff; border-color: var(--border-color); box-shadow: 0 2px 8px rgba(0,0,0,0.05); transform: translateX(2px); }
.btn-question.active { background: var(--theme-light); color: var(--theme-primary); border-color: var(--theme-primary); font-weight: bold; }
.btn-back { background: none; border: none; color: var(--text-sub); cursor: pointer; font-size: 0.85rem; padding: 4px 8px; border-radius: 4px; }
.btn-back:hover { background: rgba(0,0,0,0.05); color: var(--text-main); }
.selected-summary { background: linear-gradient(to right, var(--theme-light), transparent); padding: 15px; border-radius: 10px; margin-bottom: 15px; border-left: 4px solid var(--theme-primary); }
.btn-ai { width: 100%; padding: 15px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border: none; border-radius: 12px; font-size: 1.1rem; font-weight: bold; cursor: pointer; margin-top: 20px; box-shadow: 0 4px 15px rgba(100, 100, 255, 0.2); display: flex; align-items: center; justify-content: center; gap: 8px; font-family: var(--font-stack); }
.btn-ai:hover { transform: translateY(-2px); }

/* 深色模式 AI 修正 */
body.theme-moon .btn-category { background: rgba(255,255,255,0.05); border-color: #455a64; }
body.theme-moon .btn-question { background: rgba(255,255,255,0.03); }
body.theme-moon .btn-question:hover { background: rgba(255,255,255,0.08); }
body.theme-moon .selected-summary { background: rgba(255,255,255,0.05); }
body.theme-moon #ai-response-area { background: #1e2a38; border-color: #37474f; color: #e0e0e0; }

/* =========================================
   8. 行動裝置適配 (統一管理)
   ========================================= */
@media screen and (max-width: 768px) {
    .bazi-app { padding: 10px; width: 100%; box-sizing: border-box; overflow-x: hidden; }
    
    /* 標頭與輸入區調整 */
    .header-row { gap: 10px; }
    .app-title { font-size: 1.8rem; }
    .input-row { flex-direction: column; gap: 12px; }
    .input-group { width: 100%; }
    .bazi-input, .bazi-select { height: 45px; font-size: 1.1rem; }
    
    /* 儀表板改為垂直排列 */
    .dashboard-top { flex-direction: column; gap: 20px; }
    
    /* 圖表高度適配 */
    .chart-box { height: 260px; }
    
    /* 排盤卡片縮小寬度，確保橫向滑動 */
    .pillar-card { flex: 0 0 28%; min-width: 90px; }
    
    /* 流年卡片改為兩欄 */
    .liunian-grid { grid-template-columns: repeat(2, 1fr) !important; }
}
