/* Cut Optimizer. A one-page studio in the app shell (app-mode.css), laid out
   like T-shirt Design, with numbered steps in two side rails and the live
   layout in the middle. Below 1100px the rails stack under the preview and
   the page scrolls. Inputs and buttons reuse the TOOL PAGE TEMPLATE classes
   in styles.css.
   The page carries svg-theme-aware, whose dark-mode rule sets SVG text to
   fill: currentColor, so each text class below sets color as well as fill. */

#cut-optimizer {
  --co-rail: 400px;
  --co-radius: 12px;
  --co-sheet: #efe2c6;
  --co-sheet-edge: #b89b6a;
  --co-hatch: rgba(90, 70, 40, 0.45);
  --co-part-edge: rgba(0, 0, 0, 0.55);
  --co-part-text: #1b1b1b;
  --co-cut: #c0392b;
  --co-offcut-text: #5b4a30;
  --co-stage: #f1ece2;
  background:
    radial-gradient(circle at 50% 42%, rgba(var(--site-accent-rgb), 0.08), transparent 36%),
    var(--bs-body-bg, #f7f7f8);
}

#cut-optimizer[data-mode="paper"] {
  --co-sheet: #ffffff;
  --co-sheet-edge: #9a9a9a;
  --co-hatch: rgba(80, 80, 80, 0.35);
  --co-offcut-text: #555;
}

body.quarto-dark #cut-optimizer {
  --co-sheet: #4a3f2c;
  --co-sheet-edge: #8f7a55;
  --co-hatch: rgba(235, 215, 175, 0.35);
  --co-cut: #ff7b6b;
  --co-offcut-text: #e6d6b6;
  --co-stage: #2b2b2b;
}

body.quarto-dark #cut-optimizer[data-mode="paper"] {
  --co-sheet: #3a3a3a;
  --co-sheet-edge: #8a8a8a;
  --co-hatch: rgba(230, 230, 230, 0.3);
  --co-offcut-text: #ddd;
}

/* ---- Workspace -------------------------------------------------------- */

.co-workspace {
  position: absolute;
  inset: 58px 12px 12px;
  display: grid;
  grid-template-columns: var(--co-rail) minmax(0, 1fr) var(--co-rail);
  gap: 14px;
  min-height: 0;
}

.co-rail {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0 2px 4px;
}

/* ---- Step cards ------------------------------------------------------- */

.co-step {
  flex: 0 0 auto;
  border: 1px solid var(--site-surface-border);
  border-radius: var(--co-radius);
  background: var(--bs-body-bg, #fff);
  box-shadow: 0 5px 20px rgba(20, 24, 50, 0.07);
}

.co-step-head {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(60px, 32%) 1rem;
  column-gap: 10px;
  align-items: center;
  padding: 11px 13px;
  cursor: pointer;
  list-style: none;
  user-select: none;
}

.co-step-head::-webkit-details-marker {
  display: none;
}

.co-step-head::after {
  content: "+";
  justify-self: end;
  color: var(--site-accent);
  font-size: 1.15rem;
  line-height: 1;
}

.co-step[open] > .co-step-head::after {
  content: "\2212";
}

.co-step-head:focus-visible {
  outline: none;
  box-shadow: inset 0 0 0 3px var(--site-accent-ring);
  border-radius: var(--co-radius);
}

.co-step-head h2,
.co-preview-head h2 {
  border-bottom: 0;
  padding-bottom: 0;
}

.co-step-head h2 {
  display: flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
  margin: 0;
  color: var(--bs-body-color, #212529);
  font-size: 0.92rem;
  font-weight: 600;
  white-space: nowrap;
}

.co-step-title {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.co-step-num {
  display: inline-grid;
  place-items: center;
  flex: 0 0 auto;
  width: 1.55rem;
  height: 1.55rem;
  border-radius: 50%;
  background: var(--site-accent);
  color: var(--site-accent-contrast);
  font-size: 0.8rem;
  font-weight: 700;
}

.co-step-sum {
  overflow: hidden;
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.75rem;
  color: var(--bs-secondary-color, #666);
}

.co-step-body {
  padding: 12px 13px 13px;
  border-top: 1px solid var(--site-surface-border);
}

.co-note {
  margin: 10px 0 0;
  font-size: 0.78rem;
  line-height: 1.45;
  color: var(--bs-secondary-color, #555);
}

.co-note code {
  font-size: 0.95em;
}

/* ---- Step 1: material choice ------------------------------------------ */

.co-choice-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.co-choice {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  padding: 9px 10px;
  border: 1.5px solid var(--site-surface-border);
  border-radius: 10px;
  background: var(--bs-body-bg, #fff);
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.co-choice strong {
  font-size: 0.9rem;
}

.co-choice span {
  font-size: 0.72rem;
  color: var(--bs-secondary-color, #666);
}

.co-choice:hover {
  border-color: var(--site-accent);
}

.co-choice:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--site-accent-ring);
}

.co-choice[aria-pressed="true"] {
  border-color: var(--site-accent);
  box-shadow: 0 0 0 2px var(--site-accent-ring);
}

.co-choice[aria-pressed="true"]::after {
  content: "\2713";
  position: absolute;
  top: 6px;
  right: 9px;
  color: var(--site-accent);
  font-size: 1.1rem;
  line-height: 1;
}

.co-choice-icon {
  width: 48px;
  height: 32px;
  margin-bottom: 4px;
}

.co-icon-sheet {
  fill: var(--site-accent-soft);
  stroke: var(--site-accent);
  stroke-width: 1.5;
}

.co-icon-grain {
  fill: none;
  stroke: var(--site-accent);
  stroke-width: 1.2;
  opacity: 0.7;
}

.co-field-row {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.co-field-row > * {
  flex: 1 1 0;
  min-width: 0;
}

.co-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--bs-secondary-color, #555);
}

.co-select {
  width: 100%;
  padding: 6px 10px;
  font-size: 14px;
}

.co-select:disabled {
  opacity: 0.5;
}

/* ---- Steps 2 and 3: rows ---------------------------------------------- */

/* Each row is two lines, the name on top and width, length, quantity,
   grain, and remove below, so the sizes stay readable in a narrow rail.
   A part's color marks the left edge of its name field. */
.co-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr) 3.4em auto 32px;
  gap: 6px 5px;
  align-items: center;
}

.co-row .co-name {
  grid-column: 1 / -1;
}

.co-row-hint {
  margin: 0 0 8px;
  font-size: 0.75rem;
  color: var(--bs-secondary-color, #555);
}

.co-times {
  font-size: 13px;
  color: var(--bs-secondary-color, #666);
}

.co-rows {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* Space held for the rows the script builds, so nothing below moves when
   they appear. The page script sets the saved row counts before these
   lists are parsed. A row is two 36px lines and a 6px gap (78px), with
   10px between rows. */
.co-rows-stock:empty {
  min-height: max(0px, calc(var(--co-stock-rows, 1) * 88px - 10px));
}

.co-rows-parts:empty {
  min-height: max(0px, calc(var(--co-part-rows, 4) * 88px - 10px));
}

.co-row .tool-input {
  height: 36px;
  min-width: 0;
  padding: 4px 7px;
  font-size: 14px;
  border-width: 1.5px;
}

.co-row .tool-input[aria-invalid="true"],
.co-step .tool-input[aria-invalid="true"] {
  border-color: #dc3545;
}

.co-grain {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: 12px;
  cursor: pointer;
}

.co-grain input {
  accent-color: var(--site-accent);
}


.co-rows-parts .co-name {
  border-left-width: 6px;
}

.co-remove {
  height: 36px;
  padding: 0;
  font-size: 16px;
  line-height: 1;
}

.co-row-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.co-row-actions > .co-select {
  flex: 1 1 auto;
  width: auto;
  min-width: 0;
}

/* Kept in the layout but out of sight, so the row never changes height. */
.co-hold {
  visibility: hidden;
}

/* ---- Step 4: blade ---------------------------------------------------- */

.co-step-body > .tool-label:first-child {
  margin-top: 0;
}

.co-step .co-length {
  padding: 7px 10px;
  font-size: 15px;
}

/* ---- Middle column: preview ------------------------------------------- */

.co-preview-column {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  border: 1px solid var(--site-surface-border);
  border-radius: var(--co-radius);
  background: var(--bs-body-bg, #fff);
  box-shadow: 0 5px 20px rgba(20, 24, 50, 0.07);
  overflow: hidden;
}

.co-preview-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 14px;
  border-bottom: 1px solid var(--site-surface-border);
}

.co-preview-head h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
}

.co-check {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  cursor: pointer;
}

.co-check input {
  accent-color: var(--site-accent);
}

.co-preview {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  margin: 12px 12px 0;
  border-radius: 10px;
  background: var(--co-stage);
}

.co-sheets {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 4px;
}

.co-pager {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px 0;
}

.co-pager .tool-button {
  min-width: 34px;
  font-size: 18px;
  line-height: 1;
}

.co-pager-label {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
}

.co-summary {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 6px;
  padding: 8px 12px 0;
}

.co-tile {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
  padding: 6px 8px;
  background: var(--site-surface);
  border: 1px solid var(--site-control-border);
  border-radius: 8px;
}

.co-tile-value {
  overflow: hidden;
  min-height: 1.4em;
  font-size: 1rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.co-tile-label {
  font-size: 11px;
  color: var(--bs-secondary-color, #666);
}

.co-status-bar {
  padding: 6px 12px 0;
}

.co-status {
  min-height: 1.45em;
  margin: 0;
  font-size: 13px;
}

.co-status-error {
  color: #b02a37;
}

body.quarto-dark .co-status-error {
  color: #ff8a8a;
}

.co-unplaced {
  max-height: 5.5em;
  margin-top: 4px;
  padding: 6px 10px;
  overflow-y: auto;
  border: 1.5px solid #dc3545;
  border-left-width: 4px;
  border-radius: 8px;
  font-size: 12px;
}

.co-unplaced-list {
  margin: 2px 0 0;
  padding-left: 1.1em;
}

.co-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 10px 12px 12px;
}

.co-actions-gap {
  flex: 1 1 auto;
}

.co-actions .tool-button {
  padding: 8px 14px;
  font-size: 14px;
}

/* ---- Drawing ---------------------------------------------------------- */

.co-svg {
  display: block;
  max-width: 100%;
  max-height: 100%;
  font-family: inherit;
}

.co-sheet-bg {
  fill: var(--co-sheet);
  stroke: var(--co-sheet-edge);
  stroke-width: 1.5px;
}

.co-hatch-line {
  stroke: var(--co-hatch);
  stroke-width: 1px;
}

.co-part-rect {
  stroke: var(--co-part-edge);
  stroke-width: 1px;
}

.co-part-text {
  color: var(--co-part-text);
  fill: var(--co-part-text);
  font-weight: 600;
}

.co-offcut-text {
  color: var(--co-offcut-text);
  fill: var(--co-offcut-text);
  font-style: oblique 10deg;
}

.co-cut-line {
  stroke: var(--co-cut);
  stroke-width: 1px;
  stroke-dasharray: 4 3;
}

.co-cut-line.co-cut-hot {
  stroke-width: 4px;
  stroke-dasharray: none;
}

.co-cut-dot {
  fill: var(--co-cut);
  stroke: #fff;
  stroke-width: 1px;
}

.co-cut-mark.co-cut-hot .co-cut-dot {
  stroke-width: 3px;
}

.co-cut-num {
  color: #fff;
  fill: #fff;
  font-weight: 700;
}

/* ---- Step 5: cut sequence --------------------------------------------- */

/* A fixed-height box that scrolls, so the table filling in after load (or
   growing with the sheet) never moves the steps below it. */
.co-cut-scroll {
  height: 20rem;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.co-cut-scroll .co-cut-table thead th {
  position: sticky;
  top: 0;
  z-index: 1;
}

.co-cut-table {
  width: 100%;
  margin: 8px 0 0;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.co-cut-table th,
.co-cut-table td {
  padding: 3px 8px 3px 0;
  text-align: left;
}

.co-cut-table tbody tr:hover,
.co-cut-table tbody tr:focus {
  outline: none;
  background: var(--site-accent-soft);
}

/* ---- Step 6: downloads ------------------------------------------------ */

.co-export-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.co-export {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--site-control-border);
}

.co-export:last-child {
  padding-bottom: 0;
  border-bottom: 0;
}

.co-export strong {
  display: block;
  font-size: 0.88rem;
}

.co-export span {
  display: block;
  font-size: 0.76rem;
  line-height: 1.4;
  color: var(--bs-secondary-color, #555);
}

.co-export-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.co-about {
  font-size: 0.82rem;
  line-height: 1.5;
}

.co-about p {
  margin: 0 0 8px;
}

.co-about ul {
  margin: 0;
  padding-left: 1.1em;
}

.co-about li + li {
  margin-top: 6px;
}

/* The print view is built only when printing. */
.co-print {
  display: none;
}

/* ---- Narrower screens ------------------------------------------------- */

@media (max-width: 1600px) {
  #cut-optimizer {
    --co-rail: 360px;
  }
}

/* Laptop widths: two columns. The steps flow down the left and scroll with
   the page, and the preview stays pinned on the right. */
@media (max-width: 1350px) {
  html,
  body {
    height: auto;
    overflow: auto;
  }

  #cut-optimizer.app-shell {
    position: relative;
    inset: auto;
    min-height: 100vh;
    overflow: visible;
    padding: 0 12px 18px;
  }

  #cut-optimizer .app-pill {
    position: relative;
    top: auto;
    left: auto;
    display: inline-flex;
    margin: 12px 0 0;
  }

  .co-workspace {
    position: relative;
    inset: auto;
    margin-top: 10px;
    grid-template-columns: minmax(340px, 400px) minmax(0, 1fr);
    grid-template-areas: "left preview" "right preview";
    align-items: start;
    gap: 10px 14px;
  }

  .co-rail {
    overflow: visible;
    padding: 0;
  }

  .co-rail-left {
    grid-area: left;
  }

  .co-rail-right {
    grid-area: right;
  }

  .co-preview-column {
    grid-area: preview;
    position: sticky;
    top: 12px;
    /* The home pill sits above the grid, so the first screen has about
       56px less room than the viewport. */
    height: calc(100vh - 68px);
    min-height: 520px;
  }

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

/* Below 900px one column, with the preview first and then the steps. */
@media (max-width: 900px) {
  .co-workspace {
    display: flex;
    flex-direction: column;
  }

  .co-preview-column {
    order: -1;
    position: relative;
    top: auto;
    height: min(78vh, 680px);
  }
}

@media (max-width: 640px) {
  .co-summary {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .co-actions .tool-button {
    flex: 1 1 auto;
  }

  .co-actions-gap {
    display: none;
  }
}

/* ---- Print and save as PDF -------------------------------------------- */

@media print {
  html,
  body {
    height: auto !important;
    overflow: visible !important;
    background: #fff !important;
  }

  #cut-optimizer.app-shell {
    position: static;
    overflow: visible;
    background: #fff;
  }

  #cut-optimizer .co-workspace,
  #cut-optimizer .app-pill {
    display: none !important;
  }

  #cut-optimizer .co-print {
    display: block;
    color: #000;
  }

  .co-print-head h2 {
    margin: 0 0 4px;
    font-size: 18pt;
  }

  .co-print-sheet + .co-print-sheet {
    break-before: page;
  }

  .co-print-sheet h3 {
    margin: 10px 0 6px;
    font-size: 12pt;
  }

  .co-print .co-svg {
    width: 100%;
    height: auto;
    max-height: 60vh;
  }

  .co-print .co-cut-table {
    width: auto;
    font-size: 9pt;
  }
}
