:root {
    --bg: #f4f6fb;
    --sidebar: #1f2937;
    --sidebar-hover: #374151;
    --primary: #4f46e5;
    --primary-dark: #4338ca;
    --income: #16a34a;
    --expense: #dc2626;
    --text: #1f2937;
    --text-light: #6b7280;
    --border: #e5e7eb;
    --card: #ffffff;
    --radius: 12px;
    --shadow: 0 1px 3px rgba(0, 0, 0, .08), 0 1px 2px rgba(0, 0, 0, .04);
}

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

html, body {
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", "PingFang SC", sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    font-size: 14px;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

a { text-decoration: none; color: inherit; }
img, video { max-width: 100%; height: auto; }
.text-muted { color: var(--text-light); }
.ocr-status { display: block; margin-top: 6px; font-size: 12px; line-height: 1.5; }
.ocr-busy { color: var(--primary); }
.ocr-success { color: var(--income); }
.ocr-error { color: var(--expense); }
.ocr-muted { color: var(--text-light); }
.settings-toggle { display: flex; align-items: center; gap: 10px; font-weight: 600; cursor: pointer; }
.settings-toggle input { width: 18px; height: 18px; accent-color: var(--primary); }
.settings-meta { display: flex; align-items: center; gap: 10px; margin-bottom: 18px; }

/* ---------- 布局 ---------- */
/* 电脑端：整页高度锁视口，只滚动右侧内容，避免底部被 flex 撑出大片空白 */
.app {
    display: flex;
    height: 100vh;
    height: 100dvh;
    width: 100%;
    max-width: 100%;
    overflow: hidden;
}

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, .45);
    z-index: 90;
    opacity: 0;
    transition: opacity .2s;
}
.sidebar-overlay.show { display: block; opacity: 1; }

.sidebar {
    width: 230px;
    background: var(--sidebar);
    color: #cbd5e1;
    display: flex;
    flex-direction: column;
    padding: 20px 14px;
    height: 100%;
    flex-shrink: 0;
    z-index: 100;
    overflow-y: auto;
}
.sidebar-top {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 8px;
}
.sidebar-close {
    display: none;
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 8px;
    background: var(--sidebar-hover);
    color: #fff;
    font-size: 18px;
    cursor: pointer;
    flex-shrink: 0;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px 22px;
    color: #fff;
}
.brand-logo {
    width: 38px; height: 38px;
    background: var(--primary);
    border-radius: 10px;
    display: grid; place-items: center;
    font-size: 20px; font-weight: 700;
}
.brand-name { font-size: 16px; font-weight: 600; }

nav {
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex: 0 1 auto;
    min-height: 0;
    overflow-y: auto;
}
.nav-item {
    display: flex; align-items: center; gap: 10px;
    padding: 11px 12px;
    border-radius: 8px;
    color: #cbd5e1;
    font-weight: 500;
    transition: background .15s, color .15s;
}
.nav-item:hover { background: var(--sidebar-hover); color: #fff; }
.nav-item.active { background: var(--primary); color: #fff; }
.nav-icon { font-size: 17px; flex-shrink: 0; }
.nav-label { flex: 1; min-width: 0; }

.mobile-menu-btn {
    display: none;
    width: 40px;
    height: 40px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--card);
    color: var(--text);
    font-size: 20px;
    cursor: pointer;
    flex-shrink: 0;
}
.topbar-left {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
    flex: 1;
}
.topbar-left h1 {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.logout-form {
    margin-top: 16px;
    padding-top: 14px;
    border-top: 1px solid #374151;
    flex-shrink: 0;
}
.logout-btn {
    width: 100%;
    padding: 10px;
    background: transparent;
    border: 1px solid #4b5563;
    color: #cbd5e1;
    border-radius: 8px;
    cursor: pointer;
    font-size: 13px;
    transition: background .15s;
}
.logout-btn:hover { background: var(--sidebar-hover); color: #fff; }

.content {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    background: var(--bg);
}

.topbar {
    display: flex; align-items: center; justify-content: space-between;
    padding: 18px 32px;
    background: var(--card);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    position: sticky;
    top: 0;
    z-index: 20;
}
.topbar h1 { font-size: 20px; font-weight: 600; }
.user-chip {
    background: var(--bg);
    padding: 6px 14px;
    border-radius: 20px;
    font-size: 13px;
    color: var(--text-light);
}

/* 内容区按实际高度排布，不再 flex 撑满留下底部空白 */
.page {
    padding: 28px 32px 32px;
    flex: 0 0 auto;
    width: 100%;
}
.page > .panel:last-child { margin-bottom: 0; }
.footer { padding: 18px 32px; color: var(--text-light); font-size: 12px; text-align: center; }

/* 电脑端隐藏手机列表，避免占位 */
.mobile-list { display: none !important; }

/* ---------- Flash ---------- */
.flash {
    margin: 16px 32px 0;
    padding: 12px 18px;
    border-radius: 8px;
    font-size: 14px;
}
.flash-success { background: #dcfce7; color: #166534; }
.flash-error { background: #fee2e2; color: #991b1b; }

/* ---------- 卡片 ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 18px; margin-bottom: 24px; }
.card {
    background: var(--card);
    border-radius: var(--radius);
    padding: 20px 22px;
    box-shadow: var(--shadow);
}
.card-label { color: var(--text-light); font-size: 13px; margin-bottom: 8px; }
.card-value { font-size: 26px; font-weight: 700; }
.card-sub { font-size: 12px; color: var(--text-light); margin-top: 6px; }
.text-income { color: var(--income); }
.text-expense { color: var(--expense); }

.panel {
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    margin-bottom: 24px;
    overflow: visible;
}
.panel-head {
    display: flex; align-items: center; justify-content: space-between;
    padding: 16px 22px;
    border-bottom: 1px solid var(--border);
    gap: 12px;
    flex-wrap: wrap;
}
.panel-head h2 { font-size: 15px; font-weight: 600; flex: 1; min-width: 0; word-break: break-word; }
.panel-body { padding: 22px; }

/* ---------- 总收入 / 总支出汇总条 ---------- */
.summary-bar {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 14px;
    margin-bottom: 20px;
}
@media (max-width: 1100px) {
    .summary-bar { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
.summary-item {
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 18px 16px;
    border-left: 4px solid var(--border);
    min-width: 0;
}
.summary-item.income { border-left-color: var(--income); }
.summary-item.expense { border-left-color: var(--expense); }
.summary-item.balance { border-left-color: var(--primary); }
.summary-item.asset { border-left-color: #d97706; }
.summary-label { color: var(--text-light); font-size: 13px; margin-bottom: 6px; }
.summary-value {
    font-size: 24px;
    font-weight: 700;
    word-break: break-all;
    line-height: 1.25;
}
.summary-item.income .summary-value { color: var(--income); }
.summary-item.expense .summary-value { color: var(--expense); }
.summary-item.asset .summary-value { color: #d97706; }
.summary-sub { font-size: 12px; color: var(--text-light); margin-top: 6px; }

.badge-asset { background: #ffedd5; color: #9a3412; }
.badge-muted { background: #f3f4f6; color: #6b7280; }

.yesno-picker {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}
.yesno-option {
    position: relative;
    display: block;
    margin: 0;
    cursor: pointer;
}
.yesno-option > input {
    position: absolute !important;
    opacity: 0 !important;
    width: 1px !important;
    height: 1px !important;
    pointer-events: none;
}
.yesno-option span {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 42px;
    border: 2px solid var(--border);
    border-radius: 10px;
    background: #f9fafb;
    color: var(--text-light);
    font-weight: 600;
    transition: border-color .15s, background .15s, color .15s;
}
.yesno-option > input:checked + span {
    border-color: var(--primary);
    background: #eef2ff;
    color: var(--primary-dark);
}
.yesno-option.yesno-yes > input:checked + span {
    border-color: #d97706;
    background: #ffedd5;
    color: #9a3412;
}

/* ---------- 表格 ---------- */
.table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    width: 100%;
}
.table-scroll table { min-width: 720px; width: 100%; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 12px 22px; text-align: left; border-bottom: 1px solid var(--border); }
th { background: #f9fafb; font-size: 12px; color: var(--text-light); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
tbody tr:hover { background: #f9fafb; }
td.amount, th.amount { text-align: right; font-variant-numeric: tabular-nums; }
.empty { padding: 40px; text-align: center; color: var(--text-light); }

.badge { display: inline-block; padding: 2px 10px; border-radius: 20px; font-size: 12px; font-weight: 500; }
.badge-income { background: #dcfce7; color: #166534; }
.badge-expense { background: #fee2e2; color: #991b1b; }
.muted-tag {
    display: inline-block;
    margin-left: 6px;
    padding: 1px 8px;
    border-radius: 20px;
    background: var(--bg);
    color: var(--text-light);
    font-size: 12px;
}

.badge-pending { background: #fef3c7; color: #92400e; }
.badge-approved { background: #dcfce7; color: #166534; }
.badge-rejected { background: #e5e7eb; color: #4b5563; }

.nav-badge {
    margin-left: auto;
    background: var(--expense);
    color: #fff;
    font-size: 11px;
    font-weight: 600;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: 10px;
    display: inline-grid;
    place-items: center;
}

/* ---------- 表单 ---------- */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 14px; align-items: end; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field-full { grid-column: 1 / -1; }
.field > label { font-size: 13px; color: var(--text-light); font-weight: 500; }

/* 文本类控件：勿套在 radio/checkbox 上，否则手机端会撑成整行输入框 */
input:not([type="radio"]):not([type="checkbox"]):not([type="submit"]):not([type="button"]):not([type="image"]):not([type="reset"]),
select,
textarea {
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 14px;
    font-family: inherit;
    background: #fff;
    color: var(--text);
    width: 100%;
    max-width: 100%;
}
input[type="file"] { padding: 8px; background: #f9fafb; }
input:not([type="radio"]):not([type="checkbox"]):focus,
select:focus,
textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(79,70,229,.12);
}

/* 收入 / 支出：并排大按钮，手机可点、不易选错 */
.field-type {
    grid-column: 1 / -1;
    min-width: 0;
    width: 100%;
}
.type-picker {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    width: 100%;
}
.type-option {
    position: relative;
    display: block;
    margin: 0;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}
.type-option > input[type="radio"] {
    position: absolute !important;
    left: 0 !important;
    top: 0 !important;
    width: 1px !important;
    height: 1px !important;
    margin: 0 !important;
    padding: 0 !important;
    border: 0 !important;
    opacity: 0 !important;
    overflow: hidden !important;
    clip: rect(0, 0, 0, 0);
    clip-path: inset(50%);
    pointer-events: none;
    appearance: none;
    -webkit-appearance: none;
    box-shadow: none !important;
    background: transparent !important;
}
.type-option-inner {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    box-sizing: border-box;
    width: 100%;
    min-height: 48px;
    padding: 12px 10px;
    border: 2px solid var(--border);
    border-radius: 10px;
    background: #f9fafb;
    color: var(--text-light);
    font-weight: 700;
    font-size: 15px;
    line-height: 1.2;
    text-align: center;
    transition: border-color .15s, background .15s, color .15s, box-shadow .15s;
    user-select: none;
}
.type-option-sign {
    font-size: 20px;
    font-weight: 700;
    line-height: 1;
    font-variant-numeric: tabular-nums;
}
.type-option.type-income:hover .type-option-inner {
    border-color: #86efac;
    background: #f0fdf4;
    color: var(--income);
}
.type-option.type-expense:hover .type-option-inner {
    border-color: #fca5a5;
    background: #fef2f2;
    color: var(--expense);
}
.type-option.type-income > input:checked + .type-option-inner {
    border-color: var(--income);
    background: #dcfce7;
    color: #166534;
    box-shadow: 0 0 0 3px rgba(22, 163, 74, .18);
}
.type-option.type-expense > input:checked + .type-option-inner {
    border-color: var(--expense);
    background: #fee2e2;
    color: #991b1b;
    box-shadow: 0 0 0 3px rgba(220, 38, 38, .18);
}
.type-option > input:focus-visible + .type-option-inner {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}
textarea { resize: vertical; min-height: 40px; }
.form-hint { color: var(--text-light); margin-top: 12px; font-size: 13px; line-height: 1.5; }
.attach-current { margin-top: 6px; font-size: 13px; }
.attach-link { color: var(--primary); font-weight: 500; text-decoration: underline; }

.btn {
    display: inline-flex; align-items: center; gap: 6px;
    padding: 9px 18px;
    border: none;
    border-radius: 8px;
    background: var(--primary);
    color: #fff;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background .15s;
    white-space: nowrap;
}
.btn:hover { background: var(--primary-dark); }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.btn-outline { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn-outline:hover { background: var(--bg); }
.btn-danger { background: transparent; color: var(--expense); border: 1px solid transparent; }
.btn-danger:hover { background: #fee2e2; }
.btn-success { background: var(--income); }
.btn-success:hover { background: #15803d; }

.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.spacer { flex: 1; }

/* ---------- 登录页 ---------- */
.login-wrap {
    min-height: 100vh;
    display: grid; place-items: center;
    background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
    padding: 20px;
}
.login-card {
    background: #fff;
    width: 100%;
    max-width: 380px;
    padding: 40px 34px;
    border-radius: 16px;
    box-shadow: 0 20px 60px rgba(0,0,0,.25);
}
.login-card .logo {
    width: 54px; height: 54px;
    background: var(--primary);
    border-radius: 14px;
    display: grid; place-items: center;
    color: #fff; font-size: 28px; font-weight: 700;
    margin: 0 auto 18px;
}
.login-card h1 { text-align: center; font-size: 21px; margin-bottom: 4px; }
.login-card .sub { text-align: center; color: var(--text-light); margin-bottom: 26px; font-size: 13px; }
.login-card .field { margin-bottom: 16px; }
.login-card .btn { width: 100%; justify-content: center; padding: 11px; }
.login-hint { text-align: center; font-size: 12px; color: var(--text-light); margin-top: 18px; }
.login-error { background: #fee2e2; color: #991b1b; padding: 10px 14px; border-radius: 8px; font-size: 13px; margin-bottom: 18px; text-align: center; }

/* ---------- 图表(纯 CSS 条形) ---------- */
.chart-bars { display: flex; gap: 18px; align-items: flex-end; height: 220px; padding-top: 10px; }
.chart-col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 6px; height: 100%; justify-content: flex-end; }
.bars-pair { display: flex; gap: 4px; align-items: flex-end; height: 100%; width: 100%; justify-content: center; }
.bar { width: 22px; border-radius: 4px 4px 0 0; min-height: 2px; transition: height .3s; }
.bar-income { background: var(--income); }
.bar-expense { background: var(--expense); }
.chart-label { font-size: 12px; color: var(--text-light); }

.legend { display: flex; gap: 18px; margin-bottom: 18px; font-size: 13px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.dot { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }

.breakdown-row { display: flex; align-items: center; gap: 12px; padding: 8px 0; }
.breakdown-name { width: 90px; font-size: 13px; }
.breakdown-track { flex: 1; background: var(--bg); border-radius: 20px; height: 20px; overflow: hidden; }
.breakdown-fill { height: 100%; background: var(--primary); border-radius: 20px; }
.breakdown-val { width: 110px; text-align: right; font-size: 13px; font-variant-numeric: tabular-nums; }

.inline-form { display: inline; }

body.menu-open { overflow: hidden; position: fixed; width: 100%; }

/* ---------- 手机端 ---------- */
@media (max-width: 760px) {
    .app {
        height: auto;
        min-height: 100vh;
        min-height: 100dvh;
        overflow: visible;
        overflow-x: hidden;
    }
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        width: min(280px, 86vw);
        max-width: 86vw;
        height: 100%;
        height: 100dvh;
        transform: translateX(-105%);
        transition: transform .25s ease;
        box-shadow: none;
        padding: 16px 12px;
        padding-top: max(16px, env(safe-area-inset-top));
        padding-bottom: max(16px, env(safe-area-inset-bottom));
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    .sidebar.open {
        transform: translateX(0);
        box-shadow: 4px 0 24px rgba(0, 0, 0, .2);
    }
    .sidebar-close { display: grid; place-items: center; }
    .mobile-menu-btn { display: grid; place-items: center; }

    nav { flex: 1 1 auto; }
    .logout-form { margin-top: auto; }

    .content {
        width: 100%;
        max-width: 100%;
        min-width: 0;
        flex: 1 1 auto;
        overflow: visible;
        min-height: 0;
    }

    .topbar {
        position: sticky;
        top: 0;
        z-index: 50;
        gap: 10px;
        padding: 12px 14px;
        padding-top: max(12px, env(safe-area-inset-top));
        background: var(--card);
    }
    .topbar h1 { font-size: 16px; }
    .user-chip {
        max-width: 42vw;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-size: 12px;
        padding: 5px 10px;
        flex-shrink: 0;
    }

    .page {
        padding: 12px;
        padding-bottom: max(20px, env(safe-area-inset-bottom));
        width: 100%;
        max-width: 100%;
        overflow-x: hidden;
    }
    .flash { margin: 10px 12px 0; }

    .summary-bar {
        grid-template-columns: 1fr;
        gap: 10px;
        margin-bottom: 14px;
    }
    .summary-item { padding: 14px; }
    .summary-value { font-size: 22px; }

    .cards { grid-template-columns: 1fr; gap: 10px; }
    .card { padding: 14px 16px; }
    .card-value { font-size: 22px; word-break: break-all; }

    .panel { margin-bottom: 14px; border-radius: 10px; width: 100%; max-width: 100%; }
    .panel-head { padding: 12px 14px; }
    .panel-head h2 { font-size: 14px; line-height: 1.4; }
    .panel-head .toolbar { width: 100%; }
    .panel-head .toolbar .btn { flex: 1; justify-content: center; }
    .panel-body { padding: 14px; }

    .form-grid { grid-template-columns: 1fr; gap: 12px; }
    .field-full,
    .field-type { grid-column: 1 / -1; }
    .field .btn, .form-grid .btn { width: 100%; justify-content: center; }
    .type-picker { gap: 8px; }
    .type-option-inner {
        min-height: 52px;
        font-size: 16px;
        padding: 14px 8px;
    }
    .type-option-sign { font-size: 22px; }

    input:not([type="radio"]):not([type="checkbox"]),
    select,
    textarea,
    button { font-size: 16px; }

    .btn { min-height: 42px; }
    .toolbar { gap: 8px; width: 100%; }
    .toolbar .btn { flex: 1 1 auto; justify-content: center; min-width: 0; }

    /* 有手机卡片列表的宽表隐藏；其余表格可横向滑动 */
    .desktop-table { display: none !important; }
    .mobile-list { display: block !important; padding: 0 12px 12px; }
    .table-scroll {
        max-width: 100%;
        border-top: 1px solid var(--border);
    }
    .mobile-item {
        background: #f9fafb;
        border: 1px solid var(--border);
        border-radius: 10px;
        padding: 12px;
        margin-top: 10px;
    }
    .mobile-item-top {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 10px;
        margin-bottom: 6px;
    }
    .mobile-item-top strong {
        font-size: 18px;
        word-break: break-all;
        text-align: right;
    }
    .mobile-item-meta {
        font-size: 12px;
        color: var(--text-light);
        line-height: 1.5;
        word-break: break-word;
    }
    .mobile-item-note {
        margin-top: 6px;
        font-size: 13px;
        word-break: break-word;
    }
    .mobile-item-actions { margin-top: 10px; }

    .empty { padding: 24px 14px; font-size: 13px; }

    .chart-bars { gap: 6px; height: 160px; overflow-x: auto; }
    .bar { width: 12px; }
    .chart-label { font-size: 10px; }

    .breakdown-row { flex-wrap: wrap; gap: 6px; }
    .breakdown-name, .breakdown-val { width: 100%; text-align: left; }

    .login-wrap { padding: 16px; align-items: flex-start; padding-top: 10vh; }
    .login-card { padding: 28px 20px; width: 100%; }
    .login-card h1 { font-size: 19px; }

    .muted-tag { display: inline-block; margin: 2px 0 0 0; }
}

@media (max-width: 400px) {
    .summary-value { font-size: 20px; }
    .user-chip { max-width: 38vw; }
}
