/*
 * EncodeLabel kit: the label-paper picker (assets/js/kit-paper.js).
 *
 * Only what is particular to the picker lives here. Buttons, tabs, boxes to
 * type in, steppers and the pop-up itself come from site.css. Every colour is
 * one of the site's tokens, so the light and the dark theme both work. The
 * small pictures of sheets are drawn by EL.templates.diagram() in fixed paper
 * greys: paper looks like paper in both themes.
 *
 *   .kpa-dialog     the pop-up (a fixed height, so it does not jump as you search)
 *   .kpa-top        search box, "Custom size" and the tabs; stays put
 *   .kpa-scroll     the list of papers; scrolls
 *   .kpa-list       the grid of cards     .kpa-card  one paper
 *   .kpa-form       the custom-size form  .kpa-preview  its drawing
 *   .kpa-button     the small "this paper / Change" control on a tool page
 */

/* ---- The pop-up -------------------------------------------------------------- */
.kpa-dialog { height: min(88dvh, 860px); }
.kpa-dialog > .dialog-body { display: flex; flex-direction: column; padding: 0; overflow: hidden; }
.kpa, .kpa-browse, .kpa-custom { display: flex; flex: 1 1 auto; flex-direction: column; min-height: 0; }

.kpa-top { flex: none; padding: 14px 20px 0; }
.kpa-search-row { display: flex; gap: 8px; }
.kpa-search { position: relative; flex: 1 1 auto; min-width: 0; }
.kpa-search > .icon { position: absolute; top: 50%; left: 12px; margin-top: -.625em; color: var(--muted); pointer-events: none; }
.kpa-search > input { padding-left: 42px; }
.kpa-custom-button { flex: none; }

/* The line under the tabs runs from edge to edge of the pop-up. */
.kpa-tabs .tab-list { margin: 10px -20px 0; padding: 0 20px; }
.kpa-count {
  display: inline-block; min-width: 1.7em; padding: 1px 6px; border-radius: 999px;
  background: var(--soft); color: var(--ink);
  font: 600 .75rem/1.5 var(--body); font-variant-numeric: tabular-nums; text-align: center;
}
.tab.is-empty .kpa-count { background: none; color: var(--muted); }

.kpa-scroll {
  position: relative; flex: 1 1 auto; min-height: 0; padding: 16px 20px 24px;
  overflow-y: auto; overscroll-behavior: contain;
}
.kpa-panel { padding-top: 0; }
.kpa-status { margin: 0 0 10px; color: var(--muted); font-size: .88rem; }
.kpa-twins { margin: 0 0 12px; font-size: .92rem; }

/* ---- The cards ------------------------------------------------------------------ */
.kpa-list {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(330px, 1fr)); gap: 10px;
  margin: 0; padding: 0; list-style: none;
}
.kpa-group { grid-column: 1 / -1; margin-top: 12px; }
.kpa-group:first-child { margin-top: 0; }
.kpa-group-title { margin: 0; font-size: .95rem; letter-spacing: 0; }

.kpa-card {
  display: flex; flex-direction: column; min-width: 0;
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--card);
  transition: border-color .15s;
}
.kpa-card:hover { border-color: var(--ink); }
.kpa-card.is-current { border-color: var(--brand); background: var(--soft); box-shadow: inset 0 0 0 1px var(--brand); }
.kpa-card-main {
  display: grid; flex: 1 1 auto; grid-template-columns: 72px minmax(0, 1fr); gap: 12px; align-items: start;
  width: 100%; min-height: 44px; padding: 10px;
  border: 0; border-radius: inherit; background: none; color: var(--ink);
  font: inherit; text-align: left; cursor: pointer; -webkit-tap-highlight-color: transparent;
}

/* The little picture of the sheet, on the same grey "desk" the big previews use. */
.kpa-thumb {
  display: grid; place-items: center; width: 72px; height: 88px;
  border-radius: var(--radius-small); background: var(--desk);
}
.kpa-thumb > svg { display: block; }
.kpa-thumb-small { flex: none; width: 46px; height: 56px; }

.kpa-text { display: block; min-width: 0; }
.kpa-text > span { display: block; }
.kpa-size { font: 700 1.02rem/1.3 var(--display); overflow-wrap: anywhere; }
.kpa-badge { margin-left: 4px; border-color: var(--link); color: var(--link); }
.kpa-name { color: var(--muted); font-size: .88rem; line-height: 1.35; overflow-wrap: anywhere; }
.kpa-layout { margin-top: 7px; font-size: .88rem; line-height: 1.5; }
/* How many labels are on the sheet is what tells two sheets of the same size apart, so it is the loudest thing after the size. */
.kpa-per {
  display: inline-block; margin-right: 4px; padding: 0 7px; border-radius: 3px;
  background: var(--ink); color: var(--paper); font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap;
}
.kpa-grid-words { white-space: nowrap; }
.kpa-fine { margin-top: 3px; color: var(--muted); font-size: .8rem; line-height: 1.4; }
.kpa-text > .kpa-compat { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; overflow: hidden; }
.kpa-text > .kpa-check {
  display: flex; align-items: flex-start; gap: 5px; margin-top: 4px;
  color: var(--warn); font-size: .8rem; font-weight: 600; line-height: 1.4;
}
.kpa-check > .icon { width: 1.15em; height: 1.15em; margin-top: .12em; }
.kpa-text > .kpa-check-quiet { color: var(--muted); font-weight: 500; }

.kpa-card-tools { display: flex; gap: 4px; margin-top: -4px; padding: 0 6px 6px 90px; }

.kpa-more { margin: 14px 0 0; text-align: center; }
.kpa-foot { margin: 18px 0 0; color: var(--muted); font-size: .92rem; text-align: center; }
.kpa-empty { margin: 8px 0 0; }
.kpa-empty > p { margin: 0 0 10px; }
.kpa-empty-actions { justify-content: center; }
/* A link inside a sentence still gets a finger-sized place to tap. */
.kpa-foot .link-button, .kpa-elsewhere .link-button { display: inline-block; padding: 10px 2px; }

/* ---- The custom-size form --------------------------------------------------------- */
/* The form lays itself out by its OWN width (a "container"), not the window's: it is used in the
   pop-up and also straight on the Label paper page, where its column may be narrow on a wide screen. */
.kpa-form { --kpa-gutter: 20px; display: flex; flex: 1 1 auto; flex-direction: column; min-height: 0; container: kpaform / inline-size; }
.kpa-form:focus { outline: none; }
.kpa-form-scroll { flex: 1 1 auto; min-height: 0; padding: 18px var(--kpa-gutter) 8px; overflow-y: auto; overscroll-behavior: contain; }
.kpa-form-cols { display: grid; grid-template-columns: minmax(0, 1fr) minmax(280px, 380px); gap: 0 28px; align-items: start; }
.kpa-fields { grid-column: 1; grid-row: 1; min-width: 0; }
.kpa-preview { position: sticky; top: 0; grid-column: 2; grid-row: 1; min-width: 0; }

.kpa-drawing {
  display: grid; place-items: center; min-height: 190px; padding: 8px;
  border: 1px solid var(--desk-line); border-radius: var(--radius); background: var(--desk); overflow: hidden;
}
.kpa-drawing.is-stale > svg { opacity: .3; }
.kpa-detail { display: block; max-width: 100%; height: auto; }
.kpa-dim-line { fill: none; stroke: var(--ink); stroke-width: 1; }
.kpa-dim-ext { fill: none; stroke: var(--muted); stroke-width: 1; stroke-dasharray: 2 3; }
.kpa-dim text { fill: var(--ink); font: 600 11px/1 var(--body); font-variant-numeric: tabular-nums; }
.kpa-dim.is-active .kpa-dim-line { stroke: var(--link); stroke-width: 2; }
.kpa-dim.is-active .kpa-dim-ext { stroke: var(--link); }
.kpa-dim.is-active text { fill: var(--link); font-weight: 800; }
.kpa-fade-from { stop-color: var(--desk); stop-opacity: 0; }
.kpa-fade-to { stop-color: var(--desk); stop-opacity: 1; }

.kpa-sum { display: flex; align-items: center; gap: 12px; min-height: 56px; margin-top: 10px; }
.kpa-sum-text { display: grid; min-width: 0; line-height: 1.4; }
.kpa-sum-text > span { color: var(--muted); font-size: .88rem; }
.kpa-sum-problem { margin: 0; color: var(--error); font-size: .9rem; font-weight: 600; line-height: 1.4; }
.kpa-warnings { margin: 8px 0 0; padding: 0 0 0 18px; color: var(--muted); font-size: .82rem; line-height: 1.45; }
.kpa-warnings > li + li { margin-top: 4px; }

/* Each group of boxes has a rule above it and its name on that rule's left. */
.kpa-fields fieldset { margin: 4px 0 0; padding: 14px 0 0; border-top: 1px solid var(--line); }
.kpa-fields legend { float: left; width: 100%; margin: 0 0 10px; font-size: 1rem; font-weight: 700; }
.kpa-fields legend + * { clear: both; }
.kpa-field { margin-bottom: 12px; }
.kpa-unit { color: var(--muted); font-weight: 400; }
.kpa-stepper { display: flex; width: 100%; }
.kpa-stepper > input { flex: 1 1 auto; width: 0; min-width: 0; padding-right: 4px; padding-left: 4px; }
.kpa-hint { margin: -4px 0 12px; }

.kpa-form-actions { align-items: center; }
.kpa-form-note { flex: 1 1 180px; margin: 0; color: var(--muted); font-size: .85rem; line-height: 1.4; }
.kpa-form-note.is-error { color: var(--error); font-weight: 600; }

/* The form on a page of its own (the Label paper page): no pop-up around it. */
.kpa-form.kpa-form-page { --kpa-gutter: 0px; }
.kpa-form-page .kpa-form-scroll { padding-top: 0; overflow: visible; }
.kpa-form-page .kpa-form-actions { margin-top: 12px; padding: 14px 0 0; }
.kpa-form-page .kpa-preview { top: calc(var(--header-h, 60px) + 12px); }

/* A narrow form (a phone, or a narrow column): the drawing on top, the boxes under it. */
@container kpaform (max-width: 680px) {
  .kpa-form-cols { grid-template-columns: minmax(0, 1fr); }
  .kpa-preview { display: contents; }
  .kpa-preview > *, .kpa-fields { grid-column: 1; grid-row: auto; }
  .kpa-pin { padding-bottom: 10px; background: var(--card); }
  .kpa-drawing { min-height: 0; }
  .kpa-sum { min-height: 0; margin: 8px 0 0; }
  .kpa-sum .kpa-thumb { display: none; }
  .kpa-sum-text { display: block; }
  .kpa-sum-text > strong { margin-right: 8px; }
  .kpa-warnings { margin: 0 0 4px; }
  .kpa-fields { margin-top: 12px; }
  /* In the pop-up the drawing reaches the pop-up's edges, so the boxes can slide under it cleanly. */
  .kpa-dialog .kpa-form-scroll { padding-top: 0; }
  .kpa-dialog .kpa-pin { margin: 0 calc(-1 * var(--kpa-gutter)); padding: 14px var(--kpa-gutter) 10px; border-bottom: 1px solid var(--line); }
}
/* On a tall enough phone the drawing stays in view while the boxes scroll under it. */
@media (min-height: 700px) {
  @container kpaform (max-width: 680px) {
    .kpa-dialog .kpa-pin { position: sticky; top: 0; z-index: 2; }
  }
}

/* ---- The small control on a tool page ------------------------------------------------ */
.kpa-button {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 12px; padding: 10px;
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--card);
  cursor: pointer; transition: border-color .15s;
}
.kpa-button:hover { border-color: var(--ink); }
.kpa-button-thumb { flex: none; width: 52px; height: 64px; }
.kpa-button-text { display: grid; flex: 1 1 130px; gap: 1px; min-width: 0; line-height: 1.4; }
.kpa-button-title { overflow-wrap: anywhere; }
.kpa-button-desc { color: var(--muted); font-size: .9rem; }
.kpa-button-desc > span { white-space: nowrap; }
.kpa-button-compat { color: var(--muted); font-size: .8rem; }
.kpa-button-change { flex: none; margin-left: auto; }

/* ---- Phones ------------------------------------------------------------------------- */
@media (max-width: 560px) {
  .kpa-dialog { height: 92dvh; }
  .kpa-top { padding: 12px 16px 0; }
  .kpa-tabs .tab-list { gap: 0; margin: 8px -16px 0; padding: 0 10px; }
  .kpa-tabs .tab { gap: 5px; padding-right: 6px; padding-left: 6px; font-size: .9rem; }
  .kpa-count { min-width: 1.4em; padding: 1px 5px; }
  .kpa-wide { display: none; }
  .kpa-scroll { padding: 14px 16px calc(20px + env(safe-area-inset-bottom)); }
  .kpa-list { grid-template-columns: minmax(0, 1fr); }
  .kpa-form { --kpa-gutter: 16px; }
  .kpa-form-note { flex-basis: 100%; }
}
/* A phone held sideways: the search box and the tabs scroll away with the list, to leave it some room. */
@media (max-height: 520px) {
  .kpa-browse { overflow-y: auto; overscroll-behavior: contain; }
  .kpa-scroll { flex: none; overflow: visible; }
}
@media (max-width: 400px) {
  .kpa-custom-button { padding-right: 12px; padding-left: 12px; }
}
@media (prefers-reduced-motion: reduce) {
  .kpa-card, .kpa-button { transition: none; }
}
/* Windows high-contrast mode: the chosen card still needs a mark that does not rely on a tint. */
@media (forced-colors: active) {
  .kpa-card.is-current { outline: 2px solid Highlight; }
  .kpa-per { border: 1px solid CanvasText; }
}
