:root {
    --bg-main: #000000;
    --bg-card: #111111;
    --primary-color: #0a84ff;
    --success-color: #34c759;
    --success-btn-gradient: linear-gradient(135deg, #10b981 0%, #059669 100%);
    --danger-color: #ff3b30;
    --text-main: #d1d1d6;
    --text-muted: #8e8e93;
    --border-color: #222222;
}

* { box-sizing: border-box; margin: 0; padding: 0; outline: none; -webkit-tap-highlight-color: transparent; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }

/* 🚨 終極修復：引入 100dvh 解決 iOS 視窗高度亂跳的問題 */
body { 
    background-color: #080808; color: var(--text-main); 
    height: 100vh; /* 給舊瀏覽器的備案 */
    height: 100dvh; /* 蘋果 iOS 專屬動態高度 */
    display: flex; justify-content: center; overflow: hidden; 
}

.app-container {
    width: 100%; max-width: 430px;
    height: 100vh; height: 100dvh; 
    background-color: var(--bg-main);
    position: relative; overflow: hidden;
    box-shadow: 0 0 50px rgba(0,0,0,0.8);
}

input, select, button { font-size: 16px; }

.view-container { display: none; width: 100%; height: 100%; }
.view-container.active { display: flex; flex-direction: column; }

#login-view { justify-content: center; align-items: center; }
.login-box { text-align: center; width: 80%; }
.login-box h1 { font-size: 32px; margin-bottom: 30px; color: var(--primary-color); }
.login-box input { width: 100%; padding: 15px; margin-bottom: 15px; background: rgba(255,255,255,0.05); border: 1px solid var(--border-color); border-radius: 10px; color: white; text-align: center; }
.login-box button { width: 100%; padding: 15px; background: var(--primary-color); color: white; border: none; border-radius: 10px; font-weight: bold; cursor: pointer; }

/* 💎 頂部防彈：加入 max() 確保動態島永遠不會吃掉標題 */
#app-header { 
    /* 即使 iOS bug 給 0，強制留出 47px 給動態島 */
    padding-top: max(env(safe-area-inset-top), 47px);
    height: calc(60px + max(env(safe-area-inset-top), 47px));
    display: flex; align-items: center; justify-content: center; 
    border-bottom: 1px solid var(--border-color); 
    position: sticky; top: 0; 
    background: rgba(0,0,0,0.8); backdrop-filter: blur(10px); z-index: 100; 
}
#header-title { font-size: 18px; font-weight: bold; }

/* 💎 中間內容防彈：確保最底部的資料不會被導覽列遮住 */
#main-content { 
    flex: 1; overflow-y: auto; padding: 15px; 
    /* 預留底欄 65px + 最低安全區 25px = 至少 90px 的尾部空白 */
    padding-bottom: calc(85px + max(env(safe-area-inset-bottom), 25px));
    -webkit-overflow-scrolling: touch; 
}
#main-content::-webkit-scrollbar { display: none; }

.tab-content { display: none; flex-direction: column; gap: 15px; }
.tab-content.active { display: flex; }

.card { background-color: var(--bg-card); border-radius: 12px; padding: 15px; border: 1px solid var(--border-color); }
.card h3 { font-size: 15px; color: var(--text-muted); margin-bottom: 12px; }

#quote-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.quote-card { background: rgba(255,255,255,0.03); padding: 15px; border-radius: 10px; border: 1px solid var(--border-color); text-align: center; }

.chart-controls { display: flex; flex-direction: column; gap: 10px; margin-bottom: 15px; }
.button-group, .ma-group { display: flex; gap: 6px; flex-wrap: wrap; }
.range-btn { background: #222; color: white; border: none; padding: 8px 12px; border-radius: 6px; cursor: pointer; font-size: 13px; flex: 1; text-align: center;}
.range-btn.active { background: var(--primary-color); font-weight: bold; }
.ma-group { font-size: 12px; justify-content: space-between; margin-top: 5px;}
.ma-group label { display: flex; align-items: center; gap: 3px; }

.tv-chart-container, .chart-holder { position: relative; width: 100%; border-radius: 8px; overflow: hidden; background: #161616;}
.tv-chart-container { height: 300px; }
.pie-holder { height: 200px; }
.line-holder { height: 250px; }

.card-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.count-badge { font-size: 11px; color: #666; }
.tag-list { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.add-stock-group { display: flex; gap: 8px; margin-top: 15px;}
.add-stock-group input { flex: 1; padding: 10px; border-radius: 6px; border: 1px solid #333; background: #000; color: white;}

#ws-status-container { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; font-size: 13px; color: var(--text-muted); }
.status-light { width: 10px; height: 10px; border-radius: 50%; display: inline-block; transition: background-color 0.3s, box-shadow 0.3s;}

.status-light.red {
    background-color: var(--danger-color); color: var(--danger-color); box-shadow: 0 0 5px currentColor; animation: pulse-red 1.5s infinite;
}
@keyframes pulse-red { 0% { opacity: 0.5; box-shadow: 0 0 2px currentColor; } 50% { opacity: 1; box-shadow: 0 0 8px currentColor; } 100% { opacity: 0.5; box-shadow: 0 0 2px currentColor; } }
.status-light.green { background-color: var(--success-color); box-shadow: 0 0 10px var(--success-color); animation: none; }

.total-card { text-align: center; padding: 25px 15px; }
.total-card .label { margin-bottom: 8px; color: var(--text-muted); font-size: 14px;}
.total-card .value { font-size: 36px; font-weight: bold; margin-bottom: 20px; }
.pl-summary { display: flex; justify-content: space-around; margin-top: 15px; border-top: 1px solid #222; padding-top: 15px; }
.divider { width: 1px; background-color: #222; }
.pl-label { color: #8e8e93; font-size: 12px; }
#ledger-unrealized-pl, #ledger-realized-pl { font-size: 17px; }

.add-tx-form, .add-account-form { display: flex; flex-direction: column; gap: 12px; margin-bottom: 15px; }
.add-tx-form input, .add-tx-form select, .add-account-form input, .add-account-form select { padding: 12px; border-radius: 8px; border: 1px solid #222; background: #000; color: white; width: 100%; display: block; }
.list-group { display: flex; flex-direction: column; gap: 10px; }
.tab-switcher { display: flex; gap: 10px; margin-bottom: 15px; }
.tab-switcher button { flex: 1; background: #222; border: none; color: white; padding: 10px; border-radius: 8px; font-size: 14px;}
.tab-switcher button.active { background: #333; font-weight: bold; border: 1px solid #444;}
.net-worth-card .value { color: var(--text-main); }
.asset-summary { display: flex; justify-content: space-around; font-size: 12px; border-top: 1px solid #222; padding-top: 15px; gap: 5px; }
.s-label { color: #8e8e93; }
.s-value { font-size: 15px; }
.cash-color { color: var(--success-color); }
.stock-color { color: var(--primary-color); }
.debt-color { color: var(--danger-color); }

.fiat-section-header, .debt-section-header { font-size: 13px; font-weight: bold; text-transform: uppercase; letter-spacing: 1px; margin: 20px 0 10px 0; padding: 8px 12px; border-radius: 6px; display: inline-block; }
.fiat-section-header { color: var(--success-color); background: rgba(52, 199, 89, 0.1); border: 1px solid rgba(52, 199, 89, 0.2); box-shadow: 0 0 10px rgba(52, 199, 89, 0.1); }
.debt-section-header { color: var(--danger-color); background: rgba(255, 59, 48, 0.1); border: 1px solid rgba(255, 59, 48, 0.2); box-shadow: 0 0 10px rgba(255, 59, 48, 0.1); }

.btn-action { width: 100%; padding: 15px; font-size: 16px; font-weight: bold; color: white; border: none; border-radius: 10px; cursor: pointer; margin-top: 10px; transition: all 0.3s, transform 0.1s;}
.btn-action:hover:not(:disabled) { filter: brightness(1.1); }
.btn-action:active:not(:disabled) { transform: scale(0.97); opacity: 0.9; }
.btn-action:disabled { opacity: 0.6; cursor: not-allowed; }
.btn-action.primary { background-color: var(--primary-color); }
.btn-action.small { padding: 10px; font-size: 14px; margin: 0; width: auto;}
#rebalance-output { background: #000; padding: 15px; border-radius: 8px; border: 1px solid var(--border-color); font-family: monospace; font-size: 12px; color: #aaa; margin-top: 15px; overflow-x: auto;}
canvas { max-width: 100% !important; }

/* 🚨 報價跳動閃爍特效 */
@keyframes flash-red { 0% { background-color: rgba(255, 59, 48, 0.3); box-shadow: 0 0 20px rgba(255, 59, 48, 0.6); transform: scale(1.02); } 100% { background-color: rgba(255, 255, 255, 0.03); box-shadow: none; transform: scale(1); } }
@keyframes flash-green { 0% { background-color: rgba(52, 199, 89, 0.3); box-shadow: 0 0 20px rgba(52, 199, 89, 0.6); transform: scale(1.02); } 100% { background-color: rgba(255, 255, 255, 0.03); box-shadow: none; transform: scale(1); } }
.flash-up { animation: flash-red 0.5s ease-out !important; } 
.flash-down { animation: flash-green 0.5s ease-out !important; } 

.time-group { display: flex; background: rgba(255, 255, 255, 0.06); border-radius: 8px; padding: 4px; gap: 4px; margin-top: 10px; margin-bottom: 15px; }
.time-btn { flex: 1; background: transparent; border: none; color: #8e8e93; padding: 8px 0; border-radius: 6px; font-size: 13px; font-weight: 500; cursor: pointer; transition: all 0.2s ease; }
.time-btn.active { background: #3a3a3c; color: #fff; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3); }
.time-btn:hover:not(.active) { color: #fff; background: rgba(255, 255, 255, 0.1); }
.elegant-divider { height: 1px; width: 100%; margin: 16px 0; background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.15), transparent); }
.icon-btn { background: transparent; border: none; color: #8e8e93; font-size: 18px; cursor: pointer; padding: 6px; border-radius: 50%; display: flex; align-items: center; justify-content: center; transition: all 0.2s ease; }
.icon-btn:hover { color: #fff; background: rgba(255, 255, 255, 0.1); }
@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }
.spinning { animation: spin 1s linear infinite; pointer-events: none; opacity: 0.7; }
.btn-action.success { background: rgba(255, 255, 255, 0.08) !important; color: #e5e5ea !important; border: 1px solid rgba(255, 255, 255, 0.12) !important; box-shadow: none !important; transition: all 0.2s ease !important; }
.btn-action.success:hover { background: rgba(255, 255, 255, 0.15) !important; color: #ffffff !important; border-color: rgba(255, 255, 255, 0.3) !important; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4) !important; }
.btn-action.success:active { transform: scale(0.98) !important; background: rgba(255, 255, 255, 0.05) !important; }

#ma-toggles { display: flex; gap: 15px; align-items: center; }
#ma-toggles label { display: flex; align-items: center; gap: 6px; cursor: pointer; user-select: none; }
#ma-toggles input[type="checkbox"] { appearance: none; -webkit-appearance: none; width: 16px; height: 16px; border: 1px solid rgba(255, 255, 255, 0.15); border-radius: 4px; background: rgba(255, 255, 255, 0.05); cursor: pointer; position: relative; transition: all 0.2s ease; margin: 0; }
#ma-toggles input[type="checkbox"]:hover { border-color: rgba(255, 255, 255, 0.3); background: rgba(255, 255, 255, 0.08); }
#ma-toggles input[type="checkbox"]:checked { background: rgba(255, 255, 255, 0.2); border-color: rgba(255, 255, 255, 0.4); }
#ma-toggles input[type="checkbox"]:checked::after { content: ''; position: absolute; left: 4.5px; top: 1.5px; width: 4px; height: 8px; border: solid #ffffff; border-width: 0 2px 2px 0; transform: rotate(45deg); }

.inventory-card { cursor: pointer; transition: background 0.2s ease, border-color 0.2s ease; }
.inventory-card:hover { background: rgba(255, 255, 255, 0.06) !important; border-color: rgba(255, 255, 255, 0.2) !important; }
.stock-history-drawer { max-height: 0; opacity: 0; overflow: hidden; background: rgba(0, 0, 0, 0.3); transition: all 0.3s ease-in-out; }
.stock-history-drawer.open { max-height: 500px; opacity: 1; border-top: 1px solid rgba(255, 255, 255, 0.05); }
.drawer-content { padding: 15px; overflow-y: auto; max-height: 400px; }
.mini-tx-card { display: flex; justify-content: space-between; align-items: center; padding: 8px 0; border-bottom: 1px dashed rgba(255, 255, 255, 0.1); font-size: 13px; }
.mini-tx-card:last-child { border-bottom: none; }
.drawer-time-btn { font-size: 10px; color: #666; cursor: pointer; padding: 2px 8px; border-radius: 4px; transition: all 0.2s ease; user-select: none; }
.drawer-time-btn:hover { color: #fff; background: rgba(255, 255, 255, 0.1); }
.drawer-time-btn.active { color: #fff; background: rgba(255, 255, 255, 0.15); }

/* 💎 底部防彈：加入 max() 確保文字永遠不被橫槓吃掉 */
#bottom-bar {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    max-width: 430px;
    z-index: 1000;
    background: rgba(15, 15, 15, 0.95);
    backdrop-filter: blur(15px);
    border-top: 1px solid rgba(255, 255, 255, 0.05);

    /* 即使 iOS bug 給 0，強制留出 25px 的墊底空間 */
    padding-bottom: max(env(safe-area-inset-bottom), 25px);
    height: calc(65px + max(env(safe-area-inset-bottom), 25px));
    
    display: flex;
    justify-content: space-around;
    align-items: flex-start;
    padding-top: 10px;
}

.nav-item {
    flex: 1;
    height: 55px;
    margin: 0 5px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 5px;
    color: #666;
    cursor: pointer;
    border-radius: 12px;
    transition: all 0.2s ease;
    -webkit-user-select: none;
}

.nav-item.active { color: #ffffff; background: rgba(255, 255, 255, 0.12); }
.nav-item span { font-size: 11px; font-weight: 600; display: block; line-height: 1; }
.nav-item:hover:not(.active) { background: rgba(255, 255, 255, 0.05); color: #aaa; }

.range-btn.active { background: rgba(255, 255, 255, 0.15) !important; color: #ffffff !important; border: 1px solid rgba(255, 255, 255, 0.3) !important; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5) !important; }
.range-btn { background: rgba(255, 255, 255, 0.03) !important; color: #8e8e93 !important; border: 1px solid rgba(255, 255, 255, 0.08) !important; transition: all 0.2s ease !important; }
.range-btn:hover:not(.active) { background: rgba(255, 255, 255, 0.08) !important; color: #e5e5ea !important; }

.btn-dashed { width: 100%; padding: 12px; margin-top: 15px; background: transparent; border: 1px dashed rgba(255, 255, 255, 0.15); color: #8e8e93; border-radius: 10px; display: flex; justify-content: center; align-items: center; gap: 8px; font-size: 14px; font-weight: 500; line-height: 1; cursor: pointer; transition: all 0.2s ease; }
.btn-dashed:hover { border-color: rgba(255, 255, 255, 0.4); color: #ffffff; background: rgba(255, 255, 255, 0.03); }
.btn-dashed svg { transform: translateY(1px); }

.reb-buy { color: #ff3b30; text-shadow: 0 0 15px rgba(255, 59, 48, 0.4); }
.reb-sell { color: #34c759; text-shadow: 0 0 15px rgba(52, 199, 89, 0.4); }
.reb-hold { color: #8e8e93; letter-spacing: 1px; }

input[type="number"], input[type="text"], input[type="date"], select { width: 100%; background: rgba(255, 255, 255, 0.05); border: 1px solid rgba(255, 255, 255, 0.15); color: #ffffff; padding: 12px 15px; border-radius: 8px; font-size: 16px; box-sizing: border-box; transition: all 0.2s ease; appearance: none; -webkit-appearance: none; }
input[type="number"]:hover, input[type="text"]:hover, input[type="date"]:hover, select:hover { border-color: rgba(255, 255, 255, 0.3); }
input[type="number"]:focus, input[type="text"]:focus, input[type="date"]:focus, select:focus { outline: none; border-color: #0a84ff; background: rgba(255, 255, 255, 0.08); box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.2); }

select { background-image: url("data:image/svg+xml;charset=UTF-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='%238e8e93' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 12px center; background-size: 16px; padding-right: 40px; cursor: pointer; }
select option { background: #1c1c1e; color: #ffffff; }

.card h3, .inventory-card h3 { color: var(--text-main) !important; }
.value, .card .value, .inventory-card .value, #assets-net-worth { color: var(--text-main) !important; letter-spacing: 1px; }
.stock-btn, #new-stock-input, .add-stock-group button { color: var(--text-main) !important; }