:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #eef2f5;
  --border: #d8e0e6;
  --text: #16242e;
  --muted: #5c7183;
  --accent: #0d6e6e;
  --accent-soft: #e2f2f0;
  --warn: #b4560d;
  --warn-soft: #fdf0e3;
  --danger: #b3261e;
  --danger-soft: #fdecea;
  --ok: #1c6b3f;
  --ok-soft: #e5f3ea;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 36, 48, .07), 0 4px 14px rgba(16, 36, 48, .05);
}

* { box-sizing: border-box; }

/* hidden 屬性必須勝過各元件自己的 display，否則 [hidden] 的按鈕仍會顯示 */
[hidden] { display: none !important; }

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

h1, h2, h3 { margin: 0 0 .4em; line-height: 1.3; font-weight: 700; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.1rem; }
h3 { font-size: .98rem; }
a { color: var(--accent); }

/* ---- 版面 ---- */

.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 1rem; flex-wrap: wrap;
  padding: .6rem 1.1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 1.05rem; letter-spacing: .01em; white-space: nowrap; }
.brand span { color: var(--accent); }
.nav { display: flex; gap: .25rem; flex-wrap: wrap; flex: 1; }
.nav button {
  border: 0; background: none; padding: .42rem .75rem; border-radius: 999px;
  font: inherit; color: var(--muted); cursor: pointer; white-space: nowrap;
}
.nav button:hover { background: var(--surface-2); color: var(--text); }
.nav button[aria-current="true"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav .badge {
  display: inline-block; min-width: 1.25em; margin-inline-start: .35em; padding: 0 .35em;
  border-radius: 999px; background: var(--warn); color: #fff; font-size: .74rem; font-weight: 700;
}
.whoami { display: flex; align-items: center; gap: .6rem; font-size: .86rem; color: var(--muted); }
.role-tag {
  padding: .1rem .5rem; border-radius: 999px; font-size: .74rem; font-weight: 700;
  background: var(--accent-soft); color: var(--accent);
}

main { max-width: 1180px; margin: 0 auto; padding: 1.2rem 1.1rem 4rem; }
.page-head { display: flex; align-items: baseline; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: .9rem; }
.page-head p { margin: 0; color: var(--muted); font-size: .88rem; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
}
.card + .card { margin-top: .8rem; }
.card-body { padding: .9rem 1rem; }

/* ---- 表單元件 ---- */

button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea {
  padding: .45rem .6rem; border: 1px solid var(--border);
  border-radius: 7px; background: var(--surface); width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
label { display: block; font-size: .84rem; font-weight: 600; color: var(--muted); margin-bottom: .25rem; }
.field + .field { margin-top: .7rem; }
/* 蓋掉上面的 width:100%，勾選框不該被拉寬 */
input[type="checkbox"] { width: 1.15rem; height: 1.15rem; accent-color: var(--accent); }
.table input[type="checkbox"] { width: 1.15rem; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .35rem;
  padding: .48rem .95rem; border: 1px solid var(--border); border-radius: 7px;
  background: var(--surface); cursor: pointer; font-weight: 600; white-space: nowrap;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: #0a5a5a; }
.btn-danger { background: var(--danger-soft); border-color: #eec4c1; color: var(--danger); }
.btn-sm { padding: .3rem .6rem; font-size: .84rem; }
.btn-row { display: flex; gap: .5rem; flex-wrap: wrap; }

/* ---- 品項卡片 ---- */

.toolbar { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; margin-bottom: .9rem; }
.toolbar input[type="search"] { max-width: 320px; }
.chips { display: flex; gap: .35rem; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px;
  padding: .3rem .8rem; cursor: pointer; font-size: .86rem;
}
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }

.grid { display: grid; gap: .75rem; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }

.item {
  display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow);
}
.item-photo {
  aspect-ratio: 4 / 3; background: var(--surface-2);
  display: flex; align-items: center; justify-content: center; overflow: hidden;
}
.item-photo img { width: 100%; height: 100%; object-fit: contain; }
.item-photo svg { width: 46%; height: 46%; opacity: .5; }
.item-body { padding: .6rem .7rem .7rem; display: flex; flex-direction: column; gap: .35rem; flex: 1; }
.item-name { font-weight: 600; font-size: .93rem; line-height: 1.35; }
.item-spec { font-size: .8rem; color: var(--muted); }
.stock-line { display: flex; gap: .5rem; flex-wrap: wrap; font-size: .8rem; color: var(--muted); margin-top: auto; }
.stock-line b { color: var(--text); font-variant-numeric: tabular-nums; }
.item-add { display: flex; gap: .35rem; }
.item-add input { width: 5rem; text-align: center; }
.low-flag { font-size: .74rem; color: var(--warn); background: var(--warn-soft); padding: .05rem .4rem; border-radius: 4px; }

/* ---- 申請清單（側欄） ---- */

.layout { display: grid; gap: 1rem; grid-template-columns: 1fr 320px; align-items: start; }
.basket { position: sticky; top: 4.2rem; }
.basket-list { display: flex; flex-direction: column; gap: .5rem; max-height: 46vh; overflow-y: auto; }
.basket-row { display: grid; grid-template-columns: 1fr auto auto; gap: .4rem; align-items: center; font-size: .87rem; }
.basket-row input { width: 4.2rem; text-align: center; padding: .25rem; }
.basket-empty { color: var(--muted); font-size: .87rem; text-align: center; padding: 1.2rem 0; }
.basket-total { display: flex; justify-content: space-between; font-size: .87rem; color: var(--muted); margin: .6rem 0; }

/* ---- 表格 ---- */

.table { width: 100%; border-collapse: collapse; font-size: .89rem; }
.table th, .table td { padding: .5rem .6rem; text-align: start; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table th { font-size: .8rem; color: var(--muted); font-weight: 600; background: var(--surface-2); }
.table tbody tr:last-child td { border-bottom: 0; }
.table .num { text-align: end; font-variant-numeric: tabular-nums; }
.table input { width: 5rem; text-align: center; padding: .25rem; }
.table-wrap { overflow-x: auto; }
.thumb { width: 38px; height: 38px; object-fit: contain; background: var(--surface-2); border-radius: 5px; }

/* ---- 狀態標籤 ---- */

.status { display: inline-block; padding: .1rem .55rem; border-radius: 999px; font-size: .78rem; font-weight: 700; white-space: nowrap; }
.status-pending { background: var(--warn-soft); color: var(--warn); }
.status-approved { background: var(--ok-soft); color: var(--ok); }
.status-partial { background: var(--accent-soft); color: var(--accent); }
.status-rejected, .status-cancelled { background: var(--surface-2); color: var(--muted); }
.status-confirmed { background: var(--ok-soft); color: var(--ok); }

/* ---- 訊息 ---- */

.toast {
  position: fixed; inset-block-end: 1.2rem; inset-inline: 1.2rem; margin-inline: auto;
  max-width: 460px; z-index: 60; padding: .7rem 1rem; border-radius: 8px;
  box-shadow: 0 6px 22px rgba(16, 36, 48, .18); font-size: .9rem; font-weight: 500;
}
.toast-ok { background: var(--ok); color: #fff; }
.toast-err { background: var(--danger); color: #fff; }
.notice { padding: .6rem .8rem; border-radius: 7px; font-size: .87rem; }
.notice-warn { background: var(--warn-soft); color: var(--warn); }
.notice-err { background: var(--danger-soft); color: var(--danger); }
.notice-info { background: var(--accent-soft); color: var(--accent); }
.empty { text-align: center; color: var(--muted); padding: 2.5rem 1rem; }

/* ---- 登入 ---- */

.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1.2rem; }
.login-card { width: min(400px, 100%); }
.login-card .card-body { padding: 1.5rem; }
.login-hint { font-size: .82rem; color: var(--muted); margin-top: .9rem; }

/* 登入分頁：隊員 / 管理者 */
.tabs { display: flex; gap: .3rem; border-bottom: 1px solid var(--border); margin-top: .6rem; }
.tabs button {
  flex: 1; padding: .55rem .4rem; border: 0; border-bottom: 2px solid transparent;
  background: none; cursor: pointer; font-weight: 600; color: var(--muted);
}
.tabs button:hover { color: var(--text); }
.tabs button[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); }
.tabs button:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

/* 隊員名單的姓名、單位欄要能放得下整個字串，蓋掉 .table input 的窄欄設定 */
.member-table input { width: 100%; min-width: 7rem; text-align: start; padding: .35rem .5rem; }
.member-table .row-off { color: var(--muted); }

/* ---- 明細展開 ---- */

details.request { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); }
details.request + details.request { margin-top: .6rem; }
details.request > summary {
  padding: .7rem .9rem; cursor: pointer; display: flex; gap: .7rem;
  align-items: center; flex-wrap: wrap; list-style: none;
}
details.request > summary::-webkit-details-marker { display: none; }
details.request > summary::before { content: "▸"; color: var(--muted); font-size: .8rem; }
details.request[open] > summary::before { content: "▾"; }
.summary-main { flex: 1; min-width: 12rem; }
.summary-meta { font-size: .8rem; color: var(--muted); }
.detail-body { padding: 0 .9rem .9rem; border-top: 1px solid var(--border); padding-top: .8rem; }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .basket { position: static; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }
}

/* 窄螢幕：品牌與身分各佔一行，導覽列橫向捲動而不換行擠壓品牌名 */
@media (max-width: 640px) {
  .topbar { padding: .5rem .8rem; gap: .5rem; }
  .brand { flex: 1 0 auto; }
  .whoami { order: 2; margin-inline-start: auto; }
  .nav {
    order: 3; flex: 1 0 100%; flex-wrap: nowrap;
    overflow-x: auto; scrollbar-width: none;
    padding-bottom: .1rem;
  }
  .nav::-webkit-scrollbar { display: none; }
  main { padding: 1rem .8rem 3rem; }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #10181d; --surface: #18232a; --surface-2: #212f37; --border: #2c3d47;
    --text: #e6edf1; --muted: #93a7b4; --accent: #4bbfb2; --accent-soft: #123430;
    --warn: #e8a761; --warn-soft: #3a2b18; --danger: #f08c85; --danger-soft: #3a1e1c;
    --ok: #6fd39a; --ok-soft: #16301f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 14px rgba(0, 0, 0, .22);
  }
  .btn-primary { color: #06201e; }
  .toast-ok, .toast-err { color: #0b1418; }
}

/* ---- 相機拍照 ---- */

.modal-backdrop {
  position: fixed; inset: 0; z-index: 80; display: grid; place-items: center;
  background: rgba(9, 20, 26, .72); padding: 1rem;
}
.modal {
  width: min(560px, 100%); max-height: 92vh; overflow-y: auto;
  background: var(--surface); border-radius: var(--radius);
  box-shadow: 0 12px 40px rgba(0, 0, 0, .35);
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .8rem 1rem; border-bottom: 1px solid var(--border);
}
.modal-head h2 { margin: 0; font-size: 1rem; }
.modal-close {
  border: 0; background: none; cursor: pointer; font-size: 1.4rem;
  line-height: 1; color: var(--muted); padding: 0 .2rem;
}
.modal-body { padding: 1rem; }
.modal-foot { padding: .8rem 1rem; border-top: 1px solid var(--border); display: flex; gap: .5rem; justify-content: flex-end; flex-wrap: wrap; }

.camera-stage {
  position: relative; background: #0b1418; border-radius: 8px; overflow: hidden;
  aspect-ratio: 4 / 3; display: grid; place-items: center;
}
.camera-stage video, .camera-stage img { width: 100%; height: 100%; object-fit: contain; display: block; }
.camera-stage .placeholder { color: #9fb3bf; font-size: .88rem; padding: 1rem; text-align: center; }
.camera-current {
  display: flex; gap: .7rem; align-items: center; margin-bottom: .8rem;
  padding: .6rem; background: var(--surface-2); border-radius: 8px;
}
.camera-current img { width: 56px; height: 56px; object-fit: contain; background: var(--surface); border-radius: 6px; }
.camera-current .meta { font-size: .82rem; color: var(--muted); }

/* ---- 出庫清點與點收人簽名 ---- */

/* 簽名要留在紙本上也看得清楚，所以固定白底深筆，不跟著深色模式走 */
.sign-pad {
  display: block; width: 100%; height: 150px; background: #fff;
  border: 1px dashed var(--border); border-radius: 8px;
  touch-action: none; cursor: crosshair;
}
.sign-image { max-width: 260px; background: #fff; border: 1px solid var(--border); border-radius: 8px; }
.sign-block { margin-top: .9rem; padding: .8rem; background: var(--surface-2); border-radius: 8px; }
.sign-block > .field + .field { margin-top: .7rem; }
/* 清點沒過的那一列淡出，一眼看得出哪些不出庫 */
.row-unissued td { opacity: .45; }
.row-unissued td:first-child { opacity: 1; }

/* ---- 盤點小工具 ---- */

.stocktake-head { display: flex; gap: .9rem; align-items: flex-start; }
.stocktake-name { font-weight: 700; font-size: 1.05rem; line-height: 1.35; }
.stocktake-count { display: flex; gap: .4rem; align-items: center; justify-content: center; margin-top: 1.2rem; }
.stocktake-count input { width: 6.5rem; text-align: center; font-size: 1.5rem; font-weight: 700; padding: .45rem; }
.stocktake-diff { text-align: center; margin-top: .5rem; font-size: .85rem; color: var(--muted); }
/* 盤點數與系統不符時要看得出來 */
.stocktake-diff-gap { color: var(--warn); font-weight: 700; }
