html {
    background: #1a1a1a;
    color-scheme: dark;
}

body {
    background: #1a1a1a;
    color: #eee;
    font-family: sans-serif;
    padding: 20px;
    max-width: 1200px;
    margin: 0 auto;
}

.container {
    display: flex;
    gap: 20px;
}

.card {
    background: #2a2a2a;
    padding: 15px;
    border-radius: 8px;
    flex: 1;
}

input,
select,
button {
    background: #333;
    color: white;
    border: 1px solid #444;
    padding: 8px;
    border-radius: 4px;
}

button {
    background: #007aff;
    cursor: pointer;
    border: none;
}

table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 10px;
}

th,
td {
    text-align: left;
    padding: 10px;
    border-bottom: 1px solid #333;
    vertical-align: top;
}

.red  { color: #ff453a; }
.green { color: #32d74b; }
.blue  { color: #0a84ff; }

/* =====================
   Header & 表單區
   ===================== */
.header {
    margin-bottom: 20px;
    text-align: center;
}

/* 桌面版：水平排列，label 隱藏 */
.trade-form {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    align-items: center;
    justify-content: center;
    margin-top: 10px;
}

.form-field {
    display: flex;
    align-items: center;
    gap: 4px;
}

/* 桌面版 label 隱藏，placeholder 就夠 */
.form-field label {
    display: none;
}

/* 桌面版各欄位寬度 */
.trade-form input[name="code"]   { width: 60px; }
.trade-form input[name="name"]   { width: 100px; }
.trade-form input[name="broker"] { width: 80px; }
.trade-form input[name="date"]   { width: 130px; }
.trade-form select[name="type"]  { width: 80px; }
.trade-form input[name="price"]  { width: 80px; }
.trade-form input[name="lots"]   { width: 60px; }
.trade-form input[name="shares"] { width: 60px; }
.trade-form input[name="fee"]    { width: 70px; }
.trade-form input[name="tax"]    { width: 70px; }

.form-submit-btn,
.form-sell-btn,
.form-cancel-btn,
.form-delete-btn {
    padding: 8px 16px;
    background: #007aff;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-weight: bold;
    white-space: nowrap;
}

/* =====================
   買入歷史欄位
   ===================== */
.buy-history-th {
    min-width: 175px;
    white-space: nowrap;
}

.buy-history-cell {
    min-width: 175px;
    font-size: 12px;
    vertical-align: top;
    padding-top: 8px;
    padding-bottom: 8px;
}

.buy-history-item {
    display: flex;
    gap: 6px;
    align-items: baseline;
    padding: 3px 0;
    border-bottom: 1px solid #3a3a3a;
    white-space: nowrap;
}

.buy-history-item:last-child {
    border-bottom: none;
}

.bh-date {
    color: #888;
    font-size: 11px;
    min-width: 80px;
}

.bh-price {
    color: #ff6b6b;
    font-weight: bold;
    min-width: 55px;
}

.bh-shares {
    color: #5eaeff;
    font-size: 11px;
}

.bh-empty {
    color: #444;
}

/* =====================
   損益 Pill
   ===================== */
.pill-container {
    display: flex;
    align-items: stretch;
    gap: 15px;
    margin-top: 10px;
}

.pill {
    background: #52504b;
    border-radius: 20px;
    padding: 8px 16px;
    text-align: center;
    min-width: 90px;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.pill-title {
    font-size: 13px;
    color: #e0e0e0;
    margin-bottom: 4px;
}

.pill-value {
    font-size: 18px;
    font-weight: bold;
    color: #fff;
}

.pill-value.red   { color: #ff453a; }
.pill-value.green { color: #32d74b; }

.pill-value .action-label {
    margin-right: 5px;
    font-weight: normal;
}

.pill-separator {
    color: #fff;
    font-size: 18px;
    align-self: center;
}

.trade-header {
    font-size: 20px;
    color: #eee;
    margin-bottom: 10px;
    letter-spacing: 0.5px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.th-stock {
    font-size: 18px;
    font-weight: 700;
    color: #fff;
}

.th-stock em {
    font-style: normal;
    font-size: 14px;
    font-weight: 400;
    color: #5eaeff;
    margin-left: 6px;
}

.th-shares-badge {
    font-size: 12px;
    background: #007aff;
    color: #fff;
    padding: 2px 8px;
    border-radius: 12px;
    margin-left: 8px;
    font-weight: bold;
    display: inline-block;
    vertical-align: middle;
}


.th-date {
    font-size: 13px;
    color: #888;
    font-weight: 400;
}

.inv-qty {
    font-size: 14px;
    margin-left: 12px;
    color: #ddd;
}

.mobile-only {
    display: none;
}

.form-toggle-btn {
    display: none;
}

.table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    width: 100%;
}

/* =====================
   Mobile (≤768px)
   ===================== */
@media (max-width: 768px) {
    body {
        padding: 10px;
    }

    .container {
        flex-direction: column;
        gap: 15px;
    }

    .mobile-only {
        display: block;
    }

    .form-toggle-btn {
        display: block;
        grid-column: 1 / -1;
        width: 100%;
        padding: 14px;
        font-size: 16px;
        background: #007aff;
        color: white;
        border: none;
        border-radius: 4px;
        cursor: pointer;
        font-weight: bold;
        white-space: nowrap;
        margin-top: 4px;
    }

    .trade-form.collapsed .form-field {
        display: none !important;
    }
    .trade-form.collapsed .form-submit-btn,
    .trade-form.collapsed .form-sell-btn,
    .trade-form.collapsed .form-cancel-btn,
    .trade-form.collapsed .form-delete-btn {
        display: none !important;
    }
    .trade-form:not(.collapsed) .form-toggle-btn {
        display: none !important;
    }
    .trade-form:not(.collapsed) .form-cancel-btn {
        display: block !important;
    }

    /* --- 手機表單：2欄 grid，label 出現在每個欄位上方 --- */
    .trade-form {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 10px;
        align-items: stretch;
    }

    .form-field {
        display: flex;
        flex-direction: column;
        gap: 4px;
        align-items: flex-start;
    }

    .form-field label {
        display: block;
        font-size: 12px;
        color: #aaa;
        font-weight: 500;
    }

    .form-field input,
    .form-field select {
        width: 100% !important;
        box-sizing: border-box;
    }

    /* 日期欄位橫跨全寬（比較長） */
    .form-field-wide {
        grid-column: 1 / -1;
    }

    .form-submit-btn,
    .form-sell-btn,
    .form-cancel-btn,
    .form-delete-btn {
        grid-column: 1 / -1;
        width: 100%;
        padding: 14px;
        font-size: 16px;
        margin-top: 4px;
        margin-left: 0 !important;
    }

    /* --- Pill 橫向滾動 --- */
    .pill-container {
        flex-wrap: nowrap;
        overflow-x: auto;
        padding-bottom: 5px;
        justify-content: flex-start;
        gap: 8px;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }

    .pill-container::-webkit-scrollbar {
        display: none;
    }

    .pill {
        flex: 1 1 0;
        min-width: 0;
        padding: 8px 4px;
    }

    .pill.profit-pill {
        margin-left: 0 !important;
    }

    .inv-qty {
        display: block;
        margin-left: 0;
        margin-top: 4px;
        font-size: 13px;
    }

    .pill-separator {
        flex-shrink: 0;
    }

    /* --- 買入歷史欄位在手機版保持可讀（table 本身橫向可滾動） --- */
    .buy-history-item {
        flex-direction: column;
        gap: 1px;
        padding: 4px 0;
    }

    .bh-date,
    .bh-shares {
        font-size: 10px;
    }

    .bh-price {
        font-size: 13px;
    }
}

/* =====================
   各股買入成本分析卡片
   ===================== */
.buy-analysis-card {
    margin-top: 20px;
}

.buy-analysis-card h3 {
    margin-bottom: 16px;
}

.buy-analysis-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 16px;
}

.stock-analysis-card {
    background: #1e1e1e;
    border: 1px solid #3d3d3d;
    border-radius: 12px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

/* --- Card Header --- */
.sac-header {
    background: linear-gradient(135deg, #2a2a3a 0%, #252530 100%);
    padding: 14px 16px 12px;
    border-bottom: 1px solid #3d3d3d;
}

.sac-top {
    display: flex;
    align-items: baseline;
    gap: 10px;
    margin-bottom: 8px;
}

.sac-code {
    font-size: 20px;
    font-weight: 700;
    color: #5eaeff;
    letter-spacing: 0.5px;
}

.sac-name {
    font-size: 16px;
    color: #ddd;
    font-weight: 500;
}

.sac-summary {
    display: flex;
    gap: 14px;
    flex-wrap: wrap;
    font-size: 12px;
    color: #999;
}

.sac-summary strong {
    font-size: 13px;
}

/* --- Column Header --- */
.sac-col-head {
    display: grid;
    grid-template-columns: 88px 68px 52px 68px 1fr;
    gap: 4px;
    padding: 6px 14px;
    font-size: 11px;
    color: #666;
    background: #252525;
    border-bottom: 1px solid #333;
    letter-spacing: 0.5px;
}

.sac-col-head span:last-child {
    text-align: right;
    padding-right: 4px;
}

/* --- Lot Rows --- */
.sac-body {
    flex: 1;
    padding: 0;
}

.sac-lot-row {
    display: grid;
    grid-template-columns: 88px 68px 52px 68px 1fr;
    grid-template-rows: auto auto;
    align-items: center;
    gap: 8px 4px;
    padding: 10px 14px;
    border-bottom: 1px solid #2a2a2a;
    transition: background 0.15s;
}

.sac-lot-row .sac-date {
    grid-column: 1 / 2;
    grid-row: 1;
}

.sac-lot-row .sac-price {
    grid-column: 2 / 3;
    grid-row: 1;
}

.sac-lot-row .sac-shares {
    grid-column: 3 / 4;
    grid-row: 1;
}

.sac-lot-row .sac-amount {
    grid-column: 4 / 5;
    grid-row: 1;
}

.sac-lot-row .sac-badge {
    grid-column: 5 / 6;
    grid-row: 1;
    justify-self: end;
}

.sac-lot-row .sac-bar-wrap {
    grid-column: 1 / 6;
    grid-row: 2;
    margin-top: 2px;
}

.sac-lot-row:last-child {
    border-bottom: none;
}

.sac-lot-row:hover {
    background: #262626;
}

/* Held lots: normal brightness; Sold lots: dimmed */
.sac-lot-row.sac-sold {
    opacity: 0.55;
}

.sac-date {
    color: #888;
    font-size: 12px;
    font-variant-numeric: tabular-nums;
}

.sac-price {
    font-weight: 700;
    font-size: 14px;
    color: #ff6b6b;
}

.sac-price em {
    font-style: normal;
    font-size: 11px;
    font-weight: 400;
    color: #999;
    margin-left: 1px;
}

.sac-shares {
    font-size: 12px;
    color: #5eaeff;
}

.sac-amount {
    font-size: 12px;
    color: #aaa;
    font-variant-numeric: tabular-nums;
}

/* --- Price Level Bar --- */
.sac-bar-wrap {
    height: 6px;
    background: #2d2d2d;
    border-radius: 3px;
    overflow: hidden;
}

.sac-bar {
    height: 100%;
    background: linear-gradient(90deg, #ff8c42, #ff453a);
    border-radius: 3px;
    transition: width 0.4s ease;
}

/* Low-price entries get a cooler color */
.sac-lot-row:has(.sac-bar[style*="width:1"]) .sac-bar,
.sac-lot-row:has(.sac-bar[style*="width:2"]) .sac-bar {
    background: linear-gradient(90deg, #32d74b, #2fbc44);
}

/* --- Held / Sold Badge --- */
.sac-badge {
    font-size: 10px;
    font-weight: 700;
    padding: 2px 5px;
    border-radius: 4px;
    text-align: center;
    letter-spacing: 0.3px;
    white-space: nowrap;
}

.sac-badge.held {
    background: rgba(50, 215, 75, 0.18);
    color: #32d74b;
    border: 1px solid rgba(50, 215, 75, 0.35);
}

.sac-badge.sold {
    background: rgba(180, 180, 180, 0.1);
    color: #666;
    border: 1px solid rgba(180, 180, 180, 0.2);
}

/* --- Card Footer --- */
.sac-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    background: #252528;
    border-top: 1px solid #3a3a3a;
    gap: 8px;
    flex-wrap: wrap;
}

.sac-footer > div {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
}

.sac-footer-label {
    font-size: 10px;
    color: #666;
    letter-spacing: 0.5px;
    text-transform: uppercase;
}

/* =====================
   Mobile — Buy Analysis
   ===================== */
@media (max-width: 768px) {
    .buy-analysis-grid {
        grid-template-columns: 1fr;
    }

    /* Hide column headers for layout space */
    .sac-col-head {
        display: none !important;
    }

    /* Stacked row grid for optimal readability on mobile */
    .sac-lot-row {
        display: grid !important;
        grid-template-columns: 1.2fr 1fr 1.2fr !important;
        grid-template-rows: auto auto auto !important;
        gap: 10px 8px !important;
        padding: 14px !important;
        align-items: center !important;
        border-bottom: 1px solid #333 !important;
    }

    .sac-code {
        font-size: 22px !important;
    }
    
    .sac-name {
        font-size: 18px !important;
    }
    
    .sac-summary {
        font-size: 14px !important;
    }
    
    .sac-summary strong {
        font-size: 15px !important;
    }

    /* Date and Edit indicator */
    .sac-lot-row .sac-date {
        grid-column: 1 / 3 !important;
        grid-row: 1 !important;
        font-size: 15px !important;
        font-weight: bold !important;
        color: #ddd !important;
    }

    /* Held / Sold status badge */
    .sac-lot-row .sac-badge {
        grid-column: 3 / 4 !important;
        grid-row: 1 !important;
        justify-self: end !important;
        font-size: 12px !important;
        padding: 3px 8px !important;
    }

    /* Buy Price */
    .sac-lot-row .sac-price {
        grid-column: 1 / 2 !important;
        grid-row: 2 !important;
        font-size: 17px !important;
        color: #ff6b6b !important;
    }
    
    .sac-lot-row .sac-price em {
        font-size: 12px !important;
    }

    /* Shares count */
    .sac-lot-row .sac-shares {
        grid-column: 2 / 3 !important;
        grid-row: 2 !important;
        justify-self: start !important;
        font-size: 16px !important;
        color: #5eaeff !important;
        font-weight: bold !important;
    }

    /* Total amount and fees */
    .sac-lot-row .sac-amount {
        grid-column: 3 / 4 !important;
        grid-row: 2 !important;
        justify-self: end !important;
        text-align: right !important;
        font-size: 16px !important;
        color: #eee !important;
        font-weight: bold !important;
    }
    
    .sac-lot-row .sac-amount em {
        display: inline !important;
        font-size: 12px !important;
        color: #888 !important;
        margin-left: 4px !important;
    }

    /* Price Level Bar */
    .sac-lot-row .sac-bar-wrap {
        grid-column: 1 / 4 !important;
        grid-row: 3 !important;
        height: 6px !important;
        margin-top: 2px !important;
    }
    
    /* Footer text sizes */
    .sac-footer-label {
        font-size: 12px !important;
    }
    
    .sac-footer strong {
        font-size: 16px !important;
    }
    
    .sac-footer strong.red {
        font-size: 18px !important;
    }
}

/* =====================
   目前持股 & P&L Grid Layout
   ===================== */
.holding-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 16px;
    margin-top: 15px;
}

.holding-item {
    background: #222;
    border: 1px solid #3a3a3a;
    border-radius: 12px;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.25);
    transition: transform 0.2s, border-color 0.2s;
}

.holding-item:hover {
    transform: translateY(-2px);
    border-color: #555;
}

.hi-top {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
}

.hi-name {
    font-size: 18px;
    font-weight: 700;
    color: #fff;
}

.hi-code {
    font-size: 13px;
    color: #5eaeff;
    margin-left: 6px;
    font-weight: 500;
}

.hi-shares {
    font-size: 14px;
    color: #bbb;
    font-variant-numeric: tabular-nums;
}

.hi-rows {
    border-top: 1px solid #333;
    border-bottom: 1px solid #333;
    padding: 8px 0;
    display: flex;
    flex-direction: column;
    gap: 6px;
    max-height: 120px;
    overflow-y: auto;
}

.hi-lot {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 13px;
    color: #ddd;
    font-variant-numeric: tabular-nums;
}

.hi-lot-date {
    color: #888;
    min-width: 80px;
}

.hi-lot-price {
    font-weight: bold;
    min-width: 65px;
}

.hi-lot-shares {
    color: #5eaeff;
    min-width: 60px;
}

.hi-lot-amt {
    font-weight: bold;
    min-width: 70px;
    text-align: right;
}

.hi-footer {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    font-size: 13px;
}

.hi-footer > div {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.hi-label {
    font-size: 11px;
    color: #777;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.hi-cur-price {
    font-size: 15px;
    color: #eee;
    font-weight: bold;
}

.hi-unreal-pnl {
    font-size: 15px;
    font-weight: bold;
}

/* P&L Grid: Desktop 2-column layout */
.pnl-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    margin-top: 20px;
}

/* Unrealized P&L Table Styling */
.unreal-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 10px;
    font-size: 14px;
}

.unreal-table th {
    background: #222;
    color: #aaa;
    font-weight: 600;
    font-size: 13px;
    padding: 12px 10px;
    border-bottom: 2px solid #333;
}

.unreal-table td {
    padding: 12px 10px;
    border-bottom: 1px solid #333;
    vertical-align: middle;
}

.unreal-row:hover {
    background: #333;
}

.unreal-total-row td {
    border-top: 2px solid #007aff;
    background: #252528;
    padding: 14px 10px;
    font-weight: bold;
}

.unreal-hint {
    font-size: 12px;
    color: #888;
    font-weight: normal;
    margin-left: 4px;
}

/* Responsive updates for P&L grid and tables */
@media (max-width: 900px) {
    .pnl-grid {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 768px) {
    .holding-grid {
        grid-template-columns: 1fr;
    }
    
    /* Enlarge Unrealized P&L table text */
    .unreal-table th {
        font-size: 14px !important;
        padding: 10px 6px !important;
    }
    
    .unreal-table td {
        padding: 12px 6px !important;
        font-size: 16px !important;
    }
    
    .unreal-total-row td {
        font-size: 17px !important;
    }
    
    /* Enlarge holding item (currently held stocks) text sizes for elderly visibility */
    .hi-name {
        font-size: 21px !important;
    }
    
    .hi-code {
        font-size: 15px !important;
    }
    
    .hi-shares {
        font-size: 16px !important;
    }
    
    .hi-lot {
        font-size: 15px !important;
        padding: 5px 0 !important;
    }
    
    .hi-lot-shares {
        font-size: 15px !important;
    }
    
    .hi-lot-amt {
        font-size: 15px !important;
    }
    
    .hi-footer {
        grid-template-columns: 1fr 1fr;
        font-size: 15px !important;
    }
    
    .hi-label {
        font-size: 13px !important;
    }
    
    .hi-cur-price,
    .hi-unreal-pnl {
        font-size: 18px !important;
    }
}

/* =====================
   近日交割款樣式
   ===================== */
.settlement-summary {
    display: flex;
    gap: 15px;
    margin-bottom: 15px;
    flex-wrap: wrap;
}

.settlement-summary .summary-item {
    flex: 1;
    min-width: 220px;
    background: #222;
    padding: 12px 15px;
    border-radius: 6px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    border-left: 4px solid #444;
}

.settlement-summary .summary-item.buy {
    border-left-color: #ff453a;
}

.settlement-summary .summary-item.sell {
    border-left-color: #32d74b;
}

.settlement-summary .summary-item.net {
    border-left-color: #0a84ff;
}

.settlement-summary .summary-item .label {
    font-size: 13px;
    color: #aaa;
    font-weight: 500;
}

.settlement-summary .summary-item strong {
    font-size: 20px;
}

.settlement-table th,
.settlement-table td {
    padding: 8px 10px;
    font-size: 13px;
    white-space: nowrap;
}

.settlement-table tr:hover {
    background: rgba(255, 255, 255, 0.02);
}

@media (max-width: 768px) {
    .settlement-summary {
        flex-direction: column;
        gap: 10px;
    }
    .settlement-summary .summary-item {
        min-width: 100%;
        padding: 14px 16px !important;
    }
    .settlement-summary .summary-item .label {
        font-size: 14px !important;
    }
    .settlement-summary .summary-item strong {
        font-size: 24px !important;
    }
    .settlement-table th {
        padding: 8px 6px !important;
        font-size: 13px !important;
    }
    .settlement-table td {
        padding: 10px 8px !important;
        font-size: 15px !important;
    }
}

/* =====================
   目前持股標題統計樣式
   ===================== */
.holding-header-wrap {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
    flex-wrap: wrap;
    gap: 15px;
    border-bottom: 1px solid #333;
    padding-bottom: 12px;
}

.holding-header-wrap h3 {
    margin: 0;
}

.holding-stats-header {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 14px;
}

.holding-stats-header .stat-item {
    display: flex;
    align-items: center;
    gap: 6px;
}

.holding-stats-header .stat-label {
    color: #aaa;
}

.holding-stats-header .stat-val {
    font-weight: bold;
}

.holding-stats-header .stat-separator {
    color: #444;
    font-weight: 300;
}

@media (max-width: 768px) {
    .holding-header-wrap {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
    }
    
    .holding-stats-header {
        display: flex !important;
        flex-direction: column !important;
        gap: 8px !important;
        width: 100% !important;
        background: #222 !important;
        padding: 12px 14px !important;
        border-radius: 8px !important;
        box-sizing: border-box !important;
    }

    .holding-stats-header .stat-item {
        display: flex !important;
        justify-content: space-between !important;
        align-items: center !important;
        width: 100% !important;
        border-bottom: 1px solid #333 !important;
        padding-bottom: 8px !important;
    }

    .holding-stats-header .stat-item:last-child {
        border-bottom: none !important;
        padding-bottom: 0 !important;
    }

    .holding-stats-header .stat-label {
        font-size: 15px !important;
    }

    .holding-stats-header .stat-val {
        font-size: 18px !important;
    }

    .holding-stats-header .stat-separator {
        display: none !important;
    }
}

/* =====================
   Footer 樣式
   ===================== */
.footer {
    text-align: center;
    padding: 30px 15px;
    margin-top: 40px;
    border-top: 1px solid #333;
    color: #666;
    font-size: 13px;
}

.footer a {
    color: #888;
    text-decoration: none;
    transition: color 0.2s ease;
}

.footer a:hover {
    color: #eee;
}

