:root {
  color-scheme: light;
  --bg: #efefef;
  --panel: #ffffff;
  --sheet: #ffffff;
  --line: #bcbcbc;
  --line-strong: #555555;
  --text: #111111;
  --muted: #555555;
  --accent: #222222;
  --accent-soft: #f4f4f4;
  --danger: #9d3d31;
  --radius: 12px;
  --shadow: 0 8px 18px rgba(0, 0, 0, 0.06);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  font-family: "TH Sarabun New", "Sarabun", sans-serif;
  background: var(--bg);
  color: var(--text);
}

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

body { padding: 16px; }

.app-shell {
  display: grid;
  grid-template-columns: minmax(320px, 430px) minmax(0, 1fr);
  gap: 20px;
  align-items: start;
}

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

.panel-header,
.card {
  background: var(--panel);
  border: 1px solid #d9d9d9;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.panel-header,
.card {
  padding: 20px;
}

.panel-header h1 {
  margin: 0;
  font-size: 1.45rem;
  line-height: 1.2;
}

.panel-header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.editor-panel {
  display: grid;
  gap: 16px;
  position: sticky;
  top: 20px;
}

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

.card h2 {
  margin: 0;
  font-size: 1.1rem;
}

.field-grid {
  display: grid;
  gap: 14px;
}

.field-grid.two-columns {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.field {
  display: grid;
  gap: 7px;
}

.field span {
  font-size: 0.95rem;
  font-weight: 600;
}

.field input,
.field textarea,
.field select {
  width: 100%;
  border: 1px solid #d5cab9;
  border-radius: 12px;
  background: #fff;
  padding: 11px 13px;
  color: var(--text);
}

.field input:focus,
.field textarea:focus,
.field select:focus {
  outline: 2px solid rgba(120, 90, 47, 0.16);
  border-color: var(--accent);
}

.items-editor {
  display: grid;
  gap: 12px;
}

.item-editor-row {
  border: 1px solid #e2d9cc;
  border-radius: 14px;
  padding: 14px;
  background: #fff;
}

.item-editor-grid {
  display: grid;
  grid-template-columns: minmax(0, 2.5fr) repeat(3, minmax(110px, 1fr)) auto auto;
  gap: 10px;
  align-items: start;
}

.item-editor-grid .field {
  min-width: 0;
}

.item-editor-grid textarea {
  width: 100% !important;
  max-width: 100% !important;
  min-width: 0 !important;
  height: auto !important;
  min-height: 96px;
  resize: vertical;
}

.row-total {
  min-width: 108px;
  padding: 11px 12px;
  border-radius: 12px;
  background: var(--accent-soft);
  border: 1px solid #e2d4be;
  font-weight: 700;
  text-align: right;
}

.remove-button,
.secondary-button,
.primary-button {
  border: 0;
  border-radius: 12px;
  padding: 11px 14px;
  cursor: pointer;
  transition: transform 0.15s ease, opacity 0.15s ease;
}

.remove-button:hover,
.secondary-button:hover,
.primary-button:hover {
  transform: translateY(-1px);
}

.remove-button {
  background: rgba(157, 61, 49, 0.12);
  color: var(--danger);
  white-space: nowrap;
}

.secondary-button {
  background: var(--accent-soft);
  color: var(--accent);
  border: 1px solid #e2d4be;
}

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

.actions-card {
  display: grid;
  gap: 16px;
}

.totals-strip {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.totals-strip div {
  background: #fff;
  border: 1px solid #d8d8d8;
  border-radius: 10px;
  padding: 12px 14px;
}

.totals-strip span {
  display: block;
  font-size: 0.9rem;
  color: var(--muted);
  margin-bottom: 4px;
}

.totals-strip strong {
  font-size: 1.05rem;
}

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

.preview-panel {
  display: grid;
  gap: 0;
}

.invoice-sheet {
  width: min(100%, 920px);
  margin: 0 auto;
  background: var(--sheet);
  border-radius: 0;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  padding: 26px 30px 20px;
  border: 1px solid #d2d2d2;
  font-size: 16pt;
}

.invoice-sheet.is-pdf-capturing {
  overflow: visible !important;
}

.invoice-company {
  text-align: center;
  margin-bottom: 8px;
}

.invoice-company h2 {
  margin: 0;
  font-size: 16pt;
  font-weight: 400;
}

.invoice-company p,
.summary-note,
.signature-block p {
  margin: 0;
  line-height: 1;
  font-size: 16pt;
}

.underlined-contact {
  display: inline-block;
  padding-bottom: 1px;
  border-bottom: 1px solid #000;
}

.invoice-title {
  margin-top: 10px;
  margin-bottom: 8px;
  font-size: 20pt;
  font-weight: 700;
  text-align: center;
}

.invoice-meta {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 6px;
  font-size: 16pt;
}

.recipient-line,
.intro-line {
  margin: 0 0 8px;
  font-size: 16pt;
  line-height: 1;
}

.invoice-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 12px;
  font-size: 16pt;
  table-layout: fixed;
}

.invoice-table th,
.invoice-table td {
  border: 1px solid var(--line-strong);
  padding: 0 5.8pt;
  vertical-align: top;
  line-height: 1;
  font-weight: 400;
  white-space: normal;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.invoice-table thead th {
  background: #ffffff;
  text-align: center;
  font-weight: 400;
}

.invoice-table tbody td:nth-child(1),
.invoice-table tbody td:nth-child(3),
.invoice-table tbody td:nth-child(4),
.invoice-table tbody td:nth-child(5),
.invoice-table tbody td:nth-child(6),
.invoice-table tbody td:nth-child(7),
.invoice-table tbody td:nth-child(8) {
  text-align: center;
}

.invoice-table tbody td:nth-child(2) {
  text-align: left;
}

.invoice-table tfoot td {
  font-weight: 400;
}

.summary-label {
  text-align: right !important;
  padding-right: 12px !important;
  background: #ffffff;
}

.summary-value {
  text-align: center !important;
  font-variant-numeric: tabular-nums;
}

.grand-total-row td { background: #ffffff; font-weight: 400; }

.spacer-row td {
  height: 12pt;
}

.amount-in-words {
  margin-top: 10px;
  line-height: 1;
  font-size: 16pt;
}

.table-gap {
  height: 16pt;
}

.signature-block {
  margin-top: 18px;
  display: flex;
  justify-content: flex-end;
}

.signature-box {
  width: min(100%, 360px);
  text-align: center;
}

.signature-line {
  margin-top: 28px;
}

.empty-state {
  padding: 20px;
  text-align: center;
  border: 1px dashed var(--line);
  border-radius: 16px;
  color: var(--muted);
}

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

  .editor-panel {
    position: static;
  }
}

@media (max-width: 820px) {
  body {
    padding: 14px;
  }

  .field-grid.two-columns,
  .totals-strip {
    grid-template-columns: 1fr;
  }

  .item-editor-grid {
    grid-template-columns: 1fr;
  }

  .row-total {
    text-align: left;
  }

  .invoice-sheet {
    padding: 20px 16px;
    overflow-x: auto;
  }

  .invoice-table {
    min-width: 760px;
  }
}

@media print {
  body {
    padding: 0;
    background: #fff;
  }

  .editor-panel {
    display: none;
  }

  .invoice-sheet {
    box-shadow: none;
    border: 0;
    width: 100%;
    max-width: none;
    border-radius: 0;
    padding: 0;
  }
}
