/* === 系統癱瘓紙本單 === */

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: "微軟正黑體", "Microsoft JhengHei", "PMingLiU", sans-serif;
  font-size: 12px;
  color: #000;
  background: #e0e0e0;
  padding-bottom: 40px;
}

.app-header {
  background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
  color: white;
  padding: 8px 16px;
  position: sticky;
  top: 0;
  z-index: 100;
}
.app-header h1 { font-size: 16px; }
.app-header .subtitle { font-size: 10px; opacity: 0.85; }

.content {
  padding: 10px;
  max-width: 1900px;
  margin: 0 auto;
}

/* === 工單彙整 (極簡橫排) === */
.workorder-section {
  background: white;
  border: 2px solid #2c3e50;
  border-radius: 6px;
  padding: 10px 14px;
  margin-bottom: 10px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.08);
}
.wo-row {
  display: flex;
  gap: 14px;
  align-items: flex-end;
  flex-wrap: wrap;
}
.wo-row label {
  display: flex;
  flex-direction: row; align-items: center;
  font-size: 11px;
  color: #555;
  font-weight: bold;
  gap: 4px; white-space: nowrap;
}
.wo-row label input {
  padding: 5px 7px;
  border: 1px solid #bbb;
  border-radius: 3px;
  font-family: inherit;
  font-size: 13px;
  width: 160px;
}
.wo-buttons { display: flex; gap: 6px; padding-bottom: 1px; }
.btn-primary {
  background: #2c3e50;
  color: white;
  border: none;
  padding: 8px 14px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  font-weight: bold;
}
.btn-primary:hover { background: #34495e; }
.btn-secondary {
  background: #95a5a6;
  color: white;
  border: none;
  padding: 8px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
}
.btn-secondary:hover { background: #7f8c8d; }
.required { color: red; font-weight: bold; }
.legend-line {
  font-size: 10px;
  color: #666;
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px dashed #ddd;
}
.legend-tip { color: #999; font-style: italic; margin-left: 8px; }

/* === 自動 / 手動欄位 === */
.auto-field {
  color: #d32f2f;
  font-weight: bold;
  padding: 2px 6px;
  background: #fff8e1;
  border-left: 3px solid #d32f2f;
  border-radius: 2px;
  display: inline-block;
  min-height: 18px;
  font-size: 12px;
}
.auto-field:empty::before {
  content: '(待載入)';
  color: #ccc;
  font-weight: normal;
  font-style: italic;
  background: transparent;
  border-left: none;
}
.manual-field {
  color: #000;
  padding: 2px 6px;
  background: #fff;
  border-left: 2px solid #999;
  border-radius: 2px;
  display: inline-block;
  min-height: 18px;
  font-size: 12px;
}
.manual-field:empty::before {
  content: '(待 KEYIN)';
  color: #ccc;
  font-style: italic;
}
.big-text { font-size: 16px; font-weight: bold; padding: 2px 8px; }
.big-backno { font-size: 28px; font-weight: bold; color: #d32f2f; padding: 0 10px; }

.legend-auto {
  color: #d32f2f;
  font-weight: bold;
  background: #fff8e1;
  border-left: 3px solid #d32f2f;
  padding: 1px 6px;
  border-radius: 2px;
}
.legend-manual {
  color: #000;
  background: #fff;
  border-left: 2px solid #999;
  padding: 1px 6px;
}

/* === 4 個分頁並排 grid === */
.sheets-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  align-items: stretch;
}

.sheet-panel {
  background: white;
  border: 1px solid #ccc;
  border-radius: 5px;
  padding: 10px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.sheet-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: bold;
  font-size: 13px;
  color: #2c3e50;
  border-bottom: 2px solid #2c3e50;
  padding-bottom: 5px;
  margin-bottom: 8px;
}
.btn-print {
  background: #27ae60;
  color: white;
  border: none;
  width: 28px;
  height: 26px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
}
.btn-print:hover { background: #2ecc71; }

/* === 入庫標籤 (B6: 128 × 182 mm) === */
#sheet-label { font-family: "Microsoft JhengHei", "PMingLiU", sans-serif; }
.label-doc {
  display: flex;
  flex-direction: column;
  padding: 6px 8px;
  border: 1px solid #333;
  background: #fff;
}
.label-company {
  text-align: center;
  font-size: 13px;
  font-weight: bold;
  border-bottom: 1.5px solid #333;
  padding-bottom: 3px;
  margin-bottom: 4px;
}
.label-title {
  text-align: center;
  font-size: 15px;
  font-weight: bold;
  margin-bottom: 6px;
}
.label-body {
  display: grid;
  grid-template-columns: 1.6fr 1fr;
  gap: 6px;
  align-items: stretch;
}
.label-right { margin-left: -8mm; }
.label-fields {
  grid-column: 1;
  grid-row: 1 / span 8;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.label-fields .cell {
  display: flex;
  gap: 4px;
  font-size: 11px;
  align-items: baseline;
  border-bottom: 1px dotted #ccc;
  padding: 1px 0;
  min-width: 0;
}
.label-fields .cell .lk {
  min-width: 48px;
  font-weight: bold;
  color: #333;
  flex-shrink: 0;
  font-size: 10px;
}
.label-fields .cell .val {
  flex: 1;
  font-size: 11px;
  font-weight: bold;
  min-width: 0;
  overflow: visible;
}
/* 品名強制單行,允許超出顯示完整文字 */
.label-fields .cell:first-child .val {
  white-space: nowrap;
  overflow: visible;
}
.label-fields .cell.big-lot .val {
  font-size: 20px;
  font-weight: bold;
  color: #c00;
  white-space: nowrap;
}
.label-right {
  grid-column: 2;
  /* 從第 3 列開始 (跳過品名+規格),跨 4 列 (跨件號~收容數) */
  grid-row: 3 / span 4;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  min-width: 0;
  padding-top: 30px;
}
.backno {
  text-align: left;
  font-size: 96px;
  font-weight: 900;
  color: #000;
  line-height: 1;
  padding: 2px 4px 6px 2px;
  border-bottom: none;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  font-family: "Arial Black", "Helvetica", sans-serif;
  letter-spacing: 2px;
}
.manuf-box {
  border: 1.5px solid #000;
  display: flex;
  flex-direction: column;
}
.manuf-row {
  padding: 4px 6px;
  text-align: center;
  font-size: 11px;
  font-weight: bold;
  border-bottom: 1px solid #000;
  line-height: 1.2;
}
.manuf-row:last-child { border-bottom: none; }
.manuf-foot { font-size: 9px; font-weight: normal; padding: 3px 4px; }
.manuf-stamp { height: 60px; background: repeating-linear-gradient(45deg, transparent, transparent 8px, #f5f5f5 8px, #f5f5f5 9px); }
.label-bottom-row {
  display: flex;
  justify-content: space-between;
  margin-top: auto;
  padding-top: 6px;
  font-size: 10px;
  color: #555;
}
.label-datetime { text-align: left; }
.label-pages { text-align: right; font-weight: bold; color: #c00; font-size: 12px; }


/* === 生產流程卡 / 發料單 === */
.flow-head {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 0 12px;
  margin-bottom: 8px;
  font-size: 24px;
  border: 1px solid #888;
  border-radius: 3px;
  padding: 4px 10px;
  background: #fffbf0;
  align-items: start;
}
.flow-head > div > div {
  display: flex;
  flex-wrap: nowrap;
  gap: 4px;
  align-items: baseline;
  line-height: 1.3;
  min-width: 0;
  overflow: visible;
}
.flow-head > div > div b {
  font-size: 24px;
  color: #444;
  flex-shrink: 0;
}
.flow-head .auto-field,
.flow-head .manual-field {
  font-size: 24px;
  border-left-width: 0;
  padding: 0 4px;
  min-width: 0;
  overflow: visible;
  text-overflow: clip;
  white-space: nowrap;
  word-break: break-all;
  line-height: 1.15;
  flex-shrink: 0;
}

/* === 素板成套發料單 (sheet-issue) 標頭特例 ===
   品名可能很長 (例:後邊窗左玻璃射出成型黑邊ASSY),
   若維持 nowrap + flex-shrink:0 會被右欄截斷看不到 ASSY。
   只放寬 sheet-issue,保留 sheet-flow 的單行標頭。 */
#sheet-issue .flow-head {
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.2fr);
}
#sheet-issue .flow-head .auto-field {
  white-space: normal;
  overflow-wrap: anywhere;
  word-break: keep-all;
  flex-shrink: 1;
  line-height: 1.25;
}
/* 手動欄位 (工單單號/生產數量) 仍單行 */
#sheet-issue .flow-head .manual-field {
  white-space: nowrap;
}

.flow-table, .issue-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 22px;
  margin-top: 4px;
  table-layout: fixed;
}
.flow-table th, .flow-table td,
.issue-table th, .issue-table td {
  border: 1px solid #888;
  padding: 3px 4px;
  text-align: center;
  vertical-align: middle;
  overflow: hidden;
}
.flow-table th, .issue-table th {
  background: #f0f0f0;
  font-weight: bold;
  font-size: 22px;
}
.flow-table col.c-項次 { width: 7%; }
.flow-table col.c-本站作業 { width: 23%; white-space: nowrap; }
.flow-table col.c-投入 { width: 6%; }
.flow-table col.c-良品 { width: 6%; }
.flow-table col.c-報廢 { width: 6%; }
.flow-table col.c-下線 { width: 6%; }
.flow-table col.c-重工 { width: 6%; }
.flow-table col.c-作業員 { width: 10%; }
.flow-table col.c-檢驗員 { width: 10%; }
.flow-table col.c-日期 { width: 8%; }
.flow-table col.c-備註 { width: 12%; }
.issue-table col.c-項次 { width: 7%; }
.issue-table col.c-部位 { width: 32%; }
.issue-table col.c-料號 { width: 36%; }
.issue-table col.c-應發 { width: 12%; }
.issue-table col.c-單位 { width: 13%; }
.issue-table td {
  vertical-align: top;
  padding: 4px 4px;
}
.issue-table td > div { line-height: 1.4; min-height: 1.4em; }
.issue-table td > div.l1 { font-weight: bold; }
.issue-table td > div.l3 { color: #333; }
.flow-table input, .issue-table input {
  width: 100%;
  border: none;
  padding: 2px;
  font-size: 22px;
  text-align: center;
  background: transparent;
  font-family: inherit;
}

/* 螢幕預覽: 表格縮小讓欄寬夠用, 列印用 iframe 不受影響 */
@media screen {
  .flow-table, .issue-table {
    font-size: 14px;
    min-width: 700px;
  }
  .flow-table th, .issue-table th {
    font-size: 14px;
  }
  .flow-table input, .issue-table input {
    font-size: 14px;
  }
  .sheet-flow { overflow-x: auto; }
}

/* === 領料紀錄卡 === */
.pickup-meta {
  font-size: 10px;
  color: #666;
  background: #fffbf0;
  padding: 4px 6px;
  border-radius: 3px;
  margin-bottom: 6px;
}
#sheet-pickup .sheet-title {
  position: sticky;
  top: 50px;
  z-index: 20;
  background: #fff3cd;
  color: #7a4b00;
  border-bottom-color: #d18b00;
}
.pickup-card {
  border: 1px solid #888;
  padding: 5px 6px;
  background: white;
  border-radius: 3px;
  margin-bottom: 5px;
  font-size: 10px;
}
.pickup-card:last-child { margin-bottom: 0; }
.pickup-station-group {
  margin-bottom: 6px;
  padding: 5px;
  border: 1px solid #2c3e50;
  border-radius: 4px;
  background: #fff;
}
.pickup-station-title {
  margin-bottom: 5px;
  padding: 5px 6px;
  border-radius: 3px;
  background: #2c3e50;
  color: #fff;
  text-align: center;
  font-size: 11px;
}
.pickup-group-header {
  display: grid;
  grid-template-columns: 2fr 3fr;
  margin-bottom: 5px;
  padding: 0;
  border: 1px solid #d8b45b;
  border-radius: 3px;
  background: #fffbf0;
}
.pickup-group-header .pickup-info {
  display: flex;
  align-items: baseline;
  gap: 1.5mm;
  min-width: 0;
  margin: 0;
  padding: 1.5mm 2mm;
  border-right: 1px solid #d8b45b;
  border-bottom: 1px solid #d8b45b;
  line-height: 1.25;
}
.pickup-group-header .pickup-info:nth-child(even) { border-right: 0; }
.pickup-group-header .pickup-info:nth-child(n+5) { border-bottom: 0; }
.pickup-group-header .pickup-info b { min-width: 0; white-space: nowrap; }
.pickup-group-header .pickup-info > span { min-width: 0; }
.pickup-material-list {
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.pickup-material-card {
  display: grid;
  grid-template-columns: 1fr 1fr;
  margin: 0;
  padding: 0;
}
.pickup-material-card .pickup-info {
  display: flex;
  align-items: baseline;
  gap: 1.5mm;
  min-width: 0;
  margin: 0;
  padding: 1.5mm 2mm;
  border-right: 1px solid #888;
  border-bottom: 1px solid #888;
  line-height: 1.2;
}
.pickup-material-card .pickup-info:nth-child(even) { border-right: 0; }
.pickup-material-card .pickup-info:nth-child(n+3) { border-bottom: 0; }
.pickup-material-card .pickup-info b { min-width: 0; white-space: nowrap; }
.pickup-required {
  color: #d32f2f;
  font-size: 13px;
}
.pickup-card-head {
  font-weight: bold;
  text-align: center;
  border-bottom: 1px solid #2c3e50;
  padding-bottom: 3px;
  margin-bottom: 4px;
  color: #2c3e50;
}
.pickup-info {
  display: flex;
  gap: 4px;
  align-items: baseline;
  margin-bottom: 2px;
  font-size: 10px;
}
.pickup-info b { min-width: 60px; color: #444; font-size: 10px; }
.pickup-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 3px;
  font-size: 9px;
}
.pickup-table th, .pickup-table td {
  border: 1px solid #888;
  padding: 2px 3px;
  text-align: center;
}
.pickup-table th { background: #f0f0f0; font-size: 9px; }
.pickup-table td { height: 18px; }
.pickup-empty {
  text-align: center;
  padding: 20px;
  color: #999;
  font-style: italic;
  font-size: 11px;
}

/* === 列印模式 === */
/* 入庫標籤 B6 由 iframe 處理;其餘報表皆為完整 A4、無周圍白邊 */
/* 全報表 @page 縮 5% (從 297→280,210→198) 預防印表機裁切 */
@page { size: 198mm 280mm; margin: 0; }
@page flow-page { size: 280mm 198mm; margin: 0; }
@page issue-page { size: 198mm 280mm; margin: 0; }
@page pickup-page { size: 198mm 280mm; margin: 0; }

@media print {
  body { background: white; padding: 0; font-size: 13px; }
  /* 列印強制全黑字(避免紅色在黑白印表機上變淡) */
  *, *::before, *::after { color: #000 !important; }
  .auto-field, .required, .big-backno, .label-pages, .pickup-required { color: #000 !important; }
  .no-print { display: none !important; }
  .content { padding: 0; max-width: 100%; }
  .workorder-section { display: none; }
  .app-header { display: none; }
  .legend-line { display: none; }
  .pickup-meta { display: none; }
  .sheets-grid { display: block !important; }
  .sheet-panel {
    page-break-after: always;
    page-break-inside: avoid;
    break-inside: avoid;
    box-shadow: none;
    border: 1px solid #000;
    margin: 0 0 10px 0;
    padding: 8px 12px;
  }
  .sheet-panel:last-child {
    page-break-after: avoid !important;
    break-after: avoid-page !important;
  }
  .btn-print { display: none; }
  body.printing-flow #sheet-flow {
    page: flow-page;
    width: 280mm;
    min-height: 198mm;
    margin: 6mm auto;
    padding: 4mm;
    box-sizing: border-box;
  }
  body.printing-issue #sheet-issue {
    page: issue-page;
    width: 198mm;
    min-height: 280mm;
    margin: 6mm auto;
    padding: 6mm;
    box-sizing: border-box;
  }
  body.printing-pickup #sheet-pickup {
    page: pickup-page;
    width: 198mm;
    min-height: 280mm;
    padding: 0;
    margin: 0;
    box-sizing: border-box;
  }
  body.printing-pickup #sheet-pickup > .pickup-print-page {
    width: 188mm !important;
    min-height: 270mm;
    padding: 5mm;
    box-sizing: border-box;
  }

  /* 入庫標籤在 iframe 內列印 B6,主頁面列印時顯示為 A4 大小 */
  #sheet-label {
    width: 182mm;
    height: 128mm;
    padding: 0;
    margin: 8mm auto;
    border: none;
    overflow: hidden;
  }
  #sheet-label .label-doc {
    width: 172mm;
    height: 118mm;
    padding: 3mm;
    border: 1px solid #000;
    box-sizing: border-box;
  }
  /* iframe 列印時 sheet-label 內容 (B6) */
  iframe { page-break-after: avoid !important; }
  #sheet-label .label-company { font-size: 12px; }
  #sheet-label .label-title { font-size: 14px; }
  #sheet-label .label-fields .cell { font-size: 10px; }
  #sheet-label .label-fields .cell .val { font-size: 11px; }
  #sheet-label .label-fields .cell.big-lot .val { font-size: 22px; }
  #sheet-label .backno { font-size: 56px; }
  #sheet-label .manuf-row { font-size: 11px; padding: 5px 6px; }
  #sheet-label .manuf-row:nth-child(1) { background: #fffbe8; }
  #sheet-label .label-pages { font-size: 14px; }
  #sheet-label .label-right { margin-left: -8mm; }
}

/* === 響應式:窄螢幕自動堆疊 === */
@media (max-width: 1600px) {
  .sheets-grid { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 900px) {
  .sheets-grid { grid-template-columns: 1fr; }
  .wo-row { flex-direction: row; align-items: center; align-items: stretch; }
  .wo-row label input { width: 100%; }
}
