:root {
  --bg: #f2f5f9;
  --card: #ffffff;
  --ink: #0f172a;
  --ink-2: #475569;
  --ink-3: #94a3b8;
  --line: #e2e8f0;
  --line-2: #cbd5e1;
  --blue: #1d4ed8;
  --blue-soft: #eff4ff;
  --green: #047857;
  --red: #b91c1c;
  --shadow: 0 1px 2px rgba(15, 23, 42, .05), 0 8px 24px -12px rgba(15, 23, 42, .18);
  --r: 12px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 14px;
  -webkit-text-size-adjust: 100%;
}

button { font-family: inherit; cursor: pointer; }
input, textarea { font-family: inherit; }

/* ---------- 顶栏 ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 22px;
  background: rgba(255, 255, 255, .88);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }

.logo {
  width: 38px; height: 38px;
  flex: 0 0 38px;
  border-radius: 10px;
  background: linear-gradient(145deg, #2563eb, #1e40af);
  color: #fff;
  display: grid; place-items: center;
  font-weight: 700; font-size: 19px;
  box-shadow: 0 4px 12px -4px rgba(29, 78, 216, .6);
}

.brand-text h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 650;
  letter-spacing: .2px;
  white-space: nowrap;
}
.brand-text p {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--ink-3);
  white-space: nowrap;
}

.top-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.btn {
  height: 34px;
  padding: 0 14px;
  border-radius: 8px;
  border: 1px solid var(--line-2);
  background: #fff;
  color: var(--ink);
  font-size: 13px;
  font-weight: 500;
  transition: .15s;
  white-space: nowrap;
}
.btn:hover { border-color: #94a3b8; background: #f8fafc; }
.btn.primary {
  background: var(--blue);
  border-color: var(--blue);
  color: #fff;
  box-shadow: 0 2px 8px -2px rgba(29, 78, 216, .5);
}
.btn.primary:hover { background: #1e40af; border-color: #1e40af; }
.btn.ghost { background: transparent; }
.btn.ghost:hover { background: #f1f5f9; }
.btn.full { width: 100%; }

.save-state {
  font-size: 12px;
  color: var(--ink-3);
  min-width: 74px;
  text-align: right;
  transition: color .2s;
}
.save-state.saving { color: #b45309; }
.save-state.ok { color: var(--green); }
.save-state.err { color: var(--red); }

/* ---------- 主体 ---------- */
main {
  max-width: 1500px;
  margin: 0 auto;
  padding: 18px 22px 60px;
}

.datebar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}

.date-nav { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.nav-btn {
  width: 32px; height: 32px;
  border-radius: 8px;
  border: 1px solid var(--line-2);
  background: #fff;
  font-size: 17px;
  line-height: 1;
  color: var(--ink-2);
  padding: 0;
}
.nav-btn:hover { background: var(--blue-soft); border-color: #bfdbfe; }

.date-input {
  height: 32px;
  padding: 0 10px;
  border-radius: 8px;
  border: 1px solid var(--line-2);
  background: #fff;
  font-size: 14px;
  font-weight: 600;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.quick { display: flex; gap: 6px; margin-left: 4px; }

.chip {
  height: 30px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--line-2);
  background: #fff;
  font-size: 12.5px;
  color: var(--ink-2);
}
.chip:hover { background: var(--blue-soft); border-color: #bfdbfe; color: var(--blue); }
.chip.accent { border-color: #bfdbfe; background: var(--blue-soft); color: var(--blue); font-weight: 500; }
.chip.accent:hover { background: #e0ebff; }

.stats { display: flex; gap: 10px; flex-wrap: wrap; }

.stat {
  min-width: 96px;
  padding: 8px 14px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 1px 1px rgba(15, 23, 42, .03);
}
.stat-label { display: block; font-size: 11.5px; color: var(--ink-3); margin-bottom: 2px; }
.stat b { font-size: 18px; font-weight: 650; font-variant-numeric: tabular-nums; letter-spacing: -.3px; }

/* ---------- 表格卡片 ---------- */
.sheet {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--r);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, #fbfdff, #fff);
  flex-wrap: wrap;
}

.sheet-title { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 14px; min-width: 0; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--blue); flex: 0 0 7px; }
.muted { color: var(--ink-3); font-weight: 400; }
.sheet-title .muted { font-size: 12px; font-weight: 400; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.sheet-tools { display: flex; gap: 6px; }

.mini {
  height: 28px;
  padding: 0 10px;
  border-radius: 7px;
  border: 1px solid var(--line-2);
  background: #fff;
  font-size: 12.5px;
  color: var(--ink-2);
}
.mini:hover { background: #f8fafc; border-color: #94a3b8; }
.mini.danger:hover { color: var(--red); border-color: #fca5a5; background: #fef2f2; }

.table-wrap {
  overflow: auto;
  max-height: calc(100vh - 330px);
  min-height: 200px;
  -webkit-overflow-scrolling: touch;
}

table {
  border-collapse: separate;
  border-spacing: 0;
  table-layout: fixed;
  width: 100%;
  min-width: 780px;
}

thead th {
  position: sticky;
  top: 0;
  z-index: 3;
  background: #f8fafc;
  border-bottom: 1px solid var(--line-2);
  border-right: 1px solid var(--line);
  padding: 0 8px;
  height: 38px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-2);
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  user-select: none;
}
thead th:last-child { border-right: 0; }

tbody td {
  border-bottom: 1px solid var(--line);
  border-right: 1px solid var(--line);
  padding: 0;
  height: 36px;
  position: relative;
  background: #fff;
}
tbody td:last-child { border-right: 0; }
tbody tr:hover td { background: #fafcff; }
tbody tr:hover .row-no { color: var(--ink-2); }

td.idx, th.idx {
  width: 46px;
  text-align: center;
  color: var(--ink-3);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  position: sticky;
  left: 0;
  z-index: 2;
  background: #fcfdff;
  user-select: none;
}
thead th.idx { z-index: 4; background: #f8fafc; }
tbody tr:hover td.idx { background: #f4f8ff; }

td.act, th.act { width: 44px; text-align: center; }

.cell {
  width: 100%;
  height: 35px;
  border: 0;
  outline: 0;
  padding: 0 9px;
  background: transparent;
  font-size: 13.5px;
  color: var(--ink);
  border-radius: 0;
}
.cell:focus {
  background: #fff;
  box-shadow: inset 0 0 0 2px var(--blue);
  border-radius: 2px;
}
.cell.num { text-align: right; font-variant-numeric: tabular-nums; }
.cell.date { font-variant-numeric: tabular-nums; }
.cell::placeholder { color: #cbd5e1; }

.row-del {
  width: 24px; height: 24px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: #cbd5e1;
  font-size: 15px;
  line-height: 1;
  opacity: 0;
  transition: .12s;
}
tbody tr:hover .row-del { opacity: 1; }
.row-del:hover { background: #fee2e2; color: var(--red); }

tfoot td {
  position: sticky;
  bottom: 0;
  z-index: 2;
  background: #f8fafc;
  border-top: 1px solid var(--line-2);
  border-right: 1px solid var(--line);
  height: 38px;
  padding: 0 9px;
  font-size: 13px;
  font-weight: 650;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
tfoot td:last-child { border-right: 0; }
tfoot td.idx { text-align: center; font-size: 12px; color: var(--ink-2); }
tfoot td.num { text-align: right; }

.empty {
  padding: 46px 20px;
  text-align: center;
  color: var(--ink-3);
  font-size: 13.5px;
  line-height: 1.9;
}
.empty b { color: var(--ink-2); }

/* ---------- 表尾 ---------- */
.sheet-foot {
  display: flex;
  gap: 16px;
  align-items: flex-end;
  justify-content: space-between;
  padding: 12px 16px;
  border-top: 1px solid var(--line);
  background: #fcfdff;
  flex-wrap: wrap;
}

.day-note {
  flex: 1 1 380px;
  min-height: 44px;
  padding: 9px 11px;
  border: 1px solid var(--line-2);
  border-radius: 8px;
  font-size: 13px;
  line-height: 1.55;
  resize: vertical;
  color: var(--ink);
  background: #fff;
}
.day-note:focus { outline: 0; border-color: var(--blue); box-shadow: 0 0 0 3px rgba(29, 78, 216, .1); }

.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--ink-3); padding-bottom: 2px; }
.legend span { display: inline-flex; align-items: center; gap: 4px; }
kbd {
  display: inline-block;
  padding: 1px 5px;
  border: 1px solid var(--line-2);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: #fff;
  font-family: inherit;
  font-size: 11px;
  color: var(--ink-2);
  line-height: 1.4;
}

/* ---------- 历史 ---------- */
.history { margin-top: 26px; }
.history h2 { font-size: 14px; font-weight: 600; margin: 0 0 10px; color: var(--ink-2); }
.history h2 .muted { font-size: 12px; font-weight: 400; margin-left: 6px; }

.history-list { display: flex; gap: 8px; flex-wrap: wrap; }

.hist-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 132px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  text-align: left;
  transition: .15s;
}
.hist-item:hover { border-color: #bfdbfe; background: var(--blue-soft); }
.hist-item.active { border-color: var(--blue); background: var(--blue-soft); box-shadow: 0 0 0 1px var(--blue) inset; }
.hist-date { font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; }
.hist-meta { font-size: 11.5px; color: var(--ink-3); }
.hist-empty { font-size: 13px; color: var(--ink-3); padding: 8px 2px; }

/* ---------- 抽屉 ---------- */
.drawer-mask {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, .32);
  opacity: 0; pointer-events: none;
  transition: .2s;
  z-index: 50;
}
.drawer-mask.open { opacity: 1; pointer-events: auto; }

.drawer {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: min(420px, 92vw);
  background: #fff;
  z-index: 60;
  transform: translateX(102%);
  transition: transform .24s cubic-bezier(.4, 0, .2, 1);
  display: flex;
  flex-direction: column;
  box-shadow: -12px 0 40px -18px rgba(15, 23, 42, .4);
}
.drawer.open { transform: none; }

.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--line);
}
.drawer-head h3 { margin: 0; font-size: 15px; }
.icon-btn { border: 0; background: transparent; font-size: 16px; color: var(--ink-3); width: 28px; height: 28px; border-radius: 6px; }
.icon-btn:hover { background: #f1f5f9; color: var(--ink); }

.drawer-body { flex: 1; overflow: auto; padding: 16px; }
.drawer-foot { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line); }
.drawer-foot .btn { flex: 1; }

.field { display: block; margin-bottom: 14px; }
.field span { display: block; font-size: 12px; color: var(--ink-2); margin-bottom: 5px; }
.field input[type=text] {
  width: 100%; height: 34px; padding: 0 10px;
  border: 1px solid var(--line-2); border-radius: 8px; font-size: 13.5px;
}
.field input[type=text]:focus { outline: 0; border-color: var(--blue); box-shadow: 0 0 0 3px rgba(29, 78, 216, .1); }

.tip { font-size: 12px; color: var(--ink-3); margin: 0 0 12px; line-height: 1.6; }

.cfg-cols { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.cfg-col {
  display: grid;
  grid-template-columns: 1fr auto auto auto;
  gap: 8px;
  align-items: center;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #fcfdff;
}
.cfg-col input[type=text] {
  height: 30px; padding: 0 8px; border: 1px solid var(--line-2);
  border-radius: 6px; font-size: 13px; min-width: 0; width: 100%;
}
.cfg-col label { font-size: 11.5px; color: var(--ink-2); display: inline-flex; align-items: center; gap: 3px; white-space: nowrap; }
.cfg-col .del { border: 0; background: transparent; color: #cbd5e1; font-size: 14px; padding: 2px 4px; border-radius: 4px; }
.cfg-col .del:hover { color: var(--red); background: #fef2f2; }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%; bottom: 28px;
  transform: translate(-50%, 16px);
  background: #0f172a;
  color: #fff;
  font-size: 13px;
  padding: 9px 16px;
  border-radius: 9px;
  opacity: 0;
  pointer-events: none;
  transition: .22s;
  z-index: 100;
  max-width: 80vw;
  text-align: center;
  box-shadow: 0 10px 30px -10px rgba(0, 0, 0, .5);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- 响应式 ---------- */
@media (max-width: 860px) {
  .topbar { padding: 10px 12px; }
  .brand-text p { display: none; }
  main { padding: 12px 12px 50px; }
  .stats { width: 100%; display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
  .stat { min-width: 0; padding: 7px 9px; }
  .stat b { font-size: 15px; }
  .stat-label { font-size: 10.5px; }
  .table-wrap { max-height: none; }
  .sheet-title .muted { display: none; }
  .toast { bottom: 16px; }
}

/* ---------- 打印 ---------- */
@media print {
  @page { size: A4 landscape; margin: 12mm 10mm; }
  :root { --shadow: none; }
  body { background: #fff; }
  .topbar, .datebar .date-nav, .sheet-tools, .sheet-foot .legend, .history, .drawer, .drawer-mask, .toast { display: none !important; }
  main { max-width: none; padding: 0; }
  .datebar { margin-bottom: 8px; }
  .stats { gap: 6px; }
  .stat { border: 1px solid #999; border-radius: 0; padding: 4px 10px; box-shadow: none; }
  .stat b { font-size: 14px; }
  .sheet { border: 0; box-shadow: none; border-radius: 0; }
  .sheet-head { border-bottom: 1px solid #000; padding: 0 0 6px; background: #fff; }
  .table-wrap { overflow: visible; max-height: none; }
  table { min-width: 0; width: 100%; }
  thead th { background: #eee !important; border: 1px solid #666; position: static; }
  tbody td { border: 1px solid #666; height: 28px; }
  td.idx, tfoot td { position: static; background: #f2f2f2 !important; }
  tfoot td { border: 1px solid #666; }
  .row-del { display: none; }
  .cell { height: 27px; box-shadow: none !important; }
  .sheet-foot { border-top: 0; background: #fff; padding: 8px 0 0; }
  .day-note { border: 1px solid #666; min-height: 34px; }
  tbody tr, tfoot { break-inside: avoid; }
}

/* a 标签按钮 */
a.btn { display: inline-flex; align-items: center; text-decoration: none; }

/* ========== 顶栏分区：视图切换 与 功能按钮 分开 ========== */

/* 视图切换：分段按钮（一排连体），和右边的功能按钮明显区分 */
.view-switch {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 3px;
  background: #eef2f7;
  border: 1px solid var(--line);
  border-radius: 10px;
  flex: 0 0 auto;
}

.view-tab {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 28px;
  padding: 0 13px;
  border-radius: 7px;
  font-size: 13px;
  font-weight: 500;
  color: var(--ink-2);
  text-decoration: none;
  white-space: nowrap;
  transition: background .15s, color .15s, box-shadow .15s;
}
.view-tab:hover { background: #fff; color: var(--ink); }
.view-tab[aria-current="page"] {
  background: #fff;
  color: var(--blue);
  font-weight: 600;
  box-shadow: 0 1px 3px rgba(15, 23, 42, .14);
}

/* 功能按钮：靠右，左侧一条竖线，和视图切换彻底隔开 */
.top-actions {
  margin-left: auto;
  padding-left: 16px;
  border-left: 1px solid var(--line);
  row-gap: 8px;
}

/* 功能按钮内部再按用途分组，组间用虚线隔开 */
.action-group {
  display: flex;
  align-items: center;
  gap: 8px;
}
.action-group + .action-group {
  padding-left: 10px;
  border-left: 1px dashed var(--line-2);
}

@media (max-width: 860px) {
  /* 手机上顶栏不吸顶：否则会盖住吸顶的表头 */
  .topbar { position: static; flex-wrap: wrap; row-gap: 8px; padding: 9px 12px; }
  .brand { order: 1; }
  .save-state { display: none; }
  /* 手机上：第一行标题，第二行视图切换独占一行，第三行功能按钮可左右滑动 */
  .view-switch { order: 2; width: 100%; }
  .view-tab { flex: 1; }
  .top-actions {
    order: 3;
    width: 100%;
    margin-left: 0;
    padding-left: 0;
    border-left: 0;
    flex-wrap: nowrap;
    justify-content: flex-start;
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }
  .top-actions::-webkit-scrollbar { display: none; }
  .action-group { flex: 0 0 auto; }
  .action-group + .action-group { padding-left: 8px; }
}

@media (max-width: 520px) {
  .top-actions .btn { padding: 0 9px; height: 32px; font-size: 12.5px; }
  .action-group { gap: 6px; }
  .save-state { min-width: 0; }
}

/* ==================== 移动端优化 ==================== */

/* 宽表格的滑动提示：桌面隐藏，手机显示 */
.scroll-hint { display: none; }

@media (max-width: 860px) {
  .topbar {
    position: static;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    padding-top: calc(8px + env(safe-area-inset-top));
  }
  .brand { order: 1; gap: 8px; }
  .logo { width: 30px; height: 30px; flex: 0 0 30px; border-radius: 8px; font-size: 15px; }
  .brand-text h1 { font-size: 14.5px; letter-spacing: 0; }
  .brand-text p { display: none; }
  .save-state { display: none; }

  /* 视图切换和标题同一行，靠右 */
  .view-switch { order: 2; width: auto; margin-left: auto; padding: 2px; gap: 1px; }
  .view-tab { flex: 0 0 auto; height: 34px; padding: 0 10px; font-size: 12.5px; }

  /* 功能按钮单独一行，放不下可左右滑 */
  .top-actions {
    order: 3;
    width: 100%;
    margin-left: 0;
    padding-left: 0;
    border-left: 0;
    gap: 6px;
    flex-wrap: nowrap;
    justify-content: flex-start;
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }
  .top-actions::-webkit-scrollbar { display: none; }
  .top-actions .btn { height: 40px; padding: 0 13px; font-size: 13px; flex: 0 0 auto; }
  .action-group { flex: 0 0 auto; gap: 6px; }
  .action-group + .action-group { padding-left: 8px; }

  main { padding: 10px 10px calc(60px + env(safe-area-inset-bottom)); }

  /* 手指好按：导航按钮放大到 40px */
  .nav-btn { width: 40px; height: 40px; font-size: 18px; }
  .date-nav .btn, .date-nav .mini { height: 40px; }
  .sheet-tools .mini { height: 34px; font-size: 12.5px; }

  .scroll-hint {
    display: block;
    padding: 8px 12px;
    font-size: 11.5px;
    color: #94a3b8;
    background: #fcfdff;
    border-bottom: 1px solid var(--line);
  }
}

/* 手机上弹窗改成从底部升起的抽屉，拇指够得着 */
@media (max-width: 720px) {
  .modal { padding: 0; align-items: flex-end; }
  .modal-box {
    width: 100%;
    max-width: 100%;
    max-height: 94vh;
    border-radius: 16px 16px 0 0;
  }
  .modal-head { padding: 12px 14px; }
  .modal-head h3 { font-size: 15px; }
  .modal-body { padding: 12px; }
  .modal-foot {
    padding: 10px 12px calc(12px + env(safe-area-inset-bottom));
    gap: 8px;
  }
  .modal-foot .btn { flex: 1 1 0; height: 44px; font-size: 14px; justify-content: center; }
  .modal .icon-btn { width: 40px; height: 40px; font-size: 20px; }
  .toast { bottom: calc(20px + env(safe-area-inset-bottom)); }
}

/* 手机上标题过长时截断，避免把顶栏挤成三行 */
@media (max-width: 520px) {
  .brand { min-width: 0; flex: 0 1 auto; overflow: hidden; }
  .brand-text { min-width: 0; overflow: hidden; }
  .brand-text h1 { overflow: hidden; text-overflow: ellipsis; max-width: 100%; font-size: 14px; }
  .h1-x { display: none; }
  .view-tab { padding: 0 9px; font-size: 12.5px; }
}

/* 每日明细页：手机上冻结「产品编号」列（index.html 只加载本文件） */
@media (max-width: 860px) {
  #grid thead th:nth-child(3),
  #grid tbody td:nth-child(3) {
    position: sticky;
    left: 0;
    z-index: 2;
    background: #fff;
    box-shadow: 4px 0 6px -4px rgba(15, 23, 42, .22);
  }
  #grid thead th:nth-child(3) { z-index: 4; background: #f8fafc; width: 118px !important; }
  #grid .cell { font-size: 16px; }
}
