@import url("https://fonts.googleapis.com/css2?family=Sarabun:wght@300;400;600;700&display=swap");

:root {
  --ink: #152238;
  --muted: #667085;
  --line: #dbe3ed;
  --paper: #ffffff;
  --canvas: #edf3f8;
  --blue: #0b72e7;
  --blue-dark: #0759b7;
  --blue-soft: #e9f3ff;
  --orange: #f59e0b;
  --shadow: 0 18px 50px rgba(27, 47, 74, 0.12);
  --preview-scale: 0.5;
}

* {
  box-sizing: border-box;
}

html {
  font-family: "Sarabun", Tahoma, "Noto Sans Thai", "Leelawadee UI", sans-serif;
  color: var(--ink);
  background: #f5f8fc;
}

body {
  margin: 0;
  min-width: 320px;
  background:
    radial-gradient(circle at 10% -10%, rgba(11, 114, 231, 0.12), transparent 31rem),
    #f5f8fc;
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 2rem;
  max-width: 1480px;
  margin: 0 auto;
  padding: 2rem clamp(1rem, 4vw, 3.5rem) 1.25rem;
}

.eyebrow {
  margin: 0 0 0.25rem;
  color: var(--blue);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.16em;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0.35rem;
  font-size: clamp(1.7rem, 3vw, 2.6rem);
  line-height: 1.15;
  letter-spacing: -0.04em;
}

h2 {
  margin-bottom: 0.15rem;
  font-size: 1.12rem;
}

.subtitle,
.section-heading p,
.details-help,
.preview-hint {
  color: var(--muted);
}

.subtitle {
  margin-bottom: 0;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.save-status {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  color: #17803d;
  font-size: 0.82rem;
  font-weight: 700;
}

.save-status::before {
  width: 0.48rem;
  height: 0.48rem;
  content: "";
  border-radius: 50%;
  background: #22c55e;
  box-shadow: 0 0 0 4px rgba(34, 197, 94, 0.12);
}

.button {
  min-height: 2.75rem;
  padding: 0.65rem 1rem;
  border: 1px solid transparent;
  border-radius: 0.7rem;
  font-weight: 800;
  transition: transform 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

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

.button-primary {
  color: #fff;
  background: linear-gradient(135deg, var(--blue), #0a62c8);
  box-shadow: 0 8px 18px rgba(11, 114, 231, 0.2);
}

.button-primary:hover {
  background: linear-gradient(135deg, #0d7bf5, var(--blue-dark));
}

.button-secondary {
  color: var(--ink);
  border-color: var(--line);
  background: rgba(255, 255, 255, 0.82);
}

.text-button {
  padding: 0.4rem 0.25rem;
  color: var(--blue-dark);
  border: 0;
  background: transparent;
  font-weight: 800;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(310px, 0.65fr);
  gap: 1.1rem;
  max-width: 1480px;
  margin: 0 auto;
  padding: 0 clamp(1rem, 4vw, 3.5rem) 1.1rem;
}

.form-card,
.settings-card,
.print-stage {
  border: 1px solid rgba(206, 218, 232, 0.84);
  border-radius: 1.1rem;
  background: rgba(255, 255, 255, 0.91);
  box-shadow: 0 10px 35px rgba(27, 47, 74, 0.06);
  backdrop-filter: blur(12px);
}

.form-card,
.settings-card {
  padding: clamp(1rem, 2.2vw, 1.5rem);
}

.section-heading {
  display: flex;
  gap: 0.8rem;
  align-items: flex-start;
  margin-bottom: 1.1rem;
}

.section-heading p {
  margin-bottom: 0;
  font-size: 0.85rem;
}

.step-badge {
  display: grid;
  flex: 0 0 auto;
  width: 2rem;
  height: 2rem;
  place-items: center;
  color: var(--blue-dark);
  border-radius: 0.65rem;
  background: var(--blue-soft);
  font-weight: 900;
}

.step-badge.warm {
  color: #9a5b00;
  background: #fff3d9;
}

#chequeForm {
  display: grid;
  gap: 0.9rem;
}

.field {
  display: grid;
  gap: 0.38rem;
  min-width: 0;
}

.field > span,
.field-row .field > span {
  font-size: 0.84rem;
  font-weight: 800;
}

.field b {
  color: #e11d48;
}

.field small,
.check-field,
.switch-row small {
  color: var(--muted);
  font-size: 0.75rem;
}

input[type="text"],
input[type="date"],
input[type="number"],
textarea {
  width: 100%;
  min-height: 2.8rem;
  padding: 0.68rem 0.8rem;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 0.68rem;
  outline: none;
  background: #fff;
  transition: border 140ms ease, box-shadow 140ms ease;
}

textarea {
  min-height: 4.25rem;
  resize: vertical;
}

input:focus,
textarea:focus {
  border-color: rgba(11, 114, 231, 0.7);
  box-shadow: 0 0 0 4px rgba(11, 114, 231, 0.1);
}

.field-row {
  display: grid;
  grid-template-columns: minmax(190px, 1fr) auto;
  gap: 1rem;
  align-items: end;
}

.date-options {
  display: grid;
  gap: 0.5rem;
  padding-bottom: 0.35rem;
}

.check-field {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-height: 2.8rem;
}

.check-field.compact {
  min-height: auto;
}

input[type="checkbox"] {
  width: 1.08rem;
  height: 1.08rem;
  accent-color: var(--blue);
}

input:disabled {
  color: #98a2b3;
  background: #f2f4f7;
  cursor: not-allowed;
}

.money-input,
.unit-input {
  display: flex;
  align-items: center;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 0.68rem;
  background: #fff;
}

.money-input > span {
  padding-left: 0.9rem;
  color: var(--blue-dark);
  font-size: 1.15rem;
  font-weight: 900;
}

.money-input input,
.unit-input input {
  border: 0;
  box-shadow: none;
}

.money-input input:focus,
.unit-input input:focus {
  box-shadow: none;
}

.unit-input > span {
  padding-right: 0.7rem;
  color: var(--muted);
  font-size: 0.75rem;
  white-space: nowrap;
}

.form-actions {
  display: flex;
  gap: 1rem;
  align-items: center;
  justify-content: space-between;
  margin-top: 0.15rem;
}

.live-note {
  color: var(--muted);
  font-size: 0.76rem;
}

.wide {
  min-width: 12rem;
}

.toggle-list {
  display: grid;
  gap: 0.1rem;
  margin-bottom: 1rem;
}

.switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.78rem 0;
  border-bottom: 1px solid #edf1f6;
}

.switch-row span {
  display: grid;
  gap: 0.12rem;
}

.switch-row strong {
  font-size: 0.84rem;
}

.calibration-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.6rem;
  padding: 0.9rem;
  border-radius: 0.8rem;
  background: #f7faff;
}

.calibration-grid input {
  min-width: 0;
  padding-right: 0.15rem;
}

.fine-tune {
  margin-top: 0.85rem;
  padding-top: 0.2rem;
}

.fine-tune summary {
  padding: 0.55rem 0;
  color: var(--blue-dark);
  font-size: 0.85rem;
  font-weight: 800;
  cursor: pointer;
}

.details-help {
  margin: 0 0 0.6rem;
  font-size: 0.74rem;
}

.position-table {
  display: grid;
  gap: 0.45rem;
}

.position-row {
  display: grid;
  grid-template-columns: minmax(100px, 1fr) 5rem 5rem;
  gap: 0.45rem;
  align-items: center;
}

.position-row > span {
  font-size: 0.76rem;
  font-weight: 700;
}

.position-row label {
  position: relative;
}

.position-row label::before {
  position: absolute;
  z-index: 1;
  top: 50%;
  left: 0.45rem;
  color: var(--muted);
  content: attr(data-axis);
  font-size: 0.64rem;
  transform: translateY(-50%);
}

.position-row input {
  min-height: 2.15rem;
  padding: 0.35rem 0.25rem 0.35rem 1.3rem;
  font-size: 0.75rem;
}

.print-note {
  margin-top: 1rem;
  padding: 0.8rem 0.9rem;
  color: #725000;
  border: 1px solid #f2dfa8;
  border-radius: 0.75rem;
  background: #fffaf0;
  font-size: 0.78rem;
}

.print-note p {
  margin: 0.25rem 0 0;
  line-height: 1.55;
}

.print-stage {
  max-width: 1480px;
  margin: 0 auto 2.5rem;
  padding: 1.25rem clamp(1rem, 2.2vw, 1.5rem);
}

.preview-heading {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}

.preview-heading h2 {
  margin-bottom: 0;
}

.legend {
  display: flex;
  gap: 1rem;
  color: var(--muted);
  font-size: 0.74rem;
}

.dot {
  display: inline-block;
  width: 0.55rem;
  height: 0.55rem;
  margin-right: 0.25rem;
  border-radius: 50%;
}

.template-dot {
  background: #8c9baa;
}

.overlay-dot {
  background: var(--blue);
}

.preview-viewport {
  position: relative;
  overflow: auto;
  min-height: 330px;
  max-height: 72vh;
  padding: 1rem;
  border: 1px solid #d9e2ec;
  border-radius: 0.9rem;
  background:
    linear-gradient(45deg, #e7edf4 25%, transparent 25%),
    linear-gradient(-45deg, #e7edf4 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #e7edf4 75%),
    linear-gradient(-45deg, transparent 75%, #e7edf4 75%),
    var(--canvas);
  background-position: 0 0, 0 8px, 8px -8px, -8px 0;
  background-size: 16px 16px;
}

.cheque-sheet-shell {
  position: relative;
  width: calc(570mm * var(--preview-scale));
  height: calc(210mm * var(--preview-scale));
}

.cheque-sheet {
  position: relative;
  overflow: hidden;
  width: 570mm;
  height: 210mm;
  color: #071d38;
  background: var(--paper);
  box-shadow: var(--shadow);
  transform: scale(var(--preview-scale));
  transform-origin: 0 0;
}

.cheque-template {
  position: absolute;
  z-index: 1;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: fill;
  opacity: 0.72;
  user-select: none;
  pointer-events: none;
}

.cheque-sheet.hide-template .cheque-template {
  opacity: 0;
}

.guide-grid {
  position: absolute;
  z-index: 2;
  inset: 0;
  display: none;
  background-image:
    linear-gradient(rgba(11, 114, 231, 0.18) 1px, transparent 1px),
    linear-gradient(90deg, rgba(11, 114, 231, 0.18) 1px, transparent 1px),
    linear-gradient(rgba(11, 114, 231, 0.3) 1px, transparent 1px),
    linear-gradient(90deg, rgba(11, 114, 231, 0.3) 1px, transparent 1px);
  background-size: 5mm 5mm, 5mm 5mm, 10mm 10mm, 10mm 10mm;
  pointer-events: none;
}

.cheque-sheet.show-guides .guide-grid {
  display: block;
}

.overlay-layer {
  position: absolute;
  z-index: 3;
  inset: 0;
  color: #0068d7;
  font-family: "Sarabun", Tahoma, sans-serif;
  font-weight: 300;
  transform: translate(calc(var(--offset-x, 0) * 1mm), calc(var(--offset-y, 0) * 1mm)) scale(var(--print-scale, 1));
  transform-origin: 0 0;
}

.print-field,
.date-boxes {
  position: absolute;
  left: calc(var(--x) * 1mm);
  top: calc(var(--y) * 1mm);
}

.print-field {
  overflow: visible;
  white-space: nowrap;
  line-height: 1.18;
}

.date-stub {
  width: 93mm;
  height: 7mm;
  font-size: 5mm;
}

.payee-stub {
  width: 94mm;
  height: 7.5mm;
  font-size: 5.3mm;
}

.amount-stub {
  width: 94mm;
  height: 7.5mm;
  font-size: 5.3mm;
}

.payee-main {
  width: 333mm;
  height: 9mm;
  font-size: 6.3mm;
}

.words-main {
  width: 212mm;
  height: 10mm;
  font-size: 6mm;
}

.number-main {
  width: 140mm;
  height: 11mm;
  font-size: 8mm;
  letter-spacing: 0.08em;
}

.memo-stub {
  width: 96mm;
  height: 18mm;
  overflow: hidden;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: 3.5mm;
  line-height: 1.35;
}

.date-boxes {
  display: grid;
  grid-template-columns: repeat(8, 14.95mm);
  width: 119.6mm;
  height: 13.5mm;
  align-items: center;
  font-family: "Sarabun", Tahoma, sans-serif;
  font-size: 6.4mm;
  font-weight: 300;
  text-align: center;
}

.date-boxes span {
  display: grid;
  height: 100%;
  place-items: center;
}

.preview-hint {
  margin: 0.75rem 0 0;
  font-size: 0.76rem;
}

.app-credit {
  display: flex;
  justify-content: center;
  gap: 0.45rem;
  flex-wrap: wrap;
  max-width: 1480px;
  margin: -1rem auto 2.5rem;
  padding: 0 1rem;
  color: var(--muted);
  font-size: 0.82rem;
  text-align: center;
}

.credit-separator {
  color: #b4bfcc;
}

.toast {
  position: fixed;
  z-index: 20;
  right: 1.25rem;
  bottom: 1.25rem;
  max-width: min(25rem, calc(100vw - 2rem));
  padding: 0.8rem 1rem;
  color: #fff;
  border-radius: 0.7rem;
  background: #152238;
  box-shadow: 0 12px 30px rgba(13, 29, 50, 0.22);
  opacity: 0;
  transform: translateY(0.8rem);
  pointer-events: none;
  transition: opacity 180ms ease, transform 180ms ease;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 940px) {
  .app-header {
    align-items: flex-start;
    flex-direction: column;
  }

  .header-actions {
    justify-content: flex-start;
  }

  .workspace {
    grid-template-columns: 1fr;
  }

  .calibration-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (max-width: 560px) {
  .header-actions {
    width: 100%;
  }

  .header-actions .button {
    flex: 1 1 9rem;
  }

  .save-status {
    width: 100%;
  }

  .field-row,
  .calibration-grid {
    grid-template-columns: 1fr;
  }

  .preview-heading {
    align-items: flex-start;
    flex-direction: column;
  }

  .legend {
    flex-wrap: wrap;
  }

  .position-row {
    grid-template-columns: 1fr 4.8rem 4.8rem;
  }
}

@page {
  size: 570mm 210mm;
  margin: 0;
}

@media print {
  html,
  body {
    width: 570mm;
    height: 210mm;
    margin: 0 !important;
    padding: 0 !important;
    background: #fff !important;
  }

  .app-header,
  .workspace,
  .preview-heading,
  .preview-hint,
  .app-credit,
  .toast {
    display: none !important;
  }

  .print-stage {
    width: 570mm;
    height: 210mm;
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: #fff;
    box-shadow: none;
  }

  .preview-viewport {
    overflow: visible;
    width: 570mm;
    height: 210mm;
    min-height: 0;
    max-height: none;
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: #fff;
  }

  .cheque-sheet-shell,
  .cheque-sheet {
    width: 570mm;
    height: 210mm;
  }

  .cheque-sheet {
    overflow: hidden;
    box-shadow: none;
    transform: none;
  }

  .cheque-template {
    opacity: 0 !important;
  }

  .cheque-sheet.include-template .cheque-template {
    opacity: 1 !important;
  }

  .guide-grid {
    display: none !important;
  }

  .overlay-layer {
    color: #000;
  }
}

