/* 66_parttype.css - KP Equipment editor release (Addendum B, B1.3). The part-type picker's look (design slice F12a).
 *
 * What 66_select.js builds (PT-PICK-a / -b) and this file styles:
 *   .sel-type-list.pt-pick[role=dialog]
 *     > input.pt-pick-search
 *     > .pt-pick-list[role=listbox]
 *         > .pt-pick-section ("Your list" / "All part types"), .pt-pick-group, .pt-pick-empty,
 *           button.pt-pick-item[role=option][aria-selected] (.is-current) > .pt-pick-name + .pt-pick-also
 *     > .pt-pick-dir[role=group] > .pt-pick-dir-label, button.pt-pick-flow[data-flow][aria-pressed]
 *     > button.pt-pick-add
 *     > .pt-pick-add-form[hidden] > input.pt-pick-add-name, select.pt-pick-add-family,
 *                                   button.pt-pick-add-go, button.pt-pick-add-cancel,
 *                                   .pt-pick-near[role=alertdialog][hidden] > p.pt-pick-near-text,
 *                                     button.pt-pick-near-use, button.pt-pick-near-anyway
 *     > button.pt-pick-skip (after a shape is drawn only)
 * Three homes: #sel-bar (the Part type button), .pt-pick-float[data-after-draw] (next to a new shape) and
 * .pe-type-host in the Mark parts "Add part" dialog (81 sets hidden on Direction, Add and the form there).
 *
 * Load order: this file is linked BEFORE 66_select.css, so a rule that has to beat .sel-type-list is written
 * .pt-pick.sel-type-list (two classes). Colours come from 01_tokens.css only; --pt-* are local to the picker.
 * Sizes: 44 px controls by default (touch); a mouse gets 34 / 36 px rows and fields.
 * No state is colour alone: the active row has a bar and a frame, the current type and a pressed direction carry a
 * tick, a disabled Add is dashed and says why in words (its title, drawn under it, because touch has no tooltip).
 */

/* ---- 1. the box ---- */
.pt-pick.sel-type-list {
  --pt-row: 44px; --pt-ctl: 44px; --pt-field: 16px;
  --pt-list-max: clamp(132px, calc(100dvh - 600px), 320px);
  display: grid; grid-template-columns: minmax(0, 1fr) auto; column-gap: var(--sp-2); align-items: center; align-content: start;
  max-height: calc(100vh - 300px); max-height: max(200px, calc(100dvh - 300px));
  overflow-x: hidden; overflow-y: auto; overscroll-behavior: contain;
  color: var(--ink); font-size: 14px; line-height: 1.3; text-align: left;
}
.pt-pick [hidden] { display: none !important; } /* FIX-R1 (Martin r1b NEXT 1): every hidden node in the picker, not only direct children */
.pt-pick > * { grid-column: 1 / -1; min-width: 0; }
.pt-pick.sel-type-list > * + * { margin-top: 6px; }
.pt-pick > .pt-pick-search { grid-row: 1; }
.pt-pick > .pt-pick-list { grid-row: 2; }
.pt-pick > .pt-pick-dir { grid-row: 3; }
.pt-pick > .pt-pick-add { grid-row: 4; grid-column: 1; justify-self: start; }
.pt-pick > .pt-pick-skip { grid-row: 4; grid-column: 2; justify-self: end; align-self: start; }
.pt-pick > .pt-pick-add-form { grid-row: 5; }
.sel-bar:has(> .pt-pick) { min-width: min(420px, calc(100vw - 24px)); }

/* ---- 2. search and the two form fields ---- */
.pt-pick-search, .pt-pick-add-name, .pt-pick-add-family {
  box-sizing: border-box; width: 100%; min-width: 0; min-height: var(--pt-ctl); padding: 0 10px;
  border: 1px solid var(--ink-muted); border-radius: var(--r-sm); background: var(--surface); color: var(--ink);
  font: inherit; font-size: var(--pt-field); line-height: 1.2;
}
.pt-pick-search::placeholder, .pt-pick-add-name::placeholder { color: var(--ink-muted); opacity: 1; }
.pt-pick-add-family { cursor: pointer; }
.pt-pick-search:focus-visible, .pt-pick-add-name:focus-visible, .pt-pick-add-family:focus-visible { outline: 2px solid var(--brand); outline-offset: -1px; border-color: var(--brand); }

/* ---- 3. the list: two sections, groups, rows ---- */
.pt-pick-list {
  box-sizing: border-box; max-height: var(--pt-list-max); overflow-x: hidden; overflow-y: auto; overscroll-behavior: contain;
  padding: 0 0 4px; border: 1px solid var(--line); border-radius: var(--r-sm); background: var(--surface); scroll-padding-top: 36px;
}
.pt-pick-section {
  position: sticky; top: 0; z-index: 1; padding: 8px 12px 7px; border-bottom: 1px solid var(--line);
  background: var(--surface-alt); color: var(--ink); font-size: 13px; font-weight: 700; line-height: 1.25;
}
.pt-pick-section ~ .pt-pick-section { margin-top: 6px; border-top: 1px solid var(--line); }
.pt-pick-group { padding: 10px 12px 3px; color: var(--ink-soft); font-size: 12px; font-weight: 600; line-height: 1.3; }
.pt-pick-empty { padding: 12px; color: var(--ink-soft); font-size: 13px; line-height: 1.4; }
.pt-pick-item {
  box-sizing: border-box; display: flex; flex-wrap: wrap; align-items: baseline; align-content: center; column-gap: 4px;
  width: calc(100% - 8px); min-height: var(--pt-row); margin: 0 4px; padding: 5px 8px; border: 0; border-radius: var(--r-sm);
  background: transparent; color: var(--ink); font: inherit; line-height: 1.25; text-align: left; overflow-wrap: anywhere;
  cursor: pointer; -webkit-tap-highlight-color: transparent; touch-action: manipulation;
}
.pt-pick-name { font-weight: 500; }
.pt-pick-also { color: var(--ink-soft); font-size: 12.5px; }
.pt-pick-item:active { background: var(--brand-soft); }
.pt-pick-item[aria-selected="true"] { background: var(--brand-soft); box-shadow: inset 3px 0 0 var(--brand), inset 0 0 0 1px var(--brand); }
.pt-pick-item.is-current .pt-pick-name { font-weight: 700; }
.pt-pick-item.is-current::after {
  content: ""; flex: none; align-self: center; width: 10px; height: 5px; margin: -3px 2px 0 auto;
  border: solid var(--ink); border-width: 0 0 2px 2px; transform: rotate(-45deg);
}
.pt-pick-item:focus-visible { outline: 2px solid var(--brand); outline-offset: -2px; }
.pt-pick-item[aria-selected="true"]:focus-visible { outline-color: var(--ink); }

/* ---- 4. Direction: four choices and the Turn 90 switch ----
 * Two fixed rows on every size: the label and Turn 90 share the first, the four choices share the second in even
 * columns. Nothing wraps when a choice is pressed; a column grows only when a pressed name needs the room. */
.pt-pick-dir { display: grid; grid-template-columns: repeat(4, minmax(min-content, 1fr)); align-items: center; gap: 4px; }
.pt-pick-dir-label { grid-row: 1; grid-column: 1 / 3; color: var(--ink-soft); font-size: 13px; font-weight: 600; }
.pt-pick-flow, .pt-pick-add, .pt-pick-skip, .pt-pick-add-go, .pt-pick-add-cancel, .pt-pick-near-use, .pt-pick-near-anyway {
  box-sizing: border-box; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-width: 44px; min-height: var(--pt-ctl); padding: 0 12px;
  border: 1px solid var(--line); border-color: color-mix(in srgb, var(--ink-muted) 55%, var(--line)); border-radius: var(--r-sm);
  background: var(--surface); color: var(--ink); font: inherit; font-size: 14px; font-weight: 500; line-height: 1.2;
  cursor: pointer; -webkit-tap-highlight-color: transparent; touch-action: manipulation;
}
.pt-pick-flow { padding: 0 10px; font-size: 13px; white-space: nowrap; }
.pt-pick-flow[data-flow="turn"] { grid-row: 1; grid-column: 3 / 5; justify-self: end; border-radius: var(--r-pill); }
.pt-pick-flow[aria-pressed="true"] { border-color: var(--brand); background: var(--brand-soft); box-shadow: inset 0 0 0 1px var(--brand); font-weight: 700; }
.pt-pick-flow[aria-pressed="true"]::before {
  content: ""; flex: none; width: 9px; height: 5px; margin-top: -3px;
  border: solid currentColor; border-width: 0 0 2px 2px; transform: rotate(-45deg);
}
.pt-pick-flow:focus-visible, .pt-pick-add:focus-visible, .pt-pick-skip:focus-visible, .pt-pick-add-cancel:focus-visible, .pt-pick-near-use:focus-visible, .pt-pick-near-anyway:focus-visible { outline: 2px solid var(--brand); outline-offset: -2px; }
.pt-pick-flow[aria-pressed="true"]:focus-visible { outline-color: var(--ink); }

/* ---- 5. add a new part type ---- */
.pt-pick-add { flex-wrap: wrap; justify-content: flex-start; max-width: 100%; text-align: left; }
.pt-pick-add::before { content: "+"; font-size: 17px; font-weight: 600; line-height: 1; }
.pt-pick-add:disabled { padding-top: 6px; padding-bottom: 6px; border: 1px dashed var(--ink-muted); background: var(--surface-alt); color: var(--ink-soft); cursor: not-allowed; }
.pt-pick-add:disabled[title]::after { content: attr(title); flex: 0 0 100%; font-size: 12px; font-weight: 400; line-height: 1.35; white-space: normal; }
.pt-pick-add-form {
  position: relative; display: flex; flex-wrap: wrap; align-items: center; gap: 6px;
  padding: 10px; border: 1px solid var(--line); border-radius: var(--r-sm); background: var(--surface-alt);
}
.pt-pick-add-name { flex: 1 1 190px; width: auto; }
.pt-pick-add-family { flex: 1 1 170px; width: auto; }
.pt-pick-add-go { margin-left: auto; border-color: transparent; background: var(--brand); color: var(--on-brand); font-weight: 600; }
.pt-pick-add-go:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
.pt-pick-add-go:disabled { border: 1px dashed var(--ink-muted); background: transparent; color: var(--ink-soft); cursor: not-allowed; }
.pt-pick-add-form:has(> .pt-pick-add-go:disabled[title]) { padding-bottom: 32px; }
.pt-pick-add-go:disabled[title]::after {
  content: attr(title); position: absolute; left: 10px; right: 10px; bottom: 8px;
  color: var(--ink-soft); font-size: 12px; font-weight: 400; line-height: 1.3; text-align: left; white-space: normal; pointer-events: none;
}
/* the near-duplicate prompt in the form (role=alertdialog, shown after Add when the name is nearly one in the list):
   the question, then "Use <name>" (the answer that makes no new type, so it is the marked one) and "Add anyway".
   It takes its own row of the form; long names wrap inside the box and the buttons, nothing is cut. */
.pt-pick-near {
  box-sizing: border-box; flex: 0 0 100%; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; min-width: 0;
  padding: 8px 10px 10px 13px; border: 1px solid var(--line); border-radius: var(--r-sm); background: var(--surface);
  box-shadow: inset 3px 0 0 var(--brand);
}
.pt-pick-near[hidden] { display: none; }
.pt-pick-near-text { flex: 0 0 100%; min-width: 0; margin: 0; color: var(--ink); font-size: 14px; font-weight: 500; line-height: 1.35; overflow-wrap: anywhere; }
.pt-pick-near-use, .pt-pick-near-anyway { flex: 0 1 auto; max-width: 100%; padding-top: 6px; padding-bottom: 6px; white-space: normal; overflow-wrap: anywhere; }
.pt-pick-near-use { border-color: var(--brand); background: var(--brand-soft); font-weight: 600; }
.pt-pick-near-use:focus-visible { outline-color: var(--ink); }
.pt-pick:has(> .pt-pick-add-form:not([hidden])) > .pt-pick-list { max-height: min(var(--pt-list-max), 132px); }

@media (hover: hover) {
  .pt-pick-item:hover { background: var(--surface-alt); }
  .pt-pick-item[aria-selected="true"]:hover { background: var(--brand-soft); }
  .pt-pick-flow:hover, .pt-pick-add:hover, .pt-pick-skip:hover, .pt-pick-add-cancel:hover, .pt-pick-near-anyway:hover { background: var(--surface-alt); }
  .pt-pick-near-use:hover { box-shadow: inset 0 0 0 1px var(--brand); }
  .pt-pick-flow[aria-pressed="true"]:hover { background: var(--brand-soft); }
  .pt-pick-add-go:hover { background: var(--brand-hover); }
  .pt-pick-add-go:disabled:hover { background: transparent; }
}
@media (hover: hover) and (pointer: fine) {
  .pt-pick.sel-type-list { --pt-row: 34px; --pt-ctl: 36px; --pt-field: 14px; --pt-list-max: clamp(132px, calc(100dvh - 560px), 320px); }
  .pt-pick-flow { min-height: 32px; }
}

/* ---- 6. the bar button's value: "Belt conveyor" / "mixed" / "not set" ---- */
.sel-bar-btn > .sel-bar-type-value {
  box-sizing: border-box; flex: 0 1 auto; min-width: min(9ch, 100%); max-width: min(18ch, 42vw); overflow: hidden; padding: 2px 8px;
  border: 1px solid var(--line); border-color: color-mix(in srgb, var(--ink-muted) 55%, var(--line)); border-radius: var(--r-pill);
  background: var(--surface-alt); color: var(--ink); font-size: 13px; font-weight: 600; line-height: 1.4;
  text-overflow: ellipsis; white-space: nowrap;
}

/* ---- 7. after a shape is drawn: the floating picker ----
 * No --pt-pick-x / -y (touch): it docks. With them (mouse, wide layouts): it hangs --pt-gap under the shape's bottom
 * edge, centred on the shape; when that would run past the bottom of the plan AND there is room above, it sits above
 * the top edge instead (--pt-step is 0 or the whole jump); it always stays inside the plan area. */
.pt-pick-float {
  --pt-gap: 10px;
  position: fixed; left: 50%; bottom: 56px; z-index: 24; box-sizing: border-box; width: min(380px, calc(100vw - 16px));
  margin: 0; padding: 8px; border: 1px solid var(--line); border-radius: var(--r); background: var(--surface); color: var(--ink);
  box-shadow: var(--shadow-lg); transform: translateX(-50%);
}
.pt-pick-float:empty { display: none; }
.pt-pick-float > .pt-pick.sel-type-list { order: 0; width: auto; min-width: 0; max-height: calc(100vh - 240px); max-height: max(200px, calc(100dvh - 240px)); margin: 0; padding: 0; border: 0; }
@media screen and (min-width: 701px) and (min-height: 501px), screen and (min-width: 961px) {
  .pt-pick-float[style*="--pt-pick-x"][style*="--pt-pick-y"] {
    --pt-min-y: calc(60px + var(--editbar-h, 65px) + 8px); --pt-max-y: calc(100vh - 56px); --pt-max-x: calc(100vw - 8px);
    --pt-below: calc(var(--pt-pick-y) + var(--pt-gap));
    --pt-above: calc(var(--pt-pick-top, var(--pt-pick-y)) - var(--pt-gap) - 100%);
    --pt-can: clamp(0px, calc((var(--pt-above) - var(--pt-min-y)) * 9999), calc(var(--pt-below) - var(--pt-above)));
    --pt-step: clamp(0px, calc((var(--pt-below) + 100% - var(--pt-max-y)) * 9999), var(--pt-can));
    left: 0; top: 0; bottom: auto;
    transform: translate(clamp(8px, calc(var(--pt-pick-x) - 50%), calc(var(--pt-max-x) - 100%)), clamp(var(--pt-min-y), calc(var(--pt-below) - var(--pt-step)), calc(var(--pt-max-y) - 100%)));
  }
  body:not(.drawer-tucked) .stage:has(~ .drawer:not([hidden])) > .pt-pick-float[style*="--pt-pick-x"] { --pt-max-x: calc(100vw - var(--drawer-w, 360px) - 8px); }
}
@media (hover: hover) and (pointer: fine) {
  /* short on an 800 px screen (3 rows and a peek), so the picker fits under or over a shape in the middle of the plan */
  .pt-pick-float > .pt-pick.sel-type-list { --pt-list-max: clamp(110px, calc(100dvh - 680px), 280px); }
}

/* ---- 8. inside the Mark parts "Add part" dialog ---- */
.pe-field:has(> .pe-type-host) { grid-template-columns: minmax(0, 1fr) auto; column-gap: var(--sp-2); }
.pe-field:has(> .pe-type-host) > * { grid-column: 1 / -1; min-width: 0; }
.pe-field:has(> .pe-type-host) > #pe-part-type { grid-column: 1; text-align: left; }
.pe-field:has(> .pe-type-host) > #pe-part-type-un { grid-column: 2; }
.pe-type-host:empty { display: none; }
.pe-type-host > .pt-pick.sel-type-list {
  --pt-list-max: clamp(132px, 34dvh, 264px);
  order: 0; width: auto; min-width: 0; max-height: none; overflow: visible; margin: 2px 0 0; padding: 8px;
  border: 1px solid var(--line); border-radius: var(--r-sm); background: var(--surface-alt);
}
@media screen and (max-width: 520px) {
  .pe-field:has(> .pe-type-host) > #pe-part-type-un { grid-column: 1 / -1; order: 1; justify-self: start; }
  .pe-field:has(> .pe-type-host) > .pe-field-hint { order: 2; }
}

/* ---- 9. phone layout (the edit bar is fixed at the bottom there, 65_editor.css --eb-h) ---- */
@media screen and (max-width: 700px), screen and (max-height: 500px) and (max-width: 960px) {
  .pt-pick.sel-type-list { max-height: calc(100vh - 360px); max-height: max(200px, calc(100dvh - 360px)); }
  .pt-pick.sel-type-list:has(> .pt-pick-add-form:not([hidden])) > .pt-pick-list { display: none; }
  .pt-pick-flow:not([data-flow="turn"]) { padding: 0 6px; }
  /* the bar with the picker closed: a column of the bar is about 84 px wide and "Part type" beside its value needs
     about 130, so the label broke one letter a line. Here the button is the label over its value; the value is plain
     (the pill frame would take 18 of the 76 px) and may use two lines. One row of ordinary buttons. Open: as before. */
  .sel-bar:not(:has(> .pt-pick)) > .sel-bar-type:has(> .sel-bar-type-value) { flex-direction: column; gap: 1px; }
  .sel-bar:not(:has(> .pt-pick)) > .sel-bar-type > .sel-bar-type-value {
    display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; flex: none; min-width: 0; max-width: 100%;
    padding: 0; border: 0; border-radius: 0; background: transparent; font-size: 13px; line-height: 1.2;
    white-space: normal; overflow-wrap: break-word;
  }
  /* the picker open in the action bar: the other actions wait until it closes; the count and the Part type button
     (it closes the picker) share one row under it. The list gets what is left of 60% of the screen height. */
  .sel-bar:has(> .pt-pick) > .sel-bar-btn:not(.sel-bar-type) { display: none; }
  .sel-bar:has(> .pt-pick) > .sel-bar-count { grid-column: 1; align-self: center; }
  .sel-bar:has(> .pt-pick) > .sel-bar-type { grid-column: 1 / -1; min-height: 44px; }
  .sel-bar:has(> .pt-pick) > .sel-bar-count ~ .sel-bar-type { grid-column: 2 / -1; }
  .sel-bar > .pt-pick.sel-type-list {
    --pt-list-max: clamp(132px, calc(60dvh - 262px), 320px);
    max-height: calc(100vh - 300px); max-height: max(200px, calc(100dvh - 300px)); margin-bottom: 0;
  }
  .sel-bar > .pt-pick.sel-type-list > * + * { margin-top: 4px; }
  .sel-bar > .pt-pick .pt-pick-section { padding-top: 6px; padding-bottom: 5px; }
  .sel-bar > .pt-pick .pt-pick-section + .pt-pick-group { padding-top: 6px; }
  .pt-pick-float {
    left: 0; right: 0; top: auto; bottom: var(--eb-h, calc(72px + env(safe-area-inset-bottom, 0px))); width: auto;
    padding: 10px var(--sp-3) var(--sp-3); border-width: 1px 0 0; border-radius: var(--r) var(--r) 0 0; transform: none;
  }
  .pt-pick-float > .pt-pick.sel-type-list { max-height: calc(100vh - 300px); max-height: max(200px, calc(100dvh - 300px)); }
}
