:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #fafbfc;
  --border: #e2e5ea;
  --text: #1b2230;
  --muted: #6b7686;
  --faint: #98a2b3;

  --brand: #1f6feb;
  --brand-soft: #e8f0fe;

  --danger: #d7263d;
  --danger-soft: #fdecee;
  --warn: #e07b00;
  --warn-soft: #fff4e2;
  --ok: #17864a;
  --ok-soft: #e6f5ec;
  --idle: #7a5af5;
  --idle-soft: #f0ecff;

  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .05);
  --tap: 44px;
}

* { box-sizing: border-box; }

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

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }

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

.app { max-width: 1440px; margin: 0 auto; padding-bottom: 72px; }

.topbar {
  position: sticky; top: 0; z-index: 30;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 14px;
  display: flex; align-items: center; gap: 10px;
}
.topbar h1 { font-size: 16px; margin: 0; font-weight: 700; letter-spacing: .01em; }
.topbar .sub { font-size: 12px; color: var(--muted); }
.topbar .grow { flex: 1; min-width: 0; }
.topbar .title-wrap { min-width: 0; }
.topbar .title-wrap h1 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 14px;
  min-height: var(--tap);
  display: inline-flex; align-items: center; gap: 6px;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { filter: brightness(1.06); }
.btn-sm { min-height: 34px; padding: 4px 10px; font-size: 13px; }

/* ---------------------------------------------------------------- 統計 */

/* 手機上四個數字擠一排就好，本來 2x2 加上間距佔掉太多首屏 */
.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  padding: 8px 14px 2px;
}
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 7px 6px;
  box-shadow: var(--shadow);
  display: flex; flex-direction: column; align-items: center;
  gap: 0; text-align: center; min-width: 0;
}
.stat .n { font-size: 19px; font-weight: 700; line-height: 1.2;
           font-variant-numeric: tabular-nums; }
.stat .l { font-size: 11px; color: var(--muted); line-height: 1.25;
           white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
           max-width: 100%; }
.stat.danger .n { color: var(--danger); }
.stat.warn .n { color: var(--warn); }
.stat.idle .n { color: var(--idle); }
.stat.brand .n { color: var(--brand); }

/* ---------------------------------------------------------------- 篩選 */

.filters { padding: 8px 14px; display: flex; flex-direction: column; gap: 7px; }
.filter-row { display: flex; gap: 8px; flex-wrap: wrap; }
.filter-row > * { flex: 1 1 100%; min-width: 0; }

/* 搜尋列永遠可見，其餘篩選收在面板裡。
   手機上原本統計 + 三個全寬下拉 + 兩排 chips 要吃掉 700px，
   資料被推到螢幕外，得先捲很久才看得到第一支製令。 */
.filter-bar { display: flex; gap: 8px; align-items: center; }
.filter-bar .search { flex: 1 1 auto; min-width: 0; }
.filter-bar .btn { flex: 0 0 auto; }
.filter-count {
  background: var(--brand); color: #fff; border-radius: 999px;
  min-width: 18px; height: 18px; padding: 0 5px;
  font-size: 11px; line-height: 18px; text-align: center;
  font-variant-numeric: tabular-nums;
}

.filter-panel { display: none; flex-direction: column; gap: 7px; }
.filter-panel.open { display: flex; }

.date-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-end; }
.date-row label { flex: 1 1 140px; font-size: 12px; color: var(--muted);
                  display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.date-row .date-presets { flex: 1 1 100%; }

.field {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
  min-height: var(--tap);
  width: 100%;
}
.field:focus { outline: 2px solid var(--brand-soft); border-color: var(--brand); }

.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 13px;
  min-height: 36px;
  display: inline-flex; align-items: center; gap: 5px;
}
.chip[aria-pressed="true"] { background: var(--brand); border-color: var(--brand); color: #fff; }
.chip .cnt { opacity: .75; font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------- 卡片 */

.list { padding: 4px 14px 20px; display: grid; gap: 8px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--border);
  border-radius: var(--radius);
  padding: 11px 12px;
  box-shadow: var(--shadow);
  text-align: left;
  width: 100%;
  display: block;
}
.card:hover { border-color: var(--brand); }
.card.u-over { border-left-color: var(--danger); }
.card.u-soon { border-left-color: var(--warn); }
.card.u-ok   { border-left-color: var(--ok); }
.card.u-done { border-left-color: var(--faint); opacity: .72; }

.card-top { display: flex; align-items: baseline; gap: 8px; }
.wo { font-weight: 700; font-size: 15px; font-variant-numeric: tabular-nums; }
.spec { color: var(--muted); font-size: 13px; margin-top: 2px;
        overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.due { margin-left: auto; font-size: 12.5px; font-weight: 700; white-space: nowrap;
       padding: 2px 8px; border-radius: 999px; }
.due.over { background: var(--danger-soft); color: var(--danger); }
.due.soon { background: var(--warn-soft); color: var(--warn); }
.due.ok   { background: var(--ok-soft); color: var(--ok); }
.due.none { background: var(--surface-2); color: var(--faint); }

.bar { height: 6px; background: #eceff3; border-radius: 99px; margin: 9px 0 6px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--brand); border-radius: 99px; }
.card.u-over .bar > i { background: var(--danger); }
.card.u-done .bar > i { background: var(--ok); }

.meta { display: flex; gap: 10px; flex-wrap: wrap; font-size: 12.5px; color: var(--muted); }
.meta b { color: var(--text); font-weight: 600; }

.tags { display: flex; gap: 5px; flex-wrap: wrap; margin-top: 7px; }
.tag { font-size: 11.5px; padding: 2px 7px; border-radius: 5px; background: var(--surface-2);
       border: 1px solid var(--border); color: var(--muted); }
.tag.d { background: var(--danger-soft); border-color: #f6d4d9; color: var(--danger); }
.tag.w { background: var(--warn-soft); border-color: #f6e2c2; color: var(--warn); }
.tag.i { background: var(--idle-soft); border-color: #ddd6fb; color: var(--idle); }
.tag.s { background: var(--brand-soft); border-color: #cfe0fd; color: var(--brand); }

/* ---------------------------------------------------------------- 表格（桌機） */

/* 寬表格在自己的容器裡捲動，整個頁面不橫向捲。
   容器同時負責垂直捲動，表頭才能 sticky 在容器頂端 —— 若只設 overflow-x，
   sticky 的定位基準會變成這個容器，top 值會把表頭往下推、蓋住第一列。 */
.table-wrap { display: none; padding: 4px 14px 20px; }
.table-scroll {
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  max-height: calc(100vh - 210px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
table.grid {
  width: 100%; min-width: 1080px;
  border-collapse: separate; border-spacing: 0;
  table-layout: fixed;              /* 固定欄寬，長品名才不會把整張表擠變形 */
}
table.grid th, table.grid td {
  padding: 7px 10px; text-align: left; font-size: 13px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  vertical-align: middle;
}
table.grid th {
  background: var(--surface-2); font-size: 12px; color: var(--muted);
  font-weight: 600; letter-spacing: .02em;
  position: sticky; top: 0; z-index: 5;
  box-shadow: inset 0 -1px 0 var(--border);
}
table.grid tbody tr:hover { background: var(--brand-soft); cursor: pointer; }
table.grid tr:last-child td { border-bottom: 0; }
.num { font-variant-numeric: tabular-nums; text-align: right; }

/* 欄寬：合計 1080px，靠品名欄吸收剩餘空間 */
table.grid col.c-wo    { width: 130px; }
table.grid col.c-spec  { width: auto;  }
table.grid col.c-cat   { width: 150px; }
table.grid col.c-due   { width: 96px;  }
table.grid col.c-left  { width: 92px;  }
table.grid col.c-prog  { width: 116px; }
table.grid col.c-qty   { width: 84px;  }
table.grid col.c-last  { width: 74px;  }
table.grid col.c-st    { width: 130px; }
table.grid col.c-alert { width: 172px; }

/* 品名很長，截成一行並用 title 顯示全文，列高才穩定 */
td.spec-cell { color: var(--muted); }
td.spec-cell span { display: block; overflow: hidden; text-overflow: ellipsis; }

td.wo-cell b { font-variant-numeric: tabular-nums; }
td.alert-cell { white-space: nowrap; overflow: hidden; }
td.prog-cell .bar { margin: 0 0 3px; }
td.prog-cell .frac { font-size: 11px; color: var(--muted);
                     font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------- 詳情 */

.sheet-backdrop {
  position: fixed; inset: 0; background: rgba(16, 24, 40, .45);
  z-index: 50; display: flex; align-items: flex-end; justify-content: center;
}
.sheet {
  background: var(--surface);
  width: 100%; max-width: 760px; max-height: 92vh;
  border-radius: 14px 14px 0 0;
  display: flex; flex-direction: column;
  animation: rise .18s ease-out;
}
@keyframes rise { from { transform: translateY(18px); opacity: .6; } to { transform: none; opacity: 1; } }

.sheet-head { padding: 14px 16px 10px; border-bottom: 1px solid var(--border);
              display: flex; align-items: flex-start; gap: 10px; }
.sheet-body { overflow-y: auto; padding: 12px 16px 24px; -webkit-overflow-scrolling: touch; }

.tl { list-style: none; margin: 0; padding: 0; }
.tl li { position: relative; padding: 0 0 14px 26px; border-left: 2px solid var(--border); }
.tl li:last-child { border-left-color: transparent; padding-bottom: 4px; }
.tl li::before {
  content: ""; position: absolute; left: -7px; top: 3px;
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--surface); border: 2px solid var(--faint);
}
.tl li.done::before { background: var(--ok); border-color: var(--ok); }
.tl li.over::before { background: var(--danger); border-color: var(--danger); }
.tl li.plan::before { background: var(--surface); border-color: var(--brand); }

.tl .st { font-weight: 650; font-size: 14px; }
.tl .plan-range { font-size: 12px; color: var(--muted); }
/* 站別數量對帳：這站做了多少、誰做的、還差多少 */
.recon { margin-top: 5px; display: flex; gap: 7px; align-items: center;
         flex-wrap: wrap; font-size: 12.5px; color: var(--muted); }
.recon b { color: var(--text); font-variant-numeric: tabular-nums; }
.recon .vendors { display: flex; gap: 6px; flex-wrap: wrap; flex-basis: 100%; }
.vshare { background: var(--brand-soft); border: 1px solid #cfe0fd; color: var(--brand);
          border-radius: 5px; padding: 1px 7px; font-size: 11.5px;
          font-variant-numeric: tabular-nums; }

.tl .rows { margin-top: 5px; display: grid; gap: 4px; }
.tl .row {
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 7px; padding: 5px 8px; font-size: 13px;
  display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap;
}
.tl .row .dt { font-weight: 650; font-variant-numeric: tabular-nums; }
.tl .row .q { font-variant-numeric: tabular-nums; }
.tl .row .raw { color: var(--faint); font-size: 11.5px; margin-left: auto; }
.tl .row.sched { background: #fff; border-style: dashed; color: var(--muted); }

/* 回報表單 */
.tl .row.editable { cursor: pointer; }
.tl .row.editable:hover { border-color: var(--brand); background: var(--brand-soft); }

.st-actions { display: flex; gap: 6px; margin-top: 6px; }

.entry-form {
  margin-top: 8px; padding: 10px;
  background: var(--surface-2); border: 1px solid var(--brand);
  border-radius: var(--radius);
}
.entry-form .ef-row { display: flex; gap: 8px; margin-bottom: 8px; flex-wrap: wrap; }
.entry-form label { flex: 1 1 130px; font-size: 12px; color: var(--muted);
                    display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.entry-form .field { min-height: 40px; }
.entry-form .ef-flags { margin-bottom: 8px; }
.entry-form .ef-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.entry-form .ef-hint { font-size: 12.5px; color: var(--muted); margin-bottom: 8px; }
.btn.danger { color: var(--danger); border-color: #f6d4d9; margin-left: auto; }
.btn.danger:hover { background: var(--danger-soft); }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 3px 12px; font-size: 13.5px; margin-bottom: 12px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-weight: 600; }

.section-title { font-size: 12px; color: var(--muted); font-weight: 700; letter-spacing: .06em;
                 margin: 16px 0 8px; text-transform: uppercase; }

/* ---------------------------------------------------------------- 盤點 */

.review-item {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 11px 12px; box-shadow: var(--shadow);
}
.review-item .why { font-size: 12.5px; color: var(--muted); margin-top: 5px; }
.review-item .acts { display: flex; gap: 6px; margin-top: 9px; flex-wrap: wrap; }
.conf { font-size: 11px; padding: 2px 7px; border-radius: 5px; font-weight: 700; }
.conf.high { background: var(--danger-soft); color: var(--danger); }
.conf.medium { background: var(--warn-soft); color: var(--warn); }
.conf.low { background: var(--surface-2); color: var(--muted); }
.review-item.picked { border-color: var(--brand); background: var(--brand-soft); }

.sticky-actions {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 10px 14px; display: flex; gap: 8px; align-items: center;
  box-shadow: 0 -2px 8px rgba(16,24,40,.06);
}

/* ---------------------------------------------------------------- 雜項 */

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

.login-screen {
  position: fixed; inset: 0; z-index: 100;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 4px 24px rgba(16, 24, 40, .08);
  padding: 26px 22px;
  width: 100%; max-width: 360px;
  display: flex; flex-direction: column; gap: 12px;
}
.login-card h1 { margin: 0; font-size: 20px; }
.login-card .login-sub { margin: -8px 0 4px; font-size: 13px; color: var(--muted); }
.login-card label { font-size: 12.5px; color: var(--muted);
                    display: flex; flex-direction: column; gap: 4px; }
.login-card .field { min-height: 44px; }
.login-submit { justify-content: center; margin-top: 4px; }
.login-error {
  background: var(--danger-soft); color: var(--danger);
  border: 1px solid #f6d4d9; border-radius: 8px;
  padding: 8px 10px; font-size: 13px;
}

.empty { text-align: center; padding: 48px 20px; color: var(--muted); }
.empty .big { font-size: 34px; margin-bottom: 8px; }
.spinner { display: inline-block; width: 15px; height: 15px; border: 2px solid var(--border);
           border-top-color: var(--brand); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.hidden { display: none !important; }

.tabs { display: flex; gap: 2px; padding: 0 14px; border-bottom: 1px solid var(--border);
        background: var(--surface); overflow-x: auto; }
.tabs button { border: 0; background: none; padding: 11px 12px; font-size: 14px;
               color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs button[aria-selected="true"] { color: var(--brand); border-bottom-color: var(--brand); font-weight: 650; }

.toast {
  position: fixed; bottom: 84px; left: 50%; transform: translateX(-50%);
  background: #1b2230; color: #fff; padding: 10px 16px; border-radius: 8px;
  font-size: 14px; z-index: 90; box-shadow: var(--shadow);
}

/* ---------------------------------------------------------------- RWD */

@media (min-width: 600px) {
  .stats { gap: 8px; }
  .stat { flex-direction: row; align-items: baseline; gap: 8px;
          text-align: left; padding: 8px 12px; }
  .stat .l { font-size: 12px; }
  .filter-row > * { flex: 0 0 auto; width: auto; }
  .filter-row > select.field { flex: 0 0 auto; width: auto; max-width: 260px; }
  .filter-row .search { flex: 1 1 220px; width: auto; }
}

/* 桌機空間夠，篩選面板直接攤開，不用多按一下 */
@media (min-width: 900px) {
  .filter-panel { display: flex !important; }
  #btn-filters { display: none; }
}

@media (min-width: 1000px) {
  .topbar h1 { font-size: 18px; }
  .list.as-table { display: none; }
  .table-wrap.as-table { display: block; }
  .sheet-backdrop { align-items: center; }
  .sheet { border-radius: 14px; max-height: 86vh; }
  /* 桌機資訊密度拉高：統計列與篩選列縮排 */
  .stats { padding: 10px 14px 0; }
  .stat { padding: 7px 11px; }
  .stat .n { font-size: 19px; }
  .chip { min-height: 32px; padding: 4px 11px; font-size: 12.5px; }
  .field { min-height: 36px; padding: 6px 10px; font-size: 13.5px; }
}
