/* ============================================================
   格侖服務報告 e 化 — 樣式
   兩個視圖共用同一份資料：
     .view#viewFill  填寫模式（手機優先，大字、直排）
     .view#viewPage  整頁模式（A4 1:1 複刻，PDF 由此輸出）
   ============================================================ */

:root {
  color-scheme: light;

  --brand:        #2fa36b;   /* JIATEK 綠 */
  --brand-dark:   #238053;
  --brand-blue:   #1a63b8;   /* JIATEK 藍 */
  --ink:          #16202a;
  --ink-2:        #47576a;
  --ink-3:        #7d8b9b;
  --line:         #d9e0e7;
  --bg:           #eef1f4;
  --surface:      #ffffff;
  --focus:        #2fa36b;
  --danger:       #c0392b;

  --radius:       12px;
  --appbar-h:     56px;
  --actionbar-h:  64px;

  /* A4 幾何：與原範本量測值對齊 */
  --pw:      210mm;
  --ph:      297mm;
  --pad-x:   10mm;
  --bw:      0.25mm;   /* 表格線寬 */
  --row-h:   6.4mm;    /* 資訊列／處理項目列 */
  --part-h:  6.1mm;    /* 更換零件列 */
  --fs:      3.05mm;   /* 表單內文字級 */
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Segoe UI", -apple-system, BlinkMacSystemFont, "Microsoft JhengHei",
               "PingFang TC", "Noto Sans TC", "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}

.app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ── 上方工具列 ───────────────────────────────────────────── */

.appbar {
  position: sticky;
  top: 0;
  z-index: 30;
  height: var(--appbar-h);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 12px;
  padding-top: env(safe-area-inset-top);
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 1px 3px rgba(22, 32, 42, .06);
}

.appbar-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 15px;
  white-space: nowrap;
}
.appbar-brand img { height: 24px; width: auto; display: block; }

.seg {
  display: flex;
  margin-left: auto;
  background: #e7ebef;
  border-radius: 999px;
  padding: 3px;
}
.seg-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink-2);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  padding: 7px 14px;
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
}
.seg-btn.is-on {
  background: var(--surface);
  color: var(--brand-dark);
  box-shadow: 0 1px 3px rgba(22, 32, 42, .18);
}

.appbar-tools { display: flex; align-items: center; gap: 10px; flex: 0 0 auto; }

.save-hint {
  font-size: 12px;
  color: var(--ink-3);
  white-space: nowrap;
}

@media (max-width: 420px) {
  .appbar { gap: 8px; padding: 0 10px; }
  .appbar-brand .brand-text { display: none; }
  .seg-btn { padding: 7px 11px; }
}

.ghost-btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink-2);
  font: inherit;
  font-size: 14px;
  padding: 7px 12px;
  border-radius: 8px;
  cursor: pointer;
}
.ghost-btn:hover { border-color: var(--ink-3); }

.main { flex: 1 1 auto; min-height: 0; }

.view { display: none; }
.view.is-on { display: block; }

/* ── 填寫模式 ─────────────────────────────────────────────── */

#viewFill {
  max-width: 720px;
  margin: 0 auto;
  padding: 14px 12px calc(var(--actionbar-h) + 24px);
}

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 16px 4px;
  margin-bottom: 14px;
}

.card-h {
  margin: 0 0 14px;
  font-size: 16px;
  font-weight: 700;
  color: var(--brand-dark);
  letter-spacing: .01em;
  display: flex;
  align-items: center;
  gap: 8px;
}
.card-h::before {
  content: "";
  width: 4px;
  height: 16px;
  border-radius: 2px;
  background: linear-gradient(180deg, var(--brand), var(--brand-blue));
}

.field { margin-bottom: 16px; }

.field > label {
  display: block;
  font-size: 14px;
  font-weight: 600;
  color: var(--ink-2);
  margin-bottom: 6px;
}

/* 涵蓋 .field 與動態產生的 .item-card 兩種容器 —— 兩者都在 .card 裡 */
.card input[type="text"],
.card input[type="tel"],
.card input[type="date"],
.card input[type="datetime-local"],
.card input[type="number"],
.card textarea {
  width: 100%;
  font: inherit;
  font-size: 18px;
  line-height: 1.45;
  color: var(--ink);
  background: #fbfcfd;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 12px;
  min-height: 48px;
  resize: none;
  overflow: hidden;
}
.card textarea { line-height: 1.5; }

.card input:focus,
.card textarea:focus {
  outline: none;
  border-color: var(--focus);
  background: var(--surface);
  box-shadow: 0 0 0 3px rgba(47, 163, 107, .16);
}

.inline { display: flex; gap: 8px; align-items: stretch; }
.inline input { flex: 1 1 auto; }

.mini-btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--brand-dark);
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  padding: 0 14px;
  border-radius: 10px;
  cursor: pointer;
  white-space: nowrap;
}
.mini-btn:hover { border-color: var(--brand); }

/* 選項 chips */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  appearance: none;
  font: inherit;
  font-size: 16px;
  padding: 11px 16px;
  min-height: 46px;
  border: 1px solid var(--line);
  background: #fbfcfd;
  color: var(--ink-2);
  border-radius: 10px;
  cursor: pointer;
}
.chip:hover { border-color: var(--ink-3); }
.chip.is-on {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
  font-weight: 600;
}
.chip-extra { margin-top: 10px; }

/* 動態列表 */
.stack { display: flex; flex-direction: column; gap: 12px; margin-bottom: 12px; }

.item-card {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
  background: #fbfcfd;
}
.item-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
.item-no { font-size: 14px; font-weight: 700; color: var(--brand-dark); }
.item-sub { font-size: 13px; font-weight: 600; color: var(--ink-2); margin: 10px 0 5px; }
.item-card textarea,
.item-card input {
  background: var(--surface);
}

.del-btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink-3);
  font: inherit;
  font-size: 13px;
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
}
.del-btn:hover { color: var(--danger); border-color: var(--danger); }
/* 只剩一張卡時不給刪 —— 表單至少要有一列 */
#fillItems .item-card:only-child .del-btn,
#fillParts .item-card:only-child .del-btn { display: none; }

.part-grid { display: grid; grid-template-columns: 1fr 1fr 88px; gap: 8px; }
.part-grid > div > span {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-2);
  margin-bottom: 5px;
}
@media (max-width: 480px) {
  .part-grid { grid-template-columns: 1fr; }
}

.row-btns { margin-bottom: 16px; }
.add-btn {
  appearance: none;
  width: 100%;
  border: 1px dashed var(--brand);
  background: rgba(47, 163, 107, .06);
  color: var(--brand-dark);
  font: inherit;
  font-size: 16px;
  font-weight: 600;
  padding: 13px;
  border-radius: 10px;
  cursor: pointer;
}
.add-btn:hover { background: rgba(47, 163, 107, .12); }

/* 簽名按鈕 */
.sign-pad-btn {
  appearance: none;
  width: 100%;
  height: 120px;
  border: 1px dashed var(--ink-3);
  border-radius: 10px;
  background: #fbfcfd;
  color: var(--ink-3);
  font: inherit;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
}
.sign-pad-btn:hover { border-color: var(--brand); color: var(--brand-dark); }
.sign-pad-btn.has-sign { border-style: solid; border-color: var(--line); background: var(--surface); }
.sign-pad-btn .sign-img { max-height: 100%; max-width: 100%; object-fit: contain; }

.fill-note {
  font-size: 13px;
  line-height: 1.7;
  color: var(--ink-3);
  margin: 4px 4px 0;
}

/* ── 整頁模式 ─────────────────────────────────────────────── */

#viewPage { padding-bottom: calc(var(--actionbar-h) + 20px); }

.page-toolbar {
  position: sticky;
  top: calc(var(--appbar-h) + env(safe-area-inset-top));
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  background: rgba(238, 241, 244, .94);
  backdrop-filter: blur(6px);
  border-bottom: 1px solid var(--line);
}

.zoom { display: flex; background: #e0e5ea; border-radius: 8px; padding: 2px; }
.zoom-btn {
  appearance: none;
  border: 0;
  background: transparent;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-2);
  padding: 6px 11px;
  border-radius: 7px;
  cursor: pointer;
  white-space: nowrap;
}
.zoom-btn.is-on { background: var(--surface); color: var(--brand-dark); }

.page-tip { font-size: 12px; color: var(--ink-3); }
@media (max-width: 560px) { .page-tip { display: none; } }

.page-viewport {
  overflow-x: auto;
  overflow-y: hidden;
  padding: 16px 12px 0;
  -webkit-overflow-scrolling: touch;
}

.page {
  width: var(--pw);
  min-height: var(--ph);
  padding: 10mm var(--pad-x) 8mm;
  background: #fff;
  color: #000;
  transform-origin: top left;
  box-shadow: 0 2px 14px rgba(22, 32, 42, .16);
  display: flex;
  flex-direction: column;
  font-size: var(--fs);
  line-height: 1.32;
}

/* 頁首 */
.p-head {
  height: 14mm;
  margin-bottom: 3mm;
  display: flex;
  align-items: center;
  position: relative;
  flex: 0 0 auto;
}
.p-logo { height: 14mm; width: auto; display: block; }
.p-title {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 58mm;
  height: 10.7mm;
  border: 0.3mm solid #000;
  border-radius: 3mm;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 5.4mm;
  letter-spacing: .04em;
}

/* 表格骨架 */
.grid {
  border-top: var(--bw) solid #000;
  border-left: var(--bw) solid #000;
}
.r { display: flex; }
.c {
  border-right: var(--bw) solid #000;
  border-bottom: var(--bw) solid #000;
  padding: 0.9mm 1.4mm;
  min-height: var(--row-h);
  display: flex;
  align-items: center;
  gap: 1.2mm;
  min-width: 0;
  overflow: hidden;
}
.lb { font-style: normal; white-space: nowrap; flex: 0 0 auto; }
.ta-c { justify-content: center; }

/* 可編輯欄位 */
.f {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 4mm;
  outline: none;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  cursor: text;
}
.f:focus { background: rgba(47, 163, 107, .10); }
.f.is-empty::before {
  content: attr(data-ph);
  color: #c3ccd4;
}
.exporting .f:focus { background: transparent; }
.exporting .f.is-empty::before { content: ""; }

.f-other { flex: 0 1 22mm; border-bottom: var(--bw) dotted #999; }
.exporting .f-other { border-bottom-color: transparent; }

/* 勾選框 */
.ck {
  display: inline-flex;
  align-items: center;
  gap: 0.8mm;
  margin-right: 1.6mm;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  flex: 0 0 auto;
}
.ck .bx {
  width: 2.9mm;
  height: 2.9mm;
  border: 0.25mm solid #000;
  display: inline-block;
  position: relative;
  flex: 0 0 auto;
}
.ck.is-on .bx::after {
  content: "";
  position: absolute;
  left: 0.85mm;
  top: 0.15mm;
  width: 0.9mm;
  height: 1.9mm;
  border: solid #000;
  border-width: 0 0.4mm 0.4mm 0;
  transform: rotate(42deg);
}

/* 本文區 */
.body-grid { margin-top: 2.5mm; display: flex; flex-direction: column; }
.r-case .c { flex: 1 1 100%; min-height: 12.7mm; align-items: flex-start; }
.f-case { min-height: 10mm; }
.r-sub .c { min-height: var(--row-h); }
.rows { display: flex; flex-direction: column; }

.r-item .c { align-items: flex-start; }
.item-idx { flex: 0 0 5mm; }
.filler .c { min-height: var(--row-h); cursor: cell; }
.filler:hover .c { background: rgba(47, 163, 107, .07); }
.exporting .filler:hover .c { background: transparent; }

/* 頁尾區塊 */
.foot-grid { display: flex; align-items: stretch; margin-top: 1mm; flex: 0 0 auto; }
.foot-left  { flex: 0 0 50%; min-width: 0; }
.foot-right { flex: 1 1 auto; min-width: 0; }
.foot-left  > .grid,
.foot-right > .grid { height: 100%; display: flex; flex-direction: column; }
.foot-left  > .grid { border-right: 0; }
.foot-right > .grid { border-left: 0; }
.foot-left  .rows   { flex: 1 1 auto; }
.foot-right .r-sign { flex: 1 1 auto; }

.r-part .c { min-height: var(--part-h); }
.r-memo .c { flex: 1 1 100%; min-height: 18.6mm; align-items: flex-start; }
.f-memo { min-height: 15mm; }

.r-sign .c { min-height: 36.4mm; align-items: stretch; }
.sign-cell {
  flex-direction: column;
  gap: 0;
  cursor: pointer;
  position: relative;
}
.sign-cell:hover { background: rgba(47, 163, 107, .06); }
.exporting .sign-cell:hover { background: transparent; }
.sign-slot {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 0.5mm 0;
}
.sign-slot .sign-img { max-height: 100%; max-width: 100%; object-fit: contain; }
.sign-date {
  display: flex;
  align-items: center;
  gap: 1.2mm;
  flex: 0 0 auto;
  min-height: 4.6mm;
}

.p-foot {
  margin-top: auto;
  padding-top: 3mm;
  text-align: center;
  font-size: 3.2mm;
}

/* ── 下方動作列 ───────────────────────────────────────────── */

.actionbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  height: calc(var(--actionbar-h) + env(safe-area-inset-bottom));
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  display: flex;
  gap: 10px;
  background: rgba(255, 255, 255, .96);
  backdrop-filter: blur(8px);
  border-top: 1px solid var(--line);
}

.btn {
  appearance: none;
  flex: 1 1 0;
  font: inherit;
  font-size: 16px;
  font-weight: 600;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  border-radius: 10px;
  padding: 12px;
  cursor: pointer;
  min-height: 44px;
}
.btn:hover { border-color: var(--ink-3); }
.btn:disabled { opacity: .55; cursor: progress; }
.btn-primary {
  background: linear-gradient(180deg, var(--brand), var(--brand-dark));
  border-color: var(--brand-dark);
  color: #fff;
}
.btn-primary:hover { filter: brightness(1.05); }

/* ── 簽名板 ───────────────────────────────────────────────── */

.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(16, 24, 32, .55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
}
.modal[hidden] { display: none; }

.modal-box {
  background: var(--surface);
  border-radius: 14px;
  width: min(760px, 100%);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; }
.modal-head h2 { margin: 0; font-size: 17px; }

.sign-wrap {
  position: relative;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fbfcfd;
  height: min(46vh, 320px);
  overflow: hidden;
}
#signCanvas { display: block; width: 100%; height: 100%; touch-action: none; }
.sign-guide {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  color: #c3ccd4;
  font-size: 17px;
}
.sign-wrap.has-ink .sign-guide { display: none; }

.modal-foot { display: flex; gap: 10px; }

/* ── 提示 ─────────────────────────────────────────────────── */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--actionbar-h) + 18px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 70;
  background: rgba(22, 32, 42, .94);
  color: #fff;
  font-size: 14px;
  padding: 10px 16px;
  border-radius: 999px;
  max-width: 90vw;
  text-align: center;
}
.toast[hidden] { display: none; }

/* ── 列印（整頁模式可直接列印）───────────────────────────── */

@media print {
  @page { size: A4 portrait; margin: 0; }
  html, body { background: #fff; height: auto; }
  .app { min-height: 0; display: block; }   /* 100dvh 會把版面撐成整頁高，列印時多出一頁 */
  .appbar, .actionbar, .page-toolbar, .modal, .toast, #viewFill { display: none !important; }
  #viewPage { display: block !important; padding: 0; }
  /* applyZoom() 會寫行內 height（頁高＋邊距），行內樣式蓋過樣式表，列印時會溢頁 */
  .page-viewport { overflow: visible; padding: 0; height: auto !important; }
  /* 瀏覽器列印時，min-height 剛好 297mm 會因為次像素進位溢到第二頁。
     空白列已把內容補到 296.5mm 以內，這裡把下限調低一點就會剛好一頁。 */
  .page { transform: none !important; box-shadow: none; margin: 0; min-height: 296mm; }
  .f.is-empty::before { content: ""; }
  .f-other { border-bottom-color: transparent; }
}
