/* ============ 全局样式 ============ */
:root {
    --primary: #4f46e5;
    --primary-light: #6366f1;
    --primary-dark: #4338ca;
    --success: #10b981;
    --warning: #f59e0b;
    --danger: #ef4444;
    --info: #3b82f6;
    --bg: #f1f5f9;
    --card-bg: #ffffff;
    --text: #1e293b;
    --text-secondary: #64748b;
    --border: #e2e8f0;
    --sidebar-bg: #1e293b;
    --sidebar-text: #cbd5e1;
    --sidebar-active: #4f46e5;
    --radius: 8px;
    --shadow: 0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
    --shadow-lg: 0 10px 15px -3px rgba(0,0,0,.1), 0 4px 6px -2px rgba(0,0,0,.05);
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "PingFang SC", "Microsoft YaHei", sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
}

.app-container { display: flex; min-height: 100vh; }

/* ============ 侧边栏收起/展开开关 ============ */
#sidebar-toggle {
    position: fixed;
    top: 14px;
    z-index: 300;
    width: 38px;
    height: 38px;
    border: none;
    border-radius: 8px;
    background: rgba(255,255,255,.10);
    color: #fff;
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: left .25s ease, background .2s, color .2s;
}
#sidebar-toggle:hover { background: rgba(255,255,255,.22); }
/* 展开时按钮贴在侧栏右缘（深色背景上用白色图标） */
body:not(.sidebar-collapsed) #sidebar-toggle { left: 198px; }
/* 收起时按钮移到最左侧（浅色主区上用主色背景） */
body.sidebar-collapsed #sidebar-toggle { left: 14px; background: var(--primary); color: #fff; }

.sidebar { transition: transform .25s ease; }
body.sidebar-collapsed .sidebar { transform: translateX(-100%); }

.main-content { transition: margin-left .25s ease; }
body.sidebar-collapsed .main-content { margin-left: 0; }

/* 移动端抽屉遮罩（默认隐藏，抽屉打开时显示） */
.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(15,23,42,.45);
    z-index: 350;
}

/* 仪表盘：趋势图 + 侧栏卡片 */
.dash-grid { display: grid; grid-template-columns: 2fr 1fr; gap: 24px; }

/* ============ 侧边栏 ============ */
.sidebar {
    width: 240px;
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 100;
    overflow-y: auto;
}

.sidebar-header {
    padding: 20px;
    border-bottom: 1px solid rgba(255,255,255,.1);
}

.sidebar-header h2 {
    font-size: 18px;
    color: #fff;
    font-weight: 600;
}

.sidebar-nav {
    flex: 1;
    padding: 10px 0;
}

.nav-item {
    display: flex;
    align-items: center;
    padding: 10px 20px;
    color: var(--sidebar-text);
    text-decoration: none;
    transition: all .2s;
    font-size: 14px;
    border-left: 3px solid transparent;
}

.nav-item:hover {
    background: rgba(255,255,255,.05);
    color: #fff;
}

.nav-item.active {
    background: rgba(79,70,229,.3);
    color: #fff;
    border-left-color: var(--primary-light);
}

.nav-icon { margin-right: 10px; font-size: 16px; }

/* 侧边栏分组 */
.nav-group { margin-top: 2px; border-top: 1px solid rgba(255, 255, 255, .07); }
.nav-group:first-of-type { border-top: none; }
.nav-group-title {
    padding: 14px 20px 8px;
    font-size: 13px;
    font-weight: 700;
    letter-spacing: .14em;
    color: rgba(255, 255, 255, .85);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: space-between;
    user-select: none;
    transition: color .2s;
}
.nav-group-title::before {
    content: '';
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 2px;
    margin-right: 8px;
    background: currentColor;
    box-shadow: 0 0 6px currentColor;
}
.nav-group-title::after {
    content: '▸';
    font-size: 11px;
    opacity: .7;
}
.nav-group.expanded .nav-group-title::after { content: '▾'; }
/* 分组配色：销售=绿 / 采购=蓝 / 库房=橙 / 基础信息=紫 */
.group-sales .nav-group-title { color: #4ade80; }
.group-purchase .nav-group-title { color: #38bdf8; }
.group-finance .nav-group-title { color: #f472b6; }
.group-warehouse .nav-group-title { color: #fbbf24; }
.group-basic .nav-group-title { color: #c084fc; }
/* 折叠时弱化未展开分组的标题，提升层次感 */
.nav-group:not(.expanded) .nav-group-title { opacity: .82; }

.nav-group .nav-item { padding-left: 20px; display: none; }
.nav-group.expanded .nav-item { display: flex; }

/* ============ 主内容区 ============ */
.main-content {
    flex: 1;
    margin-left: 240px;
    padding: 24px;
    min-height: 100vh;
}

.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
}

.page-header h1 {
    font-size: 24px;
    font-weight: 700;
    color: var(--text);
}

/* ============ 统计卡片 ============ */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}

.stat-card {
    background: var(--card-bg);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: var(--shadow);
    transition: transform .2s;
}

.stat-card:hover { transform: translateY(-2px); }

.stat-card .stat-label {
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.stat-card .stat-value {
    font-size: 28px;
    font-weight: 700;
    color: var(--text);
}

.stat-card .stat-sub {
    font-size: 12px;
    color: var(--text-secondary);
    margin-top: 4px;
}

.stat-card.primary .stat-value { color: var(--primary); }
.stat-card.success .stat-value { color: var(--success); }
.stat-card.warning .stat-value { color: var(--warning); }
.stat-card.danger .stat-value { color: var(--danger); }

/* ============ 卡片 ============ */
.card {
    background: var(--card-bg);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    margin-bottom: 24px;
}

.card-header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.card-header h3 {
    font-size: 16px;
    font-weight: 600;
}

.card-body { padding: 20px; }

/* ============ 表格 ============ */
.table-container { overflow-x: auto; }

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
}

thead th {
    background: #f8fafc;
    padding: 12px 16px;
    text-align: left;
    font-weight: 600;
    color: var(--text-secondary);
    border-bottom: 2px solid var(--border);
    white-space: nowrap;
}

tbody td {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
}

tbody tr:hover { background: #f8fafc; }

/* ============ 按钮 ============ */
.btn {
    display: inline-flex;
    align-items: center;
    padding: 8px 16px;
    border: none;
    border-radius: var(--radius);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all .2s;
    gap: 6px;
    text-decoration: none;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
}
.btn-primary:hover { background: var(--primary-dark); }

.btn-success {
    background: var(--success);
    color: #fff;
}
.btn-success:hover { background: #059669; }

.btn-danger {
    background: var(--danger);
    color: #fff;
}
.btn-danger:hover { background: #dc2626; }

.btn-warning {
    background: var(--warning);
    color: #fff;
}

.btn-sm {
    padding: 4px 10px;
    font-size: 12px;
}

.btn-outline {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text);
}
.btn-outline:hover { background: #f1f5f9; }

.btn-group { display: flex; gap: 8px; }

/* ============ 表单 ============ */
.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.form-control {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 14px;
    transition: border-color .2s;
    font-family: inherit;
}

.form-control:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(79,70,229,.1);
}

select.form-control { cursor: pointer; }
textarea.form-control { resize: vertical; min-height: 80px; }

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

/* ============ 新建销售单：日期栏宽度贴合日期（点击即弹原生日期选择框） ============ */
.modal-body input[type="date"].form-control {
    width: 180px;
    max-width: 180px;
    cursor: pointer;
}
.so-date-row { grid-template-columns: repeat(auto-fit, minmax(180px, 180px)); }
.so-date-row > .form-group { max-width: 180px; }

/* 业务员选框宽度合理化：名称通常不长，固定适中宽度避免被拉伸过宽 */
.form-row .fg-salesperson { max-width: 240px; }
.form-row .fg-salesperson .search-select { width: 100%; }

/* ============ 新建销售单：销售明细所有字段排成一行，宽度合理化 ============ */
.so-item-row {
    display: flex;
    flex-wrap: nowrap;
    gap: 10px;
    align-items: flex-end;
    margin-bottom: 10px;
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: #fcfdff;
}
.so-item-row > .form-group {
    margin-bottom: 0;
    display: flex;
    flex-direction: column;
}
.so-item-row .fg-product { flex: 1 1 240px; min-width: 180px; }
.so-item-row .fg-supplier { flex: 1 1 200px; min-width: 160px; }
.so-item-row .fg-qty     { flex: 0 0 100px; }
.so-item-row .fg-unit    { flex: 0 0 80px; }
.so-item-row .fg-price   { flex: 0 0 130px; }
.so-item-row .fg-total   { flex: 0 0 130px; }
.so-item-row .fg-action  { flex: 0 0 auto; }
.so-item-row .form-group label {
    white-space: nowrap;
    margin-bottom: 6px;
}
.so-item-row .item-total {
    display: flex;
    align-items: center;
    min-height: 38px;
    font-weight: 600;
    color: var(--text);
    white-space: nowrap;
}
@media (max-width: 560px) {
    .so-item-row { flex-wrap: wrap; }
    .so-item-row .fg-product { flex: 1 1 100%; }
}

/* ============ 状态标签 ============ */
.badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 500;
}

.badge-success { background: #d1fae5; color: #065f46; }
.badge-warning { background: #fef3c7; color: #92400e; }
.badge-danger { background: #fee2e2; color: #991b1b; }
.badge-info { background: #dbeafe; color: #1e40af; }
.badge-default { background: #f1f5f9; color: #475569; }

/* ============ 审批链 / 日志 ============ */
a.link, .link { color: #2563eb; cursor: pointer; text-decoration: none; }
a.link:hover, .link:hover { text-decoration: underline; }
.approval-chain { margin: 0 0 4px 18px; padding: 0; }
.approval-chain li { margin: 2px 0; }
.approval-chain li.current { font-weight: 600; }
.approval-chain li.done { color: #27ae60; }
.approval-chain li.todo { color: #94a3b8; }
.approval-log { margin: 0 0 4px 18px; padding: 0; }
.approval-log li { margin: 2px 0; }
.approval-log li.approve { color: #15803d; }
.approval-log li.reject { color: #b91c1c; }
.approval-log li.submit { color: #475569; }

/* ============ 审批进度条 ============ */
.approval-progress {
    position: relative;
    min-width: 120px;
    height: 18px;
    background: #eef2f7;
    border-radius: 9px;
    overflow: hidden;
}
.approval-progress-fill {
    position: absolute;
    left: 0; top: 0; bottom: 0;
    border-radius: 9px;
    transition: width .3s ease;
}
.approval-progress.done .approval-progress-fill { background: #27ae60; }
.approval-progress.pending .approval-progress-fill { background: linear-gradient(90deg,#f59e0b,#e67e22); }
.approval-progress.rejected .approval-progress-fill { background: #e74c3c; }
.approval-progress.indeterminate .approval-progress-fill { background: #94a3b8; width: 100% !important; opacity: .35; }
.approval-progress-label {
    position: absolute;
    left: 0; right: 0; top: 0;
    line-height: 18px;
    text-align: center;
    font-size: 11px;
    color: #1f2937;
    font-weight: 600;
}

/* ============ 卡片头（标题 + 操作按钮） ============ */
.card-header-flex {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border, #e5e7eb);
}
.card-header-flex h2 {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
}

/* ============ 权限矩阵 ============ */
.perm-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 8px 12px;
    max-height: 260px;
    overflow-y: auto;
    padding: 10px;
    border: 1px solid var(--border, #e5e7eb);
    border-radius: 8px;
    background: #f8fafc;
}
.perm-item {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    cursor: pointer;
    user-select: none;
}
.perm-item input[type="checkbox"] {
    width: 15px;
    height: 15px;
    cursor: pointer;
}
.perm-group {
    border: 1px solid var(--border, #e5e7eb);
    border-radius: 8px;
    margin: 0 0 12px 0;
    padding: 4px 14px 12px 14px;
    background: #fff;
}
.perm-group legend {
    font-size: 13px;
    font-weight: 600;
    color: #374151;
    padding: 0 8px;
}
.perm-group .perm-grid {
    max-height: none;
    overflow: visible;
    border: none;
    background: transparent;
    padding: 8px 0 0 0;
}

/* ============ 模态框 ============ */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal {
    background: #fff;
    border-radius: 12px;
    width: 94%;
    max-width: 900px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: var(--shadow-lg);
}

/* 大号弹窗：用于新建单据 / SN 录入等较宽表单 */
.modal-lg {
    max-width: 1120px;
    max-height: 92vh;
}

.modal-header {
    padding: 16px 24px;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: sticky;
    top: 0;
    background: #fff;
    z-index: 1;
}

.modal-close {
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: var(--text-secondary);
}

.modal-body { padding: 24px; }
.modal-footer {
    padding: 16px 24px;
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

/* ============ Toast ============ */
.toast-container {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 2000;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.toast {
    padding: 12px 20px;
    border-radius: var(--radius);
    color: #fff;
    font-size: 14px;
    box-shadow: var(--shadow-lg);
    animation: slideIn .3s ease;
    min-width: 250px;
}

.toast-success { background: var(--success); }
.toast-error { background: var(--danger); }
.toast-warning { background: var(--warning); }

@keyframes slideIn {
    from { transform: translateX(100%); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}

/* ============ 搜索框 ============ */
.search-box {
    display: flex;
    gap: 10px;
    margin-bottom: 16px;
}

.search-box .form-control { max-width: 300px; }

/* ============ 分页 ============ */
.pagination {
    display: flex;
    justify-content: center;
    gap: 4px;
    margin-top: 20px;
}

.pagination button {
    padding: 6px 12px;
    border: 1px solid var(--border);
    background: #fff;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 14px;
}

.pagination button.active {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
}

.pagination button:hover:not(.active) { background: #f1f5f9; }

/* ============ 空状态 ============ */
.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-secondary);
}

.empty-state .empty-icon { font-size: 48px; margin-bottom: 16px; }

/* ============ 图表容器 ============ */
.chart-container {
    width: 100%;
    height: 300px;
    position: relative;
}

.trend-chart { height: 350px; }

/* ============ 响应式 ============ */
/* ============ 响应式：≤992px 平板/手机，侧栏改为可滑出抽屉（覆盖内容） ============ */
@media (max-width: 992px) {
    #sidebar-toggle { left: 14px; background: var(--primary); color: #fff; }
    .main-content { margin-left: 0 !important; padding: 56px 16px 16px; }
    .sidebar {
        width: 260px;
        transform: translateX(-100%);
        z-index: 400;
        box-shadow: 2px 0 16px rgba(0,0,0,.35);
    }
    body.nav-mobile-open .sidebar { transform: translateX(0); }
    body.nav-mobile-open .sidebar-backdrop { display: block; }
    .stats-grid { grid-template-columns: 1fr 1fr; }
    .page-header { flex-direction: column; align-items: flex-start; gap: 12px; }
    .page-header .btn-group { width: 100%; flex-wrap: wrap; }
    .search-box { flex-direction: column; align-items: stretch; }
    .search-box .form-control { max-width: none; width: 100%; }
    .modal { width: 96%; max-height: 92vh; }
    .modal-lg { max-width: 96%; }
    .dash-grid { grid-template-columns: 1fr; }
}

/* ============ 响应式：≤560px 手机微调 ============ */
@media (max-width: 560px) {
    .stats-grid { grid-template-columns: 1fr; }
    .main-content { padding: 52px 12px 12px; }
    .card-header { flex-direction: column; align-items: flex-start; gap: 8px; }
    .card-header .form-control { max-width: 100%; width: 100%; }
    .form-row { grid-template-columns: 1fr; }
    .modal-body { padding: 16px; }
    .modal-header, .modal-footer { padding: 14px 16px; }
    .page-header h1 { font-size: 20px; }
    .table-container { font-size: 13px; }
    .trend-chart { height: 260px; }
}

/* ============ 登录页 ============ */
.login-page {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

.login-container {
    width: 100%;
    max-width: 420px;
    padding: 20px;
}

.login-card {
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 20px 60px rgba(0,0,0,.15);
    padding: 40px;
}

.login-header {
    text-align: center;
    margin-bottom: 32px;
}

.login-header h1 {
    font-size: 24px;
    font-weight: 700;
    color: var(--text);
    margin-bottom: 8px;
}

.login-header p {
    color: var(--text-secondary);
    font-size: 14px;
}

.login-form .form-group {
    margin-bottom: 20px;
}

.login-form .form-control {
    padding: 12px 16px;
    font-size: 15px;
    border-radius: 10px;
}

.btn-block {
    width: 100%;
    justify-content: center;
    padding: 12px;
    font-size: 16px;
    border-radius: 10px;
}

.required { color: var(--danger); }

.alert {
    padding: 12px 16px;
    border-radius: var(--radius);
    margin-bottom: 16px;
    font-size: 14px;
}

.alert-danger {
    background: #fee2e2;
    color: #991b1b;
    border: 1px solid #fecaca;
}

/* ============ 侧边栏用户信息 ============ */
.sidebar-user {
    padding: 12px 20px;
    border-bottom: 1px solid rgba(255,255,255,.1);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.sidebar-user-info {
    display: flex;
    align-items: center;
    gap: 8px;
}

.sidebar-user-avatar {
    font-size: 20px;
}

.sidebar-user-name {
    color: #e2e8f0;
    font-size: 13px;
    font-weight: 500;
    max-width: 140px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-logout {
    color: #94a3b8;
    text-decoration: none;
    font-size: 16px;
    transition: color .2s;
}

.sidebar-logout:hover {
    color: #f87171;
}

/* ============ 用户管理表格 ============ */
.text-center { text-align: center; }

.table .btn-sm {
    margin-right: 4px;
}

/* ============ 带搜索的下拉选择 ============ */
.search-select {
    position: relative;
}

.search-select-input {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 14px;
    font-family: inherit;
    transition: border-color .2s;
}

.search-select-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(79,70,229,.1);
}

.search-select-list {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    max-height: 260px;
    overflow-y: auto;
    z-index: 1100;
}

.search-select-item {
    padding: 9px 12px;
    font-size: 14px;
    cursor: pointer;
    border-bottom: 1px solid #f1f5f9;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.search-select-item:last-child {
    border-bottom: none;
}

.search-select-item:hover {
    background: #f1f5f9;
}

.search-select-empty {
    padding: 12px;
    text-align: center;
    color: var(--text-secondary);
    font-size: 13px;
}

/* ============ 分类管理弹窗 ============ */
.category-list {
    max-height: 360px;
    overflow-y: auto;
    margin-bottom: 16px;
}

.category-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 8px;
}

.category-item-info strong { display: block; font-size: 14px; }
.category-item-info small { color: var(--text-secondary); font-size: 12px; }
.category-item-actions { display: flex; gap: 6px; }

/* 序列号录入/选择 */
.sn-list {
    max-height: 160px;
    overflow-y: auto;
    border: 1px solid var(--border, #e2e8f0);
    border-radius: 6px;
    padding: 8px;
    display: flex;
    flex-wrap: wrap;
    gap: 6px 14px;
    background: #fafbfc;
}
.sn-check {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font-size: 13px;
    font-weight: 400;
    margin: 0;
    cursor: pointer;
    white-space: nowrap;
}
.sn-check input { margin: 0; }
