/* =========================================
   [版本] v1.6.8 (空調版 + 列印極致壓縮防爆版)
   ========================================= */
:root {
  --primary: #1e3a8a;
  --stamp: #d32f2f;
  --bg-gray: #e5e7eb;
  --light-gray: #f3f4f6;
  --border-color: #cbd5e1;
  --paper-w: 794px;
  --paper-h: 1123px;
}

* {
  box-sizing: border-box;
}
body {
  margin: 0;
  font-family: "Microsoft JhengHei", sans-serif;
  -webkit-tap-highlight-color: transparent;
}

/* === 1. 手機編輯頁樣式 === */
body.edit-page {
  background: #f3f4f6;
  padding-bottom: 80px;
}
.sys-header {
  background: #fff;
  padding: 15px;
  text-align: center;
  border-bottom: 1px solid #ddd;
  position: sticky;
  top: 0;
  z-index: 10;
}
.sys-header h1.main-title {
  margin: 0;
  color: var(--primary);
  font-size: 1.6rem;
  letter-spacing: 1px;
}
.sys-header p {
  margin: 0;
  font-size: 0.8rem;
  color: #666;
}

.mobile-container {
  padding: 15px;
  max-width: 600px;
  margin: 0 auto;
}
.card {
  background: #fff;
  padding: 15px;
  border-radius: 12px;
  margin-bottom: 15px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}
.card h3 {
  margin-top: 0;
  color: #333;
  border-bottom: 2px solid var(--primary);
  padding-bottom: 5px;
  display: inline-block;
  font-size: 1.1rem;
}
.hint {
  font-size: 0.8rem;
  color: #888;
  margin-top: -10px;
  margin-bottom: 10px;
}

.form-group {
  margin-bottom: 15px;
}
.form-group label {
  display: block;
  font-weight: bold;
  margin-bottom: 5px;
  color: #444;
}
.form-group input,
.form-group select {
  width: 100%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 6px;
  font-size: 1rem;
}
.readonly-input {
  background-color: #f3f4f6;
  color: #9ca3af;
  cursor: not-allowed;
}

.grid-2-col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.toggle-group {
  display: flex;
  background: #eee;
  border-radius: 6px;
  padding: 4px;
}
.toggle-group input[type="radio"] {
  display: none !important;
}
.toggle-group label {
  flex: 1;
  text-align: center;
  padding: 10px;
  border-radius: 4px;
  cursor: pointer;
  transition: 0.2s;
}
.toggle-group input:checked + label {
  background: var(--primary);
  color: #fff;
  font-weight: bold;
}

.quick-add-area {
  margin-bottom: 15px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px;
  background: #f8fafc;
  border-radius: 8px;
  border: 1px solid #e2e8f0;
}
.tag-btn {
  background: #fff;
  border: 1px solid var(--primary);
  color: var(--primary);
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 0.85rem;
  font-weight: bold;
  cursor: pointer;
}

.item-row {
  background: #ffffff;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 10px;
  margin-bottom: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}
.item-row-top,
.item-row-bottom {
  display: flex;
  gap: 8px;
  width: 100%;
}
.item-row input,
.item-row select {
  height: 44px;
  padding: 0 10px;
  border: 1px solid #ccc;
  border-radius: 6px;
  font-size: 1rem;
  box-sizing: border-box;
}

.ir-name {
  flex: 1;
  font-weight: bold;
}
.ir-del {
  width: 44px;
  height: 44px;
  flex-shrink: 0;
  background: #fee2e2;
  color: #ef4444;
  border: none;
  border-radius: 6px;
  font-size: 1.2rem;
  display: flex;
  align-items: center;
  justify-content: center;
}
.ir-qty {
  flex: 1;
  text-align: center;
}
.ir-unit {
  flex: 1;
  text-align: center;
  background: #f8fafc;
}
.ir-price {
  flex: 2;
  text-align: right;
  font-family: monospace;
  font-size: 1.1rem;
}

.payment-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.pg-item {
  text-align: center;
}
.pg-item label {
  font-size: 0.9rem;
}
.pg-item input {
  width: 100%;
  text-align: center;
  font-weight: bold;
  color: var(--primary);
  font-size: 1.1rem;
  padding: 10px 5px;
}

.action-bar-placeholder {
  height: 70px;
}
.action-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  background: #fff;
  padding: 10px;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: center;
  z-index: 50;
}
.btn {
  border: none;
  border-radius: 8px;
  padding: 12px 20px;
  font-size: 1.1rem;
  font-weight: bold;
  cursor: pointer;
  text-align: center;
}
.btn-primary {
  background: var(--primary);
  color: #fff;
  width: 100%;
  max-width: 600px;
}
.btn-outline {
  background: #fff;
  border: 2px dashed #999;
  color: #666;
}

/* === 2. 單據預覽頁樣式 === */
body.preview-page {
  background: var(--bg-gray);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 30px 0;
}
.top-nav {
  margin-bottom: 20px;
  display: flex;
  gap: 10px;
  justify-content: center;
}
.btn-secondary {
  background: #fff;
  border: 2px solid #94a3b8;
  color: #475569;
}
.btn-info {
  background: #0ea5e9;
  color: #fff;
}
.btn-success {
  background: #10b981;
  color: #fff;
}

.a4-container {
  width: var(--paper-w);
  min-height: var(--paper-h);
  background: #fff;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  padding: 15mm 20mm;
  position: relative;
  display: flex;
  flex-direction: column;
}

.doc-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}
.brand-name {
  font-size: 50px;
  font-weight: 900;
  line-height: 1;
  margin: 0;
  letter-spacing: -1px;
  white-space: nowrap;
}
.brand-en {
  font-size: 14px;
  font-weight: bold;
  letter-spacing: 4px;
  margin: 4px 0 12px 0;
  color: #333;
}
.company-meta {
  font-size: 15px;
  line-height: 1.6;
  color: #111;
  font-weight: bold;
}

.doc-badge {
  text-align: right;
}
.doc-badge h2 {
  font-size: 32px;
  letter-spacing: 4px;
  line-height: 1;
  margin: 0;
  white-space: nowrap;
}
.doc-badge .subtitle {
  font-size: 14px;
  letter-spacing: 3px;
  font-weight: bold;
  margin-top: 5px;
  color: #555;
}

.header-divider {
  border: 0;
  border-top: 4px solid #000;
  margin: 15px 0;
}
.doc-meta-row {
  display: flex;
  justify-content: space-between;
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 12px;
}

.client-box {
  border: 2px solid #000;
  padding: 15px;
  margin-bottom: 15px;
}
.client-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  font-size: 17px;
}
.cg-item {
  display: flex;
  align-items: baseline;
}
.cg-item .label {
  font-weight: bold;
  width: 90px;
}
.cg-item .value {
  border-bottom: 1px dotted #666;
  flex-grow: 1;
  font-weight: bold;
}
.cg-full {
  grid-column: 1 / -1;
}

.table-container {
  flex: 1;
  margin-bottom: 15px;
}
.data-table {
  width: 100%;
  border-collapse: collapse;
}
.data-table th {
  background: var(--primary);
  color: #fff;
  padding: 8px;
  font-size: 14px;
  border: 1px solid var(--primary);
  text-align: left;
}
.data-table td {
  padding: 9px;
  border: 1px solid var(--border-color);
  font-size: 16px;
}
.data-table tr:nth-child(even) {
  background: var(--light-gray);
}
.text-center {
  text-align: center;
}
.text-right {
  text-align: right;
}

.bottom-layout {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
}
.terms-box {
  width: 55%;
  font-size: 16px;
}
.terms-box h4 {
  margin: 0 0 10px 0;
  font-size: 17px;
  text-decoration: underline;
}
.terms-list {
  margin: 0;
  padding-left: 20px;
  line-height: 1.6;
}
.terms-list li {
  margin-bottom: 6px;
}
.terms-list b {
  color: #000;
  font-weight: 900;
}

.totals-box {
  width: 40%;
  text-align: right;
}
.tot-row {
  display: flex;
  justify-content: space-between;
  font-weight: bold;
  margin-bottom: 8px;
  font-size: 17px;
}
.money-num {
  font-family: "Courier New", monospace;
  font-weight: bold;
}

.grand {
  border-top: 3px solid #000;
  border-bottom: 3px solid #000;
  padding: 10px 0;
}
.grand-label {
  font-size: 18px;
  font-weight: bold;
  text-align: left;
  margin-bottom: 5px;
}
.grand-num {
  color: var(--stamp);
  font-size: 46px;
  font-weight: 900;
}

.sign-area {
  display: flex;
  justify-content: space-between;
  margin-top: 25px;
  margin-bottom: 15px;
  page-break-inside: avoid;
}
.sign-box {
  width: 45%;
}
.sign-title {
  text-align: center;
  font-weight: bold;
  margin-bottom: 10px;
  font-size: 18px;
}
.sign-person {
  height: 70px;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  position: relative;
}
.sign-name {
  font-family: "BiauKai", serif;
  font-size: 46px;
  line-height: 1.1;
  margin-bottom: 5px;
  z-index: 2;
}
.stamp-wrapper {
  position: absolute;
  bottom: -15px;
  right: 15px;
  width: 95px;
  height: 95px;
  z-index: 1;
  pointer-events: none;
}
.stamp-svg {
  width: 100%;
  height: 100%;
  mix-blend-mode: multiply;
  opacity: 0.85;
  transform: rotate(-8deg);
}
.sign-line {
  border-bottom: 1px solid #000;
  width: 100%;
}
.slogan {
  text-align: center;
  font-weight: bold;
  border-top: 1px dashed #999;
  padding-top: 15px;
  margin-top: auto;
}

/* =========================================
   🛡️ 終極列印防爆護盾 (專治各種斷頁與切頭)
   ========================================= */
@media print {
  @page {
    size: A4 portrait;
    /* 🐛 修復 1: 給予 10mm 實體邊距，保護頭部不被切掉 */
    margin: 10mm;
  }
  body.preview-page {
    background: #fff;
    padding: 0;
    margin: 0;
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }
  .no-print {
    display: none !important;
  }
  .a4-container {
    box-shadow: none;
    margin: 0;
    /* 🐛 修復 2: 因為 @page 已經有邊距，這裡內部 padding 歸零爭取極大空間 */
    padding: 0 !important;
    width: 100%;
    display: block !important;
    height: auto !important;
    min-height: auto !important;
  }

  /* 🐛 修復 3: 極致壓縮術！瘋狂壓榨所有不必要的垂直空白 */
  .doc-header {
    padding-bottom: 0 !important;
  }
  .brand-name {
    font-size: 40px !important;
  } /* 標題微縮小 */
  .brand-en {
    margin: 2px 0 6px 0 !important;
  }
  .company-meta {
    font-size: 13px !important;
    line-height: 1.4 !important;
  }
  .doc-badge h2 {
    font-size: 26px !important;
  }

  .header-divider {
    margin: 8px 0 !important;
    border-top-width: 2px !important;
  }
  .doc-meta-row {
    margin-bottom: 8px !important;
    font-size: 14px !important;
  }

  .client-box {
    padding: 8px 12px !important;
    margin-bottom: 10px !important;
  }
  .client-grid {
    gap: 6px !important;
    font-size: 14px !important;
  }
  .cg-item .value {
    min-height: 18px !important;
  }

  .table-container {
    margin-bottom: 10px !important;
  }
  .data-table th,
  .data-table td {
    padding: 6px 4px !important; /* 壓縮表格內部留白 */
    font-size: 14px !important; /* 表格字體微縮小 */
  }

  .bottom-layout {
    margin-bottom: 8px !important;
  }
  .terms-box {
    font-size: 14px !important;
  }
  .terms-box h4 {
    margin-bottom: 5px !important;
  }
  .terms-list {
    line-height: 1.4 !important;
  }

  .tot-row {
    margin-bottom: 4px !important;
    font-size: 15px !important;
  }
  .grand {
    padding: 5px 0 !important;
  }
  .grand-num {
    font-size: 36px !important;
  } /* 總計金額微縮小 */

  .sign-area {
    margin-top: 15px !important;
    margin-bottom: 5px !important;
  }
  .sign-person {
    height: 60px !important;
  }
  .sign-name {
    font-size: 38px !important;
    margin-bottom: 2px !important;
  }
  .stamp-wrapper {
    width: 80px !important;
    height: 80px !important;
    bottom: -10px !important;
  }
  .sign-title {
    margin-bottom: 5px !important;
    font-size: 16px !important;
  }

  .slogan {
    margin-top: 10px !important;
    padding-top: 10px !important;
    font-size: 14px !important;
  }
}
