:root {
  --bg: oklch(96% 0.006 240);
  --panel: oklch(99% 0.004 240);
  --panel-2: oklch(93% 0.008 240);
  --text: oklch(21% 0.018 248);
  --muted: oklch(47% 0.02 248);
  --line: oklch(83% 0.012 248);
  --accent: oklch(48% 0.13 236);
  --accent-strong: oklch(39% 0.14 236);
  --accent-soft: oklch(94% 0.027 236);
  --danger: oklch(52% 0.16 28);
  --danger-soft: oklch(97% 0.02 28);
  --shadow: 0 20px 55px oklch(30% 0.02 248 / 0.14);
  color-scheme: light;
  font-family: "Google Sans", "Noto Sans Thai", Arial, sans-serif;
  font-optical-sizing: auto;
  font-variation-settings: "GRAD" 0;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: "Google Sans", "Noto Sans Thai", Arial, sans-serif;
  font-optical-sizing: auto;
  font-variation-settings: "GRAD" 0;
}

button,
input,
textarea {
  font: inherit;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 16px 24px;
  border-bottom: 1px solid var(--line);
  background: oklch(99% 0.004 240 / 0.96);
  backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
  z-index: 20;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 1.25rem;
  line-height: 1.2;
}

h2 {
  font-size: 1rem;
  line-height: 1.3;
}

.eyebrow {
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

.header-actions,
.image-actions,
.item-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.button {
  min-height: 44px;
  border: 1px solid transparent;
  border-radius: 7px;
  padding: 0 14px;
  cursor: pointer;
  font-weight: 650;
  box-shadow: 0 1px 0 oklch(100% 0.003 240 / 0.7) inset;
  transition: background 160ms ease, border-color 160ms ease, transform 160ms ease;
}

.button:hover {
  transform: translateY(-1px);
}

.button.primary {
  background: var(--accent);
  color: oklch(98% 0.006 240);
  box-shadow: 0 8px 18px oklch(48% 0.13 236 / 0.2);
}

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

.button.secondary {
  background: oklch(98% 0.006 240);
  border-color: oklch(78% 0.02 248);
  color: var(--text);
}

.button.secondary:hover {
  background: var(--panel-2);
}

.button.danger {
  background: var(--danger-soft);
  border-color: oklch(82% 0.05 28);
  color: var(--danger);
}

.button.compact {
  min-height: 44px;
  padding: 0 10px;
  font-size: 0.85rem;
}

.user-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: var(--surface);
  font-weight: 600;
}

.button:focus-visible,
.row-picker button:focus-visible,
.search-result:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--primary) 30%, transparent);
  outline-offset: 2px;
}

.swal2-popup,
.swal2-title,
.swal2-html-container,
.swal2-confirm,
.swal2-cancel {
  font-family: "Google Sans", "Noto Sans Thai", Arial, sans-serif !important;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

.button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
  transform: none;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(360px, 440px) minmax(0, 1fr);
  gap: 20px;
  padding: 20px;
  align-items: start;
}

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

.editor-panel {
  align-self: start;
  display: grid;
  gap: 14px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: linear-gradient(180deg, oklch(99% 0.004 240), oklch(97% 0.006 240));
  box-shadow: var(--shadow);
  position: sticky;
  top: 92px;
  max-height: calc(100vh - 112px);
  overflow: auto;
}

.section-head,
.preview-toolbar,
.item-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.search-panel {
  display: grid;
  gap: 10px;
  padding: 12px;
  border: 1px solid oklch(80% 0.018 248);
  border-radius: 8px;
  background: oklch(97% 0.009 236);
}

.search-head {
  align-items: start;
}

.search-results {
  display: grid;
  gap: 6px;
  max-height: 210px;
  overflow: auto;
}

.search-status {
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.5;
}

.search-result {
  display: grid;
  gap: 2px;
  width: 100%;
  border: 1px solid oklch(80% 0.018 248);
  border-radius: 7px;
  padding: 9px 10px;
  background: oklch(99% 0.004 240);
  color: var(--text);
  cursor: pointer;
  text-align: left;
}

.search-result:hover {
  border-color: var(--accent);
  background: oklch(96% 0.018 236);
}

.search-result strong {
  font-size: 0.9rem;
  line-height: 1.35;
}

.search-result span {
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.45;
}

.field {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 650;
}

.field input,
.field textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 10px 11px;
  background: oklch(100% 0.003 240);
  color: var(--text);
  font-size: 0.93rem;
  font-weight: 500;
}

.field textarea {
  resize: vertical;
  min-height: 76px;
}

.field input:focus,
.field textarea:focus {
  outline: 2px solid oklch(73% 0.1 236);
  outline-offset: 1px;
  border-color: var(--accent);
}

.vat-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 12px;
  border: 1px solid oklch(80% 0.018 248);
  border-radius: 8px;
  background: oklch(94% 0.012 236);
}

.vat-row strong,
.vat-row span {
  display: block;
}

.vat-row span {
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.82rem;
}

.switch input {
  position: absolute;
  opacity: 0;
  width: 48px;
  height: 28px;
  margin: 0;
  cursor: pointer;
  z-index: 1;
}

.switch {
  position: relative;
  display: inline-block;
  width: 48px;
  height: 28px;
}

.switch span {
  display: block;
  width: 48px;
  height: 28px;
  border-radius: 999px;
  background: oklch(76% 0.012 248);
  position: relative;
  cursor: pointer;
}

.switch span::after {
  content: "";
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--panel);
  position: absolute;
  left: 3px;
  top: 3px;
  transition: transform 180ms ease;
  box-shadow: 0 2px 6px oklch(30% 0.02 248 / 0.25);
}

.switch input:checked + span {
  background: var(--accent);
}

.switch input:checked + span::after {
  transform: translateX(20px);
}

.row-picker {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 7px;
}

.row-picker button {
  height: 34px;
  border: 1px solid oklch(75% 0.04 236);
  border-radius: 6px;
  background: var(--accent-soft);
  cursor: pointer;
  color: oklch(25% 0.035 236);
  font-weight: 700;
  transition: background 150ms ease, color 150ms ease, border-color 150ms ease, transform 150ms ease;
}

.row-picker button:hover {
  transform: translateY(-1px);
  border-color: var(--accent);
}

.row-picker button.has-data {
  color: var(--text);
  border-color: oklch(64% 0.08 236);
  background: oklch(90% 0.045 236);
}

.row-picker button.active {
  background: var(--accent);
  color: oklch(98% 0.006 240);
  border-color: var(--accent);
}

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

.field-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.wide {
  grid-column: 1 / -1;
}

.image-tools {
  display: grid;
  grid-template-columns: 160px 1fr;
  gap: 12px;
  align-items: start;
}

.image-preview {
  display: grid;
  place-items: center;
  width: 160px;
  aspect-ratio: 3 / 2;
  border: 1px dashed oklch(66% 0.04 236);
  border-radius: 7px;
  background: oklch(96% 0.015 236);
  overflow: hidden;
  color: var(--muted);
  font-size: 0.8rem;
  text-align: center;
}

.image-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.preview-panel {
  display: grid;
  gap: 12px;
}

.preview-toolbar {
  padding: 0 4px;
}

.saved-state {
  color: var(--muted);
  font-size: 0.82rem;
}

.document-preview {
  overflow: auto;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: linear-gradient(180deg, oklch(90% 0.012 248), oklch(84% 0.012 248));
  box-shadow: inset 0 1px 0 oklch(100% 0.003 240 / 0.6);
}

.page {
  width: 297mm;
  min-height: 210mm;
  margin: 0 auto 18px;
  padding: 6mm;
  background: oklch(100% 0.002 240);
  box-shadow: 0 12px 30px oklch(30% 0.02 248 / 0.18);
  color: oklch(18% 0.01 248);
  font-family: "Google Sans", sans-serif;
  font-optical-sizing: auto;
  font-variation-settings: "GRAD" 0;
}

.document-header {
  display: grid;
  grid-template-columns: minmax(0, 1fr) repeat(3, 33mm);
  gap: 2mm;
  margin-bottom: 3mm;
}

.project-box,
.sign-box {
  border: 1px solid oklch(12% 0.01 248);
  height: 28mm;
}

.project-box {
  display: grid;
  place-items: center;
  text-align: center;
  line-height: 1.35;
}

.project-title {
  display: block;
  font-size: 17px;
  font-weight: 700;
}

.project-meta {
  display: block;
  margin-top: 5px;
  font-size: 12px;
  font-weight: 650;
}

.sign-box {
  display: grid;
  grid-template-rows: 1fr auto auto;
  padding: 5mm 2mm 4mm;
  text-align: center;
}

.page-count-box {
  display: grid;
  place-items: center;
  grid-template-rows: 1fr;
  padding: 0 2mm;
}

.sign-label {
  font-size: 11px;
  font-weight: 700;
}

.sign-line {
  border-bottom: 1px dotted oklch(25% 0.01 248);
  min-height: 1px;
}

.sign-name {
  min-height: 13px;
  padding-top: 2px;
  font-size: 10px;
  font-weight: 650;
}

.form-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  font-size: 9.2px;
  line-height: 1.38;
}

.form-table th,
.form-table td {
  border: 1px solid oklch(12% 0.01 248);
  padding: 4px 5px;
  vertical-align: middle;
  text-align: center;
  overflow-wrap: break-word;
}

.form-table th {
  height: 9mm;
  padding: 4px 5px;
  font-size: 8.4px;
  font-weight: 700;
  line-height: 1.42;
  overflow-wrap: normal;
  word-break: normal;
  white-space: normal;
}

.nowrap {
  white-space: nowrap;
}

.form-table td.text-cell {
  text-align: center;
  vertical-align: middle;
}

.form-table .item-row td {
  height: 13mm;
  font-weight: 500;
}

.form-table .note-cell {
  text-align: center;
  font-weight: 650;
}

.product-cell {
  padding: 2px;
}

.product-cell img {
  display: block;
  width: 100%;
  height: 12mm;
  object-fit: cover;
}

.total-label {
  font-weight: 700;
}

.form-table td:nth-child(11),
.form-table td:nth-child(12) {
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 24px;
  background: oklch(16% 0.02 248 / 0.72);
}

.modal[hidden] {
  display: none;
}

.modal-surface {
  width: min(980px, 100%);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 28px 80px oklch(8% 0.02 248 / 0.42);
  padding: 16px;
}

.dialog-modal {
  align-items: center;
  justify-items: center;
  background: oklch(18% 0.018 248 / 0.55);
  backdrop-filter: blur(8px);
}

.dialog-surface {
  width: min(430px, 100%);
  border: 1px solid oklch(88% 0.012 248);
  border-radius: 12px;
  background: oklch(99% 0.004 240);
  box-shadow: 0 24px 70px oklch(18% 0.02 248 / 0.28);
  padding: 20px;
  margin: auto;
  display: grid;
  grid-template-columns: 42px 1fr;
  gap: 14px;
  animation: dialogIn 160ms ease-out;
}

.dialog-icon {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: oklch(94% 0.04 236);
  color: var(--accent);
  font-weight: 800;
}

.dialog-copy {
  min-width: 0;
}

.dialog-copy h2 {
  font-size: 1.08rem;
  line-height: 1.35;
}

.dialog-copy p:last-child {
  margin-top: 8px;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.6;
}

.dialog-actions {
  grid-column: 1 / -1;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 8px;
}

.dialog-surface.danger .dialog-icon {
  background: var(--danger-soft);
  color: var(--danger);
}

.dialog-surface.danger #dialogConfirmBtn {
  background: var(--danger);
  color: oklch(98% 0.006 240);
  box-shadow: 0 8px 18px oklch(52% 0.16 28 / 0.18);
}

.dialog-surface.info #dialogCancelBtn {
  display: none;
}

@keyframes dialogIn {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.98);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}

.crop-stage {
  position: relative;
  display: grid;
  place-items: center;
  overflow: hidden;
  border-radius: 8px;
  background: oklch(18% 0.012 248);
}

#cropCanvas {
  width: 100%;
  height: min(58vh, 560px);
  display: block;
  cursor: grab;
}

.crop-frame {
  position: absolute;
  width: min(72%, 720px);
  aspect-ratio: 3 / 2;
  border: 2px solid oklch(98% 0.006 240);
  box-shadow: 0 0 0 999px oklch(10% 0.01 248 / 0.45);
  pointer-events: none;
}

.crop-controls {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-top: 14px;
}

.crop-controls label {
  display: grid;
  gap: 4px;
  min-width: 220px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
}

.crop-controls input {
  width: 100%;
}

.crop-controls p {
  flex: 1;
  color: var(--muted);
  font-size: 0.88rem;
}

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

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

@media (max-width: 640px) {
  .app-header,
  .workspace {
    padding: 14px;
  }

  .app-header,
  .section-head,
  .preview-toolbar,
  .item-head,
  .crop-controls {
    align-items: stretch;
    flex-direction: column;
  }

  .field-grid,
  .image-tools {
    grid-template-columns: 1fr;
  }

  .image-preview {
    width: 100%;
  }
}

@media print {
  body {
    background: oklch(100% 0 0);
  }

  .app-header,
  .editor-panel,
  .preview-toolbar,
  .modal {
    display: none !important;
  }

  .workspace,
  .preview-panel,
  .document-preview {
    display: block;
    padding: 0;
    border: 0;
    background: transparent;
    box-shadow: none;
  }

  .page {
    width: 297mm;
    min-height: 210mm;
    margin: 0;
    padding: 6mm 5mm;
    box-shadow: none;
    break-inside: avoid;
    page-break-inside: avoid;
    break-after: page;
    page-break-after: always;
  }

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

  .form-table,
  .form-table thead,
  .form-table tbody,
  .form-table tr {
    break-inside: avoid;
    page-break-inside: avoid;
  }

  @page {
    size: A4 landscape;
    margin: 0;
  }
}
