:root {
  --bg: #f4efe7;
  --panel: #fffaf4;
  --panel-strong: #fff;
  --line: #d7c8b4;
  --text: #2d241d;
  --muted: #6d5e52;
  --accent: #875b2f;
  --accent-soft: #f0e2cf;
  --danger: #b04b38;
  --ok: #2d7a4b;
  --warn: #b58528;
  --shadow: 0 18px 45px rgba(74, 51, 31, 0.1);
  --font-ui: "Noto Sans SC", "PingFang SC", "Microsoft YaHei", sans-serif;
  --font-head: "Source Han Serif SC", "Songti SC", serif;
  --font-invoice: "SimSun", "NSimSun", "Songti SC", "STSong", serif;
}

@font-face {
  font-family: "InvoiceSourceHan";
  src: url("/assets/invoice-template/SourceHanSansSC-Regular.otf") format("opentype");
  font-weight: 400;
  font-style: normal;
}

html[data-locale="zh-Hant"] {
  --font-ui: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  --font-head: "Noto Serif TC", "Songti TC", serif;
}

html[data-font="serif"] {
  --font-ui: var(--font-head);
}

html[data-font="mono"] {
  --font-ui: "JetBrains Mono", "SFMono-Regular", "Consolas", monospace;
  --font-head: "JetBrains Mono", "SFMono-Regular", "Consolas", monospace;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(circle at top left, rgba(255, 255, 255, 0.9), transparent 32%),
    linear-gradient(180deg, #f8f2ea 0%, var(--bg) 100%);
  color: var(--text);
  font-family: var(--font-ui);
}

body {
  padding: 20px;
}

button,
input,
select,
textarea {
  font: inherit;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(840px, 1fr) 380px;
  gap: 18px;
  align-items: start;
}

.panel {
  background: var(--panel);
  border: 1px solid rgba(135, 91, 47, 0.18);
  border-radius: 18px;
  box-shadow: var(--shadow);
}

.entry-panel {
  padding: 22px;
}

.login-panel {
  max-width: 520px;
  margin: 0 auto 18px;
  padding: 22px;
}

.login-form {
  display: grid;
  gap: 14px;
}

.index-panel {
  position: sticky;
  top: 20px;
  padding: 18px;
  max-height: calc(100vh - 40px);
  overflow: auto;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
  margin-bottom: 18px;
}

.panel-header h1,
.panel-header h2,
.panel-header h3 {
  margin: 0;
  font-family: var(--font-head);
}

.panel-header-actions {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.subtitle {
  margin-top: 6px;
  color: var(--muted);
  font-size: 14px;
}

.meta-strip {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}

.badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 5px 10px;
  font-size: 12px;
  background: var(--accent-soft);
  color: var(--accent);
}

.editing-note {
  margin-bottom: 16px;
  padding: 12px 14px;
  border-radius: 14px;
  background: rgba(240, 226, 207, 0.7);
  border: 1px solid rgba(135, 91, 47, 0.18);
  color: var(--muted);
  font-size: 13px;
}

.restore-banner {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  padding: 12px 16px;
  background: #fff4df;
  border: 1px solid #f0cb81;
  border-radius: 14px;
  margin-bottom: 16px;
}

.restore-banner span {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 13px;
}

.restore-actions {
  display: flex;
  gap: 10px;
}

.ui-settings {
  display: flex;
  gap: 10px;
  align-items: end;
  flex-wrap: wrap;
}

.ui-settings.compact {
  gap: 8px;
  align-items: center;
}

.ui-settings label {
  display: grid;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
}

.ui-settings-group {
  display: grid;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
}

.ui-settings.compact label {
  min-width: 96px;
}

.ui-settings.compact .ui-settings-group {
  min-width: 120px;
}

.ui-settings select.field {
  min-width: 108px;
  padding: 8px 10px;
}

.ui-locale-switch {
  display: inline-flex;
  gap: 6px;
}

.ui-locale-button {
  padding: 8px 12px;
  min-width: 52px;
}

.field-grid {
  display: grid;
  grid-template-columns: minmax(260px, 1.2fr) repeat(5, minmax(96px, 1fr));
  gap: 10px;
  align-items: center;
}

.customer-fields {
  display: grid;
  grid-template-columns: minmax(220px, 1.15fr) 160px minmax(260px, 1.3fr) minmax(180px, 0.85fr);
  gap: 12px;
  align-items: start;
}

.customer-address-field textarea {
  min-height: 78px;
  resize: vertical;
}

.line-items-head,
.line-item-row {
  display: grid;
  grid-template-columns: minmax(260px, 1.4fr) 120px minmax(200px, 1.2fr) 140px 100px 140px 120px 88px;
  gap: 10px;
  align-items: center;
}

.line-items-head {
  padding: 0 0 10px;
  margin-top: 18px;
  color: var(--muted);
  font-size: 13px;
}

.line-items {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.line-item-row {
  position: relative;
  padding: 10px;
  border-radius: 14px;
  border: 1px solid rgba(135, 91, 47, 0.16);
  background: rgba(255, 255, 255, 0.72);
}

.line-item-row.pending {
  border-color: rgba(181, 133, 40, 0.3);
}

.line-item-row.unmatched {
  border-color: rgba(176, 75, 56, 0.35);
  background: rgba(255, 244, 242, 0.95);
}

.line-item-row.matched {
  border-color: rgba(45, 122, 75, 0.24);
}

.field-label {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
}

.field,
.line-item-row input,
.line-item-row textarea,
.filters input,
.filters select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  background: var(--panel-strong);
  color: var(--text);
}

.line-item-row textarea {
  min-height: 42px;
  resize: vertical;
}

.field:focus,
.line-item-row input:focus,
.line-item-row textarea:focus,
.filters input:focus,
.filters select:focus {
  outline: 2px solid rgba(135, 91, 47, 0.2);
  border-color: var(--accent);
}

.readonly {
  background: #f6f1ea;
}

.search-wrap {
  position: relative;
}

.search-dropdown {
  position: absolute;
  left: 0;
  right: 0;
  top: calc(100% + 6px);
  z-index: 5;
  background: var(--panel-strong);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.search-option {
  padding: 10px 12px;
  cursor: pointer;
  border-bottom: 1px solid #f0e4d5;
}

.search-option:last-child {
  border-bottom: 0;
}

.search-option.active,
.search-option:hover {
  background: #f6eadb;
}

.search-option strong {
  display: block;
}

.search-meta {
  color: var(--muted);
  font-size: 12px;
}

.customer-hint {
  margin-top: 8px;
}

.customer-option-modal {
  position: sticky;
  top: 10px;
  z-index: 20;
  margin-bottom: 16px;
}

.customer-option-dialog {
  padding: 18px;
}

.customer-option-list {
  display: grid;
  gap: 10px;
}

.customer-option-card {
  display: grid;
  gap: 6px;
  width: 100%;
  text-align: left;
  padding: 12px 14px;
  border-radius: 14px;
  border: 1px solid rgba(135, 91, 47, 0.18);
  background: rgba(255, 255, 255, 0.92);
  color: var(--text);
}

.customer-option-card:hover {
  border-color: rgba(135, 91, 47, 0.4);
  background: #fff;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 88px;
  padding: 7px 10px;
  border-radius: 999px;
  font-size: 12px;
  border: 1px solid transparent;
}

.status-matched {
  color: var(--ok);
  background: rgba(45, 122, 75, 0.12);
  border-color: rgba(45, 122, 75, 0.18);
}

.status-unmatched {
  color: var(--danger);
  background: rgba(176, 75, 56, 0.12);
  border-color: rgba(176, 75, 56, 0.18);
}

.status-pending {
  color: var(--warn);
  background: rgba(181, 133, 40, 0.12);
  border-color: rgba(181, 133, 40, 0.18);
}

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 18px;
  gap: 12px;
}

.button-row {
  display: flex;
  gap: 10px;
}

button {
  border: 0;
  border-radius: 12px;
  padding: 10px 16px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}

button.secondary {
  background: #e8dccf;
  color: var(--text);
}

button.ghost {
  background: transparent;
  color: var(--accent);
  border: 1px solid rgba(135, 91, 47, 0.3);
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.hint {
  font-size: 13px;
  color: var(--muted);
}

.filters {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 14px 0 12px;
}

.index-actions {
  display: flex;
  gap: 10px;
  margin-bottom: 12px;
}

.order-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.order-card {
  border: 1px solid rgba(135, 91, 47, 0.16);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.8);
  padding: 12px;
}

.order-card.selected {
  border-color: rgba(135, 91, 47, 0.45);
  box-shadow: 0 8px 24px rgba(135, 91, 47, 0.12);
}

.order-card-head {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: flex-start;
}

.order-card-title {
  font-weight: 700;
}

.order-card-meta {
  color: var(--muted);
  font-size: 12px;
  margin-top: 6px;
}

.order-card-actions {
  display: flex;
  gap: 10px;
  margin-top: 12px;
}

.preview-box {
  margin-top: 18px;
  border: 1px dashed rgba(135, 91, 47, 0.28);
  border-radius: 14px;
  padding: 14px;
  background: rgba(255, 255, 255, 0.55);
}

.preview-box h3 {
  margin: 0 0 12px;
  font-family: var(--font-head);
}

.preview-list {
  margin: 0;
  padding-left: 18px;
}

.preview-list li {
  margin-bottom: 8px;
}

.preview-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 12px;
}

.preview-table th,
.preview-table td {
  border: 1px solid #e5d7c4;
  padding: 8px 10px;
  text-align: left;
  font-size: 13px;
}

.delivery-shell {
  display: none;
  margin-top: 20px;
  padding: 18px;
  background: #fff;
  border-radius: 18px;
  border: 1px solid rgba(135, 91, 47, 0.18);
}

.delivery-shell.visible {
  display: block;
}

.delivery-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
}

.invoice-summary-list {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.invoice-summary-row {
  display: grid;
  gap: 4px;
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.7);
  border: 1px solid rgba(135, 91, 47, 0.12);
}

.invoice-document {
  display: grid;
  gap: 16px;
  background: #f5f1ea;
}

.invoice-page {
  width: 210mm;
  min-height: 297mm;
  margin: 0 auto;
  padding: 0;
  background: #fff;
  color: #201914;
  border: 1px solid #d9cbb7;
  box-shadow: 0 18px 45px rgba(74, 51, 31, 0.08);
  font-family: var(--font-invoice);
  page-break-after: always;
  break-after: page;
  -webkit-font-smoothing: antialiased;
  text-rendering: geometricPrecision;
}

.invoice-page:last-child {
  page-break-after: auto;
  break-after: auto;
}

.invoice-frame {
  position: relative;
  width: 209.9mm;
  min-height: 297.04mm;
  font-family: "InvoiceSourceHan", var(--font-invoice);
  color: #000;
}

.invoice-canvas {
  position: relative;
  width: 209.9mm;
  min-height: 297.04mm;
}

.invoice-header {
  position: absolute;
  left: 0;
  top: 0;
  width: 209.9mm;
  text-align: center;
}

.company-name-zh {
  position: absolute;
  top: 16.8mm;
  left: 50%;
  transform: translateX(-50%);
  width: 90mm;
  font-size: 14px;
  font-weight: 700;
  line-height: 16px;
}

.company-name-en {
  position: absolute;
  top: 22.7mm;
  left: 50%;
  transform: translateX(-50%);
  width: 120mm;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 13px;
  font-weight: 700;
  line-height: 15px;
}

.company-address-zh,
.company-address-en,
.company-contact {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  white-space: nowrap;
}

.company-address-zh {
  top: 30.1mm;
  font-size: 7px;
  line-height: 9px;
}

.company-address-en {
  top: 34.1mm;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 7px;
  line-height: 9px;
}

.company-contact {
  top: 38.7mm;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 7.5px;
  line-height: 9px;
}

.customer-box {
  position: absolute;
  left: 11.2mm;
  top: 43.8mm;
  width: 99.2mm;
  height: 41.3mm;
  border: 0.5px solid #1b1b1b;
  border-radius: 2mm;
}

.invoice-stamp {
  position: absolute;
  left: 110.4mm;
  top: 54mm;
  width: 23mm;
  text-align: center;
}

.stamp-zh {
  font-size: 22px;
  font-weight: 700;
  line-height: 24px;
}

.stamp-en {
  margin-top: 1.5mm;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 12px;
  font-weight: 700;
  line-height: 14px;
}

.ref-box {
  position: absolute;
  left: 134mm;
  top: 43.8mm;
  width: 60mm;
}

.field-row {
  position: absolute;
  left: 0;
  width: 100%;
}

.field-row .label-zh {
  position: absolute;
  left: 2.2mm;
  top: 0;
  font-size: 7.5px;
  line-height: 9px;
}

.field-row .label-en {
  position: absolute;
  left: 2.2mm;
  top: 3.6mm;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 6px;
  line-height: 7.5px;
  color: #555;
}

.field-row .value {
  position: absolute;
  left: 26mm;
  right: 1mm;
  top: 0.2mm;
  font-size: 8.5px;
  line-height: 10.5px;
  word-break: break-word;
}

.customer-box .account-row {
  top: 5.5mm;
}

.customer-box .name-row {
  top: 13.5mm;
}

.customer-box .address-row {
  top: 21.5mm;
}

.customer-box .address-row .value {
  line-height: 9.5px;
}

.customer-box .tel-row {
  top: 35mm;
}

.ref-box .compact .label-zh,
.ref-box .compact .label-en {
  left: 0;
}

.ref-box .compact .value {
  left: 25mm;
  right: 0;
}

.ref-box .ref-row {
  top: 5.5mm;
}

.ref-box .payment-row {
  top: 13.5mm;
}

.ref-box .delivery-row {
  top: 21.5mm;
}

.ref-box .order-row {
  top: 28.5mm;
}

.ref-box .date-row {
  top: 35mm;
}

.item-table {
  position: absolute;
  left: 11.2mm;
  top: 88.5mm;
  width: 187.2mm;
}

.item-table table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  border: 0.5px solid #1b1b1b;
}

.item-table th,
.item-table td {
  border: 0.5px solid #1b1b1b;
  padding: 1mm 1.2mm;
  vertical-align: top;
}

.item-table th {
  height: 9.9mm;
  text-align: center;
  font-weight: 400;
}

.item-table th .zh {
  display: block;
  font-size: 8px;
  line-height: 10px;
  font-weight: 700;
}

.item-table th .en {
  display: block;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 6.5px;
  line-height: 8px;
}

.item-table td {
  height: 8mm;
  font-size: 8.5px;
  line-height: 10.5px;
}

.col-item {
  width: 9mm;
}

.col-code {
  width: 21.2mm;
}

.col-desc {
  width: 64.5mm;
}

.col-qty {
  width: 15.9mm;
}

.col-unit {
  width: 11.3mm;
}

.col-price {
  width: 22.4mm;
}

.col-less {
  width: 15.8mm;
}

.col-amount {
  width: 27.1mm;
}

.ta-center {
  text-align: center;
}

.ta-right {
  text-align: right;
  font-family: Helvetica, Arial, sans-serif;
}

.eoe-section {
  position: absolute;
  left: 11.2mm;
  top: 229.3mm;
  width: 121.9mm;
  height: 15.8mm;
  border-left: 0.5px solid #1b1b1b;
  border-top: 0.5px solid #1b1b1b;
  border-bottom: 0.5px solid #1b1b1b;
}

.total-words-line {
  position: absolute;
  left: 2mm;
  right: 2mm;
  top: 10.8mm;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 8.5px;
  font-weight: 700;
  line-height: 10.5px;
  white-space: nowrap;
}

.total-words-label {
  margin-right: 2mm;
}

.eoe-mark {
  position: absolute;
  left: 0;
  bottom: 2mm;
  width: 100%;
  text-align: center;
  font-size: 7.5px;
  line-height: 9px;
}

.total-section {
  position: absolute;
  left: 133.1mm;
  top: 229.3mm;
  width: 65.3mm;
  height: 15.8mm;
  border: 0.5px solid #1b1b1b;
}

.total-title-zh {
  position: absolute;
  left: 6mm;
  top: 1.2mm;
  font-size: 9px;
  font-weight: 700;
}

.total-currency {
  position: absolute;
  left: 20mm;
  top: 8mm;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 10px;
  font-weight: 700;
}

.total-value {
  position: absolute;
  right: 2.5mm;
  top: 7.5mm;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 10px;
  font-weight: 700;
}

.total-title-en {
  position: absolute;
  left: 6mm;
  bottom: 1.5mm;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 6.5px;
  line-height: 8px;
  color: #666;
}

.remarks-box {
  position: absolute;
  left: 11.2mm;
  top: 245.1mm;
  width: 109.8mm;
  height: 33.7mm;
  border: 0.5px solid #1b1b1b;
  border-radius: 1.5mm;
}

.remarks-label-zh {
  position: absolute;
  left: 2mm;
  top: 2mm;
  font-size: 8px;
  line-height: 10px;
}

.remarks-label-en {
  position: absolute;
  left: 2mm;
  top: 6.8mm;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 6px;
  line-height: 7.5px;
  color: #555;
}

.remarks-content {
  position: absolute;
  left: 16mm;
  right: 2mm;
  top: 2.2mm;
  font-size: 8px;
  line-height: 10px;
  white-space: pre-wrap;
  word-break: break-word;
}

.signature-section {
  position: absolute;
  left: 121mm;
  top: 245.1mm;
  width: 76.3mm;
  height: 33.7mm;
}

.signature-company-en {
  position: absolute;
  left: 0.8mm;
  top: 2mm;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 7.5px;
  line-height: 9.5px;
}

.signature-company-zh {
  position: absolute;
  left: 0.8mm;
  top: 6.2mm;
  font-size: 7.5px;
  line-height: 9.5px;
}

.signature-box {
  position: absolute;
  left: 0;
  top: 12mm;
  width: 76.3mm;
  height: 21.7mm;
  border: 0.5px solid #1b1b1b;
  border-radius: 1.5mm;
}

.authorized-signature {
  position: absolute;
  left: 0;
  bottom: -4.8mm;
  width: 76.3mm;
  text-align: center;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 7.5px;
  font-weight: 700;
  line-height: 9.5px;
}

.invoice-page-index {
  position: absolute;
  right: 11.5mm;
  top: 282mm;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 7px;
}

.invoice-printed-at {
  position: absolute;
  left: 11.2mm;
  top: 283mm;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 7px;
}

.empty-state {
  padding: 18px;
  text-align: center;
  color: var(--muted);
  border: 1px dashed rgba(135, 91, 47, 0.22);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.45);
}

.badge-warn {
  color: var(--warn);
  background: rgba(181, 133, 40, 0.14);
}

.danger-text {
  color: var(--danger);
  border-color: rgba(176, 75, 56, 0.3);
}

@media (max-width: 1280px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .index-panel {
    position: static;
    max-height: none;
  }

  .customer-fields {
    grid-template-columns: 1fr;
  }
}
