:root {
  --teal: #0F766E;
  --teal-dark: #134E4A;
  --teal-soft: #CCFBF1;
  --amber: #F59E0B;
  --bg: #F1F5F9;
  --card: #FFFFFF;
  --border: #E2E8F0;
  --muted: #64748B;
  --dark: #1E293B;
  --filled-red: #DC2626;
  --radius: 10px;
  --font: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
}
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--dark);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

.app {
  height: 100vh;
  height: 100dvh;
  width: 100%;
  display: flex;
  flex-direction: column;
  padding: 10px 12px;
  gap: 8px;
  min-height: 0;
  overflow: hidden;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--teal);
  color: #fff;
  border-radius: var(--radius);
  padding: 10px 16px;
  flex-shrink: 0;
}
.brand { display: flex; align-items: baseline; gap: 10px; }
.brand strong { font-size: 18px; letter-spacing: 0.02em; }
.brand span { font-size: 13px; opacity: 0.85; }
.top-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-left: auto; }

/* Toolbar menus */
.menu { position: relative; display: inline-flex; }
.menu-toggle { display: inline-flex; align-items: center; gap: 6px; }
.menu-toggle .caret { font-size: 11px; opacity: 0.9; }
.menu-panel {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 148px;
  background: #fff;
  color: var(--dark);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 10px 28px rgba(15, 23, 42, 0.18);
  padding: 6px;
  z-index: 50;
}
.menu-item {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  border-radius: 6px;
  padding: 9px 10px;
  font: inherit;
  font-size: 13px;
  color: var(--dark);
  cursor: pointer;
}
.menu-item:hover { background: #f1f5f9; }
.menu.open .menu-toggle { filter: brightness(0.96); }

@media (max-width: 720px) {
  .topbar { align-items: stretch; }
  .top-actions {
    width: 100%;
    margin-left: 0;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 8px;
  }
  .top-actions > #btnReset { grid-column: 1 / -1; }
  .menu { display: block; }
  .menu-toggle { width: 100%; justify-content: center; }
  .menu-panel { left: 0; right: 0; min-width: 0; }
}


.btn {
  appearance: none;
  border: none;
  border-radius: 6px;
  padding: 8px 12px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  background: #e2e8f0;
  color: var(--dark);
}
.btn:hover { filter: brightness(0.97); }
.btn.ghost {
  background: var(--teal-dark);
  color: #fff;
}
.btn.accent {
  background: var(--amber);
  color: #1c1917;
  font-weight: 700;
}

.layout {
  display: grid;
  grid-template-columns: minmax(340px, 0.95fr) minmax(420px, 1.25fr);
  grid-template-rows: minmax(0, 1fr);
  gap: 10px;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  align-items: stretch;
}

.form-pane {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  max-height: 100%;
  height: 100%;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding-right: 6px;
  align-self: stretch;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
}
.card h2 {
  margin: 0 0 10px;
  font-size: 14px;
  color: var(--teal);
  font-weight: 700;
}
.card.collapsed { padding: 0; }
.card-toggle {
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  padding: 12px 14px;
  font: inherit;
  font-size: 13px;
  color: var(--teal);
  font-weight: 600;
  cursor: pointer;
}
.extra-body { padding: 0 14px 14px; }

.grid2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px 12px;
}
.span2 { grid-column: 1 / -1; }

label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
}
input, select, textarea {
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  color: var(--dark);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  background: #fff;
  width: 100%;
}
textarea { resize: vertical; min-height: 64px; }
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--teal-soft);
  border-color: var(--teal);
}
input[readonly] {
  background: #f8fafc;
  color: var(--muted);
}
.inline { display: flex; gap: 6px; }
.inline input { flex: 1; }
.inline select { width: auto; min-width: 78px; }
.hint { font-weight: 400; color: #94a3b8; font-size: 11px; }

/* filled required field text */
.required-fields input.is-filled,
.required-fields textarea.is-filled,
.required-fields select.is-filled {
  color: var(--filled-red);
  font-weight: 600;
}
.required-fields input.is-filled[readonly] {
  color: var(--filled-red);
}


.page-credit {
  text-align: center;
  color: #94a3b8;
  font-size: 12px;
  margin: 4px 0 8px;
}

.preview-pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  max-height: 100%;
  height: 100%;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  align-self: stretch;
}
.tabs {
  display: flex;
  gap: 4px;
  padding: 8px;
  background: #f8fafc;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  flex-wrap: wrap;
}
.tab {
  appearance: none;
  border: 1px solid transparent;
  background: transparent;
  border-radius: 6px;
  padding: 8px 12px;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
}
.tab:hover { background: #fff; }
.tab.active {
  background: #fff;
  color: var(--teal);
  border-color: var(--border);
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.06);
}

.preview-scroll {
  flex: 1;
  overflow: auto;
  padding: 16px;
  background: #e2e8f0;
}
.doc-panel {
  display: none;
  background: #fff;
  margin: 0 auto;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.12);
}
.doc-panel.active { display: block; }

/* Shared doc chrome */
.doc {
  width: 210mm;
  max-width: 100%;
  min-height: 260mm;
  padding: 14mm 12mm;
  color: #111;
  font-size: 12px;
  line-height: 1.35;
  background: #fff;
}
.doc table { border-collapse: collapse; width: 100%; }
.doc .muted { color: #555; }
.doc .right { text-align: right; }
.doc .center { text-align: center; }
.doc .bold { font-weight: 700; }
.doc pre.addr {
  margin: 0;
  font: inherit;
  white-space: pre-wrap;
  word-break: break-word;
}

/* Packing list / Invoice shared header style */
.pl-head, .inv-head {
  display: grid;
  grid-template-columns: 1.2fr 0.8fr;
  gap: 8px;
  margin-bottom: 8px;
}
.pl-title, .inv-title {
  text-align: right;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.08em;
}
.pl-sub, .inv-sub {
  text-align: right;
  font-size: 16px;
  font-weight: 700;
  margin-top: 18px;
}
.meta-box {
  border: 1px solid #222;
  margin-top: 8px;
}
.meta-box td, .meta-box th {
  border: 1px solid #222;
  padding: 4px 6px;
  vertical-align: top;
}
.line-table th, .line-table td {
  border: 1px solid #222;
  padding: 5px 6px;
  vertical-align: top;
}
.line-table th {
  background: #f3f4f6;
  font-weight: 700;
  text-align: center;
}

/* Sales / contract title: match invoice top-right title */
.sc-title-zh {
  text-align: right;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.08em;
}
.sc-title-en {
  text-align: right;
  font-size: 16px;
  font-weight: 700;
  margin-top: 18px;
}
.sc-company {
  font-size: 16px;
  font-weight: 700;
}
.sc-grid {
  display: grid;
  grid-template-columns: 1.4fr 0.9fr;
  gap: 8px;
  margin: 10px 0;
}
.sc-terms { margin-top: 14px; }
.sc-terms p { margin: 4px 0; }
.sc-sign {
  display: grid;
  grid-template-columns: 1fr 1fr;
  margin-top: 36px;
  text-align: center;
}

/* Customs declaration — official PRC grid, fill landscape A4 */
.cd {
  width: 297mm;
  height: 210mm;
  min-height: 210mm;
  max-width: 100%;
  padding: 4mm 5mm;
  font-size: 11px;
  background: #fff;
  color: #111;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
}
.cd h1 {
  margin: 0 0 6px;
  text-align: center;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 0.12em;
  flex: 0 0 auto;
}
.cd > .cd-top,
.cd > .cd-meta,
.cd > .cd-pack-row,
.cd > .cd-divider,
.cd > .cd-sign {
  flex: 0 0 auto;
}
.cd-top {
  display: grid;
  grid-template-columns: 1.2fr 1.2fr 0.9fr;
  gap: 10px;
  margin-bottom: 4px;
  color: #111;
  font-size: 11px;
}
.cd-table {
  width: 100%;
  border: 1.5px solid #111;
  border-collapse: collapse;
  table-layout: fixed;
}
.cd-table td, .cd-table th {
  border: 1px solid #111;
  padding: 2px 4px;
  vertical-align: top;
  word-break: break-word;
}
.cd-table .lab {
  color: #111;
  font-size: 10px;
  line-height: 1.2;
}
.cd-table .val {
  font-weight: 600;
  min-height: 16px;
  line-height: 1.3;
  font-size: 11px;
}
.cd-tall-sm { min-height: 24px; }
.cd-tall { min-height: 44px; }
.cd-meta td { height: 42px; }
.cd-pack-row {
  border-top: none;
}
.cd-items {
  border-top: none;
  flex: 1 1 auto;
  width: 100%;
  height: 100%;
}
.cd-items th {
  background: #fff;
  font-size: 10px;
  font-weight: 600;
  text-align: center;
  padding: 4px 2px;
  vertical-align: middle;
}
.cd-items td {
  font-size: 10px;
  height: 22px;
}
.cd-items tbody tr.cd-row-main td,
.cd-items tbody tr.cd-row-spare td {
  height: 50%;
  min-height: 100px;
  vertical-align: top;
  padding-top: 6px;
}
/* commodity data rows: no internal vertical rules (keep outer + header) */
.cd-items tbody td {
  border-left: none;
  border-right: none;
}
.cd-items tbody td:first-child {
  border-left: 1px solid #111;
}
.cd-items tbody td:last-child {
  border-right: 1px solid #111;
}
.cd-items .center { text-align: center; vertical-align: middle; }
.cd-items .bold { font-weight: 700; }
.cd-spec {
  margin-top: 1px;
  font-size: 9px;
  font-weight: 400;
  color: #222;
  line-height: 1.35;
}
.cd-price { line-height: 1.25; }
.cd-empty td {
  border-top-style: solid;
  vertical-align: top;
  padding-top: 6px;
}
.cd-divider {
  border-top: none;
}
.cd-divider td {
  height: 28px;
  padding: 0 !important;
  border: 1px solid #111;
}
.cd-sign {
  border-top: none;
}
.cd-sign-left {
  width: 72%;
  height: 100px;
  position: relative;
  padding: 6px 8px !important;
  vertical-align: top !important;
}
.cd-sign-top {
  display: flex;
  gap: 48px;
  font-size: 11px;
}
.cd-sign-mid {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 10px;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  font-size: 11px;
}
.cd-legal {
  font-size: 11px;
  text-align: right;
  line-height: 1.45;
  max-width: 62%;
}
.cd-customs-note {
  width: 28%;
  padding: 4px 6px !important;
  vertical-align: top !important;
}
.cd-stamp-box {
  min-height: 78px;
}

.print-root { display: none; }

@media (max-width: 1100px) {
  body { overflow: auto; }
  .app { height: auto; min-height: 100vh; overflow: visible; }
  .layout {
    grid-template-columns: 1fr;
    overflow: visible;
  }
  .form-pane, .preview-pane { max-height: none; overflow: visible; }
  .preview-scroll { max-height: none; }
}

@media print {
  @page {
    size: A4 portrait;
    margin: 12mm;
  }
  @page customs-landscape {
    size: A4 landscape;
    margin: 6mm;
  }
  html, body {
    background: #fff !important;
    overflow: visible !important;
    height: auto !important;
    margin: 0 !important;
    padding: 0 !important;
  }
  /* Hide the interactive app chrome for all print modes */
  .topbar,
  .form-pane,
  .tabs,
  .page-credit,
  .pdf-export-host {
    display: none !important;
  }

  /* Default: single-doc print from preview */
  body:not(.printing-all) .app,
  body:not(.printing-all) .layout,
  body:not(.printing-all) .preview-pane,
  body:not(.printing-all) .preview-scroll {
    display: block !important;
    overflow: visible !important;
    height: auto !important;
    background: #fff !important;
    border: none !important;
    box-shadow: none !important;
    padding: 0 !important;
    margin: 0 !important;
  }
  body:not(.printing-all) .doc-panel {
    display: none !important;
    box-shadow: none !important;
  }
  body:not(.printing-all) .doc-panel.print-now {
    display: block !important;
    break-after: auto;
    page-break-after: auto;
  }
  body:not(.printing-all) .print-root {
    display: none !important;
  }

  /* Print-all: only #printRoot, avoid blank leading page from empty .app */
  body.printing-all .app {
    display: none !important;
  }
  body.printing-all .print-root.print-mode {
    display: block !important;
    position: static !important;
    width: auto !important;
    height: auto !important;
    margin: 0 !important;
    padding: 0 !important;
  }
  body.printing-all .print-root.print-mode .doc-panel {
    display: block !important;
    box-shadow: none !important;
    break-after: page;
    page-break-after: always;
    break-inside: avoid-page;
  }
  body.printing-all .print-root.print-mode .doc-panel:last-child {
    break-after: auto;
    page-break-after: auto;
  }
  body.printing-all .print-root.print-mode .doc-panel:first-child {
    break-before: auto;
    page-break-before: auto;
  }

  .doc {
    width: auto !important;
    min-height: auto !important;
  }

  /* Customs: fill one landscape A4, no blank page 2 */
  .cd {
    width: 100% !important;
    height: 198mm !important;
    min-height: 0 !important;
    max-height: 198mm !important;
    padding: 2.5mm 3mm !important;
    box-sizing: border-box !important;
    overflow: hidden !important;
    page: auto;
    display: flex !important;
    flex-direction: column !important;
  }
  body.printing-customs .doc-panel.print-now {
    break-after: avoid !important;
    page-break-after: avoid !important;
    break-inside: auto !important;
    page-break-inside: auto !important;
  }
  body.printing-customs .cd,
  body.printing-customs .cd * {
    break-after: avoid;
    page-break-after: avoid;
  }
  body.printing-customs .cd-meta td {
    height: auto !important;
    padding: 1px 3px !important;
  }
  body.printing-customs .cd-table .val {
    min-height: 12px !important;
  }
  body.printing-customs .cd-tall-sm { min-height: 14px !important; }
  body.printing-customs .cd-tall { min-height: 22px !important; }
  body.printing-customs .cd-divider td { height: 14px !important; }
  body.printing-customs .cd-sign-left {
    height: auto !important;
    min-height: 52px !important;
  }
  body.printing-customs .cd-stamp-box { min-height: 44px !important; }
  body.printing-customs .cd h1 {
    margin: 0 0 3px !important;
    font-size: 16px !important;
  }
  body.printing-customs .cd-items,
  body.printing-all .doc-panel[data-print="customs"] .cd-items {
    flex: 1 1 auto !important;
    height: 100% !important;
  }
  body.printing-customs .cd-items tbody tr.cd-row-main td,
  body.printing-customs .cd-items tbody tr.cd-row-spare td,
  body.printing-all .doc-panel[data-print="customs"] .cd-items tbody tr.cd-row-main td,
  body.printing-all .doc-panel[data-print="customs"] .cd-items tbody tr.cd-row-spare td {
    height: 50% !important;
    min-height: 80px !important;
  }

  /* print-all: only customs panel switches to landscape named page */
  body.printing-all .doc-panel[data-print="customs"] {
    page: customs-landscape;
    break-inside: auto !important;
    page-break-inside: auto !important;
  }
  body.printing-all .doc-panel[data-print="customs"] .cd {
    page: auto;
  }
}


/* PDF export host: keep docs visible for html2canvas */
.pdf-export-host .doc {
  display: block !important;
  background: #fff !important;
  color: #111 !important;
  box-shadow: none !important;
}
.pdf-export-host .doc * {
  visibility: visible !important;
}

