:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #dc2626;
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --in: #16a34a;
  --out: #ea580c;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
}
.hidden { display: none !important; }
.flex-spacer { flex: 1; }

/* 按钮 */
.btn {
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  padding: 9px 14px;
  border-radius: 10px;
  font-size: 15px;
  cursor: pointer;
  transition: opacity .15s;
}
.btn:active { opacity: .7; }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-sm { padding: 7px 10px; font-size: 13px; }
.btn-block { width: 100%; padding: 13px; font-size: 16px; font-weight: 600; }
.icon-btn { background: none; border: none; color: #fff; font-size: 22px; cursor: pointer; }

/* 登录 */
.login-view {
  min-height: 100dvh;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  background: linear-gradient(160deg, #2563eb, #1e40af);
}
.login-card {
  background: #fff; border-radius: 18px; padding: 32px 24px;
  width: 100%; max-width: 360px; text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,.25);
}
.login-logo { font-size: 52px; }
.login-card h1 { margin: 8px 0 4px; font-size: 22px; }
.login-sub { color: var(--muted); margin-bottom: 20px; }
.login-card input {
  width: 100%; padding: 13px; border: 1px solid var(--border);
  border-radius: 10px; font-size: 16px; margin-bottom: 14px;
}
.err-text { color: var(--danger); font-size: 13px; margin-top: 10px; min-height: 18px; }

/* App 布局 */
.app { display: flex; flex-direction: column; height: 100dvh; }
.topbar {
  background: var(--primary); color: #fff;
  padding: 14px 16px; padding-top: calc(14px + env(safe-area-inset-top, 0px));
  display: flex; align-items: center; justify-content: space-between;
  font-size: 18px; font-weight: 600;
  position: sticky; top: 0; z-index: 10;
}
.content { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-bottom: 12px; }
.tab-page { padding: 12px; }

/* 底部导航 */
.tabbar {
  display: flex; background: #fff; border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
}
.tab-btn {
  flex: 1; border: none; background: none; color: var(--muted);
  padding: 8px 0 10px; font-size: 12px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.tab-btn span { font-size: 22px; }
.tab-btn.active { color: var(--primary); }

/* 搜索栏 */
.search-bar { display: flex; gap: 8px; margin-bottom: 12px; }
.search-bar input { flex: 1; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; font-size: 15px; }

/* 列表 */
.list { display: flex; flex-direction: column; gap: 10px; }
.card {
  background: var(--card); border-radius: 12px; padding: 14px;
  box-shadow: 0 1px 3px rgba(0,0,0,.06);
}
.p-row { display: flex; justify-content: space-between; align-items: flex-start; }
.p-name { font-weight: 600; font-size: 16px; }
.p-meta { color: var(--muted); font-size: 12px; margin-top: 4px; }
.p-stock { text-align: right; }
.p-stock .num { font-size: 22px; font-weight: 700; color: var(--primary); }
.p-stock .num.low { color: var(--danger); }
.p-stock .unit { color: var(--muted); font-size: 12px; }
.p-price { color: var(--out); font-size: 13px; }

.empty { text-align: center; color: var(--muted); padding: 48px 20px; }

/* 统计卡片 */
.stats-cards { display: flex; gap: 8px; margin-bottom: 12px; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.stats-cards .stat-item {
  flex: 0 0 auto; min-width: 72px; text-align: center;
  background: var(--card); border-radius: 10px; padding: 10px 8px;
  box-shadow: 0 1px 3px rgba(0,0,0,.06);
}
.stat-val { font-size: 18px; font-weight: 700; color: var(--primary); }
.stat-label { font-size: 11px; color: var(--muted); margin-top: 2px; }

/* 日期筛选 */
.date-filter { display: flex; gap: 6px; align-items: center; margin-bottom: 12px; }
.date-filter input[type="date"] {
  flex: 1; padding: 8px 10px; border: 1px solid var(--border);
  border-radius: 8px; font-size: 14px; color: var(--text);
}
.date-sep { color: var(--muted); font-size: 13px; }

/* 分段控件 */
.seg { display: flex; background: #e2e8f0; border-radius: 10px; padding: 3px; margin-bottom: 12px; }
.seg-btn {
  flex: 1; border: none; background: none; padding: 8px; border-radius: 8px;
  font-size: 14px; color: var(--muted); cursor: pointer;
}
.seg-btn.active { background: #fff; color: var(--text); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.1); }

/* 出入库操作 */
.op-add { position: relative; margin-bottom: 12px; }
.op-add input { width: 100%; padding: 11px 12px; border: 1px solid var(--border); border-radius: 10px; font-size: 15px; }
.suggest {
  position: absolute; left: 0; right: 0; top: 100%; z-index: 20;
  background: #fff; border: 1px solid var(--border); border-radius: 10px;
  margin-top: 4px; max-height: 260px; overflow-y: auto; box-shadow: 0 8px 24px rgba(0,0,0,.12);
}
.suggest-item { padding: 11px 14px; border-bottom: 1px solid var(--border); cursor: pointer; }
.suggest-item:last-child { border-bottom: none; }
.suggest-item:active { background: #f1f5f9; }
.suggest-item .s-stock { color: var(--muted); font-size: 12px; }

.cart .card { display: flex; align-items: center; gap: 10px; }
.cart-info { flex: 1; min-width: 0; }
.cart-name { font-weight: 600; }
.cart-sub { color: var(--muted); font-size: 12px; }
.qty-ctrl { display: flex; align-items: center; gap: 6px; }
.qty-ctrl button { width: 32px; height: 32px; border-radius: 8px; border: 1px solid var(--border); background: #fff; font-size: 18px; cursor: pointer; }
.qty-ctrl input { width: 48px; text-align: center; padding: 6px 2px; border: 1px solid var(--border); border-radius: 8px; font-size: 15px; }
.cart-del { color: var(--danger); background: none; border: none; font-size: 20px; cursor: pointer; padding: 4px; }

.op-foot { padding: 14px 12px calc(14px + var(--safe-bottom)); border-top: 1px solid var(--border); margin-top: 12px; background: #fff; }
.op-foot input { width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; font-size: 15px; margin-bottom: 10px; }
.op-summary { display: flex; justify-content: space-between; margin: 6px 0 12px; font-size: 15px; }
.op-summary b { font-size: 18px; }

/* 单据 */
.order-card .o-head { display: flex; justify-content: space-between; align-items: center; }
.o-no { font-weight: 600; font-size: 14px; }
.o-type { font-size: 12px; padding: 2px 8px; border-radius: 6px; color: #fff; }
.o-type.in { background: var(--in); }
.o-type.out { background: var(--out); }
.o-meta { color: var(--muted); font-size: 12px; margin-top: 6px; }
.o-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 10px; }
.o-total { font-weight: 600; }

/* 弹窗 */
.modal { position: fixed; inset: 0; z-index: 100; display: flex; align-items: flex-end; justify-content: center; }
.modal-mask { position: absolute; inset: 0; background: rgba(0,0,0,.45); }
.modal-box {
  position: relative; background: #fff; width: 100%; max-width: 480px;
  border-radius: 18px 18px 0 0; padding: 20px; max-height: 90dvh; overflow-y: auto;
  padding-bottom: calc(20px + var(--safe-bottom));
}
.modal-box h3 { margin-bottom: 16px; }
.form { display: flex; flex-direction: column; gap: 12px; }
.form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.form input { padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; font-size: 15px; color: var(--text); }
.form .row { display: flex; gap: 10px; }
.form .row label { flex: 1; }
.modal-foot { display: flex; gap: 10px; align-items: center; margin-top: 20px; }
.receipt-foot { flex-wrap: wrap; justify-content: flex-end; }
.receipt-preview { display: flex; justify-content: center; background: #f8fafc; padding: 12px; border-radius: 10px; }
.receipt-preview canvas { max-width: 100%; box-shadow: 0 2px 12px rgba(0,0,0,.15); }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: 80px; transform: translateX(-50%);
  background: rgba(15,23,42,.92); color: #fff; padding: 10px 18px;
  border-radius: 10px; font-size: 14px; z-index: 200; max-width: 80%; text-align: center;
}

@media (min-width: 640px) {
  .modal { align-items: center; }
  .modal-box { border-radius: 18px; }
}
