/* ==========================================================================
   进销存 ERP 管理后台 —— 全局样式
   风格：深色侧栏 + 浅色内容区，主色靛蓝，克制专业
   ========================================================================== */

:root {
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --primary-light: #eef2ff;
  --sidebar-bg: #1f2436;
  --sidebar-bg-soft: #262c42;
  --sidebar-text: #c3cadb;
  --bg: #f4f6fb;
  --card: #ffffff;
  --border: #e5e8f0;
  --border-strong: #d4d9e6;
  --text: #1f2437;
  --muted: #6b7280;
  --danger: #dc2626;
  --danger-bg: #fef2f2;
  --success: #16a34a;
  --success-bg: #f0fdf4;
  --warn: #d97706;
  --warn-bg: #fffbeb;
  --info: #0ea5e9;
  --info-bg: #eff6ff;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 4px 16px rgba(16, 24, 40, .05);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: "Microsoft YaHei", "PingFang SC", "Hiragino Sans GB", "Segoe UI", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

body.modal-open { overflow: hidden; }

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

/* ---------------------------------------------------------------- 布局 --- */
.layout { display: flex; min-height: 100vh; }

.sidebar {
  width: 220px;
  flex: 0 0 220px;
  background: var(--sidebar-bg);
  color: #fff;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  z-index: 30;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 18px 16px;
  border-bottom: 1px solid rgba(255, 255, 255, .08);
}

.brand-logo {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  overflow: hidden;
  flex: 0 0 auto;
}

.brand-logo img {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 9px;
}

.brand-text {
  font-size: 14px;
  font-weight: 600;
  letter-spacing: .2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 150px;
}

.menu { flex: 1; padding: 10px 0 20px; }

.menu-group-title {
  padding: 14px 18px 6px;
  font-size: 12px;
  color: #7c86a0;
  letter-spacing: 1px;
}

.menu-item {
  display: block;
  padding: 9px 18px;
  color: var(--sidebar-text);
  font-size: 14px;
  border-left: 3px solid transparent;
  cursor: pointer;
}

.menu-item:hover { background: var(--sidebar-bg-soft); color: #fff; }

.menu-item.active {
  background: var(--sidebar-bg-soft);
  color: #fff;
  border-left-color: var(--primary);
  font-weight: 600;
}

.sidebar-foot {
  padding: 12px 18px;
  font-size: 12px;
  color: #6f7893;
  border-top: 1px solid rgba(255, 255, 255, .08);
}

.main {
  margin-left: 220px;
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  height: 56px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar-title { font-size: 16px; font-weight: 600; }

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

.user-name { font-size: 13px; color: var(--muted); }

.content { padding: 20px; flex: 1; min-width: 0; }

/* ------------------------------------------------------------- 通用组件 --- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

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

.card-title { font-size: 15px; font-weight: 600; }

.card-sub { font-size: 12px; color: var(--muted); font-weight: 400; margin-left: 8px; }

.card-body { padding: 16px; }

.card-body.tight { padding: 0; }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.toolbar .spacer { flex: 1; }
.toolbar-summary { font-size: 13px; color: var(--muted); }

/* ------------------------------------------------------------- 客户地图 --- */
.map-layout {
  display: flex;
  gap: 16px;
  align-items: stretch;
}

.map-main { flex: 1 1 auto; min-width: 0; }

.map-canvas {
  width: 100%;
  height: 620px;
  background: #fbfcfe;
  border-radius: 10px;
}

.map-side {
  flex: 0 0 290px;
  max-height: 620px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
}

.map-list-title {
  position: sticky;
  top: 0;
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 600;
  color: #55607a;
  background: #fff;
  border-bottom: 1px solid var(--border);
}

.map-list-items { list-style: none; margin: 0; padding: 0; }

.map-list-item {
  padding: 10px 14px;
  border-bottom: 1px solid #f1f4f9;
  cursor: pointer;
  transition: background 0.15s;
}

.map-list-item:hover { background: #f5f8ff; }
.map-list-item.unlocated { opacity: 0.65; }

.mli-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}

.mli-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mli-value { font-size: 13px; color: var(--primary); font-weight: 600; white-space: nowrap; }
.mli-sub { margin-top: 3px; font-size: 12px; color: var(--muted); }

/* 乡镇分组下的客户名 */
.mli-customers {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 6px;
  margin-top: 6px;
}

.mli-cust {
  font-size: 11px;
  line-height: 1.6;
  padding: 1px 7px;
  border-radius: 10px;
  background: var(--primary-light);
  color: var(--primary-dark);
  cursor: pointer;
  white-space: nowrap;
}

.mli-cust:hover { background: #dbeafe; }

/* 地图面包屑（全国 › 省 › 市 › 区县 › 乡镇） */
.map-crumb {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  padding: 0 2px 8px;
  font-size: 13px;
  color: var(--muted);
}

.crumb-link { color: var(--primary); cursor: pointer; font-weight: 600; }
.crumb-link:hover { text-decoration: underline; }
.crumb-sep { color: #b6bcc9; }
.crumb-cur { color: #374151; font-weight: 600; }

/* 表单提示 */
.form-tip {
  margin-top: 10px;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.6;
}

@media (max-width: 900px) {
  .map-layout { flex-direction: column; }
  .map-side { flex: 1 1 auto; width: 100%; max-height: 300px; }
}

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

.btn:hover { border-color: var(--primary); color: var(--primary); }

.btn:disabled { opacity: .5; cursor: not-allowed; }

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

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

.btn-ghost { background: #fff; }

.btn-sm { height: 28px; padding: 0 10px; font-size: 12px; }

.btn-block { width: 100%; height: 38px; }

.btn-text {
  height: auto;
  padding: 0;
  border: none;
  background: none;
  color: var(--primary);
  font-size: 13px;
}
.btn-text:hover { text-decoration: underline; }

.btn-text.danger { color: var(--danger); }

.input, .select, .textarea {
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: #fff;
  font-size: 13px;
  font-family: inherit;
  color: var(--text);
  outline: none;
  width: 100%;
  min-width: 0;
}

.textarea { height: auto; padding: 8px 10px; resize: vertical; min-height: 64px; line-height: 1.5; }

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

.input.input-error, .select.input-error { border-color: var(--danger); }

.input-sm { height: 30px; }

.checkbox {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  user-select: none;
}

.checkbox input { width: 14px; height: 14px; accent-color: var(--primary); }

/* 表单栅格 */
.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px 16px;
}

.form-grid .field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }

.form-grid .field.full { grid-column: 1 / -1; }

.field label { font-size: 13px; color: #374151; }

.field label .req { color: var(--danger); margin-left: 2px; }

.field-hint { font-size: 12px; color: var(--muted); }

.form-error {
  margin: 0 0 14px;
  padding: 9px 12px;
  border-radius: 8px;
  background: var(--danger-bg);
  border: 1px solid #fecaca;
  color: #b91c1c;
  font-size: 13px;
}

.form-error.info { background: var(--info-bg); border-color: #bae6fd; color: #0369a1; }

.form-error:empty { display: none; }

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

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

/* 列数较多时保持最小宽度，改为横向滚动，避免把中文列挤成竖排 */
#o-table { min-width: 1120px; }

.table td.nowrap { white-space: nowrap; }

.table th, .table td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.table th {
  background: #f7f8fc;
  color: #475069;
  font-weight: 600;
  white-space: nowrap;
}

.table tbody tr:nth-child(even) { background: #fafbfd; }

.table tbody tr:hover { background: var(--primary-light); }

.table td.num, .table th.num { text-align: right; font-variant-numeric: tabular-nums; }

.table td.center, .table th.center { text-align: center; }

.table .row-actions { display: flex; gap: 10px; white-space: nowrap; }

.table-empty td {
  text-align: center;
  color: var(--muted);
  padding: 34px 12px;
}

/* 标签 */
.tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  line-height: 18px;
  white-space: nowrap;
}

.tag-primary { background: var(--primary-light); color: var(--primary-dark); }
.tag-info { background: var(--info-bg); color: #0369a1; }
.tag-success { background: var(--success-bg); color: #15803d; }
.tag-warn { background: var(--warn-bg); color: #b45309; }
.tag-danger { background: var(--danger-bg); color: #b91c1c; }
.tag-default { background: #f1f2f6; color: #4b5563; }

/* 分页 */
.pager {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 16px;
  flex-wrap: wrap;
}

.pager-info { font-size: 13px; color: var(--muted); margin-right: 4px; }

/* 空状态 / 加载态 */
.empty-state {
  padding: 40px 16px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}

.empty-state .empty-title { font-size: 15px; color: #55607a; margin-bottom: 6px; }

.loading-box {
  padding: 40px 16px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}

/* 加载态 / 空状态以叠加层方式呈现：隐藏容器内其它内容，但节点本身保留在 DOM 中，
   这样表格渲染函数始终作用在真实节点上（见 assets/js/ui.js 的 setState/clearState） */
.has-state > *:not(.view-state) { display: none; }
.view-state { display: block; }

/* 统计卡片 */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 14px;
  margin-bottom: 16px;
}

.stat-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
}

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

.stat-value {
  font-size: 22px;
  font-weight: 600;
  margin-top: 6px;
  font-variant-numeric: tabular-nums;
  word-break: break-all;
}

.stat-value .unit { font-size: 13px; font-weight: 400; color: var(--muted); margin-left: 4px; }

.stat-value.text-primary { color: var(--primary); }
.stat-value.text-success { color: var(--success); }
.stat-value.text-warn { color: var(--warn); }
.stat-value.text-danger { color: var(--danger); }

/* 纯 CSS 柱状趋势图 */
.chart { padding: 16px; }

.chart-legend {
  display: flex;
  gap: 18px;
  flex-wrap: wrap;
  margin-bottom: 12px;
  font-size: 12px;
  color: var(--muted);
}

.chart-legend span { display: inline-flex; align-items: center; gap: 6px; }

.lg { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }
.lg-sales { background: var(--primary); }
.lg-purchase { background: #94a3b8; }
.lg-profit { background: #10b981; }

.chart-body {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  height: 240px;
  padding: 10px 4px 0;
  overflow-x: auto;
  border-bottom: 1px solid var(--border);
}

.chart-col {
  flex: 1 0 58px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  height: 100%;
}

.bar-group {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 3px;
  height: 200px;
  width: 100%;
}

.bar {
  width: 13px;
  border-radius: 3px 3px 0 0;
  min-height: 2px;
  background: var(--primary);
}

.bar-sales { background: var(--primary); }
.bar-purchase { background: #94a3b8; }
.bar-profit { background: #10b981; }

.chart-label {
  font-size: 11px;
  color: var(--muted);
  padding: 6px 0 8px;
  white-space: nowrap;
}

/* 弹窗 */
.modal-mask {
  position: fixed;
  inset: 0;
  background: rgba(15, 20, 35, .45);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 16px;
  z-index: 100;
  overflow-y: auto;
}

.modal {
  background: var(--card);
  border-radius: 12px;
  width: 100%;
  max-width: 560px;
  box-shadow: 0 20px 50px rgba(15, 20, 35, .25);
  animation: modal-in .16s ease-out;
}

@keyframes modal-in {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: translateY(0); }
}

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

.modal-title { font-size: 15px; font-weight: 600; }

.modal-close {
  border: none;
  background: none;
  font-size: 22px;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
  padding: 0 4px;
}

.modal-close:hover { color: var(--text); }

.modal-body { padding: 16px; max-height: 68vh; overflow-y: auto; }

.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

.confirm-text { font-size: 14px; line-height: 1.7; }

.detail-box {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px 16px;
  margin-bottom: 14px;
  font-size: 13px;
}

.detail-box .kv { display: flex; gap: 6px; }
.detail-box .kv .k { color: var(--muted); min-width: 72px; }
.detail-box .kv .v { font-weight: 600; word-break: break-all; }

.sub-title {
  font-size: 13px;
  font-weight: 600;
  margin: 14px 0 8px;
  color: #374151;
}

/* Toast */
.toast-wrap {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
  pointer-events: none;
}

.toast {
  padding: 9px 16px;
  border-radius: 8px;
  font-size: 13px;
  color: #fff;
  background: #374151;
  box-shadow: 0 6px 20px rgba(15, 20, 35, .18);
  max-width: 80vw;
  opacity: 1;
  transition: opacity .2s;
}

.toast-out { opacity: 0; }

.toast-success { background: #15803d; }
.toast-error { background: #b91c1c; }
.toast-warn { background: #b45309; }
.toast-info { background: #1d4ed8; }

/* 登录页 */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(140deg, #1f2436 0%, #2f3a5f 55%, #4f46e5 130%);
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 24px 60px rgba(10, 14, 30, .35);
  padding: 28px 26px 22px;
}

.login-brand { text-align: center; margin-bottom: 22px; }

.login-logo {
  width: 46px;
  height: 46px;
  margin: 0 auto 12px;
  border-radius: 12px;
  overflow: hidden;
}

.login-logo img {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 12px;
}

.login-brand h1 { font-size: 19px; margin: 0 0 6px; }

.login-brand p { font-size: 13px; color: var(--muted); margin: 0; }

.login-card .field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }

.login-card .field label { font-size: 13px; color: #374151; }

.login-tip {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--muted);
  text-align: center;
}

/* 明细行内提示 */
.cell-hint { font-size: 11px; color: var(--danger); margin-top: 3px; min-height: 14px; }

/* 表格单元格内的次级说明（如商品条码） */
.cell-sub { font-size: 11px; color: var(--muted); margin-top: 2px; }

.items-table input[type="number"] { text-align: right; }

.order-total {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 10px;
  padding: 12px 0 0;
  font-size: 14px;
}

.order-total .amount { font-size: 18px; font-weight: 700; color: var(--primary); }

/* 导出中心 */
.export-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 14px;
}

.export-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  background: #fff;
}

.export-card h3 { font-size: 15px; margin: 0 0 6px; }

.export-card p { font-size: 12px; color: var(--muted); margin: 0 0 12px; min-height: 32px; }

.export-actions { display: flex; gap: 8px; }

.hint-text { font-size: 12px; color: var(--muted); margin-top: 8px; }

@media (max-width: 900px) {
  .sidebar { width: 180px; flex-basis: 180px; }
  .main { margin-left: 180px; }
  .form-grid { grid-template-columns: 1fr; }
}

/* 库存盘点 */
.toolbar-label {
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
}

.st-input {
  width: 92px;
  padding: 5px 8px;
  text-align: right;
}

.st-summary {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 18px;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  background: #fafbff;
  font-size: 13px;
  color: #374151;
}

.st-summary b { font-size: 14px; }

.st-gain { color: var(--success); font-weight: 600; }
.st-loss { color: var(--danger); font-weight: 600; }
.st-equal { color: var(--muted); }

/* 打印容器：屏幕上隐藏，打印时输出 */
#print-root { display: none; }

/* 单据版式（A4） */
.doc {
  padding: 0;
  color: #111827;
  font-size: 12px;
  background: #fff;
}

.doc-head {
  text-align: center;
  border-bottom: 2px solid #111827;
  padding-bottom: 8px;
  margin-bottom: 12px;
}

.doc-title {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 4px;
}

.doc-company {
  margin-top: 4px;
  font-size: 12px;
  color: #4b5563;
}

.doc-info {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 12px;
}

.doc-info th,
.doc-info td {
  border: 1px solid #9ca3af;
  padding: 5px 8px;
  font-size: 12px;
  text-align: left;
}

.doc-info th {
  width: 86px;
  background: #f3f4f6;
  font-weight: 600;
  color: #374151;
}

.doc-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 12px;
}

.doc-table th,
.doc-table td {
  border: 1px solid #9ca3af;
  padding: 5px 6px;
  font-size: 12px;
}

.doc-table thead th {
  background: #f3f4f6;
  font-weight: 600;
  text-align: left;
}

.doc-table tfoot td {
  background: #fafafa;
  font-weight: 700;
}

.doc-table .c { text-align: center; }
.doc-table .r { text-align: right; }

.doc-remark {
  font-size: 12px;
  color: #374151;
  margin-bottom: 10px;
  min-height: 18px;
}

.doc-sign {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  margin-top: 26px;
  font-size: 12px;
}

.doc-foot {
  margin-top: 18px;
  text-align: center;
  font-size: 11px;
  color: #6b7280;
}

@media print {
  body.print-mode #app,
  body.print-mode #modal-root,
  body.print-mode #toast-root {
    display: none !important;
  }

  body.print-mode #print-root {
    display: block !important;
  }

  body.print-mode {
    background: #fff;
  }

  @page {
    size: A4 portrait;
    margin: 10mm;
  }
}
