/* ===== 投料计算器 全局样式（蓝白配色，自适应） ===== */
:root {
  --blue-900: #0d47a1;
  --blue-700: #1565c0;
  --blue-500: #2196f3;
  --blue-100: #e3f2fd;
  --blue-50:  #f2f8fe;
  --white: #ffffff;
  --text: #1d2b3a;
  --text-sub: #5a7188;
  --line: #cfe3f5;
  --danger: #e53935;
  --success: #2e7d32;
  --radius: 10px;
}

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

/* 确保 hidden 属性优先于任何 display 样式（修复弹窗无法隐藏问题） */
[hidden] { display: none !important; }

html { font-size: 16px; }
@media (max-width: 360px) { html { font-size: 14px; } }

body {
  font-family: "PingFang SC", "Microsoft YaHei", "Helvetica Neue", Arial, sans-serif;
  background: var(--blue-50);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

/* 主页面：整屏不滚动，仅中间表格区滚动 */
html, body { height: 100%; }
body:not(.login-body) { overflow: hidden; }

/* ---------- 通用按钮 ---------- */
.btn {
  display: inline-block;
  border: none;
  border-radius: var(--radius);
  font-size: 0.95rem;
  padding: 0.7rem 0.5rem;
  cursor: pointer;
  transition: opacity .15s;
}
.btn:active { opacity: .8; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: linear-gradient(135deg, var(--blue-500), var(--blue-700)); color: var(--white); }
.btn-outline { background: var(--white); color: var(--blue-700); border: 1px solid var(--blue-500); }
.btn-block { width: 100%; }

.btn-mini {
  border: 1px solid var(--line);
  background: var(--white);
  border-radius: 6px;
  font-size: 0.8rem;
  padding: 0.3rem 0.7rem;
  cursor: pointer;
}
.btn-mini.btn-danger { color: var(--danger); border-color: #f5c2c1; }

/* ---------- 登录页 ---------- */
.login-body {
  min-height: 100vh;
  background: linear-gradient(160deg, var(--blue-700) 0%, var(--blue-500) 45%, var(--blue-100) 100%);
  display: flex;
  flex-direction: column;
  padding: 1rem;
}
.login-wrap { flex: 1; width: 100%; max-width: 400px; margin: 0 auto; display: flex; align-items: center; justify-content: center; }
.login-footer {
  flex: none;
  text-align: center;
  padding: .9rem 1rem;
  font-size: .72rem;
  color: rgba(255, 255, 255, .9);
}
.login-card {
  background: var(--white);
  border-radius: 16px;
  padding: 2rem 1.5rem 1.5rem;
  box-shadow: 0 12px 40px rgba(13, 71, 161, .25);
}
.login-logo { text-align: center; margin-bottom: .5rem; }
.login-title { text-align: center; font-size: 1.35rem; color: var(--blue-900); }
.login-sub { text-align: center; font-size: .8rem; color: var(--text-sub); margin: .4rem 0 1.4rem; }
.field { margin-bottom: 1rem; }
.field label { display: block; font-size: .85rem; color: var(--text-sub); margin-bottom: .35rem; }
.field input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: .75rem .9rem;
  font-size: 1rem;
  background: var(--blue-50);
  outline: none;
}
.field input:focus { border-color: var(--blue-500); background: var(--white); }
.login-error { color: var(--danger); font-size: .82rem; min-height: 1.1rem; margin-bottom: .5rem; }
.login-divider {
  display: flex; align-items: center; gap: .6rem;
  color: var(--text-sub); font-size: .78rem; margin: 1.1rem 0;
}
.login-divider::before, .login-divider::after {
  content: ""; flex: 1; height: 1px; background: var(--line);
}
.login-tip { text-align: center; font-size: .75rem; color: var(--text-sub); margin-top: .6rem; }

/* ---------- 主页面：固定表头 + 滚动表格 + 固定底栏 ---------- */
.page {
  max-width: 720px;
  margin: 0 auto;
  height: 100vh;
  height: 100dvh;
  /* --app-h 由 JS 依据 visualViewport 实时下发：软键盘弹出时整页收缩到可视区内 */
  height: var(--app-h, 100dvh);
  display: flex;
  flex-direction: column;
  background: var(--blue-50);
  overflow: hidden;
}
.app-header {
  flex: none;
  padding: .7rem .8rem 0;
  background: var(--blue-50);
  box-shadow: 0 2px 6px rgba(13, 71, 161, .06);
  z-index: 3;
}
.scroll-area {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  -ms-overflow-style: none;
  scrollbar-color: transparent transparent;
  padding: 0 .8rem .6rem;
  background: var(--blue-50);
  /* 键盘弹出后 scrollIntoView 时上下各留出余量，避免贴边 */
  scroll-padding-top: 2.2rem;
  scroll-padding-bottom: 2.2rem;
  overscroll-behavior: contain;
}
.scroll-area::-webkit-scrollbar { width: 0; height: 0; display: none; }

/* 顶栏 */
.topbar {
  display: flex; justify-content: space-between; align-items: center;
  background: linear-gradient(135deg, var(--blue-700), var(--blue-500));
  color: var(--white);
  border-radius: var(--radius);
  padding: .7rem .9rem;
}
.topbar-date { font-size: .92rem; font-weight: 600; }
.topbar-time { font-size: 1.05rem; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: .5px; }

/* 状态栏 */
.status-bar {
  display: flex; align-items: center; gap: .45rem;
  background: var(--blue-100);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: .55rem .8rem;
  margin-top: .6rem;
  font-size: .85rem;
  color: var(--blue-900);
  flex-wrap: wrap;
}
.status-dot { width: .55rem; height: .55rem; border-radius: 50%; background: var(--success); flex: none; }
.status-bar.offline .status-dot { background: #fb8c00; }
.status-action { margin-left: auto; color: var(--blue-700); font-size: .8rem; text-decoration: none; white-space: nowrap; }

/* 两列输入行 */
.row2 { display: flex; gap: .6rem; margin-top: .6rem; }
.cell { flex: 1; background: var(--white); border: 1px solid var(--line); border-radius: var(--radius); padding: .55rem .7rem; }
.cell label { display: block; font-size: .75rem; color: var(--text-sub); margin-bottom: .3rem; }
.cell input {
  width: 100%; border: none; outline: none;
  font-size: 1rem; font-weight: 600; color: var(--text);
  background: transparent;
}
.cell input[readonly] { color: var(--blue-700); }
.cell select {
  width: 100%; border: none; outline: none; background: transparent;
  font-size: 1rem; font-weight: 600; color: var(--text);
  -webkit-appearance: none; appearance: none;
}
.cell { position: relative; }
.cell select { padding-right: 1.4rem; }
.cell::after {
  content: "▾"; position: absolute; right: .7rem; top: 55%; transform: translateY(-50%);
  color: var(--blue-500); font-size: .8rem; pointer-events: none;
}
.barrel-hint { font-size: 1rem; font-weight: 700; color: var(--blue-700); min-height: 1.35rem; }
@media (max-width: 420px) {
  .cell label { font-size: .7rem; }
  .cell input, .barrel-hint { font-size: .95rem; }
}

/* 计算器主体 */
.calc-card {
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin-top: .6rem;
  overflow: hidden;
}
.calc-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: .55rem .8rem;
  background: var(--blue-100);
  border: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  border-radius: var(--radius) var(--radius) 0 0;
  margin-top: .6rem;
}
.calc-title { font-size: .85rem; font-weight: 600; color: var(--blue-900); }

/* 自动保存状态指示 */
.auto-state { font-size: .72rem; font-weight: 600; white-space: nowrap; }
.auto-state.pending { color: var(--text-sub); }
.auto-state.saving { color: var(--blue-700); }
.auto-state.saved { color: var(--success); }
.auto-state.fail { color: var(--danger); }

.table-wrap {
  width: 100%;
  background: var(--white);
  border: 1px solid var(--line);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  overflow: visible;
}
.calc-table { width: 100%; border-collapse: collapse; min-width: 300px; }
.calc-table th, .calc-table td {
  border-bottom: 1px solid var(--line);
  text-align: center;
  padding: .4rem .3rem;
  font-size: .88rem;
}
.calc-table th { position: sticky; top: 0; z-index: 2; background: var(--blue-50); color: var(--text-sub); font-weight: 600; font-size: .78rem; }
.col-idx { width: 3rem; }
.col-del { width: 2.6rem; }

/* 桶号：蓝色圆点 + 白色数字 */
.idx-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.5rem;
  height: 1.5rem;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--blue-500), var(--blue-700));
  color: var(--white);
  font-size: .78rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* 行尾删除小图标 */
.row-del {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.9rem;
  height: 1.9rem;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--danger);
  cursor: pointer;
}
.row-del:active { background: #fdecea; }
.calc-table input[type="number"] {
  width: 100%;
  max-width: 7rem;
  border: 1px solid transparent;
  border-radius: 6px;
  background: var(--blue-50);
  text-align: center;
  font-size: .95rem;
  padding: .45rem .2rem;
  outline: none;
  -moz-appearance: textfield;
}
.calc-table input[type="number"]::-webkit-outer-spin-button,
.calc-table input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; }
.calc-table input[type="number"]:focus { border-color: var(--blue-500); background: var(--white); }
.calc-table td.net { font-weight: 700; color: var(--blue-700); font-variant-numeric: tabular-nums; }

/* 自动反推得到的皮重高亮提示 */
.calc-table input.tare-auto {
  background: #e8f5e9;
  border-color: #a5d6a7;
  color: var(--success);
  font-weight: 700;
}

/* 汇总（位于投料明细下方、底部按钮上方的固定信息卡） */
.summary {
  flex: none;
  margin: 0 .8rem .5rem;
  padding: .6rem .8rem;
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 -2px 8px rgba(13, 71, 161, .06);
  z-index: 2;
}
.summary-line {
  display: flex; justify-content: space-between; align-items: baseline; flex-wrap: wrap; gap: .3rem;
  font-size: .88rem; color: var(--text);
}
.summary-line .num { color: var(--blue-700); font-variant-numeric: tabular-nums; }

/* 本次投料总净重 着重显示 */
.total-em { font-size: 1.02rem; font-weight: 700; color: var(--blue-900); }
.total-em .num { font-size: 1.35rem; color: var(--blue-700); }
.target-sub { color: var(--text-sub); }
.summary-tip { margin-top: .35rem; font-size: .85rem; font-weight: 600; min-height: 1.2rem; }
.summary-tip.warn { color: var(--danger); }
.summary-tip.done { color: var(--success); }
.summary-tip.over { color: #ef6c00; }

/* 底部按钮（固定在页面底部，不随表格滚动） */
.footer-btns {
  position: static;
  left: auto; right: auto; bottom: auto;
  flex: none;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: .5rem;
  padding: .6rem .8rem calc(.6rem + env(safe-area-inset-bottom));
  background: rgba(255, 255, 255, .96);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(4px);
}
.footer-btns .btn { font-size: .88rem; padding: .68rem .2rem; white-space: nowrap; }
@media (max-width: 380px) { .footer-btns .btn { font-size: .8rem; } }

/* 历史记录弹窗 */
.modal-mask {
  position: fixed; inset: 0;
  background: rgba(13, 43, 76, .45);
  display: flex; align-items: flex-end; justify-content: center;
  z-index: 50;
}
.modal {
  background: var(--white);
  width: 100%; max-width: 720px;
  max-height: 78vh;
  border-radius: 16px 16px 0 0;
  display: flex; flex-direction: column;
}
@media (min-width: 720px) {
  .modal-mask { align-items: center; }
  .modal { border-radius: 16px; }
}
.modal-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: .8rem 1rem;
  border-bottom: 1px solid var(--line);
  font-weight: 700; color: var(--blue-900);
}
.modal-close { border: none; background: none; font-size: 1.5rem; line-height: 1; color: var(--text-sub); cursor: pointer; }
.modal-body { overflow-y: auto; padding: .6rem .8rem 1.2rem; }
.empty-tip { text-align: center; color: var(--text-sub); font-size: .85rem; padding: 2rem 0; }

.history-item {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: .6rem .8rem;
  margin-bottom: .55rem;
  background: var(--blue-50);
}
.history-item .hi-top { display: flex; justify-content: space-between; font-weight: 600; font-size: .9rem; }
.history-item .hi-mid { font-size: .78rem; color: var(--text-sub); margin: .25rem 0 .45rem; }
.history-item .hi-btns { display: flex; gap: .5rem; }

/* ---------- 软键盘弹出时的紧凑布局（避免键盘遮挡输入框） ---------- */
/* 键盘弹出：隐藏纯信息区（顶栏时钟、状态栏），把空间让给输入区 */
body.kb-open .topbar,
body.kb-open .status-bar { display: none; }

body.kb-open .app-header { padding-top: .4rem; }

/* 正在录入表格（毛重/皮重）时：反应釜/物料/目标等表头输入行一并折叠，
   关键数据仍可在下方汇总条中看到 */
body.kb-table .row2 { display: none; }
body.kb-table .calc-head { margin-top: 0; }

/* 键盘弹出时压缩汇总条与底部按钮，进一步腾出可视高度 */
body.kb-open .summary { margin-bottom: .35rem; padding: .45rem .7rem; }
body.kb-open .summary-tip { margin-top: .2rem; min-height: 0; }
body.kb-open .total-em { font-size: .95rem; }
body.kb-open .total-em .num { font-size: 1.15rem; }
body.kb-open .footer-btns { padding: .4rem .8rem calc(.4rem + env(safe-area-inset-bottom)); }
body.kb-open .footer-btns .btn { padding: .5rem .2rem; font-size: .82rem; }

/* 键盘弹出时轻提示上移，避免压住汇总条 */
body.kb-open .toast { bottom: 5.6rem; }

/* 轻提示 */
.toast {
  position: fixed;
  left: 50%; bottom: 9.2rem;
  transform: translateX(-50%);
  background: rgba(13, 47, 91, .9);
  color: var(--white);
  font-size: .85rem;
  padding: .55rem 1rem;
  border-radius: 999px;
  z-index: 99;
  max-width: 85vw;
  text-align: center;
}
