:root {
  --bg: #f2f4fa;
  --card: #ffffff;
  --ink: #1c2333;
  --ink-2: #5a6478;
  --line: #e3e7f0;
  --primary: #2f5cff;
  --primary-soft: #e8edff;
  --accent: #ff7a1a;
  --accent-soft: #fff1e5;
  --good: #0e9e5c;
  --good-soft: #e2f7ec;
  --bad: #df4040;
  --bad-soft: #fdeaea;
  --warn: #b7791f;
  --warn-soft: #fdf3e0;
  --radius: 14px;
  --nav-h: 62px;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: "PingFang TC", "Noto Sans TC", system-ui, -apple-system, sans-serif;
  background: var(--bg); color: var(--ink);
  font-size: 16px; line-height: 1.55;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 16px);
}
.hidden { display: none !important; }

/* ---------- 登入 ---------- */
#login {
  position: fixed; inset: 0; z-index: 100;
  background: linear-gradient(150deg, #1d2b64, #2f5cff);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.login-box {
  background: #fff; border-radius: 18px; padding: 28px 22px;
  width: 100%; max-width: 360px; text-align: center;
}
.login-box .logo { font-size: 44px; }
.login-box h1 { font-size: 19px; margin: 6px 0 4px; }
.login-box p { font-size: 13px; color: var(--ink-2); margin-bottom: 18px; }
.login-err { color: var(--bad); font-size: 13px; margin-top: 10px; min-height: 19px; }

/* ---------- 頁首 ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: linear-gradient(135deg, #1d2b64, #2f5cff);
  color: #fff; padding: 14px 16px;
  display: flex; align-items: center; gap: 10px;
}
.topbar .logo { font-size: 22px; }
.topbar h1 { font-size: 17px; font-weight: 700; letter-spacing: .5px; flex: 1; }
.topbar .updated { font-size: 11px; opacity: .75; display: block; font-weight: 400; }
.icon-btn {
  background: rgba(255,255,255,.16); color: #fff; border: none;
  border-radius: 10px; padding: 8px 12px; font-size: 14px; cursor: pointer; font-family: inherit;
}
.icon-btn:active { background: rgba(255,255,255,.3); }

/* ---------- 版面 ---------- */
main { max-width: 900px; margin: 0 auto; padding: 14px 12px 24px; }
.view { display: none; }
.view.active { display: block; animation: fadein .18s ease; }
@keyframes fadein { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; } }
.card {
  background: var(--card); border-radius: var(--radius); border: 1px solid var(--line);
  padding: 14px; margin-bottom: 12px; box-shadow: 0 1px 3px rgba(28,35,51,.05);
}
.card h2 { font-size: 15px; margin-bottom: 10px; }
.muted { color: var(--ink-2); font-size: 13px; }
.empty { text-align: center; padding: 34px 10px; color: var(--ink-2); }
.empty .big { font-size: 34px; display: block; margin-bottom: 8px; }
.section-title { font-size: 13px; color: var(--ink-2); margin: 16px 4px 8px; font-weight: 600; }

/* ---------- 導覽 ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex; background: #fff; border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
}
.tabbar button {
  flex: 1; border: none; background: none; cursor: pointer;
  font-size: 10.5px; color: var(--ink-2); font-family: inherit; position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
}
.tabbar button .ico { font-size: 20px; }
.tabbar button.on { color: var(--primary); font-weight: 700; }
.tabbar button .badge { position: absolute; top: 4px; right: 14%; }

/* ---------- 表單 ---------- */
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 13px; color: var(--ink-2); margin-bottom: 5px; font-weight: 500; }
.field input, .field select {
  width: 100%; padding: 11px 12px; font-size: 16px;
  border: 1.5px solid var(--line); border-radius: 10px;
  background: #fff; color: var(--ink); font-family: inherit;
}
.field input:focus, .field select:focus { outline: none; border-color: var(--primary); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.hint { font-size: 12px; color: var(--ink-2); margin-top: 4px; }
.hint.warn { color: var(--accent); font-weight: 600; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: none; border-radius: 12px; cursor: pointer; font-family: inherit;
  font-size: 16px; font-weight: 700; padding: 13px 18px; transition: transform .05s ease;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; width: 100%; }
.btn-ghost { background: var(--primary-soft); color: var(--primary); }
.btn-danger-ghost { background: var(--bad-soft); color: var(--bad); }
.btn-good { background: var(--good); color: #fff; }
.btn-sm { font-size: 13px; padding: 8px 12px; border-radius: 9px; }

/* ---------- 徽章 ---------- */
.badge { display: inline-block; font-size: 11.5px; font-weight: 700; border-radius: 999px; padding: 2.5px 9px; }
.badge.blue { background: var(--primary-soft); color: var(--primary); }
.badge.orange { background: var(--accent-soft); color: var(--accent); }
.badge.green { background: var(--good-soft); color: var(--good); }
.badge.red { background: var(--bad-soft); color: var(--bad); }
.badge.grey { background: #eef0f5; color: var(--ink-2); }
.money { font-variant-numeric: tabular-nums; }
.profit-pos { color: var(--good); font-weight: 700; }
.profit-neg { color: var(--bad); font-weight: 700; }

/* ---------- 儀表板 ---------- */
.hero {
  background: linear-gradient(140deg, #1d2b64, #2f5cff); color: #fff;
  border-radius: var(--radius); padding: 18px; margin-bottom: 12px;
}
.hero .lbl { font-size: 12.5px; opacity: .8; }
.hero .big { font-size: 34px; font-weight: 900; line-height: 1.15; font-variant-numeric: tabular-nums; }
.hero .sub { font-size: 12.5px; opacity: .85; margin-top: 6px; }
.hero.pos .big { color: #7dffc0; }
.hero.neg .big { color: #ffc9c9; }
.kpi-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.kpi {
  background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px;
}
.kpi .k { font-size: 11.5px; color: var(--ink-2); display: flex; align-items: center; gap: 4px; }
.kpi .v { font-size: 19px; font-weight: 800; font-variant-numeric: tabular-nums; margin-top: 2px; }
.kpi .s { font-size: 11px; color: var(--ink-2); }
.kpi.pending { border-color: #ffd9b3; background: var(--accent-soft); }
.kpi.pending .v { color: var(--accent); }
.bar { height: 8px; border-radius: 999px; background: var(--line); overflow: hidden; margin: 8px 0 4px; }
.bar span { display: block; height: 100%; background: var(--good); }
.stat-row { display: flex; justify-content: space-between; font-size: 14px; padding: 8px 0; border-bottom: 1px dashed var(--line); }
.stat-row:last-child { border-bottom: none; }
.stat-row b { font-variant-numeric: tabular-nums; }

/* ---------- 匯入 ---------- */
.drop {
  border: 2px dashed var(--line); border-radius: var(--radius); background: #fff;
  padding: 28px 16px; text-align: center; cursor: pointer;
}
.drop.over { border-color: var(--primary); background: var(--primary-soft); }
.drop .big { font-size: 38px; display: block; margin-bottom: 6px; }
.drop .t { font-weight: 700; }
.drop .s { font-size: 12.5px; color: var(--ink-2); margin-top: 4px; }
.warn-item {
  border-radius: 10px; padding: 9px 11px; font-size: 13px; margin-bottom: 7px; line-height: 1.55;
}
.warn-item.error { background: var(--bad-soft); color: #a12b2b; }
.warn-item.warn { background: var(--warn-soft); color: #8a5a1f; }
.warn-item.info { background: #eef1f7; color: var(--ink-2); }
.plan-row {
  display: flex; align-items: center; gap: 10px; padding: 9px 0;
  border-bottom: 1px solid var(--line); font-size: 14px;
}
.plan-row:last-child { border-bottom: none; }
.plan-row .n { flex: 1; font-weight: 700; }
.plan-row .d { font-size: 12px; color: var(--ink-2); font-weight: 400; display: block; }

/* ---------- 批次（到貨勾選）---------- */
.batch-card { display: flex; align-items: flex-start; gap: 12px; }
.check {
  width: 30px; height: 30px; border-radius: 9px; border: 2px solid var(--line);
  background: #fff; cursor: pointer; flex-shrink: 0; font-size: 17px; line-height: 1;
  display: flex; align-items: center; justify-content: center; color: transparent; padding: 0;
}
.check.on { background: var(--good); border-color: var(--good); color: #fff; }
.batch-info { flex: 1; min-width: 0; }
.batch-info .t { font-weight: 800; font-size: 15px; }
.batch-info .s { font-size: 12.5px; color: var(--ink-2); }
.batch-amt { text-align: right; font-variant-numeric: tabular-nums; }
.batch-amt .a { font-weight: 800; font-size: 16px; }
.batch-items { margin-top: 10px; padding-top: 8px; border-top: 1px dashed var(--line); font-size: 12.5px; color: var(--ink-2); }
.btn-del {
  border: none; background: none; cursor: pointer; font-family: inherit;
  font-size: 15px; padding: 6px 4px; margin-top: 4px; opacity: .45; line-height: 1;
}
.btn-del:hover, .btn-del:active { opacity: 1; }

/* ---------- 庫存 ---------- */
.stock-grid { display: grid; grid-template-columns: 1fr; gap: 10px; }
.stock-card { cursor: pointer; }
.stock-head { display: flex; align-items: center; gap: 10px; }
.stock-code {
  background: var(--primary-soft); color: var(--primary); font-weight: 800;
  border-radius: 10px; padding: 7px 10px; font-size: 14px; min-width: 58px; text-align: center;
}
.stock-name { flex: 1; font-weight: 700; font-size: 15px; min-width: 0; }
.stock-name .sub { display: block; font-size: 12px; color: var(--ink-2); font-weight: 400; }
.stock-avail { text-align: right; }
.stock-avail .n { font-size: 21px; font-weight: 800; display: block; line-height: 1.1; }
.stock-avail .n.zero { color: var(--bad); }
.stock-avail .lbl { font-size: 11px; color: var(--ink-2); }
.stock-detail { display: none; margin-top: 12px; border-top: 1px dashed var(--line); padding-top: 10px; }
.stock-card.open .stock-detail { display: block; }
.stock-stats { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.batch-line { font-size: 12.5px; color: var(--ink-2); }
.stock-actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.direct-box {
  margin-top: 10px; padding: 10px; border-radius: 10px;
  background: var(--good-soft); border: 1px solid #bfe8d3;
}
.direct-box label { display: block; font-size: 12.5px; font-weight: 600; color: #0b7a48; margin-bottom: 6px; }
.direct-row { display: flex; gap: 8px; }
.direct-row input {
  flex: 1; min-width: 0; padding: 9px 11px; font-size: 16px;
  border: 1.5px solid #bfe8d3; border-radius: 9px; background: #fff;
  color: var(--ink); font-family: inherit;
}
.direct-row input:focus { outline: none; border-color: var(--good); }
.direct-box .hint { margin-top: 6px; }
.direct-row input[type="text"] { font-size: 15px; }

.loss-line {
  margin-top: 10px; padding: 10px 12px; border-radius: 10px;
  background: var(--bad-soft); border: 1px solid #f6c9c9;
}
.loss-line .ll-head { font-size: 12.5px; font-weight: 700; color: var(--bad); }
.loss-line .ll-big { font-size: 16px; font-weight: 800; color: #a12b2b; margin: 3px 0 4px; }
.loss-line .ll-sub { font-size: 12.5px; color: #8a4444; line-height: 1.6; }

/* 套刮欄位：寫成一句話避免誤解成折扣金額 */
.bundle-row { display: flex; align-items: center; gap: 6px; }
.bundle-row input {
  width: 100%; min-width: 0; flex: 1; padding: 11px 8px; font-size: 16px; text-align: center;
  border: 1.5px solid var(--line); border-radius: 10px; background: #fff;
  color: var(--ink); font-family: inherit;
}
.bundle-row input:focus { outline: none; border-color: var(--primary); }
.bundle-sep { font-size: 12.5px; color: var(--ink-2); white-space: nowrap; flex-shrink: 0; }

/* ---------- 比價方案卡 ---------- */
.plan-card.plan-best { border-color: #ffd9b3; background: linear-gradient(160deg, #fffdf9, #fff7ef); }
.plan-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 10px 0; }
.plan-cell {
  background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: 9px 10px;
}
.plan-cell .k { font-size: 11.5px; color: var(--ink-2); }
.plan-cell .v { font-size: 19px; font-weight: 800; font-variant-numeric: tabular-nums; line-height: 1.25; }
.plan-cell .s { font-size: 11px; color: var(--ink-2); }

.tradeoff { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.tradeoff th {
  font-size: 11.5px; color: var(--ink-2); font-weight: 600;
  text-align: right; padding: 4px 6px; border-bottom: 1px solid var(--line);
}
.tradeoff th:first-child { text-align: left; }
.tradeoff td {
  padding: 9px 6px; text-align: right; border-bottom: 1px solid var(--line);
  font-variant-numeric: tabular-nums;
}
.tradeoff td:first-child { text-align: left; font-weight: 600; }
.tradeoff td .sub { display: block; font-size: 11px; color: var(--ink-2); font-weight: 400; }
.tradeoff tbody tr { cursor: pointer; }
.tradeoff tbody tr:active { background: var(--primary-soft); }
.tradeoff tr.row-best td { background: #fff6ec; }
.tradeoff tr.row-picked td { box-shadow: inset 0 0 0 1.5px var(--primary); }
.tradeoff tr.row-picked.row-best td { box-shadow: inset 0 0 0 1.5px var(--accent); }

.value-box {
  margin-top: 10px; padding: 10px 12px; border-radius: 10px;
  background: #fff; border: 1px solid #ffd9b3;
}
.vb-row {
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
  font-size: 13.5px; padding: 4px 0;
}
.vb-row b { font-variant-numeric: tabular-nums; white-space: nowrap; }
.vb-note {
  font-size: 12px; color: var(--ink-2); line-height: 1.6;
  margin-top: 6px; padding-top: 6px; border-top: 1px dashed var(--line);
}

.pricing-alert {
  margin-top: 8px; padding: 10px 12px; border-radius: 10px; font-size: 13px; line-height: 1.6;
  background: var(--bad-soft); border: 1px solid #f6c9c9; color: #a12b2b;
}

/* 記憶點 */
.snap-row {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 11px 0; border-bottom: 1px solid var(--line);
}
.snap-row:last-child { border-bottom: none; }
.snap-info { flex: 1; min-width: 0; }
.snap-info .t { font-weight: 700; font-size: 14.5px; }
.snap-info .s { font-size: 12px; color: var(--ink-2); }
.snap-actions { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }

/* ---------- 建議 ---------- */
.suggest-panel { background: linear-gradient(160deg, #fffdf9, #fff7ef); border-color: #ffd9b3; }
.suggest-best { display: flex; align-items: center; gap: 14px; margin: 6px 0 10px; }
.suggest-best .num { font-size: 44px; font-weight: 900; color: var(--accent); line-height: 1; font-variant-numeric: tabular-nums; }
.suggest-best .unit { font-size: 15px; font-weight: 700; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  border: 1.5px solid var(--line); background: #fff; border-radius: 999px;
  padding: 7px 14px; font-size: 14px; font-weight: 700; cursor: pointer; font-family: inherit; color: var(--ink);
}
.chip.on { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.calc-table { width: 100%; border-collapse: collapse; font-size: 14px; margin-top: 4px; }
.calc-table td { padding: 7px 2px; border-bottom: 1px dashed var(--line); }
.calc-table tr:last-child td { border-bottom: none; }
.calc-table td:last-child { text-align: right; font-weight: 700; }
.psy-note { margin-top: 10px; font-size: 12.5px; color: #8a5a1f; background: #fff3e2; border-radius: 10px; padding: 9px 11px; line-height: 1.6; }

/* ---------- 進行中 ---------- */
.round-head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.round-title { flex: 1; font-size: 16px; font-weight: 800; min-width: 0; }
.round-title .sub { display: block; font-size: 12px; color: var(--ink-2); font-weight: 400; }
.odds-live {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: var(--primary-soft); border-radius: 12px; padding: 10px 12px; margin: 10px 0;
}
.odds-live .lbl { font-size: 12.5px; color: var(--ink-2); }
.odds-live .val { font-size: 20px; font-weight: 900; color: var(--primary); }
.sold-ctrl { display: flex; align-items: center; gap: 12px; margin: 10px 0; }
.sold-ctrl button {
  width: 44px; height: 44px; border-radius: 12px; border: 1.5px solid var(--line);
  background: #fff; font-size: 22px; font-weight: 800; cursor: pointer; color: var(--ink); font-family: inherit;
}
.sold-ctrl .sold-num { font-size: 19px; font-weight: 800; min-width: 88px; text-align: center; }
.sold-ctrl .sold-num small { display: block; font-size: 11px; color: var(--ink-2); font-weight: 400; }
.round-actions { display: flex; gap: 8px; margin-top: 6px; }
.round-actions .btn { flex: 1; }

/* ---------- 紀錄 ---------- */
.hist-line { display: flex; align-items: center; gap: 10px; padding: 11px 2px; border-bottom: 1px solid var(--line); }
.hist-line:last-child { border-bottom: none; }
.hist-main { flex: 1; min-width: 0; }
.hist-main .t { font-weight: 700; font-size: 14.5px; }
.hist-main .s { font-size: 12px; color: var(--ink-2); }
.hist-amt { text-align: right; font-size: 14.5px; font-variant-numeric: tabular-nums; }
.hist-amt .s { font-size: 11.5px; color: var(--ink-2); font-weight: 400; }

/* ---------- 對話框 ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(20,26,40,.5); z-index: 50;
  display: none; align-items: flex-end; justify-content: center;
}
.overlay.show { display: flex; }
.sheet {
  background: #fff; width: 100%; max-width: 480px;
  border-radius: 18px 18px 0 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  animation: slideup .2s ease; max-height: 88vh; overflow-y: auto;
}
@keyframes slideup { from { transform: translateY(40px); opacity: .5; } to { transform: none; opacity: 1; } }
.sheet h3 { font-size: 16px; margin-bottom: 4px; }
.sheet .sheet-sub { font-size: 13px; color: var(--ink-2); margin-bottom: 12px; }
.settle-preview {
  display: flex; justify-content: space-between; align-items: center;
  background: var(--bg); border-radius: 10px; padding: 10px 12px; margin: 10px 0 14px; font-size: 14px;
}
.settle-preview .p { font-size: 19px; font-variant-numeric: tabular-nums; }
.sheet-actions { display: flex; gap: 10px; }
.sheet-actions .btn { flex: 1; }

#toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 18px);
  background: #21283a; color: #fff; font-size: 14px; border-radius: 12px; padding: 11px 18px;
  z-index: 60; opacity: 0; pointer-events: none; transition: opacity .2s ease;
  max-width: 88vw; text-align: center; line-height: 1.5;
}
#toast.show { opacity: 1; }
#toast.err { background: var(--bad); }
#loading {
  position: fixed; inset: 0; z-index: 70; display: none;
  align-items: center; justify-content: center; background: rgba(242,244,250,.6);
}
#loading.show { display: flex; }
.spinner {
  width: 42px; height: 42px; border-radius: 50%;
  border: 4px solid var(--primary-soft); border-top-color: var(--primary);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- 桌機 ---------- */
@media (min-width: 768px) {
  body { padding-bottom: 24px; }
  .tabbar {
    position: sticky; top: 0; bottom: auto; height: auto;
    max-width: 900px; margin: 0 auto; border: 1px solid var(--line);
    border-radius: 0 0 14px 14px; padding: 4px; gap: 4px;
  }
  .tabbar button { flex-direction: row; font-size: 13.5px; padding: 10px; border-radius: 10px; }
  .tabbar button.on { background: var(--primary-soft); }
  .tabbar button .ico { font-size: 16px; }
  .tabbar button .badge { position: static; }
  .topbar { padding: 16px 24px; }
  .topbar h1 { font-size: 19px; }
  .stock-grid { grid-template-columns: 1fr 1fr; }
  .kpi-grid { grid-template-columns: repeat(4, 1fr); }
  .overlay { align-items: center; }
  .sheet { border-radius: 18px; }
  #toast { bottom: 30px; }
}
