:root {
  --bw: #0b5cad;
  --bw-dark: #084a8c;
  --bw-light: #e8f1fb;
  --bg: #f4f6f9;
  --card: #ffffff;
  --text: #1b2430;
  --muted: #6b7684;
  --border: #e2e6ec;
  --danger: #d33a2c;
  --danger-bg: #fdecea;
  --ok: #1f9d55;
  --warn: #e08a00;
  --radius: 14px;
  --tap: 46px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
  overscroll-behavior-y: none;
  overflow-x: hidden;
}
#app { min-height: 100%; }

.boot { padding: 40vh 0; text-align: center; color: var(--muted); }

/* ---------- App Shell（authed 版面：flex 直欄，只有中間內容捲動）---------- */
/* 用 flex 版面取代 fixed 底欄，避免 iOS standalone PWA 底部工具列跑版/跳動 */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

.app-header {
  flex: 0 0 auto; z-index: 10;
  background: var(--bw); color: #fff;
  padding: calc(var(--safe-top) + 12px) 16px 12px;
  display: flex; align-items: center; justify-content: space-between;
  box-shadow: 0 2px 6px rgba(0,0,0,.12);
}
.app-header h1 { font-size: 17px; margin: 0; font-weight: 700; }
.app-header .sub { font-size: 12px; opacity: .85; font-weight: 400; }
.app-header .whoami { font-size: 12px; text-align: right; opacity: .9; }

main.content {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px 14px 18px;
  width: 100%;
  max-width: 720px; margin: 0 auto;
}

/* ---------- Bottom Nav（in-flow，靠 flex 固定於底部）---------- */
.tabbar {
  flex: 0 0 auto; z-index: 20;
  background: var(--card);
  border-top: 1px solid var(--border);
  display: flex;
  padding-bottom: env(safe-area-inset-bottom, 0px);
  box-shadow: 0 -1px 6px rgba(0,0,0,.05);
}
.tabbar button {
  flex: 1; border: none; background: none; color: var(--muted);
  min-height: var(--tap); padding: 8px 2px 6px;
  font-size: 11px; display: flex; flex-direction: column; align-items: center; gap: 3px;
  cursor: pointer;
}
.tabbar button .ic { font-size: 20px; line-height: 1; }
.tabbar button.active { color: var(--bw); font-weight: 700; }

/* ---------- Cards ---------- */
.card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px; margin-bottom: 12px;
}
.card h2 { font-size: 15px; margin: 0 0 10px; }
.section-title { font-size: 13px; color: var(--muted); margin: 18px 4px 8px; font-weight: 700; letter-spacing: .3px; }

/* ---------- Forms ---------- */
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 5px; }
input, select, textarea {
  width: 100%; min-height: var(--tap);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; font-size: 16px; background: #fff; color: var(--text);
  font-family: inherit;
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--bw-light); border-color: var(--bw); }

button.btn {
  width: 100%; min-height: var(--tap);
  border: none; border-radius: 12px; font-size: 16px; font-weight: 700;
  background: var(--bw); color: #fff; cursor: pointer;
}
button.btn:active { background: var(--bw-dark); }
button.btn.secondary { background: var(--bw-light); color: var(--bw); }
button.btn.ghost { background: none; color: var(--bw); border: 1px solid var(--bw); }
button.btn.danger { background: var(--danger); }
button.btn:disabled { opacity: .5; }
button.btn.small { width: auto; min-height: 38px; padding: 0 14px; font-size: 14px; }
.row { display: flex; gap: 8px; }
.row > * { flex: 1; }

/* ---------- Stepper ---------- */
.stepper { display: flex; align-items: center; gap: 8px; }
.stepper button {
  width: var(--tap); height: var(--tap); min-width: var(--tap);
  border-radius: 12px; border: 1px solid var(--bw); background: var(--bw-light);
  color: var(--bw); font-size: 24px; font-weight: 700; cursor: pointer;
}
.stepper button:active { background: var(--bw); color: #fff; }
.stepper input { text-align: center; width: 64px; flex: none; font-size: 18px; font-weight: 700; }
/* 只留 +／− 按鈕，隱藏數字輸入框原生的上下箭頭 */
.stepper input[type="number"]::-webkit-outer-spin-button,
.stepper input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.stepper input[type="number"] { -moz-appearance: textfield; appearance: textfield; }

/* ---------- Item rows ---------- */
.item-row {
  display: flex; align-items: center; gap: 10px; padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.item-row:last-child { border-bottom: none; }
.item-row .name { flex: 1; font-size: 14px; }
.item-row .name small { display: block; color: var(--muted); font-size: 12px; }

/* ---------- Stock / alerts ---------- */
.alert-zone { background: var(--danger-bg); border: 1px solid #f3c4bf; border-radius: var(--radius); padding: 12px; margin-bottom: 14px; }
.alert-zone h2 { color: var(--danger); }
.stock-line { display: flex; justify-content: space-between; align-items: center; padding: 9px 0; border-bottom: 1px solid var(--border); }
.stock-line:last-child { border-bottom: none; }
.stock-line .meta { font-size: 13px; }
.stock-line .meta small { color: var(--muted); display: block; font-size: 11px; }
.badge { font-size: 12px; font-weight: 700; padding: 3px 9px; border-radius: 20px; }
.badge.low { background: var(--danger-bg); color: var(--danger); }
.badge.ok { background: #e6f6ec; color: var(--ok); }
.qty-big { font-size: 20px; font-weight: 800; min-width: 44px; text-align: right; }
.qty-big.low { color: var(--danger); }

.pill { display: inline-block; font-size: 11px; padding: 2px 8px; border-radius: 20px; background: var(--bw-light); color: var(--bw); font-weight: 700; }

/* 家族規格明細小標 */
.variant-row { flex: 1 1 100%; display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.variant-chip { font-size: 12px; padding: 3px 9px; border-radius: 8px; background: var(--bg); border: 1px solid var(--border); color: var(--muted); display: inline-flex; gap: 6px; align-items: center; }
.variant-chip b { color: var(--text); font-weight: 700; }
.variant-chip.zero b { color: var(--danger); }
.tx-in { color: var(--ok); font-weight: 700; }
.tx-out { color: var(--danger); font-weight: 700; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.empty { text-align: center; color: var(--muted); padding: 30px 0; }
.list-meta { font-size: 12px; color: var(--muted); }

hr.sep { border: none; border-top: 1px solid var(--border); margin: 14px 0; }

/* ---------- Login ---------- */
.login-wrap { max-width: 420px; margin: 0 auto; padding: 8vh 20px calc(40px + var(--safe-bottom)); }
.brand { text-align: center; margin-bottom: 24px; }
.brand .logo { width: 76px; height: 76px; border-radius: 20px; background: var(--bw); color: #fff; display: inline-flex; align-items: center; justify-content: center; font-size: 30px; font-weight: 800; box-shadow: 0 6px 16px rgba(11,92,173,.3); }
.brand h1 { font-size: 20px; margin: 14px 0 2px; }
.brand p { color: var(--muted); margin: 0; font-size: 13px; }
.link-btn { background: none; border: none; color: var(--bw); font-weight: 700; cursor: pointer; font-size: 14px; padding: 8px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--safe-bottom)); transform: translateX(-50%);
  background: #23303f; color: #fff; padding: 11px 18px; border-radius: 24px;
  font-size: 14px; z-index: 100; max-width: 88%; text-align: center; box-shadow: 0 6px 18px rgba(0,0,0,.25);
}
.toast.err { background: var(--danger); }
.toast.ok { background: var(--ok); }

/* ---------- Filter bar ---------- */
.filter-toggle { display: flex; justify-content: space-between; align-items: center; cursor: pointer; }
details.filter summary { list-style: none; cursor: pointer; color: var(--bw); font-weight: 700; font-size: 14px; padding: 6px 0; }
details.filter summary::-webkit-details-marker { display: none; }

.seg { display: flex; border: 1px solid var(--bw); border-radius: 12px; overflow: hidden; }
.seg button { flex: 1; min-height: var(--tap); border: none; background: #fff; color: var(--bw); font-weight: 700; font-size: 15px; cursor: pointer; }
.seg button.active { background: var(--bw); color: #fff; }

.spinner { text-align: center; padding: 30px; color: var(--muted); }

/* ---------- Icons ---------- */
.icn { display: inline-block; vertical-align: -0.18em; }
.tabbar button .ic { display: flex; }
.tabbar button .ic .icn { vertical-align: 0; }
.inline-ic { display: inline-flex; align-items: center; gap: 8px; }
.menu-ic { width: 40px; height: 40px; border-radius: 11px; background: var(--bw-light); color: var(--bw); display: flex; align-items: center; justify-content: center; flex: none; }
.alert-zone h2 .icn, .card h2 .icn { color: inherit; }
.logo-svg { width: 76px; height: 76px; border-radius: 20px; background: var(--bw); color: #fff; display: inline-flex; align-items: center; justify-content: center; box-shadow: 0 6px 16px rgba(11,92,173,.3); }
.logo-svg .icn { width: 40px; height: 40px; }
