:root {
  --bg: #f6f0e5;
  --paper: #fffdf8;
  --panel: rgba(255, 250, 242, 0.88);
  --ink: #2f241d;
  --muted: #7f6c5f;
  --line: #dcccb7;
  --accent: #b86034;
  --accent-2: #2c6a5c;
  --shadow: 0 20px 40px rgba(92, 63, 43, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  height: 100vh;
  color: var(--ink);
  font-family: "Hiragino Sans", "Yu Gothic", sans-serif;
  background:
    radial-gradient(circle at top left, rgba(184, 96, 52, 0.18), transparent 30%),
    radial-gradient(circle at bottom right, rgba(44, 106, 92, 0.16), transparent 24%),
    linear-gradient(160deg, #f8f2e8 0%, #efe3d5 100%);
  overflow: hidden;
}

.page-shell {
  display: grid;
  grid-template-columns: 360px 1fr;
  gap: 24px;
  padding: 24px;
  height: 100vh;
}

.control-panel,
.preview-panel {
  min-width: 0;
}

.control-panel {
  height: calc(100vh - 48px);
  overflow-y: auto;
  padding-right: 6px;
}

.preview-panel {
  height: calc(100vh - 48px);
  overflow: hidden;
}

.brand-block,
.panel-card,
.preview-header,
.document-canvas {
  background: var(--panel);
  backdrop-filter: blur(14px);
  border: 1px solid rgba(255, 255, 255, 0.5);
  box-shadow: var(--shadow);
}

.brand-block,
.panel-card,
.preview-header {
  border-radius: 24px;
}

.brand-block {
  padding: 28px;
  margin-bottom: 18px;
}

.eyebrow {
  margin: 0 0 8px;
  font-size: 12px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--accent);
}

h1,
h2,
h3,
h4,
p {
  margin: 0;
}

h1 {
  font-family: "Avenir Next", "Hiragino Mincho ProN", serif;
  font-size: 34px;
  line-height: 1.1;
}

.lead {
  margin-top: 14px;
  line-height: 1.7;
  color: var(--muted);
}

.panel-card {
  padding: 22px;
  margin-bottom: 18px;
}

.panel-card h2 {
  margin-bottom: 16px;
  font-size: 18px;
}

.file-field,
.select-field {
  display: block;
  margin-bottom: 14px;
}

.checkbox-field {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
  font-size: 13px;
  font-weight: 700;
}

.checkbox-field input {
  width: auto;
}

.file-field span,
.select-field span {
  display: block;
  margin-bottom: 8px;
  font-size: 13px;
  font-weight: 700;
}

input[type="file"],
select,
button,
textarea {
  width: 100%;
  border-radius: 14px;
  border: 1px solid var(--line);
  font: inherit;
}

input[type="file"],
select,
textarea {
  padding: 12px 14px;
  background: rgba(255, 255, 255, 0.8);
}

#invoiceDueDateInput {
  font-size: 60%;
}

textarea {
  resize: vertical;
  min-height: 96px;
}

button {
  padding: 13px 16px;
  background: linear-gradient(135deg, var(--accent), #d78a5e);
  color: white;
  font-weight: 700;
  cursor: pointer;
}

button.secondary-button {
  background: linear-gradient(135deg, var(--accent-2), #4b8f7f);
}

button.ghost-button {
  margin-top: 10px;
  background: transparent;
  color: var(--accent-2);
  border-color: rgba(44, 106, 92, 0.35);
}

button:disabled,
select:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.muted,
.hint {
  margin-top: 10px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
}

.form-error {
  margin: 8px 0 0;
  color: #b23b2a;
  font-size: 13px;
  font-weight: 700;
}

.action-row {
  margin-top: 12px;
}

.invoice-record-picker {
  margin-bottom: 14px;
}

#recordSelectField[hidden] {
  display: none !important;
}

.is-hidden {
  display: none !important;
}

.picker-label {
  margin-bottom: 8px;
  font-size: 13px;
  font-weight: 700;
}

.invoice-record-list {
  display: grid;
  gap: 8px;
  max-height: 180px;
  overflow: auto;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.75);
}

.invoice-record-item {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  font-size: 13px;
}

.invoice-record-item input {
  width: auto;
  margin-top: 2px;
}

.summary-list {
  display: grid;
  gap: 12px;
}

.summary-list div {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding-bottom: 10px;
  border-bottom: 1px dashed var(--line);
}

.summary-list dt {
  color: var(--muted);
}

.summary-list dd {
  font-weight: 700;
}

.loaded-customers {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px dashed var(--line);
}

.loaded-customers-label {
  margin-bottom: 8px;
  font-size: 13px;
  font-weight: 700;
}

.preview-header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: end;
  padding: 24px 28px;
  margin-bottom: 20px;
}

.preview-header h2 {
  font-size: 28px;
}

.preview-meta {
  text-align: right;
  color: var(--muted);
  line-height: 1.6;
}

.document-canvas {
  height: calc(100vh - 168px);
  padding: 28px;
  border-radius: 28px;
  overflow: auto;
}

.empty-state {
  display: grid;
  place-items: center;
}

.empty-copy {
  text-align: center;
  max-width: 420px;
}

.empty-copy p {
  margin-top: 12px;
  color: var(--muted);
  line-height: 1.7;
}

.paper {
  width: 210mm;
  height: 281mm;
  margin: 0 auto;
  padding: 11mm 10mm 5mm;
  background: var(--paper);
  color: #222;
  box-shadow: 0 18px 60px rgba(67, 44, 30, 0.15);
  overflow: hidden;
}

.paper + .paper {
  margin-top: 20px;
}

.paper-top-rule,
.paper-bottom-rule {
  height: 2px;
  background: #ab3e2f;
}

.paper-top-rule {
  margin-bottom: 7mm;
}

.paper-bottom-rule {
  margin-top: 6mm;
  opacity: 1;
}

.paper-header {
  position: relative;
  min-height: 54mm;
  padding-top: 2mm;
  padding-bottom: 2mm;
}

.paper-brand {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  min-width: 80px;
}

.paper-brand-logo {
  display: block;
  width: 290px;
  max-width: 100%;
  height: auto;
  margin-left: auto;
  margin-bottom: 6px;
}

.doc-title {
  margin-top: 0;
  font-size: 38px;
  line-height: 1;
  font-family: "Avenir Next", "Hiragino Mincho ProN", serif;
}

.paper-meta {
  position: absolute;
  top: 21mm;
  right: 0;
  width: 92mm;
}

.paper-meta div,
.detail-ribbon div,
.totals-card div {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  font-size: 12px;
  line-height: 1.5;
}

.issuer-meta {
  display: grid !important;
  justify-content: end !important;
  gap: 0 !important;
  margin-bottom: 0;
  text-align: right;
  line-height: 1.45 !important;
}

.issuer-meta span {
  font-size: 8.5pt;
  color: #5f5750;
}

.paper-meta span,
.detail-ribbon span,
.card-label,
.totals-card span {
  color: #6b5a4b;
}

.address-grid,
.notes-grid {
  display: grid;
  grid-template-columns: 1.2fr 0.8fr;
  gap: 14px;
}

.invoice-summary {
  display: grid;
  gap: 6px;
  width: 62%;
  margin: 9mm 0 0;
  padding: 0 0 2px;
  background: transparent;
}

.invoice-summary-row {
  display: flex;
  align-items: baseline;
  gap: 14px;
  font-size: 13px;
  line-height: 1.4;
}

.amount-row {
  padding-bottom: 4px;
  border-bottom: 1px solid #222;
}

.invoice-summary strong {
  font-size: 28px;
  letter-spacing: 0.02em;
}

.invoice-summary strong[data-field="invoice-due-date"] {
  font-size: 13px;
  letter-spacing: 0;
}

.address-grid {
  margin-top: 0;
  min-height: 52mm;
  align-items: start;
}

.address-column {
  display: grid;
  gap: 0;
}

.notes-grid {
  margin-top: 7mm;
}

.address-card,
.note-card,
.order-ref-card,
.totals-card {
  padding: 10px 12px;
  border: 1px solid #c8cfd5;
  background: white;
}

.customer-card {
  border: none;
  background: transparent;
  padding: 0;
  margin-top: 0;
  margin-bottom: 0;
}

.customer-card p[data-field="primary-postal"],
.customer-card p[data-field="primary-address"],
.customer-card p[data-field="primary-tel"] {
  display: none;
}

.customer-card h4[data-field="primary-name"] {
  margin: 1mm 0 0;
  font-size: 28px;
  line-height: 1.25;
  letter-spacing: 0.03em;
}

.note-group {
  display: grid;
  grid-template-columns: 3fr 1fr;
  gap: 10px;
}

.address-card h4 {
  margin: 6px 0 6px;
  font-size: 15px;
  line-height: 1.25;
}

.address-card p,
.note-card p {
  line-height: 1.45;
  white-space: pre-wrap;
}

.note-card p[data-field="note"] {
  font-size: 12px;
}

.issuer-card h4 {
  font-size: 15px;
  margin-top: 6px;
}

.issuer-card {
  width: 100%;
  margin-top: 9mm;
  padding: 11px 14px;
}

.meta-row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  font-size: 13px;
  line-height: 1.7;
}

.meta-row strong {
  font-size: 15px;
}

.detail-ribbon {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 10px;
  padding: 8px 10px;
  background: #f8f6f2;
  border: 1px solid #c8cfd5;
  min-height: 54px;
}

.line-items {
  margin-top: 6mm;
  height: 412px;
  overflow: hidden;
  padding-bottom: 2px;
}

table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
}

th,
td {
  padding: 2px 5px;
  border-bottom: 1px dotted #9ea8b1;
  border-right: 1px solid #d4d9de;
  font-size: 7pt;
  line-height: 1.15;
  vertical-align: middle;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

th {
  background: #f4efe5;
  text-align: left;
  border-top: 1px solid #9ea8b1;
  border-bottom: 1px solid #9ea8b1;
  height: 24px;
}

th:first-child,
td:first-child {
  border-left: 1px solid #d4d9de;
}

th:last-child,
td:last-child {
  border-right: 1px solid #d4d9de;
}

td.num {
  text-align: right;
  white-space: nowrap;
}

tbody tr {
  height: 34px;
}

tbody tr:last-child td {
  border-bottom: 1px dotted #9ea8b1;
}

tbody td:nth-child(3),
tbody td:nth-child(4) {
  text-overflow: ellipsis;
}

tbody td:nth-child(3) {
  white-space: normal;
  word-break: break-word;
  line-height: 1.05;
  vertical-align: middle;
  text-align: left;
}

tbody td:nth-child(4) {
  white-space: nowrap;
}

.totals-card {
  align-self: start;
  border: 1px solid #c8cfd5;
  padding: 10px 12px;
  background: white;
  min-height: 112px;
}

.totals-card div {
  padding: 6px 0;
  border-bottom: 1px solid #222;
}

.note-card {
  min-height: 112px;
  max-height: 112px;
  overflow: hidden;
}

.order-ref-card {
  min-height: 112px;
}

.order-ref-card p:last-child {
  margin-top: 6px;
  line-height: 1.45;
  white-space: pre-wrap;
}

.paper-footer-note {
  margin-top: 4px;
  font-size: 10px;
  line-height: 1.4;
  color: #5f5750;
  min-height: 10px;
}

.delivery .invoice-only,
.invoice .delivery-only {
  display: none;
}

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

  .document-canvas {
    overflow-x: auto;
  }
}

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

  body {
    background: white;
  }

  .control-panel,
  .preview-header {
    display: none;
  }

  .page-shell {
    display: block;
    padding: 0;
  }

  .preview-panel,
  .document-canvas,
  .paper {
    margin: 0;
    padding: 0;
    background: white;
    box-shadow: none;
    border: none;
  }

  .paper {
    width: 194mm;
    height: 281mm;
  }

  .paper.page-break {
    break-after: page;
    page-break-after: always;
  }
}
