:root {
  --green: #00843d;
  --green-dark: #006a31;
  --orange: #f47920;
  --red: #e4002b;
  --bg: #f4f6f5;
  --surface: #ffffff;
  --text: #1d2420;
  --muted: #6b766f;
  --border: #dde3df;
  --radius: 14px;
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121614;
    --surface: #1c221f;
    --text: #e8ece9;
    --muted: #97a39b;
    --border: #2e3732;
    --shadow: 0 2px 10px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  font-size: 16px;
  line-height: 1.5;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
code { background: rgba(0,0,0,.08); padding: 1px 5px; border-radius: 4px; }

/* ---------- 按鈕 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid transparent; border-radius: 10px;
  padding: 10px 16px; font-size: 15px; font-weight: 600;
  cursor: pointer; background: var(--surface); color: var(--text);
  transition: filter .15s, background .15s;
  white-space: nowrap;
}
.btn:hover { filter: brightness(0.96); }
.btn:disabled { opacity: .6; cursor: not-allowed; }
.btn-primary { background: var(--green); color: #fff; }
.btn-primary:hover { background: var(--green-dark); filter: none; }
.btn-scan { background: var(--orange); color: #fff; }
.btn-outline { border-color: var(--border); }
.btn-ghost { background: transparent; border-color: var(--border); }
.btn-danger { background: transparent; color: var(--red); border-color: var(--border); }
.btn-sm { padding: 6px 12px; font-size: 14px; }
.btn-block { width: 100%; }
.btn-link { background: none; border: none; color: var(--green); cursor: pointer; font-size: 14px; padding: 4px; }

/* ---------- 表單 ---------- */
.form { display: flex; flex-direction: column; gap: 14px; }
.form label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; font-weight: 600; color: var(--muted); }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--green); outline-offset: -1px; }
.input-group { display: flex; gap: 8px; }
.input-group input { flex: 1; min-width: 0; font-family: ui-monospace, Consolas, monospace; }

.msg { min-height: 1.2em; margin: 0; font-size: 14px; }
.msg.error { color: var(--red); }
.msg.ok { color: var(--green); }

/* ---------- 登入畫面 ---------- */
#auth-view { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.auth-card {
  width: 100%; max-width: 400px; background: var(--surface);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 28px 24px;
}
.brand { text-align: center; margin-bottom: 20px; }
.brand-logo { font-size: 44px; }
.brand h1 { margin: 6px 0 2px; font-size: 24px; }
.brand p { margin: 0; }
.tabs { display: flex; background: var(--bg); border-radius: 10px; padding: 4px; margin-bottom: 18px; }
.tab {
  flex: 1; border: none; background: transparent; padding: 8px; border-radius: 8px;
  font: inherit; font-weight: 600; color: var(--muted); cursor: pointer;
}
.tab.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* ---------- 主畫面 ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px; background: var(--green); color: #fff;
}
.topbar-title { font-weight: 700; font-size: 18px; }
.topbar-user { display: flex; align-items: center; gap: 10px; min-width: 0; }
.topbar-user .muted { color: rgba(255,255,255,.85); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 40vw; }
.topbar .btn-ghost { color: #fff; border-color: rgba(255,255,255,.5); }

.container { max-width: 960px; margin: 0 auto; padding: 16px; }

.summary { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 16px; }
.summary-item { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; }
.summary-label { font-size: 13px; color: var(--muted); }
.summary-value { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }

.toolbar { display: flex; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; }
.toolbar input { flex: 1 1 220px; }
.toolbar select { flex: 0 0 auto; width: auto; }

.card-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }

.gift-card {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  overflow: hidden; display: flex; flex-direction: column;
}
.gift-card-top {
  background: linear-gradient(135deg, var(--green) 0%, var(--green) 55%, var(--orange) 55%, var(--orange) 72%, var(--red) 72%);
  color: #fff; padding: 14px 16px; position: relative;
}
.gift-card-name { font-weight: 700; font-size: 16px; margin-right: 60px; word-break: break-all; }
.gift-card-balance { font-size: 28px; font-weight: 800; margin-top: 6px; font-variant-numeric: tabular-nums; }
.gift-card-badge {
  position: absolute; top: 12px; right: 12px; font-size: 12px; font-weight: 700;
  background: rgba(0,0,0,.25); padding: 2px 8px; border-radius: 99px;
}
.gift-card-body { padding: 12px 16px; display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.kv { display: flex; justify-content: space-between; gap: 10px; align-items: center; }
.kv .k { color: var(--muted); flex: 0 0 auto; }
.kv .v { font-family: ui-monospace, Consolas, monospace; word-break: break-all; text-align: right; }
.kv .v.clickable { cursor: pointer; border-bottom: 1px dashed var(--muted); }
.gift-card-note { color: var(--muted); font-size: 13px; white-space: pre-wrap; }
.gift-card-actions { display: flex; gap: 8px; padding: 0 16px 14px; margin-top: auto; }
.gift-card-actions .btn { flex: 1; }

.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
.empty-icon { font-size: 48px; }

/* ---------- Modal ---------- */
.modal {
  position: fixed; inset: 0; z-index: 100; background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal-box {
  width: 100%; max-width: 460px; max-height: calc(100vh - 32px); overflow-y: auto;
  background: var(--surface); border-radius: var(--radius); padding: 20px;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.modal-head h2 { margin: 0; font-size: 20px; }
.modal-close { border: none; background: none; font-size: 20px; color: var(--muted); cursor: pointer; padding: 4px 8px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; }

/* ---------- 餘額 ---------- */
.balance-info { background: var(--bg); border-radius: 10px; padding: 10px 14px; margin-bottom: 14px; }
.bal-current strong { font-size: 22px; }
.segmented { display: flex; gap: 6px; flex-wrap: wrap; }
.segmented label {
  flex: 1; flex-direction: row !important; align-items: center; justify-content: center; gap: 4px !important;
  border: 1px solid var(--border); border-radius: 10px; padding: 8px; cursor: pointer; color: var(--text) !important;
  white-space: nowrap;
}
.segmented input { width: auto; margin: 0; }
.segmented label:has(input:checked) { border-color: var(--green); background: rgba(0,132,61,.1); }
.bal-preview { font-size: 15px; }
.bal-preview strong { font-size: 18px; }
.bal-preview.negative strong { color: var(--red); }
.history-title { font-size: 15px; margin: 20px 0 8px; }
.history-list { list-style: none; margin: 0; padding: 0; font-size: 14px; }
.history-list li { display: flex; justify-content: space-between; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.history-list .amt-plus { color: var(--green); font-weight: 700; }
.history-list .amt-minus { color: var(--red); font-weight: 700; }
.history-list .amt-set { color: var(--orange); font-weight: 700; }

/* ---------- 掃描 ---------- */
.scanner { width: 100%; border-radius: 10px; overflow: hidden; background: #000; min-height: 220px; }
.scanner video { width: 100% !important; display: block; }
.scan-controls { display: flex; gap: 8px; margin-top: 10px; }
.scan-controls select { flex: 1; min-width: 0; }
.file-btn { cursor: pointer; }

/* ---------- 其他 ---------- */
.banner { padding: 10px 16px; font-size: 14px; text-align: center; }
.banner-warn { background: #fff3cd; color: #664d03; }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 200;
  background: #222; color: #fff; padding: 10px 18px; border-radius: 99px; font-size: 14px;
  box-shadow: 0 4px 14px rgba(0,0,0,.3); max-width: calc(100vw - 32px);
}

@media (max-width: 520px) {
  .toolbar #add-btn { width: 100%; }
  .summary-value { font-size: 22px; }
}

/* ---------- 出示條碼 ---------- */
.gift-card-top { cursor: pointer; }
.gift-card-body { cursor: pointer; }
.gift-card-hint { font-size: 12px; opacity: .85; margin-top: 2px; }
.code-balance { text-align: center; margin-bottom: 12px; }
.code-balance strong { font-size: 24px; }
.code-tabs { margin-bottom: 12px; }
/* 條碼區一律白底黑碼，深色模式下也能被掃描 */
.code-sheet { background: #fff; color: #000; border-radius: 12px; padding: 16px; display: flex; flex-direction: column; gap: 18px; }
.code-item { text-align: center; }
.code-label { font-size: 13px; font-weight: 700; color: #555; margin-bottom: 6px; }
.code-img { display: flex; justify-content: center; }
.code-img svg { width: 100%; height: auto; display: block; }
.code-img.qr svg { max-width: 220px; }
.code-img.bar svg { max-width: 100%; }
.code-text { font-family: ui-monospace, Consolas, monospace; font-size: 16px; letter-spacing: 1px; margin-top: 6px; word-break: break-all; }
.code-tip { font-size: 13px; text-align: center; margin: 12px 0; }

/* ---------- 封存 ---------- */
.summary-sub { font-size: 12px; min-height: 1em; }
.gift-card.archived .gift-card-top { background: linear-gradient(135deg, #8a948e, #6b766f); }
.gift-card.archived { opacity: .85; }

/* ---------- 出示條碼：全螢幕白底，讓畫面盡可能明亮 ---------- */
#code-modal { background: #fff; padding: 0; }
#code-modal .code-box {
  max-width: 520px; max-height: 100vh; height: 100%; border-radius: 0;
  background: #fff; color: #000; display: flex; flex-direction: column;
}
#code-modal .modal-close, #code-modal .code-tip { color: #555; }
#code-modal .tabs { background: #eef1ef; }
#code-modal .tab { color: #555; }
#code-modal .tab.active { background: #fff; color: #000; }
#code-modal .code-sheet { padding: 8px 0; }
