*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: #efebe3;
  color: #1a1a1a;
  line-height: 1.4;
}

.app {
  --panel-width: 560px;
  display: grid;
  grid-template-columns: var(--panel-width) 8px 1fr;
  min-height: 100vh;
  min-height: 100dvh;
}

.panel {
  background: #f7f4ee;
  border-right: none;
  padding: 1.15rem 1.25rem 1.5rem;
  overflow-y: auto;
  position: sticky;
  top: 0;
  height: 100vh;
  height: 100dvh;
  min-width: 0;
}

.resizer {
  grid-column: 2;
  cursor: col-resize;
  touch-action: none;
  background: #d9d3c6;
  position: relative;
  z-index: 40;
}

.resizer::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: -4px;
  right: -4px;
}

.resizer:hover,
.resizer:focus-visible,
body.is-resizing .resizer {
  background: #8b8356;
}

body.is-resizing {
  cursor: col-resize;
  user-select: none;
}

body.is-resizing iframe,
body.is-resizing img {
  pointer-events: none;
}

.panel h1 {
  margin: 0;
  font-size: 1.35rem;
  color: #1a1a1a;
  line-height: 1.25;
}

.panel .subtitle {
  margin: 0.4rem 0 0.75rem;
  font-size: 0.95rem;
  line-height: 1.45;
  color: #555;
}

.easy-tip {
  background: #ebe4d4;
  border: 1px solid #d4c9b0;
  border-radius: 12px;
  padding: 0.75rem 0.9rem;
  margin: 0 0 1rem;
  font-size: 0.92rem;
  color: #3a3a3a;
  line-height: 1.4;
}

.easy-tip strong {
  color: #6b643f;
}

.field {
  margin-bottom: 0.75rem;
  padding: 0.35rem 0.4rem;
  border-radius: 10px;
  transition: background 0.12s ease, box-shadow 0.12s ease;
}

.field label {
  display: block;
  font-size: 0.82rem;
  font-weight: 800;
  margin-bottom: 0.3rem;
  color: #3a3a3a;
  letter-spacing: 0;
  text-transform: none;
}

.field input,
.field textarea,
.order-items-table input {
  width: 100%;
  padding: 0.9rem 0.95rem;
  border: 2px solid #c4bba8;
  border-radius: 10px;
  background: #fffef9;
  color: #111;
  font: inherit;
  font-size: 16px; /* stops iPhone zoom on focus */
  line-height: 1.4;
  -webkit-appearance: none;
  appearance: none;
  transition: border-color 0.12s ease, background 0.12s ease, box-shadow 0.12s ease;
}

.field input:hover,
.field textarea:hover {
  border-color: #9e946f;
  background: #fff;
}

.field input:focus,
.field textarea:focus,
.order-items-table input:focus {
  outline: none;
  border-color: #1f6fb2;
  background: #eef6ff;
  box-shadow: 0 0 0 4px rgba(31, 111, 178, 0.22);
  color: #06243d;
  font-weight: 600;
}

.field:focus-within {
  background: rgba(31, 111, 178, 0.08);
}

.field:focus-within label {
  color: #1f6fb2;
}

.field input.is-auto,
.field input[readonly] {
  background: #f3efe6;
  color: #333;
  font-weight: 700;
}

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

.field-group {
  margin-top: 0.85rem;
  padding-top: 0.85rem;
  border-top: 1px solid #ddd5c5;
}

.section-card {
  border: 2px solid transparent;
  border-radius: 16px;
  padding: 0.95rem 0.95rem 0.7rem;
  margin-top: 0.9rem;
  border-top: 2px solid transparent;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.04);
}

.section-card:first-of-type {
  margin-top: 0.35rem;
}

.section-card h2 {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  margin: 0 0 0.8rem;
  font-size: 1.02rem;
  font-weight: 800;
  text-transform: none;
  letter-spacing: 0;
}

.section-num {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.55rem;
  height: 1.55rem;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 800;
  color: #fff;
  flex-shrink: 0;
}

.section-customer {
  background: #eef6ff;
  border-color: #b7d4f0;
}

.section-customer h2 { color: #1a568c; }
.section-customer .section-num { background: #1f6fb2; }
.section-customer .field input {
  border-color: #9fc0e0;
  background: #fff;
}

.section-claim {
  background: #f3efe4;
  border-color: #d5c9a4;
}

.section-claim h2 { color: #6b5a2a; }
.section-claim .section-num { background: #8b8356; }
.section-claim .field input {
  border-color: #c4b68a;
  background: #fff;
}

.section-amounts {
  background: #eef8ef;
  border-color: #b5d6b8;
}

.section-amounts h2 { color: #2f6b3a; }
.section-amounts .section-num { background: #2f6b3a; }
.section-amounts .field input {
  border-color: #9fc9a4;
  background: #fff;
}

.section-card:focus-within {
  box-shadow: 0 0 0 3px rgba(31, 111, 178, 0.16), 0 4px 16px rgba(0, 0, 0, 0.06);
}

.section-customer:focus-within {
  border-color: #1f6fb2;
  background: #e5f1fc;
}

.section-claim:focus-within {
  border-color: #8b8356;
  background: #ebe4d0;
  box-shadow: 0 0 0 3px rgba(139, 131, 86, 0.2), 0 4px 16px rgba(0, 0, 0, 0.06);
}

.section-amounts:focus-within {
  border-color: #2f6b3a;
  background: #e3f3e5;
  box-shadow: 0 0 0 3px rgba(47, 107, 58, 0.2), 0 4px 16px rgba(0, 0, 0, 0.06);
}

.field-group h2 {
  margin: 0 0 0.65rem;
  font-size: 0.9rem;
  font-weight: 800;
  color: #6b643f;
  text-transform: none;
  letter-spacing: 0;
}

.checkbox-field {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 0.55rem;
  font-size: 0.95rem;
  font-weight: 650;
  line-height: 1.3;
  padding: 0.7rem 0.8rem;
  background: #fff;
  border: 2px solid #b5d6b8;
  border-radius: 10px;
}

.checkbox-field.flash-target,
.field.flash-target {
  box-shadow: 0 0 0 3px rgba(31, 111, 178, 0.35);
  border-color: rgba(31, 111, 178, 0.65);
}

.checkbox-field:focus-within,
.checkbox-field:has(input:checked) {
  border-color: #2f6b3a;
  background: #f4fff5;
}

.checkbox-field input {
  width: 1.2rem;
  height: 1.2rem;
  margin: 0;
  flex-shrink: 0;
  accent-color: #2f6b3a;
}

.money-row {
  display: grid;
  grid-template-columns: minmax(7.5rem, 0.9fr) 1.3fr;
  gap: 0.55rem;
  align-items: end;
  margin-bottom: 0.15rem;
}

.money-row .field {
  margin-bottom: 0.55rem;
}

.money-row.total-row .money-amount input {
  border-color: #8b8356;
}

.estimate-lines {
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  margin: 0.35rem 0 0.75rem;
}

.estimate-line {
  display: grid;
  grid-template-columns: auto minmax(8rem, 0.9fr) 1.4fr auto;
  gap: 0.45rem;
  align-items: end;
  padding: 0.75rem;
  background: #fff;
  border: 2px solid #9fc9a4;
  border-radius: 12px;
  transition: border-color 0.12s ease, box-shadow 0.12s ease, background 0.12s ease;
}

.estimate-line:focus-within {
  border-color: #1f6fb2;
  background: #eef6ff;
  box-shadow: 0 0 0 4px rgba(31, 111, 178, 0.18);
}

.line-index {
  width: 1.55rem;
  height: 2.55rem;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: 0.65rem;
  font-weight: 800;
  font-size: 0.85rem;
  color: #2f6b3a;
}

.estimate-line:focus-within .line-index {
  color: #1f6fb2;
}

.estimate-line .field {
  margin-bottom: 0;
  padding: 0;
}

.estimate-line .field:focus-within {
  background: transparent;
}

.estimate-line select {
  width: 100%;
  padding: 0.85rem 0.7rem;
  border: 2px solid #9fc9a4;
  border-radius: 10px;
  background: #fff;
  color: #111;
  font: inherit;
  font-size: 16px;
  line-height: 1.4;
  -webkit-appearance: none;
  appearance: none;
}

.estimate-line select:focus {
  outline: none;
  border-color: #1f6fb2;
  background: #eef6ff;
  box-shadow: 0 0 0 4px rgba(31, 111, 178, 0.22);
}

.btn-icon {
  width: 2.5rem;
  height: 2.5rem;
  margin-bottom: 0.1rem;
  border: 2px solid #c9c2b4;
  border-radius: 10px;
  background: #f7f4ee;
  color: #6b1f1f;
  font-size: 1.35rem;
  line-height: 1;
  cursor: pointer;
}

.btn-icon:hover {
  background: #f3e4e4;
  border-color: #b42318;
}

.btn-small {
  min-height: 44px;
  padding: 0.65rem 0.85rem;
  font-size: 0.98rem;
  margin-bottom: 0.65rem;
}

.total-due {
  margin: 0.25rem 0 0.35rem;
  padding: 0.85rem 0.95rem;
  border-radius: 10px;
  background: #245c31;
  border: 2px solid #1d4a28;
  font-weight: 800;
  color: #fff;
  font-size: 1.05rem;
  letter-spacing: 0.01em;
}

.actions {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  margin-top: 1.25rem;
  padding-top: 0.85rem;
  border-top: 1px solid #ddd5c5;
}

/* End of form — scroll to reach these on desktop */
.actions-panel {
  position: static;
  box-shadow: none;
  background: transparent;
  backdrop-filter: none;
}

/* Desktop: also float bottom-right on the preview (like zoom top-left) */
.actions-float {
  display: none;
}

@media (min-width: 961px) {
  .actions-float {
    display: flex;
    flex-direction: column;
    gap: 0.45rem;
    position: absolute;
    right: 0.7rem;
    bottom: 0.7rem;
    z-index: 30;
    margin: 0;
    padding: 0.45rem;
    border-top: none;
    border-radius: 12px;
    background: rgba(247, 244, 238, 0.96);
    border: 1.5px solid #d4c9b0;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.12);
    backdrop-filter: blur(6px);
    min-width: 11.5rem;
  }

  .actions-float .btn {
    min-height: 44px;
    padding: 0.7rem 0.85rem;
    font-size: 0.95rem;
  }
}

.btn {
  border: none;
  border-radius: 12px;
  padding: 0.95rem 1rem;
  font: inherit;
  font-size: 1.05rem;
  font-weight: 700;
  cursor: pointer;
  min-height: 52px;
}

.btn-primary {
  background: #8b8356;
  color: #fff;
}

.btn-primary:hover {
  background: #7a7349;
}

.btn-primary:disabled {
  opacity: 0.6;
  cursor: wait;
}

.btn-message {
  background: #0a7ea4;
  color: #fff;
}

.btn-message:hover {
  background: #086987;
}

.btn-message:disabled {
  opacity: 0.6;
  cursor: wait;
}

.btn-secondary {
  background: #fff;
  color: #222;
  border: 1.5px solid #c9c2b4;
}

.btn[data-action="reset"] {
  background: #c62828;
  color: #fff;
  border: 1.5px solid #9b1c1c;
}

.btn[data-action="reset"]:hover {
  background: #9b1c1c;
  border-color: #7f1616;
  color: #fff;
}

.status {
  margin-top: 0.5rem;
  margin-bottom: 0.25rem;
  font-size: 0.92rem;
  color: #2f6b3a;
  min-height: 1.2rem;
  font-weight: 600;
}

.status.error {
  color: #b42318;
}

.preview-wrap {
  position: relative;
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100vh;
  height: 100dvh;
  padding: 0;
  overflow: hidden;
  background: #e6e1d6;
}

.zoom-controls {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.zoom-floating {
  position: absolute;
  top: 0.7rem;
  left: 0.7rem;
  z-index: 30;
  padding: 0.4rem;
  border-radius: 12px;
  background: rgba(247, 244, 238, 0.96);
  border: 1.5px solid #d4c9b0;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.12);
  backdrop-filter: blur(6px);
}

.zoom-btn {
  min-width: 2.35rem;
  height: 2.35rem;
  border: 1.5px solid #c9c2b4;
  border-radius: 10px;
  background: #fff;
  color: #1a1a1a;
  font: inherit;
  font-size: 1.15rem;
  font-weight: 800;
  line-height: 1;
  cursor: pointer;
  padding: 0 0.55rem;
}

.zoom-btn:hover {
  border-color: #8b8356;
  background: #faf8f3;
}

.zoom-btn:active {
  transform: scale(0.97);
}

.zoom-btn.zoom-fit {
  min-width: 3.2rem;
  font-size: 0.88rem;
  font-weight: 800;
}

.zoom-label {
  min-width: 3.1rem;
  text-align: center;
  font-size: 0.85rem;
  font-weight: 800;
  color: #555;
  padding: 0 0.2rem;
}

.preview-viewport {
  flex: 1;
  min-height: 0;
  overflow: auto;
  display: flex;
  /* flex-start + #pages margin:auto: centers when fit, still scrollable when zoomed */
  align-items: flex-start;
  justify-content: flex-start;
  padding: 0.75rem;
}

#pages {
  /* Centers when smaller than the viewport; when overflowed, margins go to 0
     so scrollTop/scrollLeft can reach the real top/left (unlike align-items:center). */
  margin: auto;
  width: max-content;
}

.preview-note {
  display: none;
}

.page-wrap {
  max-width: none;
  width: max-content;
  margin: 0;
  position: relative;
}

.page-stage {
  position: relative;
  display: inline-block;
  line-height: 0;
}

.region-outlines {
  position: absolute;
  inset: 0;
  z-index: 4;
  pointer-events: none;
}

.region-outline {
  position: absolute;
  box-sizing: border-box;
  border: 1.5px solid rgba(31, 111, 178, 0.55);
  border-radius: 3px;
  background: transparent;
}

.hover-highlight {
  position: absolute;
  pointer-events: none;
  z-index: 5;
  border-radius: 3px;
  background: rgba(31, 111, 178, 0.2);
  outline: 2px solid rgba(31, 111, 178, 0.75);
  box-sizing: border-box;
  opacity: 0;
  transition: opacity 0.08s ease;
}

.hover-highlight.visible {
  opacity: 1;
}

.preview-clickable {
  cursor: crosshair;
}

.preview-clickable.over-field {
  cursor: pointer;
}

.page-wrap.updating img {
  opacity: 0.55;
}

.page-wrap img {
  display: block;
  width: auto;
  max-width: none;
  height: auto;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
  transition: opacity 0.2s ease, outline-color 0.15s ease;
  background: #fff;
  outline: 3px solid transparent;
}

body.has-field-focus .page-wrap img {
  outline-color: rgba(31, 111, 178, 0.35);
}

.page-label {
  position: absolute;
  top: 0.5rem;
  left: 0.5rem;
  background: rgba(26, 26, 26, 0.75);
  color: #fff;
  font-size: 0.78rem;
  padding: 0.25rem 0.5rem;
  border-radius: 6px;
  z-index: 2;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.65rem;
}

.order-items-scroll {
  overflow-x: visible;
  margin-top: 0.35rem;
}

.order-items-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0 0.65rem;
  font-size: 0.95rem;
  min-width: 0;
}

.order-items-table th {
  text-align: left;
  font-size: 0.78rem;
  font-weight: 700;
  color: #6b643f;
  padding: 0.25rem 0.35rem;
  border-bottom: none;
  text-transform: none;
  letter-spacing: 0;
}

.order-items-table td {
  padding: 0.2rem 0.25rem;
  vertical-align: top;
}

.order-items-table .col-qty,
.order-items-table .col-uom {
  width: 4.2rem;
}

.order-items-table .col-sku {
  width: 6rem;
}

.order-items-table .col-price,
.order-items-table .col-amount {
  width: 5.5rem;
}

.order-row-label {
  font-size: 0.85rem;
  font-weight: 700;
  color: #6b643f;
  padding-top: 0.5rem !important;
}

.field-hint {
  margin: -0.2rem 0 0.65rem;
  font-size: 0.88rem;
  color: #666;
  line-height: 1.4;
}

.back-link {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  position: fixed;
  top: max(0.65rem, env(safe-area-inset-top, 0px));
  left: max(0.65rem, env(safe-area-inset-left, 0px));
  z-index: 200;
  margin: 0;
  padding: 0.55rem 0.85rem;
  color: #fff;
  font-weight: 800;
  font-size: 1rem;
  text-decoration: none;
  background: #c62828;
  border: 1.5px solid #9b1c1c;
  border-radius: 999px;
  line-height: 1.2;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.12);
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
}

.back-link:hover,
.back-link:focus-visible {
  background: #c62828;
  border-color: #9b1c1c;
  color: #fff;
  box-shadow: 0 6px 16px rgba(198, 40, 40, 0.35);
  outline: none;
}

.back-link:active {
  opacity: 0.9;
  background: #9b1c1c;
  color: #fff;
}

/* Keep form content clear of the fixed Back button */
.panel:has(#backLink:not([hidden])) {
  padding-top: 3.6rem;
}

@media (min-width: 1400px) {
  .app {
    --panel-width: 640px;
  }
}

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

  .resizer {
    display: none;
  }

  .panel {
    position: static;
    height: auto;
    border-right: none;
    padding: 1rem 1rem 1.5rem;
  }

  .back-link {
    margin-left: 0;
  }

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

  .money-row {
    grid-template-columns: 1fr;
    gap: 0;
  }

  .estimate-line {
    grid-template-columns: auto 1fr auto;
  }

  .estimate-line .note-field {
    grid-column: 1 / -1;
  }

  .estimate-line .btn-icon {
    grid-column: 3;
    grid-row: 1;
  }

  .estimate-line .line-index {
    grid-column: 1;
    grid-row: 1;
  }

  .estimate-line .amount-field {
    grid-column: 2;
    grid-row: 1;
  }

  .actions {
    border-top: none;
    margin-top: 0.75rem;
    padding-top: 0;
  }

  .actions-panel {
    position: sticky;
    bottom: 0;
    z-index: 20;
    padding: 0.75rem 0 calc(0.55rem + env(safe-area-inset-bottom));
    background: rgba(247, 244, 238, 0.97);
    border-top: 1px solid #d9d3c6;
    box-shadow: 0 -8px 20px rgba(0, 0, 0, 0.06);
    backdrop-filter: blur(8px);
  }

  .actions-float {
    display: none !important;
  }

  /* Stack line-item tables into readable cards */
  .order-items-table thead {
    display: none;
  }

  .order-items-table,
  .order-items-table tbody {
    display: block;
    width: 100%;
  }

  .order-items-table tr {
    display: block;
    width: 100%;
    background: #fff;
    border: 1.5px solid #c9c2b4;
    border-radius: 12px;
    padding: 0.75rem;
    margin-bottom: 0.75rem;
  }

  .order-items-table tr:has(.order-row-label) {
    background: transparent;
    border: none;
    padding: 0.15rem 0.1rem 0;
    margin-bottom: 0;
  }

  .order-items-table td {
    display: block;
    width: 100%;
    padding: 0.35rem 0;
  }

  .order-items-table td[data-label]::before {
    content: attr(data-label);
    display: block;
    font-size: 0.82rem;
    font-weight: 700;
    color: #555;
    margin-bottom: 0.25rem;
  }

  .order-items-table .col-qty,
  .order-items-table .col-uom,
  .order-items-table .col-sku,
  .order-items-table .col-price,
  .order-items-table .col-amount,
  .order-items-table .col-desc {
    width: 100%;
  }

  .preview-wrap {
    height: min(70vh, 640px);
    padding: 0;
    border-top: 1px solid #d9d3c6;
  }

  .preview-note {
    display: none;
  }
}
