:root {
  color-scheme: light;
  --ink: #111827;
  --muted: #5d6878;
  --line: #d8dee8;
  --panel: #ffffff;
  --page: #ffffff;
  --accent: #0f766e;
  --accent-strong: #0b5f59;
  --bg: #eef2f7;
  --soft: #f7f9fc;
  font-family: Inter, "Segoe UI", Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
}

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

button {
  cursor: pointer;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 18px 28px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(14px);
}

.app-header h1 {
  margin: 0;
  font-size: 22px;
  letter-spacing: 0;
}

.app-header p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(300px, 390px) minmax(680px, 1fr);
  gap: 24px;
  align-items: start;
  padding: 24px;
}

.editor {
  display: grid;
  gap: 14px;
}

.panel {
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.panel h2 {
  margin: 0 0 12px;
  font-size: 15px;
  letter-spacing: 0;
}

label {
  display: grid;
  gap: 6px;
  margin-bottom: 12px;
  color: #2f3b4b;
  font-size: 13px;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  padding: 9px 10px;
  outline: none;
}

textarea {
  resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.14);
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.terms-row {
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: 10px;
}

.section-row,
.item-row,
.inline-add,
.checkbox-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.section-row {
  justify-content: space-between;
  margin-bottom: 10px;
}

.item-row {
  display: grid;
  grid-template-columns: minmax(0, 1.3fr) 72px minmax(0, 1fr) 36px;
  margin-bottom: 8px;
}

.item-row input,
.item-row select {
  min-width: 0;
}

.amount-block {
  margin: 14px 0;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.amount-block label {
  margin-bottom: 6px;
}

.checkbox-row {
  display: flex;
  margin-bottom: 12px;
}

.checkbox-row input {
  width: 18px;
  height: 18px;
}

.inline-add {
  margin-top: 8px;
}

.option-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}

.option-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  max-width: 100%;
  padding: 4px 6px 4px 10px;
  border: 1px solid #cfd8e6;
  border-radius: 999px;
  background: #f8fafc;
  color: #334155;
  font-size: 12px;
}

.option-chip-name {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.option-chip-remove {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  width: 18px;
  height: 18px;
  border: none;
  border-radius: 50%;
  background: #e2e8f0;
  color: #475569;
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
}

.option-chip-remove:hover {
  background: #fecaca;
  color: #b42318;
}

.company-picker {
  position: relative;
}

.company-list {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 30;
  max-height: 230px;
  overflow-y: auto;
  margin-top: 4px;
  padding: 4px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 10px 28px rgba(15, 23, 42, 0.16);
}

.company-item {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  border-radius: 6px;
  background: none;
  color: var(--ink);
  padding: 9px 12px;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.company-item:hover,
.company-item:focus-visible {
  background: #eef7f5;
  color: var(--accent-strong);
  outline: none;
}

.company-empty {
  padding: 10px 12px;
  color: var(--muted);
  font-size: 13px;
}

.company-match-hint {
  color: var(--muted);
  font-size: 12px;
  margin-left: 4px;
  font-weight: 400;
}

.company-actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}

.company-actions button {
  flex: 1;
  min-height: 38px;
  padding: 0 14px;
  font-size: 14px;
  font-weight: 600;
}

.danger-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 38px;
  border: 1px solid #fca5a5;
  border-radius: 6px;
  padding: 0 14px;
  background: #fef2f2;
  color: #b42318;
  font-weight: 600;
}

.danger-button:hover {
  border-color: #ef4444;
  background: #fee2e2;
}

.company-form-card {
  display: grid;
  gap: 0;
  max-height: 0;
  overflow: hidden;
  opacity: 0;
  transition: max-height 0.25s ease, opacity 0.2s ease, margin 0.25s ease;
  margin-top: 0;
}

.company-form-card.open {
  max-height: 760px;
  opacity: 1;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.company-form-title {
  margin: 0 0 12px;
  font-size: 14px;
  color: var(--ink);
}

.company-form-card label {
  margin-bottom: 10px;
}

.company-form-card .primary-button {
  width: 100%;
}

.export-default {
  margin-top: 10px;
  border-color: #f59e0b;
  background: #f59e0b;
  color: #fff;
  font-weight: 700;
}

.export-default:hover {
  border-color: #d97706;
  background: #d97706;
}

.selected-company {
  display: grid;
  gap: 6px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
  color: #334155;
  font-size: 13px;
}

.selected-company strong {
  color: var(--ink);
}

.selected-company span {
  white-space: pre-line;
}

.id-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  margin-bottom: 12px;
}

.id-picker {
  position: relative;
  flex: 1;
}

.id-picker label {
  display: block;
  margin-bottom: 6px;
  color: #2f3b4b;
  font-size: 13px;
}

.id-display {
  width: 100%;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  padding: 9px 10px;
  font-size: 14px;
  cursor: pointer;
  outline: none;
}

.id-display:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.14);
}

.id-list {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 30;
  max-height: 230px;
  overflow-y: auto;
  margin-top: 4px;
  padding: 4px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 10px 28px rgba(15, 23, 42, 0.16);
}

.id-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  border: none;
  border-radius: 6px;
  background: none;
  color: var(--ink);
  padding: 9px 10px;
  font-size: 13px;
  cursor: pointer;
}

.id-item:hover,
.id-item:focus-visible {
  background: #eef7f5;
  color: var(--accent-strong);
  outline: none;
}

.id-text {
  flex: 1;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.id-empty {
  padding: 10px 12px;
  color: var(--muted);
  font-size: 13px;
}

.form-id-block {
  margin-bottom: 12px;
}

.form-id-block label {
  display: block;
  margin-bottom: 6px;
  color: #2f3b4b;
  font-size: 13px;
}

.form-id-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}

.form-id-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px 4px 10px;
  border: 1px solid #cfd8e6;
  border-radius: 999px;
  background: #f8fafc;
  color: #334155;
  font-size: 13px;
}

.form-id-remove {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border: none;
  border-radius: 50%;
  background: #e2e8f0;
  color: #475569;
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
}

.form-id-remove:hover {
  background: #fecaca;
  color: #b42318;
}

.form-id-add {
  display: flex;
  gap: 8px;
}

.form-id-add input {
  flex: 1;
  min-width: 0;
}

.form-id-add .secondary-button {
  width: auto;
  min-width: 72px;
}

.id-add-card {
  display: flex;
  gap: 8px;
  margin: 0 0 12px;
}

.id-add-card input {
  flex: 1;
  min-width: 0;
}

.id-add-card .primary-button {
  width: auto;
  min-width: 88px;
}

.primary-button,
.secondary-button,
.ghost-button,
.icon-button,
.remove-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 38px;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 0 14px;
}

.primary-button {
  background: var(--accent);
  color: #fff;
  font-weight: 700;
}

.primary-button:hover {
  background: var(--accent-strong);
}

.secondary-button {
  width: 100%;
  border-color: #b8c4d4;
  background: var(--soft);
  color: #263241;
}

.ghost-button,
.icon-button,
.remove-button {
  width: 38px;
  padding: 0;
  border-color: #b8c4d4;
  background: #fff;
  color: #263241;
}

.remove-button {
  color: #b42318;
}

.used-numbers {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}

.used-numbers button {
  border: 1px solid #cfd8e6;
  border-radius: 999px;
  background: #f8fafc;
  color: #334155;
  padding: 5px 10px;
  font-size: 12px;
}

.export-panel {
  position: sticky;
  bottom: 16px;
}

.export-panel .primary-button {
  width: 100%;
}

.preview-shell {
  overflow: auto;
  padding: 10px 10px 40px;
}

.invoice-page {
  width: 210mm;
  min-height: 297mm;
  margin: 0 auto;
  padding: 30mm 19mm 12mm;
  background: var(--page);
  box-shadow: 0 18px 50px rgba(28, 39, 56, 0.18);
  color: #000;
  font-family: "Microsoft YaHei", "微软雅黑", Arial, Helvetica, sans-serif;
  font-size: 9pt;
  line-height: 1.3;
}

.doc-company {
  text-align: center;
  font-size: 14.05pt;
  letter-spacing: 0;
  margin-bottom: 2mm;
}

.dash-rule {
  text-align: center;
  font-family: "Courier New", monospace;
  font-size: 9pt;
  line-height: 1;
  margin-bottom: 2mm;
  white-space: nowrap;
}

.invoice-title {
  text-align: center;
  font-family: Arial, "Microsoft YaHei", sans-serif;
  font-size: 23.05pt;
  font-weight: 700;
  line-height: 1;
  margin-bottom: 9mm;
}

.invoice-box {
  border-top: 1px solid #000;
  border-left: 1px solid #000;
}

.top-grid {
  display: grid;
  grid-template-columns: 43.2% 18% 38.8%;
  grid-template-rows: 8.5mm 8.5mm 8.5mm 19mm;
}

.bill-cell,
.meta-cell {
  border-right: 1px solid #000;
  border-bottom: 1px solid #000;
}

.bill-cell {
  grid-row: 1 / span 4;
  padding: 1.2mm 1.2mm;
}

.bill-label {
  font-size: 9pt;
  margin-bottom: 5mm;
}

.bill-company {
  font-family: Arial, "Microsoft YaHei", sans-serif;
  font-weight: 700;
  font-size: 9pt;
  margin-bottom: 2mm;
}

.bill-address {
  white-space: pre-line;
  font-size: 9pt;
  line-height: 1.45;
}

.meta-cell {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 2mm;
  text-align: center;
}

.meta-label,
.meta-value {
  font-size: 10.45pt;
}

.line-items {
  display: grid;
  grid-template-columns: 22.3% 38.9% 38.8%;
}

.line-items > div {
  display: flex;
  align-items: center;
  justify-content: center;
  border-right: 1px solid #000;
  border-bottom: 1px solid #000;
  padding: 0.5mm 2mm;
  text-align: center;
}

.line-items > div:nth-child(-n + 3) {
  min-height: 8.5mm;
  font-size: 10.45pt;
}

.line-items > div:nth-child(n + 4) {
  min-height: 11mm;
}

.line-items .item-text {
  font-size: 9pt;
}

.line-items .item-amount {
  font-size: 10.45pt;
}

.bill-box {
  width: 61.2%;
  margin-top: 9mm;
  border-top: 1px solid #000;
  border-left: 1px solid #000;
}

.bill-title {
  height: 7mm;
  display: flex;
  align-items: center;
  padding: 0 1.2mm;
  border-right: 1px solid #000;
  border-bottom: 1px solid #000;
  font-family: "Times New Roman", "Microsoft YaHei", serif;
  font-size: 9pt;
}

.bill-grid {
  display: grid;
  grid-template-columns: 36.2% 34.2% 29.6%;
}

.bill-grid > div {
  min-height: 11mm;
  display: flex;
  align-items: center;
  border-right: 1px solid #000;
  border-bottom: 1px solid #000;
  padding: 0.5mm 1mm;
  font-size: 9pt;
}

.bill-grid > div:nth-child(3n) {
  justify-content: center;
  text-align: center;
}

.bill-grid > div:nth-child(1),
.bill-grid > div:nth-child(4),
.bill-grid > div:nth-child(5) {
  font-family: "Times New Roman", serif;
}

.bill-grid .money-title {
  font-size: 9pt;
  line-height: 1.35;
}

.bill-grid .bill-amount {
  justify-content: center;
  font-size: 10.45pt;
}

.bank-info {
  margin-top: 24mm;
  padding-left: 3mm;
  font-size: 9pt;
  line-height: 1.3;
}

.bank-info p {
  margin: 0 0 3.5mm;
}

.bank-info:empty {
  display: none;
}

@media (max-width: 1120px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .preview-shell {
    padding-inline: 0;
  }
}

@media print {
  @page {
    size: A4;
    margin: 0;
  }

  html,
  body {
    width: 210mm;
    height: 297mm;
    margin: 0;
    background: #fff;
  }

  .app-header,
  .editor {
    display: none !important;
  }

  .workspace,
  .preview-shell {
    display: block;
    padding: 0;
    margin: 0;
    overflow: visible;
  }

  .invoice-page {
    width: 210mm;
    min-height: 297mm;
    margin: 0;
    box-shadow: none;
  }
}
