/* PI pages use the full width of the main pane; layout changes with viewport.
   The site shell caps main at 1680px and centers it; lift that only while a PI view is open. */
body.pi-open main,
main:has(#piView:not(.hidden)),
main:has(#piFormView:not(.hidden)),
main:has(#piSettingsView:not(.hidden)) {
  max-width: none;
  margin: 0;
  width: 100%;
}
#piView,
#piFormView,
#piSettingsView {
  width: 100%;
  max-width: none;
  min-width: 0;
}

#piView .workbench-header,
#piFormView .workbench-header,
#piSettingsView .workbench-header {
  display: flex;
  justify-content: space-between;
  gap: 16px 24px;
  width: 100%;
}
#piView .workbench-header > div:first-child,
#piFormView .workbench-header > div:first-child,
#piSettingsView .workbench-header > div:first-child {
  flex: 1 1 auto;
  min-width: 0;
}

.pi-header-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.pi-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px 16px;
}
.pi-toolbar .filters { flex: 0 1 auto; }
.pi-search-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  flex: 1 1 520px;
  min-width: 0;
}
.pi-search-row .search {
  min-width: 200px;
  width: auto;
  max-width: none;
  flex: 1 1 240px;
}
.pi-date, .pi-amount { width: 130px; max-width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: #fff; }

#piView .table-wrap { width: 100%; }
.pi-table { width: 100%; }
.pi-table td, .pi-table th { white-space: nowrap; }
.pi-table .pi-buyer { white-space: normal; min-width: 160px; width: 22%; }
.pi-badge { display: inline-block; border-radius: 20px; padding: 5px 10px; font-size: 12px; font-weight: 600; }
.pi-badge.draft { color: #a86100; background: #fff0e0; }
.pi-badge.issued { color: #1c4e99; background: #e8f0ff; }
.pi-badge.confirmed { color: #176b4a; background: #e5f7ee; }
.pi-badge.void { color: #bf3e3e; background: #ffeded; }
.pi-row-actions { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; }
.pi-row-actions button { border: 0; border-radius: 8px; padding: 6px 10px; font-size: 12px; font-weight: 600; cursor: pointer; color: #495267; background: #eef1f6; }
.pi-row-actions button.primary-lite { color: #fff; background: var(--blue); }

.pi-form { display: grid; gap: 16px; padding-bottom: 40px; width: 100%; min-width: 0; }
.pi-form-panel { padding: 8px 18px 18px; min-width: 0; }
.pi-form-panel .panel-heading { display: flex; justify-content: space-between; gap: 16px; align-items: flex-end; padding: 14px 0 10px; }
.pi-form-panel .panel-heading h2 { margin-bottom: 4px; font-size: 17px; }
.pi-form-panel .panel-heading p { margin: 0; color: var(--muted); font-size: 13px; }
.pi-customer-pick { min-width: 200px; max-width: 100%; font-size: 13px; }
.pi-item-rows { display: grid; gap: 12px; }
.pi-item-row {
  display: grid;
  grid-template-columns: minmax(0, 1.3fr) minmax(0, 1.6fr) minmax(72px, 0.55fr) minmax(72px, 0.5fr) minmax(90px, 0.7fr) minmax(90px, 0.7fr) minmax(100px, 0.75fr) 36px;
  gap: 8px;
  align-items: end;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fafbfd;
  min-width: 0;
}
.pi-item-row label { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.pi-item-row input, .pi-item-row textarea, .pi-form input, .pi-form select, .pi-form textarea {
  width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: #fff;
  box-sizing: border-box;
}
.pi-item-remove { height: 36px; border: 0; border-radius: 8px; background: #ffeded; color: #bf3e3e; cursor: pointer; font-size: 18px; }
.pi-freight-grid { margin-top: 14px; }
.pi-totals { margin-top: 12px; padding: 12px 14px; border-radius: 10px; background: #f3f7fd; font-size: 14px; }
.pi-totals strong { font-size: 16px; }
.pi-autofill-note { margin: 12px 0 0; color: var(--muted); font-size: 13px; }
/* Cancel .dialog-actions negative margins that overflow the main padding on mid widths. */
.pi-form-actions {
  position: sticky;
  bottom: 0;
  z-index: 4;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  margin: 12px 0 0;
  padding: 12px 0 8px;
  background: linear-gradient(to top, #f4f7fb 70%, #f4f7fb00);
  border-top: 0;
}

.pi-asset-preview { display: flex; gap: 28px; align-items: center; margin-top: 12px; flex-wrap: wrap; }
.pi-asset-preview span { display: block; margin-bottom: 6px; color: var(--muted); font-size: 12px; }
.pi-asset-preview img { max-height: 96px; background: #f4f4f4; border-radius: 8px; }
#piSettingsSuccess { color: #176b4a; min-height: 1em; }

/* Default to one column; widen by breakpoint without shrinking type. */
#piForm .form-grid,
#piSettingsForm .form-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px 18px;
}
#piForm .form-grid > .full,
#piSettingsForm .form-grid > .full,
.pi-marks-pair {
  grid-column: 1 / -1;
}
.pi-marks-pair {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px 18px;
  min-width: 0;
}

.pi-table .muted, .pi-buyer .muted { color: var(--muted); font-size: 12px; margin-top: 2px; }
.pi-mobile-list-hint { display: none; }
.pi-filter-more { display: none; }

.pi-detail-actions.hidden { display: none !important; }
.pi-danger-btn { color: #bf3e3e; background: #ffeded; }


.pi-inquiry-panel {
  background: #f6f9ff;
  border: 1px solid #d5e2f7;
  box-shadow: 0 8px 24px rgba(28, 78, 153, 0.06);
}
.pi-inquiry-kicker {
  margin: 0 0 4px;
  color: #1c4e99;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.pi-inquiry-panel h2 { font-size: 20px; }
.pi-inquiry-layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
  align-items: stretch;
  min-width: 0;
}
#piInquiryText {
  min-height: 180px;
  width: 100%;
  min-width: 0;
  border: 1px solid #c5d4ea;
  border-radius: 10px;
  padding: 14px 16px;
  font-size: 15px;
  line-height: 1.55;
  background: #fff;
  resize: vertical;
  box-sizing: border-box;
}
.pi-inquiry-side { min-width: 0; display: flex; flex-direction: column; gap: 10px; }
.pi-inquiry-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 14px;
  align-items: center;
  margin-top: 0;
}
.pi-parse-btn {
  font-size: 16px;
  padding: 10px 22px;
  min-height: 44px;
}
.pi-inquiry-file {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
  cursor: pointer;
  min-width: 0;
  max-width: 100%;
  line-height: 1.4;
}
.pi-inquiry-file input[type="file"] {
  width: auto;
  max-width: 100%;
  flex: 1 1 180px;
}
@media (min-width: 1440px) {
  .pi-inquiry-actions { flex-direction: column; align-items: stretch; }
  .pi-parse-btn { width: 100%; }
}
.pi-inquiry-note { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.55; }
.pi-inquiry-status {
  margin-top: 0;
  padding: 12px 14px;
  border-radius: 10px;
  background: #fff;
  border: 1px solid #d5e2f7;
  font-size: 13px;
  line-height: 1.55;
}
.pi-inquiry-status.is-error { border-color: #efc4c4; background: #fff7f7; color: #8a2b2b; }
.pi-inquiry-status strong { display: block; margin-bottom: 4px; }
.pi-inquiry-status ul { margin: 6px 0 0; padding-left: 18px; }
.pi-prefilled input,
.pi-prefilled textarea,
.pi-prefilled select,
.pi-item-row.pi-prefilled {
  background: #f3f8ff;
  border-color: #8fb0de;
}

/* Notebook and up: two-column fields, marks and freight on one row. */
@media (min-width: 901px) {
  #piForm .form-grid,
  #piSettingsForm .form-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .pi-marks-pair {
    grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr);
  }
  #piForm .pi-freight-grid {
    grid-template-columns: minmax(0, 1fr) 160px 140px;
  }
  #piForm .pi-freight-grid > .full {
    grid-column: auto;
  }
}

/* 1280–1439: keep the paste box full width so the side column does not crush controls. */
@media (min-width: 1280px) {
  #piInquiryText { min-height: 200px; }
  .pi-table .pi-buyer { width: 26%; }
}

/* 1440+: inquiry text and recognize/status sit side by side. */
@media (min-width: 1440px) {
  .pi-inquiry-layout {
    grid-template-columns: minmax(0, 1.75fr) minmax(300px, 0.85fr);
    align-items: stretch;
  }
  #piInquiryText { min-height: 220px; height: 100%; }
  .pi-inquiry-side { padding-top: 2px; }
}

/* 1440+ : three field columns, more room in goods rows. */
@media (min-width: 1440px) {
  #piForm .form-grid,
  #piSettingsForm .form-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
  #piForm .pi-freight-grid {
    grid-template-columns: minmax(0, 1fr) 180px 150px;
  }
  #piInquiryText { min-height: 240px; }
  .pi-item-row { gap: 10px; }
  .pi-form-panel { padding: 8px 22px 20px; }
}

/* 1920+ : four field columns; meta + buyer share a row; settings payment + defaults share a row. */
@media (min-width: 1920px) {
  #piForm.pi-form,
  #piSettingsForm.pi-form {
    grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.25fr);
    gap: 18px 22px;
    align-items: start;
  }
  #piInquiryPanel,
  .pi-panel-items,
  .pi-panel-terms,
  .pi-panel-company,
  #piFormError,
  #piSettingsError,
  #piSettingsSuccess,
  .pi-form-actions {
    grid-column: 1 / -1;
  }
  #piForm .form-grid,
  #piSettingsForm .form-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
  /* Meta has six compact fields; keep them in three columns inside its pane. */
  #piForm .pi-panel-meta .form-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
  #piForm .pi-freight-grid {
    grid-template-columns: minmax(0, 1fr) 200px 160px;
  }
  .pi-inquiry-layout {
    grid-template-columns: minmax(0, 1.9fr) minmax(320px, 0.8fr);
    gap: 20px;
  }
  #piInquiryText { min-height: 260px; }
  .pi-form-panel { padding: 10px 24px 22px; }
  .pi-table .pi-buyer { width: 28%; }
}

/* Ultrawide: keep stretching rather than capping to a centered column. */
@media (min-width: 2560px) {
  #piForm.pi-form,
  #piSettingsForm.pi-form {
    gap: 20px 28px;
  }
  .pi-inquiry-layout { gap: 28px; }
  #piInquiryText { min-height: 280px; }
  #piForm .form-grid,
  #piSettingsForm .form-grid,
  #piForm .pi-panel-meta .form-grid {
    gap: 18px 22px;
  }
  .pi-item-row { padding: 14px 16px; gap: 12px; }
}

/* Mid-narrow: wrap goods rows so numeric fields stay usable. */
@media (max-width: 1100px) {
  .pi-item-row {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
  .pi-item-row .pi-item-remove { grid-column: 4; }
}

/* Narrow window / sidebar stacked: one column, no horizontal overflow. */
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .pi-item-row { grid-template-columns: 1fr; }
  .pi-item-row .pi-item-remove { grid-column: auto; width: 44px; }
  #piView .workbench-header,
  #piFormView .workbench-header,
  #piSettingsView .workbench-header {
    flex-wrap: wrap;
    align-items: flex-start;
  }
  .pi-form-panel .panel-heading { flex-wrap: wrap; align-items: flex-start; }
  .pi-customer-pick { width: 100%; min-width: 0; }
  .pi-date, .pi-amount { width: 100%; }
  .pi-form-actions { display: flex; flex-wrap: wrap; gap: 8px; }
  .pi-form-actions button { flex: 1 1 auto; min-height: 44px; }
  .pi-table .pi-buyer { width: auto; }

  /* Mobile ledger: compact vertical list, not a wide sliding table. */
  .pi-mobile-list-hint {
    display: block;
    margin: 0;
    padding: 10px 16px 0;
    color: var(--muted);
    font-size: 13px;
    line-height: 1.45;
  }
  #piView .table-wrap,
  #piView .pi-ledger-wrap {
    overflow: visible;
  }
  #piView .table-wrap::after {
    display: none !important;
    content: none !important;
  }
  #piView .pi-table {
    display: block;
    width: 100%;
    min-width: 0 !important;
    border-collapse: separate;
    border-spacing: 0;
  }
  #piView .pi-table thead { display: none; }
  #piView .pi-table tbody {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 12px 16px 16px;
  }
  #piView .pi-table tr,
  #piView .pi-table tr.pi-ledger-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas:
      "no status"
      "buyer buyer"
      "date amount"
      "term ver";
    gap: 4px 12px;
    align-items: start;
    width: 100%;
    margin: 0;
    padding: 14px;
    border: 1px solid var(--line, #dfe4e2);
    border-radius: 12px;
    background: #fff;
    cursor: pointer;
    box-sizing: border-box;
  }
  #piView .pi-table td,
  #piView .pi-table .pi-buyer {
    display: block;
    width: auto;
    min-width: 0;
    max-width: 100%;
    padding: 0;
    border: 0;
    white-space: normal;
    overflow-wrap: anywhere;
    font-size: 15px;
    line-height: 1.4;
  }
  #piView .pi-table td:nth-child(1) { grid-area: no; font-weight: 700; }
  #piView .pi-table td:nth-child(2) { grid-area: buyer; }
  #piView .pi-table td:nth-child(3) {
    grid-area: date;
    white-space: nowrap;
    overflow: visible;
    color: var(--muted);
  }
  #piView .pi-table td:nth-child(4) {
    grid-area: amount;
    justify-self: end;
    white-space: nowrap;
    font-weight: 600;
  }
  #piView .pi-table td:nth-child(5) { grid-area: term; color: var(--muted); font-size: 13px; }
  #piView .pi-table td:nth-child(6) { grid-area: status; justify-self: end; }
  #piView .pi-table td:nth-child(7) { grid-area: ver; color: var(--muted); font-size: 13px; justify-self: end; }
  #piView .pi-table td:nth-child(8) { display: none; } /* operations happen after opening the record */

  #piView .pi-header-actions,
  #piFormView .pi-header-actions,
  #piSettingsView .pi-header-actions {
    width: 100%;
  }
  #piView .pi-header-actions button,
  #piFormView .pi-header-actions button,
  #piSettingsView .pi-header-actions button {
    flex: 1 1 auto;
    min-height: 44px;
  }
  #piView .stats {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    margin-bottom: 10px;
  }
  #piView .stats article,
  #piView .stats .primary-stat {
    min-height: 0;
    padding: 8px 8px 10px;
  }
  #piView .stats span { font-size: 12px; }
  #piView .stats strong,
  #piView .stats .primary-stat strong {
    margin-top: 2px;
    font-size: 22px;
  }
  #piView .pi-toolbar { flex-direction: column; align-items: stretch; }
  #piView .filters {
    width: 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  #piView .filters button { flex: 0 0 auto; min-height: 44px; padding: 8px 14px; }
  #piView .pi-search-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    align-items: stretch;
    flex-basis: auto;
    gap: 8px;
  }
  #piView .pi-filter-more {
    display: block;
    width: 100%;
    min-height: 44px;
  }
  #piView .pi-search-row:not(.is-open) .pi-filter-extra { display: none; }
  #piView .pi-search-row .search {
    grid-column: 1 / -1;
    width: 100%;
    max-width: none;
    min-height: 44px;
    box-sizing: border-box;
  }
  #piView #piSearchBtn {
    width: 100%;
    max-width: none;
    min-height: 44px;
    box-sizing: border-box;
  }
  #piView .pi-date,
  #piView .pi-amount {
    width: 100%;
    max-width: none;
    min-height: 44px;
    box-sizing: border-box;
  }

  #piForm input, #piForm select, #piForm textarea,
  #piSettingsForm input, #piSettingsForm select, #piSettingsForm textarea,
  .pi-item-row input, .pi-item-row textarea, .pi-item-row select,
  #piCustomerSelect, #piInquiryText {
    font-size: 16px;
    min-height: 44px;
    box-sizing: border-box;
  }
  #piInquiryText { min-height: 160px; }
  .pi-inquiry-actions { flex-direction: column; align-items: stretch; }
  .pi-parse-btn { width: 100%; min-height: 44px; }
  .pi-item-remove { width: 44px; height: 44px; min-height: 44px; }
  .pi-customer-pick { width: 100%; }
  .pi-form { padding-bottom: 88px; }
  .pi-form-actions {
    margin: 12px 0 0;
    padding: 10px 0 max(10px, env(safe-area-inset-bottom));
    background: #fff;
    border-top: 1px solid var(--line, #dfe4e2);
  }

  .pi-detail-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin: 0 0 16px;
  }
  .pi-detail-actions button {
    min-height: 44px;
    width: 100%;
  }
  .pi-detail-actions.hidden { display: none !important; }
  .pi-danger-btn { color: #bf3e3e; background: #ffeded; border-color: #f0c8c8; }
}

@media (min-width: 901px) {
  .pi-mobile-list-hint { display: none; }
.pi-filter-more { display: none; }

  .pi-detail-actions { display: none !important; }
}
