/* 君震營造內部管理系統　版面
   所有顏色、間距、圓角一律取自 tokens.css，不寫死色值。
   例外：列印樣式（目標介質固定是白紙）。 */

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

html, body { height: 100%; }
body {
  margin: 0;
  background: var(--concrete);
  color: var(--rebar);
  font-family: var(--font-sans);
  font-size: 13px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}
a { color: var(--blueprint); text-decoration: none; }
a:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--blueprint); outline-offset: 2px; }
img { max-width: 100%; }
[hidden] { display: none !important; }

/* ── 外框 ───────────────────────────────────────────────────────── */
.shell { display: grid; grid-template-rows: auto 1fr; min-height: 100%; }

.topbar {
  display: flex; align-items: center; gap: var(--space-4);
  padding: 0 var(--space-4);
  height: 52px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 10;
}
.topbar .brand { font-size: 15px; font-weight: 600; white-space: nowrap; }
.topbar .brand small { font-weight: 400; color: var(--rebar-muted); margin-left: var(--space-2); }
.topbar .spacer { flex: 1; }
.topbar .who { font-size: 12px; color: var(--rebar-muted); text-align: right; white-space: nowrap; }
.topbar .who b { display: block; color: var(--rebar); font-size: 13px; font-weight: 600; }

.body { display: grid; grid-template-columns: 184px 1fr; min-height: 0; }

/* ── 導覽 ───────────────────────────────────────────────────────── */
.nav {
  background: var(--paper);
  border-right: 1px solid var(--line);
  padding: var(--space-3) 0;
  display: flex; flex-direction: column; gap: 2px;
}
.nav a, .nav span.soon {
  display: flex; align-items: center; gap: var(--space-2);
  padding: 0 var(--space-4); height: var(--touch-min);
  color: var(--rebar); font-size: 13px; text-decoration: none;
}
.nav a:hover { background: var(--stripe); text-decoration: none; }
.nav a[aria-current="page"] {
  background: var(--blueprint-soft);
  box-shadow: inset 3px 0 0 var(--blueprint);
  font-weight: 600;
}
.nav span.soon { color: var(--rebar-muted); cursor: default; }
.nav span.soon em { font-style: normal; margin-left: auto; font-size: 11px; }
.nav .group {
  padding: var(--space-4) var(--space-4) var(--space-2);
  font-size: 11px; color: var(--rebar-muted);
}
.nav .dot { width: 8px; height: 8px; flex: none; border-radius: 50%; background: var(--line); }
.nav a[aria-current="page"] .dot { background: var(--blueprint); }

main { padding: var(--space-6) var(--space-4) var(--space-8); min-width: 0; }
.page { max-width: 1120px; }
.page > header { margin-bottom: var(--space-6); }
h1 { margin: 0; font-size: 24px; line-height: 1.3; font-weight: 600; }
.page > header p { margin: var(--space-2) 0 0; color: var(--rebar-muted); font-size: 13px; max-width: 64ch; }
h2 { margin: 0 0 var(--space-3); font-size: 18px; line-height: 1.4; font-weight: 600; }

.section { margin-bottom: var(--space-8); }
.section > header { display: flex; align-items: baseline; gap: var(--space-3); margin-bottom: var(--space-3); }
.section > header h2 { margin: 0; }
.section > header .count { font-size: 12px; color: var(--rebar-muted); }

/* ── 面板與空狀態 ───────────────────────────────────────────────── */
.panel {
  background: var(--paper); border: 1px solid var(--line);
  border-radius: var(--radius-panel);
}
.panel.pad { padding: var(--space-4); }
.empty { padding: var(--space-6) var(--space-4); text-align: center; color: var(--rebar-muted); }
.empty b { display: block; color: var(--rebar); font-size: 15px; font-weight: 600; margin-bottom: var(--space-2); }
.empty span { font-size: 12px; }

/* ── 數量磚（只給真的是重點的數字） ─────────────────────────────── */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(132px, 1fr)); gap: 1px;
         background: var(--line); border: 1px solid var(--line); border-radius: var(--radius-panel);
         overflow: hidden; }
.tiles .tile { background: var(--paper); padding: var(--space-3) var(--space-4); }
.tiles .tile .n { font-family: var(--font-mono); font-variant-numeric: tabular-nums;
                  font-size: 24px; line-height: 1.2; font-weight: 500; }
.tiles .tile .l { font-size: 12px; color: var(--rebar-muted); margin-top: var(--space-1); }

/* ── 表格 ───────────────────────────────────────────────────────── */
.scroll { overflow-x: auto; }
table.data { border-collapse: collapse; width: 100%; font-size: 13px; }
table.data thead th {
  position: sticky; top: 0; z-index: 1;
  background: var(--paper); text-align: left; font-weight: 600;
  height: var(--table-row); padding: 0 var(--space-3);
  border-bottom: 1px solid var(--line); white-space: nowrap;
}
table.data thead th:first-child, table.data tbody td:first-child { padding-left: var(--space-4); }
table.data tbody td {
  height: var(--table-row); padding: 0 var(--space-3);
  border-bottom: 1px solid var(--line);
}
table.data tbody tr:nth-child(odd) td { background: var(--stripe); }
table.data .num { text-align: right; }
table.data .mid { text-align: center; }
table.data .nowrap { white-space: nowrap; }
.mono { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
.muted { color: var(--rebar-muted); }

/* ── 狀態標籤 ───────────────────────────────────────────────────── */
.pill {
  display: inline-flex; align-items: center; gap: var(--space-1);
  height: 24px; padding: 0 var(--space-2);
  border: 1px solid; border-radius: var(--radius-control);
  font-size: 12px; white-space: nowrap;
}
.pill.late { color: var(--hazard-ink); border-color: var(--hazard); }
.pill.warn { color: var(--warn-ink);   border-color: var(--warn); }
.pill.ok   { color: var(--ok);         border-color: var(--ok); }
.pill.wait { color: var(--blueprint);  border-color: var(--blueprint); }
.pill.done { color: var(--rebar-muted);border-color: var(--line); }

/* ── 按鈕 ───────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  height: var(--touch-min); padding: 0 var(--space-4);
  border: 1px solid transparent; border-radius: var(--radius-control);
  font-family: inherit; font-size: 13px; cursor: pointer; text-decoration: none;
}
.btn:hover { text-decoration: none; }
.btn.primary   { background: var(--blueprint); color: var(--paper); }
.btn.secondary { background: transparent; color: var(--blueprint); border-color: var(--blueprint); }
.btn.quiet     { background: transparent; color: var(--rebar-muted); border-color: var(--line); }
.btn.danger    { background: transparent; color: var(--hazard-ink); border-color: var(--hazard); }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.btn.field {
  width: 100%; height: var(--field-button);
  background: var(--hivis); color: var(--rebar);
  font-size: 17px; font-weight: 600; border: 0;
}
.btnrow { display: flex; flex-wrap: wrap; gap: var(--space-2); }

/* ── 表單 ───────────────────────────────────────────────────────── */
form.stack { display: flex; flex-direction: column; gap: var(--space-4); }
.fld { display: flex; flex-direction: column; gap: var(--space-2); }
.fld label { font-size: 12px; color: var(--rebar-muted); }
.fld input, .fld select, .fld textarea {
  height: var(--touch-min); padding: 0 var(--space-3);
  background: var(--paper); color: var(--rebar);
  border: 1px solid var(--line); border-radius: var(--radius-control);
  font-family: inherit; font-size: 15px; width: 100%;
}
.fld textarea { height: auto; padding: var(--space-3); resize: vertical; }
.fld input.mono { font-family: var(--font-mono); }
.fld .hint { font-size: 12px; color: var(--rebar-muted); }
.notice {
  padding: var(--space-3); border-radius: var(--radius-control);
  font-size: 13px; border: 1px solid;
}
.notice.bad  { color: var(--hazard-ink); border-color: var(--hazard); background: var(--paper); }
.notice.good { color: var(--ok);         border-color: var(--ok);     background: var(--paper); }
.notice.info { color: var(--rebar);      border-color: var(--line);   background: var(--paper); }

/* ── 登入頁 ─────────────────────────────────────────────────────── */
.auth { min-height: 100%; display: grid; place-items: center; padding: var(--space-4); }
.auth .card {
  width: 100%; max-width: 380px;
  background: var(--paper); border: 1px solid var(--line);
  border-radius: var(--radius-panel); padding: var(--space-6);
}
.auth .mark { margin-bottom: var(--space-6); }
.auth .mark h1 { font-size: 20px; }
.auth .mark p { margin: var(--space-2) 0 0; font-size: 12px; color: var(--rebar-muted); }
.auth .strata { display: block; margin-bottom: var(--space-4); }

/* ── 權限矩陣 ───────────────────────────────────────────────────── */
table.matrix { border-collapse: collapse; font-size: 12px; width: 100%; }
table.matrix th, table.matrix td {
  border: 1px solid var(--line); padding: var(--space-2) var(--space-3);
  text-align: left; white-space: nowrap;
}
table.matrix thead th { background: var(--stripe); font-weight: 600; }
table.matrix td.mid { text-align: center; }
table.matrix .scope-all  { color: var(--ok); font-weight: 600; }
table.matrix .scope-self { color: var(--blueprint); }
table.matrix .scope-none { color: var(--line); }
table.matrix tbody th { background: var(--paper); font-weight: 400; }

/* ── 手機 ───────────────────────────────────────────────────────── */
@media (max-width: 760px) {
  .body { grid-template-columns: 1fr; }
  .nav {
    border-right: 0; border-bottom: 1px solid var(--line);
    flex-direction: row; overflow-x: auto; padding: 0 var(--space-2); gap: 0;
  }
  .nav .group { display: none; }
  .nav span.soon { display: none; }
  .nav a { white-space: nowrap; padding: 0 var(--space-3); }
  .nav a[aria-current="page"] { box-shadow: inset 0 -3px 0 var(--blueprint); }
  main { padding: var(--space-4) var(--space-4) var(--space-8); }
  .topbar .brand small { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* ── 列印 ───────────────────────────────────────────────────────── */
@media print {
  .topbar, .nav, .btnrow, .btn { display: none !important; }
  body { background: #ffffff; color: #2b3238; }
  .body { grid-template-columns: 1fr; }
  main { padding: 0; }
  .panel { border: 1px solid #2b3238; border-radius: 0; }
  table.data thead { display: table-header-group; }
  table.data tbody tr { break-inside: avoid; }
  a { color: #2b3238; text-decoration: none; }
}

/* ── 縮圖牆（UI.md 業內慣例 #11：附件是掃描件，要能快速翻） ──────────── */
.thumbwall {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(148px, 1fr));
  gap: var(--space-3);
}
.thumb { margin: 0; background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius-panel); overflow: hidden; }
.thumb .shot {
  display: block; width: 100%; aspect-ratio: 4 / 3; padding: 0;
  border: 0; border-bottom: 1px solid var(--line);
  background: var(--stripe); cursor: pointer; overflow: hidden;
}
.thumb .shot img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb .shot .ext {
  display: grid; place-items: center; width: 100%; height: 100%;
  font-size: 15px; font-weight: 500; color: var(--rebar-muted);
}
.thumb figcaption { padding: var(--space-2) var(--space-3); display: block; }
.thumb .fn {
  display: block; font-size: 12px; line-height: 1.4;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.thumb .meta { display: block; margin-top: var(--space-1); font-size: 11px; color: var(--rebar-muted); }
.thumb .meta .pill { height: 20px; font-size: 11px; margin-top: var(--space-1); }

/* ── 放大檢視 ───────────────────────────────────────────────────────── */
.lightbox {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(43, 50, 56, .94);
  display: grid; grid-template-columns: 48px 1fr 48px;
  grid-template-rows: 52px 1fr 36px; align-items: center;
}
.lightbox .lb-bar {
  grid-column: 1 / -1; grid-row: 1;
  display: flex; align-items: center; gap: var(--space-3);
  padding: 0 var(--space-4) 0 var(--space-4);
  padding-top: env(safe-area-inset-top, 0px);
  background: var(--paper); border-bottom: 1px solid var(--line);
}
.lightbox .lb-name { flex: 1; font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lightbox .lb-pos { font-size: 12px; color: var(--rebar-muted); }
.lightbox .lb-stage { grid-column: 2; grid-row: 2; display: grid; place-items: center; min-height: 0; padding: var(--space-3); }
.lightbox .lb-stage img { max-width: 100%; max-height: 100%; object-fit: contain; background: var(--paper); }
.lightbox .lb-nav {
  grid-row: 2; height: 96px; width: 40px;
  background: var(--paper); color: var(--rebar);
  border: 1px solid var(--line); border-radius: var(--radius-control);
  font-size: 24px; line-height: 1; cursor: pointer;
}
.lightbox .lb-nav.prev { grid-column: 1; justify-self: start; margin-left: var(--space-1); }
.lightbox .lb-nav.next { grid-column: 3; justify-self: end; margin-right: var(--space-1); }
.lightbox .lb-hint {
  grid-column: 1 / -1; grid-row: 3; text-align: center;
  font-size: 11px; color: var(--line);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
@media (max-width: 760px) {
  .lightbox { grid-template-columns: 40px 1fr 40px; }
  .lightbox .lb-nav { height: 72px; width: 32px; }
}
@media print { .lightbox { display: none !important; } }

/* ── 一覽表 ＋ 明細雙欄（UI.md §7.2：左選右看，不跳頁）────────────────── */
.twopane { display: grid; grid-template-columns: minmax(0,1fr) var(--detail-panel); gap: var(--space-4); align-items: start; }
.twopane > .listcol { min-width: 0; }
.twopane > .detailcol { position: sticky; top: calc(52px + var(--space-4)); }
@media (max-width: 1180px) {
  .twopane { grid-template-columns: 1fr; }
  .twopane > .detailcol { position: static; }
}

table.data tbody tr.row-sel td { background: var(--blueprint-soft); }
table.data tbody tr.row-sel td:first-child { box-shadow: inset 3px 0 0 var(--blueprint); }
table.data tbody tr.row-late td:first-child { box-shadow: inset 3px 0 0 var(--hazard); }
table.data tbody tr.row-warn td:first-child { box-shadow: inset 3px 0 0 var(--warn); }
table.data tbody tr.row-sel.row-late td:first-child,
table.data tbody tr.row-sel.row-warn td:first-child { box-shadow: inset 3px 0 0 var(--blueprint); }
table.data tbody tr[data-href] { cursor: pointer; }
table.data tbody tr[data-href]:hover td { background: var(--stripe); }
table.data tbody tr.row-sel[data-href]:hover td { background: var(--blueprint-soft); }
table.data a.plate { color: var(--rebar); text-decoration: none; }

.legend { display: flex; flex-wrap: wrap; gap: var(--space-4); margin-top: var(--space-3);
          font-size: 12px; color: var(--rebar-muted); }
.legend .s-late { color: var(--hazard-ink); } .legend .s-warn { color: var(--warn-ink); }
.legend .s-ok { color: var(--ok); }

/* 明細面板 */
.detail { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius-panel); }
.detail > header { padding: var(--space-3) var(--space-4); border-bottom: 1px solid var(--line); }
.detail > header h2 { margin: 0; font-size: 18px; }
.detail > header .sub { font-size: 12px; color: var(--rebar-muted); margin-top: var(--space-1); }
.detail section { padding: var(--space-3) var(--space-4); border-bottom: 1px solid var(--line); }
.detail section:last-child { border-bottom: 0; }
.detail section > h3 { margin: 0 0 var(--space-2); font-size: 13px; font-weight: 600;
                       display: flex; align-items: baseline; gap: var(--space-2); }
.detail section > h3 .n { font-size: 11px; color: var(--rebar-muted); font-weight: 400; }
.kv { display: grid; grid-template-columns: 76px 1fr; gap: var(--space-1) var(--space-2); font-size: 13px; }
.kv dt { color: var(--rebar-muted); } .kv dd { margin: 0; }
.minirow { display: flex; justify-content: space-between; gap: var(--space-2);
           padding: var(--space-1) 0; font-size: 12px; border-bottom: 1px dotted var(--line); }
.minirow:last-child { border-bottom: 0; }
.minirow .r { text-align: right; white-space: nowrap; }
details.sub > summary { cursor: pointer; font-size: 12px; color: var(--blueprint); padding: var(--space-1) 0; }
details.sub[open] > summary { margin-bottom: var(--space-1); }
.addform { margin-top: var(--space-2); padding-top: var(--space-2); border-top: 1px dotted var(--line); }
.addform .grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-2); }
.addform input, .addform select, .addform textarea { height: 34px; font-size: 13px; padding: 0 var(--space-2); }
.addform textarea { height: auto; padding: var(--space-2); }
.addform label { font-size: 11px; }
.addform .btn { height: 34px; padding: 0 var(--space-3); font-size: 12px; }
/* 但不能壓到現場人員的主按鈕 —— 它是 56px 反光檸檬黃，是整個手機流程的終點。
   .addform .btn 的特異性高於 .btn.field，所以必須在這裡明寫回來。 */
.addform .btn.field { height: var(--field-button); font-size: 17px; padding: 0 var(--space-4); }
.covrow { display: grid; grid-template-columns: 1fr 1fr 80px; gap: var(--space-2); margin-bottom: var(--space-1); }

/* ══════════════════════════════════════════════════════════════════════════
   手機版面（附錄一 1-3）
   ══════════════════════════════════════════════════════════════════════════
   密集表格在 400px 寬是不可能讀的 —— 橫捲會讓關鍵欄位（車牌、狀態）跑出畫面，
   而現場人員在太陽下單手操作，橫捲等於放棄。
   所以 ≤760px 時把表格整列變成一張卡：每個欄位用 data-label 顯示欄名，
   列高限制解除，狀態與金額各自成行。

   做法是純 CSS ＋ 儲存格的 data-label 屬性，不另寫一套手機版 HTML ——
   兩套 HTML 會各自腐爛。 */
@media (max-width: 760px) {
  table.data.cards { display: block; }
  table.data.cards thead { display: none; }
  table.data.cards tbody { display: block; }
  table.data.cards tbody tr {
    display: block; padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--line);
  }
  table.data.cards tbody tr:nth-child(odd) td { background: transparent; }
  table.data.cards tbody tr:nth-child(odd) { background: var(--stripe); }
  table.data.cards tbody td {
    display: flex; align-items: baseline; gap: var(--space-2);
    height: auto; min-height: 0; padding: 2px 0; border: 0; white-space: normal;
    text-align: left !important;
  }
  table.data.cards tbody td:first-child { padding-left: 0; }
  table.data.cards tbody td::before {
    content: attr(data-label); flex: 0 0 72px;
    font-size: 11px; color: var(--rebar-muted);
  }
  table.data.cards tbody td[data-label=""]::before { content: none; }
  /* 首欄（車牌／型號）當標題，不顯示欄名 */
  table.data.cards tbody td.card-title { display: block; padding-bottom: var(--space-1); }
  table.data.cards tbody td.card-title::before { content: none; }
  table.data.cards tbody td.card-title a,
  table.data.cards tbody td.card-title span { font-size: 15px; font-weight: 600; }
  /* 空值在手機上整行省掉，不要留一排「—」 */
  table.data.cards tbody td.card-hide-empty:has(.is-empty) { display: none; }
  table.data.cards tbody tr td:last-child { padding-top: var(--space-2); }
  .scroll:has(table.data.cards) { overflow-x: visible; }

  /* 明細面板在手機上不黏頂，且與一覽表之間留白 */
  .twopane { gap: var(--space-6); }
  .detail .kv { grid-template-columns: 84px 1fr; }
  .addform .grid2 { grid-template-columns: 1fr; }
  .covrow { grid-template-columns: 1fr; }
  /* 明細面板的表單在桌機刻意壓到 34px 求密度，但手機上那低於 44px 的觸控底線。
     密度在手機沒有價值 —— 戴手套點不到才是真問題。 */
  .addform input, .addform select, .addform textarea { min-height: var(--touch-min); }
  .addform textarea { min-height: 72px; }
  .addform .btn { height: var(--touch-min); font-size: 13px; }
  .addform .btn.field { height: var(--field-button); font-size: 17px; }
  .detail section .btn { min-height: var(--touch-min); }
  .detail .minirow .btn { min-height: 32px; }   /* 列內的「刪」「標記已繳」維持小尺寸但仍可點 */
  /* 手機固定兩欄：auto-fit 在 400px 會排成 3+1，剩下兩格空白會露出格線底色，
     看起來像壞掉。偶數欄位在小螢幕永遠乾淨。 */
  .tiles { grid-template-columns: repeat(2, 1fr); }
  .tiles .tile .n { font-size: 20px; }
  /* 頂欄的搜尋框在手機上換到第二行，不然擠掉登出鈕 */
  .topbar { flex-wrap: wrap; height: auto; padding-top: var(--space-2); padding-bottom: var(--space-2); }
  .topbar form[action="/search"] { order: 10; flex-basis: 100%; max-width: none; }
  .topbar .who b { font-size: 12px; }
  h1 { font-size: 20px; }
  .page > header p { font-size: 12px; }
}

/* 相機上傳：現場人員按下去直接開相機，不用先進相簿 */
.camera {
  display: flex; align-items: center; justify-content: center; gap: var(--space-2);
  width: 100%; height: var(--field-button);
  background: var(--hivis); color: var(--rebar);
  font-family: inherit; font-size: 16px; font-weight: 600;
  border: 0; border-radius: var(--radius-control); cursor: pointer;
}
.camera svg { width: 22px; height: 22px; }
.camera input { display: none; }
@media (min-width: 761px) { .camera-only-mobile { display: none; } }

/* 頂欄搜尋框 —— 用 class 而非行內樣式：這是每一頁都出現的元件，
   而且觸控目標高度是設計系統的規定，不該散在各頁的 style 屬性裡。 */
.topsearch-form { margin: 0; flex: 1; max-width: 320px; min-width: 0; }
.topsearch {
  width: 100%; height: var(--touch-min); padding: 0 var(--space-3);
  font-family: inherit; font-size: 13px;
  background: var(--concrete); color: var(--rebar);
  border: 1px solid var(--line); border-radius: var(--radius-control);
}
.topsearch::placeholder { color: var(--rebar-muted); }
@media (max-width: 760px) { .topsearch { height: var(--touch-min); } }
