:root {
  color-scheme: light;
  --bg: #f4f6f8;
  --panel: #ffffff;
  --text: #17202a;
  --muted: #667085;
  --line: #d9dee7;
  --line-strong: #aeb8c5;
  --accent: #0f766e;
  --accent-strong: #115e59;
  --soft: #eef3f7;
  --code: #111827;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Arial, Helvetica, sans-serif;
}

.app-shell {
  width: min(1440px, 100%);
  margin: 0 auto;
  padding: 14px;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 12px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.app-header h1,
.pane-head h2,
.result-head h2,
.upload-box h3,
.drawing-card h3,
p {
  margin: 0;
}

.app-header h1 {
  font-size: 24px;
  line-height: 1.2;
}

.app-header p,
.result-head p,
.upload-box p,
.pane-head span,
.status-pill {
  color: var(--muted);
  font-size: 13px;
}

.app-account-bar,
.account-summary {
  display: flex;
  align-items: center;
}

.app-account-bar {
  flex: 0 0 auto;
  gap: 10px;
}

.account-summary {
  min-width: 170px;
  flex-direction: column;
  align-items: flex-end;
  gap: 2px;
}

.account-summary strong {
  max-width: 240px;
  overflow: hidden;
  font-size: 13px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.account-summary span {
  color: var(--muted);
  font-size: 12px;
}

.account-logout {
  min-height: 32px;
  padding: 5px 9px;
}

.status-pill {
  min-width: 96px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 10px;
  text-align: center;
  background: #fff;
}

.workbench {
  display: grid;
  grid-template-columns: minmax(360px, 430px) minmax(0, 1fr);
  gap: 14px;
  align-items: stretch;
  min-height: calc(100vh - 108px);
}

.input-pane,
.result-pane {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.input-pane {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px;
  overflow: auto;
}

.result-pane {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  overflow: auto;
}

.pipeline-mode-picker {
  display: flex;
  align-items: center;
  gap: 7px;
}

.pipeline-mode-picker select {
  width: auto;
  min-width: 126px;
  height: 32px;
}

.pane-head,
.result-head,
.actions,
.upload-box,
.upload-actions,
.snip-head,
.snip-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.pane-head h2,
.result-head h2 {
  font-size: 18px;
}

.actions-primary button {
  width: 100%;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

label {
  display: grid;
  gap: 5px;
  min-width: 0;
}

label span {
  color: #344054;
  font-size: 12px;
  font-weight: 700;
}

input,
select,
textarea,
button {
  font: inherit;
}

input,
select,
textarea {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  background: #fff;
}

input,
select {
  height: 38px;
  padding: 0 10px;
}

textarea {
  min-height: 96px;
  resize: vertical;
  padding: 10px;
  line-height: 1.45;
}

.notes-field {
  display: grid;
}

.packing-options {
  border-top: 1px solid var(--line);
  padding-top: 12px;
}

.packing-fields {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.packing-preview {
  margin-top: 12px;
}

.packing-preview h3 {
  font-size: 14px;
  margin-bottom: 8px;
}

.packing-preview dl {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) minmax(140px, 1fr);
  border: 1px solid var(--line);
}

.packing-preview dt,
.packing-preview dd {
  min-width: 0;
  margin: 0;
  padding: 7px 9px;
  border-bottom: 1px solid var(--line);
  overflow-wrap: anywhere;
}

.packing-preview dt {
  background: #f4f6f8;
  font-size: 12px;
  font-weight: 700;
}

.packing-message {
  min-height: 20px;
  margin-top: 8px;
  color: #9a3412;
  font-size: 12px;
  font-weight: 700;
}

.upload-box {
  align-items: flex-start;
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  padding: 12px;
  background: #fbfcfe;
}

.upload-box h3 {
  font-size: 14px;
}

.file-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 96px;
  min-height: 36px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  color: var(--accent);
  cursor: pointer;
  background: #fff;
}

.file-button input {
  display: none;
}

.upload-actions {
  flex: 0 0 auto;
}

.file-list {
  display: grid;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.file-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 32px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 8px;
  color: var(--muted);
  font-size: 13px;
}

.file-list .file-empty {
  justify-content: flex-start;
  border-style: dashed;
}

.thumbnail-input {
  display: grid;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.thumbnail-input-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.thumbnail-input h3,
.thumbnail-input p {
  margin: 0;
}

.thumbnail-input h3 {
  font-size: 14px;
}

.thumbnail-input p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 13px;
}

.thumbnail-preview {
  position: relative;
  display: grid;
  width: min(100%, 200px);
  aspect-ratio: 1;
  place-items: center;
  overflow: hidden;
  border: 1px dashed var(--line-strong);
  border-radius: 6px;
  background: #f8fafc;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

.thumbnail-preview img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #fff;
}

.thumbnail-remove {
  position: absolute;
  top: 6px;
  right: 6px;
  background: rgba(255, 255, 255, 0.94);
}

.drawing-card {
  display: grid;
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: #fbfcfe;
}

.drawing-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.drawing-card h3 {
  font-size: 14px;
}

.drawing-card span {
  color: var(--muted);
  font-size: 12px;
}

.drawing-card pre {
  max-height: 140px;
  font-size: 12px;
}

button {
  min-height: 40px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 0 14px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  white-space: nowrap;
}

button:hover {
  background: var(--accent-strong);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

button.secondary {
  background: #fff;
  color: var(--accent);
}

button.ghost {
  border-color: var(--line);
  background: #fff;
  color: var(--muted);
}

.ghost-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 32px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 9px;
  color: var(--muted);
  background: #fff;
  font-size: 13px;
  text-decoration: none;
  white-space: nowrap;
}

.ghost-link:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.icon-button {
  width: 26px;
  min-height: 26px;
  padding: 0;
  border-color: var(--line);
  background: #fff;
  color: var(--muted);
}

.snip-overlay[hidden] {
  display: none;
}

.snip-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(15, 23, 42, 0.58);
}

.snip-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 12px;
  width: min(1120px, 96vw);
  max-height: 92vh;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  background: var(--panel);
  box-shadow: 0 18px 54px rgba(15, 23, 42, 0.28);
}

.snip-head {
  align-items: flex-start;
}

.snip-head h3 {
  margin: 0;
  font-size: 16px;
}

.snip-head p,
.snip-foot span {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.snip-stage {
  min-height: 260px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #0f172a;
}

.snip-stage canvas {
  display: block;
  max-width: 100%;
  margin: 0 auto;
  cursor: crosshair;
  touch-action: none;
}

.snip-foot {
  align-items: center;
}

.costing-sheet {
  flex: 0 0 min(70vh, 720px);
  min-height: 0;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 12px;
}

.sheet-cover {
  display: grid;
  grid-template-columns: minmax(568px, 1fr) 200px;
  gap: 12px;
  align-items: stretch;
  min-width: 780px;
  width: 100%;
}

.sheet-cover > .sheet-table-wrap {
  border-top: 1px solid var(--line-strong);
  height: 200px;
}

.costing-sheet .sheet-cover table {
  height: 100%;
  min-width: 0;
}

.costing-thumbnail {
  width: 200px;
  height: 200px;
  margin: 0;
  overflow: hidden;
  border: 1px solid var(--line-strong);
  background: #fff;
}

.costing-thumbnail img {
  display: block;
  width: 200px;
  height: 200px;
  object-fit: contain;
}

.costing-sheet h2 {
  margin: 0 0 10px;
  padding: 8px 10px;
  border: 1px solid var(--line-strong);
  background: #edf1f5;
  font-size: 18px;
  text-align: center;
  letter-spacing: 0;
}

.costing-sheet h3 {
  margin: 18px 0 0;
  padding: 7px 9px;
  border: 1px solid var(--line-strong);
  border-bottom: 0;
  background: #dfe5ec;
  color: #1f2937;
  font-size: 14px;
  text-transform: uppercase;
}

.costing-sheet p {
  margin: 8px 0;
  color: var(--muted);
  font-size: 13px;
}

.sheet-empty {
  display: grid;
  min-height: 280px;
  place-items: center;
  color: var(--muted);
  text-align: center;
  border: 1px dashed var(--line);
  border-radius: 6px;
  padding: 18px;
}

.sheet-table-wrap {
  overflow-x: auto;
  border: 1px solid var(--line-strong);
  border-top: 0;
}

.sheet-table-wrap + .sheet-table-wrap {
  margin-top: 12px;
  border-top: 1px solid var(--line-strong);
}

.costing-sheet table {
  width: 100%;
  min-width: 780px;
  border-collapse: collapse;
  table-layout: auto;
  font-size: 12px;
}

.costing-sheet th,
.costing-sheet td {
  border: 1px solid #b8c0cc;
  padding: 6px 7px;
  vertical-align: top;
  line-height: 1.35;
  background: #fff;
}

.costing-sheet th {
  background: #f2f4f7;
  color: #111827;
  font-weight: 700;
  text-align: center;
}

.costing-sheet td.num {
  text-align: right;
  white-space: nowrap;
}

.costing-sheet .excel-header td:first-child,
.costing-sheet .excel-summary td:first-child {
  font-weight: 700;
}

.costing-sheet .excel-header td:nth-child(8),
.costing-sheet .excel-header td:nth-child(9) {
  text-align: center;
  font-weight: 700;
}

.costing-sheet .excel-summary td:nth-child(3) {
  text-align: right;
  font-weight: 700;
}

.result-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

pre {
  margin: 0;
  white-space: pre-wrap;
  overflow: auto;
  min-width: 0;
  max-height: 260px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  color: var(--code);
  background: #fbfcfe;
  line-height: 1.5;
  font-family: Consolas, "Courier New", monospace;
}

details {
  border-top: 1px solid var(--line);
  padding-top: 10px;
}

summary {
  cursor: pointer;
  color: var(--muted);
}

.pipeline-workspace[hidden] {
  display: none;
}

.pipeline-workspace {
  display: grid;
  gap: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.pipeline-workspace-head,
.v2-panel-head,
.v2-export-actions,
.v2-confirm-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.pipeline-workspace-head {
  padding: 12px 14px;
  background: #f7f9fb;
}

.pipeline-workspace-head h2,
.v2-panel h3 {
  margin: 0;
  letter-spacing: 0;
}

.pipeline-workspace-head h2 {
  font-size: 17px;
}

.pipeline-workspace-head p {
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
}

.pipeline-advanced {
  border-top: 1px solid var(--line);
  padding: 10px 14px;
}

.pipeline-stage-controls {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.pipeline-stage-controls button,
.v2-export-actions button,
.v2-confirm-actions button {
  min-height: 34px;
  padding: 5px 9px;
  white-space: normal;
}

.pipeline-stage-status {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
  margin: 10px 0 0;
  padding: 0;
  list-style: none;
}

.pipeline-stage-status li {
  min-width: 0;
  border-left: 3px solid var(--line-strong);
  padding: 5px 7px;
  color: var(--muted);
  background: #f8fafc;
  font-size: 12px;
  overflow-wrap: anywhere;
}

.pipeline-stage-status li[data-status="completed"] {
  border-color: #15803d;
  color: #166534;
}

.pipeline-stage-status li[data-status="running"] {
  border-color: #0369a1;
  color: #075985;
}

.pipeline-stage-status li[data-status="failed"] {
  border-color: #b42318;
  color: #991b1b;
}

.pipeline-stage-status li[data-status="invalidated"],
.pipeline-stage-status li[data-status="review_required"] {
  border-color: #986300;
  color: #805300;
}

.v2-cost-trace { min-width: 180px; max-width: 460px; }
.v2-cost-trace summary { cursor: pointer; }
.v2-cost-trace dd { overflow-wrap: anywhere; }

.pipeline-validation {
  min-height: 38px;
  border-top: 1px solid var(--line);
  border-left: 4px solid var(--line-strong);
  padding: 9px 12px;
  color: var(--muted);
  background: #fbfcfe;
  font-size: 13px;
  overflow-wrap: anywhere;
}

.pipeline-validation[data-state="valid"] {
  border-left-color: #15803d;
  color: #166534;
}

.pipeline-validation[data-state="invalid"] {
  border-left-color: #b42318;
  color: #991b1b;
}

.pipeline-validation[data-state="running"] {
  border-left-color: #0369a1;
  color: #075985;
}

.v2-panel,
.v2-panel-grid {
  min-width: 0;
  border-top: 1px solid var(--line);
}

.v2-panel {
  padding: 12px 14px;
}

.v2-panel h3 {
  margin-bottom: 10px;
  font-size: 14px;
}

.v2-panel-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.v2-panel-grid > .v2-panel {
  border-top: 0;
  border-left: 1px solid var(--line);
}

.v2-panel-grid > .v2-panel:first-child {
  border-left: 0;
}

.v2-summary-grid,
.v2-extraction-editor {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 9px;
}

.v2-summary-grid > div {
  display: grid;
  gap: 3px;
  min-width: 0;
  border-left: 2px solid var(--line-strong);
  padding-left: 8px;
}

.v2-summary-grid span,
.v2-summary-grid strong {
  overflow-wrap: anywhere;
}

.v2-summary-grid span {
  color: var(--muted);
  font-size: 11px;
}

.v2-summary-grid strong {
  font-size: 13px;
}

.v2-extraction-editor label span {
  min-height: 28px;
}

.v2-provenance {
  grid-column: 1 / -1;
  color: var(--muted);
  font-size: 12px;
  overflow-wrap: anywhere;
}

.v2-table-wrap {
  width: 100%;
  overflow-x: auto;
  border: 1px solid var(--line);
}

.v2-table-wrap table {
  width: 100%;
  min-width: 760px;
  border-collapse: collapse;
  font-size: 12px;
}

.v2-table-wrap th,
.v2-table-wrap td {
  border: 1px solid var(--line);
  padding: 6px;
  text-align: left;
  vertical-align: top;
  overflow-wrap: anywhere;
}

.v2-table-wrap th {
  background: #eef2f6;
}

.v2-table-wrap input {
  min-width: 104px;
  height: 32px;
  padding: 0 7px;
}

.v2-table-wrap input[type="checkbox"] {
  width: 20px;
  min-width: 20px;
  height: 20px;
}

.v2-list {
  display: grid;
  gap: 7px;
  margin: 0;
  padding-left: 18px;
  font-size: 13px;
}

.v2-definition-list {
  display: grid;
  grid-template-columns: minmax(100px, 0.35fr) minmax(0, 1fr);
  gap: 7px 10px;
  margin: 0;
  font-size: 13px;
}

.v2-definition-list dt {
  color: var(--muted);
  font-weight: 700;
}

.v2-definition-list dd {
  margin: 0;
  overflow-wrap: anywhere;
}

.v2-empty {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.debug-details {
  margin-top: 0;
}

.history-shell {
  max-width: 1440px;
}

.history-workbench {
  display: grid;
  grid-template-columns: minmax(300px, 390px) minmax(0, 1fr);
  gap: 14px;
  align-items: stretch;
  min-height: calc(100vh - 108px);
}

.history-list-pane,
.history-detail-pane {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.history-list-pane {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 12px;
  padding: 14px;
}

.history-detail-pane {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  overflow: auto;
}

.history-filter {
  display: grid;
  grid-template-columns: 1fr 0.7fr auto;
  gap: 8px;
  align-items: end;
}

.history-filter button {
  min-height: 38px;
}

.history-list {
  display: grid;
  align-content: start;
  gap: 8px;
  min-height: 0;
  overflow: auto;
}

.history-run-item {
  display: grid;
  gap: 3px;
  width: 100%;
  min-height: 0;
  border: 1px solid var(--line);
  border-left: 4px solid var(--line-strong);
  border-radius: 6px;
  padding: 9px 10px;
  color: var(--text);
  background: #fff;
  text-align: left;
  white-space: normal;
}

.history-run-item:hover,
.history-run-item[data-selected="true"] {
  border-left-color: var(--accent);
  background: #f7fbfa;
}

.history-run-item strong,
.history-run-item span,
.history-run-item small,
.history-run-item em {
  min-width: 0;
  overflow-wrap: anywhere;
}

.history-run-item span,
.history-run-item small {
  color: var(--muted);
  font-size: 12px;
}

.history-run-item em {
  color: #166534;
  font-size: 12px;
  font-style: normal;
  font-weight: 700;
}

.history-message {
  margin: 0;
  border: 1px dashed var(--line);
  border-radius: 6px;
  padding: 12px;
  color: var(--muted);
  background: #fbfcfe;
  font-size: 13px;
}

@media (max-width: 980px) {
  .workbench {
    grid-template-columns: 1fr;
    min-height: auto;
  }

  .history-workbench {
    grid-template-columns: 1fr;
    min-height: auto;
  }

  .input-pane,
  .result-pane {
    overflow: visible;
  }

  .costing-sheet {
    min-height: 520px;
  }

  .v2-summary-grid,
  .v2-extraction-editor {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .v2-panel-grid {
    grid-template-columns: 1fr;
  }

  .v2-panel-grid > .v2-panel {
    border-top: 1px solid var(--line);
    border-left: 0;
  }
}

@media (max-width: 620px) {
  .app-shell {
    padding: 10px;
  }

  .app-header,
  .app-account-bar,
  .pane-head,
  .drawing-card-head,
  .result-head,
  .actions,
  .upload-box,
  .upload-actions,
  .thumbnail-input-head,
  .snip-head,
  .snip-foot {
    align-items: stretch;
    flex-direction: column;
  }

  .pipeline-mode-picker,
  .pipeline-workspace-head,
  .v2-panel-head,
  .v2-export-actions,
  .v2-confirm-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .history-filter {
    grid-template-columns: 1fr;
  }

  .pipeline-mode-picker select,
  .pipeline-stage-controls,
  .pipeline-stage-status,
  .v2-summary-grid,
  .v2-extraction-editor {
    width: 100%;
    grid-template-columns: 1fr;
  }

  .account-summary {
    width: 100%;
    align-items: flex-start;
  }

  .form-grid {
    grid-template-columns: 1fr;
  }

  .packing-fields,
  .packing-preview dl {
    grid-template-columns: 1fr;
  }

  button,
  .file-button {
    width: 100%;
  }
}

@media print {
  @page {
    size: A3 landscape;
    margin: 10mm;
  }

  body * {
    visibility: hidden;
  }

  #sheet,
  #sheet * {
    visibility: visible;
  }

  #sheet {
    position: absolute;
    inset: 0 auto auto 0;
    width: 100%;
    overflow: visible;
    border: 0;
    padding: 0;
  }

  .pipeline-workspace,
  .debug-details {
    display: none !important;
  }

  .sheet-table-wrap {
    overflow: visible;
  }
}
