:root {
    --bg: #f4f6fb;
    --card: #ffffff;
    --text: #1f2430;
    --muted: #78808f;
    --border: #e3e7f0;
    --primary: #3557d6;
    --primary-dark: #2a45ac;
    --green: #1c9d67;
    --amber: #c98a00;
    --red: #d64545;
    --radius: 10px;
    --shadow: 0 1px 2px rgba(23, 32, 63, .06), 0 6px 18px rgba(23, 32, 63, .05);
}

* { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: "PingFang SC", "Microsoft YaHei", "Hiragino Sans GB", "Source Han Sans SC", system-ui, -apple-system, "Segoe UI", sans-serif;
    font-size: 14px;
    line-height: 1.6;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { font-size: 22px; margin: 0; }
h2 { font-size: 16px; margin: 0; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.num { text-align: right; }
.link { color: var(--primary); }

/* ----------------------------------------------------------- 顶栏 */

.topbar {
    background: #fff;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 20;
}

.topbar-inner {
    max-width: 1400px;
    margin: 0 auto;
    padding: 10px 24px;
    display: flex;
    align-items: center;
    gap: 24px;
}

.brand { display: flex; align-items: center; gap: 10px; color: var(--text); font-weight: 600; }
.brand:hover { text-decoration: none; }

.brand-mark {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    border-radius: 8px;
    background: linear-gradient(135deg, #4a6bf0, #3557d6);
    color: #fff;
    font-weight: 700;
}

.brand-mark-lg { width: 46px; height: 46px; font-size: 22px; border-radius: 12px; }
.brand-text { font-size: 15px; }

.nav { display: flex; gap: 6px; flex: 1; }

.nav-link {
    padding: 6px 12px;
    border-radius: 8px;
    color: var(--muted);
    font-weight: 500;
}

.nav-link:hover { background: #f1f3fa; text-decoration: none; }
.nav-link.active { background: #eef1fd; color: var(--primary); }

.topbar-right { display: flex; align-items: center; gap: 12px; }

.user-chip { color: var(--muted); }
.user-chip em { font-style: normal; margin-left: 4px; font-size: 12px; color: #9aa1ae; }

/* ----------------------------------------------------------- 布局 */

.container { max-width: 1400px; margin: 0 auto; padding: 24px; }

.page-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 16px;
    margin-bottom: 18px;
    flex-wrap: wrap;
}

.page-head p { margin: 6px 0 0; display: flex; gap: 14px; flex-wrap: wrap; }
.page-head-actions { display: flex; gap: 10px; }

.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 18px 20px;
    margin-bottom: 18px;
}

.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.card-title { margin-bottom: 12px; }
.card-head .card-title { margin-bottom: 0; }

.empty { color: var(--muted); padding: 12px 0; }

/* ----------------------------------------------------------- 表单 */

.form { display: block; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 14px; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 13px; color: var(--muted); }
.field-wide { grid-column: 1 / -1; }
.field-action { justify-content: flex-end; }

input[type="text"],
input[type="password"],
input[type="number"],
input[type="date"],
input[type="file"],
textarea,
select {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 14px;
    font-family: inherit;
    background: #fff;
    color: var(--text);
}

input:focus, select:focus { outline: 2px solid #dbe2fe; border-color: var(--primary); }

.form-actions { display: flex; gap: 10px; margin-top: 14px; justify-content: flex-end; }

/* ----------------------------------------------------------- 按钮 */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 14px;
    border-radius: 8px;
    border: 1px solid transparent;
    background: #eef1f8;
    color: var(--text);
    font-size: 14px;
    font-family: inherit;
    cursor: pointer;
    white-space: nowrap;
}

.btn:hover { filter: brightness(.97); text-decoration: none; }
.btn:disabled { opacity: .6; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-ghost { background: #fff; border-color: var(--border); }
.btn-danger-ghost { background: #fff; border-color: #f0c9c9; color: var(--red); }
.btn-sm { padding: 5px 10px; font-size: 13px; }
.btn-block { width: 100%; }

/* ----------------------------------------------------------- 标签 */

.tag {
    display: inline-block;
    padding: 2px 9px;
    border-radius: 999px;
    background: #eef1f8;
    color: #5b6373;
    font-size: 12px;
    line-height: 1.7;
}

.tag-blue { background: #e8edfe; color: #2f52c9; }
.tag-green { background: #e4f6ee; color: #167951; }
.tag-amber { background: #fdf3df; color: #a4700a; }
.tag-red { background: #fdeaea; color: #c33b3b; }

/* ----------------------------------------------------------- 提示 */

.alert {
    padding: 10px 14px;
    border-radius: 8px;
    margin-bottom: 16px;
    border: 1px solid transparent;
}

.alert-success { background: #e7f7ef; border-color: #bfe8d4; color: #146b49; }
.alert-error { background: #fdecec; border-color: #f4c9c9; color: #b03030; }

/* ----------------------------------------------------------- 统计 */

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; margin-bottom: 18px; }

.stat-card {
    background: #fff;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px 18px;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.stat-label { color: var(--muted); font-size: 13px; }
.stat-value { font-size: 24px; }

/* ----------------------------------------------------------- 表格 */

.table-scroll { overflow-x: auto; }

.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
.table th { background: #f8f9fd; font-weight: 600; color: #55607a; font-size: 13px; white-space: nowrap; }
.table tbody tr:hover { background: #fafbff; }

.table-items { min-width: 1500px; }
.table-ocr { min-width: 1200px; }

.actions { display: flex; gap: 6px; align-items: center; white-space: nowrap; }
.inline-form { display: inline; }

.cell-input {
    width: 100%;
    min-width: 72px;
    padding: 5px 7px;
    border: 1px solid transparent;
    border-radius: 6px;
    background: transparent;
    font-size: 13px;
    font-family: inherit;
}

.cell-input:hover { border-color: var(--border); background: #fff; }
.cell-input:focus { border-color: var(--primary); background: #fff; outline: none; }
.cell-num { text-align: right; }
.cell-wide { min-width: 320px; }
.cell-invoice { white-space: nowrap; }
.item-row-new td { background: #f7fbf9; }

.row-index { color: var(--muted); font-size: 12px; width: 32px; }

/* ----------------------------------------------------------- 发票区 */

.ocr-toolbar { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; margin: 10px 0; }
.upload-form { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.upload-form input[type="file"] { max-width: 340px; }
.ocr-badges { display: flex; gap: 8px; flex-wrap: wrap; }

.file-list { list-style: none; margin: 0; padding: 0; }
.file-list li { display: flex; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px dashed var(--border); }
.file-list li:last-child { border-bottom: none; }

/* ----------------------------------------------------------- 拖拽上传 */

.drop-zone {
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    padding: 24px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s;
    background: #fafbff;
}

.drop-zone:hover,
.drop-zone-active {
    border-color: var(--primary);
    background: #eef1fd;
}

.drop-zone-input {
    display: none;
}

.drop-zone-text {
    margin: 0;
    font-size: 15px;
    color: var(--text);
}

.drop-zone-hint {
    margin: 6px 0 0;
    font-size: 12px;
    color: var(--muted);
}

/* ----------------------------------------------------------- 进度条 */

.ocr-progress {
    display: flex;
    align-items: center;
    gap: 12px;
    margin: 12px 0;
    padding: 10px 14px;
    background: #f8f9fd;
    border-radius: 8px;
}

.progress-bar {
    flex: 1;
    height: 8px;
    background: #e3e7f0;
    border-radius: 4px;
    overflow: hidden;
}

.progress-bar-fill {
    height: 100%;
    background: var(--primary);
    border-radius: 4px;
    transition: width 0.3s ease;
    width: 0%;
}

.progress-text {
    font-size: 13px;
    color: var(--muted);
    white-space: nowrap;
}

/* ----------------------------------------------------------- 筛选 */

.filter-form {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
}

.filter-input {
    min-width: 180px;
    padding: 5px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 13px;
}

.filter-select {
    padding: 5px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 13px;
    background: #fff;
}

/* ----------------------------------------------------------- 登录 */

.body-blank { background: linear-gradient(135deg, #eef1fb, #e6ecfb 55%, #e3e9fa); }

.blank-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; }

.login-card {
    width: 100%;
    max-width: 380px;
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 18px 48px rgba(30, 45, 100, .16);
    padding: 32px 30px;
}

.login-head { text-align: center; margin-bottom: 20px; }
.login-head h1 { font-size: 19px; margin: 12px 0 4px; }
.login-head p { margin: 0; color: var(--muted); font-size: 13px; }

.login-card .form { display: flex; flex-direction: column; gap: 14px; }

.login-hint { margin: 18px 0 0; text-align: center; color: var(--muted); font-size: 12px; }
.login-hint code { background: #f1f3fa; padding: 1px 6px; border-radius: 5px; }
.login-hint a { text-decoration: underline; }

.login-head .tag { margin-top: 10px; }
.login-card-admin { border-top: 4px solid var(--primary); }

.card-danger { border-color: #f0c9c9; }
.card-danger .card-title { color: var(--red); }

/* =========================================================== 触控反馈 */

@media (hover: none) {
    a:hover { text-decoration: none; }
    .btn:hover { filter: none; }
    .btn:active { transform: scale(.97); }
    .nav-link:hover { background: transparent; text-decoration: none; }
    .nav-link:active { background: #e9edfa; }
    .table tbody tr:hover { background: transparent; }
    .cell-input:hover { border-color: transparent; background: transparent; }
    .drop-zone:hover:not(.drop-zone-active) { border-color: var(--border); background: #fafbff; }
}

/* =========================================================== 横向滑动提示 */

.table-shell { position: relative; }

.table-shell::before,
.table-shell::after {
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    width: 18px;
    pointer-events: none;
    opacity: 0;
    transition: opacity .2s ease;
    z-index: 3;
}

.table-shell::before { left: 0; background: linear-gradient(to right, rgba(23, 32, 63, .16), rgba(23, 32, 63, 0)); }
.table-shell::after { right: 0; background: linear-gradient(to left, rgba(23, 32, 63, .16), rgba(23, 32, 63, 0)); }

.table-shell.is-scrollable-x:not(.at-start)::before { opacity: 1; }
.table-shell.is-scrollable-x:not(.at-end)::after { opacity: 1; }

.scroll-hint {
    display: none;
    margin: 0 0 8px;
    font-size: 12px;
    color: var(--muted);
}

.scroll-hint.is-hidden { display: none !important; }

/* =========================================================== 移动端（≤860px） */

@media (max-width: 860px) {
    body { font-size: 15px; }

    h1 { font-size: 20px; }
    h2 { font-size: 15px; }
    .small { font-size: 13px; }

    /* 触控目标 ≥44px，输入 16px 避免 iOS 聚焦自动放大 */
    .btn {
        min-height: 44px;
        padding: 10px 14px;
        touch-action: manipulation;
    }

    .btn-sm { min-height: 40px; padding: 8px 10px; font-size: 13px; }

    input[type="text"],
    input[type="password"],
    input[type="number"],
    input[type="date"],
    input[type="file"],
    textarea,
    select {
        font-size: 16px;
        min-height: 44px;
        padding: 10px 12px;
    }

    input[type="checkbox"] { width: 18px; height: 18px; touch-action: manipulation; }

    .field > span { font-size: 14px; }

    /* 顶栏 */
    .topbar { padding-top: env(safe-area-inset-top); }
    .topbar-inner { padding: 8px 12px; gap: 8px; }
    .brand { gap: 8px; }
    .brand-text { font-size: 14px; }
    .brand-mark { width: 28px; height: 28px; }
    .nav { gap: 2px; }
    .nav-link {
        display: inline-flex;
        align-items: center;
        min-height: 38px;
        padding: 8px 10px;
        touch-action: manipulation;
    }
    .topbar-right { gap: 8px; }

    /* 布局 */
    .container { padding: 16px 12px calc(22px + env(safe-area-inset-bottom)); }
    .card { padding: 14px 12px; margin-bottom: 14px; }

    .page-head { flex-direction: column; gap: 12px; margin-bottom: 14px; }
    .page-head p { gap: 8px 12px; }
    .page-head-actions {
        display: grid;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 8px;
        width: 100%;
    }
    .page-head-actions .btn {
        width: 100%;
        padding: 10px 4px;
        font-size: 13px;
        white-space: nowrap;
    }
    .page-head-actions .btn-primary {
        grid-column: 1 / -1;
        order: -1;
        padding: 12px;
        font-size: 14px;
    }

    /* 表单 */
    .form-grid { gap: 12px; }
    .field-action { justify-content: flex-start; }
    .field-action .btn { width: 100%; min-height: 44px; }
    .form-actions { gap: 8px; }
    .form-actions .btn { min-height: 44px; }

    /* 明细表：底部常驻保存条 */
    #js-items-form .form-actions {
        position: sticky;
        bottom: 0;
        z-index: 15;
        display: grid;
        grid-template-columns: 1fr 1.3fr;
        margin: 0 -12px -14px;
        padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
        background: #fff;
        border-top: 1px solid var(--border);
        box-shadow: 0 -6px 16px rgba(23, 32, 63, .07);
    }

    /* 统计卡：一行三列 */
    .stat-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin-bottom: 14px; }
    .stat-card { padding: 10px 10px; gap: 0; }
    .stat-label { font-size: 12px; }
    .stat-value { font-size: 20px; }

    /* 卡片头与筛选 */
    .card-head { flex-direction: column; align-items: stretch; gap: 10px; }
    .filter-form { width: 100%; }
    .filter-input { flex: 1 1 100%; min-width: 0; min-height: 44px; font-size: 16px; padding: 10px 12px; }
    .filter-select { flex: 1 1 calc(50% - 4px); min-width: 0; min-height: 44px; font-size: 16px; padding: 10px 12px; }
    .filter-form .btn { flex: 1 1 calc(50% - 4px); min-height: 44px; }

    /* 表格：触摸横向滚动 + 提示 */
    .table-scroll {
        overscroll-behavior-x: contain;
        -webkit-overflow-scrolling: touch;
    }
    .scroll-hint { display: block; }
    .table th, .table td { padding: 8px; }
    .cell-input { font-size: 16px; min-height: 38px; padding: 8px; }

    /* 上传与识别操作区 */
    .ocr-toolbar { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 12px 0; }
    .upload-form { display: flex; flex-direction: column; align-items: stretch; gap: 8px; grid-column: 1 / -1; }
    .upload-form input[type="file"] { max-width: none; }
    .ocr-toolbar .inline-form { display: block; }
    .ocr-toolbar .inline-form .btn,
    .ocr-toolbar > .btn { width: 100%; min-height: 44px; white-space: normal; font-size: 13px; padding: 8px 6px; }
    .drop-zone { padding: 20px 14px; }
    .drop-zone-text { font-size: 14px; }

    .ocr-progress { margin: 12px 0; padding: 10px; gap: 8px; }
    .progress-text { font-size: 12px; }

    .file-list li { flex-direction: column; align-items: flex-start; gap: 2px; padding: 10px 0; }

    /* 登录页 */
    .blank-wrap { min-height: 100vh; min-height: 100dvh; padding: 16px; }
    .login-card { padding: 26px 20px; border-radius: 14px; }
}

/* =========================================================== 小屏（≤480px） */

@media (max-width: 480px) {
    .user-chip { display: none; }
    .page-head-actions .btn { font-size: 12px; }
    .stat-value { font-size: 18px; }
    .form-actions { flex-direction: column-reverse; }
    .form-actions .btn { width: 100%; }
    #js-items-form .form-actions { grid-template-columns: 1fr 1fr; }
    .brand-text { max-width: 44vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
}

/* 窄屏顶栏：三项导航 + 退出按钮放不下时只保留标志 */
@media (max-width: 374px) {
    .brand-text { display: none; }
    .nav-link { padding: 8px 8px; }
}

/* =========================================================== 降低动效 */

@media (prefers-reduced-motion: reduce) {
    .btn:active { transform: none; }
    .table-shell::before, .table-shell::after { transition: none; }
    .progress-bar-fill { transition: none; }
    .drop-zone { transition: none; }
}

.captcha-row { display: flex; gap: 10px; align-items: stretch; }
.captcha-row input { flex: 1; min-width: 0; }
.captcha-img {
    height: 46px;
    flex: 0 0 auto;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: #fff;
    cursor: pointer;
}
