/* ============ 基礎 ============ */
* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
    --gold: #c9a227;
    --gold-dark: #a8851a;
    --gold-light: #f5e9c8;
    --bg: #f4f5f7;
    --sidebar-bg: #1d2330;
    --sidebar-hover: #2a3245;
    --text: #2b2f36;
    --muted: #8a909c;
    --border: #e3e6eb;
    --danger: #d04545;
    --green: #2e9e5b;
    --red: #d04545;
    --radius: 10px;
    --shadow: 0 2px 10px rgba(20, 26, 40, .08);
}
html, body { height: 100%; }
body {
    font-family: "Microsoft JhengHei", "PingFang TC", "Noto Sans TC", sans-serif;
    background: var(--bg); color: var(--text); font-size: 14px;
}

/* ============ 手機頂欄(桌機隱藏) ============ */
.topbar {
    display: none; position: fixed; top: 0; left: 0; right: 0; z-index: 700;
    height: 52px; background: var(--sidebar-bg); color: #fff;
    align-items: center; gap: 10px; padding: 0 12px;
    box-shadow: 0 2px 8px rgba(10,14,24,.25);
}
.topbar-title { font-size: 15px; font-weight: 700; }
.topbar-title small { font-size: 11px; color: #8a93a8; font-weight: 400; margin-left: 6px; }
.hamburger {
    width: 40px; height: 40px; border: none; background: transparent; cursor: pointer;
    display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 5px;
    border-radius: 8px;
}
.hamburger:active { background: var(--sidebar-hover); }
.hamburger span { display: block; width: 20px; height: 2px; background: #fff; border-radius: 2px; }
.backdrop {
    display: none; position: fixed; inset: 0; z-index: 790; background: rgba(18,22,32,.5);
}
.backdrop.show { display: block; }

/* ============ 版面 ============ */
.layout { display: flex; min-height: 100vh; }
.sidebar {
    width: 220px; flex: 0 0 220px; background: var(--sidebar-bg); color: #cdd3df;
    display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 18px 16px; border-bottom: 1px solid rgba(255,255,255,.08); }
.brand-logo {
    width: 40px; height: 40px; border-radius: 10px; flex: 0 0 40px;
    background: linear-gradient(135deg, #e8c95a, #a8851a);
    color: #1d2330; font-size: 20px; font-weight: 700;
    display: flex; align-items: center; justify-content: center;
}
.brand-name { font-size: 15px; font-weight: 700; color: #fff; }
.brand-sub { font-size: 11px; color: #8a93a8; margin-top: 2px; }
.nav { padding: 10px 0 24px; }
.nav-group { padding: 14px 18px 6px; font-size: 11px; color: #6b7488; letter-spacing: 2px; }
.nav-item {
    display: flex; align-items: center; gap: 10px; padding: 10px 18px;
    cursor: pointer; color: #cdd3df; transition: background .15s, color .15s;
    border-left: 3px solid transparent; user-select: none;
}
.nav-item:hover { background: var(--sidebar-hover); color: #fff; }
.nav-item.active { background: var(--sidebar-hover); color: var(--gold); border-left-color: var(--gold); font-weight: 700; }
.nav-ico { font-size: 15px; }
.main { flex: 1; padding: 24px 28px; min-width: 0; }

/* ============ 頁面元素 ============ */
.page-head { display: flex; align-items: center; gap: 14px; margin-bottom: 18px; flex-wrap: wrap; }
.page-title { font-size: 21px; font-weight: 700; }
.page-title small { font-size: 12px; color: var(--muted); font-weight: 400; margin-left: 8px; }
.spacer { flex: 1; }
.card { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; margin-bottom: 18px; }
.card-head { padding: 14px 18px; border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.card-title { font-weight: 700; font-size: 15px; }
.card-body { padding: 16px 18px; }

.stat-row { display: flex; gap: 14px; margin-bottom: 18px; flex-wrap: wrap; }
.stat {
    flex: 1; min-width: 150px; background: #fff; border-radius: var(--radius);
    box-shadow: var(--shadow); padding: 14px 18px;
}
.stat .lbl { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.stat .val { font-size: 20px; font-weight: 700; }
.stat .val.pos { color: var(--green); }
.stat .val.neg { color: var(--red); }
.stat .val.gold { color: var(--gold-dark); }

/* ============ 按鈕(全自製樣式) ============ */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    border: none; outline: none; cursor: pointer; user-select: none;
    font-family: inherit; font-size: 13.5px; font-weight: 600;
    padding: 8px 16px; border-radius: 8px;
    background: #eceef2; color: var(--text);
    transition: filter .15s, transform .05s, box-shadow .15s;
}
.btn:hover { filter: brightness(.96); }
.btn:active { transform: translateY(1px); }
.btn-gold { background: linear-gradient(135deg, #d9b545, #b08c1d); color: #fff; box-shadow: 0 2px 6px rgba(176,140,29,.35); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-sm { padding: 4px 10px; font-size: 12.5px; border-radius: 6px; }
.btn-icon { padding: 4px 8px; background: transparent; font-size: 14px; }
.btn-icon:hover { background: #f0f1f4; }

/* ============ 表格 ============ */
.tbl-wrap { overflow-x: auto; }
table.tbl { width: 100%; border-collapse: collapse; white-space: nowrap; }
.tbl th {
    background: #f7f8fa; color: #5a6170; font-weight: 700; font-size: 12.5px;
    padding: 10px 12px; text-align: left; border-bottom: 2px solid var(--border);
    position: sticky; top: 0;
}
.tbl td { padding: 9px 12px; border-bottom: 1px solid #eef0f3; }
.tbl tbody tr:hover { background: #fbf8ee; }
.tbl .num { text-align: right; font-variant-numeric: tabular-nums; }
.tbl th.num { text-align: right; }
.tbl .pos { color: var(--green); }
.tbl .neg { color: var(--red); }
.tbl .muted { color: var(--muted); }
.tbl tfoot td { background: #fdf6e0; font-weight: 700; border-top: 2px solid var(--gold-light); }
.tbl .sub-row td { background: #f4f6f9; font-weight: 700; }
.tbl .cat-row td { background: #ece4cb; font-weight: 700; color: #6b5510; }
.tbl .opening td { background: #f7f8fa; color: var(--muted); font-style: italic; }
.tag {
    display: inline-block; padding: 2px 10px; border-radius: 99px; font-size: 12px; font-weight: 700;
}
.tag-buy { background: #e8f3ec; color: var(--green); }
.tag-sell { background: #fdeaea; color: var(--red); }
.tag-other { background: #eef0f3; color: #5a6170; }
.empty { padding: 40px; text-align: center; color: var(--muted); }

/* 可點擊編輯的儲存格 */
.editable-cell { cursor: pointer; border-bottom: 1px dashed var(--gold); }
.editable-cell:hover { background: var(--gold-light); }

/* ============ 表單欄位(全自製樣式) ============ */
.fld { margin-bottom: 14px; }
.fld label { display: block; font-size: 12.5px; color: #5a6170; font-weight: 700; margin-bottom: 6px; }
.fld label .req { color: var(--danger); margin-left: 2px; }
.inp {
    width: 100%; font-family: inherit; font-size: 14px; color: var(--text);
    border: 1px solid #d4d8e0; border-radius: 8px; padding: 9px 12px;
    background: #fff; outline: none; transition: border-color .15s, box-shadow .15s;
    appearance: none; -webkit-appearance: none;
}
.inp:focus { border-color: var(--gold); box-shadow: 0 0 0 3px rgba(201,162,39,.18); }
.inp.err { border-color: var(--danger); box-shadow: 0 0 0 3px rgba(208,69,69,.15); }
.inp[readonly] { cursor: pointer; background: #fff; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.form-grid .full { grid-column: 1 / -1; }
.fld-hint { font-size: 11.5px; color: var(--muted); margin-top: 4px; }

/* ============ 自製下拉選單 ============ */
.cselect { position: relative; }
.cselect-toggle {
    width: 100%; font-family: inherit; font-size: 14px; text-align: left;
    border: 1px solid #d4d8e0; border-radius: 8px; padding: 9px 34px 9px 12px;
    background: #fff; cursor: pointer; outline: none; color: var(--text);
    transition: border-color .15s, box-shadow .15s; position: relative; user-select: none;
}
.cselect-toggle::after {
    content: ""; position: absolute; right: 13px; top: 50%;
    width: 7px; height: 7px; border-right: 2px solid #9aa1ae; border-bottom: 2px solid #9aa1ae;
    transform: translateY(-70%) rotate(45deg); transition: transform .15s;
}
.cselect.open .cselect-toggle { border-color: var(--gold); box-shadow: 0 0 0 3px rgba(201,162,39,.18); }
.cselect.err .cselect-toggle { border-color: var(--danger); box-shadow: 0 0 0 3px rgba(208,69,69,.15); }
.cselect.open .cselect-toggle::after { transform: translateY(-30%) rotate(-135deg); }
.cselect-toggle .ph { color: #9aa1ae; }
.cselect-menu {
    position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 500;
    background: #fff; border: 1px solid var(--border); border-radius: 8px;
    box-shadow: 0 8px 24px rgba(20,26,40,.14); max-height: 240px; overflow-y: auto;
    display: none; padding: 4px;
}
.cselect.open .cselect-menu { display: block; }
.cselect-item {
    padding: 8px 12px; border-radius: 6px; cursor: pointer; user-select: none;
}
.cselect-item:hover { background: var(--gold-light); }
.cselect-item.sel { background: var(--gold); color: #fff; font-weight: 700; }

/* ============ 自製日期選擇器 ============ */
.dp-panel {
    position: absolute; z-index: 900; background: #fff; border: 1px solid var(--border);
    border-radius: 10px; box-shadow: 0 10px 30px rgba(20,26,40,.18); padding: 12px; width: 252px;
}
.dp-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.dp-title { font-weight: 700; font-size: 14px; }
.dp-nav {
    border: none; background: #f0f1f4; border-radius: 6px; width: 26px; height: 26px;
    cursor: pointer; font-size: 13px; color: #5a6170; font-family: inherit;
}
.dp-nav:hover { background: var(--gold-light); }
.dp-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; }
.dp-dow { text-align: center; font-size: 11px; color: var(--muted); padding: 4px 0; font-weight: 700; }
.dp-day {
    text-align: center; padding: 6px 0; border-radius: 6px; cursor: pointer; font-size: 13px; user-select: none;
}
.dp-day:hover { background: var(--gold-light); }
.dp-day.out { color: #c4c9d2; }
.dp-day.today { box-shadow: inset 0 0 0 1px var(--gold); }
.dp-day.sel { background: var(--gold); color: #fff; font-weight: 700; }
.dp-foot { display: flex; justify-content: space-between; margin-top: 8px; }

/* ============ Modal(自製對話框) ============ */
.overlay {
    position: fixed; inset: 0; z-index: 1000; background: rgba(18, 22, 32, .45);
    display: flex; align-items: flex-start; justify-content: center; padding: 6vh 16px;
    animation: fadeIn .15s ease;
    overflow-y: auto;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.dialog {
    background: #fff; border-radius: 14px; width: 560px; max-width: 100%;
    box-shadow: 0 20px 60px rgba(10,14,24,.3); animation: slideUp .18s ease;
}
@keyframes slideUp { from { transform: translateY(14px); opacity: 0; } to { transform: none; opacity: 1; } }
.dialog-sm { width: 400px; }
.dlg-head {
    padding: 16px 20px; border-bottom: 1px solid var(--border);
    display: flex; align-items: center; justify-content: space-between;
}
.dlg-title { font-size: 16px; font-weight: 700; }
.dlg-x {
    border: none; background: transparent; font-size: 18px; cursor: pointer; color: var(--muted);
    width: 30px; height: 30px; border-radius: 8px; font-family: inherit;
}
.dlg-x:hover { background: #f0f1f4; color: var(--text); }
.dlg-body { padding: 18px 20px 6px; }
.dlg-foot { padding: 12px 20px 18px; display: flex; justify-content: flex-end; gap: 10px; }
.confirm-msg { font-size: 14.5px; line-height: 1.7; padding: 4px 0 10px; }
.confirm-icon {
    width: 44px; height: 44px; border-radius: 50%; background: #fdeaea; color: var(--danger);
    font-size: 22px; display: flex; align-items: center; justify-content: center; margin-bottom: 12px;
}

/* ============ Toast ============ */
#toast-root { position: fixed; top: 18px; right: 18px; z-index: 2000; display: flex; flex-direction: column; gap: 10px; }
.toast {
    min-width: 220px; max-width: 360px; background: #fff; border-radius: 10px;
    box-shadow: 0 8px 28px rgba(20,26,40,.22); padding: 12px 16px;
    display: flex; align-items: center; gap: 10px; font-size: 13.5px;
    border-left: 4px solid var(--green); animation: toastIn .2s ease;
}
.toast.error { border-left-color: var(--danger); }
.toast.out { opacity: 0; transform: translateX(20px); transition: all .25s; }
@keyframes toastIn { from { opacity: 0; transform: translateX(24px); } to { opacity: 1; transform: none; } }
.toast .t-ico { font-size: 16px; }

/* ============ 其他 ============ */
.formula-note {
    background: #fdf6e0; border: 1px solid #efe2b0; color: #7c6516;
    border-radius: 8px; padding: 10px 14px; font-size: 12.5px; margin-bottom: 14px; line-height: 1.7;
}
.search-inp { width: 220px; }
.inline-edit { display: inline-flex; gap: 6px; align-items: center; }
.inline-edit .inp { width: 130px; padding: 5px 8px; font-size: 13px; }
.loan-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; align-items: start; }
@media (max-width: 1100px) { .loan-grid { grid-template-columns: 1fr; } }

/* ============ 現場收銀 POS ============ */
.pos-grid { display: grid; grid-template-columns: 1.05fr .95fr; gap: 18px; align-items: start; }
.pos-seg { display: flex; gap: 10px; }
.pos-seg .seg {
    flex: 1; text-align: center; padding: 13px 6px; border-radius: 12px; cursor: pointer;
    background: #f0f1f4; border: 2px solid transparent; font-size: 17px; font-weight: 700;
    color: #5a6170; user-select: none; transition: all .15s;
}
.pos-seg .seg small { display: block; font-size: 11px; font-weight: 400; margin-top: 3px; color: #9aa1ae; }
.seg-buy.active { background: #e8f3ec; color: var(--green); border-color: var(--green); }
.seg-sell.active { background: #fdeaea; color: var(--red); border-color: var(--red); }
.seg-other.active { background: #eef0f3; color: #3c4250; border-color: #6b7488; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.chip {
    padding: 7px 14px; border-radius: 99px; background: #f0f1f4; cursor: pointer;
    font-size: 13.5px; user-select: none; transition: all .12s; border: 1px solid transparent;
}
.chip:hover { background: var(--gold-light); }
.chip.active { background: var(--gold); color: #fff; font-weight: 700; }
.pos-two { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.pos-inp { font-size: 16px; padding: 12px 14px; }
.pos-num { font-size: 22px; font-weight: 700; text-align: right; font-variant-numeric: tabular-nums; }
.pos-amount { color: var(--gold-dark); }
.pos-unit { text-align: right; font-size: 13px; color: var(--muted); margin: -6px 0 10px; }
.pos-flow { display: flex; gap: 10px; margin-bottom: 12px; }
.flow-opt {
    flex: 1; text-align: center; padding: 9px; border-radius: 9px; background: #f0f1f4;
    cursor: pointer; font-weight: 700; color: #5a6170; border: 2px solid transparent; user-select: none;
}
.flow-opt.active { background: #fdf6e0; color: var(--gold-dark); border-color: var(--gold); }
.keypad { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 4px 0 14px; }
.key {
    text-align: center; padding: 13px 0; border-radius: 10px; background: #f4f5f8;
    font-size: 19px; font-weight: 700; cursor: pointer; user-select: none; color: var(--text);
}
.key:hover { background: var(--gold-light); }
.key:active { transform: scale(.96); }
.key-fn { background: #e7e9ee; color: #5a6170; }
.pos-submit {
    width: 100%; padding: 17px; font-size: 18px; font-weight: 700; border-radius: 12px;
    background: #d8dadf; color: #6a7080;
}
.pos-submit-buy { background: linear-gradient(135deg, #45b377, #1f7a47); color: #fff; box-shadow: 0 3px 10px rgba(46,158,91,.35); }
.pos-submit-sell { background: linear-gradient(135deg, #e06363, #b53030); color: #fff; box-shadow: 0 3px 10px rgba(208,69,69,.35); }
.pos-submit-other { background: linear-gradient(135deg, #d9b545, #b08c1d); color: #fff; box-shadow: 0 3px 10px rgba(176,140,29,.35); }

/* ============ 儀表板 ============ */
.dash-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.dash-grid .full { grid-column: 1 / -1; }
.chart { width: 100%; height: auto; display: block; }
.ch-tick { font-size: 11px; fill: #8a909c; font-family: inherit; }
.ch-legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 12.5px; color: #5a6170; margin-bottom: 6px; }
.ch-legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; margin-right: 6px; }
.donut-wrap { display: flex; align-items: center; gap: 18px; flex-wrap: wrap; }
.donut-svg { width: 180px; flex: 0 0 180px; }
.donut-center-lbl { font-size: 12px; fill: #8a909c; font-family: inherit; }
.donut-center-val { font-size: 19px; font-weight: 700; fill: #2b2f36; font-family: inherit; }
.donut-legend { flex: 1; min-width: 180px; }
.dl-row { display: flex; align-items: center; gap: 8px; padding: 5px 0; font-size: 13px; border-bottom: 1px dashed #eef0f3; }
.dl-row i { width: 10px; height: 10px; border-radius: 3px; flex: 0 0 10px; }
.dl-name { flex: 1; }
.dl-val { font-weight: 700; font-variant-numeric: tabular-nums; }
.dl-pct { color: var(--muted); width: 52px; text-align: right; font-size: 12px; }
.exp-bar { background: #f0f1f4; border-radius: 99px; height: 10px; overflow: hidden; }
.exp-bar i { display: block; height: 100%; background: linear-gradient(90deg, #d9b545, #b08c1d); border-radius: 99px; }
.fund-row {
    display: flex; justify-content: space-between; align-items: center;
    padding: 9px 2px; border-bottom: 1px dashed #eef0f3; font-size: 13.5px;
}
.fund-row b { font-variant-numeric: tabular-nums; }

/* ============ 響應式:平板與手機 ============ */
.tbl-wrap { -webkit-overflow-scrolling: touch; }

@media (max-width: 860px) {
    .topbar { display: flex; }
    .layout { padding-top: 52px; }

    /* 側欄改為抽屜 */
    .sidebar {
        position: fixed; top: 0; left: 0; bottom: 0; height: 100vh; z-index: 800;
        transform: translateX(-105%); transition: transform .22s ease;
        box-shadow: 4px 0 24px rgba(10,14,24,.35);
    }
    .sidebar.open { transform: translateX(0); }

    .main { padding: 14px 12px; }
    .page-head { gap: 10px; margin-bottom: 14px; }
    .page-title { font-size: 18px; width: 100%; }
    .page-title small { display: block; margin: 4px 0 0; }

    /* 統計卡片 2 欄 */
    .stat-row { gap: 10px; margin-bottom: 14px; }
    .stat { min-width: 0; flex: 1 1 calc(50% - 8px); padding: 10px 14px; }
    .stat .val { font-size: 16px; }

    /* 表格縮小、保持橫向捲動 */
    .tbl th, .tbl td { padding: 8px 9px; font-size: 12.5px; }

    /* 表單改單欄、彈窗貼齊螢幕 */
    .form-grid { grid-template-columns: 1fr; }
    .overlay { padding: 3vh 10px; }
    .dialog { width: 100%; }
    .dlg-body { padding: 14px 14px 4px; }
    .dlg-head { padding: 13px 14px; }
    .dlg-foot { padding: 10px 14px 14px; }
    .dlg-foot .btn { flex: 1; padding: 11px 16px; }

    /* 操作按鈕加大觸控面積 */
    .btn-icon { padding: 7px 9px; font-size: 15px; }
    .btn { padding: 9px 14px; }
    .btn-sm { padding: 6px 10px; }

    /* 搜尋框滿版 */
    .search-inp { width: 100%; }
    #tx-month { width: 100% !important; }
    .page-head .btn-gold { width: 100%; }
    .card-head { padding: 12px 14px; }
    .card-head .btn-gold { width: auto; }

    /* Toast 改置中於頂部 */
    #toast-root { left: 12px; right: 12px; top: 60px; }
    .toast { max-width: none; }

    /* 損益彙總行內編輯 */
    .inline-edit .inp { width: 100px; }

    /* POS 與儀表板:單欄 */
    .pos-grid { grid-template-columns: 1fr; }
    .dash-grid { grid-template-columns: 1fr; gap: 14px; }
    .pos-seg .seg { font-size: 15px; padding: 11px 4px; }
    .key { padding: 15px 0; font-size: 20px; }
    .donut-wrap { justify-content: center; }
}
