*, *::before, *::after { box-sizing: border-box; }

/* ---------- 禁用瀏覽器手勢（避免現場操作時被誤觸） ----------
   - touch-action: pan-x pan-y → 只允許捲動；雙指縮放、雙擊縮放一律關閉
   - overscroll-behavior: none → 關閉下拉重新整理、頁面彈跳與（Chrome）邊緣滑動返回
   - 長按選字／選單、圖片長按與拖曳全部關閉；輸入框內仍可選字、貼上 */
html, body { overscroll-behavior: none; }
* { touch-action: pan-x pan-y; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; }
body { -webkit-user-select: none; user-select: none; }
input, textarea, select { -webkit-user-select: text; user-select: text; }
img { -webkit-user-drag: none; user-drag: none; }
.matrix-wrap, .gantt-wrap, .jumpbar, .scrollrow, .modal { overscroll-behavior: contain; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--c-bg); color: var(--c-text);
  font: 15px/1.5 var(--font-sans); padding-bottom: env(safe-area-inset-bottom);
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.25; }

/* ---------- 版面 ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30; display: flex; align-items: center; gap: var(--space-4);
  padding: 10px var(--space-4); background: var(--c-surface); border-bottom: 1px solid var(--c-border);
}
.brand { font-size: 17px; white-space: nowrap; }
/* 導覽列常駐在畫面下方（桌面、手機皆同）；.topbar 不可有 transform，否則 fixed 會相對它定位 */
.topbar { justify-content: space-between; }
.brand { font-size: 17px; white-space: nowrap; flex: 1; }
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; display: flex; justify-content: center; gap: 4px;
  background: var(--c-surface); border-top: 1px solid var(--c-border);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
}
.tabs button {
  flex: 1; max-width: 220px; border: 0; background: transparent; padding: 10px 4px; border-radius: var(--radius-sm);
  color: var(--c-text-dim); cursor: pointer; font-weight: 600; font-size: 14px;
}
.tabs button[aria-selected='true'] { background: var(--c-primary); color: var(--c-primary-ink); }
main { max-width: 1280px; margin: 0 auto; padding: var(--space-4); padding-bottom: calc(88px + env(safe-area-inset-bottom)); }
.tab-panel { min-width: 0; }
@media (max-width: 720px) {
  .brand { font-size: 15px; }
  .tabs button { font-size: 13px; padding: 10px 2px; }
}

/* ---------- 元件 ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--c-border);
  background: var(--c-surface); padding: 8px 14px; border-radius: var(--radius-sm); cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--c-surface-2); }
.btn.primary { background: var(--c-primary); border-color: var(--c-primary); color: var(--c-primary-ink); font-weight: 600; }
.btn.danger { color: var(--c-danger); border-color: var(--c-danger); }
.btn.sm { padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.icon-btn { border: 0; background: transparent; font-size: 18px; cursor: pointer; padding: 6px 10px; border-radius: var(--radius-sm); }
.icon-btn:hover { background: var(--c-surface-2); }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; min-height: 38px; } /* 更新鈕（SVG）與主題鈕（文字）等高 */
.icon-btn:disabled { cursor: progress; }
.icon-btn svg { display: block; }
.icon-btn.spinning svg { animation: spin 0.8s linear infinite; }
@media (prefers-reduced-motion: reduce) { .icon-btn.spinning svg { animation-duration: 2.4s; } }

input[type='text'], input[type='search'], input[type='date'], input[type='number'], select, textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--c-border); border-radius: var(--radius-sm);
  background: var(--c-surface); min-height: 38px;
}
textarea { resize: vertical; min-height: 84px; }
input:focus, select:focus, textarea:focus, .btn:focus-visible, .tabs button:focus-visible { outline: 2px solid var(--c-primary); outline-offset: 1px; }
label.field { display: grid; gap: 4px; font-size: 13px; color: var(--c-text-dim); }
label.field > :not(span) { color: var(--c-text); font-size: 15px; }

.toolbar { display: flex; flex-wrap: wrap; gap: var(--space-2); align-items: center; margin-bottom: var(--space-3); }
.toolbar .grow { flex: 1 1 200px; min-width: 140px; }
.toolbar select, .toolbar input[type='date'] { width: auto; }
.spacer { flex: 1; }
.muted { color: var(--c-text-dim); font-size: 13px; }

.card { background: var(--c-surface); border: 1px solid var(--c-border); border-radius: var(--radius-md); padding: var(--space-3) var(--space-4); box-shadow: var(--shadow-sm); }
.empty { text-align: center; color: var(--c-text-dim); padding: 48px 0; }
.more { display: flex; justify-content: center; margin: var(--space-3) 0; }

.chip { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; background: var(--c-surface-2); color: var(--c-text-dim); }
.chip.doing { background: color-mix(in srgb, var(--c-info) 18%, transparent); color: var(--c-info); }
.chip.done { background: color-mix(in srgb, var(--c-ok) 18%, transparent); color: var(--c-ok); }
.chip.issue { background: color-mix(in srgb, var(--c-danger) 18%, transparent); color: var(--c-danger); }

.bar { height: 6px; border-radius: 3px; background: var(--c-surface-2); overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--c-primary); }

/* ---------- 紀錄 ---------- */
.entry { display: grid; gap: 6px; margin-bottom: var(--space-2); }
.entry-head { display: flex; gap: var(--space-2); align-items: center; flex-wrap: wrap; }
.entry-head input[type='checkbox'] { width: 18px; height: 18px; }
.entry-head b { font-variant-numeric: tabular-nums; }
.entry-desc { white-space: pre-wrap; word-break: break-word; }
.thumbs { display: flex; gap: 6px; flex-wrap: wrap; }
.thumbs img { width: 72px; height: 72px; object-fit: cover; border-radius: var(--radius-sm); cursor: zoom-in; border: 1px solid var(--c-border); }
/* 縮圖右下角的下載鈕（全站停用長按選單，所以要有明確的下載入口） */
.thumbs .th { position: relative; display: inline-block; line-height: 0; }
.th-dl {
  position: absolute; right: 3px; bottom: 3px; width: 28px; height: 28px; padding: 0; border: 0; border-radius: 50%; cursor: pointer;
  background: rgba(0, 0, 0, 0.62); color: #fff; font-size: 15px; line-height: 28px; text-align: center; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
}
.th-dl:hover { background: rgba(0, 0, 0, 0.85); }
.th-dl:focus-visible { outline: 2px solid #fff; outline-offset: 1px; }

/* ---------- 影片：和照片共用同一套媒體格，靠 ▶ 標記區分 ---------- */
.thumbs video, .photo-edit .ph video, .d-img video { display: block; object-fit: cover; background: var(--c-surface-2); }
.thumbs video { width: 72px; height: 72px; border-radius: var(--radius-sm); cursor: zoom-in; border: 1px solid var(--c-border); }
.photo-edit .ph video { width: 96px; height: 96px; border-radius: var(--radius-sm); cursor: zoom-in; border: 1px solid var(--c-border); }
.d-img video { width: 100%; height: 100%; }
.play { position: absolute; left: 50%; top: 50%; width: 26px; height: 26px; margin: -13px 0 0 -13px; border-radius: 50%; background: rgba(0, 0, 0, 0.6); color: #fff; font-size: 11px; line-height: 26px; text-align: center; pointer-events: none; }
.vid-bad { background: var(--c-surface-2); }
.vid-bad video { visibility: hidden; }
.vid-bad .play { background: var(--c-danger); }
.busy b { font-size: 12px; font-weight: 600; color: var(--c-text-dim); font-variant-numeric: tabular-nums; }
.lightbox video { max-width: 94vw; max-height: 88vh; background: #000; }
.lb-note { position: absolute; left: 12px; right: 12px; bottom: 60px; margin: 0; padding: 10px 14px; border-radius: var(--radius-md); background: rgba(0, 0, 0, 0.72); color: #fff; font-size: 14px; line-height: 1.5; text-align: center; }
.entry-actions { display: flex; gap: 6px; }
.batchbar { position: sticky; bottom: calc(70px + env(safe-area-inset-bottom)); display: flex; gap: var(--space-2); align-items: center; padding: 10px var(--space-3); margin-top: var(--space-3); background: var(--c-surface); border: 1px solid var(--c-primary); border-radius: var(--radius-md); box-shadow: var(--shadow-md); }
.photo-edit { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.photo-edit .ph { position: relative; }
.photo-edit .ph img { width: 96px; height: 96px; object-fit: cover; border-radius: var(--radius-sm); display: block; cursor: zoom-in; border: 1px solid var(--c-border); }
.photo-edit .ph.busy { width: 96px; height: 96px; border-radius: var(--radius-sm); background: var(--c-surface-2); display: grid; place-items: center; }
.photo-edit .ph.busy i { width: 22px; height: 22px; border-radius: 50%; border: 3px solid var(--c-border); border-top-color: var(--c-primary); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.photo-edit .ph button { position: absolute; top: -6px; right: -6px; width: 20px; height: 20px; border-radius: 50%; border: 0; background: var(--c-danger); color: #fff; cursor: pointer; line-height: 1; padding: 0; }

/* ---------- Modal / lightbox / toast ---------- */
.modal-back { position: fixed; inset: 0; z-index: 60; background: rgba(0, 0, 0, 0.5); display: grid; place-items: center; padding: var(--space-3); }
.modal { width: min(560px, 100%); max-height: 92vh; overflow: auto; background: var(--c-surface); border-radius: var(--radius-lg); padding: var(--space-4) var(--space-5); box-shadow: var(--shadow-md); }
.modal h2 { font-size: 18px; margin-bottom: var(--space-3); }
.modal .grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-3); }
.modal .form { display: grid; gap: var(--space-3); }
.modal .actions { display: flex; gap: var(--space-2); justify-content: flex-end; margin-top: var(--space-4); }
@media (max-width: 480px) { .modal .grid2 { grid-template-columns: 1fr; } }

.lightbox { position: fixed; inset: 0; z-index: 80; background: rgba(0, 0, 0, 0.92); display: grid; place-items: center; }
.lightbox img { max-width: 94vw; max-height: 88vh; object-fit: contain; }
.lightbox button { position: absolute; border: 0; background: rgba(255, 255, 255, 0.15); color: #fff; font-size: 26px; width: 48px; height: 48px; border-radius: 50%; cursor: pointer; }
.lightbox .lb-close { top: 16px; right: 16px; }
.lightbox .lb-dl { top: 16px; right: 76px; font-size: 22px; }
.lightbox .lb-prev { left: 12px; top: 50%; }
.lightbox .lb-next { right: 12px; top: 50%; }
.lightbox .lb-count { position: absolute; bottom: 18px; color: #fff; font-size: 14px; }

.toasts { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); z-index: 90; display: grid; gap: 8px; justify-items: center; pointer-events: none; }
.toast { background: var(--c-text); color: var(--c-bg); padding: 8px 16px; border-radius: 999px; font-size: 14px; box-shadow: var(--shadow-md); }
.toast.err { background: var(--c-danger); color: #fff; }

/* ---------- 進度總覽 ---------- */
.matrix-wrap { overflow: auto; border: 1px solid var(--c-border); border-radius: var(--radius-md); background: var(--c-surface); max-height: 72vh; }
table.matrix { border-collapse: separate; border-spacing: 0; min-width: 100%; }
.matrix th, .matrix td { border-bottom: 1px solid var(--c-border); border-right: 1px solid var(--c-border); padding: 0; text-align: center; font-size: 13px; }
.matrix thead th { position: sticky; top: 0; z-index: 3; background: var(--c-surface-2); padding: 8px 10px; white-space: nowrap; }
.matrix thead th.grp { cursor: pointer; user-select: none; }
.matrix tbody th { position: sticky; left: 0; z-index: 2; background: var(--c-surface-2); padding: 8px 12px; text-align: left; white-space: nowrap; cursor: pointer; }
.matrix thead th:first-child { left: 0; z-index: 4; }
.cell { width: 74px; height: 40px; cursor: pointer; user-select: none; transition: background 0.12s; }
.cell:hover { filter: brightness(0.95); }
.cell[data-s='todo'] { color: var(--c-text-dim); }
.cell[data-s='doing'] { background: color-mix(in srgb, var(--c-warn) 28%, transparent); }
.cell[data-s='done'] { background: color-mix(in srgb, var(--c-ok) 28%, transparent); }
.cell[data-s='na'] { background: repeating-linear-gradient(45deg, transparent 0 6px, var(--c-surface-2) 6px 12px); color: var(--c-text-dim); }
.legend { display: flex; gap: var(--space-3); flex-wrap: wrap; font-size: 13px; color: var(--c-text-dim); }
.seg { display: inline-flex; border: 1px solid var(--c-border); border-radius: var(--radius-sm); overflow: hidden; }
.seg button { border: 0; background: var(--c-surface); padding: 6px 12px; cursor: pointer; }
.seg button[aria-pressed='true'] { background: var(--c-primary); color: var(--c-primary-ink); }
.filter-panel { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: var(--space-3); }
.filter-panel .chipbtn { border: 1px solid var(--c-border); background: var(--c-surface); border-radius: 999px; padding: 3px 12px; cursor: pointer; font-size: 13px; }
.filter-panel .chipbtn[aria-pressed='true'] { background: var(--c-primary); color: var(--c-primary-ink); border-color: var(--c-primary); }

/* ---------- 排程（甘特） ---------- */
.gantt-wrap { overflow: auto; border: 1px solid var(--c-border); border-radius: var(--radius-md); background: var(--c-surface); max-height: 72vh; position: relative; }
.gantt { position: relative; }
.g-head { position: sticky; top: 0; z-index: 5; background: var(--c-surface-2); display: flex; flex-direction: column; border-bottom: 1px solid var(--c-border); }
.g-months, .g-days { position: relative; height: 22px; }
.g-months span { position: absolute; top: 0; height: 22px; padding: 2px 0 2px 0; font-size: 12px; font-weight: 600; border-left: 1px solid var(--c-border); white-space: nowrap; }
.g-months span b { display: inline-block; padding: 0 4px; background: var(--c-surface-2); }
.g-days span { position: absolute; top: 0; height: 22px; text-align: center; font-size: 10px; line-height: 22px; color: var(--c-text-dim); }
.g-corner { position: sticky; left: 0; z-index: 6; background: var(--c-surface-2); }
.g-row { position: relative; border-bottom: 1px solid var(--c-border); }
.g-label {
  position: sticky; left: 0; z-index: 4; height: 100%; background: var(--c-surface); border-right: 1px solid var(--c-border);
  display: flex; align-items: center; gap: 4px; padding: 0 4px 0 8px; overflow: hidden;
}
.g-floorhead .g-label { background: var(--c-surface-2); font-weight: 700; gap: 6px; }
.g-floorhead { background: var(--c-surface-2); }
.g-corner { font-size: 12px; font-weight: 700; padding: 14px 8px; }
.g-floor { cursor: default; }
.g-floor .cnt { font-size: 11px; font-weight: 400; color: var(--c-text-dim); }
.g-floor .fl-add { margin-left: auto; width: 24px; height: 24px; padding: 0; border: 1px solid var(--c-border); border-radius: 50%; background: var(--c-surface); cursor: pointer; line-height: 1; }
.g-name { flex: 1; min-width: 0; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: text; padding: 2px 4px; border-radius: 4px; }
.g-name:hover { background: var(--c-surface-2); }
.g-rename { width: 100%; min-height: 28px !important; padding: 2px 6px !important; font-size: 13px !important; }
.g-del { flex: 0 0 auto; width: 22px; height: 22px; padding: 0; border: 0; background: transparent; color: var(--c-text-dim); cursor: pointer; border-radius: 50%; font-size: 16px; line-height: 1; }
.g-del:hover { background: var(--c-danger); color: #fff; }
.g-grid { position: absolute; top: 0; bottom: 0; pointer-events: none; background-image: var(--wk); background-size: calc(var(--zoom) * 7) 100%; background-repeat: repeat-x; }
.g-row.drop-hot .g-label { background: color-mix(in srgb, var(--c-primary) 22%, var(--c-surface)); }
.g-row.drop-hot .g-grid { box-shadow: inset 0 0 0 100vmax color-mix(in srgb, var(--c-primary) 10%, transparent); }
.g-today { position: absolute; width: 2px; bottom: 0; background: var(--c-danger); z-index: 3; pointer-events: none; }
/* 工項庫 */
.pal { margin-bottom: var(--space-3); padding: var(--space-2) var(--space-3); }
.pal-head { display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap; margin-bottom: 6px; }
.pal-body { display: flex; flex-wrap: wrap; gap: 6px 10px; }
.pal-group { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 4px; max-width: 100%; padding: 4px 8px; border-radius: 18px; background: var(--c-surface-2); } /* 工項多時在群組內換行，不超出卡片 */
.pal-parent { font-size: 12px; color: var(--c-text-dim); padding: 0 2px; white-space: nowrap; }
.pal-chip {
  border: 1px solid var(--c-border); background: var(--c-surface); border-radius: 999px; padding: 4px 12px; font-size: 13px; cursor: grab; white-space: nowrap;
  touch-action: none; user-select: none; -webkit-user-select: none;
}
.pal-chip.armed { background: var(--c-primary); border-color: var(--c-primary); color: var(--c-primary-ink); }
.pal-ghost { position: fixed; left: 0; top: 0; z-index: 100; pointer-events: none; background: var(--c-primary); color: var(--c-primary-ink); padding: 6px 14px; border-radius: 999px; font-size: 14px; box-shadow: var(--shadow-md); opacity: 0.92; }
/* 進度條：中間＝移動整條；兩端各一個看得見的白色把手＝只改那一端。
   把手的感應區（.h）跨在條的邊緣上：往外 --out、往內最多 --inmax，高度＝整列；條很短時往內縮到 0（全放在外側），中間永遠留得出可以拖動整條的空間。 */
.g-bar {
  --out: 12px; --inmax: 10px;
  --in: clamp(0px, calc(var(--w, 90px) / 3 - 4px), var(--inmax));
  position: absolute; height: 26px; border-radius: 5px; background: var(--c-primary); color: var(--c-primary-ink);
  font-size: 12px; line-height: 26px;
  cursor: grab; z-index: 2; box-shadow: var(--shadow-sm); touch-action: none; user-select: none;
}
.g-bar-t { display: block; height: 100%; padding: 0 calc(var(--in) + 7px); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; border-radius: 5px; pointer-events: none; }
.g-bar.dragging { cursor: grabbing; opacity: 0.88; z-index: 7; }
.g-bar .h { position: absolute; top: -5px; bottom: -5px; width: calc(var(--out) + var(--in)); cursor: ew-resize; z-index: 1; }
.g-bar .h.l { left: calc(-1 * var(--out)); } .g-bar .h.r { right: calc(-1 * var(--out)); }
/* 控制點本體：白色圓角小把手，正好壓在條的邊緣上 */
.g-bar .h::after {
  content: ''; position: absolute; top: 50%; width: 6px; height: 18px; margin-top: -9px; box-sizing: border-box;
  border-radius: 3px; background: #fff; border: 1px solid rgba(0, 0, 0, 0.45); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
  transition: height 0.1s, margin-top 0.1s;
}
.g-bar .h.l::after { left: calc(var(--out) - 3px); } .g-bar .h.r::after { right: calc(var(--out) - 3px); }
.g-bar .h:hover::after, .g-bar[data-mode='l'] .h.l::after, .g-bar[data-mode='r'] .h.r::after { height: 26px; margin-top: -13px; box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.55); }
/* 拖曳中：在條上方即時顯示新日期與天數 */
.g-bar.dragging[data-tip]::before {
  content: attr(data-tip); position: absolute; left: 50%; bottom: calc(100% + 5px); transform: translateX(-50%); z-index: 8;
  padding: 2px 8px; border-radius: 6px; background: var(--c-text); color: var(--c-bg); font-size: 12px; line-height: 18px; white-space: nowrap; box-shadow: var(--shadow-md); pointer-events: none;
}
/* 手指：把手更大（外 18px＋內最多 14px、控制點更粗） */
@media (pointer: coarse), (max-width: 720px) {
  .g-bar { --out: 18px; --inmax: 14px; }
  .g-bar .h::after { width: 8px; height: 22px; margin-top: -11px; }
  .g-bar .h.l::after { left: calc(var(--out) - 4px); } .g-bar .h.r::after { right: calc(var(--out) - 4px); }
}

/* ---------- 設定 ---------- */
.set-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: var(--space-4); align-items: start; }
.set-card h3 { margin-bottom: var(--space-3); display: flex; justify-content: space-between; align-items: center; }
.sort-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.sort-list li { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border: 1px solid var(--c-border); border-radius: var(--radius-sm); background: var(--c-surface); }
.sort-list .handle { cursor: grab; color: var(--c-text-dim); touch-action: none; padding: 0 4px; }
.sort-list .nm { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.sort-list .sub { margin: 6px 0 0 22px; }
.sort-list li.has-sub { display: block; }
.sort-list li.has-sub > .rowtop { display: flex; align-items: center; gap: 8px; }
.sortable-ghost { opacity: 0.4; }
.add-row { display: flex; gap: 6px; margin-top: var(--space-3); }

@media print { .topbar, .tabs, .toolbar, .toasts { display: none !important; } }

/* ---------- 設定頁：區塊跳轉列 ---------- */
.jumpbar {
  position: sticky; top: var(--topbar-h, 56px); z-index: 20; display: flex; gap: 6px; overflow-x: auto;
  padding: 8px 0; margin: 0 0 var(--space-3); background: var(--c-bg); scrollbar-width: none;
}
.jumpbar::-webkit-scrollbar { display: none; }
.jumpbar button {
  flex: 0 0 auto; border: 1px solid var(--c-border); background: var(--c-surface); border-radius: 999px;
  padding: 6px 14px; cursor: pointer; font-size: 14px; white-space: nowrap;
}
.jumpbar button.on { background: var(--c-primary); border-color: var(--c-primary); color: var(--c-primary-ink); font-weight: 600; }
.set-card { scroll-margin-top: calc(var(--topbar-h, 56px) + 64px); }
.set-section-title { margin: var(--space-4) 0 var(--space-2); font-size: 15px; color: var(--c-text-dim); }

/* ---------- 手機優化 ---------- */
.mobile-only { display: none; }
.toolbar input[type='date'] { flex: 1 1 130px; min-width: 0; }
@media (max-width: 720px) {
  .mobile-only { display: inline-flex; }
  /* iOS Safari：輸入框字級 < 16px 會在聚焦時自動放大整頁 */
  input[type='text'], input[type='search'], input[type='date'], input[type='number'], select, textarea { font-size: 16px; }
  .btn { min-height: 40px; }
  .btn.sm { min-height: 38px; padding: 6px 12px; }
  .icon-btn { min-width: 44px; min-height: 44px; }
  .modal-back { place-items: end center; padding: 0; }
  .modal { width: 100%; max-height: 92dvh; border-radius: var(--radius-lg) var(--radius-lg) 0 0; padding: var(--space-4); padding-bottom: calc(var(--space-4) + env(safe-area-inset-bottom)); }
  .modal .actions .btn { flex: 1; }
  /* 紀錄：篩選收合、新增鈕改為右下浮動 */
  #r-add, #d-add { position: fixed; right: 16px; bottom: calc(76px + env(safe-area-inset-bottom)); z-index: 35; width: 56px; height: 56px; padding: 0; justify-content: center; font-size: 26px; border-radius: 50%; box-shadow: var(--shadow-md); }
  #r-add span, #d-add span { display: none; }
  #tab-records .toolbar:first-child .grow, #tab-defects .toolbar:first-child .grow { flex-basis: 100%; }
  .d-actions .btn { flex: 1; }
  main { padding-left: 12px; padding-right: 12px; }
  .thumbs img, .thumbs video { width: calc((100vw - 24px - 2px - 32px - 14px) / 3); height: calc((100vw - 24px - 2px - 32px - 14px) / 3); max-width: 120px; max-height: 120px; }
  .th-dl { width: 34px; height: 34px; line-height: 34px; font-size: 17px; right: 4px; bottom: 4px; }
  .entry-actions .btn { flex: 1; }
  .sort-list li { flex-wrap: wrap; }
  .set-grid { grid-template-columns: 1fr; }
  .toasts { bottom: calc(140px + env(safe-area-inset-bottom)); }
  .batchbar { bottom: calc(70px + env(safe-area-inset-bottom)); }
}

.scrollrow { flex-wrap: nowrap; overflow-x: auto; max-width: 100%; scrollbar-width: none; }
.scrollrow::-webkit-scrollbar { display: none; }
.scrollrow .chipbtn { flex: 0 0 auto; }
#p-floors-bar { flex-wrap: nowrap; min-width: 0; }
#p-floors-bar .filter-panel { min-width: 0; }
.unit-group { border: 1px solid var(--c-border); border-radius: var(--radius-sm); margin-bottom: 6px; background: var(--c-surface); }
.unit-group > summary { cursor: pointer; padding: 8px 10px; font-weight: 600; list-style-position: inside; }
.unit-group > summary .muted { font-weight: 400; margin-left: 6px; }
.unit-group .sort-list { padding: 0 8px 8px; }
#p-floors-bar > .muted { white-space: nowrap; }

/* ---------- 進度總覽：一戶一個 chip、一層樓一列 ---------- */
.p-bld-title { margin: var(--space-4) 0 var(--space-2); font-size: 16px; }
.p-floor { margin-bottom: 6px; }
.p-floor-cap { font-size: 12px; font-weight: 700; color: var(--c-text-dim); margin: 0 0 2px 2px; }
.unit-chips { display: grid; grid-template-columns: repeat(var(--n, 6), minmax(0, 1fr)); gap: 6px; max-width: 760px; }
.unit-chip {
  display: grid; gap: 0; justify-items: center; align-content: center; min-height: 54px; padding: 4px 2px; min-width: 0;
  border: 1px solid var(--c-border); border-radius: var(--radius-md); background: var(--c-surface); cursor: pointer; text-align: center;
  transition: transform 0.1s, background 0.15s; user-select: none; -webkit-tap-highlight-color: transparent;
}
.unit-chip:hover { filter: brightness(0.96); }
.unit-chip:active { transform: scale(0.95); }
.unit-chip:disabled { opacity: 0.4; cursor: not-allowed; }
.unit-chip b { font-size: 15px; line-height: 1.2; }
.unit-chip .st { font-size: 13px; color: var(--c-text-dim); font-variant-numeric: tabular-nums; line-height: 1.3; }
.unit-chip[data-s='doing'] { background: color-mix(in srgb, var(--c-warn) 28%, var(--c-surface)); }
.unit-chip[data-s='done'] { background: color-mix(in srgb, var(--c-ok) 28%, var(--c-surface)); }
.unit-chip[data-s='done'] .st { color: var(--c-ok); font-weight: 700; }
.unit-chip[data-s='na'] { background: repeating-linear-gradient(45deg, transparent 0 6px, var(--c-surface-2) 6px 12px); }
@media (max-width: 720px) { .unit-chip b { font-size: 14px; } .unit-chip { min-height: 50px; } .unit-chips { gap: 4px; } }
.p-work { margin-bottom: 4px; }
.p-work-sticky { position: sticky; top: var(--topbar-h, 56px); z-index: 15; background: var(--c-bg); padding-top: 4px; }

.p-work-lbl { min-width: 4.2em; white-space: nowrap; }
.p-work .toolbar { margin-bottom: 4px; flex-wrap: nowrap; }
.chipbtn.child { font-size: 13px; padding: 2px 10px; border-style: dashed; }
.chipbtn.parent { font-weight: 600; }

/* 整層批次按鈕 */
.p-floor-cap { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.floor-bulk { display: inline-flex; gap: 3px; }
.floor-bulk button {
  min-width: 34px; height: 26px; padding: 0 6px; border: 1px solid var(--c-border); border-radius: 999px; background: var(--c-surface);
  font-size: 13px; line-height: 1; cursor: pointer; color: var(--c-text);
}
.floor-bulk button:hover { background: var(--c-surface-2); }
.floor-bulk button[data-bulk='done']:hover { background: color-mix(in srgb, var(--c-ok) 28%, var(--c-surface)); }
.floor-bulk button[data-bulk='doing']:hover { background: color-mix(in srgb, var(--c-warn) 28%, var(--c-surface)); }
@media (max-width: 720px) { .floor-bulk button { min-width: 40px; height: 30px; } }

/* 棟別／樓層設定 */
.bld { border: 1px solid var(--c-border); border-radius: var(--radius-md); padding: var(--space-3); margin-bottom: var(--space-2); background: var(--c-surface); }
.bld-head { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.bld-fields { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-2); }
.bld-fields .bld-codes { grid-column: 1 / -1; }
.bld-actions { display: flex; gap: 8px; margin-top: 8px; justify-content: flex-end; }
.bulk-lbl { font-size: 12px; align-self: center; margin-right: 2px; }

/* 文字輸入視窗：永遠在畫面（可見區域）正中間，不是手機版的底部面板 */
.modal-back.center { place-items: center; padding: var(--space-3); z-index: 75; }
.modal-back.center .modal { width: min(520px, 100%); border-radius: var(--radius-lg); padding: var(--space-4) var(--space-5); max-height: 100%; }
.text-popup h2 { font-size: 16px; }
.text-popup textarea { min-height: 140px; }
.text-popup input, .text-popup textarea { font-size: 16px; }

/* 區域 → 工項：每個範圍畫面上只有一張戶別 chip 表，點工項就在同一張表裡換成該工項 */
.p-scope { margin-bottom: var(--space-4); }
/* 區域列＋工項列：一起黏在頂欄下方，捲到哪都能直接換區域、換工項 */
.p-nav { position: sticky; top: var(--topbar-h, 56px); z-index: 15; background: var(--c-bg); padding-bottom: 2px; }
.p-zone-bar, .p-item-bar { display: flex; align-items: center; gap: var(--space-2); padding: 6px 0; flex-wrap: nowrap; }
.p-item-bar { padding-top: 0; }
.p-nav .p-work-lbl { min-width: 5em; } /* 兩列標籤同寬，下面的 chip 才會對齊 */
.p-item-bar .chipbtn { white-space: nowrap; }
/* 被選中的工項：高亮成主色實心（沿用區域按鈕的選中樣式；工項按鈕平常是虛線框，選中時改實線）。不改粗細：不然選中時按鈕變寬，旁邊的會被推動 */
.filter-panel .chipbtn.child[aria-pressed='true'] { border-style: solid; }
/* 矮畫面（小螢幕手機、橫放）：一樣要固定，只是頂欄與兩排的間距縮緊，把空間留給內容（絕對不能因為矮就不固定） */
@media (max-height: 560px) {
  .topbar { padding-top: 4px; padding-bottom: 4px; }
  .p-zone-bar, .p-item-bar { padding: 2px 0; }
  .p-nav { padding-bottom: 0; }
}
.p-item { border: 1px solid var(--c-border); border-radius: var(--radius-md); background: var(--c-surface); margin: 0 0 var(--space-3); padding: 0 var(--space-3) var(--space-2); }
.p-item-head { padding: 10px 0; display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.p-item-head b { font-size: 16px; }
.p-bld-title { margin: var(--space-2) 0 4px; font-size: 14px; color: var(--c-text-dim); }

/* ---------- 缺失：左右並排「改善前｜改善後」 ---------- */
.chip.s-open { background: color-mix(in srgb, var(--c-warn) 24%, transparent); color: color-mix(in srgb, var(--c-warn) 70%, var(--c-text)); }
.chip.s-fixing { background: color-mix(in srgb, var(--c-info) 18%, transparent); color: var(--c-info); }
.chip.s-done { background: color-mix(in srgb, var(--c-ok) 18%, transparent); color: var(--c-ok); }
/* 寬螢幕：卡片排成多欄（每張至少 420px），照片才不會被放得過大；手機（< 420px）自動變單欄 */
#d-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 420px), 1fr)); gap: var(--space-3); align-items: start; }
#d-list > .empty { grid-column: 1 / -1; }
.defect { display: grid; gap: 8px; margin: 0; border-left: 4px solid var(--c-border); }
.defect[data-status='done'] { border-left-color: var(--c-ok); }
.defect:not([data-status='done'])[data-sev='severe'] { border-left-color: var(--c-danger); }
.defect:not([data-status='done'])[data-sev='normal'] { border-left-color: var(--c-warn); }
.defect:not([data-status='done'])[data-sev='minor'] { border-left-color: var(--c-info); }
.d-head { display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap; }
.d-head b { font-size: 16px; }
.d-head .chip { margin-left: auto; }
.d-sev { font-size: 12px; font-weight: 700; padding: 1px 8px; border-radius: 999px; background: var(--c-surface-2); color: var(--c-text-dim); }
.d-sev[data-sev='severe'] { background: color-mix(in srgb, var(--c-danger) 18%, transparent); color: var(--c-danger); }
.d-sev[data-sev='normal'] { background: color-mix(in srgb, var(--c-warn) 24%, transparent); color: color-mix(in srgb, var(--c-warn) 70%, var(--c-text)); }
.d-sev[data-sev='minor'] { background: color-mix(in srgb, var(--c-info) 18%, transparent); color: var(--c-info); }
.d-date { margin-top: -4px; }
.d-desc { white-space: pre-wrap; word-break: break-word; }
.d-compare { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-2); }
.d-col { margin: 0; min-width: 0; }
.d-col figcaption { font-size: 12px; font-weight: 700; color: var(--c-text-dim); margin-bottom: 4px; }
.d-col.after figcaption { color: var(--c-ok); }
.d-ph { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px; }
.d-ph.n1 { grid-template-columns: minmax(0, 1fr); }
.d-img { position: relative; display: block; padding: 0; border: 1px solid var(--c-border); border-radius: var(--radius-sm); overflow: hidden; background: var(--c-surface-2); cursor: zoom-in; aspect-ratio: 1; }
.d-ph.n1 .d-img { aspect-ratio: 4 / 3; }
.d-img img { display: block; width: 100%; height: 100%; object-fit: cover; }
.d-more { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(0, 0, 0, 0.5); color: #fff; font-weight: 700; font-size: 18px; }
.d-none { display: grid; place-items: center; min-height: 72px; padding: 8px; border: 1px dashed var(--c-border); border-radius: var(--radius-sm); color: var(--c-text-dim); font-size: 13px; text-align: center; }
.d-plan { padding: 8px 10px; border-radius: var(--radius-sm); background: var(--c-surface-2); }
.d-plan-lbl { display: block; margin-bottom: 2px; font-size: 12px; font-weight: 700; color: var(--c-text-dim); }
.d-plan-txt { white-space: pre-wrap; word-break: break-word; }
.d-plan.none .d-plan-txt { color: var(--c-text-dim); }
.d-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.d-photo-btns { display: flex; gap: 6px; margin-top: 6px; }
.d-fix { border-top: 1px solid var(--c-border); padding-top: var(--space-2); }
.d-fix > summary { cursor: pointer; font-weight: 600; padding: 6px 0; margin-bottom: var(--space-2); }
.modal .field > .seg { justify-self: start; }
#tab-defects .toolbar select { max-width: 46%; }

/* 程式捲動（scrollIntoView 等）要避開固定的頂欄與底部導覽列 */
html { scroll-padding-top: 72px; scroll-padding-bottom: calc(88px + env(safe-area-inset-bottom)); }
