/* ========== Reset & Base ========== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --primary: #1a73e8;
  --primary-light: #e8f0fe;
  --primary-dark: #1557b0;
  --success: #34a853;
  --warning: #fbbc04;
  --danger: #ea4335;
  --info: #4285f4;
  --bg: #f5f7fa;
  --card-bg: #ffffff;
  --sidebar-bg: #1e293b;
  --sidebar-hover: #334155;
  --sidebar-active: var(--primary);
  --text: #1f2937;
  --text-secondary: #6b7280;
  --text-light: #9ca3af;
  --border: #e5e7eb;
  --shadow: 0 1px 3px rgba(0,0,0,0.1);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.15);
  --radius: 8px;
  --radius-lg: 12px;
  --sidebar-width: 240px;
  --header-height: 0px;
}

/* ========== 访问密码登录层 ========== */
#login-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--bg);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 320px;
  background: var(--card-bg, #fff);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px 24px;
  text-align: center;
  box-shadow: 0 8px 30px rgba(0,0,0,0.12);
}

.login-logo {
  font-size: 44px;
  line-height: 1;
}

.login-box h2 {
  margin: 12px 0 4px;
  font-size: 18px;
}

.login-tip {
  color: var(--text-secondary);
  font-size: 13px;
  margin: 0 0 16px;
}

.login-box input {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 16px;
  text-align: center;
  box-sizing: border-box;
  outline: none;
}

.login-box input:focus {
  border-color: var(--primary);
}

.login-btn {
  width: 100%;
  margin-top: 12px;
  padding: 12px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

.login-btn:active { opacity: 0.85; }

.login-error {
  color: var(--danger);
  font-size: 13px;
  margin-top: 10px;
}

/* ========== 只读权限（2312 / 666）========== */
/* 只读：所有按钮不可点、输入框/下拉/文本域不可编辑 */
body.perm-readonly .btn,
body.perm-readonly button:not(#mobile-back-btn) {
  opacity: 0.45;
  pointer-events: none;
  cursor: not-allowed;
}

/* 例外：返回首页按钮、登录按钮在只读模式下保持可点且正常显示 */
body.perm-readonly #mobile-back-btn,
body.perm-readonly #login-btn {
  pointer-events: auto;
  opacity: 1;
  cursor: pointer;
}

body.perm-readonly input,
body.perm-readonly select,
body.perm-readonly textarea {
  pointer-events: none;
  background: #f2f2f2 !important;
  color: #666 !important;
}

/* 限制角色（666）：隐藏台账、库存看板入口 */
body.perm-limited .nav-item[data-page="main-ledger"],
body.perm-limited .nav-item[data-page="main-inventory"] {
  display: none !important;
}

body {
  font-family: 'Microsoft YaHei', '微软雅黑', 'PingFang SC', 'Hiragino Sans GB', 'Source Han Sans CN', 'Noto Sans SC', -apple-system, BlinkMacSystemFont, 'Segoe UI', '黑体', 'SimHei', '宋体', 'SimSun', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  overflow-x: hidden;
}

/* ========== Layout ========== */
#app {
  display: flex;
  min-height: 100vh;
}

/* ========== Sidebar ========== */
#sidebar {
  width: var(--sidebar-width);
  background: var(--sidebar-bg);
  color: #fff;
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  transition: transform 0.3s ease;
}

.sidebar-header {
  padding: 16px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid rgba(255,255,255,0.1);
  flex-shrink: 0;
}

.app-logo { font-size: 28px; }
.sidebar-header h2 { font-size: 16px; font-weight: 600; flex: 1; }
.sidebar-close { display: none; background: none; border: none; color: #fff; font-size: 20px; cursor: pointer; padding: 4px 8px; }
.sidebar-close:hover { opacity: 0.8; }

/* 移动端浮动菜单按钮 */
#mobile-menu-btn {
  display: none;
  position: fixed;
  top: 10px;
  left: 10px;
  z-index: 1100;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  font-size: 22px;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
  line-height: 40px;
  text-align: center;
}

/* 移动端返回首页按钮 */
#mobile-back-btn {
  display: none;
  position: fixed;
  top: 10px;
  left: 10px;
  z-index: 1100;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: 18px;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
  align-items: center;
  justify-content: center;
}

.sidebar-nav { flex: 1; overflow-y: auto; padding: 8px 0; }

.nav-group { margin-bottom: 4px; }
.nav-group-title { padding: 8px 16px 4px; font-size: 11px; text-transform: uppercase; color: rgba(255,255,255,0.4); letter-spacing: 0.5px; }
.nav-divider { height: 1px; background: rgba(255,255,255,0.1); margin: 8px 16px; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  color: rgba(255,255,255,0.75);
  text-decoration: none;
  cursor: pointer;
  transition: all 0.2s;
  font-size: 13px;
  border-left: 3px solid transparent;
}

.nav-item:hover { background: var(--sidebar-hover); color: #fff; }
.nav-item.active { background: rgba(26,115,232,0.2); color: #fff; border-left-color: var(--primary); }
.nav-icon { font-size: 18px; width: 24px; text-align: center; }
.nav-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

#sidebar-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 999; }

/* ========== Main Content ========== */
#main-content {
  flex: 1;
  margin-left: var(--sidebar-width);
  padding: 20px;
  min-height: 100vh;
  max-width: calc(100vw - var(--sidebar-width));
}

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

/* ========== Cards ========== */
.card {
  background: var(--card-bg);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  margin-bottom: 20px;
  overflow: hidden;
}

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

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

/* ========== Forms ========== */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 16px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-group label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
}

.form-group input,
.form-group select,
.form-group textarea {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  outline: none;
  transition: border-color 0.2s;
  background: #fff;
  width: 100%;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(26,115,232,0.15);
}

.form-group textarea { resize: vertical; min-height: 60px; }

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

.form-group .input-with-btn input { flex: 1; }

.btn {
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}

.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: #2d8f47; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #d33426; }
.btn-warning { background: var(--warning); color: #333; }
.btn-secondary { background: #f0f0f0; color: var(--text); }
.btn-secondary:hover { background: #e0e0e0; }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-lg { padding: 10px 24px; font-size: 15px; }

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

.form-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 16px;
  grid-column: 1 / -1;
}

/* ========== Tables ========== */
.table-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

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

table th {
  background: var(--bg);
  padding: 10px 12px;
  text-align: left;
  font-weight: 600;
  color: var(--text-secondary);
  font-size: 12px;
  white-space: nowrap;
  border-bottom: 2px solid var(--border);
  position: sticky;
  top: 0;
}

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

table tr.row-color td { background-color: var(--row-bg); }
table tr.row-color:hover td { background-color: var(--row-bg-hover); }

/* ========== Badge / Tag ========== */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 500;
}

.badge-inbound { background: #e6f4ea; color: #1e7e34; }
.badge-outbound { background: #fce8e6; color: #c62828; }
.badge-stock-low { background: #fff3e0; color: #e65100; }
.badge-stock-normal { background: #e8f5e9; color: #2e7d32; }

/* ========== Tag Input ========== */
.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 4px;
}

.tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  background: var(--primary-light);
  color: var(--primary);
  border-radius: 12px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s;
}

.tag:hover { background: #d2e3fc; }
.tag .tag-remove { font-size: 14px; cursor: pointer; margin-left: 2px; color: var(--text-secondary); }
.tag .tag-remove:hover { color: var(--danger); }
.tag.active { background: var(--primary); color: #fff; }

/* ========== Chart Container ========== */
.chart-container {
  width: 100%;
  max-width: 600px;
  height: 200px;
  margin: 10px 0;
}

/* ========== Image Preview ========== */
.image-preview {
  max-width: 120px;
  max-height: 120px;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--border);
  cursor: pointer;
}

.image-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.image-preview-lg {
  max-width: 300px;
  max-height: 300px;
}

/* ========== Camera ========== */
.camera-container {
  position: relative;
  width: 100%;
  max-width: 400px;
}

.camera-container video {
  width: 100%;
  border-radius: var(--radius);
  background: #000;
}

.camera-container canvas { display: none; }

/* ========== Scanner ========== */
.scanner-container {
  position: relative;
  width: 100%;
  max-width: 400px;
  overflow: hidden;
  border-radius: var(--radius);
  background: #000;
  min-height: 200px;
}

.scanner-container video {
  width: 100%;
  display: block;
}

.scanner-overlay {
  position: absolute;
  inset: 0;
  border: 2px solid rgba(26,115,232,0.5);
  border-radius: var(--radius);
}

.scanner-line {
  position: absolute;
  left: 10%;
  right: 10%;
  height: 2px;
  background: var(--primary);
  animation: scan 2s linear infinite;
  box-shadow: 0 0 8px rgba(26,115,232,0.6);
}

@keyframes scan {
  0% { top: 10%; }
  50% { top: 90%; }
  100% { top: 10%; }
}

/* ========== Modal ========== */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 2000;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.modal-overlay.show { display: flex; }

.modal-container {
  background: var(--card-bg);
  border-radius: var(--radius-lg);
  max-width: 600px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  animation: modalIn 0.2s ease;
}

@keyframes modalIn {
  from { opacity: 0; transform: translateY(-20px); }
  to { opacity: 1; transform: translateY(0); }
}

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

.modal-header h3 { font-size: 16px; }
.modal-close { background: none; border: none; font-size: 24px; cursor: pointer; color: var(--text-secondary); padding: 0 4px; }
.modal-close:hover { color: var(--text); }
.modal-body { padding: 20px; }

/* ========== Tabs ========== */
.tabs {
  display: flex;
  border-bottom: 2px solid var(--border);
  margin-bottom: 16px;
}

.tab {
  padding: 10px 20px;
  cursor: pointer;
  color: var(--text-secondary);
  font-weight: 500;
  font-size: 13px;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: all 0.2s;
}

.tab:hover { color: var(--text); }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }

/* ========== Stats Grid ========== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}

.stat-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  text-align: center;
}

.stat-card .stat-value { font-size: 24px; font-weight: 700; color: var(--primary); }
.stat-card .stat-label { font-size: 12px; color: var(--text-secondary); margin-top: 4px; }

/* ========== Grouped Sections ========== */
.product-group {
  margin-bottom: 24px;
}

.product-group-title {
  font-size: 15px;
  font-weight: 600;
  padding: 8px 0;
  margin-bottom: 8px;
  color: var(--primary);
  border-bottom: 2px solid var(--primary-light);
}

/* ========== Toast ========== */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: #323232;
  color: #fff;
  padding: 10px 24px;
  border-radius: var(--radius);
  font-size: 14px;
  opacity: 0;
  transition: all 0.3s;
  z-index: 3000;
  white-space: nowrap;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ========== Empty State ========== */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-light);
}

.empty-state .empty-icon { font-size: 48px; margin-bottom: 12px; }
.empty-state p { font-size: 14px; }

/* ========== Tag Editor ========== */
.tag-editor {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}

.tag-editor input { flex: 1; }

.option-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.option-tags .opt-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  background: #f0f0f0;
  border-radius: 12px;
  font-size: 12px;
}

.opt-tag .opt-remove { cursor: pointer; color: var(--text-light); font-size: 14px; }
.opt-tag .opt-remove:hover { color: var(--danger); }

/* ========== Responsive ========== */
@media (max-width: 768px) {
  /* 手机版：隐藏侧边栏和菜单按钮，用首页导航 */
  #sidebar { display: none; }
  #sidebar-overlay { display: none; }
  #mobile-menu-btn { display: none; }
  .sidebar-close { display: none; }
  .sidebar-toggle { display: none; }
  
  #main-content {
    margin-left: 0;
    padding: 12px;
    max-width: 100vw;
  }

  .form-grid {
    grid-template-columns: 1fr;
  }

  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .card-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .card-header .btn-group {
    width: 100%;
    display: flex;
    gap: 8px;
  }

  .card-header .btn-group .btn {
    flex: 1;
    justify-content: center;
  }

  .modal-container {
    margin: 10px;
    max-height: 85vh;
  }

  .chart-container {
    max-width: 100%;
    height: 180px;
  }

  .table-wrapper {
    margin: 0 -12px;
    padding: 0 12px;
  }

  table { font-size: 12px; }
  table th, table td { padding: 8px 8px; }

  .tabs { overflow-x: auto; }
  .tab { padding: 8px 14px; font-size: 12px; white-space: nowrap; }
}

@media (max-width: 480px) {
  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .stat-card { padding: 12px; }
  .stat-card .stat-value { font-size: 20px; }

  .card-body { padding: 12px; }
}

/* ========== Print ========== */
@media print {
  #sidebar { display: none; }
  #main-content { margin-left: 0; }
  .card { box-shadow: none; break-inside: avoid; }
}

/* ========== 拖拽排序 ========== */
tr[draggable="true"] { transition: background 0.15s; }
tr[draggable="true"]:hover { background: #f0f7ff !important; }
tr.drag-source { opacity: 0.45; }
tr.drag-over { border-top: 2px solid #1a73e8; background: #e8f0fe !important; }
