*,
*::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: 420px;
  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;
}

.field {
  margin-bottom: 0.85rem;
}

.field label {
  display: block;
  font-size: 0.86rem;
  font-weight: 700;
  margin-bottom: 0.35rem;
  color: #333;
  letter-spacing: 0;
  text-transform: none;
}

.field input,
.field textarea,
.order-items-table input,
.order-items-table textarea {
  width: 100%;
  padding: 0.8rem 0.85rem;
  border: 1.5px solid #c9c2b4;
  border-radius: 10px;
  background: #fff;
  color: #111;
  font: inherit;
  font-size: 16px; /* stops iPhone zoom on focus */
  line-height: 1.35;
  -webkit-appearance: none;
  appearance: none;
}

.field input:focus,
.field textarea:focus,
.order-items-table input:focus,
.order-items-table textarea:focus {
  outline: 2px solid #8b8356;
  border-color: #8b8356;
  background: #fff;
}

/* Grow with content — no inner scroll / clipped text */
.field textarea,
.order-items-table textarea {
  min-height: 0;
  height: auto;
  resize: none;
  overflow: hidden;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  field-sizing: content;
}

.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-cover {
  background: #f3efe4;
  border-color: #d5c9a4;
}

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

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

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

.section-materials {
  background: #f0f7f6;
  border-color: #b5d0cc;
}

.section-materials h2 { color: #2a5f58; }
.section-materials .section-num { background: #3a7a70; }
.section-materials .field textarea {
  border-color: #9cc4be;
  background: #fff;
}

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

.section-amounts h2 { color: #2f6b3a; }
.section-amounts .section-num { background: #2f6b3a; }
.section-amounts .field textarea,
.section-amounts .scope-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-cover: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-customer:focus-within {
  border-color: #1f6fb2;
  background: #e5f1fc;
}

.section-materials:focus-within {
  border-color: #3a7a70;
  background: #e4f2ef;
  box-shadow: 0 0 0 3px rgba(58, 122, 112, 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;
}

.btn-small {
  min-height: 44px;
  padding: 0.65rem 0.85rem;
  font-size: 0.95rem;
  margin-top: 0.35rem;
}

.checkbox-field {
  display: flex;
  align-items: flex-start;
  gap: 0.65rem;
  margin-bottom: 0.85rem;
  font-size: 1rem;
  line-height: 1.35;
  padding: 0.65rem 0.75rem;
  background: #fff;
  border: 1.5px solid #c9c2b4;
  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 input {
  width: 1.25rem;
  height: 1.25rem;
  margin-top: 0.1rem;
  flex-shrink: 0;
}

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

.actions-panel {
  position: static;
  box-shadow: none;
  background: transparent;
  backdrop-filter: none;
}

.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;
}

.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 0 1rem;
  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;
  background: #fff;
}

.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;
}

.scope-list {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

.scope-row {
  display: grid;
  grid-template-columns: 1.6rem 1fr auto;
  gap: 0.4rem;
  align-items: start;
}

.scope-num {
  font-size: 0.85rem;
  font-weight: 800;
  color: #8b8356;
  padding-top: 0.95rem;
  text-align: right;
}

.scope-row textarea {
  width: 100%;
  padding: 0.8rem 0.85rem;
  border: 1.5px solid #c9c2b4;
  border-radius: 10px;
  background: #fff;
  color: #111;
  font: inherit;
  font-size: 16px;
  line-height: 1.35;
  min-height: 0;
  height: auto;
  resize: none;
  overflow: hidden;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  field-sizing: content;
  -webkit-appearance: none;
  appearance: none;
}

.scope-row textarea:focus {
  outline: 2px solid #8b8356;
  border-color: #8b8356;
}

.scope-remove {
  border: none;
  background: #f3efe6;
  color: #555;
  border-radius: 10px;
  min-width: 44px;
  min-height: 48px;
  font-size: 1.15rem;
  font-weight: 700;
  cursor: pointer;
  padding: 0 0.65rem;
}

.scope-remove:active {
  opacity: 0.85;
}

.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: #3f3b28;
  font-weight: 800;
  font-size: 1rem;
  text-decoration: none;
  background: #fff;
  border: 1.5px solid #c9c2b4;
  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;
}

.panel:has(#backLink:not([hidden])) {
  padding-top: 3.6rem;
}

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

  .resizer {
    display: none;
  }

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

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

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

  .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;
  }

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

  /* 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;
  }
}
