:root {
  --bg: #f4f1ea;
  --surface: #ffffff;
  --ink: #1b2420;
  --muted: #5d6b64;
  --line: #d9d3c6;
  --brand: #1f5b3a;
  --brand-ink: #ffffff;
  --brand-soft: #e3efe6;
  --accent: #c2571a;
  --warn-bg: #fff3d6;
  --warn-ink: #7a4b00;
  --error: #b3261e;
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121714; --surface: #1c231f; --ink: #eef3ef; --muted: #a3b2aa; --line: #2f3a34;
    --brand: #4fae78; --brand-ink: #08130c; --brand-soft: #22382b; --accent: #f08a4b;
    --warn-bg: #3a2e12; --warn-ink: #ffd88a; --error: #ff8a80; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121714; --surface: #1c231f; --ink: #eef3ef; --muted: #a3b2aa; --line: #2f3a34;
  --brand: #4fae78; --brand-ink: #08130c; --brand-soft: #22382b; --accent: #f08a4b;
  --warn-bg: #3a2e12; --warn-ink: #ffd88a; --error: #ff8a80; color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 17px/1.45 -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  padding-bottom: calc(96px + env(safe-area-inset-bottom));
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

.top {
  position: sticky; top: 0; z-index: 5; display: flex; gap: 8px; align-items: stretch;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px; background: var(--brand); color: var(--brand-ink);
}
.market, .today {
  border: 0; background: rgba(255,255,255,.14); color: inherit; border-radius: 10px; padding: 6px 12px; text-align: left;
}
.market { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.market-label { font-size: 12px; opacity: .8; }
#marketName { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.today { font-size: 14px; white-space: nowrap; }

.banner { margin: 10px 16px 0; padding: 10px 12px; border-radius: 10px; background: var(--warn-bg); color: var(--warn-ink); font-size: 15px; }

main { padding: 0 16px; }
.sec { font-size: 15px; color: var(--muted); margin: 18px 0 8px; font-weight: 600; letter-spacing: .02em; }

.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
@media (min-width: 640px) { .grid { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.card {
  position: relative; min-height: 88px; padding: 12px; border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); text-align: left; display: flex; flex-direction: column; justify-content: space-between;
}
.card:active { transform: scale(.98); }
.card-name { font-size: 19px; font-weight: 700; line-height: 1.25; }
.card-price { color: var(--muted); font-size: 15px; }
.card-badge {
  position: absolute; top: 8px; right: 8px; min-width: 26px; height: 26px; border-radius: 13px; padding: 0 7px;
  background: var(--accent); color: #fff; font-size: 14px; font-weight: 700; display: grid; place-items: center;
}

.search {
  width: 100%; padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface);
}
.list { list-style: none; margin: 10px 0 0; padding: 0; border-radius: var(--radius); overflow: hidden; border: 1px solid var(--line); background: var(--surface); }
.list:empty { display: none; }
.list li + li { border-top: 1px solid var(--line); }
.row {
  width: 100%; border: 0; background: none; display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; min-height: 56px; text-align: left;
}
.row-main { flex: 1; min-width: 0; }
.row-sub { display: block; color: var(--muted); font-size: 13px; }
.row-price { font-weight: 600; white-space: nowrap; }
.row-add { width: 34px; height: 34px; border-radius: 17px; background: var(--brand-soft); color: var(--brand); display: grid; place-items: center; font-weight: 700; }

.cartbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); background: var(--bg);
  border-top: 1px solid var(--line);
}
.cart-summary {
  width: 100%; min-height: 60px; border: 0; border-radius: 16px; background: var(--brand); color: var(--brand-ink);
  display: flex; align-items: center; gap: 14px; padding: 0 18px; font-size: 18px;
}
.cart-total { font-size: 22px; font-weight: 800; }
.cart-go { margin-left: auto; font-weight: 700; }

/* 底部面板 */
.sheet {
  width: 100%; max-width: 560px; margin: auto auto 0; padding: 0; border: 0; border-radius: 20px 20px 0 0;
  background: var(--surface); color: var(--ink); max-height: 92vh;
}
.sheet::backdrop { background: rgba(0,0,0,.45); }
.sheet-body { padding: 18px 16px calc(16px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 14px; }
.sheet h3 { margin: 0; font-size: 21px; }
.sheet-actions { display: flex; gap: 10px; position: sticky; bottom: 0; background: var(--surface); padding-top: 4px; }
.sheet-actions button { flex: 1; min-height: 52px; border-radius: 14px; font-weight: 700; }
.primary { border: 0; background: var(--brand); color: var(--brand-ink); }
.primary:disabled { opacity: .5; }
.ghost { border: 1px solid var(--line); background: transparent; }
.big { font-size: 19px; }

.pots { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.pot {
  min-height: 58px; border: 2px solid var(--line); border-radius: 12px; background: var(--surface); padding: 8px 10px; text-align: left;
}
.pot small { display: block; color: var(--muted); font-size: 13px; }
.pot[aria-pressed="true"] { border-color: var(--brand); background: var(--brand-soft); }

.qty-row { display: flex; align-items: center; justify-content: space-between; }
.stepper { display: flex; align-items: center; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.stepper button { width: 52px; height: 48px; border: 0; background: var(--brand-soft); color: var(--brand); font-size: 24px; font-weight: 700; }
.stepper output, .stepper .num { min-width: 48px; text-align: center; font-size: 20px; font-weight: 700; }

.lines { list-style: none; margin: 0; padding: 0; }
.lines li { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.line-name { font-weight: 600; }
.line-combo { color: var(--muted); font-size: 13px; }
.line-ctrl { grid-column: 1 / -1; display: flex; align-items: center; gap: 10px; }
.line-ctrl .stepper button { width: 40px; height: 38px; font-size: 20px; }
.line-ctrl .stepper .num { min-width: 36px; font-size: 17px; }
.price-btn { border: 1px dashed var(--line); background: none; border-radius: 8px; padding: 4px 10px; }
.price-btn.edited { border-color: var(--accent); color: var(--accent); }
.line-amt { margin-left: auto; font-weight: 700; }
.total-row { display: flex; justify-content: space-between; align-items: baseline; font-size: 20px; }
.total-row b { font-size: 28px; }

.seg { border: 0; padding: 0; margin: 0; display: flex; gap: 8px; }
.seg legend { font-size: 14px; color: var(--muted); margin-bottom: 6px; padding: 0; }
.seg label { flex: 1; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; text-align: center; padding: 12px 0; border: 2px solid var(--line); border-radius: 12px; font-weight: 600; }
.seg input:checked + span { border-color: var(--brand); background: var(--brand-soft); color: var(--brand); }
.seg input:focus-visible + span { outline: 3px solid var(--accent); }

.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 14px; color: var(--muted); }
.field input { padding: 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); font-size: 18px; }
.quick-cash { display: flex; gap: 8px; flex-wrap: wrap; }
.quick-cash button { border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 8px 12px; }
.change { font-weight: 700; color: var(--accent); min-height: 1.2em; }
.change.big { font-size: 26px; text-align: center; }
.inv summary { cursor: pointer; color: var(--brand); font-weight: 600; padding: 4px 0; }
.inv[open] { display: flex; flex-direction: column; gap: 10px; }
.error { color: var(--error); margin: 0; min-height: 1em; }

.done { text-align: center; align-items: center; }
.check { width: 72px; height: 72px; border-radius: 36px; background: var(--brand); color: var(--brand-ink); font-size: 40px; display: grid; place-items: center; }
.done .sheet-actions { width: 100%; }

.view-head { display: flex; align-items: center; gap: 12px; margin-top: 12px; }
.view-head h2 { margin: 0; font-size: 20px; }
.hint { color: var(--muted); font-size: 14px; }
.hint.warn { background: var(--warn-bg); color: var(--warn-ink); padding: 10px 12px; border-radius: 10px; }
.hint.ok { color: var(--brand); font-weight: 600; }
.chip { font-size: 12px; padding: 2px 8px; border-radius: 10px; background: var(--warn-bg); color: var(--warn-ink); margin-left: 6px; }
.json { margin: 0; padding: 12px; background: var(--bg); border-radius: 10px; font-size: 12px; overflow: auto; max-height: 50vh; white-space: pre-wrap; word-break: break-all; }

#toast {
  position: fixed; left: 50%; bottom: calc(96px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 15px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 20; max-width: calc(100% - 32px);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* 掃描載具 */
.with-btn { display: flex; gap: 8px; }
.with-btn input { flex: 1; min-width: 0; }
.scan-btn { flex: none; border: 0; border-radius: 10px; padding: 0 16px; background: var(--brand); color: var(--brand-ink); font-weight: 700; }
.scan {
  width: 100%; height: 100%; max-width: none; max-height: none; margin: 0; padding: 0; border: 0;
  background: #000; color: #fff;
}
.scan[open] { display: flex; flex-direction: column; }
.scan::backdrop { background: #000; }
.scan-stage { position: relative; flex: 1; overflow: hidden; }
.scan-stage video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.scan-guide {
  position: absolute; left: 6%; right: 6%; top: 50%; height: 28%; transform: translateY(-50%);
  border: 3px solid #fff; border-radius: 12px; box-shadow: 0 0 0 9999px rgba(0,0,0,.45);
}
.scan-status { margin: 0; padding: 14px 16px; text-align: center; font-size: 17px; }
.scan-cancel {
  margin: 0 16px calc(16px + env(safe-area-inset-bottom)); min-height: 52px; border-radius: 14px;
  color: #fff; border-color: rgba(255,255,255,.5); font-weight: 700;
}

/* ---- 登入、選單、修改、明細 ---- */
.menu-btn { border: 0; background: rgba(255,255,255,.14); color: inherit; border-radius: 10px; padding: 0 14px; font-size: 20px; }
.login { margin: auto; border-radius: 20px; max-width: 420px; }
.menu-item { display: block; width: 100%; text-align: left; padding: 14px 4px; border: 0; border-bottom: 1px solid var(--line); background: none; color: var(--ink); text-decoration: none; font-size: 17px; }
.banner.edit { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.banner.edit[hidden] { display: none; }
.small { padding: 6px 12px; min-height: 0; border-radius: 10px; font-size: 14px; }
.danger { border: 0; background: var(--error); color: #fff; }
.day-summary { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; }
.pill { padding: 4px 10px; border-radius: 999px; background: var(--surface); border: 1px solid var(--line); font-size: 14px; }
.pill.strong { font-weight: 700; }
.pill.warn { background: var(--warn-bg); color: var(--warn-ink); border-color: transparent; }
.pill.danger { background: var(--error); color: #fff; border-color: transparent; }
.chip.s-uploaded { background: var(--brand-soft); color: var(--brand); }
.chip.s-void { background: var(--line); color: var(--muted); }
.chip.s-unknown, .chip.s-rejected, .chip.s-uploading { background: var(--error); color: #fff; }
li.voided .row, tr.voided td { opacity: .55; }
.lines.compact li { grid-template-columns: 1fr auto; padding: 6px 0; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 8px 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-all; }
select { font: inherit; color: inherit; padding: 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); font-size: 18px; }

/* ---- 管理頁 ---- */
body.admin { padding-bottom: 32px; }
.admin-title { font-weight: 800; font-size: 19px; align-self: center; flex: 1; }
.whoami { align-self: center; opacity: .85; font-size: 14px; }
.top-link { align-self: center; color: inherit; background: rgba(255,255,255,.14); border: 0; border-radius: 10px; padding: 6px 12px; text-decoration: none; font-size: 14px; }
.admin-main { max-width: 1100px; margin: 0 auto; padding: 8px 16px; }
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin: 12px 0; }
.toolbar select { font-size: 16px; padding: 8px 10px; }
.toolbar .primary, .btn-link { border-radius: 12px; padding: 10px 16px; font-weight: 700; text-decoration: none; }
.tabs { display: flex; gap: 4px; border-bottom: 2px solid var(--line); margin-top: 12px; overflow-x: auto; }
.tabs button { border: 0; background: none; padding: 10px 14px; font-weight: 600; color: var(--muted); border-bottom: 3px solid transparent; margin-bottom: -2px; white-space: nowrap; }
.tabs button[aria-selected="true"] { color: var(--brand); border-bottom-color: var(--brand); }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.grid-table { width: 100%; border-collapse: collapse; font-size: 15px; }
.grid-table th, .grid-table td { padding: 10px 12px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
.grid-table th { font-size: 13px; color: var(--muted); font-weight: 600; background: var(--bg); }
.grid-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.grid-table td.items { white-space: normal; min-width: 200px; }
.grid-table td.actions { text-align: right; }
.grid-table td.actions button + button { margin-left: 6px; }
.grid-table td.empty { text-align: center; color: var(--muted); padding: 24px; }
.grid-table.compact th, .grid-table.compact td { padding: 6px 8px; font-size: 14px; }
.sheet.wide { max-width: 860px; }
.fingerprint code { font-size: 18px; font-weight: 700; letter-spacing: .05em; }
.card-form { margin-top: 16px; padding: 16px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); display: flex; flex-direction: column; gap: 12px; }
.card-form h3 { margin: 0; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }
.card-form .primary { align-self: flex-start; border-radius: 12px; padding: 10px 18px; font-weight: 700; }
ul.plain { margin: 4px 0; padding-left: 18px; font-size: 14px; }
@media (min-width: 900px) { body.admin .sheet { margin: auto; border-radius: 20px; } }

/* 折扣 */
.disc-btns button.on { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); font-weight: 600; }
.total-row.sub { font-size: .95em; color: var(--muted); padding-top: 2px; padding-bottom: 2px; }
.disc-field .error:empty { display: none; }
.disc-amount { display: flex; align-items: center; gap: 6px; margin-top: 8px; }
.disc-amount label { margin: 0; white-space: nowrap; color: var(--muted); }
.disc-amount .prefix { color: var(--muted); }
.disc-amount input { flex: 1; min-width: 0; }
.disc-amount.on input { border-color: var(--brand); box-shadow: 0 0 0 1px var(--brand); }

/* 收攤結算 */
.settle { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin: 10px 0 14px; }
.settle h3 { margin: 0 0 8px; font-size: 1rem; }
.settle-open { display: flex; align-items: center; gap: 6px; }
.settle-open label { white-space: nowrap; }
.settle-open .prefix { color: var(--muted); }
.settle-open input { flex: 1; min-width: 0; }
.settle .hint { margin: 4px 0 8px; }
.settle-kv dd b { font-size: 1.25rem; }
