:root {
  --paper: #fffdf0;
  --ink: #1f2a32;
  --line: #4d5960;
  --dash: rgba(77, 89, 96, 0.5);
  --accent: #265d73;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--ink);
  font-family: "Courier New", "SimSun", monospace;
  background:
    linear-gradient(90deg, rgba(38, 93, 115, 0.08) 1px, transparent 1px),
    linear-gradient(rgba(38, 93, 115, 0.08) 1px, transparent 1px),
    #e7edf0;
  background-size: 22px 22px;
}

.page-shell {
  width: min(1120px, calc(100% - 32px));
  margin: 24px auto 48px;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
  padding: 16px 18px;
  border: 1px solid rgba(31, 42, 50, 0.15);
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.84);
  box-shadow: 0 14px 40px rgba(31, 42, 50, 0.12);
}

.toolbar h1 {
  margin: 0 0 5px;
  font-size: 24px;
  letter-spacing: 2px;
}

.toolbar p {
  margin: 0;
  color: #53616a;
  font-size: 14px;
}

.toolbar-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
}

.toolbar-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 12px;
}

.toolbar button,
.toolbar-link,
.file-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 100px;
  border: 0;
  border-radius: 999px;
  padding: 12px 18px;
  color: #fff;
  font: inherit;
  text-decoration: none;
  cursor: pointer;
  background: var(--accent);
  box-shadow: 0 10px 22px rgba(38, 93, 115, 0.26);
}

.toolbar-link.secondary,
.toolbar button.secondary {
  color: var(--accent);
  background: #e7f1f4;
  box-shadow: none;
}

.file-input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.bill-paper {
  position: relative;
  overflow: hidden;
  min-height: 760px;
  padding: 24px 28px 28px;
  border: 1px solid var(--line);
  background:
    repeating-linear-gradient(0deg, rgba(38, 93, 115, 0.05) 0 1px, transparent 1px 24px),
    linear-gradient(90deg, transparent 0 18px, rgba(38, 93, 115, 0.08) 18px 19px, transparent 19px),
    var(--paper);
  box-shadow: 0 20px 58px rgba(31, 42, 50, 0.18);
}

.bill-paper::before,
.bill-paper::after {
  content: "";
  position: absolute;
  top: 12px;
  bottom: 12px;
  width: 10px;
  background: radial-gradient(circle, #d8e0dc 0 3px, transparent 4px);
  background-size: 10px 24px;
  opacity: 0.85;
}

.bill-paper::before {
  left: 8px;
}

.bill-paper::after {
  right: 8px;
}

.bill-title {
  text-align: center;
  padding-bottom: 10px;
  margin-bottom: 10px;
}

.company-title {
  width: 100%;
  border: 0;
  text-align: center;
  font-size: 32px;
  letter-spacing: 10px;
  font-weight: 700;
}

.bill-type {
  width: 220px;
  margin-top: 8px;
  border: 0;
  text-align: center;
  font-size: 18px;
  letter-spacing: 4px;
}

.bill-head {
  border-top: 2px double var(--line);
  border-bottom: 2px double var(--line);
  margin-bottom: 12px;
  padding: 8px 0;
}

.bill-head-row,
.bill-meta {
  display: grid;
  grid-template-columns: 1fr 1fr 2fr;
  gap: 10px 18px;
  font-size: 15px;
}

.bill-head-row + .bill-head-row {
  margin-top: 8px;
}

.bill-head-row label,
.bill-footer label {
  display: flex;
  align-items: center;
  min-width: 0;
  white-space: nowrap;
}

input {
  width: 100%;
  min-width: 0;
  border: 0;
  border-bottom: 1px dashed var(--line);
  outline: 0;
  color: var(--ink);
  font: inherit;
  background: transparent;
}

select {
  width: 100%;
  border: 0;
  border-bottom: 1px dashed var(--line);
  outline: 0;
  color: var(--ink);
  font: inherit;
  background: transparent;
}

.add-row-btn {
  min-height: 32px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--accent);
  font: inherit;
  cursor: pointer;
  background: rgba(255, 255, 255, 0.7);
}

.row-actions {
  display: flex;
  gap: 10px;
  margin: 14px 0 4px;
  flex-wrap: wrap;
}

.row-actions .add-row-btn {
  padding: 9px 18px;
  font-size: 14px;
  font-weight: 700;
}

.row-actions .add-row-btn:hover {
  background: var(--accent);
  color: #fff;
}

.readonly-mode .empty-row {
  display: none;
}

input[type="checkbox"] {
  width: auto;
  margin-right: 6px;
}

input:focus {
  background: rgba(38, 93, 115, 0.08);
}

.table-wrap {
  margin-top: 14px;
}

.bill-table {
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  font-size: 15px;
}

.bill-table th,
.bill-table td {
  height: 34px;
  border: 1px solid var(--line);
  padding: 4px 6px;
  text-align: center;
  vertical-align: middle;
}

.bill-table th {
  font-weight: 700;
  background: rgba(38, 93, 115, 0.09);
}

.bill-table td input {
  height: 26px;
  border-bottom: 0;
  text-align: center;
}

.bill-table .text-input {
  text-align: left;
}

.bill-table .money-cell {
  text-align: right;
  padding-right: 10px;
}

.col-no { width: 52px; }
.col-name { width: 28%; }
.col-unit { width: 72px; }
.col-qty { width: 92px; }
.col-price { width: 108px; }
.col-money { width: 118px; }
.col-note { width: 20%; }

.bill-total {
  display: grid;
  grid-template-columns: 1fr 230px;
  align-items: center;
  min-height: 46px;
  border: 1px solid var(--line);
  border-top: 0;
  font-size: 16px;
}

.total-cn {
  text-align: center;
  letter-spacing: 1px;
}

.total-num {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  border-left: 1px solid var(--line);
  font-weight: 700;
}

.bill-footer {
  margin-top: 16px;
  font-size: 15px;
}

.footer-note {
  margin-bottom: 14px;
  padding: 7px 10px;
  border: 1px dashed var(--dash);
  color: #3d4a52;
  font-size: 13px;
}

.footer-signs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 18px;
}

.qr-section {
  display: grid;
  grid-template-columns: 1fr 96px;
  align-items: center;
  gap: 14px;
  margin-top: 14px;
  padding: 12px 14px;
  border: 1px dashed var(--dash);
  border-radius: 10px;
  background: rgba(255, 253, 240, 0.92);
}

.qr-info {
  display: grid;
  gap: 5px;
  color: #3d4a52;
  font-size: 13px;
}

.qr-info strong {
  color: var(--ink);
  font-size: 15px;
}

.readonly-url {
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.qr-box {
  position: relative;
  width: 96px;
  height: 96px;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
  box-shadow: 0 6px 16px rgba(31, 42, 50, 0.12);
}

#qrCode {
  width: 100%;
  height: 100%;
  display: block;
  border: 0;
  background: #fff;
  object-fit: contain;
}

.qr-placeholder {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 6px;
  font-size: 12px;
  line-height: 1.4;
  color: #8a969e;
  background: repeating-linear-gradient(45deg, #f4f1e6 0 8px, #efeadd 8px 16px);
}

.qr-box.qr-empty #qrCode {
  visibility: hidden;
}

.qr-box.qr-empty .qr-placeholder {
  display: flex;
}

.qr-gen {
  position: absolute;
  left: -99999px;
  top: 0;
  width: 1px;
  height: 1px;
  overflow: hidden;
}

.readonly-mode .toolbar {
  display: flex;
}

.readonly-mode .toolbar > div:first-child h1,
.readonly-mode .toolbar > div:first-child p {
  display: none;
}

.readonly-mode .toolbar-actions > * {
  display: none;
}

.readonly-mode .toolbar-actions > #exportImgBtn {
  display: inline-flex;
}

.readonly-mode .manage-row,
.readonly-mode .readonly-url,
.readonly-mode .row-actions {
  display: none;
}

.readonly-mode input {
  pointer-events: none;
}

@media (max-width: 760px) {
  .page-shell {
    width: calc(100% - 18px);
    margin-top: 10px;
  }

  .toolbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .toolbar-actions {
    justify-content: flex-start;
  }

  .bill-paper {
    padding: 18px 14px;
    overflow-x: auto;
  }

  .bill-paper::before,
  .bill-paper::after {
    display: none;
  }

  .company-title {
    font-size: 24px;
    letter-spacing: 5px;
  }

  .bill-head-row,
  .bill-meta {
    grid-template-columns: 1fr;
  }

  .bill-table {
    min-width: 920px;
  }

  .bill-total {
    min-width: 920px;
  }

  .footer-signs {
    grid-template-columns: repeat(2, 1fr);
  }

  .qr-section {
    grid-template-columns: 1fr;
    justify-items: center;
    text-align: center;
    padding: 14px;
  }

  .qr-info {
    width: 100%;
  }

  .qr-box {
    width: 128px;
    height: 128px;
  }
}

@media print {
  @page {
    size: A4 portrait;
    margin: 5mm;
  }

  html,
  body {
    width: 100%;
    background: #fff;
  }

  .no-print {
    display: none !important;
  }

  .empty-row {
    display: none !important;
  }

  .page-shell {
    width: 100%;
    margin: 0;
  }

  .bill-paper {
    width: 100%;
    min-height: auto;
    padding: 0;
    border: 0;
    box-shadow: none;
    background: var(--paper);
    overflow: visible;
  }

  .bill-title {
    padding-bottom: 6px;
    margin-bottom: 8px;
  }

  .company-title {
    font-size: 24px;
    letter-spacing: 6px;
  }

  .bill-type {
    margin-top: 4px;
    font-size: 16px;
  }

  .bill-head {
    margin-bottom: 8px;
    padding: 5px 0;
  }

  .bill-head-row,
  .bill-meta {
    gap: 4px 10px;
    font-size: 12px;
  }

  .bill-head-row + .bill-head-row {
    margin-top: 5px;
  }

  .table-wrap {
    margin-top: 8px;
  }

  .bill-table {
    min-width: 0;
    font-size: 11px;
  }

  .bill-table th,
  .bill-table td {
    height: 24px;
    padding: 2px 4px;
  }

  .bill-table td input {
    height: 19px;
  }

  .bill-total {
    grid-template-columns: 1fr 170px;
    min-width: 0;
    min-height: 30px;
    font-size: 12px;
  }

  .bill-footer {
    margin-top: 6px;
    font-size: 12px;
  }

  .footer-note {
    margin-bottom: 6px;
    padding: 3px 7px;
    font-size: 11px;
  }

  .footer-signs {
    gap: 10px;
  }

  .qr-section {
    grid-template-columns: 1fr 72px;
    gap: 10px;
    margin-top: 6px;
    padding: 6px 8px;
    border-radius: 0;
    background: #fff;
    page-break-inside: avoid;
  }

  .qr-info {
    gap: 2px;
    font-size: 10px;
  }

  .qr-info strong {
    font-size: 12px;
  }

  .qr-box {
    width: 72px;
    height: 72px;
  }

  .col-no { width: 42px; }
  .col-unit { width: 54px; }
  .col-qty { width: 70px; }
  .col-price { width: 82px; }
  .col-money { width: 90px; }

  .bill-paper::before,
  .bill-paper::after {
    display: none;
  }

  input {
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }
}

.guest-banner {
  margin: 0 0 16px;
  padding: 12px 16px;
  background: #fff4e5;
  border: 1px solid #ffb74d;
  border-left: 4px solid #ff9800;
  border-radius: 10px;
  color: #8a5a00;
  font-size: 14px;
  line-height: 1.6;
}

@media print {
  .guest-banner {
    display: none !important;
  }
}

/* ===== 导出图片专用干净视图（html2canvas 不读取 @media print，需单独定义） ===== */
.export-host {
  position: fixed;
  left: -99999px;
  top: 0;
  width: 0;
  height: 0;
  overflow: hidden;
}

.export-paper {
  width: 794px;
  padding: 24px 28px 28px;
  background: #fffdf0;
  color: #1f2a32;
  font-family: "Microsoft YaHei", "PingFang SC", "Hiragino Sans GB", sans-serif;
  box-sizing: border-box;
}

.export-paper .export-title {
  text-align: center;
  padding-bottom: 10px;
  margin-bottom: 10px;
  border-bottom: 1px dashed #c5cdd1;
}

.export-paper .export-company {
  font-size: 28px;
  letter-spacing: 8px;
  font-weight: 700;
  color: #1f2a32;
}

.export-paper .export-type {
  margin-top: 6px;
  font-size: 16px;
  letter-spacing: 4px;
  color: #3d4a52;
}

.export-paper .export-head {
  border-top: 2px double #b8c2c7;
  border-bottom: 2px double #b8c2c7;
  margin-bottom: 12px;
  padding: 8px 0;
  font-size: 13px;
}

.export-paper .export-head-row {
  display: grid;
  grid-template-columns: 1fr 1fr 2fr;
  gap: 6px 12px;
  line-height: 1.8;
}

.export-paper .export-head-row + .export-head-row {
  margin-top: 5px;
}

.export-paper .export-head-row span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.export-paper .export-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  font-size: 12px;
  margin-top: 8px;
}

.export-paper .export-table th,
.export-paper .export-table td {
  height: 26px;
  border: 1px solid #b8c2c7;
  padding: 2px 6px;
  text-align: center;
  vertical-align: middle;
}

.export-paper .export-table th {
  font-weight: 700;
  background: rgba(38, 93, 115, 0.09);
}

.export-paper .export-table td.text-left { text-align: left; }
.export-paper .export-table td.text-right { text-align: right; padding-right: 10px; }

.export-paper .export-table .col-no { width: 52px; }
.export-paper .export-table .col-unit { width: 72px; }
.export-paper .export-table .col-qty { width: 92px; }
.export-paper .export-table .col-price { width: 108px; }
.export-paper .export-table .col-money { width: 118px; }

.export-paper .export-total {
  display: grid;
  grid-template-columns: 1fr 230px;
  align-items: center;
  min-height: 38px;
  border: 1px solid #b8c2c7;
  border-top: 0;
  font-size: 13px;
}

.export-paper .export-total > div {
  padding: 8px;
  text-align: center;
}

.export-paper .export-total > div + div {
  border-left: 1px solid #b8c2c7;
  font-weight: 700;
}

.export-paper .export-footer {
  margin-top: 14px;
  font-size: 13px;
}

.export-paper .export-note {
  margin-bottom: 10px;
  padding: 6px 10px;
  border: 1px dashed #b8c2c7;
  color: #3d4a52;
  font-size: 12px;
}

.export-paper .export-signs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
}

.export-paper .export-qr {
  display: grid;
  grid-template-columns: 1fr 96px;
  align-items: center;
  gap: 12px;
  margin-top: 12px;
  padding: 10px 12px;
  border: 1px dashed #b8c2c7;
  background: #fff;
}

.export-paper .export-qr-info {
  font-size: 12px;
  color: #3d4a52;
  line-height: 1.5;
  word-break: break-all;
}

.export-paper .export-qr-info strong {
  display: block;
  font-size: 14px;
  color: #1f2a32;
  margin-bottom: 2px;
}

.export-paper .export-qr-img {
  width: 96px;
  height: 96px;
  border: 1px solid #b8c2c7;
  border-radius: 6px;
  background: #fff;
  object-fit: contain;
}
